]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(artist-system): Allow make-temp-file to use temporary-file-directory.
[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, 2009 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 (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
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 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1353 {
1354 /* We have reached CHARPOS, or passed it. How the call to
1355 move_it_to can overshoot: (i) If CHARPOS is on invisible
1356 text, move_it_to stops at the end of the invisible text,
1357 after CHARPOS. (ii) If CHARPOS is in a display vector,
1358 move_it_to stops on its last glyph. */
1359 int top_x = it.current_x;
1360 int top_y = it.current_y;
1361 enum it_method it_method = it.method;
1362 /* Calling line_bottom_y may change it.method. */
1363 int bottom_y = (last_height = 0, line_bottom_y (&it));
1364 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1365
1366 if (top_y < window_top_y)
1367 visible_p = bottom_y > window_top_y;
1368 else if (top_y < it.last_visible_y)
1369 visible_p = 1;
1370 if (visible_p)
1371 {
1372 if (it_method == GET_FROM_BUFFER)
1373 {
1374 Lisp_Object window, prop;
1375
1376 XSETWINDOW (window, w);
1377 prop = Fget_char_property (make_number (it.position.charpos),
1378 Qinvisible, window);
1379
1380 /* If charpos coincides with invisible text covered with an
1381 ellipsis, use the first glyph of the ellipsis to compute
1382 the pixel positions. */
1383 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1384 {
1385 struct glyph_row *row = it.glyph_row;
1386 struct glyph *glyph = row->glyphs[TEXT_AREA];
1387 struct glyph *end = glyph + row->used[TEXT_AREA];
1388 int x = row->x;
1389
1390 for (; glyph < end
1391 && (!BUFFERP (glyph->object)
1392 || glyph->charpos < charpos);
1393 glyph++)
1394 x += glyph->pixel_width;
1395 top_x = x;
1396 }
1397 }
1398 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1399 {
1400 /* We stopped on the last glyph of a display vector.
1401 Try and recompute. Hack alert! */
1402 if (charpos < 2 || top.charpos >= charpos)
1403 top_x = it.glyph_row->x;
1404 else
1405 {
1406 struct it it2;
1407 start_display (&it2, w, top);
1408 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1409 get_next_display_element (&it2);
1410 PRODUCE_GLYPHS (&it2);
1411 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1412 || it2.current_x > it2.last_visible_x)
1413 top_x = it.glyph_row->x;
1414 else
1415 {
1416 top_x = it2.current_x;
1417 top_y = it2.current_y;
1418 }
1419 }
1420 }
1421
1422 *x = top_x;
1423 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1424 *rtop = max (0, window_top_y - top_y);
1425 *rbot = max (0, bottom_y - it.last_visible_y);
1426 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1427 - max (top_y, window_top_y)));
1428 *vpos = it.vpos;
1429 }
1430 }
1431 else
1432 {
1433 struct it it2;
1434
1435 it2 = it;
1436 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1437 move_it_by_lines (&it, 1, 0);
1438 if (charpos < IT_CHARPOS (it)
1439 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1440 {
1441 visible_p = 1;
1442 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1443 *x = it2.current_x;
1444 *y = it2.current_y + it2.max_ascent - it2.ascent;
1445 *rtop = max (0, -it2.current_y);
1446 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1447 - it.last_visible_y));
1448 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1449 it.last_visible_y)
1450 - max (it2.current_y,
1451 WINDOW_HEADER_LINE_HEIGHT (w))));
1452 *vpos = it2.vpos;
1453 }
1454 }
1455
1456 if (old_buffer)
1457 set_buffer_internal_1 (old_buffer);
1458
1459 current_header_line_height = current_mode_line_height = -1;
1460
1461 if (visible_p && XFASTINT (w->hscroll) > 0)
1462 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1463
1464 #if 0
1465 /* Debugging code. */
1466 if (visible_p)
1467 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1468 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1469 else
1470 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1471 #endif
1472
1473 return visible_p;
1474 }
1475
1476
1477 /* Return the next character from STR which is MAXLEN bytes long.
1478 Return in *LEN the length of the character. This is like
1479 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1480 we find one, we return a `?', but with the length of the invalid
1481 character. */
1482
1483 static INLINE int
1484 string_char_and_length (str, maxlen, len)
1485 const unsigned char *str;
1486 int maxlen, *len;
1487 {
1488 int c;
1489
1490 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1491 if (!CHAR_VALID_P (c, 1))
1492 /* We may not change the length here because other places in Emacs
1493 don't use this function, i.e. they silently accept invalid
1494 characters. */
1495 c = '?';
1496
1497 return c;
1498 }
1499
1500
1501
1502 /* Given a position POS containing a valid character and byte position
1503 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1504
1505 static struct text_pos
1506 string_pos_nchars_ahead (pos, string, nchars)
1507 struct text_pos pos;
1508 Lisp_Object string;
1509 int nchars;
1510 {
1511 xassert (STRINGP (string) && nchars >= 0);
1512
1513 if (STRING_MULTIBYTE (string))
1514 {
1515 int rest = SBYTES (string) - BYTEPOS (pos);
1516 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1517 int len;
1518
1519 while (nchars--)
1520 {
1521 string_char_and_length (p, rest, &len);
1522 p += len, rest -= len;
1523 xassert (rest >= 0);
1524 CHARPOS (pos) += 1;
1525 BYTEPOS (pos) += len;
1526 }
1527 }
1528 else
1529 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1530
1531 return pos;
1532 }
1533
1534
1535 /* Value is the text position, i.e. character and byte position,
1536 for character position CHARPOS in STRING. */
1537
1538 static INLINE struct text_pos
1539 string_pos (charpos, string)
1540 int charpos;
1541 Lisp_Object string;
1542 {
1543 struct text_pos pos;
1544 xassert (STRINGP (string));
1545 xassert (charpos >= 0);
1546 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1547 return pos;
1548 }
1549
1550
1551 /* Value is a text position, i.e. character and byte position, for
1552 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1553 means recognize multibyte characters. */
1554
1555 static struct text_pos
1556 c_string_pos (charpos, s, multibyte_p)
1557 int charpos;
1558 unsigned char *s;
1559 int multibyte_p;
1560 {
1561 struct text_pos pos;
1562
1563 xassert (s != NULL);
1564 xassert (charpos >= 0);
1565
1566 if (multibyte_p)
1567 {
1568 int rest = strlen (s), len;
1569
1570 SET_TEXT_POS (pos, 0, 0);
1571 while (charpos--)
1572 {
1573 string_char_and_length (s, rest, &len);
1574 s += len, rest -= len;
1575 xassert (rest >= 0);
1576 CHARPOS (pos) += 1;
1577 BYTEPOS (pos) += len;
1578 }
1579 }
1580 else
1581 SET_TEXT_POS (pos, charpos, charpos);
1582
1583 return pos;
1584 }
1585
1586
1587 /* Value is the number of characters in C string S. MULTIBYTE_P
1588 non-zero means recognize multibyte characters. */
1589
1590 static int
1591 number_of_chars (s, multibyte_p)
1592 unsigned char *s;
1593 int multibyte_p;
1594 {
1595 int nchars;
1596
1597 if (multibyte_p)
1598 {
1599 int rest = strlen (s), len;
1600 unsigned char *p = (unsigned char *) s;
1601
1602 for (nchars = 0; rest > 0; ++nchars)
1603 {
1604 string_char_and_length (p, rest, &len);
1605 rest -= len, p += len;
1606 }
1607 }
1608 else
1609 nchars = strlen (s);
1610
1611 return nchars;
1612 }
1613
1614
1615 /* Compute byte position NEWPOS->bytepos corresponding to
1616 NEWPOS->charpos. POS is a known position in string STRING.
1617 NEWPOS->charpos must be >= POS.charpos. */
1618
1619 static void
1620 compute_string_pos (newpos, pos, string)
1621 struct text_pos *newpos, pos;
1622 Lisp_Object string;
1623 {
1624 xassert (STRINGP (string));
1625 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1626
1627 if (STRING_MULTIBYTE (string))
1628 *newpos = string_pos_nchars_ahead (pos, string,
1629 CHARPOS (*newpos) - CHARPOS (pos));
1630 else
1631 BYTEPOS (*newpos) = CHARPOS (*newpos);
1632 }
1633
1634 /* EXPORT:
1635 Return an estimation of the pixel height of mode or top lines on
1636 frame F. FACE_ID specifies what line's height to estimate. */
1637
1638 int
1639 estimate_mode_line_height (f, face_id)
1640 struct frame *f;
1641 enum face_id face_id;
1642 {
1643 #ifdef HAVE_WINDOW_SYSTEM
1644 if (FRAME_WINDOW_P (f))
1645 {
1646 int height = FONT_HEIGHT (FRAME_FONT (f));
1647
1648 /* This function is called so early when Emacs starts that the face
1649 cache and mode line face are not yet initialized. */
1650 if (FRAME_FACE_CACHE (f))
1651 {
1652 struct face *face = FACE_FROM_ID (f, face_id);
1653 if (face)
1654 {
1655 if (face->font)
1656 height = FONT_HEIGHT (face->font);
1657 if (face->box_line_width > 0)
1658 height += 2 * face->box_line_width;
1659 }
1660 }
1661
1662 return height;
1663 }
1664 #endif
1665
1666 return 1;
1667 }
1668
1669 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1670 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1671 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1672 not force the value into range. */
1673
1674 void
1675 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1676 FRAME_PTR f;
1677 register int pix_x, pix_y;
1678 int *x, *y;
1679 NativeRectangle *bounds;
1680 int noclip;
1681 {
1682
1683 #ifdef HAVE_WINDOW_SYSTEM
1684 if (FRAME_WINDOW_P (f))
1685 {
1686 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1687 even for negative values. */
1688 if (pix_x < 0)
1689 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1690 if (pix_y < 0)
1691 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1692
1693 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1694 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1695
1696 if (bounds)
1697 STORE_NATIVE_RECT (*bounds,
1698 FRAME_COL_TO_PIXEL_X (f, pix_x),
1699 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1700 FRAME_COLUMN_WIDTH (f) - 1,
1701 FRAME_LINE_HEIGHT (f) - 1);
1702
1703 if (!noclip)
1704 {
1705 if (pix_x < 0)
1706 pix_x = 0;
1707 else if (pix_x > FRAME_TOTAL_COLS (f))
1708 pix_x = FRAME_TOTAL_COLS (f);
1709
1710 if (pix_y < 0)
1711 pix_y = 0;
1712 else if (pix_y > FRAME_LINES (f))
1713 pix_y = FRAME_LINES (f);
1714 }
1715 }
1716 #endif
1717
1718 *x = pix_x;
1719 *y = pix_y;
1720 }
1721
1722
1723 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1724 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1725 can't tell the positions because W's display is not up to date,
1726 return 0. */
1727
1728 int
1729 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1730 struct window *w;
1731 int hpos, vpos;
1732 int *frame_x, *frame_y;
1733 {
1734 #ifdef HAVE_WINDOW_SYSTEM
1735 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1736 {
1737 int success_p;
1738
1739 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1740 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1741
1742 if (display_completed)
1743 {
1744 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1745 struct glyph *glyph = row->glyphs[TEXT_AREA];
1746 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1747
1748 hpos = row->x;
1749 vpos = row->y;
1750 while (glyph < end)
1751 {
1752 hpos += glyph->pixel_width;
1753 ++glyph;
1754 }
1755
1756 /* If first glyph is partially visible, its first visible position is still 0. */
1757 if (hpos < 0)
1758 hpos = 0;
1759
1760 success_p = 1;
1761 }
1762 else
1763 {
1764 hpos = vpos = 0;
1765 success_p = 0;
1766 }
1767
1768 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1769 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1770 return success_p;
1771 }
1772 #endif
1773
1774 *frame_x = hpos;
1775 *frame_y = vpos;
1776 return 1;
1777 }
1778
1779
1780 #ifdef HAVE_WINDOW_SYSTEM
1781
1782 /* Find the glyph under window-relative coordinates X/Y in window W.
1783 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1784 strings. Return in *HPOS and *VPOS the row and column number of
1785 the glyph found. Return in *AREA the glyph area containing X.
1786 Value is a pointer to the glyph found or null if X/Y is not on
1787 text, or we can't tell because W's current matrix is not up to
1788 date. */
1789
1790 static
1791 struct glyph *
1792 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1793 struct window *w;
1794 int x, y;
1795 int *hpos, *vpos, *dx, *dy, *area;
1796 {
1797 struct glyph *glyph, *end;
1798 struct glyph_row *row = NULL;
1799 int x0, i;
1800
1801 /* Find row containing Y. Give up if some row is not enabled. */
1802 for (i = 0; i < w->current_matrix->nrows; ++i)
1803 {
1804 row = MATRIX_ROW (w->current_matrix, i);
1805 if (!row->enabled_p)
1806 return NULL;
1807 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1808 break;
1809 }
1810
1811 *vpos = i;
1812 *hpos = 0;
1813
1814 /* Give up if Y is not in the window. */
1815 if (i == w->current_matrix->nrows)
1816 return NULL;
1817
1818 /* Get the glyph area containing X. */
1819 if (w->pseudo_window_p)
1820 {
1821 *area = TEXT_AREA;
1822 x0 = 0;
1823 }
1824 else
1825 {
1826 if (x < window_box_left_offset (w, TEXT_AREA))
1827 {
1828 *area = LEFT_MARGIN_AREA;
1829 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1830 }
1831 else if (x < window_box_right_offset (w, TEXT_AREA))
1832 {
1833 *area = TEXT_AREA;
1834 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1835 }
1836 else
1837 {
1838 *area = RIGHT_MARGIN_AREA;
1839 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1840 }
1841 }
1842
1843 /* Find glyph containing X. */
1844 glyph = row->glyphs[*area];
1845 end = glyph + row->used[*area];
1846 x -= x0;
1847 while (glyph < end && x >= glyph->pixel_width)
1848 {
1849 x -= glyph->pixel_width;
1850 ++glyph;
1851 }
1852
1853 if (glyph == end)
1854 return NULL;
1855
1856 if (dx)
1857 {
1858 *dx = x;
1859 *dy = y - (row->y + row->ascent - glyph->ascent);
1860 }
1861
1862 *hpos = glyph - row->glyphs[*area];
1863 return glyph;
1864 }
1865
1866
1867 /* EXPORT:
1868 Convert frame-relative x/y to coordinates relative to window W.
1869 Takes pseudo-windows into account. */
1870
1871 void
1872 frame_to_window_pixel_xy (w, x, y)
1873 struct window *w;
1874 int *x, *y;
1875 {
1876 if (w->pseudo_window_p)
1877 {
1878 /* A pseudo-window is always full-width, and starts at the
1879 left edge of the frame, plus a frame border. */
1880 struct frame *f = XFRAME (w->frame);
1881 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1882 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1883 }
1884 else
1885 {
1886 *x -= WINDOW_LEFT_EDGE_X (w);
1887 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1888 }
1889 }
1890
1891 /* EXPORT:
1892 Return in RECTS[] at most N clipping rectangles for glyph string S.
1893 Return the number of stored rectangles. */
1894
1895 int
1896 get_glyph_string_clip_rects (s, rects, n)
1897 struct glyph_string *s;
1898 NativeRectangle *rects;
1899 int n;
1900 {
1901 XRectangle r;
1902
1903 if (n <= 0)
1904 return 0;
1905
1906 if (s->row->full_width_p)
1907 {
1908 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1909 r.x = WINDOW_LEFT_EDGE_X (s->w);
1910 r.width = WINDOW_TOTAL_WIDTH (s->w);
1911
1912 /* Unless displaying a mode or menu bar line, which are always
1913 fully visible, clip to the visible part of the row. */
1914 if (s->w->pseudo_window_p)
1915 r.height = s->row->visible_height;
1916 else
1917 r.height = s->height;
1918 }
1919 else
1920 {
1921 /* This is a text line that may be partially visible. */
1922 r.x = window_box_left (s->w, s->area);
1923 r.width = window_box_width (s->w, s->area);
1924 r.height = s->row->visible_height;
1925 }
1926
1927 if (s->clip_head)
1928 if (r.x < s->clip_head->x)
1929 {
1930 if (r.width >= s->clip_head->x - r.x)
1931 r.width -= s->clip_head->x - r.x;
1932 else
1933 r.width = 0;
1934 r.x = s->clip_head->x;
1935 }
1936 if (s->clip_tail)
1937 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1938 {
1939 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1940 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1941 else
1942 r.width = 0;
1943 }
1944
1945 /* If S draws overlapping rows, it's sufficient to use the top and
1946 bottom of the window for clipping because this glyph string
1947 intentionally draws over other lines. */
1948 if (s->for_overlaps)
1949 {
1950 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1951 r.height = window_text_bottom_y (s->w) - r.y;
1952
1953 /* Alas, the above simple strategy does not work for the
1954 environments with anti-aliased text: if the same text is
1955 drawn onto the same place multiple times, it gets thicker.
1956 If the overlap we are processing is for the erased cursor, we
1957 take the intersection with the rectagle of the cursor. */
1958 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1959 {
1960 XRectangle rc, r_save = r;
1961
1962 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1963 rc.y = s->w->phys_cursor.y;
1964 rc.width = s->w->phys_cursor_width;
1965 rc.height = s->w->phys_cursor_height;
1966
1967 x_intersect_rectangles (&r_save, &rc, &r);
1968 }
1969 }
1970 else
1971 {
1972 /* Don't use S->y for clipping because it doesn't take partially
1973 visible lines into account. For example, it can be negative for
1974 partially visible lines at the top of a window. */
1975 if (!s->row->full_width_p
1976 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1977 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1978 else
1979 r.y = max (0, s->row->y);
1980
1981 /* If drawing a tool-bar window, draw it over the internal border
1982 at the top of the window. */
1983 if (WINDOWP (s->f->tool_bar_window)
1984 && s->w == XWINDOW (s->f->tool_bar_window))
1985 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1986 }
1987
1988 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1989
1990 /* If drawing the cursor, don't let glyph draw outside its
1991 advertised boundaries. Cleartype does this under some circumstances. */
1992 if (s->hl == DRAW_CURSOR)
1993 {
1994 struct glyph *glyph = s->first_glyph;
1995 int height, max_y;
1996
1997 if (s->x > r.x)
1998 {
1999 r.width -= s->x - r.x;
2000 r.x = s->x;
2001 }
2002 r.width = min (r.width, glyph->pixel_width);
2003
2004 /* If r.y is below window bottom, ensure that we still see a cursor. */
2005 height = min (glyph->ascent + glyph->descent,
2006 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2007 max_y = window_text_bottom_y (s->w) - height;
2008 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2009 if (s->ybase - glyph->ascent > max_y)
2010 {
2011 r.y = max_y;
2012 r.height = height;
2013 }
2014 else
2015 {
2016 /* Don't draw cursor glyph taller than our actual glyph. */
2017 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2018 if (height < r.height)
2019 {
2020 max_y = r.y + r.height;
2021 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2022 r.height = min (max_y - r.y, height);
2023 }
2024 }
2025 }
2026
2027 if (s->row->clip)
2028 {
2029 XRectangle r_save = r;
2030
2031 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2032 r.width = 0;
2033 }
2034
2035 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2036 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2037 {
2038 #ifdef CONVERT_FROM_XRECT
2039 CONVERT_FROM_XRECT (r, *rects);
2040 #else
2041 *rects = r;
2042 #endif
2043 return 1;
2044 }
2045 else
2046 {
2047 /* If we are processing overlapping and allowed to return
2048 multiple clipping rectangles, we exclude the row of the glyph
2049 string from the clipping rectangle. This is to avoid drawing
2050 the same text on the environment with anti-aliasing. */
2051 #ifdef CONVERT_FROM_XRECT
2052 XRectangle rs[2];
2053 #else
2054 XRectangle *rs = rects;
2055 #endif
2056 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2057
2058 if (s->for_overlaps & OVERLAPS_PRED)
2059 {
2060 rs[i] = r;
2061 if (r.y + r.height > row_y)
2062 {
2063 if (r.y < row_y)
2064 rs[i].height = row_y - r.y;
2065 else
2066 rs[i].height = 0;
2067 }
2068 i++;
2069 }
2070 if (s->for_overlaps & OVERLAPS_SUCC)
2071 {
2072 rs[i] = r;
2073 if (r.y < row_y + s->row->visible_height)
2074 {
2075 if (r.y + r.height > row_y + s->row->visible_height)
2076 {
2077 rs[i].y = row_y + s->row->visible_height;
2078 rs[i].height = r.y + r.height - rs[i].y;
2079 }
2080 else
2081 rs[i].height = 0;
2082 }
2083 i++;
2084 }
2085
2086 n = i;
2087 #ifdef CONVERT_FROM_XRECT
2088 for (i = 0; i < n; i++)
2089 CONVERT_FROM_XRECT (rs[i], rects[i]);
2090 #endif
2091 return n;
2092 }
2093 }
2094
2095 /* EXPORT:
2096 Return in *NR the clipping rectangle for glyph string S. */
2097
2098 void
2099 get_glyph_string_clip_rect (s, nr)
2100 struct glyph_string *s;
2101 NativeRectangle *nr;
2102 {
2103 get_glyph_string_clip_rects (s, nr, 1);
2104 }
2105
2106
2107 /* EXPORT:
2108 Return the position and height of the phys cursor in window W.
2109 Set w->phys_cursor_width to width of phys cursor.
2110 */
2111
2112 void
2113 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2114 struct window *w;
2115 struct glyph_row *row;
2116 struct glyph *glyph;
2117 int *xp, *yp, *heightp;
2118 {
2119 struct frame *f = XFRAME (WINDOW_FRAME (w));
2120 int x, y, wd, h, h0, y0;
2121
2122 /* Compute the width of the rectangle to draw. If on a stretch
2123 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2124 rectangle as wide as the glyph, but use a canonical character
2125 width instead. */
2126 wd = glyph->pixel_width - 1;
2127 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2128 wd++; /* Why? */
2129 #endif
2130
2131 x = w->phys_cursor.x;
2132 if (x < 0)
2133 {
2134 wd += x;
2135 x = 0;
2136 }
2137
2138 if (glyph->type == STRETCH_GLYPH
2139 && !x_stretch_cursor_p)
2140 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2141 w->phys_cursor_width = wd;
2142
2143 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2144
2145 /* If y is below window bottom, ensure that we still see a cursor. */
2146 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2147
2148 h = max (h0, glyph->ascent + glyph->descent);
2149 h0 = min (h0, glyph->ascent + glyph->descent);
2150
2151 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2152 if (y < y0)
2153 {
2154 h = max (h - (y0 - y) + 1, h0);
2155 y = y0 - 1;
2156 }
2157 else
2158 {
2159 y0 = window_text_bottom_y (w) - h0;
2160 if (y > y0)
2161 {
2162 h += y - y0;
2163 y = y0;
2164 }
2165 }
2166
2167 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2168 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2169 *heightp = h;
2170 }
2171
2172 /*
2173 * Remember which glyph the mouse is over.
2174 */
2175
2176 void
2177 remember_mouse_glyph (f, gx, gy, rect)
2178 struct frame *f;
2179 int gx, gy;
2180 NativeRectangle *rect;
2181 {
2182 Lisp_Object window;
2183 struct window *w;
2184 struct glyph_row *r, *gr, *end_row;
2185 enum window_part part;
2186 enum glyph_row_area area;
2187 int x, y, width, height;
2188
2189 /* Try to determine frame pixel position and size of the glyph under
2190 frame pixel coordinates X/Y on frame F. */
2191
2192 if (!f->glyphs_initialized_p
2193 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2194 NILP (window)))
2195 {
2196 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2197 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2198 goto virtual_glyph;
2199 }
2200
2201 w = XWINDOW (window);
2202 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2203 height = WINDOW_FRAME_LINE_HEIGHT (w);
2204
2205 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2206 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2207
2208 if (w->pseudo_window_p)
2209 {
2210 area = TEXT_AREA;
2211 part = ON_MODE_LINE; /* Don't adjust margin. */
2212 goto text_glyph;
2213 }
2214
2215 switch (part)
2216 {
2217 case ON_LEFT_MARGIN:
2218 area = LEFT_MARGIN_AREA;
2219 goto text_glyph;
2220
2221 case ON_RIGHT_MARGIN:
2222 area = RIGHT_MARGIN_AREA;
2223 goto text_glyph;
2224
2225 case ON_HEADER_LINE:
2226 case ON_MODE_LINE:
2227 gr = (part == ON_HEADER_LINE
2228 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2229 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2230 gy = gr->y;
2231 area = TEXT_AREA;
2232 goto text_glyph_row_found;
2233
2234 case ON_TEXT:
2235 area = TEXT_AREA;
2236
2237 text_glyph:
2238 gr = 0; gy = 0;
2239 for (; r <= end_row && r->enabled_p; ++r)
2240 if (r->y + r->height > y)
2241 {
2242 gr = r; gy = r->y;
2243 break;
2244 }
2245
2246 text_glyph_row_found:
2247 if (gr && gy <= y)
2248 {
2249 struct glyph *g = gr->glyphs[area];
2250 struct glyph *end = g + gr->used[area];
2251
2252 height = gr->height;
2253 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2254 if (gx + g->pixel_width > x)
2255 break;
2256
2257 if (g < end)
2258 {
2259 if (g->type == IMAGE_GLYPH)
2260 {
2261 /* Don't remember when mouse is over image, as
2262 image may have hot-spots. */
2263 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2264 return;
2265 }
2266 width = g->pixel_width;
2267 }
2268 else
2269 {
2270 /* Use nominal char spacing at end of line. */
2271 x -= gx;
2272 gx += (x / width) * width;
2273 }
2274
2275 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2276 gx += window_box_left_offset (w, area);
2277 }
2278 else
2279 {
2280 /* Use nominal line height at end of window. */
2281 gx = (x / width) * width;
2282 y -= gy;
2283 gy += (y / height) * height;
2284 }
2285 break;
2286
2287 case ON_LEFT_FRINGE:
2288 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2289 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2290 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2291 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2292 goto row_glyph;
2293
2294 case ON_RIGHT_FRINGE:
2295 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2296 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2297 : window_box_right_offset (w, TEXT_AREA));
2298 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2299 goto row_glyph;
2300
2301 case ON_SCROLL_BAR:
2302 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2303 ? 0
2304 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2305 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2306 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2307 : 0)));
2308 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2309
2310 row_glyph:
2311 gr = 0, gy = 0;
2312 for (; r <= end_row && r->enabled_p; ++r)
2313 if (r->y + r->height > y)
2314 {
2315 gr = r; gy = r->y;
2316 break;
2317 }
2318
2319 if (gr && gy <= y)
2320 height = gr->height;
2321 else
2322 {
2323 /* Use nominal line height at end of window. */
2324 y -= gy;
2325 gy += (y / height) * height;
2326 }
2327 break;
2328
2329 default:
2330 ;
2331 virtual_glyph:
2332 /* If there is no glyph under the mouse, then we divide the screen
2333 into a grid of the smallest glyph in the frame, and use that
2334 as our "glyph". */
2335
2336 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2337 round down even for negative values. */
2338 if (gx < 0)
2339 gx -= width - 1;
2340 if (gy < 0)
2341 gy -= height - 1;
2342
2343 gx = (gx / width) * width;
2344 gy = (gy / height) * height;
2345
2346 goto store_rect;
2347 }
2348
2349 gx += WINDOW_LEFT_EDGE_X (w);
2350 gy += WINDOW_TOP_EDGE_Y (w);
2351
2352 store_rect:
2353 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2354
2355 /* Visible feedback for debugging. */
2356 #if 0
2357 #if HAVE_X_WINDOWS
2358 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2359 f->output_data.x->normal_gc,
2360 gx, gy, width, height);
2361 #endif
2362 #endif
2363 }
2364
2365
2366 #endif /* HAVE_WINDOW_SYSTEM */
2367
2368 \f
2369 /***********************************************************************
2370 Lisp form evaluation
2371 ***********************************************************************/
2372
2373 /* Error handler for safe_eval and safe_call. */
2374
2375 static Lisp_Object
2376 safe_eval_handler (arg)
2377 Lisp_Object arg;
2378 {
2379 add_to_log ("Error during redisplay: %s", arg, Qnil);
2380 return Qnil;
2381 }
2382
2383
2384 /* Evaluate SEXPR and return the result, or nil if something went
2385 wrong. Prevent redisplay during the evaluation. */
2386
2387 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2388 Return the result, or nil if something went wrong. Prevent
2389 redisplay during the evaluation. */
2390
2391 Lisp_Object
2392 safe_call (nargs, args)
2393 int nargs;
2394 Lisp_Object *args;
2395 {
2396 Lisp_Object val;
2397
2398 if (inhibit_eval_during_redisplay)
2399 val = Qnil;
2400 else
2401 {
2402 int count = SPECPDL_INDEX ();
2403 struct gcpro gcpro1;
2404
2405 GCPRO1 (args[0]);
2406 gcpro1.nvars = nargs;
2407 specbind (Qinhibit_redisplay, Qt);
2408 /* Use Qt to ensure debugger does not run,
2409 so there is no possibility of wanting to redisplay. */
2410 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2411 safe_eval_handler);
2412 UNGCPRO;
2413 val = unbind_to (count, val);
2414 }
2415
2416 return val;
2417 }
2418
2419
2420 /* Call function FN with one argument ARG.
2421 Return the result, or nil if something went wrong. */
2422
2423 Lisp_Object
2424 safe_call1 (fn, arg)
2425 Lisp_Object fn, arg;
2426 {
2427 Lisp_Object args[2];
2428 args[0] = fn;
2429 args[1] = arg;
2430 return safe_call (2, args);
2431 }
2432
2433 static Lisp_Object Qeval;
2434
2435 Lisp_Object
2436 safe_eval (Lisp_Object sexpr)
2437 {
2438 return safe_call1 (Qeval, sexpr);
2439 }
2440
2441 /* Call function FN with one argument ARG.
2442 Return the result, or nil if something went wrong. */
2443
2444 Lisp_Object
2445 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2446 {
2447 Lisp_Object args[3];
2448 args[0] = fn;
2449 args[1] = arg1;
2450 args[2] = arg2;
2451 return safe_call (3, args);
2452 }
2453
2454
2455 \f
2456 /***********************************************************************
2457 Debugging
2458 ***********************************************************************/
2459
2460 #if 0
2461
2462 /* Define CHECK_IT to perform sanity checks on iterators.
2463 This is for debugging. It is too slow to do unconditionally. */
2464
2465 static void
2466 check_it (it)
2467 struct it *it;
2468 {
2469 if (it->method == GET_FROM_STRING)
2470 {
2471 xassert (STRINGP (it->string));
2472 xassert (IT_STRING_CHARPOS (*it) >= 0);
2473 }
2474 else
2475 {
2476 xassert (IT_STRING_CHARPOS (*it) < 0);
2477 if (it->method == GET_FROM_BUFFER)
2478 {
2479 /* Check that character and byte positions agree. */
2480 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2481 }
2482 }
2483
2484 if (it->dpvec)
2485 xassert (it->current.dpvec_index >= 0);
2486 else
2487 xassert (it->current.dpvec_index < 0);
2488 }
2489
2490 #define CHECK_IT(IT) check_it ((IT))
2491
2492 #else /* not 0 */
2493
2494 #define CHECK_IT(IT) (void) 0
2495
2496 #endif /* not 0 */
2497
2498
2499 #if GLYPH_DEBUG
2500
2501 /* Check that the window end of window W is what we expect it
2502 to be---the last row in the current matrix displaying text. */
2503
2504 static void
2505 check_window_end (w)
2506 struct window *w;
2507 {
2508 if (!MINI_WINDOW_P (w)
2509 && !NILP (w->window_end_valid))
2510 {
2511 struct glyph_row *row;
2512 xassert ((row = MATRIX_ROW (w->current_matrix,
2513 XFASTINT (w->window_end_vpos)),
2514 !row->enabled_p
2515 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2516 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2517 }
2518 }
2519
2520 #define CHECK_WINDOW_END(W) check_window_end ((W))
2521
2522 #else /* not GLYPH_DEBUG */
2523
2524 #define CHECK_WINDOW_END(W) (void) 0
2525
2526 #endif /* not GLYPH_DEBUG */
2527
2528
2529 \f
2530 /***********************************************************************
2531 Iterator initialization
2532 ***********************************************************************/
2533
2534 /* Initialize IT for displaying current_buffer in window W, starting
2535 at character position CHARPOS. CHARPOS < 0 means that no buffer
2536 position is specified which is useful when the iterator is assigned
2537 a position later. BYTEPOS is the byte position corresponding to
2538 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2539
2540 If ROW is not null, calls to produce_glyphs with IT as parameter
2541 will produce glyphs in that row.
2542
2543 BASE_FACE_ID is the id of a base face to use. It must be one of
2544 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2545 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2546 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2547
2548 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2549 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2550 will be initialized to use the corresponding mode line glyph row of
2551 the desired matrix of W. */
2552
2553 void
2554 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2555 struct it *it;
2556 struct window *w;
2557 int charpos, bytepos;
2558 struct glyph_row *row;
2559 enum face_id base_face_id;
2560 {
2561 int highlight_region_p;
2562 enum face_id remapped_base_face_id = base_face_id;
2563
2564 /* Some precondition checks. */
2565 xassert (w != NULL && it != NULL);
2566 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2567 && charpos <= ZV));
2568
2569 /* If face attributes have been changed since the last redisplay,
2570 free realized faces now because they depend on face definitions
2571 that might have changed. Don't free faces while there might be
2572 desired matrices pending which reference these faces. */
2573 if (face_change_count && !inhibit_free_realized_faces)
2574 {
2575 face_change_count = 0;
2576 free_all_realized_faces (Qnil);
2577 }
2578
2579 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2580 if (! NILP (Vface_remapping_alist))
2581 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2582
2583 /* Use one of the mode line rows of W's desired matrix if
2584 appropriate. */
2585 if (row == NULL)
2586 {
2587 if (base_face_id == MODE_LINE_FACE_ID
2588 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2589 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2590 else if (base_face_id == HEADER_LINE_FACE_ID)
2591 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2592 }
2593
2594 /* Clear IT. */
2595 bzero (it, sizeof *it);
2596 it->current.overlay_string_index = -1;
2597 it->current.dpvec_index = -1;
2598 it->base_face_id = remapped_base_face_id;
2599 it->string = Qnil;
2600 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2601
2602 /* The window in which we iterate over current_buffer: */
2603 XSETWINDOW (it->window, w);
2604 it->w = w;
2605 it->f = XFRAME (w->frame);
2606
2607 it->cmp_it.id = -1;
2608
2609 /* Extra space between lines (on window systems only). */
2610 if (base_face_id == DEFAULT_FACE_ID
2611 && FRAME_WINDOW_P (it->f))
2612 {
2613 if (NATNUMP (current_buffer->extra_line_spacing))
2614 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2615 else if (FLOATP (current_buffer->extra_line_spacing))
2616 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2617 * FRAME_LINE_HEIGHT (it->f));
2618 else if (it->f->extra_line_spacing > 0)
2619 it->extra_line_spacing = it->f->extra_line_spacing;
2620 it->max_extra_line_spacing = 0;
2621 }
2622
2623 /* If realized faces have been removed, e.g. because of face
2624 attribute changes of named faces, recompute them. When running
2625 in batch mode, the face cache of the initial frame is null. If
2626 we happen to get called, make a dummy face cache. */
2627 if (FRAME_FACE_CACHE (it->f) == NULL)
2628 init_frame_faces (it->f);
2629 if (FRAME_FACE_CACHE (it->f)->used == 0)
2630 recompute_basic_faces (it->f);
2631
2632 /* Current value of the `slice', `space-width', and 'height' properties. */
2633 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2634 it->space_width = Qnil;
2635 it->font_height = Qnil;
2636 it->override_ascent = -1;
2637
2638 /* Are control characters displayed as `^C'? */
2639 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2640
2641 /* -1 means everything between a CR and the following line end
2642 is invisible. >0 means lines indented more than this value are
2643 invisible. */
2644 it->selective = (INTEGERP (current_buffer->selective_display)
2645 ? XFASTINT (current_buffer->selective_display)
2646 : (!NILP (current_buffer->selective_display)
2647 ? -1 : 0));
2648 it->selective_display_ellipsis_p
2649 = !NILP (current_buffer->selective_display_ellipses);
2650
2651 /* Display table to use. */
2652 it->dp = window_display_table (w);
2653
2654 /* Are multibyte characters enabled in current_buffer? */
2655 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2656
2657 /* Non-zero if we should highlight the region. */
2658 highlight_region_p
2659 = (!NILP (Vtransient_mark_mode)
2660 && !NILP (current_buffer->mark_active)
2661 && XMARKER (current_buffer->mark)->buffer != 0);
2662
2663 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2664 start and end of a visible region in window IT->w. Set both to
2665 -1 to indicate no region. */
2666 if (highlight_region_p
2667 /* Maybe highlight only in selected window. */
2668 && (/* Either show region everywhere. */
2669 highlight_nonselected_windows
2670 /* Or show region in the selected window. */
2671 || w == XWINDOW (selected_window)
2672 /* Or show the region if we are in the mini-buffer and W is
2673 the window the mini-buffer refers to. */
2674 || (MINI_WINDOW_P (XWINDOW (selected_window))
2675 && WINDOWP (minibuf_selected_window)
2676 && w == XWINDOW (minibuf_selected_window))))
2677 {
2678 int charpos = marker_position (current_buffer->mark);
2679 it->region_beg_charpos = min (PT, charpos);
2680 it->region_end_charpos = max (PT, charpos);
2681 }
2682 else
2683 it->region_beg_charpos = it->region_end_charpos = -1;
2684
2685 /* Get the position at which the redisplay_end_trigger hook should
2686 be run, if it is to be run at all. */
2687 if (MARKERP (w->redisplay_end_trigger)
2688 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2689 it->redisplay_end_trigger_charpos
2690 = marker_position (w->redisplay_end_trigger);
2691 else if (INTEGERP (w->redisplay_end_trigger))
2692 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2693
2694 /* Correct bogus values of tab_width. */
2695 it->tab_width = XINT (current_buffer->tab_width);
2696 if (it->tab_width <= 0 || it->tab_width > 1000)
2697 it->tab_width = 8;
2698
2699 /* Are lines in the display truncated? */
2700 if (base_face_id != DEFAULT_FACE_ID
2701 || XINT (it->w->hscroll)
2702 || (! WINDOW_FULL_WIDTH_P (it->w)
2703 && ((!NILP (Vtruncate_partial_width_windows)
2704 && !INTEGERP (Vtruncate_partial_width_windows))
2705 || (INTEGERP (Vtruncate_partial_width_windows)
2706 && (WINDOW_TOTAL_COLS (it->w)
2707 < XINT (Vtruncate_partial_width_windows))))))
2708 it->line_wrap = TRUNCATE;
2709 else if (NILP (current_buffer->truncate_lines))
2710 it->line_wrap = NILP (current_buffer->word_wrap)
2711 ? WINDOW_WRAP : WORD_WRAP;
2712 else
2713 it->line_wrap = TRUNCATE;
2714
2715 /* Get dimensions of truncation and continuation glyphs. These are
2716 displayed as fringe bitmaps under X, so we don't need them for such
2717 frames. */
2718 if (!FRAME_WINDOW_P (it->f))
2719 {
2720 if (it->line_wrap == TRUNCATE)
2721 {
2722 /* We will need the truncation glyph. */
2723 xassert (it->glyph_row == NULL);
2724 produce_special_glyphs (it, IT_TRUNCATION);
2725 it->truncation_pixel_width = it->pixel_width;
2726 }
2727 else
2728 {
2729 /* We will need the continuation glyph. */
2730 xassert (it->glyph_row == NULL);
2731 produce_special_glyphs (it, IT_CONTINUATION);
2732 it->continuation_pixel_width = it->pixel_width;
2733 }
2734
2735 /* Reset these values to zero because the produce_special_glyphs
2736 above has changed them. */
2737 it->pixel_width = it->ascent = it->descent = 0;
2738 it->phys_ascent = it->phys_descent = 0;
2739 }
2740
2741 /* Set this after getting the dimensions of truncation and
2742 continuation glyphs, so that we don't produce glyphs when calling
2743 produce_special_glyphs, above. */
2744 it->glyph_row = row;
2745 it->area = TEXT_AREA;
2746
2747 /* Get the dimensions of the display area. The display area
2748 consists of the visible window area plus a horizontally scrolled
2749 part to the left of the window. All x-values are relative to the
2750 start of this total display area. */
2751 if (base_face_id != DEFAULT_FACE_ID)
2752 {
2753 /* Mode lines, menu bar in terminal frames. */
2754 it->first_visible_x = 0;
2755 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2756 }
2757 else
2758 {
2759 it->first_visible_x
2760 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2761 it->last_visible_x = (it->first_visible_x
2762 + window_box_width (w, TEXT_AREA));
2763
2764 /* If we truncate lines, leave room for the truncator glyph(s) at
2765 the right margin. Otherwise, leave room for the continuation
2766 glyph(s). Truncation and continuation glyphs are not inserted
2767 for window-based redisplay. */
2768 if (!FRAME_WINDOW_P (it->f))
2769 {
2770 if (it->line_wrap == TRUNCATE)
2771 it->last_visible_x -= it->truncation_pixel_width;
2772 else
2773 it->last_visible_x -= it->continuation_pixel_width;
2774 }
2775
2776 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2777 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2778 }
2779
2780 /* Leave room for a border glyph. */
2781 if (!FRAME_WINDOW_P (it->f)
2782 && !WINDOW_RIGHTMOST_P (it->w))
2783 it->last_visible_x -= 1;
2784
2785 it->last_visible_y = window_text_bottom_y (w);
2786
2787 /* For mode lines and alike, arrange for the first glyph having a
2788 left box line if the face specifies a box. */
2789 if (base_face_id != DEFAULT_FACE_ID)
2790 {
2791 struct face *face;
2792
2793 it->face_id = remapped_base_face_id;
2794
2795 /* If we have a boxed mode line, make the first character appear
2796 with a left box line. */
2797 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2798 if (face->box != FACE_NO_BOX)
2799 it->start_of_box_run_p = 1;
2800 }
2801
2802 /* If a buffer position was specified, set the iterator there,
2803 getting overlays and face properties from that position. */
2804 if (charpos >= BUF_BEG (current_buffer))
2805 {
2806 it->end_charpos = ZV;
2807 it->face_id = -1;
2808 IT_CHARPOS (*it) = charpos;
2809
2810 /* Compute byte position if not specified. */
2811 if (bytepos < charpos)
2812 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2813 else
2814 IT_BYTEPOS (*it) = bytepos;
2815
2816 it->start = it->current;
2817
2818 /* Compute faces etc. */
2819 reseat (it, it->current.pos, 1);
2820 }
2821
2822 CHECK_IT (it);
2823 }
2824
2825
2826 /* Initialize IT for the display of window W with window start POS. */
2827
2828 void
2829 start_display (it, w, pos)
2830 struct it *it;
2831 struct window *w;
2832 struct text_pos pos;
2833 {
2834 struct glyph_row *row;
2835 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2836
2837 row = w->desired_matrix->rows + first_vpos;
2838 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2839 it->first_vpos = first_vpos;
2840
2841 /* Don't reseat to previous visible line start if current start
2842 position is in a string or image. */
2843 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2844 {
2845 int start_at_line_beg_p;
2846 int first_y = it->current_y;
2847
2848 /* If window start is not at a line start, skip forward to POS to
2849 get the correct continuation lines width. */
2850 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2851 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2852 if (!start_at_line_beg_p)
2853 {
2854 int new_x;
2855
2856 reseat_at_previous_visible_line_start (it);
2857 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2858
2859 new_x = it->current_x + it->pixel_width;
2860
2861 /* If lines are continued, this line may end in the middle
2862 of a multi-glyph character (e.g. a control character
2863 displayed as \003, or in the middle of an overlay
2864 string). In this case move_it_to above will not have
2865 taken us to the start of the continuation line but to the
2866 end of the continued line. */
2867 if (it->current_x > 0
2868 && it->line_wrap != TRUNCATE /* Lines are continued. */
2869 && (/* And glyph doesn't fit on the line. */
2870 new_x > it->last_visible_x
2871 /* Or it fits exactly and we're on a window
2872 system frame. */
2873 || (new_x == it->last_visible_x
2874 && FRAME_WINDOW_P (it->f))))
2875 {
2876 if (it->current.dpvec_index >= 0
2877 || it->current.overlay_string_index >= 0)
2878 {
2879 set_iterator_to_next (it, 1);
2880 move_it_in_display_line_to (it, -1, -1, 0);
2881 }
2882
2883 it->continuation_lines_width += it->current_x;
2884 }
2885
2886 /* We're starting a new display line, not affected by the
2887 height of the continued line, so clear the appropriate
2888 fields in the iterator structure. */
2889 it->max_ascent = it->max_descent = 0;
2890 it->max_phys_ascent = it->max_phys_descent = 0;
2891
2892 it->current_y = first_y;
2893 it->vpos = 0;
2894 it->current_x = it->hpos = 0;
2895 }
2896 }
2897
2898 #if 0 /* Don't assert the following because start_display is sometimes
2899 called intentionally with a window start that is not at a
2900 line start. Please leave this code in as a comment. */
2901
2902 /* Window start should be on a line start, now. */
2903 xassert (it->continuation_lines_width
2904 || IT_CHARPOS (it) == BEGV
2905 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2906 #endif /* 0 */
2907 }
2908
2909
2910 /* Return 1 if POS is a position in ellipses displayed for invisible
2911 text. W is the window we display, for text property lookup. */
2912
2913 static int
2914 in_ellipses_for_invisible_text_p (pos, w)
2915 struct display_pos *pos;
2916 struct window *w;
2917 {
2918 Lisp_Object prop, window;
2919 int ellipses_p = 0;
2920 int charpos = CHARPOS (pos->pos);
2921
2922 /* If POS specifies a position in a display vector, this might
2923 be for an ellipsis displayed for invisible text. We won't
2924 get the iterator set up for delivering that ellipsis unless
2925 we make sure that it gets aware of the invisible text. */
2926 if (pos->dpvec_index >= 0
2927 && pos->overlay_string_index < 0
2928 && CHARPOS (pos->string_pos) < 0
2929 && charpos > BEGV
2930 && (XSETWINDOW (window, w),
2931 prop = Fget_char_property (make_number (charpos),
2932 Qinvisible, window),
2933 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2934 {
2935 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2936 window);
2937 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2938 }
2939
2940 return ellipses_p;
2941 }
2942
2943
2944 /* Initialize IT for stepping through current_buffer in window W,
2945 starting at position POS that includes overlay string and display
2946 vector/ control character translation position information. Value
2947 is zero if there are overlay strings with newlines at POS. */
2948
2949 static int
2950 init_from_display_pos (it, w, pos)
2951 struct it *it;
2952 struct window *w;
2953 struct display_pos *pos;
2954 {
2955 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2956 int i, overlay_strings_with_newlines = 0;
2957
2958 /* If POS specifies a position in a display vector, this might
2959 be for an ellipsis displayed for invisible text. We won't
2960 get the iterator set up for delivering that ellipsis unless
2961 we make sure that it gets aware of the invisible text. */
2962 if (in_ellipses_for_invisible_text_p (pos, w))
2963 {
2964 --charpos;
2965 bytepos = 0;
2966 }
2967
2968 /* Keep in mind: the call to reseat in init_iterator skips invisible
2969 text, so we might end up at a position different from POS. This
2970 is only a problem when POS is a row start after a newline and an
2971 overlay starts there with an after-string, and the overlay has an
2972 invisible property. Since we don't skip invisible text in
2973 display_line and elsewhere immediately after consuming the
2974 newline before the row start, such a POS will not be in a string,
2975 but the call to init_iterator below will move us to the
2976 after-string. */
2977 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2978
2979 /* This only scans the current chunk -- it should scan all chunks.
2980 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2981 to 16 in 22.1 to make this a lesser problem. */
2982 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2983 {
2984 const char *s = SDATA (it->overlay_strings[i]);
2985 const char *e = s + SBYTES (it->overlay_strings[i]);
2986
2987 while (s < e && *s != '\n')
2988 ++s;
2989
2990 if (s < e)
2991 {
2992 overlay_strings_with_newlines = 1;
2993 break;
2994 }
2995 }
2996
2997 /* If position is within an overlay string, set up IT to the right
2998 overlay string. */
2999 if (pos->overlay_string_index >= 0)
3000 {
3001 int relative_index;
3002
3003 /* If the first overlay string happens to have a `display'
3004 property for an image, the iterator will be set up for that
3005 image, and we have to undo that setup first before we can
3006 correct the overlay string index. */
3007 if (it->method == GET_FROM_IMAGE)
3008 pop_it (it);
3009
3010 /* We already have the first chunk of overlay strings in
3011 IT->overlay_strings. Load more until the one for
3012 pos->overlay_string_index is in IT->overlay_strings. */
3013 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3014 {
3015 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3016 it->current.overlay_string_index = 0;
3017 while (n--)
3018 {
3019 load_overlay_strings (it, 0);
3020 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3021 }
3022 }
3023
3024 it->current.overlay_string_index = pos->overlay_string_index;
3025 relative_index = (it->current.overlay_string_index
3026 % OVERLAY_STRING_CHUNK_SIZE);
3027 it->string = it->overlay_strings[relative_index];
3028 xassert (STRINGP (it->string));
3029 it->current.string_pos = pos->string_pos;
3030 it->method = GET_FROM_STRING;
3031 }
3032
3033 if (CHARPOS (pos->string_pos) >= 0)
3034 {
3035 /* Recorded position is not in an overlay string, but in another
3036 string. This can only be a string from a `display' property.
3037 IT should already be filled with that string. */
3038 it->current.string_pos = pos->string_pos;
3039 xassert (STRINGP (it->string));
3040 }
3041
3042 /* Restore position in display vector translations, control
3043 character translations or ellipses. */
3044 if (pos->dpvec_index >= 0)
3045 {
3046 if (it->dpvec == NULL)
3047 get_next_display_element (it);
3048 xassert (it->dpvec && it->current.dpvec_index == 0);
3049 it->current.dpvec_index = pos->dpvec_index;
3050 }
3051
3052 CHECK_IT (it);
3053 return !overlay_strings_with_newlines;
3054 }
3055
3056
3057 /* Initialize IT for stepping through current_buffer in window W
3058 starting at ROW->start. */
3059
3060 static void
3061 init_to_row_start (it, w, row)
3062 struct it *it;
3063 struct window *w;
3064 struct glyph_row *row;
3065 {
3066 init_from_display_pos (it, w, &row->start);
3067 it->start = row->start;
3068 it->continuation_lines_width = row->continuation_lines_width;
3069 CHECK_IT (it);
3070 }
3071
3072
3073 /* Initialize IT for stepping through current_buffer in window W
3074 starting in the line following ROW, i.e. starting at ROW->end.
3075 Value is zero if there are overlay strings with newlines at ROW's
3076 end position. */
3077
3078 static int
3079 init_to_row_end (it, w, row)
3080 struct it *it;
3081 struct window *w;
3082 struct glyph_row *row;
3083 {
3084 int success = 0;
3085
3086 if (init_from_display_pos (it, w, &row->end))
3087 {
3088 if (row->continued_p)
3089 it->continuation_lines_width
3090 = row->continuation_lines_width + row->pixel_width;
3091 CHECK_IT (it);
3092 success = 1;
3093 }
3094
3095 return success;
3096 }
3097
3098
3099
3100 \f
3101 /***********************************************************************
3102 Text properties
3103 ***********************************************************************/
3104
3105 /* Called when IT reaches IT->stop_charpos. Handle text property and
3106 overlay changes. Set IT->stop_charpos to the next position where
3107 to stop. */
3108
3109 static void
3110 handle_stop (it)
3111 struct it *it;
3112 {
3113 enum prop_handled handled;
3114 int handle_overlay_change_p;
3115 struct props *p;
3116
3117 it->dpvec = NULL;
3118 it->current.dpvec_index = -1;
3119 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3120 it->ignore_overlay_strings_at_pos_p = 0;
3121 it->ellipsis_p = 0;
3122
3123 /* Use face of preceding text for ellipsis (if invisible) */
3124 if (it->selective_display_ellipsis_p)
3125 it->saved_face_id = it->face_id;
3126
3127 do
3128 {
3129 handled = HANDLED_NORMALLY;
3130
3131 /* Call text property handlers. */
3132 for (p = it_props; p->handler; ++p)
3133 {
3134 handled = p->handler (it);
3135
3136 if (handled == HANDLED_RECOMPUTE_PROPS)
3137 break;
3138 else if (handled == HANDLED_RETURN)
3139 {
3140 /* We still want to show before and after strings from
3141 overlays even if the actual buffer text is replaced. */
3142 if (!handle_overlay_change_p
3143 || it->sp > 1
3144 || !get_overlay_strings_1 (it, 0, 0))
3145 {
3146 if (it->ellipsis_p)
3147 setup_for_ellipsis (it, 0);
3148 /* When handling a display spec, we might load an
3149 empty string. In that case, discard it here. We
3150 used to discard it in handle_single_display_spec,
3151 but that causes get_overlay_strings_1, above, to
3152 ignore overlay strings that we must check. */
3153 if (STRINGP (it->string) && !SCHARS (it->string))
3154 pop_it (it);
3155 return;
3156 }
3157 else if (STRINGP (it->string) && !SCHARS (it->string))
3158 pop_it (it);
3159 else
3160 {
3161 it->ignore_overlay_strings_at_pos_p = 1;
3162 it->string_from_display_prop_p = 0;
3163 handle_overlay_change_p = 0;
3164 }
3165 handled = HANDLED_RECOMPUTE_PROPS;
3166 break;
3167 }
3168 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3169 handle_overlay_change_p = 0;
3170 }
3171
3172 if (handled != HANDLED_RECOMPUTE_PROPS)
3173 {
3174 /* Don't check for overlay strings below when set to deliver
3175 characters from a display vector. */
3176 if (it->method == GET_FROM_DISPLAY_VECTOR)
3177 handle_overlay_change_p = 0;
3178
3179 /* Handle overlay changes.
3180 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3181 if it finds overlays. */
3182 if (handle_overlay_change_p)
3183 handled = handle_overlay_change (it);
3184 }
3185
3186 if (it->ellipsis_p)
3187 {
3188 setup_for_ellipsis (it, 0);
3189 break;
3190 }
3191 }
3192 while (handled == HANDLED_RECOMPUTE_PROPS);
3193
3194 /* Determine where to stop next. */
3195 if (handled == HANDLED_NORMALLY)
3196 compute_stop_pos (it);
3197 }
3198
3199
3200 /* Compute IT->stop_charpos from text property and overlay change
3201 information for IT's current position. */
3202
3203 static void
3204 compute_stop_pos (it)
3205 struct it *it;
3206 {
3207 register INTERVAL iv, next_iv;
3208 Lisp_Object object, limit, position;
3209 EMACS_INT charpos, bytepos;
3210
3211 /* If nowhere else, stop at the end. */
3212 it->stop_charpos = it->end_charpos;
3213
3214 if (STRINGP (it->string))
3215 {
3216 /* Strings are usually short, so don't limit the search for
3217 properties. */
3218 object = it->string;
3219 limit = Qnil;
3220 charpos = IT_STRING_CHARPOS (*it);
3221 bytepos = IT_STRING_BYTEPOS (*it);
3222 }
3223 else
3224 {
3225 EMACS_INT pos;
3226
3227 /* If next overlay change is in front of the current stop pos
3228 (which is IT->end_charpos), stop there. Note: value of
3229 next_overlay_change is point-max if no overlay change
3230 follows. */
3231 charpos = IT_CHARPOS (*it);
3232 bytepos = IT_BYTEPOS (*it);
3233 pos = next_overlay_change (charpos);
3234 if (pos < it->stop_charpos)
3235 it->stop_charpos = pos;
3236
3237 /* If showing the region, we have to stop at the region
3238 start or end because the face might change there. */
3239 if (it->region_beg_charpos > 0)
3240 {
3241 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3242 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3243 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3244 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3245 }
3246
3247 /* Set up variables for computing the stop position from text
3248 property changes. */
3249 XSETBUFFER (object, current_buffer);
3250 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3251 }
3252
3253 /* Get the interval containing IT's position. Value is a null
3254 interval if there isn't such an interval. */
3255 position = make_number (charpos);
3256 iv = validate_interval_range (object, &position, &position, 0);
3257 if (!NULL_INTERVAL_P (iv))
3258 {
3259 Lisp_Object values_here[LAST_PROP_IDX];
3260 struct props *p;
3261
3262 /* Get properties here. */
3263 for (p = it_props; p->handler; ++p)
3264 values_here[p->idx] = textget (iv->plist, *p->name);
3265
3266 /* Look for an interval following iv that has different
3267 properties. */
3268 for (next_iv = next_interval (iv);
3269 (!NULL_INTERVAL_P (next_iv)
3270 && (NILP (limit)
3271 || XFASTINT (limit) > next_iv->position));
3272 next_iv = next_interval (next_iv))
3273 {
3274 for (p = it_props; p->handler; ++p)
3275 {
3276 Lisp_Object new_value;
3277
3278 new_value = textget (next_iv->plist, *p->name);
3279 if (!EQ (values_here[p->idx], new_value))
3280 break;
3281 }
3282
3283 if (p->handler)
3284 break;
3285 }
3286
3287 if (!NULL_INTERVAL_P (next_iv))
3288 {
3289 if (INTEGERP (limit)
3290 && next_iv->position >= XFASTINT (limit))
3291 /* No text property change up to limit. */
3292 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3293 else
3294 /* Text properties change in next_iv. */
3295 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3296 }
3297 }
3298
3299 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3300 it->stop_charpos, it->string);
3301
3302 xassert (STRINGP (it->string)
3303 || (it->stop_charpos >= BEGV
3304 && it->stop_charpos >= IT_CHARPOS (*it)));
3305 }
3306
3307
3308 /* Return the position of the next overlay change after POS in
3309 current_buffer. Value is point-max if no overlay change
3310 follows. This is like `next-overlay-change' but doesn't use
3311 xmalloc. */
3312
3313 static EMACS_INT
3314 next_overlay_change (pos)
3315 EMACS_INT pos;
3316 {
3317 int noverlays;
3318 EMACS_INT endpos;
3319 Lisp_Object *overlays;
3320 int i;
3321
3322 /* Get all overlays at the given position. */
3323 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3324
3325 /* If any of these overlays ends before endpos,
3326 use its ending point instead. */
3327 for (i = 0; i < noverlays; ++i)
3328 {
3329 Lisp_Object oend;
3330 EMACS_INT oendpos;
3331
3332 oend = OVERLAY_END (overlays[i]);
3333 oendpos = OVERLAY_POSITION (oend);
3334 endpos = min (endpos, oendpos);
3335 }
3336
3337 return endpos;
3338 }
3339
3340
3341 \f
3342 /***********************************************************************
3343 Fontification
3344 ***********************************************************************/
3345
3346 /* Handle changes in the `fontified' property of the current buffer by
3347 calling hook functions from Qfontification_functions to fontify
3348 regions of text. */
3349
3350 static enum prop_handled
3351 handle_fontified_prop (it)
3352 struct it *it;
3353 {
3354 Lisp_Object prop, pos;
3355 enum prop_handled handled = HANDLED_NORMALLY;
3356
3357 if (!NILP (Vmemory_full))
3358 return handled;
3359
3360 /* Get the value of the `fontified' property at IT's current buffer
3361 position. (The `fontified' property doesn't have a special
3362 meaning in strings.) If the value is nil, call functions from
3363 Qfontification_functions. */
3364 if (!STRINGP (it->string)
3365 && it->s == NULL
3366 && !NILP (Vfontification_functions)
3367 && !NILP (Vrun_hooks)
3368 && (pos = make_number (IT_CHARPOS (*it)),
3369 prop = Fget_char_property (pos, Qfontified, Qnil),
3370 /* Ignore the special cased nil value always present at EOB since
3371 no amount of fontifying will be able to change it. */
3372 NILP (prop) && IT_CHARPOS (*it) < Z))
3373 {
3374 int count = SPECPDL_INDEX ();
3375 Lisp_Object val;
3376
3377 val = Vfontification_functions;
3378 specbind (Qfontification_functions, Qnil);
3379
3380 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3381 safe_call1 (val, pos);
3382 else
3383 {
3384 Lisp_Object globals, fn;
3385 struct gcpro gcpro1, gcpro2;
3386
3387 globals = Qnil;
3388 GCPRO2 (val, globals);
3389
3390 for (; CONSP (val); val = XCDR (val))
3391 {
3392 fn = XCAR (val);
3393
3394 if (EQ (fn, Qt))
3395 {
3396 /* A value of t indicates this hook has a local
3397 binding; it means to run the global binding too.
3398 In a global value, t should not occur. If it
3399 does, we must ignore it to avoid an endless
3400 loop. */
3401 for (globals = Fdefault_value (Qfontification_functions);
3402 CONSP (globals);
3403 globals = XCDR (globals))
3404 {
3405 fn = XCAR (globals);
3406 if (!EQ (fn, Qt))
3407 safe_call1 (fn, pos);
3408 }
3409 }
3410 else
3411 safe_call1 (fn, pos);
3412 }
3413
3414 UNGCPRO;
3415 }
3416
3417 unbind_to (count, Qnil);
3418
3419 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3420 something. This avoids an endless loop if they failed to
3421 fontify the text for which reason ever. */
3422 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3423 handled = HANDLED_RECOMPUTE_PROPS;
3424 }
3425
3426 return handled;
3427 }
3428
3429
3430 \f
3431 /***********************************************************************
3432 Faces
3433 ***********************************************************************/
3434
3435 /* Set up iterator IT from face properties at its current position.
3436 Called from handle_stop. */
3437
3438 static enum prop_handled
3439 handle_face_prop (it)
3440 struct it *it;
3441 {
3442 int new_face_id;
3443 EMACS_INT next_stop;
3444
3445 if (!STRINGP (it->string))
3446 {
3447 new_face_id
3448 = face_at_buffer_position (it->w,
3449 IT_CHARPOS (*it),
3450 it->region_beg_charpos,
3451 it->region_end_charpos,
3452 &next_stop,
3453 (IT_CHARPOS (*it)
3454 + TEXT_PROP_DISTANCE_LIMIT),
3455 0);
3456
3457 /* Is this a start of a run of characters with box face?
3458 Caveat: this can be called for a freshly initialized
3459 iterator; face_id is -1 in this case. We know that the new
3460 face will not change until limit, i.e. if the new face has a
3461 box, all characters up to limit will have one. But, as
3462 usual, we don't know whether limit is really the end. */
3463 if (new_face_id != it->face_id)
3464 {
3465 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3466
3467 /* If new face has a box but old face has not, this is
3468 the start of a run of characters with box, i.e. it has
3469 a shadow on the left side. The value of face_id of the
3470 iterator will be -1 if this is the initial call that gets
3471 the face. In this case, we have to look in front of IT's
3472 position and see whether there is a face != new_face_id. */
3473 it->start_of_box_run_p
3474 = (new_face->box != FACE_NO_BOX
3475 && (it->face_id >= 0
3476 || IT_CHARPOS (*it) == BEG
3477 || new_face_id != face_before_it_pos (it)));
3478 it->face_box_p = new_face->box != FACE_NO_BOX;
3479 }
3480 }
3481 else
3482 {
3483 int base_face_id, bufpos;
3484 int i;
3485 Lisp_Object from_overlay
3486 = (it->current.overlay_string_index >= 0
3487 ? it->string_overlays[it->current.overlay_string_index]
3488 : Qnil);
3489
3490 /* See if we got to this string directly or indirectly from
3491 an overlay property. That includes the before-string or
3492 after-string of an overlay, strings in display properties
3493 provided by an overlay, their text properties, etc.
3494
3495 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3496 if (! NILP (from_overlay))
3497 for (i = it->sp - 1; i >= 0; i--)
3498 {
3499 if (it->stack[i].current.overlay_string_index >= 0)
3500 from_overlay
3501 = it->string_overlays[it->stack[i].current.overlay_string_index];
3502 else if (! NILP (it->stack[i].from_overlay))
3503 from_overlay = it->stack[i].from_overlay;
3504
3505 if (!NILP (from_overlay))
3506 break;
3507 }
3508
3509 if (! NILP (from_overlay))
3510 {
3511 bufpos = IT_CHARPOS (*it);
3512 /* For a string from an overlay, the base face depends
3513 only on text properties and ignores overlays. */
3514 base_face_id
3515 = face_for_overlay_string (it->w,
3516 IT_CHARPOS (*it),
3517 it->region_beg_charpos,
3518 it->region_end_charpos,
3519 &next_stop,
3520 (IT_CHARPOS (*it)
3521 + TEXT_PROP_DISTANCE_LIMIT),
3522 0,
3523 from_overlay);
3524 }
3525 else
3526 {
3527 bufpos = 0;
3528
3529 /* For strings from a `display' property, use the face at
3530 IT's current buffer position as the base face to merge
3531 with, so that overlay strings appear in the same face as
3532 surrounding text, unless they specify their own
3533 faces. */
3534 base_face_id = underlying_face_id (it);
3535 }
3536
3537 new_face_id = face_at_string_position (it->w,
3538 it->string,
3539 IT_STRING_CHARPOS (*it),
3540 bufpos,
3541 it->region_beg_charpos,
3542 it->region_end_charpos,
3543 &next_stop,
3544 base_face_id, 0);
3545
3546 #if 0 /* This shouldn't be neccessary. Let's check it. */
3547 /* If IT is used to display a mode line we would really like to
3548 use the mode line face instead of the frame's default face. */
3549 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3550 && new_face_id == DEFAULT_FACE_ID)
3551 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3552 #endif
3553
3554 /* Is this a start of a run of characters with box? Caveat:
3555 this can be called for a freshly allocated iterator; face_id
3556 is -1 is this case. We know that the new face will not
3557 change until the next check pos, i.e. if the new face has a
3558 box, all characters up to that position will have a
3559 box. But, as usual, we don't know whether that position
3560 is really the end. */
3561 if (new_face_id != it->face_id)
3562 {
3563 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3564 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3565
3566 /* If new face has a box but old face hasn't, this is the
3567 start of a run of characters with box, i.e. it has a
3568 shadow on the left side. */
3569 it->start_of_box_run_p
3570 = new_face->box && (old_face == NULL || !old_face->box);
3571 it->face_box_p = new_face->box != FACE_NO_BOX;
3572 }
3573 }
3574
3575 it->face_id = new_face_id;
3576 return HANDLED_NORMALLY;
3577 }
3578
3579
3580 /* Return the ID of the face ``underlying'' IT's current position,
3581 which is in a string. If the iterator is associated with a
3582 buffer, return the face at IT's current buffer position.
3583 Otherwise, use the iterator's base_face_id. */
3584
3585 static int
3586 underlying_face_id (it)
3587 struct it *it;
3588 {
3589 int face_id = it->base_face_id, i;
3590
3591 xassert (STRINGP (it->string));
3592
3593 for (i = it->sp - 1; i >= 0; --i)
3594 if (NILP (it->stack[i].string))
3595 face_id = it->stack[i].face_id;
3596
3597 return face_id;
3598 }
3599
3600
3601 /* Compute the face one character before or after the current position
3602 of IT. BEFORE_P non-zero means get the face in front of IT's
3603 position. Value is the id of the face. */
3604
3605 static int
3606 face_before_or_after_it_pos (it, before_p)
3607 struct it *it;
3608 int before_p;
3609 {
3610 int face_id, limit;
3611 EMACS_INT next_check_charpos;
3612 struct text_pos pos;
3613
3614 xassert (it->s == NULL);
3615
3616 if (STRINGP (it->string))
3617 {
3618 int bufpos, base_face_id;
3619
3620 /* No face change past the end of the string (for the case
3621 we are padding with spaces). No face change before the
3622 string start. */
3623 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3624 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3625 return it->face_id;
3626
3627 /* Set pos to the position before or after IT's current position. */
3628 if (before_p)
3629 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3630 else
3631 /* For composition, we must check the character after the
3632 composition. */
3633 pos = (it->what == IT_COMPOSITION
3634 ? string_pos (IT_STRING_CHARPOS (*it)
3635 + it->cmp_it.nchars, it->string)
3636 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3637
3638 if (it->current.overlay_string_index >= 0)
3639 bufpos = IT_CHARPOS (*it);
3640 else
3641 bufpos = 0;
3642
3643 base_face_id = underlying_face_id (it);
3644
3645 /* Get the face for ASCII, or unibyte. */
3646 face_id = face_at_string_position (it->w,
3647 it->string,
3648 CHARPOS (pos),
3649 bufpos,
3650 it->region_beg_charpos,
3651 it->region_end_charpos,
3652 &next_check_charpos,
3653 base_face_id, 0);
3654
3655 /* Correct the face for charsets different from ASCII. Do it
3656 for the multibyte case only. The face returned above is
3657 suitable for unibyte text if IT->string is unibyte. */
3658 if (STRING_MULTIBYTE (it->string))
3659 {
3660 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3661 int rest = SBYTES (it->string) - BYTEPOS (pos);
3662 int c, len;
3663 struct face *face = FACE_FROM_ID (it->f, face_id);
3664
3665 c = string_char_and_length (p, rest, &len);
3666 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3667 }
3668 }
3669 else
3670 {
3671 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3672 || (IT_CHARPOS (*it) <= BEGV && before_p))
3673 return it->face_id;
3674
3675 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3676 pos = it->current.pos;
3677
3678 if (before_p)
3679 DEC_TEXT_POS (pos, it->multibyte_p);
3680 else
3681 {
3682 if (it->what == IT_COMPOSITION)
3683 /* For composition, we must check the position after the
3684 composition. */
3685 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3686 else
3687 INC_TEXT_POS (pos, it->multibyte_p);
3688 }
3689
3690 /* Determine face for CHARSET_ASCII, or unibyte. */
3691 face_id = face_at_buffer_position (it->w,
3692 CHARPOS (pos),
3693 it->region_beg_charpos,
3694 it->region_end_charpos,
3695 &next_check_charpos,
3696 limit, 0);
3697
3698 /* Correct the face for charsets different from ASCII. Do it
3699 for the multibyte case only. The face returned above is
3700 suitable for unibyte text if current_buffer is unibyte. */
3701 if (it->multibyte_p)
3702 {
3703 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3704 struct face *face = FACE_FROM_ID (it->f, face_id);
3705 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3706 }
3707 }
3708
3709 return face_id;
3710 }
3711
3712
3713 \f
3714 /***********************************************************************
3715 Invisible text
3716 ***********************************************************************/
3717
3718 /* Set up iterator IT from invisible properties at its current
3719 position. Called from handle_stop. */
3720
3721 static enum prop_handled
3722 handle_invisible_prop (it)
3723 struct it *it;
3724 {
3725 enum prop_handled handled = HANDLED_NORMALLY;
3726
3727 if (STRINGP (it->string))
3728 {
3729 extern Lisp_Object Qinvisible;
3730 Lisp_Object prop, end_charpos, limit, charpos;
3731
3732 /* Get the value of the invisible text property at the
3733 current position. Value will be nil if there is no such
3734 property. */
3735 charpos = make_number (IT_STRING_CHARPOS (*it));
3736 prop = Fget_text_property (charpos, Qinvisible, it->string);
3737
3738 if (!NILP (prop)
3739 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3740 {
3741 handled = HANDLED_RECOMPUTE_PROPS;
3742
3743 /* Get the position at which the next change of the
3744 invisible text property can be found in IT->string.
3745 Value will be nil if the property value is the same for
3746 all the rest of IT->string. */
3747 XSETINT (limit, SCHARS (it->string));
3748 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3749 it->string, limit);
3750
3751 /* Text at current position is invisible. The next
3752 change in the property is at position end_charpos.
3753 Move IT's current position to that position. */
3754 if (INTEGERP (end_charpos)
3755 && XFASTINT (end_charpos) < XFASTINT (limit))
3756 {
3757 struct text_pos old;
3758 old = it->current.string_pos;
3759 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3760 compute_string_pos (&it->current.string_pos, old, it->string);
3761 }
3762 else
3763 {
3764 /* The rest of the string is invisible. If this is an
3765 overlay string, proceed with the next overlay string
3766 or whatever comes and return a character from there. */
3767 if (it->current.overlay_string_index >= 0)
3768 {
3769 next_overlay_string (it);
3770 /* Don't check for overlay strings when we just
3771 finished processing them. */
3772 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3773 }
3774 else
3775 {
3776 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3777 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3778 }
3779 }
3780 }
3781 }
3782 else
3783 {
3784 int invis_p;
3785 EMACS_INT newpos, next_stop, start_charpos;
3786 Lisp_Object pos, prop, overlay;
3787
3788 /* First of all, is there invisible text at this position? */
3789 start_charpos = IT_CHARPOS (*it);
3790 pos = make_number (IT_CHARPOS (*it));
3791 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3792 &overlay);
3793 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3794
3795 /* If we are on invisible text, skip over it. */
3796 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3797 {
3798 /* Record whether we have to display an ellipsis for the
3799 invisible text. */
3800 int display_ellipsis_p = invis_p == 2;
3801
3802 handled = HANDLED_RECOMPUTE_PROPS;
3803
3804 /* Loop skipping over invisible text. The loop is left at
3805 ZV or with IT on the first char being visible again. */
3806 do
3807 {
3808 /* Try to skip some invisible text. Return value is the
3809 position reached which can be equal to IT's position
3810 if there is nothing invisible here. This skips both
3811 over invisible text properties and overlays with
3812 invisible property. */
3813 newpos = skip_invisible (IT_CHARPOS (*it),
3814 &next_stop, ZV, it->window);
3815
3816 /* If we skipped nothing at all we weren't at invisible
3817 text in the first place. If everything to the end of
3818 the buffer was skipped, end the loop. */
3819 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3820 invis_p = 0;
3821 else
3822 {
3823 /* We skipped some characters but not necessarily
3824 all there are. Check if we ended up on visible
3825 text. Fget_char_property returns the property of
3826 the char before the given position, i.e. if we
3827 get invis_p = 0, this means that the char at
3828 newpos is visible. */
3829 pos = make_number (newpos);
3830 prop = Fget_char_property (pos, Qinvisible, it->window);
3831 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3832 }
3833
3834 /* If we ended up on invisible text, proceed to
3835 skip starting with next_stop. */
3836 if (invis_p)
3837 IT_CHARPOS (*it) = next_stop;
3838
3839 /* If there are adjacent invisible texts, don't lose the
3840 second one's ellipsis. */
3841 if (invis_p == 2)
3842 display_ellipsis_p = 1;
3843 }
3844 while (invis_p);
3845
3846 /* The position newpos is now either ZV or on visible text. */
3847 IT_CHARPOS (*it) = newpos;
3848 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3849
3850 /* If there are before-strings at the start of invisible
3851 text, and the text is invisible because of a text
3852 property, arrange to show before-strings because 20.x did
3853 it that way. (If the text is invisible because of an
3854 overlay property instead of a text property, this is
3855 already handled in the overlay code.) */
3856 if (NILP (overlay)
3857 && get_overlay_strings (it, start_charpos))
3858 {
3859 handled = HANDLED_RECOMPUTE_PROPS;
3860 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3861 }
3862 else if (display_ellipsis_p)
3863 {
3864 /* Make sure that the glyphs of the ellipsis will get
3865 correct `charpos' values. If we would not update
3866 it->position here, the glyphs would belong to the
3867 last visible character _before_ the invisible
3868 text, which confuses `set_cursor_from_row'.
3869
3870 We use the last invisible position instead of the
3871 first because this way the cursor is always drawn on
3872 the first "." of the ellipsis, whenever PT is inside
3873 the invisible text. Otherwise the cursor would be
3874 placed _after_ the ellipsis when the point is after the
3875 first invisible character. */
3876 if (!STRINGP (it->object))
3877 {
3878 it->position.charpos = IT_CHARPOS (*it) - 1;
3879 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3880 }
3881 it->ellipsis_p = 1;
3882 /* Let the ellipsis display before
3883 considering any properties of the following char.
3884 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3885 handled = HANDLED_RETURN;
3886 }
3887 }
3888 }
3889
3890 return handled;
3891 }
3892
3893
3894 /* Make iterator IT return `...' next.
3895 Replaces LEN characters from buffer. */
3896
3897 static void
3898 setup_for_ellipsis (it, len)
3899 struct it *it;
3900 int len;
3901 {
3902 /* Use the display table definition for `...'. Invalid glyphs
3903 will be handled by the method returning elements from dpvec. */
3904 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3905 {
3906 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3907 it->dpvec = v->contents;
3908 it->dpend = v->contents + v->size;
3909 }
3910 else
3911 {
3912 /* Default `...'. */
3913 it->dpvec = default_invis_vector;
3914 it->dpend = default_invis_vector + 3;
3915 }
3916
3917 it->dpvec_char_len = len;
3918 it->current.dpvec_index = 0;
3919 it->dpvec_face_id = -1;
3920
3921 /* Remember the current face id in case glyphs specify faces.
3922 IT's face is restored in set_iterator_to_next.
3923 saved_face_id was set to preceding char's face in handle_stop. */
3924 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3925 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3926
3927 it->method = GET_FROM_DISPLAY_VECTOR;
3928 it->ellipsis_p = 1;
3929 }
3930
3931
3932 \f
3933 /***********************************************************************
3934 'display' property
3935 ***********************************************************************/
3936
3937 /* Set up iterator IT from `display' property at its current position.
3938 Called from handle_stop.
3939 We return HANDLED_RETURN if some part of the display property
3940 overrides the display of the buffer text itself.
3941 Otherwise we return HANDLED_NORMALLY. */
3942
3943 static enum prop_handled
3944 handle_display_prop (it)
3945 struct it *it;
3946 {
3947 Lisp_Object prop, object, overlay;
3948 struct text_pos *position;
3949 /* Nonzero if some property replaces the display of the text itself. */
3950 int display_replaced_p = 0;
3951
3952 if (STRINGP (it->string))
3953 {
3954 object = it->string;
3955 position = &it->current.string_pos;
3956 }
3957 else
3958 {
3959 XSETWINDOW (object, it->w);
3960 position = &it->current.pos;
3961 }
3962
3963 /* Reset those iterator values set from display property values. */
3964 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3965 it->space_width = Qnil;
3966 it->font_height = Qnil;
3967 it->voffset = 0;
3968
3969 /* We don't support recursive `display' properties, i.e. string
3970 values that have a string `display' property, that have a string
3971 `display' property etc. */
3972 if (!it->string_from_display_prop_p)
3973 it->area = TEXT_AREA;
3974
3975 prop = get_char_property_and_overlay (make_number (position->charpos),
3976 Qdisplay, object, &overlay);
3977 if (NILP (prop))
3978 return HANDLED_NORMALLY;
3979 /* Now OVERLAY is the overlay that gave us this property, or nil
3980 if it was a text property. */
3981
3982 if (!STRINGP (it->string))
3983 object = it->w->buffer;
3984
3985 if (CONSP (prop)
3986 /* Simple properties. */
3987 && !EQ (XCAR (prop), Qimage)
3988 && !EQ (XCAR (prop), Qspace)
3989 && !EQ (XCAR (prop), Qwhen)
3990 && !EQ (XCAR (prop), Qslice)
3991 && !EQ (XCAR (prop), Qspace_width)
3992 && !EQ (XCAR (prop), Qheight)
3993 && !EQ (XCAR (prop), Qraise)
3994 /* Marginal area specifications. */
3995 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3996 && !EQ (XCAR (prop), Qleft_fringe)
3997 && !EQ (XCAR (prop), Qright_fringe)
3998 && !NILP (XCAR (prop)))
3999 {
4000 for (; CONSP (prop); prop = XCDR (prop))
4001 {
4002 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4003 position, display_replaced_p))
4004 {
4005 display_replaced_p = 1;
4006 /* If some text in a string is replaced, `position' no
4007 longer points to the position of `object'. */
4008 if (STRINGP (object))
4009 break;
4010 }
4011 }
4012 }
4013 else if (VECTORP (prop))
4014 {
4015 int i;
4016 for (i = 0; i < ASIZE (prop); ++i)
4017 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4018 position, display_replaced_p))
4019 {
4020 display_replaced_p = 1;
4021 /* If some text in a string is replaced, `position' no
4022 longer points to the position of `object'. */
4023 if (STRINGP (object))
4024 break;
4025 }
4026 }
4027 else
4028 {
4029 if (handle_single_display_spec (it, prop, object, overlay,
4030 position, 0))
4031 display_replaced_p = 1;
4032 }
4033
4034 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4035 }
4036
4037
4038 /* Value is the position of the end of the `display' property starting
4039 at START_POS in OBJECT. */
4040
4041 static struct text_pos
4042 display_prop_end (it, object, start_pos)
4043 struct it *it;
4044 Lisp_Object object;
4045 struct text_pos start_pos;
4046 {
4047 Lisp_Object end;
4048 struct text_pos end_pos;
4049
4050 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4051 Qdisplay, object, Qnil);
4052 CHARPOS (end_pos) = XFASTINT (end);
4053 if (STRINGP (object))
4054 compute_string_pos (&end_pos, start_pos, it->string);
4055 else
4056 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4057
4058 return end_pos;
4059 }
4060
4061
4062 /* Set up IT from a single `display' specification PROP. OBJECT
4063 is the object in which the `display' property was found. *POSITION
4064 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4065 means that we previously saw a display specification which already
4066 replaced text display with something else, for example an image;
4067 we ignore such properties after the first one has been processed.
4068
4069 OVERLAY is the overlay this `display' property came from,
4070 or nil if it was a text property.
4071
4072 If PROP is a `space' or `image' specification, and in some other
4073 cases too, set *POSITION to the position where the `display'
4074 property ends.
4075
4076 Value is non-zero if something was found which replaces the display
4077 of buffer or string text. */
4078
4079 static int
4080 handle_single_display_spec (it, spec, object, overlay, position,
4081 display_replaced_before_p)
4082 struct it *it;
4083 Lisp_Object spec;
4084 Lisp_Object object;
4085 Lisp_Object overlay;
4086 struct text_pos *position;
4087 int display_replaced_before_p;
4088 {
4089 Lisp_Object form;
4090 Lisp_Object location, value;
4091 struct text_pos start_pos, save_pos;
4092 int valid_p;
4093
4094 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4095 If the result is non-nil, use VALUE instead of SPEC. */
4096 form = Qt;
4097 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4098 {
4099 spec = XCDR (spec);
4100 if (!CONSP (spec))
4101 return 0;
4102 form = XCAR (spec);
4103 spec = XCDR (spec);
4104 }
4105
4106 if (!NILP (form) && !EQ (form, Qt))
4107 {
4108 int count = SPECPDL_INDEX ();
4109 struct gcpro gcpro1;
4110
4111 /* Bind `object' to the object having the `display' property, a
4112 buffer or string. Bind `position' to the position in the
4113 object where the property was found, and `buffer-position'
4114 to the current position in the buffer. */
4115 specbind (Qobject, object);
4116 specbind (Qposition, make_number (CHARPOS (*position)));
4117 specbind (Qbuffer_position,
4118 make_number (STRINGP (object)
4119 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4120 GCPRO1 (form);
4121 form = safe_eval (form);
4122 UNGCPRO;
4123 unbind_to (count, Qnil);
4124 }
4125
4126 if (NILP (form))
4127 return 0;
4128
4129 /* Handle `(height HEIGHT)' specifications. */
4130 if (CONSP (spec)
4131 && EQ (XCAR (spec), Qheight)
4132 && CONSP (XCDR (spec)))
4133 {
4134 if (!FRAME_WINDOW_P (it->f))
4135 return 0;
4136
4137 it->font_height = XCAR (XCDR (spec));
4138 if (!NILP (it->font_height))
4139 {
4140 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4141 int new_height = -1;
4142
4143 if (CONSP (it->font_height)
4144 && (EQ (XCAR (it->font_height), Qplus)
4145 || EQ (XCAR (it->font_height), Qminus))
4146 && CONSP (XCDR (it->font_height))
4147 && INTEGERP (XCAR (XCDR (it->font_height))))
4148 {
4149 /* `(+ N)' or `(- N)' where N is an integer. */
4150 int steps = XINT (XCAR (XCDR (it->font_height)));
4151 if (EQ (XCAR (it->font_height), Qplus))
4152 steps = - steps;
4153 it->face_id = smaller_face (it->f, it->face_id, steps);
4154 }
4155 else if (FUNCTIONP (it->font_height))
4156 {
4157 /* Call function with current height as argument.
4158 Value is the new height. */
4159 Lisp_Object height;
4160 height = safe_call1 (it->font_height,
4161 face->lface[LFACE_HEIGHT_INDEX]);
4162 if (NUMBERP (height))
4163 new_height = XFLOATINT (height);
4164 }
4165 else if (NUMBERP (it->font_height))
4166 {
4167 /* Value is a multiple of the canonical char height. */
4168 struct face *face;
4169
4170 face = FACE_FROM_ID (it->f,
4171 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4172 new_height = (XFLOATINT (it->font_height)
4173 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4174 }
4175 else
4176 {
4177 /* Evaluate IT->font_height with `height' bound to the
4178 current specified height to get the new height. */
4179 int count = SPECPDL_INDEX ();
4180
4181 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4182 value = safe_eval (it->font_height);
4183 unbind_to (count, Qnil);
4184
4185 if (NUMBERP (value))
4186 new_height = XFLOATINT (value);
4187 }
4188
4189 if (new_height > 0)
4190 it->face_id = face_with_height (it->f, it->face_id, new_height);
4191 }
4192
4193 return 0;
4194 }
4195
4196 /* Handle `(space-width WIDTH)'. */
4197 if (CONSP (spec)
4198 && EQ (XCAR (spec), Qspace_width)
4199 && CONSP (XCDR (spec)))
4200 {
4201 if (!FRAME_WINDOW_P (it->f))
4202 return 0;
4203
4204 value = XCAR (XCDR (spec));
4205 if (NUMBERP (value) && XFLOATINT (value) > 0)
4206 it->space_width = value;
4207
4208 return 0;
4209 }
4210
4211 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4212 if (CONSP (spec)
4213 && EQ (XCAR (spec), Qslice))
4214 {
4215 Lisp_Object tem;
4216
4217 if (!FRAME_WINDOW_P (it->f))
4218 return 0;
4219
4220 if (tem = XCDR (spec), CONSP (tem))
4221 {
4222 it->slice.x = XCAR (tem);
4223 if (tem = XCDR (tem), CONSP (tem))
4224 {
4225 it->slice.y = XCAR (tem);
4226 if (tem = XCDR (tem), CONSP (tem))
4227 {
4228 it->slice.width = XCAR (tem);
4229 if (tem = XCDR (tem), CONSP (tem))
4230 it->slice.height = XCAR (tem);
4231 }
4232 }
4233 }
4234
4235 return 0;
4236 }
4237
4238 /* Handle `(raise FACTOR)'. */
4239 if (CONSP (spec)
4240 && EQ (XCAR (spec), Qraise)
4241 && CONSP (XCDR (spec)))
4242 {
4243 if (!FRAME_WINDOW_P (it->f))
4244 return 0;
4245
4246 #ifdef HAVE_WINDOW_SYSTEM
4247 value = XCAR (XCDR (spec));
4248 if (NUMBERP (value))
4249 {
4250 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4251 it->voffset = - (XFLOATINT (value)
4252 * (FONT_HEIGHT (face->font)));
4253 }
4254 #endif /* HAVE_WINDOW_SYSTEM */
4255
4256 return 0;
4257 }
4258
4259 /* Don't handle the other kinds of display specifications
4260 inside a string that we got from a `display' property. */
4261 if (it->string_from_display_prop_p)
4262 return 0;
4263
4264 /* Characters having this form of property are not displayed, so
4265 we have to find the end of the property. */
4266 start_pos = *position;
4267 *position = display_prop_end (it, object, start_pos);
4268 value = Qnil;
4269
4270 /* Stop the scan at that end position--we assume that all
4271 text properties change there. */
4272 it->stop_charpos = position->charpos;
4273
4274 /* Handle `(left-fringe BITMAP [FACE])'
4275 and `(right-fringe BITMAP [FACE])'. */
4276 if (CONSP (spec)
4277 && (EQ (XCAR (spec), Qleft_fringe)
4278 || EQ (XCAR (spec), Qright_fringe))
4279 && CONSP (XCDR (spec)))
4280 {
4281 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4282 int fringe_bitmap;
4283
4284 if (!FRAME_WINDOW_P (it->f))
4285 /* If we return here, POSITION has been advanced
4286 across the text with this property. */
4287 return 0;
4288
4289 #ifdef HAVE_WINDOW_SYSTEM
4290 value = XCAR (XCDR (spec));
4291 if (!SYMBOLP (value)
4292 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4293 /* If we return here, POSITION has been advanced
4294 across the text with this property. */
4295 return 0;
4296
4297 if (CONSP (XCDR (XCDR (spec))))
4298 {
4299 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4300 int face_id2 = lookup_derived_face (it->f, face_name,
4301 FRINGE_FACE_ID, 0);
4302 if (face_id2 >= 0)
4303 face_id = face_id2;
4304 }
4305
4306 /* Save current settings of IT so that we can restore them
4307 when we are finished with the glyph property value. */
4308
4309 save_pos = it->position;
4310 it->position = *position;
4311 push_it (it);
4312 it->position = save_pos;
4313
4314 it->area = TEXT_AREA;
4315 it->what = IT_IMAGE;
4316 it->image_id = -1; /* no image */
4317 it->position = start_pos;
4318 it->object = NILP (object) ? it->w->buffer : object;
4319 it->method = GET_FROM_IMAGE;
4320 it->from_overlay = Qnil;
4321 it->face_id = face_id;
4322
4323 /* Say that we haven't consumed the characters with
4324 `display' property yet. The call to pop_it in
4325 set_iterator_to_next will clean this up. */
4326 *position = start_pos;
4327
4328 if (EQ (XCAR (spec), Qleft_fringe))
4329 {
4330 it->left_user_fringe_bitmap = fringe_bitmap;
4331 it->left_user_fringe_face_id = face_id;
4332 }
4333 else
4334 {
4335 it->right_user_fringe_bitmap = fringe_bitmap;
4336 it->right_user_fringe_face_id = face_id;
4337 }
4338 #endif /* HAVE_WINDOW_SYSTEM */
4339 return 1;
4340 }
4341
4342 /* Prepare to handle `((margin left-margin) ...)',
4343 `((margin right-margin) ...)' and `((margin nil) ...)'
4344 prefixes for display specifications. */
4345 location = Qunbound;
4346 if (CONSP (spec) && CONSP (XCAR (spec)))
4347 {
4348 Lisp_Object tem;
4349
4350 value = XCDR (spec);
4351 if (CONSP (value))
4352 value = XCAR (value);
4353
4354 tem = XCAR (spec);
4355 if (EQ (XCAR (tem), Qmargin)
4356 && (tem = XCDR (tem),
4357 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4358 (NILP (tem)
4359 || EQ (tem, Qleft_margin)
4360 || EQ (tem, Qright_margin))))
4361 location = tem;
4362 }
4363
4364 if (EQ (location, Qunbound))
4365 {
4366 location = Qnil;
4367 value = spec;
4368 }
4369
4370 /* After this point, VALUE is the property after any
4371 margin prefix has been stripped. It must be a string,
4372 an image specification, or `(space ...)'.
4373
4374 LOCATION specifies where to display: `left-margin',
4375 `right-margin' or nil. */
4376
4377 valid_p = (STRINGP (value)
4378 #ifdef HAVE_WINDOW_SYSTEM
4379 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4380 #endif /* not HAVE_WINDOW_SYSTEM */
4381 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4382
4383 if (valid_p && !display_replaced_before_p)
4384 {
4385 /* Save current settings of IT so that we can restore them
4386 when we are finished with the glyph property value. */
4387 save_pos = it->position;
4388 it->position = *position;
4389 push_it (it);
4390 it->position = save_pos;
4391 it->from_overlay = overlay;
4392
4393 if (NILP (location))
4394 it->area = TEXT_AREA;
4395 else if (EQ (location, Qleft_margin))
4396 it->area = LEFT_MARGIN_AREA;
4397 else
4398 it->area = RIGHT_MARGIN_AREA;
4399
4400 if (STRINGP (value))
4401 {
4402 it->string = value;
4403 it->multibyte_p = STRING_MULTIBYTE (it->string);
4404 it->current.overlay_string_index = -1;
4405 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4406 it->end_charpos = it->string_nchars = SCHARS (it->string);
4407 it->method = GET_FROM_STRING;
4408 it->stop_charpos = 0;
4409 it->string_from_display_prop_p = 1;
4410 /* Say that we haven't consumed the characters with
4411 `display' property yet. The call to pop_it in
4412 set_iterator_to_next will clean this up. */
4413 if (BUFFERP (object))
4414 *position = start_pos;
4415 }
4416 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4417 {
4418 it->method = GET_FROM_STRETCH;
4419 it->object = value;
4420 *position = it->position = start_pos;
4421 }
4422 #ifdef HAVE_WINDOW_SYSTEM
4423 else
4424 {
4425 it->what = IT_IMAGE;
4426 it->image_id = lookup_image (it->f, value);
4427 it->position = start_pos;
4428 it->object = NILP (object) ? it->w->buffer : object;
4429 it->method = GET_FROM_IMAGE;
4430
4431 /* Say that we haven't consumed the characters with
4432 `display' property yet. The call to pop_it in
4433 set_iterator_to_next will clean this up. */
4434 *position = start_pos;
4435 }
4436 #endif /* HAVE_WINDOW_SYSTEM */
4437
4438 return 1;
4439 }
4440
4441 /* Invalid property or property not supported. Restore
4442 POSITION to what it was before. */
4443 *position = start_pos;
4444 return 0;
4445 }
4446
4447
4448 /* Check if SPEC is a display sub-property value whose text should be
4449 treated as intangible. */
4450
4451 static int
4452 single_display_spec_intangible_p (prop)
4453 Lisp_Object prop;
4454 {
4455 /* Skip over `when FORM'. */
4456 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4457 {
4458 prop = XCDR (prop);
4459 if (!CONSP (prop))
4460 return 0;
4461 prop = XCDR (prop);
4462 }
4463
4464 if (STRINGP (prop))
4465 return 1;
4466
4467 if (!CONSP (prop))
4468 return 0;
4469
4470 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4471 we don't need to treat text as intangible. */
4472 if (EQ (XCAR (prop), Qmargin))
4473 {
4474 prop = XCDR (prop);
4475 if (!CONSP (prop))
4476 return 0;
4477
4478 prop = XCDR (prop);
4479 if (!CONSP (prop)
4480 || EQ (XCAR (prop), Qleft_margin)
4481 || EQ (XCAR (prop), Qright_margin))
4482 return 0;
4483 }
4484
4485 return (CONSP (prop)
4486 && (EQ (XCAR (prop), Qimage)
4487 || EQ (XCAR (prop), Qspace)));
4488 }
4489
4490
4491 /* Check if PROP is a display property value whose text should be
4492 treated as intangible. */
4493
4494 int
4495 display_prop_intangible_p (prop)
4496 Lisp_Object prop;
4497 {
4498 if (CONSP (prop)
4499 && CONSP (XCAR (prop))
4500 && !EQ (Qmargin, XCAR (XCAR (prop))))
4501 {
4502 /* A list of sub-properties. */
4503 while (CONSP (prop))
4504 {
4505 if (single_display_spec_intangible_p (XCAR (prop)))
4506 return 1;
4507 prop = XCDR (prop);
4508 }
4509 }
4510 else if (VECTORP (prop))
4511 {
4512 /* A vector of sub-properties. */
4513 int i;
4514 for (i = 0; i < ASIZE (prop); ++i)
4515 if (single_display_spec_intangible_p (AREF (prop, i)))
4516 return 1;
4517 }
4518 else
4519 return single_display_spec_intangible_p (prop);
4520
4521 return 0;
4522 }
4523
4524
4525 /* Return 1 if PROP is a display sub-property value containing STRING. */
4526
4527 static int
4528 single_display_spec_string_p (prop, string)
4529 Lisp_Object prop, string;
4530 {
4531 if (EQ (string, prop))
4532 return 1;
4533
4534 /* Skip over `when FORM'. */
4535 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4536 {
4537 prop = XCDR (prop);
4538 if (!CONSP (prop))
4539 return 0;
4540 prop = XCDR (prop);
4541 }
4542
4543 if (CONSP (prop))
4544 /* Skip over `margin LOCATION'. */
4545 if (EQ (XCAR (prop), Qmargin))
4546 {
4547 prop = XCDR (prop);
4548 if (!CONSP (prop))
4549 return 0;
4550
4551 prop = XCDR (prop);
4552 if (!CONSP (prop))
4553 return 0;
4554 }
4555
4556 return CONSP (prop) && EQ (XCAR (prop), string);
4557 }
4558
4559
4560 /* Return 1 if STRING appears in the `display' property PROP. */
4561
4562 static int
4563 display_prop_string_p (prop, string)
4564 Lisp_Object prop, string;
4565 {
4566 if (CONSP (prop)
4567 && CONSP (XCAR (prop))
4568 && !EQ (Qmargin, XCAR (XCAR (prop))))
4569 {
4570 /* A list of sub-properties. */
4571 while (CONSP (prop))
4572 {
4573 if (single_display_spec_string_p (XCAR (prop), string))
4574 return 1;
4575 prop = XCDR (prop);
4576 }
4577 }
4578 else if (VECTORP (prop))
4579 {
4580 /* A vector of sub-properties. */
4581 int i;
4582 for (i = 0; i < ASIZE (prop); ++i)
4583 if (single_display_spec_string_p (AREF (prop, i), string))
4584 return 1;
4585 }
4586 else
4587 return single_display_spec_string_p (prop, string);
4588
4589 return 0;
4590 }
4591
4592
4593 /* Determine from which buffer position in W's buffer STRING comes
4594 from. AROUND_CHARPOS is an approximate position where it could
4595 be from. Value is the buffer position or 0 if it couldn't be
4596 determined.
4597
4598 W's buffer must be current.
4599
4600 This function is necessary because we don't record buffer positions
4601 in glyphs generated from strings (to keep struct glyph small).
4602 This function may only use code that doesn't eval because it is
4603 called asynchronously from note_mouse_highlight. */
4604
4605 int
4606 string_buffer_position (w, string, around_charpos)
4607 struct window *w;
4608 Lisp_Object string;
4609 int around_charpos;
4610 {
4611 Lisp_Object limit, prop, pos;
4612 const int MAX_DISTANCE = 1000;
4613 int found = 0;
4614
4615 pos = make_number (around_charpos);
4616 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4617 while (!found && !EQ (pos, limit))
4618 {
4619 prop = Fget_char_property (pos, Qdisplay, Qnil);
4620 if (!NILP (prop) && display_prop_string_p (prop, string))
4621 found = 1;
4622 else
4623 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4624 }
4625
4626 if (!found)
4627 {
4628 pos = make_number (around_charpos);
4629 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4630 while (!found && !EQ (pos, limit))
4631 {
4632 prop = Fget_char_property (pos, Qdisplay, Qnil);
4633 if (!NILP (prop) && display_prop_string_p (prop, string))
4634 found = 1;
4635 else
4636 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4637 limit);
4638 }
4639 }
4640
4641 return found ? XINT (pos) : 0;
4642 }
4643
4644
4645 \f
4646 /***********************************************************************
4647 `composition' property
4648 ***********************************************************************/
4649
4650 /* Set up iterator IT from `composition' property at its current
4651 position. Called from handle_stop. */
4652
4653 static enum prop_handled
4654 handle_composition_prop (it)
4655 struct it *it;
4656 {
4657 Lisp_Object prop, string;
4658 EMACS_INT pos, pos_byte, start, end;
4659
4660 if (STRINGP (it->string))
4661 {
4662 unsigned char *s;
4663
4664 pos = IT_STRING_CHARPOS (*it);
4665 pos_byte = IT_STRING_BYTEPOS (*it);
4666 string = it->string;
4667 s = SDATA (string) + pos_byte;
4668 it->c = STRING_CHAR (s, 0);
4669 }
4670 else
4671 {
4672 pos = IT_CHARPOS (*it);
4673 pos_byte = IT_BYTEPOS (*it);
4674 string = Qnil;
4675 it->c = FETCH_CHAR (pos_byte);
4676 }
4677
4678 /* If there's a valid composition and point is not inside of the
4679 composition (in the case that the composition is from the current
4680 buffer), draw a glyph composed from the composition components. */
4681 if (find_composition (pos, -1, &start, &end, &prop, string)
4682 && COMPOSITION_VALID_P (start, end, prop)
4683 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4684 {
4685 if (start != pos)
4686 {
4687 if (STRINGP (it->string))
4688 pos_byte = string_char_to_byte (it->string, start);
4689 else
4690 pos_byte = CHAR_TO_BYTE (start);
4691 }
4692 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4693 prop, string);
4694
4695 if (it->cmp_it.id >= 0)
4696 {
4697 it->cmp_it.ch = -1;
4698 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4699 it->cmp_it.nglyphs = -1;
4700 }
4701 }
4702
4703 return HANDLED_NORMALLY;
4704 }
4705
4706
4707 \f
4708 /***********************************************************************
4709 Overlay strings
4710 ***********************************************************************/
4711
4712 /* The following structure is used to record overlay strings for
4713 later sorting in load_overlay_strings. */
4714
4715 struct overlay_entry
4716 {
4717 Lisp_Object overlay;
4718 Lisp_Object string;
4719 int priority;
4720 int after_string_p;
4721 };
4722
4723
4724 /* Set up iterator IT from overlay strings at its current position.
4725 Called from handle_stop. */
4726
4727 static enum prop_handled
4728 handle_overlay_change (it)
4729 struct it *it;
4730 {
4731 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4732 return HANDLED_RECOMPUTE_PROPS;
4733 else
4734 return HANDLED_NORMALLY;
4735 }
4736
4737
4738 /* Set up the next overlay string for delivery by IT, if there is an
4739 overlay string to deliver. Called by set_iterator_to_next when the
4740 end of the current overlay string is reached. If there are more
4741 overlay strings to display, IT->string and
4742 IT->current.overlay_string_index are set appropriately here.
4743 Otherwise IT->string is set to nil. */
4744
4745 static void
4746 next_overlay_string (it)
4747 struct it *it;
4748 {
4749 ++it->current.overlay_string_index;
4750 if (it->current.overlay_string_index == it->n_overlay_strings)
4751 {
4752 /* No more overlay strings. Restore IT's settings to what
4753 they were before overlay strings were processed, and
4754 continue to deliver from current_buffer. */
4755
4756 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4757 pop_it (it);
4758 xassert (it->sp > 0
4759 || (NILP (it->string)
4760 && it->method == GET_FROM_BUFFER
4761 && it->stop_charpos >= BEGV
4762 && it->stop_charpos <= it->end_charpos));
4763 it->current.overlay_string_index = -1;
4764 it->n_overlay_strings = 0;
4765
4766 /* If we're at the end of the buffer, record that we have
4767 processed the overlay strings there already, so that
4768 next_element_from_buffer doesn't try it again. */
4769 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4770 it->overlay_strings_at_end_processed_p = 1;
4771 }
4772 else
4773 {
4774 /* There are more overlay strings to process. If
4775 IT->current.overlay_string_index has advanced to a position
4776 where we must load IT->overlay_strings with more strings, do
4777 it. */
4778 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4779
4780 if (it->current.overlay_string_index && i == 0)
4781 load_overlay_strings (it, 0);
4782
4783 /* Initialize IT to deliver display elements from the overlay
4784 string. */
4785 it->string = it->overlay_strings[i];
4786 it->multibyte_p = STRING_MULTIBYTE (it->string);
4787 SET_TEXT_POS (it->current.string_pos, 0, 0);
4788 it->method = GET_FROM_STRING;
4789 it->stop_charpos = 0;
4790 if (it->cmp_it.stop_pos >= 0)
4791 it->cmp_it.stop_pos = 0;
4792 }
4793
4794 CHECK_IT (it);
4795 }
4796
4797
4798 /* Compare two overlay_entry structures E1 and E2. Used as a
4799 comparison function for qsort in load_overlay_strings. Overlay
4800 strings for the same position are sorted so that
4801
4802 1. All after-strings come in front of before-strings, except
4803 when they come from the same overlay.
4804
4805 2. Within after-strings, strings are sorted so that overlay strings
4806 from overlays with higher priorities come first.
4807
4808 2. Within before-strings, strings are sorted so that overlay
4809 strings from overlays with higher priorities come last.
4810
4811 Value is analogous to strcmp. */
4812
4813
4814 static int
4815 compare_overlay_entries (e1, e2)
4816 void *e1, *e2;
4817 {
4818 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4819 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4820 int result;
4821
4822 if (entry1->after_string_p != entry2->after_string_p)
4823 {
4824 /* Let after-strings appear in front of before-strings if
4825 they come from different overlays. */
4826 if (EQ (entry1->overlay, entry2->overlay))
4827 result = entry1->after_string_p ? 1 : -1;
4828 else
4829 result = entry1->after_string_p ? -1 : 1;
4830 }
4831 else if (entry1->after_string_p)
4832 /* After-strings sorted in order of decreasing priority. */
4833 result = entry2->priority - entry1->priority;
4834 else
4835 /* Before-strings sorted in order of increasing priority. */
4836 result = entry1->priority - entry2->priority;
4837
4838 return result;
4839 }
4840
4841
4842 /* Load the vector IT->overlay_strings with overlay strings from IT's
4843 current buffer position, or from CHARPOS if that is > 0. Set
4844 IT->n_overlays to the total number of overlay strings found.
4845
4846 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4847 a time. On entry into load_overlay_strings,
4848 IT->current.overlay_string_index gives the number of overlay
4849 strings that have already been loaded by previous calls to this
4850 function.
4851
4852 IT->add_overlay_start contains an additional overlay start
4853 position to consider for taking overlay strings from, if non-zero.
4854 This position comes into play when the overlay has an `invisible'
4855 property, and both before and after-strings. When we've skipped to
4856 the end of the overlay, because of its `invisible' property, we
4857 nevertheless want its before-string to appear.
4858 IT->add_overlay_start will contain the overlay start position
4859 in this case.
4860
4861 Overlay strings are sorted so that after-string strings come in
4862 front of before-string strings. Within before and after-strings,
4863 strings are sorted by overlay priority. See also function
4864 compare_overlay_entries. */
4865
4866 static void
4867 load_overlay_strings (it, charpos)
4868 struct it *it;
4869 int charpos;
4870 {
4871 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4872 Lisp_Object overlay, window, str, invisible;
4873 struct Lisp_Overlay *ov;
4874 int start, end;
4875 int size = 20;
4876 int n = 0, i, j, invis_p;
4877 struct overlay_entry *entries
4878 = (struct overlay_entry *) alloca (size * sizeof *entries);
4879
4880 if (charpos <= 0)
4881 charpos = IT_CHARPOS (*it);
4882
4883 /* Append the overlay string STRING of overlay OVERLAY to vector
4884 `entries' which has size `size' and currently contains `n'
4885 elements. AFTER_P non-zero means STRING is an after-string of
4886 OVERLAY. */
4887 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4888 do \
4889 { \
4890 Lisp_Object priority; \
4891 \
4892 if (n == size) \
4893 { \
4894 int new_size = 2 * size; \
4895 struct overlay_entry *old = entries; \
4896 entries = \
4897 (struct overlay_entry *) alloca (new_size \
4898 * sizeof *entries); \
4899 bcopy (old, entries, size * sizeof *entries); \
4900 size = new_size; \
4901 } \
4902 \
4903 entries[n].string = (STRING); \
4904 entries[n].overlay = (OVERLAY); \
4905 priority = Foverlay_get ((OVERLAY), Qpriority); \
4906 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4907 entries[n].after_string_p = (AFTER_P); \
4908 ++n; \
4909 } \
4910 while (0)
4911
4912 /* Process overlay before the overlay center. */
4913 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4914 {
4915 XSETMISC (overlay, ov);
4916 xassert (OVERLAYP (overlay));
4917 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4918 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4919
4920 if (end < charpos)
4921 break;
4922
4923 /* Skip this overlay if it doesn't start or end at IT's current
4924 position. */
4925 if (end != charpos && start != charpos)
4926 continue;
4927
4928 /* Skip this overlay if it doesn't apply to IT->w. */
4929 window = Foverlay_get (overlay, Qwindow);
4930 if (WINDOWP (window) && XWINDOW (window) != it->w)
4931 continue;
4932
4933 /* If the text ``under'' the overlay is invisible, both before-
4934 and after-strings from this overlay are visible; start and
4935 end position are indistinguishable. */
4936 invisible = Foverlay_get (overlay, Qinvisible);
4937 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4938
4939 /* If overlay has a non-empty before-string, record it. */
4940 if ((start == charpos || (end == charpos && invis_p))
4941 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4942 && SCHARS (str))
4943 RECORD_OVERLAY_STRING (overlay, str, 0);
4944
4945 /* If overlay has a non-empty after-string, record it. */
4946 if ((end == charpos || (start == charpos && invis_p))
4947 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4948 && SCHARS (str))
4949 RECORD_OVERLAY_STRING (overlay, str, 1);
4950 }
4951
4952 /* Process overlays after the overlay center. */
4953 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4954 {
4955 XSETMISC (overlay, ov);
4956 xassert (OVERLAYP (overlay));
4957 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4958 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4959
4960 if (start > charpos)
4961 break;
4962
4963 /* Skip this overlay if it doesn't start or end at IT's current
4964 position. */
4965 if (end != charpos && start != charpos)
4966 continue;
4967
4968 /* Skip this overlay if it doesn't apply to IT->w. */
4969 window = Foverlay_get (overlay, Qwindow);
4970 if (WINDOWP (window) && XWINDOW (window) != it->w)
4971 continue;
4972
4973 /* If the text ``under'' the overlay is invisible, it has a zero
4974 dimension, and both before- and after-strings apply. */
4975 invisible = Foverlay_get (overlay, Qinvisible);
4976 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4977
4978 /* If overlay has a non-empty before-string, record it. */
4979 if ((start == charpos || (end == charpos && invis_p))
4980 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4981 && SCHARS (str))
4982 RECORD_OVERLAY_STRING (overlay, str, 0);
4983
4984 /* If overlay has a non-empty after-string, record it. */
4985 if ((end == charpos || (start == charpos && invis_p))
4986 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4987 && SCHARS (str))
4988 RECORD_OVERLAY_STRING (overlay, str, 1);
4989 }
4990
4991 #undef RECORD_OVERLAY_STRING
4992
4993 /* Sort entries. */
4994 if (n > 1)
4995 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4996
4997 /* Record the total number of strings to process. */
4998 it->n_overlay_strings = n;
4999
5000 /* IT->current.overlay_string_index is the number of overlay strings
5001 that have already been consumed by IT. Copy some of the
5002 remaining overlay strings to IT->overlay_strings. */
5003 i = 0;
5004 j = it->current.overlay_string_index;
5005 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5006 {
5007 it->overlay_strings[i] = entries[j].string;
5008 it->string_overlays[i++] = entries[j++].overlay;
5009 }
5010
5011 CHECK_IT (it);
5012 }
5013
5014
5015 /* Get the first chunk of overlay strings at IT's current buffer
5016 position, or at CHARPOS if that is > 0. Value is non-zero if at
5017 least one overlay string was found. */
5018
5019 static int
5020 get_overlay_strings_1 (it, charpos, compute_stop_p)
5021 struct it *it;
5022 int charpos;
5023 int compute_stop_p;
5024 {
5025 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5026 process. This fills IT->overlay_strings with strings, and sets
5027 IT->n_overlay_strings to the total number of strings to process.
5028 IT->pos.overlay_string_index has to be set temporarily to zero
5029 because load_overlay_strings needs this; it must be set to -1
5030 when no overlay strings are found because a zero value would
5031 indicate a position in the first overlay string. */
5032 it->current.overlay_string_index = 0;
5033 load_overlay_strings (it, charpos);
5034
5035 /* If we found overlay strings, set up IT to deliver display
5036 elements from the first one. Otherwise set up IT to deliver
5037 from current_buffer. */
5038 if (it->n_overlay_strings)
5039 {
5040 /* Make sure we know settings in current_buffer, so that we can
5041 restore meaningful values when we're done with the overlay
5042 strings. */
5043 if (compute_stop_p)
5044 compute_stop_pos (it);
5045 xassert (it->face_id >= 0);
5046
5047 /* Save IT's settings. They are restored after all overlay
5048 strings have been processed. */
5049 xassert (!compute_stop_p || it->sp == 0);
5050
5051 /* When called from handle_stop, there might be an empty display
5052 string loaded. In that case, don't bother saving it. */
5053 if (!STRINGP (it->string) || SCHARS (it->string))
5054 push_it (it);
5055
5056 /* Set up IT to deliver display elements from the first overlay
5057 string. */
5058 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5059 it->string = it->overlay_strings[0];
5060 it->from_overlay = Qnil;
5061 it->stop_charpos = 0;
5062 xassert (STRINGP (it->string));
5063 it->end_charpos = SCHARS (it->string);
5064 it->multibyte_p = STRING_MULTIBYTE (it->string);
5065 it->method = GET_FROM_STRING;
5066 return 1;
5067 }
5068
5069 it->current.overlay_string_index = -1;
5070 return 0;
5071 }
5072
5073 static int
5074 get_overlay_strings (it, charpos)
5075 struct it *it;
5076 int charpos;
5077 {
5078 it->string = Qnil;
5079 it->method = GET_FROM_BUFFER;
5080
5081 (void) get_overlay_strings_1 (it, charpos, 1);
5082
5083 CHECK_IT (it);
5084
5085 /* Value is non-zero if we found at least one overlay string. */
5086 return STRINGP (it->string);
5087 }
5088
5089
5090 \f
5091 /***********************************************************************
5092 Saving and restoring state
5093 ***********************************************************************/
5094
5095 /* Save current settings of IT on IT->stack. Called, for example,
5096 before setting up IT for an overlay string, to be able to restore
5097 IT's settings to what they were after the overlay string has been
5098 processed. */
5099
5100 static void
5101 push_it (it)
5102 struct it *it;
5103 {
5104 struct iterator_stack_entry *p;
5105
5106 xassert (it->sp < IT_STACK_SIZE);
5107 p = it->stack + it->sp;
5108
5109 p->stop_charpos = it->stop_charpos;
5110 p->cmp_it = it->cmp_it;
5111 xassert (it->face_id >= 0);
5112 p->face_id = it->face_id;
5113 p->string = it->string;
5114 p->method = it->method;
5115 p->from_overlay = it->from_overlay;
5116 switch (p->method)
5117 {
5118 case GET_FROM_IMAGE:
5119 p->u.image.object = it->object;
5120 p->u.image.image_id = it->image_id;
5121 p->u.image.slice = it->slice;
5122 break;
5123 case GET_FROM_STRETCH:
5124 p->u.stretch.object = it->object;
5125 break;
5126 }
5127 p->position = it->position;
5128 p->current = it->current;
5129 p->end_charpos = it->end_charpos;
5130 p->string_nchars = it->string_nchars;
5131 p->area = it->area;
5132 p->multibyte_p = it->multibyte_p;
5133 p->avoid_cursor_p = it->avoid_cursor_p;
5134 p->space_width = it->space_width;
5135 p->font_height = it->font_height;
5136 p->voffset = it->voffset;
5137 p->string_from_display_prop_p = it->string_from_display_prop_p;
5138 p->display_ellipsis_p = 0;
5139 ++it->sp;
5140 }
5141
5142
5143 /* Restore IT's settings from IT->stack. Called, for example, when no
5144 more overlay strings must be processed, and we return to delivering
5145 display elements from a buffer, or when the end of a string from a
5146 `display' property is reached and we return to delivering display
5147 elements from an overlay string, or from a buffer. */
5148
5149 static void
5150 pop_it (it)
5151 struct it *it;
5152 {
5153 struct iterator_stack_entry *p;
5154
5155 xassert (it->sp > 0);
5156 --it->sp;
5157 p = it->stack + it->sp;
5158 it->stop_charpos = p->stop_charpos;
5159 it->cmp_it = p->cmp_it;
5160 it->face_id = p->face_id;
5161 it->current = p->current;
5162 it->position = p->position;
5163 it->string = p->string;
5164 it->from_overlay = p->from_overlay;
5165 if (NILP (it->string))
5166 SET_TEXT_POS (it->current.string_pos, -1, -1);
5167 it->method = p->method;
5168 switch (it->method)
5169 {
5170 case GET_FROM_IMAGE:
5171 it->image_id = p->u.image.image_id;
5172 it->object = p->u.image.object;
5173 it->slice = p->u.image.slice;
5174 break;
5175 case GET_FROM_STRETCH:
5176 it->object = p->u.comp.object;
5177 break;
5178 case GET_FROM_BUFFER:
5179 it->object = it->w->buffer;
5180 break;
5181 case GET_FROM_STRING:
5182 it->object = it->string;
5183 break;
5184 }
5185 it->end_charpos = p->end_charpos;
5186 it->string_nchars = p->string_nchars;
5187 it->area = p->area;
5188 it->multibyte_p = p->multibyte_p;
5189 it->avoid_cursor_p = p->avoid_cursor_p;
5190 it->space_width = p->space_width;
5191 it->font_height = p->font_height;
5192 it->voffset = p->voffset;
5193 it->string_from_display_prop_p = p->string_from_display_prop_p;
5194 }
5195
5196
5197 \f
5198 /***********************************************************************
5199 Moving over lines
5200 ***********************************************************************/
5201
5202 /* Set IT's current position to the previous line start. */
5203
5204 static void
5205 back_to_previous_line_start (it)
5206 struct it *it;
5207 {
5208 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5209 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5210 }
5211
5212
5213 /* Move IT to the next line start.
5214
5215 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5216 we skipped over part of the text (as opposed to moving the iterator
5217 continuously over the text). Otherwise, don't change the value
5218 of *SKIPPED_P.
5219
5220 Newlines may come from buffer text, overlay strings, or strings
5221 displayed via the `display' property. That's the reason we can't
5222 simply use find_next_newline_no_quit.
5223
5224 Note that this function may not skip over invisible text that is so
5225 because of text properties and immediately follows a newline. If
5226 it would, function reseat_at_next_visible_line_start, when called
5227 from set_iterator_to_next, would effectively make invisible
5228 characters following a newline part of the wrong glyph row, which
5229 leads to wrong cursor motion. */
5230
5231 static int
5232 forward_to_next_line_start (it, skipped_p)
5233 struct it *it;
5234 int *skipped_p;
5235 {
5236 int old_selective, newline_found_p, n;
5237 const int MAX_NEWLINE_DISTANCE = 500;
5238
5239 /* If already on a newline, just consume it to avoid unintended
5240 skipping over invisible text below. */
5241 if (it->what == IT_CHARACTER
5242 && it->c == '\n'
5243 && CHARPOS (it->position) == IT_CHARPOS (*it))
5244 {
5245 set_iterator_to_next (it, 0);
5246 it->c = 0;
5247 return 1;
5248 }
5249
5250 /* Don't handle selective display in the following. It's (a)
5251 unnecessary because it's done by the caller, and (b) leads to an
5252 infinite recursion because next_element_from_ellipsis indirectly
5253 calls this function. */
5254 old_selective = it->selective;
5255 it->selective = 0;
5256
5257 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5258 from buffer text. */
5259 for (n = newline_found_p = 0;
5260 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5261 n += STRINGP (it->string) ? 0 : 1)
5262 {
5263 if (!get_next_display_element (it))
5264 return 0;
5265 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5266 set_iterator_to_next (it, 0);
5267 }
5268
5269 /* If we didn't find a newline near enough, see if we can use a
5270 short-cut. */
5271 if (!newline_found_p)
5272 {
5273 int start = IT_CHARPOS (*it);
5274 int limit = find_next_newline_no_quit (start, 1);
5275 Lisp_Object pos;
5276
5277 xassert (!STRINGP (it->string));
5278
5279 /* If there isn't any `display' property in sight, and no
5280 overlays, we can just use the position of the newline in
5281 buffer text. */
5282 if (it->stop_charpos >= limit
5283 || ((pos = Fnext_single_property_change (make_number (start),
5284 Qdisplay,
5285 Qnil, make_number (limit)),
5286 NILP (pos))
5287 && next_overlay_change (start) == ZV))
5288 {
5289 IT_CHARPOS (*it) = limit;
5290 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5291 *skipped_p = newline_found_p = 1;
5292 }
5293 else
5294 {
5295 while (get_next_display_element (it)
5296 && !newline_found_p)
5297 {
5298 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5299 set_iterator_to_next (it, 0);
5300 }
5301 }
5302 }
5303
5304 it->selective = old_selective;
5305 return newline_found_p;
5306 }
5307
5308
5309 /* Set IT's current position to the previous visible line start. Skip
5310 invisible text that is so either due to text properties or due to
5311 selective display. Caution: this does not change IT->current_x and
5312 IT->hpos. */
5313
5314 static void
5315 back_to_previous_visible_line_start (it)
5316 struct it *it;
5317 {
5318 while (IT_CHARPOS (*it) > BEGV)
5319 {
5320 back_to_previous_line_start (it);
5321
5322 if (IT_CHARPOS (*it) <= BEGV)
5323 break;
5324
5325 /* If selective > 0, then lines indented more than that values
5326 are invisible. */
5327 if (it->selective > 0
5328 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5329 (double) it->selective)) /* iftc */
5330 continue;
5331
5332 /* Check the newline before point for invisibility. */
5333 {
5334 Lisp_Object prop;
5335 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5336 Qinvisible, it->window);
5337 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5338 continue;
5339 }
5340
5341 if (IT_CHARPOS (*it) <= BEGV)
5342 break;
5343
5344 {
5345 struct it it2;
5346 int pos;
5347 EMACS_INT beg, end;
5348 Lisp_Object val, overlay;
5349
5350 /* If newline is part of a composition, continue from start of composition */
5351 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5352 && beg < IT_CHARPOS (*it))
5353 goto replaced;
5354
5355 /* If newline is replaced by a display property, find start of overlay
5356 or interval and continue search from that point. */
5357 it2 = *it;
5358 pos = --IT_CHARPOS (it2);
5359 --IT_BYTEPOS (it2);
5360 it2.sp = 0;
5361 it2.string_from_display_prop_p = 0;
5362 if (handle_display_prop (&it2) == HANDLED_RETURN
5363 && !NILP (val = get_char_property_and_overlay
5364 (make_number (pos), Qdisplay, Qnil, &overlay))
5365 && (OVERLAYP (overlay)
5366 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5367 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5368 goto replaced;
5369
5370 /* Newline is not replaced by anything -- so we are done. */
5371 break;
5372
5373 replaced:
5374 if (beg < BEGV)
5375 beg = BEGV;
5376 IT_CHARPOS (*it) = beg;
5377 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5378 }
5379 }
5380
5381 it->continuation_lines_width = 0;
5382
5383 xassert (IT_CHARPOS (*it) >= BEGV);
5384 xassert (IT_CHARPOS (*it) == BEGV
5385 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5386 CHECK_IT (it);
5387 }
5388
5389
5390 /* Reseat iterator IT at the previous visible line start. Skip
5391 invisible text that is so either due to text properties or due to
5392 selective display. At the end, update IT's overlay information,
5393 face information etc. */
5394
5395 void
5396 reseat_at_previous_visible_line_start (it)
5397 struct it *it;
5398 {
5399 back_to_previous_visible_line_start (it);
5400 reseat (it, it->current.pos, 1);
5401 CHECK_IT (it);
5402 }
5403
5404
5405 /* Reseat iterator IT on the next visible line start in the current
5406 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5407 preceding the line start. Skip over invisible text that is so
5408 because of selective display. Compute faces, overlays etc at the
5409 new position. Note that this function does not skip over text that
5410 is invisible because of text properties. */
5411
5412 static void
5413 reseat_at_next_visible_line_start (it, on_newline_p)
5414 struct it *it;
5415 int on_newline_p;
5416 {
5417 int newline_found_p, skipped_p = 0;
5418
5419 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5420
5421 /* Skip over lines that are invisible because they are indented
5422 more than the value of IT->selective. */
5423 if (it->selective > 0)
5424 while (IT_CHARPOS (*it) < ZV
5425 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5426 (double) it->selective)) /* iftc */
5427 {
5428 xassert (IT_BYTEPOS (*it) == BEGV
5429 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5430 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5431 }
5432
5433 /* Position on the newline if that's what's requested. */
5434 if (on_newline_p && newline_found_p)
5435 {
5436 if (STRINGP (it->string))
5437 {
5438 if (IT_STRING_CHARPOS (*it) > 0)
5439 {
5440 --IT_STRING_CHARPOS (*it);
5441 --IT_STRING_BYTEPOS (*it);
5442 }
5443 }
5444 else if (IT_CHARPOS (*it) > BEGV)
5445 {
5446 --IT_CHARPOS (*it);
5447 --IT_BYTEPOS (*it);
5448 reseat (it, it->current.pos, 0);
5449 }
5450 }
5451 else if (skipped_p)
5452 reseat (it, it->current.pos, 0);
5453
5454 CHECK_IT (it);
5455 }
5456
5457
5458 \f
5459 /***********************************************************************
5460 Changing an iterator's position
5461 ***********************************************************************/
5462
5463 /* Change IT's current position to POS in current_buffer. If FORCE_P
5464 is non-zero, always check for text properties at the new position.
5465 Otherwise, text properties are only looked up if POS >=
5466 IT->check_charpos of a property. */
5467
5468 static void
5469 reseat (it, pos, force_p)
5470 struct it *it;
5471 struct text_pos pos;
5472 int force_p;
5473 {
5474 int original_pos = IT_CHARPOS (*it);
5475
5476 reseat_1 (it, pos, 0);
5477
5478 /* Determine where to check text properties. Avoid doing it
5479 where possible because text property lookup is very expensive. */
5480 if (force_p
5481 || CHARPOS (pos) > it->stop_charpos
5482 || CHARPOS (pos) < original_pos)
5483 handle_stop (it);
5484
5485 CHECK_IT (it);
5486 }
5487
5488
5489 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5490 IT->stop_pos to POS, also. */
5491
5492 static void
5493 reseat_1 (it, pos, set_stop_p)
5494 struct it *it;
5495 struct text_pos pos;
5496 int set_stop_p;
5497 {
5498 /* Don't call this function when scanning a C string. */
5499 xassert (it->s == NULL);
5500
5501 /* POS must be a reasonable value. */
5502 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5503
5504 it->current.pos = it->position = pos;
5505 it->end_charpos = ZV;
5506 it->dpvec = NULL;
5507 it->current.dpvec_index = -1;
5508 it->current.overlay_string_index = -1;
5509 IT_STRING_CHARPOS (*it) = -1;
5510 IT_STRING_BYTEPOS (*it) = -1;
5511 it->string = Qnil;
5512 it->string_from_display_prop_p = 0;
5513 it->method = GET_FROM_BUFFER;
5514 it->object = it->w->buffer;
5515 it->area = TEXT_AREA;
5516 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5517 it->sp = 0;
5518 it->string_from_display_prop_p = 0;
5519 it->face_before_selective_p = 0;
5520
5521 if (set_stop_p)
5522 it->stop_charpos = CHARPOS (pos);
5523 }
5524
5525
5526 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5527 If S is non-null, it is a C string to iterate over. Otherwise,
5528 STRING gives a Lisp string to iterate over.
5529
5530 If PRECISION > 0, don't return more then PRECISION number of
5531 characters from the string.
5532
5533 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5534 characters have been returned. FIELD_WIDTH < 0 means an infinite
5535 field width.
5536
5537 MULTIBYTE = 0 means disable processing of multibyte characters,
5538 MULTIBYTE > 0 means enable it,
5539 MULTIBYTE < 0 means use IT->multibyte_p.
5540
5541 IT must be initialized via a prior call to init_iterator before
5542 calling this function. */
5543
5544 static void
5545 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5546 struct it *it;
5547 unsigned char *s;
5548 Lisp_Object string;
5549 int charpos;
5550 int precision, field_width, multibyte;
5551 {
5552 /* No region in strings. */
5553 it->region_beg_charpos = it->region_end_charpos = -1;
5554
5555 /* No text property checks performed by default, but see below. */
5556 it->stop_charpos = -1;
5557
5558 /* Set iterator position and end position. */
5559 bzero (&it->current, sizeof it->current);
5560 it->current.overlay_string_index = -1;
5561 it->current.dpvec_index = -1;
5562 xassert (charpos >= 0);
5563
5564 /* If STRING is specified, use its multibyteness, otherwise use the
5565 setting of MULTIBYTE, if specified. */
5566 if (multibyte >= 0)
5567 it->multibyte_p = multibyte > 0;
5568
5569 if (s == NULL)
5570 {
5571 xassert (STRINGP (string));
5572 it->string = string;
5573 it->s = NULL;
5574 it->end_charpos = it->string_nchars = SCHARS (string);
5575 it->method = GET_FROM_STRING;
5576 it->current.string_pos = string_pos (charpos, string);
5577 }
5578 else
5579 {
5580 it->s = s;
5581 it->string = Qnil;
5582
5583 /* Note that we use IT->current.pos, not it->current.string_pos,
5584 for displaying C strings. */
5585 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5586 if (it->multibyte_p)
5587 {
5588 it->current.pos = c_string_pos (charpos, s, 1);
5589 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5590 }
5591 else
5592 {
5593 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5594 it->end_charpos = it->string_nchars = strlen (s);
5595 }
5596
5597 it->method = GET_FROM_C_STRING;
5598 }
5599
5600 /* PRECISION > 0 means don't return more than PRECISION characters
5601 from the string. */
5602 if (precision > 0 && it->end_charpos - charpos > precision)
5603 it->end_charpos = it->string_nchars = charpos + precision;
5604
5605 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5606 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5607 FIELD_WIDTH < 0 means infinite field width. This is useful for
5608 padding with `-' at the end of a mode line. */
5609 if (field_width < 0)
5610 field_width = INFINITY;
5611 if (field_width > it->end_charpos - charpos)
5612 it->end_charpos = charpos + field_width;
5613
5614 /* Use the standard display table for displaying strings. */
5615 if (DISP_TABLE_P (Vstandard_display_table))
5616 it->dp = XCHAR_TABLE (Vstandard_display_table);
5617
5618 it->stop_charpos = charpos;
5619 CHECK_IT (it);
5620 }
5621
5622
5623 \f
5624 /***********************************************************************
5625 Iteration
5626 ***********************************************************************/
5627
5628 /* Map enum it_method value to corresponding next_element_from_* function. */
5629
5630 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5631 {
5632 next_element_from_buffer,
5633 next_element_from_display_vector,
5634 next_element_from_string,
5635 next_element_from_c_string,
5636 next_element_from_image,
5637 next_element_from_stretch
5638 };
5639
5640 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5641
5642
5643 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5644 (possibly with the following characters). */
5645
5646 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5647 ((IT)->cmp_it.id >= 0 \
5648 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5649 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5650 (IT)->end_charpos, (IT)->w, \
5651 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5652 (IT)->string)))
5653
5654
5655 /* Load IT's display element fields with information about the next
5656 display element from the current position of IT. Value is zero if
5657 end of buffer (or C string) is reached. */
5658
5659 static struct frame *last_escape_glyph_frame = NULL;
5660 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5661 static int last_escape_glyph_merged_face_id = 0;
5662
5663 int
5664 get_next_display_element (it)
5665 struct it *it;
5666 {
5667 /* Non-zero means that we found a display element. Zero means that
5668 we hit the end of what we iterate over. Performance note: the
5669 function pointer `method' used here turns out to be faster than
5670 using a sequence of if-statements. */
5671 int success_p;
5672
5673 get_next:
5674 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5675
5676 if (it->what == IT_CHARACTER)
5677 {
5678 /* Map via display table or translate control characters.
5679 IT->c, IT->len etc. have been set to the next character by
5680 the function call above. If we have a display table, and it
5681 contains an entry for IT->c, translate it. Don't do this if
5682 IT->c itself comes from a display table, otherwise we could
5683 end up in an infinite recursion. (An alternative could be to
5684 count the recursion depth of this function and signal an
5685 error when a certain maximum depth is reached.) Is it worth
5686 it? */
5687 if (success_p && it->dpvec == NULL)
5688 {
5689 Lisp_Object dv;
5690
5691 if (it->dp
5692 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5693 VECTORP (dv)))
5694 {
5695 struct Lisp_Vector *v = XVECTOR (dv);
5696
5697 /* Return the first character from the display table
5698 entry, if not empty. If empty, don't display the
5699 current character. */
5700 if (v->size)
5701 {
5702 it->dpvec_char_len = it->len;
5703 it->dpvec = v->contents;
5704 it->dpend = v->contents + v->size;
5705 it->current.dpvec_index = 0;
5706 it->dpvec_face_id = -1;
5707 it->saved_face_id = it->face_id;
5708 it->method = GET_FROM_DISPLAY_VECTOR;
5709 it->ellipsis_p = 0;
5710 }
5711 else
5712 {
5713 set_iterator_to_next (it, 0);
5714 }
5715 goto get_next;
5716 }
5717
5718 /* Translate control characters into `\003' or `^C' form.
5719 Control characters coming from a display table entry are
5720 currently not translated because we use IT->dpvec to hold
5721 the translation. This could easily be changed but I
5722 don't believe that it is worth doing.
5723
5724 If it->multibyte_p is nonzero, non-printable non-ASCII
5725 characters are also translated to octal form.
5726
5727 If it->multibyte_p is zero, eight-bit characters that
5728 don't have corresponding multibyte char code are also
5729 translated to octal form. */
5730 else if ((it->c < ' '
5731 ? (it->area != TEXT_AREA
5732 /* In mode line, treat \n, \t like other crl chars. */
5733 || (it->c != '\t'
5734 && it->glyph_row && it->glyph_row->mode_line_p)
5735 || (it->c != '\n' && it->c != '\t'))
5736 : (it->multibyte_p
5737 ? (!CHAR_PRINTABLE_P (it->c)
5738 || (!NILP (Vnobreak_char_display)
5739 && (it->c == 0xA0 /* NO-BREAK SPACE */
5740 || it->c == 0xAD /* SOFT HYPHEN */)))
5741 : (it->c >= 127
5742 && (! unibyte_display_via_language_environment
5743 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5744 {
5745 /* IT->c is a control character which must be displayed
5746 either as '\003' or as `^C' where the '\\' and '^'
5747 can be defined in the display table. Fill
5748 IT->ctl_chars with glyphs for what we have to
5749 display. Then, set IT->dpvec to these glyphs. */
5750 Lisp_Object gc;
5751 int ctl_len;
5752 int face_id, lface_id = 0 ;
5753 int escape_glyph;
5754
5755 /* Handle control characters with ^. */
5756
5757 if (it->c < 128 && it->ctl_arrow_p)
5758 {
5759 int g;
5760
5761 g = '^'; /* default glyph for Control */
5762 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5763 if (it->dp
5764 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5765 && GLYPH_CODE_CHAR_VALID_P (gc))
5766 {
5767 g = GLYPH_CODE_CHAR (gc);
5768 lface_id = GLYPH_CODE_FACE (gc);
5769 }
5770 if (lface_id)
5771 {
5772 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5773 }
5774 else if (it->f == last_escape_glyph_frame
5775 && it->face_id == last_escape_glyph_face_id)
5776 {
5777 face_id = last_escape_glyph_merged_face_id;
5778 }
5779 else
5780 {
5781 /* Merge the escape-glyph face into the current face. */
5782 face_id = merge_faces (it->f, Qescape_glyph, 0,
5783 it->face_id);
5784 last_escape_glyph_frame = it->f;
5785 last_escape_glyph_face_id = it->face_id;
5786 last_escape_glyph_merged_face_id = face_id;
5787 }
5788
5789 XSETINT (it->ctl_chars[0], g);
5790 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5791 ctl_len = 2;
5792 goto display_control;
5793 }
5794
5795 /* Handle non-break space in the mode where it only gets
5796 highlighting. */
5797
5798 if (EQ (Vnobreak_char_display, Qt)
5799 && it->c == 0xA0)
5800 {
5801 /* Merge the no-break-space face into the current face. */
5802 face_id = merge_faces (it->f, Qnobreak_space, 0,
5803 it->face_id);
5804
5805 it->c = ' ';
5806 XSETINT (it->ctl_chars[0], ' ');
5807 ctl_len = 1;
5808 goto display_control;
5809 }
5810
5811 /* Handle sequences that start with the "escape glyph". */
5812
5813 /* the default escape glyph is \. */
5814 escape_glyph = '\\';
5815
5816 if (it->dp
5817 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5818 && GLYPH_CODE_CHAR_VALID_P (gc))
5819 {
5820 escape_glyph = GLYPH_CODE_CHAR (gc);
5821 lface_id = GLYPH_CODE_FACE (gc);
5822 }
5823 if (lface_id)
5824 {
5825 /* The display table specified a face.
5826 Merge it into face_id and also into escape_glyph. */
5827 face_id = merge_faces (it->f, Qt, lface_id,
5828 it->face_id);
5829 }
5830 else if (it->f == last_escape_glyph_frame
5831 && it->face_id == last_escape_glyph_face_id)
5832 {
5833 face_id = last_escape_glyph_merged_face_id;
5834 }
5835 else
5836 {
5837 /* Merge the escape-glyph face into the current face. */
5838 face_id = merge_faces (it->f, Qescape_glyph, 0,
5839 it->face_id);
5840 last_escape_glyph_frame = it->f;
5841 last_escape_glyph_face_id = it->face_id;
5842 last_escape_glyph_merged_face_id = face_id;
5843 }
5844
5845 /* Handle soft hyphens in the mode where they only get
5846 highlighting. */
5847
5848 if (EQ (Vnobreak_char_display, Qt)
5849 && it->c == 0xAD)
5850 {
5851 it->c = '-';
5852 XSETINT (it->ctl_chars[0], '-');
5853 ctl_len = 1;
5854 goto display_control;
5855 }
5856
5857 /* Handle non-break space and soft hyphen
5858 with the escape glyph. */
5859
5860 if (it->c == 0xA0 || it->c == 0xAD)
5861 {
5862 XSETINT (it->ctl_chars[0], escape_glyph);
5863 it->c = (it->c == 0xA0 ? ' ' : '-');
5864 XSETINT (it->ctl_chars[1], it->c);
5865 ctl_len = 2;
5866 goto display_control;
5867 }
5868
5869 {
5870 unsigned char str[MAX_MULTIBYTE_LENGTH];
5871 int len;
5872 int i;
5873
5874 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5875 if (CHAR_BYTE8_P (it->c))
5876 {
5877 str[0] = CHAR_TO_BYTE8 (it->c);
5878 len = 1;
5879 }
5880 else if (it->c < 256)
5881 {
5882 str[0] = it->c;
5883 len = 1;
5884 }
5885 else
5886 {
5887 /* It's an invalid character, which shouldn't
5888 happen actually, but due to bugs it may
5889 happen. Let's print the char as is, there's
5890 not much meaningful we can do with it. */
5891 str[0] = it->c;
5892 str[1] = it->c >> 8;
5893 str[2] = it->c >> 16;
5894 str[3] = it->c >> 24;
5895 len = 4;
5896 }
5897
5898 for (i = 0; i < len; i++)
5899 {
5900 int g;
5901 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5902 /* Insert three more glyphs into IT->ctl_chars for
5903 the octal display of the character. */
5904 g = ((str[i] >> 6) & 7) + '0';
5905 XSETINT (it->ctl_chars[i * 4 + 1], g);
5906 g = ((str[i] >> 3) & 7) + '0';
5907 XSETINT (it->ctl_chars[i * 4 + 2], g);
5908 g = (str[i] & 7) + '0';
5909 XSETINT (it->ctl_chars[i * 4 + 3], g);
5910 }
5911 ctl_len = len * 4;
5912 }
5913
5914 display_control:
5915 /* Set up IT->dpvec and return first character from it. */
5916 it->dpvec_char_len = it->len;
5917 it->dpvec = it->ctl_chars;
5918 it->dpend = it->dpvec + ctl_len;
5919 it->current.dpvec_index = 0;
5920 it->dpvec_face_id = face_id;
5921 it->saved_face_id = it->face_id;
5922 it->method = GET_FROM_DISPLAY_VECTOR;
5923 it->ellipsis_p = 0;
5924 goto get_next;
5925 }
5926 }
5927 }
5928
5929 #ifdef HAVE_WINDOW_SYSTEM
5930 /* Adjust face id for a multibyte character. There are no multibyte
5931 character in unibyte text. */
5932 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5933 && it->multibyte_p
5934 && success_p
5935 && FRAME_WINDOW_P (it->f))
5936 {
5937 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5938
5939 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5940 {
5941 /* Automatic composition with glyph-string. */
5942 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5943
5944 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5945 }
5946 else
5947 {
5948 int pos = (it->s ? -1
5949 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5950 : IT_CHARPOS (*it));
5951
5952 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5953 }
5954 }
5955 #endif
5956
5957 /* Is this character the last one of a run of characters with
5958 box? If yes, set IT->end_of_box_run_p to 1. */
5959 if (it->face_box_p
5960 && it->s == NULL)
5961 {
5962 if (it->method == GET_FROM_STRING && it->sp)
5963 {
5964 int face_id = underlying_face_id (it);
5965 struct face *face = FACE_FROM_ID (it->f, face_id);
5966
5967 if (face)
5968 {
5969 if (face->box == FACE_NO_BOX)
5970 {
5971 /* If the box comes from face properties in a
5972 display string, check faces in that string. */
5973 int string_face_id = face_after_it_pos (it);
5974 it->end_of_box_run_p
5975 = (FACE_FROM_ID (it->f, string_face_id)->box
5976 == FACE_NO_BOX);
5977 }
5978 /* Otherwise, the box comes from the underlying face.
5979 If this is the last string character displayed, check
5980 the next buffer location. */
5981 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5982 && (it->current.overlay_string_index
5983 == it->n_overlay_strings - 1))
5984 {
5985 EMACS_INT ignore;
5986 int next_face_id;
5987 struct text_pos pos = it->current.pos;
5988 INC_TEXT_POS (pos, it->multibyte_p);
5989
5990 next_face_id = face_at_buffer_position
5991 (it->w, CHARPOS (pos), it->region_beg_charpos,
5992 it->region_end_charpos, &ignore,
5993 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0);
5994 it->end_of_box_run_p
5995 = (FACE_FROM_ID (it->f, next_face_id)->box
5996 == FACE_NO_BOX);
5997 }
5998 }
5999 }
6000 else
6001 {
6002 int face_id = face_after_it_pos (it);
6003 it->end_of_box_run_p
6004 = (face_id != it->face_id
6005 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6006 }
6007 }
6008
6009 /* Value is 0 if end of buffer or string reached. */
6010 return success_p;
6011 }
6012
6013
6014 /* Move IT to the next display element.
6015
6016 RESEAT_P non-zero means if called on a newline in buffer text,
6017 skip to the next visible line start.
6018
6019 Functions get_next_display_element and set_iterator_to_next are
6020 separate because I find this arrangement easier to handle than a
6021 get_next_display_element function that also increments IT's
6022 position. The way it is we can first look at an iterator's current
6023 display element, decide whether it fits on a line, and if it does,
6024 increment the iterator position. The other way around we probably
6025 would either need a flag indicating whether the iterator has to be
6026 incremented the next time, or we would have to implement a
6027 decrement position function which would not be easy to write. */
6028
6029 void
6030 set_iterator_to_next (it, reseat_p)
6031 struct it *it;
6032 int reseat_p;
6033 {
6034 /* Reset flags indicating start and end of a sequence of characters
6035 with box. Reset them at the start of this function because
6036 moving the iterator to a new position might set them. */
6037 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6038
6039 switch (it->method)
6040 {
6041 case GET_FROM_BUFFER:
6042 /* The current display element of IT is a character from
6043 current_buffer. Advance in the buffer, and maybe skip over
6044 invisible lines that are so because of selective display. */
6045 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6046 reseat_at_next_visible_line_start (it, 0);
6047 else if (it->cmp_it.id >= 0)
6048 {
6049 IT_CHARPOS (*it) += it->cmp_it.nchars;
6050 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6051 if (it->cmp_it.to < it->cmp_it.nglyphs)
6052 it->cmp_it.from = it->cmp_it.to;
6053 else
6054 {
6055 it->cmp_it.id = -1;
6056 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6057 IT_BYTEPOS (*it), it->stop_charpos,
6058 Qnil);
6059 }
6060 }
6061 else
6062 {
6063 xassert (it->len != 0);
6064 IT_BYTEPOS (*it) += it->len;
6065 IT_CHARPOS (*it) += 1;
6066 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6067 }
6068 break;
6069
6070 case GET_FROM_C_STRING:
6071 /* Current display element of IT is from a C string. */
6072 IT_BYTEPOS (*it) += it->len;
6073 IT_CHARPOS (*it) += 1;
6074 break;
6075
6076 case GET_FROM_DISPLAY_VECTOR:
6077 /* Current display element of IT is from a display table entry.
6078 Advance in the display table definition. Reset it to null if
6079 end reached, and continue with characters from buffers/
6080 strings. */
6081 ++it->current.dpvec_index;
6082
6083 /* Restore face of the iterator to what they were before the
6084 display vector entry (these entries may contain faces). */
6085 it->face_id = it->saved_face_id;
6086
6087 if (it->dpvec + it->current.dpvec_index == it->dpend)
6088 {
6089 int recheck_faces = it->ellipsis_p;
6090
6091 if (it->s)
6092 it->method = GET_FROM_C_STRING;
6093 else if (STRINGP (it->string))
6094 it->method = GET_FROM_STRING;
6095 else
6096 {
6097 it->method = GET_FROM_BUFFER;
6098 it->object = it->w->buffer;
6099 }
6100
6101 it->dpvec = NULL;
6102 it->current.dpvec_index = -1;
6103
6104 /* Skip over characters which were displayed via IT->dpvec. */
6105 if (it->dpvec_char_len < 0)
6106 reseat_at_next_visible_line_start (it, 1);
6107 else if (it->dpvec_char_len > 0)
6108 {
6109 if (it->method == GET_FROM_STRING
6110 && it->n_overlay_strings > 0)
6111 it->ignore_overlay_strings_at_pos_p = 1;
6112 it->len = it->dpvec_char_len;
6113 set_iterator_to_next (it, reseat_p);
6114 }
6115
6116 /* Maybe recheck faces after display vector */
6117 if (recheck_faces)
6118 it->stop_charpos = IT_CHARPOS (*it);
6119 }
6120 break;
6121
6122 case GET_FROM_STRING:
6123 /* Current display element is a character from a Lisp string. */
6124 xassert (it->s == NULL && STRINGP (it->string));
6125 if (it->cmp_it.id >= 0)
6126 {
6127 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6128 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6129 if (it->cmp_it.to < it->cmp_it.nglyphs)
6130 it->cmp_it.from = it->cmp_it.to;
6131 else
6132 {
6133 it->cmp_it.id = -1;
6134 composition_compute_stop_pos (&it->cmp_it,
6135 IT_STRING_CHARPOS (*it),
6136 IT_STRING_BYTEPOS (*it),
6137 it->stop_charpos, it->string);
6138 }
6139 }
6140 else
6141 {
6142 IT_STRING_BYTEPOS (*it) += it->len;
6143 IT_STRING_CHARPOS (*it) += 1;
6144 }
6145
6146 consider_string_end:
6147
6148 if (it->current.overlay_string_index >= 0)
6149 {
6150 /* IT->string is an overlay string. Advance to the
6151 next, if there is one. */
6152 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6153 {
6154 it->ellipsis_p = 0;
6155 next_overlay_string (it);
6156 if (it->ellipsis_p)
6157 setup_for_ellipsis (it, 0);
6158 }
6159 }
6160 else
6161 {
6162 /* IT->string is not an overlay string. If we reached
6163 its end, and there is something on IT->stack, proceed
6164 with what is on the stack. This can be either another
6165 string, this time an overlay string, or a buffer. */
6166 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6167 && it->sp > 0)
6168 {
6169 pop_it (it);
6170 if (it->method == GET_FROM_STRING)
6171 goto consider_string_end;
6172 }
6173 }
6174 break;
6175
6176 case GET_FROM_IMAGE:
6177 case GET_FROM_STRETCH:
6178 /* The position etc with which we have to proceed are on
6179 the stack. The position may be at the end of a string,
6180 if the `display' property takes up the whole string. */
6181 xassert (it->sp > 0);
6182 pop_it (it);
6183 if (it->method == GET_FROM_STRING)
6184 goto consider_string_end;
6185 break;
6186
6187 default:
6188 /* There are no other methods defined, so this should be a bug. */
6189 abort ();
6190 }
6191
6192 xassert (it->method != GET_FROM_STRING
6193 || (STRINGP (it->string)
6194 && IT_STRING_CHARPOS (*it) >= 0));
6195 }
6196
6197 /* Load IT's display element fields with information about the next
6198 display element which comes from a display table entry or from the
6199 result of translating a control character to one of the forms `^C'
6200 or `\003'.
6201
6202 IT->dpvec holds the glyphs to return as characters.
6203 IT->saved_face_id holds the face id before the display vector--
6204 it is restored into IT->face_idin set_iterator_to_next. */
6205
6206 static int
6207 next_element_from_display_vector (it)
6208 struct it *it;
6209 {
6210 Lisp_Object gc;
6211
6212 /* Precondition. */
6213 xassert (it->dpvec && it->current.dpvec_index >= 0);
6214
6215 it->face_id = it->saved_face_id;
6216
6217 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6218 That seemed totally bogus - so I changed it... */
6219 gc = it->dpvec[it->current.dpvec_index];
6220
6221 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6222 {
6223 it->c = GLYPH_CODE_CHAR (gc);
6224 it->len = CHAR_BYTES (it->c);
6225
6226 /* The entry may contain a face id to use. Such a face id is
6227 the id of a Lisp face, not a realized face. A face id of
6228 zero means no face is specified. */
6229 if (it->dpvec_face_id >= 0)
6230 it->face_id = it->dpvec_face_id;
6231 else
6232 {
6233 int lface_id = GLYPH_CODE_FACE (gc);
6234 if (lface_id > 0)
6235 it->face_id = merge_faces (it->f, Qt, lface_id,
6236 it->saved_face_id);
6237 }
6238 }
6239 else
6240 /* Display table entry is invalid. Return a space. */
6241 it->c = ' ', it->len = 1;
6242
6243 /* Don't change position and object of the iterator here. They are
6244 still the values of the character that had this display table
6245 entry or was translated, and that's what we want. */
6246 it->what = IT_CHARACTER;
6247 return 1;
6248 }
6249
6250
6251 /* Load IT with the next display element from Lisp string IT->string.
6252 IT->current.string_pos is the current position within the string.
6253 If IT->current.overlay_string_index >= 0, the Lisp string is an
6254 overlay string. */
6255
6256 static int
6257 next_element_from_string (it)
6258 struct it *it;
6259 {
6260 struct text_pos position;
6261
6262 xassert (STRINGP (it->string));
6263 xassert (IT_STRING_CHARPOS (*it) >= 0);
6264 position = it->current.string_pos;
6265
6266 /* Time to check for invisible text? */
6267 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6268 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6269 {
6270 handle_stop (it);
6271
6272 /* Since a handler may have changed IT->method, we must
6273 recurse here. */
6274 return GET_NEXT_DISPLAY_ELEMENT (it);
6275 }
6276
6277 if (it->current.overlay_string_index >= 0)
6278 {
6279 /* Get the next character from an overlay string. In overlay
6280 strings, There is no field width or padding with spaces to
6281 do. */
6282 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6283 {
6284 it->what = IT_EOB;
6285 return 0;
6286 }
6287 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6288 IT_STRING_BYTEPOS (*it))
6289 && next_element_from_composition (it))
6290 {
6291 return 1;
6292 }
6293 else if (STRING_MULTIBYTE (it->string))
6294 {
6295 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6296 const unsigned char *s = (SDATA (it->string)
6297 + IT_STRING_BYTEPOS (*it));
6298 it->c = string_char_and_length (s, remaining, &it->len);
6299 }
6300 else
6301 {
6302 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6303 it->len = 1;
6304 }
6305 }
6306 else
6307 {
6308 /* Get the next character from a Lisp string that is not an
6309 overlay string. Such strings come from the mode line, for
6310 example. We may have to pad with spaces, or truncate the
6311 string. See also next_element_from_c_string. */
6312 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6313 {
6314 it->what = IT_EOB;
6315 return 0;
6316 }
6317 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6318 {
6319 /* Pad with spaces. */
6320 it->c = ' ', it->len = 1;
6321 CHARPOS (position) = BYTEPOS (position) = -1;
6322 }
6323 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6324 IT_STRING_BYTEPOS (*it))
6325 && next_element_from_composition (it))
6326 {
6327 return 1;
6328 }
6329 else if (STRING_MULTIBYTE (it->string))
6330 {
6331 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6332 const unsigned char *s = (SDATA (it->string)
6333 + IT_STRING_BYTEPOS (*it));
6334 it->c = string_char_and_length (s, maxlen, &it->len);
6335 }
6336 else
6337 {
6338 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6339 it->len = 1;
6340 }
6341 }
6342
6343 /* Record what we have and where it came from. */
6344 it->what = IT_CHARACTER;
6345 it->object = it->string;
6346 it->position = position;
6347 return 1;
6348 }
6349
6350
6351 /* Load IT with next display element from C string IT->s.
6352 IT->string_nchars is the maximum number of characters to return
6353 from the string. IT->end_charpos may be greater than
6354 IT->string_nchars when this function is called, in which case we
6355 may have to return padding spaces. Value is zero if end of string
6356 reached, including padding spaces. */
6357
6358 static int
6359 next_element_from_c_string (it)
6360 struct it *it;
6361 {
6362 int success_p = 1;
6363
6364 xassert (it->s);
6365 it->what = IT_CHARACTER;
6366 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6367 it->object = Qnil;
6368
6369 /* IT's position can be greater IT->string_nchars in case a field
6370 width or precision has been specified when the iterator was
6371 initialized. */
6372 if (IT_CHARPOS (*it) >= it->end_charpos)
6373 {
6374 /* End of the game. */
6375 it->what = IT_EOB;
6376 success_p = 0;
6377 }
6378 else if (IT_CHARPOS (*it) >= it->string_nchars)
6379 {
6380 /* Pad with spaces. */
6381 it->c = ' ', it->len = 1;
6382 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6383 }
6384 else if (it->multibyte_p)
6385 {
6386 /* Implementation note: The calls to strlen apparently aren't a
6387 performance problem because there is no noticeable performance
6388 difference between Emacs running in unibyte or multibyte mode. */
6389 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6390 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6391 maxlen, &it->len);
6392 }
6393 else
6394 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6395
6396 return success_p;
6397 }
6398
6399
6400 /* Set up IT to return characters from an ellipsis, if appropriate.
6401 The definition of the ellipsis glyphs may come from a display table
6402 entry. This function Fills IT with the first glyph from the
6403 ellipsis if an ellipsis is to be displayed. */
6404
6405 static int
6406 next_element_from_ellipsis (it)
6407 struct it *it;
6408 {
6409 if (it->selective_display_ellipsis_p)
6410 setup_for_ellipsis (it, it->len);
6411 else
6412 {
6413 /* The face at the current position may be different from the
6414 face we find after the invisible text. Remember what it
6415 was in IT->saved_face_id, and signal that it's there by
6416 setting face_before_selective_p. */
6417 it->saved_face_id = it->face_id;
6418 it->method = GET_FROM_BUFFER;
6419 it->object = it->w->buffer;
6420 reseat_at_next_visible_line_start (it, 1);
6421 it->face_before_selective_p = 1;
6422 }
6423
6424 return GET_NEXT_DISPLAY_ELEMENT (it);
6425 }
6426
6427
6428 /* Deliver an image display element. The iterator IT is already
6429 filled with image information (done in handle_display_prop). Value
6430 is always 1. */
6431
6432
6433 static int
6434 next_element_from_image (it)
6435 struct it *it;
6436 {
6437 it->what = IT_IMAGE;
6438 return 1;
6439 }
6440
6441
6442 /* Fill iterator IT with next display element from a stretch glyph
6443 property. IT->object is the value of the text property. Value is
6444 always 1. */
6445
6446 static int
6447 next_element_from_stretch (it)
6448 struct it *it;
6449 {
6450 it->what = IT_STRETCH;
6451 return 1;
6452 }
6453
6454
6455 /* Load IT with the next display element from current_buffer. Value
6456 is zero if end of buffer reached. IT->stop_charpos is the next
6457 position at which to stop and check for text properties or buffer
6458 end. */
6459
6460 static int
6461 next_element_from_buffer (it)
6462 struct it *it;
6463 {
6464 int success_p = 1;
6465
6466 xassert (IT_CHARPOS (*it) >= BEGV);
6467
6468 if (IT_CHARPOS (*it) >= it->stop_charpos)
6469 {
6470 if (IT_CHARPOS (*it) >= it->end_charpos)
6471 {
6472 int overlay_strings_follow_p;
6473
6474 /* End of the game, except when overlay strings follow that
6475 haven't been returned yet. */
6476 if (it->overlay_strings_at_end_processed_p)
6477 overlay_strings_follow_p = 0;
6478 else
6479 {
6480 it->overlay_strings_at_end_processed_p = 1;
6481 overlay_strings_follow_p = get_overlay_strings (it, 0);
6482 }
6483
6484 if (overlay_strings_follow_p)
6485 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6486 else
6487 {
6488 it->what = IT_EOB;
6489 it->position = it->current.pos;
6490 success_p = 0;
6491 }
6492 }
6493 else
6494 {
6495 handle_stop (it);
6496 return GET_NEXT_DISPLAY_ELEMENT (it);
6497 }
6498 }
6499 else
6500 {
6501 /* No face changes, overlays etc. in sight, so just return a
6502 character from current_buffer. */
6503 unsigned char *p;
6504
6505 /* Maybe run the redisplay end trigger hook. Performance note:
6506 This doesn't seem to cost measurable time. */
6507 if (it->redisplay_end_trigger_charpos
6508 && it->glyph_row
6509 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6510 run_redisplay_end_trigger_hook (it);
6511
6512 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6513 && next_element_from_composition (it))
6514 {
6515 return 1;
6516 }
6517
6518 /* Get the next character, maybe multibyte. */
6519 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6520 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6521 it->c = STRING_CHAR_AND_LENGTH (p, 0, it->len);
6522 else
6523 it->c = *p, it->len = 1;
6524
6525 /* Record what we have and where it came from. */
6526 it->what = IT_CHARACTER;
6527 it->object = it->w->buffer;
6528 it->position = it->current.pos;
6529
6530 /* Normally we return the character found above, except when we
6531 really want to return an ellipsis for selective display. */
6532 if (it->selective)
6533 {
6534 if (it->c == '\n')
6535 {
6536 /* A value of selective > 0 means hide lines indented more
6537 than that number of columns. */
6538 if (it->selective > 0
6539 && IT_CHARPOS (*it) + 1 < ZV
6540 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6541 IT_BYTEPOS (*it) + 1,
6542 (double) it->selective)) /* iftc */
6543 {
6544 success_p = next_element_from_ellipsis (it);
6545 it->dpvec_char_len = -1;
6546 }
6547 }
6548 else if (it->c == '\r' && it->selective == -1)
6549 {
6550 /* A value of selective == -1 means that everything from the
6551 CR to the end of the line is invisible, with maybe an
6552 ellipsis displayed for it. */
6553 success_p = next_element_from_ellipsis (it);
6554 it->dpvec_char_len = -1;
6555 }
6556 }
6557 }
6558
6559 /* Value is zero if end of buffer reached. */
6560 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6561 return success_p;
6562 }
6563
6564
6565 /* Run the redisplay end trigger hook for IT. */
6566
6567 static void
6568 run_redisplay_end_trigger_hook (it)
6569 struct it *it;
6570 {
6571 Lisp_Object args[3];
6572
6573 /* IT->glyph_row should be non-null, i.e. we should be actually
6574 displaying something, or otherwise we should not run the hook. */
6575 xassert (it->glyph_row);
6576
6577 /* Set up hook arguments. */
6578 args[0] = Qredisplay_end_trigger_functions;
6579 args[1] = it->window;
6580 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6581 it->redisplay_end_trigger_charpos = 0;
6582
6583 /* Since we are *trying* to run these functions, don't try to run
6584 them again, even if they get an error. */
6585 it->w->redisplay_end_trigger = Qnil;
6586 Frun_hook_with_args (3, args);
6587
6588 /* Notice if it changed the face of the character we are on. */
6589 handle_face_prop (it);
6590 }
6591
6592
6593 /* Deliver a composition display element. Unlike the other
6594 next_element_from_XXX, this function is not registered in the array
6595 get_next_element[]. It is called from next_element_from_buffer and
6596 next_element_from_string when necessary. */
6597
6598 static int
6599 next_element_from_composition (it)
6600 struct it *it;
6601 {
6602 it->what = IT_COMPOSITION;
6603 it->len = it->cmp_it.nbytes;
6604 if (STRINGP (it->string))
6605 {
6606 if (it->c < 0)
6607 {
6608 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6609 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6610 return 0;
6611 }
6612 it->position = it->current.string_pos;
6613 it->object = it->string;
6614 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6615 IT_STRING_BYTEPOS (*it), it->string);
6616 }
6617 else
6618 {
6619 if (it->c < 0)
6620 {
6621 IT_CHARPOS (*it) += it->cmp_it.nchars;
6622 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6623 return 0;
6624 }
6625 it->position = it->current.pos;
6626 it->object = it->w->buffer;
6627 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6628 IT_BYTEPOS (*it), Qnil);
6629 }
6630 return 1;
6631 }
6632
6633
6634 \f
6635 /***********************************************************************
6636 Moving an iterator without producing glyphs
6637 ***********************************************************************/
6638
6639 /* Check if iterator is at a position corresponding to a valid buffer
6640 position after some move_it_ call. */
6641
6642 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6643 ((it)->method == GET_FROM_STRING \
6644 ? IT_STRING_CHARPOS (*it) == 0 \
6645 : 1)
6646
6647
6648 /* Move iterator IT to a specified buffer or X position within one
6649 line on the display without producing glyphs.
6650
6651 OP should be a bit mask including some or all of these bits:
6652 MOVE_TO_X: Stop on reaching x-position TO_X.
6653 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6654 Regardless of OP's value, stop in reaching the end of the display line.
6655
6656 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6657 This means, in particular, that TO_X includes window's horizontal
6658 scroll amount.
6659
6660 The return value has several possible values that
6661 say what condition caused the scan to stop:
6662
6663 MOVE_POS_MATCH_OR_ZV
6664 - when TO_POS or ZV was reached.
6665
6666 MOVE_X_REACHED
6667 -when TO_X was reached before TO_POS or ZV were reached.
6668
6669 MOVE_LINE_CONTINUED
6670 - when we reached the end of the display area and the line must
6671 be continued.
6672
6673 MOVE_LINE_TRUNCATED
6674 - when we reached the end of the display area and the line is
6675 truncated.
6676
6677 MOVE_NEWLINE_OR_CR
6678 - when we stopped at a line end, i.e. a newline or a CR and selective
6679 display is on. */
6680
6681 static enum move_it_result
6682 move_it_in_display_line_to (struct it *it,
6683 EMACS_INT to_charpos, int to_x,
6684 enum move_operation_enum op)
6685 {
6686 enum move_it_result result = MOVE_UNDEFINED;
6687 struct glyph_row *saved_glyph_row;
6688 struct it wrap_it, atpos_it, atx_it;
6689 int may_wrap = 0;
6690
6691 /* Don't produce glyphs in produce_glyphs. */
6692 saved_glyph_row = it->glyph_row;
6693 it->glyph_row = NULL;
6694
6695 /* Use wrap_it to save a copy of IT wherever a word wrap could
6696 occur. Use atpos_it to save a copy of IT at the desired buffer
6697 position, if found, so that we can scan ahead and check if the
6698 word later overshoots the window edge. Use atx_it similarly, for
6699 pixel positions. */
6700 wrap_it.sp = -1;
6701 atpos_it.sp = -1;
6702 atx_it.sp = -1;
6703
6704 #define BUFFER_POS_REACHED_P() \
6705 ((op & MOVE_TO_POS) != 0 \
6706 && BUFFERP (it->object) \
6707 && IT_CHARPOS (*it) >= to_charpos \
6708 && (it->method == GET_FROM_BUFFER \
6709 || (it->method == GET_FROM_DISPLAY_VECTOR \
6710 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6711
6712 /* If there's a line-/wrap-prefix, handle it. */
6713 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6714 && it->current_y < it->last_visible_y)
6715 handle_line_prefix (it);
6716
6717 while (1)
6718 {
6719 int x, i, ascent = 0, descent = 0;
6720
6721 /* Utility macro to reset an iterator with x, ascent, and descent. */
6722 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6723 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6724 (IT)->max_descent = descent)
6725
6726 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6727 glyph). */
6728 if ((op & MOVE_TO_POS) != 0
6729 && BUFFERP (it->object)
6730 && it->method == GET_FROM_BUFFER
6731 && IT_CHARPOS (*it) > to_charpos)
6732 {
6733 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6734 {
6735 result = MOVE_POS_MATCH_OR_ZV;
6736 break;
6737 }
6738 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6739 /* If wrap_it is valid, the current position might be in a
6740 word that is wrapped. So, save the iterator in
6741 atpos_it and continue to see if wrapping happens. */
6742 atpos_it = *it;
6743 }
6744
6745 /* Stop when ZV reached.
6746 We used to stop here when TO_CHARPOS reached as well, but that is
6747 too soon if this glyph does not fit on this line. So we handle it
6748 explicitly below. */
6749 if (!get_next_display_element (it))
6750 {
6751 result = MOVE_POS_MATCH_OR_ZV;
6752 break;
6753 }
6754
6755 if (it->line_wrap == TRUNCATE)
6756 {
6757 if (BUFFER_POS_REACHED_P ())
6758 {
6759 result = MOVE_POS_MATCH_OR_ZV;
6760 break;
6761 }
6762 }
6763 else
6764 {
6765 if (it->line_wrap == WORD_WRAP)
6766 {
6767 if (IT_DISPLAYING_WHITESPACE (it))
6768 may_wrap = 1;
6769 else if (may_wrap)
6770 {
6771 /* We have reached a glyph that follows one or more
6772 whitespace characters. If the position is
6773 already found, we are done. */
6774 if (atpos_it.sp >= 0)
6775 {
6776 *it = atpos_it;
6777 result = MOVE_POS_MATCH_OR_ZV;
6778 goto done;
6779 }
6780 if (atx_it.sp >= 0)
6781 {
6782 *it = atx_it;
6783 result = MOVE_X_REACHED;
6784 goto done;
6785 }
6786 /* Otherwise, we can wrap here. */
6787 wrap_it = *it;
6788 may_wrap = 0;
6789 }
6790 }
6791 }
6792
6793 /* Remember the line height for the current line, in case
6794 the next element doesn't fit on the line. */
6795 ascent = it->max_ascent;
6796 descent = it->max_descent;
6797
6798 /* The call to produce_glyphs will get the metrics of the
6799 display element IT is loaded with. Record the x-position
6800 before this display element, in case it doesn't fit on the
6801 line. */
6802 x = it->current_x;
6803
6804 PRODUCE_GLYPHS (it);
6805
6806 if (it->area != TEXT_AREA)
6807 {
6808 set_iterator_to_next (it, 1);
6809 continue;
6810 }
6811
6812 /* The number of glyphs we get back in IT->nglyphs will normally
6813 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6814 character on a terminal frame, or (iii) a line end. For the
6815 second case, IT->nglyphs - 1 padding glyphs will be present
6816 (on X frames, there is only one glyph produced for a
6817 composite character.
6818
6819 The behavior implemented below means, for continuation lines,
6820 that as many spaces of a TAB as fit on the current line are
6821 displayed there. For terminal frames, as many glyphs of a
6822 multi-glyph character are displayed in the current line, too.
6823 This is what the old redisplay code did, and we keep it that
6824 way. Under X, the whole shape of a complex character must
6825 fit on the line or it will be completely displayed in the
6826 next line.
6827
6828 Note that both for tabs and padding glyphs, all glyphs have
6829 the same width. */
6830 if (it->nglyphs)
6831 {
6832 /* More than one glyph or glyph doesn't fit on line. All
6833 glyphs have the same width. */
6834 int single_glyph_width = it->pixel_width / it->nglyphs;
6835 int new_x;
6836 int x_before_this_char = x;
6837 int hpos_before_this_char = it->hpos;
6838
6839 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6840 {
6841 new_x = x + single_glyph_width;
6842
6843 /* We want to leave anything reaching TO_X to the caller. */
6844 if ((op & MOVE_TO_X) && new_x > to_x)
6845 {
6846 if (BUFFER_POS_REACHED_P ())
6847 {
6848 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6849 goto buffer_pos_reached;
6850 if (atpos_it.sp < 0)
6851 {
6852 atpos_it = *it;
6853 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6854 }
6855 }
6856 else
6857 {
6858 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6859 {
6860 it->current_x = x;
6861 result = MOVE_X_REACHED;
6862 break;
6863 }
6864 if (atx_it.sp < 0)
6865 {
6866 atx_it = *it;
6867 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6868 }
6869 }
6870 }
6871
6872 if (/* Lines are continued. */
6873 it->line_wrap != TRUNCATE
6874 && (/* And glyph doesn't fit on the line. */
6875 new_x > it->last_visible_x
6876 /* Or it fits exactly and we're on a window
6877 system frame. */
6878 || (new_x == it->last_visible_x
6879 && FRAME_WINDOW_P (it->f))))
6880 {
6881 if (/* IT->hpos == 0 means the very first glyph
6882 doesn't fit on the line, e.g. a wide image. */
6883 it->hpos == 0
6884 || (new_x == it->last_visible_x
6885 && FRAME_WINDOW_P (it->f)))
6886 {
6887 ++it->hpos;
6888 it->current_x = new_x;
6889
6890 /* The character's last glyph just barely fits
6891 in this row. */
6892 if (i == it->nglyphs - 1)
6893 {
6894 /* If this is the destination position,
6895 return a position *before* it in this row,
6896 now that we know it fits in this row. */
6897 if (BUFFER_POS_REACHED_P ())
6898 {
6899 if (it->line_wrap != WORD_WRAP
6900 || wrap_it.sp < 0)
6901 {
6902 it->hpos = hpos_before_this_char;
6903 it->current_x = x_before_this_char;
6904 result = MOVE_POS_MATCH_OR_ZV;
6905 break;
6906 }
6907 if (it->line_wrap == WORD_WRAP
6908 && atpos_it.sp < 0)
6909 {
6910 atpos_it = *it;
6911 atpos_it.current_x = x_before_this_char;
6912 atpos_it.hpos = hpos_before_this_char;
6913 }
6914 }
6915
6916 set_iterator_to_next (it, 1);
6917 #ifdef HAVE_WINDOW_SYSTEM
6918 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6919 {
6920 if (!get_next_display_element (it))
6921 {
6922 result = MOVE_POS_MATCH_OR_ZV;
6923 break;
6924 }
6925 if (BUFFER_POS_REACHED_P ())
6926 {
6927 if (ITERATOR_AT_END_OF_LINE_P (it))
6928 result = MOVE_POS_MATCH_OR_ZV;
6929 else
6930 result = MOVE_LINE_CONTINUED;
6931 break;
6932 }
6933 if (ITERATOR_AT_END_OF_LINE_P (it))
6934 {
6935 result = MOVE_NEWLINE_OR_CR;
6936 break;
6937 }
6938 }
6939 #endif /* HAVE_WINDOW_SYSTEM */
6940 }
6941 }
6942 else
6943 IT_RESET_X_ASCENT_DESCENT (it);
6944
6945 if (wrap_it.sp >= 0)
6946 {
6947 *it = wrap_it;
6948 atpos_it.sp = -1;
6949 atx_it.sp = -1;
6950 }
6951
6952 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6953 IT_CHARPOS (*it)));
6954 result = MOVE_LINE_CONTINUED;
6955 break;
6956 }
6957
6958 if (BUFFER_POS_REACHED_P ())
6959 {
6960 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6961 goto buffer_pos_reached;
6962 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6963 {
6964 atpos_it = *it;
6965 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6966 }
6967 }
6968
6969 if (new_x > it->first_visible_x)
6970 {
6971 /* Glyph is visible. Increment number of glyphs that
6972 would be displayed. */
6973 ++it->hpos;
6974 }
6975 }
6976
6977 if (result != MOVE_UNDEFINED)
6978 break;
6979 }
6980 else if (BUFFER_POS_REACHED_P ())
6981 {
6982 buffer_pos_reached:
6983 IT_RESET_X_ASCENT_DESCENT (it);
6984 result = MOVE_POS_MATCH_OR_ZV;
6985 break;
6986 }
6987 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6988 {
6989 /* Stop when TO_X specified and reached. This check is
6990 necessary here because of lines consisting of a line end,
6991 only. The line end will not produce any glyphs and we
6992 would never get MOVE_X_REACHED. */
6993 xassert (it->nglyphs == 0);
6994 result = MOVE_X_REACHED;
6995 break;
6996 }
6997
6998 /* Is this a line end? If yes, we're done. */
6999 if (ITERATOR_AT_END_OF_LINE_P (it))
7000 {
7001 result = MOVE_NEWLINE_OR_CR;
7002 break;
7003 }
7004
7005 /* The current display element has been consumed. Advance
7006 to the next. */
7007 set_iterator_to_next (it, 1);
7008
7009 /* Stop if lines are truncated and IT's current x-position is
7010 past the right edge of the window now. */
7011 if (it->line_wrap == TRUNCATE
7012 && it->current_x >= it->last_visible_x)
7013 {
7014 #ifdef HAVE_WINDOW_SYSTEM
7015 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7016 {
7017 if (!get_next_display_element (it)
7018 || BUFFER_POS_REACHED_P ())
7019 {
7020 result = MOVE_POS_MATCH_OR_ZV;
7021 break;
7022 }
7023 if (ITERATOR_AT_END_OF_LINE_P (it))
7024 {
7025 result = MOVE_NEWLINE_OR_CR;
7026 break;
7027 }
7028 }
7029 #endif /* HAVE_WINDOW_SYSTEM */
7030 result = MOVE_LINE_TRUNCATED;
7031 break;
7032 }
7033 #undef IT_RESET_X_ASCENT_DESCENT
7034 }
7035
7036 #undef BUFFER_POS_REACHED_P
7037
7038 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7039 restore the saved iterator. */
7040 if (atpos_it.sp >= 0)
7041 *it = atpos_it;
7042 else if (atx_it.sp >= 0)
7043 *it = atx_it;
7044
7045 done:
7046
7047 /* Restore the iterator settings altered at the beginning of this
7048 function. */
7049 it->glyph_row = saved_glyph_row;
7050 return result;
7051 }
7052
7053 /* For external use. */
7054 void
7055 move_it_in_display_line (struct it *it,
7056 EMACS_INT to_charpos, int to_x,
7057 enum move_operation_enum op)
7058 {
7059 if (it->line_wrap == WORD_WRAP
7060 && (op & MOVE_TO_X))
7061 {
7062 struct it save_it = *it;
7063 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7064 /* When word-wrap is on, TO_X may lie past the end
7065 of a wrapped line. Then it->current is the
7066 character on the next line, so backtrack to the
7067 space before the wrap point. */
7068 if (skip == MOVE_LINE_CONTINUED)
7069 {
7070 int prev_x = max (it->current_x - 1, 0);
7071 *it = save_it;
7072 move_it_in_display_line_to
7073 (it, -1, prev_x, MOVE_TO_X);
7074 }
7075 }
7076 else
7077 move_it_in_display_line_to (it, to_charpos, to_x, op);
7078 }
7079
7080
7081 /* Move IT forward until it satisfies one or more of the criteria in
7082 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7083
7084 OP is a bit-mask that specifies where to stop, and in particular,
7085 which of those four position arguments makes a difference. See the
7086 description of enum move_operation_enum.
7087
7088 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7089 screen line, this function will set IT to the next position >
7090 TO_CHARPOS. */
7091
7092 void
7093 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7094 struct it *it;
7095 int to_charpos, to_x, to_y, to_vpos;
7096 int op;
7097 {
7098 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7099 int line_height;
7100 int reached = 0;
7101
7102 for (;;)
7103 {
7104 if (op & MOVE_TO_VPOS)
7105 {
7106 /* If no TO_CHARPOS and no TO_X specified, stop at the
7107 start of the line TO_VPOS. */
7108 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7109 {
7110 if (it->vpos == to_vpos)
7111 {
7112 reached = 1;
7113 break;
7114 }
7115 else
7116 skip = move_it_in_display_line_to (it, -1, -1, 0);
7117 }
7118 else
7119 {
7120 /* TO_VPOS >= 0 means stop at TO_X in the line at
7121 TO_VPOS, or at TO_POS, whichever comes first. */
7122 if (it->vpos == to_vpos)
7123 {
7124 reached = 2;
7125 break;
7126 }
7127
7128 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7129
7130 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7131 {
7132 reached = 3;
7133 break;
7134 }
7135 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7136 {
7137 /* We have reached TO_X but not in the line we want. */
7138 skip = move_it_in_display_line_to (it, to_charpos,
7139 -1, MOVE_TO_POS);
7140 if (skip == MOVE_POS_MATCH_OR_ZV)
7141 {
7142 reached = 4;
7143 break;
7144 }
7145 }
7146 }
7147 }
7148 else if (op & MOVE_TO_Y)
7149 {
7150 struct it it_backup;
7151
7152 if (it->line_wrap == WORD_WRAP)
7153 it_backup = *it;
7154
7155 /* TO_Y specified means stop at TO_X in the line containing
7156 TO_Y---or at TO_CHARPOS if this is reached first. The
7157 problem is that we can't really tell whether the line
7158 contains TO_Y before we have completely scanned it, and
7159 this may skip past TO_X. What we do is to first scan to
7160 TO_X.
7161
7162 If TO_X is not specified, use a TO_X of zero. The reason
7163 is to make the outcome of this function more predictable.
7164 If we didn't use TO_X == 0, we would stop at the end of
7165 the line which is probably not what a caller would expect
7166 to happen. */
7167 skip = move_it_in_display_line_to
7168 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7169 (MOVE_TO_X | (op & MOVE_TO_POS)));
7170
7171 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7172 if (skip == MOVE_POS_MATCH_OR_ZV)
7173 reached = 5;
7174 else if (skip == MOVE_X_REACHED)
7175 {
7176 /* If TO_X was reached, we want to know whether TO_Y is
7177 in the line. We know this is the case if the already
7178 scanned glyphs make the line tall enough. Otherwise,
7179 we must check by scanning the rest of the line. */
7180 line_height = it->max_ascent + it->max_descent;
7181 if (to_y >= it->current_y
7182 && to_y < it->current_y + line_height)
7183 {
7184 reached = 6;
7185 break;
7186 }
7187 it_backup = *it;
7188 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7189 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7190 op & MOVE_TO_POS);
7191 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7192 line_height = it->max_ascent + it->max_descent;
7193 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7194
7195 if (to_y >= it->current_y
7196 && to_y < it->current_y + line_height)
7197 {
7198 /* If TO_Y is in this line and TO_X was reached
7199 above, we scanned too far. We have to restore
7200 IT's settings to the ones before skipping. */
7201 *it = it_backup;
7202 reached = 6;
7203 }
7204 else
7205 {
7206 skip = skip2;
7207 if (skip == MOVE_POS_MATCH_OR_ZV)
7208 reached = 7;
7209 }
7210 }
7211 else
7212 {
7213 /* Check whether TO_Y is in this line. */
7214 line_height = it->max_ascent + it->max_descent;
7215 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7216
7217 if (to_y >= it->current_y
7218 && to_y < it->current_y + line_height)
7219 {
7220 /* When word-wrap is on, TO_X may lie past the end
7221 of a wrapped line. Then it->current is the
7222 character on the next line, so backtrack to the
7223 space before the wrap point. */
7224 if (skip == MOVE_LINE_CONTINUED
7225 && it->line_wrap == WORD_WRAP)
7226 {
7227 int prev_x = max (it->current_x - 1, 0);
7228 *it = it_backup;
7229 skip = move_it_in_display_line_to
7230 (it, -1, prev_x, MOVE_TO_X);
7231 }
7232 reached = 6;
7233 }
7234 }
7235
7236 if (reached)
7237 break;
7238 }
7239 else if (BUFFERP (it->object)
7240 && (it->method == GET_FROM_BUFFER
7241 || it->method == GET_FROM_STRETCH)
7242 && IT_CHARPOS (*it) >= to_charpos)
7243 skip = MOVE_POS_MATCH_OR_ZV;
7244 else
7245 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7246
7247 switch (skip)
7248 {
7249 case MOVE_POS_MATCH_OR_ZV:
7250 reached = 8;
7251 goto out;
7252
7253 case MOVE_NEWLINE_OR_CR:
7254 set_iterator_to_next (it, 1);
7255 it->continuation_lines_width = 0;
7256 break;
7257
7258 case MOVE_LINE_TRUNCATED:
7259 it->continuation_lines_width = 0;
7260 reseat_at_next_visible_line_start (it, 0);
7261 if ((op & MOVE_TO_POS) != 0
7262 && IT_CHARPOS (*it) > to_charpos)
7263 {
7264 reached = 9;
7265 goto out;
7266 }
7267 break;
7268
7269 case MOVE_LINE_CONTINUED:
7270 /* For continued lines ending in a tab, some of the glyphs
7271 associated with the tab are displayed on the current
7272 line. Since it->current_x does not include these glyphs,
7273 we use it->last_visible_x instead. */
7274 if (it->c == '\t')
7275 {
7276 it->continuation_lines_width += it->last_visible_x;
7277 /* When moving by vpos, ensure that the iterator really
7278 advances to the next line (bug#847, bug#969). Fixme:
7279 do we need to do this in other circumstances? */
7280 if (it->current_x != it->last_visible_x
7281 && (op & MOVE_TO_VPOS)
7282 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7283 set_iterator_to_next (it, 0);
7284 }
7285 else
7286 it->continuation_lines_width += it->current_x;
7287 break;
7288
7289 default:
7290 abort ();
7291 }
7292
7293 /* Reset/increment for the next run. */
7294 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7295 it->current_x = it->hpos = 0;
7296 it->current_y += it->max_ascent + it->max_descent;
7297 ++it->vpos;
7298 last_height = it->max_ascent + it->max_descent;
7299 last_max_ascent = it->max_ascent;
7300 it->max_ascent = it->max_descent = 0;
7301 }
7302
7303 out:
7304
7305 /* On text terminals, we may stop at the end of a line in the middle
7306 of a multi-character glyph. If the glyph itself is continued,
7307 i.e. it is actually displayed on the next line, don't treat this
7308 stopping point as valid; move to the next line instead (unless
7309 that brings us offscreen). */
7310 if (!FRAME_WINDOW_P (it->f)
7311 && op & MOVE_TO_POS
7312 && IT_CHARPOS (*it) == to_charpos
7313 && it->what == IT_CHARACTER
7314 && it->nglyphs > 1
7315 && it->line_wrap == WINDOW_WRAP
7316 && it->current_x == it->last_visible_x - 1
7317 && it->c != '\n'
7318 && it->c != '\t'
7319 && it->vpos < XFASTINT (it->w->window_end_vpos))
7320 {
7321 it->continuation_lines_width += it->current_x;
7322 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7323 it->current_y += it->max_ascent + it->max_descent;
7324 ++it->vpos;
7325 last_height = it->max_ascent + it->max_descent;
7326 last_max_ascent = it->max_ascent;
7327 }
7328
7329 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7330 }
7331
7332
7333 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7334
7335 If DY > 0, move IT backward at least that many pixels. DY = 0
7336 means move IT backward to the preceding line start or BEGV. This
7337 function may move over more than DY pixels if IT->current_y - DY
7338 ends up in the middle of a line; in this case IT->current_y will be
7339 set to the top of the line moved to. */
7340
7341 void
7342 move_it_vertically_backward (it, dy)
7343 struct it *it;
7344 int dy;
7345 {
7346 int nlines, h;
7347 struct it it2, it3;
7348 int start_pos;
7349
7350 move_further_back:
7351 xassert (dy >= 0);
7352
7353 start_pos = IT_CHARPOS (*it);
7354
7355 /* Estimate how many newlines we must move back. */
7356 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7357
7358 /* Set the iterator's position that many lines back. */
7359 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7360 back_to_previous_visible_line_start (it);
7361
7362 /* Reseat the iterator here. When moving backward, we don't want
7363 reseat to skip forward over invisible text, set up the iterator
7364 to deliver from overlay strings at the new position etc. So,
7365 use reseat_1 here. */
7366 reseat_1 (it, it->current.pos, 1);
7367
7368 /* We are now surely at a line start. */
7369 it->current_x = it->hpos = 0;
7370 it->continuation_lines_width = 0;
7371
7372 /* Move forward and see what y-distance we moved. First move to the
7373 start of the next line so that we get its height. We need this
7374 height to be able to tell whether we reached the specified
7375 y-distance. */
7376 it2 = *it;
7377 it2.max_ascent = it2.max_descent = 0;
7378 do
7379 {
7380 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7381 MOVE_TO_POS | MOVE_TO_VPOS);
7382 }
7383 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7384 xassert (IT_CHARPOS (*it) >= BEGV);
7385 it3 = it2;
7386
7387 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7388 xassert (IT_CHARPOS (*it) >= BEGV);
7389 /* H is the actual vertical distance from the position in *IT
7390 and the starting position. */
7391 h = it2.current_y - it->current_y;
7392 /* NLINES is the distance in number of lines. */
7393 nlines = it2.vpos - it->vpos;
7394
7395 /* Correct IT's y and vpos position
7396 so that they are relative to the starting point. */
7397 it->vpos -= nlines;
7398 it->current_y -= h;
7399
7400 if (dy == 0)
7401 {
7402 /* DY == 0 means move to the start of the screen line. The
7403 value of nlines is > 0 if continuation lines were involved. */
7404 if (nlines > 0)
7405 move_it_by_lines (it, nlines, 1);
7406 #if 0
7407 /* I think this assert is bogus if buffer contains
7408 invisible text or images. KFS. */
7409 xassert (IT_CHARPOS (*it) <= start_pos);
7410 #endif
7411 }
7412 else
7413 {
7414 /* The y-position we try to reach, relative to *IT.
7415 Note that H has been subtracted in front of the if-statement. */
7416 int target_y = it->current_y + h - dy;
7417 int y0 = it3.current_y;
7418 int y1 = line_bottom_y (&it3);
7419 int line_height = y1 - y0;
7420
7421 /* If we did not reach target_y, try to move further backward if
7422 we can. If we moved too far backward, try to move forward. */
7423 if (target_y < it->current_y
7424 /* This is heuristic. In a window that's 3 lines high, with
7425 a line height of 13 pixels each, recentering with point
7426 on the bottom line will try to move -39/2 = 19 pixels
7427 backward. Try to avoid moving into the first line. */
7428 && (it->current_y - target_y
7429 > min (window_box_height (it->w), line_height * 2 / 3))
7430 && IT_CHARPOS (*it) > BEGV)
7431 {
7432 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7433 target_y - it->current_y));
7434 dy = it->current_y - target_y;
7435 goto move_further_back;
7436 }
7437 else if (target_y >= it->current_y + line_height
7438 && IT_CHARPOS (*it) < ZV)
7439 {
7440 /* Should move forward by at least one line, maybe more.
7441
7442 Note: Calling move_it_by_lines can be expensive on
7443 terminal frames, where compute_motion is used (via
7444 vmotion) to do the job, when there are very long lines
7445 and truncate-lines is nil. That's the reason for
7446 treating terminal frames specially here. */
7447
7448 if (!FRAME_WINDOW_P (it->f))
7449 move_it_vertically (it, target_y - (it->current_y + line_height));
7450 else
7451 {
7452 do
7453 {
7454 move_it_by_lines (it, 1, 1);
7455 }
7456 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7457 }
7458
7459 #if 0
7460 /* I think this assert is bogus if buffer contains
7461 invisible text or images. KFS. */
7462 xassert (IT_CHARPOS (*it) >= BEGV);
7463 #endif
7464 }
7465 }
7466 }
7467
7468
7469 /* Move IT by a specified amount of pixel lines DY. DY negative means
7470 move backwards. DY = 0 means move to start of screen line. At the
7471 end, IT will be on the start of a screen line. */
7472
7473 void
7474 move_it_vertically (it, dy)
7475 struct it *it;
7476 int dy;
7477 {
7478 if (dy <= 0)
7479 move_it_vertically_backward (it, -dy);
7480 else
7481 {
7482 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7483 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7484 MOVE_TO_POS | MOVE_TO_Y);
7485 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7486
7487 /* If buffer ends in ZV without a newline, move to the start of
7488 the line to satisfy the post-condition. */
7489 if (IT_CHARPOS (*it) == ZV
7490 && ZV > BEGV
7491 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7492 move_it_by_lines (it, 0, 0);
7493 }
7494 }
7495
7496
7497 /* Move iterator IT past the end of the text line it is in. */
7498
7499 void
7500 move_it_past_eol (it)
7501 struct it *it;
7502 {
7503 enum move_it_result rc;
7504
7505 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7506 if (rc == MOVE_NEWLINE_OR_CR)
7507 set_iterator_to_next (it, 0);
7508 }
7509
7510
7511 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7512 negative means move up. DVPOS == 0 means move to the start of the
7513 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7514 NEED_Y_P is zero, IT->current_y will be left unchanged.
7515
7516 Further optimization ideas: If we would know that IT->f doesn't use
7517 a face with proportional font, we could be faster for
7518 truncate-lines nil. */
7519
7520 void
7521 move_it_by_lines (it, dvpos, need_y_p)
7522 struct it *it;
7523 int dvpos, need_y_p;
7524 {
7525 struct position pos;
7526
7527 /* The commented-out optimization uses vmotion on terminals. This
7528 gives bad results, because elements like it->what, on which
7529 callers such as pos_visible_p rely, aren't updated. */
7530 /* if (!FRAME_WINDOW_P (it->f))
7531 {
7532 struct text_pos textpos;
7533
7534 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7535 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7536 reseat (it, textpos, 1);
7537 it->vpos += pos.vpos;
7538 it->current_y += pos.vpos;
7539 }
7540 else */
7541
7542 if (dvpos == 0)
7543 {
7544 /* DVPOS == 0 means move to the start of the screen line. */
7545 move_it_vertically_backward (it, 0);
7546 xassert (it->current_x == 0 && it->hpos == 0);
7547 /* Let next call to line_bottom_y calculate real line height */
7548 last_height = 0;
7549 }
7550 else if (dvpos > 0)
7551 {
7552 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7553 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7554 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7555 }
7556 else
7557 {
7558 struct it it2;
7559 int start_charpos, i;
7560
7561 /* Start at the beginning of the screen line containing IT's
7562 position. This may actually move vertically backwards,
7563 in case of overlays, so adjust dvpos accordingly. */
7564 dvpos += it->vpos;
7565 move_it_vertically_backward (it, 0);
7566 dvpos -= it->vpos;
7567
7568 /* Go back -DVPOS visible lines and reseat the iterator there. */
7569 start_charpos = IT_CHARPOS (*it);
7570 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7571 back_to_previous_visible_line_start (it);
7572 reseat (it, it->current.pos, 1);
7573
7574 /* Move further back if we end up in a string or an image. */
7575 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7576 {
7577 /* First try to move to start of display line. */
7578 dvpos += it->vpos;
7579 move_it_vertically_backward (it, 0);
7580 dvpos -= it->vpos;
7581 if (IT_POS_VALID_AFTER_MOVE_P (it))
7582 break;
7583 /* If start of line is still in string or image,
7584 move further back. */
7585 back_to_previous_visible_line_start (it);
7586 reseat (it, it->current.pos, 1);
7587 dvpos--;
7588 }
7589
7590 it->current_x = it->hpos = 0;
7591
7592 /* Above call may have moved too far if continuation lines
7593 are involved. Scan forward and see if it did. */
7594 it2 = *it;
7595 it2.vpos = it2.current_y = 0;
7596 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7597 it->vpos -= it2.vpos;
7598 it->current_y -= it2.current_y;
7599 it->current_x = it->hpos = 0;
7600
7601 /* If we moved too far back, move IT some lines forward. */
7602 if (it2.vpos > -dvpos)
7603 {
7604 int delta = it2.vpos + dvpos;
7605 it2 = *it;
7606 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7607 /* Move back again if we got too far ahead. */
7608 if (IT_CHARPOS (*it) >= start_charpos)
7609 *it = it2;
7610 }
7611 }
7612 }
7613
7614 /* Return 1 if IT points into the middle of a display vector. */
7615
7616 int
7617 in_display_vector_p (it)
7618 struct it *it;
7619 {
7620 return (it->method == GET_FROM_DISPLAY_VECTOR
7621 && it->current.dpvec_index > 0
7622 && it->dpvec + it->current.dpvec_index != it->dpend);
7623 }
7624
7625 \f
7626 /***********************************************************************
7627 Messages
7628 ***********************************************************************/
7629
7630
7631 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7632 to *Messages*. */
7633
7634 void
7635 add_to_log (format, arg1, arg2)
7636 char *format;
7637 Lisp_Object arg1, arg2;
7638 {
7639 Lisp_Object args[3];
7640 Lisp_Object msg, fmt;
7641 char *buffer;
7642 int len;
7643 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7644 USE_SAFE_ALLOCA;
7645
7646 /* Do nothing if called asynchronously. Inserting text into
7647 a buffer may call after-change-functions and alike and
7648 that would means running Lisp asynchronously. */
7649 if (handling_signal)
7650 return;
7651
7652 fmt = msg = Qnil;
7653 GCPRO4 (fmt, msg, arg1, arg2);
7654
7655 args[0] = fmt = build_string (format);
7656 args[1] = arg1;
7657 args[2] = arg2;
7658 msg = Fformat (3, args);
7659
7660 len = SBYTES (msg) + 1;
7661 SAFE_ALLOCA (buffer, char *, len);
7662 bcopy (SDATA (msg), buffer, len);
7663
7664 message_dolog (buffer, len - 1, 1, 0);
7665 SAFE_FREE ();
7666
7667 UNGCPRO;
7668 }
7669
7670
7671 /* Output a newline in the *Messages* buffer if "needs" one. */
7672
7673 void
7674 message_log_maybe_newline ()
7675 {
7676 if (message_log_need_newline)
7677 message_dolog ("", 0, 1, 0);
7678 }
7679
7680
7681 /* Add a string M of length NBYTES to the message log, optionally
7682 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7683 nonzero, means interpret the contents of M as multibyte. This
7684 function calls low-level routines in order to bypass text property
7685 hooks, etc. which might not be safe to run.
7686
7687 This may GC (insert may run before/after change hooks),
7688 so the buffer M must NOT point to a Lisp string. */
7689
7690 void
7691 message_dolog (m, nbytes, nlflag, multibyte)
7692 const char *m;
7693 int nbytes, nlflag, multibyte;
7694 {
7695 if (!NILP (Vmemory_full))
7696 return;
7697
7698 if (!NILP (Vmessage_log_max))
7699 {
7700 struct buffer *oldbuf;
7701 Lisp_Object oldpoint, oldbegv, oldzv;
7702 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7703 int point_at_end = 0;
7704 int zv_at_end = 0;
7705 Lisp_Object old_deactivate_mark, tem;
7706 struct gcpro gcpro1;
7707
7708 old_deactivate_mark = Vdeactivate_mark;
7709 oldbuf = current_buffer;
7710 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7711 current_buffer->undo_list = Qt;
7712
7713 oldpoint = message_dolog_marker1;
7714 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7715 oldbegv = message_dolog_marker2;
7716 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7717 oldzv = message_dolog_marker3;
7718 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7719 GCPRO1 (old_deactivate_mark);
7720
7721 if (PT == Z)
7722 point_at_end = 1;
7723 if (ZV == Z)
7724 zv_at_end = 1;
7725
7726 BEGV = BEG;
7727 BEGV_BYTE = BEG_BYTE;
7728 ZV = Z;
7729 ZV_BYTE = Z_BYTE;
7730 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7731
7732 /* Insert the string--maybe converting multibyte to single byte
7733 or vice versa, so that all the text fits the buffer. */
7734 if (multibyte
7735 && NILP (current_buffer->enable_multibyte_characters))
7736 {
7737 int i, c, char_bytes;
7738 unsigned char work[1];
7739
7740 /* Convert a multibyte string to single-byte
7741 for the *Message* buffer. */
7742 for (i = 0; i < nbytes; i += char_bytes)
7743 {
7744 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7745 work[0] = (ASCII_CHAR_P (c)
7746 ? c
7747 : multibyte_char_to_unibyte (c, Qnil));
7748 insert_1_both (work, 1, 1, 1, 0, 0);
7749 }
7750 }
7751 else if (! multibyte
7752 && ! NILP (current_buffer->enable_multibyte_characters))
7753 {
7754 int i, c, char_bytes;
7755 unsigned char *msg = (unsigned char *) m;
7756 unsigned char str[MAX_MULTIBYTE_LENGTH];
7757 /* Convert a single-byte string to multibyte
7758 for the *Message* buffer. */
7759 for (i = 0; i < nbytes; i++)
7760 {
7761 c = msg[i];
7762 c = unibyte_char_to_multibyte (c);
7763 char_bytes = CHAR_STRING (c, str);
7764 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7765 }
7766 }
7767 else if (nbytes)
7768 insert_1 (m, nbytes, 1, 0, 0);
7769
7770 if (nlflag)
7771 {
7772 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7773 insert_1 ("\n", 1, 1, 0, 0);
7774
7775 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7776 this_bol = PT;
7777 this_bol_byte = PT_BYTE;
7778
7779 /* See if this line duplicates the previous one.
7780 If so, combine duplicates. */
7781 if (this_bol > BEG)
7782 {
7783 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7784 prev_bol = PT;
7785 prev_bol_byte = PT_BYTE;
7786
7787 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7788 this_bol, this_bol_byte);
7789 if (dup)
7790 {
7791 del_range_both (prev_bol, prev_bol_byte,
7792 this_bol, this_bol_byte, 0);
7793 if (dup > 1)
7794 {
7795 char dupstr[40];
7796 int duplen;
7797
7798 /* If you change this format, don't forget to also
7799 change message_log_check_duplicate. */
7800 sprintf (dupstr, " [%d times]", dup);
7801 duplen = strlen (dupstr);
7802 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7803 insert_1 (dupstr, duplen, 1, 0, 1);
7804 }
7805 }
7806 }
7807
7808 /* If we have more than the desired maximum number of lines
7809 in the *Messages* buffer now, delete the oldest ones.
7810 This is safe because we don't have undo in this buffer. */
7811
7812 if (NATNUMP (Vmessage_log_max))
7813 {
7814 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7815 -XFASTINT (Vmessage_log_max) - 1, 0);
7816 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7817 }
7818 }
7819 BEGV = XMARKER (oldbegv)->charpos;
7820 BEGV_BYTE = marker_byte_position (oldbegv);
7821
7822 if (zv_at_end)
7823 {
7824 ZV = Z;
7825 ZV_BYTE = Z_BYTE;
7826 }
7827 else
7828 {
7829 ZV = XMARKER (oldzv)->charpos;
7830 ZV_BYTE = marker_byte_position (oldzv);
7831 }
7832
7833 if (point_at_end)
7834 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7835 else
7836 /* We can't do Fgoto_char (oldpoint) because it will run some
7837 Lisp code. */
7838 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7839 XMARKER (oldpoint)->bytepos);
7840
7841 UNGCPRO;
7842 unchain_marker (XMARKER (oldpoint));
7843 unchain_marker (XMARKER (oldbegv));
7844 unchain_marker (XMARKER (oldzv));
7845
7846 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7847 set_buffer_internal (oldbuf);
7848 if (NILP (tem))
7849 windows_or_buffers_changed = old_windows_or_buffers_changed;
7850 message_log_need_newline = !nlflag;
7851 Vdeactivate_mark = old_deactivate_mark;
7852 }
7853 }
7854
7855
7856 /* We are at the end of the buffer after just having inserted a newline.
7857 (Note: We depend on the fact we won't be crossing the gap.)
7858 Check to see if the most recent message looks a lot like the previous one.
7859 Return 0 if different, 1 if the new one should just replace it, or a
7860 value N > 1 if we should also append " [N times]". */
7861
7862 static int
7863 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7864 int prev_bol, this_bol;
7865 int prev_bol_byte, this_bol_byte;
7866 {
7867 int i;
7868 int len = Z_BYTE - 1 - this_bol_byte;
7869 int seen_dots = 0;
7870 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7871 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7872
7873 for (i = 0; i < len; i++)
7874 {
7875 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7876 seen_dots = 1;
7877 if (p1[i] != p2[i])
7878 return seen_dots;
7879 }
7880 p1 += len;
7881 if (*p1 == '\n')
7882 return 2;
7883 if (*p1++ == ' ' && *p1++ == '[')
7884 {
7885 int n = 0;
7886 while (*p1 >= '0' && *p1 <= '9')
7887 n = n * 10 + *p1++ - '0';
7888 if (strncmp (p1, " times]\n", 8) == 0)
7889 return n+1;
7890 }
7891 return 0;
7892 }
7893 \f
7894
7895 /* Display an echo area message M with a specified length of NBYTES
7896 bytes. The string may include null characters. If M is 0, clear
7897 out any existing message, and let the mini-buffer text show
7898 through.
7899
7900 This may GC, so the buffer M must NOT point to a Lisp string. */
7901
7902 void
7903 message2 (m, nbytes, multibyte)
7904 const char *m;
7905 int nbytes;
7906 int multibyte;
7907 {
7908 /* First flush out any partial line written with print. */
7909 message_log_maybe_newline ();
7910 if (m)
7911 message_dolog (m, nbytes, 1, multibyte);
7912 message2_nolog (m, nbytes, multibyte);
7913 }
7914
7915
7916 /* The non-logging counterpart of message2. */
7917
7918 void
7919 message2_nolog (m, nbytes, multibyte)
7920 const char *m;
7921 int nbytes, multibyte;
7922 {
7923 struct frame *sf = SELECTED_FRAME ();
7924 message_enable_multibyte = multibyte;
7925
7926 if (FRAME_INITIAL_P (sf))
7927 {
7928 if (noninteractive_need_newline)
7929 putc ('\n', stderr);
7930 noninteractive_need_newline = 0;
7931 if (m)
7932 fwrite (m, nbytes, 1, stderr);
7933 if (cursor_in_echo_area == 0)
7934 fprintf (stderr, "\n");
7935 fflush (stderr);
7936 }
7937 /* A null message buffer means that the frame hasn't really been
7938 initialized yet. Error messages get reported properly by
7939 cmd_error, so this must be just an informative message; toss it. */
7940 else if (INTERACTIVE
7941 && sf->glyphs_initialized_p
7942 && FRAME_MESSAGE_BUF (sf))
7943 {
7944 Lisp_Object mini_window;
7945 struct frame *f;
7946
7947 /* Get the frame containing the mini-buffer
7948 that the selected frame is using. */
7949 mini_window = FRAME_MINIBUF_WINDOW (sf);
7950 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7951
7952 FRAME_SAMPLE_VISIBILITY (f);
7953 if (FRAME_VISIBLE_P (sf)
7954 && ! FRAME_VISIBLE_P (f))
7955 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7956
7957 if (m)
7958 {
7959 set_message (m, Qnil, nbytes, multibyte);
7960 if (minibuffer_auto_raise)
7961 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7962 }
7963 else
7964 clear_message (1, 1);
7965
7966 do_pending_window_change (0);
7967 echo_area_display (1);
7968 do_pending_window_change (0);
7969 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7970 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7971 }
7972 }
7973
7974
7975 /* Display an echo area message M with a specified length of NBYTES
7976 bytes. The string may include null characters. If M is not a
7977 string, clear out any existing message, and let the mini-buffer
7978 text show through.
7979
7980 This function cancels echoing. */
7981
7982 void
7983 message3 (m, nbytes, multibyte)
7984 Lisp_Object m;
7985 int nbytes;
7986 int multibyte;
7987 {
7988 struct gcpro gcpro1;
7989
7990 GCPRO1 (m);
7991 clear_message (1,1);
7992 cancel_echoing ();
7993
7994 /* First flush out any partial line written with print. */
7995 message_log_maybe_newline ();
7996 if (STRINGP (m))
7997 {
7998 char *buffer;
7999 USE_SAFE_ALLOCA;
8000
8001 SAFE_ALLOCA (buffer, char *, nbytes);
8002 bcopy (SDATA (m), buffer, nbytes);
8003 message_dolog (buffer, nbytes, 1, multibyte);
8004 SAFE_FREE ();
8005 }
8006 message3_nolog (m, nbytes, multibyte);
8007
8008 UNGCPRO;
8009 }
8010
8011
8012 /* The non-logging version of message3.
8013 This does not cancel echoing, because it is used for echoing.
8014 Perhaps we need to make a separate function for echoing
8015 and make this cancel echoing. */
8016
8017 void
8018 message3_nolog (m, nbytes, multibyte)
8019 Lisp_Object m;
8020 int nbytes, multibyte;
8021 {
8022 struct frame *sf = SELECTED_FRAME ();
8023 message_enable_multibyte = multibyte;
8024
8025 if (FRAME_INITIAL_P (sf))
8026 {
8027 if (noninteractive_need_newline)
8028 putc ('\n', stderr);
8029 noninteractive_need_newline = 0;
8030 if (STRINGP (m))
8031 fwrite (SDATA (m), nbytes, 1, stderr);
8032 if (cursor_in_echo_area == 0)
8033 fprintf (stderr, "\n");
8034 fflush (stderr);
8035 }
8036 /* A null message buffer means that the frame hasn't really been
8037 initialized yet. Error messages get reported properly by
8038 cmd_error, so this must be just an informative message; toss it. */
8039 else if (INTERACTIVE
8040 && sf->glyphs_initialized_p
8041 && FRAME_MESSAGE_BUF (sf))
8042 {
8043 Lisp_Object mini_window;
8044 Lisp_Object frame;
8045 struct frame *f;
8046
8047 /* Get the frame containing the mini-buffer
8048 that the selected frame is using. */
8049 mini_window = FRAME_MINIBUF_WINDOW (sf);
8050 frame = XWINDOW (mini_window)->frame;
8051 f = XFRAME (frame);
8052
8053 FRAME_SAMPLE_VISIBILITY (f);
8054 if (FRAME_VISIBLE_P (sf)
8055 && !FRAME_VISIBLE_P (f))
8056 Fmake_frame_visible (frame);
8057
8058 if (STRINGP (m) && SCHARS (m) > 0)
8059 {
8060 set_message (NULL, m, nbytes, multibyte);
8061 if (minibuffer_auto_raise)
8062 Fraise_frame (frame);
8063 /* Assume we are not echoing.
8064 (If we are, echo_now will override this.) */
8065 echo_message_buffer = Qnil;
8066 }
8067 else
8068 clear_message (1, 1);
8069
8070 do_pending_window_change (0);
8071 echo_area_display (1);
8072 do_pending_window_change (0);
8073 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8074 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8075 }
8076 }
8077
8078
8079 /* Display a null-terminated echo area message M. If M is 0, clear
8080 out any existing message, and let the mini-buffer text show through.
8081
8082 The buffer M must continue to exist until after the echo area gets
8083 cleared or some other message gets displayed there. Do not pass
8084 text that is stored in a Lisp string. Do not pass text in a buffer
8085 that was alloca'd. */
8086
8087 void
8088 message1 (m)
8089 char *m;
8090 {
8091 message2 (m, (m ? strlen (m) : 0), 0);
8092 }
8093
8094
8095 /* The non-logging counterpart of message1. */
8096
8097 void
8098 message1_nolog (m)
8099 char *m;
8100 {
8101 message2_nolog (m, (m ? strlen (m) : 0), 0);
8102 }
8103
8104 /* Display a message M which contains a single %s
8105 which gets replaced with STRING. */
8106
8107 void
8108 message_with_string (m, string, log)
8109 char *m;
8110 Lisp_Object string;
8111 int log;
8112 {
8113 CHECK_STRING (string);
8114
8115 if (noninteractive)
8116 {
8117 if (m)
8118 {
8119 if (noninteractive_need_newline)
8120 putc ('\n', stderr);
8121 noninteractive_need_newline = 0;
8122 fprintf (stderr, m, SDATA (string));
8123 if (!cursor_in_echo_area)
8124 fprintf (stderr, "\n");
8125 fflush (stderr);
8126 }
8127 }
8128 else if (INTERACTIVE)
8129 {
8130 /* The frame whose minibuffer we're going to display the message on.
8131 It may be larger than the selected frame, so we need
8132 to use its buffer, not the selected frame's buffer. */
8133 Lisp_Object mini_window;
8134 struct frame *f, *sf = SELECTED_FRAME ();
8135
8136 /* Get the frame containing the minibuffer
8137 that the selected frame is using. */
8138 mini_window = FRAME_MINIBUF_WINDOW (sf);
8139 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8140
8141 /* A null message buffer means that the frame hasn't really been
8142 initialized yet. Error messages get reported properly by
8143 cmd_error, so this must be just an informative message; toss it. */
8144 if (FRAME_MESSAGE_BUF (f))
8145 {
8146 Lisp_Object args[2], message;
8147 struct gcpro gcpro1, gcpro2;
8148
8149 args[0] = build_string (m);
8150 args[1] = message = string;
8151 GCPRO2 (args[0], message);
8152 gcpro1.nvars = 2;
8153
8154 message = Fformat (2, args);
8155
8156 if (log)
8157 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8158 else
8159 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8160
8161 UNGCPRO;
8162
8163 /* Print should start at the beginning of the message
8164 buffer next time. */
8165 message_buf_print = 0;
8166 }
8167 }
8168 }
8169
8170
8171 /* Dump an informative message to the minibuf. If M is 0, clear out
8172 any existing message, and let the mini-buffer text show through. */
8173
8174 /* VARARGS 1 */
8175 void
8176 message (m, a1, a2, a3)
8177 char *m;
8178 EMACS_INT a1, a2, a3;
8179 {
8180 if (noninteractive)
8181 {
8182 if (m)
8183 {
8184 if (noninteractive_need_newline)
8185 putc ('\n', stderr);
8186 noninteractive_need_newline = 0;
8187 fprintf (stderr, m, a1, a2, a3);
8188 if (cursor_in_echo_area == 0)
8189 fprintf (stderr, "\n");
8190 fflush (stderr);
8191 }
8192 }
8193 else if (INTERACTIVE)
8194 {
8195 /* The frame whose mini-buffer we're going to display the message
8196 on. It may be larger than the selected frame, so we need to
8197 use its buffer, not the selected frame's buffer. */
8198 Lisp_Object mini_window;
8199 struct frame *f, *sf = SELECTED_FRAME ();
8200
8201 /* Get the frame containing the mini-buffer
8202 that the selected frame is using. */
8203 mini_window = FRAME_MINIBUF_WINDOW (sf);
8204 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8205
8206 /* A null message buffer means that the frame hasn't really been
8207 initialized yet. Error messages get reported properly by
8208 cmd_error, so this must be just an informative message; toss
8209 it. */
8210 if (FRAME_MESSAGE_BUF (f))
8211 {
8212 if (m)
8213 {
8214 int len;
8215 #ifdef NO_ARG_ARRAY
8216 char *a[3];
8217 a[0] = (char *) a1;
8218 a[1] = (char *) a2;
8219 a[2] = (char *) a3;
8220
8221 len = doprnt (FRAME_MESSAGE_BUF (f),
8222 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8223 #else
8224 len = doprnt (FRAME_MESSAGE_BUF (f),
8225 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8226 (char **) &a1);
8227 #endif /* NO_ARG_ARRAY */
8228
8229 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8230 }
8231 else
8232 message1 (0);
8233
8234 /* Print should start at the beginning of the message
8235 buffer next time. */
8236 message_buf_print = 0;
8237 }
8238 }
8239 }
8240
8241
8242 /* The non-logging version of message. */
8243
8244 void
8245 message_nolog (m, a1, a2, a3)
8246 char *m;
8247 EMACS_INT a1, a2, a3;
8248 {
8249 Lisp_Object old_log_max;
8250 old_log_max = Vmessage_log_max;
8251 Vmessage_log_max = Qnil;
8252 message (m, a1, a2, a3);
8253 Vmessage_log_max = old_log_max;
8254 }
8255
8256
8257 /* Display the current message in the current mini-buffer. This is
8258 only called from error handlers in process.c, and is not time
8259 critical. */
8260
8261 void
8262 update_echo_area ()
8263 {
8264 if (!NILP (echo_area_buffer[0]))
8265 {
8266 Lisp_Object string;
8267 string = Fcurrent_message ();
8268 message3 (string, SBYTES (string),
8269 !NILP (current_buffer->enable_multibyte_characters));
8270 }
8271 }
8272
8273
8274 /* Make sure echo area buffers in `echo_buffers' are live.
8275 If they aren't, make new ones. */
8276
8277 static void
8278 ensure_echo_area_buffers ()
8279 {
8280 int i;
8281
8282 for (i = 0; i < 2; ++i)
8283 if (!BUFFERP (echo_buffer[i])
8284 || NILP (XBUFFER (echo_buffer[i])->name))
8285 {
8286 char name[30];
8287 Lisp_Object old_buffer;
8288 int j;
8289
8290 old_buffer = echo_buffer[i];
8291 sprintf (name, " *Echo Area %d*", i);
8292 echo_buffer[i] = Fget_buffer_create (build_string (name));
8293 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8294 /* to force word wrap in echo area -
8295 it was decided to postpone this*/
8296 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8297
8298 for (j = 0; j < 2; ++j)
8299 if (EQ (old_buffer, echo_area_buffer[j]))
8300 echo_area_buffer[j] = echo_buffer[i];
8301 }
8302 }
8303
8304
8305 /* Call FN with args A1..A4 with either the current or last displayed
8306 echo_area_buffer as current buffer.
8307
8308 WHICH zero means use the current message buffer
8309 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8310 from echo_buffer[] and clear it.
8311
8312 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8313 suitable buffer from echo_buffer[] and clear it.
8314
8315 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8316 that the current message becomes the last displayed one, make
8317 choose a suitable buffer for echo_area_buffer[0], and clear it.
8318
8319 Value is what FN returns. */
8320
8321 static int
8322 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8323 struct window *w;
8324 int which;
8325 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8326 EMACS_INT a1;
8327 Lisp_Object a2;
8328 EMACS_INT a3, a4;
8329 {
8330 Lisp_Object buffer;
8331 int this_one, the_other, clear_buffer_p, rc;
8332 int count = SPECPDL_INDEX ();
8333
8334 /* If buffers aren't live, make new ones. */
8335 ensure_echo_area_buffers ();
8336
8337 clear_buffer_p = 0;
8338
8339 if (which == 0)
8340 this_one = 0, the_other = 1;
8341 else if (which > 0)
8342 this_one = 1, the_other = 0;
8343 else
8344 {
8345 this_one = 0, the_other = 1;
8346 clear_buffer_p = 1;
8347
8348 /* We need a fresh one in case the current echo buffer equals
8349 the one containing the last displayed echo area message. */
8350 if (!NILP (echo_area_buffer[this_one])
8351 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8352 echo_area_buffer[this_one] = Qnil;
8353 }
8354
8355 /* Choose a suitable buffer from echo_buffer[] is we don't
8356 have one. */
8357 if (NILP (echo_area_buffer[this_one]))
8358 {
8359 echo_area_buffer[this_one]
8360 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8361 ? echo_buffer[the_other]
8362 : echo_buffer[this_one]);
8363 clear_buffer_p = 1;
8364 }
8365
8366 buffer = echo_area_buffer[this_one];
8367
8368 /* Don't get confused by reusing the buffer used for echoing
8369 for a different purpose. */
8370 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8371 cancel_echoing ();
8372
8373 record_unwind_protect (unwind_with_echo_area_buffer,
8374 with_echo_area_buffer_unwind_data (w));
8375
8376 /* Make the echo area buffer current. Note that for display
8377 purposes, it is not necessary that the displayed window's buffer
8378 == current_buffer, except for text property lookup. So, let's
8379 only set that buffer temporarily here without doing a full
8380 Fset_window_buffer. We must also change w->pointm, though,
8381 because otherwise an assertions in unshow_buffer fails, and Emacs
8382 aborts. */
8383 set_buffer_internal_1 (XBUFFER (buffer));
8384 if (w)
8385 {
8386 w->buffer = buffer;
8387 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8388 }
8389
8390 current_buffer->undo_list = Qt;
8391 current_buffer->read_only = Qnil;
8392 specbind (Qinhibit_read_only, Qt);
8393 specbind (Qinhibit_modification_hooks, Qt);
8394
8395 if (clear_buffer_p && Z > BEG)
8396 del_range (BEG, Z);
8397
8398 xassert (BEGV >= BEG);
8399 xassert (ZV <= Z && ZV >= BEGV);
8400
8401 rc = fn (a1, a2, a3, a4);
8402
8403 xassert (BEGV >= BEG);
8404 xassert (ZV <= Z && ZV >= BEGV);
8405
8406 unbind_to (count, Qnil);
8407 return rc;
8408 }
8409
8410
8411 /* Save state that should be preserved around the call to the function
8412 FN called in with_echo_area_buffer. */
8413
8414 static Lisp_Object
8415 with_echo_area_buffer_unwind_data (w)
8416 struct window *w;
8417 {
8418 int i = 0;
8419 Lisp_Object vector, tmp;
8420
8421 /* Reduce consing by keeping one vector in
8422 Vwith_echo_area_save_vector. */
8423 vector = Vwith_echo_area_save_vector;
8424 Vwith_echo_area_save_vector = Qnil;
8425
8426 if (NILP (vector))
8427 vector = Fmake_vector (make_number (7), Qnil);
8428
8429 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8430 ASET (vector, i, Vdeactivate_mark); ++i;
8431 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8432
8433 if (w)
8434 {
8435 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8436 ASET (vector, i, w->buffer); ++i;
8437 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8438 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8439 }
8440 else
8441 {
8442 int end = i + 4;
8443 for (; i < end; ++i)
8444 ASET (vector, i, Qnil);
8445 }
8446
8447 xassert (i == ASIZE (vector));
8448 return vector;
8449 }
8450
8451
8452 /* Restore global state from VECTOR which was created by
8453 with_echo_area_buffer_unwind_data. */
8454
8455 static Lisp_Object
8456 unwind_with_echo_area_buffer (vector)
8457 Lisp_Object vector;
8458 {
8459 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8460 Vdeactivate_mark = AREF (vector, 1);
8461 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8462
8463 if (WINDOWP (AREF (vector, 3)))
8464 {
8465 struct window *w;
8466 Lisp_Object buffer, charpos, bytepos;
8467
8468 w = XWINDOW (AREF (vector, 3));
8469 buffer = AREF (vector, 4);
8470 charpos = AREF (vector, 5);
8471 bytepos = AREF (vector, 6);
8472
8473 w->buffer = buffer;
8474 set_marker_both (w->pointm, buffer,
8475 XFASTINT (charpos), XFASTINT (bytepos));
8476 }
8477
8478 Vwith_echo_area_save_vector = vector;
8479 return Qnil;
8480 }
8481
8482
8483 /* Set up the echo area for use by print functions. MULTIBYTE_P
8484 non-zero means we will print multibyte. */
8485
8486 void
8487 setup_echo_area_for_printing (multibyte_p)
8488 int multibyte_p;
8489 {
8490 /* If we can't find an echo area any more, exit. */
8491 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8492 Fkill_emacs (Qnil);
8493
8494 ensure_echo_area_buffers ();
8495
8496 if (!message_buf_print)
8497 {
8498 /* A message has been output since the last time we printed.
8499 Choose a fresh echo area buffer. */
8500 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8501 echo_area_buffer[0] = echo_buffer[1];
8502 else
8503 echo_area_buffer[0] = echo_buffer[0];
8504
8505 /* Switch to that buffer and clear it. */
8506 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8507 current_buffer->truncate_lines = Qnil;
8508
8509 if (Z > BEG)
8510 {
8511 int count = SPECPDL_INDEX ();
8512 specbind (Qinhibit_read_only, Qt);
8513 /* Note that undo recording is always disabled. */
8514 del_range (BEG, Z);
8515 unbind_to (count, Qnil);
8516 }
8517 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8518
8519 /* Set up the buffer for the multibyteness we need. */
8520 if (multibyte_p
8521 != !NILP (current_buffer->enable_multibyte_characters))
8522 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8523
8524 /* Raise the frame containing the echo area. */
8525 if (minibuffer_auto_raise)
8526 {
8527 struct frame *sf = SELECTED_FRAME ();
8528 Lisp_Object mini_window;
8529 mini_window = FRAME_MINIBUF_WINDOW (sf);
8530 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8531 }
8532
8533 message_log_maybe_newline ();
8534 message_buf_print = 1;
8535 }
8536 else
8537 {
8538 if (NILP (echo_area_buffer[0]))
8539 {
8540 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8541 echo_area_buffer[0] = echo_buffer[1];
8542 else
8543 echo_area_buffer[0] = echo_buffer[0];
8544 }
8545
8546 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8547 {
8548 /* Someone switched buffers between print requests. */
8549 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8550 current_buffer->truncate_lines = Qnil;
8551 }
8552 }
8553 }
8554
8555
8556 /* Display an echo area message in window W. Value is non-zero if W's
8557 height is changed. If display_last_displayed_message_p is
8558 non-zero, display the message that was last displayed, otherwise
8559 display the current message. */
8560
8561 static int
8562 display_echo_area (w)
8563 struct window *w;
8564 {
8565 int i, no_message_p, window_height_changed_p, count;
8566
8567 /* Temporarily disable garbage collections while displaying the echo
8568 area. This is done because a GC can print a message itself.
8569 That message would modify the echo area buffer's contents while a
8570 redisplay of the buffer is going on, and seriously confuse
8571 redisplay. */
8572 count = inhibit_garbage_collection ();
8573
8574 /* If there is no message, we must call display_echo_area_1
8575 nevertheless because it resizes the window. But we will have to
8576 reset the echo_area_buffer in question to nil at the end because
8577 with_echo_area_buffer will sets it to an empty buffer. */
8578 i = display_last_displayed_message_p ? 1 : 0;
8579 no_message_p = NILP (echo_area_buffer[i]);
8580
8581 window_height_changed_p
8582 = with_echo_area_buffer (w, display_last_displayed_message_p,
8583 display_echo_area_1,
8584 (EMACS_INT) w, Qnil, 0, 0);
8585
8586 if (no_message_p)
8587 echo_area_buffer[i] = Qnil;
8588
8589 unbind_to (count, Qnil);
8590 return window_height_changed_p;
8591 }
8592
8593
8594 /* Helper for display_echo_area. Display the current buffer which
8595 contains the current echo area message in window W, a mini-window,
8596 a pointer to which is passed in A1. A2..A4 are currently not used.
8597 Change the height of W so that all of the message is displayed.
8598 Value is non-zero if height of W was changed. */
8599
8600 static int
8601 display_echo_area_1 (a1, a2, a3, a4)
8602 EMACS_INT a1;
8603 Lisp_Object a2;
8604 EMACS_INT a3, a4;
8605 {
8606 struct window *w = (struct window *) a1;
8607 Lisp_Object window;
8608 struct text_pos start;
8609 int window_height_changed_p = 0;
8610
8611 /* Do this before displaying, so that we have a large enough glyph
8612 matrix for the display. If we can't get enough space for the
8613 whole text, display the last N lines. That works by setting w->start. */
8614 window_height_changed_p = resize_mini_window (w, 0);
8615
8616 /* Use the starting position chosen by resize_mini_window. */
8617 SET_TEXT_POS_FROM_MARKER (start, w->start);
8618
8619 /* Display. */
8620 clear_glyph_matrix (w->desired_matrix);
8621 XSETWINDOW (window, w);
8622 try_window (window, start, 0);
8623
8624 return window_height_changed_p;
8625 }
8626
8627
8628 /* Resize the echo area window to exactly the size needed for the
8629 currently displayed message, if there is one. If a mini-buffer
8630 is active, don't shrink it. */
8631
8632 void
8633 resize_echo_area_exactly ()
8634 {
8635 if (BUFFERP (echo_area_buffer[0])
8636 && WINDOWP (echo_area_window))
8637 {
8638 struct window *w = XWINDOW (echo_area_window);
8639 int resized_p;
8640 Lisp_Object resize_exactly;
8641
8642 if (minibuf_level == 0)
8643 resize_exactly = Qt;
8644 else
8645 resize_exactly = Qnil;
8646
8647 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8648 (EMACS_INT) w, resize_exactly, 0, 0);
8649 if (resized_p)
8650 {
8651 ++windows_or_buffers_changed;
8652 ++update_mode_lines;
8653 redisplay_internal (0);
8654 }
8655 }
8656 }
8657
8658
8659 /* Callback function for with_echo_area_buffer, when used from
8660 resize_echo_area_exactly. A1 contains a pointer to the window to
8661 resize, EXACTLY non-nil means resize the mini-window exactly to the
8662 size of the text displayed. A3 and A4 are not used. Value is what
8663 resize_mini_window returns. */
8664
8665 static int
8666 resize_mini_window_1 (a1, exactly, a3, a4)
8667 EMACS_INT a1;
8668 Lisp_Object exactly;
8669 EMACS_INT a3, a4;
8670 {
8671 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8672 }
8673
8674
8675 /* Resize mini-window W to fit the size of its contents. EXACT_P
8676 means size the window exactly to the size needed. Otherwise, it's
8677 only enlarged until W's buffer is empty.
8678
8679 Set W->start to the right place to begin display. If the whole
8680 contents fit, start at the beginning. Otherwise, start so as
8681 to make the end of the contents appear. This is particularly
8682 important for y-or-n-p, but seems desirable generally.
8683
8684 Value is non-zero if the window height has been changed. */
8685
8686 int
8687 resize_mini_window (w, exact_p)
8688 struct window *w;
8689 int exact_p;
8690 {
8691 struct frame *f = XFRAME (w->frame);
8692 int window_height_changed_p = 0;
8693
8694 xassert (MINI_WINDOW_P (w));
8695
8696 /* By default, start display at the beginning. */
8697 set_marker_both (w->start, w->buffer,
8698 BUF_BEGV (XBUFFER (w->buffer)),
8699 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8700
8701 /* Don't resize windows while redisplaying a window; it would
8702 confuse redisplay functions when the size of the window they are
8703 displaying changes from under them. Such a resizing can happen,
8704 for instance, when which-func prints a long message while
8705 we are running fontification-functions. We're running these
8706 functions with safe_call which binds inhibit-redisplay to t. */
8707 if (!NILP (Vinhibit_redisplay))
8708 return 0;
8709
8710 /* Nil means don't try to resize. */
8711 if (NILP (Vresize_mini_windows)
8712 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8713 return 0;
8714
8715 if (!FRAME_MINIBUF_ONLY_P (f))
8716 {
8717 struct it it;
8718 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8719 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8720 int height, max_height;
8721 int unit = FRAME_LINE_HEIGHT (f);
8722 struct text_pos start;
8723 struct buffer *old_current_buffer = NULL;
8724
8725 if (current_buffer != XBUFFER (w->buffer))
8726 {
8727 old_current_buffer = current_buffer;
8728 set_buffer_internal (XBUFFER (w->buffer));
8729 }
8730
8731 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8732
8733 /* Compute the max. number of lines specified by the user. */
8734 if (FLOATP (Vmax_mini_window_height))
8735 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8736 else if (INTEGERP (Vmax_mini_window_height))
8737 max_height = XINT (Vmax_mini_window_height);
8738 else
8739 max_height = total_height / 4;
8740
8741 /* Correct that max. height if it's bogus. */
8742 max_height = max (1, max_height);
8743 max_height = min (total_height, max_height);
8744
8745 /* Find out the height of the text in the window. */
8746 if (it.line_wrap == TRUNCATE)
8747 height = 1;
8748 else
8749 {
8750 last_height = 0;
8751 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8752 if (it.max_ascent == 0 && it.max_descent == 0)
8753 height = it.current_y + last_height;
8754 else
8755 height = it.current_y + it.max_ascent + it.max_descent;
8756 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8757 height = (height + unit - 1) / unit;
8758 }
8759
8760 /* Compute a suitable window start. */
8761 if (height > max_height)
8762 {
8763 height = max_height;
8764 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8765 move_it_vertically_backward (&it, (height - 1) * unit);
8766 start = it.current.pos;
8767 }
8768 else
8769 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8770 SET_MARKER_FROM_TEXT_POS (w->start, start);
8771
8772 if (EQ (Vresize_mini_windows, Qgrow_only))
8773 {
8774 /* Let it grow only, until we display an empty message, in which
8775 case the window shrinks again. */
8776 if (height > WINDOW_TOTAL_LINES (w))
8777 {
8778 int old_height = WINDOW_TOTAL_LINES (w);
8779 freeze_window_starts (f, 1);
8780 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8781 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8782 }
8783 else if (height < WINDOW_TOTAL_LINES (w)
8784 && (exact_p || BEGV == ZV))
8785 {
8786 int old_height = WINDOW_TOTAL_LINES (w);
8787 freeze_window_starts (f, 0);
8788 shrink_mini_window (w);
8789 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8790 }
8791 }
8792 else
8793 {
8794 /* Always resize to exact size needed. */
8795 if (height > WINDOW_TOTAL_LINES (w))
8796 {
8797 int old_height = WINDOW_TOTAL_LINES (w);
8798 freeze_window_starts (f, 1);
8799 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8800 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8801 }
8802 else if (height < WINDOW_TOTAL_LINES (w))
8803 {
8804 int old_height = WINDOW_TOTAL_LINES (w);
8805 freeze_window_starts (f, 0);
8806 shrink_mini_window (w);
8807
8808 if (height)
8809 {
8810 freeze_window_starts (f, 1);
8811 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8812 }
8813
8814 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8815 }
8816 }
8817
8818 if (old_current_buffer)
8819 set_buffer_internal (old_current_buffer);
8820 }
8821
8822 return window_height_changed_p;
8823 }
8824
8825
8826 /* Value is the current message, a string, or nil if there is no
8827 current message. */
8828
8829 Lisp_Object
8830 current_message ()
8831 {
8832 Lisp_Object msg;
8833
8834 if (!BUFFERP (echo_area_buffer[0]))
8835 msg = Qnil;
8836 else
8837 {
8838 with_echo_area_buffer (0, 0, current_message_1,
8839 (EMACS_INT) &msg, Qnil, 0, 0);
8840 if (NILP (msg))
8841 echo_area_buffer[0] = Qnil;
8842 }
8843
8844 return msg;
8845 }
8846
8847
8848 static int
8849 current_message_1 (a1, a2, a3, a4)
8850 EMACS_INT a1;
8851 Lisp_Object a2;
8852 EMACS_INT a3, a4;
8853 {
8854 Lisp_Object *msg = (Lisp_Object *) a1;
8855
8856 if (Z > BEG)
8857 *msg = make_buffer_string (BEG, Z, 1);
8858 else
8859 *msg = Qnil;
8860 return 0;
8861 }
8862
8863
8864 /* Push the current message on Vmessage_stack for later restauration
8865 by restore_message. Value is non-zero if the current message isn't
8866 empty. This is a relatively infrequent operation, so it's not
8867 worth optimizing. */
8868
8869 int
8870 push_message ()
8871 {
8872 Lisp_Object msg;
8873 msg = current_message ();
8874 Vmessage_stack = Fcons (msg, Vmessage_stack);
8875 return STRINGP (msg);
8876 }
8877
8878
8879 /* Restore message display from the top of Vmessage_stack. */
8880
8881 void
8882 restore_message ()
8883 {
8884 Lisp_Object msg;
8885
8886 xassert (CONSP (Vmessage_stack));
8887 msg = XCAR (Vmessage_stack);
8888 if (STRINGP (msg))
8889 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8890 else
8891 message3_nolog (msg, 0, 0);
8892 }
8893
8894
8895 /* Handler for record_unwind_protect calling pop_message. */
8896
8897 Lisp_Object
8898 pop_message_unwind (dummy)
8899 Lisp_Object dummy;
8900 {
8901 pop_message ();
8902 return Qnil;
8903 }
8904
8905 /* Pop the top-most entry off Vmessage_stack. */
8906
8907 void
8908 pop_message ()
8909 {
8910 xassert (CONSP (Vmessage_stack));
8911 Vmessage_stack = XCDR (Vmessage_stack);
8912 }
8913
8914
8915 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8916 exits. If the stack is not empty, we have a missing pop_message
8917 somewhere. */
8918
8919 void
8920 check_message_stack ()
8921 {
8922 if (!NILP (Vmessage_stack))
8923 abort ();
8924 }
8925
8926
8927 /* Truncate to NCHARS what will be displayed in the echo area the next
8928 time we display it---but don't redisplay it now. */
8929
8930 void
8931 truncate_echo_area (nchars)
8932 int nchars;
8933 {
8934 if (nchars == 0)
8935 echo_area_buffer[0] = Qnil;
8936 /* A null message buffer means that the frame hasn't really been
8937 initialized yet. Error messages get reported properly by
8938 cmd_error, so this must be just an informative message; toss it. */
8939 else if (!noninteractive
8940 && INTERACTIVE
8941 && !NILP (echo_area_buffer[0]))
8942 {
8943 struct frame *sf = SELECTED_FRAME ();
8944 if (FRAME_MESSAGE_BUF (sf))
8945 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8946 }
8947 }
8948
8949
8950 /* Helper function for truncate_echo_area. Truncate the current
8951 message to at most NCHARS characters. */
8952
8953 static int
8954 truncate_message_1 (nchars, a2, a3, a4)
8955 EMACS_INT nchars;
8956 Lisp_Object a2;
8957 EMACS_INT a3, a4;
8958 {
8959 if (BEG + nchars < Z)
8960 del_range (BEG + nchars, Z);
8961 if (Z == BEG)
8962 echo_area_buffer[0] = Qnil;
8963 return 0;
8964 }
8965
8966
8967 /* Set the current message to a substring of S or STRING.
8968
8969 If STRING is a Lisp string, set the message to the first NBYTES
8970 bytes from STRING. NBYTES zero means use the whole string. If
8971 STRING is multibyte, the message will be displayed multibyte.
8972
8973 If S is not null, set the message to the first LEN bytes of S. LEN
8974 zero means use the whole string. MULTIBYTE_P non-zero means S is
8975 multibyte. Display the message multibyte in that case.
8976
8977 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8978 to t before calling set_message_1 (which calls insert).
8979 */
8980
8981 void
8982 set_message (s, string, nbytes, multibyte_p)
8983 const char *s;
8984 Lisp_Object string;
8985 int nbytes, multibyte_p;
8986 {
8987 message_enable_multibyte
8988 = ((s && multibyte_p)
8989 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8990
8991 with_echo_area_buffer (0, -1, set_message_1,
8992 (EMACS_INT) s, string, nbytes, multibyte_p);
8993 message_buf_print = 0;
8994 help_echo_showing_p = 0;
8995 }
8996
8997
8998 /* Helper function for set_message. Arguments have the same meaning
8999 as there, with A1 corresponding to S and A2 corresponding to STRING
9000 This function is called with the echo area buffer being
9001 current. */
9002
9003 static int
9004 set_message_1 (a1, a2, nbytes, multibyte_p)
9005 EMACS_INT a1;
9006 Lisp_Object a2;
9007 EMACS_INT nbytes, multibyte_p;
9008 {
9009 const char *s = (const char *) a1;
9010 Lisp_Object string = a2;
9011
9012 /* Change multibyteness of the echo buffer appropriately. */
9013 if (message_enable_multibyte
9014 != !NILP (current_buffer->enable_multibyte_characters))
9015 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9016
9017 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9018
9019 /* Insert new message at BEG. */
9020 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9021
9022 if (STRINGP (string))
9023 {
9024 int nchars;
9025
9026 if (nbytes == 0)
9027 nbytes = SBYTES (string);
9028 nchars = string_byte_to_char (string, nbytes);
9029
9030 /* This function takes care of single/multibyte conversion. We
9031 just have to ensure that the echo area buffer has the right
9032 setting of enable_multibyte_characters. */
9033 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9034 }
9035 else if (s)
9036 {
9037 if (nbytes == 0)
9038 nbytes = strlen (s);
9039
9040 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9041 {
9042 /* Convert from multi-byte to single-byte. */
9043 int i, c, n;
9044 unsigned char work[1];
9045
9046 /* Convert a multibyte string to single-byte. */
9047 for (i = 0; i < nbytes; i += n)
9048 {
9049 c = string_char_and_length (s + i, nbytes - i, &n);
9050 work[0] = (ASCII_CHAR_P (c)
9051 ? c
9052 : multibyte_char_to_unibyte (c, Qnil));
9053 insert_1_both (work, 1, 1, 1, 0, 0);
9054 }
9055 }
9056 else if (!multibyte_p
9057 && !NILP (current_buffer->enable_multibyte_characters))
9058 {
9059 /* Convert from single-byte to multi-byte. */
9060 int i, c, n;
9061 const unsigned char *msg = (const unsigned char *) s;
9062 unsigned char str[MAX_MULTIBYTE_LENGTH];
9063
9064 /* Convert a single-byte string to multibyte. */
9065 for (i = 0; i < nbytes; i++)
9066 {
9067 c = msg[i];
9068 c = unibyte_char_to_multibyte (c);
9069 n = CHAR_STRING (c, str);
9070 insert_1_both (str, 1, n, 1, 0, 0);
9071 }
9072 }
9073 else
9074 insert_1 (s, nbytes, 1, 0, 0);
9075 }
9076
9077 return 0;
9078 }
9079
9080
9081 /* Clear messages. CURRENT_P non-zero means clear the current
9082 message. LAST_DISPLAYED_P non-zero means clear the message
9083 last displayed. */
9084
9085 void
9086 clear_message (current_p, last_displayed_p)
9087 int current_p, last_displayed_p;
9088 {
9089 if (current_p)
9090 {
9091 echo_area_buffer[0] = Qnil;
9092 message_cleared_p = 1;
9093 }
9094
9095 if (last_displayed_p)
9096 echo_area_buffer[1] = Qnil;
9097
9098 message_buf_print = 0;
9099 }
9100
9101 /* Clear garbaged frames.
9102
9103 This function is used where the old redisplay called
9104 redraw_garbaged_frames which in turn called redraw_frame which in
9105 turn called clear_frame. The call to clear_frame was a source of
9106 flickering. I believe a clear_frame is not necessary. It should
9107 suffice in the new redisplay to invalidate all current matrices,
9108 and ensure a complete redisplay of all windows. */
9109
9110 static void
9111 clear_garbaged_frames ()
9112 {
9113 if (frame_garbaged)
9114 {
9115 Lisp_Object tail, frame;
9116 int changed_count = 0;
9117
9118 FOR_EACH_FRAME (tail, frame)
9119 {
9120 struct frame *f = XFRAME (frame);
9121
9122 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9123 {
9124 if (f->resized_p)
9125 {
9126 Fredraw_frame (frame);
9127 f->force_flush_display_p = 1;
9128 }
9129 clear_current_matrices (f);
9130 changed_count++;
9131 f->garbaged = 0;
9132 f->resized_p = 0;
9133 }
9134 }
9135
9136 frame_garbaged = 0;
9137 if (changed_count)
9138 ++windows_or_buffers_changed;
9139 }
9140 }
9141
9142
9143 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9144 is non-zero update selected_frame. Value is non-zero if the
9145 mini-windows height has been changed. */
9146
9147 static int
9148 echo_area_display (update_frame_p)
9149 int update_frame_p;
9150 {
9151 Lisp_Object mini_window;
9152 struct window *w;
9153 struct frame *f;
9154 int window_height_changed_p = 0;
9155 struct frame *sf = SELECTED_FRAME ();
9156
9157 mini_window = FRAME_MINIBUF_WINDOW (sf);
9158 w = XWINDOW (mini_window);
9159 f = XFRAME (WINDOW_FRAME (w));
9160
9161 /* Don't display if frame is invisible or not yet initialized. */
9162 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9163 return 0;
9164
9165 #ifdef HAVE_WINDOW_SYSTEM
9166 /* When Emacs starts, selected_frame may be the initial terminal
9167 frame. If we let this through, a message would be displayed on
9168 the terminal. */
9169 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9170 return 0;
9171 #endif /* HAVE_WINDOW_SYSTEM */
9172
9173 /* Redraw garbaged frames. */
9174 if (frame_garbaged)
9175 clear_garbaged_frames ();
9176
9177 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9178 {
9179 echo_area_window = mini_window;
9180 window_height_changed_p = display_echo_area (w);
9181 w->must_be_updated_p = 1;
9182
9183 /* Update the display, unless called from redisplay_internal.
9184 Also don't update the screen during redisplay itself. The
9185 update will happen at the end of redisplay, and an update
9186 here could cause confusion. */
9187 if (update_frame_p && !redisplaying_p)
9188 {
9189 int n = 0;
9190
9191 /* If the display update has been interrupted by pending
9192 input, update mode lines in the frame. Due to the
9193 pending input, it might have been that redisplay hasn't
9194 been called, so that mode lines above the echo area are
9195 garbaged. This looks odd, so we prevent it here. */
9196 if (!display_completed)
9197 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9198
9199 if (window_height_changed_p
9200 /* Don't do this if Emacs is shutting down. Redisplay
9201 needs to run hooks. */
9202 && !NILP (Vrun_hooks))
9203 {
9204 /* Must update other windows. Likewise as in other
9205 cases, don't let this update be interrupted by
9206 pending input. */
9207 int count = SPECPDL_INDEX ();
9208 specbind (Qredisplay_dont_pause, Qt);
9209 windows_or_buffers_changed = 1;
9210 redisplay_internal (0);
9211 unbind_to (count, Qnil);
9212 }
9213 else if (FRAME_WINDOW_P (f) && n == 0)
9214 {
9215 /* Window configuration is the same as before.
9216 Can do with a display update of the echo area,
9217 unless we displayed some mode lines. */
9218 update_single_window (w, 1);
9219 FRAME_RIF (f)->flush_display (f);
9220 }
9221 else
9222 update_frame (f, 1, 1);
9223
9224 /* If cursor is in the echo area, make sure that the next
9225 redisplay displays the minibuffer, so that the cursor will
9226 be replaced with what the minibuffer wants. */
9227 if (cursor_in_echo_area)
9228 ++windows_or_buffers_changed;
9229 }
9230 }
9231 else if (!EQ (mini_window, selected_window))
9232 windows_or_buffers_changed++;
9233
9234 /* Last displayed message is now the current message. */
9235 echo_area_buffer[1] = echo_area_buffer[0];
9236 /* Inform read_char that we're not echoing. */
9237 echo_message_buffer = Qnil;
9238
9239 /* Prevent redisplay optimization in redisplay_internal by resetting
9240 this_line_start_pos. This is done because the mini-buffer now
9241 displays the message instead of its buffer text. */
9242 if (EQ (mini_window, selected_window))
9243 CHARPOS (this_line_start_pos) = 0;
9244
9245 return window_height_changed_p;
9246 }
9247
9248
9249 \f
9250 /***********************************************************************
9251 Mode Lines and Frame Titles
9252 ***********************************************************************/
9253
9254 /* A buffer for constructing non-propertized mode-line strings and
9255 frame titles in it; allocated from the heap in init_xdisp and
9256 resized as needed in store_mode_line_noprop_char. */
9257
9258 static char *mode_line_noprop_buf;
9259
9260 /* The buffer's end, and a current output position in it. */
9261
9262 static char *mode_line_noprop_buf_end;
9263 static char *mode_line_noprop_ptr;
9264
9265 #define MODE_LINE_NOPROP_LEN(start) \
9266 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9267
9268 static enum {
9269 MODE_LINE_DISPLAY = 0,
9270 MODE_LINE_TITLE,
9271 MODE_LINE_NOPROP,
9272 MODE_LINE_STRING
9273 } mode_line_target;
9274
9275 /* Alist that caches the results of :propertize.
9276 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9277 static Lisp_Object mode_line_proptrans_alist;
9278
9279 /* List of strings making up the mode-line. */
9280 static Lisp_Object mode_line_string_list;
9281
9282 /* Base face property when building propertized mode line string. */
9283 static Lisp_Object mode_line_string_face;
9284 static Lisp_Object mode_line_string_face_prop;
9285
9286
9287 /* Unwind data for mode line strings */
9288
9289 static Lisp_Object Vmode_line_unwind_vector;
9290
9291 static Lisp_Object
9292 format_mode_line_unwind_data (struct buffer *obuf,
9293 Lisp_Object owin,
9294 int save_proptrans)
9295 {
9296 Lisp_Object vector, tmp;
9297
9298 /* Reduce consing by keeping one vector in
9299 Vwith_echo_area_save_vector. */
9300 vector = Vmode_line_unwind_vector;
9301 Vmode_line_unwind_vector = Qnil;
9302
9303 if (NILP (vector))
9304 vector = Fmake_vector (make_number (8), Qnil);
9305
9306 ASET (vector, 0, make_number (mode_line_target));
9307 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9308 ASET (vector, 2, mode_line_string_list);
9309 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9310 ASET (vector, 4, mode_line_string_face);
9311 ASET (vector, 5, mode_line_string_face_prop);
9312
9313 if (obuf)
9314 XSETBUFFER (tmp, obuf);
9315 else
9316 tmp = Qnil;
9317 ASET (vector, 6, tmp);
9318 ASET (vector, 7, owin);
9319
9320 return vector;
9321 }
9322
9323 static Lisp_Object
9324 unwind_format_mode_line (vector)
9325 Lisp_Object vector;
9326 {
9327 mode_line_target = XINT (AREF (vector, 0));
9328 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9329 mode_line_string_list = AREF (vector, 2);
9330 if (! EQ (AREF (vector, 3), Qt))
9331 mode_line_proptrans_alist = AREF (vector, 3);
9332 mode_line_string_face = AREF (vector, 4);
9333 mode_line_string_face_prop = AREF (vector, 5);
9334
9335 if (!NILP (AREF (vector, 7)))
9336 /* Select window before buffer, since it may change the buffer. */
9337 Fselect_window (AREF (vector, 7), Qt);
9338
9339 if (!NILP (AREF (vector, 6)))
9340 {
9341 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9342 ASET (vector, 6, Qnil);
9343 }
9344
9345 Vmode_line_unwind_vector = vector;
9346 return Qnil;
9347 }
9348
9349
9350 /* Store a single character C for the frame title in mode_line_noprop_buf.
9351 Re-allocate mode_line_noprop_buf if necessary. */
9352
9353 static void
9354 #ifdef PROTOTYPES
9355 store_mode_line_noprop_char (char c)
9356 #else
9357 store_mode_line_noprop_char (c)
9358 char c;
9359 #endif
9360 {
9361 /* If output position has reached the end of the allocated buffer,
9362 double the buffer's size. */
9363 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9364 {
9365 int len = MODE_LINE_NOPROP_LEN (0);
9366 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9367 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9368 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9369 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9370 }
9371
9372 *mode_line_noprop_ptr++ = c;
9373 }
9374
9375
9376 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9377 mode_line_noprop_ptr. STR is the string to store. Do not copy
9378 characters that yield more columns than PRECISION; PRECISION <= 0
9379 means copy the whole string. Pad with spaces until FIELD_WIDTH
9380 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9381 pad. Called from display_mode_element when it is used to build a
9382 frame title. */
9383
9384 static int
9385 store_mode_line_noprop (str, field_width, precision)
9386 const unsigned char *str;
9387 int field_width, precision;
9388 {
9389 int n = 0;
9390 int dummy, nbytes;
9391
9392 /* Copy at most PRECISION chars from STR. */
9393 nbytes = strlen (str);
9394 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9395 while (nbytes--)
9396 store_mode_line_noprop_char (*str++);
9397
9398 /* Fill up with spaces until FIELD_WIDTH reached. */
9399 while (field_width > 0
9400 && n < field_width)
9401 {
9402 store_mode_line_noprop_char (' ');
9403 ++n;
9404 }
9405
9406 return n;
9407 }
9408
9409 /***********************************************************************
9410 Frame Titles
9411 ***********************************************************************/
9412
9413 #ifdef HAVE_WINDOW_SYSTEM
9414
9415 /* Set the title of FRAME, if it has changed. The title format is
9416 Vicon_title_format if FRAME is iconified, otherwise it is
9417 frame_title_format. */
9418
9419 static void
9420 x_consider_frame_title (frame)
9421 Lisp_Object frame;
9422 {
9423 struct frame *f = XFRAME (frame);
9424
9425 if (FRAME_WINDOW_P (f)
9426 || FRAME_MINIBUF_ONLY_P (f)
9427 || f->explicit_name)
9428 {
9429 /* Do we have more than one visible frame on this X display? */
9430 Lisp_Object tail;
9431 Lisp_Object fmt;
9432 int title_start;
9433 char *title;
9434 int len;
9435 struct it it;
9436 int count = SPECPDL_INDEX ();
9437
9438 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9439 {
9440 Lisp_Object other_frame = XCAR (tail);
9441 struct frame *tf = XFRAME (other_frame);
9442
9443 if (tf != f
9444 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9445 && !FRAME_MINIBUF_ONLY_P (tf)
9446 && !EQ (other_frame, tip_frame)
9447 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9448 break;
9449 }
9450
9451 /* Set global variable indicating that multiple frames exist. */
9452 multiple_frames = CONSP (tail);
9453
9454 /* Switch to the buffer of selected window of the frame. Set up
9455 mode_line_target so that display_mode_element will output into
9456 mode_line_noprop_buf; then display the title. */
9457 record_unwind_protect (unwind_format_mode_line,
9458 format_mode_line_unwind_data
9459 (current_buffer, selected_window, 0));
9460
9461 Fselect_window (f->selected_window, Qt);
9462 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9463 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9464
9465 mode_line_target = MODE_LINE_TITLE;
9466 title_start = MODE_LINE_NOPROP_LEN (0);
9467 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9468 NULL, DEFAULT_FACE_ID);
9469 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9470 len = MODE_LINE_NOPROP_LEN (title_start);
9471 title = mode_line_noprop_buf + title_start;
9472 unbind_to (count, Qnil);
9473
9474 /* Set the title only if it's changed. This avoids consing in
9475 the common case where it hasn't. (If it turns out that we've
9476 already wasted too much time by walking through the list with
9477 display_mode_element, then we might need to optimize at a
9478 higher level than this.) */
9479 if (! STRINGP (f->name)
9480 || SBYTES (f->name) != len
9481 || bcmp (title, SDATA (f->name), len) != 0)
9482 {
9483 #ifdef HAVE_NS
9484 if (FRAME_NS_P (f))
9485 {
9486 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9487 {
9488 if (EQ (fmt, Qt))
9489 ns_set_name_as_filename (f);
9490 else
9491 x_implicitly_set_name (f, make_string(title, len),
9492 Qnil);
9493 }
9494 }
9495 else
9496 #endif
9497 x_implicitly_set_name (f, make_string (title, len), Qnil);
9498 }
9499 #ifdef HAVE_NS
9500 if (FRAME_NS_P (f))
9501 {
9502 /* do this also for frames with explicit names */
9503 ns_implicitly_set_icon_type(f);
9504 ns_set_doc_edited(f, Fbuffer_modified_p
9505 (XWINDOW (f->selected_window)->buffer), Qnil);
9506 }
9507 #endif
9508 }
9509 }
9510
9511 #endif /* not HAVE_WINDOW_SYSTEM */
9512
9513
9514
9515 \f
9516 /***********************************************************************
9517 Menu Bars
9518 ***********************************************************************/
9519
9520
9521 /* Prepare for redisplay by updating menu-bar item lists when
9522 appropriate. This can call eval. */
9523
9524 void
9525 prepare_menu_bars ()
9526 {
9527 int all_windows;
9528 struct gcpro gcpro1, gcpro2;
9529 struct frame *f;
9530 Lisp_Object tooltip_frame;
9531
9532 #ifdef HAVE_WINDOW_SYSTEM
9533 tooltip_frame = tip_frame;
9534 #else
9535 tooltip_frame = Qnil;
9536 #endif
9537
9538 /* Update all frame titles based on their buffer names, etc. We do
9539 this before the menu bars so that the buffer-menu will show the
9540 up-to-date frame titles. */
9541 #ifdef HAVE_WINDOW_SYSTEM
9542 if (windows_or_buffers_changed || update_mode_lines)
9543 {
9544 Lisp_Object tail, frame;
9545
9546 FOR_EACH_FRAME (tail, frame)
9547 {
9548 f = XFRAME (frame);
9549 if (!EQ (frame, tooltip_frame)
9550 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9551 x_consider_frame_title (frame);
9552 }
9553 }
9554 #endif /* HAVE_WINDOW_SYSTEM */
9555
9556 /* Update the menu bar item lists, if appropriate. This has to be
9557 done before any actual redisplay or generation of display lines. */
9558 all_windows = (update_mode_lines
9559 || buffer_shared > 1
9560 || windows_or_buffers_changed);
9561 if (all_windows)
9562 {
9563 Lisp_Object tail, frame;
9564 int count = SPECPDL_INDEX ();
9565 /* 1 means that update_menu_bar has run its hooks
9566 so any further calls to update_menu_bar shouldn't do so again. */
9567 int menu_bar_hooks_run = 0;
9568
9569 record_unwind_save_match_data ();
9570
9571 FOR_EACH_FRAME (tail, frame)
9572 {
9573 f = XFRAME (frame);
9574
9575 /* Ignore tooltip frame. */
9576 if (EQ (frame, tooltip_frame))
9577 continue;
9578
9579 /* If a window on this frame changed size, report that to
9580 the user and clear the size-change flag. */
9581 if (FRAME_WINDOW_SIZES_CHANGED (f))
9582 {
9583 Lisp_Object functions;
9584
9585 /* Clear flag first in case we get an error below. */
9586 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9587 functions = Vwindow_size_change_functions;
9588 GCPRO2 (tail, functions);
9589
9590 while (CONSP (functions))
9591 {
9592 if (!EQ (XCAR (functions), Qt))
9593 call1 (XCAR (functions), frame);
9594 functions = XCDR (functions);
9595 }
9596 UNGCPRO;
9597 }
9598
9599 GCPRO1 (tail);
9600 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9601 #ifdef HAVE_WINDOW_SYSTEM
9602 update_tool_bar (f, 0);
9603 #endif
9604 UNGCPRO;
9605 }
9606
9607 unbind_to (count, Qnil);
9608 }
9609 else
9610 {
9611 struct frame *sf = SELECTED_FRAME ();
9612 update_menu_bar (sf, 1, 0);
9613 #ifdef HAVE_WINDOW_SYSTEM
9614 update_tool_bar (sf, 1);
9615 #endif
9616 }
9617
9618 /* Motif needs this. See comment in xmenu.c. Turn it off when
9619 pending_menu_activation is not defined. */
9620 #ifdef USE_X_TOOLKIT
9621 pending_menu_activation = 0;
9622 #endif
9623 }
9624
9625
9626 /* Update the menu bar item list for frame F. This has to be done
9627 before we start to fill in any display lines, because it can call
9628 eval.
9629
9630 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9631
9632 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9633 already ran the menu bar hooks for this redisplay, so there
9634 is no need to run them again. The return value is the
9635 updated value of this flag, to pass to the next call. */
9636
9637 static int
9638 update_menu_bar (f, save_match_data, hooks_run)
9639 struct frame *f;
9640 int save_match_data;
9641 int hooks_run;
9642 {
9643 Lisp_Object window;
9644 register struct window *w;
9645
9646 /* If called recursively during a menu update, do nothing. This can
9647 happen when, for instance, an activate-menubar-hook causes a
9648 redisplay. */
9649 if (inhibit_menubar_update)
9650 return hooks_run;
9651
9652 window = FRAME_SELECTED_WINDOW (f);
9653 w = XWINDOW (window);
9654
9655 if (FRAME_WINDOW_P (f)
9656 ?
9657 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9658 || defined (HAVE_NS) || defined (USE_GTK)
9659 FRAME_EXTERNAL_MENU_BAR (f)
9660 #else
9661 FRAME_MENU_BAR_LINES (f) > 0
9662 #endif
9663 : FRAME_MENU_BAR_LINES (f) > 0)
9664 {
9665 /* If the user has switched buffers or windows, we need to
9666 recompute to reflect the new bindings. But we'll
9667 recompute when update_mode_lines is set too; that means
9668 that people can use force-mode-line-update to request
9669 that the menu bar be recomputed. The adverse effect on
9670 the rest of the redisplay algorithm is about the same as
9671 windows_or_buffers_changed anyway. */
9672 if (windows_or_buffers_changed
9673 /* This used to test w->update_mode_line, but we believe
9674 there is no need to recompute the menu in that case. */
9675 || update_mode_lines
9676 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9677 < BUF_MODIFF (XBUFFER (w->buffer)))
9678 != !NILP (w->last_had_star))
9679 || ((!NILP (Vtransient_mark_mode)
9680 && !NILP (XBUFFER (w->buffer)->mark_active))
9681 != !NILP (w->region_showing)))
9682 {
9683 struct buffer *prev = current_buffer;
9684 int count = SPECPDL_INDEX ();
9685
9686 specbind (Qinhibit_menubar_update, Qt);
9687
9688 set_buffer_internal_1 (XBUFFER (w->buffer));
9689 if (save_match_data)
9690 record_unwind_save_match_data ();
9691 if (NILP (Voverriding_local_map_menu_flag))
9692 {
9693 specbind (Qoverriding_terminal_local_map, Qnil);
9694 specbind (Qoverriding_local_map, Qnil);
9695 }
9696
9697 if (!hooks_run)
9698 {
9699 /* Run the Lucid hook. */
9700 safe_run_hooks (Qactivate_menubar_hook);
9701
9702 /* If it has changed current-menubar from previous value,
9703 really recompute the menu-bar from the value. */
9704 if (! NILP (Vlucid_menu_bar_dirty_flag))
9705 call0 (Qrecompute_lucid_menubar);
9706
9707 safe_run_hooks (Qmenu_bar_update_hook);
9708
9709 hooks_run = 1;
9710 }
9711
9712 XSETFRAME (Vmenu_updating_frame, f);
9713 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9714
9715 /* Redisplay the menu bar in case we changed it. */
9716 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9717 || defined (HAVE_NS) || defined (USE_GTK)
9718 if (FRAME_WINDOW_P (f))
9719 {
9720 #if defined (HAVE_NS)
9721 /* All frames on Mac OS share the same menubar. So only
9722 the selected frame should be allowed to set it. */
9723 if (f == SELECTED_FRAME ())
9724 #endif
9725 set_frame_menubar (f, 0, 0);
9726 }
9727 else
9728 /* On a terminal screen, the menu bar is an ordinary screen
9729 line, and this makes it get updated. */
9730 w->update_mode_line = Qt;
9731 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9732 /* In the non-toolkit version, the menu bar is an ordinary screen
9733 line, and this makes it get updated. */
9734 w->update_mode_line = Qt;
9735 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9736
9737 unbind_to (count, Qnil);
9738 set_buffer_internal_1 (prev);
9739 }
9740 }
9741
9742 return hooks_run;
9743 }
9744
9745
9746 \f
9747 /***********************************************************************
9748 Output Cursor
9749 ***********************************************************************/
9750
9751 #ifdef HAVE_WINDOW_SYSTEM
9752
9753 /* EXPORT:
9754 Nominal cursor position -- where to draw output.
9755 HPOS and VPOS are window relative glyph matrix coordinates.
9756 X and Y are window relative pixel coordinates. */
9757
9758 struct cursor_pos output_cursor;
9759
9760
9761 /* EXPORT:
9762 Set the global variable output_cursor to CURSOR. All cursor
9763 positions are relative to updated_window. */
9764
9765 void
9766 set_output_cursor (cursor)
9767 struct cursor_pos *cursor;
9768 {
9769 output_cursor.hpos = cursor->hpos;
9770 output_cursor.vpos = cursor->vpos;
9771 output_cursor.x = cursor->x;
9772 output_cursor.y = cursor->y;
9773 }
9774
9775
9776 /* EXPORT for RIF:
9777 Set a nominal cursor position.
9778
9779 HPOS and VPOS are column/row positions in a window glyph matrix. X
9780 and Y are window text area relative pixel positions.
9781
9782 If this is done during an update, updated_window will contain the
9783 window that is being updated and the position is the future output
9784 cursor position for that window. If updated_window is null, use
9785 selected_window and display the cursor at the given position. */
9786
9787 void
9788 x_cursor_to (vpos, hpos, y, x)
9789 int vpos, hpos, y, x;
9790 {
9791 struct window *w;
9792
9793 /* If updated_window is not set, work on selected_window. */
9794 if (updated_window)
9795 w = updated_window;
9796 else
9797 w = XWINDOW (selected_window);
9798
9799 /* Set the output cursor. */
9800 output_cursor.hpos = hpos;
9801 output_cursor.vpos = vpos;
9802 output_cursor.x = x;
9803 output_cursor.y = y;
9804
9805 /* If not called as part of an update, really display the cursor.
9806 This will also set the cursor position of W. */
9807 if (updated_window == NULL)
9808 {
9809 BLOCK_INPUT;
9810 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9811 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9812 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9813 UNBLOCK_INPUT;
9814 }
9815 }
9816
9817 #endif /* HAVE_WINDOW_SYSTEM */
9818
9819 \f
9820 /***********************************************************************
9821 Tool-bars
9822 ***********************************************************************/
9823
9824 #ifdef HAVE_WINDOW_SYSTEM
9825
9826 /* Where the mouse was last time we reported a mouse event. */
9827
9828 FRAME_PTR last_mouse_frame;
9829
9830 /* Tool-bar item index of the item on which a mouse button was pressed
9831 or -1. */
9832
9833 int last_tool_bar_item;
9834
9835
9836 static Lisp_Object
9837 update_tool_bar_unwind (frame)
9838 Lisp_Object frame;
9839 {
9840 selected_frame = frame;
9841 return Qnil;
9842 }
9843
9844 /* Update the tool-bar item list for frame F. This has to be done
9845 before we start to fill in any display lines. Called from
9846 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9847 and restore it here. */
9848
9849 static void
9850 update_tool_bar (f, save_match_data)
9851 struct frame *f;
9852 int save_match_data;
9853 {
9854 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
9855 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9856 #else
9857 int do_update = WINDOWP (f->tool_bar_window)
9858 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9859 #endif
9860
9861 if (do_update)
9862 {
9863 Lisp_Object window;
9864 struct window *w;
9865
9866 window = FRAME_SELECTED_WINDOW (f);
9867 w = XWINDOW (window);
9868
9869 /* If the user has switched buffers or windows, we need to
9870 recompute to reflect the new bindings. But we'll
9871 recompute when update_mode_lines is set too; that means
9872 that people can use force-mode-line-update to request
9873 that the menu bar be recomputed. The adverse effect on
9874 the rest of the redisplay algorithm is about the same as
9875 windows_or_buffers_changed anyway. */
9876 if (windows_or_buffers_changed
9877 || !NILP (w->update_mode_line)
9878 || update_mode_lines
9879 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9880 < BUF_MODIFF (XBUFFER (w->buffer)))
9881 != !NILP (w->last_had_star))
9882 || ((!NILP (Vtransient_mark_mode)
9883 && !NILP (XBUFFER (w->buffer)->mark_active))
9884 != !NILP (w->region_showing)))
9885 {
9886 struct buffer *prev = current_buffer;
9887 int count = SPECPDL_INDEX ();
9888 Lisp_Object frame, new_tool_bar;
9889 int new_n_tool_bar;
9890 struct gcpro gcpro1;
9891
9892 /* Set current_buffer to the buffer of the selected
9893 window of the frame, so that we get the right local
9894 keymaps. */
9895 set_buffer_internal_1 (XBUFFER (w->buffer));
9896
9897 /* Save match data, if we must. */
9898 if (save_match_data)
9899 record_unwind_save_match_data ();
9900
9901 /* Make sure that we don't accidentally use bogus keymaps. */
9902 if (NILP (Voverriding_local_map_menu_flag))
9903 {
9904 specbind (Qoverriding_terminal_local_map, Qnil);
9905 specbind (Qoverriding_local_map, Qnil);
9906 }
9907
9908 GCPRO1 (new_tool_bar);
9909
9910 /* We must temporarily set the selected frame to this frame
9911 before calling tool_bar_items, because the calculation of
9912 the tool-bar keymap uses the selected frame (see
9913 `tool-bar-make-keymap' in tool-bar.el). */
9914 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9915 XSETFRAME (frame, f);
9916 selected_frame = frame;
9917
9918 /* Build desired tool-bar items from keymaps. */
9919 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9920 &new_n_tool_bar);
9921
9922 /* Redisplay the tool-bar if we changed it. */
9923 if (new_n_tool_bar != f->n_tool_bar_items
9924 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9925 {
9926 /* Redisplay that happens asynchronously due to an expose event
9927 may access f->tool_bar_items. Make sure we update both
9928 variables within BLOCK_INPUT so no such event interrupts. */
9929 BLOCK_INPUT;
9930 f->tool_bar_items = new_tool_bar;
9931 f->n_tool_bar_items = new_n_tool_bar;
9932 w->update_mode_line = Qt;
9933 UNBLOCK_INPUT;
9934 }
9935
9936 UNGCPRO;
9937
9938 unbind_to (count, Qnil);
9939 set_buffer_internal_1 (prev);
9940 }
9941 }
9942 }
9943
9944
9945 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9946 F's desired tool-bar contents. F->tool_bar_items must have
9947 been set up previously by calling prepare_menu_bars. */
9948
9949 static void
9950 build_desired_tool_bar_string (f)
9951 struct frame *f;
9952 {
9953 int i, size, size_needed;
9954 struct gcpro gcpro1, gcpro2, gcpro3;
9955 Lisp_Object image, plist, props;
9956
9957 image = plist = props = Qnil;
9958 GCPRO3 (image, plist, props);
9959
9960 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9961 Otherwise, make a new string. */
9962
9963 /* The size of the string we might be able to reuse. */
9964 size = (STRINGP (f->desired_tool_bar_string)
9965 ? SCHARS (f->desired_tool_bar_string)
9966 : 0);
9967
9968 /* We need one space in the string for each image. */
9969 size_needed = f->n_tool_bar_items;
9970
9971 /* Reuse f->desired_tool_bar_string, if possible. */
9972 if (size < size_needed || NILP (f->desired_tool_bar_string))
9973 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9974 make_number (' '));
9975 else
9976 {
9977 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9978 Fremove_text_properties (make_number (0), make_number (size),
9979 props, f->desired_tool_bar_string);
9980 }
9981
9982 /* Put a `display' property on the string for the images to display,
9983 put a `menu_item' property on tool-bar items with a value that
9984 is the index of the item in F's tool-bar item vector. */
9985 for (i = 0; i < f->n_tool_bar_items; ++i)
9986 {
9987 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9988
9989 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9990 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9991 int hmargin, vmargin, relief, idx, end;
9992 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9993
9994 /* If image is a vector, choose the image according to the
9995 button state. */
9996 image = PROP (TOOL_BAR_ITEM_IMAGES);
9997 if (VECTORP (image))
9998 {
9999 if (enabled_p)
10000 idx = (selected_p
10001 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10002 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10003 else
10004 idx = (selected_p
10005 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10006 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10007
10008 xassert (ASIZE (image) >= idx);
10009 image = AREF (image, idx);
10010 }
10011 else
10012 idx = -1;
10013
10014 /* Ignore invalid image specifications. */
10015 if (!valid_image_p (image))
10016 continue;
10017
10018 /* Display the tool-bar button pressed, or depressed. */
10019 plist = Fcopy_sequence (XCDR (image));
10020
10021 /* Compute margin and relief to draw. */
10022 relief = (tool_bar_button_relief >= 0
10023 ? tool_bar_button_relief
10024 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10025 hmargin = vmargin = relief;
10026
10027 if (INTEGERP (Vtool_bar_button_margin)
10028 && XINT (Vtool_bar_button_margin) > 0)
10029 {
10030 hmargin += XFASTINT (Vtool_bar_button_margin);
10031 vmargin += XFASTINT (Vtool_bar_button_margin);
10032 }
10033 else if (CONSP (Vtool_bar_button_margin))
10034 {
10035 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10036 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10037 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10038
10039 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10040 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10041 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10042 }
10043
10044 if (auto_raise_tool_bar_buttons_p)
10045 {
10046 /* Add a `:relief' property to the image spec if the item is
10047 selected. */
10048 if (selected_p)
10049 {
10050 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10051 hmargin -= relief;
10052 vmargin -= relief;
10053 }
10054 }
10055 else
10056 {
10057 /* If image is selected, display it pressed, i.e. with a
10058 negative relief. If it's not selected, display it with a
10059 raised relief. */
10060 plist = Fplist_put (plist, QCrelief,
10061 (selected_p
10062 ? make_number (-relief)
10063 : make_number (relief)));
10064 hmargin -= relief;
10065 vmargin -= relief;
10066 }
10067
10068 /* Put a margin around the image. */
10069 if (hmargin || vmargin)
10070 {
10071 if (hmargin == vmargin)
10072 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10073 else
10074 plist = Fplist_put (plist, QCmargin,
10075 Fcons (make_number (hmargin),
10076 make_number (vmargin)));
10077 }
10078
10079 /* If button is not enabled, and we don't have special images
10080 for the disabled state, make the image appear disabled by
10081 applying an appropriate algorithm to it. */
10082 if (!enabled_p && idx < 0)
10083 plist = Fplist_put (plist, QCconversion, Qdisabled);
10084
10085 /* Put a `display' text property on the string for the image to
10086 display. Put a `menu-item' property on the string that gives
10087 the start of this item's properties in the tool-bar items
10088 vector. */
10089 image = Fcons (Qimage, plist);
10090 props = list4 (Qdisplay, image,
10091 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10092
10093 /* Let the last image hide all remaining spaces in the tool bar
10094 string. The string can be longer than needed when we reuse a
10095 previous string. */
10096 if (i + 1 == f->n_tool_bar_items)
10097 end = SCHARS (f->desired_tool_bar_string);
10098 else
10099 end = i + 1;
10100 Fadd_text_properties (make_number (i), make_number (end),
10101 props, f->desired_tool_bar_string);
10102 #undef PROP
10103 }
10104
10105 UNGCPRO;
10106 }
10107
10108
10109 /* Display one line of the tool-bar of frame IT->f.
10110
10111 HEIGHT specifies the desired height of the tool-bar line.
10112 If the actual height of the glyph row is less than HEIGHT, the
10113 row's height is increased to HEIGHT, and the icons are centered
10114 vertically in the new height.
10115
10116 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10117 count a final empty row in case the tool-bar width exactly matches
10118 the window width.
10119 */
10120
10121 static void
10122 display_tool_bar_line (it, height)
10123 struct it *it;
10124 int height;
10125 {
10126 struct glyph_row *row = it->glyph_row;
10127 int max_x = it->last_visible_x;
10128 struct glyph *last;
10129
10130 prepare_desired_row (row);
10131 row->y = it->current_y;
10132
10133 /* Note that this isn't made use of if the face hasn't a box,
10134 so there's no need to check the face here. */
10135 it->start_of_box_run_p = 1;
10136
10137 while (it->current_x < max_x)
10138 {
10139 int x, n_glyphs_before, i, nglyphs;
10140 struct it it_before;
10141
10142 /* Get the next display element. */
10143 if (!get_next_display_element (it))
10144 {
10145 /* Don't count empty row if we are counting needed tool-bar lines. */
10146 if (height < 0 && !it->hpos)
10147 return;
10148 break;
10149 }
10150
10151 /* Produce glyphs. */
10152 n_glyphs_before = row->used[TEXT_AREA];
10153 it_before = *it;
10154
10155 PRODUCE_GLYPHS (it);
10156
10157 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10158 i = 0;
10159 x = it_before.current_x;
10160 while (i < nglyphs)
10161 {
10162 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10163
10164 if (x + glyph->pixel_width > max_x)
10165 {
10166 /* Glyph doesn't fit on line. Backtrack. */
10167 row->used[TEXT_AREA] = n_glyphs_before;
10168 *it = it_before;
10169 /* If this is the only glyph on this line, it will never fit on the
10170 toolbar, so skip it. But ensure there is at least one glyph,
10171 so we don't accidentally disable the tool-bar. */
10172 if (n_glyphs_before == 0
10173 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10174 break;
10175 goto out;
10176 }
10177
10178 ++it->hpos;
10179 x += glyph->pixel_width;
10180 ++i;
10181 }
10182
10183 /* Stop at line ends. */
10184 if (ITERATOR_AT_END_OF_LINE_P (it))
10185 break;
10186
10187 set_iterator_to_next (it, 1);
10188 }
10189
10190 out:;
10191
10192 row->displays_text_p = row->used[TEXT_AREA] != 0;
10193
10194 /* Use default face for the border below the tool bar.
10195
10196 FIXME: When auto-resize-tool-bars is grow-only, there is
10197 no additional border below the possibly empty tool-bar lines.
10198 So to make the extra empty lines look "normal", we have to
10199 use the tool-bar face for the border too. */
10200 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10201 it->face_id = DEFAULT_FACE_ID;
10202
10203 extend_face_to_end_of_line (it);
10204 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10205 last->right_box_line_p = 1;
10206 if (last == row->glyphs[TEXT_AREA])
10207 last->left_box_line_p = 1;
10208
10209 /* Make line the desired height and center it vertically. */
10210 if ((height -= it->max_ascent + it->max_descent) > 0)
10211 {
10212 /* Don't add more than one line height. */
10213 height %= FRAME_LINE_HEIGHT (it->f);
10214 it->max_ascent += height / 2;
10215 it->max_descent += (height + 1) / 2;
10216 }
10217
10218 compute_line_metrics (it);
10219
10220 /* If line is empty, make it occupy the rest of the tool-bar. */
10221 if (!row->displays_text_p)
10222 {
10223 row->height = row->phys_height = it->last_visible_y - row->y;
10224 row->visible_height = row->height;
10225 row->ascent = row->phys_ascent = 0;
10226 row->extra_line_spacing = 0;
10227 }
10228
10229 row->full_width_p = 1;
10230 row->continued_p = 0;
10231 row->truncated_on_left_p = 0;
10232 row->truncated_on_right_p = 0;
10233
10234 it->current_x = it->hpos = 0;
10235 it->current_y += row->height;
10236 ++it->vpos;
10237 ++it->glyph_row;
10238 }
10239
10240
10241 /* Max tool-bar height. */
10242
10243 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10244 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10245
10246 /* Value is the number of screen lines needed to make all tool-bar
10247 items of frame F visible. The number of actual rows needed is
10248 returned in *N_ROWS if non-NULL. */
10249
10250 static int
10251 tool_bar_lines_needed (f, n_rows)
10252 struct frame *f;
10253 int *n_rows;
10254 {
10255 struct window *w = XWINDOW (f->tool_bar_window);
10256 struct it it;
10257 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10258 the desired matrix, so use (unused) mode-line row as temporary row to
10259 avoid destroying the first tool-bar row. */
10260 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10261
10262 /* Initialize an iterator for iteration over
10263 F->desired_tool_bar_string in the tool-bar window of frame F. */
10264 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10265 it.first_visible_x = 0;
10266 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10267 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10268
10269 while (!ITERATOR_AT_END_P (&it))
10270 {
10271 clear_glyph_row (temp_row);
10272 it.glyph_row = temp_row;
10273 display_tool_bar_line (&it, -1);
10274 }
10275 clear_glyph_row (temp_row);
10276
10277 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10278 if (n_rows)
10279 *n_rows = it.vpos > 0 ? it.vpos : -1;
10280
10281 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10282 }
10283
10284
10285 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10286 0, 1, 0,
10287 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10288 (frame)
10289 Lisp_Object frame;
10290 {
10291 struct frame *f;
10292 struct window *w;
10293 int nlines = 0;
10294
10295 if (NILP (frame))
10296 frame = selected_frame;
10297 else
10298 CHECK_FRAME (frame);
10299 f = XFRAME (frame);
10300
10301 if (WINDOWP (f->tool_bar_window)
10302 || (w = XWINDOW (f->tool_bar_window),
10303 WINDOW_TOTAL_LINES (w) > 0))
10304 {
10305 update_tool_bar (f, 1);
10306 if (f->n_tool_bar_items)
10307 {
10308 build_desired_tool_bar_string (f);
10309 nlines = tool_bar_lines_needed (f, NULL);
10310 }
10311 }
10312
10313 return make_number (nlines);
10314 }
10315
10316
10317 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10318 height should be changed. */
10319
10320 static int
10321 redisplay_tool_bar (f)
10322 struct frame *f;
10323 {
10324 struct window *w;
10325 struct it it;
10326 struct glyph_row *row;
10327
10328 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
10329 if (FRAME_EXTERNAL_TOOL_BAR (f))
10330 update_frame_tool_bar (f);
10331 return 0;
10332 #endif
10333
10334 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10335 do anything. This means you must start with tool-bar-lines
10336 non-zero to get the auto-sizing effect. Or in other words, you
10337 can turn off tool-bars by specifying tool-bar-lines zero. */
10338 if (!WINDOWP (f->tool_bar_window)
10339 || (w = XWINDOW (f->tool_bar_window),
10340 WINDOW_TOTAL_LINES (w) == 0))
10341 return 0;
10342
10343 /* Set up an iterator for the tool-bar window. */
10344 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10345 it.first_visible_x = 0;
10346 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10347 row = it.glyph_row;
10348
10349 /* Build a string that represents the contents of the tool-bar. */
10350 build_desired_tool_bar_string (f);
10351 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10352
10353 if (f->n_tool_bar_rows == 0)
10354 {
10355 int nlines;
10356
10357 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10358 nlines != WINDOW_TOTAL_LINES (w)))
10359 {
10360 extern Lisp_Object Qtool_bar_lines;
10361 Lisp_Object frame;
10362 int old_height = WINDOW_TOTAL_LINES (w);
10363
10364 XSETFRAME (frame, f);
10365 Fmodify_frame_parameters (frame,
10366 Fcons (Fcons (Qtool_bar_lines,
10367 make_number (nlines)),
10368 Qnil));
10369 if (WINDOW_TOTAL_LINES (w) != old_height)
10370 {
10371 clear_glyph_matrix (w->desired_matrix);
10372 fonts_changed_p = 1;
10373 return 1;
10374 }
10375 }
10376 }
10377
10378 /* Display as many lines as needed to display all tool-bar items. */
10379
10380 if (f->n_tool_bar_rows > 0)
10381 {
10382 int border, rows, height, extra;
10383
10384 if (INTEGERP (Vtool_bar_border))
10385 border = XINT (Vtool_bar_border);
10386 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10387 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10388 else if (EQ (Vtool_bar_border, Qborder_width))
10389 border = f->border_width;
10390 else
10391 border = 0;
10392 if (border < 0)
10393 border = 0;
10394
10395 rows = f->n_tool_bar_rows;
10396 height = max (1, (it.last_visible_y - border) / rows);
10397 extra = it.last_visible_y - border - height * rows;
10398
10399 while (it.current_y < it.last_visible_y)
10400 {
10401 int h = 0;
10402 if (extra > 0 && rows-- > 0)
10403 {
10404 h = (extra + rows - 1) / rows;
10405 extra -= h;
10406 }
10407 display_tool_bar_line (&it, height + h);
10408 }
10409 }
10410 else
10411 {
10412 while (it.current_y < it.last_visible_y)
10413 display_tool_bar_line (&it, 0);
10414 }
10415
10416 /* It doesn't make much sense to try scrolling in the tool-bar
10417 window, so don't do it. */
10418 w->desired_matrix->no_scrolling_p = 1;
10419 w->must_be_updated_p = 1;
10420
10421 if (!NILP (Vauto_resize_tool_bars))
10422 {
10423 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10424 int change_height_p = 0;
10425
10426 /* If we couldn't display everything, change the tool-bar's
10427 height if there is room for more. */
10428 if (IT_STRING_CHARPOS (it) < it.end_charpos
10429 && it.current_y < max_tool_bar_height)
10430 change_height_p = 1;
10431
10432 row = it.glyph_row - 1;
10433
10434 /* If there are blank lines at the end, except for a partially
10435 visible blank line at the end that is smaller than
10436 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10437 if (!row->displays_text_p
10438 && row->height >= FRAME_LINE_HEIGHT (f))
10439 change_height_p = 1;
10440
10441 /* If row displays tool-bar items, but is partially visible,
10442 change the tool-bar's height. */
10443 if (row->displays_text_p
10444 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10445 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10446 change_height_p = 1;
10447
10448 /* Resize windows as needed by changing the `tool-bar-lines'
10449 frame parameter. */
10450 if (change_height_p)
10451 {
10452 extern Lisp_Object Qtool_bar_lines;
10453 Lisp_Object frame;
10454 int old_height = WINDOW_TOTAL_LINES (w);
10455 int nrows;
10456 int nlines = tool_bar_lines_needed (f, &nrows);
10457
10458 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10459 && !f->minimize_tool_bar_window_p)
10460 ? (nlines > old_height)
10461 : (nlines != old_height));
10462 f->minimize_tool_bar_window_p = 0;
10463
10464 if (change_height_p)
10465 {
10466 XSETFRAME (frame, f);
10467 Fmodify_frame_parameters (frame,
10468 Fcons (Fcons (Qtool_bar_lines,
10469 make_number (nlines)),
10470 Qnil));
10471 if (WINDOW_TOTAL_LINES (w) != old_height)
10472 {
10473 clear_glyph_matrix (w->desired_matrix);
10474 f->n_tool_bar_rows = nrows;
10475 fonts_changed_p = 1;
10476 return 1;
10477 }
10478 }
10479 }
10480 }
10481
10482 f->minimize_tool_bar_window_p = 0;
10483 return 0;
10484 }
10485
10486
10487 /* Get information about the tool-bar item which is displayed in GLYPH
10488 on frame F. Return in *PROP_IDX the index where tool-bar item
10489 properties start in F->tool_bar_items. Value is zero if
10490 GLYPH doesn't display a tool-bar item. */
10491
10492 static int
10493 tool_bar_item_info (f, glyph, prop_idx)
10494 struct frame *f;
10495 struct glyph *glyph;
10496 int *prop_idx;
10497 {
10498 Lisp_Object prop;
10499 int success_p;
10500 int charpos;
10501
10502 /* This function can be called asynchronously, which means we must
10503 exclude any possibility that Fget_text_property signals an
10504 error. */
10505 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10506 charpos = max (0, charpos);
10507
10508 /* Get the text property `menu-item' at pos. The value of that
10509 property is the start index of this item's properties in
10510 F->tool_bar_items. */
10511 prop = Fget_text_property (make_number (charpos),
10512 Qmenu_item, f->current_tool_bar_string);
10513 if (INTEGERP (prop))
10514 {
10515 *prop_idx = XINT (prop);
10516 success_p = 1;
10517 }
10518 else
10519 success_p = 0;
10520
10521 return success_p;
10522 }
10523
10524 \f
10525 /* Get information about the tool-bar item at position X/Y on frame F.
10526 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10527 the current matrix of the tool-bar window of F, or NULL if not
10528 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10529 item in F->tool_bar_items. Value is
10530
10531 -1 if X/Y is not on a tool-bar item
10532 0 if X/Y is on the same item that was highlighted before.
10533 1 otherwise. */
10534
10535 static int
10536 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10537 struct frame *f;
10538 int x, y;
10539 struct glyph **glyph;
10540 int *hpos, *vpos, *prop_idx;
10541 {
10542 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10543 struct window *w = XWINDOW (f->tool_bar_window);
10544 int area;
10545
10546 /* Find the glyph under X/Y. */
10547 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10548 if (*glyph == NULL)
10549 return -1;
10550
10551 /* Get the start of this tool-bar item's properties in
10552 f->tool_bar_items. */
10553 if (!tool_bar_item_info (f, *glyph, prop_idx))
10554 return -1;
10555
10556 /* Is mouse on the highlighted item? */
10557 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10558 && *vpos >= dpyinfo->mouse_face_beg_row
10559 && *vpos <= dpyinfo->mouse_face_end_row
10560 && (*vpos > dpyinfo->mouse_face_beg_row
10561 || *hpos >= dpyinfo->mouse_face_beg_col)
10562 && (*vpos < dpyinfo->mouse_face_end_row
10563 || *hpos < dpyinfo->mouse_face_end_col
10564 || dpyinfo->mouse_face_past_end))
10565 return 0;
10566
10567 return 1;
10568 }
10569
10570
10571 /* EXPORT:
10572 Handle mouse button event on the tool-bar of frame F, at
10573 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10574 0 for button release. MODIFIERS is event modifiers for button
10575 release. */
10576
10577 void
10578 handle_tool_bar_click (f, x, y, down_p, modifiers)
10579 struct frame *f;
10580 int x, y, down_p;
10581 unsigned int modifiers;
10582 {
10583 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10584 struct window *w = XWINDOW (f->tool_bar_window);
10585 int hpos, vpos, prop_idx;
10586 struct glyph *glyph;
10587 Lisp_Object enabled_p;
10588
10589 /* If not on the highlighted tool-bar item, return. */
10590 frame_to_window_pixel_xy (w, &x, &y);
10591 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10592 return;
10593
10594 /* If item is disabled, do nothing. */
10595 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10596 if (NILP (enabled_p))
10597 return;
10598
10599 if (down_p)
10600 {
10601 /* Show item in pressed state. */
10602 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10603 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10604 last_tool_bar_item = prop_idx;
10605 }
10606 else
10607 {
10608 Lisp_Object key, frame;
10609 struct input_event event;
10610 EVENT_INIT (event);
10611
10612 /* Show item in released state. */
10613 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10614 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10615
10616 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10617
10618 XSETFRAME (frame, f);
10619 event.kind = TOOL_BAR_EVENT;
10620 event.frame_or_window = frame;
10621 event.arg = frame;
10622 kbd_buffer_store_event (&event);
10623
10624 event.kind = TOOL_BAR_EVENT;
10625 event.frame_or_window = frame;
10626 event.arg = key;
10627 event.modifiers = modifiers;
10628 kbd_buffer_store_event (&event);
10629 last_tool_bar_item = -1;
10630 }
10631 }
10632
10633
10634 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10635 tool-bar window-relative coordinates X/Y. Called from
10636 note_mouse_highlight. */
10637
10638 static void
10639 note_tool_bar_highlight (f, x, y)
10640 struct frame *f;
10641 int x, y;
10642 {
10643 Lisp_Object window = f->tool_bar_window;
10644 struct window *w = XWINDOW (window);
10645 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10646 int hpos, vpos;
10647 struct glyph *glyph;
10648 struct glyph_row *row;
10649 int i;
10650 Lisp_Object enabled_p;
10651 int prop_idx;
10652 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10653 int mouse_down_p, rc;
10654
10655 /* Function note_mouse_highlight is called with negative x(y
10656 values when mouse moves outside of the frame. */
10657 if (x <= 0 || y <= 0)
10658 {
10659 clear_mouse_face (dpyinfo);
10660 return;
10661 }
10662
10663 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10664 if (rc < 0)
10665 {
10666 /* Not on tool-bar item. */
10667 clear_mouse_face (dpyinfo);
10668 return;
10669 }
10670 else if (rc == 0)
10671 /* On same tool-bar item as before. */
10672 goto set_help_echo;
10673
10674 clear_mouse_face (dpyinfo);
10675
10676 /* Mouse is down, but on different tool-bar item? */
10677 mouse_down_p = (dpyinfo->grabbed
10678 && f == last_mouse_frame
10679 && FRAME_LIVE_P (f));
10680 if (mouse_down_p
10681 && last_tool_bar_item != prop_idx)
10682 return;
10683
10684 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10685 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10686
10687 /* If tool-bar item is not enabled, don't highlight it. */
10688 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10689 if (!NILP (enabled_p))
10690 {
10691 /* Compute the x-position of the glyph. In front and past the
10692 image is a space. We include this in the highlighted area. */
10693 row = MATRIX_ROW (w->current_matrix, vpos);
10694 for (i = x = 0; i < hpos; ++i)
10695 x += row->glyphs[TEXT_AREA][i].pixel_width;
10696
10697 /* Record this as the current active region. */
10698 dpyinfo->mouse_face_beg_col = hpos;
10699 dpyinfo->mouse_face_beg_row = vpos;
10700 dpyinfo->mouse_face_beg_x = x;
10701 dpyinfo->mouse_face_beg_y = row->y;
10702 dpyinfo->mouse_face_past_end = 0;
10703
10704 dpyinfo->mouse_face_end_col = hpos + 1;
10705 dpyinfo->mouse_face_end_row = vpos;
10706 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10707 dpyinfo->mouse_face_end_y = row->y;
10708 dpyinfo->mouse_face_window = window;
10709 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10710
10711 /* Display it as active. */
10712 show_mouse_face (dpyinfo, draw);
10713 dpyinfo->mouse_face_image_state = draw;
10714 }
10715
10716 set_help_echo:
10717
10718 /* Set help_echo_string to a help string to display for this tool-bar item.
10719 XTread_socket does the rest. */
10720 help_echo_object = help_echo_window = Qnil;
10721 help_echo_pos = -1;
10722 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10723 if (NILP (help_echo_string))
10724 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10725 }
10726
10727 #endif /* HAVE_WINDOW_SYSTEM */
10728
10729
10730 \f
10731 /************************************************************************
10732 Horizontal scrolling
10733 ************************************************************************/
10734
10735 static int hscroll_window_tree P_ ((Lisp_Object));
10736 static int hscroll_windows P_ ((Lisp_Object));
10737
10738 /* For all leaf windows in the window tree rooted at WINDOW, set their
10739 hscroll value so that PT is (i) visible in the window, and (ii) so
10740 that it is not within a certain margin at the window's left and
10741 right border. Value is non-zero if any window's hscroll has been
10742 changed. */
10743
10744 static int
10745 hscroll_window_tree (window)
10746 Lisp_Object window;
10747 {
10748 int hscrolled_p = 0;
10749 int hscroll_relative_p = FLOATP (Vhscroll_step);
10750 int hscroll_step_abs = 0;
10751 double hscroll_step_rel = 0;
10752
10753 if (hscroll_relative_p)
10754 {
10755 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10756 if (hscroll_step_rel < 0)
10757 {
10758 hscroll_relative_p = 0;
10759 hscroll_step_abs = 0;
10760 }
10761 }
10762 else if (INTEGERP (Vhscroll_step))
10763 {
10764 hscroll_step_abs = XINT (Vhscroll_step);
10765 if (hscroll_step_abs < 0)
10766 hscroll_step_abs = 0;
10767 }
10768 else
10769 hscroll_step_abs = 0;
10770
10771 while (WINDOWP (window))
10772 {
10773 struct window *w = XWINDOW (window);
10774
10775 if (WINDOWP (w->hchild))
10776 hscrolled_p |= hscroll_window_tree (w->hchild);
10777 else if (WINDOWP (w->vchild))
10778 hscrolled_p |= hscroll_window_tree (w->vchild);
10779 else if (w->cursor.vpos >= 0)
10780 {
10781 int h_margin;
10782 int text_area_width;
10783 struct glyph_row *current_cursor_row
10784 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10785 struct glyph_row *desired_cursor_row
10786 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10787 struct glyph_row *cursor_row
10788 = (desired_cursor_row->enabled_p
10789 ? desired_cursor_row
10790 : current_cursor_row);
10791
10792 text_area_width = window_box_width (w, TEXT_AREA);
10793
10794 /* Scroll when cursor is inside this scroll margin. */
10795 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10796
10797 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10798 && ((XFASTINT (w->hscroll)
10799 && w->cursor.x <= h_margin)
10800 || (cursor_row->enabled_p
10801 && cursor_row->truncated_on_right_p
10802 && (w->cursor.x >= text_area_width - h_margin))))
10803 {
10804 struct it it;
10805 int hscroll;
10806 struct buffer *saved_current_buffer;
10807 int pt;
10808 int wanted_x;
10809
10810 /* Find point in a display of infinite width. */
10811 saved_current_buffer = current_buffer;
10812 current_buffer = XBUFFER (w->buffer);
10813
10814 if (w == XWINDOW (selected_window))
10815 pt = BUF_PT (current_buffer);
10816 else
10817 {
10818 pt = marker_position (w->pointm);
10819 pt = max (BEGV, pt);
10820 pt = min (ZV, pt);
10821 }
10822
10823 /* Move iterator to pt starting at cursor_row->start in
10824 a line with infinite width. */
10825 init_to_row_start (&it, w, cursor_row);
10826 it.last_visible_x = INFINITY;
10827 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10828 current_buffer = saved_current_buffer;
10829
10830 /* Position cursor in window. */
10831 if (!hscroll_relative_p && hscroll_step_abs == 0)
10832 hscroll = max (0, (it.current_x
10833 - (ITERATOR_AT_END_OF_LINE_P (&it)
10834 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10835 : (text_area_width / 2))))
10836 / FRAME_COLUMN_WIDTH (it.f);
10837 else if (w->cursor.x >= text_area_width - h_margin)
10838 {
10839 if (hscroll_relative_p)
10840 wanted_x = text_area_width * (1 - hscroll_step_rel)
10841 - h_margin;
10842 else
10843 wanted_x = text_area_width
10844 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10845 - h_margin;
10846 hscroll
10847 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10848 }
10849 else
10850 {
10851 if (hscroll_relative_p)
10852 wanted_x = text_area_width * hscroll_step_rel
10853 + h_margin;
10854 else
10855 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10856 + h_margin;
10857 hscroll
10858 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10859 }
10860 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10861
10862 /* Don't call Fset_window_hscroll if value hasn't
10863 changed because it will prevent redisplay
10864 optimizations. */
10865 if (XFASTINT (w->hscroll) != hscroll)
10866 {
10867 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10868 w->hscroll = make_number (hscroll);
10869 hscrolled_p = 1;
10870 }
10871 }
10872 }
10873
10874 window = w->next;
10875 }
10876
10877 /* Value is non-zero if hscroll of any leaf window has been changed. */
10878 return hscrolled_p;
10879 }
10880
10881
10882 /* Set hscroll so that cursor is visible and not inside horizontal
10883 scroll margins for all windows in the tree rooted at WINDOW. See
10884 also hscroll_window_tree above. Value is non-zero if any window's
10885 hscroll has been changed. If it has, desired matrices on the frame
10886 of WINDOW are cleared. */
10887
10888 static int
10889 hscroll_windows (window)
10890 Lisp_Object window;
10891 {
10892 int hscrolled_p = hscroll_window_tree (window);
10893 if (hscrolled_p)
10894 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10895 return hscrolled_p;
10896 }
10897
10898
10899 \f
10900 /************************************************************************
10901 Redisplay
10902 ************************************************************************/
10903
10904 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10905 to a non-zero value. This is sometimes handy to have in a debugger
10906 session. */
10907
10908 #if GLYPH_DEBUG
10909
10910 /* First and last unchanged row for try_window_id. */
10911
10912 int debug_first_unchanged_at_end_vpos;
10913 int debug_last_unchanged_at_beg_vpos;
10914
10915 /* Delta vpos and y. */
10916
10917 int debug_dvpos, debug_dy;
10918
10919 /* Delta in characters and bytes for try_window_id. */
10920
10921 int debug_delta, debug_delta_bytes;
10922
10923 /* Values of window_end_pos and window_end_vpos at the end of
10924 try_window_id. */
10925
10926 EMACS_INT debug_end_pos, debug_end_vpos;
10927
10928 /* Append a string to W->desired_matrix->method. FMT is a printf
10929 format string. A1...A9 are a supplement for a variable-length
10930 argument list. If trace_redisplay_p is non-zero also printf the
10931 resulting string to stderr. */
10932
10933 static void
10934 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10935 struct window *w;
10936 char *fmt;
10937 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10938 {
10939 char buffer[512];
10940 char *method = w->desired_matrix->method;
10941 int len = strlen (method);
10942 int size = sizeof w->desired_matrix->method;
10943 int remaining = size - len - 1;
10944
10945 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10946 if (len && remaining)
10947 {
10948 method[len] = '|';
10949 --remaining, ++len;
10950 }
10951
10952 strncpy (method + len, buffer, remaining);
10953
10954 if (trace_redisplay_p)
10955 fprintf (stderr, "%p (%s): %s\n",
10956 w,
10957 ((BUFFERP (w->buffer)
10958 && STRINGP (XBUFFER (w->buffer)->name))
10959 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10960 : "no buffer"),
10961 buffer);
10962 }
10963
10964 #endif /* GLYPH_DEBUG */
10965
10966
10967 /* Value is non-zero if all changes in window W, which displays
10968 current_buffer, are in the text between START and END. START is a
10969 buffer position, END is given as a distance from Z. Used in
10970 redisplay_internal for display optimization. */
10971
10972 static INLINE int
10973 text_outside_line_unchanged_p (w, start, end)
10974 struct window *w;
10975 int start, end;
10976 {
10977 int unchanged_p = 1;
10978
10979 /* If text or overlays have changed, see where. */
10980 if (XFASTINT (w->last_modified) < MODIFF
10981 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10982 {
10983 /* Gap in the line? */
10984 if (GPT < start || Z - GPT < end)
10985 unchanged_p = 0;
10986
10987 /* Changes start in front of the line, or end after it? */
10988 if (unchanged_p
10989 && (BEG_UNCHANGED < start - 1
10990 || END_UNCHANGED < end))
10991 unchanged_p = 0;
10992
10993 /* If selective display, can't optimize if changes start at the
10994 beginning of the line. */
10995 if (unchanged_p
10996 && INTEGERP (current_buffer->selective_display)
10997 && XINT (current_buffer->selective_display) > 0
10998 && (BEG_UNCHANGED < start || GPT <= start))
10999 unchanged_p = 0;
11000
11001 /* If there are overlays at the start or end of the line, these
11002 may have overlay strings with newlines in them. A change at
11003 START, for instance, may actually concern the display of such
11004 overlay strings as well, and they are displayed on different
11005 lines. So, quickly rule out this case. (For the future, it
11006 might be desirable to implement something more telling than
11007 just BEG/END_UNCHANGED.) */
11008 if (unchanged_p)
11009 {
11010 if (BEG + BEG_UNCHANGED == start
11011 && overlay_touches_p (start))
11012 unchanged_p = 0;
11013 if (END_UNCHANGED == end
11014 && overlay_touches_p (Z - end))
11015 unchanged_p = 0;
11016 }
11017 }
11018
11019 return unchanged_p;
11020 }
11021
11022
11023 /* Do a frame update, taking possible shortcuts into account. This is
11024 the main external entry point for redisplay.
11025
11026 If the last redisplay displayed an echo area message and that message
11027 is no longer requested, we clear the echo area or bring back the
11028 mini-buffer if that is in use. */
11029
11030 void
11031 redisplay ()
11032 {
11033 redisplay_internal (0);
11034 }
11035
11036
11037 static Lisp_Object
11038 overlay_arrow_string_or_property (var)
11039 Lisp_Object var;
11040 {
11041 Lisp_Object val;
11042
11043 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11044 return val;
11045
11046 return Voverlay_arrow_string;
11047 }
11048
11049 /* Return 1 if there are any overlay-arrows in current_buffer. */
11050 static int
11051 overlay_arrow_in_current_buffer_p ()
11052 {
11053 Lisp_Object vlist;
11054
11055 for (vlist = Voverlay_arrow_variable_list;
11056 CONSP (vlist);
11057 vlist = XCDR (vlist))
11058 {
11059 Lisp_Object var = XCAR (vlist);
11060 Lisp_Object val;
11061
11062 if (!SYMBOLP (var))
11063 continue;
11064 val = find_symbol_value (var);
11065 if (MARKERP (val)
11066 && current_buffer == XMARKER (val)->buffer)
11067 return 1;
11068 }
11069 return 0;
11070 }
11071
11072
11073 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11074 has changed. */
11075
11076 static int
11077 overlay_arrows_changed_p ()
11078 {
11079 Lisp_Object vlist;
11080
11081 for (vlist = Voverlay_arrow_variable_list;
11082 CONSP (vlist);
11083 vlist = XCDR (vlist))
11084 {
11085 Lisp_Object var = XCAR (vlist);
11086 Lisp_Object val, pstr;
11087
11088 if (!SYMBOLP (var))
11089 continue;
11090 val = find_symbol_value (var);
11091 if (!MARKERP (val))
11092 continue;
11093 if (! EQ (COERCE_MARKER (val),
11094 Fget (var, Qlast_arrow_position))
11095 || ! (pstr = overlay_arrow_string_or_property (var),
11096 EQ (pstr, Fget (var, Qlast_arrow_string))))
11097 return 1;
11098 }
11099 return 0;
11100 }
11101
11102 /* Mark overlay arrows to be updated on next redisplay. */
11103
11104 static void
11105 update_overlay_arrows (up_to_date)
11106 int up_to_date;
11107 {
11108 Lisp_Object vlist;
11109
11110 for (vlist = Voverlay_arrow_variable_list;
11111 CONSP (vlist);
11112 vlist = XCDR (vlist))
11113 {
11114 Lisp_Object var = XCAR (vlist);
11115
11116 if (!SYMBOLP (var))
11117 continue;
11118
11119 if (up_to_date > 0)
11120 {
11121 Lisp_Object val = find_symbol_value (var);
11122 Fput (var, Qlast_arrow_position,
11123 COERCE_MARKER (val));
11124 Fput (var, Qlast_arrow_string,
11125 overlay_arrow_string_or_property (var));
11126 }
11127 else if (up_to_date < 0
11128 || !NILP (Fget (var, Qlast_arrow_position)))
11129 {
11130 Fput (var, Qlast_arrow_position, Qt);
11131 Fput (var, Qlast_arrow_string, Qt);
11132 }
11133 }
11134 }
11135
11136
11137 /* Return overlay arrow string to display at row.
11138 Return integer (bitmap number) for arrow bitmap in left fringe.
11139 Return nil if no overlay arrow. */
11140
11141 static Lisp_Object
11142 overlay_arrow_at_row (it, row)
11143 struct it *it;
11144 struct glyph_row *row;
11145 {
11146 Lisp_Object vlist;
11147
11148 for (vlist = Voverlay_arrow_variable_list;
11149 CONSP (vlist);
11150 vlist = XCDR (vlist))
11151 {
11152 Lisp_Object var = XCAR (vlist);
11153 Lisp_Object val;
11154
11155 if (!SYMBOLP (var))
11156 continue;
11157
11158 val = find_symbol_value (var);
11159
11160 if (MARKERP (val)
11161 && current_buffer == XMARKER (val)->buffer
11162 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11163 {
11164 if (FRAME_WINDOW_P (it->f)
11165 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11166 {
11167 #ifdef HAVE_WINDOW_SYSTEM
11168 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11169 {
11170 int fringe_bitmap;
11171 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11172 return make_number (fringe_bitmap);
11173 }
11174 #endif
11175 return make_number (-1); /* Use default arrow bitmap */
11176 }
11177 return overlay_arrow_string_or_property (var);
11178 }
11179 }
11180
11181 return Qnil;
11182 }
11183
11184 /* Return 1 if point moved out of or into a composition. Otherwise
11185 return 0. PREV_BUF and PREV_PT are the last point buffer and
11186 position. BUF and PT are the current point buffer and position. */
11187
11188 int
11189 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11190 struct buffer *prev_buf, *buf;
11191 int prev_pt, pt;
11192 {
11193 EMACS_INT start, end;
11194 Lisp_Object prop;
11195 Lisp_Object buffer;
11196
11197 XSETBUFFER (buffer, buf);
11198 /* Check a composition at the last point if point moved within the
11199 same buffer. */
11200 if (prev_buf == buf)
11201 {
11202 if (prev_pt == pt)
11203 /* Point didn't move. */
11204 return 0;
11205
11206 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11207 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11208 && COMPOSITION_VALID_P (start, end, prop)
11209 && start < prev_pt && end > prev_pt)
11210 /* The last point was within the composition. Return 1 iff
11211 point moved out of the composition. */
11212 return (pt <= start || pt >= end);
11213 }
11214
11215 /* Check a composition at the current point. */
11216 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11217 && find_composition (pt, -1, &start, &end, &prop, buffer)
11218 && COMPOSITION_VALID_P (start, end, prop)
11219 && start < pt && end > pt);
11220 }
11221
11222
11223 /* Reconsider the setting of B->clip_changed which is displayed
11224 in window W. */
11225
11226 static INLINE void
11227 reconsider_clip_changes (w, b)
11228 struct window *w;
11229 struct buffer *b;
11230 {
11231 if (b->clip_changed
11232 && !NILP (w->window_end_valid)
11233 && w->current_matrix->buffer == b
11234 && w->current_matrix->zv == BUF_ZV (b)
11235 && w->current_matrix->begv == BUF_BEGV (b))
11236 b->clip_changed = 0;
11237
11238 /* If display wasn't paused, and W is not a tool bar window, see if
11239 point has been moved into or out of a composition. In that case,
11240 we set b->clip_changed to 1 to force updating the screen. If
11241 b->clip_changed has already been set to 1, we can skip this
11242 check. */
11243 if (!b->clip_changed
11244 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11245 {
11246 int pt;
11247
11248 if (w == XWINDOW (selected_window))
11249 pt = BUF_PT (current_buffer);
11250 else
11251 pt = marker_position (w->pointm);
11252
11253 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11254 || pt != XINT (w->last_point))
11255 && check_point_in_composition (w->current_matrix->buffer,
11256 XINT (w->last_point),
11257 XBUFFER (w->buffer), pt))
11258 b->clip_changed = 1;
11259 }
11260 }
11261 \f
11262
11263 /* Select FRAME to forward the values of frame-local variables into C
11264 variables so that the redisplay routines can access those values
11265 directly. */
11266
11267 static void
11268 select_frame_for_redisplay (frame)
11269 Lisp_Object frame;
11270 {
11271 Lisp_Object tail, symbol, val;
11272 Lisp_Object old = selected_frame;
11273 struct Lisp_Symbol *sym;
11274
11275 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11276
11277 selected_frame = frame;
11278
11279 do
11280 {
11281 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11282 if (CONSP (XCAR (tail))
11283 && (symbol = XCAR (XCAR (tail)),
11284 SYMBOLP (symbol))
11285 && (sym = indirect_variable (XSYMBOL (symbol)),
11286 val = sym->value,
11287 (BUFFER_LOCAL_VALUEP (val)))
11288 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11289 /* Use find_symbol_value rather than Fsymbol_value
11290 to avoid an error if it is void. */
11291 find_symbol_value (symbol);
11292 } while (!EQ (frame, old) && (frame = old, 1));
11293 }
11294
11295
11296 #define STOP_POLLING \
11297 do { if (! polling_stopped_here) stop_polling (); \
11298 polling_stopped_here = 1; } while (0)
11299
11300 #define RESUME_POLLING \
11301 do { if (polling_stopped_here) start_polling (); \
11302 polling_stopped_here = 0; } while (0)
11303
11304
11305 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11306 response to any user action; therefore, we should preserve the echo
11307 area. (Actually, our caller does that job.) Perhaps in the future
11308 avoid recentering windows if it is not necessary; currently that
11309 causes some problems. */
11310
11311 static void
11312 redisplay_internal (preserve_echo_area)
11313 int preserve_echo_area;
11314 {
11315 struct window *w = XWINDOW (selected_window);
11316 struct frame *f;
11317 int pause;
11318 int must_finish = 0;
11319 struct text_pos tlbufpos, tlendpos;
11320 int number_of_visible_frames;
11321 int count, count1;
11322 struct frame *sf;
11323 int polling_stopped_here = 0;
11324 Lisp_Object old_frame = selected_frame;
11325
11326 /* Non-zero means redisplay has to consider all windows on all
11327 frames. Zero means, only selected_window is considered. */
11328 int consider_all_windows_p;
11329
11330 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11331
11332 /* No redisplay if running in batch mode or frame is not yet fully
11333 initialized, or redisplay is explicitly turned off by setting
11334 Vinhibit_redisplay. */
11335 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11336 || !NILP (Vinhibit_redisplay))
11337 return;
11338
11339 /* Don't examine these until after testing Vinhibit_redisplay.
11340 When Emacs is shutting down, perhaps because its connection to
11341 X has dropped, we should not look at them at all. */
11342 f = XFRAME (w->frame);
11343 sf = SELECTED_FRAME ();
11344
11345 if (!f->glyphs_initialized_p)
11346 return;
11347
11348 /* The flag redisplay_performed_directly_p is set by
11349 direct_output_for_insert when it already did the whole screen
11350 update necessary. */
11351 if (redisplay_performed_directly_p)
11352 {
11353 redisplay_performed_directly_p = 0;
11354 if (!hscroll_windows (selected_window))
11355 return;
11356 }
11357
11358 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11359 if (popup_activated ())
11360 return;
11361 #endif
11362
11363 /* I don't think this happens but let's be paranoid. */
11364 if (redisplaying_p)
11365 return;
11366
11367 /* Record a function that resets redisplaying_p to its old value
11368 when we leave this function. */
11369 count = SPECPDL_INDEX ();
11370 record_unwind_protect (unwind_redisplay,
11371 Fcons (make_number (redisplaying_p), selected_frame));
11372 ++redisplaying_p;
11373 specbind (Qinhibit_free_realized_faces, Qnil);
11374
11375 {
11376 Lisp_Object tail, frame;
11377
11378 FOR_EACH_FRAME (tail, frame)
11379 {
11380 struct frame *f = XFRAME (frame);
11381 f->already_hscrolled_p = 0;
11382 }
11383 }
11384
11385 retry:
11386 if (!EQ (old_frame, selected_frame)
11387 && FRAME_LIVE_P (XFRAME (old_frame)))
11388 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11389 selected_frame and selected_window to be temporarily out-of-sync so
11390 when we come back here via `goto retry', we need to resync because we
11391 may need to run Elisp code (via prepare_menu_bars). */
11392 select_frame_for_redisplay (old_frame);
11393
11394 pause = 0;
11395 reconsider_clip_changes (w, current_buffer);
11396 last_escape_glyph_frame = NULL;
11397 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11398
11399 /* If new fonts have been loaded that make a glyph matrix adjustment
11400 necessary, do it. */
11401 if (fonts_changed_p)
11402 {
11403 adjust_glyphs (NULL);
11404 ++windows_or_buffers_changed;
11405 fonts_changed_p = 0;
11406 }
11407
11408 /* If face_change_count is non-zero, init_iterator will free all
11409 realized faces, which includes the faces referenced from current
11410 matrices. So, we can't reuse current matrices in this case. */
11411 if (face_change_count)
11412 ++windows_or_buffers_changed;
11413
11414 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11415 && FRAME_TTY (sf)->previous_frame != sf)
11416 {
11417 /* Since frames on a single ASCII terminal share the same
11418 display area, displaying a different frame means redisplay
11419 the whole thing. */
11420 windows_or_buffers_changed++;
11421 SET_FRAME_GARBAGED (sf);
11422 #ifndef DOS_NT
11423 set_tty_color_mode (FRAME_TTY (sf), sf);
11424 #endif
11425 FRAME_TTY (sf)->previous_frame = sf;
11426 }
11427
11428 /* Set the visible flags for all frames. Do this before checking
11429 for resized or garbaged frames; they want to know if their frames
11430 are visible. See the comment in frame.h for
11431 FRAME_SAMPLE_VISIBILITY. */
11432 {
11433 Lisp_Object tail, frame;
11434
11435 number_of_visible_frames = 0;
11436
11437 FOR_EACH_FRAME (tail, frame)
11438 {
11439 struct frame *f = XFRAME (frame);
11440
11441 FRAME_SAMPLE_VISIBILITY (f);
11442 if (FRAME_VISIBLE_P (f))
11443 ++number_of_visible_frames;
11444 clear_desired_matrices (f);
11445 }
11446 }
11447
11448
11449 /* Notice any pending interrupt request to change frame size. */
11450 do_pending_window_change (1);
11451
11452 /* Clear frames marked as garbaged. */
11453 if (frame_garbaged)
11454 clear_garbaged_frames ();
11455
11456 /* Build menubar and tool-bar items. */
11457 if (NILP (Vmemory_full))
11458 prepare_menu_bars ();
11459
11460 if (windows_or_buffers_changed)
11461 update_mode_lines++;
11462
11463 /* Detect case that we need to write or remove a star in the mode line. */
11464 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11465 {
11466 w->update_mode_line = Qt;
11467 if (buffer_shared > 1)
11468 update_mode_lines++;
11469 }
11470
11471 /* Avoid invocation of point motion hooks by `current_column' below. */
11472 count1 = SPECPDL_INDEX ();
11473 specbind (Qinhibit_point_motion_hooks, Qt);
11474
11475 /* If %c is in the mode line, update it if needed. */
11476 if (!NILP (w->column_number_displayed)
11477 /* This alternative quickly identifies a common case
11478 where no change is needed. */
11479 && !(PT == XFASTINT (w->last_point)
11480 && XFASTINT (w->last_modified) >= MODIFF
11481 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11482 && (XFASTINT (w->column_number_displayed)
11483 != (int) current_column ())) /* iftc */
11484 w->update_mode_line = Qt;
11485
11486 unbind_to (count1, Qnil);
11487
11488 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11489
11490 /* The variable buffer_shared is set in redisplay_window and
11491 indicates that we redisplay a buffer in different windows. See
11492 there. */
11493 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11494 || cursor_type_changed);
11495
11496 /* If specs for an arrow have changed, do thorough redisplay
11497 to ensure we remove any arrow that should no longer exist. */
11498 if (overlay_arrows_changed_p ())
11499 consider_all_windows_p = windows_or_buffers_changed = 1;
11500
11501 /* Normally the message* functions will have already displayed and
11502 updated the echo area, but the frame may have been trashed, or
11503 the update may have been preempted, so display the echo area
11504 again here. Checking message_cleared_p captures the case that
11505 the echo area should be cleared. */
11506 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11507 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11508 || (message_cleared_p
11509 && minibuf_level == 0
11510 /* If the mini-window is currently selected, this means the
11511 echo-area doesn't show through. */
11512 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11513 {
11514 int window_height_changed_p = echo_area_display (0);
11515 must_finish = 1;
11516
11517 /* If we don't display the current message, don't clear the
11518 message_cleared_p flag, because, if we did, we wouldn't clear
11519 the echo area in the next redisplay which doesn't preserve
11520 the echo area. */
11521 if (!display_last_displayed_message_p)
11522 message_cleared_p = 0;
11523
11524 if (fonts_changed_p)
11525 goto retry;
11526 else if (window_height_changed_p)
11527 {
11528 consider_all_windows_p = 1;
11529 ++update_mode_lines;
11530 ++windows_or_buffers_changed;
11531
11532 /* If window configuration was changed, frames may have been
11533 marked garbaged. Clear them or we will experience
11534 surprises wrt scrolling. */
11535 if (frame_garbaged)
11536 clear_garbaged_frames ();
11537 }
11538 }
11539 else if (EQ (selected_window, minibuf_window)
11540 && (current_buffer->clip_changed
11541 || XFASTINT (w->last_modified) < MODIFF
11542 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11543 && resize_mini_window (w, 0))
11544 {
11545 /* Resized active mini-window to fit the size of what it is
11546 showing if its contents might have changed. */
11547 must_finish = 1;
11548 /* FIXME: this causes all frames to be updated, which seems unnecessary
11549 since only the current frame needs to be considered. This function needs
11550 to be rewritten with two variables, consider_all_windows and
11551 consider_all_frames. */
11552 consider_all_windows_p = 1;
11553 ++windows_or_buffers_changed;
11554 ++update_mode_lines;
11555
11556 /* If window configuration was changed, frames may have been
11557 marked garbaged. Clear them or we will experience
11558 surprises wrt scrolling. */
11559 if (frame_garbaged)
11560 clear_garbaged_frames ();
11561 }
11562
11563
11564 /* If showing the region, and mark has changed, we must redisplay
11565 the whole window. The assignment to this_line_start_pos prevents
11566 the optimization directly below this if-statement. */
11567 if (((!NILP (Vtransient_mark_mode)
11568 && !NILP (XBUFFER (w->buffer)->mark_active))
11569 != !NILP (w->region_showing))
11570 || (!NILP (w->region_showing)
11571 && !EQ (w->region_showing,
11572 Fmarker_position (XBUFFER (w->buffer)->mark))))
11573 CHARPOS (this_line_start_pos) = 0;
11574
11575 /* Optimize the case that only the line containing the cursor in the
11576 selected window has changed. Variables starting with this_ are
11577 set in display_line and record information about the line
11578 containing the cursor. */
11579 tlbufpos = this_line_start_pos;
11580 tlendpos = this_line_end_pos;
11581 if (!consider_all_windows_p
11582 && CHARPOS (tlbufpos) > 0
11583 && NILP (w->update_mode_line)
11584 && !current_buffer->clip_changed
11585 && !current_buffer->prevent_redisplay_optimizations_p
11586 && FRAME_VISIBLE_P (XFRAME (w->frame))
11587 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11588 /* Make sure recorded data applies to current buffer, etc. */
11589 && this_line_buffer == current_buffer
11590 && current_buffer == XBUFFER (w->buffer)
11591 && NILP (w->force_start)
11592 && NILP (w->optional_new_start)
11593 /* Point must be on the line that we have info recorded about. */
11594 && PT >= CHARPOS (tlbufpos)
11595 && PT <= Z - CHARPOS (tlendpos)
11596 /* All text outside that line, including its final newline,
11597 must be unchanged */
11598 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11599 CHARPOS (tlendpos)))
11600 {
11601 if (CHARPOS (tlbufpos) > BEGV
11602 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11603 && (CHARPOS (tlbufpos) == ZV
11604 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11605 /* Former continuation line has disappeared by becoming empty */
11606 goto cancel;
11607 else if (XFASTINT (w->last_modified) < MODIFF
11608 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11609 || MINI_WINDOW_P (w))
11610 {
11611 /* We have to handle the case of continuation around a
11612 wide-column character (See the comment in indent.c around
11613 line 885).
11614
11615 For instance, in the following case:
11616
11617 -------- Insert --------
11618 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11619 J_I_ ==> J_I_ `^^' are cursors.
11620 ^^ ^^
11621 -------- --------
11622
11623 As we have to redraw the line above, we should goto cancel. */
11624
11625 struct it it;
11626 int line_height_before = this_line_pixel_height;
11627
11628 /* Note that start_display will handle the case that the
11629 line starting at tlbufpos is a continuation lines. */
11630 start_display (&it, w, tlbufpos);
11631
11632 /* Implementation note: It this still necessary? */
11633 if (it.current_x != this_line_start_x)
11634 goto cancel;
11635
11636 TRACE ((stderr, "trying display optimization 1\n"));
11637 w->cursor.vpos = -1;
11638 overlay_arrow_seen = 0;
11639 it.vpos = this_line_vpos;
11640 it.current_y = this_line_y;
11641 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11642 display_line (&it);
11643
11644 /* If line contains point, is not continued,
11645 and ends at same distance from eob as before, we win */
11646 if (w->cursor.vpos >= 0
11647 /* Line is not continued, otherwise this_line_start_pos
11648 would have been set to 0 in display_line. */
11649 && CHARPOS (this_line_start_pos)
11650 /* Line ends as before. */
11651 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11652 /* Line has same height as before. Otherwise other lines
11653 would have to be shifted up or down. */
11654 && this_line_pixel_height == line_height_before)
11655 {
11656 /* If this is not the window's last line, we must adjust
11657 the charstarts of the lines below. */
11658 if (it.current_y < it.last_visible_y)
11659 {
11660 struct glyph_row *row
11661 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11662 int delta, delta_bytes;
11663
11664 if (Z - CHARPOS (tlendpos) == ZV)
11665 {
11666 /* This line ends at end of (accessible part of)
11667 buffer. There is no newline to count. */
11668 delta = (Z
11669 - CHARPOS (tlendpos)
11670 - MATRIX_ROW_START_CHARPOS (row));
11671 delta_bytes = (Z_BYTE
11672 - BYTEPOS (tlendpos)
11673 - MATRIX_ROW_START_BYTEPOS (row));
11674 }
11675 else
11676 {
11677 /* This line ends in a newline. Must take
11678 account of the newline and the rest of the
11679 text that follows. */
11680 delta = (Z
11681 - CHARPOS (tlendpos)
11682 - MATRIX_ROW_START_CHARPOS (row));
11683 delta_bytes = (Z_BYTE
11684 - BYTEPOS (tlendpos)
11685 - MATRIX_ROW_START_BYTEPOS (row));
11686 }
11687
11688 increment_matrix_positions (w->current_matrix,
11689 this_line_vpos + 1,
11690 w->current_matrix->nrows,
11691 delta, delta_bytes);
11692 }
11693
11694 /* If this row displays text now but previously didn't,
11695 or vice versa, w->window_end_vpos may have to be
11696 adjusted. */
11697 if ((it.glyph_row - 1)->displays_text_p)
11698 {
11699 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11700 XSETINT (w->window_end_vpos, this_line_vpos);
11701 }
11702 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11703 && this_line_vpos > 0)
11704 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11705 w->window_end_valid = Qnil;
11706
11707 /* Update hint: No need to try to scroll in update_window. */
11708 w->desired_matrix->no_scrolling_p = 1;
11709
11710 #if GLYPH_DEBUG
11711 *w->desired_matrix->method = 0;
11712 debug_method_add (w, "optimization 1");
11713 #endif
11714 #ifdef HAVE_WINDOW_SYSTEM
11715 update_window_fringes (w, 0);
11716 #endif
11717 goto update;
11718 }
11719 else
11720 goto cancel;
11721 }
11722 else if (/* Cursor position hasn't changed. */
11723 PT == XFASTINT (w->last_point)
11724 /* Make sure the cursor was last displayed
11725 in this window. Otherwise we have to reposition it. */
11726 && 0 <= w->cursor.vpos
11727 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11728 {
11729 if (!must_finish)
11730 {
11731 do_pending_window_change (1);
11732
11733 /* We used to always goto end_of_redisplay here, but this
11734 isn't enough if we have a blinking cursor. */
11735 if (w->cursor_off_p == w->last_cursor_off_p)
11736 goto end_of_redisplay;
11737 }
11738 goto update;
11739 }
11740 /* If highlighting the region, or if the cursor is in the echo area,
11741 then we can't just move the cursor. */
11742 else if (! (!NILP (Vtransient_mark_mode)
11743 && !NILP (current_buffer->mark_active))
11744 && (EQ (selected_window, current_buffer->last_selected_window)
11745 || highlight_nonselected_windows)
11746 && NILP (w->region_showing)
11747 && NILP (Vshow_trailing_whitespace)
11748 && !cursor_in_echo_area)
11749 {
11750 struct it it;
11751 struct glyph_row *row;
11752
11753 /* Skip from tlbufpos to PT and see where it is. Note that
11754 PT may be in invisible text. If so, we will end at the
11755 next visible position. */
11756 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11757 NULL, DEFAULT_FACE_ID);
11758 it.current_x = this_line_start_x;
11759 it.current_y = this_line_y;
11760 it.vpos = this_line_vpos;
11761
11762 /* The call to move_it_to stops in front of PT, but
11763 moves over before-strings. */
11764 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11765
11766 if (it.vpos == this_line_vpos
11767 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11768 row->enabled_p))
11769 {
11770 xassert (this_line_vpos == it.vpos);
11771 xassert (this_line_y == it.current_y);
11772 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11773 #if GLYPH_DEBUG
11774 *w->desired_matrix->method = 0;
11775 debug_method_add (w, "optimization 3");
11776 #endif
11777 goto update;
11778 }
11779 else
11780 goto cancel;
11781 }
11782
11783 cancel:
11784 /* Text changed drastically or point moved off of line. */
11785 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11786 }
11787
11788 CHARPOS (this_line_start_pos) = 0;
11789 consider_all_windows_p |= buffer_shared > 1;
11790 ++clear_face_cache_count;
11791 #ifdef HAVE_WINDOW_SYSTEM
11792 ++clear_image_cache_count;
11793 #endif
11794
11795 /* Build desired matrices, and update the display. If
11796 consider_all_windows_p is non-zero, do it for all windows on all
11797 frames. Otherwise do it for selected_window, only. */
11798
11799 if (consider_all_windows_p)
11800 {
11801 Lisp_Object tail, frame;
11802
11803 FOR_EACH_FRAME (tail, frame)
11804 XFRAME (frame)->updated_p = 0;
11805
11806 /* Recompute # windows showing selected buffer. This will be
11807 incremented each time such a window is displayed. */
11808 buffer_shared = 0;
11809
11810 FOR_EACH_FRAME (tail, frame)
11811 {
11812 struct frame *f = XFRAME (frame);
11813
11814 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11815 {
11816 if (! EQ (frame, selected_frame))
11817 /* Select the frame, for the sake of frame-local
11818 variables. */
11819 select_frame_for_redisplay (frame);
11820
11821 /* Mark all the scroll bars to be removed; we'll redeem
11822 the ones we want when we redisplay their windows. */
11823 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11824 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11825
11826 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11827 redisplay_windows (FRAME_ROOT_WINDOW (f));
11828
11829 /* Any scroll bars which redisplay_windows should have
11830 nuked should now go away. */
11831 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11832 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11833
11834 /* If fonts changed, display again. */
11835 /* ??? rms: I suspect it is a mistake to jump all the way
11836 back to retry here. It should just retry this frame. */
11837 if (fonts_changed_p)
11838 goto retry;
11839
11840 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11841 {
11842 /* See if we have to hscroll. */
11843 if (!f->already_hscrolled_p)
11844 {
11845 f->already_hscrolled_p = 1;
11846 if (hscroll_windows (f->root_window))
11847 goto retry;
11848 }
11849
11850 /* Prevent various kinds of signals during display
11851 update. stdio is not robust about handling
11852 signals, which can cause an apparent I/O
11853 error. */
11854 if (interrupt_input)
11855 unrequest_sigio ();
11856 STOP_POLLING;
11857
11858 /* Update the display. */
11859 set_window_update_flags (XWINDOW (f->root_window), 1);
11860 pause |= update_frame (f, 0, 0);
11861 f->updated_p = 1;
11862 }
11863 }
11864 }
11865
11866 if (!EQ (old_frame, selected_frame)
11867 && FRAME_LIVE_P (XFRAME (old_frame)))
11868 /* We played a bit fast-and-loose above and allowed selected_frame
11869 and selected_window to be temporarily out-of-sync but let's make
11870 sure this stays contained. */
11871 select_frame_for_redisplay (old_frame);
11872 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11873
11874 if (!pause)
11875 {
11876 /* Do the mark_window_display_accurate after all windows have
11877 been redisplayed because this call resets flags in buffers
11878 which are needed for proper redisplay. */
11879 FOR_EACH_FRAME (tail, frame)
11880 {
11881 struct frame *f = XFRAME (frame);
11882 if (f->updated_p)
11883 {
11884 mark_window_display_accurate (f->root_window, 1);
11885 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11886 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11887 }
11888 }
11889 }
11890 }
11891 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11892 {
11893 Lisp_Object mini_window;
11894 struct frame *mini_frame;
11895
11896 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11897 /* Use list_of_error, not Qerror, so that
11898 we catch only errors and don't run the debugger. */
11899 internal_condition_case_1 (redisplay_window_1, selected_window,
11900 list_of_error,
11901 redisplay_window_error);
11902
11903 /* Compare desired and current matrices, perform output. */
11904
11905 update:
11906 /* If fonts changed, display again. */
11907 if (fonts_changed_p)
11908 goto retry;
11909
11910 /* Prevent various kinds of signals during display update.
11911 stdio is not robust about handling signals,
11912 which can cause an apparent I/O error. */
11913 if (interrupt_input)
11914 unrequest_sigio ();
11915 STOP_POLLING;
11916
11917 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11918 {
11919 if (hscroll_windows (selected_window))
11920 goto retry;
11921
11922 XWINDOW (selected_window)->must_be_updated_p = 1;
11923 pause = update_frame (sf, 0, 0);
11924 }
11925
11926 /* We may have called echo_area_display at the top of this
11927 function. If the echo area is on another frame, that may
11928 have put text on a frame other than the selected one, so the
11929 above call to update_frame would not have caught it. Catch
11930 it here. */
11931 mini_window = FRAME_MINIBUF_WINDOW (sf);
11932 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11933
11934 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11935 {
11936 XWINDOW (mini_window)->must_be_updated_p = 1;
11937 pause |= update_frame (mini_frame, 0, 0);
11938 if (!pause && hscroll_windows (mini_window))
11939 goto retry;
11940 }
11941 }
11942
11943 /* If display was paused because of pending input, make sure we do a
11944 thorough update the next time. */
11945 if (pause)
11946 {
11947 /* Prevent the optimization at the beginning of
11948 redisplay_internal that tries a single-line update of the
11949 line containing the cursor in the selected window. */
11950 CHARPOS (this_line_start_pos) = 0;
11951
11952 /* Let the overlay arrow be updated the next time. */
11953 update_overlay_arrows (0);
11954
11955 /* If we pause after scrolling, some rows in the current
11956 matrices of some windows are not valid. */
11957 if (!WINDOW_FULL_WIDTH_P (w)
11958 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11959 update_mode_lines = 1;
11960 }
11961 else
11962 {
11963 if (!consider_all_windows_p)
11964 {
11965 /* This has already been done above if
11966 consider_all_windows_p is set. */
11967 mark_window_display_accurate_1 (w, 1);
11968
11969 /* Say overlay arrows are up to date. */
11970 update_overlay_arrows (1);
11971
11972 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11973 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11974 }
11975
11976 update_mode_lines = 0;
11977 windows_or_buffers_changed = 0;
11978 cursor_type_changed = 0;
11979 }
11980
11981 /* Start SIGIO interrupts coming again. Having them off during the
11982 code above makes it less likely one will discard output, but not
11983 impossible, since there might be stuff in the system buffer here.
11984 But it is much hairier to try to do anything about that. */
11985 if (interrupt_input)
11986 request_sigio ();
11987 RESUME_POLLING;
11988
11989 /* If a frame has become visible which was not before, redisplay
11990 again, so that we display it. Expose events for such a frame
11991 (which it gets when becoming visible) don't call the parts of
11992 redisplay constructing glyphs, so simply exposing a frame won't
11993 display anything in this case. So, we have to display these
11994 frames here explicitly. */
11995 if (!pause)
11996 {
11997 Lisp_Object tail, frame;
11998 int new_count = 0;
11999
12000 FOR_EACH_FRAME (tail, frame)
12001 {
12002 int this_is_visible = 0;
12003
12004 if (XFRAME (frame)->visible)
12005 this_is_visible = 1;
12006 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12007 if (XFRAME (frame)->visible)
12008 this_is_visible = 1;
12009
12010 if (this_is_visible)
12011 new_count++;
12012 }
12013
12014 if (new_count != number_of_visible_frames)
12015 windows_or_buffers_changed++;
12016 }
12017
12018 /* Change frame size now if a change is pending. */
12019 do_pending_window_change (1);
12020
12021 /* If we just did a pending size change, or have additional
12022 visible frames, redisplay again. */
12023 if (windows_or_buffers_changed && !pause)
12024 goto retry;
12025
12026 /* Clear the face cache eventually. */
12027 if (consider_all_windows_p)
12028 {
12029 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12030 {
12031 clear_face_cache (0);
12032 clear_face_cache_count = 0;
12033 }
12034 #ifdef HAVE_WINDOW_SYSTEM
12035 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12036 {
12037 clear_image_caches (Qnil);
12038 clear_image_cache_count = 0;
12039 }
12040 #endif /* HAVE_WINDOW_SYSTEM */
12041 }
12042
12043 end_of_redisplay:
12044 unbind_to (count, Qnil);
12045 RESUME_POLLING;
12046 }
12047
12048
12049 /* Redisplay, but leave alone any recent echo area message unless
12050 another message has been requested in its place.
12051
12052 This is useful in situations where you need to redisplay but no
12053 user action has occurred, making it inappropriate for the message
12054 area to be cleared. See tracking_off and
12055 wait_reading_process_output for examples of these situations.
12056
12057 FROM_WHERE is an integer saying from where this function was
12058 called. This is useful for debugging. */
12059
12060 void
12061 redisplay_preserve_echo_area (from_where)
12062 int from_where;
12063 {
12064 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12065
12066 if (!NILP (echo_area_buffer[1]))
12067 {
12068 /* We have a previously displayed message, but no current
12069 message. Redisplay the previous message. */
12070 display_last_displayed_message_p = 1;
12071 redisplay_internal (1);
12072 display_last_displayed_message_p = 0;
12073 }
12074 else
12075 redisplay_internal (1);
12076
12077 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12078 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12079 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12080 }
12081
12082
12083 /* Function registered with record_unwind_protect in
12084 redisplay_internal. Reset redisplaying_p to the value it had
12085 before redisplay_internal was called, and clear
12086 prevent_freeing_realized_faces_p. It also selects the previously
12087 selected frame, unless it has been deleted (by an X connection
12088 failure during redisplay, for example). */
12089
12090 static Lisp_Object
12091 unwind_redisplay (val)
12092 Lisp_Object val;
12093 {
12094 Lisp_Object old_redisplaying_p, old_frame;
12095
12096 old_redisplaying_p = XCAR (val);
12097 redisplaying_p = XFASTINT (old_redisplaying_p);
12098 old_frame = XCDR (val);
12099 if (! EQ (old_frame, selected_frame)
12100 && FRAME_LIVE_P (XFRAME (old_frame)))
12101 select_frame_for_redisplay (old_frame);
12102 return Qnil;
12103 }
12104
12105
12106 /* Mark the display of window W as accurate or inaccurate. If
12107 ACCURATE_P is non-zero mark display of W as accurate. If
12108 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12109 redisplay_internal is called. */
12110
12111 static void
12112 mark_window_display_accurate_1 (w, accurate_p)
12113 struct window *w;
12114 int accurate_p;
12115 {
12116 if (BUFFERP (w->buffer))
12117 {
12118 struct buffer *b = XBUFFER (w->buffer);
12119
12120 w->last_modified
12121 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12122 w->last_overlay_modified
12123 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12124 w->last_had_star
12125 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12126
12127 if (accurate_p)
12128 {
12129 b->clip_changed = 0;
12130 b->prevent_redisplay_optimizations_p = 0;
12131
12132 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12133 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12134 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12135 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12136
12137 w->current_matrix->buffer = b;
12138 w->current_matrix->begv = BUF_BEGV (b);
12139 w->current_matrix->zv = BUF_ZV (b);
12140
12141 w->last_cursor = w->cursor;
12142 w->last_cursor_off_p = w->cursor_off_p;
12143
12144 if (w == XWINDOW (selected_window))
12145 w->last_point = make_number (BUF_PT (b));
12146 else
12147 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12148 }
12149 }
12150
12151 if (accurate_p)
12152 {
12153 w->window_end_valid = w->buffer;
12154 w->update_mode_line = Qnil;
12155 }
12156 }
12157
12158
12159 /* Mark the display of windows in the window tree rooted at WINDOW as
12160 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12161 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12162 be redisplayed the next time redisplay_internal is called. */
12163
12164 void
12165 mark_window_display_accurate (window, accurate_p)
12166 Lisp_Object window;
12167 int accurate_p;
12168 {
12169 struct window *w;
12170
12171 for (; !NILP (window); window = w->next)
12172 {
12173 w = XWINDOW (window);
12174 mark_window_display_accurate_1 (w, accurate_p);
12175
12176 if (!NILP (w->vchild))
12177 mark_window_display_accurate (w->vchild, accurate_p);
12178 if (!NILP (w->hchild))
12179 mark_window_display_accurate (w->hchild, accurate_p);
12180 }
12181
12182 if (accurate_p)
12183 {
12184 update_overlay_arrows (1);
12185 }
12186 else
12187 {
12188 /* Force a thorough redisplay the next time by setting
12189 last_arrow_position and last_arrow_string to t, which is
12190 unequal to any useful value of Voverlay_arrow_... */
12191 update_overlay_arrows (-1);
12192 }
12193 }
12194
12195
12196 /* Return value in display table DP (Lisp_Char_Table *) for character
12197 C. Since a display table doesn't have any parent, we don't have to
12198 follow parent. Do not call this function directly but use the
12199 macro DISP_CHAR_VECTOR. */
12200
12201 Lisp_Object
12202 disp_char_vector (dp, c)
12203 struct Lisp_Char_Table *dp;
12204 int c;
12205 {
12206 Lisp_Object val;
12207
12208 if (ASCII_CHAR_P (c))
12209 {
12210 val = dp->ascii;
12211 if (SUB_CHAR_TABLE_P (val))
12212 val = XSUB_CHAR_TABLE (val)->contents[c];
12213 }
12214 else
12215 {
12216 Lisp_Object table;
12217
12218 XSETCHAR_TABLE (table, dp);
12219 val = char_table_ref (table, c);
12220 }
12221 if (NILP (val))
12222 val = dp->defalt;
12223 return val;
12224 }
12225
12226
12227 \f
12228 /***********************************************************************
12229 Window Redisplay
12230 ***********************************************************************/
12231
12232 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12233
12234 static void
12235 redisplay_windows (window)
12236 Lisp_Object window;
12237 {
12238 while (!NILP (window))
12239 {
12240 struct window *w = XWINDOW (window);
12241
12242 if (!NILP (w->hchild))
12243 redisplay_windows (w->hchild);
12244 else if (!NILP (w->vchild))
12245 redisplay_windows (w->vchild);
12246 else
12247 {
12248 displayed_buffer = XBUFFER (w->buffer);
12249 /* Use list_of_error, not Qerror, so that
12250 we catch only errors and don't run the debugger. */
12251 internal_condition_case_1 (redisplay_window_0, window,
12252 list_of_error,
12253 redisplay_window_error);
12254 }
12255
12256 window = w->next;
12257 }
12258 }
12259
12260 static Lisp_Object
12261 redisplay_window_error ()
12262 {
12263 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12264 return Qnil;
12265 }
12266
12267 static Lisp_Object
12268 redisplay_window_0 (window)
12269 Lisp_Object window;
12270 {
12271 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12272 redisplay_window (window, 0);
12273 return Qnil;
12274 }
12275
12276 static Lisp_Object
12277 redisplay_window_1 (window)
12278 Lisp_Object window;
12279 {
12280 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12281 redisplay_window (window, 1);
12282 return Qnil;
12283 }
12284 \f
12285
12286 /* Increment GLYPH until it reaches END or CONDITION fails while
12287 adding (GLYPH)->pixel_width to X. */
12288
12289 #define SKIP_GLYPHS(glyph, end, x, condition) \
12290 do \
12291 { \
12292 (x) += (glyph)->pixel_width; \
12293 ++(glyph); \
12294 } \
12295 while ((glyph) < (end) && (condition))
12296
12297
12298 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12299 DELTA is the number of bytes by which positions recorded in ROW
12300 differ from current buffer positions.
12301
12302 Return 0 if cursor is not on this row. 1 otherwise. */
12303
12304 int
12305 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12306 struct window *w;
12307 struct glyph_row *row;
12308 struct glyph_matrix *matrix;
12309 int delta, delta_bytes, dy, dvpos;
12310 {
12311 struct glyph *glyph = row->glyphs[TEXT_AREA];
12312 struct glyph *end = glyph + row->used[TEXT_AREA];
12313 struct glyph *cursor = NULL;
12314 /* The first glyph that starts a sequence of glyphs from string. */
12315 struct glyph *string_start;
12316 /* The X coordinate of string_start. */
12317 int string_start_x;
12318 /* The last known character position. */
12319 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12320 /* The last known character position before string_start. */
12321 int string_before_pos;
12322 int x = row->x;
12323 int cursor_x = x;
12324 int cursor_from_overlay_pos = 0;
12325 int pt_old = PT - delta;
12326
12327 /* Skip over glyphs not having an object at the start of the row.
12328 These are special glyphs like truncation marks on terminal
12329 frames. */
12330 if (row->displays_text_p)
12331 while (glyph < end
12332 && INTEGERP (glyph->object)
12333 && glyph->charpos < 0)
12334 {
12335 x += glyph->pixel_width;
12336 ++glyph;
12337 }
12338
12339 string_start = NULL;
12340 while (glyph < end
12341 && !INTEGERP (glyph->object)
12342 && (!BUFFERP (glyph->object)
12343 || (last_pos = glyph->charpos) < pt_old
12344 || glyph->avoid_cursor_p))
12345 {
12346 if (! STRINGP (glyph->object))
12347 {
12348 string_start = NULL;
12349 x += glyph->pixel_width;
12350 ++glyph;
12351 if (cursor_from_overlay_pos
12352 && last_pos >= cursor_from_overlay_pos)
12353 {
12354 cursor_from_overlay_pos = 0;
12355 cursor = 0;
12356 }
12357 }
12358 else
12359 {
12360 if (string_start == NULL)
12361 {
12362 string_before_pos = last_pos;
12363 string_start = glyph;
12364 string_start_x = x;
12365 }
12366 /* Skip all glyphs from string. */
12367 do
12368 {
12369 Lisp_Object cprop;
12370 int pos;
12371 if ((cursor == NULL || glyph > cursor)
12372 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12373 Qcursor, (glyph)->object),
12374 !NILP (cprop))
12375 && (pos = string_buffer_position (w, glyph->object,
12376 string_before_pos),
12377 (pos == 0 /* From overlay */
12378 || pos == pt_old)))
12379 {
12380 /* Estimate overlay buffer position from the buffer
12381 positions of the glyphs before and after the overlay.
12382 Add 1 to last_pos so that if point corresponds to the
12383 glyph right after the overlay, we still use a 'cursor'
12384 property found in that overlay. */
12385 cursor_from_overlay_pos = (pos ? 0 : last_pos
12386 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12387 cursor = glyph;
12388 cursor_x = x;
12389 }
12390 x += glyph->pixel_width;
12391 ++glyph;
12392 }
12393 while (glyph < end && EQ (glyph->object, string_start->object));
12394 }
12395 }
12396
12397 if (cursor != NULL)
12398 {
12399 glyph = cursor;
12400 x = cursor_x;
12401 }
12402 else if (row->ends_in_ellipsis_p && glyph == end)
12403 {
12404 /* Scan back over the ellipsis glyphs, decrementing positions. */
12405 while (glyph > row->glyphs[TEXT_AREA]
12406 && (glyph - 1)->charpos == last_pos)
12407 glyph--, x -= glyph->pixel_width;
12408 /* That loop always goes one position too far,
12409 including the glyph before the ellipsis.
12410 So scan forward over that one. */
12411 x += glyph->pixel_width;
12412 glyph++;
12413 }
12414 else if (string_start
12415 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12416 {
12417 /* We may have skipped over point because the previous glyphs
12418 are from string. As there's no easy way to know the
12419 character position of the current glyph, find the correct
12420 glyph on point by scanning from string_start again. */
12421 Lisp_Object limit;
12422 Lisp_Object string;
12423 struct glyph *stop = glyph;
12424 int pos;
12425
12426 limit = make_number (pt_old + 1);
12427 glyph = string_start;
12428 x = string_start_x;
12429 string = glyph->object;
12430 pos = string_buffer_position (w, string, string_before_pos);
12431 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12432 because we always put cursor after overlay strings. */
12433 while (pos == 0 && glyph < stop)
12434 {
12435 string = glyph->object;
12436 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12437 if (glyph < stop)
12438 pos = string_buffer_position (w, glyph->object, string_before_pos);
12439 }
12440
12441 while (glyph < stop)
12442 {
12443 pos = XINT (Fnext_single_char_property_change
12444 (make_number (pos), Qdisplay, Qnil, limit));
12445 if (pos > pt_old)
12446 break;
12447 /* Skip glyphs from the same string. */
12448 string = glyph->object;
12449 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12450 /* Skip glyphs from an overlay. */
12451 while (glyph < stop
12452 && ! string_buffer_position (w, glyph->object, pos))
12453 {
12454 string = glyph->object;
12455 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12456 }
12457 }
12458
12459 /* If we reached the end of the line, and end was from a string,
12460 cursor is not on this line. */
12461 if (glyph == end && row->continued_p)
12462 return 0;
12463 }
12464
12465 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12466 w->cursor.x = x;
12467 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12468 w->cursor.y = row->y + dy;
12469
12470 if (w == XWINDOW (selected_window))
12471 {
12472 if (!row->continued_p
12473 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12474 && row->x == 0)
12475 {
12476 this_line_buffer = XBUFFER (w->buffer);
12477
12478 CHARPOS (this_line_start_pos)
12479 = MATRIX_ROW_START_CHARPOS (row) + delta;
12480 BYTEPOS (this_line_start_pos)
12481 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12482
12483 CHARPOS (this_line_end_pos)
12484 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12485 BYTEPOS (this_line_end_pos)
12486 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12487
12488 this_line_y = w->cursor.y;
12489 this_line_pixel_height = row->height;
12490 this_line_vpos = w->cursor.vpos;
12491 this_line_start_x = row->x;
12492 }
12493 else
12494 CHARPOS (this_line_start_pos) = 0;
12495 }
12496
12497 return 1;
12498 }
12499
12500
12501 /* Run window scroll functions, if any, for WINDOW with new window
12502 start STARTP. Sets the window start of WINDOW to that position.
12503
12504 We assume that the window's buffer is really current. */
12505
12506 static INLINE struct text_pos
12507 run_window_scroll_functions (window, startp)
12508 Lisp_Object window;
12509 struct text_pos startp;
12510 {
12511 struct window *w = XWINDOW (window);
12512 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12513
12514 if (current_buffer != XBUFFER (w->buffer))
12515 abort ();
12516
12517 if (!NILP (Vwindow_scroll_functions))
12518 {
12519 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12520 make_number (CHARPOS (startp)));
12521 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12522 /* In case the hook functions switch buffers. */
12523 if (current_buffer != XBUFFER (w->buffer))
12524 set_buffer_internal_1 (XBUFFER (w->buffer));
12525 }
12526
12527 return startp;
12528 }
12529
12530
12531 /* Make sure the line containing the cursor is fully visible.
12532 A value of 1 means there is nothing to be done.
12533 (Either the line is fully visible, or it cannot be made so,
12534 or we cannot tell.)
12535
12536 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12537 is higher than window.
12538
12539 A value of 0 means the caller should do scrolling
12540 as if point had gone off the screen. */
12541
12542 static int
12543 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12544 struct window *w;
12545 int force_p;
12546 int current_matrix_p;
12547 {
12548 struct glyph_matrix *matrix;
12549 struct glyph_row *row;
12550 int window_height;
12551
12552 if (!make_cursor_line_fully_visible_p)
12553 return 1;
12554
12555 /* It's not always possible to find the cursor, e.g, when a window
12556 is full of overlay strings. Don't do anything in that case. */
12557 if (w->cursor.vpos < 0)
12558 return 1;
12559
12560 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12561 row = MATRIX_ROW (matrix, w->cursor.vpos);
12562
12563 /* If the cursor row is not partially visible, there's nothing to do. */
12564 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12565 return 1;
12566
12567 /* If the row the cursor is in is taller than the window's height,
12568 it's not clear what to do, so do nothing. */
12569 window_height = window_box_height (w);
12570 if (row->height >= window_height)
12571 {
12572 if (!force_p || MINI_WINDOW_P (w)
12573 || w->vscroll || w->cursor.vpos == 0)
12574 return 1;
12575 }
12576 return 0;
12577
12578 #if 0
12579 /* This code used to try to scroll the window just enough to make
12580 the line visible. It returned 0 to say that the caller should
12581 allocate larger glyph matrices. */
12582
12583 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12584 {
12585 int dy = row->height - row->visible_height;
12586 w->vscroll = 0;
12587 w->cursor.y += dy;
12588 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12589 }
12590 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12591 {
12592 int dy = - (row->height - row->visible_height);
12593 w->vscroll = dy;
12594 w->cursor.y += dy;
12595 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12596 }
12597
12598 /* When we change the cursor y-position of the selected window,
12599 change this_line_y as well so that the display optimization for
12600 the cursor line of the selected window in redisplay_internal uses
12601 the correct y-position. */
12602 if (w == XWINDOW (selected_window))
12603 this_line_y = w->cursor.y;
12604
12605 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12606 redisplay with larger matrices. */
12607 if (matrix->nrows < required_matrix_height (w))
12608 {
12609 fonts_changed_p = 1;
12610 return 0;
12611 }
12612
12613 return 1;
12614 #endif /* 0 */
12615 }
12616
12617
12618 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12619 non-zero means only WINDOW is redisplayed in redisplay_internal.
12620 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12621 in redisplay_window to bring a partially visible line into view in
12622 the case that only the cursor has moved.
12623
12624 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12625 last screen line's vertical height extends past the end of the screen.
12626
12627 Value is
12628
12629 1 if scrolling succeeded
12630
12631 0 if scrolling didn't find point.
12632
12633 -1 if new fonts have been loaded so that we must interrupt
12634 redisplay, adjust glyph matrices, and try again. */
12635
12636 enum
12637 {
12638 SCROLLING_SUCCESS,
12639 SCROLLING_FAILED,
12640 SCROLLING_NEED_LARGER_MATRICES
12641 };
12642
12643 static int
12644 try_scrolling (window, just_this_one_p, scroll_conservatively,
12645 scroll_step, temp_scroll_step, last_line_misfit)
12646 Lisp_Object window;
12647 int just_this_one_p;
12648 EMACS_INT scroll_conservatively, scroll_step;
12649 int temp_scroll_step;
12650 int last_line_misfit;
12651 {
12652 struct window *w = XWINDOW (window);
12653 struct frame *f = XFRAME (w->frame);
12654 struct text_pos pos, startp;
12655 struct it it;
12656 int this_scroll_margin, scroll_max, rc, height;
12657 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12658 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12659 Lisp_Object aggressive;
12660 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12661
12662 #if GLYPH_DEBUG
12663 debug_method_add (w, "try_scrolling");
12664 #endif
12665
12666 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12667
12668 /* Compute scroll margin height in pixels. We scroll when point is
12669 within this distance from the top or bottom of the window. */
12670 if (scroll_margin > 0)
12671 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12672 * FRAME_LINE_HEIGHT (f);
12673 else
12674 this_scroll_margin = 0;
12675
12676 /* Force scroll_conservatively to have a reasonable value, to avoid
12677 overflow while computing how much to scroll. Note that the user
12678 can supply scroll-conservatively equal to `most-positive-fixnum',
12679 which can be larger than INT_MAX. */
12680 if (scroll_conservatively > scroll_limit)
12681 {
12682 scroll_conservatively = scroll_limit;
12683 scroll_max = INT_MAX;
12684 }
12685 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12686 /* Compute how much we should try to scroll maximally to bring
12687 point into view. */
12688 scroll_max = (max (scroll_step,
12689 max (scroll_conservatively, temp_scroll_step))
12690 * FRAME_LINE_HEIGHT (f));
12691 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12692 || NUMBERP (current_buffer->scroll_up_aggressively))
12693 /* We're trying to scroll because of aggressive scrolling but no
12694 scroll_step is set. Choose an arbitrary one. */
12695 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12696 else
12697 scroll_max = 0;
12698
12699 too_near_end:
12700
12701 /* Decide whether to scroll down. */
12702 if (PT > CHARPOS (startp))
12703 {
12704 int scroll_margin_y;
12705
12706 /* Compute the pixel ypos of the scroll margin, then move it to
12707 either that ypos or PT, whichever comes first. */
12708 start_display (&it, w, startp);
12709 scroll_margin_y = it.last_visible_y - this_scroll_margin
12710 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12711 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12712 (MOVE_TO_POS | MOVE_TO_Y));
12713
12714 if (PT > CHARPOS (it.current.pos))
12715 {
12716 int y0 = line_bottom_y (&it);
12717
12718 /* Compute the distance from the scroll margin to PT
12719 (including the height of the cursor line). Moving the
12720 iterator unconditionally to PT can be slow if PT is far
12721 away, so stop 10 lines past the window bottom (is there a
12722 way to do the right thing quickly?). */
12723 move_it_to (&it, PT, -1,
12724 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12725 -1, MOVE_TO_POS | MOVE_TO_Y);
12726 dy = line_bottom_y (&it) - y0;
12727
12728 if (dy > scroll_max)
12729 return SCROLLING_FAILED;
12730
12731 scroll_down_p = 1;
12732 }
12733 }
12734
12735 if (scroll_down_p)
12736 {
12737 /* Point is in or below the bottom scroll margin, so move the
12738 window start down. If scrolling conservatively, move it just
12739 enough down to make point visible. If scroll_step is set,
12740 move it down by scroll_step. */
12741 if (scroll_conservatively)
12742 amount_to_scroll
12743 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12744 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12745 else if (scroll_step || temp_scroll_step)
12746 amount_to_scroll = scroll_max;
12747 else
12748 {
12749 aggressive = current_buffer->scroll_up_aggressively;
12750 height = WINDOW_BOX_TEXT_HEIGHT (w);
12751 if (NUMBERP (aggressive))
12752 {
12753 double float_amount = XFLOATINT (aggressive) * height;
12754 amount_to_scroll = float_amount;
12755 if (amount_to_scroll == 0 && float_amount > 0)
12756 amount_to_scroll = 1;
12757 }
12758 }
12759
12760 if (amount_to_scroll <= 0)
12761 return SCROLLING_FAILED;
12762
12763 start_display (&it, w, startp);
12764 move_it_vertically (&it, amount_to_scroll);
12765
12766 /* If STARTP is unchanged, move it down another screen line. */
12767 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12768 move_it_by_lines (&it, 1, 1);
12769 startp = it.current.pos;
12770 }
12771 else
12772 {
12773 struct text_pos scroll_margin_pos = startp;
12774
12775 /* See if point is inside the scroll margin at the top of the
12776 window. */
12777 if (this_scroll_margin)
12778 {
12779 start_display (&it, w, startp);
12780 move_it_vertically (&it, this_scroll_margin);
12781 scroll_margin_pos = it.current.pos;
12782 }
12783
12784 if (PT < CHARPOS (scroll_margin_pos))
12785 {
12786 /* Point is in the scroll margin at the top of the window or
12787 above what is displayed in the window. */
12788 int y0;
12789
12790 /* Compute the vertical distance from PT to the scroll
12791 margin position. Give up if distance is greater than
12792 scroll_max. */
12793 SET_TEXT_POS (pos, PT, PT_BYTE);
12794 start_display (&it, w, pos);
12795 y0 = it.current_y;
12796 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12797 it.last_visible_y, -1,
12798 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12799 dy = it.current_y - y0;
12800 if (dy > scroll_max)
12801 return SCROLLING_FAILED;
12802
12803 /* Compute new window start. */
12804 start_display (&it, w, startp);
12805
12806 if (scroll_conservatively)
12807 amount_to_scroll
12808 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12809 else if (scroll_step || temp_scroll_step)
12810 amount_to_scroll = scroll_max;
12811 else
12812 {
12813 aggressive = current_buffer->scroll_down_aggressively;
12814 height = WINDOW_BOX_TEXT_HEIGHT (w);
12815 if (NUMBERP (aggressive))
12816 {
12817 double float_amount = XFLOATINT (aggressive) * height;
12818 amount_to_scroll = float_amount;
12819 if (amount_to_scroll == 0 && float_amount > 0)
12820 amount_to_scroll = 1;
12821 }
12822 }
12823
12824 if (amount_to_scroll <= 0)
12825 return SCROLLING_FAILED;
12826
12827 move_it_vertically_backward (&it, amount_to_scroll);
12828 startp = it.current.pos;
12829 }
12830 }
12831
12832 /* Run window scroll functions. */
12833 startp = run_window_scroll_functions (window, startp);
12834
12835 /* Display the window. Give up if new fonts are loaded, or if point
12836 doesn't appear. */
12837 if (!try_window (window, startp, 0))
12838 rc = SCROLLING_NEED_LARGER_MATRICES;
12839 else if (w->cursor.vpos < 0)
12840 {
12841 clear_glyph_matrix (w->desired_matrix);
12842 rc = SCROLLING_FAILED;
12843 }
12844 else
12845 {
12846 /* Maybe forget recorded base line for line number display. */
12847 if (!just_this_one_p
12848 || current_buffer->clip_changed
12849 || BEG_UNCHANGED < CHARPOS (startp))
12850 w->base_line_number = Qnil;
12851
12852 /* If cursor ends up on a partially visible line,
12853 treat that as being off the bottom of the screen. */
12854 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12855 {
12856 clear_glyph_matrix (w->desired_matrix);
12857 ++extra_scroll_margin_lines;
12858 goto too_near_end;
12859 }
12860 rc = SCROLLING_SUCCESS;
12861 }
12862
12863 return rc;
12864 }
12865
12866
12867 /* Compute a suitable window start for window W if display of W starts
12868 on a continuation line. Value is non-zero if a new window start
12869 was computed.
12870
12871 The new window start will be computed, based on W's width, starting
12872 from the start of the continued line. It is the start of the
12873 screen line with the minimum distance from the old start W->start. */
12874
12875 static int
12876 compute_window_start_on_continuation_line (w)
12877 struct window *w;
12878 {
12879 struct text_pos pos, start_pos;
12880 int window_start_changed_p = 0;
12881
12882 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12883
12884 /* If window start is on a continuation line... Window start may be
12885 < BEGV in case there's invisible text at the start of the
12886 buffer (M-x rmail, for example). */
12887 if (CHARPOS (start_pos) > BEGV
12888 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12889 {
12890 struct it it;
12891 struct glyph_row *row;
12892
12893 /* Handle the case that the window start is out of range. */
12894 if (CHARPOS (start_pos) < BEGV)
12895 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12896 else if (CHARPOS (start_pos) > ZV)
12897 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12898
12899 /* Find the start of the continued line. This should be fast
12900 because scan_buffer is fast (newline cache). */
12901 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12902 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12903 row, DEFAULT_FACE_ID);
12904 reseat_at_previous_visible_line_start (&it);
12905
12906 /* If the line start is "too far" away from the window start,
12907 say it takes too much time to compute a new window start. */
12908 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12909 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12910 {
12911 int min_distance, distance;
12912
12913 /* Move forward by display lines to find the new window
12914 start. If window width was enlarged, the new start can
12915 be expected to be > the old start. If window width was
12916 decreased, the new window start will be < the old start.
12917 So, we're looking for the display line start with the
12918 minimum distance from the old window start. */
12919 pos = it.current.pos;
12920 min_distance = INFINITY;
12921 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12922 distance < min_distance)
12923 {
12924 min_distance = distance;
12925 pos = it.current.pos;
12926 move_it_by_lines (&it, 1, 0);
12927 }
12928
12929 /* Set the window start there. */
12930 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12931 window_start_changed_p = 1;
12932 }
12933 }
12934
12935 return window_start_changed_p;
12936 }
12937
12938
12939 /* Try cursor movement in case text has not changed in window WINDOW,
12940 with window start STARTP. Value is
12941
12942 CURSOR_MOVEMENT_SUCCESS if successful
12943
12944 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12945
12946 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12947 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12948 we want to scroll as if scroll-step were set to 1. See the code.
12949
12950 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12951 which case we have to abort this redisplay, and adjust matrices
12952 first. */
12953
12954 enum
12955 {
12956 CURSOR_MOVEMENT_SUCCESS,
12957 CURSOR_MOVEMENT_CANNOT_BE_USED,
12958 CURSOR_MOVEMENT_MUST_SCROLL,
12959 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12960 };
12961
12962 static int
12963 try_cursor_movement (window, startp, scroll_step)
12964 Lisp_Object window;
12965 struct text_pos startp;
12966 int *scroll_step;
12967 {
12968 struct window *w = XWINDOW (window);
12969 struct frame *f = XFRAME (w->frame);
12970 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12971
12972 #if GLYPH_DEBUG
12973 if (inhibit_try_cursor_movement)
12974 return rc;
12975 #endif
12976
12977 /* Handle case where text has not changed, only point, and it has
12978 not moved off the frame. */
12979 if (/* Point may be in this window. */
12980 PT >= CHARPOS (startp)
12981 /* Selective display hasn't changed. */
12982 && !current_buffer->clip_changed
12983 /* Function force-mode-line-update is used to force a thorough
12984 redisplay. It sets either windows_or_buffers_changed or
12985 update_mode_lines. So don't take a shortcut here for these
12986 cases. */
12987 && !update_mode_lines
12988 && !windows_or_buffers_changed
12989 && !cursor_type_changed
12990 /* Can't use this case if highlighting a region. When a
12991 region exists, cursor movement has to do more than just
12992 set the cursor. */
12993 && !(!NILP (Vtransient_mark_mode)
12994 && !NILP (current_buffer->mark_active))
12995 && NILP (w->region_showing)
12996 && NILP (Vshow_trailing_whitespace)
12997 /* Right after splitting windows, last_point may be nil. */
12998 && INTEGERP (w->last_point)
12999 /* This code is not used for mini-buffer for the sake of the case
13000 of redisplaying to replace an echo area message; since in
13001 that case the mini-buffer contents per se are usually
13002 unchanged. This code is of no real use in the mini-buffer
13003 since the handling of this_line_start_pos, etc., in redisplay
13004 handles the same cases. */
13005 && !EQ (window, minibuf_window)
13006 /* When splitting windows or for new windows, it happens that
13007 redisplay is called with a nil window_end_vpos or one being
13008 larger than the window. This should really be fixed in
13009 window.c. I don't have this on my list, now, so we do
13010 approximately the same as the old redisplay code. --gerd. */
13011 && INTEGERP (w->window_end_vpos)
13012 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13013 && (FRAME_WINDOW_P (f)
13014 || !overlay_arrow_in_current_buffer_p ()))
13015 {
13016 int this_scroll_margin, top_scroll_margin;
13017 struct glyph_row *row = NULL;
13018
13019 #if GLYPH_DEBUG
13020 debug_method_add (w, "cursor movement");
13021 #endif
13022
13023 /* Scroll if point within this distance from the top or bottom
13024 of the window. This is a pixel value. */
13025 if (scroll_margin > 0)
13026 {
13027 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13028 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13029 }
13030 else
13031 this_scroll_margin = 0;
13032
13033 top_scroll_margin = this_scroll_margin;
13034 if (WINDOW_WANTS_HEADER_LINE_P (w))
13035 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13036
13037 /* Start with the row the cursor was displayed during the last
13038 not paused redisplay. Give up if that row is not valid. */
13039 if (w->last_cursor.vpos < 0
13040 || w->last_cursor.vpos >= w->current_matrix->nrows)
13041 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13042 else
13043 {
13044 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13045 if (row->mode_line_p)
13046 ++row;
13047 if (!row->enabled_p)
13048 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13049 }
13050
13051 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13052 {
13053 int scroll_p = 0;
13054 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13055
13056 if (PT > XFASTINT (w->last_point))
13057 {
13058 /* Point has moved forward. */
13059 while (MATRIX_ROW_END_CHARPOS (row) < PT
13060 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13061 {
13062 xassert (row->enabled_p);
13063 ++row;
13064 }
13065
13066 /* The end position of a row equals the start position
13067 of the next row. If PT is there, we would rather
13068 display it in the next line. */
13069 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13070 && MATRIX_ROW_END_CHARPOS (row) == PT
13071 && !cursor_row_p (w, row))
13072 ++row;
13073
13074 /* If within the scroll margin, scroll. Note that
13075 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13076 the next line would be drawn, and that
13077 this_scroll_margin can be zero. */
13078 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13079 || PT > MATRIX_ROW_END_CHARPOS (row)
13080 /* Line is completely visible last line in window
13081 and PT is to be set in the next line. */
13082 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13083 && PT == MATRIX_ROW_END_CHARPOS (row)
13084 && !row->ends_at_zv_p
13085 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13086 scroll_p = 1;
13087 }
13088 else if (PT < XFASTINT (w->last_point))
13089 {
13090 /* Cursor has to be moved backward. Note that PT >=
13091 CHARPOS (startp) because of the outer if-statement. */
13092 while (!row->mode_line_p
13093 && (MATRIX_ROW_START_CHARPOS (row) > PT
13094 || (MATRIX_ROW_START_CHARPOS (row) == PT
13095 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13096 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13097 row > w->current_matrix->rows
13098 && (row-1)->ends_in_newline_from_string_p))))
13099 && (row->y > top_scroll_margin
13100 || CHARPOS (startp) == BEGV))
13101 {
13102 xassert (row->enabled_p);
13103 --row;
13104 }
13105
13106 /* Consider the following case: Window starts at BEGV,
13107 there is invisible, intangible text at BEGV, so that
13108 display starts at some point START > BEGV. It can
13109 happen that we are called with PT somewhere between
13110 BEGV and START. Try to handle that case. */
13111 if (row < w->current_matrix->rows
13112 || row->mode_line_p)
13113 {
13114 row = w->current_matrix->rows;
13115 if (row->mode_line_p)
13116 ++row;
13117 }
13118
13119 /* Due to newlines in overlay strings, we may have to
13120 skip forward over overlay strings. */
13121 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13122 && MATRIX_ROW_END_CHARPOS (row) == PT
13123 && !cursor_row_p (w, row))
13124 ++row;
13125
13126 /* If within the scroll margin, scroll. */
13127 if (row->y < top_scroll_margin
13128 && CHARPOS (startp) != BEGV)
13129 scroll_p = 1;
13130 }
13131 else
13132 {
13133 /* Cursor did not move. So don't scroll even if cursor line
13134 is partially visible, as it was so before. */
13135 rc = CURSOR_MOVEMENT_SUCCESS;
13136 }
13137
13138 if (PT < MATRIX_ROW_START_CHARPOS (row)
13139 || PT > MATRIX_ROW_END_CHARPOS (row))
13140 {
13141 /* if PT is not in the glyph row, give up. */
13142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13143 }
13144 else if (rc != CURSOR_MOVEMENT_SUCCESS
13145 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13146 && make_cursor_line_fully_visible_p)
13147 {
13148 if (PT == MATRIX_ROW_END_CHARPOS (row)
13149 && !row->ends_at_zv_p
13150 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13151 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13152 else if (row->height > window_box_height (w))
13153 {
13154 /* If we end up in a partially visible line, let's
13155 make it fully visible, except when it's taller
13156 than the window, in which case we can't do much
13157 about it. */
13158 *scroll_step = 1;
13159 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13160 }
13161 else
13162 {
13163 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13164 if (!cursor_row_fully_visible_p (w, 0, 1))
13165 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13166 else
13167 rc = CURSOR_MOVEMENT_SUCCESS;
13168 }
13169 }
13170 else if (scroll_p)
13171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13172 else
13173 {
13174 do
13175 {
13176 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13177 {
13178 rc = CURSOR_MOVEMENT_SUCCESS;
13179 break;
13180 }
13181 ++row;
13182 }
13183 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13184 && MATRIX_ROW_START_CHARPOS (row) == PT
13185 && cursor_row_p (w, row));
13186 }
13187 }
13188 }
13189
13190 return rc;
13191 }
13192
13193 void
13194 set_vertical_scroll_bar (w)
13195 struct window *w;
13196 {
13197 int start, end, whole;
13198
13199 /* Calculate the start and end positions for the current window.
13200 At some point, it would be nice to choose between scrollbars
13201 which reflect the whole buffer size, with special markers
13202 indicating narrowing, and scrollbars which reflect only the
13203 visible region.
13204
13205 Note that mini-buffers sometimes aren't displaying any text. */
13206 if (!MINI_WINDOW_P (w)
13207 || (w == XWINDOW (minibuf_window)
13208 && NILP (echo_area_buffer[0])))
13209 {
13210 struct buffer *buf = XBUFFER (w->buffer);
13211 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13212 start = marker_position (w->start) - BUF_BEGV (buf);
13213 /* I don't think this is guaranteed to be right. For the
13214 moment, we'll pretend it is. */
13215 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13216
13217 if (end < start)
13218 end = start;
13219 if (whole < (end - start))
13220 whole = end - start;
13221 }
13222 else
13223 start = end = whole = 0;
13224
13225 /* Indicate what this scroll bar ought to be displaying now. */
13226 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13227 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13228 (w, end - start, whole, start);
13229 }
13230
13231
13232 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13233 selected_window is redisplayed.
13234
13235 We can return without actually redisplaying the window if
13236 fonts_changed_p is nonzero. In that case, redisplay_internal will
13237 retry. */
13238
13239 static void
13240 redisplay_window (window, just_this_one_p)
13241 Lisp_Object window;
13242 int just_this_one_p;
13243 {
13244 struct window *w = XWINDOW (window);
13245 struct frame *f = XFRAME (w->frame);
13246 struct buffer *buffer = XBUFFER (w->buffer);
13247 struct buffer *old = current_buffer;
13248 struct text_pos lpoint, opoint, startp;
13249 int update_mode_line;
13250 int tem;
13251 struct it it;
13252 /* Record it now because it's overwritten. */
13253 int current_matrix_up_to_date_p = 0;
13254 int used_current_matrix_p = 0;
13255 /* This is less strict than current_matrix_up_to_date_p.
13256 It indictes that the buffer contents and narrowing are unchanged. */
13257 int buffer_unchanged_p = 0;
13258 int temp_scroll_step = 0;
13259 int count = SPECPDL_INDEX ();
13260 int rc;
13261 int centering_position = -1;
13262 int last_line_misfit = 0;
13263 int beg_unchanged, end_unchanged;
13264
13265 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13266 opoint = lpoint;
13267
13268 /* W must be a leaf window here. */
13269 xassert (!NILP (w->buffer));
13270 #if GLYPH_DEBUG
13271 *w->desired_matrix->method = 0;
13272 #endif
13273
13274 restart:
13275 reconsider_clip_changes (w, buffer);
13276
13277 /* Has the mode line to be updated? */
13278 update_mode_line = (!NILP (w->update_mode_line)
13279 || update_mode_lines
13280 || buffer->clip_changed
13281 || buffer->prevent_redisplay_optimizations_p);
13282
13283 if (MINI_WINDOW_P (w))
13284 {
13285 if (w == XWINDOW (echo_area_window)
13286 && !NILP (echo_area_buffer[0]))
13287 {
13288 if (update_mode_line)
13289 /* We may have to update a tty frame's menu bar or a
13290 tool-bar. Example `M-x C-h C-h C-g'. */
13291 goto finish_menu_bars;
13292 else
13293 /* We've already displayed the echo area glyphs in this window. */
13294 goto finish_scroll_bars;
13295 }
13296 else if ((w != XWINDOW (minibuf_window)
13297 || minibuf_level == 0)
13298 /* When buffer is nonempty, redisplay window normally. */
13299 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13300 /* Quail displays non-mini buffers in minibuffer window.
13301 In that case, redisplay the window normally. */
13302 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13303 {
13304 /* W is a mini-buffer window, but it's not active, so clear
13305 it. */
13306 int yb = window_text_bottom_y (w);
13307 struct glyph_row *row;
13308 int y;
13309
13310 for (y = 0, row = w->desired_matrix->rows;
13311 y < yb;
13312 y += row->height, ++row)
13313 blank_row (w, row, y);
13314 goto finish_scroll_bars;
13315 }
13316
13317 clear_glyph_matrix (w->desired_matrix);
13318 }
13319
13320 /* Otherwise set up data on this window; select its buffer and point
13321 value. */
13322 /* Really select the buffer, for the sake of buffer-local
13323 variables. */
13324 set_buffer_internal_1 (XBUFFER (w->buffer));
13325
13326 current_matrix_up_to_date_p
13327 = (!NILP (w->window_end_valid)
13328 && !current_buffer->clip_changed
13329 && !current_buffer->prevent_redisplay_optimizations_p
13330 && XFASTINT (w->last_modified) >= MODIFF
13331 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13332
13333 /* Run the window-bottom-change-functions
13334 if it is possible that the text on the screen has changed
13335 (either due to modification of the text, or any other reason). */
13336 if (!current_matrix_up_to_date_p
13337 && !NILP (Vwindow_text_change_functions))
13338 {
13339 safe_run_hooks (Qwindow_text_change_functions);
13340 goto restart;
13341 }
13342
13343 beg_unchanged = BEG_UNCHANGED;
13344 end_unchanged = END_UNCHANGED;
13345
13346 SET_TEXT_POS (opoint, PT, PT_BYTE);
13347
13348 specbind (Qinhibit_point_motion_hooks, Qt);
13349
13350 buffer_unchanged_p
13351 = (!NILP (w->window_end_valid)
13352 && !current_buffer->clip_changed
13353 && XFASTINT (w->last_modified) >= MODIFF
13354 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13355
13356 /* When windows_or_buffers_changed is non-zero, we can't rely on
13357 the window end being valid, so set it to nil there. */
13358 if (windows_or_buffers_changed)
13359 {
13360 /* If window starts on a continuation line, maybe adjust the
13361 window start in case the window's width changed. */
13362 if (XMARKER (w->start)->buffer == current_buffer)
13363 compute_window_start_on_continuation_line (w);
13364
13365 w->window_end_valid = Qnil;
13366 }
13367
13368 /* Some sanity checks. */
13369 CHECK_WINDOW_END (w);
13370 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13371 abort ();
13372 if (BYTEPOS (opoint) < CHARPOS (opoint))
13373 abort ();
13374
13375 /* If %c is in mode line, update it if needed. */
13376 if (!NILP (w->column_number_displayed)
13377 /* This alternative quickly identifies a common case
13378 where no change is needed. */
13379 && !(PT == XFASTINT (w->last_point)
13380 && XFASTINT (w->last_modified) >= MODIFF
13381 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13382 && (XFASTINT (w->column_number_displayed)
13383 != (int) current_column ())) /* iftc */
13384 update_mode_line = 1;
13385
13386 /* Count number of windows showing the selected buffer. An indirect
13387 buffer counts as its base buffer. */
13388 if (!just_this_one_p)
13389 {
13390 struct buffer *current_base, *window_base;
13391 current_base = current_buffer;
13392 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13393 if (current_base->base_buffer)
13394 current_base = current_base->base_buffer;
13395 if (window_base->base_buffer)
13396 window_base = window_base->base_buffer;
13397 if (current_base == window_base)
13398 buffer_shared++;
13399 }
13400
13401 /* Point refers normally to the selected window. For any other
13402 window, set up appropriate value. */
13403 if (!EQ (window, selected_window))
13404 {
13405 int new_pt = XMARKER (w->pointm)->charpos;
13406 int new_pt_byte = marker_byte_position (w->pointm);
13407 if (new_pt < BEGV)
13408 {
13409 new_pt = BEGV;
13410 new_pt_byte = BEGV_BYTE;
13411 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13412 }
13413 else if (new_pt > (ZV - 1))
13414 {
13415 new_pt = ZV;
13416 new_pt_byte = ZV_BYTE;
13417 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13418 }
13419
13420 /* We don't use SET_PT so that the point-motion hooks don't run. */
13421 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13422 }
13423
13424 /* If any of the character widths specified in the display table
13425 have changed, invalidate the width run cache. It's true that
13426 this may be a bit late to catch such changes, but the rest of
13427 redisplay goes (non-fatally) haywire when the display table is
13428 changed, so why should we worry about doing any better? */
13429 if (current_buffer->width_run_cache)
13430 {
13431 struct Lisp_Char_Table *disptab = buffer_display_table ();
13432
13433 if (! disptab_matches_widthtab (disptab,
13434 XVECTOR (current_buffer->width_table)))
13435 {
13436 invalidate_region_cache (current_buffer,
13437 current_buffer->width_run_cache,
13438 BEG, Z);
13439 recompute_width_table (current_buffer, disptab);
13440 }
13441 }
13442
13443 /* If window-start is screwed up, choose a new one. */
13444 if (XMARKER (w->start)->buffer != current_buffer)
13445 goto recenter;
13446
13447 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13448
13449 /* If someone specified a new starting point but did not insist,
13450 check whether it can be used. */
13451 if (!NILP (w->optional_new_start)
13452 && CHARPOS (startp) >= BEGV
13453 && CHARPOS (startp) <= ZV)
13454 {
13455 w->optional_new_start = Qnil;
13456 start_display (&it, w, startp);
13457 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13458 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13459 if (IT_CHARPOS (it) == PT)
13460 w->force_start = Qt;
13461 /* IT may overshoot PT if text at PT is invisible. */
13462 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13463 w->force_start = Qt;
13464 }
13465
13466 force_start:
13467
13468 /* Handle case where place to start displaying has been specified,
13469 unless the specified location is outside the accessible range. */
13470 if (!NILP (w->force_start)
13471 || w->frozen_window_start_p)
13472 {
13473 /* We set this later on if we have to adjust point. */
13474 int new_vpos = -1;
13475
13476 w->force_start = Qnil;
13477 w->vscroll = 0;
13478 w->window_end_valid = Qnil;
13479
13480 /* Forget any recorded base line for line number display. */
13481 if (!buffer_unchanged_p)
13482 w->base_line_number = Qnil;
13483
13484 /* Redisplay the mode line. Select the buffer properly for that.
13485 Also, run the hook window-scroll-functions
13486 because we have scrolled. */
13487 /* Note, we do this after clearing force_start because
13488 if there's an error, it is better to forget about force_start
13489 than to get into an infinite loop calling the hook functions
13490 and having them get more errors. */
13491 if (!update_mode_line
13492 || ! NILP (Vwindow_scroll_functions))
13493 {
13494 update_mode_line = 1;
13495 w->update_mode_line = Qt;
13496 startp = run_window_scroll_functions (window, startp);
13497 }
13498
13499 w->last_modified = make_number (0);
13500 w->last_overlay_modified = make_number (0);
13501 if (CHARPOS (startp) < BEGV)
13502 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13503 else if (CHARPOS (startp) > ZV)
13504 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13505
13506 /* Redisplay, then check if cursor has been set during the
13507 redisplay. Give up if new fonts were loaded. */
13508 /* We used to issue a CHECK_MARGINS argument to try_window here,
13509 but this causes scrolling to fail when point begins inside
13510 the scroll margin (bug#148) -- cyd */
13511 if (!try_window (window, startp, 0))
13512 {
13513 w->force_start = Qt;
13514 clear_glyph_matrix (w->desired_matrix);
13515 goto need_larger_matrices;
13516 }
13517
13518 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13519 {
13520 /* If point does not appear, try to move point so it does
13521 appear. The desired matrix has been built above, so we
13522 can use it here. */
13523 new_vpos = window_box_height (w) / 2;
13524 }
13525
13526 if (!cursor_row_fully_visible_p (w, 0, 0))
13527 {
13528 /* Point does appear, but on a line partly visible at end of window.
13529 Move it back to a fully-visible line. */
13530 new_vpos = window_box_height (w);
13531 }
13532
13533 /* If we need to move point for either of the above reasons,
13534 now actually do it. */
13535 if (new_vpos >= 0)
13536 {
13537 struct glyph_row *row;
13538
13539 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13540 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13541 ++row;
13542
13543 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13544 MATRIX_ROW_START_BYTEPOS (row));
13545
13546 if (w != XWINDOW (selected_window))
13547 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13548 else if (current_buffer == old)
13549 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13550
13551 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13552
13553 /* If we are highlighting the region, then we just changed
13554 the region, so redisplay to show it. */
13555 if (!NILP (Vtransient_mark_mode)
13556 && !NILP (current_buffer->mark_active))
13557 {
13558 clear_glyph_matrix (w->desired_matrix);
13559 if (!try_window (window, startp, 0))
13560 goto need_larger_matrices;
13561 }
13562 }
13563
13564 #if GLYPH_DEBUG
13565 debug_method_add (w, "forced window start");
13566 #endif
13567 goto done;
13568 }
13569
13570 /* Handle case where text has not changed, only point, and it has
13571 not moved off the frame, and we are not retrying after hscroll.
13572 (current_matrix_up_to_date_p is nonzero when retrying.) */
13573 if (current_matrix_up_to_date_p
13574 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13575 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13576 {
13577 switch (rc)
13578 {
13579 case CURSOR_MOVEMENT_SUCCESS:
13580 used_current_matrix_p = 1;
13581 goto done;
13582
13583 #if 0 /* try_cursor_movement never returns this value. */
13584 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13585 goto need_larger_matrices;
13586 #endif
13587
13588 case CURSOR_MOVEMENT_MUST_SCROLL:
13589 goto try_to_scroll;
13590
13591 default:
13592 abort ();
13593 }
13594 }
13595 /* If current starting point was originally the beginning of a line
13596 but no longer is, find a new starting point. */
13597 else if (!NILP (w->start_at_line_beg)
13598 && !(CHARPOS (startp) <= BEGV
13599 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13600 {
13601 #if GLYPH_DEBUG
13602 debug_method_add (w, "recenter 1");
13603 #endif
13604 goto recenter;
13605 }
13606
13607 /* Try scrolling with try_window_id. Value is > 0 if update has
13608 been done, it is -1 if we know that the same window start will
13609 not work. It is 0 if unsuccessful for some other reason. */
13610 else if ((tem = try_window_id (w)) != 0)
13611 {
13612 #if GLYPH_DEBUG
13613 debug_method_add (w, "try_window_id %d", tem);
13614 #endif
13615
13616 if (fonts_changed_p)
13617 goto need_larger_matrices;
13618 if (tem > 0)
13619 goto done;
13620
13621 /* Otherwise try_window_id has returned -1 which means that we
13622 don't want the alternative below this comment to execute. */
13623 }
13624 else if (CHARPOS (startp) >= BEGV
13625 && CHARPOS (startp) <= ZV
13626 && PT >= CHARPOS (startp)
13627 && (CHARPOS (startp) < ZV
13628 /* Avoid starting at end of buffer. */
13629 || CHARPOS (startp) == BEGV
13630 || (XFASTINT (w->last_modified) >= MODIFF
13631 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13632 {
13633
13634 /* If first window line is a continuation line, and window start
13635 is inside the modified region, but the first change is before
13636 current window start, we must select a new window start.
13637
13638 However, if this is the result of a down-mouse event (e.g. by
13639 extending the mouse-drag-overlay), we don't want to select a
13640 new window start, since that would change the position under
13641 the mouse, resulting in an unwanted mouse-movement rather
13642 than a simple mouse-click. */
13643 if (NILP (w->start_at_line_beg)
13644 && NILP (do_mouse_tracking)
13645 && CHARPOS (startp) > BEGV
13646 && CHARPOS (startp) > BEG + beg_unchanged
13647 && CHARPOS (startp) <= Z - end_unchanged
13648 /* Even if w->start_at_line_beg is nil, a new window may
13649 start at a line_beg, since that's how set_buffer_window
13650 sets it. So, we need to check the return value of
13651 compute_window_start_on_continuation_line. (See also
13652 bug#197). */
13653 && XMARKER (w->start)->buffer == current_buffer
13654 && compute_window_start_on_continuation_line (w))
13655 {
13656 w->force_start = Qt;
13657 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13658 goto force_start;
13659 }
13660
13661 #if GLYPH_DEBUG
13662 debug_method_add (w, "same window start");
13663 #endif
13664
13665 /* Try to redisplay starting at same place as before.
13666 If point has not moved off frame, accept the results. */
13667 if (!current_matrix_up_to_date_p
13668 /* Don't use try_window_reusing_current_matrix in this case
13669 because a window scroll function can have changed the
13670 buffer. */
13671 || !NILP (Vwindow_scroll_functions)
13672 || MINI_WINDOW_P (w)
13673 || !(used_current_matrix_p
13674 = try_window_reusing_current_matrix (w)))
13675 {
13676 IF_DEBUG (debug_method_add (w, "1"));
13677 if (try_window (window, startp, 1) < 0)
13678 /* -1 means we need to scroll.
13679 0 means we need new matrices, but fonts_changed_p
13680 is set in that case, so we will detect it below. */
13681 goto try_to_scroll;
13682 }
13683
13684 if (fonts_changed_p)
13685 goto need_larger_matrices;
13686
13687 if (w->cursor.vpos >= 0)
13688 {
13689 if (!just_this_one_p
13690 || current_buffer->clip_changed
13691 || BEG_UNCHANGED < CHARPOS (startp))
13692 /* Forget any recorded base line for line number display. */
13693 w->base_line_number = Qnil;
13694
13695 if (!cursor_row_fully_visible_p (w, 1, 0))
13696 {
13697 clear_glyph_matrix (w->desired_matrix);
13698 last_line_misfit = 1;
13699 }
13700 /* Drop through and scroll. */
13701 else
13702 goto done;
13703 }
13704 else
13705 clear_glyph_matrix (w->desired_matrix);
13706 }
13707
13708 try_to_scroll:
13709
13710 w->last_modified = make_number (0);
13711 w->last_overlay_modified = make_number (0);
13712
13713 /* Redisplay the mode line. Select the buffer properly for that. */
13714 if (!update_mode_line)
13715 {
13716 update_mode_line = 1;
13717 w->update_mode_line = Qt;
13718 }
13719
13720 /* Try to scroll by specified few lines. */
13721 if ((scroll_conservatively
13722 || scroll_step
13723 || temp_scroll_step
13724 || NUMBERP (current_buffer->scroll_up_aggressively)
13725 || NUMBERP (current_buffer->scroll_down_aggressively))
13726 && !current_buffer->clip_changed
13727 && CHARPOS (startp) >= BEGV
13728 && CHARPOS (startp) <= ZV)
13729 {
13730 /* The function returns -1 if new fonts were loaded, 1 if
13731 successful, 0 if not successful. */
13732 int rc = try_scrolling (window, just_this_one_p,
13733 scroll_conservatively,
13734 scroll_step,
13735 temp_scroll_step, last_line_misfit);
13736 switch (rc)
13737 {
13738 case SCROLLING_SUCCESS:
13739 goto done;
13740
13741 case SCROLLING_NEED_LARGER_MATRICES:
13742 goto need_larger_matrices;
13743
13744 case SCROLLING_FAILED:
13745 break;
13746
13747 default:
13748 abort ();
13749 }
13750 }
13751
13752 /* Finally, just choose place to start which centers point */
13753
13754 recenter:
13755 if (centering_position < 0)
13756 centering_position = window_box_height (w) / 2;
13757
13758 #if GLYPH_DEBUG
13759 debug_method_add (w, "recenter");
13760 #endif
13761
13762 /* w->vscroll = 0; */
13763
13764 /* Forget any previously recorded base line for line number display. */
13765 if (!buffer_unchanged_p)
13766 w->base_line_number = Qnil;
13767
13768 /* Move backward half the height of the window. */
13769 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13770 it.current_y = it.last_visible_y;
13771 move_it_vertically_backward (&it, centering_position);
13772 xassert (IT_CHARPOS (it) >= BEGV);
13773
13774 /* The function move_it_vertically_backward may move over more
13775 than the specified y-distance. If it->w is small, e.g. a
13776 mini-buffer window, we may end up in front of the window's
13777 display area. Start displaying at the start of the line
13778 containing PT in this case. */
13779 if (it.current_y <= 0)
13780 {
13781 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13782 move_it_vertically_backward (&it, 0);
13783 it.current_y = 0;
13784 }
13785
13786 it.current_x = it.hpos = 0;
13787
13788 /* Set startp here explicitly in case that helps avoid an infinite loop
13789 in case the window-scroll-functions functions get errors. */
13790 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13791
13792 /* Run scroll hooks. */
13793 startp = run_window_scroll_functions (window, it.current.pos);
13794
13795 /* Redisplay the window. */
13796 if (!current_matrix_up_to_date_p
13797 || windows_or_buffers_changed
13798 || cursor_type_changed
13799 /* Don't use try_window_reusing_current_matrix in this case
13800 because it can have changed the buffer. */
13801 || !NILP (Vwindow_scroll_functions)
13802 || !just_this_one_p
13803 || MINI_WINDOW_P (w)
13804 || !(used_current_matrix_p
13805 = try_window_reusing_current_matrix (w)))
13806 try_window (window, startp, 0);
13807
13808 /* If new fonts have been loaded (due to fontsets), give up. We
13809 have to start a new redisplay since we need to re-adjust glyph
13810 matrices. */
13811 if (fonts_changed_p)
13812 goto need_larger_matrices;
13813
13814 /* If cursor did not appear assume that the middle of the window is
13815 in the first line of the window. Do it again with the next line.
13816 (Imagine a window of height 100, displaying two lines of height
13817 60. Moving back 50 from it->last_visible_y will end in the first
13818 line.) */
13819 if (w->cursor.vpos < 0)
13820 {
13821 if (!NILP (w->window_end_valid)
13822 && PT >= Z - XFASTINT (w->window_end_pos))
13823 {
13824 clear_glyph_matrix (w->desired_matrix);
13825 move_it_by_lines (&it, 1, 0);
13826 try_window (window, it.current.pos, 0);
13827 }
13828 else if (PT < IT_CHARPOS (it))
13829 {
13830 clear_glyph_matrix (w->desired_matrix);
13831 move_it_by_lines (&it, -1, 0);
13832 try_window (window, it.current.pos, 0);
13833 }
13834 else
13835 {
13836 /* Not much we can do about it. */
13837 }
13838 }
13839
13840 /* Consider the following case: Window starts at BEGV, there is
13841 invisible, intangible text at BEGV, so that display starts at
13842 some point START > BEGV. It can happen that we are called with
13843 PT somewhere between BEGV and START. Try to handle that case. */
13844 if (w->cursor.vpos < 0)
13845 {
13846 struct glyph_row *row = w->current_matrix->rows;
13847 if (row->mode_line_p)
13848 ++row;
13849 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13850 }
13851
13852 if (!cursor_row_fully_visible_p (w, 0, 0))
13853 {
13854 /* If vscroll is enabled, disable it and try again. */
13855 if (w->vscroll)
13856 {
13857 w->vscroll = 0;
13858 clear_glyph_matrix (w->desired_matrix);
13859 goto recenter;
13860 }
13861
13862 /* If centering point failed to make the whole line visible,
13863 put point at the top instead. That has to make the whole line
13864 visible, if it can be done. */
13865 if (centering_position == 0)
13866 goto done;
13867
13868 clear_glyph_matrix (w->desired_matrix);
13869 centering_position = 0;
13870 goto recenter;
13871 }
13872
13873 done:
13874
13875 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13876 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13877 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13878 ? Qt : Qnil);
13879
13880 /* Display the mode line, if we must. */
13881 if ((update_mode_line
13882 /* If window not full width, must redo its mode line
13883 if (a) the window to its side is being redone and
13884 (b) we do a frame-based redisplay. This is a consequence
13885 of how inverted lines are drawn in frame-based redisplay. */
13886 || (!just_this_one_p
13887 && !FRAME_WINDOW_P (f)
13888 && !WINDOW_FULL_WIDTH_P (w))
13889 /* Line number to display. */
13890 || INTEGERP (w->base_line_pos)
13891 /* Column number is displayed and different from the one displayed. */
13892 || (!NILP (w->column_number_displayed)
13893 && (XFASTINT (w->column_number_displayed)
13894 != (int) current_column ()))) /* iftc */
13895 /* This means that the window has a mode line. */
13896 && (WINDOW_WANTS_MODELINE_P (w)
13897 || WINDOW_WANTS_HEADER_LINE_P (w)))
13898 {
13899 display_mode_lines (w);
13900
13901 /* If mode line height has changed, arrange for a thorough
13902 immediate redisplay using the correct mode line height. */
13903 if (WINDOW_WANTS_MODELINE_P (w)
13904 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13905 {
13906 fonts_changed_p = 1;
13907 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13908 = DESIRED_MODE_LINE_HEIGHT (w);
13909 }
13910
13911 /* If top line height has changed, arrange for a thorough
13912 immediate redisplay using the correct mode line height. */
13913 if (WINDOW_WANTS_HEADER_LINE_P (w)
13914 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13915 {
13916 fonts_changed_p = 1;
13917 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13918 = DESIRED_HEADER_LINE_HEIGHT (w);
13919 }
13920
13921 if (fonts_changed_p)
13922 goto need_larger_matrices;
13923 }
13924
13925 if (!line_number_displayed
13926 && !BUFFERP (w->base_line_pos))
13927 {
13928 w->base_line_pos = Qnil;
13929 w->base_line_number = Qnil;
13930 }
13931
13932 finish_menu_bars:
13933
13934 /* When we reach a frame's selected window, redo the frame's menu bar. */
13935 if (update_mode_line
13936 && EQ (FRAME_SELECTED_WINDOW (f), window))
13937 {
13938 int redisplay_menu_p = 0;
13939 int redisplay_tool_bar_p = 0;
13940
13941 if (FRAME_WINDOW_P (f))
13942 {
13943 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13944 || defined (HAVE_NS) || defined (USE_GTK)
13945 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13946 #else
13947 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13948 #endif
13949 }
13950 else
13951 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13952
13953 if (redisplay_menu_p)
13954 display_menu_bar (w);
13955
13956 #ifdef HAVE_WINDOW_SYSTEM
13957 if (FRAME_WINDOW_P (f))
13958 {
13959 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
13960 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13961 #else
13962 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13963 && (FRAME_TOOL_BAR_LINES (f) > 0
13964 || !NILP (Vauto_resize_tool_bars));
13965 #endif
13966
13967 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13968 {
13969 extern int ignore_mouse_drag_p;
13970 ignore_mouse_drag_p = 1;
13971 }
13972 }
13973 #endif
13974 }
13975
13976 #ifdef HAVE_WINDOW_SYSTEM
13977 if (FRAME_WINDOW_P (f)
13978 && update_window_fringes (w, (just_this_one_p
13979 || (!used_current_matrix_p && !overlay_arrow_seen)
13980 || w->pseudo_window_p)))
13981 {
13982 update_begin (f);
13983 BLOCK_INPUT;
13984 if (draw_window_fringes (w, 1))
13985 x_draw_vertical_border (w);
13986 UNBLOCK_INPUT;
13987 update_end (f);
13988 }
13989 #endif /* HAVE_WINDOW_SYSTEM */
13990
13991 /* We go to this label, with fonts_changed_p nonzero,
13992 if it is necessary to try again using larger glyph matrices.
13993 We have to redeem the scroll bar even in this case,
13994 because the loop in redisplay_internal expects that. */
13995 need_larger_matrices:
13996 ;
13997 finish_scroll_bars:
13998
13999 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14000 {
14001 /* Set the thumb's position and size. */
14002 set_vertical_scroll_bar (w);
14003
14004 /* Note that we actually used the scroll bar attached to this
14005 window, so it shouldn't be deleted at the end of redisplay. */
14006 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14007 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14008 }
14009
14010 /* Restore current_buffer and value of point in it. */
14011 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14012 set_buffer_internal_1 (old);
14013 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14014 shorter. This can be caused by log truncation in *Messages*. */
14015 if (CHARPOS (lpoint) <= ZV)
14016 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14017
14018 unbind_to (count, Qnil);
14019 }
14020
14021
14022 /* Build the complete desired matrix of WINDOW with a window start
14023 buffer position POS.
14024
14025 Value is 1 if successful. It is zero if fonts were loaded during
14026 redisplay which makes re-adjusting glyph matrices necessary, and -1
14027 if point would appear in the scroll margins.
14028 (We check that only if CHECK_MARGINS is nonzero. */
14029
14030 int
14031 try_window (window, pos, check_margins)
14032 Lisp_Object window;
14033 struct text_pos pos;
14034 int check_margins;
14035 {
14036 struct window *w = XWINDOW (window);
14037 struct it it;
14038 struct glyph_row *last_text_row = NULL;
14039 struct frame *f = XFRAME (w->frame);
14040
14041 /* Make POS the new window start. */
14042 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14043
14044 /* Mark cursor position as unknown. No overlay arrow seen. */
14045 w->cursor.vpos = -1;
14046 overlay_arrow_seen = 0;
14047
14048 /* Initialize iterator and info to start at POS. */
14049 start_display (&it, w, pos);
14050
14051 /* Display all lines of W. */
14052 while (it.current_y < it.last_visible_y)
14053 {
14054 if (display_line (&it))
14055 last_text_row = it.glyph_row - 1;
14056 if (fonts_changed_p)
14057 return 0;
14058 }
14059
14060 /* Don't let the cursor end in the scroll margins. */
14061 if (check_margins
14062 && !MINI_WINDOW_P (w))
14063 {
14064 int this_scroll_margin;
14065
14066 if (scroll_margin > 0)
14067 {
14068 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14069 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14070 }
14071 else
14072 this_scroll_margin = 0;
14073
14074 if ((w->cursor.y >= 0 /* not vscrolled */
14075 && w->cursor.y < this_scroll_margin
14076 && CHARPOS (pos) > BEGV
14077 && IT_CHARPOS (it) < ZV)
14078 /* rms: considering make_cursor_line_fully_visible_p here
14079 seems to give wrong results. We don't want to recenter
14080 when the last line is partly visible, we want to allow
14081 that case to be handled in the usual way. */
14082 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14083 {
14084 w->cursor.vpos = -1;
14085 clear_glyph_matrix (w->desired_matrix);
14086 return -1;
14087 }
14088 }
14089
14090 /* If bottom moved off end of frame, change mode line percentage. */
14091 if (XFASTINT (w->window_end_pos) <= 0
14092 && Z != IT_CHARPOS (it))
14093 w->update_mode_line = Qt;
14094
14095 /* Set window_end_pos to the offset of the last character displayed
14096 on the window from the end of current_buffer. Set
14097 window_end_vpos to its row number. */
14098 if (last_text_row)
14099 {
14100 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14101 w->window_end_bytepos
14102 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14103 w->window_end_pos
14104 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14105 w->window_end_vpos
14106 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14107 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14108 ->displays_text_p);
14109 }
14110 else
14111 {
14112 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14113 w->window_end_pos = make_number (Z - ZV);
14114 w->window_end_vpos = make_number (0);
14115 }
14116
14117 /* But that is not valid info until redisplay finishes. */
14118 w->window_end_valid = Qnil;
14119 return 1;
14120 }
14121
14122
14123 \f
14124 /************************************************************************
14125 Window redisplay reusing current matrix when buffer has not changed
14126 ************************************************************************/
14127
14128 /* Try redisplay of window W showing an unchanged buffer with a
14129 different window start than the last time it was displayed by
14130 reusing its current matrix. Value is non-zero if successful.
14131 W->start is the new window start. */
14132
14133 static int
14134 try_window_reusing_current_matrix (w)
14135 struct window *w;
14136 {
14137 struct frame *f = XFRAME (w->frame);
14138 struct glyph_row *row, *bottom_row;
14139 struct it it;
14140 struct run run;
14141 struct text_pos start, new_start;
14142 int nrows_scrolled, i;
14143 struct glyph_row *last_text_row;
14144 struct glyph_row *last_reused_text_row;
14145 struct glyph_row *start_row;
14146 int start_vpos, min_y, max_y;
14147
14148 #if GLYPH_DEBUG
14149 if (inhibit_try_window_reusing)
14150 return 0;
14151 #endif
14152
14153 if (/* This function doesn't handle terminal frames. */
14154 !FRAME_WINDOW_P (f)
14155 /* Don't try to reuse the display if windows have been split
14156 or such. */
14157 || windows_or_buffers_changed
14158 || cursor_type_changed)
14159 return 0;
14160
14161 /* Can't do this if region may have changed. */
14162 if ((!NILP (Vtransient_mark_mode)
14163 && !NILP (current_buffer->mark_active))
14164 || !NILP (w->region_showing)
14165 || !NILP (Vshow_trailing_whitespace))
14166 return 0;
14167
14168 /* If top-line visibility has changed, give up. */
14169 if (WINDOW_WANTS_HEADER_LINE_P (w)
14170 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14171 return 0;
14172
14173 /* Give up if old or new display is scrolled vertically. We could
14174 make this function handle this, but right now it doesn't. */
14175 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14176 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14177 return 0;
14178
14179 /* The variable new_start now holds the new window start. The old
14180 start `start' can be determined from the current matrix. */
14181 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14182 start = start_row->start.pos;
14183 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14184
14185 /* Clear the desired matrix for the display below. */
14186 clear_glyph_matrix (w->desired_matrix);
14187
14188 if (CHARPOS (new_start) <= CHARPOS (start))
14189 {
14190 int first_row_y;
14191
14192 /* Don't use this method if the display starts with an ellipsis
14193 displayed for invisible text. It's not easy to handle that case
14194 below, and it's certainly not worth the effort since this is
14195 not a frequent case. */
14196 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14197 return 0;
14198
14199 IF_DEBUG (debug_method_add (w, "twu1"));
14200
14201 /* Display up to a row that can be reused. The variable
14202 last_text_row is set to the last row displayed that displays
14203 text. Note that it.vpos == 0 if or if not there is a
14204 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14205 start_display (&it, w, new_start);
14206 first_row_y = it.current_y;
14207 w->cursor.vpos = -1;
14208 last_text_row = last_reused_text_row = NULL;
14209
14210 while (it.current_y < it.last_visible_y
14211 && !fonts_changed_p)
14212 {
14213 /* If we have reached into the characters in the START row,
14214 that means the line boundaries have changed. So we
14215 can't start copying with the row START. Maybe it will
14216 work to start copying with the following row. */
14217 while (IT_CHARPOS (it) > CHARPOS (start))
14218 {
14219 /* Advance to the next row as the "start". */
14220 start_row++;
14221 start = start_row->start.pos;
14222 /* If there are no more rows to try, or just one, give up. */
14223 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14224 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14225 || CHARPOS (start) == ZV)
14226 {
14227 clear_glyph_matrix (w->desired_matrix);
14228 return 0;
14229 }
14230
14231 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14232 }
14233 /* If we have reached alignment,
14234 we can copy the rest of the rows. */
14235 if (IT_CHARPOS (it) == CHARPOS (start))
14236 break;
14237
14238 if (display_line (&it))
14239 last_text_row = it.glyph_row - 1;
14240 }
14241
14242 /* A value of current_y < last_visible_y means that we stopped
14243 at the previous window start, which in turn means that we
14244 have at least one reusable row. */
14245 if (it.current_y < it.last_visible_y)
14246 {
14247 /* IT.vpos always starts from 0; it counts text lines. */
14248 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14249
14250 /* Find PT if not already found in the lines displayed. */
14251 if (w->cursor.vpos < 0)
14252 {
14253 int dy = it.current_y - start_row->y;
14254
14255 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14256 row = row_containing_pos (w, PT, row, NULL, dy);
14257 if (row)
14258 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14259 dy, nrows_scrolled);
14260 else
14261 {
14262 clear_glyph_matrix (w->desired_matrix);
14263 return 0;
14264 }
14265 }
14266
14267 /* Scroll the display. Do it before the current matrix is
14268 changed. The problem here is that update has not yet
14269 run, i.e. part of the current matrix is not up to date.
14270 scroll_run_hook will clear the cursor, and use the
14271 current matrix to get the height of the row the cursor is
14272 in. */
14273 run.current_y = start_row->y;
14274 run.desired_y = it.current_y;
14275 run.height = it.last_visible_y - it.current_y;
14276
14277 if (run.height > 0 && run.current_y != run.desired_y)
14278 {
14279 update_begin (f);
14280 FRAME_RIF (f)->update_window_begin_hook (w);
14281 FRAME_RIF (f)->clear_window_mouse_face (w);
14282 FRAME_RIF (f)->scroll_run_hook (w, &run);
14283 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14284 update_end (f);
14285 }
14286
14287 /* Shift current matrix down by nrows_scrolled lines. */
14288 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14289 rotate_matrix (w->current_matrix,
14290 start_vpos,
14291 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14292 nrows_scrolled);
14293
14294 /* Disable lines that must be updated. */
14295 for (i = 0; i < nrows_scrolled; ++i)
14296 (start_row + i)->enabled_p = 0;
14297
14298 /* Re-compute Y positions. */
14299 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14300 max_y = it.last_visible_y;
14301 for (row = start_row + nrows_scrolled;
14302 row < bottom_row;
14303 ++row)
14304 {
14305 row->y = it.current_y;
14306 row->visible_height = row->height;
14307
14308 if (row->y < min_y)
14309 row->visible_height -= min_y - row->y;
14310 if (row->y + row->height > max_y)
14311 row->visible_height -= row->y + row->height - max_y;
14312 row->redraw_fringe_bitmaps_p = 1;
14313
14314 it.current_y += row->height;
14315
14316 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14317 last_reused_text_row = row;
14318 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14319 break;
14320 }
14321
14322 /* Disable lines in the current matrix which are now
14323 below the window. */
14324 for (++row; row < bottom_row; ++row)
14325 row->enabled_p = row->mode_line_p = 0;
14326 }
14327
14328 /* Update window_end_pos etc.; last_reused_text_row is the last
14329 reused row from the current matrix containing text, if any.
14330 The value of last_text_row is the last displayed line
14331 containing text. */
14332 if (last_reused_text_row)
14333 {
14334 w->window_end_bytepos
14335 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14336 w->window_end_pos
14337 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14338 w->window_end_vpos
14339 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14340 w->current_matrix));
14341 }
14342 else if (last_text_row)
14343 {
14344 w->window_end_bytepos
14345 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14346 w->window_end_pos
14347 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14348 w->window_end_vpos
14349 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14350 }
14351 else
14352 {
14353 /* This window must be completely empty. */
14354 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14355 w->window_end_pos = make_number (Z - ZV);
14356 w->window_end_vpos = make_number (0);
14357 }
14358 w->window_end_valid = Qnil;
14359
14360 /* Update hint: don't try scrolling again in update_window. */
14361 w->desired_matrix->no_scrolling_p = 1;
14362
14363 #if GLYPH_DEBUG
14364 debug_method_add (w, "try_window_reusing_current_matrix 1");
14365 #endif
14366 return 1;
14367 }
14368 else if (CHARPOS (new_start) > CHARPOS (start))
14369 {
14370 struct glyph_row *pt_row, *row;
14371 struct glyph_row *first_reusable_row;
14372 struct glyph_row *first_row_to_display;
14373 int dy;
14374 int yb = window_text_bottom_y (w);
14375
14376 /* Find the row starting at new_start, if there is one. Don't
14377 reuse a partially visible line at the end. */
14378 first_reusable_row = start_row;
14379 while (first_reusable_row->enabled_p
14380 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14381 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14382 < CHARPOS (new_start)))
14383 ++first_reusable_row;
14384
14385 /* Give up if there is no row to reuse. */
14386 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14387 || !first_reusable_row->enabled_p
14388 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14389 != CHARPOS (new_start)))
14390 return 0;
14391
14392 /* We can reuse fully visible rows beginning with
14393 first_reusable_row to the end of the window. Set
14394 first_row_to_display to the first row that cannot be reused.
14395 Set pt_row to the row containing point, if there is any. */
14396 pt_row = NULL;
14397 for (first_row_to_display = first_reusable_row;
14398 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14399 ++first_row_to_display)
14400 {
14401 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14402 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14403 pt_row = first_row_to_display;
14404 }
14405
14406 /* Start displaying at the start of first_row_to_display. */
14407 xassert (first_row_to_display->y < yb);
14408 init_to_row_start (&it, w, first_row_to_display);
14409
14410 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14411 - start_vpos);
14412 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14413 - nrows_scrolled);
14414 it.current_y = (first_row_to_display->y - first_reusable_row->y
14415 + WINDOW_HEADER_LINE_HEIGHT (w));
14416
14417 /* Display lines beginning with first_row_to_display in the
14418 desired matrix. Set last_text_row to the last row displayed
14419 that displays text. */
14420 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14421 if (pt_row == NULL)
14422 w->cursor.vpos = -1;
14423 last_text_row = NULL;
14424 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14425 if (display_line (&it))
14426 last_text_row = it.glyph_row - 1;
14427
14428 /* If point is in a reused row, adjust y and vpos of the cursor
14429 position. */
14430 if (pt_row)
14431 {
14432 w->cursor.vpos -= nrows_scrolled;
14433 w->cursor.y -= first_reusable_row->y - start_row->y;
14434 }
14435
14436 /* Give up if point isn't in a row displayed or reused. (This
14437 also handles the case where w->cursor.vpos < nrows_scrolled
14438 after the calls to display_line, which can happen with scroll
14439 margins. See bug#1295.) */
14440 if (w->cursor.vpos < 0)
14441 {
14442 clear_glyph_matrix (w->desired_matrix);
14443 return 0;
14444 }
14445
14446 /* Scroll the display. */
14447 run.current_y = first_reusable_row->y;
14448 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14449 run.height = it.last_visible_y - run.current_y;
14450 dy = run.current_y - run.desired_y;
14451
14452 if (run.height)
14453 {
14454 update_begin (f);
14455 FRAME_RIF (f)->update_window_begin_hook (w);
14456 FRAME_RIF (f)->clear_window_mouse_face (w);
14457 FRAME_RIF (f)->scroll_run_hook (w, &run);
14458 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14459 update_end (f);
14460 }
14461
14462 /* Adjust Y positions of reused rows. */
14463 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14464 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14465 max_y = it.last_visible_y;
14466 for (row = first_reusable_row; row < first_row_to_display; ++row)
14467 {
14468 row->y -= dy;
14469 row->visible_height = row->height;
14470 if (row->y < min_y)
14471 row->visible_height -= min_y - row->y;
14472 if (row->y + row->height > max_y)
14473 row->visible_height -= row->y + row->height - max_y;
14474 row->redraw_fringe_bitmaps_p = 1;
14475 }
14476
14477 /* Scroll the current matrix. */
14478 xassert (nrows_scrolled > 0);
14479 rotate_matrix (w->current_matrix,
14480 start_vpos,
14481 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14482 -nrows_scrolled);
14483
14484 /* Disable rows not reused. */
14485 for (row -= nrows_scrolled; row < bottom_row; ++row)
14486 row->enabled_p = 0;
14487
14488 /* Point may have moved to a different line, so we cannot assume that
14489 the previous cursor position is valid; locate the correct row. */
14490 if (pt_row)
14491 {
14492 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14493 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14494 row++)
14495 {
14496 w->cursor.vpos++;
14497 w->cursor.y = row->y;
14498 }
14499 if (row < bottom_row)
14500 {
14501 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14502 struct glyph *end = glyph + row->used[TEXT_AREA];
14503
14504 for (; glyph < end
14505 && (!BUFFERP (glyph->object)
14506 || glyph->charpos < PT);
14507 glyph++)
14508 {
14509 w->cursor.hpos++;
14510 w->cursor.x += glyph->pixel_width;
14511 }
14512 }
14513 }
14514
14515 /* Adjust window end. A null value of last_text_row means that
14516 the window end is in reused rows which in turn means that
14517 only its vpos can have changed. */
14518 if (last_text_row)
14519 {
14520 w->window_end_bytepos
14521 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14522 w->window_end_pos
14523 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14524 w->window_end_vpos
14525 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14526 }
14527 else
14528 {
14529 w->window_end_vpos
14530 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14531 }
14532
14533 w->window_end_valid = Qnil;
14534 w->desired_matrix->no_scrolling_p = 1;
14535
14536 #if GLYPH_DEBUG
14537 debug_method_add (w, "try_window_reusing_current_matrix 2");
14538 #endif
14539 return 1;
14540 }
14541
14542 return 0;
14543 }
14544
14545
14546 \f
14547 /************************************************************************
14548 Window redisplay reusing current matrix when buffer has changed
14549 ************************************************************************/
14550
14551 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14552 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14553 int *, int *));
14554 static struct glyph_row *
14555 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14556 struct glyph_row *));
14557
14558
14559 /* Return the last row in MATRIX displaying text. If row START is
14560 non-null, start searching with that row. IT gives the dimensions
14561 of the display. Value is null if matrix is empty; otherwise it is
14562 a pointer to the row found. */
14563
14564 static struct glyph_row *
14565 find_last_row_displaying_text (matrix, it, start)
14566 struct glyph_matrix *matrix;
14567 struct it *it;
14568 struct glyph_row *start;
14569 {
14570 struct glyph_row *row, *row_found;
14571
14572 /* Set row_found to the last row in IT->w's current matrix
14573 displaying text. The loop looks funny but think of partially
14574 visible lines. */
14575 row_found = NULL;
14576 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14577 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14578 {
14579 xassert (row->enabled_p);
14580 row_found = row;
14581 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14582 break;
14583 ++row;
14584 }
14585
14586 return row_found;
14587 }
14588
14589
14590 /* Return the last row in the current matrix of W that is not affected
14591 by changes at the start of current_buffer that occurred since W's
14592 current matrix was built. Value is null if no such row exists.
14593
14594 BEG_UNCHANGED us the number of characters unchanged at the start of
14595 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14596 first changed character in current_buffer. Characters at positions <
14597 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14598 when the current matrix was built. */
14599
14600 static struct glyph_row *
14601 find_last_unchanged_at_beg_row (w)
14602 struct window *w;
14603 {
14604 int first_changed_pos = BEG + BEG_UNCHANGED;
14605 struct glyph_row *row;
14606 struct glyph_row *row_found = NULL;
14607 int yb = window_text_bottom_y (w);
14608
14609 /* Find the last row displaying unchanged text. */
14610 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14611 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14612 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14613 ++row)
14614 {
14615 if (/* If row ends before first_changed_pos, it is unchanged,
14616 except in some case. */
14617 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14618 /* When row ends in ZV and we write at ZV it is not
14619 unchanged. */
14620 && !row->ends_at_zv_p
14621 /* When first_changed_pos is the end of a continued line,
14622 row is not unchanged because it may be no longer
14623 continued. */
14624 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14625 && (row->continued_p
14626 || row->exact_window_width_line_p)))
14627 row_found = row;
14628
14629 /* Stop if last visible row. */
14630 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14631 break;
14632 }
14633
14634 return row_found;
14635 }
14636
14637
14638 /* Find the first glyph row in the current matrix of W that is not
14639 affected by changes at the end of current_buffer since the
14640 time W's current matrix was built.
14641
14642 Return in *DELTA the number of chars by which buffer positions in
14643 unchanged text at the end of current_buffer must be adjusted.
14644
14645 Return in *DELTA_BYTES the corresponding number of bytes.
14646
14647 Value is null if no such row exists, i.e. all rows are affected by
14648 changes. */
14649
14650 static struct glyph_row *
14651 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14652 struct window *w;
14653 int *delta, *delta_bytes;
14654 {
14655 struct glyph_row *row;
14656 struct glyph_row *row_found = NULL;
14657
14658 *delta = *delta_bytes = 0;
14659
14660 /* Display must not have been paused, otherwise the current matrix
14661 is not up to date. */
14662 eassert (!NILP (w->window_end_valid));
14663
14664 /* A value of window_end_pos >= END_UNCHANGED means that the window
14665 end is in the range of changed text. If so, there is no
14666 unchanged row at the end of W's current matrix. */
14667 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14668 return NULL;
14669
14670 /* Set row to the last row in W's current matrix displaying text. */
14671 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14672
14673 /* If matrix is entirely empty, no unchanged row exists. */
14674 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14675 {
14676 /* The value of row is the last glyph row in the matrix having a
14677 meaningful buffer position in it. The end position of row
14678 corresponds to window_end_pos. This allows us to translate
14679 buffer positions in the current matrix to current buffer
14680 positions for characters not in changed text. */
14681 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14682 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14683 int last_unchanged_pos, last_unchanged_pos_old;
14684 struct glyph_row *first_text_row
14685 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14686
14687 *delta = Z - Z_old;
14688 *delta_bytes = Z_BYTE - Z_BYTE_old;
14689
14690 /* Set last_unchanged_pos to the buffer position of the last
14691 character in the buffer that has not been changed. Z is the
14692 index + 1 of the last character in current_buffer, i.e. by
14693 subtracting END_UNCHANGED we get the index of the last
14694 unchanged character, and we have to add BEG to get its buffer
14695 position. */
14696 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14697 last_unchanged_pos_old = last_unchanged_pos - *delta;
14698
14699 /* Search backward from ROW for a row displaying a line that
14700 starts at a minimum position >= last_unchanged_pos_old. */
14701 for (; row > first_text_row; --row)
14702 {
14703 /* This used to abort, but it can happen.
14704 It is ok to just stop the search instead here. KFS. */
14705 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14706 break;
14707
14708 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14709 row_found = row;
14710 }
14711 }
14712
14713 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14714
14715 return row_found;
14716 }
14717
14718
14719 /* Make sure that glyph rows in the current matrix of window W
14720 reference the same glyph memory as corresponding rows in the
14721 frame's frame matrix. This function is called after scrolling W's
14722 current matrix on a terminal frame in try_window_id and
14723 try_window_reusing_current_matrix. */
14724
14725 static void
14726 sync_frame_with_window_matrix_rows (w)
14727 struct window *w;
14728 {
14729 struct frame *f = XFRAME (w->frame);
14730 struct glyph_row *window_row, *window_row_end, *frame_row;
14731
14732 /* Preconditions: W must be a leaf window and full-width. Its frame
14733 must have a frame matrix. */
14734 xassert (NILP (w->hchild) && NILP (w->vchild));
14735 xassert (WINDOW_FULL_WIDTH_P (w));
14736 xassert (!FRAME_WINDOW_P (f));
14737
14738 /* If W is a full-width window, glyph pointers in W's current matrix
14739 have, by definition, to be the same as glyph pointers in the
14740 corresponding frame matrix. Note that frame matrices have no
14741 marginal areas (see build_frame_matrix). */
14742 window_row = w->current_matrix->rows;
14743 window_row_end = window_row + w->current_matrix->nrows;
14744 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14745 while (window_row < window_row_end)
14746 {
14747 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14748 struct glyph *end = window_row->glyphs[LAST_AREA];
14749
14750 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14751 frame_row->glyphs[TEXT_AREA] = start;
14752 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14753 frame_row->glyphs[LAST_AREA] = end;
14754
14755 /* Disable frame rows whose corresponding window rows have
14756 been disabled in try_window_id. */
14757 if (!window_row->enabled_p)
14758 frame_row->enabled_p = 0;
14759
14760 ++window_row, ++frame_row;
14761 }
14762 }
14763
14764
14765 /* Find the glyph row in window W containing CHARPOS. Consider all
14766 rows between START and END (not inclusive). END null means search
14767 all rows to the end of the display area of W. Value is the row
14768 containing CHARPOS or null. */
14769
14770 struct glyph_row *
14771 row_containing_pos (w, charpos, start, end, dy)
14772 struct window *w;
14773 int charpos;
14774 struct glyph_row *start, *end;
14775 int dy;
14776 {
14777 struct glyph_row *row = start;
14778 int last_y;
14779
14780 /* If we happen to start on a header-line, skip that. */
14781 if (row->mode_line_p)
14782 ++row;
14783
14784 if ((end && row >= end) || !row->enabled_p)
14785 return NULL;
14786
14787 last_y = window_text_bottom_y (w) - dy;
14788
14789 while (1)
14790 {
14791 /* Give up if we have gone too far. */
14792 if (end && row >= end)
14793 return NULL;
14794 /* This formerly returned if they were equal.
14795 I think that both quantities are of a "last plus one" type;
14796 if so, when they are equal, the row is within the screen. -- rms. */
14797 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14798 return NULL;
14799
14800 /* If it is in this row, return this row. */
14801 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14802 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14803 /* The end position of a row equals the start
14804 position of the next row. If CHARPOS is there, we
14805 would rather display it in the next line, except
14806 when this line ends in ZV. */
14807 && !row->ends_at_zv_p
14808 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14809 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14810 return row;
14811 ++row;
14812 }
14813 }
14814
14815
14816 /* Try to redisplay window W by reusing its existing display. W's
14817 current matrix must be up to date when this function is called,
14818 i.e. window_end_valid must not be nil.
14819
14820 Value is
14821
14822 1 if display has been updated
14823 0 if otherwise unsuccessful
14824 -1 if redisplay with same window start is known not to succeed
14825
14826 The following steps are performed:
14827
14828 1. Find the last row in the current matrix of W that is not
14829 affected by changes at the start of current_buffer. If no such row
14830 is found, give up.
14831
14832 2. Find the first row in W's current matrix that is not affected by
14833 changes at the end of current_buffer. Maybe there is no such row.
14834
14835 3. Display lines beginning with the row + 1 found in step 1 to the
14836 row found in step 2 or, if step 2 didn't find a row, to the end of
14837 the window.
14838
14839 4. If cursor is not known to appear on the window, give up.
14840
14841 5. If display stopped at the row found in step 2, scroll the
14842 display and current matrix as needed.
14843
14844 6. Maybe display some lines at the end of W, if we must. This can
14845 happen under various circumstances, like a partially visible line
14846 becoming fully visible, or because newly displayed lines are displayed
14847 in smaller font sizes.
14848
14849 7. Update W's window end information. */
14850
14851 static int
14852 try_window_id (w)
14853 struct window *w;
14854 {
14855 struct frame *f = XFRAME (w->frame);
14856 struct glyph_matrix *current_matrix = w->current_matrix;
14857 struct glyph_matrix *desired_matrix = w->desired_matrix;
14858 struct glyph_row *last_unchanged_at_beg_row;
14859 struct glyph_row *first_unchanged_at_end_row;
14860 struct glyph_row *row;
14861 struct glyph_row *bottom_row;
14862 int bottom_vpos;
14863 struct it it;
14864 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14865 struct text_pos start_pos;
14866 struct run run;
14867 int first_unchanged_at_end_vpos = 0;
14868 struct glyph_row *last_text_row, *last_text_row_at_end;
14869 struct text_pos start;
14870 int first_changed_charpos, last_changed_charpos;
14871
14872 #if GLYPH_DEBUG
14873 if (inhibit_try_window_id)
14874 return 0;
14875 #endif
14876
14877 /* This is handy for debugging. */
14878 #if 0
14879 #define GIVE_UP(X) \
14880 do { \
14881 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14882 return 0; \
14883 } while (0)
14884 #else
14885 #define GIVE_UP(X) return 0
14886 #endif
14887
14888 SET_TEXT_POS_FROM_MARKER (start, w->start);
14889
14890 /* Don't use this for mini-windows because these can show
14891 messages and mini-buffers, and we don't handle that here. */
14892 if (MINI_WINDOW_P (w))
14893 GIVE_UP (1);
14894
14895 /* This flag is used to prevent redisplay optimizations. */
14896 if (windows_or_buffers_changed || cursor_type_changed)
14897 GIVE_UP (2);
14898
14899 /* Verify that narrowing has not changed.
14900 Also verify that we were not told to prevent redisplay optimizations.
14901 It would be nice to further
14902 reduce the number of cases where this prevents try_window_id. */
14903 if (current_buffer->clip_changed
14904 || current_buffer->prevent_redisplay_optimizations_p)
14905 GIVE_UP (3);
14906
14907 /* Window must either use window-based redisplay or be full width. */
14908 if (!FRAME_WINDOW_P (f)
14909 && (!FRAME_LINE_INS_DEL_OK (f)
14910 || !WINDOW_FULL_WIDTH_P (w)))
14911 GIVE_UP (4);
14912
14913 /* Give up if point is not known NOT to appear in W. */
14914 if (PT < CHARPOS (start))
14915 GIVE_UP (5);
14916
14917 /* Another way to prevent redisplay optimizations. */
14918 if (XFASTINT (w->last_modified) == 0)
14919 GIVE_UP (6);
14920
14921 /* Verify that window is not hscrolled. */
14922 if (XFASTINT (w->hscroll) != 0)
14923 GIVE_UP (7);
14924
14925 /* Verify that display wasn't paused. */
14926 if (NILP (w->window_end_valid))
14927 GIVE_UP (8);
14928
14929 /* Can't use this if highlighting a region because a cursor movement
14930 will do more than just set the cursor. */
14931 if (!NILP (Vtransient_mark_mode)
14932 && !NILP (current_buffer->mark_active))
14933 GIVE_UP (9);
14934
14935 /* Likewise if highlighting trailing whitespace. */
14936 if (!NILP (Vshow_trailing_whitespace))
14937 GIVE_UP (11);
14938
14939 /* Likewise if showing a region. */
14940 if (!NILP (w->region_showing))
14941 GIVE_UP (10);
14942
14943 /* Can use this if overlay arrow position and or string have changed. */
14944 if (overlay_arrows_changed_p ())
14945 GIVE_UP (12);
14946
14947 /* When word-wrap is on, adding a space to the first word of a
14948 wrapped line can change the wrap position, altering the line
14949 above it. It might be worthwhile to handle this more
14950 intelligently, but for now just redisplay from scratch. */
14951 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14952 GIVE_UP (21);
14953
14954 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14955 only if buffer has really changed. The reason is that the gap is
14956 initially at Z for freshly visited files. The code below would
14957 set end_unchanged to 0 in that case. */
14958 if (MODIFF > SAVE_MODIFF
14959 /* This seems to happen sometimes after saving a buffer. */
14960 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14961 {
14962 if (GPT - BEG < BEG_UNCHANGED)
14963 BEG_UNCHANGED = GPT - BEG;
14964 if (Z - GPT < END_UNCHANGED)
14965 END_UNCHANGED = Z - GPT;
14966 }
14967
14968 /* The position of the first and last character that has been changed. */
14969 first_changed_charpos = BEG + BEG_UNCHANGED;
14970 last_changed_charpos = Z - END_UNCHANGED;
14971
14972 /* If window starts after a line end, and the last change is in
14973 front of that newline, then changes don't affect the display.
14974 This case happens with stealth-fontification. Note that although
14975 the display is unchanged, glyph positions in the matrix have to
14976 be adjusted, of course. */
14977 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14978 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14979 && ((last_changed_charpos < CHARPOS (start)
14980 && CHARPOS (start) == BEGV)
14981 || (last_changed_charpos < CHARPOS (start) - 1
14982 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14983 {
14984 int Z_old, delta, Z_BYTE_old, delta_bytes;
14985 struct glyph_row *r0;
14986
14987 /* Compute how many chars/bytes have been added to or removed
14988 from the buffer. */
14989 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14990 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14991 delta = Z - Z_old;
14992 delta_bytes = Z_BYTE - Z_BYTE_old;
14993
14994 /* Give up if PT is not in the window. Note that it already has
14995 been checked at the start of try_window_id that PT is not in
14996 front of the window start. */
14997 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14998 GIVE_UP (13);
14999
15000 /* If window start is unchanged, we can reuse the whole matrix
15001 as is, after adjusting glyph positions. No need to compute
15002 the window end again, since its offset from Z hasn't changed. */
15003 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15004 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15005 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15006 /* PT must not be in a partially visible line. */
15007 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15008 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15009 {
15010 /* Adjust positions in the glyph matrix. */
15011 if (delta || delta_bytes)
15012 {
15013 struct glyph_row *r1
15014 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15015 increment_matrix_positions (w->current_matrix,
15016 MATRIX_ROW_VPOS (r0, current_matrix),
15017 MATRIX_ROW_VPOS (r1, current_matrix),
15018 delta, delta_bytes);
15019 }
15020
15021 /* Set the cursor. */
15022 row = row_containing_pos (w, PT, r0, NULL, 0);
15023 if (row)
15024 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15025 else
15026 abort ();
15027 return 1;
15028 }
15029 }
15030
15031 /* Handle the case that changes are all below what is displayed in
15032 the window, and that PT is in the window. This shortcut cannot
15033 be taken if ZV is visible in the window, and text has been added
15034 there that is visible in the window. */
15035 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15036 /* ZV is not visible in the window, or there are no
15037 changes at ZV, actually. */
15038 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15039 || first_changed_charpos == last_changed_charpos))
15040 {
15041 struct glyph_row *r0;
15042
15043 /* Give up if PT is not in the window. Note that it already has
15044 been checked at the start of try_window_id that PT is not in
15045 front of the window start. */
15046 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15047 GIVE_UP (14);
15048
15049 /* If window start is unchanged, we can reuse the whole matrix
15050 as is, without changing glyph positions since no text has
15051 been added/removed in front of the window end. */
15052 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15053 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15054 /* PT must not be in a partially visible line. */
15055 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15056 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15057 {
15058 /* We have to compute the window end anew since text
15059 can have been added/removed after it. */
15060 w->window_end_pos
15061 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15062 w->window_end_bytepos
15063 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15064
15065 /* Set the cursor. */
15066 row = row_containing_pos (w, PT, r0, NULL, 0);
15067 if (row)
15068 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15069 else
15070 abort ();
15071 return 2;
15072 }
15073 }
15074
15075 /* Give up if window start is in the changed area.
15076
15077 The condition used to read
15078
15079 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15080
15081 but why that was tested escapes me at the moment. */
15082 if (CHARPOS (start) >= first_changed_charpos
15083 && CHARPOS (start) <= last_changed_charpos)
15084 GIVE_UP (15);
15085
15086 /* Check that window start agrees with the start of the first glyph
15087 row in its current matrix. Check this after we know the window
15088 start is not in changed text, otherwise positions would not be
15089 comparable. */
15090 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15091 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15092 GIVE_UP (16);
15093
15094 /* Give up if the window ends in strings. Overlay strings
15095 at the end are difficult to handle, so don't try. */
15096 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15097 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15098 GIVE_UP (20);
15099
15100 /* Compute the position at which we have to start displaying new
15101 lines. Some of the lines at the top of the window might be
15102 reusable because they are not displaying changed text. Find the
15103 last row in W's current matrix not affected by changes at the
15104 start of current_buffer. Value is null if changes start in the
15105 first line of window. */
15106 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15107 if (last_unchanged_at_beg_row)
15108 {
15109 /* Avoid starting to display in the moddle of a character, a TAB
15110 for instance. This is easier than to set up the iterator
15111 exactly, and it's not a frequent case, so the additional
15112 effort wouldn't really pay off. */
15113 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15114 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15115 && last_unchanged_at_beg_row > w->current_matrix->rows)
15116 --last_unchanged_at_beg_row;
15117
15118 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15119 GIVE_UP (17);
15120
15121 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15122 GIVE_UP (18);
15123 start_pos = it.current.pos;
15124
15125 /* Start displaying new lines in the desired matrix at the same
15126 vpos we would use in the current matrix, i.e. below
15127 last_unchanged_at_beg_row. */
15128 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15129 current_matrix);
15130 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15131 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15132
15133 xassert (it.hpos == 0 && it.current_x == 0);
15134 }
15135 else
15136 {
15137 /* There are no reusable lines at the start of the window.
15138 Start displaying in the first text line. */
15139 start_display (&it, w, start);
15140 it.vpos = it.first_vpos;
15141 start_pos = it.current.pos;
15142 }
15143
15144 /* Find the first row that is not affected by changes at the end of
15145 the buffer. Value will be null if there is no unchanged row, in
15146 which case we must redisplay to the end of the window. delta
15147 will be set to the value by which buffer positions beginning with
15148 first_unchanged_at_end_row have to be adjusted due to text
15149 changes. */
15150 first_unchanged_at_end_row
15151 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15152 IF_DEBUG (debug_delta = delta);
15153 IF_DEBUG (debug_delta_bytes = delta_bytes);
15154
15155 /* Set stop_pos to the buffer position up to which we will have to
15156 display new lines. If first_unchanged_at_end_row != NULL, this
15157 is the buffer position of the start of the line displayed in that
15158 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15159 that we don't stop at a buffer position. */
15160 stop_pos = 0;
15161 if (first_unchanged_at_end_row)
15162 {
15163 xassert (last_unchanged_at_beg_row == NULL
15164 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15165
15166 /* If this is a continuation line, move forward to the next one
15167 that isn't. Changes in lines above affect this line.
15168 Caution: this may move first_unchanged_at_end_row to a row
15169 not displaying text. */
15170 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15171 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15172 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15173 < it.last_visible_y))
15174 ++first_unchanged_at_end_row;
15175
15176 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15177 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15178 >= it.last_visible_y))
15179 first_unchanged_at_end_row = NULL;
15180 else
15181 {
15182 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15183 + delta);
15184 first_unchanged_at_end_vpos
15185 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15186 xassert (stop_pos >= Z - END_UNCHANGED);
15187 }
15188 }
15189 else if (last_unchanged_at_beg_row == NULL)
15190 GIVE_UP (19);
15191
15192
15193 #if GLYPH_DEBUG
15194
15195 /* Either there is no unchanged row at the end, or the one we have
15196 now displays text. This is a necessary condition for the window
15197 end pos calculation at the end of this function. */
15198 xassert (first_unchanged_at_end_row == NULL
15199 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15200
15201 debug_last_unchanged_at_beg_vpos
15202 = (last_unchanged_at_beg_row
15203 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15204 : -1);
15205 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15206
15207 #endif /* GLYPH_DEBUG != 0 */
15208
15209
15210 /* Display new lines. Set last_text_row to the last new line
15211 displayed which has text on it, i.e. might end up as being the
15212 line where the window_end_vpos is. */
15213 w->cursor.vpos = -1;
15214 last_text_row = NULL;
15215 overlay_arrow_seen = 0;
15216 while (it.current_y < it.last_visible_y
15217 && !fonts_changed_p
15218 && (first_unchanged_at_end_row == NULL
15219 || IT_CHARPOS (it) < stop_pos))
15220 {
15221 if (display_line (&it))
15222 last_text_row = it.glyph_row - 1;
15223 }
15224
15225 if (fonts_changed_p)
15226 return -1;
15227
15228
15229 /* Compute differences in buffer positions, y-positions etc. for
15230 lines reused at the bottom of the window. Compute what we can
15231 scroll. */
15232 if (first_unchanged_at_end_row
15233 /* No lines reused because we displayed everything up to the
15234 bottom of the window. */
15235 && it.current_y < it.last_visible_y)
15236 {
15237 dvpos = (it.vpos
15238 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15239 current_matrix));
15240 dy = it.current_y - first_unchanged_at_end_row->y;
15241 run.current_y = first_unchanged_at_end_row->y;
15242 run.desired_y = run.current_y + dy;
15243 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15244 }
15245 else
15246 {
15247 delta = delta_bytes = dvpos = dy
15248 = run.current_y = run.desired_y = run.height = 0;
15249 first_unchanged_at_end_row = NULL;
15250 }
15251 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15252
15253
15254 /* Find the cursor if not already found. We have to decide whether
15255 PT will appear on this window (it sometimes doesn't, but this is
15256 not a very frequent case.) This decision has to be made before
15257 the current matrix is altered. A value of cursor.vpos < 0 means
15258 that PT is either in one of the lines beginning at
15259 first_unchanged_at_end_row or below the window. Don't care for
15260 lines that might be displayed later at the window end; as
15261 mentioned, this is not a frequent case. */
15262 if (w->cursor.vpos < 0)
15263 {
15264 /* Cursor in unchanged rows at the top? */
15265 if (PT < CHARPOS (start_pos)
15266 && last_unchanged_at_beg_row)
15267 {
15268 row = row_containing_pos (w, PT,
15269 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15270 last_unchanged_at_beg_row + 1, 0);
15271 if (row)
15272 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15273 }
15274
15275 /* Start from first_unchanged_at_end_row looking for PT. */
15276 else if (first_unchanged_at_end_row)
15277 {
15278 row = row_containing_pos (w, PT - delta,
15279 first_unchanged_at_end_row, NULL, 0);
15280 if (row)
15281 set_cursor_from_row (w, row, w->current_matrix, delta,
15282 delta_bytes, dy, dvpos);
15283 }
15284
15285 /* Give up if cursor was not found. */
15286 if (w->cursor.vpos < 0)
15287 {
15288 clear_glyph_matrix (w->desired_matrix);
15289 return -1;
15290 }
15291 }
15292
15293 /* Don't let the cursor end in the scroll margins. */
15294 {
15295 int this_scroll_margin, cursor_height;
15296
15297 this_scroll_margin = max (0, scroll_margin);
15298 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15299 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15300 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15301
15302 if ((w->cursor.y < this_scroll_margin
15303 && CHARPOS (start) > BEGV)
15304 /* Old redisplay didn't take scroll margin into account at the bottom,
15305 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15306 || (w->cursor.y + (make_cursor_line_fully_visible_p
15307 ? cursor_height + this_scroll_margin
15308 : 1)) > it.last_visible_y)
15309 {
15310 w->cursor.vpos = -1;
15311 clear_glyph_matrix (w->desired_matrix);
15312 return -1;
15313 }
15314 }
15315
15316 /* Scroll the display. Do it before changing the current matrix so
15317 that xterm.c doesn't get confused about where the cursor glyph is
15318 found. */
15319 if (dy && run.height)
15320 {
15321 update_begin (f);
15322
15323 if (FRAME_WINDOW_P (f))
15324 {
15325 FRAME_RIF (f)->update_window_begin_hook (w);
15326 FRAME_RIF (f)->clear_window_mouse_face (w);
15327 FRAME_RIF (f)->scroll_run_hook (w, &run);
15328 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15329 }
15330 else
15331 {
15332 /* Terminal frame. In this case, dvpos gives the number of
15333 lines to scroll by; dvpos < 0 means scroll up. */
15334 int first_unchanged_at_end_vpos
15335 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15336 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15337 int end = (WINDOW_TOP_EDGE_LINE (w)
15338 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15339 + window_internal_height (w));
15340
15341 /* Perform the operation on the screen. */
15342 if (dvpos > 0)
15343 {
15344 /* Scroll last_unchanged_at_beg_row to the end of the
15345 window down dvpos lines. */
15346 set_terminal_window (f, end);
15347
15348 /* On dumb terminals delete dvpos lines at the end
15349 before inserting dvpos empty lines. */
15350 if (!FRAME_SCROLL_REGION_OK (f))
15351 ins_del_lines (f, end - dvpos, -dvpos);
15352
15353 /* Insert dvpos empty lines in front of
15354 last_unchanged_at_beg_row. */
15355 ins_del_lines (f, from, dvpos);
15356 }
15357 else if (dvpos < 0)
15358 {
15359 /* Scroll up last_unchanged_at_beg_vpos to the end of
15360 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15361 set_terminal_window (f, end);
15362
15363 /* Delete dvpos lines in front of
15364 last_unchanged_at_beg_vpos. ins_del_lines will set
15365 the cursor to the given vpos and emit |dvpos| delete
15366 line sequences. */
15367 ins_del_lines (f, from + dvpos, dvpos);
15368
15369 /* On a dumb terminal insert dvpos empty lines at the
15370 end. */
15371 if (!FRAME_SCROLL_REGION_OK (f))
15372 ins_del_lines (f, end + dvpos, -dvpos);
15373 }
15374
15375 set_terminal_window (f, 0);
15376 }
15377
15378 update_end (f);
15379 }
15380
15381 /* Shift reused rows of the current matrix to the right position.
15382 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15383 text. */
15384 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15385 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15386 if (dvpos < 0)
15387 {
15388 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15389 bottom_vpos, dvpos);
15390 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15391 bottom_vpos, 0);
15392 }
15393 else if (dvpos > 0)
15394 {
15395 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15396 bottom_vpos, dvpos);
15397 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15398 first_unchanged_at_end_vpos + dvpos, 0);
15399 }
15400
15401 /* For frame-based redisplay, make sure that current frame and window
15402 matrix are in sync with respect to glyph memory. */
15403 if (!FRAME_WINDOW_P (f))
15404 sync_frame_with_window_matrix_rows (w);
15405
15406 /* Adjust buffer positions in reused rows. */
15407 if (delta || delta_bytes)
15408 increment_matrix_positions (current_matrix,
15409 first_unchanged_at_end_vpos + dvpos,
15410 bottom_vpos, delta, delta_bytes);
15411
15412 /* Adjust Y positions. */
15413 if (dy)
15414 shift_glyph_matrix (w, current_matrix,
15415 first_unchanged_at_end_vpos + dvpos,
15416 bottom_vpos, dy);
15417
15418 if (first_unchanged_at_end_row)
15419 {
15420 first_unchanged_at_end_row += dvpos;
15421 if (first_unchanged_at_end_row->y >= it.last_visible_y
15422 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15423 first_unchanged_at_end_row = NULL;
15424 }
15425
15426 /* If scrolling up, there may be some lines to display at the end of
15427 the window. */
15428 last_text_row_at_end = NULL;
15429 if (dy < 0)
15430 {
15431 /* Scrolling up can leave for example a partially visible line
15432 at the end of the window to be redisplayed. */
15433 /* Set last_row to the glyph row in the current matrix where the
15434 window end line is found. It has been moved up or down in
15435 the matrix by dvpos. */
15436 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15437 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15438
15439 /* If last_row is the window end line, it should display text. */
15440 xassert (last_row->displays_text_p);
15441
15442 /* If window end line was partially visible before, begin
15443 displaying at that line. Otherwise begin displaying with the
15444 line following it. */
15445 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15446 {
15447 init_to_row_start (&it, w, last_row);
15448 it.vpos = last_vpos;
15449 it.current_y = last_row->y;
15450 }
15451 else
15452 {
15453 init_to_row_end (&it, w, last_row);
15454 it.vpos = 1 + last_vpos;
15455 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15456 ++last_row;
15457 }
15458
15459 /* We may start in a continuation line. If so, we have to
15460 get the right continuation_lines_width and current_x. */
15461 it.continuation_lines_width = last_row->continuation_lines_width;
15462 it.hpos = it.current_x = 0;
15463
15464 /* Display the rest of the lines at the window end. */
15465 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15466 while (it.current_y < it.last_visible_y
15467 && !fonts_changed_p)
15468 {
15469 /* Is it always sure that the display agrees with lines in
15470 the current matrix? I don't think so, so we mark rows
15471 displayed invalid in the current matrix by setting their
15472 enabled_p flag to zero. */
15473 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15474 if (display_line (&it))
15475 last_text_row_at_end = it.glyph_row - 1;
15476 }
15477 }
15478
15479 /* Update window_end_pos and window_end_vpos. */
15480 if (first_unchanged_at_end_row
15481 && !last_text_row_at_end)
15482 {
15483 /* Window end line if one of the preserved rows from the current
15484 matrix. Set row to the last row displaying text in current
15485 matrix starting at first_unchanged_at_end_row, after
15486 scrolling. */
15487 xassert (first_unchanged_at_end_row->displays_text_p);
15488 row = find_last_row_displaying_text (w->current_matrix, &it,
15489 first_unchanged_at_end_row);
15490 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15491
15492 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15493 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15494 w->window_end_vpos
15495 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15496 xassert (w->window_end_bytepos >= 0);
15497 IF_DEBUG (debug_method_add (w, "A"));
15498 }
15499 else if (last_text_row_at_end)
15500 {
15501 w->window_end_pos
15502 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15503 w->window_end_bytepos
15504 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15505 w->window_end_vpos
15506 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15507 xassert (w->window_end_bytepos >= 0);
15508 IF_DEBUG (debug_method_add (w, "B"));
15509 }
15510 else if (last_text_row)
15511 {
15512 /* We have displayed either to the end of the window or at the
15513 end of the window, i.e. the last row with text is to be found
15514 in the desired matrix. */
15515 w->window_end_pos
15516 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15517 w->window_end_bytepos
15518 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15519 w->window_end_vpos
15520 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15521 xassert (w->window_end_bytepos >= 0);
15522 }
15523 else if (first_unchanged_at_end_row == NULL
15524 && last_text_row == NULL
15525 && last_text_row_at_end == NULL)
15526 {
15527 /* Displayed to end of window, but no line containing text was
15528 displayed. Lines were deleted at the end of the window. */
15529 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15530 int vpos = XFASTINT (w->window_end_vpos);
15531 struct glyph_row *current_row = current_matrix->rows + vpos;
15532 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15533
15534 for (row = NULL;
15535 row == NULL && vpos >= first_vpos;
15536 --vpos, --current_row, --desired_row)
15537 {
15538 if (desired_row->enabled_p)
15539 {
15540 if (desired_row->displays_text_p)
15541 row = desired_row;
15542 }
15543 else if (current_row->displays_text_p)
15544 row = current_row;
15545 }
15546
15547 xassert (row != NULL);
15548 w->window_end_vpos = make_number (vpos + 1);
15549 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15550 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15551 xassert (w->window_end_bytepos >= 0);
15552 IF_DEBUG (debug_method_add (w, "C"));
15553 }
15554 else
15555 abort ();
15556
15557 #if 0 /* This leads to problems, for instance when the cursor is
15558 at ZV, and the cursor line displays no text. */
15559 /* Disable rows below what's displayed in the window. This makes
15560 debugging easier. */
15561 enable_glyph_matrix_rows (current_matrix,
15562 XFASTINT (w->window_end_vpos) + 1,
15563 bottom_vpos, 0);
15564 #endif
15565
15566 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15567 debug_end_vpos = XFASTINT (w->window_end_vpos));
15568
15569 /* Record that display has not been completed. */
15570 w->window_end_valid = Qnil;
15571 w->desired_matrix->no_scrolling_p = 1;
15572 return 3;
15573
15574 #undef GIVE_UP
15575 }
15576
15577
15578 \f
15579 /***********************************************************************
15580 More debugging support
15581 ***********************************************************************/
15582
15583 #if GLYPH_DEBUG
15584
15585 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15586 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15587 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15588
15589
15590 /* Dump the contents of glyph matrix MATRIX on stderr.
15591
15592 GLYPHS 0 means don't show glyph contents.
15593 GLYPHS 1 means show glyphs in short form
15594 GLYPHS > 1 means show glyphs in long form. */
15595
15596 void
15597 dump_glyph_matrix (matrix, glyphs)
15598 struct glyph_matrix *matrix;
15599 int glyphs;
15600 {
15601 int i;
15602 for (i = 0; i < matrix->nrows; ++i)
15603 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15604 }
15605
15606
15607 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15608 the glyph row and area where the glyph comes from. */
15609
15610 void
15611 dump_glyph (row, glyph, area)
15612 struct glyph_row *row;
15613 struct glyph *glyph;
15614 int area;
15615 {
15616 if (glyph->type == CHAR_GLYPH)
15617 {
15618 fprintf (stderr,
15619 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15620 glyph - row->glyphs[TEXT_AREA],
15621 'C',
15622 glyph->charpos,
15623 (BUFFERP (glyph->object)
15624 ? 'B'
15625 : (STRINGP (glyph->object)
15626 ? 'S'
15627 : '-')),
15628 glyph->pixel_width,
15629 glyph->u.ch,
15630 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15631 ? glyph->u.ch
15632 : '.'),
15633 glyph->face_id,
15634 glyph->left_box_line_p,
15635 glyph->right_box_line_p);
15636 }
15637 else if (glyph->type == STRETCH_GLYPH)
15638 {
15639 fprintf (stderr,
15640 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15641 glyph - row->glyphs[TEXT_AREA],
15642 'S',
15643 glyph->charpos,
15644 (BUFFERP (glyph->object)
15645 ? 'B'
15646 : (STRINGP (glyph->object)
15647 ? 'S'
15648 : '-')),
15649 glyph->pixel_width,
15650 0,
15651 '.',
15652 glyph->face_id,
15653 glyph->left_box_line_p,
15654 glyph->right_box_line_p);
15655 }
15656 else if (glyph->type == IMAGE_GLYPH)
15657 {
15658 fprintf (stderr,
15659 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15660 glyph - row->glyphs[TEXT_AREA],
15661 'I',
15662 glyph->charpos,
15663 (BUFFERP (glyph->object)
15664 ? 'B'
15665 : (STRINGP (glyph->object)
15666 ? 'S'
15667 : '-')),
15668 glyph->pixel_width,
15669 glyph->u.img_id,
15670 '.',
15671 glyph->face_id,
15672 glyph->left_box_line_p,
15673 glyph->right_box_line_p);
15674 }
15675 else if (glyph->type == COMPOSITE_GLYPH)
15676 {
15677 fprintf (stderr,
15678 " %5d %4c %6d %c %3d 0x%05x",
15679 glyph - row->glyphs[TEXT_AREA],
15680 '+',
15681 glyph->charpos,
15682 (BUFFERP (glyph->object)
15683 ? 'B'
15684 : (STRINGP (glyph->object)
15685 ? 'S'
15686 : '-')),
15687 glyph->pixel_width,
15688 glyph->u.cmp.id);
15689 if (glyph->u.cmp.automatic)
15690 fprintf (stderr,
15691 "[%d-%d]",
15692 glyph->u.cmp.from, glyph->u.cmp.to);
15693 fprintf (stderr, " . %4d %1.1d%1.1d\n"
15694 glyph->face_id,
15695 glyph->left_box_line_p,
15696 glyph->right_box_line_p);
15697 }
15698 }
15699
15700
15701 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15702 GLYPHS 0 means don't show glyph contents.
15703 GLYPHS 1 means show glyphs in short form
15704 GLYPHS > 1 means show glyphs in long form. */
15705
15706 void
15707 dump_glyph_row (row, vpos, glyphs)
15708 struct glyph_row *row;
15709 int vpos, glyphs;
15710 {
15711 if (glyphs != 1)
15712 {
15713 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15714 fprintf (stderr, "======================================================================\n");
15715
15716 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15717 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15718 vpos,
15719 MATRIX_ROW_START_CHARPOS (row),
15720 MATRIX_ROW_END_CHARPOS (row),
15721 row->used[TEXT_AREA],
15722 row->contains_overlapping_glyphs_p,
15723 row->enabled_p,
15724 row->truncated_on_left_p,
15725 row->truncated_on_right_p,
15726 row->continued_p,
15727 MATRIX_ROW_CONTINUATION_LINE_P (row),
15728 row->displays_text_p,
15729 row->ends_at_zv_p,
15730 row->fill_line_p,
15731 row->ends_in_middle_of_char_p,
15732 row->starts_in_middle_of_char_p,
15733 row->mouse_face_p,
15734 row->x,
15735 row->y,
15736 row->pixel_width,
15737 row->height,
15738 row->visible_height,
15739 row->ascent,
15740 row->phys_ascent);
15741 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15742 row->end.overlay_string_index,
15743 row->continuation_lines_width);
15744 fprintf (stderr, "%9d %5d\n",
15745 CHARPOS (row->start.string_pos),
15746 CHARPOS (row->end.string_pos));
15747 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15748 row->end.dpvec_index);
15749 }
15750
15751 if (glyphs > 1)
15752 {
15753 int area;
15754
15755 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15756 {
15757 struct glyph *glyph = row->glyphs[area];
15758 struct glyph *glyph_end = glyph + row->used[area];
15759
15760 /* Glyph for a line end in text. */
15761 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15762 ++glyph_end;
15763
15764 if (glyph < glyph_end)
15765 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15766
15767 for (; glyph < glyph_end; ++glyph)
15768 dump_glyph (row, glyph, area);
15769 }
15770 }
15771 else if (glyphs == 1)
15772 {
15773 int area;
15774
15775 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15776 {
15777 char *s = (char *) alloca (row->used[area] + 1);
15778 int i;
15779
15780 for (i = 0; i < row->used[area]; ++i)
15781 {
15782 struct glyph *glyph = row->glyphs[area] + i;
15783 if (glyph->type == CHAR_GLYPH
15784 && glyph->u.ch < 0x80
15785 && glyph->u.ch >= ' ')
15786 s[i] = glyph->u.ch;
15787 else
15788 s[i] = '.';
15789 }
15790
15791 s[i] = '\0';
15792 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15793 }
15794 }
15795 }
15796
15797
15798 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15799 Sdump_glyph_matrix, 0, 1, "p",
15800 doc: /* Dump the current matrix of the selected window to stderr.
15801 Shows contents of glyph row structures. With non-nil
15802 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15803 glyphs in short form, otherwise show glyphs in long form. */)
15804 (glyphs)
15805 Lisp_Object glyphs;
15806 {
15807 struct window *w = XWINDOW (selected_window);
15808 struct buffer *buffer = XBUFFER (w->buffer);
15809
15810 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15811 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15812 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15813 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15814 fprintf (stderr, "=============================================\n");
15815 dump_glyph_matrix (w->current_matrix,
15816 NILP (glyphs) ? 0 : XINT (glyphs));
15817 return Qnil;
15818 }
15819
15820
15821 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15822 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15823 ()
15824 {
15825 struct frame *f = XFRAME (selected_frame);
15826 dump_glyph_matrix (f->current_matrix, 1);
15827 return Qnil;
15828 }
15829
15830
15831 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15832 doc: /* Dump glyph row ROW 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 glyph_matrix *matrix;
15840 int vpos;
15841
15842 CHECK_NUMBER (row);
15843 matrix = XWINDOW (selected_window)->current_matrix;
15844 vpos = XINT (row);
15845 if (vpos >= 0 && vpos < matrix->nrows)
15846 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15847 vpos,
15848 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15849 return Qnil;
15850 }
15851
15852
15853 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15854 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15855 GLYPH 0 means don't dump glyphs.
15856 GLYPH 1 means dump glyphs in short form.
15857 GLYPH > 1 or omitted means dump glyphs in long form. */)
15858 (row, glyphs)
15859 Lisp_Object row, glyphs;
15860 {
15861 struct frame *sf = SELECTED_FRAME ();
15862 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15863 int vpos;
15864
15865 CHECK_NUMBER (row);
15866 vpos = XINT (row);
15867 if (vpos >= 0 && vpos < m->nrows)
15868 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15869 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15870 return Qnil;
15871 }
15872
15873
15874 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15875 doc: /* Toggle tracing of redisplay.
15876 With ARG, turn tracing on if and only if ARG is positive. */)
15877 (arg)
15878 Lisp_Object arg;
15879 {
15880 if (NILP (arg))
15881 trace_redisplay_p = !trace_redisplay_p;
15882 else
15883 {
15884 arg = Fprefix_numeric_value (arg);
15885 trace_redisplay_p = XINT (arg) > 0;
15886 }
15887
15888 return Qnil;
15889 }
15890
15891
15892 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15893 doc: /* Like `format', but print result to stderr.
15894 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15895 (nargs, args)
15896 int nargs;
15897 Lisp_Object *args;
15898 {
15899 Lisp_Object s = Fformat (nargs, args);
15900 fprintf (stderr, "%s", SDATA (s));
15901 return Qnil;
15902 }
15903
15904 #endif /* GLYPH_DEBUG */
15905
15906
15907 \f
15908 /***********************************************************************
15909 Building Desired Matrix Rows
15910 ***********************************************************************/
15911
15912 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15913 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15914
15915 static struct glyph_row *
15916 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15917 struct window *w;
15918 Lisp_Object overlay_arrow_string;
15919 {
15920 struct frame *f = XFRAME (WINDOW_FRAME (w));
15921 struct buffer *buffer = XBUFFER (w->buffer);
15922 struct buffer *old = current_buffer;
15923 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15924 int arrow_len = SCHARS (overlay_arrow_string);
15925 const unsigned char *arrow_end = arrow_string + arrow_len;
15926 const unsigned char *p;
15927 struct it it;
15928 int multibyte_p;
15929 int n_glyphs_before;
15930
15931 set_buffer_temp (buffer);
15932 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15933 it.glyph_row->used[TEXT_AREA] = 0;
15934 SET_TEXT_POS (it.position, 0, 0);
15935
15936 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15937 p = arrow_string;
15938 while (p < arrow_end)
15939 {
15940 Lisp_Object face, ilisp;
15941
15942 /* Get the next character. */
15943 if (multibyte_p)
15944 it.c = string_char_and_length (p, arrow_len, &it.len);
15945 else
15946 it.c = *p, it.len = 1;
15947 p += it.len;
15948
15949 /* Get its face. */
15950 ilisp = make_number (p - arrow_string);
15951 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15952 it.face_id = compute_char_face (f, it.c, face);
15953
15954 /* Compute its width, get its glyphs. */
15955 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15956 SET_TEXT_POS (it.position, -1, -1);
15957 PRODUCE_GLYPHS (&it);
15958
15959 /* If this character doesn't fit any more in the line, we have
15960 to remove some glyphs. */
15961 if (it.current_x > it.last_visible_x)
15962 {
15963 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15964 break;
15965 }
15966 }
15967
15968 set_buffer_temp (old);
15969 return it.glyph_row;
15970 }
15971
15972
15973 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15974 glyphs are only inserted for terminal frames since we can't really
15975 win with truncation glyphs when partially visible glyphs are
15976 involved. Which glyphs to insert is determined by
15977 produce_special_glyphs. */
15978
15979 static void
15980 insert_left_trunc_glyphs (it)
15981 struct it *it;
15982 {
15983 struct it truncate_it;
15984 struct glyph *from, *end, *to, *toend;
15985
15986 xassert (!FRAME_WINDOW_P (it->f));
15987
15988 /* Get the truncation glyphs. */
15989 truncate_it = *it;
15990 truncate_it.current_x = 0;
15991 truncate_it.face_id = DEFAULT_FACE_ID;
15992 truncate_it.glyph_row = &scratch_glyph_row;
15993 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15994 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15995 truncate_it.object = make_number (0);
15996 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15997
15998 /* Overwrite glyphs from IT with truncation glyphs. */
15999 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16000 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16001 to = it->glyph_row->glyphs[TEXT_AREA];
16002 toend = to + it->glyph_row->used[TEXT_AREA];
16003
16004 while (from < end)
16005 *to++ = *from++;
16006
16007 /* There may be padding glyphs left over. Overwrite them too. */
16008 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16009 {
16010 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16011 while (from < end)
16012 *to++ = *from++;
16013 }
16014
16015 if (to > toend)
16016 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16017 }
16018
16019
16020 /* Compute the pixel height and width of IT->glyph_row.
16021
16022 Most of the time, ascent and height of a display line will be equal
16023 to the max_ascent and max_height values of the display iterator
16024 structure. This is not the case if
16025
16026 1. We hit ZV without displaying anything. In this case, max_ascent
16027 and max_height will be zero.
16028
16029 2. We have some glyphs that don't contribute to the line height.
16030 (The glyph row flag contributes_to_line_height_p is for future
16031 pixmap extensions).
16032
16033 The first case is easily covered by using default values because in
16034 these cases, the line height does not really matter, except that it
16035 must not be zero. */
16036
16037 static void
16038 compute_line_metrics (it)
16039 struct it *it;
16040 {
16041 struct glyph_row *row = it->glyph_row;
16042 int area, i;
16043
16044 if (FRAME_WINDOW_P (it->f))
16045 {
16046 int i, min_y, max_y;
16047
16048 /* The line may consist of one space only, that was added to
16049 place the cursor on it. If so, the row's height hasn't been
16050 computed yet. */
16051 if (row->height == 0)
16052 {
16053 if (it->max_ascent + it->max_descent == 0)
16054 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16055 row->ascent = it->max_ascent;
16056 row->height = it->max_ascent + it->max_descent;
16057 row->phys_ascent = it->max_phys_ascent;
16058 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16059 row->extra_line_spacing = it->max_extra_line_spacing;
16060 }
16061
16062 /* Compute the width of this line. */
16063 row->pixel_width = row->x;
16064 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16065 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16066
16067 xassert (row->pixel_width >= 0);
16068 xassert (row->ascent >= 0 && row->height > 0);
16069
16070 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16071 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16072
16073 /* If first line's physical ascent is larger than its logical
16074 ascent, use the physical ascent, and make the row taller.
16075 This makes accented characters fully visible. */
16076 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16077 && row->phys_ascent > row->ascent)
16078 {
16079 row->height += row->phys_ascent - row->ascent;
16080 row->ascent = row->phys_ascent;
16081 }
16082
16083 /* Compute how much of the line is visible. */
16084 row->visible_height = row->height;
16085
16086 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16087 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16088
16089 if (row->y < min_y)
16090 row->visible_height -= min_y - row->y;
16091 if (row->y + row->height > max_y)
16092 row->visible_height -= row->y + row->height - max_y;
16093 }
16094 else
16095 {
16096 row->pixel_width = row->used[TEXT_AREA];
16097 if (row->continued_p)
16098 row->pixel_width -= it->continuation_pixel_width;
16099 else if (row->truncated_on_right_p)
16100 row->pixel_width -= it->truncation_pixel_width;
16101 row->ascent = row->phys_ascent = 0;
16102 row->height = row->phys_height = row->visible_height = 1;
16103 row->extra_line_spacing = 0;
16104 }
16105
16106 /* Compute a hash code for this row. */
16107 row->hash = 0;
16108 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16109 for (i = 0; i < row->used[area]; ++i)
16110 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16111 + row->glyphs[area][i].u.val
16112 + row->glyphs[area][i].face_id
16113 + row->glyphs[area][i].padding_p
16114 + (row->glyphs[area][i].type << 2));
16115
16116 it->max_ascent = it->max_descent = 0;
16117 it->max_phys_ascent = it->max_phys_descent = 0;
16118 }
16119
16120
16121 /* Append one space to the glyph row of iterator IT if doing a
16122 window-based redisplay. The space has the same face as
16123 IT->face_id. Value is non-zero if a space was added.
16124
16125 This function is called to make sure that there is always one glyph
16126 at the end of a glyph row that the cursor can be set on under
16127 window-systems. (If there weren't such a glyph we would not know
16128 how wide and tall a box cursor should be displayed).
16129
16130 At the same time this space let's a nicely handle clearing to the
16131 end of the line if the row ends in italic text. */
16132
16133 static int
16134 append_space_for_newline (it, default_face_p)
16135 struct it *it;
16136 int default_face_p;
16137 {
16138 if (FRAME_WINDOW_P (it->f))
16139 {
16140 int n = it->glyph_row->used[TEXT_AREA];
16141
16142 if (it->glyph_row->glyphs[TEXT_AREA] + n
16143 < it->glyph_row->glyphs[1 + TEXT_AREA])
16144 {
16145 /* Save some values that must not be changed.
16146 Must save IT->c and IT->len because otherwise
16147 ITERATOR_AT_END_P wouldn't work anymore after
16148 append_space_for_newline has been called. */
16149 enum display_element_type saved_what = it->what;
16150 int saved_c = it->c, saved_len = it->len;
16151 int saved_x = it->current_x;
16152 int saved_face_id = it->face_id;
16153 struct text_pos saved_pos;
16154 Lisp_Object saved_object;
16155 struct face *face;
16156
16157 saved_object = it->object;
16158 saved_pos = it->position;
16159
16160 it->what = IT_CHARACTER;
16161 bzero (&it->position, sizeof it->position);
16162 it->object = make_number (0);
16163 it->c = ' ';
16164 it->len = 1;
16165
16166 if (default_face_p)
16167 it->face_id = DEFAULT_FACE_ID;
16168 else if (it->face_before_selective_p)
16169 it->face_id = it->saved_face_id;
16170 face = FACE_FROM_ID (it->f, it->face_id);
16171 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16172
16173 PRODUCE_GLYPHS (it);
16174
16175 it->override_ascent = -1;
16176 it->constrain_row_ascent_descent_p = 0;
16177 it->current_x = saved_x;
16178 it->object = saved_object;
16179 it->position = saved_pos;
16180 it->what = saved_what;
16181 it->face_id = saved_face_id;
16182 it->len = saved_len;
16183 it->c = saved_c;
16184 return 1;
16185 }
16186 }
16187
16188 return 0;
16189 }
16190
16191
16192 /* Extend the face of the last glyph in the text area of IT->glyph_row
16193 to the end of the display line. Called from display_line.
16194 If the glyph row is empty, add a space glyph to it so that we
16195 know the face to draw. Set the glyph row flag fill_line_p. */
16196
16197 static void
16198 extend_face_to_end_of_line (it)
16199 struct it *it;
16200 {
16201 struct face *face;
16202 struct frame *f = it->f;
16203
16204 /* If line is already filled, do nothing. */
16205 if (it->current_x >= it->last_visible_x)
16206 return;
16207
16208 /* Face extension extends the background and box of IT->face_id
16209 to the end of the line. If the background equals the background
16210 of the frame, we don't have to do anything. */
16211 if (it->face_before_selective_p)
16212 face = FACE_FROM_ID (it->f, it->saved_face_id);
16213 else
16214 face = FACE_FROM_ID (f, it->face_id);
16215
16216 if (FRAME_WINDOW_P (f)
16217 && it->glyph_row->displays_text_p
16218 && face->box == FACE_NO_BOX
16219 && face->background == FRAME_BACKGROUND_PIXEL (f)
16220 && !face->stipple)
16221 return;
16222
16223 /* Set the glyph row flag indicating that the face of the last glyph
16224 in the text area has to be drawn to the end of the text area. */
16225 it->glyph_row->fill_line_p = 1;
16226
16227 /* If current character of IT is not ASCII, make sure we have the
16228 ASCII face. This will be automatically undone the next time
16229 get_next_display_element returns a multibyte character. Note
16230 that the character will always be single byte in unibyte text. */
16231 if (!ASCII_CHAR_P (it->c))
16232 {
16233 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16234 }
16235
16236 if (FRAME_WINDOW_P (f))
16237 {
16238 /* If the row is empty, add a space with the current face of IT,
16239 so that we know which face to draw. */
16240 if (it->glyph_row->used[TEXT_AREA] == 0)
16241 {
16242 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16243 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16244 it->glyph_row->used[TEXT_AREA] = 1;
16245 }
16246 }
16247 else
16248 {
16249 /* Save some values that must not be changed. */
16250 int saved_x = it->current_x;
16251 struct text_pos saved_pos;
16252 Lisp_Object saved_object;
16253 enum display_element_type saved_what = it->what;
16254 int saved_face_id = it->face_id;
16255
16256 saved_object = it->object;
16257 saved_pos = it->position;
16258
16259 it->what = IT_CHARACTER;
16260 bzero (&it->position, sizeof it->position);
16261 it->object = make_number (0);
16262 it->c = ' ';
16263 it->len = 1;
16264 it->face_id = face->id;
16265
16266 PRODUCE_GLYPHS (it);
16267
16268 while (it->current_x <= it->last_visible_x)
16269 PRODUCE_GLYPHS (it);
16270
16271 /* Don't count these blanks really. It would let us insert a left
16272 truncation glyph below and make us set the cursor on them, maybe. */
16273 it->current_x = saved_x;
16274 it->object = saved_object;
16275 it->position = saved_pos;
16276 it->what = saved_what;
16277 it->face_id = saved_face_id;
16278 }
16279 }
16280
16281
16282 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16283 trailing whitespace. */
16284
16285 static int
16286 trailing_whitespace_p (charpos)
16287 int charpos;
16288 {
16289 int bytepos = CHAR_TO_BYTE (charpos);
16290 int c = 0;
16291
16292 while (bytepos < ZV_BYTE
16293 && (c = FETCH_CHAR (bytepos),
16294 c == ' ' || c == '\t'))
16295 ++bytepos;
16296
16297 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16298 {
16299 if (bytepos != PT_BYTE)
16300 return 1;
16301 }
16302 return 0;
16303 }
16304
16305
16306 /* Highlight trailing whitespace, if any, in ROW. */
16307
16308 void
16309 highlight_trailing_whitespace (f, row)
16310 struct frame *f;
16311 struct glyph_row *row;
16312 {
16313 int used = row->used[TEXT_AREA];
16314
16315 if (used)
16316 {
16317 struct glyph *start = row->glyphs[TEXT_AREA];
16318 struct glyph *glyph = start + used - 1;
16319
16320 /* Skip over glyphs inserted to display the cursor at the
16321 end of a line, for extending the face of the last glyph
16322 to the end of the line on terminals, and for truncation
16323 and continuation glyphs. */
16324 while (glyph >= start
16325 && glyph->type == CHAR_GLYPH
16326 && INTEGERP (glyph->object))
16327 --glyph;
16328
16329 /* If last glyph is a space or stretch, and it's trailing
16330 whitespace, set the face of all trailing whitespace glyphs in
16331 IT->glyph_row to `trailing-whitespace'. */
16332 if (glyph >= start
16333 && BUFFERP (glyph->object)
16334 && (glyph->type == STRETCH_GLYPH
16335 || (glyph->type == CHAR_GLYPH
16336 && glyph->u.ch == ' '))
16337 && trailing_whitespace_p (glyph->charpos))
16338 {
16339 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16340 if (face_id < 0)
16341 return;
16342
16343 while (glyph >= start
16344 && BUFFERP (glyph->object)
16345 && (glyph->type == STRETCH_GLYPH
16346 || (glyph->type == CHAR_GLYPH
16347 && glyph->u.ch == ' ')))
16348 (glyph--)->face_id = face_id;
16349 }
16350 }
16351 }
16352
16353
16354 /* Value is non-zero if glyph row ROW in window W should be
16355 used to hold the cursor. */
16356
16357 static int
16358 cursor_row_p (w, row)
16359 struct window *w;
16360 struct glyph_row *row;
16361 {
16362 int cursor_row_p = 1;
16363
16364 if (PT == MATRIX_ROW_END_CHARPOS (row))
16365 {
16366 /* Suppose the row ends on a string.
16367 Unless the row is continued, that means it ends on a newline
16368 in the string. If it's anything other than a display string
16369 (e.g. a before-string from an overlay), we don't want the
16370 cursor there. (This heuristic seems to give the optimal
16371 behavior for the various types of multi-line strings.) */
16372 if (CHARPOS (row->end.string_pos) >= 0)
16373 {
16374 if (row->continued_p)
16375 cursor_row_p = 1;
16376 else
16377 {
16378 /* Check for `display' property. */
16379 struct glyph *beg = row->glyphs[TEXT_AREA];
16380 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16381 struct glyph *glyph;
16382
16383 cursor_row_p = 0;
16384 for (glyph = end; glyph >= beg; --glyph)
16385 if (STRINGP (glyph->object))
16386 {
16387 Lisp_Object prop
16388 = Fget_char_property (make_number (PT),
16389 Qdisplay, Qnil);
16390 cursor_row_p =
16391 (!NILP (prop)
16392 && display_prop_string_p (prop, glyph->object));
16393 break;
16394 }
16395 }
16396 }
16397 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16398 {
16399 /* If the row ends in middle of a real character,
16400 and the line is continued, we want the cursor here.
16401 That's because MATRIX_ROW_END_CHARPOS would equal
16402 PT if PT is before the character. */
16403 if (!row->ends_in_ellipsis_p)
16404 cursor_row_p = row->continued_p;
16405 else
16406 /* If the row ends in an ellipsis, then
16407 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16408 We want that position to be displayed after the ellipsis. */
16409 cursor_row_p = 0;
16410 }
16411 /* If the row ends at ZV, display the cursor at the end of that
16412 row instead of at the start of the row below. */
16413 else if (row->ends_at_zv_p)
16414 cursor_row_p = 1;
16415 else
16416 cursor_row_p = 0;
16417 }
16418
16419 return cursor_row_p;
16420 }
16421
16422 \f
16423
16424 /* Push the display property PROP so that it will be rendered at the
16425 current position in IT. */
16426
16427 static void
16428 push_display_prop (struct it *it, Lisp_Object prop)
16429 {
16430 push_it (it);
16431
16432 /* Never display a cursor on the prefix. */
16433 it->avoid_cursor_p = 1;
16434
16435 if (STRINGP (prop))
16436 {
16437 if (SCHARS (prop) == 0)
16438 {
16439 pop_it (it);
16440 return;
16441 }
16442
16443 it->string = prop;
16444 it->multibyte_p = STRING_MULTIBYTE (it->string);
16445 it->current.overlay_string_index = -1;
16446 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16447 it->end_charpos = it->string_nchars = SCHARS (it->string);
16448 it->method = GET_FROM_STRING;
16449 it->stop_charpos = 0;
16450 }
16451 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16452 {
16453 it->method = GET_FROM_STRETCH;
16454 it->object = prop;
16455 }
16456 #ifdef HAVE_WINDOW_SYSTEM
16457 else if (IMAGEP (prop))
16458 {
16459 it->what = IT_IMAGE;
16460 it->image_id = lookup_image (it->f, prop);
16461 it->method = GET_FROM_IMAGE;
16462 }
16463 #endif /* HAVE_WINDOW_SYSTEM */
16464 else
16465 {
16466 pop_it (it); /* bogus display property, give up */
16467 return;
16468 }
16469 }
16470
16471 /* Return the character-property PROP at the current position in IT. */
16472
16473 static Lisp_Object
16474 get_it_property (it, prop)
16475 struct it *it;
16476 Lisp_Object prop;
16477 {
16478 Lisp_Object position;
16479
16480 if (STRINGP (it->object))
16481 position = make_number (IT_STRING_CHARPOS (*it));
16482 else if (BUFFERP (it->object))
16483 position = make_number (IT_CHARPOS (*it));
16484 else
16485 return Qnil;
16486
16487 return Fget_char_property (position, prop, it->object);
16488 }
16489
16490 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16491
16492 static void
16493 handle_line_prefix (struct it *it)
16494 {
16495 Lisp_Object prefix;
16496 if (it->continuation_lines_width > 0)
16497 {
16498 prefix = get_it_property (it, Qwrap_prefix);
16499 if (NILP (prefix))
16500 prefix = Vwrap_prefix;
16501 }
16502 else
16503 {
16504 prefix = get_it_property (it, Qline_prefix);
16505 if (NILP (prefix))
16506 prefix = Vline_prefix;
16507 }
16508 if (! NILP (prefix))
16509 push_display_prop (it, prefix);
16510 }
16511
16512 \f
16513
16514 /* Construct the glyph row IT->glyph_row in the desired matrix of
16515 IT->w from text at the current position of IT. See dispextern.h
16516 for an overview of struct it. Value is non-zero if
16517 IT->glyph_row displays text, as opposed to a line displaying ZV
16518 only. */
16519
16520 static int
16521 display_line (it)
16522 struct it *it;
16523 {
16524 struct glyph_row *row = it->glyph_row;
16525 Lisp_Object overlay_arrow_string;
16526 struct it wrap_it;
16527 int may_wrap = 0, wrap_x;
16528 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16529 int wrap_row_phys_ascent, wrap_row_phys_height;
16530 int wrap_row_extra_line_spacing;
16531
16532 /* We always start displaying at hpos zero even if hscrolled. */
16533 xassert (it->hpos == 0 && it->current_x == 0);
16534
16535 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16536 >= it->w->desired_matrix->nrows)
16537 {
16538 it->w->nrows_scale_factor++;
16539 fonts_changed_p = 1;
16540 return 0;
16541 }
16542
16543 /* Is IT->w showing the region? */
16544 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16545
16546 /* Clear the result glyph row and enable it. */
16547 prepare_desired_row (row);
16548
16549 row->y = it->current_y;
16550 row->start = it->start;
16551 row->continuation_lines_width = it->continuation_lines_width;
16552 row->displays_text_p = 1;
16553 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16554 it->starts_in_middle_of_char_p = 0;
16555
16556 /* Arrange the overlays nicely for our purposes. Usually, we call
16557 display_line on only one line at a time, in which case this
16558 can't really hurt too much, or we call it on lines which appear
16559 one after another in the buffer, in which case all calls to
16560 recenter_overlay_lists but the first will be pretty cheap. */
16561 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16562
16563 /* Move over display elements that are not visible because we are
16564 hscrolled. This may stop at an x-position < IT->first_visible_x
16565 if the first glyph is partially visible or if we hit a line end. */
16566 if (it->current_x < it->first_visible_x)
16567 {
16568 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16569 MOVE_TO_POS | MOVE_TO_X);
16570 }
16571 else
16572 {
16573 /* We only do this when not calling `move_it_in_display_line_to'
16574 above, because move_it_in_display_line_to calls
16575 handle_line_prefix itself. */
16576 handle_line_prefix (it);
16577 }
16578
16579 /* Get the initial row height. This is either the height of the
16580 text hscrolled, if there is any, or zero. */
16581 row->ascent = it->max_ascent;
16582 row->height = it->max_ascent + it->max_descent;
16583 row->phys_ascent = it->max_phys_ascent;
16584 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16585 row->extra_line_spacing = it->max_extra_line_spacing;
16586
16587 /* Loop generating characters. The loop is left with IT on the next
16588 character to display. */
16589 while (1)
16590 {
16591 int n_glyphs_before, hpos_before, x_before;
16592 int x, i, nglyphs;
16593 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16594
16595 /* Retrieve the next thing to display. Value is zero if end of
16596 buffer reached. */
16597 if (!get_next_display_element (it))
16598 {
16599 /* Maybe add a space at the end of this line that is used to
16600 display the cursor there under X. Set the charpos of the
16601 first glyph of blank lines not corresponding to any text
16602 to -1. */
16603 #ifdef HAVE_WINDOW_SYSTEM
16604 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16605 row->exact_window_width_line_p = 1;
16606 else
16607 #endif /* HAVE_WINDOW_SYSTEM */
16608 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16609 || row->used[TEXT_AREA] == 0)
16610 {
16611 row->glyphs[TEXT_AREA]->charpos = -1;
16612 row->displays_text_p = 0;
16613
16614 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16615 && (!MINI_WINDOW_P (it->w)
16616 || (minibuf_level && EQ (it->window, minibuf_window))))
16617 row->indicate_empty_line_p = 1;
16618 }
16619
16620 it->continuation_lines_width = 0;
16621 row->ends_at_zv_p = 1;
16622 break;
16623 }
16624
16625 /* Now, get the metrics of what we want to display. This also
16626 generates glyphs in `row' (which is IT->glyph_row). */
16627 n_glyphs_before = row->used[TEXT_AREA];
16628 x = it->current_x;
16629
16630 /* Remember the line height so far in case the next element doesn't
16631 fit on the line. */
16632 if (it->line_wrap != TRUNCATE)
16633 {
16634 ascent = it->max_ascent;
16635 descent = it->max_descent;
16636 phys_ascent = it->max_phys_ascent;
16637 phys_descent = it->max_phys_descent;
16638
16639 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16640 {
16641 if (IT_DISPLAYING_WHITESPACE (it))
16642 may_wrap = 1;
16643 else if (may_wrap)
16644 {
16645 wrap_it = *it;
16646 wrap_x = x;
16647 wrap_row_used = row->used[TEXT_AREA];
16648 wrap_row_ascent = row->ascent;
16649 wrap_row_height = row->height;
16650 wrap_row_phys_ascent = row->phys_ascent;
16651 wrap_row_phys_height = row->phys_height;
16652 wrap_row_extra_line_spacing = row->extra_line_spacing;
16653 may_wrap = 0;
16654 }
16655 }
16656 }
16657
16658 PRODUCE_GLYPHS (it);
16659
16660 /* If this display element was in marginal areas, continue with
16661 the next one. */
16662 if (it->area != TEXT_AREA)
16663 {
16664 row->ascent = max (row->ascent, it->max_ascent);
16665 row->height = max (row->height, it->max_ascent + it->max_descent);
16666 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16667 row->phys_height = max (row->phys_height,
16668 it->max_phys_ascent + it->max_phys_descent);
16669 row->extra_line_spacing = max (row->extra_line_spacing,
16670 it->max_extra_line_spacing);
16671 set_iterator_to_next (it, 1);
16672 continue;
16673 }
16674
16675 /* Does the display element fit on the line? If we truncate
16676 lines, we should draw past the right edge of the window. If
16677 we don't truncate, we want to stop so that we can display the
16678 continuation glyph before the right margin. If lines are
16679 continued, there are two possible strategies for characters
16680 resulting in more than 1 glyph (e.g. tabs): Display as many
16681 glyphs as possible in this line and leave the rest for the
16682 continuation line, or display the whole element in the next
16683 line. Original redisplay did the former, so we do it also. */
16684 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16685 hpos_before = it->hpos;
16686 x_before = x;
16687
16688 if (/* Not a newline. */
16689 nglyphs > 0
16690 /* Glyphs produced fit entirely in the line. */
16691 && it->current_x < it->last_visible_x)
16692 {
16693 it->hpos += nglyphs;
16694 row->ascent = max (row->ascent, it->max_ascent);
16695 row->height = max (row->height, it->max_ascent + it->max_descent);
16696 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16697 row->phys_height = max (row->phys_height,
16698 it->max_phys_ascent + it->max_phys_descent);
16699 row->extra_line_spacing = max (row->extra_line_spacing,
16700 it->max_extra_line_spacing);
16701 if (it->current_x - it->pixel_width < it->first_visible_x)
16702 row->x = x - it->first_visible_x;
16703 }
16704 else
16705 {
16706 int new_x;
16707 struct glyph *glyph;
16708
16709 for (i = 0; i < nglyphs; ++i, x = new_x)
16710 {
16711 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16712 new_x = x + glyph->pixel_width;
16713
16714 if (/* Lines are continued. */
16715 it->line_wrap != TRUNCATE
16716 && (/* Glyph doesn't fit on the line. */
16717 new_x > it->last_visible_x
16718 /* Or it fits exactly on a window system frame. */
16719 || (new_x == it->last_visible_x
16720 && FRAME_WINDOW_P (it->f))))
16721 {
16722 /* End of a continued line. */
16723
16724 if (it->hpos == 0
16725 || (new_x == it->last_visible_x
16726 && FRAME_WINDOW_P (it->f)))
16727 {
16728 /* Current glyph is the only one on the line or
16729 fits exactly on the line. We must continue
16730 the line because we can't draw the cursor
16731 after the glyph. */
16732 row->continued_p = 1;
16733 it->current_x = new_x;
16734 it->continuation_lines_width += new_x;
16735 ++it->hpos;
16736 if (i == nglyphs - 1)
16737 {
16738 /* If line-wrap is on, check if a previous
16739 wrap point was found. */
16740 if (wrap_row_used > 0
16741 /* Even if there is a previous wrap
16742 point, continue the line here as
16743 usual, if (i) the previous character
16744 was a space or tab AND (ii) the
16745 current character is not. */
16746 && (!may_wrap
16747 || IT_DISPLAYING_WHITESPACE (it)))
16748 goto back_to_wrap;
16749
16750 set_iterator_to_next (it, 1);
16751 #ifdef HAVE_WINDOW_SYSTEM
16752 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16753 {
16754 if (!get_next_display_element (it))
16755 {
16756 row->exact_window_width_line_p = 1;
16757 it->continuation_lines_width = 0;
16758 row->continued_p = 0;
16759 row->ends_at_zv_p = 1;
16760 }
16761 else if (ITERATOR_AT_END_OF_LINE_P (it))
16762 {
16763 row->continued_p = 0;
16764 row->exact_window_width_line_p = 1;
16765 }
16766 }
16767 #endif /* HAVE_WINDOW_SYSTEM */
16768 }
16769 }
16770 else if (CHAR_GLYPH_PADDING_P (*glyph)
16771 && !FRAME_WINDOW_P (it->f))
16772 {
16773 /* A padding glyph that doesn't fit on this line.
16774 This means the whole character doesn't fit
16775 on the line. */
16776 row->used[TEXT_AREA] = n_glyphs_before;
16777
16778 /* Fill the rest of the row with continuation
16779 glyphs like in 20.x. */
16780 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16781 < row->glyphs[1 + TEXT_AREA])
16782 produce_special_glyphs (it, IT_CONTINUATION);
16783
16784 row->continued_p = 1;
16785 it->current_x = x_before;
16786 it->continuation_lines_width += x_before;
16787
16788 /* Restore the height to what it was before the
16789 element not fitting on the line. */
16790 it->max_ascent = ascent;
16791 it->max_descent = descent;
16792 it->max_phys_ascent = phys_ascent;
16793 it->max_phys_descent = phys_descent;
16794 }
16795 else if (wrap_row_used > 0)
16796 {
16797 back_to_wrap:
16798 *it = wrap_it;
16799 it->continuation_lines_width += wrap_x;
16800 row->used[TEXT_AREA] = wrap_row_used;
16801 row->ascent = wrap_row_ascent;
16802 row->height = wrap_row_height;
16803 row->phys_ascent = wrap_row_phys_ascent;
16804 row->phys_height = wrap_row_phys_height;
16805 row->extra_line_spacing = wrap_row_extra_line_spacing;
16806 row->continued_p = 1;
16807 row->ends_at_zv_p = 0;
16808 row->exact_window_width_line_p = 0;
16809 it->continuation_lines_width += x;
16810
16811 /* Make sure that a non-default face is extended
16812 up to the right margin of the window. */
16813 extend_face_to_end_of_line (it);
16814 }
16815 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16816 {
16817 /* A TAB that extends past the right edge of the
16818 window. This produces a single glyph on
16819 window system frames. We leave the glyph in
16820 this row and let it fill the row, but don't
16821 consume the TAB. */
16822 it->continuation_lines_width += it->last_visible_x;
16823 row->ends_in_middle_of_char_p = 1;
16824 row->continued_p = 1;
16825 glyph->pixel_width = it->last_visible_x - x;
16826 it->starts_in_middle_of_char_p = 1;
16827 }
16828 else
16829 {
16830 /* Something other than a TAB that draws past
16831 the right edge of the window. Restore
16832 positions to values before the element. */
16833 row->used[TEXT_AREA] = n_glyphs_before + i;
16834
16835 /* Display continuation glyphs. */
16836 if (!FRAME_WINDOW_P (it->f))
16837 produce_special_glyphs (it, IT_CONTINUATION);
16838 row->continued_p = 1;
16839
16840 it->current_x = x_before;
16841 it->continuation_lines_width += x;
16842 extend_face_to_end_of_line (it);
16843
16844 if (nglyphs > 1 && i > 0)
16845 {
16846 row->ends_in_middle_of_char_p = 1;
16847 it->starts_in_middle_of_char_p = 1;
16848 }
16849
16850 /* Restore the height to what it was before the
16851 element not fitting on the line. */
16852 it->max_ascent = ascent;
16853 it->max_descent = descent;
16854 it->max_phys_ascent = phys_ascent;
16855 it->max_phys_descent = phys_descent;
16856 }
16857
16858 break;
16859 }
16860 else if (new_x > it->first_visible_x)
16861 {
16862 /* Increment number of glyphs actually displayed. */
16863 ++it->hpos;
16864
16865 if (x < it->first_visible_x)
16866 /* Glyph is partially visible, i.e. row starts at
16867 negative X position. */
16868 row->x = x - it->first_visible_x;
16869 }
16870 else
16871 {
16872 /* Glyph is completely off the left margin of the
16873 window. This should not happen because of the
16874 move_it_in_display_line at the start of this
16875 function, unless the text display area of the
16876 window is empty. */
16877 xassert (it->first_visible_x <= it->last_visible_x);
16878 }
16879 }
16880
16881 row->ascent = max (row->ascent, it->max_ascent);
16882 row->height = max (row->height, it->max_ascent + it->max_descent);
16883 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16884 row->phys_height = max (row->phys_height,
16885 it->max_phys_ascent + it->max_phys_descent);
16886 row->extra_line_spacing = max (row->extra_line_spacing,
16887 it->max_extra_line_spacing);
16888
16889 /* End of this display line if row is continued. */
16890 if (row->continued_p || row->ends_at_zv_p)
16891 break;
16892 }
16893
16894 at_end_of_line:
16895 /* Is this a line end? If yes, we're also done, after making
16896 sure that a non-default face is extended up to the right
16897 margin of the window. */
16898 if (ITERATOR_AT_END_OF_LINE_P (it))
16899 {
16900 int used_before = row->used[TEXT_AREA];
16901
16902 row->ends_in_newline_from_string_p = STRINGP (it->object);
16903
16904 #ifdef HAVE_WINDOW_SYSTEM
16905 /* Add a space at the end of the line that is used to
16906 display the cursor there. */
16907 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16908 append_space_for_newline (it, 0);
16909 #endif /* HAVE_WINDOW_SYSTEM */
16910
16911 /* Extend the face to the end of the line. */
16912 extend_face_to_end_of_line (it);
16913
16914 /* Make sure we have the position. */
16915 if (used_before == 0)
16916 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16917
16918 /* Consume the line end. This skips over invisible lines. */
16919 set_iterator_to_next (it, 1);
16920 it->continuation_lines_width = 0;
16921 break;
16922 }
16923
16924 /* Proceed with next display element. Note that this skips
16925 over lines invisible because of selective display. */
16926 set_iterator_to_next (it, 1);
16927
16928 /* If we truncate lines, we are done when the last displayed
16929 glyphs reach past the right margin of the window. */
16930 if (it->line_wrap == TRUNCATE
16931 && (FRAME_WINDOW_P (it->f)
16932 ? (it->current_x >= it->last_visible_x)
16933 : (it->current_x > it->last_visible_x)))
16934 {
16935 /* Maybe add truncation glyphs. */
16936 if (!FRAME_WINDOW_P (it->f))
16937 {
16938 int i, n;
16939
16940 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16941 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16942 break;
16943
16944 for (n = row->used[TEXT_AREA]; i < n; ++i)
16945 {
16946 row->used[TEXT_AREA] = i;
16947 produce_special_glyphs (it, IT_TRUNCATION);
16948 }
16949 }
16950 #ifdef HAVE_WINDOW_SYSTEM
16951 else
16952 {
16953 /* Don't truncate if we can overflow newline into fringe. */
16954 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16955 {
16956 if (!get_next_display_element (it))
16957 {
16958 it->continuation_lines_width = 0;
16959 row->ends_at_zv_p = 1;
16960 row->exact_window_width_line_p = 1;
16961 break;
16962 }
16963 if (ITERATOR_AT_END_OF_LINE_P (it))
16964 {
16965 row->exact_window_width_line_p = 1;
16966 goto at_end_of_line;
16967 }
16968 }
16969 }
16970 #endif /* HAVE_WINDOW_SYSTEM */
16971
16972 row->truncated_on_right_p = 1;
16973 it->continuation_lines_width = 0;
16974 reseat_at_next_visible_line_start (it, 0);
16975 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16976 it->hpos = hpos_before;
16977 it->current_x = x_before;
16978 break;
16979 }
16980 }
16981
16982 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16983 at the left window margin. */
16984 if (it->first_visible_x
16985 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16986 {
16987 if (!FRAME_WINDOW_P (it->f))
16988 insert_left_trunc_glyphs (it);
16989 row->truncated_on_left_p = 1;
16990 }
16991
16992 /* If the start of this line is the overlay arrow-position, then
16993 mark this glyph row as the one containing the overlay arrow.
16994 This is clearly a mess with variable size fonts. It would be
16995 better to let it be displayed like cursors under X. */
16996 if ((row->displays_text_p || !overlay_arrow_seen)
16997 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16998 !NILP (overlay_arrow_string)))
16999 {
17000 /* Overlay arrow in window redisplay is a fringe bitmap. */
17001 if (STRINGP (overlay_arrow_string))
17002 {
17003 struct glyph_row *arrow_row
17004 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17005 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17006 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17007 struct glyph *p = row->glyphs[TEXT_AREA];
17008 struct glyph *p2, *end;
17009
17010 /* Copy the arrow glyphs. */
17011 while (glyph < arrow_end)
17012 *p++ = *glyph++;
17013
17014 /* Throw away padding glyphs. */
17015 p2 = p;
17016 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17017 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17018 ++p2;
17019 if (p2 > p)
17020 {
17021 while (p2 < end)
17022 *p++ = *p2++;
17023 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17024 }
17025 }
17026 else
17027 {
17028 xassert (INTEGERP (overlay_arrow_string));
17029 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17030 }
17031 overlay_arrow_seen = 1;
17032 }
17033
17034 /* Compute pixel dimensions of this line. */
17035 compute_line_metrics (it);
17036
17037 /* Remember the position at which this line ends. */
17038 row->end = it->current;
17039
17040 /* Record whether this row ends inside an ellipsis. */
17041 row->ends_in_ellipsis_p
17042 = (it->method == GET_FROM_DISPLAY_VECTOR
17043 && it->ellipsis_p);
17044
17045 /* Save fringe bitmaps in this row. */
17046 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17047 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17048 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17049 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17050
17051 it->left_user_fringe_bitmap = 0;
17052 it->left_user_fringe_face_id = 0;
17053 it->right_user_fringe_bitmap = 0;
17054 it->right_user_fringe_face_id = 0;
17055
17056 /* Maybe set the cursor. */
17057 if (it->w->cursor.vpos < 0
17058 && PT >= MATRIX_ROW_START_CHARPOS (row)
17059 && PT <= MATRIX_ROW_END_CHARPOS (row)
17060 && cursor_row_p (it->w, row))
17061 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17062
17063 /* Highlight trailing whitespace. */
17064 if (!NILP (Vshow_trailing_whitespace))
17065 highlight_trailing_whitespace (it->f, it->glyph_row);
17066
17067 /* Prepare for the next line. This line starts horizontally at (X
17068 HPOS) = (0 0). Vertical positions are incremented. As a
17069 convenience for the caller, IT->glyph_row is set to the next
17070 row to be used. */
17071 it->current_x = it->hpos = 0;
17072 it->current_y += row->height;
17073 ++it->vpos;
17074 ++it->glyph_row;
17075 it->start = it->current;
17076 return row->displays_text_p;
17077 }
17078
17079
17080 \f
17081 /***********************************************************************
17082 Menu Bar
17083 ***********************************************************************/
17084
17085 /* Redisplay the menu bar in the frame for window W.
17086
17087 The menu bar of X frames that don't have X toolkit support is
17088 displayed in a special window W->frame->menu_bar_window.
17089
17090 The menu bar of terminal frames is treated specially as far as
17091 glyph matrices are concerned. Menu bar lines are not part of
17092 windows, so the update is done directly on the frame matrix rows
17093 for the menu bar. */
17094
17095 static void
17096 display_menu_bar (w)
17097 struct window *w;
17098 {
17099 struct frame *f = XFRAME (WINDOW_FRAME (w));
17100 struct it it;
17101 Lisp_Object items;
17102 int i;
17103
17104 /* Don't do all this for graphical frames. */
17105 #ifdef HAVE_NTGUI
17106 if (FRAME_W32_P (f))
17107 return;
17108 #endif
17109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17110 if (FRAME_X_P (f))
17111 return;
17112 #endif
17113
17114 #ifdef HAVE_NS
17115 if (FRAME_NS_P (f))
17116 return;
17117 #endif /* HAVE_NS */
17118
17119 #ifdef USE_X_TOOLKIT
17120 xassert (!FRAME_WINDOW_P (f));
17121 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17122 it.first_visible_x = 0;
17123 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17124 #else /* not USE_X_TOOLKIT */
17125 if (FRAME_WINDOW_P (f))
17126 {
17127 /* Menu bar lines are displayed in the desired matrix of the
17128 dummy window menu_bar_window. */
17129 struct window *menu_w;
17130 xassert (WINDOWP (f->menu_bar_window));
17131 menu_w = XWINDOW (f->menu_bar_window);
17132 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17133 MENU_FACE_ID);
17134 it.first_visible_x = 0;
17135 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17136 }
17137 else
17138 {
17139 /* This is a TTY frame, i.e. character hpos/vpos are used as
17140 pixel x/y. */
17141 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17142 MENU_FACE_ID);
17143 it.first_visible_x = 0;
17144 it.last_visible_x = FRAME_COLS (f);
17145 }
17146 #endif /* not USE_X_TOOLKIT */
17147
17148 if (! mode_line_inverse_video)
17149 /* Force the menu-bar to be displayed in the default face. */
17150 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17151
17152 /* Clear all rows of the menu bar. */
17153 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17154 {
17155 struct glyph_row *row = it.glyph_row + i;
17156 clear_glyph_row (row);
17157 row->enabled_p = 1;
17158 row->full_width_p = 1;
17159 }
17160
17161 /* Display all items of the menu bar. */
17162 items = FRAME_MENU_BAR_ITEMS (it.f);
17163 for (i = 0; i < XVECTOR (items)->size; i += 4)
17164 {
17165 Lisp_Object string;
17166
17167 /* Stop at nil string. */
17168 string = AREF (items, i + 1);
17169 if (NILP (string))
17170 break;
17171
17172 /* Remember where item was displayed. */
17173 ASET (items, i + 3, make_number (it.hpos));
17174
17175 /* Display the item, pad with one space. */
17176 if (it.current_x < it.last_visible_x)
17177 display_string (NULL, string, Qnil, 0, 0, &it,
17178 SCHARS (string) + 1, 0, 0, -1);
17179 }
17180
17181 /* Fill out the line with spaces. */
17182 if (it.current_x < it.last_visible_x)
17183 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17184
17185 /* Compute the total height of the lines. */
17186 compute_line_metrics (&it);
17187 }
17188
17189
17190 \f
17191 /***********************************************************************
17192 Mode Line
17193 ***********************************************************************/
17194
17195 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17196 FORCE is non-zero, redisplay mode lines unconditionally.
17197 Otherwise, redisplay only mode lines that are garbaged. Value is
17198 the number of windows whose mode lines were redisplayed. */
17199
17200 static int
17201 redisplay_mode_lines (window, force)
17202 Lisp_Object window;
17203 int force;
17204 {
17205 int nwindows = 0;
17206
17207 while (!NILP (window))
17208 {
17209 struct window *w = XWINDOW (window);
17210
17211 if (WINDOWP (w->hchild))
17212 nwindows += redisplay_mode_lines (w->hchild, force);
17213 else if (WINDOWP (w->vchild))
17214 nwindows += redisplay_mode_lines (w->vchild, force);
17215 else if (force
17216 || FRAME_GARBAGED_P (XFRAME (w->frame))
17217 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17218 {
17219 struct text_pos lpoint;
17220 struct buffer *old = current_buffer;
17221
17222 /* Set the window's buffer for the mode line display. */
17223 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17224 set_buffer_internal_1 (XBUFFER (w->buffer));
17225
17226 /* Point refers normally to the selected window. For any
17227 other window, set up appropriate value. */
17228 if (!EQ (window, selected_window))
17229 {
17230 struct text_pos pt;
17231
17232 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17233 if (CHARPOS (pt) < BEGV)
17234 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17235 else if (CHARPOS (pt) > (ZV - 1))
17236 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17237 else
17238 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17239 }
17240
17241 /* Display mode lines. */
17242 clear_glyph_matrix (w->desired_matrix);
17243 if (display_mode_lines (w))
17244 {
17245 ++nwindows;
17246 w->must_be_updated_p = 1;
17247 }
17248
17249 /* Restore old settings. */
17250 set_buffer_internal_1 (old);
17251 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17252 }
17253
17254 window = w->next;
17255 }
17256
17257 return nwindows;
17258 }
17259
17260
17261 /* Display the mode and/or top line of window W. Value is the number
17262 of mode lines displayed. */
17263
17264 static int
17265 display_mode_lines (w)
17266 struct window *w;
17267 {
17268 Lisp_Object old_selected_window, old_selected_frame;
17269 int n = 0;
17270
17271 old_selected_frame = selected_frame;
17272 selected_frame = w->frame;
17273 old_selected_window = selected_window;
17274 XSETWINDOW (selected_window, w);
17275
17276 /* These will be set while the mode line specs are processed. */
17277 line_number_displayed = 0;
17278 w->column_number_displayed = Qnil;
17279
17280 if (WINDOW_WANTS_MODELINE_P (w))
17281 {
17282 struct window *sel_w = XWINDOW (old_selected_window);
17283
17284 /* Select mode line face based on the real selected window. */
17285 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17286 current_buffer->mode_line_format);
17287 ++n;
17288 }
17289
17290 if (WINDOW_WANTS_HEADER_LINE_P (w))
17291 {
17292 display_mode_line (w, HEADER_LINE_FACE_ID,
17293 current_buffer->header_line_format);
17294 ++n;
17295 }
17296
17297 selected_frame = old_selected_frame;
17298 selected_window = old_selected_window;
17299 return n;
17300 }
17301
17302
17303 /* Display mode or top line of window W. FACE_ID specifies which line
17304 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17305 FORMAT is the mode line format to display. Value is the pixel
17306 height of the mode line displayed. */
17307
17308 static int
17309 display_mode_line (w, face_id, format)
17310 struct window *w;
17311 enum face_id face_id;
17312 Lisp_Object format;
17313 {
17314 struct it it;
17315 struct face *face;
17316 int count = SPECPDL_INDEX ();
17317
17318 init_iterator (&it, w, -1, -1, NULL, face_id);
17319 /* Don't extend on a previously drawn mode-line.
17320 This may happen if called from pos_visible_p. */
17321 it.glyph_row->enabled_p = 0;
17322 prepare_desired_row (it.glyph_row);
17323
17324 it.glyph_row->mode_line_p = 1;
17325
17326 if (! mode_line_inverse_video)
17327 /* Force the mode-line to be displayed in the default face. */
17328 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17329
17330 record_unwind_protect (unwind_format_mode_line,
17331 format_mode_line_unwind_data (NULL, Qnil, 0));
17332
17333 mode_line_target = MODE_LINE_DISPLAY;
17334
17335 /* Temporarily make frame's keyboard the current kboard so that
17336 kboard-local variables in the mode_line_format will get the right
17337 values. */
17338 push_kboard (FRAME_KBOARD (it.f));
17339 record_unwind_save_match_data ();
17340 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17341 pop_kboard ();
17342
17343 unbind_to (count, Qnil);
17344
17345 /* Fill up with spaces. */
17346 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17347
17348 compute_line_metrics (&it);
17349 it.glyph_row->full_width_p = 1;
17350 it.glyph_row->continued_p = 0;
17351 it.glyph_row->truncated_on_left_p = 0;
17352 it.glyph_row->truncated_on_right_p = 0;
17353
17354 /* Make a 3D mode-line have a shadow at its right end. */
17355 face = FACE_FROM_ID (it.f, face_id);
17356 extend_face_to_end_of_line (&it);
17357 if (face->box != FACE_NO_BOX)
17358 {
17359 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17360 + it.glyph_row->used[TEXT_AREA] - 1);
17361 last->right_box_line_p = 1;
17362 }
17363
17364 return it.glyph_row->height;
17365 }
17366
17367 /* Move element ELT in LIST to the front of LIST.
17368 Return the updated list. */
17369
17370 static Lisp_Object
17371 move_elt_to_front (elt, list)
17372 Lisp_Object elt, list;
17373 {
17374 register Lisp_Object tail, prev;
17375 register Lisp_Object tem;
17376
17377 tail = list;
17378 prev = Qnil;
17379 while (CONSP (tail))
17380 {
17381 tem = XCAR (tail);
17382
17383 if (EQ (elt, tem))
17384 {
17385 /* Splice out the link TAIL. */
17386 if (NILP (prev))
17387 list = XCDR (tail);
17388 else
17389 Fsetcdr (prev, XCDR (tail));
17390
17391 /* Now make it the first. */
17392 Fsetcdr (tail, list);
17393 return tail;
17394 }
17395 else
17396 prev = tail;
17397 tail = XCDR (tail);
17398 QUIT;
17399 }
17400
17401 /* Not found--return unchanged LIST. */
17402 return list;
17403 }
17404
17405 /* Contribute ELT to the mode line for window IT->w. How it
17406 translates into text depends on its data type.
17407
17408 IT describes the display environment in which we display, as usual.
17409
17410 DEPTH is the depth in recursion. It is used to prevent
17411 infinite recursion here.
17412
17413 FIELD_WIDTH is the number of characters the display of ELT should
17414 occupy in the mode line, and PRECISION is the maximum number of
17415 characters to display from ELT's representation. See
17416 display_string for details.
17417
17418 Returns the hpos of the end of the text generated by ELT.
17419
17420 PROPS is a property list to add to any string we encounter.
17421
17422 If RISKY is nonzero, remove (disregard) any properties in any string
17423 we encounter, and ignore :eval and :propertize.
17424
17425 The global variable `mode_line_target' determines whether the
17426 output is passed to `store_mode_line_noprop',
17427 `store_mode_line_string', or `display_string'. */
17428
17429 static int
17430 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17431 struct it *it;
17432 int depth;
17433 int field_width, precision;
17434 Lisp_Object elt, props;
17435 int risky;
17436 {
17437 int n = 0, field, prec;
17438 int literal = 0;
17439
17440 tail_recurse:
17441 if (depth > 100)
17442 elt = build_string ("*too-deep*");
17443
17444 depth++;
17445
17446 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17447 {
17448 case Lisp_String:
17449 {
17450 /* A string: output it and check for %-constructs within it. */
17451 unsigned char c;
17452 int offset = 0;
17453
17454 if (SCHARS (elt) > 0
17455 && (!NILP (props) || risky))
17456 {
17457 Lisp_Object oprops, aelt;
17458 oprops = Ftext_properties_at (make_number (0), elt);
17459
17460 /* If the starting string's properties are not what
17461 we want, translate the string. Also, if the string
17462 is risky, do that anyway. */
17463
17464 if (NILP (Fequal (props, oprops)) || risky)
17465 {
17466 /* If the starting string has properties,
17467 merge the specified ones onto the existing ones. */
17468 if (! NILP (oprops) && !risky)
17469 {
17470 Lisp_Object tem;
17471
17472 oprops = Fcopy_sequence (oprops);
17473 tem = props;
17474 while (CONSP (tem))
17475 {
17476 oprops = Fplist_put (oprops, XCAR (tem),
17477 XCAR (XCDR (tem)));
17478 tem = XCDR (XCDR (tem));
17479 }
17480 props = oprops;
17481 }
17482
17483 aelt = Fassoc (elt, mode_line_proptrans_alist);
17484 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17485 {
17486 /* AELT is what we want. Move it to the front
17487 without consing. */
17488 elt = XCAR (aelt);
17489 mode_line_proptrans_alist
17490 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17491 }
17492 else
17493 {
17494 Lisp_Object tem;
17495
17496 /* If AELT has the wrong props, it is useless.
17497 so get rid of it. */
17498 if (! NILP (aelt))
17499 mode_line_proptrans_alist
17500 = Fdelq (aelt, mode_line_proptrans_alist);
17501
17502 elt = Fcopy_sequence (elt);
17503 Fset_text_properties (make_number (0), Flength (elt),
17504 props, elt);
17505 /* Add this item to mode_line_proptrans_alist. */
17506 mode_line_proptrans_alist
17507 = Fcons (Fcons (elt, props),
17508 mode_line_proptrans_alist);
17509 /* Truncate mode_line_proptrans_alist
17510 to at most 50 elements. */
17511 tem = Fnthcdr (make_number (50),
17512 mode_line_proptrans_alist);
17513 if (! NILP (tem))
17514 XSETCDR (tem, Qnil);
17515 }
17516 }
17517 }
17518
17519 offset = 0;
17520
17521 if (literal)
17522 {
17523 prec = precision - n;
17524 switch (mode_line_target)
17525 {
17526 case MODE_LINE_NOPROP:
17527 case MODE_LINE_TITLE:
17528 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17529 break;
17530 case MODE_LINE_STRING:
17531 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17532 break;
17533 case MODE_LINE_DISPLAY:
17534 n += display_string (NULL, elt, Qnil, 0, 0, it,
17535 0, prec, 0, STRING_MULTIBYTE (elt));
17536 break;
17537 }
17538
17539 break;
17540 }
17541
17542 /* Handle the non-literal case. */
17543
17544 while ((precision <= 0 || n < precision)
17545 && SREF (elt, offset) != 0
17546 && (mode_line_target != MODE_LINE_DISPLAY
17547 || it->current_x < it->last_visible_x))
17548 {
17549 int last_offset = offset;
17550
17551 /* Advance to end of string or next format specifier. */
17552 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17553 ;
17554
17555 if (offset - 1 != last_offset)
17556 {
17557 int nchars, nbytes;
17558
17559 /* Output to end of string or up to '%'. Field width
17560 is length of string. Don't output more than
17561 PRECISION allows us. */
17562 offset--;
17563
17564 prec = c_string_width (SDATA (elt) + last_offset,
17565 offset - last_offset, precision - n,
17566 &nchars, &nbytes);
17567
17568 switch (mode_line_target)
17569 {
17570 case MODE_LINE_NOPROP:
17571 case MODE_LINE_TITLE:
17572 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17573 break;
17574 case MODE_LINE_STRING:
17575 {
17576 int bytepos = last_offset;
17577 int charpos = string_byte_to_char (elt, bytepos);
17578 int endpos = (precision <= 0
17579 ? string_byte_to_char (elt, offset)
17580 : charpos + nchars);
17581
17582 n += store_mode_line_string (NULL,
17583 Fsubstring (elt, make_number (charpos),
17584 make_number (endpos)),
17585 0, 0, 0, Qnil);
17586 }
17587 break;
17588 case MODE_LINE_DISPLAY:
17589 {
17590 int bytepos = last_offset;
17591 int charpos = string_byte_to_char (elt, bytepos);
17592
17593 if (precision <= 0)
17594 nchars = string_byte_to_char (elt, offset) - charpos;
17595 n += display_string (NULL, elt, Qnil, 0, charpos,
17596 it, 0, nchars, 0,
17597 STRING_MULTIBYTE (elt));
17598 }
17599 break;
17600 }
17601 }
17602 else /* c == '%' */
17603 {
17604 int percent_position = offset;
17605
17606 /* Get the specified minimum width. Zero means
17607 don't pad. */
17608 field = 0;
17609 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17610 field = field * 10 + c - '0';
17611
17612 /* Don't pad beyond the total padding allowed. */
17613 if (field_width - n > 0 && field > field_width - n)
17614 field = field_width - n;
17615
17616 /* Note that either PRECISION <= 0 or N < PRECISION. */
17617 prec = precision - n;
17618
17619 if (c == 'M')
17620 n += display_mode_element (it, depth, field, prec,
17621 Vglobal_mode_string, props,
17622 risky);
17623 else if (c != 0)
17624 {
17625 int multibyte;
17626 int bytepos, charpos;
17627 unsigned char *spec;
17628
17629 bytepos = percent_position;
17630 charpos = (STRING_MULTIBYTE (elt)
17631 ? string_byte_to_char (elt, bytepos)
17632 : bytepos);
17633 spec
17634 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17635
17636 switch (mode_line_target)
17637 {
17638 case MODE_LINE_NOPROP:
17639 case MODE_LINE_TITLE:
17640 n += store_mode_line_noprop (spec, field, prec);
17641 break;
17642 case MODE_LINE_STRING:
17643 {
17644 int len = strlen (spec);
17645 Lisp_Object tem = make_string (spec, len);
17646 props = Ftext_properties_at (make_number (charpos), elt);
17647 /* Should only keep face property in props */
17648 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17649 }
17650 break;
17651 case MODE_LINE_DISPLAY:
17652 {
17653 int nglyphs_before, nwritten;
17654
17655 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17656 nwritten = display_string (spec, Qnil, elt,
17657 charpos, 0, it,
17658 field, prec, 0,
17659 multibyte);
17660
17661 /* Assign to the glyphs written above the
17662 string where the `%x' came from, position
17663 of the `%'. */
17664 if (nwritten > 0)
17665 {
17666 struct glyph *glyph
17667 = (it->glyph_row->glyphs[TEXT_AREA]
17668 + nglyphs_before);
17669 int i;
17670
17671 for (i = 0; i < nwritten; ++i)
17672 {
17673 glyph[i].object = elt;
17674 glyph[i].charpos = charpos;
17675 }
17676
17677 n += nwritten;
17678 }
17679 }
17680 break;
17681 }
17682 }
17683 else /* c == 0 */
17684 break;
17685 }
17686 }
17687 }
17688 break;
17689
17690 case Lisp_Symbol:
17691 /* A symbol: process the value of the symbol recursively
17692 as if it appeared here directly. Avoid error if symbol void.
17693 Special case: if value of symbol is a string, output the string
17694 literally. */
17695 {
17696 register Lisp_Object tem;
17697
17698 /* If the variable is not marked as risky to set
17699 then its contents are risky to use. */
17700 if (NILP (Fget (elt, Qrisky_local_variable)))
17701 risky = 1;
17702
17703 tem = Fboundp (elt);
17704 if (!NILP (tem))
17705 {
17706 tem = Fsymbol_value (elt);
17707 /* If value is a string, output that string literally:
17708 don't check for % within it. */
17709 if (STRINGP (tem))
17710 literal = 1;
17711
17712 if (!EQ (tem, elt))
17713 {
17714 /* Give up right away for nil or t. */
17715 elt = tem;
17716 goto tail_recurse;
17717 }
17718 }
17719 }
17720 break;
17721
17722 case Lisp_Cons:
17723 {
17724 register Lisp_Object car, tem;
17725
17726 /* A cons cell: five distinct cases.
17727 If first element is :eval or :propertize, do something special.
17728 If first element is a string or a cons, process all the elements
17729 and effectively concatenate them.
17730 If first element is a negative number, truncate displaying cdr to
17731 at most that many characters. If positive, pad (with spaces)
17732 to at least that many characters.
17733 If first element is a symbol, process the cadr or caddr recursively
17734 according to whether the symbol's value is non-nil or nil. */
17735 car = XCAR (elt);
17736 if (EQ (car, QCeval))
17737 {
17738 /* An element of the form (:eval FORM) means evaluate FORM
17739 and use the result as mode line elements. */
17740
17741 if (risky)
17742 break;
17743
17744 if (CONSP (XCDR (elt)))
17745 {
17746 Lisp_Object spec;
17747 spec = safe_eval (XCAR (XCDR (elt)));
17748 n += display_mode_element (it, depth, field_width - n,
17749 precision - n, spec, props,
17750 risky);
17751 }
17752 }
17753 else if (EQ (car, QCpropertize))
17754 {
17755 /* An element of the form (:propertize ELT PROPS...)
17756 means display ELT but applying properties PROPS. */
17757
17758 if (risky)
17759 break;
17760
17761 if (CONSP (XCDR (elt)))
17762 n += display_mode_element (it, depth, field_width - n,
17763 precision - n, XCAR (XCDR (elt)),
17764 XCDR (XCDR (elt)), risky);
17765 }
17766 else if (SYMBOLP (car))
17767 {
17768 tem = Fboundp (car);
17769 elt = XCDR (elt);
17770 if (!CONSP (elt))
17771 goto invalid;
17772 /* elt is now the cdr, and we know it is a cons cell.
17773 Use its car if CAR has a non-nil value. */
17774 if (!NILP (tem))
17775 {
17776 tem = Fsymbol_value (car);
17777 if (!NILP (tem))
17778 {
17779 elt = XCAR (elt);
17780 goto tail_recurse;
17781 }
17782 }
17783 /* Symbol's value is nil (or symbol is unbound)
17784 Get the cddr of the original list
17785 and if possible find the caddr and use that. */
17786 elt = XCDR (elt);
17787 if (NILP (elt))
17788 break;
17789 else if (!CONSP (elt))
17790 goto invalid;
17791 elt = XCAR (elt);
17792 goto tail_recurse;
17793 }
17794 else if (INTEGERP (car))
17795 {
17796 register int lim = XINT (car);
17797 elt = XCDR (elt);
17798 if (lim < 0)
17799 {
17800 /* Negative int means reduce maximum width. */
17801 if (precision <= 0)
17802 precision = -lim;
17803 else
17804 precision = min (precision, -lim);
17805 }
17806 else if (lim > 0)
17807 {
17808 /* Padding specified. Don't let it be more than
17809 current maximum. */
17810 if (precision > 0)
17811 lim = min (precision, lim);
17812
17813 /* If that's more padding than already wanted, queue it.
17814 But don't reduce padding already specified even if
17815 that is beyond the current truncation point. */
17816 field_width = max (lim, field_width);
17817 }
17818 goto tail_recurse;
17819 }
17820 else if (STRINGP (car) || CONSP (car))
17821 {
17822 register int limit = 50;
17823 /* Limit is to protect against circular lists. */
17824 while (CONSP (elt)
17825 && --limit > 0
17826 && (precision <= 0 || n < precision))
17827 {
17828 n += display_mode_element (it, depth,
17829 /* Do padding only after the last
17830 element in the list. */
17831 (! CONSP (XCDR (elt))
17832 ? field_width - n
17833 : 0),
17834 precision - n, XCAR (elt),
17835 props, risky);
17836 elt = XCDR (elt);
17837 }
17838 }
17839 }
17840 break;
17841
17842 default:
17843 invalid:
17844 elt = build_string ("*invalid*");
17845 goto tail_recurse;
17846 }
17847
17848 /* Pad to FIELD_WIDTH. */
17849 if (field_width > 0 && n < field_width)
17850 {
17851 switch (mode_line_target)
17852 {
17853 case MODE_LINE_NOPROP:
17854 case MODE_LINE_TITLE:
17855 n += store_mode_line_noprop ("", field_width - n, 0);
17856 break;
17857 case MODE_LINE_STRING:
17858 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17859 break;
17860 case MODE_LINE_DISPLAY:
17861 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17862 0, 0, 0);
17863 break;
17864 }
17865 }
17866
17867 return n;
17868 }
17869
17870 /* Store a mode-line string element in mode_line_string_list.
17871
17872 If STRING is non-null, display that C string. Otherwise, the Lisp
17873 string LISP_STRING is displayed.
17874
17875 FIELD_WIDTH is the minimum number of output glyphs to produce.
17876 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17877 with spaces. FIELD_WIDTH <= 0 means don't pad.
17878
17879 PRECISION is the maximum number of characters to output from
17880 STRING. PRECISION <= 0 means don't truncate the string.
17881
17882 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17883 properties to the string.
17884
17885 PROPS are the properties to add to the string.
17886 The mode_line_string_face face property is always added to the string.
17887 */
17888
17889 static int
17890 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17891 char *string;
17892 Lisp_Object lisp_string;
17893 int copy_string;
17894 int field_width;
17895 int precision;
17896 Lisp_Object props;
17897 {
17898 int len;
17899 int n = 0;
17900
17901 if (string != NULL)
17902 {
17903 len = strlen (string);
17904 if (precision > 0 && len > precision)
17905 len = precision;
17906 lisp_string = make_string (string, len);
17907 if (NILP (props))
17908 props = mode_line_string_face_prop;
17909 else if (!NILP (mode_line_string_face))
17910 {
17911 Lisp_Object face = Fplist_get (props, Qface);
17912 props = Fcopy_sequence (props);
17913 if (NILP (face))
17914 face = mode_line_string_face;
17915 else
17916 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17917 props = Fplist_put (props, Qface, face);
17918 }
17919 Fadd_text_properties (make_number (0), make_number (len),
17920 props, lisp_string);
17921 }
17922 else
17923 {
17924 len = XFASTINT (Flength (lisp_string));
17925 if (precision > 0 && len > precision)
17926 {
17927 len = precision;
17928 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17929 precision = -1;
17930 }
17931 if (!NILP (mode_line_string_face))
17932 {
17933 Lisp_Object face;
17934 if (NILP (props))
17935 props = Ftext_properties_at (make_number (0), lisp_string);
17936 face = Fplist_get (props, Qface);
17937 if (NILP (face))
17938 face = mode_line_string_face;
17939 else
17940 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17941 props = Fcons (Qface, Fcons (face, Qnil));
17942 if (copy_string)
17943 lisp_string = Fcopy_sequence (lisp_string);
17944 }
17945 if (!NILP (props))
17946 Fadd_text_properties (make_number (0), make_number (len),
17947 props, lisp_string);
17948 }
17949
17950 if (len > 0)
17951 {
17952 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17953 n += len;
17954 }
17955
17956 if (field_width > len)
17957 {
17958 field_width -= len;
17959 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17960 if (!NILP (props))
17961 Fadd_text_properties (make_number (0), make_number (field_width),
17962 props, lisp_string);
17963 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17964 n += field_width;
17965 }
17966
17967 return n;
17968 }
17969
17970
17971 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17972 1, 4, 0,
17973 doc: /* Format a string out of a mode line format specification.
17974 First arg FORMAT specifies the mode line format (see `mode-line-format'
17975 for details) to use.
17976
17977 Optional second arg FACE specifies the face property to put
17978 on all characters for which no face is specified.
17979 The value t means whatever face the window's mode line currently uses
17980 \(either `mode-line' or `mode-line-inactive', depending).
17981 A value of nil means the default is no face property.
17982 If FACE is an integer, the value string has no text properties.
17983
17984 Optional third and fourth args WINDOW and BUFFER specify the window
17985 and buffer to use as the context for the formatting (defaults
17986 are the selected window and the window's buffer). */)
17987 (format, face, window, buffer)
17988 Lisp_Object format, face, window, buffer;
17989 {
17990 struct it it;
17991 int len;
17992 struct window *w;
17993 struct buffer *old_buffer = NULL;
17994 int face_id = -1;
17995 int no_props = INTEGERP (face);
17996 int count = SPECPDL_INDEX ();
17997 Lisp_Object str;
17998 int string_start = 0;
17999
18000 if (NILP (window))
18001 window = selected_window;
18002 CHECK_WINDOW (window);
18003 w = XWINDOW (window);
18004
18005 if (NILP (buffer))
18006 buffer = w->buffer;
18007 CHECK_BUFFER (buffer);
18008
18009 /* Make formatting the modeline a non-op when noninteractive, otherwise
18010 there will be problems later caused by a partially initialized frame. */
18011 if (NILP (format) || noninteractive)
18012 return empty_unibyte_string;
18013
18014 if (no_props)
18015 face = Qnil;
18016
18017 if (!NILP (face))
18018 {
18019 if (EQ (face, Qt))
18020 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18021 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18022 }
18023
18024 if (face_id < 0)
18025 face_id = DEFAULT_FACE_ID;
18026
18027 if (XBUFFER (buffer) != current_buffer)
18028 old_buffer = current_buffer;
18029
18030 /* Save things including mode_line_proptrans_alist,
18031 and set that to nil so that we don't alter the outer value. */
18032 record_unwind_protect (unwind_format_mode_line,
18033 format_mode_line_unwind_data
18034 (old_buffer, selected_window, 1));
18035 mode_line_proptrans_alist = Qnil;
18036
18037 Fselect_window (window, Qt);
18038 if (old_buffer)
18039 set_buffer_internal_1 (XBUFFER (buffer));
18040
18041 init_iterator (&it, w, -1, -1, NULL, face_id);
18042
18043 if (no_props)
18044 {
18045 mode_line_target = MODE_LINE_NOPROP;
18046 mode_line_string_face_prop = Qnil;
18047 mode_line_string_list = Qnil;
18048 string_start = MODE_LINE_NOPROP_LEN (0);
18049 }
18050 else
18051 {
18052 mode_line_target = MODE_LINE_STRING;
18053 mode_line_string_list = Qnil;
18054 mode_line_string_face = face;
18055 mode_line_string_face_prop
18056 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18057 }
18058
18059 push_kboard (FRAME_KBOARD (it.f));
18060 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18061 pop_kboard ();
18062
18063 if (no_props)
18064 {
18065 len = MODE_LINE_NOPROP_LEN (string_start);
18066 str = make_string (mode_line_noprop_buf + string_start, len);
18067 }
18068 else
18069 {
18070 mode_line_string_list = Fnreverse (mode_line_string_list);
18071 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18072 empty_unibyte_string);
18073 }
18074
18075 unbind_to (count, Qnil);
18076 return str;
18077 }
18078
18079 /* Write a null-terminated, right justified decimal representation of
18080 the positive integer D to BUF using a minimal field width WIDTH. */
18081
18082 static void
18083 pint2str (buf, width, d)
18084 register char *buf;
18085 register int width;
18086 register int d;
18087 {
18088 register char *p = buf;
18089
18090 if (d <= 0)
18091 *p++ = '0';
18092 else
18093 {
18094 while (d > 0)
18095 {
18096 *p++ = d % 10 + '0';
18097 d /= 10;
18098 }
18099 }
18100
18101 for (width -= (int) (p - buf); width > 0; --width)
18102 *p++ = ' ';
18103 *p-- = '\0';
18104 while (p > buf)
18105 {
18106 d = *buf;
18107 *buf++ = *p;
18108 *p-- = d;
18109 }
18110 }
18111
18112 /* Write a null-terminated, right justified decimal and "human
18113 readable" representation of the nonnegative integer D to BUF using
18114 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18115
18116 static const char power_letter[] =
18117 {
18118 0, /* not used */
18119 'k', /* kilo */
18120 'M', /* mega */
18121 'G', /* giga */
18122 'T', /* tera */
18123 'P', /* peta */
18124 'E', /* exa */
18125 'Z', /* zetta */
18126 'Y' /* yotta */
18127 };
18128
18129 static void
18130 pint2hrstr (buf, width, d)
18131 char *buf;
18132 int width;
18133 int d;
18134 {
18135 /* We aim to represent the nonnegative integer D as
18136 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18137 int quotient = d;
18138 int remainder = 0;
18139 /* -1 means: do not use TENTHS. */
18140 int tenths = -1;
18141 int exponent = 0;
18142
18143 /* Length of QUOTIENT.TENTHS as a string. */
18144 int length;
18145
18146 char * psuffix;
18147 char * p;
18148
18149 if (1000 <= quotient)
18150 {
18151 /* Scale to the appropriate EXPONENT. */
18152 do
18153 {
18154 remainder = quotient % 1000;
18155 quotient /= 1000;
18156 exponent++;
18157 }
18158 while (1000 <= quotient);
18159
18160 /* Round to nearest and decide whether to use TENTHS or not. */
18161 if (quotient <= 9)
18162 {
18163 tenths = remainder / 100;
18164 if (50 <= remainder % 100)
18165 {
18166 if (tenths < 9)
18167 tenths++;
18168 else
18169 {
18170 quotient++;
18171 if (quotient == 10)
18172 tenths = -1;
18173 else
18174 tenths = 0;
18175 }
18176 }
18177 }
18178 else
18179 if (500 <= remainder)
18180 {
18181 if (quotient < 999)
18182 quotient++;
18183 else
18184 {
18185 quotient = 1;
18186 exponent++;
18187 tenths = 0;
18188 }
18189 }
18190 }
18191
18192 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18193 if (tenths == -1 && quotient <= 99)
18194 if (quotient <= 9)
18195 length = 1;
18196 else
18197 length = 2;
18198 else
18199 length = 3;
18200 p = psuffix = buf + max (width, length);
18201
18202 /* Print EXPONENT. */
18203 if (exponent)
18204 *psuffix++ = power_letter[exponent];
18205 *psuffix = '\0';
18206
18207 /* Print TENTHS. */
18208 if (tenths >= 0)
18209 {
18210 *--p = '0' + tenths;
18211 *--p = '.';
18212 }
18213
18214 /* Print QUOTIENT. */
18215 do
18216 {
18217 int digit = quotient % 10;
18218 *--p = '0' + digit;
18219 }
18220 while ((quotient /= 10) != 0);
18221
18222 /* Print leading spaces. */
18223 while (buf < p)
18224 *--p = ' ';
18225 }
18226
18227 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18228 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18229 type of CODING_SYSTEM. Return updated pointer into BUF. */
18230
18231 static unsigned char invalid_eol_type[] = "(*invalid*)";
18232
18233 static char *
18234 decode_mode_spec_coding (coding_system, buf, eol_flag)
18235 Lisp_Object coding_system;
18236 register char *buf;
18237 int eol_flag;
18238 {
18239 Lisp_Object val;
18240 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18241 const unsigned char *eol_str;
18242 int eol_str_len;
18243 /* The EOL conversion we are using. */
18244 Lisp_Object eoltype;
18245
18246 val = CODING_SYSTEM_SPEC (coding_system);
18247 eoltype = Qnil;
18248
18249 if (!VECTORP (val)) /* Not yet decided. */
18250 {
18251 if (multibyte)
18252 *buf++ = '-';
18253 if (eol_flag)
18254 eoltype = eol_mnemonic_undecided;
18255 /* Don't mention EOL conversion if it isn't decided. */
18256 }
18257 else
18258 {
18259 Lisp_Object attrs;
18260 Lisp_Object eolvalue;
18261
18262 attrs = AREF (val, 0);
18263 eolvalue = AREF (val, 2);
18264
18265 if (multibyte)
18266 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18267
18268 if (eol_flag)
18269 {
18270 /* The EOL conversion that is normal on this system. */
18271
18272 if (NILP (eolvalue)) /* Not yet decided. */
18273 eoltype = eol_mnemonic_undecided;
18274 else if (VECTORP (eolvalue)) /* Not yet decided. */
18275 eoltype = eol_mnemonic_undecided;
18276 else /* eolvalue is Qunix, Qdos, or Qmac. */
18277 eoltype = (EQ (eolvalue, Qunix)
18278 ? eol_mnemonic_unix
18279 : (EQ (eolvalue, Qdos) == 1
18280 ? eol_mnemonic_dos : eol_mnemonic_mac));
18281 }
18282 }
18283
18284 if (eol_flag)
18285 {
18286 /* Mention the EOL conversion if it is not the usual one. */
18287 if (STRINGP (eoltype))
18288 {
18289 eol_str = SDATA (eoltype);
18290 eol_str_len = SBYTES (eoltype);
18291 }
18292 else if (CHARACTERP (eoltype))
18293 {
18294 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18295 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18296 eol_str = tmp;
18297 }
18298 else
18299 {
18300 eol_str = invalid_eol_type;
18301 eol_str_len = sizeof (invalid_eol_type) - 1;
18302 }
18303 bcopy (eol_str, buf, eol_str_len);
18304 buf += eol_str_len;
18305 }
18306
18307 return buf;
18308 }
18309
18310 /* Return a string for the output of a mode line %-spec for window W,
18311 generated by character C. PRECISION >= 0 means don't return a
18312 string longer than that value. FIELD_WIDTH > 0 means pad the
18313 string returned with spaces to that value. Return 1 in *MULTIBYTE
18314 if the result is multibyte text.
18315
18316 Note we operate on the current buffer for most purposes,
18317 the exception being w->base_line_pos. */
18318
18319 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18320
18321 static char *
18322 decode_mode_spec (w, c, field_width, precision, multibyte)
18323 struct window *w;
18324 register int c;
18325 int field_width, precision;
18326 int *multibyte;
18327 {
18328 Lisp_Object obj;
18329 struct frame *f = XFRAME (WINDOW_FRAME (w));
18330 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18331 struct buffer *b = current_buffer;
18332
18333 obj = Qnil;
18334 *multibyte = 0;
18335
18336 switch (c)
18337 {
18338 case '*':
18339 if (!NILP (b->read_only))
18340 return "%";
18341 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18342 return "*";
18343 return "-";
18344
18345 case '+':
18346 /* This differs from %* only for a modified read-only buffer. */
18347 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18348 return "*";
18349 if (!NILP (b->read_only))
18350 return "%";
18351 return "-";
18352
18353 case '&':
18354 /* This differs from %* in ignoring read-only-ness. */
18355 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18356 return "*";
18357 return "-";
18358
18359 case '%':
18360 return "%";
18361
18362 case '[':
18363 {
18364 int i;
18365 char *p;
18366
18367 if (command_loop_level > 5)
18368 return "[[[... ";
18369 p = decode_mode_spec_buf;
18370 for (i = 0; i < command_loop_level; i++)
18371 *p++ = '[';
18372 *p = 0;
18373 return decode_mode_spec_buf;
18374 }
18375
18376 case ']':
18377 {
18378 int i;
18379 char *p;
18380
18381 if (command_loop_level > 5)
18382 return " ...]]]";
18383 p = decode_mode_spec_buf;
18384 for (i = 0; i < command_loop_level; i++)
18385 *p++ = ']';
18386 *p = 0;
18387 return decode_mode_spec_buf;
18388 }
18389
18390 case '-':
18391 {
18392 register int i;
18393
18394 /* Let lots_of_dashes be a string of infinite length. */
18395 if (mode_line_target == MODE_LINE_NOPROP ||
18396 mode_line_target == MODE_LINE_STRING)
18397 return "--";
18398 if (field_width <= 0
18399 || field_width > sizeof (lots_of_dashes))
18400 {
18401 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18402 decode_mode_spec_buf[i] = '-';
18403 decode_mode_spec_buf[i] = '\0';
18404 return decode_mode_spec_buf;
18405 }
18406 else
18407 return lots_of_dashes;
18408 }
18409
18410 case 'b':
18411 obj = b->name;
18412 break;
18413
18414 case 'c':
18415 /* %c and %l are ignored in `frame-title-format'.
18416 (In redisplay_internal, the frame title is drawn _before_ the
18417 windows are updated, so the stuff which depends on actual
18418 window contents (such as %l) may fail to render properly, or
18419 even crash emacs.) */
18420 if (mode_line_target == MODE_LINE_TITLE)
18421 return "";
18422 else
18423 {
18424 int col = (int) current_column (); /* iftc */
18425 w->column_number_displayed = make_number (col);
18426 pint2str (decode_mode_spec_buf, field_width, col);
18427 return decode_mode_spec_buf;
18428 }
18429
18430 case 'e':
18431 #ifndef SYSTEM_MALLOC
18432 {
18433 if (NILP (Vmemory_full))
18434 return "";
18435 else
18436 return "!MEM FULL! ";
18437 }
18438 #else
18439 return "";
18440 #endif
18441
18442 case 'F':
18443 /* %F displays the frame name. */
18444 if (!NILP (f->title))
18445 return (char *) SDATA (f->title);
18446 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18447 return (char *) SDATA (f->name);
18448 return "Emacs";
18449
18450 case 'f':
18451 obj = b->filename;
18452 break;
18453
18454 case 'i':
18455 {
18456 int size = ZV - BEGV;
18457 pint2str (decode_mode_spec_buf, field_width, size);
18458 return decode_mode_spec_buf;
18459 }
18460
18461 case 'I':
18462 {
18463 int size = ZV - BEGV;
18464 pint2hrstr (decode_mode_spec_buf, field_width, size);
18465 return decode_mode_spec_buf;
18466 }
18467
18468 case 'l':
18469 {
18470 int startpos, startpos_byte, line, linepos, linepos_byte;
18471 int topline, nlines, junk, height;
18472
18473 /* %c and %l are ignored in `frame-title-format'. */
18474 if (mode_line_target == MODE_LINE_TITLE)
18475 return "";
18476
18477 startpos = XMARKER (w->start)->charpos;
18478 startpos_byte = marker_byte_position (w->start);
18479 height = WINDOW_TOTAL_LINES (w);
18480
18481 /* If we decided that this buffer isn't suitable for line numbers,
18482 don't forget that too fast. */
18483 if (EQ (w->base_line_pos, w->buffer))
18484 goto no_value;
18485 /* But do forget it, if the window shows a different buffer now. */
18486 else if (BUFFERP (w->base_line_pos))
18487 w->base_line_pos = Qnil;
18488
18489 /* If the buffer is very big, don't waste time. */
18490 if (INTEGERP (Vline_number_display_limit)
18491 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18492 {
18493 w->base_line_pos = Qnil;
18494 w->base_line_number = Qnil;
18495 goto no_value;
18496 }
18497
18498 if (INTEGERP (w->base_line_number)
18499 && INTEGERP (w->base_line_pos)
18500 && XFASTINT (w->base_line_pos) <= startpos)
18501 {
18502 line = XFASTINT (w->base_line_number);
18503 linepos = XFASTINT (w->base_line_pos);
18504 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18505 }
18506 else
18507 {
18508 line = 1;
18509 linepos = BUF_BEGV (b);
18510 linepos_byte = BUF_BEGV_BYTE (b);
18511 }
18512
18513 /* Count lines from base line to window start position. */
18514 nlines = display_count_lines (linepos, linepos_byte,
18515 startpos_byte,
18516 startpos, &junk);
18517
18518 topline = nlines + line;
18519
18520 /* Determine a new base line, if the old one is too close
18521 or too far away, or if we did not have one.
18522 "Too close" means it's plausible a scroll-down would
18523 go back past it. */
18524 if (startpos == BUF_BEGV (b))
18525 {
18526 w->base_line_number = make_number (topline);
18527 w->base_line_pos = make_number (BUF_BEGV (b));
18528 }
18529 else if (nlines < height + 25 || nlines > height * 3 + 50
18530 || linepos == BUF_BEGV (b))
18531 {
18532 int limit = BUF_BEGV (b);
18533 int limit_byte = BUF_BEGV_BYTE (b);
18534 int position;
18535 int distance = (height * 2 + 30) * line_number_display_limit_width;
18536
18537 if (startpos - distance > limit)
18538 {
18539 limit = startpos - distance;
18540 limit_byte = CHAR_TO_BYTE (limit);
18541 }
18542
18543 nlines = display_count_lines (startpos, startpos_byte,
18544 limit_byte,
18545 - (height * 2 + 30),
18546 &position);
18547 /* If we couldn't find the lines we wanted within
18548 line_number_display_limit_width chars per line,
18549 give up on line numbers for this window. */
18550 if (position == limit_byte && limit == startpos - distance)
18551 {
18552 w->base_line_pos = w->buffer;
18553 w->base_line_number = Qnil;
18554 goto no_value;
18555 }
18556
18557 w->base_line_number = make_number (topline - nlines);
18558 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18559 }
18560
18561 /* Now count lines from the start pos to point. */
18562 nlines = display_count_lines (startpos, startpos_byte,
18563 PT_BYTE, PT, &junk);
18564
18565 /* Record that we did display the line number. */
18566 line_number_displayed = 1;
18567
18568 /* Make the string to show. */
18569 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18570 return decode_mode_spec_buf;
18571 no_value:
18572 {
18573 char* p = decode_mode_spec_buf;
18574 int pad = field_width - 2;
18575 while (pad-- > 0)
18576 *p++ = ' ';
18577 *p++ = '?';
18578 *p++ = '?';
18579 *p = '\0';
18580 return decode_mode_spec_buf;
18581 }
18582 }
18583 break;
18584
18585 case 'm':
18586 obj = b->mode_name;
18587 break;
18588
18589 case 'n':
18590 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18591 return " Narrow";
18592 break;
18593
18594 case 'p':
18595 {
18596 int pos = marker_position (w->start);
18597 int total = BUF_ZV (b) - BUF_BEGV (b);
18598
18599 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18600 {
18601 if (pos <= BUF_BEGV (b))
18602 return "All";
18603 else
18604 return "Bottom";
18605 }
18606 else if (pos <= BUF_BEGV (b))
18607 return "Top";
18608 else
18609 {
18610 if (total > 1000000)
18611 /* Do it differently for a large value, to avoid overflow. */
18612 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18613 else
18614 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18615 /* We can't normally display a 3-digit number,
18616 so get us a 2-digit number that is close. */
18617 if (total == 100)
18618 total = 99;
18619 sprintf (decode_mode_spec_buf, "%2d%%", total);
18620 return decode_mode_spec_buf;
18621 }
18622 }
18623
18624 /* Display percentage of size above the bottom of the screen. */
18625 case 'P':
18626 {
18627 int toppos = marker_position (w->start);
18628 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18629 int total = BUF_ZV (b) - BUF_BEGV (b);
18630
18631 if (botpos >= BUF_ZV (b))
18632 {
18633 if (toppos <= BUF_BEGV (b))
18634 return "All";
18635 else
18636 return "Bottom";
18637 }
18638 else
18639 {
18640 if (total > 1000000)
18641 /* Do it differently for a large value, to avoid overflow. */
18642 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18643 else
18644 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18645 /* We can't normally display a 3-digit number,
18646 so get us a 2-digit number that is close. */
18647 if (total == 100)
18648 total = 99;
18649 if (toppos <= BUF_BEGV (b))
18650 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18651 else
18652 sprintf (decode_mode_spec_buf, "%2d%%", total);
18653 return decode_mode_spec_buf;
18654 }
18655 }
18656
18657 case 's':
18658 /* status of process */
18659 obj = Fget_buffer_process (Fcurrent_buffer ());
18660 if (NILP (obj))
18661 return "no process";
18662 #ifdef subprocesses
18663 obj = Fsymbol_name (Fprocess_status (obj));
18664 #endif
18665 break;
18666
18667 case '@':
18668 {
18669 Lisp_Object val;
18670 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18671 if (NILP (val))
18672 return "-";
18673 else
18674 return "@";
18675 }
18676
18677 case 't': /* indicate TEXT or BINARY */
18678 #ifdef MODE_LINE_BINARY_TEXT
18679 return MODE_LINE_BINARY_TEXT (b);
18680 #else
18681 return "T";
18682 #endif
18683
18684 case 'z':
18685 /* coding-system (not including end-of-line format) */
18686 case 'Z':
18687 /* coding-system (including end-of-line type) */
18688 {
18689 int eol_flag = (c == 'Z');
18690 char *p = decode_mode_spec_buf;
18691
18692 if (! FRAME_WINDOW_P (f))
18693 {
18694 /* No need to mention EOL here--the terminal never needs
18695 to do EOL conversion. */
18696 p = decode_mode_spec_coding (CODING_ID_NAME
18697 (FRAME_KEYBOARD_CODING (f)->id),
18698 p, 0);
18699 p = decode_mode_spec_coding (CODING_ID_NAME
18700 (FRAME_TERMINAL_CODING (f)->id),
18701 p, 0);
18702 }
18703 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18704 p, eol_flag);
18705
18706 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18707 #ifdef subprocesses
18708 obj = Fget_buffer_process (Fcurrent_buffer ());
18709 if (PROCESSP (obj))
18710 {
18711 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18712 p, eol_flag);
18713 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18714 p, eol_flag);
18715 }
18716 #endif /* subprocesses */
18717 #endif /* 0 */
18718 *p = 0;
18719 return decode_mode_spec_buf;
18720 }
18721 }
18722
18723 if (STRINGP (obj))
18724 {
18725 *multibyte = STRING_MULTIBYTE (obj);
18726 return (char *) SDATA (obj);
18727 }
18728 else
18729 return "";
18730 }
18731
18732
18733 /* Count up to COUNT lines starting from START / START_BYTE.
18734 But don't go beyond LIMIT_BYTE.
18735 Return the number of lines thus found (always nonnegative).
18736
18737 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18738
18739 static int
18740 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18741 int start, start_byte, limit_byte, count;
18742 int *byte_pos_ptr;
18743 {
18744 register unsigned char *cursor;
18745 unsigned char *base;
18746
18747 register int ceiling;
18748 register unsigned char *ceiling_addr;
18749 int orig_count = count;
18750
18751 /* If we are not in selective display mode,
18752 check only for newlines. */
18753 int selective_display = (!NILP (current_buffer->selective_display)
18754 && !INTEGERP (current_buffer->selective_display));
18755
18756 if (count > 0)
18757 {
18758 while (start_byte < limit_byte)
18759 {
18760 ceiling = BUFFER_CEILING_OF (start_byte);
18761 ceiling = min (limit_byte - 1, ceiling);
18762 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18763 base = (cursor = BYTE_POS_ADDR (start_byte));
18764 while (1)
18765 {
18766 if (selective_display)
18767 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18768 ;
18769 else
18770 while (*cursor != '\n' && ++cursor != ceiling_addr)
18771 ;
18772
18773 if (cursor != ceiling_addr)
18774 {
18775 if (--count == 0)
18776 {
18777 start_byte += cursor - base + 1;
18778 *byte_pos_ptr = start_byte;
18779 return orig_count;
18780 }
18781 else
18782 if (++cursor == ceiling_addr)
18783 break;
18784 }
18785 else
18786 break;
18787 }
18788 start_byte += cursor - base;
18789 }
18790 }
18791 else
18792 {
18793 while (start_byte > limit_byte)
18794 {
18795 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18796 ceiling = max (limit_byte, ceiling);
18797 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18798 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18799 while (1)
18800 {
18801 if (selective_display)
18802 while (--cursor != ceiling_addr
18803 && *cursor != '\n' && *cursor != 015)
18804 ;
18805 else
18806 while (--cursor != ceiling_addr && *cursor != '\n')
18807 ;
18808
18809 if (cursor != ceiling_addr)
18810 {
18811 if (++count == 0)
18812 {
18813 start_byte += cursor - base + 1;
18814 *byte_pos_ptr = start_byte;
18815 /* When scanning backwards, we should
18816 not count the newline posterior to which we stop. */
18817 return - orig_count - 1;
18818 }
18819 }
18820 else
18821 break;
18822 }
18823 /* Here we add 1 to compensate for the last decrement
18824 of CURSOR, which took it past the valid range. */
18825 start_byte += cursor - base + 1;
18826 }
18827 }
18828
18829 *byte_pos_ptr = limit_byte;
18830
18831 if (count < 0)
18832 return - orig_count + count;
18833 return orig_count - count;
18834
18835 }
18836
18837
18838 \f
18839 /***********************************************************************
18840 Displaying strings
18841 ***********************************************************************/
18842
18843 /* Display a NUL-terminated string, starting with index START.
18844
18845 If STRING is non-null, display that C string. Otherwise, the Lisp
18846 string LISP_STRING is displayed.
18847
18848 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18849 FACE_STRING. Display STRING or LISP_STRING with the face at
18850 FACE_STRING_POS in FACE_STRING:
18851
18852 Display the string in the environment given by IT, but use the
18853 standard display table, temporarily.
18854
18855 FIELD_WIDTH is the minimum number of output glyphs to produce.
18856 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18857 with spaces. If STRING has more characters, more than FIELD_WIDTH
18858 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18859
18860 PRECISION is the maximum number of characters to output from
18861 STRING. PRECISION < 0 means don't truncate the string.
18862
18863 This is roughly equivalent to printf format specifiers:
18864
18865 FIELD_WIDTH PRECISION PRINTF
18866 ----------------------------------------
18867 -1 -1 %s
18868 -1 10 %.10s
18869 10 -1 %10s
18870 20 10 %20.10s
18871
18872 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18873 display them, and < 0 means obey the current buffer's value of
18874 enable_multibyte_characters.
18875
18876 Value is the number of columns displayed. */
18877
18878 static int
18879 display_string (string, lisp_string, face_string, face_string_pos,
18880 start, it, field_width, precision, max_x, multibyte)
18881 unsigned char *string;
18882 Lisp_Object lisp_string;
18883 Lisp_Object face_string;
18884 EMACS_INT face_string_pos;
18885 EMACS_INT start;
18886 struct it *it;
18887 int field_width, precision, max_x;
18888 int multibyte;
18889 {
18890 int hpos_at_start = it->hpos;
18891 int saved_face_id = it->face_id;
18892 struct glyph_row *row = it->glyph_row;
18893
18894 /* Initialize the iterator IT for iteration over STRING beginning
18895 with index START. */
18896 reseat_to_string (it, string, lisp_string, start,
18897 precision, field_width, multibyte);
18898
18899 /* If displaying STRING, set up the face of the iterator
18900 from LISP_STRING, if that's given. */
18901 if (STRINGP (face_string))
18902 {
18903 EMACS_INT endptr;
18904 struct face *face;
18905
18906 it->face_id
18907 = face_at_string_position (it->w, face_string, face_string_pos,
18908 0, it->region_beg_charpos,
18909 it->region_end_charpos,
18910 &endptr, it->base_face_id, 0);
18911 face = FACE_FROM_ID (it->f, it->face_id);
18912 it->face_box_p = face->box != FACE_NO_BOX;
18913 }
18914
18915 /* Set max_x to the maximum allowed X position. Don't let it go
18916 beyond the right edge of the window. */
18917 if (max_x <= 0)
18918 max_x = it->last_visible_x;
18919 else
18920 max_x = min (max_x, it->last_visible_x);
18921
18922 /* Skip over display elements that are not visible. because IT->w is
18923 hscrolled. */
18924 if (it->current_x < it->first_visible_x)
18925 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18926 MOVE_TO_POS | MOVE_TO_X);
18927
18928 row->ascent = it->max_ascent;
18929 row->height = it->max_ascent + it->max_descent;
18930 row->phys_ascent = it->max_phys_ascent;
18931 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18932 row->extra_line_spacing = it->max_extra_line_spacing;
18933
18934 /* This condition is for the case that we are called with current_x
18935 past last_visible_x. */
18936 while (it->current_x < max_x)
18937 {
18938 int x_before, x, n_glyphs_before, i, nglyphs;
18939
18940 /* Get the next display element. */
18941 if (!get_next_display_element (it))
18942 break;
18943
18944 /* Produce glyphs. */
18945 x_before = it->current_x;
18946 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18947 PRODUCE_GLYPHS (it);
18948
18949 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18950 i = 0;
18951 x = x_before;
18952 while (i < nglyphs)
18953 {
18954 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18955
18956 if (it->line_wrap != TRUNCATE
18957 && x + glyph->pixel_width > max_x)
18958 {
18959 /* End of continued line or max_x reached. */
18960 if (CHAR_GLYPH_PADDING_P (*glyph))
18961 {
18962 /* A wide character is unbreakable. */
18963 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18964 it->current_x = x_before;
18965 }
18966 else
18967 {
18968 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18969 it->current_x = x;
18970 }
18971 break;
18972 }
18973 else if (x + glyph->pixel_width >= it->first_visible_x)
18974 {
18975 /* Glyph is at least partially visible. */
18976 ++it->hpos;
18977 if (x < it->first_visible_x)
18978 it->glyph_row->x = x - it->first_visible_x;
18979 }
18980 else
18981 {
18982 /* Glyph is off the left margin of the display area.
18983 Should not happen. */
18984 abort ();
18985 }
18986
18987 row->ascent = max (row->ascent, it->max_ascent);
18988 row->height = max (row->height, it->max_ascent + it->max_descent);
18989 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18990 row->phys_height = max (row->phys_height,
18991 it->max_phys_ascent + it->max_phys_descent);
18992 row->extra_line_spacing = max (row->extra_line_spacing,
18993 it->max_extra_line_spacing);
18994 x += glyph->pixel_width;
18995 ++i;
18996 }
18997
18998 /* Stop if max_x reached. */
18999 if (i < nglyphs)
19000 break;
19001
19002 /* Stop at line ends. */
19003 if (ITERATOR_AT_END_OF_LINE_P (it))
19004 {
19005 it->continuation_lines_width = 0;
19006 break;
19007 }
19008
19009 set_iterator_to_next (it, 1);
19010
19011 /* Stop if truncating at the right edge. */
19012 if (it->line_wrap == TRUNCATE
19013 && it->current_x >= it->last_visible_x)
19014 {
19015 /* Add truncation mark, but don't do it if the line is
19016 truncated at a padding space. */
19017 if (IT_CHARPOS (*it) < it->string_nchars)
19018 {
19019 if (!FRAME_WINDOW_P (it->f))
19020 {
19021 int i, n;
19022
19023 if (it->current_x > it->last_visible_x)
19024 {
19025 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19026 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19027 break;
19028 for (n = row->used[TEXT_AREA]; i < n; ++i)
19029 {
19030 row->used[TEXT_AREA] = i;
19031 produce_special_glyphs (it, IT_TRUNCATION);
19032 }
19033 }
19034 produce_special_glyphs (it, IT_TRUNCATION);
19035 }
19036 it->glyph_row->truncated_on_right_p = 1;
19037 }
19038 break;
19039 }
19040 }
19041
19042 /* Maybe insert a truncation at the left. */
19043 if (it->first_visible_x
19044 && IT_CHARPOS (*it) > 0)
19045 {
19046 if (!FRAME_WINDOW_P (it->f))
19047 insert_left_trunc_glyphs (it);
19048 it->glyph_row->truncated_on_left_p = 1;
19049 }
19050
19051 it->face_id = saved_face_id;
19052
19053 /* Value is number of columns displayed. */
19054 return it->hpos - hpos_at_start;
19055 }
19056
19057
19058 \f
19059 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19060 appears as an element of LIST or as the car of an element of LIST.
19061 If PROPVAL is a list, compare each element against LIST in that
19062 way, and return 1/2 if any element of PROPVAL is found in LIST.
19063 Otherwise return 0. This function cannot quit.
19064 The return value is 2 if the text is invisible but with an ellipsis
19065 and 1 if it's invisible and without an ellipsis. */
19066
19067 int
19068 invisible_p (propval, list)
19069 register Lisp_Object propval;
19070 Lisp_Object list;
19071 {
19072 register Lisp_Object tail, proptail;
19073
19074 for (tail = list; CONSP (tail); tail = XCDR (tail))
19075 {
19076 register Lisp_Object tem;
19077 tem = XCAR (tail);
19078 if (EQ (propval, tem))
19079 return 1;
19080 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19081 return NILP (XCDR (tem)) ? 1 : 2;
19082 }
19083
19084 if (CONSP (propval))
19085 {
19086 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19087 {
19088 Lisp_Object propelt;
19089 propelt = XCAR (proptail);
19090 for (tail = list; CONSP (tail); tail = XCDR (tail))
19091 {
19092 register Lisp_Object tem;
19093 tem = XCAR (tail);
19094 if (EQ (propelt, tem))
19095 return 1;
19096 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19097 return NILP (XCDR (tem)) ? 1 : 2;
19098 }
19099 }
19100 }
19101
19102 return 0;
19103 }
19104
19105 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19106 doc: /* Non-nil if the property makes the text invisible.
19107 POS-OR-PROP can be a marker or number, in which case it is taken to be
19108 a position in the current buffer and the value of the `invisible' property
19109 is checked; or it can be some other value, which is then presumed to be the
19110 value of the `invisible' property of the text of interest.
19111 The non-nil value returned can be t for truly invisible text or something
19112 else if the text is replaced by an ellipsis. */)
19113 (pos_or_prop)
19114 Lisp_Object pos_or_prop;
19115 {
19116 Lisp_Object prop
19117 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19118 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19119 : pos_or_prop);
19120 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19121 return (invis == 0 ? Qnil
19122 : invis == 1 ? Qt
19123 : make_number (invis));
19124 }
19125
19126 /* Calculate a width or height in pixels from a specification using
19127 the following elements:
19128
19129 SPEC ::=
19130 NUM - a (fractional) multiple of the default font width/height
19131 (NUM) - specifies exactly NUM pixels
19132 UNIT - a fixed number of pixels, see below.
19133 ELEMENT - size of a display element in pixels, see below.
19134 (NUM . SPEC) - equals NUM * SPEC
19135 (+ SPEC SPEC ...) - add pixel values
19136 (- SPEC SPEC ...) - subtract pixel values
19137 (- SPEC) - negate pixel value
19138
19139 NUM ::=
19140 INT or FLOAT - a number constant
19141 SYMBOL - use symbol's (buffer local) variable binding.
19142
19143 UNIT ::=
19144 in - pixels per inch *)
19145 mm - pixels per 1/1000 meter *)
19146 cm - pixels per 1/100 meter *)
19147 width - width of current font in pixels.
19148 height - height of current font in pixels.
19149
19150 *) using the ratio(s) defined in display-pixels-per-inch.
19151
19152 ELEMENT ::=
19153
19154 left-fringe - left fringe width in pixels
19155 right-fringe - right fringe width in pixels
19156
19157 left-margin - left margin width in pixels
19158 right-margin - right margin width in pixels
19159
19160 scroll-bar - scroll-bar area width in pixels
19161
19162 Examples:
19163
19164 Pixels corresponding to 5 inches:
19165 (5 . in)
19166
19167 Total width of non-text areas on left side of window (if scroll-bar is on left):
19168 '(space :width (+ left-fringe left-margin scroll-bar))
19169
19170 Align to first text column (in header line):
19171 '(space :align-to 0)
19172
19173 Align to middle of text area minus half the width of variable `my-image'
19174 containing a loaded image:
19175 '(space :align-to (0.5 . (- text my-image)))
19176
19177 Width of left margin minus width of 1 character in the default font:
19178 '(space :width (- left-margin 1))
19179
19180 Width of left margin minus width of 2 characters in the current font:
19181 '(space :width (- left-margin (2 . width)))
19182
19183 Center 1 character over left-margin (in header line):
19184 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19185
19186 Different ways to express width of left fringe plus left margin minus one pixel:
19187 '(space :width (- (+ left-fringe left-margin) (1)))
19188 '(space :width (+ left-fringe left-margin (- (1))))
19189 '(space :width (+ left-fringe left-margin (-1)))
19190
19191 */
19192
19193 #define NUMVAL(X) \
19194 ((INTEGERP (X) || FLOATP (X)) \
19195 ? XFLOATINT (X) \
19196 : - 1)
19197
19198 int
19199 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19200 double *res;
19201 struct it *it;
19202 Lisp_Object prop;
19203 struct font *font;
19204 int width_p, *align_to;
19205 {
19206 double pixels;
19207
19208 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19209 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19210
19211 if (NILP (prop))
19212 return OK_PIXELS (0);
19213
19214 xassert (FRAME_LIVE_P (it->f));
19215
19216 if (SYMBOLP (prop))
19217 {
19218 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19219 {
19220 char *unit = SDATA (SYMBOL_NAME (prop));
19221
19222 if (unit[0] == 'i' && unit[1] == 'n')
19223 pixels = 1.0;
19224 else if (unit[0] == 'm' && unit[1] == 'm')
19225 pixels = 25.4;
19226 else if (unit[0] == 'c' && unit[1] == 'm')
19227 pixels = 2.54;
19228 else
19229 pixels = 0;
19230 if (pixels > 0)
19231 {
19232 double ppi;
19233 #ifdef HAVE_WINDOW_SYSTEM
19234 if (FRAME_WINDOW_P (it->f)
19235 && (ppi = (width_p
19236 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19237 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19238 ppi > 0))
19239 return OK_PIXELS (ppi / pixels);
19240 #endif
19241
19242 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19243 || (CONSP (Vdisplay_pixels_per_inch)
19244 && (ppi = (width_p
19245 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19246 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19247 ppi > 0)))
19248 return OK_PIXELS (ppi / pixels);
19249
19250 return 0;
19251 }
19252 }
19253
19254 #ifdef HAVE_WINDOW_SYSTEM
19255 if (EQ (prop, Qheight))
19256 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19257 if (EQ (prop, Qwidth))
19258 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19259 #else
19260 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19261 return OK_PIXELS (1);
19262 #endif
19263
19264 if (EQ (prop, Qtext))
19265 return OK_PIXELS (width_p
19266 ? window_box_width (it->w, TEXT_AREA)
19267 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19268
19269 if (align_to && *align_to < 0)
19270 {
19271 *res = 0;
19272 if (EQ (prop, Qleft))
19273 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19274 if (EQ (prop, Qright))
19275 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19276 if (EQ (prop, Qcenter))
19277 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19278 + window_box_width (it->w, TEXT_AREA) / 2);
19279 if (EQ (prop, Qleft_fringe))
19280 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19281 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19282 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19283 if (EQ (prop, Qright_fringe))
19284 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19285 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19286 : window_box_right_offset (it->w, TEXT_AREA));
19287 if (EQ (prop, Qleft_margin))
19288 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19289 if (EQ (prop, Qright_margin))
19290 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19291 if (EQ (prop, Qscroll_bar))
19292 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19293 ? 0
19294 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19295 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19296 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19297 : 0)));
19298 }
19299 else
19300 {
19301 if (EQ (prop, Qleft_fringe))
19302 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19303 if (EQ (prop, Qright_fringe))
19304 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19305 if (EQ (prop, Qleft_margin))
19306 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19307 if (EQ (prop, Qright_margin))
19308 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19309 if (EQ (prop, Qscroll_bar))
19310 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19311 }
19312
19313 prop = Fbuffer_local_value (prop, it->w->buffer);
19314 }
19315
19316 if (INTEGERP (prop) || FLOATP (prop))
19317 {
19318 int base_unit = (width_p
19319 ? FRAME_COLUMN_WIDTH (it->f)
19320 : FRAME_LINE_HEIGHT (it->f));
19321 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19322 }
19323
19324 if (CONSP (prop))
19325 {
19326 Lisp_Object car = XCAR (prop);
19327 Lisp_Object cdr = XCDR (prop);
19328
19329 if (SYMBOLP (car))
19330 {
19331 #ifdef HAVE_WINDOW_SYSTEM
19332 if (FRAME_WINDOW_P (it->f)
19333 && valid_image_p (prop))
19334 {
19335 int id = lookup_image (it->f, prop);
19336 struct image *img = IMAGE_FROM_ID (it->f, id);
19337
19338 return OK_PIXELS (width_p ? img->width : img->height);
19339 }
19340 #endif
19341 if (EQ (car, Qplus) || EQ (car, Qminus))
19342 {
19343 int first = 1;
19344 double px;
19345
19346 pixels = 0;
19347 while (CONSP (cdr))
19348 {
19349 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19350 font, width_p, align_to))
19351 return 0;
19352 if (first)
19353 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19354 else
19355 pixels += px;
19356 cdr = XCDR (cdr);
19357 }
19358 if (EQ (car, Qminus))
19359 pixels = -pixels;
19360 return OK_PIXELS (pixels);
19361 }
19362
19363 car = Fbuffer_local_value (car, it->w->buffer);
19364 }
19365
19366 if (INTEGERP (car) || FLOATP (car))
19367 {
19368 double fact;
19369 pixels = XFLOATINT (car);
19370 if (NILP (cdr))
19371 return OK_PIXELS (pixels);
19372 if (calc_pixel_width_or_height (&fact, it, cdr,
19373 font, width_p, align_to))
19374 return OK_PIXELS (pixels * fact);
19375 return 0;
19376 }
19377
19378 return 0;
19379 }
19380
19381 return 0;
19382 }
19383
19384 \f
19385 /***********************************************************************
19386 Glyph Display
19387 ***********************************************************************/
19388
19389 #ifdef HAVE_WINDOW_SYSTEM
19390
19391 #if GLYPH_DEBUG
19392
19393 void
19394 dump_glyph_string (s)
19395 struct glyph_string *s;
19396 {
19397 fprintf (stderr, "glyph string\n");
19398 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19399 s->x, s->y, s->width, s->height);
19400 fprintf (stderr, " ybase = %d\n", s->ybase);
19401 fprintf (stderr, " hl = %d\n", s->hl);
19402 fprintf (stderr, " left overhang = %d, right = %d\n",
19403 s->left_overhang, s->right_overhang);
19404 fprintf (stderr, " nchars = %d\n", s->nchars);
19405 fprintf (stderr, " extends to end of line = %d\n",
19406 s->extends_to_end_of_line_p);
19407 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19408 fprintf (stderr, " bg width = %d\n", s->background_width);
19409 }
19410
19411 #endif /* GLYPH_DEBUG */
19412
19413 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19414 of XChar2b structures for S; it can't be allocated in
19415 init_glyph_string because it must be allocated via `alloca'. W
19416 is the window on which S is drawn. ROW and AREA are the glyph row
19417 and area within the row from which S is constructed. START is the
19418 index of the first glyph structure covered by S. HL is a
19419 face-override for drawing S. */
19420
19421 #ifdef HAVE_NTGUI
19422 #define OPTIONAL_HDC(hdc) hdc,
19423 #define DECLARE_HDC(hdc) HDC hdc;
19424 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19425 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19426 #endif
19427
19428 #ifndef OPTIONAL_HDC
19429 #define OPTIONAL_HDC(hdc)
19430 #define DECLARE_HDC(hdc)
19431 #define ALLOCATE_HDC(hdc, f)
19432 #define RELEASE_HDC(hdc, f)
19433 #endif
19434
19435 static void
19436 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19437 struct glyph_string *s;
19438 DECLARE_HDC (hdc)
19439 XChar2b *char2b;
19440 struct window *w;
19441 struct glyph_row *row;
19442 enum glyph_row_area area;
19443 int start;
19444 enum draw_glyphs_face hl;
19445 {
19446 bzero (s, sizeof *s);
19447 s->w = w;
19448 s->f = XFRAME (w->frame);
19449 #ifdef HAVE_NTGUI
19450 s->hdc = hdc;
19451 #endif
19452 s->display = FRAME_X_DISPLAY (s->f);
19453 s->window = FRAME_X_WINDOW (s->f);
19454 s->char2b = char2b;
19455 s->hl = hl;
19456 s->row = row;
19457 s->area = area;
19458 s->first_glyph = row->glyphs[area] + start;
19459 s->height = row->height;
19460 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19461
19462 /* Display the internal border below the tool-bar window. */
19463 if (WINDOWP (s->f->tool_bar_window)
19464 && s->w == XWINDOW (s->f->tool_bar_window))
19465 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19466
19467 s->ybase = s->y + row->ascent;
19468 }
19469
19470
19471 /* Append the list of glyph strings with head H and tail T to the list
19472 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19473
19474 static INLINE void
19475 append_glyph_string_lists (head, tail, h, t)
19476 struct glyph_string **head, **tail;
19477 struct glyph_string *h, *t;
19478 {
19479 if (h)
19480 {
19481 if (*head)
19482 (*tail)->next = h;
19483 else
19484 *head = h;
19485 h->prev = *tail;
19486 *tail = t;
19487 }
19488 }
19489
19490
19491 /* Prepend the list of glyph strings with head H and tail T to the
19492 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19493 result. */
19494
19495 static INLINE void
19496 prepend_glyph_string_lists (head, tail, h, t)
19497 struct glyph_string **head, **tail;
19498 struct glyph_string *h, *t;
19499 {
19500 if (h)
19501 {
19502 if (*head)
19503 (*head)->prev = t;
19504 else
19505 *tail = t;
19506 t->next = *head;
19507 *head = h;
19508 }
19509 }
19510
19511
19512 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19513 Set *HEAD and *TAIL to the resulting list. */
19514
19515 static INLINE void
19516 append_glyph_string (head, tail, s)
19517 struct glyph_string **head, **tail;
19518 struct glyph_string *s;
19519 {
19520 s->next = s->prev = NULL;
19521 append_glyph_string_lists (head, tail, s, s);
19522 }
19523
19524
19525 /* Get face and two-byte form of character C in face FACE_ID on frame
19526 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19527 means we want to display multibyte text. DISPLAY_P non-zero means
19528 make sure that X resources for the face returned are allocated.
19529 Value is a pointer to a realized face that is ready for display if
19530 DISPLAY_P is non-zero. */
19531
19532 static INLINE struct face *
19533 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19534 struct frame *f;
19535 int c, face_id;
19536 XChar2b *char2b;
19537 int multibyte_p, display_p;
19538 {
19539 struct face *face = FACE_FROM_ID (f, face_id);
19540
19541 if (face->font)
19542 {
19543 unsigned code = face->font->driver->encode_char (face->font, c);
19544
19545 if (code != FONT_INVALID_CODE)
19546 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19547 else
19548 STORE_XCHAR2B (char2b, 0, 0);
19549 }
19550
19551 /* Make sure X resources of the face are allocated. */
19552 #ifdef HAVE_X_WINDOWS
19553 if (display_p)
19554 #endif
19555 {
19556 xassert (face != NULL);
19557 PREPARE_FACE_FOR_DISPLAY (f, face);
19558 }
19559
19560 return face;
19561 }
19562
19563
19564 /* Get face and two-byte form of character glyph GLYPH on frame F.
19565 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19566 a pointer to a realized face that is ready for display. */
19567
19568 static INLINE struct face *
19569 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19570 struct frame *f;
19571 struct glyph *glyph;
19572 XChar2b *char2b;
19573 int *two_byte_p;
19574 {
19575 struct face *face;
19576
19577 xassert (glyph->type == CHAR_GLYPH);
19578 face = FACE_FROM_ID (f, glyph->face_id);
19579
19580 if (two_byte_p)
19581 *two_byte_p = 0;
19582
19583 if (face->font)
19584 {
19585 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19586
19587 if (code != FONT_INVALID_CODE)
19588 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19589 else
19590 STORE_XCHAR2B (char2b, 0, 0);
19591 }
19592
19593 /* Make sure X resources of the face are allocated. */
19594 xassert (face != NULL);
19595 PREPARE_FACE_FOR_DISPLAY (f, face);
19596 return face;
19597 }
19598
19599
19600 /* Fill glyph string S with composition components specified by S->cmp.
19601
19602 BASE_FACE is the base face of the composition.
19603 S->cmp_from is the index of the first component for S.
19604
19605 OVERLAPS non-zero means S should draw the foreground only, and use
19606 its physical height for clipping. See also draw_glyphs.
19607
19608 Value is the index of a component not in S. */
19609
19610 static int
19611 fill_composite_glyph_string (s, base_face, overlaps)
19612 struct glyph_string *s;
19613 struct face *base_face;
19614 int overlaps;
19615 {
19616 int i;
19617 /* For all glyphs of this composition, starting at the offset
19618 S->cmp_from, until we reach the end of the definition or encounter a
19619 glyph that requires the different face, add it to S. */
19620 struct face *face;
19621
19622 xassert (s);
19623
19624 s->for_overlaps = overlaps;
19625 s->face = NULL;
19626 s->font = NULL;
19627 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19628 {
19629 int c = COMPOSITION_GLYPH (s->cmp, i);
19630
19631 if (c != '\t')
19632 {
19633 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19634 -1, Qnil);
19635
19636 face = get_char_face_and_encoding (s->f, c, face_id,
19637 s->char2b + i, 1, 1);
19638 if (face)
19639 {
19640 if (! s->face)
19641 {
19642 s->face = face;
19643 s->font = s->face->font;
19644 }
19645 else if (s->face != face)
19646 break;
19647 }
19648 }
19649 ++s->nchars;
19650 }
19651 s->cmp_to = i;
19652
19653 /* All glyph strings for the same composition has the same width,
19654 i.e. the width set for the first component of the composition. */
19655 s->width = s->first_glyph->pixel_width;
19656
19657 /* If the specified font could not be loaded, use the frame's
19658 default font, but record the fact that we couldn't load it in
19659 the glyph string so that we can draw rectangles for the
19660 characters of the glyph string. */
19661 if (s->font == NULL)
19662 {
19663 s->font_not_found_p = 1;
19664 s->font = FRAME_FONT (s->f);
19665 }
19666
19667 /* Adjust base line for subscript/superscript text. */
19668 s->ybase += s->first_glyph->voffset;
19669
19670 /* This glyph string must always be drawn with 16-bit functions. */
19671 s->two_byte_p = 1;
19672
19673 return s->cmp_to;
19674 }
19675
19676 static int
19677 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19678 struct glyph_string *s;
19679 int face_id;
19680 int start, end, overlaps;
19681 {
19682 struct glyph *glyph, *last;
19683 Lisp_Object lgstring;
19684 int i;
19685
19686 s->for_overlaps = overlaps;
19687 glyph = s->row->glyphs[s->area] + start;
19688 last = s->row->glyphs[s->area] + end;
19689 s->cmp_id = glyph->u.cmp.id;
19690 s->cmp_from = glyph->u.cmp.from;
19691 s->cmp_to = glyph->u.cmp.to;
19692 s->face = FACE_FROM_ID (s->f, face_id);
19693 lgstring = composition_gstring_from_id (s->cmp_id);
19694 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19695 glyph++;
19696 while (glyph < last
19697 && glyph->u.cmp.automatic
19698 && glyph->u.cmp.id == s->cmp_id)
19699 s->cmp_to = (glyph++)->u.cmp.to;
19700
19701 for (i = s->cmp_from; i < s->cmp_to; i++)
19702 {
19703 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19704 unsigned code = LGLYPH_CODE (lglyph);
19705
19706 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19707 }
19708 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19709 return glyph - s->row->glyphs[s->area];
19710 }
19711
19712
19713 /* Fill glyph string S from a sequence of character glyphs.
19714
19715 FACE_ID is the face id of the string. START is the index of the
19716 first glyph to consider, END is the index of the last + 1.
19717 OVERLAPS non-zero means S should draw the foreground only, and use
19718 its physical height for clipping. See also draw_glyphs.
19719
19720 Value is the index of the first glyph not in S. */
19721
19722 static int
19723 fill_glyph_string (s, face_id, start, end, overlaps)
19724 struct glyph_string *s;
19725 int face_id;
19726 int start, end, overlaps;
19727 {
19728 struct glyph *glyph, *last;
19729 int voffset;
19730 int glyph_not_available_p;
19731
19732 xassert (s->f == XFRAME (s->w->frame));
19733 xassert (s->nchars == 0);
19734 xassert (start >= 0 && end > start);
19735
19736 s->for_overlaps = overlaps;
19737 glyph = s->row->glyphs[s->area] + start;
19738 last = s->row->glyphs[s->area] + end;
19739 voffset = glyph->voffset;
19740 s->padding_p = glyph->padding_p;
19741 glyph_not_available_p = glyph->glyph_not_available_p;
19742
19743 while (glyph < last
19744 && glyph->type == CHAR_GLYPH
19745 && glyph->voffset == voffset
19746 /* Same face id implies same font, nowadays. */
19747 && glyph->face_id == face_id
19748 && glyph->glyph_not_available_p == glyph_not_available_p)
19749 {
19750 int two_byte_p;
19751
19752 s->face = get_glyph_face_and_encoding (s->f, glyph,
19753 s->char2b + s->nchars,
19754 &two_byte_p);
19755 s->two_byte_p = two_byte_p;
19756 ++s->nchars;
19757 xassert (s->nchars <= end - start);
19758 s->width += glyph->pixel_width;
19759 if (glyph++->padding_p != s->padding_p)
19760 break;
19761 }
19762
19763 s->font = s->face->font;
19764
19765 /* If the specified font could not be loaded, use the frame's font,
19766 but record the fact that we couldn't load it in
19767 S->font_not_found_p so that we can draw rectangles for the
19768 characters of the glyph string. */
19769 if (s->font == NULL || glyph_not_available_p)
19770 {
19771 s->font_not_found_p = 1;
19772 s->font = FRAME_FONT (s->f);
19773 }
19774
19775 /* Adjust base line for subscript/superscript text. */
19776 s->ybase += voffset;
19777
19778 xassert (s->face && s->face->gc);
19779 return glyph - s->row->glyphs[s->area];
19780 }
19781
19782
19783 /* Fill glyph string S from image glyph S->first_glyph. */
19784
19785 static void
19786 fill_image_glyph_string (s)
19787 struct glyph_string *s;
19788 {
19789 xassert (s->first_glyph->type == IMAGE_GLYPH);
19790 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19791 xassert (s->img);
19792 s->slice = s->first_glyph->slice;
19793 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19794 s->font = s->face->font;
19795 s->width = s->first_glyph->pixel_width;
19796
19797 /* Adjust base line for subscript/superscript text. */
19798 s->ybase += s->first_glyph->voffset;
19799 }
19800
19801
19802 /* Fill glyph string S from a sequence of stretch glyphs.
19803
19804 ROW is the glyph row in which the glyphs are found, AREA is the
19805 area within the row. START is the index of the first glyph to
19806 consider, END is the index of the last + 1.
19807
19808 Value is the index of the first glyph not in S. */
19809
19810 static int
19811 fill_stretch_glyph_string (s, row, area, start, end)
19812 struct glyph_string *s;
19813 struct glyph_row *row;
19814 enum glyph_row_area area;
19815 int start, end;
19816 {
19817 struct glyph *glyph, *last;
19818 int voffset, face_id;
19819
19820 xassert (s->first_glyph->type == STRETCH_GLYPH);
19821
19822 glyph = s->row->glyphs[s->area] + start;
19823 last = s->row->glyphs[s->area] + end;
19824 face_id = glyph->face_id;
19825 s->face = FACE_FROM_ID (s->f, face_id);
19826 s->font = s->face->font;
19827 s->width = glyph->pixel_width;
19828 s->nchars = 1;
19829 voffset = glyph->voffset;
19830
19831 for (++glyph;
19832 (glyph < last
19833 && glyph->type == STRETCH_GLYPH
19834 && glyph->voffset == voffset
19835 && glyph->face_id == face_id);
19836 ++glyph)
19837 s->width += glyph->pixel_width;
19838
19839 /* Adjust base line for subscript/superscript text. */
19840 s->ybase += voffset;
19841
19842 /* The case that face->gc == 0 is handled when drawing the glyph
19843 string by calling PREPARE_FACE_FOR_DISPLAY. */
19844 xassert (s->face);
19845 return glyph - s->row->glyphs[s->area];
19846 }
19847
19848 static struct font_metrics *
19849 get_per_char_metric (f, font, char2b)
19850 struct frame *f;
19851 struct font *font;
19852 XChar2b *char2b;
19853 {
19854 static struct font_metrics metrics;
19855 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19856
19857 if (! font || code == FONT_INVALID_CODE)
19858 return NULL;
19859 font->driver->text_extents (font, &code, 1, &metrics);
19860 return &metrics;
19861 }
19862
19863 /* EXPORT for RIF:
19864 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19865 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19866 assumed to be zero. */
19867
19868 void
19869 x_get_glyph_overhangs (glyph, f, left, right)
19870 struct glyph *glyph;
19871 struct frame *f;
19872 int *left, *right;
19873 {
19874 *left = *right = 0;
19875
19876 if (glyph->type == CHAR_GLYPH)
19877 {
19878 struct face *face;
19879 XChar2b char2b;
19880 struct font_metrics *pcm;
19881
19882 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19883 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19884 {
19885 if (pcm->rbearing > pcm->width)
19886 *right = pcm->rbearing - pcm->width;
19887 if (pcm->lbearing < 0)
19888 *left = -pcm->lbearing;
19889 }
19890 }
19891 else if (glyph->type == COMPOSITE_GLYPH)
19892 {
19893 if (! glyph->u.cmp.automatic)
19894 {
19895 struct composition *cmp = composition_table[glyph->u.cmp.id];
19896
19897 if (cmp->rbearing - cmp->pixel_width)
19898 *right = cmp->rbearing - cmp->pixel_width;
19899 if (cmp->lbearing < 0);
19900 *left = - cmp->lbearing;
19901 }
19902 else
19903 {
19904 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19905 struct font_metrics metrics;
19906
19907 composition_gstring_width (gstring, glyph->u.cmp.from,
19908 glyph->u.cmp.to, &metrics);
19909 if (metrics.rbearing > metrics.width)
19910 *right = metrics.rbearing;
19911 if (metrics.lbearing < 0)
19912 *left = - metrics.lbearing;
19913 }
19914 }
19915 }
19916
19917
19918 /* Return the index of the first glyph preceding glyph string S that
19919 is overwritten by S because of S's left overhang. Value is -1
19920 if no glyphs are overwritten. */
19921
19922 static int
19923 left_overwritten (s)
19924 struct glyph_string *s;
19925 {
19926 int k;
19927
19928 if (s->left_overhang)
19929 {
19930 int x = 0, i;
19931 struct glyph *glyphs = s->row->glyphs[s->area];
19932 int first = s->first_glyph - glyphs;
19933
19934 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19935 x -= glyphs[i].pixel_width;
19936
19937 k = i + 1;
19938 }
19939 else
19940 k = -1;
19941
19942 return k;
19943 }
19944
19945
19946 /* Return the index of the first glyph preceding glyph string S that
19947 is overwriting S because of its right overhang. Value is -1 if no
19948 glyph in front of S overwrites S. */
19949
19950 static int
19951 left_overwriting (s)
19952 struct glyph_string *s;
19953 {
19954 int i, k, x;
19955 struct glyph *glyphs = s->row->glyphs[s->area];
19956 int first = s->first_glyph - glyphs;
19957
19958 k = -1;
19959 x = 0;
19960 for (i = first - 1; i >= 0; --i)
19961 {
19962 int left, right;
19963 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19964 if (x + right > 0)
19965 k = i;
19966 x -= glyphs[i].pixel_width;
19967 }
19968
19969 return k;
19970 }
19971
19972
19973 /* Return the index of the last glyph following glyph string S that is
19974 overwritten by S because of S's right overhang. Value is -1 if
19975 no such glyph is found. */
19976
19977 static int
19978 right_overwritten (s)
19979 struct glyph_string *s;
19980 {
19981 int k = -1;
19982
19983 if (s->right_overhang)
19984 {
19985 int x = 0, i;
19986 struct glyph *glyphs = s->row->glyphs[s->area];
19987 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19988 int end = s->row->used[s->area];
19989
19990 for (i = first; i < end && s->right_overhang > x; ++i)
19991 x += glyphs[i].pixel_width;
19992
19993 k = i;
19994 }
19995
19996 return k;
19997 }
19998
19999
20000 /* Return the index of the last glyph following glyph string S that
20001 overwrites S because of its left overhang. Value is negative
20002 if no such glyph is found. */
20003
20004 static int
20005 right_overwriting (s)
20006 struct glyph_string *s;
20007 {
20008 int i, k, x;
20009 int end = s->row->used[s->area];
20010 struct glyph *glyphs = s->row->glyphs[s->area];
20011 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20012
20013 k = -1;
20014 x = 0;
20015 for (i = first; i < end; ++i)
20016 {
20017 int left, right;
20018 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20019 if (x - left < 0)
20020 k = i;
20021 x += glyphs[i].pixel_width;
20022 }
20023
20024 return k;
20025 }
20026
20027
20028 /* Set background width of glyph string S. START is the index of the
20029 first glyph following S. LAST_X is the right-most x-position + 1
20030 in the drawing area. */
20031
20032 static INLINE void
20033 set_glyph_string_background_width (s, start, last_x)
20034 struct glyph_string *s;
20035 int start;
20036 int last_x;
20037 {
20038 /* If the face of this glyph string has to be drawn to the end of
20039 the drawing area, set S->extends_to_end_of_line_p. */
20040
20041 if (start == s->row->used[s->area]
20042 && s->area == TEXT_AREA
20043 && ((s->row->fill_line_p
20044 && (s->hl == DRAW_NORMAL_TEXT
20045 || s->hl == DRAW_IMAGE_RAISED
20046 || s->hl == DRAW_IMAGE_SUNKEN))
20047 || s->hl == DRAW_MOUSE_FACE))
20048 s->extends_to_end_of_line_p = 1;
20049
20050 /* If S extends its face to the end of the line, set its
20051 background_width to the distance to the right edge of the drawing
20052 area. */
20053 if (s->extends_to_end_of_line_p)
20054 s->background_width = last_x - s->x + 1;
20055 else
20056 s->background_width = s->width;
20057 }
20058
20059
20060 /* Compute overhangs and x-positions for glyph string S and its
20061 predecessors, or successors. X is the starting x-position for S.
20062 BACKWARD_P non-zero means process predecessors. */
20063
20064 static void
20065 compute_overhangs_and_x (s, x, backward_p)
20066 struct glyph_string *s;
20067 int x;
20068 int backward_p;
20069 {
20070 if (backward_p)
20071 {
20072 while (s)
20073 {
20074 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20075 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20076 x -= s->width;
20077 s->x = x;
20078 s = s->prev;
20079 }
20080 }
20081 else
20082 {
20083 while (s)
20084 {
20085 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20086 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20087 s->x = x;
20088 x += s->width;
20089 s = s->next;
20090 }
20091 }
20092 }
20093
20094
20095
20096 /* The following macros are only called from draw_glyphs below.
20097 They reference the following parameters of that function directly:
20098 `w', `row', `area', and `overlap_p'
20099 as well as the following local variables:
20100 `s', `f', and `hdc' (in W32) */
20101
20102 #ifdef HAVE_NTGUI
20103 /* On W32, silently add local `hdc' variable to argument list of
20104 init_glyph_string. */
20105 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20106 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20107 #else
20108 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20109 init_glyph_string (s, char2b, w, row, area, start, hl)
20110 #endif
20111
20112 /* Add a glyph string for a stretch glyph to the list of strings
20113 between HEAD and TAIL. START is the index of the stretch 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 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20121 and below -- keep them on one line. */
20122 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20123 do \
20124 { \
20125 s = (struct glyph_string *) alloca (sizeof *s); \
20126 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20127 START = fill_stretch_glyph_string (s, row, area, START, END); \
20128 append_glyph_string (&HEAD, &TAIL, s); \
20129 s->x = (X); \
20130 } \
20131 while (0)
20132
20133
20134 /* Add a glyph string for an image glyph to the list of strings
20135 between HEAD and TAIL. START is the index of the image glyph in
20136 row area AREA of glyph row ROW. END is the index of the last glyph
20137 in that glyph row area. X is the current output position assigned
20138 to the new glyph string constructed. HL overrides that face of the
20139 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20140 is the right-most x-position of the drawing area. */
20141
20142 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20143 do \
20144 { \
20145 s = (struct glyph_string *) alloca (sizeof *s); \
20146 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20147 fill_image_glyph_string (s); \
20148 append_glyph_string (&HEAD, &TAIL, s); \
20149 ++START; \
20150 s->x = (X); \
20151 } \
20152 while (0)
20153
20154
20155 /* Add a glyph string for a sequence of character glyphs to the list
20156 of strings between HEAD and TAIL. START is the index of the first
20157 glyph in row area AREA of glyph row ROW that is part of the new
20158 glyph string. END is the index of the last glyph in that glyph row
20159 area. X is the current output position assigned to the new glyph
20160 string constructed. HL overrides that face of the glyph; e.g. it
20161 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20162 right-most x-position of the drawing area. */
20163
20164 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20165 do \
20166 { \
20167 int face_id; \
20168 XChar2b *char2b; \
20169 \
20170 face_id = (row)->glyphs[area][START].face_id; \
20171 \
20172 s = (struct glyph_string *) alloca (sizeof *s); \
20173 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20174 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20175 append_glyph_string (&HEAD, &TAIL, s); \
20176 s->x = (X); \
20177 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20178 } \
20179 while (0)
20180
20181
20182 /* Add a glyph string for a composite sequence to the list of strings
20183 between HEAD and TAIL. START is the index of the first glyph in
20184 row area AREA of glyph row ROW that is part of the new glyph
20185 string. END is the index of the last glyph in that glyph row area.
20186 X is the current output position assigned to the new glyph string
20187 constructed. HL overrides that face of the glyph; e.g. it is
20188 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20189 x-position of the drawing area. */
20190
20191 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20192 do { \
20193 int face_id = (row)->glyphs[area][START].face_id; \
20194 struct face *base_face = FACE_FROM_ID (f, face_id); \
20195 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20196 struct composition *cmp = composition_table[cmp_id]; \
20197 XChar2b *char2b; \
20198 struct glyph_string *first_s; \
20199 int n; \
20200 \
20201 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20202 \
20203 /* Make glyph_strings for each glyph sequence that is drawable by \
20204 the same face, and append them to HEAD/TAIL. */ \
20205 for (n = 0; n < cmp->glyph_len;) \
20206 { \
20207 s = (struct glyph_string *) alloca (sizeof *s); \
20208 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20209 append_glyph_string (&(HEAD), &(TAIL), s); \
20210 s->cmp = cmp; \
20211 s->cmp_from = n; \
20212 s->x = (X); \
20213 if (n == 0) \
20214 first_s = s; \
20215 n = fill_composite_glyph_string (s, base_face, overlaps); \
20216 } \
20217 \
20218 ++START; \
20219 s = first_s; \
20220 } while (0)
20221
20222
20223 /* Add a glyph string for a glyph-string sequence to the list of strings
20224 between HEAD and TAIL. */
20225
20226 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20227 do { \
20228 int face_id; \
20229 XChar2b *char2b; \
20230 Lisp_Object gstring; \
20231 \
20232 face_id = (row)->glyphs[area][START].face_id; \
20233 gstring = (composition_gstring_from_id \
20234 ((row)->glyphs[area][START].u.cmp.id)); \
20235 s = (struct glyph_string *) alloca (sizeof *s); \
20236 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20237 * LGSTRING_GLYPH_LEN (gstring)); \
20238 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20239 append_glyph_string (&(HEAD), &(TAIL), s); \
20240 s->x = (X); \
20241 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20242 } while (0)
20243
20244
20245 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20246 of AREA of glyph row ROW on window W between indices START and END.
20247 HL overrides the face for drawing glyph strings, e.g. it is
20248 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20249 x-positions of the drawing area.
20250
20251 This is an ugly monster macro construct because we must use alloca
20252 to allocate glyph strings (because draw_glyphs can be called
20253 asynchronously). */
20254
20255 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20256 do \
20257 { \
20258 HEAD = TAIL = NULL; \
20259 while (START < END) \
20260 { \
20261 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20262 switch (first_glyph->type) \
20263 { \
20264 case CHAR_GLYPH: \
20265 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20266 HL, X, LAST_X); \
20267 break; \
20268 \
20269 case COMPOSITE_GLYPH: \
20270 if (first_glyph->u.cmp.automatic) \
20271 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20272 HL, X, LAST_X); \
20273 else \
20274 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20275 HL, X, LAST_X); \
20276 break; \
20277 \
20278 case STRETCH_GLYPH: \
20279 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20280 HL, X, LAST_X); \
20281 break; \
20282 \
20283 case IMAGE_GLYPH: \
20284 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20285 HL, X, LAST_X); \
20286 break; \
20287 \
20288 default: \
20289 abort (); \
20290 } \
20291 \
20292 if (s) \
20293 { \
20294 set_glyph_string_background_width (s, START, LAST_X); \
20295 (X) += s->width; \
20296 } \
20297 } \
20298 } while (0)
20299
20300
20301 /* Draw glyphs between START and END in AREA of ROW on window W,
20302 starting at x-position X. X is relative to AREA in W. HL is a
20303 face-override with the following meaning:
20304
20305 DRAW_NORMAL_TEXT draw normally
20306 DRAW_CURSOR draw in cursor face
20307 DRAW_MOUSE_FACE draw in mouse face.
20308 DRAW_INVERSE_VIDEO draw in mode line face
20309 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20310 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20311
20312 If OVERLAPS is non-zero, draw only the foreground of characters and
20313 clip to the physical height of ROW. Non-zero value also defines
20314 the overlapping part to be drawn:
20315
20316 OVERLAPS_PRED overlap with preceding rows
20317 OVERLAPS_SUCC overlap with succeeding rows
20318 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20319 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20320
20321 Value is the x-position reached, relative to AREA of W. */
20322
20323 static int
20324 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20325 struct window *w;
20326 int x;
20327 struct glyph_row *row;
20328 enum glyph_row_area area;
20329 EMACS_INT start, end;
20330 enum draw_glyphs_face hl;
20331 int overlaps;
20332 {
20333 struct glyph_string *head, *tail;
20334 struct glyph_string *s;
20335 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20336 int i, j, x_reached, last_x, area_left = 0;
20337 struct frame *f = XFRAME (WINDOW_FRAME (w));
20338 DECLARE_HDC (hdc);
20339
20340 ALLOCATE_HDC (hdc, f);
20341
20342 /* Let's rather be paranoid than getting a SEGV. */
20343 end = min (end, row->used[area]);
20344 start = max (0, start);
20345 start = min (end, start);
20346
20347 /* Translate X to frame coordinates. Set last_x to the right
20348 end of the drawing area. */
20349 if (row->full_width_p)
20350 {
20351 /* X is relative to the left edge of W, without scroll bars
20352 or fringes. */
20353 area_left = WINDOW_LEFT_EDGE_X (w);
20354 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20355 }
20356 else
20357 {
20358 area_left = window_box_left (w, area);
20359 last_x = area_left + window_box_width (w, area);
20360 }
20361 x += area_left;
20362
20363 /* Build a doubly-linked list of glyph_string structures between
20364 head and tail from what we have to draw. Note that the macro
20365 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20366 the reason we use a separate variable `i'. */
20367 i = start;
20368 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20369 if (tail)
20370 x_reached = tail->x + tail->background_width;
20371 else
20372 x_reached = x;
20373
20374 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20375 the row, redraw some glyphs in front or following the glyph
20376 strings built above. */
20377 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20378 {
20379 struct glyph_string *h, *t;
20380 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20381 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20382 int dummy_x = 0;
20383
20384 /* If mouse highlighting is on, we may need to draw adjacent
20385 glyphs using mouse-face highlighting. */
20386 if (area == TEXT_AREA && row->mouse_face_p)
20387 {
20388 struct glyph_row *mouse_beg_row, *mouse_end_row;
20389
20390 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20391 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20392
20393 if (row >= mouse_beg_row && row <= mouse_end_row)
20394 {
20395 check_mouse_face = 1;
20396 mouse_beg_col = (row == mouse_beg_row)
20397 ? dpyinfo->mouse_face_beg_col : 0;
20398 mouse_end_col = (row == mouse_end_row)
20399 ? dpyinfo->mouse_face_end_col
20400 : row->used[TEXT_AREA];
20401 }
20402 }
20403
20404 /* Compute overhangs for all glyph strings. */
20405 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20406 for (s = head; s; s = s->next)
20407 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20408
20409 /* Prepend glyph strings for glyphs in front of the first glyph
20410 string that are overwritten because of the first glyph
20411 string's left overhang. The background of all strings
20412 prepended must be drawn because the first glyph string
20413 draws over it. */
20414 i = left_overwritten (head);
20415 if (i >= 0)
20416 {
20417 enum draw_glyphs_face overlap_hl;
20418
20419 /* If this row contains mouse highlighting, attempt to draw
20420 the overlapped glyphs with the correct highlight. This
20421 code fails if the overlap encompasses more than one glyph
20422 and mouse-highlight spans only some of these glyphs.
20423 However, making it work perfectly involves a lot more
20424 code, and I don't know if the pathological case occurs in
20425 practice, so we'll stick to this for now. --- cyd */
20426 if (check_mouse_face
20427 && mouse_beg_col < start && mouse_end_col > i)
20428 overlap_hl = DRAW_MOUSE_FACE;
20429 else
20430 overlap_hl = DRAW_NORMAL_TEXT;
20431
20432 j = i;
20433 BUILD_GLYPH_STRINGS (j, start, h, t,
20434 overlap_hl, dummy_x, last_x);
20435 compute_overhangs_and_x (t, head->x, 1);
20436 prepend_glyph_string_lists (&head, &tail, h, t);
20437 clip_head = head;
20438 }
20439
20440 /* Prepend glyph strings for glyphs in front of the first glyph
20441 string that overwrite that glyph string because of their
20442 right overhang. For these strings, only the foreground must
20443 be drawn, because it draws over the glyph string at `head'.
20444 The background must not be drawn because this would overwrite
20445 right overhangs of preceding glyphs for which no glyph
20446 strings exist. */
20447 i = left_overwriting (head);
20448 if (i >= 0)
20449 {
20450 enum draw_glyphs_face overlap_hl;
20451
20452 if (check_mouse_face
20453 && mouse_beg_col < start && mouse_end_col > i)
20454 overlap_hl = DRAW_MOUSE_FACE;
20455 else
20456 overlap_hl = DRAW_NORMAL_TEXT;
20457
20458 clip_head = head;
20459 BUILD_GLYPH_STRINGS (i, start, h, t,
20460 overlap_hl, dummy_x, last_x);
20461 for (s = h; s; s = s->next)
20462 s->background_filled_p = 1;
20463 compute_overhangs_and_x (t, head->x, 1);
20464 prepend_glyph_string_lists (&head, &tail, h, t);
20465 }
20466
20467 /* Append glyphs strings for glyphs following the last glyph
20468 string tail that are overwritten by tail. The background of
20469 these strings has to be drawn because tail's foreground draws
20470 over it. */
20471 i = right_overwritten (tail);
20472 if (i >= 0)
20473 {
20474 enum draw_glyphs_face overlap_hl;
20475
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 BUILD_GLYPH_STRINGS (end, i, h, t,
20483 overlap_hl, x, last_x);
20484 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20485 append_glyph_string_lists (&head, &tail, h, t);
20486 clip_tail = tail;
20487 }
20488
20489 /* Append glyph strings for glyphs following the last glyph
20490 string tail that overwrite tail. The foreground of such
20491 glyphs has to be drawn because it writes into the background
20492 of tail. The background must not be drawn because it could
20493 paint over the foreground of following glyphs. */
20494 i = right_overwriting (tail);
20495 if (i >= 0)
20496 {
20497 enum draw_glyphs_face overlap_hl;
20498 if (check_mouse_face
20499 && mouse_beg_col < i && mouse_end_col > end)
20500 overlap_hl = DRAW_MOUSE_FACE;
20501 else
20502 overlap_hl = DRAW_NORMAL_TEXT;
20503
20504 clip_tail = tail;
20505 i++; /* We must include the Ith glyph. */
20506 BUILD_GLYPH_STRINGS (end, i, h, t,
20507 overlap_hl, x, last_x);
20508 for (s = h; s; s = s->next)
20509 s->background_filled_p = 1;
20510 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20511 append_glyph_string_lists (&head, &tail, h, t);
20512 }
20513 if (clip_head || clip_tail)
20514 for (s = head; s; s = s->next)
20515 {
20516 s->clip_head = clip_head;
20517 s->clip_tail = clip_tail;
20518 }
20519 }
20520
20521 /* Draw all strings. */
20522 for (s = head; s; s = s->next)
20523 FRAME_RIF (f)->draw_glyph_string (s);
20524
20525 #ifndef HAVE_NS
20526 /* When focus a sole frame and move horizontally, this sets on_p to 0
20527 causing a failure to erase prev cursor position. */
20528 if (area == TEXT_AREA
20529 && !row->full_width_p
20530 /* When drawing overlapping rows, only the glyph strings'
20531 foreground is drawn, which doesn't erase a cursor
20532 completely. */
20533 && !overlaps)
20534 {
20535 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20536 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20537 : (tail ? tail->x + tail->background_width : x));
20538 x0 -= area_left;
20539 x1 -= area_left;
20540
20541 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20542 row->y, MATRIX_ROW_BOTTOM_Y (row));
20543 }
20544 #endif
20545
20546 /* Value is the x-position up to which drawn, relative to AREA of W.
20547 This doesn't include parts drawn because of overhangs. */
20548 if (row->full_width_p)
20549 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20550 else
20551 x_reached -= area_left;
20552
20553 RELEASE_HDC (hdc, f);
20554
20555 return x_reached;
20556 }
20557
20558 /* Expand row matrix if too narrow. Don't expand if area
20559 is not present. */
20560
20561 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20562 { \
20563 if (!fonts_changed_p \
20564 && (it->glyph_row->glyphs[area] \
20565 < it->glyph_row->glyphs[area + 1])) \
20566 { \
20567 it->w->ncols_scale_factor++; \
20568 fonts_changed_p = 1; \
20569 } \
20570 }
20571
20572 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20573 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20574
20575 static INLINE void
20576 append_glyph (it)
20577 struct it *it;
20578 {
20579 struct glyph *glyph;
20580 enum glyph_row_area area = it->area;
20581
20582 xassert (it->glyph_row);
20583 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20584
20585 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20586 if (glyph < it->glyph_row->glyphs[area + 1])
20587 {
20588 glyph->charpos = CHARPOS (it->position);
20589 glyph->object = it->object;
20590 if (it->pixel_width > 0)
20591 {
20592 glyph->pixel_width = it->pixel_width;
20593 glyph->padding_p = 0;
20594 }
20595 else
20596 {
20597 /* Assure at least 1-pixel width. Otherwise, cursor can't
20598 be displayed correctly. */
20599 glyph->pixel_width = 1;
20600 glyph->padding_p = 1;
20601 }
20602 glyph->ascent = it->ascent;
20603 glyph->descent = it->descent;
20604 glyph->voffset = it->voffset;
20605 glyph->type = CHAR_GLYPH;
20606 glyph->avoid_cursor_p = it->avoid_cursor_p;
20607 glyph->multibyte_p = it->multibyte_p;
20608 glyph->left_box_line_p = it->start_of_box_run_p;
20609 glyph->right_box_line_p = it->end_of_box_run_p;
20610 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20611 || it->phys_descent > it->descent);
20612 glyph->glyph_not_available_p = it->glyph_not_available_p;
20613 glyph->face_id = it->face_id;
20614 glyph->u.ch = it->char_to_display;
20615 glyph->slice = null_glyph_slice;
20616 glyph->font_type = FONT_TYPE_UNKNOWN;
20617 ++it->glyph_row->used[area];
20618 }
20619 else
20620 IT_EXPAND_MATRIX_WIDTH (it, area);
20621 }
20622
20623 /* Store one glyph for the composition IT->cmp_it.id in
20624 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20625 non-null. */
20626
20627 static INLINE void
20628 append_composite_glyph (it)
20629 struct it *it;
20630 {
20631 struct glyph *glyph;
20632 enum glyph_row_area area = it->area;
20633
20634 xassert (it->glyph_row);
20635
20636 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20637 if (glyph < it->glyph_row->glyphs[area + 1])
20638 {
20639 glyph->charpos = CHARPOS (it->position);
20640 glyph->object = it->object;
20641 glyph->pixel_width = it->pixel_width;
20642 glyph->ascent = it->ascent;
20643 glyph->descent = it->descent;
20644 glyph->voffset = it->voffset;
20645 glyph->type = COMPOSITE_GLYPH;
20646 if (it->cmp_it.ch < 0)
20647 {
20648 glyph->u.cmp.automatic = 0;
20649 glyph->u.cmp.id = it->cmp_it.id;
20650 }
20651 else
20652 {
20653 glyph->u.cmp.automatic = 1;
20654 glyph->u.cmp.id = it->cmp_it.id;
20655 glyph->u.cmp.from = it->cmp_it.from;
20656 glyph->u.cmp.to = it->cmp_it.to;
20657 }
20658 glyph->avoid_cursor_p = it->avoid_cursor_p;
20659 glyph->multibyte_p = it->multibyte_p;
20660 glyph->left_box_line_p = it->start_of_box_run_p;
20661 glyph->right_box_line_p = it->end_of_box_run_p;
20662 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20663 || it->phys_descent > it->descent);
20664 glyph->padding_p = 0;
20665 glyph->glyph_not_available_p = 0;
20666 glyph->face_id = it->face_id;
20667 glyph->slice = null_glyph_slice;
20668 glyph->font_type = FONT_TYPE_UNKNOWN;
20669 ++it->glyph_row->used[area];
20670 }
20671 else
20672 IT_EXPAND_MATRIX_WIDTH (it, area);
20673 }
20674
20675
20676 /* Change IT->ascent and IT->height according to the setting of
20677 IT->voffset. */
20678
20679 static INLINE void
20680 take_vertical_position_into_account (it)
20681 struct it *it;
20682 {
20683 if (it->voffset)
20684 {
20685 if (it->voffset < 0)
20686 /* Increase the ascent so that we can display the text higher
20687 in the line. */
20688 it->ascent -= it->voffset;
20689 else
20690 /* Increase the descent so that we can display the text lower
20691 in the line. */
20692 it->descent += it->voffset;
20693 }
20694 }
20695
20696
20697 /* Produce glyphs/get display metrics for the image IT is loaded with.
20698 See the description of struct display_iterator in dispextern.h for
20699 an overview of struct display_iterator. */
20700
20701 static void
20702 produce_image_glyph (it)
20703 struct it *it;
20704 {
20705 struct image *img;
20706 struct face *face;
20707 int glyph_ascent, crop;
20708 struct glyph_slice slice;
20709
20710 xassert (it->what == IT_IMAGE);
20711
20712 face = FACE_FROM_ID (it->f, it->face_id);
20713 xassert (face);
20714 /* Make sure X resources of the face is loaded. */
20715 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20716
20717 if (it->image_id < 0)
20718 {
20719 /* Fringe bitmap. */
20720 it->ascent = it->phys_ascent = 0;
20721 it->descent = it->phys_descent = 0;
20722 it->pixel_width = 0;
20723 it->nglyphs = 0;
20724 return;
20725 }
20726
20727 img = IMAGE_FROM_ID (it->f, it->image_id);
20728 xassert (img);
20729 /* Make sure X resources of the image is loaded. */
20730 prepare_image_for_display (it->f, img);
20731
20732 slice.x = slice.y = 0;
20733 slice.width = img->width;
20734 slice.height = img->height;
20735
20736 if (INTEGERP (it->slice.x))
20737 slice.x = XINT (it->slice.x);
20738 else if (FLOATP (it->slice.x))
20739 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20740
20741 if (INTEGERP (it->slice.y))
20742 slice.y = XINT (it->slice.y);
20743 else if (FLOATP (it->slice.y))
20744 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20745
20746 if (INTEGERP (it->slice.width))
20747 slice.width = XINT (it->slice.width);
20748 else if (FLOATP (it->slice.width))
20749 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20750
20751 if (INTEGERP (it->slice.height))
20752 slice.height = XINT (it->slice.height);
20753 else if (FLOATP (it->slice.height))
20754 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20755
20756 if (slice.x >= img->width)
20757 slice.x = img->width;
20758 if (slice.y >= img->height)
20759 slice.y = img->height;
20760 if (slice.x + slice.width >= img->width)
20761 slice.width = img->width - slice.x;
20762 if (slice.y + slice.height > img->height)
20763 slice.height = img->height - slice.y;
20764
20765 if (slice.width == 0 || slice.height == 0)
20766 return;
20767
20768 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20769
20770 it->descent = slice.height - glyph_ascent;
20771 if (slice.y == 0)
20772 it->descent += img->vmargin;
20773 if (slice.y + slice.height == img->height)
20774 it->descent += img->vmargin;
20775 it->phys_descent = it->descent;
20776
20777 it->pixel_width = slice.width;
20778 if (slice.x == 0)
20779 it->pixel_width += img->hmargin;
20780 if (slice.x + slice.width == img->width)
20781 it->pixel_width += img->hmargin;
20782
20783 /* It's quite possible for images to have an ascent greater than
20784 their height, so don't get confused in that case. */
20785 if (it->descent < 0)
20786 it->descent = 0;
20787
20788 #if 0 /* this breaks image tiling */
20789 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20790 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20791 if (face_ascent > it->ascent)
20792 it->ascent = it->phys_ascent = face_ascent;
20793 #endif
20794
20795 it->nglyphs = 1;
20796
20797 if (face->box != FACE_NO_BOX)
20798 {
20799 if (face->box_line_width > 0)
20800 {
20801 if (slice.y == 0)
20802 it->ascent += face->box_line_width;
20803 if (slice.y + slice.height == img->height)
20804 it->descent += face->box_line_width;
20805 }
20806
20807 if (it->start_of_box_run_p && slice.x == 0)
20808 it->pixel_width += eabs (face->box_line_width);
20809 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20810 it->pixel_width += eabs (face->box_line_width);
20811 }
20812
20813 take_vertical_position_into_account (it);
20814
20815 /* Automatically crop wide image glyphs at right edge so we can
20816 draw the cursor on same display row. */
20817 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20818 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20819 {
20820 it->pixel_width -= crop;
20821 slice.width -= crop;
20822 }
20823
20824 if (it->glyph_row)
20825 {
20826 struct glyph *glyph;
20827 enum glyph_row_area area = it->area;
20828
20829 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20830 if (glyph < it->glyph_row->glyphs[area + 1])
20831 {
20832 glyph->charpos = CHARPOS (it->position);
20833 glyph->object = it->object;
20834 glyph->pixel_width = it->pixel_width;
20835 glyph->ascent = glyph_ascent;
20836 glyph->descent = it->descent;
20837 glyph->voffset = it->voffset;
20838 glyph->type = IMAGE_GLYPH;
20839 glyph->avoid_cursor_p = it->avoid_cursor_p;
20840 glyph->multibyte_p = it->multibyte_p;
20841 glyph->left_box_line_p = it->start_of_box_run_p;
20842 glyph->right_box_line_p = it->end_of_box_run_p;
20843 glyph->overlaps_vertically_p = 0;
20844 glyph->padding_p = 0;
20845 glyph->glyph_not_available_p = 0;
20846 glyph->face_id = it->face_id;
20847 glyph->u.img_id = img->id;
20848 glyph->slice = slice;
20849 glyph->font_type = FONT_TYPE_UNKNOWN;
20850 ++it->glyph_row->used[area];
20851 }
20852 else
20853 IT_EXPAND_MATRIX_WIDTH (it, area);
20854 }
20855 }
20856
20857
20858 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20859 of the glyph, WIDTH and HEIGHT are the width and height of the
20860 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20861
20862 static void
20863 append_stretch_glyph (it, object, width, height, ascent)
20864 struct it *it;
20865 Lisp_Object object;
20866 int width, height;
20867 int ascent;
20868 {
20869 struct glyph *glyph;
20870 enum glyph_row_area area = it->area;
20871
20872 xassert (ascent >= 0 && ascent <= height);
20873
20874 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20875 if (glyph < it->glyph_row->glyphs[area + 1])
20876 {
20877 glyph->charpos = CHARPOS (it->position);
20878 glyph->object = object;
20879 glyph->pixel_width = width;
20880 glyph->ascent = ascent;
20881 glyph->descent = height - ascent;
20882 glyph->voffset = it->voffset;
20883 glyph->type = STRETCH_GLYPH;
20884 glyph->avoid_cursor_p = it->avoid_cursor_p;
20885 glyph->multibyte_p = it->multibyte_p;
20886 glyph->left_box_line_p = it->start_of_box_run_p;
20887 glyph->right_box_line_p = it->end_of_box_run_p;
20888 glyph->overlaps_vertically_p = 0;
20889 glyph->padding_p = 0;
20890 glyph->glyph_not_available_p = 0;
20891 glyph->face_id = it->face_id;
20892 glyph->u.stretch.ascent = ascent;
20893 glyph->u.stretch.height = height;
20894 glyph->slice = null_glyph_slice;
20895 glyph->font_type = FONT_TYPE_UNKNOWN;
20896 ++it->glyph_row->used[area];
20897 }
20898 else
20899 IT_EXPAND_MATRIX_WIDTH (it, area);
20900 }
20901
20902
20903 /* Produce a stretch glyph for iterator IT. IT->object is the value
20904 of the glyph property displayed. The value must be a list
20905 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20906 being recognized:
20907
20908 1. `:width WIDTH' specifies that the space should be WIDTH *
20909 canonical char width wide. WIDTH may be an integer or floating
20910 point number.
20911
20912 2. `:relative-width FACTOR' specifies that the width of the stretch
20913 should be computed from the width of the first character having the
20914 `glyph' property, and should be FACTOR times that width.
20915
20916 3. `:align-to HPOS' specifies that the space should be wide enough
20917 to reach HPOS, a value in canonical character units.
20918
20919 Exactly one of the above pairs must be present.
20920
20921 4. `:height HEIGHT' specifies that the height of the stretch produced
20922 should be HEIGHT, measured in canonical character units.
20923
20924 5. `:relative-height FACTOR' specifies that the height of the
20925 stretch should be FACTOR times the height of the characters having
20926 the glyph property.
20927
20928 Either none or exactly one of 4 or 5 must be present.
20929
20930 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20931 of the stretch should be used for the ascent of the stretch.
20932 ASCENT must be in the range 0 <= ASCENT <= 100. */
20933
20934 static void
20935 produce_stretch_glyph (it)
20936 struct it *it;
20937 {
20938 /* (space :width WIDTH :height HEIGHT ...) */
20939 Lisp_Object prop, plist;
20940 int width = 0, height = 0, align_to = -1;
20941 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20942 int ascent = 0;
20943 double tem;
20944 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20945 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20946
20947 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20948
20949 /* List should start with `space'. */
20950 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20951 plist = XCDR (it->object);
20952
20953 /* Compute the width of the stretch. */
20954 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20955 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20956 {
20957 /* Absolute width `:width WIDTH' specified and valid. */
20958 zero_width_ok_p = 1;
20959 width = (int)tem;
20960 }
20961 else if (prop = Fplist_get (plist, QCrelative_width),
20962 NUMVAL (prop) > 0)
20963 {
20964 /* Relative width `:relative-width FACTOR' specified and valid.
20965 Compute the width of the characters having the `glyph'
20966 property. */
20967 struct it it2;
20968 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20969
20970 it2 = *it;
20971 if (it->multibyte_p)
20972 {
20973 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20974 - IT_BYTEPOS (*it));
20975 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20976 }
20977 else
20978 it2.c = *p, it2.len = 1;
20979
20980 it2.glyph_row = NULL;
20981 it2.what = IT_CHARACTER;
20982 x_produce_glyphs (&it2);
20983 width = NUMVAL (prop) * it2.pixel_width;
20984 }
20985 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20986 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20987 {
20988 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20989 align_to = (align_to < 0
20990 ? 0
20991 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20992 else if (align_to < 0)
20993 align_to = window_box_left_offset (it->w, TEXT_AREA);
20994 width = max (0, (int)tem + align_to - it->current_x);
20995 zero_width_ok_p = 1;
20996 }
20997 else
20998 /* Nothing specified -> width defaults to canonical char width. */
20999 width = FRAME_COLUMN_WIDTH (it->f);
21000
21001 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21002 width = 1;
21003
21004 /* Compute height. */
21005 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21006 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21007 {
21008 height = (int)tem;
21009 zero_height_ok_p = 1;
21010 }
21011 else if (prop = Fplist_get (plist, QCrelative_height),
21012 NUMVAL (prop) > 0)
21013 height = FONT_HEIGHT (font) * NUMVAL (prop);
21014 else
21015 height = FONT_HEIGHT (font);
21016
21017 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21018 height = 1;
21019
21020 /* Compute percentage of height used for ascent. If
21021 `:ascent ASCENT' is present and valid, use that. Otherwise,
21022 derive the ascent from the font in use. */
21023 if (prop = Fplist_get (plist, QCascent),
21024 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21025 ascent = height * NUMVAL (prop) / 100.0;
21026 else if (!NILP (prop)
21027 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21028 ascent = min (max (0, (int)tem), height);
21029 else
21030 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21031
21032 if (width > 0 && it->line_wrap != TRUNCATE
21033 && it->current_x + width > it->last_visible_x)
21034 width = it->last_visible_x - it->current_x - 1;
21035
21036 if (width > 0 && height > 0 && it->glyph_row)
21037 {
21038 Lisp_Object object = it->stack[it->sp - 1].string;
21039 if (!STRINGP (object))
21040 object = it->w->buffer;
21041 append_stretch_glyph (it, object, width, height, ascent);
21042 }
21043
21044 it->pixel_width = width;
21045 it->ascent = it->phys_ascent = ascent;
21046 it->descent = it->phys_descent = height - it->ascent;
21047 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21048
21049 take_vertical_position_into_account (it);
21050 }
21051
21052 /* Calculate line-height and line-spacing properties.
21053 An integer value specifies explicit pixel value.
21054 A float value specifies relative value to current face height.
21055 A cons (float . face-name) specifies relative value to
21056 height of specified face font.
21057
21058 Returns height in pixels, or nil. */
21059
21060
21061 static Lisp_Object
21062 calc_line_height_property (it, val, font, boff, override)
21063 struct it *it;
21064 Lisp_Object val;
21065 struct font *font;
21066 int boff, override;
21067 {
21068 Lisp_Object face_name = Qnil;
21069 int ascent, descent, height;
21070
21071 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21072 return val;
21073
21074 if (CONSP (val))
21075 {
21076 face_name = XCAR (val);
21077 val = XCDR (val);
21078 if (!NUMBERP (val))
21079 val = make_number (1);
21080 if (NILP (face_name))
21081 {
21082 height = it->ascent + it->descent;
21083 goto scale;
21084 }
21085 }
21086
21087 if (NILP (face_name))
21088 {
21089 font = FRAME_FONT (it->f);
21090 boff = FRAME_BASELINE_OFFSET (it->f);
21091 }
21092 else if (EQ (face_name, Qt))
21093 {
21094 override = 0;
21095 }
21096 else
21097 {
21098 int face_id;
21099 struct face *face;
21100
21101 face_id = lookup_named_face (it->f, face_name, 0);
21102 if (face_id < 0)
21103 return make_number (-1);
21104
21105 face = FACE_FROM_ID (it->f, face_id);
21106 font = face->font;
21107 if (font == NULL)
21108 return make_number (-1);
21109 boff = font->baseline_offset;
21110 if (font->vertical_centering)
21111 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21112 }
21113
21114 ascent = FONT_BASE (font) + boff;
21115 descent = FONT_DESCENT (font) - boff;
21116
21117 if (override)
21118 {
21119 it->override_ascent = ascent;
21120 it->override_descent = descent;
21121 it->override_boff = boff;
21122 }
21123
21124 height = ascent + descent;
21125
21126 scale:
21127 if (FLOATP (val))
21128 height = (int)(XFLOAT_DATA (val) * height);
21129 else if (INTEGERP (val))
21130 height *= XINT (val);
21131
21132 return make_number (height);
21133 }
21134
21135
21136 /* RIF:
21137 Produce glyphs/get display metrics for the display element IT is
21138 loaded with. See the description of struct it in dispextern.h
21139 for an overview of struct it. */
21140
21141 void
21142 x_produce_glyphs (it)
21143 struct it *it;
21144 {
21145 int extra_line_spacing = it->extra_line_spacing;
21146
21147 it->glyph_not_available_p = 0;
21148
21149 if (it->what == IT_CHARACTER)
21150 {
21151 XChar2b char2b;
21152 struct font *font;
21153 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21154 struct font_metrics *pcm;
21155 int font_not_found_p;
21156 int boff; /* baseline offset */
21157 /* We may change it->multibyte_p upon unibyte<->multibyte
21158 conversion. So, save the current value now and restore it
21159 later.
21160
21161 Note: It seems that we don't have to record multibyte_p in
21162 struct glyph because the character code itself tells if or
21163 not the character is multibyte. Thus, in the future, we must
21164 consider eliminating the field `multibyte_p' in the struct
21165 glyph. */
21166 int saved_multibyte_p = it->multibyte_p;
21167
21168 /* Maybe translate single-byte characters to multibyte, or the
21169 other way. */
21170 it->char_to_display = it->c;
21171 if (!ASCII_BYTE_P (it->c)
21172 && ! it->multibyte_p)
21173 {
21174 if (SINGLE_BYTE_CHAR_P (it->c)
21175 && unibyte_display_via_language_environment)
21176 it->char_to_display = unibyte_char_to_multibyte (it->c);
21177 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21178 {
21179 it->multibyte_p = 1;
21180 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21181 -1, Qnil);
21182 face = FACE_FROM_ID (it->f, it->face_id);
21183 }
21184 }
21185
21186 /* Get font to use. Encode IT->char_to_display. */
21187 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21188 &char2b, it->multibyte_p, 0);
21189 font = face->font;
21190
21191 /* When no suitable font found, use the default font. */
21192 font_not_found_p = font == NULL;
21193 if (font_not_found_p)
21194 {
21195 font = FRAME_FONT (it->f);
21196 boff = FRAME_BASELINE_OFFSET (it->f);
21197 }
21198 else
21199 {
21200 boff = font->baseline_offset;
21201 if (font->vertical_centering)
21202 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21203 }
21204
21205 if (it->char_to_display >= ' '
21206 && (!it->multibyte_p || it->char_to_display < 128))
21207 {
21208 /* Either unibyte or ASCII. */
21209 int stretched_p;
21210
21211 it->nglyphs = 1;
21212
21213 pcm = get_per_char_metric (it->f, font, &char2b);
21214
21215 if (it->override_ascent >= 0)
21216 {
21217 it->ascent = it->override_ascent;
21218 it->descent = it->override_descent;
21219 boff = it->override_boff;
21220 }
21221 else
21222 {
21223 it->ascent = FONT_BASE (font) + boff;
21224 it->descent = FONT_DESCENT (font) - boff;
21225 }
21226
21227 if (pcm)
21228 {
21229 it->phys_ascent = pcm->ascent + boff;
21230 it->phys_descent = pcm->descent - boff;
21231 it->pixel_width = pcm->width;
21232 }
21233 else
21234 {
21235 it->glyph_not_available_p = 1;
21236 it->phys_ascent = it->ascent;
21237 it->phys_descent = it->descent;
21238 it->pixel_width = FONT_WIDTH (font);
21239 }
21240
21241 if (it->constrain_row_ascent_descent_p)
21242 {
21243 if (it->descent > it->max_descent)
21244 {
21245 it->ascent += it->descent - it->max_descent;
21246 it->descent = it->max_descent;
21247 }
21248 if (it->ascent > it->max_ascent)
21249 {
21250 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21251 it->ascent = it->max_ascent;
21252 }
21253 it->phys_ascent = min (it->phys_ascent, it->ascent);
21254 it->phys_descent = min (it->phys_descent, it->descent);
21255 extra_line_spacing = 0;
21256 }
21257
21258 /* If this is a space inside a region of text with
21259 `space-width' property, change its width. */
21260 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21261 if (stretched_p)
21262 it->pixel_width *= XFLOATINT (it->space_width);
21263
21264 /* If face has a box, add the box thickness to the character
21265 height. If character has a box line to the left and/or
21266 right, add the box line width to the character's width. */
21267 if (face->box != FACE_NO_BOX)
21268 {
21269 int thick = face->box_line_width;
21270
21271 if (thick > 0)
21272 {
21273 it->ascent += thick;
21274 it->descent += thick;
21275 }
21276 else
21277 thick = -thick;
21278
21279 if (it->start_of_box_run_p)
21280 it->pixel_width += thick;
21281 if (it->end_of_box_run_p)
21282 it->pixel_width += thick;
21283 }
21284
21285 /* If face has an overline, add the height of the overline
21286 (1 pixel) and a 1 pixel margin to the character height. */
21287 if (face->overline_p)
21288 it->ascent += overline_margin;
21289
21290 if (it->constrain_row_ascent_descent_p)
21291 {
21292 if (it->ascent > it->max_ascent)
21293 it->ascent = it->max_ascent;
21294 if (it->descent > it->max_descent)
21295 it->descent = it->max_descent;
21296 }
21297
21298 take_vertical_position_into_account (it);
21299
21300 /* If we have to actually produce glyphs, do it. */
21301 if (it->glyph_row)
21302 {
21303 if (stretched_p)
21304 {
21305 /* Translate a space with a `space-width' property
21306 into a stretch glyph. */
21307 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21308 / FONT_HEIGHT (font));
21309 append_stretch_glyph (it, it->object, it->pixel_width,
21310 it->ascent + it->descent, ascent);
21311 }
21312 else
21313 append_glyph (it);
21314
21315 /* If characters with lbearing or rbearing are displayed
21316 in this line, record that fact in a flag of the
21317 glyph row. This is used to optimize X output code. */
21318 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21319 it->glyph_row->contains_overlapping_glyphs_p = 1;
21320 }
21321 if (! stretched_p && it->pixel_width == 0)
21322 /* We assure that all visible glyphs have at least 1-pixel
21323 width. */
21324 it->pixel_width = 1;
21325 }
21326 else if (it->char_to_display == '\n')
21327 {
21328 /* A newline has no width but we need the height of the line.
21329 But if previous part of the line set a height, don't
21330 increase that height */
21331
21332 Lisp_Object height;
21333 Lisp_Object total_height = Qnil;
21334
21335 it->override_ascent = -1;
21336 it->pixel_width = 0;
21337 it->nglyphs = 0;
21338
21339 height = get_it_property(it, Qline_height);
21340 /* Split (line-height total-height) list */
21341 if (CONSP (height)
21342 && CONSP (XCDR (height))
21343 && NILP (XCDR (XCDR (height))))
21344 {
21345 total_height = XCAR (XCDR (height));
21346 height = XCAR (height);
21347 }
21348 height = calc_line_height_property(it, height, font, boff, 1);
21349
21350 if (it->override_ascent >= 0)
21351 {
21352 it->ascent = it->override_ascent;
21353 it->descent = it->override_descent;
21354 boff = it->override_boff;
21355 }
21356 else
21357 {
21358 it->ascent = FONT_BASE (font) + boff;
21359 it->descent = FONT_DESCENT (font) - boff;
21360 }
21361
21362 if (EQ (height, Qt))
21363 {
21364 if (it->descent > it->max_descent)
21365 {
21366 it->ascent += it->descent - it->max_descent;
21367 it->descent = it->max_descent;
21368 }
21369 if (it->ascent > it->max_ascent)
21370 {
21371 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21372 it->ascent = it->max_ascent;
21373 }
21374 it->phys_ascent = min (it->phys_ascent, it->ascent);
21375 it->phys_descent = min (it->phys_descent, it->descent);
21376 it->constrain_row_ascent_descent_p = 1;
21377 extra_line_spacing = 0;
21378 }
21379 else
21380 {
21381 Lisp_Object spacing;
21382
21383 it->phys_ascent = it->ascent;
21384 it->phys_descent = it->descent;
21385
21386 if ((it->max_ascent > 0 || it->max_descent > 0)
21387 && face->box != FACE_NO_BOX
21388 && face->box_line_width > 0)
21389 {
21390 it->ascent += face->box_line_width;
21391 it->descent += face->box_line_width;
21392 }
21393 if (!NILP (height)
21394 && XINT (height) > it->ascent + it->descent)
21395 it->ascent = XINT (height) - it->descent;
21396
21397 if (!NILP (total_height))
21398 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21399 else
21400 {
21401 spacing = get_it_property(it, Qline_spacing);
21402 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21403 }
21404 if (INTEGERP (spacing))
21405 {
21406 extra_line_spacing = XINT (spacing);
21407 if (!NILP (total_height))
21408 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21409 }
21410 }
21411 }
21412 else if (it->char_to_display == '\t')
21413 {
21414 if (font->space_width > 0)
21415 {
21416 int tab_width = it->tab_width * font->space_width;
21417 int x = it->current_x + it->continuation_lines_width;
21418 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21419
21420 /* If the distance from the current position to the next tab
21421 stop is less than a space character width, use the
21422 tab stop after that. */
21423 if (next_tab_x - x < font->space_width)
21424 next_tab_x += tab_width;
21425
21426 it->pixel_width = next_tab_x - x;
21427 it->nglyphs = 1;
21428 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21429 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21430
21431 if (it->glyph_row)
21432 {
21433 append_stretch_glyph (it, it->object, it->pixel_width,
21434 it->ascent + it->descent, it->ascent);
21435 }
21436 }
21437 else
21438 {
21439 it->pixel_width = 0;
21440 it->nglyphs = 1;
21441 }
21442 }
21443 else
21444 {
21445 /* A multi-byte character. Assume that the display width of the
21446 character is the width of the character multiplied by the
21447 width of the font. */
21448
21449 /* If we found a font, this font should give us the right
21450 metrics. If we didn't find a font, use the frame's
21451 default font and calculate the width of the character by
21452 multiplying the width of font by the width of the
21453 character. */
21454
21455 pcm = get_per_char_metric (it->f, font, &char2b);
21456
21457 if (font_not_found_p || !pcm)
21458 {
21459 int char_width = CHAR_WIDTH (it->char_to_display);
21460
21461 if (char_width == 0)
21462 /* This is a non spacing character. But, as we are
21463 going to display an empty box, the box must occupy
21464 at least one column. */
21465 char_width = 1;
21466 it->glyph_not_available_p = 1;
21467 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21468 it->phys_ascent = FONT_BASE (font) + boff;
21469 it->phys_descent = FONT_DESCENT (font) - boff;
21470 }
21471 else
21472 {
21473 it->pixel_width = pcm->width;
21474 it->phys_ascent = pcm->ascent + boff;
21475 it->phys_descent = pcm->descent - boff;
21476 if (it->glyph_row
21477 && (pcm->lbearing < 0
21478 || pcm->rbearing > pcm->width))
21479 it->glyph_row->contains_overlapping_glyphs_p = 1;
21480 }
21481 it->nglyphs = 1;
21482 it->ascent = FONT_BASE (font) + boff;
21483 it->descent = FONT_DESCENT (font) - boff;
21484 if (face->box != FACE_NO_BOX)
21485 {
21486 int thick = face->box_line_width;
21487
21488 if (thick > 0)
21489 {
21490 it->ascent += thick;
21491 it->descent += thick;
21492 }
21493 else
21494 thick = - thick;
21495
21496 if (it->start_of_box_run_p)
21497 it->pixel_width += thick;
21498 if (it->end_of_box_run_p)
21499 it->pixel_width += thick;
21500 }
21501
21502 /* If face has an overline, add the height of the overline
21503 (1 pixel) and a 1 pixel margin to the character height. */
21504 if (face->overline_p)
21505 it->ascent += overline_margin;
21506
21507 take_vertical_position_into_account (it);
21508
21509 if (it->ascent < 0)
21510 it->ascent = 0;
21511 if (it->descent < 0)
21512 it->descent = 0;
21513
21514 if (it->glyph_row)
21515 append_glyph (it);
21516 if (it->pixel_width == 0)
21517 /* We assure that all visible glyphs have at least 1-pixel
21518 width. */
21519 it->pixel_width = 1;
21520 }
21521 it->multibyte_p = saved_multibyte_p;
21522 }
21523 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21524 {
21525 /* A static compositoin.
21526
21527 Note: A composition is represented as one glyph in the
21528 glyph matrix. There are no padding glyphs.
21529
21530 Important is that pixel_width, ascent, and descent are the
21531 values of what is drawn by draw_glyphs (i.e. the values of
21532 the overall glyphs composed). */
21533 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21534 int boff; /* baseline offset */
21535 struct composition *cmp = composition_table[it->cmp_it.id];
21536 int glyph_len = cmp->glyph_len;
21537 struct font *font = face->font;
21538
21539 it->nglyphs = 1;
21540
21541 /* If we have not yet calculated pixel size data of glyphs of
21542 the composition for the current face font, calculate them
21543 now. Theoretically, we have to check all fonts for the
21544 glyphs, but that requires much time and memory space. So,
21545 here we check only the font of the first glyph. This leads
21546 to incorrect display, but it's very rare, and C-l (recenter)
21547 can correct the display anyway. */
21548 if (! cmp->font || cmp->font != font)
21549 {
21550 /* Ascent and descent of the font of the first character
21551 of this composition (adjusted by baseline offset).
21552 Ascent and descent of overall glyphs should not be less
21553 than them respectively. */
21554 int font_ascent, font_descent, font_height;
21555 /* Bounding box of the overall glyphs. */
21556 int leftmost, rightmost, lowest, highest;
21557 int lbearing, rbearing;
21558 int i, width, ascent, descent;
21559 int left_padded = 0, right_padded = 0;
21560 int c;
21561 XChar2b char2b;
21562 struct font_metrics *pcm;
21563 int font_not_found_p;
21564 int pos;
21565
21566 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21567 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21568 break;
21569 if (glyph_len < cmp->glyph_len)
21570 right_padded = 1;
21571 for (i = 0; i < glyph_len; i++)
21572 {
21573 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21574 break;
21575 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21576 }
21577 if (i > 0)
21578 left_padded = 1;
21579
21580 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21581 : IT_CHARPOS (*it));
21582 /* When no suitable font found, use the default font. */
21583 font_not_found_p = font == NULL;
21584 if (font_not_found_p)
21585 {
21586 face = face->ascii_face;
21587 font = face->font;
21588 }
21589 boff = font->baseline_offset;
21590 if (font->vertical_centering)
21591 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21592 font_ascent = FONT_BASE (font) + boff;
21593 font_descent = FONT_DESCENT (font) - boff;
21594 font_height = FONT_HEIGHT (font);
21595
21596 cmp->font = (void *) font;
21597
21598 pcm = NULL;
21599 if (! font_not_found_p)
21600 {
21601 get_char_face_and_encoding (it->f, c, it->face_id,
21602 &char2b, it->multibyte_p, 0);
21603 pcm = get_per_char_metric (it->f, font, &char2b);
21604 }
21605
21606 /* Initialize the bounding box. */
21607 if (pcm)
21608 {
21609 width = pcm->width;
21610 ascent = pcm->ascent;
21611 descent = pcm->descent;
21612 lbearing = pcm->lbearing;
21613 rbearing = pcm->rbearing;
21614 }
21615 else
21616 {
21617 width = FONT_WIDTH (font);
21618 ascent = FONT_BASE (font);
21619 descent = FONT_DESCENT (font);
21620 lbearing = 0;
21621 rbearing = width;
21622 }
21623
21624 rightmost = width;
21625 leftmost = 0;
21626 lowest = - descent + boff;
21627 highest = ascent + boff;
21628
21629 if (! font_not_found_p
21630 && font->default_ascent
21631 && CHAR_TABLE_P (Vuse_default_ascent)
21632 && !NILP (Faref (Vuse_default_ascent,
21633 make_number (it->char_to_display))))
21634 highest = font->default_ascent + boff;
21635
21636 /* Draw the first glyph at the normal position. It may be
21637 shifted to right later if some other glyphs are drawn
21638 at the left. */
21639 cmp->offsets[i * 2] = 0;
21640 cmp->offsets[i * 2 + 1] = boff;
21641 cmp->lbearing = lbearing;
21642 cmp->rbearing = rbearing;
21643
21644 /* Set cmp->offsets for the remaining glyphs. */
21645 for (i++; i < glyph_len; i++)
21646 {
21647 int left, right, btm, top;
21648 int ch = COMPOSITION_GLYPH (cmp, i);
21649 int face_id;
21650 struct face *this_face;
21651 int this_boff;
21652
21653 if (ch == '\t')
21654 ch = ' ';
21655 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21656 this_face = FACE_FROM_ID (it->f, face_id);
21657 font = this_face->font;
21658
21659 if (font == NULL)
21660 pcm = NULL;
21661 else
21662 {
21663 this_boff = font->baseline_offset;
21664 if (font->vertical_centering)
21665 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21666 get_char_face_and_encoding (it->f, ch, face_id,
21667 &char2b, it->multibyte_p, 0);
21668 pcm = get_per_char_metric (it->f, font, &char2b);
21669 }
21670 if (! pcm)
21671 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21672 else
21673 {
21674 width = pcm->width;
21675 ascent = pcm->ascent;
21676 descent = pcm->descent;
21677 lbearing = pcm->lbearing;
21678 rbearing = pcm->rbearing;
21679 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21680 {
21681 /* Relative composition with or without
21682 alternate chars. */
21683 left = (leftmost + rightmost - width) / 2;
21684 btm = - descent + boff;
21685 if (font->relative_compose
21686 && (! CHAR_TABLE_P (Vignore_relative_composition)
21687 || NILP (Faref (Vignore_relative_composition,
21688 make_number (ch)))))
21689 {
21690
21691 if (- descent >= font->relative_compose)
21692 /* One extra pixel between two glyphs. */
21693 btm = highest + 1;
21694 else if (ascent <= 0)
21695 /* One extra pixel between two glyphs. */
21696 btm = lowest - 1 - ascent - descent;
21697 }
21698 }
21699 else
21700 {
21701 /* A composition rule is specified by an integer
21702 value that encodes global and new reference
21703 points (GREF and NREF). GREF and NREF are
21704 specified by numbers as below:
21705
21706 0---1---2 -- ascent
21707 | |
21708 | |
21709 | |
21710 9--10--11 -- center
21711 | |
21712 ---3---4---5--- baseline
21713 | |
21714 6---7---8 -- descent
21715 */
21716 int rule = COMPOSITION_RULE (cmp, i);
21717 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21718
21719 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21720 grefx = gref % 3, nrefx = nref % 3;
21721 grefy = gref / 3, nrefy = nref / 3;
21722 if (xoff)
21723 xoff = font_height * (xoff - 128) / 256;
21724 if (yoff)
21725 yoff = font_height * (yoff - 128) / 256;
21726
21727 left = (leftmost
21728 + grefx * (rightmost - leftmost) / 2
21729 - nrefx * width / 2
21730 + xoff);
21731
21732 btm = ((grefy == 0 ? highest
21733 : grefy == 1 ? 0
21734 : grefy == 2 ? lowest
21735 : (highest + lowest) / 2)
21736 - (nrefy == 0 ? ascent + descent
21737 : nrefy == 1 ? descent - boff
21738 : nrefy == 2 ? 0
21739 : (ascent + descent) / 2)
21740 + yoff);
21741 }
21742
21743 cmp->offsets[i * 2] = left;
21744 cmp->offsets[i * 2 + 1] = btm + descent;
21745
21746 /* Update the bounding box of the overall glyphs. */
21747 if (width > 0)
21748 {
21749 right = left + width;
21750 if (left < leftmost)
21751 leftmost = left;
21752 if (right > rightmost)
21753 rightmost = right;
21754 }
21755 top = btm + descent + ascent;
21756 if (top > highest)
21757 highest = top;
21758 if (btm < lowest)
21759 lowest = btm;
21760
21761 if (cmp->lbearing > left + lbearing)
21762 cmp->lbearing = left + lbearing;
21763 if (cmp->rbearing < left + rbearing)
21764 cmp->rbearing = left + rbearing;
21765 }
21766 }
21767
21768 /* If there are glyphs whose x-offsets are negative,
21769 shift all glyphs to the right and make all x-offsets
21770 non-negative. */
21771 if (leftmost < 0)
21772 {
21773 for (i = 0; i < cmp->glyph_len; i++)
21774 cmp->offsets[i * 2] -= leftmost;
21775 rightmost -= leftmost;
21776 cmp->lbearing -= leftmost;
21777 cmp->rbearing -= leftmost;
21778 }
21779
21780 if (left_padded && cmp->lbearing < 0)
21781 {
21782 for (i = 0; i < cmp->glyph_len; i++)
21783 cmp->offsets[i * 2] -= cmp->lbearing;
21784 rightmost -= cmp->lbearing;
21785 cmp->rbearing -= cmp->lbearing;
21786 cmp->lbearing = 0;
21787 }
21788 if (right_padded && rightmost < cmp->rbearing)
21789 {
21790 rightmost = cmp->rbearing;
21791 }
21792
21793 cmp->pixel_width = rightmost;
21794 cmp->ascent = highest;
21795 cmp->descent = - lowest;
21796 if (cmp->ascent < font_ascent)
21797 cmp->ascent = font_ascent;
21798 if (cmp->descent < font_descent)
21799 cmp->descent = font_descent;
21800 }
21801
21802 if (it->glyph_row
21803 && (cmp->lbearing < 0
21804 || cmp->rbearing > cmp->pixel_width))
21805 it->glyph_row->contains_overlapping_glyphs_p = 1;
21806
21807 it->pixel_width = cmp->pixel_width;
21808 it->ascent = it->phys_ascent = cmp->ascent;
21809 it->descent = it->phys_descent = cmp->descent;
21810 if (face->box != FACE_NO_BOX)
21811 {
21812 int thick = face->box_line_width;
21813
21814 if (thick > 0)
21815 {
21816 it->ascent += thick;
21817 it->descent += thick;
21818 }
21819 else
21820 thick = - thick;
21821
21822 if (it->start_of_box_run_p)
21823 it->pixel_width += thick;
21824 if (it->end_of_box_run_p)
21825 it->pixel_width += thick;
21826 }
21827
21828 /* If face has an overline, add the height of the overline
21829 (1 pixel) and a 1 pixel margin to the character height. */
21830 if (face->overline_p)
21831 it->ascent += overline_margin;
21832
21833 take_vertical_position_into_account (it);
21834 if (it->ascent < 0)
21835 it->ascent = 0;
21836 if (it->descent < 0)
21837 it->descent = 0;
21838
21839 if (it->glyph_row)
21840 append_composite_glyph (it);
21841 }
21842 else if (it->what == IT_COMPOSITION)
21843 {
21844 /* A dynamic (automatic) composition. */
21845 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21846 Lisp_Object gstring;
21847 struct font_metrics metrics;
21848
21849 gstring = composition_gstring_from_id (it->cmp_it.id);
21850 it->pixel_width
21851 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21852 &metrics);
21853 if (it->glyph_row
21854 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21855 it->glyph_row->contains_overlapping_glyphs_p = 1;
21856 it->ascent = it->phys_ascent = metrics.ascent;
21857 it->descent = it->phys_descent = metrics.descent;
21858 if (face->box != FACE_NO_BOX)
21859 {
21860 int thick = face->box_line_width;
21861
21862 if (thick > 0)
21863 {
21864 it->ascent += thick;
21865 it->descent += thick;
21866 }
21867 else
21868 thick = - thick;
21869
21870 if (it->start_of_box_run_p)
21871 it->pixel_width += thick;
21872 if (it->end_of_box_run_p)
21873 it->pixel_width += thick;
21874 }
21875 /* If face has an overline, add the height of the overline
21876 (1 pixel) and a 1 pixel margin to the character height. */
21877 if (face->overline_p)
21878 it->ascent += overline_margin;
21879 take_vertical_position_into_account (it);
21880 if (it->ascent < 0)
21881 it->ascent = 0;
21882 if (it->descent < 0)
21883 it->descent = 0;
21884
21885 if (it->glyph_row)
21886 append_composite_glyph (it);
21887 }
21888 else if (it->what == IT_IMAGE)
21889 produce_image_glyph (it);
21890 else if (it->what == IT_STRETCH)
21891 produce_stretch_glyph (it);
21892
21893 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21894 because this isn't true for images with `:ascent 100'. */
21895 xassert (it->ascent >= 0 && it->descent >= 0);
21896 if (it->area == TEXT_AREA)
21897 it->current_x += it->pixel_width;
21898
21899 if (extra_line_spacing > 0)
21900 {
21901 it->descent += extra_line_spacing;
21902 if (extra_line_spacing > it->max_extra_line_spacing)
21903 it->max_extra_line_spacing = extra_line_spacing;
21904 }
21905
21906 it->max_ascent = max (it->max_ascent, it->ascent);
21907 it->max_descent = max (it->max_descent, it->descent);
21908 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21909 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21910 }
21911
21912 /* EXPORT for RIF:
21913 Output LEN glyphs starting at START at the nominal cursor position.
21914 Advance the nominal cursor over the text. The global variable
21915 updated_window contains the window being updated, updated_row is
21916 the glyph row being updated, and updated_area is the area of that
21917 row being updated. */
21918
21919 void
21920 x_write_glyphs (start, len)
21921 struct glyph *start;
21922 int len;
21923 {
21924 int x, hpos;
21925
21926 xassert (updated_window && updated_row);
21927 BLOCK_INPUT;
21928
21929 /* Write glyphs. */
21930
21931 hpos = start - updated_row->glyphs[updated_area];
21932 x = draw_glyphs (updated_window, output_cursor.x,
21933 updated_row, updated_area,
21934 hpos, hpos + len,
21935 DRAW_NORMAL_TEXT, 0);
21936
21937 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21938 if (updated_area == TEXT_AREA
21939 && updated_window->phys_cursor_on_p
21940 && updated_window->phys_cursor.vpos == output_cursor.vpos
21941 && updated_window->phys_cursor.hpos >= hpos
21942 && updated_window->phys_cursor.hpos < hpos + len)
21943 updated_window->phys_cursor_on_p = 0;
21944
21945 UNBLOCK_INPUT;
21946
21947 /* Advance the output cursor. */
21948 output_cursor.hpos += len;
21949 output_cursor.x = x;
21950 }
21951
21952
21953 /* EXPORT for RIF:
21954 Insert LEN glyphs from START at the nominal cursor position. */
21955
21956 void
21957 x_insert_glyphs (start, len)
21958 struct glyph *start;
21959 int len;
21960 {
21961 struct frame *f;
21962 struct window *w;
21963 int line_height, shift_by_width, shifted_region_width;
21964 struct glyph_row *row;
21965 struct glyph *glyph;
21966 int frame_x, frame_y;
21967 EMACS_INT hpos;
21968
21969 xassert (updated_window && updated_row);
21970 BLOCK_INPUT;
21971 w = updated_window;
21972 f = XFRAME (WINDOW_FRAME (w));
21973
21974 /* Get the height of the line we are in. */
21975 row = updated_row;
21976 line_height = row->height;
21977
21978 /* Get the width of the glyphs to insert. */
21979 shift_by_width = 0;
21980 for (glyph = start; glyph < start + len; ++glyph)
21981 shift_by_width += glyph->pixel_width;
21982
21983 /* Get the width of the region to shift right. */
21984 shifted_region_width = (window_box_width (w, updated_area)
21985 - output_cursor.x
21986 - shift_by_width);
21987
21988 /* Shift right. */
21989 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21990 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21991
21992 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21993 line_height, shift_by_width);
21994
21995 /* Write the glyphs. */
21996 hpos = start - row->glyphs[updated_area];
21997 draw_glyphs (w, output_cursor.x, row, updated_area,
21998 hpos, hpos + len,
21999 DRAW_NORMAL_TEXT, 0);
22000
22001 /* Advance the output cursor. */
22002 output_cursor.hpos += len;
22003 output_cursor.x += shift_by_width;
22004 UNBLOCK_INPUT;
22005 }
22006
22007
22008 /* EXPORT for RIF:
22009 Erase the current text line from the nominal cursor position
22010 (inclusive) to pixel column TO_X (exclusive). The idea is that
22011 everything from TO_X onward is already erased.
22012
22013 TO_X is a pixel position relative to updated_area of
22014 updated_window. TO_X == -1 means clear to the end of this area. */
22015
22016 void
22017 x_clear_end_of_line (to_x)
22018 int to_x;
22019 {
22020 struct frame *f;
22021 struct window *w = updated_window;
22022 int max_x, min_y, max_y;
22023 int from_x, from_y, to_y;
22024
22025 xassert (updated_window && updated_row);
22026 f = XFRAME (w->frame);
22027
22028 if (updated_row->full_width_p)
22029 max_x = WINDOW_TOTAL_WIDTH (w);
22030 else
22031 max_x = window_box_width (w, updated_area);
22032 max_y = window_text_bottom_y (w);
22033
22034 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22035 of window. For TO_X > 0, truncate to end of drawing area. */
22036 if (to_x == 0)
22037 return;
22038 else if (to_x < 0)
22039 to_x = max_x;
22040 else
22041 to_x = min (to_x, max_x);
22042
22043 to_y = min (max_y, output_cursor.y + updated_row->height);
22044
22045 /* Notice if the cursor will be cleared by this operation. */
22046 if (!updated_row->full_width_p)
22047 notice_overwritten_cursor (w, updated_area,
22048 output_cursor.x, -1,
22049 updated_row->y,
22050 MATRIX_ROW_BOTTOM_Y (updated_row));
22051
22052 from_x = output_cursor.x;
22053
22054 /* Translate to frame coordinates. */
22055 if (updated_row->full_width_p)
22056 {
22057 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22058 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22059 }
22060 else
22061 {
22062 int area_left = window_box_left (w, updated_area);
22063 from_x += area_left;
22064 to_x += area_left;
22065 }
22066
22067 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22068 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22069 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22070
22071 /* Prevent inadvertently clearing to end of the X window. */
22072 if (to_x > from_x && to_y > from_y)
22073 {
22074 BLOCK_INPUT;
22075 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22076 to_x - from_x, to_y - from_y);
22077 UNBLOCK_INPUT;
22078 }
22079 }
22080
22081 #endif /* HAVE_WINDOW_SYSTEM */
22082
22083
22084 \f
22085 /***********************************************************************
22086 Cursor types
22087 ***********************************************************************/
22088
22089 /* Value is the internal representation of the specified cursor type
22090 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22091 of the bar cursor. */
22092
22093 static enum text_cursor_kinds
22094 get_specified_cursor_type (arg, width)
22095 Lisp_Object arg;
22096 int *width;
22097 {
22098 enum text_cursor_kinds type;
22099
22100 if (NILP (arg))
22101 return NO_CURSOR;
22102
22103 if (EQ (arg, Qbox))
22104 return FILLED_BOX_CURSOR;
22105
22106 if (EQ (arg, Qhollow))
22107 return HOLLOW_BOX_CURSOR;
22108
22109 if (EQ (arg, Qbar))
22110 {
22111 *width = 2;
22112 return BAR_CURSOR;
22113 }
22114
22115 if (CONSP (arg)
22116 && EQ (XCAR (arg), Qbar)
22117 && INTEGERP (XCDR (arg))
22118 && XINT (XCDR (arg)) >= 0)
22119 {
22120 *width = XINT (XCDR (arg));
22121 return BAR_CURSOR;
22122 }
22123
22124 if (EQ (arg, Qhbar))
22125 {
22126 *width = 2;
22127 return HBAR_CURSOR;
22128 }
22129
22130 if (CONSP (arg)
22131 && EQ (XCAR (arg), Qhbar)
22132 && INTEGERP (XCDR (arg))
22133 && XINT (XCDR (arg)) >= 0)
22134 {
22135 *width = XINT (XCDR (arg));
22136 return HBAR_CURSOR;
22137 }
22138
22139 /* Treat anything unknown as "hollow box cursor".
22140 It was bad to signal an error; people have trouble fixing
22141 .Xdefaults with Emacs, when it has something bad in it. */
22142 type = HOLLOW_BOX_CURSOR;
22143
22144 return type;
22145 }
22146
22147 /* Set the default cursor types for specified frame. */
22148 void
22149 set_frame_cursor_types (f, arg)
22150 struct frame *f;
22151 Lisp_Object arg;
22152 {
22153 int width;
22154 Lisp_Object tem;
22155
22156 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22157 FRAME_CURSOR_WIDTH (f) = width;
22158
22159 /* By default, set up the blink-off state depending on the on-state. */
22160
22161 tem = Fassoc (arg, Vblink_cursor_alist);
22162 if (!NILP (tem))
22163 {
22164 FRAME_BLINK_OFF_CURSOR (f)
22165 = get_specified_cursor_type (XCDR (tem), &width);
22166 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22167 }
22168 else
22169 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22170 }
22171
22172
22173 /* Return the cursor we want to be displayed in window W. Return
22174 width of bar/hbar cursor through WIDTH arg. Return with
22175 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22176 (i.e. if the `system caret' should track this cursor).
22177
22178 In a mini-buffer window, we want the cursor only to appear if we
22179 are reading input from this window. For the selected window, we
22180 want the cursor type given by the frame parameter or buffer local
22181 setting of cursor-type. If explicitly marked off, draw no cursor.
22182 In all other cases, we want a hollow box cursor. */
22183
22184 static enum text_cursor_kinds
22185 get_window_cursor_type (w, glyph, width, active_cursor)
22186 struct window *w;
22187 struct glyph *glyph;
22188 int *width;
22189 int *active_cursor;
22190 {
22191 struct frame *f = XFRAME (w->frame);
22192 struct buffer *b = XBUFFER (w->buffer);
22193 int cursor_type = DEFAULT_CURSOR;
22194 Lisp_Object alt_cursor;
22195 int non_selected = 0;
22196
22197 *active_cursor = 1;
22198
22199 /* Echo area */
22200 if (cursor_in_echo_area
22201 && FRAME_HAS_MINIBUF_P (f)
22202 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22203 {
22204 if (w == XWINDOW (echo_area_window))
22205 {
22206 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22207 {
22208 *width = FRAME_CURSOR_WIDTH (f);
22209 return FRAME_DESIRED_CURSOR (f);
22210 }
22211 else
22212 return get_specified_cursor_type (b->cursor_type, width);
22213 }
22214
22215 *active_cursor = 0;
22216 non_selected = 1;
22217 }
22218
22219 /* Detect a nonselected window or nonselected frame. */
22220 else if (w != XWINDOW (f->selected_window)
22221 #ifdef HAVE_WINDOW_SYSTEM
22222 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22223 #endif
22224 )
22225 {
22226 *active_cursor = 0;
22227
22228 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22229 return NO_CURSOR;
22230
22231 non_selected = 1;
22232 }
22233
22234 /* Never display a cursor in a window in which cursor-type is nil. */
22235 if (NILP (b->cursor_type))
22236 return NO_CURSOR;
22237
22238 /* Get the normal cursor type for this window. */
22239 if (EQ (b->cursor_type, Qt))
22240 {
22241 cursor_type = FRAME_DESIRED_CURSOR (f);
22242 *width = FRAME_CURSOR_WIDTH (f);
22243 }
22244 else
22245 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22246
22247 /* Use cursor-in-non-selected-windows instead
22248 for non-selected window or frame. */
22249 if (non_selected)
22250 {
22251 alt_cursor = b->cursor_in_non_selected_windows;
22252 if (!EQ (Qt, alt_cursor))
22253 return get_specified_cursor_type (alt_cursor, width);
22254 /* t means modify the normal cursor type. */
22255 if (cursor_type == FILLED_BOX_CURSOR)
22256 cursor_type = HOLLOW_BOX_CURSOR;
22257 else if (cursor_type == BAR_CURSOR && *width > 1)
22258 --*width;
22259 return cursor_type;
22260 }
22261
22262 /* Use normal cursor if not blinked off. */
22263 if (!w->cursor_off_p)
22264 {
22265 #ifdef HAVE_WINDOW_SYSTEM
22266 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22267 {
22268 if (cursor_type == FILLED_BOX_CURSOR)
22269 {
22270 /* Using a block cursor on large images can be very annoying.
22271 So use a hollow cursor for "large" images.
22272 If image is not transparent (no mask), also use hollow cursor. */
22273 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22274 if (img != NULL && IMAGEP (img->spec))
22275 {
22276 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22277 where N = size of default frame font size.
22278 This should cover most of the "tiny" icons people may use. */
22279 if (!img->mask
22280 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22281 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22282 cursor_type = HOLLOW_BOX_CURSOR;
22283 }
22284 }
22285 else if (cursor_type != NO_CURSOR)
22286 {
22287 /* Display current only supports BOX and HOLLOW cursors for images.
22288 So for now, unconditionally use a HOLLOW cursor when cursor is
22289 not a solid box cursor. */
22290 cursor_type = HOLLOW_BOX_CURSOR;
22291 }
22292 }
22293 #endif
22294 return cursor_type;
22295 }
22296
22297 /* Cursor is blinked off, so determine how to "toggle" it. */
22298
22299 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22300 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22301 return get_specified_cursor_type (XCDR (alt_cursor), width);
22302
22303 /* Then see if frame has specified a specific blink off cursor type. */
22304 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22305 {
22306 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22307 return FRAME_BLINK_OFF_CURSOR (f);
22308 }
22309
22310 #if 0
22311 /* Some people liked having a permanently visible blinking cursor,
22312 while others had very strong opinions against it. So it was
22313 decided to remove it. KFS 2003-09-03 */
22314
22315 /* Finally perform built-in cursor blinking:
22316 filled box <-> hollow box
22317 wide [h]bar <-> narrow [h]bar
22318 narrow [h]bar <-> no cursor
22319 other type <-> no cursor */
22320
22321 if (cursor_type == FILLED_BOX_CURSOR)
22322 return HOLLOW_BOX_CURSOR;
22323
22324 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22325 {
22326 *width = 1;
22327 return cursor_type;
22328 }
22329 #endif
22330
22331 return NO_CURSOR;
22332 }
22333
22334
22335 #ifdef HAVE_WINDOW_SYSTEM
22336
22337 /* Notice when the text cursor of window W has been completely
22338 overwritten by a drawing operation that outputs glyphs in AREA
22339 starting at X0 and ending at X1 in the line starting at Y0 and
22340 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22341 the rest of the line after X0 has been written. Y coordinates
22342 are window-relative. */
22343
22344 static void
22345 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22346 struct window *w;
22347 enum glyph_row_area area;
22348 int x0, y0, x1, y1;
22349 {
22350 int cx0, cx1, cy0, cy1;
22351 struct glyph_row *row;
22352
22353 if (!w->phys_cursor_on_p)
22354 return;
22355 if (area != TEXT_AREA)
22356 return;
22357
22358 if (w->phys_cursor.vpos < 0
22359 || w->phys_cursor.vpos >= w->current_matrix->nrows
22360 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22361 !(row->enabled_p && row->displays_text_p)))
22362 return;
22363
22364 if (row->cursor_in_fringe_p)
22365 {
22366 row->cursor_in_fringe_p = 0;
22367 draw_fringe_bitmap (w, row, 0);
22368 w->phys_cursor_on_p = 0;
22369 return;
22370 }
22371
22372 cx0 = w->phys_cursor.x;
22373 cx1 = cx0 + w->phys_cursor_width;
22374 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22375 return;
22376
22377 /* The cursor image will be completely removed from the
22378 screen if the output area intersects the cursor area in
22379 y-direction. When we draw in [y0 y1[, and some part of
22380 the cursor is at y < y0, that part must have been drawn
22381 before. When scrolling, the cursor is erased before
22382 actually scrolling, so we don't come here. When not
22383 scrolling, the rows above the old cursor row must have
22384 changed, and in this case these rows must have written
22385 over the cursor image.
22386
22387 Likewise if part of the cursor is below y1, with the
22388 exception of the cursor being in the first blank row at
22389 the buffer and window end because update_text_area
22390 doesn't draw that row. (Except when it does, but
22391 that's handled in update_text_area.) */
22392
22393 cy0 = w->phys_cursor.y;
22394 cy1 = cy0 + w->phys_cursor_height;
22395 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22396 return;
22397
22398 w->phys_cursor_on_p = 0;
22399 }
22400
22401 #endif /* HAVE_WINDOW_SYSTEM */
22402
22403 \f
22404 /************************************************************************
22405 Mouse Face
22406 ************************************************************************/
22407
22408 #ifdef HAVE_WINDOW_SYSTEM
22409
22410 /* EXPORT for RIF:
22411 Fix the display of area AREA of overlapping row ROW in window W
22412 with respect to the overlapping part OVERLAPS. */
22413
22414 void
22415 x_fix_overlapping_area (w, row, area, overlaps)
22416 struct window *w;
22417 struct glyph_row *row;
22418 enum glyph_row_area area;
22419 int overlaps;
22420 {
22421 int i, x;
22422
22423 BLOCK_INPUT;
22424
22425 x = 0;
22426 for (i = 0; i < row->used[area];)
22427 {
22428 if (row->glyphs[area][i].overlaps_vertically_p)
22429 {
22430 int start = i, start_x = x;
22431
22432 do
22433 {
22434 x += row->glyphs[area][i].pixel_width;
22435 ++i;
22436 }
22437 while (i < row->used[area]
22438 && row->glyphs[area][i].overlaps_vertically_p);
22439
22440 draw_glyphs (w, start_x, row, area,
22441 start, i,
22442 DRAW_NORMAL_TEXT, overlaps);
22443 }
22444 else
22445 {
22446 x += row->glyphs[area][i].pixel_width;
22447 ++i;
22448 }
22449 }
22450
22451 UNBLOCK_INPUT;
22452 }
22453
22454
22455 /* EXPORT:
22456 Draw the cursor glyph of window W in glyph row ROW. See the
22457 comment of draw_glyphs for the meaning of HL. */
22458
22459 void
22460 draw_phys_cursor_glyph (w, row, hl)
22461 struct window *w;
22462 struct glyph_row *row;
22463 enum draw_glyphs_face hl;
22464 {
22465 /* If cursor hpos is out of bounds, don't draw garbage. This can
22466 happen in mini-buffer windows when switching between echo area
22467 glyphs and mini-buffer. */
22468 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22469 {
22470 int on_p = w->phys_cursor_on_p;
22471 int x1;
22472 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22473 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22474 hl, 0);
22475 w->phys_cursor_on_p = on_p;
22476
22477 if (hl == DRAW_CURSOR)
22478 w->phys_cursor_width = x1 - w->phys_cursor.x;
22479 /* When we erase the cursor, and ROW is overlapped by other
22480 rows, make sure that these overlapping parts of other rows
22481 are redrawn. */
22482 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22483 {
22484 w->phys_cursor_width = x1 - w->phys_cursor.x;
22485
22486 if (row > w->current_matrix->rows
22487 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22488 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22489 OVERLAPS_ERASED_CURSOR);
22490
22491 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22492 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22493 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22494 OVERLAPS_ERASED_CURSOR);
22495 }
22496 }
22497 }
22498
22499
22500 /* EXPORT:
22501 Erase the image of a cursor of window W from the screen. */
22502
22503 void
22504 erase_phys_cursor (w)
22505 struct window *w;
22506 {
22507 struct frame *f = XFRAME (w->frame);
22508 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22509 int hpos = w->phys_cursor.hpos;
22510 int vpos = w->phys_cursor.vpos;
22511 int mouse_face_here_p = 0;
22512 struct glyph_matrix *active_glyphs = w->current_matrix;
22513 struct glyph_row *cursor_row;
22514 struct glyph *cursor_glyph;
22515 enum draw_glyphs_face hl;
22516
22517 /* No cursor displayed or row invalidated => nothing to do on the
22518 screen. */
22519 if (w->phys_cursor_type == NO_CURSOR)
22520 goto mark_cursor_off;
22521
22522 /* VPOS >= active_glyphs->nrows means that window has been resized.
22523 Don't bother to erase the cursor. */
22524 if (vpos >= active_glyphs->nrows)
22525 goto mark_cursor_off;
22526
22527 /* If row containing cursor is marked invalid, there is nothing we
22528 can do. */
22529 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22530 if (!cursor_row->enabled_p)
22531 goto mark_cursor_off;
22532
22533 /* If line spacing is > 0, old cursor may only be partially visible in
22534 window after split-window. So adjust visible height. */
22535 cursor_row->visible_height = min (cursor_row->visible_height,
22536 window_text_bottom_y (w) - cursor_row->y);
22537
22538 /* If row is completely invisible, don't attempt to delete a cursor which
22539 isn't there. This can happen if cursor is at top of a window, and
22540 we switch to a buffer with a header line in that window. */
22541 if (cursor_row->visible_height <= 0)
22542 goto mark_cursor_off;
22543
22544 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22545 if (cursor_row->cursor_in_fringe_p)
22546 {
22547 cursor_row->cursor_in_fringe_p = 0;
22548 draw_fringe_bitmap (w, cursor_row, 0);
22549 goto mark_cursor_off;
22550 }
22551
22552 /* This can happen when the new row is shorter than the old one.
22553 In this case, either draw_glyphs or clear_end_of_line
22554 should have cleared the cursor. Note that we wouldn't be
22555 able to erase the cursor in this case because we don't have a
22556 cursor glyph at hand. */
22557 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22558 goto mark_cursor_off;
22559
22560 /* If the cursor is in the mouse face area, redisplay that when
22561 we clear the cursor. */
22562 if (! NILP (dpyinfo->mouse_face_window)
22563 && w == XWINDOW (dpyinfo->mouse_face_window)
22564 && (vpos > dpyinfo->mouse_face_beg_row
22565 || (vpos == dpyinfo->mouse_face_beg_row
22566 && hpos >= dpyinfo->mouse_face_beg_col))
22567 && (vpos < dpyinfo->mouse_face_end_row
22568 || (vpos == dpyinfo->mouse_face_end_row
22569 && hpos < dpyinfo->mouse_face_end_col))
22570 /* Don't redraw the cursor's spot in mouse face if it is at the
22571 end of a line (on a newline). The cursor appears there, but
22572 mouse highlighting does not. */
22573 && cursor_row->used[TEXT_AREA] > hpos)
22574 mouse_face_here_p = 1;
22575
22576 /* Maybe clear the display under the cursor. */
22577 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22578 {
22579 int x, y, left_x;
22580 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22581 int width;
22582
22583 cursor_glyph = get_phys_cursor_glyph (w);
22584 if (cursor_glyph == NULL)
22585 goto mark_cursor_off;
22586
22587 width = cursor_glyph->pixel_width;
22588 left_x = window_box_left_offset (w, TEXT_AREA);
22589 x = w->phys_cursor.x;
22590 if (x < left_x)
22591 width -= left_x - x;
22592 width = min (width, window_box_width (w, TEXT_AREA) - x);
22593 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22594 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22595
22596 if (width > 0)
22597 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22598 }
22599
22600 /* Erase the cursor by redrawing the character underneath it. */
22601 if (mouse_face_here_p)
22602 hl = DRAW_MOUSE_FACE;
22603 else
22604 hl = DRAW_NORMAL_TEXT;
22605 draw_phys_cursor_glyph (w, cursor_row, hl);
22606
22607 mark_cursor_off:
22608 w->phys_cursor_on_p = 0;
22609 w->phys_cursor_type = NO_CURSOR;
22610 }
22611
22612
22613 /* EXPORT:
22614 Display or clear cursor of window W. If ON is zero, clear the
22615 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22616 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22617
22618 void
22619 display_and_set_cursor (w, on, hpos, vpos, x, y)
22620 struct window *w;
22621 int on, hpos, vpos, x, y;
22622 {
22623 struct frame *f = XFRAME (w->frame);
22624 int new_cursor_type;
22625 int new_cursor_width;
22626 int active_cursor;
22627 struct glyph_row *glyph_row;
22628 struct glyph *glyph;
22629
22630 /* This is pointless on invisible frames, and dangerous on garbaged
22631 windows and frames; in the latter case, the frame or window may
22632 be in the midst of changing its size, and x and y may be off the
22633 window. */
22634 if (! FRAME_VISIBLE_P (f)
22635 || FRAME_GARBAGED_P (f)
22636 || vpos >= w->current_matrix->nrows
22637 || hpos >= w->current_matrix->matrix_w)
22638 return;
22639
22640 /* If cursor is off and we want it off, return quickly. */
22641 if (!on && !w->phys_cursor_on_p)
22642 return;
22643
22644 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22645 /* If cursor row is not enabled, we don't really know where to
22646 display the cursor. */
22647 if (!glyph_row->enabled_p)
22648 {
22649 w->phys_cursor_on_p = 0;
22650 return;
22651 }
22652
22653 glyph = NULL;
22654 if (!glyph_row->exact_window_width_line_p
22655 || hpos < glyph_row->used[TEXT_AREA])
22656 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22657
22658 xassert (interrupt_input_blocked);
22659
22660 /* Set new_cursor_type to the cursor we want to be displayed. */
22661 new_cursor_type = get_window_cursor_type (w, glyph,
22662 &new_cursor_width, &active_cursor);
22663
22664 /* If cursor is currently being shown and we don't want it to be or
22665 it is in the wrong place, or the cursor type is not what we want,
22666 erase it. */
22667 if (w->phys_cursor_on_p
22668 && (!on
22669 || w->phys_cursor.x != x
22670 || w->phys_cursor.y != y
22671 || new_cursor_type != w->phys_cursor_type
22672 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22673 && new_cursor_width != w->phys_cursor_width)))
22674 erase_phys_cursor (w);
22675
22676 /* Don't check phys_cursor_on_p here because that flag is only set
22677 to zero in some cases where we know that the cursor has been
22678 completely erased, to avoid the extra work of erasing the cursor
22679 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22680 still not be visible, or it has only been partly erased. */
22681 if (on)
22682 {
22683 w->phys_cursor_ascent = glyph_row->ascent;
22684 w->phys_cursor_height = glyph_row->height;
22685
22686 /* Set phys_cursor_.* before x_draw_.* is called because some
22687 of them may need the information. */
22688 w->phys_cursor.x = x;
22689 w->phys_cursor.y = glyph_row->y;
22690 w->phys_cursor.hpos = hpos;
22691 w->phys_cursor.vpos = vpos;
22692 }
22693
22694 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22695 new_cursor_type, new_cursor_width,
22696 on, active_cursor);
22697 }
22698
22699
22700 /* Switch the display of W's cursor on or off, according to the value
22701 of ON. */
22702
22703 #ifndef HAVE_NS
22704 static
22705 #endif
22706 void
22707 update_window_cursor (w, on)
22708 struct window *w;
22709 int on;
22710 {
22711 /* Don't update cursor in windows whose frame is in the process
22712 of being deleted. */
22713 if (w->current_matrix)
22714 {
22715 BLOCK_INPUT;
22716 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22717 w->phys_cursor.x, w->phys_cursor.y);
22718 UNBLOCK_INPUT;
22719 }
22720 }
22721
22722
22723 /* Call update_window_cursor with parameter ON_P on all leaf windows
22724 in the window tree rooted at W. */
22725
22726 static void
22727 update_cursor_in_window_tree (w, on_p)
22728 struct window *w;
22729 int on_p;
22730 {
22731 while (w)
22732 {
22733 if (!NILP (w->hchild))
22734 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22735 else if (!NILP (w->vchild))
22736 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22737 else
22738 update_window_cursor (w, on_p);
22739
22740 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22741 }
22742 }
22743
22744
22745 /* EXPORT:
22746 Display the cursor on window W, or clear it, according to ON_P.
22747 Don't change the cursor's position. */
22748
22749 void
22750 x_update_cursor (f, on_p)
22751 struct frame *f;
22752 int on_p;
22753 {
22754 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22755 }
22756
22757
22758 /* EXPORT:
22759 Clear the cursor of window W to background color, and mark the
22760 cursor as not shown. This is used when the text where the cursor
22761 is is about to be rewritten. */
22762
22763 void
22764 x_clear_cursor (w)
22765 struct window *w;
22766 {
22767 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22768 update_window_cursor (w, 0);
22769 }
22770
22771
22772 /* EXPORT:
22773 Display the active region described by mouse_face_* according to DRAW. */
22774
22775 void
22776 show_mouse_face (dpyinfo, draw)
22777 Display_Info *dpyinfo;
22778 enum draw_glyphs_face draw;
22779 {
22780 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22781 struct frame *f = XFRAME (WINDOW_FRAME (w));
22782
22783 if (/* If window is in the process of being destroyed, don't bother
22784 to do anything. */
22785 w->current_matrix != NULL
22786 /* Don't update mouse highlight if hidden */
22787 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22788 /* Recognize when we are called to operate on rows that don't exist
22789 anymore. This can happen when a window is split. */
22790 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22791 {
22792 int phys_cursor_on_p = w->phys_cursor_on_p;
22793 struct glyph_row *row, *first, *last;
22794
22795 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22796 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22797
22798 for (row = first; row <= last && row->enabled_p; ++row)
22799 {
22800 int start_hpos, end_hpos, start_x;
22801
22802 /* For all but the first row, the highlight starts at column 0. */
22803 if (row == first)
22804 {
22805 start_hpos = dpyinfo->mouse_face_beg_col;
22806 start_x = dpyinfo->mouse_face_beg_x;
22807 }
22808 else
22809 {
22810 start_hpos = 0;
22811 start_x = 0;
22812 }
22813
22814 if (row == last)
22815 end_hpos = dpyinfo->mouse_face_end_col;
22816 else
22817 {
22818 end_hpos = row->used[TEXT_AREA];
22819 if (draw == DRAW_NORMAL_TEXT)
22820 row->fill_line_p = 1; /* Clear to end of line */
22821 }
22822
22823 if (end_hpos > start_hpos)
22824 {
22825 draw_glyphs (w, start_x, row, TEXT_AREA,
22826 start_hpos, end_hpos,
22827 draw, 0);
22828
22829 row->mouse_face_p
22830 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22831 }
22832 }
22833
22834 /* When we've written over the cursor, arrange for it to
22835 be displayed again. */
22836 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22837 {
22838 BLOCK_INPUT;
22839 display_and_set_cursor (w, 1,
22840 w->phys_cursor.hpos, w->phys_cursor.vpos,
22841 w->phys_cursor.x, w->phys_cursor.y);
22842 UNBLOCK_INPUT;
22843 }
22844 }
22845
22846 /* Change the mouse cursor. */
22847 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22848 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22849 else if (draw == DRAW_MOUSE_FACE)
22850 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22851 else
22852 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22853 }
22854
22855 /* EXPORT:
22856 Clear out the mouse-highlighted active region.
22857 Redraw it un-highlighted first. Value is non-zero if mouse
22858 face was actually drawn unhighlighted. */
22859
22860 int
22861 clear_mouse_face (dpyinfo)
22862 Display_Info *dpyinfo;
22863 {
22864 int cleared = 0;
22865
22866 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22867 {
22868 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22869 cleared = 1;
22870 }
22871
22872 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22873 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22874 dpyinfo->mouse_face_window = Qnil;
22875 dpyinfo->mouse_face_overlay = Qnil;
22876 return cleared;
22877 }
22878
22879
22880 /* EXPORT:
22881 Non-zero if physical cursor of window W is within mouse face. */
22882
22883 int
22884 cursor_in_mouse_face_p (w)
22885 struct window *w;
22886 {
22887 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22888 int in_mouse_face = 0;
22889
22890 if (WINDOWP (dpyinfo->mouse_face_window)
22891 && XWINDOW (dpyinfo->mouse_face_window) == w)
22892 {
22893 int hpos = w->phys_cursor.hpos;
22894 int vpos = w->phys_cursor.vpos;
22895
22896 if (vpos >= dpyinfo->mouse_face_beg_row
22897 && vpos <= dpyinfo->mouse_face_end_row
22898 && (vpos > dpyinfo->mouse_face_beg_row
22899 || hpos >= dpyinfo->mouse_face_beg_col)
22900 && (vpos < dpyinfo->mouse_face_end_row
22901 || hpos < dpyinfo->mouse_face_end_col
22902 || dpyinfo->mouse_face_past_end))
22903 in_mouse_face = 1;
22904 }
22905
22906 return in_mouse_face;
22907 }
22908
22909
22910
22911 \f
22912 /* Find the glyph matrix position of buffer position CHARPOS in window
22913 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22914 current glyphs must be up to date. If CHARPOS is above window
22915 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22916 of last line in W. In the row containing CHARPOS, stop before glyphs
22917 having STOP as object. */
22918
22919 #if 1 /* This is a version of fast_find_position that's more correct
22920 in the presence of hscrolling, for example. I didn't install
22921 it right away because the problem fixed is minor, it failed
22922 in 20.x as well, and I think it's too risky to install
22923 so near the release of 21.1. 2001-09-25 gerd. */
22924
22925 static
22926 int
22927 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22928 struct window *w;
22929 EMACS_INT charpos;
22930 int *hpos, *vpos, *x, *y;
22931 Lisp_Object stop;
22932 {
22933 struct glyph_row *row, *first;
22934 struct glyph *glyph, *end;
22935 int past_end = 0;
22936
22937 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22938 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22939 {
22940 *x = first->x;
22941 *y = first->y;
22942 *hpos = 0;
22943 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22944 return 1;
22945 }
22946
22947 row = row_containing_pos (w, charpos, first, NULL, 0);
22948 if (row == NULL)
22949 {
22950 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22951 past_end = 1;
22952 }
22953
22954 /* If whole rows or last part of a row came from a display overlay,
22955 row_containing_pos will skip over such rows because their end pos
22956 equals the start pos of the overlay or interval.
22957
22958 Move back if we have a STOP object and previous row's
22959 end glyph came from STOP. */
22960 if (!NILP (stop))
22961 {
22962 struct glyph_row *prev;
22963 while ((prev = row - 1, prev >= first)
22964 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22965 && prev->used[TEXT_AREA] > 0)
22966 {
22967 struct glyph *beg = prev->glyphs[TEXT_AREA];
22968 glyph = beg + prev->used[TEXT_AREA];
22969 while (--glyph >= beg
22970 && INTEGERP (glyph->object));
22971 if (glyph < beg
22972 || !EQ (stop, glyph->object))
22973 break;
22974 row = prev;
22975 }
22976 }
22977
22978 *x = row->x;
22979 *y = row->y;
22980 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22981
22982 glyph = row->glyphs[TEXT_AREA];
22983 end = glyph + row->used[TEXT_AREA];
22984
22985 /* Skip over glyphs not having an object at the start of the row.
22986 These are special glyphs like truncation marks on terminal
22987 frames. */
22988 if (row->displays_text_p)
22989 while (glyph < end
22990 && INTEGERP (glyph->object)
22991 && !EQ (stop, glyph->object)
22992 && glyph->charpos < 0)
22993 {
22994 *x += glyph->pixel_width;
22995 ++glyph;
22996 }
22997
22998 while (glyph < end
22999 && !INTEGERP (glyph->object)
23000 && !EQ (stop, glyph->object)
23001 && (!BUFFERP (glyph->object)
23002 || glyph->charpos < charpos))
23003 {
23004 *x += glyph->pixel_width;
23005 ++glyph;
23006 }
23007
23008 *hpos = glyph - row->glyphs[TEXT_AREA];
23009 return !past_end;
23010 }
23011
23012 #else /* not 1 */
23013
23014 static int
23015 fast_find_position (w, pos, hpos, vpos, x, y, stop)
23016 struct window *w;
23017 EMACS_INT pos;
23018 int *hpos, *vpos, *x, *y;
23019 Lisp_Object stop;
23020 {
23021 int i;
23022 int lastcol;
23023 int maybe_next_line_p = 0;
23024 int line_start_position;
23025 int yb = window_text_bottom_y (w);
23026 struct glyph_row *row, *best_row;
23027 int row_vpos, best_row_vpos;
23028 int current_x;
23029
23030 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23031 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23032
23033 while (row->y < yb)
23034 {
23035 if (row->used[TEXT_AREA])
23036 line_start_position = row->glyphs[TEXT_AREA]->charpos;
23037 else
23038 line_start_position = 0;
23039
23040 if (line_start_position > pos)
23041 break;
23042 /* If the position sought is the end of the buffer,
23043 don't include the blank lines at the bottom of the window. */
23044 else if (line_start_position == pos
23045 && pos == BUF_ZV (XBUFFER (w->buffer)))
23046 {
23047 maybe_next_line_p = 1;
23048 break;
23049 }
23050 else if (line_start_position > 0)
23051 {
23052 best_row = row;
23053 best_row_vpos = row_vpos;
23054 }
23055
23056 if (row->y + row->height >= yb)
23057 break;
23058
23059 ++row;
23060 ++row_vpos;
23061 }
23062
23063 /* Find the right column within BEST_ROW. */
23064 lastcol = 0;
23065 current_x = best_row->x;
23066 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
23067 {
23068 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
23069 int charpos = glyph->charpos;
23070
23071 if (BUFFERP (glyph->object))
23072 {
23073 if (charpos == pos)
23074 {
23075 *hpos = i;
23076 *vpos = best_row_vpos;
23077 *x = current_x;
23078 *y = best_row->y;
23079 return 1;
23080 }
23081 else if (charpos > pos)
23082 break;
23083 }
23084 else if (EQ (glyph->object, stop))
23085 break;
23086
23087 if (charpos > 0)
23088 lastcol = i;
23089 current_x += glyph->pixel_width;
23090 }
23091
23092 /* If we're looking for the end of the buffer,
23093 and we didn't find it in the line we scanned,
23094 use the start of the following line. */
23095 if (maybe_next_line_p)
23096 {
23097 ++best_row;
23098 ++best_row_vpos;
23099 lastcol = 0;
23100 current_x = best_row->x;
23101 }
23102
23103 *vpos = best_row_vpos;
23104 *hpos = lastcol + 1;
23105 *x = current_x;
23106 *y = best_row->y;
23107 return 0;
23108 }
23109
23110 #endif /* not 1 */
23111
23112
23113 /* Find the position of the glyph for position POS in OBJECT in
23114 window W's current matrix, and return in *X, *Y the pixel
23115 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23116
23117 RIGHT_P non-zero means return the position of the right edge of the
23118 glyph, RIGHT_P zero means return the left edge position.
23119
23120 If no glyph for POS exists in the matrix, return the position of
23121 the glyph with the next smaller position that is in the matrix, if
23122 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23123 exists in the matrix, return the position of the glyph with the
23124 next larger position in OBJECT.
23125
23126 Value is non-zero if a glyph was found. */
23127
23128 static int
23129 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23130 struct window *w;
23131 EMACS_INT pos;
23132 Lisp_Object object;
23133 int *hpos, *vpos, *x, *y;
23134 int right_p;
23135 {
23136 int yb = window_text_bottom_y (w);
23137 struct glyph_row *r;
23138 struct glyph *best_glyph = NULL;
23139 struct glyph_row *best_row = NULL;
23140 int best_x = 0;
23141
23142 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23143 r->enabled_p && r->y < yb;
23144 ++r)
23145 {
23146 struct glyph *g = r->glyphs[TEXT_AREA];
23147 struct glyph *e = g + r->used[TEXT_AREA];
23148 int gx;
23149
23150 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23151 if (EQ (g->object, object))
23152 {
23153 if (g->charpos == pos)
23154 {
23155 best_glyph = g;
23156 best_x = gx;
23157 best_row = r;
23158 goto found;
23159 }
23160 else if (best_glyph == NULL
23161 || ((eabs (g->charpos - pos)
23162 < eabs (best_glyph->charpos - pos))
23163 && (right_p
23164 ? g->charpos < pos
23165 : g->charpos > pos)))
23166 {
23167 best_glyph = g;
23168 best_x = gx;
23169 best_row = r;
23170 }
23171 }
23172 }
23173
23174 found:
23175
23176 if (best_glyph)
23177 {
23178 *x = best_x;
23179 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23180
23181 if (right_p)
23182 {
23183 *x += best_glyph->pixel_width;
23184 ++*hpos;
23185 }
23186
23187 *y = best_row->y;
23188 *vpos = best_row - w->current_matrix->rows;
23189 }
23190
23191 return best_glyph != NULL;
23192 }
23193
23194
23195 /* See if position X, Y is within a hot-spot of an image. */
23196
23197 static int
23198 on_hot_spot_p (hot_spot, x, y)
23199 Lisp_Object hot_spot;
23200 int x, y;
23201 {
23202 if (!CONSP (hot_spot))
23203 return 0;
23204
23205 if (EQ (XCAR (hot_spot), Qrect))
23206 {
23207 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23208 Lisp_Object rect = XCDR (hot_spot);
23209 Lisp_Object tem;
23210 if (!CONSP (rect))
23211 return 0;
23212 if (!CONSP (XCAR (rect)))
23213 return 0;
23214 if (!CONSP (XCDR (rect)))
23215 return 0;
23216 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23217 return 0;
23218 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23219 return 0;
23220 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23221 return 0;
23222 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23223 return 0;
23224 return 1;
23225 }
23226 else if (EQ (XCAR (hot_spot), Qcircle))
23227 {
23228 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23229 Lisp_Object circ = XCDR (hot_spot);
23230 Lisp_Object lr, lx0, ly0;
23231 if (CONSP (circ)
23232 && CONSP (XCAR (circ))
23233 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23234 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23235 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23236 {
23237 double r = XFLOATINT (lr);
23238 double dx = XINT (lx0) - x;
23239 double dy = XINT (ly0) - y;
23240 return (dx * dx + dy * dy <= r * r);
23241 }
23242 }
23243 else if (EQ (XCAR (hot_spot), Qpoly))
23244 {
23245 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23246 if (VECTORP (XCDR (hot_spot)))
23247 {
23248 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23249 Lisp_Object *poly = v->contents;
23250 int n = v->size;
23251 int i;
23252 int inside = 0;
23253 Lisp_Object lx, ly;
23254 int x0, y0;
23255
23256 /* Need an even number of coordinates, and at least 3 edges. */
23257 if (n < 6 || n & 1)
23258 return 0;
23259
23260 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23261 If count is odd, we are inside polygon. Pixels on edges
23262 may or may not be included depending on actual geometry of the
23263 polygon. */
23264 if ((lx = poly[n-2], !INTEGERP (lx))
23265 || (ly = poly[n-1], !INTEGERP (lx)))
23266 return 0;
23267 x0 = XINT (lx), y0 = XINT (ly);
23268 for (i = 0; i < n; i += 2)
23269 {
23270 int x1 = x0, y1 = y0;
23271 if ((lx = poly[i], !INTEGERP (lx))
23272 || (ly = poly[i+1], !INTEGERP (ly)))
23273 return 0;
23274 x0 = XINT (lx), y0 = XINT (ly);
23275
23276 /* Does this segment cross the X line? */
23277 if (x0 >= x)
23278 {
23279 if (x1 >= x)
23280 continue;
23281 }
23282 else if (x1 < x)
23283 continue;
23284 if (y > y0 && y > y1)
23285 continue;
23286 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23287 inside = !inside;
23288 }
23289 return inside;
23290 }
23291 }
23292 return 0;
23293 }
23294
23295 Lisp_Object
23296 find_hot_spot (map, x, y)
23297 Lisp_Object map;
23298 int x, y;
23299 {
23300 while (CONSP (map))
23301 {
23302 if (CONSP (XCAR (map))
23303 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23304 return XCAR (map);
23305 map = XCDR (map);
23306 }
23307
23308 return Qnil;
23309 }
23310
23311 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23312 3, 3, 0,
23313 doc: /* Lookup in image map MAP coordinates X and Y.
23314 An image map is an alist where each element has the format (AREA ID PLIST).
23315 An AREA is specified as either a rectangle, a circle, or a polygon:
23316 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23317 pixel coordinates of the upper left and bottom right corners.
23318 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23319 and the radius of the circle; r may be a float or integer.
23320 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23321 vector describes one corner in the polygon.
23322 Returns the alist element for the first matching AREA in MAP. */)
23323 (map, x, y)
23324 Lisp_Object map;
23325 Lisp_Object x, y;
23326 {
23327 if (NILP (map))
23328 return Qnil;
23329
23330 CHECK_NUMBER (x);
23331 CHECK_NUMBER (y);
23332
23333 return find_hot_spot (map, XINT (x), XINT (y));
23334 }
23335
23336
23337 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23338 static void
23339 define_frame_cursor1 (f, cursor, pointer)
23340 struct frame *f;
23341 Cursor cursor;
23342 Lisp_Object pointer;
23343 {
23344 /* Do not change cursor shape while dragging mouse. */
23345 if (!NILP (do_mouse_tracking))
23346 return;
23347
23348 if (!NILP (pointer))
23349 {
23350 if (EQ (pointer, Qarrow))
23351 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23352 else if (EQ (pointer, Qhand))
23353 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23354 else if (EQ (pointer, Qtext))
23355 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23356 else if (EQ (pointer, intern ("hdrag")))
23357 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23358 #ifdef HAVE_X_WINDOWS
23359 else if (EQ (pointer, intern ("vdrag")))
23360 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23361 #endif
23362 else if (EQ (pointer, intern ("hourglass")))
23363 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23364 else if (EQ (pointer, Qmodeline))
23365 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23366 else
23367 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23368 }
23369
23370 if (cursor != No_Cursor)
23371 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23372 }
23373
23374 /* Take proper action when mouse has moved to the mode or header line
23375 or marginal area AREA of window W, x-position X and y-position Y.
23376 X is relative to the start of the text display area of W, so the
23377 width of bitmap areas and scroll bars must be subtracted to get a
23378 position relative to the start of the mode line. */
23379
23380 static void
23381 note_mode_line_or_margin_highlight (window, x, y, area)
23382 Lisp_Object window;
23383 int x, y;
23384 enum window_part area;
23385 {
23386 struct window *w = XWINDOW (window);
23387 struct frame *f = XFRAME (w->frame);
23388 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23389 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23390 Lisp_Object pointer = Qnil;
23391 int charpos, dx, dy, width, height;
23392 Lisp_Object string, object = Qnil;
23393 Lisp_Object pos, help;
23394
23395 Lisp_Object mouse_face;
23396 int original_x_pixel = x;
23397 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23398 struct glyph_row *row;
23399
23400 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23401 {
23402 int x0;
23403 struct glyph *end;
23404
23405 string = mode_line_string (w, area, &x, &y, &charpos,
23406 &object, &dx, &dy, &width, &height);
23407
23408 row = (area == ON_MODE_LINE
23409 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23410 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23411
23412 /* Find glyph */
23413 if (row->mode_line_p && row->enabled_p)
23414 {
23415 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23416 end = glyph + row->used[TEXT_AREA];
23417
23418 for (x0 = original_x_pixel;
23419 glyph < end && x0 >= glyph->pixel_width;
23420 ++glyph)
23421 x0 -= glyph->pixel_width;
23422
23423 if (glyph >= end)
23424 glyph = NULL;
23425 }
23426 }
23427 else
23428 {
23429 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23430 string = marginal_area_string (w, area, &x, &y, &charpos,
23431 &object, &dx, &dy, &width, &height);
23432 }
23433
23434 help = Qnil;
23435
23436 if (IMAGEP (object))
23437 {
23438 Lisp_Object image_map, hotspot;
23439 if ((image_map = Fplist_get (XCDR (object), QCmap),
23440 !NILP (image_map))
23441 && (hotspot = find_hot_spot (image_map, dx, dy),
23442 CONSP (hotspot))
23443 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23444 {
23445 Lisp_Object area_id, plist;
23446
23447 area_id = XCAR (hotspot);
23448 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23449 If so, we could look for mouse-enter, mouse-leave
23450 properties in PLIST (and do something...). */
23451 hotspot = XCDR (hotspot);
23452 if (CONSP (hotspot)
23453 && (plist = XCAR (hotspot), CONSP (plist)))
23454 {
23455 pointer = Fplist_get (plist, Qpointer);
23456 if (NILP (pointer))
23457 pointer = Qhand;
23458 help = Fplist_get (plist, Qhelp_echo);
23459 if (!NILP (help))
23460 {
23461 help_echo_string = help;
23462 /* Is this correct? ++kfs */
23463 XSETWINDOW (help_echo_window, w);
23464 help_echo_object = w->buffer;
23465 help_echo_pos = charpos;
23466 }
23467 }
23468 }
23469 if (NILP (pointer))
23470 pointer = Fplist_get (XCDR (object), QCpointer);
23471 }
23472
23473 if (STRINGP (string))
23474 {
23475 pos = make_number (charpos);
23476 /* If we're on a string with `help-echo' text property, arrange
23477 for the help to be displayed. This is done by setting the
23478 global variable help_echo_string to the help string. */
23479 if (NILP (help))
23480 {
23481 help = Fget_text_property (pos, Qhelp_echo, string);
23482 if (!NILP (help))
23483 {
23484 help_echo_string = help;
23485 XSETWINDOW (help_echo_window, w);
23486 help_echo_object = string;
23487 help_echo_pos = charpos;
23488 }
23489 }
23490
23491 if (NILP (pointer))
23492 pointer = Fget_text_property (pos, Qpointer, string);
23493
23494 /* Change the mouse pointer according to what is under X/Y. */
23495 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23496 {
23497 Lisp_Object map;
23498 map = Fget_text_property (pos, Qlocal_map, string);
23499 if (!KEYMAPP (map))
23500 map = Fget_text_property (pos, Qkeymap, string);
23501 if (!KEYMAPP (map))
23502 cursor = dpyinfo->vertical_scroll_bar_cursor;
23503 }
23504
23505 /* Change the mouse face according to what is under X/Y. */
23506 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23507 if (!NILP (mouse_face)
23508 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23509 && glyph)
23510 {
23511 Lisp_Object b, e;
23512
23513 struct glyph * tmp_glyph;
23514
23515 int gpos;
23516 int gseq_length;
23517 int total_pixel_width;
23518 EMACS_INT ignore;
23519
23520 int vpos, hpos;
23521
23522 b = Fprevious_single_property_change (make_number (charpos + 1),
23523 Qmouse_face, string, Qnil);
23524 if (NILP (b))
23525 b = make_number (0);
23526
23527 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23528 if (NILP (e))
23529 e = make_number (SCHARS (string));
23530
23531 /* Calculate the position(glyph position: GPOS) of GLYPH in
23532 displayed string. GPOS is different from CHARPOS.
23533
23534 CHARPOS is the position of glyph in internal string
23535 object. A mode line string format has structures which
23536 is converted to a flatten by emacs lisp interpreter.
23537 The internal string is an element of the structures.
23538 The displayed string is the flatten string. */
23539 gpos = 0;
23540 if (glyph > row_start_glyph)
23541 {
23542 tmp_glyph = glyph - 1;
23543 while (tmp_glyph >= row_start_glyph
23544 && tmp_glyph->charpos >= XINT (b)
23545 && EQ (tmp_glyph->object, glyph->object))
23546 {
23547 tmp_glyph--;
23548 gpos++;
23549 }
23550 }
23551
23552 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23553 displayed string holding GLYPH.
23554
23555 GSEQ_LENGTH is different from SCHARS (STRING).
23556 SCHARS (STRING) returns the length of the internal string. */
23557 for (tmp_glyph = glyph, gseq_length = gpos;
23558 tmp_glyph->charpos < XINT (e);
23559 tmp_glyph++, gseq_length++)
23560 {
23561 if (!EQ (tmp_glyph->object, glyph->object))
23562 break;
23563 }
23564
23565 total_pixel_width = 0;
23566 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23567 total_pixel_width += tmp_glyph->pixel_width;
23568
23569 /* Pre calculation of re-rendering position */
23570 vpos = (x - gpos);
23571 hpos = (area == ON_MODE_LINE
23572 ? (w->current_matrix)->nrows - 1
23573 : 0);
23574
23575 /* If the re-rendering position is included in the last
23576 re-rendering area, we should do nothing. */
23577 if ( EQ (window, dpyinfo->mouse_face_window)
23578 && dpyinfo->mouse_face_beg_col <= vpos
23579 && vpos < dpyinfo->mouse_face_end_col
23580 && dpyinfo->mouse_face_beg_row == hpos )
23581 return;
23582
23583 if (clear_mouse_face (dpyinfo))
23584 cursor = No_Cursor;
23585
23586 dpyinfo->mouse_face_beg_col = vpos;
23587 dpyinfo->mouse_face_beg_row = hpos;
23588
23589 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23590 dpyinfo->mouse_face_beg_y = 0;
23591
23592 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23593 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23594
23595 dpyinfo->mouse_face_end_x = 0;
23596 dpyinfo->mouse_face_end_y = 0;
23597
23598 dpyinfo->mouse_face_past_end = 0;
23599 dpyinfo->mouse_face_window = window;
23600
23601 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23602 charpos,
23603 0, 0, 0, &ignore,
23604 glyph->face_id, 1);
23605 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23606
23607 if (NILP (pointer))
23608 pointer = Qhand;
23609 }
23610 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23611 clear_mouse_face (dpyinfo);
23612 }
23613 define_frame_cursor1 (f, cursor, pointer);
23614 }
23615
23616
23617 /* EXPORT:
23618 Take proper action when the mouse has moved to position X, Y on
23619 frame F as regards highlighting characters that have mouse-face
23620 properties. Also de-highlighting chars where the mouse was before.
23621 X and Y can be negative or out of range. */
23622
23623 void
23624 note_mouse_highlight (f, x, y)
23625 struct frame *f;
23626 int x, y;
23627 {
23628 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23629 enum window_part part;
23630 Lisp_Object window;
23631 struct window *w;
23632 Cursor cursor = No_Cursor;
23633 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23634 struct buffer *b;
23635
23636 /* When a menu is active, don't highlight because this looks odd. */
23637 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23638 if (popup_activated ())
23639 return;
23640 #endif
23641
23642 if (NILP (Vmouse_highlight)
23643 || !f->glyphs_initialized_p)
23644 return;
23645
23646 dpyinfo->mouse_face_mouse_x = x;
23647 dpyinfo->mouse_face_mouse_y = y;
23648 dpyinfo->mouse_face_mouse_frame = f;
23649
23650 if (dpyinfo->mouse_face_defer)
23651 return;
23652
23653 if (gc_in_progress)
23654 {
23655 dpyinfo->mouse_face_deferred_gc = 1;
23656 return;
23657 }
23658
23659 /* Which window is that in? */
23660 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23661
23662 /* If we were displaying active text in another window, clear that.
23663 Also clear if we move out of text area in same window. */
23664 if (! EQ (window, dpyinfo->mouse_face_window)
23665 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23666 && !NILP (dpyinfo->mouse_face_window)))
23667 clear_mouse_face (dpyinfo);
23668
23669 /* Not on a window -> return. */
23670 if (!WINDOWP (window))
23671 return;
23672
23673 /* Reset help_echo_string. It will get recomputed below. */
23674 help_echo_string = Qnil;
23675
23676 /* Convert to window-relative pixel coordinates. */
23677 w = XWINDOW (window);
23678 frame_to_window_pixel_xy (w, &x, &y);
23679
23680 /* Handle tool-bar window differently since it doesn't display a
23681 buffer. */
23682 if (EQ (window, f->tool_bar_window))
23683 {
23684 note_tool_bar_highlight (f, x, y);
23685 return;
23686 }
23687
23688 /* Mouse is on the mode, header line or margin? */
23689 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23690 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23691 {
23692 note_mode_line_or_margin_highlight (window, x, y, part);
23693 return;
23694 }
23695
23696 if (part == ON_VERTICAL_BORDER)
23697 {
23698 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23699 help_echo_string = build_string ("drag-mouse-1: resize");
23700 }
23701 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23702 || part == ON_SCROLL_BAR)
23703 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23704 else
23705 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23706
23707 /* Are we in a window whose display is up to date?
23708 And verify the buffer's text has not changed. */
23709 b = XBUFFER (w->buffer);
23710 if (part == ON_TEXT
23711 && EQ (w->window_end_valid, w->buffer)
23712 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23713 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23714 {
23715 int hpos, vpos, pos, i, dx, dy, area;
23716 struct glyph *glyph;
23717 Lisp_Object object;
23718 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23719 Lisp_Object *overlay_vec = NULL;
23720 int noverlays;
23721 struct buffer *obuf;
23722 int obegv, ozv, same_region;
23723
23724 /* Find the glyph under X/Y. */
23725 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23726
23727 /* Look for :pointer property on image. */
23728 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23729 {
23730 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23731 if (img != NULL && IMAGEP (img->spec))
23732 {
23733 Lisp_Object image_map, hotspot;
23734 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23735 !NILP (image_map))
23736 && (hotspot = find_hot_spot (image_map,
23737 glyph->slice.x + dx,
23738 glyph->slice.y + dy),
23739 CONSP (hotspot))
23740 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23741 {
23742 Lisp_Object area_id, plist;
23743
23744 area_id = XCAR (hotspot);
23745 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23746 If so, we could look for mouse-enter, mouse-leave
23747 properties in PLIST (and do something...). */
23748 hotspot = XCDR (hotspot);
23749 if (CONSP (hotspot)
23750 && (plist = XCAR (hotspot), CONSP (plist)))
23751 {
23752 pointer = Fplist_get (plist, Qpointer);
23753 if (NILP (pointer))
23754 pointer = Qhand;
23755 help_echo_string = Fplist_get (plist, Qhelp_echo);
23756 if (!NILP (help_echo_string))
23757 {
23758 help_echo_window = window;
23759 help_echo_object = glyph->object;
23760 help_echo_pos = glyph->charpos;
23761 }
23762 }
23763 }
23764 if (NILP (pointer))
23765 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23766 }
23767 }
23768
23769 /* Clear mouse face if X/Y not over text. */
23770 if (glyph == NULL
23771 || area != TEXT_AREA
23772 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23773 {
23774 if (clear_mouse_face (dpyinfo))
23775 cursor = No_Cursor;
23776 if (NILP (pointer))
23777 {
23778 if (area != TEXT_AREA)
23779 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23780 else
23781 pointer = Vvoid_text_area_pointer;
23782 }
23783 goto set_cursor;
23784 }
23785
23786 pos = glyph->charpos;
23787 object = glyph->object;
23788 if (!STRINGP (object) && !BUFFERP (object))
23789 goto set_cursor;
23790
23791 /* If we get an out-of-range value, return now; avoid an error. */
23792 if (BUFFERP (object) && pos > BUF_Z (b))
23793 goto set_cursor;
23794
23795 /* Make the window's buffer temporarily current for
23796 overlays_at and compute_char_face. */
23797 obuf = current_buffer;
23798 current_buffer = b;
23799 obegv = BEGV;
23800 ozv = ZV;
23801 BEGV = BEG;
23802 ZV = Z;
23803
23804 /* Is this char mouse-active or does it have help-echo? */
23805 position = make_number (pos);
23806
23807 if (BUFFERP (object))
23808 {
23809 /* Put all the overlays we want in a vector in overlay_vec. */
23810 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23811 /* Sort overlays into increasing priority order. */
23812 noverlays = sort_overlays (overlay_vec, noverlays, w);
23813 }
23814 else
23815 noverlays = 0;
23816
23817 same_region = (EQ (window, dpyinfo->mouse_face_window)
23818 && vpos >= dpyinfo->mouse_face_beg_row
23819 && vpos <= dpyinfo->mouse_face_end_row
23820 && (vpos > dpyinfo->mouse_face_beg_row
23821 || hpos >= dpyinfo->mouse_face_beg_col)
23822 && (vpos < dpyinfo->mouse_face_end_row
23823 || hpos < dpyinfo->mouse_face_end_col
23824 || dpyinfo->mouse_face_past_end));
23825
23826 if (same_region)
23827 cursor = No_Cursor;
23828
23829 /* Check mouse-face highlighting. */
23830 if (! same_region
23831 /* If there exists an overlay with mouse-face overlapping
23832 the one we are currently highlighting, we have to
23833 check if we enter the overlapping overlay, and then
23834 highlight only that. */
23835 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23836 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23837 {
23838 /* Find the highest priority overlay that has a mouse-face
23839 property. */
23840 overlay = Qnil;
23841 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23842 {
23843 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23844 if (!NILP (mouse_face))
23845 overlay = overlay_vec[i];
23846 }
23847
23848 /* If we're actually highlighting the same overlay as
23849 before, there's no need to do that again. */
23850 if (!NILP (overlay)
23851 && EQ (overlay, dpyinfo->mouse_face_overlay))
23852 goto check_help_echo;
23853
23854 dpyinfo->mouse_face_overlay = overlay;
23855
23856 /* Clear the display of the old active region, if any. */
23857 if (clear_mouse_face (dpyinfo))
23858 cursor = No_Cursor;
23859
23860 /* If no overlay applies, get a text property. */
23861 if (NILP (overlay))
23862 mouse_face = Fget_text_property (position, Qmouse_face, object);
23863
23864 /* Handle the overlay case. */
23865 if (!NILP (overlay))
23866 {
23867 /* Find the range of text around this char that
23868 should be active. */
23869 Lisp_Object before, after;
23870 EMACS_INT ignore;
23871
23872 before = Foverlay_start (overlay);
23873 after = Foverlay_end (overlay);
23874 /* Record this as the current active region. */
23875 fast_find_position (w, XFASTINT (before),
23876 &dpyinfo->mouse_face_beg_col,
23877 &dpyinfo->mouse_face_beg_row,
23878 &dpyinfo->mouse_face_beg_x,
23879 &dpyinfo->mouse_face_beg_y, Qnil);
23880
23881 dpyinfo->mouse_face_past_end
23882 = !fast_find_position (w, XFASTINT (after),
23883 &dpyinfo->mouse_face_end_col,
23884 &dpyinfo->mouse_face_end_row,
23885 &dpyinfo->mouse_face_end_x,
23886 &dpyinfo->mouse_face_end_y, Qnil);
23887 dpyinfo->mouse_face_window = window;
23888
23889 dpyinfo->mouse_face_face_id
23890 = face_at_buffer_position (w, pos, 0, 0,
23891 &ignore, pos + 1,
23892 !dpyinfo->mouse_face_hidden);
23893
23894 /* Display it as active. */
23895 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23896 cursor = No_Cursor;
23897 }
23898 /* Handle the text property case. */
23899 else if (!NILP (mouse_face) && BUFFERP (object))
23900 {
23901 /* Find the range of text around this char that
23902 should be active. */
23903 Lisp_Object before, after, beginning, end;
23904 EMACS_INT ignore;
23905
23906 beginning = Fmarker_position (w->start);
23907 end = make_number (BUF_Z (XBUFFER (object))
23908 - XFASTINT (w->window_end_pos));
23909 before
23910 = Fprevious_single_property_change (make_number (pos + 1),
23911 Qmouse_face,
23912 object, beginning);
23913 after
23914 = Fnext_single_property_change (position, Qmouse_face,
23915 object, end);
23916
23917 /* Record this as the current active region. */
23918 fast_find_position (w, XFASTINT (before),
23919 &dpyinfo->mouse_face_beg_col,
23920 &dpyinfo->mouse_face_beg_row,
23921 &dpyinfo->mouse_face_beg_x,
23922 &dpyinfo->mouse_face_beg_y, Qnil);
23923 dpyinfo->mouse_face_past_end
23924 = !fast_find_position (w, XFASTINT (after),
23925 &dpyinfo->mouse_face_end_col,
23926 &dpyinfo->mouse_face_end_row,
23927 &dpyinfo->mouse_face_end_x,
23928 &dpyinfo->mouse_face_end_y, Qnil);
23929 dpyinfo->mouse_face_window = window;
23930
23931 if (BUFFERP (object))
23932 dpyinfo->mouse_face_face_id
23933 = face_at_buffer_position (w, pos, 0, 0,
23934 &ignore, pos + 1,
23935 !dpyinfo->mouse_face_hidden);
23936
23937 /* Display it as active. */
23938 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23939 cursor = No_Cursor;
23940 }
23941 else if (!NILP (mouse_face) && STRINGP (object))
23942 {
23943 Lisp_Object b, e;
23944 EMACS_INT ignore;
23945
23946 b = Fprevious_single_property_change (make_number (pos + 1),
23947 Qmouse_face,
23948 object, Qnil);
23949 e = Fnext_single_property_change (position, Qmouse_face,
23950 object, Qnil);
23951 if (NILP (b))
23952 b = make_number (0);
23953 if (NILP (e))
23954 e = make_number (SCHARS (object) - 1);
23955
23956 fast_find_string_pos (w, XINT (b), object,
23957 &dpyinfo->mouse_face_beg_col,
23958 &dpyinfo->mouse_face_beg_row,
23959 &dpyinfo->mouse_face_beg_x,
23960 &dpyinfo->mouse_face_beg_y, 0);
23961 fast_find_string_pos (w, XINT (e), object,
23962 &dpyinfo->mouse_face_end_col,
23963 &dpyinfo->mouse_face_end_row,
23964 &dpyinfo->mouse_face_end_x,
23965 &dpyinfo->mouse_face_end_y, 1);
23966 dpyinfo->mouse_face_past_end = 0;
23967 dpyinfo->mouse_face_window = window;
23968 dpyinfo->mouse_face_face_id
23969 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23970 glyph->face_id, 1);
23971 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23972 cursor = No_Cursor;
23973 }
23974 else if (STRINGP (object) && NILP (mouse_face))
23975 {
23976 /* A string which doesn't have mouse-face, but
23977 the text ``under'' it might have. */
23978 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23979 int start = MATRIX_ROW_START_CHARPOS (r);
23980
23981 pos = string_buffer_position (w, object, start);
23982 if (pos > 0)
23983 mouse_face = get_char_property_and_overlay (make_number (pos),
23984 Qmouse_face,
23985 w->buffer,
23986 &overlay);
23987 if (!NILP (mouse_face) && !NILP (overlay))
23988 {
23989 Lisp_Object before = Foverlay_start (overlay);
23990 Lisp_Object after = Foverlay_end (overlay);
23991 EMACS_INT ignore;
23992
23993 /* Note that we might not be able to find position
23994 BEFORE in the glyph matrix if the overlay is
23995 entirely covered by a `display' property. In
23996 this case, we overshoot. So let's stop in
23997 the glyph matrix before glyphs for OBJECT. */
23998 fast_find_position (w, XFASTINT (before),
23999 &dpyinfo->mouse_face_beg_col,
24000 &dpyinfo->mouse_face_beg_row,
24001 &dpyinfo->mouse_face_beg_x,
24002 &dpyinfo->mouse_face_beg_y,
24003 object);
24004
24005 dpyinfo->mouse_face_past_end
24006 = !fast_find_position (w, XFASTINT (after),
24007 &dpyinfo->mouse_face_end_col,
24008 &dpyinfo->mouse_face_end_row,
24009 &dpyinfo->mouse_face_end_x,
24010 &dpyinfo->mouse_face_end_y,
24011 Qnil);
24012 dpyinfo->mouse_face_window = window;
24013 dpyinfo->mouse_face_face_id
24014 = face_at_buffer_position (w, pos, 0, 0,
24015 &ignore, pos + 1,
24016 !dpyinfo->mouse_face_hidden);
24017
24018 /* Display it as active. */
24019 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24020 cursor = No_Cursor;
24021 }
24022 }
24023 }
24024
24025 check_help_echo:
24026
24027 /* Look for a `help-echo' property. */
24028 if (NILP (help_echo_string)) {
24029 Lisp_Object help, overlay;
24030
24031 /* Check overlays first. */
24032 help = overlay = Qnil;
24033 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24034 {
24035 overlay = overlay_vec[i];
24036 help = Foverlay_get (overlay, Qhelp_echo);
24037 }
24038
24039 if (!NILP (help))
24040 {
24041 help_echo_string = help;
24042 help_echo_window = window;
24043 help_echo_object = overlay;
24044 help_echo_pos = pos;
24045 }
24046 else
24047 {
24048 Lisp_Object object = glyph->object;
24049 int charpos = glyph->charpos;
24050
24051 /* Try text properties. */
24052 if (STRINGP (object)
24053 && charpos >= 0
24054 && charpos < SCHARS (object))
24055 {
24056 help = Fget_text_property (make_number (charpos),
24057 Qhelp_echo, object);
24058 if (NILP (help))
24059 {
24060 /* If the string itself doesn't specify a help-echo,
24061 see if the buffer text ``under'' it does. */
24062 struct glyph_row *r
24063 = MATRIX_ROW (w->current_matrix, vpos);
24064 int start = MATRIX_ROW_START_CHARPOS (r);
24065 int pos = string_buffer_position (w, object, start);
24066 if (pos > 0)
24067 {
24068 help = Fget_char_property (make_number (pos),
24069 Qhelp_echo, w->buffer);
24070 if (!NILP (help))
24071 {
24072 charpos = pos;
24073 object = w->buffer;
24074 }
24075 }
24076 }
24077 }
24078 else if (BUFFERP (object)
24079 && charpos >= BEGV
24080 && charpos < ZV)
24081 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24082 object);
24083
24084 if (!NILP (help))
24085 {
24086 help_echo_string = help;
24087 help_echo_window = window;
24088 help_echo_object = object;
24089 help_echo_pos = charpos;
24090 }
24091 }
24092 }
24093
24094 /* Look for a `pointer' property. */
24095 if (NILP (pointer))
24096 {
24097 /* Check overlays first. */
24098 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24099 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24100
24101 if (NILP (pointer))
24102 {
24103 Lisp_Object object = glyph->object;
24104 int charpos = glyph->charpos;
24105
24106 /* Try text properties. */
24107 if (STRINGP (object)
24108 && charpos >= 0
24109 && charpos < SCHARS (object))
24110 {
24111 pointer = Fget_text_property (make_number (charpos),
24112 Qpointer, object);
24113 if (NILP (pointer))
24114 {
24115 /* If the string itself doesn't specify a pointer,
24116 see if the buffer text ``under'' it does. */
24117 struct glyph_row *r
24118 = MATRIX_ROW (w->current_matrix, vpos);
24119 int start = MATRIX_ROW_START_CHARPOS (r);
24120 int pos = string_buffer_position (w, object, start);
24121 if (pos > 0)
24122 pointer = Fget_char_property (make_number (pos),
24123 Qpointer, w->buffer);
24124 }
24125 }
24126 else if (BUFFERP (object)
24127 && charpos >= BEGV
24128 && charpos < ZV)
24129 pointer = Fget_text_property (make_number (charpos),
24130 Qpointer, object);
24131 }
24132 }
24133
24134 BEGV = obegv;
24135 ZV = ozv;
24136 current_buffer = obuf;
24137 }
24138
24139 set_cursor:
24140
24141 define_frame_cursor1 (f, cursor, pointer);
24142 }
24143
24144
24145 /* EXPORT for RIF:
24146 Clear any mouse-face on window W. This function is part of the
24147 redisplay interface, and is called from try_window_id and similar
24148 functions to ensure the mouse-highlight is off. */
24149
24150 void
24151 x_clear_window_mouse_face (w)
24152 struct window *w;
24153 {
24154 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24155 Lisp_Object window;
24156
24157 BLOCK_INPUT;
24158 XSETWINDOW (window, w);
24159 if (EQ (window, dpyinfo->mouse_face_window))
24160 clear_mouse_face (dpyinfo);
24161 UNBLOCK_INPUT;
24162 }
24163
24164
24165 /* EXPORT:
24166 Just discard the mouse face information for frame F, if any.
24167 This is used when the size of F is changed. */
24168
24169 void
24170 cancel_mouse_face (f)
24171 struct frame *f;
24172 {
24173 Lisp_Object window;
24174 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24175
24176 window = dpyinfo->mouse_face_window;
24177 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24178 {
24179 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24180 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24181 dpyinfo->mouse_face_window = Qnil;
24182 }
24183 }
24184
24185
24186 #endif /* HAVE_WINDOW_SYSTEM */
24187
24188 \f
24189 /***********************************************************************
24190 Exposure Events
24191 ***********************************************************************/
24192
24193 #ifdef HAVE_WINDOW_SYSTEM
24194
24195 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24196 which intersects rectangle R. R is in window-relative coordinates. */
24197
24198 static void
24199 expose_area (w, row, r, area)
24200 struct window *w;
24201 struct glyph_row *row;
24202 XRectangle *r;
24203 enum glyph_row_area area;
24204 {
24205 struct glyph *first = row->glyphs[area];
24206 struct glyph *end = row->glyphs[area] + row->used[area];
24207 struct glyph *last;
24208 int first_x, start_x, x;
24209
24210 if (area == TEXT_AREA && row->fill_line_p)
24211 /* If row extends face to end of line write the whole line. */
24212 draw_glyphs (w, 0, row, area,
24213 0, row->used[area],
24214 DRAW_NORMAL_TEXT, 0);
24215 else
24216 {
24217 /* Set START_X to the window-relative start position for drawing glyphs of
24218 AREA. The first glyph of the text area can be partially visible.
24219 The first glyphs of other areas cannot. */
24220 start_x = window_box_left_offset (w, area);
24221 x = start_x;
24222 if (area == TEXT_AREA)
24223 x += row->x;
24224
24225 /* Find the first glyph that must be redrawn. */
24226 while (first < end
24227 && x + first->pixel_width < r->x)
24228 {
24229 x += first->pixel_width;
24230 ++first;
24231 }
24232
24233 /* Find the last one. */
24234 last = first;
24235 first_x = x;
24236 while (last < end
24237 && x < r->x + r->width)
24238 {
24239 x += last->pixel_width;
24240 ++last;
24241 }
24242
24243 /* Repaint. */
24244 if (last > first)
24245 draw_glyphs (w, first_x - start_x, row, area,
24246 first - row->glyphs[area], last - row->glyphs[area],
24247 DRAW_NORMAL_TEXT, 0);
24248 }
24249 }
24250
24251
24252 /* Redraw the parts of the glyph row ROW on window W intersecting
24253 rectangle R. R is in window-relative coordinates. Value is
24254 non-zero if mouse-face was overwritten. */
24255
24256 static int
24257 expose_line (w, row, r)
24258 struct window *w;
24259 struct glyph_row *row;
24260 XRectangle *r;
24261 {
24262 xassert (row->enabled_p);
24263
24264 if (row->mode_line_p || w->pseudo_window_p)
24265 draw_glyphs (w, 0, row, TEXT_AREA,
24266 0, row->used[TEXT_AREA],
24267 DRAW_NORMAL_TEXT, 0);
24268 else
24269 {
24270 if (row->used[LEFT_MARGIN_AREA])
24271 expose_area (w, row, r, LEFT_MARGIN_AREA);
24272 if (row->used[TEXT_AREA])
24273 expose_area (w, row, r, TEXT_AREA);
24274 if (row->used[RIGHT_MARGIN_AREA])
24275 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24276 draw_row_fringe_bitmaps (w, row);
24277 }
24278
24279 return row->mouse_face_p;
24280 }
24281
24282
24283 /* Redraw those parts of glyphs rows during expose event handling that
24284 overlap other rows. Redrawing of an exposed line writes over parts
24285 of lines overlapping that exposed line; this function fixes that.
24286
24287 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24288 row in W's current matrix that is exposed and overlaps other rows.
24289 LAST_OVERLAPPING_ROW is the last such row. */
24290
24291 static void
24292 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24293 struct window *w;
24294 struct glyph_row *first_overlapping_row;
24295 struct glyph_row *last_overlapping_row;
24296 XRectangle *r;
24297 {
24298 struct glyph_row *row;
24299
24300 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24301 if (row->overlapping_p)
24302 {
24303 xassert (row->enabled_p && !row->mode_line_p);
24304
24305 row->clip = r;
24306 if (row->used[LEFT_MARGIN_AREA])
24307 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24308
24309 if (row->used[TEXT_AREA])
24310 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24311
24312 if (row->used[RIGHT_MARGIN_AREA])
24313 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24314 row->clip = NULL;
24315 }
24316 }
24317
24318
24319 /* Return non-zero if W's cursor intersects rectangle R. */
24320
24321 static int
24322 phys_cursor_in_rect_p (w, r)
24323 struct window *w;
24324 XRectangle *r;
24325 {
24326 XRectangle cr, result;
24327 struct glyph *cursor_glyph;
24328 struct glyph_row *row;
24329
24330 if (w->phys_cursor.vpos >= 0
24331 && w->phys_cursor.vpos < w->current_matrix->nrows
24332 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24333 row->enabled_p)
24334 && row->cursor_in_fringe_p)
24335 {
24336 /* Cursor is in the fringe. */
24337 cr.x = window_box_right_offset (w,
24338 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24339 ? RIGHT_MARGIN_AREA
24340 : TEXT_AREA));
24341 cr.y = row->y;
24342 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24343 cr.height = row->height;
24344 return x_intersect_rectangles (&cr, r, &result);
24345 }
24346
24347 cursor_glyph = get_phys_cursor_glyph (w);
24348 if (cursor_glyph)
24349 {
24350 /* r is relative to W's box, but w->phys_cursor.x is relative
24351 to left edge of W's TEXT area. Adjust it. */
24352 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24353 cr.y = w->phys_cursor.y;
24354 cr.width = cursor_glyph->pixel_width;
24355 cr.height = w->phys_cursor_height;
24356 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24357 I assume the effect is the same -- and this is portable. */
24358 return x_intersect_rectangles (&cr, r, &result);
24359 }
24360 /* If we don't understand the format, pretend we're not in the hot-spot. */
24361 return 0;
24362 }
24363
24364
24365 /* EXPORT:
24366 Draw a vertical window border to the right of window W if W doesn't
24367 have vertical scroll bars. */
24368
24369 void
24370 x_draw_vertical_border (w)
24371 struct window *w;
24372 {
24373 struct frame *f = XFRAME (WINDOW_FRAME (w));
24374
24375 /* We could do better, if we knew what type of scroll-bar the adjacent
24376 windows (on either side) have... But we don't :-(
24377 However, I think this works ok. ++KFS 2003-04-25 */
24378
24379 /* Redraw borders between horizontally adjacent windows. Don't
24380 do it for frames with vertical scroll bars because either the
24381 right scroll bar of a window, or the left scroll bar of its
24382 neighbor will suffice as a border. */
24383 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24384 return;
24385
24386 if (!WINDOW_RIGHTMOST_P (w)
24387 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24388 {
24389 int x0, x1, y0, y1;
24390
24391 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24392 y1 -= 1;
24393
24394 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24395 x1 -= 1;
24396
24397 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24398 }
24399 else if (!WINDOW_LEFTMOST_P (w)
24400 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24401 {
24402 int x0, x1, y0, y1;
24403
24404 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24405 y1 -= 1;
24406
24407 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24408 x0 -= 1;
24409
24410 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24411 }
24412 }
24413
24414
24415 /* Redraw the part of window W intersection rectangle FR. Pixel
24416 coordinates in FR are frame-relative. Call this function with
24417 input blocked. Value is non-zero if the exposure overwrites
24418 mouse-face. */
24419
24420 static int
24421 expose_window (w, fr)
24422 struct window *w;
24423 XRectangle *fr;
24424 {
24425 struct frame *f = XFRAME (w->frame);
24426 XRectangle wr, r;
24427 int mouse_face_overwritten_p = 0;
24428
24429 /* If window is not yet fully initialized, do nothing. This can
24430 happen when toolkit scroll bars are used and a window is split.
24431 Reconfiguring the scroll bar will generate an expose for a newly
24432 created window. */
24433 if (w->current_matrix == NULL)
24434 return 0;
24435
24436 /* When we're currently updating the window, display and current
24437 matrix usually don't agree. Arrange for a thorough display
24438 later. */
24439 if (w == updated_window)
24440 {
24441 SET_FRAME_GARBAGED (f);
24442 return 0;
24443 }
24444
24445 /* Frame-relative pixel rectangle of W. */
24446 wr.x = WINDOW_LEFT_EDGE_X (w);
24447 wr.y = WINDOW_TOP_EDGE_Y (w);
24448 wr.width = WINDOW_TOTAL_WIDTH (w);
24449 wr.height = WINDOW_TOTAL_HEIGHT (w);
24450
24451 if (x_intersect_rectangles (fr, &wr, &r))
24452 {
24453 int yb = window_text_bottom_y (w);
24454 struct glyph_row *row;
24455 int cursor_cleared_p;
24456 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24457
24458 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24459 r.x, r.y, r.width, r.height));
24460
24461 /* Convert to window coordinates. */
24462 r.x -= WINDOW_LEFT_EDGE_X (w);
24463 r.y -= WINDOW_TOP_EDGE_Y (w);
24464
24465 /* Turn off the cursor. */
24466 if (!w->pseudo_window_p
24467 && phys_cursor_in_rect_p (w, &r))
24468 {
24469 x_clear_cursor (w);
24470 cursor_cleared_p = 1;
24471 }
24472 else
24473 cursor_cleared_p = 0;
24474
24475 /* Update lines intersecting rectangle R. */
24476 first_overlapping_row = last_overlapping_row = NULL;
24477 for (row = w->current_matrix->rows;
24478 row->enabled_p;
24479 ++row)
24480 {
24481 int y0 = row->y;
24482 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24483
24484 if ((y0 >= r.y && y0 < r.y + r.height)
24485 || (y1 > r.y && y1 < r.y + r.height)
24486 || (r.y >= y0 && r.y < y1)
24487 || (r.y + r.height > y0 && r.y + r.height < y1))
24488 {
24489 /* A header line may be overlapping, but there is no need
24490 to fix overlapping areas for them. KFS 2005-02-12 */
24491 if (row->overlapping_p && !row->mode_line_p)
24492 {
24493 if (first_overlapping_row == NULL)
24494 first_overlapping_row = row;
24495 last_overlapping_row = row;
24496 }
24497
24498 row->clip = fr;
24499 if (expose_line (w, row, &r))
24500 mouse_face_overwritten_p = 1;
24501 row->clip = NULL;
24502 }
24503 else if (row->overlapping_p)
24504 {
24505 /* We must redraw a row overlapping the exposed area. */
24506 if (y0 < r.y
24507 ? y0 + row->phys_height > r.y
24508 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24509 {
24510 if (first_overlapping_row == NULL)
24511 first_overlapping_row = row;
24512 last_overlapping_row = row;
24513 }
24514 }
24515
24516 if (y1 >= yb)
24517 break;
24518 }
24519
24520 /* Display the mode line if there is one. */
24521 if (WINDOW_WANTS_MODELINE_P (w)
24522 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24523 row->enabled_p)
24524 && row->y < r.y + r.height)
24525 {
24526 if (expose_line (w, row, &r))
24527 mouse_face_overwritten_p = 1;
24528 }
24529
24530 if (!w->pseudo_window_p)
24531 {
24532 /* Fix the display of overlapping rows. */
24533 if (first_overlapping_row)
24534 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24535 fr);
24536
24537 /* Draw border between windows. */
24538 x_draw_vertical_border (w);
24539
24540 /* Turn the cursor on again. */
24541 if (cursor_cleared_p)
24542 update_window_cursor (w, 1);
24543 }
24544 }
24545
24546 return mouse_face_overwritten_p;
24547 }
24548
24549
24550
24551 /* Redraw (parts) of all windows in the window tree rooted at W that
24552 intersect R. R contains frame pixel coordinates. Value is
24553 non-zero if the exposure overwrites mouse-face. */
24554
24555 static int
24556 expose_window_tree (w, r)
24557 struct window *w;
24558 XRectangle *r;
24559 {
24560 struct frame *f = XFRAME (w->frame);
24561 int mouse_face_overwritten_p = 0;
24562
24563 while (w && !FRAME_GARBAGED_P (f))
24564 {
24565 if (!NILP (w->hchild))
24566 mouse_face_overwritten_p
24567 |= expose_window_tree (XWINDOW (w->hchild), r);
24568 else if (!NILP (w->vchild))
24569 mouse_face_overwritten_p
24570 |= expose_window_tree (XWINDOW (w->vchild), r);
24571 else
24572 mouse_face_overwritten_p |= expose_window (w, r);
24573
24574 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24575 }
24576
24577 return mouse_face_overwritten_p;
24578 }
24579
24580
24581 /* EXPORT:
24582 Redisplay an exposed area of frame F. X and Y are the upper-left
24583 corner of the exposed rectangle. W and H are width and height of
24584 the exposed area. All are pixel values. W or H zero means redraw
24585 the entire frame. */
24586
24587 void
24588 expose_frame (f, x, y, w, h)
24589 struct frame *f;
24590 int x, y, w, h;
24591 {
24592 XRectangle r;
24593 int mouse_face_overwritten_p = 0;
24594
24595 TRACE ((stderr, "expose_frame "));
24596
24597 /* No need to redraw if frame will be redrawn soon. */
24598 if (FRAME_GARBAGED_P (f))
24599 {
24600 TRACE ((stderr, " garbaged\n"));
24601 return;
24602 }
24603
24604 /* If basic faces haven't been realized yet, there is no point in
24605 trying to redraw anything. This can happen when we get an expose
24606 event while Emacs is starting, e.g. by moving another window. */
24607 if (FRAME_FACE_CACHE (f) == NULL
24608 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24609 {
24610 TRACE ((stderr, " no faces\n"));
24611 return;
24612 }
24613
24614 if (w == 0 || h == 0)
24615 {
24616 r.x = r.y = 0;
24617 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24618 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24619 }
24620 else
24621 {
24622 r.x = x;
24623 r.y = y;
24624 r.width = w;
24625 r.height = h;
24626 }
24627
24628 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24629 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24630
24631 if (WINDOWP (f->tool_bar_window))
24632 mouse_face_overwritten_p
24633 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24634
24635 #ifdef HAVE_X_WINDOWS
24636 #ifndef MSDOS
24637 #ifndef USE_X_TOOLKIT
24638 if (WINDOWP (f->menu_bar_window))
24639 mouse_face_overwritten_p
24640 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24641 #endif /* not USE_X_TOOLKIT */
24642 #endif
24643 #endif
24644
24645 /* Some window managers support a focus-follows-mouse style with
24646 delayed raising of frames. Imagine a partially obscured frame,
24647 and moving the mouse into partially obscured mouse-face on that
24648 frame. The visible part of the mouse-face will be highlighted,
24649 then the WM raises the obscured frame. With at least one WM, KDE
24650 2.1, Emacs is not getting any event for the raising of the frame
24651 (even tried with SubstructureRedirectMask), only Expose events.
24652 These expose events will draw text normally, i.e. not
24653 highlighted. Which means we must redo the highlight here.
24654 Subsume it under ``we love X''. --gerd 2001-08-15 */
24655 /* Included in Windows version because Windows most likely does not
24656 do the right thing if any third party tool offers
24657 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24658 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24659 {
24660 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24661 if (f == dpyinfo->mouse_face_mouse_frame)
24662 {
24663 int x = dpyinfo->mouse_face_mouse_x;
24664 int y = dpyinfo->mouse_face_mouse_y;
24665 clear_mouse_face (dpyinfo);
24666 note_mouse_highlight (f, x, y);
24667 }
24668 }
24669 }
24670
24671
24672 /* EXPORT:
24673 Determine the intersection of two rectangles R1 and R2. Return
24674 the intersection in *RESULT. Value is non-zero if RESULT is not
24675 empty. */
24676
24677 int
24678 x_intersect_rectangles (r1, r2, result)
24679 XRectangle *r1, *r2, *result;
24680 {
24681 XRectangle *left, *right;
24682 XRectangle *upper, *lower;
24683 int intersection_p = 0;
24684
24685 /* Rearrange so that R1 is the left-most rectangle. */
24686 if (r1->x < r2->x)
24687 left = r1, right = r2;
24688 else
24689 left = r2, right = r1;
24690
24691 /* X0 of the intersection is right.x0, if this is inside R1,
24692 otherwise there is no intersection. */
24693 if (right->x <= left->x + left->width)
24694 {
24695 result->x = right->x;
24696
24697 /* The right end of the intersection is the minimum of the
24698 the right ends of left and right. */
24699 result->width = (min (left->x + left->width, right->x + right->width)
24700 - result->x);
24701
24702 /* Same game for Y. */
24703 if (r1->y < r2->y)
24704 upper = r1, lower = r2;
24705 else
24706 upper = r2, lower = r1;
24707
24708 /* The upper end of the intersection is lower.y0, if this is inside
24709 of upper. Otherwise, there is no intersection. */
24710 if (lower->y <= upper->y + upper->height)
24711 {
24712 result->y = lower->y;
24713
24714 /* The lower end of the intersection is the minimum of the lower
24715 ends of upper and lower. */
24716 result->height = (min (lower->y + lower->height,
24717 upper->y + upper->height)
24718 - result->y);
24719 intersection_p = 1;
24720 }
24721 }
24722
24723 return intersection_p;
24724 }
24725
24726 #endif /* HAVE_WINDOW_SYSTEM */
24727
24728 \f
24729 /***********************************************************************
24730 Initialization
24731 ***********************************************************************/
24732
24733 void
24734 syms_of_xdisp ()
24735 {
24736 Vwith_echo_area_save_vector = Qnil;
24737 staticpro (&Vwith_echo_area_save_vector);
24738
24739 Vmessage_stack = Qnil;
24740 staticpro (&Vmessage_stack);
24741
24742 Qinhibit_redisplay = intern ("inhibit-redisplay");
24743 staticpro (&Qinhibit_redisplay);
24744
24745 message_dolog_marker1 = Fmake_marker ();
24746 staticpro (&message_dolog_marker1);
24747 message_dolog_marker2 = Fmake_marker ();
24748 staticpro (&message_dolog_marker2);
24749 message_dolog_marker3 = Fmake_marker ();
24750 staticpro (&message_dolog_marker3);
24751
24752 #if GLYPH_DEBUG
24753 defsubr (&Sdump_frame_glyph_matrix);
24754 defsubr (&Sdump_glyph_matrix);
24755 defsubr (&Sdump_glyph_row);
24756 defsubr (&Sdump_tool_bar_row);
24757 defsubr (&Strace_redisplay);
24758 defsubr (&Strace_to_stderr);
24759 #endif
24760 #ifdef HAVE_WINDOW_SYSTEM
24761 defsubr (&Stool_bar_lines_needed);
24762 defsubr (&Slookup_image_map);
24763 #endif
24764 defsubr (&Sformat_mode_line);
24765 defsubr (&Sinvisible_p);
24766
24767 staticpro (&Qmenu_bar_update_hook);
24768 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24769
24770 staticpro (&Qoverriding_terminal_local_map);
24771 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24772
24773 staticpro (&Qoverriding_local_map);
24774 Qoverriding_local_map = intern ("overriding-local-map");
24775
24776 staticpro (&Qwindow_scroll_functions);
24777 Qwindow_scroll_functions = intern ("window-scroll-functions");
24778
24779 staticpro (&Qwindow_text_change_functions);
24780 Qwindow_text_change_functions = intern ("window-text-change-functions");
24781
24782 staticpro (&Qredisplay_end_trigger_functions);
24783 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24784
24785 staticpro (&Qinhibit_point_motion_hooks);
24786 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24787
24788 Qeval = intern ("eval");
24789 staticpro (&Qeval);
24790
24791 QCdata = intern (":data");
24792 staticpro (&QCdata);
24793 Qdisplay = intern ("display");
24794 staticpro (&Qdisplay);
24795 Qspace_width = intern ("space-width");
24796 staticpro (&Qspace_width);
24797 Qraise = intern ("raise");
24798 staticpro (&Qraise);
24799 Qslice = intern ("slice");
24800 staticpro (&Qslice);
24801 Qspace = intern ("space");
24802 staticpro (&Qspace);
24803 Qmargin = intern ("margin");
24804 staticpro (&Qmargin);
24805 Qpointer = intern ("pointer");
24806 staticpro (&Qpointer);
24807 Qleft_margin = intern ("left-margin");
24808 staticpro (&Qleft_margin);
24809 Qright_margin = intern ("right-margin");
24810 staticpro (&Qright_margin);
24811 Qcenter = intern ("center");
24812 staticpro (&Qcenter);
24813 Qline_height = intern ("line-height");
24814 staticpro (&Qline_height);
24815 QCalign_to = intern (":align-to");
24816 staticpro (&QCalign_to);
24817 QCrelative_width = intern (":relative-width");
24818 staticpro (&QCrelative_width);
24819 QCrelative_height = intern (":relative-height");
24820 staticpro (&QCrelative_height);
24821 QCeval = intern (":eval");
24822 staticpro (&QCeval);
24823 QCpropertize = intern (":propertize");
24824 staticpro (&QCpropertize);
24825 QCfile = intern (":file");
24826 staticpro (&QCfile);
24827 Qfontified = intern ("fontified");
24828 staticpro (&Qfontified);
24829 Qfontification_functions = intern ("fontification-functions");
24830 staticpro (&Qfontification_functions);
24831 Qtrailing_whitespace = intern ("trailing-whitespace");
24832 staticpro (&Qtrailing_whitespace);
24833 Qescape_glyph = intern ("escape-glyph");
24834 staticpro (&Qescape_glyph);
24835 Qnobreak_space = intern ("nobreak-space");
24836 staticpro (&Qnobreak_space);
24837 Qimage = intern ("image");
24838 staticpro (&Qimage);
24839 QCmap = intern (":map");
24840 staticpro (&QCmap);
24841 QCpointer = intern (":pointer");
24842 staticpro (&QCpointer);
24843 Qrect = intern ("rect");
24844 staticpro (&Qrect);
24845 Qcircle = intern ("circle");
24846 staticpro (&Qcircle);
24847 Qpoly = intern ("poly");
24848 staticpro (&Qpoly);
24849 Qmessage_truncate_lines = intern ("message-truncate-lines");
24850 staticpro (&Qmessage_truncate_lines);
24851 Qgrow_only = intern ("grow-only");
24852 staticpro (&Qgrow_only);
24853 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24854 staticpro (&Qinhibit_menubar_update);
24855 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24856 staticpro (&Qinhibit_eval_during_redisplay);
24857 Qposition = intern ("position");
24858 staticpro (&Qposition);
24859 Qbuffer_position = intern ("buffer-position");
24860 staticpro (&Qbuffer_position);
24861 Qobject = intern ("object");
24862 staticpro (&Qobject);
24863 Qbar = intern ("bar");
24864 staticpro (&Qbar);
24865 Qhbar = intern ("hbar");
24866 staticpro (&Qhbar);
24867 Qbox = intern ("box");
24868 staticpro (&Qbox);
24869 Qhollow = intern ("hollow");
24870 staticpro (&Qhollow);
24871 Qhand = intern ("hand");
24872 staticpro (&Qhand);
24873 Qarrow = intern ("arrow");
24874 staticpro (&Qarrow);
24875 Qtext = intern ("text");
24876 staticpro (&Qtext);
24877 Qrisky_local_variable = intern ("risky-local-variable");
24878 staticpro (&Qrisky_local_variable);
24879 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24880 staticpro (&Qinhibit_free_realized_faces);
24881
24882 list_of_error = Fcons (Fcons (intern ("error"),
24883 Fcons (intern ("void-variable"), Qnil)),
24884 Qnil);
24885 staticpro (&list_of_error);
24886
24887 Qlast_arrow_position = intern ("last-arrow-position");
24888 staticpro (&Qlast_arrow_position);
24889 Qlast_arrow_string = intern ("last-arrow-string");
24890 staticpro (&Qlast_arrow_string);
24891
24892 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24893 staticpro (&Qoverlay_arrow_string);
24894 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24895 staticpro (&Qoverlay_arrow_bitmap);
24896
24897 echo_buffer[0] = echo_buffer[1] = Qnil;
24898 staticpro (&echo_buffer[0]);
24899 staticpro (&echo_buffer[1]);
24900
24901 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24902 staticpro (&echo_area_buffer[0]);
24903 staticpro (&echo_area_buffer[1]);
24904
24905 Vmessages_buffer_name = build_string ("*Messages*");
24906 staticpro (&Vmessages_buffer_name);
24907
24908 mode_line_proptrans_alist = Qnil;
24909 staticpro (&mode_line_proptrans_alist);
24910 mode_line_string_list = Qnil;
24911 staticpro (&mode_line_string_list);
24912 mode_line_string_face = Qnil;
24913 staticpro (&mode_line_string_face);
24914 mode_line_string_face_prop = Qnil;
24915 staticpro (&mode_line_string_face_prop);
24916 Vmode_line_unwind_vector = Qnil;
24917 staticpro (&Vmode_line_unwind_vector);
24918
24919 help_echo_string = Qnil;
24920 staticpro (&help_echo_string);
24921 help_echo_object = Qnil;
24922 staticpro (&help_echo_object);
24923 help_echo_window = Qnil;
24924 staticpro (&help_echo_window);
24925 previous_help_echo_string = Qnil;
24926 staticpro (&previous_help_echo_string);
24927 help_echo_pos = -1;
24928
24929 #ifdef HAVE_WINDOW_SYSTEM
24930 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24931 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24932 For example, if a block cursor is over a tab, it will be drawn as
24933 wide as that tab on the display. */);
24934 x_stretch_cursor_p = 0;
24935 #endif
24936
24937 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24938 doc: /* *Non-nil means highlight trailing whitespace.
24939 The face used for trailing whitespace is `trailing-whitespace'. */);
24940 Vshow_trailing_whitespace = Qnil;
24941
24942 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24943 doc: /* *Control highlighting of nobreak space and soft hyphen.
24944 A value of t means highlight the character itself (for nobreak space,
24945 use face `nobreak-space').
24946 A value of nil means no highlighting.
24947 Other values mean display the escape glyph followed by an ordinary
24948 space or ordinary hyphen. */);
24949 Vnobreak_char_display = Qt;
24950
24951 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24952 doc: /* *The pointer shape to show in void text areas.
24953 A value of nil means to show the text pointer. Other options are `arrow',
24954 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24955 Vvoid_text_area_pointer = Qarrow;
24956
24957 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24958 doc: /* Non-nil means don't actually do any redisplay.
24959 This is used for internal purposes. */);
24960 Vinhibit_redisplay = Qnil;
24961
24962 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24963 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24964 Vglobal_mode_string = Qnil;
24965
24966 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24967 doc: /* Marker for where to display an arrow on top of the buffer text.
24968 This must be the beginning of a line in order to work.
24969 See also `overlay-arrow-string'. */);
24970 Voverlay_arrow_position = Qnil;
24971
24972 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24973 doc: /* String to display as an arrow in non-window frames.
24974 See also `overlay-arrow-position'. */);
24975 Voverlay_arrow_string = build_string ("=>");
24976
24977 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24978 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24979 The symbols on this list are examined during redisplay to determine
24980 where to display overlay arrows. */);
24981 Voverlay_arrow_variable_list
24982 = Fcons (intern ("overlay-arrow-position"), Qnil);
24983
24984 DEFVAR_INT ("scroll-step", &scroll_step,
24985 doc: /* *The number of lines to try scrolling a window by when point moves out.
24986 If that fails to bring point back on frame, point is centered instead.
24987 If this is zero, point is always centered after it moves off frame.
24988 If you want scrolling to always be a line at a time, you should set
24989 `scroll-conservatively' to a large value rather than set this to 1. */);
24990
24991 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24992 doc: /* *Scroll up to this many lines, to bring point back on screen.
24993 If point moves off-screen, redisplay will scroll by up to
24994 `scroll-conservatively' lines in order to bring point just barely
24995 onto the screen again. If that cannot be done, then redisplay
24996 recenters point as usual.
24997
24998 A value of zero means always recenter point if it moves off screen. */);
24999 scroll_conservatively = 0;
25000
25001 DEFVAR_INT ("scroll-margin", &scroll_margin,
25002 doc: /* *Number of lines of margin at the top and bottom of a window.
25003 Recenter the window whenever point gets within this many lines
25004 of the top or bottom of the window. */);
25005 scroll_margin = 0;
25006
25007 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25008 doc: /* Pixels per inch value for non-window system displays.
25009 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25010 Vdisplay_pixels_per_inch = make_float (72.0);
25011
25012 #if GLYPH_DEBUG
25013 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25014 #endif
25015
25016 DEFVAR_LISP ("truncate-partial-width-windows",
25017 &Vtruncate_partial_width_windows,
25018 doc: /* Non-nil means truncate lines in windows with less than the frame width.
25019 For an integer value, truncate lines in each window with less than the
25020 full frame width, provided the window width is less than that integer;
25021 otherwise, respect the value of `truncate-lines'.
25022
25023 For any other non-nil value, truncate lines in all windows with
25024 less than the full frame width.
25025
25026 A value of nil means to respect the value of `truncate-lines'.
25027
25028 If `word-wrap' is enabled, you might want to reduce this. */);
25029 Vtruncate_partial_width_windows = make_number (50);
25030
25031 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25032 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25033 Any other value means to use the appropriate face, `mode-line',
25034 `header-line', or `menu' respectively. */);
25035 mode_line_inverse_video = 1;
25036
25037 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25038 doc: /* *Maximum buffer size for which line number should be displayed.
25039 If the buffer is bigger than this, the line number does not appear
25040 in the mode line. A value of nil means no limit. */);
25041 Vline_number_display_limit = Qnil;
25042
25043 DEFVAR_INT ("line-number-display-limit-width",
25044 &line_number_display_limit_width,
25045 doc: /* *Maximum line width (in characters) for line number display.
25046 If the average length of the lines near point is bigger than this, then the
25047 line number may be omitted from the mode line. */);
25048 line_number_display_limit_width = 200;
25049
25050 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25051 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25052 highlight_nonselected_windows = 0;
25053
25054 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25055 doc: /* Non-nil if more than one frame is visible on this display.
25056 Minibuffer-only frames don't count, but iconified frames do.
25057 This variable is not guaranteed to be accurate except while processing
25058 `frame-title-format' and `icon-title-format'. */);
25059
25060 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25061 doc: /* Template for displaying the title bar of visible frames.
25062 \(Assuming the window manager supports this feature.)
25063
25064 This variable has the same structure as `mode-line-format', except that
25065 the %c and %l constructs are ignored. It is used only on frames for
25066 which no explicit name has been set \(see `modify-frame-parameters'). */);
25067
25068 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25069 doc: /* Template for displaying the title bar of an iconified frame.
25070 \(Assuming the window manager supports this feature.)
25071 This variable has the same structure as `mode-line-format' (which see),
25072 and is used only on frames for which no explicit name has been set
25073 \(see `modify-frame-parameters'). */);
25074 Vicon_title_format
25075 = Vframe_title_format
25076 = Fcons (intern ("multiple-frames"),
25077 Fcons (build_string ("%b"),
25078 Fcons (Fcons (empty_unibyte_string,
25079 Fcons (intern ("invocation-name"),
25080 Fcons (build_string ("@"),
25081 Fcons (intern ("system-name"),
25082 Qnil)))),
25083 Qnil)));
25084
25085 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25086 doc: /* Maximum number of lines to keep in the message log buffer.
25087 If nil, disable message logging. If t, log messages but don't truncate
25088 the buffer when it becomes large. */);
25089 Vmessage_log_max = make_number (100);
25090
25091 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25092 doc: /* Functions called before redisplay, if window sizes have changed.
25093 The value should be a list of functions that take one argument.
25094 Just before redisplay, for each frame, if any of its windows have changed
25095 size since the last redisplay, or have been split or deleted,
25096 all the functions in the list are called, with the frame as argument. */);
25097 Vwindow_size_change_functions = Qnil;
25098
25099 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25100 doc: /* List of functions to call before redisplaying a window with scrolling.
25101 Each function is called with two arguments, the window and its new
25102 display-start position. Note that these functions are also called by
25103 `set-window-buffer'. Also note that the value of `window-end' is not
25104 valid when these functions are called. */);
25105 Vwindow_scroll_functions = Qnil;
25106
25107 DEFVAR_LISP ("window-text-change-functions",
25108 &Vwindow_text_change_functions,
25109 doc: /* Functions to call in redisplay when text in the window might change. */);
25110 Vwindow_text_change_functions = Qnil;
25111
25112 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25113 doc: /* Functions called when redisplay of a window reaches the end trigger.
25114 Each function is called with two arguments, the window and the end trigger value.
25115 See `set-window-redisplay-end-trigger'. */);
25116 Vredisplay_end_trigger_functions = Qnil;
25117
25118 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25119 doc: /* *Non-nil means autoselect window with mouse pointer.
25120 If nil, do not autoselect windows.
25121 A positive number means delay autoselection by that many seconds: a
25122 window is autoselected only after the mouse has remained in that
25123 window for the duration of the delay.
25124 A negative number has a similar effect, but causes windows to be
25125 autoselected only after the mouse has stopped moving. \(Because of
25126 the way Emacs compares mouse events, you will occasionally wait twice
25127 that time before the window gets selected.\)
25128 Any other value means to autoselect window instantaneously when the
25129 mouse pointer enters it.
25130
25131 Autoselection selects the minibuffer only if it is active, and never
25132 unselects the minibuffer if it is active.
25133
25134 When customizing this variable make sure that the actual value of
25135 `focus-follows-mouse' matches the behavior of your window manager. */);
25136 Vmouse_autoselect_window = Qnil;
25137
25138 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25139 doc: /* *Non-nil means automatically resize tool-bars.
25140 This dynamically changes the tool-bar's height to the minimum height
25141 that is needed to make all tool-bar items visible.
25142 If value is `grow-only', the tool-bar's height is only increased
25143 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25144 Vauto_resize_tool_bars = Qt;
25145
25146 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25147 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25148 auto_raise_tool_bar_buttons_p = 1;
25149
25150 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25151 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25152 make_cursor_line_fully_visible_p = 1;
25153
25154 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25155 doc: /* *Border below tool-bar in pixels.
25156 If an integer, use it as the height of the border.
25157 If it is one of `internal-border-width' or `border-width', use the
25158 value of the corresponding frame parameter.
25159 Otherwise, no border is added below the tool-bar. */);
25160 Vtool_bar_border = Qinternal_border_width;
25161
25162 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25163 doc: /* *Margin around tool-bar buttons in pixels.
25164 If an integer, use that for both horizontal and vertical margins.
25165 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25166 HORZ specifying the horizontal margin, and VERT specifying the
25167 vertical margin. */);
25168 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25169
25170 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25171 doc: /* *Relief thickness of tool-bar buttons. */);
25172 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25173
25174 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25175 doc: /* List of functions to call to fontify regions of text.
25176 Each function is called with one argument POS. Functions must
25177 fontify a region starting at POS in the current buffer, and give
25178 fontified regions the property `fontified'. */);
25179 Vfontification_functions = Qnil;
25180 Fmake_variable_buffer_local (Qfontification_functions);
25181
25182 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25183 &unibyte_display_via_language_environment,
25184 doc: /* *Non-nil means display unibyte text according to language environment.
25185 Specifically this means that unibyte non-ASCII characters
25186 are displayed by converting them to the equivalent multibyte characters
25187 according to the current language environment. As a result, they are
25188 displayed according to the current fontset. */);
25189 unibyte_display_via_language_environment = 0;
25190
25191 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25192 doc: /* *Maximum height for resizing mini-windows.
25193 If a float, it specifies a fraction of the mini-window frame's height.
25194 If an integer, it specifies a number of lines. */);
25195 Vmax_mini_window_height = make_float (0.25);
25196
25197 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25198 doc: /* *How to resize mini-windows.
25199 A value of nil means don't automatically resize mini-windows.
25200 A value of t means resize them to fit the text displayed in them.
25201 A value of `grow-only', the default, means let mini-windows grow
25202 only, until their display becomes empty, at which point the windows
25203 go back to their normal size. */);
25204 Vresize_mini_windows = Qgrow_only;
25205
25206 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25207 doc: /* Alist specifying how to blink the cursor off.
25208 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25209 `cursor-type' frame-parameter or variable equals ON-STATE,
25210 comparing using `equal', Emacs uses OFF-STATE to specify
25211 how to blink it off. ON-STATE and OFF-STATE are values for
25212 the `cursor-type' frame parameter.
25213
25214 If a frame's ON-STATE has no entry in this list,
25215 the frame's other specifications determine how to blink the cursor off. */);
25216 Vblink_cursor_alist = Qnil;
25217
25218 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25219 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25220 automatic_hscrolling_p = 1;
25221 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25222 staticpro (&Qauto_hscroll_mode);
25223
25224 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25225 doc: /* *How many columns away from the window edge point is allowed to get
25226 before automatic hscrolling will horizontally scroll the window. */);
25227 hscroll_margin = 5;
25228
25229 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25230 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25231 When point is less than `hscroll-margin' columns from the window
25232 edge, automatic hscrolling will scroll the window by the amount of columns
25233 determined by this variable. If its value is a positive integer, scroll that
25234 many columns. If it's a positive floating-point number, it specifies the
25235 fraction of the window's width to scroll. If it's nil or zero, point will be
25236 centered horizontally after the scroll. Any other value, including negative
25237 numbers, are treated as if the value were zero.
25238
25239 Automatic hscrolling always moves point outside the scroll margin, so if
25240 point was more than scroll step columns inside the margin, the window will
25241 scroll more than the value given by the scroll step.
25242
25243 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25244 and `scroll-right' overrides this variable's effect. */);
25245 Vhscroll_step = make_number (0);
25246
25247 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25248 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25249 Bind this around calls to `message' to let it take effect. */);
25250 message_truncate_lines = 0;
25251
25252 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25253 doc: /* Normal hook run to update the menu bar definitions.
25254 Redisplay runs this hook before it redisplays the menu bar.
25255 This is used to update submenus such as Buffers,
25256 whose contents depend on various data. */);
25257 Vmenu_bar_update_hook = Qnil;
25258
25259 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25260 doc: /* Frame for which we are updating a menu.
25261 The enable predicate for a menu binding should check this variable. */);
25262 Vmenu_updating_frame = Qnil;
25263
25264 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25265 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25266 inhibit_menubar_update = 0;
25267
25268 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25269 doc: /* Prefix added to the beginning of all continuation lines at display-time.
25270 May be a string, an image, or a stretch-glyph such as used by the
25271 `display' text-property.
25272
25273 This variable is overridden by any `wrap-prefix' text-property.
25274
25275 To add a prefix to non-continuation lines, use the `line-prefix' variable. */);
25276 Vwrap_prefix = Qnil;
25277 staticpro (&Qwrap_prefix);
25278 Qwrap_prefix = intern ("wrap-prefix");
25279 Fmake_variable_buffer_local (Qwrap_prefix);
25280
25281 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25282 doc: /* Prefix added to the beginning of all non-continuation lines at display-time.
25283 May be a string, an image, or a stretch-glyph such as used by the
25284 `display' text-property.
25285
25286 This variable is overridden by any `line-prefix' text-property.
25287
25288 To add a prefix to continuation lines, use the `wrap-prefix' variable. */);
25289 Vline_prefix = Qnil;
25290 staticpro (&Qline_prefix);
25291 Qline_prefix = intern ("line-prefix");
25292 Fmake_variable_buffer_local (Qline_prefix);
25293
25294 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25295 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25296 inhibit_eval_during_redisplay = 0;
25297
25298 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25299 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25300 inhibit_free_realized_faces = 0;
25301
25302 #if GLYPH_DEBUG
25303 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25304 doc: /* Inhibit try_window_id display optimization. */);
25305 inhibit_try_window_id = 0;
25306
25307 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25308 doc: /* Inhibit try_window_reusing display optimization. */);
25309 inhibit_try_window_reusing = 0;
25310
25311 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25312 doc: /* Inhibit try_cursor_movement display optimization. */);
25313 inhibit_try_cursor_movement = 0;
25314 #endif /* GLYPH_DEBUG */
25315
25316 DEFVAR_INT ("overline-margin", &overline_margin,
25317 doc: /* *Space between overline and text, in pixels.
25318 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25319 margin to the caracter height. */);
25320 overline_margin = 2;
25321
25322 DEFVAR_INT ("underline-minimum-offset",
25323 &underline_minimum_offset,
25324 doc: /* Minimum distance between baseline and underline.
25325 This can improve legibility of underlined text at small font sizes,
25326 particularly when using variable `x-use-underline-position-properties'
25327 with fonts that specify an UNDERLINE_POSITION relatively close to the
25328 baseline. The default value is 1. */);
25329 underline_minimum_offset = 1;
25330
25331 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25332 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25333 display_hourglass_p = 1;
25334
25335 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25336 doc: /* *Seconds to wait before displaying an hourglass pointer.
25337 Value must be an integer or float. */);
25338 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25339
25340 hourglass_atimer = NULL;
25341 hourglass_shown_p = 0;
25342 }
25343
25344
25345 /* Initialize this module when Emacs starts. */
25346
25347 void
25348 init_xdisp ()
25349 {
25350 Lisp_Object root_window;
25351 struct window *mini_w;
25352
25353 current_header_line_height = current_mode_line_height = -1;
25354
25355 CHARPOS (this_line_start_pos) = 0;
25356
25357 mini_w = XWINDOW (minibuf_window);
25358 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25359
25360 if (!noninteractive)
25361 {
25362 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25363 int i;
25364
25365 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25366 set_window_height (root_window,
25367 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25368 0);
25369 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25370 set_window_height (minibuf_window, 1, 0);
25371
25372 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25373 mini_w->total_cols = make_number (FRAME_COLS (f));
25374
25375 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25376 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25377 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25378
25379 /* The default ellipsis glyphs `...'. */
25380 for (i = 0; i < 3; ++i)
25381 default_invis_vector[i] = make_number ('.');
25382 }
25383
25384 {
25385 /* Allocate the buffer for frame titles.
25386 Also used for `format-mode-line'. */
25387 int size = 100;
25388 mode_line_noprop_buf = (char *) xmalloc (size);
25389 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25390 mode_line_noprop_ptr = mode_line_noprop_buf;
25391 mode_line_target = MODE_LINE_DISPLAY;
25392 }
25393
25394 help_echo_showing_p = 0;
25395 }
25396
25397 /* Since w32 does not support atimers, it defines its own implementation of
25398 the following three functions in w32fns.c. */
25399 #ifndef WINDOWSNT
25400
25401 /* Platform-independent portion of hourglass implementation. */
25402
25403 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25404 int
25405 hourglass_started ()
25406 {
25407 return hourglass_shown_p || hourglass_atimer != NULL;
25408 }
25409
25410 /* Cancel a currently active hourglass timer, and start a new one. */
25411 void
25412 start_hourglass ()
25413 {
25414 #if defined (HAVE_WINDOW_SYSTEM)
25415 EMACS_TIME delay;
25416 int secs, usecs = 0;
25417
25418 cancel_hourglass ();
25419
25420 if (INTEGERP (Vhourglass_delay)
25421 && XINT (Vhourglass_delay) > 0)
25422 secs = XFASTINT (Vhourglass_delay);
25423 else if (FLOATP (Vhourglass_delay)
25424 && XFLOAT_DATA (Vhourglass_delay) > 0)
25425 {
25426 Lisp_Object tem;
25427 tem = Ftruncate (Vhourglass_delay, Qnil);
25428 secs = XFASTINT (tem);
25429 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25430 }
25431 else
25432 secs = DEFAULT_HOURGLASS_DELAY;
25433
25434 EMACS_SET_SECS_USECS (delay, secs, usecs);
25435 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25436 show_hourglass, NULL);
25437 #endif
25438 }
25439
25440
25441 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25442 shown. */
25443 void
25444 cancel_hourglass ()
25445 {
25446 #if defined (HAVE_WINDOW_SYSTEM)
25447 if (hourglass_atimer)
25448 {
25449 cancel_atimer (hourglass_atimer);
25450 hourglass_atimer = NULL;
25451 }
25452
25453 if (hourglass_shown_p)
25454 hide_hourglass ();
25455 #endif
25456 }
25457 #endif /* ! WINDOWSNT */
25458
25459 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25460 (do not change this comment) */