Merge from emacs--devo--0
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 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 2, or (at your option)
11 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; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.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 MAC_OS
202 #include "macterm.h"
203 #endif
204
205 #ifdef HAVE_WINDOW_SYSTEM
206 #ifdef USE_FONT_BACKEND
207 #include "font.h"
208 #endif /* USE_FONT_BACKEND */
209 #endif /* HAVE_WINDOW_SYSTEM */
210
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
214
215 #define INFINITY 10000000
216
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
218 || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
222
223 extern int interrupt_input;
224 extern int command_loop_level;
225
226 extern Lisp_Object do_mouse_tracking;
227
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
230
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
243 Lisp_Object Qinhibit_point_motion_hooks;
244 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
245 Lisp_Object Qfontified;
246 Lisp_Object Qgrow_only;
247 Lisp_Object Qinhibit_eval_during_redisplay;
248 Lisp_Object Qbuffer_position, Qposition, Qobject;
249
250 /* Cursor shapes */
251 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
252
253 /* Pointer shapes */
254 Lisp_Object Qarrow, Qhand, Qtext;
255
256 Lisp_Object Qrisky_local_variable;
257
258 /* Holds the list (error). */
259 Lisp_Object list_of_error;
260
261 /* Functions called to fontify regions of text. */
262
263 Lisp_Object Vfontification_functions;
264 Lisp_Object Qfontification_functions;
265
266 /* Non-nil means automatically select any window when the mouse
267 cursor moves into it. */
268 Lisp_Object Vmouse_autoselect_window;
269
270 /* Non-zero means draw tool bar buttons raised when the mouse moves
271 over them. */
272
273 int auto_raise_tool_bar_buttons_p;
274
275 /* Non-zero means to reposition window if cursor line is only partially visible. */
276
277 int make_cursor_line_fully_visible_p;
278
279 /* Margin below tool bar in pixels. 0 or nil means no margin.
280 If value is `internal-border-width' or `border-width',
281 the corresponding frame parameter is used. */
282
283 Lisp_Object Vtool_bar_border;
284
285 /* Margin around tool bar buttons in pixels. */
286
287 Lisp_Object Vtool_bar_button_margin;
288
289 /* Thickness of shadow to draw around tool bar buttons. */
290
291 EMACS_INT tool_bar_button_relief;
292
293 /* Non-nil means automatically resize tool-bars so that all tool-bar
294 items are visible, and no blank lines remain.
295
296 If value is `grow-only', only make tool-bar bigger. */
297
298 Lisp_Object Vauto_resize_tool_bars;
299
300 /* Non-zero means draw block and hollow cursor as wide as the glyph
301 under it. For example, if a block cursor is over a tab, it will be
302 drawn as wide as that tab on the display. */
303
304 int x_stretch_cursor_p;
305
306 /* Non-nil means don't actually do any redisplay. */
307
308 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
309
310 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
311
312 int inhibit_eval_during_redisplay;
313
314 /* Names of text properties relevant for redisplay. */
315
316 Lisp_Object Qdisplay;
317 extern Lisp_Object Qface, Qinvisible, Qwidth;
318
319 /* Symbols used in text property values. */
320
321 Lisp_Object Vdisplay_pixels_per_inch;
322 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
323 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
324 Lisp_Object Qslice;
325 Lisp_Object Qcenter;
326 Lisp_Object Qmargin, Qpointer;
327 Lisp_Object Qline_height;
328 extern Lisp_Object Qheight;
329 extern Lisp_Object QCwidth, QCheight, QCascent;
330 extern Lisp_Object Qscroll_bar;
331 extern Lisp_Object Qcursor;
332
333 /* Non-nil means highlight trailing whitespace. */
334
335 Lisp_Object Vshow_trailing_whitespace;
336
337 /* Non-nil means escape non-break space and hyphens. */
338
339 Lisp_Object Vnobreak_char_display;
340
341 #ifdef HAVE_WINDOW_SYSTEM
342 extern Lisp_Object Voverflow_newline_into_fringe;
343
344 /* Test if overflow newline into fringe. Called with iterator IT
345 at or past right window margin, and with IT->current_x set. */
346
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
348 (!NILP (Voverflow_newline_into_fringe) \
349 && FRAME_WINDOW_P (it->f) \
350 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
351 && it->current_x == it->last_visible_x)
352
353 #endif /* HAVE_WINDOW_SYSTEM */
354
355 /* Non-nil means show the text cursor in void text areas
356 i.e. in blank areas after eol and eob. This used to be
357 the default in 21.3. */
358
359 Lisp_Object Vvoid_text_area_pointer;
360
361 /* Name of the face used to highlight trailing whitespace. */
362
363 Lisp_Object Qtrailing_whitespace;
364
365 /* Name and number of the face used to highlight escape glyphs. */
366
367 Lisp_Object Qescape_glyph;
368
369 /* Name and number of the face used to highlight non-breaking spaces. */
370
371 Lisp_Object Qnobreak_space;
372
373 /* The symbol `image' which is the car of the lists used to represent
374 images in Lisp. */
375
376 Lisp_Object Qimage;
377
378 /* The image map types. */
379 Lisp_Object QCmap, QCpointer;
380 Lisp_Object Qrect, Qcircle, Qpoly;
381
382 /* Non-zero means print newline to stdout before next mini-buffer
383 message. */
384
385 int noninteractive_need_newline;
386
387 /* Non-zero means print newline to message log before next message. */
388
389 static int message_log_need_newline;
390
391 /* Three markers that message_dolog uses.
392 It could allocate them itself, but that causes trouble
393 in handling memory-full errors. */
394 static Lisp_Object message_dolog_marker1;
395 static Lisp_Object message_dolog_marker2;
396 static Lisp_Object message_dolog_marker3;
397 \f
398 /* The buffer position of the first character appearing entirely or
399 partially on the line of the selected window which contains the
400 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
401 redisplay optimization in redisplay_internal. */
402
403 static struct text_pos this_line_start_pos;
404
405 /* Number of characters past the end of the line above, including the
406 terminating newline. */
407
408 static struct text_pos this_line_end_pos;
409
410 /* The vertical positions and the height of this line. */
411
412 static int this_line_vpos;
413 static int this_line_y;
414 static int this_line_pixel_height;
415
416 /* X position at which this display line starts. Usually zero;
417 negative if first character is partially visible. */
418
419 static int this_line_start_x;
420
421 /* Buffer that this_line_.* variables are referring to. */
422
423 static struct buffer *this_line_buffer;
424
425 /* Nonzero means truncate lines in all windows less wide than the
426 frame. */
427
428 int truncate_partial_width_windows;
429
430 /* A flag to control how to display unibyte 8-bit character. */
431
432 int unibyte_display_via_language_environment;
433
434 /* Nonzero means we have more than one non-mini-buffer-only frame.
435 Not guaranteed to be accurate except while parsing
436 frame-title-format. */
437
438 int multiple_frames;
439
440 Lisp_Object Vglobal_mode_string;
441
442
443 /* List of variables (symbols) which hold markers for overlay arrows.
444 The symbols on this list are examined during redisplay to determine
445 where to display overlay arrows. */
446
447 Lisp_Object Voverlay_arrow_variable_list;
448
449 /* Marker for where to display an arrow on top of the buffer text. */
450
451 Lisp_Object Voverlay_arrow_position;
452
453 /* String to display for the arrow. Only used on terminal frames. */
454
455 Lisp_Object Voverlay_arrow_string;
456
457 /* Values of those variables at last redisplay are stored as
458 properties on `overlay-arrow-position' symbol. However, if
459 Voverlay_arrow_position is a marker, last-arrow-position is its
460 numerical position. */
461
462 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
463
464 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
465 properties on a symbol in overlay-arrow-variable-list. */
466
467 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
468
469 /* Like mode-line-format, but for the title bar on a visible frame. */
470
471 Lisp_Object Vframe_title_format;
472
473 /* Like mode-line-format, but for the title bar on an iconified frame. */
474
475 Lisp_Object Vicon_title_format;
476
477 /* List of functions to call when a window's size changes. These
478 functions get one arg, a frame on which one or more windows' sizes
479 have changed. */
480
481 static Lisp_Object Vwindow_size_change_functions;
482
483 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
484
485 /* Nonzero if an overlay arrow has been displayed in this window. */
486
487 static int overlay_arrow_seen;
488
489 /* Nonzero means highlight the region even in nonselected windows. */
490
491 int highlight_nonselected_windows;
492
493 /* If cursor motion alone moves point off frame, try scrolling this
494 many lines up or down if that will bring it back. */
495
496 static EMACS_INT scroll_step;
497
498 /* Nonzero means scroll just far enough to bring point back on the
499 screen, when appropriate. */
500
501 static EMACS_INT scroll_conservatively;
502
503 /* Recenter the window whenever point gets within this many lines of
504 the top or bottom of the window. This value is translated into a
505 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
506 that there is really a fixed pixel height scroll margin. */
507
508 EMACS_INT scroll_margin;
509
510 /* Number of windows showing the buffer of the selected window (or
511 another buffer with the same base buffer). keyboard.c refers to
512 this. */
513
514 int buffer_shared;
515
516 /* Vector containing glyphs for an ellipsis `...'. */
517
518 static Lisp_Object default_invis_vector[3];
519
520 /* Zero means display the mode-line/header-line/menu-bar in the default face
521 (this slightly odd definition is for compatibility with previous versions
522 of emacs), non-zero means display them using their respective faces.
523
524 This variable is deprecated. */
525
526 int mode_line_inverse_video;
527
528 /* Prompt to display in front of the mini-buffer contents. */
529
530 Lisp_Object minibuf_prompt;
531
532 /* Width of current mini-buffer prompt. Only set after display_line
533 of the line that contains the prompt. */
534
535 int minibuf_prompt_width;
536
537 /* This is the window where the echo area message was displayed. It
538 is always a mini-buffer window, but it may not be the same window
539 currently active as a mini-buffer. */
540
541 Lisp_Object echo_area_window;
542
543 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
544 pushes the current message and the value of
545 message_enable_multibyte on the stack, the function restore_message
546 pops the stack and displays MESSAGE again. */
547
548 Lisp_Object Vmessage_stack;
549
550 /* Nonzero means multibyte characters were enabled when the echo area
551 message was specified. */
552
553 int message_enable_multibyte;
554
555 /* Nonzero if we should redraw the mode lines on the next redisplay. */
556
557 int update_mode_lines;
558
559 /* Nonzero if window sizes or contents have changed since last
560 redisplay that finished. */
561
562 int windows_or_buffers_changed;
563
564 /* Nonzero means a frame's cursor type has been changed. */
565
566 int cursor_type_changed;
567
568 /* Nonzero after display_mode_line if %l was used and it displayed a
569 line number. */
570
571 int line_number_displayed;
572
573 /* Maximum buffer size for which to display line numbers. */
574
575 Lisp_Object Vline_number_display_limit;
576
577 /* Line width to consider when repositioning for line number display. */
578
579 static EMACS_INT line_number_display_limit_width;
580
581 /* Number of lines to keep in the message log buffer. t means
582 infinite. nil means don't log at all. */
583
584 Lisp_Object Vmessage_log_max;
585
586 /* The name of the *Messages* buffer, a string. */
587
588 static Lisp_Object Vmessages_buffer_name;
589
590 /* Index 0 is the buffer that holds the current (desired) echo area message,
591 or nil if none is desired right now.
592
593 Index 1 is the buffer that holds the previously displayed echo area message,
594 or nil to indicate no message. This is normally what's on the screen now.
595
596 These two can point to the same buffer. That happens when the last
597 message output by the user (or made by echoing) has been displayed. */
598
599 Lisp_Object echo_area_buffer[2];
600
601 /* Permanent pointers to the two buffers that are used for echo area
602 purposes. Once the two buffers are made, and their pointers are
603 placed here, these two slots remain unchanged unless those buffers
604 need to be created afresh. */
605
606 static Lisp_Object echo_buffer[2];
607
608 /* A vector saved used in with_area_buffer to reduce consing. */
609
610 static Lisp_Object Vwith_echo_area_save_vector;
611
612 /* Non-zero means display_echo_area should display the last echo area
613 message again. Set by redisplay_preserve_echo_area. */
614
615 static int display_last_displayed_message_p;
616
617 /* Nonzero if echo area is being used by print; zero if being used by
618 message. */
619
620 int message_buf_print;
621
622 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
623
624 Lisp_Object Qinhibit_menubar_update;
625 int inhibit_menubar_update;
626
627 /* When evaluating expressions from menu bar items (enable conditions,
628 for instance), this is the frame they are being processed for. */
629
630 Lisp_Object Vmenu_updating_frame;
631
632 /* Maximum height for resizing mini-windows. Either a float
633 specifying a fraction of the available height, or an integer
634 specifying a number of lines. */
635
636 Lisp_Object Vmax_mini_window_height;
637
638 /* Non-zero means messages should be displayed with truncated
639 lines instead of being continued. */
640
641 int message_truncate_lines;
642 Lisp_Object Qmessage_truncate_lines;
643
644 /* Set to 1 in clear_message to make redisplay_internal aware
645 of an emptied echo area. */
646
647 static int message_cleared_p;
648
649 /* How to blink the default frame cursor off. */
650 Lisp_Object Vblink_cursor_alist;
651
652 /* A scratch glyph row with contents used for generating truncation
653 glyphs. Also used in direct_output_for_insert. */
654
655 #define MAX_SCRATCH_GLYPHS 100
656 struct glyph_row scratch_glyph_row;
657 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
658
659 /* Ascent and height of the last line processed by move_it_to. */
660
661 static int last_max_ascent, last_height;
662
663 /* Non-zero if there's a help-echo in the echo area. */
664
665 int help_echo_showing_p;
666
667 /* If >= 0, computed, exact values of mode-line and header-line height
668 to use in the macros CURRENT_MODE_LINE_HEIGHT and
669 CURRENT_HEADER_LINE_HEIGHT. */
670
671 int current_mode_line_height, current_header_line_height;
672
673 /* The maximum distance to look ahead for text properties. Values
674 that are too small let us call compute_char_face and similar
675 functions too often which is expensive. Values that are too large
676 let us call compute_char_face and alike too often because we
677 might not be interested in text properties that far away. */
678
679 #define TEXT_PROP_DISTANCE_LIMIT 100
680
681 #if GLYPH_DEBUG
682
683 /* Variables to turn off display optimizations from Lisp. */
684
685 int inhibit_try_window_id, inhibit_try_window_reusing;
686 int inhibit_try_cursor_movement;
687
688 /* Non-zero means print traces of redisplay if compiled with
689 GLYPH_DEBUG != 0. */
690
691 int trace_redisplay_p;
692
693 #endif /* GLYPH_DEBUG */
694
695 #ifdef DEBUG_TRACE_MOVE
696 /* Non-zero means trace with TRACE_MOVE to stderr. */
697 int trace_move;
698
699 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
700 #else
701 #define TRACE_MOVE(x) (void) 0
702 #endif
703
704 /* Non-zero means automatically scroll windows horizontally to make
705 point visible. */
706
707 int automatic_hscrolling_p;
708
709 /* How close to the margin can point get before the window is scrolled
710 horizontally. */
711 EMACS_INT hscroll_margin;
712
713 /* How much to scroll horizontally when point is inside the above margin. */
714 Lisp_Object Vhscroll_step;
715
716 /* The variable `resize-mini-windows'. If nil, don't resize
717 mini-windows. If t, always resize them to fit the text they
718 display. If `grow-only', let mini-windows grow only until they
719 become empty. */
720
721 Lisp_Object Vresize_mini_windows;
722
723 /* Buffer being redisplayed -- for redisplay_window_error. */
724
725 struct buffer *displayed_buffer;
726
727 /* Space between overline and text. */
728
729 EMACS_INT overline_margin;
730
731 /* Value returned from text property handlers (see below). */
732
733 enum prop_handled
734 {
735 HANDLED_NORMALLY,
736 HANDLED_RECOMPUTE_PROPS,
737 HANDLED_OVERLAY_STRING_CONSUMED,
738 HANDLED_RETURN
739 };
740
741 /* A description of text properties that redisplay is interested
742 in. */
743
744 struct props
745 {
746 /* The name of the property. */
747 Lisp_Object *name;
748
749 /* A unique index for the property. */
750 enum prop_idx idx;
751
752 /* A handler function called to set up iterator IT from the property
753 at IT's current position. Value is used to steer handle_stop. */
754 enum prop_handled (*handler) P_ ((struct it *it));
755 };
756
757 static enum prop_handled handle_face_prop P_ ((struct it *));
758 static enum prop_handled handle_invisible_prop P_ ((struct it *));
759 static enum prop_handled handle_display_prop P_ ((struct it *));
760 static enum prop_handled handle_composition_prop P_ ((struct it *));
761 static enum prop_handled handle_overlay_change P_ ((struct it *));
762 static enum prop_handled handle_fontified_prop P_ ((struct it *));
763 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
764
765 /* Properties handled by iterators. */
766
767 static struct props it_props[] =
768 {
769 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
770 /* Handle `face' before `display' because some sub-properties of
771 `display' need to know the face. */
772 {&Qface, FACE_PROP_IDX, handle_face_prop},
773 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
774 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
775 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
776 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
777 {NULL, 0, NULL}
778 };
779
780 /* Value is the position described by X. If X is a marker, value is
781 the marker_position of X. Otherwise, value is X. */
782
783 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
784
785 /* Enumeration returned by some move_it_.* functions internally. */
786
787 enum move_it_result
788 {
789 /* Not used. Undefined value. */
790 MOVE_UNDEFINED,
791
792 /* Move ended at the requested buffer position or ZV. */
793 MOVE_POS_MATCH_OR_ZV,
794
795 /* Move ended at the requested X pixel position. */
796 MOVE_X_REACHED,
797
798 /* Move within a line ended at the end of a line that must be
799 continued. */
800 MOVE_LINE_CONTINUED,
801
802 /* Move within a line ended at the end of a line that would
803 be displayed truncated. */
804 MOVE_LINE_TRUNCATED,
805
806 /* Move within a line ended at a line end. */
807 MOVE_NEWLINE_OR_CR
808 };
809
810 /* This counter is used to clear the face cache every once in a while
811 in redisplay_internal. It is incremented for each redisplay.
812 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
813 cleared. */
814
815 #define CLEAR_FACE_CACHE_COUNT 500
816 static int clear_face_cache_count;
817
818 /* Similarly for the image cache. */
819
820 #ifdef HAVE_WINDOW_SYSTEM
821 #define CLEAR_IMAGE_CACHE_COUNT 101
822 static int clear_image_cache_count;
823 #endif
824
825 /* Record the previous terminal frame we displayed. */
826
827 static struct frame *previous_terminal_frame;
828
829 /* Non-zero while redisplay_internal is in progress. */
830
831 int redisplaying_p;
832
833 /* Non-zero means don't free realized faces. Bound while freeing
834 realized faces is dangerous because glyph matrices might still
835 reference them. */
836
837 int inhibit_free_realized_faces;
838 Lisp_Object Qinhibit_free_realized_faces;
839
840 /* If a string, XTread_socket generates an event to display that string.
841 (The display is done in read_char.) */
842
843 Lisp_Object help_echo_string;
844 Lisp_Object help_echo_window;
845 Lisp_Object help_echo_object;
846 int help_echo_pos;
847
848 /* Temporary variable for XTread_socket. */
849
850 Lisp_Object previous_help_echo_string;
851
852 /* Null glyph slice */
853
854 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
855
856 \f
857 /* Function prototypes. */
858
859 static void setup_for_ellipsis P_ ((struct it *, int));
860 static void mark_window_display_accurate_1 P_ ((struct window *, int));
861 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
862 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
863 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
864 static int redisplay_mode_lines P_ ((Lisp_Object, int));
865 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
866
867 #if 0
868 static int invisible_text_between_p P_ ((struct it *, int, int));
869 #endif
870
871 static void pint2str P_ ((char *, int, int));
872 static void pint2hrstr P_ ((char *, int, int));
873 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
874 struct text_pos));
875 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
876 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
877 static void store_mode_line_noprop_char P_ ((char));
878 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
879 static void x_consider_frame_title P_ ((Lisp_Object));
880 static void handle_stop P_ ((struct it *));
881 static int tool_bar_lines_needed P_ ((struct frame *, int *));
882 static int single_display_spec_intangible_p P_ ((Lisp_Object));
883 static void ensure_echo_area_buffers P_ ((void));
884 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
885 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
886 static int with_echo_area_buffer P_ ((struct window *, int,
887 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
888 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
889 static void clear_garbaged_frames P_ ((void));
890 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
891 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
892 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
893 static int display_echo_area P_ ((struct window *));
894 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
895 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
896 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
897 static int string_char_and_length P_ ((const unsigned char *, int, int *));
898 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
899 struct text_pos));
900 static int compute_window_start_on_continuation_line P_ ((struct window *));
901 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
902 static void insert_left_trunc_glyphs P_ ((struct it *));
903 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
904 Lisp_Object));
905 static void extend_face_to_end_of_line P_ ((struct it *));
906 static int append_space_for_newline P_ ((struct it *, int));
907 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
908 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
909 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
910 static int trailing_whitespace_p P_ ((int));
911 static int message_log_check_duplicate P_ ((int, int, int, int));
912 static void push_it P_ ((struct it *));
913 static void pop_it P_ ((struct it *));
914 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
915 static void select_frame_for_redisplay P_ ((Lisp_Object));
916 static void redisplay_internal P_ ((int));
917 static int echo_area_display P_ ((int));
918 static void redisplay_windows P_ ((Lisp_Object));
919 static void redisplay_window P_ ((Lisp_Object, int));
920 static Lisp_Object redisplay_window_error ();
921 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
922 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
923 static int update_menu_bar P_ ((struct frame *, int, int));
924 static int try_window_reusing_current_matrix P_ ((struct window *));
925 static int try_window_id P_ ((struct window *));
926 static int display_line P_ ((struct it *));
927 static int display_mode_lines P_ ((struct window *));
928 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
929 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
930 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
931 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
932 static void display_menu_bar P_ ((struct window *));
933 static int display_count_lines P_ ((int, int, int, int, int *));
934 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
935 int, int, struct it *, int, int, int, int));
936 static void compute_line_metrics P_ ((struct it *));
937 static void run_redisplay_end_trigger_hook P_ ((struct it *));
938 static int get_overlay_strings P_ ((struct it *, int));
939 static int get_overlay_strings_1 P_ ((struct it *, int, int));
940 static void next_overlay_string P_ ((struct it *));
941 static void reseat P_ ((struct it *, struct text_pos, int));
942 static void reseat_1 P_ ((struct it *, struct text_pos, int));
943 static void back_to_previous_visible_line_start P_ ((struct it *));
944 void reseat_at_previous_visible_line_start P_ ((struct it *));
945 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
946 static int next_element_from_ellipsis P_ ((struct it *));
947 static int next_element_from_display_vector P_ ((struct it *));
948 static int next_element_from_string P_ ((struct it *));
949 static int next_element_from_c_string P_ ((struct it *));
950 static int next_element_from_buffer P_ ((struct it *));
951 static int next_element_from_composition P_ ((struct it *));
952 static int next_element_from_image P_ ((struct it *));
953 static int next_element_from_stretch P_ ((struct it *));
954 static void load_overlay_strings P_ ((struct it *, int));
955 static int init_from_display_pos P_ ((struct it *, struct window *,
956 struct display_pos *));
957 static void reseat_to_string P_ ((struct it *, unsigned char *,
958 Lisp_Object, int, int, int, int));
959 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
960 int, int, int));
961 void move_it_vertically_backward P_ ((struct it *, int));
962 static void init_to_row_start P_ ((struct it *, struct window *,
963 struct glyph_row *));
964 static int init_to_row_end P_ ((struct it *, struct window *,
965 struct glyph_row *));
966 static void back_to_previous_line_start P_ ((struct it *));
967 static int forward_to_next_line_start P_ ((struct it *, int *));
968 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
969 Lisp_Object, int));
970 static struct text_pos string_pos P_ ((int, Lisp_Object));
971 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
972 static int number_of_chars P_ ((unsigned char *, int));
973 static void compute_stop_pos P_ ((struct it *));
974 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
975 Lisp_Object));
976 static int face_before_or_after_it_pos P_ ((struct it *, int));
977 static int next_overlay_change P_ ((int));
978 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
979 Lisp_Object, struct text_pos *,
980 int));
981 static int underlying_face_id P_ ((struct it *));
982 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
983 struct window *));
984
985 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
986 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
987
988 #ifdef HAVE_WINDOW_SYSTEM
989
990 static void update_tool_bar P_ ((struct frame *, int));
991 static void build_desired_tool_bar_string P_ ((struct frame *f));
992 static int redisplay_tool_bar P_ ((struct frame *));
993 static void display_tool_bar_line P_ ((struct it *, int));
994 static void notice_overwritten_cursor P_ ((struct window *,
995 enum glyph_row_area,
996 int, int, int, int));
997
998
999
1000 #endif /* HAVE_WINDOW_SYSTEM */
1001
1002 \f
1003 /***********************************************************************
1004 Window display dimensions
1005 ***********************************************************************/
1006
1007 /* Return the bottom boundary y-position for text lines in window W.
1008 This is the first y position at which a line cannot start.
1009 It is relative to the top of the window.
1010
1011 This is the height of W minus the height of a mode line, if any. */
1012
1013 INLINE int
1014 window_text_bottom_y (w)
1015 struct window *w;
1016 {
1017 int height = WINDOW_TOTAL_HEIGHT (w);
1018
1019 if (WINDOW_WANTS_MODELINE_P (w))
1020 height -= CURRENT_MODE_LINE_HEIGHT (w);
1021 return height;
1022 }
1023
1024 /* Return the pixel width of display area AREA of window W. AREA < 0
1025 means return the total width of W, not including fringes to
1026 the left and right of the window. */
1027
1028 INLINE int
1029 window_box_width (w, area)
1030 struct window *w;
1031 int area;
1032 {
1033 int cols = XFASTINT (w->total_cols);
1034 int pixels = 0;
1035
1036 if (!w->pseudo_window_p)
1037 {
1038 cols -= WINDOW_SCROLL_BAR_COLS (w);
1039
1040 if (area == TEXT_AREA)
1041 {
1042 if (INTEGERP (w->left_margin_cols))
1043 cols -= XFASTINT (w->left_margin_cols);
1044 if (INTEGERP (w->right_margin_cols))
1045 cols -= XFASTINT (w->right_margin_cols);
1046 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1047 }
1048 else if (area == LEFT_MARGIN_AREA)
1049 {
1050 cols = (INTEGERP (w->left_margin_cols)
1051 ? XFASTINT (w->left_margin_cols) : 0);
1052 pixels = 0;
1053 }
1054 else if (area == RIGHT_MARGIN_AREA)
1055 {
1056 cols = (INTEGERP (w->right_margin_cols)
1057 ? XFASTINT (w->right_margin_cols) : 0);
1058 pixels = 0;
1059 }
1060 }
1061
1062 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1063 }
1064
1065
1066 /* Return the pixel height of the display area of window W, not
1067 including mode lines of W, if any. */
1068
1069 INLINE int
1070 window_box_height (w)
1071 struct window *w;
1072 {
1073 struct frame *f = XFRAME (w->frame);
1074 int height = WINDOW_TOTAL_HEIGHT (w);
1075
1076 xassert (height >= 0);
1077
1078 /* Note: the code below that determines the mode-line/header-line
1079 height is essentially the same as that contained in the macro
1080 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1081 the appropriate glyph row has its `mode_line_p' flag set,
1082 and if it doesn't, uses estimate_mode_line_height instead. */
1083
1084 if (WINDOW_WANTS_MODELINE_P (w))
1085 {
1086 struct glyph_row *ml_row
1087 = (w->current_matrix && w->current_matrix->rows
1088 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1089 : 0);
1090 if (ml_row && ml_row->mode_line_p)
1091 height -= ml_row->height;
1092 else
1093 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1094 }
1095
1096 if (WINDOW_WANTS_HEADER_LINE_P (w))
1097 {
1098 struct glyph_row *hl_row
1099 = (w->current_matrix && w->current_matrix->rows
1100 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1101 : 0);
1102 if (hl_row && hl_row->mode_line_p)
1103 height -= hl_row->height;
1104 else
1105 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1106 }
1107
1108 /* With a very small font and a mode-line that's taller than
1109 default, we might end up with a negative height. */
1110 return max (0, height);
1111 }
1112
1113 /* Return the window-relative coordinate of the left edge of display
1114 area AREA of window W. AREA < 0 means return the left edge of the
1115 whole window, to the right of the left fringe of W. */
1116
1117 INLINE int
1118 window_box_left_offset (w, area)
1119 struct window *w;
1120 int area;
1121 {
1122 int x;
1123
1124 if (w->pseudo_window_p)
1125 return 0;
1126
1127 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1128
1129 if (area == TEXT_AREA)
1130 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1131 + window_box_width (w, LEFT_MARGIN_AREA));
1132 else if (area == RIGHT_MARGIN_AREA)
1133 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1134 + window_box_width (w, LEFT_MARGIN_AREA)
1135 + window_box_width (w, TEXT_AREA)
1136 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1137 ? 0
1138 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1139 else if (area == LEFT_MARGIN_AREA
1140 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1141 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1142
1143 return x;
1144 }
1145
1146
1147 /* Return the window-relative coordinate of the right edge of display
1148 area AREA of window W. AREA < 0 means return the left edge of the
1149 whole window, to the left of the right fringe of W. */
1150
1151 INLINE int
1152 window_box_right_offset (w, area)
1153 struct window *w;
1154 int area;
1155 {
1156 return window_box_left_offset (w, area) + window_box_width (w, area);
1157 }
1158
1159 /* Return the frame-relative coordinate of the left edge of display
1160 area AREA of window W. AREA < 0 means return the left edge of the
1161 whole window, to the right of the left fringe of W. */
1162
1163 INLINE int
1164 window_box_left (w, area)
1165 struct window *w;
1166 int area;
1167 {
1168 struct frame *f = XFRAME (w->frame);
1169 int x;
1170
1171 if (w->pseudo_window_p)
1172 return FRAME_INTERNAL_BORDER_WIDTH (f);
1173
1174 x = (WINDOW_LEFT_EDGE_X (w)
1175 + window_box_left_offset (w, area));
1176
1177 return x;
1178 }
1179
1180
1181 /* Return the frame-relative coordinate of the right edge of display
1182 area AREA of window W. AREA < 0 means return the left edge of the
1183 whole window, to the left of the right fringe of W. */
1184
1185 INLINE int
1186 window_box_right (w, area)
1187 struct window *w;
1188 int area;
1189 {
1190 return window_box_left (w, area) + window_box_width (w, area);
1191 }
1192
1193 /* Get the bounding box of the display area AREA of window W, without
1194 mode lines, in frame-relative coordinates. AREA < 0 means the
1195 whole window, not including the left and right fringes of
1196 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1197 coordinates of the upper-left corner of the box. Return in
1198 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1199
1200 INLINE void
1201 window_box (w, area, box_x, box_y, box_width, box_height)
1202 struct window *w;
1203 int area;
1204 int *box_x, *box_y, *box_width, *box_height;
1205 {
1206 if (box_width)
1207 *box_width = window_box_width (w, area);
1208 if (box_height)
1209 *box_height = window_box_height (w);
1210 if (box_x)
1211 *box_x = window_box_left (w, area);
1212 if (box_y)
1213 {
1214 *box_y = WINDOW_TOP_EDGE_Y (w);
1215 if (WINDOW_WANTS_HEADER_LINE_P (w))
1216 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1217 }
1218 }
1219
1220
1221 /* Get the bounding box of the display area AREA of window W, without
1222 mode lines. AREA < 0 means the whole window, not including the
1223 left and right fringe of the window. Return in *TOP_LEFT_X
1224 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1225 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1226 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1227 box. */
1228
1229 INLINE void
1230 window_box_edges (w, area, top_left_x, top_left_y,
1231 bottom_right_x, bottom_right_y)
1232 struct window *w;
1233 int area;
1234 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1235 {
1236 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1237 bottom_right_y);
1238 *bottom_right_x += *top_left_x;
1239 *bottom_right_y += *top_left_y;
1240 }
1241
1242
1243 \f
1244 /***********************************************************************
1245 Utilities
1246 ***********************************************************************/
1247
1248 /* Return the bottom y-position of the line the iterator IT is in.
1249 This can modify IT's settings. */
1250
1251 int
1252 line_bottom_y (it)
1253 struct it *it;
1254 {
1255 int line_height = it->max_ascent + it->max_descent;
1256 int line_top_y = it->current_y;
1257
1258 if (line_height == 0)
1259 {
1260 if (last_height)
1261 line_height = last_height;
1262 else if (IT_CHARPOS (*it) < ZV)
1263 {
1264 move_it_by_lines (it, 1, 1);
1265 line_height = (it->max_ascent || it->max_descent
1266 ? it->max_ascent + it->max_descent
1267 : last_height);
1268 }
1269 else
1270 {
1271 struct glyph_row *row = it->glyph_row;
1272
1273 /* Use the default character height. */
1274 it->glyph_row = NULL;
1275 it->what = IT_CHARACTER;
1276 it->c = ' ';
1277 it->len = 1;
1278 PRODUCE_GLYPHS (it);
1279 line_height = it->ascent + it->descent;
1280 it->glyph_row = row;
1281 }
1282 }
1283
1284 return line_top_y + line_height;
1285 }
1286
1287
1288 /* Return 1 if position CHARPOS is visible in window W.
1289 CHARPOS < 0 means return info about WINDOW_END position.
1290 If visible, set *X and *Y to pixel coordinates of top left corner.
1291 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1292 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1293
1294 int
1295 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1296 struct window *w;
1297 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1298 {
1299 struct it it;
1300 struct text_pos top;
1301 int visible_p = 0;
1302 struct buffer *old_buffer = NULL;
1303
1304 if (noninteractive)
1305 return visible_p;
1306
1307 if (XBUFFER (w->buffer) != current_buffer)
1308 {
1309 old_buffer = current_buffer;
1310 set_buffer_internal_1 (XBUFFER (w->buffer));
1311 }
1312
1313 SET_TEXT_POS_FROM_MARKER (top, w->start);
1314
1315 /* Compute exact mode line heights. */
1316 if (WINDOW_WANTS_MODELINE_P (w))
1317 current_mode_line_height
1318 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1319 current_buffer->mode_line_format);
1320
1321 if (WINDOW_WANTS_HEADER_LINE_P (w))
1322 current_header_line_height
1323 = display_mode_line (w, HEADER_LINE_FACE_ID,
1324 current_buffer->header_line_format);
1325
1326 start_display (&it, w, top);
1327 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1328 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1329
1330 /* Note that we may overshoot because of invisible text. */
1331 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1332 {
1333 int top_x = it.current_x;
1334 int top_y = it.current_y;
1335 int bottom_y = (last_height = 0, line_bottom_y (&it));
1336 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1337
1338 if (top_y < window_top_y)
1339 visible_p = bottom_y > window_top_y;
1340 else if (top_y < it.last_visible_y)
1341 visible_p = 1;
1342 if (visible_p)
1343 {
1344 *x = top_x;
1345 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1346 *rtop = max (0, window_top_y - top_y);
1347 *rbot = max (0, bottom_y - it.last_visible_y);
1348 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1349 - max (top_y, window_top_y)));
1350 *vpos = it.vpos;
1351 }
1352 }
1353 else
1354 {
1355 struct it it2;
1356
1357 it2 = it;
1358 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1359 move_it_by_lines (&it, 1, 0);
1360 if (charpos < IT_CHARPOS (it)
1361 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1362 {
1363 visible_p = 1;
1364 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1365 *x = it2.current_x;
1366 *y = it2.current_y + it2.max_ascent - it2.ascent;
1367 *rtop = max (0, -it2.current_y);
1368 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1369 - it.last_visible_y));
1370 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1371 it.last_visible_y)
1372 - max (it2.current_y,
1373 WINDOW_HEADER_LINE_HEIGHT (w))));
1374 *vpos = it2.vpos;
1375 }
1376 }
1377
1378 if (old_buffer)
1379 set_buffer_internal_1 (old_buffer);
1380
1381 current_header_line_height = current_mode_line_height = -1;
1382
1383 if (visible_p && XFASTINT (w->hscroll) > 0)
1384 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1385
1386 #if 0
1387 /* Debugging code. */
1388 if (visible_p)
1389 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1390 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1391 else
1392 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1393 #endif
1394
1395 return visible_p;
1396 }
1397
1398
1399 /* Return the next character from STR which is MAXLEN bytes long.
1400 Return in *LEN the length of the character. This is like
1401 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1402 we find one, we return a `?', but with the length of the invalid
1403 character. */
1404
1405 static INLINE int
1406 string_char_and_length (str, maxlen, len)
1407 const unsigned char *str;
1408 int maxlen, *len;
1409 {
1410 int c;
1411
1412 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1413 if (!CHAR_VALID_P (c, 1))
1414 /* We may not change the length here because other places in Emacs
1415 don't use this function, i.e. they silently accept invalid
1416 characters. */
1417 c = '?';
1418
1419 return c;
1420 }
1421
1422
1423
1424 /* Given a position POS containing a valid character and byte position
1425 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1426
1427 static struct text_pos
1428 string_pos_nchars_ahead (pos, string, nchars)
1429 struct text_pos pos;
1430 Lisp_Object string;
1431 int nchars;
1432 {
1433 xassert (STRINGP (string) && nchars >= 0);
1434
1435 if (STRING_MULTIBYTE (string))
1436 {
1437 int rest = SBYTES (string) - BYTEPOS (pos);
1438 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1439 int len;
1440
1441 while (nchars--)
1442 {
1443 string_char_and_length (p, rest, &len);
1444 p += len, rest -= len;
1445 xassert (rest >= 0);
1446 CHARPOS (pos) += 1;
1447 BYTEPOS (pos) += len;
1448 }
1449 }
1450 else
1451 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1452
1453 return pos;
1454 }
1455
1456
1457 /* Value is the text position, i.e. character and byte position,
1458 for character position CHARPOS in STRING. */
1459
1460 static INLINE struct text_pos
1461 string_pos (charpos, string)
1462 int charpos;
1463 Lisp_Object string;
1464 {
1465 struct text_pos pos;
1466 xassert (STRINGP (string));
1467 xassert (charpos >= 0);
1468 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1469 return pos;
1470 }
1471
1472
1473 /* Value is a text position, i.e. character and byte position, for
1474 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1475 means recognize multibyte characters. */
1476
1477 static struct text_pos
1478 c_string_pos (charpos, s, multibyte_p)
1479 int charpos;
1480 unsigned char *s;
1481 int multibyte_p;
1482 {
1483 struct text_pos pos;
1484
1485 xassert (s != NULL);
1486 xassert (charpos >= 0);
1487
1488 if (multibyte_p)
1489 {
1490 int rest = strlen (s), len;
1491
1492 SET_TEXT_POS (pos, 0, 0);
1493 while (charpos--)
1494 {
1495 string_char_and_length (s, rest, &len);
1496 s += len, rest -= len;
1497 xassert (rest >= 0);
1498 CHARPOS (pos) += 1;
1499 BYTEPOS (pos) += len;
1500 }
1501 }
1502 else
1503 SET_TEXT_POS (pos, charpos, charpos);
1504
1505 return pos;
1506 }
1507
1508
1509 /* Value is the number of characters in C string S. MULTIBYTE_P
1510 non-zero means recognize multibyte characters. */
1511
1512 static int
1513 number_of_chars (s, multibyte_p)
1514 unsigned char *s;
1515 int multibyte_p;
1516 {
1517 int nchars;
1518
1519 if (multibyte_p)
1520 {
1521 int rest = strlen (s), len;
1522 unsigned char *p = (unsigned char *) s;
1523
1524 for (nchars = 0; rest > 0; ++nchars)
1525 {
1526 string_char_and_length (p, rest, &len);
1527 rest -= len, p += len;
1528 }
1529 }
1530 else
1531 nchars = strlen (s);
1532
1533 return nchars;
1534 }
1535
1536
1537 /* Compute byte position NEWPOS->bytepos corresponding to
1538 NEWPOS->charpos. POS is a known position in string STRING.
1539 NEWPOS->charpos must be >= POS.charpos. */
1540
1541 static void
1542 compute_string_pos (newpos, pos, string)
1543 struct text_pos *newpos, pos;
1544 Lisp_Object string;
1545 {
1546 xassert (STRINGP (string));
1547 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1548
1549 if (STRING_MULTIBYTE (string))
1550 *newpos = string_pos_nchars_ahead (pos, string,
1551 CHARPOS (*newpos) - CHARPOS (pos));
1552 else
1553 BYTEPOS (*newpos) = CHARPOS (*newpos);
1554 }
1555
1556 /* EXPORT:
1557 Return an estimation of the pixel height of mode or top lines on
1558 frame F. FACE_ID specifies what line's height to estimate. */
1559
1560 int
1561 estimate_mode_line_height (f, face_id)
1562 struct frame *f;
1563 enum face_id face_id;
1564 {
1565 #ifdef HAVE_WINDOW_SYSTEM
1566 if (FRAME_WINDOW_P (f))
1567 {
1568 int height = FONT_HEIGHT (FRAME_FONT (f));
1569
1570 /* This function is called so early when Emacs starts that the face
1571 cache and mode line face are not yet initialized. */
1572 if (FRAME_FACE_CACHE (f))
1573 {
1574 struct face *face = FACE_FROM_ID (f, face_id);
1575 if (face)
1576 {
1577 if (face->font)
1578 height = FONT_HEIGHT (face->font);
1579 if (face->box_line_width > 0)
1580 height += 2 * face->box_line_width;
1581 }
1582 }
1583
1584 return height;
1585 }
1586 #endif
1587
1588 return 1;
1589 }
1590
1591 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1592 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1593 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1594 not force the value into range. */
1595
1596 void
1597 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1598 FRAME_PTR f;
1599 register int pix_x, pix_y;
1600 int *x, *y;
1601 NativeRectangle *bounds;
1602 int noclip;
1603 {
1604
1605 #ifdef HAVE_WINDOW_SYSTEM
1606 if (FRAME_WINDOW_P (f))
1607 {
1608 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1609 even for negative values. */
1610 if (pix_x < 0)
1611 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1612 if (pix_y < 0)
1613 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1614
1615 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1616 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1617
1618 if (bounds)
1619 STORE_NATIVE_RECT (*bounds,
1620 FRAME_COL_TO_PIXEL_X (f, pix_x),
1621 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1622 FRAME_COLUMN_WIDTH (f) - 1,
1623 FRAME_LINE_HEIGHT (f) - 1);
1624
1625 if (!noclip)
1626 {
1627 if (pix_x < 0)
1628 pix_x = 0;
1629 else if (pix_x > FRAME_TOTAL_COLS (f))
1630 pix_x = FRAME_TOTAL_COLS (f);
1631
1632 if (pix_y < 0)
1633 pix_y = 0;
1634 else if (pix_y > FRAME_LINES (f))
1635 pix_y = FRAME_LINES (f);
1636 }
1637 }
1638 #endif
1639
1640 *x = pix_x;
1641 *y = pix_y;
1642 }
1643
1644
1645 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1646 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1647 can't tell the positions because W's display is not up to date,
1648 return 0. */
1649
1650 int
1651 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1652 struct window *w;
1653 int hpos, vpos;
1654 int *frame_x, *frame_y;
1655 {
1656 #ifdef HAVE_WINDOW_SYSTEM
1657 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1658 {
1659 int success_p;
1660
1661 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1662 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1663
1664 if (display_completed)
1665 {
1666 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1667 struct glyph *glyph = row->glyphs[TEXT_AREA];
1668 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1669
1670 hpos = row->x;
1671 vpos = row->y;
1672 while (glyph < end)
1673 {
1674 hpos += glyph->pixel_width;
1675 ++glyph;
1676 }
1677
1678 /* If first glyph is partially visible, its first visible position is still 0. */
1679 if (hpos < 0)
1680 hpos = 0;
1681
1682 success_p = 1;
1683 }
1684 else
1685 {
1686 hpos = vpos = 0;
1687 success_p = 0;
1688 }
1689
1690 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1691 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1692 return success_p;
1693 }
1694 #endif
1695
1696 *frame_x = hpos;
1697 *frame_y = vpos;
1698 return 1;
1699 }
1700
1701
1702 #ifdef HAVE_WINDOW_SYSTEM
1703
1704 /* Find the glyph under window-relative coordinates X/Y in window W.
1705 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1706 strings. Return in *HPOS and *VPOS the row and column number of
1707 the glyph found. Return in *AREA the glyph area containing X.
1708 Value is a pointer to the glyph found or null if X/Y is not on
1709 text, or we can't tell because W's current matrix is not up to
1710 date. */
1711
1712 static struct glyph *
1713 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1714 struct window *w;
1715 int x, y;
1716 int *hpos, *vpos, *dx, *dy, *area;
1717 {
1718 struct glyph *glyph, *end;
1719 struct glyph_row *row = NULL;
1720 int x0, i;
1721
1722 /* Find row containing Y. Give up if some row is not enabled. */
1723 for (i = 0; i < w->current_matrix->nrows; ++i)
1724 {
1725 row = MATRIX_ROW (w->current_matrix, i);
1726 if (!row->enabled_p)
1727 return NULL;
1728 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1729 break;
1730 }
1731
1732 *vpos = i;
1733 *hpos = 0;
1734
1735 /* Give up if Y is not in the window. */
1736 if (i == w->current_matrix->nrows)
1737 return NULL;
1738
1739 /* Get the glyph area containing X. */
1740 if (w->pseudo_window_p)
1741 {
1742 *area = TEXT_AREA;
1743 x0 = 0;
1744 }
1745 else
1746 {
1747 if (x < window_box_left_offset (w, TEXT_AREA))
1748 {
1749 *area = LEFT_MARGIN_AREA;
1750 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1751 }
1752 else if (x < window_box_right_offset (w, TEXT_AREA))
1753 {
1754 *area = TEXT_AREA;
1755 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1756 }
1757 else
1758 {
1759 *area = RIGHT_MARGIN_AREA;
1760 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1761 }
1762 }
1763
1764 /* Find glyph containing X. */
1765 glyph = row->glyphs[*area];
1766 end = glyph + row->used[*area];
1767 x -= x0;
1768 while (glyph < end && x >= glyph->pixel_width)
1769 {
1770 x -= glyph->pixel_width;
1771 ++glyph;
1772 }
1773
1774 if (glyph == end)
1775 return NULL;
1776
1777 if (dx)
1778 {
1779 *dx = x;
1780 *dy = y - (row->y + row->ascent - glyph->ascent);
1781 }
1782
1783 *hpos = glyph - row->glyphs[*area];
1784 return glyph;
1785 }
1786
1787
1788 /* EXPORT:
1789 Convert frame-relative x/y to coordinates relative to window W.
1790 Takes pseudo-windows into account. */
1791
1792 void
1793 frame_to_window_pixel_xy (w, x, y)
1794 struct window *w;
1795 int *x, *y;
1796 {
1797 if (w->pseudo_window_p)
1798 {
1799 /* A pseudo-window is always full-width, and starts at the
1800 left edge of the frame, plus a frame border. */
1801 struct frame *f = XFRAME (w->frame);
1802 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1803 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1804 }
1805 else
1806 {
1807 *x -= WINDOW_LEFT_EDGE_X (w);
1808 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1809 }
1810 }
1811
1812 /* EXPORT:
1813 Return in RECTS[] at most N clipping rectangles for glyph string S.
1814 Return the number of stored rectangles. */
1815
1816 int
1817 get_glyph_string_clip_rects (s, rects, n)
1818 struct glyph_string *s;
1819 NativeRectangle *rects;
1820 int n;
1821 {
1822 XRectangle r;
1823
1824 if (n <= 0)
1825 return 0;
1826
1827 if (s->row->full_width_p)
1828 {
1829 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1830 r.x = WINDOW_LEFT_EDGE_X (s->w);
1831 r.width = WINDOW_TOTAL_WIDTH (s->w);
1832
1833 /* Unless displaying a mode or menu bar line, which are always
1834 fully visible, clip to the visible part of the row. */
1835 if (s->w->pseudo_window_p)
1836 r.height = s->row->visible_height;
1837 else
1838 r.height = s->height;
1839 }
1840 else
1841 {
1842 /* This is a text line that may be partially visible. */
1843 r.x = window_box_left (s->w, s->area);
1844 r.width = window_box_width (s->w, s->area);
1845 r.height = s->row->visible_height;
1846 }
1847
1848 if (s->clip_head)
1849 if (r.x < s->clip_head->x)
1850 {
1851 if (r.width >= s->clip_head->x - r.x)
1852 r.width -= s->clip_head->x - r.x;
1853 else
1854 r.width = 0;
1855 r.x = s->clip_head->x;
1856 }
1857 if (s->clip_tail)
1858 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1859 {
1860 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1861 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1862 else
1863 r.width = 0;
1864 }
1865
1866 /* If S draws overlapping rows, it's sufficient to use the top and
1867 bottom of the window for clipping because this glyph string
1868 intentionally draws over other lines. */
1869 if (s->for_overlaps)
1870 {
1871 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1872 r.height = window_text_bottom_y (s->w) - r.y;
1873
1874 /* Alas, the above simple strategy does not work for the
1875 environments with anti-aliased text: if the same text is
1876 drawn onto the same place multiple times, it gets thicker.
1877 If the overlap we are processing is for the erased cursor, we
1878 take the intersection with the rectagle of the cursor. */
1879 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1880 {
1881 XRectangle rc, r_save = r;
1882
1883 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1884 rc.y = s->w->phys_cursor.y;
1885 rc.width = s->w->phys_cursor_width;
1886 rc.height = s->w->phys_cursor_height;
1887
1888 x_intersect_rectangles (&r_save, &rc, &r);
1889 }
1890 }
1891 else
1892 {
1893 /* Don't use S->y for clipping because it doesn't take partially
1894 visible lines into account. For example, it can be negative for
1895 partially visible lines at the top of a window. */
1896 if (!s->row->full_width_p
1897 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1898 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1899 else
1900 r.y = max (0, s->row->y);
1901
1902 /* If drawing a tool-bar window, draw it over the internal border
1903 at the top of the window. */
1904 if (WINDOWP (s->f->tool_bar_window)
1905 && s->w == XWINDOW (s->f->tool_bar_window))
1906 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1907 }
1908
1909 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1910
1911 /* If drawing the cursor, don't let glyph draw outside its
1912 advertised boundaries. Cleartype does this under some circumstances. */
1913 if (s->hl == DRAW_CURSOR)
1914 {
1915 struct glyph *glyph = s->first_glyph;
1916 int height, max_y;
1917
1918 if (s->x > r.x)
1919 {
1920 r.width -= s->x - r.x;
1921 r.x = s->x;
1922 }
1923 r.width = min (r.width, glyph->pixel_width);
1924
1925 /* If r.y is below window bottom, ensure that we still see a cursor. */
1926 height = min (glyph->ascent + glyph->descent,
1927 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1928 max_y = window_text_bottom_y (s->w) - height;
1929 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1930 if (s->ybase - glyph->ascent > max_y)
1931 {
1932 r.y = max_y;
1933 r.height = height;
1934 }
1935 else
1936 {
1937 /* Don't draw cursor glyph taller than our actual glyph. */
1938 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1939 if (height < r.height)
1940 {
1941 max_y = r.y + r.height;
1942 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1943 r.height = min (max_y - r.y, height);
1944 }
1945 }
1946 }
1947
1948 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1949 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1950 {
1951 #ifdef CONVERT_FROM_XRECT
1952 CONVERT_FROM_XRECT (r, *rects);
1953 #else
1954 *rects = r;
1955 #endif
1956 return 1;
1957 }
1958 else
1959 {
1960 /* If we are processing overlapping and allowed to return
1961 multiple clipping rectangles, we exclude the row of the glyph
1962 string from the clipping rectangle. This is to avoid drawing
1963 the same text on the environment with anti-aliasing. */
1964 #ifdef CONVERT_FROM_XRECT
1965 XRectangle rs[2];
1966 #else
1967 XRectangle *rs = rects;
1968 #endif
1969 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1970
1971 if (s->for_overlaps & OVERLAPS_PRED)
1972 {
1973 rs[i] = r;
1974 if (r.y + r.height > row_y)
1975 {
1976 if (r.y < row_y)
1977 rs[i].height = row_y - r.y;
1978 else
1979 rs[i].height = 0;
1980 }
1981 i++;
1982 }
1983 if (s->for_overlaps & OVERLAPS_SUCC)
1984 {
1985 rs[i] = r;
1986 if (r.y < row_y + s->row->visible_height)
1987 {
1988 if (r.y + r.height > row_y + s->row->visible_height)
1989 {
1990 rs[i].y = row_y + s->row->visible_height;
1991 rs[i].height = r.y + r.height - rs[i].y;
1992 }
1993 else
1994 rs[i].height = 0;
1995 }
1996 i++;
1997 }
1998
1999 n = i;
2000 #ifdef CONVERT_FROM_XRECT
2001 for (i = 0; i < n; i++)
2002 CONVERT_FROM_XRECT (rs[i], rects[i]);
2003 #endif
2004 return n;
2005 }
2006 }
2007
2008 /* EXPORT:
2009 Return in *NR the clipping rectangle for glyph string S. */
2010
2011 void
2012 get_glyph_string_clip_rect (s, nr)
2013 struct glyph_string *s;
2014 NativeRectangle *nr;
2015 {
2016 get_glyph_string_clip_rects (s, nr, 1);
2017 }
2018
2019
2020 /* EXPORT:
2021 Return the position and height of the phys cursor in window W.
2022 Set w->phys_cursor_width to width of phys cursor.
2023 */
2024
2025 void
2026 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2027 struct window *w;
2028 struct glyph_row *row;
2029 struct glyph *glyph;
2030 int *xp, *yp, *heightp;
2031 {
2032 struct frame *f = XFRAME (WINDOW_FRAME (w));
2033 int x, y, wd, h, h0, y0;
2034
2035 /* Compute the width of the rectangle to draw. If on a stretch
2036 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2037 rectangle as wide as the glyph, but use a canonical character
2038 width instead. */
2039 wd = glyph->pixel_width - 1;
2040 #ifdef HAVE_NTGUI
2041 wd++; /* Why? */
2042 #endif
2043
2044 x = w->phys_cursor.x;
2045 if (x < 0)
2046 {
2047 wd += x;
2048 x = 0;
2049 }
2050
2051 if (glyph->type == STRETCH_GLYPH
2052 && !x_stretch_cursor_p)
2053 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2054 w->phys_cursor_width = wd;
2055
2056 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2057
2058 /* If y is below window bottom, ensure that we still see a cursor. */
2059 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2060
2061 h = max (h0, glyph->ascent + glyph->descent);
2062 h0 = min (h0, glyph->ascent + glyph->descent);
2063
2064 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2065 if (y < y0)
2066 {
2067 h = max (h - (y0 - y) + 1, h0);
2068 y = y0 - 1;
2069 }
2070 else
2071 {
2072 y0 = window_text_bottom_y (w) - h0;
2073 if (y > y0)
2074 {
2075 h += y - y0;
2076 y = y0;
2077 }
2078 }
2079
2080 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2081 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2082 *heightp = h;
2083 }
2084
2085 /*
2086 * Remember which glyph the mouse is over.
2087 */
2088
2089 void
2090 remember_mouse_glyph (f, gx, gy, rect)
2091 struct frame *f;
2092 int gx, gy;
2093 NativeRectangle *rect;
2094 {
2095 Lisp_Object window;
2096 struct window *w;
2097 struct glyph_row *r, *gr, *end_row;
2098 enum window_part part;
2099 enum glyph_row_area area;
2100 int x, y, width, height;
2101
2102 /* Try to determine frame pixel position and size of the glyph under
2103 frame pixel coordinates X/Y on frame F. */
2104
2105 if (!f->glyphs_initialized_p
2106 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2107 NILP (window)))
2108 {
2109 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2110 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2111 goto virtual_glyph;
2112 }
2113
2114 w = XWINDOW (window);
2115 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2116 height = WINDOW_FRAME_LINE_HEIGHT (w);
2117
2118 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2119 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2120
2121 if (w->pseudo_window_p)
2122 {
2123 area = TEXT_AREA;
2124 part = ON_MODE_LINE; /* Don't adjust margin. */
2125 goto text_glyph;
2126 }
2127
2128 switch (part)
2129 {
2130 case ON_LEFT_MARGIN:
2131 area = LEFT_MARGIN_AREA;
2132 goto text_glyph;
2133
2134 case ON_RIGHT_MARGIN:
2135 area = RIGHT_MARGIN_AREA;
2136 goto text_glyph;
2137
2138 case ON_HEADER_LINE:
2139 case ON_MODE_LINE:
2140 gr = (part == ON_HEADER_LINE
2141 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2142 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2143 gy = gr->y;
2144 area = TEXT_AREA;
2145 goto text_glyph_row_found;
2146
2147 case ON_TEXT:
2148 area = TEXT_AREA;
2149
2150 text_glyph:
2151 gr = 0; gy = 0;
2152 for (; r <= end_row && r->enabled_p; ++r)
2153 if (r->y + r->height > y)
2154 {
2155 gr = r; gy = r->y;
2156 break;
2157 }
2158
2159 text_glyph_row_found:
2160 if (gr && gy <= y)
2161 {
2162 struct glyph *g = gr->glyphs[area];
2163 struct glyph *end = g + gr->used[area];
2164
2165 height = gr->height;
2166 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2167 if (gx + g->pixel_width > x)
2168 break;
2169
2170 if (g < end)
2171 {
2172 if (g->type == IMAGE_GLYPH)
2173 {
2174 /* Don't remember when mouse is over image, as
2175 image may have hot-spots. */
2176 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2177 return;
2178 }
2179 width = g->pixel_width;
2180 }
2181 else
2182 {
2183 /* Use nominal char spacing at end of line. */
2184 x -= gx;
2185 gx += (x / width) * width;
2186 }
2187
2188 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2189 gx += window_box_left_offset (w, area);
2190 }
2191 else
2192 {
2193 /* Use nominal line height at end of window. */
2194 gx = (x / width) * width;
2195 y -= gy;
2196 gy += (y / height) * height;
2197 }
2198 break;
2199
2200 case ON_LEFT_FRINGE:
2201 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2202 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2203 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2204 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2205 goto row_glyph;
2206
2207 case ON_RIGHT_FRINGE:
2208 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2209 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2210 : window_box_right_offset (w, TEXT_AREA));
2211 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2212 goto row_glyph;
2213
2214 case ON_SCROLL_BAR:
2215 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2216 ? 0
2217 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2218 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2219 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2220 : 0)));
2221 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2222
2223 row_glyph:
2224 gr = 0, gy = 0;
2225 for (; r <= end_row && r->enabled_p; ++r)
2226 if (r->y + r->height > y)
2227 {
2228 gr = r; gy = r->y;
2229 break;
2230 }
2231
2232 if (gr && gy <= y)
2233 height = gr->height;
2234 else
2235 {
2236 /* Use nominal line height at end of window. */
2237 y -= gy;
2238 gy += (y / height) * height;
2239 }
2240 break;
2241
2242 default:
2243 ;
2244 virtual_glyph:
2245 /* If there is no glyph under the mouse, then we divide the screen
2246 into a grid of the smallest glyph in the frame, and use that
2247 as our "glyph". */
2248
2249 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2250 round down even for negative values. */
2251 if (gx < 0)
2252 gx -= width - 1;
2253 if (gy < 0)
2254 gy -= height - 1;
2255
2256 gx = (gx / width) * width;
2257 gy = (gy / height) * height;
2258
2259 goto store_rect;
2260 }
2261
2262 gx += WINDOW_LEFT_EDGE_X (w);
2263 gy += WINDOW_TOP_EDGE_Y (w);
2264
2265 store_rect:
2266 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2267
2268 /* Visible feedback for debugging. */
2269 #if 0
2270 #if HAVE_X_WINDOWS
2271 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2272 f->output_data.x->normal_gc,
2273 gx, gy, width, height);
2274 #endif
2275 #endif
2276 }
2277
2278
2279 #endif /* HAVE_WINDOW_SYSTEM */
2280
2281 \f
2282 /***********************************************************************
2283 Lisp form evaluation
2284 ***********************************************************************/
2285
2286 /* Error handler for safe_eval and safe_call. */
2287
2288 static Lisp_Object
2289 safe_eval_handler (arg)
2290 Lisp_Object arg;
2291 {
2292 add_to_log ("Error during redisplay: %s", arg, Qnil);
2293 return Qnil;
2294 }
2295
2296
2297 /* Evaluate SEXPR and return the result, or nil if something went
2298 wrong. Prevent redisplay during the evaluation. */
2299
2300 Lisp_Object
2301 safe_eval (sexpr)
2302 Lisp_Object sexpr;
2303 {
2304 Lisp_Object val;
2305
2306 if (inhibit_eval_during_redisplay)
2307 val = Qnil;
2308 else
2309 {
2310 int count = SPECPDL_INDEX ();
2311 struct gcpro gcpro1;
2312
2313 GCPRO1 (sexpr);
2314 specbind (Qinhibit_redisplay, Qt);
2315 /* Use Qt to ensure debugger does not run,
2316 so there is no possibility of wanting to redisplay. */
2317 val = internal_condition_case_1 (Feval, sexpr, Qt,
2318 safe_eval_handler);
2319 UNGCPRO;
2320 val = unbind_to (count, val);
2321 }
2322
2323 return val;
2324 }
2325
2326
2327 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2328 Return the result, or nil if something went wrong. Prevent
2329 redisplay during the evaluation. */
2330
2331 Lisp_Object
2332 safe_call (nargs, args)
2333 int nargs;
2334 Lisp_Object *args;
2335 {
2336 Lisp_Object val;
2337
2338 if (inhibit_eval_during_redisplay)
2339 val = Qnil;
2340 else
2341 {
2342 int count = SPECPDL_INDEX ();
2343 struct gcpro gcpro1;
2344
2345 GCPRO1 (args[0]);
2346 gcpro1.nvars = nargs;
2347 specbind (Qinhibit_redisplay, Qt);
2348 /* Use Qt to ensure debugger does not run,
2349 so there is no possibility of wanting to redisplay. */
2350 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2351 safe_eval_handler);
2352 UNGCPRO;
2353 val = unbind_to (count, val);
2354 }
2355
2356 return val;
2357 }
2358
2359
2360 /* Call function FN with one argument ARG.
2361 Return the result, or nil if something went wrong. */
2362
2363 Lisp_Object
2364 safe_call1 (fn, arg)
2365 Lisp_Object fn, arg;
2366 {
2367 Lisp_Object args[2];
2368 args[0] = fn;
2369 args[1] = arg;
2370 return safe_call (2, args);
2371 }
2372
2373
2374 \f
2375 /***********************************************************************
2376 Debugging
2377 ***********************************************************************/
2378
2379 #if 0
2380
2381 /* Define CHECK_IT to perform sanity checks on iterators.
2382 This is for debugging. It is too slow to do unconditionally. */
2383
2384 static void
2385 check_it (it)
2386 struct it *it;
2387 {
2388 if (it->method == GET_FROM_STRING)
2389 {
2390 xassert (STRINGP (it->string));
2391 xassert (IT_STRING_CHARPOS (*it) >= 0);
2392 }
2393 else
2394 {
2395 xassert (IT_STRING_CHARPOS (*it) < 0);
2396 if (it->method == GET_FROM_BUFFER)
2397 {
2398 /* Check that character and byte positions agree. */
2399 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2400 }
2401 }
2402
2403 if (it->dpvec)
2404 xassert (it->current.dpvec_index >= 0);
2405 else
2406 xassert (it->current.dpvec_index < 0);
2407 }
2408
2409 #define CHECK_IT(IT) check_it ((IT))
2410
2411 #else /* not 0 */
2412
2413 #define CHECK_IT(IT) (void) 0
2414
2415 #endif /* not 0 */
2416
2417
2418 #if GLYPH_DEBUG
2419
2420 /* Check that the window end of window W is what we expect it
2421 to be---the last row in the current matrix displaying text. */
2422
2423 static void
2424 check_window_end (w)
2425 struct window *w;
2426 {
2427 if (!MINI_WINDOW_P (w)
2428 && !NILP (w->window_end_valid))
2429 {
2430 struct glyph_row *row;
2431 xassert ((row = MATRIX_ROW (w->current_matrix,
2432 XFASTINT (w->window_end_vpos)),
2433 !row->enabled_p
2434 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2435 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2436 }
2437 }
2438
2439 #define CHECK_WINDOW_END(W) check_window_end ((W))
2440
2441 #else /* not GLYPH_DEBUG */
2442
2443 #define CHECK_WINDOW_END(W) (void) 0
2444
2445 #endif /* not GLYPH_DEBUG */
2446
2447
2448 \f
2449 /***********************************************************************
2450 Iterator initialization
2451 ***********************************************************************/
2452
2453 /* Initialize IT for displaying current_buffer in window W, starting
2454 at character position CHARPOS. CHARPOS < 0 means that no buffer
2455 position is specified which is useful when the iterator is assigned
2456 a position later. BYTEPOS is the byte position corresponding to
2457 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2458
2459 If ROW is not null, calls to produce_glyphs with IT as parameter
2460 will produce glyphs in that row.
2461
2462 BASE_FACE_ID is the id of a base face to use. It must be one of
2463 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2464 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2465 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2466
2467 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2468 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2469 will be initialized to use the corresponding mode line glyph row of
2470 the desired matrix of W. */
2471
2472 void
2473 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2474 struct it *it;
2475 struct window *w;
2476 int charpos, bytepos;
2477 struct glyph_row *row;
2478 enum face_id base_face_id;
2479 {
2480 int highlight_region_p;
2481
2482 /* Some precondition checks. */
2483 xassert (w != NULL && it != NULL);
2484 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2485 && charpos <= ZV));
2486
2487 /* If face attributes have been changed since the last redisplay,
2488 free realized faces now because they depend on face definitions
2489 that might have changed. Don't free faces while there might be
2490 desired matrices pending which reference these faces. */
2491 if (face_change_count && !inhibit_free_realized_faces)
2492 {
2493 face_change_count = 0;
2494 free_all_realized_faces (Qnil);
2495 }
2496
2497 /* Use one of the mode line rows of W's desired matrix if
2498 appropriate. */
2499 if (row == NULL)
2500 {
2501 if (base_face_id == MODE_LINE_FACE_ID
2502 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2503 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2504 else if (base_face_id == HEADER_LINE_FACE_ID)
2505 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2506 }
2507
2508 /* Clear IT. */
2509 bzero (it, sizeof *it);
2510 it->current.overlay_string_index = -1;
2511 it->current.dpvec_index = -1;
2512 it->base_face_id = base_face_id;
2513 it->string = Qnil;
2514 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2515
2516 /* The window in which we iterate over current_buffer: */
2517 XSETWINDOW (it->window, w);
2518 it->w = w;
2519 it->f = XFRAME (w->frame);
2520
2521 /* Extra space between lines (on window systems only). */
2522 if (base_face_id == DEFAULT_FACE_ID
2523 && FRAME_WINDOW_P (it->f))
2524 {
2525 if (NATNUMP (current_buffer->extra_line_spacing))
2526 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2527 else if (FLOATP (current_buffer->extra_line_spacing))
2528 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2529 * FRAME_LINE_HEIGHT (it->f));
2530 else if (it->f->extra_line_spacing > 0)
2531 it->extra_line_spacing = it->f->extra_line_spacing;
2532 it->max_extra_line_spacing = 0;
2533 }
2534
2535 /* If realized faces have been removed, e.g. because of face
2536 attribute changes of named faces, recompute them. When running
2537 in batch mode, the face cache of Vterminal_frame is null. If
2538 we happen to get called, make a dummy face cache. */
2539 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2540 init_frame_faces (it->f);
2541 if (FRAME_FACE_CACHE (it->f)->used == 0)
2542 recompute_basic_faces (it->f);
2543
2544 /* Current value of the `slice', `space-width', and 'height' properties. */
2545 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2546 it->space_width = Qnil;
2547 it->font_height = Qnil;
2548 it->override_ascent = -1;
2549
2550 /* Are control characters displayed as `^C'? */
2551 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2552
2553 /* -1 means everything between a CR and the following line end
2554 is invisible. >0 means lines indented more than this value are
2555 invisible. */
2556 it->selective = (INTEGERP (current_buffer->selective_display)
2557 ? XFASTINT (current_buffer->selective_display)
2558 : (!NILP (current_buffer->selective_display)
2559 ? -1 : 0));
2560 it->selective_display_ellipsis_p
2561 = !NILP (current_buffer->selective_display_ellipses);
2562
2563 /* Display table to use. */
2564 it->dp = window_display_table (w);
2565
2566 /* Are multibyte characters enabled in current_buffer? */
2567 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2568
2569 /* Non-zero if we should highlight the region. */
2570 highlight_region_p
2571 = (!NILP (Vtransient_mark_mode)
2572 && !NILP (current_buffer->mark_active)
2573 && XMARKER (current_buffer->mark)->buffer != 0);
2574
2575 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2576 start and end of a visible region in window IT->w. Set both to
2577 -1 to indicate no region. */
2578 if (highlight_region_p
2579 /* Maybe highlight only in selected window. */
2580 && (/* Either show region everywhere. */
2581 highlight_nonselected_windows
2582 /* Or show region in the selected window. */
2583 || w == XWINDOW (selected_window)
2584 /* Or show the region if we are in the mini-buffer and W is
2585 the window the mini-buffer refers to. */
2586 || (MINI_WINDOW_P (XWINDOW (selected_window))
2587 && WINDOWP (minibuf_selected_window)
2588 && w == XWINDOW (minibuf_selected_window))))
2589 {
2590 int charpos = marker_position (current_buffer->mark);
2591 it->region_beg_charpos = min (PT, charpos);
2592 it->region_end_charpos = max (PT, charpos);
2593 }
2594 else
2595 it->region_beg_charpos = it->region_end_charpos = -1;
2596
2597 /* Get the position at which the redisplay_end_trigger hook should
2598 be run, if it is to be run at all. */
2599 if (MARKERP (w->redisplay_end_trigger)
2600 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2601 it->redisplay_end_trigger_charpos
2602 = marker_position (w->redisplay_end_trigger);
2603 else if (INTEGERP (w->redisplay_end_trigger))
2604 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2605
2606 /* Correct bogus values of tab_width. */
2607 it->tab_width = XINT (current_buffer->tab_width);
2608 if (it->tab_width <= 0 || it->tab_width > 1000)
2609 it->tab_width = 8;
2610
2611 /* Are lines in the display truncated? */
2612 it->truncate_lines_p
2613 = (base_face_id != DEFAULT_FACE_ID
2614 || XINT (it->w->hscroll)
2615 || (truncate_partial_width_windows
2616 && !WINDOW_FULL_WIDTH_P (it->w))
2617 || !NILP (current_buffer->truncate_lines));
2618
2619 /* Get dimensions of truncation and continuation glyphs. These are
2620 displayed as fringe bitmaps under X, so we don't need them for such
2621 frames. */
2622 if (!FRAME_WINDOW_P (it->f))
2623 {
2624 if (it->truncate_lines_p)
2625 {
2626 /* We will need the truncation glyph. */
2627 xassert (it->glyph_row == NULL);
2628 produce_special_glyphs (it, IT_TRUNCATION);
2629 it->truncation_pixel_width = it->pixel_width;
2630 }
2631 else
2632 {
2633 /* We will need the continuation glyph. */
2634 xassert (it->glyph_row == NULL);
2635 produce_special_glyphs (it, IT_CONTINUATION);
2636 it->continuation_pixel_width = it->pixel_width;
2637 }
2638
2639 /* Reset these values to zero because the produce_special_glyphs
2640 above has changed them. */
2641 it->pixel_width = it->ascent = it->descent = 0;
2642 it->phys_ascent = it->phys_descent = 0;
2643 }
2644
2645 /* Set this after getting the dimensions of truncation and
2646 continuation glyphs, so that we don't produce glyphs when calling
2647 produce_special_glyphs, above. */
2648 it->glyph_row = row;
2649 it->area = TEXT_AREA;
2650
2651 /* Get the dimensions of the display area. The display area
2652 consists of the visible window area plus a horizontally scrolled
2653 part to the left of the window. All x-values are relative to the
2654 start of this total display area. */
2655 if (base_face_id != DEFAULT_FACE_ID)
2656 {
2657 /* Mode lines, menu bar in terminal frames. */
2658 it->first_visible_x = 0;
2659 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2660 }
2661 else
2662 {
2663 it->first_visible_x
2664 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2665 it->last_visible_x = (it->first_visible_x
2666 + window_box_width (w, TEXT_AREA));
2667
2668 /* If we truncate lines, leave room for the truncator glyph(s) at
2669 the right margin. Otherwise, leave room for the continuation
2670 glyph(s). Truncation and continuation glyphs are not inserted
2671 for window-based redisplay. */
2672 if (!FRAME_WINDOW_P (it->f))
2673 {
2674 if (it->truncate_lines_p)
2675 it->last_visible_x -= it->truncation_pixel_width;
2676 else
2677 it->last_visible_x -= it->continuation_pixel_width;
2678 }
2679
2680 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2681 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2682 }
2683
2684 /* Leave room for a border glyph. */
2685 if (!FRAME_WINDOW_P (it->f)
2686 && !WINDOW_RIGHTMOST_P (it->w))
2687 it->last_visible_x -= 1;
2688
2689 it->last_visible_y = window_text_bottom_y (w);
2690
2691 /* For mode lines and alike, arrange for the first glyph having a
2692 left box line if the face specifies a box. */
2693 if (base_face_id != DEFAULT_FACE_ID)
2694 {
2695 struct face *face;
2696
2697 it->face_id = base_face_id;
2698
2699 /* If we have a boxed mode line, make the first character appear
2700 with a left box line. */
2701 face = FACE_FROM_ID (it->f, base_face_id);
2702 if (face->box != FACE_NO_BOX)
2703 it->start_of_box_run_p = 1;
2704 }
2705
2706 /* If a buffer position was specified, set the iterator there,
2707 getting overlays and face properties from that position. */
2708 if (charpos >= BUF_BEG (current_buffer))
2709 {
2710 it->end_charpos = ZV;
2711 it->face_id = -1;
2712 IT_CHARPOS (*it) = charpos;
2713
2714 /* Compute byte position if not specified. */
2715 if (bytepos < charpos)
2716 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2717 else
2718 IT_BYTEPOS (*it) = bytepos;
2719
2720 it->start = it->current;
2721
2722 /* Compute faces etc. */
2723 reseat (it, it->current.pos, 1);
2724 }
2725
2726 CHECK_IT (it);
2727 }
2728
2729
2730 /* Initialize IT for the display of window W with window start POS. */
2731
2732 void
2733 start_display (it, w, pos)
2734 struct it *it;
2735 struct window *w;
2736 struct text_pos pos;
2737 {
2738 struct glyph_row *row;
2739 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2740
2741 row = w->desired_matrix->rows + first_vpos;
2742 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2743 it->first_vpos = first_vpos;
2744
2745 /* Don't reseat to previous visible line start if current start
2746 position is in a string or image. */
2747 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2748 {
2749 int start_at_line_beg_p;
2750 int first_y = it->current_y;
2751
2752 /* If window start is not at a line start, skip forward to POS to
2753 get the correct continuation lines width. */
2754 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2755 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2756 if (!start_at_line_beg_p)
2757 {
2758 int new_x;
2759
2760 reseat_at_previous_visible_line_start (it);
2761 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2762
2763 new_x = it->current_x + it->pixel_width;
2764
2765 /* If lines are continued, this line may end in the middle
2766 of a multi-glyph character (e.g. a control character
2767 displayed as \003, or in the middle of an overlay
2768 string). In this case move_it_to above will not have
2769 taken us to the start of the continuation line but to the
2770 end of the continued line. */
2771 if (it->current_x > 0
2772 && !it->truncate_lines_p /* Lines are continued. */
2773 && (/* And glyph doesn't fit on the line. */
2774 new_x > it->last_visible_x
2775 /* Or it fits exactly and we're on a window
2776 system frame. */
2777 || (new_x == it->last_visible_x
2778 && FRAME_WINDOW_P (it->f))))
2779 {
2780 if (it->current.dpvec_index >= 0
2781 || it->current.overlay_string_index >= 0)
2782 {
2783 set_iterator_to_next (it, 1);
2784 move_it_in_display_line_to (it, -1, -1, 0);
2785 }
2786
2787 it->continuation_lines_width += it->current_x;
2788 }
2789
2790 /* We're starting a new display line, not affected by the
2791 height of the continued line, so clear the appropriate
2792 fields in the iterator structure. */
2793 it->max_ascent = it->max_descent = 0;
2794 it->max_phys_ascent = it->max_phys_descent = 0;
2795
2796 it->current_y = first_y;
2797 it->vpos = 0;
2798 it->current_x = it->hpos = 0;
2799 }
2800 }
2801
2802 #if 0 /* Don't assert the following because start_display is sometimes
2803 called intentionally with a window start that is not at a
2804 line start. Please leave this code in as a comment. */
2805
2806 /* Window start should be on a line start, now. */
2807 xassert (it->continuation_lines_width
2808 || IT_CHARPOS (it) == BEGV
2809 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2810 #endif /* 0 */
2811 }
2812
2813
2814 /* Return 1 if POS is a position in ellipses displayed for invisible
2815 text. W is the window we display, for text property lookup. */
2816
2817 static int
2818 in_ellipses_for_invisible_text_p (pos, w)
2819 struct display_pos *pos;
2820 struct window *w;
2821 {
2822 Lisp_Object prop, window;
2823 int ellipses_p = 0;
2824 int charpos = CHARPOS (pos->pos);
2825
2826 /* If POS specifies a position in a display vector, this might
2827 be for an ellipsis displayed for invisible text. We won't
2828 get the iterator set up for delivering that ellipsis unless
2829 we make sure that it gets aware of the invisible text. */
2830 if (pos->dpvec_index >= 0
2831 && pos->overlay_string_index < 0
2832 && CHARPOS (pos->string_pos) < 0
2833 && charpos > BEGV
2834 && (XSETWINDOW (window, w),
2835 prop = Fget_char_property (make_number (charpos),
2836 Qinvisible, window),
2837 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2838 {
2839 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2840 window);
2841 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2842 }
2843
2844 return ellipses_p;
2845 }
2846
2847
2848 /* Initialize IT for stepping through current_buffer in window W,
2849 starting at position POS that includes overlay string and display
2850 vector/ control character translation position information. Value
2851 is zero if there are overlay strings with newlines at POS. */
2852
2853 static int
2854 init_from_display_pos (it, w, pos)
2855 struct it *it;
2856 struct window *w;
2857 struct display_pos *pos;
2858 {
2859 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2860 int i, overlay_strings_with_newlines = 0;
2861
2862 /* If POS specifies a position in a display vector, this might
2863 be for an ellipsis displayed for invisible text. We won't
2864 get the iterator set up for delivering that ellipsis unless
2865 we make sure that it gets aware of the invisible text. */
2866 if (in_ellipses_for_invisible_text_p (pos, w))
2867 {
2868 --charpos;
2869 bytepos = 0;
2870 }
2871
2872 /* Keep in mind: the call to reseat in init_iterator skips invisible
2873 text, so we might end up at a position different from POS. This
2874 is only a problem when POS is a row start after a newline and an
2875 overlay starts there with an after-string, and the overlay has an
2876 invisible property. Since we don't skip invisible text in
2877 display_line and elsewhere immediately after consuming the
2878 newline before the row start, such a POS will not be in a string,
2879 but the call to init_iterator below will move us to the
2880 after-string. */
2881 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2882
2883 /* This only scans the current chunk -- it should scan all chunks.
2884 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2885 to 16 in 22.1 to make this a lesser problem. */
2886 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2887 {
2888 const char *s = SDATA (it->overlay_strings[i]);
2889 const char *e = s + SBYTES (it->overlay_strings[i]);
2890
2891 while (s < e && *s != '\n')
2892 ++s;
2893
2894 if (s < e)
2895 {
2896 overlay_strings_with_newlines = 1;
2897 break;
2898 }
2899 }
2900
2901 /* If position is within an overlay string, set up IT to the right
2902 overlay string. */
2903 if (pos->overlay_string_index >= 0)
2904 {
2905 int relative_index;
2906
2907 /* If the first overlay string happens to have a `display'
2908 property for an image, the iterator will be set up for that
2909 image, and we have to undo that setup first before we can
2910 correct the overlay string index. */
2911 if (it->method == GET_FROM_IMAGE)
2912 pop_it (it);
2913
2914 /* We already have the first chunk of overlay strings in
2915 IT->overlay_strings. Load more until the one for
2916 pos->overlay_string_index is in IT->overlay_strings. */
2917 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2918 {
2919 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2920 it->current.overlay_string_index = 0;
2921 while (n--)
2922 {
2923 load_overlay_strings (it, 0);
2924 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2925 }
2926 }
2927
2928 it->current.overlay_string_index = pos->overlay_string_index;
2929 relative_index = (it->current.overlay_string_index
2930 % OVERLAY_STRING_CHUNK_SIZE);
2931 it->string = it->overlay_strings[relative_index];
2932 xassert (STRINGP (it->string));
2933 it->current.string_pos = pos->string_pos;
2934 it->method = GET_FROM_STRING;
2935 }
2936
2937 #if 0 /* This is bogus because POS not having an overlay string
2938 position does not mean it's after the string. Example: A
2939 line starting with a before-string and initialization of IT
2940 to the previous row's end position. */
2941 else if (it->current.overlay_string_index >= 0)
2942 {
2943 /* If POS says we're already after an overlay string ending at
2944 POS, make sure to pop the iterator because it will be in
2945 front of that overlay string. When POS is ZV, we've thereby
2946 also ``processed'' overlay strings at ZV. */
2947 while (it->sp)
2948 pop_it (it);
2949 xassert (it->current.overlay_string_index == -1);
2950 xassert (it->method == GET_FROM_BUFFER);
2951 if (CHARPOS (pos->pos) == ZV)
2952 it->overlay_strings_at_end_processed_p = 1;
2953 }
2954 #endif /* 0 */
2955
2956 if (CHARPOS (pos->string_pos) >= 0)
2957 {
2958 /* Recorded position is not in an overlay string, but in another
2959 string. This can only be a string from a `display' property.
2960 IT should already be filled with that string. */
2961 it->current.string_pos = pos->string_pos;
2962 xassert (STRINGP (it->string));
2963 }
2964
2965 /* Restore position in display vector translations, control
2966 character translations or ellipses. */
2967 if (pos->dpvec_index >= 0)
2968 {
2969 if (it->dpvec == NULL)
2970 get_next_display_element (it);
2971 xassert (it->dpvec && it->current.dpvec_index == 0);
2972 it->current.dpvec_index = pos->dpvec_index;
2973 }
2974
2975 CHECK_IT (it);
2976 return !overlay_strings_with_newlines;
2977 }
2978
2979
2980 /* Initialize IT for stepping through current_buffer in window W
2981 starting at ROW->start. */
2982
2983 static void
2984 init_to_row_start (it, w, row)
2985 struct it *it;
2986 struct window *w;
2987 struct glyph_row *row;
2988 {
2989 init_from_display_pos (it, w, &row->start);
2990 it->start = row->start;
2991 it->continuation_lines_width = row->continuation_lines_width;
2992 CHECK_IT (it);
2993 }
2994
2995
2996 /* Initialize IT for stepping through current_buffer in window W
2997 starting in the line following ROW, i.e. starting at ROW->end.
2998 Value is zero if there are overlay strings with newlines at ROW's
2999 end position. */
3000
3001 static int
3002 init_to_row_end (it, w, row)
3003 struct it *it;
3004 struct window *w;
3005 struct glyph_row *row;
3006 {
3007 int success = 0;
3008
3009 if (init_from_display_pos (it, w, &row->end))
3010 {
3011 if (row->continued_p)
3012 it->continuation_lines_width
3013 = row->continuation_lines_width + row->pixel_width;
3014 CHECK_IT (it);
3015 success = 1;
3016 }
3017
3018 return success;
3019 }
3020
3021
3022
3023 \f
3024 /***********************************************************************
3025 Text properties
3026 ***********************************************************************/
3027
3028 /* Called when IT reaches IT->stop_charpos. Handle text property and
3029 overlay changes. Set IT->stop_charpos to the next position where
3030 to stop. */
3031
3032 static void
3033 handle_stop (it)
3034 struct it *it;
3035 {
3036 enum prop_handled handled;
3037 int handle_overlay_change_p;
3038 struct props *p;
3039
3040 it->dpvec = NULL;
3041 it->current.dpvec_index = -1;
3042 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3043 it->ignore_overlay_strings_at_pos_p = 0;
3044
3045 /* Use face of preceding text for ellipsis (if invisible) */
3046 if (it->selective_display_ellipsis_p)
3047 it->saved_face_id = it->face_id;
3048
3049 do
3050 {
3051 handled = HANDLED_NORMALLY;
3052
3053 /* Call text property handlers. */
3054 for (p = it_props; p->handler; ++p)
3055 {
3056 handled = p->handler (it);
3057
3058 if (handled == HANDLED_RECOMPUTE_PROPS)
3059 break;
3060 else if (handled == HANDLED_RETURN)
3061 {
3062 /* We still want to show before and after strings from
3063 overlays even if the actual buffer text is replaced. */
3064 if (!handle_overlay_change_p || it->sp > 1)
3065 return;
3066 if (!get_overlay_strings_1 (it, 0, 0))
3067 return;
3068 it->ignore_overlay_strings_at_pos_p = 1;
3069 it->string_from_display_prop_p = 0;
3070 handle_overlay_change_p = 0;
3071 handled = HANDLED_RECOMPUTE_PROPS;
3072 break;
3073 }
3074 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3075 handle_overlay_change_p = 0;
3076 }
3077
3078 if (handled != HANDLED_RECOMPUTE_PROPS)
3079 {
3080 /* Don't check for overlay strings below when set to deliver
3081 characters from a display vector. */
3082 if (it->method == GET_FROM_DISPLAY_VECTOR)
3083 handle_overlay_change_p = 0;
3084
3085 /* Handle overlay changes. */
3086 if (handle_overlay_change_p)
3087 handled = handle_overlay_change (it);
3088
3089 /* Determine where to stop next. */
3090 if (handled == HANDLED_NORMALLY)
3091 compute_stop_pos (it);
3092 }
3093 }
3094 while (handled == HANDLED_RECOMPUTE_PROPS);
3095 }
3096
3097
3098 /* Compute IT->stop_charpos from text property and overlay change
3099 information for IT's current position. */
3100
3101 static void
3102 compute_stop_pos (it)
3103 struct it *it;
3104 {
3105 register INTERVAL iv, next_iv;
3106 Lisp_Object object, limit, position;
3107
3108 /* If nowhere else, stop at the end. */
3109 it->stop_charpos = it->end_charpos;
3110
3111 if (STRINGP (it->string))
3112 {
3113 /* Strings are usually short, so don't limit the search for
3114 properties. */
3115 object = it->string;
3116 limit = Qnil;
3117 position = make_number (IT_STRING_CHARPOS (*it));
3118 }
3119 else
3120 {
3121 int charpos;
3122
3123 /* If next overlay change is in front of the current stop pos
3124 (which is IT->end_charpos), stop there. Note: value of
3125 next_overlay_change is point-max if no overlay change
3126 follows. */
3127 charpos = next_overlay_change (IT_CHARPOS (*it));
3128 if (charpos < it->stop_charpos)
3129 it->stop_charpos = charpos;
3130
3131 /* If showing the region, we have to stop at the region
3132 start or end because the face might change there. */
3133 if (it->region_beg_charpos > 0)
3134 {
3135 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3136 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3137 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3138 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3139 }
3140
3141 /* Set up variables for computing the stop position from text
3142 property changes. */
3143 XSETBUFFER (object, current_buffer);
3144 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3145 position = make_number (IT_CHARPOS (*it));
3146
3147 }
3148
3149 /* Get the interval containing IT's position. Value is a null
3150 interval if there isn't such an interval. */
3151 iv = validate_interval_range (object, &position, &position, 0);
3152 if (!NULL_INTERVAL_P (iv))
3153 {
3154 Lisp_Object values_here[LAST_PROP_IDX];
3155 struct props *p;
3156
3157 /* Get properties here. */
3158 for (p = it_props; p->handler; ++p)
3159 values_here[p->idx] = textget (iv->plist, *p->name);
3160
3161 /* Look for an interval following iv that has different
3162 properties. */
3163 for (next_iv = next_interval (iv);
3164 (!NULL_INTERVAL_P (next_iv)
3165 && (NILP (limit)
3166 || XFASTINT (limit) > next_iv->position));
3167 next_iv = next_interval (next_iv))
3168 {
3169 for (p = it_props; p->handler; ++p)
3170 {
3171 Lisp_Object new_value;
3172
3173 new_value = textget (next_iv->plist, *p->name);
3174 if (!EQ (values_here[p->idx], new_value))
3175 break;
3176 }
3177
3178 if (p->handler)
3179 break;
3180 }
3181
3182 if (!NULL_INTERVAL_P (next_iv))
3183 {
3184 if (INTEGERP (limit)
3185 && next_iv->position >= XFASTINT (limit))
3186 /* No text property change up to limit. */
3187 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3188 else
3189 /* Text properties change in next_iv. */
3190 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3191 }
3192 }
3193
3194 xassert (STRINGP (it->string)
3195 || (it->stop_charpos >= BEGV
3196 && it->stop_charpos >= IT_CHARPOS (*it)));
3197 }
3198
3199
3200 /* Return the position of the next overlay change after POS in
3201 current_buffer. Value is point-max if no overlay change
3202 follows. This is like `next-overlay-change' but doesn't use
3203 xmalloc. */
3204
3205 static int
3206 next_overlay_change (pos)
3207 int pos;
3208 {
3209 int noverlays;
3210 int endpos;
3211 Lisp_Object *overlays;
3212 int i;
3213
3214 /* Get all overlays at the given position. */
3215 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3216
3217 /* If any of these overlays ends before endpos,
3218 use its ending point instead. */
3219 for (i = 0; i < noverlays; ++i)
3220 {
3221 Lisp_Object oend;
3222 int oendpos;
3223
3224 oend = OVERLAY_END (overlays[i]);
3225 oendpos = OVERLAY_POSITION (oend);
3226 endpos = min (endpos, oendpos);
3227 }
3228
3229 return endpos;
3230 }
3231
3232
3233 \f
3234 /***********************************************************************
3235 Fontification
3236 ***********************************************************************/
3237
3238 /* Handle changes in the `fontified' property of the current buffer by
3239 calling hook functions from Qfontification_functions to fontify
3240 regions of text. */
3241
3242 static enum prop_handled
3243 handle_fontified_prop (it)
3244 struct it *it;
3245 {
3246 Lisp_Object prop, pos;
3247 enum prop_handled handled = HANDLED_NORMALLY;
3248
3249 if (!NILP (Vmemory_full))
3250 return handled;
3251
3252 /* Get the value of the `fontified' property at IT's current buffer
3253 position. (The `fontified' property doesn't have a special
3254 meaning in strings.) If the value is nil, call functions from
3255 Qfontification_functions. */
3256 if (!STRINGP (it->string)
3257 && it->s == NULL
3258 && !NILP (Vfontification_functions)
3259 && !NILP (Vrun_hooks)
3260 && (pos = make_number (IT_CHARPOS (*it)),
3261 prop = Fget_char_property (pos, Qfontified, Qnil),
3262 /* Ignore the special cased nil value always present at EOB since
3263 no amount of fontifying will be able to change it. */
3264 NILP (prop) && IT_CHARPOS (*it) < Z))
3265 {
3266 int count = SPECPDL_INDEX ();
3267 Lisp_Object val;
3268
3269 val = Vfontification_functions;
3270 specbind (Qfontification_functions, Qnil);
3271
3272 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3273 safe_call1 (val, pos);
3274 else
3275 {
3276 Lisp_Object globals, fn;
3277 struct gcpro gcpro1, gcpro2;
3278
3279 globals = Qnil;
3280 GCPRO2 (val, globals);
3281
3282 for (; CONSP (val); val = XCDR (val))
3283 {
3284 fn = XCAR (val);
3285
3286 if (EQ (fn, Qt))
3287 {
3288 /* A value of t indicates this hook has a local
3289 binding; it means to run the global binding too.
3290 In a global value, t should not occur. If it
3291 does, we must ignore it to avoid an endless
3292 loop. */
3293 for (globals = Fdefault_value (Qfontification_functions);
3294 CONSP (globals);
3295 globals = XCDR (globals))
3296 {
3297 fn = XCAR (globals);
3298 if (!EQ (fn, Qt))
3299 safe_call1 (fn, pos);
3300 }
3301 }
3302 else
3303 safe_call1 (fn, pos);
3304 }
3305
3306 UNGCPRO;
3307 }
3308
3309 unbind_to (count, Qnil);
3310
3311 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3312 something. This avoids an endless loop if they failed to
3313 fontify the text for which reason ever. */
3314 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3315 handled = HANDLED_RECOMPUTE_PROPS;
3316 }
3317
3318 return handled;
3319 }
3320
3321
3322 \f
3323 /***********************************************************************
3324 Faces
3325 ***********************************************************************/
3326
3327 /* Set up iterator IT from face properties at its current position.
3328 Called from handle_stop. */
3329
3330 static enum prop_handled
3331 handle_face_prop (it)
3332 struct it *it;
3333 {
3334 int new_face_id, next_stop;
3335
3336 if (!STRINGP (it->string))
3337 {
3338 new_face_id
3339 = face_at_buffer_position (it->w,
3340 IT_CHARPOS (*it),
3341 it->region_beg_charpos,
3342 it->region_end_charpos,
3343 &next_stop,
3344 (IT_CHARPOS (*it)
3345 + TEXT_PROP_DISTANCE_LIMIT),
3346 0);
3347
3348 /* Is this a start of a run of characters with box face?
3349 Caveat: this can be called for a freshly initialized
3350 iterator; face_id is -1 in this case. We know that the new
3351 face will not change until limit, i.e. if the new face has a
3352 box, all characters up to limit will have one. But, as
3353 usual, we don't know whether limit is really the end. */
3354 if (new_face_id != it->face_id)
3355 {
3356 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3357
3358 /* If new face has a box but old face has not, this is
3359 the start of a run of characters with box, i.e. it has
3360 a shadow on the left side. The value of face_id of the
3361 iterator will be -1 if this is the initial call that gets
3362 the face. In this case, we have to look in front of IT's
3363 position and see whether there is a face != new_face_id. */
3364 it->start_of_box_run_p
3365 = (new_face->box != FACE_NO_BOX
3366 && (it->face_id >= 0
3367 || IT_CHARPOS (*it) == BEG
3368 || new_face_id != face_before_it_pos (it)));
3369 it->face_box_p = new_face->box != FACE_NO_BOX;
3370 }
3371 }
3372 else
3373 {
3374 int base_face_id, bufpos;
3375
3376 if (it->current.overlay_string_index >= 0)
3377 bufpos = IT_CHARPOS (*it);
3378 else
3379 bufpos = 0;
3380
3381 /* For strings from a buffer, i.e. overlay strings or strings
3382 from a `display' property, use the face at IT's current
3383 buffer position as the base face to merge with, so that
3384 overlay strings appear in the same face as surrounding
3385 text, unless they specify their own faces. */
3386 base_face_id = underlying_face_id (it);
3387
3388 new_face_id = face_at_string_position (it->w,
3389 it->string,
3390 IT_STRING_CHARPOS (*it),
3391 bufpos,
3392 it->region_beg_charpos,
3393 it->region_end_charpos,
3394 &next_stop,
3395 base_face_id, 0);
3396
3397 #if 0 /* This shouldn't be neccessary. Let's check it. */
3398 /* If IT is used to display a mode line we would really like to
3399 use the mode line face instead of the frame's default face. */
3400 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3401 && new_face_id == DEFAULT_FACE_ID)
3402 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3403 #endif
3404
3405 /* Is this a start of a run of characters with box? Caveat:
3406 this can be called for a freshly allocated iterator; face_id
3407 is -1 is this case. We know that the new face will not
3408 change until the next check pos, i.e. if the new face has a
3409 box, all characters up to that position will have a
3410 box. But, as usual, we don't know whether that position
3411 is really the end. */
3412 if (new_face_id != it->face_id)
3413 {
3414 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3415 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3416
3417 /* If new face has a box but old face hasn't, this is the
3418 start of a run of characters with box, i.e. it has a
3419 shadow on the left side. */
3420 it->start_of_box_run_p
3421 = new_face->box && (old_face == NULL || !old_face->box);
3422 it->face_box_p = new_face->box != FACE_NO_BOX;
3423 }
3424 }
3425
3426 it->face_id = new_face_id;
3427 return HANDLED_NORMALLY;
3428 }
3429
3430
3431 /* Return the ID of the face ``underlying'' IT's current position,
3432 which is in a string. If the iterator is associated with a
3433 buffer, return the face at IT's current buffer position.
3434 Otherwise, use the iterator's base_face_id. */
3435
3436 static int
3437 underlying_face_id (it)
3438 struct it *it;
3439 {
3440 int face_id = it->base_face_id, i;
3441
3442 xassert (STRINGP (it->string));
3443
3444 for (i = it->sp - 1; i >= 0; --i)
3445 if (NILP (it->stack[i].string))
3446 face_id = it->stack[i].face_id;
3447
3448 return face_id;
3449 }
3450
3451
3452 /* Compute the face one character before or after the current position
3453 of IT. BEFORE_P non-zero means get the face in front of IT's
3454 position. Value is the id of the face. */
3455
3456 static int
3457 face_before_or_after_it_pos (it, before_p)
3458 struct it *it;
3459 int before_p;
3460 {
3461 int face_id, limit;
3462 int next_check_charpos;
3463 struct text_pos pos;
3464
3465 xassert (it->s == NULL);
3466
3467 if (STRINGP (it->string))
3468 {
3469 int bufpos, base_face_id;
3470
3471 /* No face change past the end of the string (for the case
3472 we are padding with spaces). No face change before the
3473 string start. */
3474 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3475 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3476 return it->face_id;
3477
3478 /* Set pos to the position before or after IT's current position. */
3479 if (before_p)
3480 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3481 else
3482 /* For composition, we must check the character after the
3483 composition. */
3484 pos = (it->what == IT_COMPOSITION
3485 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3486 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3487
3488 if (it->current.overlay_string_index >= 0)
3489 bufpos = IT_CHARPOS (*it);
3490 else
3491 bufpos = 0;
3492
3493 base_face_id = underlying_face_id (it);
3494
3495 /* Get the face for ASCII, or unibyte. */
3496 face_id = face_at_string_position (it->w,
3497 it->string,
3498 CHARPOS (pos),
3499 bufpos,
3500 it->region_beg_charpos,
3501 it->region_end_charpos,
3502 &next_check_charpos,
3503 base_face_id, 0);
3504
3505 /* Correct the face for charsets different from ASCII. Do it
3506 for the multibyte case only. The face returned above is
3507 suitable for unibyte text if IT->string is unibyte. */
3508 if (STRING_MULTIBYTE (it->string))
3509 {
3510 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3511 int rest = SBYTES (it->string) - BYTEPOS (pos);
3512 int c, len;
3513 struct face *face = FACE_FROM_ID (it->f, face_id);
3514
3515 c = string_char_and_length (p, rest, &len);
3516 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3517 }
3518 }
3519 else
3520 {
3521 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3522 || (IT_CHARPOS (*it) <= BEGV && before_p))
3523 return it->face_id;
3524
3525 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3526 pos = it->current.pos;
3527
3528 if (before_p)
3529 DEC_TEXT_POS (pos, it->multibyte_p);
3530 else
3531 {
3532 if (it->what == IT_COMPOSITION)
3533 /* For composition, we must check the position after the
3534 composition. */
3535 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3536 else
3537 INC_TEXT_POS (pos, it->multibyte_p);
3538 }
3539
3540 /* Determine face for CHARSET_ASCII, or unibyte. */
3541 face_id = face_at_buffer_position (it->w,
3542 CHARPOS (pos),
3543 it->region_beg_charpos,
3544 it->region_end_charpos,
3545 &next_check_charpos,
3546 limit, 0);
3547
3548 /* Correct the face for charsets different from ASCII. Do it
3549 for the multibyte case only. The face returned above is
3550 suitable for unibyte text if current_buffer is unibyte. */
3551 if (it->multibyte_p)
3552 {
3553 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3554 struct face *face = FACE_FROM_ID (it->f, face_id);
3555 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3556 }
3557 }
3558
3559 return face_id;
3560 }
3561
3562
3563 \f
3564 /***********************************************************************
3565 Invisible text
3566 ***********************************************************************/
3567
3568 /* Set up iterator IT from invisible properties at its current
3569 position. Called from handle_stop. */
3570
3571 static enum prop_handled
3572 handle_invisible_prop (it)
3573 struct it *it;
3574 {
3575 enum prop_handled handled = HANDLED_NORMALLY;
3576
3577 if (STRINGP (it->string))
3578 {
3579 extern Lisp_Object Qinvisible;
3580 Lisp_Object prop, end_charpos, limit, charpos;
3581
3582 /* Get the value of the invisible text property at the
3583 current position. Value will be nil if there is no such
3584 property. */
3585 charpos = make_number (IT_STRING_CHARPOS (*it));
3586 prop = Fget_text_property (charpos, Qinvisible, it->string);
3587
3588 if (!NILP (prop)
3589 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3590 {
3591 handled = HANDLED_RECOMPUTE_PROPS;
3592
3593 /* Get the position at which the next change of the
3594 invisible text property can be found in IT->string.
3595 Value will be nil if the property value is the same for
3596 all the rest of IT->string. */
3597 XSETINT (limit, SCHARS (it->string));
3598 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3599 it->string, limit);
3600
3601 /* Text at current position is invisible. The next
3602 change in the property is at position end_charpos.
3603 Move IT's current position to that position. */
3604 if (INTEGERP (end_charpos)
3605 && XFASTINT (end_charpos) < XFASTINT (limit))
3606 {
3607 struct text_pos old;
3608 old = it->current.string_pos;
3609 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3610 compute_string_pos (&it->current.string_pos, old, it->string);
3611 }
3612 else
3613 {
3614 /* The rest of the string is invisible. If this is an
3615 overlay string, proceed with the next overlay string
3616 or whatever comes and return a character from there. */
3617 if (it->current.overlay_string_index >= 0)
3618 {
3619 next_overlay_string (it);
3620 /* Don't check for overlay strings when we just
3621 finished processing them. */
3622 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3623 }
3624 else
3625 {
3626 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3627 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3628 }
3629 }
3630 }
3631 }
3632 else
3633 {
3634 int invis_p, newpos, next_stop, start_charpos;
3635 Lisp_Object pos, prop, overlay;
3636
3637 /* First of all, is there invisible text at this position? */
3638 start_charpos = IT_CHARPOS (*it);
3639 pos = make_number (IT_CHARPOS (*it));
3640 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3641 &overlay);
3642 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3643
3644 /* If we are on invisible text, skip over it. */
3645 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3646 {
3647 /* Record whether we have to display an ellipsis for the
3648 invisible text. */
3649 int display_ellipsis_p = invis_p == 2;
3650
3651 handled = HANDLED_RECOMPUTE_PROPS;
3652
3653 /* Loop skipping over invisible text. The loop is left at
3654 ZV or with IT on the first char being visible again. */
3655 do
3656 {
3657 /* Try to skip some invisible text. Return value is the
3658 position reached which can be equal to IT's position
3659 if there is nothing invisible here. This skips both
3660 over invisible text properties and overlays with
3661 invisible property. */
3662 newpos = skip_invisible (IT_CHARPOS (*it),
3663 &next_stop, ZV, it->window);
3664
3665 /* If we skipped nothing at all we weren't at invisible
3666 text in the first place. If everything to the end of
3667 the buffer was skipped, end the loop. */
3668 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3669 invis_p = 0;
3670 else
3671 {
3672 /* We skipped some characters but not necessarily
3673 all there are. Check if we ended up on visible
3674 text. Fget_char_property returns the property of
3675 the char before the given position, i.e. if we
3676 get invis_p = 0, this means that the char at
3677 newpos is visible. */
3678 pos = make_number (newpos);
3679 prop = Fget_char_property (pos, Qinvisible, it->window);
3680 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3681 }
3682
3683 /* If we ended up on invisible text, proceed to
3684 skip starting with next_stop. */
3685 if (invis_p)
3686 IT_CHARPOS (*it) = next_stop;
3687
3688 /* If there are adjacent invisible texts, don't lose the
3689 second one's ellipsis. */
3690 if (invis_p == 2)
3691 display_ellipsis_p = 1;
3692 }
3693 while (invis_p);
3694
3695 /* The position newpos is now either ZV or on visible text. */
3696 IT_CHARPOS (*it) = newpos;
3697 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3698
3699 /* If there are before-strings at the start of invisible
3700 text, and the text is invisible because of a text
3701 property, arrange to show before-strings because 20.x did
3702 it that way. (If the text is invisible because of an
3703 overlay property instead of a text property, this is
3704 already handled in the overlay code.) */
3705 if (NILP (overlay)
3706 && get_overlay_strings (it, start_charpos))
3707 {
3708 handled = HANDLED_RECOMPUTE_PROPS;
3709 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3710 }
3711 else if (display_ellipsis_p)
3712 {
3713 /* Make sure that the glyphs of the ellipsis will get
3714 correct `charpos' values. If we would not update
3715 it->position here, the glyphs would belong to the
3716 last visible character _before_ the invisible
3717 text, which confuses `set_cursor_from_row'.
3718
3719 We use the last invisible position instead of the
3720 first because this way the cursor is always drawn on
3721 the first "." of the ellipsis, whenever PT is inside
3722 the invisible text. Otherwise the cursor would be
3723 placed _after_ the ellipsis when the point is after the
3724 first invisible character. */
3725 if (!STRINGP (it->object))
3726 {
3727 it->position.charpos = IT_CHARPOS (*it) - 1;
3728 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3729 }
3730 setup_for_ellipsis (it, 0);
3731 }
3732 }
3733 }
3734
3735 return handled;
3736 }
3737
3738
3739 /* Make iterator IT return `...' next.
3740 Replaces LEN characters from buffer. */
3741
3742 static void
3743 setup_for_ellipsis (it, len)
3744 struct it *it;
3745 int len;
3746 {
3747 /* Use the display table definition for `...'. Invalid glyphs
3748 will be handled by the method returning elements from dpvec. */
3749 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3750 {
3751 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3752 it->dpvec = v->contents;
3753 it->dpend = v->contents + v->size;
3754 }
3755 else
3756 {
3757 /* Default `...'. */
3758 it->dpvec = default_invis_vector;
3759 it->dpend = default_invis_vector + 3;
3760 }
3761
3762 it->dpvec_char_len = len;
3763 it->current.dpvec_index = 0;
3764 it->dpvec_face_id = -1;
3765
3766 /* Remember the current face id in case glyphs specify faces.
3767 IT's face is restored in set_iterator_to_next.
3768 saved_face_id was set to preceding char's face in handle_stop. */
3769 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3770 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3771
3772 it->method = GET_FROM_DISPLAY_VECTOR;
3773 it->ellipsis_p = 1;
3774 }
3775
3776
3777 \f
3778 /***********************************************************************
3779 'display' property
3780 ***********************************************************************/
3781
3782 /* Set up iterator IT from `display' property at its current position.
3783 Called from handle_stop.
3784 We return HANDLED_RETURN if some part of the display property
3785 overrides the display of the buffer text itself.
3786 Otherwise we return HANDLED_NORMALLY. */
3787
3788 static enum prop_handled
3789 handle_display_prop (it)
3790 struct it *it;
3791 {
3792 Lisp_Object prop, object;
3793 struct text_pos *position;
3794 /* Nonzero if some property replaces the display of the text itself. */
3795 int display_replaced_p = 0;
3796
3797 if (STRINGP (it->string))
3798 {
3799 object = it->string;
3800 position = &it->current.string_pos;
3801 }
3802 else
3803 {
3804 XSETWINDOW (object, it->w);
3805 position = &it->current.pos;
3806 }
3807
3808 /* Reset those iterator values set from display property values. */
3809 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3810 it->space_width = Qnil;
3811 it->font_height = Qnil;
3812 it->voffset = 0;
3813
3814 /* We don't support recursive `display' properties, i.e. string
3815 values that have a string `display' property, that have a string
3816 `display' property etc. */
3817 if (!it->string_from_display_prop_p)
3818 it->area = TEXT_AREA;
3819
3820 prop = Fget_char_property (make_number (position->charpos),
3821 Qdisplay, object);
3822 if (NILP (prop))
3823 return HANDLED_NORMALLY;
3824
3825 if (!STRINGP (it->string))
3826 object = it->w->buffer;
3827
3828 if (CONSP (prop)
3829 /* Simple properties. */
3830 && !EQ (XCAR (prop), Qimage)
3831 && !EQ (XCAR (prop), Qspace)
3832 && !EQ (XCAR (prop), Qwhen)
3833 && !EQ (XCAR (prop), Qslice)
3834 && !EQ (XCAR (prop), Qspace_width)
3835 && !EQ (XCAR (prop), Qheight)
3836 && !EQ (XCAR (prop), Qraise)
3837 /* Marginal area specifications. */
3838 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3839 && !EQ (XCAR (prop), Qleft_fringe)
3840 && !EQ (XCAR (prop), Qright_fringe)
3841 && !NILP (XCAR (prop)))
3842 {
3843 for (; CONSP (prop); prop = XCDR (prop))
3844 {
3845 if (handle_single_display_spec (it, XCAR (prop), object,
3846 position, display_replaced_p))
3847 display_replaced_p = 1;
3848 }
3849 }
3850 else if (VECTORP (prop))
3851 {
3852 int i;
3853 for (i = 0; i < ASIZE (prop); ++i)
3854 if (handle_single_display_spec (it, AREF (prop, i), object,
3855 position, display_replaced_p))
3856 display_replaced_p = 1;
3857 }
3858 else
3859 {
3860 int ret = handle_single_display_spec (it, prop, object, position, 0);
3861 if (ret < 0) /* Replaced by "", i.e. nothing. */
3862 return HANDLED_RECOMPUTE_PROPS;
3863 if (ret)
3864 display_replaced_p = 1;
3865 }
3866
3867 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3868 }
3869
3870
3871 /* Value is the position of the end of the `display' property starting
3872 at START_POS in OBJECT. */
3873
3874 static struct text_pos
3875 display_prop_end (it, object, start_pos)
3876 struct it *it;
3877 Lisp_Object object;
3878 struct text_pos start_pos;
3879 {
3880 Lisp_Object end;
3881 struct text_pos end_pos;
3882
3883 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3884 Qdisplay, object, Qnil);
3885 CHARPOS (end_pos) = XFASTINT (end);
3886 if (STRINGP (object))
3887 compute_string_pos (&end_pos, start_pos, it->string);
3888 else
3889 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3890
3891 return end_pos;
3892 }
3893
3894
3895 /* Set up IT from a single `display' specification PROP. OBJECT
3896 is the object in which the `display' property was found. *POSITION
3897 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3898 means that we previously saw a display specification which already
3899 replaced text display with something else, for example an image;
3900 we ignore such properties after the first one has been processed.
3901
3902 If PROP is a `space' or `image' specification, and in some other
3903 cases too, set *POSITION to the position where the `display'
3904 property ends.
3905
3906 Value is non-zero if something was found which replaces the display
3907 of buffer or string text. Specifically, the value is -1 if that
3908 "something" is "nothing". */
3909
3910 static int
3911 handle_single_display_spec (it, spec, object, position,
3912 display_replaced_before_p)
3913 struct it *it;
3914 Lisp_Object spec;
3915 Lisp_Object object;
3916 struct text_pos *position;
3917 int display_replaced_before_p;
3918 {
3919 Lisp_Object form;
3920 Lisp_Object location, value;
3921 struct text_pos start_pos, save_pos;
3922 int valid_p;
3923
3924 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3925 If the result is non-nil, use VALUE instead of SPEC. */
3926 form = Qt;
3927 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3928 {
3929 spec = XCDR (spec);
3930 if (!CONSP (spec))
3931 return 0;
3932 form = XCAR (spec);
3933 spec = XCDR (spec);
3934 }
3935
3936 if (!NILP (form) && !EQ (form, Qt))
3937 {
3938 int count = SPECPDL_INDEX ();
3939 struct gcpro gcpro1;
3940
3941 /* Bind `object' to the object having the `display' property, a
3942 buffer or string. Bind `position' to the position in the
3943 object where the property was found, and `buffer-position'
3944 to the current position in the buffer. */
3945 specbind (Qobject, object);
3946 specbind (Qposition, make_number (CHARPOS (*position)));
3947 specbind (Qbuffer_position,
3948 make_number (STRINGP (object)
3949 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3950 GCPRO1 (form);
3951 form = safe_eval (form);
3952 UNGCPRO;
3953 unbind_to (count, Qnil);
3954 }
3955
3956 if (NILP (form))
3957 return 0;
3958
3959 /* Handle `(height HEIGHT)' specifications. */
3960 if (CONSP (spec)
3961 && EQ (XCAR (spec), Qheight)
3962 && CONSP (XCDR (spec)))
3963 {
3964 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3965 return 0;
3966
3967 it->font_height = XCAR (XCDR (spec));
3968 if (!NILP (it->font_height))
3969 {
3970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3971 int new_height = -1;
3972
3973 if (CONSP (it->font_height)
3974 && (EQ (XCAR (it->font_height), Qplus)
3975 || EQ (XCAR (it->font_height), Qminus))
3976 && CONSP (XCDR (it->font_height))
3977 && INTEGERP (XCAR (XCDR (it->font_height))))
3978 {
3979 /* `(+ N)' or `(- N)' where N is an integer. */
3980 int steps = XINT (XCAR (XCDR (it->font_height)));
3981 if (EQ (XCAR (it->font_height), Qplus))
3982 steps = - steps;
3983 it->face_id = smaller_face (it->f, it->face_id, steps);
3984 }
3985 else if (FUNCTIONP (it->font_height))
3986 {
3987 /* Call function with current height as argument.
3988 Value is the new height. */
3989 Lisp_Object height;
3990 height = safe_call1 (it->font_height,
3991 face->lface[LFACE_HEIGHT_INDEX]);
3992 if (NUMBERP (height))
3993 new_height = XFLOATINT (height);
3994 }
3995 else if (NUMBERP (it->font_height))
3996 {
3997 /* Value is a multiple of the canonical char height. */
3998 struct face *face;
3999
4000 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4001 new_height = (XFLOATINT (it->font_height)
4002 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4003 }
4004 else
4005 {
4006 /* Evaluate IT->font_height with `height' bound to the
4007 current specified height to get the new height. */
4008 int count = SPECPDL_INDEX ();
4009
4010 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4011 value = safe_eval (it->font_height);
4012 unbind_to (count, Qnil);
4013
4014 if (NUMBERP (value))
4015 new_height = XFLOATINT (value);
4016 }
4017
4018 if (new_height > 0)
4019 it->face_id = face_with_height (it->f, it->face_id, new_height);
4020 }
4021
4022 return 0;
4023 }
4024
4025 /* Handle `(space_width WIDTH)'. */
4026 if (CONSP (spec)
4027 && EQ (XCAR (spec), Qspace_width)
4028 && CONSP (XCDR (spec)))
4029 {
4030 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4031 return 0;
4032
4033 value = XCAR (XCDR (spec));
4034 if (NUMBERP (value) && XFLOATINT (value) > 0)
4035 it->space_width = value;
4036
4037 return 0;
4038 }
4039
4040 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4041 if (CONSP (spec)
4042 && EQ (XCAR (spec), Qslice))
4043 {
4044 Lisp_Object tem;
4045
4046 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4047 return 0;
4048
4049 if (tem = XCDR (spec), CONSP (tem))
4050 {
4051 it->slice.x = XCAR (tem);
4052 if (tem = XCDR (tem), CONSP (tem))
4053 {
4054 it->slice.y = XCAR (tem);
4055 if (tem = XCDR (tem), CONSP (tem))
4056 {
4057 it->slice.width = XCAR (tem);
4058 if (tem = XCDR (tem), CONSP (tem))
4059 it->slice.height = XCAR (tem);
4060 }
4061 }
4062 }
4063
4064 return 0;
4065 }
4066
4067 /* Handle `(raise FACTOR)'. */
4068 if (CONSP (spec)
4069 && EQ (XCAR (spec), Qraise)
4070 && CONSP (XCDR (spec)))
4071 {
4072 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4073 return 0;
4074
4075 #ifdef HAVE_WINDOW_SYSTEM
4076 value = XCAR (XCDR (spec));
4077 if (NUMBERP (value))
4078 {
4079 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4080 it->voffset = - (XFLOATINT (value)
4081 * (FONT_HEIGHT (face->font)));
4082 }
4083 #endif /* HAVE_WINDOW_SYSTEM */
4084
4085 return 0;
4086 }
4087
4088 /* Don't handle the other kinds of display specifications
4089 inside a string that we got from a `display' property. */
4090 if (it->string_from_display_prop_p)
4091 return 0;
4092
4093 /* Characters having this form of property are not displayed, so
4094 we have to find the end of the property. */
4095 start_pos = *position;
4096 *position = display_prop_end (it, object, start_pos);
4097 value = Qnil;
4098
4099 /* Stop the scan at that end position--we assume that all
4100 text properties change there. */
4101 it->stop_charpos = position->charpos;
4102
4103 /* Handle `(left-fringe BITMAP [FACE])'
4104 and `(right-fringe BITMAP [FACE])'. */
4105 if (CONSP (spec)
4106 && (EQ (XCAR (spec), Qleft_fringe)
4107 || EQ (XCAR (spec), Qright_fringe))
4108 && CONSP (XCDR (spec)))
4109 {
4110 int face_id = DEFAULT_FACE_ID;
4111 int fringe_bitmap;
4112
4113 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4114 /* If we return here, POSITION has been advanced
4115 across the text with this property. */
4116 return 0;
4117
4118 #ifdef HAVE_WINDOW_SYSTEM
4119 value = XCAR (XCDR (spec));
4120 if (!SYMBOLP (value)
4121 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4122 /* If we return here, POSITION has been advanced
4123 across the text with this property. */
4124 return 0;
4125
4126 if (CONSP (XCDR (XCDR (spec))))
4127 {
4128 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4129 int face_id2 = lookup_derived_face (it->f, face_name,
4130 FRINGE_FACE_ID, 0);
4131 if (face_id2 >= 0)
4132 face_id = face_id2;
4133 }
4134
4135 /* Save current settings of IT so that we can restore them
4136 when we are finished with the glyph property value. */
4137
4138 save_pos = it->position;
4139 it->position = *position;
4140 push_it (it);
4141 it->position = save_pos;
4142
4143 it->area = TEXT_AREA;
4144 it->what = IT_IMAGE;
4145 it->image_id = -1; /* no image */
4146 it->position = start_pos;
4147 it->object = NILP (object) ? it->w->buffer : object;
4148 it->method = GET_FROM_IMAGE;
4149 it->face_id = face_id;
4150
4151 /* Say that we haven't consumed the characters with
4152 `display' property yet. The call to pop_it in
4153 set_iterator_to_next will clean this up. */
4154 *position = start_pos;
4155
4156 if (EQ (XCAR (spec), Qleft_fringe))
4157 {
4158 it->left_user_fringe_bitmap = fringe_bitmap;
4159 it->left_user_fringe_face_id = face_id;
4160 }
4161 else
4162 {
4163 it->right_user_fringe_bitmap = fringe_bitmap;
4164 it->right_user_fringe_face_id = face_id;
4165 }
4166 #endif /* HAVE_WINDOW_SYSTEM */
4167 return 1;
4168 }
4169
4170 /* Prepare to handle `((margin left-margin) ...)',
4171 `((margin right-margin) ...)' and `((margin nil) ...)'
4172 prefixes for display specifications. */
4173 location = Qunbound;
4174 if (CONSP (spec) && CONSP (XCAR (spec)))
4175 {
4176 Lisp_Object tem;
4177
4178 value = XCDR (spec);
4179 if (CONSP (value))
4180 value = XCAR (value);
4181
4182 tem = XCAR (spec);
4183 if (EQ (XCAR (tem), Qmargin)
4184 && (tem = XCDR (tem),
4185 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4186 (NILP (tem)
4187 || EQ (tem, Qleft_margin)
4188 || EQ (tem, Qright_margin))))
4189 location = tem;
4190 }
4191
4192 if (EQ (location, Qunbound))
4193 {
4194 location = Qnil;
4195 value = spec;
4196 }
4197
4198 /* After this point, VALUE is the property after any
4199 margin prefix has been stripped. It must be a string,
4200 an image specification, or `(space ...)'.
4201
4202 LOCATION specifies where to display: `left-margin',
4203 `right-margin' or nil. */
4204
4205 valid_p = (STRINGP (value)
4206 #ifdef HAVE_WINDOW_SYSTEM
4207 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4208 #endif /* not HAVE_WINDOW_SYSTEM */
4209 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4210
4211 if (valid_p && !display_replaced_before_p)
4212 {
4213 /* Save current settings of IT so that we can restore them
4214 when we are finished with the glyph property value. */
4215 save_pos = it->position;
4216 it->position = *position;
4217 push_it (it);
4218 it->position = save_pos;
4219
4220 if (NILP (location))
4221 it->area = TEXT_AREA;
4222 else if (EQ (location, Qleft_margin))
4223 it->area = LEFT_MARGIN_AREA;
4224 else
4225 it->area = RIGHT_MARGIN_AREA;
4226
4227 if (STRINGP (value))
4228 {
4229 if (SCHARS (value) == 0)
4230 {
4231 pop_it (it);
4232 return -1; /* Replaced by "", i.e. nothing. */
4233 }
4234 it->string = value;
4235 it->multibyte_p = STRING_MULTIBYTE (it->string);
4236 it->current.overlay_string_index = -1;
4237 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4238 it->end_charpos = it->string_nchars = SCHARS (it->string);
4239 it->method = GET_FROM_STRING;
4240 it->stop_charpos = 0;
4241 it->string_from_display_prop_p = 1;
4242 /* Say that we haven't consumed the characters with
4243 `display' property yet. The call to pop_it in
4244 set_iterator_to_next will clean this up. */
4245 *position = start_pos;
4246 }
4247 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4248 {
4249 it->method = GET_FROM_STRETCH;
4250 it->object = value;
4251 *position = it->position = start_pos;
4252 }
4253 #ifdef HAVE_WINDOW_SYSTEM
4254 else
4255 {
4256 it->what = IT_IMAGE;
4257 it->image_id = lookup_image (it->f, value);
4258 it->position = start_pos;
4259 it->object = NILP (object) ? it->w->buffer : object;
4260 it->method = GET_FROM_IMAGE;
4261
4262 /* Say that we haven't consumed the characters with
4263 `display' property yet. The call to pop_it in
4264 set_iterator_to_next will clean this up. */
4265 *position = start_pos;
4266 }
4267 #endif /* HAVE_WINDOW_SYSTEM */
4268
4269 return 1;
4270 }
4271
4272 /* Invalid property or property not supported. Restore
4273 POSITION to what it was before. */
4274 *position = start_pos;
4275 return 0;
4276 }
4277
4278
4279 /* Check if SPEC is a display specification value whose text should be
4280 treated as intangible. */
4281
4282 static int
4283 single_display_spec_intangible_p (prop)
4284 Lisp_Object prop;
4285 {
4286 /* Skip over `when FORM'. */
4287 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4288 {
4289 prop = XCDR (prop);
4290 if (!CONSP (prop))
4291 return 0;
4292 prop = XCDR (prop);
4293 }
4294
4295 if (STRINGP (prop))
4296 return 1;
4297
4298 if (!CONSP (prop))
4299 return 0;
4300
4301 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4302 we don't need to treat text as intangible. */
4303 if (EQ (XCAR (prop), Qmargin))
4304 {
4305 prop = XCDR (prop);
4306 if (!CONSP (prop))
4307 return 0;
4308
4309 prop = XCDR (prop);
4310 if (!CONSP (prop)
4311 || EQ (XCAR (prop), Qleft_margin)
4312 || EQ (XCAR (prop), Qright_margin))
4313 return 0;
4314 }
4315
4316 return (CONSP (prop)
4317 && (EQ (XCAR (prop), Qimage)
4318 || EQ (XCAR (prop), Qspace)));
4319 }
4320
4321
4322 /* Check if PROP is a display property value whose text should be
4323 treated as intangible. */
4324
4325 int
4326 display_prop_intangible_p (prop)
4327 Lisp_Object prop;
4328 {
4329 if (CONSP (prop)
4330 && CONSP (XCAR (prop))
4331 && !EQ (Qmargin, XCAR (XCAR (prop))))
4332 {
4333 /* A list of sub-properties. */
4334 while (CONSP (prop))
4335 {
4336 if (single_display_spec_intangible_p (XCAR (prop)))
4337 return 1;
4338 prop = XCDR (prop);
4339 }
4340 }
4341 else if (VECTORP (prop))
4342 {
4343 /* A vector of sub-properties. */
4344 int i;
4345 for (i = 0; i < ASIZE (prop); ++i)
4346 if (single_display_spec_intangible_p (AREF (prop, i)))
4347 return 1;
4348 }
4349 else
4350 return single_display_spec_intangible_p (prop);
4351
4352 return 0;
4353 }
4354
4355
4356 /* Return 1 if PROP is a display sub-property value containing STRING. */
4357
4358 static int
4359 single_display_spec_string_p (prop, string)
4360 Lisp_Object prop, string;
4361 {
4362 if (EQ (string, prop))
4363 return 1;
4364
4365 /* Skip over `when FORM'. */
4366 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4367 {
4368 prop = XCDR (prop);
4369 if (!CONSP (prop))
4370 return 0;
4371 prop = XCDR (prop);
4372 }
4373
4374 if (CONSP (prop))
4375 /* Skip over `margin LOCATION'. */
4376 if (EQ (XCAR (prop), Qmargin))
4377 {
4378 prop = XCDR (prop);
4379 if (!CONSP (prop))
4380 return 0;
4381
4382 prop = XCDR (prop);
4383 if (!CONSP (prop))
4384 return 0;
4385 }
4386
4387 return CONSP (prop) && EQ (XCAR (prop), string);
4388 }
4389
4390
4391 /* Return 1 if STRING appears in the `display' property PROP. */
4392
4393 static int
4394 display_prop_string_p (prop, string)
4395 Lisp_Object prop, string;
4396 {
4397 if (CONSP (prop)
4398 && CONSP (XCAR (prop))
4399 && !EQ (Qmargin, XCAR (XCAR (prop))))
4400 {
4401 /* A list of sub-properties. */
4402 while (CONSP (prop))
4403 {
4404 if (single_display_spec_string_p (XCAR (prop), string))
4405 return 1;
4406 prop = XCDR (prop);
4407 }
4408 }
4409 else if (VECTORP (prop))
4410 {
4411 /* A vector of sub-properties. */
4412 int i;
4413 for (i = 0; i < ASIZE (prop); ++i)
4414 if (single_display_spec_string_p (AREF (prop, i), string))
4415 return 1;
4416 }
4417 else
4418 return single_display_spec_string_p (prop, string);
4419
4420 return 0;
4421 }
4422
4423
4424 /* Determine from which buffer position in W's buffer STRING comes
4425 from. AROUND_CHARPOS is an approximate position where it could
4426 be from. Value is the buffer position or 0 if it couldn't be
4427 determined.
4428
4429 W's buffer must be current.
4430
4431 This function is necessary because we don't record buffer positions
4432 in glyphs generated from strings (to keep struct glyph small).
4433 This function may only use code that doesn't eval because it is
4434 called asynchronously from note_mouse_highlight. */
4435
4436 int
4437 string_buffer_position (w, string, around_charpos)
4438 struct window *w;
4439 Lisp_Object string;
4440 int around_charpos;
4441 {
4442 Lisp_Object limit, prop, pos;
4443 const int MAX_DISTANCE = 1000;
4444 int found = 0;
4445
4446 pos = make_number (around_charpos);
4447 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4448 while (!found && !EQ (pos, limit))
4449 {
4450 prop = Fget_char_property (pos, Qdisplay, Qnil);
4451 if (!NILP (prop) && display_prop_string_p (prop, string))
4452 found = 1;
4453 else
4454 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4455 }
4456
4457 if (!found)
4458 {
4459 pos = make_number (around_charpos);
4460 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4461 while (!found && !EQ (pos, limit))
4462 {
4463 prop = Fget_char_property (pos, Qdisplay, Qnil);
4464 if (!NILP (prop) && display_prop_string_p (prop, string))
4465 found = 1;
4466 else
4467 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4468 limit);
4469 }
4470 }
4471
4472 return found ? XINT (pos) : 0;
4473 }
4474
4475
4476 \f
4477 /***********************************************************************
4478 `composition' property
4479 ***********************************************************************/
4480
4481 static enum prop_handled
4482 handle_auto_composed_prop (it)
4483 struct it *it;
4484 {
4485 enum prop_handled handled = HANDLED_NORMALLY;
4486
4487 if (FUNCTIONP (Vauto_composition_function))
4488 {
4489 Lisp_Object val;
4490 EMACS_INT pos, this_pos;
4491
4492 if (STRINGP (it->string))
4493 pos = IT_STRING_CHARPOS (*it);
4494 else
4495 pos = IT_CHARPOS (*it);
4496 this_pos = pos;
4497
4498 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4499 if (! NILP (val))
4500 {
4501 Lisp_Object limit = Qnil, next;
4502
4503 /* As Fnext_single_char_property_change is very slow, we
4504 limit the search to the current line. */
4505 if (STRINGP (it->string))
4506 limit = make_number (SCHARS (it->string));
4507 else
4508 limit = make_number (find_next_newline_no_quit (pos, 1));
4509
4510 next = (Fnext_single_property_change
4511 (make_number (pos), Qauto_composed, it->string, limit));
4512 if (XINT (next) < XINT (limit))
4513 {
4514 /* The current point is auto-composed, but there exist
4515 characters not yet composed beyond the auto-composed
4516 region. There's a possiblity that the last
4517 characters in the region may be newly composed. */
4518 int charpos = XINT (next) - 1, bytepos, c;
4519
4520 if (STRINGP (it->string))
4521 {
4522 bytepos = string_char_to_byte (it->string, charpos);
4523 c = SDATA (it->string)[bytepos];
4524 }
4525 else
4526 {
4527 bytepos = CHAR_TO_BYTE (charpos);
4528 c = FETCH_BYTE (bytepos);
4529 }
4530 if (c != '\n')
4531 /* If the last character is not newline, it may be
4532 composed with the following characters. */
4533 val = Qnil, pos = charpos + 1;
4534 }
4535 }
4536 if (NILP (val))
4537 {
4538 int count = SPECPDL_INDEX ();
4539 Lisp_Object args[4];
4540
4541 args[0] = Vauto_composition_function;
4542 specbind (Qauto_composition_function, Qnil);
4543 args[1] = make_number (pos);
4544 args[2] = it->string;
4545 #ifdef USE_FONT_BACKEND
4546 if (enable_font_backend)
4547 {
4548 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4549 int c;
4550
4551 if (STRINGP (it->string))
4552 {
4553 EMACS_INT pos_byte = IT_STRING_BYTEPOS (*it);
4554 const unsigned char *s = SDATA (it->string) + pos_byte;
4555
4556 if (STRING_MULTIBYTE (it->string))
4557 it->c = STRING_CHAR (s, 0);
4558 else
4559 it->c = *s;
4560 }
4561 else
4562 {
4563 EMACS_INT pos_byte = IT_BYTEPOS (*it);
4564
4565 it->c = FETCH_CHAR (pos_byte);
4566 }
4567 args[3] = font_at (it->c, this_pos, face, it->w, it->string);
4568 }
4569 else
4570 #endif /* USE_FONT_BACKEND */
4571 args[3] = Qnil;
4572 safe_call (4, args);
4573 unbind_to (count, Qnil);
4574
4575 if (this_pos == pos)
4576 {
4577 val = Fget_char_property (args[1], Qauto_composed, it->string);
4578 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4579 something. This avoids an endless loop if they failed to
4580 fontify the text for which reason ever. */
4581 if (! NILP (val))
4582 handled = HANDLED_RECOMPUTE_PROPS;
4583 }
4584 else
4585 handled = HANDLED_RECOMPUTE_PROPS;
4586 }
4587 }
4588
4589 return handled;
4590 }
4591
4592 /* Set up iterator IT from `composition' property at its current
4593 position. Called from handle_stop. */
4594
4595 static enum prop_handled
4596 handle_composition_prop (it)
4597 struct it *it;
4598 {
4599 Lisp_Object prop, string;
4600 EMACS_INT pos, pos_byte, start, end;
4601 enum prop_handled handled = HANDLED_NORMALLY;
4602
4603 if (STRINGP (it->string))
4604 {
4605 pos = IT_STRING_CHARPOS (*it);
4606 pos_byte = IT_STRING_BYTEPOS (*it);
4607 string = it->string;
4608 }
4609 else
4610 {
4611 pos = IT_CHARPOS (*it);
4612 pos_byte = IT_BYTEPOS (*it);
4613 string = Qnil;
4614 }
4615
4616 /* If there's a valid composition and point is not inside of the
4617 composition (in the case that the composition is from the current
4618 buffer), draw a glyph composed from the composition components. */
4619 if (find_composition (pos, -1, &start, &end, &prop, string)
4620 && COMPOSITION_VALID_P (start, end, prop)
4621 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4622 {
4623 int id;
4624
4625 if (start != pos)
4626 {
4627 if (STRINGP (it->string))
4628 pos_byte = string_char_to_byte (it->string, start);
4629 else
4630 pos_byte = CHAR_TO_BYTE (start);
4631 }
4632 id = get_composition_id (start, pos_byte, end - start, prop, string);
4633
4634 if (id >= 0)
4635 {
4636 struct composition *cmp = composition_table[id];
4637
4638 if (cmp->glyph_len == 0)
4639 {
4640 /* No glyph. */
4641 if (STRINGP (it->string))
4642 {
4643 IT_STRING_CHARPOS (*it) = end;
4644 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4645 end);
4646 }
4647 else
4648 {
4649 IT_CHARPOS (*it) = end;
4650 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4651 }
4652 return HANDLED_RECOMPUTE_PROPS;
4653 }
4654
4655 it->stop_charpos = end;
4656 push_it (it);
4657
4658 it->method = GET_FROM_COMPOSITION;
4659 it->cmp_id = id;
4660 it->cmp_len = COMPOSITION_LENGTH (prop);
4661 /* For a terminal, draw only the first (non-TAB) character
4662 of the components. */
4663 #ifdef USE_FONT_BACKEND
4664 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4665 {
4666 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4667 ->key_and_value,
4668 cmp->hash_index * 2);
4669
4670 it->c = XINT (LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, 0)));
4671 }
4672 else
4673 #endif /* USE_FONT_BACKEND */
4674 {
4675 int i;
4676
4677 for (i = 0; i < cmp->glyph_len; i++)
4678 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4679 != '\t')
4680 break;
4681 }
4682 if (it->c == '\t')
4683 it->c = ' ';
4684 it->len = (STRINGP (it->string)
4685 ? string_char_to_byte (it->string, end)
4686 : CHAR_TO_BYTE (end)) - pos_byte;
4687 handled = HANDLED_RETURN;
4688 }
4689 }
4690
4691 return handled;
4692 }
4693
4694
4695 \f
4696 /***********************************************************************
4697 Overlay strings
4698 ***********************************************************************/
4699
4700 /* The following structure is used to record overlay strings for
4701 later sorting in load_overlay_strings. */
4702
4703 struct overlay_entry
4704 {
4705 Lisp_Object overlay;
4706 Lisp_Object string;
4707 int priority;
4708 int after_string_p;
4709 };
4710
4711
4712 /* Set up iterator IT from overlay strings at its current position.
4713 Called from handle_stop. */
4714
4715 static enum prop_handled
4716 handle_overlay_change (it)
4717 struct it *it;
4718 {
4719 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4720 return HANDLED_RECOMPUTE_PROPS;
4721 else
4722 return HANDLED_NORMALLY;
4723 }
4724
4725
4726 /* Set up the next overlay string for delivery by IT, if there is an
4727 overlay string to deliver. Called by set_iterator_to_next when the
4728 end of the current overlay string is reached. If there are more
4729 overlay strings to display, IT->string and
4730 IT->current.overlay_string_index are set appropriately here.
4731 Otherwise IT->string is set to nil. */
4732
4733 static void
4734 next_overlay_string (it)
4735 struct it *it;
4736 {
4737 ++it->current.overlay_string_index;
4738 if (it->current.overlay_string_index == it->n_overlay_strings)
4739 {
4740 /* No more overlay strings. Restore IT's settings to what
4741 they were before overlay strings were processed, and
4742 continue to deliver from current_buffer. */
4743 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4744
4745 pop_it (it);
4746 xassert (it->sp > 0
4747 || it->method == GET_FROM_COMPOSITION
4748 || (NILP (it->string)
4749 && it->method == GET_FROM_BUFFER
4750 && it->stop_charpos >= BEGV
4751 && it->stop_charpos <= it->end_charpos));
4752 it->current.overlay_string_index = -1;
4753 it->n_overlay_strings = 0;
4754
4755 /* If we're at the end of the buffer, record that we have
4756 processed the overlay strings there already, so that
4757 next_element_from_buffer doesn't try it again. */
4758 if (IT_CHARPOS (*it) >= it->end_charpos)
4759 it->overlay_strings_at_end_processed_p = 1;
4760
4761 /* If we have to display `...' for invisible text, set
4762 the iterator up for that. */
4763 if (display_ellipsis_p)
4764 setup_for_ellipsis (it, 0);
4765 }
4766 else
4767 {
4768 /* There are more overlay strings to process. If
4769 IT->current.overlay_string_index has advanced to a position
4770 where we must load IT->overlay_strings with more strings, do
4771 it. */
4772 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4773
4774 if (it->current.overlay_string_index && i == 0)
4775 load_overlay_strings (it, 0);
4776
4777 /* Initialize IT to deliver display elements from the overlay
4778 string. */
4779 it->string = it->overlay_strings[i];
4780 it->multibyte_p = STRING_MULTIBYTE (it->string);
4781 SET_TEXT_POS (it->current.string_pos, 0, 0);
4782 it->method = GET_FROM_STRING;
4783 it->stop_charpos = 0;
4784 }
4785
4786 CHECK_IT (it);
4787 }
4788
4789
4790 /* Compare two overlay_entry structures E1 and E2. Used as a
4791 comparison function for qsort in load_overlay_strings. Overlay
4792 strings for the same position are sorted so that
4793
4794 1. All after-strings come in front of before-strings, except
4795 when they come from the same overlay.
4796
4797 2. Within after-strings, strings are sorted so that overlay strings
4798 from overlays with higher priorities come first.
4799
4800 2. Within before-strings, strings are sorted so that overlay
4801 strings from overlays with higher priorities come last.
4802
4803 Value is analogous to strcmp. */
4804
4805
4806 static int
4807 compare_overlay_entries (e1, e2)
4808 void *e1, *e2;
4809 {
4810 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4811 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4812 int result;
4813
4814 if (entry1->after_string_p != entry2->after_string_p)
4815 {
4816 /* Let after-strings appear in front of before-strings if
4817 they come from different overlays. */
4818 if (EQ (entry1->overlay, entry2->overlay))
4819 result = entry1->after_string_p ? 1 : -1;
4820 else
4821 result = entry1->after_string_p ? -1 : 1;
4822 }
4823 else if (entry1->after_string_p)
4824 /* After-strings sorted in order of decreasing priority. */
4825 result = entry2->priority - entry1->priority;
4826 else
4827 /* Before-strings sorted in order of increasing priority. */
4828 result = entry1->priority - entry2->priority;
4829
4830 return result;
4831 }
4832
4833
4834 /* Load the vector IT->overlay_strings with overlay strings from IT's
4835 current buffer position, or from CHARPOS if that is > 0. Set
4836 IT->n_overlays to the total number of overlay strings found.
4837
4838 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4839 a time. On entry into load_overlay_strings,
4840 IT->current.overlay_string_index gives the number of overlay
4841 strings that have already been loaded by previous calls to this
4842 function.
4843
4844 IT->add_overlay_start contains an additional overlay start
4845 position to consider for taking overlay strings from, if non-zero.
4846 This position comes into play when the overlay has an `invisible'
4847 property, and both before and after-strings. When we've skipped to
4848 the end of the overlay, because of its `invisible' property, we
4849 nevertheless want its before-string to appear.
4850 IT->add_overlay_start will contain the overlay start position
4851 in this case.
4852
4853 Overlay strings are sorted so that after-string strings come in
4854 front of before-string strings. Within before and after-strings,
4855 strings are sorted by overlay priority. See also function
4856 compare_overlay_entries. */
4857
4858 static void
4859 load_overlay_strings (it, charpos)
4860 struct it *it;
4861 int charpos;
4862 {
4863 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4864 Lisp_Object overlay, window, str, invisible;
4865 struct Lisp_Overlay *ov;
4866 int start, end;
4867 int size = 20;
4868 int n = 0, i, j, invis_p;
4869 struct overlay_entry *entries
4870 = (struct overlay_entry *) alloca (size * sizeof *entries);
4871
4872 if (charpos <= 0)
4873 charpos = IT_CHARPOS (*it);
4874
4875 /* Append the overlay string STRING of overlay OVERLAY to vector
4876 `entries' which has size `size' and currently contains `n'
4877 elements. AFTER_P non-zero means STRING is an after-string of
4878 OVERLAY. */
4879 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4880 do \
4881 { \
4882 Lisp_Object priority; \
4883 \
4884 if (n == size) \
4885 { \
4886 int new_size = 2 * size; \
4887 struct overlay_entry *old = entries; \
4888 entries = \
4889 (struct overlay_entry *) alloca (new_size \
4890 * sizeof *entries); \
4891 bcopy (old, entries, size * sizeof *entries); \
4892 size = new_size; \
4893 } \
4894 \
4895 entries[n].string = (STRING); \
4896 entries[n].overlay = (OVERLAY); \
4897 priority = Foverlay_get ((OVERLAY), Qpriority); \
4898 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4899 entries[n].after_string_p = (AFTER_P); \
4900 ++n; \
4901 } \
4902 while (0)
4903
4904 /* Process overlay before the overlay center. */
4905 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4906 {
4907 XSETMISC (overlay, ov);
4908 xassert (OVERLAYP (overlay));
4909 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4910 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4911
4912 if (end < charpos)
4913 break;
4914
4915 /* Skip this overlay if it doesn't start or end at IT's current
4916 position. */
4917 if (end != charpos && start != charpos)
4918 continue;
4919
4920 /* Skip this overlay if it doesn't apply to IT->w. */
4921 window = Foverlay_get (overlay, Qwindow);
4922 if (WINDOWP (window) && XWINDOW (window) != it->w)
4923 continue;
4924
4925 /* If the text ``under'' the overlay is invisible, both before-
4926 and after-strings from this overlay are visible; start and
4927 end position are indistinguishable. */
4928 invisible = Foverlay_get (overlay, Qinvisible);
4929 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4930
4931 /* If overlay has a non-empty before-string, record it. */
4932 if ((start == charpos || (end == charpos && invis_p))
4933 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4934 && SCHARS (str))
4935 RECORD_OVERLAY_STRING (overlay, str, 0);
4936
4937 /* If overlay has a non-empty after-string, record it. */
4938 if ((end == charpos || (start == charpos && invis_p))
4939 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4940 && SCHARS (str))
4941 RECORD_OVERLAY_STRING (overlay, str, 1);
4942 }
4943
4944 /* Process overlays after the overlay center. */
4945 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4946 {
4947 XSETMISC (overlay, ov);
4948 xassert (OVERLAYP (overlay));
4949 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4950 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4951
4952 if (start > charpos)
4953 break;
4954
4955 /* Skip this overlay if it doesn't start or end at IT's current
4956 position. */
4957 if (end != charpos && start != charpos)
4958 continue;
4959
4960 /* Skip this overlay if it doesn't apply to IT->w. */
4961 window = Foverlay_get (overlay, Qwindow);
4962 if (WINDOWP (window) && XWINDOW (window) != it->w)
4963 continue;
4964
4965 /* If the text ``under'' the overlay is invisible, it has a zero
4966 dimension, and both before- and after-strings apply. */
4967 invisible = Foverlay_get (overlay, Qinvisible);
4968 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4969
4970 /* If overlay has a non-empty before-string, record it. */
4971 if ((start == charpos || (end == charpos && invis_p))
4972 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4973 && SCHARS (str))
4974 RECORD_OVERLAY_STRING (overlay, str, 0);
4975
4976 /* If overlay has a non-empty after-string, record it. */
4977 if ((end == charpos || (start == charpos && invis_p))
4978 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4979 && SCHARS (str))
4980 RECORD_OVERLAY_STRING (overlay, str, 1);
4981 }
4982
4983 #undef RECORD_OVERLAY_STRING
4984
4985 /* Sort entries. */
4986 if (n > 1)
4987 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4988
4989 /* Record the total number of strings to process. */
4990 it->n_overlay_strings = n;
4991
4992 /* IT->current.overlay_string_index is the number of overlay strings
4993 that have already been consumed by IT. Copy some of the
4994 remaining overlay strings to IT->overlay_strings. */
4995 i = 0;
4996 j = it->current.overlay_string_index;
4997 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4998 it->overlay_strings[i++] = entries[j++].string;
4999
5000 CHECK_IT (it);
5001 }
5002
5003
5004 /* Get the first chunk of overlay strings at IT's current buffer
5005 position, or at CHARPOS if that is > 0. Value is non-zero if at
5006 least one overlay string was found. */
5007
5008 static int
5009 get_overlay_strings_1 (it, charpos, compute_stop_p)
5010 struct it *it;
5011 int charpos;
5012 {
5013 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5014 process. This fills IT->overlay_strings with strings, and sets
5015 IT->n_overlay_strings to the total number of strings to process.
5016 IT->pos.overlay_string_index has to be set temporarily to zero
5017 because load_overlay_strings needs this; it must be set to -1
5018 when no overlay strings are found because a zero value would
5019 indicate a position in the first overlay string. */
5020 it->current.overlay_string_index = 0;
5021 load_overlay_strings (it, charpos);
5022
5023 /* If we found overlay strings, set up IT to deliver display
5024 elements from the first one. Otherwise set up IT to deliver
5025 from current_buffer. */
5026 if (it->n_overlay_strings)
5027 {
5028 /* Make sure we know settings in current_buffer, so that we can
5029 restore meaningful values when we're done with the overlay
5030 strings. */
5031 if (compute_stop_p)
5032 compute_stop_pos (it);
5033 xassert (it->face_id >= 0);
5034
5035 /* Save IT's settings. They are restored after all overlay
5036 strings have been processed. */
5037 xassert (!compute_stop_p || it->sp == 0);
5038 push_it (it);
5039
5040 /* Set up IT to deliver display elements from the first overlay
5041 string. */
5042 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5043 it->string = it->overlay_strings[0];
5044 it->stop_charpos = 0;
5045 xassert (STRINGP (it->string));
5046 it->end_charpos = SCHARS (it->string);
5047 it->multibyte_p = STRING_MULTIBYTE (it->string);
5048 it->method = GET_FROM_STRING;
5049 return 1;
5050 }
5051
5052 it->current.overlay_string_index = -1;
5053 return 0;
5054 }
5055
5056 static int
5057 get_overlay_strings (it, charpos)
5058 struct it *it;
5059 int charpos;
5060 {
5061 it->string = Qnil;
5062 it->method = GET_FROM_BUFFER;
5063
5064 (void) get_overlay_strings_1 (it, charpos, 1);
5065
5066 CHECK_IT (it);
5067
5068 /* Value is non-zero if we found at least one overlay string. */
5069 return STRINGP (it->string);
5070 }
5071
5072
5073 \f
5074 /***********************************************************************
5075 Saving and restoring state
5076 ***********************************************************************/
5077
5078 /* Save current settings of IT on IT->stack. Called, for example,
5079 before setting up IT for an overlay string, to be able to restore
5080 IT's settings to what they were after the overlay string has been
5081 processed. */
5082
5083 static void
5084 push_it (it)
5085 struct it *it;
5086 {
5087 struct iterator_stack_entry *p;
5088
5089 xassert (it->sp < IT_STACK_SIZE);
5090 p = it->stack + it->sp;
5091
5092 p->stop_charpos = it->stop_charpos;
5093 xassert (it->face_id >= 0);
5094 p->face_id = it->face_id;
5095 p->string = it->string;
5096 p->method = it->method;
5097 switch (p->method)
5098 {
5099 case GET_FROM_IMAGE:
5100 p->u.image.object = it->object;
5101 p->u.image.image_id = it->image_id;
5102 p->u.image.slice = it->slice;
5103 break;
5104 case GET_FROM_COMPOSITION:
5105 p->u.comp.object = it->object;
5106 p->u.comp.c = it->c;
5107 p->u.comp.len = it->len;
5108 p->u.comp.cmp_id = it->cmp_id;
5109 p->u.comp.cmp_len = it->cmp_len;
5110 break;
5111 case GET_FROM_STRETCH:
5112 p->u.stretch.object = it->object;
5113 break;
5114 }
5115 p->position = it->position;
5116 p->current = it->current;
5117 p->end_charpos = it->end_charpos;
5118 p->string_nchars = it->string_nchars;
5119 p->area = it->area;
5120 p->multibyte_p = it->multibyte_p;
5121 p->space_width = it->space_width;
5122 p->font_height = it->font_height;
5123 p->voffset = it->voffset;
5124 p->string_from_display_prop_p = it->string_from_display_prop_p;
5125 p->display_ellipsis_p = 0;
5126 ++it->sp;
5127 }
5128
5129
5130 /* Restore IT's settings from IT->stack. Called, for example, when no
5131 more overlay strings must be processed, and we return to delivering
5132 display elements from a buffer, or when the end of a string from a
5133 `display' property is reached and we return to delivering display
5134 elements from an overlay string, or from a buffer. */
5135
5136 static void
5137 pop_it (it)
5138 struct it *it;
5139 {
5140 struct iterator_stack_entry *p;
5141
5142 xassert (it->sp > 0);
5143 --it->sp;
5144 p = it->stack + it->sp;
5145 it->stop_charpos = p->stop_charpos;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 if (NILP (it->string))
5151 SET_TEXT_POS (it->current.string_pos, -1, -1);
5152 it->method = p->method;
5153 switch (it->method)
5154 {
5155 case GET_FROM_IMAGE:
5156 it->image_id = p->u.image.image_id;
5157 it->object = p->u.image.object;
5158 it->slice = p->u.image.slice;
5159 break;
5160 case GET_FROM_COMPOSITION:
5161 it->object = p->u.comp.object;
5162 it->c = p->u.comp.c;
5163 it->len = p->u.comp.len;
5164 it->cmp_id = p->u.comp.cmp_id;
5165 it->cmp_len = p->u.comp.cmp_len;
5166 break;
5167 case GET_FROM_STRETCH:
5168 it->object = p->u.comp.object;
5169 break;
5170 case GET_FROM_BUFFER:
5171 it->object = it->w->buffer;
5172 break;
5173 case GET_FROM_STRING:
5174 it->object = it->string;
5175 break;
5176 }
5177 it->end_charpos = p->end_charpos;
5178 it->string_nchars = p->string_nchars;
5179 it->area = p->area;
5180 it->multibyte_p = p->multibyte_p;
5181 it->space_width = p->space_width;
5182 it->font_height = p->font_height;
5183 it->voffset = p->voffset;
5184 it->string_from_display_prop_p = p->string_from_display_prop_p;
5185 }
5186
5187
5188 \f
5189 /***********************************************************************
5190 Moving over lines
5191 ***********************************************************************/
5192
5193 /* Set IT's current position to the previous line start. */
5194
5195 static void
5196 back_to_previous_line_start (it)
5197 struct it *it;
5198 {
5199 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5200 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5201 }
5202
5203
5204 /* Move IT to the next line start.
5205
5206 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5207 we skipped over part of the text (as opposed to moving the iterator
5208 continuously over the text). Otherwise, don't change the value
5209 of *SKIPPED_P.
5210
5211 Newlines may come from buffer text, overlay strings, or strings
5212 displayed via the `display' property. That's the reason we can't
5213 simply use find_next_newline_no_quit.
5214
5215 Note that this function may not skip over invisible text that is so
5216 because of text properties and immediately follows a newline. If
5217 it would, function reseat_at_next_visible_line_start, when called
5218 from set_iterator_to_next, would effectively make invisible
5219 characters following a newline part of the wrong glyph row, which
5220 leads to wrong cursor motion. */
5221
5222 static int
5223 forward_to_next_line_start (it, skipped_p)
5224 struct it *it;
5225 int *skipped_p;
5226 {
5227 int old_selective, newline_found_p, n;
5228 const int MAX_NEWLINE_DISTANCE = 500;
5229
5230 /* If already on a newline, just consume it to avoid unintended
5231 skipping over invisible text below. */
5232 if (it->what == IT_CHARACTER
5233 && it->c == '\n'
5234 && CHARPOS (it->position) == IT_CHARPOS (*it))
5235 {
5236 set_iterator_to_next (it, 0);
5237 it->c = 0;
5238 return 1;
5239 }
5240
5241 /* Don't handle selective display in the following. It's (a)
5242 unnecessary because it's done by the caller, and (b) leads to an
5243 infinite recursion because next_element_from_ellipsis indirectly
5244 calls this function. */
5245 old_selective = it->selective;
5246 it->selective = 0;
5247
5248 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5249 from buffer text. */
5250 for (n = newline_found_p = 0;
5251 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5252 n += STRINGP (it->string) ? 0 : 1)
5253 {
5254 if (!get_next_display_element (it))
5255 return 0;
5256 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5257 set_iterator_to_next (it, 0);
5258 }
5259
5260 /* If we didn't find a newline near enough, see if we can use a
5261 short-cut. */
5262 if (!newline_found_p)
5263 {
5264 int start = IT_CHARPOS (*it);
5265 int limit = find_next_newline_no_quit (start, 1);
5266 Lisp_Object pos;
5267
5268 xassert (!STRINGP (it->string));
5269
5270 /* If there isn't any `display' property in sight, and no
5271 overlays, we can just use the position of the newline in
5272 buffer text. */
5273 if (it->stop_charpos >= limit
5274 || ((pos = Fnext_single_property_change (make_number (start),
5275 Qdisplay,
5276 Qnil, make_number (limit)),
5277 NILP (pos))
5278 && next_overlay_change (start) == ZV))
5279 {
5280 IT_CHARPOS (*it) = limit;
5281 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5282 *skipped_p = newline_found_p = 1;
5283 }
5284 else
5285 {
5286 while (get_next_display_element (it)
5287 && !newline_found_p)
5288 {
5289 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5290 set_iterator_to_next (it, 0);
5291 }
5292 }
5293 }
5294
5295 it->selective = old_selective;
5296 return newline_found_p;
5297 }
5298
5299
5300 /* Set IT's current position to the previous visible line start. Skip
5301 invisible text that is so either due to text properties or due to
5302 selective display. Caution: this does not change IT->current_x and
5303 IT->hpos. */
5304
5305 static void
5306 back_to_previous_visible_line_start (it)
5307 struct it *it;
5308 {
5309 while (IT_CHARPOS (*it) > BEGV)
5310 {
5311 back_to_previous_line_start (it);
5312
5313 if (IT_CHARPOS (*it) <= BEGV)
5314 break;
5315
5316 /* If selective > 0, then lines indented more than that values
5317 are invisible. */
5318 if (it->selective > 0
5319 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5320 (double) it->selective)) /* iftc */
5321 continue;
5322
5323 /* Check the newline before point for invisibility. */
5324 {
5325 Lisp_Object prop;
5326 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5327 Qinvisible, it->window);
5328 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5329 continue;
5330 }
5331
5332 if (IT_CHARPOS (*it) <= BEGV)
5333 break;
5334
5335 {
5336 struct it it2;
5337 int pos;
5338 int beg, end;
5339 Lisp_Object val, overlay;
5340
5341 /* If newline is part of a composition, continue from start of composition */
5342 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5343 && beg < IT_CHARPOS (*it))
5344 goto replaced;
5345
5346 /* If newline is replaced by a display property, find start of overlay
5347 or interval and continue search from that point. */
5348 it2 = *it;
5349 pos = --IT_CHARPOS (it2);
5350 --IT_BYTEPOS (it2);
5351 it2.sp = 0;
5352 if (handle_display_prop (&it2) == HANDLED_RETURN
5353 && !NILP (val = get_char_property_and_overlay
5354 (make_number (pos), Qdisplay, Qnil, &overlay))
5355 && (OVERLAYP (overlay)
5356 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5357 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5358 goto replaced;
5359
5360 /* Newline is not replaced by anything -- so we are done. */
5361 break;
5362
5363 replaced:
5364 if (beg < BEGV)
5365 beg = BEGV;
5366 IT_CHARPOS (*it) = beg;
5367 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5368 }
5369 }
5370
5371 it->continuation_lines_width = 0;
5372
5373 xassert (IT_CHARPOS (*it) >= BEGV);
5374 xassert (IT_CHARPOS (*it) == BEGV
5375 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5376 CHECK_IT (it);
5377 }
5378
5379
5380 /* Reseat iterator IT at the previous visible line start. Skip
5381 invisible text that is so either due to text properties or due to
5382 selective display. At the end, update IT's overlay information,
5383 face information etc. */
5384
5385 void
5386 reseat_at_previous_visible_line_start (it)
5387 struct it *it;
5388 {
5389 back_to_previous_visible_line_start (it);
5390 reseat (it, it->current.pos, 1);
5391 CHECK_IT (it);
5392 }
5393
5394
5395 /* Reseat iterator IT on the next visible line start in the current
5396 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5397 preceding the line start. Skip over invisible text that is so
5398 because of selective display. Compute faces, overlays etc at the
5399 new position. Note that this function does not skip over text that
5400 is invisible because of text properties. */
5401
5402 static void
5403 reseat_at_next_visible_line_start (it, on_newline_p)
5404 struct it *it;
5405 int on_newline_p;
5406 {
5407 int newline_found_p, skipped_p = 0;
5408
5409 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5410
5411 /* Skip over lines that are invisible because they are indented
5412 more than the value of IT->selective. */
5413 if (it->selective > 0)
5414 while (IT_CHARPOS (*it) < ZV
5415 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5416 (double) it->selective)) /* iftc */
5417 {
5418 xassert (IT_BYTEPOS (*it) == BEGV
5419 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5420 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5421 }
5422
5423 /* Position on the newline if that's what's requested. */
5424 if (on_newline_p && newline_found_p)
5425 {
5426 if (STRINGP (it->string))
5427 {
5428 if (IT_STRING_CHARPOS (*it) > 0)
5429 {
5430 --IT_STRING_CHARPOS (*it);
5431 --IT_STRING_BYTEPOS (*it);
5432 }
5433 }
5434 else if (IT_CHARPOS (*it) > BEGV)
5435 {
5436 --IT_CHARPOS (*it);
5437 --IT_BYTEPOS (*it);
5438 reseat (it, it->current.pos, 0);
5439 }
5440 }
5441 else if (skipped_p)
5442 reseat (it, it->current.pos, 0);
5443
5444 CHECK_IT (it);
5445 }
5446
5447
5448 \f
5449 /***********************************************************************
5450 Changing an iterator's position
5451 ***********************************************************************/
5452
5453 /* Change IT's current position to POS in current_buffer. If FORCE_P
5454 is non-zero, always check for text properties at the new position.
5455 Otherwise, text properties are only looked up if POS >=
5456 IT->check_charpos of a property. */
5457
5458 static void
5459 reseat (it, pos, force_p)
5460 struct it *it;
5461 struct text_pos pos;
5462 int force_p;
5463 {
5464 int original_pos = IT_CHARPOS (*it);
5465
5466 reseat_1 (it, pos, 0);
5467
5468 /* Determine where to check text properties. Avoid doing it
5469 where possible because text property lookup is very expensive. */
5470 if (force_p
5471 || CHARPOS (pos) > it->stop_charpos
5472 || CHARPOS (pos) < original_pos)
5473 handle_stop (it);
5474
5475 CHECK_IT (it);
5476 }
5477
5478
5479 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5480 IT->stop_pos to POS, also. */
5481
5482 static void
5483 reseat_1 (it, pos, set_stop_p)
5484 struct it *it;
5485 struct text_pos pos;
5486 int set_stop_p;
5487 {
5488 /* Don't call this function when scanning a C string. */
5489 xassert (it->s == NULL);
5490
5491 /* POS must be a reasonable value. */
5492 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5493
5494 it->current.pos = it->position = pos;
5495 it->end_charpos = ZV;
5496 it->dpvec = NULL;
5497 it->current.dpvec_index = -1;
5498 it->current.overlay_string_index = -1;
5499 IT_STRING_CHARPOS (*it) = -1;
5500 IT_STRING_BYTEPOS (*it) = -1;
5501 it->string = Qnil;
5502 it->method = GET_FROM_BUFFER;
5503 it->object = it->w->buffer;
5504 it->area = TEXT_AREA;
5505 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5506 it->sp = 0;
5507 it->string_from_display_prop_p = 0;
5508 it->face_before_selective_p = 0;
5509
5510 if (set_stop_p)
5511 it->stop_charpos = CHARPOS (pos);
5512 }
5513
5514
5515 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5516 If S is non-null, it is a C string to iterate over. Otherwise,
5517 STRING gives a Lisp string to iterate over.
5518
5519 If PRECISION > 0, don't return more then PRECISION number of
5520 characters from the string.
5521
5522 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5523 characters have been returned. FIELD_WIDTH < 0 means an infinite
5524 field width.
5525
5526 MULTIBYTE = 0 means disable processing of multibyte characters,
5527 MULTIBYTE > 0 means enable it,
5528 MULTIBYTE < 0 means use IT->multibyte_p.
5529
5530 IT must be initialized via a prior call to init_iterator before
5531 calling this function. */
5532
5533 static void
5534 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5535 struct it *it;
5536 unsigned char *s;
5537 Lisp_Object string;
5538 int charpos;
5539 int precision, field_width, multibyte;
5540 {
5541 /* No region in strings. */
5542 it->region_beg_charpos = it->region_end_charpos = -1;
5543
5544 /* No text property checks performed by default, but see below. */
5545 it->stop_charpos = -1;
5546
5547 /* Set iterator position and end position. */
5548 bzero (&it->current, sizeof it->current);
5549 it->current.overlay_string_index = -1;
5550 it->current.dpvec_index = -1;
5551 xassert (charpos >= 0);
5552
5553 /* If STRING is specified, use its multibyteness, otherwise use the
5554 setting of MULTIBYTE, if specified. */
5555 if (multibyte >= 0)
5556 it->multibyte_p = multibyte > 0;
5557
5558 if (s == NULL)
5559 {
5560 xassert (STRINGP (string));
5561 it->string = string;
5562 it->s = NULL;
5563 it->end_charpos = it->string_nchars = SCHARS (string);
5564 it->method = GET_FROM_STRING;
5565 it->current.string_pos = string_pos (charpos, string);
5566 }
5567 else
5568 {
5569 it->s = s;
5570 it->string = Qnil;
5571
5572 /* Note that we use IT->current.pos, not it->current.string_pos,
5573 for displaying C strings. */
5574 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5575 if (it->multibyte_p)
5576 {
5577 it->current.pos = c_string_pos (charpos, s, 1);
5578 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5579 }
5580 else
5581 {
5582 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5583 it->end_charpos = it->string_nchars = strlen (s);
5584 }
5585
5586 it->method = GET_FROM_C_STRING;
5587 }
5588
5589 /* PRECISION > 0 means don't return more than PRECISION characters
5590 from the string. */
5591 if (precision > 0 && it->end_charpos - charpos > precision)
5592 it->end_charpos = it->string_nchars = charpos + precision;
5593
5594 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5595 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5596 FIELD_WIDTH < 0 means infinite field width. This is useful for
5597 padding with `-' at the end of a mode line. */
5598 if (field_width < 0)
5599 field_width = INFINITY;
5600 if (field_width > it->end_charpos - charpos)
5601 it->end_charpos = charpos + field_width;
5602
5603 /* Use the standard display table for displaying strings. */
5604 if (DISP_TABLE_P (Vstandard_display_table))
5605 it->dp = XCHAR_TABLE (Vstandard_display_table);
5606
5607 it->stop_charpos = charpos;
5608 CHECK_IT (it);
5609 }
5610
5611
5612 \f
5613 /***********************************************************************
5614 Iteration
5615 ***********************************************************************/
5616
5617 /* Map enum it_method value to corresponding next_element_from_* function. */
5618
5619 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5620 {
5621 next_element_from_buffer,
5622 next_element_from_display_vector,
5623 next_element_from_composition,
5624 next_element_from_string,
5625 next_element_from_c_string,
5626 next_element_from_image,
5627 next_element_from_stretch
5628 };
5629
5630
5631 /* Load IT's display element fields with information about the next
5632 display element from the current position of IT. Value is zero if
5633 end of buffer (or C string) is reached. */
5634
5635 static struct frame *last_escape_glyph_frame = NULL;
5636 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5637 static int last_escape_glyph_merged_face_id = 0;
5638
5639 int
5640 get_next_display_element (it)
5641 struct it *it;
5642 {
5643 /* Non-zero means that we found a display element. Zero means that
5644 we hit the end of what we iterate over. Performance note: the
5645 function pointer `method' used here turns out to be faster than
5646 using a sequence of if-statements. */
5647 int success_p;
5648
5649 get_next:
5650 success_p = (*get_next_element[it->method]) (it);
5651
5652 if (it->what == IT_CHARACTER)
5653 {
5654 /* Map via display table or translate control characters.
5655 IT->c, IT->len etc. have been set to the next character by
5656 the function call above. If we have a display table, and it
5657 contains an entry for IT->c, translate it. Don't do this if
5658 IT->c itself comes from a display table, otherwise we could
5659 end up in an infinite recursion. (An alternative could be to
5660 count the recursion depth of this function and signal an
5661 error when a certain maximum depth is reached.) Is it worth
5662 it? */
5663 if (success_p && it->dpvec == NULL)
5664 {
5665 Lisp_Object dv;
5666
5667 if (it->dp
5668 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5669 VECTORP (dv)))
5670 {
5671 struct Lisp_Vector *v = XVECTOR (dv);
5672
5673 /* Return the first character from the display table
5674 entry, if not empty. If empty, don't display the
5675 current character. */
5676 if (v->size)
5677 {
5678 it->dpvec_char_len = it->len;
5679 it->dpvec = v->contents;
5680 it->dpend = v->contents + v->size;
5681 it->current.dpvec_index = 0;
5682 it->dpvec_face_id = -1;
5683 it->saved_face_id = it->face_id;
5684 it->method = GET_FROM_DISPLAY_VECTOR;
5685 it->ellipsis_p = 0;
5686 }
5687 else
5688 {
5689 set_iterator_to_next (it, 0);
5690 }
5691 goto get_next;
5692 }
5693
5694 /* Translate control characters into `\003' or `^C' form.
5695 Control characters coming from a display table entry are
5696 currently not translated because we use IT->dpvec to hold
5697 the translation. This could easily be changed but I
5698 don't believe that it is worth doing.
5699
5700 If it->multibyte_p is nonzero, non-printable non-ASCII
5701 characters are also translated to octal form.
5702
5703 If it->multibyte_p is zero, eight-bit characters that
5704 don't have corresponding multibyte char code are also
5705 translated to octal form. */
5706 else if ((it->c < ' '
5707 ? (it->area != TEXT_AREA
5708 /* In mode line, treat \n, \t like other crl chars. */
5709 || (it->c != '\t'
5710 && it->glyph_row && it->glyph_row->mode_line_p)
5711 || (it->c != '\n' && it->c != '\t'))
5712 : (it->multibyte_p
5713 ? (!CHAR_PRINTABLE_P (it->c)
5714 || (!NILP (Vnobreak_char_display)
5715 && (it->c == 0xA0 /* NO-BREAK SPACE */
5716 || it->c == 0xAD /* SOFT HYPHEN */)))
5717 : (it->c >= 127
5718 && (! unibyte_display_via_language_environment
5719 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5720 {
5721 /* IT->c is a control character which must be displayed
5722 either as '\003' or as `^C' where the '\\' and '^'
5723 can be defined in the display table. Fill
5724 IT->ctl_chars with glyphs for what we have to
5725 display. Then, set IT->dpvec to these glyphs. */
5726 GLYPH g;
5727 int ctl_len;
5728 int face_id, lface_id = 0 ;
5729 GLYPH escape_glyph;
5730
5731 /* Handle control characters with ^. */
5732
5733 if (it->c < 128 && it->ctl_arrow_p)
5734 {
5735 g = '^'; /* default glyph for Control */
5736 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5737 if (it->dp
5738 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5739 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5740 {
5741 g = XINT (DISP_CTRL_GLYPH (it->dp));
5742 lface_id = FAST_GLYPH_FACE (g);
5743 }
5744 if (lface_id)
5745 {
5746 g = FAST_GLYPH_CHAR (g);
5747 face_id = merge_faces (it->f, Qt, lface_id,
5748 it->face_id);
5749 }
5750 else if (it->f == last_escape_glyph_frame
5751 && it->face_id == last_escape_glyph_face_id)
5752 {
5753 face_id = last_escape_glyph_merged_face_id;
5754 }
5755 else
5756 {
5757 /* Merge the escape-glyph face into the current face. */
5758 face_id = merge_faces (it->f, Qescape_glyph, 0,
5759 it->face_id);
5760 last_escape_glyph_frame = it->f;
5761 last_escape_glyph_face_id = it->face_id;
5762 last_escape_glyph_merged_face_id = face_id;
5763 }
5764
5765 XSETINT (it->ctl_chars[0], g);
5766 g = it->c ^ 0100;
5767 XSETINT (it->ctl_chars[1], g);
5768 ctl_len = 2;
5769 goto display_control;
5770 }
5771
5772 /* Handle non-break space in the mode where it only gets
5773 highlighting. */
5774
5775 if (EQ (Vnobreak_char_display, Qt)
5776 && it->c == 0xA0)
5777 {
5778 /* Merge the no-break-space face into the current face. */
5779 face_id = merge_faces (it->f, Qnobreak_space, 0,
5780 it->face_id);
5781
5782 g = it->c = ' ';
5783 XSETINT (it->ctl_chars[0], g);
5784 ctl_len = 1;
5785 goto display_control;
5786 }
5787
5788 /* Handle sequences that start with the "escape glyph". */
5789
5790 /* the default escape glyph is \. */
5791 escape_glyph = '\\';
5792
5793 if (it->dp
5794 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5795 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5796 {
5797 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5798 lface_id = FAST_GLYPH_FACE (escape_glyph);
5799 }
5800 if (lface_id)
5801 {
5802 /* The display table specified a face.
5803 Merge it into face_id and also into escape_glyph. */
5804 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5805 face_id = merge_faces (it->f, Qt, lface_id,
5806 it->face_id);
5807 }
5808 else if (it->f == last_escape_glyph_frame
5809 && it->face_id == last_escape_glyph_face_id)
5810 {
5811 face_id = last_escape_glyph_merged_face_id;
5812 }
5813 else
5814 {
5815 /* Merge the escape-glyph face into the current face. */
5816 face_id = merge_faces (it->f, Qescape_glyph, 0,
5817 it->face_id);
5818 last_escape_glyph_frame = it->f;
5819 last_escape_glyph_face_id = it->face_id;
5820 last_escape_glyph_merged_face_id = face_id;
5821 }
5822
5823 /* Handle soft hyphens in the mode where they only get
5824 highlighting. */
5825
5826 if (EQ (Vnobreak_char_display, Qt)
5827 && it->c == 0xAD)
5828 {
5829 g = it->c = '-';
5830 XSETINT (it->ctl_chars[0], g);
5831 ctl_len = 1;
5832 goto display_control;
5833 }
5834
5835 /* Handle non-break space and soft hyphen
5836 with the escape glyph. */
5837
5838 if (it->c == 0xA0 || it->c == 0xAD)
5839 {
5840 XSETINT (it->ctl_chars[0], escape_glyph);
5841 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5842 XSETINT (it->ctl_chars[1], g);
5843 ctl_len = 2;
5844 goto display_control;
5845 }
5846
5847 {
5848 unsigned char str[MAX_MULTIBYTE_LENGTH];
5849 int len;
5850 int i;
5851
5852 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5853 if (CHAR_BYTE8_P (it->c))
5854 {
5855 str[0] = CHAR_TO_BYTE8 (it->c);
5856 len = 1;
5857 }
5858 else if (it->c < 256)
5859 {
5860 str[0] = it->c;
5861 len = 1;
5862 }
5863 else
5864 {
5865 /* It's an invalid character, which shouldn't
5866 happen actually, but due to bugs it may
5867 happen. Let's print the char as is, there's
5868 not much meaningful we can do with it. */
5869 str[0] = it->c;
5870 str[1] = it->c >> 8;
5871 str[2] = it->c >> 16;
5872 str[3] = it->c >> 24;
5873 len = 4;
5874 }
5875
5876 for (i = 0; i < len; i++)
5877 {
5878 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5879 /* Insert three more glyphs into IT->ctl_chars for
5880 the octal display of the character. */
5881 g = ((str[i] >> 6) & 7) + '0';
5882 XSETINT (it->ctl_chars[i * 4 + 1], g);
5883 g = ((str[i] >> 3) & 7) + '0';
5884 XSETINT (it->ctl_chars[i * 4 + 2], g);
5885 g = (str[i] & 7) + '0';
5886 XSETINT (it->ctl_chars[i * 4 + 3], g);
5887 }
5888 ctl_len = len * 4;
5889 }
5890
5891 display_control:
5892 /* Set up IT->dpvec and return first character from it. */
5893 it->dpvec_char_len = it->len;
5894 it->dpvec = it->ctl_chars;
5895 it->dpend = it->dpvec + ctl_len;
5896 it->current.dpvec_index = 0;
5897 it->dpvec_face_id = face_id;
5898 it->saved_face_id = it->face_id;
5899 it->method = GET_FROM_DISPLAY_VECTOR;
5900 it->ellipsis_p = 0;
5901 goto get_next;
5902 }
5903 }
5904 }
5905
5906 /* Adjust face id for a multibyte character. There are no multibyte
5907 character in unibyte text. */
5908 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5909 && it->multibyte_p
5910 && success_p
5911 && FRAME_WINDOW_P (it->f))
5912 {
5913 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5914 int pos = (it->s ? -1
5915 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5916 : IT_CHARPOS (*it));
5917
5918 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5919 }
5920
5921 /* Is this character the last one of a run of characters with
5922 box? If yes, set IT->end_of_box_run_p to 1. */
5923 if (it->face_box_p
5924 && it->s == NULL)
5925 {
5926 int face_id;
5927 struct face *face;
5928
5929 it->end_of_box_run_p
5930 = ((face_id = face_after_it_pos (it),
5931 face_id != it->face_id)
5932 && (face = FACE_FROM_ID (it->f, face_id),
5933 face->box == FACE_NO_BOX));
5934 }
5935
5936 /* Value is 0 if end of buffer or string reached. */
5937 return success_p;
5938 }
5939
5940
5941 /* Move IT to the next display element.
5942
5943 RESEAT_P non-zero means if called on a newline in buffer text,
5944 skip to the next visible line start.
5945
5946 Functions get_next_display_element and set_iterator_to_next are
5947 separate because I find this arrangement easier to handle than a
5948 get_next_display_element function that also increments IT's
5949 position. The way it is we can first look at an iterator's current
5950 display element, decide whether it fits on a line, and if it does,
5951 increment the iterator position. The other way around we probably
5952 would either need a flag indicating whether the iterator has to be
5953 incremented the next time, or we would have to implement a
5954 decrement position function which would not be easy to write. */
5955
5956 void
5957 set_iterator_to_next (it, reseat_p)
5958 struct it *it;
5959 int reseat_p;
5960 {
5961 /* Reset flags indicating start and end of a sequence of characters
5962 with box. Reset them at the start of this function because
5963 moving the iterator to a new position might set them. */
5964 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5965
5966 switch (it->method)
5967 {
5968 case GET_FROM_BUFFER:
5969 /* The current display element of IT is a character from
5970 current_buffer. Advance in the buffer, and maybe skip over
5971 invisible lines that are so because of selective display. */
5972 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5973 reseat_at_next_visible_line_start (it, 0);
5974 else
5975 {
5976 xassert (it->len != 0);
5977 IT_BYTEPOS (*it) += it->len;
5978 IT_CHARPOS (*it) += 1;
5979 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5980 }
5981 break;
5982
5983 case GET_FROM_COMPOSITION:
5984 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5985 xassert (it->sp > 0);
5986 pop_it (it);
5987 if (it->method == GET_FROM_STRING)
5988 {
5989 IT_STRING_BYTEPOS (*it) += it->len;
5990 IT_STRING_CHARPOS (*it) += it->cmp_len;
5991 goto consider_string_end;
5992 }
5993 else if (it->method == GET_FROM_BUFFER)
5994 {
5995 IT_BYTEPOS (*it) += it->len;
5996 IT_CHARPOS (*it) += it->cmp_len;
5997 }
5998 break;
5999
6000 case GET_FROM_C_STRING:
6001 /* Current display element of IT is from a C string. */
6002 IT_BYTEPOS (*it) += it->len;
6003 IT_CHARPOS (*it) += 1;
6004 break;
6005
6006 case GET_FROM_DISPLAY_VECTOR:
6007 /* Current display element of IT is from a display table entry.
6008 Advance in the display table definition. Reset it to null if
6009 end reached, and continue with characters from buffers/
6010 strings. */
6011 ++it->current.dpvec_index;
6012
6013 /* Restore face of the iterator to what they were before the
6014 display vector entry (these entries may contain faces). */
6015 it->face_id = it->saved_face_id;
6016
6017 if (it->dpvec + it->current.dpvec_index == it->dpend)
6018 {
6019 int recheck_faces = it->ellipsis_p;
6020
6021 if (it->s)
6022 it->method = GET_FROM_C_STRING;
6023 else if (STRINGP (it->string))
6024 it->method = GET_FROM_STRING;
6025 else
6026 {
6027 it->method = GET_FROM_BUFFER;
6028 it->object = it->w->buffer;
6029 }
6030
6031 it->dpvec = NULL;
6032 it->current.dpvec_index = -1;
6033
6034 /* Skip over characters which were displayed via IT->dpvec. */
6035 if (it->dpvec_char_len < 0)
6036 reseat_at_next_visible_line_start (it, 1);
6037 else if (it->dpvec_char_len > 0)
6038 {
6039 if (it->method == GET_FROM_STRING
6040 && it->n_overlay_strings > 0)
6041 it->ignore_overlay_strings_at_pos_p = 1;
6042 it->len = it->dpvec_char_len;
6043 set_iterator_to_next (it, reseat_p);
6044 }
6045
6046 /* Maybe recheck faces after display vector */
6047 if (recheck_faces)
6048 it->stop_charpos = IT_CHARPOS (*it);
6049 }
6050 break;
6051
6052 case GET_FROM_STRING:
6053 /* Current display element is a character from a Lisp string. */
6054 xassert (it->s == NULL && STRINGP (it->string));
6055 IT_STRING_BYTEPOS (*it) += it->len;
6056 IT_STRING_CHARPOS (*it) += 1;
6057
6058 consider_string_end:
6059
6060 if (it->current.overlay_string_index >= 0)
6061 {
6062 /* IT->string is an overlay string. Advance to the
6063 next, if there is one. */
6064 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6065 next_overlay_string (it);
6066 }
6067 else
6068 {
6069 /* IT->string is not an overlay string. If we reached
6070 its end, and there is something on IT->stack, proceed
6071 with what is on the stack. This can be either another
6072 string, this time an overlay string, or a buffer. */
6073 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6074 && it->sp > 0)
6075 {
6076 pop_it (it);
6077 if (it->method == GET_FROM_STRING)
6078 goto consider_string_end;
6079 }
6080 }
6081 break;
6082
6083 case GET_FROM_IMAGE:
6084 case GET_FROM_STRETCH:
6085 /* The position etc with which we have to proceed are on
6086 the stack. The position may be at the end of a string,
6087 if the `display' property takes up the whole string. */
6088 xassert (it->sp > 0);
6089 pop_it (it);
6090 if (it->method == GET_FROM_STRING)
6091 goto consider_string_end;
6092 break;
6093
6094 default:
6095 /* There are no other methods defined, so this should be a bug. */
6096 abort ();
6097 }
6098
6099 xassert (it->method != GET_FROM_STRING
6100 || (STRINGP (it->string)
6101 && IT_STRING_CHARPOS (*it) >= 0));
6102 }
6103
6104 /* Load IT's display element fields with information about the next
6105 display element which comes from a display table entry or from the
6106 result of translating a control character to one of the forms `^C'
6107 or `\003'.
6108
6109 IT->dpvec holds the glyphs to return as characters.
6110 IT->saved_face_id holds the face id before the display vector--
6111 it is restored into IT->face_idin set_iterator_to_next. */
6112
6113 static int
6114 next_element_from_display_vector (it)
6115 struct it *it;
6116 {
6117 /* Precondition. */
6118 xassert (it->dpvec && it->current.dpvec_index >= 0);
6119
6120 it->face_id = it->saved_face_id;
6121
6122 if (INTEGERP (*it->dpvec)
6123 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6124 {
6125 GLYPH g;
6126
6127 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6128 it->c = FAST_GLYPH_CHAR (g);
6129 it->len = CHAR_BYTES (it->c);
6130
6131 /* The entry may contain a face id to use. Such a face id is
6132 the id of a Lisp face, not a realized face. A face id of
6133 zero means no face is specified. */
6134 if (it->dpvec_face_id >= 0)
6135 it->face_id = it->dpvec_face_id;
6136 else
6137 {
6138 int lface_id = FAST_GLYPH_FACE (g);
6139 if (lface_id > 0)
6140 it->face_id = merge_faces (it->f, Qt, lface_id,
6141 it->saved_face_id);
6142 }
6143 }
6144 else
6145 /* Display table entry is invalid. Return a space. */
6146 it->c = ' ', it->len = 1;
6147
6148 /* Don't change position and object of the iterator here. They are
6149 still the values of the character that had this display table
6150 entry or was translated, and that's what we want. */
6151 it->what = IT_CHARACTER;
6152 return 1;
6153 }
6154
6155
6156 /* Load IT with the next display element from Lisp string IT->string.
6157 IT->current.string_pos is the current position within the string.
6158 If IT->current.overlay_string_index >= 0, the Lisp string is an
6159 overlay string. */
6160
6161 static int
6162 next_element_from_string (it)
6163 struct it *it;
6164 {
6165 struct text_pos position;
6166
6167 xassert (STRINGP (it->string));
6168 xassert (IT_STRING_CHARPOS (*it) >= 0);
6169 position = it->current.string_pos;
6170
6171 /* Time to check for invisible text? */
6172 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6173 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6174 {
6175 handle_stop (it);
6176
6177 /* Since a handler may have changed IT->method, we must
6178 recurse here. */
6179 return get_next_display_element (it);
6180 }
6181
6182 if (it->current.overlay_string_index >= 0)
6183 {
6184 /* Get the next character from an overlay string. In overlay
6185 strings, There is no field width or padding with spaces to
6186 do. */
6187 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6188 {
6189 it->what = IT_EOB;
6190 return 0;
6191 }
6192 else if (STRING_MULTIBYTE (it->string))
6193 {
6194 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6195 const unsigned char *s = (SDATA (it->string)
6196 + IT_STRING_BYTEPOS (*it));
6197 it->c = string_char_and_length (s, remaining, &it->len);
6198 }
6199 else
6200 {
6201 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6202 it->len = 1;
6203 }
6204 }
6205 else
6206 {
6207 /* Get the next character from a Lisp string that is not an
6208 overlay string. Such strings come from the mode line, for
6209 example. We may have to pad with spaces, or truncate the
6210 string. See also next_element_from_c_string. */
6211 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6212 {
6213 it->what = IT_EOB;
6214 return 0;
6215 }
6216 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6217 {
6218 /* Pad with spaces. */
6219 it->c = ' ', it->len = 1;
6220 CHARPOS (position) = BYTEPOS (position) = -1;
6221 }
6222 else if (STRING_MULTIBYTE (it->string))
6223 {
6224 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6225 const unsigned char *s = (SDATA (it->string)
6226 + IT_STRING_BYTEPOS (*it));
6227 it->c = string_char_and_length (s, maxlen, &it->len);
6228 }
6229 else
6230 {
6231 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6232 it->len = 1;
6233 }
6234 }
6235
6236 /* Record what we have and where it came from. */
6237 it->what = IT_CHARACTER;
6238 it->object = it->string;
6239 it->position = position;
6240 return 1;
6241 }
6242
6243
6244 /* Load IT with next display element from C string IT->s.
6245 IT->string_nchars is the maximum number of characters to return
6246 from the string. IT->end_charpos may be greater than
6247 IT->string_nchars when this function is called, in which case we
6248 may have to return padding spaces. Value is zero if end of string
6249 reached, including padding spaces. */
6250
6251 static int
6252 next_element_from_c_string (it)
6253 struct it *it;
6254 {
6255 int success_p = 1;
6256
6257 xassert (it->s);
6258 it->what = IT_CHARACTER;
6259 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6260 it->object = Qnil;
6261
6262 /* IT's position can be greater IT->string_nchars in case a field
6263 width or precision has been specified when the iterator was
6264 initialized. */
6265 if (IT_CHARPOS (*it) >= it->end_charpos)
6266 {
6267 /* End of the game. */
6268 it->what = IT_EOB;
6269 success_p = 0;
6270 }
6271 else if (IT_CHARPOS (*it) >= it->string_nchars)
6272 {
6273 /* Pad with spaces. */
6274 it->c = ' ', it->len = 1;
6275 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6276 }
6277 else if (it->multibyte_p)
6278 {
6279 /* Implementation note: The calls to strlen apparently aren't a
6280 performance problem because there is no noticeable performance
6281 difference between Emacs running in unibyte or multibyte mode. */
6282 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6283 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6284 maxlen, &it->len);
6285 }
6286 else
6287 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6288
6289 return success_p;
6290 }
6291
6292
6293 /* Set up IT to return characters from an ellipsis, if appropriate.
6294 The definition of the ellipsis glyphs may come from a display table
6295 entry. This function Fills IT with the first glyph from the
6296 ellipsis if an ellipsis is to be displayed. */
6297
6298 static int
6299 next_element_from_ellipsis (it)
6300 struct it *it;
6301 {
6302 if (it->selective_display_ellipsis_p)
6303 setup_for_ellipsis (it, it->len);
6304 else
6305 {
6306 /* The face at the current position may be different from the
6307 face we find after the invisible text. Remember what it
6308 was in IT->saved_face_id, and signal that it's there by
6309 setting face_before_selective_p. */
6310 it->saved_face_id = it->face_id;
6311 it->method = GET_FROM_BUFFER;
6312 it->object = it->w->buffer;
6313 reseat_at_next_visible_line_start (it, 1);
6314 it->face_before_selective_p = 1;
6315 }
6316
6317 return get_next_display_element (it);
6318 }
6319
6320
6321 /* Deliver an image display element. The iterator IT is already
6322 filled with image information (done in handle_display_prop). Value
6323 is always 1. */
6324
6325
6326 static int
6327 next_element_from_image (it)
6328 struct it *it;
6329 {
6330 it->what = IT_IMAGE;
6331 return 1;
6332 }
6333
6334
6335 /* Fill iterator IT with next display element from a stretch glyph
6336 property. IT->object is the value of the text property. Value is
6337 always 1. */
6338
6339 static int
6340 next_element_from_stretch (it)
6341 struct it *it;
6342 {
6343 it->what = IT_STRETCH;
6344 return 1;
6345 }
6346
6347
6348 /* Load IT with the next display element from current_buffer. Value
6349 is zero if end of buffer reached. IT->stop_charpos is the next
6350 position at which to stop and check for text properties or buffer
6351 end. */
6352
6353 static int
6354 next_element_from_buffer (it)
6355 struct it *it;
6356 {
6357 int success_p = 1;
6358
6359 /* Check this assumption, otherwise, we would never enter the
6360 if-statement, below. */
6361 xassert (IT_CHARPOS (*it) >= BEGV
6362 && IT_CHARPOS (*it) <= it->stop_charpos);
6363
6364 if (IT_CHARPOS (*it) >= it->stop_charpos)
6365 {
6366 if (IT_CHARPOS (*it) >= it->end_charpos)
6367 {
6368 int overlay_strings_follow_p;
6369
6370 /* End of the game, except when overlay strings follow that
6371 haven't been returned yet. */
6372 if (it->overlay_strings_at_end_processed_p)
6373 overlay_strings_follow_p = 0;
6374 else
6375 {
6376 it->overlay_strings_at_end_processed_p = 1;
6377 overlay_strings_follow_p = get_overlay_strings (it, 0);
6378 }
6379
6380 if (overlay_strings_follow_p)
6381 success_p = get_next_display_element (it);
6382 else
6383 {
6384 it->what = IT_EOB;
6385 it->position = it->current.pos;
6386 success_p = 0;
6387 }
6388 }
6389 else
6390 {
6391 handle_stop (it);
6392 return get_next_display_element (it);
6393 }
6394 }
6395 else
6396 {
6397 /* No face changes, overlays etc. in sight, so just return a
6398 character from current_buffer. */
6399 unsigned char *p;
6400
6401 /* Maybe run the redisplay end trigger hook. Performance note:
6402 This doesn't seem to cost measurable time. */
6403 if (it->redisplay_end_trigger_charpos
6404 && it->glyph_row
6405 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6406 run_redisplay_end_trigger_hook (it);
6407
6408 /* Get the next character, maybe multibyte. */
6409 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6410 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6411 {
6412 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6413 - IT_BYTEPOS (*it));
6414 it->c = string_char_and_length (p, maxlen, &it->len);
6415 }
6416 else
6417 it->c = *p, it->len = 1;
6418
6419 /* Record what we have and where it came from. */
6420 it->what = IT_CHARACTER;
6421 it->object = it->w->buffer;
6422 it->position = it->current.pos;
6423
6424 /* Normally we return the character found above, except when we
6425 really want to return an ellipsis for selective display. */
6426 if (it->selective)
6427 {
6428 if (it->c == '\n')
6429 {
6430 /* A value of selective > 0 means hide lines indented more
6431 than that number of columns. */
6432 if (it->selective > 0
6433 && IT_CHARPOS (*it) + 1 < ZV
6434 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6435 IT_BYTEPOS (*it) + 1,
6436 (double) it->selective)) /* iftc */
6437 {
6438 success_p = next_element_from_ellipsis (it);
6439 it->dpvec_char_len = -1;
6440 }
6441 }
6442 else if (it->c == '\r' && it->selective == -1)
6443 {
6444 /* A value of selective == -1 means that everything from the
6445 CR to the end of the line is invisible, with maybe an
6446 ellipsis displayed for it. */
6447 success_p = next_element_from_ellipsis (it);
6448 it->dpvec_char_len = -1;
6449 }
6450 }
6451 }
6452
6453 /* Value is zero if end of buffer reached. */
6454 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6455 return success_p;
6456 }
6457
6458
6459 /* Run the redisplay end trigger hook for IT. */
6460
6461 static void
6462 run_redisplay_end_trigger_hook (it)
6463 struct it *it;
6464 {
6465 Lisp_Object args[3];
6466
6467 /* IT->glyph_row should be non-null, i.e. we should be actually
6468 displaying something, or otherwise we should not run the hook. */
6469 xassert (it->glyph_row);
6470
6471 /* Set up hook arguments. */
6472 args[0] = Qredisplay_end_trigger_functions;
6473 args[1] = it->window;
6474 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6475 it->redisplay_end_trigger_charpos = 0;
6476
6477 /* Since we are *trying* to run these functions, don't try to run
6478 them again, even if they get an error. */
6479 it->w->redisplay_end_trigger = Qnil;
6480 Frun_hook_with_args (3, args);
6481
6482 /* Notice if it changed the face of the character we are on. */
6483 handle_face_prop (it);
6484 }
6485
6486
6487 /* Deliver a composition display element. The iterator IT is already
6488 filled with composition information (done in
6489 handle_composition_prop). Value is always 1. */
6490
6491 static int
6492 next_element_from_composition (it)
6493 struct it *it;
6494 {
6495 it->what = IT_COMPOSITION;
6496 it->position = (STRINGP (it->string)
6497 ? it->current.string_pos
6498 : it->current.pos);
6499 if (STRINGP (it->string))
6500 it->object = it->string;
6501 else
6502 it->object = it->w->buffer;
6503 return 1;
6504 }
6505
6506
6507 \f
6508 /***********************************************************************
6509 Moving an iterator without producing glyphs
6510 ***********************************************************************/
6511
6512 /* Check if iterator is at a position corresponding to a valid buffer
6513 position after some move_it_ call. */
6514
6515 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6516 ((it)->method == GET_FROM_STRING \
6517 ? IT_STRING_CHARPOS (*it) == 0 \
6518 : 1)
6519
6520
6521 /* Move iterator IT to a specified buffer or X position within one
6522 line on the display without producing glyphs.
6523
6524 OP should be a bit mask including some or all of these bits:
6525 MOVE_TO_X: Stop on reaching x-position TO_X.
6526 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6527 Regardless of OP's value, stop in reaching the end of the display line.
6528
6529 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6530 This means, in particular, that TO_X includes window's horizontal
6531 scroll amount.
6532
6533 The return value has several possible values that
6534 say what condition caused the scan to stop:
6535
6536 MOVE_POS_MATCH_OR_ZV
6537 - when TO_POS or ZV was reached.
6538
6539 MOVE_X_REACHED
6540 -when TO_X was reached before TO_POS or ZV were reached.
6541
6542 MOVE_LINE_CONTINUED
6543 - when we reached the end of the display area and the line must
6544 be continued.
6545
6546 MOVE_LINE_TRUNCATED
6547 - when we reached the end of the display area and the line is
6548 truncated.
6549
6550 MOVE_NEWLINE_OR_CR
6551 - when we stopped at a line end, i.e. a newline or a CR and selective
6552 display is on. */
6553
6554 static enum move_it_result
6555 move_it_in_display_line_to (it, to_charpos, to_x, op)
6556 struct it *it;
6557 int to_charpos, to_x, op;
6558 {
6559 enum move_it_result result = MOVE_UNDEFINED;
6560 struct glyph_row *saved_glyph_row;
6561
6562 /* Don't produce glyphs in produce_glyphs. */
6563 saved_glyph_row = it->glyph_row;
6564 it->glyph_row = NULL;
6565
6566 #define BUFFER_POS_REACHED_P() \
6567 ((op & MOVE_TO_POS) != 0 \
6568 && BUFFERP (it->object) \
6569 && IT_CHARPOS (*it) >= to_charpos \
6570 && (it->method == GET_FROM_BUFFER \
6571 || (it->method == GET_FROM_DISPLAY_VECTOR \
6572 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6573
6574
6575 while (1)
6576 {
6577 int x, i, ascent = 0, descent = 0;
6578
6579 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6580 if ((op & MOVE_TO_POS) != 0
6581 && BUFFERP (it->object)
6582 && it->method == GET_FROM_BUFFER
6583 && IT_CHARPOS (*it) > to_charpos)
6584 {
6585 result = MOVE_POS_MATCH_OR_ZV;
6586 break;
6587 }
6588
6589 /* Stop when ZV reached.
6590 We used to stop here when TO_CHARPOS reached as well, but that is
6591 too soon if this glyph does not fit on this line. So we handle it
6592 explicitly below. */
6593 if (!get_next_display_element (it)
6594 || (it->truncate_lines_p
6595 && BUFFER_POS_REACHED_P ()))
6596 {
6597 result = MOVE_POS_MATCH_OR_ZV;
6598 break;
6599 }
6600
6601 /* The call to produce_glyphs will get the metrics of the
6602 display element IT is loaded with. We record in x the
6603 x-position before this display element in case it does not
6604 fit on the line. */
6605 x = it->current_x;
6606
6607 /* Remember the line height so far in case the next element doesn't
6608 fit on the line. */
6609 if (!it->truncate_lines_p)
6610 {
6611 ascent = it->max_ascent;
6612 descent = it->max_descent;
6613 }
6614
6615 PRODUCE_GLYPHS (it);
6616
6617 if (it->area != TEXT_AREA)
6618 {
6619 set_iterator_to_next (it, 1);
6620 continue;
6621 }
6622
6623 /* The number of glyphs we get back in IT->nglyphs will normally
6624 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6625 character on a terminal frame, or (iii) a line end. For the
6626 second case, IT->nglyphs - 1 padding glyphs will be present
6627 (on X frames, there is only one glyph produced for a
6628 composite character.
6629
6630 The behavior implemented below means, for continuation lines,
6631 that as many spaces of a TAB as fit on the current line are
6632 displayed there. For terminal frames, as many glyphs of a
6633 multi-glyph character are displayed in the current line, too.
6634 This is what the old redisplay code did, and we keep it that
6635 way. Under X, the whole shape of a complex character must
6636 fit on the line or it will be completely displayed in the
6637 next line.
6638
6639 Note that both for tabs and padding glyphs, all glyphs have
6640 the same width. */
6641 if (it->nglyphs)
6642 {
6643 /* More than one glyph or glyph doesn't fit on line. All
6644 glyphs have the same width. */
6645 int single_glyph_width = it->pixel_width / it->nglyphs;
6646 int new_x;
6647 int x_before_this_char = x;
6648 int hpos_before_this_char = it->hpos;
6649
6650 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6651 {
6652 new_x = x + single_glyph_width;
6653
6654 /* We want to leave anything reaching TO_X to the caller. */
6655 if ((op & MOVE_TO_X) && new_x > to_x)
6656 {
6657 if (BUFFER_POS_REACHED_P ())
6658 goto buffer_pos_reached;
6659 it->current_x = x;
6660 result = MOVE_X_REACHED;
6661 break;
6662 }
6663 else if (/* Lines are continued. */
6664 !it->truncate_lines_p
6665 && (/* And glyph doesn't fit on the line. */
6666 new_x > it->last_visible_x
6667 /* Or it fits exactly and we're on a window
6668 system frame. */
6669 || (new_x == it->last_visible_x
6670 && FRAME_WINDOW_P (it->f))))
6671 {
6672 if (/* IT->hpos == 0 means the very first glyph
6673 doesn't fit on the line, e.g. a wide image. */
6674 it->hpos == 0
6675 || (new_x == it->last_visible_x
6676 && FRAME_WINDOW_P (it->f)))
6677 {
6678 ++it->hpos;
6679 it->current_x = new_x;
6680
6681 /* The character's last glyph just barely fits
6682 in this row. */
6683 if (i == it->nglyphs - 1)
6684 {
6685 /* If this is the destination position,
6686 return a position *before* it in this row,
6687 now that we know it fits in this row. */
6688 if (BUFFER_POS_REACHED_P ())
6689 {
6690 it->hpos = hpos_before_this_char;
6691 it->current_x = x_before_this_char;
6692 result = MOVE_POS_MATCH_OR_ZV;
6693 break;
6694 }
6695
6696 set_iterator_to_next (it, 1);
6697 #ifdef HAVE_WINDOW_SYSTEM
6698 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6699 {
6700 if (!get_next_display_element (it))
6701 {
6702 result = MOVE_POS_MATCH_OR_ZV;
6703 break;
6704 }
6705 if (BUFFER_POS_REACHED_P ())
6706 {
6707 if (ITERATOR_AT_END_OF_LINE_P (it))
6708 result = MOVE_POS_MATCH_OR_ZV;
6709 else
6710 result = MOVE_LINE_CONTINUED;
6711 break;
6712 }
6713 if (ITERATOR_AT_END_OF_LINE_P (it))
6714 {
6715 result = MOVE_NEWLINE_OR_CR;
6716 break;
6717 }
6718 }
6719 #endif /* HAVE_WINDOW_SYSTEM */
6720 }
6721 }
6722 else
6723 {
6724 it->current_x = x;
6725 it->max_ascent = ascent;
6726 it->max_descent = descent;
6727 }
6728
6729 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6730 IT_CHARPOS (*it)));
6731 result = MOVE_LINE_CONTINUED;
6732 break;
6733 }
6734 else if (BUFFER_POS_REACHED_P ())
6735 goto buffer_pos_reached;
6736 else if (new_x > it->first_visible_x)
6737 {
6738 /* Glyph is visible. Increment number of glyphs that
6739 would be displayed. */
6740 ++it->hpos;
6741 }
6742 else
6743 {
6744 /* Glyph is completely off the left margin of the display
6745 area. Nothing to do. */
6746 }
6747 }
6748
6749 if (result != MOVE_UNDEFINED)
6750 break;
6751 }
6752 else if (BUFFER_POS_REACHED_P ())
6753 {
6754 buffer_pos_reached:
6755 it->current_x = x;
6756 it->max_ascent = ascent;
6757 it->max_descent = descent;
6758 result = MOVE_POS_MATCH_OR_ZV;
6759 break;
6760 }
6761 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6762 {
6763 /* Stop when TO_X specified and reached. This check is
6764 necessary here because of lines consisting of a line end,
6765 only. The line end will not produce any glyphs and we
6766 would never get MOVE_X_REACHED. */
6767 xassert (it->nglyphs == 0);
6768 result = MOVE_X_REACHED;
6769 break;
6770 }
6771
6772 /* Is this a line end? If yes, we're done. */
6773 if (ITERATOR_AT_END_OF_LINE_P (it))
6774 {
6775 result = MOVE_NEWLINE_OR_CR;
6776 break;
6777 }
6778
6779 /* The current display element has been consumed. Advance
6780 to the next. */
6781 set_iterator_to_next (it, 1);
6782
6783 /* Stop if lines are truncated and IT's current x-position is
6784 past the right edge of the window now. */
6785 if (it->truncate_lines_p
6786 && it->current_x >= it->last_visible_x)
6787 {
6788 #ifdef HAVE_WINDOW_SYSTEM
6789 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6790 {
6791 if (!get_next_display_element (it)
6792 || BUFFER_POS_REACHED_P ())
6793 {
6794 result = MOVE_POS_MATCH_OR_ZV;
6795 break;
6796 }
6797 if (ITERATOR_AT_END_OF_LINE_P (it))
6798 {
6799 result = MOVE_NEWLINE_OR_CR;
6800 break;
6801 }
6802 }
6803 #endif /* HAVE_WINDOW_SYSTEM */
6804 result = MOVE_LINE_TRUNCATED;
6805 break;
6806 }
6807 }
6808
6809 #undef BUFFER_POS_REACHED_P
6810
6811 /* Restore the iterator settings altered at the beginning of this
6812 function. */
6813 it->glyph_row = saved_glyph_row;
6814 return result;
6815 }
6816
6817
6818 /* Move IT forward until it satisfies one or more of the criteria in
6819 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6820
6821 OP is a bit-mask that specifies where to stop, and in particular,
6822 which of those four position arguments makes a difference. See the
6823 description of enum move_operation_enum.
6824
6825 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6826 screen line, this function will set IT to the next position >
6827 TO_CHARPOS. */
6828
6829 void
6830 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6831 struct it *it;
6832 int to_charpos, to_x, to_y, to_vpos;
6833 int op;
6834 {
6835 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6836 int line_height;
6837 int reached = 0;
6838
6839 for (;;)
6840 {
6841 if (op & MOVE_TO_VPOS)
6842 {
6843 /* If no TO_CHARPOS and no TO_X specified, stop at the
6844 start of the line TO_VPOS. */
6845 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6846 {
6847 if (it->vpos == to_vpos)
6848 {
6849 reached = 1;
6850 break;
6851 }
6852 else
6853 skip = move_it_in_display_line_to (it, -1, -1, 0);
6854 }
6855 else
6856 {
6857 /* TO_VPOS >= 0 means stop at TO_X in the line at
6858 TO_VPOS, or at TO_POS, whichever comes first. */
6859 if (it->vpos == to_vpos)
6860 {
6861 reached = 2;
6862 break;
6863 }
6864
6865 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6866
6867 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6868 {
6869 reached = 3;
6870 break;
6871 }
6872 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6873 {
6874 /* We have reached TO_X but not in the line we want. */
6875 skip = move_it_in_display_line_to (it, to_charpos,
6876 -1, MOVE_TO_POS);
6877 if (skip == MOVE_POS_MATCH_OR_ZV)
6878 {
6879 reached = 4;
6880 break;
6881 }
6882 }
6883 }
6884 }
6885 else if (op & MOVE_TO_Y)
6886 {
6887 struct it it_backup;
6888
6889 /* TO_Y specified means stop at TO_X in the line containing
6890 TO_Y---or at TO_CHARPOS if this is reached first. The
6891 problem is that we can't really tell whether the line
6892 contains TO_Y before we have completely scanned it, and
6893 this may skip past TO_X. What we do is to first scan to
6894 TO_X.
6895
6896 If TO_X is not specified, use a TO_X of zero. The reason
6897 is to make the outcome of this function more predictable.
6898 If we didn't use TO_X == 0, we would stop at the end of
6899 the line which is probably not what a caller would expect
6900 to happen. */
6901 skip = move_it_in_display_line_to (it, to_charpos,
6902 ((op & MOVE_TO_X)
6903 ? to_x : 0),
6904 (MOVE_TO_X
6905 | (op & MOVE_TO_POS)));
6906
6907 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6908 if (skip == MOVE_POS_MATCH_OR_ZV)
6909 {
6910 reached = 5;
6911 break;
6912 }
6913
6914 /* If TO_X was reached, we would like to know whether TO_Y
6915 is in the line. This can only be said if we know the
6916 total line height which requires us to scan the rest of
6917 the line. */
6918 if (skip == MOVE_X_REACHED)
6919 {
6920 /* Wait! We can conclude that TO_Y is in the line if
6921 the already scanned glyphs make the line tall enough
6922 because further scanning doesn't make it shorter. */
6923 line_height = it->max_ascent + it->max_descent;
6924 if (to_y >= it->current_y
6925 && to_y < it->current_y + line_height)
6926 {
6927 reached = 6;
6928 break;
6929 }
6930 it_backup = *it;
6931 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6932 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6933 op & MOVE_TO_POS);
6934 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6935 }
6936
6937 /* Now, decide whether TO_Y is in this line. */
6938 line_height = it->max_ascent + it->max_descent;
6939 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6940
6941 if (to_y >= it->current_y
6942 && to_y < it->current_y + line_height)
6943 {
6944 if (skip == MOVE_X_REACHED)
6945 /* If TO_Y is in this line and TO_X was reached above,
6946 we scanned too far. We have to restore IT's settings
6947 to the ones before skipping. */
6948 *it = it_backup;
6949 reached = 6;
6950 }
6951 else if (skip == MOVE_X_REACHED)
6952 {
6953 skip = skip2;
6954 if (skip == MOVE_POS_MATCH_OR_ZV)
6955 reached = 7;
6956 }
6957
6958 if (reached)
6959 break;
6960 }
6961 else if (BUFFERP (it->object)
6962 && it->method == GET_FROM_BUFFER
6963 && IT_CHARPOS (*it) >= to_charpos)
6964 skip = MOVE_POS_MATCH_OR_ZV;
6965 else
6966 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6967
6968 switch (skip)
6969 {
6970 case MOVE_POS_MATCH_OR_ZV:
6971 reached = 8;
6972 goto out;
6973
6974 case MOVE_NEWLINE_OR_CR:
6975 set_iterator_to_next (it, 1);
6976 it->continuation_lines_width = 0;
6977 break;
6978
6979 case MOVE_LINE_TRUNCATED:
6980 it->continuation_lines_width = 0;
6981 reseat_at_next_visible_line_start (it, 0);
6982 if ((op & MOVE_TO_POS) != 0
6983 && IT_CHARPOS (*it) > to_charpos)
6984 {
6985 reached = 9;
6986 goto out;
6987 }
6988 break;
6989
6990 case MOVE_LINE_CONTINUED:
6991 /* For continued lines ending in a tab, some of the glyphs
6992 associated with the tab are displayed on the current
6993 line. Since it->current_x does not include these glyphs,
6994 we use it->last_visible_x instead. */
6995 it->continuation_lines_width +=
6996 (it->c == '\t') ? it->last_visible_x : it->current_x;
6997 break;
6998
6999 default:
7000 abort ();
7001 }
7002
7003 /* Reset/increment for the next run. */
7004 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7005 it->current_x = it->hpos = 0;
7006 it->current_y += it->max_ascent + it->max_descent;
7007 ++it->vpos;
7008 last_height = it->max_ascent + it->max_descent;
7009 last_max_ascent = it->max_ascent;
7010 it->max_ascent = it->max_descent = 0;
7011 }
7012
7013 out:
7014
7015 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7016 }
7017
7018
7019 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7020
7021 If DY > 0, move IT backward at least that many pixels. DY = 0
7022 means move IT backward to the preceding line start or BEGV. This
7023 function may move over more than DY pixels if IT->current_y - DY
7024 ends up in the middle of a line; in this case IT->current_y will be
7025 set to the top of the line moved to. */
7026
7027 void
7028 move_it_vertically_backward (it, dy)
7029 struct it *it;
7030 int dy;
7031 {
7032 int nlines, h;
7033 struct it it2, it3;
7034 int start_pos;
7035
7036 move_further_back:
7037 xassert (dy >= 0);
7038
7039 start_pos = IT_CHARPOS (*it);
7040
7041 /* Estimate how many newlines we must move back. */
7042 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7043
7044 /* Set the iterator's position that many lines back. */
7045 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7046 back_to_previous_visible_line_start (it);
7047
7048 /* Reseat the iterator here. When moving backward, we don't want
7049 reseat to skip forward over invisible text, set up the iterator
7050 to deliver from overlay strings at the new position etc. So,
7051 use reseat_1 here. */
7052 reseat_1 (it, it->current.pos, 1);
7053
7054 /* We are now surely at a line start. */
7055 it->current_x = it->hpos = 0;
7056 it->continuation_lines_width = 0;
7057
7058 /* Move forward and see what y-distance we moved. First move to the
7059 start of the next line so that we get its height. We need this
7060 height to be able to tell whether we reached the specified
7061 y-distance. */
7062 it2 = *it;
7063 it2.max_ascent = it2.max_descent = 0;
7064 do
7065 {
7066 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7067 MOVE_TO_POS | MOVE_TO_VPOS);
7068 }
7069 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7070 xassert (IT_CHARPOS (*it) >= BEGV);
7071 it3 = it2;
7072
7073 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7074 xassert (IT_CHARPOS (*it) >= BEGV);
7075 /* H is the actual vertical distance from the position in *IT
7076 and the starting position. */
7077 h = it2.current_y - it->current_y;
7078 /* NLINES is the distance in number of lines. */
7079 nlines = it2.vpos - it->vpos;
7080
7081 /* Correct IT's y and vpos position
7082 so that they are relative to the starting point. */
7083 it->vpos -= nlines;
7084 it->current_y -= h;
7085
7086 if (dy == 0)
7087 {
7088 /* DY == 0 means move to the start of the screen line. The
7089 value of nlines is > 0 if continuation lines were involved. */
7090 if (nlines > 0)
7091 move_it_by_lines (it, nlines, 1);
7092 #if 0
7093 /* I think this assert is bogus if buffer contains
7094 invisible text or images. KFS. */
7095 xassert (IT_CHARPOS (*it) <= start_pos);
7096 #endif
7097 }
7098 else
7099 {
7100 /* The y-position we try to reach, relative to *IT.
7101 Note that H has been subtracted in front of the if-statement. */
7102 int target_y = it->current_y + h - dy;
7103 int y0 = it3.current_y;
7104 int y1 = line_bottom_y (&it3);
7105 int line_height = y1 - y0;
7106
7107 /* If we did not reach target_y, try to move further backward if
7108 we can. If we moved too far backward, try to move forward. */
7109 if (target_y < it->current_y
7110 /* This is heuristic. In a window that's 3 lines high, with
7111 a line height of 13 pixels each, recentering with point
7112 on the bottom line will try to move -39/2 = 19 pixels
7113 backward. Try to avoid moving into the first line. */
7114 && (it->current_y - target_y
7115 > min (window_box_height (it->w), line_height * 2 / 3))
7116 && IT_CHARPOS (*it) > BEGV)
7117 {
7118 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7119 target_y - it->current_y));
7120 dy = it->current_y - target_y;
7121 goto move_further_back;
7122 }
7123 else if (target_y >= it->current_y + line_height
7124 && IT_CHARPOS (*it) < ZV)
7125 {
7126 /* Should move forward by at least one line, maybe more.
7127
7128 Note: Calling move_it_by_lines can be expensive on
7129 terminal frames, where compute_motion is used (via
7130 vmotion) to do the job, when there are very long lines
7131 and truncate-lines is nil. That's the reason for
7132 treating terminal frames specially here. */
7133
7134 if (!FRAME_WINDOW_P (it->f))
7135 move_it_vertically (it, target_y - (it->current_y + line_height));
7136 else
7137 {
7138 do
7139 {
7140 move_it_by_lines (it, 1, 1);
7141 }
7142 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7143 }
7144
7145 #if 0
7146 /* I think this assert is bogus if buffer contains
7147 invisible text or images. KFS. */
7148 xassert (IT_CHARPOS (*it) >= BEGV);
7149 #endif
7150 }
7151 }
7152 }
7153
7154
7155 /* Move IT by a specified amount of pixel lines DY. DY negative means
7156 move backwards. DY = 0 means move to start of screen line. At the
7157 end, IT will be on the start of a screen line. */
7158
7159 void
7160 move_it_vertically (it, dy)
7161 struct it *it;
7162 int dy;
7163 {
7164 if (dy <= 0)
7165 move_it_vertically_backward (it, -dy);
7166 else
7167 {
7168 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7169 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7170 MOVE_TO_POS | MOVE_TO_Y);
7171 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7172
7173 /* If buffer ends in ZV without a newline, move to the start of
7174 the line to satisfy the post-condition. */
7175 if (IT_CHARPOS (*it) == ZV
7176 && ZV > BEGV
7177 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7178 move_it_by_lines (it, 0, 0);
7179 }
7180 }
7181
7182
7183 /* Move iterator IT past the end of the text line it is in. */
7184
7185 void
7186 move_it_past_eol (it)
7187 struct it *it;
7188 {
7189 enum move_it_result rc;
7190
7191 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7192 if (rc == MOVE_NEWLINE_OR_CR)
7193 set_iterator_to_next (it, 0);
7194 }
7195
7196
7197 #if 0 /* Currently not used. */
7198
7199 /* Return non-zero if some text between buffer positions START_CHARPOS
7200 and END_CHARPOS is invisible. IT->window is the window for text
7201 property lookup. */
7202
7203 static int
7204 invisible_text_between_p (it, start_charpos, end_charpos)
7205 struct it *it;
7206 int start_charpos, end_charpos;
7207 {
7208 Lisp_Object prop, limit;
7209 int invisible_found_p;
7210
7211 xassert (it != NULL && start_charpos <= end_charpos);
7212
7213 /* Is text at START invisible? */
7214 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7215 it->window);
7216 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7217 invisible_found_p = 1;
7218 else
7219 {
7220 limit = Fnext_single_char_property_change (make_number (start_charpos),
7221 Qinvisible, Qnil,
7222 make_number (end_charpos));
7223 invisible_found_p = XFASTINT (limit) < end_charpos;
7224 }
7225
7226 return invisible_found_p;
7227 }
7228
7229 #endif /* 0 */
7230
7231
7232 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7233 negative means move up. DVPOS == 0 means move to the start of the
7234 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7235 NEED_Y_P is zero, IT->current_y will be left unchanged.
7236
7237 Further optimization ideas: If we would know that IT->f doesn't use
7238 a face with proportional font, we could be faster for
7239 truncate-lines nil. */
7240
7241 void
7242 move_it_by_lines (it, dvpos, need_y_p)
7243 struct it *it;
7244 int dvpos, need_y_p;
7245 {
7246 struct position pos;
7247
7248 if (!FRAME_WINDOW_P (it->f))
7249 {
7250 struct text_pos textpos;
7251
7252 /* We can use vmotion on frames without proportional fonts. */
7253 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7254 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7255 reseat (it, textpos, 1);
7256 it->vpos += pos.vpos;
7257 it->current_y += pos.vpos;
7258 }
7259 else if (dvpos == 0)
7260 {
7261 /* DVPOS == 0 means move to the start of the screen line. */
7262 move_it_vertically_backward (it, 0);
7263 xassert (it->current_x == 0 && it->hpos == 0);
7264 /* Let next call to line_bottom_y calculate real line height */
7265 last_height = 0;
7266 }
7267 else if (dvpos > 0)
7268 {
7269 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7270 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7271 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7272 }
7273 else
7274 {
7275 struct it it2;
7276 int start_charpos, i;
7277
7278 /* Start at the beginning of the screen line containing IT's
7279 position. This may actually move vertically backwards,
7280 in case of overlays, so adjust dvpos accordingly. */
7281 dvpos += it->vpos;
7282 move_it_vertically_backward (it, 0);
7283 dvpos -= it->vpos;
7284
7285 /* Go back -DVPOS visible lines and reseat the iterator there. */
7286 start_charpos = IT_CHARPOS (*it);
7287 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7288 back_to_previous_visible_line_start (it);
7289 reseat (it, it->current.pos, 1);
7290
7291 /* Move further back if we end up in a string or an image. */
7292 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7293 {
7294 /* First try to move to start of display line. */
7295 dvpos += it->vpos;
7296 move_it_vertically_backward (it, 0);
7297 dvpos -= it->vpos;
7298 if (IT_POS_VALID_AFTER_MOVE_P (it))
7299 break;
7300 /* If start of line is still in string or image,
7301 move further back. */
7302 back_to_previous_visible_line_start (it);
7303 reseat (it, it->current.pos, 1);
7304 dvpos--;
7305 }
7306
7307 it->current_x = it->hpos = 0;
7308
7309 /* Above call may have moved too far if continuation lines
7310 are involved. Scan forward and see if it did. */
7311 it2 = *it;
7312 it2.vpos = it2.current_y = 0;
7313 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7314 it->vpos -= it2.vpos;
7315 it->current_y -= it2.current_y;
7316 it->current_x = it->hpos = 0;
7317
7318 /* If we moved too far back, move IT some lines forward. */
7319 if (it2.vpos > -dvpos)
7320 {
7321 int delta = it2.vpos + dvpos;
7322 it2 = *it;
7323 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7324 /* Move back again if we got too far ahead. */
7325 if (IT_CHARPOS (*it) >= start_charpos)
7326 *it = it2;
7327 }
7328 }
7329 }
7330
7331 /* Return 1 if IT points into the middle of a display vector. */
7332
7333 int
7334 in_display_vector_p (it)
7335 struct it *it;
7336 {
7337 return (it->method == GET_FROM_DISPLAY_VECTOR
7338 && it->current.dpvec_index > 0
7339 && it->dpvec + it->current.dpvec_index != it->dpend);
7340 }
7341
7342 \f
7343 /***********************************************************************
7344 Messages
7345 ***********************************************************************/
7346
7347
7348 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7349 to *Messages*. */
7350
7351 void
7352 add_to_log (format, arg1, arg2)
7353 char *format;
7354 Lisp_Object arg1, arg2;
7355 {
7356 Lisp_Object args[3];
7357 Lisp_Object msg, fmt;
7358 char *buffer;
7359 int len;
7360 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7361 USE_SAFE_ALLOCA;
7362
7363 /* Do nothing if called asynchronously. Inserting text into
7364 a buffer may call after-change-functions and alike and
7365 that would means running Lisp asynchronously. */
7366 if (handling_signal)
7367 return;
7368
7369 fmt = msg = Qnil;
7370 GCPRO4 (fmt, msg, arg1, arg2);
7371
7372 args[0] = fmt = build_string (format);
7373 args[1] = arg1;
7374 args[2] = arg2;
7375 msg = Fformat (3, args);
7376
7377 len = SBYTES (msg) + 1;
7378 SAFE_ALLOCA (buffer, char *, len);
7379 bcopy (SDATA (msg), buffer, len);
7380
7381 message_dolog (buffer, len - 1, 1, 0);
7382 SAFE_FREE ();
7383
7384 UNGCPRO;
7385 }
7386
7387
7388 /* Output a newline in the *Messages* buffer if "needs" one. */
7389
7390 void
7391 message_log_maybe_newline ()
7392 {
7393 if (message_log_need_newline)
7394 message_dolog ("", 0, 1, 0);
7395 }
7396
7397
7398 /* Add a string M of length NBYTES to the message log, optionally
7399 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7400 nonzero, means interpret the contents of M as multibyte. This
7401 function calls low-level routines in order to bypass text property
7402 hooks, etc. which might not be safe to run.
7403
7404 This may GC (insert may run before/after change hooks),
7405 so the buffer M must NOT point to a Lisp string. */
7406
7407 void
7408 message_dolog (m, nbytes, nlflag, multibyte)
7409 const char *m;
7410 int nbytes, nlflag, multibyte;
7411 {
7412 if (!NILP (Vmemory_full))
7413 return;
7414
7415 if (!NILP (Vmessage_log_max))
7416 {
7417 struct buffer *oldbuf;
7418 Lisp_Object oldpoint, oldbegv, oldzv;
7419 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7420 int point_at_end = 0;
7421 int zv_at_end = 0;
7422 Lisp_Object old_deactivate_mark, tem;
7423 struct gcpro gcpro1;
7424
7425 old_deactivate_mark = Vdeactivate_mark;
7426 oldbuf = current_buffer;
7427 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7428 current_buffer->undo_list = Qt;
7429
7430 oldpoint = message_dolog_marker1;
7431 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7432 oldbegv = message_dolog_marker2;
7433 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7434 oldzv = message_dolog_marker3;
7435 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7436 GCPRO1 (old_deactivate_mark);
7437
7438 if (PT == Z)
7439 point_at_end = 1;
7440 if (ZV == Z)
7441 zv_at_end = 1;
7442
7443 BEGV = BEG;
7444 BEGV_BYTE = BEG_BYTE;
7445 ZV = Z;
7446 ZV_BYTE = Z_BYTE;
7447 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7448
7449 /* Insert the string--maybe converting multibyte to single byte
7450 or vice versa, so that all the text fits the buffer. */
7451 if (multibyte
7452 && NILP (current_buffer->enable_multibyte_characters))
7453 {
7454 int i, c, char_bytes;
7455 unsigned char work[1];
7456
7457 /* Convert a multibyte string to single-byte
7458 for the *Message* buffer. */
7459 for (i = 0; i < nbytes; i += char_bytes)
7460 {
7461 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7462 work[0] = (ASCII_CHAR_P (c)
7463 ? c
7464 : multibyte_char_to_unibyte (c, Qnil));
7465 insert_1_both (work, 1, 1, 1, 0, 0);
7466 }
7467 }
7468 else if (! multibyte
7469 && ! NILP (current_buffer->enable_multibyte_characters))
7470 {
7471 int i, c, char_bytes;
7472 unsigned char *msg = (unsigned char *) m;
7473 unsigned char str[MAX_MULTIBYTE_LENGTH];
7474 /* Convert a single-byte string to multibyte
7475 for the *Message* buffer. */
7476 for (i = 0; i < nbytes; i++)
7477 {
7478 c = msg[i];
7479 c = unibyte_char_to_multibyte (c);
7480 char_bytes = CHAR_STRING (c, str);
7481 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7482 }
7483 }
7484 else if (nbytes)
7485 insert_1 (m, nbytes, 1, 0, 0);
7486
7487 if (nlflag)
7488 {
7489 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7490 insert_1 ("\n", 1, 1, 0, 0);
7491
7492 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7493 this_bol = PT;
7494 this_bol_byte = PT_BYTE;
7495
7496 /* See if this line duplicates the previous one.
7497 If so, combine duplicates. */
7498 if (this_bol > BEG)
7499 {
7500 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7501 prev_bol = PT;
7502 prev_bol_byte = PT_BYTE;
7503
7504 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7505 this_bol, this_bol_byte);
7506 if (dup)
7507 {
7508 del_range_both (prev_bol, prev_bol_byte,
7509 this_bol, this_bol_byte, 0);
7510 if (dup > 1)
7511 {
7512 char dupstr[40];
7513 int duplen;
7514
7515 /* If you change this format, don't forget to also
7516 change message_log_check_duplicate. */
7517 sprintf (dupstr, " [%d times]", dup);
7518 duplen = strlen (dupstr);
7519 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7520 insert_1 (dupstr, duplen, 1, 0, 1);
7521 }
7522 }
7523 }
7524
7525 /* If we have more than the desired maximum number of lines
7526 in the *Messages* buffer now, delete the oldest ones.
7527 This is safe because we don't have undo in this buffer. */
7528
7529 if (NATNUMP (Vmessage_log_max))
7530 {
7531 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7532 -XFASTINT (Vmessage_log_max) - 1, 0);
7533 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7534 }
7535 }
7536 BEGV = XMARKER (oldbegv)->charpos;
7537 BEGV_BYTE = marker_byte_position (oldbegv);
7538
7539 if (zv_at_end)
7540 {
7541 ZV = Z;
7542 ZV_BYTE = Z_BYTE;
7543 }
7544 else
7545 {
7546 ZV = XMARKER (oldzv)->charpos;
7547 ZV_BYTE = marker_byte_position (oldzv);
7548 }
7549
7550 if (point_at_end)
7551 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7552 else
7553 /* We can't do Fgoto_char (oldpoint) because it will run some
7554 Lisp code. */
7555 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7556 XMARKER (oldpoint)->bytepos);
7557
7558 UNGCPRO;
7559 unchain_marker (XMARKER (oldpoint));
7560 unchain_marker (XMARKER (oldbegv));
7561 unchain_marker (XMARKER (oldzv));
7562
7563 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7564 set_buffer_internal (oldbuf);
7565 if (NILP (tem))
7566 windows_or_buffers_changed = old_windows_or_buffers_changed;
7567 message_log_need_newline = !nlflag;
7568 Vdeactivate_mark = old_deactivate_mark;
7569 }
7570 }
7571
7572
7573 /* We are at the end of the buffer after just having inserted a newline.
7574 (Note: We depend on the fact we won't be crossing the gap.)
7575 Check to see if the most recent message looks a lot like the previous one.
7576 Return 0 if different, 1 if the new one should just replace it, or a
7577 value N > 1 if we should also append " [N times]". */
7578
7579 static int
7580 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7581 int prev_bol, this_bol;
7582 int prev_bol_byte, this_bol_byte;
7583 {
7584 int i;
7585 int len = Z_BYTE - 1 - this_bol_byte;
7586 int seen_dots = 0;
7587 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7588 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7589
7590 for (i = 0; i < len; i++)
7591 {
7592 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7593 seen_dots = 1;
7594 if (p1[i] != p2[i])
7595 return seen_dots;
7596 }
7597 p1 += len;
7598 if (*p1 == '\n')
7599 return 2;
7600 if (*p1++ == ' ' && *p1++ == '[')
7601 {
7602 int n = 0;
7603 while (*p1 >= '0' && *p1 <= '9')
7604 n = n * 10 + *p1++ - '0';
7605 if (strncmp (p1, " times]\n", 8) == 0)
7606 return n+1;
7607 }
7608 return 0;
7609 }
7610 \f
7611
7612 /* Display an echo area message M with a specified length of NBYTES
7613 bytes. The string may include null characters. If M is 0, clear
7614 out any existing message, and let the mini-buffer text show
7615 through.
7616
7617 This may GC, so the buffer M must NOT point to a Lisp string. */
7618
7619 void
7620 message2 (m, nbytes, multibyte)
7621 const char *m;
7622 int nbytes;
7623 int multibyte;
7624 {
7625 /* First flush out any partial line written with print. */
7626 message_log_maybe_newline ();
7627 if (m)
7628 message_dolog (m, nbytes, 1, multibyte);
7629 message2_nolog (m, nbytes, multibyte);
7630 }
7631
7632
7633 /* The non-logging counterpart of message2. */
7634
7635 void
7636 message2_nolog (m, nbytes, multibyte)
7637 const char *m;
7638 int nbytes, multibyte;
7639 {
7640 struct frame *sf = SELECTED_FRAME ();
7641 message_enable_multibyte = multibyte;
7642
7643 if (noninteractive)
7644 {
7645 if (noninteractive_need_newline)
7646 putc ('\n', stderr);
7647 noninteractive_need_newline = 0;
7648 if (m)
7649 fwrite (m, nbytes, 1, stderr);
7650 if (cursor_in_echo_area == 0)
7651 fprintf (stderr, "\n");
7652 fflush (stderr);
7653 }
7654 /* A null message buffer means that the frame hasn't really been
7655 initialized yet. Error messages get reported properly by
7656 cmd_error, so this must be just an informative message; toss it. */
7657 else if (INTERACTIVE
7658 && sf->glyphs_initialized_p
7659 && FRAME_MESSAGE_BUF (sf))
7660 {
7661 Lisp_Object mini_window;
7662 struct frame *f;
7663
7664 /* Get the frame containing the mini-buffer
7665 that the selected frame is using. */
7666 mini_window = FRAME_MINIBUF_WINDOW (sf);
7667 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7668
7669 FRAME_SAMPLE_VISIBILITY (f);
7670 if (FRAME_VISIBLE_P (sf)
7671 && ! FRAME_VISIBLE_P (f))
7672 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7673
7674 if (m)
7675 {
7676 set_message (m, Qnil, nbytes, multibyte);
7677 if (minibuffer_auto_raise)
7678 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7679 }
7680 else
7681 clear_message (1, 1);
7682
7683 do_pending_window_change (0);
7684 echo_area_display (1);
7685 do_pending_window_change (0);
7686 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7687 (*frame_up_to_date_hook) (f);
7688 }
7689 }
7690
7691
7692 /* Display an echo area message M with a specified length of NBYTES
7693 bytes. The string may include null characters. If M is not a
7694 string, clear out any existing message, and let the mini-buffer
7695 text show through.
7696
7697 This function cancels echoing. */
7698
7699 void
7700 message3 (m, nbytes, multibyte)
7701 Lisp_Object m;
7702 int nbytes;
7703 int multibyte;
7704 {
7705 struct gcpro gcpro1;
7706
7707 GCPRO1 (m);
7708 clear_message (1,1);
7709 cancel_echoing ();
7710
7711 /* First flush out any partial line written with print. */
7712 message_log_maybe_newline ();
7713 if (STRINGP (m))
7714 {
7715 char *buffer;
7716 USE_SAFE_ALLOCA;
7717
7718 SAFE_ALLOCA (buffer, char *, nbytes);
7719 bcopy (SDATA (m), buffer, nbytes);
7720 message_dolog (buffer, nbytes, 1, multibyte);
7721 SAFE_FREE ();
7722 }
7723 message3_nolog (m, nbytes, multibyte);
7724
7725 UNGCPRO;
7726 }
7727
7728
7729 /* The non-logging version of message3.
7730 This does not cancel echoing, because it is used for echoing.
7731 Perhaps we need to make a separate function for echoing
7732 and make this cancel echoing. */
7733
7734 void
7735 message3_nolog (m, nbytes, multibyte)
7736 Lisp_Object m;
7737 int nbytes, multibyte;
7738 {
7739 struct frame *sf = SELECTED_FRAME ();
7740 message_enable_multibyte = multibyte;
7741
7742 if (noninteractive)
7743 {
7744 if (noninteractive_need_newline)
7745 putc ('\n', stderr);
7746 noninteractive_need_newline = 0;
7747 if (STRINGP (m))
7748 fwrite (SDATA (m), nbytes, 1, stderr);
7749 if (cursor_in_echo_area == 0)
7750 fprintf (stderr, "\n");
7751 fflush (stderr);
7752 }
7753 /* A null message buffer means that the frame hasn't really been
7754 initialized yet. Error messages get reported properly by
7755 cmd_error, so this must be just an informative message; toss it. */
7756 else if (INTERACTIVE
7757 && sf->glyphs_initialized_p
7758 && FRAME_MESSAGE_BUF (sf))
7759 {
7760 Lisp_Object mini_window;
7761 Lisp_Object frame;
7762 struct frame *f;
7763
7764 /* Get the frame containing the mini-buffer
7765 that the selected frame is using. */
7766 mini_window = FRAME_MINIBUF_WINDOW (sf);
7767 frame = XWINDOW (mini_window)->frame;
7768 f = XFRAME (frame);
7769
7770 FRAME_SAMPLE_VISIBILITY (f);
7771 if (FRAME_VISIBLE_P (sf)
7772 && !FRAME_VISIBLE_P (f))
7773 Fmake_frame_visible (frame);
7774
7775 if (STRINGP (m) && SCHARS (m) > 0)
7776 {
7777 set_message (NULL, m, nbytes, multibyte);
7778 if (minibuffer_auto_raise)
7779 Fraise_frame (frame);
7780 /* Assume we are not echoing.
7781 (If we are, echo_now will override this.) */
7782 echo_message_buffer = Qnil;
7783 }
7784 else
7785 clear_message (1, 1);
7786
7787 do_pending_window_change (0);
7788 echo_area_display (1);
7789 do_pending_window_change (0);
7790 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7791 (*frame_up_to_date_hook) (f);
7792 }
7793 }
7794
7795
7796 /* Display a null-terminated echo area message M. If M is 0, clear
7797 out any existing message, and let the mini-buffer text show through.
7798
7799 The buffer M must continue to exist until after the echo area gets
7800 cleared or some other message gets displayed there. Do not pass
7801 text that is stored in a Lisp string. Do not pass text in a buffer
7802 that was alloca'd. */
7803
7804 void
7805 message1 (m)
7806 char *m;
7807 {
7808 message2 (m, (m ? strlen (m) : 0), 0);
7809 }
7810
7811
7812 /* The non-logging counterpart of message1. */
7813
7814 void
7815 message1_nolog (m)
7816 char *m;
7817 {
7818 message2_nolog (m, (m ? strlen (m) : 0), 0);
7819 }
7820
7821 /* Display a message M which contains a single %s
7822 which gets replaced with STRING. */
7823
7824 void
7825 message_with_string (m, string, log)
7826 char *m;
7827 Lisp_Object string;
7828 int log;
7829 {
7830 CHECK_STRING (string);
7831
7832 if (noninteractive)
7833 {
7834 if (m)
7835 {
7836 if (noninteractive_need_newline)
7837 putc ('\n', stderr);
7838 noninteractive_need_newline = 0;
7839 fprintf (stderr, m, SDATA (string));
7840 if (cursor_in_echo_area == 0)
7841 fprintf (stderr, "\n");
7842 fflush (stderr);
7843 }
7844 }
7845 else if (INTERACTIVE)
7846 {
7847 /* The frame whose minibuffer we're going to display the message on.
7848 It may be larger than the selected frame, so we need
7849 to use its buffer, not the selected frame's buffer. */
7850 Lisp_Object mini_window;
7851 struct frame *f, *sf = SELECTED_FRAME ();
7852
7853 /* Get the frame containing the minibuffer
7854 that the selected frame is using. */
7855 mini_window = FRAME_MINIBUF_WINDOW (sf);
7856 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7857
7858 /* A null message buffer means that the frame hasn't really been
7859 initialized yet. Error messages get reported properly by
7860 cmd_error, so this must be just an informative message; toss it. */
7861 if (FRAME_MESSAGE_BUF (f))
7862 {
7863 Lisp_Object args[2], message;
7864 struct gcpro gcpro1, gcpro2;
7865
7866 args[0] = build_string (m);
7867 args[1] = message = string;
7868 GCPRO2 (args[0], message);
7869 gcpro1.nvars = 2;
7870
7871 message = Fformat (2, args);
7872
7873 if (log)
7874 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7875 else
7876 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7877
7878 UNGCPRO;
7879
7880 /* Print should start at the beginning of the message
7881 buffer next time. */
7882 message_buf_print = 0;
7883 }
7884 }
7885 }
7886
7887
7888 /* Dump an informative message to the minibuf. If M is 0, clear out
7889 any existing message, and let the mini-buffer text show through. */
7890
7891 /* VARARGS 1 */
7892 void
7893 message (m, a1, a2, a3)
7894 char *m;
7895 EMACS_INT a1, a2, a3;
7896 {
7897 if (noninteractive)
7898 {
7899 if (m)
7900 {
7901 if (noninteractive_need_newline)
7902 putc ('\n', stderr);
7903 noninteractive_need_newline = 0;
7904 fprintf (stderr, m, a1, a2, a3);
7905 if (cursor_in_echo_area == 0)
7906 fprintf (stderr, "\n");
7907 fflush (stderr);
7908 }
7909 }
7910 else if (INTERACTIVE)
7911 {
7912 /* The frame whose mini-buffer we're going to display the message
7913 on. It may be larger than the selected frame, so we need to
7914 use its buffer, not the selected frame's buffer. */
7915 Lisp_Object mini_window;
7916 struct frame *f, *sf = SELECTED_FRAME ();
7917
7918 /* Get the frame containing the mini-buffer
7919 that the selected frame is using. */
7920 mini_window = FRAME_MINIBUF_WINDOW (sf);
7921 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7922
7923 /* A null message buffer means that the frame hasn't really been
7924 initialized yet. Error messages get reported properly by
7925 cmd_error, so this must be just an informative message; toss
7926 it. */
7927 if (FRAME_MESSAGE_BUF (f))
7928 {
7929 if (m)
7930 {
7931 int len;
7932 #ifdef NO_ARG_ARRAY
7933 char *a[3];
7934 a[0] = (char *) a1;
7935 a[1] = (char *) a2;
7936 a[2] = (char *) a3;
7937
7938 len = doprnt (FRAME_MESSAGE_BUF (f),
7939 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7940 #else
7941 len = doprnt (FRAME_MESSAGE_BUF (f),
7942 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7943 (char **) &a1);
7944 #endif /* NO_ARG_ARRAY */
7945
7946 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7947 }
7948 else
7949 message1 (0);
7950
7951 /* Print should start at the beginning of the message
7952 buffer next time. */
7953 message_buf_print = 0;
7954 }
7955 }
7956 }
7957
7958
7959 /* The non-logging version of message. */
7960
7961 void
7962 message_nolog (m, a1, a2, a3)
7963 char *m;
7964 EMACS_INT a1, a2, a3;
7965 {
7966 Lisp_Object old_log_max;
7967 old_log_max = Vmessage_log_max;
7968 Vmessage_log_max = Qnil;
7969 message (m, a1, a2, a3);
7970 Vmessage_log_max = old_log_max;
7971 }
7972
7973
7974 /* Display the current message in the current mini-buffer. This is
7975 only called from error handlers in process.c, and is not time
7976 critical. */
7977
7978 void
7979 update_echo_area ()
7980 {
7981 if (!NILP (echo_area_buffer[0]))
7982 {
7983 Lisp_Object string;
7984 string = Fcurrent_message ();
7985 message3 (string, SBYTES (string),
7986 !NILP (current_buffer->enable_multibyte_characters));
7987 }
7988 }
7989
7990
7991 /* Make sure echo area buffers in `echo_buffers' are live.
7992 If they aren't, make new ones. */
7993
7994 static void
7995 ensure_echo_area_buffers ()
7996 {
7997 int i;
7998
7999 for (i = 0; i < 2; ++i)
8000 if (!BUFFERP (echo_buffer[i])
8001 || NILP (XBUFFER (echo_buffer[i])->name))
8002 {
8003 char name[30];
8004 Lisp_Object old_buffer;
8005 int j;
8006
8007 old_buffer = echo_buffer[i];
8008 sprintf (name, " *Echo Area %d*", i);
8009 echo_buffer[i] = Fget_buffer_create (build_string (name));
8010 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8011
8012 for (j = 0; j < 2; ++j)
8013 if (EQ (old_buffer, echo_area_buffer[j]))
8014 echo_area_buffer[j] = echo_buffer[i];
8015 }
8016 }
8017
8018
8019 /* Call FN with args A1..A4 with either the current or last displayed
8020 echo_area_buffer as current buffer.
8021
8022 WHICH zero means use the current message buffer
8023 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8024 from echo_buffer[] and clear it.
8025
8026 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8027 suitable buffer from echo_buffer[] and clear it.
8028
8029 Value is what FN returns. */
8030
8031 static int
8032 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8033 struct window *w;
8034 int which;
8035 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8036 EMACS_INT a1;
8037 Lisp_Object a2;
8038 EMACS_INT a3, a4;
8039 {
8040 Lisp_Object buffer;
8041 int this_one, the_other, clear_buffer_p, rc;
8042 int count = SPECPDL_INDEX ();
8043
8044 /* If buffers aren't live, make new ones. */
8045 ensure_echo_area_buffers ();
8046
8047 clear_buffer_p = 0;
8048
8049 if (which == 0)
8050 this_one = 0, the_other = 1;
8051 else if (which > 0)
8052 this_one = 1, the_other = 0;
8053
8054 /* Choose a suitable buffer from echo_buffer[] is we don't
8055 have one. */
8056 if (NILP (echo_area_buffer[this_one]))
8057 {
8058 echo_area_buffer[this_one]
8059 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8060 ? echo_buffer[the_other]
8061 : echo_buffer[this_one]);
8062 clear_buffer_p = 1;
8063 }
8064
8065 buffer = echo_area_buffer[this_one];
8066
8067 /* Don't get confused by reusing the buffer used for echoing
8068 for a different purpose. */
8069 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8070 cancel_echoing ();
8071
8072 record_unwind_protect (unwind_with_echo_area_buffer,
8073 with_echo_area_buffer_unwind_data (w));
8074
8075 /* Make the echo area buffer current. Note that for display
8076 purposes, it is not necessary that the displayed window's buffer
8077 == current_buffer, except for text property lookup. So, let's
8078 only set that buffer temporarily here without doing a full
8079 Fset_window_buffer. We must also change w->pointm, though,
8080 because otherwise an assertions in unshow_buffer fails, and Emacs
8081 aborts. */
8082 set_buffer_internal_1 (XBUFFER (buffer));
8083 if (w)
8084 {
8085 w->buffer = buffer;
8086 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8087 }
8088
8089 current_buffer->undo_list = Qt;
8090 current_buffer->read_only = Qnil;
8091 specbind (Qinhibit_read_only, Qt);
8092 specbind (Qinhibit_modification_hooks, Qt);
8093
8094 if (clear_buffer_p && Z > BEG)
8095 del_range (BEG, Z);
8096
8097 xassert (BEGV >= BEG);
8098 xassert (ZV <= Z && ZV >= BEGV);
8099
8100 rc = fn (a1, a2, a3, a4);
8101
8102 xassert (BEGV >= BEG);
8103 xassert (ZV <= Z && ZV >= BEGV);
8104
8105 unbind_to (count, Qnil);
8106 return rc;
8107 }
8108
8109
8110 /* Save state that should be preserved around the call to the function
8111 FN called in with_echo_area_buffer. */
8112
8113 static Lisp_Object
8114 with_echo_area_buffer_unwind_data (w)
8115 struct window *w;
8116 {
8117 int i = 0;
8118 Lisp_Object vector;
8119
8120 /* Reduce consing by keeping one vector in
8121 Vwith_echo_area_save_vector. */
8122 vector = Vwith_echo_area_save_vector;
8123 Vwith_echo_area_save_vector = Qnil;
8124
8125 if (NILP (vector))
8126 vector = Fmake_vector (make_number (7), Qnil);
8127
8128 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8129 AREF (vector, i) = Vdeactivate_mark, ++i;
8130 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8131
8132 if (w)
8133 {
8134 XSETWINDOW (AREF (vector, i), w); ++i;
8135 AREF (vector, i) = w->buffer; ++i;
8136 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8137 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8138 }
8139 else
8140 {
8141 int end = i + 4;
8142 for (; i < end; ++i)
8143 AREF (vector, i) = Qnil;
8144 }
8145
8146 xassert (i == ASIZE (vector));
8147 return vector;
8148 }
8149
8150
8151 /* Restore global state from VECTOR which was created by
8152 with_echo_area_buffer_unwind_data. */
8153
8154 static Lisp_Object
8155 unwind_with_echo_area_buffer (vector)
8156 Lisp_Object vector;
8157 {
8158 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8159 Vdeactivate_mark = AREF (vector, 1);
8160 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8161
8162 if (WINDOWP (AREF (vector, 3)))
8163 {
8164 struct window *w;
8165 Lisp_Object buffer, charpos, bytepos;
8166
8167 w = XWINDOW (AREF (vector, 3));
8168 buffer = AREF (vector, 4);
8169 charpos = AREF (vector, 5);
8170 bytepos = AREF (vector, 6);
8171
8172 w->buffer = buffer;
8173 set_marker_both (w->pointm, buffer,
8174 XFASTINT (charpos), XFASTINT (bytepos));
8175 }
8176
8177 Vwith_echo_area_save_vector = vector;
8178 return Qnil;
8179 }
8180
8181
8182 /* Set up the echo area for use by print functions. MULTIBYTE_P
8183 non-zero means we will print multibyte. */
8184
8185 void
8186 setup_echo_area_for_printing (multibyte_p)
8187 int multibyte_p;
8188 {
8189 /* If we can't find an echo area any more, exit. */
8190 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8191 Fkill_emacs (Qnil);
8192
8193 ensure_echo_area_buffers ();
8194
8195 if (!message_buf_print)
8196 {
8197 /* A message has been output since the last time we printed.
8198 Choose a fresh echo area buffer. */
8199 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8200 echo_area_buffer[0] = echo_buffer[1];
8201 else
8202 echo_area_buffer[0] = echo_buffer[0];
8203
8204 /* Switch to that buffer and clear it. */
8205 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8206 current_buffer->truncate_lines = Qnil;
8207
8208 if (Z > BEG)
8209 {
8210 int count = SPECPDL_INDEX ();
8211 specbind (Qinhibit_read_only, Qt);
8212 /* Note that undo recording is always disabled. */
8213 del_range (BEG, Z);
8214 unbind_to (count, Qnil);
8215 }
8216 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8217
8218 /* Set up the buffer for the multibyteness we need. */
8219 if (multibyte_p
8220 != !NILP (current_buffer->enable_multibyte_characters))
8221 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8222
8223 /* Raise the frame containing the echo area. */
8224 if (minibuffer_auto_raise)
8225 {
8226 struct frame *sf = SELECTED_FRAME ();
8227 Lisp_Object mini_window;
8228 mini_window = FRAME_MINIBUF_WINDOW (sf);
8229 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8230 }
8231
8232 message_log_maybe_newline ();
8233 message_buf_print = 1;
8234 }
8235 else
8236 {
8237 if (NILP (echo_area_buffer[0]))
8238 {
8239 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8240 echo_area_buffer[0] = echo_buffer[1];
8241 else
8242 echo_area_buffer[0] = echo_buffer[0];
8243 }
8244
8245 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8246 {
8247 /* Someone switched buffers between print requests. */
8248 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8249 current_buffer->truncate_lines = Qnil;
8250 }
8251 }
8252 }
8253
8254
8255 /* Display an echo area message in window W. Value is non-zero if W's
8256 height is changed. If display_last_displayed_message_p is
8257 non-zero, display the message that was last displayed, otherwise
8258 display the current message. */
8259
8260 static int
8261 display_echo_area (w)
8262 struct window *w;
8263 {
8264 int i, no_message_p, window_height_changed_p, count;
8265
8266 /* Temporarily disable garbage collections while displaying the echo
8267 area. This is done because a GC can print a message itself.
8268 That message would modify the echo area buffer's contents while a
8269 redisplay of the buffer is going on, and seriously confuse
8270 redisplay. */
8271 count = inhibit_garbage_collection ();
8272
8273 /* If there is no message, we must call display_echo_area_1
8274 nevertheless because it resizes the window. But we will have to
8275 reset the echo_area_buffer in question to nil at the end because
8276 with_echo_area_buffer will sets it to an empty buffer. */
8277 i = display_last_displayed_message_p ? 1 : 0;
8278 no_message_p = NILP (echo_area_buffer[i]);
8279
8280 window_height_changed_p
8281 = with_echo_area_buffer (w, display_last_displayed_message_p,
8282 display_echo_area_1,
8283 (EMACS_INT) w, Qnil, 0, 0);
8284
8285 if (no_message_p)
8286 echo_area_buffer[i] = Qnil;
8287
8288 unbind_to (count, Qnil);
8289 return window_height_changed_p;
8290 }
8291
8292
8293 /* Helper for display_echo_area. Display the current buffer which
8294 contains the current echo area message in window W, a mini-window,
8295 a pointer to which is passed in A1. A2..A4 are currently not used.
8296 Change the height of W so that all of the message is displayed.
8297 Value is non-zero if height of W was changed. */
8298
8299 static int
8300 display_echo_area_1 (a1, a2, a3, a4)
8301 EMACS_INT a1;
8302 Lisp_Object a2;
8303 EMACS_INT a3, a4;
8304 {
8305 struct window *w = (struct window *) a1;
8306 Lisp_Object window;
8307 struct text_pos start;
8308 int window_height_changed_p = 0;
8309
8310 /* Do this before displaying, so that we have a large enough glyph
8311 matrix for the display. If we can't get enough space for the
8312 whole text, display the last N lines. That works by setting w->start. */
8313 window_height_changed_p = resize_mini_window (w, 0);
8314
8315 /* Use the starting position chosen by resize_mini_window. */
8316 SET_TEXT_POS_FROM_MARKER (start, w->start);
8317
8318 /* Display. */
8319 clear_glyph_matrix (w->desired_matrix);
8320 XSETWINDOW (window, w);
8321 try_window (window, start, 0);
8322
8323 return window_height_changed_p;
8324 }
8325
8326
8327 /* Resize the echo area window to exactly the size needed for the
8328 currently displayed message, if there is one. If a mini-buffer
8329 is active, don't shrink it. */
8330
8331 void
8332 resize_echo_area_exactly ()
8333 {
8334 if (BUFFERP (echo_area_buffer[0])
8335 && WINDOWP (echo_area_window))
8336 {
8337 struct window *w = XWINDOW (echo_area_window);
8338 int resized_p;
8339 Lisp_Object resize_exactly;
8340
8341 if (minibuf_level == 0)
8342 resize_exactly = Qt;
8343 else
8344 resize_exactly = Qnil;
8345
8346 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8347 (EMACS_INT) w, resize_exactly, 0, 0);
8348 if (resized_p)
8349 {
8350 ++windows_or_buffers_changed;
8351 ++update_mode_lines;
8352 redisplay_internal (0);
8353 }
8354 }
8355 }
8356
8357
8358 /* Callback function for with_echo_area_buffer, when used from
8359 resize_echo_area_exactly. A1 contains a pointer to the window to
8360 resize, EXACTLY non-nil means resize the mini-window exactly to the
8361 size of the text displayed. A3 and A4 are not used. Value is what
8362 resize_mini_window returns. */
8363
8364 static int
8365 resize_mini_window_1 (a1, exactly, a3, a4)
8366 EMACS_INT a1;
8367 Lisp_Object exactly;
8368 EMACS_INT a3, a4;
8369 {
8370 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8371 }
8372
8373
8374 /* Resize mini-window W to fit the size of its contents. EXACT:P
8375 means size the window exactly to the size needed. Otherwise, it's
8376 only enlarged until W's buffer is empty.
8377
8378 Set W->start to the right place to begin display. If the whole
8379 contents fit, start at the beginning. Otherwise, start so as
8380 to make the end of the contents appear. This is particularly
8381 important for y-or-n-p, but seems desirable generally.
8382
8383 Value is non-zero if the window height has been changed. */
8384
8385 int
8386 resize_mini_window (w, exact_p)
8387 struct window *w;
8388 int exact_p;
8389 {
8390 struct frame *f = XFRAME (w->frame);
8391 int window_height_changed_p = 0;
8392
8393 xassert (MINI_WINDOW_P (w));
8394
8395 /* By default, start display at the beginning. */
8396 set_marker_both (w->start, w->buffer,
8397 BUF_BEGV (XBUFFER (w->buffer)),
8398 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8399
8400 /* Don't resize windows while redisplaying a window; it would
8401 confuse redisplay functions when the size of the window they are
8402 displaying changes from under them. Such a resizing can happen,
8403 for instance, when which-func prints a long message while
8404 we are running fontification-functions. We're running these
8405 functions with safe_call which binds inhibit-redisplay to t. */
8406 if (!NILP (Vinhibit_redisplay))
8407 return 0;
8408
8409 /* Nil means don't try to resize. */
8410 if (NILP (Vresize_mini_windows)
8411 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8412 return 0;
8413
8414 if (!FRAME_MINIBUF_ONLY_P (f))
8415 {
8416 struct it it;
8417 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8418 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8419 int height, max_height;
8420 int unit = FRAME_LINE_HEIGHT (f);
8421 struct text_pos start;
8422 struct buffer *old_current_buffer = NULL;
8423
8424 if (current_buffer != XBUFFER (w->buffer))
8425 {
8426 old_current_buffer = current_buffer;
8427 set_buffer_internal (XBUFFER (w->buffer));
8428 }
8429
8430 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8431
8432 /* Compute the max. number of lines specified by the user. */
8433 if (FLOATP (Vmax_mini_window_height))
8434 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8435 else if (INTEGERP (Vmax_mini_window_height))
8436 max_height = XINT (Vmax_mini_window_height);
8437 else
8438 max_height = total_height / 4;
8439
8440 /* Correct that max. height if it's bogus. */
8441 max_height = max (1, max_height);
8442 max_height = min (total_height, max_height);
8443
8444 /* Find out the height of the text in the window. */
8445 if (it.truncate_lines_p)
8446 height = 1;
8447 else
8448 {
8449 last_height = 0;
8450 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8451 if (it.max_ascent == 0 && it.max_descent == 0)
8452 height = it.current_y + last_height;
8453 else
8454 height = it.current_y + it.max_ascent + it.max_descent;
8455 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8456 height = (height + unit - 1) / unit;
8457 }
8458
8459 /* Compute a suitable window start. */
8460 if (height > max_height)
8461 {
8462 height = max_height;
8463 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8464 move_it_vertically_backward (&it, (height - 1) * unit);
8465 start = it.current.pos;
8466 }
8467 else
8468 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8469 SET_MARKER_FROM_TEXT_POS (w->start, start);
8470
8471 if (EQ (Vresize_mini_windows, Qgrow_only))
8472 {
8473 /* Let it grow only, until we display an empty message, in which
8474 case the window shrinks again. */
8475 if (height > WINDOW_TOTAL_LINES (w))
8476 {
8477 int old_height = WINDOW_TOTAL_LINES (w);
8478 freeze_window_starts (f, 1);
8479 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8480 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8481 }
8482 else if (height < WINDOW_TOTAL_LINES (w)
8483 && (exact_p || BEGV == ZV))
8484 {
8485 int old_height = WINDOW_TOTAL_LINES (w);
8486 freeze_window_starts (f, 0);
8487 shrink_mini_window (w);
8488 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8489 }
8490 }
8491 else
8492 {
8493 /* Always resize to exact size needed. */
8494 if (height > WINDOW_TOTAL_LINES (w))
8495 {
8496 int old_height = WINDOW_TOTAL_LINES (w);
8497 freeze_window_starts (f, 1);
8498 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8499 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8500 }
8501 else if (height < WINDOW_TOTAL_LINES (w))
8502 {
8503 int old_height = WINDOW_TOTAL_LINES (w);
8504 freeze_window_starts (f, 0);
8505 shrink_mini_window (w);
8506
8507 if (height)
8508 {
8509 freeze_window_starts (f, 1);
8510 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8511 }
8512
8513 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8514 }
8515 }
8516
8517 if (old_current_buffer)
8518 set_buffer_internal (old_current_buffer);
8519 }
8520
8521 return window_height_changed_p;
8522 }
8523
8524
8525 /* Value is the current message, a string, or nil if there is no
8526 current message. */
8527
8528 Lisp_Object
8529 current_message ()
8530 {
8531 Lisp_Object msg;
8532
8533 if (NILP (echo_area_buffer[0]))
8534 msg = Qnil;
8535 else
8536 {
8537 with_echo_area_buffer (0, 0, current_message_1,
8538 (EMACS_INT) &msg, Qnil, 0, 0);
8539 if (NILP (msg))
8540 echo_area_buffer[0] = Qnil;
8541 }
8542
8543 return msg;
8544 }
8545
8546
8547 static int
8548 current_message_1 (a1, a2, a3, a4)
8549 EMACS_INT a1;
8550 Lisp_Object a2;
8551 EMACS_INT a3, a4;
8552 {
8553 Lisp_Object *msg = (Lisp_Object *) a1;
8554
8555 if (Z > BEG)
8556 *msg = make_buffer_string (BEG, Z, 1);
8557 else
8558 *msg = Qnil;
8559 return 0;
8560 }
8561
8562
8563 /* Push the current message on Vmessage_stack for later restauration
8564 by restore_message. Value is non-zero if the current message isn't
8565 empty. This is a relatively infrequent operation, so it's not
8566 worth optimizing. */
8567
8568 int
8569 push_message ()
8570 {
8571 Lisp_Object msg;
8572 msg = current_message ();
8573 Vmessage_stack = Fcons (msg, Vmessage_stack);
8574 return STRINGP (msg);
8575 }
8576
8577
8578 /* Restore message display from the top of Vmessage_stack. */
8579
8580 void
8581 restore_message ()
8582 {
8583 Lisp_Object msg;
8584
8585 xassert (CONSP (Vmessage_stack));
8586 msg = XCAR (Vmessage_stack);
8587 if (STRINGP (msg))
8588 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8589 else
8590 message3_nolog (msg, 0, 0);
8591 }
8592
8593
8594 /* Handler for record_unwind_protect calling pop_message. */
8595
8596 Lisp_Object
8597 pop_message_unwind (dummy)
8598 Lisp_Object dummy;
8599 {
8600 pop_message ();
8601 return Qnil;
8602 }
8603
8604 /* Pop the top-most entry off Vmessage_stack. */
8605
8606 void
8607 pop_message ()
8608 {
8609 xassert (CONSP (Vmessage_stack));
8610 Vmessage_stack = XCDR (Vmessage_stack);
8611 }
8612
8613
8614 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8615 exits. If the stack is not empty, we have a missing pop_message
8616 somewhere. */
8617
8618 void
8619 check_message_stack ()
8620 {
8621 if (!NILP (Vmessage_stack))
8622 abort ();
8623 }
8624
8625
8626 /* Truncate to NCHARS what will be displayed in the echo area the next
8627 time we display it---but don't redisplay it now. */
8628
8629 void
8630 truncate_echo_area (nchars)
8631 int nchars;
8632 {
8633 if (nchars == 0)
8634 echo_area_buffer[0] = Qnil;
8635 /* A null message buffer means that the frame hasn't really been
8636 initialized yet. Error messages get reported properly by
8637 cmd_error, so this must be just an informative message; toss it. */
8638 else if (!noninteractive
8639 && INTERACTIVE
8640 && !NILP (echo_area_buffer[0]))
8641 {
8642 struct frame *sf = SELECTED_FRAME ();
8643 if (FRAME_MESSAGE_BUF (sf))
8644 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8645 }
8646 }
8647
8648
8649 /* Helper function for truncate_echo_area. Truncate the current
8650 message to at most NCHARS characters. */
8651
8652 static int
8653 truncate_message_1 (nchars, a2, a3, a4)
8654 EMACS_INT nchars;
8655 Lisp_Object a2;
8656 EMACS_INT a3, a4;
8657 {
8658 if (BEG + nchars < Z)
8659 del_range (BEG + nchars, Z);
8660 if (Z == BEG)
8661 echo_area_buffer[0] = Qnil;
8662 return 0;
8663 }
8664
8665
8666 /* Set the current message to a substring of S or STRING.
8667
8668 If STRING is a Lisp string, set the message to the first NBYTES
8669 bytes from STRING. NBYTES zero means use the whole string. If
8670 STRING is multibyte, the message will be displayed multibyte.
8671
8672 If S is not null, set the message to the first LEN bytes of S. LEN
8673 zero means use the whole string. MULTIBYTE_P non-zero means S is
8674 multibyte. Display the message multibyte in that case.
8675
8676 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8677 to t before calling set_message_1 (which calls insert).
8678 */
8679
8680 void
8681 set_message (s, string, nbytes, multibyte_p)
8682 const char *s;
8683 Lisp_Object string;
8684 int nbytes, multibyte_p;
8685 {
8686 message_enable_multibyte
8687 = ((s && multibyte_p)
8688 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8689
8690 with_echo_area_buffer (0, 0, set_message_1,
8691 (EMACS_INT) s, string, nbytes, multibyte_p);
8692 message_buf_print = 0;
8693 help_echo_showing_p = 0;
8694 }
8695
8696
8697 /* Helper function for set_message. Arguments have the same meaning
8698 as there, with A1 corresponding to S and A2 corresponding to STRING
8699 This function is called with the echo area buffer being
8700 current. */
8701
8702 static int
8703 set_message_1 (a1, a2, nbytes, multibyte_p)
8704 EMACS_INT a1;
8705 Lisp_Object a2;
8706 EMACS_INT nbytes, multibyte_p;
8707 {
8708 const char *s = (const char *) a1;
8709 Lisp_Object string = a2;
8710
8711 /* Change multibyteness of the echo buffer appropriately. */
8712 if (message_enable_multibyte
8713 != !NILP (current_buffer->enable_multibyte_characters))
8714 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8715
8716 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8717
8718 /* Insert new message at BEG. */
8719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8720 Ferase_buffer ();
8721
8722 if (STRINGP (string))
8723 {
8724 int nchars;
8725
8726 if (nbytes == 0)
8727 nbytes = SBYTES (string);
8728 nchars = string_byte_to_char (string, nbytes);
8729
8730 /* This function takes care of single/multibyte conversion. We
8731 just have to ensure that the echo area buffer has the right
8732 setting of enable_multibyte_characters. */
8733 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8734 }
8735 else if (s)
8736 {
8737 if (nbytes == 0)
8738 nbytes = strlen (s);
8739
8740 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8741 {
8742 /* Convert from multi-byte to single-byte. */
8743 int i, c, n;
8744 unsigned char work[1];
8745
8746 /* Convert a multibyte string to single-byte. */
8747 for (i = 0; i < nbytes; i += n)
8748 {
8749 c = string_char_and_length (s + i, nbytes - i, &n);
8750 work[0] = (ASCII_CHAR_P (c)
8751 ? c
8752 : multibyte_char_to_unibyte (c, Qnil));
8753 insert_1_both (work, 1, 1, 1, 0, 0);
8754 }
8755 }
8756 else if (!multibyte_p
8757 && !NILP (current_buffer->enable_multibyte_characters))
8758 {
8759 /* Convert from single-byte to multi-byte. */
8760 int i, c, n;
8761 const unsigned char *msg = (const unsigned char *) s;
8762 unsigned char str[MAX_MULTIBYTE_LENGTH];
8763
8764 /* Convert a single-byte string to multibyte. */
8765 for (i = 0; i < nbytes; i++)
8766 {
8767 c = msg[i];
8768 c = unibyte_char_to_multibyte (c);
8769 n = CHAR_STRING (c, str);
8770 insert_1_both (str, 1, n, 1, 0, 0);
8771 }
8772 }
8773 else
8774 insert_1 (s, nbytes, 1, 0, 0);
8775 }
8776
8777 return 0;
8778 }
8779
8780
8781 /* Clear messages. CURRENT_P non-zero means clear the current
8782 message. LAST_DISPLAYED_P non-zero means clear the message
8783 last displayed. */
8784
8785 void
8786 clear_message (current_p, last_displayed_p)
8787 int current_p, last_displayed_p;
8788 {
8789 if (current_p)
8790 {
8791 echo_area_buffer[0] = Qnil;
8792 message_cleared_p = 1;
8793 }
8794
8795 if (last_displayed_p)
8796 echo_area_buffer[1] = Qnil;
8797
8798 message_buf_print = 0;
8799 }
8800
8801 /* Clear garbaged frames.
8802
8803 This function is used where the old redisplay called
8804 redraw_garbaged_frames which in turn called redraw_frame which in
8805 turn called clear_frame. The call to clear_frame was a source of
8806 flickering. I believe a clear_frame is not necessary. It should
8807 suffice in the new redisplay to invalidate all current matrices,
8808 and ensure a complete redisplay of all windows. */
8809
8810 static void
8811 clear_garbaged_frames ()
8812 {
8813 if (frame_garbaged)
8814 {
8815 Lisp_Object tail, frame;
8816 int changed_count = 0;
8817
8818 FOR_EACH_FRAME (tail, frame)
8819 {
8820 struct frame *f = XFRAME (frame);
8821
8822 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8823 {
8824 if (f->resized_p)
8825 {
8826 Fredraw_frame (frame);
8827 f->force_flush_display_p = 1;
8828 }
8829 clear_current_matrices (f);
8830 changed_count++;
8831 f->garbaged = 0;
8832 f->resized_p = 0;
8833 }
8834 }
8835
8836 frame_garbaged = 0;
8837 if (changed_count)
8838 ++windows_or_buffers_changed;
8839 }
8840 }
8841
8842
8843 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8844 is non-zero update selected_frame. Value is non-zero if the
8845 mini-windows height has been changed. */
8846
8847 static int
8848 echo_area_display (update_frame_p)
8849 int update_frame_p;
8850 {
8851 Lisp_Object mini_window;
8852 struct window *w;
8853 struct frame *f;
8854 int window_height_changed_p = 0;
8855 struct frame *sf = SELECTED_FRAME ();
8856
8857 mini_window = FRAME_MINIBUF_WINDOW (sf);
8858 w = XWINDOW (mini_window);
8859 f = XFRAME (WINDOW_FRAME (w));
8860
8861 /* Don't display if frame is invisible or not yet initialized. */
8862 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8863 return 0;
8864
8865 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8866 #ifndef MAC_OS8
8867 #ifdef HAVE_WINDOW_SYSTEM
8868 /* When Emacs starts, selected_frame may be a visible terminal
8869 frame, even if we run under a window system. If we let this
8870 through, a message would be displayed on the terminal. */
8871 if (EQ (selected_frame, Vterminal_frame)
8872 && !NILP (Vwindow_system))
8873 return 0;
8874 #endif /* HAVE_WINDOW_SYSTEM */
8875 #endif
8876
8877 /* Redraw garbaged frames. */
8878 if (frame_garbaged)
8879 clear_garbaged_frames ();
8880
8881 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8882 {
8883 echo_area_window = mini_window;
8884 window_height_changed_p = display_echo_area (w);
8885 w->must_be_updated_p = 1;
8886
8887 /* Update the display, unless called from redisplay_internal.
8888 Also don't update the screen during redisplay itself. The
8889 update will happen at the end of redisplay, and an update
8890 here could cause confusion. */
8891 if (update_frame_p && !redisplaying_p)
8892 {
8893 int n = 0;
8894
8895 /* If the display update has been interrupted by pending
8896 input, update mode lines in the frame. Due to the
8897 pending input, it might have been that redisplay hasn't
8898 been called, so that mode lines above the echo area are
8899 garbaged. This looks odd, so we prevent it here. */
8900 if (!display_completed)
8901 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8902
8903 if (window_height_changed_p
8904 /* Don't do this if Emacs is shutting down. Redisplay
8905 needs to run hooks. */
8906 && !NILP (Vrun_hooks))
8907 {
8908 /* Must update other windows. Likewise as in other
8909 cases, don't let this update be interrupted by
8910 pending input. */
8911 int count = SPECPDL_INDEX ();
8912 specbind (Qredisplay_dont_pause, Qt);
8913 windows_or_buffers_changed = 1;
8914 redisplay_internal (0);
8915 unbind_to (count, Qnil);
8916 }
8917 else if (FRAME_WINDOW_P (f) && n == 0)
8918 {
8919 /* Window configuration is the same as before.
8920 Can do with a display update of the echo area,
8921 unless we displayed some mode lines. */
8922 update_single_window (w, 1);
8923 rif->flush_display (f);
8924 }
8925 else
8926 update_frame (f, 1, 1);
8927
8928 /* If cursor is in the echo area, make sure that the next
8929 redisplay displays the minibuffer, so that the cursor will
8930 be replaced with what the minibuffer wants. */
8931 if (cursor_in_echo_area)
8932 ++windows_or_buffers_changed;
8933 }
8934 }
8935 else if (!EQ (mini_window, selected_window))
8936 windows_or_buffers_changed++;
8937
8938 /* The current message is now also the last one displayed. */
8939 echo_area_buffer[1] = echo_area_buffer[0];
8940
8941 /* Prevent redisplay optimization in redisplay_internal by resetting
8942 this_line_start_pos. This is done because the mini-buffer now
8943 displays the message instead of its buffer text. */
8944 if (EQ (mini_window, selected_window))
8945 CHARPOS (this_line_start_pos) = 0;
8946
8947 return window_height_changed_p;
8948 }
8949
8950
8951 \f
8952 /***********************************************************************
8953 Mode Lines and Frame Titles
8954 ***********************************************************************/
8955
8956 /* A buffer for constructing non-propertized mode-line strings and
8957 frame titles in it; allocated from the heap in init_xdisp and
8958 resized as needed in store_mode_line_noprop_char. */
8959
8960 static char *mode_line_noprop_buf;
8961
8962 /* The buffer's end, and a current output position in it. */
8963
8964 static char *mode_line_noprop_buf_end;
8965 static char *mode_line_noprop_ptr;
8966
8967 #define MODE_LINE_NOPROP_LEN(start) \
8968 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8969
8970 static enum {
8971 MODE_LINE_DISPLAY = 0,
8972 MODE_LINE_TITLE,
8973 MODE_LINE_NOPROP,
8974 MODE_LINE_STRING
8975 } mode_line_target;
8976
8977 /* Alist that caches the results of :propertize.
8978 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8979 static Lisp_Object mode_line_proptrans_alist;
8980
8981 /* List of strings making up the mode-line. */
8982 static Lisp_Object mode_line_string_list;
8983
8984 /* Base face property when building propertized mode line string. */
8985 static Lisp_Object mode_line_string_face;
8986 static Lisp_Object mode_line_string_face_prop;
8987
8988
8989 /* Unwind data for mode line strings */
8990
8991 static Lisp_Object Vmode_line_unwind_vector;
8992
8993 static Lisp_Object
8994 format_mode_line_unwind_data (obuf, save_proptrans)
8995 struct buffer *obuf;
8996 {
8997 Lisp_Object vector;
8998
8999 /* Reduce consing by keeping one vector in
9000 Vwith_echo_area_save_vector. */
9001 vector = Vmode_line_unwind_vector;
9002 Vmode_line_unwind_vector = Qnil;
9003
9004 if (NILP (vector))
9005 vector = Fmake_vector (make_number (7), Qnil);
9006
9007 AREF (vector, 0) = make_number (mode_line_target);
9008 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
9009 AREF (vector, 2) = mode_line_string_list;
9010 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
9011 AREF (vector, 4) = mode_line_string_face;
9012 AREF (vector, 5) = mode_line_string_face_prop;
9013
9014 if (obuf)
9015 XSETBUFFER (AREF (vector, 6), obuf);
9016 else
9017 AREF (vector, 6) = Qnil;
9018
9019 return vector;
9020 }
9021
9022 static Lisp_Object
9023 unwind_format_mode_line (vector)
9024 Lisp_Object vector;
9025 {
9026 mode_line_target = XINT (AREF (vector, 0));
9027 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9028 mode_line_string_list = AREF (vector, 2);
9029 if (! EQ (AREF (vector, 3), Qt))
9030 mode_line_proptrans_alist = AREF (vector, 3);
9031 mode_line_string_face = AREF (vector, 4);
9032 mode_line_string_face_prop = AREF (vector, 5);
9033
9034 if (!NILP (AREF (vector, 6)))
9035 {
9036 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9037 AREF (vector, 6) = Qnil;
9038 }
9039
9040 Vmode_line_unwind_vector = vector;
9041 return Qnil;
9042 }
9043
9044
9045 /* Store a single character C for the frame title in mode_line_noprop_buf.
9046 Re-allocate mode_line_noprop_buf if necessary. */
9047
9048 static void
9049 #ifdef PROTOTYPES
9050 store_mode_line_noprop_char (char c)
9051 #else
9052 store_mode_line_noprop_char (c)
9053 char c;
9054 #endif
9055 {
9056 /* If output position has reached the end of the allocated buffer,
9057 double the buffer's size. */
9058 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9059 {
9060 int len = MODE_LINE_NOPROP_LEN (0);
9061 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9062 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9063 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9064 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9065 }
9066
9067 *mode_line_noprop_ptr++ = c;
9068 }
9069
9070
9071 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9072 mode_line_noprop_ptr. STR is the string to store. Do not copy
9073 characters that yield more columns than PRECISION; PRECISION <= 0
9074 means copy the whole string. Pad with spaces until FIELD_WIDTH
9075 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9076 pad. Called from display_mode_element when it is used to build a
9077 frame title. */
9078
9079 static int
9080 store_mode_line_noprop (str, field_width, precision)
9081 const unsigned char *str;
9082 int field_width, precision;
9083 {
9084 int n = 0;
9085 int dummy, nbytes;
9086
9087 /* Copy at most PRECISION chars from STR. */
9088 nbytes = strlen (str);
9089 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9090 while (nbytes--)
9091 store_mode_line_noprop_char (*str++);
9092
9093 /* Fill up with spaces until FIELD_WIDTH reached. */
9094 while (field_width > 0
9095 && n < field_width)
9096 {
9097 store_mode_line_noprop_char (' ');
9098 ++n;
9099 }
9100
9101 return n;
9102 }
9103
9104 /***********************************************************************
9105 Frame Titles
9106 ***********************************************************************/
9107
9108 #ifdef HAVE_WINDOW_SYSTEM
9109
9110 /* Set the title of FRAME, if it has changed. The title format is
9111 Vicon_title_format if FRAME is iconified, otherwise it is
9112 frame_title_format. */
9113
9114 static void
9115 x_consider_frame_title (frame)
9116 Lisp_Object frame;
9117 {
9118 struct frame *f = XFRAME (frame);
9119
9120 if (FRAME_WINDOW_P (f)
9121 || FRAME_MINIBUF_ONLY_P (f)
9122 || f->explicit_name)
9123 {
9124 /* Do we have more than one visible frame on this X display? */
9125 Lisp_Object tail;
9126 Lisp_Object fmt;
9127 int title_start;
9128 char *title;
9129 int len;
9130 struct it it;
9131 int count = SPECPDL_INDEX ();
9132
9133 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9134 {
9135 Lisp_Object other_frame = XCAR (tail);
9136 struct frame *tf = XFRAME (other_frame);
9137
9138 if (tf != f
9139 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9140 && !FRAME_MINIBUF_ONLY_P (tf)
9141 && !EQ (other_frame, tip_frame)
9142 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9143 break;
9144 }
9145
9146 /* Set global variable indicating that multiple frames exist. */
9147 multiple_frames = CONSP (tail);
9148
9149 /* Switch to the buffer of selected window of the frame. Set up
9150 mode_line_target so that display_mode_element will output into
9151 mode_line_noprop_buf; then display the title. */
9152 record_unwind_protect (unwind_format_mode_line,
9153 format_mode_line_unwind_data (current_buffer, 0));
9154
9155 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9156 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9157
9158 mode_line_target = MODE_LINE_TITLE;
9159 title_start = MODE_LINE_NOPROP_LEN (0);
9160 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9161 NULL, DEFAULT_FACE_ID);
9162 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9163 len = MODE_LINE_NOPROP_LEN (title_start);
9164 title = mode_line_noprop_buf + title_start;
9165 unbind_to (count, Qnil);
9166
9167 /* Set the title only if it's changed. This avoids consing in
9168 the common case where it hasn't. (If it turns out that we've
9169 already wasted too much time by walking through the list with
9170 display_mode_element, then we might need to optimize at a
9171 higher level than this.) */
9172 if (! STRINGP (f->name)
9173 || SBYTES (f->name) != len
9174 || bcmp (title, SDATA (f->name), len) != 0)
9175 x_implicitly_set_name (f, make_string (title, len), Qnil);
9176 }
9177 }
9178
9179 #endif /* not HAVE_WINDOW_SYSTEM */
9180
9181
9182
9183 \f
9184 /***********************************************************************
9185 Menu Bars
9186 ***********************************************************************/
9187
9188
9189 /* Prepare for redisplay by updating menu-bar item lists when
9190 appropriate. This can call eval. */
9191
9192 void
9193 prepare_menu_bars ()
9194 {
9195 int all_windows;
9196 struct gcpro gcpro1, gcpro2;
9197 struct frame *f;
9198 Lisp_Object tooltip_frame;
9199
9200 #ifdef HAVE_WINDOW_SYSTEM
9201 tooltip_frame = tip_frame;
9202 #else
9203 tooltip_frame = Qnil;
9204 #endif
9205
9206 /* Update all frame titles based on their buffer names, etc. We do
9207 this before the menu bars so that the buffer-menu will show the
9208 up-to-date frame titles. */
9209 #ifdef HAVE_WINDOW_SYSTEM
9210 if (windows_or_buffers_changed || update_mode_lines)
9211 {
9212 Lisp_Object tail, frame;
9213
9214 FOR_EACH_FRAME (tail, frame)
9215 {
9216 f = XFRAME (frame);
9217 if (!EQ (frame, tooltip_frame)
9218 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9219 x_consider_frame_title (frame);
9220 }
9221 }
9222 #endif /* HAVE_WINDOW_SYSTEM */
9223
9224 /* Update the menu bar item lists, if appropriate. This has to be
9225 done before any actual redisplay or generation of display lines. */
9226 all_windows = (update_mode_lines
9227 || buffer_shared > 1
9228 || windows_or_buffers_changed);
9229 if (all_windows)
9230 {
9231 Lisp_Object tail, frame;
9232 int count = SPECPDL_INDEX ();
9233 /* 1 means that update_menu_bar has run its hooks
9234 so any further calls to update_menu_bar shouldn't do so again. */
9235 int menu_bar_hooks_run = 0;
9236
9237 record_unwind_save_match_data ();
9238
9239 FOR_EACH_FRAME (tail, frame)
9240 {
9241 f = XFRAME (frame);
9242
9243 /* Ignore tooltip frame. */
9244 if (EQ (frame, tooltip_frame))
9245 continue;
9246
9247 /* If a window on this frame changed size, report that to
9248 the user and clear the size-change flag. */
9249 if (FRAME_WINDOW_SIZES_CHANGED (f))
9250 {
9251 Lisp_Object functions;
9252
9253 /* Clear flag first in case we get an error below. */
9254 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9255 functions = Vwindow_size_change_functions;
9256 GCPRO2 (tail, functions);
9257
9258 while (CONSP (functions))
9259 {
9260 call1 (XCAR (functions), frame);
9261 functions = XCDR (functions);
9262 }
9263 UNGCPRO;
9264 }
9265
9266 GCPRO1 (tail);
9267 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9268 #ifdef HAVE_WINDOW_SYSTEM
9269 update_tool_bar (f, 0);
9270 #ifdef MAC_OS
9271 mac_update_title_bar (f, 0);
9272 #endif
9273 #endif
9274 UNGCPRO;
9275 }
9276
9277 unbind_to (count, Qnil);
9278 }
9279 else
9280 {
9281 struct frame *sf = SELECTED_FRAME ();
9282 update_menu_bar (sf, 1, 0);
9283 #ifdef HAVE_WINDOW_SYSTEM
9284 update_tool_bar (sf, 1);
9285 #ifdef MAC_OS
9286 mac_update_title_bar (sf, 1);
9287 #endif
9288 #endif
9289 }
9290
9291 /* Motif needs this. See comment in xmenu.c. Turn it off when
9292 pending_menu_activation is not defined. */
9293 #ifdef USE_X_TOOLKIT
9294 pending_menu_activation = 0;
9295 #endif
9296 }
9297
9298
9299 /* Update the menu bar item list for frame F. This has to be done
9300 before we start to fill in any display lines, because it can call
9301 eval.
9302
9303 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9304
9305 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9306 already ran the menu bar hooks for this redisplay, so there
9307 is no need to run them again. The return value is the
9308 updated value of this flag, to pass to the next call. */
9309
9310 static int
9311 update_menu_bar (f, save_match_data, hooks_run)
9312 struct frame *f;
9313 int save_match_data;
9314 int hooks_run;
9315 {
9316 Lisp_Object window;
9317 register struct window *w;
9318
9319 /* If called recursively during a menu update, do nothing. This can
9320 happen when, for instance, an activate-menubar-hook causes a
9321 redisplay. */
9322 if (inhibit_menubar_update)
9323 return hooks_run;
9324
9325 window = FRAME_SELECTED_WINDOW (f);
9326 w = XWINDOW (window);
9327
9328 #if 0 /* The if statement below this if statement used to include the
9329 condition !NILP (w->update_mode_line), rather than using
9330 update_mode_lines directly, and this if statement may have
9331 been added to make that condition work. Now the if
9332 statement below matches its comment, this isn't needed. */
9333 if (update_mode_lines)
9334 w->update_mode_line = Qt;
9335 #endif
9336
9337 if (FRAME_WINDOW_P (f)
9338 ?
9339 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9340 || defined (USE_GTK)
9341 FRAME_EXTERNAL_MENU_BAR (f)
9342 #else
9343 FRAME_MENU_BAR_LINES (f) > 0
9344 #endif
9345 : FRAME_MENU_BAR_LINES (f) > 0)
9346 {
9347 /* If the user has switched buffers or windows, we need to
9348 recompute to reflect the new bindings. But we'll
9349 recompute when update_mode_lines is set too; that means
9350 that people can use force-mode-line-update to request
9351 that the menu bar be recomputed. The adverse effect on
9352 the rest of the redisplay algorithm is about the same as
9353 windows_or_buffers_changed anyway. */
9354 if (windows_or_buffers_changed
9355 /* This used to test w->update_mode_line, but we believe
9356 there is no need to recompute the menu in that case. */
9357 || update_mode_lines
9358 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9359 < BUF_MODIFF (XBUFFER (w->buffer)))
9360 != !NILP (w->last_had_star))
9361 || ((!NILP (Vtransient_mark_mode)
9362 && !NILP (XBUFFER (w->buffer)->mark_active))
9363 != !NILP (w->region_showing)))
9364 {
9365 struct buffer *prev = current_buffer;
9366 int count = SPECPDL_INDEX ();
9367
9368 specbind (Qinhibit_menubar_update, Qt);
9369
9370 set_buffer_internal_1 (XBUFFER (w->buffer));
9371 if (save_match_data)
9372 record_unwind_save_match_data ();
9373 if (NILP (Voverriding_local_map_menu_flag))
9374 {
9375 specbind (Qoverriding_terminal_local_map, Qnil);
9376 specbind (Qoverriding_local_map, Qnil);
9377 }
9378
9379 if (!hooks_run)
9380 {
9381 /* Run the Lucid hook. */
9382 safe_run_hooks (Qactivate_menubar_hook);
9383
9384 /* If it has changed current-menubar from previous value,
9385 really recompute the menu-bar from the value. */
9386 if (! NILP (Vlucid_menu_bar_dirty_flag))
9387 call0 (Qrecompute_lucid_menubar);
9388
9389 safe_run_hooks (Qmenu_bar_update_hook);
9390
9391 hooks_run = 1;
9392 }
9393
9394 XSETFRAME (Vmenu_updating_frame, f);
9395 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9396
9397 /* Redisplay the menu bar in case we changed it. */
9398 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9399 || defined (USE_GTK)
9400 if (FRAME_WINDOW_P (f))
9401 {
9402 #ifdef MAC_OS
9403 /* All frames on Mac OS share the same menubar. So only
9404 the selected frame should be allowed to set it. */
9405 if (f == SELECTED_FRAME ())
9406 #endif
9407 set_frame_menubar (f, 0, 0);
9408 }
9409 else
9410 /* On a terminal screen, the menu bar is an ordinary screen
9411 line, and this makes it get updated. */
9412 w->update_mode_line = Qt;
9413 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9414 /* In the non-toolkit version, the menu bar is an ordinary screen
9415 line, and this makes it get updated. */
9416 w->update_mode_line = Qt;
9417 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9418
9419 unbind_to (count, Qnil);
9420 set_buffer_internal_1 (prev);
9421 }
9422 }
9423
9424 return hooks_run;
9425 }
9426
9427
9428 \f
9429 /***********************************************************************
9430 Output Cursor
9431 ***********************************************************************/
9432
9433 #ifdef HAVE_WINDOW_SYSTEM
9434
9435 /* EXPORT:
9436 Nominal cursor position -- where to draw output.
9437 HPOS and VPOS are window relative glyph matrix coordinates.
9438 X and Y are window relative pixel coordinates. */
9439
9440 struct cursor_pos output_cursor;
9441
9442
9443 /* EXPORT:
9444 Set the global variable output_cursor to CURSOR. All cursor
9445 positions are relative to updated_window. */
9446
9447 void
9448 set_output_cursor (cursor)
9449 struct cursor_pos *cursor;
9450 {
9451 output_cursor.hpos = cursor->hpos;
9452 output_cursor.vpos = cursor->vpos;
9453 output_cursor.x = cursor->x;
9454 output_cursor.y = cursor->y;
9455 }
9456
9457
9458 /* EXPORT for RIF:
9459 Set a nominal cursor position.
9460
9461 HPOS and VPOS are column/row positions in a window glyph matrix. X
9462 and Y are window text area relative pixel positions.
9463
9464 If this is done during an update, updated_window will contain the
9465 window that is being updated and the position is the future output
9466 cursor position for that window. If updated_window is null, use
9467 selected_window and display the cursor at the given position. */
9468
9469 void
9470 x_cursor_to (vpos, hpos, y, x)
9471 int vpos, hpos, y, x;
9472 {
9473 struct window *w;
9474
9475 /* If updated_window is not set, work on selected_window. */
9476 if (updated_window)
9477 w = updated_window;
9478 else
9479 w = XWINDOW (selected_window);
9480
9481 /* Set the output cursor. */
9482 output_cursor.hpos = hpos;
9483 output_cursor.vpos = vpos;
9484 output_cursor.x = x;
9485 output_cursor.y = y;
9486
9487 /* If not called as part of an update, really display the cursor.
9488 This will also set the cursor position of W. */
9489 if (updated_window == NULL)
9490 {
9491 BLOCK_INPUT;
9492 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9493 if (rif->flush_display_optional)
9494 rif->flush_display_optional (SELECTED_FRAME ());
9495 UNBLOCK_INPUT;
9496 }
9497 }
9498
9499 #endif /* HAVE_WINDOW_SYSTEM */
9500
9501 \f
9502 /***********************************************************************
9503 Tool-bars
9504 ***********************************************************************/
9505
9506 #ifdef HAVE_WINDOW_SYSTEM
9507
9508 /* Where the mouse was last time we reported a mouse event. */
9509
9510 FRAME_PTR last_mouse_frame;
9511
9512 /* Tool-bar item index of the item on which a mouse button was pressed
9513 or -1. */
9514
9515 int last_tool_bar_item;
9516
9517
9518 /* Update the tool-bar item list for frame F. This has to be done
9519 before we start to fill in any display lines. Called from
9520 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9521 and restore it here. */
9522
9523 static void
9524 update_tool_bar (f, save_match_data)
9525 struct frame *f;
9526 int save_match_data;
9527 {
9528 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9529 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9530 #else
9531 int do_update = WINDOWP (f->tool_bar_window)
9532 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9533 #endif
9534
9535 if (do_update)
9536 {
9537 Lisp_Object window;
9538 struct window *w;
9539
9540 window = FRAME_SELECTED_WINDOW (f);
9541 w = XWINDOW (window);
9542
9543 /* If the user has switched buffers or windows, we need to
9544 recompute to reflect the new bindings. But we'll
9545 recompute when update_mode_lines is set too; that means
9546 that people can use force-mode-line-update to request
9547 that the menu bar be recomputed. The adverse effect on
9548 the rest of the redisplay algorithm is about the same as
9549 windows_or_buffers_changed anyway. */
9550 if (windows_or_buffers_changed
9551 || !NILP (w->update_mode_line)
9552 || update_mode_lines
9553 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9554 < BUF_MODIFF (XBUFFER (w->buffer)))
9555 != !NILP (w->last_had_star))
9556 || ((!NILP (Vtransient_mark_mode)
9557 && !NILP (XBUFFER (w->buffer)->mark_active))
9558 != !NILP (w->region_showing)))
9559 {
9560 struct buffer *prev = current_buffer;
9561 int count = SPECPDL_INDEX ();
9562 Lisp_Object new_tool_bar;
9563 int new_n_tool_bar;
9564 struct gcpro gcpro1;
9565
9566 /* Set current_buffer to the buffer of the selected
9567 window of the frame, so that we get the right local
9568 keymaps. */
9569 set_buffer_internal_1 (XBUFFER (w->buffer));
9570
9571 /* Save match data, if we must. */
9572 if (save_match_data)
9573 record_unwind_save_match_data ();
9574
9575 /* Make sure that we don't accidentally use bogus keymaps. */
9576 if (NILP (Voverriding_local_map_menu_flag))
9577 {
9578 specbind (Qoverriding_terminal_local_map, Qnil);
9579 specbind (Qoverriding_local_map, Qnil);
9580 }
9581
9582 GCPRO1 (new_tool_bar);
9583
9584 /* Build desired tool-bar items from keymaps. */
9585 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9586 &new_n_tool_bar);
9587
9588 /* Redisplay the tool-bar if we changed it. */
9589 if (new_n_tool_bar != f->n_tool_bar_items
9590 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9591 {
9592 /* Redisplay that happens asynchronously due to an expose event
9593 may access f->tool_bar_items. Make sure we update both
9594 variables within BLOCK_INPUT so no such event interrupts. */
9595 BLOCK_INPUT;
9596 f->tool_bar_items = new_tool_bar;
9597 f->n_tool_bar_items = new_n_tool_bar;
9598 w->update_mode_line = Qt;
9599 UNBLOCK_INPUT;
9600 }
9601
9602 UNGCPRO;
9603
9604 unbind_to (count, Qnil);
9605 set_buffer_internal_1 (prev);
9606 }
9607 }
9608 }
9609
9610
9611 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9612 F's desired tool-bar contents. F->tool_bar_items must have
9613 been set up previously by calling prepare_menu_bars. */
9614
9615 static void
9616 build_desired_tool_bar_string (f)
9617 struct frame *f;
9618 {
9619 int i, size, size_needed;
9620 struct gcpro gcpro1, gcpro2, gcpro3;
9621 Lisp_Object image, plist, props;
9622
9623 image = plist = props = Qnil;
9624 GCPRO3 (image, plist, props);
9625
9626 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9627 Otherwise, make a new string. */
9628
9629 /* The size of the string we might be able to reuse. */
9630 size = (STRINGP (f->desired_tool_bar_string)
9631 ? SCHARS (f->desired_tool_bar_string)
9632 : 0);
9633
9634 /* We need one space in the string for each image. */
9635 size_needed = f->n_tool_bar_items;
9636
9637 /* Reuse f->desired_tool_bar_string, if possible. */
9638 if (size < size_needed || NILP (f->desired_tool_bar_string))
9639 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9640 make_number (' '));
9641 else
9642 {
9643 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9644 Fremove_text_properties (make_number (0), make_number (size),
9645 props, f->desired_tool_bar_string);
9646 }
9647
9648 /* Put a `display' property on the string for the images to display,
9649 put a `menu_item' property on tool-bar items with a value that
9650 is the index of the item in F's tool-bar item vector. */
9651 for (i = 0; i < f->n_tool_bar_items; ++i)
9652 {
9653 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9654
9655 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9656 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9657 int hmargin, vmargin, relief, idx, end;
9658 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9659
9660 /* If image is a vector, choose the image according to the
9661 button state. */
9662 image = PROP (TOOL_BAR_ITEM_IMAGES);
9663 if (VECTORP (image))
9664 {
9665 if (enabled_p)
9666 idx = (selected_p
9667 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9668 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9669 else
9670 idx = (selected_p
9671 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9672 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9673
9674 xassert (ASIZE (image) >= idx);
9675 image = AREF (image, idx);
9676 }
9677 else
9678 idx = -1;
9679
9680 /* Ignore invalid image specifications. */
9681 if (!valid_image_p (image))
9682 continue;
9683
9684 /* Display the tool-bar button pressed, or depressed. */
9685 plist = Fcopy_sequence (XCDR (image));
9686
9687 /* Compute margin and relief to draw. */
9688 relief = (tool_bar_button_relief >= 0
9689 ? tool_bar_button_relief
9690 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9691 hmargin = vmargin = relief;
9692
9693 if (INTEGERP (Vtool_bar_button_margin)
9694 && XINT (Vtool_bar_button_margin) > 0)
9695 {
9696 hmargin += XFASTINT (Vtool_bar_button_margin);
9697 vmargin += XFASTINT (Vtool_bar_button_margin);
9698 }
9699 else if (CONSP (Vtool_bar_button_margin))
9700 {
9701 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9702 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9703 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9704
9705 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9706 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9707 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9708 }
9709
9710 if (auto_raise_tool_bar_buttons_p)
9711 {
9712 /* Add a `:relief' property to the image spec if the item is
9713 selected. */
9714 if (selected_p)
9715 {
9716 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9717 hmargin -= relief;
9718 vmargin -= relief;
9719 }
9720 }
9721 else
9722 {
9723 /* If image is selected, display it pressed, i.e. with a
9724 negative relief. If it's not selected, display it with a
9725 raised relief. */
9726 plist = Fplist_put (plist, QCrelief,
9727 (selected_p
9728 ? make_number (-relief)
9729 : make_number (relief)));
9730 hmargin -= relief;
9731 vmargin -= relief;
9732 }
9733
9734 /* Put a margin around the image. */
9735 if (hmargin || vmargin)
9736 {
9737 if (hmargin == vmargin)
9738 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9739 else
9740 plist = Fplist_put (plist, QCmargin,
9741 Fcons (make_number (hmargin),
9742 make_number (vmargin)));
9743 }
9744
9745 /* If button is not enabled, and we don't have special images
9746 for the disabled state, make the image appear disabled by
9747 applying an appropriate algorithm to it. */
9748 if (!enabled_p && idx < 0)
9749 plist = Fplist_put (plist, QCconversion, Qdisabled);
9750
9751 /* Put a `display' text property on the string for the image to
9752 display. Put a `menu-item' property on the string that gives
9753 the start of this item's properties in the tool-bar items
9754 vector. */
9755 image = Fcons (Qimage, plist);
9756 props = list4 (Qdisplay, image,
9757 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9758
9759 /* Let the last image hide all remaining spaces in the tool bar
9760 string. The string can be longer than needed when we reuse a
9761 previous string. */
9762 if (i + 1 == f->n_tool_bar_items)
9763 end = SCHARS (f->desired_tool_bar_string);
9764 else
9765 end = i + 1;
9766 Fadd_text_properties (make_number (i), make_number (end),
9767 props, f->desired_tool_bar_string);
9768 #undef PROP
9769 }
9770
9771 UNGCPRO;
9772 }
9773
9774
9775 /* Display one line of the tool-bar of frame IT->f.
9776
9777 HEIGHT specifies the desired height of the tool-bar line.
9778 If the actual height of the glyph row is less than HEIGHT, the
9779 row's height is increased to HEIGHT, and the icons are centered
9780 vertically in the new height.
9781
9782 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9783 count a final empty row in case the tool-bar width exactly matches
9784 the window width.
9785 */
9786
9787 static void
9788 display_tool_bar_line (it, height)
9789 struct it *it;
9790 int height;
9791 {
9792 struct glyph_row *row = it->glyph_row;
9793 int max_x = it->last_visible_x;
9794 struct glyph *last;
9795
9796 prepare_desired_row (row);
9797 row->y = it->current_y;
9798
9799 /* Note that this isn't made use of if the face hasn't a box,
9800 so there's no need to check the face here. */
9801 it->start_of_box_run_p = 1;
9802
9803 while (it->current_x < max_x)
9804 {
9805 int x, n_glyphs_before, i, nglyphs;
9806 struct it it_before;
9807
9808 /* Get the next display element. */
9809 if (!get_next_display_element (it))
9810 {
9811 /* Don't count empty row if we are counting needed tool-bar lines. */
9812 if (height < 0 && !it->hpos)
9813 return;
9814 break;
9815 }
9816
9817 /* Produce glyphs. */
9818 n_glyphs_before = row->used[TEXT_AREA];
9819 it_before = *it;
9820
9821 PRODUCE_GLYPHS (it);
9822
9823 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9824 i = 0;
9825 x = it_before.current_x;
9826 while (i < nglyphs)
9827 {
9828 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9829
9830 if (x + glyph->pixel_width > max_x)
9831 {
9832 /* Glyph doesn't fit on line. Backtrack. */
9833 row->used[TEXT_AREA] = n_glyphs_before;
9834 *it = it_before;
9835 /* If this is the only glyph on this line, it will never fit on the
9836 toolbar, so skip it. But ensure there is at least one glyph,
9837 so we don't accidentally disable the tool-bar. */
9838 if (n_glyphs_before == 0
9839 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9840 break;
9841 goto out;
9842 }
9843
9844 ++it->hpos;
9845 x += glyph->pixel_width;
9846 ++i;
9847 }
9848
9849 /* Stop at line ends. */
9850 if (ITERATOR_AT_END_OF_LINE_P (it))
9851 break;
9852
9853 set_iterator_to_next (it, 1);
9854 }
9855
9856 out:;
9857
9858 row->displays_text_p = row->used[TEXT_AREA] != 0;
9859
9860 /* Use default face for the border below the tool bar.
9861
9862 FIXME: When auto-resize-tool-bars is grow-only, there is
9863 no additional border below the possibly empty tool-bar lines.
9864 So to make the extra empty lines look "normal", we have to
9865 use the tool-bar face for the border too. */
9866 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9867 it->face_id = DEFAULT_FACE_ID;
9868
9869 extend_face_to_end_of_line (it);
9870 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9871 last->right_box_line_p = 1;
9872 if (last == row->glyphs[TEXT_AREA])
9873 last->left_box_line_p = 1;
9874
9875 /* Make line the desired height and center it vertically. */
9876 if ((height -= it->max_ascent + it->max_descent) > 0)
9877 {
9878 /* Don't add more than one line height. */
9879 height %= FRAME_LINE_HEIGHT (it->f);
9880 it->max_ascent += height / 2;
9881 it->max_descent += (height + 1) / 2;
9882 }
9883
9884 compute_line_metrics (it);
9885
9886 /* If line is empty, make it occupy the rest of the tool-bar. */
9887 if (!row->displays_text_p)
9888 {
9889 row->height = row->phys_height = it->last_visible_y - row->y;
9890 row->visible_height = row->height;
9891 row->ascent = row->phys_ascent = 0;
9892 row->extra_line_spacing = 0;
9893 }
9894
9895 row->full_width_p = 1;
9896 row->continued_p = 0;
9897 row->truncated_on_left_p = 0;
9898 row->truncated_on_right_p = 0;
9899
9900 it->current_x = it->hpos = 0;
9901 it->current_y += row->height;
9902 ++it->vpos;
9903 ++it->glyph_row;
9904 }
9905
9906
9907 /* Max tool-bar height. */
9908
9909 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9910 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9911
9912 /* Value is the number of screen lines needed to make all tool-bar
9913 items of frame F visible. The number of actual rows needed is
9914 returned in *N_ROWS if non-NULL. */
9915
9916 static int
9917 tool_bar_lines_needed (f, n_rows)
9918 struct frame *f;
9919 int *n_rows;
9920 {
9921 struct window *w = XWINDOW (f->tool_bar_window);
9922 struct it it;
9923 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9924 the desired matrix, so use (unused) mode-line row as temporary row to
9925 avoid destroying the first tool-bar row. */
9926 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9927
9928 /* Initialize an iterator for iteration over
9929 F->desired_tool_bar_string in the tool-bar window of frame F. */
9930 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9931 it.first_visible_x = 0;
9932 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9933 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9934
9935 while (!ITERATOR_AT_END_P (&it))
9936 {
9937 clear_glyph_row (temp_row);
9938 it.glyph_row = temp_row;
9939 display_tool_bar_line (&it, -1);
9940 }
9941 clear_glyph_row (temp_row);
9942
9943 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9944 if (n_rows)
9945 *n_rows = it.vpos > 0 ? it.vpos : -1;
9946
9947 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9948 }
9949
9950
9951 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9952 0, 1, 0,
9953 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9954 (frame)
9955 Lisp_Object frame;
9956 {
9957 struct frame *f;
9958 struct window *w;
9959 int nlines = 0;
9960
9961 if (NILP (frame))
9962 frame = selected_frame;
9963 else
9964 CHECK_FRAME (frame);
9965 f = XFRAME (frame);
9966
9967 if (WINDOWP (f->tool_bar_window)
9968 || (w = XWINDOW (f->tool_bar_window),
9969 WINDOW_TOTAL_LINES (w) > 0))
9970 {
9971 update_tool_bar (f, 1);
9972 if (f->n_tool_bar_items)
9973 {
9974 build_desired_tool_bar_string (f);
9975 nlines = tool_bar_lines_needed (f, NULL);
9976 }
9977 }
9978
9979 return make_number (nlines);
9980 }
9981
9982
9983 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9984 height should be changed. */
9985
9986 static int
9987 redisplay_tool_bar (f)
9988 struct frame *f;
9989 {
9990 struct window *w;
9991 struct it it;
9992 struct glyph_row *row;
9993
9994 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9995 if (FRAME_EXTERNAL_TOOL_BAR (f))
9996 update_frame_tool_bar (f);
9997 return 0;
9998 #endif
9999
10000 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10001 do anything. This means you must start with tool-bar-lines
10002 non-zero to get the auto-sizing effect. Or in other words, you
10003 can turn off tool-bars by specifying tool-bar-lines zero. */
10004 if (!WINDOWP (f->tool_bar_window)
10005 || (w = XWINDOW (f->tool_bar_window),
10006 WINDOW_TOTAL_LINES (w) == 0))
10007 return 0;
10008
10009 /* Set up an iterator for the tool-bar window. */
10010 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10011 it.first_visible_x = 0;
10012 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10013 row = it.glyph_row;
10014
10015 /* Build a string that represents the contents of the tool-bar. */
10016 build_desired_tool_bar_string (f);
10017 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10018
10019 if (f->n_tool_bar_rows == 0)
10020 {
10021 int nlines;
10022
10023 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10024 nlines != WINDOW_TOTAL_LINES (w)))
10025 {
10026 extern Lisp_Object Qtool_bar_lines;
10027 Lisp_Object frame;
10028 int old_height = WINDOW_TOTAL_LINES (w);
10029
10030 XSETFRAME (frame, f);
10031 Fmodify_frame_parameters (frame,
10032 Fcons (Fcons (Qtool_bar_lines,
10033 make_number (nlines)),
10034 Qnil));
10035 if (WINDOW_TOTAL_LINES (w) != old_height)
10036 {
10037 clear_glyph_matrix (w->desired_matrix);
10038 fonts_changed_p = 1;
10039 return 1;
10040 }
10041 }
10042 }
10043
10044 /* Display as many lines as needed to display all tool-bar items. */
10045
10046 if (f->n_tool_bar_rows > 0)
10047 {
10048 int border, rows, height, extra;
10049
10050 if (INTEGERP (Vtool_bar_border))
10051 border = XINT (Vtool_bar_border);
10052 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10053 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10054 else if (EQ (Vtool_bar_border, Qborder_width))
10055 border = f->border_width;
10056 else
10057 border = 0;
10058 if (border < 0)
10059 border = 0;
10060
10061 rows = f->n_tool_bar_rows;
10062 height = max (1, (it.last_visible_y - border) / rows);
10063 extra = it.last_visible_y - border - height * rows;
10064
10065 while (it.current_y < it.last_visible_y)
10066 {
10067 int h = 0;
10068 if (extra > 0 && rows-- > 0)
10069 {
10070 h = (extra + rows - 1) / rows;
10071 extra -= h;
10072 }
10073 display_tool_bar_line (&it, height + h);
10074 }
10075 }
10076 else
10077 {
10078 while (it.current_y < it.last_visible_y)
10079 display_tool_bar_line (&it, 0);
10080 }
10081
10082 /* It doesn't make much sense to try scrolling in the tool-bar
10083 window, so don't do it. */
10084 w->desired_matrix->no_scrolling_p = 1;
10085 w->must_be_updated_p = 1;
10086
10087 if (!NILP (Vauto_resize_tool_bars))
10088 {
10089 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10090 int change_height_p = 0;
10091
10092 /* If we couldn't display everything, change the tool-bar's
10093 height if there is room for more. */
10094 if (IT_STRING_CHARPOS (it) < it.end_charpos
10095 && it.current_y < max_tool_bar_height)
10096 change_height_p = 1;
10097
10098 row = it.glyph_row - 1;
10099
10100 /* If there are blank lines at the end, except for a partially
10101 visible blank line at the end that is smaller than
10102 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10103 if (!row->displays_text_p
10104 && row->height >= FRAME_LINE_HEIGHT (f))
10105 change_height_p = 1;
10106
10107 /* If row displays tool-bar items, but is partially visible,
10108 change the tool-bar's height. */
10109 if (row->displays_text_p
10110 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10111 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10112 change_height_p = 1;
10113
10114 /* Resize windows as needed by changing the `tool-bar-lines'
10115 frame parameter. */
10116 if (change_height_p)
10117 {
10118 extern Lisp_Object Qtool_bar_lines;
10119 Lisp_Object frame;
10120 int old_height = WINDOW_TOTAL_LINES (w);
10121 int nrows;
10122 int nlines = tool_bar_lines_needed (f, &nrows);
10123
10124 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10125 && !f->minimize_tool_bar_window_p)
10126 ? (nlines > old_height)
10127 : (nlines != old_height));
10128 f->minimize_tool_bar_window_p = 0;
10129
10130 if (change_height_p)
10131 {
10132 XSETFRAME (frame, f);
10133 Fmodify_frame_parameters (frame,
10134 Fcons (Fcons (Qtool_bar_lines,
10135 make_number (nlines)),
10136 Qnil));
10137 if (WINDOW_TOTAL_LINES (w) != old_height)
10138 {
10139 clear_glyph_matrix (w->desired_matrix);
10140 f->n_tool_bar_rows = nrows;
10141 fonts_changed_p = 1;
10142 return 1;
10143 }
10144 }
10145 }
10146 }
10147
10148 f->minimize_tool_bar_window_p = 0;
10149 return 0;
10150 }
10151
10152
10153 /* Get information about the tool-bar item which is displayed in GLYPH
10154 on frame F. Return in *PROP_IDX the index where tool-bar item
10155 properties start in F->tool_bar_items. Value is zero if
10156 GLYPH doesn't display a tool-bar item. */
10157
10158 static int
10159 tool_bar_item_info (f, glyph, prop_idx)
10160 struct frame *f;
10161 struct glyph *glyph;
10162 int *prop_idx;
10163 {
10164 Lisp_Object prop;
10165 int success_p;
10166 int charpos;
10167
10168 /* This function can be called asynchronously, which means we must
10169 exclude any possibility that Fget_text_property signals an
10170 error. */
10171 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10172 charpos = max (0, charpos);
10173
10174 /* Get the text property `menu-item' at pos. The value of that
10175 property is the start index of this item's properties in
10176 F->tool_bar_items. */
10177 prop = Fget_text_property (make_number (charpos),
10178 Qmenu_item, f->current_tool_bar_string);
10179 if (INTEGERP (prop))
10180 {
10181 *prop_idx = XINT (prop);
10182 success_p = 1;
10183 }
10184 else
10185 success_p = 0;
10186
10187 return success_p;
10188 }
10189
10190 \f
10191 /* Get information about the tool-bar item at position X/Y on frame F.
10192 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10193 the current matrix of the tool-bar window of F, or NULL if not
10194 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10195 item in F->tool_bar_items. Value is
10196
10197 -1 if X/Y is not on a tool-bar item
10198 0 if X/Y is on the same item that was highlighted before.
10199 1 otherwise. */
10200
10201 static int
10202 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10203 struct frame *f;
10204 int x, y;
10205 struct glyph **glyph;
10206 int *hpos, *vpos, *prop_idx;
10207 {
10208 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10209 struct window *w = XWINDOW (f->tool_bar_window);
10210 int area;
10211
10212 /* Find the glyph under X/Y. */
10213 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10214 if (*glyph == NULL)
10215 return -1;
10216
10217 /* Get the start of this tool-bar item's properties in
10218 f->tool_bar_items. */
10219 if (!tool_bar_item_info (f, *glyph, prop_idx))
10220 return -1;
10221
10222 /* Is mouse on the highlighted item? */
10223 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10224 && *vpos >= dpyinfo->mouse_face_beg_row
10225 && *vpos <= dpyinfo->mouse_face_end_row
10226 && (*vpos > dpyinfo->mouse_face_beg_row
10227 || *hpos >= dpyinfo->mouse_face_beg_col)
10228 && (*vpos < dpyinfo->mouse_face_end_row
10229 || *hpos < dpyinfo->mouse_face_end_col
10230 || dpyinfo->mouse_face_past_end))
10231 return 0;
10232
10233 return 1;
10234 }
10235
10236
10237 /* EXPORT:
10238 Handle mouse button event on the tool-bar of frame F, at
10239 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10240 0 for button release. MODIFIERS is event modifiers for button
10241 release. */
10242
10243 void
10244 handle_tool_bar_click (f, x, y, down_p, modifiers)
10245 struct frame *f;
10246 int x, y, down_p;
10247 unsigned int modifiers;
10248 {
10249 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10250 struct window *w = XWINDOW (f->tool_bar_window);
10251 int hpos, vpos, prop_idx;
10252 struct glyph *glyph;
10253 Lisp_Object enabled_p;
10254
10255 /* If not on the highlighted tool-bar item, return. */
10256 frame_to_window_pixel_xy (w, &x, &y);
10257 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10258 return;
10259
10260 /* If item is disabled, do nothing. */
10261 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10262 if (NILP (enabled_p))
10263 return;
10264
10265 if (down_p)
10266 {
10267 /* Show item in pressed state. */
10268 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10269 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10270 last_tool_bar_item = prop_idx;
10271 }
10272 else
10273 {
10274 Lisp_Object key, frame;
10275 struct input_event event;
10276 EVENT_INIT (event);
10277
10278 /* Show item in released state. */
10279 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10280 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10281
10282 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10283
10284 XSETFRAME (frame, f);
10285 event.kind = TOOL_BAR_EVENT;
10286 event.frame_or_window = frame;
10287 event.arg = frame;
10288 kbd_buffer_store_event (&event);
10289
10290 event.kind = TOOL_BAR_EVENT;
10291 event.frame_or_window = frame;
10292 event.arg = key;
10293 event.modifiers = modifiers;
10294 kbd_buffer_store_event (&event);
10295 last_tool_bar_item = -1;
10296 }
10297 }
10298
10299
10300 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10301 tool-bar window-relative coordinates X/Y. Called from
10302 note_mouse_highlight. */
10303
10304 static void
10305 note_tool_bar_highlight (f, x, y)
10306 struct frame *f;
10307 int x, y;
10308 {
10309 Lisp_Object window = f->tool_bar_window;
10310 struct window *w = XWINDOW (window);
10311 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10312 int hpos, vpos;
10313 struct glyph *glyph;
10314 struct glyph_row *row;
10315 int i;
10316 Lisp_Object enabled_p;
10317 int prop_idx;
10318 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10319 int mouse_down_p, rc;
10320
10321 /* Function note_mouse_highlight is called with negative x(y
10322 values when mouse moves outside of the frame. */
10323 if (x <= 0 || y <= 0)
10324 {
10325 clear_mouse_face (dpyinfo);
10326 return;
10327 }
10328
10329 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10330 if (rc < 0)
10331 {
10332 /* Not on tool-bar item. */
10333 clear_mouse_face (dpyinfo);
10334 return;
10335 }
10336 else if (rc == 0)
10337 /* On same tool-bar item as before. */
10338 goto set_help_echo;
10339
10340 clear_mouse_face (dpyinfo);
10341
10342 /* Mouse is down, but on different tool-bar item? */
10343 mouse_down_p = (dpyinfo->grabbed
10344 && f == last_mouse_frame
10345 && FRAME_LIVE_P (f));
10346 if (mouse_down_p
10347 && last_tool_bar_item != prop_idx)
10348 return;
10349
10350 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10351 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10352
10353 /* If tool-bar item is not enabled, don't highlight it. */
10354 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10355 if (!NILP (enabled_p))
10356 {
10357 /* Compute the x-position of the glyph. In front and past the
10358 image is a space. We include this in the highlighted area. */
10359 row = MATRIX_ROW (w->current_matrix, vpos);
10360 for (i = x = 0; i < hpos; ++i)
10361 x += row->glyphs[TEXT_AREA][i].pixel_width;
10362
10363 /* Record this as the current active region. */
10364 dpyinfo->mouse_face_beg_col = hpos;
10365 dpyinfo->mouse_face_beg_row = vpos;
10366 dpyinfo->mouse_face_beg_x = x;
10367 dpyinfo->mouse_face_beg_y = row->y;
10368 dpyinfo->mouse_face_past_end = 0;
10369
10370 dpyinfo->mouse_face_end_col = hpos + 1;
10371 dpyinfo->mouse_face_end_row = vpos;
10372 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10373 dpyinfo->mouse_face_end_y = row->y;
10374 dpyinfo->mouse_face_window = window;
10375 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10376
10377 /* Display it as active. */
10378 show_mouse_face (dpyinfo, draw);
10379 dpyinfo->mouse_face_image_state = draw;
10380 }
10381
10382 set_help_echo:
10383
10384 /* Set help_echo_string to a help string to display for this tool-bar item.
10385 XTread_socket does the rest. */
10386 help_echo_object = help_echo_window = Qnil;
10387 help_echo_pos = -1;
10388 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10389 if (NILP (help_echo_string))
10390 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10391 }
10392
10393 #endif /* HAVE_WINDOW_SYSTEM */
10394
10395
10396 \f
10397 /************************************************************************
10398 Horizontal scrolling
10399 ************************************************************************/
10400
10401 static int hscroll_window_tree P_ ((Lisp_Object));
10402 static int hscroll_windows P_ ((Lisp_Object));
10403
10404 /* For all leaf windows in the window tree rooted at WINDOW, set their
10405 hscroll value so that PT is (i) visible in the window, and (ii) so
10406 that it is not within a certain margin at the window's left and
10407 right border. Value is non-zero if any window's hscroll has been
10408 changed. */
10409
10410 static int
10411 hscroll_window_tree (window)
10412 Lisp_Object window;
10413 {
10414 int hscrolled_p = 0;
10415 int hscroll_relative_p = FLOATP (Vhscroll_step);
10416 int hscroll_step_abs = 0;
10417 double hscroll_step_rel = 0;
10418
10419 if (hscroll_relative_p)
10420 {
10421 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10422 if (hscroll_step_rel < 0)
10423 {
10424 hscroll_relative_p = 0;
10425 hscroll_step_abs = 0;
10426 }
10427 }
10428 else if (INTEGERP (Vhscroll_step))
10429 {
10430 hscroll_step_abs = XINT (Vhscroll_step);
10431 if (hscroll_step_abs < 0)
10432 hscroll_step_abs = 0;
10433 }
10434 else
10435 hscroll_step_abs = 0;
10436
10437 while (WINDOWP (window))
10438 {
10439 struct window *w = XWINDOW (window);
10440
10441 if (WINDOWP (w->hchild))
10442 hscrolled_p |= hscroll_window_tree (w->hchild);
10443 else if (WINDOWP (w->vchild))
10444 hscrolled_p |= hscroll_window_tree (w->vchild);
10445 else if (w->cursor.vpos >= 0)
10446 {
10447 int h_margin;
10448 int text_area_width;
10449 struct glyph_row *current_cursor_row
10450 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10451 struct glyph_row *desired_cursor_row
10452 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10453 struct glyph_row *cursor_row
10454 = (desired_cursor_row->enabled_p
10455 ? desired_cursor_row
10456 : current_cursor_row);
10457
10458 text_area_width = window_box_width (w, TEXT_AREA);
10459
10460 /* Scroll when cursor is inside this scroll margin. */
10461 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10462
10463 if ((XFASTINT (w->hscroll)
10464 && w->cursor.x <= h_margin)
10465 || (cursor_row->enabled_p
10466 && cursor_row->truncated_on_right_p
10467 && (w->cursor.x >= text_area_width - h_margin)))
10468 {
10469 struct it it;
10470 int hscroll;
10471 struct buffer *saved_current_buffer;
10472 int pt;
10473 int wanted_x;
10474
10475 /* Find point in a display of infinite width. */
10476 saved_current_buffer = current_buffer;
10477 current_buffer = XBUFFER (w->buffer);
10478
10479 if (w == XWINDOW (selected_window))
10480 pt = BUF_PT (current_buffer);
10481 else
10482 {
10483 pt = marker_position (w->pointm);
10484 pt = max (BEGV, pt);
10485 pt = min (ZV, pt);
10486 }
10487
10488 /* Move iterator to pt starting at cursor_row->start in
10489 a line with infinite width. */
10490 init_to_row_start (&it, w, cursor_row);
10491 it.last_visible_x = INFINITY;
10492 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10493 current_buffer = saved_current_buffer;
10494
10495 /* Position cursor in window. */
10496 if (!hscroll_relative_p && hscroll_step_abs == 0)
10497 hscroll = max (0, (it.current_x
10498 - (ITERATOR_AT_END_OF_LINE_P (&it)
10499 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10500 : (text_area_width / 2))))
10501 / FRAME_COLUMN_WIDTH (it.f);
10502 else if (w->cursor.x >= text_area_width - h_margin)
10503 {
10504 if (hscroll_relative_p)
10505 wanted_x = text_area_width * (1 - hscroll_step_rel)
10506 - h_margin;
10507 else
10508 wanted_x = text_area_width
10509 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10510 - h_margin;
10511 hscroll
10512 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10513 }
10514 else
10515 {
10516 if (hscroll_relative_p)
10517 wanted_x = text_area_width * hscroll_step_rel
10518 + h_margin;
10519 else
10520 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10521 + h_margin;
10522 hscroll
10523 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10524 }
10525 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10526
10527 /* Don't call Fset_window_hscroll if value hasn't
10528 changed because it will prevent redisplay
10529 optimizations. */
10530 if (XFASTINT (w->hscroll) != hscroll)
10531 {
10532 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10533 w->hscroll = make_number (hscroll);
10534 hscrolled_p = 1;
10535 }
10536 }
10537 }
10538
10539 window = w->next;
10540 }
10541
10542 /* Value is non-zero if hscroll of any leaf window has been changed. */
10543 return hscrolled_p;
10544 }
10545
10546
10547 /* Set hscroll so that cursor is visible and not inside horizontal
10548 scroll margins for all windows in the tree rooted at WINDOW. See
10549 also hscroll_window_tree above. Value is non-zero if any window's
10550 hscroll has been changed. If it has, desired matrices on the frame
10551 of WINDOW are cleared. */
10552
10553 static int
10554 hscroll_windows (window)
10555 Lisp_Object window;
10556 {
10557 int hscrolled_p;
10558
10559 if (automatic_hscrolling_p)
10560 {
10561 hscrolled_p = hscroll_window_tree (window);
10562 if (hscrolled_p)
10563 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10564 }
10565 else
10566 hscrolled_p = 0;
10567 return hscrolled_p;
10568 }
10569
10570
10571 \f
10572 /************************************************************************
10573 Redisplay
10574 ************************************************************************/
10575
10576 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10577 to a non-zero value. This is sometimes handy to have in a debugger
10578 session. */
10579
10580 #if GLYPH_DEBUG
10581
10582 /* First and last unchanged row for try_window_id. */
10583
10584 int debug_first_unchanged_at_end_vpos;
10585 int debug_last_unchanged_at_beg_vpos;
10586
10587 /* Delta vpos and y. */
10588
10589 int debug_dvpos, debug_dy;
10590
10591 /* Delta in characters and bytes for try_window_id. */
10592
10593 int debug_delta, debug_delta_bytes;
10594
10595 /* Values of window_end_pos and window_end_vpos at the end of
10596 try_window_id. */
10597
10598 EMACS_INT debug_end_pos, debug_end_vpos;
10599
10600 /* Append a string to W->desired_matrix->method. FMT is a printf
10601 format string. A1...A9 are a supplement for a variable-length
10602 argument list. If trace_redisplay_p is non-zero also printf the
10603 resulting string to stderr. */
10604
10605 static void
10606 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10607 struct window *w;
10608 char *fmt;
10609 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10610 {
10611 char buffer[512];
10612 char *method = w->desired_matrix->method;
10613 int len = strlen (method);
10614 int size = sizeof w->desired_matrix->method;
10615 int remaining = size - len - 1;
10616
10617 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10618 if (len && remaining)
10619 {
10620 method[len] = '|';
10621 --remaining, ++len;
10622 }
10623
10624 strncpy (method + len, buffer, remaining);
10625
10626 if (trace_redisplay_p)
10627 fprintf (stderr, "%p (%s): %s\n",
10628 w,
10629 ((BUFFERP (w->buffer)
10630 && STRINGP (XBUFFER (w->buffer)->name))
10631 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10632 : "no buffer"),
10633 buffer);
10634 }
10635
10636 #endif /* GLYPH_DEBUG */
10637
10638
10639 /* Value is non-zero if all changes in window W, which displays
10640 current_buffer, are in the text between START and END. START is a
10641 buffer position, END is given as a distance from Z. Used in
10642 redisplay_internal for display optimization. */
10643
10644 static INLINE int
10645 text_outside_line_unchanged_p (w, start, end)
10646 struct window *w;
10647 int start, end;
10648 {
10649 int unchanged_p = 1;
10650
10651 /* If text or overlays have changed, see where. */
10652 if (XFASTINT (w->last_modified) < MODIFF
10653 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10654 {
10655 /* Gap in the line? */
10656 if (GPT < start || Z - GPT < end)
10657 unchanged_p = 0;
10658
10659 /* Changes start in front of the line, or end after it? */
10660 if (unchanged_p
10661 && (BEG_UNCHANGED < start - 1
10662 || END_UNCHANGED < end))
10663 unchanged_p = 0;
10664
10665 /* If selective display, can't optimize if changes start at the
10666 beginning of the line. */
10667 if (unchanged_p
10668 && INTEGERP (current_buffer->selective_display)
10669 && XINT (current_buffer->selective_display) > 0
10670 && (BEG_UNCHANGED < start || GPT <= start))
10671 unchanged_p = 0;
10672
10673 /* If there are overlays at the start or end of the line, these
10674 may have overlay strings with newlines in them. A change at
10675 START, for instance, may actually concern the display of such
10676 overlay strings as well, and they are displayed on different
10677 lines. So, quickly rule out this case. (For the future, it
10678 might be desirable to implement something more telling than
10679 just BEG/END_UNCHANGED.) */
10680 if (unchanged_p)
10681 {
10682 if (BEG + BEG_UNCHANGED == start
10683 && overlay_touches_p (start))
10684 unchanged_p = 0;
10685 if (END_UNCHANGED == end
10686 && overlay_touches_p (Z - end))
10687 unchanged_p = 0;
10688 }
10689 }
10690
10691 return unchanged_p;
10692 }
10693
10694
10695 /* Do a frame update, taking possible shortcuts into account. This is
10696 the main external entry point for redisplay.
10697
10698 If the last redisplay displayed an echo area message and that message
10699 is no longer requested, we clear the echo area or bring back the
10700 mini-buffer if that is in use. */
10701
10702 void
10703 redisplay ()
10704 {
10705 redisplay_internal (0);
10706 }
10707
10708
10709 static Lisp_Object
10710 overlay_arrow_string_or_property (var)
10711 Lisp_Object var;
10712 {
10713 Lisp_Object val;
10714
10715 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10716 return val;
10717
10718 return Voverlay_arrow_string;
10719 }
10720
10721 /* Return 1 if there are any overlay-arrows in current_buffer. */
10722 static int
10723 overlay_arrow_in_current_buffer_p ()
10724 {
10725 Lisp_Object vlist;
10726
10727 for (vlist = Voverlay_arrow_variable_list;
10728 CONSP (vlist);
10729 vlist = XCDR (vlist))
10730 {
10731 Lisp_Object var = XCAR (vlist);
10732 Lisp_Object val;
10733
10734 if (!SYMBOLP (var))
10735 continue;
10736 val = find_symbol_value (var);
10737 if (MARKERP (val)
10738 && current_buffer == XMARKER (val)->buffer)
10739 return 1;
10740 }
10741 return 0;
10742 }
10743
10744
10745 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10746 has changed. */
10747
10748 static int
10749 overlay_arrows_changed_p ()
10750 {
10751 Lisp_Object vlist;
10752
10753 for (vlist = Voverlay_arrow_variable_list;
10754 CONSP (vlist);
10755 vlist = XCDR (vlist))
10756 {
10757 Lisp_Object var = XCAR (vlist);
10758 Lisp_Object val, pstr;
10759
10760 if (!SYMBOLP (var))
10761 continue;
10762 val = find_symbol_value (var);
10763 if (!MARKERP (val))
10764 continue;
10765 if (! EQ (COERCE_MARKER (val),
10766 Fget (var, Qlast_arrow_position))
10767 || ! (pstr = overlay_arrow_string_or_property (var),
10768 EQ (pstr, Fget (var, Qlast_arrow_string))))
10769 return 1;
10770 }
10771 return 0;
10772 }
10773
10774 /* Mark overlay arrows to be updated on next redisplay. */
10775
10776 static void
10777 update_overlay_arrows (up_to_date)
10778 int up_to_date;
10779 {
10780 Lisp_Object vlist;
10781
10782 for (vlist = Voverlay_arrow_variable_list;
10783 CONSP (vlist);
10784 vlist = XCDR (vlist))
10785 {
10786 Lisp_Object var = XCAR (vlist);
10787
10788 if (!SYMBOLP (var))
10789 continue;
10790
10791 if (up_to_date > 0)
10792 {
10793 Lisp_Object val = find_symbol_value (var);
10794 Fput (var, Qlast_arrow_position,
10795 COERCE_MARKER (val));
10796 Fput (var, Qlast_arrow_string,
10797 overlay_arrow_string_or_property (var));
10798 }
10799 else if (up_to_date < 0
10800 || !NILP (Fget (var, Qlast_arrow_position)))
10801 {
10802 Fput (var, Qlast_arrow_position, Qt);
10803 Fput (var, Qlast_arrow_string, Qt);
10804 }
10805 }
10806 }
10807
10808
10809 /* Return overlay arrow string to display at row.
10810 Return integer (bitmap number) for arrow bitmap in left fringe.
10811 Return nil if no overlay arrow. */
10812
10813 static Lisp_Object
10814 overlay_arrow_at_row (it, row)
10815 struct it *it;
10816 struct glyph_row *row;
10817 {
10818 Lisp_Object vlist;
10819
10820 for (vlist = Voverlay_arrow_variable_list;
10821 CONSP (vlist);
10822 vlist = XCDR (vlist))
10823 {
10824 Lisp_Object var = XCAR (vlist);
10825 Lisp_Object val;
10826
10827 if (!SYMBOLP (var))
10828 continue;
10829
10830 val = find_symbol_value (var);
10831
10832 if (MARKERP (val)
10833 && current_buffer == XMARKER (val)->buffer
10834 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10835 {
10836 if (FRAME_WINDOW_P (it->f)
10837 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10838 {
10839 #ifdef HAVE_WINDOW_SYSTEM
10840 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10841 {
10842 int fringe_bitmap;
10843 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10844 return make_number (fringe_bitmap);
10845 }
10846 #endif
10847 return make_number (-1); /* Use default arrow bitmap */
10848 }
10849 return overlay_arrow_string_or_property (var);
10850 }
10851 }
10852
10853 return Qnil;
10854 }
10855
10856 /* Return 1 if point moved out of or into a composition. Otherwise
10857 return 0. PREV_BUF and PREV_PT are the last point buffer and
10858 position. BUF and PT are the current point buffer and position. */
10859
10860 int
10861 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10862 struct buffer *prev_buf, *buf;
10863 int prev_pt, pt;
10864 {
10865 EMACS_INT start, end;
10866 Lisp_Object prop;
10867 Lisp_Object buffer;
10868
10869 XSETBUFFER (buffer, buf);
10870 /* Check a composition at the last point if point moved within the
10871 same buffer. */
10872 if (prev_buf == buf)
10873 {
10874 if (prev_pt == pt)
10875 /* Point didn't move. */
10876 return 0;
10877
10878 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10879 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10880 && COMPOSITION_VALID_P (start, end, prop)
10881 && start < prev_pt && end > prev_pt)
10882 /* The last point was within the composition. Return 1 iff
10883 point moved out of the composition. */
10884 return (pt <= start || pt >= end);
10885 }
10886
10887 /* Check a composition at the current point. */
10888 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10889 && find_composition (pt, -1, &start, &end, &prop, buffer)
10890 && COMPOSITION_VALID_P (start, end, prop)
10891 && start < pt && end > pt);
10892 }
10893
10894
10895 /* Reconsider the setting of B->clip_changed which is displayed
10896 in window W. */
10897
10898 static INLINE void
10899 reconsider_clip_changes (w, b)
10900 struct window *w;
10901 struct buffer *b;
10902 {
10903 if (b->clip_changed
10904 && !NILP (w->window_end_valid)
10905 && w->current_matrix->buffer == b
10906 && w->current_matrix->zv == BUF_ZV (b)
10907 && w->current_matrix->begv == BUF_BEGV (b))
10908 b->clip_changed = 0;
10909
10910 /* If display wasn't paused, and W is not a tool bar window, see if
10911 point has been moved into or out of a composition. In that case,
10912 we set b->clip_changed to 1 to force updating the screen. If
10913 b->clip_changed has already been set to 1, we can skip this
10914 check. */
10915 if (!b->clip_changed
10916 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10917 {
10918 int pt;
10919
10920 if (w == XWINDOW (selected_window))
10921 pt = BUF_PT (current_buffer);
10922 else
10923 pt = marker_position (w->pointm);
10924
10925 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10926 || pt != XINT (w->last_point))
10927 && check_point_in_composition (w->current_matrix->buffer,
10928 XINT (w->last_point),
10929 XBUFFER (w->buffer), pt))
10930 b->clip_changed = 1;
10931 }
10932 }
10933 \f
10934
10935 /* Select FRAME to forward the values of frame-local variables into C
10936 variables so that the redisplay routines can access those values
10937 directly. */
10938
10939 static void
10940 select_frame_for_redisplay (frame)
10941 Lisp_Object frame;
10942 {
10943 Lisp_Object tail, sym, val;
10944 Lisp_Object old = selected_frame;
10945
10946 selected_frame = frame;
10947
10948 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10949 if (CONSP (XCAR (tail))
10950 && (sym = XCAR (XCAR (tail)),
10951 SYMBOLP (sym))
10952 && (sym = indirect_variable (sym),
10953 val = SYMBOL_VALUE (sym),
10954 (BUFFER_LOCAL_VALUEP (val)
10955 || SOME_BUFFER_LOCAL_VALUEP (val)))
10956 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10957 /* Use find_symbol_value rather than Fsymbol_value
10958 to avoid an error if it is void. */
10959 find_symbol_value (sym);
10960
10961 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10962 if (CONSP (XCAR (tail))
10963 && (sym = XCAR (XCAR (tail)),
10964 SYMBOLP (sym))
10965 && (sym = indirect_variable (sym),
10966 val = SYMBOL_VALUE (sym),
10967 (BUFFER_LOCAL_VALUEP (val)
10968 || SOME_BUFFER_LOCAL_VALUEP (val)))
10969 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10970 find_symbol_value (sym);
10971 }
10972
10973
10974 #define STOP_POLLING \
10975 do { if (! polling_stopped_here) stop_polling (); \
10976 polling_stopped_here = 1; } while (0)
10977
10978 #define RESUME_POLLING \
10979 do { if (polling_stopped_here) start_polling (); \
10980 polling_stopped_here = 0; } while (0)
10981
10982
10983 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10984 response to any user action; therefore, we should preserve the echo
10985 area. (Actually, our caller does that job.) Perhaps in the future
10986 avoid recentering windows if it is not necessary; currently that
10987 causes some problems. */
10988
10989 static void
10990 redisplay_internal (preserve_echo_area)
10991 int preserve_echo_area;
10992 {
10993 struct window *w = XWINDOW (selected_window);
10994 struct frame *f;
10995 int pause;
10996 int must_finish = 0;
10997 struct text_pos tlbufpos, tlendpos;
10998 int number_of_visible_frames;
10999 int count, count1;
11000 struct frame *sf;
11001 int polling_stopped_here = 0;
11002
11003 /* Non-zero means redisplay has to consider all windows on all
11004 frames. Zero means, only selected_window is considered. */
11005 int consider_all_windows_p;
11006
11007 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11008
11009 /* No redisplay if running in batch mode or frame is not yet fully
11010 initialized, or redisplay is explicitly turned off by setting
11011 Vinhibit_redisplay. */
11012 if (noninteractive
11013 || !NILP (Vinhibit_redisplay))
11014 return;
11015
11016 /* Don't examine these until after testing Vinhibit_redisplay.
11017 When Emacs is shutting down, perhaps because its connection to
11018 X has dropped, we should not look at them at all. */
11019 f = XFRAME (w->frame);
11020 sf = SELECTED_FRAME ();
11021
11022 if (!f->glyphs_initialized_p)
11023 return;
11024
11025 /* The flag redisplay_performed_directly_p is set by
11026 direct_output_for_insert when it already did the whole screen
11027 update necessary. */
11028 if (redisplay_performed_directly_p)
11029 {
11030 redisplay_performed_directly_p = 0;
11031 if (!hscroll_windows (selected_window))
11032 return;
11033 }
11034
11035 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11036 if (popup_activated ())
11037 return;
11038 #endif
11039
11040 /* I don't think this happens but let's be paranoid. */
11041 if (redisplaying_p)
11042 return;
11043
11044 /* Record a function that resets redisplaying_p to its old value
11045 when we leave this function. */
11046 count = SPECPDL_INDEX ();
11047 record_unwind_protect (unwind_redisplay,
11048 Fcons (make_number (redisplaying_p), selected_frame));
11049 ++redisplaying_p;
11050 specbind (Qinhibit_free_realized_faces, Qnil);
11051
11052 {
11053 Lisp_Object tail, frame;
11054
11055 FOR_EACH_FRAME (tail, frame)
11056 {
11057 struct frame *f = XFRAME (frame);
11058 f->already_hscrolled_p = 0;
11059 }
11060 }
11061
11062 retry:
11063 pause = 0;
11064 reconsider_clip_changes (w, current_buffer);
11065 last_escape_glyph_frame = NULL;
11066 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11067
11068 /* If new fonts have been loaded that make a glyph matrix adjustment
11069 necessary, do it. */
11070 if (fonts_changed_p)
11071 {
11072 adjust_glyphs (NULL);
11073 ++windows_or_buffers_changed;
11074 fonts_changed_p = 0;
11075 }
11076
11077 /* If face_change_count is non-zero, init_iterator will free all
11078 realized faces, which includes the faces referenced from current
11079 matrices. So, we can't reuse current matrices in this case. */
11080 if (face_change_count)
11081 ++windows_or_buffers_changed;
11082
11083 if (! FRAME_WINDOW_P (sf)
11084 && previous_terminal_frame != sf)
11085 {
11086 /* Since frames on an ASCII terminal share the same display
11087 area, displaying a different frame means redisplay the whole
11088 thing. */
11089 windows_or_buffers_changed++;
11090 SET_FRAME_GARBAGED (sf);
11091 XSETFRAME (Vterminal_frame, sf);
11092 }
11093 previous_terminal_frame = sf;
11094
11095 /* Set the visible flags for all frames. Do this before checking
11096 for resized or garbaged frames; they want to know if their frames
11097 are visible. See the comment in frame.h for
11098 FRAME_SAMPLE_VISIBILITY. */
11099 {
11100 Lisp_Object tail, frame;
11101
11102 number_of_visible_frames = 0;
11103
11104 FOR_EACH_FRAME (tail, frame)
11105 {
11106 struct frame *f = XFRAME (frame);
11107
11108 FRAME_SAMPLE_VISIBILITY (f);
11109 if (FRAME_VISIBLE_P (f))
11110 ++number_of_visible_frames;
11111 clear_desired_matrices (f);
11112 }
11113 }
11114
11115 /* Notice any pending interrupt request to change frame size. */
11116 do_pending_window_change (1);
11117
11118 /* Clear frames marked as garbaged. */
11119 if (frame_garbaged)
11120 clear_garbaged_frames ();
11121
11122 /* Build menubar and tool-bar items. */
11123 if (NILP (Vmemory_full))
11124 prepare_menu_bars ();
11125
11126 if (windows_or_buffers_changed)
11127 update_mode_lines++;
11128
11129 /* Detect case that we need to write or remove a star in the mode line. */
11130 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11131 {
11132 w->update_mode_line = Qt;
11133 if (buffer_shared > 1)
11134 update_mode_lines++;
11135 }
11136
11137 /* Avoid invocation of point motion hooks by `current_column' below. */
11138 count1 = SPECPDL_INDEX ();
11139 specbind (Qinhibit_point_motion_hooks, Qt);
11140
11141 /* If %c is in the mode line, update it if needed. */
11142 if (!NILP (w->column_number_displayed)
11143 /* This alternative quickly identifies a common case
11144 where no change is needed. */
11145 && !(PT == XFASTINT (w->last_point)
11146 && XFASTINT (w->last_modified) >= MODIFF
11147 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11148 && (XFASTINT (w->column_number_displayed)
11149 != (int) current_column ())) /* iftc */
11150 w->update_mode_line = Qt;
11151
11152 unbind_to (count1, Qnil);
11153
11154 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11155
11156 /* The variable buffer_shared is set in redisplay_window and
11157 indicates that we redisplay a buffer in different windows. See
11158 there. */
11159 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11160 || cursor_type_changed);
11161
11162 /* If specs for an arrow have changed, do thorough redisplay
11163 to ensure we remove any arrow that should no longer exist. */
11164 if (overlay_arrows_changed_p ())
11165 consider_all_windows_p = windows_or_buffers_changed = 1;
11166
11167 /* Normally the message* functions will have already displayed and
11168 updated the echo area, but the frame may have been trashed, or
11169 the update may have been preempted, so display the echo area
11170 again here. Checking message_cleared_p captures the case that
11171 the echo area should be cleared. */
11172 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11173 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11174 || (message_cleared_p
11175 && minibuf_level == 0
11176 /* If the mini-window is currently selected, this means the
11177 echo-area doesn't show through. */
11178 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11179 {
11180 int window_height_changed_p = echo_area_display (0);
11181 must_finish = 1;
11182
11183 /* If we don't display the current message, don't clear the
11184 message_cleared_p flag, because, if we did, we wouldn't clear
11185 the echo area in the next redisplay which doesn't preserve
11186 the echo area. */
11187 if (!display_last_displayed_message_p)
11188 message_cleared_p = 0;
11189
11190 if (fonts_changed_p)
11191 goto retry;
11192 else if (window_height_changed_p)
11193 {
11194 consider_all_windows_p = 1;
11195 ++update_mode_lines;
11196 ++windows_or_buffers_changed;
11197
11198 /* If window configuration was changed, frames may have been
11199 marked garbaged. Clear them or we will experience
11200 surprises wrt scrolling. */
11201 if (frame_garbaged)
11202 clear_garbaged_frames ();
11203 }
11204 }
11205 else if (EQ (selected_window, minibuf_window)
11206 && (current_buffer->clip_changed
11207 || XFASTINT (w->last_modified) < MODIFF
11208 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11209 && resize_mini_window (w, 0))
11210 {
11211 /* Resized active mini-window to fit the size of what it is
11212 showing if its contents might have changed. */
11213 must_finish = 1;
11214 consider_all_windows_p = 1;
11215 ++windows_or_buffers_changed;
11216 ++update_mode_lines;
11217
11218 /* If window configuration was changed, frames may have been
11219 marked garbaged. Clear them or we will experience
11220 surprises wrt scrolling. */
11221 if (frame_garbaged)
11222 clear_garbaged_frames ();
11223 }
11224
11225
11226 /* If showing the region, and mark has changed, we must redisplay
11227 the whole window. The assignment to this_line_start_pos prevents
11228 the optimization directly below this if-statement. */
11229 if (((!NILP (Vtransient_mark_mode)
11230 && !NILP (XBUFFER (w->buffer)->mark_active))
11231 != !NILP (w->region_showing))
11232 || (!NILP (w->region_showing)
11233 && !EQ (w->region_showing,
11234 Fmarker_position (XBUFFER (w->buffer)->mark))))
11235 CHARPOS (this_line_start_pos) = 0;
11236
11237 /* Optimize the case that only the line containing the cursor in the
11238 selected window has changed. Variables starting with this_ are
11239 set in display_line and record information about the line
11240 containing the cursor. */
11241 tlbufpos = this_line_start_pos;
11242 tlendpos = this_line_end_pos;
11243 if (!consider_all_windows_p
11244 && CHARPOS (tlbufpos) > 0
11245 && NILP (w->update_mode_line)
11246 && !current_buffer->clip_changed
11247 && !current_buffer->prevent_redisplay_optimizations_p
11248 && FRAME_VISIBLE_P (XFRAME (w->frame))
11249 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11250 /* Make sure recorded data applies to current buffer, etc. */
11251 && this_line_buffer == current_buffer
11252 && current_buffer == XBUFFER (w->buffer)
11253 && NILP (w->force_start)
11254 && NILP (w->optional_new_start)
11255 /* Point must be on the line that we have info recorded about. */
11256 && PT >= CHARPOS (tlbufpos)
11257 && PT <= Z - CHARPOS (tlendpos)
11258 /* All text outside that line, including its final newline,
11259 must be unchanged */
11260 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11261 CHARPOS (tlendpos)))
11262 {
11263 if (CHARPOS (tlbufpos) > BEGV
11264 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11265 && (CHARPOS (tlbufpos) == ZV
11266 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11267 /* Former continuation line has disappeared by becoming empty */
11268 goto cancel;
11269 else if (XFASTINT (w->last_modified) < MODIFF
11270 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11271 || MINI_WINDOW_P (w))
11272 {
11273 /* We have to handle the case of continuation around a
11274 wide-column character (See the comment in indent.c around
11275 line 885).
11276
11277 For instance, in the following case:
11278
11279 -------- Insert --------
11280 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11281 J_I_ ==> J_I_ `^^' are cursors.
11282 ^^ ^^
11283 -------- --------
11284
11285 As we have to redraw the line above, we should goto cancel. */
11286
11287 struct it it;
11288 int line_height_before = this_line_pixel_height;
11289
11290 /* Note that start_display will handle the case that the
11291 line starting at tlbufpos is a continuation lines. */
11292 start_display (&it, w, tlbufpos);
11293
11294 /* Implementation note: It this still necessary? */
11295 if (it.current_x != this_line_start_x)
11296 goto cancel;
11297
11298 TRACE ((stderr, "trying display optimization 1\n"));
11299 w->cursor.vpos = -1;
11300 overlay_arrow_seen = 0;
11301 it.vpos = this_line_vpos;
11302 it.current_y = this_line_y;
11303 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11304 display_line (&it);
11305
11306 /* If line contains point, is not continued,
11307 and ends at same distance from eob as before, we win */
11308 if (w->cursor.vpos >= 0
11309 /* Line is not continued, otherwise this_line_start_pos
11310 would have been set to 0 in display_line. */
11311 && CHARPOS (this_line_start_pos)
11312 /* Line ends as before. */
11313 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11314 /* Line has same height as before. Otherwise other lines
11315 would have to be shifted up or down. */
11316 && this_line_pixel_height == line_height_before)
11317 {
11318 /* If this is not the window's last line, we must adjust
11319 the charstarts of the lines below. */
11320 if (it.current_y < it.last_visible_y)
11321 {
11322 struct glyph_row *row
11323 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11324 int delta, delta_bytes;
11325
11326 if (Z - CHARPOS (tlendpos) == ZV)
11327 {
11328 /* This line ends at end of (accessible part of)
11329 buffer. There is no newline to count. */
11330 delta = (Z
11331 - CHARPOS (tlendpos)
11332 - MATRIX_ROW_START_CHARPOS (row));
11333 delta_bytes = (Z_BYTE
11334 - BYTEPOS (tlendpos)
11335 - MATRIX_ROW_START_BYTEPOS (row));
11336 }
11337 else
11338 {
11339 /* This line ends in a newline. Must take
11340 account of the newline and the rest of the
11341 text that follows. */
11342 delta = (Z
11343 - CHARPOS (tlendpos)
11344 - MATRIX_ROW_START_CHARPOS (row));
11345 delta_bytes = (Z_BYTE
11346 - BYTEPOS (tlendpos)
11347 - MATRIX_ROW_START_BYTEPOS (row));
11348 }
11349
11350 increment_matrix_positions (w->current_matrix,
11351 this_line_vpos + 1,
11352 w->current_matrix->nrows,
11353 delta, delta_bytes);
11354 }
11355
11356 /* If this row displays text now but previously didn't,
11357 or vice versa, w->window_end_vpos may have to be
11358 adjusted. */
11359 if ((it.glyph_row - 1)->displays_text_p)
11360 {
11361 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11362 XSETINT (w->window_end_vpos, this_line_vpos);
11363 }
11364 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11365 && this_line_vpos > 0)
11366 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11367 w->window_end_valid = Qnil;
11368
11369 /* Update hint: No need to try to scroll in update_window. */
11370 w->desired_matrix->no_scrolling_p = 1;
11371
11372 #if GLYPH_DEBUG
11373 *w->desired_matrix->method = 0;
11374 debug_method_add (w, "optimization 1");
11375 #endif
11376 #ifdef HAVE_WINDOW_SYSTEM
11377 update_window_fringes (w, 0);
11378 #endif
11379 goto update;
11380 }
11381 else
11382 goto cancel;
11383 }
11384 else if (/* Cursor position hasn't changed. */
11385 PT == XFASTINT (w->last_point)
11386 /* Make sure the cursor was last displayed
11387 in this window. Otherwise we have to reposition it. */
11388 && 0 <= w->cursor.vpos
11389 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11390 {
11391 if (!must_finish)
11392 {
11393 do_pending_window_change (1);
11394
11395 /* We used to always goto end_of_redisplay here, but this
11396 isn't enough if we have a blinking cursor. */
11397 if (w->cursor_off_p == w->last_cursor_off_p)
11398 goto end_of_redisplay;
11399 }
11400 goto update;
11401 }
11402 /* If highlighting the region, or if the cursor is in the echo area,
11403 then we can't just move the cursor. */
11404 else if (! (!NILP (Vtransient_mark_mode)
11405 && !NILP (current_buffer->mark_active))
11406 && (EQ (selected_window, current_buffer->last_selected_window)
11407 || highlight_nonselected_windows)
11408 && NILP (w->region_showing)
11409 && NILP (Vshow_trailing_whitespace)
11410 && !cursor_in_echo_area)
11411 {
11412 struct it it;
11413 struct glyph_row *row;
11414
11415 /* Skip from tlbufpos to PT and see where it is. Note that
11416 PT may be in invisible text. If so, we will end at the
11417 next visible position. */
11418 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11419 NULL, DEFAULT_FACE_ID);
11420 it.current_x = this_line_start_x;
11421 it.current_y = this_line_y;
11422 it.vpos = this_line_vpos;
11423
11424 /* The call to move_it_to stops in front of PT, but
11425 moves over before-strings. */
11426 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11427
11428 if (it.vpos == this_line_vpos
11429 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11430 row->enabled_p))
11431 {
11432 xassert (this_line_vpos == it.vpos);
11433 xassert (this_line_y == it.current_y);
11434 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11435 #if GLYPH_DEBUG
11436 *w->desired_matrix->method = 0;
11437 debug_method_add (w, "optimization 3");
11438 #endif
11439 goto update;
11440 }
11441 else
11442 goto cancel;
11443 }
11444
11445 cancel:
11446 /* Text changed drastically or point moved off of line. */
11447 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11448 }
11449
11450 CHARPOS (this_line_start_pos) = 0;
11451 consider_all_windows_p |= buffer_shared > 1;
11452 ++clear_face_cache_count;
11453 #ifdef HAVE_WINDOW_SYSTEM
11454 ++clear_image_cache_count;
11455 #endif
11456
11457 /* Build desired matrices, and update the display. If
11458 consider_all_windows_p is non-zero, do it for all windows on all
11459 frames. Otherwise do it for selected_window, only. */
11460
11461 if (consider_all_windows_p)
11462 {
11463 Lisp_Object tail, frame;
11464
11465 FOR_EACH_FRAME (tail, frame)
11466 XFRAME (frame)->updated_p = 0;
11467
11468 /* Recompute # windows showing selected buffer. This will be
11469 incremented each time such a window is displayed. */
11470 buffer_shared = 0;
11471
11472 FOR_EACH_FRAME (tail, frame)
11473 {
11474 struct frame *f = XFRAME (frame);
11475
11476 if (FRAME_WINDOW_P (f) || f == sf)
11477 {
11478 if (! EQ (frame, selected_frame))
11479 /* Select the frame, for the sake of frame-local
11480 variables. */
11481 select_frame_for_redisplay (frame);
11482
11483 /* Mark all the scroll bars to be removed; we'll redeem
11484 the ones we want when we redisplay their windows. */
11485 if (condemn_scroll_bars_hook)
11486 condemn_scroll_bars_hook (f);
11487
11488 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11489 redisplay_windows (FRAME_ROOT_WINDOW (f));
11490
11491 /* Any scroll bars which redisplay_windows should have
11492 nuked should now go away. */
11493 if (judge_scroll_bars_hook)
11494 judge_scroll_bars_hook (f);
11495
11496 /* If fonts changed, display again. */
11497 /* ??? rms: I suspect it is a mistake to jump all the way
11498 back to retry here. It should just retry this frame. */
11499 if (fonts_changed_p)
11500 goto retry;
11501
11502 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11503 {
11504 /* See if we have to hscroll. */
11505 if (!f->already_hscrolled_p)
11506 {
11507 f->already_hscrolled_p = 1;
11508 if (hscroll_windows (f->root_window))
11509 goto retry;
11510 }
11511
11512 /* Prevent various kinds of signals during display
11513 update. stdio is not robust about handling
11514 signals, which can cause an apparent I/O
11515 error. */
11516 if (interrupt_input)
11517 unrequest_sigio ();
11518 STOP_POLLING;
11519
11520 /* Update the display. */
11521 set_window_update_flags (XWINDOW (f->root_window), 1);
11522 pause |= update_frame (f, 0, 0);
11523 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11524 if (pause)
11525 break;
11526 #endif
11527
11528 f->updated_p = 1;
11529 }
11530 }
11531 }
11532
11533 if (!pause)
11534 {
11535 /* Do the mark_window_display_accurate after all windows have
11536 been redisplayed because this call resets flags in buffers
11537 which are needed for proper redisplay. */
11538 FOR_EACH_FRAME (tail, frame)
11539 {
11540 struct frame *f = XFRAME (frame);
11541 if (f->updated_p)
11542 {
11543 mark_window_display_accurate (f->root_window, 1);
11544 if (frame_up_to_date_hook)
11545 frame_up_to_date_hook (f);
11546 }
11547 }
11548 }
11549 }
11550 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11551 {
11552 Lisp_Object mini_window;
11553 struct frame *mini_frame;
11554
11555 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11556 /* Use list_of_error, not Qerror, so that
11557 we catch only errors and don't run the debugger. */
11558 internal_condition_case_1 (redisplay_window_1, selected_window,
11559 list_of_error,
11560 redisplay_window_error);
11561
11562 /* Compare desired and current matrices, perform output. */
11563
11564 update:
11565 /* If fonts changed, display again. */
11566 if (fonts_changed_p)
11567 goto retry;
11568
11569 /* Prevent various kinds of signals during display update.
11570 stdio is not robust about handling signals,
11571 which can cause an apparent I/O error. */
11572 if (interrupt_input)
11573 unrequest_sigio ();
11574 STOP_POLLING;
11575
11576 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11577 {
11578 if (hscroll_windows (selected_window))
11579 goto retry;
11580
11581 XWINDOW (selected_window)->must_be_updated_p = 1;
11582 pause = update_frame (sf, 0, 0);
11583 }
11584
11585 /* We may have called echo_area_display at the top of this
11586 function. If the echo area is on another frame, that may
11587 have put text on a frame other than the selected one, so the
11588 above call to update_frame would not have caught it. Catch
11589 it here. */
11590 mini_window = FRAME_MINIBUF_WINDOW (sf);
11591 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11592
11593 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11594 {
11595 XWINDOW (mini_window)->must_be_updated_p = 1;
11596 pause |= update_frame (mini_frame, 0, 0);
11597 if (!pause && hscroll_windows (mini_window))
11598 goto retry;
11599 }
11600 }
11601
11602 /* If display was paused because of pending input, make sure we do a
11603 thorough update the next time. */
11604 if (pause)
11605 {
11606 /* Prevent the optimization at the beginning of
11607 redisplay_internal that tries a single-line update of the
11608 line containing the cursor in the selected window. */
11609 CHARPOS (this_line_start_pos) = 0;
11610
11611 /* Let the overlay arrow be updated the next time. */
11612 update_overlay_arrows (0);
11613
11614 /* If we pause after scrolling, some rows in the current
11615 matrices of some windows are not valid. */
11616 if (!WINDOW_FULL_WIDTH_P (w)
11617 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11618 update_mode_lines = 1;
11619 }
11620 else
11621 {
11622 if (!consider_all_windows_p)
11623 {
11624 /* This has already been done above if
11625 consider_all_windows_p is set. */
11626 mark_window_display_accurate_1 (w, 1);
11627
11628 /* Say overlay arrows are up to date. */
11629 update_overlay_arrows (1);
11630
11631 if (frame_up_to_date_hook != 0)
11632 frame_up_to_date_hook (sf);
11633 }
11634
11635 update_mode_lines = 0;
11636 windows_or_buffers_changed = 0;
11637 cursor_type_changed = 0;
11638 }
11639
11640 /* Start SIGIO interrupts coming again. Having them off during the
11641 code above makes it less likely one will discard output, but not
11642 impossible, since there might be stuff in the system buffer here.
11643 But it is much hairier to try to do anything about that. */
11644 if (interrupt_input)
11645 request_sigio ();
11646 RESUME_POLLING;
11647
11648 /* If a frame has become visible which was not before, redisplay
11649 again, so that we display it. Expose events for such a frame
11650 (which it gets when becoming visible) don't call the parts of
11651 redisplay constructing glyphs, so simply exposing a frame won't
11652 display anything in this case. So, we have to display these
11653 frames here explicitly. */
11654 if (!pause)
11655 {
11656 Lisp_Object tail, frame;
11657 int new_count = 0;
11658
11659 FOR_EACH_FRAME (tail, frame)
11660 {
11661 int this_is_visible = 0;
11662
11663 if (XFRAME (frame)->visible)
11664 this_is_visible = 1;
11665 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11666 if (XFRAME (frame)->visible)
11667 this_is_visible = 1;
11668
11669 if (this_is_visible)
11670 new_count++;
11671 }
11672
11673 if (new_count != number_of_visible_frames)
11674 windows_or_buffers_changed++;
11675 }
11676
11677 /* Change frame size now if a change is pending. */
11678 do_pending_window_change (1);
11679
11680 /* If we just did a pending size change, or have additional
11681 visible frames, redisplay again. */
11682 if (windows_or_buffers_changed && !pause)
11683 goto retry;
11684
11685 /* Clear the face cache eventually. */
11686 if (consider_all_windows_p)
11687 {
11688 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11689 {
11690 clear_face_cache (0);
11691 clear_face_cache_count = 0;
11692 }
11693 #ifdef HAVE_WINDOW_SYSTEM
11694 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11695 {
11696 Lisp_Object tail, frame;
11697 FOR_EACH_FRAME (tail, frame)
11698 {
11699 struct frame *f = XFRAME (frame);
11700 if (FRAME_WINDOW_P (f))
11701 clear_image_cache (f, 0);
11702 }
11703 clear_image_cache_count = 0;
11704 }
11705 #endif /* HAVE_WINDOW_SYSTEM */
11706 }
11707
11708 end_of_redisplay:
11709 unbind_to (count, Qnil);
11710 RESUME_POLLING;
11711 }
11712
11713
11714 /* Redisplay, but leave alone any recent echo area message unless
11715 another message has been requested in its place.
11716
11717 This is useful in situations where you need to redisplay but no
11718 user action has occurred, making it inappropriate for the message
11719 area to be cleared. See tracking_off and
11720 wait_reading_process_output for examples of these situations.
11721
11722 FROM_WHERE is an integer saying from where this function was
11723 called. This is useful for debugging. */
11724
11725 void
11726 redisplay_preserve_echo_area (from_where)
11727 int from_where;
11728 {
11729 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11730
11731 if (!NILP (echo_area_buffer[1]))
11732 {
11733 /* We have a previously displayed message, but no current
11734 message. Redisplay the previous message. */
11735 display_last_displayed_message_p = 1;
11736 redisplay_internal (1);
11737 display_last_displayed_message_p = 0;
11738 }
11739 else
11740 redisplay_internal (1);
11741
11742 if (rif != NULL && rif->flush_display_optional)
11743 rif->flush_display_optional (NULL);
11744 }
11745
11746
11747 /* Function registered with record_unwind_protect in
11748 redisplay_internal. Reset redisplaying_p to the value it had
11749 before redisplay_internal was called, and clear
11750 prevent_freeing_realized_faces_p. It also selects the previously
11751 selected frame. */
11752
11753 static Lisp_Object
11754 unwind_redisplay (val)
11755 Lisp_Object val;
11756 {
11757 Lisp_Object old_redisplaying_p, old_frame;
11758
11759 old_redisplaying_p = XCAR (val);
11760 redisplaying_p = XFASTINT (old_redisplaying_p);
11761 old_frame = XCDR (val);
11762 if (! EQ (old_frame, selected_frame))
11763 select_frame_for_redisplay (old_frame);
11764 return Qnil;
11765 }
11766
11767
11768 /* Mark the display of window W as accurate or inaccurate. If
11769 ACCURATE_P is non-zero mark display of W as accurate. If
11770 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11771 redisplay_internal is called. */
11772
11773 static void
11774 mark_window_display_accurate_1 (w, accurate_p)
11775 struct window *w;
11776 int accurate_p;
11777 {
11778 if (BUFFERP (w->buffer))
11779 {
11780 struct buffer *b = XBUFFER (w->buffer);
11781
11782 w->last_modified
11783 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11784 w->last_overlay_modified
11785 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11786 w->last_had_star
11787 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11788
11789 if (accurate_p)
11790 {
11791 b->clip_changed = 0;
11792 b->prevent_redisplay_optimizations_p = 0;
11793
11794 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11795 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11796 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11797 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11798
11799 w->current_matrix->buffer = b;
11800 w->current_matrix->begv = BUF_BEGV (b);
11801 w->current_matrix->zv = BUF_ZV (b);
11802
11803 w->last_cursor = w->cursor;
11804 w->last_cursor_off_p = w->cursor_off_p;
11805
11806 if (w == XWINDOW (selected_window))
11807 w->last_point = make_number (BUF_PT (b));
11808 else
11809 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11810 }
11811 }
11812
11813 if (accurate_p)
11814 {
11815 w->window_end_valid = w->buffer;
11816 #if 0 /* This is incorrect with variable-height lines. */
11817 xassert (XINT (w->window_end_vpos)
11818 < (WINDOW_TOTAL_LINES (w)
11819 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11820 #endif
11821 w->update_mode_line = Qnil;
11822 }
11823 }
11824
11825
11826 /* Mark the display of windows in the window tree rooted at WINDOW as
11827 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11828 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11829 be redisplayed the next time redisplay_internal is called. */
11830
11831 void
11832 mark_window_display_accurate (window, accurate_p)
11833 Lisp_Object window;
11834 int accurate_p;
11835 {
11836 struct window *w;
11837
11838 for (; !NILP (window); window = w->next)
11839 {
11840 w = XWINDOW (window);
11841 mark_window_display_accurate_1 (w, accurate_p);
11842
11843 if (!NILP (w->vchild))
11844 mark_window_display_accurate (w->vchild, accurate_p);
11845 if (!NILP (w->hchild))
11846 mark_window_display_accurate (w->hchild, accurate_p);
11847 }
11848
11849 if (accurate_p)
11850 {
11851 update_overlay_arrows (1);
11852 }
11853 else
11854 {
11855 /* Force a thorough redisplay the next time by setting
11856 last_arrow_position and last_arrow_string to t, which is
11857 unequal to any useful value of Voverlay_arrow_... */
11858 update_overlay_arrows (-1);
11859 }
11860 }
11861
11862
11863 /* Return value in display table DP (Lisp_Char_Table *) for character
11864 C. Since a display table doesn't have any parent, we don't have to
11865 follow parent. Do not call this function directly but use the
11866 macro DISP_CHAR_VECTOR. */
11867
11868 Lisp_Object
11869 disp_char_vector (dp, c)
11870 struct Lisp_Char_Table *dp;
11871 int c;
11872 {
11873 Lisp_Object val;
11874
11875 if (ASCII_CHAR_P (c))
11876 {
11877 val = dp->ascii;
11878 if (SUB_CHAR_TABLE_P (val))
11879 val = XSUB_CHAR_TABLE (val)->contents[c];
11880 }
11881 else
11882 {
11883 Lisp_Object table;
11884
11885 XSETCHAR_TABLE (table, dp);
11886 val = char_table_ref (table, c);
11887 }
11888 if (NILP (val))
11889 val = dp->defalt;
11890 return val;
11891 }
11892
11893
11894 \f
11895 /***********************************************************************
11896 Window Redisplay
11897 ***********************************************************************/
11898
11899 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11900
11901 static void
11902 redisplay_windows (window)
11903 Lisp_Object window;
11904 {
11905 while (!NILP (window))
11906 {
11907 struct window *w = XWINDOW (window);
11908
11909 if (!NILP (w->hchild))
11910 redisplay_windows (w->hchild);
11911 else if (!NILP (w->vchild))
11912 redisplay_windows (w->vchild);
11913 else
11914 {
11915 displayed_buffer = XBUFFER (w->buffer);
11916 /* Use list_of_error, not Qerror, so that
11917 we catch only errors and don't run the debugger. */
11918 internal_condition_case_1 (redisplay_window_0, window,
11919 list_of_error,
11920 redisplay_window_error);
11921 }
11922
11923 window = w->next;
11924 }
11925 }
11926
11927 static Lisp_Object
11928 redisplay_window_error ()
11929 {
11930 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11931 return Qnil;
11932 }
11933
11934 static Lisp_Object
11935 redisplay_window_0 (window)
11936 Lisp_Object window;
11937 {
11938 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11939 redisplay_window (window, 0);
11940 return Qnil;
11941 }
11942
11943 static Lisp_Object
11944 redisplay_window_1 (window)
11945 Lisp_Object window;
11946 {
11947 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11948 redisplay_window (window, 1);
11949 return Qnil;
11950 }
11951 \f
11952
11953 /* Increment GLYPH until it reaches END or CONDITION fails while
11954 adding (GLYPH)->pixel_width to X. */
11955
11956 #define SKIP_GLYPHS(glyph, end, x, condition) \
11957 do \
11958 { \
11959 (x) += (glyph)->pixel_width; \
11960 ++(glyph); \
11961 } \
11962 while ((glyph) < (end) && (condition))
11963
11964
11965 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11966 DELTA is the number of bytes by which positions recorded in ROW
11967 differ from current buffer positions.
11968
11969 Return 0 if cursor is not on this row. 1 otherwise. */
11970
11971 int
11972 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11973 struct window *w;
11974 struct glyph_row *row;
11975 struct glyph_matrix *matrix;
11976 int delta, delta_bytes, dy, dvpos;
11977 {
11978 struct glyph *glyph = row->glyphs[TEXT_AREA];
11979 struct glyph *end = glyph + row->used[TEXT_AREA];
11980 struct glyph *cursor = NULL;
11981 /* The first glyph that starts a sequence of glyphs from string. */
11982 struct glyph *string_start;
11983 /* The X coordinate of string_start. */
11984 int string_start_x;
11985 /* The last known character position. */
11986 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11987 /* The last known character position before string_start. */
11988 int string_before_pos;
11989 int x = row->x;
11990 int cursor_x = x;
11991 int cursor_from_overlay_pos = 0;
11992 int pt_old = PT - delta;
11993
11994 /* Skip over glyphs not having an object at the start of the row.
11995 These are special glyphs like truncation marks on terminal
11996 frames. */
11997 if (row->displays_text_p)
11998 while (glyph < end
11999 && INTEGERP (glyph->object)
12000 && glyph->charpos < 0)
12001 {
12002 x += glyph->pixel_width;
12003 ++glyph;
12004 }
12005
12006 string_start = NULL;
12007 while (glyph < end
12008 && !INTEGERP (glyph->object)
12009 && (!BUFFERP (glyph->object)
12010 || (last_pos = glyph->charpos) < pt_old))
12011 {
12012 if (! STRINGP (glyph->object))
12013 {
12014 string_start = NULL;
12015 x += glyph->pixel_width;
12016 ++glyph;
12017 if (cursor_from_overlay_pos
12018 && last_pos >= cursor_from_overlay_pos)
12019 {
12020 cursor_from_overlay_pos = 0;
12021 cursor = 0;
12022 }
12023 }
12024 else
12025 {
12026 if (string_start == NULL)
12027 {
12028 string_before_pos = last_pos;
12029 string_start = glyph;
12030 string_start_x = x;
12031 }
12032 /* Skip all glyphs from string. */
12033 do
12034 {
12035 Lisp_Object cprop;
12036 int pos;
12037 if ((cursor == NULL || glyph > cursor)
12038 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12039 Qcursor, (glyph)->object),
12040 !NILP (cprop))
12041 && (pos = string_buffer_position (w, glyph->object,
12042 string_before_pos),
12043 (pos == 0 /* From overlay */
12044 || pos == pt_old)))
12045 {
12046 /* Estimate overlay buffer position from the buffer
12047 positions of the glyphs before and after the overlay.
12048 Add 1 to last_pos so that if point corresponds to the
12049 glyph right after the overlay, we still use a 'cursor'
12050 property found in that overlay. */
12051 cursor_from_overlay_pos = (pos ? 0 : last_pos
12052 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12053 cursor = glyph;
12054 cursor_x = x;
12055 }
12056 x += glyph->pixel_width;
12057 ++glyph;
12058 }
12059 while (glyph < end && EQ (glyph->object, string_start->object));
12060 }
12061 }
12062
12063 if (cursor != NULL)
12064 {
12065 glyph = cursor;
12066 x = cursor_x;
12067 }
12068 else if (row->ends_in_ellipsis_p && glyph == end)
12069 {
12070 /* Scan back over the ellipsis glyphs, decrementing positions. */
12071 while (glyph > row->glyphs[TEXT_AREA]
12072 && (glyph - 1)->charpos == last_pos)
12073 glyph--, x -= glyph->pixel_width;
12074 /* That loop always goes one position too far,
12075 including the glyph before the ellipsis.
12076 So scan forward over that one. */
12077 x += glyph->pixel_width;
12078 glyph++;
12079 }
12080 else if (string_start
12081 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12082 {
12083 /* We may have skipped over point because the previous glyphs
12084 are from string. As there's no easy way to know the
12085 character position of the current glyph, find the correct
12086 glyph on point by scanning from string_start again. */
12087 Lisp_Object limit;
12088 Lisp_Object string;
12089 struct glyph *stop = glyph;
12090 int pos;
12091
12092 limit = make_number (pt_old + 1);
12093 glyph = string_start;
12094 x = string_start_x;
12095 string = glyph->object;
12096 pos = string_buffer_position (w, string, string_before_pos);
12097 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12098 because we always put cursor after overlay strings. */
12099 while (pos == 0 && glyph < stop)
12100 {
12101 string = glyph->object;
12102 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12103 if (glyph < stop)
12104 pos = string_buffer_position (w, glyph->object, string_before_pos);
12105 }
12106
12107 while (glyph < stop)
12108 {
12109 pos = XINT (Fnext_single_char_property_change
12110 (make_number (pos), Qdisplay, Qnil, limit));
12111 if (pos > pt_old)
12112 break;
12113 /* Skip glyphs from the same string. */
12114 string = glyph->object;
12115 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12116 /* Skip glyphs from an overlay. */
12117 while (glyph < stop
12118 && ! string_buffer_position (w, glyph->object, pos))
12119 {
12120 string = glyph->object;
12121 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12122 }
12123 }
12124
12125 /* If we reached the end of the line, and end was from a string,
12126 cursor is not on this line. */
12127 if (glyph == end && row->continued_p)
12128 return 0;
12129 }
12130
12131 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12132 w->cursor.x = x;
12133 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12134 w->cursor.y = row->y + dy;
12135
12136 if (w == XWINDOW (selected_window))
12137 {
12138 if (!row->continued_p
12139 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12140 && row->x == 0)
12141 {
12142 this_line_buffer = XBUFFER (w->buffer);
12143
12144 CHARPOS (this_line_start_pos)
12145 = MATRIX_ROW_START_CHARPOS (row) + delta;
12146 BYTEPOS (this_line_start_pos)
12147 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12148
12149 CHARPOS (this_line_end_pos)
12150 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12151 BYTEPOS (this_line_end_pos)
12152 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12153
12154 this_line_y = w->cursor.y;
12155 this_line_pixel_height = row->height;
12156 this_line_vpos = w->cursor.vpos;
12157 this_line_start_x = row->x;
12158 }
12159 else
12160 CHARPOS (this_line_start_pos) = 0;
12161 }
12162
12163 return 1;
12164 }
12165
12166
12167 /* Run window scroll functions, if any, for WINDOW with new window
12168 start STARTP. Sets the window start of WINDOW to that position.
12169
12170 We assume that the window's buffer is really current. */
12171
12172 static INLINE struct text_pos
12173 run_window_scroll_functions (window, startp)
12174 Lisp_Object window;
12175 struct text_pos startp;
12176 {
12177 struct window *w = XWINDOW (window);
12178 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12179
12180 if (current_buffer != XBUFFER (w->buffer))
12181 abort ();
12182
12183 if (!NILP (Vwindow_scroll_functions))
12184 {
12185 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12186 make_number (CHARPOS (startp)));
12187 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12188 /* In case the hook functions switch buffers. */
12189 if (current_buffer != XBUFFER (w->buffer))
12190 set_buffer_internal_1 (XBUFFER (w->buffer));
12191 }
12192
12193 return startp;
12194 }
12195
12196
12197 /* Make sure the line containing the cursor is fully visible.
12198 A value of 1 means there is nothing to be done.
12199 (Either the line is fully visible, or it cannot be made so,
12200 or we cannot tell.)
12201
12202 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12203 is higher than window.
12204
12205 A value of 0 means the caller should do scrolling
12206 as if point had gone off the screen. */
12207
12208 static int
12209 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12210 struct window *w;
12211 int force_p;
12212 int current_matrix_p;
12213 {
12214 struct glyph_matrix *matrix;
12215 struct glyph_row *row;
12216 int window_height;
12217
12218 if (!make_cursor_line_fully_visible_p)
12219 return 1;
12220
12221 /* It's not always possible to find the cursor, e.g, when a window
12222 is full of overlay strings. Don't do anything in that case. */
12223 if (w->cursor.vpos < 0)
12224 return 1;
12225
12226 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12227 row = MATRIX_ROW (matrix, w->cursor.vpos);
12228
12229 /* If the cursor row is not partially visible, there's nothing to do. */
12230 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12231 return 1;
12232
12233 /* If the row the cursor is in is taller than the window's height,
12234 it's not clear what to do, so do nothing. */
12235 window_height = window_box_height (w);
12236 if (row->height >= window_height)
12237 {
12238 if (!force_p || MINI_WINDOW_P (w)
12239 || w->vscroll || w->cursor.vpos == 0)
12240 return 1;
12241 }
12242 return 0;
12243
12244 #if 0
12245 /* This code used to try to scroll the window just enough to make
12246 the line visible. It returned 0 to say that the caller should
12247 allocate larger glyph matrices. */
12248
12249 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12250 {
12251 int dy = row->height - row->visible_height;
12252 w->vscroll = 0;
12253 w->cursor.y += dy;
12254 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12255 }
12256 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12257 {
12258 int dy = - (row->height - row->visible_height);
12259 w->vscroll = dy;
12260 w->cursor.y += dy;
12261 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12262 }
12263
12264 /* When we change the cursor y-position of the selected window,
12265 change this_line_y as well so that the display optimization for
12266 the cursor line of the selected window in redisplay_internal uses
12267 the correct y-position. */
12268 if (w == XWINDOW (selected_window))
12269 this_line_y = w->cursor.y;
12270
12271 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12272 redisplay with larger matrices. */
12273 if (matrix->nrows < required_matrix_height (w))
12274 {
12275 fonts_changed_p = 1;
12276 return 0;
12277 }
12278
12279 return 1;
12280 #endif /* 0 */
12281 }
12282
12283
12284 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12285 non-zero means only WINDOW is redisplayed in redisplay_internal.
12286 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12287 in redisplay_window to bring a partially visible line into view in
12288 the case that only the cursor has moved.
12289
12290 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12291 last screen line's vertical height extends past the end of the screen.
12292
12293 Value is
12294
12295 1 if scrolling succeeded
12296
12297 0 if scrolling didn't find point.
12298
12299 -1 if new fonts have been loaded so that we must interrupt
12300 redisplay, adjust glyph matrices, and try again. */
12301
12302 enum
12303 {
12304 SCROLLING_SUCCESS,
12305 SCROLLING_FAILED,
12306 SCROLLING_NEED_LARGER_MATRICES
12307 };
12308
12309 static int
12310 try_scrolling (window, just_this_one_p, scroll_conservatively,
12311 scroll_step, temp_scroll_step, last_line_misfit)
12312 Lisp_Object window;
12313 int just_this_one_p;
12314 EMACS_INT scroll_conservatively, scroll_step;
12315 int temp_scroll_step;
12316 int last_line_misfit;
12317 {
12318 struct window *w = XWINDOW (window);
12319 struct frame *f = XFRAME (w->frame);
12320 struct text_pos scroll_margin_pos;
12321 struct text_pos pos;
12322 struct text_pos startp;
12323 struct it it;
12324 Lisp_Object window_end;
12325 int this_scroll_margin;
12326 int dy = 0;
12327 int scroll_max;
12328 int rc;
12329 int amount_to_scroll = 0;
12330 Lisp_Object aggressive;
12331 int height;
12332 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12333
12334 #if GLYPH_DEBUG
12335 debug_method_add (w, "try_scrolling");
12336 #endif
12337
12338 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12339
12340 /* Compute scroll margin height in pixels. We scroll when point is
12341 within this distance from the top or bottom of the window. */
12342 if (scroll_margin > 0)
12343 {
12344 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12345 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12346 }
12347 else
12348 this_scroll_margin = 0;
12349
12350 /* Force scroll_conservatively to have a reasonable value so it doesn't
12351 cause an overflow while computing how much to scroll. */
12352 if (scroll_conservatively)
12353 scroll_conservatively = min (scroll_conservatively,
12354 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12355
12356 /* Compute how much we should try to scroll maximally to bring point
12357 into view. */
12358 if (scroll_step || scroll_conservatively || temp_scroll_step)
12359 scroll_max = max (scroll_step,
12360 max (scroll_conservatively, temp_scroll_step));
12361 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12362 || NUMBERP (current_buffer->scroll_up_aggressively))
12363 /* We're trying to scroll because of aggressive scrolling
12364 but no scroll_step is set. Choose an arbitrary one. Maybe
12365 there should be a variable for this. */
12366 scroll_max = 10;
12367 else
12368 scroll_max = 0;
12369 scroll_max *= FRAME_LINE_HEIGHT (f);
12370
12371 /* Decide whether we have to scroll down. Start at the window end
12372 and move this_scroll_margin up to find the position of the scroll
12373 margin. */
12374 window_end = Fwindow_end (window, Qt);
12375
12376 too_near_end:
12377
12378 CHARPOS (scroll_margin_pos) = XINT (window_end);
12379 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12380
12381 if (this_scroll_margin || extra_scroll_margin_lines)
12382 {
12383 start_display (&it, w, scroll_margin_pos);
12384 if (this_scroll_margin)
12385 move_it_vertically_backward (&it, this_scroll_margin);
12386 if (extra_scroll_margin_lines)
12387 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12388 scroll_margin_pos = it.current.pos;
12389 }
12390
12391 if (PT >= CHARPOS (scroll_margin_pos))
12392 {
12393 int y0;
12394
12395 /* Point is in the scroll margin at the bottom of the window, or
12396 below. Compute a new window start that makes point visible. */
12397
12398 /* Compute the distance from the scroll margin to PT.
12399 Give up if the distance is greater than scroll_max. */
12400 start_display (&it, w, scroll_margin_pos);
12401 y0 = it.current_y;
12402 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12403 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12404
12405 /* To make point visible, we have to move the window start
12406 down so that the line the cursor is in is visible, which
12407 means we have to add in the height of the cursor line. */
12408 dy = line_bottom_y (&it) - y0;
12409
12410 if (dy > scroll_max)
12411 return SCROLLING_FAILED;
12412
12413 /* Move the window start down. If scrolling conservatively,
12414 move it just enough down to make point visible. If
12415 scroll_step is set, move it down by scroll_step. */
12416 start_display (&it, w, startp);
12417
12418 if (scroll_conservatively)
12419 /* Set AMOUNT_TO_SCROLL to at least one line,
12420 and at most scroll_conservatively lines. */
12421 amount_to_scroll
12422 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12423 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12424 else if (scroll_step || temp_scroll_step)
12425 amount_to_scroll = scroll_max;
12426 else
12427 {
12428 aggressive = current_buffer->scroll_up_aggressively;
12429 height = WINDOW_BOX_TEXT_HEIGHT (w);
12430 if (NUMBERP (aggressive))
12431 {
12432 double float_amount = XFLOATINT (aggressive) * height;
12433 amount_to_scroll = float_amount;
12434 if (amount_to_scroll == 0 && float_amount > 0)
12435 amount_to_scroll = 1;
12436 }
12437 }
12438
12439 if (amount_to_scroll <= 0)
12440 return SCROLLING_FAILED;
12441
12442 /* If moving by amount_to_scroll leaves STARTP unchanged,
12443 move it down one screen line. */
12444
12445 move_it_vertically (&it, amount_to_scroll);
12446 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12447 move_it_by_lines (&it, 1, 1);
12448 startp = it.current.pos;
12449 }
12450 else
12451 {
12452 /* See if point is inside the scroll margin at the top of the
12453 window. */
12454 scroll_margin_pos = startp;
12455 if (this_scroll_margin)
12456 {
12457 start_display (&it, w, startp);
12458 move_it_vertically (&it, this_scroll_margin);
12459 scroll_margin_pos = it.current.pos;
12460 }
12461
12462 if (PT < CHARPOS (scroll_margin_pos))
12463 {
12464 /* Point is in the scroll margin at the top of the window or
12465 above what is displayed in the window. */
12466 int y0;
12467
12468 /* Compute the vertical distance from PT to the scroll
12469 margin position. Give up if distance is greater than
12470 scroll_max. */
12471 SET_TEXT_POS (pos, PT, PT_BYTE);
12472 start_display (&it, w, pos);
12473 y0 = it.current_y;
12474 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12475 it.last_visible_y, -1,
12476 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12477 dy = it.current_y - y0;
12478 if (dy > scroll_max)
12479 return SCROLLING_FAILED;
12480
12481 /* Compute new window start. */
12482 start_display (&it, w, startp);
12483
12484 if (scroll_conservatively)
12485 amount_to_scroll
12486 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12487 else if (scroll_step || temp_scroll_step)
12488 amount_to_scroll = scroll_max;
12489 else
12490 {
12491 aggressive = current_buffer->scroll_down_aggressively;
12492 height = WINDOW_BOX_TEXT_HEIGHT (w);
12493 if (NUMBERP (aggressive))
12494 {
12495 double float_amount = XFLOATINT (aggressive) * height;
12496 amount_to_scroll = float_amount;
12497 if (amount_to_scroll == 0 && float_amount > 0)
12498 amount_to_scroll = 1;
12499 }
12500 }
12501
12502 if (amount_to_scroll <= 0)
12503 return SCROLLING_FAILED;
12504
12505 move_it_vertically_backward (&it, amount_to_scroll);
12506 startp = it.current.pos;
12507 }
12508 }
12509
12510 /* Run window scroll functions. */
12511 startp = run_window_scroll_functions (window, startp);
12512
12513 /* Display the window. Give up if new fonts are loaded, or if point
12514 doesn't appear. */
12515 if (!try_window (window, startp, 0))
12516 rc = SCROLLING_NEED_LARGER_MATRICES;
12517 else if (w->cursor.vpos < 0)
12518 {
12519 clear_glyph_matrix (w->desired_matrix);
12520 rc = SCROLLING_FAILED;
12521 }
12522 else
12523 {
12524 /* Maybe forget recorded base line for line number display. */
12525 if (!just_this_one_p
12526 || current_buffer->clip_changed
12527 || BEG_UNCHANGED < CHARPOS (startp))
12528 w->base_line_number = Qnil;
12529
12530 /* If cursor ends up on a partially visible line,
12531 treat that as being off the bottom of the screen. */
12532 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12533 {
12534 clear_glyph_matrix (w->desired_matrix);
12535 ++extra_scroll_margin_lines;
12536 goto too_near_end;
12537 }
12538 rc = SCROLLING_SUCCESS;
12539 }
12540
12541 return rc;
12542 }
12543
12544
12545 /* Compute a suitable window start for window W if display of W starts
12546 on a continuation line. Value is non-zero if a new window start
12547 was computed.
12548
12549 The new window start will be computed, based on W's width, starting
12550 from the start of the continued line. It is the start of the
12551 screen line with the minimum distance from the old start W->start. */
12552
12553 static int
12554 compute_window_start_on_continuation_line (w)
12555 struct window *w;
12556 {
12557 struct text_pos pos, start_pos;
12558 int window_start_changed_p = 0;
12559
12560 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12561
12562 /* If window start is on a continuation line... Window start may be
12563 < BEGV in case there's invisible text at the start of the
12564 buffer (M-x rmail, for example). */
12565 if (CHARPOS (start_pos) > BEGV
12566 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12567 {
12568 struct it it;
12569 struct glyph_row *row;
12570
12571 /* Handle the case that the window start is out of range. */
12572 if (CHARPOS (start_pos) < BEGV)
12573 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12574 else if (CHARPOS (start_pos) > ZV)
12575 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12576
12577 /* Find the start of the continued line. This should be fast
12578 because scan_buffer is fast (newline cache). */
12579 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12580 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12581 row, DEFAULT_FACE_ID);
12582 reseat_at_previous_visible_line_start (&it);
12583
12584 /* If the line start is "too far" away from the window start,
12585 say it takes too much time to compute a new window start. */
12586 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12587 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12588 {
12589 int min_distance, distance;
12590
12591 /* Move forward by display lines to find the new window
12592 start. If window width was enlarged, the new start can
12593 be expected to be > the old start. If window width was
12594 decreased, the new window start will be < the old start.
12595 So, we're looking for the display line start with the
12596 minimum distance from the old window start. */
12597 pos = it.current.pos;
12598 min_distance = INFINITY;
12599 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12600 distance < min_distance)
12601 {
12602 min_distance = distance;
12603 pos = it.current.pos;
12604 move_it_by_lines (&it, 1, 0);
12605 }
12606
12607 /* Set the window start there. */
12608 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12609 window_start_changed_p = 1;
12610 }
12611 }
12612
12613 return window_start_changed_p;
12614 }
12615
12616
12617 /* Try cursor movement in case text has not changed in window WINDOW,
12618 with window start STARTP. Value is
12619
12620 CURSOR_MOVEMENT_SUCCESS if successful
12621
12622 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12623
12624 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12625 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12626 we want to scroll as if scroll-step were set to 1. See the code.
12627
12628 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12629 which case we have to abort this redisplay, and adjust matrices
12630 first. */
12631
12632 enum
12633 {
12634 CURSOR_MOVEMENT_SUCCESS,
12635 CURSOR_MOVEMENT_CANNOT_BE_USED,
12636 CURSOR_MOVEMENT_MUST_SCROLL,
12637 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12638 };
12639
12640 static int
12641 try_cursor_movement (window, startp, scroll_step)
12642 Lisp_Object window;
12643 struct text_pos startp;
12644 int *scroll_step;
12645 {
12646 struct window *w = XWINDOW (window);
12647 struct frame *f = XFRAME (w->frame);
12648 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12649
12650 #if GLYPH_DEBUG
12651 if (inhibit_try_cursor_movement)
12652 return rc;
12653 #endif
12654
12655 /* Handle case where text has not changed, only point, and it has
12656 not moved off the frame. */
12657 if (/* Point may be in this window. */
12658 PT >= CHARPOS (startp)
12659 /* Selective display hasn't changed. */
12660 && !current_buffer->clip_changed
12661 /* Function force-mode-line-update is used to force a thorough
12662 redisplay. It sets either windows_or_buffers_changed or
12663 update_mode_lines. So don't take a shortcut here for these
12664 cases. */
12665 && !update_mode_lines
12666 && !windows_or_buffers_changed
12667 && !cursor_type_changed
12668 /* Can't use this case if highlighting a region. When a
12669 region exists, cursor movement has to do more than just
12670 set the cursor. */
12671 && !(!NILP (Vtransient_mark_mode)
12672 && !NILP (current_buffer->mark_active))
12673 && NILP (w->region_showing)
12674 && NILP (Vshow_trailing_whitespace)
12675 /* Right after splitting windows, last_point may be nil. */
12676 && INTEGERP (w->last_point)
12677 /* This code is not used for mini-buffer for the sake of the case
12678 of redisplaying to replace an echo area message; since in
12679 that case the mini-buffer contents per se are usually
12680 unchanged. This code is of no real use in the mini-buffer
12681 since the handling of this_line_start_pos, etc., in redisplay
12682 handles the same cases. */
12683 && !EQ (window, minibuf_window)
12684 /* When splitting windows or for new windows, it happens that
12685 redisplay is called with a nil window_end_vpos or one being
12686 larger than the window. This should really be fixed in
12687 window.c. I don't have this on my list, now, so we do
12688 approximately the same as the old redisplay code. --gerd. */
12689 && INTEGERP (w->window_end_vpos)
12690 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12691 && (FRAME_WINDOW_P (f)
12692 || !overlay_arrow_in_current_buffer_p ()))
12693 {
12694 int this_scroll_margin, top_scroll_margin;
12695 struct glyph_row *row = NULL;
12696
12697 #if GLYPH_DEBUG
12698 debug_method_add (w, "cursor movement");
12699 #endif
12700
12701 /* Scroll if point within this distance from the top or bottom
12702 of the window. This is a pixel value. */
12703 this_scroll_margin = max (0, scroll_margin);
12704 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12705 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12706
12707 top_scroll_margin = this_scroll_margin;
12708 if (WINDOW_WANTS_HEADER_LINE_P (w))
12709 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12710
12711 /* Start with the row the cursor was displayed during the last
12712 not paused redisplay. Give up if that row is not valid. */
12713 if (w->last_cursor.vpos < 0
12714 || w->last_cursor.vpos >= w->current_matrix->nrows)
12715 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12716 else
12717 {
12718 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12719 if (row->mode_line_p)
12720 ++row;
12721 if (!row->enabled_p)
12722 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12723 }
12724
12725 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12726 {
12727 int scroll_p = 0;
12728 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12729
12730 if (PT > XFASTINT (w->last_point))
12731 {
12732 /* Point has moved forward. */
12733 while (MATRIX_ROW_END_CHARPOS (row) < PT
12734 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12735 {
12736 xassert (row->enabled_p);
12737 ++row;
12738 }
12739
12740 /* The end position of a row equals the start position
12741 of the next row. If PT is there, we would rather
12742 display it in the next line. */
12743 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12744 && MATRIX_ROW_END_CHARPOS (row) == PT
12745 && !cursor_row_p (w, row))
12746 ++row;
12747
12748 /* If within the scroll margin, scroll. Note that
12749 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12750 the next line would be drawn, and that
12751 this_scroll_margin can be zero. */
12752 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12753 || PT > MATRIX_ROW_END_CHARPOS (row)
12754 /* Line is completely visible last line in window
12755 and PT is to be set in the next line. */
12756 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12757 && PT == MATRIX_ROW_END_CHARPOS (row)
12758 && !row->ends_at_zv_p
12759 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12760 scroll_p = 1;
12761 }
12762 else if (PT < XFASTINT (w->last_point))
12763 {
12764 /* Cursor has to be moved backward. Note that PT >=
12765 CHARPOS (startp) because of the outer if-statement. */
12766 while (!row->mode_line_p
12767 && (MATRIX_ROW_START_CHARPOS (row) > PT
12768 || (MATRIX_ROW_START_CHARPOS (row) == PT
12769 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12770 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12771 row > w->current_matrix->rows
12772 && (row-1)->ends_in_newline_from_string_p))))
12773 && (row->y > top_scroll_margin
12774 || CHARPOS (startp) == BEGV))
12775 {
12776 xassert (row->enabled_p);
12777 --row;
12778 }
12779
12780 /* Consider the following case: Window starts at BEGV,
12781 there is invisible, intangible text at BEGV, so that
12782 display starts at some point START > BEGV. It can
12783 happen that we are called with PT somewhere between
12784 BEGV and START. Try to handle that case. */
12785 if (row < w->current_matrix->rows
12786 || row->mode_line_p)
12787 {
12788 row = w->current_matrix->rows;
12789 if (row->mode_line_p)
12790 ++row;
12791 }
12792
12793 /* Due to newlines in overlay strings, we may have to
12794 skip forward over overlay strings. */
12795 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12796 && MATRIX_ROW_END_CHARPOS (row) == PT
12797 && !cursor_row_p (w, row))
12798 ++row;
12799
12800 /* If within the scroll margin, scroll. */
12801 if (row->y < top_scroll_margin
12802 && CHARPOS (startp) != BEGV)
12803 scroll_p = 1;
12804 }
12805 else
12806 {
12807 /* Cursor did not move. So don't scroll even if cursor line
12808 is partially visible, as it was so before. */
12809 rc = CURSOR_MOVEMENT_SUCCESS;
12810 }
12811
12812 if (PT < MATRIX_ROW_START_CHARPOS (row)
12813 || PT > MATRIX_ROW_END_CHARPOS (row))
12814 {
12815 /* if PT is not in the glyph row, give up. */
12816 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12817 }
12818 else if (rc != CURSOR_MOVEMENT_SUCCESS
12819 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12820 && make_cursor_line_fully_visible_p)
12821 {
12822 if (PT == MATRIX_ROW_END_CHARPOS (row)
12823 && !row->ends_at_zv_p
12824 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12825 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12826 else if (row->height > window_box_height (w))
12827 {
12828 /* If we end up in a partially visible line, let's
12829 make it fully visible, except when it's taller
12830 than the window, in which case we can't do much
12831 about it. */
12832 *scroll_step = 1;
12833 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12834 }
12835 else
12836 {
12837 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12838 if (!cursor_row_fully_visible_p (w, 0, 1))
12839 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12840 else
12841 rc = CURSOR_MOVEMENT_SUCCESS;
12842 }
12843 }
12844 else if (scroll_p)
12845 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12846 else
12847 {
12848 do
12849 {
12850 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12851 {
12852 rc = CURSOR_MOVEMENT_SUCCESS;
12853 break;
12854 }
12855 ++row;
12856 }
12857 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12858 && MATRIX_ROW_START_CHARPOS (row) == PT
12859 && cursor_row_p (w, row));
12860 }
12861 }
12862 }
12863
12864 return rc;
12865 }
12866
12867 void
12868 set_vertical_scroll_bar (w)
12869 struct window *w;
12870 {
12871 int start, end, whole;
12872
12873 /* Calculate the start and end positions for the current window.
12874 At some point, it would be nice to choose between scrollbars
12875 which reflect the whole buffer size, with special markers
12876 indicating narrowing, and scrollbars which reflect only the
12877 visible region.
12878
12879 Note that mini-buffers sometimes aren't displaying any text. */
12880 if (!MINI_WINDOW_P (w)
12881 || (w == XWINDOW (minibuf_window)
12882 && NILP (echo_area_buffer[0])))
12883 {
12884 struct buffer *buf = XBUFFER (w->buffer);
12885 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12886 start = marker_position (w->start) - BUF_BEGV (buf);
12887 /* I don't think this is guaranteed to be right. For the
12888 moment, we'll pretend it is. */
12889 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12890
12891 if (end < start)
12892 end = start;
12893 if (whole < (end - start))
12894 whole = end - start;
12895 }
12896 else
12897 start = end = whole = 0;
12898
12899 /* Indicate what this scroll bar ought to be displaying now. */
12900 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12901 }
12902
12903
12904 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12905 selected_window is redisplayed.
12906
12907 We can return without actually redisplaying the window if
12908 fonts_changed_p is nonzero. In that case, redisplay_internal will
12909 retry. */
12910
12911 static void
12912 redisplay_window (window, just_this_one_p)
12913 Lisp_Object window;
12914 int just_this_one_p;
12915 {
12916 struct window *w = XWINDOW (window);
12917 struct frame *f = XFRAME (w->frame);
12918 struct buffer *buffer = XBUFFER (w->buffer);
12919 struct buffer *old = current_buffer;
12920 struct text_pos lpoint, opoint, startp;
12921 int update_mode_line;
12922 int tem;
12923 struct it it;
12924 /* Record it now because it's overwritten. */
12925 int current_matrix_up_to_date_p = 0;
12926 int used_current_matrix_p = 0;
12927 /* This is less strict than current_matrix_up_to_date_p.
12928 It indictes that the buffer contents and narrowing are unchanged. */
12929 int buffer_unchanged_p = 0;
12930 int temp_scroll_step = 0;
12931 int count = SPECPDL_INDEX ();
12932 int rc;
12933 int centering_position = -1;
12934 int last_line_misfit = 0;
12935 int beg_unchanged, end_unchanged;
12936
12937 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12938 opoint = lpoint;
12939
12940 /* W must be a leaf window here. */
12941 xassert (!NILP (w->buffer));
12942 #if GLYPH_DEBUG
12943 *w->desired_matrix->method = 0;
12944 #endif
12945
12946 specbind (Qinhibit_point_motion_hooks, Qt);
12947
12948 reconsider_clip_changes (w, buffer);
12949
12950 /* Has the mode line to be updated? */
12951 update_mode_line = (!NILP (w->update_mode_line)
12952 || update_mode_lines
12953 || buffer->clip_changed
12954 || buffer->prevent_redisplay_optimizations_p);
12955
12956 if (MINI_WINDOW_P (w))
12957 {
12958 if (w == XWINDOW (echo_area_window)
12959 && !NILP (echo_area_buffer[0]))
12960 {
12961 if (update_mode_line)
12962 /* We may have to update a tty frame's menu bar or a
12963 tool-bar. Example `M-x C-h C-h C-g'. */
12964 goto finish_menu_bars;
12965 else
12966 /* We've already displayed the echo area glyphs in this window. */
12967 goto finish_scroll_bars;
12968 }
12969 else if ((w != XWINDOW (minibuf_window)
12970 || minibuf_level == 0)
12971 /* When buffer is nonempty, redisplay window normally. */
12972 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12973 /* Quail displays non-mini buffers in minibuffer window.
12974 In that case, redisplay the window normally. */
12975 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12976 {
12977 /* W is a mini-buffer window, but it's not active, so clear
12978 it. */
12979 int yb = window_text_bottom_y (w);
12980 struct glyph_row *row;
12981 int y;
12982
12983 for (y = 0, row = w->desired_matrix->rows;
12984 y < yb;
12985 y += row->height, ++row)
12986 blank_row (w, row, y);
12987 goto finish_scroll_bars;
12988 }
12989
12990 clear_glyph_matrix (w->desired_matrix);
12991 }
12992
12993 /* Otherwise set up data on this window; select its buffer and point
12994 value. */
12995 /* Really select the buffer, for the sake of buffer-local
12996 variables. */
12997 set_buffer_internal_1 (XBUFFER (w->buffer));
12998 SET_TEXT_POS (opoint, PT, PT_BYTE);
12999
13000 beg_unchanged = BEG_UNCHANGED;
13001 end_unchanged = END_UNCHANGED;
13002
13003 current_matrix_up_to_date_p
13004 = (!NILP (w->window_end_valid)
13005 && !current_buffer->clip_changed
13006 && !current_buffer->prevent_redisplay_optimizations_p
13007 && XFASTINT (w->last_modified) >= MODIFF
13008 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13009
13010 buffer_unchanged_p
13011 = (!NILP (w->window_end_valid)
13012 && !current_buffer->clip_changed
13013 && XFASTINT (w->last_modified) >= MODIFF
13014 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13015
13016 /* When windows_or_buffers_changed is non-zero, we can't rely on
13017 the window end being valid, so set it to nil there. */
13018 if (windows_or_buffers_changed)
13019 {
13020 /* If window starts on a continuation line, maybe adjust the
13021 window start in case the window's width changed. */
13022 if (XMARKER (w->start)->buffer == current_buffer)
13023 compute_window_start_on_continuation_line (w);
13024
13025 w->window_end_valid = Qnil;
13026 }
13027
13028 /* Some sanity checks. */
13029 CHECK_WINDOW_END (w);
13030 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13031 abort ();
13032 if (BYTEPOS (opoint) < CHARPOS (opoint))
13033 abort ();
13034
13035 /* If %c is in mode line, update it if needed. */
13036 if (!NILP (w->column_number_displayed)
13037 /* This alternative quickly identifies a common case
13038 where no change is needed. */
13039 && !(PT == XFASTINT (w->last_point)
13040 && XFASTINT (w->last_modified) >= MODIFF
13041 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13042 && (XFASTINT (w->column_number_displayed)
13043 != (int) current_column ())) /* iftc */
13044 update_mode_line = 1;
13045
13046 /* Count number of windows showing the selected buffer. An indirect
13047 buffer counts as its base buffer. */
13048 if (!just_this_one_p)
13049 {
13050 struct buffer *current_base, *window_base;
13051 current_base = current_buffer;
13052 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13053 if (current_base->base_buffer)
13054 current_base = current_base->base_buffer;
13055 if (window_base->base_buffer)
13056 window_base = window_base->base_buffer;
13057 if (current_base == window_base)
13058 buffer_shared++;
13059 }
13060
13061 /* Point refers normally to the selected window. For any other
13062 window, set up appropriate value. */
13063 if (!EQ (window, selected_window))
13064 {
13065 int new_pt = XMARKER (w->pointm)->charpos;
13066 int new_pt_byte = marker_byte_position (w->pointm);
13067 if (new_pt < BEGV)
13068 {
13069 new_pt = BEGV;
13070 new_pt_byte = BEGV_BYTE;
13071 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13072 }
13073 else if (new_pt > (ZV - 1))
13074 {
13075 new_pt = ZV;
13076 new_pt_byte = ZV_BYTE;
13077 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13078 }
13079
13080 /* We don't use SET_PT so that the point-motion hooks don't run. */
13081 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13082 }
13083
13084 /* If any of the character widths specified in the display table
13085 have changed, invalidate the width run cache. It's true that
13086 this may be a bit late to catch such changes, but the rest of
13087 redisplay goes (non-fatally) haywire when the display table is
13088 changed, so why should we worry about doing any better? */
13089 if (current_buffer->width_run_cache)
13090 {
13091 struct Lisp_Char_Table *disptab = buffer_display_table ();
13092
13093 if (! disptab_matches_widthtab (disptab,
13094 XVECTOR (current_buffer->width_table)))
13095 {
13096 invalidate_region_cache (current_buffer,
13097 current_buffer->width_run_cache,
13098 BEG, Z);
13099 recompute_width_table (current_buffer, disptab);
13100 }
13101 }
13102
13103 /* If window-start is screwed up, choose a new one. */
13104 if (XMARKER (w->start)->buffer != current_buffer)
13105 goto recenter;
13106
13107 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13108
13109 /* If someone specified a new starting point but did not insist,
13110 check whether it can be used. */
13111 if (!NILP (w->optional_new_start)
13112 && CHARPOS (startp) >= BEGV
13113 && CHARPOS (startp) <= ZV)
13114 {
13115 w->optional_new_start = Qnil;
13116 start_display (&it, w, startp);
13117 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13118 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13119 if (IT_CHARPOS (it) == PT)
13120 w->force_start = Qt;
13121 /* IT may overshoot PT if text at PT is invisible. */
13122 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13123 w->force_start = Qt;
13124 }
13125
13126 force_start:
13127
13128 /* Handle case where place to start displaying has been specified,
13129 unless the specified location is outside the accessible range. */
13130 if (!NILP (w->force_start)
13131 || w->frozen_window_start_p)
13132 {
13133 /* We set this later on if we have to adjust point. */
13134 int new_vpos = -1;
13135 int val;
13136
13137 w->force_start = Qnil;
13138 w->vscroll = 0;
13139 w->window_end_valid = Qnil;
13140
13141 /* Forget any recorded base line for line number display. */
13142 if (!buffer_unchanged_p)
13143 w->base_line_number = Qnil;
13144
13145 /* Redisplay the mode line. Select the buffer properly for that.
13146 Also, run the hook window-scroll-functions
13147 because we have scrolled. */
13148 /* Note, we do this after clearing force_start because
13149 if there's an error, it is better to forget about force_start
13150 than to get into an infinite loop calling the hook functions
13151 and having them get more errors. */
13152 if (!update_mode_line
13153 || ! NILP (Vwindow_scroll_functions))
13154 {
13155 update_mode_line = 1;
13156 w->update_mode_line = Qt;
13157 startp = run_window_scroll_functions (window, startp);
13158 }
13159
13160 w->last_modified = make_number (0);
13161 w->last_overlay_modified = make_number (0);
13162 if (CHARPOS (startp) < BEGV)
13163 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13164 else if (CHARPOS (startp) > ZV)
13165 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13166
13167 /* Redisplay, then check if cursor has been set during the
13168 redisplay. Give up if new fonts were loaded. */
13169 val = try_window (window, startp, 1);
13170 if (!val)
13171 {
13172 w->force_start = Qt;
13173 clear_glyph_matrix (w->desired_matrix);
13174 goto need_larger_matrices;
13175 }
13176 /* Point was outside the scroll margins. */
13177 if (val < 0)
13178 new_vpos = window_box_height (w) / 2;
13179
13180 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13181 {
13182 /* If point does not appear, try to move point so it does
13183 appear. The desired matrix has been built above, so we
13184 can use it here. */
13185 new_vpos = window_box_height (w) / 2;
13186 }
13187
13188 if (!cursor_row_fully_visible_p (w, 0, 0))
13189 {
13190 /* Point does appear, but on a line partly visible at end of window.
13191 Move it back to a fully-visible line. */
13192 new_vpos = window_box_height (w);
13193 }
13194
13195 /* If we need to move point for either of the above reasons,
13196 now actually do it. */
13197 if (new_vpos >= 0)
13198 {
13199 struct glyph_row *row;
13200
13201 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13202 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13203 ++row;
13204
13205 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13206 MATRIX_ROW_START_BYTEPOS (row));
13207
13208 if (w != XWINDOW (selected_window))
13209 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13210 else if (current_buffer == old)
13211 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13212
13213 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13214
13215 /* If we are highlighting the region, then we just changed
13216 the region, so redisplay to show it. */
13217 if (!NILP (Vtransient_mark_mode)
13218 && !NILP (current_buffer->mark_active))
13219 {
13220 clear_glyph_matrix (w->desired_matrix);
13221 if (!try_window (window, startp, 0))
13222 goto need_larger_matrices;
13223 }
13224 }
13225
13226 #if GLYPH_DEBUG
13227 debug_method_add (w, "forced window start");
13228 #endif
13229 goto done;
13230 }
13231
13232 /* Handle case where text has not changed, only point, and it has
13233 not moved off the frame, and we are not retrying after hscroll.
13234 (current_matrix_up_to_date_p is nonzero when retrying.) */
13235 if (current_matrix_up_to_date_p
13236 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13237 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13238 {
13239 switch (rc)
13240 {
13241 case CURSOR_MOVEMENT_SUCCESS:
13242 used_current_matrix_p = 1;
13243 goto done;
13244
13245 #if 0 /* try_cursor_movement never returns this value. */
13246 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13247 goto need_larger_matrices;
13248 #endif
13249
13250 case CURSOR_MOVEMENT_MUST_SCROLL:
13251 goto try_to_scroll;
13252
13253 default:
13254 abort ();
13255 }
13256 }
13257 /* If current starting point was originally the beginning of a line
13258 but no longer is, find a new starting point. */
13259 else if (!NILP (w->start_at_line_beg)
13260 && !(CHARPOS (startp) <= BEGV
13261 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13262 {
13263 #if GLYPH_DEBUG
13264 debug_method_add (w, "recenter 1");
13265 #endif
13266 goto recenter;
13267 }
13268
13269 /* Try scrolling with try_window_id. Value is > 0 if update has
13270 been done, it is -1 if we know that the same window start will
13271 not work. It is 0 if unsuccessful for some other reason. */
13272 else if ((tem = try_window_id (w)) != 0)
13273 {
13274 #if GLYPH_DEBUG
13275 debug_method_add (w, "try_window_id %d", tem);
13276 #endif
13277
13278 if (fonts_changed_p)
13279 goto need_larger_matrices;
13280 if (tem > 0)
13281 goto done;
13282
13283 /* Otherwise try_window_id has returned -1 which means that we
13284 don't want the alternative below this comment to execute. */
13285 }
13286 else if (CHARPOS (startp) >= BEGV
13287 && CHARPOS (startp) <= ZV
13288 && PT >= CHARPOS (startp)
13289 && (CHARPOS (startp) < ZV
13290 /* Avoid starting at end of buffer. */
13291 || CHARPOS (startp) == BEGV
13292 || (XFASTINT (w->last_modified) >= MODIFF
13293 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13294 {
13295
13296 /* If first window line is a continuation line, and window start
13297 is inside the modified region, but the first change is before
13298 current window start, we must select a new window start.
13299
13300 However, if this is the result of a down-mouse event (e.g. by
13301 extending the mouse-drag-overlay), we don't want to select a
13302 new window start, since that would change the position under
13303 the mouse, resulting in an unwanted mouse-movement rather
13304 than a simple mouse-click. */
13305 if (NILP (w->start_at_line_beg)
13306 && NILP (do_mouse_tracking)
13307 && CHARPOS (startp) > BEGV
13308 && CHARPOS (startp) > BEG + beg_unchanged
13309 && CHARPOS (startp) <= Z - end_unchanged)
13310 {
13311 w->force_start = Qt;
13312 if (XMARKER (w->start)->buffer == current_buffer)
13313 compute_window_start_on_continuation_line (w);
13314 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13315 goto force_start;
13316 }
13317
13318 #if GLYPH_DEBUG
13319 debug_method_add (w, "same window start");
13320 #endif
13321
13322 /* Try to redisplay starting at same place as before.
13323 If point has not moved off frame, accept the results. */
13324 if (!current_matrix_up_to_date_p
13325 /* Don't use try_window_reusing_current_matrix in this case
13326 because a window scroll function can have changed the
13327 buffer. */
13328 || !NILP (Vwindow_scroll_functions)
13329 || MINI_WINDOW_P (w)
13330 || !(used_current_matrix_p
13331 = try_window_reusing_current_matrix (w)))
13332 {
13333 IF_DEBUG (debug_method_add (w, "1"));
13334 if (try_window (window, startp, 1) < 0)
13335 /* -1 means we need to scroll.
13336 0 means we need new matrices, but fonts_changed_p
13337 is set in that case, so we will detect it below. */
13338 goto try_to_scroll;
13339 }
13340
13341 if (fonts_changed_p)
13342 goto need_larger_matrices;
13343
13344 if (w->cursor.vpos >= 0)
13345 {
13346 if (!just_this_one_p
13347 || current_buffer->clip_changed
13348 || BEG_UNCHANGED < CHARPOS (startp))
13349 /* Forget any recorded base line for line number display. */
13350 w->base_line_number = Qnil;
13351
13352 if (!cursor_row_fully_visible_p (w, 1, 0))
13353 {
13354 clear_glyph_matrix (w->desired_matrix);
13355 last_line_misfit = 1;
13356 }
13357 /* Drop through and scroll. */
13358 else
13359 goto done;
13360 }
13361 else
13362 clear_glyph_matrix (w->desired_matrix);
13363 }
13364
13365 try_to_scroll:
13366
13367 w->last_modified = make_number (0);
13368 w->last_overlay_modified = make_number (0);
13369
13370 /* Redisplay the mode line. Select the buffer properly for that. */
13371 if (!update_mode_line)
13372 {
13373 update_mode_line = 1;
13374 w->update_mode_line = Qt;
13375 }
13376
13377 /* Try to scroll by specified few lines. */
13378 if ((scroll_conservatively
13379 || scroll_step
13380 || temp_scroll_step
13381 || NUMBERP (current_buffer->scroll_up_aggressively)
13382 || NUMBERP (current_buffer->scroll_down_aggressively))
13383 && !current_buffer->clip_changed
13384 && CHARPOS (startp) >= BEGV
13385 && CHARPOS (startp) <= ZV)
13386 {
13387 /* The function returns -1 if new fonts were loaded, 1 if
13388 successful, 0 if not successful. */
13389 int rc = try_scrolling (window, just_this_one_p,
13390 scroll_conservatively,
13391 scroll_step,
13392 temp_scroll_step, last_line_misfit);
13393 switch (rc)
13394 {
13395 case SCROLLING_SUCCESS:
13396 goto done;
13397
13398 case SCROLLING_NEED_LARGER_MATRICES:
13399 goto need_larger_matrices;
13400
13401 case SCROLLING_FAILED:
13402 break;
13403
13404 default:
13405 abort ();
13406 }
13407 }
13408
13409 /* Finally, just choose place to start which centers point */
13410
13411 recenter:
13412 if (centering_position < 0)
13413 centering_position = window_box_height (w) / 2;
13414
13415 #if GLYPH_DEBUG
13416 debug_method_add (w, "recenter");
13417 #endif
13418
13419 /* w->vscroll = 0; */
13420
13421 /* Forget any previously recorded base line for line number display. */
13422 if (!buffer_unchanged_p)
13423 w->base_line_number = Qnil;
13424
13425 /* Move backward half the height of the window. */
13426 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13427 it.current_y = it.last_visible_y;
13428 move_it_vertically_backward (&it, centering_position);
13429 xassert (IT_CHARPOS (it) >= BEGV);
13430
13431 /* The function move_it_vertically_backward may move over more
13432 than the specified y-distance. If it->w is small, e.g. a
13433 mini-buffer window, we may end up in front of the window's
13434 display area. Start displaying at the start of the line
13435 containing PT in this case. */
13436 if (it.current_y <= 0)
13437 {
13438 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13439 move_it_vertically_backward (&it, 0);
13440 #if 0
13441 /* I think this assert is bogus if buffer contains
13442 invisible text or images. KFS. */
13443 xassert (IT_CHARPOS (it) <= PT);
13444 #endif
13445 it.current_y = 0;
13446 }
13447
13448 it.current_x = it.hpos = 0;
13449
13450 /* Set startp here explicitly in case that helps avoid an infinite loop
13451 in case the window-scroll-functions functions get errors. */
13452 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13453
13454 /* Run scroll hooks. */
13455 startp = run_window_scroll_functions (window, it.current.pos);
13456
13457 /* Redisplay the window. */
13458 if (!current_matrix_up_to_date_p
13459 || windows_or_buffers_changed
13460 || cursor_type_changed
13461 /* Don't use try_window_reusing_current_matrix in this case
13462 because it can have changed the buffer. */
13463 || !NILP (Vwindow_scroll_functions)
13464 || !just_this_one_p
13465 || MINI_WINDOW_P (w)
13466 || !(used_current_matrix_p
13467 = try_window_reusing_current_matrix (w)))
13468 try_window (window, startp, 0);
13469
13470 /* If new fonts have been loaded (due to fontsets), give up. We
13471 have to start a new redisplay since we need to re-adjust glyph
13472 matrices. */
13473 if (fonts_changed_p)
13474 goto need_larger_matrices;
13475
13476 /* If cursor did not appear assume that the middle of the window is
13477 in the first line of the window. Do it again with the next line.
13478 (Imagine a window of height 100, displaying two lines of height
13479 60. Moving back 50 from it->last_visible_y will end in the first
13480 line.) */
13481 if (w->cursor.vpos < 0)
13482 {
13483 if (!NILP (w->window_end_valid)
13484 && PT >= Z - XFASTINT (w->window_end_pos))
13485 {
13486 clear_glyph_matrix (w->desired_matrix);
13487 move_it_by_lines (&it, 1, 0);
13488 try_window (window, it.current.pos, 0);
13489 }
13490 else if (PT < IT_CHARPOS (it))
13491 {
13492 clear_glyph_matrix (w->desired_matrix);
13493 move_it_by_lines (&it, -1, 0);
13494 try_window (window, it.current.pos, 0);
13495 }
13496 else
13497 {
13498 /* Not much we can do about it. */
13499 }
13500 }
13501
13502 /* Consider the following case: Window starts at BEGV, there is
13503 invisible, intangible text at BEGV, so that display starts at
13504 some point START > BEGV. It can happen that we are called with
13505 PT somewhere between BEGV and START. Try to handle that case. */
13506 if (w->cursor.vpos < 0)
13507 {
13508 struct glyph_row *row = w->current_matrix->rows;
13509 if (row->mode_line_p)
13510 ++row;
13511 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13512 }
13513
13514 if (!cursor_row_fully_visible_p (w, 0, 0))
13515 {
13516 /* If vscroll is enabled, disable it and try again. */
13517 if (w->vscroll)
13518 {
13519 w->vscroll = 0;
13520 clear_glyph_matrix (w->desired_matrix);
13521 goto recenter;
13522 }
13523
13524 /* If centering point failed to make the whole line visible,
13525 put point at the top instead. That has to make the whole line
13526 visible, if it can be done. */
13527 if (centering_position == 0)
13528 goto done;
13529
13530 clear_glyph_matrix (w->desired_matrix);
13531 centering_position = 0;
13532 goto recenter;
13533 }
13534
13535 done:
13536
13537 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13538 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13539 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13540 ? Qt : Qnil);
13541
13542 /* Display the mode line, if we must. */
13543 if ((update_mode_line
13544 /* If window not full width, must redo its mode line
13545 if (a) the window to its side is being redone and
13546 (b) we do a frame-based redisplay. This is a consequence
13547 of how inverted lines are drawn in frame-based redisplay. */
13548 || (!just_this_one_p
13549 && !FRAME_WINDOW_P (f)
13550 && !WINDOW_FULL_WIDTH_P (w))
13551 /* Line number to display. */
13552 || INTEGERP (w->base_line_pos)
13553 /* Column number is displayed and different from the one displayed. */
13554 || (!NILP (w->column_number_displayed)
13555 && (XFASTINT (w->column_number_displayed)
13556 != (int) current_column ()))) /* iftc */
13557 /* This means that the window has a mode line. */
13558 && (WINDOW_WANTS_MODELINE_P (w)
13559 || WINDOW_WANTS_HEADER_LINE_P (w)))
13560 {
13561 display_mode_lines (w);
13562
13563 /* If mode line height has changed, arrange for a thorough
13564 immediate redisplay using the correct mode line height. */
13565 if (WINDOW_WANTS_MODELINE_P (w)
13566 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13567 {
13568 fonts_changed_p = 1;
13569 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13570 = DESIRED_MODE_LINE_HEIGHT (w);
13571 }
13572
13573 /* If top line height has changed, arrange for a thorough
13574 immediate redisplay using the correct mode line height. */
13575 if (WINDOW_WANTS_HEADER_LINE_P (w)
13576 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13577 {
13578 fonts_changed_p = 1;
13579 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13580 = DESIRED_HEADER_LINE_HEIGHT (w);
13581 }
13582
13583 if (fonts_changed_p)
13584 goto need_larger_matrices;
13585 }
13586
13587 if (!line_number_displayed
13588 && !BUFFERP (w->base_line_pos))
13589 {
13590 w->base_line_pos = Qnil;
13591 w->base_line_number = Qnil;
13592 }
13593
13594 finish_menu_bars:
13595
13596 /* When we reach a frame's selected window, redo the frame's menu bar. */
13597 if (update_mode_line
13598 && EQ (FRAME_SELECTED_WINDOW (f), window))
13599 {
13600 int redisplay_menu_p = 0;
13601 int redisplay_tool_bar_p = 0;
13602
13603 if (FRAME_WINDOW_P (f))
13604 {
13605 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13606 || defined (USE_GTK)
13607 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13608 #else
13609 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13610 #endif
13611 }
13612 else
13613 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13614
13615 if (redisplay_menu_p)
13616 display_menu_bar (w);
13617
13618 #ifdef HAVE_WINDOW_SYSTEM
13619 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13620 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13621 #else
13622 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13623 && (FRAME_TOOL_BAR_LINES (f) > 0
13624 || !NILP (Vauto_resize_tool_bars));
13625
13626 #endif
13627
13628 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13629 {
13630 extern int ignore_mouse_drag_p;
13631 ignore_mouse_drag_p = 1;
13632 }
13633 #endif
13634 }
13635
13636 #ifdef HAVE_WINDOW_SYSTEM
13637 if (FRAME_WINDOW_P (f)
13638 && update_window_fringes (w, (just_this_one_p
13639 || (!used_current_matrix_p && !overlay_arrow_seen)
13640 || w->pseudo_window_p)))
13641 {
13642 update_begin (f);
13643 BLOCK_INPUT;
13644 if (draw_window_fringes (w, 1))
13645 x_draw_vertical_border (w);
13646 UNBLOCK_INPUT;
13647 update_end (f);
13648 }
13649 #endif /* HAVE_WINDOW_SYSTEM */
13650
13651 /* We go to this label, with fonts_changed_p nonzero,
13652 if it is necessary to try again using larger glyph matrices.
13653 We have to redeem the scroll bar even in this case,
13654 because the loop in redisplay_internal expects that. */
13655 need_larger_matrices:
13656 ;
13657 finish_scroll_bars:
13658
13659 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13660 {
13661 /* Set the thumb's position and size. */
13662 set_vertical_scroll_bar (w);
13663
13664 /* Note that we actually used the scroll bar attached to this
13665 window, so it shouldn't be deleted at the end of redisplay. */
13666 redeem_scroll_bar_hook (w);
13667 }
13668
13669 /* Restore current_buffer and value of point in it. */
13670 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13671 set_buffer_internal_1 (old);
13672 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13673
13674 unbind_to (count, Qnil);
13675 }
13676
13677
13678 /* Build the complete desired matrix of WINDOW with a window start
13679 buffer position POS.
13680
13681 Value is 1 if successful. It is zero if fonts were loaded during
13682 redisplay which makes re-adjusting glyph matrices necessary, and -1
13683 if point would appear in the scroll margins.
13684 (We check that only if CHECK_MARGINS is nonzero. */
13685
13686 int
13687 try_window (window, pos, check_margins)
13688 Lisp_Object window;
13689 struct text_pos pos;
13690 int check_margins;
13691 {
13692 struct window *w = XWINDOW (window);
13693 struct it it;
13694 struct glyph_row *last_text_row = NULL;
13695 struct frame *f = XFRAME (w->frame);
13696
13697 /* Make POS the new window start. */
13698 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13699
13700 /* Mark cursor position as unknown. No overlay arrow seen. */
13701 w->cursor.vpos = -1;
13702 overlay_arrow_seen = 0;
13703
13704 /* Initialize iterator and info to start at POS. */
13705 start_display (&it, w, pos);
13706
13707 /* Display all lines of W. */
13708 while (it.current_y < it.last_visible_y)
13709 {
13710 if (display_line (&it))
13711 last_text_row = it.glyph_row - 1;
13712 if (fonts_changed_p)
13713 return 0;
13714 }
13715
13716 /* Don't let the cursor end in the scroll margins. */
13717 if (check_margins
13718 && !MINI_WINDOW_P (w))
13719 {
13720 int this_scroll_margin;
13721
13722 this_scroll_margin = max (0, scroll_margin);
13723 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13724 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13725
13726 if ((w->cursor.y >= 0 /* not vscrolled */
13727 && w->cursor.y < this_scroll_margin
13728 && CHARPOS (pos) > BEGV
13729 && IT_CHARPOS (it) < ZV)
13730 /* rms: considering make_cursor_line_fully_visible_p here
13731 seems to give wrong results. We don't want to recenter
13732 when the last line is partly visible, we want to allow
13733 that case to be handled in the usual way. */
13734 || (w->cursor.y + 1) > it.last_visible_y)
13735 {
13736 w->cursor.vpos = -1;
13737 clear_glyph_matrix (w->desired_matrix);
13738 return -1;
13739 }
13740 }
13741
13742 /* If bottom moved off end of frame, change mode line percentage. */
13743 if (XFASTINT (w->window_end_pos) <= 0
13744 && Z != IT_CHARPOS (it))
13745 w->update_mode_line = Qt;
13746
13747 /* Set window_end_pos to the offset of the last character displayed
13748 on the window from the end of current_buffer. Set
13749 window_end_vpos to its row number. */
13750 if (last_text_row)
13751 {
13752 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13753 w->window_end_bytepos
13754 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13755 w->window_end_pos
13756 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13757 w->window_end_vpos
13758 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13759 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13760 ->displays_text_p);
13761 }
13762 else
13763 {
13764 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13765 w->window_end_pos = make_number (Z - ZV);
13766 w->window_end_vpos = make_number (0);
13767 }
13768
13769 /* But that is not valid info until redisplay finishes. */
13770 w->window_end_valid = Qnil;
13771 return 1;
13772 }
13773
13774
13775 \f
13776 /************************************************************************
13777 Window redisplay reusing current matrix when buffer has not changed
13778 ************************************************************************/
13779
13780 /* Try redisplay of window W showing an unchanged buffer with a
13781 different window start than the last time it was displayed by
13782 reusing its current matrix. Value is non-zero if successful.
13783 W->start is the new window start. */
13784
13785 static int
13786 try_window_reusing_current_matrix (w)
13787 struct window *w;
13788 {
13789 struct frame *f = XFRAME (w->frame);
13790 struct glyph_row *row, *bottom_row;
13791 struct it it;
13792 struct run run;
13793 struct text_pos start, new_start;
13794 int nrows_scrolled, i;
13795 struct glyph_row *last_text_row;
13796 struct glyph_row *last_reused_text_row;
13797 struct glyph_row *start_row;
13798 int start_vpos, min_y, max_y;
13799
13800 #if GLYPH_DEBUG
13801 if (inhibit_try_window_reusing)
13802 return 0;
13803 #endif
13804
13805 if (/* This function doesn't handle terminal frames. */
13806 !FRAME_WINDOW_P (f)
13807 /* Don't try to reuse the display if windows have been split
13808 or such. */
13809 || windows_or_buffers_changed
13810 || cursor_type_changed)
13811 return 0;
13812
13813 /* Can't do this if region may have changed. */
13814 if ((!NILP (Vtransient_mark_mode)
13815 && !NILP (current_buffer->mark_active))
13816 || !NILP (w->region_showing)
13817 || !NILP (Vshow_trailing_whitespace))
13818 return 0;
13819
13820 /* If top-line visibility has changed, give up. */
13821 if (WINDOW_WANTS_HEADER_LINE_P (w)
13822 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13823 return 0;
13824
13825 /* Give up if old or new display is scrolled vertically. We could
13826 make this function handle this, but right now it doesn't. */
13827 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13828 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13829 return 0;
13830
13831 /* The variable new_start now holds the new window start. The old
13832 start `start' can be determined from the current matrix. */
13833 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13834 start = start_row->start.pos;
13835 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13836
13837 /* Clear the desired matrix for the display below. */
13838 clear_glyph_matrix (w->desired_matrix);
13839
13840 if (CHARPOS (new_start) <= CHARPOS (start))
13841 {
13842 int first_row_y;
13843
13844 /* Don't use this method if the display starts with an ellipsis
13845 displayed for invisible text. It's not easy to handle that case
13846 below, and it's certainly not worth the effort since this is
13847 not a frequent case. */
13848 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13849 return 0;
13850
13851 IF_DEBUG (debug_method_add (w, "twu1"));
13852
13853 /* Display up to a row that can be reused. The variable
13854 last_text_row is set to the last row displayed that displays
13855 text. Note that it.vpos == 0 if or if not there is a
13856 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13857 start_display (&it, w, new_start);
13858 first_row_y = it.current_y;
13859 w->cursor.vpos = -1;
13860 last_text_row = last_reused_text_row = NULL;
13861
13862 while (it.current_y < it.last_visible_y
13863 && !fonts_changed_p)
13864 {
13865 /* If we have reached into the characters in the START row,
13866 that means the line boundaries have changed. So we
13867 can't start copying with the row START. Maybe it will
13868 work to start copying with the following row. */
13869 while (IT_CHARPOS (it) > CHARPOS (start))
13870 {
13871 /* Advance to the next row as the "start". */
13872 start_row++;
13873 start = start_row->start.pos;
13874 /* If there are no more rows to try, or just one, give up. */
13875 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13876 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13877 || CHARPOS (start) == ZV)
13878 {
13879 clear_glyph_matrix (w->desired_matrix);
13880 return 0;
13881 }
13882
13883 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13884 }
13885 /* If we have reached alignment,
13886 we can copy the rest of the rows. */
13887 if (IT_CHARPOS (it) == CHARPOS (start))
13888 break;
13889
13890 if (display_line (&it))
13891 last_text_row = it.glyph_row - 1;
13892 }
13893
13894 /* A value of current_y < last_visible_y means that we stopped
13895 at the previous window start, which in turn means that we
13896 have at least one reusable row. */
13897 if (it.current_y < it.last_visible_y)
13898 {
13899 /* IT.vpos always starts from 0; it counts text lines. */
13900 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13901
13902 /* Find PT if not already found in the lines displayed. */
13903 if (w->cursor.vpos < 0)
13904 {
13905 int dy = it.current_y - start_row->y;
13906
13907 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13908 row = row_containing_pos (w, PT, row, NULL, dy);
13909 if (row)
13910 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13911 dy, nrows_scrolled);
13912 else
13913 {
13914 clear_glyph_matrix (w->desired_matrix);
13915 return 0;
13916 }
13917 }
13918
13919 /* Scroll the display. Do it before the current matrix is
13920 changed. The problem here is that update has not yet
13921 run, i.e. part of the current matrix is not up to date.
13922 scroll_run_hook will clear the cursor, and use the
13923 current matrix to get the height of the row the cursor is
13924 in. */
13925 run.current_y = start_row->y;
13926 run.desired_y = it.current_y;
13927 run.height = it.last_visible_y - it.current_y;
13928
13929 if (run.height > 0 && run.current_y != run.desired_y)
13930 {
13931 update_begin (f);
13932 rif->update_window_begin_hook (w);
13933 rif->clear_window_mouse_face (w);
13934 rif->scroll_run_hook (w, &run);
13935 rif->update_window_end_hook (w, 0, 0);
13936 update_end (f);
13937 }
13938
13939 /* Shift current matrix down by nrows_scrolled lines. */
13940 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13941 rotate_matrix (w->current_matrix,
13942 start_vpos,
13943 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13944 nrows_scrolled);
13945
13946 /* Disable lines that must be updated. */
13947 for (i = 0; i < nrows_scrolled; ++i)
13948 (start_row + i)->enabled_p = 0;
13949
13950 /* Re-compute Y positions. */
13951 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13952 max_y = it.last_visible_y;
13953 for (row = start_row + nrows_scrolled;
13954 row < bottom_row;
13955 ++row)
13956 {
13957 row->y = it.current_y;
13958 row->visible_height = row->height;
13959
13960 if (row->y < min_y)
13961 row->visible_height -= min_y - row->y;
13962 if (row->y + row->height > max_y)
13963 row->visible_height -= row->y + row->height - max_y;
13964 row->redraw_fringe_bitmaps_p = 1;
13965
13966 it.current_y += row->height;
13967
13968 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13969 last_reused_text_row = row;
13970 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13971 break;
13972 }
13973
13974 /* Disable lines in the current matrix which are now
13975 below the window. */
13976 for (++row; row < bottom_row; ++row)
13977 row->enabled_p = row->mode_line_p = 0;
13978 }
13979
13980 /* Update window_end_pos etc.; last_reused_text_row is the last
13981 reused row from the current matrix containing text, if any.
13982 The value of last_text_row is the last displayed line
13983 containing text. */
13984 if (last_reused_text_row)
13985 {
13986 w->window_end_bytepos
13987 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13988 w->window_end_pos
13989 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13990 w->window_end_vpos
13991 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13992 w->current_matrix));
13993 }
13994 else if (last_text_row)
13995 {
13996 w->window_end_bytepos
13997 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13998 w->window_end_pos
13999 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14000 w->window_end_vpos
14001 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14002 }
14003 else
14004 {
14005 /* This window must be completely empty. */
14006 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14007 w->window_end_pos = make_number (Z - ZV);
14008 w->window_end_vpos = make_number (0);
14009 }
14010 w->window_end_valid = Qnil;
14011
14012 /* Update hint: don't try scrolling again in update_window. */
14013 w->desired_matrix->no_scrolling_p = 1;
14014
14015 #if GLYPH_DEBUG
14016 debug_method_add (w, "try_window_reusing_current_matrix 1");
14017 #endif
14018 return 1;
14019 }
14020 else if (CHARPOS (new_start) > CHARPOS (start))
14021 {
14022 struct glyph_row *pt_row, *row;
14023 struct glyph_row *first_reusable_row;
14024 struct glyph_row *first_row_to_display;
14025 int dy;
14026 int yb = window_text_bottom_y (w);
14027
14028 /* Find the row starting at new_start, if there is one. Don't
14029 reuse a partially visible line at the end. */
14030 first_reusable_row = start_row;
14031 while (first_reusable_row->enabled_p
14032 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14033 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14034 < CHARPOS (new_start)))
14035 ++first_reusable_row;
14036
14037 /* Give up if there is no row to reuse. */
14038 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14039 || !first_reusable_row->enabled_p
14040 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14041 != CHARPOS (new_start)))
14042 return 0;
14043
14044 /* We can reuse fully visible rows beginning with
14045 first_reusable_row to the end of the window. Set
14046 first_row_to_display to the first row that cannot be reused.
14047 Set pt_row to the row containing point, if there is any. */
14048 pt_row = NULL;
14049 for (first_row_to_display = first_reusable_row;
14050 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14051 ++first_row_to_display)
14052 {
14053 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14054 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14055 pt_row = first_row_to_display;
14056 }
14057
14058 /* Start displaying at the start of first_row_to_display. */
14059 xassert (first_row_to_display->y < yb);
14060 init_to_row_start (&it, w, first_row_to_display);
14061
14062 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14063 - start_vpos);
14064 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14065 - nrows_scrolled);
14066 it.current_y = (first_row_to_display->y - first_reusable_row->y
14067 + WINDOW_HEADER_LINE_HEIGHT (w));
14068
14069 /* Display lines beginning with first_row_to_display in the
14070 desired matrix. Set last_text_row to the last row displayed
14071 that displays text. */
14072 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14073 if (pt_row == NULL)
14074 w->cursor.vpos = -1;
14075 last_text_row = NULL;
14076 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14077 if (display_line (&it))
14078 last_text_row = it.glyph_row - 1;
14079
14080 /* Give up If point isn't in a row displayed or reused. */
14081 if (w->cursor.vpos < 0)
14082 {
14083 clear_glyph_matrix (w->desired_matrix);
14084 return 0;
14085 }
14086
14087 /* If point is in a reused row, adjust y and vpos of the cursor
14088 position. */
14089 if (pt_row)
14090 {
14091 w->cursor.vpos -= nrows_scrolled;
14092 w->cursor.y -= first_reusable_row->y - start_row->y;
14093 }
14094
14095 /* Scroll the display. */
14096 run.current_y = first_reusable_row->y;
14097 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14098 run.height = it.last_visible_y - run.current_y;
14099 dy = run.current_y - run.desired_y;
14100
14101 if (run.height)
14102 {
14103 update_begin (f);
14104 rif->update_window_begin_hook (w);
14105 rif->clear_window_mouse_face (w);
14106 rif->scroll_run_hook (w, &run);
14107 rif->update_window_end_hook (w, 0, 0);
14108 update_end (f);
14109 }
14110
14111 /* Adjust Y positions of reused rows. */
14112 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14113 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14114 max_y = it.last_visible_y;
14115 for (row = first_reusable_row; row < first_row_to_display; ++row)
14116 {
14117 row->y -= dy;
14118 row->visible_height = row->height;
14119 if (row->y < min_y)
14120 row->visible_height -= min_y - row->y;
14121 if (row->y + row->height > max_y)
14122 row->visible_height -= row->y + row->height - max_y;
14123 row->redraw_fringe_bitmaps_p = 1;
14124 }
14125
14126 /* Scroll the current matrix. */
14127 xassert (nrows_scrolled > 0);
14128 rotate_matrix (w->current_matrix,
14129 start_vpos,
14130 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14131 -nrows_scrolled);
14132
14133 /* Disable rows not reused. */
14134 for (row -= nrows_scrolled; row < bottom_row; ++row)
14135 row->enabled_p = 0;
14136
14137 /* Point may have moved to a different line, so we cannot assume that
14138 the previous cursor position is valid; locate the correct row. */
14139 if (pt_row)
14140 {
14141 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14142 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14143 row++)
14144 {
14145 w->cursor.vpos++;
14146 w->cursor.y = row->y;
14147 }
14148 if (row < bottom_row)
14149 {
14150 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14151 while (glyph->charpos < PT)
14152 {
14153 w->cursor.hpos++;
14154 w->cursor.x += glyph->pixel_width;
14155 glyph++;
14156 }
14157 }
14158 }
14159
14160 /* Adjust window end. A null value of last_text_row means that
14161 the window end is in reused rows which in turn means that
14162 only its vpos can have changed. */
14163 if (last_text_row)
14164 {
14165 w->window_end_bytepos
14166 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14167 w->window_end_pos
14168 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14169 w->window_end_vpos
14170 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14171 }
14172 else
14173 {
14174 w->window_end_vpos
14175 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14176 }
14177
14178 w->window_end_valid = Qnil;
14179 w->desired_matrix->no_scrolling_p = 1;
14180
14181 #if GLYPH_DEBUG
14182 debug_method_add (w, "try_window_reusing_current_matrix 2");
14183 #endif
14184 return 1;
14185 }
14186
14187 return 0;
14188 }
14189
14190
14191 \f
14192 /************************************************************************
14193 Window redisplay reusing current matrix when buffer has changed
14194 ************************************************************************/
14195
14196 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14197 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14198 int *, int *));
14199 static struct glyph_row *
14200 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14201 struct glyph_row *));
14202
14203
14204 /* Return the last row in MATRIX displaying text. If row START is
14205 non-null, start searching with that row. IT gives the dimensions
14206 of the display. Value is null if matrix is empty; otherwise it is
14207 a pointer to the row found. */
14208
14209 static struct glyph_row *
14210 find_last_row_displaying_text (matrix, it, start)
14211 struct glyph_matrix *matrix;
14212 struct it *it;
14213 struct glyph_row *start;
14214 {
14215 struct glyph_row *row, *row_found;
14216
14217 /* Set row_found to the last row in IT->w's current matrix
14218 displaying text. The loop looks funny but think of partially
14219 visible lines. */
14220 row_found = NULL;
14221 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14222 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14223 {
14224 xassert (row->enabled_p);
14225 row_found = row;
14226 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14227 break;
14228 ++row;
14229 }
14230
14231 return row_found;
14232 }
14233
14234
14235 /* Return the last row in the current matrix of W that is not affected
14236 by changes at the start of current_buffer that occurred since W's
14237 current matrix was built. Value is null if no such row exists.
14238
14239 BEG_UNCHANGED us the number of characters unchanged at the start of
14240 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14241 first changed character in current_buffer. Characters at positions <
14242 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14243 when the current matrix was built. */
14244
14245 static struct glyph_row *
14246 find_last_unchanged_at_beg_row (w)
14247 struct window *w;
14248 {
14249 int first_changed_pos = BEG + BEG_UNCHANGED;
14250 struct glyph_row *row;
14251 struct glyph_row *row_found = NULL;
14252 int yb = window_text_bottom_y (w);
14253
14254 /* Find the last row displaying unchanged text. */
14255 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14256 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14257 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14258 {
14259 if (/* If row ends before first_changed_pos, it is unchanged,
14260 except in some case. */
14261 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14262 /* When row ends in ZV and we write at ZV it is not
14263 unchanged. */
14264 && !row->ends_at_zv_p
14265 /* When first_changed_pos is the end of a continued line,
14266 row is not unchanged because it may be no longer
14267 continued. */
14268 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14269 && (row->continued_p
14270 || row->exact_window_width_line_p)))
14271 row_found = row;
14272
14273 /* Stop if last visible row. */
14274 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14275 break;
14276
14277 ++row;
14278 }
14279
14280 return row_found;
14281 }
14282
14283
14284 /* Find the first glyph row in the current matrix of W that is not
14285 affected by changes at the end of current_buffer since the
14286 time W's current matrix was built.
14287
14288 Return in *DELTA the number of chars by which buffer positions in
14289 unchanged text at the end of current_buffer must be adjusted.
14290
14291 Return in *DELTA_BYTES the corresponding number of bytes.
14292
14293 Value is null if no such row exists, i.e. all rows are affected by
14294 changes. */
14295
14296 static struct glyph_row *
14297 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14298 struct window *w;
14299 int *delta, *delta_bytes;
14300 {
14301 struct glyph_row *row;
14302 struct glyph_row *row_found = NULL;
14303
14304 *delta = *delta_bytes = 0;
14305
14306 /* Display must not have been paused, otherwise the current matrix
14307 is not up to date. */
14308 if (NILP (w->window_end_valid))
14309 abort ();
14310
14311 /* A value of window_end_pos >= END_UNCHANGED means that the window
14312 end is in the range of changed text. If so, there is no
14313 unchanged row at the end of W's current matrix. */
14314 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14315 return NULL;
14316
14317 /* Set row to the last row in W's current matrix displaying text. */
14318 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14319
14320 /* If matrix is entirely empty, no unchanged row exists. */
14321 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14322 {
14323 /* The value of row is the last glyph row in the matrix having a
14324 meaningful buffer position in it. The end position of row
14325 corresponds to window_end_pos. This allows us to translate
14326 buffer positions in the current matrix to current buffer
14327 positions for characters not in changed text. */
14328 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14329 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14330 int last_unchanged_pos, last_unchanged_pos_old;
14331 struct glyph_row *first_text_row
14332 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14333
14334 *delta = Z - Z_old;
14335 *delta_bytes = Z_BYTE - Z_BYTE_old;
14336
14337 /* Set last_unchanged_pos to the buffer position of the last
14338 character in the buffer that has not been changed. Z is the
14339 index + 1 of the last character in current_buffer, i.e. by
14340 subtracting END_UNCHANGED we get the index of the last
14341 unchanged character, and we have to add BEG to get its buffer
14342 position. */
14343 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14344 last_unchanged_pos_old = last_unchanged_pos - *delta;
14345
14346 /* Search backward from ROW for a row displaying a line that
14347 starts at a minimum position >= last_unchanged_pos_old. */
14348 for (; row > first_text_row; --row)
14349 {
14350 /* This used to abort, but it can happen.
14351 It is ok to just stop the search instead here. KFS. */
14352 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14353 break;
14354
14355 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14356 row_found = row;
14357 }
14358 }
14359
14360 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14361 abort ();
14362
14363 return row_found;
14364 }
14365
14366
14367 /* Make sure that glyph rows in the current matrix of window W
14368 reference the same glyph memory as corresponding rows in the
14369 frame's frame matrix. This function is called after scrolling W's
14370 current matrix on a terminal frame in try_window_id and
14371 try_window_reusing_current_matrix. */
14372
14373 static void
14374 sync_frame_with_window_matrix_rows (w)
14375 struct window *w;
14376 {
14377 struct frame *f = XFRAME (w->frame);
14378 struct glyph_row *window_row, *window_row_end, *frame_row;
14379
14380 /* Preconditions: W must be a leaf window and full-width. Its frame
14381 must have a frame matrix. */
14382 xassert (NILP (w->hchild) && NILP (w->vchild));
14383 xassert (WINDOW_FULL_WIDTH_P (w));
14384 xassert (!FRAME_WINDOW_P (f));
14385
14386 /* If W is a full-width window, glyph pointers in W's current matrix
14387 have, by definition, to be the same as glyph pointers in the
14388 corresponding frame matrix. Note that frame matrices have no
14389 marginal areas (see build_frame_matrix). */
14390 window_row = w->current_matrix->rows;
14391 window_row_end = window_row + w->current_matrix->nrows;
14392 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14393 while (window_row < window_row_end)
14394 {
14395 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14396 struct glyph *end = window_row->glyphs[LAST_AREA];
14397
14398 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14399 frame_row->glyphs[TEXT_AREA] = start;
14400 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14401 frame_row->glyphs[LAST_AREA] = end;
14402
14403 /* Disable frame rows whose corresponding window rows have
14404 been disabled in try_window_id. */
14405 if (!window_row->enabled_p)
14406 frame_row->enabled_p = 0;
14407
14408 ++window_row, ++frame_row;
14409 }
14410 }
14411
14412
14413 /* Find the glyph row in window W containing CHARPOS. Consider all
14414 rows between START and END (not inclusive). END null means search
14415 all rows to the end of the display area of W. Value is the row
14416 containing CHARPOS or null. */
14417
14418 struct glyph_row *
14419 row_containing_pos (w, charpos, start, end, dy)
14420 struct window *w;
14421 int charpos;
14422 struct glyph_row *start, *end;
14423 int dy;
14424 {
14425 struct glyph_row *row = start;
14426 int last_y;
14427
14428 /* If we happen to start on a header-line, skip that. */
14429 if (row->mode_line_p)
14430 ++row;
14431
14432 if ((end && row >= end) || !row->enabled_p)
14433 return NULL;
14434
14435 last_y = window_text_bottom_y (w) - dy;
14436
14437 while (1)
14438 {
14439 /* Give up if we have gone too far. */
14440 if (end && row >= end)
14441 return NULL;
14442 /* This formerly returned if they were equal.
14443 I think that both quantities are of a "last plus one" type;
14444 if so, when they are equal, the row is within the screen. -- rms. */
14445 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14446 return NULL;
14447
14448 /* If it is in this row, return this row. */
14449 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14450 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14451 /* The end position of a row equals the start
14452 position of the next row. If CHARPOS is there, we
14453 would rather display it in the next line, except
14454 when this line ends in ZV. */
14455 && !row->ends_at_zv_p
14456 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14457 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14458 return row;
14459 ++row;
14460 }
14461 }
14462
14463
14464 /* Try to redisplay window W by reusing its existing display. W's
14465 current matrix must be up to date when this function is called,
14466 i.e. window_end_valid must not be nil.
14467
14468 Value is
14469
14470 1 if display has been updated
14471 0 if otherwise unsuccessful
14472 -1 if redisplay with same window start is known not to succeed
14473
14474 The following steps are performed:
14475
14476 1. Find the last row in the current matrix of W that is not
14477 affected by changes at the start of current_buffer. If no such row
14478 is found, give up.
14479
14480 2. Find the first row in W's current matrix that is not affected by
14481 changes at the end of current_buffer. Maybe there is no such row.
14482
14483 3. Display lines beginning with the row + 1 found in step 1 to the
14484 row found in step 2 or, if step 2 didn't find a row, to the end of
14485 the window.
14486
14487 4. If cursor is not known to appear on the window, give up.
14488
14489 5. If display stopped at the row found in step 2, scroll the
14490 display and current matrix as needed.
14491
14492 6. Maybe display some lines at the end of W, if we must. This can
14493 happen under various circumstances, like a partially visible line
14494 becoming fully visible, or because newly displayed lines are displayed
14495 in smaller font sizes.
14496
14497 7. Update W's window end information. */
14498
14499 static int
14500 try_window_id (w)
14501 struct window *w;
14502 {
14503 struct frame *f = XFRAME (w->frame);
14504 struct glyph_matrix *current_matrix = w->current_matrix;
14505 struct glyph_matrix *desired_matrix = w->desired_matrix;
14506 struct glyph_row *last_unchanged_at_beg_row;
14507 struct glyph_row *first_unchanged_at_end_row;
14508 struct glyph_row *row;
14509 struct glyph_row *bottom_row;
14510 int bottom_vpos;
14511 struct it it;
14512 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14513 struct text_pos start_pos;
14514 struct run run;
14515 int first_unchanged_at_end_vpos = 0;
14516 struct glyph_row *last_text_row, *last_text_row_at_end;
14517 struct text_pos start;
14518 int first_changed_charpos, last_changed_charpos;
14519
14520 #if GLYPH_DEBUG
14521 if (inhibit_try_window_id)
14522 return 0;
14523 #endif
14524
14525 /* This is handy for debugging. */
14526 #if 0
14527 #define GIVE_UP(X) \
14528 do { \
14529 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14530 return 0; \
14531 } while (0)
14532 #else
14533 #define GIVE_UP(X) return 0
14534 #endif
14535
14536 SET_TEXT_POS_FROM_MARKER (start, w->start);
14537
14538 /* Don't use this for mini-windows because these can show
14539 messages and mini-buffers, and we don't handle that here. */
14540 if (MINI_WINDOW_P (w))
14541 GIVE_UP (1);
14542
14543 /* This flag is used to prevent redisplay optimizations. */
14544 if (windows_or_buffers_changed || cursor_type_changed)
14545 GIVE_UP (2);
14546
14547 /* Verify that narrowing has not changed.
14548 Also verify that we were not told to prevent redisplay optimizations.
14549 It would be nice to further
14550 reduce the number of cases where this prevents try_window_id. */
14551 if (current_buffer->clip_changed
14552 || current_buffer->prevent_redisplay_optimizations_p)
14553 GIVE_UP (3);
14554
14555 /* Window must either use window-based redisplay or be full width. */
14556 if (!FRAME_WINDOW_P (f)
14557 && (!line_ins_del_ok
14558 || !WINDOW_FULL_WIDTH_P (w)))
14559 GIVE_UP (4);
14560
14561 /* Give up if point is not known NOT to appear in W. */
14562 if (PT < CHARPOS (start))
14563 GIVE_UP (5);
14564
14565 /* Another way to prevent redisplay optimizations. */
14566 if (XFASTINT (w->last_modified) == 0)
14567 GIVE_UP (6);
14568
14569 /* Verify that window is not hscrolled. */
14570 if (XFASTINT (w->hscroll) != 0)
14571 GIVE_UP (7);
14572
14573 /* Verify that display wasn't paused. */
14574 if (NILP (w->window_end_valid))
14575 GIVE_UP (8);
14576
14577 /* Can't use this if highlighting a region because a cursor movement
14578 will do more than just set the cursor. */
14579 if (!NILP (Vtransient_mark_mode)
14580 && !NILP (current_buffer->mark_active))
14581 GIVE_UP (9);
14582
14583 /* Likewise if highlighting trailing whitespace. */
14584 if (!NILP (Vshow_trailing_whitespace))
14585 GIVE_UP (11);
14586
14587 /* Likewise if showing a region. */
14588 if (!NILP (w->region_showing))
14589 GIVE_UP (10);
14590
14591 /* Can use this if overlay arrow position and or string have changed. */
14592 if (overlay_arrows_changed_p ())
14593 GIVE_UP (12);
14594
14595
14596 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14597 only if buffer has really changed. The reason is that the gap is
14598 initially at Z for freshly visited files. The code below would
14599 set end_unchanged to 0 in that case. */
14600 if (MODIFF > SAVE_MODIFF
14601 /* This seems to happen sometimes after saving a buffer. */
14602 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14603 {
14604 if (GPT - BEG < BEG_UNCHANGED)
14605 BEG_UNCHANGED = GPT - BEG;
14606 if (Z - GPT < END_UNCHANGED)
14607 END_UNCHANGED = Z - GPT;
14608 }
14609
14610 /* The position of the first and last character that has been changed. */
14611 first_changed_charpos = BEG + BEG_UNCHANGED;
14612 last_changed_charpos = Z - END_UNCHANGED;
14613
14614 /* If window starts after a line end, and the last change is in
14615 front of that newline, then changes don't affect the display.
14616 This case happens with stealth-fontification. Note that although
14617 the display is unchanged, glyph positions in the matrix have to
14618 be adjusted, of course. */
14619 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14620 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14621 && ((last_changed_charpos < CHARPOS (start)
14622 && CHARPOS (start) == BEGV)
14623 || (last_changed_charpos < CHARPOS (start) - 1
14624 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14625 {
14626 int Z_old, delta, Z_BYTE_old, delta_bytes;
14627 struct glyph_row *r0;
14628
14629 /* Compute how many chars/bytes have been added to or removed
14630 from the buffer. */
14631 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14632 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14633 delta = Z - Z_old;
14634 delta_bytes = Z_BYTE - Z_BYTE_old;
14635
14636 /* Give up if PT is not in the window. Note that it already has
14637 been checked at the start of try_window_id that PT is not in
14638 front of the window start. */
14639 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14640 GIVE_UP (13);
14641
14642 /* If window start is unchanged, we can reuse the whole matrix
14643 as is, after adjusting glyph positions. No need to compute
14644 the window end again, since its offset from Z hasn't changed. */
14645 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14646 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14647 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14648 /* PT must not be in a partially visible line. */
14649 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14650 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14651 {
14652 /* Adjust positions in the glyph matrix. */
14653 if (delta || delta_bytes)
14654 {
14655 struct glyph_row *r1
14656 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14657 increment_matrix_positions (w->current_matrix,
14658 MATRIX_ROW_VPOS (r0, current_matrix),
14659 MATRIX_ROW_VPOS (r1, current_matrix),
14660 delta, delta_bytes);
14661 }
14662
14663 /* Set the cursor. */
14664 row = row_containing_pos (w, PT, r0, NULL, 0);
14665 if (row)
14666 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14667 else
14668 abort ();
14669 return 1;
14670 }
14671 }
14672
14673 /* Handle the case that changes are all below what is displayed in
14674 the window, and that PT is in the window. This shortcut cannot
14675 be taken if ZV is visible in the window, and text has been added
14676 there that is visible in the window. */
14677 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14678 /* ZV is not visible in the window, or there are no
14679 changes at ZV, actually. */
14680 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14681 || first_changed_charpos == last_changed_charpos))
14682 {
14683 struct glyph_row *r0;
14684
14685 /* Give up if PT is not in the window. Note that it already has
14686 been checked at the start of try_window_id that PT is not in
14687 front of the window start. */
14688 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14689 GIVE_UP (14);
14690
14691 /* If window start is unchanged, we can reuse the whole matrix
14692 as is, without changing glyph positions since no text has
14693 been added/removed in front of the window end. */
14694 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14695 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14696 /* PT must not be in a partially visible line. */
14697 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14698 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14699 {
14700 /* We have to compute the window end anew since text
14701 can have been added/removed after it. */
14702 w->window_end_pos
14703 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14704 w->window_end_bytepos
14705 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14706
14707 /* Set the cursor. */
14708 row = row_containing_pos (w, PT, r0, NULL, 0);
14709 if (row)
14710 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14711 else
14712 abort ();
14713 return 2;
14714 }
14715 }
14716
14717 /* Give up if window start is in the changed area.
14718
14719 The condition used to read
14720
14721 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14722
14723 but why that was tested escapes me at the moment. */
14724 if (CHARPOS (start) >= first_changed_charpos
14725 && CHARPOS (start) <= last_changed_charpos)
14726 GIVE_UP (15);
14727
14728 /* Check that window start agrees with the start of the first glyph
14729 row in its current matrix. Check this after we know the window
14730 start is not in changed text, otherwise positions would not be
14731 comparable. */
14732 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14733 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14734 GIVE_UP (16);
14735
14736 /* Give up if the window ends in strings. Overlay strings
14737 at the end are difficult to handle, so don't try. */
14738 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14739 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14740 GIVE_UP (20);
14741
14742 /* Compute the position at which we have to start displaying new
14743 lines. Some of the lines at the top of the window might be
14744 reusable because they are not displaying changed text. Find the
14745 last row in W's current matrix not affected by changes at the
14746 start of current_buffer. Value is null if changes start in the
14747 first line of window. */
14748 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14749 if (last_unchanged_at_beg_row)
14750 {
14751 /* Avoid starting to display in the moddle of a character, a TAB
14752 for instance. This is easier than to set up the iterator
14753 exactly, and it's not a frequent case, so the additional
14754 effort wouldn't really pay off. */
14755 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14756 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14757 && last_unchanged_at_beg_row > w->current_matrix->rows)
14758 --last_unchanged_at_beg_row;
14759
14760 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14761 GIVE_UP (17);
14762
14763 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14764 GIVE_UP (18);
14765 start_pos = it.current.pos;
14766
14767 /* Start displaying new lines in the desired matrix at the same
14768 vpos we would use in the current matrix, i.e. below
14769 last_unchanged_at_beg_row. */
14770 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14771 current_matrix);
14772 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14773 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14774
14775 xassert (it.hpos == 0 && it.current_x == 0);
14776 }
14777 else
14778 {
14779 /* There are no reusable lines at the start of the window.
14780 Start displaying in the first text line. */
14781 start_display (&it, w, start);
14782 it.vpos = it.first_vpos;
14783 start_pos = it.current.pos;
14784 }
14785
14786 /* Find the first row that is not affected by changes at the end of
14787 the buffer. Value will be null if there is no unchanged row, in
14788 which case we must redisplay to the end of the window. delta
14789 will be set to the value by which buffer positions beginning with
14790 first_unchanged_at_end_row have to be adjusted due to text
14791 changes. */
14792 first_unchanged_at_end_row
14793 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14794 IF_DEBUG (debug_delta = delta);
14795 IF_DEBUG (debug_delta_bytes = delta_bytes);
14796
14797 /* Set stop_pos to the buffer position up to which we will have to
14798 display new lines. If first_unchanged_at_end_row != NULL, this
14799 is the buffer position of the start of the line displayed in that
14800 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14801 that we don't stop at a buffer position. */
14802 stop_pos = 0;
14803 if (first_unchanged_at_end_row)
14804 {
14805 xassert (last_unchanged_at_beg_row == NULL
14806 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14807
14808 /* If this is a continuation line, move forward to the next one
14809 that isn't. Changes in lines above affect this line.
14810 Caution: this may move first_unchanged_at_end_row to a row
14811 not displaying text. */
14812 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14813 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14814 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14815 < it.last_visible_y))
14816 ++first_unchanged_at_end_row;
14817
14818 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14819 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14820 >= it.last_visible_y))
14821 first_unchanged_at_end_row = NULL;
14822 else
14823 {
14824 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14825 + delta);
14826 first_unchanged_at_end_vpos
14827 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14828 xassert (stop_pos >= Z - END_UNCHANGED);
14829 }
14830 }
14831 else if (last_unchanged_at_beg_row == NULL)
14832 GIVE_UP (19);
14833
14834
14835 #if GLYPH_DEBUG
14836
14837 /* Either there is no unchanged row at the end, or the one we have
14838 now displays text. This is a necessary condition for the window
14839 end pos calculation at the end of this function. */
14840 xassert (first_unchanged_at_end_row == NULL
14841 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14842
14843 debug_last_unchanged_at_beg_vpos
14844 = (last_unchanged_at_beg_row
14845 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14846 : -1);
14847 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14848
14849 #endif /* GLYPH_DEBUG != 0 */
14850
14851
14852 /* Display new lines. Set last_text_row to the last new line
14853 displayed which has text on it, i.e. might end up as being the
14854 line where the window_end_vpos is. */
14855 w->cursor.vpos = -1;
14856 last_text_row = NULL;
14857 overlay_arrow_seen = 0;
14858 while (it.current_y < it.last_visible_y
14859 && !fonts_changed_p
14860 && (first_unchanged_at_end_row == NULL
14861 || IT_CHARPOS (it) < stop_pos))
14862 {
14863 if (display_line (&it))
14864 last_text_row = it.glyph_row - 1;
14865 }
14866
14867 if (fonts_changed_p)
14868 return -1;
14869
14870
14871 /* Compute differences in buffer positions, y-positions etc. for
14872 lines reused at the bottom of the window. Compute what we can
14873 scroll. */
14874 if (first_unchanged_at_end_row
14875 /* No lines reused because we displayed everything up to the
14876 bottom of the window. */
14877 && it.current_y < it.last_visible_y)
14878 {
14879 dvpos = (it.vpos
14880 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14881 current_matrix));
14882 dy = it.current_y - first_unchanged_at_end_row->y;
14883 run.current_y = first_unchanged_at_end_row->y;
14884 run.desired_y = run.current_y + dy;
14885 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14886 }
14887 else
14888 {
14889 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14890 first_unchanged_at_end_row = NULL;
14891 }
14892 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14893
14894
14895 /* Find the cursor if not already found. We have to decide whether
14896 PT will appear on this window (it sometimes doesn't, but this is
14897 not a very frequent case.) This decision has to be made before
14898 the current matrix is altered. A value of cursor.vpos < 0 means
14899 that PT is either in one of the lines beginning at
14900 first_unchanged_at_end_row or below the window. Don't care for
14901 lines that might be displayed later at the window end; as
14902 mentioned, this is not a frequent case. */
14903 if (w->cursor.vpos < 0)
14904 {
14905 /* Cursor in unchanged rows at the top? */
14906 if (PT < CHARPOS (start_pos)
14907 && last_unchanged_at_beg_row)
14908 {
14909 row = row_containing_pos (w, PT,
14910 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14911 last_unchanged_at_beg_row + 1, 0);
14912 if (row)
14913 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14914 }
14915
14916 /* Start from first_unchanged_at_end_row looking for PT. */
14917 else if (first_unchanged_at_end_row)
14918 {
14919 row = row_containing_pos (w, PT - delta,
14920 first_unchanged_at_end_row, NULL, 0);
14921 if (row)
14922 set_cursor_from_row (w, row, w->current_matrix, delta,
14923 delta_bytes, dy, dvpos);
14924 }
14925
14926 /* Give up if cursor was not found. */
14927 if (w->cursor.vpos < 0)
14928 {
14929 clear_glyph_matrix (w->desired_matrix);
14930 return -1;
14931 }
14932 }
14933
14934 /* Don't let the cursor end in the scroll margins. */
14935 {
14936 int this_scroll_margin, cursor_height;
14937
14938 this_scroll_margin = max (0, scroll_margin);
14939 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14940 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14941 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14942
14943 if ((w->cursor.y < this_scroll_margin
14944 && CHARPOS (start) > BEGV)
14945 /* Old redisplay didn't take scroll margin into account at the bottom,
14946 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14947 || (w->cursor.y + (make_cursor_line_fully_visible_p
14948 ? cursor_height + this_scroll_margin
14949 : 1)) > it.last_visible_y)
14950 {
14951 w->cursor.vpos = -1;
14952 clear_glyph_matrix (w->desired_matrix);
14953 return -1;
14954 }
14955 }
14956
14957 /* Scroll the display. Do it before changing the current matrix so
14958 that xterm.c doesn't get confused about where the cursor glyph is
14959 found. */
14960 if (dy && run.height)
14961 {
14962 update_begin (f);
14963
14964 if (FRAME_WINDOW_P (f))
14965 {
14966 rif->update_window_begin_hook (w);
14967 rif->clear_window_mouse_face (w);
14968 rif->scroll_run_hook (w, &run);
14969 rif->update_window_end_hook (w, 0, 0);
14970 }
14971 else
14972 {
14973 /* Terminal frame. In this case, dvpos gives the number of
14974 lines to scroll by; dvpos < 0 means scroll up. */
14975 int first_unchanged_at_end_vpos
14976 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14977 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14978 int end = (WINDOW_TOP_EDGE_LINE (w)
14979 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14980 + window_internal_height (w));
14981
14982 /* Perform the operation on the screen. */
14983 if (dvpos > 0)
14984 {
14985 /* Scroll last_unchanged_at_beg_row to the end of the
14986 window down dvpos lines. */
14987 set_terminal_window (end);
14988
14989 /* On dumb terminals delete dvpos lines at the end
14990 before inserting dvpos empty lines. */
14991 if (!scroll_region_ok)
14992 ins_del_lines (end - dvpos, -dvpos);
14993
14994 /* Insert dvpos empty lines in front of
14995 last_unchanged_at_beg_row. */
14996 ins_del_lines (from, dvpos);
14997 }
14998 else if (dvpos < 0)
14999 {
15000 /* Scroll up last_unchanged_at_beg_vpos to the end of
15001 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15002 set_terminal_window (end);
15003
15004 /* Delete dvpos lines in front of
15005 last_unchanged_at_beg_vpos. ins_del_lines will set
15006 the cursor to the given vpos and emit |dvpos| delete
15007 line sequences. */
15008 ins_del_lines (from + dvpos, dvpos);
15009
15010 /* On a dumb terminal insert dvpos empty lines at the
15011 end. */
15012 if (!scroll_region_ok)
15013 ins_del_lines (end + dvpos, -dvpos);
15014 }
15015
15016 set_terminal_window (0);
15017 }
15018
15019 update_end (f);
15020 }
15021
15022 /* Shift reused rows of the current matrix to the right position.
15023 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15024 text. */
15025 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15026 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15027 if (dvpos < 0)
15028 {
15029 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15030 bottom_vpos, dvpos);
15031 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15032 bottom_vpos, 0);
15033 }
15034 else if (dvpos > 0)
15035 {
15036 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15037 bottom_vpos, dvpos);
15038 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15039 first_unchanged_at_end_vpos + dvpos, 0);
15040 }
15041
15042 /* For frame-based redisplay, make sure that current frame and window
15043 matrix are in sync with respect to glyph memory. */
15044 if (!FRAME_WINDOW_P (f))
15045 sync_frame_with_window_matrix_rows (w);
15046
15047 /* Adjust buffer positions in reused rows. */
15048 if (delta || delta_bytes)
15049 increment_matrix_positions (current_matrix,
15050 first_unchanged_at_end_vpos + dvpos,
15051 bottom_vpos, delta, delta_bytes);
15052
15053 /* Adjust Y positions. */
15054 if (dy)
15055 shift_glyph_matrix (w, current_matrix,
15056 first_unchanged_at_end_vpos + dvpos,
15057 bottom_vpos, dy);
15058
15059 if (first_unchanged_at_end_row)
15060 {
15061 first_unchanged_at_end_row += dvpos;
15062 if (first_unchanged_at_end_row->y >= it.last_visible_y
15063 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15064 first_unchanged_at_end_row = NULL;
15065 }
15066
15067 /* If scrolling up, there may be some lines to display at the end of
15068 the window. */
15069 last_text_row_at_end = NULL;
15070 if (dy < 0)
15071 {
15072 /* Scrolling up can leave for example a partially visible line
15073 at the end of the window to be redisplayed. */
15074 /* Set last_row to the glyph row in the current matrix where the
15075 window end line is found. It has been moved up or down in
15076 the matrix by dvpos. */
15077 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15078 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15079
15080 /* If last_row is the window end line, it should display text. */
15081 xassert (last_row->displays_text_p);
15082
15083 /* If window end line was partially visible before, begin
15084 displaying at that line. Otherwise begin displaying with the
15085 line following it. */
15086 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15087 {
15088 init_to_row_start (&it, w, last_row);
15089 it.vpos = last_vpos;
15090 it.current_y = last_row->y;
15091 }
15092 else
15093 {
15094 init_to_row_end (&it, w, last_row);
15095 it.vpos = 1 + last_vpos;
15096 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15097 ++last_row;
15098 }
15099
15100 /* We may start in a continuation line. If so, we have to
15101 get the right continuation_lines_width and current_x. */
15102 it.continuation_lines_width = last_row->continuation_lines_width;
15103 it.hpos = it.current_x = 0;
15104
15105 /* Display the rest of the lines at the window end. */
15106 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15107 while (it.current_y < it.last_visible_y
15108 && !fonts_changed_p)
15109 {
15110 /* Is it always sure that the display agrees with lines in
15111 the current matrix? I don't think so, so we mark rows
15112 displayed invalid in the current matrix by setting their
15113 enabled_p flag to zero. */
15114 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15115 if (display_line (&it))
15116 last_text_row_at_end = it.glyph_row - 1;
15117 }
15118 }
15119
15120 /* Update window_end_pos and window_end_vpos. */
15121 if (first_unchanged_at_end_row
15122 && !last_text_row_at_end)
15123 {
15124 /* Window end line if one of the preserved rows from the current
15125 matrix. Set row to the last row displaying text in current
15126 matrix starting at first_unchanged_at_end_row, after
15127 scrolling. */
15128 xassert (first_unchanged_at_end_row->displays_text_p);
15129 row = find_last_row_displaying_text (w->current_matrix, &it,
15130 first_unchanged_at_end_row);
15131 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15132
15133 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15134 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15135 w->window_end_vpos
15136 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15137 xassert (w->window_end_bytepos >= 0);
15138 IF_DEBUG (debug_method_add (w, "A"));
15139 }
15140 else if (last_text_row_at_end)
15141 {
15142 w->window_end_pos
15143 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15144 w->window_end_bytepos
15145 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15146 w->window_end_vpos
15147 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15148 xassert (w->window_end_bytepos >= 0);
15149 IF_DEBUG (debug_method_add (w, "B"));
15150 }
15151 else if (last_text_row)
15152 {
15153 /* We have displayed either to the end of the window or at the
15154 end of the window, i.e. the last row with text is to be found
15155 in the desired matrix. */
15156 w->window_end_pos
15157 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15158 w->window_end_bytepos
15159 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15160 w->window_end_vpos
15161 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15162 xassert (w->window_end_bytepos >= 0);
15163 }
15164 else if (first_unchanged_at_end_row == NULL
15165 && last_text_row == NULL
15166 && last_text_row_at_end == NULL)
15167 {
15168 /* Displayed to end of window, but no line containing text was
15169 displayed. Lines were deleted at the end of the window. */
15170 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15171 int vpos = XFASTINT (w->window_end_vpos);
15172 struct glyph_row *current_row = current_matrix->rows + vpos;
15173 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15174
15175 for (row = NULL;
15176 row == NULL && vpos >= first_vpos;
15177 --vpos, --current_row, --desired_row)
15178 {
15179 if (desired_row->enabled_p)
15180 {
15181 if (desired_row->displays_text_p)
15182 row = desired_row;
15183 }
15184 else if (current_row->displays_text_p)
15185 row = current_row;
15186 }
15187
15188 xassert (row != NULL);
15189 w->window_end_vpos = make_number (vpos + 1);
15190 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15191 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15192 xassert (w->window_end_bytepos >= 0);
15193 IF_DEBUG (debug_method_add (w, "C"));
15194 }
15195 else
15196 abort ();
15197
15198 #if 0 /* This leads to problems, for instance when the cursor is
15199 at ZV, and the cursor line displays no text. */
15200 /* Disable rows below what's displayed in the window. This makes
15201 debugging easier. */
15202 enable_glyph_matrix_rows (current_matrix,
15203 XFASTINT (w->window_end_vpos) + 1,
15204 bottom_vpos, 0);
15205 #endif
15206
15207 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15208 debug_end_vpos = XFASTINT (w->window_end_vpos));
15209
15210 /* Record that display has not been completed. */
15211 w->window_end_valid = Qnil;
15212 w->desired_matrix->no_scrolling_p = 1;
15213 return 3;
15214
15215 #undef GIVE_UP
15216 }
15217
15218
15219 \f
15220 /***********************************************************************
15221 More debugging support
15222 ***********************************************************************/
15223
15224 #if GLYPH_DEBUG
15225
15226 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15227 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15228 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15229
15230
15231 /* Dump the contents of glyph matrix MATRIX on stderr.
15232
15233 GLYPHS 0 means don't show glyph contents.
15234 GLYPHS 1 means show glyphs in short form
15235 GLYPHS > 1 means show glyphs in long form. */
15236
15237 void
15238 dump_glyph_matrix (matrix, glyphs)
15239 struct glyph_matrix *matrix;
15240 int glyphs;
15241 {
15242 int i;
15243 for (i = 0; i < matrix->nrows; ++i)
15244 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15245 }
15246
15247
15248 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15249 the glyph row and area where the glyph comes from. */
15250
15251 void
15252 dump_glyph (row, glyph, area)
15253 struct glyph_row *row;
15254 struct glyph *glyph;
15255 int area;
15256 {
15257 if (glyph->type == CHAR_GLYPH)
15258 {
15259 fprintf (stderr,
15260 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15261 glyph - row->glyphs[TEXT_AREA],
15262 'C',
15263 glyph->charpos,
15264 (BUFFERP (glyph->object)
15265 ? 'B'
15266 : (STRINGP (glyph->object)
15267 ? 'S'
15268 : '-')),
15269 glyph->pixel_width,
15270 glyph->u.ch,
15271 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15272 ? glyph->u.ch
15273 : '.'),
15274 glyph->face_id,
15275 glyph->left_box_line_p,
15276 glyph->right_box_line_p);
15277 }
15278 else if (glyph->type == STRETCH_GLYPH)
15279 {
15280 fprintf (stderr,
15281 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15282 glyph - row->glyphs[TEXT_AREA],
15283 'S',
15284 glyph->charpos,
15285 (BUFFERP (glyph->object)
15286 ? 'B'
15287 : (STRINGP (glyph->object)
15288 ? 'S'
15289 : '-')),
15290 glyph->pixel_width,
15291 0,
15292 '.',
15293 glyph->face_id,
15294 glyph->left_box_line_p,
15295 glyph->right_box_line_p);
15296 }
15297 else if (glyph->type == IMAGE_GLYPH)
15298 {
15299 fprintf (stderr,
15300 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15301 glyph - row->glyphs[TEXT_AREA],
15302 'I',
15303 glyph->charpos,
15304 (BUFFERP (glyph->object)
15305 ? 'B'
15306 : (STRINGP (glyph->object)
15307 ? 'S'
15308 : '-')),
15309 glyph->pixel_width,
15310 glyph->u.img_id,
15311 '.',
15312 glyph->face_id,
15313 glyph->left_box_line_p,
15314 glyph->right_box_line_p);
15315 }
15316 else if (glyph->type == COMPOSITE_GLYPH)
15317 {
15318 fprintf (stderr,
15319 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15320 glyph - row->glyphs[TEXT_AREA],
15321 '+',
15322 glyph->charpos,
15323 (BUFFERP (glyph->object)
15324 ? 'B'
15325 : (STRINGP (glyph->object)
15326 ? 'S'
15327 : '-')),
15328 glyph->pixel_width,
15329 glyph->u.cmp_id,
15330 '.',
15331 glyph->face_id,
15332 glyph->left_box_line_p,
15333 glyph->right_box_line_p);
15334 }
15335 }
15336
15337
15338 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15339 GLYPHS 0 means don't show glyph contents.
15340 GLYPHS 1 means show glyphs in short form
15341 GLYPHS > 1 means show glyphs in long form. */
15342
15343 void
15344 dump_glyph_row (row, vpos, glyphs)
15345 struct glyph_row *row;
15346 int vpos, glyphs;
15347 {
15348 if (glyphs != 1)
15349 {
15350 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15351 fprintf (stderr, "======================================================================\n");
15352
15353 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15354 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15355 vpos,
15356 MATRIX_ROW_START_CHARPOS (row),
15357 MATRIX_ROW_END_CHARPOS (row),
15358 row->used[TEXT_AREA],
15359 row->contains_overlapping_glyphs_p,
15360 row->enabled_p,
15361 row->truncated_on_left_p,
15362 row->truncated_on_right_p,
15363 row->continued_p,
15364 MATRIX_ROW_CONTINUATION_LINE_P (row),
15365 row->displays_text_p,
15366 row->ends_at_zv_p,
15367 row->fill_line_p,
15368 row->ends_in_middle_of_char_p,
15369 row->starts_in_middle_of_char_p,
15370 row->mouse_face_p,
15371 row->x,
15372 row->y,
15373 row->pixel_width,
15374 row->height,
15375 row->visible_height,
15376 row->ascent,
15377 row->phys_ascent);
15378 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15379 row->end.overlay_string_index,
15380 row->continuation_lines_width);
15381 fprintf (stderr, "%9d %5d\n",
15382 CHARPOS (row->start.string_pos),
15383 CHARPOS (row->end.string_pos));
15384 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15385 row->end.dpvec_index);
15386 }
15387
15388 if (glyphs > 1)
15389 {
15390 int area;
15391
15392 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15393 {
15394 struct glyph *glyph = row->glyphs[area];
15395 struct glyph *glyph_end = glyph + row->used[area];
15396
15397 /* Glyph for a line end in text. */
15398 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15399 ++glyph_end;
15400
15401 if (glyph < glyph_end)
15402 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15403
15404 for (; glyph < glyph_end; ++glyph)
15405 dump_glyph (row, glyph, area);
15406 }
15407 }
15408 else if (glyphs == 1)
15409 {
15410 int area;
15411
15412 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15413 {
15414 char *s = (char *) alloca (row->used[area] + 1);
15415 int i;
15416
15417 for (i = 0; i < row->used[area]; ++i)
15418 {
15419 struct glyph *glyph = row->glyphs[area] + i;
15420 if (glyph->type == CHAR_GLYPH
15421 && glyph->u.ch < 0x80
15422 && glyph->u.ch >= ' ')
15423 s[i] = glyph->u.ch;
15424 else
15425 s[i] = '.';
15426 }
15427
15428 s[i] = '\0';
15429 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15430 }
15431 }
15432 }
15433
15434
15435 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15436 Sdump_glyph_matrix, 0, 1, "p",
15437 doc: /* Dump the current matrix of the selected window to stderr.
15438 Shows contents of glyph row structures. With non-nil
15439 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15440 glyphs in short form, otherwise show glyphs in long form. */)
15441 (glyphs)
15442 Lisp_Object glyphs;
15443 {
15444 struct window *w = XWINDOW (selected_window);
15445 struct buffer *buffer = XBUFFER (w->buffer);
15446
15447 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15448 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15449 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15450 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15451 fprintf (stderr, "=============================================\n");
15452 dump_glyph_matrix (w->current_matrix,
15453 NILP (glyphs) ? 0 : XINT (glyphs));
15454 return Qnil;
15455 }
15456
15457
15458 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15459 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15460 ()
15461 {
15462 struct frame *f = XFRAME (selected_frame);
15463 dump_glyph_matrix (f->current_matrix, 1);
15464 return Qnil;
15465 }
15466
15467
15468 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15469 doc: /* Dump glyph row ROW to stderr.
15470 GLYPH 0 means don't dump glyphs.
15471 GLYPH 1 means dump glyphs in short form.
15472 GLYPH > 1 or omitted means dump glyphs in long form. */)
15473 (row, glyphs)
15474 Lisp_Object row, glyphs;
15475 {
15476 struct glyph_matrix *matrix;
15477 int vpos;
15478
15479 CHECK_NUMBER (row);
15480 matrix = XWINDOW (selected_window)->current_matrix;
15481 vpos = XINT (row);
15482 if (vpos >= 0 && vpos < matrix->nrows)
15483 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15484 vpos,
15485 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15486 return Qnil;
15487 }
15488
15489
15490 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15491 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15492 GLYPH 0 means don't dump glyphs.
15493 GLYPH 1 means dump glyphs in short form.
15494 GLYPH > 1 or omitted means dump glyphs in long form. */)
15495 (row, glyphs)
15496 Lisp_Object row, glyphs;
15497 {
15498 struct frame *sf = SELECTED_FRAME ();
15499 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15500 int vpos;
15501
15502 CHECK_NUMBER (row);
15503 vpos = XINT (row);
15504 if (vpos >= 0 && vpos < m->nrows)
15505 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15506 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15507 return Qnil;
15508 }
15509
15510
15511 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15512 doc: /* Toggle tracing of redisplay.
15513 With ARG, turn tracing on if and only if ARG is positive. */)
15514 (arg)
15515 Lisp_Object arg;
15516 {
15517 if (NILP (arg))
15518 trace_redisplay_p = !trace_redisplay_p;
15519 else
15520 {
15521 arg = Fprefix_numeric_value (arg);
15522 trace_redisplay_p = XINT (arg) > 0;
15523 }
15524
15525 return Qnil;
15526 }
15527
15528
15529 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15530 doc: /* Like `format', but print result to stderr.
15531 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15532 (nargs, args)
15533 int nargs;
15534 Lisp_Object *args;
15535 {
15536 Lisp_Object s = Fformat (nargs, args);
15537 fprintf (stderr, "%s", SDATA (s));
15538 return Qnil;
15539 }
15540
15541 #endif /* GLYPH_DEBUG */
15542
15543
15544 \f
15545 /***********************************************************************
15546 Building Desired Matrix Rows
15547 ***********************************************************************/
15548
15549 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15550 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15551
15552 static struct glyph_row *
15553 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15554 struct window *w;
15555 Lisp_Object overlay_arrow_string;
15556 {
15557 struct frame *f = XFRAME (WINDOW_FRAME (w));
15558 struct buffer *buffer = XBUFFER (w->buffer);
15559 struct buffer *old = current_buffer;
15560 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15561 int arrow_len = SCHARS (overlay_arrow_string);
15562 const unsigned char *arrow_end = arrow_string + arrow_len;
15563 const unsigned char *p;
15564 struct it it;
15565 int multibyte_p;
15566 int n_glyphs_before;
15567
15568 set_buffer_temp (buffer);
15569 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15570 it.glyph_row->used[TEXT_AREA] = 0;
15571 SET_TEXT_POS (it.position, 0, 0);
15572
15573 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15574 p = arrow_string;
15575 while (p < arrow_end)
15576 {
15577 Lisp_Object face, ilisp;
15578
15579 /* Get the next character. */
15580 if (multibyte_p)
15581 it.c = string_char_and_length (p, arrow_len, &it.len);
15582 else
15583 it.c = *p, it.len = 1;
15584 p += it.len;
15585
15586 /* Get its face. */
15587 ilisp = make_number (p - arrow_string);
15588 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15589 it.face_id = compute_char_face (f, it.c, face);
15590
15591 /* Compute its width, get its glyphs. */
15592 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15593 SET_TEXT_POS (it.position, -1, -1);
15594 PRODUCE_GLYPHS (&it);
15595
15596 /* If this character doesn't fit any more in the line, we have
15597 to remove some glyphs. */
15598 if (it.current_x > it.last_visible_x)
15599 {
15600 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15601 break;
15602 }
15603 }
15604
15605 set_buffer_temp (old);
15606 return it.glyph_row;
15607 }
15608
15609
15610 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15611 glyphs are only inserted for terminal frames since we can't really
15612 win with truncation glyphs when partially visible glyphs are
15613 involved. Which glyphs to insert is determined by
15614 produce_special_glyphs. */
15615
15616 static void
15617 insert_left_trunc_glyphs (it)
15618 struct it *it;
15619 {
15620 struct it truncate_it;
15621 struct glyph *from, *end, *to, *toend;
15622
15623 xassert (!FRAME_WINDOW_P (it->f));
15624
15625 /* Get the truncation glyphs. */
15626 truncate_it = *it;
15627 truncate_it.current_x = 0;
15628 truncate_it.face_id = DEFAULT_FACE_ID;
15629 truncate_it.glyph_row = &scratch_glyph_row;
15630 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15631 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15632 truncate_it.object = make_number (0);
15633 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15634
15635 /* Overwrite glyphs from IT with truncation glyphs. */
15636 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15637 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15638 to = it->glyph_row->glyphs[TEXT_AREA];
15639 toend = to + it->glyph_row->used[TEXT_AREA];
15640
15641 while (from < end)
15642 *to++ = *from++;
15643
15644 /* There may be padding glyphs left over. Overwrite them too. */
15645 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15646 {
15647 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15648 while (from < end)
15649 *to++ = *from++;
15650 }
15651
15652 if (to > toend)
15653 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15654 }
15655
15656
15657 /* Compute the pixel height and width of IT->glyph_row.
15658
15659 Most of the time, ascent and height of a display line will be equal
15660 to the max_ascent and max_height values of the display iterator
15661 structure. This is not the case if
15662
15663 1. We hit ZV without displaying anything. In this case, max_ascent
15664 and max_height will be zero.
15665
15666 2. We have some glyphs that don't contribute to the line height.
15667 (The glyph row flag contributes_to_line_height_p is for future
15668 pixmap extensions).
15669
15670 The first case is easily covered by using default values because in
15671 these cases, the line height does not really matter, except that it
15672 must not be zero. */
15673
15674 static void
15675 compute_line_metrics (it)
15676 struct it *it;
15677 {
15678 struct glyph_row *row = it->glyph_row;
15679 int area, i;
15680
15681 if (FRAME_WINDOW_P (it->f))
15682 {
15683 int i, min_y, max_y;
15684
15685 /* The line may consist of one space only, that was added to
15686 place the cursor on it. If so, the row's height hasn't been
15687 computed yet. */
15688 if (row->height == 0)
15689 {
15690 if (it->max_ascent + it->max_descent == 0)
15691 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15692 row->ascent = it->max_ascent;
15693 row->height = it->max_ascent + it->max_descent;
15694 row->phys_ascent = it->max_phys_ascent;
15695 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15696 row->extra_line_spacing = it->max_extra_line_spacing;
15697 }
15698
15699 /* Compute the width of this line. */
15700 row->pixel_width = row->x;
15701 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15702 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15703
15704 xassert (row->pixel_width >= 0);
15705 xassert (row->ascent >= 0 && row->height > 0);
15706
15707 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15708 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15709
15710 /* If first line's physical ascent is larger than its logical
15711 ascent, use the physical ascent, and make the row taller.
15712 This makes accented characters fully visible. */
15713 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15714 && row->phys_ascent > row->ascent)
15715 {
15716 row->height += row->phys_ascent - row->ascent;
15717 row->ascent = row->phys_ascent;
15718 }
15719
15720 /* Compute how much of the line is visible. */
15721 row->visible_height = row->height;
15722
15723 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15724 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15725
15726 if (row->y < min_y)
15727 row->visible_height -= min_y - row->y;
15728 if (row->y + row->height > max_y)
15729 row->visible_height -= row->y + row->height - max_y;
15730 }
15731 else
15732 {
15733 row->pixel_width = row->used[TEXT_AREA];
15734 if (row->continued_p)
15735 row->pixel_width -= it->continuation_pixel_width;
15736 else if (row->truncated_on_right_p)
15737 row->pixel_width -= it->truncation_pixel_width;
15738 row->ascent = row->phys_ascent = 0;
15739 row->height = row->phys_height = row->visible_height = 1;
15740 row->extra_line_spacing = 0;
15741 }
15742
15743 /* Compute a hash code for this row. */
15744 row->hash = 0;
15745 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15746 for (i = 0; i < row->used[area]; ++i)
15747 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15748 + row->glyphs[area][i].u.val
15749 + row->glyphs[area][i].face_id
15750 + row->glyphs[area][i].padding_p
15751 + (row->glyphs[area][i].type << 2));
15752
15753 it->max_ascent = it->max_descent = 0;
15754 it->max_phys_ascent = it->max_phys_descent = 0;
15755 }
15756
15757
15758 /* Append one space to the glyph row of iterator IT if doing a
15759 window-based redisplay. The space has the same face as
15760 IT->face_id. Value is non-zero if a space was added.
15761
15762 This function is called to make sure that there is always one glyph
15763 at the end of a glyph row that the cursor can be set on under
15764 window-systems. (If there weren't such a glyph we would not know
15765 how wide and tall a box cursor should be displayed).
15766
15767 At the same time this space let's a nicely handle clearing to the
15768 end of the line if the row ends in italic text. */
15769
15770 static int
15771 append_space_for_newline (it, default_face_p)
15772 struct it *it;
15773 int default_face_p;
15774 {
15775 if (FRAME_WINDOW_P (it->f))
15776 {
15777 int n = it->glyph_row->used[TEXT_AREA];
15778
15779 if (it->glyph_row->glyphs[TEXT_AREA] + n
15780 < it->glyph_row->glyphs[1 + TEXT_AREA])
15781 {
15782 /* Save some values that must not be changed.
15783 Must save IT->c and IT->len because otherwise
15784 ITERATOR_AT_END_P wouldn't work anymore after
15785 append_space_for_newline has been called. */
15786 enum display_element_type saved_what = it->what;
15787 int saved_c = it->c, saved_len = it->len;
15788 int saved_x = it->current_x;
15789 int saved_face_id = it->face_id;
15790 struct text_pos saved_pos;
15791 Lisp_Object saved_object;
15792 struct face *face;
15793
15794 saved_object = it->object;
15795 saved_pos = it->position;
15796
15797 it->what = IT_CHARACTER;
15798 bzero (&it->position, sizeof it->position);
15799 it->object = make_number (0);
15800 it->c = ' ';
15801 it->len = 1;
15802
15803 if (default_face_p)
15804 it->face_id = DEFAULT_FACE_ID;
15805 else if (it->face_before_selective_p)
15806 it->face_id = it->saved_face_id;
15807 face = FACE_FROM_ID (it->f, it->face_id);
15808 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15809
15810 PRODUCE_GLYPHS (it);
15811
15812 it->override_ascent = -1;
15813 it->constrain_row_ascent_descent_p = 0;
15814 it->current_x = saved_x;
15815 it->object = saved_object;
15816 it->position = saved_pos;
15817 it->what = saved_what;
15818 it->face_id = saved_face_id;
15819 it->len = saved_len;
15820 it->c = saved_c;
15821 return 1;
15822 }
15823 }
15824
15825 return 0;
15826 }
15827
15828
15829 /* Extend the face of the last glyph in the text area of IT->glyph_row
15830 to the end of the display line. Called from display_line.
15831 If the glyph row is empty, add a space glyph to it so that we
15832 know the face to draw. Set the glyph row flag fill_line_p. */
15833
15834 static void
15835 extend_face_to_end_of_line (it)
15836 struct it *it;
15837 {
15838 struct face *face;
15839 struct frame *f = it->f;
15840
15841 /* If line is already filled, do nothing. */
15842 if (it->current_x >= it->last_visible_x)
15843 return;
15844
15845 /* Face extension extends the background and box of IT->face_id
15846 to the end of the line. If the background equals the background
15847 of the frame, we don't have to do anything. */
15848 if (it->face_before_selective_p)
15849 face = FACE_FROM_ID (it->f, it->saved_face_id);
15850 else
15851 face = FACE_FROM_ID (f, it->face_id);
15852
15853 if (FRAME_WINDOW_P (f)
15854 && it->glyph_row->displays_text_p
15855 && face->box == FACE_NO_BOX
15856 && face->background == FRAME_BACKGROUND_PIXEL (f)
15857 && !face->stipple)
15858 return;
15859
15860 /* Set the glyph row flag indicating that the face of the last glyph
15861 in the text area has to be drawn to the end of the text area. */
15862 it->glyph_row->fill_line_p = 1;
15863
15864 /* If current character of IT is not ASCII, make sure we have the
15865 ASCII face. This will be automatically undone the next time
15866 get_next_display_element returns a multibyte character. Note
15867 that the character will always be single byte in unibyte text. */
15868 if (!ASCII_CHAR_P (it->c))
15869 {
15870 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15871 }
15872
15873 if (FRAME_WINDOW_P (f))
15874 {
15875 /* If the row is empty, add a space with the current face of IT,
15876 so that we know which face to draw. */
15877 if (it->glyph_row->used[TEXT_AREA] == 0)
15878 {
15879 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15880 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15881 it->glyph_row->used[TEXT_AREA] = 1;
15882 }
15883 }
15884 else
15885 {
15886 /* Save some values that must not be changed. */
15887 int saved_x = it->current_x;
15888 struct text_pos saved_pos;
15889 Lisp_Object saved_object;
15890 enum display_element_type saved_what = it->what;
15891 int saved_face_id = it->face_id;
15892
15893 saved_object = it->object;
15894 saved_pos = it->position;
15895
15896 it->what = IT_CHARACTER;
15897 bzero (&it->position, sizeof it->position);
15898 it->object = make_number (0);
15899 it->c = ' ';
15900 it->len = 1;
15901 it->face_id = face->id;
15902
15903 PRODUCE_GLYPHS (it);
15904
15905 while (it->current_x <= it->last_visible_x)
15906 PRODUCE_GLYPHS (it);
15907
15908 /* Don't count these blanks really. It would let us insert a left
15909 truncation glyph below and make us set the cursor on them, maybe. */
15910 it->current_x = saved_x;
15911 it->object = saved_object;
15912 it->position = saved_pos;
15913 it->what = saved_what;
15914 it->face_id = saved_face_id;
15915 }
15916 }
15917
15918
15919 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15920 trailing whitespace. */
15921
15922 static int
15923 trailing_whitespace_p (charpos)
15924 int charpos;
15925 {
15926 int bytepos = CHAR_TO_BYTE (charpos);
15927 int c = 0;
15928
15929 while (bytepos < ZV_BYTE
15930 && (c = FETCH_CHAR (bytepos),
15931 c == ' ' || c == '\t'))
15932 ++bytepos;
15933
15934 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15935 {
15936 if (bytepos != PT_BYTE)
15937 return 1;
15938 }
15939 return 0;
15940 }
15941
15942
15943 /* Highlight trailing whitespace, if any, in ROW. */
15944
15945 void
15946 highlight_trailing_whitespace (f, row)
15947 struct frame *f;
15948 struct glyph_row *row;
15949 {
15950 int used = row->used[TEXT_AREA];
15951
15952 if (used)
15953 {
15954 struct glyph *start = row->glyphs[TEXT_AREA];
15955 struct glyph *glyph = start + used - 1;
15956
15957 /* Skip over glyphs inserted to display the cursor at the
15958 end of a line, for extending the face of the last glyph
15959 to the end of the line on terminals, and for truncation
15960 and continuation glyphs. */
15961 while (glyph >= start
15962 && glyph->type == CHAR_GLYPH
15963 && INTEGERP (glyph->object))
15964 --glyph;
15965
15966 /* If last glyph is a space or stretch, and it's trailing
15967 whitespace, set the face of all trailing whitespace glyphs in
15968 IT->glyph_row to `trailing-whitespace'. */
15969 if (glyph >= start
15970 && BUFFERP (glyph->object)
15971 && (glyph->type == STRETCH_GLYPH
15972 || (glyph->type == CHAR_GLYPH
15973 && glyph->u.ch == ' '))
15974 && trailing_whitespace_p (glyph->charpos))
15975 {
15976 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
15977 if (face_id < 0)
15978 return;
15979
15980 while (glyph >= start
15981 && BUFFERP (glyph->object)
15982 && (glyph->type == STRETCH_GLYPH
15983 || (glyph->type == CHAR_GLYPH
15984 && glyph->u.ch == ' ')))
15985 (glyph--)->face_id = face_id;
15986 }
15987 }
15988 }
15989
15990
15991 /* Value is non-zero if glyph row ROW in window W should be
15992 used to hold the cursor. */
15993
15994 static int
15995 cursor_row_p (w, row)
15996 struct window *w;
15997 struct glyph_row *row;
15998 {
15999 int cursor_row_p = 1;
16000
16001 if (PT == MATRIX_ROW_END_CHARPOS (row))
16002 {
16003 /* Suppose the row ends on a string.
16004 Unless the row is continued, that means it ends on a newline
16005 in the string. If it's anything other than a display string
16006 (e.g. a before-string from an overlay), we don't want the
16007 cursor there. (This heuristic seems to give the optimal
16008 behavior for the various types of multi-line strings.) */
16009 if (CHARPOS (row->end.string_pos) >= 0)
16010 {
16011 if (row->continued_p)
16012 cursor_row_p = 1;
16013 else
16014 {
16015 /* Check for `display' property. */
16016 struct glyph *beg = row->glyphs[TEXT_AREA];
16017 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16018 struct glyph *glyph;
16019
16020 cursor_row_p = 0;
16021 for (glyph = end; glyph >= beg; --glyph)
16022 if (STRINGP (glyph->object))
16023 {
16024 Lisp_Object prop
16025 = Fget_char_property (make_number (PT),
16026 Qdisplay, Qnil);
16027 cursor_row_p =
16028 (!NILP (prop)
16029 && display_prop_string_p (prop, glyph->object));
16030 break;
16031 }
16032 }
16033 }
16034 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16035 {
16036 /* If the row ends in middle of a real character,
16037 and the line is continued, we want the cursor here.
16038 That's because MATRIX_ROW_END_CHARPOS would equal
16039 PT if PT is before the character. */
16040 if (!row->ends_in_ellipsis_p)
16041 cursor_row_p = row->continued_p;
16042 else
16043 /* If the row ends in an ellipsis, then
16044 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16045 We want that position to be displayed after the ellipsis. */
16046 cursor_row_p = 0;
16047 }
16048 /* If the row ends at ZV, display the cursor at the end of that
16049 row instead of at the start of the row below. */
16050 else if (row->ends_at_zv_p)
16051 cursor_row_p = 1;
16052 else
16053 cursor_row_p = 0;
16054 }
16055
16056 return cursor_row_p;
16057 }
16058
16059
16060 /* Construct the glyph row IT->glyph_row in the desired matrix of
16061 IT->w from text at the current position of IT. See dispextern.h
16062 for an overview of struct it. Value is non-zero if
16063 IT->glyph_row displays text, as opposed to a line displaying ZV
16064 only. */
16065
16066 static int
16067 display_line (it)
16068 struct it *it;
16069 {
16070 struct glyph_row *row = it->glyph_row;
16071 Lisp_Object overlay_arrow_string;
16072
16073 /* We always start displaying at hpos zero even if hscrolled. */
16074 xassert (it->hpos == 0 && it->current_x == 0);
16075
16076 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16077 >= it->w->desired_matrix->nrows)
16078 {
16079 it->w->nrows_scale_factor++;
16080 fonts_changed_p = 1;
16081 return 0;
16082 }
16083
16084 /* Is IT->w showing the region? */
16085 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16086
16087 /* Clear the result glyph row and enable it. */
16088 prepare_desired_row (row);
16089
16090 row->y = it->current_y;
16091 row->start = it->start;
16092 row->continuation_lines_width = it->continuation_lines_width;
16093 row->displays_text_p = 1;
16094 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16095 it->starts_in_middle_of_char_p = 0;
16096
16097 /* Arrange the overlays nicely for our purposes. Usually, we call
16098 display_line on only one line at a time, in which case this
16099 can't really hurt too much, or we call it on lines which appear
16100 one after another in the buffer, in which case all calls to
16101 recenter_overlay_lists but the first will be pretty cheap. */
16102 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16103
16104 /* Move over display elements that are not visible because we are
16105 hscrolled. This may stop at an x-position < IT->first_visible_x
16106 if the first glyph is partially visible or if we hit a line end. */
16107 if (it->current_x < it->first_visible_x)
16108 {
16109 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16110 MOVE_TO_POS | MOVE_TO_X);
16111 }
16112
16113 /* Get the initial row height. This is either the height of the
16114 text hscrolled, if there is any, or zero. */
16115 row->ascent = it->max_ascent;
16116 row->height = it->max_ascent + it->max_descent;
16117 row->phys_ascent = it->max_phys_ascent;
16118 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16119 row->extra_line_spacing = it->max_extra_line_spacing;
16120
16121 /* Loop generating characters. The loop is left with IT on the next
16122 character to display. */
16123 while (1)
16124 {
16125 int n_glyphs_before, hpos_before, x_before;
16126 int x, i, nglyphs;
16127 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16128
16129 /* Retrieve the next thing to display. Value is zero if end of
16130 buffer reached. */
16131 if (!get_next_display_element (it))
16132 {
16133 /* Maybe add a space at the end of this line that is used to
16134 display the cursor there under X. Set the charpos of the
16135 first glyph of blank lines not corresponding to any text
16136 to -1. */
16137 #ifdef HAVE_WINDOW_SYSTEM
16138 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16139 row->exact_window_width_line_p = 1;
16140 else
16141 #endif /* HAVE_WINDOW_SYSTEM */
16142 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16143 || row->used[TEXT_AREA] == 0)
16144 {
16145 row->glyphs[TEXT_AREA]->charpos = -1;
16146 row->displays_text_p = 0;
16147
16148 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16149 && (!MINI_WINDOW_P (it->w)
16150 || (minibuf_level && EQ (it->window, minibuf_window))))
16151 row->indicate_empty_line_p = 1;
16152 }
16153
16154 it->continuation_lines_width = 0;
16155 row->ends_at_zv_p = 1;
16156 break;
16157 }
16158
16159 /* Now, get the metrics of what we want to display. This also
16160 generates glyphs in `row' (which is IT->glyph_row). */
16161 n_glyphs_before = row->used[TEXT_AREA];
16162 x = it->current_x;
16163
16164 /* Remember the line height so far in case the next element doesn't
16165 fit on the line. */
16166 if (!it->truncate_lines_p)
16167 {
16168 ascent = it->max_ascent;
16169 descent = it->max_descent;
16170 phys_ascent = it->max_phys_ascent;
16171 phys_descent = it->max_phys_descent;
16172 }
16173
16174 PRODUCE_GLYPHS (it);
16175
16176 /* If this display element was in marginal areas, continue with
16177 the next one. */
16178 if (it->area != TEXT_AREA)
16179 {
16180 row->ascent = max (row->ascent, it->max_ascent);
16181 row->height = max (row->height, it->max_ascent + it->max_descent);
16182 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16183 row->phys_height = max (row->phys_height,
16184 it->max_phys_ascent + it->max_phys_descent);
16185 row->extra_line_spacing = max (row->extra_line_spacing,
16186 it->max_extra_line_spacing);
16187 set_iterator_to_next (it, 1);
16188 continue;
16189 }
16190
16191 /* Does the display element fit on the line? If we truncate
16192 lines, we should draw past the right edge of the window. If
16193 we don't truncate, we want to stop so that we can display the
16194 continuation glyph before the right margin. If lines are
16195 continued, there are two possible strategies for characters
16196 resulting in more than 1 glyph (e.g. tabs): Display as many
16197 glyphs as possible in this line and leave the rest for the
16198 continuation line, or display the whole element in the next
16199 line. Original redisplay did the former, so we do it also. */
16200 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16201 hpos_before = it->hpos;
16202 x_before = x;
16203
16204 if (/* Not a newline. */
16205 nglyphs > 0
16206 /* Glyphs produced fit entirely in the line. */
16207 && it->current_x < it->last_visible_x)
16208 {
16209 it->hpos += nglyphs;
16210 row->ascent = max (row->ascent, it->max_ascent);
16211 row->height = max (row->height, it->max_ascent + it->max_descent);
16212 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16213 row->phys_height = max (row->phys_height,
16214 it->max_phys_ascent + it->max_phys_descent);
16215 row->extra_line_spacing = max (row->extra_line_spacing,
16216 it->max_extra_line_spacing);
16217 if (it->current_x - it->pixel_width < it->first_visible_x)
16218 row->x = x - it->first_visible_x;
16219 }
16220 else
16221 {
16222 int new_x;
16223 struct glyph *glyph;
16224
16225 for (i = 0; i < nglyphs; ++i, x = new_x)
16226 {
16227 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16228 new_x = x + glyph->pixel_width;
16229
16230 if (/* Lines are continued. */
16231 !it->truncate_lines_p
16232 && (/* Glyph doesn't fit on the line. */
16233 new_x > it->last_visible_x
16234 /* Or it fits exactly on a window system frame. */
16235 || (new_x == it->last_visible_x
16236 && FRAME_WINDOW_P (it->f))))
16237 {
16238 /* End of a continued line. */
16239
16240 if (it->hpos == 0
16241 || (new_x == it->last_visible_x
16242 && FRAME_WINDOW_P (it->f)))
16243 {
16244 /* Current glyph is the only one on the line or
16245 fits exactly on the line. We must continue
16246 the line because we can't draw the cursor
16247 after the glyph. */
16248 row->continued_p = 1;
16249 it->current_x = new_x;
16250 it->continuation_lines_width += new_x;
16251 ++it->hpos;
16252 if (i == nglyphs - 1)
16253 {
16254 set_iterator_to_next (it, 1);
16255 #ifdef HAVE_WINDOW_SYSTEM
16256 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16257 {
16258 if (!get_next_display_element (it))
16259 {
16260 row->exact_window_width_line_p = 1;
16261 it->continuation_lines_width = 0;
16262 row->continued_p = 0;
16263 row->ends_at_zv_p = 1;
16264 }
16265 else if (ITERATOR_AT_END_OF_LINE_P (it))
16266 {
16267 row->continued_p = 0;
16268 row->exact_window_width_line_p = 1;
16269 }
16270 }
16271 #endif /* HAVE_WINDOW_SYSTEM */
16272 }
16273 }
16274 else if (CHAR_GLYPH_PADDING_P (*glyph)
16275 && !FRAME_WINDOW_P (it->f))
16276 {
16277 /* A padding glyph that doesn't fit on this line.
16278 This means the whole character doesn't fit
16279 on the line. */
16280 row->used[TEXT_AREA] = n_glyphs_before;
16281
16282 /* Fill the rest of the row with continuation
16283 glyphs like in 20.x. */
16284 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16285 < row->glyphs[1 + TEXT_AREA])
16286 produce_special_glyphs (it, IT_CONTINUATION);
16287
16288 row->continued_p = 1;
16289 it->current_x = x_before;
16290 it->continuation_lines_width += x_before;
16291
16292 /* Restore the height to what it was before the
16293 element not fitting on the line. */
16294 it->max_ascent = ascent;
16295 it->max_descent = descent;
16296 it->max_phys_ascent = phys_ascent;
16297 it->max_phys_descent = phys_descent;
16298 }
16299 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16300 {
16301 /* A TAB that extends past the right edge of the
16302 window. This produces a single glyph on
16303 window system frames. We leave the glyph in
16304 this row and let it fill the row, but don't
16305 consume the TAB. */
16306 it->continuation_lines_width += it->last_visible_x;
16307 row->ends_in_middle_of_char_p = 1;
16308 row->continued_p = 1;
16309 glyph->pixel_width = it->last_visible_x - x;
16310 it->starts_in_middle_of_char_p = 1;
16311 }
16312 else
16313 {
16314 /* Something other than a TAB that draws past
16315 the right edge of the window. Restore
16316 positions to values before the element. */
16317 row->used[TEXT_AREA] = n_glyphs_before + i;
16318
16319 /* Display continuation glyphs. */
16320 if (!FRAME_WINDOW_P (it->f))
16321 produce_special_glyphs (it, IT_CONTINUATION);
16322 row->continued_p = 1;
16323
16324 it->current_x = x_before;
16325 it->continuation_lines_width += x;
16326 extend_face_to_end_of_line (it);
16327
16328 if (nglyphs > 1 && i > 0)
16329 {
16330 row->ends_in_middle_of_char_p = 1;
16331 it->starts_in_middle_of_char_p = 1;
16332 }
16333
16334 /* Restore the height to what it was before the
16335 element not fitting on the line. */
16336 it->max_ascent = ascent;
16337 it->max_descent = descent;
16338 it->max_phys_ascent = phys_ascent;
16339 it->max_phys_descent = phys_descent;
16340 }
16341
16342 break;
16343 }
16344 else if (new_x > it->first_visible_x)
16345 {
16346 /* Increment number of glyphs actually displayed. */
16347 ++it->hpos;
16348
16349 if (x < it->first_visible_x)
16350 /* Glyph is partially visible, i.e. row starts at
16351 negative X position. */
16352 row->x = x - it->first_visible_x;
16353 }
16354 else
16355 {
16356 /* Glyph is completely off the left margin of the
16357 window. This should not happen because of the
16358 move_it_in_display_line at the start of this
16359 function, unless the text display area of the
16360 window is empty. */
16361 xassert (it->first_visible_x <= it->last_visible_x);
16362 }
16363 }
16364
16365 row->ascent = max (row->ascent, it->max_ascent);
16366 row->height = max (row->height, it->max_ascent + it->max_descent);
16367 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16368 row->phys_height = max (row->phys_height,
16369 it->max_phys_ascent + it->max_phys_descent);
16370 row->extra_line_spacing = max (row->extra_line_spacing,
16371 it->max_extra_line_spacing);
16372
16373 /* End of this display line if row is continued. */
16374 if (row->continued_p || row->ends_at_zv_p)
16375 break;
16376 }
16377
16378 at_end_of_line:
16379 /* Is this a line end? If yes, we're also done, after making
16380 sure that a non-default face is extended up to the right
16381 margin of the window. */
16382 if (ITERATOR_AT_END_OF_LINE_P (it))
16383 {
16384 int used_before = row->used[TEXT_AREA];
16385
16386 row->ends_in_newline_from_string_p = STRINGP (it->object);
16387
16388 #ifdef HAVE_WINDOW_SYSTEM
16389 /* Add a space at the end of the line that is used to
16390 display the cursor there. */
16391 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16392 append_space_for_newline (it, 0);
16393 #endif /* HAVE_WINDOW_SYSTEM */
16394
16395 /* Extend the face to the end of the line. */
16396 extend_face_to_end_of_line (it);
16397
16398 /* Make sure we have the position. */
16399 if (used_before == 0)
16400 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16401
16402 /* Consume the line end. This skips over invisible lines. */
16403 set_iterator_to_next (it, 1);
16404 it->continuation_lines_width = 0;
16405 break;
16406 }
16407
16408 /* Proceed with next display element. Note that this skips
16409 over lines invisible because of selective display. */
16410 set_iterator_to_next (it, 1);
16411
16412 /* If we truncate lines, we are done when the last displayed
16413 glyphs reach past the right margin of the window. */
16414 if (it->truncate_lines_p
16415 && (FRAME_WINDOW_P (it->f)
16416 ? (it->current_x >= it->last_visible_x)
16417 : (it->current_x > it->last_visible_x)))
16418 {
16419 /* Maybe add truncation glyphs. */
16420 if (!FRAME_WINDOW_P (it->f))
16421 {
16422 int i, n;
16423
16424 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16425 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16426 break;
16427
16428 for (n = row->used[TEXT_AREA]; i < n; ++i)
16429 {
16430 row->used[TEXT_AREA] = i;
16431 produce_special_glyphs (it, IT_TRUNCATION);
16432 }
16433 }
16434 #ifdef HAVE_WINDOW_SYSTEM
16435 else
16436 {
16437 /* Don't truncate if we can overflow newline into fringe. */
16438 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16439 {
16440 if (!get_next_display_element (it))
16441 {
16442 it->continuation_lines_width = 0;
16443 row->ends_at_zv_p = 1;
16444 row->exact_window_width_line_p = 1;
16445 break;
16446 }
16447 if (ITERATOR_AT_END_OF_LINE_P (it))
16448 {
16449 row->exact_window_width_line_p = 1;
16450 goto at_end_of_line;
16451 }
16452 }
16453 }
16454 #endif /* HAVE_WINDOW_SYSTEM */
16455
16456 row->truncated_on_right_p = 1;
16457 it->continuation_lines_width = 0;
16458 reseat_at_next_visible_line_start (it, 0);
16459 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16460 it->hpos = hpos_before;
16461 it->current_x = x_before;
16462 break;
16463 }
16464 }
16465
16466 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16467 at the left window margin. */
16468 if (it->first_visible_x
16469 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16470 {
16471 if (!FRAME_WINDOW_P (it->f))
16472 insert_left_trunc_glyphs (it);
16473 row->truncated_on_left_p = 1;
16474 }
16475
16476 /* If the start of this line is the overlay arrow-position, then
16477 mark this glyph row as the one containing the overlay arrow.
16478 This is clearly a mess with variable size fonts. It would be
16479 better to let it be displayed like cursors under X. */
16480 if ((row->displays_text_p || !overlay_arrow_seen)
16481 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16482 !NILP (overlay_arrow_string)))
16483 {
16484 /* Overlay arrow in window redisplay is a fringe bitmap. */
16485 if (STRINGP (overlay_arrow_string))
16486 {
16487 struct glyph_row *arrow_row
16488 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16489 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16490 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16491 struct glyph *p = row->glyphs[TEXT_AREA];
16492 struct glyph *p2, *end;
16493
16494 /* Copy the arrow glyphs. */
16495 while (glyph < arrow_end)
16496 *p++ = *glyph++;
16497
16498 /* Throw away padding glyphs. */
16499 p2 = p;
16500 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16501 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16502 ++p2;
16503 if (p2 > p)
16504 {
16505 while (p2 < end)
16506 *p++ = *p2++;
16507 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16508 }
16509 }
16510 else
16511 {
16512 xassert (INTEGERP (overlay_arrow_string));
16513 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16514 }
16515 overlay_arrow_seen = 1;
16516 }
16517
16518 /* Compute pixel dimensions of this line. */
16519 compute_line_metrics (it);
16520
16521 /* Remember the position at which this line ends. */
16522 row->end = it->current;
16523
16524 /* Record whether this row ends inside an ellipsis. */
16525 row->ends_in_ellipsis_p
16526 = (it->method == GET_FROM_DISPLAY_VECTOR
16527 && it->ellipsis_p);
16528
16529 /* Save fringe bitmaps in this row. */
16530 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16531 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16532 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16533 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16534
16535 it->left_user_fringe_bitmap = 0;
16536 it->left_user_fringe_face_id = 0;
16537 it->right_user_fringe_bitmap = 0;
16538 it->right_user_fringe_face_id = 0;
16539
16540 /* Maybe set the cursor. */
16541 if (it->w->cursor.vpos < 0
16542 && PT >= MATRIX_ROW_START_CHARPOS (row)
16543 && PT <= MATRIX_ROW_END_CHARPOS (row)
16544 && cursor_row_p (it->w, row))
16545 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16546
16547 /* Highlight trailing whitespace. */
16548 if (!NILP (Vshow_trailing_whitespace))
16549 highlight_trailing_whitespace (it->f, it->glyph_row);
16550
16551 /* Prepare for the next line. This line starts horizontally at (X
16552 HPOS) = (0 0). Vertical positions are incremented. As a
16553 convenience for the caller, IT->glyph_row is set to the next
16554 row to be used. */
16555 it->current_x = it->hpos = 0;
16556 it->current_y += row->height;
16557 ++it->vpos;
16558 ++it->glyph_row;
16559 it->start = it->current;
16560 return row->displays_text_p;
16561 }
16562
16563
16564 \f
16565 /***********************************************************************
16566 Menu Bar
16567 ***********************************************************************/
16568
16569 /* Redisplay the menu bar in the frame for window W.
16570
16571 The menu bar of X frames that don't have X toolkit support is
16572 displayed in a special window W->frame->menu_bar_window.
16573
16574 The menu bar of terminal frames is treated specially as far as
16575 glyph matrices are concerned. Menu bar lines are not part of
16576 windows, so the update is done directly on the frame matrix rows
16577 for the menu bar. */
16578
16579 static void
16580 display_menu_bar (w)
16581 struct window *w;
16582 {
16583 struct frame *f = XFRAME (WINDOW_FRAME (w));
16584 struct it it;
16585 Lisp_Object items;
16586 int i;
16587
16588 /* Don't do all this for graphical frames. */
16589 #ifdef HAVE_NTGUI
16590 if (!NILP (Vwindow_system))
16591 return;
16592 #endif
16593 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16594 if (FRAME_X_P (f))
16595 return;
16596 #endif
16597 #ifdef MAC_OS
16598 if (FRAME_MAC_P (f))
16599 return;
16600 #endif
16601
16602 #ifdef USE_X_TOOLKIT
16603 xassert (!FRAME_WINDOW_P (f));
16604 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16605 it.first_visible_x = 0;
16606 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16607 #else /* not USE_X_TOOLKIT */
16608 if (FRAME_WINDOW_P (f))
16609 {
16610 /* Menu bar lines are displayed in the desired matrix of the
16611 dummy window menu_bar_window. */
16612 struct window *menu_w;
16613 xassert (WINDOWP (f->menu_bar_window));
16614 menu_w = XWINDOW (f->menu_bar_window);
16615 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16616 MENU_FACE_ID);
16617 it.first_visible_x = 0;
16618 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16619 }
16620 else
16621 {
16622 /* This is a TTY frame, i.e. character hpos/vpos are used as
16623 pixel x/y. */
16624 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16625 MENU_FACE_ID);
16626 it.first_visible_x = 0;
16627 it.last_visible_x = FRAME_COLS (f);
16628 }
16629 #endif /* not USE_X_TOOLKIT */
16630
16631 if (! mode_line_inverse_video)
16632 /* Force the menu-bar to be displayed in the default face. */
16633 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16634
16635 /* Clear all rows of the menu bar. */
16636 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16637 {
16638 struct glyph_row *row = it.glyph_row + i;
16639 clear_glyph_row (row);
16640 row->enabled_p = 1;
16641 row->full_width_p = 1;
16642 }
16643
16644 /* Display all items of the menu bar. */
16645 items = FRAME_MENU_BAR_ITEMS (it.f);
16646 for (i = 0; i < XVECTOR (items)->size; i += 4)
16647 {
16648 Lisp_Object string;
16649
16650 /* Stop at nil string. */
16651 string = AREF (items, i + 1);
16652 if (NILP (string))
16653 break;
16654
16655 /* Remember where item was displayed. */
16656 AREF (items, i + 3) = make_number (it.hpos);
16657
16658 /* Display the item, pad with one space. */
16659 if (it.current_x < it.last_visible_x)
16660 display_string (NULL, string, Qnil, 0, 0, &it,
16661 SCHARS (string) + 1, 0, 0, -1);
16662 }
16663
16664 /* Fill out the line with spaces. */
16665 if (it.current_x < it.last_visible_x)
16666 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16667
16668 /* Compute the total height of the lines. */
16669 compute_line_metrics (&it);
16670 }
16671
16672
16673 \f
16674 /***********************************************************************
16675 Mode Line
16676 ***********************************************************************/
16677
16678 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16679 FORCE is non-zero, redisplay mode lines unconditionally.
16680 Otherwise, redisplay only mode lines that are garbaged. Value is
16681 the number of windows whose mode lines were redisplayed. */
16682
16683 static int
16684 redisplay_mode_lines (window, force)
16685 Lisp_Object window;
16686 int force;
16687 {
16688 int nwindows = 0;
16689
16690 while (!NILP (window))
16691 {
16692 struct window *w = XWINDOW (window);
16693
16694 if (WINDOWP (w->hchild))
16695 nwindows += redisplay_mode_lines (w->hchild, force);
16696 else if (WINDOWP (w->vchild))
16697 nwindows += redisplay_mode_lines (w->vchild, force);
16698 else if (force
16699 || FRAME_GARBAGED_P (XFRAME (w->frame))
16700 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16701 {
16702 struct text_pos lpoint;
16703 struct buffer *old = current_buffer;
16704
16705 /* Set the window's buffer for the mode line display. */
16706 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16707 set_buffer_internal_1 (XBUFFER (w->buffer));
16708
16709 /* Point refers normally to the selected window. For any
16710 other window, set up appropriate value. */
16711 if (!EQ (window, selected_window))
16712 {
16713 struct text_pos pt;
16714
16715 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16716 if (CHARPOS (pt) < BEGV)
16717 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16718 else if (CHARPOS (pt) > (ZV - 1))
16719 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16720 else
16721 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16722 }
16723
16724 /* Display mode lines. */
16725 clear_glyph_matrix (w->desired_matrix);
16726 if (display_mode_lines (w))
16727 {
16728 ++nwindows;
16729 w->must_be_updated_p = 1;
16730 }
16731
16732 /* Restore old settings. */
16733 set_buffer_internal_1 (old);
16734 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16735 }
16736
16737 window = w->next;
16738 }
16739
16740 return nwindows;
16741 }
16742
16743
16744 /* Display the mode and/or top line of window W. Value is the number
16745 of mode lines displayed. */
16746
16747 static int
16748 display_mode_lines (w)
16749 struct window *w;
16750 {
16751 Lisp_Object old_selected_window, old_selected_frame;
16752 int n = 0;
16753
16754 old_selected_frame = selected_frame;
16755 selected_frame = w->frame;
16756 old_selected_window = selected_window;
16757 XSETWINDOW (selected_window, w);
16758
16759 /* These will be set while the mode line specs are processed. */
16760 line_number_displayed = 0;
16761 w->column_number_displayed = Qnil;
16762
16763 if (WINDOW_WANTS_MODELINE_P (w))
16764 {
16765 struct window *sel_w = XWINDOW (old_selected_window);
16766
16767 /* Select mode line face based on the real selected window. */
16768 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16769 current_buffer->mode_line_format);
16770 ++n;
16771 }
16772
16773 if (WINDOW_WANTS_HEADER_LINE_P (w))
16774 {
16775 display_mode_line (w, HEADER_LINE_FACE_ID,
16776 current_buffer->header_line_format);
16777 ++n;
16778 }
16779
16780 selected_frame = old_selected_frame;
16781 selected_window = old_selected_window;
16782 return n;
16783 }
16784
16785
16786 /* Display mode or top line of window W. FACE_ID specifies which line
16787 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16788 FORMAT is the mode line format to display. Value is the pixel
16789 height of the mode line displayed. */
16790
16791 static int
16792 display_mode_line (w, face_id, format)
16793 struct window *w;
16794 enum face_id face_id;
16795 Lisp_Object format;
16796 {
16797 struct it it;
16798 struct face *face;
16799 int count = SPECPDL_INDEX ();
16800
16801 init_iterator (&it, w, -1, -1, NULL, face_id);
16802 /* Don't extend on a previously drawn mode-line.
16803 This may happen if called from pos_visible_p. */
16804 it.glyph_row->enabled_p = 0;
16805 prepare_desired_row (it.glyph_row);
16806
16807 it.glyph_row->mode_line_p = 1;
16808
16809 if (! mode_line_inverse_video)
16810 /* Force the mode-line to be displayed in the default face. */
16811 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16812
16813 record_unwind_protect (unwind_format_mode_line,
16814 format_mode_line_unwind_data (NULL, 0));
16815
16816 mode_line_target = MODE_LINE_DISPLAY;
16817
16818 /* Temporarily make frame's keyboard the current kboard so that
16819 kboard-local variables in the mode_line_format will get the right
16820 values. */
16821 push_frame_kboard (it.f);
16822 record_unwind_save_match_data ();
16823 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16824 pop_frame_kboard ();
16825
16826 unbind_to (count, Qnil);
16827
16828 /* Fill up with spaces. */
16829 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16830
16831 compute_line_metrics (&it);
16832 it.glyph_row->full_width_p = 1;
16833 it.glyph_row->continued_p = 0;
16834 it.glyph_row->truncated_on_left_p = 0;
16835 it.glyph_row->truncated_on_right_p = 0;
16836
16837 /* Make a 3D mode-line have a shadow at its right end. */
16838 face = FACE_FROM_ID (it.f, face_id);
16839 extend_face_to_end_of_line (&it);
16840 if (face->box != FACE_NO_BOX)
16841 {
16842 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16843 + it.glyph_row->used[TEXT_AREA] - 1);
16844 last->right_box_line_p = 1;
16845 }
16846
16847 return it.glyph_row->height;
16848 }
16849
16850 /* Move element ELT in LIST to the front of LIST.
16851 Return the updated list. */
16852
16853 static Lisp_Object
16854 move_elt_to_front (elt, list)
16855 Lisp_Object elt, list;
16856 {
16857 register Lisp_Object tail, prev;
16858 register Lisp_Object tem;
16859
16860 tail = list;
16861 prev = Qnil;
16862 while (CONSP (tail))
16863 {
16864 tem = XCAR (tail);
16865
16866 if (EQ (elt, tem))
16867 {
16868 /* Splice out the link TAIL. */
16869 if (NILP (prev))
16870 list = XCDR (tail);
16871 else
16872 Fsetcdr (prev, XCDR (tail));
16873
16874 /* Now make it the first. */
16875 Fsetcdr (tail, list);
16876 return tail;
16877 }
16878 else
16879 prev = tail;
16880 tail = XCDR (tail);
16881 QUIT;
16882 }
16883
16884 /* Not found--return unchanged LIST. */
16885 return list;
16886 }
16887
16888 /* Contribute ELT to the mode line for window IT->w. How it
16889 translates into text depends on its data type.
16890
16891 IT describes the display environment in which we display, as usual.
16892
16893 DEPTH is the depth in recursion. It is used to prevent
16894 infinite recursion here.
16895
16896 FIELD_WIDTH is the number of characters the display of ELT should
16897 occupy in the mode line, and PRECISION is the maximum number of
16898 characters to display from ELT's representation. See
16899 display_string for details.
16900
16901 Returns the hpos of the end of the text generated by ELT.
16902
16903 PROPS is a property list to add to any string we encounter.
16904
16905 If RISKY is nonzero, remove (disregard) any properties in any string
16906 we encounter, and ignore :eval and :propertize.
16907
16908 The global variable `mode_line_target' determines whether the
16909 output is passed to `store_mode_line_noprop',
16910 `store_mode_line_string', or `display_string'. */
16911
16912 static int
16913 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16914 struct it *it;
16915 int depth;
16916 int field_width, precision;
16917 Lisp_Object elt, props;
16918 int risky;
16919 {
16920 int n = 0, field, prec;
16921 int literal = 0;
16922
16923 tail_recurse:
16924 if (depth > 100)
16925 elt = build_string ("*too-deep*");
16926
16927 depth++;
16928
16929 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16930 {
16931 case Lisp_String:
16932 {
16933 /* A string: output it and check for %-constructs within it. */
16934 unsigned char c;
16935 int offset = 0;
16936
16937 if (SCHARS (elt) > 0
16938 && (!NILP (props) || risky))
16939 {
16940 Lisp_Object oprops, aelt;
16941 oprops = Ftext_properties_at (make_number (0), elt);
16942
16943 /* If the starting string's properties are not what
16944 we want, translate the string. Also, if the string
16945 is risky, do that anyway. */
16946
16947 if (NILP (Fequal (props, oprops)) || risky)
16948 {
16949 /* If the starting string has properties,
16950 merge the specified ones onto the existing ones. */
16951 if (! NILP (oprops) && !risky)
16952 {
16953 Lisp_Object tem;
16954
16955 oprops = Fcopy_sequence (oprops);
16956 tem = props;
16957 while (CONSP (tem))
16958 {
16959 oprops = Fplist_put (oprops, XCAR (tem),
16960 XCAR (XCDR (tem)));
16961 tem = XCDR (XCDR (tem));
16962 }
16963 props = oprops;
16964 }
16965
16966 aelt = Fassoc (elt, mode_line_proptrans_alist);
16967 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16968 {
16969 /* AELT is what we want. Move it to the front
16970 without consing. */
16971 elt = XCAR (aelt);
16972 mode_line_proptrans_alist
16973 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16974 }
16975 else
16976 {
16977 Lisp_Object tem;
16978
16979 /* If AELT has the wrong props, it is useless.
16980 so get rid of it. */
16981 if (! NILP (aelt))
16982 mode_line_proptrans_alist
16983 = Fdelq (aelt, mode_line_proptrans_alist);
16984
16985 elt = Fcopy_sequence (elt);
16986 Fset_text_properties (make_number (0), Flength (elt),
16987 props, elt);
16988 /* Add this item to mode_line_proptrans_alist. */
16989 mode_line_proptrans_alist
16990 = Fcons (Fcons (elt, props),
16991 mode_line_proptrans_alist);
16992 /* Truncate mode_line_proptrans_alist
16993 to at most 50 elements. */
16994 tem = Fnthcdr (make_number (50),
16995 mode_line_proptrans_alist);
16996 if (! NILP (tem))
16997 XSETCDR (tem, Qnil);
16998 }
16999 }
17000 }
17001
17002 offset = 0;
17003
17004 if (literal)
17005 {
17006 prec = precision - n;
17007 switch (mode_line_target)
17008 {
17009 case MODE_LINE_NOPROP:
17010 case MODE_LINE_TITLE:
17011 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17012 break;
17013 case MODE_LINE_STRING:
17014 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17015 break;
17016 case MODE_LINE_DISPLAY:
17017 n += display_string (NULL, elt, Qnil, 0, 0, it,
17018 0, prec, 0, STRING_MULTIBYTE (elt));
17019 break;
17020 }
17021
17022 break;
17023 }
17024
17025 /* Handle the non-literal case. */
17026
17027 while ((precision <= 0 || n < precision)
17028 && SREF (elt, offset) != 0
17029 && (mode_line_target != MODE_LINE_DISPLAY
17030 || it->current_x < it->last_visible_x))
17031 {
17032 int last_offset = offset;
17033
17034 /* Advance to end of string or next format specifier. */
17035 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17036 ;
17037
17038 if (offset - 1 != last_offset)
17039 {
17040 int nchars, nbytes;
17041
17042 /* Output to end of string or up to '%'. Field width
17043 is length of string. Don't output more than
17044 PRECISION allows us. */
17045 offset--;
17046
17047 prec = c_string_width (SDATA (elt) + last_offset,
17048 offset - last_offset, precision - n,
17049 &nchars, &nbytes);
17050
17051 switch (mode_line_target)
17052 {
17053 case MODE_LINE_NOPROP:
17054 case MODE_LINE_TITLE:
17055 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17056 break;
17057 case MODE_LINE_STRING:
17058 {
17059 int bytepos = last_offset;
17060 int charpos = string_byte_to_char (elt, bytepos);
17061 int endpos = (precision <= 0
17062 ? string_byte_to_char (elt, offset)
17063 : charpos + nchars);
17064
17065 n += store_mode_line_string (NULL,
17066 Fsubstring (elt, make_number (charpos),
17067 make_number (endpos)),
17068 0, 0, 0, Qnil);
17069 }
17070 break;
17071 case MODE_LINE_DISPLAY:
17072 {
17073 int bytepos = last_offset;
17074 int charpos = string_byte_to_char (elt, bytepos);
17075
17076 if (precision <= 0)
17077 nchars = string_byte_to_char (elt, offset) - charpos;
17078 n += display_string (NULL, elt, Qnil, 0, charpos,
17079 it, 0, nchars, 0,
17080 STRING_MULTIBYTE (elt));
17081 }
17082 break;
17083 }
17084 }
17085 else /* c == '%' */
17086 {
17087 int percent_position = offset;
17088
17089 /* Get the specified minimum width. Zero means
17090 don't pad. */
17091 field = 0;
17092 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17093 field = field * 10 + c - '0';
17094
17095 /* Don't pad beyond the total padding allowed. */
17096 if (field_width - n > 0 && field > field_width - n)
17097 field = field_width - n;
17098
17099 /* Note that either PRECISION <= 0 or N < PRECISION. */
17100 prec = precision - n;
17101
17102 if (c == 'M')
17103 n += display_mode_element (it, depth, field, prec,
17104 Vglobal_mode_string, props,
17105 risky);
17106 else if (c != 0)
17107 {
17108 int multibyte;
17109 int bytepos, charpos;
17110 unsigned char *spec;
17111
17112 bytepos = percent_position;
17113 charpos = (STRING_MULTIBYTE (elt)
17114 ? string_byte_to_char (elt, bytepos)
17115 : bytepos);
17116
17117 spec
17118 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17119
17120 switch (mode_line_target)
17121 {
17122 case MODE_LINE_NOPROP:
17123 case MODE_LINE_TITLE:
17124 n += store_mode_line_noprop (spec, field, prec);
17125 break;
17126 case MODE_LINE_STRING:
17127 {
17128 int len = strlen (spec);
17129 Lisp_Object tem = make_string (spec, len);
17130 props = Ftext_properties_at (make_number (charpos), elt);
17131 /* Should only keep face property in props */
17132 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17133 }
17134 break;
17135 case MODE_LINE_DISPLAY:
17136 {
17137 int nglyphs_before, nwritten;
17138
17139 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17140 nwritten = display_string (spec, Qnil, elt,
17141 charpos, 0, it,
17142 field, prec, 0,
17143 multibyte);
17144
17145 /* Assign to the glyphs written above the
17146 string where the `%x' came from, position
17147 of the `%'. */
17148 if (nwritten > 0)
17149 {
17150 struct glyph *glyph
17151 = (it->glyph_row->glyphs[TEXT_AREA]
17152 + nglyphs_before);
17153 int i;
17154
17155 for (i = 0; i < nwritten; ++i)
17156 {
17157 glyph[i].object = elt;
17158 glyph[i].charpos = charpos;
17159 }
17160
17161 n += nwritten;
17162 }
17163 }
17164 break;
17165 }
17166 }
17167 else /* c == 0 */
17168 break;
17169 }
17170 }
17171 }
17172 break;
17173
17174 case Lisp_Symbol:
17175 /* A symbol: process the value of the symbol recursively
17176 as if it appeared here directly. Avoid error if symbol void.
17177 Special case: if value of symbol is a string, output the string
17178 literally. */
17179 {
17180 register Lisp_Object tem;
17181
17182 /* If the variable is not marked as risky to set
17183 then its contents are risky to use. */
17184 if (NILP (Fget (elt, Qrisky_local_variable)))
17185 risky = 1;
17186
17187 tem = Fboundp (elt);
17188 if (!NILP (tem))
17189 {
17190 tem = Fsymbol_value (elt);
17191 /* If value is a string, output that string literally:
17192 don't check for % within it. */
17193 if (STRINGP (tem))
17194 literal = 1;
17195
17196 if (!EQ (tem, elt))
17197 {
17198 /* Give up right away for nil or t. */
17199 elt = tem;
17200 goto tail_recurse;
17201 }
17202 }
17203 }
17204 break;
17205
17206 case Lisp_Cons:
17207 {
17208 register Lisp_Object car, tem;
17209
17210 /* A cons cell: five distinct cases.
17211 If first element is :eval or :propertize, do something special.
17212 If first element is a string or a cons, process all the elements
17213 and effectively concatenate them.
17214 If first element is a negative number, truncate displaying cdr to
17215 at most that many characters. If positive, pad (with spaces)
17216 to at least that many characters.
17217 If first element is a symbol, process the cadr or caddr recursively
17218 according to whether the symbol's value is non-nil or nil. */
17219 car = XCAR (elt);
17220 if (EQ (car, QCeval))
17221 {
17222 /* An element of the form (:eval FORM) means evaluate FORM
17223 and use the result as mode line elements. */
17224
17225 if (risky)
17226 break;
17227
17228 if (CONSP (XCDR (elt)))
17229 {
17230 Lisp_Object spec;
17231 spec = safe_eval (XCAR (XCDR (elt)));
17232 n += display_mode_element (it, depth, field_width - n,
17233 precision - n, spec, props,
17234 risky);
17235 }
17236 }
17237 else if (EQ (car, QCpropertize))
17238 {
17239 /* An element of the form (:propertize ELT PROPS...)
17240 means display ELT but applying properties PROPS. */
17241
17242 if (risky)
17243 break;
17244
17245 if (CONSP (XCDR (elt)))
17246 n += display_mode_element (it, depth, field_width - n,
17247 precision - n, XCAR (XCDR (elt)),
17248 XCDR (XCDR (elt)), risky);
17249 }
17250 else if (SYMBOLP (car))
17251 {
17252 tem = Fboundp (car);
17253 elt = XCDR (elt);
17254 if (!CONSP (elt))
17255 goto invalid;
17256 /* elt is now the cdr, and we know it is a cons cell.
17257 Use its car if CAR has a non-nil value. */
17258 if (!NILP (tem))
17259 {
17260 tem = Fsymbol_value (car);
17261 if (!NILP (tem))
17262 {
17263 elt = XCAR (elt);
17264 goto tail_recurse;
17265 }
17266 }
17267 /* Symbol's value is nil (or symbol is unbound)
17268 Get the cddr of the original list
17269 and if possible find the caddr and use that. */
17270 elt = XCDR (elt);
17271 if (NILP (elt))
17272 break;
17273 else if (!CONSP (elt))
17274 goto invalid;
17275 elt = XCAR (elt);
17276 goto tail_recurse;
17277 }
17278 else if (INTEGERP (car))
17279 {
17280 register int lim = XINT (car);
17281 elt = XCDR (elt);
17282 if (lim < 0)
17283 {
17284 /* Negative int means reduce maximum width. */
17285 if (precision <= 0)
17286 precision = -lim;
17287 else
17288 precision = min (precision, -lim);
17289 }
17290 else if (lim > 0)
17291 {
17292 /* Padding specified. Don't let it be more than
17293 current maximum. */
17294 if (precision > 0)
17295 lim = min (precision, lim);
17296
17297 /* If that's more padding than already wanted, queue it.
17298 But don't reduce padding already specified even if
17299 that is beyond the current truncation point. */
17300 field_width = max (lim, field_width);
17301 }
17302 goto tail_recurse;
17303 }
17304 else if (STRINGP (car) || CONSP (car))
17305 {
17306 register int limit = 50;
17307 /* Limit is to protect against circular lists. */
17308 while (CONSP (elt)
17309 && --limit > 0
17310 && (precision <= 0 || n < precision))
17311 {
17312 n += display_mode_element (it, depth,
17313 /* Do padding only after the last
17314 element in the list. */
17315 (! CONSP (XCDR (elt))
17316 ? field_width - n
17317 : 0),
17318 precision - n, XCAR (elt),
17319 props, risky);
17320 elt = XCDR (elt);
17321 }
17322 }
17323 }
17324 break;
17325
17326 default:
17327 invalid:
17328 elt = build_string ("*invalid*");
17329 goto tail_recurse;
17330 }
17331
17332 /* Pad to FIELD_WIDTH. */
17333 if (field_width > 0 && n < field_width)
17334 {
17335 switch (mode_line_target)
17336 {
17337 case MODE_LINE_NOPROP:
17338 case MODE_LINE_TITLE:
17339 n += store_mode_line_noprop ("", field_width - n, 0);
17340 break;
17341 case MODE_LINE_STRING:
17342 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17343 break;
17344 case MODE_LINE_DISPLAY:
17345 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17346 0, 0, 0);
17347 break;
17348 }
17349 }
17350
17351 return n;
17352 }
17353
17354 /* Store a mode-line string element in mode_line_string_list.
17355
17356 If STRING is non-null, display that C string. Otherwise, the Lisp
17357 string LISP_STRING is displayed.
17358
17359 FIELD_WIDTH is the minimum number of output glyphs to produce.
17360 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17361 with spaces. FIELD_WIDTH <= 0 means don't pad.
17362
17363 PRECISION is the maximum number of characters to output from
17364 STRING. PRECISION <= 0 means don't truncate the string.
17365
17366 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17367 properties to the string.
17368
17369 PROPS are the properties to add to the string.
17370 The mode_line_string_face face property is always added to the string.
17371 */
17372
17373 static int
17374 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17375 char *string;
17376 Lisp_Object lisp_string;
17377 int copy_string;
17378 int field_width;
17379 int precision;
17380 Lisp_Object props;
17381 {
17382 int len;
17383 int n = 0;
17384
17385 if (string != NULL)
17386 {
17387 len = strlen (string);
17388 if (precision > 0 && len > precision)
17389 len = precision;
17390 lisp_string = make_string (string, len);
17391 if (NILP (props))
17392 props = mode_line_string_face_prop;
17393 else if (!NILP (mode_line_string_face))
17394 {
17395 Lisp_Object face = Fplist_get (props, Qface);
17396 props = Fcopy_sequence (props);
17397 if (NILP (face))
17398 face = mode_line_string_face;
17399 else
17400 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17401 props = Fplist_put (props, Qface, face);
17402 }
17403 Fadd_text_properties (make_number (0), make_number (len),
17404 props, lisp_string);
17405 }
17406 else
17407 {
17408 len = XFASTINT (Flength (lisp_string));
17409 if (precision > 0 && len > precision)
17410 {
17411 len = precision;
17412 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17413 precision = -1;
17414 }
17415 if (!NILP (mode_line_string_face))
17416 {
17417 Lisp_Object face;
17418 if (NILP (props))
17419 props = Ftext_properties_at (make_number (0), lisp_string);
17420 face = Fplist_get (props, Qface);
17421 if (NILP (face))
17422 face = mode_line_string_face;
17423 else
17424 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17425 props = Fcons (Qface, Fcons (face, Qnil));
17426 if (copy_string)
17427 lisp_string = Fcopy_sequence (lisp_string);
17428 }
17429 if (!NILP (props))
17430 Fadd_text_properties (make_number (0), make_number (len),
17431 props, lisp_string);
17432 }
17433
17434 if (len > 0)
17435 {
17436 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17437 n += len;
17438 }
17439
17440 if (field_width > len)
17441 {
17442 field_width -= len;
17443 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17444 if (!NILP (props))
17445 Fadd_text_properties (make_number (0), make_number (field_width),
17446 props, lisp_string);
17447 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17448 n += field_width;
17449 }
17450
17451 return n;
17452 }
17453
17454
17455 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17456 1, 4, 0,
17457 doc: /* Format a string out of a mode line format specification.
17458 First arg FORMAT specifies the mode line format (see `mode-line-format'
17459 for details) to use.
17460
17461 Optional second arg FACE specifies the face property to put
17462 on all characters for which no face is specified.
17463 The value t means whatever face the window's mode line currently uses
17464 \(either `mode-line' or `mode-line-inactive', depending).
17465 A value of nil means the default is no face property.
17466 If FACE is an integer, the value string has no text properties.
17467
17468 Optional third and fourth args WINDOW and BUFFER specify the window
17469 and buffer to use as the context for the formatting (defaults
17470 are the selected window and the window's buffer). */)
17471 (format, face, window, buffer)
17472 Lisp_Object format, face, window, buffer;
17473 {
17474 struct it it;
17475 int len;
17476 struct window *w;
17477 struct buffer *old_buffer = NULL;
17478 int face_id = -1;
17479 int no_props = INTEGERP (face);
17480 int count = SPECPDL_INDEX ();
17481 Lisp_Object str;
17482 int string_start = 0;
17483
17484 if (NILP (window))
17485 window = selected_window;
17486 CHECK_WINDOW (window);
17487 w = XWINDOW (window);
17488
17489 if (NILP (buffer))
17490 buffer = w->buffer;
17491 CHECK_BUFFER (buffer);
17492
17493 if (NILP (format))
17494 return empty_unibyte_string;
17495
17496 if (no_props)
17497 face = Qnil;
17498
17499 if (!NILP (face))
17500 {
17501 if (EQ (face, Qt))
17502 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17503 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17504 }
17505
17506 if (face_id < 0)
17507 face_id = DEFAULT_FACE_ID;
17508
17509 if (XBUFFER (buffer) != current_buffer)
17510 old_buffer = current_buffer;
17511
17512 /* Save things including mode_line_proptrans_alist,
17513 and set that to nil so that we don't alter the outer value. */
17514 record_unwind_protect (unwind_format_mode_line,
17515 format_mode_line_unwind_data (old_buffer, 1));
17516 mode_line_proptrans_alist = Qnil;
17517
17518 if (old_buffer)
17519 set_buffer_internal_1 (XBUFFER (buffer));
17520
17521 init_iterator (&it, w, -1, -1, NULL, face_id);
17522
17523 if (no_props)
17524 {
17525 mode_line_target = MODE_LINE_NOPROP;
17526 mode_line_string_face_prop = Qnil;
17527 mode_line_string_list = Qnil;
17528 string_start = MODE_LINE_NOPROP_LEN (0);
17529 }
17530 else
17531 {
17532 mode_line_target = MODE_LINE_STRING;
17533 mode_line_string_list = Qnil;
17534 mode_line_string_face = face;
17535 mode_line_string_face_prop
17536 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17537 }
17538
17539 push_frame_kboard (it.f);
17540 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17541 pop_frame_kboard ();
17542
17543 if (no_props)
17544 {
17545 len = MODE_LINE_NOPROP_LEN (string_start);
17546 str = make_string (mode_line_noprop_buf + string_start, len);
17547 }
17548 else
17549 {
17550 mode_line_string_list = Fnreverse (mode_line_string_list);
17551 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17552 empty_unibyte_string);
17553 }
17554
17555 unbind_to (count, Qnil);
17556 return str;
17557 }
17558
17559 /* Write a null-terminated, right justified decimal representation of
17560 the positive integer D to BUF using a minimal field width WIDTH. */
17561
17562 static void
17563 pint2str (buf, width, d)
17564 register char *buf;
17565 register int width;
17566 register int d;
17567 {
17568 register char *p = buf;
17569
17570 if (d <= 0)
17571 *p++ = '0';
17572 else
17573 {
17574 while (d > 0)
17575 {
17576 *p++ = d % 10 + '0';
17577 d /= 10;
17578 }
17579 }
17580
17581 for (width -= (int) (p - buf); width > 0; --width)
17582 *p++ = ' ';
17583 *p-- = '\0';
17584 while (p > buf)
17585 {
17586 d = *buf;
17587 *buf++ = *p;
17588 *p-- = d;
17589 }
17590 }
17591
17592 /* Write a null-terminated, right justified decimal and "human
17593 readable" representation of the nonnegative integer D to BUF using
17594 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17595
17596 static const char power_letter[] =
17597 {
17598 0, /* not used */
17599 'k', /* kilo */
17600 'M', /* mega */
17601 'G', /* giga */
17602 'T', /* tera */
17603 'P', /* peta */
17604 'E', /* exa */
17605 'Z', /* zetta */
17606 'Y' /* yotta */
17607 };
17608
17609 static void
17610 pint2hrstr (buf, width, d)
17611 char *buf;
17612 int width;
17613 int d;
17614 {
17615 /* We aim to represent the nonnegative integer D as
17616 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17617 int quotient = d;
17618 int remainder = 0;
17619 /* -1 means: do not use TENTHS. */
17620 int tenths = -1;
17621 int exponent = 0;
17622
17623 /* Length of QUOTIENT.TENTHS as a string. */
17624 int length;
17625
17626 char * psuffix;
17627 char * p;
17628
17629 if (1000 <= quotient)
17630 {
17631 /* Scale to the appropriate EXPONENT. */
17632 do
17633 {
17634 remainder = quotient % 1000;
17635 quotient /= 1000;
17636 exponent++;
17637 }
17638 while (1000 <= quotient);
17639
17640 /* Round to nearest and decide whether to use TENTHS or not. */
17641 if (quotient <= 9)
17642 {
17643 tenths = remainder / 100;
17644 if (50 <= remainder % 100)
17645 {
17646 if (tenths < 9)
17647 tenths++;
17648 else
17649 {
17650 quotient++;
17651 if (quotient == 10)
17652 tenths = -1;
17653 else
17654 tenths = 0;
17655 }
17656 }
17657 }
17658 else
17659 if (500 <= remainder)
17660 {
17661 if (quotient < 999)
17662 quotient++;
17663 else
17664 {
17665 quotient = 1;
17666 exponent++;
17667 tenths = 0;
17668 }
17669 }
17670 }
17671
17672 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17673 if (tenths == -1 && quotient <= 99)
17674 if (quotient <= 9)
17675 length = 1;
17676 else
17677 length = 2;
17678 else
17679 length = 3;
17680 p = psuffix = buf + max (width, length);
17681
17682 /* Print EXPONENT. */
17683 if (exponent)
17684 *psuffix++ = power_letter[exponent];
17685 *psuffix = '\0';
17686
17687 /* Print TENTHS. */
17688 if (tenths >= 0)
17689 {
17690 *--p = '0' + tenths;
17691 *--p = '.';
17692 }
17693
17694 /* Print QUOTIENT. */
17695 do
17696 {
17697 int digit = quotient % 10;
17698 *--p = '0' + digit;
17699 }
17700 while ((quotient /= 10) != 0);
17701
17702 /* Print leading spaces. */
17703 while (buf < p)
17704 *--p = ' ';
17705 }
17706
17707 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17708 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17709 type of CODING_SYSTEM. Return updated pointer into BUF. */
17710
17711 static unsigned char invalid_eol_type[] = "(*invalid*)";
17712
17713 static char *
17714 decode_mode_spec_coding (coding_system, buf, eol_flag)
17715 Lisp_Object coding_system;
17716 register char *buf;
17717 int eol_flag;
17718 {
17719 Lisp_Object val;
17720 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17721 const unsigned char *eol_str;
17722 int eol_str_len;
17723 /* The EOL conversion we are using. */
17724 Lisp_Object eoltype;
17725
17726 val = CODING_SYSTEM_SPEC (coding_system);
17727 eoltype = Qnil;
17728
17729 if (!VECTORP (val)) /* Not yet decided. */
17730 {
17731 if (multibyte)
17732 *buf++ = '-';
17733 if (eol_flag)
17734 eoltype = eol_mnemonic_undecided;
17735 /* Don't mention EOL conversion if it isn't decided. */
17736 }
17737 else
17738 {
17739 Lisp_Object attrs;
17740 Lisp_Object eolvalue;
17741
17742 attrs = AREF (val, 0);
17743 eolvalue = AREF (val, 2);
17744
17745 if (multibyte)
17746 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17747
17748 if (eol_flag)
17749 {
17750 /* The EOL conversion that is normal on this system. */
17751
17752 if (NILP (eolvalue)) /* Not yet decided. */
17753 eoltype = eol_mnemonic_undecided;
17754 else if (VECTORP (eolvalue)) /* Not yet decided. */
17755 eoltype = eol_mnemonic_undecided;
17756 else /* eolvalue is Qunix, Qdos, or Qmac. */
17757 eoltype = (EQ (eolvalue, Qunix)
17758 ? eol_mnemonic_unix
17759 : (EQ (eolvalue, Qdos) == 1
17760 ? eol_mnemonic_dos : eol_mnemonic_mac));
17761 }
17762 }
17763
17764 if (eol_flag)
17765 {
17766 /* Mention the EOL conversion if it is not the usual one. */
17767 if (STRINGP (eoltype))
17768 {
17769 eol_str = SDATA (eoltype);
17770 eol_str_len = SBYTES (eoltype);
17771 }
17772 else if (CHARACTERP (eoltype))
17773 {
17774 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17775 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17776 eol_str = tmp;
17777 }
17778 else
17779 {
17780 eol_str = invalid_eol_type;
17781 eol_str_len = sizeof (invalid_eol_type) - 1;
17782 }
17783 bcopy (eol_str, buf, eol_str_len);
17784 buf += eol_str_len;
17785 }
17786
17787 return buf;
17788 }
17789
17790 /* Return a string for the output of a mode line %-spec for window W,
17791 generated by character C. PRECISION >= 0 means don't return a
17792 string longer than that value. FIELD_WIDTH > 0 means pad the
17793 string returned with spaces to that value. Return 1 in *MULTIBYTE
17794 if the result is multibyte text.
17795
17796 Note we operate on the current buffer for most purposes,
17797 the exception being w->base_line_pos. */
17798
17799 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17800
17801 static char *
17802 decode_mode_spec (w, c, field_width, precision, multibyte)
17803 struct window *w;
17804 register int c;
17805 int field_width, precision;
17806 int *multibyte;
17807 {
17808 Lisp_Object obj;
17809 struct frame *f = XFRAME (WINDOW_FRAME (w));
17810 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17811 struct buffer *b = current_buffer;
17812
17813 obj = Qnil;
17814 *multibyte = 0;
17815
17816 switch (c)
17817 {
17818 case '*':
17819 if (!NILP (b->read_only))
17820 return "%";
17821 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17822 return "*";
17823 return "-";
17824
17825 case '+':
17826 /* This differs from %* only for a modified read-only buffer. */
17827 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17828 return "*";
17829 if (!NILP (b->read_only))
17830 return "%";
17831 return "-";
17832
17833 case '&':
17834 /* This differs from %* in ignoring read-only-ness. */
17835 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17836 return "*";
17837 return "-";
17838
17839 case '%':
17840 return "%";
17841
17842 case '[':
17843 {
17844 int i;
17845 char *p;
17846
17847 if (command_loop_level > 5)
17848 return "[[[... ";
17849 p = decode_mode_spec_buf;
17850 for (i = 0; i < command_loop_level; i++)
17851 *p++ = '[';
17852 *p = 0;
17853 return decode_mode_spec_buf;
17854 }
17855
17856 case ']':
17857 {
17858 int i;
17859 char *p;
17860
17861 if (command_loop_level > 5)
17862 return " ...]]]";
17863 p = decode_mode_spec_buf;
17864 for (i = 0; i < command_loop_level; i++)
17865 *p++ = ']';
17866 *p = 0;
17867 return decode_mode_spec_buf;
17868 }
17869
17870 case '-':
17871 {
17872 register int i;
17873
17874 /* Let lots_of_dashes be a string of infinite length. */
17875 if (mode_line_target == MODE_LINE_NOPROP ||
17876 mode_line_target == MODE_LINE_STRING)
17877 return "--";
17878 if (field_width <= 0
17879 || field_width > sizeof (lots_of_dashes))
17880 {
17881 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17882 decode_mode_spec_buf[i] = '-';
17883 decode_mode_spec_buf[i] = '\0';
17884 return decode_mode_spec_buf;
17885 }
17886 else
17887 return lots_of_dashes;
17888 }
17889
17890 case 'b':
17891 obj = b->name;
17892 break;
17893
17894 case 'c':
17895 /* %c and %l are ignored in `frame-title-format'.
17896 (In redisplay_internal, the frame title is drawn _before_ the
17897 windows are updated, so the stuff which depends on actual
17898 window contents (such as %l) may fail to render properly, or
17899 even crash emacs.) */
17900 if (mode_line_target == MODE_LINE_TITLE)
17901 return "";
17902 else
17903 {
17904 int col = (int) current_column (); /* iftc */
17905 w->column_number_displayed = make_number (col);
17906 pint2str (decode_mode_spec_buf, field_width, col);
17907 return decode_mode_spec_buf;
17908 }
17909
17910 case 'e':
17911 #ifndef SYSTEM_MALLOC
17912 {
17913 if (NILP (Vmemory_full))
17914 return "";
17915 else
17916 return "!MEM FULL! ";
17917 }
17918 #else
17919 return "";
17920 #endif
17921
17922 case 'F':
17923 /* %F displays the frame name. */
17924 if (!NILP (f->title))
17925 return (char *) SDATA (f->title);
17926 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17927 return (char *) SDATA (f->name);
17928 return "Emacs";
17929
17930 case 'f':
17931 obj = b->filename;
17932 break;
17933
17934 case 'i':
17935 {
17936 int size = ZV - BEGV;
17937 pint2str (decode_mode_spec_buf, field_width, size);
17938 return decode_mode_spec_buf;
17939 }
17940
17941 case 'I':
17942 {
17943 int size = ZV - BEGV;
17944 pint2hrstr (decode_mode_spec_buf, field_width, size);
17945 return decode_mode_spec_buf;
17946 }
17947
17948 case 'l':
17949 {
17950 int startpos, startpos_byte, line, linepos, linepos_byte;
17951 int topline, nlines, junk, height;
17952
17953 /* %c and %l are ignored in `frame-title-format'. */
17954 if (mode_line_target == MODE_LINE_TITLE)
17955 return "";
17956
17957 startpos = XMARKER (w->start)->charpos;
17958 startpos_byte = marker_byte_position (w->start);
17959 height = WINDOW_TOTAL_LINES (w);
17960
17961 /* If we decided that this buffer isn't suitable for line numbers,
17962 don't forget that too fast. */
17963 if (EQ (w->base_line_pos, w->buffer))
17964 goto no_value;
17965 /* But do forget it, if the window shows a different buffer now. */
17966 else if (BUFFERP (w->base_line_pos))
17967 w->base_line_pos = Qnil;
17968
17969 /* If the buffer is very big, don't waste time. */
17970 if (INTEGERP (Vline_number_display_limit)
17971 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17972 {
17973 w->base_line_pos = Qnil;
17974 w->base_line_number = Qnil;
17975 goto no_value;
17976 }
17977
17978 if (!NILP (w->base_line_number)
17979 && !NILP (w->base_line_pos)
17980 && XFASTINT (w->base_line_pos) <= startpos)
17981 {
17982 line = XFASTINT (w->base_line_number);
17983 linepos = XFASTINT (w->base_line_pos);
17984 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17985 }
17986 else
17987 {
17988 line = 1;
17989 linepos = BUF_BEGV (b);
17990 linepos_byte = BUF_BEGV_BYTE (b);
17991 }
17992
17993 /* Count lines from base line to window start position. */
17994 nlines = display_count_lines (linepos, linepos_byte,
17995 startpos_byte,
17996 startpos, &junk);
17997
17998 topline = nlines + line;
17999
18000 /* Determine a new base line, if the old one is too close
18001 or too far away, or if we did not have one.
18002 "Too close" means it's plausible a scroll-down would
18003 go back past it. */
18004 if (startpos == BUF_BEGV (b))
18005 {
18006 w->base_line_number = make_number (topline);
18007 w->base_line_pos = make_number (BUF_BEGV (b));
18008 }
18009 else if (nlines < height + 25 || nlines > height * 3 + 50
18010 || linepos == BUF_BEGV (b))
18011 {
18012 int limit = BUF_BEGV (b);
18013 int limit_byte = BUF_BEGV_BYTE (b);
18014 int position;
18015 int distance = (height * 2 + 30) * line_number_display_limit_width;
18016
18017 if (startpos - distance > limit)
18018 {
18019 limit = startpos - distance;
18020 limit_byte = CHAR_TO_BYTE (limit);
18021 }
18022
18023 nlines = display_count_lines (startpos, startpos_byte,
18024 limit_byte,
18025 - (height * 2 + 30),
18026 &position);
18027 /* If we couldn't find the lines we wanted within
18028 line_number_display_limit_width chars per line,
18029 give up on line numbers for this window. */
18030 if (position == limit_byte && limit == startpos - distance)
18031 {
18032 w->base_line_pos = w->buffer;
18033 w->base_line_number = Qnil;
18034 goto no_value;
18035 }
18036
18037 w->base_line_number = make_number (topline - nlines);
18038 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18039 }
18040
18041 /* Now count lines from the start pos to point. */
18042 nlines = display_count_lines (startpos, startpos_byte,
18043 PT_BYTE, PT, &junk);
18044
18045 /* Record that we did display the line number. */
18046 line_number_displayed = 1;
18047
18048 /* Make the string to show. */
18049 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18050 return decode_mode_spec_buf;
18051 no_value:
18052 {
18053 char* p = decode_mode_spec_buf;
18054 int pad = field_width - 2;
18055 while (pad-- > 0)
18056 *p++ = ' ';
18057 *p++ = '?';
18058 *p++ = '?';
18059 *p = '\0';
18060 return decode_mode_spec_buf;
18061 }
18062 }
18063 break;
18064
18065 case 'm':
18066 obj = b->mode_name;
18067 break;
18068
18069 case 'n':
18070 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18071 return " Narrow";
18072 break;
18073
18074 case 'p':
18075 {
18076 int pos = marker_position (w->start);
18077 int total = BUF_ZV (b) - BUF_BEGV (b);
18078
18079 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18080 {
18081 if (pos <= BUF_BEGV (b))
18082 return "All";
18083 else
18084 return "Bottom";
18085 }
18086 else if (pos <= BUF_BEGV (b))
18087 return "Top";
18088 else
18089 {
18090 if (total > 1000000)
18091 /* Do it differently for a large value, to avoid overflow. */
18092 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18093 else
18094 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18095 /* We can't normally display a 3-digit number,
18096 so get us a 2-digit number that is close. */
18097 if (total == 100)
18098 total = 99;
18099 sprintf (decode_mode_spec_buf, "%2d%%", total);
18100 return decode_mode_spec_buf;
18101 }
18102 }
18103
18104 /* Display percentage of size above the bottom of the screen. */
18105 case 'P':
18106 {
18107 int toppos = marker_position (w->start);
18108 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18109 int total = BUF_ZV (b) - BUF_BEGV (b);
18110
18111 if (botpos >= BUF_ZV (b))
18112 {
18113 if (toppos <= BUF_BEGV (b))
18114 return "All";
18115 else
18116 return "Bottom";
18117 }
18118 else
18119 {
18120 if (total > 1000000)
18121 /* Do it differently for a large value, to avoid overflow. */
18122 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18123 else
18124 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18125 /* We can't normally display a 3-digit number,
18126 so get us a 2-digit number that is close. */
18127 if (total == 100)
18128 total = 99;
18129 if (toppos <= BUF_BEGV (b))
18130 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18131 else
18132 sprintf (decode_mode_spec_buf, "%2d%%", total);
18133 return decode_mode_spec_buf;
18134 }
18135 }
18136
18137 case 's':
18138 /* status of process */
18139 obj = Fget_buffer_process (Fcurrent_buffer ());
18140 if (NILP (obj))
18141 return "no process";
18142 #ifdef subprocesses
18143 obj = Fsymbol_name (Fprocess_status (obj));
18144 #endif
18145 break;
18146
18147 case 't': /* indicate TEXT or BINARY */
18148 #ifdef MODE_LINE_BINARY_TEXT
18149 return MODE_LINE_BINARY_TEXT (b);
18150 #else
18151 return "T";
18152 #endif
18153
18154 case 'z':
18155 /* coding-system (not including end-of-line format) */
18156 case 'Z':
18157 /* coding-system (including end-of-line type) */
18158 {
18159 int eol_flag = (c == 'Z');
18160 char *p = decode_mode_spec_buf;
18161
18162 if (! FRAME_WINDOW_P (f))
18163 {
18164 /* No need to mention EOL here--the terminal never needs
18165 to do EOL conversion. */
18166 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
18167 p, 0);
18168 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
18169 p, 0);
18170 }
18171 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18172 p, eol_flag);
18173
18174 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18175 #ifdef subprocesses
18176 obj = Fget_buffer_process (Fcurrent_buffer ());
18177 if (PROCESSP (obj))
18178 {
18179 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18180 p, eol_flag);
18181 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18182 p, eol_flag);
18183 }
18184 #endif /* subprocesses */
18185 #endif /* 0 */
18186 *p = 0;
18187 return decode_mode_spec_buf;
18188 }
18189 }
18190
18191 if (STRINGP (obj))
18192 {
18193 *multibyte = STRING_MULTIBYTE (obj);
18194 return (char *) SDATA (obj);
18195 }
18196 else
18197 return "";
18198 }
18199
18200
18201 /* Count up to COUNT lines starting from START / START_BYTE.
18202 But don't go beyond LIMIT_BYTE.
18203 Return the number of lines thus found (always nonnegative).
18204
18205 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18206
18207 static int
18208 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18209 int start, start_byte, limit_byte, count;
18210 int *byte_pos_ptr;
18211 {
18212 register unsigned char *cursor;
18213 unsigned char *base;
18214
18215 register int ceiling;
18216 register unsigned char *ceiling_addr;
18217 int orig_count = count;
18218
18219 /* If we are not in selective display mode,
18220 check only for newlines. */
18221 int selective_display = (!NILP (current_buffer->selective_display)
18222 && !INTEGERP (current_buffer->selective_display));
18223
18224 if (count > 0)
18225 {
18226 while (start_byte < limit_byte)
18227 {
18228 ceiling = BUFFER_CEILING_OF (start_byte);
18229 ceiling = min (limit_byte - 1, ceiling);
18230 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18231 base = (cursor = BYTE_POS_ADDR (start_byte));
18232 while (1)
18233 {
18234 if (selective_display)
18235 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18236 ;
18237 else
18238 while (*cursor != '\n' && ++cursor != ceiling_addr)
18239 ;
18240
18241 if (cursor != ceiling_addr)
18242 {
18243 if (--count == 0)
18244 {
18245 start_byte += cursor - base + 1;
18246 *byte_pos_ptr = start_byte;
18247 return orig_count;
18248 }
18249 else
18250 if (++cursor == ceiling_addr)
18251 break;
18252 }
18253 else
18254 break;
18255 }
18256 start_byte += cursor - base;
18257 }
18258 }
18259 else
18260 {
18261 while (start_byte > limit_byte)
18262 {
18263 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18264 ceiling = max (limit_byte, ceiling);
18265 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18266 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18267 while (1)
18268 {
18269 if (selective_display)
18270 while (--cursor != ceiling_addr
18271 && *cursor != '\n' && *cursor != 015)
18272 ;
18273 else
18274 while (--cursor != ceiling_addr && *cursor != '\n')
18275 ;
18276
18277 if (cursor != ceiling_addr)
18278 {
18279 if (++count == 0)
18280 {
18281 start_byte += cursor - base + 1;
18282 *byte_pos_ptr = start_byte;
18283 /* When scanning backwards, we should
18284 not count the newline posterior to which we stop. */
18285 return - orig_count - 1;
18286 }
18287 }
18288 else
18289 break;
18290 }
18291 /* Here we add 1 to compensate for the last decrement
18292 of CURSOR, which took it past the valid range. */
18293 start_byte += cursor - base + 1;
18294 }
18295 }
18296
18297 *byte_pos_ptr = limit_byte;
18298
18299 if (count < 0)
18300 return - orig_count + count;
18301 return orig_count - count;
18302
18303 }
18304
18305
18306 \f
18307 /***********************************************************************
18308 Displaying strings
18309 ***********************************************************************/
18310
18311 /* Display a NUL-terminated string, starting with index START.
18312
18313 If STRING is non-null, display that C string. Otherwise, the Lisp
18314 string LISP_STRING is displayed.
18315
18316 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18317 FACE_STRING. Display STRING or LISP_STRING with the face at
18318 FACE_STRING_POS in FACE_STRING:
18319
18320 Display the string in the environment given by IT, but use the
18321 standard display table, temporarily.
18322
18323 FIELD_WIDTH is the minimum number of output glyphs to produce.
18324 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18325 with spaces. If STRING has more characters, more than FIELD_WIDTH
18326 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18327
18328 PRECISION is the maximum number of characters to output from
18329 STRING. PRECISION < 0 means don't truncate the string.
18330
18331 This is roughly equivalent to printf format specifiers:
18332
18333 FIELD_WIDTH PRECISION PRINTF
18334 ----------------------------------------
18335 -1 -1 %s
18336 -1 10 %.10s
18337 10 -1 %10s
18338 20 10 %20.10s
18339
18340 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18341 display them, and < 0 means obey the current buffer's value of
18342 enable_multibyte_characters.
18343
18344 Value is the number of columns displayed. */
18345
18346 static int
18347 display_string (string, lisp_string, face_string, face_string_pos,
18348 start, it, field_width, precision, max_x, multibyte)
18349 unsigned char *string;
18350 Lisp_Object lisp_string;
18351 Lisp_Object face_string;
18352 int face_string_pos;
18353 int start;
18354 struct it *it;
18355 int field_width, precision, max_x;
18356 int multibyte;
18357 {
18358 int hpos_at_start = it->hpos;
18359 int saved_face_id = it->face_id;
18360 struct glyph_row *row = it->glyph_row;
18361
18362 /* Initialize the iterator IT for iteration over STRING beginning
18363 with index START. */
18364 reseat_to_string (it, string, lisp_string, start,
18365 precision, field_width, multibyte);
18366
18367 /* If displaying STRING, set up the face of the iterator
18368 from LISP_STRING, if that's given. */
18369 if (STRINGP (face_string))
18370 {
18371 int endptr;
18372 struct face *face;
18373
18374 it->face_id
18375 = face_at_string_position (it->w, face_string, face_string_pos,
18376 0, it->region_beg_charpos,
18377 it->region_end_charpos,
18378 &endptr, it->base_face_id, 0);
18379 face = FACE_FROM_ID (it->f, it->face_id);
18380 it->face_box_p = face->box != FACE_NO_BOX;
18381 }
18382
18383 /* Set max_x to the maximum allowed X position. Don't let it go
18384 beyond the right edge of the window. */
18385 if (max_x <= 0)
18386 max_x = it->last_visible_x;
18387 else
18388 max_x = min (max_x, it->last_visible_x);
18389
18390 /* Skip over display elements that are not visible. because IT->w is
18391 hscrolled. */
18392 if (it->current_x < it->first_visible_x)
18393 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18394 MOVE_TO_POS | MOVE_TO_X);
18395
18396 row->ascent = it->max_ascent;
18397 row->height = it->max_ascent + it->max_descent;
18398 row->phys_ascent = it->max_phys_ascent;
18399 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18400 row->extra_line_spacing = it->max_extra_line_spacing;
18401
18402 /* This condition is for the case that we are called with current_x
18403 past last_visible_x. */
18404 while (it->current_x < max_x)
18405 {
18406 int x_before, x, n_glyphs_before, i, nglyphs;
18407
18408 /* Get the next display element. */
18409 if (!get_next_display_element (it))
18410 break;
18411
18412 /* Produce glyphs. */
18413 x_before = it->current_x;
18414 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18415 PRODUCE_GLYPHS (it);
18416
18417 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18418 i = 0;
18419 x = x_before;
18420 while (i < nglyphs)
18421 {
18422 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18423
18424 if (!it->truncate_lines_p
18425 && x + glyph->pixel_width > max_x)
18426 {
18427 /* End of continued line or max_x reached. */
18428 if (CHAR_GLYPH_PADDING_P (*glyph))
18429 {
18430 /* A wide character is unbreakable. */
18431 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18432 it->current_x = x_before;
18433 }
18434 else
18435 {
18436 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18437 it->current_x = x;
18438 }
18439 break;
18440 }
18441 else if (x + glyph->pixel_width >= it->first_visible_x)
18442 {
18443 /* Glyph is at least partially visible. */
18444 ++it->hpos;
18445 if (x < it->first_visible_x)
18446 it->glyph_row->x = x - it->first_visible_x;
18447 }
18448 else
18449 {
18450 /* Glyph is off the left margin of the display area.
18451 Should not happen. */
18452 abort ();
18453 }
18454
18455 row->ascent = max (row->ascent, it->max_ascent);
18456 row->height = max (row->height, it->max_ascent + it->max_descent);
18457 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18458 row->phys_height = max (row->phys_height,
18459 it->max_phys_ascent + it->max_phys_descent);
18460 row->extra_line_spacing = max (row->extra_line_spacing,
18461 it->max_extra_line_spacing);
18462 x += glyph->pixel_width;
18463 ++i;
18464 }
18465
18466 /* Stop if max_x reached. */
18467 if (i < nglyphs)
18468 break;
18469
18470 /* Stop at line ends. */
18471 if (ITERATOR_AT_END_OF_LINE_P (it))
18472 {
18473 it->continuation_lines_width = 0;
18474 break;
18475 }
18476
18477 set_iterator_to_next (it, 1);
18478
18479 /* Stop if truncating at the right edge. */
18480 if (it->truncate_lines_p
18481 && it->current_x >= it->last_visible_x)
18482 {
18483 /* Add truncation mark, but don't do it if the line is
18484 truncated at a padding space. */
18485 if (IT_CHARPOS (*it) < it->string_nchars)
18486 {
18487 if (!FRAME_WINDOW_P (it->f))
18488 {
18489 int i, n;
18490
18491 if (it->current_x > it->last_visible_x)
18492 {
18493 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18494 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18495 break;
18496 for (n = row->used[TEXT_AREA]; i < n; ++i)
18497 {
18498 row->used[TEXT_AREA] = i;
18499 produce_special_glyphs (it, IT_TRUNCATION);
18500 }
18501 }
18502 produce_special_glyphs (it, IT_TRUNCATION);
18503 }
18504 it->glyph_row->truncated_on_right_p = 1;
18505 }
18506 break;
18507 }
18508 }
18509
18510 /* Maybe insert a truncation at the left. */
18511 if (it->first_visible_x
18512 && IT_CHARPOS (*it) > 0)
18513 {
18514 if (!FRAME_WINDOW_P (it->f))
18515 insert_left_trunc_glyphs (it);
18516 it->glyph_row->truncated_on_left_p = 1;
18517 }
18518
18519 it->face_id = saved_face_id;
18520
18521 /* Value is number of columns displayed. */
18522 return it->hpos - hpos_at_start;
18523 }
18524
18525
18526 \f
18527 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18528 appears as an element of LIST or as the car of an element of LIST.
18529 If PROPVAL is a list, compare each element against LIST in that
18530 way, and return 1/2 if any element of PROPVAL is found in LIST.
18531 Otherwise return 0. This function cannot quit.
18532 The return value is 2 if the text is invisible but with an ellipsis
18533 and 1 if it's invisible and without an ellipsis. */
18534
18535 int
18536 invisible_p (propval, list)
18537 register Lisp_Object propval;
18538 Lisp_Object list;
18539 {
18540 register Lisp_Object tail, proptail;
18541
18542 for (tail = list; CONSP (tail); tail = XCDR (tail))
18543 {
18544 register Lisp_Object tem;
18545 tem = XCAR (tail);
18546 if (EQ (propval, tem))
18547 return 1;
18548 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18549 return NILP (XCDR (tem)) ? 1 : 2;
18550 }
18551
18552 if (CONSP (propval))
18553 {
18554 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18555 {
18556 Lisp_Object propelt;
18557 propelt = XCAR (proptail);
18558 for (tail = list; CONSP (tail); tail = XCDR (tail))
18559 {
18560 register Lisp_Object tem;
18561 tem = XCAR (tail);
18562 if (EQ (propelt, tem))
18563 return 1;
18564 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18565 return NILP (XCDR (tem)) ? 1 : 2;
18566 }
18567 }
18568 }
18569
18570 return 0;
18571 }
18572
18573 /* Calculate a width or height in pixels from a specification using
18574 the following elements:
18575
18576 SPEC ::=
18577 NUM - a (fractional) multiple of the default font width/height
18578 (NUM) - specifies exactly NUM pixels
18579 UNIT - a fixed number of pixels, see below.
18580 ELEMENT - size of a display element in pixels, see below.
18581 (NUM . SPEC) - equals NUM * SPEC
18582 (+ SPEC SPEC ...) - add pixel values
18583 (- SPEC SPEC ...) - subtract pixel values
18584 (- SPEC) - negate pixel value
18585
18586 NUM ::=
18587 INT or FLOAT - a number constant
18588 SYMBOL - use symbol's (buffer local) variable binding.
18589
18590 UNIT ::=
18591 in - pixels per inch *)
18592 mm - pixels per 1/1000 meter *)
18593 cm - pixels per 1/100 meter *)
18594 width - width of current font in pixels.
18595 height - height of current font in pixels.
18596
18597 *) using the ratio(s) defined in display-pixels-per-inch.
18598
18599 ELEMENT ::=
18600
18601 left-fringe - left fringe width in pixels
18602 right-fringe - right fringe width in pixels
18603
18604 left-margin - left margin width in pixels
18605 right-margin - right margin width in pixels
18606
18607 scroll-bar - scroll-bar area width in pixels
18608
18609 Examples:
18610
18611 Pixels corresponding to 5 inches:
18612 (5 . in)
18613
18614 Total width of non-text areas on left side of window (if scroll-bar is on left):
18615 '(space :width (+ left-fringe left-margin scroll-bar))
18616
18617 Align to first text column (in header line):
18618 '(space :align-to 0)
18619
18620 Align to middle of text area minus half the width of variable `my-image'
18621 containing a loaded image:
18622 '(space :align-to (0.5 . (- text my-image)))
18623
18624 Width of left margin minus width of 1 character in the default font:
18625 '(space :width (- left-margin 1))
18626
18627 Width of left margin minus width of 2 characters in the current font:
18628 '(space :width (- left-margin (2 . width)))
18629
18630 Center 1 character over left-margin (in header line):
18631 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18632
18633 Different ways to express width of left fringe plus left margin minus one pixel:
18634 '(space :width (- (+ left-fringe left-margin) (1)))
18635 '(space :width (+ left-fringe left-margin (- (1))))
18636 '(space :width (+ left-fringe left-margin (-1)))
18637
18638 */
18639
18640 #define NUMVAL(X) \
18641 ((INTEGERP (X) || FLOATP (X)) \
18642 ? XFLOATINT (X) \
18643 : - 1)
18644
18645 int
18646 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18647 double *res;
18648 struct it *it;
18649 Lisp_Object prop;
18650 void *font;
18651 int width_p, *align_to;
18652 {
18653 double pixels;
18654
18655 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18656 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18657
18658 if (NILP (prop))
18659 return OK_PIXELS (0);
18660
18661 if (SYMBOLP (prop))
18662 {
18663 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18664 {
18665 char *unit = SDATA (SYMBOL_NAME (prop));
18666
18667 if (unit[0] == 'i' && unit[1] == 'n')
18668 pixels = 1.0;
18669 else if (unit[0] == 'm' && unit[1] == 'm')
18670 pixels = 25.4;
18671 else if (unit[0] == 'c' && unit[1] == 'm')
18672 pixels = 2.54;
18673 else
18674 pixels = 0;
18675 if (pixels > 0)
18676 {
18677 double ppi;
18678 #ifdef HAVE_WINDOW_SYSTEM
18679 if (FRAME_WINDOW_P (it->f)
18680 && (ppi = (width_p
18681 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18682 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18683 ppi > 0))
18684 return OK_PIXELS (ppi / pixels);
18685 #endif
18686
18687 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18688 || (CONSP (Vdisplay_pixels_per_inch)
18689 && (ppi = (width_p
18690 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18691 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18692 ppi > 0)))
18693 return OK_PIXELS (ppi / pixels);
18694
18695 return 0;
18696 }
18697 }
18698
18699 #ifdef HAVE_WINDOW_SYSTEM
18700 if (EQ (prop, Qheight))
18701 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18702 if (EQ (prop, Qwidth))
18703 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18704 #else
18705 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18706 return OK_PIXELS (1);
18707 #endif
18708
18709 if (EQ (prop, Qtext))
18710 return OK_PIXELS (width_p
18711 ? window_box_width (it->w, TEXT_AREA)
18712 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18713
18714 if (align_to && *align_to < 0)
18715 {
18716 *res = 0;
18717 if (EQ (prop, Qleft))
18718 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18719 if (EQ (prop, Qright))
18720 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18721 if (EQ (prop, Qcenter))
18722 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18723 + window_box_width (it->w, TEXT_AREA) / 2);
18724 if (EQ (prop, Qleft_fringe))
18725 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18726 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18727 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18728 if (EQ (prop, Qright_fringe))
18729 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18730 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18731 : window_box_right_offset (it->w, TEXT_AREA));
18732 if (EQ (prop, Qleft_margin))
18733 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18734 if (EQ (prop, Qright_margin))
18735 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18736 if (EQ (prop, Qscroll_bar))
18737 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18738 ? 0
18739 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18740 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18741 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18742 : 0)));
18743 }
18744 else
18745 {
18746 if (EQ (prop, Qleft_fringe))
18747 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18748 if (EQ (prop, Qright_fringe))
18749 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18750 if (EQ (prop, Qleft_margin))
18751 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18752 if (EQ (prop, Qright_margin))
18753 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18754 if (EQ (prop, Qscroll_bar))
18755 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18756 }
18757
18758 prop = Fbuffer_local_value (prop, it->w->buffer);
18759 }
18760
18761 if (INTEGERP (prop) || FLOATP (prop))
18762 {
18763 int base_unit = (width_p
18764 ? FRAME_COLUMN_WIDTH (it->f)
18765 : FRAME_LINE_HEIGHT (it->f));
18766 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18767 }
18768
18769 if (CONSP (prop))
18770 {
18771 Lisp_Object car = XCAR (prop);
18772 Lisp_Object cdr = XCDR (prop);
18773
18774 if (SYMBOLP (car))
18775 {
18776 #ifdef HAVE_WINDOW_SYSTEM
18777 if (valid_image_p (prop))
18778 {
18779 int id = lookup_image (it->f, prop);
18780 struct image *img = IMAGE_FROM_ID (it->f, id);
18781
18782 return OK_PIXELS (width_p ? img->width : img->height);
18783 }
18784 #endif
18785 if (EQ (car, Qplus) || EQ (car, Qminus))
18786 {
18787 int first = 1;
18788 double px;
18789
18790 pixels = 0;
18791 while (CONSP (cdr))
18792 {
18793 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18794 font, width_p, align_to))
18795 return 0;
18796 if (first)
18797 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18798 else
18799 pixels += px;
18800 cdr = XCDR (cdr);
18801 }
18802 if (EQ (car, Qminus))
18803 pixels = -pixels;
18804 return OK_PIXELS (pixels);
18805 }
18806
18807 car = Fbuffer_local_value (car, it->w->buffer);
18808 }
18809
18810 if (INTEGERP (car) || FLOATP (car))
18811 {
18812 double fact;
18813 pixels = XFLOATINT (car);
18814 if (NILP (cdr))
18815 return OK_PIXELS (pixels);
18816 if (calc_pixel_width_or_height (&fact, it, cdr,
18817 font, width_p, align_to))
18818 return OK_PIXELS (pixels * fact);
18819 return 0;
18820 }
18821
18822 return 0;
18823 }
18824
18825 return 0;
18826 }
18827
18828 \f
18829 /***********************************************************************
18830 Glyph Display
18831 ***********************************************************************/
18832
18833 #ifdef HAVE_WINDOW_SYSTEM
18834
18835 #if GLYPH_DEBUG
18836
18837 void
18838 dump_glyph_string (s)
18839 struct glyph_string *s;
18840 {
18841 fprintf (stderr, "glyph string\n");
18842 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18843 s->x, s->y, s->width, s->height);
18844 fprintf (stderr, " ybase = %d\n", s->ybase);
18845 fprintf (stderr, " hl = %d\n", s->hl);
18846 fprintf (stderr, " left overhang = %d, right = %d\n",
18847 s->left_overhang, s->right_overhang);
18848 fprintf (stderr, " nchars = %d\n", s->nchars);
18849 fprintf (stderr, " extends to end of line = %d\n",
18850 s->extends_to_end_of_line_p);
18851 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18852 fprintf (stderr, " bg width = %d\n", s->background_width);
18853 }
18854
18855 #endif /* GLYPH_DEBUG */
18856
18857 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18858 of XChar2b structures for S; it can't be allocated in
18859 init_glyph_string because it must be allocated via `alloca'. W
18860 is the window on which S is drawn. ROW and AREA are the glyph row
18861 and area within the row from which S is constructed. START is the
18862 index of the first glyph structure covered by S. HL is a
18863 face-override for drawing S. */
18864
18865 #ifdef HAVE_NTGUI
18866 #define OPTIONAL_HDC(hdc) hdc,
18867 #define DECLARE_HDC(hdc) HDC hdc;
18868 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18869 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18870 #endif
18871
18872 #ifndef OPTIONAL_HDC
18873 #define OPTIONAL_HDC(hdc)
18874 #define DECLARE_HDC(hdc)
18875 #define ALLOCATE_HDC(hdc, f)
18876 #define RELEASE_HDC(hdc, f)
18877 #endif
18878
18879 static void
18880 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18881 struct glyph_string *s;
18882 DECLARE_HDC (hdc)
18883 XChar2b *char2b;
18884 struct window *w;
18885 struct glyph_row *row;
18886 enum glyph_row_area area;
18887 int start;
18888 enum draw_glyphs_face hl;
18889 {
18890 bzero (s, sizeof *s);
18891 s->w = w;
18892 s->f = XFRAME (w->frame);
18893 #ifdef HAVE_NTGUI
18894 s->hdc = hdc;
18895 #endif
18896 s->display = FRAME_X_DISPLAY (s->f);
18897 s->window = FRAME_X_WINDOW (s->f);
18898 s->char2b = char2b;
18899 s->hl = hl;
18900 s->row = row;
18901 s->area = area;
18902 s->first_glyph = row->glyphs[area] + start;
18903 s->height = row->height;
18904 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18905
18906 /* Display the internal border below the tool-bar window. */
18907 if (WINDOWP (s->f->tool_bar_window)
18908 && s->w == XWINDOW (s->f->tool_bar_window))
18909 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18910
18911 s->ybase = s->y + row->ascent;
18912 }
18913
18914
18915 /* Append the list of glyph strings with head H and tail T to the list
18916 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18917
18918 static INLINE void
18919 append_glyph_string_lists (head, tail, h, t)
18920 struct glyph_string **head, **tail;
18921 struct glyph_string *h, *t;
18922 {
18923 if (h)
18924 {
18925 if (*head)
18926 (*tail)->next = h;
18927 else
18928 *head = h;
18929 h->prev = *tail;
18930 *tail = t;
18931 }
18932 }
18933
18934
18935 /* Prepend the list of glyph strings with head H and tail T to the
18936 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18937 result. */
18938
18939 static INLINE void
18940 prepend_glyph_string_lists (head, tail, h, t)
18941 struct glyph_string **head, **tail;
18942 struct glyph_string *h, *t;
18943 {
18944 if (h)
18945 {
18946 if (*head)
18947 (*head)->prev = t;
18948 else
18949 *tail = t;
18950 t->next = *head;
18951 *head = h;
18952 }
18953 }
18954
18955
18956 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18957 Set *HEAD and *TAIL to the resulting list. */
18958
18959 static INLINE void
18960 append_glyph_string (head, tail, s)
18961 struct glyph_string **head, **tail;
18962 struct glyph_string *s;
18963 {
18964 s->next = s->prev = NULL;
18965 append_glyph_string_lists (head, tail, s, s);
18966 }
18967
18968
18969 /* Get face and two-byte form of character C in face FACE_ID on frame
18970 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18971 means we want to display multibyte text. DISPLAY_P non-zero means
18972 make sure that X resources for the face returned are allocated.
18973 Value is a pointer to a realized face that is ready for display if
18974 DISPLAY_P is non-zero. */
18975
18976 static INLINE struct face *
18977 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18978 struct frame *f;
18979 int c, face_id;
18980 XChar2b *char2b;
18981 int multibyte_p, display_p;
18982 {
18983 struct face *face = FACE_FROM_ID (f, face_id);
18984
18985 #ifdef USE_FONT_BACKEND
18986 if (enable_font_backend)
18987 {
18988 struct font *font = (struct font *) face->font_info;
18989
18990 if (font)
18991 {
18992 unsigned code = font->driver->encode_char (font, c);
18993
18994 if (code != FONT_INVALID_CODE)
18995 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
18996 else
18997 STORE_XCHAR2B (char2b, 0, 0);
18998 }
18999 }
19000 else
19001 #endif /* USE_FONT_BACKEND */
19002 if (!multibyte_p)
19003 {
19004 /* Unibyte case. We don't have to encode, but we have to make
19005 sure to use a face suitable for unibyte. */
19006 STORE_XCHAR2B (char2b, 0, c);
19007 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19008 face = FACE_FROM_ID (f, face_id);
19009 }
19010 else if (c < 128)
19011 {
19012 /* Case of ASCII in a face known to fit ASCII. */
19013 STORE_XCHAR2B (char2b, 0, c);
19014 }
19015 else if (face->font != NULL)
19016 {
19017 struct font_info *font_info
19018 = FONT_INFO_FROM_ID (f, face->font_info_id);
19019 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19020 unsigned code = ENCODE_CHAR (charset, c);
19021
19022 if (CHARSET_DIMENSION (charset) == 1)
19023 STORE_XCHAR2B (char2b, 0, code);
19024 else
19025 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19026 /* Maybe encode the character in *CHAR2B. */
19027 rif->encode_char (c, char2b, font_info, charset, NULL);
19028 }
19029
19030 /* Make sure X resources of the face are allocated. */
19031 #ifdef HAVE_X_WINDOWS
19032 if (display_p)
19033 #endif
19034 {
19035 xassert (face != NULL);
19036 PREPARE_FACE_FOR_DISPLAY (f, face);
19037 }
19038
19039 return face;
19040 }
19041
19042
19043 /* Get face and two-byte form of character glyph GLYPH on frame F.
19044 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19045 a pointer to a realized face that is ready for display. */
19046
19047 static INLINE struct face *
19048 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19049 struct frame *f;
19050 struct glyph *glyph;
19051 XChar2b *char2b;
19052 int *two_byte_p;
19053 {
19054 struct face *face;
19055
19056 xassert (glyph->type == CHAR_GLYPH);
19057 face = FACE_FROM_ID (f, glyph->face_id);
19058
19059 if (two_byte_p)
19060 *two_byte_p = 0;
19061
19062 #ifdef USE_FONT_BACKEND
19063 if (enable_font_backend)
19064 {
19065 struct font *font = (struct font *) face->font_info;
19066
19067 if (font)
19068 {
19069 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19070
19071 if (code != FONT_INVALID_CODE)
19072 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19073 else
19074 STORE_XCHAR2B (char2b, 0, code);
19075 }
19076 }
19077 else
19078 #endif /* USE_FONT_BACKEND */
19079 if (!glyph->multibyte_p)
19080 {
19081 /* Unibyte case. We don't have to encode, but we have to make
19082 sure to use a face suitable for unibyte. */
19083 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19084 }
19085 else if (glyph->u.ch < 128)
19086 {
19087 /* Case of ASCII in a face known to fit ASCII. */
19088 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19089 }
19090 else
19091 {
19092 struct font_info *font_info
19093 = FONT_INFO_FROM_ID (f, face->font_info_id);
19094 if (font_info)
19095 {
19096 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19097 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19098
19099 if (CHARSET_DIMENSION (charset) == 1)
19100 STORE_XCHAR2B (char2b, 0, code);
19101 else
19102 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19103
19104 /* Maybe encode the character in *CHAR2B. */
19105 if (CHARSET_ID (charset) != charset_ascii)
19106 {
19107 glyph->font_type
19108 = rif->encode_char (glyph->u.ch, char2b, font_info, charset,
19109 two_byte_p);
19110 }
19111 }
19112 }
19113
19114 /* Make sure X resources of the face are allocated. */
19115 xassert (face != NULL);
19116 PREPARE_FACE_FOR_DISPLAY (f, face);
19117 return face;
19118 }
19119
19120
19121 /* Fill glyph string S with composition components specified by S->cmp.
19122
19123 BASE_FACE is the base face of the composition.
19124 S->gidx is the index of the first component for S.
19125
19126 OVERLAPS non-zero means S should draw the foreground only, and use
19127 its physical height for clipping. See also draw_glyphs.
19128
19129 Value is the index of a component not in S. */
19130
19131 static int
19132 fill_composite_glyph_string (s, base_face, overlaps)
19133 struct glyph_string *s;
19134 struct face *base_face;
19135 int overlaps;
19136 {
19137 int i;
19138
19139 xassert (s);
19140
19141 s->for_overlaps = overlaps;
19142
19143 #ifdef USE_FONT_BACKEND
19144 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19145 {
19146 Lisp_Object gstring
19147 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19148 s->cmp->hash_index * 2);
19149
19150 s->face = base_face;
19151 s->font_info = s->cmp->font;
19152 s->font = s->font_info->font;
19153 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19154 {
19155 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19156 unsigned code;
19157 XChar2b * store_pos;
19158 if (NILP (LGLYPH_FROM (g)))
19159 break;
19160 code = XUINT (LGLYPH_CODE (g));
19161 store_pos = s->char2b + i;
19162 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19163 }
19164 s->width = s->cmp->pixel_width;
19165 }
19166 else
19167 #endif /* USE_FONT_BACKEND */
19168 {
19169 /* For all glyphs of this composition, starting at the offset
19170 S->gidx, until we reach the end of the definition or encounter a
19171 glyph that requires the different face, add it to S. */
19172 struct face *face;
19173
19174 s->face = NULL;
19175 s->font = NULL;
19176 s->font_info = NULL;
19177 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19178 {
19179 int c = COMPOSITION_GLYPH (s->cmp, i);
19180
19181 if (c != '\t')
19182 {
19183 int face_id = FACE_FOR_CHAR (s->f, base_face, c, -1, Qnil);
19184
19185 face = get_char_face_and_encoding (s->f, c, face_id,
19186 s->char2b + i, 1, 1);
19187 if (face)
19188 {
19189 if (! s->face)
19190 {
19191 s->face = face;
19192 s->font = s->face->font;
19193 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19194 }
19195 else if (s->face != face)
19196 break;
19197 }
19198 }
19199 ++s->nchars;
19200 }
19201
19202 /* All glyph strings for the same composition has the same width,
19203 i.e. the width set for the first component of the composition. */
19204 s->width = s->first_glyph->pixel_width;
19205 }
19206
19207 /* If the specified font could not be loaded, use the frame's
19208 default font, but record the fact that we couldn't load it in
19209 the glyph string so that we can draw rectangles for the
19210 characters of the glyph string. */
19211 if (s->font == NULL)
19212 {
19213 s->font_not_found_p = 1;
19214 s->font = FRAME_FONT (s->f);
19215 }
19216
19217 /* Adjust base line for subscript/superscript text. */
19218 s->ybase += s->first_glyph->voffset;
19219
19220 /* This glyph string must always be drawn with 16-bit functions. */
19221 s->two_byte_p = 1;
19222
19223 return s->gidx + s->nchars;
19224 }
19225
19226
19227 /* Fill glyph string S from a sequence of character glyphs.
19228
19229 FACE_ID is the face id of the string. START is the index of the
19230 first glyph to consider, END is the index of the last + 1.
19231 OVERLAPS non-zero means S should draw the foreground only, and use
19232 its physical height for clipping. See also draw_glyphs.
19233
19234 Value is the index of the first glyph not in S. */
19235
19236 static int
19237 fill_glyph_string (s, face_id, start, end, overlaps)
19238 struct glyph_string *s;
19239 int face_id;
19240 int start, end, overlaps;
19241 {
19242 struct glyph *glyph, *last;
19243 int voffset;
19244 int glyph_not_available_p;
19245
19246 xassert (s->f == XFRAME (s->w->frame));
19247 xassert (s->nchars == 0);
19248 xassert (start >= 0 && end > start);
19249
19250 s->for_overlaps = overlaps,
19251 glyph = s->row->glyphs[s->area] + start;
19252 last = s->row->glyphs[s->area] + end;
19253 voffset = glyph->voffset;
19254
19255 glyph_not_available_p = glyph->glyph_not_available_p;
19256
19257 while (glyph < last
19258 && glyph->type == CHAR_GLYPH
19259 && glyph->voffset == voffset
19260 /* Same face id implies same font, nowadays. */
19261 && glyph->face_id == face_id
19262 && glyph->glyph_not_available_p == glyph_not_available_p)
19263 {
19264 int two_byte_p;
19265
19266 s->face = get_glyph_face_and_encoding (s->f, glyph,
19267 s->char2b + s->nchars,
19268 &two_byte_p);
19269 s->two_byte_p = two_byte_p;
19270 ++s->nchars;
19271 xassert (s->nchars <= end - start);
19272 s->width += glyph->pixel_width;
19273 ++glyph;
19274 }
19275
19276 s->font = s->face->font;
19277 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19278
19279 /* If the specified font could not be loaded, use the frame's font,
19280 but record the fact that we couldn't load it in
19281 S->font_not_found_p so that we can draw rectangles for the
19282 characters of the glyph string. */
19283 if (s->font == NULL || glyph_not_available_p)
19284 {
19285 s->font_not_found_p = 1;
19286 s->font = FRAME_FONT (s->f);
19287 }
19288
19289 /* Adjust base line for subscript/superscript text. */
19290 s->ybase += voffset;
19291
19292 xassert (s->face && s->face->gc);
19293 return glyph - s->row->glyphs[s->area];
19294 }
19295
19296
19297 /* Fill glyph string S from image glyph S->first_glyph. */
19298
19299 static void
19300 fill_image_glyph_string (s)
19301 struct glyph_string *s;
19302 {
19303 xassert (s->first_glyph->type == IMAGE_GLYPH);
19304 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19305 xassert (s->img);
19306 s->slice = s->first_glyph->slice;
19307 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19308 s->font = s->face->font;
19309 s->width = s->first_glyph->pixel_width;
19310
19311 /* Adjust base line for subscript/superscript text. */
19312 s->ybase += s->first_glyph->voffset;
19313 }
19314
19315
19316 /* Fill glyph string S from a sequence of stretch glyphs.
19317
19318 ROW is the glyph row in which the glyphs are found, AREA is the
19319 area within the row. START is the index of the first glyph to
19320 consider, END is the index of the last + 1.
19321
19322 Value is the index of the first glyph not in S. */
19323
19324 static int
19325 fill_stretch_glyph_string (s, row, area, start, end)
19326 struct glyph_string *s;
19327 struct glyph_row *row;
19328 enum glyph_row_area area;
19329 int start, end;
19330 {
19331 struct glyph *glyph, *last;
19332 int voffset, face_id;
19333
19334 xassert (s->first_glyph->type == STRETCH_GLYPH);
19335
19336 glyph = s->row->glyphs[s->area] + start;
19337 last = s->row->glyphs[s->area] + end;
19338 face_id = glyph->face_id;
19339 s->face = FACE_FROM_ID (s->f, face_id);
19340 s->font = s->face->font;
19341 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19342 s->width = glyph->pixel_width;
19343 s->nchars = 1;
19344 voffset = glyph->voffset;
19345
19346 for (++glyph;
19347 (glyph < last
19348 && glyph->type == STRETCH_GLYPH
19349 && glyph->voffset == voffset
19350 && glyph->face_id == face_id);
19351 ++glyph)
19352 s->width += glyph->pixel_width;
19353
19354 /* Adjust base line for subscript/superscript text. */
19355 s->ybase += voffset;
19356
19357 /* The case that face->gc == 0 is handled when drawing the glyph
19358 string by calling PREPARE_FACE_FOR_DISPLAY. */
19359 xassert (s->face);
19360 return glyph - s->row->glyphs[s->area];
19361 }
19362
19363 static XCharStruct *
19364 get_per_char_metric (font, font_info, char2b, font_type)
19365 XFontStruct *font;
19366 struct font_info *font_info;
19367 XChar2b *char2b;
19368 int font_type;
19369 {
19370 #ifdef USE_FONT_BACKEND
19371 if (enable_font_backend)
19372 {
19373 static XCharStruct pcm_value;
19374 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19375 struct font *fontp;
19376 struct font_metrics metrics;
19377
19378 if (! font_info || code == FONT_INVALID_CODE)
19379 return NULL;
19380 fontp = (struct font *) font_info;
19381 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19382 pcm_value.lbearing = metrics.lbearing;
19383 pcm_value.rbearing = metrics.rbearing;
19384 pcm_value.ascent = metrics.ascent;
19385 pcm_value.descent = metrics.descent;
19386 pcm_value.width = metrics.width;
19387 return &pcm_value;
19388 }
19389 #endif /* USE_FONT_BACKEND */
19390 return rif->per_char_metric (font, char2b, font_type);
19391 }
19392
19393 /* EXPORT for RIF:
19394 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19395 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19396 assumed to be zero. */
19397
19398 void
19399 x_get_glyph_overhangs (glyph, f, left, right)
19400 struct glyph *glyph;
19401 struct frame *f;
19402 int *left, *right;
19403 {
19404 *left = *right = 0;
19405
19406 if (glyph->type == CHAR_GLYPH)
19407 {
19408 XFontStruct *font;
19409 struct face *face;
19410 struct font_info *font_info;
19411 XChar2b char2b;
19412 XCharStruct *pcm;
19413
19414 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19415 font = face->font;
19416 font_info = FONT_INFO_FROM_FACE (f, face);
19417 if (font /* ++KFS: Should this be font_info ? */
19418 && (pcm = get_per_char_metric (font, font_info, &char2b, glyph->font_type)))
19419 {
19420 if (pcm->rbearing > pcm->width)
19421 *right = pcm->rbearing - pcm->width;
19422 if (pcm->lbearing < 0)
19423 *left = -pcm->lbearing;
19424 }
19425 }
19426 else if (glyph->type == COMPOSITE_GLYPH)
19427 {
19428 struct composition *cmp = composition_table[glyph->u.cmp_id];
19429
19430 *right = cmp->rbearing - cmp->pixel_width;
19431 *left = - cmp->lbearing;
19432 }
19433 }
19434
19435
19436 /* Return the index of the first glyph preceding glyph string S that
19437 is overwritten by S because of S's left overhang. Value is -1
19438 if no glyphs are overwritten. */
19439
19440 static int
19441 left_overwritten (s)
19442 struct glyph_string *s;
19443 {
19444 int k;
19445
19446 if (s->left_overhang)
19447 {
19448 int x = 0, i;
19449 struct glyph *glyphs = s->row->glyphs[s->area];
19450 int first = s->first_glyph - glyphs;
19451
19452 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19453 x -= glyphs[i].pixel_width;
19454
19455 k = i + 1;
19456 }
19457 else
19458 k = -1;
19459
19460 return k;
19461 }
19462
19463
19464 /* Return the index of the first glyph preceding glyph string S that
19465 is overwriting S because of its right overhang. Value is -1 if no
19466 glyph in front of S overwrites S. */
19467
19468 static int
19469 left_overwriting (s)
19470 struct glyph_string *s;
19471 {
19472 int i, k, x;
19473 struct glyph *glyphs = s->row->glyphs[s->area];
19474 int first = s->first_glyph - glyphs;
19475
19476 k = -1;
19477 x = 0;
19478 for (i = first - 1; i >= 0; --i)
19479 {
19480 int left, right;
19481 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19482 if (x + right > 0)
19483 k = i;
19484 x -= glyphs[i].pixel_width;
19485 }
19486
19487 return k;
19488 }
19489
19490
19491 /* Return the index of the last glyph following glyph string S that is
19492 not overwritten by S because of S's right overhang. Value is -1 if
19493 no such glyph is found. */
19494
19495 static int
19496 right_overwritten (s)
19497 struct glyph_string *s;
19498 {
19499 int k = -1;
19500
19501 if (s->right_overhang)
19502 {
19503 int x = 0, i;
19504 struct glyph *glyphs = s->row->glyphs[s->area];
19505 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19506 int end = s->row->used[s->area];
19507
19508 for (i = first; i < end && s->right_overhang > x; ++i)
19509 x += glyphs[i].pixel_width;
19510
19511 k = i;
19512 }
19513
19514 return k;
19515 }
19516
19517
19518 /* Return the index of the last glyph following glyph string S that
19519 overwrites S because of its left overhang. Value is negative
19520 if no such glyph is found. */
19521
19522 static int
19523 right_overwriting (s)
19524 struct glyph_string *s;
19525 {
19526 int i, k, x;
19527 int end = s->row->used[s->area];
19528 struct glyph *glyphs = s->row->glyphs[s->area];
19529 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19530
19531 k = -1;
19532 x = 0;
19533 for (i = first; i < end; ++i)
19534 {
19535 int left, right;
19536 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19537 if (x - left < 0)
19538 k = i;
19539 x += glyphs[i].pixel_width;
19540 }
19541
19542 return k;
19543 }
19544
19545
19546 /* Set background width of glyph string S. START is the index of the
19547 first glyph following S. LAST_X is the right-most x-position + 1
19548 in the drawing area. */
19549
19550 static INLINE void
19551 set_glyph_string_background_width (s, start, last_x)
19552 struct glyph_string *s;
19553 int start;
19554 int last_x;
19555 {
19556 /* If the face of this glyph string has to be drawn to the end of
19557 the drawing area, set S->extends_to_end_of_line_p. */
19558
19559 if (start == s->row->used[s->area]
19560 && s->area == TEXT_AREA
19561 && ((s->row->fill_line_p
19562 && (s->hl == DRAW_NORMAL_TEXT
19563 || s->hl == DRAW_IMAGE_RAISED
19564 || s->hl == DRAW_IMAGE_SUNKEN))
19565 || s->hl == DRAW_MOUSE_FACE))
19566 s->extends_to_end_of_line_p = 1;
19567
19568 /* If S extends its face to the end of the line, set its
19569 background_width to the distance to the right edge of the drawing
19570 area. */
19571 if (s->extends_to_end_of_line_p)
19572 s->background_width = last_x - s->x + 1;
19573 else
19574 s->background_width = s->width;
19575 }
19576
19577
19578 /* Compute overhangs and x-positions for glyph string S and its
19579 predecessors, or successors. X is the starting x-position for S.
19580 BACKWARD_P non-zero means process predecessors. */
19581
19582 static void
19583 compute_overhangs_and_x (s, x, backward_p)
19584 struct glyph_string *s;
19585 int x;
19586 int backward_p;
19587 {
19588 if (backward_p)
19589 {
19590 while (s)
19591 {
19592 if (rif->compute_glyph_string_overhangs)
19593 rif->compute_glyph_string_overhangs (s);
19594 x -= s->width;
19595 s->x = x;
19596 s = s->prev;
19597 }
19598 }
19599 else
19600 {
19601 while (s)
19602 {
19603 if (rif->compute_glyph_string_overhangs)
19604 rif->compute_glyph_string_overhangs (s);
19605 s->x = x;
19606 x += s->width;
19607 s = s->next;
19608 }
19609 }
19610 }
19611
19612
19613
19614 /* The following macros are only called from draw_glyphs below.
19615 They reference the following parameters of that function directly:
19616 `w', `row', `area', and `overlap_p'
19617 as well as the following local variables:
19618 `s', `f', and `hdc' (in W32) */
19619
19620 #ifdef HAVE_NTGUI
19621 /* On W32, silently add local `hdc' variable to argument list of
19622 init_glyph_string. */
19623 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19624 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19625 #else
19626 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19627 init_glyph_string (s, char2b, w, row, area, start, hl)
19628 #endif
19629
19630 /* Add a glyph string for a stretch glyph to the list of strings
19631 between HEAD and TAIL. START is the index of the stretch glyph in
19632 row area AREA of glyph row ROW. END is the index of the last glyph
19633 in that glyph row area. X is the current output position assigned
19634 to the new glyph string constructed. HL overrides that face of the
19635 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19636 is the right-most x-position of the drawing area. */
19637
19638 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19639 and below -- keep them on one line. */
19640 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19641 do \
19642 { \
19643 s = (struct glyph_string *) alloca (sizeof *s); \
19644 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19645 START = fill_stretch_glyph_string (s, row, area, START, END); \
19646 append_glyph_string (&HEAD, &TAIL, s); \
19647 s->x = (X); \
19648 } \
19649 while (0)
19650
19651
19652 /* Add a glyph string for an image glyph to the list of strings
19653 between HEAD and TAIL. START is the index of the image glyph in
19654 row area AREA of glyph row ROW. END is the index of the last glyph
19655 in that glyph row area. X is the current output position assigned
19656 to the new glyph string constructed. HL overrides that face of the
19657 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19658 is the right-most x-position of the drawing area. */
19659
19660 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19661 do \
19662 { \
19663 s = (struct glyph_string *) alloca (sizeof *s); \
19664 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19665 fill_image_glyph_string (s); \
19666 append_glyph_string (&HEAD, &TAIL, s); \
19667 ++START; \
19668 s->x = (X); \
19669 } \
19670 while (0)
19671
19672
19673 /* Add a glyph string for a sequence of character glyphs to the list
19674 of strings between HEAD and TAIL. START is the index of the first
19675 glyph in row area AREA of glyph row ROW that is part of the new
19676 glyph string. END is the index of the last glyph in that glyph row
19677 area. X is the current output position assigned to the new glyph
19678 string constructed. HL overrides that face of the glyph; e.g. it
19679 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19680 right-most x-position of the drawing area. */
19681
19682 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19683 do \
19684 { \
19685 int face_id; \
19686 XChar2b *char2b; \
19687 \
19688 face_id = (row)->glyphs[area][START].face_id; \
19689 \
19690 s = (struct glyph_string *) alloca (sizeof *s); \
19691 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19692 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19693 append_glyph_string (&HEAD, &TAIL, s); \
19694 s->x = (X); \
19695 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19696 } \
19697 while (0)
19698
19699
19700 /* Add a glyph string for a composite sequence to the list of strings
19701 between HEAD and TAIL. START is the index of the first glyph in
19702 row area AREA of glyph row ROW that is part of the new glyph
19703 string. END is the index of the last glyph in that glyph row area.
19704 X is the current output position assigned to the new glyph string
19705 constructed. HL overrides that face of the glyph; e.g. it is
19706 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19707 x-position of the drawing area. */
19708
19709 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19710 do { \
19711 int face_id = (row)->glyphs[area][START].face_id; \
19712 struct face *base_face = FACE_FROM_ID (f, face_id); \
19713 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19714 struct composition *cmp = composition_table[cmp_id]; \
19715 XChar2b *char2b; \
19716 struct glyph_string *first_s; \
19717 int n; \
19718 \
19719 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19720 base_face = base_face->ascii_face; \
19721 \
19722 /* Make glyph_strings for each glyph sequence that is drawable by \
19723 the same face, and append them to HEAD/TAIL. */ \
19724 for (n = 0; n < cmp->glyph_len;) \
19725 { \
19726 s = (struct glyph_string *) alloca (sizeof *s); \
19727 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19728 append_glyph_string (&(HEAD), &(TAIL), s); \
19729 s->cmp = cmp; \
19730 s->gidx = n; \
19731 s->x = (X); \
19732 if (n == 0) \
19733 first_s = s; \
19734 n = fill_composite_glyph_string (s, base_face, overlaps); \
19735 } \
19736 \
19737 ++START; \
19738 s = first_s; \
19739 } while (0)
19740
19741
19742 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19743 of AREA of glyph row ROW on window W between indices START and END.
19744 HL overrides the face for drawing glyph strings, e.g. it is
19745 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19746 x-positions of the drawing area.
19747
19748 This is an ugly monster macro construct because we must use alloca
19749 to allocate glyph strings (because draw_glyphs can be called
19750 asynchronously). */
19751
19752 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19753 do \
19754 { \
19755 HEAD = TAIL = NULL; \
19756 while (START < END) \
19757 { \
19758 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19759 switch (first_glyph->type) \
19760 { \
19761 case CHAR_GLYPH: \
19762 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19763 HL, X, LAST_X); \
19764 break; \
19765 \
19766 case COMPOSITE_GLYPH: \
19767 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19768 HL, X, LAST_X); \
19769 break; \
19770 \
19771 case STRETCH_GLYPH: \
19772 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19773 HL, X, LAST_X); \
19774 break; \
19775 \
19776 case IMAGE_GLYPH: \
19777 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19778 HL, X, LAST_X); \
19779 break; \
19780 \
19781 default: \
19782 abort (); \
19783 } \
19784 \
19785 if (s) \
19786 { \
19787 set_glyph_string_background_width (s, START, LAST_X); \
19788 (X) += s->width; \
19789 } \
19790 } \
19791 } \
19792 while (0)
19793
19794
19795 /* Draw glyphs between START and END in AREA of ROW on window W,
19796 starting at x-position X. X is relative to AREA in W. HL is a
19797 face-override with the following meaning:
19798
19799 DRAW_NORMAL_TEXT draw normally
19800 DRAW_CURSOR draw in cursor face
19801 DRAW_MOUSE_FACE draw in mouse face.
19802 DRAW_INVERSE_VIDEO draw in mode line face
19803 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19804 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19805
19806 If OVERLAPS is non-zero, draw only the foreground of characters and
19807 clip to the physical height of ROW. Non-zero value also defines
19808 the overlapping part to be drawn:
19809
19810 OVERLAPS_PRED overlap with preceding rows
19811 OVERLAPS_SUCC overlap with succeeding rows
19812 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19813 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19814
19815 Value is the x-position reached, relative to AREA of W. */
19816
19817 static int
19818 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19819 struct window *w;
19820 int x;
19821 struct glyph_row *row;
19822 enum glyph_row_area area;
19823 EMACS_INT start, end;
19824 enum draw_glyphs_face hl;
19825 int overlaps;
19826 {
19827 struct glyph_string *head, *tail;
19828 struct glyph_string *s;
19829 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19830 int last_x, area_width;
19831 int x_reached;
19832 int i, j;
19833 struct frame *f = XFRAME (WINDOW_FRAME (w));
19834 DECLARE_HDC (hdc);
19835
19836 ALLOCATE_HDC (hdc, f);
19837
19838 /* Let's rather be paranoid than getting a SEGV. */
19839 end = min (end, row->used[area]);
19840 start = max (0, start);
19841 start = min (end, start);
19842
19843 /* Translate X to frame coordinates. Set last_x to the right
19844 end of the drawing area. */
19845 if (row->full_width_p)
19846 {
19847 /* X is relative to the left edge of W, without scroll bars
19848 or fringes. */
19849 x += WINDOW_LEFT_EDGE_X (w);
19850 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19851 }
19852 else
19853 {
19854 int area_left = window_box_left (w, area);
19855 x += area_left;
19856 area_width = window_box_width (w, area);
19857 last_x = area_left + area_width;
19858 }
19859
19860 /* Build a doubly-linked list of glyph_string structures between
19861 head and tail from what we have to draw. Note that the macro
19862 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19863 the reason we use a separate variable `i'. */
19864 i = start;
19865 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19866 if (tail)
19867 x_reached = tail->x + tail->background_width;
19868 else
19869 x_reached = x;
19870
19871 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19872 the row, redraw some glyphs in front or following the glyph
19873 strings built above. */
19874 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19875 {
19876 int dummy_x = 0;
19877 struct glyph_string *h, *t;
19878
19879 /* Compute overhangs for all glyph strings. */
19880 if (rif->compute_glyph_string_overhangs)
19881 for (s = head; s; s = s->next)
19882 rif->compute_glyph_string_overhangs (s);
19883
19884 /* Prepend glyph strings for glyphs in front of the first glyph
19885 string that are overwritten because of the first glyph
19886 string's left overhang. The background of all strings
19887 prepended must be drawn because the first glyph string
19888 draws over it. */
19889 i = left_overwritten (head);
19890 if (i >= 0)
19891 {
19892 j = i;
19893 BUILD_GLYPH_STRINGS (j, start, h, t,
19894 DRAW_NORMAL_TEXT, dummy_x, last_x);
19895 start = i;
19896 compute_overhangs_and_x (t, head->x, 1);
19897 prepend_glyph_string_lists (&head, &tail, h, t);
19898 clip_head = head;
19899 }
19900
19901 /* Prepend glyph strings for glyphs in front of the first glyph
19902 string that overwrite that glyph string because of their
19903 right overhang. For these strings, only the foreground must
19904 be drawn, because it draws over the glyph string at `head'.
19905 The background must not be drawn because this would overwrite
19906 right overhangs of preceding glyphs for which no glyph
19907 strings exist. */
19908 i = left_overwriting (head);
19909 if (i >= 0)
19910 {
19911 clip_head = head;
19912 BUILD_GLYPH_STRINGS (i, start, h, t,
19913 DRAW_NORMAL_TEXT, dummy_x, last_x);
19914 for (s = h; s; s = s->next)
19915 s->background_filled_p = 1;
19916 compute_overhangs_and_x (t, head->x, 1);
19917 prepend_glyph_string_lists (&head, &tail, h, t);
19918 }
19919
19920 /* Append glyphs strings for glyphs following the last glyph
19921 string tail that are overwritten by tail. The background of
19922 these strings has to be drawn because tail's foreground draws
19923 over it. */
19924 i = right_overwritten (tail);
19925 if (i >= 0)
19926 {
19927 BUILD_GLYPH_STRINGS (end, i, h, t,
19928 DRAW_NORMAL_TEXT, x, last_x);
19929 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19930 append_glyph_string_lists (&head, &tail, h, t);
19931 clip_tail = tail;
19932 }
19933
19934 /* Append glyph strings for glyphs following the last glyph
19935 string tail that overwrite tail. The foreground of such
19936 glyphs has to be drawn because it writes into the background
19937 of tail. The background must not be drawn because it could
19938 paint over the foreground of following glyphs. */
19939 i = right_overwriting (tail);
19940 if (i >= 0)
19941 {
19942 clip_tail = tail;
19943 i++; /* We must include the Ith glyph. */
19944 BUILD_GLYPH_STRINGS (end, i, h, t,
19945 DRAW_NORMAL_TEXT, x, last_x);
19946 for (s = h; s; s = s->next)
19947 s->background_filled_p = 1;
19948 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19949 append_glyph_string_lists (&head, &tail, h, t);
19950 }
19951 if (clip_head || clip_tail)
19952 for (s = head; s; s = s->next)
19953 {
19954 s->clip_head = clip_head;
19955 s->clip_tail = clip_tail;
19956 }
19957 }
19958
19959 /* Draw all strings. */
19960 for (s = head; s; s = s->next)
19961 rif->draw_glyph_string (s);
19962
19963 if (area == TEXT_AREA
19964 && !row->full_width_p
19965 /* When drawing overlapping rows, only the glyph strings'
19966 foreground is drawn, which doesn't erase a cursor
19967 completely. */
19968 && !overlaps)
19969 {
19970 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19971 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19972 : (tail ? tail->x + tail->background_width : x));
19973
19974 int text_left = window_box_left (w, TEXT_AREA);
19975 x0 -= text_left;
19976 x1 -= text_left;
19977
19978 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19979 row->y, MATRIX_ROW_BOTTOM_Y (row));
19980 }
19981
19982 /* Value is the x-position up to which drawn, relative to AREA of W.
19983 This doesn't include parts drawn because of overhangs. */
19984 if (row->full_width_p)
19985 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19986 else
19987 x_reached -= window_box_left (w, area);
19988
19989 RELEASE_HDC (hdc, f);
19990
19991 return x_reached;
19992 }
19993
19994 /* Expand row matrix if too narrow. Don't expand if area
19995 is not present. */
19996
19997 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19998 { \
19999 if (!fonts_changed_p \
20000 && (it->glyph_row->glyphs[area] \
20001 < it->glyph_row->glyphs[area + 1])) \
20002 { \
20003 it->w->ncols_scale_factor++; \
20004 fonts_changed_p = 1; \
20005 } \
20006 }
20007
20008 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20009 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20010
20011 static INLINE void
20012 append_glyph (it)
20013 struct it *it;
20014 {
20015 struct glyph *glyph;
20016 enum glyph_row_area area = it->area;
20017
20018 xassert (it->glyph_row);
20019 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20020
20021 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20022 if (glyph < it->glyph_row->glyphs[area + 1])
20023 {
20024 glyph->charpos = CHARPOS (it->position);
20025 glyph->object = it->object;
20026 glyph->pixel_width = it->pixel_width;
20027 glyph->ascent = it->ascent;
20028 glyph->descent = it->descent;
20029 glyph->voffset = it->voffset;
20030 glyph->type = CHAR_GLYPH;
20031 glyph->multibyte_p = it->multibyte_p;
20032 glyph->left_box_line_p = it->start_of_box_run_p;
20033 glyph->right_box_line_p = it->end_of_box_run_p;
20034 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20035 || it->phys_descent > it->descent);
20036 glyph->padding_p = 0;
20037 glyph->glyph_not_available_p = it->glyph_not_available_p;
20038 glyph->face_id = it->face_id;
20039 glyph->u.ch = it->char_to_display;
20040 glyph->slice = null_glyph_slice;
20041 glyph->font_type = FONT_TYPE_UNKNOWN;
20042 ++it->glyph_row->used[area];
20043 }
20044 else
20045 IT_EXPAND_MATRIX_WIDTH (it, area);
20046 }
20047
20048 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20049 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20050
20051 static INLINE void
20052 append_composite_glyph (it)
20053 struct it *it;
20054 {
20055 struct glyph *glyph;
20056 enum glyph_row_area area = it->area;
20057
20058 xassert (it->glyph_row);
20059
20060 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20061 if (glyph < it->glyph_row->glyphs[area + 1])
20062 {
20063 glyph->charpos = CHARPOS (it->position);
20064 glyph->object = it->object;
20065 glyph->pixel_width = it->pixel_width;
20066 glyph->ascent = it->ascent;
20067 glyph->descent = it->descent;
20068 glyph->voffset = it->voffset;
20069 glyph->type = COMPOSITE_GLYPH;
20070 glyph->multibyte_p = it->multibyte_p;
20071 glyph->left_box_line_p = it->start_of_box_run_p;
20072 glyph->right_box_line_p = it->end_of_box_run_p;
20073 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20074 || it->phys_descent > it->descent);
20075 glyph->padding_p = 0;
20076 glyph->glyph_not_available_p = 0;
20077 glyph->face_id = it->face_id;
20078 glyph->u.cmp_id = it->cmp_id;
20079 glyph->slice = null_glyph_slice;
20080 glyph->font_type = FONT_TYPE_UNKNOWN;
20081 ++it->glyph_row->used[area];
20082 }
20083 else
20084 IT_EXPAND_MATRIX_WIDTH (it, area);
20085 }
20086
20087
20088 /* Change IT->ascent and IT->height according to the setting of
20089 IT->voffset. */
20090
20091 static INLINE void
20092 take_vertical_position_into_account (it)
20093 struct it *it;
20094 {
20095 if (it->voffset)
20096 {
20097 if (it->voffset < 0)
20098 /* Increase the ascent so that we can display the text higher
20099 in the line. */
20100 it->ascent -= it->voffset;
20101 else
20102 /* Increase the descent so that we can display the text lower
20103 in the line. */
20104 it->descent += it->voffset;
20105 }
20106 }
20107
20108
20109 /* Produce glyphs/get display metrics for the image IT is loaded with.
20110 See the description of struct display_iterator in dispextern.h for
20111 an overview of struct display_iterator. */
20112
20113 static void
20114 produce_image_glyph (it)
20115 struct it *it;
20116 {
20117 struct image *img;
20118 struct face *face;
20119 int glyph_ascent, crop;
20120 struct glyph_slice slice;
20121
20122 xassert (it->what == IT_IMAGE);
20123
20124 face = FACE_FROM_ID (it->f, it->face_id);
20125 xassert (face);
20126 /* Make sure X resources of the face is loaded. */
20127 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20128
20129 if (it->image_id < 0)
20130 {
20131 /* Fringe bitmap. */
20132 it->ascent = it->phys_ascent = 0;
20133 it->descent = it->phys_descent = 0;
20134 it->pixel_width = 0;
20135 it->nglyphs = 0;
20136 return;
20137 }
20138
20139 img = IMAGE_FROM_ID (it->f, it->image_id);
20140 xassert (img);
20141 /* Make sure X resources of the image is loaded. */
20142 prepare_image_for_display (it->f, img);
20143
20144 slice.x = slice.y = 0;
20145 slice.width = img->width;
20146 slice.height = img->height;
20147
20148 if (INTEGERP (it->slice.x))
20149 slice.x = XINT (it->slice.x);
20150 else if (FLOATP (it->slice.x))
20151 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20152
20153 if (INTEGERP (it->slice.y))
20154 slice.y = XINT (it->slice.y);
20155 else if (FLOATP (it->slice.y))
20156 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20157
20158 if (INTEGERP (it->slice.width))
20159 slice.width = XINT (it->slice.width);
20160 else if (FLOATP (it->slice.width))
20161 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20162
20163 if (INTEGERP (it->slice.height))
20164 slice.height = XINT (it->slice.height);
20165 else if (FLOATP (it->slice.height))
20166 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20167
20168 if (slice.x >= img->width)
20169 slice.x = img->width;
20170 if (slice.y >= img->height)
20171 slice.y = img->height;
20172 if (slice.x + slice.width >= img->width)
20173 slice.width = img->width - slice.x;
20174 if (slice.y + slice.height > img->height)
20175 slice.height = img->height - slice.y;
20176
20177 if (slice.width == 0 || slice.height == 0)
20178 return;
20179
20180 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20181
20182 it->descent = slice.height - glyph_ascent;
20183 if (slice.y == 0)
20184 it->descent += img->vmargin;
20185 if (slice.y + slice.height == img->height)
20186 it->descent += img->vmargin;
20187 it->phys_descent = it->descent;
20188
20189 it->pixel_width = slice.width;
20190 if (slice.x == 0)
20191 it->pixel_width += img->hmargin;
20192 if (slice.x + slice.width == img->width)
20193 it->pixel_width += img->hmargin;
20194
20195 /* It's quite possible for images to have an ascent greater than
20196 their height, so don't get confused in that case. */
20197 if (it->descent < 0)
20198 it->descent = 0;
20199
20200 #if 0 /* this breaks image tiling */
20201 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20202 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20203 if (face_ascent > it->ascent)
20204 it->ascent = it->phys_ascent = face_ascent;
20205 #endif
20206
20207 it->nglyphs = 1;
20208
20209 if (face->box != FACE_NO_BOX)
20210 {
20211 if (face->box_line_width > 0)
20212 {
20213 if (slice.y == 0)
20214 it->ascent += face->box_line_width;
20215 if (slice.y + slice.height == img->height)
20216 it->descent += face->box_line_width;
20217 }
20218
20219 if (it->start_of_box_run_p && slice.x == 0)
20220 it->pixel_width += abs (face->box_line_width);
20221 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20222 it->pixel_width += abs (face->box_line_width);
20223 }
20224
20225 take_vertical_position_into_account (it);
20226
20227 /* Automatically crop wide image glyphs at right edge so we can
20228 draw the cursor on same display row. */
20229 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20230 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20231 {
20232 it->pixel_width -= crop;
20233 slice.width -= crop;
20234 }
20235
20236 if (it->glyph_row)
20237 {
20238 struct glyph *glyph;
20239 enum glyph_row_area area = it->area;
20240
20241 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20242 if (glyph < it->glyph_row->glyphs[area + 1])
20243 {
20244 glyph->charpos = CHARPOS (it->position);
20245 glyph->object = it->object;
20246 glyph->pixel_width = it->pixel_width;
20247 glyph->ascent = glyph_ascent;
20248 glyph->descent = it->descent;
20249 glyph->voffset = it->voffset;
20250 glyph->type = IMAGE_GLYPH;
20251 glyph->multibyte_p = it->multibyte_p;
20252 glyph->left_box_line_p = it->start_of_box_run_p;
20253 glyph->right_box_line_p = it->end_of_box_run_p;
20254 glyph->overlaps_vertically_p = 0;
20255 glyph->padding_p = 0;
20256 glyph->glyph_not_available_p = 0;
20257 glyph->face_id = it->face_id;
20258 glyph->u.img_id = img->id;
20259 glyph->slice = slice;
20260 glyph->font_type = FONT_TYPE_UNKNOWN;
20261 ++it->glyph_row->used[area];
20262 }
20263 else
20264 IT_EXPAND_MATRIX_WIDTH (it, area);
20265 }
20266 }
20267
20268
20269 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20270 of the glyph, WIDTH and HEIGHT are the width and height of the
20271 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20272
20273 static void
20274 append_stretch_glyph (it, object, width, height, ascent)
20275 struct it *it;
20276 Lisp_Object object;
20277 int width, height;
20278 int ascent;
20279 {
20280 struct glyph *glyph;
20281 enum glyph_row_area area = it->area;
20282
20283 xassert (ascent >= 0 && ascent <= height);
20284
20285 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20286 if (glyph < it->glyph_row->glyphs[area + 1])
20287 {
20288 glyph->charpos = CHARPOS (it->position);
20289 glyph->object = object;
20290 glyph->pixel_width = width;
20291 glyph->ascent = ascent;
20292 glyph->descent = height - ascent;
20293 glyph->voffset = it->voffset;
20294 glyph->type = STRETCH_GLYPH;
20295 glyph->multibyte_p = it->multibyte_p;
20296 glyph->left_box_line_p = it->start_of_box_run_p;
20297 glyph->right_box_line_p = it->end_of_box_run_p;
20298 glyph->overlaps_vertically_p = 0;
20299 glyph->padding_p = 0;
20300 glyph->glyph_not_available_p = 0;
20301 glyph->face_id = it->face_id;
20302 glyph->u.stretch.ascent = ascent;
20303 glyph->u.stretch.height = height;
20304 glyph->slice = null_glyph_slice;
20305 glyph->font_type = FONT_TYPE_UNKNOWN;
20306 ++it->glyph_row->used[area];
20307 }
20308 else
20309 IT_EXPAND_MATRIX_WIDTH (it, area);
20310 }
20311
20312
20313 /* Produce a stretch glyph for iterator IT. IT->object is the value
20314 of the glyph property displayed. The value must be a list
20315 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20316 being recognized:
20317
20318 1. `:width WIDTH' specifies that the space should be WIDTH *
20319 canonical char width wide. WIDTH may be an integer or floating
20320 point number.
20321
20322 2. `:relative-width FACTOR' specifies that the width of the stretch
20323 should be computed from the width of the first character having the
20324 `glyph' property, and should be FACTOR times that width.
20325
20326 3. `:align-to HPOS' specifies that the space should be wide enough
20327 to reach HPOS, a value in canonical character units.
20328
20329 Exactly one of the above pairs must be present.
20330
20331 4. `:height HEIGHT' specifies that the height of the stretch produced
20332 should be HEIGHT, measured in canonical character units.
20333
20334 5. `:relative-height FACTOR' specifies that the height of the
20335 stretch should be FACTOR times the height of the characters having
20336 the glyph property.
20337
20338 Either none or exactly one of 4 or 5 must be present.
20339
20340 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20341 of the stretch should be used for the ascent of the stretch.
20342 ASCENT must be in the range 0 <= ASCENT <= 100. */
20343
20344 static void
20345 produce_stretch_glyph (it)
20346 struct it *it;
20347 {
20348 /* (space :width WIDTH :height HEIGHT ...) */
20349 Lisp_Object prop, plist;
20350 int width = 0, height = 0, align_to = -1;
20351 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20352 int ascent = 0;
20353 double tem;
20354 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20355 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20356
20357 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20358
20359 /* List should start with `space'. */
20360 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20361 plist = XCDR (it->object);
20362
20363 /* Compute the width of the stretch. */
20364 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20365 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20366 {
20367 /* Absolute width `:width WIDTH' specified and valid. */
20368 zero_width_ok_p = 1;
20369 width = (int)tem;
20370 }
20371 else if (prop = Fplist_get (plist, QCrelative_width),
20372 NUMVAL (prop) > 0)
20373 {
20374 /* Relative width `:relative-width FACTOR' specified and valid.
20375 Compute the width of the characters having the `glyph'
20376 property. */
20377 struct it it2;
20378 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20379
20380 it2 = *it;
20381 if (it->multibyte_p)
20382 {
20383 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20384 - IT_BYTEPOS (*it));
20385 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20386 }
20387 else
20388 it2.c = *p, it2.len = 1;
20389
20390 it2.glyph_row = NULL;
20391 it2.what = IT_CHARACTER;
20392 x_produce_glyphs (&it2);
20393 width = NUMVAL (prop) * it2.pixel_width;
20394 }
20395 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20396 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20397 {
20398 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20399 align_to = (align_to < 0
20400 ? 0
20401 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20402 else if (align_to < 0)
20403 align_to = window_box_left_offset (it->w, TEXT_AREA);
20404 width = max (0, (int)tem + align_to - it->current_x);
20405 zero_width_ok_p = 1;
20406 }
20407 else
20408 /* Nothing specified -> width defaults to canonical char width. */
20409 width = FRAME_COLUMN_WIDTH (it->f);
20410
20411 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20412 width = 1;
20413
20414 /* Compute height. */
20415 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20416 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20417 {
20418 height = (int)tem;
20419 zero_height_ok_p = 1;
20420 }
20421 else if (prop = Fplist_get (plist, QCrelative_height),
20422 NUMVAL (prop) > 0)
20423 height = FONT_HEIGHT (font) * NUMVAL (prop);
20424 else
20425 height = FONT_HEIGHT (font);
20426
20427 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20428 height = 1;
20429
20430 /* Compute percentage of height used for ascent. If
20431 `:ascent ASCENT' is present and valid, use that. Otherwise,
20432 derive the ascent from the font in use. */
20433 if (prop = Fplist_get (plist, QCascent),
20434 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20435 ascent = height * NUMVAL (prop) / 100.0;
20436 else if (!NILP (prop)
20437 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20438 ascent = min (max (0, (int)tem), height);
20439 else
20440 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20441
20442 if (width > 0 && !it->truncate_lines_p
20443 && it->current_x + width > it->last_visible_x)
20444 width = it->last_visible_x - it->current_x - 1;
20445
20446 if (width > 0 && height > 0 && it->glyph_row)
20447 {
20448 Lisp_Object object = it->stack[it->sp - 1].string;
20449 if (!STRINGP (object))
20450 object = it->w->buffer;
20451 append_stretch_glyph (it, object, width, height, ascent);
20452 }
20453
20454 it->pixel_width = width;
20455 it->ascent = it->phys_ascent = ascent;
20456 it->descent = it->phys_descent = height - it->ascent;
20457 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20458
20459 take_vertical_position_into_account (it);
20460 }
20461
20462 /* Get line-height and line-spacing property at point.
20463 If line-height has format (HEIGHT TOTAL), return TOTAL
20464 in TOTAL_HEIGHT. */
20465
20466 static Lisp_Object
20467 get_line_height_property (it, prop)
20468 struct it *it;
20469 Lisp_Object prop;
20470 {
20471 Lisp_Object position;
20472
20473 if (STRINGP (it->object))
20474 position = make_number (IT_STRING_CHARPOS (*it));
20475 else if (BUFFERP (it->object))
20476 position = make_number (IT_CHARPOS (*it));
20477 else
20478 return Qnil;
20479
20480 return Fget_char_property (position, prop, it->object);
20481 }
20482
20483 /* Calculate line-height and line-spacing properties.
20484 An integer value specifies explicit pixel value.
20485 A float value specifies relative value to current face height.
20486 A cons (float . face-name) specifies relative value to
20487 height of specified face font.
20488
20489 Returns height in pixels, or nil. */
20490
20491
20492 static Lisp_Object
20493 calc_line_height_property (it, val, font, boff, override)
20494 struct it *it;
20495 Lisp_Object val;
20496 XFontStruct *font;
20497 int boff, override;
20498 {
20499 Lisp_Object face_name = Qnil;
20500 int ascent, descent, height;
20501
20502 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20503 return val;
20504
20505 if (CONSP (val))
20506 {
20507 face_name = XCAR (val);
20508 val = XCDR (val);
20509 if (!NUMBERP (val))
20510 val = make_number (1);
20511 if (NILP (face_name))
20512 {
20513 height = it->ascent + it->descent;
20514 goto scale;
20515 }
20516 }
20517
20518 if (NILP (face_name))
20519 {
20520 font = FRAME_FONT (it->f);
20521 boff = FRAME_BASELINE_OFFSET (it->f);
20522 }
20523 else if (EQ (face_name, Qt))
20524 {
20525 override = 0;
20526 }
20527 else
20528 {
20529 int face_id;
20530 struct face *face;
20531 struct font_info *font_info;
20532
20533 face_id = lookup_named_face (it->f, face_name, 0);
20534 if (face_id < 0)
20535 return make_number (-1);
20536
20537 face = FACE_FROM_ID (it->f, face_id);
20538 font = face->font;
20539 if (font == NULL)
20540 return make_number (-1);
20541
20542 font_info = FONT_INFO_FROM_FACE (it->f, face);
20543 boff = font_info->baseline_offset;
20544 if (font_info->vertical_centering)
20545 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20546 }
20547
20548 ascent = FONT_BASE (font) + boff;
20549 descent = FONT_DESCENT (font) - boff;
20550
20551 if (override)
20552 {
20553 it->override_ascent = ascent;
20554 it->override_descent = descent;
20555 it->override_boff = boff;
20556 }
20557
20558 height = ascent + descent;
20559
20560 scale:
20561 if (FLOATP (val))
20562 height = (int)(XFLOAT_DATA (val) * height);
20563 else if (INTEGERP (val))
20564 height *= XINT (val);
20565
20566 return make_number (height);
20567 }
20568
20569
20570 /* RIF:
20571 Produce glyphs/get display metrics for the display element IT is
20572 loaded with. See the description of struct it in dispextern.h
20573 for an overview of struct it. */
20574
20575 void
20576 x_produce_glyphs (it)
20577 struct it *it;
20578 {
20579 int extra_line_spacing = it->extra_line_spacing;
20580
20581 it->glyph_not_available_p = 0;
20582
20583 if (it->what == IT_CHARACTER)
20584 {
20585 XChar2b char2b;
20586 XFontStruct *font;
20587 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20588 XCharStruct *pcm;
20589 int font_not_found_p;
20590 struct font_info *font_info;
20591 int boff; /* baseline offset */
20592 /* We may change it->multibyte_p upon unibyte<->multibyte
20593 conversion. So, save the current value now and restore it
20594 later.
20595
20596 Note: It seems that we don't have to record multibyte_p in
20597 struct glyph because the character code itself tells if or
20598 not the character is multibyte. Thus, in the future, we must
20599 consider eliminating the field `multibyte_p' in the struct
20600 glyph. */
20601 int saved_multibyte_p = it->multibyte_p;
20602
20603 /* Maybe translate single-byte characters to multibyte, or the
20604 other way. */
20605 it->char_to_display = it->c;
20606 if (!ASCII_BYTE_P (it->c)
20607 && ! it->multibyte_p)
20608 {
20609 if (SINGLE_BYTE_CHAR_P (it->c)
20610 && unibyte_display_via_language_environment)
20611 it->char_to_display = unibyte_char_to_multibyte (it->c);
20612 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20613 {
20614 it->multibyte_p = 1;
20615 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20616 -1, Qnil);
20617 face = FACE_FROM_ID (it->f, it->face_id);
20618 }
20619 }
20620
20621 /* Get font to use. Encode IT->char_to_display. */
20622 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20623 &char2b, it->multibyte_p, 0);
20624 font = face->font;
20625
20626 /* When no suitable font found, use the default font. */
20627 font_not_found_p = font == NULL;
20628 if (font_not_found_p)
20629 {
20630 font = FRAME_FONT (it->f);
20631 boff = FRAME_BASELINE_OFFSET (it->f);
20632 font_info = NULL;
20633 }
20634 else
20635 {
20636 font_info = FONT_INFO_FROM_FACE (it->f, face);
20637 boff = font_info->baseline_offset;
20638 if (font_info->vertical_centering)
20639 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20640 }
20641
20642 if (it->char_to_display >= ' '
20643 && (!it->multibyte_p || it->char_to_display < 128))
20644 {
20645 /* Either unibyte or ASCII. */
20646 int stretched_p;
20647
20648 it->nglyphs = 1;
20649
20650 pcm = get_per_char_metric (font, font_info, &char2b,
20651 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20652
20653 if (it->override_ascent >= 0)
20654 {
20655 it->ascent = it->override_ascent;
20656 it->descent = it->override_descent;
20657 boff = it->override_boff;
20658 }
20659 else
20660 {
20661 it->ascent = FONT_BASE (font) + boff;
20662 it->descent = FONT_DESCENT (font) - boff;
20663 }
20664
20665 if (pcm)
20666 {
20667 it->phys_ascent = pcm->ascent + boff;
20668 it->phys_descent = pcm->descent - boff;
20669 it->pixel_width = pcm->width;
20670 }
20671 else
20672 {
20673 it->glyph_not_available_p = 1;
20674 it->phys_ascent = it->ascent;
20675 it->phys_descent = it->descent;
20676 it->pixel_width = FONT_WIDTH (font);
20677 }
20678
20679 if (it->constrain_row_ascent_descent_p)
20680 {
20681 if (it->descent > it->max_descent)
20682 {
20683 it->ascent += it->descent - it->max_descent;
20684 it->descent = it->max_descent;
20685 }
20686 if (it->ascent > it->max_ascent)
20687 {
20688 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20689 it->ascent = it->max_ascent;
20690 }
20691 it->phys_ascent = min (it->phys_ascent, it->ascent);
20692 it->phys_descent = min (it->phys_descent, it->descent);
20693 extra_line_spacing = 0;
20694 }
20695
20696 /* If this is a space inside a region of text with
20697 `space-width' property, change its width. */
20698 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20699 if (stretched_p)
20700 it->pixel_width *= XFLOATINT (it->space_width);
20701
20702 /* If face has a box, add the box thickness to the character
20703 height. If character has a box line to the left and/or
20704 right, add the box line width to the character's width. */
20705 if (face->box != FACE_NO_BOX)
20706 {
20707 int thick = face->box_line_width;
20708
20709 if (thick > 0)
20710 {
20711 it->ascent += thick;
20712 it->descent += thick;
20713 }
20714 else
20715 thick = -thick;
20716
20717 if (it->start_of_box_run_p)
20718 it->pixel_width += thick;
20719 if (it->end_of_box_run_p)
20720 it->pixel_width += thick;
20721 }
20722
20723 /* If face has an overline, add the height of the overline
20724 (1 pixel) and a 1 pixel margin to the character height. */
20725 if (face->overline_p)
20726 it->ascent += overline_margin;
20727
20728 if (it->constrain_row_ascent_descent_p)
20729 {
20730 if (it->ascent > it->max_ascent)
20731 it->ascent = it->max_ascent;
20732 if (it->descent > it->max_descent)
20733 it->descent = it->max_descent;
20734 }
20735
20736 take_vertical_position_into_account (it);
20737
20738 /* If we have to actually produce glyphs, do it. */
20739 if (it->glyph_row)
20740 {
20741 if (stretched_p)
20742 {
20743 /* Translate a space with a `space-width' property
20744 into a stretch glyph. */
20745 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20746 / FONT_HEIGHT (font));
20747 append_stretch_glyph (it, it->object, it->pixel_width,
20748 it->ascent + it->descent, ascent);
20749 }
20750 else
20751 append_glyph (it);
20752
20753 /* If characters with lbearing or rbearing are displayed
20754 in this line, record that fact in a flag of the
20755 glyph row. This is used to optimize X output code. */
20756 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20757 it->glyph_row->contains_overlapping_glyphs_p = 1;
20758 }
20759 }
20760 else if (it->char_to_display == '\n')
20761 {
20762 /* A newline has no width but we need the height of the line.
20763 But if previous part of the line set a height, don't
20764 increase that height */
20765
20766 Lisp_Object height;
20767 Lisp_Object total_height = Qnil;
20768
20769 it->override_ascent = -1;
20770 it->pixel_width = 0;
20771 it->nglyphs = 0;
20772
20773 height = get_line_height_property(it, Qline_height);
20774 /* Split (line-height total-height) list */
20775 if (CONSP (height)
20776 && CONSP (XCDR (height))
20777 && NILP (XCDR (XCDR (height))))
20778 {
20779 total_height = XCAR (XCDR (height));
20780 height = XCAR (height);
20781 }
20782 height = calc_line_height_property(it, height, font, boff, 1);
20783
20784 if (it->override_ascent >= 0)
20785 {
20786 it->ascent = it->override_ascent;
20787 it->descent = it->override_descent;
20788 boff = it->override_boff;
20789 }
20790 else
20791 {
20792 it->ascent = FONT_BASE (font) + boff;
20793 it->descent = FONT_DESCENT (font) - boff;
20794 }
20795
20796 if (EQ (height, Qt))
20797 {
20798 if (it->descent > it->max_descent)
20799 {
20800 it->ascent += it->descent - it->max_descent;
20801 it->descent = it->max_descent;
20802 }
20803 if (it->ascent > it->max_ascent)
20804 {
20805 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20806 it->ascent = it->max_ascent;
20807 }
20808 it->phys_ascent = min (it->phys_ascent, it->ascent);
20809 it->phys_descent = min (it->phys_descent, it->descent);
20810 it->constrain_row_ascent_descent_p = 1;
20811 extra_line_spacing = 0;
20812 }
20813 else
20814 {
20815 Lisp_Object spacing;
20816
20817 it->phys_ascent = it->ascent;
20818 it->phys_descent = it->descent;
20819
20820 if ((it->max_ascent > 0 || it->max_descent > 0)
20821 && face->box != FACE_NO_BOX
20822 && face->box_line_width > 0)
20823 {
20824 it->ascent += face->box_line_width;
20825 it->descent += face->box_line_width;
20826 }
20827 if (!NILP (height)
20828 && XINT (height) > it->ascent + it->descent)
20829 it->ascent = XINT (height) - it->descent;
20830
20831 if (!NILP (total_height))
20832 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20833 else
20834 {
20835 spacing = get_line_height_property(it, Qline_spacing);
20836 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20837 }
20838 if (INTEGERP (spacing))
20839 {
20840 extra_line_spacing = XINT (spacing);
20841 if (!NILP (total_height))
20842 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20843 }
20844 }
20845 }
20846 else if (it->char_to_display == '\t')
20847 {
20848 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20849 int x = it->current_x + it->continuation_lines_width;
20850 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20851
20852 /* If the distance from the current position to the next tab
20853 stop is less than a space character width, use the
20854 tab stop after that. */
20855 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20856 next_tab_x += tab_width;
20857
20858 it->pixel_width = next_tab_x - x;
20859 it->nglyphs = 1;
20860 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20861 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20862
20863 if (it->glyph_row)
20864 {
20865 append_stretch_glyph (it, it->object, it->pixel_width,
20866 it->ascent + it->descent, it->ascent);
20867 }
20868 }
20869 else
20870 {
20871 /* A multi-byte character. Assume that the display width of the
20872 character is the width of the character multiplied by the
20873 width of the font. */
20874
20875 /* If we found a font, this font should give us the right
20876 metrics. If we didn't find a font, use the frame's
20877 default font and calculate the width of the character by
20878 multiplying the width of font by the width of the
20879 character. */
20880
20881 pcm = get_per_char_metric (font, font_info, &char2b,
20882 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20883
20884 if (font_not_found_p || !pcm)
20885 {
20886 int char_width = CHAR_WIDTH (it->char_to_display);
20887
20888 if (char_width == 0)
20889 /* This is a non spacing character. But, as we are
20890 going to display an empty box, the box must occupy
20891 at least one column. */
20892 char_width = 1;
20893 it->glyph_not_available_p = 1;
20894 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
20895 it->phys_ascent = FONT_BASE (font) + boff;
20896 it->phys_descent = FONT_DESCENT (font) - boff;
20897 }
20898 else
20899 {
20900 it->pixel_width = pcm->width;
20901 it->phys_ascent = pcm->ascent + boff;
20902 it->phys_descent = pcm->descent - boff;
20903 if (it->glyph_row
20904 && (pcm->lbearing < 0
20905 || pcm->rbearing > pcm->width))
20906 it->glyph_row->contains_overlapping_glyphs_p = 1;
20907 }
20908 it->nglyphs = 1;
20909 it->ascent = FONT_BASE (font) + boff;
20910 it->descent = FONT_DESCENT (font) - boff;
20911 if (face->box != FACE_NO_BOX)
20912 {
20913 int thick = face->box_line_width;
20914
20915 if (thick > 0)
20916 {
20917 it->ascent += thick;
20918 it->descent += thick;
20919 }
20920 else
20921 thick = - thick;
20922
20923 if (it->start_of_box_run_p)
20924 it->pixel_width += thick;
20925 if (it->end_of_box_run_p)
20926 it->pixel_width += thick;
20927 }
20928
20929 /* If face has an overline, add the height of the overline
20930 (1 pixel) and a 1 pixel margin to the character height. */
20931 if (face->overline_p)
20932 it->ascent += overline_margin;
20933
20934 take_vertical_position_into_account (it);
20935
20936 if (it->glyph_row)
20937 append_glyph (it);
20938 }
20939 it->multibyte_p = saved_multibyte_p;
20940 }
20941 else if (it->what == IT_COMPOSITION)
20942 {
20943 /* Note: A composition is represented as one glyph in the
20944 glyph matrix. There are no padding glyphs.
20945
20946 Important is that pixel_width, ascent, and descent are the
20947 values of what is drawn by draw_glyphs (i.e. the values of
20948 the overall glyphs composed). */
20949 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20950 int boff; /* baseline offset */
20951 struct composition *cmp = composition_table[it->cmp_id];
20952 int glyph_len = cmp->glyph_len;
20953 XFontStruct *font = face->font;
20954
20955 it->nglyphs = 1;
20956
20957 #ifdef USE_FONT_BACKEND
20958 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
20959 {
20960 if (! cmp->font || cmp->font != font)
20961 font_prepare_composition (cmp);
20962 }
20963 else
20964 #endif /* USE_FONT_BACKEND */
20965 /* If we have not yet calculated pixel size data of glyphs of
20966 the composition for the current face font, calculate them
20967 now. Theoretically, we have to check all fonts for the
20968 glyphs, but that requires much time and memory space. So,
20969 here we check only the font of the first glyph. This leads
20970 to incorrect display, but it's very rare, and C-l (recenter)
20971 can correct the display anyway. */
20972 if (! cmp->font || cmp->font != font)
20973 {
20974 /* Ascent and descent of the font of the first character
20975 of this composition (adjusted by baseline offset).
20976 Ascent and descent of overall glyphs should not be less
20977 than them respectively. */
20978 int font_ascent, font_descent, font_height;
20979 /* Bounding box of the overall glyphs. */
20980 int leftmost, rightmost, lowest, highest;
20981 int lbearing, rbearing;
20982 int i, width, ascent, descent;
20983 int left_padded = 0, right_padded = 0;
20984 int face_id;
20985 int c;
20986 XChar2b char2b;
20987 XCharStruct *pcm;
20988 int font_not_found_p;
20989 struct font_info *font_info;
20990 int pos;
20991
20992 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
20993 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
20994 break;
20995 if (glyph_len < cmp->glyph_len)
20996 right_padded = 1;
20997 for (i = 0; i < glyph_len; i++)
20998 {
20999 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21000 break;
21001 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21002 }
21003 if (i > 0)
21004 left_padded = 1;
21005
21006 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21007 : IT_CHARPOS (*it));
21008 /* When no suitable font found, use the default font. */
21009 font_not_found_p = font == NULL;
21010 if (font_not_found_p)
21011 {
21012 face = face->ascii_face;
21013 font = face->font;
21014 }
21015 font_info = FONT_INFO_FROM_FACE (it->f, face);
21016 boff = font_info->baseline_offset;
21017 if (font_info->vertical_centering)
21018 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21019 font_ascent = FONT_BASE (font) + boff;
21020 font_descent = FONT_DESCENT (font) - boff;
21021 font_height = FONT_HEIGHT (font);
21022
21023 cmp->font = (void *) font;
21024
21025 pcm = NULL;
21026 if (! font_not_found_p)
21027 {
21028 get_char_face_and_encoding (it->f, c, it->face_id,
21029 &char2b, it->multibyte_p, 0);
21030 pcm = get_per_char_metric (font, font_info, &char2b,
21031 FONT_TYPE_FOR_MULTIBYTE (font, c));
21032 }
21033
21034 /* Initialize the bounding box. */
21035 if (pcm)
21036 {
21037 width = pcm->width;
21038 ascent = pcm->ascent;
21039 descent = pcm->descent;
21040 lbearing = pcm->lbearing;
21041 rbearing = pcm->rbearing;
21042 }
21043 else
21044 {
21045 width = FONT_WIDTH (font);
21046 ascent = FONT_BASE (font);
21047 descent = FONT_DESCENT (font);
21048 lbearing = 0;
21049 rbearing = width;
21050 }
21051
21052 rightmost = width;
21053 leftmost = 0;
21054 lowest = - descent + boff;
21055 highest = ascent + boff;
21056
21057 if (! font_not_found_p
21058 && font_info->default_ascent
21059 && CHAR_TABLE_P (Vuse_default_ascent)
21060 && !NILP (Faref (Vuse_default_ascent,
21061 make_number (it->char_to_display))))
21062 highest = font_info->default_ascent + boff;
21063
21064 /* Draw the first glyph at the normal position. It may be
21065 shifted to right later if some other glyphs are drawn
21066 at the left. */
21067 cmp->offsets[i * 2] = 0;
21068 cmp->offsets[i * 2 + 1] = boff;
21069 cmp->lbearing = lbearing;
21070 cmp->rbearing = rbearing;
21071
21072 /* Set cmp->offsets for the remaining glyphs. */
21073 for (i++; i < glyph_len; i++)
21074 {
21075 int left, right, btm, top;
21076 int ch = COMPOSITION_GLYPH (cmp, i);
21077 int face_id;
21078 struct face *this_face;
21079 int this_boff;
21080
21081 if (ch == '\t')
21082 ch = ' ';
21083 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21084 this_face = FACE_FROM_ID (it->f, face_id);
21085 font = this_face->font;
21086
21087 if (font == NULL)
21088 pcm = NULL;
21089 else
21090 {
21091 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21092 this_boff = font_info->baseline_offset;
21093 if (font_info->vertical_centering)
21094 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21095 get_char_face_and_encoding (it->f, ch, face_id,
21096 &char2b, it->multibyte_p, 0);
21097 pcm = get_per_char_metric (font, font_info, &char2b,
21098 FONT_TYPE_FOR_MULTIBYTE (font,
21099 ch));
21100 }
21101 if (! pcm)
21102 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21103 else
21104 {
21105 width = pcm->width;
21106 ascent = pcm->ascent;
21107 descent = pcm->descent;
21108 lbearing = pcm->lbearing;
21109 rbearing = pcm->rbearing;
21110 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21111 {
21112 /* Relative composition with or without
21113 alternate chars. */
21114 left = (leftmost + rightmost - width) / 2;
21115 btm = - descent + boff;
21116 if (font_info->relative_compose
21117 && (! CHAR_TABLE_P (Vignore_relative_composition)
21118 || NILP (Faref (Vignore_relative_composition,
21119 make_number (ch)))))
21120 {
21121
21122 if (- descent >= font_info->relative_compose)
21123 /* One extra pixel between two glyphs. */
21124 btm = highest + 1;
21125 else if (ascent <= 0)
21126 /* One extra pixel between two glyphs. */
21127 btm = lowest - 1 - ascent - descent;
21128 }
21129 }
21130 else
21131 {
21132 /* A composition rule is specified by an integer
21133 value that encodes global and new reference
21134 points (GREF and NREF). GREF and NREF are
21135 specified by numbers as below:
21136
21137 0---1---2 -- ascent
21138 | |
21139 | |
21140 | |
21141 9--10--11 -- center
21142 | |
21143 ---3---4---5--- baseline
21144 | |
21145 6---7---8 -- descent
21146 */
21147 int rule = COMPOSITION_RULE (cmp, i);
21148 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21149
21150 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21151 grefx = gref % 3, nrefx = nref % 3;
21152 grefy = gref / 3, nrefy = nref / 3;
21153 if (xoff)
21154 xoff = font_height * (xoff - 128) / 256;
21155 if (yoff)
21156 yoff = font_height * (yoff - 128) / 256;
21157
21158 left = (leftmost
21159 + grefx * (rightmost - leftmost) / 2
21160 - nrefx * width / 2
21161 + xoff);
21162
21163 btm = ((grefy == 0 ? highest
21164 : grefy == 1 ? 0
21165 : grefy == 2 ? lowest
21166 : (highest + lowest) / 2)
21167 - (nrefy == 0 ? ascent + descent
21168 : nrefy == 1 ? descent - boff
21169 : nrefy == 2 ? 0
21170 : (ascent + descent) / 2)
21171 + yoff);
21172 }
21173
21174 cmp->offsets[i * 2] = left;
21175 cmp->offsets[i * 2 + 1] = btm + descent;
21176
21177 /* Update the bounding box of the overall glyphs. */
21178 if (width > 0)
21179 {
21180 right = left + width;
21181 if (left < leftmost)
21182 leftmost = left;
21183 if (right > rightmost)
21184 rightmost = right;
21185 }
21186 top = btm + descent + ascent;
21187 if (top > highest)
21188 highest = top;
21189 if (btm < lowest)
21190 lowest = btm;
21191
21192 if (cmp->lbearing > left + lbearing)
21193 cmp->lbearing = left + lbearing;
21194 if (cmp->rbearing < left + rbearing)
21195 cmp->rbearing = left + rbearing;
21196 }
21197 }
21198
21199 /* If there are glyphs whose x-offsets are negative,
21200 shift all glyphs to the right and make all x-offsets
21201 non-negative. */
21202 if (leftmost < 0)
21203 {
21204 for (i = 0; i < cmp->glyph_len; i++)
21205 cmp->offsets[i * 2] -= leftmost;
21206 rightmost -= leftmost;
21207 cmp->lbearing -= leftmost;
21208 cmp->rbearing -= leftmost;
21209 }
21210
21211 if (left_padded && cmp->lbearing < 0)
21212 {
21213 for (i = 0; i < cmp->glyph_len; i++)
21214 cmp->offsets[i * 2] -= cmp->lbearing;
21215 rightmost -= cmp->lbearing;
21216 cmp->rbearing -= cmp->lbearing;
21217 cmp->lbearing = 0;
21218 }
21219 if (right_padded && rightmost < cmp->rbearing)
21220 {
21221 rightmost = cmp->rbearing;
21222 }
21223
21224 cmp->pixel_width = rightmost;
21225 cmp->ascent = highest;
21226 cmp->descent = - lowest;
21227 if (cmp->ascent < font_ascent)
21228 cmp->ascent = font_ascent;
21229 if (cmp->descent < font_descent)
21230 cmp->descent = font_descent;
21231 }
21232
21233 if (it->glyph_row
21234 && (cmp->lbearing < 0
21235 || cmp->rbearing > cmp->pixel_width))
21236 it->glyph_row->contains_overlapping_glyphs_p = 1;
21237
21238 it->pixel_width = cmp->pixel_width;
21239 it->ascent = it->phys_ascent = cmp->ascent;
21240 it->descent = it->phys_descent = cmp->descent;
21241
21242 if (face->box != FACE_NO_BOX)
21243 {
21244 int thick = face->box_line_width;
21245
21246 if (thick > 0)
21247 {
21248 it->ascent += thick;
21249 it->descent += thick;
21250 }
21251 else
21252 thick = - thick;
21253
21254 if (it->start_of_box_run_p)
21255 it->pixel_width += thick;
21256 if (it->end_of_box_run_p)
21257 it->pixel_width += thick;
21258 }
21259
21260 /* If face has an overline, add the height of the overline
21261 (1 pixel) and a 1 pixel margin to the character height. */
21262 if (face->overline_p)
21263 it->ascent += overline_margin;
21264
21265 take_vertical_position_into_account (it);
21266
21267 if (it->glyph_row)
21268 append_composite_glyph (it);
21269 }
21270 else if (it->what == IT_IMAGE)
21271 produce_image_glyph (it);
21272 else if (it->what == IT_STRETCH)
21273 produce_stretch_glyph (it);
21274
21275 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21276 because this isn't true for images with `:ascent 100'. */
21277 xassert (it->ascent >= 0 && it->descent >= 0);
21278 if (it->area == TEXT_AREA)
21279 it->current_x += it->pixel_width;
21280
21281 if (extra_line_spacing > 0)
21282 {
21283 it->descent += extra_line_spacing;
21284 if (extra_line_spacing > it->max_extra_line_spacing)
21285 it->max_extra_line_spacing = extra_line_spacing;
21286 }
21287
21288 it->max_ascent = max (it->max_ascent, it->ascent);
21289 it->max_descent = max (it->max_descent, it->descent);
21290 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21291 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21292 }
21293
21294 /* EXPORT for RIF:
21295 Output LEN glyphs starting at START at the nominal cursor position.
21296 Advance the nominal cursor over the text. The global variable
21297 updated_window contains the window being updated, updated_row is
21298 the glyph row being updated, and updated_area is the area of that
21299 row being updated. */
21300
21301 void
21302 x_write_glyphs (start, len)
21303 struct glyph *start;
21304 int len;
21305 {
21306 int x, hpos;
21307
21308 xassert (updated_window && updated_row);
21309 BLOCK_INPUT;
21310
21311 /* Write glyphs. */
21312
21313 hpos = start - updated_row->glyphs[updated_area];
21314 x = draw_glyphs (updated_window, output_cursor.x,
21315 updated_row, updated_area,
21316 hpos, hpos + len,
21317 DRAW_NORMAL_TEXT, 0);
21318
21319 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21320 if (updated_area == TEXT_AREA
21321 && updated_window->phys_cursor_on_p
21322 && updated_window->phys_cursor.vpos == output_cursor.vpos
21323 && updated_window->phys_cursor.hpos >= hpos
21324 && updated_window->phys_cursor.hpos < hpos + len)
21325 updated_window->phys_cursor_on_p = 0;
21326
21327 UNBLOCK_INPUT;
21328
21329 /* Advance the output cursor. */
21330 output_cursor.hpos += len;
21331 output_cursor.x = x;
21332 }
21333
21334
21335 /* EXPORT for RIF:
21336 Insert LEN glyphs from START at the nominal cursor position. */
21337
21338 void
21339 x_insert_glyphs (start, len)
21340 struct glyph *start;
21341 int len;
21342 {
21343 struct frame *f;
21344 struct window *w;
21345 int line_height, shift_by_width, shifted_region_width;
21346 struct glyph_row *row;
21347 struct glyph *glyph;
21348 int frame_x, frame_y;
21349 EMACS_INT hpos;
21350
21351 xassert (updated_window && updated_row);
21352 BLOCK_INPUT;
21353 w = updated_window;
21354 f = XFRAME (WINDOW_FRAME (w));
21355
21356 /* Get the height of the line we are in. */
21357 row = updated_row;
21358 line_height = row->height;
21359
21360 /* Get the width of the glyphs to insert. */
21361 shift_by_width = 0;
21362 for (glyph = start; glyph < start + len; ++glyph)
21363 shift_by_width += glyph->pixel_width;
21364
21365 /* Get the width of the region to shift right. */
21366 shifted_region_width = (window_box_width (w, updated_area)
21367 - output_cursor.x
21368 - shift_by_width);
21369
21370 /* Shift right. */
21371 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21372 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21373
21374 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21375 line_height, shift_by_width);
21376
21377 /* Write the glyphs. */
21378 hpos = start - row->glyphs[updated_area];
21379 draw_glyphs (w, output_cursor.x, row, updated_area,
21380 hpos, hpos + len,
21381 DRAW_NORMAL_TEXT, 0);
21382
21383 /* Advance the output cursor. */
21384 output_cursor.hpos += len;
21385 output_cursor.x += shift_by_width;
21386 UNBLOCK_INPUT;
21387 }
21388
21389
21390 /* EXPORT for RIF:
21391 Erase the current text line from the nominal cursor position
21392 (inclusive) to pixel column TO_X (exclusive). The idea is that
21393 everything from TO_X onward is already erased.
21394
21395 TO_X is a pixel position relative to updated_area of
21396 updated_window. TO_X == -1 means clear to the end of this area. */
21397
21398 void
21399 x_clear_end_of_line (to_x)
21400 int to_x;
21401 {
21402 struct frame *f;
21403 struct window *w = updated_window;
21404 int max_x, min_y, max_y;
21405 int from_x, from_y, to_y;
21406
21407 xassert (updated_window && updated_row);
21408 f = XFRAME (w->frame);
21409
21410 if (updated_row->full_width_p)
21411 max_x = WINDOW_TOTAL_WIDTH (w);
21412 else
21413 max_x = window_box_width (w, updated_area);
21414 max_y = window_text_bottom_y (w);
21415
21416 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21417 of window. For TO_X > 0, truncate to end of drawing area. */
21418 if (to_x == 0)
21419 return;
21420 else if (to_x < 0)
21421 to_x = max_x;
21422 else
21423 to_x = min (to_x, max_x);
21424
21425 to_y = min (max_y, output_cursor.y + updated_row->height);
21426
21427 /* Notice if the cursor will be cleared by this operation. */
21428 if (!updated_row->full_width_p)
21429 notice_overwritten_cursor (w, updated_area,
21430 output_cursor.x, -1,
21431 updated_row->y,
21432 MATRIX_ROW_BOTTOM_Y (updated_row));
21433
21434 from_x = output_cursor.x;
21435
21436 /* Translate to frame coordinates. */
21437 if (updated_row->full_width_p)
21438 {
21439 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21440 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21441 }
21442 else
21443 {
21444 int area_left = window_box_left (w, updated_area);
21445 from_x += area_left;
21446 to_x += area_left;
21447 }
21448
21449 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21450 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21451 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21452
21453 /* Prevent inadvertently clearing to end of the X window. */
21454 if (to_x > from_x && to_y > from_y)
21455 {
21456 BLOCK_INPUT;
21457 rif->clear_frame_area (f, from_x, from_y,
21458 to_x - from_x, to_y - from_y);
21459 UNBLOCK_INPUT;
21460 }
21461 }
21462
21463 #endif /* HAVE_WINDOW_SYSTEM */
21464
21465
21466 \f
21467 /***********************************************************************
21468 Cursor types
21469 ***********************************************************************/
21470
21471 /* Value is the internal representation of the specified cursor type
21472 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21473 of the bar cursor. */
21474
21475 static enum text_cursor_kinds
21476 get_specified_cursor_type (arg, width)
21477 Lisp_Object arg;
21478 int *width;
21479 {
21480 enum text_cursor_kinds type;
21481
21482 if (NILP (arg))
21483 return NO_CURSOR;
21484
21485 if (EQ (arg, Qbox))
21486 return FILLED_BOX_CURSOR;
21487
21488 if (EQ (arg, Qhollow))
21489 return HOLLOW_BOX_CURSOR;
21490
21491 if (EQ (arg, Qbar))
21492 {
21493 *width = 2;
21494 return BAR_CURSOR;
21495 }
21496
21497 if (CONSP (arg)
21498 && EQ (XCAR (arg), Qbar)
21499 && INTEGERP (XCDR (arg))
21500 && XINT (XCDR (arg)) >= 0)
21501 {
21502 *width = XINT (XCDR (arg));
21503 return BAR_CURSOR;
21504 }
21505
21506 if (EQ (arg, Qhbar))
21507 {
21508 *width = 2;
21509 return HBAR_CURSOR;
21510 }
21511
21512 if (CONSP (arg)
21513 && EQ (XCAR (arg), Qhbar)
21514 && INTEGERP (XCDR (arg))
21515 && XINT (XCDR (arg)) >= 0)
21516 {
21517 *width = XINT (XCDR (arg));
21518 return HBAR_CURSOR;
21519 }
21520
21521 /* Treat anything unknown as "hollow box cursor".
21522 It was bad to signal an error; people have trouble fixing
21523 .Xdefaults with Emacs, when it has something bad in it. */
21524 type = HOLLOW_BOX_CURSOR;
21525
21526 return type;
21527 }
21528
21529 /* Set the default cursor types for specified frame. */
21530 void
21531 set_frame_cursor_types (f, arg)
21532 struct frame *f;
21533 Lisp_Object arg;
21534 {
21535 int width;
21536 Lisp_Object tem;
21537
21538 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21539 FRAME_CURSOR_WIDTH (f) = width;
21540
21541 /* By default, set up the blink-off state depending on the on-state. */
21542
21543 tem = Fassoc (arg, Vblink_cursor_alist);
21544 if (!NILP (tem))
21545 {
21546 FRAME_BLINK_OFF_CURSOR (f)
21547 = get_specified_cursor_type (XCDR (tem), &width);
21548 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21549 }
21550 else
21551 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21552 }
21553
21554
21555 /* Return the cursor we want to be displayed in window W. Return
21556 width of bar/hbar cursor through WIDTH arg. Return with
21557 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21558 (i.e. if the `system caret' should track this cursor).
21559
21560 In a mini-buffer window, we want the cursor only to appear if we
21561 are reading input from this window. For the selected window, we
21562 want the cursor type given by the frame parameter or buffer local
21563 setting of cursor-type. If explicitly marked off, draw no cursor.
21564 In all other cases, we want a hollow box cursor. */
21565
21566 static enum text_cursor_kinds
21567 get_window_cursor_type (w, glyph, width, active_cursor)
21568 struct window *w;
21569 struct glyph *glyph;
21570 int *width;
21571 int *active_cursor;
21572 {
21573 struct frame *f = XFRAME (w->frame);
21574 struct buffer *b = XBUFFER (w->buffer);
21575 int cursor_type = DEFAULT_CURSOR;
21576 Lisp_Object alt_cursor;
21577 int non_selected = 0;
21578
21579 *active_cursor = 1;
21580
21581 /* Echo area */
21582 if (cursor_in_echo_area
21583 && FRAME_HAS_MINIBUF_P (f)
21584 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21585 {
21586 if (w == XWINDOW (echo_area_window))
21587 {
21588 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21589 {
21590 *width = FRAME_CURSOR_WIDTH (f);
21591 return FRAME_DESIRED_CURSOR (f);
21592 }
21593 else
21594 return get_specified_cursor_type (b->cursor_type, width);
21595 }
21596
21597 *active_cursor = 0;
21598 non_selected = 1;
21599 }
21600
21601 /* Nonselected window or nonselected frame. */
21602 else if (w != XWINDOW (f->selected_window)
21603 #ifdef HAVE_WINDOW_SYSTEM
21604 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21605 #endif
21606 )
21607 {
21608 *active_cursor = 0;
21609
21610 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21611 return NO_CURSOR;
21612
21613 non_selected = 1;
21614 }
21615
21616 /* Never display a cursor in a window in which cursor-type is nil. */
21617 if (NILP (b->cursor_type))
21618 return NO_CURSOR;
21619
21620 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21621 if (non_selected)
21622 {
21623 alt_cursor = b->cursor_in_non_selected_windows;
21624 return get_specified_cursor_type (alt_cursor, width);
21625 }
21626
21627 /* Get the normal cursor type for this window. */
21628 if (EQ (b->cursor_type, Qt))
21629 {
21630 cursor_type = FRAME_DESIRED_CURSOR (f);
21631 *width = FRAME_CURSOR_WIDTH (f);
21632 }
21633 else
21634 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21635
21636 /* Use normal cursor if not blinked off. */
21637 if (!w->cursor_off_p)
21638 {
21639 #ifdef HAVE_WINDOW_SYSTEM
21640 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21641 {
21642 if (cursor_type == FILLED_BOX_CURSOR)
21643 {
21644 /* Using a block cursor on large images can be very annoying.
21645 So use a hollow cursor for "large" images.
21646 If image is not transparent (no mask), also use hollow cursor. */
21647 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21648 if (img != NULL && IMAGEP (img->spec))
21649 {
21650 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21651 where N = size of default frame font size.
21652 This should cover most of the "tiny" icons people may use. */
21653 if (!img->mask
21654 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21655 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21656 cursor_type = HOLLOW_BOX_CURSOR;
21657 }
21658 }
21659 else if (cursor_type != NO_CURSOR)
21660 {
21661 /* Display current only supports BOX and HOLLOW cursors for images.
21662 So for now, unconditionally use a HOLLOW cursor when cursor is
21663 not a solid box cursor. */
21664 cursor_type = HOLLOW_BOX_CURSOR;
21665 }
21666 }
21667 #endif
21668 return cursor_type;
21669 }
21670
21671 /* Cursor is blinked off, so determine how to "toggle" it. */
21672
21673 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21674 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21675 return get_specified_cursor_type (XCDR (alt_cursor), width);
21676
21677 /* Then see if frame has specified a specific blink off cursor type. */
21678 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21679 {
21680 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21681 return FRAME_BLINK_OFF_CURSOR (f);
21682 }
21683
21684 #if 0
21685 /* Some people liked having a permanently visible blinking cursor,
21686 while others had very strong opinions against it. So it was
21687 decided to remove it. KFS 2003-09-03 */
21688
21689 /* Finally perform built-in cursor blinking:
21690 filled box <-> hollow box
21691 wide [h]bar <-> narrow [h]bar
21692 narrow [h]bar <-> no cursor
21693 other type <-> no cursor */
21694
21695 if (cursor_type == FILLED_BOX_CURSOR)
21696 return HOLLOW_BOX_CURSOR;
21697
21698 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21699 {
21700 *width = 1;
21701 return cursor_type;
21702 }
21703 #endif
21704
21705 return NO_CURSOR;
21706 }
21707
21708
21709 #ifdef HAVE_WINDOW_SYSTEM
21710
21711 /* Notice when the text cursor of window W has been completely
21712 overwritten by a drawing operation that outputs glyphs in AREA
21713 starting at X0 and ending at X1 in the line starting at Y0 and
21714 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21715 the rest of the line after X0 has been written. Y coordinates
21716 are window-relative. */
21717
21718 static void
21719 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21720 struct window *w;
21721 enum glyph_row_area area;
21722 int x0, y0, x1, y1;
21723 {
21724 int cx0, cx1, cy0, cy1;
21725 struct glyph_row *row;
21726
21727 if (!w->phys_cursor_on_p)
21728 return;
21729 if (area != TEXT_AREA)
21730 return;
21731
21732 if (w->phys_cursor.vpos < 0
21733 || w->phys_cursor.vpos >= w->current_matrix->nrows
21734 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21735 !(row->enabled_p && row->displays_text_p)))
21736 return;
21737
21738 if (row->cursor_in_fringe_p)
21739 {
21740 row->cursor_in_fringe_p = 0;
21741 draw_fringe_bitmap (w, row, 0);
21742 w->phys_cursor_on_p = 0;
21743 return;
21744 }
21745
21746 cx0 = w->phys_cursor.x;
21747 cx1 = cx0 + w->phys_cursor_width;
21748 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21749 return;
21750
21751 /* The cursor image will be completely removed from the
21752 screen if the output area intersects the cursor area in
21753 y-direction. When we draw in [y0 y1[, and some part of
21754 the cursor is at y < y0, that part must have been drawn
21755 before. When scrolling, the cursor is erased before
21756 actually scrolling, so we don't come here. When not
21757 scrolling, the rows above the old cursor row must have
21758 changed, and in this case these rows must have written
21759 over the cursor image.
21760
21761 Likewise if part of the cursor is below y1, with the
21762 exception of the cursor being in the first blank row at
21763 the buffer and window end because update_text_area
21764 doesn't draw that row. (Except when it does, but
21765 that's handled in update_text_area.) */
21766
21767 cy0 = w->phys_cursor.y;
21768 cy1 = cy0 + w->phys_cursor_height;
21769 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21770 return;
21771
21772 w->phys_cursor_on_p = 0;
21773 }
21774
21775 #endif /* HAVE_WINDOW_SYSTEM */
21776
21777 \f
21778 /************************************************************************
21779 Mouse Face
21780 ************************************************************************/
21781
21782 #ifdef HAVE_WINDOW_SYSTEM
21783
21784 /* EXPORT for RIF:
21785 Fix the display of area AREA of overlapping row ROW in window W
21786 with respect to the overlapping part OVERLAPS. */
21787
21788 void
21789 x_fix_overlapping_area (w, row, area, overlaps)
21790 struct window *w;
21791 struct glyph_row *row;
21792 enum glyph_row_area area;
21793 int overlaps;
21794 {
21795 int i, x;
21796
21797 BLOCK_INPUT;
21798
21799 x = 0;
21800 for (i = 0; i < row->used[area];)
21801 {
21802 if (row->glyphs[area][i].overlaps_vertically_p)
21803 {
21804 int start = i, start_x = x;
21805
21806 do
21807 {
21808 x += row->glyphs[area][i].pixel_width;
21809 ++i;
21810 }
21811 while (i < row->used[area]
21812 && row->glyphs[area][i].overlaps_vertically_p);
21813
21814 draw_glyphs (w, start_x, row, area,
21815 start, i,
21816 DRAW_NORMAL_TEXT, overlaps);
21817 }
21818 else
21819 {
21820 x += row->glyphs[area][i].pixel_width;
21821 ++i;
21822 }
21823 }
21824
21825 UNBLOCK_INPUT;
21826 }
21827
21828
21829 /* EXPORT:
21830 Draw the cursor glyph of window W in glyph row ROW. See the
21831 comment of draw_glyphs for the meaning of HL. */
21832
21833 void
21834 draw_phys_cursor_glyph (w, row, hl)
21835 struct window *w;
21836 struct glyph_row *row;
21837 enum draw_glyphs_face hl;
21838 {
21839 /* If cursor hpos is out of bounds, don't draw garbage. This can
21840 happen in mini-buffer windows when switching between echo area
21841 glyphs and mini-buffer. */
21842 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21843 {
21844 int on_p = w->phys_cursor_on_p;
21845 int x1;
21846 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21847 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21848 hl, 0);
21849 w->phys_cursor_on_p = on_p;
21850
21851 if (hl == DRAW_CURSOR)
21852 w->phys_cursor_width = x1 - w->phys_cursor.x;
21853 /* When we erase the cursor, and ROW is overlapped by other
21854 rows, make sure that these overlapping parts of other rows
21855 are redrawn. */
21856 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21857 {
21858 w->phys_cursor_width = x1 - w->phys_cursor.x;
21859
21860 if (row > w->current_matrix->rows
21861 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21862 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21863 OVERLAPS_ERASED_CURSOR);
21864
21865 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21866 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21867 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21868 OVERLAPS_ERASED_CURSOR);
21869 }
21870 }
21871 }
21872
21873
21874 /* EXPORT:
21875 Erase the image of a cursor of window W from the screen. */
21876
21877 void
21878 erase_phys_cursor (w)
21879 struct window *w;
21880 {
21881 struct frame *f = XFRAME (w->frame);
21882 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21883 int hpos = w->phys_cursor.hpos;
21884 int vpos = w->phys_cursor.vpos;
21885 int mouse_face_here_p = 0;
21886 struct glyph_matrix *active_glyphs = w->current_matrix;
21887 struct glyph_row *cursor_row;
21888 struct glyph *cursor_glyph;
21889 enum draw_glyphs_face hl;
21890
21891 /* No cursor displayed or row invalidated => nothing to do on the
21892 screen. */
21893 if (w->phys_cursor_type == NO_CURSOR)
21894 goto mark_cursor_off;
21895
21896 /* VPOS >= active_glyphs->nrows means that window has been resized.
21897 Don't bother to erase the cursor. */
21898 if (vpos >= active_glyphs->nrows)
21899 goto mark_cursor_off;
21900
21901 /* If row containing cursor is marked invalid, there is nothing we
21902 can do. */
21903 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21904 if (!cursor_row->enabled_p)
21905 goto mark_cursor_off;
21906
21907 /* If line spacing is > 0, old cursor may only be partially visible in
21908 window after split-window. So adjust visible height. */
21909 cursor_row->visible_height = min (cursor_row->visible_height,
21910 window_text_bottom_y (w) - cursor_row->y);
21911
21912 /* If row is completely invisible, don't attempt to delete a cursor which
21913 isn't there. This can happen if cursor is at top of a window, and
21914 we switch to a buffer with a header line in that window. */
21915 if (cursor_row->visible_height <= 0)
21916 goto mark_cursor_off;
21917
21918 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21919 if (cursor_row->cursor_in_fringe_p)
21920 {
21921 cursor_row->cursor_in_fringe_p = 0;
21922 draw_fringe_bitmap (w, cursor_row, 0);
21923 goto mark_cursor_off;
21924 }
21925
21926 /* This can happen when the new row is shorter than the old one.
21927 In this case, either draw_glyphs or clear_end_of_line
21928 should have cleared the cursor. Note that we wouldn't be
21929 able to erase the cursor in this case because we don't have a
21930 cursor glyph at hand. */
21931 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21932 goto mark_cursor_off;
21933
21934 /* If the cursor is in the mouse face area, redisplay that when
21935 we clear the cursor. */
21936 if (! NILP (dpyinfo->mouse_face_window)
21937 && w == XWINDOW (dpyinfo->mouse_face_window)
21938 && (vpos > dpyinfo->mouse_face_beg_row
21939 || (vpos == dpyinfo->mouse_face_beg_row
21940 && hpos >= dpyinfo->mouse_face_beg_col))
21941 && (vpos < dpyinfo->mouse_face_end_row
21942 || (vpos == dpyinfo->mouse_face_end_row
21943 && hpos < dpyinfo->mouse_face_end_col))
21944 /* Don't redraw the cursor's spot in mouse face if it is at the
21945 end of a line (on a newline). The cursor appears there, but
21946 mouse highlighting does not. */
21947 && cursor_row->used[TEXT_AREA] > hpos)
21948 mouse_face_here_p = 1;
21949
21950 /* Maybe clear the display under the cursor. */
21951 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21952 {
21953 int x, y, left_x;
21954 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21955 int width;
21956
21957 cursor_glyph = get_phys_cursor_glyph (w);
21958 if (cursor_glyph == NULL)
21959 goto mark_cursor_off;
21960
21961 width = cursor_glyph->pixel_width;
21962 left_x = window_box_left_offset (w, TEXT_AREA);
21963 x = w->phys_cursor.x;
21964 if (x < left_x)
21965 width -= left_x - x;
21966 width = min (width, window_box_width (w, TEXT_AREA) - x);
21967 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21968 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21969
21970 if (width > 0)
21971 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21972 }
21973
21974 /* Erase the cursor by redrawing the character underneath it. */
21975 if (mouse_face_here_p)
21976 hl = DRAW_MOUSE_FACE;
21977 else
21978 hl = DRAW_NORMAL_TEXT;
21979 draw_phys_cursor_glyph (w, cursor_row, hl);
21980
21981 mark_cursor_off:
21982 w->phys_cursor_on_p = 0;
21983 w->phys_cursor_type = NO_CURSOR;
21984 }
21985
21986
21987 /* EXPORT:
21988 Display or clear cursor of window W. If ON is zero, clear the
21989 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21990 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21991
21992 void
21993 display_and_set_cursor (w, on, hpos, vpos, x, y)
21994 struct window *w;
21995 int on, hpos, vpos, x, y;
21996 {
21997 struct frame *f = XFRAME (w->frame);
21998 int new_cursor_type;
21999 int new_cursor_width;
22000 int active_cursor;
22001 struct glyph_row *glyph_row;
22002 struct glyph *glyph;
22003
22004 /* This is pointless on invisible frames, and dangerous on garbaged
22005 windows and frames; in the latter case, the frame or window may
22006 be in the midst of changing its size, and x and y may be off the
22007 window. */
22008 if (! FRAME_VISIBLE_P (f)
22009 || FRAME_GARBAGED_P (f)
22010 || vpos >= w->current_matrix->nrows
22011 || hpos >= w->current_matrix->matrix_w)
22012 return;
22013
22014 /* If cursor is off and we want it off, return quickly. */
22015 if (!on && !w->phys_cursor_on_p)
22016 return;
22017
22018 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22019 /* If cursor row is not enabled, we don't really know where to
22020 display the cursor. */
22021 if (!glyph_row->enabled_p)
22022 {
22023 w->phys_cursor_on_p = 0;
22024 return;
22025 }
22026
22027 glyph = NULL;
22028 if (!glyph_row->exact_window_width_line_p
22029 || hpos < glyph_row->used[TEXT_AREA])
22030 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22031
22032 xassert (interrupt_input_blocked);
22033
22034 /* Set new_cursor_type to the cursor we want to be displayed. */
22035 new_cursor_type = get_window_cursor_type (w, glyph,
22036 &new_cursor_width, &active_cursor);
22037
22038 /* If cursor is currently being shown and we don't want it to be or
22039 it is in the wrong place, or the cursor type is not what we want,
22040 erase it. */
22041 if (w->phys_cursor_on_p
22042 && (!on
22043 || w->phys_cursor.x != x
22044 || w->phys_cursor.y != y
22045 || new_cursor_type != w->phys_cursor_type
22046 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22047 && new_cursor_width != w->phys_cursor_width)))
22048 erase_phys_cursor (w);
22049
22050 /* Don't check phys_cursor_on_p here because that flag is only set
22051 to zero in some cases where we know that the cursor has been
22052 completely erased, to avoid the extra work of erasing the cursor
22053 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22054 still not be visible, or it has only been partly erased. */
22055 if (on)
22056 {
22057 w->phys_cursor_ascent = glyph_row->ascent;
22058 w->phys_cursor_height = glyph_row->height;
22059
22060 /* Set phys_cursor_.* before x_draw_.* is called because some
22061 of them may need the information. */
22062 w->phys_cursor.x = x;
22063 w->phys_cursor.y = glyph_row->y;
22064 w->phys_cursor.hpos = hpos;
22065 w->phys_cursor.vpos = vpos;
22066 }
22067
22068 rif->draw_window_cursor (w, glyph_row, x, y,
22069 new_cursor_type, new_cursor_width,
22070 on, active_cursor);
22071 }
22072
22073
22074 /* Switch the display of W's cursor on or off, according to the value
22075 of ON. */
22076
22077 static void
22078 update_window_cursor (w, on)
22079 struct window *w;
22080 int on;
22081 {
22082 /* Don't update cursor in windows whose frame is in the process
22083 of being deleted. */
22084 if (w->current_matrix)
22085 {
22086 BLOCK_INPUT;
22087 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22088 w->phys_cursor.x, w->phys_cursor.y);
22089 UNBLOCK_INPUT;
22090 }
22091 }
22092
22093
22094 /* Call update_window_cursor with parameter ON_P on all leaf windows
22095 in the window tree rooted at W. */
22096
22097 static void
22098 update_cursor_in_window_tree (w, on_p)
22099 struct window *w;
22100 int on_p;
22101 {
22102 while (w)
22103 {
22104 if (!NILP (w->hchild))
22105 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22106 else if (!NILP (w->vchild))
22107 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22108 else
22109 update_window_cursor (w, on_p);
22110
22111 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22112 }
22113 }
22114
22115
22116 /* EXPORT:
22117 Display the cursor on window W, or clear it, according to ON_P.
22118 Don't change the cursor's position. */
22119
22120 void
22121 x_update_cursor (f, on_p)
22122 struct frame *f;
22123 int on_p;
22124 {
22125 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22126 }
22127
22128
22129 /* EXPORT:
22130 Clear the cursor of window W to background color, and mark the
22131 cursor as not shown. This is used when the text where the cursor
22132 is is about to be rewritten. */
22133
22134 void
22135 x_clear_cursor (w)
22136 struct window *w;
22137 {
22138 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22139 update_window_cursor (w, 0);
22140 }
22141
22142
22143 /* EXPORT:
22144 Display the active region described by mouse_face_* according to DRAW. */
22145
22146 void
22147 show_mouse_face (dpyinfo, draw)
22148 Display_Info *dpyinfo;
22149 enum draw_glyphs_face draw;
22150 {
22151 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22152 struct frame *f = XFRAME (WINDOW_FRAME (w));
22153
22154 if (/* If window is in the process of being destroyed, don't bother
22155 to do anything. */
22156 w->current_matrix != NULL
22157 /* Don't update mouse highlight if hidden */
22158 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22159 /* Recognize when we are called to operate on rows that don't exist
22160 anymore. This can happen when a window is split. */
22161 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22162 {
22163 int phys_cursor_on_p = w->phys_cursor_on_p;
22164 struct glyph_row *row, *first, *last;
22165
22166 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22167 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22168
22169 for (row = first; row <= last && row->enabled_p; ++row)
22170 {
22171 int start_hpos, end_hpos, start_x;
22172
22173 /* For all but the first row, the highlight starts at column 0. */
22174 if (row == first)
22175 {
22176 start_hpos = dpyinfo->mouse_face_beg_col;
22177 start_x = dpyinfo->mouse_face_beg_x;
22178 }
22179 else
22180 {
22181 start_hpos = 0;
22182 start_x = 0;
22183 }
22184
22185 if (row == last)
22186 end_hpos = dpyinfo->mouse_face_end_col;
22187 else
22188 {
22189 end_hpos = row->used[TEXT_AREA];
22190 if (draw == DRAW_NORMAL_TEXT)
22191 row->fill_line_p = 1; /* Clear to end of line */
22192 }
22193
22194 if (end_hpos > start_hpos)
22195 {
22196 draw_glyphs (w, start_x, row, TEXT_AREA,
22197 start_hpos, end_hpos,
22198 draw, 0);
22199
22200 row->mouse_face_p
22201 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22202 }
22203 }
22204
22205 /* When we've written over the cursor, arrange for it to
22206 be displayed again. */
22207 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22208 {
22209 BLOCK_INPUT;
22210 display_and_set_cursor (w, 1,
22211 w->phys_cursor.hpos, w->phys_cursor.vpos,
22212 w->phys_cursor.x, w->phys_cursor.y);
22213 UNBLOCK_INPUT;
22214 }
22215 }
22216
22217 /* Change the mouse cursor. */
22218 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22219 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22220 else if (draw == DRAW_MOUSE_FACE)
22221 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22222 else
22223 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22224 }
22225
22226 /* EXPORT:
22227 Clear out the mouse-highlighted active region.
22228 Redraw it un-highlighted first. Value is non-zero if mouse
22229 face was actually drawn unhighlighted. */
22230
22231 int
22232 clear_mouse_face (dpyinfo)
22233 Display_Info *dpyinfo;
22234 {
22235 int cleared = 0;
22236
22237 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22238 {
22239 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22240 cleared = 1;
22241 }
22242
22243 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22244 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22245 dpyinfo->mouse_face_window = Qnil;
22246 dpyinfo->mouse_face_overlay = Qnil;
22247 return cleared;
22248 }
22249
22250
22251 /* EXPORT:
22252 Non-zero if physical cursor of window W is within mouse face. */
22253
22254 int
22255 cursor_in_mouse_face_p (w)
22256 struct window *w;
22257 {
22258 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22259 int in_mouse_face = 0;
22260
22261 if (WINDOWP (dpyinfo->mouse_face_window)
22262 && XWINDOW (dpyinfo->mouse_face_window) == w)
22263 {
22264 int hpos = w->phys_cursor.hpos;
22265 int vpos = w->phys_cursor.vpos;
22266
22267 if (vpos >= dpyinfo->mouse_face_beg_row
22268 && vpos <= dpyinfo->mouse_face_end_row
22269 && (vpos > dpyinfo->mouse_face_beg_row
22270 || hpos >= dpyinfo->mouse_face_beg_col)
22271 && (vpos < dpyinfo->mouse_face_end_row
22272 || hpos < dpyinfo->mouse_face_end_col
22273 || dpyinfo->mouse_face_past_end))
22274 in_mouse_face = 1;
22275 }
22276
22277 return in_mouse_face;
22278 }
22279
22280
22281
22282 \f
22283 /* Find the glyph matrix position of buffer position CHARPOS in window
22284 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22285 current glyphs must be up to date. If CHARPOS is above window
22286 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22287 of last line in W. In the row containing CHARPOS, stop before glyphs
22288 having STOP as object. */
22289
22290 #if 1 /* This is a version of fast_find_position that's more correct
22291 in the presence of hscrolling, for example. I didn't install
22292 it right away because the problem fixed is minor, it failed
22293 in 20.x as well, and I think it's too risky to install
22294 so near the release of 21.1. 2001-09-25 gerd. */
22295
22296 static int
22297 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22298 struct window *w;
22299 EMACS_INT charpos;
22300 int *hpos, *vpos, *x, *y;
22301 Lisp_Object stop;
22302 {
22303 struct glyph_row *row, *first;
22304 struct glyph *glyph, *end;
22305 int past_end = 0;
22306
22307 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22308 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22309 {
22310 *x = first->x;
22311 *y = first->y;
22312 *hpos = 0;
22313 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22314 return 1;
22315 }
22316
22317 row = row_containing_pos (w, charpos, first, NULL, 0);
22318 if (row == NULL)
22319 {
22320 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22321 past_end = 1;
22322 }
22323
22324 /* If whole rows or last part of a row came from a display overlay,
22325 row_containing_pos will skip over such rows because their end pos
22326 equals the start pos of the overlay or interval.
22327
22328 Move back if we have a STOP object and previous row's
22329 end glyph came from STOP. */
22330 if (!NILP (stop))
22331 {
22332 struct glyph_row *prev;
22333 while ((prev = row - 1, prev >= first)
22334 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22335 && prev->used[TEXT_AREA] > 0)
22336 {
22337 struct glyph *beg = prev->glyphs[TEXT_AREA];
22338 glyph = beg + prev->used[TEXT_AREA];
22339 while (--glyph >= beg
22340 && INTEGERP (glyph->object));
22341 if (glyph < beg
22342 || !EQ (stop, glyph->object))
22343 break;
22344 row = prev;
22345 }
22346 }
22347
22348 *x = row->x;
22349 *y = row->y;
22350 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22351
22352 glyph = row->glyphs[TEXT_AREA];
22353 end = glyph + row->used[TEXT_AREA];
22354
22355 /* Skip over glyphs not having an object at the start of the row.
22356 These are special glyphs like truncation marks on terminal
22357 frames. */
22358 if (row->displays_text_p)
22359 while (glyph < end
22360 && INTEGERP (glyph->object)
22361 && !EQ (stop, glyph->object)
22362 && glyph->charpos < 0)
22363 {
22364 *x += glyph->pixel_width;
22365 ++glyph;
22366 }
22367
22368 while (glyph < end
22369 && !INTEGERP (glyph->object)
22370 && !EQ (stop, glyph->object)
22371 && (!BUFFERP (glyph->object)
22372 || glyph->charpos < charpos))
22373 {
22374 *x += glyph->pixel_width;
22375 ++glyph;
22376 }
22377
22378 *hpos = glyph - row->glyphs[TEXT_AREA];
22379 return !past_end;
22380 }
22381
22382 #else /* not 1 */
22383
22384 static int
22385 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22386 struct window *w;
22387 EMACS_INT pos;
22388 int *hpos, *vpos, *x, *y;
22389 Lisp_Object stop;
22390 {
22391 int i;
22392 int lastcol;
22393 int maybe_next_line_p = 0;
22394 int line_start_position;
22395 int yb = window_text_bottom_y (w);
22396 struct glyph_row *row, *best_row;
22397 int row_vpos, best_row_vpos;
22398 int current_x;
22399
22400 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22401 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22402
22403 while (row->y < yb)
22404 {
22405 if (row->used[TEXT_AREA])
22406 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22407 else
22408 line_start_position = 0;
22409
22410 if (line_start_position > pos)
22411 break;
22412 /* If the position sought is the end of the buffer,
22413 don't include the blank lines at the bottom of the window. */
22414 else if (line_start_position == pos
22415 && pos == BUF_ZV (XBUFFER (w->buffer)))
22416 {
22417 maybe_next_line_p = 1;
22418 break;
22419 }
22420 else if (line_start_position > 0)
22421 {
22422 best_row = row;
22423 best_row_vpos = row_vpos;
22424 }
22425
22426 if (row->y + row->height >= yb)
22427 break;
22428
22429 ++row;
22430 ++row_vpos;
22431 }
22432
22433 /* Find the right column within BEST_ROW. */
22434 lastcol = 0;
22435 current_x = best_row->x;
22436 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22437 {
22438 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22439 int charpos = glyph->charpos;
22440
22441 if (BUFFERP (glyph->object))
22442 {
22443 if (charpos == pos)
22444 {
22445 *hpos = i;
22446 *vpos = best_row_vpos;
22447 *x = current_x;
22448 *y = best_row->y;
22449 return 1;
22450 }
22451 else if (charpos > pos)
22452 break;
22453 }
22454 else if (EQ (glyph->object, stop))
22455 break;
22456
22457 if (charpos > 0)
22458 lastcol = i;
22459 current_x += glyph->pixel_width;
22460 }
22461
22462 /* If we're looking for the end of the buffer,
22463 and we didn't find it in the line we scanned,
22464 use the start of the following line. */
22465 if (maybe_next_line_p)
22466 {
22467 ++best_row;
22468 ++best_row_vpos;
22469 lastcol = 0;
22470 current_x = best_row->x;
22471 }
22472
22473 *vpos = best_row_vpos;
22474 *hpos = lastcol + 1;
22475 *x = current_x;
22476 *y = best_row->y;
22477 return 0;
22478 }
22479
22480 #endif /* not 1 */
22481
22482
22483 /* Find the position of the glyph for position POS in OBJECT in
22484 window W's current matrix, and return in *X, *Y the pixel
22485 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22486
22487 RIGHT_P non-zero means return the position of the right edge of the
22488 glyph, RIGHT_P zero means return the left edge position.
22489
22490 If no glyph for POS exists in the matrix, return the position of
22491 the glyph with the next smaller position that is in the matrix, if
22492 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22493 exists in the matrix, return the position of the glyph with the
22494 next larger position in OBJECT.
22495
22496 Value is non-zero if a glyph was found. */
22497
22498 static int
22499 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22500 struct window *w;
22501 EMACS_INT pos;
22502 Lisp_Object object;
22503 int *hpos, *vpos, *x, *y;
22504 int right_p;
22505 {
22506 int yb = window_text_bottom_y (w);
22507 struct glyph_row *r;
22508 struct glyph *best_glyph = NULL;
22509 struct glyph_row *best_row = NULL;
22510 int best_x = 0;
22511
22512 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22513 r->enabled_p && r->y < yb;
22514 ++r)
22515 {
22516 struct glyph *g = r->glyphs[TEXT_AREA];
22517 struct glyph *e = g + r->used[TEXT_AREA];
22518 int gx;
22519
22520 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22521 if (EQ (g->object, object))
22522 {
22523 if (g->charpos == pos)
22524 {
22525 best_glyph = g;
22526 best_x = gx;
22527 best_row = r;
22528 goto found;
22529 }
22530 else if (best_glyph == NULL
22531 || ((abs (g->charpos - pos)
22532 < abs (best_glyph->charpos - pos))
22533 && (right_p
22534 ? g->charpos < pos
22535 : g->charpos > pos)))
22536 {
22537 best_glyph = g;
22538 best_x = gx;
22539 best_row = r;
22540 }
22541 }
22542 }
22543
22544 found:
22545
22546 if (best_glyph)
22547 {
22548 *x = best_x;
22549 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22550
22551 if (right_p)
22552 {
22553 *x += best_glyph->pixel_width;
22554 ++*hpos;
22555 }
22556
22557 *y = best_row->y;
22558 *vpos = best_row - w->current_matrix->rows;
22559 }
22560
22561 return best_glyph != NULL;
22562 }
22563
22564
22565 /* See if position X, Y is within a hot-spot of an image. */
22566
22567 static int
22568 on_hot_spot_p (hot_spot, x, y)
22569 Lisp_Object hot_spot;
22570 int x, y;
22571 {
22572 if (!CONSP (hot_spot))
22573 return 0;
22574
22575 if (EQ (XCAR (hot_spot), Qrect))
22576 {
22577 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22578 Lisp_Object rect = XCDR (hot_spot);
22579 Lisp_Object tem;
22580 if (!CONSP (rect))
22581 return 0;
22582 if (!CONSP (XCAR (rect)))
22583 return 0;
22584 if (!CONSP (XCDR (rect)))
22585 return 0;
22586 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22587 return 0;
22588 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22589 return 0;
22590 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22591 return 0;
22592 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22593 return 0;
22594 return 1;
22595 }
22596 else if (EQ (XCAR (hot_spot), Qcircle))
22597 {
22598 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22599 Lisp_Object circ = XCDR (hot_spot);
22600 Lisp_Object lr, lx0, ly0;
22601 if (CONSP (circ)
22602 && CONSP (XCAR (circ))
22603 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22604 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22605 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22606 {
22607 double r = XFLOATINT (lr);
22608 double dx = XINT (lx0) - x;
22609 double dy = XINT (ly0) - y;
22610 return (dx * dx + dy * dy <= r * r);
22611 }
22612 }
22613 else if (EQ (XCAR (hot_spot), Qpoly))
22614 {
22615 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22616 if (VECTORP (XCDR (hot_spot)))
22617 {
22618 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22619 Lisp_Object *poly = v->contents;
22620 int n = v->size;
22621 int i;
22622 int inside = 0;
22623 Lisp_Object lx, ly;
22624 int x0, y0;
22625
22626 /* Need an even number of coordinates, and at least 3 edges. */
22627 if (n < 6 || n & 1)
22628 return 0;
22629
22630 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22631 If count is odd, we are inside polygon. Pixels on edges
22632 may or may not be included depending on actual geometry of the
22633 polygon. */
22634 if ((lx = poly[n-2], !INTEGERP (lx))
22635 || (ly = poly[n-1], !INTEGERP (lx)))
22636 return 0;
22637 x0 = XINT (lx), y0 = XINT (ly);
22638 for (i = 0; i < n; i += 2)
22639 {
22640 int x1 = x0, y1 = y0;
22641 if ((lx = poly[i], !INTEGERP (lx))
22642 || (ly = poly[i+1], !INTEGERP (ly)))
22643 return 0;
22644 x0 = XINT (lx), y0 = XINT (ly);
22645
22646 /* Does this segment cross the X line? */
22647 if (x0 >= x)
22648 {
22649 if (x1 >= x)
22650 continue;
22651 }
22652 else if (x1 < x)
22653 continue;
22654 if (y > y0 && y > y1)
22655 continue;
22656 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22657 inside = !inside;
22658 }
22659 return inside;
22660 }
22661 }
22662 /* If we don't understand the format, pretend we're not in the hot-spot. */
22663 return 0;
22664 }
22665
22666 Lisp_Object
22667 find_hot_spot (map, x, y)
22668 Lisp_Object map;
22669 int x, y;
22670 {
22671 while (CONSP (map))
22672 {
22673 if (CONSP (XCAR (map))
22674 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22675 return XCAR (map);
22676 map = XCDR (map);
22677 }
22678
22679 return Qnil;
22680 }
22681
22682 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22683 3, 3, 0,
22684 doc: /* Lookup in image map MAP coordinates X and Y.
22685 An image map is an alist where each element has the format (AREA ID PLIST).
22686 An AREA is specified as either a rectangle, a circle, or a polygon:
22687 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22688 pixel coordinates of the upper left and bottom right corners.
22689 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22690 and the radius of the circle; r may be a float or integer.
22691 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22692 vector describes one corner in the polygon.
22693 Returns the alist element for the first matching AREA in MAP. */)
22694 (map, x, y)
22695 Lisp_Object map;
22696 Lisp_Object x, y;
22697 {
22698 if (NILP (map))
22699 return Qnil;
22700
22701 CHECK_NUMBER (x);
22702 CHECK_NUMBER (y);
22703
22704 return find_hot_spot (map, XINT (x), XINT (y));
22705 }
22706
22707
22708 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22709 static void
22710 define_frame_cursor1 (f, cursor, pointer)
22711 struct frame *f;
22712 Cursor cursor;
22713 Lisp_Object pointer;
22714 {
22715 /* Do not change cursor shape while dragging mouse. */
22716 if (!NILP (do_mouse_tracking))
22717 return;
22718
22719 if (!NILP (pointer))
22720 {
22721 if (EQ (pointer, Qarrow))
22722 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22723 else if (EQ (pointer, Qhand))
22724 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22725 else if (EQ (pointer, Qtext))
22726 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22727 else if (EQ (pointer, intern ("hdrag")))
22728 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22729 #ifdef HAVE_X_WINDOWS
22730 else if (EQ (pointer, intern ("vdrag")))
22731 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22732 #endif
22733 else if (EQ (pointer, intern ("hourglass")))
22734 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22735 else if (EQ (pointer, Qmodeline))
22736 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22737 else
22738 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22739 }
22740
22741 if (cursor != No_Cursor)
22742 rif->define_frame_cursor (f, cursor);
22743 }
22744
22745 /* Take proper action when mouse has moved to the mode or header line
22746 or marginal area AREA of window W, x-position X and y-position Y.
22747 X is relative to the start of the text display area of W, so the
22748 width of bitmap areas and scroll bars must be subtracted to get a
22749 position relative to the start of the mode line. */
22750
22751 static void
22752 note_mode_line_or_margin_highlight (window, x, y, area)
22753 Lisp_Object window;
22754 int x, y;
22755 enum window_part area;
22756 {
22757 struct window *w = XWINDOW (window);
22758 struct frame *f = XFRAME (w->frame);
22759 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22760 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22761 Lisp_Object pointer = Qnil;
22762 int charpos, dx, dy, width, height;
22763 Lisp_Object string, object = Qnil;
22764 Lisp_Object pos, help;
22765
22766 Lisp_Object mouse_face;
22767 int original_x_pixel = x;
22768 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22769 struct glyph_row *row;
22770
22771 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22772 {
22773 int x0;
22774 struct glyph *end;
22775
22776 string = mode_line_string (w, area, &x, &y, &charpos,
22777 &object, &dx, &dy, &width, &height);
22778
22779 row = (area == ON_MODE_LINE
22780 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22781 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22782
22783 /* Find glyph */
22784 if (row->mode_line_p && row->enabled_p)
22785 {
22786 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22787 end = glyph + row->used[TEXT_AREA];
22788
22789 for (x0 = original_x_pixel;
22790 glyph < end && x0 >= glyph->pixel_width;
22791 ++glyph)
22792 x0 -= glyph->pixel_width;
22793
22794 if (glyph >= end)
22795 glyph = NULL;
22796 }
22797 }
22798 else
22799 {
22800 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22801 string = marginal_area_string (w, area, &x, &y, &charpos,
22802 &object, &dx, &dy, &width, &height);
22803 }
22804
22805 help = Qnil;
22806
22807 if (IMAGEP (object))
22808 {
22809 Lisp_Object image_map, hotspot;
22810 if ((image_map = Fplist_get (XCDR (object), QCmap),
22811 !NILP (image_map))
22812 && (hotspot = find_hot_spot (image_map, dx, dy),
22813 CONSP (hotspot))
22814 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22815 {
22816 Lisp_Object area_id, plist;
22817
22818 area_id = XCAR (hotspot);
22819 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22820 If so, we could look for mouse-enter, mouse-leave
22821 properties in PLIST (and do something...). */
22822 hotspot = XCDR (hotspot);
22823 if (CONSP (hotspot)
22824 && (plist = XCAR (hotspot), CONSP (plist)))
22825 {
22826 pointer = Fplist_get (plist, Qpointer);
22827 if (NILP (pointer))
22828 pointer = Qhand;
22829 help = Fplist_get (plist, Qhelp_echo);
22830 if (!NILP (help))
22831 {
22832 help_echo_string = help;
22833 /* Is this correct? ++kfs */
22834 XSETWINDOW (help_echo_window, w);
22835 help_echo_object = w->buffer;
22836 help_echo_pos = charpos;
22837 }
22838 }
22839 }
22840 if (NILP (pointer))
22841 pointer = Fplist_get (XCDR (object), QCpointer);
22842 }
22843
22844 if (STRINGP (string))
22845 {
22846 pos = make_number (charpos);
22847 /* If we're on a string with `help-echo' text property, arrange
22848 for the help to be displayed. This is done by setting the
22849 global variable help_echo_string to the help string. */
22850 if (NILP (help))
22851 {
22852 help = Fget_text_property (pos, Qhelp_echo, string);
22853 if (!NILP (help))
22854 {
22855 help_echo_string = help;
22856 XSETWINDOW (help_echo_window, w);
22857 help_echo_object = string;
22858 help_echo_pos = charpos;
22859 }
22860 }
22861
22862 if (NILP (pointer))
22863 pointer = Fget_text_property (pos, Qpointer, string);
22864
22865 /* Change the mouse pointer according to what is under X/Y. */
22866 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22867 {
22868 Lisp_Object map;
22869 map = Fget_text_property (pos, Qlocal_map, string);
22870 if (!KEYMAPP (map))
22871 map = Fget_text_property (pos, Qkeymap, string);
22872 if (!KEYMAPP (map))
22873 cursor = dpyinfo->vertical_scroll_bar_cursor;
22874 }
22875
22876 /* Change the mouse face according to what is under X/Y. */
22877 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22878 if (!NILP (mouse_face)
22879 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22880 && glyph)
22881 {
22882 Lisp_Object b, e;
22883
22884 struct glyph * tmp_glyph;
22885
22886 int gpos;
22887 int gseq_length;
22888 int total_pixel_width;
22889 int ignore;
22890
22891 int vpos, hpos;
22892
22893 b = Fprevious_single_property_change (make_number (charpos + 1),
22894 Qmouse_face, string, Qnil);
22895 if (NILP (b))
22896 b = make_number (0);
22897
22898 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22899 if (NILP (e))
22900 e = make_number (SCHARS (string));
22901
22902 /* Calculate the position(glyph position: GPOS) of GLYPH in
22903 displayed string. GPOS is different from CHARPOS.
22904
22905 CHARPOS is the position of glyph in internal string
22906 object. A mode line string format has structures which
22907 is converted to a flatten by emacs lisp interpreter.
22908 The internal string is an element of the structures.
22909 The displayed string is the flatten string. */
22910 gpos = 0;
22911 if (glyph > row_start_glyph)
22912 {
22913 tmp_glyph = glyph - 1;
22914 while (tmp_glyph >= row_start_glyph
22915 && tmp_glyph->charpos >= XINT (b)
22916 && EQ (tmp_glyph->object, glyph->object))
22917 {
22918 tmp_glyph--;
22919 gpos++;
22920 }
22921 }
22922
22923 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22924 displayed string holding GLYPH.
22925
22926 GSEQ_LENGTH is different from SCHARS (STRING).
22927 SCHARS (STRING) returns the length of the internal string. */
22928 for (tmp_glyph = glyph, gseq_length = gpos;
22929 tmp_glyph->charpos < XINT (e);
22930 tmp_glyph++, gseq_length++)
22931 {
22932 if (!EQ (tmp_glyph->object, glyph->object))
22933 break;
22934 }
22935
22936 total_pixel_width = 0;
22937 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22938 total_pixel_width += tmp_glyph->pixel_width;
22939
22940 /* Pre calculation of re-rendering position */
22941 vpos = (x - gpos);
22942 hpos = (area == ON_MODE_LINE
22943 ? (w->current_matrix)->nrows - 1
22944 : 0);
22945
22946 /* If the re-rendering position is included in the last
22947 re-rendering area, we should do nothing. */
22948 if ( EQ (window, dpyinfo->mouse_face_window)
22949 && dpyinfo->mouse_face_beg_col <= vpos
22950 && vpos < dpyinfo->mouse_face_end_col
22951 && dpyinfo->mouse_face_beg_row == hpos )
22952 return;
22953
22954 if (clear_mouse_face (dpyinfo))
22955 cursor = No_Cursor;
22956
22957 dpyinfo->mouse_face_beg_col = vpos;
22958 dpyinfo->mouse_face_beg_row = hpos;
22959
22960 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22961 dpyinfo->mouse_face_beg_y = 0;
22962
22963 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22964 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22965
22966 dpyinfo->mouse_face_end_x = 0;
22967 dpyinfo->mouse_face_end_y = 0;
22968
22969 dpyinfo->mouse_face_past_end = 0;
22970 dpyinfo->mouse_face_window = window;
22971
22972 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22973 charpos,
22974 0, 0, 0, &ignore,
22975 glyph->face_id, 1);
22976 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22977
22978 if (NILP (pointer))
22979 pointer = Qhand;
22980 }
22981 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22982 clear_mouse_face (dpyinfo);
22983 }
22984 define_frame_cursor1 (f, cursor, pointer);
22985 }
22986
22987
22988 /* EXPORT:
22989 Take proper action when the mouse has moved to position X, Y on
22990 frame F as regards highlighting characters that have mouse-face
22991 properties. Also de-highlighting chars where the mouse was before.
22992 X and Y can be negative or out of range. */
22993
22994 void
22995 note_mouse_highlight (f, x, y)
22996 struct frame *f;
22997 int x, y;
22998 {
22999 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23000 enum window_part part;
23001 Lisp_Object window;
23002 struct window *w;
23003 Cursor cursor = No_Cursor;
23004 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23005 struct buffer *b;
23006
23007 /* When a menu is active, don't highlight because this looks odd. */
23008 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23009 if (popup_activated ())
23010 return;
23011 #endif
23012
23013 if (NILP (Vmouse_highlight)
23014 || !f->glyphs_initialized_p)
23015 return;
23016
23017 dpyinfo->mouse_face_mouse_x = x;
23018 dpyinfo->mouse_face_mouse_y = y;
23019 dpyinfo->mouse_face_mouse_frame = f;
23020
23021 if (dpyinfo->mouse_face_defer)
23022 return;
23023
23024 if (gc_in_progress)
23025 {
23026 dpyinfo->mouse_face_deferred_gc = 1;
23027 return;
23028 }
23029
23030 /* Which window is that in? */
23031 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23032
23033 /* If we were displaying active text in another window, clear that.
23034 Also clear if we move out of text area in same window. */
23035 if (! EQ (window, dpyinfo->mouse_face_window)
23036 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23037 && !NILP (dpyinfo->mouse_face_window)))
23038 clear_mouse_face (dpyinfo);
23039
23040 /* Not on a window -> return. */
23041 if (!WINDOWP (window))
23042 return;
23043
23044 /* Reset help_echo_string. It will get recomputed below. */
23045 help_echo_string = Qnil;
23046
23047 /* Convert to window-relative pixel coordinates. */
23048 w = XWINDOW (window);
23049 frame_to_window_pixel_xy (w, &x, &y);
23050
23051 /* Handle tool-bar window differently since it doesn't display a
23052 buffer. */
23053 if (EQ (window, f->tool_bar_window))
23054 {
23055 note_tool_bar_highlight (f, x, y);
23056 return;
23057 }
23058
23059 /* Mouse is on the mode, header line or margin? */
23060 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23061 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23062 {
23063 note_mode_line_or_margin_highlight (window, x, y, part);
23064 return;
23065 }
23066
23067 if (part == ON_VERTICAL_BORDER)
23068 {
23069 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23070 help_echo_string = build_string ("drag-mouse-1: resize");
23071 }
23072 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23073 || part == ON_SCROLL_BAR)
23074 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23075 else
23076 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23077
23078 /* Are we in a window whose display is up to date?
23079 And verify the buffer's text has not changed. */
23080 b = XBUFFER (w->buffer);
23081 if (part == ON_TEXT
23082 && EQ (w->window_end_valid, w->buffer)
23083 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23084 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23085 {
23086 int hpos, vpos, pos, i, dx, dy, area;
23087 struct glyph *glyph;
23088 Lisp_Object object;
23089 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23090 Lisp_Object *overlay_vec = NULL;
23091 int noverlays;
23092 struct buffer *obuf;
23093 int obegv, ozv, same_region;
23094
23095 /* Find the glyph under X/Y. */
23096 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23097
23098 /* Look for :pointer property on image. */
23099 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23100 {
23101 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23102 if (img != NULL && IMAGEP (img->spec))
23103 {
23104 Lisp_Object image_map, hotspot;
23105 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23106 !NILP (image_map))
23107 && (hotspot = find_hot_spot (image_map,
23108 glyph->slice.x + dx,
23109 glyph->slice.y + dy),
23110 CONSP (hotspot))
23111 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23112 {
23113 Lisp_Object area_id, plist;
23114
23115 area_id = XCAR (hotspot);
23116 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23117 If so, we could look for mouse-enter, mouse-leave
23118 properties in PLIST (and do something...). */
23119 hotspot = XCDR (hotspot);
23120 if (CONSP (hotspot)
23121 && (plist = XCAR (hotspot), CONSP (plist)))
23122 {
23123 pointer = Fplist_get (plist, Qpointer);
23124 if (NILP (pointer))
23125 pointer = Qhand;
23126 help_echo_string = Fplist_get (plist, Qhelp_echo);
23127 if (!NILP (help_echo_string))
23128 {
23129 help_echo_window = window;
23130 help_echo_object = glyph->object;
23131 help_echo_pos = glyph->charpos;
23132 }
23133 }
23134 }
23135 if (NILP (pointer))
23136 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23137 }
23138 }
23139
23140 /* Clear mouse face if X/Y not over text. */
23141 if (glyph == NULL
23142 || area != TEXT_AREA
23143 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23144 {
23145 if (clear_mouse_face (dpyinfo))
23146 cursor = No_Cursor;
23147 if (NILP (pointer))
23148 {
23149 if (area != TEXT_AREA)
23150 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23151 else
23152 pointer = Vvoid_text_area_pointer;
23153 }
23154 goto set_cursor;
23155 }
23156
23157 pos = glyph->charpos;
23158 object = glyph->object;
23159 if (!STRINGP (object) && !BUFFERP (object))
23160 goto set_cursor;
23161
23162 /* If we get an out-of-range value, return now; avoid an error. */
23163 if (BUFFERP (object) && pos > BUF_Z (b))
23164 goto set_cursor;
23165
23166 /* Make the window's buffer temporarily current for
23167 overlays_at and compute_char_face. */
23168 obuf = current_buffer;
23169 current_buffer = b;
23170 obegv = BEGV;
23171 ozv = ZV;
23172 BEGV = BEG;
23173 ZV = Z;
23174
23175 /* Is this char mouse-active or does it have help-echo? */
23176 position = make_number (pos);
23177
23178 if (BUFFERP (object))
23179 {
23180 /* Put all the overlays we want in a vector in overlay_vec. */
23181 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23182 /* Sort overlays into increasing priority order. */
23183 noverlays = sort_overlays (overlay_vec, noverlays, w);
23184 }
23185 else
23186 noverlays = 0;
23187
23188 same_region = (EQ (window, dpyinfo->mouse_face_window)
23189 && vpos >= dpyinfo->mouse_face_beg_row
23190 && vpos <= dpyinfo->mouse_face_end_row
23191 && (vpos > dpyinfo->mouse_face_beg_row
23192 || hpos >= dpyinfo->mouse_face_beg_col)
23193 && (vpos < dpyinfo->mouse_face_end_row
23194 || hpos < dpyinfo->mouse_face_end_col
23195 || dpyinfo->mouse_face_past_end));
23196
23197 if (same_region)
23198 cursor = No_Cursor;
23199
23200 /* Check mouse-face highlighting. */
23201 if (! same_region
23202 /* If there exists an overlay with mouse-face overlapping
23203 the one we are currently highlighting, we have to
23204 check if we enter the overlapping overlay, and then
23205 highlight only that. */
23206 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23207 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23208 {
23209 /* Find the highest priority overlay that has a mouse-face
23210 property. */
23211 overlay = Qnil;
23212 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23213 {
23214 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23215 if (!NILP (mouse_face))
23216 overlay = overlay_vec[i];
23217 }
23218
23219 /* If we're actually highlighting the same overlay as
23220 before, there's no need to do that again. */
23221 if (!NILP (overlay)
23222 && EQ (overlay, dpyinfo->mouse_face_overlay))
23223 goto check_help_echo;
23224
23225 dpyinfo->mouse_face_overlay = overlay;
23226
23227 /* Clear the display of the old active region, if any. */
23228 if (clear_mouse_face (dpyinfo))
23229 cursor = No_Cursor;
23230
23231 /* If no overlay applies, get a text property. */
23232 if (NILP (overlay))
23233 mouse_face = Fget_text_property (position, Qmouse_face, object);
23234
23235 /* Handle the overlay case. */
23236 if (!NILP (overlay))
23237 {
23238 /* Find the range of text around this char that
23239 should be active. */
23240 Lisp_Object before, after;
23241 int ignore;
23242
23243 before = Foverlay_start (overlay);
23244 after = Foverlay_end (overlay);
23245 /* Record this as the current active region. */
23246 fast_find_position (w, XFASTINT (before),
23247 &dpyinfo->mouse_face_beg_col,
23248 &dpyinfo->mouse_face_beg_row,
23249 &dpyinfo->mouse_face_beg_x,
23250 &dpyinfo->mouse_face_beg_y, Qnil);
23251
23252 dpyinfo->mouse_face_past_end
23253 = !fast_find_position (w, XFASTINT (after),
23254 &dpyinfo->mouse_face_end_col,
23255 &dpyinfo->mouse_face_end_row,
23256 &dpyinfo->mouse_face_end_x,
23257 &dpyinfo->mouse_face_end_y, Qnil);
23258 dpyinfo->mouse_face_window = window;
23259
23260 dpyinfo->mouse_face_face_id
23261 = face_at_buffer_position (w, pos, 0, 0,
23262 &ignore, pos + 1,
23263 !dpyinfo->mouse_face_hidden);
23264
23265 /* Display it as active. */
23266 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23267 cursor = No_Cursor;
23268 }
23269 /* Handle the text property case. */
23270 else if (!NILP (mouse_face) && BUFFERP (object))
23271 {
23272 /* Find the range of text around this char that
23273 should be active. */
23274 Lisp_Object before, after, beginning, end;
23275 int ignore;
23276
23277 beginning = Fmarker_position (w->start);
23278 end = make_number (BUF_Z (XBUFFER (object))
23279 - XFASTINT (w->window_end_pos));
23280 before
23281 = Fprevious_single_property_change (make_number (pos + 1),
23282 Qmouse_face,
23283 object, beginning);
23284 after
23285 = Fnext_single_property_change (position, Qmouse_face,
23286 object, end);
23287
23288 /* Record this as the current active region. */
23289 fast_find_position (w, XFASTINT (before),
23290 &dpyinfo->mouse_face_beg_col,
23291 &dpyinfo->mouse_face_beg_row,
23292 &dpyinfo->mouse_face_beg_x,
23293 &dpyinfo->mouse_face_beg_y, Qnil);
23294 dpyinfo->mouse_face_past_end
23295 = !fast_find_position (w, XFASTINT (after),
23296 &dpyinfo->mouse_face_end_col,
23297 &dpyinfo->mouse_face_end_row,
23298 &dpyinfo->mouse_face_end_x,
23299 &dpyinfo->mouse_face_end_y, Qnil);
23300 dpyinfo->mouse_face_window = window;
23301
23302 if (BUFFERP (object))
23303 dpyinfo->mouse_face_face_id
23304 = face_at_buffer_position (w, pos, 0, 0,
23305 &ignore, pos + 1,
23306 !dpyinfo->mouse_face_hidden);
23307
23308 /* Display it as active. */
23309 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23310 cursor = No_Cursor;
23311 }
23312 else if (!NILP (mouse_face) && STRINGP (object))
23313 {
23314 Lisp_Object b, e;
23315 int ignore;
23316
23317 b = Fprevious_single_property_change (make_number (pos + 1),
23318 Qmouse_face,
23319 object, Qnil);
23320 e = Fnext_single_property_change (position, Qmouse_face,
23321 object, Qnil);
23322 if (NILP (b))
23323 b = make_number (0);
23324 if (NILP (e))
23325 e = make_number (SCHARS (object) - 1);
23326
23327 fast_find_string_pos (w, XINT (b), object,
23328 &dpyinfo->mouse_face_beg_col,
23329 &dpyinfo->mouse_face_beg_row,
23330 &dpyinfo->mouse_face_beg_x,
23331 &dpyinfo->mouse_face_beg_y, 0);
23332 fast_find_string_pos (w, XINT (e), object,
23333 &dpyinfo->mouse_face_end_col,
23334 &dpyinfo->mouse_face_end_row,
23335 &dpyinfo->mouse_face_end_x,
23336 &dpyinfo->mouse_face_end_y, 1);
23337 dpyinfo->mouse_face_past_end = 0;
23338 dpyinfo->mouse_face_window = window;
23339 dpyinfo->mouse_face_face_id
23340 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23341 glyph->face_id, 1);
23342 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23343 cursor = No_Cursor;
23344 }
23345 else if (STRINGP (object) && NILP (mouse_face))
23346 {
23347 /* A string which doesn't have mouse-face, but
23348 the text ``under'' it might have. */
23349 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23350 int start = MATRIX_ROW_START_CHARPOS (r);
23351
23352 pos = string_buffer_position (w, object, start);
23353 if (pos > 0)
23354 mouse_face = get_char_property_and_overlay (make_number (pos),
23355 Qmouse_face,
23356 w->buffer,
23357 &overlay);
23358 if (!NILP (mouse_face) && !NILP (overlay))
23359 {
23360 Lisp_Object before = Foverlay_start (overlay);
23361 Lisp_Object after = Foverlay_end (overlay);
23362 int ignore;
23363
23364 /* Note that we might not be able to find position
23365 BEFORE in the glyph matrix if the overlay is
23366 entirely covered by a `display' property. In
23367 this case, we overshoot. So let's stop in
23368 the glyph matrix before glyphs for OBJECT. */
23369 fast_find_position (w, XFASTINT (before),
23370 &dpyinfo->mouse_face_beg_col,
23371 &dpyinfo->mouse_face_beg_row,
23372 &dpyinfo->mouse_face_beg_x,
23373 &dpyinfo->mouse_face_beg_y,
23374 object);
23375
23376 dpyinfo->mouse_face_past_end
23377 = !fast_find_position (w, XFASTINT (after),
23378 &dpyinfo->mouse_face_end_col,
23379 &dpyinfo->mouse_face_end_row,
23380 &dpyinfo->mouse_face_end_x,
23381 &dpyinfo->mouse_face_end_y,
23382 Qnil);
23383 dpyinfo->mouse_face_window = window;
23384 dpyinfo->mouse_face_face_id
23385 = face_at_buffer_position (w, pos, 0, 0,
23386 &ignore, pos + 1,
23387 !dpyinfo->mouse_face_hidden);
23388
23389 /* Display it as active. */
23390 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23391 cursor = No_Cursor;
23392 }
23393 }
23394 }
23395
23396 check_help_echo:
23397
23398 /* Look for a `help-echo' property. */
23399 if (NILP (help_echo_string)) {
23400 Lisp_Object help, overlay;
23401
23402 /* Check overlays first. */
23403 help = overlay = Qnil;
23404 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23405 {
23406 overlay = overlay_vec[i];
23407 help = Foverlay_get (overlay, Qhelp_echo);
23408 }
23409
23410 if (!NILP (help))
23411 {
23412 help_echo_string = help;
23413 help_echo_window = window;
23414 help_echo_object = overlay;
23415 help_echo_pos = pos;
23416 }
23417 else
23418 {
23419 Lisp_Object object = glyph->object;
23420 int charpos = glyph->charpos;
23421
23422 /* Try text properties. */
23423 if (STRINGP (object)
23424 && charpos >= 0
23425 && charpos < SCHARS (object))
23426 {
23427 help = Fget_text_property (make_number (charpos),
23428 Qhelp_echo, object);
23429 if (NILP (help))
23430 {
23431 /* If the string itself doesn't specify a help-echo,
23432 see if the buffer text ``under'' it does. */
23433 struct glyph_row *r
23434 = MATRIX_ROW (w->current_matrix, vpos);
23435 int start = MATRIX_ROW_START_CHARPOS (r);
23436 int pos = string_buffer_position (w, object, start);
23437 if (pos > 0)
23438 {
23439 help = Fget_char_property (make_number (pos),
23440 Qhelp_echo, w->buffer);
23441 if (!NILP (help))
23442 {
23443 charpos = pos;
23444 object = w->buffer;
23445 }
23446 }
23447 }
23448 }
23449 else if (BUFFERP (object)
23450 && charpos >= BEGV
23451 && charpos < ZV)
23452 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23453 object);
23454
23455 if (!NILP (help))
23456 {
23457 help_echo_string = help;
23458 help_echo_window = window;
23459 help_echo_object = object;
23460 help_echo_pos = charpos;
23461 }
23462 }
23463 }
23464
23465 /* Look for a `pointer' property. */
23466 if (NILP (pointer))
23467 {
23468 /* Check overlays first. */
23469 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23470 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23471
23472 if (NILP (pointer))
23473 {
23474 Lisp_Object object = glyph->object;
23475 int charpos = glyph->charpos;
23476
23477 /* Try text properties. */
23478 if (STRINGP (object)
23479 && charpos >= 0
23480 && charpos < SCHARS (object))
23481 {
23482 pointer = Fget_text_property (make_number (charpos),
23483 Qpointer, object);
23484 if (NILP (pointer))
23485 {
23486 /* If the string itself doesn't specify a pointer,
23487 see if the buffer text ``under'' it does. */
23488 struct glyph_row *r
23489 = MATRIX_ROW (w->current_matrix, vpos);
23490 int start = MATRIX_ROW_START_CHARPOS (r);
23491 int pos = string_buffer_position (w, object, start);
23492 if (pos > 0)
23493 pointer = Fget_char_property (make_number (pos),
23494 Qpointer, w->buffer);
23495 }
23496 }
23497 else if (BUFFERP (object)
23498 && charpos >= BEGV
23499 && charpos < ZV)
23500 pointer = Fget_text_property (make_number (charpos),
23501 Qpointer, object);
23502 }
23503 }
23504
23505 BEGV = obegv;
23506 ZV = ozv;
23507 current_buffer = obuf;
23508 }
23509
23510 set_cursor:
23511
23512 define_frame_cursor1 (f, cursor, pointer);
23513 }
23514
23515
23516 /* EXPORT for RIF:
23517 Clear any mouse-face on window W. This function is part of the
23518 redisplay interface, and is called from try_window_id and similar
23519 functions to ensure the mouse-highlight is off. */
23520
23521 void
23522 x_clear_window_mouse_face (w)
23523 struct window *w;
23524 {
23525 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23526 Lisp_Object window;
23527
23528 BLOCK_INPUT;
23529 XSETWINDOW (window, w);
23530 if (EQ (window, dpyinfo->mouse_face_window))
23531 clear_mouse_face (dpyinfo);
23532 UNBLOCK_INPUT;
23533 }
23534
23535
23536 /* EXPORT:
23537 Just discard the mouse face information for frame F, if any.
23538 This is used when the size of F is changed. */
23539
23540 void
23541 cancel_mouse_face (f)
23542 struct frame *f;
23543 {
23544 Lisp_Object window;
23545 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23546
23547 window = dpyinfo->mouse_face_window;
23548 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23549 {
23550 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23551 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23552 dpyinfo->mouse_face_window = Qnil;
23553 }
23554 }
23555
23556
23557 #endif /* HAVE_WINDOW_SYSTEM */
23558
23559 \f
23560 /***********************************************************************
23561 Exposure Events
23562 ***********************************************************************/
23563
23564 #ifdef HAVE_WINDOW_SYSTEM
23565
23566 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23567 which intersects rectangle R. R is in window-relative coordinates. */
23568
23569 static void
23570 expose_area (w, row, r, area)
23571 struct window *w;
23572 struct glyph_row *row;
23573 XRectangle *r;
23574 enum glyph_row_area area;
23575 {
23576 struct glyph *first = row->glyphs[area];
23577 struct glyph *end = row->glyphs[area] + row->used[area];
23578 struct glyph *last;
23579 int first_x, start_x, x;
23580
23581 if (area == TEXT_AREA && row->fill_line_p)
23582 /* If row extends face to end of line write the whole line. */
23583 draw_glyphs (w, 0, row, area,
23584 0, row->used[area],
23585 DRAW_NORMAL_TEXT, 0);
23586 else
23587 {
23588 /* Set START_X to the window-relative start position for drawing glyphs of
23589 AREA. The first glyph of the text area can be partially visible.
23590 The first glyphs of other areas cannot. */
23591 start_x = window_box_left_offset (w, area);
23592 x = start_x;
23593 if (area == TEXT_AREA)
23594 x += row->x;
23595
23596 /* Find the first glyph that must be redrawn. */
23597 while (first < end
23598 && x + first->pixel_width < r->x)
23599 {
23600 x += first->pixel_width;
23601 ++first;
23602 }
23603
23604 /* Find the last one. */
23605 last = first;
23606 first_x = x;
23607 while (last < end
23608 && x < r->x + r->width)
23609 {
23610 x += last->pixel_width;
23611 ++last;
23612 }
23613
23614 /* Repaint. */
23615 if (last > first)
23616 draw_glyphs (w, first_x - start_x, row, area,
23617 first - row->glyphs[area], last - row->glyphs[area],
23618 DRAW_NORMAL_TEXT, 0);
23619 }
23620 }
23621
23622
23623 /* Redraw the parts of the glyph row ROW on window W intersecting
23624 rectangle R. R is in window-relative coordinates. Value is
23625 non-zero if mouse-face was overwritten. */
23626
23627 static int
23628 expose_line (w, row, r)
23629 struct window *w;
23630 struct glyph_row *row;
23631 XRectangle *r;
23632 {
23633 xassert (row->enabled_p);
23634
23635 if (row->mode_line_p || w->pseudo_window_p)
23636 draw_glyphs (w, 0, row, TEXT_AREA,
23637 0, row->used[TEXT_AREA],
23638 DRAW_NORMAL_TEXT, 0);
23639 else
23640 {
23641 if (row->used[LEFT_MARGIN_AREA])
23642 expose_area (w, row, r, LEFT_MARGIN_AREA);
23643 if (row->used[TEXT_AREA])
23644 expose_area (w, row, r, TEXT_AREA);
23645 if (row->used[RIGHT_MARGIN_AREA])
23646 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23647 draw_row_fringe_bitmaps (w, row);
23648 }
23649
23650 return row->mouse_face_p;
23651 }
23652
23653
23654 /* Redraw those parts of glyphs rows during expose event handling that
23655 overlap other rows. Redrawing of an exposed line writes over parts
23656 of lines overlapping that exposed line; this function fixes that.
23657
23658 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23659 row in W's current matrix that is exposed and overlaps other rows.
23660 LAST_OVERLAPPING_ROW is the last such row. */
23661
23662 static void
23663 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23664 struct window *w;
23665 struct glyph_row *first_overlapping_row;
23666 struct glyph_row *last_overlapping_row;
23667 {
23668 struct glyph_row *row;
23669
23670 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23671 if (row->overlapping_p)
23672 {
23673 xassert (row->enabled_p && !row->mode_line_p);
23674
23675 if (row->used[LEFT_MARGIN_AREA])
23676 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23677
23678 if (row->used[TEXT_AREA])
23679 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23680
23681 if (row->used[RIGHT_MARGIN_AREA])
23682 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23683 }
23684 }
23685
23686
23687 /* Return non-zero if W's cursor intersects rectangle R. */
23688
23689 static int
23690 phys_cursor_in_rect_p (w, r)
23691 struct window *w;
23692 XRectangle *r;
23693 {
23694 XRectangle cr, result;
23695 struct glyph *cursor_glyph;
23696
23697 cursor_glyph = get_phys_cursor_glyph (w);
23698 if (cursor_glyph)
23699 {
23700 /* r is relative to W's box, but w->phys_cursor.x is relative
23701 to left edge of W's TEXT area. Adjust it. */
23702 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23703 cr.y = w->phys_cursor.y;
23704 cr.width = cursor_glyph->pixel_width;
23705 cr.height = w->phys_cursor_height;
23706 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23707 I assume the effect is the same -- and this is portable. */
23708 return x_intersect_rectangles (&cr, r, &result);
23709 }
23710 else
23711 return 0;
23712 }
23713
23714
23715 /* EXPORT:
23716 Draw a vertical window border to the right of window W if W doesn't
23717 have vertical scroll bars. */
23718
23719 void
23720 x_draw_vertical_border (w)
23721 struct window *w;
23722 {
23723 /* We could do better, if we knew what type of scroll-bar the adjacent
23724 windows (on either side) have... But we don't :-(
23725 However, I think this works ok. ++KFS 2003-04-25 */
23726
23727 /* Redraw borders between horizontally adjacent windows. Don't
23728 do it for frames with vertical scroll bars because either the
23729 right scroll bar of a window, or the left scroll bar of its
23730 neighbor will suffice as a border. */
23731 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23732 return;
23733
23734 if (!WINDOW_RIGHTMOST_P (w)
23735 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23736 {
23737 int x0, x1, y0, y1;
23738
23739 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23740 y1 -= 1;
23741
23742 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23743 x1 -= 1;
23744
23745 rif->draw_vertical_window_border (w, x1, y0, y1);
23746 }
23747 else if (!WINDOW_LEFTMOST_P (w)
23748 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23749 {
23750 int x0, x1, y0, y1;
23751
23752 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23753 y1 -= 1;
23754
23755 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23756 x0 -= 1;
23757
23758 rif->draw_vertical_window_border (w, x0, y0, y1);
23759 }
23760 }
23761
23762
23763 /* Redraw the part of window W intersection rectangle FR. Pixel
23764 coordinates in FR are frame-relative. Call this function with
23765 input blocked. Value is non-zero if the exposure overwrites
23766 mouse-face. */
23767
23768 static int
23769 expose_window (w, fr)
23770 struct window *w;
23771 XRectangle *fr;
23772 {
23773 struct frame *f = XFRAME (w->frame);
23774 XRectangle wr, r;
23775 int mouse_face_overwritten_p = 0;
23776
23777 /* If window is not yet fully initialized, do nothing. This can
23778 happen when toolkit scroll bars are used and a window is split.
23779 Reconfiguring the scroll bar will generate an expose for a newly
23780 created window. */
23781 if (w->current_matrix == NULL)
23782 return 0;
23783
23784 /* When we're currently updating the window, display and current
23785 matrix usually don't agree. Arrange for a thorough display
23786 later. */
23787 if (w == updated_window)
23788 {
23789 SET_FRAME_GARBAGED (f);
23790 return 0;
23791 }
23792
23793 /* Frame-relative pixel rectangle of W. */
23794 wr.x = WINDOW_LEFT_EDGE_X (w);
23795 wr.y = WINDOW_TOP_EDGE_Y (w);
23796 wr.width = WINDOW_TOTAL_WIDTH (w);
23797 wr.height = WINDOW_TOTAL_HEIGHT (w);
23798
23799 if (x_intersect_rectangles (fr, &wr, &r))
23800 {
23801 int yb = window_text_bottom_y (w);
23802 struct glyph_row *row;
23803 int cursor_cleared_p;
23804 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23805
23806 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23807 r.x, r.y, r.width, r.height));
23808
23809 /* Convert to window coordinates. */
23810 r.x -= WINDOW_LEFT_EDGE_X (w);
23811 r.y -= WINDOW_TOP_EDGE_Y (w);
23812
23813 /* Turn off the cursor. */
23814 if (!w->pseudo_window_p
23815 && phys_cursor_in_rect_p (w, &r))
23816 {
23817 x_clear_cursor (w);
23818 cursor_cleared_p = 1;
23819 }
23820 else
23821 cursor_cleared_p = 0;
23822
23823 /* Update lines intersecting rectangle R. */
23824 first_overlapping_row = last_overlapping_row = NULL;
23825 for (row = w->current_matrix->rows;
23826 row->enabled_p;
23827 ++row)
23828 {
23829 int y0 = row->y;
23830 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23831
23832 if ((y0 >= r.y && y0 < r.y + r.height)
23833 || (y1 > r.y && y1 < r.y + r.height)
23834 || (r.y >= y0 && r.y < y1)
23835 || (r.y + r.height > y0 && r.y + r.height < y1))
23836 {
23837 /* A header line may be overlapping, but there is no need
23838 to fix overlapping areas for them. KFS 2005-02-12 */
23839 if (row->overlapping_p && !row->mode_line_p)
23840 {
23841 if (first_overlapping_row == NULL)
23842 first_overlapping_row = row;
23843 last_overlapping_row = row;
23844 }
23845
23846 if (expose_line (w, row, &r))
23847 mouse_face_overwritten_p = 1;
23848 }
23849
23850 if (y1 >= yb)
23851 break;
23852 }
23853
23854 /* Display the mode line if there is one. */
23855 if (WINDOW_WANTS_MODELINE_P (w)
23856 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23857 row->enabled_p)
23858 && row->y < r.y + r.height)
23859 {
23860 if (expose_line (w, row, &r))
23861 mouse_face_overwritten_p = 1;
23862 }
23863
23864 if (!w->pseudo_window_p)
23865 {
23866 /* Fix the display of overlapping rows. */
23867 if (first_overlapping_row)
23868 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23869
23870 /* Draw border between windows. */
23871 x_draw_vertical_border (w);
23872
23873 /* Turn the cursor on again. */
23874 if (cursor_cleared_p)
23875 update_window_cursor (w, 1);
23876 }
23877 }
23878
23879 return mouse_face_overwritten_p;
23880 }
23881
23882
23883
23884 /* Redraw (parts) of all windows in the window tree rooted at W that
23885 intersect R. R contains frame pixel coordinates. Value is
23886 non-zero if the exposure overwrites mouse-face. */
23887
23888 static int
23889 expose_window_tree (w, r)
23890 struct window *w;
23891 XRectangle *r;
23892 {
23893 struct frame *f = XFRAME (w->frame);
23894 int mouse_face_overwritten_p = 0;
23895
23896 while (w && !FRAME_GARBAGED_P (f))
23897 {
23898 if (!NILP (w->hchild))
23899 mouse_face_overwritten_p
23900 |= expose_window_tree (XWINDOW (w->hchild), r);
23901 else if (!NILP (w->vchild))
23902 mouse_face_overwritten_p
23903 |= expose_window_tree (XWINDOW (w->vchild), r);
23904 else
23905 mouse_face_overwritten_p |= expose_window (w, r);
23906
23907 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23908 }
23909
23910 return mouse_face_overwritten_p;
23911 }
23912
23913
23914 /* EXPORT:
23915 Redisplay an exposed area of frame F. X and Y are the upper-left
23916 corner of the exposed rectangle. W and H are width and height of
23917 the exposed area. All are pixel values. W or H zero means redraw
23918 the entire frame. */
23919
23920 void
23921 expose_frame (f, x, y, w, h)
23922 struct frame *f;
23923 int x, y, w, h;
23924 {
23925 XRectangle r;
23926 int mouse_face_overwritten_p = 0;
23927
23928 TRACE ((stderr, "expose_frame "));
23929
23930 /* No need to redraw if frame will be redrawn soon. */
23931 if (FRAME_GARBAGED_P (f))
23932 {
23933 TRACE ((stderr, " garbaged\n"));
23934 return;
23935 }
23936
23937 /* If basic faces haven't been realized yet, there is no point in
23938 trying to redraw anything. This can happen when we get an expose
23939 event while Emacs is starting, e.g. by moving another window. */
23940 if (FRAME_FACE_CACHE (f) == NULL
23941 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23942 {
23943 TRACE ((stderr, " no faces\n"));
23944 return;
23945 }
23946
23947 if (w == 0 || h == 0)
23948 {
23949 r.x = r.y = 0;
23950 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23951 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23952 }
23953 else
23954 {
23955 r.x = x;
23956 r.y = y;
23957 r.width = w;
23958 r.height = h;
23959 }
23960
23961 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23962 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23963
23964 if (WINDOWP (f->tool_bar_window))
23965 mouse_face_overwritten_p
23966 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23967
23968 #ifdef HAVE_X_WINDOWS
23969 #ifndef MSDOS
23970 #ifndef USE_X_TOOLKIT
23971 if (WINDOWP (f->menu_bar_window))
23972 mouse_face_overwritten_p
23973 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23974 #endif /* not USE_X_TOOLKIT */
23975 #endif
23976 #endif
23977
23978 /* Some window managers support a focus-follows-mouse style with
23979 delayed raising of frames. Imagine a partially obscured frame,
23980 and moving the mouse into partially obscured mouse-face on that
23981 frame. The visible part of the mouse-face will be highlighted,
23982 then the WM raises the obscured frame. With at least one WM, KDE
23983 2.1, Emacs is not getting any event for the raising of the frame
23984 (even tried with SubstructureRedirectMask), only Expose events.
23985 These expose events will draw text normally, i.e. not
23986 highlighted. Which means we must redo the highlight here.
23987 Subsume it under ``we love X''. --gerd 2001-08-15 */
23988 /* Included in Windows version because Windows most likely does not
23989 do the right thing if any third party tool offers
23990 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23991 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23992 {
23993 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23994 if (f == dpyinfo->mouse_face_mouse_frame)
23995 {
23996 int x = dpyinfo->mouse_face_mouse_x;
23997 int y = dpyinfo->mouse_face_mouse_y;
23998 clear_mouse_face (dpyinfo);
23999 note_mouse_highlight (f, x, y);
24000 }
24001 }
24002 }
24003
24004
24005 /* EXPORT:
24006 Determine the intersection of two rectangles R1 and R2. Return
24007 the intersection in *RESULT. Value is non-zero if RESULT is not
24008 empty. */
24009
24010 int
24011 x_intersect_rectangles (r1, r2, result)
24012 XRectangle *r1, *r2, *result;
24013 {
24014 XRectangle *left, *right;
24015 XRectangle *upper, *lower;
24016 int intersection_p = 0;
24017
24018 /* Rearrange so that R1 is the left-most rectangle. */
24019 if (r1->x < r2->x)
24020 left = r1, right = r2;
24021 else
24022 left = r2, right = r1;
24023
24024 /* X0 of the intersection is right.x0, if this is inside R1,
24025 otherwise there is no intersection. */
24026 if (right->x <= left->x + left->width)
24027 {
24028 result->x = right->x;
24029
24030 /* The right end of the intersection is the minimum of the
24031 the right ends of left and right. */
24032 result->width = (min (left->x + left->width, right->x + right->width)
24033 - result->x);
24034
24035 /* Same game for Y. */
24036 if (r1->y < r2->y)
24037 upper = r1, lower = r2;
24038 else
24039 upper = r2, lower = r1;
24040
24041 /* The upper end of the intersection is lower.y0, if this is inside
24042 of upper. Otherwise, there is no intersection. */
24043 if (lower->y <= upper->y + upper->height)
24044 {
24045 result->y = lower->y;
24046
24047 /* The lower end of the intersection is the minimum of the lower
24048 ends of upper and lower. */
24049 result->height = (min (lower->y + lower->height,
24050 upper->y + upper->height)
24051 - result->y);
24052 intersection_p = 1;
24053 }
24054 }
24055
24056 return intersection_p;
24057 }
24058
24059 #endif /* HAVE_WINDOW_SYSTEM */
24060
24061 \f
24062 /***********************************************************************
24063 Initialization
24064 ***********************************************************************/
24065
24066 void
24067 syms_of_xdisp ()
24068 {
24069 Vwith_echo_area_save_vector = Qnil;
24070 staticpro (&Vwith_echo_area_save_vector);
24071
24072 Vmessage_stack = Qnil;
24073 staticpro (&Vmessage_stack);
24074
24075 Qinhibit_redisplay = intern ("inhibit-redisplay");
24076 staticpro (&Qinhibit_redisplay);
24077
24078 message_dolog_marker1 = Fmake_marker ();
24079 staticpro (&message_dolog_marker1);
24080 message_dolog_marker2 = Fmake_marker ();
24081 staticpro (&message_dolog_marker2);
24082 message_dolog_marker3 = Fmake_marker ();
24083 staticpro (&message_dolog_marker3);
24084
24085 #if GLYPH_DEBUG
24086 defsubr (&Sdump_frame_glyph_matrix);
24087 defsubr (&Sdump_glyph_matrix);
24088 defsubr (&Sdump_glyph_row);
24089 defsubr (&Sdump_tool_bar_row);
24090 defsubr (&Strace_redisplay);
24091 defsubr (&Strace_to_stderr);
24092 #endif
24093 #ifdef HAVE_WINDOW_SYSTEM
24094 defsubr (&Stool_bar_lines_needed);
24095 defsubr (&Slookup_image_map);
24096 #endif
24097 defsubr (&Sformat_mode_line);
24098
24099 staticpro (&Qmenu_bar_update_hook);
24100 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24101
24102 staticpro (&Qoverriding_terminal_local_map);
24103 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24104
24105 staticpro (&Qoverriding_local_map);
24106 Qoverriding_local_map = intern ("overriding-local-map");
24107
24108 staticpro (&Qwindow_scroll_functions);
24109 Qwindow_scroll_functions = intern ("window-scroll-functions");
24110
24111 staticpro (&Qredisplay_end_trigger_functions);
24112 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24113
24114 staticpro (&Qinhibit_point_motion_hooks);
24115 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24116
24117 QCdata = intern (":data");
24118 staticpro (&QCdata);
24119 Qdisplay = intern ("display");
24120 staticpro (&Qdisplay);
24121 Qspace_width = intern ("space-width");
24122 staticpro (&Qspace_width);
24123 Qraise = intern ("raise");
24124 staticpro (&Qraise);
24125 Qslice = intern ("slice");
24126 staticpro (&Qslice);
24127 Qspace = intern ("space");
24128 staticpro (&Qspace);
24129 Qmargin = intern ("margin");
24130 staticpro (&Qmargin);
24131 Qpointer = intern ("pointer");
24132 staticpro (&Qpointer);
24133 Qleft_margin = intern ("left-margin");
24134 staticpro (&Qleft_margin);
24135 Qright_margin = intern ("right-margin");
24136 staticpro (&Qright_margin);
24137 Qcenter = intern ("center");
24138 staticpro (&Qcenter);
24139 Qline_height = intern ("line-height");
24140 staticpro (&Qline_height);
24141 QCalign_to = intern (":align-to");
24142 staticpro (&QCalign_to);
24143 QCrelative_width = intern (":relative-width");
24144 staticpro (&QCrelative_width);
24145 QCrelative_height = intern (":relative-height");
24146 staticpro (&QCrelative_height);
24147 QCeval = intern (":eval");
24148 staticpro (&QCeval);
24149 QCpropertize = intern (":propertize");
24150 staticpro (&QCpropertize);
24151 QCfile = intern (":file");
24152 staticpro (&QCfile);
24153 Qfontified = intern ("fontified");
24154 staticpro (&Qfontified);
24155 Qfontification_functions = intern ("fontification-functions");
24156 staticpro (&Qfontification_functions);
24157 Qtrailing_whitespace = intern ("trailing-whitespace");
24158 staticpro (&Qtrailing_whitespace);
24159 Qescape_glyph = intern ("escape-glyph");
24160 staticpro (&Qescape_glyph);
24161 Qnobreak_space = intern ("nobreak-space");
24162 staticpro (&Qnobreak_space);
24163 Qimage = intern ("image");
24164 staticpro (&Qimage);
24165 QCmap = intern (":map");
24166 staticpro (&QCmap);
24167 QCpointer = intern (":pointer");
24168 staticpro (&QCpointer);
24169 Qrect = intern ("rect");
24170 staticpro (&Qrect);
24171 Qcircle = intern ("circle");
24172 staticpro (&Qcircle);
24173 Qpoly = intern ("poly");
24174 staticpro (&Qpoly);
24175 Qmessage_truncate_lines = intern ("message-truncate-lines");
24176 staticpro (&Qmessage_truncate_lines);
24177 Qgrow_only = intern ("grow-only");
24178 staticpro (&Qgrow_only);
24179 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24180 staticpro (&Qinhibit_menubar_update);
24181 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24182 staticpro (&Qinhibit_eval_during_redisplay);
24183 Qposition = intern ("position");
24184 staticpro (&Qposition);
24185 Qbuffer_position = intern ("buffer-position");
24186 staticpro (&Qbuffer_position);
24187 Qobject = intern ("object");
24188 staticpro (&Qobject);
24189 Qbar = intern ("bar");
24190 staticpro (&Qbar);
24191 Qhbar = intern ("hbar");
24192 staticpro (&Qhbar);
24193 Qbox = intern ("box");
24194 staticpro (&Qbox);
24195 Qhollow = intern ("hollow");
24196 staticpro (&Qhollow);
24197 Qhand = intern ("hand");
24198 staticpro (&Qhand);
24199 Qarrow = intern ("arrow");
24200 staticpro (&Qarrow);
24201 Qtext = intern ("text");
24202 staticpro (&Qtext);
24203 Qrisky_local_variable = intern ("risky-local-variable");
24204 staticpro (&Qrisky_local_variable);
24205 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24206 staticpro (&Qinhibit_free_realized_faces);
24207
24208 list_of_error = Fcons (Fcons (intern ("error"),
24209 Fcons (intern ("void-variable"), Qnil)),
24210 Qnil);
24211 staticpro (&list_of_error);
24212
24213 Qlast_arrow_position = intern ("last-arrow-position");
24214 staticpro (&Qlast_arrow_position);
24215 Qlast_arrow_string = intern ("last-arrow-string");
24216 staticpro (&Qlast_arrow_string);
24217
24218 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24219 staticpro (&Qoverlay_arrow_string);
24220 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24221 staticpro (&Qoverlay_arrow_bitmap);
24222
24223 echo_buffer[0] = echo_buffer[1] = Qnil;
24224 staticpro (&echo_buffer[0]);
24225 staticpro (&echo_buffer[1]);
24226
24227 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24228 staticpro (&echo_area_buffer[0]);
24229 staticpro (&echo_area_buffer[1]);
24230
24231 Vmessages_buffer_name = build_string ("*Messages*");
24232 staticpro (&Vmessages_buffer_name);
24233
24234 mode_line_proptrans_alist = Qnil;
24235 staticpro (&mode_line_proptrans_alist);
24236 mode_line_string_list = Qnil;
24237 staticpro (&mode_line_string_list);
24238 mode_line_string_face = Qnil;
24239 staticpro (&mode_line_string_face);
24240 mode_line_string_face_prop = Qnil;
24241 staticpro (&mode_line_string_face_prop);
24242 Vmode_line_unwind_vector = Qnil;
24243 staticpro (&Vmode_line_unwind_vector);
24244
24245 help_echo_string = Qnil;
24246 staticpro (&help_echo_string);
24247 help_echo_object = Qnil;
24248 staticpro (&help_echo_object);
24249 help_echo_window = Qnil;
24250 staticpro (&help_echo_window);
24251 previous_help_echo_string = Qnil;
24252 staticpro (&previous_help_echo_string);
24253 help_echo_pos = -1;
24254
24255 #ifdef HAVE_WINDOW_SYSTEM
24256 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24257 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24258 For example, if a block cursor is over a tab, it will be drawn as
24259 wide as that tab on the display. */);
24260 x_stretch_cursor_p = 0;
24261 #endif
24262
24263 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24264 doc: /* *Non-nil means highlight trailing whitespace.
24265 The face used for trailing whitespace is `trailing-whitespace'. */);
24266 Vshow_trailing_whitespace = Qnil;
24267
24268 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24269 doc: /* *Control highlighting of nobreak space and soft hyphen.
24270 A value of t means highlight the character itself (for nobreak space,
24271 use face `nobreak-space').
24272 A value of nil means no highlighting.
24273 Other values mean display the escape glyph followed by an ordinary
24274 space or ordinary hyphen. */);
24275 Vnobreak_char_display = Qt;
24276
24277 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24278 doc: /* *The pointer shape to show in void text areas.
24279 A value of nil means to show the text pointer. Other options are `arrow',
24280 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24281 Vvoid_text_area_pointer = Qarrow;
24282
24283 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24284 doc: /* Non-nil means don't actually do any redisplay.
24285 This is used for internal purposes. */);
24286 Vinhibit_redisplay = Qnil;
24287
24288 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24289 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24290 Vglobal_mode_string = Qnil;
24291
24292 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24293 doc: /* Marker for where to display an arrow on top of the buffer text.
24294 This must be the beginning of a line in order to work.
24295 See also `overlay-arrow-string'. */);
24296 Voverlay_arrow_position = Qnil;
24297
24298 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24299 doc: /* String to display as an arrow in non-window frames.
24300 See also `overlay-arrow-position'. */);
24301 Voverlay_arrow_string = build_string ("=>");
24302
24303 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24304 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24305 The symbols on this list are examined during redisplay to determine
24306 where to display overlay arrows. */);
24307 Voverlay_arrow_variable_list
24308 = Fcons (intern ("overlay-arrow-position"), Qnil);
24309
24310 DEFVAR_INT ("scroll-step", &scroll_step,
24311 doc: /* *The number of lines to try scrolling a window by when point moves out.
24312 If that fails to bring point back on frame, point is centered instead.
24313 If this is zero, point is always centered after it moves off frame.
24314 If you want scrolling to always be a line at a time, you should set
24315 `scroll-conservatively' to a large value rather than set this to 1. */);
24316
24317 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24318 doc: /* *Scroll up to this many lines, to bring point back on screen.
24319 A value of zero means to scroll the text to center point vertically
24320 in the window. */);
24321 scroll_conservatively = 0;
24322
24323 DEFVAR_INT ("scroll-margin", &scroll_margin,
24324 doc: /* *Number of lines of margin at the top and bottom of a window.
24325 Recenter the window whenever point gets within this many lines
24326 of the top or bottom of the window. */);
24327 scroll_margin = 0;
24328
24329 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24330 doc: /* Pixels per inch value for non-window system displays.
24331 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24332 Vdisplay_pixels_per_inch = make_float (72.0);
24333
24334 #if GLYPH_DEBUG
24335 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24336 #endif
24337
24338 DEFVAR_BOOL ("truncate-partial-width-windows",
24339 &truncate_partial_width_windows,
24340 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24341 truncate_partial_width_windows = 1;
24342
24343 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24344 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24345 Any other value means to use the appropriate face, `mode-line',
24346 `header-line', or `menu' respectively. */);
24347 mode_line_inverse_video = 1;
24348
24349 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24350 doc: /* *Maximum buffer size for which line number should be displayed.
24351 If the buffer is bigger than this, the line number does not appear
24352 in the mode line. A value of nil means no limit. */);
24353 Vline_number_display_limit = Qnil;
24354
24355 DEFVAR_INT ("line-number-display-limit-width",
24356 &line_number_display_limit_width,
24357 doc: /* *Maximum line width (in characters) for line number display.
24358 If the average length of the lines near point is bigger than this, then the
24359 line number may be omitted from the mode line. */);
24360 line_number_display_limit_width = 200;
24361
24362 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24363 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24364 highlight_nonselected_windows = 0;
24365
24366 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24367 doc: /* Non-nil if more than one frame is visible on this display.
24368 Minibuffer-only frames don't count, but iconified frames do.
24369 This variable is not guaranteed to be accurate except while processing
24370 `frame-title-format' and `icon-title-format'. */);
24371
24372 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24373 doc: /* Template for displaying the title bar of visible frames.
24374 \(Assuming the window manager supports this feature.)
24375
24376 This variable has the same structure as `mode-line-format', except that
24377 the %c and %l constructs are ignored. It is used only on frames for
24378 which no explicit name has been set \(see `modify-frame-parameters'). */);
24379
24380 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24381 doc: /* Template for displaying the title bar of an iconified frame.
24382 \(Assuming the window manager supports this feature.)
24383 This variable has the same structure as `mode-line-format' (which see),
24384 and is used only on frames for which no explicit name has been set
24385 \(see `modify-frame-parameters'). */);
24386 Vicon_title_format
24387 = Vframe_title_format
24388 = Fcons (intern ("multiple-frames"),
24389 Fcons (build_string ("%b"),
24390 Fcons (Fcons (empty_unibyte_string,
24391 Fcons (intern ("invocation-name"),
24392 Fcons (build_string ("@"),
24393 Fcons (intern ("system-name"),
24394 Qnil)))),
24395 Qnil)));
24396
24397 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24398 doc: /* Maximum number of lines to keep in the message log buffer.
24399 If nil, disable message logging. If t, log messages but don't truncate
24400 the buffer when it becomes large. */);
24401 Vmessage_log_max = make_number (100);
24402
24403 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24404 doc: /* Functions called before redisplay, if window sizes have changed.
24405 The value should be a list of functions that take one argument.
24406 Just before redisplay, for each frame, if any of its windows have changed
24407 size since the last redisplay, or have been split or deleted,
24408 all the functions in the list are called, with the frame as argument. */);
24409 Vwindow_size_change_functions = Qnil;
24410
24411 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24412 doc: /* List of functions to call before redisplaying a window with scrolling.
24413 Each function is called with two arguments, the window
24414 and its new display-start position. Note that the value of `window-end'
24415 is not valid when these functions are called. */);
24416 Vwindow_scroll_functions = Qnil;
24417
24418 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24419 doc: /* Functions called when redisplay of a window reaches the end trigger.
24420 Each function is called with two arguments, the window and the end trigger value.
24421 See `set-window-redisplay-end-trigger'. */);
24422 Vredisplay_end_trigger_functions = Qnil;
24423
24424 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24425 doc: /* *Non-nil means autoselect window with mouse pointer.
24426 If nil, do not autoselect windows.
24427 A positive number means delay autoselection by that many seconds: a
24428 window is autoselected only after the mouse has remained in that
24429 window for the duration of the delay.
24430 A negative number has a similar effect, but causes windows to be
24431 autoselected only after the mouse has stopped moving. \(Because of
24432 the way Emacs compares mouse events, you will occasionally wait twice
24433 that time before the window gets selected.\)
24434 Any other value means to autoselect window instantaneously when the
24435 mouse pointer enters it.
24436
24437 Autoselection selects the minibuffer only if it is active, and never
24438 unselects the minibuffer if it is active. */);
24439 Vmouse_autoselect_window = Qnil;
24440
24441 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24442 doc: /* *Non-nil means automatically resize tool-bars.
24443 This dynamically changes the tool-bar's height to the minimum height
24444 that is needed to make all tool-bar items visible.
24445 If value is `grow-only', the tool-bar's height is only increased
24446 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24447 Vauto_resize_tool_bars = Qt;
24448
24449 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24450 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24451 auto_raise_tool_bar_buttons_p = 1;
24452
24453 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24454 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24455 make_cursor_line_fully_visible_p = 1;
24456
24457 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24458 doc: /* *Border below tool-bar in pixels.
24459 If an integer, use it as the height of the border.
24460 If it is one of `internal-border-width' or `border-width', use the
24461 value of the corresponding frame parameter.
24462 Otherwise, no border is added below the tool-bar. */);
24463 Vtool_bar_border = Qinternal_border_width;
24464
24465 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24466 doc: /* *Margin around tool-bar buttons in pixels.
24467 If an integer, use that for both horizontal and vertical margins.
24468 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24469 HORZ specifying the horizontal margin, and VERT specifying the
24470 vertical margin. */);
24471 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24472
24473 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24474 doc: /* *Relief thickness of tool-bar buttons. */);
24475 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24476
24477 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24478 doc: /* List of functions to call to fontify regions of text.
24479 Each function is called with one argument POS. Functions must
24480 fontify a region starting at POS in the current buffer, and give
24481 fontified regions the property `fontified'. */);
24482 Vfontification_functions = Qnil;
24483 Fmake_variable_buffer_local (Qfontification_functions);
24484
24485 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24486 &unibyte_display_via_language_environment,
24487 doc: /* *Non-nil means display unibyte text according to language environment.
24488 Specifically this means that unibyte non-ASCII characters
24489 are displayed by converting them to the equivalent multibyte characters
24490 according to the current language environment. As a result, they are
24491 displayed according to the current fontset. */);
24492 unibyte_display_via_language_environment = 0;
24493
24494 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24495 doc: /* *Maximum height for resizing mini-windows.
24496 If a float, it specifies a fraction of the mini-window frame's height.
24497 If an integer, it specifies a number of lines. */);
24498 Vmax_mini_window_height = make_float (0.25);
24499
24500 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24501 doc: /* *How to resize mini-windows.
24502 A value of nil means don't automatically resize mini-windows.
24503 A value of t means resize them to fit the text displayed in them.
24504 A value of `grow-only', the default, means let mini-windows grow
24505 only, until their display becomes empty, at which point the windows
24506 go back to their normal size. */);
24507 Vresize_mini_windows = Qgrow_only;
24508
24509 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24510 doc: /* Alist specifying how to blink the cursor off.
24511 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24512 `cursor-type' frame-parameter or variable equals ON-STATE,
24513 comparing using `equal', Emacs uses OFF-STATE to specify
24514 how to blink it off. ON-STATE and OFF-STATE are values for
24515 the `cursor-type' frame parameter.
24516
24517 If a frame's ON-STATE has no entry in this list,
24518 the frame's other specifications determine how to blink the cursor off. */);
24519 Vblink_cursor_alist = Qnil;
24520
24521 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24522 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24523 automatic_hscrolling_p = 1;
24524
24525 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24526 doc: /* *How many columns away from the window edge point is allowed to get
24527 before automatic hscrolling will horizontally scroll the window. */);
24528 hscroll_margin = 5;
24529
24530 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24531 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24532 When point is less than `hscroll-margin' columns from the window
24533 edge, automatic hscrolling will scroll the window by the amount of columns
24534 determined by this variable. If its value is a positive integer, scroll that
24535 many columns. If it's a positive floating-point number, it specifies the
24536 fraction of the window's width to scroll. If it's nil or zero, point will be
24537 centered horizontally after the scroll. Any other value, including negative
24538 numbers, are treated as if the value were zero.
24539
24540 Automatic hscrolling always moves point outside the scroll margin, so if
24541 point was more than scroll step columns inside the margin, the window will
24542 scroll more than the value given by the scroll step.
24543
24544 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24545 and `scroll-right' overrides this variable's effect. */);
24546 Vhscroll_step = make_number (0);
24547
24548 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24549 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24550 Bind this around calls to `message' to let it take effect. */);
24551 message_truncate_lines = 0;
24552
24553 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24554 doc: /* Normal hook run to update the menu bar definitions.
24555 Redisplay runs this hook before it redisplays the menu bar.
24556 This is used to update submenus such as Buffers,
24557 whose contents depend on various data. */);
24558 Vmenu_bar_update_hook = Qnil;
24559
24560 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24561 doc: /* Frame for which we are updating a menu.
24562 The enable predicate for a menu binding should check this variable. */);
24563 Vmenu_updating_frame = Qnil;
24564
24565 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24566 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24567 inhibit_menubar_update = 0;
24568
24569 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24570 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24571 inhibit_eval_during_redisplay = 0;
24572
24573 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24574 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24575 inhibit_free_realized_faces = 0;
24576
24577 #if GLYPH_DEBUG
24578 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24579 doc: /* Inhibit try_window_id display optimization. */);
24580 inhibit_try_window_id = 0;
24581
24582 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24583 doc: /* Inhibit try_window_reusing display optimization. */);
24584 inhibit_try_window_reusing = 0;
24585
24586 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24587 doc: /* Inhibit try_cursor_movement display optimization. */);
24588 inhibit_try_cursor_movement = 0;
24589 #endif /* GLYPH_DEBUG */
24590
24591 DEFVAR_INT ("overline-margin", &overline_margin,
24592 doc: /* *Space between overline and text, in pixels.
24593 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24594 margin to the caracter height. */);
24595 overline_margin = 2;
24596 }
24597
24598
24599 /* Initialize this module when Emacs starts. */
24600
24601 void
24602 init_xdisp ()
24603 {
24604 Lisp_Object root_window;
24605 struct window *mini_w;
24606
24607 current_header_line_height = current_mode_line_height = -1;
24608
24609 CHARPOS (this_line_start_pos) = 0;
24610
24611 mini_w = XWINDOW (minibuf_window);
24612 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24613
24614 if (!noninteractive)
24615 {
24616 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24617 int i;
24618
24619 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24620 set_window_height (root_window,
24621 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24622 0);
24623 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24624 set_window_height (minibuf_window, 1, 0);
24625
24626 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24627 mini_w->total_cols = make_number (FRAME_COLS (f));
24628
24629 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24630 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24631 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24632
24633 /* The default ellipsis glyphs `...'. */
24634 for (i = 0; i < 3; ++i)
24635 default_invis_vector[i] = make_number ('.');
24636 }
24637
24638 {
24639 /* Allocate the buffer for frame titles.
24640 Also used for `format-mode-line'. */
24641 int size = 100;
24642 mode_line_noprop_buf = (char *) xmalloc (size);
24643 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24644 mode_line_noprop_ptr = mode_line_noprop_buf;
24645 mode_line_target = MODE_LINE_DISPLAY;
24646 }
24647
24648 help_echo_showing_p = 0;
24649 }
24650
24651
24652 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24653 (do not change this comment) */