]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(detect_coding_sjis): Handle shift_jis-2004 correctly.
[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 }
5171 it->end_charpos = p->end_charpos;
5172 it->string_nchars = p->string_nchars;
5173 it->area = p->area;
5174 it->multibyte_p = p->multibyte_p;
5175 it->avoid_cursor_p = p->avoid_cursor_p;
5176 it->space_width = p->space_width;
5177 it->font_height = p->font_height;
5178 it->voffset = p->voffset;
5179 it->string_from_display_prop_p = p->string_from_display_prop_p;
5180 it->line_wrap = p->line_wrap;
5181 }
5182
5183
5184 \f
5185 /***********************************************************************
5186 Moving over lines
5187 ***********************************************************************/
5188
5189 /* Set IT's current position to the previous line start. */
5190
5191 static void
5192 back_to_previous_line_start (it)
5193 struct it *it;
5194 {
5195 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5196 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5197 }
5198
5199
5200 /* Move IT to the next line start.
5201
5202 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5203 we skipped over part of the text (as opposed to moving the iterator
5204 continuously over the text). Otherwise, don't change the value
5205 of *SKIPPED_P.
5206
5207 Newlines may come from buffer text, overlay strings, or strings
5208 displayed via the `display' property. That's the reason we can't
5209 simply use find_next_newline_no_quit.
5210
5211 Note that this function may not skip over invisible text that is so
5212 because of text properties and immediately follows a newline. If
5213 it would, function reseat_at_next_visible_line_start, when called
5214 from set_iterator_to_next, would effectively make invisible
5215 characters following a newline part of the wrong glyph row, which
5216 leads to wrong cursor motion. */
5217
5218 static int
5219 forward_to_next_line_start (it, skipped_p)
5220 struct it *it;
5221 int *skipped_p;
5222 {
5223 int old_selective, newline_found_p, n;
5224 const int MAX_NEWLINE_DISTANCE = 500;
5225
5226 /* If already on a newline, just consume it to avoid unintended
5227 skipping over invisible text below. */
5228 if (it->what == IT_CHARACTER
5229 && it->c == '\n'
5230 && CHARPOS (it->position) == IT_CHARPOS (*it))
5231 {
5232 set_iterator_to_next (it, 0);
5233 it->c = 0;
5234 return 1;
5235 }
5236
5237 /* Don't handle selective display in the following. It's (a)
5238 unnecessary because it's done by the caller, and (b) leads to an
5239 infinite recursion because next_element_from_ellipsis indirectly
5240 calls this function. */
5241 old_selective = it->selective;
5242 it->selective = 0;
5243
5244 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5245 from buffer text. */
5246 for (n = newline_found_p = 0;
5247 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5248 n += STRINGP (it->string) ? 0 : 1)
5249 {
5250 if (!get_next_display_element (it))
5251 return 0;
5252 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5253 set_iterator_to_next (it, 0);
5254 }
5255
5256 /* If we didn't find a newline near enough, see if we can use a
5257 short-cut. */
5258 if (!newline_found_p)
5259 {
5260 int start = IT_CHARPOS (*it);
5261 int limit = find_next_newline_no_quit (start, 1);
5262 Lisp_Object pos;
5263
5264 xassert (!STRINGP (it->string));
5265
5266 /* If there isn't any `display' property in sight, and no
5267 overlays, we can just use the position of the newline in
5268 buffer text. */
5269 if (it->stop_charpos >= limit
5270 || ((pos = Fnext_single_property_change (make_number (start),
5271 Qdisplay,
5272 Qnil, make_number (limit)),
5273 NILP (pos))
5274 && next_overlay_change (start) == ZV))
5275 {
5276 IT_CHARPOS (*it) = limit;
5277 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5278 *skipped_p = newline_found_p = 1;
5279 }
5280 else
5281 {
5282 while (get_next_display_element (it)
5283 && !newline_found_p)
5284 {
5285 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5286 set_iterator_to_next (it, 0);
5287 }
5288 }
5289 }
5290
5291 it->selective = old_selective;
5292 return newline_found_p;
5293 }
5294
5295
5296 /* Set IT's current position to the previous visible line start. Skip
5297 invisible text that is so either due to text properties or due to
5298 selective display. Caution: this does not change IT->current_x and
5299 IT->hpos. */
5300
5301 static void
5302 back_to_previous_visible_line_start (it)
5303 struct it *it;
5304 {
5305 while (IT_CHARPOS (*it) > BEGV)
5306 {
5307 back_to_previous_line_start (it);
5308
5309 if (IT_CHARPOS (*it) <= BEGV)
5310 break;
5311
5312 /* If selective > 0, then lines indented more than that values
5313 are invisible. */
5314 if (it->selective > 0
5315 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5316 (double) it->selective)) /* iftc */
5317 continue;
5318
5319 /* Check the newline before point for invisibility. */
5320 {
5321 Lisp_Object prop;
5322 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5323 Qinvisible, it->window);
5324 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5325 continue;
5326 }
5327
5328 if (IT_CHARPOS (*it) <= BEGV)
5329 break;
5330
5331 {
5332 struct it it2;
5333 int pos;
5334 EMACS_INT beg, end;
5335 Lisp_Object val, overlay;
5336
5337 /* If newline is part of a composition, continue from start of composition */
5338 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5339 && beg < IT_CHARPOS (*it))
5340 goto replaced;
5341
5342 /* If newline is replaced by a display property, find start of overlay
5343 or interval and continue search from that point. */
5344 it2 = *it;
5345 pos = --IT_CHARPOS (it2);
5346 --IT_BYTEPOS (it2);
5347 it2.sp = 0;
5348 it2.string_from_display_prop_p = 0;
5349 if (handle_display_prop (&it2) == HANDLED_RETURN
5350 && !NILP (val = get_char_property_and_overlay
5351 (make_number (pos), Qdisplay, Qnil, &overlay))
5352 && (OVERLAYP (overlay)
5353 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5354 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5355 goto replaced;
5356
5357 /* Newline is not replaced by anything -- so we are done. */
5358 break;
5359
5360 replaced:
5361 if (beg < BEGV)
5362 beg = BEGV;
5363 IT_CHARPOS (*it) = beg;
5364 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5365 }
5366 }
5367
5368 it->continuation_lines_width = 0;
5369
5370 xassert (IT_CHARPOS (*it) >= BEGV);
5371 xassert (IT_CHARPOS (*it) == BEGV
5372 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5373 CHECK_IT (it);
5374 }
5375
5376
5377 /* Reseat iterator IT at the previous visible line start. Skip
5378 invisible text that is so either due to text properties or due to
5379 selective display. At the end, update IT's overlay information,
5380 face information etc. */
5381
5382 void
5383 reseat_at_previous_visible_line_start (it)
5384 struct it *it;
5385 {
5386 back_to_previous_visible_line_start (it);
5387 reseat (it, it->current.pos, 1);
5388 CHECK_IT (it);
5389 }
5390
5391
5392 /* Reseat iterator IT on the next visible line start in the current
5393 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5394 preceding the line start. Skip over invisible text that is so
5395 because of selective display. Compute faces, overlays etc at the
5396 new position. Note that this function does not skip over text that
5397 is invisible because of text properties. */
5398
5399 static void
5400 reseat_at_next_visible_line_start (it, on_newline_p)
5401 struct it *it;
5402 int on_newline_p;
5403 {
5404 int newline_found_p, skipped_p = 0;
5405
5406 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5407
5408 /* Skip over lines that are invisible because they are indented
5409 more than the value of IT->selective. */
5410 if (it->selective > 0)
5411 while (IT_CHARPOS (*it) < ZV
5412 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5413 (double) it->selective)) /* iftc */
5414 {
5415 xassert (IT_BYTEPOS (*it) == BEGV
5416 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5417 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5418 }
5419
5420 /* Position on the newline if that's what's requested. */
5421 if (on_newline_p && newline_found_p)
5422 {
5423 if (STRINGP (it->string))
5424 {
5425 if (IT_STRING_CHARPOS (*it) > 0)
5426 {
5427 --IT_STRING_CHARPOS (*it);
5428 --IT_STRING_BYTEPOS (*it);
5429 }
5430 }
5431 else if (IT_CHARPOS (*it) > BEGV)
5432 {
5433 --IT_CHARPOS (*it);
5434 --IT_BYTEPOS (*it);
5435 reseat (it, it->current.pos, 0);
5436 }
5437 }
5438 else if (skipped_p)
5439 reseat (it, it->current.pos, 0);
5440
5441 CHECK_IT (it);
5442 }
5443
5444
5445 \f
5446 /***********************************************************************
5447 Changing an iterator's position
5448 ***********************************************************************/
5449
5450 /* Change IT's current position to POS in current_buffer. If FORCE_P
5451 is non-zero, always check for text properties at the new position.
5452 Otherwise, text properties are only looked up if POS >=
5453 IT->check_charpos of a property. */
5454
5455 static void
5456 reseat (it, pos, force_p)
5457 struct it *it;
5458 struct text_pos pos;
5459 int force_p;
5460 {
5461 int original_pos = IT_CHARPOS (*it);
5462
5463 reseat_1 (it, pos, 0);
5464
5465 /* Determine where to check text properties. Avoid doing it
5466 where possible because text property lookup is very expensive. */
5467 if (force_p
5468 || CHARPOS (pos) > it->stop_charpos
5469 || CHARPOS (pos) < original_pos)
5470 handle_stop (it);
5471
5472 CHECK_IT (it);
5473 }
5474
5475
5476 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5477 IT->stop_pos to POS, also. */
5478
5479 static void
5480 reseat_1 (it, pos, set_stop_p)
5481 struct it *it;
5482 struct text_pos pos;
5483 int set_stop_p;
5484 {
5485 /* Don't call this function when scanning a C string. */
5486 xassert (it->s == NULL);
5487
5488 /* POS must be a reasonable value. */
5489 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5490
5491 it->current.pos = it->position = pos;
5492 it->end_charpos = ZV;
5493 it->dpvec = NULL;
5494 it->current.dpvec_index = -1;
5495 it->current.overlay_string_index = -1;
5496 IT_STRING_CHARPOS (*it) = -1;
5497 IT_STRING_BYTEPOS (*it) = -1;
5498 it->string = Qnil;
5499 it->string_from_display_prop_p = 0;
5500 it->method = GET_FROM_BUFFER;
5501 it->object = it->w->buffer;
5502 it->area = TEXT_AREA;
5503 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5504 it->sp = 0;
5505 it->string_from_display_prop_p = 0;
5506 it->face_before_selective_p = 0;
5507
5508 if (set_stop_p)
5509 it->stop_charpos = CHARPOS (pos);
5510 }
5511
5512
5513 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5514 If S is non-null, it is a C string to iterate over. Otherwise,
5515 STRING gives a Lisp string to iterate over.
5516
5517 If PRECISION > 0, don't return more then PRECISION number of
5518 characters from the string.
5519
5520 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5521 characters have been returned. FIELD_WIDTH < 0 means an infinite
5522 field width.
5523
5524 MULTIBYTE = 0 means disable processing of multibyte characters,
5525 MULTIBYTE > 0 means enable it,
5526 MULTIBYTE < 0 means use IT->multibyte_p.
5527
5528 IT must be initialized via a prior call to init_iterator before
5529 calling this function. */
5530
5531 static void
5532 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5533 struct it *it;
5534 unsigned char *s;
5535 Lisp_Object string;
5536 int charpos;
5537 int precision, field_width, multibyte;
5538 {
5539 /* No region in strings. */
5540 it->region_beg_charpos = it->region_end_charpos = -1;
5541
5542 /* No text property checks performed by default, but see below. */
5543 it->stop_charpos = -1;
5544
5545 /* Set iterator position and end position. */
5546 bzero (&it->current, sizeof it->current);
5547 it->current.overlay_string_index = -1;
5548 it->current.dpvec_index = -1;
5549 xassert (charpos >= 0);
5550
5551 /* If STRING is specified, use its multibyteness, otherwise use the
5552 setting of MULTIBYTE, if specified. */
5553 if (multibyte >= 0)
5554 it->multibyte_p = multibyte > 0;
5555
5556 if (s == NULL)
5557 {
5558 xassert (STRINGP (string));
5559 it->string = string;
5560 it->s = NULL;
5561 it->end_charpos = it->string_nchars = SCHARS (string);
5562 it->method = GET_FROM_STRING;
5563 it->current.string_pos = string_pos (charpos, string);
5564 }
5565 else
5566 {
5567 it->s = s;
5568 it->string = Qnil;
5569
5570 /* Note that we use IT->current.pos, not it->current.string_pos,
5571 for displaying C strings. */
5572 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5573 if (it->multibyte_p)
5574 {
5575 it->current.pos = c_string_pos (charpos, s, 1);
5576 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5577 }
5578 else
5579 {
5580 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5581 it->end_charpos = it->string_nchars = strlen (s);
5582 }
5583
5584 it->method = GET_FROM_C_STRING;
5585 }
5586
5587 /* PRECISION > 0 means don't return more than PRECISION characters
5588 from the string. */
5589 if (precision > 0 && it->end_charpos - charpos > precision)
5590 it->end_charpos = it->string_nchars = charpos + precision;
5591
5592 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5593 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5594 FIELD_WIDTH < 0 means infinite field width. This is useful for
5595 padding with `-' at the end of a mode line. */
5596 if (field_width < 0)
5597 field_width = INFINITY;
5598 if (field_width > it->end_charpos - charpos)
5599 it->end_charpos = charpos + field_width;
5600
5601 /* Use the standard display table for displaying strings. */
5602 if (DISP_TABLE_P (Vstandard_display_table))
5603 it->dp = XCHAR_TABLE (Vstandard_display_table);
5604
5605 it->stop_charpos = charpos;
5606 CHECK_IT (it);
5607 }
5608
5609
5610 \f
5611 /***********************************************************************
5612 Iteration
5613 ***********************************************************************/
5614
5615 /* Map enum it_method value to corresponding next_element_from_* function. */
5616
5617 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5618 {
5619 next_element_from_buffer,
5620 next_element_from_display_vector,
5621 next_element_from_string,
5622 next_element_from_c_string,
5623 next_element_from_image,
5624 next_element_from_stretch
5625 };
5626
5627 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5628
5629
5630 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5631 (possibly with the following characters). */
5632
5633 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5634 ((IT)->cmp_it.id >= 0 \
5635 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5636 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5637 (IT)->end_charpos, (IT)->w, \
5638 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5639 (IT)->string)))
5640
5641
5642 /* Load IT's display element fields with information about the next
5643 display element from the current position of IT. Value is zero if
5644 end of buffer (or C string) is reached. */
5645
5646 static struct frame *last_escape_glyph_frame = NULL;
5647 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5648 static int last_escape_glyph_merged_face_id = 0;
5649
5650 int
5651 get_next_display_element (it)
5652 struct it *it;
5653 {
5654 /* Non-zero means that we found a display element. Zero means that
5655 we hit the end of what we iterate over. Performance note: the
5656 function pointer `method' used here turns out to be faster than
5657 using a sequence of if-statements. */
5658 int success_p;
5659
5660 get_next:
5661 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5662
5663 if (it->what == IT_CHARACTER)
5664 {
5665 /* Map via display table or translate control characters.
5666 IT->c, IT->len etc. have been set to the next character by
5667 the function call above. If we have a display table, and it
5668 contains an entry for IT->c, translate it. Don't do this if
5669 IT->c itself comes from a display table, otherwise we could
5670 end up in an infinite recursion. (An alternative could be to
5671 count the recursion depth of this function and signal an
5672 error when a certain maximum depth is reached.) Is it worth
5673 it? */
5674 if (success_p && it->dpvec == NULL)
5675 {
5676 Lisp_Object dv;
5677 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5678
5679 if (it->dp
5680 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5681 VECTORP (dv)))
5682 {
5683 struct Lisp_Vector *v = XVECTOR (dv);
5684
5685 /* Return the first character from the display table
5686 entry, if not empty. If empty, don't display the
5687 current character. */
5688 if (v->size)
5689 {
5690 it->dpvec_char_len = it->len;
5691 it->dpvec = v->contents;
5692 it->dpend = v->contents + v->size;
5693 it->current.dpvec_index = 0;
5694 it->dpvec_face_id = -1;
5695 it->saved_face_id = it->face_id;
5696 it->method = GET_FROM_DISPLAY_VECTOR;
5697 it->ellipsis_p = 0;
5698 }
5699 else
5700 {
5701 set_iterator_to_next (it, 0);
5702 }
5703 goto get_next;
5704 }
5705
5706 /* Translate control characters into `\003' or `^C' form.
5707 Control characters coming from a display table entry are
5708 currently not translated because we use IT->dpvec to hold
5709 the translation. This could easily be changed but I
5710 don't believe that it is worth doing.
5711
5712 If it->multibyte_p is nonzero, non-printable non-ASCII
5713 characters are also translated to octal form.
5714
5715 If it->multibyte_p is zero, eight-bit characters that
5716 don't have corresponding multibyte char code are also
5717 translated to octal form. */
5718 else if ((it->c < ' '
5719 ? (it->area != TEXT_AREA
5720 /* In mode line, treat \n, \t like other crl chars. */
5721 || (it->c != '\t'
5722 && it->glyph_row
5723 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5724 || (it->c != '\n' && it->c != '\t'))
5725 : (it->multibyte_p
5726 ? (!CHAR_PRINTABLE_P (it->c)
5727 || (!NILP (Vnobreak_char_display)
5728 && (it->c == 0xA0 /* NO-BREAK SPACE */
5729 || it->c == 0xAD /* SOFT HYPHEN */)))
5730 : (it->c >= 127
5731 && (! unibyte_display_via_language_environment
5732 || (DECODE_CHAR (unibyte, it->c) <= 0xA0))))))
5733 {
5734 /* IT->c is a control character which must be displayed
5735 either as '\003' or as `^C' where the '\\' and '^'
5736 can be defined in the display table. Fill
5737 IT->ctl_chars with glyphs for what we have to
5738 display. Then, set IT->dpvec to these glyphs. */
5739 Lisp_Object gc;
5740 int ctl_len;
5741 int face_id, lface_id = 0 ;
5742 int escape_glyph;
5743
5744 /* Handle control characters with ^. */
5745
5746 if (it->c < 128 && it->ctl_arrow_p)
5747 {
5748 int g;
5749
5750 g = '^'; /* default glyph for Control */
5751 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5752 if (it->dp
5753 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5754 && GLYPH_CODE_CHAR_VALID_P (gc))
5755 {
5756 g = GLYPH_CODE_CHAR (gc);
5757 lface_id = GLYPH_CODE_FACE (gc);
5758 }
5759 if (lface_id)
5760 {
5761 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5762 }
5763 else if (it->f == last_escape_glyph_frame
5764 && it->face_id == last_escape_glyph_face_id)
5765 {
5766 face_id = last_escape_glyph_merged_face_id;
5767 }
5768 else
5769 {
5770 /* Merge the escape-glyph face into the current face. */
5771 face_id = merge_faces (it->f, Qescape_glyph, 0,
5772 it->face_id);
5773 last_escape_glyph_frame = it->f;
5774 last_escape_glyph_face_id = it->face_id;
5775 last_escape_glyph_merged_face_id = face_id;
5776 }
5777
5778 XSETINT (it->ctl_chars[0], g);
5779 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5780 ctl_len = 2;
5781 goto display_control;
5782 }
5783
5784 /* Handle non-break space in the mode where it only gets
5785 highlighting. */
5786
5787 if (EQ (Vnobreak_char_display, Qt)
5788 && it->c == 0xA0)
5789 {
5790 /* Merge the no-break-space face into the current face. */
5791 face_id = merge_faces (it->f, Qnobreak_space, 0,
5792 it->face_id);
5793
5794 it->c = ' ';
5795 XSETINT (it->ctl_chars[0], ' ');
5796 ctl_len = 1;
5797 goto display_control;
5798 }
5799
5800 /* Handle sequences that start with the "escape glyph". */
5801
5802 /* the default escape glyph is \. */
5803 escape_glyph = '\\';
5804
5805 if (it->dp
5806 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5807 && GLYPH_CODE_CHAR_VALID_P (gc))
5808 {
5809 escape_glyph = GLYPH_CODE_CHAR (gc);
5810 lface_id = GLYPH_CODE_FACE (gc);
5811 }
5812 if (lface_id)
5813 {
5814 /* The display table specified a face.
5815 Merge it into face_id and also into escape_glyph. */
5816 face_id = merge_faces (it->f, Qt, lface_id,
5817 it->face_id);
5818 }
5819 else if (it->f == last_escape_glyph_frame
5820 && it->face_id == last_escape_glyph_face_id)
5821 {
5822 face_id = last_escape_glyph_merged_face_id;
5823 }
5824 else
5825 {
5826 /* Merge the escape-glyph face into the current face. */
5827 face_id = merge_faces (it->f, Qescape_glyph, 0,
5828 it->face_id);
5829 last_escape_glyph_frame = it->f;
5830 last_escape_glyph_face_id = it->face_id;
5831 last_escape_glyph_merged_face_id = face_id;
5832 }
5833
5834 /* Handle soft hyphens in the mode where they only get
5835 highlighting. */
5836
5837 if (EQ (Vnobreak_char_display, Qt)
5838 && it->c == 0xAD)
5839 {
5840 it->c = '-';
5841 XSETINT (it->ctl_chars[0], '-');
5842 ctl_len = 1;
5843 goto display_control;
5844 }
5845
5846 /* Handle non-break space and soft hyphen
5847 with the escape glyph. */
5848
5849 if (it->c == 0xA0 || it->c == 0xAD)
5850 {
5851 XSETINT (it->ctl_chars[0], escape_glyph);
5852 it->c = (it->c == 0xA0 ? ' ' : '-');
5853 XSETINT (it->ctl_chars[1], it->c);
5854 ctl_len = 2;
5855 goto display_control;
5856 }
5857
5858 {
5859 unsigned char str[MAX_MULTIBYTE_LENGTH];
5860 int len;
5861 int i;
5862
5863 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5864 if (CHAR_BYTE8_P (it->c))
5865 {
5866 str[0] = CHAR_TO_BYTE8 (it->c);
5867 len = 1;
5868 }
5869 else if (it->c < 256)
5870 {
5871 str[0] = it->c;
5872 len = 1;
5873 }
5874 else
5875 {
5876 /* It's an invalid character, which shouldn't
5877 happen actually, but due to bugs it may
5878 happen. Let's print the char as is, there's
5879 not much meaningful we can do with it. */
5880 str[0] = it->c;
5881 str[1] = it->c >> 8;
5882 str[2] = it->c >> 16;
5883 str[3] = it->c >> 24;
5884 len = 4;
5885 }
5886
5887 for (i = 0; i < len; i++)
5888 {
5889 int g;
5890 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5891 /* Insert three more glyphs into IT->ctl_chars for
5892 the octal display of the character. */
5893 g = ((str[i] >> 6) & 7) + '0';
5894 XSETINT (it->ctl_chars[i * 4 + 1], g);
5895 g = ((str[i] >> 3) & 7) + '0';
5896 XSETINT (it->ctl_chars[i * 4 + 2], g);
5897 g = (str[i] & 7) + '0';
5898 XSETINT (it->ctl_chars[i * 4 + 3], g);
5899 }
5900 ctl_len = len * 4;
5901 }
5902
5903 display_control:
5904 /* Set up IT->dpvec and return first character from it. */
5905 it->dpvec_char_len = it->len;
5906 it->dpvec = it->ctl_chars;
5907 it->dpend = it->dpvec + ctl_len;
5908 it->current.dpvec_index = 0;
5909 it->dpvec_face_id = face_id;
5910 it->saved_face_id = it->face_id;
5911 it->method = GET_FROM_DISPLAY_VECTOR;
5912 it->ellipsis_p = 0;
5913 goto get_next;
5914 }
5915 }
5916 }
5917
5918 #ifdef HAVE_WINDOW_SYSTEM
5919 /* Adjust face id for a multibyte character. There are no multibyte
5920 character in unibyte text. */
5921 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5922 && it->multibyte_p
5923 && success_p
5924 && FRAME_WINDOW_P (it->f))
5925 {
5926 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5927
5928 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5929 {
5930 /* Automatic composition with glyph-string. */
5931 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5932
5933 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5934 }
5935 else
5936 {
5937 int pos = (it->s ? -1
5938 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5939 : IT_CHARPOS (*it));
5940
5941 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5942 }
5943 }
5944 #endif
5945
5946 /* Is this character the last one of a run of characters with
5947 box? If yes, set IT->end_of_box_run_p to 1. */
5948 if (it->face_box_p
5949 && it->s == NULL)
5950 {
5951 if (it->method == GET_FROM_STRING && it->sp)
5952 {
5953 int face_id = underlying_face_id (it);
5954 struct face *face = FACE_FROM_ID (it->f, face_id);
5955
5956 if (face)
5957 {
5958 if (face->box == FACE_NO_BOX)
5959 {
5960 /* If the box comes from face properties in a
5961 display string, check faces in that string. */
5962 int string_face_id = face_after_it_pos (it);
5963 it->end_of_box_run_p
5964 = (FACE_FROM_ID (it->f, string_face_id)->box
5965 == FACE_NO_BOX);
5966 }
5967 /* Otherwise, the box comes from the underlying face.
5968 If this is the last string character displayed, check
5969 the next buffer location. */
5970 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5971 && (it->current.overlay_string_index
5972 == it->n_overlay_strings - 1))
5973 {
5974 EMACS_INT ignore;
5975 int next_face_id;
5976 struct text_pos pos = it->current.pos;
5977 INC_TEXT_POS (pos, it->multibyte_p);
5978
5979 next_face_id = face_at_buffer_position
5980 (it->w, CHARPOS (pos), it->region_beg_charpos,
5981 it->region_end_charpos, &ignore,
5982 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5983 -1);
5984 it->end_of_box_run_p
5985 = (FACE_FROM_ID (it->f, next_face_id)->box
5986 == FACE_NO_BOX);
5987 }
5988 }
5989 }
5990 else
5991 {
5992 int face_id = face_after_it_pos (it);
5993 it->end_of_box_run_p
5994 = (face_id != it->face_id
5995 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5996 }
5997 }
5998
5999 /* Value is 0 if end of buffer or string reached. */
6000 return success_p;
6001 }
6002
6003
6004 /* Move IT to the next display element.
6005
6006 RESEAT_P non-zero means if called on a newline in buffer text,
6007 skip to the next visible line start.
6008
6009 Functions get_next_display_element and set_iterator_to_next are
6010 separate because I find this arrangement easier to handle than a
6011 get_next_display_element function that also increments IT's
6012 position. The way it is we can first look at an iterator's current
6013 display element, decide whether it fits on a line, and if it does,
6014 increment the iterator position. The other way around we probably
6015 would either need a flag indicating whether the iterator has to be
6016 incremented the next time, or we would have to implement a
6017 decrement position function which would not be easy to write. */
6018
6019 void
6020 set_iterator_to_next (it, reseat_p)
6021 struct it *it;
6022 int reseat_p;
6023 {
6024 /* Reset flags indicating start and end of a sequence of characters
6025 with box. Reset them at the start of this function because
6026 moving the iterator to a new position might set them. */
6027 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6028
6029 switch (it->method)
6030 {
6031 case GET_FROM_BUFFER:
6032 /* The current display element of IT is a character from
6033 current_buffer. Advance in the buffer, and maybe skip over
6034 invisible lines that are so because of selective display. */
6035 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6036 reseat_at_next_visible_line_start (it, 0);
6037 else if (it->cmp_it.id >= 0)
6038 {
6039 IT_CHARPOS (*it) += it->cmp_it.nchars;
6040 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6041 if (it->cmp_it.to < it->cmp_it.nglyphs)
6042 it->cmp_it.from = it->cmp_it.to;
6043 else
6044 {
6045 it->cmp_it.id = -1;
6046 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6047 IT_BYTEPOS (*it), it->stop_charpos,
6048 Qnil);
6049 }
6050 }
6051 else
6052 {
6053 xassert (it->len != 0);
6054 IT_BYTEPOS (*it) += it->len;
6055 IT_CHARPOS (*it) += 1;
6056 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6057 }
6058 break;
6059
6060 case GET_FROM_C_STRING:
6061 /* Current display element of IT is from a C string. */
6062 IT_BYTEPOS (*it) += it->len;
6063 IT_CHARPOS (*it) += 1;
6064 break;
6065
6066 case GET_FROM_DISPLAY_VECTOR:
6067 /* Current display element of IT is from a display table entry.
6068 Advance in the display table definition. Reset it to null if
6069 end reached, and continue with characters from buffers/
6070 strings. */
6071 ++it->current.dpvec_index;
6072
6073 /* Restore face of the iterator to what they were before the
6074 display vector entry (these entries may contain faces). */
6075 it->face_id = it->saved_face_id;
6076
6077 if (it->dpvec + it->current.dpvec_index == it->dpend)
6078 {
6079 int recheck_faces = it->ellipsis_p;
6080
6081 if (it->s)
6082 it->method = GET_FROM_C_STRING;
6083 else if (STRINGP (it->string))
6084 it->method = GET_FROM_STRING;
6085 else
6086 {
6087 it->method = GET_FROM_BUFFER;
6088 it->object = it->w->buffer;
6089 }
6090
6091 it->dpvec = NULL;
6092 it->current.dpvec_index = -1;
6093
6094 /* Skip over characters which were displayed via IT->dpvec. */
6095 if (it->dpvec_char_len < 0)
6096 reseat_at_next_visible_line_start (it, 1);
6097 else if (it->dpvec_char_len > 0)
6098 {
6099 if (it->method == GET_FROM_STRING
6100 && it->n_overlay_strings > 0)
6101 it->ignore_overlay_strings_at_pos_p = 1;
6102 it->len = it->dpvec_char_len;
6103 set_iterator_to_next (it, reseat_p);
6104 }
6105
6106 /* Maybe recheck faces after display vector */
6107 if (recheck_faces)
6108 it->stop_charpos = IT_CHARPOS (*it);
6109 }
6110 break;
6111
6112 case GET_FROM_STRING:
6113 /* Current display element is a character from a Lisp string. */
6114 xassert (it->s == NULL && STRINGP (it->string));
6115 if (it->cmp_it.id >= 0)
6116 {
6117 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6118 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6119 if (it->cmp_it.to < it->cmp_it.nglyphs)
6120 it->cmp_it.from = it->cmp_it.to;
6121 else
6122 {
6123 it->cmp_it.id = -1;
6124 composition_compute_stop_pos (&it->cmp_it,
6125 IT_STRING_CHARPOS (*it),
6126 IT_STRING_BYTEPOS (*it),
6127 it->stop_charpos, it->string);
6128 }
6129 }
6130 else
6131 {
6132 IT_STRING_BYTEPOS (*it) += it->len;
6133 IT_STRING_CHARPOS (*it) += 1;
6134 }
6135
6136 consider_string_end:
6137
6138 if (it->current.overlay_string_index >= 0)
6139 {
6140 /* IT->string is an overlay string. Advance to the
6141 next, if there is one. */
6142 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6143 {
6144 it->ellipsis_p = 0;
6145 next_overlay_string (it);
6146 if (it->ellipsis_p)
6147 setup_for_ellipsis (it, 0);
6148 }
6149 }
6150 else
6151 {
6152 /* IT->string is not an overlay string. If we reached
6153 its end, and there is something on IT->stack, proceed
6154 with what is on the stack. This can be either another
6155 string, this time an overlay string, or a buffer. */
6156 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6157 && it->sp > 0)
6158 {
6159 pop_it (it);
6160 if (it->method == GET_FROM_STRING)
6161 goto consider_string_end;
6162 }
6163 }
6164 break;
6165
6166 case GET_FROM_IMAGE:
6167 case GET_FROM_STRETCH:
6168 /* The position etc with which we have to proceed are on
6169 the stack. The position may be at the end of a string,
6170 if the `display' property takes up the whole string. */
6171 xassert (it->sp > 0);
6172 pop_it (it);
6173 if (it->method == GET_FROM_STRING)
6174 goto consider_string_end;
6175 break;
6176
6177 default:
6178 /* There are no other methods defined, so this should be a bug. */
6179 abort ();
6180 }
6181
6182 xassert (it->method != GET_FROM_STRING
6183 || (STRINGP (it->string)
6184 && IT_STRING_CHARPOS (*it) >= 0));
6185 }
6186
6187 /* Load IT's display element fields with information about the next
6188 display element which comes from a display table entry or from the
6189 result of translating a control character to one of the forms `^C'
6190 or `\003'.
6191
6192 IT->dpvec holds the glyphs to return as characters.
6193 IT->saved_face_id holds the face id before the display vector--
6194 it is restored into IT->face_idin set_iterator_to_next. */
6195
6196 static int
6197 next_element_from_display_vector (it)
6198 struct it *it;
6199 {
6200 Lisp_Object gc;
6201
6202 /* Precondition. */
6203 xassert (it->dpvec && it->current.dpvec_index >= 0);
6204
6205 it->face_id = it->saved_face_id;
6206
6207 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6208 That seemed totally bogus - so I changed it... */
6209 gc = it->dpvec[it->current.dpvec_index];
6210
6211 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6212 {
6213 it->c = GLYPH_CODE_CHAR (gc);
6214 it->len = CHAR_BYTES (it->c);
6215
6216 /* The entry may contain a face id to use. Such a face id is
6217 the id of a Lisp face, not a realized face. A face id of
6218 zero means no face is specified. */
6219 if (it->dpvec_face_id >= 0)
6220 it->face_id = it->dpvec_face_id;
6221 else
6222 {
6223 int lface_id = GLYPH_CODE_FACE (gc);
6224 if (lface_id > 0)
6225 it->face_id = merge_faces (it->f, Qt, lface_id,
6226 it->saved_face_id);
6227 }
6228 }
6229 else
6230 /* Display table entry is invalid. Return a space. */
6231 it->c = ' ', it->len = 1;
6232
6233 /* Don't change position and object of the iterator here. They are
6234 still the values of the character that had this display table
6235 entry or was translated, and that's what we want. */
6236 it->what = IT_CHARACTER;
6237 return 1;
6238 }
6239
6240
6241 /* Load IT with the next display element from Lisp string IT->string.
6242 IT->current.string_pos is the current position within the string.
6243 If IT->current.overlay_string_index >= 0, the Lisp string is an
6244 overlay string. */
6245
6246 static int
6247 next_element_from_string (it)
6248 struct it *it;
6249 {
6250 struct text_pos position;
6251
6252 xassert (STRINGP (it->string));
6253 xassert (IT_STRING_CHARPOS (*it) >= 0);
6254 position = it->current.string_pos;
6255
6256 /* Time to check for invisible text? */
6257 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6258 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6259 {
6260 handle_stop (it);
6261
6262 /* Since a handler may have changed IT->method, we must
6263 recurse here. */
6264 return GET_NEXT_DISPLAY_ELEMENT (it);
6265 }
6266
6267 if (it->current.overlay_string_index >= 0)
6268 {
6269 /* Get the next character from an overlay string. In overlay
6270 strings, There is no field width or padding with spaces to
6271 do. */
6272 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6273 {
6274 it->what = IT_EOB;
6275 return 0;
6276 }
6277 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6278 IT_STRING_BYTEPOS (*it))
6279 && next_element_from_composition (it))
6280 {
6281 return 1;
6282 }
6283 else if (STRING_MULTIBYTE (it->string))
6284 {
6285 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6286 const unsigned char *s = (SDATA (it->string)
6287 + IT_STRING_BYTEPOS (*it));
6288 it->c = string_char_and_length (s, remaining, &it->len);
6289 }
6290 else
6291 {
6292 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6293 it->len = 1;
6294 }
6295 }
6296 else
6297 {
6298 /* Get the next character from a Lisp string that is not an
6299 overlay string. Such strings come from the mode line, for
6300 example. We may have to pad with spaces, or truncate the
6301 string. See also next_element_from_c_string. */
6302 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6303 {
6304 it->what = IT_EOB;
6305 return 0;
6306 }
6307 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6308 {
6309 /* Pad with spaces. */
6310 it->c = ' ', it->len = 1;
6311 CHARPOS (position) = BYTEPOS (position) = -1;
6312 }
6313 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6314 IT_STRING_BYTEPOS (*it))
6315 && next_element_from_composition (it))
6316 {
6317 return 1;
6318 }
6319 else if (STRING_MULTIBYTE (it->string))
6320 {
6321 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6322 const unsigned char *s = (SDATA (it->string)
6323 + IT_STRING_BYTEPOS (*it));
6324 it->c = string_char_and_length (s, maxlen, &it->len);
6325 }
6326 else
6327 {
6328 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6329 it->len = 1;
6330 }
6331 }
6332
6333 /* Record what we have and where it came from. */
6334 it->what = IT_CHARACTER;
6335 it->object = it->string;
6336 it->position = position;
6337 return 1;
6338 }
6339
6340
6341 /* Load IT with next display element from C string IT->s.
6342 IT->string_nchars is the maximum number of characters to return
6343 from the string. IT->end_charpos may be greater than
6344 IT->string_nchars when this function is called, in which case we
6345 may have to return padding spaces. Value is zero if end of string
6346 reached, including padding spaces. */
6347
6348 static int
6349 next_element_from_c_string (it)
6350 struct it *it;
6351 {
6352 int success_p = 1;
6353
6354 xassert (it->s);
6355 it->what = IT_CHARACTER;
6356 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6357 it->object = Qnil;
6358
6359 /* IT's position can be greater IT->string_nchars in case a field
6360 width or precision has been specified when the iterator was
6361 initialized. */
6362 if (IT_CHARPOS (*it) >= it->end_charpos)
6363 {
6364 /* End of the game. */
6365 it->what = IT_EOB;
6366 success_p = 0;
6367 }
6368 else if (IT_CHARPOS (*it) >= it->string_nchars)
6369 {
6370 /* Pad with spaces. */
6371 it->c = ' ', it->len = 1;
6372 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6373 }
6374 else if (it->multibyte_p)
6375 {
6376 /* Implementation note: The calls to strlen apparently aren't a
6377 performance problem because there is no noticeable performance
6378 difference between Emacs running in unibyte or multibyte mode. */
6379 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6380 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6381 maxlen, &it->len);
6382 }
6383 else
6384 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6385
6386 return success_p;
6387 }
6388
6389
6390 /* Set up IT to return characters from an ellipsis, if appropriate.
6391 The definition of the ellipsis glyphs may come from a display table
6392 entry. This function Fills IT with the first glyph from the
6393 ellipsis if an ellipsis is to be displayed. */
6394
6395 static int
6396 next_element_from_ellipsis (it)
6397 struct it *it;
6398 {
6399 if (it->selective_display_ellipsis_p)
6400 setup_for_ellipsis (it, it->len);
6401 else
6402 {
6403 /* The face at the current position may be different from the
6404 face we find after the invisible text. Remember what it
6405 was in IT->saved_face_id, and signal that it's there by
6406 setting face_before_selective_p. */
6407 it->saved_face_id = it->face_id;
6408 it->method = GET_FROM_BUFFER;
6409 it->object = it->w->buffer;
6410 reseat_at_next_visible_line_start (it, 1);
6411 it->face_before_selective_p = 1;
6412 }
6413
6414 return GET_NEXT_DISPLAY_ELEMENT (it);
6415 }
6416
6417
6418 /* Deliver an image display element. The iterator IT is already
6419 filled with image information (done in handle_display_prop). Value
6420 is always 1. */
6421
6422
6423 static int
6424 next_element_from_image (it)
6425 struct it *it;
6426 {
6427 it->what = IT_IMAGE;
6428 return 1;
6429 }
6430
6431
6432 /* Fill iterator IT with next display element from a stretch glyph
6433 property. IT->object is the value of the text property. Value is
6434 always 1. */
6435
6436 static int
6437 next_element_from_stretch (it)
6438 struct it *it;
6439 {
6440 it->what = IT_STRETCH;
6441 return 1;
6442 }
6443
6444
6445 /* Load IT with the next display element from current_buffer. Value
6446 is zero if end of buffer reached. IT->stop_charpos is the next
6447 position at which to stop and check for text properties or buffer
6448 end. */
6449
6450 static int
6451 next_element_from_buffer (it)
6452 struct it *it;
6453 {
6454 int success_p = 1;
6455
6456 xassert (IT_CHARPOS (*it) >= BEGV);
6457
6458 if (IT_CHARPOS (*it) >= it->stop_charpos)
6459 {
6460 if (IT_CHARPOS (*it) >= it->end_charpos)
6461 {
6462 int overlay_strings_follow_p;
6463
6464 /* End of the game, except when overlay strings follow that
6465 haven't been returned yet. */
6466 if (it->overlay_strings_at_end_processed_p)
6467 overlay_strings_follow_p = 0;
6468 else
6469 {
6470 it->overlay_strings_at_end_processed_p = 1;
6471 overlay_strings_follow_p = get_overlay_strings (it, 0);
6472 }
6473
6474 if (overlay_strings_follow_p)
6475 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6476 else
6477 {
6478 it->what = IT_EOB;
6479 it->position = it->current.pos;
6480 success_p = 0;
6481 }
6482 }
6483 else
6484 {
6485 handle_stop (it);
6486 return GET_NEXT_DISPLAY_ELEMENT (it);
6487 }
6488 }
6489 else
6490 {
6491 /* No face changes, overlays etc. in sight, so just return a
6492 character from current_buffer. */
6493 unsigned char *p;
6494
6495 /* Maybe run the redisplay end trigger hook. Performance note:
6496 This doesn't seem to cost measurable time. */
6497 if (it->redisplay_end_trigger_charpos
6498 && it->glyph_row
6499 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6500 run_redisplay_end_trigger_hook (it);
6501
6502 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6503 && next_element_from_composition (it))
6504 {
6505 return 1;
6506 }
6507
6508 /* Get the next character, maybe multibyte. */
6509 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6510 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6511 it->c = STRING_CHAR_AND_LENGTH (p, 0, it->len);
6512 else
6513 it->c = *p, it->len = 1;
6514
6515 /* Record what we have and where it came from. */
6516 it->what = IT_CHARACTER;
6517 it->object = it->w->buffer;
6518 it->position = it->current.pos;
6519
6520 /* Normally we return the character found above, except when we
6521 really want to return an ellipsis for selective display. */
6522 if (it->selective)
6523 {
6524 if (it->c == '\n')
6525 {
6526 /* A value of selective > 0 means hide lines indented more
6527 than that number of columns. */
6528 if (it->selective > 0
6529 && IT_CHARPOS (*it) + 1 < ZV
6530 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6531 IT_BYTEPOS (*it) + 1,
6532 (double) it->selective)) /* iftc */
6533 {
6534 success_p = next_element_from_ellipsis (it);
6535 it->dpvec_char_len = -1;
6536 }
6537 }
6538 else if (it->c == '\r' && it->selective == -1)
6539 {
6540 /* A value of selective == -1 means that everything from the
6541 CR to the end of the line is invisible, with maybe an
6542 ellipsis displayed for it. */
6543 success_p = next_element_from_ellipsis (it);
6544 it->dpvec_char_len = -1;
6545 }
6546 }
6547 }
6548
6549 /* Value is zero if end of buffer reached. */
6550 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6551 return success_p;
6552 }
6553
6554
6555 /* Run the redisplay end trigger hook for IT. */
6556
6557 static void
6558 run_redisplay_end_trigger_hook (it)
6559 struct it *it;
6560 {
6561 Lisp_Object args[3];
6562
6563 /* IT->glyph_row should be non-null, i.e. we should be actually
6564 displaying something, or otherwise we should not run the hook. */
6565 xassert (it->glyph_row);
6566
6567 /* Set up hook arguments. */
6568 args[0] = Qredisplay_end_trigger_functions;
6569 args[1] = it->window;
6570 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6571 it->redisplay_end_trigger_charpos = 0;
6572
6573 /* Since we are *trying* to run these functions, don't try to run
6574 them again, even if they get an error. */
6575 it->w->redisplay_end_trigger = Qnil;
6576 Frun_hook_with_args (3, args);
6577
6578 /* Notice if it changed the face of the character we are on. */
6579 handle_face_prop (it);
6580 }
6581
6582
6583 /* Deliver a composition display element. Unlike the other
6584 next_element_from_XXX, this function is not registered in the array
6585 get_next_element[]. It is called from next_element_from_buffer and
6586 next_element_from_string when necessary. */
6587
6588 static int
6589 next_element_from_composition (it)
6590 struct it *it;
6591 {
6592 it->what = IT_COMPOSITION;
6593 it->len = it->cmp_it.nbytes;
6594 if (STRINGP (it->string))
6595 {
6596 if (it->c < 0)
6597 {
6598 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6599 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6600 return 0;
6601 }
6602 it->position = it->current.string_pos;
6603 it->object = it->string;
6604 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6605 IT_STRING_BYTEPOS (*it), it->string);
6606 }
6607 else
6608 {
6609 if (it->c < 0)
6610 {
6611 IT_CHARPOS (*it) += it->cmp_it.nchars;
6612 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6613 return 0;
6614 }
6615 it->position = it->current.pos;
6616 it->object = it->w->buffer;
6617 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6618 IT_BYTEPOS (*it), Qnil);
6619 }
6620 return 1;
6621 }
6622
6623
6624 \f
6625 /***********************************************************************
6626 Moving an iterator without producing glyphs
6627 ***********************************************************************/
6628
6629 /* Check if iterator is at a position corresponding to a valid buffer
6630 position after some move_it_ call. */
6631
6632 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6633 ((it)->method == GET_FROM_STRING \
6634 ? IT_STRING_CHARPOS (*it) == 0 \
6635 : 1)
6636
6637
6638 /* Move iterator IT to a specified buffer or X position within one
6639 line on the display without producing glyphs.
6640
6641 OP should be a bit mask including some or all of these bits:
6642 MOVE_TO_X: Stop on reaching x-position TO_X.
6643 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6644 Regardless of OP's value, stop in reaching the end of the display line.
6645
6646 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6647 This means, in particular, that TO_X includes window's horizontal
6648 scroll amount.
6649
6650 The return value has several possible values that
6651 say what condition caused the scan to stop:
6652
6653 MOVE_POS_MATCH_OR_ZV
6654 - when TO_POS or ZV was reached.
6655
6656 MOVE_X_REACHED
6657 -when TO_X was reached before TO_POS or ZV were reached.
6658
6659 MOVE_LINE_CONTINUED
6660 - when we reached the end of the display area and the line must
6661 be continued.
6662
6663 MOVE_LINE_TRUNCATED
6664 - when we reached the end of the display area and the line is
6665 truncated.
6666
6667 MOVE_NEWLINE_OR_CR
6668 - when we stopped at a line end, i.e. a newline or a CR and selective
6669 display is on. */
6670
6671 static enum move_it_result
6672 move_it_in_display_line_to (struct it *it,
6673 EMACS_INT to_charpos, int to_x,
6674 enum move_operation_enum op)
6675 {
6676 enum move_it_result result = MOVE_UNDEFINED;
6677 struct glyph_row *saved_glyph_row;
6678 struct it wrap_it, atpos_it, atx_it;
6679 int may_wrap = 0;
6680
6681 /* Don't produce glyphs in produce_glyphs. */
6682 saved_glyph_row = it->glyph_row;
6683 it->glyph_row = NULL;
6684
6685 /* Use wrap_it to save a copy of IT wherever a word wrap could
6686 occur. Use atpos_it to save a copy of IT at the desired buffer
6687 position, if found, so that we can scan ahead and check if the
6688 word later overshoots the window edge. Use atx_it similarly, for
6689 pixel positions. */
6690 wrap_it.sp = -1;
6691 atpos_it.sp = -1;
6692 atx_it.sp = -1;
6693
6694 #define BUFFER_POS_REACHED_P() \
6695 ((op & MOVE_TO_POS) != 0 \
6696 && BUFFERP (it->object) \
6697 && IT_CHARPOS (*it) >= to_charpos \
6698 && (it->method == GET_FROM_BUFFER \
6699 || (it->method == GET_FROM_DISPLAY_VECTOR \
6700 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6701
6702 /* If there's a line-/wrap-prefix, handle it. */
6703 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6704 && it->current_y < it->last_visible_y)
6705 handle_line_prefix (it);
6706
6707 while (1)
6708 {
6709 int x, i, ascent = 0, descent = 0;
6710
6711 /* Utility macro to reset an iterator with x, ascent, and descent. */
6712 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6713 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6714 (IT)->max_descent = descent)
6715
6716 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6717 glyph). */
6718 if ((op & MOVE_TO_POS) != 0
6719 && BUFFERP (it->object)
6720 && it->method == GET_FROM_BUFFER
6721 && IT_CHARPOS (*it) > to_charpos)
6722 {
6723 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6724 {
6725 result = MOVE_POS_MATCH_OR_ZV;
6726 break;
6727 }
6728 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6729 /* If wrap_it is valid, the current position might be in a
6730 word that is wrapped. So, save the iterator in
6731 atpos_it and continue to see if wrapping happens. */
6732 atpos_it = *it;
6733 }
6734
6735 /* Stop when ZV reached.
6736 We used to stop here when TO_CHARPOS reached as well, but that is
6737 too soon if this glyph does not fit on this line. So we handle it
6738 explicitly below. */
6739 if (!get_next_display_element (it))
6740 {
6741 result = MOVE_POS_MATCH_OR_ZV;
6742 break;
6743 }
6744
6745 if (it->line_wrap == TRUNCATE)
6746 {
6747 if (BUFFER_POS_REACHED_P ())
6748 {
6749 result = MOVE_POS_MATCH_OR_ZV;
6750 break;
6751 }
6752 }
6753 else
6754 {
6755 if (it->line_wrap == WORD_WRAP)
6756 {
6757 if (IT_DISPLAYING_WHITESPACE (it))
6758 may_wrap = 1;
6759 else if (may_wrap)
6760 {
6761 /* We have reached a glyph that follows one or more
6762 whitespace characters. If the position is
6763 already found, we are done. */
6764 if (atpos_it.sp >= 0)
6765 {
6766 *it = atpos_it;
6767 result = MOVE_POS_MATCH_OR_ZV;
6768 goto done;
6769 }
6770 if (atx_it.sp >= 0)
6771 {
6772 *it = atx_it;
6773 result = MOVE_X_REACHED;
6774 goto done;
6775 }
6776 /* Otherwise, we can wrap here. */
6777 wrap_it = *it;
6778 may_wrap = 0;
6779 }
6780 }
6781 }
6782
6783 /* Remember the line height for the current line, in case
6784 the next element doesn't fit on the line. */
6785 ascent = it->max_ascent;
6786 descent = it->max_descent;
6787
6788 /* The call to produce_glyphs will get the metrics of the
6789 display element IT is loaded with. Record the x-position
6790 before this display element, in case it doesn't fit on the
6791 line. */
6792 x = it->current_x;
6793
6794 PRODUCE_GLYPHS (it);
6795
6796 if (it->area != TEXT_AREA)
6797 {
6798 set_iterator_to_next (it, 1);
6799 continue;
6800 }
6801
6802 /* The number of glyphs we get back in IT->nglyphs will normally
6803 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6804 character on a terminal frame, or (iii) a line end. For the
6805 second case, IT->nglyphs - 1 padding glyphs will be present
6806 (on X frames, there is only one glyph produced for a
6807 composite character.
6808
6809 The behavior implemented below means, for continuation lines,
6810 that as many spaces of a TAB as fit on the current line are
6811 displayed there. For terminal frames, as many glyphs of a
6812 multi-glyph character are displayed in the current line, too.
6813 This is what the old redisplay code did, and we keep it that
6814 way. Under X, the whole shape of a complex character must
6815 fit on the line or it will be completely displayed in the
6816 next line.
6817
6818 Note that both for tabs and padding glyphs, all glyphs have
6819 the same width. */
6820 if (it->nglyphs)
6821 {
6822 /* More than one glyph or glyph doesn't fit on line. All
6823 glyphs have the same width. */
6824 int single_glyph_width = it->pixel_width / it->nglyphs;
6825 int new_x;
6826 int x_before_this_char = x;
6827 int hpos_before_this_char = it->hpos;
6828
6829 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6830 {
6831 new_x = x + single_glyph_width;
6832
6833 /* We want to leave anything reaching TO_X to the caller. */
6834 if ((op & MOVE_TO_X) && new_x > to_x)
6835 {
6836 if (BUFFER_POS_REACHED_P ())
6837 {
6838 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6839 goto buffer_pos_reached;
6840 if (atpos_it.sp < 0)
6841 {
6842 atpos_it = *it;
6843 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6844 }
6845 }
6846 else
6847 {
6848 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6849 {
6850 it->current_x = x;
6851 result = MOVE_X_REACHED;
6852 break;
6853 }
6854 if (atx_it.sp < 0)
6855 {
6856 atx_it = *it;
6857 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6858 }
6859 }
6860 }
6861
6862 if (/* Lines are continued. */
6863 it->line_wrap != TRUNCATE
6864 && (/* And glyph doesn't fit on the line. */
6865 new_x > it->last_visible_x
6866 /* Or it fits exactly and we're on a window
6867 system frame. */
6868 || (new_x == it->last_visible_x
6869 && FRAME_WINDOW_P (it->f))))
6870 {
6871 if (/* IT->hpos == 0 means the very first glyph
6872 doesn't fit on the line, e.g. a wide image. */
6873 it->hpos == 0
6874 || (new_x == it->last_visible_x
6875 && FRAME_WINDOW_P (it->f)))
6876 {
6877 ++it->hpos;
6878 it->current_x = new_x;
6879
6880 /* The character's last glyph just barely fits
6881 in this row. */
6882 if (i == it->nglyphs - 1)
6883 {
6884 /* If this is the destination position,
6885 return a position *before* it in this row,
6886 now that we know it fits in this row. */
6887 if (BUFFER_POS_REACHED_P ())
6888 {
6889 if (it->line_wrap != WORD_WRAP
6890 || wrap_it.sp < 0)
6891 {
6892 it->hpos = hpos_before_this_char;
6893 it->current_x = x_before_this_char;
6894 result = MOVE_POS_MATCH_OR_ZV;
6895 break;
6896 }
6897 if (it->line_wrap == WORD_WRAP
6898 && atpos_it.sp < 0)
6899 {
6900 atpos_it = *it;
6901 atpos_it.current_x = x_before_this_char;
6902 atpos_it.hpos = hpos_before_this_char;
6903 }
6904 }
6905
6906 set_iterator_to_next (it, 1);
6907 /* One graphical terminals, newlines may
6908 "overflow" into the fringe if
6909 overflow-newline-into-fringe is non-nil.
6910 On text-only terminals, newlines may
6911 overflow into the last glyph on the
6912 display line.*/
6913 if (!FRAME_WINDOW_P (it->f)
6914 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6915 {
6916 if (!get_next_display_element (it))
6917 {
6918 result = MOVE_POS_MATCH_OR_ZV;
6919 break;
6920 }
6921 if (BUFFER_POS_REACHED_P ())
6922 {
6923 if (ITERATOR_AT_END_OF_LINE_P (it))
6924 result = MOVE_POS_MATCH_OR_ZV;
6925 else
6926 result = MOVE_LINE_CONTINUED;
6927 break;
6928 }
6929 if (ITERATOR_AT_END_OF_LINE_P (it))
6930 {
6931 result = MOVE_NEWLINE_OR_CR;
6932 break;
6933 }
6934 }
6935 }
6936 }
6937 else
6938 IT_RESET_X_ASCENT_DESCENT (it);
6939
6940 if (wrap_it.sp >= 0)
6941 {
6942 *it = wrap_it;
6943 atpos_it.sp = -1;
6944 atx_it.sp = -1;
6945 }
6946
6947 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6948 IT_CHARPOS (*it)));
6949 result = MOVE_LINE_CONTINUED;
6950 break;
6951 }
6952
6953 if (BUFFER_POS_REACHED_P ())
6954 {
6955 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6956 goto buffer_pos_reached;
6957 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6958 {
6959 atpos_it = *it;
6960 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6961 }
6962 }
6963
6964 if (new_x > it->first_visible_x)
6965 {
6966 /* Glyph is visible. Increment number of glyphs that
6967 would be displayed. */
6968 ++it->hpos;
6969 }
6970 }
6971
6972 if (result != MOVE_UNDEFINED)
6973 break;
6974 }
6975 else if (BUFFER_POS_REACHED_P ())
6976 {
6977 buffer_pos_reached:
6978 IT_RESET_X_ASCENT_DESCENT (it);
6979 result = MOVE_POS_MATCH_OR_ZV;
6980 break;
6981 }
6982 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6983 {
6984 /* Stop when TO_X specified and reached. This check is
6985 necessary here because of lines consisting of a line end,
6986 only. The line end will not produce any glyphs and we
6987 would never get MOVE_X_REACHED. */
6988 xassert (it->nglyphs == 0);
6989 result = MOVE_X_REACHED;
6990 break;
6991 }
6992
6993 /* Is this a line end? If yes, we're done. */
6994 if (ITERATOR_AT_END_OF_LINE_P (it))
6995 {
6996 result = MOVE_NEWLINE_OR_CR;
6997 break;
6998 }
6999
7000 /* The current display element has been consumed. Advance
7001 to the next. */
7002 set_iterator_to_next (it, 1);
7003
7004 /* Stop if lines are truncated and IT's current x-position is
7005 past the right edge of the window now. */
7006 if (it->line_wrap == TRUNCATE
7007 && it->current_x >= it->last_visible_x)
7008 {
7009 if (!FRAME_WINDOW_P (it->f)
7010 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7011 {
7012 if (!get_next_display_element (it)
7013 || BUFFER_POS_REACHED_P ())
7014 {
7015 result = MOVE_POS_MATCH_OR_ZV;
7016 break;
7017 }
7018 if (ITERATOR_AT_END_OF_LINE_P (it))
7019 {
7020 result = MOVE_NEWLINE_OR_CR;
7021 break;
7022 }
7023 }
7024 result = MOVE_LINE_TRUNCATED;
7025 break;
7026 }
7027 #undef IT_RESET_X_ASCENT_DESCENT
7028 }
7029
7030 #undef BUFFER_POS_REACHED_P
7031
7032 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7033 restore the saved iterator. */
7034 if (atpos_it.sp >= 0)
7035 *it = atpos_it;
7036 else if (atx_it.sp >= 0)
7037 *it = atx_it;
7038
7039 done:
7040
7041 /* Restore the iterator settings altered at the beginning of this
7042 function. */
7043 it->glyph_row = saved_glyph_row;
7044 return result;
7045 }
7046
7047 /* For external use. */
7048 void
7049 move_it_in_display_line (struct it *it,
7050 EMACS_INT to_charpos, int to_x,
7051 enum move_operation_enum op)
7052 {
7053 if (it->line_wrap == WORD_WRAP
7054 && (op & MOVE_TO_X))
7055 {
7056 struct it save_it = *it;
7057 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7058 /* When word-wrap is on, TO_X may lie past the end
7059 of a wrapped line. Then it->current is the
7060 character on the next line, so backtrack to the
7061 space before the wrap point. */
7062 if (skip == MOVE_LINE_CONTINUED)
7063 {
7064 int prev_x = max (it->current_x - 1, 0);
7065 *it = save_it;
7066 move_it_in_display_line_to
7067 (it, -1, prev_x, MOVE_TO_X);
7068 }
7069 }
7070 else
7071 move_it_in_display_line_to (it, to_charpos, to_x, op);
7072 }
7073
7074
7075 /* Move IT forward until it satisfies one or more of the criteria in
7076 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7077
7078 OP is a bit-mask that specifies where to stop, and in particular,
7079 which of those four position arguments makes a difference. See the
7080 description of enum move_operation_enum.
7081
7082 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7083 screen line, this function will set IT to the next position >
7084 TO_CHARPOS. */
7085
7086 void
7087 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7088 struct it *it;
7089 int to_charpos, to_x, to_y, to_vpos;
7090 int op;
7091 {
7092 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7093 int line_height;
7094 int reached = 0;
7095
7096 for (;;)
7097 {
7098 if (op & MOVE_TO_VPOS)
7099 {
7100 /* If no TO_CHARPOS and no TO_X specified, stop at the
7101 start of the line TO_VPOS. */
7102 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7103 {
7104 if (it->vpos == to_vpos)
7105 {
7106 reached = 1;
7107 break;
7108 }
7109 else
7110 skip = move_it_in_display_line_to (it, -1, -1, 0);
7111 }
7112 else
7113 {
7114 /* TO_VPOS >= 0 means stop at TO_X in the line at
7115 TO_VPOS, or at TO_POS, whichever comes first. */
7116 if (it->vpos == to_vpos)
7117 {
7118 reached = 2;
7119 break;
7120 }
7121
7122 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7123
7124 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7125 {
7126 reached = 3;
7127 break;
7128 }
7129 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7130 {
7131 /* We have reached TO_X but not in the line we want. */
7132 skip = move_it_in_display_line_to (it, to_charpos,
7133 -1, MOVE_TO_POS);
7134 if (skip == MOVE_POS_MATCH_OR_ZV)
7135 {
7136 reached = 4;
7137 break;
7138 }
7139 }
7140 }
7141 }
7142 else if (op & MOVE_TO_Y)
7143 {
7144 struct it it_backup;
7145
7146 if (it->line_wrap == WORD_WRAP)
7147 it_backup = *it;
7148
7149 /* TO_Y specified means stop at TO_X in the line containing
7150 TO_Y---or at TO_CHARPOS if this is reached first. The
7151 problem is that we can't really tell whether the line
7152 contains TO_Y before we have completely scanned it, and
7153 this may skip past TO_X. What we do is to first scan to
7154 TO_X.
7155
7156 If TO_X is not specified, use a TO_X of zero. The reason
7157 is to make the outcome of this function more predictable.
7158 If we didn't use TO_X == 0, we would stop at the end of
7159 the line which is probably not what a caller would expect
7160 to happen. */
7161 skip = move_it_in_display_line_to
7162 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7163 (MOVE_TO_X | (op & MOVE_TO_POS)));
7164
7165 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7166 if (skip == MOVE_POS_MATCH_OR_ZV)
7167 reached = 5;
7168 else if (skip == MOVE_X_REACHED)
7169 {
7170 /* If TO_X was reached, we want to know whether TO_Y is
7171 in the line. We know this is the case if the already
7172 scanned glyphs make the line tall enough. Otherwise,
7173 we must check by scanning the rest of the line. */
7174 line_height = it->max_ascent + it->max_descent;
7175 if (to_y >= it->current_y
7176 && to_y < it->current_y + line_height)
7177 {
7178 reached = 6;
7179 break;
7180 }
7181 it_backup = *it;
7182 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7183 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7184 op & MOVE_TO_POS);
7185 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7186 line_height = it->max_ascent + it->max_descent;
7187 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7188
7189 if (to_y >= it->current_y
7190 && to_y < it->current_y + line_height)
7191 {
7192 /* If TO_Y is in this line and TO_X was reached
7193 above, we scanned too far. We have to restore
7194 IT's settings to the ones before skipping. */
7195 *it = it_backup;
7196 reached = 6;
7197 }
7198 else
7199 {
7200 skip = skip2;
7201 if (skip == MOVE_POS_MATCH_OR_ZV)
7202 reached = 7;
7203 }
7204 }
7205 else
7206 {
7207 /* Check whether TO_Y is in this line. */
7208 line_height = it->max_ascent + it->max_descent;
7209 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7210
7211 if (to_y >= it->current_y
7212 && to_y < it->current_y + line_height)
7213 {
7214 /* When word-wrap is on, TO_X may lie past the end
7215 of a wrapped line. Then it->current is the
7216 character on the next line, so backtrack to the
7217 space before the wrap point. */
7218 if (skip == MOVE_LINE_CONTINUED
7219 && it->line_wrap == WORD_WRAP)
7220 {
7221 int prev_x = max (it->current_x - 1, 0);
7222 *it = it_backup;
7223 skip = move_it_in_display_line_to
7224 (it, -1, prev_x, MOVE_TO_X);
7225 }
7226 reached = 6;
7227 }
7228 }
7229
7230 if (reached)
7231 break;
7232 }
7233 else if (BUFFERP (it->object)
7234 && (it->method == GET_FROM_BUFFER
7235 || it->method == GET_FROM_STRETCH)
7236 && IT_CHARPOS (*it) >= to_charpos)
7237 skip = MOVE_POS_MATCH_OR_ZV;
7238 else
7239 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7240
7241 switch (skip)
7242 {
7243 case MOVE_POS_MATCH_OR_ZV:
7244 reached = 8;
7245 goto out;
7246
7247 case MOVE_NEWLINE_OR_CR:
7248 set_iterator_to_next (it, 1);
7249 it->continuation_lines_width = 0;
7250 break;
7251
7252 case MOVE_LINE_TRUNCATED:
7253 it->continuation_lines_width = 0;
7254 reseat_at_next_visible_line_start (it, 0);
7255 if ((op & MOVE_TO_POS) != 0
7256 && IT_CHARPOS (*it) > to_charpos)
7257 {
7258 reached = 9;
7259 goto out;
7260 }
7261 break;
7262
7263 case MOVE_LINE_CONTINUED:
7264 /* For continued lines ending in a tab, some of the glyphs
7265 associated with the tab are displayed on the current
7266 line. Since it->current_x does not include these glyphs,
7267 we use it->last_visible_x instead. */
7268 if (it->c == '\t')
7269 {
7270 it->continuation_lines_width += it->last_visible_x;
7271 /* When moving by vpos, ensure that the iterator really
7272 advances to the next line (bug#847, bug#969). Fixme:
7273 do we need to do this in other circumstances? */
7274 if (it->current_x != it->last_visible_x
7275 && (op & MOVE_TO_VPOS)
7276 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7277 set_iterator_to_next (it, 0);
7278 }
7279 else
7280 it->continuation_lines_width += it->current_x;
7281 break;
7282
7283 default:
7284 abort ();
7285 }
7286
7287 /* Reset/increment for the next run. */
7288 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7289 it->current_x = it->hpos = 0;
7290 it->current_y += it->max_ascent + it->max_descent;
7291 ++it->vpos;
7292 last_height = it->max_ascent + it->max_descent;
7293 last_max_ascent = it->max_ascent;
7294 it->max_ascent = it->max_descent = 0;
7295 }
7296
7297 out:
7298
7299 /* On text terminals, we may stop at the end of a line in the middle
7300 of a multi-character glyph. If the glyph itself is continued,
7301 i.e. it is actually displayed on the next line, don't treat this
7302 stopping point as valid; move to the next line instead (unless
7303 that brings us offscreen). */
7304 if (!FRAME_WINDOW_P (it->f)
7305 && op & MOVE_TO_POS
7306 && IT_CHARPOS (*it) == to_charpos
7307 && it->what == IT_CHARACTER
7308 && it->nglyphs > 1
7309 && it->line_wrap == WINDOW_WRAP
7310 && it->current_x == it->last_visible_x - 1
7311 && it->c != '\n'
7312 && it->c != '\t'
7313 && it->vpos < XFASTINT (it->w->window_end_vpos))
7314 {
7315 it->continuation_lines_width += it->current_x;
7316 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7317 it->current_y += it->max_ascent + it->max_descent;
7318 ++it->vpos;
7319 last_height = it->max_ascent + it->max_descent;
7320 last_max_ascent = it->max_ascent;
7321 }
7322
7323 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7324 }
7325
7326
7327 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7328
7329 If DY > 0, move IT backward at least that many pixels. DY = 0
7330 means move IT backward to the preceding line start or BEGV. This
7331 function may move over more than DY pixels if IT->current_y - DY
7332 ends up in the middle of a line; in this case IT->current_y will be
7333 set to the top of the line moved to. */
7334
7335 void
7336 move_it_vertically_backward (it, dy)
7337 struct it *it;
7338 int dy;
7339 {
7340 int nlines, h;
7341 struct it it2, it3;
7342 int start_pos;
7343
7344 move_further_back:
7345 xassert (dy >= 0);
7346
7347 start_pos = IT_CHARPOS (*it);
7348
7349 /* Estimate how many newlines we must move back. */
7350 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7351
7352 /* Set the iterator's position that many lines back. */
7353 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7354 back_to_previous_visible_line_start (it);
7355
7356 /* Reseat the iterator here. When moving backward, we don't want
7357 reseat to skip forward over invisible text, set up the iterator
7358 to deliver from overlay strings at the new position etc. So,
7359 use reseat_1 here. */
7360 reseat_1 (it, it->current.pos, 1);
7361
7362 /* We are now surely at a line start. */
7363 it->current_x = it->hpos = 0;
7364 it->continuation_lines_width = 0;
7365
7366 /* Move forward and see what y-distance we moved. First move to the
7367 start of the next line so that we get its height. We need this
7368 height to be able to tell whether we reached the specified
7369 y-distance. */
7370 it2 = *it;
7371 it2.max_ascent = it2.max_descent = 0;
7372 do
7373 {
7374 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7375 MOVE_TO_POS | MOVE_TO_VPOS);
7376 }
7377 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7378 xassert (IT_CHARPOS (*it) >= BEGV);
7379 it3 = it2;
7380
7381 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7382 xassert (IT_CHARPOS (*it) >= BEGV);
7383 /* H is the actual vertical distance from the position in *IT
7384 and the starting position. */
7385 h = it2.current_y - it->current_y;
7386 /* NLINES is the distance in number of lines. */
7387 nlines = it2.vpos - it->vpos;
7388
7389 /* Correct IT's y and vpos position
7390 so that they are relative to the starting point. */
7391 it->vpos -= nlines;
7392 it->current_y -= h;
7393
7394 if (dy == 0)
7395 {
7396 /* DY == 0 means move to the start of the screen line. The
7397 value of nlines is > 0 if continuation lines were involved. */
7398 if (nlines > 0)
7399 move_it_by_lines (it, nlines, 1);
7400 }
7401 else
7402 {
7403 /* The y-position we try to reach, relative to *IT.
7404 Note that H has been subtracted in front of the if-statement. */
7405 int target_y = it->current_y + h - dy;
7406 int y0 = it3.current_y;
7407 int y1 = line_bottom_y (&it3);
7408 int line_height = y1 - y0;
7409
7410 /* If we did not reach target_y, try to move further backward if
7411 we can. If we moved too far backward, try to move forward. */
7412 if (target_y < it->current_y
7413 /* This is heuristic. In a window that's 3 lines high, with
7414 a line height of 13 pixels each, recentering with point
7415 on the bottom line will try to move -39/2 = 19 pixels
7416 backward. Try to avoid moving into the first line. */
7417 && (it->current_y - target_y
7418 > min (window_box_height (it->w), line_height * 2 / 3))
7419 && IT_CHARPOS (*it) > BEGV)
7420 {
7421 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7422 target_y - it->current_y));
7423 dy = it->current_y - target_y;
7424 goto move_further_back;
7425 }
7426 else if (target_y >= it->current_y + line_height
7427 && IT_CHARPOS (*it) < ZV)
7428 {
7429 /* Should move forward by at least one line, maybe more.
7430
7431 Note: Calling move_it_by_lines can be expensive on
7432 terminal frames, where compute_motion is used (via
7433 vmotion) to do the job, when there are very long lines
7434 and truncate-lines is nil. That's the reason for
7435 treating terminal frames specially here. */
7436
7437 if (!FRAME_WINDOW_P (it->f))
7438 move_it_vertically (it, target_y - (it->current_y + line_height));
7439 else
7440 {
7441 do
7442 {
7443 move_it_by_lines (it, 1, 1);
7444 }
7445 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7446 }
7447 }
7448 }
7449 }
7450
7451
7452 /* Move IT by a specified amount of pixel lines DY. DY negative means
7453 move backwards. DY = 0 means move to start of screen line. At the
7454 end, IT will be on the start of a screen line. */
7455
7456 void
7457 move_it_vertically (it, dy)
7458 struct it *it;
7459 int dy;
7460 {
7461 if (dy <= 0)
7462 move_it_vertically_backward (it, -dy);
7463 else
7464 {
7465 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7466 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7467 MOVE_TO_POS | MOVE_TO_Y);
7468 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7469
7470 /* If buffer ends in ZV without a newline, move to the start of
7471 the line to satisfy the post-condition. */
7472 if (IT_CHARPOS (*it) == ZV
7473 && ZV > BEGV
7474 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7475 move_it_by_lines (it, 0, 0);
7476 }
7477 }
7478
7479
7480 /* Move iterator IT past the end of the text line it is in. */
7481
7482 void
7483 move_it_past_eol (it)
7484 struct it *it;
7485 {
7486 enum move_it_result rc;
7487
7488 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7489 if (rc == MOVE_NEWLINE_OR_CR)
7490 set_iterator_to_next (it, 0);
7491 }
7492
7493
7494 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7495 negative means move up. DVPOS == 0 means move to the start of the
7496 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7497 NEED_Y_P is zero, IT->current_y will be left unchanged.
7498
7499 Further optimization ideas: If we would know that IT->f doesn't use
7500 a face with proportional font, we could be faster for
7501 truncate-lines nil. */
7502
7503 void
7504 move_it_by_lines (it, dvpos, need_y_p)
7505 struct it *it;
7506 int dvpos, need_y_p;
7507 {
7508 struct position pos;
7509
7510 /* The commented-out optimization uses vmotion on terminals. This
7511 gives bad results, because elements like it->what, on which
7512 callers such as pos_visible_p rely, aren't updated. */
7513 /* if (!FRAME_WINDOW_P (it->f))
7514 {
7515 struct text_pos textpos;
7516
7517 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7518 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7519 reseat (it, textpos, 1);
7520 it->vpos += pos.vpos;
7521 it->current_y += pos.vpos;
7522 }
7523 else */
7524
7525 if (dvpos == 0)
7526 {
7527 /* DVPOS == 0 means move to the start of the screen line. */
7528 move_it_vertically_backward (it, 0);
7529 xassert (it->current_x == 0 && it->hpos == 0);
7530 /* Let next call to line_bottom_y calculate real line height */
7531 last_height = 0;
7532 }
7533 else if (dvpos > 0)
7534 {
7535 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7536 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7537 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7538 }
7539 else
7540 {
7541 struct it it2;
7542 int start_charpos, i;
7543
7544 /* Start at the beginning of the screen line containing IT's
7545 position. This may actually move vertically backwards,
7546 in case of overlays, so adjust dvpos accordingly. */
7547 dvpos += it->vpos;
7548 move_it_vertically_backward (it, 0);
7549 dvpos -= it->vpos;
7550
7551 /* Go back -DVPOS visible lines and reseat the iterator there. */
7552 start_charpos = IT_CHARPOS (*it);
7553 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7554 back_to_previous_visible_line_start (it);
7555 reseat (it, it->current.pos, 1);
7556
7557 /* Move further back if we end up in a string or an image. */
7558 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7559 {
7560 /* First try to move to start of display line. */
7561 dvpos += it->vpos;
7562 move_it_vertically_backward (it, 0);
7563 dvpos -= it->vpos;
7564 if (IT_POS_VALID_AFTER_MOVE_P (it))
7565 break;
7566 /* If start of line is still in string or image,
7567 move further back. */
7568 back_to_previous_visible_line_start (it);
7569 reseat (it, it->current.pos, 1);
7570 dvpos--;
7571 }
7572
7573 it->current_x = it->hpos = 0;
7574
7575 /* Above call may have moved too far if continuation lines
7576 are involved. Scan forward and see if it did. */
7577 it2 = *it;
7578 it2.vpos = it2.current_y = 0;
7579 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7580 it->vpos -= it2.vpos;
7581 it->current_y -= it2.current_y;
7582 it->current_x = it->hpos = 0;
7583
7584 /* If we moved too far back, move IT some lines forward. */
7585 if (it2.vpos > -dvpos)
7586 {
7587 int delta = it2.vpos + dvpos;
7588 it2 = *it;
7589 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7590 /* Move back again if we got too far ahead. */
7591 if (IT_CHARPOS (*it) >= start_charpos)
7592 *it = it2;
7593 }
7594 }
7595 }
7596
7597 /* Return 1 if IT points into the middle of a display vector. */
7598
7599 int
7600 in_display_vector_p (it)
7601 struct it *it;
7602 {
7603 return (it->method == GET_FROM_DISPLAY_VECTOR
7604 && it->current.dpvec_index > 0
7605 && it->dpvec + it->current.dpvec_index != it->dpend);
7606 }
7607
7608 \f
7609 /***********************************************************************
7610 Messages
7611 ***********************************************************************/
7612
7613
7614 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7615 to *Messages*. */
7616
7617 void
7618 add_to_log (format, arg1, arg2)
7619 char *format;
7620 Lisp_Object arg1, arg2;
7621 {
7622 Lisp_Object args[3];
7623 Lisp_Object msg, fmt;
7624 char *buffer;
7625 int len;
7626 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7627 USE_SAFE_ALLOCA;
7628
7629 /* Do nothing if called asynchronously. Inserting text into
7630 a buffer may call after-change-functions and alike and
7631 that would means running Lisp asynchronously. */
7632 if (handling_signal)
7633 return;
7634
7635 fmt = msg = Qnil;
7636 GCPRO4 (fmt, msg, arg1, arg2);
7637
7638 args[0] = fmt = build_string (format);
7639 args[1] = arg1;
7640 args[2] = arg2;
7641 msg = Fformat (3, args);
7642
7643 len = SBYTES (msg) + 1;
7644 SAFE_ALLOCA (buffer, char *, len);
7645 bcopy (SDATA (msg), buffer, len);
7646
7647 message_dolog (buffer, len - 1, 1, 0);
7648 SAFE_FREE ();
7649
7650 UNGCPRO;
7651 }
7652
7653
7654 /* Output a newline in the *Messages* buffer if "needs" one. */
7655
7656 void
7657 message_log_maybe_newline ()
7658 {
7659 if (message_log_need_newline)
7660 message_dolog ("", 0, 1, 0);
7661 }
7662
7663
7664 /* Add a string M of length NBYTES to the message log, optionally
7665 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7666 nonzero, means interpret the contents of M as multibyte. This
7667 function calls low-level routines in order to bypass text property
7668 hooks, etc. which might not be safe to run.
7669
7670 This may GC (insert may run before/after change hooks),
7671 so the buffer M must NOT point to a Lisp string. */
7672
7673 void
7674 message_dolog (m, nbytes, nlflag, multibyte)
7675 const char *m;
7676 int nbytes, nlflag, multibyte;
7677 {
7678 if (!NILP (Vmemory_full))
7679 return;
7680
7681 if (!NILP (Vmessage_log_max))
7682 {
7683 struct buffer *oldbuf;
7684 Lisp_Object oldpoint, oldbegv, oldzv;
7685 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7686 int point_at_end = 0;
7687 int zv_at_end = 0;
7688 Lisp_Object old_deactivate_mark, tem;
7689 struct gcpro gcpro1;
7690
7691 old_deactivate_mark = Vdeactivate_mark;
7692 oldbuf = current_buffer;
7693 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7694 current_buffer->undo_list = Qt;
7695
7696 oldpoint = message_dolog_marker1;
7697 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7698 oldbegv = message_dolog_marker2;
7699 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7700 oldzv = message_dolog_marker3;
7701 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7702 GCPRO1 (old_deactivate_mark);
7703
7704 if (PT == Z)
7705 point_at_end = 1;
7706 if (ZV == Z)
7707 zv_at_end = 1;
7708
7709 BEGV = BEG;
7710 BEGV_BYTE = BEG_BYTE;
7711 ZV = Z;
7712 ZV_BYTE = Z_BYTE;
7713 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7714
7715 /* Insert the string--maybe converting multibyte to single byte
7716 or vice versa, so that all the text fits the buffer. */
7717 if (multibyte
7718 && NILP (current_buffer->enable_multibyte_characters))
7719 {
7720 int i, c, char_bytes;
7721 unsigned char work[1];
7722
7723 /* Convert a multibyte string to single-byte
7724 for the *Message* buffer. */
7725 for (i = 0; i < nbytes; i += char_bytes)
7726 {
7727 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7728 work[0] = (ASCII_CHAR_P (c)
7729 ? c
7730 : multibyte_char_to_unibyte (c, Qnil));
7731 insert_1_both (work, 1, 1, 1, 0, 0);
7732 }
7733 }
7734 else if (! multibyte
7735 && ! NILP (current_buffer->enable_multibyte_characters))
7736 {
7737 int i, c, char_bytes;
7738 unsigned char *msg = (unsigned char *) m;
7739 unsigned char str[MAX_MULTIBYTE_LENGTH];
7740 /* Convert a single-byte string to multibyte
7741 for the *Message* buffer. */
7742 for (i = 0; i < nbytes; i++)
7743 {
7744 c = msg[i];
7745 c = unibyte_char_to_multibyte (c);
7746 char_bytes = CHAR_STRING (c, str);
7747 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7748 }
7749 }
7750 else if (nbytes)
7751 insert_1 (m, nbytes, 1, 0, 0);
7752
7753 if (nlflag)
7754 {
7755 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7756 insert_1 ("\n", 1, 1, 0, 0);
7757
7758 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7759 this_bol = PT;
7760 this_bol_byte = PT_BYTE;
7761
7762 /* See if this line duplicates the previous one.
7763 If so, combine duplicates. */
7764 if (this_bol > BEG)
7765 {
7766 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7767 prev_bol = PT;
7768 prev_bol_byte = PT_BYTE;
7769
7770 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7771 this_bol, this_bol_byte);
7772 if (dup)
7773 {
7774 del_range_both (prev_bol, prev_bol_byte,
7775 this_bol, this_bol_byte, 0);
7776 if (dup > 1)
7777 {
7778 char dupstr[40];
7779 int duplen;
7780
7781 /* If you change this format, don't forget to also
7782 change message_log_check_duplicate. */
7783 sprintf (dupstr, " [%d times]", dup);
7784 duplen = strlen (dupstr);
7785 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7786 insert_1 (dupstr, duplen, 1, 0, 1);
7787 }
7788 }
7789 }
7790
7791 /* If we have more than the desired maximum number of lines
7792 in the *Messages* buffer now, delete the oldest ones.
7793 This is safe because we don't have undo in this buffer. */
7794
7795 if (NATNUMP (Vmessage_log_max))
7796 {
7797 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7798 -XFASTINT (Vmessage_log_max) - 1, 0);
7799 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7800 }
7801 }
7802 BEGV = XMARKER (oldbegv)->charpos;
7803 BEGV_BYTE = marker_byte_position (oldbegv);
7804
7805 if (zv_at_end)
7806 {
7807 ZV = Z;
7808 ZV_BYTE = Z_BYTE;
7809 }
7810 else
7811 {
7812 ZV = XMARKER (oldzv)->charpos;
7813 ZV_BYTE = marker_byte_position (oldzv);
7814 }
7815
7816 if (point_at_end)
7817 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7818 else
7819 /* We can't do Fgoto_char (oldpoint) because it will run some
7820 Lisp code. */
7821 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7822 XMARKER (oldpoint)->bytepos);
7823
7824 UNGCPRO;
7825 unchain_marker (XMARKER (oldpoint));
7826 unchain_marker (XMARKER (oldbegv));
7827 unchain_marker (XMARKER (oldzv));
7828
7829 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7830 set_buffer_internal (oldbuf);
7831 if (NILP (tem))
7832 windows_or_buffers_changed = old_windows_or_buffers_changed;
7833 message_log_need_newline = !nlflag;
7834 Vdeactivate_mark = old_deactivate_mark;
7835 }
7836 }
7837
7838
7839 /* We are at the end of the buffer after just having inserted a newline.
7840 (Note: We depend on the fact we won't be crossing the gap.)
7841 Check to see if the most recent message looks a lot like the previous one.
7842 Return 0 if different, 1 if the new one should just replace it, or a
7843 value N > 1 if we should also append " [N times]". */
7844
7845 static int
7846 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7847 int prev_bol, this_bol;
7848 int prev_bol_byte, this_bol_byte;
7849 {
7850 int i;
7851 int len = Z_BYTE - 1 - this_bol_byte;
7852 int seen_dots = 0;
7853 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7854 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7855
7856 for (i = 0; i < len; i++)
7857 {
7858 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7859 seen_dots = 1;
7860 if (p1[i] != p2[i])
7861 return seen_dots;
7862 }
7863 p1 += len;
7864 if (*p1 == '\n')
7865 return 2;
7866 if (*p1++ == ' ' && *p1++ == '[')
7867 {
7868 int n = 0;
7869 while (*p1 >= '0' && *p1 <= '9')
7870 n = n * 10 + *p1++ - '0';
7871 if (strncmp (p1, " times]\n", 8) == 0)
7872 return n+1;
7873 }
7874 return 0;
7875 }
7876 \f
7877
7878 /* Display an echo area message M with a specified length of NBYTES
7879 bytes. The string may include null characters. If M is 0, clear
7880 out any existing message, and let the mini-buffer text show
7881 through.
7882
7883 This may GC, so the buffer M must NOT point to a Lisp string. */
7884
7885 void
7886 message2 (m, nbytes, multibyte)
7887 const char *m;
7888 int nbytes;
7889 int multibyte;
7890 {
7891 /* First flush out any partial line written with print. */
7892 message_log_maybe_newline ();
7893 if (m)
7894 message_dolog (m, nbytes, 1, multibyte);
7895 message2_nolog (m, nbytes, multibyte);
7896 }
7897
7898
7899 /* The non-logging counterpart of message2. */
7900
7901 void
7902 message2_nolog (m, nbytes, multibyte)
7903 const char *m;
7904 int nbytes, multibyte;
7905 {
7906 struct frame *sf = SELECTED_FRAME ();
7907 message_enable_multibyte = multibyte;
7908
7909 if (FRAME_INITIAL_P (sf))
7910 {
7911 if (noninteractive_need_newline)
7912 putc ('\n', stderr);
7913 noninteractive_need_newline = 0;
7914 if (m)
7915 fwrite (m, nbytes, 1, stderr);
7916 if (cursor_in_echo_area == 0)
7917 fprintf (stderr, "\n");
7918 fflush (stderr);
7919 }
7920 /* A null message buffer means that the frame hasn't really been
7921 initialized yet. Error messages get reported properly by
7922 cmd_error, so this must be just an informative message; toss it. */
7923 else if (INTERACTIVE
7924 && sf->glyphs_initialized_p
7925 && FRAME_MESSAGE_BUF (sf))
7926 {
7927 Lisp_Object mini_window;
7928 struct frame *f;
7929
7930 /* Get the frame containing the mini-buffer
7931 that the selected frame is using. */
7932 mini_window = FRAME_MINIBUF_WINDOW (sf);
7933 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7934
7935 FRAME_SAMPLE_VISIBILITY (f);
7936 if (FRAME_VISIBLE_P (sf)
7937 && ! FRAME_VISIBLE_P (f))
7938 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7939
7940 if (m)
7941 {
7942 set_message (m, Qnil, nbytes, multibyte);
7943 if (minibuffer_auto_raise)
7944 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7945 }
7946 else
7947 clear_message (1, 1);
7948
7949 do_pending_window_change (0);
7950 echo_area_display (1);
7951 do_pending_window_change (0);
7952 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7953 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7954 }
7955 }
7956
7957
7958 /* Display an echo area message M with a specified length of NBYTES
7959 bytes. The string may include null characters. If M is not a
7960 string, clear out any existing message, and let the mini-buffer
7961 text show through.
7962
7963 This function cancels echoing. */
7964
7965 void
7966 message3 (m, nbytes, multibyte)
7967 Lisp_Object m;
7968 int nbytes;
7969 int multibyte;
7970 {
7971 struct gcpro gcpro1;
7972
7973 GCPRO1 (m);
7974 clear_message (1,1);
7975 cancel_echoing ();
7976
7977 /* First flush out any partial line written with print. */
7978 message_log_maybe_newline ();
7979 if (STRINGP (m))
7980 {
7981 char *buffer;
7982 USE_SAFE_ALLOCA;
7983
7984 SAFE_ALLOCA (buffer, char *, nbytes);
7985 bcopy (SDATA (m), buffer, nbytes);
7986 message_dolog (buffer, nbytes, 1, multibyte);
7987 SAFE_FREE ();
7988 }
7989 message3_nolog (m, nbytes, multibyte);
7990
7991 UNGCPRO;
7992 }
7993
7994
7995 /* The non-logging version of message3.
7996 This does not cancel echoing, because it is used for echoing.
7997 Perhaps we need to make a separate function for echoing
7998 and make this cancel echoing. */
7999
8000 void
8001 message3_nolog (m, nbytes, multibyte)
8002 Lisp_Object m;
8003 int nbytes, multibyte;
8004 {
8005 struct frame *sf = SELECTED_FRAME ();
8006 message_enable_multibyte = multibyte;
8007
8008 if (FRAME_INITIAL_P (sf))
8009 {
8010 if (noninteractive_need_newline)
8011 putc ('\n', stderr);
8012 noninteractive_need_newline = 0;
8013 if (STRINGP (m))
8014 fwrite (SDATA (m), nbytes, 1, stderr);
8015 if (cursor_in_echo_area == 0)
8016 fprintf (stderr, "\n");
8017 fflush (stderr);
8018 }
8019 /* A null message buffer means that the frame hasn't really been
8020 initialized yet. Error messages get reported properly by
8021 cmd_error, so this must be just an informative message; toss it. */
8022 else if (INTERACTIVE
8023 && sf->glyphs_initialized_p
8024 && FRAME_MESSAGE_BUF (sf))
8025 {
8026 Lisp_Object mini_window;
8027 Lisp_Object frame;
8028 struct frame *f;
8029
8030 /* Get the frame containing the mini-buffer
8031 that the selected frame is using. */
8032 mini_window = FRAME_MINIBUF_WINDOW (sf);
8033 frame = XWINDOW (mini_window)->frame;
8034 f = XFRAME (frame);
8035
8036 FRAME_SAMPLE_VISIBILITY (f);
8037 if (FRAME_VISIBLE_P (sf)
8038 && !FRAME_VISIBLE_P (f))
8039 Fmake_frame_visible (frame);
8040
8041 if (STRINGP (m) && SCHARS (m) > 0)
8042 {
8043 set_message (NULL, m, nbytes, multibyte);
8044 if (minibuffer_auto_raise)
8045 Fraise_frame (frame);
8046 /* Assume we are not echoing.
8047 (If we are, echo_now will override this.) */
8048 echo_message_buffer = Qnil;
8049 }
8050 else
8051 clear_message (1, 1);
8052
8053 do_pending_window_change (0);
8054 echo_area_display (1);
8055 do_pending_window_change (0);
8056 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8057 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8058 }
8059 }
8060
8061
8062 /* Display a null-terminated echo area message M. If M is 0, clear
8063 out any existing message, and let the mini-buffer text show through.
8064
8065 The buffer M must continue to exist until after the echo area gets
8066 cleared or some other message gets displayed there. Do not pass
8067 text that is stored in a Lisp string. Do not pass text in a buffer
8068 that was alloca'd. */
8069
8070 void
8071 message1 (m)
8072 char *m;
8073 {
8074 message2 (m, (m ? strlen (m) : 0), 0);
8075 }
8076
8077
8078 /* The non-logging counterpart of message1. */
8079
8080 void
8081 message1_nolog (m)
8082 char *m;
8083 {
8084 message2_nolog (m, (m ? strlen (m) : 0), 0);
8085 }
8086
8087 /* Display a message M which contains a single %s
8088 which gets replaced with STRING. */
8089
8090 void
8091 message_with_string (m, string, log)
8092 char *m;
8093 Lisp_Object string;
8094 int log;
8095 {
8096 CHECK_STRING (string);
8097
8098 if (noninteractive)
8099 {
8100 if (m)
8101 {
8102 if (noninteractive_need_newline)
8103 putc ('\n', stderr);
8104 noninteractive_need_newline = 0;
8105 fprintf (stderr, m, SDATA (string));
8106 if (!cursor_in_echo_area)
8107 fprintf (stderr, "\n");
8108 fflush (stderr);
8109 }
8110 }
8111 else if (INTERACTIVE)
8112 {
8113 /* The frame whose minibuffer we're going to display the message on.
8114 It may be larger than the selected frame, so we need
8115 to use its buffer, not the selected frame's buffer. */
8116 Lisp_Object mini_window;
8117 struct frame *f, *sf = SELECTED_FRAME ();
8118
8119 /* Get the frame containing the minibuffer
8120 that the selected frame is using. */
8121 mini_window = FRAME_MINIBUF_WINDOW (sf);
8122 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8123
8124 /* A null message buffer means that the frame hasn't really been
8125 initialized yet. Error messages get reported properly by
8126 cmd_error, so this must be just an informative message; toss it. */
8127 if (FRAME_MESSAGE_BUF (f))
8128 {
8129 Lisp_Object args[2], message;
8130 struct gcpro gcpro1, gcpro2;
8131
8132 args[0] = build_string (m);
8133 args[1] = message = string;
8134 GCPRO2 (args[0], message);
8135 gcpro1.nvars = 2;
8136
8137 message = Fformat (2, args);
8138
8139 if (log)
8140 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8141 else
8142 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8143
8144 UNGCPRO;
8145
8146 /* Print should start at the beginning of the message
8147 buffer next time. */
8148 message_buf_print = 0;
8149 }
8150 }
8151 }
8152
8153
8154 /* Dump an informative message to the minibuf. If M is 0, clear out
8155 any existing message, and let the mini-buffer text show through. */
8156
8157 /* VARARGS 1 */
8158 void
8159 message (m, a1, a2, a3)
8160 char *m;
8161 EMACS_INT a1, a2, a3;
8162 {
8163 if (noninteractive)
8164 {
8165 if (m)
8166 {
8167 if (noninteractive_need_newline)
8168 putc ('\n', stderr);
8169 noninteractive_need_newline = 0;
8170 fprintf (stderr, m, a1, a2, a3);
8171 if (cursor_in_echo_area == 0)
8172 fprintf (stderr, "\n");
8173 fflush (stderr);
8174 }
8175 }
8176 else if (INTERACTIVE)
8177 {
8178 /* The frame whose mini-buffer we're going to display the message
8179 on. It may be larger than the selected frame, so we need to
8180 use its buffer, not the selected frame's buffer. */
8181 Lisp_Object mini_window;
8182 struct frame *f, *sf = SELECTED_FRAME ();
8183
8184 /* Get the frame containing the mini-buffer
8185 that the selected frame is using. */
8186 mini_window = FRAME_MINIBUF_WINDOW (sf);
8187 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8188
8189 /* A null message buffer means that the frame hasn't really been
8190 initialized yet. Error messages get reported properly by
8191 cmd_error, so this must be just an informative message; toss
8192 it. */
8193 if (FRAME_MESSAGE_BUF (f))
8194 {
8195 if (m)
8196 {
8197 int len;
8198 #ifdef NO_ARG_ARRAY
8199 char *a[3];
8200 a[0] = (char *) a1;
8201 a[1] = (char *) a2;
8202 a[2] = (char *) a3;
8203
8204 len = doprnt (FRAME_MESSAGE_BUF (f),
8205 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8206 #else
8207 len = doprnt (FRAME_MESSAGE_BUF (f),
8208 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8209 (char **) &a1);
8210 #endif /* NO_ARG_ARRAY */
8211
8212 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8213 }
8214 else
8215 message1 (0);
8216
8217 /* Print should start at the beginning of the message
8218 buffer next time. */
8219 message_buf_print = 0;
8220 }
8221 }
8222 }
8223
8224
8225 /* The non-logging version of message. */
8226
8227 void
8228 message_nolog (m, a1, a2, a3)
8229 char *m;
8230 EMACS_INT a1, a2, a3;
8231 {
8232 Lisp_Object old_log_max;
8233 old_log_max = Vmessage_log_max;
8234 Vmessage_log_max = Qnil;
8235 message (m, a1, a2, a3);
8236 Vmessage_log_max = old_log_max;
8237 }
8238
8239
8240 /* Display the current message in the current mini-buffer. This is
8241 only called from error handlers in process.c, and is not time
8242 critical. */
8243
8244 void
8245 update_echo_area ()
8246 {
8247 if (!NILP (echo_area_buffer[0]))
8248 {
8249 Lisp_Object string;
8250 string = Fcurrent_message ();
8251 message3 (string, SBYTES (string),
8252 !NILP (current_buffer->enable_multibyte_characters));
8253 }
8254 }
8255
8256
8257 /* Make sure echo area buffers in `echo_buffers' are live.
8258 If they aren't, make new ones. */
8259
8260 static void
8261 ensure_echo_area_buffers ()
8262 {
8263 int i;
8264
8265 for (i = 0; i < 2; ++i)
8266 if (!BUFFERP (echo_buffer[i])
8267 || NILP (XBUFFER (echo_buffer[i])->name))
8268 {
8269 char name[30];
8270 Lisp_Object old_buffer;
8271 int j;
8272
8273 old_buffer = echo_buffer[i];
8274 sprintf (name, " *Echo Area %d*", i);
8275 echo_buffer[i] = Fget_buffer_create (build_string (name));
8276 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8277 /* to force word wrap in echo area -
8278 it was decided to postpone this*/
8279 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8280
8281 for (j = 0; j < 2; ++j)
8282 if (EQ (old_buffer, echo_area_buffer[j]))
8283 echo_area_buffer[j] = echo_buffer[i];
8284 }
8285 }
8286
8287
8288 /* Call FN with args A1..A4 with either the current or last displayed
8289 echo_area_buffer as current buffer.
8290
8291 WHICH zero means use the current message buffer
8292 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8293 from echo_buffer[] and clear it.
8294
8295 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8296 suitable buffer from echo_buffer[] and clear it.
8297
8298 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8299 that the current message becomes the last displayed one, make
8300 choose a suitable buffer for echo_area_buffer[0], and clear it.
8301
8302 Value is what FN returns. */
8303
8304 static int
8305 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8306 struct window *w;
8307 int which;
8308 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8309 EMACS_INT a1;
8310 Lisp_Object a2;
8311 EMACS_INT a3, a4;
8312 {
8313 Lisp_Object buffer;
8314 int this_one, the_other, clear_buffer_p, rc;
8315 int count = SPECPDL_INDEX ();
8316
8317 /* If buffers aren't live, make new ones. */
8318 ensure_echo_area_buffers ();
8319
8320 clear_buffer_p = 0;
8321
8322 if (which == 0)
8323 this_one = 0, the_other = 1;
8324 else if (which > 0)
8325 this_one = 1, the_other = 0;
8326 else
8327 {
8328 this_one = 0, the_other = 1;
8329 clear_buffer_p = 1;
8330
8331 /* We need a fresh one in case the current echo buffer equals
8332 the one containing the last displayed echo area message. */
8333 if (!NILP (echo_area_buffer[this_one])
8334 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8335 echo_area_buffer[this_one] = Qnil;
8336 }
8337
8338 /* Choose a suitable buffer from echo_buffer[] is we don't
8339 have one. */
8340 if (NILP (echo_area_buffer[this_one]))
8341 {
8342 echo_area_buffer[this_one]
8343 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8344 ? echo_buffer[the_other]
8345 : echo_buffer[this_one]);
8346 clear_buffer_p = 1;
8347 }
8348
8349 buffer = echo_area_buffer[this_one];
8350
8351 /* Don't get confused by reusing the buffer used for echoing
8352 for a different purpose. */
8353 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8354 cancel_echoing ();
8355
8356 record_unwind_protect (unwind_with_echo_area_buffer,
8357 with_echo_area_buffer_unwind_data (w));
8358
8359 /* Make the echo area buffer current. Note that for display
8360 purposes, it is not necessary that the displayed window's buffer
8361 == current_buffer, except for text property lookup. So, let's
8362 only set that buffer temporarily here without doing a full
8363 Fset_window_buffer. We must also change w->pointm, though,
8364 because otherwise an assertions in unshow_buffer fails, and Emacs
8365 aborts. */
8366 set_buffer_internal_1 (XBUFFER (buffer));
8367 if (w)
8368 {
8369 w->buffer = buffer;
8370 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8371 }
8372
8373 current_buffer->undo_list = Qt;
8374 current_buffer->read_only = Qnil;
8375 specbind (Qinhibit_read_only, Qt);
8376 specbind (Qinhibit_modification_hooks, Qt);
8377
8378 if (clear_buffer_p && Z > BEG)
8379 del_range (BEG, Z);
8380
8381 xassert (BEGV >= BEG);
8382 xassert (ZV <= Z && ZV >= BEGV);
8383
8384 rc = fn (a1, a2, a3, a4);
8385
8386 xassert (BEGV >= BEG);
8387 xassert (ZV <= Z && ZV >= BEGV);
8388
8389 unbind_to (count, Qnil);
8390 return rc;
8391 }
8392
8393
8394 /* Save state that should be preserved around the call to the function
8395 FN called in with_echo_area_buffer. */
8396
8397 static Lisp_Object
8398 with_echo_area_buffer_unwind_data (w)
8399 struct window *w;
8400 {
8401 int i = 0;
8402 Lisp_Object vector, tmp;
8403
8404 /* Reduce consing by keeping one vector in
8405 Vwith_echo_area_save_vector. */
8406 vector = Vwith_echo_area_save_vector;
8407 Vwith_echo_area_save_vector = Qnil;
8408
8409 if (NILP (vector))
8410 vector = Fmake_vector (make_number (7), Qnil);
8411
8412 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8413 ASET (vector, i, Vdeactivate_mark); ++i;
8414 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8415
8416 if (w)
8417 {
8418 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8419 ASET (vector, i, w->buffer); ++i;
8420 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8421 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8422 }
8423 else
8424 {
8425 int end = i + 4;
8426 for (; i < end; ++i)
8427 ASET (vector, i, Qnil);
8428 }
8429
8430 xassert (i == ASIZE (vector));
8431 return vector;
8432 }
8433
8434
8435 /* Restore global state from VECTOR which was created by
8436 with_echo_area_buffer_unwind_data. */
8437
8438 static Lisp_Object
8439 unwind_with_echo_area_buffer (vector)
8440 Lisp_Object vector;
8441 {
8442 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8443 Vdeactivate_mark = AREF (vector, 1);
8444 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8445
8446 if (WINDOWP (AREF (vector, 3)))
8447 {
8448 struct window *w;
8449 Lisp_Object buffer, charpos, bytepos;
8450
8451 w = XWINDOW (AREF (vector, 3));
8452 buffer = AREF (vector, 4);
8453 charpos = AREF (vector, 5);
8454 bytepos = AREF (vector, 6);
8455
8456 w->buffer = buffer;
8457 set_marker_both (w->pointm, buffer,
8458 XFASTINT (charpos), XFASTINT (bytepos));
8459 }
8460
8461 Vwith_echo_area_save_vector = vector;
8462 return Qnil;
8463 }
8464
8465
8466 /* Set up the echo area for use by print functions. MULTIBYTE_P
8467 non-zero means we will print multibyte. */
8468
8469 void
8470 setup_echo_area_for_printing (multibyte_p)
8471 int multibyte_p;
8472 {
8473 /* If we can't find an echo area any more, exit. */
8474 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8475 Fkill_emacs (Qnil);
8476
8477 ensure_echo_area_buffers ();
8478
8479 if (!message_buf_print)
8480 {
8481 /* A message has been output since the last time we printed.
8482 Choose a fresh echo area buffer. */
8483 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8484 echo_area_buffer[0] = echo_buffer[1];
8485 else
8486 echo_area_buffer[0] = echo_buffer[0];
8487
8488 /* Switch to that buffer and clear it. */
8489 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8490 current_buffer->truncate_lines = Qnil;
8491
8492 if (Z > BEG)
8493 {
8494 int count = SPECPDL_INDEX ();
8495 specbind (Qinhibit_read_only, Qt);
8496 /* Note that undo recording is always disabled. */
8497 del_range (BEG, Z);
8498 unbind_to (count, Qnil);
8499 }
8500 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8501
8502 /* Set up the buffer for the multibyteness we need. */
8503 if (multibyte_p
8504 != !NILP (current_buffer->enable_multibyte_characters))
8505 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8506
8507 /* Raise the frame containing the echo area. */
8508 if (minibuffer_auto_raise)
8509 {
8510 struct frame *sf = SELECTED_FRAME ();
8511 Lisp_Object mini_window;
8512 mini_window = FRAME_MINIBUF_WINDOW (sf);
8513 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8514 }
8515
8516 message_log_maybe_newline ();
8517 message_buf_print = 1;
8518 }
8519 else
8520 {
8521 if (NILP (echo_area_buffer[0]))
8522 {
8523 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8524 echo_area_buffer[0] = echo_buffer[1];
8525 else
8526 echo_area_buffer[0] = echo_buffer[0];
8527 }
8528
8529 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8530 {
8531 /* Someone switched buffers between print requests. */
8532 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8533 current_buffer->truncate_lines = Qnil;
8534 }
8535 }
8536 }
8537
8538
8539 /* Display an echo area message in window W. Value is non-zero if W's
8540 height is changed. If display_last_displayed_message_p is
8541 non-zero, display the message that was last displayed, otherwise
8542 display the current message. */
8543
8544 static int
8545 display_echo_area (w)
8546 struct window *w;
8547 {
8548 int i, no_message_p, window_height_changed_p, count;
8549
8550 /* Temporarily disable garbage collections while displaying the echo
8551 area. This is done because a GC can print a message itself.
8552 That message would modify the echo area buffer's contents while a
8553 redisplay of the buffer is going on, and seriously confuse
8554 redisplay. */
8555 count = inhibit_garbage_collection ();
8556
8557 /* If there is no message, we must call display_echo_area_1
8558 nevertheless because it resizes the window. But we will have to
8559 reset the echo_area_buffer in question to nil at the end because
8560 with_echo_area_buffer will sets it to an empty buffer. */
8561 i = display_last_displayed_message_p ? 1 : 0;
8562 no_message_p = NILP (echo_area_buffer[i]);
8563
8564 window_height_changed_p
8565 = with_echo_area_buffer (w, display_last_displayed_message_p,
8566 display_echo_area_1,
8567 (EMACS_INT) w, Qnil, 0, 0);
8568
8569 if (no_message_p)
8570 echo_area_buffer[i] = Qnil;
8571
8572 unbind_to (count, Qnil);
8573 return window_height_changed_p;
8574 }
8575
8576
8577 /* Helper for display_echo_area. Display the current buffer which
8578 contains the current echo area message in window W, a mini-window,
8579 a pointer to which is passed in A1. A2..A4 are currently not used.
8580 Change the height of W so that all of the message is displayed.
8581 Value is non-zero if height of W was changed. */
8582
8583 static int
8584 display_echo_area_1 (a1, a2, a3, a4)
8585 EMACS_INT a1;
8586 Lisp_Object a2;
8587 EMACS_INT a3, a4;
8588 {
8589 struct window *w = (struct window *) a1;
8590 Lisp_Object window;
8591 struct text_pos start;
8592 int window_height_changed_p = 0;
8593
8594 /* Do this before displaying, so that we have a large enough glyph
8595 matrix for the display. If we can't get enough space for the
8596 whole text, display the last N lines. That works by setting w->start. */
8597 window_height_changed_p = resize_mini_window (w, 0);
8598
8599 /* Use the starting position chosen by resize_mini_window. */
8600 SET_TEXT_POS_FROM_MARKER (start, w->start);
8601
8602 /* Display. */
8603 clear_glyph_matrix (w->desired_matrix);
8604 XSETWINDOW (window, w);
8605 try_window (window, start, 0);
8606
8607 return window_height_changed_p;
8608 }
8609
8610
8611 /* Resize the echo area window to exactly the size needed for the
8612 currently displayed message, if there is one. If a mini-buffer
8613 is active, don't shrink it. */
8614
8615 void
8616 resize_echo_area_exactly ()
8617 {
8618 if (BUFFERP (echo_area_buffer[0])
8619 && WINDOWP (echo_area_window))
8620 {
8621 struct window *w = XWINDOW (echo_area_window);
8622 int resized_p;
8623 Lisp_Object resize_exactly;
8624
8625 if (minibuf_level == 0)
8626 resize_exactly = Qt;
8627 else
8628 resize_exactly = Qnil;
8629
8630 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8631 (EMACS_INT) w, resize_exactly, 0, 0);
8632 if (resized_p)
8633 {
8634 ++windows_or_buffers_changed;
8635 ++update_mode_lines;
8636 redisplay_internal (0);
8637 }
8638 }
8639 }
8640
8641
8642 /* Callback function for with_echo_area_buffer, when used from
8643 resize_echo_area_exactly. A1 contains a pointer to the window to
8644 resize, EXACTLY non-nil means resize the mini-window exactly to the
8645 size of the text displayed. A3 and A4 are not used. Value is what
8646 resize_mini_window returns. */
8647
8648 static int
8649 resize_mini_window_1 (a1, exactly, a3, a4)
8650 EMACS_INT a1;
8651 Lisp_Object exactly;
8652 EMACS_INT a3, a4;
8653 {
8654 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8655 }
8656
8657
8658 /* Resize mini-window W to fit the size of its contents. EXACT_P
8659 means size the window exactly to the size needed. Otherwise, it's
8660 only enlarged until W's buffer is empty.
8661
8662 Set W->start to the right place to begin display. If the whole
8663 contents fit, start at the beginning. Otherwise, start so as
8664 to make the end of the contents appear. This is particularly
8665 important for y-or-n-p, but seems desirable generally.
8666
8667 Value is non-zero if the window height has been changed. */
8668
8669 int
8670 resize_mini_window (w, exact_p)
8671 struct window *w;
8672 int exact_p;
8673 {
8674 struct frame *f = XFRAME (w->frame);
8675 int window_height_changed_p = 0;
8676
8677 xassert (MINI_WINDOW_P (w));
8678
8679 /* By default, start display at the beginning. */
8680 set_marker_both (w->start, w->buffer,
8681 BUF_BEGV (XBUFFER (w->buffer)),
8682 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8683
8684 /* Don't resize windows while redisplaying a window; it would
8685 confuse redisplay functions when the size of the window they are
8686 displaying changes from under them. Such a resizing can happen,
8687 for instance, when which-func prints a long message while
8688 we are running fontification-functions. We're running these
8689 functions with safe_call which binds inhibit-redisplay to t. */
8690 if (!NILP (Vinhibit_redisplay))
8691 return 0;
8692
8693 /* Nil means don't try to resize. */
8694 if (NILP (Vresize_mini_windows)
8695 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8696 return 0;
8697
8698 if (!FRAME_MINIBUF_ONLY_P (f))
8699 {
8700 struct it it;
8701 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8702 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8703 int height, max_height;
8704 int unit = FRAME_LINE_HEIGHT (f);
8705 struct text_pos start;
8706 struct buffer *old_current_buffer = NULL;
8707
8708 if (current_buffer != XBUFFER (w->buffer))
8709 {
8710 old_current_buffer = current_buffer;
8711 set_buffer_internal (XBUFFER (w->buffer));
8712 }
8713
8714 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8715
8716 /* Compute the max. number of lines specified by the user. */
8717 if (FLOATP (Vmax_mini_window_height))
8718 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8719 else if (INTEGERP (Vmax_mini_window_height))
8720 max_height = XINT (Vmax_mini_window_height);
8721 else
8722 max_height = total_height / 4;
8723
8724 /* Correct that max. height if it's bogus. */
8725 max_height = max (1, max_height);
8726 max_height = min (total_height, max_height);
8727
8728 /* Find out the height of the text in the window. */
8729 if (it.line_wrap == TRUNCATE)
8730 height = 1;
8731 else
8732 {
8733 last_height = 0;
8734 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8735 if (it.max_ascent == 0 && it.max_descent == 0)
8736 height = it.current_y + last_height;
8737 else
8738 height = it.current_y + it.max_ascent + it.max_descent;
8739 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8740 height = (height + unit - 1) / unit;
8741 }
8742
8743 /* Compute a suitable window start. */
8744 if (height > max_height)
8745 {
8746 height = max_height;
8747 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8748 move_it_vertically_backward (&it, (height - 1) * unit);
8749 start = it.current.pos;
8750 }
8751 else
8752 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8753 SET_MARKER_FROM_TEXT_POS (w->start, start);
8754
8755 if (EQ (Vresize_mini_windows, Qgrow_only))
8756 {
8757 /* Let it grow only, until we display an empty message, in which
8758 case the window shrinks again. */
8759 if (height > WINDOW_TOTAL_LINES (w))
8760 {
8761 int old_height = WINDOW_TOTAL_LINES (w);
8762 freeze_window_starts (f, 1);
8763 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8764 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8765 }
8766 else if (height < WINDOW_TOTAL_LINES (w)
8767 && (exact_p || BEGV == ZV))
8768 {
8769 int old_height = WINDOW_TOTAL_LINES (w);
8770 freeze_window_starts (f, 0);
8771 shrink_mini_window (w);
8772 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8773 }
8774 }
8775 else
8776 {
8777 /* Always resize to exact size needed. */
8778 if (height > WINDOW_TOTAL_LINES (w))
8779 {
8780 int old_height = WINDOW_TOTAL_LINES (w);
8781 freeze_window_starts (f, 1);
8782 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8783 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8784 }
8785 else if (height < WINDOW_TOTAL_LINES (w))
8786 {
8787 int old_height = WINDOW_TOTAL_LINES (w);
8788 freeze_window_starts (f, 0);
8789 shrink_mini_window (w);
8790
8791 if (height)
8792 {
8793 freeze_window_starts (f, 1);
8794 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8795 }
8796
8797 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8798 }
8799 }
8800
8801 if (old_current_buffer)
8802 set_buffer_internal (old_current_buffer);
8803 }
8804
8805 return window_height_changed_p;
8806 }
8807
8808
8809 /* Value is the current message, a string, or nil if there is no
8810 current message. */
8811
8812 Lisp_Object
8813 current_message ()
8814 {
8815 Lisp_Object msg;
8816
8817 if (!BUFFERP (echo_area_buffer[0]))
8818 msg = Qnil;
8819 else
8820 {
8821 with_echo_area_buffer (0, 0, current_message_1,
8822 (EMACS_INT) &msg, Qnil, 0, 0);
8823 if (NILP (msg))
8824 echo_area_buffer[0] = Qnil;
8825 }
8826
8827 return msg;
8828 }
8829
8830
8831 static int
8832 current_message_1 (a1, a2, a3, a4)
8833 EMACS_INT a1;
8834 Lisp_Object a2;
8835 EMACS_INT a3, a4;
8836 {
8837 Lisp_Object *msg = (Lisp_Object *) a1;
8838
8839 if (Z > BEG)
8840 *msg = make_buffer_string (BEG, Z, 1);
8841 else
8842 *msg = Qnil;
8843 return 0;
8844 }
8845
8846
8847 /* Push the current message on Vmessage_stack for later restauration
8848 by restore_message. Value is non-zero if the current message isn't
8849 empty. This is a relatively infrequent operation, so it's not
8850 worth optimizing. */
8851
8852 int
8853 push_message ()
8854 {
8855 Lisp_Object msg;
8856 msg = current_message ();
8857 Vmessage_stack = Fcons (msg, Vmessage_stack);
8858 return STRINGP (msg);
8859 }
8860
8861
8862 /* Restore message display from the top of Vmessage_stack. */
8863
8864 void
8865 restore_message ()
8866 {
8867 Lisp_Object msg;
8868
8869 xassert (CONSP (Vmessage_stack));
8870 msg = XCAR (Vmessage_stack);
8871 if (STRINGP (msg))
8872 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8873 else
8874 message3_nolog (msg, 0, 0);
8875 }
8876
8877
8878 /* Handler for record_unwind_protect calling pop_message. */
8879
8880 Lisp_Object
8881 pop_message_unwind (dummy)
8882 Lisp_Object dummy;
8883 {
8884 pop_message ();
8885 return Qnil;
8886 }
8887
8888 /* Pop the top-most entry off Vmessage_stack. */
8889
8890 void
8891 pop_message ()
8892 {
8893 xassert (CONSP (Vmessage_stack));
8894 Vmessage_stack = XCDR (Vmessage_stack);
8895 }
8896
8897
8898 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8899 exits. If the stack is not empty, we have a missing pop_message
8900 somewhere. */
8901
8902 void
8903 check_message_stack ()
8904 {
8905 if (!NILP (Vmessage_stack))
8906 abort ();
8907 }
8908
8909
8910 /* Truncate to NCHARS what will be displayed in the echo area the next
8911 time we display it---but don't redisplay it now. */
8912
8913 void
8914 truncate_echo_area (nchars)
8915 int nchars;
8916 {
8917 if (nchars == 0)
8918 echo_area_buffer[0] = Qnil;
8919 /* A null message buffer means that the frame hasn't really been
8920 initialized yet. Error messages get reported properly by
8921 cmd_error, so this must be just an informative message; toss it. */
8922 else if (!noninteractive
8923 && INTERACTIVE
8924 && !NILP (echo_area_buffer[0]))
8925 {
8926 struct frame *sf = SELECTED_FRAME ();
8927 if (FRAME_MESSAGE_BUF (sf))
8928 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8929 }
8930 }
8931
8932
8933 /* Helper function for truncate_echo_area. Truncate the current
8934 message to at most NCHARS characters. */
8935
8936 static int
8937 truncate_message_1 (nchars, a2, a3, a4)
8938 EMACS_INT nchars;
8939 Lisp_Object a2;
8940 EMACS_INT a3, a4;
8941 {
8942 if (BEG + nchars < Z)
8943 del_range (BEG + nchars, Z);
8944 if (Z == BEG)
8945 echo_area_buffer[0] = Qnil;
8946 return 0;
8947 }
8948
8949
8950 /* Set the current message to a substring of S or STRING.
8951
8952 If STRING is a Lisp string, set the message to the first NBYTES
8953 bytes from STRING. NBYTES zero means use the whole string. If
8954 STRING is multibyte, the message will be displayed multibyte.
8955
8956 If S is not null, set the message to the first LEN bytes of S. LEN
8957 zero means use the whole string. MULTIBYTE_P non-zero means S is
8958 multibyte. Display the message multibyte in that case.
8959
8960 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8961 to t before calling set_message_1 (which calls insert).
8962 */
8963
8964 void
8965 set_message (s, string, nbytes, multibyte_p)
8966 const char *s;
8967 Lisp_Object string;
8968 int nbytes, multibyte_p;
8969 {
8970 message_enable_multibyte
8971 = ((s && multibyte_p)
8972 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8973
8974 with_echo_area_buffer (0, -1, set_message_1,
8975 (EMACS_INT) s, string, nbytes, multibyte_p);
8976 message_buf_print = 0;
8977 help_echo_showing_p = 0;
8978 }
8979
8980
8981 /* Helper function for set_message. Arguments have the same meaning
8982 as there, with A1 corresponding to S and A2 corresponding to STRING
8983 This function is called with the echo area buffer being
8984 current. */
8985
8986 static int
8987 set_message_1 (a1, a2, nbytes, multibyte_p)
8988 EMACS_INT a1;
8989 Lisp_Object a2;
8990 EMACS_INT nbytes, multibyte_p;
8991 {
8992 const char *s = (const char *) a1;
8993 Lisp_Object string = a2;
8994
8995 /* Change multibyteness of the echo buffer appropriately. */
8996 if (message_enable_multibyte
8997 != !NILP (current_buffer->enable_multibyte_characters))
8998 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8999
9000 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9001
9002 /* Insert new message at BEG. */
9003 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9004
9005 if (STRINGP (string))
9006 {
9007 int nchars;
9008
9009 if (nbytes == 0)
9010 nbytes = SBYTES (string);
9011 nchars = string_byte_to_char (string, nbytes);
9012
9013 /* This function takes care of single/multibyte conversion. We
9014 just have to ensure that the echo area buffer has the right
9015 setting of enable_multibyte_characters. */
9016 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9017 }
9018 else if (s)
9019 {
9020 if (nbytes == 0)
9021 nbytes = strlen (s);
9022
9023 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9024 {
9025 /* Convert from multi-byte to single-byte. */
9026 int i, c, n;
9027 unsigned char work[1];
9028
9029 /* Convert a multibyte string to single-byte. */
9030 for (i = 0; i < nbytes; i += n)
9031 {
9032 c = string_char_and_length (s + i, nbytes - i, &n);
9033 work[0] = (ASCII_CHAR_P (c)
9034 ? c
9035 : multibyte_char_to_unibyte (c, Qnil));
9036 insert_1_both (work, 1, 1, 1, 0, 0);
9037 }
9038 }
9039 else if (!multibyte_p
9040 && !NILP (current_buffer->enable_multibyte_characters))
9041 {
9042 /* Convert from single-byte to multi-byte. */
9043 int i, c, n;
9044 const unsigned char *msg = (const unsigned char *) s;
9045 unsigned char str[MAX_MULTIBYTE_LENGTH];
9046
9047 /* Convert a single-byte string to multibyte. */
9048 for (i = 0; i < nbytes; i++)
9049 {
9050 c = msg[i];
9051 c = unibyte_char_to_multibyte (c);
9052 n = CHAR_STRING (c, str);
9053 insert_1_both (str, 1, n, 1, 0, 0);
9054 }
9055 }
9056 else
9057 insert_1 (s, nbytes, 1, 0, 0);
9058 }
9059
9060 return 0;
9061 }
9062
9063
9064 /* Clear messages. CURRENT_P non-zero means clear the current
9065 message. LAST_DISPLAYED_P non-zero means clear the message
9066 last displayed. */
9067
9068 void
9069 clear_message (current_p, last_displayed_p)
9070 int current_p, last_displayed_p;
9071 {
9072 if (current_p)
9073 {
9074 echo_area_buffer[0] = Qnil;
9075 message_cleared_p = 1;
9076 }
9077
9078 if (last_displayed_p)
9079 echo_area_buffer[1] = Qnil;
9080
9081 message_buf_print = 0;
9082 }
9083
9084 /* Clear garbaged frames.
9085
9086 This function is used where the old redisplay called
9087 redraw_garbaged_frames which in turn called redraw_frame which in
9088 turn called clear_frame. The call to clear_frame was a source of
9089 flickering. I believe a clear_frame is not necessary. It should
9090 suffice in the new redisplay to invalidate all current matrices,
9091 and ensure a complete redisplay of all windows. */
9092
9093 static void
9094 clear_garbaged_frames ()
9095 {
9096 if (frame_garbaged)
9097 {
9098 Lisp_Object tail, frame;
9099 int changed_count = 0;
9100
9101 FOR_EACH_FRAME (tail, frame)
9102 {
9103 struct frame *f = XFRAME (frame);
9104
9105 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9106 {
9107 if (f->resized_p)
9108 {
9109 Fredraw_frame (frame);
9110 f->force_flush_display_p = 1;
9111 }
9112 clear_current_matrices (f);
9113 changed_count++;
9114 f->garbaged = 0;
9115 f->resized_p = 0;
9116 }
9117 }
9118
9119 frame_garbaged = 0;
9120 if (changed_count)
9121 ++windows_or_buffers_changed;
9122 }
9123 }
9124
9125
9126 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9127 is non-zero update selected_frame. Value is non-zero if the
9128 mini-windows height has been changed. */
9129
9130 static int
9131 echo_area_display (update_frame_p)
9132 int update_frame_p;
9133 {
9134 Lisp_Object mini_window;
9135 struct window *w;
9136 struct frame *f;
9137 int window_height_changed_p = 0;
9138 struct frame *sf = SELECTED_FRAME ();
9139
9140 mini_window = FRAME_MINIBUF_WINDOW (sf);
9141 w = XWINDOW (mini_window);
9142 f = XFRAME (WINDOW_FRAME (w));
9143
9144 /* Don't display if frame is invisible or not yet initialized. */
9145 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9146 return 0;
9147
9148 #ifdef HAVE_WINDOW_SYSTEM
9149 /* When Emacs starts, selected_frame may be the initial terminal
9150 frame. If we let this through, a message would be displayed on
9151 the terminal. */
9152 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9153 return 0;
9154 #endif /* HAVE_WINDOW_SYSTEM */
9155
9156 /* Redraw garbaged frames. */
9157 if (frame_garbaged)
9158 clear_garbaged_frames ();
9159
9160 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9161 {
9162 echo_area_window = mini_window;
9163 window_height_changed_p = display_echo_area (w);
9164 w->must_be_updated_p = 1;
9165
9166 /* Update the display, unless called from redisplay_internal.
9167 Also don't update the screen during redisplay itself. The
9168 update will happen at the end of redisplay, and an update
9169 here could cause confusion. */
9170 if (update_frame_p && !redisplaying_p)
9171 {
9172 int n = 0;
9173
9174 /* If the display update has been interrupted by pending
9175 input, update mode lines in the frame. Due to the
9176 pending input, it might have been that redisplay hasn't
9177 been called, so that mode lines above the echo area are
9178 garbaged. This looks odd, so we prevent it here. */
9179 if (!display_completed)
9180 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9181
9182 if (window_height_changed_p
9183 /* Don't do this if Emacs is shutting down. Redisplay
9184 needs to run hooks. */
9185 && !NILP (Vrun_hooks))
9186 {
9187 /* Must update other windows. Likewise as in other
9188 cases, don't let this update be interrupted by
9189 pending input. */
9190 int count = SPECPDL_INDEX ();
9191 specbind (Qredisplay_dont_pause, Qt);
9192 windows_or_buffers_changed = 1;
9193 redisplay_internal (0);
9194 unbind_to (count, Qnil);
9195 }
9196 else if (FRAME_WINDOW_P (f) && n == 0)
9197 {
9198 /* Window configuration is the same as before.
9199 Can do with a display update of the echo area,
9200 unless we displayed some mode lines. */
9201 update_single_window (w, 1);
9202 FRAME_RIF (f)->flush_display (f);
9203 }
9204 else
9205 update_frame (f, 1, 1);
9206
9207 /* If cursor is in the echo area, make sure that the next
9208 redisplay displays the minibuffer, so that the cursor will
9209 be replaced with what the minibuffer wants. */
9210 if (cursor_in_echo_area)
9211 ++windows_or_buffers_changed;
9212 }
9213 }
9214 else if (!EQ (mini_window, selected_window))
9215 windows_or_buffers_changed++;
9216
9217 /* Last displayed message is now the current message. */
9218 echo_area_buffer[1] = echo_area_buffer[0];
9219 /* Inform read_char that we're not echoing. */
9220 echo_message_buffer = Qnil;
9221
9222 /* Prevent redisplay optimization in redisplay_internal by resetting
9223 this_line_start_pos. This is done because the mini-buffer now
9224 displays the message instead of its buffer text. */
9225 if (EQ (mini_window, selected_window))
9226 CHARPOS (this_line_start_pos) = 0;
9227
9228 return window_height_changed_p;
9229 }
9230
9231
9232 \f
9233 /***********************************************************************
9234 Mode Lines and Frame Titles
9235 ***********************************************************************/
9236
9237 /* A buffer for constructing non-propertized mode-line strings and
9238 frame titles in it; allocated from the heap in init_xdisp and
9239 resized as needed in store_mode_line_noprop_char. */
9240
9241 static char *mode_line_noprop_buf;
9242
9243 /* The buffer's end, and a current output position in it. */
9244
9245 static char *mode_line_noprop_buf_end;
9246 static char *mode_line_noprop_ptr;
9247
9248 #define MODE_LINE_NOPROP_LEN(start) \
9249 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9250
9251 static enum {
9252 MODE_LINE_DISPLAY = 0,
9253 MODE_LINE_TITLE,
9254 MODE_LINE_NOPROP,
9255 MODE_LINE_STRING
9256 } mode_line_target;
9257
9258 /* Alist that caches the results of :propertize.
9259 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9260 static Lisp_Object mode_line_proptrans_alist;
9261
9262 /* List of strings making up the mode-line. */
9263 static Lisp_Object mode_line_string_list;
9264
9265 /* Base face property when building propertized mode line string. */
9266 static Lisp_Object mode_line_string_face;
9267 static Lisp_Object mode_line_string_face_prop;
9268
9269
9270 /* Unwind data for mode line strings */
9271
9272 static Lisp_Object Vmode_line_unwind_vector;
9273
9274 static Lisp_Object
9275 format_mode_line_unwind_data (struct buffer *obuf,
9276 Lisp_Object owin,
9277 int save_proptrans)
9278 {
9279 Lisp_Object vector, tmp;
9280
9281 /* Reduce consing by keeping one vector in
9282 Vwith_echo_area_save_vector. */
9283 vector = Vmode_line_unwind_vector;
9284 Vmode_line_unwind_vector = Qnil;
9285
9286 if (NILP (vector))
9287 vector = Fmake_vector (make_number (8), Qnil);
9288
9289 ASET (vector, 0, make_number (mode_line_target));
9290 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9291 ASET (vector, 2, mode_line_string_list);
9292 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9293 ASET (vector, 4, mode_line_string_face);
9294 ASET (vector, 5, mode_line_string_face_prop);
9295
9296 if (obuf)
9297 XSETBUFFER (tmp, obuf);
9298 else
9299 tmp = Qnil;
9300 ASET (vector, 6, tmp);
9301 ASET (vector, 7, owin);
9302
9303 return vector;
9304 }
9305
9306 static Lisp_Object
9307 unwind_format_mode_line (vector)
9308 Lisp_Object vector;
9309 {
9310 mode_line_target = XINT (AREF (vector, 0));
9311 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9312 mode_line_string_list = AREF (vector, 2);
9313 if (! EQ (AREF (vector, 3), Qt))
9314 mode_line_proptrans_alist = AREF (vector, 3);
9315 mode_line_string_face = AREF (vector, 4);
9316 mode_line_string_face_prop = AREF (vector, 5);
9317
9318 if (!NILP (AREF (vector, 7)))
9319 /* Select window before buffer, since it may change the buffer. */
9320 Fselect_window (AREF (vector, 7), Qt);
9321
9322 if (!NILP (AREF (vector, 6)))
9323 {
9324 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9325 ASET (vector, 6, Qnil);
9326 }
9327
9328 Vmode_line_unwind_vector = vector;
9329 return Qnil;
9330 }
9331
9332
9333 /* Store a single character C for the frame title in mode_line_noprop_buf.
9334 Re-allocate mode_line_noprop_buf if necessary. */
9335
9336 static void
9337 #ifdef PROTOTYPES
9338 store_mode_line_noprop_char (char c)
9339 #else
9340 store_mode_line_noprop_char (c)
9341 char c;
9342 #endif
9343 {
9344 /* If output position has reached the end of the allocated buffer,
9345 double the buffer's size. */
9346 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9347 {
9348 int len = MODE_LINE_NOPROP_LEN (0);
9349 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9350 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9351 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9352 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9353 }
9354
9355 *mode_line_noprop_ptr++ = c;
9356 }
9357
9358
9359 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9360 mode_line_noprop_ptr. STR is the string to store. Do not copy
9361 characters that yield more columns than PRECISION; PRECISION <= 0
9362 means copy the whole string. Pad with spaces until FIELD_WIDTH
9363 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9364 pad. Called from display_mode_element when it is used to build a
9365 frame title. */
9366
9367 static int
9368 store_mode_line_noprop (str, field_width, precision)
9369 const unsigned char *str;
9370 int field_width, precision;
9371 {
9372 int n = 0;
9373 int dummy, nbytes;
9374
9375 /* Copy at most PRECISION chars from STR. */
9376 nbytes = strlen (str);
9377 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9378 while (nbytes--)
9379 store_mode_line_noprop_char (*str++);
9380
9381 /* Fill up with spaces until FIELD_WIDTH reached. */
9382 while (field_width > 0
9383 && n < field_width)
9384 {
9385 store_mode_line_noprop_char (' ');
9386 ++n;
9387 }
9388
9389 return n;
9390 }
9391
9392 /***********************************************************************
9393 Frame Titles
9394 ***********************************************************************/
9395
9396 #ifdef HAVE_WINDOW_SYSTEM
9397
9398 /* Set the title of FRAME, if it has changed. The title format is
9399 Vicon_title_format if FRAME is iconified, otherwise it is
9400 frame_title_format. */
9401
9402 static void
9403 x_consider_frame_title (frame)
9404 Lisp_Object frame;
9405 {
9406 struct frame *f = XFRAME (frame);
9407
9408 if (FRAME_WINDOW_P (f)
9409 || FRAME_MINIBUF_ONLY_P (f)
9410 || f->explicit_name)
9411 {
9412 /* Do we have more than one visible frame on this X display? */
9413 Lisp_Object tail;
9414 Lisp_Object fmt;
9415 int title_start;
9416 char *title;
9417 int len;
9418 struct it it;
9419 int count = SPECPDL_INDEX ();
9420
9421 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9422 {
9423 Lisp_Object other_frame = XCAR (tail);
9424 struct frame *tf = XFRAME (other_frame);
9425
9426 if (tf != f
9427 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9428 && !FRAME_MINIBUF_ONLY_P (tf)
9429 && !EQ (other_frame, tip_frame)
9430 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9431 break;
9432 }
9433
9434 /* Set global variable indicating that multiple frames exist. */
9435 multiple_frames = CONSP (tail);
9436
9437 /* Switch to the buffer of selected window of the frame. Set up
9438 mode_line_target so that display_mode_element will output into
9439 mode_line_noprop_buf; then display the title. */
9440 record_unwind_protect (unwind_format_mode_line,
9441 format_mode_line_unwind_data
9442 (current_buffer, selected_window, 0));
9443
9444 Fselect_window (f->selected_window, Qt);
9445 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9446 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9447
9448 mode_line_target = MODE_LINE_TITLE;
9449 title_start = MODE_LINE_NOPROP_LEN (0);
9450 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9451 NULL, DEFAULT_FACE_ID);
9452 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9453 len = MODE_LINE_NOPROP_LEN (title_start);
9454 title = mode_line_noprop_buf + title_start;
9455 unbind_to (count, Qnil);
9456
9457 /* Set the title only if it's changed. This avoids consing in
9458 the common case where it hasn't. (If it turns out that we've
9459 already wasted too much time by walking through the list with
9460 display_mode_element, then we might need to optimize at a
9461 higher level than this.) */
9462 if (! STRINGP (f->name)
9463 || SBYTES (f->name) != len
9464 || bcmp (title, SDATA (f->name), len) != 0)
9465 {
9466 #ifdef HAVE_NS
9467 if (FRAME_NS_P (f))
9468 {
9469 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9470 {
9471 if (EQ (fmt, Qt))
9472 ns_set_name_as_filename (f);
9473 else
9474 x_implicitly_set_name (f, make_string(title, len),
9475 Qnil);
9476 }
9477 }
9478 else
9479 #endif
9480 x_implicitly_set_name (f, make_string (title, len), Qnil);
9481 }
9482 #ifdef HAVE_NS
9483 if (FRAME_NS_P (f))
9484 {
9485 /* do this also for frames with explicit names */
9486 ns_implicitly_set_icon_type(f);
9487 ns_set_doc_edited(f, Fbuffer_modified_p
9488 (XWINDOW (f->selected_window)->buffer), Qnil);
9489 }
9490 #endif
9491 }
9492 }
9493
9494 #endif /* not HAVE_WINDOW_SYSTEM */
9495
9496
9497
9498 \f
9499 /***********************************************************************
9500 Menu Bars
9501 ***********************************************************************/
9502
9503
9504 /* Prepare for redisplay by updating menu-bar item lists when
9505 appropriate. This can call eval. */
9506
9507 void
9508 prepare_menu_bars ()
9509 {
9510 int all_windows;
9511 struct gcpro gcpro1, gcpro2;
9512 struct frame *f;
9513 Lisp_Object tooltip_frame;
9514
9515 #ifdef HAVE_WINDOW_SYSTEM
9516 tooltip_frame = tip_frame;
9517 #else
9518 tooltip_frame = Qnil;
9519 #endif
9520
9521 /* Update all frame titles based on their buffer names, etc. We do
9522 this before the menu bars so that the buffer-menu will show the
9523 up-to-date frame titles. */
9524 #ifdef HAVE_WINDOW_SYSTEM
9525 if (windows_or_buffers_changed || update_mode_lines)
9526 {
9527 Lisp_Object tail, frame;
9528
9529 FOR_EACH_FRAME (tail, frame)
9530 {
9531 f = XFRAME (frame);
9532 if (!EQ (frame, tooltip_frame)
9533 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9534 x_consider_frame_title (frame);
9535 }
9536 }
9537 #endif /* HAVE_WINDOW_SYSTEM */
9538
9539 /* Update the menu bar item lists, if appropriate. This has to be
9540 done before any actual redisplay or generation of display lines. */
9541 all_windows = (update_mode_lines
9542 || buffer_shared > 1
9543 || windows_or_buffers_changed);
9544 if (all_windows)
9545 {
9546 Lisp_Object tail, frame;
9547 int count = SPECPDL_INDEX ();
9548 /* 1 means that update_menu_bar has run its hooks
9549 so any further calls to update_menu_bar shouldn't do so again. */
9550 int menu_bar_hooks_run = 0;
9551
9552 record_unwind_save_match_data ();
9553
9554 FOR_EACH_FRAME (tail, frame)
9555 {
9556 f = XFRAME (frame);
9557
9558 /* Ignore tooltip frame. */
9559 if (EQ (frame, tooltip_frame))
9560 continue;
9561
9562 /* If a window on this frame changed size, report that to
9563 the user and clear the size-change flag. */
9564 if (FRAME_WINDOW_SIZES_CHANGED (f))
9565 {
9566 Lisp_Object functions;
9567
9568 /* Clear flag first in case we get an error below. */
9569 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9570 functions = Vwindow_size_change_functions;
9571 GCPRO2 (tail, functions);
9572
9573 while (CONSP (functions))
9574 {
9575 if (!EQ (XCAR (functions), Qt))
9576 call1 (XCAR (functions), frame);
9577 functions = XCDR (functions);
9578 }
9579 UNGCPRO;
9580 }
9581
9582 GCPRO1 (tail);
9583 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9584 #ifdef HAVE_WINDOW_SYSTEM
9585 update_tool_bar (f, 0);
9586 #endif
9587 UNGCPRO;
9588 }
9589
9590 unbind_to (count, Qnil);
9591 }
9592 else
9593 {
9594 struct frame *sf = SELECTED_FRAME ();
9595 update_menu_bar (sf, 1, 0);
9596 #ifdef HAVE_WINDOW_SYSTEM
9597 update_tool_bar (sf, 1);
9598 #endif
9599 }
9600
9601 /* Motif needs this. See comment in xmenu.c. Turn it off when
9602 pending_menu_activation is not defined. */
9603 #ifdef USE_X_TOOLKIT
9604 pending_menu_activation = 0;
9605 #endif
9606 }
9607
9608
9609 /* Update the menu bar item list for frame F. This has to be done
9610 before we start to fill in any display lines, because it can call
9611 eval.
9612
9613 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9614
9615 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9616 already ran the menu bar hooks for this redisplay, so there
9617 is no need to run them again. The return value is the
9618 updated value of this flag, to pass to the next call. */
9619
9620 static int
9621 update_menu_bar (f, save_match_data, hooks_run)
9622 struct frame *f;
9623 int save_match_data;
9624 int hooks_run;
9625 {
9626 Lisp_Object window;
9627 register struct window *w;
9628
9629 /* If called recursively during a menu update, do nothing. This can
9630 happen when, for instance, an activate-menubar-hook causes a
9631 redisplay. */
9632 if (inhibit_menubar_update)
9633 return hooks_run;
9634
9635 window = FRAME_SELECTED_WINDOW (f);
9636 w = XWINDOW (window);
9637
9638 if (FRAME_WINDOW_P (f)
9639 ?
9640 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9641 || defined (HAVE_NS) || defined (USE_GTK)
9642 FRAME_EXTERNAL_MENU_BAR (f)
9643 #else
9644 FRAME_MENU_BAR_LINES (f) > 0
9645 #endif
9646 : FRAME_MENU_BAR_LINES (f) > 0)
9647 {
9648 /* If the user has switched buffers or windows, we need to
9649 recompute to reflect the new bindings. But we'll
9650 recompute when update_mode_lines is set too; that means
9651 that people can use force-mode-line-update to request
9652 that the menu bar be recomputed. The adverse effect on
9653 the rest of the redisplay algorithm is about the same as
9654 windows_or_buffers_changed anyway. */
9655 if (windows_or_buffers_changed
9656 /* This used to test w->update_mode_line, but we believe
9657 there is no need to recompute the menu in that case. */
9658 || update_mode_lines
9659 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9660 < BUF_MODIFF (XBUFFER (w->buffer)))
9661 != !NILP (w->last_had_star))
9662 || ((!NILP (Vtransient_mark_mode)
9663 && !NILP (XBUFFER (w->buffer)->mark_active))
9664 != !NILP (w->region_showing)))
9665 {
9666 struct buffer *prev = current_buffer;
9667 int count = SPECPDL_INDEX ();
9668
9669 specbind (Qinhibit_menubar_update, Qt);
9670
9671 set_buffer_internal_1 (XBUFFER (w->buffer));
9672 if (save_match_data)
9673 record_unwind_save_match_data ();
9674 if (NILP (Voverriding_local_map_menu_flag))
9675 {
9676 specbind (Qoverriding_terminal_local_map, Qnil);
9677 specbind (Qoverriding_local_map, Qnil);
9678 }
9679
9680 if (!hooks_run)
9681 {
9682 /* Run the Lucid hook. */
9683 safe_run_hooks (Qactivate_menubar_hook);
9684
9685 /* If it has changed current-menubar from previous value,
9686 really recompute the menu-bar from the value. */
9687 if (! NILP (Vlucid_menu_bar_dirty_flag))
9688 call0 (Qrecompute_lucid_menubar);
9689
9690 safe_run_hooks (Qmenu_bar_update_hook);
9691
9692 hooks_run = 1;
9693 }
9694
9695 XSETFRAME (Vmenu_updating_frame, f);
9696 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9697
9698 /* Redisplay the menu bar in case we changed it. */
9699 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9700 || defined (HAVE_NS) || defined (USE_GTK)
9701 if (FRAME_WINDOW_P (f))
9702 {
9703 #if defined (HAVE_NS)
9704 /* All frames on Mac OS share the same menubar. So only
9705 the selected frame should be allowed to set it. */
9706 if (f == SELECTED_FRAME ())
9707 #endif
9708 set_frame_menubar (f, 0, 0);
9709 }
9710 else
9711 /* On a terminal screen, the menu bar is an ordinary screen
9712 line, and this makes it get updated. */
9713 w->update_mode_line = Qt;
9714 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9715 /* In the non-toolkit version, the menu bar is an ordinary screen
9716 line, and this makes it get updated. */
9717 w->update_mode_line = Qt;
9718 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9719
9720 unbind_to (count, Qnil);
9721 set_buffer_internal_1 (prev);
9722 }
9723 }
9724
9725 return hooks_run;
9726 }
9727
9728
9729 \f
9730 /***********************************************************************
9731 Output Cursor
9732 ***********************************************************************/
9733
9734 #ifdef HAVE_WINDOW_SYSTEM
9735
9736 /* EXPORT:
9737 Nominal cursor position -- where to draw output.
9738 HPOS and VPOS are window relative glyph matrix coordinates.
9739 X and Y are window relative pixel coordinates. */
9740
9741 struct cursor_pos output_cursor;
9742
9743
9744 /* EXPORT:
9745 Set the global variable output_cursor to CURSOR. All cursor
9746 positions are relative to updated_window. */
9747
9748 void
9749 set_output_cursor (cursor)
9750 struct cursor_pos *cursor;
9751 {
9752 output_cursor.hpos = cursor->hpos;
9753 output_cursor.vpos = cursor->vpos;
9754 output_cursor.x = cursor->x;
9755 output_cursor.y = cursor->y;
9756 }
9757
9758
9759 /* EXPORT for RIF:
9760 Set a nominal cursor position.
9761
9762 HPOS and VPOS are column/row positions in a window glyph matrix. X
9763 and Y are window text area relative pixel positions.
9764
9765 If this is done during an update, updated_window will contain the
9766 window that is being updated and the position is the future output
9767 cursor position for that window. If updated_window is null, use
9768 selected_window and display the cursor at the given position. */
9769
9770 void
9771 x_cursor_to (vpos, hpos, y, x)
9772 int vpos, hpos, y, x;
9773 {
9774 struct window *w;
9775
9776 /* If updated_window is not set, work on selected_window. */
9777 if (updated_window)
9778 w = updated_window;
9779 else
9780 w = XWINDOW (selected_window);
9781
9782 /* Set the output cursor. */
9783 output_cursor.hpos = hpos;
9784 output_cursor.vpos = vpos;
9785 output_cursor.x = x;
9786 output_cursor.y = y;
9787
9788 /* If not called as part of an update, really display the cursor.
9789 This will also set the cursor position of W. */
9790 if (updated_window == NULL)
9791 {
9792 BLOCK_INPUT;
9793 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9794 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9795 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9796 UNBLOCK_INPUT;
9797 }
9798 }
9799
9800 #endif /* HAVE_WINDOW_SYSTEM */
9801
9802 \f
9803 /***********************************************************************
9804 Tool-bars
9805 ***********************************************************************/
9806
9807 #ifdef HAVE_WINDOW_SYSTEM
9808
9809 /* Where the mouse was last time we reported a mouse event. */
9810
9811 FRAME_PTR last_mouse_frame;
9812
9813 /* Tool-bar item index of the item on which a mouse button was pressed
9814 or -1. */
9815
9816 int last_tool_bar_item;
9817
9818
9819 static Lisp_Object
9820 update_tool_bar_unwind (frame)
9821 Lisp_Object frame;
9822 {
9823 selected_frame = frame;
9824 return Qnil;
9825 }
9826
9827 /* Update the tool-bar item list for frame F. This has to be done
9828 before we start to fill in any display lines. Called from
9829 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9830 and restore it here. */
9831
9832 static void
9833 update_tool_bar (f, save_match_data)
9834 struct frame *f;
9835 int save_match_data;
9836 {
9837 #if defined (USE_GTK) || defined (HAVE_NS)
9838 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9839 #else
9840 int do_update = WINDOWP (f->tool_bar_window)
9841 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9842 #endif
9843
9844 if (do_update)
9845 {
9846 Lisp_Object window;
9847 struct window *w;
9848
9849 window = FRAME_SELECTED_WINDOW (f);
9850 w = XWINDOW (window);
9851
9852 /* If the user has switched buffers or windows, we need to
9853 recompute to reflect the new bindings. But we'll
9854 recompute when update_mode_lines is set too; that means
9855 that people can use force-mode-line-update to request
9856 that the menu bar be recomputed. The adverse effect on
9857 the rest of the redisplay algorithm is about the same as
9858 windows_or_buffers_changed anyway. */
9859 if (windows_or_buffers_changed
9860 || !NILP (w->update_mode_line)
9861 || update_mode_lines
9862 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9863 < BUF_MODIFF (XBUFFER (w->buffer)))
9864 != !NILP (w->last_had_star))
9865 || ((!NILP (Vtransient_mark_mode)
9866 && !NILP (XBUFFER (w->buffer)->mark_active))
9867 != !NILP (w->region_showing)))
9868 {
9869 struct buffer *prev = current_buffer;
9870 int count = SPECPDL_INDEX ();
9871 Lisp_Object frame, new_tool_bar;
9872 int new_n_tool_bar;
9873 struct gcpro gcpro1;
9874
9875 /* Set current_buffer to the buffer of the selected
9876 window of the frame, so that we get the right local
9877 keymaps. */
9878 set_buffer_internal_1 (XBUFFER (w->buffer));
9879
9880 /* Save match data, if we must. */
9881 if (save_match_data)
9882 record_unwind_save_match_data ();
9883
9884 /* Make sure that we don't accidentally use bogus keymaps. */
9885 if (NILP (Voverriding_local_map_menu_flag))
9886 {
9887 specbind (Qoverriding_terminal_local_map, Qnil);
9888 specbind (Qoverriding_local_map, Qnil);
9889 }
9890
9891 GCPRO1 (new_tool_bar);
9892
9893 /* We must temporarily set the selected frame to this frame
9894 before calling tool_bar_items, because the calculation of
9895 the tool-bar keymap uses the selected frame (see
9896 `tool-bar-make-keymap' in tool-bar.el). */
9897 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9898 XSETFRAME (frame, f);
9899 selected_frame = frame;
9900
9901 /* Build desired tool-bar items from keymaps. */
9902 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9903 &new_n_tool_bar);
9904
9905 /* Redisplay the tool-bar if we changed it. */
9906 if (new_n_tool_bar != f->n_tool_bar_items
9907 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9908 {
9909 /* Redisplay that happens asynchronously due to an expose event
9910 may access f->tool_bar_items. Make sure we update both
9911 variables within BLOCK_INPUT so no such event interrupts. */
9912 BLOCK_INPUT;
9913 f->tool_bar_items = new_tool_bar;
9914 f->n_tool_bar_items = new_n_tool_bar;
9915 w->update_mode_line = Qt;
9916 UNBLOCK_INPUT;
9917 }
9918
9919 UNGCPRO;
9920
9921 unbind_to (count, Qnil);
9922 set_buffer_internal_1 (prev);
9923 }
9924 }
9925 }
9926
9927
9928 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9929 F's desired tool-bar contents. F->tool_bar_items must have
9930 been set up previously by calling prepare_menu_bars. */
9931
9932 static void
9933 build_desired_tool_bar_string (f)
9934 struct frame *f;
9935 {
9936 int i, size, size_needed;
9937 struct gcpro gcpro1, gcpro2, gcpro3;
9938 Lisp_Object image, plist, props;
9939
9940 image = plist = props = Qnil;
9941 GCPRO3 (image, plist, props);
9942
9943 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9944 Otherwise, make a new string. */
9945
9946 /* The size of the string we might be able to reuse. */
9947 size = (STRINGP (f->desired_tool_bar_string)
9948 ? SCHARS (f->desired_tool_bar_string)
9949 : 0);
9950
9951 /* We need one space in the string for each image. */
9952 size_needed = f->n_tool_bar_items;
9953
9954 /* Reuse f->desired_tool_bar_string, if possible. */
9955 if (size < size_needed || NILP (f->desired_tool_bar_string))
9956 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9957 make_number (' '));
9958 else
9959 {
9960 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9961 Fremove_text_properties (make_number (0), make_number (size),
9962 props, f->desired_tool_bar_string);
9963 }
9964
9965 /* Put a `display' property on the string for the images to display,
9966 put a `menu_item' property on tool-bar items with a value that
9967 is the index of the item in F's tool-bar item vector. */
9968 for (i = 0; i < f->n_tool_bar_items; ++i)
9969 {
9970 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9971
9972 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9973 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9974 int hmargin, vmargin, relief, idx, end;
9975 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9976
9977 /* If image is a vector, choose the image according to the
9978 button state. */
9979 image = PROP (TOOL_BAR_ITEM_IMAGES);
9980 if (VECTORP (image))
9981 {
9982 if (enabled_p)
9983 idx = (selected_p
9984 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9985 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9986 else
9987 idx = (selected_p
9988 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9989 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9990
9991 xassert (ASIZE (image) >= idx);
9992 image = AREF (image, idx);
9993 }
9994 else
9995 idx = -1;
9996
9997 /* Ignore invalid image specifications. */
9998 if (!valid_image_p (image))
9999 continue;
10000
10001 /* Display the tool-bar button pressed, or depressed. */
10002 plist = Fcopy_sequence (XCDR (image));
10003
10004 /* Compute margin and relief to draw. */
10005 relief = (tool_bar_button_relief >= 0
10006 ? tool_bar_button_relief
10007 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10008 hmargin = vmargin = relief;
10009
10010 if (INTEGERP (Vtool_bar_button_margin)
10011 && XINT (Vtool_bar_button_margin) > 0)
10012 {
10013 hmargin += XFASTINT (Vtool_bar_button_margin);
10014 vmargin += XFASTINT (Vtool_bar_button_margin);
10015 }
10016 else if (CONSP (Vtool_bar_button_margin))
10017 {
10018 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10019 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10020 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10021
10022 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10023 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10024 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10025 }
10026
10027 if (auto_raise_tool_bar_buttons_p)
10028 {
10029 /* Add a `:relief' property to the image spec if the item is
10030 selected. */
10031 if (selected_p)
10032 {
10033 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10034 hmargin -= relief;
10035 vmargin -= relief;
10036 }
10037 }
10038 else
10039 {
10040 /* If image is selected, display it pressed, i.e. with a
10041 negative relief. If it's not selected, display it with a
10042 raised relief. */
10043 plist = Fplist_put (plist, QCrelief,
10044 (selected_p
10045 ? make_number (-relief)
10046 : make_number (relief)));
10047 hmargin -= relief;
10048 vmargin -= relief;
10049 }
10050
10051 /* Put a margin around the image. */
10052 if (hmargin || vmargin)
10053 {
10054 if (hmargin == vmargin)
10055 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10056 else
10057 plist = Fplist_put (plist, QCmargin,
10058 Fcons (make_number (hmargin),
10059 make_number (vmargin)));
10060 }
10061
10062 /* If button is not enabled, and we don't have special images
10063 for the disabled state, make the image appear disabled by
10064 applying an appropriate algorithm to it. */
10065 if (!enabled_p && idx < 0)
10066 plist = Fplist_put (plist, QCconversion, Qdisabled);
10067
10068 /* Put a `display' text property on the string for the image to
10069 display. Put a `menu-item' property on the string that gives
10070 the start of this item's properties in the tool-bar items
10071 vector. */
10072 image = Fcons (Qimage, plist);
10073 props = list4 (Qdisplay, image,
10074 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10075
10076 /* Let the last image hide all remaining spaces in the tool bar
10077 string. The string can be longer than needed when we reuse a
10078 previous string. */
10079 if (i + 1 == f->n_tool_bar_items)
10080 end = SCHARS (f->desired_tool_bar_string);
10081 else
10082 end = i + 1;
10083 Fadd_text_properties (make_number (i), make_number (end),
10084 props, f->desired_tool_bar_string);
10085 #undef PROP
10086 }
10087
10088 UNGCPRO;
10089 }
10090
10091
10092 /* Display one line of the tool-bar of frame IT->f.
10093
10094 HEIGHT specifies the desired height of the tool-bar line.
10095 If the actual height of the glyph row is less than HEIGHT, the
10096 row's height is increased to HEIGHT, and the icons are centered
10097 vertically in the new height.
10098
10099 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10100 count a final empty row in case the tool-bar width exactly matches
10101 the window width.
10102 */
10103
10104 static void
10105 display_tool_bar_line (it, height)
10106 struct it *it;
10107 int height;
10108 {
10109 struct glyph_row *row = it->glyph_row;
10110 int max_x = it->last_visible_x;
10111 struct glyph *last;
10112
10113 prepare_desired_row (row);
10114 row->y = it->current_y;
10115
10116 /* Note that this isn't made use of if the face hasn't a box,
10117 so there's no need to check the face here. */
10118 it->start_of_box_run_p = 1;
10119
10120 while (it->current_x < max_x)
10121 {
10122 int x, n_glyphs_before, i, nglyphs;
10123 struct it it_before;
10124
10125 /* Get the next display element. */
10126 if (!get_next_display_element (it))
10127 {
10128 /* Don't count empty row if we are counting needed tool-bar lines. */
10129 if (height < 0 && !it->hpos)
10130 return;
10131 break;
10132 }
10133
10134 /* Produce glyphs. */
10135 n_glyphs_before = row->used[TEXT_AREA];
10136 it_before = *it;
10137
10138 PRODUCE_GLYPHS (it);
10139
10140 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10141 i = 0;
10142 x = it_before.current_x;
10143 while (i < nglyphs)
10144 {
10145 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10146
10147 if (x + glyph->pixel_width > max_x)
10148 {
10149 /* Glyph doesn't fit on line. Backtrack. */
10150 row->used[TEXT_AREA] = n_glyphs_before;
10151 *it = it_before;
10152 /* If this is the only glyph on this line, it will never fit on the
10153 toolbar, so skip it. But ensure there is at least one glyph,
10154 so we don't accidentally disable the tool-bar. */
10155 if (n_glyphs_before == 0
10156 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10157 break;
10158 goto out;
10159 }
10160
10161 ++it->hpos;
10162 x += glyph->pixel_width;
10163 ++i;
10164 }
10165
10166 /* Stop at line ends. */
10167 if (ITERATOR_AT_END_OF_LINE_P (it))
10168 break;
10169
10170 set_iterator_to_next (it, 1);
10171 }
10172
10173 out:;
10174
10175 row->displays_text_p = row->used[TEXT_AREA] != 0;
10176
10177 /* Use default face for the border below the tool bar.
10178
10179 FIXME: When auto-resize-tool-bars is grow-only, there is
10180 no additional border below the possibly empty tool-bar lines.
10181 So to make the extra empty lines look "normal", we have to
10182 use the tool-bar face for the border too. */
10183 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10184 it->face_id = DEFAULT_FACE_ID;
10185
10186 extend_face_to_end_of_line (it);
10187 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10188 last->right_box_line_p = 1;
10189 if (last == row->glyphs[TEXT_AREA])
10190 last->left_box_line_p = 1;
10191
10192 /* Make line the desired height and center it vertically. */
10193 if ((height -= it->max_ascent + it->max_descent) > 0)
10194 {
10195 /* Don't add more than one line height. */
10196 height %= FRAME_LINE_HEIGHT (it->f);
10197 it->max_ascent += height / 2;
10198 it->max_descent += (height + 1) / 2;
10199 }
10200
10201 compute_line_metrics (it);
10202
10203 /* If line is empty, make it occupy the rest of the tool-bar. */
10204 if (!row->displays_text_p)
10205 {
10206 row->height = row->phys_height = it->last_visible_y - row->y;
10207 row->visible_height = row->height;
10208 row->ascent = row->phys_ascent = 0;
10209 row->extra_line_spacing = 0;
10210 }
10211
10212 row->full_width_p = 1;
10213 row->continued_p = 0;
10214 row->truncated_on_left_p = 0;
10215 row->truncated_on_right_p = 0;
10216
10217 it->current_x = it->hpos = 0;
10218 it->current_y += row->height;
10219 ++it->vpos;
10220 ++it->glyph_row;
10221 }
10222
10223
10224 /* Max tool-bar height. */
10225
10226 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10227 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10228
10229 /* Value is the number of screen lines needed to make all tool-bar
10230 items of frame F visible. The number of actual rows needed is
10231 returned in *N_ROWS if non-NULL. */
10232
10233 static int
10234 tool_bar_lines_needed (f, n_rows)
10235 struct frame *f;
10236 int *n_rows;
10237 {
10238 struct window *w = XWINDOW (f->tool_bar_window);
10239 struct it it;
10240 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10241 the desired matrix, so use (unused) mode-line row as temporary row to
10242 avoid destroying the first tool-bar row. */
10243 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10244
10245 /* Initialize an iterator for iteration over
10246 F->desired_tool_bar_string in the tool-bar window of frame F. */
10247 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10248 it.first_visible_x = 0;
10249 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10250 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10251
10252 while (!ITERATOR_AT_END_P (&it))
10253 {
10254 clear_glyph_row (temp_row);
10255 it.glyph_row = temp_row;
10256 display_tool_bar_line (&it, -1);
10257 }
10258 clear_glyph_row (temp_row);
10259
10260 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10261 if (n_rows)
10262 *n_rows = it.vpos > 0 ? it.vpos : -1;
10263
10264 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10265 }
10266
10267
10268 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10269 0, 1, 0,
10270 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10271 (frame)
10272 Lisp_Object frame;
10273 {
10274 struct frame *f;
10275 struct window *w;
10276 int nlines = 0;
10277
10278 if (NILP (frame))
10279 frame = selected_frame;
10280 else
10281 CHECK_FRAME (frame);
10282 f = XFRAME (frame);
10283
10284 if (WINDOWP (f->tool_bar_window)
10285 || (w = XWINDOW (f->tool_bar_window),
10286 WINDOW_TOTAL_LINES (w) > 0))
10287 {
10288 update_tool_bar (f, 1);
10289 if (f->n_tool_bar_items)
10290 {
10291 build_desired_tool_bar_string (f);
10292 nlines = tool_bar_lines_needed (f, NULL);
10293 }
10294 }
10295
10296 return make_number (nlines);
10297 }
10298
10299
10300 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10301 height should be changed. */
10302
10303 static int
10304 redisplay_tool_bar (f)
10305 struct frame *f;
10306 {
10307 struct window *w;
10308 struct it it;
10309 struct glyph_row *row;
10310
10311 #if defined (USE_GTK) || defined (HAVE_NS)
10312 if (FRAME_EXTERNAL_TOOL_BAR (f))
10313 update_frame_tool_bar (f);
10314 return 0;
10315 #endif
10316
10317 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10318 do anything. This means you must start with tool-bar-lines
10319 non-zero to get the auto-sizing effect. Or in other words, you
10320 can turn off tool-bars by specifying tool-bar-lines zero. */
10321 if (!WINDOWP (f->tool_bar_window)
10322 || (w = XWINDOW (f->tool_bar_window),
10323 WINDOW_TOTAL_LINES (w) == 0))
10324 return 0;
10325
10326 /* Set up an iterator for the tool-bar window. */
10327 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10328 it.first_visible_x = 0;
10329 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10330 row = it.glyph_row;
10331
10332 /* Build a string that represents the contents of the tool-bar. */
10333 build_desired_tool_bar_string (f);
10334 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10335
10336 if (f->n_tool_bar_rows == 0)
10337 {
10338 int nlines;
10339
10340 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10341 nlines != WINDOW_TOTAL_LINES (w)))
10342 {
10343 extern Lisp_Object Qtool_bar_lines;
10344 Lisp_Object frame;
10345 int old_height = WINDOW_TOTAL_LINES (w);
10346
10347 XSETFRAME (frame, f);
10348 Fmodify_frame_parameters (frame,
10349 Fcons (Fcons (Qtool_bar_lines,
10350 make_number (nlines)),
10351 Qnil));
10352 if (WINDOW_TOTAL_LINES (w) != old_height)
10353 {
10354 clear_glyph_matrix (w->desired_matrix);
10355 fonts_changed_p = 1;
10356 return 1;
10357 }
10358 }
10359 }
10360
10361 /* Display as many lines as needed to display all tool-bar items. */
10362
10363 if (f->n_tool_bar_rows > 0)
10364 {
10365 int border, rows, height, extra;
10366
10367 if (INTEGERP (Vtool_bar_border))
10368 border = XINT (Vtool_bar_border);
10369 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10370 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10371 else if (EQ (Vtool_bar_border, Qborder_width))
10372 border = f->border_width;
10373 else
10374 border = 0;
10375 if (border < 0)
10376 border = 0;
10377
10378 rows = f->n_tool_bar_rows;
10379 height = max (1, (it.last_visible_y - border) / rows);
10380 extra = it.last_visible_y - border - height * rows;
10381
10382 while (it.current_y < it.last_visible_y)
10383 {
10384 int h = 0;
10385 if (extra > 0 && rows-- > 0)
10386 {
10387 h = (extra + rows - 1) / rows;
10388 extra -= h;
10389 }
10390 display_tool_bar_line (&it, height + h);
10391 }
10392 }
10393 else
10394 {
10395 while (it.current_y < it.last_visible_y)
10396 display_tool_bar_line (&it, 0);
10397 }
10398
10399 /* It doesn't make much sense to try scrolling in the tool-bar
10400 window, so don't do it. */
10401 w->desired_matrix->no_scrolling_p = 1;
10402 w->must_be_updated_p = 1;
10403
10404 if (!NILP (Vauto_resize_tool_bars))
10405 {
10406 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10407 int change_height_p = 0;
10408
10409 /* If we couldn't display everything, change the tool-bar's
10410 height if there is room for more. */
10411 if (IT_STRING_CHARPOS (it) < it.end_charpos
10412 && it.current_y < max_tool_bar_height)
10413 change_height_p = 1;
10414
10415 row = it.glyph_row - 1;
10416
10417 /* If there are blank lines at the end, except for a partially
10418 visible blank line at the end that is smaller than
10419 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10420 if (!row->displays_text_p
10421 && row->height >= FRAME_LINE_HEIGHT (f))
10422 change_height_p = 1;
10423
10424 /* If row displays tool-bar items, but is partially visible,
10425 change the tool-bar's height. */
10426 if (row->displays_text_p
10427 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10428 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10429 change_height_p = 1;
10430
10431 /* Resize windows as needed by changing the `tool-bar-lines'
10432 frame parameter. */
10433 if (change_height_p)
10434 {
10435 extern Lisp_Object Qtool_bar_lines;
10436 Lisp_Object frame;
10437 int old_height = WINDOW_TOTAL_LINES (w);
10438 int nrows;
10439 int nlines = tool_bar_lines_needed (f, &nrows);
10440
10441 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10442 && !f->minimize_tool_bar_window_p)
10443 ? (nlines > old_height)
10444 : (nlines != old_height));
10445 f->minimize_tool_bar_window_p = 0;
10446
10447 if (change_height_p)
10448 {
10449 XSETFRAME (frame, f);
10450 Fmodify_frame_parameters (frame,
10451 Fcons (Fcons (Qtool_bar_lines,
10452 make_number (nlines)),
10453 Qnil));
10454 if (WINDOW_TOTAL_LINES (w) != old_height)
10455 {
10456 clear_glyph_matrix (w->desired_matrix);
10457 f->n_tool_bar_rows = nrows;
10458 fonts_changed_p = 1;
10459 return 1;
10460 }
10461 }
10462 }
10463 }
10464
10465 f->minimize_tool_bar_window_p = 0;
10466 return 0;
10467 }
10468
10469
10470 /* Get information about the tool-bar item which is displayed in GLYPH
10471 on frame F. Return in *PROP_IDX the index where tool-bar item
10472 properties start in F->tool_bar_items. Value is zero if
10473 GLYPH doesn't display a tool-bar item. */
10474
10475 static int
10476 tool_bar_item_info (f, glyph, prop_idx)
10477 struct frame *f;
10478 struct glyph *glyph;
10479 int *prop_idx;
10480 {
10481 Lisp_Object prop;
10482 int success_p;
10483 int charpos;
10484
10485 /* This function can be called asynchronously, which means we must
10486 exclude any possibility that Fget_text_property signals an
10487 error. */
10488 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10489 charpos = max (0, charpos);
10490
10491 /* Get the text property `menu-item' at pos. The value of that
10492 property is the start index of this item's properties in
10493 F->tool_bar_items. */
10494 prop = Fget_text_property (make_number (charpos),
10495 Qmenu_item, f->current_tool_bar_string);
10496 if (INTEGERP (prop))
10497 {
10498 *prop_idx = XINT (prop);
10499 success_p = 1;
10500 }
10501 else
10502 success_p = 0;
10503
10504 return success_p;
10505 }
10506
10507 \f
10508 /* Get information about the tool-bar item at position X/Y on frame F.
10509 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10510 the current matrix of the tool-bar window of F, or NULL if not
10511 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10512 item in F->tool_bar_items. Value is
10513
10514 -1 if X/Y is not on a tool-bar item
10515 0 if X/Y is on the same item that was highlighted before.
10516 1 otherwise. */
10517
10518 static int
10519 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10520 struct frame *f;
10521 int x, y;
10522 struct glyph **glyph;
10523 int *hpos, *vpos, *prop_idx;
10524 {
10525 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10526 struct window *w = XWINDOW (f->tool_bar_window);
10527 int area;
10528
10529 /* Find the glyph under X/Y. */
10530 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10531 if (*glyph == NULL)
10532 return -1;
10533
10534 /* Get the start of this tool-bar item's properties in
10535 f->tool_bar_items. */
10536 if (!tool_bar_item_info (f, *glyph, prop_idx))
10537 return -1;
10538
10539 /* Is mouse on the highlighted item? */
10540 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10541 && *vpos >= dpyinfo->mouse_face_beg_row
10542 && *vpos <= dpyinfo->mouse_face_end_row
10543 && (*vpos > dpyinfo->mouse_face_beg_row
10544 || *hpos >= dpyinfo->mouse_face_beg_col)
10545 && (*vpos < dpyinfo->mouse_face_end_row
10546 || *hpos < dpyinfo->mouse_face_end_col
10547 || dpyinfo->mouse_face_past_end))
10548 return 0;
10549
10550 return 1;
10551 }
10552
10553
10554 /* EXPORT:
10555 Handle mouse button event on the tool-bar of frame F, at
10556 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10557 0 for button release. MODIFIERS is event modifiers for button
10558 release. */
10559
10560 void
10561 handle_tool_bar_click (f, x, y, down_p, modifiers)
10562 struct frame *f;
10563 int x, y, down_p;
10564 unsigned int modifiers;
10565 {
10566 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10567 struct window *w = XWINDOW (f->tool_bar_window);
10568 int hpos, vpos, prop_idx;
10569 struct glyph *glyph;
10570 Lisp_Object enabled_p;
10571
10572 /* If not on the highlighted tool-bar item, return. */
10573 frame_to_window_pixel_xy (w, &x, &y);
10574 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10575 return;
10576
10577 /* If item is disabled, do nothing. */
10578 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10579 if (NILP (enabled_p))
10580 return;
10581
10582 if (down_p)
10583 {
10584 /* Show item in pressed state. */
10585 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10586 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10587 last_tool_bar_item = prop_idx;
10588 }
10589 else
10590 {
10591 Lisp_Object key, frame;
10592 struct input_event event;
10593 EVENT_INIT (event);
10594
10595 /* Show item in released state. */
10596 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10597 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10598
10599 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10600
10601 XSETFRAME (frame, f);
10602 event.kind = TOOL_BAR_EVENT;
10603 event.frame_or_window = frame;
10604 event.arg = frame;
10605 kbd_buffer_store_event (&event);
10606
10607 event.kind = TOOL_BAR_EVENT;
10608 event.frame_or_window = frame;
10609 event.arg = key;
10610 event.modifiers = modifiers;
10611 kbd_buffer_store_event (&event);
10612 last_tool_bar_item = -1;
10613 }
10614 }
10615
10616
10617 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10618 tool-bar window-relative coordinates X/Y. Called from
10619 note_mouse_highlight. */
10620
10621 static void
10622 note_tool_bar_highlight (f, x, y)
10623 struct frame *f;
10624 int x, y;
10625 {
10626 Lisp_Object window = f->tool_bar_window;
10627 struct window *w = XWINDOW (window);
10628 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10629 int hpos, vpos;
10630 struct glyph *glyph;
10631 struct glyph_row *row;
10632 int i;
10633 Lisp_Object enabled_p;
10634 int prop_idx;
10635 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10636 int mouse_down_p, rc;
10637
10638 /* Function note_mouse_highlight is called with negative x(y
10639 values when mouse moves outside of the frame. */
10640 if (x <= 0 || y <= 0)
10641 {
10642 clear_mouse_face (dpyinfo);
10643 return;
10644 }
10645
10646 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10647 if (rc < 0)
10648 {
10649 /* Not on tool-bar item. */
10650 clear_mouse_face (dpyinfo);
10651 return;
10652 }
10653 else if (rc == 0)
10654 /* On same tool-bar item as before. */
10655 goto set_help_echo;
10656
10657 clear_mouse_face (dpyinfo);
10658
10659 /* Mouse is down, but on different tool-bar item? */
10660 mouse_down_p = (dpyinfo->grabbed
10661 && f == last_mouse_frame
10662 && FRAME_LIVE_P (f));
10663 if (mouse_down_p
10664 && last_tool_bar_item != prop_idx)
10665 return;
10666
10667 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10668 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10669
10670 /* If tool-bar item is not enabled, don't highlight it. */
10671 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10672 if (!NILP (enabled_p))
10673 {
10674 /* Compute the x-position of the glyph. In front and past the
10675 image is a space. We include this in the highlighted area. */
10676 row = MATRIX_ROW (w->current_matrix, vpos);
10677 for (i = x = 0; i < hpos; ++i)
10678 x += row->glyphs[TEXT_AREA][i].pixel_width;
10679
10680 /* Record this as the current active region. */
10681 dpyinfo->mouse_face_beg_col = hpos;
10682 dpyinfo->mouse_face_beg_row = vpos;
10683 dpyinfo->mouse_face_beg_x = x;
10684 dpyinfo->mouse_face_beg_y = row->y;
10685 dpyinfo->mouse_face_past_end = 0;
10686
10687 dpyinfo->mouse_face_end_col = hpos + 1;
10688 dpyinfo->mouse_face_end_row = vpos;
10689 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10690 dpyinfo->mouse_face_end_y = row->y;
10691 dpyinfo->mouse_face_window = window;
10692 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10693
10694 /* Display it as active. */
10695 show_mouse_face (dpyinfo, draw);
10696 dpyinfo->mouse_face_image_state = draw;
10697 }
10698
10699 set_help_echo:
10700
10701 /* Set help_echo_string to a help string to display for this tool-bar item.
10702 XTread_socket does the rest. */
10703 help_echo_object = help_echo_window = Qnil;
10704 help_echo_pos = -1;
10705 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10706 if (NILP (help_echo_string))
10707 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10708 }
10709
10710 #endif /* HAVE_WINDOW_SYSTEM */
10711
10712
10713 \f
10714 /************************************************************************
10715 Horizontal scrolling
10716 ************************************************************************/
10717
10718 static int hscroll_window_tree P_ ((Lisp_Object));
10719 static int hscroll_windows P_ ((Lisp_Object));
10720
10721 /* For all leaf windows in the window tree rooted at WINDOW, set their
10722 hscroll value so that PT is (i) visible in the window, and (ii) so
10723 that it is not within a certain margin at the window's left and
10724 right border. Value is non-zero if any window's hscroll has been
10725 changed. */
10726
10727 static int
10728 hscroll_window_tree (window)
10729 Lisp_Object window;
10730 {
10731 int hscrolled_p = 0;
10732 int hscroll_relative_p = FLOATP (Vhscroll_step);
10733 int hscroll_step_abs = 0;
10734 double hscroll_step_rel = 0;
10735
10736 if (hscroll_relative_p)
10737 {
10738 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10739 if (hscroll_step_rel < 0)
10740 {
10741 hscroll_relative_p = 0;
10742 hscroll_step_abs = 0;
10743 }
10744 }
10745 else if (INTEGERP (Vhscroll_step))
10746 {
10747 hscroll_step_abs = XINT (Vhscroll_step);
10748 if (hscroll_step_abs < 0)
10749 hscroll_step_abs = 0;
10750 }
10751 else
10752 hscroll_step_abs = 0;
10753
10754 while (WINDOWP (window))
10755 {
10756 struct window *w = XWINDOW (window);
10757
10758 if (WINDOWP (w->hchild))
10759 hscrolled_p |= hscroll_window_tree (w->hchild);
10760 else if (WINDOWP (w->vchild))
10761 hscrolled_p |= hscroll_window_tree (w->vchild);
10762 else if (w->cursor.vpos >= 0)
10763 {
10764 int h_margin;
10765 int text_area_width;
10766 struct glyph_row *current_cursor_row
10767 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10768 struct glyph_row *desired_cursor_row
10769 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10770 struct glyph_row *cursor_row
10771 = (desired_cursor_row->enabled_p
10772 ? desired_cursor_row
10773 : current_cursor_row);
10774
10775 text_area_width = window_box_width (w, TEXT_AREA);
10776
10777 /* Scroll when cursor is inside this scroll margin. */
10778 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10779
10780 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10781 && ((XFASTINT (w->hscroll)
10782 && w->cursor.x <= h_margin)
10783 || (cursor_row->enabled_p
10784 && cursor_row->truncated_on_right_p
10785 && (w->cursor.x >= text_area_width - h_margin))))
10786 {
10787 struct it it;
10788 int hscroll;
10789 struct buffer *saved_current_buffer;
10790 int pt;
10791 int wanted_x;
10792
10793 /* Find point in a display of infinite width. */
10794 saved_current_buffer = current_buffer;
10795 current_buffer = XBUFFER (w->buffer);
10796
10797 if (w == XWINDOW (selected_window))
10798 pt = BUF_PT (current_buffer);
10799 else
10800 {
10801 pt = marker_position (w->pointm);
10802 pt = max (BEGV, pt);
10803 pt = min (ZV, pt);
10804 }
10805
10806 /* Move iterator to pt starting at cursor_row->start in
10807 a line with infinite width. */
10808 init_to_row_start (&it, w, cursor_row);
10809 it.last_visible_x = INFINITY;
10810 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10811 current_buffer = saved_current_buffer;
10812
10813 /* Position cursor in window. */
10814 if (!hscroll_relative_p && hscroll_step_abs == 0)
10815 hscroll = max (0, (it.current_x
10816 - (ITERATOR_AT_END_OF_LINE_P (&it)
10817 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10818 : (text_area_width / 2))))
10819 / FRAME_COLUMN_WIDTH (it.f);
10820 else if (w->cursor.x >= text_area_width - h_margin)
10821 {
10822 if (hscroll_relative_p)
10823 wanted_x = text_area_width * (1 - hscroll_step_rel)
10824 - h_margin;
10825 else
10826 wanted_x = text_area_width
10827 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10828 - h_margin;
10829 hscroll
10830 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10831 }
10832 else
10833 {
10834 if (hscroll_relative_p)
10835 wanted_x = text_area_width * hscroll_step_rel
10836 + h_margin;
10837 else
10838 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10839 + h_margin;
10840 hscroll
10841 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10842 }
10843 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10844
10845 /* Don't call Fset_window_hscroll if value hasn't
10846 changed because it will prevent redisplay
10847 optimizations. */
10848 if (XFASTINT (w->hscroll) != hscroll)
10849 {
10850 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10851 w->hscroll = make_number (hscroll);
10852 hscrolled_p = 1;
10853 }
10854 }
10855 }
10856
10857 window = w->next;
10858 }
10859
10860 /* Value is non-zero if hscroll of any leaf window has been changed. */
10861 return hscrolled_p;
10862 }
10863
10864
10865 /* Set hscroll so that cursor is visible and not inside horizontal
10866 scroll margins for all windows in the tree rooted at WINDOW. See
10867 also hscroll_window_tree above. Value is non-zero if any window's
10868 hscroll has been changed. If it has, desired matrices on the frame
10869 of WINDOW are cleared. */
10870
10871 static int
10872 hscroll_windows (window)
10873 Lisp_Object window;
10874 {
10875 int hscrolled_p = hscroll_window_tree (window);
10876 if (hscrolled_p)
10877 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10878 return hscrolled_p;
10879 }
10880
10881
10882 \f
10883 /************************************************************************
10884 Redisplay
10885 ************************************************************************/
10886
10887 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10888 to a non-zero value. This is sometimes handy to have in a debugger
10889 session. */
10890
10891 #if GLYPH_DEBUG
10892
10893 /* First and last unchanged row for try_window_id. */
10894
10895 int debug_first_unchanged_at_end_vpos;
10896 int debug_last_unchanged_at_beg_vpos;
10897
10898 /* Delta vpos and y. */
10899
10900 int debug_dvpos, debug_dy;
10901
10902 /* Delta in characters and bytes for try_window_id. */
10903
10904 int debug_delta, debug_delta_bytes;
10905
10906 /* Values of window_end_pos and window_end_vpos at the end of
10907 try_window_id. */
10908
10909 EMACS_INT debug_end_pos, debug_end_vpos;
10910
10911 /* Append a string to W->desired_matrix->method. FMT is a printf
10912 format string. A1...A9 are a supplement for a variable-length
10913 argument list. If trace_redisplay_p is non-zero also printf the
10914 resulting string to stderr. */
10915
10916 static void
10917 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10918 struct window *w;
10919 char *fmt;
10920 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10921 {
10922 char buffer[512];
10923 char *method = w->desired_matrix->method;
10924 int len = strlen (method);
10925 int size = sizeof w->desired_matrix->method;
10926 int remaining = size - len - 1;
10927
10928 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10929 if (len && remaining)
10930 {
10931 method[len] = '|';
10932 --remaining, ++len;
10933 }
10934
10935 strncpy (method + len, buffer, remaining);
10936
10937 if (trace_redisplay_p)
10938 fprintf (stderr, "%p (%s): %s\n",
10939 w,
10940 ((BUFFERP (w->buffer)
10941 && STRINGP (XBUFFER (w->buffer)->name))
10942 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10943 : "no buffer"),
10944 buffer);
10945 }
10946
10947 #endif /* GLYPH_DEBUG */
10948
10949
10950 /* Value is non-zero if all changes in window W, which displays
10951 current_buffer, are in the text between START and END. START is a
10952 buffer position, END is given as a distance from Z. Used in
10953 redisplay_internal for display optimization. */
10954
10955 static INLINE int
10956 text_outside_line_unchanged_p (w, start, end)
10957 struct window *w;
10958 int start, end;
10959 {
10960 int unchanged_p = 1;
10961
10962 /* If text or overlays have changed, see where. */
10963 if (XFASTINT (w->last_modified) < MODIFF
10964 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10965 {
10966 /* Gap in the line? */
10967 if (GPT < start || Z - GPT < end)
10968 unchanged_p = 0;
10969
10970 /* Changes start in front of the line, or end after it? */
10971 if (unchanged_p
10972 && (BEG_UNCHANGED < start - 1
10973 || END_UNCHANGED < end))
10974 unchanged_p = 0;
10975
10976 /* If selective display, can't optimize if changes start at the
10977 beginning of the line. */
10978 if (unchanged_p
10979 && INTEGERP (current_buffer->selective_display)
10980 && XINT (current_buffer->selective_display) > 0
10981 && (BEG_UNCHANGED < start || GPT <= start))
10982 unchanged_p = 0;
10983
10984 /* If there are overlays at the start or end of the line, these
10985 may have overlay strings with newlines in them. A change at
10986 START, for instance, may actually concern the display of such
10987 overlay strings as well, and they are displayed on different
10988 lines. So, quickly rule out this case. (For the future, it
10989 might be desirable to implement something more telling than
10990 just BEG/END_UNCHANGED.) */
10991 if (unchanged_p)
10992 {
10993 if (BEG + BEG_UNCHANGED == start
10994 && overlay_touches_p (start))
10995 unchanged_p = 0;
10996 if (END_UNCHANGED == end
10997 && overlay_touches_p (Z - end))
10998 unchanged_p = 0;
10999 }
11000 }
11001
11002 return unchanged_p;
11003 }
11004
11005
11006 /* Do a frame update, taking possible shortcuts into account. This is
11007 the main external entry point for redisplay.
11008
11009 If the last redisplay displayed an echo area message and that message
11010 is no longer requested, we clear the echo area or bring back the
11011 mini-buffer if that is in use. */
11012
11013 void
11014 redisplay ()
11015 {
11016 redisplay_internal (0);
11017 }
11018
11019
11020 static Lisp_Object
11021 overlay_arrow_string_or_property (var)
11022 Lisp_Object var;
11023 {
11024 Lisp_Object val;
11025
11026 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11027 return val;
11028
11029 return Voverlay_arrow_string;
11030 }
11031
11032 /* Return 1 if there are any overlay-arrows in current_buffer. */
11033 static int
11034 overlay_arrow_in_current_buffer_p ()
11035 {
11036 Lisp_Object vlist;
11037
11038 for (vlist = Voverlay_arrow_variable_list;
11039 CONSP (vlist);
11040 vlist = XCDR (vlist))
11041 {
11042 Lisp_Object var = XCAR (vlist);
11043 Lisp_Object val;
11044
11045 if (!SYMBOLP (var))
11046 continue;
11047 val = find_symbol_value (var);
11048 if (MARKERP (val)
11049 && current_buffer == XMARKER (val)->buffer)
11050 return 1;
11051 }
11052 return 0;
11053 }
11054
11055
11056 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11057 has changed. */
11058
11059 static int
11060 overlay_arrows_changed_p ()
11061 {
11062 Lisp_Object vlist;
11063
11064 for (vlist = Voverlay_arrow_variable_list;
11065 CONSP (vlist);
11066 vlist = XCDR (vlist))
11067 {
11068 Lisp_Object var = XCAR (vlist);
11069 Lisp_Object val, pstr;
11070
11071 if (!SYMBOLP (var))
11072 continue;
11073 val = find_symbol_value (var);
11074 if (!MARKERP (val))
11075 continue;
11076 if (! EQ (COERCE_MARKER (val),
11077 Fget (var, Qlast_arrow_position))
11078 || ! (pstr = overlay_arrow_string_or_property (var),
11079 EQ (pstr, Fget (var, Qlast_arrow_string))))
11080 return 1;
11081 }
11082 return 0;
11083 }
11084
11085 /* Mark overlay arrows to be updated on next redisplay. */
11086
11087 static void
11088 update_overlay_arrows (up_to_date)
11089 int up_to_date;
11090 {
11091 Lisp_Object vlist;
11092
11093 for (vlist = Voverlay_arrow_variable_list;
11094 CONSP (vlist);
11095 vlist = XCDR (vlist))
11096 {
11097 Lisp_Object var = XCAR (vlist);
11098
11099 if (!SYMBOLP (var))
11100 continue;
11101
11102 if (up_to_date > 0)
11103 {
11104 Lisp_Object val = find_symbol_value (var);
11105 Fput (var, Qlast_arrow_position,
11106 COERCE_MARKER (val));
11107 Fput (var, Qlast_arrow_string,
11108 overlay_arrow_string_or_property (var));
11109 }
11110 else if (up_to_date < 0
11111 || !NILP (Fget (var, Qlast_arrow_position)))
11112 {
11113 Fput (var, Qlast_arrow_position, Qt);
11114 Fput (var, Qlast_arrow_string, Qt);
11115 }
11116 }
11117 }
11118
11119
11120 /* Return overlay arrow string to display at row.
11121 Return integer (bitmap number) for arrow bitmap in left fringe.
11122 Return nil if no overlay arrow. */
11123
11124 static Lisp_Object
11125 overlay_arrow_at_row (it, row)
11126 struct it *it;
11127 struct glyph_row *row;
11128 {
11129 Lisp_Object vlist;
11130
11131 for (vlist = Voverlay_arrow_variable_list;
11132 CONSP (vlist);
11133 vlist = XCDR (vlist))
11134 {
11135 Lisp_Object var = XCAR (vlist);
11136 Lisp_Object val;
11137
11138 if (!SYMBOLP (var))
11139 continue;
11140
11141 val = find_symbol_value (var);
11142
11143 if (MARKERP (val)
11144 && current_buffer == XMARKER (val)->buffer
11145 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11146 {
11147 if (FRAME_WINDOW_P (it->f)
11148 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11149 {
11150 #ifdef HAVE_WINDOW_SYSTEM
11151 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11152 {
11153 int fringe_bitmap;
11154 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11155 return make_number (fringe_bitmap);
11156 }
11157 #endif
11158 return make_number (-1); /* Use default arrow bitmap */
11159 }
11160 return overlay_arrow_string_or_property (var);
11161 }
11162 }
11163
11164 return Qnil;
11165 }
11166
11167 /* Return 1 if point moved out of or into a composition. Otherwise
11168 return 0. PREV_BUF and PREV_PT are the last point buffer and
11169 position. BUF and PT are the current point buffer and position. */
11170
11171 int
11172 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11173 struct buffer *prev_buf, *buf;
11174 int prev_pt, pt;
11175 {
11176 EMACS_INT start, end;
11177 Lisp_Object prop;
11178 Lisp_Object buffer;
11179
11180 XSETBUFFER (buffer, buf);
11181 /* Check a composition at the last point if point moved within the
11182 same buffer. */
11183 if (prev_buf == buf)
11184 {
11185 if (prev_pt == pt)
11186 /* Point didn't move. */
11187 return 0;
11188
11189 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11190 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11191 && COMPOSITION_VALID_P (start, end, prop)
11192 && start < prev_pt && end > prev_pt)
11193 /* The last point was within the composition. Return 1 iff
11194 point moved out of the composition. */
11195 return (pt <= start || pt >= end);
11196 }
11197
11198 /* Check a composition at the current point. */
11199 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11200 && find_composition (pt, -1, &start, &end, &prop, buffer)
11201 && COMPOSITION_VALID_P (start, end, prop)
11202 && start < pt && end > pt);
11203 }
11204
11205
11206 /* Reconsider the setting of B->clip_changed which is displayed
11207 in window W. */
11208
11209 static INLINE void
11210 reconsider_clip_changes (w, b)
11211 struct window *w;
11212 struct buffer *b;
11213 {
11214 if (b->clip_changed
11215 && !NILP (w->window_end_valid)
11216 && w->current_matrix->buffer == b
11217 && w->current_matrix->zv == BUF_ZV (b)
11218 && w->current_matrix->begv == BUF_BEGV (b))
11219 b->clip_changed = 0;
11220
11221 /* If display wasn't paused, and W is not a tool bar window, see if
11222 point has been moved into or out of a composition. In that case,
11223 we set b->clip_changed to 1 to force updating the screen. If
11224 b->clip_changed has already been set to 1, we can skip this
11225 check. */
11226 if (!b->clip_changed
11227 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11228 {
11229 int pt;
11230
11231 if (w == XWINDOW (selected_window))
11232 pt = BUF_PT (current_buffer);
11233 else
11234 pt = marker_position (w->pointm);
11235
11236 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11237 || pt != XINT (w->last_point))
11238 && check_point_in_composition (w->current_matrix->buffer,
11239 XINT (w->last_point),
11240 XBUFFER (w->buffer), pt))
11241 b->clip_changed = 1;
11242 }
11243 }
11244 \f
11245
11246 /* Select FRAME to forward the values of frame-local variables into C
11247 variables so that the redisplay routines can access those values
11248 directly. */
11249
11250 static void
11251 select_frame_for_redisplay (frame)
11252 Lisp_Object frame;
11253 {
11254 Lisp_Object tail, symbol, val;
11255 Lisp_Object old = selected_frame;
11256 struct Lisp_Symbol *sym;
11257
11258 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11259
11260 selected_frame = frame;
11261
11262 do
11263 {
11264 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11265 if (CONSP (XCAR (tail))
11266 && (symbol = XCAR (XCAR (tail)),
11267 SYMBOLP (symbol))
11268 && (sym = indirect_variable (XSYMBOL (symbol)),
11269 val = sym->value,
11270 (BUFFER_LOCAL_VALUEP (val)))
11271 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11272 /* Use find_symbol_value rather than Fsymbol_value
11273 to avoid an error if it is void. */
11274 find_symbol_value (symbol);
11275 } while (!EQ (frame, old) && (frame = old, 1));
11276 }
11277
11278
11279 #define STOP_POLLING \
11280 do { if (! polling_stopped_here) stop_polling (); \
11281 polling_stopped_here = 1; } while (0)
11282
11283 #define RESUME_POLLING \
11284 do { if (polling_stopped_here) start_polling (); \
11285 polling_stopped_here = 0; } while (0)
11286
11287
11288 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11289 response to any user action; therefore, we should preserve the echo
11290 area. (Actually, our caller does that job.) Perhaps in the future
11291 avoid recentering windows if it is not necessary; currently that
11292 causes some problems. */
11293
11294 static void
11295 redisplay_internal (preserve_echo_area)
11296 int preserve_echo_area;
11297 {
11298 struct window *w = XWINDOW (selected_window);
11299 struct frame *f;
11300 int pause;
11301 int must_finish = 0;
11302 struct text_pos tlbufpos, tlendpos;
11303 int number_of_visible_frames;
11304 int count, count1;
11305 struct frame *sf;
11306 int polling_stopped_here = 0;
11307 Lisp_Object old_frame = selected_frame;
11308
11309 /* Non-zero means redisplay has to consider all windows on all
11310 frames. Zero means, only selected_window is considered. */
11311 int consider_all_windows_p;
11312
11313 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11314
11315 /* No redisplay if running in batch mode or frame is not yet fully
11316 initialized, or redisplay is explicitly turned off by setting
11317 Vinhibit_redisplay. */
11318 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11319 || !NILP (Vinhibit_redisplay))
11320 return;
11321
11322 /* Don't examine these until after testing Vinhibit_redisplay.
11323 When Emacs is shutting down, perhaps because its connection to
11324 X has dropped, we should not look at them at all. */
11325 f = XFRAME (w->frame);
11326 sf = SELECTED_FRAME ();
11327
11328 if (!f->glyphs_initialized_p)
11329 return;
11330
11331 /* The flag redisplay_performed_directly_p is set by
11332 direct_output_for_insert when it already did the whole screen
11333 update necessary. */
11334 if (redisplay_performed_directly_p)
11335 {
11336 redisplay_performed_directly_p = 0;
11337 if (!hscroll_windows (selected_window))
11338 return;
11339 }
11340
11341 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11342 if (popup_activated ())
11343 return;
11344 #endif
11345
11346 /* I don't think this happens but let's be paranoid. */
11347 if (redisplaying_p)
11348 return;
11349
11350 /* Record a function that resets redisplaying_p to its old value
11351 when we leave this function. */
11352 count = SPECPDL_INDEX ();
11353 record_unwind_protect (unwind_redisplay,
11354 Fcons (make_number (redisplaying_p), selected_frame));
11355 ++redisplaying_p;
11356 specbind (Qinhibit_free_realized_faces, Qnil);
11357
11358 {
11359 Lisp_Object tail, frame;
11360
11361 FOR_EACH_FRAME (tail, frame)
11362 {
11363 struct frame *f = XFRAME (frame);
11364 f->already_hscrolled_p = 0;
11365 }
11366 }
11367
11368 retry:
11369 if (!EQ (old_frame, selected_frame)
11370 && FRAME_LIVE_P (XFRAME (old_frame)))
11371 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11372 selected_frame and selected_window to be temporarily out-of-sync so
11373 when we come back here via `goto retry', we need to resync because we
11374 may need to run Elisp code (via prepare_menu_bars). */
11375 select_frame_for_redisplay (old_frame);
11376
11377 pause = 0;
11378 reconsider_clip_changes (w, current_buffer);
11379 last_escape_glyph_frame = NULL;
11380 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11381
11382 /* If new fonts have been loaded that make a glyph matrix adjustment
11383 necessary, do it. */
11384 if (fonts_changed_p)
11385 {
11386 adjust_glyphs (NULL);
11387 ++windows_or_buffers_changed;
11388 fonts_changed_p = 0;
11389 }
11390
11391 /* If face_change_count is non-zero, init_iterator will free all
11392 realized faces, which includes the faces referenced from current
11393 matrices. So, we can't reuse current matrices in this case. */
11394 if (face_change_count)
11395 ++windows_or_buffers_changed;
11396
11397 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11398 && FRAME_TTY (sf)->previous_frame != sf)
11399 {
11400 /* Since frames on a single ASCII terminal share the same
11401 display area, displaying a different frame means redisplay
11402 the whole thing. */
11403 windows_or_buffers_changed++;
11404 SET_FRAME_GARBAGED (sf);
11405 #ifndef DOS_NT
11406 set_tty_color_mode (FRAME_TTY (sf), sf);
11407 #endif
11408 FRAME_TTY (sf)->previous_frame = sf;
11409 }
11410
11411 /* Set the visible flags for all frames. Do this before checking
11412 for resized or garbaged frames; they want to know if their frames
11413 are visible. See the comment in frame.h for
11414 FRAME_SAMPLE_VISIBILITY. */
11415 {
11416 Lisp_Object tail, frame;
11417
11418 number_of_visible_frames = 0;
11419
11420 FOR_EACH_FRAME (tail, frame)
11421 {
11422 struct frame *f = XFRAME (frame);
11423
11424 FRAME_SAMPLE_VISIBILITY (f);
11425 if (FRAME_VISIBLE_P (f))
11426 ++number_of_visible_frames;
11427 clear_desired_matrices (f);
11428 }
11429 }
11430
11431 /* Notice any pending interrupt request to change frame size. */
11432 do_pending_window_change (1);
11433
11434 /* Clear frames marked as garbaged. */
11435 if (frame_garbaged)
11436 clear_garbaged_frames ();
11437
11438 /* Build menubar and tool-bar items. */
11439 if (NILP (Vmemory_full))
11440 prepare_menu_bars ();
11441
11442 if (windows_or_buffers_changed)
11443 update_mode_lines++;
11444
11445 /* Detect case that we need to write or remove a star in the mode line. */
11446 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11447 {
11448 w->update_mode_line = Qt;
11449 if (buffer_shared > 1)
11450 update_mode_lines++;
11451 }
11452
11453 /* Avoid invocation of point motion hooks by `current_column' below. */
11454 count1 = SPECPDL_INDEX ();
11455 specbind (Qinhibit_point_motion_hooks, Qt);
11456
11457 /* If %c is in the mode line, update it if needed. */
11458 if (!NILP (w->column_number_displayed)
11459 /* This alternative quickly identifies a common case
11460 where no change is needed. */
11461 && !(PT == XFASTINT (w->last_point)
11462 && XFASTINT (w->last_modified) >= MODIFF
11463 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11464 && (XFASTINT (w->column_number_displayed)
11465 != (int) current_column ())) /* iftc */
11466 w->update_mode_line = Qt;
11467
11468 unbind_to (count1, Qnil);
11469
11470 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11471
11472 /* The variable buffer_shared is set in redisplay_window and
11473 indicates that we redisplay a buffer in different windows. See
11474 there. */
11475 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11476 || cursor_type_changed);
11477
11478 /* If specs for an arrow have changed, do thorough redisplay
11479 to ensure we remove any arrow that should no longer exist. */
11480 if (overlay_arrows_changed_p ())
11481 consider_all_windows_p = windows_or_buffers_changed = 1;
11482
11483 /* Normally the message* functions will have already displayed and
11484 updated the echo area, but the frame may have been trashed, or
11485 the update may have been preempted, so display the echo area
11486 again here. Checking message_cleared_p captures the case that
11487 the echo area should be cleared. */
11488 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11489 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11490 || (message_cleared_p
11491 && minibuf_level == 0
11492 /* If the mini-window is currently selected, this means the
11493 echo-area doesn't show through. */
11494 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11495 {
11496 int window_height_changed_p = echo_area_display (0);
11497 must_finish = 1;
11498
11499 /* If we don't display the current message, don't clear the
11500 message_cleared_p flag, because, if we did, we wouldn't clear
11501 the echo area in the next redisplay which doesn't preserve
11502 the echo area. */
11503 if (!display_last_displayed_message_p)
11504 message_cleared_p = 0;
11505
11506 if (fonts_changed_p)
11507 goto retry;
11508 else if (window_height_changed_p)
11509 {
11510 consider_all_windows_p = 1;
11511 ++update_mode_lines;
11512 ++windows_or_buffers_changed;
11513
11514 /* If window configuration was changed, frames may have been
11515 marked garbaged. Clear them or we will experience
11516 surprises wrt scrolling. */
11517 if (frame_garbaged)
11518 clear_garbaged_frames ();
11519 }
11520 }
11521 else if (EQ (selected_window, minibuf_window)
11522 && (current_buffer->clip_changed
11523 || XFASTINT (w->last_modified) < MODIFF
11524 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11525 && resize_mini_window (w, 0))
11526 {
11527 /* Resized active mini-window to fit the size of what it is
11528 showing if its contents might have changed. */
11529 must_finish = 1;
11530 /* FIXME: this causes all frames to be updated, which seems unnecessary
11531 since only the current frame needs to be considered. This function needs
11532 to be rewritten with two variables, consider_all_windows and
11533 consider_all_frames. */
11534 consider_all_windows_p = 1;
11535 ++windows_or_buffers_changed;
11536 ++update_mode_lines;
11537
11538 /* If window configuration was changed, frames may have been
11539 marked garbaged. Clear them or we will experience
11540 surprises wrt scrolling. */
11541 if (frame_garbaged)
11542 clear_garbaged_frames ();
11543 }
11544
11545
11546 /* If showing the region, and mark has changed, we must redisplay
11547 the whole window. The assignment to this_line_start_pos prevents
11548 the optimization directly below this if-statement. */
11549 if (((!NILP (Vtransient_mark_mode)
11550 && !NILP (XBUFFER (w->buffer)->mark_active))
11551 != !NILP (w->region_showing))
11552 || (!NILP (w->region_showing)
11553 && !EQ (w->region_showing,
11554 Fmarker_position (XBUFFER (w->buffer)->mark))))
11555 CHARPOS (this_line_start_pos) = 0;
11556
11557 /* Optimize the case that only the line containing the cursor in the
11558 selected window has changed. Variables starting with this_ are
11559 set in display_line and record information about the line
11560 containing the cursor. */
11561 tlbufpos = this_line_start_pos;
11562 tlendpos = this_line_end_pos;
11563 if (!consider_all_windows_p
11564 && CHARPOS (tlbufpos) > 0
11565 && NILP (w->update_mode_line)
11566 && !current_buffer->clip_changed
11567 && !current_buffer->prevent_redisplay_optimizations_p
11568 && FRAME_VISIBLE_P (XFRAME (w->frame))
11569 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11570 /* Make sure recorded data applies to current buffer, etc. */
11571 && this_line_buffer == current_buffer
11572 && current_buffer == XBUFFER (w->buffer)
11573 && NILP (w->force_start)
11574 && NILP (w->optional_new_start)
11575 /* Point must be on the line that we have info recorded about. */
11576 && PT >= CHARPOS (tlbufpos)
11577 && PT <= Z - CHARPOS (tlendpos)
11578 /* All text outside that line, including its final newline,
11579 must be unchanged */
11580 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11581 CHARPOS (tlendpos)))
11582 {
11583 if (CHARPOS (tlbufpos) > BEGV
11584 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11585 && (CHARPOS (tlbufpos) == ZV
11586 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11587 /* Former continuation line has disappeared by becoming empty */
11588 goto cancel;
11589 else if (XFASTINT (w->last_modified) < MODIFF
11590 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11591 || MINI_WINDOW_P (w))
11592 {
11593 /* We have to handle the case of continuation around a
11594 wide-column character (See the comment in indent.c around
11595 line 885).
11596
11597 For instance, in the following case:
11598
11599 -------- Insert --------
11600 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11601 J_I_ ==> J_I_ `^^' are cursors.
11602 ^^ ^^
11603 -------- --------
11604
11605 As we have to redraw the line above, we should goto cancel. */
11606
11607 struct it it;
11608 int line_height_before = this_line_pixel_height;
11609
11610 /* Note that start_display will handle the case that the
11611 line starting at tlbufpos is a continuation lines. */
11612 start_display (&it, w, tlbufpos);
11613
11614 /* Implementation note: It this still necessary? */
11615 if (it.current_x != this_line_start_x)
11616 goto cancel;
11617
11618 TRACE ((stderr, "trying display optimization 1\n"));
11619 w->cursor.vpos = -1;
11620 overlay_arrow_seen = 0;
11621 it.vpos = this_line_vpos;
11622 it.current_y = this_line_y;
11623 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11624 display_line (&it);
11625
11626 /* If line contains point, is not continued,
11627 and ends at same distance from eob as before, we win */
11628 if (w->cursor.vpos >= 0
11629 /* Line is not continued, otherwise this_line_start_pos
11630 would have been set to 0 in display_line. */
11631 && CHARPOS (this_line_start_pos)
11632 /* Line ends as before. */
11633 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11634 /* Line has same height as before. Otherwise other lines
11635 would have to be shifted up or down. */
11636 && this_line_pixel_height == line_height_before)
11637 {
11638 /* If this is not the window's last line, we must adjust
11639 the charstarts of the lines below. */
11640 if (it.current_y < it.last_visible_y)
11641 {
11642 struct glyph_row *row
11643 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11644 int delta, delta_bytes;
11645
11646 if (Z - CHARPOS (tlendpos) == ZV)
11647 {
11648 /* This line ends at end of (accessible part of)
11649 buffer. There is no newline to count. */
11650 delta = (Z
11651 - CHARPOS (tlendpos)
11652 - MATRIX_ROW_START_CHARPOS (row));
11653 delta_bytes = (Z_BYTE
11654 - BYTEPOS (tlendpos)
11655 - MATRIX_ROW_START_BYTEPOS (row));
11656 }
11657 else
11658 {
11659 /* This line ends in a newline. Must take
11660 account of the newline and the rest of the
11661 text that follows. */
11662 delta = (Z
11663 - CHARPOS (tlendpos)
11664 - MATRIX_ROW_START_CHARPOS (row));
11665 delta_bytes = (Z_BYTE
11666 - BYTEPOS (tlendpos)
11667 - MATRIX_ROW_START_BYTEPOS (row));
11668 }
11669
11670 increment_matrix_positions (w->current_matrix,
11671 this_line_vpos + 1,
11672 w->current_matrix->nrows,
11673 delta, delta_bytes);
11674 }
11675
11676 /* If this row displays text now but previously didn't,
11677 or vice versa, w->window_end_vpos may have to be
11678 adjusted. */
11679 if ((it.glyph_row - 1)->displays_text_p)
11680 {
11681 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11682 XSETINT (w->window_end_vpos, this_line_vpos);
11683 }
11684 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11685 && this_line_vpos > 0)
11686 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11687 w->window_end_valid = Qnil;
11688
11689 /* Update hint: No need to try to scroll in update_window. */
11690 w->desired_matrix->no_scrolling_p = 1;
11691
11692 #if GLYPH_DEBUG
11693 *w->desired_matrix->method = 0;
11694 debug_method_add (w, "optimization 1");
11695 #endif
11696 #ifdef HAVE_WINDOW_SYSTEM
11697 update_window_fringes (w, 0);
11698 #endif
11699 goto update;
11700 }
11701 else
11702 goto cancel;
11703 }
11704 else if (/* Cursor position hasn't changed. */
11705 PT == XFASTINT (w->last_point)
11706 /* Make sure the cursor was last displayed
11707 in this window. Otherwise we have to reposition it. */
11708 && 0 <= w->cursor.vpos
11709 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11710 {
11711 if (!must_finish)
11712 {
11713 do_pending_window_change (1);
11714
11715 /* We used to always goto end_of_redisplay here, but this
11716 isn't enough if we have a blinking cursor. */
11717 if (w->cursor_off_p == w->last_cursor_off_p)
11718 goto end_of_redisplay;
11719 }
11720 goto update;
11721 }
11722 /* If highlighting the region, or if the cursor is in the echo area,
11723 then we can't just move the cursor. */
11724 else if (! (!NILP (Vtransient_mark_mode)
11725 && !NILP (current_buffer->mark_active))
11726 && (EQ (selected_window, current_buffer->last_selected_window)
11727 || highlight_nonselected_windows)
11728 && NILP (w->region_showing)
11729 && NILP (Vshow_trailing_whitespace)
11730 && !cursor_in_echo_area)
11731 {
11732 struct it it;
11733 struct glyph_row *row;
11734
11735 /* Skip from tlbufpos to PT and see where it is. Note that
11736 PT may be in invisible text. If so, we will end at the
11737 next visible position. */
11738 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11739 NULL, DEFAULT_FACE_ID);
11740 it.current_x = this_line_start_x;
11741 it.current_y = this_line_y;
11742 it.vpos = this_line_vpos;
11743
11744 /* The call to move_it_to stops in front of PT, but
11745 moves over before-strings. */
11746 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11747
11748 if (it.vpos == this_line_vpos
11749 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11750 row->enabled_p))
11751 {
11752 xassert (this_line_vpos == it.vpos);
11753 xassert (this_line_y == it.current_y);
11754 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11755 #if GLYPH_DEBUG
11756 *w->desired_matrix->method = 0;
11757 debug_method_add (w, "optimization 3");
11758 #endif
11759 goto update;
11760 }
11761 else
11762 goto cancel;
11763 }
11764
11765 cancel:
11766 /* Text changed drastically or point moved off of line. */
11767 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11768 }
11769
11770 CHARPOS (this_line_start_pos) = 0;
11771 consider_all_windows_p |= buffer_shared > 1;
11772 ++clear_face_cache_count;
11773 #ifdef HAVE_WINDOW_SYSTEM
11774 ++clear_image_cache_count;
11775 #endif
11776
11777 /* Build desired matrices, and update the display. If
11778 consider_all_windows_p is non-zero, do it for all windows on all
11779 frames. Otherwise do it for selected_window, only. */
11780
11781 if (consider_all_windows_p)
11782 {
11783 Lisp_Object tail, frame;
11784
11785 FOR_EACH_FRAME (tail, frame)
11786 XFRAME (frame)->updated_p = 0;
11787
11788 /* Recompute # windows showing selected buffer. This will be
11789 incremented each time such a window is displayed. */
11790 buffer_shared = 0;
11791
11792 FOR_EACH_FRAME (tail, frame)
11793 {
11794 struct frame *f = XFRAME (frame);
11795
11796 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11797 {
11798 if (! EQ (frame, selected_frame))
11799 /* Select the frame, for the sake of frame-local
11800 variables. */
11801 select_frame_for_redisplay (frame);
11802
11803 /* Mark all the scroll bars to be removed; we'll redeem
11804 the ones we want when we redisplay their windows. */
11805 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11806 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11807
11808 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11809 redisplay_windows (FRAME_ROOT_WINDOW (f));
11810
11811 /* The X error handler may have deleted that frame. */
11812 if (!FRAME_LIVE_P (f))
11813 continue;
11814
11815 /* Any scroll bars which redisplay_windows should have
11816 nuked should now go away. */
11817 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11818 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11819
11820 /* If fonts changed, display again. */
11821 /* ??? rms: I suspect it is a mistake to jump all the way
11822 back to retry here. It should just retry this frame. */
11823 if (fonts_changed_p)
11824 goto retry;
11825
11826 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11827 {
11828 /* See if we have to hscroll. */
11829 if (!f->already_hscrolled_p)
11830 {
11831 f->already_hscrolled_p = 1;
11832 if (hscroll_windows (f->root_window))
11833 goto retry;
11834 }
11835
11836 /* Prevent various kinds of signals during display
11837 update. stdio is not robust about handling
11838 signals, which can cause an apparent I/O
11839 error. */
11840 if (interrupt_input)
11841 unrequest_sigio ();
11842 STOP_POLLING;
11843
11844 /* Update the display. */
11845 set_window_update_flags (XWINDOW (f->root_window), 1);
11846 pause |= update_frame (f, 0, 0);
11847 f->updated_p = 1;
11848 }
11849 }
11850 }
11851
11852 if (!EQ (old_frame, selected_frame)
11853 && FRAME_LIVE_P (XFRAME (old_frame)))
11854 /* We played a bit fast-and-loose above and allowed selected_frame
11855 and selected_window to be temporarily out-of-sync but let's make
11856 sure this stays contained. */
11857 select_frame_for_redisplay (old_frame);
11858 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11859
11860 if (!pause)
11861 {
11862 /* Do the mark_window_display_accurate after all windows have
11863 been redisplayed because this call resets flags in buffers
11864 which are needed for proper redisplay. */
11865 FOR_EACH_FRAME (tail, frame)
11866 {
11867 struct frame *f = XFRAME (frame);
11868 if (f->updated_p)
11869 {
11870 mark_window_display_accurate (f->root_window, 1);
11871 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11872 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11873 }
11874 }
11875 }
11876 }
11877 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11878 {
11879 Lisp_Object mini_window;
11880 struct frame *mini_frame;
11881
11882 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11883 /* Use list_of_error, not Qerror, so that
11884 we catch only errors and don't run the debugger. */
11885 internal_condition_case_1 (redisplay_window_1, selected_window,
11886 list_of_error,
11887 redisplay_window_error);
11888
11889 /* Compare desired and current matrices, perform output. */
11890
11891 update:
11892 /* If fonts changed, display again. */
11893 if (fonts_changed_p)
11894 goto retry;
11895
11896 /* Prevent various kinds of signals during display update.
11897 stdio is not robust about handling signals,
11898 which can cause an apparent I/O error. */
11899 if (interrupt_input)
11900 unrequest_sigio ();
11901 STOP_POLLING;
11902
11903 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11904 {
11905 if (hscroll_windows (selected_window))
11906 goto retry;
11907
11908 XWINDOW (selected_window)->must_be_updated_p = 1;
11909 pause = update_frame (sf, 0, 0);
11910 }
11911
11912 /* We may have called echo_area_display at the top of this
11913 function. If the echo area is on another frame, that may
11914 have put text on a frame other than the selected one, so the
11915 above call to update_frame would not have caught it. Catch
11916 it here. */
11917 mini_window = FRAME_MINIBUF_WINDOW (sf);
11918 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11919
11920 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11921 {
11922 XWINDOW (mini_window)->must_be_updated_p = 1;
11923 pause |= update_frame (mini_frame, 0, 0);
11924 if (!pause && hscroll_windows (mini_window))
11925 goto retry;
11926 }
11927 }
11928
11929 /* If display was paused because of pending input, make sure we do a
11930 thorough update the next time. */
11931 if (pause)
11932 {
11933 /* Prevent the optimization at the beginning of
11934 redisplay_internal that tries a single-line update of the
11935 line containing the cursor in the selected window. */
11936 CHARPOS (this_line_start_pos) = 0;
11937
11938 /* Let the overlay arrow be updated the next time. */
11939 update_overlay_arrows (0);
11940
11941 /* If we pause after scrolling, some rows in the current
11942 matrices of some windows are not valid. */
11943 if (!WINDOW_FULL_WIDTH_P (w)
11944 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11945 update_mode_lines = 1;
11946 }
11947 else
11948 {
11949 if (!consider_all_windows_p)
11950 {
11951 /* This has already been done above if
11952 consider_all_windows_p is set. */
11953 mark_window_display_accurate_1 (w, 1);
11954
11955 /* Say overlay arrows are up to date. */
11956 update_overlay_arrows (1);
11957
11958 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11959 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11960 }
11961
11962 update_mode_lines = 0;
11963 windows_or_buffers_changed = 0;
11964 cursor_type_changed = 0;
11965 }
11966
11967 /* Start SIGIO interrupts coming again. Having them off during the
11968 code above makes it less likely one will discard output, but not
11969 impossible, since there might be stuff in the system buffer here.
11970 But it is much hairier to try to do anything about that. */
11971 if (interrupt_input)
11972 request_sigio ();
11973 RESUME_POLLING;
11974
11975 /* If a frame has become visible which was not before, redisplay
11976 again, so that we display it. Expose events for such a frame
11977 (which it gets when becoming visible) don't call the parts of
11978 redisplay constructing glyphs, so simply exposing a frame won't
11979 display anything in this case. So, we have to display these
11980 frames here explicitly. */
11981 if (!pause)
11982 {
11983 Lisp_Object tail, frame;
11984 int new_count = 0;
11985
11986 FOR_EACH_FRAME (tail, frame)
11987 {
11988 int this_is_visible = 0;
11989
11990 if (XFRAME (frame)->visible)
11991 this_is_visible = 1;
11992 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11993 if (XFRAME (frame)->visible)
11994 this_is_visible = 1;
11995
11996 if (this_is_visible)
11997 new_count++;
11998 }
11999
12000 if (new_count != number_of_visible_frames)
12001 windows_or_buffers_changed++;
12002 }
12003
12004 /* Change frame size now if a change is pending. */
12005 do_pending_window_change (1);
12006
12007 /* If we just did a pending size change, or have additional
12008 visible frames, redisplay again. */
12009 if (windows_or_buffers_changed && !pause)
12010 goto retry;
12011
12012 /* Clear the face cache eventually. */
12013 if (consider_all_windows_p)
12014 {
12015 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12016 {
12017 clear_face_cache (0);
12018 clear_face_cache_count = 0;
12019 }
12020 #ifdef HAVE_WINDOW_SYSTEM
12021 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12022 {
12023 clear_image_caches (Qnil);
12024 clear_image_cache_count = 0;
12025 }
12026 #endif /* HAVE_WINDOW_SYSTEM */
12027 }
12028
12029 end_of_redisplay:
12030 unbind_to (count, Qnil);
12031 RESUME_POLLING;
12032 }
12033
12034
12035 /* Redisplay, but leave alone any recent echo area message unless
12036 another message has been requested in its place.
12037
12038 This is useful in situations where you need to redisplay but no
12039 user action has occurred, making it inappropriate for the message
12040 area to be cleared. See tracking_off and
12041 wait_reading_process_output for examples of these situations.
12042
12043 FROM_WHERE is an integer saying from where this function was
12044 called. This is useful for debugging. */
12045
12046 void
12047 redisplay_preserve_echo_area (from_where)
12048 int from_where;
12049 {
12050 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12051
12052 if (!NILP (echo_area_buffer[1]))
12053 {
12054 /* We have a previously displayed message, but no current
12055 message. Redisplay the previous message. */
12056 display_last_displayed_message_p = 1;
12057 redisplay_internal (1);
12058 display_last_displayed_message_p = 0;
12059 }
12060 else
12061 redisplay_internal (1);
12062
12063 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12064 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12065 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12066 }
12067
12068
12069 /* Function registered with record_unwind_protect in
12070 redisplay_internal. Reset redisplaying_p to the value it had
12071 before redisplay_internal was called, and clear
12072 prevent_freeing_realized_faces_p. It also selects the previously
12073 selected frame, unless it has been deleted (by an X connection
12074 failure during redisplay, for example). */
12075
12076 static Lisp_Object
12077 unwind_redisplay (val)
12078 Lisp_Object val;
12079 {
12080 Lisp_Object old_redisplaying_p, old_frame;
12081
12082 old_redisplaying_p = XCAR (val);
12083 redisplaying_p = XFASTINT (old_redisplaying_p);
12084 old_frame = XCDR (val);
12085 if (! EQ (old_frame, selected_frame)
12086 && FRAME_LIVE_P (XFRAME (old_frame)))
12087 select_frame_for_redisplay (old_frame);
12088 return Qnil;
12089 }
12090
12091
12092 /* Mark the display of window W as accurate or inaccurate. If
12093 ACCURATE_P is non-zero mark display of W as accurate. If
12094 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12095 redisplay_internal is called. */
12096
12097 static void
12098 mark_window_display_accurate_1 (w, accurate_p)
12099 struct window *w;
12100 int accurate_p;
12101 {
12102 if (BUFFERP (w->buffer))
12103 {
12104 struct buffer *b = XBUFFER (w->buffer);
12105
12106 w->last_modified
12107 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12108 w->last_overlay_modified
12109 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12110 w->last_had_star
12111 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12112
12113 if (accurate_p)
12114 {
12115 b->clip_changed = 0;
12116 b->prevent_redisplay_optimizations_p = 0;
12117
12118 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12119 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12120 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12121 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12122
12123 w->current_matrix->buffer = b;
12124 w->current_matrix->begv = BUF_BEGV (b);
12125 w->current_matrix->zv = BUF_ZV (b);
12126
12127 w->last_cursor = w->cursor;
12128 w->last_cursor_off_p = w->cursor_off_p;
12129
12130 if (w == XWINDOW (selected_window))
12131 w->last_point = make_number (BUF_PT (b));
12132 else
12133 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12134 }
12135 }
12136
12137 if (accurate_p)
12138 {
12139 w->window_end_valid = w->buffer;
12140 w->update_mode_line = Qnil;
12141 }
12142 }
12143
12144
12145 /* Mark the display of windows in the window tree rooted at WINDOW as
12146 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12147 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12148 be redisplayed the next time redisplay_internal is called. */
12149
12150 void
12151 mark_window_display_accurate (window, accurate_p)
12152 Lisp_Object window;
12153 int accurate_p;
12154 {
12155 struct window *w;
12156
12157 for (; !NILP (window); window = w->next)
12158 {
12159 w = XWINDOW (window);
12160 mark_window_display_accurate_1 (w, accurate_p);
12161
12162 if (!NILP (w->vchild))
12163 mark_window_display_accurate (w->vchild, accurate_p);
12164 if (!NILP (w->hchild))
12165 mark_window_display_accurate (w->hchild, accurate_p);
12166 }
12167
12168 if (accurate_p)
12169 {
12170 update_overlay_arrows (1);
12171 }
12172 else
12173 {
12174 /* Force a thorough redisplay the next time by setting
12175 last_arrow_position and last_arrow_string to t, which is
12176 unequal to any useful value of Voverlay_arrow_... */
12177 update_overlay_arrows (-1);
12178 }
12179 }
12180
12181
12182 /* Return value in display table DP (Lisp_Char_Table *) for character
12183 C. Since a display table doesn't have any parent, we don't have to
12184 follow parent. Do not call this function directly but use the
12185 macro DISP_CHAR_VECTOR. */
12186
12187 Lisp_Object
12188 disp_char_vector (dp, c)
12189 struct Lisp_Char_Table *dp;
12190 int c;
12191 {
12192 Lisp_Object val;
12193
12194 if (ASCII_CHAR_P (c))
12195 {
12196 val = dp->ascii;
12197 if (SUB_CHAR_TABLE_P (val))
12198 val = XSUB_CHAR_TABLE (val)->contents[c];
12199 }
12200 else
12201 {
12202 Lisp_Object table;
12203
12204 XSETCHAR_TABLE (table, dp);
12205 val = char_table_ref (table, c);
12206 }
12207 if (NILP (val))
12208 val = dp->defalt;
12209 return val;
12210 }
12211
12212
12213 \f
12214 /***********************************************************************
12215 Window Redisplay
12216 ***********************************************************************/
12217
12218 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12219
12220 static void
12221 redisplay_windows (window)
12222 Lisp_Object window;
12223 {
12224 while (!NILP (window))
12225 {
12226 struct window *w = XWINDOW (window);
12227
12228 if (!NILP (w->hchild))
12229 redisplay_windows (w->hchild);
12230 else if (!NILP (w->vchild))
12231 redisplay_windows (w->vchild);
12232 else if (!NILP (w->buffer))
12233 {
12234 displayed_buffer = XBUFFER (w->buffer);
12235 /* Use list_of_error, not Qerror, so that
12236 we catch only errors and don't run the debugger. */
12237 internal_condition_case_1 (redisplay_window_0, window,
12238 list_of_error,
12239 redisplay_window_error);
12240 }
12241
12242 window = w->next;
12243 }
12244 }
12245
12246 static Lisp_Object
12247 redisplay_window_error ()
12248 {
12249 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12250 return Qnil;
12251 }
12252
12253 static Lisp_Object
12254 redisplay_window_0 (window)
12255 Lisp_Object window;
12256 {
12257 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12258 redisplay_window (window, 0);
12259 return Qnil;
12260 }
12261
12262 static Lisp_Object
12263 redisplay_window_1 (window)
12264 Lisp_Object window;
12265 {
12266 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12267 redisplay_window (window, 1);
12268 return Qnil;
12269 }
12270 \f
12271
12272 /* Increment GLYPH until it reaches END or CONDITION fails while
12273 adding (GLYPH)->pixel_width to X. */
12274
12275 #define SKIP_GLYPHS(glyph, end, x, condition) \
12276 do \
12277 { \
12278 (x) += (glyph)->pixel_width; \
12279 ++(glyph); \
12280 } \
12281 while ((glyph) < (end) && (condition))
12282
12283
12284 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12285 DELTA is the number of bytes by which positions recorded in ROW
12286 differ from current buffer positions.
12287
12288 Return 0 if cursor is not on this row. 1 otherwise. */
12289
12290 int
12291 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12292 struct window *w;
12293 struct glyph_row *row;
12294 struct glyph_matrix *matrix;
12295 int delta, delta_bytes, dy, dvpos;
12296 {
12297 struct glyph *glyph = row->glyphs[TEXT_AREA];
12298 struct glyph *end = glyph + row->used[TEXT_AREA];
12299 struct glyph *cursor = NULL;
12300 /* The first glyph that starts a sequence of glyphs from string. */
12301 struct glyph *string_start;
12302 /* The X coordinate of string_start. */
12303 int string_start_x;
12304 /* The last known character position. */
12305 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12306 /* The last known character position before string_start. */
12307 int string_before_pos;
12308 int x = row->x;
12309 int cursor_x = x;
12310 int cursor_from_overlay_pos = 0;
12311 int pt_old = PT - delta;
12312
12313 /* Skip over glyphs not having an object at the start of the row.
12314 These are special glyphs like truncation marks on terminal
12315 frames. */
12316 if (row->displays_text_p)
12317 while (glyph < end
12318 && INTEGERP (glyph->object)
12319 && glyph->charpos < 0)
12320 {
12321 x += glyph->pixel_width;
12322 ++glyph;
12323 }
12324
12325 string_start = NULL;
12326 while (glyph < end
12327 && !INTEGERP (glyph->object)
12328 && (!BUFFERP (glyph->object)
12329 || (last_pos = glyph->charpos) < pt_old
12330 || glyph->avoid_cursor_p))
12331 {
12332 if (! STRINGP (glyph->object))
12333 {
12334 string_start = NULL;
12335 x += glyph->pixel_width;
12336 ++glyph;
12337 if (cursor_from_overlay_pos
12338 && last_pos >= cursor_from_overlay_pos)
12339 {
12340 cursor_from_overlay_pos = 0;
12341 cursor = 0;
12342 }
12343 }
12344 else
12345 {
12346 if (string_start == NULL)
12347 {
12348 string_before_pos = last_pos;
12349 string_start = glyph;
12350 string_start_x = x;
12351 }
12352 /* Skip all glyphs from string. */
12353 do
12354 {
12355 Lisp_Object cprop;
12356 int pos;
12357 if ((cursor == NULL || glyph > cursor)
12358 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12359 Qcursor, (glyph)->object),
12360 !NILP (cprop))
12361 && (pos = string_buffer_position (w, glyph->object,
12362 string_before_pos),
12363 (pos == 0 /* From overlay */
12364 || pos == pt_old)))
12365 {
12366 /* Estimate overlay buffer position from the buffer
12367 positions of the glyphs before and after the overlay.
12368 Add 1 to last_pos so that if point corresponds to the
12369 glyph right after the overlay, we still use a 'cursor'
12370 property found in that overlay. */
12371 cursor_from_overlay_pos = (pos ? 0 : last_pos
12372 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12373 cursor = glyph;
12374 cursor_x = x;
12375 }
12376 x += glyph->pixel_width;
12377 ++glyph;
12378 }
12379 while (glyph < end && EQ (glyph->object, string_start->object));
12380 }
12381 }
12382
12383 if (cursor != NULL)
12384 {
12385 glyph = cursor;
12386 x = cursor_x;
12387 }
12388 else if (row->ends_in_ellipsis_p && glyph == end)
12389 {
12390 /* Scan back over the ellipsis glyphs, decrementing positions. */
12391 while (glyph > row->glyphs[TEXT_AREA]
12392 && (glyph - 1)->charpos == last_pos)
12393 glyph--, x -= glyph->pixel_width;
12394 /* That loop always goes one position too far,
12395 including the glyph before the ellipsis.
12396 So scan forward over that one. */
12397 x += glyph->pixel_width;
12398 glyph++;
12399 }
12400 else if (string_start
12401 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12402 {
12403 /* We may have skipped over point because the previous glyphs
12404 are from string. As there's no easy way to know the
12405 character position of the current glyph, find the correct
12406 glyph on point by scanning from string_start again. */
12407 Lisp_Object limit;
12408 Lisp_Object string;
12409 struct glyph *stop = glyph;
12410 int pos;
12411
12412 limit = make_number (pt_old + 1);
12413 glyph = string_start;
12414 x = string_start_x;
12415 string = glyph->object;
12416 pos = string_buffer_position (w, string, string_before_pos);
12417 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12418 because we always put cursor after overlay strings. */
12419 while (pos == 0 && glyph < stop)
12420 {
12421 string = glyph->object;
12422 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12423 if (glyph < stop)
12424 pos = string_buffer_position (w, glyph->object, string_before_pos);
12425 }
12426
12427 while (glyph < stop)
12428 {
12429 pos = XINT (Fnext_single_char_property_change
12430 (make_number (pos), Qdisplay, Qnil, limit));
12431 if (pos > pt_old)
12432 break;
12433 /* Skip glyphs from the same string. */
12434 string = glyph->object;
12435 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12436 /* Skip glyphs from an overlay. */
12437 while (glyph < stop
12438 && ! string_buffer_position (w, glyph->object, pos))
12439 {
12440 string = glyph->object;
12441 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12442 }
12443 }
12444
12445 /* If we reached the end of the line, and end was from a string,
12446 cursor is not on this line. */
12447 if (glyph == end && row->continued_p)
12448 return 0;
12449 }
12450
12451 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12452 w->cursor.x = x;
12453 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12454 w->cursor.y = row->y + dy;
12455
12456 if (w == XWINDOW (selected_window))
12457 {
12458 if (!row->continued_p
12459 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12460 && row->x == 0)
12461 {
12462 this_line_buffer = XBUFFER (w->buffer);
12463
12464 CHARPOS (this_line_start_pos)
12465 = MATRIX_ROW_START_CHARPOS (row) + delta;
12466 BYTEPOS (this_line_start_pos)
12467 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12468
12469 CHARPOS (this_line_end_pos)
12470 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12471 BYTEPOS (this_line_end_pos)
12472 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12473
12474 this_line_y = w->cursor.y;
12475 this_line_pixel_height = row->height;
12476 this_line_vpos = w->cursor.vpos;
12477 this_line_start_x = row->x;
12478 }
12479 else
12480 CHARPOS (this_line_start_pos) = 0;
12481 }
12482
12483 return 1;
12484 }
12485
12486
12487 /* Run window scroll functions, if any, for WINDOW with new window
12488 start STARTP. Sets the window start of WINDOW to that position.
12489
12490 We assume that the window's buffer is really current. */
12491
12492 static INLINE struct text_pos
12493 run_window_scroll_functions (window, startp)
12494 Lisp_Object window;
12495 struct text_pos startp;
12496 {
12497 struct window *w = XWINDOW (window);
12498 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12499
12500 if (current_buffer != XBUFFER (w->buffer))
12501 abort ();
12502
12503 if (!NILP (Vwindow_scroll_functions))
12504 {
12505 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12506 make_number (CHARPOS (startp)));
12507 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12508 /* In case the hook functions switch buffers. */
12509 if (current_buffer != XBUFFER (w->buffer))
12510 set_buffer_internal_1 (XBUFFER (w->buffer));
12511 }
12512
12513 return startp;
12514 }
12515
12516
12517 /* Make sure the line containing the cursor is fully visible.
12518 A value of 1 means there is nothing to be done.
12519 (Either the line is fully visible, or it cannot be made so,
12520 or we cannot tell.)
12521
12522 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12523 is higher than window.
12524
12525 A value of 0 means the caller should do scrolling
12526 as if point had gone off the screen. */
12527
12528 static int
12529 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12530 struct window *w;
12531 int force_p;
12532 int current_matrix_p;
12533 {
12534 struct glyph_matrix *matrix;
12535 struct glyph_row *row;
12536 int window_height;
12537
12538 if (!make_cursor_line_fully_visible_p)
12539 return 1;
12540
12541 /* It's not always possible to find the cursor, e.g, when a window
12542 is full of overlay strings. Don't do anything in that case. */
12543 if (w->cursor.vpos < 0)
12544 return 1;
12545
12546 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12547 row = MATRIX_ROW (matrix, w->cursor.vpos);
12548
12549 /* If the cursor row is not partially visible, there's nothing to do. */
12550 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12551 return 1;
12552
12553 /* If the row the cursor is in is taller than the window's height,
12554 it's not clear what to do, so do nothing. */
12555 window_height = window_box_height (w);
12556 if (row->height >= window_height)
12557 {
12558 if (!force_p || MINI_WINDOW_P (w)
12559 || w->vscroll || w->cursor.vpos == 0)
12560 return 1;
12561 }
12562 return 0;
12563 }
12564
12565
12566 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12567 non-zero means only WINDOW is redisplayed in redisplay_internal.
12568 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12569 in redisplay_window to bring a partially visible line into view in
12570 the case that only the cursor has moved.
12571
12572 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12573 last screen line's vertical height extends past the end of the screen.
12574
12575 Value is
12576
12577 1 if scrolling succeeded
12578
12579 0 if scrolling didn't find point.
12580
12581 -1 if new fonts have been loaded so that we must interrupt
12582 redisplay, adjust glyph matrices, and try again. */
12583
12584 enum
12585 {
12586 SCROLLING_SUCCESS,
12587 SCROLLING_FAILED,
12588 SCROLLING_NEED_LARGER_MATRICES
12589 };
12590
12591 static int
12592 try_scrolling (window, just_this_one_p, scroll_conservatively,
12593 scroll_step, temp_scroll_step, last_line_misfit)
12594 Lisp_Object window;
12595 int just_this_one_p;
12596 EMACS_INT scroll_conservatively, scroll_step;
12597 int temp_scroll_step;
12598 int last_line_misfit;
12599 {
12600 struct window *w = XWINDOW (window);
12601 struct frame *f = XFRAME (w->frame);
12602 struct text_pos pos, startp;
12603 struct it it;
12604 int this_scroll_margin, scroll_max, rc, height;
12605 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12606 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12607 Lisp_Object aggressive;
12608 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12609
12610 #if GLYPH_DEBUG
12611 debug_method_add (w, "try_scrolling");
12612 #endif
12613
12614 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12615
12616 /* Compute scroll margin height in pixels. We scroll when point is
12617 within this distance from the top or bottom of the window. */
12618 if (scroll_margin > 0)
12619 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12620 * FRAME_LINE_HEIGHT (f);
12621 else
12622 this_scroll_margin = 0;
12623
12624 /* Force scroll_conservatively to have a reasonable value, to avoid
12625 overflow while computing how much to scroll. Note that the user
12626 can supply scroll-conservatively equal to `most-positive-fixnum',
12627 which can be larger than INT_MAX. */
12628 if (scroll_conservatively > scroll_limit)
12629 {
12630 scroll_conservatively = scroll_limit;
12631 scroll_max = INT_MAX;
12632 }
12633 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12634 /* Compute how much we should try to scroll maximally to bring
12635 point into view. */
12636 scroll_max = (max (scroll_step,
12637 max (scroll_conservatively, temp_scroll_step))
12638 * FRAME_LINE_HEIGHT (f));
12639 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12640 || NUMBERP (current_buffer->scroll_up_aggressively))
12641 /* We're trying to scroll because of aggressive scrolling but no
12642 scroll_step is set. Choose an arbitrary one. */
12643 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12644 else
12645 scroll_max = 0;
12646
12647 too_near_end:
12648
12649 /* Decide whether to scroll down. */
12650 if (PT > CHARPOS (startp))
12651 {
12652 int scroll_margin_y;
12653
12654 /* Compute the pixel ypos of the scroll margin, then move it to
12655 either that ypos or PT, whichever comes first. */
12656 start_display (&it, w, startp);
12657 scroll_margin_y = it.last_visible_y - this_scroll_margin
12658 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12659 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12660 (MOVE_TO_POS | MOVE_TO_Y));
12661
12662 if (PT > CHARPOS (it.current.pos))
12663 {
12664 int y0 = line_bottom_y (&it);
12665
12666 /* Compute the distance from the scroll margin to PT
12667 (including the height of the cursor line). Moving the
12668 iterator unconditionally to PT can be slow if PT is far
12669 away, so stop 10 lines past the window bottom (is there a
12670 way to do the right thing quickly?). */
12671 move_it_to (&it, PT, -1,
12672 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12673 -1, MOVE_TO_POS | MOVE_TO_Y);
12674 dy = line_bottom_y (&it) - y0;
12675
12676 if (dy > scroll_max)
12677 return SCROLLING_FAILED;
12678
12679 scroll_down_p = 1;
12680 }
12681 }
12682
12683 if (scroll_down_p)
12684 {
12685 /* Point is in or below the bottom scroll margin, so move the
12686 window start down. If scrolling conservatively, move it just
12687 enough down to make point visible. If scroll_step is set,
12688 move it down by scroll_step. */
12689 if (scroll_conservatively)
12690 amount_to_scroll
12691 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12692 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12693 else if (scroll_step || temp_scroll_step)
12694 amount_to_scroll = scroll_max;
12695 else
12696 {
12697 aggressive = current_buffer->scroll_up_aggressively;
12698 height = WINDOW_BOX_TEXT_HEIGHT (w);
12699 if (NUMBERP (aggressive))
12700 {
12701 double float_amount = XFLOATINT (aggressive) * height;
12702 amount_to_scroll = float_amount;
12703 if (amount_to_scroll == 0 && float_amount > 0)
12704 amount_to_scroll = 1;
12705 }
12706 }
12707
12708 if (amount_to_scroll <= 0)
12709 return SCROLLING_FAILED;
12710
12711 start_display (&it, w, startp);
12712 move_it_vertically (&it, amount_to_scroll);
12713
12714 /* If STARTP is unchanged, move it down another screen line. */
12715 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12716 move_it_by_lines (&it, 1, 1);
12717 startp = it.current.pos;
12718 }
12719 else
12720 {
12721 struct text_pos scroll_margin_pos = startp;
12722
12723 /* See if point is inside the scroll margin at the top of the
12724 window. */
12725 if (this_scroll_margin)
12726 {
12727 start_display (&it, w, startp);
12728 move_it_vertically (&it, this_scroll_margin);
12729 scroll_margin_pos = it.current.pos;
12730 }
12731
12732 if (PT < CHARPOS (scroll_margin_pos))
12733 {
12734 /* Point is in the scroll margin at the top of the window or
12735 above what is displayed in the window. */
12736 int y0;
12737
12738 /* Compute the vertical distance from PT to the scroll
12739 margin position. Give up if distance is greater than
12740 scroll_max. */
12741 SET_TEXT_POS (pos, PT, PT_BYTE);
12742 start_display (&it, w, pos);
12743 y0 = it.current_y;
12744 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12745 it.last_visible_y, -1,
12746 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12747 dy = it.current_y - y0;
12748 if (dy > scroll_max)
12749 return SCROLLING_FAILED;
12750
12751 /* Compute new window start. */
12752 start_display (&it, w, startp);
12753
12754 if (scroll_conservatively)
12755 amount_to_scroll
12756 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12757 else if (scroll_step || temp_scroll_step)
12758 amount_to_scroll = scroll_max;
12759 else
12760 {
12761 aggressive = current_buffer->scroll_down_aggressively;
12762 height = WINDOW_BOX_TEXT_HEIGHT (w);
12763 if (NUMBERP (aggressive))
12764 {
12765 double float_amount = XFLOATINT (aggressive) * height;
12766 amount_to_scroll = float_amount;
12767 if (amount_to_scroll == 0 && float_amount > 0)
12768 amount_to_scroll = 1;
12769 }
12770 }
12771
12772 if (amount_to_scroll <= 0)
12773 return SCROLLING_FAILED;
12774
12775 move_it_vertically_backward (&it, amount_to_scroll);
12776 startp = it.current.pos;
12777 }
12778 }
12779
12780 /* Run window scroll functions. */
12781 startp = run_window_scroll_functions (window, startp);
12782
12783 /* Display the window. Give up if new fonts are loaded, or if point
12784 doesn't appear. */
12785 if (!try_window (window, startp, 0))
12786 rc = SCROLLING_NEED_LARGER_MATRICES;
12787 else if (w->cursor.vpos < 0)
12788 {
12789 clear_glyph_matrix (w->desired_matrix);
12790 rc = SCROLLING_FAILED;
12791 }
12792 else
12793 {
12794 /* Maybe forget recorded base line for line number display. */
12795 if (!just_this_one_p
12796 || current_buffer->clip_changed
12797 || BEG_UNCHANGED < CHARPOS (startp))
12798 w->base_line_number = Qnil;
12799
12800 /* If cursor ends up on a partially visible line,
12801 treat that as being off the bottom of the screen. */
12802 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12803 {
12804 clear_glyph_matrix (w->desired_matrix);
12805 ++extra_scroll_margin_lines;
12806 goto too_near_end;
12807 }
12808 rc = SCROLLING_SUCCESS;
12809 }
12810
12811 return rc;
12812 }
12813
12814
12815 /* Compute a suitable window start for window W if display of W starts
12816 on a continuation line. Value is non-zero if a new window start
12817 was computed.
12818
12819 The new window start will be computed, based on W's width, starting
12820 from the start of the continued line. It is the start of the
12821 screen line with the minimum distance from the old start W->start. */
12822
12823 static int
12824 compute_window_start_on_continuation_line (w)
12825 struct window *w;
12826 {
12827 struct text_pos pos, start_pos;
12828 int window_start_changed_p = 0;
12829
12830 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12831
12832 /* If window start is on a continuation line... Window start may be
12833 < BEGV in case there's invisible text at the start of the
12834 buffer (M-x rmail, for example). */
12835 if (CHARPOS (start_pos) > BEGV
12836 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12837 {
12838 struct it it;
12839 struct glyph_row *row;
12840
12841 /* Handle the case that the window start is out of range. */
12842 if (CHARPOS (start_pos) < BEGV)
12843 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12844 else if (CHARPOS (start_pos) > ZV)
12845 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12846
12847 /* Find the start of the continued line. This should be fast
12848 because scan_buffer is fast (newline cache). */
12849 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12850 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12851 row, DEFAULT_FACE_ID);
12852 reseat_at_previous_visible_line_start (&it);
12853
12854 /* If the line start is "too far" away from the window start,
12855 say it takes too much time to compute a new window start. */
12856 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12857 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12858 {
12859 int min_distance, distance;
12860
12861 /* Move forward by display lines to find the new window
12862 start. If window width was enlarged, the new start can
12863 be expected to be > the old start. If window width was
12864 decreased, the new window start will be < the old start.
12865 So, we're looking for the display line start with the
12866 minimum distance from the old window start. */
12867 pos = it.current.pos;
12868 min_distance = INFINITY;
12869 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12870 distance < min_distance)
12871 {
12872 min_distance = distance;
12873 pos = it.current.pos;
12874 move_it_by_lines (&it, 1, 0);
12875 }
12876
12877 /* Set the window start there. */
12878 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12879 window_start_changed_p = 1;
12880 }
12881 }
12882
12883 return window_start_changed_p;
12884 }
12885
12886
12887 /* Try cursor movement in case text has not changed in window WINDOW,
12888 with window start STARTP. Value is
12889
12890 CURSOR_MOVEMENT_SUCCESS if successful
12891
12892 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12893
12894 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12895 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12896 we want to scroll as if scroll-step were set to 1. See the code.
12897
12898 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12899 which case we have to abort this redisplay, and adjust matrices
12900 first. */
12901
12902 enum
12903 {
12904 CURSOR_MOVEMENT_SUCCESS,
12905 CURSOR_MOVEMENT_CANNOT_BE_USED,
12906 CURSOR_MOVEMENT_MUST_SCROLL,
12907 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12908 };
12909
12910 static int
12911 try_cursor_movement (window, startp, scroll_step)
12912 Lisp_Object window;
12913 struct text_pos startp;
12914 int *scroll_step;
12915 {
12916 struct window *w = XWINDOW (window);
12917 struct frame *f = XFRAME (w->frame);
12918 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12919
12920 #if GLYPH_DEBUG
12921 if (inhibit_try_cursor_movement)
12922 return rc;
12923 #endif
12924
12925 /* Handle case where text has not changed, only point, and it has
12926 not moved off the frame. */
12927 if (/* Point may be in this window. */
12928 PT >= CHARPOS (startp)
12929 /* Selective display hasn't changed. */
12930 && !current_buffer->clip_changed
12931 /* Function force-mode-line-update is used to force a thorough
12932 redisplay. It sets either windows_or_buffers_changed or
12933 update_mode_lines. So don't take a shortcut here for these
12934 cases. */
12935 && !update_mode_lines
12936 && !windows_or_buffers_changed
12937 && !cursor_type_changed
12938 /* Can't use this case if highlighting a region. When a
12939 region exists, cursor movement has to do more than just
12940 set the cursor. */
12941 && !(!NILP (Vtransient_mark_mode)
12942 && !NILP (current_buffer->mark_active))
12943 && NILP (w->region_showing)
12944 && NILP (Vshow_trailing_whitespace)
12945 /* Right after splitting windows, last_point may be nil. */
12946 && INTEGERP (w->last_point)
12947 /* This code is not used for mini-buffer for the sake of the case
12948 of redisplaying to replace an echo area message; since in
12949 that case the mini-buffer contents per se are usually
12950 unchanged. This code is of no real use in the mini-buffer
12951 since the handling of this_line_start_pos, etc., in redisplay
12952 handles the same cases. */
12953 && !EQ (window, minibuf_window)
12954 /* When splitting windows or for new windows, it happens that
12955 redisplay is called with a nil window_end_vpos or one being
12956 larger than the window. This should really be fixed in
12957 window.c. I don't have this on my list, now, so we do
12958 approximately the same as the old redisplay code. --gerd. */
12959 && INTEGERP (w->window_end_vpos)
12960 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12961 && (FRAME_WINDOW_P (f)
12962 || !overlay_arrow_in_current_buffer_p ()))
12963 {
12964 int this_scroll_margin, top_scroll_margin;
12965 struct glyph_row *row = NULL;
12966
12967 #if GLYPH_DEBUG
12968 debug_method_add (w, "cursor movement");
12969 #endif
12970
12971 /* Scroll if point within this distance from the top or bottom
12972 of the window. This is a pixel value. */
12973 if (scroll_margin > 0)
12974 {
12975 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12976 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12977 }
12978 else
12979 this_scroll_margin = 0;
12980
12981 top_scroll_margin = this_scroll_margin;
12982 if (WINDOW_WANTS_HEADER_LINE_P (w))
12983 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12984
12985 /* Start with the row the cursor was displayed during the last
12986 not paused redisplay. Give up if that row is not valid. */
12987 if (w->last_cursor.vpos < 0
12988 || w->last_cursor.vpos >= w->current_matrix->nrows)
12989 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12990 else
12991 {
12992 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12993 if (row->mode_line_p)
12994 ++row;
12995 if (!row->enabled_p)
12996 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12997 }
12998
12999 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13000 {
13001 int scroll_p = 0;
13002 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13003
13004 if (PT > XFASTINT (w->last_point))
13005 {
13006 /* Point has moved forward. */
13007 while (MATRIX_ROW_END_CHARPOS (row) < PT
13008 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13009 {
13010 xassert (row->enabled_p);
13011 ++row;
13012 }
13013
13014 /* The end position of a row equals the start position
13015 of the next row. If PT is there, we would rather
13016 display it in the next line. */
13017 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13018 && MATRIX_ROW_END_CHARPOS (row) == PT
13019 && !cursor_row_p (w, row))
13020 ++row;
13021
13022 /* If within the scroll margin, scroll. Note that
13023 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13024 the next line would be drawn, and that
13025 this_scroll_margin can be zero. */
13026 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13027 || PT > MATRIX_ROW_END_CHARPOS (row)
13028 /* Line is completely visible last line in window
13029 and PT is to be set in the next line. */
13030 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13031 && PT == MATRIX_ROW_END_CHARPOS (row)
13032 && !row->ends_at_zv_p
13033 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13034 scroll_p = 1;
13035 }
13036 else if (PT < XFASTINT (w->last_point))
13037 {
13038 /* Cursor has to be moved backward. Note that PT >=
13039 CHARPOS (startp) because of the outer if-statement. */
13040 while (!row->mode_line_p
13041 && (MATRIX_ROW_START_CHARPOS (row) > PT
13042 || (MATRIX_ROW_START_CHARPOS (row) == PT
13043 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13044 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13045 row > w->current_matrix->rows
13046 && (row-1)->ends_in_newline_from_string_p))))
13047 && (row->y > top_scroll_margin
13048 || CHARPOS (startp) == BEGV))
13049 {
13050 xassert (row->enabled_p);
13051 --row;
13052 }
13053
13054 /* Consider the following case: Window starts at BEGV,
13055 there is invisible, intangible text at BEGV, so that
13056 display starts at some point START > BEGV. It can
13057 happen that we are called with PT somewhere between
13058 BEGV and START. Try to handle that case. */
13059 if (row < w->current_matrix->rows
13060 || row->mode_line_p)
13061 {
13062 row = w->current_matrix->rows;
13063 if (row->mode_line_p)
13064 ++row;
13065 }
13066
13067 /* Due to newlines in overlay strings, we may have to
13068 skip forward over overlay strings. */
13069 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13070 && MATRIX_ROW_END_CHARPOS (row) == PT
13071 && !cursor_row_p (w, row))
13072 ++row;
13073
13074 /* If within the scroll margin, scroll. */
13075 if (row->y < top_scroll_margin
13076 && CHARPOS (startp) != BEGV)
13077 scroll_p = 1;
13078 }
13079 else
13080 {
13081 /* Cursor did not move. So don't scroll even if cursor line
13082 is partially visible, as it was so before. */
13083 rc = CURSOR_MOVEMENT_SUCCESS;
13084 }
13085
13086 if (PT < MATRIX_ROW_START_CHARPOS (row)
13087 || PT > MATRIX_ROW_END_CHARPOS (row))
13088 {
13089 /* if PT is not in the glyph row, give up. */
13090 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13091 }
13092 else if (rc != CURSOR_MOVEMENT_SUCCESS
13093 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13094 && make_cursor_line_fully_visible_p)
13095 {
13096 if (PT == MATRIX_ROW_END_CHARPOS (row)
13097 && !row->ends_at_zv_p
13098 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13099 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13100 else if (row->height > window_box_height (w))
13101 {
13102 /* If we end up in a partially visible line, let's
13103 make it fully visible, except when it's taller
13104 than the window, in which case we can't do much
13105 about it. */
13106 *scroll_step = 1;
13107 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13108 }
13109 else
13110 {
13111 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13112 if (!cursor_row_fully_visible_p (w, 0, 1))
13113 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13114 else
13115 rc = CURSOR_MOVEMENT_SUCCESS;
13116 }
13117 }
13118 else if (scroll_p)
13119 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13120 else
13121 {
13122 do
13123 {
13124 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13125 {
13126 rc = CURSOR_MOVEMENT_SUCCESS;
13127 break;
13128 }
13129 ++row;
13130 }
13131 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13132 && MATRIX_ROW_START_CHARPOS (row) == PT
13133 && cursor_row_p (w, row));
13134 }
13135 }
13136 }
13137
13138 return rc;
13139 }
13140
13141 void
13142 set_vertical_scroll_bar (w)
13143 struct window *w;
13144 {
13145 int start, end, whole;
13146
13147 /* Calculate the start and end positions for the current window.
13148 At some point, it would be nice to choose between scrollbars
13149 which reflect the whole buffer size, with special markers
13150 indicating narrowing, and scrollbars which reflect only the
13151 visible region.
13152
13153 Note that mini-buffers sometimes aren't displaying any text. */
13154 if (!MINI_WINDOW_P (w)
13155 || (w == XWINDOW (minibuf_window)
13156 && NILP (echo_area_buffer[0])))
13157 {
13158 struct buffer *buf = XBUFFER (w->buffer);
13159 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13160 start = marker_position (w->start) - BUF_BEGV (buf);
13161 /* I don't think this is guaranteed to be right. For the
13162 moment, we'll pretend it is. */
13163 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13164
13165 if (end < start)
13166 end = start;
13167 if (whole < (end - start))
13168 whole = end - start;
13169 }
13170 else
13171 start = end = whole = 0;
13172
13173 /* Indicate what this scroll bar ought to be displaying now. */
13174 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13175 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13176 (w, end - start, whole, start);
13177 }
13178
13179
13180 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13181 selected_window is redisplayed.
13182
13183 We can return without actually redisplaying the window if
13184 fonts_changed_p is nonzero. In that case, redisplay_internal will
13185 retry. */
13186
13187 static void
13188 redisplay_window (window, just_this_one_p)
13189 Lisp_Object window;
13190 int just_this_one_p;
13191 {
13192 struct window *w = XWINDOW (window);
13193 struct frame *f = XFRAME (w->frame);
13194 struct buffer *buffer = XBUFFER (w->buffer);
13195 struct buffer *old = current_buffer;
13196 struct text_pos lpoint, opoint, startp;
13197 int update_mode_line;
13198 int tem;
13199 struct it it;
13200 /* Record it now because it's overwritten. */
13201 int current_matrix_up_to_date_p = 0;
13202 int used_current_matrix_p = 0;
13203 /* This is less strict than current_matrix_up_to_date_p.
13204 It indictes that the buffer contents and narrowing are unchanged. */
13205 int buffer_unchanged_p = 0;
13206 int temp_scroll_step = 0;
13207 int count = SPECPDL_INDEX ();
13208 int rc;
13209 int centering_position = -1;
13210 int last_line_misfit = 0;
13211 int beg_unchanged, end_unchanged;
13212
13213 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13214 opoint = lpoint;
13215
13216 /* W must be a leaf window here. */
13217 xassert (!NILP (w->buffer));
13218 #if GLYPH_DEBUG
13219 *w->desired_matrix->method = 0;
13220 #endif
13221
13222 restart:
13223 reconsider_clip_changes (w, buffer);
13224
13225 /* Has the mode line to be updated? */
13226 update_mode_line = (!NILP (w->update_mode_line)
13227 || update_mode_lines
13228 || buffer->clip_changed
13229 || buffer->prevent_redisplay_optimizations_p);
13230
13231 if (MINI_WINDOW_P (w))
13232 {
13233 if (w == XWINDOW (echo_area_window)
13234 && !NILP (echo_area_buffer[0]))
13235 {
13236 if (update_mode_line)
13237 /* We may have to update a tty frame's menu bar or a
13238 tool-bar. Example `M-x C-h C-h C-g'. */
13239 goto finish_menu_bars;
13240 else
13241 /* We've already displayed the echo area glyphs in this window. */
13242 goto finish_scroll_bars;
13243 }
13244 else if ((w != XWINDOW (minibuf_window)
13245 || minibuf_level == 0)
13246 /* When buffer is nonempty, redisplay window normally. */
13247 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13248 /* Quail displays non-mini buffers in minibuffer window.
13249 In that case, redisplay the window normally. */
13250 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13251 {
13252 /* W is a mini-buffer window, but it's not active, so clear
13253 it. */
13254 int yb = window_text_bottom_y (w);
13255 struct glyph_row *row;
13256 int y;
13257
13258 for (y = 0, row = w->desired_matrix->rows;
13259 y < yb;
13260 y += row->height, ++row)
13261 blank_row (w, row, y);
13262 goto finish_scroll_bars;
13263 }
13264
13265 clear_glyph_matrix (w->desired_matrix);
13266 }
13267
13268 /* Otherwise set up data on this window; select its buffer and point
13269 value. */
13270 /* Really select the buffer, for the sake of buffer-local
13271 variables. */
13272 set_buffer_internal_1 (XBUFFER (w->buffer));
13273
13274 current_matrix_up_to_date_p
13275 = (!NILP (w->window_end_valid)
13276 && !current_buffer->clip_changed
13277 && !current_buffer->prevent_redisplay_optimizations_p
13278 && XFASTINT (w->last_modified) >= MODIFF
13279 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13280
13281 /* Run the window-bottom-change-functions
13282 if it is possible that the text on the screen has changed
13283 (either due to modification of the text, or any other reason). */
13284 if (!current_matrix_up_to_date_p
13285 && !NILP (Vwindow_text_change_functions))
13286 {
13287 safe_run_hooks (Qwindow_text_change_functions);
13288 goto restart;
13289 }
13290
13291 beg_unchanged = BEG_UNCHANGED;
13292 end_unchanged = END_UNCHANGED;
13293
13294 SET_TEXT_POS (opoint, PT, PT_BYTE);
13295
13296 specbind (Qinhibit_point_motion_hooks, Qt);
13297
13298 buffer_unchanged_p
13299 = (!NILP (w->window_end_valid)
13300 && !current_buffer->clip_changed
13301 && XFASTINT (w->last_modified) >= MODIFF
13302 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13303
13304 /* When windows_or_buffers_changed is non-zero, we can't rely on
13305 the window end being valid, so set it to nil there. */
13306 if (windows_or_buffers_changed)
13307 {
13308 /* If window starts on a continuation line, maybe adjust the
13309 window start in case the window's width changed. */
13310 if (XMARKER (w->start)->buffer == current_buffer)
13311 compute_window_start_on_continuation_line (w);
13312
13313 w->window_end_valid = Qnil;
13314 }
13315
13316 /* Some sanity checks. */
13317 CHECK_WINDOW_END (w);
13318 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13319 abort ();
13320 if (BYTEPOS (opoint) < CHARPOS (opoint))
13321 abort ();
13322
13323 /* If %c is in mode line, update it if needed. */
13324 if (!NILP (w->column_number_displayed)
13325 /* This alternative quickly identifies a common case
13326 where no change is needed. */
13327 && !(PT == XFASTINT (w->last_point)
13328 && XFASTINT (w->last_modified) >= MODIFF
13329 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13330 && (XFASTINT (w->column_number_displayed)
13331 != (int) current_column ())) /* iftc */
13332 update_mode_line = 1;
13333
13334 /* Count number of windows showing the selected buffer. An indirect
13335 buffer counts as its base buffer. */
13336 if (!just_this_one_p)
13337 {
13338 struct buffer *current_base, *window_base;
13339 current_base = current_buffer;
13340 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13341 if (current_base->base_buffer)
13342 current_base = current_base->base_buffer;
13343 if (window_base->base_buffer)
13344 window_base = window_base->base_buffer;
13345 if (current_base == window_base)
13346 buffer_shared++;
13347 }
13348
13349 /* Point refers normally to the selected window. For any other
13350 window, set up appropriate value. */
13351 if (!EQ (window, selected_window))
13352 {
13353 int new_pt = XMARKER (w->pointm)->charpos;
13354 int new_pt_byte = marker_byte_position (w->pointm);
13355 if (new_pt < BEGV)
13356 {
13357 new_pt = BEGV;
13358 new_pt_byte = BEGV_BYTE;
13359 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13360 }
13361 else if (new_pt > (ZV - 1))
13362 {
13363 new_pt = ZV;
13364 new_pt_byte = ZV_BYTE;
13365 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13366 }
13367
13368 /* We don't use SET_PT so that the point-motion hooks don't run. */
13369 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13370 }
13371
13372 /* If any of the character widths specified in the display table
13373 have changed, invalidate the width run cache. It's true that
13374 this may be a bit late to catch such changes, but the rest of
13375 redisplay goes (non-fatally) haywire when the display table is
13376 changed, so why should we worry about doing any better? */
13377 if (current_buffer->width_run_cache)
13378 {
13379 struct Lisp_Char_Table *disptab = buffer_display_table ();
13380
13381 if (! disptab_matches_widthtab (disptab,
13382 XVECTOR (current_buffer->width_table)))
13383 {
13384 invalidate_region_cache (current_buffer,
13385 current_buffer->width_run_cache,
13386 BEG, Z);
13387 recompute_width_table (current_buffer, disptab);
13388 }
13389 }
13390
13391 /* If window-start is screwed up, choose a new one. */
13392 if (XMARKER (w->start)->buffer != current_buffer)
13393 goto recenter;
13394
13395 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13396
13397 /* If someone specified a new starting point but did not insist,
13398 check whether it can be used. */
13399 if (!NILP (w->optional_new_start)
13400 && CHARPOS (startp) >= BEGV
13401 && CHARPOS (startp) <= ZV)
13402 {
13403 w->optional_new_start = Qnil;
13404 start_display (&it, w, startp);
13405 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13406 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13407 if (IT_CHARPOS (it) == PT)
13408 w->force_start = Qt;
13409 /* IT may overshoot PT if text at PT is invisible. */
13410 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13411 w->force_start = Qt;
13412 }
13413
13414 force_start:
13415
13416 /* Handle case where place to start displaying has been specified,
13417 unless the specified location is outside the accessible range. */
13418 if (!NILP (w->force_start)
13419 || w->frozen_window_start_p)
13420 {
13421 /* We set this later on if we have to adjust point. */
13422 int new_vpos = -1;
13423
13424 w->force_start = Qnil;
13425 w->vscroll = 0;
13426 w->window_end_valid = Qnil;
13427
13428 /* Forget any recorded base line for line number display. */
13429 if (!buffer_unchanged_p)
13430 w->base_line_number = Qnil;
13431
13432 /* Redisplay the mode line. Select the buffer properly for that.
13433 Also, run the hook window-scroll-functions
13434 because we have scrolled. */
13435 /* Note, we do this after clearing force_start because
13436 if there's an error, it is better to forget about force_start
13437 than to get into an infinite loop calling the hook functions
13438 and having them get more errors. */
13439 if (!update_mode_line
13440 || ! NILP (Vwindow_scroll_functions))
13441 {
13442 update_mode_line = 1;
13443 w->update_mode_line = Qt;
13444 startp = run_window_scroll_functions (window, startp);
13445 }
13446
13447 w->last_modified = make_number (0);
13448 w->last_overlay_modified = make_number (0);
13449 if (CHARPOS (startp) < BEGV)
13450 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13451 else if (CHARPOS (startp) > ZV)
13452 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13453
13454 /* Redisplay, then check if cursor has been set during the
13455 redisplay. Give up if new fonts were loaded. */
13456 /* We used to issue a CHECK_MARGINS argument to try_window here,
13457 but this causes scrolling to fail when point begins inside
13458 the scroll margin (bug#148) -- cyd */
13459 if (!try_window (window, startp, 0))
13460 {
13461 w->force_start = Qt;
13462 clear_glyph_matrix (w->desired_matrix);
13463 goto need_larger_matrices;
13464 }
13465
13466 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13467 {
13468 /* If point does not appear, try to move point so it does
13469 appear. The desired matrix has been built above, so we
13470 can use it here. */
13471 new_vpos = window_box_height (w) / 2;
13472 }
13473
13474 if (!cursor_row_fully_visible_p (w, 0, 0))
13475 {
13476 /* Point does appear, but on a line partly visible at end of window.
13477 Move it back to a fully-visible line. */
13478 new_vpos = window_box_height (w);
13479 }
13480
13481 /* If we need to move point for either of the above reasons,
13482 now actually do it. */
13483 if (new_vpos >= 0)
13484 {
13485 struct glyph_row *row;
13486
13487 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13488 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13489 ++row;
13490
13491 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13492 MATRIX_ROW_START_BYTEPOS (row));
13493
13494 if (w != XWINDOW (selected_window))
13495 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13496 else if (current_buffer == old)
13497 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13498
13499 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13500
13501 /* If we are highlighting the region, then we just changed
13502 the region, so redisplay to show it. */
13503 if (!NILP (Vtransient_mark_mode)
13504 && !NILP (current_buffer->mark_active))
13505 {
13506 clear_glyph_matrix (w->desired_matrix);
13507 if (!try_window (window, startp, 0))
13508 goto need_larger_matrices;
13509 }
13510 }
13511
13512 #if GLYPH_DEBUG
13513 debug_method_add (w, "forced window start");
13514 #endif
13515 goto done;
13516 }
13517
13518 /* Handle case where text has not changed, only point, and it has
13519 not moved off the frame, and we are not retrying after hscroll.
13520 (current_matrix_up_to_date_p is nonzero when retrying.) */
13521 if (current_matrix_up_to_date_p
13522 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13523 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13524 {
13525 switch (rc)
13526 {
13527 case CURSOR_MOVEMENT_SUCCESS:
13528 used_current_matrix_p = 1;
13529 goto done;
13530
13531 case CURSOR_MOVEMENT_MUST_SCROLL:
13532 goto try_to_scroll;
13533
13534 default:
13535 abort ();
13536 }
13537 }
13538 /* If current starting point was originally the beginning of a line
13539 but no longer is, find a new starting point. */
13540 else if (!NILP (w->start_at_line_beg)
13541 && !(CHARPOS (startp) <= BEGV
13542 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13543 {
13544 #if GLYPH_DEBUG
13545 debug_method_add (w, "recenter 1");
13546 #endif
13547 goto recenter;
13548 }
13549
13550 /* Try scrolling with try_window_id. Value is > 0 if update has
13551 been done, it is -1 if we know that the same window start will
13552 not work. It is 0 if unsuccessful for some other reason. */
13553 else if ((tem = try_window_id (w)) != 0)
13554 {
13555 #if GLYPH_DEBUG
13556 debug_method_add (w, "try_window_id %d", tem);
13557 #endif
13558
13559 if (fonts_changed_p)
13560 goto need_larger_matrices;
13561 if (tem > 0)
13562 goto done;
13563
13564 /* Otherwise try_window_id has returned -1 which means that we
13565 don't want the alternative below this comment to execute. */
13566 }
13567 else if (CHARPOS (startp) >= BEGV
13568 && CHARPOS (startp) <= ZV
13569 && PT >= CHARPOS (startp)
13570 && (CHARPOS (startp) < ZV
13571 /* Avoid starting at end of buffer. */
13572 || CHARPOS (startp) == BEGV
13573 || (XFASTINT (w->last_modified) >= MODIFF
13574 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13575 {
13576
13577 /* If first window line is a continuation line, and window start
13578 is inside the modified region, but the first change is before
13579 current window start, we must select a new window start.
13580
13581 However, if this is the result of a down-mouse event (e.g. by
13582 extending the mouse-drag-overlay), we don't want to select a
13583 new window start, since that would change the position under
13584 the mouse, resulting in an unwanted mouse-movement rather
13585 than a simple mouse-click. */
13586 if (NILP (w->start_at_line_beg)
13587 && NILP (do_mouse_tracking)
13588 && CHARPOS (startp) > BEGV
13589 && CHARPOS (startp) > BEG + beg_unchanged
13590 && CHARPOS (startp) <= Z - end_unchanged
13591 /* Even if w->start_at_line_beg is nil, a new window may
13592 start at a line_beg, since that's how set_buffer_window
13593 sets it. So, we need to check the return value of
13594 compute_window_start_on_continuation_line. (See also
13595 bug#197). */
13596 && XMARKER (w->start)->buffer == current_buffer
13597 && compute_window_start_on_continuation_line (w))
13598 {
13599 w->force_start = Qt;
13600 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13601 goto force_start;
13602 }
13603
13604 #if GLYPH_DEBUG
13605 debug_method_add (w, "same window start");
13606 #endif
13607
13608 /* Try to redisplay starting at same place as before.
13609 If point has not moved off frame, accept the results. */
13610 if (!current_matrix_up_to_date_p
13611 /* Don't use try_window_reusing_current_matrix in this case
13612 because a window scroll function can have changed the
13613 buffer. */
13614 || !NILP (Vwindow_scroll_functions)
13615 || MINI_WINDOW_P (w)
13616 || !(used_current_matrix_p
13617 = try_window_reusing_current_matrix (w)))
13618 {
13619 IF_DEBUG (debug_method_add (w, "1"));
13620 if (try_window (window, startp, 1) < 0)
13621 /* -1 means we need to scroll.
13622 0 means we need new matrices, but fonts_changed_p
13623 is set in that case, so we will detect it below. */
13624 goto try_to_scroll;
13625 }
13626
13627 if (fonts_changed_p)
13628 goto need_larger_matrices;
13629
13630 if (w->cursor.vpos >= 0)
13631 {
13632 if (!just_this_one_p
13633 || current_buffer->clip_changed
13634 || BEG_UNCHANGED < CHARPOS (startp))
13635 /* Forget any recorded base line for line number display. */
13636 w->base_line_number = Qnil;
13637
13638 if (!cursor_row_fully_visible_p (w, 1, 0))
13639 {
13640 clear_glyph_matrix (w->desired_matrix);
13641 last_line_misfit = 1;
13642 }
13643 /* Drop through and scroll. */
13644 else
13645 goto done;
13646 }
13647 else
13648 clear_glyph_matrix (w->desired_matrix);
13649 }
13650
13651 try_to_scroll:
13652
13653 w->last_modified = make_number (0);
13654 w->last_overlay_modified = make_number (0);
13655
13656 /* Redisplay the mode line. Select the buffer properly for that. */
13657 if (!update_mode_line)
13658 {
13659 update_mode_line = 1;
13660 w->update_mode_line = Qt;
13661 }
13662
13663 /* Try to scroll by specified few lines. */
13664 if ((scroll_conservatively
13665 || scroll_step
13666 || temp_scroll_step
13667 || NUMBERP (current_buffer->scroll_up_aggressively)
13668 || NUMBERP (current_buffer->scroll_down_aggressively))
13669 && !current_buffer->clip_changed
13670 && CHARPOS (startp) >= BEGV
13671 && CHARPOS (startp) <= ZV)
13672 {
13673 /* The function returns -1 if new fonts were loaded, 1 if
13674 successful, 0 if not successful. */
13675 int rc = try_scrolling (window, just_this_one_p,
13676 scroll_conservatively,
13677 scroll_step,
13678 temp_scroll_step, last_line_misfit);
13679 switch (rc)
13680 {
13681 case SCROLLING_SUCCESS:
13682 goto done;
13683
13684 case SCROLLING_NEED_LARGER_MATRICES:
13685 goto need_larger_matrices;
13686
13687 case SCROLLING_FAILED:
13688 break;
13689
13690 default:
13691 abort ();
13692 }
13693 }
13694
13695 /* Finally, just choose place to start which centers point */
13696
13697 recenter:
13698 if (centering_position < 0)
13699 centering_position = window_box_height (w) / 2;
13700
13701 #if GLYPH_DEBUG
13702 debug_method_add (w, "recenter");
13703 #endif
13704
13705 /* w->vscroll = 0; */
13706
13707 /* Forget any previously recorded base line for line number display. */
13708 if (!buffer_unchanged_p)
13709 w->base_line_number = Qnil;
13710
13711 /* Move backward half the height of the window. */
13712 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13713 it.current_y = it.last_visible_y;
13714 move_it_vertically_backward (&it, centering_position);
13715 xassert (IT_CHARPOS (it) >= BEGV);
13716
13717 /* The function move_it_vertically_backward may move over more
13718 than the specified y-distance. If it->w is small, e.g. a
13719 mini-buffer window, we may end up in front of the window's
13720 display area. Start displaying at the start of the line
13721 containing PT in this case. */
13722 if (it.current_y <= 0)
13723 {
13724 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13725 move_it_vertically_backward (&it, 0);
13726 it.current_y = 0;
13727 }
13728
13729 it.current_x = it.hpos = 0;
13730
13731 /* Set startp here explicitly in case that helps avoid an infinite loop
13732 in case the window-scroll-functions functions get errors. */
13733 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13734
13735 /* Run scroll hooks. */
13736 startp = run_window_scroll_functions (window, it.current.pos);
13737
13738 /* Redisplay the window. */
13739 if (!current_matrix_up_to_date_p
13740 || windows_or_buffers_changed
13741 || cursor_type_changed
13742 /* Don't use try_window_reusing_current_matrix in this case
13743 because it can have changed the buffer. */
13744 || !NILP (Vwindow_scroll_functions)
13745 || !just_this_one_p
13746 || MINI_WINDOW_P (w)
13747 || !(used_current_matrix_p
13748 = try_window_reusing_current_matrix (w)))
13749 try_window (window, startp, 0);
13750
13751 /* If new fonts have been loaded (due to fontsets), give up. We
13752 have to start a new redisplay since we need to re-adjust glyph
13753 matrices. */
13754 if (fonts_changed_p)
13755 goto need_larger_matrices;
13756
13757 /* If cursor did not appear assume that the middle of the window is
13758 in the first line of the window. Do it again with the next line.
13759 (Imagine a window of height 100, displaying two lines of height
13760 60. Moving back 50 from it->last_visible_y will end in the first
13761 line.) */
13762 if (w->cursor.vpos < 0)
13763 {
13764 if (!NILP (w->window_end_valid)
13765 && PT >= Z - XFASTINT (w->window_end_pos))
13766 {
13767 clear_glyph_matrix (w->desired_matrix);
13768 move_it_by_lines (&it, 1, 0);
13769 try_window (window, it.current.pos, 0);
13770 }
13771 else if (PT < IT_CHARPOS (it))
13772 {
13773 clear_glyph_matrix (w->desired_matrix);
13774 move_it_by_lines (&it, -1, 0);
13775 try_window (window, it.current.pos, 0);
13776 }
13777 else
13778 {
13779 /* Not much we can do about it. */
13780 }
13781 }
13782
13783 /* Consider the following case: Window starts at BEGV, there is
13784 invisible, intangible text at BEGV, so that display starts at
13785 some point START > BEGV. It can happen that we are called with
13786 PT somewhere between BEGV and START. Try to handle that case. */
13787 if (w->cursor.vpos < 0)
13788 {
13789 struct glyph_row *row = w->current_matrix->rows;
13790 if (row->mode_line_p)
13791 ++row;
13792 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13793 }
13794
13795 if (!cursor_row_fully_visible_p (w, 0, 0))
13796 {
13797 /* If vscroll is enabled, disable it and try again. */
13798 if (w->vscroll)
13799 {
13800 w->vscroll = 0;
13801 clear_glyph_matrix (w->desired_matrix);
13802 goto recenter;
13803 }
13804
13805 /* If centering point failed to make the whole line visible,
13806 put point at the top instead. That has to make the whole line
13807 visible, if it can be done. */
13808 if (centering_position == 0)
13809 goto done;
13810
13811 clear_glyph_matrix (w->desired_matrix);
13812 centering_position = 0;
13813 goto recenter;
13814 }
13815
13816 done:
13817
13818 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13819 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13820 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13821 ? Qt : Qnil);
13822
13823 /* Display the mode line, if we must. */
13824 if ((update_mode_line
13825 /* If window not full width, must redo its mode line
13826 if (a) the window to its side is being redone and
13827 (b) we do a frame-based redisplay. This is a consequence
13828 of how inverted lines are drawn in frame-based redisplay. */
13829 || (!just_this_one_p
13830 && !FRAME_WINDOW_P (f)
13831 && !WINDOW_FULL_WIDTH_P (w))
13832 /* Line number to display. */
13833 || INTEGERP (w->base_line_pos)
13834 /* Column number is displayed and different from the one displayed. */
13835 || (!NILP (w->column_number_displayed)
13836 && (XFASTINT (w->column_number_displayed)
13837 != (int) current_column ()))) /* iftc */
13838 /* This means that the window has a mode line. */
13839 && (WINDOW_WANTS_MODELINE_P (w)
13840 || WINDOW_WANTS_HEADER_LINE_P (w)))
13841 {
13842 display_mode_lines (w);
13843
13844 /* If mode line height has changed, arrange for a thorough
13845 immediate redisplay using the correct mode line height. */
13846 if (WINDOW_WANTS_MODELINE_P (w)
13847 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13848 {
13849 fonts_changed_p = 1;
13850 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13851 = DESIRED_MODE_LINE_HEIGHT (w);
13852 }
13853
13854 /* If top line height has changed, arrange for a thorough
13855 immediate redisplay using the correct mode line height. */
13856 if (WINDOW_WANTS_HEADER_LINE_P (w)
13857 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13858 {
13859 fonts_changed_p = 1;
13860 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13861 = DESIRED_HEADER_LINE_HEIGHT (w);
13862 }
13863
13864 if (fonts_changed_p)
13865 goto need_larger_matrices;
13866 }
13867
13868 if (!line_number_displayed
13869 && !BUFFERP (w->base_line_pos))
13870 {
13871 w->base_line_pos = Qnil;
13872 w->base_line_number = Qnil;
13873 }
13874
13875 finish_menu_bars:
13876
13877 /* When we reach a frame's selected window, redo the frame's menu bar. */
13878 if (update_mode_line
13879 && EQ (FRAME_SELECTED_WINDOW (f), window))
13880 {
13881 int redisplay_menu_p = 0;
13882 int redisplay_tool_bar_p = 0;
13883
13884 if (FRAME_WINDOW_P (f))
13885 {
13886 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13887 || defined (HAVE_NS) || defined (USE_GTK)
13888 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13889 #else
13890 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13891 #endif
13892 }
13893 else
13894 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13895
13896 if (redisplay_menu_p)
13897 display_menu_bar (w);
13898
13899 #ifdef HAVE_WINDOW_SYSTEM
13900 if (FRAME_WINDOW_P (f))
13901 {
13902 #if defined (USE_GTK) || defined (HAVE_NS)
13903 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13904 #else
13905 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13906 && (FRAME_TOOL_BAR_LINES (f) > 0
13907 || !NILP (Vauto_resize_tool_bars));
13908 #endif
13909
13910 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13911 {
13912 extern int ignore_mouse_drag_p;
13913 ignore_mouse_drag_p = 1;
13914 }
13915 }
13916 #endif
13917 }
13918
13919 #ifdef HAVE_WINDOW_SYSTEM
13920 if (FRAME_WINDOW_P (f)
13921 && update_window_fringes (w, (just_this_one_p
13922 || (!used_current_matrix_p && !overlay_arrow_seen)
13923 || w->pseudo_window_p)))
13924 {
13925 update_begin (f);
13926 BLOCK_INPUT;
13927 if (draw_window_fringes (w, 1))
13928 x_draw_vertical_border (w);
13929 UNBLOCK_INPUT;
13930 update_end (f);
13931 }
13932 #endif /* HAVE_WINDOW_SYSTEM */
13933
13934 /* We go to this label, with fonts_changed_p nonzero,
13935 if it is necessary to try again using larger glyph matrices.
13936 We have to redeem the scroll bar even in this case,
13937 because the loop in redisplay_internal expects that. */
13938 need_larger_matrices:
13939 ;
13940 finish_scroll_bars:
13941
13942 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13943 {
13944 /* Set the thumb's position and size. */
13945 set_vertical_scroll_bar (w);
13946
13947 /* Note that we actually used the scroll bar attached to this
13948 window, so it shouldn't be deleted at the end of redisplay. */
13949 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13950 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13951 }
13952
13953 /* Restore current_buffer and value of point in it. */
13954 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13955 set_buffer_internal_1 (old);
13956 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13957 shorter. This can be caused by log truncation in *Messages*. */
13958 if (CHARPOS (lpoint) <= ZV)
13959 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13960
13961 unbind_to (count, Qnil);
13962 }
13963
13964
13965 /* Build the complete desired matrix of WINDOW with a window start
13966 buffer position POS.
13967
13968 Value is 1 if successful. It is zero if fonts were loaded during
13969 redisplay which makes re-adjusting glyph matrices necessary, and -1
13970 if point would appear in the scroll margins.
13971 (We check that only if CHECK_MARGINS is nonzero. */
13972
13973 int
13974 try_window (window, pos, check_margins)
13975 Lisp_Object window;
13976 struct text_pos pos;
13977 int check_margins;
13978 {
13979 struct window *w = XWINDOW (window);
13980 struct it it;
13981 struct glyph_row *last_text_row = NULL;
13982 struct frame *f = XFRAME (w->frame);
13983
13984 /* Make POS the new window start. */
13985 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13986
13987 /* Mark cursor position as unknown. No overlay arrow seen. */
13988 w->cursor.vpos = -1;
13989 overlay_arrow_seen = 0;
13990
13991 /* Initialize iterator and info to start at POS. */
13992 start_display (&it, w, pos);
13993
13994 /* Display all lines of W. */
13995 while (it.current_y < it.last_visible_y)
13996 {
13997 if (display_line (&it))
13998 last_text_row = it.glyph_row - 1;
13999 if (fonts_changed_p)
14000 return 0;
14001 }
14002
14003 /* Don't let the cursor end in the scroll margins. */
14004 if (check_margins
14005 && !MINI_WINDOW_P (w))
14006 {
14007 int this_scroll_margin;
14008
14009 if (scroll_margin > 0)
14010 {
14011 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14012 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14013 }
14014 else
14015 this_scroll_margin = 0;
14016
14017 if ((w->cursor.y >= 0 /* not vscrolled */
14018 && w->cursor.y < this_scroll_margin
14019 && CHARPOS (pos) > BEGV
14020 && IT_CHARPOS (it) < ZV)
14021 /* rms: considering make_cursor_line_fully_visible_p here
14022 seems to give wrong results. We don't want to recenter
14023 when the last line is partly visible, we want to allow
14024 that case to be handled in the usual way. */
14025 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14026 {
14027 w->cursor.vpos = -1;
14028 clear_glyph_matrix (w->desired_matrix);
14029 return -1;
14030 }
14031 }
14032
14033 /* If bottom moved off end of frame, change mode line percentage. */
14034 if (XFASTINT (w->window_end_pos) <= 0
14035 && Z != IT_CHARPOS (it))
14036 w->update_mode_line = Qt;
14037
14038 /* Set window_end_pos to the offset of the last character displayed
14039 on the window from the end of current_buffer. Set
14040 window_end_vpos to its row number. */
14041 if (last_text_row)
14042 {
14043 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14044 w->window_end_bytepos
14045 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14046 w->window_end_pos
14047 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14048 w->window_end_vpos
14049 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14050 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14051 ->displays_text_p);
14052 }
14053 else
14054 {
14055 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14056 w->window_end_pos = make_number (Z - ZV);
14057 w->window_end_vpos = make_number (0);
14058 }
14059
14060 /* But that is not valid info until redisplay finishes. */
14061 w->window_end_valid = Qnil;
14062 return 1;
14063 }
14064
14065
14066 \f
14067 /************************************************************************
14068 Window redisplay reusing current matrix when buffer has not changed
14069 ************************************************************************/
14070
14071 /* Try redisplay of window W showing an unchanged buffer with a
14072 different window start than the last time it was displayed by
14073 reusing its current matrix. Value is non-zero if successful.
14074 W->start is the new window start. */
14075
14076 static int
14077 try_window_reusing_current_matrix (w)
14078 struct window *w;
14079 {
14080 struct frame *f = XFRAME (w->frame);
14081 struct glyph_row *row, *bottom_row;
14082 struct it it;
14083 struct run run;
14084 struct text_pos start, new_start;
14085 int nrows_scrolled, i;
14086 struct glyph_row *last_text_row;
14087 struct glyph_row *last_reused_text_row;
14088 struct glyph_row *start_row;
14089 int start_vpos, min_y, max_y;
14090
14091 #if GLYPH_DEBUG
14092 if (inhibit_try_window_reusing)
14093 return 0;
14094 #endif
14095
14096 if (/* This function doesn't handle terminal frames. */
14097 !FRAME_WINDOW_P (f)
14098 /* Don't try to reuse the display if windows have been split
14099 or such. */
14100 || windows_or_buffers_changed
14101 || cursor_type_changed)
14102 return 0;
14103
14104 /* Can't do this if region may have changed. */
14105 if ((!NILP (Vtransient_mark_mode)
14106 && !NILP (current_buffer->mark_active))
14107 || !NILP (w->region_showing)
14108 || !NILP (Vshow_trailing_whitespace))
14109 return 0;
14110
14111 /* If top-line visibility has changed, give up. */
14112 if (WINDOW_WANTS_HEADER_LINE_P (w)
14113 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14114 return 0;
14115
14116 /* Give up if old or new display is scrolled vertically. We could
14117 make this function handle this, but right now it doesn't. */
14118 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14119 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14120 return 0;
14121
14122 /* The variable new_start now holds the new window start. The old
14123 start `start' can be determined from the current matrix. */
14124 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14125 start = start_row->start.pos;
14126 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14127
14128 /* Clear the desired matrix for the display below. */
14129 clear_glyph_matrix (w->desired_matrix);
14130
14131 if (CHARPOS (new_start) <= CHARPOS (start))
14132 {
14133 int first_row_y;
14134
14135 /* Don't use this method if the display starts with an ellipsis
14136 displayed for invisible text. It's not easy to handle that case
14137 below, and it's certainly not worth the effort since this is
14138 not a frequent case. */
14139 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14140 return 0;
14141
14142 IF_DEBUG (debug_method_add (w, "twu1"));
14143
14144 /* Display up to a row that can be reused. The variable
14145 last_text_row is set to the last row displayed that displays
14146 text. Note that it.vpos == 0 if or if not there is a
14147 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14148 start_display (&it, w, new_start);
14149 first_row_y = it.current_y;
14150 w->cursor.vpos = -1;
14151 last_text_row = last_reused_text_row = NULL;
14152
14153 while (it.current_y < it.last_visible_y
14154 && !fonts_changed_p)
14155 {
14156 /* If we have reached into the characters in the START row,
14157 that means the line boundaries have changed. So we
14158 can't start copying with the row START. Maybe it will
14159 work to start copying with the following row. */
14160 while (IT_CHARPOS (it) > CHARPOS (start))
14161 {
14162 /* Advance to the next row as the "start". */
14163 start_row++;
14164 start = start_row->start.pos;
14165 /* If there are no more rows to try, or just one, give up. */
14166 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14167 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14168 || CHARPOS (start) == ZV)
14169 {
14170 clear_glyph_matrix (w->desired_matrix);
14171 return 0;
14172 }
14173
14174 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14175 }
14176 /* If we have reached alignment,
14177 we can copy the rest of the rows. */
14178 if (IT_CHARPOS (it) == CHARPOS (start))
14179 break;
14180
14181 if (display_line (&it))
14182 last_text_row = it.glyph_row - 1;
14183 }
14184
14185 /* A value of current_y < last_visible_y means that we stopped
14186 at the previous window start, which in turn means that we
14187 have at least one reusable row. */
14188 if (it.current_y < it.last_visible_y)
14189 {
14190 /* IT.vpos always starts from 0; it counts text lines. */
14191 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14192
14193 /* Find PT if not already found in the lines displayed. */
14194 if (w->cursor.vpos < 0)
14195 {
14196 int dy = it.current_y - start_row->y;
14197
14198 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14199 row = row_containing_pos (w, PT, row, NULL, dy);
14200 if (row)
14201 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14202 dy, nrows_scrolled);
14203 else
14204 {
14205 clear_glyph_matrix (w->desired_matrix);
14206 return 0;
14207 }
14208 }
14209
14210 /* Scroll the display. Do it before the current matrix is
14211 changed. The problem here is that update has not yet
14212 run, i.e. part of the current matrix is not up to date.
14213 scroll_run_hook will clear the cursor, and use the
14214 current matrix to get the height of the row the cursor is
14215 in. */
14216 run.current_y = start_row->y;
14217 run.desired_y = it.current_y;
14218 run.height = it.last_visible_y - it.current_y;
14219
14220 if (run.height > 0 && run.current_y != run.desired_y)
14221 {
14222 update_begin (f);
14223 FRAME_RIF (f)->update_window_begin_hook (w);
14224 FRAME_RIF (f)->clear_window_mouse_face (w);
14225 FRAME_RIF (f)->scroll_run_hook (w, &run);
14226 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14227 update_end (f);
14228 }
14229
14230 /* Shift current matrix down by nrows_scrolled lines. */
14231 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14232 rotate_matrix (w->current_matrix,
14233 start_vpos,
14234 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14235 nrows_scrolled);
14236
14237 /* Disable lines that must be updated. */
14238 for (i = 0; i < nrows_scrolled; ++i)
14239 (start_row + i)->enabled_p = 0;
14240
14241 /* Re-compute Y positions. */
14242 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14243 max_y = it.last_visible_y;
14244 for (row = start_row + nrows_scrolled;
14245 row < bottom_row;
14246 ++row)
14247 {
14248 row->y = it.current_y;
14249 row->visible_height = row->height;
14250
14251 if (row->y < min_y)
14252 row->visible_height -= min_y - row->y;
14253 if (row->y + row->height > max_y)
14254 row->visible_height -= row->y + row->height - max_y;
14255 row->redraw_fringe_bitmaps_p = 1;
14256
14257 it.current_y += row->height;
14258
14259 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14260 last_reused_text_row = row;
14261 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14262 break;
14263 }
14264
14265 /* Disable lines in the current matrix which are now
14266 below the window. */
14267 for (++row; row < bottom_row; ++row)
14268 row->enabled_p = row->mode_line_p = 0;
14269 }
14270
14271 /* Update window_end_pos etc.; last_reused_text_row is the last
14272 reused row from the current matrix containing text, if any.
14273 The value of last_text_row is the last displayed line
14274 containing text. */
14275 if (last_reused_text_row)
14276 {
14277 w->window_end_bytepos
14278 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14279 w->window_end_pos
14280 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14281 w->window_end_vpos
14282 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14283 w->current_matrix));
14284 }
14285 else if (last_text_row)
14286 {
14287 w->window_end_bytepos
14288 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14289 w->window_end_pos
14290 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14291 w->window_end_vpos
14292 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14293 }
14294 else
14295 {
14296 /* This window must be completely empty. */
14297 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14298 w->window_end_pos = make_number (Z - ZV);
14299 w->window_end_vpos = make_number (0);
14300 }
14301 w->window_end_valid = Qnil;
14302
14303 /* Update hint: don't try scrolling again in update_window. */
14304 w->desired_matrix->no_scrolling_p = 1;
14305
14306 #if GLYPH_DEBUG
14307 debug_method_add (w, "try_window_reusing_current_matrix 1");
14308 #endif
14309 return 1;
14310 }
14311 else if (CHARPOS (new_start) > CHARPOS (start))
14312 {
14313 struct glyph_row *pt_row, *row;
14314 struct glyph_row *first_reusable_row;
14315 struct glyph_row *first_row_to_display;
14316 int dy;
14317 int yb = window_text_bottom_y (w);
14318
14319 /* Find the row starting at new_start, if there is one. Don't
14320 reuse a partially visible line at the end. */
14321 first_reusable_row = start_row;
14322 while (first_reusable_row->enabled_p
14323 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14324 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14325 < CHARPOS (new_start)))
14326 ++first_reusable_row;
14327
14328 /* Give up if there is no row to reuse. */
14329 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14330 || !first_reusable_row->enabled_p
14331 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14332 != CHARPOS (new_start)))
14333 return 0;
14334
14335 /* We can reuse fully visible rows beginning with
14336 first_reusable_row to the end of the window. Set
14337 first_row_to_display to the first row that cannot be reused.
14338 Set pt_row to the row containing point, if there is any. */
14339 pt_row = NULL;
14340 for (first_row_to_display = first_reusable_row;
14341 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14342 ++first_row_to_display)
14343 {
14344 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14345 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14346 pt_row = first_row_to_display;
14347 }
14348
14349 /* Start displaying at the start of first_row_to_display. */
14350 xassert (first_row_to_display->y < yb);
14351 init_to_row_start (&it, w, first_row_to_display);
14352
14353 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14354 - start_vpos);
14355 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14356 - nrows_scrolled);
14357 it.current_y = (first_row_to_display->y - first_reusable_row->y
14358 + WINDOW_HEADER_LINE_HEIGHT (w));
14359
14360 /* Display lines beginning with first_row_to_display in the
14361 desired matrix. Set last_text_row to the last row displayed
14362 that displays text. */
14363 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14364 if (pt_row == NULL)
14365 w->cursor.vpos = -1;
14366 last_text_row = NULL;
14367 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14368 if (display_line (&it))
14369 last_text_row = it.glyph_row - 1;
14370
14371 /* If point is in a reused row, adjust y and vpos of the cursor
14372 position. */
14373 if (pt_row)
14374 {
14375 w->cursor.vpos -= nrows_scrolled;
14376 w->cursor.y -= first_reusable_row->y - start_row->y;
14377 }
14378
14379 /* Give up if point isn't in a row displayed or reused. (This
14380 also handles the case where w->cursor.vpos < nrows_scrolled
14381 after the calls to display_line, which can happen with scroll
14382 margins. See bug#1295.) */
14383 if (w->cursor.vpos < 0)
14384 {
14385 clear_glyph_matrix (w->desired_matrix);
14386 return 0;
14387 }
14388
14389 /* Scroll the display. */
14390 run.current_y = first_reusable_row->y;
14391 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14392 run.height = it.last_visible_y - run.current_y;
14393 dy = run.current_y - run.desired_y;
14394
14395 if (run.height)
14396 {
14397 update_begin (f);
14398 FRAME_RIF (f)->update_window_begin_hook (w);
14399 FRAME_RIF (f)->clear_window_mouse_face (w);
14400 FRAME_RIF (f)->scroll_run_hook (w, &run);
14401 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14402 update_end (f);
14403 }
14404
14405 /* Adjust Y positions of reused rows. */
14406 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14407 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14408 max_y = it.last_visible_y;
14409 for (row = first_reusable_row; row < first_row_to_display; ++row)
14410 {
14411 row->y -= dy;
14412 row->visible_height = row->height;
14413 if (row->y < min_y)
14414 row->visible_height -= min_y - row->y;
14415 if (row->y + row->height > max_y)
14416 row->visible_height -= row->y + row->height - max_y;
14417 row->redraw_fringe_bitmaps_p = 1;
14418 }
14419
14420 /* Scroll the current matrix. */
14421 xassert (nrows_scrolled > 0);
14422 rotate_matrix (w->current_matrix,
14423 start_vpos,
14424 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14425 -nrows_scrolled);
14426
14427 /* Disable rows not reused. */
14428 for (row -= nrows_scrolled; row < bottom_row; ++row)
14429 row->enabled_p = 0;
14430
14431 /* Point may have moved to a different line, so we cannot assume that
14432 the previous cursor position is valid; locate the correct row. */
14433 if (pt_row)
14434 {
14435 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14436 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14437 row++)
14438 {
14439 w->cursor.vpos++;
14440 w->cursor.y = row->y;
14441 }
14442 if (row < bottom_row)
14443 {
14444 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14445 struct glyph *end = glyph + row->used[TEXT_AREA];
14446
14447 for (; glyph < end
14448 && (!BUFFERP (glyph->object)
14449 || glyph->charpos < PT);
14450 glyph++)
14451 {
14452 w->cursor.hpos++;
14453 w->cursor.x += glyph->pixel_width;
14454 }
14455 }
14456 }
14457
14458 /* Adjust window end. A null value of last_text_row means that
14459 the window end is in reused rows which in turn means that
14460 only its vpos can have changed. */
14461 if (last_text_row)
14462 {
14463 w->window_end_bytepos
14464 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14465 w->window_end_pos
14466 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14467 w->window_end_vpos
14468 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14469 }
14470 else
14471 {
14472 w->window_end_vpos
14473 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14474 }
14475
14476 w->window_end_valid = Qnil;
14477 w->desired_matrix->no_scrolling_p = 1;
14478
14479 #if GLYPH_DEBUG
14480 debug_method_add (w, "try_window_reusing_current_matrix 2");
14481 #endif
14482 return 1;
14483 }
14484
14485 return 0;
14486 }
14487
14488
14489 \f
14490 /************************************************************************
14491 Window redisplay reusing current matrix when buffer has changed
14492 ************************************************************************/
14493
14494 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14495 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14496 int *, int *));
14497 static struct glyph_row *
14498 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14499 struct glyph_row *));
14500
14501
14502 /* Return the last row in MATRIX displaying text. If row START is
14503 non-null, start searching with that row. IT gives the dimensions
14504 of the display. Value is null if matrix is empty; otherwise it is
14505 a pointer to the row found. */
14506
14507 static struct glyph_row *
14508 find_last_row_displaying_text (matrix, it, start)
14509 struct glyph_matrix *matrix;
14510 struct it *it;
14511 struct glyph_row *start;
14512 {
14513 struct glyph_row *row, *row_found;
14514
14515 /* Set row_found to the last row in IT->w's current matrix
14516 displaying text. The loop looks funny but think of partially
14517 visible lines. */
14518 row_found = NULL;
14519 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14520 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14521 {
14522 xassert (row->enabled_p);
14523 row_found = row;
14524 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14525 break;
14526 ++row;
14527 }
14528
14529 return row_found;
14530 }
14531
14532
14533 /* Return the last row in the current matrix of W that is not affected
14534 by changes at the start of current_buffer that occurred since W's
14535 current matrix was built. Value is null if no such row exists.
14536
14537 BEG_UNCHANGED us the number of characters unchanged at the start of
14538 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14539 first changed character in current_buffer. Characters at positions <
14540 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14541 when the current matrix was built. */
14542
14543 static struct glyph_row *
14544 find_last_unchanged_at_beg_row (w)
14545 struct window *w;
14546 {
14547 int first_changed_pos = BEG + BEG_UNCHANGED;
14548 struct glyph_row *row;
14549 struct glyph_row *row_found = NULL;
14550 int yb = window_text_bottom_y (w);
14551
14552 /* Find the last row displaying unchanged text. */
14553 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14554 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14555 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14556 ++row)
14557 {
14558 if (/* If row ends before first_changed_pos, it is unchanged,
14559 except in some case. */
14560 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14561 /* When row ends in ZV and we write at ZV it is not
14562 unchanged. */
14563 && !row->ends_at_zv_p
14564 /* When first_changed_pos is the end of a continued line,
14565 row is not unchanged because it may be no longer
14566 continued. */
14567 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14568 && (row->continued_p
14569 || row->exact_window_width_line_p)))
14570 row_found = row;
14571
14572 /* Stop if last visible row. */
14573 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14574 break;
14575 }
14576
14577 return row_found;
14578 }
14579
14580
14581 /* Find the first glyph row in the current matrix of W that is not
14582 affected by changes at the end of current_buffer since the
14583 time W's current matrix was built.
14584
14585 Return in *DELTA the number of chars by which buffer positions in
14586 unchanged text at the end of current_buffer must be adjusted.
14587
14588 Return in *DELTA_BYTES the corresponding number of bytes.
14589
14590 Value is null if no such row exists, i.e. all rows are affected by
14591 changes. */
14592
14593 static struct glyph_row *
14594 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14595 struct window *w;
14596 int *delta, *delta_bytes;
14597 {
14598 struct glyph_row *row;
14599 struct glyph_row *row_found = NULL;
14600
14601 *delta = *delta_bytes = 0;
14602
14603 /* Display must not have been paused, otherwise the current matrix
14604 is not up to date. */
14605 eassert (!NILP (w->window_end_valid));
14606
14607 /* A value of window_end_pos >= END_UNCHANGED means that the window
14608 end is in the range of changed text. If so, there is no
14609 unchanged row at the end of W's current matrix. */
14610 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14611 return NULL;
14612
14613 /* Set row to the last row in W's current matrix displaying text. */
14614 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14615
14616 /* If matrix is entirely empty, no unchanged row exists. */
14617 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14618 {
14619 /* The value of row is the last glyph row in the matrix having a
14620 meaningful buffer position in it. The end position of row
14621 corresponds to window_end_pos. This allows us to translate
14622 buffer positions in the current matrix to current buffer
14623 positions for characters not in changed text. */
14624 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14625 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14626 int last_unchanged_pos, last_unchanged_pos_old;
14627 struct glyph_row *first_text_row
14628 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14629
14630 *delta = Z - Z_old;
14631 *delta_bytes = Z_BYTE - Z_BYTE_old;
14632
14633 /* Set last_unchanged_pos to the buffer position of the last
14634 character in the buffer that has not been changed. Z is the
14635 index + 1 of the last character in current_buffer, i.e. by
14636 subtracting END_UNCHANGED we get the index of the last
14637 unchanged character, and we have to add BEG to get its buffer
14638 position. */
14639 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14640 last_unchanged_pos_old = last_unchanged_pos - *delta;
14641
14642 /* Search backward from ROW for a row displaying a line that
14643 starts at a minimum position >= last_unchanged_pos_old. */
14644 for (; row > first_text_row; --row)
14645 {
14646 /* This used to abort, but it can happen.
14647 It is ok to just stop the search instead here. KFS. */
14648 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14649 break;
14650
14651 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14652 row_found = row;
14653 }
14654 }
14655
14656 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14657
14658 return row_found;
14659 }
14660
14661
14662 /* Make sure that glyph rows in the current matrix of window W
14663 reference the same glyph memory as corresponding rows in the
14664 frame's frame matrix. This function is called after scrolling W's
14665 current matrix on a terminal frame in try_window_id and
14666 try_window_reusing_current_matrix. */
14667
14668 static void
14669 sync_frame_with_window_matrix_rows (w)
14670 struct window *w;
14671 {
14672 struct frame *f = XFRAME (w->frame);
14673 struct glyph_row *window_row, *window_row_end, *frame_row;
14674
14675 /* Preconditions: W must be a leaf window and full-width. Its frame
14676 must have a frame matrix. */
14677 xassert (NILP (w->hchild) && NILP (w->vchild));
14678 xassert (WINDOW_FULL_WIDTH_P (w));
14679 xassert (!FRAME_WINDOW_P (f));
14680
14681 /* If W is a full-width window, glyph pointers in W's current matrix
14682 have, by definition, to be the same as glyph pointers in the
14683 corresponding frame matrix. Note that frame matrices have no
14684 marginal areas (see build_frame_matrix). */
14685 window_row = w->current_matrix->rows;
14686 window_row_end = window_row + w->current_matrix->nrows;
14687 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14688 while (window_row < window_row_end)
14689 {
14690 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14691 struct glyph *end = window_row->glyphs[LAST_AREA];
14692
14693 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14694 frame_row->glyphs[TEXT_AREA] = start;
14695 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14696 frame_row->glyphs[LAST_AREA] = end;
14697
14698 /* Disable frame rows whose corresponding window rows have
14699 been disabled in try_window_id. */
14700 if (!window_row->enabled_p)
14701 frame_row->enabled_p = 0;
14702
14703 ++window_row, ++frame_row;
14704 }
14705 }
14706
14707
14708 /* Find the glyph row in window W containing CHARPOS. Consider all
14709 rows between START and END (not inclusive). END null means search
14710 all rows to the end of the display area of W. Value is the row
14711 containing CHARPOS or null. */
14712
14713 struct glyph_row *
14714 row_containing_pos (w, charpos, start, end, dy)
14715 struct window *w;
14716 int charpos;
14717 struct glyph_row *start, *end;
14718 int dy;
14719 {
14720 struct glyph_row *row = start;
14721 int last_y;
14722
14723 /* If we happen to start on a header-line, skip that. */
14724 if (row->mode_line_p)
14725 ++row;
14726
14727 if ((end && row >= end) || !row->enabled_p)
14728 return NULL;
14729
14730 last_y = window_text_bottom_y (w) - dy;
14731
14732 while (1)
14733 {
14734 /* Give up if we have gone too far. */
14735 if (end && row >= end)
14736 return NULL;
14737 /* This formerly returned if they were equal.
14738 I think that both quantities are of a "last plus one" type;
14739 if so, when they are equal, the row is within the screen. -- rms. */
14740 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14741 return NULL;
14742
14743 /* If it is in this row, return this row. */
14744 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14745 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14746 /* The end position of a row equals the start
14747 position of the next row. If CHARPOS is there, we
14748 would rather display it in the next line, except
14749 when this line ends in ZV. */
14750 && !row->ends_at_zv_p
14751 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14752 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14753 return row;
14754 ++row;
14755 }
14756 }
14757
14758
14759 /* Try to redisplay window W by reusing its existing display. W's
14760 current matrix must be up to date when this function is called,
14761 i.e. window_end_valid must not be nil.
14762
14763 Value is
14764
14765 1 if display has been updated
14766 0 if otherwise unsuccessful
14767 -1 if redisplay with same window start is known not to succeed
14768
14769 The following steps are performed:
14770
14771 1. Find the last row in the current matrix of W that is not
14772 affected by changes at the start of current_buffer. If no such row
14773 is found, give up.
14774
14775 2. Find the first row in W's current matrix that is not affected by
14776 changes at the end of current_buffer. Maybe there is no such row.
14777
14778 3. Display lines beginning with the row + 1 found in step 1 to the
14779 row found in step 2 or, if step 2 didn't find a row, to the end of
14780 the window.
14781
14782 4. If cursor is not known to appear on the window, give up.
14783
14784 5. If display stopped at the row found in step 2, scroll the
14785 display and current matrix as needed.
14786
14787 6. Maybe display some lines at the end of W, if we must. This can
14788 happen under various circumstances, like a partially visible line
14789 becoming fully visible, or because newly displayed lines are displayed
14790 in smaller font sizes.
14791
14792 7. Update W's window end information. */
14793
14794 static int
14795 try_window_id (w)
14796 struct window *w;
14797 {
14798 struct frame *f = XFRAME (w->frame);
14799 struct glyph_matrix *current_matrix = w->current_matrix;
14800 struct glyph_matrix *desired_matrix = w->desired_matrix;
14801 struct glyph_row *last_unchanged_at_beg_row;
14802 struct glyph_row *first_unchanged_at_end_row;
14803 struct glyph_row *row;
14804 struct glyph_row *bottom_row;
14805 int bottom_vpos;
14806 struct it it;
14807 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14808 struct text_pos start_pos;
14809 struct run run;
14810 int first_unchanged_at_end_vpos = 0;
14811 struct glyph_row *last_text_row, *last_text_row_at_end;
14812 struct text_pos start;
14813 int first_changed_charpos, last_changed_charpos;
14814
14815 #if GLYPH_DEBUG
14816 if (inhibit_try_window_id)
14817 return 0;
14818 #endif
14819
14820 /* This is handy for debugging. */
14821 #if 0
14822 #define GIVE_UP(X) \
14823 do { \
14824 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14825 return 0; \
14826 } while (0)
14827 #else
14828 #define GIVE_UP(X) return 0
14829 #endif
14830
14831 SET_TEXT_POS_FROM_MARKER (start, w->start);
14832
14833 /* Don't use this for mini-windows because these can show
14834 messages and mini-buffers, and we don't handle that here. */
14835 if (MINI_WINDOW_P (w))
14836 GIVE_UP (1);
14837
14838 /* This flag is used to prevent redisplay optimizations. */
14839 if (windows_or_buffers_changed || cursor_type_changed)
14840 GIVE_UP (2);
14841
14842 /* Verify that narrowing has not changed.
14843 Also verify that we were not told to prevent redisplay optimizations.
14844 It would be nice to further
14845 reduce the number of cases where this prevents try_window_id. */
14846 if (current_buffer->clip_changed
14847 || current_buffer->prevent_redisplay_optimizations_p)
14848 GIVE_UP (3);
14849
14850 /* Window must either use window-based redisplay or be full width. */
14851 if (!FRAME_WINDOW_P (f)
14852 && (!FRAME_LINE_INS_DEL_OK (f)
14853 || !WINDOW_FULL_WIDTH_P (w)))
14854 GIVE_UP (4);
14855
14856 /* Give up if point is not known NOT to appear in W. */
14857 if (PT < CHARPOS (start))
14858 GIVE_UP (5);
14859
14860 /* Another way to prevent redisplay optimizations. */
14861 if (XFASTINT (w->last_modified) == 0)
14862 GIVE_UP (6);
14863
14864 /* Verify that window is not hscrolled. */
14865 if (XFASTINT (w->hscroll) != 0)
14866 GIVE_UP (7);
14867
14868 /* Verify that display wasn't paused. */
14869 if (NILP (w->window_end_valid))
14870 GIVE_UP (8);
14871
14872 /* Can't use this if highlighting a region because a cursor movement
14873 will do more than just set the cursor. */
14874 if (!NILP (Vtransient_mark_mode)
14875 && !NILP (current_buffer->mark_active))
14876 GIVE_UP (9);
14877
14878 /* Likewise if highlighting trailing whitespace. */
14879 if (!NILP (Vshow_trailing_whitespace))
14880 GIVE_UP (11);
14881
14882 /* Likewise if showing a region. */
14883 if (!NILP (w->region_showing))
14884 GIVE_UP (10);
14885
14886 /* Can use this if overlay arrow position and or string have changed. */
14887 if (overlay_arrows_changed_p ())
14888 GIVE_UP (12);
14889
14890 /* When word-wrap is on, adding a space to the first word of a
14891 wrapped line can change the wrap position, altering the line
14892 above it. It might be worthwhile to handle this more
14893 intelligently, but for now just redisplay from scratch. */
14894 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14895 GIVE_UP (21);
14896
14897 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14898 only if buffer has really changed. The reason is that the gap is
14899 initially at Z for freshly visited files. The code below would
14900 set end_unchanged to 0 in that case. */
14901 if (MODIFF > SAVE_MODIFF
14902 /* This seems to happen sometimes after saving a buffer. */
14903 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14904 {
14905 if (GPT - BEG < BEG_UNCHANGED)
14906 BEG_UNCHANGED = GPT - BEG;
14907 if (Z - GPT < END_UNCHANGED)
14908 END_UNCHANGED = Z - GPT;
14909 }
14910
14911 /* The position of the first and last character that has been changed. */
14912 first_changed_charpos = BEG + BEG_UNCHANGED;
14913 last_changed_charpos = Z - END_UNCHANGED;
14914
14915 /* If window starts after a line end, and the last change is in
14916 front of that newline, then changes don't affect the display.
14917 This case happens with stealth-fontification. Note that although
14918 the display is unchanged, glyph positions in the matrix have to
14919 be adjusted, of course. */
14920 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14921 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14922 && ((last_changed_charpos < CHARPOS (start)
14923 && CHARPOS (start) == BEGV)
14924 || (last_changed_charpos < CHARPOS (start) - 1
14925 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14926 {
14927 int Z_old, delta, Z_BYTE_old, delta_bytes;
14928 struct glyph_row *r0;
14929
14930 /* Compute how many chars/bytes have been added to or removed
14931 from the buffer. */
14932 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14933 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14934 delta = Z - Z_old;
14935 delta_bytes = Z_BYTE - Z_BYTE_old;
14936
14937 /* Give up if PT is not in the window. Note that it already has
14938 been checked at the start of try_window_id that PT is not in
14939 front of the window start. */
14940 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14941 GIVE_UP (13);
14942
14943 /* If window start is unchanged, we can reuse the whole matrix
14944 as is, after adjusting glyph positions. No need to compute
14945 the window end again, since its offset from Z hasn't changed. */
14946 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14947 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14948 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14949 /* PT must not be in a partially visible line. */
14950 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14951 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14952 {
14953 /* Adjust positions in the glyph matrix. */
14954 if (delta || delta_bytes)
14955 {
14956 struct glyph_row *r1
14957 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14958 increment_matrix_positions (w->current_matrix,
14959 MATRIX_ROW_VPOS (r0, current_matrix),
14960 MATRIX_ROW_VPOS (r1, current_matrix),
14961 delta, delta_bytes);
14962 }
14963
14964 /* Set the cursor. */
14965 row = row_containing_pos (w, PT, r0, NULL, 0);
14966 if (row)
14967 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14968 else
14969 abort ();
14970 return 1;
14971 }
14972 }
14973
14974 /* Handle the case that changes are all below what is displayed in
14975 the window, and that PT is in the window. This shortcut cannot
14976 be taken if ZV is visible in the window, and text has been added
14977 there that is visible in the window. */
14978 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14979 /* ZV is not visible in the window, or there are no
14980 changes at ZV, actually. */
14981 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14982 || first_changed_charpos == last_changed_charpos))
14983 {
14984 struct glyph_row *r0;
14985
14986 /* Give up if PT is not in the window. Note that it already has
14987 been checked at the start of try_window_id that PT is not in
14988 front of the window start. */
14989 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14990 GIVE_UP (14);
14991
14992 /* If window start is unchanged, we can reuse the whole matrix
14993 as is, without changing glyph positions since no text has
14994 been added/removed in front of the window end. */
14995 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14996 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14997 /* PT must not be in a partially visible line. */
14998 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14999 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15000 {
15001 /* We have to compute the window end anew since text
15002 can have been added/removed after it. */
15003 w->window_end_pos
15004 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15005 w->window_end_bytepos
15006 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15007
15008 /* Set the cursor. */
15009 row = row_containing_pos (w, PT, r0, NULL, 0);
15010 if (row)
15011 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15012 else
15013 abort ();
15014 return 2;
15015 }
15016 }
15017
15018 /* Give up if window start is in the changed area.
15019
15020 The condition used to read
15021
15022 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15023
15024 but why that was tested escapes me at the moment. */
15025 if (CHARPOS (start) >= first_changed_charpos
15026 && CHARPOS (start) <= last_changed_charpos)
15027 GIVE_UP (15);
15028
15029 /* Check that window start agrees with the start of the first glyph
15030 row in its current matrix. Check this after we know the window
15031 start is not in changed text, otherwise positions would not be
15032 comparable. */
15033 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15034 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15035 GIVE_UP (16);
15036
15037 /* Give up if the window ends in strings. Overlay strings
15038 at the end are difficult to handle, so don't try. */
15039 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15040 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15041 GIVE_UP (20);
15042
15043 /* Compute the position at which we have to start displaying new
15044 lines. Some of the lines at the top of the window might be
15045 reusable because they are not displaying changed text. Find the
15046 last row in W's current matrix not affected by changes at the
15047 start of current_buffer. Value is null if changes start in the
15048 first line of window. */
15049 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15050 if (last_unchanged_at_beg_row)
15051 {
15052 /* Avoid starting to display in the moddle of a character, a TAB
15053 for instance. This is easier than to set up the iterator
15054 exactly, and it's not a frequent case, so the additional
15055 effort wouldn't really pay off. */
15056 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15057 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15058 && last_unchanged_at_beg_row > w->current_matrix->rows)
15059 --last_unchanged_at_beg_row;
15060
15061 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15062 GIVE_UP (17);
15063
15064 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15065 GIVE_UP (18);
15066 start_pos = it.current.pos;
15067
15068 /* Start displaying new lines in the desired matrix at the same
15069 vpos we would use in the current matrix, i.e. below
15070 last_unchanged_at_beg_row. */
15071 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15072 current_matrix);
15073 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15074 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15075
15076 xassert (it.hpos == 0 && it.current_x == 0);
15077 }
15078 else
15079 {
15080 /* There are no reusable lines at the start of the window.
15081 Start displaying in the first text line. */
15082 start_display (&it, w, start);
15083 it.vpos = it.first_vpos;
15084 start_pos = it.current.pos;
15085 }
15086
15087 /* Find the first row that is not affected by changes at the end of
15088 the buffer. Value will be null if there is no unchanged row, in
15089 which case we must redisplay to the end of the window. delta
15090 will be set to the value by which buffer positions beginning with
15091 first_unchanged_at_end_row have to be adjusted due to text
15092 changes. */
15093 first_unchanged_at_end_row
15094 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15095 IF_DEBUG (debug_delta = delta);
15096 IF_DEBUG (debug_delta_bytes = delta_bytes);
15097
15098 /* Set stop_pos to the buffer position up to which we will have to
15099 display new lines. If first_unchanged_at_end_row != NULL, this
15100 is the buffer position of the start of the line displayed in that
15101 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15102 that we don't stop at a buffer position. */
15103 stop_pos = 0;
15104 if (first_unchanged_at_end_row)
15105 {
15106 xassert (last_unchanged_at_beg_row == NULL
15107 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15108
15109 /* If this is a continuation line, move forward to the next one
15110 that isn't. Changes in lines above affect this line.
15111 Caution: this may move first_unchanged_at_end_row to a row
15112 not displaying text. */
15113 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15114 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15115 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15116 < it.last_visible_y))
15117 ++first_unchanged_at_end_row;
15118
15119 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15120 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15121 >= it.last_visible_y))
15122 first_unchanged_at_end_row = NULL;
15123 else
15124 {
15125 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15126 + delta);
15127 first_unchanged_at_end_vpos
15128 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15129 xassert (stop_pos >= Z - END_UNCHANGED);
15130 }
15131 }
15132 else if (last_unchanged_at_beg_row == NULL)
15133 GIVE_UP (19);
15134
15135
15136 #if GLYPH_DEBUG
15137
15138 /* Either there is no unchanged row at the end, or the one we have
15139 now displays text. This is a necessary condition for the window
15140 end pos calculation at the end of this function. */
15141 xassert (first_unchanged_at_end_row == NULL
15142 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15143
15144 debug_last_unchanged_at_beg_vpos
15145 = (last_unchanged_at_beg_row
15146 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15147 : -1);
15148 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15149
15150 #endif /* GLYPH_DEBUG != 0 */
15151
15152
15153 /* Display new lines. Set last_text_row to the last new line
15154 displayed which has text on it, i.e. might end up as being the
15155 line where the window_end_vpos is. */
15156 w->cursor.vpos = -1;
15157 last_text_row = NULL;
15158 overlay_arrow_seen = 0;
15159 while (it.current_y < it.last_visible_y
15160 && !fonts_changed_p
15161 && (first_unchanged_at_end_row == NULL
15162 || IT_CHARPOS (it) < stop_pos))
15163 {
15164 if (display_line (&it))
15165 last_text_row = it.glyph_row - 1;
15166 }
15167
15168 if (fonts_changed_p)
15169 return -1;
15170
15171
15172 /* Compute differences in buffer positions, y-positions etc. for
15173 lines reused at the bottom of the window. Compute what we can
15174 scroll. */
15175 if (first_unchanged_at_end_row
15176 /* No lines reused because we displayed everything up to the
15177 bottom of the window. */
15178 && it.current_y < it.last_visible_y)
15179 {
15180 dvpos = (it.vpos
15181 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15182 current_matrix));
15183 dy = it.current_y - first_unchanged_at_end_row->y;
15184 run.current_y = first_unchanged_at_end_row->y;
15185 run.desired_y = run.current_y + dy;
15186 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15187 }
15188 else
15189 {
15190 delta = delta_bytes = dvpos = dy
15191 = run.current_y = run.desired_y = run.height = 0;
15192 first_unchanged_at_end_row = NULL;
15193 }
15194 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15195
15196
15197 /* Find the cursor if not already found. We have to decide whether
15198 PT will appear on this window (it sometimes doesn't, but this is
15199 not a very frequent case.) This decision has to be made before
15200 the current matrix is altered. A value of cursor.vpos < 0 means
15201 that PT is either in one of the lines beginning at
15202 first_unchanged_at_end_row or below the window. Don't care for
15203 lines that might be displayed later at the window end; as
15204 mentioned, this is not a frequent case. */
15205 if (w->cursor.vpos < 0)
15206 {
15207 /* Cursor in unchanged rows at the top? */
15208 if (PT < CHARPOS (start_pos)
15209 && last_unchanged_at_beg_row)
15210 {
15211 row = row_containing_pos (w, PT,
15212 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15213 last_unchanged_at_beg_row + 1, 0);
15214 if (row)
15215 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15216 }
15217
15218 /* Start from first_unchanged_at_end_row looking for PT. */
15219 else if (first_unchanged_at_end_row)
15220 {
15221 row = row_containing_pos (w, PT - delta,
15222 first_unchanged_at_end_row, NULL, 0);
15223 if (row)
15224 set_cursor_from_row (w, row, w->current_matrix, delta,
15225 delta_bytes, dy, dvpos);
15226 }
15227
15228 /* Give up if cursor was not found. */
15229 if (w->cursor.vpos < 0)
15230 {
15231 clear_glyph_matrix (w->desired_matrix);
15232 return -1;
15233 }
15234 }
15235
15236 /* Don't let the cursor end in the scroll margins. */
15237 {
15238 int this_scroll_margin, cursor_height;
15239
15240 this_scroll_margin = max (0, scroll_margin);
15241 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15242 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15243 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15244
15245 if ((w->cursor.y < this_scroll_margin
15246 && CHARPOS (start) > BEGV)
15247 /* Old redisplay didn't take scroll margin into account at the bottom,
15248 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15249 || (w->cursor.y + (make_cursor_line_fully_visible_p
15250 ? cursor_height + this_scroll_margin
15251 : 1)) > it.last_visible_y)
15252 {
15253 w->cursor.vpos = -1;
15254 clear_glyph_matrix (w->desired_matrix);
15255 return -1;
15256 }
15257 }
15258
15259 /* Scroll the display. Do it before changing the current matrix so
15260 that xterm.c doesn't get confused about where the cursor glyph is
15261 found. */
15262 if (dy && run.height)
15263 {
15264 update_begin (f);
15265
15266 if (FRAME_WINDOW_P (f))
15267 {
15268 FRAME_RIF (f)->update_window_begin_hook (w);
15269 FRAME_RIF (f)->clear_window_mouse_face (w);
15270 FRAME_RIF (f)->scroll_run_hook (w, &run);
15271 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15272 }
15273 else
15274 {
15275 /* Terminal frame. In this case, dvpos gives the number of
15276 lines to scroll by; dvpos < 0 means scroll up. */
15277 int first_unchanged_at_end_vpos
15278 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15279 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15280 int end = (WINDOW_TOP_EDGE_LINE (w)
15281 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15282 + window_internal_height (w));
15283
15284 /* Perform the operation on the screen. */
15285 if (dvpos > 0)
15286 {
15287 /* Scroll last_unchanged_at_beg_row to the end of the
15288 window down dvpos lines. */
15289 set_terminal_window (f, end);
15290
15291 /* On dumb terminals delete dvpos lines at the end
15292 before inserting dvpos empty lines. */
15293 if (!FRAME_SCROLL_REGION_OK (f))
15294 ins_del_lines (f, end - dvpos, -dvpos);
15295
15296 /* Insert dvpos empty lines in front of
15297 last_unchanged_at_beg_row. */
15298 ins_del_lines (f, from, dvpos);
15299 }
15300 else if (dvpos < 0)
15301 {
15302 /* Scroll up last_unchanged_at_beg_vpos to the end of
15303 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15304 set_terminal_window (f, end);
15305
15306 /* Delete dvpos lines in front of
15307 last_unchanged_at_beg_vpos. ins_del_lines will set
15308 the cursor to the given vpos and emit |dvpos| delete
15309 line sequences. */
15310 ins_del_lines (f, from + dvpos, dvpos);
15311
15312 /* On a dumb terminal insert dvpos empty lines at the
15313 end. */
15314 if (!FRAME_SCROLL_REGION_OK (f))
15315 ins_del_lines (f, end + dvpos, -dvpos);
15316 }
15317
15318 set_terminal_window (f, 0);
15319 }
15320
15321 update_end (f);
15322 }
15323
15324 /* Shift reused rows of the current matrix to the right position.
15325 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15326 text. */
15327 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15328 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15329 if (dvpos < 0)
15330 {
15331 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15332 bottom_vpos, dvpos);
15333 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15334 bottom_vpos, 0);
15335 }
15336 else if (dvpos > 0)
15337 {
15338 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15339 bottom_vpos, dvpos);
15340 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15341 first_unchanged_at_end_vpos + dvpos, 0);
15342 }
15343
15344 /* For frame-based redisplay, make sure that current frame and window
15345 matrix are in sync with respect to glyph memory. */
15346 if (!FRAME_WINDOW_P (f))
15347 sync_frame_with_window_matrix_rows (w);
15348
15349 /* Adjust buffer positions in reused rows. */
15350 if (delta || delta_bytes)
15351 increment_matrix_positions (current_matrix,
15352 first_unchanged_at_end_vpos + dvpos,
15353 bottom_vpos, delta, delta_bytes);
15354
15355 /* Adjust Y positions. */
15356 if (dy)
15357 shift_glyph_matrix (w, current_matrix,
15358 first_unchanged_at_end_vpos + dvpos,
15359 bottom_vpos, dy);
15360
15361 if (first_unchanged_at_end_row)
15362 {
15363 first_unchanged_at_end_row += dvpos;
15364 if (first_unchanged_at_end_row->y >= it.last_visible_y
15365 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15366 first_unchanged_at_end_row = NULL;
15367 }
15368
15369 /* If scrolling up, there may be some lines to display at the end of
15370 the window. */
15371 last_text_row_at_end = NULL;
15372 if (dy < 0)
15373 {
15374 /* Scrolling up can leave for example a partially visible line
15375 at the end of the window to be redisplayed. */
15376 /* Set last_row to the glyph row in the current matrix where the
15377 window end line is found. It has been moved up or down in
15378 the matrix by dvpos. */
15379 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15380 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15381
15382 /* If last_row is the window end line, it should display text. */
15383 xassert (last_row->displays_text_p);
15384
15385 /* If window end line was partially visible before, begin
15386 displaying at that line. Otherwise begin displaying with the
15387 line following it. */
15388 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15389 {
15390 init_to_row_start (&it, w, last_row);
15391 it.vpos = last_vpos;
15392 it.current_y = last_row->y;
15393 }
15394 else
15395 {
15396 init_to_row_end (&it, w, last_row);
15397 it.vpos = 1 + last_vpos;
15398 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15399 ++last_row;
15400 }
15401
15402 /* We may start in a continuation line. If so, we have to
15403 get the right continuation_lines_width and current_x. */
15404 it.continuation_lines_width = last_row->continuation_lines_width;
15405 it.hpos = it.current_x = 0;
15406
15407 /* Display the rest of the lines at the window end. */
15408 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15409 while (it.current_y < it.last_visible_y
15410 && !fonts_changed_p)
15411 {
15412 /* Is it always sure that the display agrees with lines in
15413 the current matrix? I don't think so, so we mark rows
15414 displayed invalid in the current matrix by setting their
15415 enabled_p flag to zero. */
15416 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15417 if (display_line (&it))
15418 last_text_row_at_end = it.glyph_row - 1;
15419 }
15420 }
15421
15422 /* Update window_end_pos and window_end_vpos. */
15423 if (first_unchanged_at_end_row
15424 && !last_text_row_at_end)
15425 {
15426 /* Window end line if one of the preserved rows from the current
15427 matrix. Set row to the last row displaying text in current
15428 matrix starting at first_unchanged_at_end_row, after
15429 scrolling. */
15430 xassert (first_unchanged_at_end_row->displays_text_p);
15431 row = find_last_row_displaying_text (w->current_matrix, &it,
15432 first_unchanged_at_end_row);
15433 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15434
15435 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15436 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15437 w->window_end_vpos
15438 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15439 xassert (w->window_end_bytepos >= 0);
15440 IF_DEBUG (debug_method_add (w, "A"));
15441 }
15442 else if (last_text_row_at_end)
15443 {
15444 w->window_end_pos
15445 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15446 w->window_end_bytepos
15447 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15448 w->window_end_vpos
15449 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15450 xassert (w->window_end_bytepos >= 0);
15451 IF_DEBUG (debug_method_add (w, "B"));
15452 }
15453 else if (last_text_row)
15454 {
15455 /* We have displayed either to the end of the window or at the
15456 end of the window, i.e. the last row with text is to be found
15457 in the desired matrix. */
15458 w->window_end_pos
15459 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15460 w->window_end_bytepos
15461 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15462 w->window_end_vpos
15463 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15464 xassert (w->window_end_bytepos >= 0);
15465 }
15466 else if (first_unchanged_at_end_row == NULL
15467 && last_text_row == NULL
15468 && last_text_row_at_end == NULL)
15469 {
15470 /* Displayed to end of window, but no line containing text was
15471 displayed. Lines were deleted at the end of the window. */
15472 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15473 int vpos = XFASTINT (w->window_end_vpos);
15474 struct glyph_row *current_row = current_matrix->rows + vpos;
15475 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15476
15477 for (row = NULL;
15478 row == NULL && vpos >= first_vpos;
15479 --vpos, --current_row, --desired_row)
15480 {
15481 if (desired_row->enabled_p)
15482 {
15483 if (desired_row->displays_text_p)
15484 row = desired_row;
15485 }
15486 else if (current_row->displays_text_p)
15487 row = current_row;
15488 }
15489
15490 xassert (row != NULL);
15491 w->window_end_vpos = make_number (vpos + 1);
15492 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15493 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15494 xassert (w->window_end_bytepos >= 0);
15495 IF_DEBUG (debug_method_add (w, "C"));
15496 }
15497 else
15498 abort ();
15499
15500 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15501 debug_end_vpos = XFASTINT (w->window_end_vpos));
15502
15503 /* Record that display has not been completed. */
15504 w->window_end_valid = Qnil;
15505 w->desired_matrix->no_scrolling_p = 1;
15506 return 3;
15507
15508 #undef GIVE_UP
15509 }
15510
15511
15512 \f
15513 /***********************************************************************
15514 More debugging support
15515 ***********************************************************************/
15516
15517 #if GLYPH_DEBUG
15518
15519 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15520 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15521 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15522
15523
15524 /* Dump the contents of glyph matrix MATRIX on stderr.
15525
15526 GLYPHS 0 means don't show glyph contents.
15527 GLYPHS 1 means show glyphs in short form
15528 GLYPHS > 1 means show glyphs in long form. */
15529
15530 void
15531 dump_glyph_matrix (matrix, glyphs)
15532 struct glyph_matrix *matrix;
15533 int glyphs;
15534 {
15535 int i;
15536 for (i = 0; i < matrix->nrows; ++i)
15537 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15538 }
15539
15540
15541 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15542 the glyph row and area where the glyph comes from. */
15543
15544 void
15545 dump_glyph (row, glyph, area)
15546 struct glyph_row *row;
15547 struct glyph *glyph;
15548 int area;
15549 {
15550 if (glyph->type == CHAR_GLYPH)
15551 {
15552 fprintf (stderr,
15553 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15554 glyph - row->glyphs[TEXT_AREA],
15555 'C',
15556 glyph->charpos,
15557 (BUFFERP (glyph->object)
15558 ? 'B'
15559 : (STRINGP (glyph->object)
15560 ? 'S'
15561 : '-')),
15562 glyph->pixel_width,
15563 glyph->u.ch,
15564 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15565 ? glyph->u.ch
15566 : '.'),
15567 glyph->face_id,
15568 glyph->left_box_line_p,
15569 glyph->right_box_line_p);
15570 }
15571 else if (glyph->type == STRETCH_GLYPH)
15572 {
15573 fprintf (stderr,
15574 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15575 glyph - row->glyphs[TEXT_AREA],
15576 'S',
15577 glyph->charpos,
15578 (BUFFERP (glyph->object)
15579 ? 'B'
15580 : (STRINGP (glyph->object)
15581 ? 'S'
15582 : '-')),
15583 glyph->pixel_width,
15584 0,
15585 '.',
15586 glyph->face_id,
15587 glyph->left_box_line_p,
15588 glyph->right_box_line_p);
15589 }
15590 else if (glyph->type == IMAGE_GLYPH)
15591 {
15592 fprintf (stderr,
15593 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15594 glyph - row->glyphs[TEXT_AREA],
15595 'I',
15596 glyph->charpos,
15597 (BUFFERP (glyph->object)
15598 ? 'B'
15599 : (STRINGP (glyph->object)
15600 ? 'S'
15601 : '-')),
15602 glyph->pixel_width,
15603 glyph->u.img_id,
15604 '.',
15605 glyph->face_id,
15606 glyph->left_box_line_p,
15607 glyph->right_box_line_p);
15608 }
15609 else if (glyph->type == COMPOSITE_GLYPH)
15610 {
15611 fprintf (stderr,
15612 " %5d %4c %6d %c %3d 0x%05x",
15613 glyph - row->glyphs[TEXT_AREA],
15614 '+',
15615 glyph->charpos,
15616 (BUFFERP (glyph->object)
15617 ? 'B'
15618 : (STRINGP (glyph->object)
15619 ? 'S'
15620 : '-')),
15621 glyph->pixel_width,
15622 glyph->u.cmp.id);
15623 if (glyph->u.cmp.automatic)
15624 fprintf (stderr,
15625 "[%d-%d]",
15626 glyph->u.cmp.from, glyph->u.cmp.to);
15627 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15628 glyph->face_id,
15629 glyph->left_box_line_p,
15630 glyph->right_box_line_p);
15631 }
15632 }
15633
15634
15635 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15636 GLYPHS 0 means don't show glyph contents.
15637 GLYPHS 1 means show glyphs in short form
15638 GLYPHS > 1 means show glyphs in long form. */
15639
15640 void
15641 dump_glyph_row (row, vpos, glyphs)
15642 struct glyph_row *row;
15643 int vpos, glyphs;
15644 {
15645 if (glyphs != 1)
15646 {
15647 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15648 fprintf (stderr, "======================================================================\n");
15649
15650 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15651 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15652 vpos,
15653 MATRIX_ROW_START_CHARPOS (row),
15654 MATRIX_ROW_END_CHARPOS (row),
15655 row->used[TEXT_AREA],
15656 row->contains_overlapping_glyphs_p,
15657 row->enabled_p,
15658 row->truncated_on_left_p,
15659 row->truncated_on_right_p,
15660 row->continued_p,
15661 MATRIX_ROW_CONTINUATION_LINE_P (row),
15662 row->displays_text_p,
15663 row->ends_at_zv_p,
15664 row->fill_line_p,
15665 row->ends_in_middle_of_char_p,
15666 row->starts_in_middle_of_char_p,
15667 row->mouse_face_p,
15668 row->x,
15669 row->y,
15670 row->pixel_width,
15671 row->height,
15672 row->visible_height,
15673 row->ascent,
15674 row->phys_ascent);
15675 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15676 row->end.overlay_string_index,
15677 row->continuation_lines_width);
15678 fprintf (stderr, "%9d %5d\n",
15679 CHARPOS (row->start.string_pos),
15680 CHARPOS (row->end.string_pos));
15681 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15682 row->end.dpvec_index);
15683 }
15684
15685 if (glyphs > 1)
15686 {
15687 int area;
15688
15689 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15690 {
15691 struct glyph *glyph = row->glyphs[area];
15692 struct glyph *glyph_end = glyph + row->used[area];
15693
15694 /* Glyph for a line end in text. */
15695 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15696 ++glyph_end;
15697
15698 if (glyph < glyph_end)
15699 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15700
15701 for (; glyph < glyph_end; ++glyph)
15702 dump_glyph (row, glyph, area);
15703 }
15704 }
15705 else if (glyphs == 1)
15706 {
15707 int area;
15708
15709 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15710 {
15711 char *s = (char *) alloca (row->used[area] + 1);
15712 int i;
15713
15714 for (i = 0; i < row->used[area]; ++i)
15715 {
15716 struct glyph *glyph = row->glyphs[area] + i;
15717 if (glyph->type == CHAR_GLYPH
15718 && glyph->u.ch < 0x80
15719 && glyph->u.ch >= ' ')
15720 s[i] = glyph->u.ch;
15721 else
15722 s[i] = '.';
15723 }
15724
15725 s[i] = '\0';
15726 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15727 }
15728 }
15729 }
15730
15731
15732 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15733 Sdump_glyph_matrix, 0, 1, "p",
15734 doc: /* Dump the current matrix of the selected window to stderr.
15735 Shows contents of glyph row structures. With non-nil
15736 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15737 glyphs in short form, otherwise show glyphs in long form. */)
15738 (glyphs)
15739 Lisp_Object glyphs;
15740 {
15741 struct window *w = XWINDOW (selected_window);
15742 struct buffer *buffer = XBUFFER (w->buffer);
15743
15744 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15745 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15746 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15747 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15748 fprintf (stderr, "=============================================\n");
15749 dump_glyph_matrix (w->current_matrix,
15750 NILP (glyphs) ? 0 : XINT (glyphs));
15751 return Qnil;
15752 }
15753
15754
15755 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15756 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15757 ()
15758 {
15759 struct frame *f = XFRAME (selected_frame);
15760 dump_glyph_matrix (f->current_matrix, 1);
15761 return Qnil;
15762 }
15763
15764
15765 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15766 doc: /* Dump glyph row ROW to stderr.
15767 GLYPH 0 means don't dump glyphs.
15768 GLYPH 1 means dump glyphs in short form.
15769 GLYPH > 1 or omitted means dump glyphs in long form. */)
15770 (row, glyphs)
15771 Lisp_Object row, glyphs;
15772 {
15773 struct glyph_matrix *matrix;
15774 int vpos;
15775
15776 CHECK_NUMBER (row);
15777 matrix = XWINDOW (selected_window)->current_matrix;
15778 vpos = XINT (row);
15779 if (vpos >= 0 && vpos < matrix->nrows)
15780 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15781 vpos,
15782 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15783 return Qnil;
15784 }
15785
15786
15787 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15788 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15789 GLYPH 0 means don't dump glyphs.
15790 GLYPH 1 means dump glyphs in short form.
15791 GLYPH > 1 or omitted means dump glyphs in long form. */)
15792 (row, glyphs)
15793 Lisp_Object row, glyphs;
15794 {
15795 struct frame *sf = SELECTED_FRAME ();
15796 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15797 int vpos;
15798
15799 CHECK_NUMBER (row);
15800 vpos = XINT (row);
15801 if (vpos >= 0 && vpos < m->nrows)
15802 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15803 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15804 return Qnil;
15805 }
15806
15807
15808 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15809 doc: /* Toggle tracing of redisplay.
15810 With ARG, turn tracing on if and only if ARG is positive. */)
15811 (arg)
15812 Lisp_Object arg;
15813 {
15814 if (NILP (arg))
15815 trace_redisplay_p = !trace_redisplay_p;
15816 else
15817 {
15818 arg = Fprefix_numeric_value (arg);
15819 trace_redisplay_p = XINT (arg) > 0;
15820 }
15821
15822 return Qnil;
15823 }
15824
15825
15826 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15827 doc: /* Like `format', but print result to stderr.
15828 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15829 (nargs, args)
15830 int nargs;
15831 Lisp_Object *args;
15832 {
15833 Lisp_Object s = Fformat (nargs, args);
15834 fprintf (stderr, "%s", SDATA (s));
15835 return Qnil;
15836 }
15837
15838 #endif /* GLYPH_DEBUG */
15839
15840
15841 \f
15842 /***********************************************************************
15843 Building Desired Matrix Rows
15844 ***********************************************************************/
15845
15846 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15847 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15848
15849 static struct glyph_row *
15850 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15851 struct window *w;
15852 Lisp_Object overlay_arrow_string;
15853 {
15854 struct frame *f = XFRAME (WINDOW_FRAME (w));
15855 struct buffer *buffer = XBUFFER (w->buffer);
15856 struct buffer *old = current_buffer;
15857 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15858 int arrow_len = SCHARS (overlay_arrow_string);
15859 const unsigned char *arrow_end = arrow_string + arrow_len;
15860 const unsigned char *p;
15861 struct it it;
15862 int multibyte_p;
15863 int n_glyphs_before;
15864
15865 set_buffer_temp (buffer);
15866 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15867 it.glyph_row->used[TEXT_AREA] = 0;
15868 SET_TEXT_POS (it.position, 0, 0);
15869
15870 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15871 p = arrow_string;
15872 while (p < arrow_end)
15873 {
15874 Lisp_Object face, ilisp;
15875
15876 /* Get the next character. */
15877 if (multibyte_p)
15878 it.c = string_char_and_length (p, arrow_len, &it.len);
15879 else
15880 it.c = *p, it.len = 1;
15881 p += it.len;
15882
15883 /* Get its face. */
15884 ilisp = make_number (p - arrow_string);
15885 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15886 it.face_id = compute_char_face (f, it.c, face);
15887
15888 /* Compute its width, get its glyphs. */
15889 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15890 SET_TEXT_POS (it.position, -1, -1);
15891 PRODUCE_GLYPHS (&it);
15892
15893 /* If this character doesn't fit any more in the line, we have
15894 to remove some glyphs. */
15895 if (it.current_x > it.last_visible_x)
15896 {
15897 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15898 break;
15899 }
15900 }
15901
15902 set_buffer_temp (old);
15903 return it.glyph_row;
15904 }
15905
15906
15907 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15908 glyphs are only inserted for terminal frames since we can't really
15909 win with truncation glyphs when partially visible glyphs are
15910 involved. Which glyphs to insert is determined by
15911 produce_special_glyphs. */
15912
15913 static void
15914 insert_left_trunc_glyphs (it)
15915 struct it *it;
15916 {
15917 struct it truncate_it;
15918 struct glyph *from, *end, *to, *toend;
15919
15920 xassert (!FRAME_WINDOW_P (it->f));
15921
15922 /* Get the truncation glyphs. */
15923 truncate_it = *it;
15924 truncate_it.current_x = 0;
15925 truncate_it.face_id = DEFAULT_FACE_ID;
15926 truncate_it.glyph_row = &scratch_glyph_row;
15927 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15928 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15929 truncate_it.object = make_number (0);
15930 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15931
15932 /* Overwrite glyphs from IT with truncation glyphs. */
15933 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15934 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15935 to = it->glyph_row->glyphs[TEXT_AREA];
15936 toend = to + it->glyph_row->used[TEXT_AREA];
15937
15938 while (from < end)
15939 *to++ = *from++;
15940
15941 /* There may be padding glyphs left over. Overwrite them too. */
15942 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15943 {
15944 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15945 while (from < end)
15946 *to++ = *from++;
15947 }
15948
15949 if (to > toend)
15950 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15951 }
15952
15953
15954 /* Compute the pixel height and width of IT->glyph_row.
15955
15956 Most of the time, ascent and height of a display line will be equal
15957 to the max_ascent and max_height values of the display iterator
15958 structure. This is not the case if
15959
15960 1. We hit ZV without displaying anything. In this case, max_ascent
15961 and max_height will be zero.
15962
15963 2. We have some glyphs that don't contribute to the line height.
15964 (The glyph row flag contributes_to_line_height_p is for future
15965 pixmap extensions).
15966
15967 The first case is easily covered by using default values because in
15968 these cases, the line height does not really matter, except that it
15969 must not be zero. */
15970
15971 static void
15972 compute_line_metrics (it)
15973 struct it *it;
15974 {
15975 struct glyph_row *row = it->glyph_row;
15976 int area, i;
15977
15978 if (FRAME_WINDOW_P (it->f))
15979 {
15980 int i, min_y, max_y;
15981
15982 /* The line may consist of one space only, that was added to
15983 place the cursor on it. If so, the row's height hasn't been
15984 computed yet. */
15985 if (row->height == 0)
15986 {
15987 if (it->max_ascent + it->max_descent == 0)
15988 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15989 row->ascent = it->max_ascent;
15990 row->height = it->max_ascent + it->max_descent;
15991 row->phys_ascent = it->max_phys_ascent;
15992 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15993 row->extra_line_spacing = it->max_extra_line_spacing;
15994 }
15995
15996 /* Compute the width of this line. */
15997 row->pixel_width = row->x;
15998 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15999 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16000
16001 xassert (row->pixel_width >= 0);
16002 xassert (row->ascent >= 0 && row->height > 0);
16003
16004 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16005 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16006
16007 /* If first line's physical ascent is larger than its logical
16008 ascent, use the physical ascent, and make the row taller.
16009 This makes accented characters fully visible. */
16010 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16011 && row->phys_ascent > row->ascent)
16012 {
16013 row->height += row->phys_ascent - row->ascent;
16014 row->ascent = row->phys_ascent;
16015 }
16016
16017 /* Compute how much of the line is visible. */
16018 row->visible_height = row->height;
16019
16020 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16021 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16022
16023 if (row->y < min_y)
16024 row->visible_height -= min_y - row->y;
16025 if (row->y + row->height > max_y)
16026 row->visible_height -= row->y + row->height - max_y;
16027 }
16028 else
16029 {
16030 row->pixel_width = row->used[TEXT_AREA];
16031 if (row->continued_p)
16032 row->pixel_width -= it->continuation_pixel_width;
16033 else if (row->truncated_on_right_p)
16034 row->pixel_width -= it->truncation_pixel_width;
16035 row->ascent = row->phys_ascent = 0;
16036 row->height = row->phys_height = row->visible_height = 1;
16037 row->extra_line_spacing = 0;
16038 }
16039
16040 /* Compute a hash code for this row. */
16041 row->hash = 0;
16042 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16043 for (i = 0; i < row->used[area]; ++i)
16044 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16045 + row->glyphs[area][i].u.val
16046 + row->glyphs[area][i].face_id
16047 + row->glyphs[area][i].padding_p
16048 + (row->glyphs[area][i].type << 2));
16049
16050 it->max_ascent = it->max_descent = 0;
16051 it->max_phys_ascent = it->max_phys_descent = 0;
16052 }
16053
16054
16055 /* Append one space to the glyph row of iterator IT if doing a
16056 window-based redisplay. The space has the same face as
16057 IT->face_id. Value is non-zero if a space was added.
16058
16059 This function is called to make sure that there is always one glyph
16060 at the end of a glyph row that the cursor can be set on under
16061 window-systems. (If there weren't such a glyph we would not know
16062 how wide and tall a box cursor should be displayed).
16063
16064 At the same time this space let's a nicely handle clearing to the
16065 end of the line if the row ends in italic text. */
16066
16067 static int
16068 append_space_for_newline (it, default_face_p)
16069 struct it *it;
16070 int default_face_p;
16071 {
16072 if (FRAME_WINDOW_P (it->f))
16073 {
16074 int n = it->glyph_row->used[TEXT_AREA];
16075
16076 if (it->glyph_row->glyphs[TEXT_AREA] + n
16077 < it->glyph_row->glyphs[1 + TEXT_AREA])
16078 {
16079 /* Save some values that must not be changed.
16080 Must save IT->c and IT->len because otherwise
16081 ITERATOR_AT_END_P wouldn't work anymore after
16082 append_space_for_newline has been called. */
16083 enum display_element_type saved_what = it->what;
16084 int saved_c = it->c, saved_len = it->len;
16085 int saved_x = it->current_x;
16086 int saved_face_id = it->face_id;
16087 struct text_pos saved_pos;
16088 Lisp_Object saved_object;
16089 struct face *face;
16090
16091 saved_object = it->object;
16092 saved_pos = it->position;
16093
16094 it->what = IT_CHARACTER;
16095 bzero (&it->position, sizeof it->position);
16096 it->object = make_number (0);
16097 it->c = ' ';
16098 it->len = 1;
16099
16100 if (default_face_p)
16101 it->face_id = DEFAULT_FACE_ID;
16102 else if (it->face_before_selective_p)
16103 it->face_id = it->saved_face_id;
16104 face = FACE_FROM_ID (it->f, it->face_id);
16105 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16106
16107 PRODUCE_GLYPHS (it);
16108
16109 it->override_ascent = -1;
16110 it->constrain_row_ascent_descent_p = 0;
16111 it->current_x = saved_x;
16112 it->object = saved_object;
16113 it->position = saved_pos;
16114 it->what = saved_what;
16115 it->face_id = saved_face_id;
16116 it->len = saved_len;
16117 it->c = saved_c;
16118 return 1;
16119 }
16120 }
16121
16122 return 0;
16123 }
16124
16125
16126 /* Extend the face of the last glyph in the text area of IT->glyph_row
16127 to the end of the display line. Called from display_line.
16128 If the glyph row is empty, add a space glyph to it so that we
16129 know the face to draw. Set the glyph row flag fill_line_p. */
16130
16131 static void
16132 extend_face_to_end_of_line (it)
16133 struct it *it;
16134 {
16135 struct face *face;
16136 struct frame *f = it->f;
16137
16138 /* If line is already filled, do nothing. */
16139 if (it->current_x >= it->last_visible_x)
16140 return;
16141
16142 /* Face extension extends the background and box of IT->face_id
16143 to the end of the line. If the background equals the background
16144 of the frame, we don't have to do anything. */
16145 if (it->face_before_selective_p)
16146 face = FACE_FROM_ID (it->f, it->saved_face_id);
16147 else
16148 face = FACE_FROM_ID (f, it->face_id);
16149
16150 if (FRAME_WINDOW_P (f)
16151 && it->glyph_row->displays_text_p
16152 && face->box == FACE_NO_BOX
16153 && face->background == FRAME_BACKGROUND_PIXEL (f)
16154 && !face->stipple)
16155 return;
16156
16157 /* Set the glyph row flag indicating that the face of the last glyph
16158 in the text area has to be drawn to the end of the text area. */
16159 it->glyph_row->fill_line_p = 1;
16160
16161 /* If current character of IT is not ASCII, make sure we have the
16162 ASCII face. This will be automatically undone the next time
16163 get_next_display_element returns a multibyte character. Note
16164 that the character will always be single byte in unibyte text. */
16165 if (!ASCII_CHAR_P (it->c))
16166 {
16167 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16168 }
16169
16170 if (FRAME_WINDOW_P (f))
16171 {
16172 /* If the row is empty, add a space with the current face of IT,
16173 so that we know which face to draw. */
16174 if (it->glyph_row->used[TEXT_AREA] == 0)
16175 {
16176 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16177 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16178 it->glyph_row->used[TEXT_AREA] = 1;
16179 }
16180 }
16181 else
16182 {
16183 /* Save some values that must not be changed. */
16184 int saved_x = it->current_x;
16185 struct text_pos saved_pos;
16186 Lisp_Object saved_object;
16187 enum display_element_type saved_what = it->what;
16188 int saved_face_id = it->face_id;
16189
16190 saved_object = it->object;
16191 saved_pos = it->position;
16192
16193 it->what = IT_CHARACTER;
16194 bzero (&it->position, sizeof it->position);
16195 it->object = make_number (0);
16196 it->c = ' ';
16197 it->len = 1;
16198 it->face_id = face->id;
16199
16200 PRODUCE_GLYPHS (it);
16201
16202 while (it->current_x <= it->last_visible_x)
16203 PRODUCE_GLYPHS (it);
16204
16205 /* Don't count these blanks really. It would let us insert a left
16206 truncation glyph below and make us set the cursor on them, maybe. */
16207 it->current_x = saved_x;
16208 it->object = saved_object;
16209 it->position = saved_pos;
16210 it->what = saved_what;
16211 it->face_id = saved_face_id;
16212 }
16213 }
16214
16215
16216 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16217 trailing whitespace. */
16218
16219 static int
16220 trailing_whitespace_p (charpos)
16221 int charpos;
16222 {
16223 int bytepos = CHAR_TO_BYTE (charpos);
16224 int c = 0;
16225
16226 while (bytepos < ZV_BYTE
16227 && (c = FETCH_CHAR (bytepos),
16228 c == ' ' || c == '\t'))
16229 ++bytepos;
16230
16231 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16232 {
16233 if (bytepos != PT_BYTE)
16234 return 1;
16235 }
16236 return 0;
16237 }
16238
16239
16240 /* Highlight trailing whitespace, if any, in ROW. */
16241
16242 void
16243 highlight_trailing_whitespace (f, row)
16244 struct frame *f;
16245 struct glyph_row *row;
16246 {
16247 int used = row->used[TEXT_AREA];
16248
16249 if (used)
16250 {
16251 struct glyph *start = row->glyphs[TEXT_AREA];
16252 struct glyph *glyph = start + used - 1;
16253
16254 /* Skip over glyphs inserted to display the cursor at the
16255 end of a line, for extending the face of the last glyph
16256 to the end of the line on terminals, and for truncation
16257 and continuation glyphs. */
16258 while (glyph >= start
16259 && glyph->type == CHAR_GLYPH
16260 && INTEGERP (glyph->object))
16261 --glyph;
16262
16263 /* If last glyph is a space or stretch, and it's trailing
16264 whitespace, set the face of all trailing whitespace glyphs in
16265 IT->glyph_row to `trailing-whitespace'. */
16266 if (glyph >= start
16267 && BUFFERP (glyph->object)
16268 && (glyph->type == STRETCH_GLYPH
16269 || (glyph->type == CHAR_GLYPH
16270 && glyph->u.ch == ' '))
16271 && trailing_whitespace_p (glyph->charpos))
16272 {
16273 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16274 if (face_id < 0)
16275 return;
16276
16277 while (glyph >= start
16278 && BUFFERP (glyph->object)
16279 && (glyph->type == STRETCH_GLYPH
16280 || (glyph->type == CHAR_GLYPH
16281 && glyph->u.ch == ' ')))
16282 (glyph--)->face_id = face_id;
16283 }
16284 }
16285 }
16286
16287
16288 /* Value is non-zero if glyph row ROW in window W should be
16289 used to hold the cursor. */
16290
16291 static int
16292 cursor_row_p (w, row)
16293 struct window *w;
16294 struct glyph_row *row;
16295 {
16296 int cursor_row_p = 1;
16297
16298 if (PT == MATRIX_ROW_END_CHARPOS (row))
16299 {
16300 /* Suppose the row ends on a string.
16301 Unless the row is continued, that means it ends on a newline
16302 in the string. If it's anything other than a display string
16303 (e.g. a before-string from an overlay), we don't want the
16304 cursor there. (This heuristic seems to give the optimal
16305 behavior for the various types of multi-line strings.) */
16306 if (CHARPOS (row->end.string_pos) >= 0)
16307 {
16308 if (row->continued_p)
16309 cursor_row_p = 1;
16310 else
16311 {
16312 /* Check for `display' property. */
16313 struct glyph *beg = row->glyphs[TEXT_AREA];
16314 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16315 struct glyph *glyph;
16316
16317 cursor_row_p = 0;
16318 for (glyph = end; glyph >= beg; --glyph)
16319 if (STRINGP (glyph->object))
16320 {
16321 Lisp_Object prop
16322 = Fget_char_property (make_number (PT),
16323 Qdisplay, Qnil);
16324 cursor_row_p =
16325 (!NILP (prop)
16326 && display_prop_string_p (prop, glyph->object));
16327 break;
16328 }
16329 }
16330 }
16331 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16332 {
16333 /* If the row ends in middle of a real character,
16334 and the line is continued, we want the cursor here.
16335 That's because MATRIX_ROW_END_CHARPOS would equal
16336 PT if PT is before the character. */
16337 if (!row->ends_in_ellipsis_p)
16338 cursor_row_p = row->continued_p;
16339 else
16340 /* If the row ends in an ellipsis, then
16341 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16342 We want that position to be displayed after the ellipsis. */
16343 cursor_row_p = 0;
16344 }
16345 /* If the row ends at ZV, display the cursor at the end of that
16346 row instead of at the start of the row below. */
16347 else if (row->ends_at_zv_p)
16348 cursor_row_p = 1;
16349 else
16350 cursor_row_p = 0;
16351 }
16352
16353 return cursor_row_p;
16354 }
16355
16356 \f
16357
16358 /* Push the display property PROP so that it will be rendered at the
16359 current position in IT. */
16360
16361 static void
16362 push_display_prop (struct it *it, Lisp_Object prop)
16363 {
16364 push_it (it);
16365
16366 /* Never display a cursor on the prefix. */
16367 it->avoid_cursor_p = 1;
16368
16369 if (STRINGP (prop))
16370 {
16371 if (SCHARS (prop) == 0)
16372 {
16373 pop_it (it);
16374 return;
16375 }
16376
16377 it->string = prop;
16378 it->multibyte_p = STRING_MULTIBYTE (it->string);
16379 it->current.overlay_string_index = -1;
16380 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16381 it->end_charpos = it->string_nchars = SCHARS (it->string);
16382 it->method = GET_FROM_STRING;
16383 it->stop_charpos = 0;
16384 }
16385 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16386 {
16387 it->method = GET_FROM_STRETCH;
16388 it->object = prop;
16389 }
16390 #ifdef HAVE_WINDOW_SYSTEM
16391 else if (IMAGEP (prop))
16392 {
16393 it->what = IT_IMAGE;
16394 it->image_id = lookup_image (it->f, prop);
16395 it->method = GET_FROM_IMAGE;
16396 }
16397 #endif /* HAVE_WINDOW_SYSTEM */
16398 else
16399 {
16400 pop_it (it); /* bogus display property, give up */
16401 return;
16402 }
16403 }
16404
16405 /* Return the character-property PROP at the current position in IT. */
16406
16407 static Lisp_Object
16408 get_it_property (it, prop)
16409 struct it *it;
16410 Lisp_Object prop;
16411 {
16412 Lisp_Object position;
16413
16414 if (STRINGP (it->object))
16415 position = make_number (IT_STRING_CHARPOS (*it));
16416 else if (BUFFERP (it->object))
16417 position = make_number (IT_CHARPOS (*it));
16418 else
16419 return Qnil;
16420
16421 return Fget_char_property (position, prop, it->object);
16422 }
16423
16424 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16425
16426 static void
16427 handle_line_prefix (struct it *it)
16428 {
16429 Lisp_Object prefix;
16430 if (it->continuation_lines_width > 0)
16431 {
16432 prefix = get_it_property (it, Qwrap_prefix);
16433 if (NILP (prefix))
16434 prefix = Vwrap_prefix;
16435 }
16436 else
16437 {
16438 prefix = get_it_property (it, Qline_prefix);
16439 if (NILP (prefix))
16440 prefix = Vline_prefix;
16441 }
16442 if (! NILP (prefix))
16443 {
16444 push_display_prop (it, prefix);
16445 /* If the prefix is wider than the window, and we try to wrap
16446 it, it would acquire its own wrap prefix, and so on till the
16447 iterator stack overflows. So, don't wrap the prefix. */
16448 it->line_wrap = TRUNCATE;
16449 }
16450 }
16451
16452 \f
16453
16454 /* Construct the glyph row IT->glyph_row in the desired matrix of
16455 IT->w from text at the current position of IT. See dispextern.h
16456 for an overview of struct it. Value is non-zero if
16457 IT->glyph_row displays text, as opposed to a line displaying ZV
16458 only. */
16459
16460 static int
16461 display_line (it)
16462 struct it *it;
16463 {
16464 struct glyph_row *row = it->glyph_row;
16465 Lisp_Object overlay_arrow_string;
16466 struct it wrap_it;
16467 int may_wrap = 0, wrap_x;
16468 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16469 int wrap_row_phys_ascent, wrap_row_phys_height;
16470 int wrap_row_extra_line_spacing;
16471
16472 /* We always start displaying at hpos zero even if hscrolled. */
16473 xassert (it->hpos == 0 && it->current_x == 0);
16474
16475 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16476 >= it->w->desired_matrix->nrows)
16477 {
16478 it->w->nrows_scale_factor++;
16479 fonts_changed_p = 1;
16480 return 0;
16481 }
16482
16483 /* Is IT->w showing the region? */
16484 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16485
16486 /* Clear the result glyph row and enable it. */
16487 prepare_desired_row (row);
16488
16489 row->y = it->current_y;
16490 row->start = it->start;
16491 row->continuation_lines_width = it->continuation_lines_width;
16492 row->displays_text_p = 1;
16493 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16494 it->starts_in_middle_of_char_p = 0;
16495
16496 /* Arrange the overlays nicely for our purposes. Usually, we call
16497 display_line on only one line at a time, in which case this
16498 can't really hurt too much, or we call it on lines which appear
16499 one after another in the buffer, in which case all calls to
16500 recenter_overlay_lists but the first will be pretty cheap. */
16501 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16502
16503 /* Move over display elements that are not visible because we are
16504 hscrolled. This may stop at an x-position < IT->first_visible_x
16505 if the first glyph is partially visible or if we hit a line end. */
16506 if (it->current_x < it->first_visible_x)
16507 {
16508 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16509 MOVE_TO_POS | MOVE_TO_X);
16510 }
16511 else
16512 {
16513 /* We only do this when not calling `move_it_in_display_line_to'
16514 above, because move_it_in_display_line_to calls
16515 handle_line_prefix itself. */
16516 handle_line_prefix (it);
16517 }
16518
16519 /* Get the initial row height. This is either the height of the
16520 text hscrolled, if there is any, or zero. */
16521 row->ascent = it->max_ascent;
16522 row->height = it->max_ascent + it->max_descent;
16523 row->phys_ascent = it->max_phys_ascent;
16524 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16525 row->extra_line_spacing = it->max_extra_line_spacing;
16526
16527 /* Loop generating characters. The loop is left with IT on the next
16528 character to display. */
16529 while (1)
16530 {
16531 int n_glyphs_before, hpos_before, x_before;
16532 int x, i, nglyphs;
16533 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16534
16535 /* Retrieve the next thing to display. Value is zero if end of
16536 buffer reached. */
16537 if (!get_next_display_element (it))
16538 {
16539 /* Maybe add a space at the end of this line that is used to
16540 display the cursor there under X. Set the charpos of the
16541 first glyph of blank lines not corresponding to any text
16542 to -1. */
16543 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16544 row->exact_window_width_line_p = 1;
16545 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16546 || row->used[TEXT_AREA] == 0)
16547 {
16548 row->glyphs[TEXT_AREA]->charpos = -1;
16549 row->displays_text_p = 0;
16550
16551 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16552 && (!MINI_WINDOW_P (it->w)
16553 || (minibuf_level && EQ (it->window, minibuf_window))))
16554 row->indicate_empty_line_p = 1;
16555 }
16556
16557 it->continuation_lines_width = 0;
16558 row->ends_at_zv_p = 1;
16559 break;
16560 }
16561
16562 /* Now, get the metrics of what we want to display. This also
16563 generates glyphs in `row' (which is IT->glyph_row). */
16564 n_glyphs_before = row->used[TEXT_AREA];
16565 x = it->current_x;
16566
16567 /* Remember the line height so far in case the next element doesn't
16568 fit on the line. */
16569 if (it->line_wrap != TRUNCATE)
16570 {
16571 ascent = it->max_ascent;
16572 descent = it->max_descent;
16573 phys_ascent = it->max_phys_ascent;
16574 phys_descent = it->max_phys_descent;
16575
16576 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16577 {
16578 if (IT_DISPLAYING_WHITESPACE (it))
16579 may_wrap = 1;
16580 else if (may_wrap)
16581 {
16582 wrap_it = *it;
16583 wrap_x = x;
16584 wrap_row_used = row->used[TEXT_AREA];
16585 wrap_row_ascent = row->ascent;
16586 wrap_row_height = row->height;
16587 wrap_row_phys_ascent = row->phys_ascent;
16588 wrap_row_phys_height = row->phys_height;
16589 wrap_row_extra_line_spacing = row->extra_line_spacing;
16590 may_wrap = 0;
16591 }
16592 }
16593 }
16594
16595 PRODUCE_GLYPHS (it);
16596
16597 /* If this display element was in marginal areas, continue with
16598 the next one. */
16599 if (it->area != TEXT_AREA)
16600 {
16601 row->ascent = max (row->ascent, it->max_ascent);
16602 row->height = max (row->height, it->max_ascent + it->max_descent);
16603 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16604 row->phys_height = max (row->phys_height,
16605 it->max_phys_ascent + it->max_phys_descent);
16606 row->extra_line_spacing = max (row->extra_line_spacing,
16607 it->max_extra_line_spacing);
16608 set_iterator_to_next (it, 1);
16609 continue;
16610 }
16611
16612 /* Does the display element fit on the line? If we truncate
16613 lines, we should draw past the right edge of the window. If
16614 we don't truncate, we want to stop so that we can display the
16615 continuation glyph before the right margin. If lines are
16616 continued, there are two possible strategies for characters
16617 resulting in more than 1 glyph (e.g. tabs): Display as many
16618 glyphs as possible in this line and leave the rest for the
16619 continuation line, or display the whole element in the next
16620 line. Original redisplay did the former, so we do it also. */
16621 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16622 hpos_before = it->hpos;
16623 x_before = x;
16624
16625 if (/* Not a newline. */
16626 nglyphs > 0
16627 /* Glyphs produced fit entirely in the line. */
16628 && it->current_x < it->last_visible_x)
16629 {
16630 it->hpos += nglyphs;
16631 row->ascent = max (row->ascent, it->max_ascent);
16632 row->height = max (row->height, it->max_ascent + it->max_descent);
16633 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16634 row->phys_height = max (row->phys_height,
16635 it->max_phys_ascent + it->max_phys_descent);
16636 row->extra_line_spacing = max (row->extra_line_spacing,
16637 it->max_extra_line_spacing);
16638 if (it->current_x - it->pixel_width < it->first_visible_x)
16639 row->x = x - it->first_visible_x;
16640 }
16641 else
16642 {
16643 int new_x;
16644 struct glyph *glyph;
16645
16646 for (i = 0; i < nglyphs; ++i, x = new_x)
16647 {
16648 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16649 new_x = x + glyph->pixel_width;
16650
16651 if (/* Lines are continued. */
16652 it->line_wrap != TRUNCATE
16653 && (/* Glyph doesn't fit on the line. */
16654 new_x > it->last_visible_x
16655 /* Or it fits exactly on a window system frame. */
16656 || (new_x == it->last_visible_x
16657 && FRAME_WINDOW_P (it->f))))
16658 {
16659 /* End of a continued line. */
16660
16661 if (it->hpos == 0
16662 || (new_x == it->last_visible_x
16663 && FRAME_WINDOW_P (it->f)))
16664 {
16665 /* Current glyph is the only one on the line or
16666 fits exactly on the line. We must continue
16667 the line because we can't draw the cursor
16668 after the glyph. */
16669 row->continued_p = 1;
16670 it->current_x = new_x;
16671 it->continuation_lines_width += new_x;
16672 ++it->hpos;
16673 if (i == nglyphs - 1)
16674 {
16675 /* If line-wrap is on, check if a previous
16676 wrap point was found. */
16677 if (wrap_row_used > 0
16678 /* Even if there is a previous wrap
16679 point, continue the line here as
16680 usual, if (i) the previous character
16681 was a space or tab AND (ii) the
16682 current character is not. */
16683 && (!may_wrap
16684 || IT_DISPLAYING_WHITESPACE (it)))
16685 goto back_to_wrap;
16686
16687 set_iterator_to_next (it, 1);
16688 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16689 {
16690 if (!get_next_display_element (it))
16691 {
16692 row->exact_window_width_line_p = 1;
16693 it->continuation_lines_width = 0;
16694 row->continued_p = 0;
16695 row->ends_at_zv_p = 1;
16696 }
16697 else if (ITERATOR_AT_END_OF_LINE_P (it))
16698 {
16699 row->continued_p = 0;
16700 row->exact_window_width_line_p = 1;
16701 }
16702 }
16703 }
16704 }
16705 else if (CHAR_GLYPH_PADDING_P (*glyph)
16706 && !FRAME_WINDOW_P (it->f))
16707 {
16708 /* A padding glyph that doesn't fit on this line.
16709 This means the whole character doesn't fit
16710 on the line. */
16711 row->used[TEXT_AREA] = n_glyphs_before;
16712
16713 /* Fill the rest of the row with continuation
16714 glyphs like in 20.x. */
16715 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16716 < row->glyphs[1 + TEXT_AREA])
16717 produce_special_glyphs (it, IT_CONTINUATION);
16718
16719 row->continued_p = 1;
16720 it->current_x = x_before;
16721 it->continuation_lines_width += x_before;
16722
16723 /* Restore the height to what it was before the
16724 element not fitting on the line. */
16725 it->max_ascent = ascent;
16726 it->max_descent = descent;
16727 it->max_phys_ascent = phys_ascent;
16728 it->max_phys_descent = phys_descent;
16729 }
16730 else if (wrap_row_used > 0)
16731 {
16732 back_to_wrap:
16733 *it = wrap_it;
16734 it->continuation_lines_width += wrap_x;
16735 row->used[TEXT_AREA] = wrap_row_used;
16736 row->ascent = wrap_row_ascent;
16737 row->height = wrap_row_height;
16738 row->phys_ascent = wrap_row_phys_ascent;
16739 row->phys_height = wrap_row_phys_height;
16740 row->extra_line_spacing = wrap_row_extra_line_spacing;
16741 row->continued_p = 1;
16742 row->ends_at_zv_p = 0;
16743 row->exact_window_width_line_p = 0;
16744 it->continuation_lines_width += x;
16745
16746 /* Make sure that a non-default face is extended
16747 up to the right margin of the window. */
16748 extend_face_to_end_of_line (it);
16749 }
16750 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16751 {
16752 /* A TAB that extends past the right edge of the
16753 window. This produces a single glyph on
16754 window system frames. We leave the glyph in
16755 this row and let it fill the row, but don't
16756 consume the TAB. */
16757 it->continuation_lines_width += it->last_visible_x;
16758 row->ends_in_middle_of_char_p = 1;
16759 row->continued_p = 1;
16760 glyph->pixel_width = it->last_visible_x - x;
16761 it->starts_in_middle_of_char_p = 1;
16762 }
16763 else
16764 {
16765 /* Something other than a TAB that draws past
16766 the right edge of the window. Restore
16767 positions to values before the element. */
16768 row->used[TEXT_AREA] = n_glyphs_before + i;
16769
16770 /* Display continuation glyphs. */
16771 if (!FRAME_WINDOW_P (it->f))
16772 produce_special_glyphs (it, IT_CONTINUATION);
16773 row->continued_p = 1;
16774
16775 it->current_x = x_before;
16776 it->continuation_lines_width += x;
16777 extend_face_to_end_of_line (it);
16778
16779 if (nglyphs > 1 && i > 0)
16780 {
16781 row->ends_in_middle_of_char_p = 1;
16782 it->starts_in_middle_of_char_p = 1;
16783 }
16784
16785 /* Restore the height to what it was before the
16786 element not fitting on the line. */
16787 it->max_ascent = ascent;
16788 it->max_descent = descent;
16789 it->max_phys_ascent = phys_ascent;
16790 it->max_phys_descent = phys_descent;
16791 }
16792
16793 break;
16794 }
16795 else if (new_x > it->first_visible_x)
16796 {
16797 /* Increment number of glyphs actually displayed. */
16798 ++it->hpos;
16799
16800 if (x < it->first_visible_x)
16801 /* Glyph is partially visible, i.e. row starts at
16802 negative X position. */
16803 row->x = x - it->first_visible_x;
16804 }
16805 else
16806 {
16807 /* Glyph is completely off the left margin of the
16808 window. This should not happen because of the
16809 move_it_in_display_line at the start of this
16810 function, unless the text display area of the
16811 window is empty. */
16812 xassert (it->first_visible_x <= it->last_visible_x);
16813 }
16814 }
16815
16816 row->ascent = max (row->ascent, it->max_ascent);
16817 row->height = max (row->height, it->max_ascent + it->max_descent);
16818 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16819 row->phys_height = max (row->phys_height,
16820 it->max_phys_ascent + it->max_phys_descent);
16821 row->extra_line_spacing = max (row->extra_line_spacing,
16822 it->max_extra_line_spacing);
16823
16824 /* End of this display line if row is continued. */
16825 if (row->continued_p || row->ends_at_zv_p)
16826 break;
16827 }
16828
16829 at_end_of_line:
16830 /* Is this a line end? If yes, we're also done, after making
16831 sure that a non-default face is extended up to the right
16832 margin of the window. */
16833 if (ITERATOR_AT_END_OF_LINE_P (it))
16834 {
16835 int used_before = row->used[TEXT_AREA];
16836
16837 row->ends_in_newline_from_string_p = STRINGP (it->object);
16838
16839 /* Add a space at the end of the line that is used to
16840 display the cursor there. */
16841 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16842 append_space_for_newline (it, 0);
16843
16844 /* Extend the face to the end of the line. */
16845 extend_face_to_end_of_line (it);
16846
16847 /* Make sure we have the position. */
16848 if (used_before == 0)
16849 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16850
16851 /* Consume the line end. This skips over invisible lines. */
16852 set_iterator_to_next (it, 1);
16853 it->continuation_lines_width = 0;
16854 break;
16855 }
16856
16857 /* Proceed with next display element. Note that this skips
16858 over lines invisible because of selective display. */
16859 set_iterator_to_next (it, 1);
16860
16861 /* If we truncate lines, we are done when the last displayed
16862 glyphs reach past the right margin of the window. */
16863 if (it->line_wrap == TRUNCATE
16864 && (FRAME_WINDOW_P (it->f)
16865 ? (it->current_x >= it->last_visible_x)
16866 : (it->current_x > it->last_visible_x)))
16867 {
16868 /* Maybe add truncation glyphs. */
16869 if (!FRAME_WINDOW_P (it->f))
16870 {
16871 int i, n;
16872
16873 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16874 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16875 break;
16876
16877 for (n = row->used[TEXT_AREA]; i < n; ++i)
16878 {
16879 row->used[TEXT_AREA] = i;
16880 produce_special_glyphs (it, IT_TRUNCATION);
16881 }
16882 }
16883 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16884 {
16885 /* Don't truncate if we can overflow newline into fringe. */
16886 if (!get_next_display_element (it))
16887 {
16888 it->continuation_lines_width = 0;
16889 row->ends_at_zv_p = 1;
16890 row->exact_window_width_line_p = 1;
16891 break;
16892 }
16893 if (ITERATOR_AT_END_OF_LINE_P (it))
16894 {
16895 row->exact_window_width_line_p = 1;
16896 goto at_end_of_line;
16897 }
16898 }
16899
16900 row->truncated_on_right_p = 1;
16901 it->continuation_lines_width = 0;
16902 reseat_at_next_visible_line_start (it, 0);
16903 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16904 it->hpos = hpos_before;
16905 it->current_x = x_before;
16906 break;
16907 }
16908 }
16909
16910 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16911 at the left window margin. */
16912 if (it->first_visible_x
16913 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16914 {
16915 if (!FRAME_WINDOW_P (it->f))
16916 insert_left_trunc_glyphs (it);
16917 row->truncated_on_left_p = 1;
16918 }
16919
16920 /* If the start of this line is the overlay arrow-position, then
16921 mark this glyph row as the one containing the overlay arrow.
16922 This is clearly a mess with variable size fonts. It would be
16923 better to let it be displayed like cursors under X. */
16924 if ((row->displays_text_p || !overlay_arrow_seen)
16925 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16926 !NILP (overlay_arrow_string)))
16927 {
16928 /* Overlay arrow in window redisplay is a fringe bitmap. */
16929 if (STRINGP (overlay_arrow_string))
16930 {
16931 struct glyph_row *arrow_row
16932 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16933 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16934 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16935 struct glyph *p = row->glyphs[TEXT_AREA];
16936 struct glyph *p2, *end;
16937
16938 /* Copy the arrow glyphs. */
16939 while (glyph < arrow_end)
16940 *p++ = *glyph++;
16941
16942 /* Throw away padding glyphs. */
16943 p2 = p;
16944 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16945 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16946 ++p2;
16947 if (p2 > p)
16948 {
16949 while (p2 < end)
16950 *p++ = *p2++;
16951 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16952 }
16953 }
16954 else
16955 {
16956 xassert (INTEGERP (overlay_arrow_string));
16957 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16958 }
16959 overlay_arrow_seen = 1;
16960 }
16961
16962 /* Compute pixel dimensions of this line. */
16963 compute_line_metrics (it);
16964
16965 /* Remember the position at which this line ends. */
16966 row->end = it->current;
16967
16968 /* Record whether this row ends inside an ellipsis. */
16969 row->ends_in_ellipsis_p
16970 = (it->method == GET_FROM_DISPLAY_VECTOR
16971 && it->ellipsis_p);
16972
16973 /* Save fringe bitmaps in this row. */
16974 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16975 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16976 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16977 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16978
16979 it->left_user_fringe_bitmap = 0;
16980 it->left_user_fringe_face_id = 0;
16981 it->right_user_fringe_bitmap = 0;
16982 it->right_user_fringe_face_id = 0;
16983
16984 /* Maybe set the cursor. */
16985 if (it->w->cursor.vpos < 0
16986 && PT >= MATRIX_ROW_START_CHARPOS (row)
16987 && PT <= MATRIX_ROW_END_CHARPOS (row)
16988 && cursor_row_p (it->w, row))
16989 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16990
16991 /* Highlight trailing whitespace. */
16992 if (!NILP (Vshow_trailing_whitespace))
16993 highlight_trailing_whitespace (it->f, it->glyph_row);
16994
16995 /* Prepare for the next line. This line starts horizontally at (X
16996 HPOS) = (0 0). Vertical positions are incremented. As a
16997 convenience for the caller, IT->glyph_row is set to the next
16998 row to be used. */
16999 it->current_x = it->hpos = 0;
17000 it->current_y += row->height;
17001 ++it->vpos;
17002 ++it->glyph_row;
17003 it->start = it->current;
17004 return row->displays_text_p;
17005 }
17006
17007
17008 \f
17009 /***********************************************************************
17010 Menu Bar
17011 ***********************************************************************/
17012
17013 /* Redisplay the menu bar in the frame for window W.
17014
17015 The menu bar of X frames that don't have X toolkit support is
17016 displayed in a special window W->frame->menu_bar_window.
17017
17018 The menu bar of terminal frames is treated specially as far as
17019 glyph matrices are concerned. Menu bar lines are not part of
17020 windows, so the update is done directly on the frame matrix rows
17021 for the menu bar. */
17022
17023 static void
17024 display_menu_bar (w)
17025 struct window *w;
17026 {
17027 struct frame *f = XFRAME (WINDOW_FRAME (w));
17028 struct it it;
17029 Lisp_Object items;
17030 int i;
17031
17032 /* Don't do all this for graphical frames. */
17033 #ifdef HAVE_NTGUI
17034 if (FRAME_W32_P (f))
17035 return;
17036 #endif
17037 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17038 if (FRAME_X_P (f))
17039 return;
17040 #endif
17041
17042 #ifdef HAVE_NS
17043 if (FRAME_NS_P (f))
17044 return;
17045 #endif /* HAVE_NS */
17046
17047 #ifdef USE_X_TOOLKIT
17048 xassert (!FRAME_WINDOW_P (f));
17049 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17050 it.first_visible_x = 0;
17051 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17052 #else /* not USE_X_TOOLKIT */
17053 if (FRAME_WINDOW_P (f))
17054 {
17055 /* Menu bar lines are displayed in the desired matrix of the
17056 dummy window menu_bar_window. */
17057 struct window *menu_w;
17058 xassert (WINDOWP (f->menu_bar_window));
17059 menu_w = XWINDOW (f->menu_bar_window);
17060 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17061 MENU_FACE_ID);
17062 it.first_visible_x = 0;
17063 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17064 }
17065 else
17066 {
17067 /* This is a TTY frame, i.e. character hpos/vpos are used as
17068 pixel x/y. */
17069 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17070 MENU_FACE_ID);
17071 it.first_visible_x = 0;
17072 it.last_visible_x = FRAME_COLS (f);
17073 }
17074 #endif /* not USE_X_TOOLKIT */
17075
17076 if (! mode_line_inverse_video)
17077 /* Force the menu-bar to be displayed in the default face. */
17078 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17079
17080 /* Clear all rows of the menu bar. */
17081 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17082 {
17083 struct glyph_row *row = it.glyph_row + i;
17084 clear_glyph_row (row);
17085 row->enabled_p = 1;
17086 row->full_width_p = 1;
17087 }
17088
17089 /* Display all items of the menu bar. */
17090 items = FRAME_MENU_BAR_ITEMS (it.f);
17091 for (i = 0; i < XVECTOR (items)->size; i += 4)
17092 {
17093 Lisp_Object string;
17094
17095 /* Stop at nil string. */
17096 string = AREF (items, i + 1);
17097 if (NILP (string))
17098 break;
17099
17100 /* Remember where item was displayed. */
17101 ASET (items, i + 3, make_number (it.hpos));
17102
17103 /* Display the item, pad with one space. */
17104 if (it.current_x < it.last_visible_x)
17105 display_string (NULL, string, Qnil, 0, 0, &it,
17106 SCHARS (string) + 1, 0, 0, -1);
17107 }
17108
17109 /* Fill out the line with spaces. */
17110 if (it.current_x < it.last_visible_x)
17111 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17112
17113 /* Compute the total height of the lines. */
17114 compute_line_metrics (&it);
17115 }
17116
17117
17118 \f
17119 /***********************************************************************
17120 Mode Line
17121 ***********************************************************************/
17122
17123 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17124 FORCE is non-zero, redisplay mode lines unconditionally.
17125 Otherwise, redisplay only mode lines that are garbaged. Value is
17126 the number of windows whose mode lines were redisplayed. */
17127
17128 static int
17129 redisplay_mode_lines (window, force)
17130 Lisp_Object window;
17131 int force;
17132 {
17133 int nwindows = 0;
17134
17135 while (!NILP (window))
17136 {
17137 struct window *w = XWINDOW (window);
17138
17139 if (WINDOWP (w->hchild))
17140 nwindows += redisplay_mode_lines (w->hchild, force);
17141 else if (WINDOWP (w->vchild))
17142 nwindows += redisplay_mode_lines (w->vchild, force);
17143 else if (force
17144 || FRAME_GARBAGED_P (XFRAME (w->frame))
17145 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17146 {
17147 struct text_pos lpoint;
17148 struct buffer *old = current_buffer;
17149
17150 /* Set the window's buffer for the mode line display. */
17151 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17152 set_buffer_internal_1 (XBUFFER (w->buffer));
17153
17154 /* Point refers normally to the selected window. For any
17155 other window, set up appropriate value. */
17156 if (!EQ (window, selected_window))
17157 {
17158 struct text_pos pt;
17159
17160 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17161 if (CHARPOS (pt) < BEGV)
17162 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17163 else if (CHARPOS (pt) > (ZV - 1))
17164 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17165 else
17166 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17167 }
17168
17169 /* Display mode lines. */
17170 clear_glyph_matrix (w->desired_matrix);
17171 if (display_mode_lines (w))
17172 {
17173 ++nwindows;
17174 w->must_be_updated_p = 1;
17175 }
17176
17177 /* Restore old settings. */
17178 set_buffer_internal_1 (old);
17179 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17180 }
17181
17182 window = w->next;
17183 }
17184
17185 return nwindows;
17186 }
17187
17188
17189 /* Display the mode and/or top line of window W. Value is the number
17190 of mode lines displayed. */
17191
17192 static int
17193 display_mode_lines (w)
17194 struct window *w;
17195 {
17196 Lisp_Object old_selected_window, old_selected_frame;
17197 int n = 0;
17198
17199 old_selected_frame = selected_frame;
17200 selected_frame = w->frame;
17201 old_selected_window = selected_window;
17202 XSETWINDOW (selected_window, w);
17203
17204 /* These will be set while the mode line specs are processed. */
17205 line_number_displayed = 0;
17206 w->column_number_displayed = Qnil;
17207
17208 if (WINDOW_WANTS_MODELINE_P (w))
17209 {
17210 struct window *sel_w = XWINDOW (old_selected_window);
17211
17212 /* Select mode line face based on the real selected window. */
17213 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17214 current_buffer->mode_line_format);
17215 ++n;
17216 }
17217
17218 if (WINDOW_WANTS_HEADER_LINE_P (w))
17219 {
17220 display_mode_line (w, HEADER_LINE_FACE_ID,
17221 current_buffer->header_line_format);
17222 ++n;
17223 }
17224
17225 selected_frame = old_selected_frame;
17226 selected_window = old_selected_window;
17227 return n;
17228 }
17229
17230
17231 /* Display mode or top line of window W. FACE_ID specifies which line
17232 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17233 FORMAT is the mode line format to display. Value is the pixel
17234 height of the mode line displayed. */
17235
17236 static int
17237 display_mode_line (w, face_id, format)
17238 struct window *w;
17239 enum face_id face_id;
17240 Lisp_Object format;
17241 {
17242 struct it it;
17243 struct face *face;
17244 int count = SPECPDL_INDEX ();
17245
17246 init_iterator (&it, w, -1, -1, NULL, face_id);
17247 /* Don't extend on a previously drawn mode-line.
17248 This may happen if called from pos_visible_p. */
17249 it.glyph_row->enabled_p = 0;
17250 prepare_desired_row (it.glyph_row);
17251
17252 it.glyph_row->mode_line_p = 1;
17253
17254 if (! mode_line_inverse_video)
17255 /* Force the mode-line to be displayed in the default face. */
17256 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17257
17258 record_unwind_protect (unwind_format_mode_line,
17259 format_mode_line_unwind_data (NULL, Qnil, 0));
17260
17261 mode_line_target = MODE_LINE_DISPLAY;
17262
17263 /* Temporarily make frame's keyboard the current kboard so that
17264 kboard-local variables in the mode_line_format will get the right
17265 values. */
17266 push_kboard (FRAME_KBOARD (it.f));
17267 record_unwind_save_match_data ();
17268 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17269 pop_kboard ();
17270
17271 unbind_to (count, Qnil);
17272
17273 /* Fill up with spaces. */
17274 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17275
17276 compute_line_metrics (&it);
17277 it.glyph_row->full_width_p = 1;
17278 it.glyph_row->continued_p = 0;
17279 it.glyph_row->truncated_on_left_p = 0;
17280 it.glyph_row->truncated_on_right_p = 0;
17281
17282 /* Make a 3D mode-line have a shadow at its right end. */
17283 face = FACE_FROM_ID (it.f, face_id);
17284 extend_face_to_end_of_line (&it);
17285 if (face->box != FACE_NO_BOX)
17286 {
17287 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17288 + it.glyph_row->used[TEXT_AREA] - 1);
17289 last->right_box_line_p = 1;
17290 }
17291
17292 return it.glyph_row->height;
17293 }
17294
17295 /* Move element ELT in LIST to the front of LIST.
17296 Return the updated list. */
17297
17298 static Lisp_Object
17299 move_elt_to_front (elt, list)
17300 Lisp_Object elt, list;
17301 {
17302 register Lisp_Object tail, prev;
17303 register Lisp_Object tem;
17304
17305 tail = list;
17306 prev = Qnil;
17307 while (CONSP (tail))
17308 {
17309 tem = XCAR (tail);
17310
17311 if (EQ (elt, tem))
17312 {
17313 /* Splice out the link TAIL. */
17314 if (NILP (prev))
17315 list = XCDR (tail);
17316 else
17317 Fsetcdr (prev, XCDR (tail));
17318
17319 /* Now make it the first. */
17320 Fsetcdr (tail, list);
17321 return tail;
17322 }
17323 else
17324 prev = tail;
17325 tail = XCDR (tail);
17326 QUIT;
17327 }
17328
17329 /* Not found--return unchanged LIST. */
17330 return list;
17331 }
17332
17333 /* Contribute ELT to the mode line for window IT->w. How it
17334 translates into text depends on its data type.
17335
17336 IT describes the display environment in which we display, as usual.
17337
17338 DEPTH is the depth in recursion. It is used to prevent
17339 infinite recursion here.
17340
17341 FIELD_WIDTH is the number of characters the display of ELT should
17342 occupy in the mode line, and PRECISION is the maximum number of
17343 characters to display from ELT's representation. See
17344 display_string for details.
17345
17346 Returns the hpos of the end of the text generated by ELT.
17347
17348 PROPS is a property list to add to any string we encounter.
17349
17350 If RISKY is nonzero, remove (disregard) any properties in any string
17351 we encounter, and ignore :eval and :propertize.
17352
17353 The global variable `mode_line_target' determines whether the
17354 output is passed to `store_mode_line_noprop',
17355 `store_mode_line_string', or `display_string'. */
17356
17357 static int
17358 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17359 struct it *it;
17360 int depth;
17361 int field_width, precision;
17362 Lisp_Object elt, props;
17363 int risky;
17364 {
17365 int n = 0, field, prec;
17366 int literal = 0;
17367
17368 tail_recurse:
17369 if (depth > 100)
17370 elt = build_string ("*too-deep*");
17371
17372 depth++;
17373
17374 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17375 {
17376 case Lisp_String:
17377 {
17378 /* A string: output it and check for %-constructs within it. */
17379 unsigned char c;
17380 int offset = 0;
17381
17382 if (SCHARS (elt) > 0
17383 && (!NILP (props) || risky))
17384 {
17385 Lisp_Object oprops, aelt;
17386 oprops = Ftext_properties_at (make_number (0), elt);
17387
17388 /* If the starting string's properties are not what
17389 we want, translate the string. Also, if the string
17390 is risky, do that anyway. */
17391
17392 if (NILP (Fequal (props, oprops)) || risky)
17393 {
17394 /* If the starting string has properties,
17395 merge the specified ones onto the existing ones. */
17396 if (! NILP (oprops) && !risky)
17397 {
17398 Lisp_Object tem;
17399
17400 oprops = Fcopy_sequence (oprops);
17401 tem = props;
17402 while (CONSP (tem))
17403 {
17404 oprops = Fplist_put (oprops, XCAR (tem),
17405 XCAR (XCDR (tem)));
17406 tem = XCDR (XCDR (tem));
17407 }
17408 props = oprops;
17409 }
17410
17411 aelt = Fassoc (elt, mode_line_proptrans_alist);
17412 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17413 {
17414 /* AELT is what we want. Move it to the front
17415 without consing. */
17416 elt = XCAR (aelt);
17417 mode_line_proptrans_alist
17418 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17419 }
17420 else
17421 {
17422 Lisp_Object tem;
17423
17424 /* If AELT has the wrong props, it is useless.
17425 so get rid of it. */
17426 if (! NILP (aelt))
17427 mode_line_proptrans_alist
17428 = Fdelq (aelt, mode_line_proptrans_alist);
17429
17430 elt = Fcopy_sequence (elt);
17431 Fset_text_properties (make_number (0), Flength (elt),
17432 props, elt);
17433 /* Add this item to mode_line_proptrans_alist. */
17434 mode_line_proptrans_alist
17435 = Fcons (Fcons (elt, props),
17436 mode_line_proptrans_alist);
17437 /* Truncate mode_line_proptrans_alist
17438 to at most 50 elements. */
17439 tem = Fnthcdr (make_number (50),
17440 mode_line_proptrans_alist);
17441 if (! NILP (tem))
17442 XSETCDR (tem, Qnil);
17443 }
17444 }
17445 }
17446
17447 offset = 0;
17448
17449 if (literal)
17450 {
17451 prec = precision - n;
17452 switch (mode_line_target)
17453 {
17454 case MODE_LINE_NOPROP:
17455 case MODE_LINE_TITLE:
17456 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17457 break;
17458 case MODE_LINE_STRING:
17459 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17460 break;
17461 case MODE_LINE_DISPLAY:
17462 n += display_string (NULL, elt, Qnil, 0, 0, it,
17463 0, prec, 0, STRING_MULTIBYTE (elt));
17464 break;
17465 }
17466
17467 break;
17468 }
17469
17470 /* Handle the non-literal case. */
17471
17472 while ((precision <= 0 || n < precision)
17473 && SREF (elt, offset) != 0
17474 && (mode_line_target != MODE_LINE_DISPLAY
17475 || it->current_x < it->last_visible_x))
17476 {
17477 int last_offset = offset;
17478
17479 /* Advance to end of string or next format specifier. */
17480 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17481 ;
17482
17483 if (offset - 1 != last_offset)
17484 {
17485 int nchars, nbytes;
17486
17487 /* Output to end of string or up to '%'. Field width
17488 is length of string. Don't output more than
17489 PRECISION allows us. */
17490 offset--;
17491
17492 prec = c_string_width (SDATA (elt) + last_offset,
17493 offset - last_offset, precision - n,
17494 &nchars, &nbytes);
17495
17496 switch (mode_line_target)
17497 {
17498 case MODE_LINE_NOPROP:
17499 case MODE_LINE_TITLE:
17500 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17501 break;
17502 case MODE_LINE_STRING:
17503 {
17504 int bytepos = last_offset;
17505 int charpos = string_byte_to_char (elt, bytepos);
17506 int endpos = (precision <= 0
17507 ? string_byte_to_char (elt, offset)
17508 : charpos + nchars);
17509
17510 n += store_mode_line_string (NULL,
17511 Fsubstring (elt, make_number (charpos),
17512 make_number (endpos)),
17513 0, 0, 0, Qnil);
17514 }
17515 break;
17516 case MODE_LINE_DISPLAY:
17517 {
17518 int bytepos = last_offset;
17519 int charpos = string_byte_to_char (elt, bytepos);
17520
17521 if (precision <= 0)
17522 nchars = string_byte_to_char (elt, offset) - charpos;
17523 n += display_string (NULL, elt, Qnil, 0, charpos,
17524 it, 0, nchars, 0,
17525 STRING_MULTIBYTE (elt));
17526 }
17527 break;
17528 }
17529 }
17530 else /* c == '%' */
17531 {
17532 int percent_position = offset;
17533
17534 /* Get the specified minimum width. Zero means
17535 don't pad. */
17536 field = 0;
17537 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17538 field = field * 10 + c - '0';
17539
17540 /* Don't pad beyond the total padding allowed. */
17541 if (field_width - n > 0 && field > field_width - n)
17542 field = field_width - n;
17543
17544 /* Note that either PRECISION <= 0 or N < PRECISION. */
17545 prec = precision - n;
17546
17547 if (c == 'M')
17548 n += display_mode_element (it, depth, field, prec,
17549 Vglobal_mode_string, props,
17550 risky);
17551 else if (c != 0)
17552 {
17553 int multibyte;
17554 int bytepos, charpos;
17555 unsigned char *spec;
17556
17557 bytepos = percent_position;
17558 charpos = (STRING_MULTIBYTE (elt)
17559 ? string_byte_to_char (elt, bytepos)
17560 : bytepos);
17561 spec
17562 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17563
17564 switch (mode_line_target)
17565 {
17566 case MODE_LINE_NOPROP:
17567 case MODE_LINE_TITLE:
17568 n += store_mode_line_noprop (spec, field, prec);
17569 break;
17570 case MODE_LINE_STRING:
17571 {
17572 int len = strlen (spec);
17573 Lisp_Object tem = make_string (spec, len);
17574 props = Ftext_properties_at (make_number (charpos), elt);
17575 /* Should only keep face property in props */
17576 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17577 }
17578 break;
17579 case MODE_LINE_DISPLAY:
17580 {
17581 int nglyphs_before, nwritten;
17582
17583 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17584 nwritten = display_string (spec, Qnil, elt,
17585 charpos, 0, it,
17586 field, prec, 0,
17587 multibyte);
17588
17589 /* Assign to the glyphs written above the
17590 string where the `%x' came from, position
17591 of the `%'. */
17592 if (nwritten > 0)
17593 {
17594 struct glyph *glyph
17595 = (it->glyph_row->glyphs[TEXT_AREA]
17596 + nglyphs_before);
17597 int i;
17598
17599 for (i = 0; i < nwritten; ++i)
17600 {
17601 glyph[i].object = elt;
17602 glyph[i].charpos = charpos;
17603 }
17604
17605 n += nwritten;
17606 }
17607 }
17608 break;
17609 }
17610 }
17611 else /* c == 0 */
17612 break;
17613 }
17614 }
17615 }
17616 break;
17617
17618 case Lisp_Symbol:
17619 /* A symbol: process the value of the symbol recursively
17620 as if it appeared here directly. Avoid error if symbol void.
17621 Special case: if value of symbol is a string, output the string
17622 literally. */
17623 {
17624 register Lisp_Object tem;
17625
17626 /* If the variable is not marked as risky to set
17627 then its contents are risky to use. */
17628 if (NILP (Fget (elt, Qrisky_local_variable)))
17629 risky = 1;
17630
17631 tem = Fboundp (elt);
17632 if (!NILP (tem))
17633 {
17634 tem = Fsymbol_value (elt);
17635 /* If value is a string, output that string literally:
17636 don't check for % within it. */
17637 if (STRINGP (tem))
17638 literal = 1;
17639
17640 if (!EQ (tem, elt))
17641 {
17642 /* Give up right away for nil or t. */
17643 elt = tem;
17644 goto tail_recurse;
17645 }
17646 }
17647 }
17648 break;
17649
17650 case Lisp_Cons:
17651 {
17652 register Lisp_Object car, tem;
17653
17654 /* A cons cell: five distinct cases.
17655 If first element is :eval or :propertize, do something special.
17656 If first element is a string or a cons, process all the elements
17657 and effectively concatenate them.
17658 If first element is a negative number, truncate displaying cdr to
17659 at most that many characters. If positive, pad (with spaces)
17660 to at least that many characters.
17661 If first element is a symbol, process the cadr or caddr recursively
17662 according to whether the symbol's value is non-nil or nil. */
17663 car = XCAR (elt);
17664 if (EQ (car, QCeval))
17665 {
17666 /* An element of the form (:eval FORM) means evaluate FORM
17667 and use the result as mode line elements. */
17668
17669 if (risky)
17670 break;
17671
17672 if (CONSP (XCDR (elt)))
17673 {
17674 Lisp_Object spec;
17675 spec = safe_eval (XCAR (XCDR (elt)));
17676 n += display_mode_element (it, depth, field_width - n,
17677 precision - n, spec, props,
17678 risky);
17679 }
17680 }
17681 else if (EQ (car, QCpropertize))
17682 {
17683 /* An element of the form (:propertize ELT PROPS...)
17684 means display ELT but applying properties PROPS. */
17685
17686 if (risky)
17687 break;
17688
17689 if (CONSP (XCDR (elt)))
17690 n += display_mode_element (it, depth, field_width - n,
17691 precision - n, XCAR (XCDR (elt)),
17692 XCDR (XCDR (elt)), risky);
17693 }
17694 else if (SYMBOLP (car))
17695 {
17696 tem = Fboundp (car);
17697 elt = XCDR (elt);
17698 if (!CONSP (elt))
17699 goto invalid;
17700 /* elt is now the cdr, and we know it is a cons cell.
17701 Use its car if CAR has a non-nil value. */
17702 if (!NILP (tem))
17703 {
17704 tem = Fsymbol_value (car);
17705 if (!NILP (tem))
17706 {
17707 elt = XCAR (elt);
17708 goto tail_recurse;
17709 }
17710 }
17711 /* Symbol's value is nil (or symbol is unbound)
17712 Get the cddr of the original list
17713 and if possible find the caddr and use that. */
17714 elt = XCDR (elt);
17715 if (NILP (elt))
17716 break;
17717 else if (!CONSP (elt))
17718 goto invalid;
17719 elt = XCAR (elt);
17720 goto tail_recurse;
17721 }
17722 else if (INTEGERP (car))
17723 {
17724 register int lim = XINT (car);
17725 elt = XCDR (elt);
17726 if (lim < 0)
17727 {
17728 /* Negative int means reduce maximum width. */
17729 if (precision <= 0)
17730 precision = -lim;
17731 else
17732 precision = min (precision, -lim);
17733 }
17734 else if (lim > 0)
17735 {
17736 /* Padding specified. Don't let it be more than
17737 current maximum. */
17738 if (precision > 0)
17739 lim = min (precision, lim);
17740
17741 /* If that's more padding than already wanted, queue it.
17742 But don't reduce padding already specified even if
17743 that is beyond the current truncation point. */
17744 field_width = max (lim, field_width);
17745 }
17746 goto tail_recurse;
17747 }
17748 else if (STRINGP (car) || CONSP (car))
17749 {
17750 register int limit = 50;
17751 /* Limit is to protect against circular lists. */
17752 while (CONSP (elt)
17753 && --limit > 0
17754 && (precision <= 0 || n < precision))
17755 {
17756 n += display_mode_element (it, depth,
17757 /* Do padding only after the last
17758 element in the list. */
17759 (! CONSP (XCDR (elt))
17760 ? field_width - n
17761 : 0),
17762 precision - n, XCAR (elt),
17763 props, risky);
17764 elt = XCDR (elt);
17765 }
17766 }
17767 }
17768 break;
17769
17770 default:
17771 invalid:
17772 elt = build_string ("*invalid*");
17773 goto tail_recurse;
17774 }
17775
17776 /* Pad to FIELD_WIDTH. */
17777 if (field_width > 0 && n < field_width)
17778 {
17779 switch (mode_line_target)
17780 {
17781 case MODE_LINE_NOPROP:
17782 case MODE_LINE_TITLE:
17783 n += store_mode_line_noprop ("", field_width - n, 0);
17784 break;
17785 case MODE_LINE_STRING:
17786 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17787 break;
17788 case MODE_LINE_DISPLAY:
17789 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17790 0, 0, 0);
17791 break;
17792 }
17793 }
17794
17795 return n;
17796 }
17797
17798 /* Store a mode-line string element in mode_line_string_list.
17799
17800 If STRING is non-null, display that C string. Otherwise, the Lisp
17801 string LISP_STRING is displayed.
17802
17803 FIELD_WIDTH is the minimum number of output glyphs to produce.
17804 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17805 with spaces. FIELD_WIDTH <= 0 means don't pad.
17806
17807 PRECISION is the maximum number of characters to output from
17808 STRING. PRECISION <= 0 means don't truncate the string.
17809
17810 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17811 properties to the string.
17812
17813 PROPS are the properties to add to the string.
17814 The mode_line_string_face face property is always added to the string.
17815 */
17816
17817 static int
17818 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17819 char *string;
17820 Lisp_Object lisp_string;
17821 int copy_string;
17822 int field_width;
17823 int precision;
17824 Lisp_Object props;
17825 {
17826 int len;
17827 int n = 0;
17828
17829 if (string != NULL)
17830 {
17831 len = strlen (string);
17832 if (precision > 0 && len > precision)
17833 len = precision;
17834 lisp_string = make_string (string, len);
17835 if (NILP (props))
17836 props = mode_line_string_face_prop;
17837 else if (!NILP (mode_line_string_face))
17838 {
17839 Lisp_Object face = Fplist_get (props, Qface);
17840 props = Fcopy_sequence (props);
17841 if (NILP (face))
17842 face = mode_line_string_face;
17843 else
17844 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17845 props = Fplist_put (props, Qface, face);
17846 }
17847 Fadd_text_properties (make_number (0), make_number (len),
17848 props, lisp_string);
17849 }
17850 else
17851 {
17852 len = XFASTINT (Flength (lisp_string));
17853 if (precision > 0 && len > precision)
17854 {
17855 len = precision;
17856 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17857 precision = -1;
17858 }
17859 if (!NILP (mode_line_string_face))
17860 {
17861 Lisp_Object face;
17862 if (NILP (props))
17863 props = Ftext_properties_at (make_number (0), lisp_string);
17864 face = Fplist_get (props, Qface);
17865 if (NILP (face))
17866 face = mode_line_string_face;
17867 else
17868 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17869 props = Fcons (Qface, Fcons (face, Qnil));
17870 if (copy_string)
17871 lisp_string = Fcopy_sequence (lisp_string);
17872 }
17873 if (!NILP (props))
17874 Fadd_text_properties (make_number (0), make_number (len),
17875 props, lisp_string);
17876 }
17877
17878 if (len > 0)
17879 {
17880 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17881 n += len;
17882 }
17883
17884 if (field_width > len)
17885 {
17886 field_width -= len;
17887 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17888 if (!NILP (props))
17889 Fadd_text_properties (make_number (0), make_number (field_width),
17890 props, lisp_string);
17891 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17892 n += field_width;
17893 }
17894
17895 return n;
17896 }
17897
17898
17899 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17900 1, 4, 0,
17901 doc: /* Format a string out of a mode line format specification.
17902 First arg FORMAT specifies the mode line format (see `mode-line-format'
17903 for details) to use.
17904
17905 Optional second arg FACE specifies the face property to put
17906 on all characters for which no face is specified.
17907 The value t means whatever face the window's mode line currently uses
17908 \(either `mode-line' or `mode-line-inactive', depending).
17909 A value of nil means the default is no face property.
17910 If FACE is an integer, the value string has no text properties.
17911
17912 Optional third and fourth args WINDOW and BUFFER specify the window
17913 and buffer to use as the context for the formatting (defaults
17914 are the selected window and the window's buffer). */)
17915 (format, face, window, buffer)
17916 Lisp_Object format, face, window, buffer;
17917 {
17918 struct it it;
17919 int len;
17920 struct window *w;
17921 struct buffer *old_buffer = NULL;
17922 int face_id = -1;
17923 int no_props = INTEGERP (face);
17924 int count = SPECPDL_INDEX ();
17925 Lisp_Object str;
17926 int string_start = 0;
17927
17928 if (NILP (window))
17929 window = selected_window;
17930 CHECK_WINDOW (window);
17931 w = XWINDOW (window);
17932
17933 if (NILP (buffer))
17934 buffer = w->buffer;
17935 CHECK_BUFFER (buffer);
17936
17937 /* Make formatting the modeline a non-op when noninteractive, otherwise
17938 there will be problems later caused by a partially initialized frame. */
17939 if (NILP (format) || noninteractive)
17940 return empty_unibyte_string;
17941
17942 if (no_props)
17943 face = Qnil;
17944
17945 if (!NILP (face))
17946 {
17947 if (EQ (face, Qt))
17948 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17949 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17950 }
17951
17952 if (face_id < 0)
17953 face_id = DEFAULT_FACE_ID;
17954
17955 if (XBUFFER (buffer) != current_buffer)
17956 old_buffer = current_buffer;
17957
17958 /* Save things including mode_line_proptrans_alist,
17959 and set that to nil so that we don't alter the outer value. */
17960 record_unwind_protect (unwind_format_mode_line,
17961 format_mode_line_unwind_data
17962 (old_buffer, selected_window, 1));
17963 mode_line_proptrans_alist = Qnil;
17964
17965 Fselect_window (window, Qt);
17966 if (old_buffer)
17967 set_buffer_internal_1 (XBUFFER (buffer));
17968
17969 init_iterator (&it, w, -1, -1, NULL, face_id);
17970
17971 if (no_props)
17972 {
17973 mode_line_target = MODE_LINE_NOPROP;
17974 mode_line_string_face_prop = Qnil;
17975 mode_line_string_list = Qnil;
17976 string_start = MODE_LINE_NOPROP_LEN (0);
17977 }
17978 else
17979 {
17980 mode_line_target = MODE_LINE_STRING;
17981 mode_line_string_list = Qnil;
17982 mode_line_string_face = face;
17983 mode_line_string_face_prop
17984 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17985 }
17986
17987 push_kboard (FRAME_KBOARD (it.f));
17988 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17989 pop_kboard ();
17990
17991 if (no_props)
17992 {
17993 len = MODE_LINE_NOPROP_LEN (string_start);
17994 str = make_string (mode_line_noprop_buf + string_start, len);
17995 }
17996 else
17997 {
17998 mode_line_string_list = Fnreverse (mode_line_string_list);
17999 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18000 empty_unibyte_string);
18001 }
18002
18003 unbind_to (count, Qnil);
18004 return str;
18005 }
18006
18007 /* Write a null-terminated, right justified decimal representation of
18008 the positive integer D to BUF using a minimal field width WIDTH. */
18009
18010 static void
18011 pint2str (buf, width, d)
18012 register char *buf;
18013 register int width;
18014 register int d;
18015 {
18016 register char *p = buf;
18017
18018 if (d <= 0)
18019 *p++ = '0';
18020 else
18021 {
18022 while (d > 0)
18023 {
18024 *p++ = d % 10 + '0';
18025 d /= 10;
18026 }
18027 }
18028
18029 for (width -= (int) (p - buf); width > 0; --width)
18030 *p++ = ' ';
18031 *p-- = '\0';
18032 while (p > buf)
18033 {
18034 d = *buf;
18035 *buf++ = *p;
18036 *p-- = d;
18037 }
18038 }
18039
18040 /* Write a null-terminated, right justified decimal and "human
18041 readable" representation of the nonnegative integer D to BUF using
18042 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18043
18044 static const char power_letter[] =
18045 {
18046 0, /* not used */
18047 'k', /* kilo */
18048 'M', /* mega */
18049 'G', /* giga */
18050 'T', /* tera */
18051 'P', /* peta */
18052 'E', /* exa */
18053 'Z', /* zetta */
18054 'Y' /* yotta */
18055 };
18056
18057 static void
18058 pint2hrstr (buf, width, d)
18059 char *buf;
18060 int width;
18061 int d;
18062 {
18063 /* We aim to represent the nonnegative integer D as
18064 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18065 int quotient = d;
18066 int remainder = 0;
18067 /* -1 means: do not use TENTHS. */
18068 int tenths = -1;
18069 int exponent = 0;
18070
18071 /* Length of QUOTIENT.TENTHS as a string. */
18072 int length;
18073
18074 char * psuffix;
18075 char * p;
18076
18077 if (1000 <= quotient)
18078 {
18079 /* Scale to the appropriate EXPONENT. */
18080 do
18081 {
18082 remainder = quotient % 1000;
18083 quotient /= 1000;
18084 exponent++;
18085 }
18086 while (1000 <= quotient);
18087
18088 /* Round to nearest and decide whether to use TENTHS or not. */
18089 if (quotient <= 9)
18090 {
18091 tenths = remainder / 100;
18092 if (50 <= remainder % 100)
18093 {
18094 if (tenths < 9)
18095 tenths++;
18096 else
18097 {
18098 quotient++;
18099 if (quotient == 10)
18100 tenths = -1;
18101 else
18102 tenths = 0;
18103 }
18104 }
18105 }
18106 else
18107 if (500 <= remainder)
18108 {
18109 if (quotient < 999)
18110 quotient++;
18111 else
18112 {
18113 quotient = 1;
18114 exponent++;
18115 tenths = 0;
18116 }
18117 }
18118 }
18119
18120 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18121 if (tenths == -1 && quotient <= 99)
18122 if (quotient <= 9)
18123 length = 1;
18124 else
18125 length = 2;
18126 else
18127 length = 3;
18128 p = psuffix = buf + max (width, length);
18129
18130 /* Print EXPONENT. */
18131 if (exponent)
18132 *psuffix++ = power_letter[exponent];
18133 *psuffix = '\0';
18134
18135 /* Print TENTHS. */
18136 if (tenths >= 0)
18137 {
18138 *--p = '0' + tenths;
18139 *--p = '.';
18140 }
18141
18142 /* Print QUOTIENT. */
18143 do
18144 {
18145 int digit = quotient % 10;
18146 *--p = '0' + digit;
18147 }
18148 while ((quotient /= 10) != 0);
18149
18150 /* Print leading spaces. */
18151 while (buf < p)
18152 *--p = ' ';
18153 }
18154
18155 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18156 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18157 type of CODING_SYSTEM. Return updated pointer into BUF. */
18158
18159 static unsigned char invalid_eol_type[] = "(*invalid*)";
18160
18161 static char *
18162 decode_mode_spec_coding (coding_system, buf, eol_flag)
18163 Lisp_Object coding_system;
18164 register char *buf;
18165 int eol_flag;
18166 {
18167 Lisp_Object val;
18168 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18169 const unsigned char *eol_str;
18170 int eol_str_len;
18171 /* The EOL conversion we are using. */
18172 Lisp_Object eoltype;
18173
18174 val = CODING_SYSTEM_SPEC (coding_system);
18175 eoltype = Qnil;
18176
18177 if (!VECTORP (val)) /* Not yet decided. */
18178 {
18179 if (multibyte)
18180 *buf++ = '-';
18181 if (eol_flag)
18182 eoltype = eol_mnemonic_undecided;
18183 /* Don't mention EOL conversion if it isn't decided. */
18184 }
18185 else
18186 {
18187 Lisp_Object attrs;
18188 Lisp_Object eolvalue;
18189
18190 attrs = AREF (val, 0);
18191 eolvalue = AREF (val, 2);
18192
18193 if (multibyte)
18194 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18195
18196 if (eol_flag)
18197 {
18198 /* The EOL conversion that is normal on this system. */
18199
18200 if (NILP (eolvalue)) /* Not yet decided. */
18201 eoltype = eol_mnemonic_undecided;
18202 else if (VECTORP (eolvalue)) /* Not yet decided. */
18203 eoltype = eol_mnemonic_undecided;
18204 else /* eolvalue is Qunix, Qdos, or Qmac. */
18205 eoltype = (EQ (eolvalue, Qunix)
18206 ? eol_mnemonic_unix
18207 : (EQ (eolvalue, Qdos) == 1
18208 ? eol_mnemonic_dos : eol_mnemonic_mac));
18209 }
18210 }
18211
18212 if (eol_flag)
18213 {
18214 /* Mention the EOL conversion if it is not the usual one. */
18215 if (STRINGP (eoltype))
18216 {
18217 eol_str = SDATA (eoltype);
18218 eol_str_len = SBYTES (eoltype);
18219 }
18220 else if (CHARACTERP (eoltype))
18221 {
18222 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18223 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18224 eol_str = tmp;
18225 }
18226 else
18227 {
18228 eol_str = invalid_eol_type;
18229 eol_str_len = sizeof (invalid_eol_type) - 1;
18230 }
18231 bcopy (eol_str, buf, eol_str_len);
18232 buf += eol_str_len;
18233 }
18234
18235 return buf;
18236 }
18237
18238 /* Return a string for the output of a mode line %-spec for window W,
18239 generated by character C. PRECISION >= 0 means don't return a
18240 string longer than that value. FIELD_WIDTH > 0 means pad the
18241 string returned with spaces to that value. Return 1 in *MULTIBYTE
18242 if the result is multibyte text.
18243
18244 Note we operate on the current buffer for most purposes,
18245 the exception being w->base_line_pos. */
18246
18247 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18248
18249 static char *
18250 decode_mode_spec (w, c, field_width, precision, multibyte)
18251 struct window *w;
18252 register int c;
18253 int field_width, precision;
18254 int *multibyte;
18255 {
18256 Lisp_Object obj;
18257 struct frame *f = XFRAME (WINDOW_FRAME (w));
18258 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18259 struct buffer *b = current_buffer;
18260
18261 obj = Qnil;
18262 *multibyte = 0;
18263
18264 switch (c)
18265 {
18266 case '*':
18267 if (!NILP (b->read_only))
18268 return "%";
18269 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18270 return "*";
18271 return "-";
18272
18273 case '+':
18274 /* This differs from %* only for a modified read-only buffer. */
18275 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18276 return "*";
18277 if (!NILP (b->read_only))
18278 return "%";
18279 return "-";
18280
18281 case '&':
18282 /* This differs from %* in ignoring read-only-ness. */
18283 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18284 return "*";
18285 return "-";
18286
18287 case '%':
18288 return "%";
18289
18290 case '[':
18291 {
18292 int i;
18293 char *p;
18294
18295 if (command_loop_level > 5)
18296 return "[[[... ";
18297 p = decode_mode_spec_buf;
18298 for (i = 0; i < command_loop_level; i++)
18299 *p++ = '[';
18300 *p = 0;
18301 return decode_mode_spec_buf;
18302 }
18303
18304 case ']':
18305 {
18306 int i;
18307 char *p;
18308
18309 if (command_loop_level > 5)
18310 return " ...]]]";
18311 p = decode_mode_spec_buf;
18312 for (i = 0; i < command_loop_level; i++)
18313 *p++ = ']';
18314 *p = 0;
18315 return decode_mode_spec_buf;
18316 }
18317
18318 case '-':
18319 {
18320 register int i;
18321
18322 /* Let lots_of_dashes be a string of infinite length. */
18323 if (mode_line_target == MODE_LINE_NOPROP ||
18324 mode_line_target == MODE_LINE_STRING)
18325 return "--";
18326 if (field_width <= 0
18327 || field_width > sizeof (lots_of_dashes))
18328 {
18329 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18330 decode_mode_spec_buf[i] = '-';
18331 decode_mode_spec_buf[i] = '\0';
18332 return decode_mode_spec_buf;
18333 }
18334 else
18335 return lots_of_dashes;
18336 }
18337
18338 case 'b':
18339 obj = b->name;
18340 break;
18341
18342 case 'c':
18343 /* %c and %l are ignored in `frame-title-format'.
18344 (In redisplay_internal, the frame title is drawn _before_ the
18345 windows are updated, so the stuff which depends on actual
18346 window contents (such as %l) may fail to render properly, or
18347 even crash emacs.) */
18348 if (mode_line_target == MODE_LINE_TITLE)
18349 return "";
18350 else
18351 {
18352 int col = (int) current_column (); /* iftc */
18353 w->column_number_displayed = make_number (col);
18354 pint2str (decode_mode_spec_buf, field_width, col);
18355 return decode_mode_spec_buf;
18356 }
18357
18358 case 'e':
18359 #ifndef SYSTEM_MALLOC
18360 {
18361 if (NILP (Vmemory_full))
18362 return "";
18363 else
18364 return "!MEM FULL! ";
18365 }
18366 #else
18367 return "";
18368 #endif
18369
18370 case 'F':
18371 /* %F displays the frame name. */
18372 if (!NILP (f->title))
18373 return (char *) SDATA (f->title);
18374 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18375 return (char *) SDATA (f->name);
18376 return "Emacs";
18377
18378 case 'f':
18379 obj = b->filename;
18380 break;
18381
18382 case 'i':
18383 {
18384 int size = ZV - BEGV;
18385 pint2str (decode_mode_spec_buf, field_width, size);
18386 return decode_mode_spec_buf;
18387 }
18388
18389 case 'I':
18390 {
18391 int size = ZV - BEGV;
18392 pint2hrstr (decode_mode_spec_buf, field_width, size);
18393 return decode_mode_spec_buf;
18394 }
18395
18396 case 'l':
18397 {
18398 int startpos, startpos_byte, line, linepos, linepos_byte;
18399 int topline, nlines, junk, height;
18400
18401 /* %c and %l are ignored in `frame-title-format'. */
18402 if (mode_line_target == MODE_LINE_TITLE)
18403 return "";
18404
18405 startpos = XMARKER (w->start)->charpos;
18406 startpos_byte = marker_byte_position (w->start);
18407 height = WINDOW_TOTAL_LINES (w);
18408
18409 /* If we decided that this buffer isn't suitable for line numbers,
18410 don't forget that too fast. */
18411 if (EQ (w->base_line_pos, w->buffer))
18412 goto no_value;
18413 /* But do forget it, if the window shows a different buffer now. */
18414 else if (BUFFERP (w->base_line_pos))
18415 w->base_line_pos = Qnil;
18416
18417 /* If the buffer is very big, don't waste time. */
18418 if (INTEGERP (Vline_number_display_limit)
18419 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18420 {
18421 w->base_line_pos = Qnil;
18422 w->base_line_number = Qnil;
18423 goto no_value;
18424 }
18425
18426 if (INTEGERP (w->base_line_number)
18427 && INTEGERP (w->base_line_pos)
18428 && XFASTINT (w->base_line_pos) <= startpos)
18429 {
18430 line = XFASTINT (w->base_line_number);
18431 linepos = XFASTINT (w->base_line_pos);
18432 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18433 }
18434 else
18435 {
18436 line = 1;
18437 linepos = BUF_BEGV (b);
18438 linepos_byte = BUF_BEGV_BYTE (b);
18439 }
18440
18441 /* Count lines from base line to window start position. */
18442 nlines = display_count_lines (linepos, linepos_byte,
18443 startpos_byte,
18444 startpos, &junk);
18445
18446 topline = nlines + line;
18447
18448 /* Determine a new base line, if the old one is too close
18449 or too far away, or if we did not have one.
18450 "Too close" means it's plausible a scroll-down would
18451 go back past it. */
18452 if (startpos == BUF_BEGV (b))
18453 {
18454 w->base_line_number = make_number (topline);
18455 w->base_line_pos = make_number (BUF_BEGV (b));
18456 }
18457 else if (nlines < height + 25 || nlines > height * 3 + 50
18458 || linepos == BUF_BEGV (b))
18459 {
18460 int limit = BUF_BEGV (b);
18461 int limit_byte = BUF_BEGV_BYTE (b);
18462 int position;
18463 int distance = (height * 2 + 30) * line_number_display_limit_width;
18464
18465 if (startpos - distance > limit)
18466 {
18467 limit = startpos - distance;
18468 limit_byte = CHAR_TO_BYTE (limit);
18469 }
18470
18471 nlines = display_count_lines (startpos, startpos_byte,
18472 limit_byte,
18473 - (height * 2 + 30),
18474 &position);
18475 /* If we couldn't find the lines we wanted within
18476 line_number_display_limit_width chars per line,
18477 give up on line numbers for this window. */
18478 if (position == limit_byte && limit == startpos - distance)
18479 {
18480 w->base_line_pos = w->buffer;
18481 w->base_line_number = Qnil;
18482 goto no_value;
18483 }
18484
18485 w->base_line_number = make_number (topline - nlines);
18486 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18487 }
18488
18489 /* Now count lines from the start pos to point. */
18490 nlines = display_count_lines (startpos, startpos_byte,
18491 PT_BYTE, PT, &junk);
18492
18493 /* Record that we did display the line number. */
18494 line_number_displayed = 1;
18495
18496 /* Make the string to show. */
18497 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18498 return decode_mode_spec_buf;
18499 no_value:
18500 {
18501 char* p = decode_mode_spec_buf;
18502 int pad = field_width - 2;
18503 while (pad-- > 0)
18504 *p++ = ' ';
18505 *p++ = '?';
18506 *p++ = '?';
18507 *p = '\0';
18508 return decode_mode_spec_buf;
18509 }
18510 }
18511 break;
18512
18513 case 'm':
18514 obj = b->mode_name;
18515 break;
18516
18517 case 'n':
18518 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18519 return " Narrow";
18520 break;
18521
18522 case 'p':
18523 {
18524 int pos = marker_position (w->start);
18525 int total = BUF_ZV (b) - BUF_BEGV (b);
18526
18527 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18528 {
18529 if (pos <= BUF_BEGV (b))
18530 return "All";
18531 else
18532 return "Bottom";
18533 }
18534 else if (pos <= BUF_BEGV (b))
18535 return "Top";
18536 else
18537 {
18538 if (total > 1000000)
18539 /* Do it differently for a large value, to avoid overflow. */
18540 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18541 else
18542 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18543 /* We can't normally display a 3-digit number,
18544 so get us a 2-digit number that is close. */
18545 if (total == 100)
18546 total = 99;
18547 sprintf (decode_mode_spec_buf, "%2d%%", total);
18548 return decode_mode_spec_buf;
18549 }
18550 }
18551
18552 /* Display percentage of size above the bottom of the screen. */
18553 case 'P':
18554 {
18555 int toppos = marker_position (w->start);
18556 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18557 int total = BUF_ZV (b) - BUF_BEGV (b);
18558
18559 if (botpos >= BUF_ZV (b))
18560 {
18561 if (toppos <= BUF_BEGV (b))
18562 return "All";
18563 else
18564 return "Bottom";
18565 }
18566 else
18567 {
18568 if (total > 1000000)
18569 /* Do it differently for a large value, to avoid overflow. */
18570 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18571 else
18572 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18573 /* We can't normally display a 3-digit number,
18574 so get us a 2-digit number that is close. */
18575 if (total == 100)
18576 total = 99;
18577 if (toppos <= BUF_BEGV (b))
18578 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18579 else
18580 sprintf (decode_mode_spec_buf, "%2d%%", total);
18581 return decode_mode_spec_buf;
18582 }
18583 }
18584
18585 case 's':
18586 /* status of process */
18587 obj = Fget_buffer_process (Fcurrent_buffer ());
18588 if (NILP (obj))
18589 return "no process";
18590 #ifdef subprocesses
18591 obj = Fsymbol_name (Fprocess_status (obj));
18592 #endif
18593 break;
18594
18595 case '@':
18596 {
18597 Lisp_Object val;
18598 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18599 if (NILP (val))
18600 return "-";
18601 else
18602 return "@";
18603 }
18604
18605 case 't': /* indicate TEXT or BINARY */
18606 #ifdef MODE_LINE_BINARY_TEXT
18607 return MODE_LINE_BINARY_TEXT (b);
18608 #else
18609 return "T";
18610 #endif
18611
18612 case 'z':
18613 /* coding-system (not including end-of-line format) */
18614 case 'Z':
18615 /* coding-system (including end-of-line type) */
18616 {
18617 int eol_flag = (c == 'Z');
18618 char *p = decode_mode_spec_buf;
18619
18620 if (! FRAME_WINDOW_P (f))
18621 {
18622 /* No need to mention EOL here--the terminal never needs
18623 to do EOL conversion. */
18624 p = decode_mode_spec_coding (CODING_ID_NAME
18625 (FRAME_KEYBOARD_CODING (f)->id),
18626 p, 0);
18627 p = decode_mode_spec_coding (CODING_ID_NAME
18628 (FRAME_TERMINAL_CODING (f)->id),
18629 p, 0);
18630 }
18631 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18632 p, eol_flag);
18633
18634 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18635 #ifdef subprocesses
18636 obj = Fget_buffer_process (Fcurrent_buffer ());
18637 if (PROCESSP (obj))
18638 {
18639 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18640 p, eol_flag);
18641 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18642 p, eol_flag);
18643 }
18644 #endif /* subprocesses */
18645 #endif /* 0 */
18646 *p = 0;
18647 return decode_mode_spec_buf;
18648 }
18649 }
18650
18651 if (STRINGP (obj))
18652 {
18653 *multibyte = STRING_MULTIBYTE (obj);
18654 return (char *) SDATA (obj);
18655 }
18656 else
18657 return "";
18658 }
18659
18660
18661 /* Count up to COUNT lines starting from START / START_BYTE.
18662 But don't go beyond LIMIT_BYTE.
18663 Return the number of lines thus found (always nonnegative).
18664
18665 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18666
18667 static int
18668 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18669 int start, start_byte, limit_byte, count;
18670 int *byte_pos_ptr;
18671 {
18672 register unsigned char *cursor;
18673 unsigned char *base;
18674
18675 register int ceiling;
18676 register unsigned char *ceiling_addr;
18677 int orig_count = count;
18678
18679 /* If we are not in selective display mode,
18680 check only for newlines. */
18681 int selective_display = (!NILP (current_buffer->selective_display)
18682 && !INTEGERP (current_buffer->selective_display));
18683
18684 if (count > 0)
18685 {
18686 while (start_byte < limit_byte)
18687 {
18688 ceiling = BUFFER_CEILING_OF (start_byte);
18689 ceiling = min (limit_byte - 1, ceiling);
18690 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18691 base = (cursor = BYTE_POS_ADDR (start_byte));
18692 while (1)
18693 {
18694 if (selective_display)
18695 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18696 ;
18697 else
18698 while (*cursor != '\n' && ++cursor != ceiling_addr)
18699 ;
18700
18701 if (cursor != ceiling_addr)
18702 {
18703 if (--count == 0)
18704 {
18705 start_byte += cursor - base + 1;
18706 *byte_pos_ptr = start_byte;
18707 return orig_count;
18708 }
18709 else
18710 if (++cursor == ceiling_addr)
18711 break;
18712 }
18713 else
18714 break;
18715 }
18716 start_byte += cursor - base;
18717 }
18718 }
18719 else
18720 {
18721 while (start_byte > limit_byte)
18722 {
18723 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18724 ceiling = max (limit_byte, ceiling);
18725 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18726 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18727 while (1)
18728 {
18729 if (selective_display)
18730 while (--cursor != ceiling_addr
18731 && *cursor != '\n' && *cursor != 015)
18732 ;
18733 else
18734 while (--cursor != ceiling_addr && *cursor != '\n')
18735 ;
18736
18737 if (cursor != ceiling_addr)
18738 {
18739 if (++count == 0)
18740 {
18741 start_byte += cursor - base + 1;
18742 *byte_pos_ptr = start_byte;
18743 /* When scanning backwards, we should
18744 not count the newline posterior to which we stop. */
18745 return - orig_count - 1;
18746 }
18747 }
18748 else
18749 break;
18750 }
18751 /* Here we add 1 to compensate for the last decrement
18752 of CURSOR, which took it past the valid range. */
18753 start_byte += cursor - base + 1;
18754 }
18755 }
18756
18757 *byte_pos_ptr = limit_byte;
18758
18759 if (count < 0)
18760 return - orig_count + count;
18761 return orig_count - count;
18762
18763 }
18764
18765
18766 \f
18767 /***********************************************************************
18768 Displaying strings
18769 ***********************************************************************/
18770
18771 /* Display a NUL-terminated string, starting with index START.
18772
18773 If STRING is non-null, display that C string. Otherwise, the Lisp
18774 string LISP_STRING is displayed.
18775
18776 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18777 FACE_STRING. Display STRING or LISP_STRING with the face at
18778 FACE_STRING_POS in FACE_STRING:
18779
18780 Display the string in the environment given by IT, but use the
18781 standard display table, temporarily.
18782
18783 FIELD_WIDTH is the minimum number of output glyphs to produce.
18784 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18785 with spaces. If STRING has more characters, more than FIELD_WIDTH
18786 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18787
18788 PRECISION is the maximum number of characters to output from
18789 STRING. PRECISION < 0 means don't truncate the string.
18790
18791 This is roughly equivalent to printf format specifiers:
18792
18793 FIELD_WIDTH PRECISION PRINTF
18794 ----------------------------------------
18795 -1 -1 %s
18796 -1 10 %.10s
18797 10 -1 %10s
18798 20 10 %20.10s
18799
18800 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18801 display them, and < 0 means obey the current buffer's value of
18802 enable_multibyte_characters.
18803
18804 Value is the number of columns displayed. */
18805
18806 static int
18807 display_string (string, lisp_string, face_string, face_string_pos,
18808 start, it, field_width, precision, max_x, multibyte)
18809 unsigned char *string;
18810 Lisp_Object lisp_string;
18811 Lisp_Object face_string;
18812 EMACS_INT face_string_pos;
18813 EMACS_INT start;
18814 struct it *it;
18815 int field_width, precision, max_x;
18816 int multibyte;
18817 {
18818 int hpos_at_start = it->hpos;
18819 int saved_face_id = it->face_id;
18820 struct glyph_row *row = it->glyph_row;
18821
18822 /* Initialize the iterator IT for iteration over STRING beginning
18823 with index START. */
18824 reseat_to_string (it, string, lisp_string, start,
18825 precision, field_width, multibyte);
18826
18827 /* If displaying STRING, set up the face of the iterator
18828 from LISP_STRING, if that's given. */
18829 if (STRINGP (face_string))
18830 {
18831 EMACS_INT endptr;
18832 struct face *face;
18833
18834 it->face_id
18835 = face_at_string_position (it->w, face_string, face_string_pos,
18836 0, it->region_beg_charpos,
18837 it->region_end_charpos,
18838 &endptr, it->base_face_id, 0);
18839 face = FACE_FROM_ID (it->f, it->face_id);
18840 it->face_box_p = face->box != FACE_NO_BOX;
18841 }
18842
18843 /* Set max_x to the maximum allowed X position. Don't let it go
18844 beyond the right edge of the window. */
18845 if (max_x <= 0)
18846 max_x = it->last_visible_x;
18847 else
18848 max_x = min (max_x, it->last_visible_x);
18849
18850 /* Skip over display elements that are not visible. because IT->w is
18851 hscrolled. */
18852 if (it->current_x < it->first_visible_x)
18853 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18854 MOVE_TO_POS | MOVE_TO_X);
18855
18856 row->ascent = it->max_ascent;
18857 row->height = it->max_ascent + it->max_descent;
18858 row->phys_ascent = it->max_phys_ascent;
18859 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18860 row->extra_line_spacing = it->max_extra_line_spacing;
18861
18862 /* This condition is for the case that we are called with current_x
18863 past last_visible_x. */
18864 while (it->current_x < max_x)
18865 {
18866 int x_before, x, n_glyphs_before, i, nglyphs;
18867
18868 /* Get the next display element. */
18869 if (!get_next_display_element (it))
18870 break;
18871
18872 /* Produce glyphs. */
18873 x_before = it->current_x;
18874 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18875 PRODUCE_GLYPHS (it);
18876
18877 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18878 i = 0;
18879 x = x_before;
18880 while (i < nglyphs)
18881 {
18882 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18883
18884 if (it->line_wrap != TRUNCATE
18885 && x + glyph->pixel_width > max_x)
18886 {
18887 /* End of continued line or max_x reached. */
18888 if (CHAR_GLYPH_PADDING_P (*glyph))
18889 {
18890 /* A wide character is unbreakable. */
18891 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18892 it->current_x = x_before;
18893 }
18894 else
18895 {
18896 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18897 it->current_x = x;
18898 }
18899 break;
18900 }
18901 else if (x + glyph->pixel_width >= it->first_visible_x)
18902 {
18903 /* Glyph is at least partially visible. */
18904 ++it->hpos;
18905 if (x < it->first_visible_x)
18906 it->glyph_row->x = x - it->first_visible_x;
18907 }
18908 else
18909 {
18910 /* Glyph is off the left margin of the display area.
18911 Should not happen. */
18912 abort ();
18913 }
18914
18915 row->ascent = max (row->ascent, it->max_ascent);
18916 row->height = max (row->height, it->max_ascent + it->max_descent);
18917 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18918 row->phys_height = max (row->phys_height,
18919 it->max_phys_ascent + it->max_phys_descent);
18920 row->extra_line_spacing = max (row->extra_line_spacing,
18921 it->max_extra_line_spacing);
18922 x += glyph->pixel_width;
18923 ++i;
18924 }
18925
18926 /* Stop if max_x reached. */
18927 if (i < nglyphs)
18928 break;
18929
18930 /* Stop at line ends. */
18931 if (ITERATOR_AT_END_OF_LINE_P (it))
18932 {
18933 it->continuation_lines_width = 0;
18934 break;
18935 }
18936
18937 set_iterator_to_next (it, 1);
18938
18939 /* Stop if truncating at the right edge. */
18940 if (it->line_wrap == TRUNCATE
18941 && it->current_x >= it->last_visible_x)
18942 {
18943 /* Add truncation mark, but don't do it if the line is
18944 truncated at a padding space. */
18945 if (IT_CHARPOS (*it) < it->string_nchars)
18946 {
18947 if (!FRAME_WINDOW_P (it->f))
18948 {
18949 int i, n;
18950
18951 if (it->current_x > it->last_visible_x)
18952 {
18953 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18954 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18955 break;
18956 for (n = row->used[TEXT_AREA]; i < n; ++i)
18957 {
18958 row->used[TEXT_AREA] = i;
18959 produce_special_glyphs (it, IT_TRUNCATION);
18960 }
18961 }
18962 produce_special_glyphs (it, IT_TRUNCATION);
18963 }
18964 it->glyph_row->truncated_on_right_p = 1;
18965 }
18966 break;
18967 }
18968 }
18969
18970 /* Maybe insert a truncation at the left. */
18971 if (it->first_visible_x
18972 && IT_CHARPOS (*it) > 0)
18973 {
18974 if (!FRAME_WINDOW_P (it->f))
18975 insert_left_trunc_glyphs (it);
18976 it->glyph_row->truncated_on_left_p = 1;
18977 }
18978
18979 it->face_id = saved_face_id;
18980
18981 /* Value is number of columns displayed. */
18982 return it->hpos - hpos_at_start;
18983 }
18984
18985
18986 \f
18987 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18988 appears as an element of LIST or as the car of an element of LIST.
18989 If PROPVAL is a list, compare each element against LIST in that
18990 way, and return 1/2 if any element of PROPVAL is found in LIST.
18991 Otherwise return 0. This function cannot quit.
18992 The return value is 2 if the text is invisible but with an ellipsis
18993 and 1 if it's invisible and without an ellipsis. */
18994
18995 int
18996 invisible_p (propval, list)
18997 register Lisp_Object propval;
18998 Lisp_Object list;
18999 {
19000 register Lisp_Object tail, proptail;
19001
19002 for (tail = list; CONSP (tail); tail = XCDR (tail))
19003 {
19004 register Lisp_Object tem;
19005 tem = XCAR (tail);
19006 if (EQ (propval, tem))
19007 return 1;
19008 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19009 return NILP (XCDR (tem)) ? 1 : 2;
19010 }
19011
19012 if (CONSP (propval))
19013 {
19014 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19015 {
19016 Lisp_Object propelt;
19017 propelt = XCAR (proptail);
19018 for (tail = list; CONSP (tail); tail = XCDR (tail))
19019 {
19020 register Lisp_Object tem;
19021 tem = XCAR (tail);
19022 if (EQ (propelt, tem))
19023 return 1;
19024 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19025 return NILP (XCDR (tem)) ? 1 : 2;
19026 }
19027 }
19028 }
19029
19030 return 0;
19031 }
19032
19033 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19034 doc: /* Non-nil if the property makes the text invisible.
19035 POS-OR-PROP can be a marker or number, in which case it is taken to be
19036 a position in the current buffer and the value of the `invisible' property
19037 is checked; or it can be some other value, which is then presumed to be the
19038 value of the `invisible' property of the text of interest.
19039 The non-nil value returned can be t for truly invisible text or something
19040 else if the text is replaced by an ellipsis. */)
19041 (pos_or_prop)
19042 Lisp_Object pos_or_prop;
19043 {
19044 Lisp_Object prop
19045 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19046 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19047 : pos_or_prop);
19048 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19049 return (invis == 0 ? Qnil
19050 : invis == 1 ? Qt
19051 : make_number (invis));
19052 }
19053
19054 /* Calculate a width or height in pixels from a specification using
19055 the following elements:
19056
19057 SPEC ::=
19058 NUM - a (fractional) multiple of the default font width/height
19059 (NUM) - specifies exactly NUM pixels
19060 UNIT - a fixed number of pixels, see below.
19061 ELEMENT - size of a display element in pixels, see below.
19062 (NUM . SPEC) - equals NUM * SPEC
19063 (+ SPEC SPEC ...) - add pixel values
19064 (- SPEC SPEC ...) - subtract pixel values
19065 (- SPEC) - negate pixel value
19066
19067 NUM ::=
19068 INT or FLOAT - a number constant
19069 SYMBOL - use symbol's (buffer local) variable binding.
19070
19071 UNIT ::=
19072 in - pixels per inch *)
19073 mm - pixels per 1/1000 meter *)
19074 cm - pixels per 1/100 meter *)
19075 width - width of current font in pixels.
19076 height - height of current font in pixels.
19077
19078 *) using the ratio(s) defined in display-pixels-per-inch.
19079
19080 ELEMENT ::=
19081
19082 left-fringe - left fringe width in pixels
19083 right-fringe - right fringe width in pixels
19084
19085 left-margin - left margin width in pixels
19086 right-margin - right margin width in pixels
19087
19088 scroll-bar - scroll-bar area width in pixels
19089
19090 Examples:
19091
19092 Pixels corresponding to 5 inches:
19093 (5 . in)
19094
19095 Total width of non-text areas on left side of window (if scroll-bar is on left):
19096 '(space :width (+ left-fringe left-margin scroll-bar))
19097
19098 Align to first text column (in header line):
19099 '(space :align-to 0)
19100
19101 Align to middle of text area minus half the width of variable `my-image'
19102 containing a loaded image:
19103 '(space :align-to (0.5 . (- text my-image)))
19104
19105 Width of left margin minus width of 1 character in the default font:
19106 '(space :width (- left-margin 1))
19107
19108 Width of left margin minus width of 2 characters in the current font:
19109 '(space :width (- left-margin (2 . width)))
19110
19111 Center 1 character over left-margin (in header line):
19112 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19113
19114 Different ways to express width of left fringe plus left margin minus one pixel:
19115 '(space :width (- (+ left-fringe left-margin) (1)))
19116 '(space :width (+ left-fringe left-margin (- (1))))
19117 '(space :width (+ left-fringe left-margin (-1)))
19118
19119 */
19120
19121 #define NUMVAL(X) \
19122 ((INTEGERP (X) || FLOATP (X)) \
19123 ? XFLOATINT (X) \
19124 : - 1)
19125
19126 int
19127 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19128 double *res;
19129 struct it *it;
19130 Lisp_Object prop;
19131 struct font *font;
19132 int width_p, *align_to;
19133 {
19134 double pixels;
19135
19136 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19137 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19138
19139 if (NILP (prop))
19140 return OK_PIXELS (0);
19141
19142 xassert (FRAME_LIVE_P (it->f));
19143
19144 if (SYMBOLP (prop))
19145 {
19146 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19147 {
19148 char *unit = SDATA (SYMBOL_NAME (prop));
19149
19150 if (unit[0] == 'i' && unit[1] == 'n')
19151 pixels = 1.0;
19152 else if (unit[0] == 'm' && unit[1] == 'm')
19153 pixels = 25.4;
19154 else if (unit[0] == 'c' && unit[1] == 'm')
19155 pixels = 2.54;
19156 else
19157 pixels = 0;
19158 if (pixels > 0)
19159 {
19160 double ppi;
19161 #ifdef HAVE_WINDOW_SYSTEM
19162 if (FRAME_WINDOW_P (it->f)
19163 && (ppi = (width_p
19164 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19165 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19166 ppi > 0))
19167 return OK_PIXELS (ppi / pixels);
19168 #endif
19169
19170 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19171 || (CONSP (Vdisplay_pixels_per_inch)
19172 && (ppi = (width_p
19173 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19174 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19175 ppi > 0)))
19176 return OK_PIXELS (ppi / pixels);
19177
19178 return 0;
19179 }
19180 }
19181
19182 #ifdef HAVE_WINDOW_SYSTEM
19183 if (EQ (prop, Qheight))
19184 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19185 if (EQ (prop, Qwidth))
19186 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19187 #else
19188 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19189 return OK_PIXELS (1);
19190 #endif
19191
19192 if (EQ (prop, Qtext))
19193 return OK_PIXELS (width_p
19194 ? window_box_width (it->w, TEXT_AREA)
19195 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19196
19197 if (align_to && *align_to < 0)
19198 {
19199 *res = 0;
19200 if (EQ (prop, Qleft))
19201 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19202 if (EQ (prop, Qright))
19203 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19204 if (EQ (prop, Qcenter))
19205 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19206 + window_box_width (it->w, TEXT_AREA) / 2);
19207 if (EQ (prop, Qleft_fringe))
19208 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19209 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19210 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19211 if (EQ (prop, Qright_fringe))
19212 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19213 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19214 : window_box_right_offset (it->w, TEXT_AREA));
19215 if (EQ (prop, Qleft_margin))
19216 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19217 if (EQ (prop, Qright_margin))
19218 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19219 if (EQ (prop, Qscroll_bar))
19220 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19221 ? 0
19222 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19223 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19224 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19225 : 0)));
19226 }
19227 else
19228 {
19229 if (EQ (prop, Qleft_fringe))
19230 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19231 if (EQ (prop, Qright_fringe))
19232 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19233 if (EQ (prop, Qleft_margin))
19234 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19235 if (EQ (prop, Qright_margin))
19236 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19237 if (EQ (prop, Qscroll_bar))
19238 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19239 }
19240
19241 prop = Fbuffer_local_value (prop, it->w->buffer);
19242 }
19243
19244 if (INTEGERP (prop) || FLOATP (prop))
19245 {
19246 int base_unit = (width_p
19247 ? FRAME_COLUMN_WIDTH (it->f)
19248 : FRAME_LINE_HEIGHT (it->f));
19249 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19250 }
19251
19252 if (CONSP (prop))
19253 {
19254 Lisp_Object car = XCAR (prop);
19255 Lisp_Object cdr = XCDR (prop);
19256
19257 if (SYMBOLP (car))
19258 {
19259 #ifdef HAVE_WINDOW_SYSTEM
19260 if (FRAME_WINDOW_P (it->f)
19261 && valid_image_p (prop))
19262 {
19263 int id = lookup_image (it->f, prop);
19264 struct image *img = IMAGE_FROM_ID (it->f, id);
19265
19266 return OK_PIXELS (width_p ? img->width : img->height);
19267 }
19268 #endif
19269 if (EQ (car, Qplus) || EQ (car, Qminus))
19270 {
19271 int first = 1;
19272 double px;
19273
19274 pixels = 0;
19275 while (CONSP (cdr))
19276 {
19277 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19278 font, width_p, align_to))
19279 return 0;
19280 if (first)
19281 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19282 else
19283 pixels += px;
19284 cdr = XCDR (cdr);
19285 }
19286 if (EQ (car, Qminus))
19287 pixels = -pixels;
19288 return OK_PIXELS (pixels);
19289 }
19290
19291 car = Fbuffer_local_value (car, it->w->buffer);
19292 }
19293
19294 if (INTEGERP (car) || FLOATP (car))
19295 {
19296 double fact;
19297 pixels = XFLOATINT (car);
19298 if (NILP (cdr))
19299 return OK_PIXELS (pixels);
19300 if (calc_pixel_width_or_height (&fact, it, cdr,
19301 font, width_p, align_to))
19302 return OK_PIXELS (pixels * fact);
19303 return 0;
19304 }
19305
19306 return 0;
19307 }
19308
19309 return 0;
19310 }
19311
19312 \f
19313 /***********************************************************************
19314 Glyph Display
19315 ***********************************************************************/
19316
19317 #ifdef HAVE_WINDOW_SYSTEM
19318
19319 #if GLYPH_DEBUG
19320
19321 void
19322 dump_glyph_string (s)
19323 struct glyph_string *s;
19324 {
19325 fprintf (stderr, "glyph string\n");
19326 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19327 s->x, s->y, s->width, s->height);
19328 fprintf (stderr, " ybase = %d\n", s->ybase);
19329 fprintf (stderr, " hl = %d\n", s->hl);
19330 fprintf (stderr, " left overhang = %d, right = %d\n",
19331 s->left_overhang, s->right_overhang);
19332 fprintf (stderr, " nchars = %d\n", s->nchars);
19333 fprintf (stderr, " extends to end of line = %d\n",
19334 s->extends_to_end_of_line_p);
19335 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19336 fprintf (stderr, " bg width = %d\n", s->background_width);
19337 }
19338
19339 #endif /* GLYPH_DEBUG */
19340
19341 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19342 of XChar2b structures for S; it can't be allocated in
19343 init_glyph_string because it must be allocated via `alloca'. W
19344 is the window on which S is drawn. ROW and AREA are the glyph row
19345 and area within the row from which S is constructed. START is the
19346 index of the first glyph structure covered by S. HL is a
19347 face-override for drawing S. */
19348
19349 #ifdef HAVE_NTGUI
19350 #define OPTIONAL_HDC(hdc) hdc,
19351 #define DECLARE_HDC(hdc) HDC hdc;
19352 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19353 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19354 #endif
19355
19356 #ifndef OPTIONAL_HDC
19357 #define OPTIONAL_HDC(hdc)
19358 #define DECLARE_HDC(hdc)
19359 #define ALLOCATE_HDC(hdc, f)
19360 #define RELEASE_HDC(hdc, f)
19361 #endif
19362
19363 static void
19364 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19365 struct glyph_string *s;
19366 DECLARE_HDC (hdc)
19367 XChar2b *char2b;
19368 struct window *w;
19369 struct glyph_row *row;
19370 enum glyph_row_area area;
19371 int start;
19372 enum draw_glyphs_face hl;
19373 {
19374 bzero (s, sizeof *s);
19375 s->w = w;
19376 s->f = XFRAME (w->frame);
19377 #ifdef HAVE_NTGUI
19378 s->hdc = hdc;
19379 #endif
19380 s->display = FRAME_X_DISPLAY (s->f);
19381 s->window = FRAME_X_WINDOW (s->f);
19382 s->char2b = char2b;
19383 s->hl = hl;
19384 s->row = row;
19385 s->area = area;
19386 s->first_glyph = row->glyphs[area] + start;
19387 s->height = row->height;
19388 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19389
19390 /* Display the internal border below the tool-bar window. */
19391 if (WINDOWP (s->f->tool_bar_window)
19392 && s->w == XWINDOW (s->f->tool_bar_window))
19393 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19394
19395 s->ybase = s->y + row->ascent;
19396 }
19397
19398
19399 /* Append the list of glyph strings with head H and tail T to the list
19400 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19401
19402 static INLINE void
19403 append_glyph_string_lists (head, tail, h, t)
19404 struct glyph_string **head, **tail;
19405 struct glyph_string *h, *t;
19406 {
19407 if (h)
19408 {
19409 if (*head)
19410 (*tail)->next = h;
19411 else
19412 *head = h;
19413 h->prev = *tail;
19414 *tail = t;
19415 }
19416 }
19417
19418
19419 /* Prepend the list of glyph strings with head H and tail T to the
19420 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19421 result. */
19422
19423 static INLINE void
19424 prepend_glyph_string_lists (head, tail, h, t)
19425 struct glyph_string **head, **tail;
19426 struct glyph_string *h, *t;
19427 {
19428 if (h)
19429 {
19430 if (*head)
19431 (*head)->prev = t;
19432 else
19433 *tail = t;
19434 t->next = *head;
19435 *head = h;
19436 }
19437 }
19438
19439
19440 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19441 Set *HEAD and *TAIL to the resulting list. */
19442
19443 static INLINE void
19444 append_glyph_string (head, tail, s)
19445 struct glyph_string **head, **tail;
19446 struct glyph_string *s;
19447 {
19448 s->next = s->prev = NULL;
19449 append_glyph_string_lists (head, tail, s, s);
19450 }
19451
19452
19453 /* Get face and two-byte form of character C in face FACE_ID on frame
19454 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19455 means we want to display multibyte text. DISPLAY_P non-zero means
19456 make sure that X resources for the face returned are allocated.
19457 Value is a pointer to a realized face that is ready for display if
19458 DISPLAY_P is non-zero. */
19459
19460 static INLINE struct face *
19461 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19462 struct frame *f;
19463 int c, face_id;
19464 XChar2b *char2b;
19465 int multibyte_p, display_p;
19466 {
19467 struct face *face = FACE_FROM_ID (f, face_id);
19468
19469 if (face->font)
19470 {
19471 unsigned code = face->font->driver->encode_char (face->font, c);
19472
19473 if (code != FONT_INVALID_CODE)
19474 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19475 else
19476 STORE_XCHAR2B (char2b, 0, 0);
19477 }
19478
19479 /* Make sure X resources of the face are allocated. */
19480 #ifdef HAVE_X_WINDOWS
19481 if (display_p)
19482 #endif
19483 {
19484 xassert (face != NULL);
19485 PREPARE_FACE_FOR_DISPLAY (f, face);
19486 }
19487
19488 return face;
19489 }
19490
19491
19492 /* Get face and two-byte form of character glyph GLYPH on frame F.
19493 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19494 a pointer to a realized face that is ready for display. */
19495
19496 static INLINE struct face *
19497 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19498 struct frame *f;
19499 struct glyph *glyph;
19500 XChar2b *char2b;
19501 int *two_byte_p;
19502 {
19503 struct face *face;
19504
19505 xassert (glyph->type == CHAR_GLYPH);
19506 face = FACE_FROM_ID (f, glyph->face_id);
19507
19508 if (two_byte_p)
19509 *two_byte_p = 0;
19510
19511 if (face->font)
19512 {
19513 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19514
19515 if (code != FONT_INVALID_CODE)
19516 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19517 else
19518 STORE_XCHAR2B (char2b, 0, 0);
19519 }
19520
19521 /* Make sure X resources of the face are allocated. */
19522 xassert (face != NULL);
19523 PREPARE_FACE_FOR_DISPLAY (f, face);
19524 return face;
19525 }
19526
19527
19528 /* Fill glyph string S with composition components specified by S->cmp.
19529
19530 BASE_FACE is the base face of the composition.
19531 S->cmp_from is the index of the first component for S.
19532
19533 OVERLAPS non-zero means S should draw the foreground only, and use
19534 its physical height for clipping. See also draw_glyphs.
19535
19536 Value is the index of a component not in S. */
19537
19538 static int
19539 fill_composite_glyph_string (s, base_face, overlaps)
19540 struct glyph_string *s;
19541 struct face *base_face;
19542 int overlaps;
19543 {
19544 int i;
19545 /* For all glyphs of this composition, starting at the offset
19546 S->cmp_from, until we reach the end of the definition or encounter a
19547 glyph that requires the different face, add it to S. */
19548 struct face *face;
19549
19550 xassert (s);
19551
19552 s->for_overlaps = overlaps;
19553 s->face = NULL;
19554 s->font = NULL;
19555 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19556 {
19557 int c = COMPOSITION_GLYPH (s->cmp, i);
19558
19559 if (c != '\t')
19560 {
19561 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19562 -1, Qnil);
19563
19564 face = get_char_face_and_encoding (s->f, c, face_id,
19565 s->char2b + i, 1, 1);
19566 if (face)
19567 {
19568 if (! s->face)
19569 {
19570 s->face = face;
19571 s->font = s->face->font;
19572 }
19573 else if (s->face != face)
19574 break;
19575 }
19576 }
19577 ++s->nchars;
19578 }
19579 s->cmp_to = i;
19580
19581 /* All glyph strings for the same composition has the same width,
19582 i.e. the width set for the first component of the composition. */
19583 s->width = s->first_glyph->pixel_width;
19584
19585 /* If the specified font could not be loaded, use the frame's
19586 default font, but record the fact that we couldn't load it in
19587 the glyph string so that we can draw rectangles for the
19588 characters of the glyph string. */
19589 if (s->font == NULL)
19590 {
19591 s->font_not_found_p = 1;
19592 s->font = FRAME_FONT (s->f);
19593 }
19594
19595 /* Adjust base line for subscript/superscript text. */
19596 s->ybase += s->first_glyph->voffset;
19597
19598 /* This glyph string must always be drawn with 16-bit functions. */
19599 s->two_byte_p = 1;
19600
19601 return s->cmp_to;
19602 }
19603
19604 static int
19605 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19606 struct glyph_string *s;
19607 int face_id;
19608 int start, end, overlaps;
19609 {
19610 struct glyph *glyph, *last;
19611 Lisp_Object lgstring;
19612 int i;
19613
19614 s->for_overlaps = overlaps;
19615 glyph = s->row->glyphs[s->area] + start;
19616 last = s->row->glyphs[s->area] + end;
19617 s->cmp_id = glyph->u.cmp.id;
19618 s->cmp_from = glyph->u.cmp.from;
19619 s->cmp_to = glyph->u.cmp.to + 1;
19620 s->face = FACE_FROM_ID (s->f, face_id);
19621 lgstring = composition_gstring_from_id (s->cmp_id);
19622 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19623 glyph++;
19624 while (glyph < last
19625 && glyph->u.cmp.automatic
19626 && glyph->u.cmp.id == s->cmp_id
19627 && s->cmp_to == glyph->u.cmp.from)
19628 s->cmp_to = (glyph++)->u.cmp.to + 1;
19629
19630 for (i = s->cmp_from; i < s->cmp_to; i++)
19631 {
19632 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19633 unsigned code = LGLYPH_CODE (lglyph);
19634
19635 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19636 }
19637 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19638 return glyph - s->row->glyphs[s->area];
19639 }
19640
19641
19642 /* Fill glyph string S from a sequence of character glyphs.
19643
19644 FACE_ID is the face id of the string. START is the index of the
19645 first glyph to consider, END is the index of the last + 1.
19646 OVERLAPS non-zero means S should draw the foreground only, and use
19647 its physical height for clipping. See also draw_glyphs.
19648
19649 Value is the index of the first glyph not in S. */
19650
19651 static int
19652 fill_glyph_string (s, face_id, start, end, overlaps)
19653 struct glyph_string *s;
19654 int face_id;
19655 int start, end, overlaps;
19656 {
19657 struct glyph *glyph, *last;
19658 int voffset;
19659 int glyph_not_available_p;
19660
19661 xassert (s->f == XFRAME (s->w->frame));
19662 xassert (s->nchars == 0);
19663 xassert (start >= 0 && end > start);
19664
19665 s->for_overlaps = overlaps;
19666 glyph = s->row->glyphs[s->area] + start;
19667 last = s->row->glyphs[s->area] + end;
19668 voffset = glyph->voffset;
19669 s->padding_p = glyph->padding_p;
19670 glyph_not_available_p = glyph->glyph_not_available_p;
19671
19672 while (glyph < last
19673 && glyph->type == CHAR_GLYPH
19674 && glyph->voffset == voffset
19675 /* Same face id implies same font, nowadays. */
19676 && glyph->face_id == face_id
19677 && glyph->glyph_not_available_p == glyph_not_available_p)
19678 {
19679 int two_byte_p;
19680
19681 s->face = get_glyph_face_and_encoding (s->f, glyph,
19682 s->char2b + s->nchars,
19683 &two_byte_p);
19684 s->two_byte_p = two_byte_p;
19685 ++s->nchars;
19686 xassert (s->nchars <= end - start);
19687 s->width += glyph->pixel_width;
19688 if (glyph++->padding_p != s->padding_p)
19689 break;
19690 }
19691
19692 s->font = s->face->font;
19693
19694 /* If the specified font could not be loaded, use the frame's font,
19695 but record the fact that we couldn't load it in
19696 S->font_not_found_p so that we can draw rectangles for the
19697 characters of the glyph string. */
19698 if (s->font == NULL || glyph_not_available_p)
19699 {
19700 s->font_not_found_p = 1;
19701 s->font = FRAME_FONT (s->f);
19702 }
19703
19704 /* Adjust base line for subscript/superscript text. */
19705 s->ybase += voffset;
19706
19707 xassert (s->face && s->face->gc);
19708 return glyph - s->row->glyphs[s->area];
19709 }
19710
19711
19712 /* Fill glyph string S from image glyph S->first_glyph. */
19713
19714 static void
19715 fill_image_glyph_string (s)
19716 struct glyph_string *s;
19717 {
19718 xassert (s->first_glyph->type == IMAGE_GLYPH);
19719 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19720 xassert (s->img);
19721 s->slice = s->first_glyph->slice;
19722 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19723 s->font = s->face->font;
19724 s->width = s->first_glyph->pixel_width;
19725
19726 /* Adjust base line for subscript/superscript text. */
19727 s->ybase += s->first_glyph->voffset;
19728 }
19729
19730
19731 /* Fill glyph string S from a sequence of stretch glyphs.
19732
19733 ROW is the glyph row in which the glyphs are found, AREA is the
19734 area within the row. START is the index of the first glyph to
19735 consider, END is the index of the last + 1.
19736
19737 Value is the index of the first glyph not in S. */
19738
19739 static int
19740 fill_stretch_glyph_string (s, row, area, start, end)
19741 struct glyph_string *s;
19742 struct glyph_row *row;
19743 enum glyph_row_area area;
19744 int start, end;
19745 {
19746 struct glyph *glyph, *last;
19747 int voffset, face_id;
19748
19749 xassert (s->first_glyph->type == STRETCH_GLYPH);
19750
19751 glyph = s->row->glyphs[s->area] + start;
19752 last = s->row->glyphs[s->area] + end;
19753 face_id = glyph->face_id;
19754 s->face = FACE_FROM_ID (s->f, face_id);
19755 s->font = s->face->font;
19756 s->width = glyph->pixel_width;
19757 s->nchars = 1;
19758 voffset = glyph->voffset;
19759
19760 for (++glyph;
19761 (glyph < last
19762 && glyph->type == STRETCH_GLYPH
19763 && glyph->voffset == voffset
19764 && glyph->face_id == face_id);
19765 ++glyph)
19766 s->width += glyph->pixel_width;
19767
19768 /* Adjust base line for subscript/superscript text. */
19769 s->ybase += voffset;
19770
19771 /* The case that face->gc == 0 is handled when drawing the glyph
19772 string by calling PREPARE_FACE_FOR_DISPLAY. */
19773 xassert (s->face);
19774 return glyph - s->row->glyphs[s->area];
19775 }
19776
19777 static struct font_metrics *
19778 get_per_char_metric (f, font, char2b)
19779 struct frame *f;
19780 struct font *font;
19781 XChar2b *char2b;
19782 {
19783 static struct font_metrics metrics;
19784 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19785
19786 if (! font || code == FONT_INVALID_CODE)
19787 return NULL;
19788 font->driver->text_extents (font, &code, 1, &metrics);
19789 return &metrics;
19790 }
19791
19792 /* EXPORT for RIF:
19793 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19794 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19795 assumed to be zero. */
19796
19797 void
19798 x_get_glyph_overhangs (glyph, f, left, right)
19799 struct glyph *glyph;
19800 struct frame *f;
19801 int *left, *right;
19802 {
19803 *left = *right = 0;
19804
19805 if (glyph->type == CHAR_GLYPH)
19806 {
19807 struct face *face;
19808 XChar2b char2b;
19809 struct font_metrics *pcm;
19810
19811 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19812 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19813 {
19814 if (pcm->rbearing > pcm->width)
19815 *right = pcm->rbearing - pcm->width;
19816 if (pcm->lbearing < 0)
19817 *left = -pcm->lbearing;
19818 }
19819 }
19820 else if (glyph->type == COMPOSITE_GLYPH)
19821 {
19822 if (! glyph->u.cmp.automatic)
19823 {
19824 struct composition *cmp = composition_table[glyph->u.cmp.id];
19825
19826 if (cmp->rbearing > cmp->pixel_width)
19827 *right = cmp->rbearing - cmp->pixel_width;
19828 if (cmp->lbearing < 0)
19829 *left = - cmp->lbearing;
19830 }
19831 else
19832 {
19833 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19834 struct font_metrics metrics;
19835
19836 composition_gstring_width (gstring, glyph->u.cmp.from,
19837 glyph->u.cmp.to + 1, &metrics);
19838 if (metrics.rbearing > metrics.width)
19839 *right = metrics.rbearing - metrics.width;
19840 if (metrics.lbearing < 0)
19841 *left = - metrics.lbearing;
19842 }
19843 }
19844 }
19845
19846
19847 /* Return the index of the first glyph preceding glyph string S that
19848 is overwritten by S because of S's left overhang. Value is -1
19849 if no glyphs are overwritten. */
19850
19851 static int
19852 left_overwritten (s)
19853 struct glyph_string *s;
19854 {
19855 int k;
19856
19857 if (s->left_overhang)
19858 {
19859 int x = 0, i;
19860 struct glyph *glyphs = s->row->glyphs[s->area];
19861 int first = s->first_glyph - glyphs;
19862
19863 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19864 x -= glyphs[i].pixel_width;
19865
19866 k = i + 1;
19867 }
19868 else
19869 k = -1;
19870
19871 return k;
19872 }
19873
19874
19875 /* Return the index of the first glyph preceding glyph string S that
19876 is overwriting S because of its right overhang. Value is -1 if no
19877 glyph in front of S overwrites S. */
19878
19879 static int
19880 left_overwriting (s)
19881 struct glyph_string *s;
19882 {
19883 int i, k, x;
19884 struct glyph *glyphs = s->row->glyphs[s->area];
19885 int first = s->first_glyph - glyphs;
19886
19887 k = -1;
19888 x = 0;
19889 for (i = first - 1; i >= 0; --i)
19890 {
19891 int left, right;
19892 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19893 if (x + right > 0)
19894 k = i;
19895 x -= glyphs[i].pixel_width;
19896 }
19897
19898 return k;
19899 }
19900
19901
19902 /* Return the index of the last glyph following glyph string S that is
19903 overwritten by S because of S's right overhang. Value is -1 if
19904 no such glyph is found. */
19905
19906 static int
19907 right_overwritten (s)
19908 struct glyph_string *s;
19909 {
19910 int k = -1;
19911
19912 if (s->right_overhang)
19913 {
19914 int x = 0, i;
19915 struct glyph *glyphs = s->row->glyphs[s->area];
19916 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19917 int end = s->row->used[s->area];
19918
19919 for (i = first; i < end && s->right_overhang > x; ++i)
19920 x += glyphs[i].pixel_width;
19921
19922 k = i;
19923 }
19924
19925 return k;
19926 }
19927
19928
19929 /* Return the index of the last glyph following glyph string S that
19930 overwrites S because of its left overhang. Value is negative
19931 if no such glyph is found. */
19932
19933 static int
19934 right_overwriting (s)
19935 struct glyph_string *s;
19936 {
19937 int i, k, x;
19938 int end = s->row->used[s->area];
19939 struct glyph *glyphs = s->row->glyphs[s->area];
19940 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19941
19942 k = -1;
19943 x = 0;
19944 for (i = first; i < end; ++i)
19945 {
19946 int left, right;
19947 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19948 if (x - left < 0)
19949 k = i;
19950 x += glyphs[i].pixel_width;
19951 }
19952
19953 return k;
19954 }
19955
19956
19957 /* Set background width of glyph string S. START is the index of the
19958 first glyph following S. LAST_X is the right-most x-position + 1
19959 in the drawing area. */
19960
19961 static INLINE void
19962 set_glyph_string_background_width (s, start, last_x)
19963 struct glyph_string *s;
19964 int start;
19965 int last_x;
19966 {
19967 /* If the face of this glyph string has to be drawn to the end of
19968 the drawing area, set S->extends_to_end_of_line_p. */
19969
19970 if (start == s->row->used[s->area]
19971 && s->area == TEXT_AREA
19972 && ((s->row->fill_line_p
19973 && (s->hl == DRAW_NORMAL_TEXT
19974 || s->hl == DRAW_IMAGE_RAISED
19975 || s->hl == DRAW_IMAGE_SUNKEN))
19976 || s->hl == DRAW_MOUSE_FACE))
19977 s->extends_to_end_of_line_p = 1;
19978
19979 /* If S extends its face to the end of the line, set its
19980 background_width to the distance to the right edge of the drawing
19981 area. */
19982 if (s->extends_to_end_of_line_p)
19983 s->background_width = last_x - s->x + 1;
19984 else
19985 s->background_width = s->width;
19986 }
19987
19988
19989 /* Compute overhangs and x-positions for glyph string S and its
19990 predecessors, or successors. X is the starting x-position for S.
19991 BACKWARD_P non-zero means process predecessors. */
19992
19993 static void
19994 compute_overhangs_and_x (s, x, backward_p)
19995 struct glyph_string *s;
19996 int x;
19997 int backward_p;
19998 {
19999 if (backward_p)
20000 {
20001 while (s)
20002 {
20003 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20004 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20005 x -= s->width;
20006 s->x = x;
20007 s = s->prev;
20008 }
20009 }
20010 else
20011 {
20012 while (s)
20013 {
20014 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20015 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20016 s->x = x;
20017 x += s->width;
20018 s = s->next;
20019 }
20020 }
20021 }
20022
20023
20024
20025 /* The following macros are only called from draw_glyphs below.
20026 They reference the following parameters of that function directly:
20027 `w', `row', `area', and `overlap_p'
20028 as well as the following local variables:
20029 `s', `f', and `hdc' (in W32) */
20030
20031 #ifdef HAVE_NTGUI
20032 /* On W32, silently add local `hdc' variable to argument list of
20033 init_glyph_string. */
20034 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20035 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20036 #else
20037 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20038 init_glyph_string (s, char2b, w, row, area, start, hl)
20039 #endif
20040
20041 /* Add a glyph string for a stretch glyph to the list of strings
20042 between HEAD and TAIL. START is the index of the stretch glyph in
20043 row area AREA of glyph row ROW. END is the index of the last glyph
20044 in that glyph row area. X is the current output position assigned
20045 to the new glyph string constructed. HL overrides that face of the
20046 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20047 is the right-most x-position of the drawing area. */
20048
20049 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20050 and below -- keep them on one line. */
20051 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20052 do \
20053 { \
20054 s = (struct glyph_string *) alloca (sizeof *s); \
20055 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20056 START = fill_stretch_glyph_string (s, row, area, START, END); \
20057 append_glyph_string (&HEAD, &TAIL, s); \
20058 s->x = (X); \
20059 } \
20060 while (0)
20061
20062
20063 /* Add a glyph string for an image glyph to the list of strings
20064 between HEAD and TAIL. START is the index of the image glyph in
20065 row area AREA of glyph row ROW. END is the index of the last glyph
20066 in that glyph row area. X is the current output position assigned
20067 to the new glyph string constructed. HL overrides that face of the
20068 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20069 is the right-most x-position of the drawing area. */
20070
20071 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20072 do \
20073 { \
20074 s = (struct glyph_string *) alloca (sizeof *s); \
20075 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20076 fill_image_glyph_string (s); \
20077 append_glyph_string (&HEAD, &TAIL, s); \
20078 ++START; \
20079 s->x = (X); \
20080 } \
20081 while (0)
20082
20083
20084 /* Add a glyph string for a sequence of character glyphs to the list
20085 of strings between HEAD and TAIL. START is the index of the first
20086 glyph in row area AREA of glyph row ROW that is part of the new
20087 glyph string. END is the index of the last glyph in that glyph row
20088 area. X is the current output position assigned to the new glyph
20089 string constructed. HL overrides that face of the glyph; e.g. it
20090 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20091 right-most x-position of the drawing area. */
20092
20093 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20094 do \
20095 { \
20096 int face_id; \
20097 XChar2b *char2b; \
20098 \
20099 face_id = (row)->glyphs[area][START].face_id; \
20100 \
20101 s = (struct glyph_string *) alloca (sizeof *s); \
20102 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20103 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20104 append_glyph_string (&HEAD, &TAIL, s); \
20105 s->x = (X); \
20106 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20107 } \
20108 while (0)
20109
20110
20111 /* Add a glyph string for a composite sequence to the list of strings
20112 between HEAD and TAIL. START is the index of the first glyph in
20113 row area AREA of glyph row ROW that is part of the new glyph
20114 string. END is the index of the last glyph in that glyph row area.
20115 X is the current output position assigned to the new glyph string
20116 constructed. HL overrides that face of the glyph; e.g. it is
20117 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20118 x-position of the drawing area. */
20119
20120 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20121 do { \
20122 int face_id = (row)->glyphs[area][START].face_id; \
20123 struct face *base_face = FACE_FROM_ID (f, face_id); \
20124 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20125 struct composition *cmp = composition_table[cmp_id]; \
20126 XChar2b *char2b; \
20127 struct glyph_string *first_s; \
20128 int n; \
20129 \
20130 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20131 \
20132 /* Make glyph_strings for each glyph sequence that is drawable by \
20133 the same face, and append them to HEAD/TAIL. */ \
20134 for (n = 0; n < cmp->glyph_len;) \
20135 { \
20136 s = (struct glyph_string *) alloca (sizeof *s); \
20137 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20138 append_glyph_string (&(HEAD), &(TAIL), s); \
20139 s->cmp = cmp; \
20140 s->cmp_from = n; \
20141 s->x = (X); \
20142 if (n == 0) \
20143 first_s = s; \
20144 n = fill_composite_glyph_string (s, base_face, overlaps); \
20145 } \
20146 \
20147 ++START; \
20148 s = first_s; \
20149 } while (0)
20150
20151
20152 /* Add a glyph string for a glyph-string sequence to the list of strings
20153 between HEAD and TAIL. */
20154
20155 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20156 do { \
20157 int face_id; \
20158 XChar2b *char2b; \
20159 Lisp_Object gstring; \
20160 \
20161 face_id = (row)->glyphs[area][START].face_id; \
20162 gstring = (composition_gstring_from_id \
20163 ((row)->glyphs[area][START].u.cmp.id)); \
20164 s = (struct glyph_string *) alloca (sizeof *s); \
20165 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20166 * LGSTRING_GLYPH_LEN (gstring)); \
20167 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20168 append_glyph_string (&(HEAD), &(TAIL), s); \
20169 s->x = (X); \
20170 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20171 } while (0)
20172
20173
20174 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20175 of AREA of glyph row ROW on window W between indices START and END.
20176 HL overrides the face for drawing glyph strings, e.g. it is
20177 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20178 x-positions of the drawing area.
20179
20180 This is an ugly monster macro construct because we must use alloca
20181 to allocate glyph strings (because draw_glyphs can be called
20182 asynchronously). */
20183
20184 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20185 do \
20186 { \
20187 HEAD = TAIL = NULL; \
20188 while (START < END) \
20189 { \
20190 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20191 switch (first_glyph->type) \
20192 { \
20193 case CHAR_GLYPH: \
20194 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20195 HL, X, LAST_X); \
20196 break; \
20197 \
20198 case COMPOSITE_GLYPH: \
20199 if (first_glyph->u.cmp.automatic) \
20200 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20201 HL, X, LAST_X); \
20202 else \
20203 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20204 HL, X, LAST_X); \
20205 break; \
20206 \
20207 case STRETCH_GLYPH: \
20208 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20209 HL, X, LAST_X); \
20210 break; \
20211 \
20212 case IMAGE_GLYPH: \
20213 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20214 HL, X, LAST_X); \
20215 break; \
20216 \
20217 default: \
20218 abort (); \
20219 } \
20220 \
20221 if (s) \
20222 { \
20223 set_glyph_string_background_width (s, START, LAST_X); \
20224 (X) += s->width; \
20225 } \
20226 } \
20227 } while (0)
20228
20229
20230 /* Draw glyphs between START and END in AREA of ROW on window W,
20231 starting at x-position X. X is relative to AREA in W. HL is a
20232 face-override with the following meaning:
20233
20234 DRAW_NORMAL_TEXT draw normally
20235 DRAW_CURSOR draw in cursor face
20236 DRAW_MOUSE_FACE draw in mouse face.
20237 DRAW_INVERSE_VIDEO draw in mode line face
20238 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20239 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20240
20241 If OVERLAPS is non-zero, draw only the foreground of characters and
20242 clip to the physical height of ROW. Non-zero value also defines
20243 the overlapping part to be drawn:
20244
20245 OVERLAPS_PRED overlap with preceding rows
20246 OVERLAPS_SUCC overlap with succeeding rows
20247 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20248 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20249
20250 Value is the x-position reached, relative to AREA of W. */
20251
20252 static int
20253 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20254 struct window *w;
20255 int x;
20256 struct glyph_row *row;
20257 enum glyph_row_area area;
20258 EMACS_INT start, end;
20259 enum draw_glyphs_face hl;
20260 int overlaps;
20261 {
20262 struct glyph_string *head, *tail;
20263 struct glyph_string *s;
20264 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20265 int i, j, x_reached, last_x, area_left = 0;
20266 struct frame *f = XFRAME (WINDOW_FRAME (w));
20267 DECLARE_HDC (hdc);
20268
20269 ALLOCATE_HDC (hdc, f);
20270
20271 /* Let's rather be paranoid than getting a SEGV. */
20272 end = min (end, row->used[area]);
20273 start = max (0, start);
20274 start = min (end, start);
20275
20276 /* Translate X to frame coordinates. Set last_x to the right
20277 end of the drawing area. */
20278 if (row->full_width_p)
20279 {
20280 /* X is relative to the left edge of W, without scroll bars
20281 or fringes. */
20282 area_left = WINDOW_LEFT_EDGE_X (w);
20283 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20284 }
20285 else
20286 {
20287 area_left = window_box_left (w, area);
20288 last_x = area_left + window_box_width (w, area);
20289 }
20290 x += area_left;
20291
20292 /* Build a doubly-linked list of glyph_string structures between
20293 head and tail from what we have to draw. Note that the macro
20294 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20295 the reason we use a separate variable `i'. */
20296 i = start;
20297 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20298 if (tail)
20299 x_reached = tail->x + tail->background_width;
20300 else
20301 x_reached = x;
20302
20303 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20304 the row, redraw some glyphs in front or following the glyph
20305 strings built above. */
20306 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20307 {
20308 struct glyph_string *h, *t;
20309 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20310 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20311 int dummy_x = 0;
20312
20313 /* If mouse highlighting is on, we may need to draw adjacent
20314 glyphs using mouse-face highlighting. */
20315 if (area == TEXT_AREA && row->mouse_face_p)
20316 {
20317 struct glyph_row *mouse_beg_row, *mouse_end_row;
20318
20319 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20320 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20321
20322 if (row >= mouse_beg_row && row <= mouse_end_row)
20323 {
20324 check_mouse_face = 1;
20325 mouse_beg_col = (row == mouse_beg_row)
20326 ? dpyinfo->mouse_face_beg_col : 0;
20327 mouse_end_col = (row == mouse_end_row)
20328 ? dpyinfo->mouse_face_end_col
20329 : row->used[TEXT_AREA];
20330 }
20331 }
20332
20333 /* Compute overhangs for all glyph strings. */
20334 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20335 for (s = head; s; s = s->next)
20336 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20337
20338 /* Prepend glyph strings for glyphs in front of the first glyph
20339 string that are overwritten because of the first glyph
20340 string's left overhang. The background of all strings
20341 prepended must be drawn because the first glyph string
20342 draws over it. */
20343 i = left_overwritten (head);
20344 if (i >= 0)
20345 {
20346 enum draw_glyphs_face overlap_hl;
20347
20348 /* If this row contains mouse highlighting, attempt to draw
20349 the overlapped glyphs with the correct highlight. This
20350 code fails if the overlap encompasses more than one glyph
20351 and mouse-highlight spans only some of these glyphs.
20352 However, making it work perfectly involves a lot more
20353 code, and I don't know if the pathological case occurs in
20354 practice, so we'll stick to this for now. --- cyd */
20355 if (check_mouse_face
20356 && mouse_beg_col < start && mouse_end_col > i)
20357 overlap_hl = DRAW_MOUSE_FACE;
20358 else
20359 overlap_hl = DRAW_NORMAL_TEXT;
20360
20361 j = i;
20362 BUILD_GLYPH_STRINGS (j, start, h, t,
20363 overlap_hl, dummy_x, last_x);
20364 compute_overhangs_and_x (t, head->x, 1);
20365 prepend_glyph_string_lists (&head, &tail, h, t);
20366 clip_head = head;
20367 }
20368
20369 /* Prepend glyph strings for glyphs in front of the first glyph
20370 string that overwrite that glyph string because of their
20371 right overhang. For these strings, only the foreground must
20372 be drawn, because it draws over the glyph string at `head'.
20373 The background must not be drawn because this would overwrite
20374 right overhangs of preceding glyphs for which no glyph
20375 strings exist. */
20376 i = left_overwriting (head);
20377 if (i >= 0)
20378 {
20379 enum draw_glyphs_face overlap_hl;
20380
20381 if (check_mouse_face
20382 && mouse_beg_col < start && mouse_end_col > i)
20383 overlap_hl = DRAW_MOUSE_FACE;
20384 else
20385 overlap_hl = DRAW_NORMAL_TEXT;
20386
20387 clip_head = head;
20388 BUILD_GLYPH_STRINGS (i, start, h, t,
20389 overlap_hl, dummy_x, last_x);
20390 for (s = h; s; s = s->next)
20391 s->background_filled_p = 1;
20392 compute_overhangs_and_x (t, head->x, 1);
20393 prepend_glyph_string_lists (&head, &tail, h, t);
20394 }
20395
20396 /* Append glyphs strings for glyphs following the last glyph
20397 string tail that are overwritten by tail. The background of
20398 these strings has to be drawn because tail's foreground draws
20399 over it. */
20400 i = right_overwritten (tail);
20401 if (i >= 0)
20402 {
20403 enum draw_glyphs_face overlap_hl;
20404
20405 if (check_mouse_face
20406 && mouse_beg_col < i && mouse_end_col > end)
20407 overlap_hl = DRAW_MOUSE_FACE;
20408 else
20409 overlap_hl = DRAW_NORMAL_TEXT;
20410
20411 BUILD_GLYPH_STRINGS (end, i, h, t,
20412 overlap_hl, x, last_x);
20413 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20414 append_glyph_string_lists (&head, &tail, h, t);
20415 clip_tail = tail;
20416 }
20417
20418 /* Append glyph strings for glyphs following the last glyph
20419 string tail that overwrite tail. The foreground of such
20420 glyphs has to be drawn because it writes into the background
20421 of tail. The background must not be drawn because it could
20422 paint over the foreground of following glyphs. */
20423 i = right_overwriting (tail);
20424 if (i >= 0)
20425 {
20426 enum draw_glyphs_face overlap_hl;
20427 if (check_mouse_face
20428 && mouse_beg_col < i && mouse_end_col > end)
20429 overlap_hl = DRAW_MOUSE_FACE;
20430 else
20431 overlap_hl = DRAW_NORMAL_TEXT;
20432
20433 clip_tail = tail;
20434 i++; /* We must include the Ith glyph. */
20435 BUILD_GLYPH_STRINGS (end, i, h, t,
20436 overlap_hl, x, last_x);
20437 for (s = h; s; s = s->next)
20438 s->background_filled_p = 1;
20439 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20440 append_glyph_string_lists (&head, &tail, h, t);
20441 }
20442 if (clip_head || clip_tail)
20443 for (s = head; s; s = s->next)
20444 {
20445 s->clip_head = clip_head;
20446 s->clip_tail = clip_tail;
20447 }
20448 }
20449
20450 /* Draw all strings. */
20451 for (s = head; s; s = s->next)
20452 FRAME_RIF (f)->draw_glyph_string (s);
20453
20454 #ifndef HAVE_NS
20455 /* When focus a sole frame and move horizontally, this sets on_p to 0
20456 causing a failure to erase prev cursor position. */
20457 if (area == TEXT_AREA
20458 && !row->full_width_p
20459 /* When drawing overlapping rows, only the glyph strings'
20460 foreground is drawn, which doesn't erase a cursor
20461 completely. */
20462 && !overlaps)
20463 {
20464 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20465 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20466 : (tail ? tail->x + tail->background_width : x));
20467 x0 -= area_left;
20468 x1 -= area_left;
20469
20470 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20471 row->y, MATRIX_ROW_BOTTOM_Y (row));
20472 }
20473 #endif
20474
20475 /* Value is the x-position up to which drawn, relative to AREA of W.
20476 This doesn't include parts drawn because of overhangs. */
20477 if (row->full_width_p)
20478 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20479 else
20480 x_reached -= area_left;
20481
20482 RELEASE_HDC (hdc, f);
20483
20484 return x_reached;
20485 }
20486
20487 /* Expand row matrix if too narrow. Don't expand if area
20488 is not present. */
20489
20490 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20491 { \
20492 if (!fonts_changed_p \
20493 && (it->glyph_row->glyphs[area] \
20494 < it->glyph_row->glyphs[area + 1])) \
20495 { \
20496 it->w->ncols_scale_factor++; \
20497 fonts_changed_p = 1; \
20498 } \
20499 }
20500
20501 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20502 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20503
20504 static INLINE void
20505 append_glyph (it)
20506 struct it *it;
20507 {
20508 struct glyph *glyph;
20509 enum glyph_row_area area = it->area;
20510
20511 xassert (it->glyph_row);
20512 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20513
20514 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20515 if (glyph < it->glyph_row->glyphs[area + 1])
20516 {
20517 glyph->charpos = CHARPOS (it->position);
20518 glyph->object = it->object;
20519 if (it->pixel_width > 0)
20520 {
20521 glyph->pixel_width = it->pixel_width;
20522 glyph->padding_p = 0;
20523 }
20524 else
20525 {
20526 /* Assure at least 1-pixel width. Otherwise, cursor can't
20527 be displayed correctly. */
20528 glyph->pixel_width = 1;
20529 glyph->padding_p = 1;
20530 }
20531 glyph->ascent = it->ascent;
20532 glyph->descent = it->descent;
20533 glyph->voffset = it->voffset;
20534 glyph->type = CHAR_GLYPH;
20535 glyph->avoid_cursor_p = it->avoid_cursor_p;
20536 glyph->multibyte_p = it->multibyte_p;
20537 glyph->left_box_line_p = it->start_of_box_run_p;
20538 glyph->right_box_line_p = it->end_of_box_run_p;
20539 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20540 || it->phys_descent > it->descent);
20541 glyph->glyph_not_available_p = it->glyph_not_available_p;
20542 glyph->face_id = it->face_id;
20543 glyph->u.ch = it->char_to_display;
20544 glyph->slice = null_glyph_slice;
20545 glyph->font_type = FONT_TYPE_UNKNOWN;
20546 ++it->glyph_row->used[area];
20547 }
20548 else
20549 IT_EXPAND_MATRIX_WIDTH (it, area);
20550 }
20551
20552 /* Store one glyph for the composition IT->cmp_it.id in
20553 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20554 non-null. */
20555
20556 static INLINE void
20557 append_composite_glyph (it)
20558 struct it *it;
20559 {
20560 struct glyph *glyph;
20561 enum glyph_row_area area = it->area;
20562
20563 xassert (it->glyph_row);
20564
20565 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20566 if (glyph < it->glyph_row->glyphs[area + 1])
20567 {
20568 glyph->charpos = CHARPOS (it->position);
20569 glyph->object = it->object;
20570 glyph->pixel_width = it->pixel_width;
20571 glyph->ascent = it->ascent;
20572 glyph->descent = it->descent;
20573 glyph->voffset = it->voffset;
20574 glyph->type = COMPOSITE_GLYPH;
20575 if (it->cmp_it.ch < 0)
20576 {
20577 glyph->u.cmp.automatic = 0;
20578 glyph->u.cmp.id = it->cmp_it.id;
20579 }
20580 else
20581 {
20582 glyph->u.cmp.automatic = 1;
20583 glyph->u.cmp.id = it->cmp_it.id;
20584 glyph->u.cmp.from = it->cmp_it.from;
20585 glyph->u.cmp.to = it->cmp_it.to - 1;
20586 }
20587 glyph->avoid_cursor_p = it->avoid_cursor_p;
20588 glyph->multibyte_p = it->multibyte_p;
20589 glyph->left_box_line_p = it->start_of_box_run_p;
20590 glyph->right_box_line_p = it->end_of_box_run_p;
20591 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20592 || it->phys_descent > it->descent);
20593 glyph->padding_p = 0;
20594 glyph->glyph_not_available_p = 0;
20595 glyph->face_id = it->face_id;
20596 glyph->slice = null_glyph_slice;
20597 glyph->font_type = FONT_TYPE_UNKNOWN;
20598 ++it->glyph_row->used[area];
20599 }
20600 else
20601 IT_EXPAND_MATRIX_WIDTH (it, area);
20602 }
20603
20604
20605 /* Change IT->ascent and IT->height according to the setting of
20606 IT->voffset. */
20607
20608 static INLINE void
20609 take_vertical_position_into_account (it)
20610 struct it *it;
20611 {
20612 if (it->voffset)
20613 {
20614 if (it->voffset < 0)
20615 /* Increase the ascent so that we can display the text higher
20616 in the line. */
20617 it->ascent -= it->voffset;
20618 else
20619 /* Increase the descent so that we can display the text lower
20620 in the line. */
20621 it->descent += it->voffset;
20622 }
20623 }
20624
20625
20626 /* Produce glyphs/get display metrics for the image IT is loaded with.
20627 See the description of struct display_iterator in dispextern.h for
20628 an overview of struct display_iterator. */
20629
20630 static void
20631 produce_image_glyph (it)
20632 struct it *it;
20633 {
20634 struct image *img;
20635 struct face *face;
20636 int glyph_ascent, crop;
20637 struct glyph_slice slice;
20638
20639 xassert (it->what == IT_IMAGE);
20640
20641 face = FACE_FROM_ID (it->f, it->face_id);
20642 xassert (face);
20643 /* Make sure X resources of the face is loaded. */
20644 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20645
20646 if (it->image_id < 0)
20647 {
20648 /* Fringe bitmap. */
20649 it->ascent = it->phys_ascent = 0;
20650 it->descent = it->phys_descent = 0;
20651 it->pixel_width = 0;
20652 it->nglyphs = 0;
20653 return;
20654 }
20655
20656 img = IMAGE_FROM_ID (it->f, it->image_id);
20657 xassert (img);
20658 /* Make sure X resources of the image is loaded. */
20659 prepare_image_for_display (it->f, img);
20660
20661 slice.x = slice.y = 0;
20662 slice.width = img->width;
20663 slice.height = img->height;
20664
20665 if (INTEGERP (it->slice.x))
20666 slice.x = XINT (it->slice.x);
20667 else if (FLOATP (it->slice.x))
20668 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20669
20670 if (INTEGERP (it->slice.y))
20671 slice.y = XINT (it->slice.y);
20672 else if (FLOATP (it->slice.y))
20673 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20674
20675 if (INTEGERP (it->slice.width))
20676 slice.width = XINT (it->slice.width);
20677 else if (FLOATP (it->slice.width))
20678 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20679
20680 if (INTEGERP (it->slice.height))
20681 slice.height = XINT (it->slice.height);
20682 else if (FLOATP (it->slice.height))
20683 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20684
20685 if (slice.x >= img->width)
20686 slice.x = img->width;
20687 if (slice.y >= img->height)
20688 slice.y = img->height;
20689 if (slice.x + slice.width >= img->width)
20690 slice.width = img->width - slice.x;
20691 if (slice.y + slice.height > img->height)
20692 slice.height = img->height - slice.y;
20693
20694 if (slice.width == 0 || slice.height == 0)
20695 return;
20696
20697 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20698
20699 it->descent = slice.height - glyph_ascent;
20700 if (slice.y == 0)
20701 it->descent += img->vmargin;
20702 if (slice.y + slice.height == img->height)
20703 it->descent += img->vmargin;
20704 it->phys_descent = it->descent;
20705
20706 it->pixel_width = slice.width;
20707 if (slice.x == 0)
20708 it->pixel_width += img->hmargin;
20709 if (slice.x + slice.width == img->width)
20710 it->pixel_width += img->hmargin;
20711
20712 /* It's quite possible for images to have an ascent greater than
20713 their height, so don't get confused in that case. */
20714 if (it->descent < 0)
20715 it->descent = 0;
20716
20717 it->nglyphs = 1;
20718
20719 if (face->box != FACE_NO_BOX)
20720 {
20721 if (face->box_line_width > 0)
20722 {
20723 if (slice.y == 0)
20724 it->ascent += face->box_line_width;
20725 if (slice.y + slice.height == img->height)
20726 it->descent += face->box_line_width;
20727 }
20728
20729 if (it->start_of_box_run_p && slice.x == 0)
20730 it->pixel_width += eabs (face->box_line_width);
20731 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20732 it->pixel_width += eabs (face->box_line_width);
20733 }
20734
20735 take_vertical_position_into_account (it);
20736
20737 /* Automatically crop wide image glyphs at right edge so we can
20738 draw the cursor on same display row. */
20739 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20740 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20741 {
20742 it->pixel_width -= crop;
20743 slice.width -= crop;
20744 }
20745
20746 if (it->glyph_row)
20747 {
20748 struct glyph *glyph;
20749 enum glyph_row_area area = it->area;
20750
20751 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20752 if (glyph < it->glyph_row->glyphs[area + 1])
20753 {
20754 glyph->charpos = CHARPOS (it->position);
20755 glyph->object = it->object;
20756 glyph->pixel_width = it->pixel_width;
20757 glyph->ascent = glyph_ascent;
20758 glyph->descent = it->descent;
20759 glyph->voffset = it->voffset;
20760 glyph->type = IMAGE_GLYPH;
20761 glyph->avoid_cursor_p = it->avoid_cursor_p;
20762 glyph->multibyte_p = it->multibyte_p;
20763 glyph->left_box_line_p = it->start_of_box_run_p;
20764 glyph->right_box_line_p = it->end_of_box_run_p;
20765 glyph->overlaps_vertically_p = 0;
20766 glyph->padding_p = 0;
20767 glyph->glyph_not_available_p = 0;
20768 glyph->face_id = it->face_id;
20769 glyph->u.img_id = img->id;
20770 glyph->slice = slice;
20771 glyph->font_type = FONT_TYPE_UNKNOWN;
20772 ++it->glyph_row->used[area];
20773 }
20774 else
20775 IT_EXPAND_MATRIX_WIDTH (it, area);
20776 }
20777 }
20778
20779
20780 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20781 of the glyph, WIDTH and HEIGHT are the width and height of the
20782 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20783
20784 static void
20785 append_stretch_glyph (it, object, width, height, ascent)
20786 struct it *it;
20787 Lisp_Object object;
20788 int width, height;
20789 int ascent;
20790 {
20791 struct glyph *glyph;
20792 enum glyph_row_area area = it->area;
20793
20794 xassert (ascent >= 0 && ascent <= height);
20795
20796 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20797 if (glyph < it->glyph_row->glyphs[area + 1])
20798 {
20799 glyph->charpos = CHARPOS (it->position);
20800 glyph->object = object;
20801 glyph->pixel_width = width;
20802 glyph->ascent = ascent;
20803 glyph->descent = height - ascent;
20804 glyph->voffset = it->voffset;
20805 glyph->type = STRETCH_GLYPH;
20806 glyph->avoid_cursor_p = it->avoid_cursor_p;
20807 glyph->multibyte_p = it->multibyte_p;
20808 glyph->left_box_line_p = it->start_of_box_run_p;
20809 glyph->right_box_line_p = it->end_of_box_run_p;
20810 glyph->overlaps_vertically_p = 0;
20811 glyph->padding_p = 0;
20812 glyph->glyph_not_available_p = 0;
20813 glyph->face_id = it->face_id;
20814 glyph->u.stretch.ascent = ascent;
20815 glyph->u.stretch.height = height;
20816 glyph->slice = null_glyph_slice;
20817 glyph->font_type = FONT_TYPE_UNKNOWN;
20818 ++it->glyph_row->used[area];
20819 }
20820 else
20821 IT_EXPAND_MATRIX_WIDTH (it, area);
20822 }
20823
20824
20825 /* Produce a stretch glyph for iterator IT. IT->object is the value
20826 of the glyph property displayed. The value must be a list
20827 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20828 being recognized:
20829
20830 1. `:width WIDTH' specifies that the space should be WIDTH *
20831 canonical char width wide. WIDTH may be an integer or floating
20832 point number.
20833
20834 2. `:relative-width FACTOR' specifies that the width of the stretch
20835 should be computed from the width of the first character having the
20836 `glyph' property, and should be FACTOR times that width.
20837
20838 3. `:align-to HPOS' specifies that the space should be wide enough
20839 to reach HPOS, a value in canonical character units.
20840
20841 Exactly one of the above pairs must be present.
20842
20843 4. `:height HEIGHT' specifies that the height of the stretch produced
20844 should be HEIGHT, measured in canonical character units.
20845
20846 5. `:relative-height FACTOR' specifies that the height of the
20847 stretch should be FACTOR times the height of the characters having
20848 the glyph property.
20849
20850 Either none or exactly one of 4 or 5 must be present.
20851
20852 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20853 of the stretch should be used for the ascent of the stretch.
20854 ASCENT must be in the range 0 <= ASCENT <= 100. */
20855
20856 static void
20857 produce_stretch_glyph (it)
20858 struct it *it;
20859 {
20860 /* (space :width WIDTH :height HEIGHT ...) */
20861 Lisp_Object prop, plist;
20862 int width = 0, height = 0, align_to = -1;
20863 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20864 int ascent = 0;
20865 double tem;
20866 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20867 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20868
20869 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20870
20871 /* List should start with `space'. */
20872 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20873 plist = XCDR (it->object);
20874
20875 /* Compute the width of the stretch. */
20876 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20877 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20878 {
20879 /* Absolute width `:width WIDTH' specified and valid. */
20880 zero_width_ok_p = 1;
20881 width = (int)tem;
20882 }
20883 else if (prop = Fplist_get (plist, QCrelative_width),
20884 NUMVAL (prop) > 0)
20885 {
20886 /* Relative width `:relative-width FACTOR' specified and valid.
20887 Compute the width of the characters having the `glyph'
20888 property. */
20889 struct it it2;
20890 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20891
20892 it2 = *it;
20893 if (it->multibyte_p)
20894 {
20895 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20896 - IT_BYTEPOS (*it));
20897 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20898 }
20899 else
20900 it2.c = *p, it2.len = 1;
20901
20902 it2.glyph_row = NULL;
20903 it2.what = IT_CHARACTER;
20904 x_produce_glyphs (&it2);
20905 width = NUMVAL (prop) * it2.pixel_width;
20906 }
20907 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20908 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20909 {
20910 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20911 align_to = (align_to < 0
20912 ? 0
20913 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20914 else if (align_to < 0)
20915 align_to = window_box_left_offset (it->w, TEXT_AREA);
20916 width = max (0, (int)tem + align_to - it->current_x);
20917 zero_width_ok_p = 1;
20918 }
20919 else
20920 /* Nothing specified -> width defaults to canonical char width. */
20921 width = FRAME_COLUMN_WIDTH (it->f);
20922
20923 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20924 width = 1;
20925
20926 /* Compute height. */
20927 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20928 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20929 {
20930 height = (int)tem;
20931 zero_height_ok_p = 1;
20932 }
20933 else if (prop = Fplist_get (plist, QCrelative_height),
20934 NUMVAL (prop) > 0)
20935 height = FONT_HEIGHT (font) * NUMVAL (prop);
20936 else
20937 height = FONT_HEIGHT (font);
20938
20939 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20940 height = 1;
20941
20942 /* Compute percentage of height used for ascent. If
20943 `:ascent ASCENT' is present and valid, use that. Otherwise,
20944 derive the ascent from the font in use. */
20945 if (prop = Fplist_get (plist, QCascent),
20946 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20947 ascent = height * NUMVAL (prop) / 100.0;
20948 else if (!NILP (prop)
20949 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20950 ascent = min (max (0, (int)tem), height);
20951 else
20952 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20953
20954 if (width > 0 && it->line_wrap != TRUNCATE
20955 && it->current_x + width > it->last_visible_x)
20956 width = it->last_visible_x - it->current_x - 1;
20957
20958 if (width > 0 && height > 0 && it->glyph_row)
20959 {
20960 Lisp_Object object = it->stack[it->sp - 1].string;
20961 if (!STRINGP (object))
20962 object = it->w->buffer;
20963 append_stretch_glyph (it, object, width, height, ascent);
20964 }
20965
20966 it->pixel_width = width;
20967 it->ascent = it->phys_ascent = ascent;
20968 it->descent = it->phys_descent = height - it->ascent;
20969 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20970
20971 take_vertical_position_into_account (it);
20972 }
20973
20974 /* Calculate line-height and line-spacing properties.
20975 An integer value specifies explicit pixel value.
20976 A float value specifies relative value to current face height.
20977 A cons (float . face-name) specifies relative value to
20978 height of specified face font.
20979
20980 Returns height in pixels, or nil. */
20981
20982
20983 static Lisp_Object
20984 calc_line_height_property (it, val, font, boff, override)
20985 struct it *it;
20986 Lisp_Object val;
20987 struct font *font;
20988 int boff, override;
20989 {
20990 Lisp_Object face_name = Qnil;
20991 int ascent, descent, height;
20992
20993 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20994 return val;
20995
20996 if (CONSP (val))
20997 {
20998 face_name = XCAR (val);
20999 val = XCDR (val);
21000 if (!NUMBERP (val))
21001 val = make_number (1);
21002 if (NILP (face_name))
21003 {
21004 height = it->ascent + it->descent;
21005 goto scale;
21006 }
21007 }
21008
21009 if (NILP (face_name))
21010 {
21011 font = FRAME_FONT (it->f);
21012 boff = FRAME_BASELINE_OFFSET (it->f);
21013 }
21014 else if (EQ (face_name, Qt))
21015 {
21016 override = 0;
21017 }
21018 else
21019 {
21020 int face_id;
21021 struct face *face;
21022
21023 face_id = lookup_named_face (it->f, face_name, 0);
21024 if (face_id < 0)
21025 return make_number (-1);
21026
21027 face = FACE_FROM_ID (it->f, face_id);
21028 font = face->font;
21029 if (font == NULL)
21030 return make_number (-1);
21031 boff = font->baseline_offset;
21032 if (font->vertical_centering)
21033 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21034 }
21035
21036 ascent = FONT_BASE (font) + boff;
21037 descent = FONT_DESCENT (font) - boff;
21038
21039 if (override)
21040 {
21041 it->override_ascent = ascent;
21042 it->override_descent = descent;
21043 it->override_boff = boff;
21044 }
21045
21046 height = ascent + descent;
21047
21048 scale:
21049 if (FLOATP (val))
21050 height = (int)(XFLOAT_DATA (val) * height);
21051 else if (INTEGERP (val))
21052 height *= XINT (val);
21053
21054 return make_number (height);
21055 }
21056
21057
21058 /* RIF:
21059 Produce glyphs/get display metrics for the display element IT is
21060 loaded with. See the description of struct it in dispextern.h
21061 for an overview of struct it. */
21062
21063 void
21064 x_produce_glyphs (it)
21065 struct it *it;
21066 {
21067 int extra_line_spacing = it->extra_line_spacing;
21068
21069 it->glyph_not_available_p = 0;
21070
21071 if (it->what == IT_CHARACTER)
21072 {
21073 XChar2b char2b;
21074 struct font *font;
21075 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21076 struct font_metrics *pcm;
21077 int font_not_found_p;
21078 int boff; /* baseline offset */
21079 /* We may change it->multibyte_p upon unibyte<->multibyte
21080 conversion. So, save the current value now and restore it
21081 later.
21082
21083 Note: It seems that we don't have to record multibyte_p in
21084 struct glyph because the character code itself tells if or
21085 not the character is multibyte. Thus, in the future, we must
21086 consider eliminating the field `multibyte_p' in the struct
21087 glyph. */
21088 int saved_multibyte_p = it->multibyte_p;
21089
21090 /* Maybe translate single-byte characters to multibyte, or the
21091 other way. */
21092 it->char_to_display = it->c;
21093 if (!ASCII_BYTE_P (it->c)
21094 && ! it->multibyte_p)
21095 {
21096 if (SINGLE_BYTE_CHAR_P (it->c)
21097 && unibyte_display_via_language_environment)
21098 {
21099 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21100
21101 /* get_next_display_element assures that this decoding
21102 never fails. */
21103 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21104 it->multibyte_p = 1;
21105 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21106 -1, Qnil);
21107 face = FACE_FROM_ID (it->f, it->face_id);
21108 }
21109 }
21110
21111 /* Get font to use. Encode IT->char_to_display. */
21112 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21113 &char2b, it->multibyte_p, 0);
21114 font = face->font;
21115
21116 /* When no suitable font found, use the default font. */
21117 font_not_found_p = font == NULL;
21118 if (font_not_found_p)
21119 {
21120 font = FRAME_FONT (it->f);
21121 boff = FRAME_BASELINE_OFFSET (it->f);
21122 }
21123 else
21124 {
21125 boff = font->baseline_offset;
21126 if (font->vertical_centering)
21127 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21128 }
21129
21130 if (it->char_to_display >= ' '
21131 && (!it->multibyte_p || it->char_to_display < 128))
21132 {
21133 /* Either unibyte or ASCII. */
21134 int stretched_p;
21135
21136 it->nglyphs = 1;
21137
21138 pcm = get_per_char_metric (it->f, font, &char2b);
21139
21140 if (it->override_ascent >= 0)
21141 {
21142 it->ascent = it->override_ascent;
21143 it->descent = it->override_descent;
21144 boff = it->override_boff;
21145 }
21146 else
21147 {
21148 it->ascent = FONT_BASE (font) + boff;
21149 it->descent = FONT_DESCENT (font) - boff;
21150 }
21151
21152 if (pcm)
21153 {
21154 it->phys_ascent = pcm->ascent + boff;
21155 it->phys_descent = pcm->descent - boff;
21156 it->pixel_width = pcm->width;
21157 }
21158 else
21159 {
21160 it->glyph_not_available_p = 1;
21161 it->phys_ascent = it->ascent;
21162 it->phys_descent = it->descent;
21163 it->pixel_width = FONT_WIDTH (font);
21164 }
21165
21166 if (it->constrain_row_ascent_descent_p)
21167 {
21168 if (it->descent > it->max_descent)
21169 {
21170 it->ascent += it->descent - it->max_descent;
21171 it->descent = it->max_descent;
21172 }
21173 if (it->ascent > it->max_ascent)
21174 {
21175 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21176 it->ascent = it->max_ascent;
21177 }
21178 it->phys_ascent = min (it->phys_ascent, it->ascent);
21179 it->phys_descent = min (it->phys_descent, it->descent);
21180 extra_line_spacing = 0;
21181 }
21182
21183 /* If this is a space inside a region of text with
21184 `space-width' property, change its width. */
21185 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21186 if (stretched_p)
21187 it->pixel_width *= XFLOATINT (it->space_width);
21188
21189 /* If face has a box, add the box thickness to the character
21190 height. If character has a box line to the left and/or
21191 right, add the box line width to the character's width. */
21192 if (face->box != FACE_NO_BOX)
21193 {
21194 int thick = face->box_line_width;
21195
21196 if (thick > 0)
21197 {
21198 it->ascent += thick;
21199 it->descent += thick;
21200 }
21201 else
21202 thick = -thick;
21203
21204 if (it->start_of_box_run_p)
21205 it->pixel_width += thick;
21206 if (it->end_of_box_run_p)
21207 it->pixel_width += thick;
21208 }
21209
21210 /* If face has an overline, add the height of the overline
21211 (1 pixel) and a 1 pixel margin to the character height. */
21212 if (face->overline_p)
21213 it->ascent += overline_margin;
21214
21215 if (it->constrain_row_ascent_descent_p)
21216 {
21217 if (it->ascent > it->max_ascent)
21218 it->ascent = it->max_ascent;
21219 if (it->descent > it->max_descent)
21220 it->descent = it->max_descent;
21221 }
21222
21223 take_vertical_position_into_account (it);
21224
21225 /* If we have to actually produce glyphs, do it. */
21226 if (it->glyph_row)
21227 {
21228 if (stretched_p)
21229 {
21230 /* Translate a space with a `space-width' property
21231 into a stretch glyph. */
21232 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21233 / FONT_HEIGHT (font));
21234 append_stretch_glyph (it, it->object, it->pixel_width,
21235 it->ascent + it->descent, ascent);
21236 }
21237 else
21238 append_glyph (it);
21239
21240 /* If characters with lbearing or rbearing are displayed
21241 in this line, record that fact in a flag of the
21242 glyph row. This is used to optimize X output code. */
21243 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21244 it->glyph_row->contains_overlapping_glyphs_p = 1;
21245 }
21246 if (! stretched_p && it->pixel_width == 0)
21247 /* We assure that all visible glyphs have at least 1-pixel
21248 width. */
21249 it->pixel_width = 1;
21250 }
21251 else if (it->char_to_display == '\n')
21252 {
21253 /* A newline has no width but we need the height of the line.
21254 But if previous part of the line set a height, don't
21255 increase that height */
21256
21257 Lisp_Object height;
21258 Lisp_Object total_height = Qnil;
21259
21260 it->override_ascent = -1;
21261 it->pixel_width = 0;
21262 it->nglyphs = 0;
21263
21264 height = get_it_property(it, Qline_height);
21265 /* Split (line-height total-height) list */
21266 if (CONSP (height)
21267 && CONSP (XCDR (height))
21268 && NILP (XCDR (XCDR (height))))
21269 {
21270 total_height = XCAR (XCDR (height));
21271 height = XCAR (height);
21272 }
21273 height = calc_line_height_property(it, height, font, boff, 1);
21274
21275 if (it->override_ascent >= 0)
21276 {
21277 it->ascent = it->override_ascent;
21278 it->descent = it->override_descent;
21279 boff = it->override_boff;
21280 }
21281 else
21282 {
21283 it->ascent = FONT_BASE (font) + boff;
21284 it->descent = FONT_DESCENT (font) - boff;
21285 }
21286
21287 if (EQ (height, Qt))
21288 {
21289 if (it->descent > it->max_descent)
21290 {
21291 it->ascent += it->descent - it->max_descent;
21292 it->descent = it->max_descent;
21293 }
21294 if (it->ascent > it->max_ascent)
21295 {
21296 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21297 it->ascent = it->max_ascent;
21298 }
21299 it->phys_ascent = min (it->phys_ascent, it->ascent);
21300 it->phys_descent = min (it->phys_descent, it->descent);
21301 it->constrain_row_ascent_descent_p = 1;
21302 extra_line_spacing = 0;
21303 }
21304 else
21305 {
21306 Lisp_Object spacing;
21307
21308 it->phys_ascent = it->ascent;
21309 it->phys_descent = it->descent;
21310
21311 if ((it->max_ascent > 0 || it->max_descent > 0)
21312 && face->box != FACE_NO_BOX
21313 && face->box_line_width > 0)
21314 {
21315 it->ascent += face->box_line_width;
21316 it->descent += face->box_line_width;
21317 }
21318 if (!NILP (height)
21319 && XINT (height) > it->ascent + it->descent)
21320 it->ascent = XINT (height) - it->descent;
21321
21322 if (!NILP (total_height))
21323 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21324 else
21325 {
21326 spacing = get_it_property(it, Qline_spacing);
21327 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21328 }
21329 if (INTEGERP (spacing))
21330 {
21331 extra_line_spacing = XINT (spacing);
21332 if (!NILP (total_height))
21333 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21334 }
21335 }
21336 }
21337 else if (it->char_to_display == '\t')
21338 {
21339 if (font->space_width > 0)
21340 {
21341 int tab_width = it->tab_width * font->space_width;
21342 int x = it->current_x + it->continuation_lines_width;
21343 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21344
21345 /* If the distance from the current position to the next tab
21346 stop is less than a space character width, use the
21347 tab stop after that. */
21348 if (next_tab_x - x < font->space_width)
21349 next_tab_x += tab_width;
21350
21351 it->pixel_width = next_tab_x - x;
21352 it->nglyphs = 1;
21353 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21354 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21355
21356 if (it->glyph_row)
21357 {
21358 append_stretch_glyph (it, it->object, it->pixel_width,
21359 it->ascent + it->descent, it->ascent);
21360 }
21361 }
21362 else
21363 {
21364 it->pixel_width = 0;
21365 it->nglyphs = 1;
21366 }
21367 }
21368 else
21369 {
21370 /* A multi-byte character. Assume that the display width of the
21371 character is the width of the character multiplied by the
21372 width of the font. */
21373
21374 /* If we found a font, this font should give us the right
21375 metrics. If we didn't find a font, use the frame's
21376 default font and calculate the width of the character by
21377 multiplying the width of font by the width of the
21378 character. */
21379
21380 pcm = get_per_char_metric (it->f, font, &char2b);
21381
21382 if (font_not_found_p || !pcm)
21383 {
21384 int char_width = CHAR_WIDTH (it->char_to_display);
21385
21386 if (char_width == 0)
21387 /* This is a non spacing character. But, as we are
21388 going to display an empty box, the box must occupy
21389 at least one column. */
21390 char_width = 1;
21391 it->glyph_not_available_p = 1;
21392 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21393 it->phys_ascent = FONT_BASE (font) + boff;
21394 it->phys_descent = FONT_DESCENT (font) - boff;
21395 }
21396 else
21397 {
21398 it->pixel_width = pcm->width;
21399 it->phys_ascent = pcm->ascent + boff;
21400 it->phys_descent = pcm->descent - boff;
21401 if (it->glyph_row
21402 && (pcm->lbearing < 0
21403 || pcm->rbearing > pcm->width))
21404 it->glyph_row->contains_overlapping_glyphs_p = 1;
21405 }
21406 it->nglyphs = 1;
21407 it->ascent = FONT_BASE (font) + boff;
21408 it->descent = FONT_DESCENT (font) - boff;
21409 if (face->box != FACE_NO_BOX)
21410 {
21411 int thick = face->box_line_width;
21412
21413 if (thick > 0)
21414 {
21415 it->ascent += thick;
21416 it->descent += thick;
21417 }
21418 else
21419 thick = - thick;
21420
21421 if (it->start_of_box_run_p)
21422 it->pixel_width += thick;
21423 if (it->end_of_box_run_p)
21424 it->pixel_width += thick;
21425 }
21426
21427 /* If face has an overline, add the height of the overline
21428 (1 pixel) and a 1 pixel margin to the character height. */
21429 if (face->overline_p)
21430 it->ascent += overline_margin;
21431
21432 take_vertical_position_into_account (it);
21433
21434 if (it->ascent < 0)
21435 it->ascent = 0;
21436 if (it->descent < 0)
21437 it->descent = 0;
21438
21439 if (it->glyph_row)
21440 append_glyph (it);
21441 if (it->pixel_width == 0)
21442 /* We assure that all visible glyphs have at least 1-pixel
21443 width. */
21444 it->pixel_width = 1;
21445 }
21446 it->multibyte_p = saved_multibyte_p;
21447 }
21448 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21449 {
21450 /* A static compositoin.
21451
21452 Note: A composition is represented as one glyph in the
21453 glyph matrix. There are no padding glyphs.
21454
21455 Important is that pixel_width, ascent, and descent are the
21456 values of what is drawn by draw_glyphs (i.e. the values of
21457 the overall glyphs composed). */
21458 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21459 int boff; /* baseline offset */
21460 struct composition *cmp = composition_table[it->cmp_it.id];
21461 int glyph_len = cmp->glyph_len;
21462 struct font *font = face->font;
21463
21464 it->nglyphs = 1;
21465
21466 /* If we have not yet calculated pixel size data of glyphs of
21467 the composition for the current face font, calculate them
21468 now. Theoretically, we have to check all fonts for the
21469 glyphs, but that requires much time and memory space. So,
21470 here we check only the font of the first glyph. This leads
21471 to incorrect display, but it's very rare, and C-l (recenter)
21472 can correct the display anyway. */
21473 if (! cmp->font || cmp->font != font)
21474 {
21475 /* Ascent and descent of the font of the first character
21476 of this composition (adjusted by baseline offset).
21477 Ascent and descent of overall glyphs should not be less
21478 than them respectively. */
21479 int font_ascent, font_descent, font_height;
21480 /* Bounding box of the overall glyphs. */
21481 int leftmost, rightmost, lowest, highest;
21482 int lbearing, rbearing;
21483 int i, width, ascent, descent;
21484 int left_padded = 0, right_padded = 0;
21485 int c;
21486 XChar2b char2b;
21487 struct font_metrics *pcm;
21488 int font_not_found_p;
21489 int pos;
21490
21491 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21492 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21493 break;
21494 if (glyph_len < cmp->glyph_len)
21495 right_padded = 1;
21496 for (i = 0; i < glyph_len; i++)
21497 {
21498 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21499 break;
21500 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21501 }
21502 if (i > 0)
21503 left_padded = 1;
21504
21505 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21506 : IT_CHARPOS (*it));
21507 /* When no suitable font found, use the default font. */
21508 font_not_found_p = font == NULL;
21509 if (font_not_found_p)
21510 {
21511 face = face->ascii_face;
21512 font = face->font;
21513 }
21514 boff = font->baseline_offset;
21515 if (font->vertical_centering)
21516 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21517 font_ascent = FONT_BASE (font) + boff;
21518 font_descent = FONT_DESCENT (font) - boff;
21519 font_height = FONT_HEIGHT (font);
21520
21521 cmp->font = (void *) font;
21522
21523 pcm = NULL;
21524 if (! font_not_found_p)
21525 {
21526 get_char_face_and_encoding (it->f, c, it->face_id,
21527 &char2b, it->multibyte_p, 0);
21528 pcm = get_per_char_metric (it->f, font, &char2b);
21529 }
21530
21531 /* Initialize the bounding box. */
21532 if (pcm)
21533 {
21534 width = pcm->width;
21535 ascent = pcm->ascent;
21536 descent = pcm->descent;
21537 lbearing = pcm->lbearing;
21538 rbearing = pcm->rbearing;
21539 }
21540 else
21541 {
21542 width = FONT_WIDTH (font);
21543 ascent = FONT_BASE (font);
21544 descent = FONT_DESCENT (font);
21545 lbearing = 0;
21546 rbearing = width;
21547 }
21548
21549 rightmost = width;
21550 leftmost = 0;
21551 lowest = - descent + boff;
21552 highest = ascent + boff;
21553
21554 if (! font_not_found_p
21555 && font->default_ascent
21556 && CHAR_TABLE_P (Vuse_default_ascent)
21557 && !NILP (Faref (Vuse_default_ascent,
21558 make_number (it->char_to_display))))
21559 highest = font->default_ascent + boff;
21560
21561 /* Draw the first glyph at the normal position. It may be
21562 shifted to right later if some other glyphs are drawn
21563 at the left. */
21564 cmp->offsets[i * 2] = 0;
21565 cmp->offsets[i * 2 + 1] = boff;
21566 cmp->lbearing = lbearing;
21567 cmp->rbearing = rbearing;
21568
21569 /* Set cmp->offsets for the remaining glyphs. */
21570 for (i++; i < glyph_len; i++)
21571 {
21572 int left, right, btm, top;
21573 int ch = COMPOSITION_GLYPH (cmp, i);
21574 int face_id;
21575 struct face *this_face;
21576 int this_boff;
21577
21578 if (ch == '\t')
21579 ch = ' ';
21580 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21581 this_face = FACE_FROM_ID (it->f, face_id);
21582 font = this_face->font;
21583
21584 if (font == NULL)
21585 pcm = NULL;
21586 else
21587 {
21588 this_boff = font->baseline_offset;
21589 if (font->vertical_centering)
21590 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21591 get_char_face_and_encoding (it->f, ch, face_id,
21592 &char2b, it->multibyte_p, 0);
21593 pcm = get_per_char_metric (it->f, font, &char2b);
21594 }
21595 if (! pcm)
21596 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21597 else
21598 {
21599 width = pcm->width;
21600 ascent = pcm->ascent;
21601 descent = pcm->descent;
21602 lbearing = pcm->lbearing;
21603 rbearing = pcm->rbearing;
21604 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21605 {
21606 /* Relative composition with or without
21607 alternate chars. */
21608 left = (leftmost + rightmost - width) / 2;
21609 btm = - descent + boff;
21610 if (font->relative_compose
21611 && (! CHAR_TABLE_P (Vignore_relative_composition)
21612 || NILP (Faref (Vignore_relative_composition,
21613 make_number (ch)))))
21614 {
21615
21616 if (- descent >= font->relative_compose)
21617 /* One extra pixel between two glyphs. */
21618 btm = highest + 1;
21619 else if (ascent <= 0)
21620 /* One extra pixel between two glyphs. */
21621 btm = lowest - 1 - ascent - descent;
21622 }
21623 }
21624 else
21625 {
21626 /* A composition rule is specified by an integer
21627 value that encodes global and new reference
21628 points (GREF and NREF). GREF and NREF are
21629 specified by numbers as below:
21630
21631 0---1---2 -- ascent
21632 | |
21633 | |
21634 | |
21635 9--10--11 -- center
21636 | |
21637 ---3---4---5--- baseline
21638 | |
21639 6---7---8 -- descent
21640 */
21641 int rule = COMPOSITION_RULE (cmp, i);
21642 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21643
21644 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21645 grefx = gref % 3, nrefx = nref % 3;
21646 grefy = gref / 3, nrefy = nref / 3;
21647 if (xoff)
21648 xoff = font_height * (xoff - 128) / 256;
21649 if (yoff)
21650 yoff = font_height * (yoff - 128) / 256;
21651
21652 left = (leftmost
21653 + grefx * (rightmost - leftmost) / 2
21654 - nrefx * width / 2
21655 + xoff);
21656
21657 btm = ((grefy == 0 ? highest
21658 : grefy == 1 ? 0
21659 : grefy == 2 ? lowest
21660 : (highest + lowest) / 2)
21661 - (nrefy == 0 ? ascent + descent
21662 : nrefy == 1 ? descent - boff
21663 : nrefy == 2 ? 0
21664 : (ascent + descent) / 2)
21665 + yoff);
21666 }
21667
21668 cmp->offsets[i * 2] = left;
21669 cmp->offsets[i * 2 + 1] = btm + descent;
21670
21671 /* Update the bounding box of the overall glyphs. */
21672 if (width > 0)
21673 {
21674 right = left + width;
21675 if (left < leftmost)
21676 leftmost = left;
21677 if (right > rightmost)
21678 rightmost = right;
21679 }
21680 top = btm + descent + ascent;
21681 if (top > highest)
21682 highest = top;
21683 if (btm < lowest)
21684 lowest = btm;
21685
21686 if (cmp->lbearing > left + lbearing)
21687 cmp->lbearing = left + lbearing;
21688 if (cmp->rbearing < left + rbearing)
21689 cmp->rbearing = left + rbearing;
21690 }
21691 }
21692
21693 /* If there are glyphs whose x-offsets are negative,
21694 shift all glyphs to the right and make all x-offsets
21695 non-negative. */
21696 if (leftmost < 0)
21697 {
21698 for (i = 0; i < cmp->glyph_len; i++)
21699 cmp->offsets[i * 2] -= leftmost;
21700 rightmost -= leftmost;
21701 cmp->lbearing -= leftmost;
21702 cmp->rbearing -= leftmost;
21703 }
21704
21705 if (left_padded && cmp->lbearing < 0)
21706 {
21707 for (i = 0; i < cmp->glyph_len; i++)
21708 cmp->offsets[i * 2] -= cmp->lbearing;
21709 rightmost -= cmp->lbearing;
21710 cmp->rbearing -= cmp->lbearing;
21711 cmp->lbearing = 0;
21712 }
21713 if (right_padded && rightmost < cmp->rbearing)
21714 {
21715 rightmost = cmp->rbearing;
21716 }
21717
21718 cmp->pixel_width = rightmost;
21719 cmp->ascent = highest;
21720 cmp->descent = - lowest;
21721 if (cmp->ascent < font_ascent)
21722 cmp->ascent = font_ascent;
21723 if (cmp->descent < font_descent)
21724 cmp->descent = font_descent;
21725 }
21726
21727 if (it->glyph_row
21728 && (cmp->lbearing < 0
21729 || cmp->rbearing > cmp->pixel_width))
21730 it->glyph_row->contains_overlapping_glyphs_p = 1;
21731
21732 it->pixel_width = cmp->pixel_width;
21733 it->ascent = it->phys_ascent = cmp->ascent;
21734 it->descent = it->phys_descent = cmp->descent;
21735 if (face->box != FACE_NO_BOX)
21736 {
21737 int thick = face->box_line_width;
21738
21739 if (thick > 0)
21740 {
21741 it->ascent += thick;
21742 it->descent += thick;
21743 }
21744 else
21745 thick = - thick;
21746
21747 if (it->start_of_box_run_p)
21748 it->pixel_width += thick;
21749 if (it->end_of_box_run_p)
21750 it->pixel_width += thick;
21751 }
21752
21753 /* If face has an overline, add the height of the overline
21754 (1 pixel) and a 1 pixel margin to the character height. */
21755 if (face->overline_p)
21756 it->ascent += overline_margin;
21757
21758 take_vertical_position_into_account (it);
21759 if (it->ascent < 0)
21760 it->ascent = 0;
21761 if (it->descent < 0)
21762 it->descent = 0;
21763
21764 if (it->glyph_row)
21765 append_composite_glyph (it);
21766 }
21767 else if (it->what == IT_COMPOSITION)
21768 {
21769 /* A dynamic (automatic) composition. */
21770 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21771 Lisp_Object gstring;
21772 struct font_metrics metrics;
21773
21774 gstring = composition_gstring_from_id (it->cmp_it.id);
21775 it->pixel_width
21776 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21777 &metrics);
21778 if (it->glyph_row
21779 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21780 it->glyph_row->contains_overlapping_glyphs_p = 1;
21781 it->ascent = it->phys_ascent = metrics.ascent;
21782 it->descent = it->phys_descent = metrics.descent;
21783 if (face->box != FACE_NO_BOX)
21784 {
21785 int thick = face->box_line_width;
21786
21787 if (thick > 0)
21788 {
21789 it->ascent += thick;
21790 it->descent += thick;
21791 }
21792 else
21793 thick = - thick;
21794
21795 if (it->start_of_box_run_p)
21796 it->pixel_width += thick;
21797 if (it->end_of_box_run_p)
21798 it->pixel_width += thick;
21799 }
21800 /* If face has an overline, add the height of the overline
21801 (1 pixel) and a 1 pixel margin to the character height. */
21802 if (face->overline_p)
21803 it->ascent += overline_margin;
21804 take_vertical_position_into_account (it);
21805 if (it->ascent < 0)
21806 it->ascent = 0;
21807 if (it->descent < 0)
21808 it->descent = 0;
21809
21810 if (it->glyph_row)
21811 append_composite_glyph (it);
21812 }
21813 else if (it->what == IT_IMAGE)
21814 produce_image_glyph (it);
21815 else if (it->what == IT_STRETCH)
21816 produce_stretch_glyph (it);
21817
21818 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21819 because this isn't true for images with `:ascent 100'. */
21820 xassert (it->ascent >= 0 && it->descent >= 0);
21821 if (it->area == TEXT_AREA)
21822 it->current_x += it->pixel_width;
21823
21824 if (extra_line_spacing > 0)
21825 {
21826 it->descent += extra_line_spacing;
21827 if (extra_line_spacing > it->max_extra_line_spacing)
21828 it->max_extra_line_spacing = extra_line_spacing;
21829 }
21830
21831 it->max_ascent = max (it->max_ascent, it->ascent);
21832 it->max_descent = max (it->max_descent, it->descent);
21833 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21834 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21835 }
21836
21837 /* EXPORT for RIF:
21838 Output LEN glyphs starting at START at the nominal cursor position.
21839 Advance the nominal cursor over the text. The global variable
21840 updated_window contains the window being updated, updated_row is
21841 the glyph row being updated, and updated_area is the area of that
21842 row being updated. */
21843
21844 void
21845 x_write_glyphs (start, len)
21846 struct glyph *start;
21847 int len;
21848 {
21849 int x, hpos;
21850
21851 xassert (updated_window && updated_row);
21852 BLOCK_INPUT;
21853
21854 /* Write glyphs. */
21855
21856 hpos = start - updated_row->glyphs[updated_area];
21857 x = draw_glyphs (updated_window, output_cursor.x,
21858 updated_row, updated_area,
21859 hpos, hpos + len,
21860 DRAW_NORMAL_TEXT, 0);
21861
21862 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21863 if (updated_area == TEXT_AREA
21864 && updated_window->phys_cursor_on_p
21865 && updated_window->phys_cursor.vpos == output_cursor.vpos
21866 && updated_window->phys_cursor.hpos >= hpos
21867 && updated_window->phys_cursor.hpos < hpos + len)
21868 updated_window->phys_cursor_on_p = 0;
21869
21870 UNBLOCK_INPUT;
21871
21872 /* Advance the output cursor. */
21873 output_cursor.hpos += len;
21874 output_cursor.x = x;
21875 }
21876
21877
21878 /* EXPORT for RIF:
21879 Insert LEN glyphs from START at the nominal cursor position. */
21880
21881 void
21882 x_insert_glyphs (start, len)
21883 struct glyph *start;
21884 int len;
21885 {
21886 struct frame *f;
21887 struct window *w;
21888 int line_height, shift_by_width, shifted_region_width;
21889 struct glyph_row *row;
21890 struct glyph *glyph;
21891 int frame_x, frame_y;
21892 EMACS_INT hpos;
21893
21894 xassert (updated_window && updated_row);
21895 BLOCK_INPUT;
21896 w = updated_window;
21897 f = XFRAME (WINDOW_FRAME (w));
21898
21899 /* Get the height of the line we are in. */
21900 row = updated_row;
21901 line_height = row->height;
21902
21903 /* Get the width of the glyphs to insert. */
21904 shift_by_width = 0;
21905 for (glyph = start; glyph < start + len; ++glyph)
21906 shift_by_width += glyph->pixel_width;
21907
21908 /* Get the width of the region to shift right. */
21909 shifted_region_width = (window_box_width (w, updated_area)
21910 - output_cursor.x
21911 - shift_by_width);
21912
21913 /* Shift right. */
21914 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21915 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21916
21917 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21918 line_height, shift_by_width);
21919
21920 /* Write the glyphs. */
21921 hpos = start - row->glyphs[updated_area];
21922 draw_glyphs (w, output_cursor.x, row, updated_area,
21923 hpos, hpos + len,
21924 DRAW_NORMAL_TEXT, 0);
21925
21926 /* Advance the output cursor. */
21927 output_cursor.hpos += len;
21928 output_cursor.x += shift_by_width;
21929 UNBLOCK_INPUT;
21930 }
21931
21932
21933 /* EXPORT for RIF:
21934 Erase the current text line from the nominal cursor position
21935 (inclusive) to pixel column TO_X (exclusive). The idea is that
21936 everything from TO_X onward is already erased.
21937
21938 TO_X is a pixel position relative to updated_area of
21939 updated_window. TO_X == -1 means clear to the end of this area. */
21940
21941 void
21942 x_clear_end_of_line (to_x)
21943 int to_x;
21944 {
21945 struct frame *f;
21946 struct window *w = updated_window;
21947 int max_x, min_y, max_y;
21948 int from_x, from_y, to_y;
21949
21950 xassert (updated_window && updated_row);
21951 f = XFRAME (w->frame);
21952
21953 if (updated_row->full_width_p)
21954 max_x = WINDOW_TOTAL_WIDTH (w);
21955 else
21956 max_x = window_box_width (w, updated_area);
21957 max_y = window_text_bottom_y (w);
21958
21959 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21960 of window. For TO_X > 0, truncate to end of drawing area. */
21961 if (to_x == 0)
21962 return;
21963 else if (to_x < 0)
21964 to_x = max_x;
21965 else
21966 to_x = min (to_x, max_x);
21967
21968 to_y = min (max_y, output_cursor.y + updated_row->height);
21969
21970 /* Notice if the cursor will be cleared by this operation. */
21971 if (!updated_row->full_width_p)
21972 notice_overwritten_cursor (w, updated_area,
21973 output_cursor.x, -1,
21974 updated_row->y,
21975 MATRIX_ROW_BOTTOM_Y (updated_row));
21976
21977 from_x = output_cursor.x;
21978
21979 /* Translate to frame coordinates. */
21980 if (updated_row->full_width_p)
21981 {
21982 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21983 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21984 }
21985 else
21986 {
21987 int area_left = window_box_left (w, updated_area);
21988 from_x += area_left;
21989 to_x += area_left;
21990 }
21991
21992 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21993 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21994 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21995
21996 /* Prevent inadvertently clearing to end of the X window. */
21997 if (to_x > from_x && to_y > from_y)
21998 {
21999 BLOCK_INPUT;
22000 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22001 to_x - from_x, to_y - from_y);
22002 UNBLOCK_INPUT;
22003 }
22004 }
22005
22006 #endif /* HAVE_WINDOW_SYSTEM */
22007
22008
22009 \f
22010 /***********************************************************************
22011 Cursor types
22012 ***********************************************************************/
22013
22014 /* Value is the internal representation of the specified cursor type
22015 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22016 of the bar cursor. */
22017
22018 static enum text_cursor_kinds
22019 get_specified_cursor_type (arg, width)
22020 Lisp_Object arg;
22021 int *width;
22022 {
22023 enum text_cursor_kinds type;
22024
22025 if (NILP (arg))
22026 return NO_CURSOR;
22027
22028 if (EQ (arg, Qbox))
22029 return FILLED_BOX_CURSOR;
22030
22031 if (EQ (arg, Qhollow))
22032 return HOLLOW_BOX_CURSOR;
22033
22034 if (EQ (arg, Qbar))
22035 {
22036 *width = 2;
22037 return BAR_CURSOR;
22038 }
22039
22040 if (CONSP (arg)
22041 && EQ (XCAR (arg), Qbar)
22042 && INTEGERP (XCDR (arg))
22043 && XINT (XCDR (arg)) >= 0)
22044 {
22045 *width = XINT (XCDR (arg));
22046 return BAR_CURSOR;
22047 }
22048
22049 if (EQ (arg, Qhbar))
22050 {
22051 *width = 2;
22052 return HBAR_CURSOR;
22053 }
22054
22055 if (CONSP (arg)
22056 && EQ (XCAR (arg), Qhbar)
22057 && INTEGERP (XCDR (arg))
22058 && XINT (XCDR (arg)) >= 0)
22059 {
22060 *width = XINT (XCDR (arg));
22061 return HBAR_CURSOR;
22062 }
22063
22064 /* Treat anything unknown as "hollow box cursor".
22065 It was bad to signal an error; people have trouble fixing
22066 .Xdefaults with Emacs, when it has something bad in it. */
22067 type = HOLLOW_BOX_CURSOR;
22068
22069 return type;
22070 }
22071
22072 /* Set the default cursor types for specified frame. */
22073 void
22074 set_frame_cursor_types (f, arg)
22075 struct frame *f;
22076 Lisp_Object arg;
22077 {
22078 int width;
22079 Lisp_Object tem;
22080
22081 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22082 FRAME_CURSOR_WIDTH (f) = width;
22083
22084 /* By default, set up the blink-off state depending on the on-state. */
22085
22086 tem = Fassoc (arg, Vblink_cursor_alist);
22087 if (!NILP (tem))
22088 {
22089 FRAME_BLINK_OFF_CURSOR (f)
22090 = get_specified_cursor_type (XCDR (tem), &width);
22091 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22092 }
22093 else
22094 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22095 }
22096
22097
22098 /* Return the cursor we want to be displayed in window W. Return
22099 width of bar/hbar cursor through WIDTH arg. Return with
22100 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22101 (i.e. if the `system caret' should track this cursor).
22102
22103 In a mini-buffer window, we want the cursor only to appear if we
22104 are reading input from this window. For the selected window, we
22105 want the cursor type given by the frame parameter or buffer local
22106 setting of cursor-type. If explicitly marked off, draw no cursor.
22107 In all other cases, we want a hollow box cursor. */
22108
22109 static enum text_cursor_kinds
22110 get_window_cursor_type (w, glyph, width, active_cursor)
22111 struct window *w;
22112 struct glyph *glyph;
22113 int *width;
22114 int *active_cursor;
22115 {
22116 struct frame *f = XFRAME (w->frame);
22117 struct buffer *b = XBUFFER (w->buffer);
22118 int cursor_type = DEFAULT_CURSOR;
22119 Lisp_Object alt_cursor;
22120 int non_selected = 0;
22121
22122 *active_cursor = 1;
22123
22124 /* Echo area */
22125 if (cursor_in_echo_area
22126 && FRAME_HAS_MINIBUF_P (f)
22127 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22128 {
22129 if (w == XWINDOW (echo_area_window))
22130 {
22131 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22132 {
22133 *width = FRAME_CURSOR_WIDTH (f);
22134 return FRAME_DESIRED_CURSOR (f);
22135 }
22136 else
22137 return get_specified_cursor_type (b->cursor_type, width);
22138 }
22139
22140 *active_cursor = 0;
22141 non_selected = 1;
22142 }
22143
22144 /* Detect a nonselected window or nonselected frame. */
22145 else if (w != XWINDOW (f->selected_window)
22146 #ifdef HAVE_WINDOW_SYSTEM
22147 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22148 #endif
22149 )
22150 {
22151 *active_cursor = 0;
22152
22153 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22154 return NO_CURSOR;
22155
22156 non_selected = 1;
22157 }
22158
22159 /* Never display a cursor in a window in which cursor-type is nil. */
22160 if (NILP (b->cursor_type))
22161 return NO_CURSOR;
22162
22163 /* Get the normal cursor type for this window. */
22164 if (EQ (b->cursor_type, Qt))
22165 {
22166 cursor_type = FRAME_DESIRED_CURSOR (f);
22167 *width = FRAME_CURSOR_WIDTH (f);
22168 }
22169 else
22170 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22171
22172 /* Use cursor-in-non-selected-windows instead
22173 for non-selected window or frame. */
22174 if (non_selected)
22175 {
22176 alt_cursor = b->cursor_in_non_selected_windows;
22177 if (!EQ (Qt, alt_cursor))
22178 return get_specified_cursor_type (alt_cursor, width);
22179 /* t means modify the normal cursor type. */
22180 if (cursor_type == FILLED_BOX_CURSOR)
22181 cursor_type = HOLLOW_BOX_CURSOR;
22182 else if (cursor_type == BAR_CURSOR && *width > 1)
22183 --*width;
22184 return cursor_type;
22185 }
22186
22187 /* Use normal cursor if not blinked off. */
22188 if (!w->cursor_off_p)
22189 {
22190 #ifdef HAVE_WINDOW_SYSTEM
22191 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22192 {
22193 if (cursor_type == FILLED_BOX_CURSOR)
22194 {
22195 /* Using a block cursor on large images can be very annoying.
22196 So use a hollow cursor for "large" images.
22197 If image is not transparent (no mask), also use hollow cursor. */
22198 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22199 if (img != NULL && IMAGEP (img->spec))
22200 {
22201 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22202 where N = size of default frame font size.
22203 This should cover most of the "tiny" icons people may use. */
22204 if (!img->mask
22205 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22206 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22207 cursor_type = HOLLOW_BOX_CURSOR;
22208 }
22209 }
22210 else if (cursor_type != NO_CURSOR)
22211 {
22212 /* Display current only supports BOX and HOLLOW cursors for images.
22213 So for now, unconditionally use a HOLLOW cursor when cursor is
22214 not a solid box cursor. */
22215 cursor_type = HOLLOW_BOX_CURSOR;
22216 }
22217 }
22218 #endif
22219 return cursor_type;
22220 }
22221
22222 /* Cursor is blinked off, so determine how to "toggle" it. */
22223
22224 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22225 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22226 return get_specified_cursor_type (XCDR (alt_cursor), width);
22227
22228 /* Then see if frame has specified a specific blink off cursor type. */
22229 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22230 {
22231 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22232 return FRAME_BLINK_OFF_CURSOR (f);
22233 }
22234
22235 #if 0
22236 /* Some people liked having a permanently visible blinking cursor,
22237 while others had very strong opinions against it. So it was
22238 decided to remove it. KFS 2003-09-03 */
22239
22240 /* Finally perform built-in cursor blinking:
22241 filled box <-> hollow box
22242 wide [h]bar <-> narrow [h]bar
22243 narrow [h]bar <-> no cursor
22244 other type <-> no cursor */
22245
22246 if (cursor_type == FILLED_BOX_CURSOR)
22247 return HOLLOW_BOX_CURSOR;
22248
22249 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22250 {
22251 *width = 1;
22252 return cursor_type;
22253 }
22254 #endif
22255
22256 return NO_CURSOR;
22257 }
22258
22259
22260 #ifdef HAVE_WINDOW_SYSTEM
22261
22262 /* Notice when the text cursor of window W has been completely
22263 overwritten by a drawing operation that outputs glyphs in AREA
22264 starting at X0 and ending at X1 in the line starting at Y0 and
22265 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22266 the rest of the line after X0 has been written. Y coordinates
22267 are window-relative. */
22268
22269 static void
22270 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22271 struct window *w;
22272 enum glyph_row_area area;
22273 int x0, y0, x1, y1;
22274 {
22275 int cx0, cx1, cy0, cy1;
22276 struct glyph_row *row;
22277
22278 if (!w->phys_cursor_on_p)
22279 return;
22280 if (area != TEXT_AREA)
22281 return;
22282
22283 if (w->phys_cursor.vpos < 0
22284 || w->phys_cursor.vpos >= w->current_matrix->nrows
22285 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22286 !(row->enabled_p && row->displays_text_p)))
22287 return;
22288
22289 if (row->cursor_in_fringe_p)
22290 {
22291 row->cursor_in_fringe_p = 0;
22292 draw_fringe_bitmap (w, row, 0);
22293 w->phys_cursor_on_p = 0;
22294 return;
22295 }
22296
22297 cx0 = w->phys_cursor.x;
22298 cx1 = cx0 + w->phys_cursor_width;
22299 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22300 return;
22301
22302 /* The cursor image will be completely removed from the
22303 screen if the output area intersects the cursor area in
22304 y-direction. When we draw in [y0 y1[, and some part of
22305 the cursor is at y < y0, that part must have been drawn
22306 before. When scrolling, the cursor is erased before
22307 actually scrolling, so we don't come here. When not
22308 scrolling, the rows above the old cursor row must have
22309 changed, and in this case these rows must have written
22310 over the cursor image.
22311
22312 Likewise if part of the cursor is below y1, with the
22313 exception of the cursor being in the first blank row at
22314 the buffer and window end because update_text_area
22315 doesn't draw that row. (Except when it does, but
22316 that's handled in update_text_area.) */
22317
22318 cy0 = w->phys_cursor.y;
22319 cy1 = cy0 + w->phys_cursor_height;
22320 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22321 return;
22322
22323 w->phys_cursor_on_p = 0;
22324 }
22325
22326 #endif /* HAVE_WINDOW_SYSTEM */
22327
22328 \f
22329 /************************************************************************
22330 Mouse Face
22331 ************************************************************************/
22332
22333 #ifdef HAVE_WINDOW_SYSTEM
22334
22335 /* EXPORT for RIF:
22336 Fix the display of area AREA of overlapping row ROW in window W
22337 with respect to the overlapping part OVERLAPS. */
22338
22339 void
22340 x_fix_overlapping_area (w, row, area, overlaps)
22341 struct window *w;
22342 struct glyph_row *row;
22343 enum glyph_row_area area;
22344 int overlaps;
22345 {
22346 int i, x;
22347
22348 BLOCK_INPUT;
22349
22350 x = 0;
22351 for (i = 0; i < row->used[area];)
22352 {
22353 if (row->glyphs[area][i].overlaps_vertically_p)
22354 {
22355 int start = i, start_x = x;
22356
22357 do
22358 {
22359 x += row->glyphs[area][i].pixel_width;
22360 ++i;
22361 }
22362 while (i < row->used[area]
22363 && row->glyphs[area][i].overlaps_vertically_p);
22364
22365 draw_glyphs (w, start_x, row, area,
22366 start, i,
22367 DRAW_NORMAL_TEXT, overlaps);
22368 }
22369 else
22370 {
22371 x += row->glyphs[area][i].pixel_width;
22372 ++i;
22373 }
22374 }
22375
22376 UNBLOCK_INPUT;
22377 }
22378
22379
22380 /* EXPORT:
22381 Draw the cursor glyph of window W in glyph row ROW. See the
22382 comment of draw_glyphs for the meaning of HL. */
22383
22384 void
22385 draw_phys_cursor_glyph (w, row, hl)
22386 struct window *w;
22387 struct glyph_row *row;
22388 enum draw_glyphs_face hl;
22389 {
22390 /* If cursor hpos is out of bounds, don't draw garbage. This can
22391 happen in mini-buffer windows when switching between echo area
22392 glyphs and mini-buffer. */
22393 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22394 {
22395 int on_p = w->phys_cursor_on_p;
22396 int x1;
22397 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22398 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22399 hl, 0);
22400 w->phys_cursor_on_p = on_p;
22401
22402 if (hl == DRAW_CURSOR)
22403 w->phys_cursor_width = x1 - w->phys_cursor.x;
22404 /* When we erase the cursor, and ROW is overlapped by other
22405 rows, make sure that these overlapping parts of other rows
22406 are redrawn. */
22407 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22408 {
22409 w->phys_cursor_width = x1 - w->phys_cursor.x;
22410
22411 if (row > w->current_matrix->rows
22412 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22413 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22414 OVERLAPS_ERASED_CURSOR);
22415
22416 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22417 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22418 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22419 OVERLAPS_ERASED_CURSOR);
22420 }
22421 }
22422 }
22423
22424
22425 /* EXPORT:
22426 Erase the image of a cursor of window W from the screen. */
22427
22428 void
22429 erase_phys_cursor (w)
22430 struct window *w;
22431 {
22432 struct frame *f = XFRAME (w->frame);
22433 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22434 int hpos = w->phys_cursor.hpos;
22435 int vpos = w->phys_cursor.vpos;
22436 int mouse_face_here_p = 0;
22437 struct glyph_matrix *active_glyphs = w->current_matrix;
22438 struct glyph_row *cursor_row;
22439 struct glyph *cursor_glyph;
22440 enum draw_glyphs_face hl;
22441
22442 /* No cursor displayed or row invalidated => nothing to do on the
22443 screen. */
22444 if (w->phys_cursor_type == NO_CURSOR)
22445 goto mark_cursor_off;
22446
22447 /* VPOS >= active_glyphs->nrows means that window has been resized.
22448 Don't bother to erase the cursor. */
22449 if (vpos >= active_glyphs->nrows)
22450 goto mark_cursor_off;
22451
22452 /* If row containing cursor is marked invalid, there is nothing we
22453 can do. */
22454 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22455 if (!cursor_row->enabled_p)
22456 goto mark_cursor_off;
22457
22458 /* If line spacing is > 0, old cursor may only be partially visible in
22459 window after split-window. So adjust visible height. */
22460 cursor_row->visible_height = min (cursor_row->visible_height,
22461 window_text_bottom_y (w) - cursor_row->y);
22462
22463 /* If row is completely invisible, don't attempt to delete a cursor which
22464 isn't there. This can happen if cursor is at top of a window, and
22465 we switch to a buffer with a header line in that window. */
22466 if (cursor_row->visible_height <= 0)
22467 goto mark_cursor_off;
22468
22469 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22470 if (cursor_row->cursor_in_fringe_p)
22471 {
22472 cursor_row->cursor_in_fringe_p = 0;
22473 draw_fringe_bitmap (w, cursor_row, 0);
22474 goto mark_cursor_off;
22475 }
22476
22477 /* This can happen when the new row is shorter than the old one.
22478 In this case, either draw_glyphs or clear_end_of_line
22479 should have cleared the cursor. Note that we wouldn't be
22480 able to erase the cursor in this case because we don't have a
22481 cursor glyph at hand. */
22482 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22483 goto mark_cursor_off;
22484
22485 /* If the cursor is in the mouse face area, redisplay that when
22486 we clear the cursor. */
22487 if (! NILP (dpyinfo->mouse_face_window)
22488 && w == XWINDOW (dpyinfo->mouse_face_window)
22489 && (vpos > dpyinfo->mouse_face_beg_row
22490 || (vpos == dpyinfo->mouse_face_beg_row
22491 && hpos >= dpyinfo->mouse_face_beg_col))
22492 && (vpos < dpyinfo->mouse_face_end_row
22493 || (vpos == dpyinfo->mouse_face_end_row
22494 && hpos < dpyinfo->mouse_face_end_col))
22495 /* Don't redraw the cursor's spot in mouse face if it is at the
22496 end of a line (on a newline). The cursor appears there, but
22497 mouse highlighting does not. */
22498 && cursor_row->used[TEXT_AREA] > hpos)
22499 mouse_face_here_p = 1;
22500
22501 /* Maybe clear the display under the cursor. */
22502 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22503 {
22504 int x, y, left_x;
22505 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22506 int width;
22507
22508 cursor_glyph = get_phys_cursor_glyph (w);
22509 if (cursor_glyph == NULL)
22510 goto mark_cursor_off;
22511
22512 width = cursor_glyph->pixel_width;
22513 left_x = window_box_left_offset (w, TEXT_AREA);
22514 x = w->phys_cursor.x;
22515 if (x < left_x)
22516 width -= left_x - x;
22517 width = min (width, window_box_width (w, TEXT_AREA) - x);
22518 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22519 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22520
22521 if (width > 0)
22522 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22523 }
22524
22525 /* Erase the cursor by redrawing the character underneath it. */
22526 if (mouse_face_here_p)
22527 hl = DRAW_MOUSE_FACE;
22528 else
22529 hl = DRAW_NORMAL_TEXT;
22530 draw_phys_cursor_glyph (w, cursor_row, hl);
22531
22532 mark_cursor_off:
22533 w->phys_cursor_on_p = 0;
22534 w->phys_cursor_type = NO_CURSOR;
22535 }
22536
22537
22538 /* EXPORT:
22539 Display or clear cursor of window W. If ON is zero, clear the
22540 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22541 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22542
22543 void
22544 display_and_set_cursor (w, on, hpos, vpos, x, y)
22545 struct window *w;
22546 int on, hpos, vpos, x, y;
22547 {
22548 struct frame *f = XFRAME (w->frame);
22549 int new_cursor_type;
22550 int new_cursor_width;
22551 int active_cursor;
22552 struct glyph_row *glyph_row;
22553 struct glyph *glyph;
22554
22555 /* This is pointless on invisible frames, and dangerous on garbaged
22556 windows and frames; in the latter case, the frame or window may
22557 be in the midst of changing its size, and x and y may be off the
22558 window. */
22559 if (! FRAME_VISIBLE_P (f)
22560 || FRAME_GARBAGED_P (f)
22561 || vpos >= w->current_matrix->nrows
22562 || hpos >= w->current_matrix->matrix_w)
22563 return;
22564
22565 /* If cursor is off and we want it off, return quickly. */
22566 if (!on && !w->phys_cursor_on_p)
22567 return;
22568
22569 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22570 /* If cursor row is not enabled, we don't really know where to
22571 display the cursor. */
22572 if (!glyph_row->enabled_p)
22573 {
22574 w->phys_cursor_on_p = 0;
22575 return;
22576 }
22577
22578 glyph = NULL;
22579 if (!glyph_row->exact_window_width_line_p
22580 || hpos < glyph_row->used[TEXT_AREA])
22581 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22582
22583 xassert (interrupt_input_blocked);
22584
22585 /* Set new_cursor_type to the cursor we want to be displayed. */
22586 new_cursor_type = get_window_cursor_type (w, glyph,
22587 &new_cursor_width, &active_cursor);
22588
22589 /* If cursor is currently being shown and we don't want it to be or
22590 it is in the wrong place, or the cursor type is not what we want,
22591 erase it. */
22592 if (w->phys_cursor_on_p
22593 && (!on
22594 || w->phys_cursor.x != x
22595 || w->phys_cursor.y != y
22596 || new_cursor_type != w->phys_cursor_type
22597 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22598 && new_cursor_width != w->phys_cursor_width)))
22599 erase_phys_cursor (w);
22600
22601 /* Don't check phys_cursor_on_p here because that flag is only set
22602 to zero in some cases where we know that the cursor has been
22603 completely erased, to avoid the extra work of erasing the cursor
22604 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22605 still not be visible, or it has only been partly erased. */
22606 if (on)
22607 {
22608 w->phys_cursor_ascent = glyph_row->ascent;
22609 w->phys_cursor_height = glyph_row->height;
22610
22611 /* Set phys_cursor_.* before x_draw_.* is called because some
22612 of them may need the information. */
22613 w->phys_cursor.x = x;
22614 w->phys_cursor.y = glyph_row->y;
22615 w->phys_cursor.hpos = hpos;
22616 w->phys_cursor.vpos = vpos;
22617 }
22618
22619 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22620 new_cursor_type, new_cursor_width,
22621 on, active_cursor);
22622 }
22623
22624
22625 /* Switch the display of W's cursor on or off, according to the value
22626 of ON. */
22627
22628 #ifndef HAVE_NS
22629 static
22630 #endif
22631 void
22632 update_window_cursor (w, on)
22633 struct window *w;
22634 int on;
22635 {
22636 /* Don't update cursor in windows whose frame is in the process
22637 of being deleted. */
22638 if (w->current_matrix)
22639 {
22640 BLOCK_INPUT;
22641 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22642 w->phys_cursor.x, w->phys_cursor.y);
22643 UNBLOCK_INPUT;
22644 }
22645 }
22646
22647
22648 /* Call update_window_cursor with parameter ON_P on all leaf windows
22649 in the window tree rooted at W. */
22650
22651 static void
22652 update_cursor_in_window_tree (w, on_p)
22653 struct window *w;
22654 int on_p;
22655 {
22656 while (w)
22657 {
22658 if (!NILP (w->hchild))
22659 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22660 else if (!NILP (w->vchild))
22661 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22662 else
22663 update_window_cursor (w, on_p);
22664
22665 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22666 }
22667 }
22668
22669
22670 /* EXPORT:
22671 Display the cursor on window W, or clear it, according to ON_P.
22672 Don't change the cursor's position. */
22673
22674 void
22675 x_update_cursor (f, on_p)
22676 struct frame *f;
22677 int on_p;
22678 {
22679 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22680 }
22681
22682
22683 /* EXPORT:
22684 Clear the cursor of window W to background color, and mark the
22685 cursor as not shown. This is used when the text where the cursor
22686 is is about to be rewritten. */
22687
22688 void
22689 x_clear_cursor (w)
22690 struct window *w;
22691 {
22692 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22693 update_window_cursor (w, 0);
22694 }
22695
22696
22697 /* EXPORT:
22698 Display the active region described by mouse_face_* according to DRAW. */
22699
22700 void
22701 show_mouse_face (dpyinfo, draw)
22702 Display_Info *dpyinfo;
22703 enum draw_glyphs_face draw;
22704 {
22705 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22706 struct frame *f = XFRAME (WINDOW_FRAME (w));
22707
22708 if (/* If window is in the process of being destroyed, don't bother
22709 to do anything. */
22710 w->current_matrix != NULL
22711 /* Don't update mouse highlight if hidden */
22712 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22713 /* Recognize when we are called to operate on rows that don't exist
22714 anymore. This can happen when a window is split. */
22715 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22716 {
22717 int phys_cursor_on_p = w->phys_cursor_on_p;
22718 struct glyph_row *row, *first, *last;
22719
22720 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22721 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22722
22723 for (row = first; row <= last && row->enabled_p; ++row)
22724 {
22725 int start_hpos, end_hpos, start_x;
22726
22727 /* For all but the first row, the highlight starts at column 0. */
22728 if (row == first)
22729 {
22730 start_hpos = dpyinfo->mouse_face_beg_col;
22731 start_x = dpyinfo->mouse_face_beg_x;
22732 }
22733 else
22734 {
22735 start_hpos = 0;
22736 start_x = 0;
22737 }
22738
22739 if (row == last)
22740 end_hpos = dpyinfo->mouse_face_end_col;
22741 else
22742 {
22743 end_hpos = row->used[TEXT_AREA];
22744 if (draw == DRAW_NORMAL_TEXT)
22745 row->fill_line_p = 1; /* Clear to end of line */
22746 }
22747
22748 if (end_hpos > start_hpos)
22749 {
22750 draw_glyphs (w, start_x, row, TEXT_AREA,
22751 start_hpos, end_hpos,
22752 draw, 0);
22753
22754 row->mouse_face_p
22755 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22756 }
22757 }
22758
22759 /* When we've written over the cursor, arrange for it to
22760 be displayed again. */
22761 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22762 {
22763 BLOCK_INPUT;
22764 display_and_set_cursor (w, 1,
22765 w->phys_cursor.hpos, w->phys_cursor.vpos,
22766 w->phys_cursor.x, w->phys_cursor.y);
22767 UNBLOCK_INPUT;
22768 }
22769 }
22770
22771 /* Change the mouse cursor. */
22772 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22773 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22774 else if (draw == DRAW_MOUSE_FACE)
22775 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22776 else
22777 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22778 }
22779
22780 /* EXPORT:
22781 Clear out the mouse-highlighted active region.
22782 Redraw it un-highlighted first. Value is non-zero if mouse
22783 face was actually drawn unhighlighted. */
22784
22785 int
22786 clear_mouse_face (dpyinfo)
22787 Display_Info *dpyinfo;
22788 {
22789 int cleared = 0;
22790
22791 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22792 {
22793 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22794 cleared = 1;
22795 }
22796
22797 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22798 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22799 dpyinfo->mouse_face_window = Qnil;
22800 dpyinfo->mouse_face_overlay = Qnil;
22801 return cleared;
22802 }
22803
22804
22805 /* EXPORT:
22806 Non-zero if physical cursor of window W is within mouse face. */
22807
22808 int
22809 cursor_in_mouse_face_p (w)
22810 struct window *w;
22811 {
22812 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22813 int in_mouse_face = 0;
22814
22815 if (WINDOWP (dpyinfo->mouse_face_window)
22816 && XWINDOW (dpyinfo->mouse_face_window) == w)
22817 {
22818 int hpos = w->phys_cursor.hpos;
22819 int vpos = w->phys_cursor.vpos;
22820
22821 if (vpos >= dpyinfo->mouse_face_beg_row
22822 && vpos <= dpyinfo->mouse_face_end_row
22823 && (vpos > dpyinfo->mouse_face_beg_row
22824 || hpos >= dpyinfo->mouse_face_beg_col)
22825 && (vpos < dpyinfo->mouse_face_end_row
22826 || hpos < dpyinfo->mouse_face_end_col
22827 || dpyinfo->mouse_face_past_end))
22828 in_mouse_face = 1;
22829 }
22830
22831 return in_mouse_face;
22832 }
22833
22834
22835
22836 \f
22837 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22838 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22839 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22840 for the overlay or run of text properties specifying the mouse
22841 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22842 before-string and after-string that must also be highlighted.
22843 DISPLAY_STRING, if non-nil, is a display string that may cover some
22844 or all of the highlighted text. */
22845
22846 static void
22847 mouse_face_from_buffer_pos (Lisp_Object window,
22848 Display_Info *dpyinfo,
22849 EMACS_INT mouse_charpos,
22850 EMACS_INT start_charpos,
22851 EMACS_INT end_charpos,
22852 Lisp_Object before_string,
22853 Lisp_Object after_string,
22854 Lisp_Object display_string)
22855 {
22856 struct window *w = XWINDOW (window);
22857 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22858 struct glyph_row *row;
22859 struct glyph *glyph, *end;
22860 EMACS_INT ignore;
22861 int x;
22862
22863 xassert (NILP (display_string) || STRINGP (display_string));
22864 xassert (NILP (before_string) || STRINGP (before_string));
22865 xassert (NILP (after_string) || STRINGP (after_string));
22866
22867 /* Find the first highlighted glyph. */
22868 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22869 {
22870 dpyinfo->mouse_face_beg_col = 0;
22871 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22872 dpyinfo->mouse_face_beg_x = first->x;
22873 dpyinfo->mouse_face_beg_y = first->y;
22874 }
22875 else
22876 {
22877 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22878 if (row == NULL)
22879 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22880
22881 /* If the before-string or display-string contains newlines,
22882 row_containing_pos skips to its last row. Move back. */
22883 if (!NILP (before_string) || !NILP (display_string))
22884 {
22885 struct glyph_row *prev;
22886 while ((prev = row - 1, prev >= first)
22887 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22888 && prev->used[TEXT_AREA] > 0)
22889 {
22890 struct glyph *beg = prev->glyphs[TEXT_AREA];
22891 glyph = beg + prev->used[TEXT_AREA];
22892 while (--glyph >= beg && INTEGERP (glyph->object));
22893 if (glyph < beg
22894 || !(EQ (glyph->object, before_string)
22895 || EQ (glyph->object, display_string)))
22896 break;
22897 row = prev;
22898 }
22899 }
22900
22901 glyph = row->glyphs[TEXT_AREA];
22902 end = glyph + row->used[TEXT_AREA];
22903 x = row->x;
22904 dpyinfo->mouse_face_beg_y = row->y;
22905 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22906
22907 /* Skip truncation glyphs at the start of the glyph row. */
22908 if (row->displays_text_p)
22909 for (; glyph < end
22910 && INTEGERP (glyph->object)
22911 && glyph->charpos < 0;
22912 ++glyph)
22913 x += glyph->pixel_width;
22914
22915 /* Scan the glyph row, stopping before BEFORE_STRING or
22916 DISPLAY_STRING or START_CHARPOS. */
22917 for (; glyph < end
22918 && !INTEGERP (glyph->object)
22919 && !EQ (glyph->object, before_string)
22920 && !EQ (glyph->object, display_string)
22921 && !(BUFFERP (glyph->object)
22922 && glyph->charpos >= start_charpos);
22923 ++glyph)
22924 x += glyph->pixel_width;
22925
22926 dpyinfo->mouse_face_beg_x = x;
22927 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22928 }
22929
22930 /* Find the last highlighted glyph. */
22931 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22932 if (row == NULL)
22933 {
22934 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22935 dpyinfo->mouse_face_past_end = 1;
22936 }
22937 else if (!NILP (after_string))
22938 {
22939 /* If the after-string has newlines, advance to its last row. */
22940 struct glyph_row *next;
22941 struct glyph_row *last
22942 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22943
22944 for (next = row + 1;
22945 next <= last
22946 && next->used[TEXT_AREA] > 0
22947 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22948 ++next)
22949 row = next;
22950 }
22951
22952 glyph = row->glyphs[TEXT_AREA];
22953 end = glyph + row->used[TEXT_AREA];
22954 x = row->x;
22955 dpyinfo->mouse_face_end_y = row->y;
22956 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22957
22958 /* Skip truncation glyphs at the start of the row. */
22959 if (row->displays_text_p)
22960 for (; glyph < end
22961 && INTEGERP (glyph->object)
22962 && glyph->charpos < 0;
22963 ++glyph)
22964 x += glyph->pixel_width;
22965
22966 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22967 AFTER_STRING. */
22968 for (; glyph < end
22969 && !INTEGERP (glyph->object)
22970 && !EQ (glyph->object, after_string)
22971 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22972 ++glyph)
22973 x += glyph->pixel_width;
22974
22975 /* If we found AFTER_STRING, consume it and stop. */
22976 if (EQ (glyph->object, after_string))
22977 {
22978 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22979 x += glyph->pixel_width;
22980 }
22981 else
22982 {
22983 /* If there's no after-string, we must check if we overshot,
22984 which might be the case if we stopped after a string glyph.
22985 That glyph may belong to a before-string or display-string
22986 associated with the end position, which must not be
22987 highlighted. */
22988 Lisp_Object prev_object;
22989 int pos;
22990
22991 while (glyph > row->glyphs[TEXT_AREA])
22992 {
22993 prev_object = (glyph - 1)->object;
22994 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
22995 break;
22996
22997 pos = string_buffer_position (w, prev_object, end_charpos);
22998 if (pos && pos < end_charpos)
22999 break;
23000
23001 for (; glyph > row->glyphs[TEXT_AREA]
23002 && EQ ((glyph - 1)->object, prev_object);
23003 --glyph)
23004 x -= (glyph - 1)->pixel_width;
23005 }
23006 }
23007
23008 dpyinfo->mouse_face_end_x = x;
23009 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23010 dpyinfo->mouse_face_window = window;
23011 dpyinfo->mouse_face_face_id
23012 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23013 mouse_charpos + 1,
23014 !dpyinfo->mouse_face_hidden, -1);
23015 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23016 }
23017
23018
23019 /* Find the position of the glyph for position POS in OBJECT in
23020 window W's current matrix, and return in *X, *Y the pixel
23021 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23022
23023 RIGHT_P non-zero means return the position of the right edge of the
23024 glyph, RIGHT_P zero means return the left edge position.
23025
23026 If no glyph for POS exists in the matrix, return the position of
23027 the glyph with the next smaller position that is in the matrix, if
23028 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23029 exists in the matrix, return the position of the glyph with the
23030 next larger position in OBJECT.
23031
23032 Value is non-zero if a glyph was found. */
23033
23034 static int
23035 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23036 struct window *w;
23037 EMACS_INT pos;
23038 Lisp_Object object;
23039 int *hpos, *vpos, *x, *y;
23040 int right_p;
23041 {
23042 int yb = window_text_bottom_y (w);
23043 struct glyph_row *r;
23044 struct glyph *best_glyph = NULL;
23045 struct glyph_row *best_row = NULL;
23046 int best_x = 0;
23047
23048 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23049 r->enabled_p && r->y < yb;
23050 ++r)
23051 {
23052 struct glyph *g = r->glyphs[TEXT_AREA];
23053 struct glyph *e = g + r->used[TEXT_AREA];
23054 int gx;
23055
23056 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23057 if (EQ (g->object, object))
23058 {
23059 if (g->charpos == pos)
23060 {
23061 best_glyph = g;
23062 best_x = gx;
23063 best_row = r;
23064 goto found;
23065 }
23066 else if (best_glyph == NULL
23067 || ((eabs (g->charpos - pos)
23068 < eabs (best_glyph->charpos - pos))
23069 && (right_p
23070 ? g->charpos < pos
23071 : g->charpos > pos)))
23072 {
23073 best_glyph = g;
23074 best_x = gx;
23075 best_row = r;
23076 }
23077 }
23078 }
23079
23080 found:
23081
23082 if (best_glyph)
23083 {
23084 *x = best_x;
23085 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23086
23087 if (right_p)
23088 {
23089 *x += best_glyph->pixel_width;
23090 ++*hpos;
23091 }
23092
23093 *y = best_row->y;
23094 *vpos = best_row - w->current_matrix->rows;
23095 }
23096
23097 return best_glyph != NULL;
23098 }
23099
23100
23101 /* See if position X, Y is within a hot-spot of an image. */
23102
23103 static int
23104 on_hot_spot_p (hot_spot, x, y)
23105 Lisp_Object hot_spot;
23106 int x, y;
23107 {
23108 if (!CONSP (hot_spot))
23109 return 0;
23110
23111 if (EQ (XCAR (hot_spot), Qrect))
23112 {
23113 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23114 Lisp_Object rect = XCDR (hot_spot);
23115 Lisp_Object tem;
23116 if (!CONSP (rect))
23117 return 0;
23118 if (!CONSP (XCAR (rect)))
23119 return 0;
23120 if (!CONSP (XCDR (rect)))
23121 return 0;
23122 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23123 return 0;
23124 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23125 return 0;
23126 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23127 return 0;
23128 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23129 return 0;
23130 return 1;
23131 }
23132 else if (EQ (XCAR (hot_spot), Qcircle))
23133 {
23134 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23135 Lisp_Object circ = XCDR (hot_spot);
23136 Lisp_Object lr, lx0, ly0;
23137 if (CONSP (circ)
23138 && CONSP (XCAR (circ))
23139 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23140 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23141 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23142 {
23143 double r = XFLOATINT (lr);
23144 double dx = XINT (lx0) - x;
23145 double dy = XINT (ly0) - y;
23146 return (dx * dx + dy * dy <= r * r);
23147 }
23148 }
23149 else if (EQ (XCAR (hot_spot), Qpoly))
23150 {
23151 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23152 if (VECTORP (XCDR (hot_spot)))
23153 {
23154 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23155 Lisp_Object *poly = v->contents;
23156 int n = v->size;
23157 int i;
23158 int inside = 0;
23159 Lisp_Object lx, ly;
23160 int x0, y0;
23161
23162 /* Need an even number of coordinates, and at least 3 edges. */
23163 if (n < 6 || n & 1)
23164 return 0;
23165
23166 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23167 If count is odd, we are inside polygon. Pixels on edges
23168 may or may not be included depending on actual geometry of the
23169 polygon. */
23170 if ((lx = poly[n-2], !INTEGERP (lx))
23171 || (ly = poly[n-1], !INTEGERP (lx)))
23172 return 0;
23173 x0 = XINT (lx), y0 = XINT (ly);
23174 for (i = 0; i < n; i += 2)
23175 {
23176 int x1 = x0, y1 = y0;
23177 if ((lx = poly[i], !INTEGERP (lx))
23178 || (ly = poly[i+1], !INTEGERP (ly)))
23179 return 0;
23180 x0 = XINT (lx), y0 = XINT (ly);
23181
23182 /* Does this segment cross the X line? */
23183 if (x0 >= x)
23184 {
23185 if (x1 >= x)
23186 continue;
23187 }
23188 else if (x1 < x)
23189 continue;
23190 if (y > y0 && y > y1)
23191 continue;
23192 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23193 inside = !inside;
23194 }
23195 return inside;
23196 }
23197 }
23198 return 0;
23199 }
23200
23201 Lisp_Object
23202 find_hot_spot (map, x, y)
23203 Lisp_Object map;
23204 int x, y;
23205 {
23206 while (CONSP (map))
23207 {
23208 if (CONSP (XCAR (map))
23209 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23210 return XCAR (map);
23211 map = XCDR (map);
23212 }
23213
23214 return Qnil;
23215 }
23216
23217 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23218 3, 3, 0,
23219 doc: /* Lookup in image map MAP coordinates X and Y.
23220 An image map is an alist where each element has the format (AREA ID PLIST).
23221 An AREA is specified as either a rectangle, a circle, or a polygon:
23222 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23223 pixel coordinates of the upper left and bottom right corners.
23224 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23225 and the radius of the circle; r may be a float or integer.
23226 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23227 vector describes one corner in the polygon.
23228 Returns the alist element for the first matching AREA in MAP. */)
23229 (map, x, y)
23230 Lisp_Object map;
23231 Lisp_Object x, y;
23232 {
23233 if (NILP (map))
23234 return Qnil;
23235
23236 CHECK_NUMBER (x);
23237 CHECK_NUMBER (y);
23238
23239 return find_hot_spot (map, XINT (x), XINT (y));
23240 }
23241
23242
23243 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23244 static void
23245 define_frame_cursor1 (f, cursor, pointer)
23246 struct frame *f;
23247 Cursor cursor;
23248 Lisp_Object pointer;
23249 {
23250 /* Do not change cursor shape while dragging mouse. */
23251 if (!NILP (do_mouse_tracking))
23252 return;
23253
23254 if (!NILP (pointer))
23255 {
23256 if (EQ (pointer, Qarrow))
23257 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23258 else if (EQ (pointer, Qhand))
23259 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23260 else if (EQ (pointer, Qtext))
23261 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23262 else if (EQ (pointer, intern ("hdrag")))
23263 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23264 #ifdef HAVE_X_WINDOWS
23265 else if (EQ (pointer, intern ("vdrag")))
23266 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23267 #endif
23268 else if (EQ (pointer, intern ("hourglass")))
23269 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23270 else if (EQ (pointer, Qmodeline))
23271 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23272 else
23273 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23274 }
23275
23276 if (cursor != No_Cursor)
23277 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23278 }
23279
23280 /* Take proper action when mouse has moved to the mode or header line
23281 or marginal area AREA of window W, x-position X and y-position Y.
23282 X is relative to the start of the text display area of W, so the
23283 width of bitmap areas and scroll bars must be subtracted to get a
23284 position relative to the start of the mode line. */
23285
23286 static void
23287 note_mode_line_or_margin_highlight (window, x, y, area)
23288 Lisp_Object window;
23289 int x, y;
23290 enum window_part area;
23291 {
23292 struct window *w = XWINDOW (window);
23293 struct frame *f = XFRAME (w->frame);
23294 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23295 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23296 Lisp_Object pointer = Qnil;
23297 int charpos, dx, dy, width, height;
23298 Lisp_Object string, object = Qnil;
23299 Lisp_Object pos, help;
23300
23301 Lisp_Object mouse_face;
23302 int original_x_pixel = x;
23303 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23304 struct glyph_row *row;
23305
23306 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23307 {
23308 int x0;
23309 struct glyph *end;
23310
23311 string = mode_line_string (w, area, &x, &y, &charpos,
23312 &object, &dx, &dy, &width, &height);
23313
23314 row = (area == ON_MODE_LINE
23315 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23316 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23317
23318 /* Find glyph */
23319 if (row->mode_line_p && row->enabled_p)
23320 {
23321 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23322 end = glyph + row->used[TEXT_AREA];
23323
23324 for (x0 = original_x_pixel;
23325 glyph < end && x0 >= glyph->pixel_width;
23326 ++glyph)
23327 x0 -= glyph->pixel_width;
23328
23329 if (glyph >= end)
23330 glyph = NULL;
23331 }
23332 }
23333 else
23334 {
23335 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23336 string = marginal_area_string (w, area, &x, &y, &charpos,
23337 &object, &dx, &dy, &width, &height);
23338 }
23339
23340 help = Qnil;
23341
23342 if (IMAGEP (object))
23343 {
23344 Lisp_Object image_map, hotspot;
23345 if ((image_map = Fplist_get (XCDR (object), QCmap),
23346 !NILP (image_map))
23347 && (hotspot = find_hot_spot (image_map, dx, dy),
23348 CONSP (hotspot))
23349 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23350 {
23351 Lisp_Object area_id, plist;
23352
23353 area_id = XCAR (hotspot);
23354 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23355 If so, we could look for mouse-enter, mouse-leave
23356 properties in PLIST (and do something...). */
23357 hotspot = XCDR (hotspot);
23358 if (CONSP (hotspot)
23359 && (plist = XCAR (hotspot), CONSP (plist)))
23360 {
23361 pointer = Fplist_get (plist, Qpointer);
23362 if (NILP (pointer))
23363 pointer = Qhand;
23364 help = Fplist_get (plist, Qhelp_echo);
23365 if (!NILP (help))
23366 {
23367 help_echo_string = help;
23368 /* Is this correct? ++kfs */
23369 XSETWINDOW (help_echo_window, w);
23370 help_echo_object = w->buffer;
23371 help_echo_pos = charpos;
23372 }
23373 }
23374 }
23375 if (NILP (pointer))
23376 pointer = Fplist_get (XCDR (object), QCpointer);
23377 }
23378
23379 if (STRINGP (string))
23380 {
23381 pos = make_number (charpos);
23382 /* If we're on a string with `help-echo' text property, arrange
23383 for the help to be displayed. This is done by setting the
23384 global variable help_echo_string to the help string. */
23385 if (NILP (help))
23386 {
23387 help = Fget_text_property (pos, Qhelp_echo, string);
23388 if (!NILP (help))
23389 {
23390 help_echo_string = help;
23391 XSETWINDOW (help_echo_window, w);
23392 help_echo_object = string;
23393 help_echo_pos = charpos;
23394 }
23395 }
23396
23397 if (NILP (pointer))
23398 pointer = Fget_text_property (pos, Qpointer, string);
23399
23400 /* Change the mouse pointer according to what is under X/Y. */
23401 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23402 {
23403 Lisp_Object map;
23404 map = Fget_text_property (pos, Qlocal_map, string);
23405 if (!KEYMAPP (map))
23406 map = Fget_text_property (pos, Qkeymap, string);
23407 if (!KEYMAPP (map))
23408 cursor = dpyinfo->vertical_scroll_bar_cursor;
23409 }
23410
23411 /* Change the mouse face according to what is under X/Y. */
23412 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23413 if (!NILP (mouse_face)
23414 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23415 && glyph)
23416 {
23417 Lisp_Object b, e;
23418
23419 struct glyph * tmp_glyph;
23420
23421 int gpos;
23422 int gseq_length;
23423 int total_pixel_width;
23424 EMACS_INT ignore;
23425
23426 int vpos, hpos;
23427
23428 b = Fprevious_single_property_change (make_number (charpos + 1),
23429 Qmouse_face, string, Qnil);
23430 if (NILP (b))
23431 b = make_number (0);
23432
23433 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23434 if (NILP (e))
23435 e = make_number (SCHARS (string));
23436
23437 /* Calculate the position(glyph position: GPOS) of GLYPH in
23438 displayed string. GPOS is different from CHARPOS.
23439
23440 CHARPOS is the position of glyph in internal string
23441 object. A mode line string format has structures which
23442 is converted to a flatten by emacs lisp interpreter.
23443 The internal string is an element of the structures.
23444 The displayed string is the flatten string. */
23445 gpos = 0;
23446 if (glyph > row_start_glyph)
23447 {
23448 tmp_glyph = glyph - 1;
23449 while (tmp_glyph >= row_start_glyph
23450 && tmp_glyph->charpos >= XINT (b)
23451 && EQ (tmp_glyph->object, glyph->object))
23452 {
23453 tmp_glyph--;
23454 gpos++;
23455 }
23456 }
23457
23458 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23459 displayed string holding GLYPH.
23460
23461 GSEQ_LENGTH is different from SCHARS (STRING).
23462 SCHARS (STRING) returns the length of the internal string. */
23463 for (tmp_glyph = glyph, gseq_length = gpos;
23464 tmp_glyph->charpos < XINT (e);
23465 tmp_glyph++, gseq_length++)
23466 {
23467 if (!EQ (tmp_glyph->object, glyph->object))
23468 break;
23469 }
23470
23471 total_pixel_width = 0;
23472 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23473 total_pixel_width += tmp_glyph->pixel_width;
23474
23475 /* Pre calculation of re-rendering position */
23476 vpos = (x - gpos);
23477 hpos = (area == ON_MODE_LINE
23478 ? (w->current_matrix)->nrows - 1
23479 : 0);
23480
23481 /* If the re-rendering position is included in the last
23482 re-rendering area, we should do nothing. */
23483 if ( EQ (window, dpyinfo->mouse_face_window)
23484 && dpyinfo->mouse_face_beg_col <= vpos
23485 && vpos < dpyinfo->mouse_face_end_col
23486 && dpyinfo->mouse_face_beg_row == hpos )
23487 return;
23488
23489 if (clear_mouse_face (dpyinfo))
23490 cursor = No_Cursor;
23491
23492 dpyinfo->mouse_face_beg_col = vpos;
23493 dpyinfo->mouse_face_beg_row = hpos;
23494
23495 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23496 dpyinfo->mouse_face_beg_y = 0;
23497
23498 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23499 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23500
23501 dpyinfo->mouse_face_end_x = 0;
23502 dpyinfo->mouse_face_end_y = 0;
23503
23504 dpyinfo->mouse_face_past_end = 0;
23505 dpyinfo->mouse_face_window = window;
23506
23507 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23508 charpos,
23509 0, 0, 0, &ignore,
23510 glyph->face_id, 1);
23511 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23512
23513 if (NILP (pointer))
23514 pointer = Qhand;
23515 }
23516 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23517 clear_mouse_face (dpyinfo);
23518 }
23519 define_frame_cursor1 (f, cursor, pointer);
23520 }
23521
23522
23523 /* EXPORT:
23524 Take proper action when the mouse has moved to position X, Y on
23525 frame F as regards highlighting characters that have mouse-face
23526 properties. Also de-highlighting chars where the mouse was before.
23527 X and Y can be negative or out of range. */
23528
23529 void
23530 note_mouse_highlight (f, x, y)
23531 struct frame *f;
23532 int x, y;
23533 {
23534 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23535 enum window_part part;
23536 Lisp_Object window;
23537 struct window *w;
23538 Cursor cursor = No_Cursor;
23539 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23540 struct buffer *b;
23541
23542 /* When a menu is active, don't highlight because this looks odd. */
23543 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23544 if (popup_activated ())
23545 return;
23546 #endif
23547
23548 if (NILP (Vmouse_highlight)
23549 || !f->glyphs_initialized_p)
23550 return;
23551
23552 dpyinfo->mouse_face_mouse_x = x;
23553 dpyinfo->mouse_face_mouse_y = y;
23554 dpyinfo->mouse_face_mouse_frame = f;
23555
23556 if (dpyinfo->mouse_face_defer)
23557 return;
23558
23559 if (gc_in_progress)
23560 {
23561 dpyinfo->mouse_face_deferred_gc = 1;
23562 return;
23563 }
23564
23565 /* Which window is that in? */
23566 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23567
23568 /* If we were displaying active text in another window, clear that.
23569 Also clear if we move out of text area in same window. */
23570 if (! EQ (window, dpyinfo->mouse_face_window)
23571 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23572 && !NILP (dpyinfo->mouse_face_window)))
23573 clear_mouse_face (dpyinfo);
23574
23575 /* Not on a window -> return. */
23576 if (!WINDOWP (window))
23577 return;
23578
23579 /* Reset help_echo_string. It will get recomputed below. */
23580 help_echo_string = Qnil;
23581
23582 /* Convert to window-relative pixel coordinates. */
23583 w = XWINDOW (window);
23584 frame_to_window_pixel_xy (w, &x, &y);
23585
23586 /* Handle tool-bar window differently since it doesn't display a
23587 buffer. */
23588 if (EQ (window, f->tool_bar_window))
23589 {
23590 note_tool_bar_highlight (f, x, y);
23591 return;
23592 }
23593
23594 /* Mouse is on the mode, header line or margin? */
23595 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23596 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23597 {
23598 note_mode_line_or_margin_highlight (window, x, y, part);
23599 return;
23600 }
23601
23602 if (part == ON_VERTICAL_BORDER)
23603 {
23604 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23605 help_echo_string = build_string ("drag-mouse-1: resize");
23606 }
23607 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23608 || part == ON_SCROLL_BAR)
23609 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23610 else
23611 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23612
23613 /* Are we in a window whose display is up to date?
23614 And verify the buffer's text has not changed. */
23615 b = XBUFFER (w->buffer);
23616 if (part == ON_TEXT
23617 && EQ (w->window_end_valid, w->buffer)
23618 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23619 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23620 {
23621 int hpos, vpos, pos, i, dx, dy, area;
23622 struct glyph *glyph;
23623 Lisp_Object object;
23624 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23625 Lisp_Object *overlay_vec = NULL;
23626 int noverlays;
23627 struct buffer *obuf;
23628 int obegv, ozv, same_region;
23629
23630 /* Find the glyph under X/Y. */
23631 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23632
23633 /* Look for :pointer property on image. */
23634 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23635 {
23636 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23637 if (img != NULL && IMAGEP (img->spec))
23638 {
23639 Lisp_Object image_map, hotspot;
23640 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23641 !NILP (image_map))
23642 && (hotspot = find_hot_spot (image_map,
23643 glyph->slice.x + dx,
23644 glyph->slice.y + dy),
23645 CONSP (hotspot))
23646 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23647 {
23648 Lisp_Object area_id, plist;
23649
23650 area_id = XCAR (hotspot);
23651 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23652 If so, we could look for mouse-enter, mouse-leave
23653 properties in PLIST (and do something...). */
23654 hotspot = XCDR (hotspot);
23655 if (CONSP (hotspot)
23656 && (plist = XCAR (hotspot), CONSP (plist)))
23657 {
23658 pointer = Fplist_get (plist, Qpointer);
23659 if (NILP (pointer))
23660 pointer = Qhand;
23661 help_echo_string = Fplist_get (plist, Qhelp_echo);
23662 if (!NILP (help_echo_string))
23663 {
23664 help_echo_window = window;
23665 help_echo_object = glyph->object;
23666 help_echo_pos = glyph->charpos;
23667 }
23668 }
23669 }
23670 if (NILP (pointer))
23671 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23672 }
23673 }
23674
23675 /* Clear mouse face if X/Y not over text. */
23676 if (glyph == NULL
23677 || area != TEXT_AREA
23678 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23679 {
23680 if (clear_mouse_face (dpyinfo))
23681 cursor = No_Cursor;
23682 if (NILP (pointer))
23683 {
23684 if (area != TEXT_AREA)
23685 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23686 else
23687 pointer = Vvoid_text_area_pointer;
23688 }
23689 goto set_cursor;
23690 }
23691
23692 pos = glyph->charpos;
23693 object = glyph->object;
23694 if (!STRINGP (object) && !BUFFERP (object))
23695 goto set_cursor;
23696
23697 /* If we get an out-of-range value, return now; avoid an error. */
23698 if (BUFFERP (object) && pos > BUF_Z (b))
23699 goto set_cursor;
23700
23701 /* Make the window's buffer temporarily current for
23702 overlays_at and compute_char_face. */
23703 obuf = current_buffer;
23704 current_buffer = b;
23705 obegv = BEGV;
23706 ozv = ZV;
23707 BEGV = BEG;
23708 ZV = Z;
23709
23710 /* Is this char mouse-active or does it have help-echo? */
23711 position = make_number (pos);
23712
23713 if (BUFFERP (object))
23714 {
23715 /* Put all the overlays we want in a vector in overlay_vec. */
23716 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23717 /* Sort overlays into increasing priority order. */
23718 noverlays = sort_overlays (overlay_vec, noverlays, w);
23719 }
23720 else
23721 noverlays = 0;
23722
23723 same_region = (EQ (window, dpyinfo->mouse_face_window)
23724 && vpos >= dpyinfo->mouse_face_beg_row
23725 && vpos <= dpyinfo->mouse_face_end_row
23726 && (vpos > dpyinfo->mouse_face_beg_row
23727 || hpos >= dpyinfo->mouse_face_beg_col)
23728 && (vpos < dpyinfo->mouse_face_end_row
23729 || hpos < dpyinfo->mouse_face_end_col
23730 || dpyinfo->mouse_face_past_end));
23731
23732 if (same_region)
23733 cursor = No_Cursor;
23734
23735 /* Check mouse-face highlighting. */
23736 if (! same_region
23737 /* If there exists an overlay with mouse-face overlapping
23738 the one we are currently highlighting, we have to
23739 check if we enter the overlapping overlay, and then
23740 highlight only that. */
23741 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23742 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23743 {
23744 /* Find the highest priority overlay with a mouse-face. */
23745 overlay = Qnil;
23746 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23747 {
23748 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23749 if (!NILP (mouse_face))
23750 overlay = overlay_vec[i];
23751 }
23752
23753 /* If we're highlighting the same overlay as before, there's
23754 no need to do that again. */
23755 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23756 goto check_help_echo;
23757 dpyinfo->mouse_face_overlay = overlay;
23758
23759 /* Clear the display of the old active region, if any. */
23760 if (clear_mouse_face (dpyinfo))
23761 cursor = No_Cursor;
23762
23763 /* If no overlay applies, get a text property. */
23764 if (NILP (overlay))
23765 mouse_face = Fget_text_property (position, Qmouse_face, object);
23766
23767 /* Next, compute the bounds of the mouse highlighting and
23768 display it. */
23769 if (!NILP (mouse_face) && STRINGP (object))
23770 {
23771 /* The mouse-highlighting comes from a display string
23772 with a mouse-face. */
23773 Lisp_Object b, e;
23774 EMACS_INT ignore;
23775
23776 b = Fprevious_single_property_change
23777 (make_number (pos + 1), Qmouse_face, object, Qnil);
23778 e = Fnext_single_property_change
23779 (position, Qmouse_face, object, Qnil);
23780 if (NILP (b))
23781 b = make_number (0);
23782 if (NILP (e))
23783 e = make_number (SCHARS (object) - 1);
23784
23785 fast_find_string_pos (w, XINT (b), object,
23786 &dpyinfo->mouse_face_beg_col,
23787 &dpyinfo->mouse_face_beg_row,
23788 &dpyinfo->mouse_face_beg_x,
23789 &dpyinfo->mouse_face_beg_y, 0);
23790 fast_find_string_pos (w, XINT (e), object,
23791 &dpyinfo->mouse_face_end_col,
23792 &dpyinfo->mouse_face_end_row,
23793 &dpyinfo->mouse_face_end_x,
23794 &dpyinfo->mouse_face_end_y, 1);
23795 dpyinfo->mouse_face_past_end = 0;
23796 dpyinfo->mouse_face_window = window;
23797 dpyinfo->mouse_face_face_id
23798 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23799 glyph->face_id, 1);
23800 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23801 cursor = No_Cursor;
23802 }
23803 else
23804 {
23805 /* The mouse-highlighting, if any, comes from an overlay
23806 or text property in the buffer. */
23807 Lisp_Object buffer, display_string;
23808
23809 if (STRINGP (object))
23810 {
23811 /* If we are on a display string with no mouse-face,
23812 check if the text under it has one. */
23813 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23814 int start = MATRIX_ROW_START_CHARPOS (r);
23815 pos = string_buffer_position (w, object, start);
23816 if (pos > 0)
23817 {
23818 mouse_face = get_char_property_and_overlay
23819 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23820 buffer = w->buffer;
23821 display_string = object;
23822 }
23823 }
23824 else
23825 {
23826 buffer = object;
23827 display_string = Qnil;
23828 }
23829
23830 if (!NILP (mouse_face))
23831 {
23832 Lisp_Object before, after;
23833 Lisp_Object before_string, after_string;
23834
23835 if (NILP (overlay))
23836 {
23837 /* Handle the text property case. */
23838 before = Fprevious_single_property_change
23839 (make_number (pos + 1), Qmouse_face, buffer,
23840 Fmarker_position (w->start));
23841 after = Fnext_single_property_change
23842 (make_number (pos), Qmouse_face, buffer,
23843 make_number (BUF_Z (XBUFFER (buffer))
23844 - XFASTINT (w->window_end_pos)));
23845 before_string = after_string = Qnil;
23846 }
23847 else
23848 {
23849 /* Handle the overlay case. */
23850 before = Foverlay_start (overlay);
23851 after = Foverlay_end (overlay);
23852 before_string = Foverlay_get (overlay, Qbefore_string);
23853 after_string = Foverlay_get (overlay, Qafter_string);
23854
23855 if (!STRINGP (before_string)) before_string = Qnil;
23856 if (!STRINGP (after_string)) after_string = Qnil;
23857 }
23858
23859 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23860 XFASTINT (before),
23861 XFASTINT (after),
23862 before_string, after_string,
23863 display_string);
23864 cursor = No_Cursor;
23865 }
23866 }
23867 }
23868
23869 check_help_echo:
23870
23871 /* Look for a `help-echo' property. */
23872 if (NILP (help_echo_string)) {
23873 Lisp_Object help, overlay;
23874
23875 /* Check overlays first. */
23876 help = overlay = Qnil;
23877 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23878 {
23879 overlay = overlay_vec[i];
23880 help = Foverlay_get (overlay, Qhelp_echo);
23881 }
23882
23883 if (!NILP (help))
23884 {
23885 help_echo_string = help;
23886 help_echo_window = window;
23887 help_echo_object = overlay;
23888 help_echo_pos = pos;
23889 }
23890 else
23891 {
23892 Lisp_Object object = glyph->object;
23893 int charpos = glyph->charpos;
23894
23895 /* Try text properties. */
23896 if (STRINGP (object)
23897 && charpos >= 0
23898 && charpos < SCHARS (object))
23899 {
23900 help = Fget_text_property (make_number (charpos),
23901 Qhelp_echo, object);
23902 if (NILP (help))
23903 {
23904 /* If the string itself doesn't specify a help-echo,
23905 see if the buffer text ``under'' it does. */
23906 struct glyph_row *r
23907 = MATRIX_ROW (w->current_matrix, vpos);
23908 int start = MATRIX_ROW_START_CHARPOS (r);
23909 int pos = string_buffer_position (w, object, start);
23910 if (pos > 0)
23911 {
23912 help = Fget_char_property (make_number (pos),
23913 Qhelp_echo, w->buffer);
23914 if (!NILP (help))
23915 {
23916 charpos = pos;
23917 object = w->buffer;
23918 }
23919 }
23920 }
23921 }
23922 else if (BUFFERP (object)
23923 && charpos >= BEGV
23924 && charpos < ZV)
23925 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23926 object);
23927
23928 if (!NILP (help))
23929 {
23930 help_echo_string = help;
23931 help_echo_window = window;
23932 help_echo_object = object;
23933 help_echo_pos = charpos;
23934 }
23935 }
23936 }
23937
23938 /* Look for a `pointer' property. */
23939 if (NILP (pointer))
23940 {
23941 /* Check overlays first. */
23942 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23943 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23944
23945 if (NILP (pointer))
23946 {
23947 Lisp_Object object = glyph->object;
23948 int charpos = glyph->charpos;
23949
23950 /* Try text properties. */
23951 if (STRINGP (object)
23952 && charpos >= 0
23953 && charpos < SCHARS (object))
23954 {
23955 pointer = Fget_text_property (make_number (charpos),
23956 Qpointer, object);
23957 if (NILP (pointer))
23958 {
23959 /* If the string itself doesn't specify a pointer,
23960 see if the buffer text ``under'' it does. */
23961 struct glyph_row *r
23962 = MATRIX_ROW (w->current_matrix, vpos);
23963 int start = MATRIX_ROW_START_CHARPOS (r);
23964 int pos = string_buffer_position (w, object, start);
23965 if (pos > 0)
23966 pointer = Fget_char_property (make_number (pos),
23967 Qpointer, w->buffer);
23968 }
23969 }
23970 else if (BUFFERP (object)
23971 && charpos >= BEGV
23972 && charpos < ZV)
23973 pointer = Fget_text_property (make_number (charpos),
23974 Qpointer, object);
23975 }
23976 }
23977
23978 BEGV = obegv;
23979 ZV = ozv;
23980 current_buffer = obuf;
23981 }
23982
23983 set_cursor:
23984
23985 define_frame_cursor1 (f, cursor, pointer);
23986 }
23987
23988
23989 /* EXPORT for RIF:
23990 Clear any mouse-face on window W. This function is part of the
23991 redisplay interface, and is called from try_window_id and similar
23992 functions to ensure the mouse-highlight is off. */
23993
23994 void
23995 x_clear_window_mouse_face (w)
23996 struct window *w;
23997 {
23998 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23999 Lisp_Object window;
24000
24001 BLOCK_INPUT;
24002 XSETWINDOW (window, w);
24003 if (EQ (window, dpyinfo->mouse_face_window))
24004 clear_mouse_face (dpyinfo);
24005 UNBLOCK_INPUT;
24006 }
24007
24008
24009 /* EXPORT:
24010 Just discard the mouse face information for frame F, if any.
24011 This is used when the size of F is changed. */
24012
24013 void
24014 cancel_mouse_face (f)
24015 struct frame *f;
24016 {
24017 Lisp_Object window;
24018 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24019
24020 window = dpyinfo->mouse_face_window;
24021 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24022 {
24023 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24024 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24025 dpyinfo->mouse_face_window = Qnil;
24026 }
24027 }
24028
24029
24030 #endif /* HAVE_WINDOW_SYSTEM */
24031
24032 \f
24033 /***********************************************************************
24034 Exposure Events
24035 ***********************************************************************/
24036
24037 #ifdef HAVE_WINDOW_SYSTEM
24038
24039 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24040 which intersects rectangle R. R is in window-relative coordinates. */
24041
24042 static void
24043 expose_area (w, row, r, area)
24044 struct window *w;
24045 struct glyph_row *row;
24046 XRectangle *r;
24047 enum glyph_row_area area;
24048 {
24049 struct glyph *first = row->glyphs[area];
24050 struct glyph *end = row->glyphs[area] + row->used[area];
24051 struct glyph *last;
24052 int first_x, start_x, x;
24053
24054 if (area == TEXT_AREA && row->fill_line_p)
24055 /* If row extends face to end of line write the whole line. */
24056 draw_glyphs (w, 0, row, area,
24057 0, row->used[area],
24058 DRAW_NORMAL_TEXT, 0);
24059 else
24060 {
24061 /* Set START_X to the window-relative start position for drawing glyphs of
24062 AREA. The first glyph of the text area can be partially visible.
24063 The first glyphs of other areas cannot. */
24064 start_x = window_box_left_offset (w, area);
24065 x = start_x;
24066 if (area == TEXT_AREA)
24067 x += row->x;
24068
24069 /* Find the first glyph that must be redrawn. */
24070 while (first < end
24071 && x + first->pixel_width < r->x)
24072 {
24073 x += first->pixel_width;
24074 ++first;
24075 }
24076
24077 /* Find the last one. */
24078 last = first;
24079 first_x = x;
24080 while (last < end
24081 && x < r->x + r->width)
24082 {
24083 x += last->pixel_width;
24084 ++last;
24085 }
24086
24087 /* Repaint. */
24088 if (last > first)
24089 draw_glyphs (w, first_x - start_x, row, area,
24090 first - row->glyphs[area], last - row->glyphs[area],
24091 DRAW_NORMAL_TEXT, 0);
24092 }
24093 }
24094
24095
24096 /* Redraw the parts of the glyph row ROW on window W intersecting
24097 rectangle R. R is in window-relative coordinates. Value is
24098 non-zero if mouse-face was overwritten. */
24099
24100 static int
24101 expose_line (w, row, r)
24102 struct window *w;
24103 struct glyph_row *row;
24104 XRectangle *r;
24105 {
24106 xassert (row->enabled_p);
24107
24108 if (row->mode_line_p || w->pseudo_window_p)
24109 draw_glyphs (w, 0, row, TEXT_AREA,
24110 0, row->used[TEXT_AREA],
24111 DRAW_NORMAL_TEXT, 0);
24112 else
24113 {
24114 if (row->used[LEFT_MARGIN_AREA])
24115 expose_area (w, row, r, LEFT_MARGIN_AREA);
24116 if (row->used[TEXT_AREA])
24117 expose_area (w, row, r, TEXT_AREA);
24118 if (row->used[RIGHT_MARGIN_AREA])
24119 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24120 draw_row_fringe_bitmaps (w, row);
24121 }
24122
24123 return row->mouse_face_p;
24124 }
24125
24126
24127 /* Redraw those parts of glyphs rows during expose event handling that
24128 overlap other rows. Redrawing of an exposed line writes over parts
24129 of lines overlapping that exposed line; this function fixes that.
24130
24131 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24132 row in W's current matrix that is exposed and overlaps other rows.
24133 LAST_OVERLAPPING_ROW is the last such row. */
24134
24135 static void
24136 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24137 struct window *w;
24138 struct glyph_row *first_overlapping_row;
24139 struct glyph_row *last_overlapping_row;
24140 XRectangle *r;
24141 {
24142 struct glyph_row *row;
24143
24144 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24145 if (row->overlapping_p)
24146 {
24147 xassert (row->enabled_p && !row->mode_line_p);
24148
24149 row->clip = r;
24150 if (row->used[LEFT_MARGIN_AREA])
24151 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24152
24153 if (row->used[TEXT_AREA])
24154 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24155
24156 if (row->used[RIGHT_MARGIN_AREA])
24157 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24158 row->clip = NULL;
24159 }
24160 }
24161
24162
24163 /* Return non-zero if W's cursor intersects rectangle R. */
24164
24165 static int
24166 phys_cursor_in_rect_p (w, r)
24167 struct window *w;
24168 XRectangle *r;
24169 {
24170 XRectangle cr, result;
24171 struct glyph *cursor_glyph;
24172 struct glyph_row *row;
24173
24174 if (w->phys_cursor.vpos >= 0
24175 && w->phys_cursor.vpos < w->current_matrix->nrows
24176 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24177 row->enabled_p)
24178 && row->cursor_in_fringe_p)
24179 {
24180 /* Cursor is in the fringe. */
24181 cr.x = window_box_right_offset (w,
24182 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24183 ? RIGHT_MARGIN_AREA
24184 : TEXT_AREA));
24185 cr.y = row->y;
24186 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24187 cr.height = row->height;
24188 return x_intersect_rectangles (&cr, r, &result);
24189 }
24190
24191 cursor_glyph = get_phys_cursor_glyph (w);
24192 if (cursor_glyph)
24193 {
24194 /* r is relative to W's box, but w->phys_cursor.x is relative
24195 to left edge of W's TEXT area. Adjust it. */
24196 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24197 cr.y = w->phys_cursor.y;
24198 cr.width = cursor_glyph->pixel_width;
24199 cr.height = w->phys_cursor_height;
24200 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24201 I assume the effect is the same -- and this is portable. */
24202 return x_intersect_rectangles (&cr, r, &result);
24203 }
24204 /* If we don't understand the format, pretend we're not in the hot-spot. */
24205 return 0;
24206 }
24207
24208
24209 /* EXPORT:
24210 Draw a vertical window border to the right of window W if W doesn't
24211 have vertical scroll bars. */
24212
24213 void
24214 x_draw_vertical_border (w)
24215 struct window *w;
24216 {
24217 struct frame *f = XFRAME (WINDOW_FRAME (w));
24218
24219 /* We could do better, if we knew what type of scroll-bar the adjacent
24220 windows (on either side) have... But we don't :-(
24221 However, I think this works ok. ++KFS 2003-04-25 */
24222
24223 /* Redraw borders between horizontally adjacent windows. Don't
24224 do it for frames with vertical scroll bars because either the
24225 right scroll bar of a window, or the left scroll bar of its
24226 neighbor will suffice as a border. */
24227 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24228 return;
24229
24230 if (!WINDOW_RIGHTMOST_P (w)
24231 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24232 {
24233 int x0, x1, y0, y1;
24234
24235 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24236 y1 -= 1;
24237
24238 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24239 x1 -= 1;
24240
24241 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24242 }
24243 else if (!WINDOW_LEFTMOST_P (w)
24244 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24245 {
24246 int x0, x1, y0, y1;
24247
24248 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24249 y1 -= 1;
24250
24251 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24252 x0 -= 1;
24253
24254 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24255 }
24256 }
24257
24258
24259 /* Redraw the part of window W intersection rectangle FR. Pixel
24260 coordinates in FR are frame-relative. Call this function with
24261 input blocked. Value is non-zero if the exposure overwrites
24262 mouse-face. */
24263
24264 static int
24265 expose_window (w, fr)
24266 struct window *w;
24267 XRectangle *fr;
24268 {
24269 struct frame *f = XFRAME (w->frame);
24270 XRectangle wr, r;
24271 int mouse_face_overwritten_p = 0;
24272
24273 /* If window is not yet fully initialized, do nothing. This can
24274 happen when toolkit scroll bars are used and a window is split.
24275 Reconfiguring the scroll bar will generate an expose for a newly
24276 created window. */
24277 if (w->current_matrix == NULL)
24278 return 0;
24279
24280 /* When we're currently updating the window, display and current
24281 matrix usually don't agree. Arrange for a thorough display
24282 later. */
24283 if (w == updated_window)
24284 {
24285 SET_FRAME_GARBAGED (f);
24286 return 0;
24287 }
24288
24289 /* Frame-relative pixel rectangle of W. */
24290 wr.x = WINDOW_LEFT_EDGE_X (w);
24291 wr.y = WINDOW_TOP_EDGE_Y (w);
24292 wr.width = WINDOW_TOTAL_WIDTH (w);
24293 wr.height = WINDOW_TOTAL_HEIGHT (w);
24294
24295 if (x_intersect_rectangles (fr, &wr, &r))
24296 {
24297 int yb = window_text_bottom_y (w);
24298 struct glyph_row *row;
24299 int cursor_cleared_p;
24300 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24301
24302 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24303 r.x, r.y, r.width, r.height));
24304
24305 /* Convert to window coordinates. */
24306 r.x -= WINDOW_LEFT_EDGE_X (w);
24307 r.y -= WINDOW_TOP_EDGE_Y (w);
24308
24309 /* Turn off the cursor. */
24310 if (!w->pseudo_window_p
24311 && phys_cursor_in_rect_p (w, &r))
24312 {
24313 x_clear_cursor (w);
24314 cursor_cleared_p = 1;
24315 }
24316 else
24317 cursor_cleared_p = 0;
24318
24319 /* Update lines intersecting rectangle R. */
24320 first_overlapping_row = last_overlapping_row = NULL;
24321 for (row = w->current_matrix->rows;
24322 row->enabled_p;
24323 ++row)
24324 {
24325 int y0 = row->y;
24326 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24327
24328 if ((y0 >= r.y && y0 < r.y + r.height)
24329 || (y1 > r.y && y1 < r.y + r.height)
24330 || (r.y >= y0 && r.y < y1)
24331 || (r.y + r.height > y0 && r.y + r.height < y1))
24332 {
24333 /* A header line may be overlapping, but there is no need
24334 to fix overlapping areas for them. KFS 2005-02-12 */
24335 if (row->overlapping_p && !row->mode_line_p)
24336 {
24337 if (first_overlapping_row == NULL)
24338 first_overlapping_row = row;
24339 last_overlapping_row = row;
24340 }
24341
24342 row->clip = fr;
24343 if (expose_line (w, row, &r))
24344 mouse_face_overwritten_p = 1;
24345 row->clip = NULL;
24346 }
24347 else if (row->overlapping_p)
24348 {
24349 /* We must redraw a row overlapping the exposed area. */
24350 if (y0 < r.y
24351 ? y0 + row->phys_height > r.y
24352 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24353 {
24354 if (first_overlapping_row == NULL)
24355 first_overlapping_row = row;
24356 last_overlapping_row = row;
24357 }
24358 }
24359
24360 if (y1 >= yb)
24361 break;
24362 }
24363
24364 /* Display the mode line if there is one. */
24365 if (WINDOW_WANTS_MODELINE_P (w)
24366 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24367 row->enabled_p)
24368 && row->y < r.y + r.height)
24369 {
24370 if (expose_line (w, row, &r))
24371 mouse_face_overwritten_p = 1;
24372 }
24373
24374 if (!w->pseudo_window_p)
24375 {
24376 /* Fix the display of overlapping rows. */
24377 if (first_overlapping_row)
24378 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24379 fr);
24380
24381 /* Draw border between windows. */
24382 x_draw_vertical_border (w);
24383
24384 /* Turn the cursor on again. */
24385 if (cursor_cleared_p)
24386 update_window_cursor (w, 1);
24387 }
24388 }
24389
24390 return mouse_face_overwritten_p;
24391 }
24392
24393
24394
24395 /* Redraw (parts) of all windows in the window tree rooted at W that
24396 intersect R. R contains frame pixel coordinates. Value is
24397 non-zero if the exposure overwrites mouse-face. */
24398
24399 static int
24400 expose_window_tree (w, r)
24401 struct window *w;
24402 XRectangle *r;
24403 {
24404 struct frame *f = XFRAME (w->frame);
24405 int mouse_face_overwritten_p = 0;
24406
24407 while (w && !FRAME_GARBAGED_P (f))
24408 {
24409 if (!NILP (w->hchild))
24410 mouse_face_overwritten_p
24411 |= expose_window_tree (XWINDOW (w->hchild), r);
24412 else if (!NILP (w->vchild))
24413 mouse_face_overwritten_p
24414 |= expose_window_tree (XWINDOW (w->vchild), r);
24415 else
24416 mouse_face_overwritten_p |= expose_window (w, r);
24417
24418 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24419 }
24420
24421 return mouse_face_overwritten_p;
24422 }
24423
24424
24425 /* EXPORT:
24426 Redisplay an exposed area of frame F. X and Y are the upper-left
24427 corner of the exposed rectangle. W and H are width and height of
24428 the exposed area. All are pixel values. W or H zero means redraw
24429 the entire frame. */
24430
24431 void
24432 expose_frame (f, x, y, w, h)
24433 struct frame *f;
24434 int x, y, w, h;
24435 {
24436 XRectangle r;
24437 int mouse_face_overwritten_p = 0;
24438
24439 TRACE ((stderr, "expose_frame "));
24440
24441 /* No need to redraw if frame will be redrawn soon. */
24442 if (FRAME_GARBAGED_P (f))
24443 {
24444 TRACE ((stderr, " garbaged\n"));
24445 return;
24446 }
24447
24448 /* If basic faces haven't been realized yet, there is no point in
24449 trying to redraw anything. This can happen when we get an expose
24450 event while Emacs is starting, e.g. by moving another window. */
24451 if (FRAME_FACE_CACHE (f) == NULL
24452 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24453 {
24454 TRACE ((stderr, " no faces\n"));
24455 return;
24456 }
24457
24458 if (w == 0 || h == 0)
24459 {
24460 r.x = r.y = 0;
24461 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24462 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24463 }
24464 else
24465 {
24466 r.x = x;
24467 r.y = y;
24468 r.width = w;
24469 r.height = h;
24470 }
24471
24472 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24473 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24474
24475 if (WINDOWP (f->tool_bar_window))
24476 mouse_face_overwritten_p
24477 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24478
24479 #ifdef HAVE_X_WINDOWS
24480 #ifndef MSDOS
24481 #ifndef USE_X_TOOLKIT
24482 if (WINDOWP (f->menu_bar_window))
24483 mouse_face_overwritten_p
24484 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24485 #endif /* not USE_X_TOOLKIT */
24486 #endif
24487 #endif
24488
24489 /* Some window managers support a focus-follows-mouse style with
24490 delayed raising of frames. Imagine a partially obscured frame,
24491 and moving the mouse into partially obscured mouse-face on that
24492 frame. The visible part of the mouse-face will be highlighted,
24493 then the WM raises the obscured frame. With at least one WM, KDE
24494 2.1, Emacs is not getting any event for the raising of the frame
24495 (even tried with SubstructureRedirectMask), only Expose events.
24496 These expose events will draw text normally, i.e. not
24497 highlighted. Which means we must redo the highlight here.
24498 Subsume it under ``we love X''. --gerd 2001-08-15 */
24499 /* Included in Windows version because Windows most likely does not
24500 do the right thing if any third party tool offers
24501 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24502 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24503 {
24504 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24505 if (f == dpyinfo->mouse_face_mouse_frame)
24506 {
24507 int x = dpyinfo->mouse_face_mouse_x;
24508 int y = dpyinfo->mouse_face_mouse_y;
24509 clear_mouse_face (dpyinfo);
24510 note_mouse_highlight (f, x, y);
24511 }
24512 }
24513 }
24514
24515
24516 /* EXPORT:
24517 Determine the intersection of two rectangles R1 and R2. Return
24518 the intersection in *RESULT. Value is non-zero if RESULT is not
24519 empty. */
24520
24521 int
24522 x_intersect_rectangles (r1, r2, result)
24523 XRectangle *r1, *r2, *result;
24524 {
24525 XRectangle *left, *right;
24526 XRectangle *upper, *lower;
24527 int intersection_p = 0;
24528
24529 /* Rearrange so that R1 is the left-most rectangle. */
24530 if (r1->x < r2->x)
24531 left = r1, right = r2;
24532 else
24533 left = r2, right = r1;
24534
24535 /* X0 of the intersection is right.x0, if this is inside R1,
24536 otherwise there is no intersection. */
24537 if (right->x <= left->x + left->width)
24538 {
24539 result->x = right->x;
24540
24541 /* The right end of the intersection is the minimum of the
24542 the right ends of left and right. */
24543 result->width = (min (left->x + left->width, right->x + right->width)
24544 - result->x);
24545
24546 /* Same game for Y. */
24547 if (r1->y < r2->y)
24548 upper = r1, lower = r2;
24549 else
24550 upper = r2, lower = r1;
24551
24552 /* The upper end of the intersection is lower.y0, if this is inside
24553 of upper. Otherwise, there is no intersection. */
24554 if (lower->y <= upper->y + upper->height)
24555 {
24556 result->y = lower->y;
24557
24558 /* The lower end of the intersection is the minimum of the lower
24559 ends of upper and lower. */
24560 result->height = (min (lower->y + lower->height,
24561 upper->y + upper->height)
24562 - result->y);
24563 intersection_p = 1;
24564 }
24565 }
24566
24567 return intersection_p;
24568 }
24569
24570 #endif /* HAVE_WINDOW_SYSTEM */
24571
24572 \f
24573 /***********************************************************************
24574 Initialization
24575 ***********************************************************************/
24576
24577 void
24578 syms_of_xdisp ()
24579 {
24580 Vwith_echo_area_save_vector = Qnil;
24581 staticpro (&Vwith_echo_area_save_vector);
24582
24583 Vmessage_stack = Qnil;
24584 staticpro (&Vmessage_stack);
24585
24586 Qinhibit_redisplay = intern ("inhibit-redisplay");
24587 staticpro (&Qinhibit_redisplay);
24588
24589 message_dolog_marker1 = Fmake_marker ();
24590 staticpro (&message_dolog_marker1);
24591 message_dolog_marker2 = Fmake_marker ();
24592 staticpro (&message_dolog_marker2);
24593 message_dolog_marker3 = Fmake_marker ();
24594 staticpro (&message_dolog_marker3);
24595
24596 #if GLYPH_DEBUG
24597 defsubr (&Sdump_frame_glyph_matrix);
24598 defsubr (&Sdump_glyph_matrix);
24599 defsubr (&Sdump_glyph_row);
24600 defsubr (&Sdump_tool_bar_row);
24601 defsubr (&Strace_redisplay);
24602 defsubr (&Strace_to_stderr);
24603 #endif
24604 #ifdef HAVE_WINDOW_SYSTEM
24605 defsubr (&Stool_bar_lines_needed);
24606 defsubr (&Slookup_image_map);
24607 #endif
24608 defsubr (&Sformat_mode_line);
24609 defsubr (&Sinvisible_p);
24610
24611 staticpro (&Qmenu_bar_update_hook);
24612 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24613
24614 staticpro (&Qoverriding_terminal_local_map);
24615 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24616
24617 staticpro (&Qoverriding_local_map);
24618 Qoverriding_local_map = intern ("overriding-local-map");
24619
24620 staticpro (&Qwindow_scroll_functions);
24621 Qwindow_scroll_functions = intern ("window-scroll-functions");
24622
24623 staticpro (&Qwindow_text_change_functions);
24624 Qwindow_text_change_functions = intern ("window-text-change-functions");
24625
24626 staticpro (&Qredisplay_end_trigger_functions);
24627 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24628
24629 staticpro (&Qinhibit_point_motion_hooks);
24630 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24631
24632 Qeval = intern ("eval");
24633 staticpro (&Qeval);
24634
24635 QCdata = intern (":data");
24636 staticpro (&QCdata);
24637 Qdisplay = intern ("display");
24638 staticpro (&Qdisplay);
24639 Qspace_width = intern ("space-width");
24640 staticpro (&Qspace_width);
24641 Qraise = intern ("raise");
24642 staticpro (&Qraise);
24643 Qslice = intern ("slice");
24644 staticpro (&Qslice);
24645 Qspace = intern ("space");
24646 staticpro (&Qspace);
24647 Qmargin = intern ("margin");
24648 staticpro (&Qmargin);
24649 Qpointer = intern ("pointer");
24650 staticpro (&Qpointer);
24651 Qleft_margin = intern ("left-margin");
24652 staticpro (&Qleft_margin);
24653 Qright_margin = intern ("right-margin");
24654 staticpro (&Qright_margin);
24655 Qcenter = intern ("center");
24656 staticpro (&Qcenter);
24657 Qline_height = intern ("line-height");
24658 staticpro (&Qline_height);
24659 QCalign_to = intern (":align-to");
24660 staticpro (&QCalign_to);
24661 QCrelative_width = intern (":relative-width");
24662 staticpro (&QCrelative_width);
24663 QCrelative_height = intern (":relative-height");
24664 staticpro (&QCrelative_height);
24665 QCeval = intern (":eval");
24666 staticpro (&QCeval);
24667 QCpropertize = intern (":propertize");
24668 staticpro (&QCpropertize);
24669 QCfile = intern (":file");
24670 staticpro (&QCfile);
24671 Qfontified = intern ("fontified");
24672 staticpro (&Qfontified);
24673 Qfontification_functions = intern ("fontification-functions");
24674 staticpro (&Qfontification_functions);
24675 Qtrailing_whitespace = intern ("trailing-whitespace");
24676 staticpro (&Qtrailing_whitespace);
24677 Qescape_glyph = intern ("escape-glyph");
24678 staticpro (&Qescape_glyph);
24679 Qnobreak_space = intern ("nobreak-space");
24680 staticpro (&Qnobreak_space);
24681 Qimage = intern ("image");
24682 staticpro (&Qimage);
24683 QCmap = intern (":map");
24684 staticpro (&QCmap);
24685 QCpointer = intern (":pointer");
24686 staticpro (&QCpointer);
24687 Qrect = intern ("rect");
24688 staticpro (&Qrect);
24689 Qcircle = intern ("circle");
24690 staticpro (&Qcircle);
24691 Qpoly = intern ("poly");
24692 staticpro (&Qpoly);
24693 Qmessage_truncate_lines = intern ("message-truncate-lines");
24694 staticpro (&Qmessage_truncate_lines);
24695 Qgrow_only = intern ("grow-only");
24696 staticpro (&Qgrow_only);
24697 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24698 staticpro (&Qinhibit_menubar_update);
24699 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24700 staticpro (&Qinhibit_eval_during_redisplay);
24701 Qposition = intern ("position");
24702 staticpro (&Qposition);
24703 Qbuffer_position = intern ("buffer-position");
24704 staticpro (&Qbuffer_position);
24705 Qobject = intern ("object");
24706 staticpro (&Qobject);
24707 Qbar = intern ("bar");
24708 staticpro (&Qbar);
24709 Qhbar = intern ("hbar");
24710 staticpro (&Qhbar);
24711 Qbox = intern ("box");
24712 staticpro (&Qbox);
24713 Qhollow = intern ("hollow");
24714 staticpro (&Qhollow);
24715 Qhand = intern ("hand");
24716 staticpro (&Qhand);
24717 Qarrow = intern ("arrow");
24718 staticpro (&Qarrow);
24719 Qtext = intern ("text");
24720 staticpro (&Qtext);
24721 Qrisky_local_variable = intern ("risky-local-variable");
24722 staticpro (&Qrisky_local_variable);
24723 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24724 staticpro (&Qinhibit_free_realized_faces);
24725
24726 list_of_error = Fcons (Fcons (intern ("error"),
24727 Fcons (intern ("void-variable"), Qnil)),
24728 Qnil);
24729 staticpro (&list_of_error);
24730
24731 Qlast_arrow_position = intern ("last-arrow-position");
24732 staticpro (&Qlast_arrow_position);
24733 Qlast_arrow_string = intern ("last-arrow-string");
24734 staticpro (&Qlast_arrow_string);
24735
24736 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24737 staticpro (&Qoverlay_arrow_string);
24738 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24739 staticpro (&Qoverlay_arrow_bitmap);
24740
24741 echo_buffer[0] = echo_buffer[1] = Qnil;
24742 staticpro (&echo_buffer[0]);
24743 staticpro (&echo_buffer[1]);
24744
24745 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24746 staticpro (&echo_area_buffer[0]);
24747 staticpro (&echo_area_buffer[1]);
24748
24749 Vmessages_buffer_name = build_string ("*Messages*");
24750 staticpro (&Vmessages_buffer_name);
24751
24752 mode_line_proptrans_alist = Qnil;
24753 staticpro (&mode_line_proptrans_alist);
24754 mode_line_string_list = Qnil;
24755 staticpro (&mode_line_string_list);
24756 mode_line_string_face = Qnil;
24757 staticpro (&mode_line_string_face);
24758 mode_line_string_face_prop = Qnil;
24759 staticpro (&mode_line_string_face_prop);
24760 Vmode_line_unwind_vector = Qnil;
24761 staticpro (&Vmode_line_unwind_vector);
24762
24763 help_echo_string = Qnil;
24764 staticpro (&help_echo_string);
24765 help_echo_object = Qnil;
24766 staticpro (&help_echo_object);
24767 help_echo_window = Qnil;
24768 staticpro (&help_echo_window);
24769 previous_help_echo_string = Qnil;
24770 staticpro (&previous_help_echo_string);
24771 help_echo_pos = -1;
24772
24773 #ifdef HAVE_WINDOW_SYSTEM
24774 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24775 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24776 For example, if a block cursor is over a tab, it will be drawn as
24777 wide as that tab on the display. */);
24778 x_stretch_cursor_p = 0;
24779 #endif
24780
24781 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24782 doc: /* *Non-nil means highlight trailing whitespace.
24783 The face used for trailing whitespace is `trailing-whitespace'. */);
24784 Vshow_trailing_whitespace = Qnil;
24785
24786 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24787 doc: /* *Control highlighting of nobreak space and soft hyphen.
24788 A value of t means highlight the character itself (for nobreak space,
24789 use face `nobreak-space').
24790 A value of nil means no highlighting.
24791 Other values mean display the escape glyph followed by an ordinary
24792 space or ordinary hyphen. */);
24793 Vnobreak_char_display = Qt;
24794
24795 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24796 doc: /* *The pointer shape to show in void text areas.
24797 A value of nil means to show the text pointer. Other options are `arrow',
24798 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24799 Vvoid_text_area_pointer = Qarrow;
24800
24801 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24802 doc: /* Non-nil means don't actually do any redisplay.
24803 This is used for internal purposes. */);
24804 Vinhibit_redisplay = Qnil;
24805
24806 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24807 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24808 Vglobal_mode_string = Qnil;
24809
24810 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24811 doc: /* Marker for where to display an arrow on top of the buffer text.
24812 This must be the beginning of a line in order to work.
24813 See also `overlay-arrow-string'. */);
24814 Voverlay_arrow_position = Qnil;
24815
24816 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24817 doc: /* String to display as an arrow in non-window frames.
24818 See also `overlay-arrow-position'. */);
24819 Voverlay_arrow_string = build_string ("=>");
24820
24821 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24822 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24823 The symbols on this list are examined during redisplay to determine
24824 where to display overlay arrows. */);
24825 Voverlay_arrow_variable_list
24826 = Fcons (intern ("overlay-arrow-position"), Qnil);
24827
24828 DEFVAR_INT ("scroll-step", &scroll_step,
24829 doc: /* *The number of lines to try scrolling a window by when point moves out.
24830 If that fails to bring point back on frame, point is centered instead.
24831 If this is zero, point is always centered after it moves off frame.
24832 If you want scrolling to always be a line at a time, you should set
24833 `scroll-conservatively' to a large value rather than set this to 1. */);
24834
24835 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24836 doc: /* *Scroll up to this many lines, to bring point back on screen.
24837 If point moves off-screen, redisplay will scroll by up to
24838 `scroll-conservatively' lines in order to bring point just barely
24839 onto the screen again. If that cannot be done, then redisplay
24840 recenters point as usual.
24841
24842 A value of zero means always recenter point if it moves off screen. */);
24843 scroll_conservatively = 0;
24844
24845 DEFVAR_INT ("scroll-margin", &scroll_margin,
24846 doc: /* *Number of lines of margin at the top and bottom of a window.
24847 Recenter the window whenever point gets within this many lines
24848 of the top or bottom of the window. */);
24849 scroll_margin = 0;
24850
24851 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24852 doc: /* Pixels per inch value for non-window system displays.
24853 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24854 Vdisplay_pixels_per_inch = make_float (72.0);
24855
24856 #if GLYPH_DEBUG
24857 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24858 #endif
24859
24860 DEFVAR_LISP ("truncate-partial-width-windows",
24861 &Vtruncate_partial_width_windows,
24862 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24863 For an integer value, truncate lines in each window with less than the
24864 full frame width, provided the window width is less than that integer;
24865 otherwise, respect the value of `truncate-lines'.
24866
24867 For any other non-nil value, truncate lines in all windows with
24868 less than the full frame width.
24869
24870 A value of nil means to respect the value of `truncate-lines'.
24871
24872 If `word-wrap' is enabled, you might want to reduce this. */);
24873 Vtruncate_partial_width_windows = make_number (50);
24874
24875 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24876 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24877 Any other value means to use the appropriate face, `mode-line',
24878 `header-line', or `menu' respectively. */);
24879 mode_line_inverse_video = 1;
24880
24881 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24882 doc: /* *Maximum buffer size for which line number should be displayed.
24883 If the buffer is bigger than this, the line number does not appear
24884 in the mode line. A value of nil means no limit. */);
24885 Vline_number_display_limit = Qnil;
24886
24887 DEFVAR_INT ("line-number-display-limit-width",
24888 &line_number_display_limit_width,
24889 doc: /* *Maximum line width (in characters) for line number display.
24890 If the average length of the lines near point is bigger than this, then the
24891 line number may be omitted from the mode line. */);
24892 line_number_display_limit_width = 200;
24893
24894 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24895 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24896 highlight_nonselected_windows = 0;
24897
24898 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24899 doc: /* Non-nil if more than one frame is visible on this display.
24900 Minibuffer-only frames don't count, but iconified frames do.
24901 This variable is not guaranteed to be accurate except while processing
24902 `frame-title-format' and `icon-title-format'. */);
24903
24904 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24905 doc: /* Template for displaying the title bar of visible frames.
24906 \(Assuming the window manager supports this feature.)
24907
24908 This variable has the same structure as `mode-line-format', except that
24909 the %c and %l constructs are ignored. It is used only on frames for
24910 which no explicit name has been set \(see `modify-frame-parameters'). */);
24911
24912 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24913 doc: /* Template for displaying the title bar of an iconified frame.
24914 \(Assuming the window manager supports this feature.)
24915 This variable has the same structure as `mode-line-format' (which see),
24916 and is used only on frames for which no explicit name has been set
24917 \(see `modify-frame-parameters'). */);
24918 Vicon_title_format
24919 = Vframe_title_format
24920 = Fcons (intern ("multiple-frames"),
24921 Fcons (build_string ("%b"),
24922 Fcons (Fcons (empty_unibyte_string,
24923 Fcons (intern ("invocation-name"),
24924 Fcons (build_string ("@"),
24925 Fcons (intern ("system-name"),
24926 Qnil)))),
24927 Qnil)));
24928
24929 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24930 doc: /* Maximum number of lines to keep in the message log buffer.
24931 If nil, disable message logging. If t, log messages but don't truncate
24932 the buffer when it becomes large. */);
24933 Vmessage_log_max = make_number (100);
24934
24935 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24936 doc: /* Functions called before redisplay, if window sizes have changed.
24937 The value should be a list of functions that take one argument.
24938 Just before redisplay, for each frame, if any of its windows have changed
24939 size since the last redisplay, or have been split or deleted,
24940 all the functions in the list are called, with the frame as argument. */);
24941 Vwindow_size_change_functions = Qnil;
24942
24943 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24944 doc: /* List of functions to call before redisplaying a window with scrolling.
24945 Each function is called with two arguments, the window and its new
24946 display-start position. Note that these functions are also called by
24947 `set-window-buffer'. Also note that the value of `window-end' is not
24948 valid when these functions are called. */);
24949 Vwindow_scroll_functions = Qnil;
24950
24951 DEFVAR_LISP ("window-text-change-functions",
24952 &Vwindow_text_change_functions,
24953 doc: /* Functions to call in redisplay when text in the window might change. */);
24954 Vwindow_text_change_functions = Qnil;
24955
24956 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24957 doc: /* Functions called when redisplay of a window reaches the end trigger.
24958 Each function is called with two arguments, the window and the end trigger value.
24959 See `set-window-redisplay-end-trigger'. */);
24960 Vredisplay_end_trigger_functions = Qnil;
24961
24962 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24963 doc: /* *Non-nil means autoselect window with mouse pointer.
24964 If nil, do not autoselect windows.
24965 A positive number means delay autoselection by that many seconds: a
24966 window is autoselected only after the mouse has remained in that
24967 window for the duration of the delay.
24968 A negative number has a similar effect, but causes windows to be
24969 autoselected only after the mouse has stopped moving. \(Because of
24970 the way Emacs compares mouse events, you will occasionally wait twice
24971 that time before the window gets selected.\)
24972 Any other value means to autoselect window instantaneously when the
24973 mouse pointer enters it.
24974
24975 Autoselection selects the minibuffer only if it is active, and never
24976 unselects the minibuffer if it is active.
24977
24978 When customizing this variable make sure that the actual value of
24979 `focus-follows-mouse' matches the behavior of your window manager. */);
24980 Vmouse_autoselect_window = Qnil;
24981
24982 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24983 doc: /* *Non-nil means automatically resize tool-bars.
24984 This dynamically changes the tool-bar's height to the minimum height
24985 that is needed to make all tool-bar items visible.
24986 If value is `grow-only', the tool-bar's height is only increased
24987 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24988 Vauto_resize_tool_bars = Qt;
24989
24990 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24991 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24992 auto_raise_tool_bar_buttons_p = 1;
24993
24994 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24995 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24996 make_cursor_line_fully_visible_p = 1;
24997
24998 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24999 doc: /* *Border below tool-bar in pixels.
25000 If an integer, use it as the height of the border.
25001 If it is one of `internal-border-width' or `border-width', use the
25002 value of the corresponding frame parameter.
25003 Otherwise, no border is added below the tool-bar. */);
25004 Vtool_bar_border = Qinternal_border_width;
25005
25006 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25007 doc: /* *Margin around tool-bar buttons in pixels.
25008 If an integer, use that for both horizontal and vertical margins.
25009 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25010 HORZ specifying the horizontal margin, and VERT specifying the
25011 vertical margin. */);
25012 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25013
25014 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25015 doc: /* *Relief thickness of tool-bar buttons. */);
25016 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25017
25018 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25019 doc: /* List of functions to call to fontify regions of text.
25020 Each function is called with one argument POS. Functions must
25021 fontify a region starting at POS in the current buffer, and give
25022 fontified regions the property `fontified'. */);
25023 Vfontification_functions = Qnil;
25024 Fmake_variable_buffer_local (Qfontification_functions);
25025
25026 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25027 &unibyte_display_via_language_environment,
25028 doc: /* *Non-nil means display unibyte text according to language environment.
25029 Specifically this means that unibyte non-ASCII characters
25030 are displayed by converting them to the equivalent multibyte characters
25031 according to the current language environment. As a result, they are
25032 displayed according to the current fontset. */);
25033 unibyte_display_via_language_environment = 0;
25034
25035 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25036 doc: /* *Maximum height for resizing mini-windows.
25037 If a float, it specifies a fraction of the mini-window frame's height.
25038 If an integer, it specifies a number of lines. */);
25039 Vmax_mini_window_height = make_float (0.25);
25040
25041 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25042 doc: /* *How to resize mini-windows.
25043 A value of nil means don't automatically resize mini-windows.
25044 A value of t means resize them to fit the text displayed in them.
25045 A value of `grow-only', the default, means let mini-windows grow
25046 only, until their display becomes empty, at which point the windows
25047 go back to their normal size. */);
25048 Vresize_mini_windows = Qgrow_only;
25049
25050 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25051 doc: /* Alist specifying how to blink the cursor off.
25052 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25053 `cursor-type' frame-parameter or variable equals ON-STATE,
25054 comparing using `equal', Emacs uses OFF-STATE to specify
25055 how to blink it off. ON-STATE and OFF-STATE are values for
25056 the `cursor-type' frame parameter.
25057
25058 If a frame's ON-STATE has no entry in this list,
25059 the frame's other specifications determine how to blink the cursor off. */);
25060 Vblink_cursor_alist = Qnil;
25061
25062 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25063 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25064 automatic_hscrolling_p = 1;
25065 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25066 staticpro (&Qauto_hscroll_mode);
25067
25068 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25069 doc: /* *How many columns away from the window edge point is allowed to get
25070 before automatic hscrolling will horizontally scroll the window. */);
25071 hscroll_margin = 5;
25072
25073 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25074 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25075 When point is less than `hscroll-margin' columns from the window
25076 edge, automatic hscrolling will scroll the window by the amount of columns
25077 determined by this variable. If its value is a positive integer, scroll that
25078 many columns. If it's a positive floating-point number, it specifies the
25079 fraction of the window's width to scroll. If it's nil or zero, point will be
25080 centered horizontally after the scroll. Any other value, including negative
25081 numbers, are treated as if the value were zero.
25082
25083 Automatic hscrolling always moves point outside the scroll margin, so if
25084 point was more than scroll step columns inside the margin, the window will
25085 scroll more than the value given by the scroll step.
25086
25087 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25088 and `scroll-right' overrides this variable's effect. */);
25089 Vhscroll_step = make_number (0);
25090
25091 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25092 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25093 Bind this around calls to `message' to let it take effect. */);
25094 message_truncate_lines = 0;
25095
25096 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25097 doc: /* Normal hook run to update the menu bar definitions.
25098 Redisplay runs this hook before it redisplays the menu bar.
25099 This is used to update submenus such as Buffers,
25100 whose contents depend on various data. */);
25101 Vmenu_bar_update_hook = Qnil;
25102
25103 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25104 doc: /* Frame for which we are updating a menu.
25105 The enable predicate for a menu binding should check this variable. */);
25106 Vmenu_updating_frame = Qnil;
25107
25108 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25109 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25110 inhibit_menubar_update = 0;
25111
25112 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25113 doc: /* Prefix prepended to all continuation lines at display time.
25114 The value may be a string, an image, or a stretch-glyph; it is
25115 interpreted in the same way as the value of a `display' text property.
25116
25117 This variable is overridden by any `wrap-prefix' text or overlay
25118 property.
25119
25120 To add a prefix to non-continuation lines, use `line-prefix'. */);
25121 Vwrap_prefix = Qnil;
25122 staticpro (&Qwrap_prefix);
25123 Qwrap_prefix = intern ("wrap-prefix");
25124 Fmake_variable_buffer_local (Qwrap_prefix);
25125
25126 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25127 doc: /* Prefix prepended to all non-continuation lines at display time.
25128 The value may be a string, an image, or a stretch-glyph; it is
25129 interpreted in the same way as the value of a `display' text property.
25130
25131 This variable is overridden by any `line-prefix' text or overlay
25132 property.
25133
25134 To add a prefix to continuation lines, use `wrap-prefix'. */);
25135 Vline_prefix = Qnil;
25136 staticpro (&Qline_prefix);
25137 Qline_prefix = intern ("line-prefix");
25138 Fmake_variable_buffer_local (Qline_prefix);
25139
25140 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25141 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25142 inhibit_eval_during_redisplay = 0;
25143
25144 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25145 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25146 inhibit_free_realized_faces = 0;
25147
25148 #if GLYPH_DEBUG
25149 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25150 doc: /* Inhibit try_window_id display optimization. */);
25151 inhibit_try_window_id = 0;
25152
25153 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25154 doc: /* Inhibit try_window_reusing display optimization. */);
25155 inhibit_try_window_reusing = 0;
25156
25157 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25158 doc: /* Inhibit try_cursor_movement display optimization. */);
25159 inhibit_try_cursor_movement = 0;
25160 #endif /* GLYPH_DEBUG */
25161
25162 DEFVAR_INT ("overline-margin", &overline_margin,
25163 doc: /* *Space between overline and text, in pixels.
25164 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25165 margin to the caracter height. */);
25166 overline_margin = 2;
25167
25168 DEFVAR_INT ("underline-minimum-offset",
25169 &underline_minimum_offset,
25170 doc: /* Minimum distance between baseline and underline.
25171 This can improve legibility of underlined text at small font sizes,
25172 particularly when using variable `x-use-underline-position-properties'
25173 with fonts that specify an UNDERLINE_POSITION relatively close to the
25174 baseline. The default value is 1. */);
25175 underline_minimum_offset = 1;
25176
25177 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25178 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25179 display_hourglass_p = 1;
25180
25181 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25182 doc: /* *Seconds to wait before displaying an hourglass pointer.
25183 Value must be an integer or float. */);
25184 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25185
25186 hourglass_atimer = NULL;
25187 hourglass_shown_p = 0;
25188 }
25189
25190
25191 /* Initialize this module when Emacs starts. */
25192
25193 void
25194 init_xdisp ()
25195 {
25196 Lisp_Object root_window;
25197 struct window *mini_w;
25198
25199 current_header_line_height = current_mode_line_height = -1;
25200
25201 CHARPOS (this_line_start_pos) = 0;
25202
25203 mini_w = XWINDOW (minibuf_window);
25204 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25205
25206 if (!noninteractive)
25207 {
25208 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25209 int i;
25210
25211 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25212 set_window_height (root_window,
25213 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25214 0);
25215 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25216 set_window_height (minibuf_window, 1, 0);
25217
25218 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25219 mini_w->total_cols = make_number (FRAME_COLS (f));
25220
25221 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25222 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25223 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25224
25225 /* The default ellipsis glyphs `...'. */
25226 for (i = 0; i < 3; ++i)
25227 default_invis_vector[i] = make_number ('.');
25228 }
25229
25230 {
25231 /* Allocate the buffer for frame titles.
25232 Also used for `format-mode-line'. */
25233 int size = 100;
25234 mode_line_noprop_buf = (char *) xmalloc (size);
25235 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25236 mode_line_noprop_ptr = mode_line_noprop_buf;
25237 mode_line_target = MODE_LINE_DISPLAY;
25238 }
25239
25240 help_echo_showing_p = 0;
25241 }
25242
25243 /* Since w32 does not support atimers, it defines its own implementation of
25244 the following three functions in w32fns.c. */
25245 #ifndef WINDOWSNT
25246
25247 /* Platform-independent portion of hourglass implementation. */
25248
25249 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25250 int
25251 hourglass_started ()
25252 {
25253 return hourglass_shown_p || hourglass_atimer != NULL;
25254 }
25255
25256 /* Cancel a currently active hourglass timer, and start a new one. */
25257 void
25258 start_hourglass ()
25259 {
25260 #if defined (HAVE_WINDOW_SYSTEM)
25261 EMACS_TIME delay;
25262 int secs, usecs = 0;
25263
25264 cancel_hourglass ();
25265
25266 if (INTEGERP (Vhourglass_delay)
25267 && XINT (Vhourglass_delay) > 0)
25268 secs = XFASTINT (Vhourglass_delay);
25269 else if (FLOATP (Vhourglass_delay)
25270 && XFLOAT_DATA (Vhourglass_delay) > 0)
25271 {
25272 Lisp_Object tem;
25273 tem = Ftruncate (Vhourglass_delay, Qnil);
25274 secs = XFASTINT (tem);
25275 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25276 }
25277 else
25278 secs = DEFAULT_HOURGLASS_DELAY;
25279
25280 EMACS_SET_SECS_USECS (delay, secs, usecs);
25281 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25282 show_hourglass, NULL);
25283 #endif
25284 }
25285
25286
25287 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25288 shown. */
25289 void
25290 cancel_hourglass ()
25291 {
25292 #if defined (HAVE_WINDOW_SYSTEM)
25293 if (hourglass_atimer)
25294 {
25295 cancel_atimer (hourglass_atimer);
25296 hourglass_atimer = NULL;
25297 }
25298
25299 if (hourglass_shown_p)
25300 hide_hourglass ();
25301 #endif
25302 }
25303 #endif /* ! WINDOWSNT */
25304
25305 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25306 (do not change this comment) */