]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(main): Sort scores before trimming them,
[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 extern Lisp_Object Qbefore_string, Qafter_string;
239
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
243 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
244 Lisp_Object Qinhibit_point_motion_hooks;
245 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
246 Lisp_Object Qfontified;
247 Lisp_Object Qgrow_only;
248 Lisp_Object Qinhibit_eval_during_redisplay;
249 Lisp_Object Qbuffer_position, Qposition, Qobject;
250
251 /* Cursor shapes */
252 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
253
254 /* Pointer shapes */
255 Lisp_Object Qarrow, Qhand, Qtext;
256
257 Lisp_Object Qrisky_local_variable;
258
259 /* Holds the list (error). */
260 Lisp_Object list_of_error;
261
262 /* Functions called to fontify regions of text. */
263
264 Lisp_Object Vfontification_functions;
265 Lisp_Object Qfontification_functions;
266
267 /* Non-nil means automatically select any window when the mouse
268 cursor moves into it. */
269 Lisp_Object Vmouse_autoselect_window;
270
271 Lisp_Object Vwrap_prefix, Qwrap_prefix;
272 Lisp_Object Vline_prefix, Qline_prefix;
273
274 /* Non-zero means draw tool bar buttons raised when the mouse moves
275 over them. */
276
277 int auto_raise_tool_bar_buttons_p;
278
279 /* Non-zero means to reposition window if cursor line is only partially visible. */
280
281 int make_cursor_line_fully_visible_p;
282
283 /* Margin below tool bar in pixels. 0 or nil means no margin.
284 If value is `internal-border-width' or `border-width',
285 the corresponding frame parameter is used. */
286
287 Lisp_Object Vtool_bar_border;
288
289 /* Margin around tool bar buttons in pixels. */
290
291 Lisp_Object Vtool_bar_button_margin;
292
293 /* Thickness of shadow to draw around tool bar buttons. */
294
295 EMACS_INT tool_bar_button_relief;
296
297 /* Non-nil means automatically resize tool-bars so that all tool-bar
298 items are visible, and no blank lines remain.
299
300 If value is `grow-only', only make tool-bar bigger. */
301
302 Lisp_Object Vauto_resize_tool_bars;
303
304 /* Non-zero means draw block and hollow cursor as wide as the glyph
305 under it. For example, if a block cursor is over a tab, it will be
306 drawn as wide as that tab on the display. */
307
308 int x_stretch_cursor_p;
309
310 /* Non-nil means don't actually do any redisplay. */
311
312 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
313
314 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
315
316 int inhibit_eval_during_redisplay;
317
318 /* Names of text properties relevant for redisplay. */
319
320 Lisp_Object Qdisplay;
321 extern Lisp_Object Qface, Qinvisible, Qwidth;
322
323 /* Symbols used in text property values. */
324
325 Lisp_Object Vdisplay_pixels_per_inch;
326 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
327 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
328 Lisp_Object Qslice;
329 Lisp_Object Qcenter;
330 Lisp_Object Qmargin, Qpointer;
331 Lisp_Object Qline_height;
332 extern Lisp_Object Qheight;
333 extern Lisp_Object QCwidth, QCheight, QCascent;
334 extern Lisp_Object Qscroll_bar;
335 extern Lisp_Object Qcursor;
336
337 /* Non-nil means highlight trailing whitespace. */
338
339 Lisp_Object Vshow_trailing_whitespace;
340
341 /* Non-nil means escape non-break space and hyphens. */
342
343 Lisp_Object Vnobreak_char_display;
344
345 #ifdef HAVE_WINDOW_SYSTEM
346 extern Lisp_Object Voverflow_newline_into_fringe;
347
348 /* Test if overflow newline into fringe. Called with iterator IT
349 at or past right window margin, and with IT->current_x set. */
350
351 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
352 (!NILP (Voverflow_newline_into_fringe) \
353 && FRAME_WINDOW_P (it->f) \
354 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
355 && it->current_x == it->last_visible_x \
356 && it->line_wrap != WORD_WRAP)
357
358 #else /* !HAVE_WINDOW_SYSTEM */
359 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
360 #endif /* HAVE_WINDOW_SYSTEM */
361
362 /* Test if the display element loaded in IT is a space or tab
363 character. This is used to determine word wrapping. */
364
365 #define IT_DISPLAYING_WHITESPACE(it) \
366 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
367
368 /* Non-nil means show the text cursor in void text areas
369 i.e. in blank areas after eol and eob. This used to be
370 the default in 21.3. */
371
372 Lisp_Object Vvoid_text_area_pointer;
373
374 /* Name of the face used to highlight trailing whitespace. */
375
376 Lisp_Object Qtrailing_whitespace;
377
378 /* Name and number of the face used to highlight escape glyphs. */
379
380 Lisp_Object Qescape_glyph;
381
382 /* Name and number of the face used to highlight non-breaking spaces. */
383
384 Lisp_Object Qnobreak_space;
385
386 /* The symbol `image' which is the car of the lists used to represent
387 images in Lisp. */
388
389 Lisp_Object Qimage;
390
391 /* The image map types. */
392 Lisp_Object QCmap, QCpointer;
393 Lisp_Object Qrect, Qcircle, Qpoly;
394
395 /* Non-zero means print newline to stdout before next mini-buffer
396 message. */
397
398 int noninteractive_need_newline;
399
400 /* Non-zero means print newline to message log before next message. */
401
402 static int message_log_need_newline;
403
404 /* Three markers that message_dolog uses.
405 It could allocate them itself, but that causes trouble
406 in handling memory-full errors. */
407 static Lisp_Object message_dolog_marker1;
408 static Lisp_Object message_dolog_marker2;
409 static Lisp_Object message_dolog_marker3;
410 \f
411 /* The buffer position of the first character appearing entirely or
412 partially on the line of the selected window which contains the
413 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
414 redisplay optimization in redisplay_internal. */
415
416 static struct text_pos this_line_start_pos;
417
418 /* Number of characters past the end of the line above, including the
419 terminating newline. */
420
421 static struct text_pos this_line_end_pos;
422
423 /* The vertical positions and the height of this line. */
424
425 static int this_line_vpos;
426 static int this_line_y;
427 static int this_line_pixel_height;
428
429 /* X position at which this display line starts. Usually zero;
430 negative if first character is partially visible. */
431
432 static int this_line_start_x;
433
434 /* Buffer that this_line_.* variables are referring to. */
435
436 static struct buffer *this_line_buffer;
437
438 /* Nonzero means truncate lines in all windows less wide than the
439 frame. */
440
441 Lisp_Object Vtruncate_partial_width_windows;
442
443 /* A flag to control how to display unibyte 8-bit character. */
444
445 int unibyte_display_via_language_environment;
446
447 /* Nonzero means we have more than one non-mini-buffer-only frame.
448 Not guaranteed to be accurate except while parsing
449 frame-title-format. */
450
451 int multiple_frames;
452
453 Lisp_Object Vglobal_mode_string;
454
455
456 /* List of variables (symbols) which hold markers for overlay arrows.
457 The symbols on this list are examined during redisplay to determine
458 where to display overlay arrows. */
459
460 Lisp_Object Voverlay_arrow_variable_list;
461
462 /* Marker for where to display an arrow on top of the buffer text. */
463
464 Lisp_Object Voverlay_arrow_position;
465
466 /* String to display for the arrow. Only used on terminal frames. */
467
468 Lisp_Object Voverlay_arrow_string;
469
470 /* Values of those variables at last redisplay are stored as
471 properties on `overlay-arrow-position' symbol. However, if
472 Voverlay_arrow_position is a marker, last-arrow-position is its
473 numerical position. */
474
475 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
476
477 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
478 properties on a symbol in overlay-arrow-variable-list. */
479
480 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
481
482 /* Like mode-line-format, but for the title bar on a visible frame. */
483
484 Lisp_Object Vframe_title_format;
485
486 /* Like mode-line-format, but for the title bar on an iconified frame. */
487
488 Lisp_Object Vicon_title_format;
489
490 /* List of functions to call when a window's size changes. These
491 functions get one arg, a frame on which one or more windows' sizes
492 have changed. */
493
494 static Lisp_Object Vwindow_size_change_functions;
495
496 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
497
498 /* Nonzero if an overlay arrow has been displayed in this window. */
499
500 static int overlay_arrow_seen;
501
502 /* Nonzero means highlight the region even in nonselected windows. */
503
504 int highlight_nonselected_windows;
505
506 /* If cursor motion alone moves point off frame, try scrolling this
507 many lines up or down if that will bring it back. */
508
509 static EMACS_INT scroll_step;
510
511 /* Nonzero means scroll just far enough to bring point back on the
512 screen, when appropriate. */
513
514 static EMACS_INT scroll_conservatively;
515
516 /* Recenter the window whenever point gets within this many lines of
517 the top or bottom of the window. This value is translated into a
518 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
519 that there is really a fixed pixel height scroll margin. */
520
521 EMACS_INT scroll_margin;
522
523 /* Number of windows showing the buffer of the selected window (or
524 another buffer with the same base buffer). keyboard.c refers to
525 this. */
526
527 int buffer_shared;
528
529 /* Vector containing glyphs for an ellipsis `...'. */
530
531 static Lisp_Object default_invis_vector[3];
532
533 /* Zero means display the mode-line/header-line/menu-bar in the default face
534 (this slightly odd definition is for compatibility with previous versions
535 of emacs), non-zero means display them using their respective faces.
536
537 This variable is deprecated. */
538
539 int mode_line_inverse_video;
540
541 /* Prompt to display in front of the mini-buffer contents. */
542
543 Lisp_Object minibuf_prompt;
544
545 /* Width of current mini-buffer prompt. Only set after display_line
546 of the line that contains the prompt. */
547
548 int minibuf_prompt_width;
549
550 /* This is the window where the echo area message was displayed. It
551 is always a mini-buffer window, but it may not be the same window
552 currently active as a mini-buffer. */
553
554 Lisp_Object echo_area_window;
555
556 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
557 pushes the current message and the value of
558 message_enable_multibyte on the stack, the function restore_message
559 pops the stack and displays MESSAGE again. */
560
561 Lisp_Object Vmessage_stack;
562
563 /* Nonzero means multibyte characters were enabled when the echo area
564 message was specified. */
565
566 int message_enable_multibyte;
567
568 /* Nonzero if we should redraw the mode lines on the next redisplay. */
569
570 int update_mode_lines;
571
572 /* Nonzero if window sizes or contents have changed since last
573 redisplay that finished. */
574
575 int windows_or_buffers_changed;
576
577 /* Nonzero means a frame's cursor type has been changed. */
578
579 int cursor_type_changed;
580
581 /* Nonzero after display_mode_line if %l was used and it displayed a
582 line number. */
583
584 int line_number_displayed;
585
586 /* Maximum buffer size for which to display line numbers. */
587
588 Lisp_Object Vline_number_display_limit;
589
590 /* Line width to consider when repositioning for line number display. */
591
592 static EMACS_INT line_number_display_limit_width;
593
594 /* Number of lines to keep in the message log buffer. t means
595 infinite. nil means don't log at all. */
596
597 Lisp_Object Vmessage_log_max;
598
599 /* The name of the *Messages* buffer, a string. */
600
601 static Lisp_Object Vmessages_buffer_name;
602
603 /* Current, index 0, and last displayed echo area message. Either
604 buffers from echo_buffers, or nil to indicate no message. */
605
606 Lisp_Object echo_area_buffer[2];
607
608 /* The buffers referenced from echo_area_buffer. */
609
610 static Lisp_Object echo_buffer[2];
611
612 /* A vector saved used in with_area_buffer to reduce consing. */
613
614 static Lisp_Object Vwith_echo_area_save_vector;
615
616 /* Non-zero means display_echo_area should display the last echo area
617 message again. Set by redisplay_preserve_echo_area. */
618
619 static int display_last_displayed_message_p;
620
621 /* Nonzero if echo area is being used by print; zero if being used by
622 message. */
623
624 int message_buf_print;
625
626 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
627
628 Lisp_Object Qinhibit_menubar_update;
629 int inhibit_menubar_update;
630
631 /* When evaluating expressions from menu bar items (enable conditions,
632 for instance), this is the frame they are being processed for. */
633
634 Lisp_Object Vmenu_updating_frame;
635
636 /* Maximum height for resizing mini-windows. Either a float
637 specifying a fraction of the available height, or an integer
638 specifying a number of lines. */
639
640 Lisp_Object Vmax_mini_window_height;
641
642 /* Non-zero means messages should be displayed with truncated
643 lines instead of being continued. */
644
645 int message_truncate_lines;
646 Lisp_Object Qmessage_truncate_lines;
647
648 /* Set to 1 in clear_message to make redisplay_internal aware
649 of an emptied echo area. */
650
651 static int message_cleared_p;
652
653 /* How to blink the default frame cursor off. */
654 Lisp_Object Vblink_cursor_alist;
655
656 /* A scratch glyph row with contents used for generating truncation
657 glyphs. Also used in direct_output_for_insert. */
658
659 #define MAX_SCRATCH_GLYPHS 100
660 struct glyph_row scratch_glyph_row;
661 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
662
663 /* Ascent and height of the last line processed by move_it_to. */
664
665 static int last_max_ascent, last_height;
666
667 /* Non-zero if there's a help-echo in the echo area. */
668
669 int help_echo_showing_p;
670
671 /* If >= 0, computed, exact values of mode-line and header-line height
672 to use in the macros CURRENT_MODE_LINE_HEIGHT and
673 CURRENT_HEADER_LINE_HEIGHT. */
674
675 int current_mode_line_height, current_header_line_height;
676
677 /* The maximum distance to look ahead for text properties. Values
678 that are too small let us call compute_char_face and similar
679 functions too often which is expensive. Values that are too large
680 let us call compute_char_face and alike too often because we
681 might not be interested in text properties that far away. */
682
683 #define TEXT_PROP_DISTANCE_LIMIT 100
684
685 #if GLYPH_DEBUG
686
687 /* Variables to turn off display optimizations from Lisp. */
688
689 int inhibit_try_window_id, inhibit_try_window_reusing;
690 int inhibit_try_cursor_movement;
691
692 /* Non-zero means print traces of redisplay if compiled with
693 GLYPH_DEBUG != 0. */
694
695 int trace_redisplay_p;
696
697 #endif /* GLYPH_DEBUG */
698
699 #ifdef DEBUG_TRACE_MOVE
700 /* Non-zero means trace with TRACE_MOVE to stderr. */
701 int trace_move;
702
703 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
704 #else
705 #define TRACE_MOVE(x) (void) 0
706 #endif
707
708 /* Non-zero means automatically scroll windows horizontally to make
709 point visible. */
710
711 int automatic_hscrolling_p;
712 Lisp_Object Qauto_hscroll_mode;
713
714 /* How close to the margin can point get before the window is scrolled
715 horizontally. */
716 EMACS_INT hscroll_margin;
717
718 /* How much to scroll horizontally when point is inside the above margin. */
719 Lisp_Object Vhscroll_step;
720
721 /* The variable `resize-mini-windows'. If nil, don't resize
722 mini-windows. If t, always resize them to fit the text they
723 display. If `grow-only', let mini-windows grow only until they
724 become empty. */
725
726 Lisp_Object Vresize_mini_windows;
727
728 /* Buffer being redisplayed -- for redisplay_window_error. */
729
730 struct buffer *displayed_buffer;
731
732 /* Space between overline and text. */
733
734 EMACS_INT overline_margin;
735
736 /* Require underline to be at least this many screen pixels below baseline
737 This to avoid underline "merging" with the base of letters at small
738 font sizes, particularly when x_use_underline_position_properties is on. */
739
740 EMACS_INT underline_minimum_offset;
741
742 /* Value returned from text property handlers (see below). */
743
744 enum prop_handled
745 {
746 HANDLED_NORMALLY,
747 HANDLED_RECOMPUTE_PROPS,
748 HANDLED_OVERLAY_STRING_CONSUMED,
749 HANDLED_RETURN
750 };
751
752 /* A description of text properties that redisplay is interested
753 in. */
754
755 struct props
756 {
757 /* The name of the property. */
758 Lisp_Object *name;
759
760 /* A unique index for the property. */
761 enum prop_idx idx;
762
763 /* A handler function called to set up iterator IT from the property
764 at IT's current position. Value is used to steer handle_stop. */
765 enum prop_handled (*handler) P_ ((struct it *it));
766 };
767
768 static enum prop_handled handle_face_prop P_ ((struct it *));
769 static enum prop_handled handle_invisible_prop P_ ((struct it *));
770 static enum prop_handled handle_display_prop P_ ((struct it *));
771 static enum prop_handled handle_composition_prop P_ ((struct it *));
772 static enum prop_handled handle_overlay_change P_ ((struct it *));
773 static enum prop_handled handle_fontified_prop P_ ((struct it *));
774
775 /* Properties handled by iterators. */
776
777 static struct props it_props[] =
778 {
779 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
780 /* Handle `face' before `display' because some sub-properties of
781 `display' need to know the face. */
782 {&Qface, FACE_PROP_IDX, handle_face_prop},
783 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
784 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
785 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
786 {NULL, 0, NULL}
787 };
788
789 /* Value is the position described by X. If X is a marker, value is
790 the marker_position of X. Otherwise, value is X. */
791
792 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
793
794 /* Enumeration returned by some move_it_.* functions internally. */
795
796 enum move_it_result
797 {
798 /* Not used. Undefined value. */
799 MOVE_UNDEFINED,
800
801 /* Move ended at the requested buffer position or ZV. */
802 MOVE_POS_MATCH_OR_ZV,
803
804 /* Move ended at the requested X pixel position. */
805 MOVE_X_REACHED,
806
807 /* Move within a line ended at the end of a line that must be
808 continued. */
809 MOVE_LINE_CONTINUED,
810
811 /* Move within a line ended at the end of a line that would
812 be displayed truncated. */
813 MOVE_LINE_TRUNCATED,
814
815 /* Move within a line ended at a line end. */
816 MOVE_NEWLINE_OR_CR
817 };
818
819 /* This counter is used to clear the face cache every once in a while
820 in redisplay_internal. It is incremented for each redisplay.
821 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
822 cleared. */
823
824 #define CLEAR_FACE_CACHE_COUNT 500
825 static int clear_face_cache_count;
826
827 /* Similarly for the image cache. */
828
829 #ifdef HAVE_WINDOW_SYSTEM
830 #define CLEAR_IMAGE_CACHE_COUNT 101
831 static int clear_image_cache_count;
832 #endif
833
834 /* Non-zero while redisplay_internal is in progress. */
835
836 int redisplaying_p;
837
838 /* Non-zero means don't free realized faces. Bound while freeing
839 realized faces is dangerous because glyph matrices might still
840 reference them. */
841
842 int inhibit_free_realized_faces;
843 Lisp_Object Qinhibit_free_realized_faces;
844
845 /* If a string, XTread_socket generates an event to display that string.
846 (The display is done in read_char.) */
847
848 Lisp_Object help_echo_string;
849 Lisp_Object help_echo_window;
850 Lisp_Object help_echo_object;
851 int help_echo_pos;
852
853 /* Temporary variable for XTread_socket. */
854
855 Lisp_Object previous_help_echo_string;
856
857 /* Null glyph slice */
858
859 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
860
861 /* Platform-independent portion of hourglass implementation. */
862
863 /* Non-zero means we're allowed to display a hourglass pointer. */
864 int display_hourglass_p;
865
866 /* Non-zero means an hourglass cursor is currently shown. */
867 int hourglass_shown_p;
868
869 /* If non-null, an asynchronous timer that, when it expires, displays
870 an hourglass cursor on all frames. */
871 struct atimer *hourglass_atimer;
872
873 /* Number of seconds to wait before displaying an hourglass cursor. */
874 Lisp_Object Vhourglass_delay;
875
876 /* Default number of seconds to wait before displaying an hourglass
877 cursor. */
878 #define DEFAULT_HOURGLASS_DELAY 1
879
880 \f
881 /* Function prototypes. */
882
883 static void setup_for_ellipsis P_ ((struct it *, int));
884 static void mark_window_display_accurate_1 P_ ((struct window *, int));
885 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
886 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
887 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
888 static int redisplay_mode_lines P_ ((Lisp_Object, int));
889 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
890
891 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
892
893 static void handle_line_prefix P_ ((struct it *));
894
895 static void pint2str P_ ((char *, int, int));
896 static void pint2hrstr P_ ((char *, int, int));
897 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
898 struct text_pos));
899 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
900 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
901 static void store_mode_line_noprop_char P_ ((char));
902 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
903 static void x_consider_frame_title P_ ((Lisp_Object));
904 static void handle_stop P_ ((struct it *));
905 static int tool_bar_lines_needed P_ ((struct frame *, int *));
906 static int single_display_spec_intangible_p P_ ((Lisp_Object));
907 static void ensure_echo_area_buffers P_ ((void));
908 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
909 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
910 static int with_echo_area_buffer P_ ((struct window *, int,
911 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
912 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
913 static void clear_garbaged_frames P_ ((void));
914 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
916 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int display_echo_area P_ ((struct window *));
918 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
920 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
921 static int string_char_and_length P_ ((const unsigned char *, int, int *));
922 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
923 struct text_pos));
924 static int compute_window_start_on_continuation_line P_ ((struct window *));
925 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
926 static void insert_left_trunc_glyphs P_ ((struct it *));
927 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
928 Lisp_Object));
929 static void extend_face_to_end_of_line P_ ((struct it *));
930 static int append_space_for_newline P_ ((struct it *, int));
931 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
932 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
933 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
934 static int trailing_whitespace_p P_ ((int));
935 static int message_log_check_duplicate P_ ((int, int, int, int));
936 static void push_it P_ ((struct it *));
937 static void pop_it P_ ((struct it *));
938 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
939 static void select_frame_for_redisplay P_ ((Lisp_Object));
940 static void redisplay_internal P_ ((int));
941 static int echo_area_display P_ ((int));
942 static void redisplay_windows P_ ((Lisp_Object));
943 static void redisplay_window P_ ((Lisp_Object, int));
944 static Lisp_Object redisplay_window_error ();
945 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
946 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
947 static int update_menu_bar P_ ((struct frame *, int, int));
948 static int try_window_reusing_current_matrix P_ ((struct window *));
949 static int try_window_id P_ ((struct window *));
950 static int display_line P_ ((struct it *));
951 static int display_mode_lines P_ ((struct window *));
952 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
953 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
954 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
955 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
956 static void display_menu_bar P_ ((struct window *));
957 static int display_count_lines P_ ((int, int, int, int, int *));
958 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
959 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
960 static void compute_line_metrics P_ ((struct it *));
961 static void run_redisplay_end_trigger_hook P_ ((struct it *));
962 static int get_overlay_strings P_ ((struct it *, int));
963 static int get_overlay_strings_1 P_ ((struct it *, int, int));
964 static void next_overlay_string P_ ((struct it *));
965 static void reseat P_ ((struct it *, struct text_pos, int));
966 static void reseat_1 P_ ((struct it *, struct text_pos, int));
967 static void back_to_previous_visible_line_start P_ ((struct it *));
968 void reseat_at_previous_visible_line_start P_ ((struct it *));
969 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
970 static int next_element_from_ellipsis P_ ((struct it *));
971 static int next_element_from_display_vector P_ ((struct it *));
972 static int next_element_from_string P_ ((struct it *));
973 static int next_element_from_c_string P_ ((struct it *));
974 static int next_element_from_buffer P_ ((struct it *));
975 static int next_element_from_composition P_ ((struct it *));
976 static int next_element_from_image P_ ((struct it *));
977 static int next_element_from_stretch P_ ((struct it *));
978 static void load_overlay_strings P_ ((struct it *, int));
979 static int init_from_display_pos P_ ((struct it *, struct window *,
980 struct display_pos *));
981 static void reseat_to_string P_ ((struct it *, unsigned char *,
982 Lisp_Object, int, int, int, int));
983 static enum move_it_result
984 move_it_in_display_line_to (struct it *, EMACS_INT, int,
985 enum move_operation_enum);
986 void move_it_vertically_backward P_ ((struct it *, int));
987 static void init_to_row_start P_ ((struct it *, struct window *,
988 struct glyph_row *));
989 static int init_to_row_end P_ ((struct it *, struct window *,
990 struct glyph_row *));
991 static void back_to_previous_line_start P_ ((struct it *));
992 static int forward_to_next_line_start P_ ((struct it *, int *));
993 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
994 Lisp_Object, int));
995 static struct text_pos string_pos P_ ((int, Lisp_Object));
996 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
997 static int number_of_chars P_ ((unsigned char *, int));
998 static void compute_stop_pos P_ ((struct it *));
999 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1000 Lisp_Object));
1001 static int face_before_or_after_it_pos P_ ((struct it *, int));
1002 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1003 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1004 Lisp_Object, Lisp_Object,
1005 struct text_pos *, int));
1006 static int underlying_face_id P_ ((struct it *));
1007 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1008 struct window *));
1009
1010 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1011 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1012
1013 #ifdef HAVE_WINDOW_SYSTEM
1014
1015 static void update_tool_bar P_ ((struct frame *, int));
1016 static void build_desired_tool_bar_string P_ ((struct frame *f));
1017 static int redisplay_tool_bar P_ ((struct frame *));
1018 static void display_tool_bar_line P_ ((struct it *, int));
1019 static void notice_overwritten_cursor P_ ((struct window *,
1020 enum glyph_row_area,
1021 int, int, int, int));
1022
1023
1024
1025 #endif /* HAVE_WINDOW_SYSTEM */
1026
1027 \f
1028 /***********************************************************************
1029 Window display dimensions
1030 ***********************************************************************/
1031
1032 /* Return the bottom boundary y-position for text lines in window W.
1033 This is the first y position at which a line cannot start.
1034 It is relative to the top of the window.
1035
1036 This is the height of W minus the height of a mode line, if any. */
1037
1038 INLINE int
1039 window_text_bottom_y (w)
1040 struct window *w;
1041 {
1042 int height = WINDOW_TOTAL_HEIGHT (w);
1043
1044 if (WINDOW_WANTS_MODELINE_P (w))
1045 height -= CURRENT_MODE_LINE_HEIGHT (w);
1046 return height;
1047 }
1048
1049 /* Return the pixel width of display area AREA of window W. AREA < 0
1050 means return the total width of W, not including fringes to
1051 the left and right of the window. */
1052
1053 INLINE int
1054 window_box_width (w, area)
1055 struct window *w;
1056 int area;
1057 {
1058 int cols = XFASTINT (w->total_cols);
1059 int pixels = 0;
1060
1061 if (!w->pseudo_window_p)
1062 {
1063 cols -= WINDOW_SCROLL_BAR_COLS (w);
1064
1065 if (area == TEXT_AREA)
1066 {
1067 if (INTEGERP (w->left_margin_cols))
1068 cols -= XFASTINT (w->left_margin_cols);
1069 if (INTEGERP (w->right_margin_cols))
1070 cols -= XFASTINT (w->right_margin_cols);
1071 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1072 }
1073 else if (area == LEFT_MARGIN_AREA)
1074 {
1075 cols = (INTEGERP (w->left_margin_cols)
1076 ? XFASTINT (w->left_margin_cols) : 0);
1077 pixels = 0;
1078 }
1079 else if (area == RIGHT_MARGIN_AREA)
1080 {
1081 cols = (INTEGERP (w->right_margin_cols)
1082 ? XFASTINT (w->right_margin_cols) : 0);
1083 pixels = 0;
1084 }
1085 }
1086
1087 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1088 }
1089
1090
1091 /* Return the pixel height of the display area of window W, not
1092 including mode lines of W, if any. */
1093
1094 INLINE int
1095 window_box_height (w)
1096 struct window *w;
1097 {
1098 struct frame *f = XFRAME (w->frame);
1099 int height = WINDOW_TOTAL_HEIGHT (w);
1100
1101 xassert (height >= 0);
1102
1103 /* Note: the code below that determines the mode-line/header-line
1104 height is essentially the same as that contained in the macro
1105 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1106 the appropriate glyph row has its `mode_line_p' flag set,
1107 and if it doesn't, uses estimate_mode_line_height instead. */
1108
1109 if (WINDOW_WANTS_MODELINE_P (w))
1110 {
1111 struct glyph_row *ml_row
1112 = (w->current_matrix && w->current_matrix->rows
1113 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1114 : 0);
1115 if (ml_row && ml_row->mode_line_p)
1116 height -= ml_row->height;
1117 else
1118 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1119 }
1120
1121 if (WINDOW_WANTS_HEADER_LINE_P (w))
1122 {
1123 struct glyph_row *hl_row
1124 = (w->current_matrix && w->current_matrix->rows
1125 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1126 : 0);
1127 if (hl_row && hl_row->mode_line_p)
1128 height -= hl_row->height;
1129 else
1130 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1131 }
1132
1133 /* With a very small font and a mode-line that's taller than
1134 default, we might end up with a negative height. */
1135 return max (0, height);
1136 }
1137
1138 /* Return the window-relative coordinate of the left edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the right of the left fringe of W. */
1141
1142 INLINE int
1143 window_box_left_offset (w, area)
1144 struct window *w;
1145 int area;
1146 {
1147 int x;
1148
1149 if (w->pseudo_window_p)
1150 return 0;
1151
1152 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1153
1154 if (area == TEXT_AREA)
1155 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1156 + window_box_width (w, LEFT_MARGIN_AREA));
1157 else if (area == RIGHT_MARGIN_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA)
1160 + window_box_width (w, TEXT_AREA)
1161 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1162 ? 0
1163 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1164 else if (area == LEFT_MARGIN_AREA
1165 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1166 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1167
1168 return x;
1169 }
1170
1171
1172 /* Return the window-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1175
1176 INLINE int
1177 window_box_right_offset (w, area)
1178 struct window *w;
1179 int area;
1180 {
1181 return window_box_left_offset (w, area) + window_box_width (w, area);
1182 }
1183
1184 /* Return the frame-relative coordinate of the left edge of display
1185 area AREA of window W. AREA < 0 means return the left edge of the
1186 whole window, to the right of the left fringe of W. */
1187
1188 INLINE int
1189 window_box_left (w, area)
1190 struct window *w;
1191 int area;
1192 {
1193 struct frame *f = XFRAME (w->frame);
1194 int x;
1195
1196 if (w->pseudo_window_p)
1197 return FRAME_INTERNAL_BORDER_WIDTH (f);
1198
1199 x = (WINDOW_LEFT_EDGE_X (w)
1200 + window_box_left_offset (w, area));
1201
1202 return x;
1203 }
1204
1205
1206 /* Return the frame-relative coordinate of the right edge of display
1207 area AREA of window W. AREA < 0 means return the left edge of the
1208 whole window, to the left of the right fringe of W. */
1209
1210 INLINE int
1211 window_box_right (w, area)
1212 struct window *w;
1213 int area;
1214 {
1215 return window_box_left (w, area) + window_box_width (w, area);
1216 }
1217
1218 /* Get the bounding box of the display area AREA of window W, without
1219 mode lines, in frame-relative coordinates. AREA < 0 means the
1220 whole window, not including the left and right fringes of
1221 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1222 coordinates of the upper-left corner of the box. Return in
1223 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1224
1225 INLINE void
1226 window_box (w, area, box_x, box_y, box_width, box_height)
1227 struct window *w;
1228 int area;
1229 int *box_x, *box_y, *box_width, *box_height;
1230 {
1231 if (box_width)
1232 *box_width = window_box_width (w, area);
1233 if (box_height)
1234 *box_height = window_box_height (w);
1235 if (box_x)
1236 *box_x = window_box_left (w, area);
1237 if (box_y)
1238 {
1239 *box_y = WINDOW_TOP_EDGE_Y (w);
1240 if (WINDOW_WANTS_HEADER_LINE_P (w))
1241 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1242 }
1243 }
1244
1245
1246 /* Get the bounding box of the display area AREA of window W, without
1247 mode lines. AREA < 0 means the whole window, not including the
1248 left and right fringe of the window. Return in *TOP_LEFT_X
1249 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1250 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1251 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1252 box. */
1253
1254 INLINE void
1255 window_box_edges (w, area, top_left_x, top_left_y,
1256 bottom_right_x, bottom_right_y)
1257 struct window *w;
1258 int area;
1259 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1260 {
1261 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1262 bottom_right_y);
1263 *bottom_right_x += *top_left_x;
1264 *bottom_right_y += *top_left_y;
1265 }
1266
1267
1268 \f
1269 /***********************************************************************
1270 Utilities
1271 ***********************************************************************/
1272
1273 /* Return the bottom y-position of the line the iterator IT is in.
1274 This can modify IT's settings. */
1275
1276 int
1277 line_bottom_y (it)
1278 struct it *it;
1279 {
1280 int line_height = it->max_ascent + it->max_descent;
1281 int line_top_y = it->current_y;
1282
1283 if (line_height == 0)
1284 {
1285 if (last_height)
1286 line_height = last_height;
1287 else if (IT_CHARPOS (*it) < ZV)
1288 {
1289 move_it_by_lines (it, 1, 1);
1290 line_height = (it->max_ascent || it->max_descent
1291 ? it->max_ascent + it->max_descent
1292 : last_height);
1293 }
1294 else
1295 {
1296 struct glyph_row *row = it->glyph_row;
1297
1298 /* Use the default character height. */
1299 it->glyph_row = NULL;
1300 it->what = IT_CHARACTER;
1301 it->c = ' ';
1302 it->len = 1;
1303 PRODUCE_GLYPHS (it);
1304 line_height = it->ascent + it->descent;
1305 it->glyph_row = row;
1306 }
1307 }
1308
1309 return line_top_y + line_height;
1310 }
1311
1312
1313 /* Return 1 if position CHARPOS is visible in window W.
1314 CHARPOS < 0 means return info about WINDOW_END position.
1315 If visible, set *X and *Y to pixel coordinates of top left corner.
1316 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1317 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1318
1319 int
1320 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1321 struct window *w;
1322 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1323 {
1324 struct it it;
1325 struct text_pos top;
1326 int visible_p = 0;
1327 struct buffer *old_buffer = NULL;
1328
1329 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1330 return visible_p;
1331
1332 if (XBUFFER (w->buffer) != current_buffer)
1333 {
1334 old_buffer = current_buffer;
1335 set_buffer_internal_1 (XBUFFER (w->buffer));
1336 }
1337
1338 SET_TEXT_POS_FROM_MARKER (top, w->start);
1339
1340 /* Compute exact mode line heights. */
1341 if (WINDOW_WANTS_MODELINE_P (w))
1342 current_mode_line_height
1343 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1344 current_buffer->mode_line_format);
1345
1346 if (WINDOW_WANTS_HEADER_LINE_P (w))
1347 current_header_line_height
1348 = display_mode_line (w, HEADER_LINE_FACE_ID,
1349 current_buffer->header_line_format);
1350
1351 start_display (&it, w, top);
1352 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1353 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1354
1355 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1356 {
1357 /* We have reached CHARPOS, or passed it. How the call to
1358 move_it_to can overshoot: (i) If CHARPOS is on invisible
1359 text, move_it_to stops at the end of the invisible text,
1360 after CHARPOS. (ii) If CHARPOS is in a display vector,
1361 move_it_to stops on its last glyph. */
1362 int top_x = it.current_x;
1363 int top_y = it.current_y;
1364 enum it_method it_method = it.method;
1365 /* Calling line_bottom_y may change it.method. */
1366 int bottom_y = (last_height = 0, line_bottom_y (&it));
1367 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1368
1369 if (top_y < window_top_y)
1370 visible_p = bottom_y > window_top_y;
1371 else if (top_y < it.last_visible_y)
1372 visible_p = 1;
1373 if (visible_p)
1374 {
1375 if (it_method == GET_FROM_BUFFER)
1376 {
1377 Lisp_Object window, prop;
1378
1379 XSETWINDOW (window, w);
1380 prop = Fget_char_property (make_number (it.position.charpos),
1381 Qinvisible, window);
1382
1383 /* If charpos coincides with invisible text covered with an
1384 ellipsis, use the first glyph of the ellipsis to compute
1385 the pixel positions. */
1386 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1387 {
1388 struct glyph_row *row = it.glyph_row;
1389 struct glyph *glyph = row->glyphs[TEXT_AREA];
1390 struct glyph *end = glyph + row->used[TEXT_AREA];
1391 int x = row->x;
1392
1393 for (; glyph < end
1394 && (!BUFFERP (glyph->object)
1395 || glyph->charpos < charpos);
1396 glyph++)
1397 x += glyph->pixel_width;
1398 top_x = x;
1399 }
1400 }
1401 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1402 {
1403 /* We stopped on the last glyph of a display vector.
1404 Try and recompute. Hack alert! */
1405 if (charpos < 2 || top.charpos >= charpos)
1406 top_x = it.glyph_row->x;
1407 else
1408 {
1409 struct it it2;
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 get_next_display_element (&it2);
1413 PRODUCE_GLYPHS (&it2);
1414 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1415 || it2.current_x > it2.last_visible_x)
1416 top_x = it.glyph_row->x;
1417 else
1418 {
1419 top_x = it2.current_x;
1420 top_y = it2.current_y;
1421 }
1422 }
1423 }
1424
1425 *x = top_x;
1426 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1427 *rtop = max (0, window_top_y - top_y);
1428 *rbot = max (0, bottom_y - it.last_visible_y);
1429 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1430 - max (top_y, window_top_y)));
1431 *vpos = it.vpos;
1432 }
1433 }
1434 else
1435 {
1436 struct it it2;
1437
1438 it2 = it;
1439 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1440 move_it_by_lines (&it, 1, 0);
1441 if (charpos < IT_CHARPOS (it)
1442 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1443 {
1444 visible_p = 1;
1445 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1446 *x = it2.current_x;
1447 *y = it2.current_y + it2.max_ascent - it2.ascent;
1448 *rtop = max (0, -it2.current_y);
1449 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1450 - it.last_visible_y));
1451 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1452 it.last_visible_y)
1453 - max (it2.current_y,
1454 WINDOW_HEADER_LINE_HEIGHT (w))));
1455 *vpos = it2.vpos;
1456 }
1457 }
1458
1459 if (old_buffer)
1460 set_buffer_internal_1 (old_buffer);
1461
1462 current_header_line_height = current_mode_line_height = -1;
1463
1464 if (visible_p && XFASTINT (w->hscroll) > 0)
1465 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1466
1467 #if 0
1468 /* Debugging code. */
1469 if (visible_p)
1470 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1471 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1472 else
1473 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1474 #endif
1475
1476 return visible_p;
1477 }
1478
1479
1480 /* Return the next character from STR which is MAXLEN bytes long.
1481 Return in *LEN the length of the character. This is like
1482 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1483 we find one, we return a `?', but with the length of the invalid
1484 character. */
1485
1486 static INLINE int
1487 string_char_and_length (str, maxlen, len)
1488 const unsigned char *str;
1489 int maxlen, *len;
1490 {
1491 int c;
1492
1493 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1494 if (!CHAR_VALID_P (c, 1))
1495 /* We may not change the length here because other places in Emacs
1496 don't use this function, i.e. they silently accept invalid
1497 characters. */
1498 c = '?';
1499
1500 return c;
1501 }
1502
1503
1504
1505 /* Given a position POS containing a valid character and byte position
1506 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1507
1508 static struct text_pos
1509 string_pos_nchars_ahead (pos, string, nchars)
1510 struct text_pos pos;
1511 Lisp_Object string;
1512 int nchars;
1513 {
1514 xassert (STRINGP (string) && nchars >= 0);
1515
1516 if (STRING_MULTIBYTE (string))
1517 {
1518 int rest = SBYTES (string) - BYTEPOS (pos);
1519 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1520 int len;
1521
1522 while (nchars--)
1523 {
1524 string_char_and_length (p, rest, &len);
1525 p += len, rest -= len;
1526 xassert (rest >= 0);
1527 CHARPOS (pos) += 1;
1528 BYTEPOS (pos) += len;
1529 }
1530 }
1531 else
1532 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1533
1534 return pos;
1535 }
1536
1537
1538 /* Value is the text position, i.e. character and byte position,
1539 for character position CHARPOS in STRING. */
1540
1541 static INLINE struct text_pos
1542 string_pos (charpos, string)
1543 int charpos;
1544 Lisp_Object string;
1545 {
1546 struct text_pos pos;
1547 xassert (STRINGP (string));
1548 xassert (charpos >= 0);
1549 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1550 return pos;
1551 }
1552
1553
1554 /* Value is a text position, i.e. character and byte position, for
1555 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1556 means recognize multibyte characters. */
1557
1558 static struct text_pos
1559 c_string_pos (charpos, s, multibyte_p)
1560 int charpos;
1561 unsigned char *s;
1562 int multibyte_p;
1563 {
1564 struct text_pos pos;
1565
1566 xassert (s != NULL);
1567 xassert (charpos >= 0);
1568
1569 if (multibyte_p)
1570 {
1571 int rest = strlen (s), len;
1572
1573 SET_TEXT_POS (pos, 0, 0);
1574 while (charpos--)
1575 {
1576 string_char_and_length (s, rest, &len);
1577 s += len, rest -= len;
1578 xassert (rest >= 0);
1579 CHARPOS (pos) += 1;
1580 BYTEPOS (pos) += len;
1581 }
1582 }
1583 else
1584 SET_TEXT_POS (pos, charpos, charpos);
1585
1586 return pos;
1587 }
1588
1589
1590 /* Value is the number of characters in C string S. MULTIBYTE_P
1591 non-zero means recognize multibyte characters. */
1592
1593 static int
1594 number_of_chars (s, multibyte_p)
1595 unsigned char *s;
1596 int multibyte_p;
1597 {
1598 int nchars;
1599
1600 if (multibyte_p)
1601 {
1602 int rest = strlen (s), len;
1603 unsigned char *p = (unsigned char *) s;
1604
1605 for (nchars = 0; rest > 0; ++nchars)
1606 {
1607 string_char_and_length (p, rest, &len);
1608 rest -= len, p += len;
1609 }
1610 }
1611 else
1612 nchars = strlen (s);
1613
1614 return nchars;
1615 }
1616
1617
1618 /* Compute byte position NEWPOS->bytepos corresponding to
1619 NEWPOS->charpos. POS is a known position in string STRING.
1620 NEWPOS->charpos must be >= POS.charpos. */
1621
1622 static void
1623 compute_string_pos (newpos, pos, string)
1624 struct text_pos *newpos, pos;
1625 Lisp_Object string;
1626 {
1627 xassert (STRINGP (string));
1628 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1629
1630 if (STRING_MULTIBYTE (string))
1631 *newpos = string_pos_nchars_ahead (pos, string,
1632 CHARPOS (*newpos) - CHARPOS (pos));
1633 else
1634 BYTEPOS (*newpos) = CHARPOS (*newpos);
1635 }
1636
1637 /* EXPORT:
1638 Return an estimation of the pixel height of mode or top lines on
1639 frame F. FACE_ID specifies what line's height to estimate. */
1640
1641 int
1642 estimate_mode_line_height (f, face_id)
1643 struct frame *f;
1644 enum face_id face_id;
1645 {
1646 #ifdef HAVE_WINDOW_SYSTEM
1647 if (FRAME_WINDOW_P (f))
1648 {
1649 int height = FONT_HEIGHT (FRAME_FONT (f));
1650
1651 /* This function is called so early when Emacs starts that the face
1652 cache and mode line face are not yet initialized. */
1653 if (FRAME_FACE_CACHE (f))
1654 {
1655 struct face *face = FACE_FROM_ID (f, face_id);
1656 if (face)
1657 {
1658 if (face->font)
1659 height = FONT_HEIGHT (face->font);
1660 if (face->box_line_width > 0)
1661 height += 2 * face->box_line_width;
1662 }
1663 }
1664
1665 return height;
1666 }
1667 #endif
1668
1669 return 1;
1670 }
1671
1672 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1673 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1674 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1675 not force the value into range. */
1676
1677 void
1678 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1679 FRAME_PTR f;
1680 register int pix_x, pix_y;
1681 int *x, *y;
1682 NativeRectangle *bounds;
1683 int noclip;
1684 {
1685
1686 #ifdef HAVE_WINDOW_SYSTEM
1687 if (FRAME_WINDOW_P (f))
1688 {
1689 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1690 even for negative values. */
1691 if (pix_x < 0)
1692 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1693 if (pix_y < 0)
1694 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1695
1696 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1697 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1698
1699 if (bounds)
1700 STORE_NATIVE_RECT (*bounds,
1701 FRAME_COL_TO_PIXEL_X (f, pix_x),
1702 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1703 FRAME_COLUMN_WIDTH (f) - 1,
1704 FRAME_LINE_HEIGHT (f) - 1);
1705
1706 if (!noclip)
1707 {
1708 if (pix_x < 0)
1709 pix_x = 0;
1710 else if (pix_x > FRAME_TOTAL_COLS (f))
1711 pix_x = FRAME_TOTAL_COLS (f);
1712
1713 if (pix_y < 0)
1714 pix_y = 0;
1715 else if (pix_y > FRAME_LINES (f))
1716 pix_y = FRAME_LINES (f);
1717 }
1718 }
1719 #endif
1720
1721 *x = pix_x;
1722 *y = pix_y;
1723 }
1724
1725
1726 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1727 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1728 can't tell the positions because W's display is not up to date,
1729 return 0. */
1730
1731 int
1732 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1733 struct window *w;
1734 int hpos, vpos;
1735 int *frame_x, *frame_y;
1736 {
1737 #ifdef HAVE_WINDOW_SYSTEM
1738 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1739 {
1740 int success_p;
1741
1742 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1743 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1744
1745 if (display_completed)
1746 {
1747 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1748 struct glyph *glyph = row->glyphs[TEXT_AREA];
1749 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1750
1751 hpos = row->x;
1752 vpos = row->y;
1753 while (glyph < end)
1754 {
1755 hpos += glyph->pixel_width;
1756 ++glyph;
1757 }
1758
1759 /* If first glyph is partially visible, its first visible position is still 0. */
1760 if (hpos < 0)
1761 hpos = 0;
1762
1763 success_p = 1;
1764 }
1765 else
1766 {
1767 hpos = vpos = 0;
1768 success_p = 0;
1769 }
1770
1771 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1772 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1773 return success_p;
1774 }
1775 #endif
1776
1777 *frame_x = hpos;
1778 *frame_y = vpos;
1779 return 1;
1780 }
1781
1782
1783 #ifdef HAVE_WINDOW_SYSTEM
1784
1785 /* Find the glyph under window-relative coordinates X/Y in window W.
1786 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1787 strings. Return in *HPOS and *VPOS the row and column number of
1788 the glyph found. Return in *AREA the glyph area containing X.
1789 Value is a pointer to the glyph found or null if X/Y is not on
1790 text, or we can't tell because W's current matrix is not up to
1791 date. */
1792
1793 static
1794 struct glyph *
1795 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1796 struct window *w;
1797 int x, y;
1798 int *hpos, *vpos, *dx, *dy, *area;
1799 {
1800 struct glyph *glyph, *end;
1801 struct glyph_row *row = NULL;
1802 int x0, i;
1803
1804 /* Find row containing Y. Give up if some row is not enabled. */
1805 for (i = 0; i < w->current_matrix->nrows; ++i)
1806 {
1807 row = MATRIX_ROW (w->current_matrix, i);
1808 if (!row->enabled_p)
1809 return NULL;
1810 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1811 break;
1812 }
1813
1814 *vpos = i;
1815 *hpos = 0;
1816
1817 /* Give up if Y is not in the window. */
1818 if (i == w->current_matrix->nrows)
1819 return NULL;
1820
1821 /* Get the glyph area containing X. */
1822 if (w->pseudo_window_p)
1823 {
1824 *area = TEXT_AREA;
1825 x0 = 0;
1826 }
1827 else
1828 {
1829 if (x < window_box_left_offset (w, TEXT_AREA))
1830 {
1831 *area = LEFT_MARGIN_AREA;
1832 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1833 }
1834 else if (x < window_box_right_offset (w, TEXT_AREA))
1835 {
1836 *area = TEXT_AREA;
1837 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1838 }
1839 else
1840 {
1841 *area = RIGHT_MARGIN_AREA;
1842 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1843 }
1844 }
1845
1846 /* Find glyph containing X. */
1847 glyph = row->glyphs[*area];
1848 end = glyph + row->used[*area];
1849 x -= x0;
1850 while (glyph < end && x >= glyph->pixel_width)
1851 {
1852 x -= glyph->pixel_width;
1853 ++glyph;
1854 }
1855
1856 if (glyph == end)
1857 return NULL;
1858
1859 if (dx)
1860 {
1861 *dx = x;
1862 *dy = y - (row->y + row->ascent - glyph->ascent);
1863 }
1864
1865 *hpos = glyph - row->glyphs[*area];
1866 return glyph;
1867 }
1868
1869
1870 /* EXPORT:
1871 Convert frame-relative x/y to coordinates relative to window W.
1872 Takes pseudo-windows into account. */
1873
1874 void
1875 frame_to_window_pixel_xy (w, x, y)
1876 struct window *w;
1877 int *x, *y;
1878 {
1879 if (w->pseudo_window_p)
1880 {
1881 /* A pseudo-window is always full-width, and starts at the
1882 left edge of the frame, plus a frame border. */
1883 struct frame *f = XFRAME (w->frame);
1884 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1885 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1886 }
1887 else
1888 {
1889 *x -= WINDOW_LEFT_EDGE_X (w);
1890 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1891 }
1892 }
1893
1894 /* EXPORT:
1895 Return in RECTS[] at most N clipping rectangles for glyph string S.
1896 Return the number of stored rectangles. */
1897
1898 int
1899 get_glyph_string_clip_rects (s, rects, n)
1900 struct glyph_string *s;
1901 NativeRectangle *rects;
1902 int n;
1903 {
1904 XRectangle r;
1905
1906 if (n <= 0)
1907 return 0;
1908
1909 if (s->row->full_width_p)
1910 {
1911 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1912 r.x = WINDOW_LEFT_EDGE_X (s->w);
1913 r.width = WINDOW_TOTAL_WIDTH (s->w);
1914
1915 /* Unless displaying a mode or menu bar line, which are always
1916 fully visible, clip to the visible part of the row. */
1917 if (s->w->pseudo_window_p)
1918 r.height = s->row->visible_height;
1919 else
1920 r.height = s->height;
1921 }
1922 else
1923 {
1924 /* This is a text line that may be partially visible. */
1925 r.x = window_box_left (s->w, s->area);
1926 r.width = window_box_width (s->w, s->area);
1927 r.height = s->row->visible_height;
1928 }
1929
1930 if (s->clip_head)
1931 if (r.x < s->clip_head->x)
1932 {
1933 if (r.width >= s->clip_head->x - r.x)
1934 r.width -= s->clip_head->x - r.x;
1935 else
1936 r.width = 0;
1937 r.x = s->clip_head->x;
1938 }
1939 if (s->clip_tail)
1940 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1941 {
1942 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1943 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1944 else
1945 r.width = 0;
1946 }
1947
1948 /* If S draws overlapping rows, it's sufficient to use the top and
1949 bottom of the window for clipping because this glyph string
1950 intentionally draws over other lines. */
1951 if (s->for_overlaps)
1952 {
1953 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1954 r.height = window_text_bottom_y (s->w) - r.y;
1955
1956 /* Alas, the above simple strategy does not work for the
1957 environments with anti-aliased text: if the same text is
1958 drawn onto the same place multiple times, it gets thicker.
1959 If the overlap we are processing is for the erased cursor, we
1960 take the intersection with the rectagle of the cursor. */
1961 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1962 {
1963 XRectangle rc, r_save = r;
1964
1965 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1966 rc.y = s->w->phys_cursor.y;
1967 rc.width = s->w->phys_cursor_width;
1968 rc.height = s->w->phys_cursor_height;
1969
1970 x_intersect_rectangles (&r_save, &rc, &r);
1971 }
1972 }
1973 else
1974 {
1975 /* Don't use S->y for clipping because it doesn't take partially
1976 visible lines into account. For example, it can be negative for
1977 partially visible lines at the top of a window. */
1978 if (!s->row->full_width_p
1979 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1980 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1981 else
1982 r.y = max (0, s->row->y);
1983
1984 /* If drawing a tool-bar window, draw it over the internal border
1985 at the top of the window. */
1986 if (WINDOWP (s->f->tool_bar_window)
1987 && s->w == XWINDOW (s->f->tool_bar_window))
1988 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1989 }
1990
1991 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1992
1993 /* If drawing the cursor, don't let glyph draw outside its
1994 advertised boundaries. Cleartype does this under some circumstances. */
1995 if (s->hl == DRAW_CURSOR)
1996 {
1997 struct glyph *glyph = s->first_glyph;
1998 int height, max_y;
1999
2000 if (s->x > r.x)
2001 {
2002 r.width -= s->x - r.x;
2003 r.x = s->x;
2004 }
2005 r.width = min (r.width, glyph->pixel_width);
2006
2007 /* If r.y is below window bottom, ensure that we still see a cursor. */
2008 height = min (glyph->ascent + glyph->descent,
2009 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2010 max_y = window_text_bottom_y (s->w) - height;
2011 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2012 if (s->ybase - glyph->ascent > max_y)
2013 {
2014 r.y = max_y;
2015 r.height = height;
2016 }
2017 else
2018 {
2019 /* Don't draw cursor glyph taller than our actual glyph. */
2020 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2021 if (height < r.height)
2022 {
2023 max_y = r.y + r.height;
2024 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2025 r.height = min (max_y - r.y, height);
2026 }
2027 }
2028 }
2029
2030 if (s->row->clip)
2031 {
2032 XRectangle r_save = r;
2033
2034 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2035 r.width = 0;
2036 }
2037
2038 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2039 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2040 {
2041 #ifdef CONVERT_FROM_XRECT
2042 CONVERT_FROM_XRECT (r, *rects);
2043 #else
2044 *rects = r;
2045 #endif
2046 return 1;
2047 }
2048 else
2049 {
2050 /* If we are processing overlapping and allowed to return
2051 multiple clipping rectangles, we exclude the row of the glyph
2052 string from the clipping rectangle. This is to avoid drawing
2053 the same text on the environment with anti-aliasing. */
2054 #ifdef CONVERT_FROM_XRECT
2055 XRectangle rs[2];
2056 #else
2057 XRectangle *rs = rects;
2058 #endif
2059 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2060
2061 if (s->for_overlaps & OVERLAPS_PRED)
2062 {
2063 rs[i] = r;
2064 if (r.y + r.height > row_y)
2065 {
2066 if (r.y < row_y)
2067 rs[i].height = row_y - r.y;
2068 else
2069 rs[i].height = 0;
2070 }
2071 i++;
2072 }
2073 if (s->for_overlaps & OVERLAPS_SUCC)
2074 {
2075 rs[i] = r;
2076 if (r.y < row_y + s->row->visible_height)
2077 {
2078 if (r.y + r.height > row_y + s->row->visible_height)
2079 {
2080 rs[i].y = row_y + s->row->visible_height;
2081 rs[i].height = r.y + r.height - rs[i].y;
2082 }
2083 else
2084 rs[i].height = 0;
2085 }
2086 i++;
2087 }
2088
2089 n = i;
2090 #ifdef CONVERT_FROM_XRECT
2091 for (i = 0; i < n; i++)
2092 CONVERT_FROM_XRECT (rs[i], rects[i]);
2093 #endif
2094 return n;
2095 }
2096 }
2097
2098 /* EXPORT:
2099 Return in *NR the clipping rectangle for glyph string S. */
2100
2101 void
2102 get_glyph_string_clip_rect (s, nr)
2103 struct glyph_string *s;
2104 NativeRectangle *nr;
2105 {
2106 get_glyph_string_clip_rects (s, nr, 1);
2107 }
2108
2109
2110 /* EXPORT:
2111 Return the position and height of the phys cursor in window W.
2112 Set w->phys_cursor_width to width of phys cursor.
2113 */
2114
2115 void
2116 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2117 struct window *w;
2118 struct glyph_row *row;
2119 struct glyph *glyph;
2120 int *xp, *yp, *heightp;
2121 {
2122 struct frame *f = XFRAME (WINDOW_FRAME (w));
2123 int x, y, wd, h, h0, y0;
2124
2125 /* Compute the width of the rectangle to draw. If on a stretch
2126 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2127 rectangle as wide as the glyph, but use a canonical character
2128 width instead. */
2129 wd = glyph->pixel_width - 1;
2130 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2131 wd++; /* Why? */
2132 #endif
2133
2134 x = w->phys_cursor.x;
2135 if (x < 0)
2136 {
2137 wd += x;
2138 x = 0;
2139 }
2140
2141 if (glyph->type == STRETCH_GLYPH
2142 && !x_stretch_cursor_p)
2143 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2144 w->phys_cursor_width = wd;
2145
2146 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2147
2148 /* If y is below window bottom, ensure that we still see a cursor. */
2149 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2150
2151 h = max (h0, glyph->ascent + glyph->descent);
2152 h0 = min (h0, glyph->ascent + glyph->descent);
2153
2154 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2155 if (y < y0)
2156 {
2157 h = max (h - (y0 - y) + 1, h0);
2158 y = y0 - 1;
2159 }
2160 else
2161 {
2162 y0 = window_text_bottom_y (w) - h0;
2163 if (y > y0)
2164 {
2165 h += y - y0;
2166 y = y0;
2167 }
2168 }
2169
2170 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2171 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2172 *heightp = h;
2173 }
2174
2175 /*
2176 * Remember which glyph the mouse is over.
2177 */
2178
2179 void
2180 remember_mouse_glyph (f, gx, gy, rect)
2181 struct frame *f;
2182 int gx, gy;
2183 NativeRectangle *rect;
2184 {
2185 Lisp_Object window;
2186 struct window *w;
2187 struct glyph_row *r, *gr, *end_row;
2188 enum window_part part;
2189 enum glyph_row_area area;
2190 int x, y, width, height;
2191
2192 /* Try to determine frame pixel position and size of the glyph under
2193 frame pixel coordinates X/Y on frame F. */
2194
2195 if (!f->glyphs_initialized_p
2196 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2197 NILP (window)))
2198 {
2199 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2200 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2201 goto virtual_glyph;
2202 }
2203
2204 w = XWINDOW (window);
2205 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2206 height = WINDOW_FRAME_LINE_HEIGHT (w);
2207
2208 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2209 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2210
2211 if (w->pseudo_window_p)
2212 {
2213 area = TEXT_AREA;
2214 part = ON_MODE_LINE; /* Don't adjust margin. */
2215 goto text_glyph;
2216 }
2217
2218 switch (part)
2219 {
2220 case ON_LEFT_MARGIN:
2221 area = LEFT_MARGIN_AREA;
2222 goto text_glyph;
2223
2224 case ON_RIGHT_MARGIN:
2225 area = RIGHT_MARGIN_AREA;
2226 goto text_glyph;
2227
2228 case ON_HEADER_LINE:
2229 case ON_MODE_LINE:
2230 gr = (part == ON_HEADER_LINE
2231 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2232 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2233 gy = gr->y;
2234 area = TEXT_AREA;
2235 goto text_glyph_row_found;
2236
2237 case ON_TEXT:
2238 area = TEXT_AREA;
2239
2240 text_glyph:
2241 gr = 0; gy = 0;
2242 for (; r <= end_row && r->enabled_p; ++r)
2243 if (r->y + r->height > y)
2244 {
2245 gr = r; gy = r->y;
2246 break;
2247 }
2248
2249 text_glyph_row_found:
2250 if (gr && gy <= y)
2251 {
2252 struct glyph *g = gr->glyphs[area];
2253 struct glyph *end = g + gr->used[area];
2254
2255 height = gr->height;
2256 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2257 if (gx + g->pixel_width > x)
2258 break;
2259
2260 if (g < end)
2261 {
2262 if (g->type == IMAGE_GLYPH)
2263 {
2264 /* Don't remember when mouse is over image, as
2265 image may have hot-spots. */
2266 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2267 return;
2268 }
2269 width = g->pixel_width;
2270 }
2271 else
2272 {
2273 /* Use nominal char spacing at end of line. */
2274 x -= gx;
2275 gx += (x / width) * width;
2276 }
2277
2278 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2279 gx += window_box_left_offset (w, area);
2280 }
2281 else
2282 {
2283 /* Use nominal line height at end of window. */
2284 gx = (x / width) * width;
2285 y -= gy;
2286 gy += (y / height) * height;
2287 }
2288 break;
2289
2290 case ON_LEFT_FRINGE:
2291 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2292 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2293 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2294 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2295 goto row_glyph;
2296
2297 case ON_RIGHT_FRINGE:
2298 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2300 : window_box_right_offset (w, TEXT_AREA));
2301 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2302 goto row_glyph;
2303
2304 case ON_SCROLL_BAR:
2305 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2306 ? 0
2307 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2308 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2309 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2310 : 0)));
2311 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2312
2313 row_glyph:
2314 gr = 0, gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2317 {
2318 gr = r; gy = r->y;
2319 break;
2320 }
2321
2322 if (gr && gy <= y)
2323 height = gr->height;
2324 else
2325 {
2326 /* Use nominal line height at end of window. */
2327 y -= gy;
2328 gy += (y / height) * height;
2329 }
2330 break;
2331
2332 default:
2333 ;
2334 virtual_glyph:
2335 /* If there is no glyph under the mouse, then we divide the screen
2336 into a grid of the smallest glyph in the frame, and use that
2337 as our "glyph". */
2338
2339 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2340 round down even for negative values. */
2341 if (gx < 0)
2342 gx -= width - 1;
2343 if (gy < 0)
2344 gy -= height - 1;
2345
2346 gx = (gx / width) * width;
2347 gy = (gy / height) * height;
2348
2349 goto store_rect;
2350 }
2351
2352 gx += WINDOW_LEFT_EDGE_X (w);
2353 gy += WINDOW_TOP_EDGE_Y (w);
2354
2355 store_rect:
2356 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2357
2358 /* Visible feedback for debugging. */
2359 #if 0
2360 #if HAVE_X_WINDOWS
2361 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2362 f->output_data.x->normal_gc,
2363 gx, gy, width, height);
2364 #endif
2365 #endif
2366 }
2367
2368
2369 #endif /* HAVE_WINDOW_SYSTEM */
2370
2371 \f
2372 /***********************************************************************
2373 Lisp form evaluation
2374 ***********************************************************************/
2375
2376 /* Error handler for safe_eval and safe_call. */
2377
2378 static Lisp_Object
2379 safe_eval_handler (arg)
2380 Lisp_Object arg;
2381 {
2382 add_to_log ("Error during redisplay: %s", arg, Qnil);
2383 return Qnil;
2384 }
2385
2386
2387 /* Evaluate SEXPR and return the result, or nil if something went
2388 wrong. Prevent redisplay during the evaluation. */
2389
2390 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2391 Return the result, or nil if something went wrong. Prevent
2392 redisplay during the evaluation. */
2393
2394 Lisp_Object
2395 safe_call (nargs, args)
2396 int nargs;
2397 Lisp_Object *args;
2398 {
2399 Lisp_Object val;
2400
2401 if (inhibit_eval_during_redisplay)
2402 val = Qnil;
2403 else
2404 {
2405 int count = SPECPDL_INDEX ();
2406 struct gcpro gcpro1;
2407
2408 GCPRO1 (args[0]);
2409 gcpro1.nvars = nargs;
2410 specbind (Qinhibit_redisplay, Qt);
2411 /* Use Qt to ensure debugger does not run,
2412 so there is no possibility of wanting to redisplay. */
2413 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2414 safe_eval_handler);
2415 UNGCPRO;
2416 val = unbind_to (count, val);
2417 }
2418
2419 return val;
2420 }
2421
2422
2423 /* Call function FN with one argument ARG.
2424 Return the result, or nil if something went wrong. */
2425
2426 Lisp_Object
2427 safe_call1 (fn, arg)
2428 Lisp_Object fn, arg;
2429 {
2430 Lisp_Object args[2];
2431 args[0] = fn;
2432 args[1] = arg;
2433 return safe_call (2, args);
2434 }
2435
2436 static Lisp_Object Qeval;
2437
2438 Lisp_Object
2439 safe_eval (Lisp_Object sexpr)
2440 {
2441 return safe_call1 (Qeval, sexpr);
2442 }
2443
2444 /* Call function FN with one argument ARG.
2445 Return the result, or nil if something went wrong. */
2446
2447 Lisp_Object
2448 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2449 {
2450 Lisp_Object args[3];
2451 args[0] = fn;
2452 args[1] = arg1;
2453 args[2] = arg2;
2454 return safe_call (3, args);
2455 }
2456
2457
2458 \f
2459 /***********************************************************************
2460 Debugging
2461 ***********************************************************************/
2462
2463 #if 0
2464
2465 /* Define CHECK_IT to perform sanity checks on iterators.
2466 This is for debugging. It is too slow to do unconditionally. */
2467
2468 static void
2469 check_it (it)
2470 struct it *it;
2471 {
2472 if (it->method == GET_FROM_STRING)
2473 {
2474 xassert (STRINGP (it->string));
2475 xassert (IT_STRING_CHARPOS (*it) >= 0);
2476 }
2477 else
2478 {
2479 xassert (IT_STRING_CHARPOS (*it) < 0);
2480 if (it->method == GET_FROM_BUFFER)
2481 {
2482 /* Check that character and byte positions agree. */
2483 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2484 }
2485 }
2486
2487 if (it->dpvec)
2488 xassert (it->current.dpvec_index >= 0);
2489 else
2490 xassert (it->current.dpvec_index < 0);
2491 }
2492
2493 #define CHECK_IT(IT) check_it ((IT))
2494
2495 #else /* not 0 */
2496
2497 #define CHECK_IT(IT) (void) 0
2498
2499 #endif /* not 0 */
2500
2501
2502 #if GLYPH_DEBUG
2503
2504 /* Check that the window end of window W is what we expect it
2505 to be---the last row in the current matrix displaying text. */
2506
2507 static void
2508 check_window_end (w)
2509 struct window *w;
2510 {
2511 if (!MINI_WINDOW_P (w)
2512 && !NILP (w->window_end_valid))
2513 {
2514 struct glyph_row *row;
2515 xassert ((row = MATRIX_ROW (w->current_matrix,
2516 XFASTINT (w->window_end_vpos)),
2517 !row->enabled_p
2518 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2519 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2520 }
2521 }
2522
2523 #define CHECK_WINDOW_END(W) check_window_end ((W))
2524
2525 #else /* not GLYPH_DEBUG */
2526
2527 #define CHECK_WINDOW_END(W) (void) 0
2528
2529 #endif /* not GLYPH_DEBUG */
2530
2531
2532 \f
2533 /***********************************************************************
2534 Iterator initialization
2535 ***********************************************************************/
2536
2537 /* Initialize IT for displaying current_buffer in window W, starting
2538 at character position CHARPOS. CHARPOS < 0 means that no buffer
2539 position is specified which is useful when the iterator is assigned
2540 a position later. BYTEPOS is the byte position corresponding to
2541 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2542
2543 If ROW is not null, calls to produce_glyphs with IT as parameter
2544 will produce glyphs in that row.
2545
2546 BASE_FACE_ID is the id of a base face to use. It must be one of
2547 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2548 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2549 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2550
2551 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2552 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2553 will be initialized to use the corresponding mode line glyph row of
2554 the desired matrix of W. */
2555
2556 void
2557 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2558 struct it *it;
2559 struct window *w;
2560 int charpos, bytepos;
2561 struct glyph_row *row;
2562 enum face_id base_face_id;
2563 {
2564 int highlight_region_p;
2565 enum face_id remapped_base_face_id = base_face_id;
2566
2567 /* Some precondition checks. */
2568 xassert (w != NULL && it != NULL);
2569 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2570 && charpos <= ZV));
2571
2572 /* If face attributes have been changed since the last redisplay,
2573 free realized faces now because they depend on face definitions
2574 that might have changed. Don't free faces while there might be
2575 desired matrices pending which reference these faces. */
2576 if (face_change_count && !inhibit_free_realized_faces)
2577 {
2578 face_change_count = 0;
2579 free_all_realized_faces (Qnil);
2580 }
2581
2582 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2583 if (! NILP (Vface_remapping_alist))
2584 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2585
2586 /* Use one of the mode line rows of W's desired matrix if
2587 appropriate. */
2588 if (row == NULL)
2589 {
2590 if (base_face_id == MODE_LINE_FACE_ID
2591 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2592 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2593 else if (base_face_id == HEADER_LINE_FACE_ID)
2594 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2595 }
2596
2597 /* Clear IT. */
2598 bzero (it, sizeof *it);
2599 it->current.overlay_string_index = -1;
2600 it->current.dpvec_index = -1;
2601 it->base_face_id = remapped_base_face_id;
2602 it->string = Qnil;
2603 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2604
2605 /* The window in which we iterate over current_buffer: */
2606 XSETWINDOW (it->window, w);
2607 it->w = w;
2608 it->f = XFRAME (w->frame);
2609
2610 it->cmp_it.id = -1;
2611
2612 /* Extra space between lines (on window systems only). */
2613 if (base_face_id == DEFAULT_FACE_ID
2614 && FRAME_WINDOW_P (it->f))
2615 {
2616 if (NATNUMP (current_buffer->extra_line_spacing))
2617 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2618 else if (FLOATP (current_buffer->extra_line_spacing))
2619 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2620 * FRAME_LINE_HEIGHT (it->f));
2621 else if (it->f->extra_line_spacing > 0)
2622 it->extra_line_spacing = it->f->extra_line_spacing;
2623 it->max_extra_line_spacing = 0;
2624 }
2625
2626 /* If realized faces have been removed, e.g. because of face
2627 attribute changes of named faces, recompute them. When running
2628 in batch mode, the face cache of the initial frame is null. If
2629 we happen to get called, make a dummy face cache. */
2630 if (FRAME_FACE_CACHE (it->f) == NULL)
2631 init_frame_faces (it->f);
2632 if (FRAME_FACE_CACHE (it->f)->used == 0)
2633 recompute_basic_faces (it->f);
2634
2635 /* Current value of the `slice', `space-width', and 'height' properties. */
2636 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2637 it->space_width = Qnil;
2638 it->font_height = Qnil;
2639 it->override_ascent = -1;
2640
2641 /* Are control characters displayed as `^C'? */
2642 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2643
2644 /* -1 means everything between a CR and the following line end
2645 is invisible. >0 means lines indented more than this value are
2646 invisible. */
2647 it->selective = (INTEGERP (current_buffer->selective_display)
2648 ? XFASTINT (current_buffer->selective_display)
2649 : (!NILP (current_buffer->selective_display)
2650 ? -1 : 0));
2651 it->selective_display_ellipsis_p
2652 = !NILP (current_buffer->selective_display_ellipses);
2653
2654 /* Display table to use. */
2655 it->dp = window_display_table (w);
2656
2657 /* Are multibyte characters enabled in current_buffer? */
2658 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2659
2660 /* Non-zero if we should highlight the region. */
2661 highlight_region_p
2662 = (!NILP (Vtransient_mark_mode)
2663 && !NILP (current_buffer->mark_active)
2664 && XMARKER (current_buffer->mark)->buffer != 0);
2665
2666 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2667 start and end of a visible region in window IT->w. Set both to
2668 -1 to indicate no region. */
2669 if (highlight_region_p
2670 /* Maybe highlight only in selected window. */
2671 && (/* Either show region everywhere. */
2672 highlight_nonselected_windows
2673 /* Or show region in the selected window. */
2674 || w == XWINDOW (selected_window)
2675 /* Or show the region if we are in the mini-buffer and W is
2676 the window the mini-buffer refers to. */
2677 || (MINI_WINDOW_P (XWINDOW (selected_window))
2678 && WINDOWP (minibuf_selected_window)
2679 && w == XWINDOW (minibuf_selected_window))))
2680 {
2681 int charpos = marker_position (current_buffer->mark);
2682 it->region_beg_charpos = min (PT, charpos);
2683 it->region_end_charpos = max (PT, charpos);
2684 }
2685 else
2686 it->region_beg_charpos = it->region_end_charpos = -1;
2687
2688 /* Get the position at which the redisplay_end_trigger hook should
2689 be run, if it is to be run at all. */
2690 if (MARKERP (w->redisplay_end_trigger)
2691 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2692 it->redisplay_end_trigger_charpos
2693 = marker_position (w->redisplay_end_trigger);
2694 else if (INTEGERP (w->redisplay_end_trigger))
2695 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2696
2697 /* Correct bogus values of tab_width. */
2698 it->tab_width = XINT (current_buffer->tab_width);
2699 if (it->tab_width <= 0 || it->tab_width > 1000)
2700 it->tab_width = 8;
2701
2702 /* Are lines in the display truncated? */
2703 if (base_face_id != DEFAULT_FACE_ID
2704 || XINT (it->w->hscroll)
2705 || (! WINDOW_FULL_WIDTH_P (it->w)
2706 && ((!NILP (Vtruncate_partial_width_windows)
2707 && !INTEGERP (Vtruncate_partial_width_windows))
2708 || (INTEGERP (Vtruncate_partial_width_windows)
2709 && (WINDOW_TOTAL_COLS (it->w)
2710 < XINT (Vtruncate_partial_width_windows))))))
2711 it->line_wrap = TRUNCATE;
2712 else if (NILP (current_buffer->truncate_lines))
2713 it->line_wrap = NILP (current_buffer->word_wrap)
2714 ? WINDOW_WRAP : WORD_WRAP;
2715 else
2716 it->line_wrap = TRUNCATE;
2717
2718 /* Get dimensions of truncation and continuation glyphs. These are
2719 displayed as fringe bitmaps under X, so we don't need them for such
2720 frames. */
2721 if (!FRAME_WINDOW_P (it->f))
2722 {
2723 if (it->line_wrap == TRUNCATE)
2724 {
2725 /* We will need the truncation glyph. */
2726 xassert (it->glyph_row == NULL);
2727 produce_special_glyphs (it, IT_TRUNCATION);
2728 it->truncation_pixel_width = it->pixel_width;
2729 }
2730 else
2731 {
2732 /* We will need the continuation glyph. */
2733 xassert (it->glyph_row == NULL);
2734 produce_special_glyphs (it, IT_CONTINUATION);
2735 it->continuation_pixel_width = it->pixel_width;
2736 }
2737
2738 /* Reset these values to zero because the produce_special_glyphs
2739 above has changed them. */
2740 it->pixel_width = it->ascent = it->descent = 0;
2741 it->phys_ascent = it->phys_descent = 0;
2742 }
2743
2744 /* Set this after getting the dimensions of truncation and
2745 continuation glyphs, so that we don't produce glyphs when calling
2746 produce_special_glyphs, above. */
2747 it->glyph_row = row;
2748 it->area = TEXT_AREA;
2749
2750 /* Get the dimensions of the display area. The display area
2751 consists of the visible window area plus a horizontally scrolled
2752 part to the left of the window. All x-values are relative to the
2753 start of this total display area. */
2754 if (base_face_id != DEFAULT_FACE_ID)
2755 {
2756 /* Mode lines, menu bar in terminal frames. */
2757 it->first_visible_x = 0;
2758 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2759 }
2760 else
2761 {
2762 it->first_visible_x
2763 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2764 it->last_visible_x = (it->first_visible_x
2765 + window_box_width (w, TEXT_AREA));
2766
2767 /* If we truncate lines, leave room for the truncator glyph(s) at
2768 the right margin. Otherwise, leave room for the continuation
2769 glyph(s). Truncation and continuation glyphs are not inserted
2770 for window-based redisplay. */
2771 if (!FRAME_WINDOW_P (it->f))
2772 {
2773 if (it->line_wrap == TRUNCATE)
2774 it->last_visible_x -= it->truncation_pixel_width;
2775 else
2776 it->last_visible_x -= it->continuation_pixel_width;
2777 }
2778
2779 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2780 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2781 }
2782
2783 /* Leave room for a border glyph. */
2784 if (!FRAME_WINDOW_P (it->f)
2785 && !WINDOW_RIGHTMOST_P (it->w))
2786 it->last_visible_x -= 1;
2787
2788 it->last_visible_y = window_text_bottom_y (w);
2789
2790 /* For mode lines and alike, arrange for the first glyph having a
2791 left box line if the face specifies a box. */
2792 if (base_face_id != DEFAULT_FACE_ID)
2793 {
2794 struct face *face;
2795
2796 it->face_id = remapped_base_face_id;
2797
2798 /* If we have a boxed mode line, make the first character appear
2799 with a left box line. */
2800 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2801 if (face->box != FACE_NO_BOX)
2802 it->start_of_box_run_p = 1;
2803 }
2804
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2808 {
2809 it->end_charpos = ZV;
2810 it->face_id = -1;
2811 IT_CHARPOS (*it) = charpos;
2812
2813 /* Compute byte position if not specified. */
2814 if (bytepos < charpos)
2815 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2816 else
2817 IT_BYTEPOS (*it) = bytepos;
2818
2819 it->start = it->current;
2820
2821 /* Compute faces etc. */
2822 reseat (it, it->current.pos, 1);
2823 }
2824
2825 CHECK_IT (it);
2826 }
2827
2828
2829 /* Initialize IT for the display of window W with window start POS. */
2830
2831 void
2832 start_display (it, w, pos)
2833 struct it *it;
2834 struct window *w;
2835 struct text_pos pos;
2836 {
2837 struct glyph_row *row;
2838 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2839
2840 row = w->desired_matrix->rows + first_vpos;
2841 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2842 it->first_vpos = first_vpos;
2843
2844 /* Don't reseat to previous visible line start if current start
2845 position is in a string or image. */
2846 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2847 {
2848 int start_at_line_beg_p;
2849 int first_y = it->current_y;
2850
2851 /* If window start is not at a line start, skip forward to POS to
2852 get the correct continuation lines width. */
2853 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2854 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2855 if (!start_at_line_beg_p)
2856 {
2857 int new_x;
2858
2859 reseat_at_previous_visible_line_start (it);
2860 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2861
2862 new_x = it->current_x + it->pixel_width;
2863
2864 /* If lines are continued, this line may end in the middle
2865 of a multi-glyph character (e.g. a control character
2866 displayed as \003, or in the middle of an overlay
2867 string). In this case move_it_to above will not have
2868 taken us to the start of the continuation line but to the
2869 end of the continued line. */
2870 if (it->current_x > 0
2871 && it->line_wrap != TRUNCATE /* Lines are continued. */
2872 && (/* And glyph doesn't fit on the line. */
2873 new_x > it->last_visible_x
2874 /* Or it fits exactly and we're on a window
2875 system frame. */
2876 || (new_x == it->last_visible_x
2877 && FRAME_WINDOW_P (it->f))))
2878 {
2879 if (it->current.dpvec_index >= 0
2880 || it->current.overlay_string_index >= 0)
2881 {
2882 set_iterator_to_next (it, 1);
2883 move_it_in_display_line_to (it, -1, -1, 0);
2884 }
2885
2886 it->continuation_lines_width += it->current_x;
2887 }
2888
2889 /* We're starting a new display line, not affected by the
2890 height of the continued line, so clear the appropriate
2891 fields in the iterator structure. */
2892 it->max_ascent = it->max_descent = 0;
2893 it->max_phys_ascent = it->max_phys_descent = 0;
2894
2895 it->current_y = first_y;
2896 it->vpos = 0;
2897 it->current_x = it->hpos = 0;
2898 }
2899 }
2900 }
2901
2902
2903 /* Return 1 if POS is a position in ellipses displayed for invisible
2904 text. W is the window we display, for text property lookup. */
2905
2906 static int
2907 in_ellipses_for_invisible_text_p (pos, w)
2908 struct display_pos *pos;
2909 struct window *w;
2910 {
2911 Lisp_Object prop, window;
2912 int ellipses_p = 0;
2913 int charpos = CHARPOS (pos->pos);
2914
2915 /* If POS specifies a position in a display vector, this might
2916 be for an ellipsis displayed for invisible text. We won't
2917 get the iterator set up for delivering that ellipsis unless
2918 we make sure that it gets aware of the invisible text. */
2919 if (pos->dpvec_index >= 0
2920 && pos->overlay_string_index < 0
2921 && CHARPOS (pos->string_pos) < 0
2922 && charpos > BEGV
2923 && (XSETWINDOW (window, w),
2924 prop = Fget_char_property (make_number (charpos),
2925 Qinvisible, window),
2926 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2927 {
2928 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2929 window);
2930 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2931 }
2932
2933 return ellipses_p;
2934 }
2935
2936
2937 /* Initialize IT for stepping through current_buffer in window W,
2938 starting at position POS that includes overlay string and display
2939 vector/ control character translation position information. Value
2940 is zero if there are overlay strings with newlines at POS. */
2941
2942 static int
2943 init_from_display_pos (it, w, pos)
2944 struct it *it;
2945 struct window *w;
2946 struct display_pos *pos;
2947 {
2948 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2949 int i, overlay_strings_with_newlines = 0;
2950
2951 /* If POS specifies a position in a display vector, this might
2952 be for an ellipsis displayed for invisible text. We won't
2953 get the iterator set up for delivering that ellipsis unless
2954 we make sure that it gets aware of the invisible text. */
2955 if (in_ellipses_for_invisible_text_p (pos, w))
2956 {
2957 --charpos;
2958 bytepos = 0;
2959 }
2960
2961 /* Keep in mind: the call to reseat in init_iterator skips invisible
2962 text, so we might end up at a position different from POS. This
2963 is only a problem when POS is a row start after a newline and an
2964 overlay starts there with an after-string, and the overlay has an
2965 invisible property. Since we don't skip invisible text in
2966 display_line and elsewhere immediately after consuming the
2967 newline before the row start, such a POS will not be in a string,
2968 but the call to init_iterator below will move us to the
2969 after-string. */
2970 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2971
2972 /* This only scans the current chunk -- it should scan all chunks.
2973 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2974 to 16 in 22.1 to make this a lesser problem. */
2975 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2976 {
2977 const char *s = SDATA (it->overlay_strings[i]);
2978 const char *e = s + SBYTES (it->overlay_strings[i]);
2979
2980 while (s < e && *s != '\n')
2981 ++s;
2982
2983 if (s < e)
2984 {
2985 overlay_strings_with_newlines = 1;
2986 break;
2987 }
2988 }
2989
2990 /* If position is within an overlay string, set up IT to the right
2991 overlay string. */
2992 if (pos->overlay_string_index >= 0)
2993 {
2994 int relative_index;
2995
2996 /* If the first overlay string happens to have a `display'
2997 property for an image, the iterator will be set up for that
2998 image, and we have to undo that setup first before we can
2999 correct the overlay string index. */
3000 if (it->method == GET_FROM_IMAGE)
3001 pop_it (it);
3002
3003 /* We already have the first chunk of overlay strings in
3004 IT->overlay_strings. Load more until the one for
3005 pos->overlay_string_index is in IT->overlay_strings. */
3006 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3007 {
3008 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3009 it->current.overlay_string_index = 0;
3010 while (n--)
3011 {
3012 load_overlay_strings (it, 0);
3013 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3014 }
3015 }
3016
3017 it->current.overlay_string_index = pos->overlay_string_index;
3018 relative_index = (it->current.overlay_string_index
3019 % OVERLAY_STRING_CHUNK_SIZE);
3020 it->string = it->overlay_strings[relative_index];
3021 xassert (STRINGP (it->string));
3022 it->current.string_pos = pos->string_pos;
3023 it->method = GET_FROM_STRING;
3024 }
3025
3026 if (CHARPOS (pos->string_pos) >= 0)
3027 {
3028 /* Recorded position is not in an overlay string, but in another
3029 string. This can only be a string from a `display' property.
3030 IT should already be filled with that string. */
3031 it->current.string_pos = pos->string_pos;
3032 xassert (STRINGP (it->string));
3033 }
3034
3035 /* Restore position in display vector translations, control
3036 character translations or ellipses. */
3037 if (pos->dpvec_index >= 0)
3038 {
3039 if (it->dpvec == NULL)
3040 get_next_display_element (it);
3041 xassert (it->dpvec && it->current.dpvec_index == 0);
3042 it->current.dpvec_index = pos->dpvec_index;
3043 }
3044
3045 CHECK_IT (it);
3046 return !overlay_strings_with_newlines;
3047 }
3048
3049
3050 /* Initialize IT for stepping through current_buffer in window W
3051 starting at ROW->start. */
3052
3053 static void
3054 init_to_row_start (it, w, row)
3055 struct it *it;
3056 struct window *w;
3057 struct glyph_row *row;
3058 {
3059 init_from_display_pos (it, w, &row->start);
3060 it->start = row->start;
3061 it->continuation_lines_width = row->continuation_lines_width;
3062 CHECK_IT (it);
3063 }
3064
3065
3066 /* Initialize IT for stepping through current_buffer in window W
3067 starting in the line following ROW, i.e. starting at ROW->end.
3068 Value is zero if there are overlay strings with newlines at ROW's
3069 end position. */
3070
3071 static int
3072 init_to_row_end (it, w, row)
3073 struct it *it;
3074 struct window *w;
3075 struct glyph_row *row;
3076 {
3077 int success = 0;
3078
3079 if (init_from_display_pos (it, w, &row->end))
3080 {
3081 if (row->continued_p)
3082 it->continuation_lines_width
3083 = row->continuation_lines_width + row->pixel_width;
3084 CHECK_IT (it);
3085 success = 1;
3086 }
3087
3088 return success;
3089 }
3090
3091
3092
3093 \f
3094 /***********************************************************************
3095 Text properties
3096 ***********************************************************************/
3097
3098 /* Called when IT reaches IT->stop_charpos. Handle text property and
3099 overlay changes. Set IT->stop_charpos to the next position where
3100 to stop. */
3101
3102 static void
3103 handle_stop (it)
3104 struct it *it;
3105 {
3106 enum prop_handled handled;
3107 int handle_overlay_change_p;
3108 struct props *p;
3109
3110 it->dpvec = NULL;
3111 it->current.dpvec_index = -1;
3112 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3113 it->ignore_overlay_strings_at_pos_p = 0;
3114 it->ellipsis_p = 0;
3115
3116 /* Use face of preceding text for ellipsis (if invisible) */
3117 if (it->selective_display_ellipsis_p)
3118 it->saved_face_id = it->face_id;
3119
3120 do
3121 {
3122 handled = HANDLED_NORMALLY;
3123
3124 /* Call text property handlers. */
3125 for (p = it_props; p->handler; ++p)
3126 {
3127 handled = p->handler (it);
3128
3129 if (handled == HANDLED_RECOMPUTE_PROPS)
3130 break;
3131 else if (handled == HANDLED_RETURN)
3132 {
3133 /* We still want to show before and after strings from
3134 overlays even if the actual buffer text is replaced. */
3135 if (!handle_overlay_change_p
3136 || it->sp > 1
3137 || !get_overlay_strings_1 (it, 0, 0))
3138 {
3139 if (it->ellipsis_p)
3140 setup_for_ellipsis (it, 0);
3141 /* When handling a display spec, we might load an
3142 empty string. In that case, discard it here. We
3143 used to discard it in handle_single_display_spec,
3144 but that causes get_overlay_strings_1, above, to
3145 ignore overlay strings that we must check. */
3146 if (STRINGP (it->string) && !SCHARS (it->string))
3147 pop_it (it);
3148 return;
3149 }
3150 else if (STRINGP (it->string) && !SCHARS (it->string))
3151 pop_it (it);
3152 else
3153 {
3154 it->ignore_overlay_strings_at_pos_p = 1;
3155 it->string_from_display_prop_p = 0;
3156 handle_overlay_change_p = 0;
3157 }
3158 handled = HANDLED_RECOMPUTE_PROPS;
3159 break;
3160 }
3161 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3162 handle_overlay_change_p = 0;
3163 }
3164
3165 if (handled != HANDLED_RECOMPUTE_PROPS)
3166 {
3167 /* Don't check for overlay strings below when set to deliver
3168 characters from a display vector. */
3169 if (it->method == GET_FROM_DISPLAY_VECTOR)
3170 handle_overlay_change_p = 0;
3171
3172 /* Handle overlay changes.
3173 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3174 if it finds overlays. */
3175 if (handle_overlay_change_p)
3176 handled = handle_overlay_change (it);
3177 }
3178
3179 if (it->ellipsis_p)
3180 {
3181 setup_for_ellipsis (it, 0);
3182 break;
3183 }
3184 }
3185 while (handled == HANDLED_RECOMPUTE_PROPS);
3186
3187 /* Determine where to stop next. */
3188 if (handled == HANDLED_NORMALLY)
3189 compute_stop_pos (it);
3190 }
3191
3192
3193 /* Compute IT->stop_charpos from text property and overlay change
3194 information for IT's current position. */
3195
3196 static void
3197 compute_stop_pos (it)
3198 struct it *it;
3199 {
3200 register INTERVAL iv, next_iv;
3201 Lisp_Object object, limit, position;
3202 EMACS_INT charpos, bytepos;
3203
3204 /* If nowhere else, stop at the end. */
3205 it->stop_charpos = it->end_charpos;
3206
3207 if (STRINGP (it->string))
3208 {
3209 /* Strings are usually short, so don't limit the search for
3210 properties. */
3211 object = it->string;
3212 limit = Qnil;
3213 charpos = IT_STRING_CHARPOS (*it);
3214 bytepos = IT_STRING_BYTEPOS (*it);
3215 }
3216 else
3217 {
3218 EMACS_INT pos;
3219
3220 /* If next overlay change is in front of the current stop pos
3221 (which is IT->end_charpos), stop there. Note: value of
3222 next_overlay_change is point-max if no overlay change
3223 follows. */
3224 charpos = IT_CHARPOS (*it);
3225 bytepos = IT_BYTEPOS (*it);
3226 pos = next_overlay_change (charpos);
3227 if (pos < it->stop_charpos)
3228 it->stop_charpos = pos;
3229
3230 /* If showing the region, we have to stop at the region
3231 start or end because the face might change there. */
3232 if (it->region_beg_charpos > 0)
3233 {
3234 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3235 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3236 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3237 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3238 }
3239
3240 /* Set up variables for computing the stop position from text
3241 property changes. */
3242 XSETBUFFER (object, current_buffer);
3243 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3244 }
3245
3246 /* Get the interval containing IT's position. Value is a null
3247 interval if there isn't such an interval. */
3248 position = make_number (charpos);
3249 iv = validate_interval_range (object, &position, &position, 0);
3250 if (!NULL_INTERVAL_P (iv))
3251 {
3252 Lisp_Object values_here[LAST_PROP_IDX];
3253 struct props *p;
3254
3255 /* Get properties here. */
3256 for (p = it_props; p->handler; ++p)
3257 values_here[p->idx] = textget (iv->plist, *p->name);
3258
3259 /* Look for an interval following iv that has different
3260 properties. */
3261 for (next_iv = next_interval (iv);
3262 (!NULL_INTERVAL_P (next_iv)
3263 && (NILP (limit)
3264 || XFASTINT (limit) > next_iv->position));
3265 next_iv = next_interval (next_iv))
3266 {
3267 for (p = it_props; p->handler; ++p)
3268 {
3269 Lisp_Object new_value;
3270
3271 new_value = textget (next_iv->plist, *p->name);
3272 if (!EQ (values_here[p->idx], new_value))
3273 break;
3274 }
3275
3276 if (p->handler)
3277 break;
3278 }
3279
3280 if (!NULL_INTERVAL_P (next_iv))
3281 {
3282 if (INTEGERP (limit)
3283 && next_iv->position >= XFASTINT (limit))
3284 /* No text property change up to limit. */
3285 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3286 else
3287 /* Text properties change in next_iv. */
3288 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3289 }
3290 }
3291
3292 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3293 it->stop_charpos, it->string);
3294
3295 xassert (STRINGP (it->string)
3296 || (it->stop_charpos >= BEGV
3297 && it->stop_charpos >= IT_CHARPOS (*it)));
3298 }
3299
3300
3301 /* Return the position of the next overlay change after POS in
3302 current_buffer. Value is point-max if no overlay change
3303 follows. This is like `next-overlay-change' but doesn't use
3304 xmalloc. */
3305
3306 static EMACS_INT
3307 next_overlay_change (pos)
3308 EMACS_INT pos;
3309 {
3310 int noverlays;
3311 EMACS_INT endpos;
3312 Lisp_Object *overlays;
3313 int i;
3314
3315 /* Get all overlays at the given position. */
3316 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3317
3318 /* If any of these overlays ends before endpos,
3319 use its ending point instead. */
3320 for (i = 0; i < noverlays; ++i)
3321 {
3322 Lisp_Object oend;
3323 EMACS_INT oendpos;
3324
3325 oend = OVERLAY_END (overlays[i]);
3326 oendpos = OVERLAY_POSITION (oend);
3327 endpos = min (endpos, oendpos);
3328 }
3329
3330 return endpos;
3331 }
3332
3333
3334 \f
3335 /***********************************************************************
3336 Fontification
3337 ***********************************************************************/
3338
3339 /* Handle changes in the `fontified' property of the current buffer by
3340 calling hook functions from Qfontification_functions to fontify
3341 regions of text. */
3342
3343 static enum prop_handled
3344 handle_fontified_prop (it)
3345 struct it *it;
3346 {
3347 Lisp_Object prop, pos;
3348 enum prop_handled handled = HANDLED_NORMALLY;
3349
3350 if (!NILP (Vmemory_full))
3351 return handled;
3352
3353 /* Get the value of the `fontified' property at IT's current buffer
3354 position. (The `fontified' property doesn't have a special
3355 meaning in strings.) If the value is nil, call functions from
3356 Qfontification_functions. */
3357 if (!STRINGP (it->string)
3358 && it->s == NULL
3359 && !NILP (Vfontification_functions)
3360 && !NILP (Vrun_hooks)
3361 && (pos = make_number (IT_CHARPOS (*it)),
3362 prop = Fget_char_property (pos, Qfontified, Qnil),
3363 /* Ignore the special cased nil value always present at EOB since
3364 no amount of fontifying will be able to change it. */
3365 NILP (prop) && IT_CHARPOS (*it) < Z))
3366 {
3367 int count = SPECPDL_INDEX ();
3368 Lisp_Object val;
3369
3370 val = Vfontification_functions;
3371 specbind (Qfontification_functions, Qnil);
3372
3373 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3374 safe_call1 (val, pos);
3375 else
3376 {
3377 Lisp_Object globals, fn;
3378 struct gcpro gcpro1, gcpro2;
3379
3380 globals = Qnil;
3381 GCPRO2 (val, globals);
3382
3383 for (; CONSP (val); val = XCDR (val))
3384 {
3385 fn = XCAR (val);
3386
3387 if (EQ (fn, Qt))
3388 {
3389 /* A value of t indicates this hook has a local
3390 binding; it means to run the global binding too.
3391 In a global value, t should not occur. If it
3392 does, we must ignore it to avoid an endless
3393 loop. */
3394 for (globals = Fdefault_value (Qfontification_functions);
3395 CONSP (globals);
3396 globals = XCDR (globals))
3397 {
3398 fn = XCAR (globals);
3399 if (!EQ (fn, Qt))
3400 safe_call1 (fn, pos);
3401 }
3402 }
3403 else
3404 safe_call1 (fn, pos);
3405 }
3406
3407 UNGCPRO;
3408 }
3409
3410 unbind_to (count, Qnil);
3411
3412 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3413 something. This avoids an endless loop if they failed to
3414 fontify the text for which reason ever. */
3415 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3416 handled = HANDLED_RECOMPUTE_PROPS;
3417 }
3418
3419 return handled;
3420 }
3421
3422
3423 \f
3424 /***********************************************************************
3425 Faces
3426 ***********************************************************************/
3427
3428 /* Set up iterator IT from face properties at its current position.
3429 Called from handle_stop. */
3430
3431 static enum prop_handled
3432 handle_face_prop (it)
3433 struct it *it;
3434 {
3435 int new_face_id;
3436 EMACS_INT next_stop;
3437
3438 if (!STRINGP (it->string))
3439 {
3440 new_face_id
3441 = face_at_buffer_position (it->w,
3442 IT_CHARPOS (*it),
3443 it->region_beg_charpos,
3444 it->region_end_charpos,
3445 &next_stop,
3446 (IT_CHARPOS (*it)
3447 + TEXT_PROP_DISTANCE_LIMIT),
3448 0, it->base_face_id);
3449
3450 /* Is this a start of a run of characters with box face?
3451 Caveat: this can be called for a freshly initialized
3452 iterator; face_id is -1 in this case. We know that the new
3453 face will not change until limit, i.e. if the new face has a
3454 box, all characters up to limit will have one. But, as
3455 usual, we don't know whether limit is really the end. */
3456 if (new_face_id != it->face_id)
3457 {
3458 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3459
3460 /* If new face has a box but old face has not, this is
3461 the start of a run of characters with box, i.e. it has
3462 a shadow on the left side. The value of face_id of the
3463 iterator will be -1 if this is the initial call that gets
3464 the face. In this case, we have to look in front of IT's
3465 position and see whether there is a face != new_face_id. */
3466 it->start_of_box_run_p
3467 = (new_face->box != FACE_NO_BOX
3468 && (it->face_id >= 0
3469 || IT_CHARPOS (*it) == BEG
3470 || new_face_id != face_before_it_pos (it)));
3471 it->face_box_p = new_face->box != FACE_NO_BOX;
3472 }
3473 }
3474 else
3475 {
3476 int base_face_id, bufpos;
3477 int i;
3478 Lisp_Object from_overlay
3479 = (it->current.overlay_string_index >= 0
3480 ? it->string_overlays[it->current.overlay_string_index]
3481 : Qnil);
3482
3483 /* See if we got to this string directly or indirectly from
3484 an overlay property. That includes the before-string or
3485 after-string of an overlay, strings in display properties
3486 provided by an overlay, their text properties, etc.
3487
3488 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3489 if (! NILP (from_overlay))
3490 for (i = it->sp - 1; i >= 0; i--)
3491 {
3492 if (it->stack[i].current.overlay_string_index >= 0)
3493 from_overlay
3494 = it->string_overlays[it->stack[i].current.overlay_string_index];
3495 else if (! NILP (it->stack[i].from_overlay))
3496 from_overlay = it->stack[i].from_overlay;
3497
3498 if (!NILP (from_overlay))
3499 break;
3500 }
3501
3502 if (! NILP (from_overlay))
3503 {
3504 bufpos = IT_CHARPOS (*it);
3505 /* For a string from an overlay, the base face depends
3506 only on text properties and ignores overlays. */
3507 base_face_id
3508 = face_for_overlay_string (it->w,
3509 IT_CHARPOS (*it),
3510 it->region_beg_charpos,
3511 it->region_end_charpos,
3512 &next_stop,
3513 (IT_CHARPOS (*it)
3514 + TEXT_PROP_DISTANCE_LIMIT),
3515 0,
3516 from_overlay);
3517 }
3518 else
3519 {
3520 bufpos = 0;
3521
3522 /* For strings from a `display' property, use the face at
3523 IT's current buffer position as the base face to merge
3524 with, so that overlay strings appear in the same face as
3525 surrounding text, unless they specify their own
3526 faces. */
3527 base_face_id = underlying_face_id (it);
3528 }
3529
3530 new_face_id = face_at_string_position (it->w,
3531 it->string,
3532 IT_STRING_CHARPOS (*it),
3533 bufpos,
3534 it->region_beg_charpos,
3535 it->region_end_charpos,
3536 &next_stop,
3537 base_face_id, 0);
3538
3539 /* Is this a start of a run of characters with box? Caveat:
3540 this can be called for a freshly allocated iterator; face_id
3541 is -1 is this case. We know that the new face will not
3542 change until the next check pos, i.e. if the new face has a
3543 box, all characters up to that position will have a
3544 box. But, as usual, we don't know whether that position
3545 is really the end. */
3546 if (new_face_id != it->face_id)
3547 {
3548 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3549 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3550
3551 /* If new face has a box but old face hasn't, this is the
3552 start of a run of characters with box, i.e. it has a
3553 shadow on the left side. */
3554 it->start_of_box_run_p
3555 = new_face->box && (old_face == NULL || !old_face->box);
3556 it->face_box_p = new_face->box != FACE_NO_BOX;
3557 }
3558 }
3559
3560 it->face_id = new_face_id;
3561 return HANDLED_NORMALLY;
3562 }
3563
3564
3565 /* Return the ID of the face ``underlying'' IT's current position,
3566 which is in a string. If the iterator is associated with a
3567 buffer, return the face at IT's current buffer position.
3568 Otherwise, use the iterator's base_face_id. */
3569
3570 static int
3571 underlying_face_id (it)
3572 struct it *it;
3573 {
3574 int face_id = it->base_face_id, i;
3575
3576 xassert (STRINGP (it->string));
3577
3578 for (i = it->sp - 1; i >= 0; --i)
3579 if (NILP (it->stack[i].string))
3580 face_id = it->stack[i].face_id;
3581
3582 return face_id;
3583 }
3584
3585
3586 /* Compute the face one character before or after the current position
3587 of IT. BEFORE_P non-zero means get the face in front of IT's
3588 position. Value is the id of the face. */
3589
3590 static int
3591 face_before_or_after_it_pos (it, before_p)
3592 struct it *it;
3593 int before_p;
3594 {
3595 int face_id, limit;
3596 EMACS_INT next_check_charpos;
3597 struct text_pos pos;
3598
3599 xassert (it->s == NULL);
3600
3601 if (STRINGP (it->string))
3602 {
3603 int bufpos, base_face_id;
3604
3605 /* No face change past the end of the string (for the case
3606 we are padding with spaces). No face change before the
3607 string start. */
3608 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3609 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3610 return it->face_id;
3611
3612 /* Set pos to the position before or after IT's current position. */
3613 if (before_p)
3614 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3615 else
3616 /* For composition, we must check the character after the
3617 composition. */
3618 pos = (it->what == IT_COMPOSITION
3619 ? string_pos (IT_STRING_CHARPOS (*it)
3620 + it->cmp_it.nchars, it->string)
3621 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3622
3623 if (it->current.overlay_string_index >= 0)
3624 bufpos = IT_CHARPOS (*it);
3625 else
3626 bufpos = 0;
3627
3628 base_face_id = underlying_face_id (it);
3629
3630 /* Get the face for ASCII, or unibyte. */
3631 face_id = face_at_string_position (it->w,
3632 it->string,
3633 CHARPOS (pos),
3634 bufpos,
3635 it->region_beg_charpos,
3636 it->region_end_charpos,
3637 &next_check_charpos,
3638 base_face_id, 0);
3639
3640 /* Correct the face for charsets different from ASCII. Do it
3641 for the multibyte case only. The face returned above is
3642 suitable for unibyte text if IT->string is unibyte. */
3643 if (STRING_MULTIBYTE (it->string))
3644 {
3645 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3646 int rest = SBYTES (it->string) - BYTEPOS (pos);
3647 int c, len;
3648 struct face *face = FACE_FROM_ID (it->f, face_id);
3649
3650 c = string_char_and_length (p, rest, &len);
3651 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3652 }
3653 }
3654 else
3655 {
3656 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3657 || (IT_CHARPOS (*it) <= BEGV && before_p))
3658 return it->face_id;
3659
3660 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3661 pos = it->current.pos;
3662
3663 if (before_p)
3664 DEC_TEXT_POS (pos, it->multibyte_p);
3665 else
3666 {
3667 if (it->what == IT_COMPOSITION)
3668 /* For composition, we must check the position after the
3669 composition. */
3670 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3671 else
3672 INC_TEXT_POS (pos, it->multibyte_p);
3673 }
3674
3675 /* Determine face for CHARSET_ASCII, or unibyte. */
3676 face_id = face_at_buffer_position (it->w,
3677 CHARPOS (pos),
3678 it->region_beg_charpos,
3679 it->region_end_charpos,
3680 &next_check_charpos,
3681 limit, 0, -1);
3682
3683 /* Correct the face for charsets different from ASCII. Do it
3684 for the multibyte case only. The face returned above is
3685 suitable for unibyte text if current_buffer is unibyte. */
3686 if (it->multibyte_p)
3687 {
3688 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3689 struct face *face = FACE_FROM_ID (it->f, face_id);
3690 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3691 }
3692 }
3693
3694 return face_id;
3695 }
3696
3697
3698 \f
3699 /***********************************************************************
3700 Invisible text
3701 ***********************************************************************/
3702
3703 /* Set up iterator IT from invisible properties at its current
3704 position. Called from handle_stop. */
3705
3706 static enum prop_handled
3707 handle_invisible_prop (it)
3708 struct it *it;
3709 {
3710 enum prop_handled handled = HANDLED_NORMALLY;
3711
3712 if (STRINGP (it->string))
3713 {
3714 extern Lisp_Object Qinvisible;
3715 Lisp_Object prop, end_charpos, limit, charpos;
3716
3717 /* Get the value of the invisible text property at the
3718 current position. Value will be nil if there is no such
3719 property. */
3720 charpos = make_number (IT_STRING_CHARPOS (*it));
3721 prop = Fget_text_property (charpos, Qinvisible, it->string);
3722
3723 if (!NILP (prop)
3724 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3725 {
3726 handled = HANDLED_RECOMPUTE_PROPS;
3727
3728 /* Get the position at which the next change of the
3729 invisible text property can be found in IT->string.
3730 Value will be nil if the property value is the same for
3731 all the rest of IT->string. */
3732 XSETINT (limit, SCHARS (it->string));
3733 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3734 it->string, limit);
3735
3736 /* Text at current position is invisible. The next
3737 change in the property is at position end_charpos.
3738 Move IT's current position to that position. */
3739 if (INTEGERP (end_charpos)
3740 && XFASTINT (end_charpos) < XFASTINT (limit))
3741 {
3742 struct text_pos old;
3743 old = it->current.string_pos;
3744 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3745 compute_string_pos (&it->current.string_pos, old, it->string);
3746 }
3747 else
3748 {
3749 /* The rest of the string is invisible. If this is an
3750 overlay string, proceed with the next overlay string
3751 or whatever comes and return a character from there. */
3752 if (it->current.overlay_string_index >= 0)
3753 {
3754 next_overlay_string (it);
3755 /* Don't check for overlay strings when we just
3756 finished processing them. */
3757 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3758 }
3759 else
3760 {
3761 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3762 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3763 }
3764 }
3765 }
3766 }
3767 else
3768 {
3769 int invis_p;
3770 EMACS_INT newpos, next_stop, start_charpos;
3771 Lisp_Object pos, prop, overlay;
3772
3773 /* First of all, is there invisible text at this position? */
3774 start_charpos = IT_CHARPOS (*it);
3775 pos = make_number (IT_CHARPOS (*it));
3776 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3777 &overlay);
3778 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3779
3780 /* If we are on invisible text, skip over it. */
3781 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3782 {
3783 /* Record whether we have to display an ellipsis for the
3784 invisible text. */
3785 int display_ellipsis_p = invis_p == 2;
3786
3787 handled = HANDLED_RECOMPUTE_PROPS;
3788
3789 /* Loop skipping over invisible text. The loop is left at
3790 ZV or with IT on the first char being visible again. */
3791 do
3792 {
3793 /* Try to skip some invisible text. Return value is the
3794 position reached which can be equal to IT's position
3795 if there is nothing invisible here. This skips both
3796 over invisible text properties and overlays with
3797 invisible property. */
3798 newpos = skip_invisible (IT_CHARPOS (*it),
3799 &next_stop, ZV, it->window);
3800
3801 /* If we skipped nothing at all we weren't at invisible
3802 text in the first place. If everything to the end of
3803 the buffer was skipped, end the loop. */
3804 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3805 invis_p = 0;
3806 else
3807 {
3808 /* We skipped some characters but not necessarily
3809 all there are. Check if we ended up on visible
3810 text. Fget_char_property returns the property of
3811 the char before the given position, i.e. if we
3812 get invis_p = 0, this means that the char at
3813 newpos is visible. */
3814 pos = make_number (newpos);
3815 prop = Fget_char_property (pos, Qinvisible, it->window);
3816 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3817 }
3818
3819 /* If we ended up on invisible text, proceed to
3820 skip starting with next_stop. */
3821 if (invis_p)
3822 IT_CHARPOS (*it) = next_stop;
3823
3824 /* If there are adjacent invisible texts, don't lose the
3825 second one's ellipsis. */
3826 if (invis_p == 2)
3827 display_ellipsis_p = 1;
3828 }
3829 while (invis_p);
3830
3831 /* The position newpos is now either ZV or on visible text. */
3832 IT_CHARPOS (*it) = newpos;
3833 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3834
3835 /* If there are before-strings at the start of invisible
3836 text, and the text is invisible because of a text
3837 property, arrange to show before-strings because 20.x did
3838 it that way. (If the text is invisible because of an
3839 overlay property instead of a text property, this is
3840 already handled in the overlay code.) */
3841 if (NILP (overlay)
3842 && get_overlay_strings (it, start_charpos))
3843 {
3844 handled = HANDLED_RECOMPUTE_PROPS;
3845 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3846 }
3847 else if (display_ellipsis_p)
3848 {
3849 /* Make sure that the glyphs of the ellipsis will get
3850 correct `charpos' values. If we would not update
3851 it->position here, the glyphs would belong to the
3852 last visible character _before_ the invisible
3853 text, which confuses `set_cursor_from_row'.
3854
3855 We use the last invisible position instead of the
3856 first because this way the cursor is always drawn on
3857 the first "." of the ellipsis, whenever PT is inside
3858 the invisible text. Otherwise the cursor would be
3859 placed _after_ the ellipsis when the point is after the
3860 first invisible character. */
3861 if (!STRINGP (it->object))
3862 {
3863 it->position.charpos = IT_CHARPOS (*it) - 1;
3864 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3865 }
3866 it->ellipsis_p = 1;
3867 /* Let the ellipsis display before
3868 considering any properties of the following char.
3869 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3870 handled = HANDLED_RETURN;
3871 }
3872 }
3873 }
3874
3875 return handled;
3876 }
3877
3878
3879 /* Make iterator IT return `...' next.
3880 Replaces LEN characters from buffer. */
3881
3882 static void
3883 setup_for_ellipsis (it, len)
3884 struct it *it;
3885 int len;
3886 {
3887 /* Use the display table definition for `...'. Invalid glyphs
3888 will be handled by the method returning elements from dpvec. */
3889 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3890 {
3891 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3892 it->dpvec = v->contents;
3893 it->dpend = v->contents + v->size;
3894 }
3895 else
3896 {
3897 /* Default `...'. */
3898 it->dpvec = default_invis_vector;
3899 it->dpend = default_invis_vector + 3;
3900 }
3901
3902 it->dpvec_char_len = len;
3903 it->current.dpvec_index = 0;
3904 it->dpvec_face_id = -1;
3905
3906 /* Remember the current face id in case glyphs specify faces.
3907 IT's face is restored in set_iterator_to_next.
3908 saved_face_id was set to preceding char's face in handle_stop. */
3909 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3910 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3911
3912 it->method = GET_FROM_DISPLAY_VECTOR;
3913 it->ellipsis_p = 1;
3914 }
3915
3916
3917 \f
3918 /***********************************************************************
3919 'display' property
3920 ***********************************************************************/
3921
3922 /* Set up iterator IT from `display' property at its current position.
3923 Called from handle_stop.
3924 We return HANDLED_RETURN if some part of the display property
3925 overrides the display of the buffer text itself.
3926 Otherwise we return HANDLED_NORMALLY. */
3927
3928 static enum prop_handled
3929 handle_display_prop (it)
3930 struct it *it;
3931 {
3932 Lisp_Object prop, object, overlay;
3933 struct text_pos *position;
3934 /* Nonzero if some property replaces the display of the text itself. */
3935 int display_replaced_p = 0;
3936
3937 if (STRINGP (it->string))
3938 {
3939 object = it->string;
3940 position = &it->current.string_pos;
3941 }
3942 else
3943 {
3944 XSETWINDOW (object, it->w);
3945 position = &it->current.pos;
3946 }
3947
3948 /* Reset those iterator values set from display property values. */
3949 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3950 it->space_width = Qnil;
3951 it->font_height = Qnil;
3952 it->voffset = 0;
3953
3954 /* We don't support recursive `display' properties, i.e. string
3955 values that have a string `display' property, that have a string
3956 `display' property etc. */
3957 if (!it->string_from_display_prop_p)
3958 it->area = TEXT_AREA;
3959
3960 prop = get_char_property_and_overlay (make_number (position->charpos),
3961 Qdisplay, object, &overlay);
3962 if (NILP (prop))
3963 return HANDLED_NORMALLY;
3964 /* Now OVERLAY is the overlay that gave us this property, or nil
3965 if it was a text property. */
3966
3967 if (!STRINGP (it->string))
3968 object = it->w->buffer;
3969
3970 if (CONSP (prop)
3971 /* Simple properties. */
3972 && !EQ (XCAR (prop), Qimage)
3973 && !EQ (XCAR (prop), Qspace)
3974 && !EQ (XCAR (prop), Qwhen)
3975 && !EQ (XCAR (prop), Qslice)
3976 && !EQ (XCAR (prop), Qspace_width)
3977 && !EQ (XCAR (prop), Qheight)
3978 && !EQ (XCAR (prop), Qraise)
3979 /* Marginal area specifications. */
3980 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3981 && !EQ (XCAR (prop), Qleft_fringe)
3982 && !EQ (XCAR (prop), Qright_fringe)
3983 && !NILP (XCAR (prop)))
3984 {
3985 for (; CONSP (prop); prop = XCDR (prop))
3986 {
3987 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3988 position, display_replaced_p))
3989 {
3990 display_replaced_p = 1;
3991 /* If some text in a string is replaced, `position' no
3992 longer points to the position of `object'. */
3993 if (STRINGP (object))
3994 break;
3995 }
3996 }
3997 }
3998 else if (VECTORP (prop))
3999 {
4000 int i;
4001 for (i = 0; i < ASIZE (prop); ++i)
4002 if (handle_single_display_spec (it, AREF (prop, i), 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 else
4013 {
4014 if (handle_single_display_spec (it, prop, object, overlay,
4015 position, 0))
4016 display_replaced_p = 1;
4017 }
4018
4019 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4020 }
4021
4022
4023 /* Value is the position of the end of the `display' property starting
4024 at START_POS in OBJECT. */
4025
4026 static struct text_pos
4027 display_prop_end (it, object, start_pos)
4028 struct it *it;
4029 Lisp_Object object;
4030 struct text_pos start_pos;
4031 {
4032 Lisp_Object end;
4033 struct text_pos end_pos;
4034
4035 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4036 Qdisplay, object, Qnil);
4037 CHARPOS (end_pos) = XFASTINT (end);
4038 if (STRINGP (object))
4039 compute_string_pos (&end_pos, start_pos, it->string);
4040 else
4041 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4042
4043 return end_pos;
4044 }
4045
4046
4047 /* Set up IT from a single `display' specification PROP. OBJECT
4048 is the object in which the `display' property was found. *POSITION
4049 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4050 means that we previously saw a display specification which already
4051 replaced text display with something else, for example an image;
4052 we ignore such properties after the first one has been processed.
4053
4054 OVERLAY is the overlay this `display' property came from,
4055 or nil if it was a text property.
4056
4057 If PROP is a `space' or `image' specification, and in some other
4058 cases too, set *POSITION to the position where the `display'
4059 property ends.
4060
4061 Value is non-zero if something was found which replaces the display
4062 of buffer or string text. */
4063
4064 static int
4065 handle_single_display_spec (it, spec, object, overlay, position,
4066 display_replaced_before_p)
4067 struct it *it;
4068 Lisp_Object spec;
4069 Lisp_Object object;
4070 Lisp_Object overlay;
4071 struct text_pos *position;
4072 int display_replaced_before_p;
4073 {
4074 Lisp_Object form;
4075 Lisp_Object location, value;
4076 struct text_pos start_pos, save_pos;
4077 int valid_p;
4078
4079 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4080 If the result is non-nil, use VALUE instead of SPEC. */
4081 form = Qt;
4082 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4083 {
4084 spec = XCDR (spec);
4085 if (!CONSP (spec))
4086 return 0;
4087 form = XCAR (spec);
4088 spec = XCDR (spec);
4089 }
4090
4091 if (!NILP (form) && !EQ (form, Qt))
4092 {
4093 int count = SPECPDL_INDEX ();
4094 struct gcpro gcpro1;
4095
4096 /* Bind `object' to the object having the `display' property, a
4097 buffer or string. Bind `position' to the position in the
4098 object where the property was found, and `buffer-position'
4099 to the current position in the buffer. */
4100 specbind (Qobject, object);
4101 specbind (Qposition, make_number (CHARPOS (*position)));
4102 specbind (Qbuffer_position,
4103 make_number (STRINGP (object)
4104 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4105 GCPRO1 (form);
4106 form = safe_eval (form);
4107 UNGCPRO;
4108 unbind_to (count, Qnil);
4109 }
4110
4111 if (NILP (form))
4112 return 0;
4113
4114 /* Handle `(height HEIGHT)' specifications. */
4115 if (CONSP (spec)
4116 && EQ (XCAR (spec), Qheight)
4117 && CONSP (XCDR (spec)))
4118 {
4119 if (!FRAME_WINDOW_P (it->f))
4120 return 0;
4121
4122 it->font_height = XCAR (XCDR (spec));
4123 if (!NILP (it->font_height))
4124 {
4125 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4126 int new_height = -1;
4127
4128 if (CONSP (it->font_height)
4129 && (EQ (XCAR (it->font_height), Qplus)
4130 || EQ (XCAR (it->font_height), Qminus))
4131 && CONSP (XCDR (it->font_height))
4132 && INTEGERP (XCAR (XCDR (it->font_height))))
4133 {
4134 /* `(+ N)' or `(- N)' where N is an integer. */
4135 int steps = XINT (XCAR (XCDR (it->font_height)));
4136 if (EQ (XCAR (it->font_height), Qplus))
4137 steps = - steps;
4138 it->face_id = smaller_face (it->f, it->face_id, steps);
4139 }
4140 else if (FUNCTIONP (it->font_height))
4141 {
4142 /* Call function with current height as argument.
4143 Value is the new height. */
4144 Lisp_Object height;
4145 height = safe_call1 (it->font_height,
4146 face->lface[LFACE_HEIGHT_INDEX]);
4147 if (NUMBERP (height))
4148 new_height = XFLOATINT (height);
4149 }
4150 else if (NUMBERP (it->font_height))
4151 {
4152 /* Value is a multiple of the canonical char height. */
4153 struct face *face;
4154
4155 face = FACE_FROM_ID (it->f,
4156 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4157 new_height = (XFLOATINT (it->font_height)
4158 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4159 }
4160 else
4161 {
4162 /* Evaluate IT->font_height with `height' bound to the
4163 current specified height to get the new height. */
4164 int count = SPECPDL_INDEX ();
4165
4166 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4167 value = safe_eval (it->font_height);
4168 unbind_to (count, Qnil);
4169
4170 if (NUMBERP (value))
4171 new_height = XFLOATINT (value);
4172 }
4173
4174 if (new_height > 0)
4175 it->face_id = face_with_height (it->f, it->face_id, new_height);
4176 }
4177
4178 return 0;
4179 }
4180
4181 /* Handle `(space-width WIDTH)'. */
4182 if (CONSP (spec)
4183 && EQ (XCAR (spec), Qspace_width)
4184 && CONSP (XCDR (spec)))
4185 {
4186 if (!FRAME_WINDOW_P (it->f))
4187 return 0;
4188
4189 value = XCAR (XCDR (spec));
4190 if (NUMBERP (value) && XFLOATINT (value) > 0)
4191 it->space_width = value;
4192
4193 return 0;
4194 }
4195
4196 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4197 if (CONSP (spec)
4198 && EQ (XCAR (spec), Qslice))
4199 {
4200 Lisp_Object tem;
4201
4202 if (!FRAME_WINDOW_P (it->f))
4203 return 0;
4204
4205 if (tem = XCDR (spec), CONSP (tem))
4206 {
4207 it->slice.x = XCAR (tem);
4208 if (tem = XCDR (tem), CONSP (tem))
4209 {
4210 it->slice.y = XCAR (tem);
4211 if (tem = XCDR (tem), CONSP (tem))
4212 {
4213 it->slice.width = XCAR (tem);
4214 if (tem = XCDR (tem), CONSP (tem))
4215 it->slice.height = XCAR (tem);
4216 }
4217 }
4218 }
4219
4220 return 0;
4221 }
4222
4223 /* Handle `(raise FACTOR)'. */
4224 if (CONSP (spec)
4225 && EQ (XCAR (spec), Qraise)
4226 && CONSP (XCDR (spec)))
4227 {
4228 if (!FRAME_WINDOW_P (it->f))
4229 return 0;
4230
4231 #ifdef HAVE_WINDOW_SYSTEM
4232 value = XCAR (XCDR (spec));
4233 if (NUMBERP (value))
4234 {
4235 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4236 it->voffset = - (XFLOATINT (value)
4237 * (FONT_HEIGHT (face->font)));
4238 }
4239 #endif /* HAVE_WINDOW_SYSTEM */
4240
4241 return 0;
4242 }
4243
4244 /* Don't handle the other kinds of display specifications
4245 inside a string that we got from a `display' property. */
4246 if (it->string_from_display_prop_p)
4247 return 0;
4248
4249 /* Characters having this form of property are not displayed, so
4250 we have to find the end of the property. */
4251 start_pos = *position;
4252 *position = display_prop_end (it, object, start_pos);
4253 value = Qnil;
4254
4255 /* Stop the scan at that end position--we assume that all
4256 text properties change there. */
4257 it->stop_charpos = position->charpos;
4258
4259 /* Handle `(left-fringe BITMAP [FACE])'
4260 and `(right-fringe BITMAP [FACE])'. */
4261 if (CONSP (spec)
4262 && (EQ (XCAR (spec), Qleft_fringe)
4263 || EQ (XCAR (spec), Qright_fringe))
4264 && CONSP (XCDR (spec)))
4265 {
4266 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4267 int fringe_bitmap;
4268
4269 if (!FRAME_WINDOW_P (it->f))
4270 /* If we return here, POSITION has been advanced
4271 across the text with this property. */
4272 return 0;
4273
4274 #ifdef HAVE_WINDOW_SYSTEM
4275 value = XCAR (XCDR (spec));
4276 if (!SYMBOLP (value)
4277 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4278 /* If we return here, POSITION has been advanced
4279 across the text with this property. */
4280 return 0;
4281
4282 if (CONSP (XCDR (XCDR (spec))))
4283 {
4284 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4285 int face_id2 = lookup_derived_face (it->f, face_name,
4286 FRINGE_FACE_ID, 0);
4287 if (face_id2 >= 0)
4288 face_id = face_id2;
4289 }
4290
4291 /* Save current settings of IT so that we can restore them
4292 when we are finished with the glyph property value. */
4293
4294 save_pos = it->position;
4295 it->position = *position;
4296 push_it (it);
4297 it->position = save_pos;
4298
4299 it->area = TEXT_AREA;
4300 it->what = IT_IMAGE;
4301 it->image_id = -1; /* no image */
4302 it->position = start_pos;
4303 it->object = NILP (object) ? it->w->buffer : object;
4304 it->method = GET_FROM_IMAGE;
4305 it->from_overlay = Qnil;
4306 it->face_id = face_id;
4307
4308 /* Say that we haven't consumed the characters with
4309 `display' property yet. The call to pop_it in
4310 set_iterator_to_next will clean this up. */
4311 *position = start_pos;
4312
4313 if (EQ (XCAR (spec), Qleft_fringe))
4314 {
4315 it->left_user_fringe_bitmap = fringe_bitmap;
4316 it->left_user_fringe_face_id = face_id;
4317 }
4318 else
4319 {
4320 it->right_user_fringe_bitmap = fringe_bitmap;
4321 it->right_user_fringe_face_id = face_id;
4322 }
4323 #endif /* HAVE_WINDOW_SYSTEM */
4324 return 1;
4325 }
4326
4327 /* Prepare to handle `((margin left-margin) ...)',
4328 `((margin right-margin) ...)' and `((margin nil) ...)'
4329 prefixes for display specifications. */
4330 location = Qunbound;
4331 if (CONSP (spec) && CONSP (XCAR (spec)))
4332 {
4333 Lisp_Object tem;
4334
4335 value = XCDR (spec);
4336 if (CONSP (value))
4337 value = XCAR (value);
4338
4339 tem = XCAR (spec);
4340 if (EQ (XCAR (tem), Qmargin)
4341 && (tem = XCDR (tem),
4342 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4343 (NILP (tem)
4344 || EQ (tem, Qleft_margin)
4345 || EQ (tem, Qright_margin))))
4346 location = tem;
4347 }
4348
4349 if (EQ (location, Qunbound))
4350 {
4351 location = Qnil;
4352 value = spec;
4353 }
4354
4355 /* After this point, VALUE is the property after any
4356 margin prefix has been stripped. It must be a string,
4357 an image specification, or `(space ...)'.
4358
4359 LOCATION specifies where to display: `left-margin',
4360 `right-margin' or nil. */
4361
4362 valid_p = (STRINGP (value)
4363 #ifdef HAVE_WINDOW_SYSTEM
4364 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4365 #endif /* not HAVE_WINDOW_SYSTEM */
4366 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4367
4368 if (valid_p && !display_replaced_before_p)
4369 {
4370 /* Save current settings of IT so that we can restore them
4371 when we are finished with the glyph property value. */
4372 save_pos = it->position;
4373 it->position = *position;
4374 push_it (it);
4375 it->position = save_pos;
4376 it->from_overlay = overlay;
4377
4378 if (NILP (location))
4379 it->area = TEXT_AREA;
4380 else if (EQ (location, Qleft_margin))
4381 it->area = LEFT_MARGIN_AREA;
4382 else
4383 it->area = RIGHT_MARGIN_AREA;
4384
4385 if (STRINGP (value))
4386 {
4387 it->string = value;
4388 it->multibyte_p = STRING_MULTIBYTE (it->string);
4389 it->current.overlay_string_index = -1;
4390 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4391 it->end_charpos = it->string_nchars = SCHARS (it->string);
4392 it->method = GET_FROM_STRING;
4393 it->stop_charpos = 0;
4394 it->string_from_display_prop_p = 1;
4395 /* Say that we haven't consumed the characters with
4396 `display' property yet. The call to pop_it in
4397 set_iterator_to_next will clean this up. */
4398 if (BUFFERP (object))
4399 *position = start_pos;
4400 }
4401 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4402 {
4403 it->method = GET_FROM_STRETCH;
4404 it->object = value;
4405 *position = it->position = start_pos;
4406 }
4407 #ifdef HAVE_WINDOW_SYSTEM
4408 else
4409 {
4410 it->what = IT_IMAGE;
4411 it->image_id = lookup_image (it->f, value);
4412 it->position = start_pos;
4413 it->object = NILP (object) ? it->w->buffer : object;
4414 it->method = GET_FROM_IMAGE;
4415
4416 /* Say that we haven't consumed the characters with
4417 `display' property yet. The call to pop_it in
4418 set_iterator_to_next will clean this up. */
4419 *position = start_pos;
4420 }
4421 #endif /* HAVE_WINDOW_SYSTEM */
4422
4423 return 1;
4424 }
4425
4426 /* Invalid property or property not supported. Restore
4427 POSITION to what it was before. */
4428 *position = start_pos;
4429 return 0;
4430 }
4431
4432
4433 /* Check if SPEC is a display sub-property value whose text should be
4434 treated as intangible. */
4435
4436 static int
4437 single_display_spec_intangible_p (prop)
4438 Lisp_Object prop;
4439 {
4440 /* Skip over `when FORM'. */
4441 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4442 {
4443 prop = XCDR (prop);
4444 if (!CONSP (prop))
4445 return 0;
4446 prop = XCDR (prop);
4447 }
4448
4449 if (STRINGP (prop))
4450 return 1;
4451
4452 if (!CONSP (prop))
4453 return 0;
4454
4455 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4456 we don't need to treat text as intangible. */
4457 if (EQ (XCAR (prop), Qmargin))
4458 {
4459 prop = XCDR (prop);
4460 if (!CONSP (prop))
4461 return 0;
4462
4463 prop = XCDR (prop);
4464 if (!CONSP (prop)
4465 || EQ (XCAR (prop), Qleft_margin)
4466 || EQ (XCAR (prop), Qright_margin))
4467 return 0;
4468 }
4469
4470 return (CONSP (prop)
4471 && (EQ (XCAR (prop), Qimage)
4472 || EQ (XCAR (prop), Qspace)));
4473 }
4474
4475
4476 /* Check if PROP is a display property value whose text should be
4477 treated as intangible. */
4478
4479 int
4480 display_prop_intangible_p (prop)
4481 Lisp_Object prop;
4482 {
4483 if (CONSP (prop)
4484 && CONSP (XCAR (prop))
4485 && !EQ (Qmargin, XCAR (XCAR (prop))))
4486 {
4487 /* A list of sub-properties. */
4488 while (CONSP (prop))
4489 {
4490 if (single_display_spec_intangible_p (XCAR (prop)))
4491 return 1;
4492 prop = XCDR (prop);
4493 }
4494 }
4495 else if (VECTORP (prop))
4496 {
4497 /* A vector of sub-properties. */
4498 int i;
4499 for (i = 0; i < ASIZE (prop); ++i)
4500 if (single_display_spec_intangible_p (AREF (prop, i)))
4501 return 1;
4502 }
4503 else
4504 return single_display_spec_intangible_p (prop);
4505
4506 return 0;
4507 }
4508
4509
4510 /* Return 1 if PROP is a display sub-property value containing STRING. */
4511
4512 static int
4513 single_display_spec_string_p (prop, string)
4514 Lisp_Object prop, string;
4515 {
4516 if (EQ (string, prop))
4517 return 1;
4518
4519 /* Skip over `when FORM'. */
4520 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4521 {
4522 prop = XCDR (prop);
4523 if (!CONSP (prop))
4524 return 0;
4525 prop = XCDR (prop);
4526 }
4527
4528 if (CONSP (prop))
4529 /* Skip over `margin LOCATION'. */
4530 if (EQ (XCAR (prop), Qmargin))
4531 {
4532 prop = XCDR (prop);
4533 if (!CONSP (prop))
4534 return 0;
4535
4536 prop = XCDR (prop);
4537 if (!CONSP (prop))
4538 return 0;
4539 }
4540
4541 return CONSP (prop) && EQ (XCAR (prop), string);
4542 }
4543
4544
4545 /* Return 1 if STRING appears in the `display' property PROP. */
4546
4547 static int
4548 display_prop_string_p (prop, string)
4549 Lisp_Object prop, string;
4550 {
4551 if (CONSP (prop)
4552 && CONSP (XCAR (prop))
4553 && !EQ (Qmargin, XCAR (XCAR (prop))))
4554 {
4555 /* A list of sub-properties. */
4556 while (CONSP (prop))
4557 {
4558 if (single_display_spec_string_p (XCAR (prop), string))
4559 return 1;
4560 prop = XCDR (prop);
4561 }
4562 }
4563 else if (VECTORP (prop))
4564 {
4565 /* A vector of sub-properties. */
4566 int i;
4567 for (i = 0; i < ASIZE (prop); ++i)
4568 if (single_display_spec_string_p (AREF (prop, i), string))
4569 return 1;
4570 }
4571 else
4572 return single_display_spec_string_p (prop, string);
4573
4574 return 0;
4575 }
4576
4577
4578 /* Determine from which buffer position in W's buffer STRING comes
4579 from. AROUND_CHARPOS is an approximate position where it could
4580 be from. Value is the buffer position or 0 if it couldn't be
4581 determined.
4582
4583 W's buffer must be current.
4584
4585 This function is necessary because we don't record buffer positions
4586 in glyphs generated from strings (to keep struct glyph small).
4587 This function may only use code that doesn't eval because it is
4588 called asynchronously from note_mouse_highlight. */
4589
4590 int
4591 string_buffer_position (w, string, around_charpos)
4592 struct window *w;
4593 Lisp_Object string;
4594 int around_charpos;
4595 {
4596 Lisp_Object limit, prop, pos;
4597 const int MAX_DISTANCE = 1000;
4598 int found = 0;
4599
4600 pos = make_number (around_charpos);
4601 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4602 while (!found && !EQ (pos, limit))
4603 {
4604 prop = Fget_char_property (pos, Qdisplay, Qnil);
4605 if (!NILP (prop) && display_prop_string_p (prop, string))
4606 found = 1;
4607 else
4608 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4609 }
4610
4611 if (!found)
4612 {
4613 pos = make_number (around_charpos);
4614 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4615 while (!found && !EQ (pos, limit))
4616 {
4617 prop = Fget_char_property (pos, Qdisplay, Qnil);
4618 if (!NILP (prop) && display_prop_string_p (prop, string))
4619 found = 1;
4620 else
4621 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4622 limit);
4623 }
4624 }
4625
4626 return found ? XINT (pos) : 0;
4627 }
4628
4629
4630 \f
4631 /***********************************************************************
4632 `composition' property
4633 ***********************************************************************/
4634
4635 /* Set up iterator IT from `composition' property at its current
4636 position. Called from handle_stop. */
4637
4638 static enum prop_handled
4639 handle_composition_prop (it)
4640 struct it *it;
4641 {
4642 Lisp_Object prop, string;
4643 EMACS_INT pos, pos_byte, start, end;
4644
4645 if (STRINGP (it->string))
4646 {
4647 unsigned char *s;
4648
4649 pos = IT_STRING_CHARPOS (*it);
4650 pos_byte = IT_STRING_BYTEPOS (*it);
4651 string = it->string;
4652 s = SDATA (string) + pos_byte;
4653 it->c = STRING_CHAR (s, 0);
4654 }
4655 else
4656 {
4657 pos = IT_CHARPOS (*it);
4658 pos_byte = IT_BYTEPOS (*it);
4659 string = Qnil;
4660 it->c = FETCH_CHAR (pos_byte);
4661 }
4662
4663 /* If there's a valid composition and point is not inside of the
4664 composition (in the case that the composition is from the current
4665 buffer), draw a glyph composed from the composition components. */
4666 if (find_composition (pos, -1, &start, &end, &prop, string)
4667 && COMPOSITION_VALID_P (start, end, prop)
4668 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4669 {
4670 if (start != pos)
4671 {
4672 if (STRINGP (it->string))
4673 pos_byte = string_char_to_byte (it->string, start);
4674 else
4675 pos_byte = CHAR_TO_BYTE (start);
4676 }
4677 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4678 prop, string);
4679
4680 if (it->cmp_it.id >= 0)
4681 {
4682 it->cmp_it.ch = -1;
4683 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4684 it->cmp_it.nglyphs = -1;
4685 }
4686 }
4687
4688 return HANDLED_NORMALLY;
4689 }
4690
4691
4692 \f
4693 /***********************************************************************
4694 Overlay strings
4695 ***********************************************************************/
4696
4697 /* The following structure is used to record overlay strings for
4698 later sorting in load_overlay_strings. */
4699
4700 struct overlay_entry
4701 {
4702 Lisp_Object overlay;
4703 Lisp_Object string;
4704 int priority;
4705 int after_string_p;
4706 };
4707
4708
4709 /* Set up iterator IT from overlay strings at its current position.
4710 Called from handle_stop. */
4711
4712 static enum prop_handled
4713 handle_overlay_change (it)
4714 struct it *it;
4715 {
4716 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4717 return HANDLED_RECOMPUTE_PROPS;
4718 else
4719 return HANDLED_NORMALLY;
4720 }
4721
4722
4723 /* Set up the next overlay string for delivery by IT, if there is an
4724 overlay string to deliver. Called by set_iterator_to_next when the
4725 end of the current overlay string is reached. If there are more
4726 overlay strings to display, IT->string and
4727 IT->current.overlay_string_index are set appropriately here.
4728 Otherwise IT->string is set to nil. */
4729
4730 static void
4731 next_overlay_string (it)
4732 struct it *it;
4733 {
4734 ++it->current.overlay_string_index;
4735 if (it->current.overlay_string_index == it->n_overlay_strings)
4736 {
4737 /* No more overlay strings. Restore IT's settings to what
4738 they were before overlay strings were processed, and
4739 continue to deliver from current_buffer. */
4740
4741 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4742 pop_it (it);
4743 xassert (it->sp > 0
4744 || (NILP (it->string)
4745 && it->method == GET_FROM_BUFFER
4746 && it->stop_charpos >= BEGV
4747 && it->stop_charpos <= it->end_charpos));
4748 it->current.overlay_string_index = -1;
4749 it->n_overlay_strings = 0;
4750
4751 /* If we're at the end of the buffer, record that we have
4752 processed the overlay strings there already, so that
4753 next_element_from_buffer doesn't try it again. */
4754 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4755 it->overlay_strings_at_end_processed_p = 1;
4756 }
4757 else
4758 {
4759 /* There are more overlay strings to process. If
4760 IT->current.overlay_string_index has advanced to a position
4761 where we must load IT->overlay_strings with more strings, do
4762 it. */
4763 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4764
4765 if (it->current.overlay_string_index && i == 0)
4766 load_overlay_strings (it, 0);
4767
4768 /* Initialize IT to deliver display elements from the overlay
4769 string. */
4770 it->string = it->overlay_strings[i];
4771 it->multibyte_p = STRING_MULTIBYTE (it->string);
4772 SET_TEXT_POS (it->current.string_pos, 0, 0);
4773 it->method = GET_FROM_STRING;
4774 it->stop_charpos = 0;
4775 if (it->cmp_it.stop_pos >= 0)
4776 it->cmp_it.stop_pos = 0;
4777 }
4778
4779 CHECK_IT (it);
4780 }
4781
4782
4783 /* Compare two overlay_entry structures E1 and E2. Used as a
4784 comparison function for qsort in load_overlay_strings. Overlay
4785 strings for the same position are sorted so that
4786
4787 1. All after-strings come in front of before-strings, except
4788 when they come from the same overlay.
4789
4790 2. Within after-strings, strings are sorted so that overlay strings
4791 from overlays with higher priorities come first.
4792
4793 2. Within before-strings, strings are sorted so that overlay
4794 strings from overlays with higher priorities come last.
4795
4796 Value is analogous to strcmp. */
4797
4798
4799 static int
4800 compare_overlay_entries (e1, e2)
4801 void *e1, *e2;
4802 {
4803 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4804 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4805 int result;
4806
4807 if (entry1->after_string_p != entry2->after_string_p)
4808 {
4809 /* Let after-strings appear in front of before-strings if
4810 they come from different overlays. */
4811 if (EQ (entry1->overlay, entry2->overlay))
4812 result = entry1->after_string_p ? 1 : -1;
4813 else
4814 result = entry1->after_string_p ? -1 : 1;
4815 }
4816 else if (entry1->after_string_p)
4817 /* After-strings sorted in order of decreasing priority. */
4818 result = entry2->priority - entry1->priority;
4819 else
4820 /* Before-strings sorted in order of increasing priority. */
4821 result = entry1->priority - entry2->priority;
4822
4823 return result;
4824 }
4825
4826
4827 /* Load the vector IT->overlay_strings with overlay strings from IT's
4828 current buffer position, or from CHARPOS if that is > 0. Set
4829 IT->n_overlays to the total number of overlay strings found.
4830
4831 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4832 a time. On entry into load_overlay_strings,
4833 IT->current.overlay_string_index gives the number of overlay
4834 strings that have already been loaded by previous calls to this
4835 function.
4836
4837 IT->add_overlay_start contains an additional overlay start
4838 position to consider for taking overlay strings from, if non-zero.
4839 This position comes into play when the overlay has an `invisible'
4840 property, and both before and after-strings. When we've skipped to
4841 the end of the overlay, because of its `invisible' property, we
4842 nevertheless want its before-string to appear.
4843 IT->add_overlay_start will contain the overlay start position
4844 in this case.
4845
4846 Overlay strings are sorted so that after-string strings come in
4847 front of before-string strings. Within before and after-strings,
4848 strings are sorted by overlay priority. See also function
4849 compare_overlay_entries. */
4850
4851 static void
4852 load_overlay_strings (it, charpos)
4853 struct it *it;
4854 int charpos;
4855 {
4856 extern Lisp_Object Qwindow, Qpriority;
4857 Lisp_Object overlay, window, str, invisible;
4858 struct Lisp_Overlay *ov;
4859 int start, end;
4860 int size = 20;
4861 int n = 0, i, j, invis_p;
4862 struct overlay_entry *entries
4863 = (struct overlay_entry *) alloca (size * sizeof *entries);
4864
4865 if (charpos <= 0)
4866 charpos = IT_CHARPOS (*it);
4867
4868 /* Append the overlay string STRING of overlay OVERLAY to vector
4869 `entries' which has size `size' and currently contains `n'
4870 elements. AFTER_P non-zero means STRING is an after-string of
4871 OVERLAY. */
4872 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4873 do \
4874 { \
4875 Lisp_Object priority; \
4876 \
4877 if (n == size) \
4878 { \
4879 int new_size = 2 * size; \
4880 struct overlay_entry *old = entries; \
4881 entries = \
4882 (struct overlay_entry *) alloca (new_size \
4883 * sizeof *entries); \
4884 bcopy (old, entries, size * sizeof *entries); \
4885 size = new_size; \
4886 } \
4887 \
4888 entries[n].string = (STRING); \
4889 entries[n].overlay = (OVERLAY); \
4890 priority = Foverlay_get ((OVERLAY), Qpriority); \
4891 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4892 entries[n].after_string_p = (AFTER_P); \
4893 ++n; \
4894 } \
4895 while (0)
4896
4897 /* Process overlay before the overlay center. */
4898 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4899 {
4900 XSETMISC (overlay, ov);
4901 xassert (OVERLAYP (overlay));
4902 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4903 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4904
4905 if (end < charpos)
4906 break;
4907
4908 /* Skip this overlay if it doesn't start or end at IT's current
4909 position. */
4910 if (end != charpos && start != charpos)
4911 continue;
4912
4913 /* Skip this overlay if it doesn't apply to IT->w. */
4914 window = Foverlay_get (overlay, Qwindow);
4915 if (WINDOWP (window) && XWINDOW (window) != it->w)
4916 continue;
4917
4918 /* If the text ``under'' the overlay is invisible, both before-
4919 and after-strings from this overlay are visible; start and
4920 end position are indistinguishable. */
4921 invisible = Foverlay_get (overlay, Qinvisible);
4922 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4923
4924 /* If overlay has a non-empty before-string, record it. */
4925 if ((start == charpos || (end == charpos && invis_p))
4926 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4927 && SCHARS (str))
4928 RECORD_OVERLAY_STRING (overlay, str, 0);
4929
4930 /* If overlay has a non-empty after-string, record it. */
4931 if ((end == charpos || (start == charpos && invis_p))
4932 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4933 && SCHARS (str))
4934 RECORD_OVERLAY_STRING (overlay, str, 1);
4935 }
4936
4937 /* Process overlays after the overlay center. */
4938 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4939 {
4940 XSETMISC (overlay, ov);
4941 xassert (OVERLAYP (overlay));
4942 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4943 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4944
4945 if (start > charpos)
4946 break;
4947
4948 /* Skip this overlay if it doesn't start or end at IT's current
4949 position. */
4950 if (end != charpos && start != charpos)
4951 continue;
4952
4953 /* Skip this overlay if it doesn't apply to IT->w. */
4954 window = Foverlay_get (overlay, Qwindow);
4955 if (WINDOWP (window) && XWINDOW (window) != it->w)
4956 continue;
4957
4958 /* If the text ``under'' the overlay is invisible, it has a zero
4959 dimension, and both before- and after-strings apply. */
4960 invisible = Foverlay_get (overlay, Qinvisible);
4961 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4962
4963 /* If overlay has a non-empty before-string, record it. */
4964 if ((start == charpos || (end == charpos && invis_p))
4965 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4966 && SCHARS (str))
4967 RECORD_OVERLAY_STRING (overlay, str, 0);
4968
4969 /* If overlay has a non-empty after-string, record it. */
4970 if ((end == charpos || (start == charpos && invis_p))
4971 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4972 && SCHARS (str))
4973 RECORD_OVERLAY_STRING (overlay, str, 1);
4974 }
4975
4976 #undef RECORD_OVERLAY_STRING
4977
4978 /* Sort entries. */
4979 if (n > 1)
4980 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4981
4982 /* Record the total number of strings to process. */
4983 it->n_overlay_strings = n;
4984
4985 /* IT->current.overlay_string_index is the number of overlay strings
4986 that have already been consumed by IT. Copy some of the
4987 remaining overlay strings to IT->overlay_strings. */
4988 i = 0;
4989 j = it->current.overlay_string_index;
4990 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4991 {
4992 it->overlay_strings[i] = entries[j].string;
4993 it->string_overlays[i++] = entries[j++].overlay;
4994 }
4995
4996 CHECK_IT (it);
4997 }
4998
4999
5000 /* Get the first chunk of overlay strings at IT's current buffer
5001 position, or at CHARPOS if that is > 0. Value is non-zero if at
5002 least one overlay string was found. */
5003
5004 static int
5005 get_overlay_strings_1 (it, charpos, compute_stop_p)
5006 struct it *it;
5007 int charpos;
5008 int compute_stop_p;
5009 {
5010 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5011 process. This fills IT->overlay_strings with strings, and sets
5012 IT->n_overlay_strings to the total number of strings to process.
5013 IT->pos.overlay_string_index has to be set temporarily to zero
5014 because load_overlay_strings needs this; it must be set to -1
5015 when no overlay strings are found because a zero value would
5016 indicate a position in the first overlay string. */
5017 it->current.overlay_string_index = 0;
5018 load_overlay_strings (it, charpos);
5019
5020 /* If we found overlay strings, set up IT to deliver display
5021 elements from the first one. Otherwise set up IT to deliver
5022 from current_buffer. */
5023 if (it->n_overlay_strings)
5024 {
5025 /* Make sure we know settings in current_buffer, so that we can
5026 restore meaningful values when we're done with the overlay
5027 strings. */
5028 if (compute_stop_p)
5029 compute_stop_pos (it);
5030 xassert (it->face_id >= 0);
5031
5032 /* Save IT's settings. They are restored after all overlay
5033 strings have been processed. */
5034 xassert (!compute_stop_p || it->sp == 0);
5035
5036 /* When called from handle_stop, there might be an empty display
5037 string loaded. In that case, don't bother saving it. */
5038 if (!STRINGP (it->string) || SCHARS (it->string))
5039 push_it (it);
5040
5041 /* Set up IT to deliver display elements from the first overlay
5042 string. */
5043 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5044 it->string = it->overlay_strings[0];
5045 it->from_overlay = Qnil;
5046 it->stop_charpos = 0;
5047 xassert (STRINGP (it->string));
5048 it->end_charpos = SCHARS (it->string);
5049 it->multibyte_p = STRING_MULTIBYTE (it->string);
5050 it->method = GET_FROM_STRING;
5051 return 1;
5052 }
5053
5054 it->current.overlay_string_index = -1;
5055 return 0;
5056 }
5057
5058 static int
5059 get_overlay_strings (it, charpos)
5060 struct it *it;
5061 int charpos;
5062 {
5063 it->string = Qnil;
5064 it->method = GET_FROM_BUFFER;
5065
5066 (void) get_overlay_strings_1 (it, charpos, 1);
5067
5068 CHECK_IT (it);
5069
5070 /* Value is non-zero if we found at least one overlay string. */
5071 return STRINGP (it->string);
5072 }
5073
5074
5075 \f
5076 /***********************************************************************
5077 Saving and restoring state
5078 ***********************************************************************/
5079
5080 /* Save current settings of IT on IT->stack. Called, for example,
5081 before setting up IT for an overlay string, to be able to restore
5082 IT's settings to what they were after the overlay string has been
5083 processed. */
5084
5085 static void
5086 push_it (it)
5087 struct it *it;
5088 {
5089 struct iterator_stack_entry *p;
5090
5091 xassert (it->sp < IT_STACK_SIZE);
5092 p = it->stack + it->sp;
5093
5094 p->stop_charpos = it->stop_charpos;
5095 p->cmp_it = it->cmp_it;
5096 xassert (it->face_id >= 0);
5097 p->face_id = it->face_id;
5098 p->string = it->string;
5099 p->method = it->method;
5100 p->from_overlay = it->from_overlay;
5101 switch (p->method)
5102 {
5103 case GET_FROM_IMAGE:
5104 p->u.image.object = it->object;
5105 p->u.image.image_id = it->image_id;
5106 p->u.image.slice = it->slice;
5107 break;
5108 case GET_FROM_STRETCH:
5109 p->u.stretch.object = it->object;
5110 break;
5111 }
5112 p->position = it->position;
5113 p->current = it->current;
5114 p->end_charpos = it->end_charpos;
5115 p->string_nchars = it->string_nchars;
5116 p->area = it->area;
5117 p->multibyte_p = it->multibyte_p;
5118 p->avoid_cursor_p = it->avoid_cursor_p;
5119 p->space_width = it->space_width;
5120 p->font_height = it->font_height;
5121 p->voffset = it->voffset;
5122 p->string_from_display_prop_p = it->string_from_display_prop_p;
5123 p->display_ellipsis_p = 0;
5124 p->line_wrap = it->line_wrap;
5125 ++it->sp;
5126 }
5127
5128
5129 /* Restore IT's settings from IT->stack. Called, for example, when no
5130 more overlay strings must be processed, and we return to delivering
5131 display elements from a buffer, or when the end of a string from a
5132 `display' property is reached and we return to delivering display
5133 elements from an overlay string, or from a buffer. */
5134
5135 static void
5136 pop_it (it)
5137 struct it *it;
5138 {
5139 struct iterator_stack_entry *p;
5140
5141 xassert (it->sp > 0);
5142 --it->sp;
5143 p = it->stack + it->sp;
5144 it->stop_charpos = p->stop_charpos;
5145 it->cmp_it = p->cmp_it;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 it->from_overlay = p->from_overlay;
5151 if (NILP (it->string))
5152 SET_TEXT_POS (it->current.string_pos, -1, -1);
5153 it->method = p->method;
5154 switch (it->method)
5155 {
5156 case GET_FROM_IMAGE:
5157 it->image_id = p->u.image.image_id;
5158 it->object = p->u.image.object;
5159 it->slice = p->u.image.slice;
5160 break;
5161 case GET_FROM_STRETCH:
5162 it->object = p->u.comp.object;
5163 break;
5164 case GET_FROM_BUFFER:
5165 it->object = it->w->buffer;
5166 break;
5167 case GET_FROM_STRING:
5168 it->object = it->string;
5169 break;
5170 case GET_FROM_DISPLAY_VECTOR:
5171 if (it->s)
5172 it->method = GET_FROM_C_STRING;
5173 else if (STRINGP (it->string))
5174 it->method = GET_FROM_STRING;
5175 else
5176 {
5177 it->method = GET_FROM_BUFFER;
5178 it->object = it->w->buffer;
5179 }
5180 }
5181 it->end_charpos = p->end_charpos;
5182 it->string_nchars = p->string_nchars;
5183 it->area = p->area;
5184 it->multibyte_p = p->multibyte_p;
5185 it->avoid_cursor_p = p->avoid_cursor_p;
5186 it->space_width = p->space_width;
5187 it->font_height = p->font_height;
5188 it->voffset = p->voffset;
5189 it->string_from_display_prop_p = p->string_from_display_prop_p;
5190 it->line_wrap = p->line_wrap;
5191 }
5192
5193
5194 \f
5195 /***********************************************************************
5196 Moving over lines
5197 ***********************************************************************/
5198
5199 /* Set IT's current position to the previous line start. */
5200
5201 static void
5202 back_to_previous_line_start (it)
5203 struct it *it;
5204 {
5205 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5206 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5207 }
5208
5209
5210 /* Move IT to the next line start.
5211
5212 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5213 we skipped over part of the text (as opposed to moving the iterator
5214 continuously over the text). Otherwise, don't change the value
5215 of *SKIPPED_P.
5216
5217 Newlines may come from buffer text, overlay strings, or strings
5218 displayed via the `display' property. That's the reason we can't
5219 simply use find_next_newline_no_quit.
5220
5221 Note that this function may not skip over invisible text that is so
5222 because of text properties and immediately follows a newline. If
5223 it would, function reseat_at_next_visible_line_start, when called
5224 from set_iterator_to_next, would effectively make invisible
5225 characters following a newline part of the wrong glyph row, which
5226 leads to wrong cursor motion. */
5227
5228 static int
5229 forward_to_next_line_start (it, skipped_p)
5230 struct it *it;
5231 int *skipped_p;
5232 {
5233 int old_selective, newline_found_p, n;
5234 const int MAX_NEWLINE_DISTANCE = 500;
5235
5236 /* If already on a newline, just consume it to avoid unintended
5237 skipping over invisible text below. */
5238 if (it->what == IT_CHARACTER
5239 && it->c == '\n'
5240 && CHARPOS (it->position) == IT_CHARPOS (*it))
5241 {
5242 set_iterator_to_next (it, 0);
5243 it->c = 0;
5244 return 1;
5245 }
5246
5247 /* Don't handle selective display in the following. It's (a)
5248 unnecessary because it's done by the caller, and (b) leads to an
5249 infinite recursion because next_element_from_ellipsis indirectly
5250 calls this function. */
5251 old_selective = it->selective;
5252 it->selective = 0;
5253
5254 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5255 from buffer text. */
5256 for (n = newline_found_p = 0;
5257 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5258 n += STRINGP (it->string) ? 0 : 1)
5259 {
5260 if (!get_next_display_element (it))
5261 return 0;
5262 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5263 set_iterator_to_next (it, 0);
5264 }
5265
5266 /* If we didn't find a newline near enough, see if we can use a
5267 short-cut. */
5268 if (!newline_found_p)
5269 {
5270 int start = IT_CHARPOS (*it);
5271 int limit = find_next_newline_no_quit (start, 1);
5272 Lisp_Object pos;
5273
5274 xassert (!STRINGP (it->string));
5275
5276 /* If there isn't any `display' property in sight, and no
5277 overlays, we can just use the position of the newline in
5278 buffer text. */
5279 if (it->stop_charpos >= limit
5280 || ((pos = Fnext_single_property_change (make_number (start),
5281 Qdisplay,
5282 Qnil, make_number (limit)),
5283 NILP (pos))
5284 && next_overlay_change (start) == ZV))
5285 {
5286 IT_CHARPOS (*it) = limit;
5287 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5288 *skipped_p = newline_found_p = 1;
5289 }
5290 else
5291 {
5292 while (get_next_display_element (it)
5293 && !newline_found_p)
5294 {
5295 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5296 set_iterator_to_next (it, 0);
5297 }
5298 }
5299 }
5300
5301 it->selective = old_selective;
5302 return newline_found_p;
5303 }
5304
5305
5306 /* Set IT's current position to the previous visible line start. Skip
5307 invisible text that is so either due to text properties or due to
5308 selective display. Caution: this does not change IT->current_x and
5309 IT->hpos. */
5310
5311 static void
5312 back_to_previous_visible_line_start (it)
5313 struct it *it;
5314 {
5315 while (IT_CHARPOS (*it) > BEGV)
5316 {
5317 back_to_previous_line_start (it);
5318
5319 if (IT_CHARPOS (*it) <= BEGV)
5320 break;
5321
5322 /* If selective > 0, then lines indented more than that values
5323 are invisible. */
5324 if (it->selective > 0
5325 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5326 (double) it->selective)) /* iftc */
5327 continue;
5328
5329 /* Check the newline before point for invisibility. */
5330 {
5331 Lisp_Object prop;
5332 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5333 Qinvisible, it->window);
5334 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5335 continue;
5336 }
5337
5338 if (IT_CHARPOS (*it) <= BEGV)
5339 break;
5340
5341 {
5342 struct it it2;
5343 int pos;
5344 EMACS_INT beg, end;
5345 Lisp_Object val, overlay;
5346
5347 /* If newline is part of a composition, continue from start of composition */
5348 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5349 && beg < IT_CHARPOS (*it))
5350 goto replaced;
5351
5352 /* If newline is replaced by a display property, find start of overlay
5353 or interval and continue search from that point. */
5354 it2 = *it;
5355 pos = --IT_CHARPOS (it2);
5356 --IT_BYTEPOS (it2);
5357 it2.sp = 0;
5358 it2.string_from_display_prop_p = 0;
5359 if (handle_display_prop (&it2) == HANDLED_RETURN
5360 && !NILP (val = get_char_property_and_overlay
5361 (make_number (pos), Qdisplay, Qnil, &overlay))
5362 && (OVERLAYP (overlay)
5363 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5364 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5365 goto replaced;
5366
5367 /* Newline is not replaced by anything -- so we are done. */
5368 break;
5369
5370 replaced:
5371 if (beg < BEGV)
5372 beg = BEGV;
5373 IT_CHARPOS (*it) = beg;
5374 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5375 }
5376 }
5377
5378 it->continuation_lines_width = 0;
5379
5380 xassert (IT_CHARPOS (*it) >= BEGV);
5381 xassert (IT_CHARPOS (*it) == BEGV
5382 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5383 CHECK_IT (it);
5384 }
5385
5386
5387 /* Reseat iterator IT at the previous visible line start. Skip
5388 invisible text that is so either due to text properties or due to
5389 selective display. At the end, update IT's overlay information,
5390 face information etc. */
5391
5392 void
5393 reseat_at_previous_visible_line_start (it)
5394 struct it *it;
5395 {
5396 back_to_previous_visible_line_start (it);
5397 reseat (it, it->current.pos, 1);
5398 CHECK_IT (it);
5399 }
5400
5401
5402 /* Reseat iterator IT on the next visible line start in the current
5403 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5404 preceding the line start. Skip over invisible text that is so
5405 because of selective display. Compute faces, overlays etc at the
5406 new position. Note that this function does not skip over text that
5407 is invisible because of text properties. */
5408
5409 static void
5410 reseat_at_next_visible_line_start (it, on_newline_p)
5411 struct it *it;
5412 int on_newline_p;
5413 {
5414 int newline_found_p, skipped_p = 0;
5415
5416 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5417
5418 /* Skip over lines that are invisible because they are indented
5419 more than the value of IT->selective. */
5420 if (it->selective > 0)
5421 while (IT_CHARPOS (*it) < ZV
5422 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5423 (double) it->selective)) /* iftc */
5424 {
5425 xassert (IT_BYTEPOS (*it) == BEGV
5426 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5427 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5428 }
5429
5430 /* Position on the newline if that's what's requested. */
5431 if (on_newline_p && newline_found_p)
5432 {
5433 if (STRINGP (it->string))
5434 {
5435 if (IT_STRING_CHARPOS (*it) > 0)
5436 {
5437 --IT_STRING_CHARPOS (*it);
5438 --IT_STRING_BYTEPOS (*it);
5439 }
5440 }
5441 else if (IT_CHARPOS (*it) > BEGV)
5442 {
5443 --IT_CHARPOS (*it);
5444 --IT_BYTEPOS (*it);
5445 reseat (it, it->current.pos, 0);
5446 }
5447 }
5448 else if (skipped_p)
5449 reseat (it, it->current.pos, 0);
5450
5451 CHECK_IT (it);
5452 }
5453
5454
5455 \f
5456 /***********************************************************************
5457 Changing an iterator's position
5458 ***********************************************************************/
5459
5460 /* Change IT's current position to POS in current_buffer. If FORCE_P
5461 is non-zero, always check for text properties at the new position.
5462 Otherwise, text properties are only looked up if POS >=
5463 IT->check_charpos of a property. */
5464
5465 static void
5466 reseat (it, pos, force_p)
5467 struct it *it;
5468 struct text_pos pos;
5469 int force_p;
5470 {
5471 int original_pos = IT_CHARPOS (*it);
5472
5473 reseat_1 (it, pos, 0);
5474
5475 /* Determine where to check text properties. Avoid doing it
5476 where possible because text property lookup is very expensive. */
5477 if (force_p
5478 || CHARPOS (pos) > it->stop_charpos
5479 || CHARPOS (pos) < original_pos)
5480 handle_stop (it);
5481
5482 CHECK_IT (it);
5483 }
5484
5485
5486 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5487 IT->stop_pos to POS, also. */
5488
5489 static void
5490 reseat_1 (it, pos, set_stop_p)
5491 struct it *it;
5492 struct text_pos pos;
5493 int set_stop_p;
5494 {
5495 /* Don't call this function when scanning a C string. */
5496 xassert (it->s == NULL);
5497
5498 /* POS must be a reasonable value. */
5499 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5500
5501 it->current.pos = it->position = pos;
5502 it->end_charpos = ZV;
5503 it->dpvec = NULL;
5504 it->current.dpvec_index = -1;
5505 it->current.overlay_string_index = -1;
5506 IT_STRING_CHARPOS (*it) = -1;
5507 IT_STRING_BYTEPOS (*it) = -1;
5508 it->string = Qnil;
5509 it->string_from_display_prop_p = 0;
5510 it->method = GET_FROM_BUFFER;
5511 it->object = it->w->buffer;
5512 it->area = TEXT_AREA;
5513 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5514 it->sp = 0;
5515 it->string_from_display_prop_p = 0;
5516 it->face_before_selective_p = 0;
5517
5518 if (set_stop_p)
5519 it->stop_charpos = CHARPOS (pos);
5520 }
5521
5522
5523 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5524 If S is non-null, it is a C string to iterate over. Otherwise,
5525 STRING gives a Lisp string to iterate over.
5526
5527 If PRECISION > 0, don't return more then PRECISION number of
5528 characters from the string.
5529
5530 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5531 characters have been returned. FIELD_WIDTH < 0 means an infinite
5532 field width.
5533
5534 MULTIBYTE = 0 means disable processing of multibyte characters,
5535 MULTIBYTE > 0 means enable it,
5536 MULTIBYTE < 0 means use IT->multibyte_p.
5537
5538 IT must be initialized via a prior call to init_iterator before
5539 calling this function. */
5540
5541 static void
5542 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5543 struct it *it;
5544 unsigned char *s;
5545 Lisp_Object string;
5546 int charpos;
5547 int precision, field_width, multibyte;
5548 {
5549 /* No region in strings. */
5550 it->region_beg_charpos = it->region_end_charpos = -1;
5551
5552 /* No text property checks performed by default, but see below. */
5553 it->stop_charpos = -1;
5554
5555 /* Set iterator position and end position. */
5556 bzero (&it->current, sizeof it->current);
5557 it->current.overlay_string_index = -1;
5558 it->current.dpvec_index = -1;
5559 xassert (charpos >= 0);
5560
5561 /* If STRING is specified, use its multibyteness, otherwise use the
5562 setting of MULTIBYTE, if specified. */
5563 if (multibyte >= 0)
5564 it->multibyte_p = multibyte > 0;
5565
5566 if (s == NULL)
5567 {
5568 xassert (STRINGP (string));
5569 it->string = string;
5570 it->s = NULL;
5571 it->end_charpos = it->string_nchars = SCHARS (string);
5572 it->method = GET_FROM_STRING;
5573 it->current.string_pos = string_pos (charpos, string);
5574 }
5575 else
5576 {
5577 it->s = s;
5578 it->string = Qnil;
5579
5580 /* Note that we use IT->current.pos, not it->current.string_pos,
5581 for displaying C strings. */
5582 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5583 if (it->multibyte_p)
5584 {
5585 it->current.pos = c_string_pos (charpos, s, 1);
5586 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5587 }
5588 else
5589 {
5590 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5591 it->end_charpos = it->string_nchars = strlen (s);
5592 }
5593
5594 it->method = GET_FROM_C_STRING;
5595 }
5596
5597 /* PRECISION > 0 means don't return more than PRECISION characters
5598 from the string. */
5599 if (precision > 0 && it->end_charpos - charpos > precision)
5600 it->end_charpos = it->string_nchars = charpos + precision;
5601
5602 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5603 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5604 FIELD_WIDTH < 0 means infinite field width. This is useful for
5605 padding with `-' at the end of a mode line. */
5606 if (field_width < 0)
5607 field_width = INFINITY;
5608 if (field_width > it->end_charpos - charpos)
5609 it->end_charpos = charpos + field_width;
5610
5611 /* Use the standard display table for displaying strings. */
5612 if (DISP_TABLE_P (Vstandard_display_table))
5613 it->dp = XCHAR_TABLE (Vstandard_display_table);
5614
5615 it->stop_charpos = charpos;
5616 CHECK_IT (it);
5617 }
5618
5619
5620 \f
5621 /***********************************************************************
5622 Iteration
5623 ***********************************************************************/
5624
5625 /* Map enum it_method value to corresponding next_element_from_* function. */
5626
5627 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5628 {
5629 next_element_from_buffer,
5630 next_element_from_display_vector,
5631 next_element_from_string,
5632 next_element_from_c_string,
5633 next_element_from_image,
5634 next_element_from_stretch
5635 };
5636
5637 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5638
5639
5640 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5641 (possibly with the following characters). */
5642
5643 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5644 ((IT)->cmp_it.id >= 0 \
5645 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5646 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5647 (IT)->end_charpos, (IT)->w, \
5648 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5649 (IT)->string)))
5650
5651
5652 /* Load IT's display element fields with information about the next
5653 display element from the current position of IT. Value is zero if
5654 end of buffer (or C string) is reached. */
5655
5656 static struct frame *last_escape_glyph_frame = NULL;
5657 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5658 static int last_escape_glyph_merged_face_id = 0;
5659
5660 int
5661 get_next_display_element (it)
5662 struct it *it;
5663 {
5664 /* Non-zero means that we found a display element. Zero means that
5665 we hit the end of what we iterate over. Performance note: the
5666 function pointer `method' used here turns out to be faster than
5667 using a sequence of if-statements. */
5668 int success_p;
5669
5670 get_next:
5671 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5672
5673 if (it->what == IT_CHARACTER)
5674 {
5675 /* Map via display table or translate control characters.
5676 IT->c, IT->len etc. have been set to the next character by
5677 the function call above. If we have a display table, and it
5678 contains an entry for IT->c, translate it. Don't do this if
5679 IT->c itself comes from a display table, otherwise we could
5680 end up in an infinite recursion. (An alternative could be to
5681 count the recursion depth of this function and signal an
5682 error when a certain maximum depth is reached.) Is it worth
5683 it? */
5684 if (success_p && it->dpvec == NULL)
5685 {
5686 Lisp_Object dv;
5687 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5688
5689 if (it->dp
5690 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5691 VECTORP (dv)))
5692 {
5693 struct Lisp_Vector *v = XVECTOR (dv);
5694
5695 /* Return the first character from the display table
5696 entry, if not empty. If empty, don't display the
5697 current character. */
5698 if (v->size)
5699 {
5700 it->dpvec_char_len = it->len;
5701 it->dpvec = v->contents;
5702 it->dpend = v->contents + v->size;
5703 it->current.dpvec_index = 0;
5704 it->dpvec_face_id = -1;
5705 it->saved_face_id = it->face_id;
5706 it->method = GET_FROM_DISPLAY_VECTOR;
5707 it->ellipsis_p = 0;
5708 }
5709 else
5710 {
5711 set_iterator_to_next (it, 0);
5712 }
5713 goto get_next;
5714 }
5715
5716 /* Translate control characters into `\003' or `^C' form.
5717 Control characters coming from a display table entry are
5718 currently not translated because we use IT->dpvec to hold
5719 the translation. This could easily be changed but I
5720 don't believe that it is worth doing.
5721
5722 If it->multibyte_p is nonzero, non-printable non-ASCII
5723 characters are also translated to octal form.
5724
5725 If it->multibyte_p is zero, eight-bit characters that
5726 don't have corresponding multibyte char code are also
5727 translated to octal form. */
5728 else if ((it->c < ' '
5729 ? (it->area != TEXT_AREA
5730 /* In mode line, treat \n, \t like other crl chars. */
5731 || (it->c != '\t'
5732 && it->glyph_row
5733 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5734 || (it->c != '\n' && it->c != '\t'))
5735 : (it->multibyte_p
5736 ? (!CHAR_PRINTABLE_P (it->c)
5737 || (!NILP (Vnobreak_char_display)
5738 && (it->c == 0xA0 /* NO-BREAK SPACE */
5739 || it->c == 0xAD /* SOFT HYPHEN */)))
5740 : (it->c >= 127
5741 && (! unibyte_display_via_language_environment
5742 || (DECODE_CHAR (unibyte, it->c) <= 0xA0))))))
5743 {
5744 /* IT->c is a control character which must be displayed
5745 either as '\003' or as `^C' where the '\\' and '^'
5746 can be defined in the display table. Fill
5747 IT->ctl_chars with glyphs for what we have to
5748 display. Then, set IT->dpvec to these glyphs. */
5749 Lisp_Object gc;
5750 int ctl_len;
5751 int face_id, lface_id = 0 ;
5752 int escape_glyph;
5753
5754 /* Handle control characters with ^. */
5755
5756 if (it->c < 128 && it->ctl_arrow_p)
5757 {
5758 int g;
5759
5760 g = '^'; /* default glyph for Control */
5761 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5762 if (it->dp
5763 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5764 && GLYPH_CODE_CHAR_VALID_P (gc))
5765 {
5766 g = GLYPH_CODE_CHAR (gc);
5767 lface_id = GLYPH_CODE_FACE (gc);
5768 }
5769 if (lface_id)
5770 {
5771 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5772 }
5773 else if (it->f == last_escape_glyph_frame
5774 && it->face_id == last_escape_glyph_face_id)
5775 {
5776 face_id = last_escape_glyph_merged_face_id;
5777 }
5778 else
5779 {
5780 /* Merge the escape-glyph face into the current face. */
5781 face_id = merge_faces (it->f, Qescape_glyph, 0,
5782 it->face_id);
5783 last_escape_glyph_frame = it->f;
5784 last_escape_glyph_face_id = it->face_id;
5785 last_escape_glyph_merged_face_id = face_id;
5786 }
5787
5788 XSETINT (it->ctl_chars[0], g);
5789 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5790 ctl_len = 2;
5791 goto display_control;
5792 }
5793
5794 /* Handle non-break space in the mode where it only gets
5795 highlighting. */
5796
5797 if (EQ (Vnobreak_char_display, Qt)
5798 && it->c == 0xA0)
5799 {
5800 /* Merge the no-break-space face into the current face. */
5801 face_id = merge_faces (it->f, Qnobreak_space, 0,
5802 it->face_id);
5803
5804 it->c = ' ';
5805 XSETINT (it->ctl_chars[0], ' ');
5806 ctl_len = 1;
5807 goto display_control;
5808 }
5809
5810 /* Handle sequences that start with the "escape glyph". */
5811
5812 /* the default escape glyph is \. */
5813 escape_glyph = '\\';
5814
5815 if (it->dp
5816 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5817 && GLYPH_CODE_CHAR_VALID_P (gc))
5818 {
5819 escape_glyph = GLYPH_CODE_CHAR (gc);
5820 lface_id = GLYPH_CODE_FACE (gc);
5821 }
5822 if (lface_id)
5823 {
5824 /* The display table specified a face.
5825 Merge it into face_id and also into escape_glyph. */
5826 face_id = merge_faces (it->f, Qt, lface_id,
5827 it->face_id);
5828 }
5829 else if (it->f == last_escape_glyph_frame
5830 && it->face_id == last_escape_glyph_face_id)
5831 {
5832 face_id = last_escape_glyph_merged_face_id;
5833 }
5834 else
5835 {
5836 /* Merge the escape-glyph face into the current face. */
5837 face_id = merge_faces (it->f, Qescape_glyph, 0,
5838 it->face_id);
5839 last_escape_glyph_frame = it->f;
5840 last_escape_glyph_face_id = it->face_id;
5841 last_escape_glyph_merged_face_id = face_id;
5842 }
5843
5844 /* Handle soft hyphens in the mode where they only get
5845 highlighting. */
5846
5847 if (EQ (Vnobreak_char_display, Qt)
5848 && it->c == 0xAD)
5849 {
5850 it->c = '-';
5851 XSETINT (it->ctl_chars[0], '-');
5852 ctl_len = 1;
5853 goto display_control;
5854 }
5855
5856 /* Handle non-break space and soft hyphen
5857 with the escape glyph. */
5858
5859 if (it->c == 0xA0 || it->c == 0xAD)
5860 {
5861 XSETINT (it->ctl_chars[0], escape_glyph);
5862 it->c = (it->c == 0xA0 ? ' ' : '-');
5863 XSETINT (it->ctl_chars[1], it->c);
5864 ctl_len = 2;
5865 goto display_control;
5866 }
5867
5868 {
5869 unsigned char str[MAX_MULTIBYTE_LENGTH];
5870 int len;
5871 int i;
5872
5873 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5874 if (CHAR_BYTE8_P (it->c))
5875 {
5876 str[0] = CHAR_TO_BYTE8 (it->c);
5877 len = 1;
5878 }
5879 else if (it->c < 256)
5880 {
5881 str[0] = it->c;
5882 len = 1;
5883 }
5884 else
5885 {
5886 /* It's an invalid character, which shouldn't
5887 happen actually, but due to bugs it may
5888 happen. Let's print the char as is, there's
5889 not much meaningful we can do with it. */
5890 str[0] = it->c;
5891 str[1] = it->c >> 8;
5892 str[2] = it->c >> 16;
5893 str[3] = it->c >> 24;
5894 len = 4;
5895 }
5896
5897 for (i = 0; i < len; i++)
5898 {
5899 int g;
5900 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5901 /* Insert three more glyphs into IT->ctl_chars for
5902 the octal display of the character. */
5903 g = ((str[i] >> 6) & 7) + '0';
5904 XSETINT (it->ctl_chars[i * 4 + 1], g);
5905 g = ((str[i] >> 3) & 7) + '0';
5906 XSETINT (it->ctl_chars[i * 4 + 2], g);
5907 g = (str[i] & 7) + '0';
5908 XSETINT (it->ctl_chars[i * 4 + 3], g);
5909 }
5910 ctl_len = len * 4;
5911 }
5912
5913 display_control:
5914 /* Set up IT->dpvec and return first character from it. */
5915 it->dpvec_char_len = it->len;
5916 it->dpvec = it->ctl_chars;
5917 it->dpend = it->dpvec + ctl_len;
5918 it->current.dpvec_index = 0;
5919 it->dpvec_face_id = face_id;
5920 it->saved_face_id = it->face_id;
5921 it->method = GET_FROM_DISPLAY_VECTOR;
5922 it->ellipsis_p = 0;
5923 goto get_next;
5924 }
5925 }
5926 }
5927
5928 #ifdef HAVE_WINDOW_SYSTEM
5929 /* Adjust face id for a multibyte character. There are no multibyte
5930 character in unibyte text. */
5931 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5932 && it->multibyte_p
5933 && success_p
5934 && FRAME_WINDOW_P (it->f))
5935 {
5936 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5937
5938 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5939 {
5940 /* Automatic composition with glyph-string. */
5941 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5942
5943 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5944 }
5945 else
5946 {
5947 int pos = (it->s ? -1
5948 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5949 : IT_CHARPOS (*it));
5950
5951 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5952 }
5953 }
5954 #endif
5955
5956 /* Is this character the last one of a run of characters with
5957 box? If yes, set IT->end_of_box_run_p to 1. */
5958 if (it->face_box_p
5959 && it->s == NULL)
5960 {
5961 if (it->method == GET_FROM_STRING && it->sp)
5962 {
5963 int face_id = underlying_face_id (it);
5964 struct face *face = FACE_FROM_ID (it->f, face_id);
5965
5966 if (face)
5967 {
5968 if (face->box == FACE_NO_BOX)
5969 {
5970 /* If the box comes from face properties in a
5971 display string, check faces in that string. */
5972 int string_face_id = face_after_it_pos (it);
5973 it->end_of_box_run_p
5974 = (FACE_FROM_ID (it->f, string_face_id)->box
5975 == FACE_NO_BOX);
5976 }
5977 /* Otherwise, the box comes from the underlying face.
5978 If this is the last string character displayed, check
5979 the next buffer location. */
5980 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5981 && (it->current.overlay_string_index
5982 == it->n_overlay_strings - 1))
5983 {
5984 EMACS_INT ignore;
5985 int next_face_id;
5986 struct text_pos pos = it->current.pos;
5987 INC_TEXT_POS (pos, it->multibyte_p);
5988
5989 next_face_id = face_at_buffer_position
5990 (it->w, CHARPOS (pos), it->region_beg_charpos,
5991 it->region_end_charpos, &ignore,
5992 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5993 -1);
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 /* One graphical terminals, newlines may
6918 "overflow" into the fringe if
6919 overflow-newline-into-fringe is non-nil.
6920 On text-only terminals, newlines may
6921 overflow into the last glyph on the
6922 display line.*/
6923 if (!FRAME_WINDOW_P (it->f)
6924 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6925 {
6926 if (!get_next_display_element (it))
6927 {
6928 result = MOVE_POS_MATCH_OR_ZV;
6929 break;
6930 }
6931 if (BUFFER_POS_REACHED_P ())
6932 {
6933 if (ITERATOR_AT_END_OF_LINE_P (it))
6934 result = MOVE_POS_MATCH_OR_ZV;
6935 else
6936 result = MOVE_LINE_CONTINUED;
6937 break;
6938 }
6939 if (ITERATOR_AT_END_OF_LINE_P (it))
6940 {
6941 result = MOVE_NEWLINE_OR_CR;
6942 break;
6943 }
6944 }
6945 }
6946 }
6947 else
6948 IT_RESET_X_ASCENT_DESCENT (it);
6949
6950 if (wrap_it.sp >= 0)
6951 {
6952 *it = wrap_it;
6953 atpos_it.sp = -1;
6954 atx_it.sp = -1;
6955 }
6956
6957 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6958 IT_CHARPOS (*it)));
6959 result = MOVE_LINE_CONTINUED;
6960 break;
6961 }
6962
6963 if (BUFFER_POS_REACHED_P ())
6964 {
6965 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6966 goto buffer_pos_reached;
6967 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6968 {
6969 atpos_it = *it;
6970 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6971 }
6972 }
6973
6974 if (new_x > it->first_visible_x)
6975 {
6976 /* Glyph is visible. Increment number of glyphs that
6977 would be displayed. */
6978 ++it->hpos;
6979 }
6980 }
6981
6982 if (result != MOVE_UNDEFINED)
6983 break;
6984 }
6985 else if (BUFFER_POS_REACHED_P ())
6986 {
6987 buffer_pos_reached:
6988 IT_RESET_X_ASCENT_DESCENT (it);
6989 result = MOVE_POS_MATCH_OR_ZV;
6990 break;
6991 }
6992 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6993 {
6994 /* Stop when TO_X specified and reached. This check is
6995 necessary here because of lines consisting of a line end,
6996 only. The line end will not produce any glyphs and we
6997 would never get MOVE_X_REACHED. */
6998 xassert (it->nglyphs == 0);
6999 result = MOVE_X_REACHED;
7000 break;
7001 }
7002
7003 /* Is this a line end? If yes, we're done. */
7004 if (ITERATOR_AT_END_OF_LINE_P (it))
7005 {
7006 result = MOVE_NEWLINE_OR_CR;
7007 break;
7008 }
7009
7010 /* The current display element has been consumed. Advance
7011 to the next. */
7012 set_iterator_to_next (it, 1);
7013
7014 /* Stop if lines are truncated and IT's current x-position is
7015 past the right edge of the window now. */
7016 if (it->line_wrap == TRUNCATE
7017 && it->current_x >= it->last_visible_x)
7018 {
7019 if (!FRAME_WINDOW_P (it->f)
7020 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7021 {
7022 if (!get_next_display_element (it)
7023 || BUFFER_POS_REACHED_P ())
7024 {
7025 result = MOVE_POS_MATCH_OR_ZV;
7026 break;
7027 }
7028 if (ITERATOR_AT_END_OF_LINE_P (it))
7029 {
7030 result = MOVE_NEWLINE_OR_CR;
7031 break;
7032 }
7033 }
7034 result = MOVE_LINE_TRUNCATED;
7035 break;
7036 }
7037 #undef IT_RESET_X_ASCENT_DESCENT
7038 }
7039
7040 #undef BUFFER_POS_REACHED_P
7041
7042 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7043 restore the saved iterator. */
7044 if (atpos_it.sp >= 0)
7045 *it = atpos_it;
7046 else if (atx_it.sp >= 0)
7047 *it = atx_it;
7048
7049 done:
7050
7051 /* Restore the iterator settings altered at the beginning of this
7052 function. */
7053 it->glyph_row = saved_glyph_row;
7054 return result;
7055 }
7056
7057 /* For external use. */
7058 void
7059 move_it_in_display_line (struct it *it,
7060 EMACS_INT to_charpos, int to_x,
7061 enum move_operation_enum op)
7062 {
7063 if (it->line_wrap == WORD_WRAP
7064 && (op & MOVE_TO_X))
7065 {
7066 struct it save_it = *it;
7067 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7068 /* When word-wrap is on, TO_X may lie past the end
7069 of a wrapped line. Then it->current is the
7070 character on the next line, so backtrack to the
7071 space before the wrap point. */
7072 if (skip == MOVE_LINE_CONTINUED)
7073 {
7074 int prev_x = max (it->current_x - 1, 0);
7075 *it = save_it;
7076 move_it_in_display_line_to
7077 (it, -1, prev_x, MOVE_TO_X);
7078 }
7079 }
7080 else
7081 move_it_in_display_line_to (it, to_charpos, to_x, op);
7082 }
7083
7084
7085 /* Move IT forward until it satisfies one or more of the criteria in
7086 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7087
7088 OP is a bit-mask that specifies where to stop, and in particular,
7089 which of those four position arguments makes a difference. See the
7090 description of enum move_operation_enum.
7091
7092 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7093 screen line, this function will set IT to the next position >
7094 TO_CHARPOS. */
7095
7096 void
7097 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7098 struct it *it;
7099 int to_charpos, to_x, to_y, to_vpos;
7100 int op;
7101 {
7102 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7103 int line_height, line_start_x = 0, reached = 0;
7104
7105 for (;;)
7106 {
7107 if (op & MOVE_TO_VPOS)
7108 {
7109 /* If no TO_CHARPOS and no TO_X specified, stop at the
7110 start of the line TO_VPOS. */
7111 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7112 {
7113 if (it->vpos == to_vpos)
7114 {
7115 reached = 1;
7116 break;
7117 }
7118 else
7119 skip = move_it_in_display_line_to (it, -1, -1, 0);
7120 }
7121 else
7122 {
7123 /* TO_VPOS >= 0 means stop at TO_X in the line at
7124 TO_VPOS, or at TO_POS, whichever comes first. */
7125 if (it->vpos == to_vpos)
7126 {
7127 reached = 2;
7128 break;
7129 }
7130
7131 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7132
7133 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7134 {
7135 reached = 3;
7136 break;
7137 }
7138 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7139 {
7140 /* We have reached TO_X but not in the line we want. */
7141 skip = move_it_in_display_line_to (it, to_charpos,
7142 -1, MOVE_TO_POS);
7143 if (skip == MOVE_POS_MATCH_OR_ZV)
7144 {
7145 reached = 4;
7146 break;
7147 }
7148 }
7149 }
7150 }
7151 else if (op & MOVE_TO_Y)
7152 {
7153 struct it it_backup;
7154
7155 if (it->line_wrap == WORD_WRAP)
7156 it_backup = *it;
7157
7158 /* TO_Y specified means stop at TO_X in the line containing
7159 TO_Y---or at TO_CHARPOS if this is reached first. The
7160 problem is that we can't really tell whether the line
7161 contains TO_Y before we have completely scanned it, and
7162 this may skip past TO_X. What we do is to first scan to
7163 TO_X.
7164
7165 If TO_X is not specified, use a TO_X of zero. The reason
7166 is to make the outcome of this function more predictable.
7167 If we didn't use TO_X == 0, we would stop at the end of
7168 the line which is probably not what a caller would expect
7169 to happen. */
7170 skip = move_it_in_display_line_to
7171 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7172 (MOVE_TO_X | (op & MOVE_TO_POS)));
7173
7174 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7175 if (skip == MOVE_POS_MATCH_OR_ZV)
7176 reached = 5;
7177 else if (skip == MOVE_X_REACHED)
7178 {
7179 /* If TO_X was reached, we want to know whether TO_Y is
7180 in the line. We know this is the case if the already
7181 scanned glyphs make the line tall enough. Otherwise,
7182 we must check by scanning the rest of the line. */
7183 line_height = it->max_ascent + it->max_descent;
7184 if (to_y >= it->current_y
7185 && to_y < it->current_y + line_height)
7186 {
7187 reached = 6;
7188 break;
7189 }
7190 it_backup = *it;
7191 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7192 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7193 op & MOVE_TO_POS);
7194 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7195 line_height = it->max_ascent + it->max_descent;
7196 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7197
7198 if (to_y >= it->current_y
7199 && to_y < it->current_y + line_height)
7200 {
7201 /* If TO_Y is in this line and TO_X was reached
7202 above, we scanned too far. We have to restore
7203 IT's settings to the ones before skipping. */
7204 *it = it_backup;
7205 reached = 6;
7206 }
7207 else
7208 {
7209 skip = skip2;
7210 if (skip == MOVE_POS_MATCH_OR_ZV)
7211 reached = 7;
7212 }
7213 }
7214 else
7215 {
7216 /* Check whether TO_Y is in this line. */
7217 line_height = it->max_ascent + it->max_descent;
7218 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7219
7220 if (to_y >= it->current_y
7221 && to_y < it->current_y + line_height)
7222 {
7223 /* When word-wrap is on, TO_X may lie past the end
7224 of a wrapped line. Then it->current is the
7225 character on the next line, so backtrack to the
7226 space before the wrap point. */
7227 if (skip == MOVE_LINE_CONTINUED
7228 && it->line_wrap == WORD_WRAP)
7229 {
7230 int prev_x = max (it->current_x - 1, 0);
7231 *it = it_backup;
7232 skip = move_it_in_display_line_to
7233 (it, -1, prev_x, MOVE_TO_X);
7234 }
7235 reached = 6;
7236 }
7237 }
7238
7239 if (reached)
7240 break;
7241 }
7242 else if (BUFFERP (it->object)
7243 && (it->method == GET_FROM_BUFFER
7244 || it->method == GET_FROM_STRETCH)
7245 && IT_CHARPOS (*it) >= to_charpos)
7246 skip = MOVE_POS_MATCH_OR_ZV;
7247 else
7248 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7249
7250 switch (skip)
7251 {
7252 case MOVE_POS_MATCH_OR_ZV:
7253 reached = 8;
7254 goto out;
7255
7256 case MOVE_NEWLINE_OR_CR:
7257 set_iterator_to_next (it, 1);
7258 it->continuation_lines_width = 0;
7259 break;
7260
7261 case MOVE_LINE_TRUNCATED:
7262 it->continuation_lines_width = 0;
7263 reseat_at_next_visible_line_start (it, 0);
7264 if ((op & MOVE_TO_POS) != 0
7265 && IT_CHARPOS (*it) > to_charpos)
7266 {
7267 reached = 9;
7268 goto out;
7269 }
7270 break;
7271
7272 case MOVE_LINE_CONTINUED:
7273 /* For continued lines ending in a tab, some of the glyphs
7274 associated with the tab are displayed on the current
7275 line. Since it->current_x does not include these glyphs,
7276 we use it->last_visible_x instead. */
7277 if (it->c == '\t')
7278 {
7279 it->continuation_lines_width += it->last_visible_x;
7280 /* When moving by vpos, ensure that the iterator really
7281 advances to the next line (bug#847, bug#969). Fixme:
7282 do we need to do this in other circumstances? */
7283 if (it->current_x != it->last_visible_x
7284 && (op & MOVE_TO_VPOS)
7285 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7286 {
7287 line_start_x = it->current_x + it->pixel_width
7288 - it->last_visible_x;
7289 set_iterator_to_next (it, 0);
7290 }
7291 }
7292 else
7293 it->continuation_lines_width += it->current_x;
7294 break;
7295
7296 default:
7297 abort ();
7298 }
7299
7300 /* Reset/increment for the next run. */
7301 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7302 it->current_x = line_start_x;
7303 line_start_x = 0;
7304 it->hpos = 0;
7305 it->current_y += it->max_ascent + it->max_descent;
7306 ++it->vpos;
7307 last_height = it->max_ascent + it->max_descent;
7308 last_max_ascent = it->max_ascent;
7309 it->max_ascent = it->max_descent = 0;
7310 }
7311
7312 out:
7313
7314 /* On text terminals, we may stop at the end of a line in the middle
7315 of a multi-character glyph. If the glyph itself is continued,
7316 i.e. it is actually displayed on the next line, don't treat this
7317 stopping point as valid; move to the next line instead (unless
7318 that brings us offscreen). */
7319 if (!FRAME_WINDOW_P (it->f)
7320 && op & MOVE_TO_POS
7321 && IT_CHARPOS (*it) == to_charpos
7322 && it->what == IT_CHARACTER
7323 && it->nglyphs > 1
7324 && it->line_wrap == WINDOW_WRAP
7325 && it->current_x == it->last_visible_x - 1
7326 && it->c != '\n'
7327 && it->c != '\t'
7328 && it->vpos < XFASTINT (it->w->window_end_vpos))
7329 {
7330 it->continuation_lines_width += it->current_x;
7331 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7332 it->current_y += it->max_ascent + it->max_descent;
7333 ++it->vpos;
7334 last_height = it->max_ascent + it->max_descent;
7335 last_max_ascent = it->max_ascent;
7336 }
7337
7338 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7339 }
7340
7341
7342 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7343
7344 If DY > 0, move IT backward at least that many pixels. DY = 0
7345 means move IT backward to the preceding line start or BEGV. This
7346 function may move over more than DY pixels if IT->current_y - DY
7347 ends up in the middle of a line; in this case IT->current_y will be
7348 set to the top of the line moved to. */
7349
7350 void
7351 move_it_vertically_backward (it, dy)
7352 struct it *it;
7353 int dy;
7354 {
7355 int nlines, h;
7356 struct it it2, it3;
7357 int start_pos;
7358
7359 move_further_back:
7360 xassert (dy >= 0);
7361
7362 start_pos = IT_CHARPOS (*it);
7363
7364 /* Estimate how many newlines we must move back. */
7365 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7366
7367 /* Set the iterator's position that many lines back. */
7368 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7369 back_to_previous_visible_line_start (it);
7370
7371 /* Reseat the iterator here. When moving backward, we don't want
7372 reseat to skip forward over invisible text, set up the iterator
7373 to deliver from overlay strings at the new position etc. So,
7374 use reseat_1 here. */
7375 reseat_1 (it, it->current.pos, 1);
7376
7377 /* We are now surely at a line start. */
7378 it->current_x = it->hpos = 0;
7379 it->continuation_lines_width = 0;
7380
7381 /* Move forward and see what y-distance we moved. First move to the
7382 start of the next line so that we get its height. We need this
7383 height to be able to tell whether we reached the specified
7384 y-distance. */
7385 it2 = *it;
7386 it2.max_ascent = it2.max_descent = 0;
7387 do
7388 {
7389 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7390 MOVE_TO_POS | MOVE_TO_VPOS);
7391 }
7392 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7393 xassert (IT_CHARPOS (*it) >= BEGV);
7394 it3 = it2;
7395
7396 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7397 xassert (IT_CHARPOS (*it) >= BEGV);
7398 /* H is the actual vertical distance from the position in *IT
7399 and the starting position. */
7400 h = it2.current_y - it->current_y;
7401 /* NLINES is the distance in number of lines. */
7402 nlines = it2.vpos - it->vpos;
7403
7404 /* Correct IT's y and vpos position
7405 so that they are relative to the starting point. */
7406 it->vpos -= nlines;
7407 it->current_y -= h;
7408
7409 if (dy == 0)
7410 {
7411 /* DY == 0 means move to the start of the screen line. The
7412 value of nlines is > 0 if continuation lines were involved. */
7413 if (nlines > 0)
7414 move_it_by_lines (it, nlines, 1);
7415 }
7416 else
7417 {
7418 /* The y-position we try to reach, relative to *IT.
7419 Note that H has been subtracted in front of the if-statement. */
7420 int target_y = it->current_y + h - dy;
7421 int y0 = it3.current_y;
7422 int y1 = line_bottom_y (&it3);
7423 int line_height = y1 - y0;
7424
7425 /* If we did not reach target_y, try to move further backward if
7426 we can. If we moved too far backward, try to move forward. */
7427 if (target_y < it->current_y
7428 /* This is heuristic. In a window that's 3 lines high, with
7429 a line height of 13 pixels each, recentering with point
7430 on the bottom line will try to move -39/2 = 19 pixels
7431 backward. Try to avoid moving into the first line. */
7432 && (it->current_y - target_y
7433 > min (window_box_height (it->w), line_height * 2 / 3))
7434 && IT_CHARPOS (*it) > BEGV)
7435 {
7436 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7437 target_y - it->current_y));
7438 dy = it->current_y - target_y;
7439 goto move_further_back;
7440 }
7441 else if (target_y >= it->current_y + line_height
7442 && IT_CHARPOS (*it) < ZV)
7443 {
7444 /* Should move forward by at least one line, maybe more.
7445
7446 Note: Calling move_it_by_lines can be expensive on
7447 terminal frames, where compute_motion is used (via
7448 vmotion) to do the job, when there are very long lines
7449 and truncate-lines is nil. That's the reason for
7450 treating terminal frames specially here. */
7451
7452 if (!FRAME_WINDOW_P (it->f))
7453 move_it_vertically (it, target_y - (it->current_y + line_height));
7454 else
7455 {
7456 do
7457 {
7458 move_it_by_lines (it, 1, 1);
7459 }
7460 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7461 }
7462 }
7463 }
7464 }
7465
7466
7467 /* Move IT by a specified amount of pixel lines DY. DY negative means
7468 move backwards. DY = 0 means move to start of screen line. At the
7469 end, IT will be on the start of a screen line. */
7470
7471 void
7472 move_it_vertically (it, dy)
7473 struct it *it;
7474 int dy;
7475 {
7476 if (dy <= 0)
7477 move_it_vertically_backward (it, -dy);
7478 else
7479 {
7480 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7481 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7482 MOVE_TO_POS | MOVE_TO_Y);
7483 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7484
7485 /* If buffer ends in ZV without a newline, move to the start of
7486 the line to satisfy the post-condition. */
7487 if (IT_CHARPOS (*it) == ZV
7488 && ZV > BEGV
7489 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7490 move_it_by_lines (it, 0, 0);
7491 }
7492 }
7493
7494
7495 /* Move iterator IT past the end of the text line it is in. */
7496
7497 void
7498 move_it_past_eol (it)
7499 struct it *it;
7500 {
7501 enum move_it_result rc;
7502
7503 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7504 if (rc == MOVE_NEWLINE_OR_CR)
7505 set_iterator_to_next (it, 0);
7506 }
7507
7508
7509 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7510 negative means move up. DVPOS == 0 means move to the start of the
7511 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7512 NEED_Y_P is zero, IT->current_y will be left unchanged.
7513
7514 Further optimization ideas: If we would know that IT->f doesn't use
7515 a face with proportional font, we could be faster for
7516 truncate-lines nil. */
7517
7518 void
7519 move_it_by_lines (it, dvpos, need_y_p)
7520 struct it *it;
7521 int dvpos, need_y_p;
7522 {
7523 struct position pos;
7524
7525 /* The commented-out optimization uses vmotion on terminals. This
7526 gives bad results, because elements like it->what, on which
7527 callers such as pos_visible_p rely, aren't updated. */
7528 /* if (!FRAME_WINDOW_P (it->f))
7529 {
7530 struct text_pos textpos;
7531
7532 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7533 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7534 reseat (it, textpos, 1);
7535 it->vpos += pos.vpos;
7536 it->current_y += pos.vpos;
7537 }
7538 else */
7539
7540 if (dvpos == 0)
7541 {
7542 /* DVPOS == 0 means move to the start of the screen line. */
7543 move_it_vertically_backward (it, 0);
7544 xassert (it->current_x == 0 && it->hpos == 0);
7545 /* Let next call to line_bottom_y calculate real line height */
7546 last_height = 0;
7547 }
7548 else if (dvpos > 0)
7549 {
7550 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7551 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7552 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7553 }
7554 else
7555 {
7556 struct it it2;
7557 int start_charpos, i;
7558
7559 /* Start at the beginning of the screen line containing IT's
7560 position. This may actually move vertically backwards,
7561 in case of overlays, so adjust dvpos accordingly. */
7562 dvpos += it->vpos;
7563 move_it_vertically_backward (it, 0);
7564 dvpos -= it->vpos;
7565
7566 /* Go back -DVPOS visible lines and reseat the iterator there. */
7567 start_charpos = IT_CHARPOS (*it);
7568 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7569 back_to_previous_visible_line_start (it);
7570 reseat (it, it->current.pos, 1);
7571
7572 /* Move further back if we end up in a string or an image. */
7573 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7574 {
7575 /* First try to move to start of display line. */
7576 dvpos += it->vpos;
7577 move_it_vertically_backward (it, 0);
7578 dvpos -= it->vpos;
7579 if (IT_POS_VALID_AFTER_MOVE_P (it))
7580 break;
7581 /* If start of line is still in string or image,
7582 move further back. */
7583 back_to_previous_visible_line_start (it);
7584 reseat (it, it->current.pos, 1);
7585 dvpos--;
7586 }
7587
7588 it->current_x = it->hpos = 0;
7589
7590 /* Above call may have moved too far if continuation lines
7591 are involved. Scan forward and see if it did. */
7592 it2 = *it;
7593 it2.vpos = it2.current_y = 0;
7594 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7595 it->vpos -= it2.vpos;
7596 it->current_y -= it2.current_y;
7597 it->current_x = it->hpos = 0;
7598
7599 /* If we moved too far back, move IT some lines forward. */
7600 if (it2.vpos > -dvpos)
7601 {
7602 int delta = it2.vpos + dvpos;
7603 it2 = *it;
7604 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7605 /* Move back again if we got too far ahead. */
7606 if (IT_CHARPOS (*it) >= start_charpos)
7607 *it = it2;
7608 }
7609 }
7610 }
7611
7612 /* Return 1 if IT points into the middle of a display vector. */
7613
7614 int
7615 in_display_vector_p (it)
7616 struct it *it;
7617 {
7618 return (it->method == GET_FROM_DISPLAY_VECTOR
7619 && it->current.dpvec_index > 0
7620 && it->dpvec + it->current.dpvec_index != it->dpend);
7621 }
7622
7623 \f
7624 /***********************************************************************
7625 Messages
7626 ***********************************************************************/
7627
7628
7629 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7630 to *Messages*. */
7631
7632 void
7633 add_to_log (format, arg1, arg2)
7634 char *format;
7635 Lisp_Object arg1, arg2;
7636 {
7637 Lisp_Object args[3];
7638 Lisp_Object msg, fmt;
7639 char *buffer;
7640 int len;
7641 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7642 USE_SAFE_ALLOCA;
7643
7644 /* Do nothing if called asynchronously. Inserting text into
7645 a buffer may call after-change-functions and alike and
7646 that would means running Lisp asynchronously. */
7647 if (handling_signal)
7648 return;
7649
7650 fmt = msg = Qnil;
7651 GCPRO4 (fmt, msg, arg1, arg2);
7652
7653 args[0] = fmt = build_string (format);
7654 args[1] = arg1;
7655 args[2] = arg2;
7656 msg = Fformat (3, args);
7657
7658 len = SBYTES (msg) + 1;
7659 SAFE_ALLOCA (buffer, char *, len);
7660 bcopy (SDATA (msg), buffer, len);
7661
7662 message_dolog (buffer, len - 1, 1, 0);
7663 SAFE_FREE ();
7664
7665 UNGCPRO;
7666 }
7667
7668
7669 /* Output a newline in the *Messages* buffer if "needs" one. */
7670
7671 void
7672 message_log_maybe_newline ()
7673 {
7674 if (message_log_need_newline)
7675 message_dolog ("", 0, 1, 0);
7676 }
7677
7678
7679 /* Add a string M of length NBYTES to the message log, optionally
7680 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7681 nonzero, means interpret the contents of M as multibyte. This
7682 function calls low-level routines in order to bypass text property
7683 hooks, etc. which might not be safe to run.
7684
7685 This may GC (insert may run before/after change hooks),
7686 so the buffer M must NOT point to a Lisp string. */
7687
7688 void
7689 message_dolog (m, nbytes, nlflag, multibyte)
7690 const char *m;
7691 int nbytes, nlflag, multibyte;
7692 {
7693 if (!NILP (Vmemory_full))
7694 return;
7695
7696 if (!NILP (Vmessage_log_max))
7697 {
7698 struct buffer *oldbuf;
7699 Lisp_Object oldpoint, oldbegv, oldzv;
7700 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7701 int point_at_end = 0;
7702 int zv_at_end = 0;
7703 Lisp_Object old_deactivate_mark, tem;
7704 struct gcpro gcpro1;
7705
7706 old_deactivate_mark = Vdeactivate_mark;
7707 oldbuf = current_buffer;
7708 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7709 current_buffer->undo_list = Qt;
7710
7711 oldpoint = message_dolog_marker1;
7712 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7713 oldbegv = message_dolog_marker2;
7714 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7715 oldzv = message_dolog_marker3;
7716 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7717 GCPRO1 (old_deactivate_mark);
7718
7719 if (PT == Z)
7720 point_at_end = 1;
7721 if (ZV == Z)
7722 zv_at_end = 1;
7723
7724 BEGV = BEG;
7725 BEGV_BYTE = BEG_BYTE;
7726 ZV = Z;
7727 ZV_BYTE = Z_BYTE;
7728 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7729
7730 /* Insert the string--maybe converting multibyte to single byte
7731 or vice versa, so that all the text fits the buffer. */
7732 if (multibyte
7733 && NILP (current_buffer->enable_multibyte_characters))
7734 {
7735 int i, c, char_bytes;
7736 unsigned char work[1];
7737
7738 /* Convert a multibyte string to single-byte
7739 for the *Message* buffer. */
7740 for (i = 0; i < nbytes; i += char_bytes)
7741 {
7742 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7743 work[0] = (ASCII_CHAR_P (c)
7744 ? c
7745 : multibyte_char_to_unibyte (c, Qnil));
7746 insert_1_both (work, 1, 1, 1, 0, 0);
7747 }
7748 }
7749 else if (! multibyte
7750 && ! NILP (current_buffer->enable_multibyte_characters))
7751 {
7752 int i, c, char_bytes;
7753 unsigned char *msg = (unsigned char *) m;
7754 unsigned char str[MAX_MULTIBYTE_LENGTH];
7755 /* Convert a single-byte string to multibyte
7756 for the *Message* buffer. */
7757 for (i = 0; i < nbytes; i++)
7758 {
7759 c = msg[i];
7760 c = unibyte_char_to_multibyte (c);
7761 char_bytes = CHAR_STRING (c, str);
7762 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7763 }
7764 }
7765 else if (nbytes)
7766 insert_1 (m, nbytes, 1, 0, 0);
7767
7768 if (nlflag)
7769 {
7770 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7771 insert_1 ("\n", 1, 1, 0, 0);
7772
7773 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7774 this_bol = PT;
7775 this_bol_byte = PT_BYTE;
7776
7777 /* See if this line duplicates the previous one.
7778 If so, combine duplicates. */
7779 if (this_bol > BEG)
7780 {
7781 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7782 prev_bol = PT;
7783 prev_bol_byte = PT_BYTE;
7784
7785 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7786 this_bol, this_bol_byte);
7787 if (dup)
7788 {
7789 del_range_both (prev_bol, prev_bol_byte,
7790 this_bol, this_bol_byte, 0);
7791 if (dup > 1)
7792 {
7793 char dupstr[40];
7794 int duplen;
7795
7796 /* If you change this format, don't forget to also
7797 change message_log_check_duplicate. */
7798 sprintf (dupstr, " [%d times]", dup);
7799 duplen = strlen (dupstr);
7800 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7801 insert_1 (dupstr, duplen, 1, 0, 1);
7802 }
7803 }
7804 }
7805
7806 /* If we have more than the desired maximum number of lines
7807 in the *Messages* buffer now, delete the oldest ones.
7808 This is safe because we don't have undo in this buffer. */
7809
7810 if (NATNUMP (Vmessage_log_max))
7811 {
7812 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7813 -XFASTINT (Vmessage_log_max) - 1, 0);
7814 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7815 }
7816 }
7817 BEGV = XMARKER (oldbegv)->charpos;
7818 BEGV_BYTE = marker_byte_position (oldbegv);
7819
7820 if (zv_at_end)
7821 {
7822 ZV = Z;
7823 ZV_BYTE = Z_BYTE;
7824 }
7825 else
7826 {
7827 ZV = XMARKER (oldzv)->charpos;
7828 ZV_BYTE = marker_byte_position (oldzv);
7829 }
7830
7831 if (point_at_end)
7832 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7833 else
7834 /* We can't do Fgoto_char (oldpoint) because it will run some
7835 Lisp code. */
7836 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7837 XMARKER (oldpoint)->bytepos);
7838
7839 UNGCPRO;
7840 unchain_marker (XMARKER (oldpoint));
7841 unchain_marker (XMARKER (oldbegv));
7842 unchain_marker (XMARKER (oldzv));
7843
7844 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7845 set_buffer_internal (oldbuf);
7846 if (NILP (tem))
7847 windows_or_buffers_changed = old_windows_or_buffers_changed;
7848 message_log_need_newline = !nlflag;
7849 Vdeactivate_mark = old_deactivate_mark;
7850 }
7851 }
7852
7853
7854 /* We are at the end of the buffer after just having inserted a newline.
7855 (Note: We depend on the fact we won't be crossing the gap.)
7856 Check to see if the most recent message looks a lot like the previous one.
7857 Return 0 if different, 1 if the new one should just replace it, or a
7858 value N > 1 if we should also append " [N times]". */
7859
7860 static int
7861 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7862 int prev_bol, this_bol;
7863 int prev_bol_byte, this_bol_byte;
7864 {
7865 int i;
7866 int len = Z_BYTE - 1 - this_bol_byte;
7867 int seen_dots = 0;
7868 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7869 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7870
7871 for (i = 0; i < len; i++)
7872 {
7873 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7874 seen_dots = 1;
7875 if (p1[i] != p2[i])
7876 return seen_dots;
7877 }
7878 p1 += len;
7879 if (*p1 == '\n')
7880 return 2;
7881 if (*p1++ == ' ' && *p1++ == '[')
7882 {
7883 int n = 0;
7884 while (*p1 >= '0' && *p1 <= '9')
7885 n = n * 10 + *p1++ - '0';
7886 if (strncmp (p1, " times]\n", 8) == 0)
7887 return n+1;
7888 }
7889 return 0;
7890 }
7891 \f
7892
7893 /* Display an echo area message M with a specified length of NBYTES
7894 bytes. The string may include null characters. If M is 0, clear
7895 out any existing message, and let the mini-buffer text show
7896 through.
7897
7898 This may GC, so the buffer M must NOT point to a Lisp string. */
7899
7900 void
7901 message2 (m, nbytes, multibyte)
7902 const char *m;
7903 int nbytes;
7904 int multibyte;
7905 {
7906 /* First flush out any partial line written with print. */
7907 message_log_maybe_newline ();
7908 if (m)
7909 message_dolog (m, nbytes, 1, multibyte);
7910 message2_nolog (m, nbytes, multibyte);
7911 }
7912
7913
7914 /* The non-logging counterpart of message2. */
7915
7916 void
7917 message2_nolog (m, nbytes, multibyte)
7918 const char *m;
7919 int nbytes, multibyte;
7920 {
7921 struct frame *sf = SELECTED_FRAME ();
7922 message_enable_multibyte = multibyte;
7923
7924 if (FRAME_INITIAL_P (sf))
7925 {
7926 if (noninteractive_need_newline)
7927 putc ('\n', stderr);
7928 noninteractive_need_newline = 0;
7929 if (m)
7930 fwrite (m, nbytes, 1, stderr);
7931 if (cursor_in_echo_area == 0)
7932 fprintf (stderr, "\n");
7933 fflush (stderr);
7934 }
7935 /* A null message buffer means that the frame hasn't really been
7936 initialized yet. Error messages get reported properly by
7937 cmd_error, so this must be just an informative message; toss it. */
7938 else if (INTERACTIVE
7939 && sf->glyphs_initialized_p
7940 && FRAME_MESSAGE_BUF (sf))
7941 {
7942 Lisp_Object mini_window;
7943 struct frame *f;
7944
7945 /* Get the frame containing the mini-buffer
7946 that the selected frame is using. */
7947 mini_window = FRAME_MINIBUF_WINDOW (sf);
7948 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7949
7950 FRAME_SAMPLE_VISIBILITY (f);
7951 if (FRAME_VISIBLE_P (sf)
7952 && ! FRAME_VISIBLE_P (f))
7953 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7954
7955 if (m)
7956 {
7957 set_message (m, Qnil, nbytes, multibyte);
7958 if (minibuffer_auto_raise)
7959 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7960 }
7961 else
7962 clear_message (1, 1);
7963
7964 do_pending_window_change (0);
7965 echo_area_display (1);
7966 do_pending_window_change (0);
7967 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7968 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7969 }
7970 }
7971
7972
7973 /* Display an echo area message M with a specified length of NBYTES
7974 bytes. The string may include null characters. If M is not a
7975 string, clear out any existing message, and let the mini-buffer
7976 text show through.
7977
7978 This function cancels echoing. */
7979
7980 void
7981 message3 (m, nbytes, multibyte)
7982 Lisp_Object m;
7983 int nbytes;
7984 int multibyte;
7985 {
7986 struct gcpro gcpro1;
7987
7988 GCPRO1 (m);
7989 clear_message (1,1);
7990 cancel_echoing ();
7991
7992 /* First flush out any partial line written with print. */
7993 message_log_maybe_newline ();
7994 if (STRINGP (m))
7995 {
7996 char *buffer;
7997 USE_SAFE_ALLOCA;
7998
7999 SAFE_ALLOCA (buffer, char *, nbytes);
8000 bcopy (SDATA (m), buffer, nbytes);
8001 message_dolog (buffer, nbytes, 1, multibyte);
8002 SAFE_FREE ();
8003 }
8004 message3_nolog (m, nbytes, multibyte);
8005
8006 UNGCPRO;
8007 }
8008
8009
8010 /* The non-logging version of message3.
8011 This does not cancel echoing, because it is used for echoing.
8012 Perhaps we need to make a separate function for echoing
8013 and make this cancel echoing. */
8014
8015 void
8016 message3_nolog (m, nbytes, multibyte)
8017 Lisp_Object m;
8018 int nbytes, multibyte;
8019 {
8020 struct frame *sf = SELECTED_FRAME ();
8021 message_enable_multibyte = multibyte;
8022
8023 if (FRAME_INITIAL_P (sf))
8024 {
8025 if (noninteractive_need_newline)
8026 putc ('\n', stderr);
8027 noninteractive_need_newline = 0;
8028 if (STRINGP (m))
8029 fwrite (SDATA (m), nbytes, 1, stderr);
8030 if (cursor_in_echo_area == 0)
8031 fprintf (stderr, "\n");
8032 fflush (stderr);
8033 }
8034 /* A null message buffer means that the frame hasn't really been
8035 initialized yet. Error messages get reported properly by
8036 cmd_error, so this must be just an informative message; toss it. */
8037 else if (INTERACTIVE
8038 && sf->glyphs_initialized_p
8039 && FRAME_MESSAGE_BUF (sf))
8040 {
8041 Lisp_Object mini_window;
8042 Lisp_Object frame;
8043 struct frame *f;
8044
8045 /* Get the frame containing the mini-buffer
8046 that the selected frame is using. */
8047 mini_window = FRAME_MINIBUF_WINDOW (sf);
8048 frame = XWINDOW (mini_window)->frame;
8049 f = XFRAME (frame);
8050
8051 FRAME_SAMPLE_VISIBILITY (f);
8052 if (FRAME_VISIBLE_P (sf)
8053 && !FRAME_VISIBLE_P (f))
8054 Fmake_frame_visible (frame);
8055
8056 if (STRINGP (m) && SCHARS (m) > 0)
8057 {
8058 set_message (NULL, m, nbytes, multibyte);
8059 if (minibuffer_auto_raise)
8060 Fraise_frame (frame);
8061 /* Assume we are not echoing.
8062 (If we are, echo_now will override this.) */
8063 echo_message_buffer = Qnil;
8064 }
8065 else
8066 clear_message (1, 1);
8067
8068 do_pending_window_change (0);
8069 echo_area_display (1);
8070 do_pending_window_change (0);
8071 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8072 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8073 }
8074 }
8075
8076
8077 /* Display a null-terminated echo area message M. If M is 0, clear
8078 out any existing message, and let the mini-buffer text show through.
8079
8080 The buffer M must continue to exist until after the echo area gets
8081 cleared or some other message gets displayed there. Do not pass
8082 text that is stored in a Lisp string. Do not pass text in a buffer
8083 that was alloca'd. */
8084
8085 void
8086 message1 (m)
8087 char *m;
8088 {
8089 message2 (m, (m ? strlen (m) : 0), 0);
8090 }
8091
8092
8093 /* The non-logging counterpart of message1. */
8094
8095 void
8096 message1_nolog (m)
8097 char *m;
8098 {
8099 message2_nolog (m, (m ? strlen (m) : 0), 0);
8100 }
8101
8102 /* Display a message M which contains a single %s
8103 which gets replaced with STRING. */
8104
8105 void
8106 message_with_string (m, string, log)
8107 char *m;
8108 Lisp_Object string;
8109 int log;
8110 {
8111 CHECK_STRING (string);
8112
8113 if (noninteractive)
8114 {
8115 if (m)
8116 {
8117 if (noninteractive_need_newline)
8118 putc ('\n', stderr);
8119 noninteractive_need_newline = 0;
8120 fprintf (stderr, m, SDATA (string));
8121 if (!cursor_in_echo_area)
8122 fprintf (stderr, "\n");
8123 fflush (stderr);
8124 }
8125 }
8126 else if (INTERACTIVE)
8127 {
8128 /* The frame whose minibuffer we're going to display the message on.
8129 It may be larger than the selected frame, so we need
8130 to use its buffer, not the selected frame's buffer. */
8131 Lisp_Object mini_window;
8132 struct frame *f, *sf = SELECTED_FRAME ();
8133
8134 /* Get the frame containing the minibuffer
8135 that the selected frame is using. */
8136 mini_window = FRAME_MINIBUF_WINDOW (sf);
8137 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8138
8139 /* A null message buffer means that the frame hasn't really been
8140 initialized yet. Error messages get reported properly by
8141 cmd_error, so this must be just an informative message; toss it. */
8142 if (FRAME_MESSAGE_BUF (f))
8143 {
8144 Lisp_Object args[2], message;
8145 struct gcpro gcpro1, gcpro2;
8146
8147 args[0] = build_string (m);
8148 args[1] = message = string;
8149 GCPRO2 (args[0], message);
8150 gcpro1.nvars = 2;
8151
8152 message = Fformat (2, args);
8153
8154 if (log)
8155 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8156 else
8157 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8158
8159 UNGCPRO;
8160
8161 /* Print should start at the beginning of the message
8162 buffer next time. */
8163 message_buf_print = 0;
8164 }
8165 }
8166 }
8167
8168
8169 /* Dump an informative message to the minibuf. If M is 0, clear out
8170 any existing message, and let the mini-buffer text show through. */
8171
8172 /* VARARGS 1 */
8173 void
8174 message (m, a1, a2, a3)
8175 char *m;
8176 EMACS_INT a1, a2, a3;
8177 {
8178 if (noninteractive)
8179 {
8180 if (m)
8181 {
8182 if (noninteractive_need_newline)
8183 putc ('\n', stderr);
8184 noninteractive_need_newline = 0;
8185 fprintf (stderr, m, a1, a2, a3);
8186 if (cursor_in_echo_area == 0)
8187 fprintf (stderr, "\n");
8188 fflush (stderr);
8189 }
8190 }
8191 else if (INTERACTIVE)
8192 {
8193 /* The frame whose mini-buffer we're going to display the message
8194 on. It may be larger than the selected frame, so we need to
8195 use its buffer, not the selected frame's buffer. */
8196 Lisp_Object mini_window;
8197 struct frame *f, *sf = SELECTED_FRAME ();
8198
8199 /* Get the frame containing the mini-buffer
8200 that the selected frame is using. */
8201 mini_window = FRAME_MINIBUF_WINDOW (sf);
8202 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8203
8204 /* A null message buffer means that the frame hasn't really been
8205 initialized yet. Error messages get reported properly by
8206 cmd_error, so this must be just an informative message; toss
8207 it. */
8208 if (FRAME_MESSAGE_BUF (f))
8209 {
8210 if (m)
8211 {
8212 int len;
8213 #ifdef NO_ARG_ARRAY
8214 char *a[3];
8215 a[0] = (char *) a1;
8216 a[1] = (char *) a2;
8217 a[2] = (char *) a3;
8218
8219 len = doprnt (FRAME_MESSAGE_BUF (f),
8220 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8221 #else
8222 len = doprnt (FRAME_MESSAGE_BUF (f),
8223 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8224 (char **) &a1);
8225 #endif /* NO_ARG_ARRAY */
8226
8227 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8228 }
8229 else
8230 message1 (0);
8231
8232 /* Print should start at the beginning of the message
8233 buffer next time. */
8234 message_buf_print = 0;
8235 }
8236 }
8237 }
8238
8239
8240 /* The non-logging version of message. */
8241
8242 void
8243 message_nolog (m, a1, a2, a3)
8244 char *m;
8245 EMACS_INT a1, a2, a3;
8246 {
8247 Lisp_Object old_log_max;
8248 old_log_max = Vmessage_log_max;
8249 Vmessage_log_max = Qnil;
8250 message (m, a1, a2, a3);
8251 Vmessage_log_max = old_log_max;
8252 }
8253
8254
8255 /* Display the current message in the current mini-buffer. This is
8256 only called from error handlers in process.c, and is not time
8257 critical. */
8258
8259 void
8260 update_echo_area ()
8261 {
8262 if (!NILP (echo_area_buffer[0]))
8263 {
8264 Lisp_Object string;
8265 string = Fcurrent_message ();
8266 message3 (string, SBYTES (string),
8267 !NILP (current_buffer->enable_multibyte_characters));
8268 }
8269 }
8270
8271
8272 /* Make sure echo area buffers in `echo_buffers' are live.
8273 If they aren't, make new ones. */
8274
8275 static void
8276 ensure_echo_area_buffers ()
8277 {
8278 int i;
8279
8280 for (i = 0; i < 2; ++i)
8281 if (!BUFFERP (echo_buffer[i])
8282 || NILP (XBUFFER (echo_buffer[i])->name))
8283 {
8284 char name[30];
8285 Lisp_Object old_buffer;
8286 int j;
8287
8288 old_buffer = echo_buffer[i];
8289 sprintf (name, " *Echo Area %d*", i);
8290 echo_buffer[i] = Fget_buffer_create (build_string (name));
8291 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8292 /* to force word wrap in echo area -
8293 it was decided to postpone this*/
8294 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8295
8296 for (j = 0; j < 2; ++j)
8297 if (EQ (old_buffer, echo_area_buffer[j]))
8298 echo_area_buffer[j] = echo_buffer[i];
8299 }
8300 }
8301
8302
8303 /* Call FN with args A1..A4 with either the current or last displayed
8304 echo_area_buffer as current buffer.
8305
8306 WHICH zero means use the current message buffer
8307 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8308 from echo_buffer[] and clear it.
8309
8310 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8311 suitable buffer from echo_buffer[] and clear it.
8312
8313 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8314 that the current message becomes the last displayed one, make
8315 choose a suitable buffer for echo_area_buffer[0], and clear it.
8316
8317 Value is what FN returns. */
8318
8319 static int
8320 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8321 struct window *w;
8322 int which;
8323 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8324 EMACS_INT a1;
8325 Lisp_Object a2;
8326 EMACS_INT a3, a4;
8327 {
8328 Lisp_Object buffer;
8329 int this_one, the_other, clear_buffer_p, rc;
8330 int count = SPECPDL_INDEX ();
8331
8332 /* If buffers aren't live, make new ones. */
8333 ensure_echo_area_buffers ();
8334
8335 clear_buffer_p = 0;
8336
8337 if (which == 0)
8338 this_one = 0, the_other = 1;
8339 else if (which > 0)
8340 this_one = 1, the_other = 0;
8341 else
8342 {
8343 this_one = 0, the_other = 1;
8344 clear_buffer_p = 1;
8345
8346 /* We need a fresh one in case the current echo buffer equals
8347 the one containing the last displayed echo area message. */
8348 if (!NILP (echo_area_buffer[this_one])
8349 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8350 echo_area_buffer[this_one] = Qnil;
8351 }
8352
8353 /* Choose a suitable buffer from echo_buffer[] is we don't
8354 have one. */
8355 if (NILP (echo_area_buffer[this_one]))
8356 {
8357 echo_area_buffer[this_one]
8358 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8359 ? echo_buffer[the_other]
8360 : echo_buffer[this_one]);
8361 clear_buffer_p = 1;
8362 }
8363
8364 buffer = echo_area_buffer[this_one];
8365
8366 /* Don't get confused by reusing the buffer used for echoing
8367 for a different purpose. */
8368 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8369 cancel_echoing ();
8370
8371 record_unwind_protect (unwind_with_echo_area_buffer,
8372 with_echo_area_buffer_unwind_data (w));
8373
8374 /* Make the echo area buffer current. Note that for display
8375 purposes, it is not necessary that the displayed window's buffer
8376 == current_buffer, except for text property lookup. So, let's
8377 only set that buffer temporarily here without doing a full
8378 Fset_window_buffer. We must also change w->pointm, though,
8379 because otherwise an assertions in unshow_buffer fails, and Emacs
8380 aborts. */
8381 set_buffer_internal_1 (XBUFFER (buffer));
8382 if (w)
8383 {
8384 w->buffer = buffer;
8385 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8386 }
8387
8388 current_buffer->undo_list = Qt;
8389 current_buffer->read_only = Qnil;
8390 specbind (Qinhibit_read_only, Qt);
8391 specbind (Qinhibit_modification_hooks, Qt);
8392
8393 if (clear_buffer_p && Z > BEG)
8394 del_range (BEG, Z);
8395
8396 xassert (BEGV >= BEG);
8397 xassert (ZV <= Z && ZV >= BEGV);
8398
8399 rc = fn (a1, a2, a3, a4);
8400
8401 xassert (BEGV >= BEG);
8402 xassert (ZV <= Z && ZV >= BEGV);
8403
8404 unbind_to (count, Qnil);
8405 return rc;
8406 }
8407
8408
8409 /* Save state that should be preserved around the call to the function
8410 FN called in with_echo_area_buffer. */
8411
8412 static Lisp_Object
8413 with_echo_area_buffer_unwind_data (w)
8414 struct window *w;
8415 {
8416 int i = 0;
8417 Lisp_Object vector, tmp;
8418
8419 /* Reduce consing by keeping one vector in
8420 Vwith_echo_area_save_vector. */
8421 vector = Vwith_echo_area_save_vector;
8422 Vwith_echo_area_save_vector = Qnil;
8423
8424 if (NILP (vector))
8425 vector = Fmake_vector (make_number (7), Qnil);
8426
8427 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8428 ASET (vector, i, Vdeactivate_mark); ++i;
8429 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8430
8431 if (w)
8432 {
8433 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8434 ASET (vector, i, w->buffer); ++i;
8435 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8436 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8437 }
8438 else
8439 {
8440 int end = i + 4;
8441 for (; i < end; ++i)
8442 ASET (vector, i, Qnil);
8443 }
8444
8445 xassert (i == ASIZE (vector));
8446 return vector;
8447 }
8448
8449
8450 /* Restore global state from VECTOR which was created by
8451 with_echo_area_buffer_unwind_data. */
8452
8453 static Lisp_Object
8454 unwind_with_echo_area_buffer (vector)
8455 Lisp_Object vector;
8456 {
8457 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8458 Vdeactivate_mark = AREF (vector, 1);
8459 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8460
8461 if (WINDOWP (AREF (vector, 3)))
8462 {
8463 struct window *w;
8464 Lisp_Object buffer, charpos, bytepos;
8465
8466 w = XWINDOW (AREF (vector, 3));
8467 buffer = AREF (vector, 4);
8468 charpos = AREF (vector, 5);
8469 bytepos = AREF (vector, 6);
8470
8471 w->buffer = buffer;
8472 set_marker_both (w->pointm, buffer,
8473 XFASTINT (charpos), XFASTINT (bytepos));
8474 }
8475
8476 Vwith_echo_area_save_vector = vector;
8477 return Qnil;
8478 }
8479
8480
8481 /* Set up the echo area for use by print functions. MULTIBYTE_P
8482 non-zero means we will print multibyte. */
8483
8484 void
8485 setup_echo_area_for_printing (multibyte_p)
8486 int multibyte_p;
8487 {
8488 /* If we can't find an echo area any more, exit. */
8489 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8490 Fkill_emacs (Qnil);
8491
8492 ensure_echo_area_buffers ();
8493
8494 if (!message_buf_print)
8495 {
8496 /* A message has been output since the last time we printed.
8497 Choose a fresh echo area buffer. */
8498 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8499 echo_area_buffer[0] = echo_buffer[1];
8500 else
8501 echo_area_buffer[0] = echo_buffer[0];
8502
8503 /* Switch to that buffer and clear it. */
8504 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8505 current_buffer->truncate_lines = Qnil;
8506
8507 if (Z > BEG)
8508 {
8509 int count = SPECPDL_INDEX ();
8510 specbind (Qinhibit_read_only, Qt);
8511 /* Note that undo recording is always disabled. */
8512 del_range (BEG, Z);
8513 unbind_to (count, Qnil);
8514 }
8515 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8516
8517 /* Set up the buffer for the multibyteness we need. */
8518 if (multibyte_p
8519 != !NILP (current_buffer->enable_multibyte_characters))
8520 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8521
8522 /* Raise the frame containing the echo area. */
8523 if (minibuffer_auto_raise)
8524 {
8525 struct frame *sf = SELECTED_FRAME ();
8526 Lisp_Object mini_window;
8527 mini_window = FRAME_MINIBUF_WINDOW (sf);
8528 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8529 }
8530
8531 message_log_maybe_newline ();
8532 message_buf_print = 1;
8533 }
8534 else
8535 {
8536 if (NILP (echo_area_buffer[0]))
8537 {
8538 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8539 echo_area_buffer[0] = echo_buffer[1];
8540 else
8541 echo_area_buffer[0] = echo_buffer[0];
8542 }
8543
8544 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8545 {
8546 /* Someone switched buffers between print requests. */
8547 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8548 current_buffer->truncate_lines = Qnil;
8549 }
8550 }
8551 }
8552
8553
8554 /* Display an echo area message in window W. Value is non-zero if W's
8555 height is changed. If display_last_displayed_message_p is
8556 non-zero, display the message that was last displayed, otherwise
8557 display the current message. */
8558
8559 static int
8560 display_echo_area (w)
8561 struct window *w;
8562 {
8563 int i, no_message_p, window_height_changed_p, count;
8564
8565 /* Temporarily disable garbage collections while displaying the echo
8566 area. This is done because a GC can print a message itself.
8567 That message would modify the echo area buffer's contents while a
8568 redisplay of the buffer is going on, and seriously confuse
8569 redisplay. */
8570 count = inhibit_garbage_collection ();
8571
8572 /* If there is no message, we must call display_echo_area_1
8573 nevertheless because it resizes the window. But we will have to
8574 reset the echo_area_buffer in question to nil at the end because
8575 with_echo_area_buffer will sets it to an empty buffer. */
8576 i = display_last_displayed_message_p ? 1 : 0;
8577 no_message_p = NILP (echo_area_buffer[i]);
8578
8579 window_height_changed_p
8580 = with_echo_area_buffer (w, display_last_displayed_message_p,
8581 display_echo_area_1,
8582 (EMACS_INT) w, Qnil, 0, 0);
8583
8584 if (no_message_p)
8585 echo_area_buffer[i] = Qnil;
8586
8587 unbind_to (count, Qnil);
8588 return window_height_changed_p;
8589 }
8590
8591
8592 /* Helper for display_echo_area. Display the current buffer which
8593 contains the current echo area message in window W, a mini-window,
8594 a pointer to which is passed in A1. A2..A4 are currently not used.
8595 Change the height of W so that all of the message is displayed.
8596 Value is non-zero if height of W was changed. */
8597
8598 static int
8599 display_echo_area_1 (a1, a2, a3, a4)
8600 EMACS_INT a1;
8601 Lisp_Object a2;
8602 EMACS_INT a3, a4;
8603 {
8604 struct window *w = (struct window *) a1;
8605 Lisp_Object window;
8606 struct text_pos start;
8607 int window_height_changed_p = 0;
8608
8609 /* Do this before displaying, so that we have a large enough glyph
8610 matrix for the display. If we can't get enough space for the
8611 whole text, display the last N lines. That works by setting w->start. */
8612 window_height_changed_p = resize_mini_window (w, 0);
8613
8614 /* Use the starting position chosen by resize_mini_window. */
8615 SET_TEXT_POS_FROM_MARKER (start, w->start);
8616
8617 /* Display. */
8618 clear_glyph_matrix (w->desired_matrix);
8619 XSETWINDOW (window, w);
8620 try_window (window, start, 0);
8621
8622 return window_height_changed_p;
8623 }
8624
8625
8626 /* Resize the echo area window to exactly the size needed for the
8627 currently displayed message, if there is one. If a mini-buffer
8628 is active, don't shrink it. */
8629
8630 void
8631 resize_echo_area_exactly ()
8632 {
8633 if (BUFFERP (echo_area_buffer[0])
8634 && WINDOWP (echo_area_window))
8635 {
8636 struct window *w = XWINDOW (echo_area_window);
8637 int resized_p;
8638 Lisp_Object resize_exactly;
8639
8640 if (minibuf_level == 0)
8641 resize_exactly = Qt;
8642 else
8643 resize_exactly = Qnil;
8644
8645 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8646 (EMACS_INT) w, resize_exactly, 0, 0);
8647 if (resized_p)
8648 {
8649 ++windows_or_buffers_changed;
8650 ++update_mode_lines;
8651 redisplay_internal (0);
8652 }
8653 }
8654 }
8655
8656
8657 /* Callback function for with_echo_area_buffer, when used from
8658 resize_echo_area_exactly. A1 contains a pointer to the window to
8659 resize, EXACTLY non-nil means resize the mini-window exactly to the
8660 size of the text displayed. A3 and A4 are not used. Value is what
8661 resize_mini_window returns. */
8662
8663 static int
8664 resize_mini_window_1 (a1, exactly, a3, a4)
8665 EMACS_INT a1;
8666 Lisp_Object exactly;
8667 EMACS_INT a3, a4;
8668 {
8669 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8670 }
8671
8672
8673 /* Resize mini-window W to fit the size of its contents. EXACT_P
8674 means size the window exactly to the size needed. Otherwise, it's
8675 only enlarged until W's buffer is empty.
8676
8677 Set W->start to the right place to begin display. If the whole
8678 contents fit, start at the beginning. Otherwise, start so as
8679 to make the end of the contents appear. This is particularly
8680 important for y-or-n-p, but seems desirable generally.
8681
8682 Value is non-zero if the window height has been changed. */
8683
8684 int
8685 resize_mini_window (w, exact_p)
8686 struct window *w;
8687 int exact_p;
8688 {
8689 struct frame *f = XFRAME (w->frame);
8690 int window_height_changed_p = 0;
8691
8692 xassert (MINI_WINDOW_P (w));
8693
8694 /* By default, start display at the beginning. */
8695 set_marker_both (w->start, w->buffer,
8696 BUF_BEGV (XBUFFER (w->buffer)),
8697 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8698
8699 /* Don't resize windows while redisplaying a window; it would
8700 confuse redisplay functions when the size of the window they are
8701 displaying changes from under them. Such a resizing can happen,
8702 for instance, when which-func prints a long message while
8703 we are running fontification-functions. We're running these
8704 functions with safe_call which binds inhibit-redisplay to t. */
8705 if (!NILP (Vinhibit_redisplay))
8706 return 0;
8707
8708 /* Nil means don't try to resize. */
8709 if (NILP (Vresize_mini_windows)
8710 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8711 return 0;
8712
8713 if (!FRAME_MINIBUF_ONLY_P (f))
8714 {
8715 struct it it;
8716 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8717 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8718 int height, max_height;
8719 int unit = FRAME_LINE_HEIGHT (f);
8720 struct text_pos start;
8721 struct buffer *old_current_buffer = NULL;
8722
8723 if (current_buffer != XBUFFER (w->buffer))
8724 {
8725 old_current_buffer = current_buffer;
8726 set_buffer_internal (XBUFFER (w->buffer));
8727 }
8728
8729 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8730
8731 /* Compute the max. number of lines specified by the user. */
8732 if (FLOATP (Vmax_mini_window_height))
8733 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8734 else if (INTEGERP (Vmax_mini_window_height))
8735 max_height = XINT (Vmax_mini_window_height);
8736 else
8737 max_height = total_height / 4;
8738
8739 /* Correct that max. height if it's bogus. */
8740 max_height = max (1, max_height);
8741 max_height = min (total_height, max_height);
8742
8743 /* Find out the height of the text in the window. */
8744 if (it.line_wrap == TRUNCATE)
8745 height = 1;
8746 else
8747 {
8748 last_height = 0;
8749 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8750 if (it.max_ascent == 0 && it.max_descent == 0)
8751 height = it.current_y + last_height;
8752 else
8753 height = it.current_y + it.max_ascent + it.max_descent;
8754 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8755 height = (height + unit - 1) / unit;
8756 }
8757
8758 /* Compute a suitable window start. */
8759 if (height > max_height)
8760 {
8761 height = max_height;
8762 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8763 move_it_vertically_backward (&it, (height - 1) * unit);
8764 start = it.current.pos;
8765 }
8766 else
8767 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8768 SET_MARKER_FROM_TEXT_POS (w->start, start);
8769
8770 if (EQ (Vresize_mini_windows, Qgrow_only))
8771 {
8772 /* Let it grow only, until we display an empty message, in which
8773 case the window shrinks again. */
8774 if (height > WINDOW_TOTAL_LINES (w))
8775 {
8776 int old_height = WINDOW_TOTAL_LINES (w);
8777 freeze_window_starts (f, 1);
8778 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8779 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8780 }
8781 else if (height < WINDOW_TOTAL_LINES (w)
8782 && (exact_p || BEGV == ZV))
8783 {
8784 int old_height = WINDOW_TOTAL_LINES (w);
8785 freeze_window_starts (f, 0);
8786 shrink_mini_window (w);
8787 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8788 }
8789 }
8790 else
8791 {
8792 /* Always resize to exact size needed. */
8793 if (height > WINDOW_TOTAL_LINES (w))
8794 {
8795 int old_height = WINDOW_TOTAL_LINES (w);
8796 freeze_window_starts (f, 1);
8797 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8798 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8799 }
8800 else if (height < WINDOW_TOTAL_LINES (w))
8801 {
8802 int old_height = WINDOW_TOTAL_LINES (w);
8803 freeze_window_starts (f, 0);
8804 shrink_mini_window (w);
8805
8806 if (height)
8807 {
8808 freeze_window_starts (f, 1);
8809 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8810 }
8811
8812 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8813 }
8814 }
8815
8816 if (old_current_buffer)
8817 set_buffer_internal (old_current_buffer);
8818 }
8819
8820 return window_height_changed_p;
8821 }
8822
8823
8824 /* Value is the current message, a string, or nil if there is no
8825 current message. */
8826
8827 Lisp_Object
8828 current_message ()
8829 {
8830 Lisp_Object msg;
8831
8832 if (!BUFFERP (echo_area_buffer[0]))
8833 msg = Qnil;
8834 else
8835 {
8836 with_echo_area_buffer (0, 0, current_message_1,
8837 (EMACS_INT) &msg, Qnil, 0, 0);
8838 if (NILP (msg))
8839 echo_area_buffer[0] = Qnil;
8840 }
8841
8842 return msg;
8843 }
8844
8845
8846 static int
8847 current_message_1 (a1, a2, a3, a4)
8848 EMACS_INT a1;
8849 Lisp_Object a2;
8850 EMACS_INT a3, a4;
8851 {
8852 Lisp_Object *msg = (Lisp_Object *) a1;
8853
8854 if (Z > BEG)
8855 *msg = make_buffer_string (BEG, Z, 1);
8856 else
8857 *msg = Qnil;
8858 return 0;
8859 }
8860
8861
8862 /* Push the current message on Vmessage_stack for later restauration
8863 by restore_message. Value is non-zero if the current message isn't
8864 empty. This is a relatively infrequent operation, so it's not
8865 worth optimizing. */
8866
8867 int
8868 push_message ()
8869 {
8870 Lisp_Object msg;
8871 msg = current_message ();
8872 Vmessage_stack = Fcons (msg, Vmessage_stack);
8873 return STRINGP (msg);
8874 }
8875
8876
8877 /* Restore message display from the top of Vmessage_stack. */
8878
8879 void
8880 restore_message ()
8881 {
8882 Lisp_Object msg;
8883
8884 xassert (CONSP (Vmessage_stack));
8885 msg = XCAR (Vmessage_stack);
8886 if (STRINGP (msg))
8887 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8888 else
8889 message3_nolog (msg, 0, 0);
8890 }
8891
8892
8893 /* Handler for record_unwind_protect calling pop_message. */
8894
8895 Lisp_Object
8896 pop_message_unwind (dummy)
8897 Lisp_Object dummy;
8898 {
8899 pop_message ();
8900 return Qnil;
8901 }
8902
8903 /* Pop the top-most entry off Vmessage_stack. */
8904
8905 void
8906 pop_message ()
8907 {
8908 xassert (CONSP (Vmessage_stack));
8909 Vmessage_stack = XCDR (Vmessage_stack);
8910 }
8911
8912
8913 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8914 exits. If the stack is not empty, we have a missing pop_message
8915 somewhere. */
8916
8917 void
8918 check_message_stack ()
8919 {
8920 if (!NILP (Vmessage_stack))
8921 abort ();
8922 }
8923
8924
8925 /* Truncate to NCHARS what will be displayed in the echo area the next
8926 time we display it---but don't redisplay it now. */
8927
8928 void
8929 truncate_echo_area (nchars)
8930 int nchars;
8931 {
8932 if (nchars == 0)
8933 echo_area_buffer[0] = Qnil;
8934 /* A null message buffer means that the frame hasn't really been
8935 initialized yet. Error messages get reported properly by
8936 cmd_error, so this must be just an informative message; toss it. */
8937 else if (!noninteractive
8938 && INTERACTIVE
8939 && !NILP (echo_area_buffer[0]))
8940 {
8941 struct frame *sf = SELECTED_FRAME ();
8942 if (FRAME_MESSAGE_BUF (sf))
8943 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8944 }
8945 }
8946
8947
8948 /* Helper function for truncate_echo_area. Truncate the current
8949 message to at most NCHARS characters. */
8950
8951 static int
8952 truncate_message_1 (nchars, a2, a3, a4)
8953 EMACS_INT nchars;
8954 Lisp_Object a2;
8955 EMACS_INT a3, a4;
8956 {
8957 if (BEG + nchars < Z)
8958 del_range (BEG + nchars, Z);
8959 if (Z == BEG)
8960 echo_area_buffer[0] = Qnil;
8961 return 0;
8962 }
8963
8964
8965 /* Set the current message to a substring of S or STRING.
8966
8967 If STRING is a Lisp string, set the message to the first NBYTES
8968 bytes from STRING. NBYTES zero means use the whole string. If
8969 STRING is multibyte, the message will be displayed multibyte.
8970
8971 If S is not null, set the message to the first LEN bytes of S. LEN
8972 zero means use the whole string. MULTIBYTE_P non-zero means S is
8973 multibyte. Display the message multibyte in that case.
8974
8975 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8976 to t before calling set_message_1 (which calls insert).
8977 */
8978
8979 void
8980 set_message (s, string, nbytes, multibyte_p)
8981 const char *s;
8982 Lisp_Object string;
8983 int nbytes, multibyte_p;
8984 {
8985 message_enable_multibyte
8986 = ((s && multibyte_p)
8987 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8988
8989 with_echo_area_buffer (0, -1, set_message_1,
8990 (EMACS_INT) s, string, nbytes, multibyte_p);
8991 message_buf_print = 0;
8992 help_echo_showing_p = 0;
8993 }
8994
8995
8996 /* Helper function for set_message. Arguments have the same meaning
8997 as there, with A1 corresponding to S and A2 corresponding to STRING
8998 This function is called with the echo area buffer being
8999 current. */
9000
9001 static int
9002 set_message_1 (a1, a2, nbytes, multibyte_p)
9003 EMACS_INT a1;
9004 Lisp_Object a2;
9005 EMACS_INT nbytes, multibyte_p;
9006 {
9007 const char *s = (const char *) a1;
9008 Lisp_Object string = a2;
9009
9010 /* Change multibyteness of the echo buffer appropriately. */
9011 if (message_enable_multibyte
9012 != !NILP (current_buffer->enable_multibyte_characters))
9013 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9014
9015 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9016
9017 /* Insert new message at BEG. */
9018 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9019
9020 if (STRINGP (string))
9021 {
9022 int nchars;
9023
9024 if (nbytes == 0)
9025 nbytes = SBYTES (string);
9026 nchars = string_byte_to_char (string, nbytes);
9027
9028 /* This function takes care of single/multibyte conversion. We
9029 just have to ensure that the echo area buffer has the right
9030 setting of enable_multibyte_characters. */
9031 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9032 }
9033 else if (s)
9034 {
9035 if (nbytes == 0)
9036 nbytes = strlen (s);
9037
9038 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9039 {
9040 /* Convert from multi-byte to single-byte. */
9041 int i, c, n;
9042 unsigned char work[1];
9043
9044 /* Convert a multibyte string to single-byte. */
9045 for (i = 0; i < nbytes; i += n)
9046 {
9047 c = string_char_and_length (s + i, nbytes - i, &n);
9048 work[0] = (ASCII_CHAR_P (c)
9049 ? c
9050 : multibyte_char_to_unibyte (c, Qnil));
9051 insert_1_both (work, 1, 1, 1, 0, 0);
9052 }
9053 }
9054 else if (!multibyte_p
9055 && !NILP (current_buffer->enable_multibyte_characters))
9056 {
9057 /* Convert from single-byte to multi-byte. */
9058 int i, c, n;
9059 const unsigned char *msg = (const unsigned char *) s;
9060 unsigned char str[MAX_MULTIBYTE_LENGTH];
9061
9062 /* Convert a single-byte string to multibyte. */
9063 for (i = 0; i < nbytes; i++)
9064 {
9065 c = msg[i];
9066 c = unibyte_char_to_multibyte (c);
9067 n = CHAR_STRING (c, str);
9068 insert_1_both (str, 1, n, 1, 0, 0);
9069 }
9070 }
9071 else
9072 insert_1 (s, nbytes, 1, 0, 0);
9073 }
9074
9075 return 0;
9076 }
9077
9078
9079 /* Clear messages. CURRENT_P non-zero means clear the current
9080 message. LAST_DISPLAYED_P non-zero means clear the message
9081 last displayed. */
9082
9083 void
9084 clear_message (current_p, last_displayed_p)
9085 int current_p, last_displayed_p;
9086 {
9087 if (current_p)
9088 {
9089 echo_area_buffer[0] = Qnil;
9090 message_cleared_p = 1;
9091 }
9092
9093 if (last_displayed_p)
9094 echo_area_buffer[1] = Qnil;
9095
9096 message_buf_print = 0;
9097 }
9098
9099 /* Clear garbaged frames.
9100
9101 This function is used where the old redisplay called
9102 redraw_garbaged_frames which in turn called redraw_frame which in
9103 turn called clear_frame. The call to clear_frame was a source of
9104 flickering. I believe a clear_frame is not necessary. It should
9105 suffice in the new redisplay to invalidate all current matrices,
9106 and ensure a complete redisplay of all windows. */
9107
9108 static void
9109 clear_garbaged_frames ()
9110 {
9111 if (frame_garbaged)
9112 {
9113 Lisp_Object tail, frame;
9114 int changed_count = 0;
9115
9116 FOR_EACH_FRAME (tail, frame)
9117 {
9118 struct frame *f = XFRAME (frame);
9119
9120 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9121 {
9122 if (f->resized_p)
9123 {
9124 Fredraw_frame (frame);
9125 f->force_flush_display_p = 1;
9126 }
9127 clear_current_matrices (f);
9128 changed_count++;
9129 f->garbaged = 0;
9130 f->resized_p = 0;
9131 }
9132 }
9133
9134 frame_garbaged = 0;
9135 if (changed_count)
9136 ++windows_or_buffers_changed;
9137 }
9138 }
9139
9140
9141 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9142 is non-zero update selected_frame. Value is non-zero if the
9143 mini-windows height has been changed. */
9144
9145 static int
9146 echo_area_display (update_frame_p)
9147 int update_frame_p;
9148 {
9149 Lisp_Object mini_window;
9150 struct window *w;
9151 struct frame *f;
9152 int window_height_changed_p = 0;
9153 struct frame *sf = SELECTED_FRAME ();
9154
9155 mini_window = FRAME_MINIBUF_WINDOW (sf);
9156 w = XWINDOW (mini_window);
9157 f = XFRAME (WINDOW_FRAME (w));
9158
9159 /* Don't display if frame is invisible or not yet initialized. */
9160 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9161 return 0;
9162
9163 #ifdef HAVE_WINDOW_SYSTEM
9164 /* When Emacs starts, selected_frame may be the initial terminal
9165 frame. If we let this through, a message would be displayed on
9166 the terminal. */
9167 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9168 return 0;
9169 #endif /* HAVE_WINDOW_SYSTEM */
9170
9171 /* Redraw garbaged frames. */
9172 if (frame_garbaged)
9173 clear_garbaged_frames ();
9174
9175 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9176 {
9177 echo_area_window = mini_window;
9178 window_height_changed_p = display_echo_area (w);
9179 w->must_be_updated_p = 1;
9180
9181 /* Update the display, unless called from redisplay_internal.
9182 Also don't update the screen during redisplay itself. The
9183 update will happen at the end of redisplay, and an update
9184 here could cause confusion. */
9185 if (update_frame_p && !redisplaying_p)
9186 {
9187 int n = 0;
9188
9189 /* If the display update has been interrupted by pending
9190 input, update mode lines in the frame. Due to the
9191 pending input, it might have been that redisplay hasn't
9192 been called, so that mode lines above the echo area are
9193 garbaged. This looks odd, so we prevent it here. */
9194 if (!display_completed)
9195 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9196
9197 if (window_height_changed_p
9198 /* Don't do this if Emacs is shutting down. Redisplay
9199 needs to run hooks. */
9200 && !NILP (Vrun_hooks))
9201 {
9202 /* Must update other windows. Likewise as in other
9203 cases, don't let this update be interrupted by
9204 pending input. */
9205 int count = SPECPDL_INDEX ();
9206 specbind (Qredisplay_dont_pause, Qt);
9207 windows_or_buffers_changed = 1;
9208 redisplay_internal (0);
9209 unbind_to (count, Qnil);
9210 }
9211 else if (FRAME_WINDOW_P (f) && n == 0)
9212 {
9213 /* Window configuration is the same as before.
9214 Can do with a display update of the echo area,
9215 unless we displayed some mode lines. */
9216 update_single_window (w, 1);
9217 FRAME_RIF (f)->flush_display (f);
9218 }
9219 else
9220 update_frame (f, 1, 1);
9221
9222 /* If cursor is in the echo area, make sure that the next
9223 redisplay displays the minibuffer, so that the cursor will
9224 be replaced with what the minibuffer wants. */
9225 if (cursor_in_echo_area)
9226 ++windows_or_buffers_changed;
9227 }
9228 }
9229 else if (!EQ (mini_window, selected_window))
9230 windows_or_buffers_changed++;
9231
9232 /* Last displayed message is now the current message. */
9233 echo_area_buffer[1] = echo_area_buffer[0];
9234 /* Inform read_char that we're not echoing. */
9235 echo_message_buffer = Qnil;
9236
9237 /* Prevent redisplay optimization in redisplay_internal by resetting
9238 this_line_start_pos. This is done because the mini-buffer now
9239 displays the message instead of its buffer text. */
9240 if (EQ (mini_window, selected_window))
9241 CHARPOS (this_line_start_pos) = 0;
9242
9243 return window_height_changed_p;
9244 }
9245
9246
9247 \f
9248 /***********************************************************************
9249 Mode Lines and Frame Titles
9250 ***********************************************************************/
9251
9252 /* A buffer for constructing non-propertized mode-line strings and
9253 frame titles in it; allocated from the heap in init_xdisp and
9254 resized as needed in store_mode_line_noprop_char. */
9255
9256 static char *mode_line_noprop_buf;
9257
9258 /* The buffer's end, and a current output position in it. */
9259
9260 static char *mode_line_noprop_buf_end;
9261 static char *mode_line_noprop_ptr;
9262
9263 #define MODE_LINE_NOPROP_LEN(start) \
9264 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9265
9266 static enum {
9267 MODE_LINE_DISPLAY = 0,
9268 MODE_LINE_TITLE,
9269 MODE_LINE_NOPROP,
9270 MODE_LINE_STRING
9271 } mode_line_target;
9272
9273 /* Alist that caches the results of :propertize.
9274 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9275 static Lisp_Object mode_line_proptrans_alist;
9276
9277 /* List of strings making up the mode-line. */
9278 static Lisp_Object mode_line_string_list;
9279
9280 /* Base face property when building propertized mode line string. */
9281 static Lisp_Object mode_line_string_face;
9282 static Lisp_Object mode_line_string_face_prop;
9283
9284
9285 /* Unwind data for mode line strings */
9286
9287 static Lisp_Object Vmode_line_unwind_vector;
9288
9289 static Lisp_Object
9290 format_mode_line_unwind_data (struct buffer *obuf,
9291 Lisp_Object owin,
9292 int save_proptrans)
9293 {
9294 Lisp_Object vector, tmp;
9295
9296 /* Reduce consing by keeping one vector in
9297 Vwith_echo_area_save_vector. */
9298 vector = Vmode_line_unwind_vector;
9299 Vmode_line_unwind_vector = Qnil;
9300
9301 if (NILP (vector))
9302 vector = Fmake_vector (make_number (8), Qnil);
9303
9304 ASET (vector, 0, make_number (mode_line_target));
9305 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9306 ASET (vector, 2, mode_line_string_list);
9307 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9308 ASET (vector, 4, mode_line_string_face);
9309 ASET (vector, 5, mode_line_string_face_prop);
9310
9311 if (obuf)
9312 XSETBUFFER (tmp, obuf);
9313 else
9314 tmp = Qnil;
9315 ASET (vector, 6, tmp);
9316 ASET (vector, 7, owin);
9317
9318 return vector;
9319 }
9320
9321 static Lisp_Object
9322 unwind_format_mode_line (vector)
9323 Lisp_Object vector;
9324 {
9325 mode_line_target = XINT (AREF (vector, 0));
9326 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9327 mode_line_string_list = AREF (vector, 2);
9328 if (! EQ (AREF (vector, 3), Qt))
9329 mode_line_proptrans_alist = AREF (vector, 3);
9330 mode_line_string_face = AREF (vector, 4);
9331 mode_line_string_face_prop = AREF (vector, 5);
9332
9333 if (!NILP (AREF (vector, 7)))
9334 /* Select window before buffer, since it may change the buffer. */
9335 Fselect_window (AREF (vector, 7), Qt);
9336
9337 if (!NILP (AREF (vector, 6)))
9338 {
9339 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9340 ASET (vector, 6, Qnil);
9341 }
9342
9343 Vmode_line_unwind_vector = vector;
9344 return Qnil;
9345 }
9346
9347
9348 /* Store a single character C for the frame title in mode_line_noprop_buf.
9349 Re-allocate mode_line_noprop_buf if necessary. */
9350
9351 static void
9352 #ifdef PROTOTYPES
9353 store_mode_line_noprop_char (char c)
9354 #else
9355 store_mode_line_noprop_char (c)
9356 char c;
9357 #endif
9358 {
9359 /* If output position has reached the end of the allocated buffer,
9360 double the buffer's size. */
9361 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9362 {
9363 int len = MODE_LINE_NOPROP_LEN (0);
9364 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9365 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9366 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9367 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9368 }
9369
9370 *mode_line_noprop_ptr++ = c;
9371 }
9372
9373
9374 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9375 mode_line_noprop_ptr. STR is the string to store. Do not copy
9376 characters that yield more columns than PRECISION; PRECISION <= 0
9377 means copy the whole string. Pad with spaces until FIELD_WIDTH
9378 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9379 pad. Called from display_mode_element when it is used to build a
9380 frame title. */
9381
9382 static int
9383 store_mode_line_noprop (str, field_width, precision)
9384 const unsigned char *str;
9385 int field_width, precision;
9386 {
9387 int n = 0;
9388 int dummy, nbytes;
9389
9390 /* Copy at most PRECISION chars from STR. */
9391 nbytes = strlen (str);
9392 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9393 while (nbytes--)
9394 store_mode_line_noprop_char (*str++);
9395
9396 /* Fill up with spaces until FIELD_WIDTH reached. */
9397 while (field_width > 0
9398 && n < field_width)
9399 {
9400 store_mode_line_noprop_char (' ');
9401 ++n;
9402 }
9403
9404 return n;
9405 }
9406
9407 /***********************************************************************
9408 Frame Titles
9409 ***********************************************************************/
9410
9411 #ifdef HAVE_WINDOW_SYSTEM
9412
9413 /* Set the title of FRAME, if it has changed. The title format is
9414 Vicon_title_format if FRAME is iconified, otherwise it is
9415 frame_title_format. */
9416
9417 static void
9418 x_consider_frame_title (frame)
9419 Lisp_Object frame;
9420 {
9421 struct frame *f = XFRAME (frame);
9422
9423 if (FRAME_WINDOW_P (f)
9424 || FRAME_MINIBUF_ONLY_P (f)
9425 || f->explicit_name)
9426 {
9427 /* Do we have more than one visible frame on this X display? */
9428 Lisp_Object tail;
9429 Lisp_Object fmt;
9430 int title_start;
9431 char *title;
9432 int len;
9433 struct it it;
9434 int count = SPECPDL_INDEX ();
9435
9436 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9437 {
9438 Lisp_Object other_frame = XCAR (tail);
9439 struct frame *tf = XFRAME (other_frame);
9440
9441 if (tf != f
9442 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9443 && !FRAME_MINIBUF_ONLY_P (tf)
9444 && !EQ (other_frame, tip_frame)
9445 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9446 break;
9447 }
9448
9449 /* Set global variable indicating that multiple frames exist. */
9450 multiple_frames = CONSP (tail);
9451
9452 /* Switch to the buffer of selected window of the frame. Set up
9453 mode_line_target so that display_mode_element will output into
9454 mode_line_noprop_buf; then display the title. */
9455 record_unwind_protect (unwind_format_mode_line,
9456 format_mode_line_unwind_data
9457 (current_buffer, selected_window, 0));
9458
9459 Fselect_window (f->selected_window, Qt);
9460 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9461 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9462
9463 mode_line_target = MODE_LINE_TITLE;
9464 title_start = MODE_LINE_NOPROP_LEN (0);
9465 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9466 NULL, DEFAULT_FACE_ID);
9467 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9468 len = MODE_LINE_NOPROP_LEN (title_start);
9469 title = mode_line_noprop_buf + title_start;
9470 unbind_to (count, Qnil);
9471
9472 /* Set the title only if it's changed. This avoids consing in
9473 the common case where it hasn't. (If it turns out that we've
9474 already wasted too much time by walking through the list with
9475 display_mode_element, then we might need to optimize at a
9476 higher level than this.) */
9477 if (! STRINGP (f->name)
9478 || SBYTES (f->name) != len
9479 || bcmp (title, SDATA (f->name), len) != 0)
9480 {
9481 #ifdef HAVE_NS
9482 if (FRAME_NS_P (f))
9483 {
9484 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9485 {
9486 if (EQ (fmt, Qt))
9487 ns_set_name_as_filename (f);
9488 else
9489 x_implicitly_set_name (f, make_string(title, len),
9490 Qnil);
9491 }
9492 }
9493 else
9494 #endif
9495 x_implicitly_set_name (f, make_string (title, len), Qnil);
9496 }
9497 #ifdef HAVE_NS
9498 if (FRAME_NS_P (f))
9499 {
9500 /* do this also for frames with explicit names */
9501 ns_implicitly_set_icon_type(f);
9502 ns_set_doc_edited(f, Fbuffer_modified_p
9503 (XWINDOW (f->selected_window)->buffer), Qnil);
9504 }
9505 #endif
9506 }
9507 }
9508
9509 #endif /* not HAVE_WINDOW_SYSTEM */
9510
9511
9512
9513 \f
9514 /***********************************************************************
9515 Menu Bars
9516 ***********************************************************************/
9517
9518
9519 /* Prepare for redisplay by updating menu-bar item lists when
9520 appropriate. This can call eval. */
9521
9522 void
9523 prepare_menu_bars ()
9524 {
9525 int all_windows;
9526 struct gcpro gcpro1, gcpro2;
9527 struct frame *f;
9528 Lisp_Object tooltip_frame;
9529
9530 #ifdef HAVE_WINDOW_SYSTEM
9531 tooltip_frame = tip_frame;
9532 #else
9533 tooltip_frame = Qnil;
9534 #endif
9535
9536 /* Update all frame titles based on their buffer names, etc. We do
9537 this before the menu bars so that the buffer-menu will show the
9538 up-to-date frame titles. */
9539 #ifdef HAVE_WINDOW_SYSTEM
9540 if (windows_or_buffers_changed || update_mode_lines)
9541 {
9542 Lisp_Object tail, frame;
9543
9544 FOR_EACH_FRAME (tail, frame)
9545 {
9546 f = XFRAME (frame);
9547 if (!EQ (frame, tooltip_frame)
9548 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9549 x_consider_frame_title (frame);
9550 }
9551 }
9552 #endif /* HAVE_WINDOW_SYSTEM */
9553
9554 /* Update the menu bar item lists, if appropriate. This has to be
9555 done before any actual redisplay or generation of display lines. */
9556 all_windows = (update_mode_lines
9557 || buffer_shared > 1
9558 || windows_or_buffers_changed);
9559 if (all_windows)
9560 {
9561 Lisp_Object tail, frame;
9562 int count = SPECPDL_INDEX ();
9563 /* 1 means that update_menu_bar has run its hooks
9564 so any further calls to update_menu_bar shouldn't do so again. */
9565 int menu_bar_hooks_run = 0;
9566
9567 record_unwind_save_match_data ();
9568
9569 FOR_EACH_FRAME (tail, frame)
9570 {
9571 f = XFRAME (frame);
9572
9573 /* Ignore tooltip frame. */
9574 if (EQ (frame, tooltip_frame))
9575 continue;
9576
9577 /* If a window on this frame changed size, report that to
9578 the user and clear the size-change flag. */
9579 if (FRAME_WINDOW_SIZES_CHANGED (f))
9580 {
9581 Lisp_Object functions;
9582
9583 /* Clear flag first in case we get an error below. */
9584 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9585 functions = Vwindow_size_change_functions;
9586 GCPRO2 (tail, functions);
9587
9588 while (CONSP (functions))
9589 {
9590 if (!EQ (XCAR (functions), Qt))
9591 call1 (XCAR (functions), frame);
9592 functions = XCDR (functions);
9593 }
9594 UNGCPRO;
9595 }
9596
9597 GCPRO1 (tail);
9598 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9599 #ifdef HAVE_WINDOW_SYSTEM
9600 update_tool_bar (f, 0);
9601 #endif
9602 UNGCPRO;
9603 }
9604
9605 unbind_to (count, Qnil);
9606 }
9607 else
9608 {
9609 struct frame *sf = SELECTED_FRAME ();
9610 update_menu_bar (sf, 1, 0);
9611 #ifdef HAVE_WINDOW_SYSTEM
9612 update_tool_bar (sf, 1);
9613 #endif
9614 }
9615
9616 /* Motif needs this. See comment in xmenu.c. Turn it off when
9617 pending_menu_activation is not defined. */
9618 #ifdef USE_X_TOOLKIT
9619 pending_menu_activation = 0;
9620 #endif
9621 }
9622
9623
9624 /* Update the menu bar item list for frame F. This has to be done
9625 before we start to fill in any display lines, because it can call
9626 eval.
9627
9628 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9629
9630 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9631 already ran the menu bar hooks for this redisplay, so there
9632 is no need to run them again. The return value is the
9633 updated value of this flag, to pass to the next call. */
9634
9635 static int
9636 update_menu_bar (f, save_match_data, hooks_run)
9637 struct frame *f;
9638 int save_match_data;
9639 int hooks_run;
9640 {
9641 Lisp_Object window;
9642 register struct window *w;
9643
9644 /* If called recursively during a menu update, do nothing. This can
9645 happen when, for instance, an activate-menubar-hook causes a
9646 redisplay. */
9647 if (inhibit_menubar_update)
9648 return hooks_run;
9649
9650 window = FRAME_SELECTED_WINDOW (f);
9651 w = XWINDOW (window);
9652
9653 if (FRAME_WINDOW_P (f)
9654 ?
9655 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9656 || defined (HAVE_NS) || defined (USE_GTK)
9657 FRAME_EXTERNAL_MENU_BAR (f)
9658 #else
9659 FRAME_MENU_BAR_LINES (f) > 0
9660 #endif
9661 : FRAME_MENU_BAR_LINES (f) > 0)
9662 {
9663 /* If the user has switched buffers or windows, we need to
9664 recompute to reflect the new bindings. But we'll
9665 recompute when update_mode_lines is set too; that means
9666 that people can use force-mode-line-update to request
9667 that the menu bar be recomputed. The adverse effect on
9668 the rest of the redisplay algorithm is about the same as
9669 windows_or_buffers_changed anyway. */
9670 if (windows_or_buffers_changed
9671 /* This used to test w->update_mode_line, but we believe
9672 there is no need to recompute the menu in that case. */
9673 || update_mode_lines
9674 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9675 < BUF_MODIFF (XBUFFER (w->buffer)))
9676 != !NILP (w->last_had_star))
9677 || ((!NILP (Vtransient_mark_mode)
9678 && !NILP (XBUFFER (w->buffer)->mark_active))
9679 != !NILP (w->region_showing)))
9680 {
9681 struct buffer *prev = current_buffer;
9682 int count = SPECPDL_INDEX ();
9683
9684 specbind (Qinhibit_menubar_update, Qt);
9685
9686 set_buffer_internal_1 (XBUFFER (w->buffer));
9687 if (save_match_data)
9688 record_unwind_save_match_data ();
9689 if (NILP (Voverriding_local_map_menu_flag))
9690 {
9691 specbind (Qoverriding_terminal_local_map, Qnil);
9692 specbind (Qoverriding_local_map, Qnil);
9693 }
9694
9695 if (!hooks_run)
9696 {
9697 /* Run the Lucid hook. */
9698 safe_run_hooks (Qactivate_menubar_hook);
9699
9700 /* If it has changed current-menubar from previous value,
9701 really recompute the menu-bar from the value. */
9702 if (! NILP (Vlucid_menu_bar_dirty_flag))
9703 call0 (Qrecompute_lucid_menubar);
9704
9705 safe_run_hooks (Qmenu_bar_update_hook);
9706
9707 hooks_run = 1;
9708 }
9709
9710 XSETFRAME (Vmenu_updating_frame, f);
9711 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9712
9713 /* Redisplay the menu bar in case we changed it. */
9714 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9715 || defined (HAVE_NS) || defined (USE_GTK)
9716 if (FRAME_WINDOW_P (f))
9717 {
9718 #if defined (HAVE_NS)
9719 /* All frames on Mac OS share the same menubar. So only
9720 the selected frame should be allowed to set it. */
9721 if (f == SELECTED_FRAME ())
9722 #endif
9723 set_frame_menubar (f, 0, 0);
9724 }
9725 else
9726 /* On a terminal screen, the menu bar is an ordinary screen
9727 line, and this makes it get updated. */
9728 w->update_mode_line = Qt;
9729 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9730 /* In the non-toolkit version, the menu bar is an ordinary screen
9731 line, and this makes it get updated. */
9732 w->update_mode_line = Qt;
9733 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9734
9735 unbind_to (count, Qnil);
9736 set_buffer_internal_1 (prev);
9737 }
9738 }
9739
9740 return hooks_run;
9741 }
9742
9743
9744 \f
9745 /***********************************************************************
9746 Output Cursor
9747 ***********************************************************************/
9748
9749 #ifdef HAVE_WINDOW_SYSTEM
9750
9751 /* EXPORT:
9752 Nominal cursor position -- where to draw output.
9753 HPOS and VPOS are window relative glyph matrix coordinates.
9754 X and Y are window relative pixel coordinates. */
9755
9756 struct cursor_pos output_cursor;
9757
9758
9759 /* EXPORT:
9760 Set the global variable output_cursor to CURSOR. All cursor
9761 positions are relative to updated_window. */
9762
9763 void
9764 set_output_cursor (cursor)
9765 struct cursor_pos *cursor;
9766 {
9767 output_cursor.hpos = cursor->hpos;
9768 output_cursor.vpos = cursor->vpos;
9769 output_cursor.x = cursor->x;
9770 output_cursor.y = cursor->y;
9771 }
9772
9773
9774 /* EXPORT for RIF:
9775 Set a nominal cursor position.
9776
9777 HPOS and VPOS are column/row positions in a window glyph matrix. X
9778 and Y are window text area relative pixel positions.
9779
9780 If this is done during an update, updated_window will contain the
9781 window that is being updated and the position is the future output
9782 cursor position for that window. If updated_window is null, use
9783 selected_window and display the cursor at the given position. */
9784
9785 void
9786 x_cursor_to (vpos, hpos, y, x)
9787 int vpos, hpos, y, x;
9788 {
9789 struct window *w;
9790
9791 /* If updated_window is not set, work on selected_window. */
9792 if (updated_window)
9793 w = updated_window;
9794 else
9795 w = XWINDOW (selected_window);
9796
9797 /* Set the output cursor. */
9798 output_cursor.hpos = hpos;
9799 output_cursor.vpos = vpos;
9800 output_cursor.x = x;
9801 output_cursor.y = y;
9802
9803 /* If not called as part of an update, really display the cursor.
9804 This will also set the cursor position of W. */
9805 if (updated_window == NULL)
9806 {
9807 BLOCK_INPUT;
9808 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9809 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9810 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9811 UNBLOCK_INPUT;
9812 }
9813 }
9814
9815 #endif /* HAVE_WINDOW_SYSTEM */
9816
9817 \f
9818 /***********************************************************************
9819 Tool-bars
9820 ***********************************************************************/
9821
9822 #ifdef HAVE_WINDOW_SYSTEM
9823
9824 /* Where the mouse was last time we reported a mouse event. */
9825
9826 FRAME_PTR last_mouse_frame;
9827
9828 /* Tool-bar item index of the item on which a mouse button was pressed
9829 or -1. */
9830
9831 int last_tool_bar_item;
9832
9833
9834 static Lisp_Object
9835 update_tool_bar_unwind (frame)
9836 Lisp_Object frame;
9837 {
9838 selected_frame = frame;
9839 return Qnil;
9840 }
9841
9842 /* Update the tool-bar item list for frame F. This has to be done
9843 before we start to fill in any display lines. Called from
9844 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9845 and restore it here. */
9846
9847 static void
9848 update_tool_bar (f, save_match_data)
9849 struct frame *f;
9850 int save_match_data;
9851 {
9852 #if defined (USE_GTK) || defined (HAVE_NS)
9853 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9854 #else
9855 int do_update = WINDOWP (f->tool_bar_window)
9856 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9857 #endif
9858
9859 if (do_update)
9860 {
9861 Lisp_Object window;
9862 struct window *w;
9863
9864 window = FRAME_SELECTED_WINDOW (f);
9865 w = XWINDOW (window);
9866
9867 /* If the user has switched buffers or windows, we need to
9868 recompute to reflect the new bindings. But we'll
9869 recompute when update_mode_lines is set too; that means
9870 that people can use force-mode-line-update to request
9871 that the menu bar be recomputed. The adverse effect on
9872 the rest of the redisplay algorithm is about the same as
9873 windows_or_buffers_changed anyway. */
9874 if (windows_or_buffers_changed
9875 || !NILP (w->update_mode_line)
9876 || update_mode_lines
9877 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9878 < BUF_MODIFF (XBUFFER (w->buffer)))
9879 != !NILP (w->last_had_star))
9880 || ((!NILP (Vtransient_mark_mode)
9881 && !NILP (XBUFFER (w->buffer)->mark_active))
9882 != !NILP (w->region_showing)))
9883 {
9884 struct buffer *prev = current_buffer;
9885 int count = SPECPDL_INDEX ();
9886 Lisp_Object frame, new_tool_bar;
9887 int new_n_tool_bar;
9888 struct gcpro gcpro1;
9889
9890 /* Set current_buffer to the buffer of the selected
9891 window of the frame, so that we get the right local
9892 keymaps. */
9893 set_buffer_internal_1 (XBUFFER (w->buffer));
9894
9895 /* Save match data, if we must. */
9896 if (save_match_data)
9897 record_unwind_save_match_data ();
9898
9899 /* Make sure that we don't accidentally use bogus keymaps. */
9900 if (NILP (Voverriding_local_map_menu_flag))
9901 {
9902 specbind (Qoverriding_terminal_local_map, Qnil);
9903 specbind (Qoverriding_local_map, Qnil);
9904 }
9905
9906 GCPRO1 (new_tool_bar);
9907
9908 /* We must temporarily set the selected frame to this frame
9909 before calling tool_bar_items, because the calculation of
9910 the tool-bar keymap uses the selected frame (see
9911 `tool-bar-make-keymap' in tool-bar.el). */
9912 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9913 XSETFRAME (frame, f);
9914 selected_frame = frame;
9915
9916 /* Build desired tool-bar items from keymaps. */
9917 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9918 &new_n_tool_bar);
9919
9920 /* Redisplay the tool-bar if we changed it. */
9921 if (new_n_tool_bar != f->n_tool_bar_items
9922 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9923 {
9924 /* Redisplay that happens asynchronously due to an expose event
9925 may access f->tool_bar_items. Make sure we update both
9926 variables within BLOCK_INPUT so no such event interrupts. */
9927 BLOCK_INPUT;
9928 f->tool_bar_items = new_tool_bar;
9929 f->n_tool_bar_items = new_n_tool_bar;
9930 w->update_mode_line = Qt;
9931 UNBLOCK_INPUT;
9932 }
9933
9934 UNGCPRO;
9935
9936 unbind_to (count, Qnil);
9937 set_buffer_internal_1 (prev);
9938 }
9939 }
9940 }
9941
9942
9943 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9944 F's desired tool-bar contents. F->tool_bar_items must have
9945 been set up previously by calling prepare_menu_bars. */
9946
9947 static void
9948 build_desired_tool_bar_string (f)
9949 struct frame *f;
9950 {
9951 int i, size, size_needed;
9952 struct gcpro gcpro1, gcpro2, gcpro3;
9953 Lisp_Object image, plist, props;
9954
9955 image = plist = props = Qnil;
9956 GCPRO3 (image, plist, props);
9957
9958 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9959 Otherwise, make a new string. */
9960
9961 /* The size of the string we might be able to reuse. */
9962 size = (STRINGP (f->desired_tool_bar_string)
9963 ? SCHARS (f->desired_tool_bar_string)
9964 : 0);
9965
9966 /* We need one space in the string for each image. */
9967 size_needed = f->n_tool_bar_items;
9968
9969 /* Reuse f->desired_tool_bar_string, if possible. */
9970 if (size < size_needed || NILP (f->desired_tool_bar_string))
9971 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9972 make_number (' '));
9973 else
9974 {
9975 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9976 Fremove_text_properties (make_number (0), make_number (size),
9977 props, f->desired_tool_bar_string);
9978 }
9979
9980 /* Put a `display' property on the string for the images to display,
9981 put a `menu_item' property on tool-bar items with a value that
9982 is the index of the item in F's tool-bar item vector. */
9983 for (i = 0; i < f->n_tool_bar_items; ++i)
9984 {
9985 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9986
9987 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9988 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9989 int hmargin, vmargin, relief, idx, end;
9990 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9991
9992 /* If image is a vector, choose the image according to the
9993 button state. */
9994 image = PROP (TOOL_BAR_ITEM_IMAGES);
9995 if (VECTORP (image))
9996 {
9997 if (enabled_p)
9998 idx = (selected_p
9999 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10000 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10001 else
10002 idx = (selected_p
10003 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10004 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10005
10006 xassert (ASIZE (image) >= idx);
10007 image = AREF (image, idx);
10008 }
10009 else
10010 idx = -1;
10011
10012 /* Ignore invalid image specifications. */
10013 if (!valid_image_p (image))
10014 continue;
10015
10016 /* Display the tool-bar button pressed, or depressed. */
10017 plist = Fcopy_sequence (XCDR (image));
10018
10019 /* Compute margin and relief to draw. */
10020 relief = (tool_bar_button_relief >= 0
10021 ? tool_bar_button_relief
10022 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10023 hmargin = vmargin = relief;
10024
10025 if (INTEGERP (Vtool_bar_button_margin)
10026 && XINT (Vtool_bar_button_margin) > 0)
10027 {
10028 hmargin += XFASTINT (Vtool_bar_button_margin);
10029 vmargin += XFASTINT (Vtool_bar_button_margin);
10030 }
10031 else if (CONSP (Vtool_bar_button_margin))
10032 {
10033 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10034 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10035 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10036
10037 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10038 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10039 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10040 }
10041
10042 if (auto_raise_tool_bar_buttons_p)
10043 {
10044 /* Add a `:relief' property to the image spec if the item is
10045 selected. */
10046 if (selected_p)
10047 {
10048 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10049 hmargin -= relief;
10050 vmargin -= relief;
10051 }
10052 }
10053 else
10054 {
10055 /* If image is selected, display it pressed, i.e. with a
10056 negative relief. If it's not selected, display it with a
10057 raised relief. */
10058 plist = Fplist_put (plist, QCrelief,
10059 (selected_p
10060 ? make_number (-relief)
10061 : make_number (relief)));
10062 hmargin -= relief;
10063 vmargin -= relief;
10064 }
10065
10066 /* Put a margin around the image. */
10067 if (hmargin || vmargin)
10068 {
10069 if (hmargin == vmargin)
10070 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10071 else
10072 plist = Fplist_put (plist, QCmargin,
10073 Fcons (make_number (hmargin),
10074 make_number (vmargin)));
10075 }
10076
10077 /* If button is not enabled, and we don't have special images
10078 for the disabled state, make the image appear disabled by
10079 applying an appropriate algorithm to it. */
10080 if (!enabled_p && idx < 0)
10081 plist = Fplist_put (plist, QCconversion, Qdisabled);
10082
10083 /* Put a `display' text property on the string for the image to
10084 display. Put a `menu-item' property on the string that gives
10085 the start of this item's properties in the tool-bar items
10086 vector. */
10087 image = Fcons (Qimage, plist);
10088 props = list4 (Qdisplay, image,
10089 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10090
10091 /* Let the last image hide all remaining spaces in the tool bar
10092 string. The string can be longer than needed when we reuse a
10093 previous string. */
10094 if (i + 1 == f->n_tool_bar_items)
10095 end = SCHARS (f->desired_tool_bar_string);
10096 else
10097 end = i + 1;
10098 Fadd_text_properties (make_number (i), make_number (end),
10099 props, f->desired_tool_bar_string);
10100 #undef PROP
10101 }
10102
10103 UNGCPRO;
10104 }
10105
10106
10107 /* Display one line of the tool-bar of frame IT->f.
10108
10109 HEIGHT specifies the desired height of the tool-bar line.
10110 If the actual height of the glyph row is less than HEIGHT, the
10111 row's height is increased to HEIGHT, and the icons are centered
10112 vertically in the new height.
10113
10114 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10115 count a final empty row in case the tool-bar width exactly matches
10116 the window width.
10117 */
10118
10119 static void
10120 display_tool_bar_line (it, height)
10121 struct it *it;
10122 int height;
10123 {
10124 struct glyph_row *row = it->glyph_row;
10125 int max_x = it->last_visible_x;
10126 struct glyph *last;
10127
10128 prepare_desired_row (row);
10129 row->y = it->current_y;
10130
10131 /* Note that this isn't made use of if the face hasn't a box,
10132 so there's no need to check the face here. */
10133 it->start_of_box_run_p = 1;
10134
10135 while (it->current_x < max_x)
10136 {
10137 int x, n_glyphs_before, i, nglyphs;
10138 struct it it_before;
10139
10140 /* Get the next display element. */
10141 if (!get_next_display_element (it))
10142 {
10143 /* Don't count empty row if we are counting needed tool-bar lines. */
10144 if (height < 0 && !it->hpos)
10145 return;
10146 break;
10147 }
10148
10149 /* Produce glyphs. */
10150 n_glyphs_before = row->used[TEXT_AREA];
10151 it_before = *it;
10152
10153 PRODUCE_GLYPHS (it);
10154
10155 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10156 i = 0;
10157 x = it_before.current_x;
10158 while (i < nglyphs)
10159 {
10160 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10161
10162 if (x + glyph->pixel_width > max_x)
10163 {
10164 /* Glyph doesn't fit on line. Backtrack. */
10165 row->used[TEXT_AREA] = n_glyphs_before;
10166 *it = it_before;
10167 /* If this is the only glyph on this line, it will never fit on the
10168 toolbar, so skip it. But ensure there is at least one glyph,
10169 so we don't accidentally disable the tool-bar. */
10170 if (n_glyphs_before == 0
10171 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10172 break;
10173 goto out;
10174 }
10175
10176 ++it->hpos;
10177 x += glyph->pixel_width;
10178 ++i;
10179 }
10180
10181 /* Stop at line ends. */
10182 if (ITERATOR_AT_END_OF_LINE_P (it))
10183 break;
10184
10185 set_iterator_to_next (it, 1);
10186 }
10187
10188 out:;
10189
10190 row->displays_text_p = row->used[TEXT_AREA] != 0;
10191
10192 /* Use default face for the border below the tool bar.
10193
10194 FIXME: When auto-resize-tool-bars is grow-only, there is
10195 no additional border below the possibly empty tool-bar lines.
10196 So to make the extra empty lines look "normal", we have to
10197 use the tool-bar face for the border too. */
10198 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10199 it->face_id = DEFAULT_FACE_ID;
10200
10201 extend_face_to_end_of_line (it);
10202 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10203 last->right_box_line_p = 1;
10204 if (last == row->glyphs[TEXT_AREA])
10205 last->left_box_line_p = 1;
10206
10207 /* Make line the desired height and center it vertically. */
10208 if ((height -= it->max_ascent + it->max_descent) > 0)
10209 {
10210 /* Don't add more than one line height. */
10211 height %= FRAME_LINE_HEIGHT (it->f);
10212 it->max_ascent += height / 2;
10213 it->max_descent += (height + 1) / 2;
10214 }
10215
10216 compute_line_metrics (it);
10217
10218 /* If line is empty, make it occupy the rest of the tool-bar. */
10219 if (!row->displays_text_p)
10220 {
10221 row->height = row->phys_height = it->last_visible_y - row->y;
10222 row->visible_height = row->height;
10223 row->ascent = row->phys_ascent = 0;
10224 row->extra_line_spacing = 0;
10225 }
10226
10227 row->full_width_p = 1;
10228 row->continued_p = 0;
10229 row->truncated_on_left_p = 0;
10230 row->truncated_on_right_p = 0;
10231
10232 it->current_x = it->hpos = 0;
10233 it->current_y += row->height;
10234 ++it->vpos;
10235 ++it->glyph_row;
10236 }
10237
10238
10239 /* Max tool-bar height. */
10240
10241 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10242 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10243
10244 /* Value is the number of screen lines needed to make all tool-bar
10245 items of frame F visible. The number of actual rows needed is
10246 returned in *N_ROWS if non-NULL. */
10247
10248 static int
10249 tool_bar_lines_needed (f, n_rows)
10250 struct frame *f;
10251 int *n_rows;
10252 {
10253 struct window *w = XWINDOW (f->tool_bar_window);
10254 struct it it;
10255 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10256 the desired matrix, so use (unused) mode-line row as temporary row to
10257 avoid destroying the first tool-bar row. */
10258 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10259
10260 /* Initialize an iterator for iteration over
10261 F->desired_tool_bar_string in the tool-bar window of frame F. */
10262 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10263 it.first_visible_x = 0;
10264 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10265 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10266
10267 while (!ITERATOR_AT_END_P (&it))
10268 {
10269 clear_glyph_row (temp_row);
10270 it.glyph_row = temp_row;
10271 display_tool_bar_line (&it, -1);
10272 }
10273 clear_glyph_row (temp_row);
10274
10275 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10276 if (n_rows)
10277 *n_rows = it.vpos > 0 ? it.vpos : -1;
10278
10279 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10280 }
10281
10282
10283 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10284 0, 1, 0,
10285 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10286 (frame)
10287 Lisp_Object frame;
10288 {
10289 struct frame *f;
10290 struct window *w;
10291 int nlines = 0;
10292
10293 if (NILP (frame))
10294 frame = selected_frame;
10295 else
10296 CHECK_FRAME (frame);
10297 f = XFRAME (frame);
10298
10299 if (WINDOWP (f->tool_bar_window)
10300 || (w = XWINDOW (f->tool_bar_window),
10301 WINDOW_TOTAL_LINES (w) > 0))
10302 {
10303 update_tool_bar (f, 1);
10304 if (f->n_tool_bar_items)
10305 {
10306 build_desired_tool_bar_string (f);
10307 nlines = tool_bar_lines_needed (f, NULL);
10308 }
10309 }
10310
10311 return make_number (nlines);
10312 }
10313
10314
10315 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10316 height should be changed. */
10317
10318 static int
10319 redisplay_tool_bar (f)
10320 struct frame *f;
10321 {
10322 struct window *w;
10323 struct it it;
10324 struct glyph_row *row;
10325
10326 #if defined (USE_GTK) || defined (HAVE_NS)
10327 if (FRAME_EXTERNAL_TOOL_BAR (f))
10328 update_frame_tool_bar (f);
10329 return 0;
10330 #endif
10331
10332 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10333 do anything. This means you must start with tool-bar-lines
10334 non-zero to get the auto-sizing effect. Or in other words, you
10335 can turn off tool-bars by specifying tool-bar-lines zero. */
10336 if (!WINDOWP (f->tool_bar_window)
10337 || (w = XWINDOW (f->tool_bar_window),
10338 WINDOW_TOTAL_LINES (w) == 0))
10339 return 0;
10340
10341 /* Set up an iterator for the tool-bar window. */
10342 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10343 it.first_visible_x = 0;
10344 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10345 row = it.glyph_row;
10346
10347 /* Build a string that represents the contents of the tool-bar. */
10348 build_desired_tool_bar_string (f);
10349 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10350
10351 if (f->n_tool_bar_rows == 0)
10352 {
10353 int nlines;
10354
10355 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10356 nlines != WINDOW_TOTAL_LINES (w)))
10357 {
10358 extern Lisp_Object Qtool_bar_lines;
10359 Lisp_Object frame;
10360 int old_height = WINDOW_TOTAL_LINES (w);
10361
10362 XSETFRAME (frame, f);
10363 Fmodify_frame_parameters (frame,
10364 Fcons (Fcons (Qtool_bar_lines,
10365 make_number (nlines)),
10366 Qnil));
10367 if (WINDOW_TOTAL_LINES (w) != old_height)
10368 {
10369 clear_glyph_matrix (w->desired_matrix);
10370 fonts_changed_p = 1;
10371 return 1;
10372 }
10373 }
10374 }
10375
10376 /* Display as many lines as needed to display all tool-bar items. */
10377
10378 if (f->n_tool_bar_rows > 0)
10379 {
10380 int border, rows, height, extra;
10381
10382 if (INTEGERP (Vtool_bar_border))
10383 border = XINT (Vtool_bar_border);
10384 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10385 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10386 else if (EQ (Vtool_bar_border, Qborder_width))
10387 border = f->border_width;
10388 else
10389 border = 0;
10390 if (border < 0)
10391 border = 0;
10392
10393 rows = f->n_tool_bar_rows;
10394 height = max (1, (it.last_visible_y - border) / rows);
10395 extra = it.last_visible_y - border - height * rows;
10396
10397 while (it.current_y < it.last_visible_y)
10398 {
10399 int h = 0;
10400 if (extra > 0 && rows-- > 0)
10401 {
10402 h = (extra + rows - 1) / rows;
10403 extra -= h;
10404 }
10405 display_tool_bar_line (&it, height + h);
10406 }
10407 }
10408 else
10409 {
10410 while (it.current_y < it.last_visible_y)
10411 display_tool_bar_line (&it, 0);
10412 }
10413
10414 /* It doesn't make much sense to try scrolling in the tool-bar
10415 window, so don't do it. */
10416 w->desired_matrix->no_scrolling_p = 1;
10417 w->must_be_updated_p = 1;
10418
10419 if (!NILP (Vauto_resize_tool_bars))
10420 {
10421 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10422 int change_height_p = 0;
10423
10424 /* If we couldn't display everything, change the tool-bar's
10425 height if there is room for more. */
10426 if (IT_STRING_CHARPOS (it) < it.end_charpos
10427 && it.current_y < max_tool_bar_height)
10428 change_height_p = 1;
10429
10430 row = it.glyph_row - 1;
10431
10432 /* If there are blank lines at the end, except for a partially
10433 visible blank line at the end that is smaller than
10434 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10435 if (!row->displays_text_p
10436 && row->height >= FRAME_LINE_HEIGHT (f))
10437 change_height_p = 1;
10438
10439 /* If row displays tool-bar items, but is partially visible,
10440 change the tool-bar's height. */
10441 if (row->displays_text_p
10442 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10443 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10444 change_height_p = 1;
10445
10446 /* Resize windows as needed by changing the `tool-bar-lines'
10447 frame parameter. */
10448 if (change_height_p)
10449 {
10450 extern Lisp_Object Qtool_bar_lines;
10451 Lisp_Object frame;
10452 int old_height = WINDOW_TOTAL_LINES (w);
10453 int nrows;
10454 int nlines = tool_bar_lines_needed (f, &nrows);
10455
10456 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10457 && !f->minimize_tool_bar_window_p)
10458 ? (nlines > old_height)
10459 : (nlines != old_height));
10460 f->minimize_tool_bar_window_p = 0;
10461
10462 if (change_height_p)
10463 {
10464 XSETFRAME (frame, f);
10465 Fmodify_frame_parameters (frame,
10466 Fcons (Fcons (Qtool_bar_lines,
10467 make_number (nlines)),
10468 Qnil));
10469 if (WINDOW_TOTAL_LINES (w) != old_height)
10470 {
10471 clear_glyph_matrix (w->desired_matrix);
10472 f->n_tool_bar_rows = nrows;
10473 fonts_changed_p = 1;
10474 return 1;
10475 }
10476 }
10477 }
10478 }
10479
10480 f->minimize_tool_bar_window_p = 0;
10481 return 0;
10482 }
10483
10484
10485 /* Get information about the tool-bar item which is displayed in GLYPH
10486 on frame F. Return in *PROP_IDX the index where tool-bar item
10487 properties start in F->tool_bar_items. Value is zero if
10488 GLYPH doesn't display a tool-bar item. */
10489
10490 static int
10491 tool_bar_item_info (f, glyph, prop_idx)
10492 struct frame *f;
10493 struct glyph *glyph;
10494 int *prop_idx;
10495 {
10496 Lisp_Object prop;
10497 int success_p;
10498 int charpos;
10499
10500 /* This function can be called asynchronously, which means we must
10501 exclude any possibility that Fget_text_property signals an
10502 error. */
10503 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10504 charpos = max (0, charpos);
10505
10506 /* Get the text property `menu-item' at pos. The value of that
10507 property is the start index of this item's properties in
10508 F->tool_bar_items. */
10509 prop = Fget_text_property (make_number (charpos),
10510 Qmenu_item, f->current_tool_bar_string);
10511 if (INTEGERP (prop))
10512 {
10513 *prop_idx = XINT (prop);
10514 success_p = 1;
10515 }
10516 else
10517 success_p = 0;
10518
10519 return success_p;
10520 }
10521
10522 \f
10523 /* Get information about the tool-bar item at position X/Y on frame F.
10524 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10525 the current matrix of the tool-bar window of F, or NULL if not
10526 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10527 item in F->tool_bar_items. Value is
10528
10529 -1 if X/Y is not on a tool-bar item
10530 0 if X/Y is on the same item that was highlighted before.
10531 1 otherwise. */
10532
10533 static int
10534 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10535 struct frame *f;
10536 int x, y;
10537 struct glyph **glyph;
10538 int *hpos, *vpos, *prop_idx;
10539 {
10540 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10541 struct window *w = XWINDOW (f->tool_bar_window);
10542 int area;
10543
10544 /* Find the glyph under X/Y. */
10545 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10546 if (*glyph == NULL)
10547 return -1;
10548
10549 /* Get the start of this tool-bar item's properties in
10550 f->tool_bar_items. */
10551 if (!tool_bar_item_info (f, *glyph, prop_idx))
10552 return -1;
10553
10554 /* Is mouse on the highlighted item? */
10555 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10556 && *vpos >= dpyinfo->mouse_face_beg_row
10557 && *vpos <= dpyinfo->mouse_face_end_row
10558 && (*vpos > dpyinfo->mouse_face_beg_row
10559 || *hpos >= dpyinfo->mouse_face_beg_col)
10560 && (*vpos < dpyinfo->mouse_face_end_row
10561 || *hpos < dpyinfo->mouse_face_end_col
10562 || dpyinfo->mouse_face_past_end))
10563 return 0;
10564
10565 return 1;
10566 }
10567
10568
10569 /* EXPORT:
10570 Handle mouse button event on the tool-bar of frame F, at
10571 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10572 0 for button release. MODIFIERS is event modifiers for button
10573 release. */
10574
10575 void
10576 handle_tool_bar_click (f, x, y, down_p, modifiers)
10577 struct frame *f;
10578 int x, y, down_p;
10579 unsigned int modifiers;
10580 {
10581 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10582 struct window *w = XWINDOW (f->tool_bar_window);
10583 int hpos, vpos, prop_idx;
10584 struct glyph *glyph;
10585 Lisp_Object enabled_p;
10586
10587 /* If not on the highlighted tool-bar item, return. */
10588 frame_to_window_pixel_xy (w, &x, &y);
10589 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10590 return;
10591
10592 /* If item is disabled, do nothing. */
10593 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10594 if (NILP (enabled_p))
10595 return;
10596
10597 if (down_p)
10598 {
10599 /* Show item in pressed state. */
10600 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10601 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10602 last_tool_bar_item = prop_idx;
10603 }
10604 else
10605 {
10606 Lisp_Object key, frame;
10607 struct input_event event;
10608 EVENT_INIT (event);
10609
10610 /* Show item in released state. */
10611 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10612 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10613
10614 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10615
10616 XSETFRAME (frame, f);
10617 event.kind = TOOL_BAR_EVENT;
10618 event.frame_or_window = frame;
10619 event.arg = frame;
10620 kbd_buffer_store_event (&event);
10621
10622 event.kind = TOOL_BAR_EVENT;
10623 event.frame_or_window = frame;
10624 event.arg = key;
10625 event.modifiers = modifiers;
10626 kbd_buffer_store_event (&event);
10627 last_tool_bar_item = -1;
10628 }
10629 }
10630
10631
10632 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10633 tool-bar window-relative coordinates X/Y. Called from
10634 note_mouse_highlight. */
10635
10636 static void
10637 note_tool_bar_highlight (f, x, y)
10638 struct frame *f;
10639 int x, y;
10640 {
10641 Lisp_Object window = f->tool_bar_window;
10642 struct window *w = XWINDOW (window);
10643 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10644 int hpos, vpos;
10645 struct glyph *glyph;
10646 struct glyph_row *row;
10647 int i;
10648 Lisp_Object enabled_p;
10649 int prop_idx;
10650 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10651 int mouse_down_p, rc;
10652
10653 /* Function note_mouse_highlight is called with negative x(y
10654 values when mouse moves outside of the frame. */
10655 if (x <= 0 || y <= 0)
10656 {
10657 clear_mouse_face (dpyinfo);
10658 return;
10659 }
10660
10661 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10662 if (rc < 0)
10663 {
10664 /* Not on tool-bar item. */
10665 clear_mouse_face (dpyinfo);
10666 return;
10667 }
10668 else if (rc == 0)
10669 /* On same tool-bar item as before. */
10670 goto set_help_echo;
10671
10672 clear_mouse_face (dpyinfo);
10673
10674 /* Mouse is down, but on different tool-bar item? */
10675 mouse_down_p = (dpyinfo->grabbed
10676 && f == last_mouse_frame
10677 && FRAME_LIVE_P (f));
10678 if (mouse_down_p
10679 && last_tool_bar_item != prop_idx)
10680 return;
10681
10682 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10683 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10684
10685 /* If tool-bar item is not enabled, don't highlight it. */
10686 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10687 if (!NILP (enabled_p))
10688 {
10689 /* Compute the x-position of the glyph. In front and past the
10690 image is a space. We include this in the highlighted area. */
10691 row = MATRIX_ROW (w->current_matrix, vpos);
10692 for (i = x = 0; i < hpos; ++i)
10693 x += row->glyphs[TEXT_AREA][i].pixel_width;
10694
10695 /* Record this as the current active region. */
10696 dpyinfo->mouse_face_beg_col = hpos;
10697 dpyinfo->mouse_face_beg_row = vpos;
10698 dpyinfo->mouse_face_beg_x = x;
10699 dpyinfo->mouse_face_beg_y = row->y;
10700 dpyinfo->mouse_face_past_end = 0;
10701
10702 dpyinfo->mouse_face_end_col = hpos + 1;
10703 dpyinfo->mouse_face_end_row = vpos;
10704 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10705 dpyinfo->mouse_face_end_y = row->y;
10706 dpyinfo->mouse_face_window = window;
10707 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10708
10709 /* Display it as active. */
10710 show_mouse_face (dpyinfo, draw);
10711 dpyinfo->mouse_face_image_state = draw;
10712 }
10713
10714 set_help_echo:
10715
10716 /* Set help_echo_string to a help string to display for this tool-bar item.
10717 XTread_socket does the rest. */
10718 help_echo_object = help_echo_window = Qnil;
10719 help_echo_pos = -1;
10720 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10721 if (NILP (help_echo_string))
10722 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10723 }
10724
10725 #endif /* HAVE_WINDOW_SYSTEM */
10726
10727
10728 \f
10729 /************************************************************************
10730 Horizontal scrolling
10731 ************************************************************************/
10732
10733 static int hscroll_window_tree P_ ((Lisp_Object));
10734 static int hscroll_windows P_ ((Lisp_Object));
10735
10736 /* For all leaf windows in the window tree rooted at WINDOW, set their
10737 hscroll value so that PT is (i) visible in the window, and (ii) so
10738 that it is not within a certain margin at the window's left and
10739 right border. Value is non-zero if any window's hscroll has been
10740 changed. */
10741
10742 static int
10743 hscroll_window_tree (window)
10744 Lisp_Object window;
10745 {
10746 int hscrolled_p = 0;
10747 int hscroll_relative_p = FLOATP (Vhscroll_step);
10748 int hscroll_step_abs = 0;
10749 double hscroll_step_rel = 0;
10750
10751 if (hscroll_relative_p)
10752 {
10753 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10754 if (hscroll_step_rel < 0)
10755 {
10756 hscroll_relative_p = 0;
10757 hscroll_step_abs = 0;
10758 }
10759 }
10760 else if (INTEGERP (Vhscroll_step))
10761 {
10762 hscroll_step_abs = XINT (Vhscroll_step);
10763 if (hscroll_step_abs < 0)
10764 hscroll_step_abs = 0;
10765 }
10766 else
10767 hscroll_step_abs = 0;
10768
10769 while (WINDOWP (window))
10770 {
10771 struct window *w = XWINDOW (window);
10772
10773 if (WINDOWP (w->hchild))
10774 hscrolled_p |= hscroll_window_tree (w->hchild);
10775 else if (WINDOWP (w->vchild))
10776 hscrolled_p |= hscroll_window_tree (w->vchild);
10777 else if (w->cursor.vpos >= 0)
10778 {
10779 int h_margin;
10780 int text_area_width;
10781 struct glyph_row *current_cursor_row
10782 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10783 struct glyph_row *desired_cursor_row
10784 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10785 struct glyph_row *cursor_row
10786 = (desired_cursor_row->enabled_p
10787 ? desired_cursor_row
10788 : current_cursor_row);
10789
10790 text_area_width = window_box_width (w, TEXT_AREA);
10791
10792 /* Scroll when cursor is inside this scroll margin. */
10793 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10794
10795 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10796 && ((XFASTINT (w->hscroll)
10797 && w->cursor.x <= h_margin)
10798 || (cursor_row->enabled_p
10799 && cursor_row->truncated_on_right_p
10800 && (w->cursor.x >= text_area_width - h_margin))))
10801 {
10802 struct it it;
10803 int hscroll;
10804 struct buffer *saved_current_buffer;
10805 int pt;
10806 int wanted_x;
10807
10808 /* Find point in a display of infinite width. */
10809 saved_current_buffer = current_buffer;
10810 current_buffer = XBUFFER (w->buffer);
10811
10812 if (w == XWINDOW (selected_window))
10813 pt = BUF_PT (current_buffer);
10814 else
10815 {
10816 pt = marker_position (w->pointm);
10817 pt = max (BEGV, pt);
10818 pt = min (ZV, pt);
10819 }
10820
10821 /* Move iterator to pt starting at cursor_row->start in
10822 a line with infinite width. */
10823 init_to_row_start (&it, w, cursor_row);
10824 it.last_visible_x = INFINITY;
10825 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10826 current_buffer = saved_current_buffer;
10827
10828 /* Position cursor in window. */
10829 if (!hscroll_relative_p && hscroll_step_abs == 0)
10830 hscroll = max (0, (it.current_x
10831 - (ITERATOR_AT_END_OF_LINE_P (&it)
10832 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10833 : (text_area_width / 2))))
10834 / FRAME_COLUMN_WIDTH (it.f);
10835 else if (w->cursor.x >= text_area_width - h_margin)
10836 {
10837 if (hscroll_relative_p)
10838 wanted_x = text_area_width * (1 - hscroll_step_rel)
10839 - h_margin;
10840 else
10841 wanted_x = text_area_width
10842 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10843 - h_margin;
10844 hscroll
10845 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10846 }
10847 else
10848 {
10849 if (hscroll_relative_p)
10850 wanted_x = text_area_width * hscroll_step_rel
10851 + h_margin;
10852 else
10853 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10854 + h_margin;
10855 hscroll
10856 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10857 }
10858 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10859
10860 /* Don't call Fset_window_hscroll if value hasn't
10861 changed because it will prevent redisplay
10862 optimizations. */
10863 if (XFASTINT (w->hscroll) != hscroll)
10864 {
10865 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10866 w->hscroll = make_number (hscroll);
10867 hscrolled_p = 1;
10868 }
10869 }
10870 }
10871
10872 window = w->next;
10873 }
10874
10875 /* Value is non-zero if hscroll of any leaf window has been changed. */
10876 return hscrolled_p;
10877 }
10878
10879
10880 /* Set hscroll so that cursor is visible and not inside horizontal
10881 scroll margins for all windows in the tree rooted at WINDOW. See
10882 also hscroll_window_tree above. Value is non-zero if any window's
10883 hscroll has been changed. If it has, desired matrices on the frame
10884 of WINDOW are cleared. */
10885
10886 static int
10887 hscroll_windows (window)
10888 Lisp_Object window;
10889 {
10890 int hscrolled_p = hscroll_window_tree (window);
10891 if (hscrolled_p)
10892 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10893 return hscrolled_p;
10894 }
10895
10896
10897 \f
10898 /************************************************************************
10899 Redisplay
10900 ************************************************************************/
10901
10902 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10903 to a non-zero value. This is sometimes handy to have in a debugger
10904 session. */
10905
10906 #if GLYPH_DEBUG
10907
10908 /* First and last unchanged row for try_window_id. */
10909
10910 int debug_first_unchanged_at_end_vpos;
10911 int debug_last_unchanged_at_beg_vpos;
10912
10913 /* Delta vpos and y. */
10914
10915 int debug_dvpos, debug_dy;
10916
10917 /* Delta in characters and bytes for try_window_id. */
10918
10919 int debug_delta, debug_delta_bytes;
10920
10921 /* Values of window_end_pos and window_end_vpos at the end of
10922 try_window_id. */
10923
10924 EMACS_INT debug_end_pos, debug_end_vpos;
10925
10926 /* Append a string to W->desired_matrix->method. FMT is a printf
10927 format string. A1...A9 are a supplement for a variable-length
10928 argument list. If trace_redisplay_p is non-zero also printf the
10929 resulting string to stderr. */
10930
10931 static void
10932 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10933 struct window *w;
10934 char *fmt;
10935 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10936 {
10937 char buffer[512];
10938 char *method = w->desired_matrix->method;
10939 int len = strlen (method);
10940 int size = sizeof w->desired_matrix->method;
10941 int remaining = size - len - 1;
10942
10943 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10944 if (len && remaining)
10945 {
10946 method[len] = '|';
10947 --remaining, ++len;
10948 }
10949
10950 strncpy (method + len, buffer, remaining);
10951
10952 if (trace_redisplay_p)
10953 fprintf (stderr, "%p (%s): %s\n",
10954 w,
10955 ((BUFFERP (w->buffer)
10956 && STRINGP (XBUFFER (w->buffer)->name))
10957 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10958 : "no buffer"),
10959 buffer);
10960 }
10961
10962 #endif /* GLYPH_DEBUG */
10963
10964
10965 /* Value is non-zero if all changes in window W, which displays
10966 current_buffer, are in the text between START and END. START is a
10967 buffer position, END is given as a distance from Z. Used in
10968 redisplay_internal for display optimization. */
10969
10970 static INLINE int
10971 text_outside_line_unchanged_p (w, start, end)
10972 struct window *w;
10973 int start, end;
10974 {
10975 int unchanged_p = 1;
10976
10977 /* If text or overlays have changed, see where. */
10978 if (XFASTINT (w->last_modified) < MODIFF
10979 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10980 {
10981 /* Gap in the line? */
10982 if (GPT < start || Z - GPT < end)
10983 unchanged_p = 0;
10984
10985 /* Changes start in front of the line, or end after it? */
10986 if (unchanged_p
10987 && (BEG_UNCHANGED < start - 1
10988 || END_UNCHANGED < end))
10989 unchanged_p = 0;
10990
10991 /* If selective display, can't optimize if changes start at the
10992 beginning of the line. */
10993 if (unchanged_p
10994 && INTEGERP (current_buffer->selective_display)
10995 && XINT (current_buffer->selective_display) > 0
10996 && (BEG_UNCHANGED < start || GPT <= start))
10997 unchanged_p = 0;
10998
10999 /* If there are overlays at the start or end of the line, these
11000 may have overlay strings with newlines in them. A change at
11001 START, for instance, may actually concern the display of such
11002 overlay strings as well, and they are displayed on different
11003 lines. So, quickly rule out this case. (For the future, it
11004 might be desirable to implement something more telling than
11005 just BEG/END_UNCHANGED.) */
11006 if (unchanged_p)
11007 {
11008 if (BEG + BEG_UNCHANGED == start
11009 && overlay_touches_p (start))
11010 unchanged_p = 0;
11011 if (END_UNCHANGED == end
11012 && overlay_touches_p (Z - end))
11013 unchanged_p = 0;
11014 }
11015 }
11016
11017 return unchanged_p;
11018 }
11019
11020
11021 /* Do a frame update, taking possible shortcuts into account. This is
11022 the main external entry point for redisplay.
11023
11024 If the last redisplay displayed an echo area message and that message
11025 is no longer requested, we clear the echo area or bring back the
11026 mini-buffer if that is in use. */
11027
11028 void
11029 redisplay ()
11030 {
11031 redisplay_internal (0);
11032 }
11033
11034
11035 static Lisp_Object
11036 overlay_arrow_string_or_property (var)
11037 Lisp_Object var;
11038 {
11039 Lisp_Object val;
11040
11041 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11042 return val;
11043
11044 return Voverlay_arrow_string;
11045 }
11046
11047 /* Return 1 if there are any overlay-arrows in current_buffer. */
11048 static int
11049 overlay_arrow_in_current_buffer_p ()
11050 {
11051 Lisp_Object vlist;
11052
11053 for (vlist = Voverlay_arrow_variable_list;
11054 CONSP (vlist);
11055 vlist = XCDR (vlist))
11056 {
11057 Lisp_Object var = XCAR (vlist);
11058 Lisp_Object val;
11059
11060 if (!SYMBOLP (var))
11061 continue;
11062 val = find_symbol_value (var);
11063 if (MARKERP (val)
11064 && current_buffer == XMARKER (val)->buffer)
11065 return 1;
11066 }
11067 return 0;
11068 }
11069
11070
11071 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11072 has changed. */
11073
11074 static int
11075 overlay_arrows_changed_p ()
11076 {
11077 Lisp_Object vlist;
11078
11079 for (vlist = Voverlay_arrow_variable_list;
11080 CONSP (vlist);
11081 vlist = XCDR (vlist))
11082 {
11083 Lisp_Object var = XCAR (vlist);
11084 Lisp_Object val, pstr;
11085
11086 if (!SYMBOLP (var))
11087 continue;
11088 val = find_symbol_value (var);
11089 if (!MARKERP (val))
11090 continue;
11091 if (! EQ (COERCE_MARKER (val),
11092 Fget (var, Qlast_arrow_position))
11093 || ! (pstr = overlay_arrow_string_or_property (var),
11094 EQ (pstr, Fget (var, Qlast_arrow_string))))
11095 return 1;
11096 }
11097 return 0;
11098 }
11099
11100 /* Mark overlay arrows to be updated on next redisplay. */
11101
11102 static void
11103 update_overlay_arrows (up_to_date)
11104 int up_to_date;
11105 {
11106 Lisp_Object vlist;
11107
11108 for (vlist = Voverlay_arrow_variable_list;
11109 CONSP (vlist);
11110 vlist = XCDR (vlist))
11111 {
11112 Lisp_Object var = XCAR (vlist);
11113
11114 if (!SYMBOLP (var))
11115 continue;
11116
11117 if (up_to_date > 0)
11118 {
11119 Lisp_Object val = find_symbol_value (var);
11120 Fput (var, Qlast_arrow_position,
11121 COERCE_MARKER (val));
11122 Fput (var, Qlast_arrow_string,
11123 overlay_arrow_string_or_property (var));
11124 }
11125 else if (up_to_date < 0
11126 || !NILP (Fget (var, Qlast_arrow_position)))
11127 {
11128 Fput (var, Qlast_arrow_position, Qt);
11129 Fput (var, Qlast_arrow_string, Qt);
11130 }
11131 }
11132 }
11133
11134
11135 /* Return overlay arrow string to display at row.
11136 Return integer (bitmap number) for arrow bitmap in left fringe.
11137 Return nil if no overlay arrow. */
11138
11139 static Lisp_Object
11140 overlay_arrow_at_row (it, row)
11141 struct it *it;
11142 struct glyph_row *row;
11143 {
11144 Lisp_Object vlist;
11145
11146 for (vlist = Voverlay_arrow_variable_list;
11147 CONSP (vlist);
11148 vlist = XCDR (vlist))
11149 {
11150 Lisp_Object var = XCAR (vlist);
11151 Lisp_Object val;
11152
11153 if (!SYMBOLP (var))
11154 continue;
11155
11156 val = find_symbol_value (var);
11157
11158 if (MARKERP (val)
11159 && current_buffer == XMARKER (val)->buffer
11160 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11161 {
11162 if (FRAME_WINDOW_P (it->f)
11163 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11164 {
11165 #ifdef HAVE_WINDOW_SYSTEM
11166 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11167 {
11168 int fringe_bitmap;
11169 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11170 return make_number (fringe_bitmap);
11171 }
11172 #endif
11173 return make_number (-1); /* Use default arrow bitmap */
11174 }
11175 return overlay_arrow_string_or_property (var);
11176 }
11177 }
11178
11179 return Qnil;
11180 }
11181
11182 /* Return 1 if point moved out of or into a composition. Otherwise
11183 return 0. PREV_BUF and PREV_PT are the last point buffer and
11184 position. BUF and PT are the current point buffer and position. */
11185
11186 int
11187 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11188 struct buffer *prev_buf, *buf;
11189 int prev_pt, pt;
11190 {
11191 EMACS_INT start, end;
11192 Lisp_Object prop;
11193 Lisp_Object buffer;
11194
11195 XSETBUFFER (buffer, buf);
11196 /* Check a composition at the last point if point moved within the
11197 same buffer. */
11198 if (prev_buf == buf)
11199 {
11200 if (prev_pt == pt)
11201 /* Point didn't move. */
11202 return 0;
11203
11204 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11205 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11206 && COMPOSITION_VALID_P (start, end, prop)
11207 && start < prev_pt && end > prev_pt)
11208 /* The last point was within the composition. Return 1 iff
11209 point moved out of the composition. */
11210 return (pt <= start || pt >= end);
11211 }
11212
11213 /* Check a composition at the current point. */
11214 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11215 && find_composition (pt, -1, &start, &end, &prop, buffer)
11216 && COMPOSITION_VALID_P (start, end, prop)
11217 && start < pt && end > pt);
11218 }
11219
11220
11221 /* Reconsider the setting of B->clip_changed which is displayed
11222 in window W. */
11223
11224 static INLINE void
11225 reconsider_clip_changes (w, b)
11226 struct window *w;
11227 struct buffer *b;
11228 {
11229 if (b->clip_changed
11230 && !NILP (w->window_end_valid)
11231 && w->current_matrix->buffer == b
11232 && w->current_matrix->zv == BUF_ZV (b)
11233 && w->current_matrix->begv == BUF_BEGV (b))
11234 b->clip_changed = 0;
11235
11236 /* If display wasn't paused, and W is not a tool bar window, see if
11237 point has been moved into or out of a composition. In that case,
11238 we set b->clip_changed to 1 to force updating the screen. If
11239 b->clip_changed has already been set to 1, we can skip this
11240 check. */
11241 if (!b->clip_changed
11242 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11243 {
11244 int pt;
11245
11246 if (w == XWINDOW (selected_window))
11247 pt = BUF_PT (current_buffer);
11248 else
11249 pt = marker_position (w->pointm);
11250
11251 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11252 || pt != XINT (w->last_point))
11253 && check_point_in_composition (w->current_matrix->buffer,
11254 XINT (w->last_point),
11255 XBUFFER (w->buffer), pt))
11256 b->clip_changed = 1;
11257 }
11258 }
11259 \f
11260
11261 /* Select FRAME to forward the values of frame-local variables into C
11262 variables so that the redisplay routines can access those values
11263 directly. */
11264
11265 static void
11266 select_frame_for_redisplay (frame)
11267 Lisp_Object frame;
11268 {
11269 Lisp_Object tail, symbol, val;
11270 Lisp_Object old = selected_frame;
11271 struct Lisp_Symbol *sym;
11272
11273 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11274
11275 selected_frame = frame;
11276
11277 do
11278 {
11279 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11280 if (CONSP (XCAR (tail))
11281 && (symbol = XCAR (XCAR (tail)),
11282 SYMBOLP (symbol))
11283 && (sym = indirect_variable (XSYMBOL (symbol)),
11284 val = sym->value,
11285 (BUFFER_LOCAL_VALUEP (val)))
11286 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11287 /* Use find_symbol_value rather than Fsymbol_value
11288 to avoid an error if it is void. */
11289 find_symbol_value (symbol);
11290 } while (!EQ (frame, old) && (frame = old, 1));
11291 }
11292
11293
11294 #define STOP_POLLING \
11295 do { if (! polling_stopped_here) stop_polling (); \
11296 polling_stopped_here = 1; } while (0)
11297
11298 #define RESUME_POLLING \
11299 do { if (polling_stopped_here) start_polling (); \
11300 polling_stopped_here = 0; } while (0)
11301
11302
11303 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11304 response to any user action; therefore, we should preserve the echo
11305 area. (Actually, our caller does that job.) Perhaps in the future
11306 avoid recentering windows if it is not necessary; currently that
11307 causes some problems. */
11308
11309 static void
11310 redisplay_internal (preserve_echo_area)
11311 int preserve_echo_area;
11312 {
11313 struct window *w = XWINDOW (selected_window);
11314 struct frame *f;
11315 int pause;
11316 int must_finish = 0;
11317 struct text_pos tlbufpos, tlendpos;
11318 int number_of_visible_frames;
11319 int count, count1;
11320 struct frame *sf;
11321 int polling_stopped_here = 0;
11322 Lisp_Object old_frame = selected_frame;
11323
11324 /* Non-zero means redisplay has to consider all windows on all
11325 frames. Zero means, only selected_window is considered. */
11326 int consider_all_windows_p;
11327
11328 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11329
11330 /* No redisplay if running in batch mode or frame is not yet fully
11331 initialized, or redisplay is explicitly turned off by setting
11332 Vinhibit_redisplay. */
11333 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11334 || !NILP (Vinhibit_redisplay))
11335 return;
11336
11337 /* Don't examine these until after testing Vinhibit_redisplay.
11338 When Emacs is shutting down, perhaps because its connection to
11339 X has dropped, we should not look at them at all. */
11340 f = XFRAME (w->frame);
11341 sf = SELECTED_FRAME ();
11342
11343 if (!f->glyphs_initialized_p)
11344 return;
11345
11346 /* The flag redisplay_performed_directly_p is set by
11347 direct_output_for_insert when it already did the whole screen
11348 update necessary. */
11349 if (redisplay_performed_directly_p)
11350 {
11351 redisplay_performed_directly_p = 0;
11352 if (!hscroll_windows (selected_window))
11353 return;
11354 }
11355
11356 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11357 if (popup_activated ())
11358 return;
11359 #endif
11360
11361 /* I don't think this happens but let's be paranoid. */
11362 if (redisplaying_p)
11363 return;
11364
11365 /* Record a function that resets redisplaying_p to its old value
11366 when we leave this function. */
11367 count = SPECPDL_INDEX ();
11368 record_unwind_protect (unwind_redisplay,
11369 Fcons (make_number (redisplaying_p), selected_frame));
11370 ++redisplaying_p;
11371 specbind (Qinhibit_free_realized_faces, Qnil);
11372
11373 {
11374 Lisp_Object tail, frame;
11375
11376 FOR_EACH_FRAME (tail, frame)
11377 {
11378 struct frame *f = XFRAME (frame);
11379 f->already_hscrolled_p = 0;
11380 }
11381 }
11382
11383 retry:
11384 if (!EQ (old_frame, selected_frame)
11385 && FRAME_LIVE_P (XFRAME (old_frame)))
11386 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11387 selected_frame and selected_window to be temporarily out-of-sync so
11388 when we come back here via `goto retry', we need to resync because we
11389 may need to run Elisp code (via prepare_menu_bars). */
11390 select_frame_for_redisplay (old_frame);
11391
11392 pause = 0;
11393 reconsider_clip_changes (w, current_buffer);
11394 last_escape_glyph_frame = NULL;
11395 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11396
11397 /* If new fonts have been loaded that make a glyph matrix adjustment
11398 necessary, do it. */
11399 if (fonts_changed_p)
11400 {
11401 adjust_glyphs (NULL);
11402 ++windows_or_buffers_changed;
11403 fonts_changed_p = 0;
11404 }
11405
11406 /* If face_change_count is non-zero, init_iterator will free all
11407 realized faces, which includes the faces referenced from current
11408 matrices. So, we can't reuse current matrices in this case. */
11409 if (face_change_count)
11410 ++windows_or_buffers_changed;
11411
11412 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11413 && FRAME_TTY (sf)->previous_frame != sf)
11414 {
11415 /* Since frames on a single ASCII terminal share the same
11416 display area, displaying a different frame means redisplay
11417 the whole thing. */
11418 windows_or_buffers_changed++;
11419 SET_FRAME_GARBAGED (sf);
11420 #ifndef DOS_NT
11421 set_tty_color_mode (FRAME_TTY (sf), sf);
11422 #endif
11423 FRAME_TTY (sf)->previous_frame = sf;
11424 }
11425
11426 /* Set the visible flags for all frames. Do this before checking
11427 for resized or garbaged frames; they want to know if their frames
11428 are visible. See the comment in frame.h for
11429 FRAME_SAMPLE_VISIBILITY. */
11430 {
11431 Lisp_Object tail, frame;
11432
11433 number_of_visible_frames = 0;
11434
11435 FOR_EACH_FRAME (tail, frame)
11436 {
11437 struct frame *f = XFRAME (frame);
11438
11439 FRAME_SAMPLE_VISIBILITY (f);
11440 if (FRAME_VISIBLE_P (f))
11441 ++number_of_visible_frames;
11442 clear_desired_matrices (f);
11443 }
11444 }
11445
11446 /* Notice any pending interrupt request to change frame size. */
11447 do_pending_window_change (1);
11448
11449 /* Clear frames marked as garbaged. */
11450 if (frame_garbaged)
11451 clear_garbaged_frames ();
11452
11453 /* Build menubar and tool-bar items. */
11454 if (NILP (Vmemory_full))
11455 prepare_menu_bars ();
11456
11457 if (windows_or_buffers_changed)
11458 update_mode_lines++;
11459
11460 /* Detect case that we need to write or remove a star in the mode line. */
11461 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11462 {
11463 w->update_mode_line = Qt;
11464 if (buffer_shared > 1)
11465 update_mode_lines++;
11466 }
11467
11468 /* Avoid invocation of point motion hooks by `current_column' below. */
11469 count1 = SPECPDL_INDEX ();
11470 specbind (Qinhibit_point_motion_hooks, Qt);
11471
11472 /* If %c is in the mode line, update it if needed. */
11473 if (!NILP (w->column_number_displayed)
11474 /* This alternative quickly identifies a common case
11475 where no change is needed. */
11476 && !(PT == XFASTINT (w->last_point)
11477 && XFASTINT (w->last_modified) >= MODIFF
11478 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11479 && (XFASTINT (w->column_number_displayed)
11480 != (int) current_column ())) /* iftc */
11481 w->update_mode_line = Qt;
11482
11483 unbind_to (count1, Qnil);
11484
11485 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11486
11487 /* The variable buffer_shared is set in redisplay_window and
11488 indicates that we redisplay a buffer in different windows. See
11489 there. */
11490 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11491 || cursor_type_changed);
11492
11493 /* If specs for an arrow have changed, do thorough redisplay
11494 to ensure we remove any arrow that should no longer exist. */
11495 if (overlay_arrows_changed_p ())
11496 consider_all_windows_p = windows_or_buffers_changed = 1;
11497
11498 /* Normally the message* functions will have already displayed and
11499 updated the echo area, but the frame may have been trashed, or
11500 the update may have been preempted, so display the echo area
11501 again here. Checking message_cleared_p captures the case that
11502 the echo area should be cleared. */
11503 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11504 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11505 || (message_cleared_p
11506 && minibuf_level == 0
11507 /* If the mini-window is currently selected, this means the
11508 echo-area doesn't show through. */
11509 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11510 {
11511 int window_height_changed_p = echo_area_display (0);
11512 must_finish = 1;
11513
11514 /* If we don't display the current message, don't clear the
11515 message_cleared_p flag, because, if we did, we wouldn't clear
11516 the echo area in the next redisplay which doesn't preserve
11517 the echo area. */
11518 if (!display_last_displayed_message_p)
11519 message_cleared_p = 0;
11520
11521 if (fonts_changed_p)
11522 goto retry;
11523 else if (window_height_changed_p)
11524 {
11525 consider_all_windows_p = 1;
11526 ++update_mode_lines;
11527 ++windows_or_buffers_changed;
11528
11529 /* If window configuration was changed, frames may have been
11530 marked garbaged. Clear them or we will experience
11531 surprises wrt scrolling. */
11532 if (frame_garbaged)
11533 clear_garbaged_frames ();
11534 }
11535 }
11536 else if (EQ (selected_window, minibuf_window)
11537 && (current_buffer->clip_changed
11538 || XFASTINT (w->last_modified) < MODIFF
11539 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11540 && resize_mini_window (w, 0))
11541 {
11542 /* Resized active mini-window to fit the size of what it is
11543 showing if its contents might have changed. */
11544 must_finish = 1;
11545 /* FIXME: this causes all frames to be updated, which seems unnecessary
11546 since only the current frame needs to be considered. This function needs
11547 to be rewritten with two variables, consider_all_windows and
11548 consider_all_frames. */
11549 consider_all_windows_p = 1;
11550 ++windows_or_buffers_changed;
11551 ++update_mode_lines;
11552
11553 /* If window configuration was changed, frames may have been
11554 marked garbaged. Clear them or we will experience
11555 surprises wrt scrolling. */
11556 if (frame_garbaged)
11557 clear_garbaged_frames ();
11558 }
11559
11560
11561 /* If showing the region, and mark has changed, we must redisplay
11562 the whole window. The assignment to this_line_start_pos prevents
11563 the optimization directly below this if-statement. */
11564 if (((!NILP (Vtransient_mark_mode)
11565 && !NILP (XBUFFER (w->buffer)->mark_active))
11566 != !NILP (w->region_showing))
11567 || (!NILP (w->region_showing)
11568 && !EQ (w->region_showing,
11569 Fmarker_position (XBUFFER (w->buffer)->mark))))
11570 CHARPOS (this_line_start_pos) = 0;
11571
11572 /* Optimize the case that only the line containing the cursor in the
11573 selected window has changed. Variables starting with this_ are
11574 set in display_line and record information about the line
11575 containing the cursor. */
11576 tlbufpos = this_line_start_pos;
11577 tlendpos = this_line_end_pos;
11578 if (!consider_all_windows_p
11579 && CHARPOS (tlbufpos) > 0
11580 && NILP (w->update_mode_line)
11581 && !current_buffer->clip_changed
11582 && !current_buffer->prevent_redisplay_optimizations_p
11583 && FRAME_VISIBLE_P (XFRAME (w->frame))
11584 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11585 /* Make sure recorded data applies to current buffer, etc. */
11586 && this_line_buffer == current_buffer
11587 && current_buffer == XBUFFER (w->buffer)
11588 && NILP (w->force_start)
11589 && NILP (w->optional_new_start)
11590 /* Point must be on the line that we have info recorded about. */
11591 && PT >= CHARPOS (tlbufpos)
11592 && PT <= Z - CHARPOS (tlendpos)
11593 /* All text outside that line, including its final newline,
11594 must be unchanged */
11595 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11596 CHARPOS (tlendpos)))
11597 {
11598 if (CHARPOS (tlbufpos) > BEGV
11599 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11600 && (CHARPOS (tlbufpos) == ZV
11601 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11602 /* Former continuation line has disappeared by becoming empty */
11603 goto cancel;
11604 else if (XFASTINT (w->last_modified) < MODIFF
11605 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11606 || MINI_WINDOW_P (w))
11607 {
11608 /* We have to handle the case of continuation around a
11609 wide-column character (See the comment in indent.c around
11610 line 885).
11611
11612 For instance, in the following case:
11613
11614 -------- Insert --------
11615 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11616 J_I_ ==> J_I_ `^^' are cursors.
11617 ^^ ^^
11618 -------- --------
11619
11620 As we have to redraw the line above, we should goto cancel. */
11621
11622 struct it it;
11623 int line_height_before = this_line_pixel_height;
11624
11625 /* Note that start_display will handle the case that the
11626 line starting at tlbufpos is a continuation lines. */
11627 start_display (&it, w, tlbufpos);
11628
11629 /* Implementation note: It this still necessary? */
11630 if (it.current_x != this_line_start_x)
11631 goto cancel;
11632
11633 TRACE ((stderr, "trying display optimization 1\n"));
11634 w->cursor.vpos = -1;
11635 overlay_arrow_seen = 0;
11636 it.vpos = this_line_vpos;
11637 it.current_y = this_line_y;
11638 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11639 display_line (&it);
11640
11641 /* If line contains point, is not continued,
11642 and ends at same distance from eob as before, we win */
11643 if (w->cursor.vpos >= 0
11644 /* Line is not continued, otherwise this_line_start_pos
11645 would have been set to 0 in display_line. */
11646 && CHARPOS (this_line_start_pos)
11647 /* Line ends as before. */
11648 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11649 /* Line has same height as before. Otherwise other lines
11650 would have to be shifted up or down. */
11651 && this_line_pixel_height == line_height_before)
11652 {
11653 /* If this is not the window's last line, we must adjust
11654 the charstarts of the lines below. */
11655 if (it.current_y < it.last_visible_y)
11656 {
11657 struct glyph_row *row
11658 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11659 int delta, delta_bytes;
11660
11661 /* We used to distinguish between two cases here,
11662 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11663 when the line ends in a newline or the end of the
11664 buffer's accessible portion. But both cases did
11665 the same, so they were collapsed. */
11666 delta = (Z
11667 - CHARPOS (tlendpos)
11668 - MATRIX_ROW_START_CHARPOS (row));
11669 delta_bytes = (Z_BYTE
11670 - BYTEPOS (tlendpos)
11671 - MATRIX_ROW_START_BYTEPOS (row));
11672
11673 increment_matrix_positions (w->current_matrix,
11674 this_line_vpos + 1,
11675 w->current_matrix->nrows,
11676 delta, delta_bytes);
11677 }
11678
11679 /* If this row displays text now but previously didn't,
11680 or vice versa, w->window_end_vpos may have to be
11681 adjusted. */
11682 if ((it.glyph_row - 1)->displays_text_p)
11683 {
11684 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11685 XSETINT (w->window_end_vpos, this_line_vpos);
11686 }
11687 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11688 && this_line_vpos > 0)
11689 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11690 w->window_end_valid = Qnil;
11691
11692 /* Update hint: No need to try to scroll in update_window. */
11693 w->desired_matrix->no_scrolling_p = 1;
11694
11695 #if GLYPH_DEBUG
11696 *w->desired_matrix->method = 0;
11697 debug_method_add (w, "optimization 1");
11698 #endif
11699 #ifdef HAVE_WINDOW_SYSTEM
11700 update_window_fringes (w, 0);
11701 #endif
11702 goto update;
11703 }
11704 else
11705 goto cancel;
11706 }
11707 else if (/* Cursor position hasn't changed. */
11708 PT == XFASTINT (w->last_point)
11709 /* Make sure the cursor was last displayed
11710 in this window. Otherwise we have to reposition it. */
11711 && 0 <= w->cursor.vpos
11712 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11713 {
11714 if (!must_finish)
11715 {
11716 do_pending_window_change (1);
11717
11718 /* We used to always goto end_of_redisplay here, but this
11719 isn't enough if we have a blinking cursor. */
11720 if (w->cursor_off_p == w->last_cursor_off_p)
11721 goto end_of_redisplay;
11722 }
11723 goto update;
11724 }
11725 /* If highlighting the region, or if the cursor is in the echo area,
11726 then we can't just move the cursor. */
11727 else if (! (!NILP (Vtransient_mark_mode)
11728 && !NILP (current_buffer->mark_active))
11729 && (EQ (selected_window, current_buffer->last_selected_window)
11730 || highlight_nonselected_windows)
11731 && NILP (w->region_showing)
11732 && NILP (Vshow_trailing_whitespace)
11733 && !cursor_in_echo_area)
11734 {
11735 struct it it;
11736 struct glyph_row *row;
11737
11738 /* Skip from tlbufpos to PT and see where it is. Note that
11739 PT may be in invisible text. If so, we will end at the
11740 next visible position. */
11741 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11742 NULL, DEFAULT_FACE_ID);
11743 it.current_x = this_line_start_x;
11744 it.current_y = this_line_y;
11745 it.vpos = this_line_vpos;
11746
11747 /* The call to move_it_to stops in front of PT, but
11748 moves over before-strings. */
11749 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11750
11751 if (it.vpos == this_line_vpos
11752 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11753 row->enabled_p))
11754 {
11755 xassert (this_line_vpos == it.vpos);
11756 xassert (this_line_y == it.current_y);
11757 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11758 #if GLYPH_DEBUG
11759 *w->desired_matrix->method = 0;
11760 debug_method_add (w, "optimization 3");
11761 #endif
11762 goto update;
11763 }
11764 else
11765 goto cancel;
11766 }
11767
11768 cancel:
11769 /* Text changed drastically or point moved off of line. */
11770 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11771 }
11772
11773 CHARPOS (this_line_start_pos) = 0;
11774 consider_all_windows_p |= buffer_shared > 1;
11775 ++clear_face_cache_count;
11776 #ifdef HAVE_WINDOW_SYSTEM
11777 ++clear_image_cache_count;
11778 #endif
11779
11780 /* Build desired matrices, and update the display. If
11781 consider_all_windows_p is non-zero, do it for all windows on all
11782 frames. Otherwise do it for selected_window, only. */
11783
11784 if (consider_all_windows_p)
11785 {
11786 Lisp_Object tail, frame;
11787
11788 FOR_EACH_FRAME (tail, frame)
11789 XFRAME (frame)->updated_p = 0;
11790
11791 /* Recompute # windows showing selected buffer. This will be
11792 incremented each time such a window is displayed. */
11793 buffer_shared = 0;
11794
11795 FOR_EACH_FRAME (tail, frame)
11796 {
11797 struct frame *f = XFRAME (frame);
11798
11799 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11800 {
11801 if (! EQ (frame, selected_frame))
11802 /* Select the frame, for the sake of frame-local
11803 variables. */
11804 select_frame_for_redisplay (frame);
11805
11806 /* Mark all the scroll bars to be removed; we'll redeem
11807 the ones we want when we redisplay their windows. */
11808 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11809 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11810
11811 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11812 redisplay_windows (FRAME_ROOT_WINDOW (f));
11813
11814 /* The X error handler may have deleted that frame. */
11815 if (!FRAME_LIVE_P (f))
11816 continue;
11817
11818 /* Any scroll bars which redisplay_windows should have
11819 nuked should now go away. */
11820 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11821 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11822
11823 /* If fonts changed, display again. */
11824 /* ??? rms: I suspect it is a mistake to jump all the way
11825 back to retry here. It should just retry this frame. */
11826 if (fonts_changed_p)
11827 goto retry;
11828
11829 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11830 {
11831 /* See if we have to hscroll. */
11832 if (!f->already_hscrolled_p)
11833 {
11834 f->already_hscrolled_p = 1;
11835 if (hscroll_windows (f->root_window))
11836 goto retry;
11837 }
11838
11839 /* Prevent various kinds of signals during display
11840 update. stdio is not robust about handling
11841 signals, which can cause an apparent I/O
11842 error. */
11843 if (interrupt_input)
11844 unrequest_sigio ();
11845 STOP_POLLING;
11846
11847 /* Update the display. */
11848 set_window_update_flags (XWINDOW (f->root_window), 1);
11849 pause |= update_frame (f, 0, 0);
11850 f->updated_p = 1;
11851 }
11852 }
11853 }
11854
11855 if (!EQ (old_frame, selected_frame)
11856 && FRAME_LIVE_P (XFRAME (old_frame)))
11857 /* We played a bit fast-and-loose above and allowed selected_frame
11858 and selected_window to be temporarily out-of-sync but let's make
11859 sure this stays contained. */
11860 select_frame_for_redisplay (old_frame);
11861 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11862
11863 if (!pause)
11864 {
11865 /* Do the mark_window_display_accurate after all windows have
11866 been redisplayed because this call resets flags in buffers
11867 which are needed for proper redisplay. */
11868 FOR_EACH_FRAME (tail, frame)
11869 {
11870 struct frame *f = XFRAME (frame);
11871 if (f->updated_p)
11872 {
11873 mark_window_display_accurate (f->root_window, 1);
11874 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11875 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11876 }
11877 }
11878 }
11879 }
11880 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11881 {
11882 Lisp_Object mini_window;
11883 struct frame *mini_frame;
11884
11885 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11886 /* Use list_of_error, not Qerror, so that
11887 we catch only errors and don't run the debugger. */
11888 internal_condition_case_1 (redisplay_window_1, selected_window,
11889 list_of_error,
11890 redisplay_window_error);
11891
11892 /* Compare desired and current matrices, perform output. */
11893
11894 update:
11895 /* If fonts changed, display again. */
11896 if (fonts_changed_p)
11897 goto retry;
11898
11899 /* Prevent various kinds of signals during display update.
11900 stdio is not robust about handling signals,
11901 which can cause an apparent I/O error. */
11902 if (interrupt_input)
11903 unrequest_sigio ();
11904 STOP_POLLING;
11905
11906 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11907 {
11908 if (hscroll_windows (selected_window))
11909 goto retry;
11910
11911 XWINDOW (selected_window)->must_be_updated_p = 1;
11912 pause = update_frame (sf, 0, 0);
11913 }
11914
11915 /* We may have called echo_area_display at the top of this
11916 function. If the echo area is on another frame, that may
11917 have put text on a frame other than the selected one, so the
11918 above call to update_frame would not have caught it. Catch
11919 it here. */
11920 mini_window = FRAME_MINIBUF_WINDOW (sf);
11921 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11922
11923 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11924 {
11925 XWINDOW (mini_window)->must_be_updated_p = 1;
11926 pause |= update_frame (mini_frame, 0, 0);
11927 if (!pause && hscroll_windows (mini_window))
11928 goto retry;
11929 }
11930 }
11931
11932 /* If display was paused because of pending input, make sure we do a
11933 thorough update the next time. */
11934 if (pause)
11935 {
11936 /* Prevent the optimization at the beginning of
11937 redisplay_internal that tries a single-line update of the
11938 line containing the cursor in the selected window. */
11939 CHARPOS (this_line_start_pos) = 0;
11940
11941 /* Let the overlay arrow be updated the next time. */
11942 update_overlay_arrows (0);
11943
11944 /* If we pause after scrolling, some rows in the current
11945 matrices of some windows are not valid. */
11946 if (!WINDOW_FULL_WIDTH_P (w)
11947 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11948 update_mode_lines = 1;
11949 }
11950 else
11951 {
11952 if (!consider_all_windows_p)
11953 {
11954 /* This has already been done above if
11955 consider_all_windows_p is set. */
11956 mark_window_display_accurate_1 (w, 1);
11957
11958 /* Say overlay arrows are up to date. */
11959 update_overlay_arrows (1);
11960
11961 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11962 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11963 }
11964
11965 update_mode_lines = 0;
11966 windows_or_buffers_changed = 0;
11967 cursor_type_changed = 0;
11968 }
11969
11970 /* Start SIGIO interrupts coming again. Having them off during the
11971 code above makes it less likely one will discard output, but not
11972 impossible, since there might be stuff in the system buffer here.
11973 But it is much hairier to try to do anything about that. */
11974 if (interrupt_input)
11975 request_sigio ();
11976 RESUME_POLLING;
11977
11978 /* If a frame has become visible which was not before, redisplay
11979 again, so that we display it. Expose events for such a frame
11980 (which it gets when becoming visible) don't call the parts of
11981 redisplay constructing glyphs, so simply exposing a frame won't
11982 display anything in this case. So, we have to display these
11983 frames here explicitly. */
11984 if (!pause)
11985 {
11986 Lisp_Object tail, frame;
11987 int new_count = 0;
11988
11989 FOR_EACH_FRAME (tail, frame)
11990 {
11991 int this_is_visible = 0;
11992
11993 if (XFRAME (frame)->visible)
11994 this_is_visible = 1;
11995 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11996 if (XFRAME (frame)->visible)
11997 this_is_visible = 1;
11998
11999 if (this_is_visible)
12000 new_count++;
12001 }
12002
12003 if (new_count != number_of_visible_frames)
12004 windows_or_buffers_changed++;
12005 }
12006
12007 /* Change frame size now if a change is pending. */
12008 do_pending_window_change (1);
12009
12010 /* If we just did a pending size change, or have additional
12011 visible frames, redisplay again. */
12012 if (windows_or_buffers_changed && !pause)
12013 goto retry;
12014
12015 /* Clear the face cache eventually. */
12016 if (consider_all_windows_p)
12017 {
12018 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12019 {
12020 clear_face_cache (0);
12021 clear_face_cache_count = 0;
12022 }
12023 #ifdef HAVE_WINDOW_SYSTEM
12024 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12025 {
12026 clear_image_caches (Qnil);
12027 clear_image_cache_count = 0;
12028 }
12029 #endif /* HAVE_WINDOW_SYSTEM */
12030 }
12031
12032 end_of_redisplay:
12033 unbind_to (count, Qnil);
12034 RESUME_POLLING;
12035 }
12036
12037
12038 /* Redisplay, but leave alone any recent echo area message unless
12039 another message has been requested in its place.
12040
12041 This is useful in situations where you need to redisplay but no
12042 user action has occurred, making it inappropriate for the message
12043 area to be cleared. See tracking_off and
12044 wait_reading_process_output for examples of these situations.
12045
12046 FROM_WHERE is an integer saying from where this function was
12047 called. This is useful for debugging. */
12048
12049 void
12050 redisplay_preserve_echo_area (from_where)
12051 int from_where;
12052 {
12053 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12054
12055 if (!NILP (echo_area_buffer[1]))
12056 {
12057 /* We have a previously displayed message, but no current
12058 message. Redisplay the previous message. */
12059 display_last_displayed_message_p = 1;
12060 redisplay_internal (1);
12061 display_last_displayed_message_p = 0;
12062 }
12063 else
12064 redisplay_internal (1);
12065
12066 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12067 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12068 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12069 }
12070
12071
12072 /* Function registered with record_unwind_protect in
12073 redisplay_internal. Reset redisplaying_p to the value it had
12074 before redisplay_internal was called, and clear
12075 prevent_freeing_realized_faces_p. It also selects the previously
12076 selected frame, unless it has been deleted (by an X connection
12077 failure during redisplay, for example). */
12078
12079 static Lisp_Object
12080 unwind_redisplay (val)
12081 Lisp_Object val;
12082 {
12083 Lisp_Object old_redisplaying_p, old_frame;
12084
12085 old_redisplaying_p = XCAR (val);
12086 redisplaying_p = XFASTINT (old_redisplaying_p);
12087 old_frame = XCDR (val);
12088 if (! EQ (old_frame, selected_frame)
12089 && FRAME_LIVE_P (XFRAME (old_frame)))
12090 select_frame_for_redisplay (old_frame);
12091 return Qnil;
12092 }
12093
12094
12095 /* Mark the display of window W as accurate or inaccurate. If
12096 ACCURATE_P is non-zero mark display of W as accurate. If
12097 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12098 redisplay_internal is called. */
12099
12100 static void
12101 mark_window_display_accurate_1 (w, accurate_p)
12102 struct window *w;
12103 int accurate_p;
12104 {
12105 if (BUFFERP (w->buffer))
12106 {
12107 struct buffer *b = XBUFFER (w->buffer);
12108
12109 w->last_modified
12110 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12111 w->last_overlay_modified
12112 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12113 w->last_had_star
12114 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12115
12116 if (accurate_p)
12117 {
12118 b->clip_changed = 0;
12119 b->prevent_redisplay_optimizations_p = 0;
12120
12121 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12122 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12123 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12124 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12125
12126 w->current_matrix->buffer = b;
12127 w->current_matrix->begv = BUF_BEGV (b);
12128 w->current_matrix->zv = BUF_ZV (b);
12129
12130 w->last_cursor = w->cursor;
12131 w->last_cursor_off_p = w->cursor_off_p;
12132
12133 if (w == XWINDOW (selected_window))
12134 w->last_point = make_number (BUF_PT (b));
12135 else
12136 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12137 }
12138 }
12139
12140 if (accurate_p)
12141 {
12142 w->window_end_valid = w->buffer;
12143 w->update_mode_line = Qnil;
12144 }
12145 }
12146
12147
12148 /* Mark the display of windows in the window tree rooted at WINDOW as
12149 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12150 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12151 be redisplayed the next time redisplay_internal is called. */
12152
12153 void
12154 mark_window_display_accurate (window, accurate_p)
12155 Lisp_Object window;
12156 int accurate_p;
12157 {
12158 struct window *w;
12159
12160 for (; !NILP (window); window = w->next)
12161 {
12162 w = XWINDOW (window);
12163 mark_window_display_accurate_1 (w, accurate_p);
12164
12165 if (!NILP (w->vchild))
12166 mark_window_display_accurate (w->vchild, accurate_p);
12167 if (!NILP (w->hchild))
12168 mark_window_display_accurate (w->hchild, accurate_p);
12169 }
12170
12171 if (accurate_p)
12172 {
12173 update_overlay_arrows (1);
12174 }
12175 else
12176 {
12177 /* Force a thorough redisplay the next time by setting
12178 last_arrow_position and last_arrow_string to t, which is
12179 unequal to any useful value of Voverlay_arrow_... */
12180 update_overlay_arrows (-1);
12181 }
12182 }
12183
12184
12185 /* Return value in display table DP (Lisp_Char_Table *) for character
12186 C. Since a display table doesn't have any parent, we don't have to
12187 follow parent. Do not call this function directly but use the
12188 macro DISP_CHAR_VECTOR. */
12189
12190 Lisp_Object
12191 disp_char_vector (dp, c)
12192 struct Lisp_Char_Table *dp;
12193 int c;
12194 {
12195 Lisp_Object val;
12196
12197 if (ASCII_CHAR_P (c))
12198 {
12199 val = dp->ascii;
12200 if (SUB_CHAR_TABLE_P (val))
12201 val = XSUB_CHAR_TABLE (val)->contents[c];
12202 }
12203 else
12204 {
12205 Lisp_Object table;
12206
12207 XSETCHAR_TABLE (table, dp);
12208 val = char_table_ref (table, c);
12209 }
12210 if (NILP (val))
12211 val = dp->defalt;
12212 return val;
12213 }
12214
12215
12216 \f
12217 /***********************************************************************
12218 Window Redisplay
12219 ***********************************************************************/
12220
12221 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12222
12223 static void
12224 redisplay_windows (window)
12225 Lisp_Object window;
12226 {
12227 while (!NILP (window))
12228 {
12229 struct window *w = XWINDOW (window);
12230
12231 if (!NILP (w->hchild))
12232 redisplay_windows (w->hchild);
12233 else if (!NILP (w->vchild))
12234 redisplay_windows (w->vchild);
12235 else if (!NILP (w->buffer))
12236 {
12237 displayed_buffer = XBUFFER (w->buffer);
12238 /* Use list_of_error, not Qerror, so that
12239 we catch only errors and don't run the debugger. */
12240 internal_condition_case_1 (redisplay_window_0, window,
12241 list_of_error,
12242 redisplay_window_error);
12243 }
12244
12245 window = w->next;
12246 }
12247 }
12248
12249 static Lisp_Object
12250 redisplay_window_error ()
12251 {
12252 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12253 return Qnil;
12254 }
12255
12256 static Lisp_Object
12257 redisplay_window_0 (window)
12258 Lisp_Object window;
12259 {
12260 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12261 redisplay_window (window, 0);
12262 return Qnil;
12263 }
12264
12265 static Lisp_Object
12266 redisplay_window_1 (window)
12267 Lisp_Object window;
12268 {
12269 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12270 redisplay_window (window, 1);
12271 return Qnil;
12272 }
12273 \f
12274
12275 /* Increment GLYPH until it reaches END or CONDITION fails while
12276 adding (GLYPH)->pixel_width to X. */
12277
12278 #define SKIP_GLYPHS(glyph, end, x, condition) \
12279 do \
12280 { \
12281 (x) += (glyph)->pixel_width; \
12282 ++(glyph); \
12283 } \
12284 while ((glyph) < (end) && (condition))
12285
12286
12287 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12288 DELTA is the number of bytes by which positions recorded in ROW
12289 differ from current buffer positions.
12290
12291 Return 0 if cursor is not on this row. 1 otherwise. */
12292
12293 int
12294 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12295 struct window *w;
12296 struct glyph_row *row;
12297 struct glyph_matrix *matrix;
12298 int delta, delta_bytes, dy, dvpos;
12299 {
12300 struct glyph *glyph = row->glyphs[TEXT_AREA];
12301 struct glyph *end = glyph + row->used[TEXT_AREA];
12302 struct glyph *cursor = NULL;
12303 /* The first glyph that starts a sequence of glyphs from string. */
12304 struct glyph *string_start;
12305 /* The X coordinate of string_start. */
12306 int string_start_x;
12307 /* The last known character position. */
12308 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12309 /* The last known character position before string_start. */
12310 int string_before_pos;
12311 int x = row->x;
12312 int cursor_x = x;
12313 int cursor_from_overlay_pos = 0;
12314 int pt_old = PT - delta;
12315
12316 /* Skip over glyphs not having an object at the start of the row.
12317 These are special glyphs like truncation marks on terminal
12318 frames. */
12319 if (row->displays_text_p)
12320 while (glyph < end
12321 && INTEGERP (glyph->object)
12322 && glyph->charpos < 0)
12323 {
12324 x += glyph->pixel_width;
12325 ++glyph;
12326 }
12327
12328 string_start = NULL;
12329 while (glyph < end
12330 && !INTEGERP (glyph->object)
12331 && (!BUFFERP (glyph->object)
12332 || (last_pos = glyph->charpos) < pt_old
12333 || glyph->avoid_cursor_p))
12334 {
12335 if (! STRINGP (glyph->object))
12336 {
12337 string_start = NULL;
12338 x += glyph->pixel_width;
12339 ++glyph;
12340 if (cursor_from_overlay_pos
12341 && last_pos >= cursor_from_overlay_pos)
12342 {
12343 cursor_from_overlay_pos = 0;
12344 cursor = 0;
12345 }
12346 }
12347 else
12348 {
12349 if (string_start == NULL)
12350 {
12351 string_before_pos = last_pos;
12352 string_start = glyph;
12353 string_start_x = x;
12354 }
12355 /* Skip all glyphs from string. */
12356 do
12357 {
12358 Lisp_Object cprop;
12359 int pos;
12360 if ((cursor == NULL || glyph > cursor)
12361 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12362 Qcursor, (glyph)->object),
12363 !NILP (cprop))
12364 && (pos = string_buffer_position (w, glyph->object,
12365 string_before_pos),
12366 (pos == 0 /* From overlay */
12367 || pos == pt_old)))
12368 {
12369 /* Estimate overlay buffer position from the buffer
12370 positions of the glyphs before and after the overlay.
12371 Add 1 to last_pos so that if point corresponds to the
12372 glyph right after the overlay, we still use a 'cursor'
12373 property found in that overlay. */
12374 cursor_from_overlay_pos = (pos ? 0 : last_pos
12375 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12376 cursor = glyph;
12377 cursor_x = x;
12378 }
12379 x += glyph->pixel_width;
12380 ++glyph;
12381 }
12382 while (glyph < end && EQ (glyph->object, string_start->object));
12383 }
12384 }
12385
12386 if (cursor != NULL)
12387 {
12388 glyph = cursor;
12389 x = cursor_x;
12390 }
12391 else if (row->ends_in_ellipsis_p && glyph == end)
12392 {
12393 /* Scan back over the ellipsis glyphs, decrementing positions. */
12394 while (glyph > row->glyphs[TEXT_AREA]
12395 && (glyph - 1)->charpos == last_pos)
12396 glyph--, x -= glyph->pixel_width;
12397 /* That loop always goes one position too far,
12398 including the glyph before the ellipsis.
12399 So scan forward over that one. */
12400 x += glyph->pixel_width;
12401 glyph++;
12402 }
12403 else if (string_start
12404 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12405 {
12406 /* We may have skipped over point because the previous glyphs
12407 are from string. As there's no easy way to know the
12408 character position of the current glyph, find the correct
12409 glyph on point by scanning from string_start again. */
12410 Lisp_Object limit;
12411 Lisp_Object string;
12412 struct glyph *stop = glyph;
12413 int pos;
12414
12415 limit = make_number (pt_old + 1);
12416 glyph = string_start;
12417 x = string_start_x;
12418 string = glyph->object;
12419 pos = string_buffer_position (w, string, string_before_pos);
12420 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12421 because we always put cursor after overlay strings. */
12422 while (pos == 0 && glyph < stop)
12423 {
12424 string = glyph->object;
12425 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12426 if (glyph < stop)
12427 pos = string_buffer_position (w, glyph->object, string_before_pos);
12428 }
12429
12430 while (glyph < stop)
12431 {
12432 pos = XINT (Fnext_single_char_property_change
12433 (make_number (pos), Qdisplay, Qnil, limit));
12434 if (pos > pt_old)
12435 break;
12436 /* Skip glyphs from the same string. */
12437 string = glyph->object;
12438 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12439 /* Skip glyphs from an overlay. */
12440 while (glyph < stop
12441 && ! string_buffer_position (w, glyph->object, pos))
12442 {
12443 string = glyph->object;
12444 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12445 }
12446 }
12447
12448 /* If we reached the end of the line, and end was from a string,
12449 cursor is not on this line. */
12450 if (glyph == end && row->continued_p)
12451 return 0;
12452 }
12453
12454 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12455 w->cursor.x = x;
12456 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12457 w->cursor.y = row->y + dy;
12458
12459 if (w == XWINDOW (selected_window))
12460 {
12461 if (!row->continued_p
12462 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12463 && row->x == 0)
12464 {
12465 this_line_buffer = XBUFFER (w->buffer);
12466
12467 CHARPOS (this_line_start_pos)
12468 = MATRIX_ROW_START_CHARPOS (row) + delta;
12469 BYTEPOS (this_line_start_pos)
12470 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12471
12472 CHARPOS (this_line_end_pos)
12473 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12474 BYTEPOS (this_line_end_pos)
12475 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12476
12477 this_line_y = w->cursor.y;
12478 this_line_pixel_height = row->height;
12479 this_line_vpos = w->cursor.vpos;
12480 this_line_start_x = row->x;
12481 }
12482 else
12483 CHARPOS (this_line_start_pos) = 0;
12484 }
12485
12486 return 1;
12487 }
12488
12489
12490 /* Run window scroll functions, if any, for WINDOW with new window
12491 start STARTP. Sets the window start of WINDOW to that position.
12492
12493 We assume that the window's buffer is really current. */
12494
12495 static INLINE struct text_pos
12496 run_window_scroll_functions (window, startp)
12497 Lisp_Object window;
12498 struct text_pos startp;
12499 {
12500 struct window *w = XWINDOW (window);
12501 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12502
12503 if (current_buffer != XBUFFER (w->buffer))
12504 abort ();
12505
12506 if (!NILP (Vwindow_scroll_functions))
12507 {
12508 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12509 make_number (CHARPOS (startp)));
12510 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12511 /* In case the hook functions switch buffers. */
12512 if (current_buffer != XBUFFER (w->buffer))
12513 set_buffer_internal_1 (XBUFFER (w->buffer));
12514 }
12515
12516 return startp;
12517 }
12518
12519
12520 /* Make sure the line containing the cursor is fully visible.
12521 A value of 1 means there is nothing to be done.
12522 (Either the line is fully visible, or it cannot be made so,
12523 or we cannot tell.)
12524
12525 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12526 is higher than window.
12527
12528 A value of 0 means the caller should do scrolling
12529 as if point had gone off the screen. */
12530
12531 static int
12532 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12533 struct window *w;
12534 int force_p;
12535 int current_matrix_p;
12536 {
12537 struct glyph_matrix *matrix;
12538 struct glyph_row *row;
12539 int window_height;
12540
12541 if (!make_cursor_line_fully_visible_p)
12542 return 1;
12543
12544 /* It's not always possible to find the cursor, e.g, when a window
12545 is full of overlay strings. Don't do anything in that case. */
12546 if (w->cursor.vpos < 0)
12547 return 1;
12548
12549 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12550 row = MATRIX_ROW (matrix, w->cursor.vpos);
12551
12552 /* If the cursor row is not partially visible, there's nothing to do. */
12553 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12554 return 1;
12555
12556 /* If the row the cursor is in is taller than the window's height,
12557 it's not clear what to do, so do nothing. */
12558 window_height = window_box_height (w);
12559 if (row->height >= window_height)
12560 {
12561 if (!force_p || MINI_WINDOW_P (w)
12562 || w->vscroll || w->cursor.vpos == 0)
12563 return 1;
12564 }
12565 return 0;
12566 }
12567
12568
12569 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12570 non-zero means only WINDOW is redisplayed in redisplay_internal.
12571 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12572 in redisplay_window to bring a partially visible line into view in
12573 the case that only the cursor has moved.
12574
12575 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12576 last screen line's vertical height extends past the end of the screen.
12577
12578 Value is
12579
12580 1 if scrolling succeeded
12581
12582 0 if scrolling didn't find point.
12583
12584 -1 if new fonts have been loaded so that we must interrupt
12585 redisplay, adjust glyph matrices, and try again. */
12586
12587 enum
12588 {
12589 SCROLLING_SUCCESS,
12590 SCROLLING_FAILED,
12591 SCROLLING_NEED_LARGER_MATRICES
12592 };
12593
12594 static int
12595 try_scrolling (window, just_this_one_p, scroll_conservatively,
12596 scroll_step, temp_scroll_step, last_line_misfit)
12597 Lisp_Object window;
12598 int just_this_one_p;
12599 EMACS_INT scroll_conservatively, scroll_step;
12600 int temp_scroll_step;
12601 int last_line_misfit;
12602 {
12603 struct window *w = XWINDOW (window);
12604 struct frame *f = XFRAME (w->frame);
12605 struct text_pos pos, startp;
12606 struct it it;
12607 int this_scroll_margin, scroll_max, rc, height;
12608 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12609 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12610 Lisp_Object aggressive;
12611 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12612
12613 #if GLYPH_DEBUG
12614 debug_method_add (w, "try_scrolling");
12615 #endif
12616
12617 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12618
12619 /* Compute scroll margin height in pixels. We scroll when point is
12620 within this distance from the top or bottom of the window. */
12621 if (scroll_margin > 0)
12622 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12623 * FRAME_LINE_HEIGHT (f);
12624 else
12625 this_scroll_margin = 0;
12626
12627 /* Force scroll_conservatively to have a reasonable value, to avoid
12628 overflow while computing how much to scroll. Note that the user
12629 can supply scroll-conservatively equal to `most-positive-fixnum',
12630 which can be larger than INT_MAX. */
12631 if (scroll_conservatively > scroll_limit)
12632 {
12633 scroll_conservatively = scroll_limit;
12634 scroll_max = INT_MAX;
12635 }
12636 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12637 /* Compute how much we should try to scroll maximally to bring
12638 point into view. */
12639 scroll_max = (max (scroll_step,
12640 max (scroll_conservatively, temp_scroll_step))
12641 * FRAME_LINE_HEIGHT (f));
12642 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12643 || NUMBERP (current_buffer->scroll_up_aggressively))
12644 /* We're trying to scroll because of aggressive scrolling but no
12645 scroll_step is set. Choose an arbitrary one. */
12646 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12647 else
12648 scroll_max = 0;
12649
12650 too_near_end:
12651
12652 /* Decide whether to scroll down. */
12653 if (PT > CHARPOS (startp))
12654 {
12655 int scroll_margin_y;
12656
12657 /* Compute the pixel ypos of the scroll margin, then move it to
12658 either that ypos or PT, whichever comes first. */
12659 start_display (&it, w, startp);
12660 scroll_margin_y = it.last_visible_y - this_scroll_margin
12661 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12662 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12663 (MOVE_TO_POS | MOVE_TO_Y));
12664
12665 if (PT > CHARPOS (it.current.pos))
12666 {
12667 int y0 = line_bottom_y (&it);
12668
12669 /* Compute the distance from the scroll margin to PT
12670 (including the height of the cursor line). Moving the
12671 iterator unconditionally to PT can be slow if PT is far
12672 away, so stop 10 lines past the window bottom (is there a
12673 way to do the right thing quickly?). */
12674 move_it_to (&it, PT, -1,
12675 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12676 -1, MOVE_TO_POS | MOVE_TO_Y);
12677 dy = line_bottom_y (&it) - y0;
12678
12679 if (dy > scroll_max)
12680 return SCROLLING_FAILED;
12681
12682 scroll_down_p = 1;
12683 }
12684 }
12685
12686 if (scroll_down_p)
12687 {
12688 /* Point is in or below the bottom scroll margin, so move the
12689 window start down. If scrolling conservatively, move it just
12690 enough down to make point visible. If scroll_step is set,
12691 move it down by scroll_step. */
12692 if (scroll_conservatively)
12693 amount_to_scroll
12694 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12695 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12696 else if (scroll_step || temp_scroll_step)
12697 amount_to_scroll = scroll_max;
12698 else
12699 {
12700 aggressive = current_buffer->scroll_up_aggressively;
12701 height = WINDOW_BOX_TEXT_HEIGHT (w);
12702 if (NUMBERP (aggressive))
12703 {
12704 double float_amount = XFLOATINT (aggressive) * height;
12705 amount_to_scroll = float_amount;
12706 if (amount_to_scroll == 0 && float_amount > 0)
12707 amount_to_scroll = 1;
12708 }
12709 }
12710
12711 if (amount_to_scroll <= 0)
12712 return SCROLLING_FAILED;
12713
12714 start_display (&it, w, startp);
12715 move_it_vertically (&it, amount_to_scroll);
12716
12717 /* If STARTP is unchanged, move it down another screen line. */
12718 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12719 move_it_by_lines (&it, 1, 1);
12720 startp = it.current.pos;
12721 }
12722 else
12723 {
12724 struct text_pos scroll_margin_pos = startp;
12725
12726 /* See if point is inside the scroll margin at the top of the
12727 window. */
12728 if (this_scroll_margin)
12729 {
12730 start_display (&it, w, startp);
12731 move_it_vertically (&it, this_scroll_margin);
12732 scroll_margin_pos = it.current.pos;
12733 }
12734
12735 if (PT < CHARPOS (scroll_margin_pos))
12736 {
12737 /* Point is in the scroll margin at the top of the window or
12738 above what is displayed in the window. */
12739 int y0;
12740
12741 /* Compute the vertical distance from PT to the scroll
12742 margin position. Give up if distance is greater than
12743 scroll_max. */
12744 SET_TEXT_POS (pos, PT, PT_BYTE);
12745 start_display (&it, w, pos);
12746 y0 = it.current_y;
12747 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12748 it.last_visible_y, -1,
12749 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12750 dy = it.current_y - y0;
12751 if (dy > scroll_max)
12752 return SCROLLING_FAILED;
12753
12754 /* Compute new window start. */
12755 start_display (&it, w, startp);
12756
12757 if (scroll_conservatively)
12758 amount_to_scroll
12759 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12760 else if (scroll_step || temp_scroll_step)
12761 amount_to_scroll = scroll_max;
12762 else
12763 {
12764 aggressive = current_buffer->scroll_down_aggressively;
12765 height = WINDOW_BOX_TEXT_HEIGHT (w);
12766 if (NUMBERP (aggressive))
12767 {
12768 double float_amount = XFLOATINT (aggressive) * height;
12769 amount_to_scroll = float_amount;
12770 if (amount_to_scroll == 0 && float_amount > 0)
12771 amount_to_scroll = 1;
12772 }
12773 }
12774
12775 if (amount_to_scroll <= 0)
12776 return SCROLLING_FAILED;
12777
12778 move_it_vertically_backward (&it, amount_to_scroll);
12779 startp = it.current.pos;
12780 }
12781 }
12782
12783 /* Run window scroll functions. */
12784 startp = run_window_scroll_functions (window, startp);
12785
12786 /* Display the window. Give up if new fonts are loaded, or if point
12787 doesn't appear. */
12788 if (!try_window (window, startp, 0))
12789 rc = SCROLLING_NEED_LARGER_MATRICES;
12790 else if (w->cursor.vpos < 0)
12791 {
12792 clear_glyph_matrix (w->desired_matrix);
12793 rc = SCROLLING_FAILED;
12794 }
12795 else
12796 {
12797 /* Maybe forget recorded base line for line number display. */
12798 if (!just_this_one_p
12799 || current_buffer->clip_changed
12800 || BEG_UNCHANGED < CHARPOS (startp))
12801 w->base_line_number = Qnil;
12802
12803 /* If cursor ends up on a partially visible line,
12804 treat that as being off the bottom of the screen. */
12805 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12806 {
12807 clear_glyph_matrix (w->desired_matrix);
12808 ++extra_scroll_margin_lines;
12809 goto too_near_end;
12810 }
12811 rc = SCROLLING_SUCCESS;
12812 }
12813
12814 return rc;
12815 }
12816
12817
12818 /* Compute a suitable window start for window W if display of W starts
12819 on a continuation line. Value is non-zero if a new window start
12820 was computed.
12821
12822 The new window start will be computed, based on W's width, starting
12823 from the start of the continued line. It is the start of the
12824 screen line with the minimum distance from the old start W->start. */
12825
12826 static int
12827 compute_window_start_on_continuation_line (w)
12828 struct window *w;
12829 {
12830 struct text_pos pos, start_pos;
12831 int window_start_changed_p = 0;
12832
12833 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12834
12835 /* If window start is on a continuation line... Window start may be
12836 < BEGV in case there's invisible text at the start of the
12837 buffer (M-x rmail, for example). */
12838 if (CHARPOS (start_pos) > BEGV
12839 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12840 {
12841 struct it it;
12842 struct glyph_row *row;
12843
12844 /* Handle the case that the window start is out of range. */
12845 if (CHARPOS (start_pos) < BEGV)
12846 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12847 else if (CHARPOS (start_pos) > ZV)
12848 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12849
12850 /* Find the start of the continued line. This should be fast
12851 because scan_buffer is fast (newline cache). */
12852 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12853 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12854 row, DEFAULT_FACE_ID);
12855 reseat_at_previous_visible_line_start (&it);
12856
12857 /* If the line start is "too far" away from the window start,
12858 say it takes too much time to compute a new window start. */
12859 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12860 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12861 {
12862 int min_distance, distance;
12863
12864 /* Move forward by display lines to find the new window
12865 start. If window width was enlarged, the new start can
12866 be expected to be > the old start. If window width was
12867 decreased, the new window start will be < the old start.
12868 So, we're looking for the display line start with the
12869 minimum distance from the old window start. */
12870 pos = it.current.pos;
12871 min_distance = INFINITY;
12872 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12873 distance < min_distance)
12874 {
12875 min_distance = distance;
12876 pos = it.current.pos;
12877 move_it_by_lines (&it, 1, 0);
12878 }
12879
12880 /* Set the window start there. */
12881 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12882 window_start_changed_p = 1;
12883 }
12884 }
12885
12886 return window_start_changed_p;
12887 }
12888
12889
12890 /* Try cursor movement in case text has not changed in window WINDOW,
12891 with window start STARTP. Value is
12892
12893 CURSOR_MOVEMENT_SUCCESS if successful
12894
12895 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12896
12897 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12898 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12899 we want to scroll as if scroll-step were set to 1. See the code.
12900
12901 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12902 which case we have to abort this redisplay, and adjust matrices
12903 first. */
12904
12905 enum
12906 {
12907 CURSOR_MOVEMENT_SUCCESS,
12908 CURSOR_MOVEMENT_CANNOT_BE_USED,
12909 CURSOR_MOVEMENT_MUST_SCROLL,
12910 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12911 };
12912
12913 static int
12914 try_cursor_movement (window, startp, scroll_step)
12915 Lisp_Object window;
12916 struct text_pos startp;
12917 int *scroll_step;
12918 {
12919 struct window *w = XWINDOW (window);
12920 struct frame *f = XFRAME (w->frame);
12921 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12922
12923 #if GLYPH_DEBUG
12924 if (inhibit_try_cursor_movement)
12925 return rc;
12926 #endif
12927
12928 /* Handle case where text has not changed, only point, and it has
12929 not moved off the frame. */
12930 if (/* Point may be in this window. */
12931 PT >= CHARPOS (startp)
12932 /* Selective display hasn't changed. */
12933 && !current_buffer->clip_changed
12934 /* Function force-mode-line-update is used to force a thorough
12935 redisplay. It sets either windows_or_buffers_changed or
12936 update_mode_lines. So don't take a shortcut here for these
12937 cases. */
12938 && !update_mode_lines
12939 && !windows_or_buffers_changed
12940 && !cursor_type_changed
12941 /* Can't use this case if highlighting a region. When a
12942 region exists, cursor movement has to do more than just
12943 set the cursor. */
12944 && !(!NILP (Vtransient_mark_mode)
12945 && !NILP (current_buffer->mark_active))
12946 && NILP (w->region_showing)
12947 && NILP (Vshow_trailing_whitespace)
12948 /* Right after splitting windows, last_point may be nil. */
12949 && INTEGERP (w->last_point)
12950 /* This code is not used for mini-buffer for the sake of the case
12951 of redisplaying to replace an echo area message; since in
12952 that case the mini-buffer contents per se are usually
12953 unchanged. This code is of no real use in the mini-buffer
12954 since the handling of this_line_start_pos, etc., in redisplay
12955 handles the same cases. */
12956 && !EQ (window, minibuf_window)
12957 /* When splitting windows or for new windows, it happens that
12958 redisplay is called with a nil window_end_vpos or one being
12959 larger than the window. This should really be fixed in
12960 window.c. I don't have this on my list, now, so we do
12961 approximately the same as the old redisplay code. --gerd. */
12962 && INTEGERP (w->window_end_vpos)
12963 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12964 && (FRAME_WINDOW_P (f)
12965 || !overlay_arrow_in_current_buffer_p ()))
12966 {
12967 int this_scroll_margin, top_scroll_margin;
12968 struct glyph_row *row = NULL;
12969
12970 #if GLYPH_DEBUG
12971 debug_method_add (w, "cursor movement");
12972 #endif
12973
12974 /* Scroll if point within this distance from the top or bottom
12975 of the window. This is a pixel value. */
12976 if (scroll_margin > 0)
12977 {
12978 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12979 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12980 }
12981 else
12982 this_scroll_margin = 0;
12983
12984 top_scroll_margin = this_scroll_margin;
12985 if (WINDOW_WANTS_HEADER_LINE_P (w))
12986 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12987
12988 /* Start with the row the cursor was displayed during the last
12989 not paused redisplay. Give up if that row is not valid. */
12990 if (w->last_cursor.vpos < 0
12991 || w->last_cursor.vpos >= w->current_matrix->nrows)
12992 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12993 else
12994 {
12995 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12996 if (row->mode_line_p)
12997 ++row;
12998 if (!row->enabled_p)
12999 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13000 }
13001
13002 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13003 {
13004 int scroll_p = 0;
13005 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13006
13007 if (PT > XFASTINT (w->last_point))
13008 {
13009 /* Point has moved forward. */
13010 while (MATRIX_ROW_END_CHARPOS (row) < PT
13011 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13012 {
13013 xassert (row->enabled_p);
13014 ++row;
13015 }
13016
13017 /* The end position of a row equals the start position
13018 of the next row. If PT is there, we would rather
13019 display it in the next line. */
13020 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13021 && MATRIX_ROW_END_CHARPOS (row) == PT
13022 && !cursor_row_p (w, row))
13023 ++row;
13024
13025 /* If within the scroll margin, scroll. Note that
13026 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13027 the next line would be drawn, and that
13028 this_scroll_margin can be zero. */
13029 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13030 || PT > MATRIX_ROW_END_CHARPOS (row)
13031 /* Line is completely visible last line in window
13032 and PT is to be set in the next line. */
13033 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13034 && PT == MATRIX_ROW_END_CHARPOS (row)
13035 && !row->ends_at_zv_p
13036 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13037 scroll_p = 1;
13038 }
13039 else if (PT < XFASTINT (w->last_point))
13040 {
13041 /* Cursor has to be moved backward. Note that PT >=
13042 CHARPOS (startp) because of the outer if-statement. */
13043 while (!row->mode_line_p
13044 && (MATRIX_ROW_START_CHARPOS (row) > PT
13045 || (MATRIX_ROW_START_CHARPOS (row) == PT
13046 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13047 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13048 row > w->current_matrix->rows
13049 && (row-1)->ends_in_newline_from_string_p))))
13050 && (row->y > top_scroll_margin
13051 || CHARPOS (startp) == BEGV))
13052 {
13053 xassert (row->enabled_p);
13054 --row;
13055 }
13056
13057 /* Consider the following case: Window starts at BEGV,
13058 there is invisible, intangible text at BEGV, so that
13059 display starts at some point START > BEGV. It can
13060 happen that we are called with PT somewhere between
13061 BEGV and START. Try to handle that case. */
13062 if (row < w->current_matrix->rows
13063 || row->mode_line_p)
13064 {
13065 row = w->current_matrix->rows;
13066 if (row->mode_line_p)
13067 ++row;
13068 }
13069
13070 /* Due to newlines in overlay strings, we may have to
13071 skip forward over overlay strings. */
13072 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13073 && MATRIX_ROW_END_CHARPOS (row) == PT
13074 && !cursor_row_p (w, row))
13075 ++row;
13076
13077 /* If within the scroll margin, scroll. */
13078 if (row->y < top_scroll_margin
13079 && CHARPOS (startp) != BEGV)
13080 scroll_p = 1;
13081 }
13082 else
13083 {
13084 /* Cursor did not move. So don't scroll even if cursor line
13085 is partially visible, as it was so before. */
13086 rc = CURSOR_MOVEMENT_SUCCESS;
13087 }
13088
13089 if (PT < MATRIX_ROW_START_CHARPOS (row)
13090 || PT > MATRIX_ROW_END_CHARPOS (row))
13091 {
13092 /* if PT is not in the glyph row, give up. */
13093 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13094 }
13095 else if (rc != CURSOR_MOVEMENT_SUCCESS
13096 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13097 && make_cursor_line_fully_visible_p)
13098 {
13099 if (PT == MATRIX_ROW_END_CHARPOS (row)
13100 && !row->ends_at_zv_p
13101 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13102 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13103 else if (row->height > window_box_height (w))
13104 {
13105 /* If we end up in a partially visible line, let's
13106 make it fully visible, except when it's taller
13107 than the window, in which case we can't do much
13108 about it. */
13109 *scroll_step = 1;
13110 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13111 }
13112 else
13113 {
13114 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13115 if (!cursor_row_fully_visible_p (w, 0, 1))
13116 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13117 else
13118 rc = CURSOR_MOVEMENT_SUCCESS;
13119 }
13120 }
13121 else if (scroll_p)
13122 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13123 else
13124 {
13125 do
13126 {
13127 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13128 {
13129 rc = CURSOR_MOVEMENT_SUCCESS;
13130 break;
13131 }
13132 ++row;
13133 }
13134 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13135 && MATRIX_ROW_START_CHARPOS (row) == PT
13136 && cursor_row_p (w, row));
13137 }
13138 }
13139 }
13140
13141 return rc;
13142 }
13143
13144 void
13145 set_vertical_scroll_bar (w)
13146 struct window *w;
13147 {
13148 int start, end, whole;
13149
13150 /* Calculate the start and end positions for the current window.
13151 At some point, it would be nice to choose between scrollbars
13152 which reflect the whole buffer size, with special markers
13153 indicating narrowing, and scrollbars which reflect only the
13154 visible region.
13155
13156 Note that mini-buffers sometimes aren't displaying any text. */
13157 if (!MINI_WINDOW_P (w)
13158 || (w == XWINDOW (minibuf_window)
13159 && NILP (echo_area_buffer[0])))
13160 {
13161 struct buffer *buf = XBUFFER (w->buffer);
13162 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13163 start = marker_position (w->start) - BUF_BEGV (buf);
13164 /* I don't think this is guaranteed to be right. For the
13165 moment, we'll pretend it is. */
13166 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13167
13168 if (end < start)
13169 end = start;
13170 if (whole < (end - start))
13171 whole = end - start;
13172 }
13173 else
13174 start = end = whole = 0;
13175
13176 /* Indicate what this scroll bar ought to be displaying now. */
13177 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13178 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13179 (w, end - start, whole, start);
13180 }
13181
13182
13183 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13184 selected_window is redisplayed.
13185
13186 We can return without actually redisplaying the window if
13187 fonts_changed_p is nonzero. In that case, redisplay_internal will
13188 retry. */
13189
13190 static void
13191 redisplay_window (window, just_this_one_p)
13192 Lisp_Object window;
13193 int just_this_one_p;
13194 {
13195 struct window *w = XWINDOW (window);
13196 struct frame *f = XFRAME (w->frame);
13197 struct buffer *buffer = XBUFFER (w->buffer);
13198 struct buffer *old = current_buffer;
13199 struct text_pos lpoint, opoint, startp;
13200 int update_mode_line;
13201 int tem;
13202 struct it it;
13203 /* Record it now because it's overwritten. */
13204 int current_matrix_up_to_date_p = 0;
13205 int used_current_matrix_p = 0;
13206 /* This is less strict than current_matrix_up_to_date_p.
13207 It indictes that the buffer contents and narrowing are unchanged. */
13208 int buffer_unchanged_p = 0;
13209 int temp_scroll_step = 0;
13210 int count = SPECPDL_INDEX ();
13211 int rc;
13212 int centering_position = -1;
13213 int last_line_misfit = 0;
13214 int beg_unchanged, end_unchanged;
13215
13216 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13217 opoint = lpoint;
13218
13219 /* W must be a leaf window here. */
13220 xassert (!NILP (w->buffer));
13221 #if GLYPH_DEBUG
13222 *w->desired_matrix->method = 0;
13223 #endif
13224
13225 restart:
13226 reconsider_clip_changes (w, buffer);
13227
13228 /* Has the mode line to be updated? */
13229 update_mode_line = (!NILP (w->update_mode_line)
13230 || update_mode_lines
13231 || buffer->clip_changed
13232 || buffer->prevent_redisplay_optimizations_p);
13233
13234 if (MINI_WINDOW_P (w))
13235 {
13236 if (w == XWINDOW (echo_area_window)
13237 && !NILP (echo_area_buffer[0]))
13238 {
13239 if (update_mode_line)
13240 /* We may have to update a tty frame's menu bar or a
13241 tool-bar. Example `M-x C-h C-h C-g'. */
13242 goto finish_menu_bars;
13243 else
13244 /* We've already displayed the echo area glyphs in this window. */
13245 goto finish_scroll_bars;
13246 }
13247 else if ((w != XWINDOW (minibuf_window)
13248 || minibuf_level == 0)
13249 /* When buffer is nonempty, redisplay window normally. */
13250 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13251 /* Quail displays non-mini buffers in minibuffer window.
13252 In that case, redisplay the window normally. */
13253 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13254 {
13255 /* W is a mini-buffer window, but it's not active, so clear
13256 it. */
13257 int yb = window_text_bottom_y (w);
13258 struct glyph_row *row;
13259 int y;
13260
13261 for (y = 0, row = w->desired_matrix->rows;
13262 y < yb;
13263 y += row->height, ++row)
13264 blank_row (w, row, y);
13265 goto finish_scroll_bars;
13266 }
13267
13268 clear_glyph_matrix (w->desired_matrix);
13269 }
13270
13271 /* Otherwise set up data on this window; select its buffer and point
13272 value. */
13273 /* Really select the buffer, for the sake of buffer-local
13274 variables. */
13275 set_buffer_internal_1 (XBUFFER (w->buffer));
13276
13277 current_matrix_up_to_date_p
13278 = (!NILP (w->window_end_valid)
13279 && !current_buffer->clip_changed
13280 && !current_buffer->prevent_redisplay_optimizations_p
13281 && XFASTINT (w->last_modified) >= MODIFF
13282 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13283
13284 /* Run the window-bottom-change-functions
13285 if it is possible that the text on the screen has changed
13286 (either due to modification of the text, or any other reason). */
13287 if (!current_matrix_up_to_date_p
13288 && !NILP (Vwindow_text_change_functions))
13289 {
13290 safe_run_hooks (Qwindow_text_change_functions);
13291 goto restart;
13292 }
13293
13294 beg_unchanged = BEG_UNCHANGED;
13295 end_unchanged = END_UNCHANGED;
13296
13297 SET_TEXT_POS (opoint, PT, PT_BYTE);
13298
13299 specbind (Qinhibit_point_motion_hooks, Qt);
13300
13301 buffer_unchanged_p
13302 = (!NILP (w->window_end_valid)
13303 && !current_buffer->clip_changed
13304 && XFASTINT (w->last_modified) >= MODIFF
13305 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13306
13307 /* When windows_or_buffers_changed is non-zero, we can't rely on
13308 the window end being valid, so set it to nil there. */
13309 if (windows_or_buffers_changed)
13310 {
13311 /* If window starts on a continuation line, maybe adjust the
13312 window start in case the window's width changed. */
13313 if (XMARKER (w->start)->buffer == current_buffer)
13314 compute_window_start_on_continuation_line (w);
13315
13316 w->window_end_valid = Qnil;
13317 }
13318
13319 /* Some sanity checks. */
13320 CHECK_WINDOW_END (w);
13321 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13322 abort ();
13323 if (BYTEPOS (opoint) < CHARPOS (opoint))
13324 abort ();
13325
13326 /* If %c is in mode line, update it if needed. */
13327 if (!NILP (w->column_number_displayed)
13328 /* This alternative quickly identifies a common case
13329 where no change is needed. */
13330 && !(PT == XFASTINT (w->last_point)
13331 && XFASTINT (w->last_modified) >= MODIFF
13332 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13333 && (XFASTINT (w->column_number_displayed)
13334 != (int) current_column ())) /* iftc */
13335 update_mode_line = 1;
13336
13337 /* Count number of windows showing the selected buffer. An indirect
13338 buffer counts as its base buffer. */
13339 if (!just_this_one_p)
13340 {
13341 struct buffer *current_base, *window_base;
13342 current_base = current_buffer;
13343 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13344 if (current_base->base_buffer)
13345 current_base = current_base->base_buffer;
13346 if (window_base->base_buffer)
13347 window_base = window_base->base_buffer;
13348 if (current_base == window_base)
13349 buffer_shared++;
13350 }
13351
13352 /* Point refers normally to the selected window. For any other
13353 window, set up appropriate value. */
13354 if (!EQ (window, selected_window))
13355 {
13356 int new_pt = XMARKER (w->pointm)->charpos;
13357 int new_pt_byte = marker_byte_position (w->pointm);
13358 if (new_pt < BEGV)
13359 {
13360 new_pt = BEGV;
13361 new_pt_byte = BEGV_BYTE;
13362 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13363 }
13364 else if (new_pt > (ZV - 1))
13365 {
13366 new_pt = ZV;
13367 new_pt_byte = ZV_BYTE;
13368 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13369 }
13370
13371 /* We don't use SET_PT so that the point-motion hooks don't run. */
13372 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13373 }
13374
13375 /* If any of the character widths specified in the display table
13376 have changed, invalidate the width run cache. It's true that
13377 this may be a bit late to catch such changes, but the rest of
13378 redisplay goes (non-fatally) haywire when the display table is
13379 changed, so why should we worry about doing any better? */
13380 if (current_buffer->width_run_cache)
13381 {
13382 struct Lisp_Char_Table *disptab = buffer_display_table ();
13383
13384 if (! disptab_matches_widthtab (disptab,
13385 XVECTOR (current_buffer->width_table)))
13386 {
13387 invalidate_region_cache (current_buffer,
13388 current_buffer->width_run_cache,
13389 BEG, Z);
13390 recompute_width_table (current_buffer, disptab);
13391 }
13392 }
13393
13394 /* If window-start is screwed up, choose a new one. */
13395 if (XMARKER (w->start)->buffer != current_buffer)
13396 goto recenter;
13397
13398 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13399
13400 /* If someone specified a new starting point but did not insist,
13401 check whether it can be used. */
13402 if (!NILP (w->optional_new_start)
13403 && CHARPOS (startp) >= BEGV
13404 && CHARPOS (startp) <= ZV)
13405 {
13406 w->optional_new_start = Qnil;
13407 start_display (&it, w, startp);
13408 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13409 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13410 if (IT_CHARPOS (it) == PT)
13411 w->force_start = Qt;
13412 /* IT may overshoot PT if text at PT is invisible. */
13413 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13414 w->force_start = Qt;
13415 }
13416
13417 force_start:
13418
13419 /* Handle case where place to start displaying has been specified,
13420 unless the specified location is outside the accessible range. */
13421 if (!NILP (w->force_start)
13422 || w->frozen_window_start_p)
13423 {
13424 /* We set this later on if we have to adjust point. */
13425 int new_vpos = -1;
13426
13427 w->force_start = Qnil;
13428 w->vscroll = 0;
13429 w->window_end_valid = Qnil;
13430
13431 /* Forget any recorded base line for line number display. */
13432 if (!buffer_unchanged_p)
13433 w->base_line_number = Qnil;
13434
13435 /* Redisplay the mode line. Select the buffer properly for that.
13436 Also, run the hook window-scroll-functions
13437 because we have scrolled. */
13438 /* Note, we do this after clearing force_start because
13439 if there's an error, it is better to forget about force_start
13440 than to get into an infinite loop calling the hook functions
13441 and having them get more errors. */
13442 if (!update_mode_line
13443 || ! NILP (Vwindow_scroll_functions))
13444 {
13445 update_mode_line = 1;
13446 w->update_mode_line = Qt;
13447 startp = run_window_scroll_functions (window, startp);
13448 }
13449
13450 w->last_modified = make_number (0);
13451 w->last_overlay_modified = make_number (0);
13452 if (CHARPOS (startp) < BEGV)
13453 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13454 else if (CHARPOS (startp) > ZV)
13455 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13456
13457 /* Redisplay, then check if cursor has been set during the
13458 redisplay. Give up if new fonts were loaded. */
13459 /* We used to issue a CHECK_MARGINS argument to try_window here,
13460 but this causes scrolling to fail when point begins inside
13461 the scroll margin (bug#148) -- cyd */
13462 if (!try_window (window, startp, 0))
13463 {
13464 w->force_start = Qt;
13465 clear_glyph_matrix (w->desired_matrix);
13466 goto need_larger_matrices;
13467 }
13468
13469 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13470 {
13471 /* If point does not appear, try to move point so it does
13472 appear. The desired matrix has been built above, so we
13473 can use it here. */
13474 new_vpos = window_box_height (w) / 2;
13475 }
13476
13477 if (!cursor_row_fully_visible_p (w, 0, 0))
13478 {
13479 /* Point does appear, but on a line partly visible at end of window.
13480 Move it back to a fully-visible line. */
13481 new_vpos = window_box_height (w);
13482 }
13483
13484 /* If we need to move point for either of the above reasons,
13485 now actually do it. */
13486 if (new_vpos >= 0)
13487 {
13488 struct glyph_row *row;
13489
13490 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13491 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13492 ++row;
13493
13494 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13495 MATRIX_ROW_START_BYTEPOS (row));
13496
13497 if (w != XWINDOW (selected_window))
13498 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13499 else if (current_buffer == old)
13500 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13501
13502 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13503
13504 /* If we are highlighting the region, then we just changed
13505 the region, so redisplay to show it. */
13506 if (!NILP (Vtransient_mark_mode)
13507 && !NILP (current_buffer->mark_active))
13508 {
13509 clear_glyph_matrix (w->desired_matrix);
13510 if (!try_window (window, startp, 0))
13511 goto need_larger_matrices;
13512 }
13513 }
13514
13515 #if GLYPH_DEBUG
13516 debug_method_add (w, "forced window start");
13517 #endif
13518 goto done;
13519 }
13520
13521 /* Handle case where text has not changed, only point, and it has
13522 not moved off the frame, and we are not retrying after hscroll.
13523 (current_matrix_up_to_date_p is nonzero when retrying.) */
13524 if (current_matrix_up_to_date_p
13525 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13526 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13527 {
13528 switch (rc)
13529 {
13530 case CURSOR_MOVEMENT_SUCCESS:
13531 used_current_matrix_p = 1;
13532 goto done;
13533
13534 case CURSOR_MOVEMENT_MUST_SCROLL:
13535 goto try_to_scroll;
13536
13537 default:
13538 abort ();
13539 }
13540 }
13541 /* If current starting point was originally the beginning of a line
13542 but no longer is, find a new starting point. */
13543 else if (!NILP (w->start_at_line_beg)
13544 && !(CHARPOS (startp) <= BEGV
13545 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13546 {
13547 #if GLYPH_DEBUG
13548 debug_method_add (w, "recenter 1");
13549 #endif
13550 goto recenter;
13551 }
13552
13553 /* Try scrolling with try_window_id. Value is > 0 if update has
13554 been done, it is -1 if we know that the same window start will
13555 not work. It is 0 if unsuccessful for some other reason. */
13556 else if ((tem = try_window_id (w)) != 0)
13557 {
13558 #if GLYPH_DEBUG
13559 debug_method_add (w, "try_window_id %d", tem);
13560 #endif
13561
13562 if (fonts_changed_p)
13563 goto need_larger_matrices;
13564 if (tem > 0)
13565 goto done;
13566
13567 /* Otherwise try_window_id has returned -1 which means that we
13568 don't want the alternative below this comment to execute. */
13569 }
13570 else if (CHARPOS (startp) >= BEGV
13571 && CHARPOS (startp) <= ZV
13572 && PT >= CHARPOS (startp)
13573 && (CHARPOS (startp) < ZV
13574 /* Avoid starting at end of buffer. */
13575 || CHARPOS (startp) == BEGV
13576 || (XFASTINT (w->last_modified) >= MODIFF
13577 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13578 {
13579
13580 /* If first window line is a continuation line, and window start
13581 is inside the modified region, but the first change is before
13582 current window start, we must select a new window start.
13583
13584 However, if this is the result of a down-mouse event (e.g. by
13585 extending the mouse-drag-overlay), we don't want to select a
13586 new window start, since that would change the position under
13587 the mouse, resulting in an unwanted mouse-movement rather
13588 than a simple mouse-click. */
13589 if (NILP (w->start_at_line_beg)
13590 && NILP (do_mouse_tracking)
13591 && CHARPOS (startp) > BEGV
13592 && CHARPOS (startp) > BEG + beg_unchanged
13593 && CHARPOS (startp) <= Z - end_unchanged
13594 /* Even if w->start_at_line_beg is nil, a new window may
13595 start at a line_beg, since that's how set_buffer_window
13596 sets it. So, we need to check the return value of
13597 compute_window_start_on_continuation_line. (See also
13598 bug#197). */
13599 && XMARKER (w->start)->buffer == current_buffer
13600 && compute_window_start_on_continuation_line (w))
13601 {
13602 w->force_start = Qt;
13603 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13604 goto force_start;
13605 }
13606
13607 #if GLYPH_DEBUG
13608 debug_method_add (w, "same window start");
13609 #endif
13610
13611 /* Try to redisplay starting at same place as before.
13612 If point has not moved off frame, accept the results. */
13613 if (!current_matrix_up_to_date_p
13614 /* Don't use try_window_reusing_current_matrix in this case
13615 because a window scroll function can have changed the
13616 buffer. */
13617 || !NILP (Vwindow_scroll_functions)
13618 || MINI_WINDOW_P (w)
13619 || !(used_current_matrix_p
13620 = try_window_reusing_current_matrix (w)))
13621 {
13622 IF_DEBUG (debug_method_add (w, "1"));
13623 if (try_window (window, startp, 1) < 0)
13624 /* -1 means we need to scroll.
13625 0 means we need new matrices, but fonts_changed_p
13626 is set in that case, so we will detect it below. */
13627 goto try_to_scroll;
13628 }
13629
13630 if (fonts_changed_p)
13631 goto need_larger_matrices;
13632
13633 if (w->cursor.vpos >= 0)
13634 {
13635 if (!just_this_one_p
13636 || current_buffer->clip_changed
13637 || BEG_UNCHANGED < CHARPOS (startp))
13638 /* Forget any recorded base line for line number display. */
13639 w->base_line_number = Qnil;
13640
13641 if (!cursor_row_fully_visible_p (w, 1, 0))
13642 {
13643 clear_glyph_matrix (w->desired_matrix);
13644 last_line_misfit = 1;
13645 }
13646 /* Drop through and scroll. */
13647 else
13648 goto done;
13649 }
13650 else
13651 clear_glyph_matrix (w->desired_matrix);
13652 }
13653
13654 try_to_scroll:
13655
13656 w->last_modified = make_number (0);
13657 w->last_overlay_modified = make_number (0);
13658
13659 /* Redisplay the mode line. Select the buffer properly for that. */
13660 if (!update_mode_line)
13661 {
13662 update_mode_line = 1;
13663 w->update_mode_line = Qt;
13664 }
13665
13666 /* Try to scroll by specified few lines. */
13667 if ((scroll_conservatively
13668 || scroll_step
13669 || temp_scroll_step
13670 || NUMBERP (current_buffer->scroll_up_aggressively)
13671 || NUMBERP (current_buffer->scroll_down_aggressively))
13672 && !current_buffer->clip_changed
13673 && CHARPOS (startp) >= BEGV
13674 && CHARPOS (startp) <= ZV)
13675 {
13676 /* The function returns -1 if new fonts were loaded, 1 if
13677 successful, 0 if not successful. */
13678 int rc = try_scrolling (window, just_this_one_p,
13679 scroll_conservatively,
13680 scroll_step,
13681 temp_scroll_step, last_line_misfit);
13682 switch (rc)
13683 {
13684 case SCROLLING_SUCCESS:
13685 goto done;
13686
13687 case SCROLLING_NEED_LARGER_MATRICES:
13688 goto need_larger_matrices;
13689
13690 case SCROLLING_FAILED:
13691 break;
13692
13693 default:
13694 abort ();
13695 }
13696 }
13697
13698 /* Finally, just choose place to start which centers point */
13699
13700 recenter:
13701 if (centering_position < 0)
13702 centering_position = window_box_height (w) / 2;
13703
13704 #if GLYPH_DEBUG
13705 debug_method_add (w, "recenter");
13706 #endif
13707
13708 /* w->vscroll = 0; */
13709
13710 /* Forget any previously recorded base line for line number display. */
13711 if (!buffer_unchanged_p)
13712 w->base_line_number = Qnil;
13713
13714 /* Move backward half the height of the window. */
13715 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13716 it.current_y = it.last_visible_y;
13717 move_it_vertically_backward (&it, centering_position);
13718 xassert (IT_CHARPOS (it) >= BEGV);
13719
13720 /* The function move_it_vertically_backward may move over more
13721 than the specified y-distance. If it->w is small, e.g. a
13722 mini-buffer window, we may end up in front of the window's
13723 display area. Start displaying at the start of the line
13724 containing PT in this case. */
13725 if (it.current_y <= 0)
13726 {
13727 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13728 move_it_vertically_backward (&it, 0);
13729 it.current_y = 0;
13730 }
13731
13732 it.current_x = it.hpos = 0;
13733
13734 /* Set startp here explicitly in case that helps avoid an infinite loop
13735 in case the window-scroll-functions functions get errors. */
13736 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13737
13738 /* Run scroll hooks. */
13739 startp = run_window_scroll_functions (window, it.current.pos);
13740
13741 /* Redisplay the window. */
13742 if (!current_matrix_up_to_date_p
13743 || windows_or_buffers_changed
13744 || cursor_type_changed
13745 /* Don't use try_window_reusing_current_matrix in this case
13746 because it can have changed the buffer. */
13747 || !NILP (Vwindow_scroll_functions)
13748 || !just_this_one_p
13749 || MINI_WINDOW_P (w)
13750 || !(used_current_matrix_p
13751 = try_window_reusing_current_matrix (w)))
13752 try_window (window, startp, 0);
13753
13754 /* If new fonts have been loaded (due to fontsets), give up. We
13755 have to start a new redisplay since we need to re-adjust glyph
13756 matrices. */
13757 if (fonts_changed_p)
13758 goto need_larger_matrices;
13759
13760 /* If cursor did not appear assume that the middle of the window is
13761 in the first line of the window. Do it again with the next line.
13762 (Imagine a window of height 100, displaying two lines of height
13763 60. Moving back 50 from it->last_visible_y will end in the first
13764 line.) */
13765 if (w->cursor.vpos < 0)
13766 {
13767 if (!NILP (w->window_end_valid)
13768 && PT >= Z - XFASTINT (w->window_end_pos))
13769 {
13770 clear_glyph_matrix (w->desired_matrix);
13771 move_it_by_lines (&it, 1, 0);
13772 try_window (window, it.current.pos, 0);
13773 }
13774 else if (PT < IT_CHARPOS (it))
13775 {
13776 clear_glyph_matrix (w->desired_matrix);
13777 move_it_by_lines (&it, -1, 0);
13778 try_window (window, it.current.pos, 0);
13779 }
13780 else
13781 {
13782 /* Not much we can do about it. */
13783 }
13784 }
13785
13786 /* Consider the following case: Window starts at BEGV, there is
13787 invisible, intangible text at BEGV, so that display starts at
13788 some point START > BEGV. It can happen that we are called with
13789 PT somewhere between BEGV and START. Try to handle that case. */
13790 if (w->cursor.vpos < 0)
13791 {
13792 struct glyph_row *row = w->current_matrix->rows;
13793 if (row->mode_line_p)
13794 ++row;
13795 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13796 }
13797
13798 if (!cursor_row_fully_visible_p (w, 0, 0))
13799 {
13800 /* If vscroll is enabled, disable it and try again. */
13801 if (w->vscroll)
13802 {
13803 w->vscroll = 0;
13804 clear_glyph_matrix (w->desired_matrix);
13805 goto recenter;
13806 }
13807
13808 /* If centering point failed to make the whole line visible,
13809 put point at the top instead. That has to make the whole line
13810 visible, if it can be done. */
13811 if (centering_position == 0)
13812 goto done;
13813
13814 clear_glyph_matrix (w->desired_matrix);
13815 centering_position = 0;
13816 goto recenter;
13817 }
13818
13819 done:
13820
13821 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13822 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13823 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13824 ? Qt : Qnil);
13825
13826 /* Display the mode line, if we must. */
13827 if ((update_mode_line
13828 /* If window not full width, must redo its mode line
13829 if (a) the window to its side is being redone and
13830 (b) we do a frame-based redisplay. This is a consequence
13831 of how inverted lines are drawn in frame-based redisplay. */
13832 || (!just_this_one_p
13833 && !FRAME_WINDOW_P (f)
13834 && !WINDOW_FULL_WIDTH_P (w))
13835 /* Line number to display. */
13836 || INTEGERP (w->base_line_pos)
13837 /* Column number is displayed and different from the one displayed. */
13838 || (!NILP (w->column_number_displayed)
13839 && (XFASTINT (w->column_number_displayed)
13840 != (int) current_column ()))) /* iftc */
13841 /* This means that the window has a mode line. */
13842 && (WINDOW_WANTS_MODELINE_P (w)
13843 || WINDOW_WANTS_HEADER_LINE_P (w)))
13844 {
13845 display_mode_lines (w);
13846
13847 /* If mode line height has changed, arrange for a thorough
13848 immediate redisplay using the correct mode line height. */
13849 if (WINDOW_WANTS_MODELINE_P (w)
13850 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13851 {
13852 fonts_changed_p = 1;
13853 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13854 = DESIRED_MODE_LINE_HEIGHT (w);
13855 }
13856
13857 /* If top line height has changed, arrange for a thorough
13858 immediate redisplay using the correct mode line height. */
13859 if (WINDOW_WANTS_HEADER_LINE_P (w)
13860 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13861 {
13862 fonts_changed_p = 1;
13863 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13864 = DESIRED_HEADER_LINE_HEIGHT (w);
13865 }
13866
13867 if (fonts_changed_p)
13868 goto need_larger_matrices;
13869 }
13870
13871 if (!line_number_displayed
13872 && !BUFFERP (w->base_line_pos))
13873 {
13874 w->base_line_pos = Qnil;
13875 w->base_line_number = Qnil;
13876 }
13877
13878 finish_menu_bars:
13879
13880 /* When we reach a frame's selected window, redo the frame's menu bar. */
13881 if (update_mode_line
13882 && EQ (FRAME_SELECTED_WINDOW (f), window))
13883 {
13884 int redisplay_menu_p = 0;
13885 int redisplay_tool_bar_p = 0;
13886
13887 if (FRAME_WINDOW_P (f))
13888 {
13889 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13890 || defined (HAVE_NS) || defined (USE_GTK)
13891 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13892 #else
13893 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13894 #endif
13895 }
13896 else
13897 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13898
13899 if (redisplay_menu_p)
13900 display_menu_bar (w);
13901
13902 #ifdef HAVE_WINDOW_SYSTEM
13903 if (FRAME_WINDOW_P (f))
13904 {
13905 #if defined (USE_GTK) || defined (HAVE_NS)
13906 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13907 #else
13908 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13909 && (FRAME_TOOL_BAR_LINES (f) > 0
13910 || !NILP (Vauto_resize_tool_bars));
13911 #endif
13912
13913 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13914 {
13915 extern int ignore_mouse_drag_p;
13916 ignore_mouse_drag_p = 1;
13917 }
13918 }
13919 #endif
13920 }
13921
13922 #ifdef HAVE_WINDOW_SYSTEM
13923 if (FRAME_WINDOW_P (f)
13924 && update_window_fringes (w, (just_this_one_p
13925 || (!used_current_matrix_p && !overlay_arrow_seen)
13926 || w->pseudo_window_p)))
13927 {
13928 update_begin (f);
13929 BLOCK_INPUT;
13930 if (draw_window_fringes (w, 1))
13931 x_draw_vertical_border (w);
13932 UNBLOCK_INPUT;
13933 update_end (f);
13934 }
13935 #endif /* HAVE_WINDOW_SYSTEM */
13936
13937 /* We go to this label, with fonts_changed_p nonzero,
13938 if it is necessary to try again using larger glyph matrices.
13939 We have to redeem the scroll bar even in this case,
13940 because the loop in redisplay_internal expects that. */
13941 need_larger_matrices:
13942 ;
13943 finish_scroll_bars:
13944
13945 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13946 {
13947 /* Set the thumb's position and size. */
13948 set_vertical_scroll_bar (w);
13949
13950 /* Note that we actually used the scroll bar attached to this
13951 window, so it shouldn't be deleted at the end of redisplay. */
13952 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13953 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13954 }
13955
13956 /* Restore current_buffer and value of point in it. */
13957 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13958 set_buffer_internal_1 (old);
13959 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13960 shorter. This can be caused by log truncation in *Messages*. */
13961 if (CHARPOS (lpoint) <= ZV)
13962 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13963
13964 unbind_to (count, Qnil);
13965 }
13966
13967
13968 /* Build the complete desired matrix of WINDOW with a window start
13969 buffer position POS.
13970
13971 Value is 1 if successful. It is zero if fonts were loaded during
13972 redisplay which makes re-adjusting glyph matrices necessary, and -1
13973 if point would appear in the scroll margins.
13974 (We check that only if CHECK_MARGINS is nonzero. */
13975
13976 int
13977 try_window (window, pos, check_margins)
13978 Lisp_Object window;
13979 struct text_pos pos;
13980 int check_margins;
13981 {
13982 struct window *w = XWINDOW (window);
13983 struct it it;
13984 struct glyph_row *last_text_row = NULL;
13985 struct frame *f = XFRAME (w->frame);
13986
13987 /* Make POS the new window start. */
13988 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13989
13990 /* Mark cursor position as unknown. No overlay arrow seen. */
13991 w->cursor.vpos = -1;
13992 overlay_arrow_seen = 0;
13993
13994 /* Initialize iterator and info to start at POS. */
13995 start_display (&it, w, pos);
13996
13997 /* Display all lines of W. */
13998 while (it.current_y < it.last_visible_y)
13999 {
14000 if (display_line (&it))
14001 last_text_row = it.glyph_row - 1;
14002 if (fonts_changed_p)
14003 return 0;
14004 }
14005
14006 /* Don't let the cursor end in the scroll margins. */
14007 if (check_margins
14008 && !MINI_WINDOW_P (w))
14009 {
14010 int this_scroll_margin;
14011
14012 if (scroll_margin > 0)
14013 {
14014 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14015 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14016 }
14017 else
14018 this_scroll_margin = 0;
14019
14020 if ((w->cursor.y >= 0 /* not vscrolled */
14021 && w->cursor.y < this_scroll_margin
14022 && CHARPOS (pos) > BEGV
14023 && IT_CHARPOS (it) < ZV)
14024 /* rms: considering make_cursor_line_fully_visible_p here
14025 seems to give wrong results. We don't want to recenter
14026 when the last line is partly visible, we want to allow
14027 that case to be handled in the usual way. */
14028 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14029 {
14030 w->cursor.vpos = -1;
14031 clear_glyph_matrix (w->desired_matrix);
14032 return -1;
14033 }
14034 }
14035
14036 /* If bottom moved off end of frame, change mode line percentage. */
14037 if (XFASTINT (w->window_end_pos) <= 0
14038 && Z != IT_CHARPOS (it))
14039 w->update_mode_line = Qt;
14040
14041 /* Set window_end_pos to the offset of the last character displayed
14042 on the window from the end of current_buffer. Set
14043 window_end_vpos to its row number. */
14044 if (last_text_row)
14045 {
14046 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14047 w->window_end_bytepos
14048 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14049 w->window_end_pos
14050 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14051 w->window_end_vpos
14052 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14053 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14054 ->displays_text_p);
14055 }
14056 else
14057 {
14058 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14059 w->window_end_pos = make_number (Z - ZV);
14060 w->window_end_vpos = make_number (0);
14061 }
14062
14063 /* But that is not valid info until redisplay finishes. */
14064 w->window_end_valid = Qnil;
14065 return 1;
14066 }
14067
14068
14069 \f
14070 /************************************************************************
14071 Window redisplay reusing current matrix when buffer has not changed
14072 ************************************************************************/
14073
14074 /* Try redisplay of window W showing an unchanged buffer with a
14075 different window start than the last time it was displayed by
14076 reusing its current matrix. Value is non-zero if successful.
14077 W->start is the new window start. */
14078
14079 static int
14080 try_window_reusing_current_matrix (w)
14081 struct window *w;
14082 {
14083 struct frame *f = XFRAME (w->frame);
14084 struct glyph_row *row, *bottom_row;
14085 struct it it;
14086 struct run run;
14087 struct text_pos start, new_start;
14088 int nrows_scrolled, i;
14089 struct glyph_row *last_text_row;
14090 struct glyph_row *last_reused_text_row;
14091 struct glyph_row *start_row;
14092 int start_vpos, min_y, max_y;
14093
14094 #if GLYPH_DEBUG
14095 if (inhibit_try_window_reusing)
14096 return 0;
14097 #endif
14098
14099 if (/* This function doesn't handle terminal frames. */
14100 !FRAME_WINDOW_P (f)
14101 /* Don't try to reuse the display if windows have been split
14102 or such. */
14103 || windows_or_buffers_changed
14104 || cursor_type_changed)
14105 return 0;
14106
14107 /* Can't do this if region may have changed. */
14108 if ((!NILP (Vtransient_mark_mode)
14109 && !NILP (current_buffer->mark_active))
14110 || !NILP (w->region_showing)
14111 || !NILP (Vshow_trailing_whitespace))
14112 return 0;
14113
14114 /* If top-line visibility has changed, give up. */
14115 if (WINDOW_WANTS_HEADER_LINE_P (w)
14116 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14117 return 0;
14118
14119 /* Give up if old or new display is scrolled vertically. We could
14120 make this function handle this, but right now it doesn't. */
14121 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14122 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14123 return 0;
14124
14125 /* The variable new_start now holds the new window start. The old
14126 start `start' can be determined from the current matrix. */
14127 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14128 start = start_row->start.pos;
14129 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14130
14131 /* Clear the desired matrix for the display below. */
14132 clear_glyph_matrix (w->desired_matrix);
14133
14134 if (CHARPOS (new_start) <= CHARPOS (start))
14135 {
14136 int first_row_y;
14137
14138 /* Don't use this method if the display starts with an ellipsis
14139 displayed for invisible text. It's not easy to handle that case
14140 below, and it's certainly not worth the effort since this is
14141 not a frequent case. */
14142 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14143 return 0;
14144
14145 IF_DEBUG (debug_method_add (w, "twu1"));
14146
14147 /* Display up to a row that can be reused. The variable
14148 last_text_row is set to the last row displayed that displays
14149 text. Note that it.vpos == 0 if or if not there is a
14150 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14151 start_display (&it, w, new_start);
14152 first_row_y = it.current_y;
14153 w->cursor.vpos = -1;
14154 last_text_row = last_reused_text_row = NULL;
14155
14156 while (it.current_y < it.last_visible_y
14157 && !fonts_changed_p)
14158 {
14159 /* If we have reached into the characters in the START row,
14160 that means the line boundaries have changed. So we
14161 can't start copying with the row START. Maybe it will
14162 work to start copying with the following row. */
14163 while (IT_CHARPOS (it) > CHARPOS (start))
14164 {
14165 /* Advance to the next row as the "start". */
14166 start_row++;
14167 start = start_row->start.pos;
14168 /* If there are no more rows to try, or just one, give up. */
14169 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14170 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14171 || CHARPOS (start) == ZV)
14172 {
14173 clear_glyph_matrix (w->desired_matrix);
14174 return 0;
14175 }
14176
14177 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14178 }
14179 /* If we have reached alignment,
14180 we can copy the rest of the rows. */
14181 if (IT_CHARPOS (it) == CHARPOS (start))
14182 break;
14183
14184 if (display_line (&it))
14185 last_text_row = it.glyph_row - 1;
14186 }
14187
14188 /* A value of current_y < last_visible_y means that we stopped
14189 at the previous window start, which in turn means that we
14190 have at least one reusable row. */
14191 if (it.current_y < it.last_visible_y)
14192 {
14193 /* IT.vpos always starts from 0; it counts text lines. */
14194 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14195
14196 /* Find PT if not already found in the lines displayed. */
14197 if (w->cursor.vpos < 0)
14198 {
14199 int dy = it.current_y - start_row->y;
14200
14201 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14202 row = row_containing_pos (w, PT, row, NULL, dy);
14203 if (row)
14204 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14205 dy, nrows_scrolled);
14206 else
14207 {
14208 clear_glyph_matrix (w->desired_matrix);
14209 return 0;
14210 }
14211 }
14212
14213 /* Scroll the display. Do it before the current matrix is
14214 changed. The problem here is that update has not yet
14215 run, i.e. part of the current matrix is not up to date.
14216 scroll_run_hook will clear the cursor, and use the
14217 current matrix to get the height of the row the cursor is
14218 in. */
14219 run.current_y = start_row->y;
14220 run.desired_y = it.current_y;
14221 run.height = it.last_visible_y - it.current_y;
14222
14223 if (run.height > 0 && run.current_y != run.desired_y)
14224 {
14225 update_begin (f);
14226 FRAME_RIF (f)->update_window_begin_hook (w);
14227 FRAME_RIF (f)->clear_window_mouse_face (w);
14228 FRAME_RIF (f)->scroll_run_hook (w, &run);
14229 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14230 update_end (f);
14231 }
14232
14233 /* Shift current matrix down by nrows_scrolled lines. */
14234 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14235 rotate_matrix (w->current_matrix,
14236 start_vpos,
14237 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14238 nrows_scrolled);
14239
14240 /* Disable lines that must be updated. */
14241 for (i = 0; i < nrows_scrolled; ++i)
14242 (start_row + i)->enabled_p = 0;
14243
14244 /* Re-compute Y positions. */
14245 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14246 max_y = it.last_visible_y;
14247 for (row = start_row + nrows_scrolled;
14248 row < bottom_row;
14249 ++row)
14250 {
14251 row->y = it.current_y;
14252 row->visible_height = row->height;
14253
14254 if (row->y < min_y)
14255 row->visible_height -= min_y - row->y;
14256 if (row->y + row->height > max_y)
14257 row->visible_height -= row->y + row->height - max_y;
14258 row->redraw_fringe_bitmaps_p = 1;
14259
14260 it.current_y += row->height;
14261
14262 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14263 last_reused_text_row = row;
14264 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14265 break;
14266 }
14267
14268 /* Disable lines in the current matrix which are now
14269 below the window. */
14270 for (++row; row < bottom_row; ++row)
14271 row->enabled_p = row->mode_line_p = 0;
14272 }
14273
14274 /* Update window_end_pos etc.; last_reused_text_row is the last
14275 reused row from the current matrix containing text, if any.
14276 The value of last_text_row is the last displayed line
14277 containing text. */
14278 if (last_reused_text_row)
14279 {
14280 w->window_end_bytepos
14281 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14282 w->window_end_pos
14283 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14284 w->window_end_vpos
14285 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14286 w->current_matrix));
14287 }
14288 else if (last_text_row)
14289 {
14290 w->window_end_bytepos
14291 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14292 w->window_end_pos
14293 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14294 w->window_end_vpos
14295 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14296 }
14297 else
14298 {
14299 /* This window must be completely empty. */
14300 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14301 w->window_end_pos = make_number (Z - ZV);
14302 w->window_end_vpos = make_number (0);
14303 }
14304 w->window_end_valid = Qnil;
14305
14306 /* Update hint: don't try scrolling again in update_window. */
14307 w->desired_matrix->no_scrolling_p = 1;
14308
14309 #if GLYPH_DEBUG
14310 debug_method_add (w, "try_window_reusing_current_matrix 1");
14311 #endif
14312 return 1;
14313 }
14314 else if (CHARPOS (new_start) > CHARPOS (start))
14315 {
14316 struct glyph_row *pt_row, *row;
14317 struct glyph_row *first_reusable_row;
14318 struct glyph_row *first_row_to_display;
14319 int dy;
14320 int yb = window_text_bottom_y (w);
14321
14322 /* Find the row starting at new_start, if there is one. Don't
14323 reuse a partially visible line at the end. */
14324 first_reusable_row = start_row;
14325 while (first_reusable_row->enabled_p
14326 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14327 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14328 < CHARPOS (new_start)))
14329 ++first_reusable_row;
14330
14331 /* Give up if there is no row to reuse. */
14332 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14333 || !first_reusable_row->enabled_p
14334 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14335 != CHARPOS (new_start)))
14336 return 0;
14337
14338 /* We can reuse fully visible rows beginning with
14339 first_reusable_row to the end of the window. Set
14340 first_row_to_display to the first row that cannot be reused.
14341 Set pt_row to the row containing point, if there is any. */
14342 pt_row = NULL;
14343 for (first_row_to_display = first_reusable_row;
14344 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14345 ++first_row_to_display)
14346 {
14347 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14348 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14349 pt_row = first_row_to_display;
14350 }
14351
14352 /* Start displaying at the start of first_row_to_display. */
14353 xassert (first_row_to_display->y < yb);
14354 init_to_row_start (&it, w, first_row_to_display);
14355
14356 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14357 - start_vpos);
14358 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14359 - nrows_scrolled);
14360 it.current_y = (first_row_to_display->y - first_reusable_row->y
14361 + WINDOW_HEADER_LINE_HEIGHT (w));
14362
14363 /* Display lines beginning with first_row_to_display in the
14364 desired matrix. Set last_text_row to the last row displayed
14365 that displays text. */
14366 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14367 if (pt_row == NULL)
14368 w->cursor.vpos = -1;
14369 last_text_row = NULL;
14370 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14371 if (display_line (&it))
14372 last_text_row = it.glyph_row - 1;
14373
14374 /* If point is in a reused row, adjust y and vpos of the cursor
14375 position. */
14376 if (pt_row)
14377 {
14378 w->cursor.vpos -= nrows_scrolled;
14379 w->cursor.y -= first_reusable_row->y - start_row->y;
14380 }
14381
14382 /* Give up if point isn't in a row displayed or reused. (This
14383 also handles the case where w->cursor.vpos < nrows_scrolled
14384 after the calls to display_line, which can happen with scroll
14385 margins. See bug#1295.) */
14386 if (w->cursor.vpos < 0)
14387 {
14388 clear_glyph_matrix (w->desired_matrix);
14389 return 0;
14390 }
14391
14392 /* Scroll the display. */
14393 run.current_y = first_reusable_row->y;
14394 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14395 run.height = it.last_visible_y - run.current_y;
14396 dy = run.current_y - run.desired_y;
14397
14398 if (run.height)
14399 {
14400 update_begin (f);
14401 FRAME_RIF (f)->update_window_begin_hook (w);
14402 FRAME_RIF (f)->clear_window_mouse_face (w);
14403 FRAME_RIF (f)->scroll_run_hook (w, &run);
14404 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14405 update_end (f);
14406 }
14407
14408 /* Adjust Y positions of reused rows. */
14409 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14410 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14411 max_y = it.last_visible_y;
14412 for (row = first_reusable_row; row < first_row_to_display; ++row)
14413 {
14414 row->y -= dy;
14415 row->visible_height = row->height;
14416 if (row->y < min_y)
14417 row->visible_height -= min_y - row->y;
14418 if (row->y + row->height > max_y)
14419 row->visible_height -= row->y + row->height - max_y;
14420 row->redraw_fringe_bitmaps_p = 1;
14421 }
14422
14423 /* Scroll the current matrix. */
14424 xassert (nrows_scrolled > 0);
14425 rotate_matrix (w->current_matrix,
14426 start_vpos,
14427 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14428 -nrows_scrolled);
14429
14430 /* Disable rows not reused. */
14431 for (row -= nrows_scrolled; row < bottom_row; ++row)
14432 row->enabled_p = 0;
14433
14434 /* Point may have moved to a different line, so we cannot assume that
14435 the previous cursor position is valid; locate the correct row. */
14436 if (pt_row)
14437 {
14438 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14439 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14440 row++)
14441 {
14442 w->cursor.vpos++;
14443 w->cursor.y = row->y;
14444 }
14445 if (row < bottom_row)
14446 {
14447 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14448 struct glyph *end = glyph + row->used[TEXT_AREA];
14449
14450 for (; glyph < end
14451 && (!BUFFERP (glyph->object)
14452 || glyph->charpos < PT);
14453 glyph++)
14454 {
14455 w->cursor.hpos++;
14456 w->cursor.x += glyph->pixel_width;
14457 }
14458 }
14459 }
14460
14461 /* Adjust window end. A null value of last_text_row means that
14462 the window end is in reused rows which in turn means that
14463 only its vpos can have changed. */
14464 if (last_text_row)
14465 {
14466 w->window_end_bytepos
14467 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14468 w->window_end_pos
14469 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14470 w->window_end_vpos
14471 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14472 }
14473 else
14474 {
14475 w->window_end_vpos
14476 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14477 }
14478
14479 w->window_end_valid = Qnil;
14480 w->desired_matrix->no_scrolling_p = 1;
14481
14482 #if GLYPH_DEBUG
14483 debug_method_add (w, "try_window_reusing_current_matrix 2");
14484 #endif
14485 return 1;
14486 }
14487
14488 return 0;
14489 }
14490
14491
14492 \f
14493 /************************************************************************
14494 Window redisplay reusing current matrix when buffer has changed
14495 ************************************************************************/
14496
14497 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14498 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14499 int *, int *));
14500 static struct glyph_row *
14501 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14502 struct glyph_row *));
14503
14504
14505 /* Return the last row in MATRIX displaying text. If row START is
14506 non-null, start searching with that row. IT gives the dimensions
14507 of the display. Value is null if matrix is empty; otherwise it is
14508 a pointer to the row found. */
14509
14510 static struct glyph_row *
14511 find_last_row_displaying_text (matrix, it, start)
14512 struct glyph_matrix *matrix;
14513 struct it *it;
14514 struct glyph_row *start;
14515 {
14516 struct glyph_row *row, *row_found;
14517
14518 /* Set row_found to the last row in IT->w's current matrix
14519 displaying text. The loop looks funny but think of partially
14520 visible lines. */
14521 row_found = NULL;
14522 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14523 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14524 {
14525 xassert (row->enabled_p);
14526 row_found = row;
14527 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14528 break;
14529 ++row;
14530 }
14531
14532 return row_found;
14533 }
14534
14535
14536 /* Return the last row in the current matrix of W that is not affected
14537 by changes at the start of current_buffer that occurred since W's
14538 current matrix was built. Value is null if no such row exists.
14539
14540 BEG_UNCHANGED us the number of characters unchanged at the start of
14541 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14542 first changed character in current_buffer. Characters at positions <
14543 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14544 when the current matrix was built. */
14545
14546 static struct glyph_row *
14547 find_last_unchanged_at_beg_row (w)
14548 struct window *w;
14549 {
14550 int first_changed_pos = BEG + BEG_UNCHANGED;
14551 struct glyph_row *row;
14552 struct glyph_row *row_found = NULL;
14553 int yb = window_text_bottom_y (w);
14554
14555 /* Find the last row displaying unchanged text. */
14556 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14557 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14558 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14559 ++row)
14560 {
14561 if (/* If row ends before first_changed_pos, it is unchanged,
14562 except in some case. */
14563 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14564 /* When row ends in ZV and we write at ZV it is not
14565 unchanged. */
14566 && !row->ends_at_zv_p
14567 /* When first_changed_pos is the end of a continued line,
14568 row is not unchanged because it may be no longer
14569 continued. */
14570 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14571 && (row->continued_p
14572 || row->exact_window_width_line_p)))
14573 row_found = row;
14574
14575 /* Stop if last visible row. */
14576 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14577 break;
14578 }
14579
14580 return row_found;
14581 }
14582
14583
14584 /* Find the first glyph row in the current matrix of W that is not
14585 affected by changes at the end of current_buffer since the
14586 time W's current matrix was built.
14587
14588 Return in *DELTA the number of chars by which buffer positions in
14589 unchanged text at the end of current_buffer must be adjusted.
14590
14591 Return in *DELTA_BYTES the corresponding number of bytes.
14592
14593 Value is null if no such row exists, i.e. all rows are affected by
14594 changes. */
14595
14596 static struct glyph_row *
14597 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14598 struct window *w;
14599 int *delta, *delta_bytes;
14600 {
14601 struct glyph_row *row;
14602 struct glyph_row *row_found = NULL;
14603
14604 *delta = *delta_bytes = 0;
14605
14606 /* Display must not have been paused, otherwise the current matrix
14607 is not up to date. */
14608 eassert (!NILP (w->window_end_valid));
14609
14610 /* A value of window_end_pos >= END_UNCHANGED means that the window
14611 end is in the range of changed text. If so, there is no
14612 unchanged row at the end of W's current matrix. */
14613 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14614 return NULL;
14615
14616 /* Set row to the last row in W's current matrix displaying text. */
14617 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14618
14619 /* If matrix is entirely empty, no unchanged row exists. */
14620 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14621 {
14622 /* The value of row is the last glyph row in the matrix having a
14623 meaningful buffer position in it. The end position of row
14624 corresponds to window_end_pos. This allows us to translate
14625 buffer positions in the current matrix to current buffer
14626 positions for characters not in changed text. */
14627 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14628 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14629 int last_unchanged_pos, last_unchanged_pos_old;
14630 struct glyph_row *first_text_row
14631 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14632
14633 *delta = Z - Z_old;
14634 *delta_bytes = Z_BYTE - Z_BYTE_old;
14635
14636 /* Set last_unchanged_pos to the buffer position of the last
14637 character in the buffer that has not been changed. Z is the
14638 index + 1 of the last character in current_buffer, i.e. by
14639 subtracting END_UNCHANGED we get the index of the last
14640 unchanged character, and we have to add BEG to get its buffer
14641 position. */
14642 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14643 last_unchanged_pos_old = last_unchanged_pos - *delta;
14644
14645 /* Search backward from ROW for a row displaying a line that
14646 starts at a minimum position >= last_unchanged_pos_old. */
14647 for (; row > first_text_row; --row)
14648 {
14649 /* This used to abort, but it can happen.
14650 It is ok to just stop the search instead here. KFS. */
14651 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14652 break;
14653
14654 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14655 row_found = row;
14656 }
14657 }
14658
14659 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14660
14661 return row_found;
14662 }
14663
14664
14665 /* Make sure that glyph rows in the current matrix of window W
14666 reference the same glyph memory as corresponding rows in the
14667 frame's frame matrix. This function is called after scrolling W's
14668 current matrix on a terminal frame in try_window_id and
14669 try_window_reusing_current_matrix. */
14670
14671 static void
14672 sync_frame_with_window_matrix_rows (w)
14673 struct window *w;
14674 {
14675 struct frame *f = XFRAME (w->frame);
14676 struct glyph_row *window_row, *window_row_end, *frame_row;
14677
14678 /* Preconditions: W must be a leaf window and full-width. Its frame
14679 must have a frame matrix. */
14680 xassert (NILP (w->hchild) && NILP (w->vchild));
14681 xassert (WINDOW_FULL_WIDTH_P (w));
14682 xassert (!FRAME_WINDOW_P (f));
14683
14684 /* If W is a full-width window, glyph pointers in W's current matrix
14685 have, by definition, to be the same as glyph pointers in the
14686 corresponding frame matrix. Note that frame matrices have no
14687 marginal areas (see build_frame_matrix). */
14688 window_row = w->current_matrix->rows;
14689 window_row_end = window_row + w->current_matrix->nrows;
14690 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14691 while (window_row < window_row_end)
14692 {
14693 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14694 struct glyph *end = window_row->glyphs[LAST_AREA];
14695
14696 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14697 frame_row->glyphs[TEXT_AREA] = start;
14698 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14699 frame_row->glyphs[LAST_AREA] = end;
14700
14701 /* Disable frame rows whose corresponding window rows have
14702 been disabled in try_window_id. */
14703 if (!window_row->enabled_p)
14704 frame_row->enabled_p = 0;
14705
14706 ++window_row, ++frame_row;
14707 }
14708 }
14709
14710
14711 /* Find the glyph row in window W containing CHARPOS. Consider all
14712 rows between START and END (not inclusive). END null means search
14713 all rows to the end of the display area of W. Value is the row
14714 containing CHARPOS or null. */
14715
14716 struct glyph_row *
14717 row_containing_pos (w, charpos, start, end, dy)
14718 struct window *w;
14719 int charpos;
14720 struct glyph_row *start, *end;
14721 int dy;
14722 {
14723 struct glyph_row *row = start;
14724 int last_y;
14725
14726 /* If we happen to start on a header-line, skip that. */
14727 if (row->mode_line_p)
14728 ++row;
14729
14730 if ((end && row >= end) || !row->enabled_p)
14731 return NULL;
14732
14733 last_y = window_text_bottom_y (w) - dy;
14734
14735 while (1)
14736 {
14737 /* Give up if we have gone too far. */
14738 if (end && row >= end)
14739 return NULL;
14740 /* This formerly returned if they were equal.
14741 I think that both quantities are of a "last plus one" type;
14742 if so, when they are equal, the row is within the screen. -- rms. */
14743 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14744 return NULL;
14745
14746 /* If it is in this row, return this row. */
14747 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14748 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14749 /* The end position of a row equals the start
14750 position of the next row. If CHARPOS is there, we
14751 would rather display it in the next line, except
14752 when this line ends in ZV. */
14753 && !row->ends_at_zv_p
14754 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14755 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14756 return row;
14757 ++row;
14758 }
14759 }
14760
14761
14762 /* Try to redisplay window W by reusing its existing display. W's
14763 current matrix must be up to date when this function is called,
14764 i.e. window_end_valid must not be nil.
14765
14766 Value is
14767
14768 1 if display has been updated
14769 0 if otherwise unsuccessful
14770 -1 if redisplay with same window start is known not to succeed
14771
14772 The following steps are performed:
14773
14774 1. Find the last row in the current matrix of W that is not
14775 affected by changes at the start of current_buffer. If no such row
14776 is found, give up.
14777
14778 2. Find the first row in W's current matrix that is not affected by
14779 changes at the end of current_buffer. Maybe there is no such row.
14780
14781 3. Display lines beginning with the row + 1 found in step 1 to the
14782 row found in step 2 or, if step 2 didn't find a row, to the end of
14783 the window.
14784
14785 4. If cursor is not known to appear on the window, give up.
14786
14787 5. If display stopped at the row found in step 2, scroll the
14788 display and current matrix as needed.
14789
14790 6. Maybe display some lines at the end of W, if we must. This can
14791 happen under various circumstances, like a partially visible line
14792 becoming fully visible, or because newly displayed lines are displayed
14793 in smaller font sizes.
14794
14795 7. Update W's window end information. */
14796
14797 static int
14798 try_window_id (w)
14799 struct window *w;
14800 {
14801 struct frame *f = XFRAME (w->frame);
14802 struct glyph_matrix *current_matrix = w->current_matrix;
14803 struct glyph_matrix *desired_matrix = w->desired_matrix;
14804 struct glyph_row *last_unchanged_at_beg_row;
14805 struct glyph_row *first_unchanged_at_end_row;
14806 struct glyph_row *row;
14807 struct glyph_row *bottom_row;
14808 int bottom_vpos;
14809 struct it it;
14810 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14811 struct text_pos start_pos;
14812 struct run run;
14813 int first_unchanged_at_end_vpos = 0;
14814 struct glyph_row *last_text_row, *last_text_row_at_end;
14815 struct text_pos start;
14816 int first_changed_charpos, last_changed_charpos;
14817
14818 #if GLYPH_DEBUG
14819 if (inhibit_try_window_id)
14820 return 0;
14821 #endif
14822
14823 /* This is handy for debugging. */
14824 #if 0
14825 #define GIVE_UP(X) \
14826 do { \
14827 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14828 return 0; \
14829 } while (0)
14830 #else
14831 #define GIVE_UP(X) return 0
14832 #endif
14833
14834 SET_TEXT_POS_FROM_MARKER (start, w->start);
14835
14836 /* Don't use this for mini-windows because these can show
14837 messages and mini-buffers, and we don't handle that here. */
14838 if (MINI_WINDOW_P (w))
14839 GIVE_UP (1);
14840
14841 /* This flag is used to prevent redisplay optimizations. */
14842 if (windows_or_buffers_changed || cursor_type_changed)
14843 GIVE_UP (2);
14844
14845 /* Verify that narrowing has not changed.
14846 Also verify that we were not told to prevent redisplay optimizations.
14847 It would be nice to further
14848 reduce the number of cases where this prevents try_window_id. */
14849 if (current_buffer->clip_changed
14850 || current_buffer->prevent_redisplay_optimizations_p)
14851 GIVE_UP (3);
14852
14853 /* Window must either use window-based redisplay or be full width. */
14854 if (!FRAME_WINDOW_P (f)
14855 && (!FRAME_LINE_INS_DEL_OK (f)
14856 || !WINDOW_FULL_WIDTH_P (w)))
14857 GIVE_UP (4);
14858
14859 /* Give up if point is not known NOT to appear in W. */
14860 if (PT < CHARPOS (start))
14861 GIVE_UP (5);
14862
14863 /* Another way to prevent redisplay optimizations. */
14864 if (XFASTINT (w->last_modified) == 0)
14865 GIVE_UP (6);
14866
14867 /* Verify that window is not hscrolled. */
14868 if (XFASTINT (w->hscroll) != 0)
14869 GIVE_UP (7);
14870
14871 /* Verify that display wasn't paused. */
14872 if (NILP (w->window_end_valid))
14873 GIVE_UP (8);
14874
14875 /* Can't use this if highlighting a region because a cursor movement
14876 will do more than just set the cursor. */
14877 if (!NILP (Vtransient_mark_mode)
14878 && !NILP (current_buffer->mark_active))
14879 GIVE_UP (9);
14880
14881 /* Likewise if highlighting trailing whitespace. */
14882 if (!NILP (Vshow_trailing_whitespace))
14883 GIVE_UP (11);
14884
14885 /* Likewise if showing a region. */
14886 if (!NILP (w->region_showing))
14887 GIVE_UP (10);
14888
14889 /* Can use this if overlay arrow position and or string have changed. */
14890 if (overlay_arrows_changed_p ())
14891 GIVE_UP (12);
14892
14893 /* When word-wrap is on, adding a space to the first word of a
14894 wrapped line can change the wrap position, altering the line
14895 above it. It might be worthwhile to handle this more
14896 intelligently, but for now just redisplay from scratch. */
14897 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14898 GIVE_UP (21);
14899
14900 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14901 only if buffer has really changed. The reason is that the gap is
14902 initially at Z for freshly visited files. The code below would
14903 set end_unchanged to 0 in that case. */
14904 if (MODIFF > SAVE_MODIFF
14905 /* This seems to happen sometimes after saving a buffer. */
14906 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14907 {
14908 if (GPT - BEG < BEG_UNCHANGED)
14909 BEG_UNCHANGED = GPT - BEG;
14910 if (Z - GPT < END_UNCHANGED)
14911 END_UNCHANGED = Z - GPT;
14912 }
14913
14914 /* The position of the first and last character that has been changed. */
14915 first_changed_charpos = BEG + BEG_UNCHANGED;
14916 last_changed_charpos = Z - END_UNCHANGED;
14917
14918 /* If window starts after a line end, and the last change is in
14919 front of that newline, then changes don't affect the display.
14920 This case happens with stealth-fontification. Note that although
14921 the display is unchanged, glyph positions in the matrix have to
14922 be adjusted, of course. */
14923 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14924 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14925 && ((last_changed_charpos < CHARPOS (start)
14926 && CHARPOS (start) == BEGV)
14927 || (last_changed_charpos < CHARPOS (start) - 1
14928 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14929 {
14930 int Z_old, delta, Z_BYTE_old, delta_bytes;
14931 struct glyph_row *r0;
14932
14933 /* Compute how many chars/bytes have been added to or removed
14934 from the buffer. */
14935 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14936 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14937 delta = Z - Z_old;
14938 delta_bytes = Z_BYTE - Z_BYTE_old;
14939
14940 /* Give up if PT is not in the window. Note that it already has
14941 been checked at the start of try_window_id that PT is not in
14942 front of the window start. */
14943 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14944 GIVE_UP (13);
14945
14946 /* If window start is unchanged, we can reuse the whole matrix
14947 as is, after adjusting glyph positions. No need to compute
14948 the window end again, since its offset from Z hasn't changed. */
14949 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14950 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14951 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14952 /* PT must not be in a partially visible line. */
14953 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14954 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14955 {
14956 /* Adjust positions in the glyph matrix. */
14957 if (delta || delta_bytes)
14958 {
14959 struct glyph_row *r1
14960 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14961 increment_matrix_positions (w->current_matrix,
14962 MATRIX_ROW_VPOS (r0, current_matrix),
14963 MATRIX_ROW_VPOS (r1, current_matrix),
14964 delta, delta_bytes);
14965 }
14966
14967 /* Set the cursor. */
14968 row = row_containing_pos (w, PT, r0, NULL, 0);
14969 if (row)
14970 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14971 else
14972 abort ();
14973 return 1;
14974 }
14975 }
14976
14977 /* Handle the case that changes are all below what is displayed in
14978 the window, and that PT is in the window. This shortcut cannot
14979 be taken if ZV is visible in the window, and text has been added
14980 there that is visible in the window. */
14981 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14982 /* ZV is not visible in the window, or there are no
14983 changes at ZV, actually. */
14984 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14985 || first_changed_charpos == last_changed_charpos))
14986 {
14987 struct glyph_row *r0;
14988
14989 /* Give up if PT is not in the window. Note that it already has
14990 been checked at the start of try_window_id that PT is not in
14991 front of the window start. */
14992 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14993 GIVE_UP (14);
14994
14995 /* If window start is unchanged, we can reuse the whole matrix
14996 as is, without changing glyph positions since no text has
14997 been added/removed in front of the window end. */
14998 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14999 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15000 /* PT must not be in a partially visible line. */
15001 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15002 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15003 {
15004 /* We have to compute the window end anew since text
15005 can have been added/removed after it. */
15006 w->window_end_pos
15007 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15008 w->window_end_bytepos
15009 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15010
15011 /* Set the cursor. */
15012 row = row_containing_pos (w, PT, r0, NULL, 0);
15013 if (row)
15014 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15015 else
15016 abort ();
15017 return 2;
15018 }
15019 }
15020
15021 /* Give up if window start is in the changed area.
15022
15023 The condition used to read
15024
15025 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15026
15027 but why that was tested escapes me at the moment. */
15028 if (CHARPOS (start) >= first_changed_charpos
15029 && CHARPOS (start) <= last_changed_charpos)
15030 GIVE_UP (15);
15031
15032 /* Check that window start agrees with the start of the first glyph
15033 row in its current matrix. Check this after we know the window
15034 start is not in changed text, otherwise positions would not be
15035 comparable. */
15036 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15037 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15038 GIVE_UP (16);
15039
15040 /* Give up if the window ends in strings. Overlay strings
15041 at the end are difficult to handle, so don't try. */
15042 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15043 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15044 GIVE_UP (20);
15045
15046 /* Compute the position at which we have to start displaying new
15047 lines. Some of the lines at the top of the window might be
15048 reusable because they are not displaying changed text. Find the
15049 last row in W's current matrix not affected by changes at the
15050 start of current_buffer. Value is null if changes start in the
15051 first line of window. */
15052 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15053 if (last_unchanged_at_beg_row)
15054 {
15055 /* Avoid starting to display in the moddle of a character, a TAB
15056 for instance. This is easier than to set up the iterator
15057 exactly, and it's not a frequent case, so the additional
15058 effort wouldn't really pay off. */
15059 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15060 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15061 && last_unchanged_at_beg_row > w->current_matrix->rows)
15062 --last_unchanged_at_beg_row;
15063
15064 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15065 GIVE_UP (17);
15066
15067 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15068 GIVE_UP (18);
15069 start_pos = it.current.pos;
15070
15071 /* Start displaying new lines in the desired matrix at the same
15072 vpos we would use in the current matrix, i.e. below
15073 last_unchanged_at_beg_row. */
15074 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15075 current_matrix);
15076 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15077 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15078
15079 xassert (it.hpos == 0 && it.current_x == 0);
15080 }
15081 else
15082 {
15083 /* There are no reusable lines at the start of the window.
15084 Start displaying in the first text line. */
15085 start_display (&it, w, start);
15086 it.vpos = it.first_vpos;
15087 start_pos = it.current.pos;
15088 }
15089
15090 /* Find the first row that is not affected by changes at the end of
15091 the buffer. Value will be null if there is no unchanged row, in
15092 which case we must redisplay to the end of the window. delta
15093 will be set to the value by which buffer positions beginning with
15094 first_unchanged_at_end_row have to be adjusted due to text
15095 changes. */
15096 first_unchanged_at_end_row
15097 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15098 IF_DEBUG (debug_delta = delta);
15099 IF_DEBUG (debug_delta_bytes = delta_bytes);
15100
15101 /* Set stop_pos to the buffer position up to which we will have to
15102 display new lines. If first_unchanged_at_end_row != NULL, this
15103 is the buffer position of the start of the line displayed in that
15104 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15105 that we don't stop at a buffer position. */
15106 stop_pos = 0;
15107 if (first_unchanged_at_end_row)
15108 {
15109 xassert (last_unchanged_at_beg_row == NULL
15110 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15111
15112 /* If this is a continuation line, move forward to the next one
15113 that isn't. Changes in lines above affect this line.
15114 Caution: this may move first_unchanged_at_end_row to a row
15115 not displaying text. */
15116 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15117 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15118 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15119 < it.last_visible_y))
15120 ++first_unchanged_at_end_row;
15121
15122 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15123 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15124 >= it.last_visible_y))
15125 first_unchanged_at_end_row = NULL;
15126 else
15127 {
15128 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15129 + delta);
15130 first_unchanged_at_end_vpos
15131 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15132 xassert (stop_pos >= Z - END_UNCHANGED);
15133 }
15134 }
15135 else if (last_unchanged_at_beg_row == NULL)
15136 GIVE_UP (19);
15137
15138
15139 #if GLYPH_DEBUG
15140
15141 /* Either there is no unchanged row at the end, or the one we have
15142 now displays text. This is a necessary condition for the window
15143 end pos calculation at the end of this function. */
15144 xassert (first_unchanged_at_end_row == NULL
15145 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15146
15147 debug_last_unchanged_at_beg_vpos
15148 = (last_unchanged_at_beg_row
15149 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15150 : -1);
15151 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15152
15153 #endif /* GLYPH_DEBUG != 0 */
15154
15155
15156 /* Display new lines. Set last_text_row to the last new line
15157 displayed which has text on it, i.e. might end up as being the
15158 line where the window_end_vpos is. */
15159 w->cursor.vpos = -1;
15160 last_text_row = NULL;
15161 overlay_arrow_seen = 0;
15162 while (it.current_y < it.last_visible_y
15163 && !fonts_changed_p
15164 && (first_unchanged_at_end_row == NULL
15165 || IT_CHARPOS (it) < stop_pos))
15166 {
15167 if (display_line (&it))
15168 last_text_row = it.glyph_row - 1;
15169 }
15170
15171 if (fonts_changed_p)
15172 return -1;
15173
15174
15175 /* Compute differences in buffer positions, y-positions etc. for
15176 lines reused at the bottom of the window. Compute what we can
15177 scroll. */
15178 if (first_unchanged_at_end_row
15179 /* No lines reused because we displayed everything up to the
15180 bottom of the window. */
15181 && it.current_y < it.last_visible_y)
15182 {
15183 dvpos = (it.vpos
15184 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15185 current_matrix));
15186 dy = it.current_y - first_unchanged_at_end_row->y;
15187 run.current_y = first_unchanged_at_end_row->y;
15188 run.desired_y = run.current_y + dy;
15189 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15190 }
15191 else
15192 {
15193 delta = delta_bytes = dvpos = dy
15194 = run.current_y = run.desired_y = run.height = 0;
15195 first_unchanged_at_end_row = NULL;
15196 }
15197 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15198
15199
15200 /* Find the cursor if not already found. We have to decide whether
15201 PT will appear on this window (it sometimes doesn't, but this is
15202 not a very frequent case.) This decision has to be made before
15203 the current matrix is altered. A value of cursor.vpos < 0 means
15204 that PT is either in one of the lines beginning at
15205 first_unchanged_at_end_row or below the window. Don't care for
15206 lines that might be displayed later at the window end; as
15207 mentioned, this is not a frequent case. */
15208 if (w->cursor.vpos < 0)
15209 {
15210 /* Cursor in unchanged rows at the top? */
15211 if (PT < CHARPOS (start_pos)
15212 && last_unchanged_at_beg_row)
15213 {
15214 row = row_containing_pos (w, PT,
15215 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15216 last_unchanged_at_beg_row + 1, 0);
15217 if (row)
15218 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15219 }
15220
15221 /* Start from first_unchanged_at_end_row looking for PT. */
15222 else if (first_unchanged_at_end_row)
15223 {
15224 row = row_containing_pos (w, PT - delta,
15225 first_unchanged_at_end_row, NULL, 0);
15226 if (row)
15227 set_cursor_from_row (w, row, w->current_matrix, delta,
15228 delta_bytes, dy, dvpos);
15229 }
15230
15231 /* Give up if cursor was not found. */
15232 if (w->cursor.vpos < 0)
15233 {
15234 clear_glyph_matrix (w->desired_matrix);
15235 return -1;
15236 }
15237 }
15238
15239 /* Don't let the cursor end in the scroll margins. */
15240 {
15241 int this_scroll_margin, cursor_height;
15242
15243 this_scroll_margin = max (0, scroll_margin);
15244 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15245 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15246 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15247
15248 if ((w->cursor.y < this_scroll_margin
15249 && CHARPOS (start) > BEGV)
15250 /* Old redisplay didn't take scroll margin into account at the bottom,
15251 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15252 || (w->cursor.y + (make_cursor_line_fully_visible_p
15253 ? cursor_height + this_scroll_margin
15254 : 1)) > it.last_visible_y)
15255 {
15256 w->cursor.vpos = -1;
15257 clear_glyph_matrix (w->desired_matrix);
15258 return -1;
15259 }
15260 }
15261
15262 /* Scroll the display. Do it before changing the current matrix so
15263 that xterm.c doesn't get confused about where the cursor glyph is
15264 found. */
15265 if (dy && run.height)
15266 {
15267 update_begin (f);
15268
15269 if (FRAME_WINDOW_P (f))
15270 {
15271 FRAME_RIF (f)->update_window_begin_hook (w);
15272 FRAME_RIF (f)->clear_window_mouse_face (w);
15273 FRAME_RIF (f)->scroll_run_hook (w, &run);
15274 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15275 }
15276 else
15277 {
15278 /* Terminal frame. In this case, dvpos gives the number of
15279 lines to scroll by; dvpos < 0 means scroll up. */
15280 int first_unchanged_at_end_vpos
15281 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15282 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15283 int end = (WINDOW_TOP_EDGE_LINE (w)
15284 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15285 + window_internal_height (w));
15286
15287 /* Perform the operation on the screen. */
15288 if (dvpos > 0)
15289 {
15290 /* Scroll last_unchanged_at_beg_row to the end of the
15291 window down dvpos lines. */
15292 set_terminal_window (f, end);
15293
15294 /* On dumb terminals delete dvpos lines at the end
15295 before inserting dvpos empty lines. */
15296 if (!FRAME_SCROLL_REGION_OK (f))
15297 ins_del_lines (f, end - dvpos, -dvpos);
15298
15299 /* Insert dvpos empty lines in front of
15300 last_unchanged_at_beg_row. */
15301 ins_del_lines (f, from, dvpos);
15302 }
15303 else if (dvpos < 0)
15304 {
15305 /* Scroll up last_unchanged_at_beg_vpos to the end of
15306 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15307 set_terminal_window (f, end);
15308
15309 /* Delete dvpos lines in front of
15310 last_unchanged_at_beg_vpos. ins_del_lines will set
15311 the cursor to the given vpos and emit |dvpos| delete
15312 line sequences. */
15313 ins_del_lines (f, from + dvpos, dvpos);
15314
15315 /* On a dumb terminal insert dvpos empty lines at the
15316 end. */
15317 if (!FRAME_SCROLL_REGION_OK (f))
15318 ins_del_lines (f, end + dvpos, -dvpos);
15319 }
15320
15321 set_terminal_window (f, 0);
15322 }
15323
15324 update_end (f);
15325 }
15326
15327 /* Shift reused rows of the current matrix to the right position.
15328 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15329 text. */
15330 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15331 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15332 if (dvpos < 0)
15333 {
15334 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15335 bottom_vpos, dvpos);
15336 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15337 bottom_vpos, 0);
15338 }
15339 else if (dvpos > 0)
15340 {
15341 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15342 bottom_vpos, dvpos);
15343 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15344 first_unchanged_at_end_vpos + dvpos, 0);
15345 }
15346
15347 /* For frame-based redisplay, make sure that current frame and window
15348 matrix are in sync with respect to glyph memory. */
15349 if (!FRAME_WINDOW_P (f))
15350 sync_frame_with_window_matrix_rows (w);
15351
15352 /* Adjust buffer positions in reused rows. */
15353 if (delta || delta_bytes)
15354 increment_matrix_positions (current_matrix,
15355 first_unchanged_at_end_vpos + dvpos,
15356 bottom_vpos, delta, delta_bytes);
15357
15358 /* Adjust Y positions. */
15359 if (dy)
15360 shift_glyph_matrix (w, current_matrix,
15361 first_unchanged_at_end_vpos + dvpos,
15362 bottom_vpos, dy);
15363
15364 if (first_unchanged_at_end_row)
15365 {
15366 first_unchanged_at_end_row += dvpos;
15367 if (first_unchanged_at_end_row->y >= it.last_visible_y
15368 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15369 first_unchanged_at_end_row = NULL;
15370 }
15371
15372 /* If scrolling up, there may be some lines to display at the end of
15373 the window. */
15374 last_text_row_at_end = NULL;
15375 if (dy < 0)
15376 {
15377 /* Scrolling up can leave for example a partially visible line
15378 at the end of the window to be redisplayed. */
15379 /* Set last_row to the glyph row in the current matrix where the
15380 window end line is found. It has been moved up or down in
15381 the matrix by dvpos. */
15382 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15383 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15384
15385 /* If last_row is the window end line, it should display text. */
15386 xassert (last_row->displays_text_p);
15387
15388 /* If window end line was partially visible before, begin
15389 displaying at that line. Otherwise begin displaying with the
15390 line following it. */
15391 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15392 {
15393 init_to_row_start (&it, w, last_row);
15394 it.vpos = last_vpos;
15395 it.current_y = last_row->y;
15396 }
15397 else
15398 {
15399 init_to_row_end (&it, w, last_row);
15400 it.vpos = 1 + last_vpos;
15401 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15402 ++last_row;
15403 }
15404
15405 /* We may start in a continuation line. If so, we have to
15406 get the right continuation_lines_width and current_x. */
15407 it.continuation_lines_width = last_row->continuation_lines_width;
15408 it.hpos = it.current_x = 0;
15409
15410 /* Display the rest of the lines at the window end. */
15411 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15412 while (it.current_y < it.last_visible_y
15413 && !fonts_changed_p)
15414 {
15415 /* Is it always sure that the display agrees with lines in
15416 the current matrix? I don't think so, so we mark rows
15417 displayed invalid in the current matrix by setting their
15418 enabled_p flag to zero. */
15419 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15420 if (display_line (&it))
15421 last_text_row_at_end = it.glyph_row - 1;
15422 }
15423 }
15424
15425 /* Update window_end_pos and window_end_vpos. */
15426 if (first_unchanged_at_end_row
15427 && !last_text_row_at_end)
15428 {
15429 /* Window end line if one of the preserved rows from the current
15430 matrix. Set row to the last row displaying text in current
15431 matrix starting at first_unchanged_at_end_row, after
15432 scrolling. */
15433 xassert (first_unchanged_at_end_row->displays_text_p);
15434 row = find_last_row_displaying_text (w->current_matrix, &it,
15435 first_unchanged_at_end_row);
15436 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15437
15438 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15439 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15440 w->window_end_vpos
15441 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15442 xassert (w->window_end_bytepos >= 0);
15443 IF_DEBUG (debug_method_add (w, "A"));
15444 }
15445 else if (last_text_row_at_end)
15446 {
15447 w->window_end_pos
15448 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15449 w->window_end_bytepos
15450 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15451 w->window_end_vpos
15452 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15453 xassert (w->window_end_bytepos >= 0);
15454 IF_DEBUG (debug_method_add (w, "B"));
15455 }
15456 else if (last_text_row)
15457 {
15458 /* We have displayed either to the end of the window or at the
15459 end of the window, i.e. the last row with text is to be found
15460 in the desired matrix. */
15461 w->window_end_pos
15462 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15463 w->window_end_bytepos
15464 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15465 w->window_end_vpos
15466 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15467 xassert (w->window_end_bytepos >= 0);
15468 }
15469 else if (first_unchanged_at_end_row == NULL
15470 && last_text_row == NULL
15471 && last_text_row_at_end == NULL)
15472 {
15473 /* Displayed to end of window, but no line containing text was
15474 displayed. Lines were deleted at the end of the window. */
15475 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15476 int vpos = XFASTINT (w->window_end_vpos);
15477 struct glyph_row *current_row = current_matrix->rows + vpos;
15478 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15479
15480 for (row = NULL;
15481 row == NULL && vpos >= first_vpos;
15482 --vpos, --current_row, --desired_row)
15483 {
15484 if (desired_row->enabled_p)
15485 {
15486 if (desired_row->displays_text_p)
15487 row = desired_row;
15488 }
15489 else if (current_row->displays_text_p)
15490 row = current_row;
15491 }
15492
15493 xassert (row != NULL);
15494 w->window_end_vpos = make_number (vpos + 1);
15495 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15496 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15497 xassert (w->window_end_bytepos >= 0);
15498 IF_DEBUG (debug_method_add (w, "C"));
15499 }
15500 else
15501 abort ();
15502
15503 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15504 debug_end_vpos = XFASTINT (w->window_end_vpos));
15505
15506 /* Record that display has not been completed. */
15507 w->window_end_valid = Qnil;
15508 w->desired_matrix->no_scrolling_p = 1;
15509 return 3;
15510
15511 #undef GIVE_UP
15512 }
15513
15514
15515 \f
15516 /***********************************************************************
15517 More debugging support
15518 ***********************************************************************/
15519
15520 #if GLYPH_DEBUG
15521
15522 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15523 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15524 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15525
15526
15527 /* Dump the contents of glyph matrix MATRIX on stderr.
15528
15529 GLYPHS 0 means don't show glyph contents.
15530 GLYPHS 1 means show glyphs in short form
15531 GLYPHS > 1 means show glyphs in long form. */
15532
15533 void
15534 dump_glyph_matrix (matrix, glyphs)
15535 struct glyph_matrix *matrix;
15536 int glyphs;
15537 {
15538 int i;
15539 for (i = 0; i < matrix->nrows; ++i)
15540 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15541 }
15542
15543
15544 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15545 the glyph row and area where the glyph comes from. */
15546
15547 void
15548 dump_glyph (row, glyph, area)
15549 struct glyph_row *row;
15550 struct glyph *glyph;
15551 int area;
15552 {
15553 if (glyph->type == CHAR_GLYPH)
15554 {
15555 fprintf (stderr,
15556 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15557 glyph - row->glyphs[TEXT_AREA],
15558 'C',
15559 glyph->charpos,
15560 (BUFFERP (glyph->object)
15561 ? 'B'
15562 : (STRINGP (glyph->object)
15563 ? 'S'
15564 : '-')),
15565 glyph->pixel_width,
15566 glyph->u.ch,
15567 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15568 ? glyph->u.ch
15569 : '.'),
15570 glyph->face_id,
15571 glyph->left_box_line_p,
15572 glyph->right_box_line_p);
15573 }
15574 else if (glyph->type == STRETCH_GLYPH)
15575 {
15576 fprintf (stderr,
15577 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15578 glyph - row->glyphs[TEXT_AREA],
15579 'S',
15580 glyph->charpos,
15581 (BUFFERP (glyph->object)
15582 ? 'B'
15583 : (STRINGP (glyph->object)
15584 ? 'S'
15585 : '-')),
15586 glyph->pixel_width,
15587 0,
15588 '.',
15589 glyph->face_id,
15590 glyph->left_box_line_p,
15591 glyph->right_box_line_p);
15592 }
15593 else if (glyph->type == IMAGE_GLYPH)
15594 {
15595 fprintf (stderr,
15596 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15597 glyph - row->glyphs[TEXT_AREA],
15598 'I',
15599 glyph->charpos,
15600 (BUFFERP (glyph->object)
15601 ? 'B'
15602 : (STRINGP (glyph->object)
15603 ? 'S'
15604 : '-')),
15605 glyph->pixel_width,
15606 glyph->u.img_id,
15607 '.',
15608 glyph->face_id,
15609 glyph->left_box_line_p,
15610 glyph->right_box_line_p);
15611 }
15612 else if (glyph->type == COMPOSITE_GLYPH)
15613 {
15614 fprintf (stderr,
15615 " %5d %4c %6d %c %3d 0x%05x",
15616 glyph - row->glyphs[TEXT_AREA],
15617 '+',
15618 glyph->charpos,
15619 (BUFFERP (glyph->object)
15620 ? 'B'
15621 : (STRINGP (glyph->object)
15622 ? 'S'
15623 : '-')),
15624 glyph->pixel_width,
15625 glyph->u.cmp.id);
15626 if (glyph->u.cmp.automatic)
15627 fprintf (stderr,
15628 "[%d-%d]",
15629 glyph->u.cmp.from, glyph->u.cmp.to);
15630 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15631 glyph->face_id,
15632 glyph->left_box_line_p,
15633 glyph->right_box_line_p);
15634 }
15635 }
15636
15637
15638 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15639 GLYPHS 0 means don't show glyph contents.
15640 GLYPHS 1 means show glyphs in short form
15641 GLYPHS > 1 means show glyphs in long form. */
15642
15643 void
15644 dump_glyph_row (row, vpos, glyphs)
15645 struct glyph_row *row;
15646 int vpos, glyphs;
15647 {
15648 if (glyphs != 1)
15649 {
15650 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15651 fprintf (stderr, "======================================================================\n");
15652
15653 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15654 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15655 vpos,
15656 MATRIX_ROW_START_CHARPOS (row),
15657 MATRIX_ROW_END_CHARPOS (row),
15658 row->used[TEXT_AREA],
15659 row->contains_overlapping_glyphs_p,
15660 row->enabled_p,
15661 row->truncated_on_left_p,
15662 row->truncated_on_right_p,
15663 row->continued_p,
15664 MATRIX_ROW_CONTINUATION_LINE_P (row),
15665 row->displays_text_p,
15666 row->ends_at_zv_p,
15667 row->fill_line_p,
15668 row->ends_in_middle_of_char_p,
15669 row->starts_in_middle_of_char_p,
15670 row->mouse_face_p,
15671 row->x,
15672 row->y,
15673 row->pixel_width,
15674 row->height,
15675 row->visible_height,
15676 row->ascent,
15677 row->phys_ascent);
15678 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15679 row->end.overlay_string_index,
15680 row->continuation_lines_width);
15681 fprintf (stderr, "%9d %5d\n",
15682 CHARPOS (row->start.string_pos),
15683 CHARPOS (row->end.string_pos));
15684 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15685 row->end.dpvec_index);
15686 }
15687
15688 if (glyphs > 1)
15689 {
15690 int area;
15691
15692 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15693 {
15694 struct glyph *glyph = row->glyphs[area];
15695 struct glyph *glyph_end = glyph + row->used[area];
15696
15697 /* Glyph for a line end in text. */
15698 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15699 ++glyph_end;
15700
15701 if (glyph < glyph_end)
15702 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15703
15704 for (; glyph < glyph_end; ++glyph)
15705 dump_glyph (row, glyph, area);
15706 }
15707 }
15708 else if (glyphs == 1)
15709 {
15710 int area;
15711
15712 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15713 {
15714 char *s = (char *) alloca (row->used[area] + 1);
15715 int i;
15716
15717 for (i = 0; i < row->used[area]; ++i)
15718 {
15719 struct glyph *glyph = row->glyphs[area] + i;
15720 if (glyph->type == CHAR_GLYPH
15721 && glyph->u.ch < 0x80
15722 && glyph->u.ch >= ' ')
15723 s[i] = glyph->u.ch;
15724 else
15725 s[i] = '.';
15726 }
15727
15728 s[i] = '\0';
15729 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15730 }
15731 }
15732 }
15733
15734
15735 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15736 Sdump_glyph_matrix, 0, 1, "p",
15737 doc: /* Dump the current matrix of the selected window to stderr.
15738 Shows contents of glyph row structures. With non-nil
15739 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15740 glyphs in short form, otherwise show glyphs in long form. */)
15741 (glyphs)
15742 Lisp_Object glyphs;
15743 {
15744 struct window *w = XWINDOW (selected_window);
15745 struct buffer *buffer = XBUFFER (w->buffer);
15746
15747 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15748 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15749 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15750 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15751 fprintf (stderr, "=============================================\n");
15752 dump_glyph_matrix (w->current_matrix,
15753 NILP (glyphs) ? 0 : XINT (glyphs));
15754 return Qnil;
15755 }
15756
15757
15758 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15759 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15760 ()
15761 {
15762 struct frame *f = XFRAME (selected_frame);
15763 dump_glyph_matrix (f->current_matrix, 1);
15764 return Qnil;
15765 }
15766
15767
15768 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15769 doc: /* Dump glyph row ROW to stderr.
15770 GLYPH 0 means don't dump glyphs.
15771 GLYPH 1 means dump glyphs in short form.
15772 GLYPH > 1 or omitted means dump glyphs in long form. */)
15773 (row, glyphs)
15774 Lisp_Object row, glyphs;
15775 {
15776 struct glyph_matrix *matrix;
15777 int vpos;
15778
15779 CHECK_NUMBER (row);
15780 matrix = XWINDOW (selected_window)->current_matrix;
15781 vpos = XINT (row);
15782 if (vpos >= 0 && vpos < matrix->nrows)
15783 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15784 vpos,
15785 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15786 return Qnil;
15787 }
15788
15789
15790 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15791 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15792 GLYPH 0 means don't dump glyphs.
15793 GLYPH 1 means dump glyphs in short form.
15794 GLYPH > 1 or omitted means dump glyphs in long form. */)
15795 (row, glyphs)
15796 Lisp_Object row, glyphs;
15797 {
15798 struct frame *sf = SELECTED_FRAME ();
15799 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15800 int vpos;
15801
15802 CHECK_NUMBER (row);
15803 vpos = XINT (row);
15804 if (vpos >= 0 && vpos < m->nrows)
15805 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15806 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15807 return Qnil;
15808 }
15809
15810
15811 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15812 doc: /* Toggle tracing of redisplay.
15813 With ARG, turn tracing on if and only if ARG is positive. */)
15814 (arg)
15815 Lisp_Object arg;
15816 {
15817 if (NILP (arg))
15818 trace_redisplay_p = !trace_redisplay_p;
15819 else
15820 {
15821 arg = Fprefix_numeric_value (arg);
15822 trace_redisplay_p = XINT (arg) > 0;
15823 }
15824
15825 return Qnil;
15826 }
15827
15828
15829 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15830 doc: /* Like `format', but print result to stderr.
15831 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15832 (nargs, args)
15833 int nargs;
15834 Lisp_Object *args;
15835 {
15836 Lisp_Object s = Fformat (nargs, args);
15837 fprintf (stderr, "%s", SDATA (s));
15838 return Qnil;
15839 }
15840
15841 #endif /* GLYPH_DEBUG */
15842
15843
15844 \f
15845 /***********************************************************************
15846 Building Desired Matrix Rows
15847 ***********************************************************************/
15848
15849 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15850 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15851
15852 static struct glyph_row *
15853 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15854 struct window *w;
15855 Lisp_Object overlay_arrow_string;
15856 {
15857 struct frame *f = XFRAME (WINDOW_FRAME (w));
15858 struct buffer *buffer = XBUFFER (w->buffer);
15859 struct buffer *old = current_buffer;
15860 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15861 int arrow_len = SCHARS (overlay_arrow_string);
15862 const unsigned char *arrow_end = arrow_string + arrow_len;
15863 const unsigned char *p;
15864 struct it it;
15865 int multibyte_p;
15866 int n_glyphs_before;
15867
15868 set_buffer_temp (buffer);
15869 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15870 it.glyph_row->used[TEXT_AREA] = 0;
15871 SET_TEXT_POS (it.position, 0, 0);
15872
15873 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15874 p = arrow_string;
15875 while (p < arrow_end)
15876 {
15877 Lisp_Object face, ilisp;
15878
15879 /* Get the next character. */
15880 if (multibyte_p)
15881 it.c = string_char_and_length (p, arrow_len, &it.len);
15882 else
15883 it.c = *p, it.len = 1;
15884 p += it.len;
15885
15886 /* Get its face. */
15887 ilisp = make_number (p - arrow_string);
15888 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15889 it.face_id = compute_char_face (f, it.c, face);
15890
15891 /* Compute its width, get its glyphs. */
15892 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15893 SET_TEXT_POS (it.position, -1, -1);
15894 PRODUCE_GLYPHS (&it);
15895
15896 /* If this character doesn't fit any more in the line, we have
15897 to remove some glyphs. */
15898 if (it.current_x > it.last_visible_x)
15899 {
15900 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15901 break;
15902 }
15903 }
15904
15905 set_buffer_temp (old);
15906 return it.glyph_row;
15907 }
15908
15909
15910 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15911 glyphs are only inserted for terminal frames since we can't really
15912 win with truncation glyphs when partially visible glyphs are
15913 involved. Which glyphs to insert is determined by
15914 produce_special_glyphs. */
15915
15916 static void
15917 insert_left_trunc_glyphs (it)
15918 struct it *it;
15919 {
15920 struct it truncate_it;
15921 struct glyph *from, *end, *to, *toend;
15922
15923 xassert (!FRAME_WINDOW_P (it->f));
15924
15925 /* Get the truncation glyphs. */
15926 truncate_it = *it;
15927 truncate_it.current_x = 0;
15928 truncate_it.face_id = DEFAULT_FACE_ID;
15929 truncate_it.glyph_row = &scratch_glyph_row;
15930 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15931 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15932 truncate_it.object = make_number (0);
15933 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15934
15935 /* Overwrite glyphs from IT with truncation glyphs. */
15936 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15937 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15938 to = it->glyph_row->glyphs[TEXT_AREA];
15939 toend = to + it->glyph_row->used[TEXT_AREA];
15940
15941 while (from < end)
15942 *to++ = *from++;
15943
15944 /* There may be padding glyphs left over. Overwrite them too. */
15945 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15946 {
15947 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15948 while (from < end)
15949 *to++ = *from++;
15950 }
15951
15952 if (to > toend)
15953 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15954 }
15955
15956
15957 /* Compute the pixel height and width of IT->glyph_row.
15958
15959 Most of the time, ascent and height of a display line will be equal
15960 to the max_ascent and max_height values of the display iterator
15961 structure. This is not the case if
15962
15963 1. We hit ZV without displaying anything. In this case, max_ascent
15964 and max_height will be zero.
15965
15966 2. We have some glyphs that don't contribute to the line height.
15967 (The glyph row flag contributes_to_line_height_p is for future
15968 pixmap extensions).
15969
15970 The first case is easily covered by using default values because in
15971 these cases, the line height does not really matter, except that it
15972 must not be zero. */
15973
15974 static void
15975 compute_line_metrics (it)
15976 struct it *it;
15977 {
15978 struct glyph_row *row = it->glyph_row;
15979 int area, i;
15980
15981 if (FRAME_WINDOW_P (it->f))
15982 {
15983 int i, min_y, max_y;
15984
15985 /* The line may consist of one space only, that was added to
15986 place the cursor on it. If so, the row's height hasn't been
15987 computed yet. */
15988 if (row->height == 0)
15989 {
15990 if (it->max_ascent + it->max_descent == 0)
15991 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15992 row->ascent = it->max_ascent;
15993 row->height = it->max_ascent + it->max_descent;
15994 row->phys_ascent = it->max_phys_ascent;
15995 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15996 row->extra_line_spacing = it->max_extra_line_spacing;
15997 }
15998
15999 /* Compute the width of this line. */
16000 row->pixel_width = row->x;
16001 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16002 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16003
16004 xassert (row->pixel_width >= 0);
16005 xassert (row->ascent >= 0 && row->height > 0);
16006
16007 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16008 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16009
16010 /* If first line's physical ascent is larger than its logical
16011 ascent, use the physical ascent, and make the row taller.
16012 This makes accented characters fully visible. */
16013 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16014 && row->phys_ascent > row->ascent)
16015 {
16016 row->height += row->phys_ascent - row->ascent;
16017 row->ascent = row->phys_ascent;
16018 }
16019
16020 /* Compute how much of the line is visible. */
16021 row->visible_height = row->height;
16022
16023 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16024 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16025
16026 if (row->y < min_y)
16027 row->visible_height -= min_y - row->y;
16028 if (row->y + row->height > max_y)
16029 row->visible_height -= row->y + row->height - max_y;
16030 }
16031 else
16032 {
16033 row->pixel_width = row->used[TEXT_AREA];
16034 if (row->continued_p)
16035 row->pixel_width -= it->continuation_pixel_width;
16036 else if (row->truncated_on_right_p)
16037 row->pixel_width -= it->truncation_pixel_width;
16038 row->ascent = row->phys_ascent = 0;
16039 row->height = row->phys_height = row->visible_height = 1;
16040 row->extra_line_spacing = 0;
16041 }
16042
16043 /* Compute a hash code for this row. */
16044 row->hash = 0;
16045 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16046 for (i = 0; i < row->used[area]; ++i)
16047 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16048 + row->glyphs[area][i].u.val
16049 + row->glyphs[area][i].face_id
16050 + row->glyphs[area][i].padding_p
16051 + (row->glyphs[area][i].type << 2));
16052
16053 it->max_ascent = it->max_descent = 0;
16054 it->max_phys_ascent = it->max_phys_descent = 0;
16055 }
16056
16057
16058 /* Append one space to the glyph row of iterator IT if doing a
16059 window-based redisplay. The space has the same face as
16060 IT->face_id. Value is non-zero if a space was added.
16061
16062 This function is called to make sure that there is always one glyph
16063 at the end of a glyph row that the cursor can be set on under
16064 window-systems. (If there weren't such a glyph we would not know
16065 how wide and tall a box cursor should be displayed).
16066
16067 At the same time this space let's a nicely handle clearing to the
16068 end of the line if the row ends in italic text. */
16069
16070 static int
16071 append_space_for_newline (it, default_face_p)
16072 struct it *it;
16073 int default_face_p;
16074 {
16075 if (FRAME_WINDOW_P (it->f))
16076 {
16077 int n = it->glyph_row->used[TEXT_AREA];
16078
16079 if (it->glyph_row->glyphs[TEXT_AREA] + n
16080 < it->glyph_row->glyphs[1 + TEXT_AREA])
16081 {
16082 /* Save some values that must not be changed.
16083 Must save IT->c and IT->len because otherwise
16084 ITERATOR_AT_END_P wouldn't work anymore after
16085 append_space_for_newline has been called. */
16086 enum display_element_type saved_what = it->what;
16087 int saved_c = it->c, saved_len = it->len;
16088 int saved_x = it->current_x;
16089 int saved_face_id = it->face_id;
16090 struct text_pos saved_pos;
16091 Lisp_Object saved_object;
16092 struct face *face;
16093
16094 saved_object = it->object;
16095 saved_pos = it->position;
16096
16097 it->what = IT_CHARACTER;
16098 bzero (&it->position, sizeof it->position);
16099 it->object = make_number (0);
16100 it->c = ' ';
16101 it->len = 1;
16102
16103 if (default_face_p)
16104 it->face_id = DEFAULT_FACE_ID;
16105 else if (it->face_before_selective_p)
16106 it->face_id = it->saved_face_id;
16107 face = FACE_FROM_ID (it->f, it->face_id);
16108 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16109
16110 PRODUCE_GLYPHS (it);
16111
16112 it->override_ascent = -1;
16113 it->constrain_row_ascent_descent_p = 0;
16114 it->current_x = saved_x;
16115 it->object = saved_object;
16116 it->position = saved_pos;
16117 it->what = saved_what;
16118 it->face_id = saved_face_id;
16119 it->len = saved_len;
16120 it->c = saved_c;
16121 return 1;
16122 }
16123 }
16124
16125 return 0;
16126 }
16127
16128
16129 /* Extend the face of the last glyph in the text area of IT->glyph_row
16130 to the end of the display line. Called from display_line.
16131 If the glyph row is empty, add a space glyph to it so that we
16132 know the face to draw. Set the glyph row flag fill_line_p. */
16133
16134 static void
16135 extend_face_to_end_of_line (it)
16136 struct it *it;
16137 {
16138 struct face *face;
16139 struct frame *f = it->f;
16140
16141 /* If line is already filled, do nothing. */
16142 if (it->current_x >= it->last_visible_x)
16143 return;
16144
16145 /* Face extension extends the background and box of IT->face_id
16146 to the end of the line. If the background equals the background
16147 of the frame, we don't have to do anything. */
16148 if (it->face_before_selective_p)
16149 face = FACE_FROM_ID (it->f, it->saved_face_id);
16150 else
16151 face = FACE_FROM_ID (f, it->face_id);
16152
16153 if (FRAME_WINDOW_P (f)
16154 && it->glyph_row->displays_text_p
16155 && face->box == FACE_NO_BOX
16156 && face->background == FRAME_BACKGROUND_PIXEL (f)
16157 && !face->stipple)
16158 return;
16159
16160 /* Set the glyph row flag indicating that the face of the last glyph
16161 in the text area has to be drawn to the end of the text area. */
16162 it->glyph_row->fill_line_p = 1;
16163
16164 /* If current character of IT is not ASCII, make sure we have the
16165 ASCII face. This will be automatically undone the next time
16166 get_next_display_element returns a multibyte character. Note
16167 that the character will always be single byte in unibyte text. */
16168 if (!ASCII_CHAR_P (it->c))
16169 {
16170 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16171 }
16172
16173 if (FRAME_WINDOW_P (f))
16174 {
16175 /* If the row is empty, add a space with the current face of IT,
16176 so that we know which face to draw. */
16177 if (it->glyph_row->used[TEXT_AREA] == 0)
16178 {
16179 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16180 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16181 it->glyph_row->used[TEXT_AREA] = 1;
16182 }
16183 }
16184 else
16185 {
16186 /* Save some values that must not be changed. */
16187 int saved_x = it->current_x;
16188 struct text_pos saved_pos;
16189 Lisp_Object saved_object;
16190 enum display_element_type saved_what = it->what;
16191 int saved_face_id = it->face_id;
16192
16193 saved_object = it->object;
16194 saved_pos = it->position;
16195
16196 it->what = IT_CHARACTER;
16197 bzero (&it->position, sizeof it->position);
16198 it->object = make_number (0);
16199 it->c = ' ';
16200 it->len = 1;
16201 it->face_id = face->id;
16202
16203 PRODUCE_GLYPHS (it);
16204
16205 while (it->current_x <= it->last_visible_x)
16206 PRODUCE_GLYPHS (it);
16207
16208 /* Don't count these blanks really. It would let us insert a left
16209 truncation glyph below and make us set the cursor on them, maybe. */
16210 it->current_x = saved_x;
16211 it->object = saved_object;
16212 it->position = saved_pos;
16213 it->what = saved_what;
16214 it->face_id = saved_face_id;
16215 }
16216 }
16217
16218
16219 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16220 trailing whitespace. */
16221
16222 static int
16223 trailing_whitespace_p (charpos)
16224 int charpos;
16225 {
16226 int bytepos = CHAR_TO_BYTE (charpos);
16227 int c = 0;
16228
16229 while (bytepos < ZV_BYTE
16230 && (c = FETCH_CHAR (bytepos),
16231 c == ' ' || c == '\t'))
16232 ++bytepos;
16233
16234 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16235 {
16236 if (bytepos != PT_BYTE)
16237 return 1;
16238 }
16239 return 0;
16240 }
16241
16242
16243 /* Highlight trailing whitespace, if any, in ROW. */
16244
16245 void
16246 highlight_trailing_whitespace (f, row)
16247 struct frame *f;
16248 struct glyph_row *row;
16249 {
16250 int used = row->used[TEXT_AREA];
16251
16252 if (used)
16253 {
16254 struct glyph *start = row->glyphs[TEXT_AREA];
16255 struct glyph *glyph = start + used - 1;
16256
16257 /* Skip over glyphs inserted to display the cursor at the
16258 end of a line, for extending the face of the last glyph
16259 to the end of the line on terminals, and for truncation
16260 and continuation glyphs. */
16261 while (glyph >= start
16262 && glyph->type == CHAR_GLYPH
16263 && INTEGERP (glyph->object))
16264 --glyph;
16265
16266 /* If last glyph is a space or stretch, and it's trailing
16267 whitespace, set the face of all trailing whitespace glyphs in
16268 IT->glyph_row to `trailing-whitespace'. */
16269 if (glyph >= start
16270 && BUFFERP (glyph->object)
16271 && (glyph->type == STRETCH_GLYPH
16272 || (glyph->type == CHAR_GLYPH
16273 && glyph->u.ch == ' '))
16274 && trailing_whitespace_p (glyph->charpos))
16275 {
16276 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16277 if (face_id < 0)
16278 return;
16279
16280 while (glyph >= start
16281 && BUFFERP (glyph->object)
16282 && (glyph->type == STRETCH_GLYPH
16283 || (glyph->type == CHAR_GLYPH
16284 && glyph->u.ch == ' ')))
16285 (glyph--)->face_id = face_id;
16286 }
16287 }
16288 }
16289
16290
16291 /* Value is non-zero if glyph row ROW in window W should be
16292 used to hold the cursor. */
16293
16294 static int
16295 cursor_row_p (w, row)
16296 struct window *w;
16297 struct glyph_row *row;
16298 {
16299 int cursor_row_p = 1;
16300
16301 if (PT == MATRIX_ROW_END_CHARPOS (row))
16302 {
16303 /* Suppose the row ends on a string.
16304 Unless the row is continued, that means it ends on a newline
16305 in the string. If it's anything other than a display string
16306 (e.g. a before-string from an overlay), we don't want the
16307 cursor there. (This heuristic seems to give the optimal
16308 behavior for the various types of multi-line strings.) */
16309 if (CHARPOS (row->end.string_pos) >= 0)
16310 {
16311 if (row->continued_p)
16312 cursor_row_p = 1;
16313 else
16314 {
16315 /* Check for `display' property. */
16316 struct glyph *beg = row->glyphs[TEXT_AREA];
16317 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16318 struct glyph *glyph;
16319
16320 cursor_row_p = 0;
16321 for (glyph = end; glyph >= beg; --glyph)
16322 if (STRINGP (glyph->object))
16323 {
16324 Lisp_Object prop
16325 = Fget_char_property (make_number (PT),
16326 Qdisplay, Qnil);
16327 cursor_row_p =
16328 (!NILP (prop)
16329 && display_prop_string_p (prop, glyph->object));
16330 break;
16331 }
16332 }
16333 }
16334 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16335 {
16336 /* If the row ends in middle of a real character,
16337 and the line is continued, we want the cursor here.
16338 That's because MATRIX_ROW_END_CHARPOS would equal
16339 PT if PT is before the character. */
16340 if (!row->ends_in_ellipsis_p)
16341 cursor_row_p = row->continued_p;
16342 else
16343 /* If the row ends in an ellipsis, then
16344 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16345 We want that position to be displayed after the ellipsis. */
16346 cursor_row_p = 0;
16347 }
16348 /* If the row ends at ZV, display the cursor at the end of that
16349 row instead of at the start of the row below. */
16350 else if (row->ends_at_zv_p)
16351 cursor_row_p = 1;
16352 else
16353 cursor_row_p = 0;
16354 }
16355
16356 return cursor_row_p;
16357 }
16358
16359 \f
16360
16361 /* Push the display property PROP so that it will be rendered at the
16362 current position in IT. */
16363
16364 static void
16365 push_display_prop (struct it *it, Lisp_Object prop)
16366 {
16367 push_it (it);
16368
16369 /* Never display a cursor on the prefix. */
16370 it->avoid_cursor_p = 1;
16371
16372 if (STRINGP (prop))
16373 {
16374 if (SCHARS (prop) == 0)
16375 {
16376 pop_it (it);
16377 return;
16378 }
16379
16380 it->string = prop;
16381 it->multibyte_p = STRING_MULTIBYTE (it->string);
16382 it->current.overlay_string_index = -1;
16383 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16384 it->end_charpos = it->string_nchars = SCHARS (it->string);
16385 it->method = GET_FROM_STRING;
16386 it->stop_charpos = 0;
16387 }
16388 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16389 {
16390 it->method = GET_FROM_STRETCH;
16391 it->object = prop;
16392 }
16393 #ifdef HAVE_WINDOW_SYSTEM
16394 else if (IMAGEP (prop))
16395 {
16396 it->what = IT_IMAGE;
16397 it->image_id = lookup_image (it->f, prop);
16398 it->method = GET_FROM_IMAGE;
16399 }
16400 #endif /* HAVE_WINDOW_SYSTEM */
16401 else
16402 {
16403 pop_it (it); /* bogus display property, give up */
16404 return;
16405 }
16406 }
16407
16408 /* Return the character-property PROP at the current position in IT. */
16409
16410 static Lisp_Object
16411 get_it_property (it, prop)
16412 struct it *it;
16413 Lisp_Object prop;
16414 {
16415 Lisp_Object position;
16416
16417 if (STRINGP (it->object))
16418 position = make_number (IT_STRING_CHARPOS (*it));
16419 else if (BUFFERP (it->object))
16420 position = make_number (IT_CHARPOS (*it));
16421 else
16422 return Qnil;
16423
16424 return Fget_char_property (position, prop, it->object);
16425 }
16426
16427 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16428
16429 static void
16430 handle_line_prefix (struct it *it)
16431 {
16432 Lisp_Object prefix;
16433 if (it->continuation_lines_width > 0)
16434 {
16435 prefix = get_it_property (it, Qwrap_prefix);
16436 if (NILP (prefix))
16437 prefix = Vwrap_prefix;
16438 }
16439 else
16440 {
16441 prefix = get_it_property (it, Qline_prefix);
16442 if (NILP (prefix))
16443 prefix = Vline_prefix;
16444 }
16445 if (! NILP (prefix))
16446 {
16447 push_display_prop (it, prefix);
16448 /* If the prefix is wider than the window, and we try to wrap
16449 it, it would acquire its own wrap prefix, and so on till the
16450 iterator stack overflows. So, don't wrap the prefix. */
16451 it->line_wrap = TRUNCATE;
16452 }
16453 }
16454
16455 \f
16456
16457 /* Construct the glyph row IT->glyph_row in the desired matrix of
16458 IT->w from text at the current position of IT. See dispextern.h
16459 for an overview of struct it. Value is non-zero if
16460 IT->glyph_row displays text, as opposed to a line displaying ZV
16461 only. */
16462
16463 static int
16464 display_line (it)
16465 struct it *it;
16466 {
16467 struct glyph_row *row = it->glyph_row;
16468 Lisp_Object overlay_arrow_string;
16469 struct it wrap_it;
16470 int may_wrap = 0, wrap_x;
16471 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16472 int wrap_row_phys_ascent, wrap_row_phys_height;
16473 int wrap_row_extra_line_spacing;
16474
16475 /* We always start displaying at hpos zero even if hscrolled. */
16476 xassert (it->hpos == 0 && it->current_x == 0);
16477
16478 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16479 >= it->w->desired_matrix->nrows)
16480 {
16481 it->w->nrows_scale_factor++;
16482 fonts_changed_p = 1;
16483 return 0;
16484 }
16485
16486 /* Is IT->w showing the region? */
16487 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16488
16489 /* Clear the result glyph row and enable it. */
16490 prepare_desired_row (row);
16491
16492 row->y = it->current_y;
16493 row->start = it->start;
16494 row->continuation_lines_width = it->continuation_lines_width;
16495 row->displays_text_p = 1;
16496 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16497 it->starts_in_middle_of_char_p = 0;
16498
16499 /* Arrange the overlays nicely for our purposes. Usually, we call
16500 display_line on only one line at a time, in which case this
16501 can't really hurt too much, or we call it on lines which appear
16502 one after another in the buffer, in which case all calls to
16503 recenter_overlay_lists but the first will be pretty cheap. */
16504 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16505
16506 /* Move over display elements that are not visible because we are
16507 hscrolled. This may stop at an x-position < IT->first_visible_x
16508 if the first glyph is partially visible or if we hit a line end. */
16509 if (it->current_x < it->first_visible_x)
16510 {
16511 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16512 MOVE_TO_POS | MOVE_TO_X);
16513 }
16514 else
16515 {
16516 /* We only do this when not calling `move_it_in_display_line_to'
16517 above, because move_it_in_display_line_to calls
16518 handle_line_prefix itself. */
16519 handle_line_prefix (it);
16520 }
16521
16522 /* Get the initial row height. This is either the height of the
16523 text hscrolled, if there is any, or zero. */
16524 row->ascent = it->max_ascent;
16525 row->height = it->max_ascent + it->max_descent;
16526 row->phys_ascent = it->max_phys_ascent;
16527 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16528 row->extra_line_spacing = it->max_extra_line_spacing;
16529
16530 /* Loop generating characters. The loop is left with IT on the next
16531 character to display. */
16532 while (1)
16533 {
16534 int n_glyphs_before, hpos_before, x_before;
16535 int x, i, nglyphs;
16536 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16537
16538 /* Retrieve the next thing to display. Value is zero if end of
16539 buffer reached. */
16540 if (!get_next_display_element (it))
16541 {
16542 /* Maybe add a space at the end of this line that is used to
16543 display the cursor there under X. Set the charpos of the
16544 first glyph of blank lines not corresponding to any text
16545 to -1. */
16546 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16547 row->exact_window_width_line_p = 1;
16548 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16549 || row->used[TEXT_AREA] == 0)
16550 {
16551 row->glyphs[TEXT_AREA]->charpos = -1;
16552 row->displays_text_p = 0;
16553
16554 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16555 && (!MINI_WINDOW_P (it->w)
16556 || (minibuf_level && EQ (it->window, minibuf_window))))
16557 row->indicate_empty_line_p = 1;
16558 }
16559
16560 it->continuation_lines_width = 0;
16561 row->ends_at_zv_p = 1;
16562 break;
16563 }
16564
16565 /* Now, get the metrics of what we want to display. This also
16566 generates glyphs in `row' (which is IT->glyph_row). */
16567 n_glyphs_before = row->used[TEXT_AREA];
16568 x = it->current_x;
16569
16570 /* Remember the line height so far in case the next element doesn't
16571 fit on the line. */
16572 if (it->line_wrap != TRUNCATE)
16573 {
16574 ascent = it->max_ascent;
16575 descent = it->max_descent;
16576 phys_ascent = it->max_phys_ascent;
16577 phys_descent = it->max_phys_descent;
16578
16579 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16580 {
16581 if (IT_DISPLAYING_WHITESPACE (it))
16582 may_wrap = 1;
16583 else if (may_wrap)
16584 {
16585 wrap_it = *it;
16586 wrap_x = x;
16587 wrap_row_used = row->used[TEXT_AREA];
16588 wrap_row_ascent = row->ascent;
16589 wrap_row_height = row->height;
16590 wrap_row_phys_ascent = row->phys_ascent;
16591 wrap_row_phys_height = row->phys_height;
16592 wrap_row_extra_line_spacing = row->extra_line_spacing;
16593 may_wrap = 0;
16594 }
16595 }
16596 }
16597
16598 PRODUCE_GLYPHS (it);
16599
16600 /* If this display element was in marginal areas, continue with
16601 the next one. */
16602 if (it->area != TEXT_AREA)
16603 {
16604 row->ascent = max (row->ascent, it->max_ascent);
16605 row->height = max (row->height, it->max_ascent + it->max_descent);
16606 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16607 row->phys_height = max (row->phys_height,
16608 it->max_phys_ascent + it->max_phys_descent);
16609 row->extra_line_spacing = max (row->extra_line_spacing,
16610 it->max_extra_line_spacing);
16611 set_iterator_to_next (it, 1);
16612 continue;
16613 }
16614
16615 /* Does the display element fit on the line? If we truncate
16616 lines, we should draw past the right edge of the window. If
16617 we don't truncate, we want to stop so that we can display the
16618 continuation glyph before the right margin. If lines are
16619 continued, there are two possible strategies for characters
16620 resulting in more than 1 glyph (e.g. tabs): Display as many
16621 glyphs as possible in this line and leave the rest for the
16622 continuation line, or display the whole element in the next
16623 line. Original redisplay did the former, so we do it also. */
16624 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16625 hpos_before = it->hpos;
16626 x_before = x;
16627
16628 if (/* Not a newline. */
16629 nglyphs > 0
16630 /* Glyphs produced fit entirely in the line. */
16631 && it->current_x < it->last_visible_x)
16632 {
16633 it->hpos += nglyphs;
16634 row->ascent = max (row->ascent, it->max_ascent);
16635 row->height = max (row->height, it->max_ascent + it->max_descent);
16636 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16637 row->phys_height = max (row->phys_height,
16638 it->max_phys_ascent + it->max_phys_descent);
16639 row->extra_line_spacing = max (row->extra_line_spacing,
16640 it->max_extra_line_spacing);
16641 if (it->current_x - it->pixel_width < it->first_visible_x)
16642 row->x = x - it->first_visible_x;
16643 }
16644 else
16645 {
16646 int new_x;
16647 struct glyph *glyph;
16648
16649 for (i = 0; i < nglyphs; ++i, x = new_x)
16650 {
16651 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16652 new_x = x + glyph->pixel_width;
16653
16654 if (/* Lines are continued. */
16655 it->line_wrap != TRUNCATE
16656 && (/* Glyph doesn't fit on the line. */
16657 new_x > it->last_visible_x
16658 /* Or it fits exactly on a window system frame. */
16659 || (new_x == it->last_visible_x
16660 && FRAME_WINDOW_P (it->f))))
16661 {
16662 /* End of a continued line. */
16663
16664 if (it->hpos == 0
16665 || (new_x == it->last_visible_x
16666 && FRAME_WINDOW_P (it->f)))
16667 {
16668 /* Current glyph is the only one on the line or
16669 fits exactly on the line. We must continue
16670 the line because we can't draw the cursor
16671 after the glyph. */
16672 row->continued_p = 1;
16673 it->current_x = new_x;
16674 it->continuation_lines_width += new_x;
16675 ++it->hpos;
16676 if (i == nglyphs - 1)
16677 {
16678 /* If line-wrap is on, check if a previous
16679 wrap point was found. */
16680 if (wrap_row_used > 0
16681 /* Even if there is a previous wrap
16682 point, continue the line here as
16683 usual, if (i) the previous character
16684 was a space or tab AND (ii) the
16685 current character is not. */
16686 && (!may_wrap
16687 || IT_DISPLAYING_WHITESPACE (it)))
16688 goto back_to_wrap;
16689
16690 set_iterator_to_next (it, 1);
16691 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16692 {
16693 if (!get_next_display_element (it))
16694 {
16695 row->exact_window_width_line_p = 1;
16696 it->continuation_lines_width = 0;
16697 row->continued_p = 0;
16698 row->ends_at_zv_p = 1;
16699 }
16700 else if (ITERATOR_AT_END_OF_LINE_P (it))
16701 {
16702 row->continued_p = 0;
16703 row->exact_window_width_line_p = 1;
16704 }
16705 }
16706 }
16707 }
16708 else if (CHAR_GLYPH_PADDING_P (*glyph)
16709 && !FRAME_WINDOW_P (it->f))
16710 {
16711 /* A padding glyph that doesn't fit on this line.
16712 This means the whole character doesn't fit
16713 on the line. */
16714 row->used[TEXT_AREA] = n_glyphs_before;
16715
16716 /* Fill the rest of the row with continuation
16717 glyphs like in 20.x. */
16718 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16719 < row->glyphs[1 + TEXT_AREA])
16720 produce_special_glyphs (it, IT_CONTINUATION);
16721
16722 row->continued_p = 1;
16723 it->current_x = x_before;
16724 it->continuation_lines_width += x_before;
16725
16726 /* Restore the height to what it was before the
16727 element not fitting on the line. */
16728 it->max_ascent = ascent;
16729 it->max_descent = descent;
16730 it->max_phys_ascent = phys_ascent;
16731 it->max_phys_descent = phys_descent;
16732 }
16733 else if (wrap_row_used > 0)
16734 {
16735 back_to_wrap:
16736 *it = wrap_it;
16737 it->continuation_lines_width += wrap_x;
16738 row->used[TEXT_AREA] = wrap_row_used;
16739 row->ascent = wrap_row_ascent;
16740 row->height = wrap_row_height;
16741 row->phys_ascent = wrap_row_phys_ascent;
16742 row->phys_height = wrap_row_phys_height;
16743 row->extra_line_spacing = wrap_row_extra_line_spacing;
16744 row->continued_p = 1;
16745 row->ends_at_zv_p = 0;
16746 row->exact_window_width_line_p = 0;
16747 it->continuation_lines_width += x;
16748
16749 /* Make sure that a non-default face is extended
16750 up to the right margin of the window. */
16751 extend_face_to_end_of_line (it);
16752 }
16753 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16754 {
16755 /* A TAB that extends past the right edge of the
16756 window. This produces a single glyph on
16757 window system frames. We leave the glyph in
16758 this row and let it fill the row, but don't
16759 consume the TAB. */
16760 it->continuation_lines_width += it->last_visible_x;
16761 row->ends_in_middle_of_char_p = 1;
16762 row->continued_p = 1;
16763 glyph->pixel_width = it->last_visible_x - x;
16764 it->starts_in_middle_of_char_p = 1;
16765 }
16766 else
16767 {
16768 /* Something other than a TAB that draws past
16769 the right edge of the window. Restore
16770 positions to values before the element. */
16771 row->used[TEXT_AREA] = n_glyphs_before + i;
16772
16773 /* Display continuation glyphs. */
16774 if (!FRAME_WINDOW_P (it->f))
16775 produce_special_glyphs (it, IT_CONTINUATION);
16776 row->continued_p = 1;
16777
16778 it->current_x = x_before;
16779 it->continuation_lines_width += x;
16780 extend_face_to_end_of_line (it);
16781
16782 if (nglyphs > 1 && i > 0)
16783 {
16784 row->ends_in_middle_of_char_p = 1;
16785 it->starts_in_middle_of_char_p = 1;
16786 }
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
16796 break;
16797 }
16798 else if (new_x > it->first_visible_x)
16799 {
16800 /* Increment number of glyphs actually displayed. */
16801 ++it->hpos;
16802
16803 if (x < it->first_visible_x)
16804 /* Glyph is partially visible, i.e. row starts at
16805 negative X position. */
16806 row->x = x - it->first_visible_x;
16807 }
16808 else
16809 {
16810 /* Glyph is completely off the left margin of the
16811 window. This should not happen because of the
16812 move_it_in_display_line at the start of this
16813 function, unless the text display area of the
16814 window is empty. */
16815 xassert (it->first_visible_x <= it->last_visible_x);
16816 }
16817 }
16818
16819 row->ascent = max (row->ascent, it->max_ascent);
16820 row->height = max (row->height, it->max_ascent + it->max_descent);
16821 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16822 row->phys_height = max (row->phys_height,
16823 it->max_phys_ascent + it->max_phys_descent);
16824 row->extra_line_spacing = max (row->extra_line_spacing,
16825 it->max_extra_line_spacing);
16826
16827 /* End of this display line if row is continued. */
16828 if (row->continued_p || row->ends_at_zv_p)
16829 break;
16830 }
16831
16832 at_end_of_line:
16833 /* Is this a line end? If yes, we're also done, after making
16834 sure that a non-default face is extended up to the right
16835 margin of the window. */
16836 if (ITERATOR_AT_END_OF_LINE_P (it))
16837 {
16838 int used_before = row->used[TEXT_AREA];
16839
16840 row->ends_in_newline_from_string_p = STRINGP (it->object);
16841
16842 /* Add a space at the end of the line that is used to
16843 display the cursor there. */
16844 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16845 append_space_for_newline (it, 0);
16846
16847 /* Extend the face to the end of the line. */
16848 extend_face_to_end_of_line (it);
16849
16850 /* Make sure we have the position. */
16851 if (used_before == 0)
16852 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16853
16854 /* Consume the line end. This skips over invisible lines. */
16855 set_iterator_to_next (it, 1);
16856 it->continuation_lines_width = 0;
16857 break;
16858 }
16859
16860 /* Proceed with next display element. Note that this skips
16861 over lines invisible because of selective display. */
16862 set_iterator_to_next (it, 1);
16863
16864 /* If we truncate lines, we are done when the last displayed
16865 glyphs reach past the right margin of the window. */
16866 if (it->line_wrap == TRUNCATE
16867 && (FRAME_WINDOW_P (it->f)
16868 ? (it->current_x >= it->last_visible_x)
16869 : (it->current_x > it->last_visible_x)))
16870 {
16871 /* Maybe add truncation glyphs. */
16872 if (!FRAME_WINDOW_P (it->f))
16873 {
16874 int i, n;
16875
16876 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16877 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16878 break;
16879
16880 for (n = row->used[TEXT_AREA]; i < n; ++i)
16881 {
16882 row->used[TEXT_AREA] = i;
16883 produce_special_glyphs (it, IT_TRUNCATION);
16884 }
16885 }
16886 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16887 {
16888 /* Don't truncate if we can overflow newline into fringe. */
16889 if (!get_next_display_element (it))
16890 {
16891 it->continuation_lines_width = 0;
16892 row->ends_at_zv_p = 1;
16893 row->exact_window_width_line_p = 1;
16894 break;
16895 }
16896 if (ITERATOR_AT_END_OF_LINE_P (it))
16897 {
16898 row->exact_window_width_line_p = 1;
16899 goto at_end_of_line;
16900 }
16901 }
16902
16903 row->truncated_on_right_p = 1;
16904 it->continuation_lines_width = 0;
16905 reseat_at_next_visible_line_start (it, 0);
16906 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16907 it->hpos = hpos_before;
16908 it->current_x = x_before;
16909 break;
16910 }
16911 }
16912
16913 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16914 at the left window margin. */
16915 if (it->first_visible_x
16916 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16917 {
16918 if (!FRAME_WINDOW_P (it->f))
16919 insert_left_trunc_glyphs (it);
16920 row->truncated_on_left_p = 1;
16921 }
16922
16923 /* If the start of this line is the overlay arrow-position, then
16924 mark this glyph row as the one containing the overlay arrow.
16925 This is clearly a mess with variable size fonts. It would be
16926 better to let it be displayed like cursors under X. */
16927 if ((row->displays_text_p || !overlay_arrow_seen)
16928 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16929 !NILP (overlay_arrow_string)))
16930 {
16931 /* Overlay arrow in window redisplay is a fringe bitmap. */
16932 if (STRINGP (overlay_arrow_string))
16933 {
16934 struct glyph_row *arrow_row
16935 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16936 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16937 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16938 struct glyph *p = row->glyphs[TEXT_AREA];
16939 struct glyph *p2, *end;
16940
16941 /* Copy the arrow glyphs. */
16942 while (glyph < arrow_end)
16943 *p++ = *glyph++;
16944
16945 /* Throw away padding glyphs. */
16946 p2 = p;
16947 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16948 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16949 ++p2;
16950 if (p2 > p)
16951 {
16952 while (p2 < end)
16953 *p++ = *p2++;
16954 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16955 }
16956 }
16957 else
16958 {
16959 xassert (INTEGERP (overlay_arrow_string));
16960 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16961 }
16962 overlay_arrow_seen = 1;
16963 }
16964
16965 /* Compute pixel dimensions of this line. */
16966 compute_line_metrics (it);
16967
16968 /* Remember the position at which this line ends. */
16969 row->end = it->current;
16970
16971 /* Record whether this row ends inside an ellipsis. */
16972 row->ends_in_ellipsis_p
16973 = (it->method == GET_FROM_DISPLAY_VECTOR
16974 && it->ellipsis_p);
16975
16976 /* Save fringe bitmaps in this row. */
16977 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16978 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16979 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16980 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16981
16982 it->left_user_fringe_bitmap = 0;
16983 it->left_user_fringe_face_id = 0;
16984 it->right_user_fringe_bitmap = 0;
16985 it->right_user_fringe_face_id = 0;
16986
16987 /* Maybe set the cursor. */
16988 if (it->w->cursor.vpos < 0
16989 && PT >= MATRIX_ROW_START_CHARPOS (row)
16990 && PT <= MATRIX_ROW_END_CHARPOS (row)
16991 && cursor_row_p (it->w, row))
16992 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16993
16994 /* Highlight trailing whitespace. */
16995 if (!NILP (Vshow_trailing_whitespace))
16996 highlight_trailing_whitespace (it->f, it->glyph_row);
16997
16998 /* Prepare for the next line. This line starts horizontally at (X
16999 HPOS) = (0 0). Vertical positions are incremented. As a
17000 convenience for the caller, IT->glyph_row is set to the next
17001 row to be used. */
17002 it->current_x = it->hpos = 0;
17003 it->current_y += row->height;
17004 ++it->vpos;
17005 ++it->glyph_row;
17006 it->start = it->current;
17007 return row->displays_text_p;
17008 }
17009
17010
17011 \f
17012 /***********************************************************************
17013 Menu Bar
17014 ***********************************************************************/
17015
17016 /* Redisplay the menu bar in the frame for window W.
17017
17018 The menu bar of X frames that don't have X toolkit support is
17019 displayed in a special window W->frame->menu_bar_window.
17020
17021 The menu bar of terminal frames is treated specially as far as
17022 glyph matrices are concerned. Menu bar lines are not part of
17023 windows, so the update is done directly on the frame matrix rows
17024 for the menu bar. */
17025
17026 static void
17027 display_menu_bar (w)
17028 struct window *w;
17029 {
17030 struct frame *f = XFRAME (WINDOW_FRAME (w));
17031 struct it it;
17032 Lisp_Object items;
17033 int i;
17034
17035 /* Don't do all this for graphical frames. */
17036 #ifdef HAVE_NTGUI
17037 if (FRAME_W32_P (f))
17038 return;
17039 #endif
17040 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17041 if (FRAME_X_P (f))
17042 return;
17043 #endif
17044
17045 #ifdef HAVE_NS
17046 if (FRAME_NS_P (f))
17047 return;
17048 #endif /* HAVE_NS */
17049
17050 #ifdef USE_X_TOOLKIT
17051 xassert (!FRAME_WINDOW_P (f));
17052 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17053 it.first_visible_x = 0;
17054 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17055 #else /* not USE_X_TOOLKIT */
17056 if (FRAME_WINDOW_P (f))
17057 {
17058 /* Menu bar lines are displayed in the desired matrix of the
17059 dummy window menu_bar_window. */
17060 struct window *menu_w;
17061 xassert (WINDOWP (f->menu_bar_window));
17062 menu_w = XWINDOW (f->menu_bar_window);
17063 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17064 MENU_FACE_ID);
17065 it.first_visible_x = 0;
17066 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17067 }
17068 else
17069 {
17070 /* This is a TTY frame, i.e. character hpos/vpos are used as
17071 pixel x/y. */
17072 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17073 MENU_FACE_ID);
17074 it.first_visible_x = 0;
17075 it.last_visible_x = FRAME_COLS (f);
17076 }
17077 #endif /* not USE_X_TOOLKIT */
17078
17079 if (! mode_line_inverse_video)
17080 /* Force the menu-bar to be displayed in the default face. */
17081 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17082
17083 /* Clear all rows of the menu bar. */
17084 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17085 {
17086 struct glyph_row *row = it.glyph_row + i;
17087 clear_glyph_row (row);
17088 row->enabled_p = 1;
17089 row->full_width_p = 1;
17090 }
17091
17092 /* Display all items of the menu bar. */
17093 items = FRAME_MENU_BAR_ITEMS (it.f);
17094 for (i = 0; i < XVECTOR (items)->size; i += 4)
17095 {
17096 Lisp_Object string;
17097
17098 /* Stop at nil string. */
17099 string = AREF (items, i + 1);
17100 if (NILP (string))
17101 break;
17102
17103 /* Remember where item was displayed. */
17104 ASET (items, i + 3, make_number (it.hpos));
17105
17106 /* Display the item, pad with one space. */
17107 if (it.current_x < it.last_visible_x)
17108 display_string (NULL, string, Qnil, 0, 0, &it,
17109 SCHARS (string) + 1, 0, 0, -1);
17110 }
17111
17112 /* Fill out the line with spaces. */
17113 if (it.current_x < it.last_visible_x)
17114 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17115
17116 /* Compute the total height of the lines. */
17117 compute_line_metrics (&it);
17118 }
17119
17120
17121 \f
17122 /***********************************************************************
17123 Mode Line
17124 ***********************************************************************/
17125
17126 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17127 FORCE is non-zero, redisplay mode lines unconditionally.
17128 Otherwise, redisplay only mode lines that are garbaged. Value is
17129 the number of windows whose mode lines were redisplayed. */
17130
17131 static int
17132 redisplay_mode_lines (window, force)
17133 Lisp_Object window;
17134 int force;
17135 {
17136 int nwindows = 0;
17137
17138 while (!NILP (window))
17139 {
17140 struct window *w = XWINDOW (window);
17141
17142 if (WINDOWP (w->hchild))
17143 nwindows += redisplay_mode_lines (w->hchild, force);
17144 else if (WINDOWP (w->vchild))
17145 nwindows += redisplay_mode_lines (w->vchild, force);
17146 else if (force
17147 || FRAME_GARBAGED_P (XFRAME (w->frame))
17148 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17149 {
17150 struct text_pos lpoint;
17151 struct buffer *old = current_buffer;
17152
17153 /* Set the window's buffer for the mode line display. */
17154 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17155 set_buffer_internal_1 (XBUFFER (w->buffer));
17156
17157 /* Point refers normally to the selected window. For any
17158 other window, set up appropriate value. */
17159 if (!EQ (window, selected_window))
17160 {
17161 struct text_pos pt;
17162
17163 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17164 if (CHARPOS (pt) < BEGV)
17165 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17166 else if (CHARPOS (pt) > (ZV - 1))
17167 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17168 else
17169 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17170 }
17171
17172 /* Display mode lines. */
17173 clear_glyph_matrix (w->desired_matrix);
17174 if (display_mode_lines (w))
17175 {
17176 ++nwindows;
17177 w->must_be_updated_p = 1;
17178 }
17179
17180 /* Restore old settings. */
17181 set_buffer_internal_1 (old);
17182 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17183 }
17184
17185 window = w->next;
17186 }
17187
17188 return nwindows;
17189 }
17190
17191
17192 /* Display the mode and/or top line of window W. Value is the number
17193 of mode lines displayed. */
17194
17195 static int
17196 display_mode_lines (w)
17197 struct window *w;
17198 {
17199 Lisp_Object old_selected_window, old_selected_frame;
17200 int n = 0;
17201
17202 old_selected_frame = selected_frame;
17203 selected_frame = w->frame;
17204 old_selected_window = selected_window;
17205 XSETWINDOW (selected_window, w);
17206
17207 /* These will be set while the mode line specs are processed. */
17208 line_number_displayed = 0;
17209 w->column_number_displayed = Qnil;
17210
17211 if (WINDOW_WANTS_MODELINE_P (w))
17212 {
17213 struct window *sel_w = XWINDOW (old_selected_window);
17214
17215 /* Select mode line face based on the real selected window. */
17216 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17217 current_buffer->mode_line_format);
17218 ++n;
17219 }
17220
17221 if (WINDOW_WANTS_HEADER_LINE_P (w))
17222 {
17223 display_mode_line (w, HEADER_LINE_FACE_ID,
17224 current_buffer->header_line_format);
17225 ++n;
17226 }
17227
17228 selected_frame = old_selected_frame;
17229 selected_window = old_selected_window;
17230 return n;
17231 }
17232
17233
17234 /* Display mode or top line of window W. FACE_ID specifies which line
17235 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17236 FORMAT is the mode line format to display. Value is the pixel
17237 height of the mode line displayed. */
17238
17239 static int
17240 display_mode_line (w, face_id, format)
17241 struct window *w;
17242 enum face_id face_id;
17243 Lisp_Object format;
17244 {
17245 struct it it;
17246 struct face *face;
17247 int count = SPECPDL_INDEX ();
17248
17249 init_iterator (&it, w, -1, -1, NULL, face_id);
17250 /* Don't extend on a previously drawn mode-line.
17251 This may happen if called from pos_visible_p. */
17252 it.glyph_row->enabled_p = 0;
17253 prepare_desired_row (it.glyph_row);
17254
17255 it.glyph_row->mode_line_p = 1;
17256
17257 if (! mode_line_inverse_video)
17258 /* Force the mode-line to be displayed in the default face. */
17259 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17260
17261 record_unwind_protect (unwind_format_mode_line,
17262 format_mode_line_unwind_data (NULL, Qnil, 0));
17263
17264 mode_line_target = MODE_LINE_DISPLAY;
17265
17266 /* Temporarily make frame's keyboard the current kboard so that
17267 kboard-local variables in the mode_line_format will get the right
17268 values. */
17269 push_kboard (FRAME_KBOARD (it.f));
17270 record_unwind_save_match_data ();
17271 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17272 pop_kboard ();
17273
17274 unbind_to (count, Qnil);
17275
17276 /* Fill up with spaces. */
17277 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17278
17279 compute_line_metrics (&it);
17280 it.glyph_row->full_width_p = 1;
17281 it.glyph_row->continued_p = 0;
17282 it.glyph_row->truncated_on_left_p = 0;
17283 it.glyph_row->truncated_on_right_p = 0;
17284
17285 /* Make a 3D mode-line have a shadow at its right end. */
17286 face = FACE_FROM_ID (it.f, face_id);
17287 extend_face_to_end_of_line (&it);
17288 if (face->box != FACE_NO_BOX)
17289 {
17290 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17291 + it.glyph_row->used[TEXT_AREA] - 1);
17292 last->right_box_line_p = 1;
17293 }
17294
17295 return it.glyph_row->height;
17296 }
17297
17298 /* Move element ELT in LIST to the front of LIST.
17299 Return the updated list. */
17300
17301 static Lisp_Object
17302 move_elt_to_front (elt, list)
17303 Lisp_Object elt, list;
17304 {
17305 register Lisp_Object tail, prev;
17306 register Lisp_Object tem;
17307
17308 tail = list;
17309 prev = Qnil;
17310 while (CONSP (tail))
17311 {
17312 tem = XCAR (tail);
17313
17314 if (EQ (elt, tem))
17315 {
17316 /* Splice out the link TAIL. */
17317 if (NILP (prev))
17318 list = XCDR (tail);
17319 else
17320 Fsetcdr (prev, XCDR (tail));
17321
17322 /* Now make it the first. */
17323 Fsetcdr (tail, list);
17324 return tail;
17325 }
17326 else
17327 prev = tail;
17328 tail = XCDR (tail);
17329 QUIT;
17330 }
17331
17332 /* Not found--return unchanged LIST. */
17333 return list;
17334 }
17335
17336 /* Contribute ELT to the mode line for window IT->w. How it
17337 translates into text depends on its data type.
17338
17339 IT describes the display environment in which we display, as usual.
17340
17341 DEPTH is the depth in recursion. It is used to prevent
17342 infinite recursion here.
17343
17344 FIELD_WIDTH is the number of characters the display of ELT should
17345 occupy in the mode line, and PRECISION is the maximum number of
17346 characters to display from ELT's representation. See
17347 display_string for details.
17348
17349 Returns the hpos of the end of the text generated by ELT.
17350
17351 PROPS is a property list to add to any string we encounter.
17352
17353 If RISKY is nonzero, remove (disregard) any properties in any string
17354 we encounter, and ignore :eval and :propertize.
17355
17356 The global variable `mode_line_target' determines whether the
17357 output is passed to `store_mode_line_noprop',
17358 `store_mode_line_string', or `display_string'. */
17359
17360 static int
17361 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17362 struct it *it;
17363 int depth;
17364 int field_width, precision;
17365 Lisp_Object elt, props;
17366 int risky;
17367 {
17368 int n = 0, field, prec;
17369 int literal = 0;
17370
17371 tail_recurse:
17372 if (depth > 100)
17373 elt = build_string ("*too-deep*");
17374
17375 depth++;
17376
17377 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17378 {
17379 case Lisp_String:
17380 {
17381 /* A string: output it and check for %-constructs within it. */
17382 unsigned char c;
17383 int offset = 0;
17384
17385 if (SCHARS (elt) > 0
17386 && (!NILP (props) || risky))
17387 {
17388 Lisp_Object oprops, aelt;
17389 oprops = Ftext_properties_at (make_number (0), elt);
17390
17391 /* If the starting string's properties are not what
17392 we want, translate the string. Also, if the string
17393 is risky, do that anyway. */
17394
17395 if (NILP (Fequal (props, oprops)) || risky)
17396 {
17397 /* If the starting string has properties,
17398 merge the specified ones onto the existing ones. */
17399 if (! NILP (oprops) && !risky)
17400 {
17401 Lisp_Object tem;
17402
17403 oprops = Fcopy_sequence (oprops);
17404 tem = props;
17405 while (CONSP (tem))
17406 {
17407 oprops = Fplist_put (oprops, XCAR (tem),
17408 XCAR (XCDR (tem)));
17409 tem = XCDR (XCDR (tem));
17410 }
17411 props = oprops;
17412 }
17413
17414 aelt = Fassoc (elt, mode_line_proptrans_alist);
17415 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17416 {
17417 /* AELT is what we want. Move it to the front
17418 without consing. */
17419 elt = XCAR (aelt);
17420 mode_line_proptrans_alist
17421 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17422 }
17423 else
17424 {
17425 Lisp_Object tem;
17426
17427 /* If AELT has the wrong props, it is useless.
17428 so get rid of it. */
17429 if (! NILP (aelt))
17430 mode_line_proptrans_alist
17431 = Fdelq (aelt, mode_line_proptrans_alist);
17432
17433 elt = Fcopy_sequence (elt);
17434 Fset_text_properties (make_number (0), Flength (elt),
17435 props, elt);
17436 /* Add this item to mode_line_proptrans_alist. */
17437 mode_line_proptrans_alist
17438 = Fcons (Fcons (elt, props),
17439 mode_line_proptrans_alist);
17440 /* Truncate mode_line_proptrans_alist
17441 to at most 50 elements. */
17442 tem = Fnthcdr (make_number (50),
17443 mode_line_proptrans_alist);
17444 if (! NILP (tem))
17445 XSETCDR (tem, Qnil);
17446 }
17447 }
17448 }
17449
17450 offset = 0;
17451
17452 if (literal)
17453 {
17454 prec = precision - n;
17455 switch (mode_line_target)
17456 {
17457 case MODE_LINE_NOPROP:
17458 case MODE_LINE_TITLE:
17459 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17460 break;
17461 case MODE_LINE_STRING:
17462 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17463 break;
17464 case MODE_LINE_DISPLAY:
17465 n += display_string (NULL, elt, Qnil, 0, 0, it,
17466 0, prec, 0, STRING_MULTIBYTE (elt));
17467 break;
17468 }
17469
17470 break;
17471 }
17472
17473 /* Handle the non-literal case. */
17474
17475 while ((precision <= 0 || n < precision)
17476 && SREF (elt, offset) != 0
17477 && (mode_line_target != MODE_LINE_DISPLAY
17478 || it->current_x < it->last_visible_x))
17479 {
17480 int last_offset = offset;
17481
17482 /* Advance to end of string or next format specifier. */
17483 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17484 ;
17485
17486 if (offset - 1 != last_offset)
17487 {
17488 int nchars, nbytes;
17489
17490 /* Output to end of string or up to '%'. Field width
17491 is length of string. Don't output more than
17492 PRECISION allows us. */
17493 offset--;
17494
17495 prec = c_string_width (SDATA (elt) + last_offset,
17496 offset - last_offset, precision - n,
17497 &nchars, &nbytes);
17498
17499 switch (mode_line_target)
17500 {
17501 case MODE_LINE_NOPROP:
17502 case MODE_LINE_TITLE:
17503 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17504 break;
17505 case MODE_LINE_STRING:
17506 {
17507 int bytepos = last_offset;
17508 int charpos = string_byte_to_char (elt, bytepos);
17509 int endpos = (precision <= 0
17510 ? string_byte_to_char (elt, offset)
17511 : charpos + nchars);
17512
17513 n += store_mode_line_string (NULL,
17514 Fsubstring (elt, make_number (charpos),
17515 make_number (endpos)),
17516 0, 0, 0, Qnil);
17517 }
17518 break;
17519 case MODE_LINE_DISPLAY:
17520 {
17521 int bytepos = last_offset;
17522 int charpos = string_byte_to_char (elt, bytepos);
17523
17524 if (precision <= 0)
17525 nchars = string_byte_to_char (elt, offset) - charpos;
17526 n += display_string (NULL, elt, Qnil, 0, charpos,
17527 it, 0, nchars, 0,
17528 STRING_MULTIBYTE (elt));
17529 }
17530 break;
17531 }
17532 }
17533 else /* c == '%' */
17534 {
17535 int percent_position = offset;
17536
17537 /* Get the specified minimum width. Zero means
17538 don't pad. */
17539 field = 0;
17540 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17541 field = field * 10 + c - '0';
17542
17543 /* Don't pad beyond the total padding allowed. */
17544 if (field_width - n > 0 && field > field_width - n)
17545 field = field_width - n;
17546
17547 /* Note that either PRECISION <= 0 or N < PRECISION. */
17548 prec = precision - n;
17549
17550 if (c == 'M')
17551 n += display_mode_element (it, depth, field, prec,
17552 Vglobal_mode_string, props,
17553 risky);
17554 else if (c != 0)
17555 {
17556 int multibyte;
17557 int bytepos, charpos;
17558 unsigned char *spec;
17559
17560 bytepos = percent_position;
17561 charpos = (STRING_MULTIBYTE (elt)
17562 ? string_byte_to_char (elt, bytepos)
17563 : bytepos);
17564 spec
17565 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17566
17567 switch (mode_line_target)
17568 {
17569 case MODE_LINE_NOPROP:
17570 case MODE_LINE_TITLE:
17571 n += store_mode_line_noprop (spec, field, prec);
17572 break;
17573 case MODE_LINE_STRING:
17574 {
17575 int len = strlen (spec);
17576 Lisp_Object tem = make_string (spec, len);
17577 props = Ftext_properties_at (make_number (charpos), elt);
17578 /* Should only keep face property in props */
17579 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17580 }
17581 break;
17582 case MODE_LINE_DISPLAY:
17583 {
17584 int nglyphs_before, nwritten;
17585
17586 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17587 nwritten = display_string (spec, Qnil, elt,
17588 charpos, 0, it,
17589 field, prec, 0,
17590 multibyte);
17591
17592 /* Assign to the glyphs written above the
17593 string where the `%x' came from, position
17594 of the `%'. */
17595 if (nwritten > 0)
17596 {
17597 struct glyph *glyph
17598 = (it->glyph_row->glyphs[TEXT_AREA]
17599 + nglyphs_before);
17600 int i;
17601
17602 for (i = 0; i < nwritten; ++i)
17603 {
17604 glyph[i].object = elt;
17605 glyph[i].charpos = charpos;
17606 }
17607
17608 n += nwritten;
17609 }
17610 }
17611 break;
17612 }
17613 }
17614 else /* c == 0 */
17615 break;
17616 }
17617 }
17618 }
17619 break;
17620
17621 case Lisp_Symbol:
17622 /* A symbol: process the value of the symbol recursively
17623 as if it appeared here directly. Avoid error if symbol void.
17624 Special case: if value of symbol is a string, output the string
17625 literally. */
17626 {
17627 register Lisp_Object tem;
17628
17629 /* If the variable is not marked as risky to set
17630 then its contents are risky to use. */
17631 if (NILP (Fget (elt, Qrisky_local_variable)))
17632 risky = 1;
17633
17634 tem = Fboundp (elt);
17635 if (!NILP (tem))
17636 {
17637 tem = Fsymbol_value (elt);
17638 /* If value is a string, output that string literally:
17639 don't check for % within it. */
17640 if (STRINGP (tem))
17641 literal = 1;
17642
17643 if (!EQ (tem, elt))
17644 {
17645 /* Give up right away for nil or t. */
17646 elt = tem;
17647 goto tail_recurse;
17648 }
17649 }
17650 }
17651 break;
17652
17653 case Lisp_Cons:
17654 {
17655 register Lisp_Object car, tem;
17656
17657 /* A cons cell: five distinct cases.
17658 If first element is :eval or :propertize, do something special.
17659 If first element is a string or a cons, process all the elements
17660 and effectively concatenate them.
17661 If first element is a negative number, truncate displaying cdr to
17662 at most that many characters. If positive, pad (with spaces)
17663 to at least that many characters.
17664 If first element is a symbol, process the cadr or caddr recursively
17665 according to whether the symbol's value is non-nil or nil. */
17666 car = XCAR (elt);
17667 if (EQ (car, QCeval))
17668 {
17669 /* An element of the form (:eval FORM) means evaluate FORM
17670 and use the result as mode line elements. */
17671
17672 if (risky)
17673 break;
17674
17675 if (CONSP (XCDR (elt)))
17676 {
17677 Lisp_Object spec;
17678 spec = safe_eval (XCAR (XCDR (elt)));
17679 n += display_mode_element (it, depth, field_width - n,
17680 precision - n, spec, props,
17681 risky);
17682 }
17683 }
17684 else if (EQ (car, QCpropertize))
17685 {
17686 /* An element of the form (:propertize ELT PROPS...)
17687 means display ELT but applying properties PROPS. */
17688
17689 if (risky)
17690 break;
17691
17692 if (CONSP (XCDR (elt)))
17693 n += display_mode_element (it, depth, field_width - n,
17694 precision - n, XCAR (XCDR (elt)),
17695 XCDR (XCDR (elt)), risky);
17696 }
17697 else if (SYMBOLP (car))
17698 {
17699 tem = Fboundp (car);
17700 elt = XCDR (elt);
17701 if (!CONSP (elt))
17702 goto invalid;
17703 /* elt is now the cdr, and we know it is a cons cell.
17704 Use its car if CAR has a non-nil value. */
17705 if (!NILP (tem))
17706 {
17707 tem = Fsymbol_value (car);
17708 if (!NILP (tem))
17709 {
17710 elt = XCAR (elt);
17711 goto tail_recurse;
17712 }
17713 }
17714 /* Symbol's value is nil (or symbol is unbound)
17715 Get the cddr of the original list
17716 and if possible find the caddr and use that. */
17717 elt = XCDR (elt);
17718 if (NILP (elt))
17719 break;
17720 else if (!CONSP (elt))
17721 goto invalid;
17722 elt = XCAR (elt);
17723 goto tail_recurse;
17724 }
17725 else if (INTEGERP (car))
17726 {
17727 register int lim = XINT (car);
17728 elt = XCDR (elt);
17729 if (lim < 0)
17730 {
17731 /* Negative int means reduce maximum width. */
17732 if (precision <= 0)
17733 precision = -lim;
17734 else
17735 precision = min (precision, -lim);
17736 }
17737 else if (lim > 0)
17738 {
17739 /* Padding specified. Don't let it be more than
17740 current maximum. */
17741 if (precision > 0)
17742 lim = min (precision, lim);
17743
17744 /* If that's more padding than already wanted, queue it.
17745 But don't reduce padding already specified even if
17746 that is beyond the current truncation point. */
17747 field_width = max (lim, field_width);
17748 }
17749 goto tail_recurse;
17750 }
17751 else if (STRINGP (car) || CONSP (car))
17752 {
17753 Lisp_Object halftail = elt;
17754 int len = 0;
17755
17756 while (CONSP (elt)
17757 && (precision <= 0 || n < precision))
17758 {
17759 n += display_mode_element (it, depth,
17760 /* Do padding only after the last
17761 element in the list. */
17762 (! CONSP (XCDR (elt))
17763 ? field_width - n
17764 : 0),
17765 precision - n, XCAR (elt),
17766 props, risky);
17767 elt = XCDR (elt);
17768 len++;
17769 if ((len & 1) == 0)
17770 halftail = XCDR (halftail);
17771 /* Check for cycle. */
17772 if (EQ (halftail, elt))
17773 break;
17774 }
17775 }
17776 }
17777 break;
17778
17779 default:
17780 invalid:
17781 elt = build_string ("*invalid*");
17782 goto tail_recurse;
17783 }
17784
17785 /* Pad to FIELD_WIDTH. */
17786 if (field_width > 0 && n < field_width)
17787 {
17788 switch (mode_line_target)
17789 {
17790 case MODE_LINE_NOPROP:
17791 case MODE_LINE_TITLE:
17792 n += store_mode_line_noprop ("", field_width - n, 0);
17793 break;
17794 case MODE_LINE_STRING:
17795 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17796 break;
17797 case MODE_LINE_DISPLAY:
17798 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17799 0, 0, 0);
17800 break;
17801 }
17802 }
17803
17804 return n;
17805 }
17806
17807 /* Store a mode-line string element in mode_line_string_list.
17808
17809 If STRING is non-null, display that C string. Otherwise, the Lisp
17810 string LISP_STRING is displayed.
17811
17812 FIELD_WIDTH is the minimum number of output glyphs to produce.
17813 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17814 with spaces. FIELD_WIDTH <= 0 means don't pad.
17815
17816 PRECISION is the maximum number of characters to output from
17817 STRING. PRECISION <= 0 means don't truncate the string.
17818
17819 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17820 properties to the string.
17821
17822 PROPS are the properties to add to the string.
17823 The mode_line_string_face face property is always added to the string.
17824 */
17825
17826 static int
17827 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17828 char *string;
17829 Lisp_Object lisp_string;
17830 int copy_string;
17831 int field_width;
17832 int precision;
17833 Lisp_Object props;
17834 {
17835 int len;
17836 int n = 0;
17837
17838 if (string != NULL)
17839 {
17840 len = strlen (string);
17841 if (precision > 0 && len > precision)
17842 len = precision;
17843 lisp_string = make_string (string, len);
17844 if (NILP (props))
17845 props = mode_line_string_face_prop;
17846 else if (!NILP (mode_line_string_face))
17847 {
17848 Lisp_Object face = Fplist_get (props, Qface);
17849 props = Fcopy_sequence (props);
17850 if (NILP (face))
17851 face = mode_line_string_face;
17852 else
17853 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17854 props = Fplist_put (props, Qface, face);
17855 }
17856 Fadd_text_properties (make_number (0), make_number (len),
17857 props, lisp_string);
17858 }
17859 else
17860 {
17861 len = XFASTINT (Flength (lisp_string));
17862 if (precision > 0 && len > precision)
17863 {
17864 len = precision;
17865 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17866 precision = -1;
17867 }
17868 if (!NILP (mode_line_string_face))
17869 {
17870 Lisp_Object face;
17871 if (NILP (props))
17872 props = Ftext_properties_at (make_number (0), lisp_string);
17873 face = Fplist_get (props, Qface);
17874 if (NILP (face))
17875 face = mode_line_string_face;
17876 else
17877 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17878 props = Fcons (Qface, Fcons (face, Qnil));
17879 if (copy_string)
17880 lisp_string = Fcopy_sequence (lisp_string);
17881 }
17882 if (!NILP (props))
17883 Fadd_text_properties (make_number (0), make_number (len),
17884 props, lisp_string);
17885 }
17886
17887 if (len > 0)
17888 {
17889 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17890 n += len;
17891 }
17892
17893 if (field_width > len)
17894 {
17895 field_width -= len;
17896 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17897 if (!NILP (props))
17898 Fadd_text_properties (make_number (0), make_number (field_width),
17899 props, lisp_string);
17900 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17901 n += field_width;
17902 }
17903
17904 return n;
17905 }
17906
17907
17908 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17909 1, 4, 0,
17910 doc: /* Format a string out of a mode line format specification.
17911 First arg FORMAT specifies the mode line format (see `mode-line-format'
17912 for details) to use.
17913
17914 Optional second arg FACE specifies the face property to put
17915 on all characters for which no face is specified.
17916 The value t means whatever face the window's mode line currently uses
17917 \(either `mode-line' or `mode-line-inactive', depending).
17918 A value of nil means the default is no face property.
17919 If FACE is an integer, the value string has no text properties.
17920
17921 Optional third and fourth args WINDOW and BUFFER specify the window
17922 and buffer to use as the context for the formatting (defaults
17923 are the selected window and the window's buffer). */)
17924 (format, face, window, buffer)
17925 Lisp_Object format, face, window, buffer;
17926 {
17927 struct it it;
17928 int len;
17929 struct window *w;
17930 struct buffer *old_buffer = NULL;
17931 int face_id = -1;
17932 int no_props = INTEGERP (face);
17933 int count = SPECPDL_INDEX ();
17934 Lisp_Object str;
17935 int string_start = 0;
17936
17937 if (NILP (window))
17938 window = selected_window;
17939 CHECK_WINDOW (window);
17940 w = XWINDOW (window);
17941
17942 if (NILP (buffer))
17943 buffer = w->buffer;
17944 CHECK_BUFFER (buffer);
17945
17946 /* Make formatting the modeline a non-op when noninteractive, otherwise
17947 there will be problems later caused by a partially initialized frame. */
17948 if (NILP (format) || noninteractive)
17949 return empty_unibyte_string;
17950
17951 if (no_props)
17952 face = Qnil;
17953
17954 if (!NILP (face))
17955 {
17956 if (EQ (face, Qt))
17957 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17958 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17959 }
17960
17961 if (face_id < 0)
17962 face_id = DEFAULT_FACE_ID;
17963
17964 if (XBUFFER (buffer) != current_buffer)
17965 old_buffer = current_buffer;
17966
17967 /* Save things including mode_line_proptrans_alist,
17968 and set that to nil so that we don't alter the outer value. */
17969 record_unwind_protect (unwind_format_mode_line,
17970 format_mode_line_unwind_data
17971 (old_buffer, selected_window, 1));
17972 mode_line_proptrans_alist = Qnil;
17973
17974 Fselect_window (window, Qt);
17975 if (old_buffer)
17976 set_buffer_internal_1 (XBUFFER (buffer));
17977
17978 init_iterator (&it, w, -1, -1, NULL, face_id);
17979
17980 if (no_props)
17981 {
17982 mode_line_target = MODE_LINE_NOPROP;
17983 mode_line_string_face_prop = Qnil;
17984 mode_line_string_list = Qnil;
17985 string_start = MODE_LINE_NOPROP_LEN (0);
17986 }
17987 else
17988 {
17989 mode_line_target = MODE_LINE_STRING;
17990 mode_line_string_list = Qnil;
17991 mode_line_string_face = face;
17992 mode_line_string_face_prop
17993 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17994 }
17995
17996 push_kboard (FRAME_KBOARD (it.f));
17997 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17998 pop_kboard ();
17999
18000 if (no_props)
18001 {
18002 len = MODE_LINE_NOPROP_LEN (string_start);
18003 str = make_string (mode_line_noprop_buf + string_start, len);
18004 }
18005 else
18006 {
18007 mode_line_string_list = Fnreverse (mode_line_string_list);
18008 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18009 empty_unibyte_string);
18010 }
18011
18012 unbind_to (count, Qnil);
18013 return str;
18014 }
18015
18016 /* Write a null-terminated, right justified decimal representation of
18017 the positive integer D to BUF using a minimal field width WIDTH. */
18018
18019 static void
18020 pint2str (buf, width, d)
18021 register char *buf;
18022 register int width;
18023 register int d;
18024 {
18025 register char *p = buf;
18026
18027 if (d <= 0)
18028 *p++ = '0';
18029 else
18030 {
18031 while (d > 0)
18032 {
18033 *p++ = d % 10 + '0';
18034 d /= 10;
18035 }
18036 }
18037
18038 for (width -= (int) (p - buf); width > 0; --width)
18039 *p++ = ' ';
18040 *p-- = '\0';
18041 while (p > buf)
18042 {
18043 d = *buf;
18044 *buf++ = *p;
18045 *p-- = d;
18046 }
18047 }
18048
18049 /* Write a null-terminated, right justified decimal and "human
18050 readable" representation of the nonnegative integer D to BUF using
18051 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18052
18053 static const char power_letter[] =
18054 {
18055 0, /* not used */
18056 'k', /* kilo */
18057 'M', /* mega */
18058 'G', /* giga */
18059 'T', /* tera */
18060 'P', /* peta */
18061 'E', /* exa */
18062 'Z', /* zetta */
18063 'Y' /* yotta */
18064 };
18065
18066 static void
18067 pint2hrstr (buf, width, d)
18068 char *buf;
18069 int width;
18070 int d;
18071 {
18072 /* We aim to represent the nonnegative integer D as
18073 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18074 int quotient = d;
18075 int remainder = 0;
18076 /* -1 means: do not use TENTHS. */
18077 int tenths = -1;
18078 int exponent = 0;
18079
18080 /* Length of QUOTIENT.TENTHS as a string. */
18081 int length;
18082
18083 char * psuffix;
18084 char * p;
18085
18086 if (1000 <= quotient)
18087 {
18088 /* Scale to the appropriate EXPONENT. */
18089 do
18090 {
18091 remainder = quotient % 1000;
18092 quotient /= 1000;
18093 exponent++;
18094 }
18095 while (1000 <= quotient);
18096
18097 /* Round to nearest and decide whether to use TENTHS or not. */
18098 if (quotient <= 9)
18099 {
18100 tenths = remainder / 100;
18101 if (50 <= remainder % 100)
18102 {
18103 if (tenths < 9)
18104 tenths++;
18105 else
18106 {
18107 quotient++;
18108 if (quotient == 10)
18109 tenths = -1;
18110 else
18111 tenths = 0;
18112 }
18113 }
18114 }
18115 else
18116 if (500 <= remainder)
18117 {
18118 if (quotient < 999)
18119 quotient++;
18120 else
18121 {
18122 quotient = 1;
18123 exponent++;
18124 tenths = 0;
18125 }
18126 }
18127 }
18128
18129 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18130 if (tenths == -1 && quotient <= 99)
18131 if (quotient <= 9)
18132 length = 1;
18133 else
18134 length = 2;
18135 else
18136 length = 3;
18137 p = psuffix = buf + max (width, length);
18138
18139 /* Print EXPONENT. */
18140 if (exponent)
18141 *psuffix++ = power_letter[exponent];
18142 *psuffix = '\0';
18143
18144 /* Print TENTHS. */
18145 if (tenths >= 0)
18146 {
18147 *--p = '0' + tenths;
18148 *--p = '.';
18149 }
18150
18151 /* Print QUOTIENT. */
18152 do
18153 {
18154 int digit = quotient % 10;
18155 *--p = '0' + digit;
18156 }
18157 while ((quotient /= 10) != 0);
18158
18159 /* Print leading spaces. */
18160 while (buf < p)
18161 *--p = ' ';
18162 }
18163
18164 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18165 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18166 type of CODING_SYSTEM. Return updated pointer into BUF. */
18167
18168 static unsigned char invalid_eol_type[] = "(*invalid*)";
18169
18170 static char *
18171 decode_mode_spec_coding (coding_system, buf, eol_flag)
18172 Lisp_Object coding_system;
18173 register char *buf;
18174 int eol_flag;
18175 {
18176 Lisp_Object val;
18177 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18178 const unsigned char *eol_str;
18179 int eol_str_len;
18180 /* The EOL conversion we are using. */
18181 Lisp_Object eoltype;
18182
18183 val = CODING_SYSTEM_SPEC (coding_system);
18184 eoltype = Qnil;
18185
18186 if (!VECTORP (val)) /* Not yet decided. */
18187 {
18188 if (multibyte)
18189 *buf++ = '-';
18190 if (eol_flag)
18191 eoltype = eol_mnemonic_undecided;
18192 /* Don't mention EOL conversion if it isn't decided. */
18193 }
18194 else
18195 {
18196 Lisp_Object attrs;
18197 Lisp_Object eolvalue;
18198
18199 attrs = AREF (val, 0);
18200 eolvalue = AREF (val, 2);
18201
18202 if (multibyte)
18203 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18204
18205 if (eol_flag)
18206 {
18207 /* The EOL conversion that is normal on this system. */
18208
18209 if (NILP (eolvalue)) /* Not yet decided. */
18210 eoltype = eol_mnemonic_undecided;
18211 else if (VECTORP (eolvalue)) /* Not yet decided. */
18212 eoltype = eol_mnemonic_undecided;
18213 else /* eolvalue is Qunix, Qdos, or Qmac. */
18214 eoltype = (EQ (eolvalue, Qunix)
18215 ? eol_mnemonic_unix
18216 : (EQ (eolvalue, Qdos) == 1
18217 ? eol_mnemonic_dos : eol_mnemonic_mac));
18218 }
18219 }
18220
18221 if (eol_flag)
18222 {
18223 /* Mention the EOL conversion if it is not the usual one. */
18224 if (STRINGP (eoltype))
18225 {
18226 eol_str = SDATA (eoltype);
18227 eol_str_len = SBYTES (eoltype);
18228 }
18229 else if (CHARACTERP (eoltype))
18230 {
18231 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18232 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18233 eol_str = tmp;
18234 }
18235 else
18236 {
18237 eol_str = invalid_eol_type;
18238 eol_str_len = sizeof (invalid_eol_type) - 1;
18239 }
18240 bcopy (eol_str, buf, eol_str_len);
18241 buf += eol_str_len;
18242 }
18243
18244 return buf;
18245 }
18246
18247 /* Return a string for the output of a mode line %-spec for window W,
18248 generated by character C. PRECISION >= 0 means don't return a
18249 string longer than that value. FIELD_WIDTH > 0 means pad the
18250 string returned with spaces to that value. Return 1 in *MULTIBYTE
18251 if the result is multibyte text.
18252
18253 Note we operate on the current buffer for most purposes,
18254 the exception being w->base_line_pos. */
18255
18256 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18257
18258 static char *
18259 decode_mode_spec (w, c, field_width, precision, multibyte)
18260 struct window *w;
18261 register int c;
18262 int field_width, precision;
18263 int *multibyte;
18264 {
18265 Lisp_Object obj;
18266 struct frame *f = XFRAME (WINDOW_FRAME (w));
18267 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18268 struct buffer *b = current_buffer;
18269
18270 obj = Qnil;
18271 *multibyte = 0;
18272
18273 switch (c)
18274 {
18275 case '*':
18276 if (!NILP (b->read_only))
18277 return "%";
18278 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18279 return "*";
18280 return "-";
18281
18282 case '+':
18283 /* This differs from %* only for a modified read-only buffer. */
18284 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18285 return "*";
18286 if (!NILP (b->read_only))
18287 return "%";
18288 return "-";
18289
18290 case '&':
18291 /* This differs from %* in ignoring read-only-ness. */
18292 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18293 return "*";
18294 return "-";
18295
18296 case '%':
18297 return "%";
18298
18299 case '[':
18300 {
18301 int i;
18302 char *p;
18303
18304 if (command_loop_level > 5)
18305 return "[[[... ";
18306 p = decode_mode_spec_buf;
18307 for (i = 0; i < command_loop_level; i++)
18308 *p++ = '[';
18309 *p = 0;
18310 return decode_mode_spec_buf;
18311 }
18312
18313 case ']':
18314 {
18315 int i;
18316 char *p;
18317
18318 if (command_loop_level > 5)
18319 return " ...]]]";
18320 p = decode_mode_spec_buf;
18321 for (i = 0; i < command_loop_level; i++)
18322 *p++ = ']';
18323 *p = 0;
18324 return decode_mode_spec_buf;
18325 }
18326
18327 case '-':
18328 {
18329 register int i;
18330
18331 /* Let lots_of_dashes be a string of infinite length. */
18332 if (mode_line_target == MODE_LINE_NOPROP ||
18333 mode_line_target == MODE_LINE_STRING)
18334 return "--";
18335 if (field_width <= 0
18336 || field_width > sizeof (lots_of_dashes))
18337 {
18338 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18339 decode_mode_spec_buf[i] = '-';
18340 decode_mode_spec_buf[i] = '\0';
18341 return decode_mode_spec_buf;
18342 }
18343 else
18344 return lots_of_dashes;
18345 }
18346
18347 case 'b':
18348 obj = b->name;
18349 break;
18350
18351 case 'c':
18352 /* %c and %l are ignored in `frame-title-format'.
18353 (In redisplay_internal, the frame title is drawn _before_ the
18354 windows are updated, so the stuff which depends on actual
18355 window contents (such as %l) may fail to render properly, or
18356 even crash emacs.) */
18357 if (mode_line_target == MODE_LINE_TITLE)
18358 return "";
18359 else
18360 {
18361 int col = (int) current_column (); /* iftc */
18362 w->column_number_displayed = make_number (col);
18363 pint2str (decode_mode_spec_buf, field_width, col);
18364 return decode_mode_spec_buf;
18365 }
18366
18367 case 'e':
18368 #ifndef SYSTEM_MALLOC
18369 {
18370 if (NILP (Vmemory_full))
18371 return "";
18372 else
18373 return "!MEM FULL! ";
18374 }
18375 #else
18376 return "";
18377 #endif
18378
18379 case 'F':
18380 /* %F displays the frame name. */
18381 if (!NILP (f->title))
18382 return (char *) SDATA (f->title);
18383 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18384 return (char *) SDATA (f->name);
18385 return "Emacs";
18386
18387 case 'f':
18388 obj = b->filename;
18389 break;
18390
18391 case 'i':
18392 {
18393 int size = ZV - BEGV;
18394 pint2str (decode_mode_spec_buf, field_width, size);
18395 return decode_mode_spec_buf;
18396 }
18397
18398 case 'I':
18399 {
18400 int size = ZV - BEGV;
18401 pint2hrstr (decode_mode_spec_buf, field_width, size);
18402 return decode_mode_spec_buf;
18403 }
18404
18405 case 'l':
18406 {
18407 int startpos, startpos_byte, line, linepos, linepos_byte;
18408 int topline, nlines, junk, height;
18409
18410 /* %c and %l are ignored in `frame-title-format'. */
18411 if (mode_line_target == MODE_LINE_TITLE)
18412 return "";
18413
18414 startpos = XMARKER (w->start)->charpos;
18415 startpos_byte = marker_byte_position (w->start);
18416 height = WINDOW_TOTAL_LINES (w);
18417
18418 /* If we decided that this buffer isn't suitable for line numbers,
18419 don't forget that too fast. */
18420 if (EQ (w->base_line_pos, w->buffer))
18421 goto no_value;
18422 /* But do forget it, if the window shows a different buffer now. */
18423 else if (BUFFERP (w->base_line_pos))
18424 w->base_line_pos = Qnil;
18425
18426 /* If the buffer is very big, don't waste time. */
18427 if (INTEGERP (Vline_number_display_limit)
18428 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18429 {
18430 w->base_line_pos = Qnil;
18431 w->base_line_number = Qnil;
18432 goto no_value;
18433 }
18434
18435 if (INTEGERP (w->base_line_number)
18436 && INTEGERP (w->base_line_pos)
18437 && XFASTINT (w->base_line_pos) <= startpos)
18438 {
18439 line = XFASTINT (w->base_line_number);
18440 linepos = XFASTINT (w->base_line_pos);
18441 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18442 }
18443 else
18444 {
18445 line = 1;
18446 linepos = BUF_BEGV (b);
18447 linepos_byte = BUF_BEGV_BYTE (b);
18448 }
18449
18450 /* Count lines from base line to window start position. */
18451 nlines = display_count_lines (linepos, linepos_byte,
18452 startpos_byte,
18453 startpos, &junk);
18454
18455 topline = nlines + line;
18456
18457 /* Determine a new base line, if the old one is too close
18458 or too far away, or if we did not have one.
18459 "Too close" means it's plausible a scroll-down would
18460 go back past it. */
18461 if (startpos == BUF_BEGV (b))
18462 {
18463 w->base_line_number = make_number (topline);
18464 w->base_line_pos = make_number (BUF_BEGV (b));
18465 }
18466 else if (nlines < height + 25 || nlines > height * 3 + 50
18467 || linepos == BUF_BEGV (b))
18468 {
18469 int limit = BUF_BEGV (b);
18470 int limit_byte = BUF_BEGV_BYTE (b);
18471 int position;
18472 int distance = (height * 2 + 30) * line_number_display_limit_width;
18473
18474 if (startpos - distance > limit)
18475 {
18476 limit = startpos - distance;
18477 limit_byte = CHAR_TO_BYTE (limit);
18478 }
18479
18480 nlines = display_count_lines (startpos, startpos_byte,
18481 limit_byte,
18482 - (height * 2 + 30),
18483 &position);
18484 /* If we couldn't find the lines we wanted within
18485 line_number_display_limit_width chars per line,
18486 give up on line numbers for this window. */
18487 if (position == limit_byte && limit == startpos - distance)
18488 {
18489 w->base_line_pos = w->buffer;
18490 w->base_line_number = Qnil;
18491 goto no_value;
18492 }
18493
18494 w->base_line_number = make_number (topline - nlines);
18495 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18496 }
18497
18498 /* Now count lines from the start pos to point. */
18499 nlines = display_count_lines (startpos, startpos_byte,
18500 PT_BYTE, PT, &junk);
18501
18502 /* Record that we did display the line number. */
18503 line_number_displayed = 1;
18504
18505 /* Make the string to show. */
18506 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18507 return decode_mode_spec_buf;
18508 no_value:
18509 {
18510 char* p = decode_mode_spec_buf;
18511 int pad = field_width - 2;
18512 while (pad-- > 0)
18513 *p++ = ' ';
18514 *p++ = '?';
18515 *p++ = '?';
18516 *p = '\0';
18517 return decode_mode_spec_buf;
18518 }
18519 }
18520 break;
18521
18522 case 'm':
18523 obj = b->mode_name;
18524 break;
18525
18526 case 'n':
18527 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18528 return " Narrow";
18529 break;
18530
18531 case 'p':
18532 {
18533 int pos = marker_position (w->start);
18534 int total = BUF_ZV (b) - BUF_BEGV (b);
18535
18536 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18537 {
18538 if (pos <= BUF_BEGV (b))
18539 return "All";
18540 else
18541 return "Bottom";
18542 }
18543 else if (pos <= BUF_BEGV (b))
18544 return "Top";
18545 else
18546 {
18547 if (total > 1000000)
18548 /* Do it differently for a large value, to avoid overflow. */
18549 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18550 else
18551 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18552 /* We can't normally display a 3-digit number,
18553 so get us a 2-digit number that is close. */
18554 if (total == 100)
18555 total = 99;
18556 sprintf (decode_mode_spec_buf, "%2d%%", total);
18557 return decode_mode_spec_buf;
18558 }
18559 }
18560
18561 /* Display percentage of size above the bottom of the screen. */
18562 case 'P':
18563 {
18564 int toppos = marker_position (w->start);
18565 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18566 int total = BUF_ZV (b) - BUF_BEGV (b);
18567
18568 if (botpos >= BUF_ZV (b))
18569 {
18570 if (toppos <= BUF_BEGV (b))
18571 return "All";
18572 else
18573 return "Bottom";
18574 }
18575 else
18576 {
18577 if (total > 1000000)
18578 /* Do it differently for a large value, to avoid overflow. */
18579 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18580 else
18581 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18582 /* We can't normally display a 3-digit number,
18583 so get us a 2-digit number that is close. */
18584 if (total == 100)
18585 total = 99;
18586 if (toppos <= BUF_BEGV (b))
18587 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18588 else
18589 sprintf (decode_mode_spec_buf, "%2d%%", total);
18590 return decode_mode_spec_buf;
18591 }
18592 }
18593
18594 case 's':
18595 /* status of process */
18596 obj = Fget_buffer_process (Fcurrent_buffer ());
18597 if (NILP (obj))
18598 return "no process";
18599 #ifdef subprocesses
18600 obj = Fsymbol_name (Fprocess_status (obj));
18601 #endif
18602 break;
18603
18604 case '@':
18605 {
18606 Lisp_Object val;
18607 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18608 if (NILP (val))
18609 return "-";
18610 else
18611 return "@";
18612 }
18613
18614 case 't': /* indicate TEXT or BINARY */
18615 #ifdef MODE_LINE_BINARY_TEXT
18616 return MODE_LINE_BINARY_TEXT (b);
18617 #else
18618 return "T";
18619 #endif
18620
18621 case 'z':
18622 /* coding-system (not including end-of-line format) */
18623 case 'Z':
18624 /* coding-system (including end-of-line type) */
18625 {
18626 int eol_flag = (c == 'Z');
18627 char *p = decode_mode_spec_buf;
18628
18629 if (! FRAME_WINDOW_P (f))
18630 {
18631 /* No need to mention EOL here--the terminal never needs
18632 to do EOL conversion. */
18633 p = decode_mode_spec_coding (CODING_ID_NAME
18634 (FRAME_KEYBOARD_CODING (f)->id),
18635 p, 0);
18636 p = decode_mode_spec_coding (CODING_ID_NAME
18637 (FRAME_TERMINAL_CODING (f)->id),
18638 p, 0);
18639 }
18640 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18641 p, eol_flag);
18642
18643 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18644 #ifdef subprocesses
18645 obj = Fget_buffer_process (Fcurrent_buffer ());
18646 if (PROCESSP (obj))
18647 {
18648 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18649 p, eol_flag);
18650 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18651 p, eol_flag);
18652 }
18653 #endif /* subprocesses */
18654 #endif /* 0 */
18655 *p = 0;
18656 return decode_mode_spec_buf;
18657 }
18658 }
18659
18660 if (STRINGP (obj))
18661 {
18662 *multibyte = STRING_MULTIBYTE (obj);
18663 return (char *) SDATA (obj);
18664 }
18665 else
18666 return "";
18667 }
18668
18669
18670 /* Count up to COUNT lines starting from START / START_BYTE.
18671 But don't go beyond LIMIT_BYTE.
18672 Return the number of lines thus found (always nonnegative).
18673
18674 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18675
18676 static int
18677 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18678 int start, start_byte, limit_byte, count;
18679 int *byte_pos_ptr;
18680 {
18681 register unsigned char *cursor;
18682 unsigned char *base;
18683
18684 register int ceiling;
18685 register unsigned char *ceiling_addr;
18686 int orig_count = count;
18687
18688 /* If we are not in selective display mode,
18689 check only for newlines. */
18690 int selective_display = (!NILP (current_buffer->selective_display)
18691 && !INTEGERP (current_buffer->selective_display));
18692
18693 if (count > 0)
18694 {
18695 while (start_byte < limit_byte)
18696 {
18697 ceiling = BUFFER_CEILING_OF (start_byte);
18698 ceiling = min (limit_byte - 1, ceiling);
18699 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18700 base = (cursor = BYTE_POS_ADDR (start_byte));
18701 while (1)
18702 {
18703 if (selective_display)
18704 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18705 ;
18706 else
18707 while (*cursor != '\n' && ++cursor != ceiling_addr)
18708 ;
18709
18710 if (cursor != ceiling_addr)
18711 {
18712 if (--count == 0)
18713 {
18714 start_byte += cursor - base + 1;
18715 *byte_pos_ptr = start_byte;
18716 return orig_count;
18717 }
18718 else
18719 if (++cursor == ceiling_addr)
18720 break;
18721 }
18722 else
18723 break;
18724 }
18725 start_byte += cursor - base;
18726 }
18727 }
18728 else
18729 {
18730 while (start_byte > limit_byte)
18731 {
18732 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18733 ceiling = max (limit_byte, ceiling);
18734 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18735 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18736 while (1)
18737 {
18738 if (selective_display)
18739 while (--cursor != ceiling_addr
18740 && *cursor != '\n' && *cursor != 015)
18741 ;
18742 else
18743 while (--cursor != ceiling_addr && *cursor != '\n')
18744 ;
18745
18746 if (cursor != ceiling_addr)
18747 {
18748 if (++count == 0)
18749 {
18750 start_byte += cursor - base + 1;
18751 *byte_pos_ptr = start_byte;
18752 /* When scanning backwards, we should
18753 not count the newline posterior to which we stop. */
18754 return - orig_count - 1;
18755 }
18756 }
18757 else
18758 break;
18759 }
18760 /* Here we add 1 to compensate for the last decrement
18761 of CURSOR, which took it past the valid range. */
18762 start_byte += cursor - base + 1;
18763 }
18764 }
18765
18766 *byte_pos_ptr = limit_byte;
18767
18768 if (count < 0)
18769 return - orig_count + count;
18770 return orig_count - count;
18771
18772 }
18773
18774
18775 \f
18776 /***********************************************************************
18777 Displaying strings
18778 ***********************************************************************/
18779
18780 /* Display a NUL-terminated string, starting with index START.
18781
18782 If STRING is non-null, display that C string. Otherwise, the Lisp
18783 string LISP_STRING is displayed.
18784
18785 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18786 FACE_STRING. Display STRING or LISP_STRING with the face at
18787 FACE_STRING_POS in FACE_STRING:
18788
18789 Display the string in the environment given by IT, but use the
18790 standard display table, temporarily.
18791
18792 FIELD_WIDTH is the minimum number of output glyphs to produce.
18793 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18794 with spaces. If STRING has more characters, more than FIELD_WIDTH
18795 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18796
18797 PRECISION is the maximum number of characters to output from
18798 STRING. PRECISION < 0 means don't truncate the string.
18799
18800 This is roughly equivalent to printf format specifiers:
18801
18802 FIELD_WIDTH PRECISION PRINTF
18803 ----------------------------------------
18804 -1 -1 %s
18805 -1 10 %.10s
18806 10 -1 %10s
18807 20 10 %20.10s
18808
18809 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18810 display them, and < 0 means obey the current buffer's value of
18811 enable_multibyte_characters.
18812
18813 Value is the number of columns displayed. */
18814
18815 static int
18816 display_string (string, lisp_string, face_string, face_string_pos,
18817 start, it, field_width, precision, max_x, multibyte)
18818 unsigned char *string;
18819 Lisp_Object lisp_string;
18820 Lisp_Object face_string;
18821 EMACS_INT face_string_pos;
18822 EMACS_INT start;
18823 struct it *it;
18824 int field_width, precision, max_x;
18825 int multibyte;
18826 {
18827 int hpos_at_start = it->hpos;
18828 int saved_face_id = it->face_id;
18829 struct glyph_row *row = it->glyph_row;
18830
18831 /* Initialize the iterator IT for iteration over STRING beginning
18832 with index START. */
18833 reseat_to_string (it, string, lisp_string, start,
18834 precision, field_width, multibyte);
18835
18836 /* If displaying STRING, set up the face of the iterator
18837 from LISP_STRING, if that's given. */
18838 if (STRINGP (face_string))
18839 {
18840 EMACS_INT endptr;
18841 struct face *face;
18842
18843 it->face_id
18844 = face_at_string_position (it->w, face_string, face_string_pos,
18845 0, it->region_beg_charpos,
18846 it->region_end_charpos,
18847 &endptr, it->base_face_id, 0);
18848 face = FACE_FROM_ID (it->f, it->face_id);
18849 it->face_box_p = face->box != FACE_NO_BOX;
18850 }
18851
18852 /* Set max_x to the maximum allowed X position. Don't let it go
18853 beyond the right edge of the window. */
18854 if (max_x <= 0)
18855 max_x = it->last_visible_x;
18856 else
18857 max_x = min (max_x, it->last_visible_x);
18858
18859 /* Skip over display elements that are not visible. because IT->w is
18860 hscrolled. */
18861 if (it->current_x < it->first_visible_x)
18862 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18863 MOVE_TO_POS | MOVE_TO_X);
18864
18865 row->ascent = it->max_ascent;
18866 row->height = it->max_ascent + it->max_descent;
18867 row->phys_ascent = it->max_phys_ascent;
18868 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18869 row->extra_line_spacing = it->max_extra_line_spacing;
18870
18871 /* This condition is for the case that we are called with current_x
18872 past last_visible_x. */
18873 while (it->current_x < max_x)
18874 {
18875 int x_before, x, n_glyphs_before, i, nglyphs;
18876
18877 /* Get the next display element. */
18878 if (!get_next_display_element (it))
18879 break;
18880
18881 /* Produce glyphs. */
18882 x_before = it->current_x;
18883 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18884 PRODUCE_GLYPHS (it);
18885
18886 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18887 i = 0;
18888 x = x_before;
18889 while (i < nglyphs)
18890 {
18891 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18892
18893 if (it->line_wrap != TRUNCATE
18894 && x + glyph->pixel_width > max_x)
18895 {
18896 /* End of continued line or max_x reached. */
18897 if (CHAR_GLYPH_PADDING_P (*glyph))
18898 {
18899 /* A wide character is unbreakable. */
18900 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18901 it->current_x = x_before;
18902 }
18903 else
18904 {
18905 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18906 it->current_x = x;
18907 }
18908 break;
18909 }
18910 else if (x + glyph->pixel_width >= it->first_visible_x)
18911 {
18912 /* Glyph is at least partially visible. */
18913 ++it->hpos;
18914 if (x < it->first_visible_x)
18915 it->glyph_row->x = x - it->first_visible_x;
18916 }
18917 else
18918 {
18919 /* Glyph is off the left margin of the display area.
18920 Should not happen. */
18921 abort ();
18922 }
18923
18924 row->ascent = max (row->ascent, it->max_ascent);
18925 row->height = max (row->height, it->max_ascent + it->max_descent);
18926 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18927 row->phys_height = max (row->phys_height,
18928 it->max_phys_ascent + it->max_phys_descent);
18929 row->extra_line_spacing = max (row->extra_line_spacing,
18930 it->max_extra_line_spacing);
18931 x += glyph->pixel_width;
18932 ++i;
18933 }
18934
18935 /* Stop if max_x reached. */
18936 if (i < nglyphs)
18937 break;
18938
18939 /* Stop at line ends. */
18940 if (ITERATOR_AT_END_OF_LINE_P (it))
18941 {
18942 it->continuation_lines_width = 0;
18943 break;
18944 }
18945
18946 set_iterator_to_next (it, 1);
18947
18948 /* Stop if truncating at the right edge. */
18949 if (it->line_wrap == TRUNCATE
18950 && it->current_x >= it->last_visible_x)
18951 {
18952 /* Add truncation mark, but don't do it if the line is
18953 truncated at a padding space. */
18954 if (IT_CHARPOS (*it) < it->string_nchars)
18955 {
18956 if (!FRAME_WINDOW_P (it->f))
18957 {
18958 int i, n;
18959
18960 if (it->current_x > it->last_visible_x)
18961 {
18962 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18963 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18964 break;
18965 for (n = row->used[TEXT_AREA]; i < n; ++i)
18966 {
18967 row->used[TEXT_AREA] = i;
18968 produce_special_glyphs (it, IT_TRUNCATION);
18969 }
18970 }
18971 produce_special_glyphs (it, IT_TRUNCATION);
18972 }
18973 it->glyph_row->truncated_on_right_p = 1;
18974 }
18975 break;
18976 }
18977 }
18978
18979 /* Maybe insert a truncation at the left. */
18980 if (it->first_visible_x
18981 && IT_CHARPOS (*it) > 0)
18982 {
18983 if (!FRAME_WINDOW_P (it->f))
18984 insert_left_trunc_glyphs (it);
18985 it->glyph_row->truncated_on_left_p = 1;
18986 }
18987
18988 it->face_id = saved_face_id;
18989
18990 /* Value is number of columns displayed. */
18991 return it->hpos - hpos_at_start;
18992 }
18993
18994
18995 \f
18996 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18997 appears as an element of LIST or as the car of an element of LIST.
18998 If PROPVAL is a list, compare each element against LIST in that
18999 way, and return 1/2 if any element of PROPVAL is found in LIST.
19000 Otherwise return 0. This function cannot quit.
19001 The return value is 2 if the text is invisible but with an ellipsis
19002 and 1 if it's invisible and without an ellipsis. */
19003
19004 int
19005 invisible_p (propval, list)
19006 register Lisp_Object propval;
19007 Lisp_Object list;
19008 {
19009 register Lisp_Object tail, proptail;
19010
19011 for (tail = list; CONSP (tail); tail = XCDR (tail))
19012 {
19013 register Lisp_Object tem;
19014 tem = XCAR (tail);
19015 if (EQ (propval, tem))
19016 return 1;
19017 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19018 return NILP (XCDR (tem)) ? 1 : 2;
19019 }
19020
19021 if (CONSP (propval))
19022 {
19023 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19024 {
19025 Lisp_Object propelt;
19026 propelt = XCAR (proptail);
19027 for (tail = list; CONSP (tail); tail = XCDR (tail))
19028 {
19029 register Lisp_Object tem;
19030 tem = XCAR (tail);
19031 if (EQ (propelt, tem))
19032 return 1;
19033 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19034 return NILP (XCDR (tem)) ? 1 : 2;
19035 }
19036 }
19037 }
19038
19039 return 0;
19040 }
19041
19042 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19043 doc: /* Non-nil if the property makes the text invisible.
19044 POS-OR-PROP can be a marker or number, in which case it is taken to be
19045 a position in the current buffer and the value of the `invisible' property
19046 is checked; or it can be some other value, which is then presumed to be the
19047 value of the `invisible' property of the text of interest.
19048 The non-nil value returned can be t for truly invisible text or something
19049 else if the text is replaced by an ellipsis. */)
19050 (pos_or_prop)
19051 Lisp_Object pos_or_prop;
19052 {
19053 Lisp_Object prop
19054 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19055 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19056 : pos_or_prop);
19057 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19058 return (invis == 0 ? Qnil
19059 : invis == 1 ? Qt
19060 : make_number (invis));
19061 }
19062
19063 /* Calculate a width or height in pixels from a specification using
19064 the following elements:
19065
19066 SPEC ::=
19067 NUM - a (fractional) multiple of the default font width/height
19068 (NUM) - specifies exactly NUM pixels
19069 UNIT - a fixed number of pixels, see below.
19070 ELEMENT - size of a display element in pixels, see below.
19071 (NUM . SPEC) - equals NUM * SPEC
19072 (+ SPEC SPEC ...) - add pixel values
19073 (- SPEC SPEC ...) - subtract pixel values
19074 (- SPEC) - negate pixel value
19075
19076 NUM ::=
19077 INT or FLOAT - a number constant
19078 SYMBOL - use symbol's (buffer local) variable binding.
19079
19080 UNIT ::=
19081 in - pixels per inch *)
19082 mm - pixels per 1/1000 meter *)
19083 cm - pixels per 1/100 meter *)
19084 width - width of current font in pixels.
19085 height - height of current font in pixels.
19086
19087 *) using the ratio(s) defined in display-pixels-per-inch.
19088
19089 ELEMENT ::=
19090
19091 left-fringe - left fringe width in pixels
19092 right-fringe - right fringe width in pixels
19093
19094 left-margin - left margin width in pixels
19095 right-margin - right margin width in pixels
19096
19097 scroll-bar - scroll-bar area width in pixels
19098
19099 Examples:
19100
19101 Pixels corresponding to 5 inches:
19102 (5 . in)
19103
19104 Total width of non-text areas on left side of window (if scroll-bar is on left):
19105 '(space :width (+ left-fringe left-margin scroll-bar))
19106
19107 Align to first text column (in header line):
19108 '(space :align-to 0)
19109
19110 Align to middle of text area minus half the width of variable `my-image'
19111 containing a loaded image:
19112 '(space :align-to (0.5 . (- text my-image)))
19113
19114 Width of left margin minus width of 1 character in the default font:
19115 '(space :width (- left-margin 1))
19116
19117 Width of left margin minus width of 2 characters in the current font:
19118 '(space :width (- left-margin (2 . width)))
19119
19120 Center 1 character over left-margin (in header line):
19121 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19122
19123 Different ways to express width of left fringe plus left margin minus one pixel:
19124 '(space :width (- (+ left-fringe left-margin) (1)))
19125 '(space :width (+ left-fringe left-margin (- (1))))
19126 '(space :width (+ left-fringe left-margin (-1)))
19127
19128 */
19129
19130 #define NUMVAL(X) \
19131 ((INTEGERP (X) || FLOATP (X)) \
19132 ? XFLOATINT (X) \
19133 : - 1)
19134
19135 int
19136 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19137 double *res;
19138 struct it *it;
19139 Lisp_Object prop;
19140 struct font *font;
19141 int width_p, *align_to;
19142 {
19143 double pixels;
19144
19145 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19146 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19147
19148 if (NILP (prop))
19149 return OK_PIXELS (0);
19150
19151 xassert (FRAME_LIVE_P (it->f));
19152
19153 if (SYMBOLP (prop))
19154 {
19155 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19156 {
19157 char *unit = SDATA (SYMBOL_NAME (prop));
19158
19159 if (unit[0] == 'i' && unit[1] == 'n')
19160 pixels = 1.0;
19161 else if (unit[0] == 'm' && unit[1] == 'm')
19162 pixels = 25.4;
19163 else if (unit[0] == 'c' && unit[1] == 'm')
19164 pixels = 2.54;
19165 else
19166 pixels = 0;
19167 if (pixels > 0)
19168 {
19169 double ppi;
19170 #ifdef HAVE_WINDOW_SYSTEM
19171 if (FRAME_WINDOW_P (it->f)
19172 && (ppi = (width_p
19173 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19174 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19175 ppi > 0))
19176 return OK_PIXELS (ppi / pixels);
19177 #endif
19178
19179 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19180 || (CONSP (Vdisplay_pixels_per_inch)
19181 && (ppi = (width_p
19182 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19183 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19184 ppi > 0)))
19185 return OK_PIXELS (ppi / pixels);
19186
19187 return 0;
19188 }
19189 }
19190
19191 #ifdef HAVE_WINDOW_SYSTEM
19192 if (EQ (prop, Qheight))
19193 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19194 if (EQ (prop, Qwidth))
19195 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19196 #else
19197 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19198 return OK_PIXELS (1);
19199 #endif
19200
19201 if (EQ (prop, Qtext))
19202 return OK_PIXELS (width_p
19203 ? window_box_width (it->w, TEXT_AREA)
19204 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19205
19206 if (align_to && *align_to < 0)
19207 {
19208 *res = 0;
19209 if (EQ (prop, Qleft))
19210 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19211 if (EQ (prop, Qright))
19212 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19213 if (EQ (prop, Qcenter))
19214 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19215 + window_box_width (it->w, TEXT_AREA) / 2);
19216 if (EQ (prop, Qleft_fringe))
19217 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19218 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19219 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19220 if (EQ (prop, Qright_fringe))
19221 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19222 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19223 : window_box_right_offset (it->w, TEXT_AREA));
19224 if (EQ (prop, Qleft_margin))
19225 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19226 if (EQ (prop, Qright_margin))
19227 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19228 if (EQ (prop, Qscroll_bar))
19229 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19230 ? 0
19231 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19232 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19233 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19234 : 0)));
19235 }
19236 else
19237 {
19238 if (EQ (prop, Qleft_fringe))
19239 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19240 if (EQ (prop, Qright_fringe))
19241 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19242 if (EQ (prop, Qleft_margin))
19243 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19244 if (EQ (prop, Qright_margin))
19245 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19246 if (EQ (prop, Qscroll_bar))
19247 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19248 }
19249
19250 prop = Fbuffer_local_value (prop, it->w->buffer);
19251 }
19252
19253 if (INTEGERP (prop) || FLOATP (prop))
19254 {
19255 int base_unit = (width_p
19256 ? FRAME_COLUMN_WIDTH (it->f)
19257 : FRAME_LINE_HEIGHT (it->f));
19258 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19259 }
19260
19261 if (CONSP (prop))
19262 {
19263 Lisp_Object car = XCAR (prop);
19264 Lisp_Object cdr = XCDR (prop);
19265
19266 if (SYMBOLP (car))
19267 {
19268 #ifdef HAVE_WINDOW_SYSTEM
19269 if (FRAME_WINDOW_P (it->f)
19270 && valid_image_p (prop))
19271 {
19272 int id = lookup_image (it->f, prop);
19273 struct image *img = IMAGE_FROM_ID (it->f, id);
19274
19275 return OK_PIXELS (width_p ? img->width : img->height);
19276 }
19277 #endif
19278 if (EQ (car, Qplus) || EQ (car, Qminus))
19279 {
19280 int first = 1;
19281 double px;
19282
19283 pixels = 0;
19284 while (CONSP (cdr))
19285 {
19286 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19287 font, width_p, align_to))
19288 return 0;
19289 if (first)
19290 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19291 else
19292 pixels += px;
19293 cdr = XCDR (cdr);
19294 }
19295 if (EQ (car, Qminus))
19296 pixels = -pixels;
19297 return OK_PIXELS (pixels);
19298 }
19299
19300 car = Fbuffer_local_value (car, it->w->buffer);
19301 }
19302
19303 if (INTEGERP (car) || FLOATP (car))
19304 {
19305 double fact;
19306 pixels = XFLOATINT (car);
19307 if (NILP (cdr))
19308 return OK_PIXELS (pixels);
19309 if (calc_pixel_width_or_height (&fact, it, cdr,
19310 font, width_p, align_to))
19311 return OK_PIXELS (pixels * fact);
19312 return 0;
19313 }
19314
19315 return 0;
19316 }
19317
19318 return 0;
19319 }
19320
19321 \f
19322 /***********************************************************************
19323 Glyph Display
19324 ***********************************************************************/
19325
19326 #ifdef HAVE_WINDOW_SYSTEM
19327
19328 #if GLYPH_DEBUG
19329
19330 void
19331 dump_glyph_string (s)
19332 struct glyph_string *s;
19333 {
19334 fprintf (stderr, "glyph string\n");
19335 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19336 s->x, s->y, s->width, s->height);
19337 fprintf (stderr, " ybase = %d\n", s->ybase);
19338 fprintf (stderr, " hl = %d\n", s->hl);
19339 fprintf (stderr, " left overhang = %d, right = %d\n",
19340 s->left_overhang, s->right_overhang);
19341 fprintf (stderr, " nchars = %d\n", s->nchars);
19342 fprintf (stderr, " extends to end of line = %d\n",
19343 s->extends_to_end_of_line_p);
19344 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19345 fprintf (stderr, " bg width = %d\n", s->background_width);
19346 }
19347
19348 #endif /* GLYPH_DEBUG */
19349
19350 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19351 of XChar2b structures for S; it can't be allocated in
19352 init_glyph_string because it must be allocated via `alloca'. W
19353 is the window on which S is drawn. ROW and AREA are the glyph row
19354 and area within the row from which S is constructed. START is the
19355 index of the first glyph structure covered by S. HL is a
19356 face-override for drawing S. */
19357
19358 #ifdef HAVE_NTGUI
19359 #define OPTIONAL_HDC(hdc) hdc,
19360 #define DECLARE_HDC(hdc) HDC hdc;
19361 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19362 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19363 #endif
19364
19365 #ifndef OPTIONAL_HDC
19366 #define OPTIONAL_HDC(hdc)
19367 #define DECLARE_HDC(hdc)
19368 #define ALLOCATE_HDC(hdc, f)
19369 #define RELEASE_HDC(hdc, f)
19370 #endif
19371
19372 static void
19373 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19374 struct glyph_string *s;
19375 DECLARE_HDC (hdc)
19376 XChar2b *char2b;
19377 struct window *w;
19378 struct glyph_row *row;
19379 enum glyph_row_area area;
19380 int start;
19381 enum draw_glyphs_face hl;
19382 {
19383 bzero (s, sizeof *s);
19384 s->w = w;
19385 s->f = XFRAME (w->frame);
19386 #ifdef HAVE_NTGUI
19387 s->hdc = hdc;
19388 #endif
19389 s->display = FRAME_X_DISPLAY (s->f);
19390 s->window = FRAME_X_WINDOW (s->f);
19391 s->char2b = char2b;
19392 s->hl = hl;
19393 s->row = row;
19394 s->area = area;
19395 s->first_glyph = row->glyphs[area] + start;
19396 s->height = row->height;
19397 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19398
19399 /* Display the internal border below the tool-bar window. */
19400 if (WINDOWP (s->f->tool_bar_window)
19401 && s->w == XWINDOW (s->f->tool_bar_window))
19402 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19403
19404 s->ybase = s->y + row->ascent;
19405 }
19406
19407
19408 /* Append the list of glyph strings with head H and tail T to the list
19409 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19410
19411 static INLINE void
19412 append_glyph_string_lists (head, tail, h, t)
19413 struct glyph_string **head, **tail;
19414 struct glyph_string *h, *t;
19415 {
19416 if (h)
19417 {
19418 if (*head)
19419 (*tail)->next = h;
19420 else
19421 *head = h;
19422 h->prev = *tail;
19423 *tail = t;
19424 }
19425 }
19426
19427
19428 /* Prepend the list of glyph strings with head H and tail T to the
19429 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19430 result. */
19431
19432 static INLINE void
19433 prepend_glyph_string_lists (head, tail, h, t)
19434 struct glyph_string **head, **tail;
19435 struct glyph_string *h, *t;
19436 {
19437 if (h)
19438 {
19439 if (*head)
19440 (*head)->prev = t;
19441 else
19442 *tail = t;
19443 t->next = *head;
19444 *head = h;
19445 }
19446 }
19447
19448
19449 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19450 Set *HEAD and *TAIL to the resulting list. */
19451
19452 static INLINE void
19453 append_glyph_string (head, tail, s)
19454 struct glyph_string **head, **tail;
19455 struct glyph_string *s;
19456 {
19457 s->next = s->prev = NULL;
19458 append_glyph_string_lists (head, tail, s, s);
19459 }
19460
19461
19462 /* Get face and two-byte form of character C in face FACE_ID on frame
19463 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19464 means we want to display multibyte text. DISPLAY_P non-zero means
19465 make sure that X resources for the face returned are allocated.
19466 Value is a pointer to a realized face that is ready for display if
19467 DISPLAY_P is non-zero. */
19468
19469 static INLINE struct face *
19470 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19471 struct frame *f;
19472 int c, face_id;
19473 XChar2b *char2b;
19474 int multibyte_p, display_p;
19475 {
19476 struct face *face = FACE_FROM_ID (f, face_id);
19477
19478 if (face->font)
19479 {
19480 unsigned code = face->font->driver->encode_char (face->font, c);
19481
19482 if (code != FONT_INVALID_CODE)
19483 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19484 else
19485 STORE_XCHAR2B (char2b, 0, 0);
19486 }
19487
19488 /* Make sure X resources of the face are allocated. */
19489 #ifdef HAVE_X_WINDOWS
19490 if (display_p)
19491 #endif
19492 {
19493 xassert (face != NULL);
19494 PREPARE_FACE_FOR_DISPLAY (f, face);
19495 }
19496
19497 return face;
19498 }
19499
19500
19501 /* Get face and two-byte form of character glyph GLYPH on frame F.
19502 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19503 a pointer to a realized face that is ready for display. */
19504
19505 static INLINE struct face *
19506 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19507 struct frame *f;
19508 struct glyph *glyph;
19509 XChar2b *char2b;
19510 int *two_byte_p;
19511 {
19512 struct face *face;
19513
19514 xassert (glyph->type == CHAR_GLYPH);
19515 face = FACE_FROM_ID (f, glyph->face_id);
19516
19517 if (two_byte_p)
19518 *two_byte_p = 0;
19519
19520 if (face->font)
19521 {
19522 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19523
19524 if (code != FONT_INVALID_CODE)
19525 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19526 else
19527 STORE_XCHAR2B (char2b, 0, 0);
19528 }
19529
19530 /* Make sure X resources of the face are allocated. */
19531 xassert (face != NULL);
19532 PREPARE_FACE_FOR_DISPLAY (f, face);
19533 return face;
19534 }
19535
19536
19537 /* Fill glyph string S with composition components specified by S->cmp.
19538
19539 BASE_FACE is the base face of the composition.
19540 S->cmp_from is the index of the first component for S.
19541
19542 OVERLAPS non-zero means S should draw the foreground only, and use
19543 its physical height for clipping. See also draw_glyphs.
19544
19545 Value is the index of a component not in S. */
19546
19547 static int
19548 fill_composite_glyph_string (s, base_face, overlaps)
19549 struct glyph_string *s;
19550 struct face *base_face;
19551 int overlaps;
19552 {
19553 int i;
19554 /* For all glyphs of this composition, starting at the offset
19555 S->cmp_from, until we reach the end of the definition or encounter a
19556 glyph that requires the different face, add it to S. */
19557 struct face *face;
19558
19559 xassert (s);
19560
19561 s->for_overlaps = overlaps;
19562 s->face = NULL;
19563 s->font = NULL;
19564 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19565 {
19566 int c = COMPOSITION_GLYPH (s->cmp, i);
19567
19568 if (c != '\t')
19569 {
19570 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19571 -1, Qnil);
19572
19573 face = get_char_face_and_encoding (s->f, c, face_id,
19574 s->char2b + i, 1, 1);
19575 if (face)
19576 {
19577 if (! s->face)
19578 {
19579 s->face = face;
19580 s->font = s->face->font;
19581 }
19582 else if (s->face != face)
19583 break;
19584 }
19585 }
19586 ++s->nchars;
19587 }
19588 s->cmp_to = i;
19589
19590 /* All glyph strings for the same composition has the same width,
19591 i.e. the width set for the first component of the composition. */
19592 s->width = s->first_glyph->pixel_width;
19593
19594 /* If the specified font could not be loaded, use the frame's
19595 default font, but record the fact that we couldn't load it in
19596 the glyph string so that we can draw rectangles for the
19597 characters of the glyph string. */
19598 if (s->font == NULL)
19599 {
19600 s->font_not_found_p = 1;
19601 s->font = FRAME_FONT (s->f);
19602 }
19603
19604 /* Adjust base line for subscript/superscript text. */
19605 s->ybase += s->first_glyph->voffset;
19606
19607 /* This glyph string must always be drawn with 16-bit functions. */
19608 s->two_byte_p = 1;
19609
19610 return s->cmp_to;
19611 }
19612
19613 static int
19614 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19615 struct glyph_string *s;
19616 int face_id;
19617 int start, end, overlaps;
19618 {
19619 struct glyph *glyph, *last;
19620 Lisp_Object lgstring;
19621 int i;
19622
19623 s->for_overlaps = overlaps;
19624 glyph = s->row->glyphs[s->area] + start;
19625 last = s->row->glyphs[s->area] + end;
19626 s->cmp_id = glyph->u.cmp.id;
19627 s->cmp_from = glyph->u.cmp.from;
19628 s->cmp_to = glyph->u.cmp.to + 1;
19629 s->face = FACE_FROM_ID (s->f, face_id);
19630 lgstring = composition_gstring_from_id (s->cmp_id);
19631 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19632 glyph++;
19633 while (glyph < last
19634 && glyph->u.cmp.automatic
19635 && glyph->u.cmp.id == s->cmp_id
19636 && s->cmp_to == glyph->u.cmp.from)
19637 s->cmp_to = (glyph++)->u.cmp.to + 1;
19638
19639 for (i = s->cmp_from; i < s->cmp_to; i++)
19640 {
19641 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19642 unsigned code = LGLYPH_CODE (lglyph);
19643
19644 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19645 }
19646 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19647 return glyph - s->row->glyphs[s->area];
19648 }
19649
19650
19651 /* Fill glyph string S from a sequence of character glyphs.
19652
19653 FACE_ID is the face id of the string. START is the index of the
19654 first glyph to consider, END is the index of the last + 1.
19655 OVERLAPS non-zero means S should draw the foreground only, and use
19656 its physical height for clipping. See also draw_glyphs.
19657
19658 Value is the index of the first glyph not in S. */
19659
19660 static int
19661 fill_glyph_string (s, face_id, start, end, overlaps)
19662 struct glyph_string *s;
19663 int face_id;
19664 int start, end, overlaps;
19665 {
19666 struct glyph *glyph, *last;
19667 int voffset;
19668 int glyph_not_available_p;
19669
19670 xassert (s->f == XFRAME (s->w->frame));
19671 xassert (s->nchars == 0);
19672 xassert (start >= 0 && end > start);
19673
19674 s->for_overlaps = overlaps;
19675 glyph = s->row->glyphs[s->area] + start;
19676 last = s->row->glyphs[s->area] + end;
19677 voffset = glyph->voffset;
19678 s->padding_p = glyph->padding_p;
19679 glyph_not_available_p = glyph->glyph_not_available_p;
19680
19681 while (glyph < last
19682 && glyph->type == CHAR_GLYPH
19683 && glyph->voffset == voffset
19684 /* Same face id implies same font, nowadays. */
19685 && glyph->face_id == face_id
19686 && glyph->glyph_not_available_p == glyph_not_available_p)
19687 {
19688 int two_byte_p;
19689
19690 s->face = get_glyph_face_and_encoding (s->f, glyph,
19691 s->char2b + s->nchars,
19692 &two_byte_p);
19693 s->two_byte_p = two_byte_p;
19694 ++s->nchars;
19695 xassert (s->nchars <= end - start);
19696 s->width += glyph->pixel_width;
19697 if (glyph++->padding_p != s->padding_p)
19698 break;
19699 }
19700
19701 s->font = s->face->font;
19702
19703 /* If the specified font could not be loaded, use the frame's font,
19704 but record the fact that we couldn't load it in
19705 S->font_not_found_p so that we can draw rectangles for the
19706 characters of the glyph string. */
19707 if (s->font == NULL || glyph_not_available_p)
19708 {
19709 s->font_not_found_p = 1;
19710 s->font = FRAME_FONT (s->f);
19711 }
19712
19713 /* Adjust base line for subscript/superscript text. */
19714 s->ybase += voffset;
19715
19716 xassert (s->face && s->face->gc);
19717 return glyph - s->row->glyphs[s->area];
19718 }
19719
19720
19721 /* Fill glyph string S from image glyph S->first_glyph. */
19722
19723 static void
19724 fill_image_glyph_string (s)
19725 struct glyph_string *s;
19726 {
19727 xassert (s->first_glyph->type == IMAGE_GLYPH);
19728 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19729 xassert (s->img);
19730 s->slice = s->first_glyph->slice;
19731 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19732 s->font = s->face->font;
19733 s->width = s->first_glyph->pixel_width;
19734
19735 /* Adjust base line for subscript/superscript text. */
19736 s->ybase += s->first_glyph->voffset;
19737 }
19738
19739
19740 /* Fill glyph string S from a sequence of stretch glyphs.
19741
19742 ROW is the glyph row in which the glyphs are found, AREA is the
19743 area within the row. START is the index of the first glyph to
19744 consider, END is the index of the last + 1.
19745
19746 Value is the index of the first glyph not in S. */
19747
19748 static int
19749 fill_stretch_glyph_string (s, row, area, start, end)
19750 struct glyph_string *s;
19751 struct glyph_row *row;
19752 enum glyph_row_area area;
19753 int start, end;
19754 {
19755 struct glyph *glyph, *last;
19756 int voffset, face_id;
19757
19758 xassert (s->first_glyph->type == STRETCH_GLYPH);
19759
19760 glyph = s->row->glyphs[s->area] + start;
19761 last = s->row->glyphs[s->area] + end;
19762 face_id = glyph->face_id;
19763 s->face = FACE_FROM_ID (s->f, face_id);
19764 s->font = s->face->font;
19765 s->width = glyph->pixel_width;
19766 s->nchars = 1;
19767 voffset = glyph->voffset;
19768
19769 for (++glyph;
19770 (glyph < last
19771 && glyph->type == STRETCH_GLYPH
19772 && glyph->voffset == voffset
19773 && glyph->face_id == face_id);
19774 ++glyph)
19775 s->width += glyph->pixel_width;
19776
19777 /* Adjust base line for subscript/superscript text. */
19778 s->ybase += voffset;
19779
19780 /* The case that face->gc == 0 is handled when drawing the glyph
19781 string by calling PREPARE_FACE_FOR_DISPLAY. */
19782 xassert (s->face);
19783 return glyph - s->row->glyphs[s->area];
19784 }
19785
19786 static struct font_metrics *
19787 get_per_char_metric (f, font, char2b)
19788 struct frame *f;
19789 struct font *font;
19790 XChar2b *char2b;
19791 {
19792 static struct font_metrics metrics;
19793 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19794
19795 if (! font || code == FONT_INVALID_CODE)
19796 return NULL;
19797 font->driver->text_extents (font, &code, 1, &metrics);
19798 return &metrics;
19799 }
19800
19801 /* EXPORT for RIF:
19802 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19803 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19804 assumed to be zero. */
19805
19806 void
19807 x_get_glyph_overhangs (glyph, f, left, right)
19808 struct glyph *glyph;
19809 struct frame *f;
19810 int *left, *right;
19811 {
19812 *left = *right = 0;
19813
19814 if (glyph->type == CHAR_GLYPH)
19815 {
19816 struct face *face;
19817 XChar2b char2b;
19818 struct font_metrics *pcm;
19819
19820 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19821 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19822 {
19823 if (pcm->rbearing > pcm->width)
19824 *right = pcm->rbearing - pcm->width;
19825 if (pcm->lbearing < 0)
19826 *left = -pcm->lbearing;
19827 }
19828 }
19829 else if (glyph->type == COMPOSITE_GLYPH)
19830 {
19831 if (! glyph->u.cmp.automatic)
19832 {
19833 struct composition *cmp = composition_table[glyph->u.cmp.id];
19834
19835 if (cmp->rbearing > cmp->pixel_width)
19836 *right = cmp->rbearing - cmp->pixel_width;
19837 if (cmp->lbearing < 0)
19838 *left = - cmp->lbearing;
19839 }
19840 else
19841 {
19842 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19843 struct font_metrics metrics;
19844
19845 composition_gstring_width (gstring, glyph->u.cmp.from,
19846 glyph->u.cmp.to + 1, &metrics);
19847 if (metrics.rbearing > metrics.width)
19848 *right = metrics.rbearing - metrics.width;
19849 if (metrics.lbearing < 0)
19850 *left = - metrics.lbearing;
19851 }
19852 }
19853 }
19854
19855
19856 /* Return the index of the first glyph preceding glyph string S that
19857 is overwritten by S because of S's left overhang. Value is -1
19858 if no glyphs are overwritten. */
19859
19860 static int
19861 left_overwritten (s)
19862 struct glyph_string *s;
19863 {
19864 int k;
19865
19866 if (s->left_overhang)
19867 {
19868 int x = 0, i;
19869 struct glyph *glyphs = s->row->glyphs[s->area];
19870 int first = s->first_glyph - glyphs;
19871
19872 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19873 x -= glyphs[i].pixel_width;
19874
19875 k = i + 1;
19876 }
19877 else
19878 k = -1;
19879
19880 return k;
19881 }
19882
19883
19884 /* Return the index of the first glyph preceding glyph string S that
19885 is overwriting S because of its right overhang. Value is -1 if no
19886 glyph in front of S overwrites S. */
19887
19888 static int
19889 left_overwriting (s)
19890 struct glyph_string *s;
19891 {
19892 int i, k, x;
19893 struct glyph *glyphs = s->row->glyphs[s->area];
19894 int first = s->first_glyph - glyphs;
19895
19896 k = -1;
19897 x = 0;
19898 for (i = first - 1; i >= 0; --i)
19899 {
19900 int left, right;
19901 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19902 if (x + right > 0)
19903 k = i;
19904 x -= glyphs[i].pixel_width;
19905 }
19906
19907 return k;
19908 }
19909
19910
19911 /* Return the index of the last glyph following glyph string S that is
19912 overwritten by S because of S's right overhang. Value is -1 if
19913 no such glyph is found. */
19914
19915 static int
19916 right_overwritten (s)
19917 struct glyph_string *s;
19918 {
19919 int k = -1;
19920
19921 if (s->right_overhang)
19922 {
19923 int x = 0, i;
19924 struct glyph *glyphs = s->row->glyphs[s->area];
19925 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19926 int end = s->row->used[s->area];
19927
19928 for (i = first; i < end && s->right_overhang > x; ++i)
19929 x += glyphs[i].pixel_width;
19930
19931 k = i;
19932 }
19933
19934 return k;
19935 }
19936
19937
19938 /* Return the index of the last glyph following glyph string S that
19939 overwrites S because of its left overhang. Value is negative
19940 if no such glyph is found. */
19941
19942 static int
19943 right_overwriting (s)
19944 struct glyph_string *s;
19945 {
19946 int i, k, x;
19947 int end = s->row->used[s->area];
19948 struct glyph *glyphs = s->row->glyphs[s->area];
19949 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19950
19951 k = -1;
19952 x = 0;
19953 for (i = first; i < end; ++i)
19954 {
19955 int left, right;
19956 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19957 if (x - left < 0)
19958 k = i;
19959 x += glyphs[i].pixel_width;
19960 }
19961
19962 return k;
19963 }
19964
19965
19966 /* Set background width of glyph string S. START is the index of the
19967 first glyph following S. LAST_X is the right-most x-position + 1
19968 in the drawing area. */
19969
19970 static INLINE void
19971 set_glyph_string_background_width (s, start, last_x)
19972 struct glyph_string *s;
19973 int start;
19974 int last_x;
19975 {
19976 /* If the face of this glyph string has to be drawn to the end of
19977 the drawing area, set S->extends_to_end_of_line_p. */
19978
19979 if (start == s->row->used[s->area]
19980 && s->area == TEXT_AREA
19981 && ((s->row->fill_line_p
19982 && (s->hl == DRAW_NORMAL_TEXT
19983 || s->hl == DRAW_IMAGE_RAISED
19984 || s->hl == DRAW_IMAGE_SUNKEN))
19985 || s->hl == DRAW_MOUSE_FACE))
19986 s->extends_to_end_of_line_p = 1;
19987
19988 /* If S extends its face to the end of the line, set its
19989 background_width to the distance to the right edge of the drawing
19990 area. */
19991 if (s->extends_to_end_of_line_p)
19992 s->background_width = last_x - s->x + 1;
19993 else
19994 s->background_width = s->width;
19995 }
19996
19997
19998 /* Compute overhangs and x-positions for glyph string S and its
19999 predecessors, or successors. X is the starting x-position for S.
20000 BACKWARD_P non-zero means process predecessors. */
20001
20002 static void
20003 compute_overhangs_and_x (s, x, backward_p)
20004 struct glyph_string *s;
20005 int x;
20006 int backward_p;
20007 {
20008 if (backward_p)
20009 {
20010 while (s)
20011 {
20012 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20013 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20014 x -= s->width;
20015 s->x = x;
20016 s = s->prev;
20017 }
20018 }
20019 else
20020 {
20021 while (s)
20022 {
20023 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20024 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20025 s->x = x;
20026 x += s->width;
20027 s = s->next;
20028 }
20029 }
20030 }
20031
20032
20033
20034 /* The following macros are only called from draw_glyphs below.
20035 They reference the following parameters of that function directly:
20036 `w', `row', `area', and `overlap_p'
20037 as well as the following local variables:
20038 `s', `f', and `hdc' (in W32) */
20039
20040 #ifdef HAVE_NTGUI
20041 /* On W32, silently add local `hdc' variable to argument list of
20042 init_glyph_string. */
20043 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20044 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20045 #else
20046 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20047 init_glyph_string (s, char2b, w, row, area, start, hl)
20048 #endif
20049
20050 /* Add a glyph string for a stretch glyph to the list of strings
20051 between HEAD and TAIL. START is the index of the stretch glyph in
20052 row area AREA of glyph row ROW. END is the index of the last glyph
20053 in that glyph row area. X is the current output position assigned
20054 to the new glyph string constructed. HL overrides that face of the
20055 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20056 is the right-most x-position of the drawing area. */
20057
20058 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20059 and below -- keep them on one line. */
20060 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20061 do \
20062 { \
20063 s = (struct glyph_string *) alloca (sizeof *s); \
20064 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20065 START = fill_stretch_glyph_string (s, row, area, START, END); \
20066 append_glyph_string (&HEAD, &TAIL, s); \
20067 s->x = (X); \
20068 } \
20069 while (0)
20070
20071
20072 /* Add a glyph string for an image glyph to the list of strings
20073 between HEAD and TAIL. START is the index of the image glyph in
20074 row area AREA of glyph row ROW. END is the index of the last glyph
20075 in that glyph row area. X is the current output position assigned
20076 to the new glyph string constructed. HL overrides that face of the
20077 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20078 is the right-most x-position of the drawing area. */
20079
20080 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20081 do \
20082 { \
20083 s = (struct glyph_string *) alloca (sizeof *s); \
20084 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20085 fill_image_glyph_string (s); \
20086 append_glyph_string (&HEAD, &TAIL, s); \
20087 ++START; \
20088 s->x = (X); \
20089 } \
20090 while (0)
20091
20092
20093 /* Add a glyph string for a sequence of character glyphs to the list
20094 of strings between HEAD and TAIL. START is the index of the first
20095 glyph in row area AREA of glyph row ROW that is part of the new
20096 glyph string. END is the index of the last glyph in that glyph row
20097 area. X is the current output position assigned to the new glyph
20098 string constructed. HL overrides that face of the glyph; e.g. it
20099 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20100 right-most x-position of the drawing area. */
20101
20102 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20103 do \
20104 { \
20105 int face_id; \
20106 XChar2b *char2b; \
20107 \
20108 face_id = (row)->glyphs[area][START].face_id; \
20109 \
20110 s = (struct glyph_string *) alloca (sizeof *s); \
20111 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20112 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20113 append_glyph_string (&HEAD, &TAIL, s); \
20114 s->x = (X); \
20115 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20116 } \
20117 while (0)
20118
20119
20120 /* Add a glyph string for a composite sequence to the list of strings
20121 between HEAD and TAIL. START is the index of the first glyph in
20122 row area AREA of glyph row ROW that is part of the new glyph
20123 string. END is the index of the last glyph in that glyph row area.
20124 X is the current output position assigned to the new glyph string
20125 constructed. HL overrides that face of the glyph; e.g. it is
20126 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20127 x-position of the drawing area. */
20128
20129 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20130 do { \
20131 int face_id = (row)->glyphs[area][START].face_id; \
20132 struct face *base_face = FACE_FROM_ID (f, face_id); \
20133 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20134 struct composition *cmp = composition_table[cmp_id]; \
20135 XChar2b *char2b; \
20136 struct glyph_string *first_s; \
20137 int n; \
20138 \
20139 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20140 \
20141 /* Make glyph_strings for each glyph sequence that is drawable by \
20142 the same face, and append them to HEAD/TAIL. */ \
20143 for (n = 0; n < cmp->glyph_len;) \
20144 { \
20145 s = (struct glyph_string *) alloca (sizeof *s); \
20146 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20147 append_glyph_string (&(HEAD), &(TAIL), s); \
20148 s->cmp = cmp; \
20149 s->cmp_from = n; \
20150 s->x = (X); \
20151 if (n == 0) \
20152 first_s = s; \
20153 n = fill_composite_glyph_string (s, base_face, overlaps); \
20154 } \
20155 \
20156 ++START; \
20157 s = first_s; \
20158 } while (0)
20159
20160
20161 /* Add a glyph string for a glyph-string sequence to the list of strings
20162 between HEAD and TAIL. */
20163
20164 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20165 do { \
20166 int face_id; \
20167 XChar2b *char2b; \
20168 Lisp_Object gstring; \
20169 \
20170 face_id = (row)->glyphs[area][START].face_id; \
20171 gstring = (composition_gstring_from_id \
20172 ((row)->glyphs[area][START].u.cmp.id)); \
20173 s = (struct glyph_string *) alloca (sizeof *s); \
20174 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20175 * LGSTRING_GLYPH_LEN (gstring)); \
20176 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20177 append_glyph_string (&(HEAD), &(TAIL), s); \
20178 s->x = (X); \
20179 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20180 } while (0)
20181
20182
20183 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20184 of AREA of glyph row ROW on window W between indices START and END.
20185 HL overrides the face for drawing glyph strings, e.g. it is
20186 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20187 x-positions of the drawing area.
20188
20189 This is an ugly monster macro construct because we must use alloca
20190 to allocate glyph strings (because draw_glyphs can be called
20191 asynchronously). */
20192
20193 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20194 do \
20195 { \
20196 HEAD = TAIL = NULL; \
20197 while (START < END) \
20198 { \
20199 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20200 switch (first_glyph->type) \
20201 { \
20202 case CHAR_GLYPH: \
20203 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20204 HL, X, LAST_X); \
20205 break; \
20206 \
20207 case COMPOSITE_GLYPH: \
20208 if (first_glyph->u.cmp.automatic) \
20209 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20210 HL, X, LAST_X); \
20211 else \
20212 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20213 HL, X, LAST_X); \
20214 break; \
20215 \
20216 case STRETCH_GLYPH: \
20217 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20218 HL, X, LAST_X); \
20219 break; \
20220 \
20221 case IMAGE_GLYPH: \
20222 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20223 HL, X, LAST_X); \
20224 break; \
20225 \
20226 default: \
20227 abort (); \
20228 } \
20229 \
20230 if (s) \
20231 { \
20232 set_glyph_string_background_width (s, START, LAST_X); \
20233 (X) += s->width; \
20234 } \
20235 } \
20236 } while (0)
20237
20238
20239 /* Draw glyphs between START and END in AREA of ROW on window W,
20240 starting at x-position X. X is relative to AREA in W. HL is a
20241 face-override with the following meaning:
20242
20243 DRAW_NORMAL_TEXT draw normally
20244 DRAW_CURSOR draw in cursor face
20245 DRAW_MOUSE_FACE draw in mouse face.
20246 DRAW_INVERSE_VIDEO draw in mode line face
20247 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20248 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20249
20250 If OVERLAPS is non-zero, draw only the foreground of characters and
20251 clip to the physical height of ROW. Non-zero value also defines
20252 the overlapping part to be drawn:
20253
20254 OVERLAPS_PRED overlap with preceding rows
20255 OVERLAPS_SUCC overlap with succeeding rows
20256 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20257 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20258
20259 Value is the x-position reached, relative to AREA of W. */
20260
20261 static int
20262 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20263 struct window *w;
20264 int x;
20265 struct glyph_row *row;
20266 enum glyph_row_area area;
20267 EMACS_INT start, end;
20268 enum draw_glyphs_face hl;
20269 int overlaps;
20270 {
20271 struct glyph_string *head, *tail;
20272 struct glyph_string *s;
20273 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20274 int i, j, x_reached, last_x, area_left = 0;
20275 struct frame *f = XFRAME (WINDOW_FRAME (w));
20276 DECLARE_HDC (hdc);
20277
20278 ALLOCATE_HDC (hdc, f);
20279
20280 /* Let's rather be paranoid than getting a SEGV. */
20281 end = min (end, row->used[area]);
20282 start = max (0, start);
20283 start = min (end, start);
20284
20285 /* Translate X to frame coordinates. Set last_x to the right
20286 end of the drawing area. */
20287 if (row->full_width_p)
20288 {
20289 /* X is relative to the left edge of W, without scroll bars
20290 or fringes. */
20291 area_left = WINDOW_LEFT_EDGE_X (w);
20292 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20293 }
20294 else
20295 {
20296 area_left = window_box_left (w, area);
20297 last_x = area_left + window_box_width (w, area);
20298 }
20299 x += area_left;
20300
20301 /* Build a doubly-linked list of glyph_string structures between
20302 head and tail from what we have to draw. Note that the macro
20303 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20304 the reason we use a separate variable `i'. */
20305 i = start;
20306 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20307 if (tail)
20308 x_reached = tail->x + tail->background_width;
20309 else
20310 x_reached = x;
20311
20312 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20313 the row, redraw some glyphs in front or following the glyph
20314 strings built above. */
20315 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20316 {
20317 struct glyph_string *h, *t;
20318 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20319 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20320 int dummy_x = 0;
20321
20322 /* If mouse highlighting is on, we may need to draw adjacent
20323 glyphs using mouse-face highlighting. */
20324 if (area == TEXT_AREA && row->mouse_face_p)
20325 {
20326 struct glyph_row *mouse_beg_row, *mouse_end_row;
20327
20328 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20329 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20330
20331 if (row >= mouse_beg_row && row <= mouse_end_row)
20332 {
20333 check_mouse_face = 1;
20334 mouse_beg_col = (row == mouse_beg_row)
20335 ? dpyinfo->mouse_face_beg_col : 0;
20336 mouse_end_col = (row == mouse_end_row)
20337 ? dpyinfo->mouse_face_end_col
20338 : row->used[TEXT_AREA];
20339 }
20340 }
20341
20342 /* Compute overhangs for all glyph strings. */
20343 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20344 for (s = head; s; s = s->next)
20345 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20346
20347 /* Prepend glyph strings for glyphs in front of the first glyph
20348 string that are overwritten because of the first glyph
20349 string's left overhang. The background of all strings
20350 prepended must be drawn because the first glyph string
20351 draws over it. */
20352 i = left_overwritten (head);
20353 if (i >= 0)
20354 {
20355 enum draw_glyphs_face overlap_hl;
20356
20357 /* If this row contains mouse highlighting, attempt to draw
20358 the overlapped glyphs with the correct highlight. This
20359 code fails if the overlap encompasses more than one glyph
20360 and mouse-highlight spans only some of these glyphs.
20361 However, making it work perfectly involves a lot more
20362 code, and I don't know if the pathological case occurs in
20363 practice, so we'll stick to this for now. --- cyd */
20364 if (check_mouse_face
20365 && mouse_beg_col < start && mouse_end_col > i)
20366 overlap_hl = DRAW_MOUSE_FACE;
20367 else
20368 overlap_hl = DRAW_NORMAL_TEXT;
20369
20370 j = i;
20371 BUILD_GLYPH_STRINGS (j, start, h, t,
20372 overlap_hl, dummy_x, last_x);
20373 compute_overhangs_and_x (t, head->x, 1);
20374 prepend_glyph_string_lists (&head, &tail, h, t);
20375 clip_head = head;
20376 }
20377
20378 /* Prepend glyph strings for glyphs in front of the first glyph
20379 string that overwrite that glyph string because of their
20380 right overhang. For these strings, only the foreground must
20381 be drawn, because it draws over the glyph string at `head'.
20382 The background must not be drawn because this would overwrite
20383 right overhangs of preceding glyphs for which no glyph
20384 strings exist. */
20385 i = left_overwriting (head);
20386 if (i >= 0)
20387 {
20388 enum draw_glyphs_face overlap_hl;
20389
20390 if (check_mouse_face
20391 && mouse_beg_col < start && mouse_end_col > i)
20392 overlap_hl = DRAW_MOUSE_FACE;
20393 else
20394 overlap_hl = DRAW_NORMAL_TEXT;
20395
20396 clip_head = head;
20397 BUILD_GLYPH_STRINGS (i, start, h, t,
20398 overlap_hl, dummy_x, last_x);
20399 for (s = h; s; s = s->next)
20400 s->background_filled_p = 1;
20401 compute_overhangs_and_x (t, head->x, 1);
20402 prepend_glyph_string_lists (&head, &tail, h, t);
20403 }
20404
20405 /* Append glyphs strings for glyphs following the last glyph
20406 string tail that are overwritten by tail. The background of
20407 these strings has to be drawn because tail's foreground draws
20408 over it. */
20409 i = right_overwritten (tail);
20410 if (i >= 0)
20411 {
20412 enum draw_glyphs_face overlap_hl;
20413
20414 if (check_mouse_face
20415 && mouse_beg_col < i && mouse_end_col > end)
20416 overlap_hl = DRAW_MOUSE_FACE;
20417 else
20418 overlap_hl = DRAW_NORMAL_TEXT;
20419
20420 BUILD_GLYPH_STRINGS (end, i, h, t,
20421 overlap_hl, x, last_x);
20422 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20423 append_glyph_string_lists (&head, &tail, h, t);
20424 clip_tail = tail;
20425 }
20426
20427 /* Append glyph strings for glyphs following the last glyph
20428 string tail that overwrite tail. The foreground of such
20429 glyphs has to be drawn because it writes into the background
20430 of tail. The background must not be drawn because it could
20431 paint over the foreground of following glyphs. */
20432 i = right_overwriting (tail);
20433 if (i >= 0)
20434 {
20435 enum draw_glyphs_face overlap_hl;
20436 if (check_mouse_face
20437 && mouse_beg_col < i && mouse_end_col > end)
20438 overlap_hl = DRAW_MOUSE_FACE;
20439 else
20440 overlap_hl = DRAW_NORMAL_TEXT;
20441
20442 clip_tail = tail;
20443 i++; /* We must include the Ith glyph. */
20444 BUILD_GLYPH_STRINGS (end, i, h, t,
20445 overlap_hl, x, last_x);
20446 for (s = h; s; s = s->next)
20447 s->background_filled_p = 1;
20448 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20449 append_glyph_string_lists (&head, &tail, h, t);
20450 }
20451 if (clip_head || clip_tail)
20452 for (s = head; s; s = s->next)
20453 {
20454 s->clip_head = clip_head;
20455 s->clip_tail = clip_tail;
20456 }
20457 }
20458
20459 /* Draw all strings. */
20460 for (s = head; s; s = s->next)
20461 FRAME_RIF (f)->draw_glyph_string (s);
20462
20463 #ifndef HAVE_NS
20464 /* When focus a sole frame and move horizontally, this sets on_p to 0
20465 causing a failure to erase prev cursor position. */
20466 if (area == TEXT_AREA
20467 && !row->full_width_p
20468 /* When drawing overlapping rows, only the glyph strings'
20469 foreground is drawn, which doesn't erase a cursor
20470 completely. */
20471 && !overlaps)
20472 {
20473 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20474 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20475 : (tail ? tail->x + tail->background_width : x));
20476 x0 -= area_left;
20477 x1 -= area_left;
20478
20479 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20480 row->y, MATRIX_ROW_BOTTOM_Y (row));
20481 }
20482 #endif
20483
20484 /* Value is the x-position up to which drawn, relative to AREA of W.
20485 This doesn't include parts drawn because of overhangs. */
20486 if (row->full_width_p)
20487 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20488 else
20489 x_reached -= area_left;
20490
20491 RELEASE_HDC (hdc, f);
20492
20493 return x_reached;
20494 }
20495
20496 /* Expand row matrix if too narrow. Don't expand if area
20497 is not present. */
20498
20499 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20500 { \
20501 if (!fonts_changed_p \
20502 && (it->glyph_row->glyphs[area] \
20503 < it->glyph_row->glyphs[area + 1])) \
20504 { \
20505 it->w->ncols_scale_factor++; \
20506 fonts_changed_p = 1; \
20507 } \
20508 }
20509
20510 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20511 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20512
20513 static INLINE void
20514 append_glyph (it)
20515 struct it *it;
20516 {
20517 struct glyph *glyph;
20518 enum glyph_row_area area = it->area;
20519
20520 xassert (it->glyph_row);
20521 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20522
20523 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20524 if (glyph < it->glyph_row->glyphs[area + 1])
20525 {
20526 glyph->charpos = CHARPOS (it->position);
20527 glyph->object = it->object;
20528 if (it->pixel_width > 0)
20529 {
20530 glyph->pixel_width = it->pixel_width;
20531 glyph->padding_p = 0;
20532 }
20533 else
20534 {
20535 /* Assure at least 1-pixel width. Otherwise, cursor can't
20536 be displayed correctly. */
20537 glyph->pixel_width = 1;
20538 glyph->padding_p = 1;
20539 }
20540 glyph->ascent = it->ascent;
20541 glyph->descent = it->descent;
20542 glyph->voffset = it->voffset;
20543 glyph->type = CHAR_GLYPH;
20544 glyph->avoid_cursor_p = it->avoid_cursor_p;
20545 glyph->multibyte_p = it->multibyte_p;
20546 glyph->left_box_line_p = it->start_of_box_run_p;
20547 glyph->right_box_line_p = it->end_of_box_run_p;
20548 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20549 || it->phys_descent > it->descent);
20550 glyph->glyph_not_available_p = it->glyph_not_available_p;
20551 glyph->face_id = it->face_id;
20552 glyph->u.ch = it->char_to_display;
20553 glyph->slice = null_glyph_slice;
20554 glyph->font_type = FONT_TYPE_UNKNOWN;
20555 ++it->glyph_row->used[area];
20556 }
20557 else
20558 IT_EXPAND_MATRIX_WIDTH (it, area);
20559 }
20560
20561 /* Store one glyph for the composition IT->cmp_it.id in
20562 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20563 non-null. */
20564
20565 static INLINE void
20566 append_composite_glyph (it)
20567 struct it *it;
20568 {
20569 struct glyph *glyph;
20570 enum glyph_row_area area = it->area;
20571
20572 xassert (it->glyph_row);
20573
20574 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20575 if (glyph < it->glyph_row->glyphs[area + 1])
20576 {
20577 glyph->charpos = CHARPOS (it->position);
20578 glyph->object = it->object;
20579 glyph->pixel_width = it->pixel_width;
20580 glyph->ascent = it->ascent;
20581 glyph->descent = it->descent;
20582 glyph->voffset = it->voffset;
20583 glyph->type = COMPOSITE_GLYPH;
20584 if (it->cmp_it.ch < 0)
20585 {
20586 glyph->u.cmp.automatic = 0;
20587 glyph->u.cmp.id = it->cmp_it.id;
20588 }
20589 else
20590 {
20591 glyph->u.cmp.automatic = 1;
20592 glyph->u.cmp.id = it->cmp_it.id;
20593 glyph->u.cmp.from = it->cmp_it.from;
20594 glyph->u.cmp.to = it->cmp_it.to - 1;
20595 }
20596 glyph->avoid_cursor_p = it->avoid_cursor_p;
20597 glyph->multibyte_p = it->multibyte_p;
20598 glyph->left_box_line_p = it->start_of_box_run_p;
20599 glyph->right_box_line_p = it->end_of_box_run_p;
20600 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20601 || it->phys_descent > it->descent);
20602 glyph->padding_p = 0;
20603 glyph->glyph_not_available_p = 0;
20604 glyph->face_id = it->face_id;
20605 glyph->slice = null_glyph_slice;
20606 glyph->font_type = FONT_TYPE_UNKNOWN;
20607 ++it->glyph_row->used[area];
20608 }
20609 else
20610 IT_EXPAND_MATRIX_WIDTH (it, area);
20611 }
20612
20613
20614 /* Change IT->ascent and IT->height according to the setting of
20615 IT->voffset. */
20616
20617 static INLINE void
20618 take_vertical_position_into_account (it)
20619 struct it *it;
20620 {
20621 if (it->voffset)
20622 {
20623 if (it->voffset < 0)
20624 /* Increase the ascent so that we can display the text higher
20625 in the line. */
20626 it->ascent -= it->voffset;
20627 else
20628 /* Increase the descent so that we can display the text lower
20629 in the line. */
20630 it->descent += it->voffset;
20631 }
20632 }
20633
20634
20635 /* Produce glyphs/get display metrics for the image IT is loaded with.
20636 See the description of struct display_iterator in dispextern.h for
20637 an overview of struct display_iterator. */
20638
20639 static void
20640 produce_image_glyph (it)
20641 struct it *it;
20642 {
20643 struct image *img;
20644 struct face *face;
20645 int glyph_ascent, crop;
20646 struct glyph_slice slice;
20647
20648 xassert (it->what == IT_IMAGE);
20649
20650 face = FACE_FROM_ID (it->f, it->face_id);
20651 xassert (face);
20652 /* Make sure X resources of the face is loaded. */
20653 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20654
20655 if (it->image_id < 0)
20656 {
20657 /* Fringe bitmap. */
20658 it->ascent = it->phys_ascent = 0;
20659 it->descent = it->phys_descent = 0;
20660 it->pixel_width = 0;
20661 it->nglyphs = 0;
20662 return;
20663 }
20664
20665 img = IMAGE_FROM_ID (it->f, it->image_id);
20666 xassert (img);
20667 /* Make sure X resources of the image is loaded. */
20668 prepare_image_for_display (it->f, img);
20669
20670 slice.x = slice.y = 0;
20671 slice.width = img->width;
20672 slice.height = img->height;
20673
20674 if (INTEGERP (it->slice.x))
20675 slice.x = XINT (it->slice.x);
20676 else if (FLOATP (it->slice.x))
20677 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20678
20679 if (INTEGERP (it->slice.y))
20680 slice.y = XINT (it->slice.y);
20681 else if (FLOATP (it->slice.y))
20682 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20683
20684 if (INTEGERP (it->slice.width))
20685 slice.width = XINT (it->slice.width);
20686 else if (FLOATP (it->slice.width))
20687 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20688
20689 if (INTEGERP (it->slice.height))
20690 slice.height = XINT (it->slice.height);
20691 else if (FLOATP (it->slice.height))
20692 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20693
20694 if (slice.x >= img->width)
20695 slice.x = img->width;
20696 if (slice.y >= img->height)
20697 slice.y = img->height;
20698 if (slice.x + slice.width >= img->width)
20699 slice.width = img->width - slice.x;
20700 if (slice.y + slice.height > img->height)
20701 slice.height = img->height - slice.y;
20702
20703 if (slice.width == 0 || slice.height == 0)
20704 return;
20705
20706 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20707
20708 it->descent = slice.height - glyph_ascent;
20709 if (slice.y == 0)
20710 it->descent += img->vmargin;
20711 if (slice.y + slice.height == img->height)
20712 it->descent += img->vmargin;
20713 it->phys_descent = it->descent;
20714
20715 it->pixel_width = slice.width;
20716 if (slice.x == 0)
20717 it->pixel_width += img->hmargin;
20718 if (slice.x + slice.width == img->width)
20719 it->pixel_width += img->hmargin;
20720
20721 /* It's quite possible for images to have an ascent greater than
20722 their height, so don't get confused in that case. */
20723 if (it->descent < 0)
20724 it->descent = 0;
20725
20726 it->nglyphs = 1;
20727
20728 if (face->box != FACE_NO_BOX)
20729 {
20730 if (face->box_line_width > 0)
20731 {
20732 if (slice.y == 0)
20733 it->ascent += face->box_line_width;
20734 if (slice.y + slice.height == img->height)
20735 it->descent += face->box_line_width;
20736 }
20737
20738 if (it->start_of_box_run_p && slice.x == 0)
20739 it->pixel_width += eabs (face->box_line_width);
20740 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20741 it->pixel_width += eabs (face->box_line_width);
20742 }
20743
20744 take_vertical_position_into_account (it);
20745
20746 /* Automatically crop wide image glyphs at right edge so we can
20747 draw the cursor on same display row. */
20748 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20749 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20750 {
20751 it->pixel_width -= crop;
20752 slice.width -= crop;
20753 }
20754
20755 if (it->glyph_row)
20756 {
20757 struct glyph *glyph;
20758 enum glyph_row_area area = it->area;
20759
20760 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20761 if (glyph < it->glyph_row->glyphs[area + 1])
20762 {
20763 glyph->charpos = CHARPOS (it->position);
20764 glyph->object = it->object;
20765 glyph->pixel_width = it->pixel_width;
20766 glyph->ascent = glyph_ascent;
20767 glyph->descent = it->descent;
20768 glyph->voffset = it->voffset;
20769 glyph->type = IMAGE_GLYPH;
20770 glyph->avoid_cursor_p = it->avoid_cursor_p;
20771 glyph->multibyte_p = it->multibyte_p;
20772 glyph->left_box_line_p = it->start_of_box_run_p;
20773 glyph->right_box_line_p = it->end_of_box_run_p;
20774 glyph->overlaps_vertically_p = 0;
20775 glyph->padding_p = 0;
20776 glyph->glyph_not_available_p = 0;
20777 glyph->face_id = it->face_id;
20778 glyph->u.img_id = img->id;
20779 glyph->slice = slice;
20780 glyph->font_type = FONT_TYPE_UNKNOWN;
20781 ++it->glyph_row->used[area];
20782 }
20783 else
20784 IT_EXPAND_MATRIX_WIDTH (it, area);
20785 }
20786 }
20787
20788
20789 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20790 of the glyph, WIDTH and HEIGHT are the width and height of the
20791 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20792
20793 static void
20794 append_stretch_glyph (it, object, width, height, ascent)
20795 struct it *it;
20796 Lisp_Object object;
20797 int width, height;
20798 int ascent;
20799 {
20800 struct glyph *glyph;
20801 enum glyph_row_area area = it->area;
20802
20803 xassert (ascent >= 0 && ascent <= height);
20804
20805 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20806 if (glyph < it->glyph_row->glyphs[area + 1])
20807 {
20808 glyph->charpos = CHARPOS (it->position);
20809 glyph->object = object;
20810 glyph->pixel_width = width;
20811 glyph->ascent = ascent;
20812 glyph->descent = height - ascent;
20813 glyph->voffset = it->voffset;
20814 glyph->type = STRETCH_GLYPH;
20815 glyph->avoid_cursor_p = it->avoid_cursor_p;
20816 glyph->multibyte_p = it->multibyte_p;
20817 glyph->left_box_line_p = it->start_of_box_run_p;
20818 glyph->right_box_line_p = it->end_of_box_run_p;
20819 glyph->overlaps_vertically_p = 0;
20820 glyph->padding_p = 0;
20821 glyph->glyph_not_available_p = 0;
20822 glyph->face_id = it->face_id;
20823 glyph->u.stretch.ascent = ascent;
20824 glyph->u.stretch.height = height;
20825 glyph->slice = null_glyph_slice;
20826 glyph->font_type = FONT_TYPE_UNKNOWN;
20827 ++it->glyph_row->used[area];
20828 }
20829 else
20830 IT_EXPAND_MATRIX_WIDTH (it, area);
20831 }
20832
20833
20834 /* Produce a stretch glyph for iterator IT. IT->object is the value
20835 of the glyph property displayed. The value must be a list
20836 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20837 being recognized:
20838
20839 1. `:width WIDTH' specifies that the space should be WIDTH *
20840 canonical char width wide. WIDTH may be an integer or floating
20841 point number.
20842
20843 2. `:relative-width FACTOR' specifies that the width of the stretch
20844 should be computed from the width of the first character having the
20845 `glyph' property, and should be FACTOR times that width.
20846
20847 3. `:align-to HPOS' specifies that the space should be wide enough
20848 to reach HPOS, a value in canonical character units.
20849
20850 Exactly one of the above pairs must be present.
20851
20852 4. `:height HEIGHT' specifies that the height of the stretch produced
20853 should be HEIGHT, measured in canonical character units.
20854
20855 5. `:relative-height FACTOR' specifies that the height of the
20856 stretch should be FACTOR times the height of the characters having
20857 the glyph property.
20858
20859 Either none or exactly one of 4 or 5 must be present.
20860
20861 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20862 of the stretch should be used for the ascent of the stretch.
20863 ASCENT must be in the range 0 <= ASCENT <= 100. */
20864
20865 static void
20866 produce_stretch_glyph (it)
20867 struct it *it;
20868 {
20869 /* (space :width WIDTH :height HEIGHT ...) */
20870 Lisp_Object prop, plist;
20871 int width = 0, height = 0, align_to = -1;
20872 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20873 int ascent = 0;
20874 double tem;
20875 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20876 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20877
20878 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20879
20880 /* List should start with `space'. */
20881 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20882 plist = XCDR (it->object);
20883
20884 /* Compute the width of the stretch. */
20885 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20886 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20887 {
20888 /* Absolute width `:width WIDTH' specified and valid. */
20889 zero_width_ok_p = 1;
20890 width = (int)tem;
20891 }
20892 else if (prop = Fplist_get (plist, QCrelative_width),
20893 NUMVAL (prop) > 0)
20894 {
20895 /* Relative width `:relative-width FACTOR' specified and valid.
20896 Compute the width of the characters having the `glyph'
20897 property. */
20898 struct it it2;
20899 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20900
20901 it2 = *it;
20902 if (it->multibyte_p)
20903 {
20904 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20905 - IT_BYTEPOS (*it));
20906 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20907 }
20908 else
20909 it2.c = *p, it2.len = 1;
20910
20911 it2.glyph_row = NULL;
20912 it2.what = IT_CHARACTER;
20913 x_produce_glyphs (&it2);
20914 width = NUMVAL (prop) * it2.pixel_width;
20915 }
20916 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20917 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20918 {
20919 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20920 align_to = (align_to < 0
20921 ? 0
20922 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20923 else if (align_to < 0)
20924 align_to = window_box_left_offset (it->w, TEXT_AREA);
20925 width = max (0, (int)tem + align_to - it->current_x);
20926 zero_width_ok_p = 1;
20927 }
20928 else
20929 /* Nothing specified -> width defaults to canonical char width. */
20930 width = FRAME_COLUMN_WIDTH (it->f);
20931
20932 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20933 width = 1;
20934
20935 /* Compute height. */
20936 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20937 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20938 {
20939 height = (int)tem;
20940 zero_height_ok_p = 1;
20941 }
20942 else if (prop = Fplist_get (plist, QCrelative_height),
20943 NUMVAL (prop) > 0)
20944 height = FONT_HEIGHT (font) * NUMVAL (prop);
20945 else
20946 height = FONT_HEIGHT (font);
20947
20948 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20949 height = 1;
20950
20951 /* Compute percentage of height used for ascent. If
20952 `:ascent ASCENT' is present and valid, use that. Otherwise,
20953 derive the ascent from the font in use. */
20954 if (prop = Fplist_get (plist, QCascent),
20955 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20956 ascent = height * NUMVAL (prop) / 100.0;
20957 else if (!NILP (prop)
20958 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20959 ascent = min (max (0, (int)tem), height);
20960 else
20961 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20962
20963 if (width > 0 && it->line_wrap != TRUNCATE
20964 && it->current_x + width > it->last_visible_x)
20965 width = it->last_visible_x - it->current_x - 1;
20966
20967 if (width > 0 && height > 0 && it->glyph_row)
20968 {
20969 Lisp_Object object = it->stack[it->sp - 1].string;
20970 if (!STRINGP (object))
20971 object = it->w->buffer;
20972 append_stretch_glyph (it, object, width, height, ascent);
20973 }
20974
20975 it->pixel_width = width;
20976 it->ascent = it->phys_ascent = ascent;
20977 it->descent = it->phys_descent = height - it->ascent;
20978 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20979
20980 take_vertical_position_into_account (it);
20981 }
20982
20983 /* Calculate line-height and line-spacing properties.
20984 An integer value specifies explicit pixel value.
20985 A float value specifies relative value to current face height.
20986 A cons (float . face-name) specifies relative value to
20987 height of specified face font.
20988
20989 Returns height in pixels, or nil. */
20990
20991
20992 static Lisp_Object
20993 calc_line_height_property (it, val, font, boff, override)
20994 struct it *it;
20995 Lisp_Object val;
20996 struct font *font;
20997 int boff, override;
20998 {
20999 Lisp_Object face_name = Qnil;
21000 int ascent, descent, height;
21001
21002 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21003 return val;
21004
21005 if (CONSP (val))
21006 {
21007 face_name = XCAR (val);
21008 val = XCDR (val);
21009 if (!NUMBERP (val))
21010 val = make_number (1);
21011 if (NILP (face_name))
21012 {
21013 height = it->ascent + it->descent;
21014 goto scale;
21015 }
21016 }
21017
21018 if (NILP (face_name))
21019 {
21020 font = FRAME_FONT (it->f);
21021 boff = FRAME_BASELINE_OFFSET (it->f);
21022 }
21023 else if (EQ (face_name, Qt))
21024 {
21025 override = 0;
21026 }
21027 else
21028 {
21029 int face_id;
21030 struct face *face;
21031
21032 face_id = lookup_named_face (it->f, face_name, 0);
21033 if (face_id < 0)
21034 return make_number (-1);
21035
21036 face = FACE_FROM_ID (it->f, face_id);
21037 font = face->font;
21038 if (font == NULL)
21039 return make_number (-1);
21040 boff = font->baseline_offset;
21041 if (font->vertical_centering)
21042 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21043 }
21044
21045 ascent = FONT_BASE (font) + boff;
21046 descent = FONT_DESCENT (font) - boff;
21047
21048 if (override)
21049 {
21050 it->override_ascent = ascent;
21051 it->override_descent = descent;
21052 it->override_boff = boff;
21053 }
21054
21055 height = ascent + descent;
21056
21057 scale:
21058 if (FLOATP (val))
21059 height = (int)(XFLOAT_DATA (val) * height);
21060 else if (INTEGERP (val))
21061 height *= XINT (val);
21062
21063 return make_number (height);
21064 }
21065
21066
21067 /* RIF:
21068 Produce glyphs/get display metrics for the display element IT is
21069 loaded with. See the description of struct it in dispextern.h
21070 for an overview of struct it. */
21071
21072 void
21073 x_produce_glyphs (it)
21074 struct it *it;
21075 {
21076 int extra_line_spacing = it->extra_line_spacing;
21077
21078 it->glyph_not_available_p = 0;
21079
21080 if (it->what == IT_CHARACTER)
21081 {
21082 XChar2b char2b;
21083 struct font *font;
21084 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21085 struct font_metrics *pcm;
21086 int font_not_found_p;
21087 int boff; /* baseline offset */
21088 /* We may change it->multibyte_p upon unibyte<->multibyte
21089 conversion. So, save the current value now and restore it
21090 later.
21091
21092 Note: It seems that we don't have to record multibyte_p in
21093 struct glyph because the character code itself tells if or
21094 not the character is multibyte. Thus, in the future, we must
21095 consider eliminating the field `multibyte_p' in the struct
21096 glyph. */
21097 int saved_multibyte_p = it->multibyte_p;
21098
21099 /* Maybe translate single-byte characters to multibyte, or the
21100 other way. */
21101 it->char_to_display = it->c;
21102 if (!ASCII_BYTE_P (it->c)
21103 && ! it->multibyte_p)
21104 {
21105 if (SINGLE_BYTE_CHAR_P (it->c)
21106 && unibyte_display_via_language_environment)
21107 {
21108 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21109
21110 /* get_next_display_element assures that this decoding
21111 never fails. */
21112 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21113 it->multibyte_p = 1;
21114 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21115 -1, Qnil);
21116 face = FACE_FROM_ID (it->f, it->face_id);
21117 }
21118 }
21119
21120 /* Get font to use. Encode IT->char_to_display. */
21121 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21122 &char2b, it->multibyte_p, 0);
21123 font = face->font;
21124
21125 /* When no suitable font found, use the default font. */
21126 font_not_found_p = font == NULL;
21127 if (font_not_found_p)
21128 {
21129 font = FRAME_FONT (it->f);
21130 boff = FRAME_BASELINE_OFFSET (it->f);
21131 }
21132 else
21133 {
21134 boff = font->baseline_offset;
21135 if (font->vertical_centering)
21136 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21137 }
21138
21139 if (it->char_to_display >= ' '
21140 && (!it->multibyte_p || it->char_to_display < 128))
21141 {
21142 /* Either unibyte or ASCII. */
21143 int stretched_p;
21144
21145 it->nglyphs = 1;
21146
21147 pcm = get_per_char_metric (it->f, font, &char2b);
21148
21149 if (it->override_ascent >= 0)
21150 {
21151 it->ascent = it->override_ascent;
21152 it->descent = it->override_descent;
21153 boff = it->override_boff;
21154 }
21155 else
21156 {
21157 it->ascent = FONT_BASE (font) + boff;
21158 it->descent = FONT_DESCENT (font) - boff;
21159 }
21160
21161 if (pcm)
21162 {
21163 it->phys_ascent = pcm->ascent + boff;
21164 it->phys_descent = pcm->descent - boff;
21165 it->pixel_width = pcm->width;
21166 }
21167 else
21168 {
21169 it->glyph_not_available_p = 1;
21170 it->phys_ascent = it->ascent;
21171 it->phys_descent = it->descent;
21172 it->pixel_width = FONT_WIDTH (font);
21173 }
21174
21175 if (it->constrain_row_ascent_descent_p)
21176 {
21177 if (it->descent > it->max_descent)
21178 {
21179 it->ascent += it->descent - it->max_descent;
21180 it->descent = it->max_descent;
21181 }
21182 if (it->ascent > it->max_ascent)
21183 {
21184 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21185 it->ascent = it->max_ascent;
21186 }
21187 it->phys_ascent = min (it->phys_ascent, it->ascent);
21188 it->phys_descent = min (it->phys_descent, it->descent);
21189 extra_line_spacing = 0;
21190 }
21191
21192 /* If this is a space inside a region of text with
21193 `space-width' property, change its width. */
21194 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21195 if (stretched_p)
21196 it->pixel_width *= XFLOATINT (it->space_width);
21197
21198 /* If face has a box, add the box thickness to the character
21199 height. If character has a box line to the left and/or
21200 right, add the box line width to the character's width. */
21201 if (face->box != FACE_NO_BOX)
21202 {
21203 int thick = face->box_line_width;
21204
21205 if (thick > 0)
21206 {
21207 it->ascent += thick;
21208 it->descent += thick;
21209 }
21210 else
21211 thick = -thick;
21212
21213 if (it->start_of_box_run_p)
21214 it->pixel_width += thick;
21215 if (it->end_of_box_run_p)
21216 it->pixel_width += thick;
21217 }
21218
21219 /* If face has an overline, add the height of the overline
21220 (1 pixel) and a 1 pixel margin to the character height. */
21221 if (face->overline_p)
21222 it->ascent += overline_margin;
21223
21224 if (it->constrain_row_ascent_descent_p)
21225 {
21226 if (it->ascent > it->max_ascent)
21227 it->ascent = it->max_ascent;
21228 if (it->descent > it->max_descent)
21229 it->descent = it->max_descent;
21230 }
21231
21232 take_vertical_position_into_account (it);
21233
21234 /* If we have to actually produce glyphs, do it. */
21235 if (it->glyph_row)
21236 {
21237 if (stretched_p)
21238 {
21239 /* Translate a space with a `space-width' property
21240 into a stretch glyph. */
21241 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21242 / FONT_HEIGHT (font));
21243 append_stretch_glyph (it, it->object, it->pixel_width,
21244 it->ascent + it->descent, ascent);
21245 }
21246 else
21247 append_glyph (it);
21248
21249 /* If characters with lbearing or rbearing are displayed
21250 in this line, record that fact in a flag of the
21251 glyph row. This is used to optimize X output code. */
21252 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21253 it->glyph_row->contains_overlapping_glyphs_p = 1;
21254 }
21255 if (! stretched_p && it->pixel_width == 0)
21256 /* We assure that all visible glyphs have at least 1-pixel
21257 width. */
21258 it->pixel_width = 1;
21259 }
21260 else if (it->char_to_display == '\n')
21261 {
21262 /* A newline has no width but we need the height of the line.
21263 But if previous part of the line set a height, don't
21264 increase that height */
21265
21266 Lisp_Object height;
21267 Lisp_Object total_height = Qnil;
21268
21269 it->override_ascent = -1;
21270 it->pixel_width = 0;
21271 it->nglyphs = 0;
21272
21273 height = get_it_property(it, Qline_height);
21274 /* Split (line-height total-height) list */
21275 if (CONSP (height)
21276 && CONSP (XCDR (height))
21277 && NILP (XCDR (XCDR (height))))
21278 {
21279 total_height = XCAR (XCDR (height));
21280 height = XCAR (height);
21281 }
21282 height = calc_line_height_property(it, height, font, boff, 1);
21283
21284 if (it->override_ascent >= 0)
21285 {
21286 it->ascent = it->override_ascent;
21287 it->descent = it->override_descent;
21288 boff = it->override_boff;
21289 }
21290 else
21291 {
21292 it->ascent = FONT_BASE (font) + boff;
21293 it->descent = FONT_DESCENT (font) - boff;
21294 }
21295
21296 if (EQ (height, Qt))
21297 {
21298 if (it->descent > it->max_descent)
21299 {
21300 it->ascent += it->descent - it->max_descent;
21301 it->descent = it->max_descent;
21302 }
21303 if (it->ascent > it->max_ascent)
21304 {
21305 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21306 it->ascent = it->max_ascent;
21307 }
21308 it->phys_ascent = min (it->phys_ascent, it->ascent);
21309 it->phys_descent = min (it->phys_descent, it->descent);
21310 it->constrain_row_ascent_descent_p = 1;
21311 extra_line_spacing = 0;
21312 }
21313 else
21314 {
21315 Lisp_Object spacing;
21316
21317 it->phys_ascent = it->ascent;
21318 it->phys_descent = it->descent;
21319
21320 if ((it->max_ascent > 0 || it->max_descent > 0)
21321 && face->box != FACE_NO_BOX
21322 && face->box_line_width > 0)
21323 {
21324 it->ascent += face->box_line_width;
21325 it->descent += face->box_line_width;
21326 }
21327 if (!NILP (height)
21328 && XINT (height) > it->ascent + it->descent)
21329 it->ascent = XINT (height) - it->descent;
21330
21331 if (!NILP (total_height))
21332 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21333 else
21334 {
21335 spacing = get_it_property(it, Qline_spacing);
21336 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21337 }
21338 if (INTEGERP (spacing))
21339 {
21340 extra_line_spacing = XINT (spacing);
21341 if (!NILP (total_height))
21342 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21343 }
21344 }
21345 }
21346 else if (it->char_to_display == '\t')
21347 {
21348 if (font->space_width > 0)
21349 {
21350 int tab_width = it->tab_width * font->space_width;
21351 int x = it->current_x + it->continuation_lines_width;
21352 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21353
21354 /* If the distance from the current position to the next tab
21355 stop is less than a space character width, use the
21356 tab stop after that. */
21357 if (next_tab_x - x < font->space_width)
21358 next_tab_x += tab_width;
21359
21360 it->pixel_width = next_tab_x - x;
21361 it->nglyphs = 1;
21362 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21363 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21364
21365 if (it->glyph_row)
21366 {
21367 append_stretch_glyph (it, it->object, it->pixel_width,
21368 it->ascent + it->descent, it->ascent);
21369 }
21370 }
21371 else
21372 {
21373 it->pixel_width = 0;
21374 it->nglyphs = 1;
21375 }
21376 }
21377 else
21378 {
21379 /* A multi-byte character. Assume that the display width of the
21380 character is the width of the character multiplied by the
21381 width of the font. */
21382
21383 /* If we found a font, this font should give us the right
21384 metrics. If we didn't find a font, use the frame's
21385 default font and calculate the width of the character by
21386 multiplying the width of font by the width of the
21387 character. */
21388
21389 pcm = get_per_char_metric (it->f, font, &char2b);
21390
21391 if (font_not_found_p || !pcm)
21392 {
21393 int char_width = CHAR_WIDTH (it->char_to_display);
21394
21395 if (char_width == 0)
21396 /* This is a non spacing character. But, as we are
21397 going to display an empty box, the box must occupy
21398 at least one column. */
21399 char_width = 1;
21400 it->glyph_not_available_p = 1;
21401 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21402 it->phys_ascent = FONT_BASE (font) + boff;
21403 it->phys_descent = FONT_DESCENT (font) - boff;
21404 }
21405 else
21406 {
21407 it->pixel_width = pcm->width;
21408 it->phys_ascent = pcm->ascent + boff;
21409 it->phys_descent = pcm->descent - boff;
21410 if (it->glyph_row
21411 && (pcm->lbearing < 0
21412 || pcm->rbearing > pcm->width))
21413 it->glyph_row->contains_overlapping_glyphs_p = 1;
21414 }
21415 it->nglyphs = 1;
21416 it->ascent = FONT_BASE (font) + boff;
21417 it->descent = FONT_DESCENT (font) - boff;
21418 if (face->box != FACE_NO_BOX)
21419 {
21420 int thick = face->box_line_width;
21421
21422 if (thick > 0)
21423 {
21424 it->ascent += thick;
21425 it->descent += thick;
21426 }
21427 else
21428 thick = - thick;
21429
21430 if (it->start_of_box_run_p)
21431 it->pixel_width += thick;
21432 if (it->end_of_box_run_p)
21433 it->pixel_width += thick;
21434 }
21435
21436 /* If face has an overline, add the height of the overline
21437 (1 pixel) and a 1 pixel margin to the character height. */
21438 if (face->overline_p)
21439 it->ascent += overline_margin;
21440
21441 take_vertical_position_into_account (it);
21442
21443 if (it->ascent < 0)
21444 it->ascent = 0;
21445 if (it->descent < 0)
21446 it->descent = 0;
21447
21448 if (it->glyph_row)
21449 append_glyph (it);
21450 if (it->pixel_width == 0)
21451 /* We assure that all visible glyphs have at least 1-pixel
21452 width. */
21453 it->pixel_width = 1;
21454 }
21455 it->multibyte_p = saved_multibyte_p;
21456 }
21457 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21458 {
21459 /* A static compositoin.
21460
21461 Note: A composition is represented as one glyph in the
21462 glyph matrix. There are no padding glyphs.
21463
21464 Important is that pixel_width, ascent, and descent are the
21465 values of what is drawn by draw_glyphs (i.e. the values of
21466 the overall glyphs composed). */
21467 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21468 int boff; /* baseline offset */
21469 struct composition *cmp = composition_table[it->cmp_it.id];
21470 int glyph_len = cmp->glyph_len;
21471 struct font *font = face->font;
21472
21473 it->nglyphs = 1;
21474
21475 /* If we have not yet calculated pixel size data of glyphs of
21476 the composition for the current face font, calculate them
21477 now. Theoretically, we have to check all fonts for the
21478 glyphs, but that requires much time and memory space. So,
21479 here we check only the font of the first glyph. This leads
21480 to incorrect display, but it's very rare, and C-l (recenter)
21481 can correct the display anyway. */
21482 if (! cmp->font || cmp->font != font)
21483 {
21484 /* Ascent and descent of the font of the first character
21485 of this composition (adjusted by baseline offset).
21486 Ascent and descent of overall glyphs should not be less
21487 than them respectively. */
21488 int font_ascent, font_descent, font_height;
21489 /* Bounding box of the overall glyphs. */
21490 int leftmost, rightmost, lowest, highest;
21491 int lbearing, rbearing;
21492 int i, width, ascent, descent;
21493 int left_padded = 0, right_padded = 0;
21494 int c;
21495 XChar2b char2b;
21496 struct font_metrics *pcm;
21497 int font_not_found_p;
21498 int pos;
21499
21500 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21501 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21502 break;
21503 if (glyph_len < cmp->glyph_len)
21504 right_padded = 1;
21505 for (i = 0; i < glyph_len; i++)
21506 {
21507 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21508 break;
21509 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21510 }
21511 if (i > 0)
21512 left_padded = 1;
21513
21514 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21515 : IT_CHARPOS (*it));
21516 /* When no suitable font found, use the default font. */
21517 font_not_found_p = font == NULL;
21518 if (font_not_found_p)
21519 {
21520 face = face->ascii_face;
21521 font = face->font;
21522 }
21523 boff = font->baseline_offset;
21524 if (font->vertical_centering)
21525 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21526 font_ascent = FONT_BASE (font) + boff;
21527 font_descent = FONT_DESCENT (font) - boff;
21528 font_height = FONT_HEIGHT (font);
21529
21530 cmp->font = (void *) font;
21531
21532 pcm = NULL;
21533 if (! font_not_found_p)
21534 {
21535 get_char_face_and_encoding (it->f, c, it->face_id,
21536 &char2b, it->multibyte_p, 0);
21537 pcm = get_per_char_metric (it->f, font, &char2b);
21538 }
21539
21540 /* Initialize the bounding box. */
21541 if (pcm)
21542 {
21543 width = pcm->width;
21544 ascent = pcm->ascent;
21545 descent = pcm->descent;
21546 lbearing = pcm->lbearing;
21547 rbearing = pcm->rbearing;
21548 }
21549 else
21550 {
21551 width = FONT_WIDTH (font);
21552 ascent = FONT_BASE (font);
21553 descent = FONT_DESCENT (font);
21554 lbearing = 0;
21555 rbearing = width;
21556 }
21557
21558 rightmost = width;
21559 leftmost = 0;
21560 lowest = - descent + boff;
21561 highest = ascent + boff;
21562
21563 if (! font_not_found_p
21564 && font->default_ascent
21565 && CHAR_TABLE_P (Vuse_default_ascent)
21566 && !NILP (Faref (Vuse_default_ascent,
21567 make_number (it->char_to_display))))
21568 highest = font->default_ascent + boff;
21569
21570 /* Draw the first glyph at the normal position. It may be
21571 shifted to right later if some other glyphs are drawn
21572 at the left. */
21573 cmp->offsets[i * 2] = 0;
21574 cmp->offsets[i * 2 + 1] = boff;
21575 cmp->lbearing = lbearing;
21576 cmp->rbearing = rbearing;
21577
21578 /* Set cmp->offsets for the remaining glyphs. */
21579 for (i++; i < glyph_len; i++)
21580 {
21581 int left, right, btm, top;
21582 int ch = COMPOSITION_GLYPH (cmp, i);
21583 int face_id;
21584 struct face *this_face;
21585 int this_boff;
21586
21587 if (ch == '\t')
21588 ch = ' ';
21589 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21590 this_face = FACE_FROM_ID (it->f, face_id);
21591 font = this_face->font;
21592
21593 if (font == NULL)
21594 pcm = NULL;
21595 else
21596 {
21597 this_boff = font->baseline_offset;
21598 if (font->vertical_centering)
21599 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21600 get_char_face_and_encoding (it->f, ch, face_id,
21601 &char2b, it->multibyte_p, 0);
21602 pcm = get_per_char_metric (it->f, font, &char2b);
21603 }
21604 if (! pcm)
21605 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21606 else
21607 {
21608 width = pcm->width;
21609 ascent = pcm->ascent;
21610 descent = pcm->descent;
21611 lbearing = pcm->lbearing;
21612 rbearing = pcm->rbearing;
21613 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21614 {
21615 /* Relative composition with or without
21616 alternate chars. */
21617 left = (leftmost + rightmost - width) / 2;
21618 btm = - descent + boff;
21619 if (font->relative_compose
21620 && (! CHAR_TABLE_P (Vignore_relative_composition)
21621 || NILP (Faref (Vignore_relative_composition,
21622 make_number (ch)))))
21623 {
21624
21625 if (- descent >= font->relative_compose)
21626 /* One extra pixel between two glyphs. */
21627 btm = highest + 1;
21628 else if (ascent <= 0)
21629 /* One extra pixel between two glyphs. */
21630 btm = lowest - 1 - ascent - descent;
21631 }
21632 }
21633 else
21634 {
21635 /* A composition rule is specified by an integer
21636 value that encodes global and new reference
21637 points (GREF and NREF). GREF and NREF are
21638 specified by numbers as below:
21639
21640 0---1---2 -- ascent
21641 | |
21642 | |
21643 | |
21644 9--10--11 -- center
21645 | |
21646 ---3---4---5--- baseline
21647 | |
21648 6---7---8 -- descent
21649 */
21650 int rule = COMPOSITION_RULE (cmp, i);
21651 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21652
21653 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21654 grefx = gref % 3, nrefx = nref % 3;
21655 grefy = gref / 3, nrefy = nref / 3;
21656 if (xoff)
21657 xoff = font_height * (xoff - 128) / 256;
21658 if (yoff)
21659 yoff = font_height * (yoff - 128) / 256;
21660
21661 left = (leftmost
21662 + grefx * (rightmost - leftmost) / 2
21663 - nrefx * width / 2
21664 + xoff);
21665
21666 btm = ((grefy == 0 ? highest
21667 : grefy == 1 ? 0
21668 : grefy == 2 ? lowest
21669 : (highest + lowest) / 2)
21670 - (nrefy == 0 ? ascent + descent
21671 : nrefy == 1 ? descent - boff
21672 : nrefy == 2 ? 0
21673 : (ascent + descent) / 2)
21674 + yoff);
21675 }
21676
21677 cmp->offsets[i * 2] = left;
21678 cmp->offsets[i * 2 + 1] = btm + descent;
21679
21680 /* Update the bounding box of the overall glyphs. */
21681 if (width > 0)
21682 {
21683 right = left + width;
21684 if (left < leftmost)
21685 leftmost = left;
21686 if (right > rightmost)
21687 rightmost = right;
21688 }
21689 top = btm + descent + ascent;
21690 if (top > highest)
21691 highest = top;
21692 if (btm < lowest)
21693 lowest = btm;
21694
21695 if (cmp->lbearing > left + lbearing)
21696 cmp->lbearing = left + lbearing;
21697 if (cmp->rbearing < left + rbearing)
21698 cmp->rbearing = left + rbearing;
21699 }
21700 }
21701
21702 /* If there are glyphs whose x-offsets are negative,
21703 shift all glyphs to the right and make all x-offsets
21704 non-negative. */
21705 if (leftmost < 0)
21706 {
21707 for (i = 0; i < cmp->glyph_len; i++)
21708 cmp->offsets[i * 2] -= leftmost;
21709 rightmost -= leftmost;
21710 cmp->lbearing -= leftmost;
21711 cmp->rbearing -= leftmost;
21712 }
21713
21714 if (left_padded && cmp->lbearing < 0)
21715 {
21716 for (i = 0; i < cmp->glyph_len; i++)
21717 cmp->offsets[i * 2] -= cmp->lbearing;
21718 rightmost -= cmp->lbearing;
21719 cmp->rbearing -= cmp->lbearing;
21720 cmp->lbearing = 0;
21721 }
21722 if (right_padded && rightmost < cmp->rbearing)
21723 {
21724 rightmost = cmp->rbearing;
21725 }
21726
21727 cmp->pixel_width = rightmost;
21728 cmp->ascent = highest;
21729 cmp->descent = - lowest;
21730 if (cmp->ascent < font_ascent)
21731 cmp->ascent = font_ascent;
21732 if (cmp->descent < font_descent)
21733 cmp->descent = font_descent;
21734 }
21735
21736 if (it->glyph_row
21737 && (cmp->lbearing < 0
21738 || cmp->rbearing > cmp->pixel_width))
21739 it->glyph_row->contains_overlapping_glyphs_p = 1;
21740
21741 it->pixel_width = cmp->pixel_width;
21742 it->ascent = it->phys_ascent = cmp->ascent;
21743 it->descent = it->phys_descent = cmp->descent;
21744 if (face->box != FACE_NO_BOX)
21745 {
21746 int thick = face->box_line_width;
21747
21748 if (thick > 0)
21749 {
21750 it->ascent += thick;
21751 it->descent += thick;
21752 }
21753 else
21754 thick = - thick;
21755
21756 if (it->start_of_box_run_p)
21757 it->pixel_width += thick;
21758 if (it->end_of_box_run_p)
21759 it->pixel_width += thick;
21760 }
21761
21762 /* If face has an overline, add the height of the overline
21763 (1 pixel) and a 1 pixel margin to the character height. */
21764 if (face->overline_p)
21765 it->ascent += overline_margin;
21766
21767 take_vertical_position_into_account (it);
21768 if (it->ascent < 0)
21769 it->ascent = 0;
21770 if (it->descent < 0)
21771 it->descent = 0;
21772
21773 if (it->glyph_row)
21774 append_composite_glyph (it);
21775 }
21776 else if (it->what == IT_COMPOSITION)
21777 {
21778 /* A dynamic (automatic) composition. */
21779 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21780 Lisp_Object gstring;
21781 struct font_metrics metrics;
21782
21783 gstring = composition_gstring_from_id (it->cmp_it.id);
21784 it->pixel_width
21785 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21786 &metrics);
21787 if (it->glyph_row
21788 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21789 it->glyph_row->contains_overlapping_glyphs_p = 1;
21790 it->ascent = it->phys_ascent = metrics.ascent;
21791 it->descent = it->phys_descent = metrics.descent;
21792 if (face->box != FACE_NO_BOX)
21793 {
21794 int thick = face->box_line_width;
21795
21796 if (thick > 0)
21797 {
21798 it->ascent += thick;
21799 it->descent += thick;
21800 }
21801 else
21802 thick = - thick;
21803
21804 if (it->start_of_box_run_p)
21805 it->pixel_width += thick;
21806 if (it->end_of_box_run_p)
21807 it->pixel_width += thick;
21808 }
21809 /* If face has an overline, add the height of the overline
21810 (1 pixel) and a 1 pixel margin to the character height. */
21811 if (face->overline_p)
21812 it->ascent += overline_margin;
21813 take_vertical_position_into_account (it);
21814 if (it->ascent < 0)
21815 it->ascent = 0;
21816 if (it->descent < 0)
21817 it->descent = 0;
21818
21819 if (it->glyph_row)
21820 append_composite_glyph (it);
21821 }
21822 else if (it->what == IT_IMAGE)
21823 produce_image_glyph (it);
21824 else if (it->what == IT_STRETCH)
21825 produce_stretch_glyph (it);
21826
21827 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21828 because this isn't true for images with `:ascent 100'. */
21829 xassert (it->ascent >= 0 && it->descent >= 0);
21830 if (it->area == TEXT_AREA)
21831 it->current_x += it->pixel_width;
21832
21833 if (extra_line_spacing > 0)
21834 {
21835 it->descent += extra_line_spacing;
21836 if (extra_line_spacing > it->max_extra_line_spacing)
21837 it->max_extra_line_spacing = extra_line_spacing;
21838 }
21839
21840 it->max_ascent = max (it->max_ascent, it->ascent);
21841 it->max_descent = max (it->max_descent, it->descent);
21842 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21843 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21844 }
21845
21846 /* EXPORT for RIF:
21847 Output LEN glyphs starting at START at the nominal cursor position.
21848 Advance the nominal cursor over the text. The global variable
21849 updated_window contains the window being updated, updated_row is
21850 the glyph row being updated, and updated_area is the area of that
21851 row being updated. */
21852
21853 void
21854 x_write_glyphs (start, len)
21855 struct glyph *start;
21856 int len;
21857 {
21858 int x, hpos;
21859
21860 xassert (updated_window && updated_row);
21861 BLOCK_INPUT;
21862
21863 /* Write glyphs. */
21864
21865 hpos = start - updated_row->glyphs[updated_area];
21866 x = draw_glyphs (updated_window, output_cursor.x,
21867 updated_row, updated_area,
21868 hpos, hpos + len,
21869 DRAW_NORMAL_TEXT, 0);
21870
21871 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21872 if (updated_area == TEXT_AREA
21873 && updated_window->phys_cursor_on_p
21874 && updated_window->phys_cursor.vpos == output_cursor.vpos
21875 && updated_window->phys_cursor.hpos >= hpos
21876 && updated_window->phys_cursor.hpos < hpos + len)
21877 updated_window->phys_cursor_on_p = 0;
21878
21879 UNBLOCK_INPUT;
21880
21881 /* Advance the output cursor. */
21882 output_cursor.hpos += len;
21883 output_cursor.x = x;
21884 }
21885
21886
21887 /* EXPORT for RIF:
21888 Insert LEN glyphs from START at the nominal cursor position. */
21889
21890 void
21891 x_insert_glyphs (start, len)
21892 struct glyph *start;
21893 int len;
21894 {
21895 struct frame *f;
21896 struct window *w;
21897 int line_height, shift_by_width, shifted_region_width;
21898 struct glyph_row *row;
21899 struct glyph *glyph;
21900 int frame_x, frame_y;
21901 EMACS_INT hpos;
21902
21903 xassert (updated_window && updated_row);
21904 BLOCK_INPUT;
21905 w = updated_window;
21906 f = XFRAME (WINDOW_FRAME (w));
21907
21908 /* Get the height of the line we are in. */
21909 row = updated_row;
21910 line_height = row->height;
21911
21912 /* Get the width of the glyphs to insert. */
21913 shift_by_width = 0;
21914 for (glyph = start; glyph < start + len; ++glyph)
21915 shift_by_width += glyph->pixel_width;
21916
21917 /* Get the width of the region to shift right. */
21918 shifted_region_width = (window_box_width (w, updated_area)
21919 - output_cursor.x
21920 - shift_by_width);
21921
21922 /* Shift right. */
21923 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21924 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21925
21926 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21927 line_height, shift_by_width);
21928
21929 /* Write the glyphs. */
21930 hpos = start - row->glyphs[updated_area];
21931 draw_glyphs (w, output_cursor.x, row, updated_area,
21932 hpos, hpos + len,
21933 DRAW_NORMAL_TEXT, 0);
21934
21935 /* Advance the output cursor. */
21936 output_cursor.hpos += len;
21937 output_cursor.x += shift_by_width;
21938 UNBLOCK_INPUT;
21939 }
21940
21941
21942 /* EXPORT for RIF:
21943 Erase the current text line from the nominal cursor position
21944 (inclusive) to pixel column TO_X (exclusive). The idea is that
21945 everything from TO_X onward is already erased.
21946
21947 TO_X is a pixel position relative to updated_area of
21948 updated_window. TO_X == -1 means clear to the end of this area. */
21949
21950 void
21951 x_clear_end_of_line (to_x)
21952 int to_x;
21953 {
21954 struct frame *f;
21955 struct window *w = updated_window;
21956 int max_x, min_y, max_y;
21957 int from_x, from_y, to_y;
21958
21959 xassert (updated_window && updated_row);
21960 f = XFRAME (w->frame);
21961
21962 if (updated_row->full_width_p)
21963 max_x = WINDOW_TOTAL_WIDTH (w);
21964 else
21965 max_x = window_box_width (w, updated_area);
21966 max_y = window_text_bottom_y (w);
21967
21968 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21969 of window. For TO_X > 0, truncate to end of drawing area. */
21970 if (to_x == 0)
21971 return;
21972 else if (to_x < 0)
21973 to_x = max_x;
21974 else
21975 to_x = min (to_x, max_x);
21976
21977 to_y = min (max_y, output_cursor.y + updated_row->height);
21978
21979 /* Notice if the cursor will be cleared by this operation. */
21980 if (!updated_row->full_width_p)
21981 notice_overwritten_cursor (w, updated_area,
21982 output_cursor.x, -1,
21983 updated_row->y,
21984 MATRIX_ROW_BOTTOM_Y (updated_row));
21985
21986 from_x = output_cursor.x;
21987
21988 /* Translate to frame coordinates. */
21989 if (updated_row->full_width_p)
21990 {
21991 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21992 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21993 }
21994 else
21995 {
21996 int area_left = window_box_left (w, updated_area);
21997 from_x += area_left;
21998 to_x += area_left;
21999 }
22000
22001 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22002 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22003 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22004
22005 /* Prevent inadvertently clearing to end of the X window. */
22006 if (to_x > from_x && to_y > from_y)
22007 {
22008 BLOCK_INPUT;
22009 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22010 to_x - from_x, to_y - from_y);
22011 UNBLOCK_INPUT;
22012 }
22013 }
22014
22015 #endif /* HAVE_WINDOW_SYSTEM */
22016
22017
22018 \f
22019 /***********************************************************************
22020 Cursor types
22021 ***********************************************************************/
22022
22023 /* Value is the internal representation of the specified cursor type
22024 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22025 of the bar cursor. */
22026
22027 static enum text_cursor_kinds
22028 get_specified_cursor_type (arg, width)
22029 Lisp_Object arg;
22030 int *width;
22031 {
22032 enum text_cursor_kinds type;
22033
22034 if (NILP (arg))
22035 return NO_CURSOR;
22036
22037 if (EQ (arg, Qbox))
22038 return FILLED_BOX_CURSOR;
22039
22040 if (EQ (arg, Qhollow))
22041 return HOLLOW_BOX_CURSOR;
22042
22043 if (EQ (arg, Qbar))
22044 {
22045 *width = 2;
22046 return BAR_CURSOR;
22047 }
22048
22049 if (CONSP (arg)
22050 && EQ (XCAR (arg), Qbar)
22051 && INTEGERP (XCDR (arg))
22052 && XINT (XCDR (arg)) >= 0)
22053 {
22054 *width = XINT (XCDR (arg));
22055 return BAR_CURSOR;
22056 }
22057
22058 if (EQ (arg, Qhbar))
22059 {
22060 *width = 2;
22061 return HBAR_CURSOR;
22062 }
22063
22064 if (CONSP (arg)
22065 && EQ (XCAR (arg), Qhbar)
22066 && INTEGERP (XCDR (arg))
22067 && XINT (XCDR (arg)) >= 0)
22068 {
22069 *width = XINT (XCDR (arg));
22070 return HBAR_CURSOR;
22071 }
22072
22073 /* Treat anything unknown as "hollow box cursor".
22074 It was bad to signal an error; people have trouble fixing
22075 .Xdefaults with Emacs, when it has something bad in it. */
22076 type = HOLLOW_BOX_CURSOR;
22077
22078 return type;
22079 }
22080
22081 /* Set the default cursor types for specified frame. */
22082 void
22083 set_frame_cursor_types (f, arg)
22084 struct frame *f;
22085 Lisp_Object arg;
22086 {
22087 int width;
22088 Lisp_Object tem;
22089
22090 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22091 FRAME_CURSOR_WIDTH (f) = width;
22092
22093 /* By default, set up the blink-off state depending on the on-state. */
22094
22095 tem = Fassoc (arg, Vblink_cursor_alist);
22096 if (!NILP (tem))
22097 {
22098 FRAME_BLINK_OFF_CURSOR (f)
22099 = get_specified_cursor_type (XCDR (tem), &width);
22100 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22101 }
22102 else
22103 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22104 }
22105
22106
22107 /* Return the cursor we want to be displayed in window W. Return
22108 width of bar/hbar cursor through WIDTH arg. Return with
22109 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22110 (i.e. if the `system caret' should track this cursor).
22111
22112 In a mini-buffer window, we want the cursor only to appear if we
22113 are reading input from this window. For the selected window, we
22114 want the cursor type given by the frame parameter or buffer local
22115 setting of cursor-type. If explicitly marked off, draw no cursor.
22116 In all other cases, we want a hollow box cursor. */
22117
22118 static enum text_cursor_kinds
22119 get_window_cursor_type (w, glyph, width, active_cursor)
22120 struct window *w;
22121 struct glyph *glyph;
22122 int *width;
22123 int *active_cursor;
22124 {
22125 struct frame *f = XFRAME (w->frame);
22126 struct buffer *b = XBUFFER (w->buffer);
22127 int cursor_type = DEFAULT_CURSOR;
22128 Lisp_Object alt_cursor;
22129 int non_selected = 0;
22130
22131 *active_cursor = 1;
22132
22133 /* Echo area */
22134 if (cursor_in_echo_area
22135 && FRAME_HAS_MINIBUF_P (f)
22136 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22137 {
22138 if (w == XWINDOW (echo_area_window))
22139 {
22140 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22141 {
22142 *width = FRAME_CURSOR_WIDTH (f);
22143 return FRAME_DESIRED_CURSOR (f);
22144 }
22145 else
22146 return get_specified_cursor_type (b->cursor_type, width);
22147 }
22148
22149 *active_cursor = 0;
22150 non_selected = 1;
22151 }
22152
22153 /* Detect a nonselected window or nonselected frame. */
22154 else if (w != XWINDOW (f->selected_window)
22155 #ifdef HAVE_WINDOW_SYSTEM
22156 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22157 #endif
22158 )
22159 {
22160 *active_cursor = 0;
22161
22162 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22163 return NO_CURSOR;
22164
22165 non_selected = 1;
22166 }
22167
22168 /* Never display a cursor in a window in which cursor-type is nil. */
22169 if (NILP (b->cursor_type))
22170 return NO_CURSOR;
22171
22172 /* Get the normal cursor type for this window. */
22173 if (EQ (b->cursor_type, Qt))
22174 {
22175 cursor_type = FRAME_DESIRED_CURSOR (f);
22176 *width = FRAME_CURSOR_WIDTH (f);
22177 }
22178 else
22179 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22180
22181 /* Use cursor-in-non-selected-windows instead
22182 for non-selected window or frame. */
22183 if (non_selected)
22184 {
22185 alt_cursor = b->cursor_in_non_selected_windows;
22186 if (!EQ (Qt, alt_cursor))
22187 return get_specified_cursor_type (alt_cursor, width);
22188 /* t means modify the normal cursor type. */
22189 if (cursor_type == FILLED_BOX_CURSOR)
22190 cursor_type = HOLLOW_BOX_CURSOR;
22191 else if (cursor_type == BAR_CURSOR && *width > 1)
22192 --*width;
22193 return cursor_type;
22194 }
22195
22196 /* Use normal cursor if not blinked off. */
22197 if (!w->cursor_off_p)
22198 {
22199 #ifdef HAVE_WINDOW_SYSTEM
22200 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22201 {
22202 if (cursor_type == FILLED_BOX_CURSOR)
22203 {
22204 /* Using a block cursor on large images can be very annoying.
22205 So use a hollow cursor for "large" images.
22206 If image is not transparent (no mask), also use hollow cursor. */
22207 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22208 if (img != NULL && IMAGEP (img->spec))
22209 {
22210 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22211 where N = size of default frame font size.
22212 This should cover most of the "tiny" icons people may use. */
22213 if (!img->mask
22214 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22215 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22216 cursor_type = HOLLOW_BOX_CURSOR;
22217 }
22218 }
22219 else if (cursor_type != NO_CURSOR)
22220 {
22221 /* Display current only supports BOX and HOLLOW cursors for images.
22222 So for now, unconditionally use a HOLLOW cursor when cursor is
22223 not a solid box cursor. */
22224 cursor_type = HOLLOW_BOX_CURSOR;
22225 }
22226 }
22227 #endif
22228 return cursor_type;
22229 }
22230
22231 /* Cursor is blinked off, so determine how to "toggle" it. */
22232
22233 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22234 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22235 return get_specified_cursor_type (XCDR (alt_cursor), width);
22236
22237 /* Then see if frame has specified a specific blink off cursor type. */
22238 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22239 {
22240 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22241 return FRAME_BLINK_OFF_CURSOR (f);
22242 }
22243
22244 #if 0
22245 /* Some people liked having a permanently visible blinking cursor,
22246 while others had very strong opinions against it. So it was
22247 decided to remove it. KFS 2003-09-03 */
22248
22249 /* Finally perform built-in cursor blinking:
22250 filled box <-> hollow box
22251 wide [h]bar <-> narrow [h]bar
22252 narrow [h]bar <-> no cursor
22253 other type <-> no cursor */
22254
22255 if (cursor_type == FILLED_BOX_CURSOR)
22256 return HOLLOW_BOX_CURSOR;
22257
22258 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22259 {
22260 *width = 1;
22261 return cursor_type;
22262 }
22263 #endif
22264
22265 return NO_CURSOR;
22266 }
22267
22268
22269 #ifdef HAVE_WINDOW_SYSTEM
22270
22271 /* Notice when the text cursor of window W has been completely
22272 overwritten by a drawing operation that outputs glyphs in AREA
22273 starting at X0 and ending at X1 in the line starting at Y0 and
22274 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22275 the rest of the line after X0 has been written. Y coordinates
22276 are window-relative. */
22277
22278 static void
22279 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22280 struct window *w;
22281 enum glyph_row_area area;
22282 int x0, y0, x1, y1;
22283 {
22284 int cx0, cx1, cy0, cy1;
22285 struct glyph_row *row;
22286
22287 if (!w->phys_cursor_on_p)
22288 return;
22289 if (area != TEXT_AREA)
22290 return;
22291
22292 if (w->phys_cursor.vpos < 0
22293 || w->phys_cursor.vpos >= w->current_matrix->nrows
22294 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22295 !(row->enabled_p && row->displays_text_p)))
22296 return;
22297
22298 if (row->cursor_in_fringe_p)
22299 {
22300 row->cursor_in_fringe_p = 0;
22301 draw_fringe_bitmap (w, row, 0);
22302 w->phys_cursor_on_p = 0;
22303 return;
22304 }
22305
22306 cx0 = w->phys_cursor.x;
22307 cx1 = cx0 + w->phys_cursor_width;
22308 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22309 return;
22310
22311 /* The cursor image will be completely removed from the
22312 screen if the output area intersects the cursor area in
22313 y-direction. When we draw in [y0 y1[, and some part of
22314 the cursor is at y < y0, that part must have been drawn
22315 before. When scrolling, the cursor is erased before
22316 actually scrolling, so we don't come here. When not
22317 scrolling, the rows above the old cursor row must have
22318 changed, and in this case these rows must have written
22319 over the cursor image.
22320
22321 Likewise if part of the cursor is below y1, with the
22322 exception of the cursor being in the first blank row at
22323 the buffer and window end because update_text_area
22324 doesn't draw that row. (Except when it does, but
22325 that's handled in update_text_area.) */
22326
22327 cy0 = w->phys_cursor.y;
22328 cy1 = cy0 + w->phys_cursor_height;
22329 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22330 return;
22331
22332 w->phys_cursor_on_p = 0;
22333 }
22334
22335 #endif /* HAVE_WINDOW_SYSTEM */
22336
22337 \f
22338 /************************************************************************
22339 Mouse Face
22340 ************************************************************************/
22341
22342 #ifdef HAVE_WINDOW_SYSTEM
22343
22344 /* EXPORT for RIF:
22345 Fix the display of area AREA of overlapping row ROW in window W
22346 with respect to the overlapping part OVERLAPS. */
22347
22348 void
22349 x_fix_overlapping_area (w, row, area, overlaps)
22350 struct window *w;
22351 struct glyph_row *row;
22352 enum glyph_row_area area;
22353 int overlaps;
22354 {
22355 int i, x;
22356
22357 BLOCK_INPUT;
22358
22359 x = 0;
22360 for (i = 0; i < row->used[area];)
22361 {
22362 if (row->glyphs[area][i].overlaps_vertically_p)
22363 {
22364 int start = i, start_x = x;
22365
22366 do
22367 {
22368 x += row->glyphs[area][i].pixel_width;
22369 ++i;
22370 }
22371 while (i < row->used[area]
22372 && row->glyphs[area][i].overlaps_vertically_p);
22373
22374 draw_glyphs (w, start_x, row, area,
22375 start, i,
22376 DRAW_NORMAL_TEXT, overlaps);
22377 }
22378 else
22379 {
22380 x += row->glyphs[area][i].pixel_width;
22381 ++i;
22382 }
22383 }
22384
22385 UNBLOCK_INPUT;
22386 }
22387
22388
22389 /* EXPORT:
22390 Draw the cursor glyph of window W in glyph row ROW. See the
22391 comment of draw_glyphs for the meaning of HL. */
22392
22393 void
22394 draw_phys_cursor_glyph (w, row, hl)
22395 struct window *w;
22396 struct glyph_row *row;
22397 enum draw_glyphs_face hl;
22398 {
22399 /* If cursor hpos is out of bounds, don't draw garbage. This can
22400 happen in mini-buffer windows when switching between echo area
22401 glyphs and mini-buffer. */
22402 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22403 {
22404 int on_p = w->phys_cursor_on_p;
22405 int x1;
22406 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22407 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22408 hl, 0);
22409 w->phys_cursor_on_p = on_p;
22410
22411 if (hl == DRAW_CURSOR)
22412 w->phys_cursor_width = x1 - w->phys_cursor.x;
22413 /* When we erase the cursor, and ROW is overlapped by other
22414 rows, make sure that these overlapping parts of other rows
22415 are redrawn. */
22416 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22417 {
22418 w->phys_cursor_width = x1 - w->phys_cursor.x;
22419
22420 if (row > w->current_matrix->rows
22421 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22422 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22423 OVERLAPS_ERASED_CURSOR);
22424
22425 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22426 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22427 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22428 OVERLAPS_ERASED_CURSOR);
22429 }
22430 }
22431 }
22432
22433
22434 /* EXPORT:
22435 Erase the image of a cursor of window W from the screen. */
22436
22437 void
22438 erase_phys_cursor (w)
22439 struct window *w;
22440 {
22441 struct frame *f = XFRAME (w->frame);
22442 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22443 int hpos = w->phys_cursor.hpos;
22444 int vpos = w->phys_cursor.vpos;
22445 int mouse_face_here_p = 0;
22446 struct glyph_matrix *active_glyphs = w->current_matrix;
22447 struct glyph_row *cursor_row;
22448 struct glyph *cursor_glyph;
22449 enum draw_glyphs_face hl;
22450
22451 /* No cursor displayed or row invalidated => nothing to do on the
22452 screen. */
22453 if (w->phys_cursor_type == NO_CURSOR)
22454 goto mark_cursor_off;
22455
22456 /* VPOS >= active_glyphs->nrows means that window has been resized.
22457 Don't bother to erase the cursor. */
22458 if (vpos >= active_glyphs->nrows)
22459 goto mark_cursor_off;
22460
22461 /* If row containing cursor is marked invalid, there is nothing we
22462 can do. */
22463 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22464 if (!cursor_row->enabled_p)
22465 goto mark_cursor_off;
22466
22467 /* If line spacing is > 0, old cursor may only be partially visible in
22468 window after split-window. So adjust visible height. */
22469 cursor_row->visible_height = min (cursor_row->visible_height,
22470 window_text_bottom_y (w) - cursor_row->y);
22471
22472 /* If row is completely invisible, don't attempt to delete a cursor which
22473 isn't there. This can happen if cursor is at top of a window, and
22474 we switch to a buffer with a header line in that window. */
22475 if (cursor_row->visible_height <= 0)
22476 goto mark_cursor_off;
22477
22478 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22479 if (cursor_row->cursor_in_fringe_p)
22480 {
22481 cursor_row->cursor_in_fringe_p = 0;
22482 draw_fringe_bitmap (w, cursor_row, 0);
22483 goto mark_cursor_off;
22484 }
22485
22486 /* This can happen when the new row is shorter than the old one.
22487 In this case, either draw_glyphs or clear_end_of_line
22488 should have cleared the cursor. Note that we wouldn't be
22489 able to erase the cursor in this case because we don't have a
22490 cursor glyph at hand. */
22491 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22492 goto mark_cursor_off;
22493
22494 /* If the cursor is in the mouse face area, redisplay that when
22495 we clear the cursor. */
22496 if (! NILP (dpyinfo->mouse_face_window)
22497 && w == XWINDOW (dpyinfo->mouse_face_window)
22498 && (vpos > dpyinfo->mouse_face_beg_row
22499 || (vpos == dpyinfo->mouse_face_beg_row
22500 && hpos >= dpyinfo->mouse_face_beg_col))
22501 && (vpos < dpyinfo->mouse_face_end_row
22502 || (vpos == dpyinfo->mouse_face_end_row
22503 && hpos < dpyinfo->mouse_face_end_col))
22504 /* Don't redraw the cursor's spot in mouse face if it is at the
22505 end of a line (on a newline). The cursor appears there, but
22506 mouse highlighting does not. */
22507 && cursor_row->used[TEXT_AREA] > hpos)
22508 mouse_face_here_p = 1;
22509
22510 /* Maybe clear the display under the cursor. */
22511 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22512 {
22513 int x, y, left_x;
22514 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22515 int width;
22516
22517 cursor_glyph = get_phys_cursor_glyph (w);
22518 if (cursor_glyph == NULL)
22519 goto mark_cursor_off;
22520
22521 width = cursor_glyph->pixel_width;
22522 left_x = window_box_left_offset (w, TEXT_AREA);
22523 x = w->phys_cursor.x;
22524 if (x < left_x)
22525 width -= left_x - x;
22526 width = min (width, window_box_width (w, TEXT_AREA) - x);
22527 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22528 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22529
22530 if (width > 0)
22531 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22532 }
22533
22534 /* Erase the cursor by redrawing the character underneath it. */
22535 if (mouse_face_here_p)
22536 hl = DRAW_MOUSE_FACE;
22537 else
22538 hl = DRAW_NORMAL_TEXT;
22539 draw_phys_cursor_glyph (w, cursor_row, hl);
22540
22541 mark_cursor_off:
22542 w->phys_cursor_on_p = 0;
22543 w->phys_cursor_type = NO_CURSOR;
22544 }
22545
22546
22547 /* EXPORT:
22548 Display or clear cursor of window W. If ON is zero, clear the
22549 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22550 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22551
22552 void
22553 display_and_set_cursor (w, on, hpos, vpos, x, y)
22554 struct window *w;
22555 int on, hpos, vpos, x, y;
22556 {
22557 struct frame *f = XFRAME (w->frame);
22558 int new_cursor_type;
22559 int new_cursor_width;
22560 int active_cursor;
22561 struct glyph_row *glyph_row;
22562 struct glyph *glyph;
22563
22564 /* This is pointless on invisible frames, and dangerous on garbaged
22565 windows and frames; in the latter case, the frame or window may
22566 be in the midst of changing its size, and x and y may be off the
22567 window. */
22568 if (! FRAME_VISIBLE_P (f)
22569 || FRAME_GARBAGED_P (f)
22570 || vpos >= w->current_matrix->nrows
22571 || hpos >= w->current_matrix->matrix_w)
22572 return;
22573
22574 /* If cursor is off and we want it off, return quickly. */
22575 if (!on && !w->phys_cursor_on_p)
22576 return;
22577
22578 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22579 /* If cursor row is not enabled, we don't really know where to
22580 display the cursor. */
22581 if (!glyph_row->enabled_p)
22582 {
22583 w->phys_cursor_on_p = 0;
22584 return;
22585 }
22586
22587 glyph = NULL;
22588 if (!glyph_row->exact_window_width_line_p
22589 || hpos < glyph_row->used[TEXT_AREA])
22590 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22591
22592 xassert (interrupt_input_blocked);
22593
22594 /* Set new_cursor_type to the cursor we want to be displayed. */
22595 new_cursor_type = get_window_cursor_type (w, glyph,
22596 &new_cursor_width, &active_cursor);
22597
22598 /* If cursor is currently being shown and we don't want it to be or
22599 it is in the wrong place, or the cursor type is not what we want,
22600 erase it. */
22601 if (w->phys_cursor_on_p
22602 && (!on
22603 || w->phys_cursor.x != x
22604 || w->phys_cursor.y != y
22605 || new_cursor_type != w->phys_cursor_type
22606 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22607 && new_cursor_width != w->phys_cursor_width)))
22608 erase_phys_cursor (w);
22609
22610 /* Don't check phys_cursor_on_p here because that flag is only set
22611 to zero in some cases where we know that the cursor has been
22612 completely erased, to avoid the extra work of erasing the cursor
22613 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22614 still not be visible, or it has only been partly erased. */
22615 if (on)
22616 {
22617 w->phys_cursor_ascent = glyph_row->ascent;
22618 w->phys_cursor_height = glyph_row->height;
22619
22620 /* Set phys_cursor_.* before x_draw_.* is called because some
22621 of them may need the information. */
22622 w->phys_cursor.x = x;
22623 w->phys_cursor.y = glyph_row->y;
22624 w->phys_cursor.hpos = hpos;
22625 w->phys_cursor.vpos = vpos;
22626 }
22627
22628 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22629 new_cursor_type, new_cursor_width,
22630 on, active_cursor);
22631 }
22632
22633
22634 /* Switch the display of W's cursor on or off, according to the value
22635 of ON. */
22636
22637 #ifndef HAVE_NS
22638 static
22639 #endif
22640 void
22641 update_window_cursor (w, on)
22642 struct window *w;
22643 int on;
22644 {
22645 /* Don't update cursor in windows whose frame is in the process
22646 of being deleted. */
22647 if (w->current_matrix)
22648 {
22649 BLOCK_INPUT;
22650 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22651 w->phys_cursor.x, w->phys_cursor.y);
22652 UNBLOCK_INPUT;
22653 }
22654 }
22655
22656
22657 /* Call update_window_cursor with parameter ON_P on all leaf windows
22658 in the window tree rooted at W. */
22659
22660 static void
22661 update_cursor_in_window_tree (w, on_p)
22662 struct window *w;
22663 int on_p;
22664 {
22665 while (w)
22666 {
22667 if (!NILP (w->hchild))
22668 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22669 else if (!NILP (w->vchild))
22670 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22671 else
22672 update_window_cursor (w, on_p);
22673
22674 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22675 }
22676 }
22677
22678
22679 /* EXPORT:
22680 Display the cursor on window W, or clear it, according to ON_P.
22681 Don't change the cursor's position. */
22682
22683 void
22684 x_update_cursor (f, on_p)
22685 struct frame *f;
22686 int on_p;
22687 {
22688 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22689 }
22690
22691
22692 /* EXPORT:
22693 Clear the cursor of window W to background color, and mark the
22694 cursor as not shown. This is used when the text where the cursor
22695 is is about to be rewritten. */
22696
22697 void
22698 x_clear_cursor (w)
22699 struct window *w;
22700 {
22701 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22702 update_window_cursor (w, 0);
22703 }
22704
22705
22706 /* EXPORT:
22707 Display the active region described by mouse_face_* according to DRAW. */
22708
22709 void
22710 show_mouse_face (dpyinfo, draw)
22711 Display_Info *dpyinfo;
22712 enum draw_glyphs_face draw;
22713 {
22714 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22715 struct frame *f = XFRAME (WINDOW_FRAME (w));
22716
22717 if (/* If window is in the process of being destroyed, don't bother
22718 to do anything. */
22719 w->current_matrix != NULL
22720 /* Don't update mouse highlight if hidden */
22721 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22722 /* Recognize when we are called to operate on rows that don't exist
22723 anymore. This can happen when a window is split. */
22724 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22725 {
22726 int phys_cursor_on_p = w->phys_cursor_on_p;
22727 struct glyph_row *row, *first, *last;
22728
22729 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22730 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22731
22732 for (row = first; row <= last && row->enabled_p; ++row)
22733 {
22734 int start_hpos, end_hpos, start_x;
22735
22736 /* For all but the first row, the highlight starts at column 0. */
22737 if (row == first)
22738 {
22739 start_hpos = dpyinfo->mouse_face_beg_col;
22740 start_x = dpyinfo->mouse_face_beg_x;
22741 }
22742 else
22743 {
22744 start_hpos = 0;
22745 start_x = 0;
22746 }
22747
22748 if (row == last)
22749 end_hpos = dpyinfo->mouse_face_end_col;
22750 else
22751 {
22752 end_hpos = row->used[TEXT_AREA];
22753 if (draw == DRAW_NORMAL_TEXT)
22754 row->fill_line_p = 1; /* Clear to end of line */
22755 }
22756
22757 if (end_hpos > start_hpos)
22758 {
22759 draw_glyphs (w, start_x, row, TEXT_AREA,
22760 start_hpos, end_hpos,
22761 draw, 0);
22762
22763 row->mouse_face_p
22764 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22765 }
22766 }
22767
22768 /* When we've written over the cursor, arrange for it to
22769 be displayed again. */
22770 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22771 {
22772 BLOCK_INPUT;
22773 display_and_set_cursor (w, 1,
22774 w->phys_cursor.hpos, w->phys_cursor.vpos,
22775 w->phys_cursor.x, w->phys_cursor.y);
22776 UNBLOCK_INPUT;
22777 }
22778 }
22779
22780 /* Change the mouse cursor. */
22781 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22782 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22783 else if (draw == DRAW_MOUSE_FACE)
22784 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22785 else
22786 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22787 }
22788
22789 /* EXPORT:
22790 Clear out the mouse-highlighted active region.
22791 Redraw it un-highlighted first. Value is non-zero if mouse
22792 face was actually drawn unhighlighted. */
22793
22794 int
22795 clear_mouse_face (dpyinfo)
22796 Display_Info *dpyinfo;
22797 {
22798 int cleared = 0;
22799
22800 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22801 {
22802 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22803 cleared = 1;
22804 }
22805
22806 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22807 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22808 dpyinfo->mouse_face_window = Qnil;
22809 dpyinfo->mouse_face_overlay = Qnil;
22810 return cleared;
22811 }
22812
22813
22814 /* EXPORT:
22815 Non-zero if physical cursor of window W is within mouse face. */
22816
22817 int
22818 cursor_in_mouse_face_p (w)
22819 struct window *w;
22820 {
22821 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22822 int in_mouse_face = 0;
22823
22824 if (WINDOWP (dpyinfo->mouse_face_window)
22825 && XWINDOW (dpyinfo->mouse_face_window) == w)
22826 {
22827 int hpos = w->phys_cursor.hpos;
22828 int vpos = w->phys_cursor.vpos;
22829
22830 if (vpos >= dpyinfo->mouse_face_beg_row
22831 && vpos <= dpyinfo->mouse_face_end_row
22832 && (vpos > dpyinfo->mouse_face_beg_row
22833 || hpos >= dpyinfo->mouse_face_beg_col)
22834 && (vpos < dpyinfo->mouse_face_end_row
22835 || hpos < dpyinfo->mouse_face_end_col
22836 || dpyinfo->mouse_face_past_end))
22837 in_mouse_face = 1;
22838 }
22839
22840 return in_mouse_face;
22841 }
22842
22843
22844
22845 \f
22846 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22847 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22848 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22849 for the overlay or run of text properties specifying the mouse
22850 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22851 before-string and after-string that must also be highlighted.
22852 DISPLAY_STRING, if non-nil, is a display string that may cover some
22853 or all of the highlighted text. */
22854
22855 static void
22856 mouse_face_from_buffer_pos (Lisp_Object window,
22857 Display_Info *dpyinfo,
22858 EMACS_INT mouse_charpos,
22859 EMACS_INT start_charpos,
22860 EMACS_INT end_charpos,
22861 Lisp_Object before_string,
22862 Lisp_Object after_string,
22863 Lisp_Object display_string)
22864 {
22865 struct window *w = XWINDOW (window);
22866 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22867 struct glyph_row *row;
22868 struct glyph *glyph, *end;
22869 EMACS_INT ignore;
22870 int x;
22871
22872 xassert (NILP (display_string) || STRINGP (display_string));
22873 xassert (NILP (before_string) || STRINGP (before_string));
22874 xassert (NILP (after_string) || STRINGP (after_string));
22875
22876 /* Find the first highlighted glyph. */
22877 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22878 {
22879 dpyinfo->mouse_face_beg_col = 0;
22880 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22881 dpyinfo->mouse_face_beg_x = first->x;
22882 dpyinfo->mouse_face_beg_y = first->y;
22883 }
22884 else
22885 {
22886 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22887 if (row == NULL)
22888 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22889
22890 /* If the before-string or display-string contains newlines,
22891 row_containing_pos skips to its last row. Move back. */
22892 if (!NILP (before_string) || !NILP (display_string))
22893 {
22894 struct glyph_row *prev;
22895 while ((prev = row - 1, prev >= first)
22896 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22897 && prev->used[TEXT_AREA] > 0)
22898 {
22899 struct glyph *beg = prev->glyphs[TEXT_AREA];
22900 glyph = beg + prev->used[TEXT_AREA];
22901 while (--glyph >= beg && INTEGERP (glyph->object));
22902 if (glyph < beg
22903 || !(EQ (glyph->object, before_string)
22904 || EQ (glyph->object, display_string)))
22905 break;
22906 row = prev;
22907 }
22908 }
22909
22910 glyph = row->glyphs[TEXT_AREA];
22911 end = glyph + row->used[TEXT_AREA];
22912 x = row->x;
22913 dpyinfo->mouse_face_beg_y = row->y;
22914 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22915
22916 /* Skip truncation glyphs at the start of the glyph row. */
22917 if (row->displays_text_p)
22918 for (; glyph < end
22919 && INTEGERP (glyph->object)
22920 && glyph->charpos < 0;
22921 ++glyph)
22922 x += glyph->pixel_width;
22923
22924 /* Scan the glyph row, stopping before BEFORE_STRING or
22925 DISPLAY_STRING or START_CHARPOS. */
22926 for (; glyph < end
22927 && !INTEGERP (glyph->object)
22928 && !EQ (glyph->object, before_string)
22929 && !EQ (glyph->object, display_string)
22930 && !(BUFFERP (glyph->object)
22931 && glyph->charpos >= start_charpos);
22932 ++glyph)
22933 x += glyph->pixel_width;
22934
22935 dpyinfo->mouse_face_beg_x = x;
22936 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22937 }
22938
22939 /* Find the last highlighted glyph. */
22940 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22941 if (row == NULL)
22942 {
22943 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22944 dpyinfo->mouse_face_past_end = 1;
22945 }
22946 else if (!NILP (after_string))
22947 {
22948 /* If the after-string has newlines, advance to its last row. */
22949 struct glyph_row *next;
22950 struct glyph_row *last
22951 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22952
22953 for (next = row + 1;
22954 next <= last
22955 && next->used[TEXT_AREA] > 0
22956 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22957 ++next)
22958 row = next;
22959 }
22960
22961 glyph = row->glyphs[TEXT_AREA];
22962 end = glyph + row->used[TEXT_AREA];
22963 x = row->x;
22964 dpyinfo->mouse_face_end_y = row->y;
22965 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22966
22967 /* Skip truncation glyphs at the start of the row. */
22968 if (row->displays_text_p)
22969 for (; glyph < end
22970 && INTEGERP (glyph->object)
22971 && glyph->charpos < 0;
22972 ++glyph)
22973 x += glyph->pixel_width;
22974
22975 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22976 AFTER_STRING. */
22977 for (; glyph < end
22978 && !INTEGERP (glyph->object)
22979 && !EQ (glyph->object, after_string)
22980 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22981 ++glyph)
22982 x += glyph->pixel_width;
22983
22984 /* If we found AFTER_STRING, consume it and stop. */
22985 if (EQ (glyph->object, after_string))
22986 {
22987 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22988 x += glyph->pixel_width;
22989 }
22990 else
22991 {
22992 /* If there's no after-string, we must check if we overshot,
22993 which might be the case if we stopped after a string glyph.
22994 That glyph may belong to a before-string or display-string
22995 associated with the end position, which must not be
22996 highlighted. */
22997 Lisp_Object prev_object;
22998 int pos;
22999
23000 while (glyph > row->glyphs[TEXT_AREA])
23001 {
23002 prev_object = (glyph - 1)->object;
23003 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23004 break;
23005
23006 pos = string_buffer_position (w, prev_object, end_charpos);
23007 if (pos && pos < end_charpos)
23008 break;
23009
23010 for (; glyph > row->glyphs[TEXT_AREA]
23011 && EQ ((glyph - 1)->object, prev_object);
23012 --glyph)
23013 x -= (glyph - 1)->pixel_width;
23014 }
23015 }
23016
23017 dpyinfo->mouse_face_end_x = x;
23018 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23019 dpyinfo->mouse_face_window = window;
23020 dpyinfo->mouse_face_face_id
23021 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23022 mouse_charpos + 1,
23023 !dpyinfo->mouse_face_hidden, -1);
23024 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23025 }
23026
23027
23028 /* Find the position of the glyph for position POS in OBJECT in
23029 window W's current matrix, and return in *X, *Y the pixel
23030 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23031
23032 RIGHT_P non-zero means return the position of the right edge of the
23033 glyph, RIGHT_P zero means return the left edge position.
23034
23035 If no glyph for POS exists in the matrix, return the position of
23036 the glyph with the next smaller position that is in the matrix, if
23037 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23038 exists in the matrix, return the position of the glyph with the
23039 next larger position in OBJECT.
23040
23041 Value is non-zero if a glyph was found. */
23042
23043 static int
23044 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23045 struct window *w;
23046 EMACS_INT pos;
23047 Lisp_Object object;
23048 int *hpos, *vpos, *x, *y;
23049 int right_p;
23050 {
23051 int yb = window_text_bottom_y (w);
23052 struct glyph_row *r;
23053 struct glyph *best_glyph = NULL;
23054 struct glyph_row *best_row = NULL;
23055 int best_x = 0;
23056
23057 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23058 r->enabled_p && r->y < yb;
23059 ++r)
23060 {
23061 struct glyph *g = r->glyphs[TEXT_AREA];
23062 struct glyph *e = g + r->used[TEXT_AREA];
23063 int gx;
23064
23065 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23066 if (EQ (g->object, object))
23067 {
23068 if (g->charpos == pos)
23069 {
23070 best_glyph = g;
23071 best_x = gx;
23072 best_row = r;
23073 goto found;
23074 }
23075 else if (best_glyph == NULL
23076 || ((eabs (g->charpos - pos)
23077 < eabs (best_glyph->charpos - pos))
23078 && (right_p
23079 ? g->charpos < pos
23080 : g->charpos > pos)))
23081 {
23082 best_glyph = g;
23083 best_x = gx;
23084 best_row = r;
23085 }
23086 }
23087 }
23088
23089 found:
23090
23091 if (best_glyph)
23092 {
23093 *x = best_x;
23094 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23095
23096 if (right_p)
23097 {
23098 *x += best_glyph->pixel_width;
23099 ++*hpos;
23100 }
23101
23102 *y = best_row->y;
23103 *vpos = best_row - w->current_matrix->rows;
23104 }
23105
23106 return best_glyph != NULL;
23107 }
23108
23109
23110 /* See if position X, Y is within a hot-spot of an image. */
23111
23112 static int
23113 on_hot_spot_p (hot_spot, x, y)
23114 Lisp_Object hot_spot;
23115 int x, y;
23116 {
23117 if (!CONSP (hot_spot))
23118 return 0;
23119
23120 if (EQ (XCAR (hot_spot), Qrect))
23121 {
23122 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23123 Lisp_Object rect = XCDR (hot_spot);
23124 Lisp_Object tem;
23125 if (!CONSP (rect))
23126 return 0;
23127 if (!CONSP (XCAR (rect)))
23128 return 0;
23129 if (!CONSP (XCDR (rect)))
23130 return 0;
23131 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23132 return 0;
23133 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23134 return 0;
23135 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23136 return 0;
23137 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23138 return 0;
23139 return 1;
23140 }
23141 else if (EQ (XCAR (hot_spot), Qcircle))
23142 {
23143 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23144 Lisp_Object circ = XCDR (hot_spot);
23145 Lisp_Object lr, lx0, ly0;
23146 if (CONSP (circ)
23147 && CONSP (XCAR (circ))
23148 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23149 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23150 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23151 {
23152 double r = XFLOATINT (lr);
23153 double dx = XINT (lx0) - x;
23154 double dy = XINT (ly0) - y;
23155 return (dx * dx + dy * dy <= r * r);
23156 }
23157 }
23158 else if (EQ (XCAR (hot_spot), Qpoly))
23159 {
23160 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23161 if (VECTORP (XCDR (hot_spot)))
23162 {
23163 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23164 Lisp_Object *poly = v->contents;
23165 int n = v->size;
23166 int i;
23167 int inside = 0;
23168 Lisp_Object lx, ly;
23169 int x0, y0;
23170
23171 /* Need an even number of coordinates, and at least 3 edges. */
23172 if (n < 6 || n & 1)
23173 return 0;
23174
23175 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23176 If count is odd, we are inside polygon. Pixels on edges
23177 may or may not be included depending on actual geometry of the
23178 polygon. */
23179 if ((lx = poly[n-2], !INTEGERP (lx))
23180 || (ly = poly[n-1], !INTEGERP (lx)))
23181 return 0;
23182 x0 = XINT (lx), y0 = XINT (ly);
23183 for (i = 0; i < n; i += 2)
23184 {
23185 int x1 = x0, y1 = y0;
23186 if ((lx = poly[i], !INTEGERP (lx))
23187 || (ly = poly[i+1], !INTEGERP (ly)))
23188 return 0;
23189 x0 = XINT (lx), y0 = XINT (ly);
23190
23191 /* Does this segment cross the X line? */
23192 if (x0 >= x)
23193 {
23194 if (x1 >= x)
23195 continue;
23196 }
23197 else if (x1 < x)
23198 continue;
23199 if (y > y0 && y > y1)
23200 continue;
23201 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23202 inside = !inside;
23203 }
23204 return inside;
23205 }
23206 }
23207 return 0;
23208 }
23209
23210 Lisp_Object
23211 find_hot_spot (map, x, y)
23212 Lisp_Object map;
23213 int x, y;
23214 {
23215 while (CONSP (map))
23216 {
23217 if (CONSP (XCAR (map))
23218 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23219 return XCAR (map);
23220 map = XCDR (map);
23221 }
23222
23223 return Qnil;
23224 }
23225
23226 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23227 3, 3, 0,
23228 doc: /* Lookup in image map MAP coordinates X and Y.
23229 An image map is an alist where each element has the format (AREA ID PLIST).
23230 An AREA is specified as either a rectangle, a circle, or a polygon:
23231 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23232 pixel coordinates of the upper left and bottom right corners.
23233 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23234 and the radius of the circle; r may be a float or integer.
23235 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23236 vector describes one corner in the polygon.
23237 Returns the alist element for the first matching AREA in MAP. */)
23238 (map, x, y)
23239 Lisp_Object map;
23240 Lisp_Object x, y;
23241 {
23242 if (NILP (map))
23243 return Qnil;
23244
23245 CHECK_NUMBER (x);
23246 CHECK_NUMBER (y);
23247
23248 return find_hot_spot (map, XINT (x), XINT (y));
23249 }
23250
23251
23252 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23253 static void
23254 define_frame_cursor1 (f, cursor, pointer)
23255 struct frame *f;
23256 Cursor cursor;
23257 Lisp_Object pointer;
23258 {
23259 /* Do not change cursor shape while dragging mouse. */
23260 if (!NILP (do_mouse_tracking))
23261 return;
23262
23263 if (!NILP (pointer))
23264 {
23265 if (EQ (pointer, Qarrow))
23266 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23267 else if (EQ (pointer, Qhand))
23268 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23269 else if (EQ (pointer, Qtext))
23270 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23271 else if (EQ (pointer, intern ("hdrag")))
23272 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23273 #ifdef HAVE_X_WINDOWS
23274 else if (EQ (pointer, intern ("vdrag")))
23275 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23276 #endif
23277 else if (EQ (pointer, intern ("hourglass")))
23278 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23279 else if (EQ (pointer, Qmodeline))
23280 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23281 else
23282 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23283 }
23284
23285 if (cursor != No_Cursor)
23286 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23287 }
23288
23289 /* Take proper action when mouse has moved to the mode or header line
23290 or marginal area AREA of window W, x-position X and y-position Y.
23291 X is relative to the start of the text display area of W, so the
23292 width of bitmap areas and scroll bars must be subtracted to get a
23293 position relative to the start of the mode line. */
23294
23295 static void
23296 note_mode_line_or_margin_highlight (window, x, y, area)
23297 Lisp_Object window;
23298 int x, y;
23299 enum window_part area;
23300 {
23301 struct window *w = XWINDOW (window);
23302 struct frame *f = XFRAME (w->frame);
23303 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23304 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23305 Lisp_Object pointer = Qnil;
23306 int charpos, dx, dy, width, height;
23307 Lisp_Object string, object = Qnil;
23308 Lisp_Object pos, help;
23309
23310 Lisp_Object mouse_face;
23311 int original_x_pixel = x;
23312 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23313 struct glyph_row *row;
23314
23315 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23316 {
23317 int x0;
23318 struct glyph *end;
23319
23320 string = mode_line_string (w, area, &x, &y, &charpos,
23321 &object, &dx, &dy, &width, &height);
23322
23323 row = (area == ON_MODE_LINE
23324 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23325 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23326
23327 /* Find glyph */
23328 if (row->mode_line_p && row->enabled_p)
23329 {
23330 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23331 end = glyph + row->used[TEXT_AREA];
23332
23333 for (x0 = original_x_pixel;
23334 glyph < end && x0 >= glyph->pixel_width;
23335 ++glyph)
23336 x0 -= glyph->pixel_width;
23337
23338 if (glyph >= end)
23339 glyph = NULL;
23340 }
23341 }
23342 else
23343 {
23344 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23345 string = marginal_area_string (w, area, &x, &y, &charpos,
23346 &object, &dx, &dy, &width, &height);
23347 }
23348
23349 help = Qnil;
23350
23351 if (IMAGEP (object))
23352 {
23353 Lisp_Object image_map, hotspot;
23354 if ((image_map = Fplist_get (XCDR (object), QCmap),
23355 !NILP (image_map))
23356 && (hotspot = find_hot_spot (image_map, dx, dy),
23357 CONSP (hotspot))
23358 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23359 {
23360 Lisp_Object area_id, plist;
23361
23362 area_id = XCAR (hotspot);
23363 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23364 If so, we could look for mouse-enter, mouse-leave
23365 properties in PLIST (and do something...). */
23366 hotspot = XCDR (hotspot);
23367 if (CONSP (hotspot)
23368 && (plist = XCAR (hotspot), CONSP (plist)))
23369 {
23370 pointer = Fplist_get (plist, Qpointer);
23371 if (NILP (pointer))
23372 pointer = Qhand;
23373 help = Fplist_get (plist, Qhelp_echo);
23374 if (!NILP (help))
23375 {
23376 help_echo_string = help;
23377 /* Is this correct? ++kfs */
23378 XSETWINDOW (help_echo_window, w);
23379 help_echo_object = w->buffer;
23380 help_echo_pos = charpos;
23381 }
23382 }
23383 }
23384 if (NILP (pointer))
23385 pointer = Fplist_get (XCDR (object), QCpointer);
23386 }
23387
23388 if (STRINGP (string))
23389 {
23390 pos = make_number (charpos);
23391 /* If we're on a string with `help-echo' text property, arrange
23392 for the help to be displayed. This is done by setting the
23393 global variable help_echo_string to the help string. */
23394 if (NILP (help))
23395 {
23396 help = Fget_text_property (pos, Qhelp_echo, string);
23397 if (!NILP (help))
23398 {
23399 help_echo_string = help;
23400 XSETWINDOW (help_echo_window, w);
23401 help_echo_object = string;
23402 help_echo_pos = charpos;
23403 }
23404 }
23405
23406 if (NILP (pointer))
23407 pointer = Fget_text_property (pos, Qpointer, string);
23408
23409 /* Change the mouse pointer according to what is under X/Y. */
23410 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23411 {
23412 Lisp_Object map;
23413 map = Fget_text_property (pos, Qlocal_map, string);
23414 if (!KEYMAPP (map))
23415 map = Fget_text_property (pos, Qkeymap, string);
23416 if (!KEYMAPP (map))
23417 cursor = dpyinfo->vertical_scroll_bar_cursor;
23418 }
23419
23420 /* Change the mouse face according to what is under X/Y. */
23421 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23422 if (!NILP (mouse_face)
23423 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23424 && glyph)
23425 {
23426 Lisp_Object b, e;
23427
23428 struct glyph * tmp_glyph;
23429
23430 int gpos;
23431 int gseq_length;
23432 int total_pixel_width;
23433 EMACS_INT ignore;
23434
23435 int vpos, hpos;
23436
23437 b = Fprevious_single_property_change (make_number (charpos + 1),
23438 Qmouse_face, string, Qnil);
23439 if (NILP (b))
23440 b = make_number (0);
23441
23442 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23443 if (NILP (e))
23444 e = make_number (SCHARS (string));
23445
23446 /* Calculate the position(glyph position: GPOS) of GLYPH in
23447 displayed string. GPOS is different from CHARPOS.
23448
23449 CHARPOS is the position of glyph in internal string
23450 object. A mode line string format has structures which
23451 is converted to a flatten by emacs lisp interpreter.
23452 The internal string is an element of the structures.
23453 The displayed string is the flatten string. */
23454 gpos = 0;
23455 if (glyph > row_start_glyph)
23456 {
23457 tmp_glyph = glyph - 1;
23458 while (tmp_glyph >= row_start_glyph
23459 && tmp_glyph->charpos >= XINT (b)
23460 && EQ (tmp_glyph->object, glyph->object))
23461 {
23462 tmp_glyph--;
23463 gpos++;
23464 }
23465 }
23466
23467 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23468 displayed string holding GLYPH.
23469
23470 GSEQ_LENGTH is different from SCHARS (STRING).
23471 SCHARS (STRING) returns the length of the internal string. */
23472 for (tmp_glyph = glyph, gseq_length = gpos;
23473 tmp_glyph->charpos < XINT (e);
23474 tmp_glyph++, gseq_length++)
23475 {
23476 if (!EQ (tmp_glyph->object, glyph->object))
23477 break;
23478 }
23479
23480 total_pixel_width = 0;
23481 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23482 total_pixel_width += tmp_glyph->pixel_width;
23483
23484 /* Pre calculation of re-rendering position */
23485 vpos = (x - gpos);
23486 hpos = (area == ON_MODE_LINE
23487 ? (w->current_matrix)->nrows - 1
23488 : 0);
23489
23490 /* If the re-rendering position is included in the last
23491 re-rendering area, we should do nothing. */
23492 if ( EQ (window, dpyinfo->mouse_face_window)
23493 && dpyinfo->mouse_face_beg_col <= vpos
23494 && vpos < dpyinfo->mouse_face_end_col
23495 && dpyinfo->mouse_face_beg_row == hpos )
23496 return;
23497
23498 if (clear_mouse_face (dpyinfo))
23499 cursor = No_Cursor;
23500
23501 dpyinfo->mouse_face_beg_col = vpos;
23502 dpyinfo->mouse_face_beg_row = hpos;
23503
23504 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23505 dpyinfo->mouse_face_beg_y = 0;
23506
23507 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23508 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23509
23510 dpyinfo->mouse_face_end_x = 0;
23511 dpyinfo->mouse_face_end_y = 0;
23512
23513 dpyinfo->mouse_face_past_end = 0;
23514 dpyinfo->mouse_face_window = window;
23515
23516 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23517 charpos,
23518 0, 0, 0, &ignore,
23519 glyph->face_id, 1);
23520 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23521
23522 if (NILP (pointer))
23523 pointer = Qhand;
23524 }
23525 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23526 clear_mouse_face (dpyinfo);
23527 }
23528 define_frame_cursor1 (f, cursor, pointer);
23529 }
23530
23531
23532 /* EXPORT:
23533 Take proper action when the mouse has moved to position X, Y on
23534 frame F as regards highlighting characters that have mouse-face
23535 properties. Also de-highlighting chars where the mouse was before.
23536 X and Y can be negative or out of range. */
23537
23538 void
23539 note_mouse_highlight (f, x, y)
23540 struct frame *f;
23541 int x, y;
23542 {
23543 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23544 enum window_part part;
23545 Lisp_Object window;
23546 struct window *w;
23547 Cursor cursor = No_Cursor;
23548 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23549 struct buffer *b;
23550
23551 /* When a menu is active, don't highlight because this looks odd. */
23552 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23553 if (popup_activated ())
23554 return;
23555 #endif
23556
23557 if (NILP (Vmouse_highlight)
23558 || !f->glyphs_initialized_p)
23559 return;
23560
23561 dpyinfo->mouse_face_mouse_x = x;
23562 dpyinfo->mouse_face_mouse_y = y;
23563 dpyinfo->mouse_face_mouse_frame = f;
23564
23565 if (dpyinfo->mouse_face_defer)
23566 return;
23567
23568 if (gc_in_progress)
23569 {
23570 dpyinfo->mouse_face_deferred_gc = 1;
23571 return;
23572 }
23573
23574 /* Which window is that in? */
23575 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23576
23577 /* If we were displaying active text in another window, clear that.
23578 Also clear if we move out of text area in same window. */
23579 if (! EQ (window, dpyinfo->mouse_face_window)
23580 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23581 && !NILP (dpyinfo->mouse_face_window)))
23582 clear_mouse_face (dpyinfo);
23583
23584 /* Not on a window -> return. */
23585 if (!WINDOWP (window))
23586 return;
23587
23588 /* Reset help_echo_string. It will get recomputed below. */
23589 help_echo_string = Qnil;
23590
23591 /* Convert to window-relative pixel coordinates. */
23592 w = XWINDOW (window);
23593 frame_to_window_pixel_xy (w, &x, &y);
23594
23595 /* Handle tool-bar window differently since it doesn't display a
23596 buffer. */
23597 if (EQ (window, f->tool_bar_window))
23598 {
23599 note_tool_bar_highlight (f, x, y);
23600 return;
23601 }
23602
23603 /* Mouse is on the mode, header line or margin? */
23604 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23605 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23606 {
23607 note_mode_line_or_margin_highlight (window, x, y, part);
23608 return;
23609 }
23610
23611 if (part == ON_VERTICAL_BORDER)
23612 {
23613 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23614 help_echo_string = build_string ("drag-mouse-1: resize");
23615 }
23616 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23617 || part == ON_SCROLL_BAR)
23618 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23619 else
23620 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23621
23622 /* Are we in a window whose display is up to date?
23623 And verify the buffer's text has not changed. */
23624 b = XBUFFER (w->buffer);
23625 if (part == ON_TEXT
23626 && EQ (w->window_end_valid, w->buffer)
23627 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23628 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23629 {
23630 int hpos, vpos, pos, i, dx, dy, area;
23631 struct glyph *glyph;
23632 Lisp_Object object;
23633 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23634 Lisp_Object *overlay_vec = NULL;
23635 int noverlays;
23636 struct buffer *obuf;
23637 int obegv, ozv, same_region;
23638
23639 /* Find the glyph under X/Y. */
23640 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23641
23642 /* Look for :pointer property on image. */
23643 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23644 {
23645 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23646 if (img != NULL && IMAGEP (img->spec))
23647 {
23648 Lisp_Object image_map, hotspot;
23649 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23650 !NILP (image_map))
23651 && (hotspot = find_hot_spot (image_map,
23652 glyph->slice.x + dx,
23653 glyph->slice.y + dy),
23654 CONSP (hotspot))
23655 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23656 {
23657 Lisp_Object area_id, plist;
23658
23659 area_id = XCAR (hotspot);
23660 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23661 If so, we could look for mouse-enter, mouse-leave
23662 properties in PLIST (and do something...). */
23663 hotspot = XCDR (hotspot);
23664 if (CONSP (hotspot)
23665 && (plist = XCAR (hotspot), CONSP (plist)))
23666 {
23667 pointer = Fplist_get (plist, Qpointer);
23668 if (NILP (pointer))
23669 pointer = Qhand;
23670 help_echo_string = Fplist_get (plist, Qhelp_echo);
23671 if (!NILP (help_echo_string))
23672 {
23673 help_echo_window = window;
23674 help_echo_object = glyph->object;
23675 help_echo_pos = glyph->charpos;
23676 }
23677 }
23678 }
23679 if (NILP (pointer))
23680 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23681 }
23682 }
23683
23684 /* Clear mouse face if X/Y not over text. */
23685 if (glyph == NULL
23686 || area != TEXT_AREA
23687 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23688 {
23689 if (clear_mouse_face (dpyinfo))
23690 cursor = No_Cursor;
23691 if (NILP (pointer))
23692 {
23693 if (area != TEXT_AREA)
23694 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23695 else
23696 pointer = Vvoid_text_area_pointer;
23697 }
23698 goto set_cursor;
23699 }
23700
23701 pos = glyph->charpos;
23702 object = glyph->object;
23703 if (!STRINGP (object) && !BUFFERP (object))
23704 goto set_cursor;
23705
23706 /* If we get an out-of-range value, return now; avoid an error. */
23707 if (BUFFERP (object) && pos > BUF_Z (b))
23708 goto set_cursor;
23709
23710 /* Make the window's buffer temporarily current for
23711 overlays_at and compute_char_face. */
23712 obuf = current_buffer;
23713 current_buffer = b;
23714 obegv = BEGV;
23715 ozv = ZV;
23716 BEGV = BEG;
23717 ZV = Z;
23718
23719 /* Is this char mouse-active or does it have help-echo? */
23720 position = make_number (pos);
23721
23722 if (BUFFERP (object))
23723 {
23724 /* Put all the overlays we want in a vector in overlay_vec. */
23725 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23726 /* Sort overlays into increasing priority order. */
23727 noverlays = sort_overlays (overlay_vec, noverlays, w);
23728 }
23729 else
23730 noverlays = 0;
23731
23732 same_region = (EQ (window, dpyinfo->mouse_face_window)
23733 && vpos >= dpyinfo->mouse_face_beg_row
23734 && vpos <= dpyinfo->mouse_face_end_row
23735 && (vpos > dpyinfo->mouse_face_beg_row
23736 || hpos >= dpyinfo->mouse_face_beg_col)
23737 && (vpos < dpyinfo->mouse_face_end_row
23738 || hpos < dpyinfo->mouse_face_end_col
23739 || dpyinfo->mouse_face_past_end));
23740
23741 if (same_region)
23742 cursor = No_Cursor;
23743
23744 /* Check mouse-face highlighting. */
23745 if (! same_region
23746 /* If there exists an overlay with mouse-face overlapping
23747 the one we are currently highlighting, we have to
23748 check if we enter the overlapping overlay, and then
23749 highlight only that. */
23750 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23751 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23752 {
23753 /* Find the highest priority overlay with a mouse-face. */
23754 overlay = Qnil;
23755 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23756 {
23757 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23758 if (!NILP (mouse_face))
23759 overlay = overlay_vec[i];
23760 }
23761
23762 /* If we're highlighting the same overlay as before, there's
23763 no need to do that again. */
23764 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23765 goto check_help_echo;
23766 dpyinfo->mouse_face_overlay = overlay;
23767
23768 /* Clear the display of the old active region, if any. */
23769 if (clear_mouse_face (dpyinfo))
23770 cursor = No_Cursor;
23771
23772 /* If no overlay applies, get a text property. */
23773 if (NILP (overlay))
23774 mouse_face = Fget_text_property (position, Qmouse_face, object);
23775
23776 /* Next, compute the bounds of the mouse highlighting and
23777 display it. */
23778 if (!NILP (mouse_face) && STRINGP (object))
23779 {
23780 /* The mouse-highlighting comes from a display string
23781 with a mouse-face. */
23782 Lisp_Object b, e;
23783 EMACS_INT ignore;
23784
23785 b = Fprevious_single_property_change
23786 (make_number (pos + 1), Qmouse_face, object, Qnil);
23787 e = Fnext_single_property_change
23788 (position, Qmouse_face, object, Qnil);
23789 if (NILP (b))
23790 b = make_number (0);
23791 if (NILP (e))
23792 e = make_number (SCHARS (object) - 1);
23793
23794 fast_find_string_pos (w, XINT (b), object,
23795 &dpyinfo->mouse_face_beg_col,
23796 &dpyinfo->mouse_face_beg_row,
23797 &dpyinfo->mouse_face_beg_x,
23798 &dpyinfo->mouse_face_beg_y, 0);
23799 fast_find_string_pos (w, XINT (e), object,
23800 &dpyinfo->mouse_face_end_col,
23801 &dpyinfo->mouse_face_end_row,
23802 &dpyinfo->mouse_face_end_x,
23803 &dpyinfo->mouse_face_end_y, 1);
23804 dpyinfo->mouse_face_past_end = 0;
23805 dpyinfo->mouse_face_window = window;
23806 dpyinfo->mouse_face_face_id
23807 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23808 glyph->face_id, 1);
23809 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23810 cursor = No_Cursor;
23811 }
23812 else
23813 {
23814 /* The mouse-highlighting, if any, comes from an overlay
23815 or text property in the buffer. */
23816 Lisp_Object buffer, display_string;
23817
23818 if (STRINGP (object))
23819 {
23820 /* If we are on a display string with no mouse-face,
23821 check if the text under it has one. */
23822 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23823 int start = MATRIX_ROW_START_CHARPOS (r);
23824 pos = string_buffer_position (w, object, start);
23825 if (pos > 0)
23826 {
23827 mouse_face = get_char_property_and_overlay
23828 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23829 buffer = w->buffer;
23830 display_string = object;
23831 }
23832 }
23833 else
23834 {
23835 buffer = object;
23836 display_string = Qnil;
23837 }
23838
23839 if (!NILP (mouse_face))
23840 {
23841 Lisp_Object before, after;
23842 Lisp_Object before_string, after_string;
23843
23844 if (NILP (overlay))
23845 {
23846 /* Handle the text property case. */
23847 before = Fprevious_single_property_change
23848 (make_number (pos + 1), Qmouse_face, buffer,
23849 Fmarker_position (w->start));
23850 after = Fnext_single_property_change
23851 (make_number (pos), Qmouse_face, buffer,
23852 make_number (BUF_Z (XBUFFER (buffer))
23853 - XFASTINT (w->window_end_pos)));
23854 before_string = after_string = Qnil;
23855 }
23856 else
23857 {
23858 /* Handle the overlay case. */
23859 before = Foverlay_start (overlay);
23860 after = Foverlay_end (overlay);
23861 before_string = Foverlay_get (overlay, Qbefore_string);
23862 after_string = Foverlay_get (overlay, Qafter_string);
23863
23864 if (!STRINGP (before_string)) before_string = Qnil;
23865 if (!STRINGP (after_string)) after_string = Qnil;
23866 }
23867
23868 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23869 XFASTINT (before),
23870 XFASTINT (after),
23871 before_string, after_string,
23872 display_string);
23873 cursor = No_Cursor;
23874 }
23875 }
23876 }
23877
23878 check_help_echo:
23879
23880 /* Look for a `help-echo' property. */
23881 if (NILP (help_echo_string)) {
23882 Lisp_Object help, overlay;
23883
23884 /* Check overlays first. */
23885 help = overlay = Qnil;
23886 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23887 {
23888 overlay = overlay_vec[i];
23889 help = Foverlay_get (overlay, Qhelp_echo);
23890 }
23891
23892 if (!NILP (help))
23893 {
23894 help_echo_string = help;
23895 help_echo_window = window;
23896 help_echo_object = overlay;
23897 help_echo_pos = pos;
23898 }
23899 else
23900 {
23901 Lisp_Object object = glyph->object;
23902 int charpos = glyph->charpos;
23903
23904 /* Try text properties. */
23905 if (STRINGP (object)
23906 && charpos >= 0
23907 && charpos < SCHARS (object))
23908 {
23909 help = Fget_text_property (make_number (charpos),
23910 Qhelp_echo, object);
23911 if (NILP (help))
23912 {
23913 /* If the string itself doesn't specify a help-echo,
23914 see if the buffer text ``under'' it does. */
23915 struct glyph_row *r
23916 = MATRIX_ROW (w->current_matrix, vpos);
23917 int start = MATRIX_ROW_START_CHARPOS (r);
23918 int pos = string_buffer_position (w, object, start);
23919 if (pos > 0)
23920 {
23921 help = Fget_char_property (make_number (pos),
23922 Qhelp_echo, w->buffer);
23923 if (!NILP (help))
23924 {
23925 charpos = pos;
23926 object = w->buffer;
23927 }
23928 }
23929 }
23930 }
23931 else if (BUFFERP (object)
23932 && charpos >= BEGV
23933 && charpos < ZV)
23934 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23935 object);
23936
23937 if (!NILP (help))
23938 {
23939 help_echo_string = help;
23940 help_echo_window = window;
23941 help_echo_object = object;
23942 help_echo_pos = charpos;
23943 }
23944 }
23945 }
23946
23947 /* Look for a `pointer' property. */
23948 if (NILP (pointer))
23949 {
23950 /* Check overlays first. */
23951 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23952 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23953
23954 if (NILP (pointer))
23955 {
23956 Lisp_Object object = glyph->object;
23957 int charpos = glyph->charpos;
23958
23959 /* Try text properties. */
23960 if (STRINGP (object)
23961 && charpos >= 0
23962 && charpos < SCHARS (object))
23963 {
23964 pointer = Fget_text_property (make_number (charpos),
23965 Qpointer, object);
23966 if (NILP (pointer))
23967 {
23968 /* If the string itself doesn't specify a pointer,
23969 see if the buffer text ``under'' it does. */
23970 struct glyph_row *r
23971 = MATRIX_ROW (w->current_matrix, vpos);
23972 int start = MATRIX_ROW_START_CHARPOS (r);
23973 int pos = string_buffer_position (w, object, start);
23974 if (pos > 0)
23975 pointer = Fget_char_property (make_number (pos),
23976 Qpointer, w->buffer);
23977 }
23978 }
23979 else if (BUFFERP (object)
23980 && charpos >= BEGV
23981 && charpos < ZV)
23982 pointer = Fget_text_property (make_number (charpos),
23983 Qpointer, object);
23984 }
23985 }
23986
23987 BEGV = obegv;
23988 ZV = ozv;
23989 current_buffer = obuf;
23990 }
23991
23992 set_cursor:
23993
23994 define_frame_cursor1 (f, cursor, pointer);
23995 }
23996
23997
23998 /* EXPORT for RIF:
23999 Clear any mouse-face on window W. This function is part of the
24000 redisplay interface, and is called from try_window_id and similar
24001 functions to ensure the mouse-highlight is off. */
24002
24003 void
24004 x_clear_window_mouse_face (w)
24005 struct window *w;
24006 {
24007 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24008 Lisp_Object window;
24009
24010 BLOCK_INPUT;
24011 XSETWINDOW (window, w);
24012 if (EQ (window, dpyinfo->mouse_face_window))
24013 clear_mouse_face (dpyinfo);
24014 UNBLOCK_INPUT;
24015 }
24016
24017
24018 /* EXPORT:
24019 Just discard the mouse face information for frame F, if any.
24020 This is used when the size of F is changed. */
24021
24022 void
24023 cancel_mouse_face (f)
24024 struct frame *f;
24025 {
24026 Lisp_Object window;
24027 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24028
24029 window = dpyinfo->mouse_face_window;
24030 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24031 {
24032 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24033 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24034 dpyinfo->mouse_face_window = Qnil;
24035 }
24036 }
24037
24038
24039 #endif /* HAVE_WINDOW_SYSTEM */
24040
24041 \f
24042 /***********************************************************************
24043 Exposure Events
24044 ***********************************************************************/
24045
24046 #ifdef HAVE_WINDOW_SYSTEM
24047
24048 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24049 which intersects rectangle R. R is in window-relative coordinates. */
24050
24051 static void
24052 expose_area (w, row, r, area)
24053 struct window *w;
24054 struct glyph_row *row;
24055 XRectangle *r;
24056 enum glyph_row_area area;
24057 {
24058 struct glyph *first = row->glyphs[area];
24059 struct glyph *end = row->glyphs[area] + row->used[area];
24060 struct glyph *last;
24061 int first_x, start_x, x;
24062
24063 if (area == TEXT_AREA && row->fill_line_p)
24064 /* If row extends face to end of line write the whole line. */
24065 draw_glyphs (w, 0, row, area,
24066 0, row->used[area],
24067 DRAW_NORMAL_TEXT, 0);
24068 else
24069 {
24070 /* Set START_X to the window-relative start position for drawing glyphs of
24071 AREA. The first glyph of the text area can be partially visible.
24072 The first glyphs of other areas cannot. */
24073 start_x = window_box_left_offset (w, area);
24074 x = start_x;
24075 if (area == TEXT_AREA)
24076 x += row->x;
24077
24078 /* Find the first glyph that must be redrawn. */
24079 while (first < end
24080 && x + first->pixel_width < r->x)
24081 {
24082 x += first->pixel_width;
24083 ++first;
24084 }
24085
24086 /* Find the last one. */
24087 last = first;
24088 first_x = x;
24089 while (last < end
24090 && x < r->x + r->width)
24091 {
24092 x += last->pixel_width;
24093 ++last;
24094 }
24095
24096 /* Repaint. */
24097 if (last > first)
24098 draw_glyphs (w, first_x - start_x, row, area,
24099 first - row->glyphs[area], last - row->glyphs[area],
24100 DRAW_NORMAL_TEXT, 0);
24101 }
24102 }
24103
24104
24105 /* Redraw the parts of the glyph row ROW on window W intersecting
24106 rectangle R. R is in window-relative coordinates. Value is
24107 non-zero if mouse-face was overwritten. */
24108
24109 static int
24110 expose_line (w, row, r)
24111 struct window *w;
24112 struct glyph_row *row;
24113 XRectangle *r;
24114 {
24115 xassert (row->enabled_p);
24116
24117 if (row->mode_line_p || w->pseudo_window_p)
24118 draw_glyphs (w, 0, row, TEXT_AREA,
24119 0, row->used[TEXT_AREA],
24120 DRAW_NORMAL_TEXT, 0);
24121 else
24122 {
24123 if (row->used[LEFT_MARGIN_AREA])
24124 expose_area (w, row, r, LEFT_MARGIN_AREA);
24125 if (row->used[TEXT_AREA])
24126 expose_area (w, row, r, TEXT_AREA);
24127 if (row->used[RIGHT_MARGIN_AREA])
24128 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24129 draw_row_fringe_bitmaps (w, row);
24130 }
24131
24132 return row->mouse_face_p;
24133 }
24134
24135
24136 /* Redraw those parts of glyphs rows during expose event handling that
24137 overlap other rows. Redrawing of an exposed line writes over parts
24138 of lines overlapping that exposed line; this function fixes that.
24139
24140 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24141 row in W's current matrix that is exposed and overlaps other rows.
24142 LAST_OVERLAPPING_ROW is the last such row. */
24143
24144 static void
24145 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24146 struct window *w;
24147 struct glyph_row *first_overlapping_row;
24148 struct glyph_row *last_overlapping_row;
24149 XRectangle *r;
24150 {
24151 struct glyph_row *row;
24152
24153 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24154 if (row->overlapping_p)
24155 {
24156 xassert (row->enabled_p && !row->mode_line_p);
24157
24158 row->clip = r;
24159 if (row->used[LEFT_MARGIN_AREA])
24160 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24161
24162 if (row->used[TEXT_AREA])
24163 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24164
24165 if (row->used[RIGHT_MARGIN_AREA])
24166 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24167 row->clip = NULL;
24168 }
24169 }
24170
24171
24172 /* Return non-zero if W's cursor intersects rectangle R. */
24173
24174 static int
24175 phys_cursor_in_rect_p (w, r)
24176 struct window *w;
24177 XRectangle *r;
24178 {
24179 XRectangle cr, result;
24180 struct glyph *cursor_glyph;
24181 struct glyph_row *row;
24182
24183 if (w->phys_cursor.vpos >= 0
24184 && w->phys_cursor.vpos < w->current_matrix->nrows
24185 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24186 row->enabled_p)
24187 && row->cursor_in_fringe_p)
24188 {
24189 /* Cursor is in the fringe. */
24190 cr.x = window_box_right_offset (w,
24191 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24192 ? RIGHT_MARGIN_AREA
24193 : TEXT_AREA));
24194 cr.y = row->y;
24195 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24196 cr.height = row->height;
24197 return x_intersect_rectangles (&cr, r, &result);
24198 }
24199
24200 cursor_glyph = get_phys_cursor_glyph (w);
24201 if (cursor_glyph)
24202 {
24203 /* r is relative to W's box, but w->phys_cursor.x is relative
24204 to left edge of W's TEXT area. Adjust it. */
24205 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24206 cr.y = w->phys_cursor.y;
24207 cr.width = cursor_glyph->pixel_width;
24208 cr.height = w->phys_cursor_height;
24209 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24210 I assume the effect is the same -- and this is portable. */
24211 return x_intersect_rectangles (&cr, r, &result);
24212 }
24213 /* If we don't understand the format, pretend we're not in the hot-spot. */
24214 return 0;
24215 }
24216
24217
24218 /* EXPORT:
24219 Draw a vertical window border to the right of window W if W doesn't
24220 have vertical scroll bars. */
24221
24222 void
24223 x_draw_vertical_border (w)
24224 struct window *w;
24225 {
24226 struct frame *f = XFRAME (WINDOW_FRAME (w));
24227
24228 /* We could do better, if we knew what type of scroll-bar the adjacent
24229 windows (on either side) have... But we don't :-(
24230 However, I think this works ok. ++KFS 2003-04-25 */
24231
24232 /* Redraw borders between horizontally adjacent windows. Don't
24233 do it for frames with vertical scroll bars because either the
24234 right scroll bar of a window, or the left scroll bar of its
24235 neighbor will suffice as a border. */
24236 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24237 return;
24238
24239 if (!WINDOW_RIGHTMOST_P (w)
24240 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24241 {
24242 int x0, x1, y0, y1;
24243
24244 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24245 y1 -= 1;
24246
24247 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24248 x1 -= 1;
24249
24250 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24251 }
24252 else if (!WINDOW_LEFTMOST_P (w)
24253 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24254 {
24255 int x0, x1, y0, y1;
24256
24257 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24258 y1 -= 1;
24259
24260 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24261 x0 -= 1;
24262
24263 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24264 }
24265 }
24266
24267
24268 /* Redraw the part of window W intersection rectangle FR. Pixel
24269 coordinates in FR are frame-relative. Call this function with
24270 input blocked. Value is non-zero if the exposure overwrites
24271 mouse-face. */
24272
24273 static int
24274 expose_window (w, fr)
24275 struct window *w;
24276 XRectangle *fr;
24277 {
24278 struct frame *f = XFRAME (w->frame);
24279 XRectangle wr, r;
24280 int mouse_face_overwritten_p = 0;
24281
24282 /* If window is not yet fully initialized, do nothing. This can
24283 happen when toolkit scroll bars are used and a window is split.
24284 Reconfiguring the scroll bar will generate an expose for a newly
24285 created window. */
24286 if (w->current_matrix == NULL)
24287 return 0;
24288
24289 /* When we're currently updating the window, display and current
24290 matrix usually don't agree. Arrange for a thorough display
24291 later. */
24292 if (w == updated_window)
24293 {
24294 SET_FRAME_GARBAGED (f);
24295 return 0;
24296 }
24297
24298 /* Frame-relative pixel rectangle of W. */
24299 wr.x = WINDOW_LEFT_EDGE_X (w);
24300 wr.y = WINDOW_TOP_EDGE_Y (w);
24301 wr.width = WINDOW_TOTAL_WIDTH (w);
24302 wr.height = WINDOW_TOTAL_HEIGHT (w);
24303
24304 if (x_intersect_rectangles (fr, &wr, &r))
24305 {
24306 int yb = window_text_bottom_y (w);
24307 struct glyph_row *row;
24308 int cursor_cleared_p;
24309 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24310
24311 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24312 r.x, r.y, r.width, r.height));
24313
24314 /* Convert to window coordinates. */
24315 r.x -= WINDOW_LEFT_EDGE_X (w);
24316 r.y -= WINDOW_TOP_EDGE_Y (w);
24317
24318 /* Turn off the cursor. */
24319 if (!w->pseudo_window_p
24320 && phys_cursor_in_rect_p (w, &r))
24321 {
24322 x_clear_cursor (w);
24323 cursor_cleared_p = 1;
24324 }
24325 else
24326 cursor_cleared_p = 0;
24327
24328 /* Update lines intersecting rectangle R. */
24329 first_overlapping_row = last_overlapping_row = NULL;
24330 for (row = w->current_matrix->rows;
24331 row->enabled_p;
24332 ++row)
24333 {
24334 int y0 = row->y;
24335 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24336
24337 if ((y0 >= r.y && y0 < r.y + r.height)
24338 || (y1 > r.y && y1 < r.y + r.height)
24339 || (r.y >= y0 && r.y < y1)
24340 || (r.y + r.height > y0 && r.y + r.height < y1))
24341 {
24342 /* A header line may be overlapping, but there is no need
24343 to fix overlapping areas for them. KFS 2005-02-12 */
24344 if (row->overlapping_p && !row->mode_line_p)
24345 {
24346 if (first_overlapping_row == NULL)
24347 first_overlapping_row = row;
24348 last_overlapping_row = row;
24349 }
24350
24351 row->clip = fr;
24352 if (expose_line (w, row, &r))
24353 mouse_face_overwritten_p = 1;
24354 row->clip = NULL;
24355 }
24356 else if (row->overlapping_p)
24357 {
24358 /* We must redraw a row overlapping the exposed area. */
24359 if (y0 < r.y
24360 ? y0 + row->phys_height > r.y
24361 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24362 {
24363 if (first_overlapping_row == NULL)
24364 first_overlapping_row = row;
24365 last_overlapping_row = row;
24366 }
24367 }
24368
24369 if (y1 >= yb)
24370 break;
24371 }
24372
24373 /* Display the mode line if there is one. */
24374 if (WINDOW_WANTS_MODELINE_P (w)
24375 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24376 row->enabled_p)
24377 && row->y < r.y + r.height)
24378 {
24379 if (expose_line (w, row, &r))
24380 mouse_face_overwritten_p = 1;
24381 }
24382
24383 if (!w->pseudo_window_p)
24384 {
24385 /* Fix the display of overlapping rows. */
24386 if (first_overlapping_row)
24387 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24388 fr);
24389
24390 /* Draw border between windows. */
24391 x_draw_vertical_border (w);
24392
24393 /* Turn the cursor on again. */
24394 if (cursor_cleared_p)
24395 update_window_cursor (w, 1);
24396 }
24397 }
24398
24399 return mouse_face_overwritten_p;
24400 }
24401
24402
24403
24404 /* Redraw (parts) of all windows in the window tree rooted at W that
24405 intersect R. R contains frame pixel coordinates. Value is
24406 non-zero if the exposure overwrites mouse-face. */
24407
24408 static int
24409 expose_window_tree (w, r)
24410 struct window *w;
24411 XRectangle *r;
24412 {
24413 struct frame *f = XFRAME (w->frame);
24414 int mouse_face_overwritten_p = 0;
24415
24416 while (w && !FRAME_GARBAGED_P (f))
24417 {
24418 if (!NILP (w->hchild))
24419 mouse_face_overwritten_p
24420 |= expose_window_tree (XWINDOW (w->hchild), r);
24421 else if (!NILP (w->vchild))
24422 mouse_face_overwritten_p
24423 |= expose_window_tree (XWINDOW (w->vchild), r);
24424 else
24425 mouse_face_overwritten_p |= expose_window (w, r);
24426
24427 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24428 }
24429
24430 return mouse_face_overwritten_p;
24431 }
24432
24433
24434 /* EXPORT:
24435 Redisplay an exposed area of frame F. X and Y are the upper-left
24436 corner of the exposed rectangle. W and H are width and height of
24437 the exposed area. All are pixel values. W or H zero means redraw
24438 the entire frame. */
24439
24440 void
24441 expose_frame (f, x, y, w, h)
24442 struct frame *f;
24443 int x, y, w, h;
24444 {
24445 XRectangle r;
24446 int mouse_face_overwritten_p = 0;
24447
24448 TRACE ((stderr, "expose_frame "));
24449
24450 /* No need to redraw if frame will be redrawn soon. */
24451 if (FRAME_GARBAGED_P (f))
24452 {
24453 TRACE ((stderr, " garbaged\n"));
24454 return;
24455 }
24456
24457 /* If basic faces haven't been realized yet, there is no point in
24458 trying to redraw anything. This can happen when we get an expose
24459 event while Emacs is starting, e.g. by moving another window. */
24460 if (FRAME_FACE_CACHE (f) == NULL
24461 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24462 {
24463 TRACE ((stderr, " no faces\n"));
24464 return;
24465 }
24466
24467 if (w == 0 || h == 0)
24468 {
24469 r.x = r.y = 0;
24470 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24471 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24472 }
24473 else
24474 {
24475 r.x = x;
24476 r.y = y;
24477 r.width = w;
24478 r.height = h;
24479 }
24480
24481 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24482 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24483
24484 if (WINDOWP (f->tool_bar_window))
24485 mouse_face_overwritten_p
24486 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24487
24488 #ifdef HAVE_X_WINDOWS
24489 #ifndef MSDOS
24490 #ifndef USE_X_TOOLKIT
24491 if (WINDOWP (f->menu_bar_window))
24492 mouse_face_overwritten_p
24493 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24494 #endif /* not USE_X_TOOLKIT */
24495 #endif
24496 #endif
24497
24498 /* Some window managers support a focus-follows-mouse style with
24499 delayed raising of frames. Imagine a partially obscured frame,
24500 and moving the mouse into partially obscured mouse-face on that
24501 frame. The visible part of the mouse-face will be highlighted,
24502 then the WM raises the obscured frame. With at least one WM, KDE
24503 2.1, Emacs is not getting any event for the raising of the frame
24504 (even tried with SubstructureRedirectMask), only Expose events.
24505 These expose events will draw text normally, i.e. not
24506 highlighted. Which means we must redo the highlight here.
24507 Subsume it under ``we love X''. --gerd 2001-08-15 */
24508 /* Included in Windows version because Windows most likely does not
24509 do the right thing if any third party tool offers
24510 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24511 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24512 {
24513 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24514 if (f == dpyinfo->mouse_face_mouse_frame)
24515 {
24516 int x = dpyinfo->mouse_face_mouse_x;
24517 int y = dpyinfo->mouse_face_mouse_y;
24518 clear_mouse_face (dpyinfo);
24519 note_mouse_highlight (f, x, y);
24520 }
24521 }
24522 }
24523
24524
24525 /* EXPORT:
24526 Determine the intersection of two rectangles R1 and R2. Return
24527 the intersection in *RESULT. Value is non-zero if RESULT is not
24528 empty. */
24529
24530 int
24531 x_intersect_rectangles (r1, r2, result)
24532 XRectangle *r1, *r2, *result;
24533 {
24534 XRectangle *left, *right;
24535 XRectangle *upper, *lower;
24536 int intersection_p = 0;
24537
24538 /* Rearrange so that R1 is the left-most rectangle. */
24539 if (r1->x < r2->x)
24540 left = r1, right = r2;
24541 else
24542 left = r2, right = r1;
24543
24544 /* X0 of the intersection is right.x0, if this is inside R1,
24545 otherwise there is no intersection. */
24546 if (right->x <= left->x + left->width)
24547 {
24548 result->x = right->x;
24549
24550 /* The right end of the intersection is the minimum of the
24551 the right ends of left and right. */
24552 result->width = (min (left->x + left->width, right->x + right->width)
24553 - result->x);
24554
24555 /* Same game for Y. */
24556 if (r1->y < r2->y)
24557 upper = r1, lower = r2;
24558 else
24559 upper = r2, lower = r1;
24560
24561 /* The upper end of the intersection is lower.y0, if this is inside
24562 of upper. Otherwise, there is no intersection. */
24563 if (lower->y <= upper->y + upper->height)
24564 {
24565 result->y = lower->y;
24566
24567 /* The lower end of the intersection is the minimum of the lower
24568 ends of upper and lower. */
24569 result->height = (min (lower->y + lower->height,
24570 upper->y + upper->height)
24571 - result->y);
24572 intersection_p = 1;
24573 }
24574 }
24575
24576 return intersection_p;
24577 }
24578
24579 #endif /* HAVE_WINDOW_SYSTEM */
24580
24581 \f
24582 /***********************************************************************
24583 Initialization
24584 ***********************************************************************/
24585
24586 void
24587 syms_of_xdisp ()
24588 {
24589 Vwith_echo_area_save_vector = Qnil;
24590 staticpro (&Vwith_echo_area_save_vector);
24591
24592 Vmessage_stack = Qnil;
24593 staticpro (&Vmessage_stack);
24594
24595 Qinhibit_redisplay = intern ("inhibit-redisplay");
24596 staticpro (&Qinhibit_redisplay);
24597
24598 message_dolog_marker1 = Fmake_marker ();
24599 staticpro (&message_dolog_marker1);
24600 message_dolog_marker2 = Fmake_marker ();
24601 staticpro (&message_dolog_marker2);
24602 message_dolog_marker3 = Fmake_marker ();
24603 staticpro (&message_dolog_marker3);
24604
24605 #if GLYPH_DEBUG
24606 defsubr (&Sdump_frame_glyph_matrix);
24607 defsubr (&Sdump_glyph_matrix);
24608 defsubr (&Sdump_glyph_row);
24609 defsubr (&Sdump_tool_bar_row);
24610 defsubr (&Strace_redisplay);
24611 defsubr (&Strace_to_stderr);
24612 #endif
24613 #ifdef HAVE_WINDOW_SYSTEM
24614 defsubr (&Stool_bar_lines_needed);
24615 defsubr (&Slookup_image_map);
24616 #endif
24617 defsubr (&Sformat_mode_line);
24618 defsubr (&Sinvisible_p);
24619
24620 staticpro (&Qmenu_bar_update_hook);
24621 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24622
24623 staticpro (&Qoverriding_terminal_local_map);
24624 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24625
24626 staticpro (&Qoverriding_local_map);
24627 Qoverriding_local_map = intern ("overriding-local-map");
24628
24629 staticpro (&Qwindow_scroll_functions);
24630 Qwindow_scroll_functions = intern ("window-scroll-functions");
24631
24632 staticpro (&Qwindow_text_change_functions);
24633 Qwindow_text_change_functions = intern ("window-text-change-functions");
24634
24635 staticpro (&Qredisplay_end_trigger_functions);
24636 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24637
24638 staticpro (&Qinhibit_point_motion_hooks);
24639 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24640
24641 Qeval = intern ("eval");
24642 staticpro (&Qeval);
24643
24644 QCdata = intern (":data");
24645 staticpro (&QCdata);
24646 Qdisplay = intern ("display");
24647 staticpro (&Qdisplay);
24648 Qspace_width = intern ("space-width");
24649 staticpro (&Qspace_width);
24650 Qraise = intern ("raise");
24651 staticpro (&Qraise);
24652 Qslice = intern ("slice");
24653 staticpro (&Qslice);
24654 Qspace = intern ("space");
24655 staticpro (&Qspace);
24656 Qmargin = intern ("margin");
24657 staticpro (&Qmargin);
24658 Qpointer = intern ("pointer");
24659 staticpro (&Qpointer);
24660 Qleft_margin = intern ("left-margin");
24661 staticpro (&Qleft_margin);
24662 Qright_margin = intern ("right-margin");
24663 staticpro (&Qright_margin);
24664 Qcenter = intern ("center");
24665 staticpro (&Qcenter);
24666 Qline_height = intern ("line-height");
24667 staticpro (&Qline_height);
24668 QCalign_to = intern (":align-to");
24669 staticpro (&QCalign_to);
24670 QCrelative_width = intern (":relative-width");
24671 staticpro (&QCrelative_width);
24672 QCrelative_height = intern (":relative-height");
24673 staticpro (&QCrelative_height);
24674 QCeval = intern (":eval");
24675 staticpro (&QCeval);
24676 QCpropertize = intern (":propertize");
24677 staticpro (&QCpropertize);
24678 QCfile = intern (":file");
24679 staticpro (&QCfile);
24680 Qfontified = intern ("fontified");
24681 staticpro (&Qfontified);
24682 Qfontification_functions = intern ("fontification-functions");
24683 staticpro (&Qfontification_functions);
24684 Qtrailing_whitespace = intern ("trailing-whitespace");
24685 staticpro (&Qtrailing_whitespace);
24686 Qescape_glyph = intern ("escape-glyph");
24687 staticpro (&Qescape_glyph);
24688 Qnobreak_space = intern ("nobreak-space");
24689 staticpro (&Qnobreak_space);
24690 Qimage = intern ("image");
24691 staticpro (&Qimage);
24692 QCmap = intern (":map");
24693 staticpro (&QCmap);
24694 QCpointer = intern (":pointer");
24695 staticpro (&QCpointer);
24696 Qrect = intern ("rect");
24697 staticpro (&Qrect);
24698 Qcircle = intern ("circle");
24699 staticpro (&Qcircle);
24700 Qpoly = intern ("poly");
24701 staticpro (&Qpoly);
24702 Qmessage_truncate_lines = intern ("message-truncate-lines");
24703 staticpro (&Qmessage_truncate_lines);
24704 Qgrow_only = intern ("grow-only");
24705 staticpro (&Qgrow_only);
24706 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24707 staticpro (&Qinhibit_menubar_update);
24708 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24709 staticpro (&Qinhibit_eval_during_redisplay);
24710 Qposition = intern ("position");
24711 staticpro (&Qposition);
24712 Qbuffer_position = intern ("buffer-position");
24713 staticpro (&Qbuffer_position);
24714 Qobject = intern ("object");
24715 staticpro (&Qobject);
24716 Qbar = intern ("bar");
24717 staticpro (&Qbar);
24718 Qhbar = intern ("hbar");
24719 staticpro (&Qhbar);
24720 Qbox = intern ("box");
24721 staticpro (&Qbox);
24722 Qhollow = intern ("hollow");
24723 staticpro (&Qhollow);
24724 Qhand = intern ("hand");
24725 staticpro (&Qhand);
24726 Qarrow = intern ("arrow");
24727 staticpro (&Qarrow);
24728 Qtext = intern ("text");
24729 staticpro (&Qtext);
24730 Qrisky_local_variable = intern ("risky-local-variable");
24731 staticpro (&Qrisky_local_variable);
24732 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24733 staticpro (&Qinhibit_free_realized_faces);
24734
24735 list_of_error = Fcons (Fcons (intern ("error"),
24736 Fcons (intern ("void-variable"), Qnil)),
24737 Qnil);
24738 staticpro (&list_of_error);
24739
24740 Qlast_arrow_position = intern ("last-arrow-position");
24741 staticpro (&Qlast_arrow_position);
24742 Qlast_arrow_string = intern ("last-arrow-string");
24743 staticpro (&Qlast_arrow_string);
24744
24745 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24746 staticpro (&Qoverlay_arrow_string);
24747 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24748 staticpro (&Qoverlay_arrow_bitmap);
24749
24750 echo_buffer[0] = echo_buffer[1] = Qnil;
24751 staticpro (&echo_buffer[0]);
24752 staticpro (&echo_buffer[1]);
24753
24754 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24755 staticpro (&echo_area_buffer[0]);
24756 staticpro (&echo_area_buffer[1]);
24757
24758 Vmessages_buffer_name = build_string ("*Messages*");
24759 staticpro (&Vmessages_buffer_name);
24760
24761 mode_line_proptrans_alist = Qnil;
24762 staticpro (&mode_line_proptrans_alist);
24763 mode_line_string_list = Qnil;
24764 staticpro (&mode_line_string_list);
24765 mode_line_string_face = Qnil;
24766 staticpro (&mode_line_string_face);
24767 mode_line_string_face_prop = Qnil;
24768 staticpro (&mode_line_string_face_prop);
24769 Vmode_line_unwind_vector = Qnil;
24770 staticpro (&Vmode_line_unwind_vector);
24771
24772 help_echo_string = Qnil;
24773 staticpro (&help_echo_string);
24774 help_echo_object = Qnil;
24775 staticpro (&help_echo_object);
24776 help_echo_window = Qnil;
24777 staticpro (&help_echo_window);
24778 previous_help_echo_string = Qnil;
24779 staticpro (&previous_help_echo_string);
24780 help_echo_pos = -1;
24781
24782 #ifdef HAVE_WINDOW_SYSTEM
24783 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24784 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24785 For example, if a block cursor is over a tab, it will be drawn as
24786 wide as that tab on the display. */);
24787 x_stretch_cursor_p = 0;
24788 #endif
24789
24790 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24791 doc: /* *Non-nil means highlight trailing whitespace.
24792 The face used for trailing whitespace is `trailing-whitespace'. */);
24793 Vshow_trailing_whitespace = Qnil;
24794
24795 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24796 doc: /* *Control highlighting of nobreak space and soft hyphen.
24797 A value of t means highlight the character itself (for nobreak space,
24798 use face `nobreak-space').
24799 A value of nil means no highlighting.
24800 Other values mean display the escape glyph followed by an ordinary
24801 space or ordinary hyphen. */);
24802 Vnobreak_char_display = Qt;
24803
24804 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24805 doc: /* *The pointer shape to show in void text areas.
24806 A value of nil means to show the text pointer. Other options are `arrow',
24807 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24808 Vvoid_text_area_pointer = Qarrow;
24809
24810 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24811 doc: /* Non-nil means don't actually do any redisplay.
24812 This is used for internal purposes. */);
24813 Vinhibit_redisplay = Qnil;
24814
24815 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24816 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24817 Vglobal_mode_string = Qnil;
24818
24819 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24820 doc: /* Marker for where to display an arrow on top of the buffer text.
24821 This must be the beginning of a line in order to work.
24822 See also `overlay-arrow-string'. */);
24823 Voverlay_arrow_position = Qnil;
24824
24825 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24826 doc: /* String to display as an arrow in non-window frames.
24827 See also `overlay-arrow-position'. */);
24828 Voverlay_arrow_string = build_string ("=>");
24829
24830 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24831 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24832 The symbols on this list are examined during redisplay to determine
24833 where to display overlay arrows. */);
24834 Voverlay_arrow_variable_list
24835 = Fcons (intern ("overlay-arrow-position"), Qnil);
24836
24837 DEFVAR_INT ("scroll-step", &scroll_step,
24838 doc: /* *The number of lines to try scrolling a window by when point moves out.
24839 If that fails to bring point back on frame, point is centered instead.
24840 If this is zero, point is always centered after it moves off frame.
24841 If you want scrolling to always be a line at a time, you should set
24842 `scroll-conservatively' to a large value rather than set this to 1. */);
24843
24844 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24845 doc: /* *Scroll up to this many lines, to bring point back on screen.
24846 If point moves off-screen, redisplay will scroll by up to
24847 `scroll-conservatively' lines in order to bring point just barely
24848 onto the screen again. If that cannot be done, then redisplay
24849 recenters point as usual.
24850
24851 A value of zero means always recenter point if it moves off screen. */);
24852 scroll_conservatively = 0;
24853
24854 DEFVAR_INT ("scroll-margin", &scroll_margin,
24855 doc: /* *Number of lines of margin at the top and bottom of a window.
24856 Recenter the window whenever point gets within this many lines
24857 of the top or bottom of the window. */);
24858 scroll_margin = 0;
24859
24860 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24861 doc: /* Pixels per inch value for non-window system displays.
24862 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24863 Vdisplay_pixels_per_inch = make_float (72.0);
24864
24865 #if GLYPH_DEBUG
24866 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24867 #endif
24868
24869 DEFVAR_LISP ("truncate-partial-width-windows",
24870 &Vtruncate_partial_width_windows,
24871 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24872 For an integer value, truncate lines in each window narrower than the
24873 full frame width, provided the window width is less than that integer;
24874 otherwise, respect the value of `truncate-lines'.
24875
24876 For any other non-nil value, truncate lines in all windows that do
24877 not span the full frame width.
24878
24879 A value of nil means to respect the value of `truncate-lines'.
24880
24881 If `word-wrap' is enabled, you might want to reduce this. */);
24882 Vtruncate_partial_width_windows = make_number (50);
24883
24884 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24885 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24886 Any other value means to use the appropriate face, `mode-line',
24887 `header-line', or `menu' respectively. */);
24888 mode_line_inverse_video = 1;
24889
24890 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24891 doc: /* *Maximum buffer size for which line number should be displayed.
24892 If the buffer is bigger than this, the line number does not appear
24893 in the mode line. A value of nil means no limit. */);
24894 Vline_number_display_limit = Qnil;
24895
24896 DEFVAR_INT ("line-number-display-limit-width",
24897 &line_number_display_limit_width,
24898 doc: /* *Maximum line width (in characters) for line number display.
24899 If the average length of the lines near point is bigger than this, then the
24900 line number may be omitted from the mode line. */);
24901 line_number_display_limit_width = 200;
24902
24903 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24904 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24905 highlight_nonselected_windows = 0;
24906
24907 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24908 doc: /* Non-nil if more than one frame is visible on this display.
24909 Minibuffer-only frames don't count, but iconified frames do.
24910 This variable is not guaranteed to be accurate except while processing
24911 `frame-title-format' and `icon-title-format'. */);
24912
24913 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24914 doc: /* Template for displaying the title bar of visible frames.
24915 \(Assuming the window manager supports this feature.)
24916
24917 This variable has the same structure as `mode-line-format', except that
24918 the %c and %l constructs are ignored. It is used only on frames for
24919 which no explicit name has been set \(see `modify-frame-parameters'). */);
24920
24921 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24922 doc: /* Template for displaying the title bar of an iconified frame.
24923 \(Assuming the window manager supports this feature.)
24924 This variable has the same structure as `mode-line-format' (which see),
24925 and is used only on frames for which no explicit name has been set
24926 \(see `modify-frame-parameters'). */);
24927 Vicon_title_format
24928 = Vframe_title_format
24929 = Fcons (intern ("multiple-frames"),
24930 Fcons (build_string ("%b"),
24931 Fcons (Fcons (empty_unibyte_string,
24932 Fcons (intern ("invocation-name"),
24933 Fcons (build_string ("@"),
24934 Fcons (intern ("system-name"),
24935 Qnil)))),
24936 Qnil)));
24937
24938 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24939 doc: /* Maximum number of lines to keep in the message log buffer.
24940 If nil, disable message logging. If t, log messages but don't truncate
24941 the buffer when it becomes large. */);
24942 Vmessage_log_max = make_number (100);
24943
24944 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24945 doc: /* Functions called before redisplay, if window sizes have changed.
24946 The value should be a list of functions that take one argument.
24947 Just before redisplay, for each frame, if any of its windows have changed
24948 size since the last redisplay, or have been split or deleted,
24949 all the functions in the list are called, with the frame as argument. */);
24950 Vwindow_size_change_functions = Qnil;
24951
24952 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24953 doc: /* List of functions to call before redisplaying a window with scrolling.
24954 Each function is called with two arguments, the window and its new
24955 display-start position. Note that these functions are also called by
24956 `set-window-buffer'. Also note that the value of `window-end' is not
24957 valid when these functions are called. */);
24958 Vwindow_scroll_functions = Qnil;
24959
24960 DEFVAR_LISP ("window-text-change-functions",
24961 &Vwindow_text_change_functions,
24962 doc: /* Functions to call in redisplay when text in the window might change. */);
24963 Vwindow_text_change_functions = Qnil;
24964
24965 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24966 doc: /* Functions called when redisplay of a window reaches the end trigger.
24967 Each function is called with two arguments, the window and the end trigger value.
24968 See `set-window-redisplay-end-trigger'. */);
24969 Vredisplay_end_trigger_functions = Qnil;
24970
24971 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24972 doc: /* *Non-nil means autoselect window with mouse pointer.
24973 If nil, do not autoselect windows.
24974 A positive number means delay autoselection by that many seconds: a
24975 window is autoselected only after the mouse has remained in that
24976 window for the duration of the delay.
24977 A negative number has a similar effect, but causes windows to be
24978 autoselected only after the mouse has stopped moving. \(Because of
24979 the way Emacs compares mouse events, you will occasionally wait twice
24980 that time before the window gets selected.\)
24981 Any other value means to autoselect window instantaneously when the
24982 mouse pointer enters it.
24983
24984 Autoselection selects the minibuffer only if it is active, and never
24985 unselects the minibuffer if it is active.
24986
24987 When customizing this variable make sure that the actual value of
24988 `focus-follows-mouse' matches the behavior of your window manager. */);
24989 Vmouse_autoselect_window = Qnil;
24990
24991 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24992 doc: /* *Non-nil means automatically resize tool-bars.
24993 This dynamically changes the tool-bar's height to the minimum height
24994 that is needed to make all tool-bar items visible.
24995 If value is `grow-only', the tool-bar's height is only increased
24996 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24997 Vauto_resize_tool_bars = Qt;
24998
24999 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25000 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25001 auto_raise_tool_bar_buttons_p = 1;
25002
25003 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25004 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25005 make_cursor_line_fully_visible_p = 1;
25006
25007 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25008 doc: /* *Border below tool-bar in pixels.
25009 If an integer, use it as the height of the border.
25010 If it is one of `internal-border-width' or `border-width', use the
25011 value of the corresponding frame parameter.
25012 Otherwise, no border is added below the tool-bar. */);
25013 Vtool_bar_border = Qinternal_border_width;
25014
25015 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25016 doc: /* *Margin around tool-bar buttons in pixels.
25017 If an integer, use that for both horizontal and vertical margins.
25018 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25019 HORZ specifying the horizontal margin, and VERT specifying the
25020 vertical margin. */);
25021 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25022
25023 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25024 doc: /* *Relief thickness of tool-bar buttons. */);
25025 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25026
25027 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25028 doc: /* List of functions to call to fontify regions of text.
25029 Each function is called with one argument POS. Functions must
25030 fontify a region starting at POS in the current buffer, and give
25031 fontified regions the property `fontified'. */);
25032 Vfontification_functions = Qnil;
25033 Fmake_variable_buffer_local (Qfontification_functions);
25034
25035 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25036 &unibyte_display_via_language_environment,
25037 doc: /* *Non-nil means display unibyte text according to language environment.
25038 Specifically this means that unibyte non-ASCII characters
25039 are displayed by converting them to the equivalent multibyte characters
25040 according to the current language environment. As a result, they are
25041 displayed according to the current fontset. */);
25042 unibyte_display_via_language_environment = 0;
25043
25044 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25045 doc: /* *Maximum height for resizing mini-windows.
25046 If a float, it specifies a fraction of the mini-window frame's height.
25047 If an integer, it specifies a number of lines. */);
25048 Vmax_mini_window_height = make_float (0.25);
25049
25050 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25051 doc: /* *How to resize mini-windows.
25052 A value of nil means don't automatically resize mini-windows.
25053 A value of t means resize them to fit the text displayed in them.
25054 A value of `grow-only', the default, means let mini-windows grow
25055 only, until their display becomes empty, at which point the windows
25056 go back to their normal size. */);
25057 Vresize_mini_windows = Qgrow_only;
25058
25059 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25060 doc: /* Alist specifying how to blink the cursor off.
25061 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25062 `cursor-type' frame-parameter or variable equals ON-STATE,
25063 comparing using `equal', Emacs uses OFF-STATE to specify
25064 how to blink it off. ON-STATE and OFF-STATE are values for
25065 the `cursor-type' frame parameter.
25066
25067 If a frame's ON-STATE has no entry in this list,
25068 the frame's other specifications determine how to blink the cursor off. */);
25069 Vblink_cursor_alist = Qnil;
25070
25071 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25072 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25073 automatic_hscrolling_p = 1;
25074 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25075 staticpro (&Qauto_hscroll_mode);
25076
25077 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25078 doc: /* *How many columns away from the window edge point is allowed to get
25079 before automatic hscrolling will horizontally scroll the window. */);
25080 hscroll_margin = 5;
25081
25082 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25083 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25084 When point is less than `hscroll-margin' columns from the window
25085 edge, automatic hscrolling will scroll the window by the amount of columns
25086 determined by this variable. If its value is a positive integer, scroll that
25087 many columns. If it's a positive floating-point number, it specifies the
25088 fraction of the window's width to scroll. If it's nil or zero, point will be
25089 centered horizontally after the scroll. Any other value, including negative
25090 numbers, are treated as if the value were zero.
25091
25092 Automatic hscrolling always moves point outside the scroll margin, so if
25093 point was more than scroll step columns inside the margin, the window will
25094 scroll more than the value given by the scroll step.
25095
25096 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25097 and `scroll-right' overrides this variable's effect. */);
25098 Vhscroll_step = make_number (0);
25099
25100 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25101 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25102 Bind this around calls to `message' to let it take effect. */);
25103 message_truncate_lines = 0;
25104
25105 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25106 doc: /* Normal hook run to update the menu bar definitions.
25107 Redisplay runs this hook before it redisplays the menu bar.
25108 This is used to update submenus such as Buffers,
25109 whose contents depend on various data. */);
25110 Vmenu_bar_update_hook = Qnil;
25111
25112 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25113 doc: /* Frame for which we are updating a menu.
25114 The enable predicate for a menu binding should check this variable. */);
25115 Vmenu_updating_frame = Qnil;
25116
25117 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25118 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25119 inhibit_menubar_update = 0;
25120
25121 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25122 doc: /* Prefix prepended to all continuation lines at display time.
25123 The value may be a string, an image, or a stretch-glyph; it is
25124 interpreted in the same way as the value of a `display' text property.
25125
25126 This variable is overridden by any `wrap-prefix' text or overlay
25127 property.
25128
25129 To add a prefix to non-continuation lines, use `line-prefix'. */);
25130 Vwrap_prefix = Qnil;
25131 staticpro (&Qwrap_prefix);
25132 Qwrap_prefix = intern ("wrap-prefix");
25133 Fmake_variable_buffer_local (Qwrap_prefix);
25134
25135 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25136 doc: /* Prefix prepended to all non-continuation lines at display time.
25137 The value may be a string, an image, or a stretch-glyph; it is
25138 interpreted in the same way as the value of a `display' text property.
25139
25140 This variable is overridden by any `line-prefix' text or overlay
25141 property.
25142
25143 To add a prefix to continuation lines, use `wrap-prefix'. */);
25144 Vline_prefix = Qnil;
25145 staticpro (&Qline_prefix);
25146 Qline_prefix = intern ("line-prefix");
25147 Fmake_variable_buffer_local (Qline_prefix);
25148
25149 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25150 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25151 inhibit_eval_during_redisplay = 0;
25152
25153 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25154 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25155 inhibit_free_realized_faces = 0;
25156
25157 #if GLYPH_DEBUG
25158 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25159 doc: /* Inhibit try_window_id display optimization. */);
25160 inhibit_try_window_id = 0;
25161
25162 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25163 doc: /* Inhibit try_window_reusing display optimization. */);
25164 inhibit_try_window_reusing = 0;
25165
25166 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25167 doc: /* Inhibit try_cursor_movement display optimization. */);
25168 inhibit_try_cursor_movement = 0;
25169 #endif /* GLYPH_DEBUG */
25170
25171 DEFVAR_INT ("overline-margin", &overline_margin,
25172 doc: /* *Space between overline and text, in pixels.
25173 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25174 margin to the caracter height. */);
25175 overline_margin = 2;
25176
25177 DEFVAR_INT ("underline-minimum-offset",
25178 &underline_minimum_offset,
25179 doc: /* Minimum distance between baseline and underline.
25180 This can improve legibility of underlined text at small font sizes,
25181 particularly when using variable `x-use-underline-position-properties'
25182 with fonts that specify an UNDERLINE_POSITION relatively close to the
25183 baseline. The default value is 1. */);
25184 underline_minimum_offset = 1;
25185
25186 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25187 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25188 display_hourglass_p = 1;
25189
25190 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25191 doc: /* *Seconds to wait before displaying an hourglass pointer.
25192 Value must be an integer or float. */);
25193 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25194
25195 hourglass_atimer = NULL;
25196 hourglass_shown_p = 0;
25197 }
25198
25199
25200 /* Initialize this module when Emacs starts. */
25201
25202 void
25203 init_xdisp ()
25204 {
25205 Lisp_Object root_window;
25206 struct window *mini_w;
25207
25208 current_header_line_height = current_mode_line_height = -1;
25209
25210 CHARPOS (this_line_start_pos) = 0;
25211
25212 mini_w = XWINDOW (minibuf_window);
25213 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25214
25215 if (!noninteractive)
25216 {
25217 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25218 int i;
25219
25220 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25221 set_window_height (root_window,
25222 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25223 0);
25224 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25225 set_window_height (minibuf_window, 1, 0);
25226
25227 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25228 mini_w->total_cols = make_number (FRAME_COLS (f));
25229
25230 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25231 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25232 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25233
25234 /* The default ellipsis glyphs `...'. */
25235 for (i = 0; i < 3; ++i)
25236 default_invis_vector[i] = make_number ('.');
25237 }
25238
25239 {
25240 /* Allocate the buffer for frame titles.
25241 Also used for `format-mode-line'. */
25242 int size = 100;
25243 mode_line_noprop_buf = (char *) xmalloc (size);
25244 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25245 mode_line_noprop_ptr = mode_line_noprop_buf;
25246 mode_line_target = MODE_LINE_DISPLAY;
25247 }
25248
25249 help_echo_showing_p = 0;
25250 }
25251
25252 /* Since w32 does not support atimers, it defines its own implementation of
25253 the following three functions in w32fns.c. */
25254 #ifndef WINDOWSNT
25255
25256 /* Platform-independent portion of hourglass implementation. */
25257
25258 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25259 int
25260 hourglass_started ()
25261 {
25262 return hourglass_shown_p || hourglass_atimer != NULL;
25263 }
25264
25265 /* Cancel a currently active hourglass timer, and start a new one. */
25266 void
25267 start_hourglass ()
25268 {
25269 #if defined (HAVE_WINDOW_SYSTEM)
25270 EMACS_TIME delay;
25271 int secs, usecs = 0;
25272
25273 cancel_hourglass ();
25274
25275 if (INTEGERP (Vhourglass_delay)
25276 && XINT (Vhourglass_delay) > 0)
25277 secs = XFASTINT (Vhourglass_delay);
25278 else if (FLOATP (Vhourglass_delay)
25279 && XFLOAT_DATA (Vhourglass_delay) > 0)
25280 {
25281 Lisp_Object tem;
25282 tem = Ftruncate (Vhourglass_delay, Qnil);
25283 secs = XFASTINT (tem);
25284 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25285 }
25286 else
25287 secs = DEFAULT_HOURGLASS_DELAY;
25288
25289 EMACS_SET_SECS_USECS (delay, secs, usecs);
25290 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25291 show_hourglass, NULL);
25292 #endif
25293 }
25294
25295
25296 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25297 shown. */
25298 void
25299 cancel_hourglass ()
25300 {
25301 #if defined (HAVE_WINDOW_SYSTEM)
25302 if (hourglass_atimer)
25303 {
25304 cancel_atimer (hourglass_atimer);
25305 hourglass_atimer = NULL;
25306 }
25307
25308 if (hourglass_shown_p)
25309 hide_hourglass ();
25310 #endif
25311 }
25312 #endif /* ! WINDOWSNT */
25313
25314 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25315 (do not change this comment) */