Merged in changes from CVS HEAD
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201
202 Cursor No_Cursor;
203 #endif
204
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
208
209 #define INFINITY 10000000
210
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
216
217 extern int interrupt_input;
218 extern int command_loop_level;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Margin around tool bar buttons in pixels. */
268
269 Lisp_Object Vtool_bar_button_margin;
270
271 /* Thickness of shadow to draw around tool bar buttons. */
272
273 EMACS_INT tool_bar_button_relief;
274
275 /* Non-zero means automatically resize tool-bars so that all tool-bar
276 items are visible, and no blank lines remain. */
277
278 int auto_resize_tool_bars_p;
279
280 /* Non-zero means draw block and hollow cursor as wide as the glyph
281 under it. For example, if a block cursor is over a tab, it will be
282 drawn as wide as that tab on the display. */
283
284 int x_stretch_cursor_p;
285
286 /* Non-nil means don't actually do any redisplay. */
287
288 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
289
290 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
291
292 int inhibit_eval_during_redisplay;
293
294 /* Names of text properties relevant for redisplay. */
295
296 Lisp_Object Qdisplay;
297 extern Lisp_Object Qface, Qinvisible, Qwidth;
298
299 /* Symbols used in text property values. */
300
301 Lisp_Object Vdisplay_pixels_per_inch;
302 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
303 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
304 Lisp_Object Qmargin, Qpointer;
305 extern Lisp_Object Qheight;
306 extern Lisp_Object QCwidth, QCheight, QCascent;
307 extern Lisp_Object Qscroll_bar;
308
309 /* Non-nil means highlight trailing whitespace. */
310
311 Lisp_Object Vshow_trailing_whitespace;
312
313 /* Non-nil means show the text cursor in void text areas
314 i.e. in blank areas after eol and eob. This used to be
315 the default in 21.3. */
316
317 Lisp_Object Vvoid_text_area_pointer;
318
319 /* Name of the face used to highlight trailing whitespace. */
320
321 Lisp_Object Qtrailing_whitespace;
322
323 /* The symbol `image' which is the car of the lists used to represent
324 images in Lisp. */
325
326 Lisp_Object Qimage;
327
328 /* The image map types. */
329 Lisp_Object QCmap, QCpointer;
330 Lisp_Object Qrect, Qcircle, Qpoly;
331
332 /* Non-zero means print newline to stdout before next mini-buffer
333 message. */
334
335 int noninteractive_need_newline;
336
337 /* Non-zero means print newline to message log before next message. */
338
339 static int message_log_need_newline;
340
341 /* Three markers that message_dolog uses.
342 It could allocate them itself, but that causes trouble
343 in handling memory-full errors. */
344 static Lisp_Object message_dolog_marker1;
345 static Lisp_Object message_dolog_marker2;
346 static Lisp_Object message_dolog_marker3;
347 \f
348 /* The buffer position of the first character appearing entirely or
349 partially on the line of the selected window which contains the
350 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
351 redisplay optimization in redisplay_internal. */
352
353 static struct text_pos this_line_start_pos;
354
355 /* Number of characters past the end of the line above, including the
356 terminating newline. */
357
358 static struct text_pos this_line_end_pos;
359
360 /* The vertical positions and the height of this line. */
361
362 static int this_line_vpos;
363 static int this_line_y;
364 static int this_line_pixel_height;
365
366 /* X position at which this display line starts. Usually zero;
367 negative if first character is partially visible. */
368
369 static int this_line_start_x;
370
371 /* Buffer that this_line_.* variables are referring to. */
372
373 static struct buffer *this_line_buffer;
374
375 /* Nonzero means truncate lines in all windows less wide than the
376 frame. */
377
378 int truncate_partial_width_windows;
379
380 /* A flag to control how to display unibyte 8-bit character. */
381
382 int unibyte_display_via_language_environment;
383
384 /* Nonzero means we have more than one non-mini-buffer-only frame.
385 Not guaranteed to be accurate except while parsing
386 frame-title-format. */
387
388 int multiple_frames;
389
390 Lisp_Object Vglobal_mode_string;
391
392 /* Marker for where to display an arrow on top of the buffer text. */
393
394 Lisp_Object Voverlay_arrow_position;
395
396 /* String to display for the arrow. Only used on terminal frames. */
397
398 Lisp_Object Voverlay_arrow_string;
399
400 /* Values of those variables at last redisplay. However, if
401 Voverlay_arrow_position is a marker, last_arrow_position is its
402 numerical position. */
403
404 static Lisp_Object last_arrow_position, last_arrow_string;
405
406 /* Like mode-line-format, but for the title bar on a visible frame. */
407
408 Lisp_Object Vframe_title_format;
409
410 /* Like mode-line-format, but for the title bar on an iconified frame. */
411
412 Lisp_Object Vicon_title_format;
413
414 /* List of functions to call when a window's size changes. These
415 functions get one arg, a frame on which one or more windows' sizes
416 have changed. */
417
418 static Lisp_Object Vwindow_size_change_functions;
419
420 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
421
422 /* Nonzero if overlay arrow has been displayed once in this window. */
423
424 static int overlay_arrow_seen;
425
426 /* Nonzero means highlight the region even in nonselected windows. */
427
428 int highlight_nonselected_windows;
429
430 /* If cursor motion alone moves point off frame, try scrolling this
431 many lines up or down if that will bring it back. */
432
433 static EMACS_INT scroll_step;
434
435 /* Nonzero means scroll just far enough to bring point back on the
436 screen, when appropriate. */
437
438 static EMACS_INT scroll_conservatively;
439
440 /* Recenter the window whenever point gets within this many lines of
441 the top or bottom of the window. This value is translated into a
442 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
443 that there is really a fixed pixel height scroll margin. */
444
445 EMACS_INT scroll_margin;
446
447 /* Number of windows showing the buffer of the selected window (or
448 another buffer with the same base buffer). keyboard.c refers to
449 this. */
450
451 int buffer_shared;
452
453 /* Vector containing glyphs for an ellipsis `...'. */
454
455 static Lisp_Object default_invis_vector[3];
456
457 /* Zero means display the mode-line/header-line/menu-bar in the default face
458 (this slightly odd definition is for compatibility with previous versions
459 of emacs), non-zero means display them using their respective faces.
460
461 This variable is deprecated. */
462
463 int mode_line_inverse_video;
464
465 /* Prompt to display in front of the mini-buffer contents. */
466
467 Lisp_Object minibuf_prompt;
468
469 /* Width of current mini-buffer prompt. Only set after display_line
470 of the line that contains the prompt. */
471
472 int minibuf_prompt_width;
473
474 /* This is the window where the echo area message was displayed. It
475 is always a mini-buffer window, but it may not be the same window
476 currently active as a mini-buffer. */
477
478 Lisp_Object echo_area_window;
479
480 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
481 pushes the current message and the value of
482 message_enable_multibyte on the stack, the function restore_message
483 pops the stack and displays MESSAGE again. */
484
485 Lisp_Object Vmessage_stack;
486
487 /* Nonzero means multibyte characters were enabled when the echo area
488 message was specified. */
489
490 int message_enable_multibyte;
491
492 /* Nonzero if we should redraw the mode lines on the next redisplay. */
493
494 int update_mode_lines;
495
496 /* Nonzero if window sizes or contents have changed since last
497 redisplay that finished. */
498
499 int windows_or_buffers_changed;
500
501 /* Nonzero means a frame's cursor type has been changed. */
502
503 int cursor_type_changed;
504
505 /* Nonzero after display_mode_line if %l was used and it displayed a
506 line number. */
507
508 int line_number_displayed;
509
510 /* Maximum buffer size for which to display line numbers. */
511
512 Lisp_Object Vline_number_display_limit;
513
514 /* Line width to consider when repositioning for line number display. */
515
516 static EMACS_INT line_number_display_limit_width;
517
518 /* Number of lines to keep in the message log buffer. t means
519 infinite. nil means don't log at all. */
520
521 Lisp_Object Vmessage_log_max;
522
523 /* The name of the *Messages* buffer, a string. */
524
525 static Lisp_Object Vmessages_buffer_name;
526
527 /* Current, index 0, and last displayed echo area message. Either
528 buffers from echo_buffers, or nil to indicate no message. */
529
530 Lisp_Object echo_area_buffer[2];
531
532 /* The buffers referenced from echo_area_buffer. */
533
534 static Lisp_Object echo_buffer[2];
535
536 /* A vector saved used in with_area_buffer to reduce consing. */
537
538 static Lisp_Object Vwith_echo_area_save_vector;
539
540 /* Non-zero means display_echo_area should display the last echo area
541 message again. Set by redisplay_preserve_echo_area. */
542
543 static int display_last_displayed_message_p;
544
545 /* Nonzero if echo area is being used by print; zero if being used by
546 message. */
547
548 int message_buf_print;
549
550 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
551
552 Lisp_Object Qinhibit_menubar_update;
553 int inhibit_menubar_update;
554
555 /* Maximum height for resizing mini-windows. Either a float
556 specifying a fraction of the available height, or an integer
557 specifying a number of lines. */
558
559 Lisp_Object Vmax_mini_window_height;
560
561 /* Non-zero means messages should be displayed with truncated
562 lines instead of being continued. */
563
564 int message_truncate_lines;
565 Lisp_Object Qmessage_truncate_lines;
566
567 /* Set to 1 in clear_message to make redisplay_internal aware
568 of an emptied echo area. */
569
570 static int message_cleared_p;
571
572 /* Non-zero means we want a hollow cursor in windows that are not
573 selected. Zero means there's no cursor in such windows. */
574
575 Lisp_Object Vcursor_in_non_selected_windows;
576 Lisp_Object Qcursor_in_non_selected_windows;
577
578 /* How to blink the default frame cursor off. */
579 Lisp_Object Vblink_cursor_alist;
580
581 /* A scratch glyph row with contents used for generating truncation
582 glyphs. Also used in direct_output_for_insert. */
583
584 #define MAX_SCRATCH_GLYPHS 100
585 struct glyph_row scratch_glyph_row;
586 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
587
588 /* Ascent and height of the last line processed by move_it_to. */
589
590 static int last_max_ascent, last_height;
591
592 /* Non-zero if there's a help-echo in the echo area. */
593
594 int help_echo_showing_p;
595
596 /* If >= 0, computed, exact values of mode-line and header-line height
597 to use in the macros CURRENT_MODE_LINE_HEIGHT and
598 CURRENT_HEADER_LINE_HEIGHT. */
599
600 int current_mode_line_height, current_header_line_height;
601
602 /* The maximum distance to look ahead for text properties. Values
603 that are too small let us call compute_char_face and similar
604 functions too often which is expensive. Values that are too large
605 let us call compute_char_face and alike too often because we
606 might not be interested in text properties that far away. */
607
608 #define TEXT_PROP_DISTANCE_LIMIT 100
609
610 #if GLYPH_DEBUG
611
612 /* Variables to turn off display optimizations from Lisp. */
613
614 int inhibit_try_window_id, inhibit_try_window_reusing;
615 int inhibit_try_cursor_movement;
616
617 /* Non-zero means print traces of redisplay if compiled with
618 GLYPH_DEBUG != 0. */
619
620 int trace_redisplay_p;
621
622 #endif /* GLYPH_DEBUG */
623
624 #ifdef DEBUG_TRACE_MOVE
625 /* Non-zero means trace with TRACE_MOVE to stderr. */
626 int trace_move;
627
628 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
629 #else
630 #define TRACE_MOVE(x) (void) 0
631 #endif
632
633 /* Non-zero means automatically scroll windows horizontally to make
634 point visible. */
635
636 int automatic_hscrolling_p;
637
638 /* How close to the margin can point get before the window is scrolled
639 horizontally. */
640 EMACS_INT hscroll_margin;
641
642 /* How much to scroll horizontally when point is inside the above margin. */
643 Lisp_Object Vhscroll_step;
644
645 /* A list of symbols, one for each supported image type. */
646
647 Lisp_Object Vimage_types;
648
649 /* The variable `resize-mini-windows'. If nil, don't resize
650 mini-windows. If t, always resize them to fit the text they
651 display. If `grow-only', let mini-windows grow only until they
652 become empty. */
653
654 Lisp_Object Vresize_mini_windows;
655
656 /* Buffer being redisplayed -- for redisplay_window_error. */
657
658 struct buffer *displayed_buffer;
659
660 /* Value returned from text property handlers (see below). */
661
662 enum prop_handled
663 {
664 HANDLED_NORMALLY,
665 HANDLED_RECOMPUTE_PROPS,
666 HANDLED_OVERLAY_STRING_CONSUMED,
667 HANDLED_RETURN
668 };
669
670 /* A description of text properties that redisplay is interested
671 in. */
672
673 struct props
674 {
675 /* The name of the property. */
676 Lisp_Object *name;
677
678 /* A unique index for the property. */
679 enum prop_idx idx;
680
681 /* A handler function called to set up iterator IT from the property
682 at IT's current position. Value is used to steer handle_stop. */
683 enum prop_handled (*handler) P_ ((struct it *it));
684 };
685
686 static enum prop_handled handle_face_prop P_ ((struct it *));
687 static enum prop_handled handle_invisible_prop P_ ((struct it *));
688 static enum prop_handled handle_display_prop P_ ((struct it *));
689 static enum prop_handled handle_composition_prop P_ ((struct it *));
690 static enum prop_handled handle_overlay_change P_ ((struct it *));
691 static enum prop_handled handle_fontified_prop P_ ((struct it *));
692
693 /* Properties handled by iterators. */
694
695 static struct props it_props[] =
696 {
697 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
698 /* Handle `face' before `display' because some sub-properties of
699 `display' need to know the face. */
700 {&Qface, FACE_PROP_IDX, handle_face_prop},
701 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
702 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
703 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
704 {NULL, 0, NULL}
705 };
706
707 /* Value is the position described by X. If X is a marker, value is
708 the marker_position of X. Otherwise, value is X. */
709
710 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
711
712 /* Enumeration returned by some move_it_.* functions internally. */
713
714 enum move_it_result
715 {
716 /* Not used. Undefined value. */
717 MOVE_UNDEFINED,
718
719 /* Move ended at the requested buffer position or ZV. */
720 MOVE_POS_MATCH_OR_ZV,
721
722 /* Move ended at the requested X pixel position. */
723 MOVE_X_REACHED,
724
725 /* Move within a line ended at the end of a line that must be
726 continued. */
727 MOVE_LINE_CONTINUED,
728
729 /* Move within a line ended at the end of a line that would
730 be displayed truncated. */
731 MOVE_LINE_TRUNCATED,
732
733 /* Move within a line ended at a line end. */
734 MOVE_NEWLINE_OR_CR
735 };
736
737 /* This counter is used to clear the face cache every once in a while
738 in redisplay_internal. It is incremented for each redisplay.
739 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
740 cleared. */
741
742 #define CLEAR_FACE_CACHE_COUNT 500
743 static int clear_face_cache_count;
744
745 /* Non-zero while redisplay_internal is in progress. */
746
747 int redisplaying_p;
748
749 /* Non-zero means don't free realized faces. Bound while freeing
750 realized faces is dangerous because glyph matrices might still
751 reference them. */
752
753 int inhibit_free_realized_faces;
754 Lisp_Object Qinhibit_free_realized_faces;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 int help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768
769 \f
770 /* Function prototypes. */
771
772 static void setup_for_ellipsis P_ ((struct it *));
773 static void mark_window_display_accurate_1 P_ ((struct window *, int));
774 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
775 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
776 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
777 static int redisplay_mode_lines P_ ((Lisp_Object, int));
778 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
779
780 #if 0
781 static int invisible_text_between_p P_ ((struct it *, int, int));
782 #endif
783
784 static int next_element_from_ellipsis P_ ((struct it *));
785 static void pint2str P_ ((char *, int, int));
786 static void pint2hrstr P_ ((char *, int, int));
787 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
788 struct text_pos));
789 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
790 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
791 static void store_frame_title_char P_ ((char));
792 static int store_frame_title P_ ((const unsigned char *, int, int));
793 static void x_consider_frame_title P_ ((Lisp_Object));
794 static void handle_stop P_ ((struct it *));
795 static int tool_bar_lines_needed P_ ((struct frame *));
796 static int single_display_prop_intangible_p P_ ((Lisp_Object));
797 static void ensure_echo_area_buffers P_ ((void));
798 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
799 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
800 static int with_echo_area_buffer P_ ((struct window *, int,
801 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
802 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
803 static void clear_garbaged_frames P_ ((void));
804 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
805 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
806 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
807 static int display_echo_area P_ ((struct window *));
808 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
809 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
810 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
811 static int string_char_and_length P_ ((const unsigned char *, int, int *));
812 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
813 struct text_pos));
814 static int compute_window_start_on_continuation_line P_ ((struct window *));
815 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
816 static void insert_left_trunc_glyphs P_ ((struct it *));
817 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
818 static void extend_face_to_end_of_line P_ ((struct it *));
819 static int append_space P_ ((struct it *, int));
820 static int make_cursor_line_fully_visible P_ ((struct window *));
821 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
822 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
823 static int trailing_whitespace_p P_ ((int));
824 static int message_log_check_duplicate P_ ((int, int, int, int));
825 static void push_it P_ ((struct it *));
826 static void pop_it P_ ((struct it *));
827 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
828 static void select_frame_for_redisplay P_ ((Lisp_Object));
829 static void redisplay_internal P_ ((int));
830 static int echo_area_display P_ ((int));
831 static void redisplay_windows P_ ((Lisp_Object));
832 static void redisplay_window P_ ((Lisp_Object, int));
833 static Lisp_Object redisplay_window_error ();
834 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
835 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
836 static void update_menu_bar P_ ((struct frame *, int));
837 static int try_window_reusing_current_matrix P_ ((struct window *));
838 static int try_window_id P_ ((struct window *));
839 static int display_line P_ ((struct it *));
840 static int display_mode_lines P_ ((struct window *));
841 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
842 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
843 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
844 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
845 static void display_menu_bar P_ ((struct window *));
846 static int display_count_lines P_ ((int, int, int, int, int *));
847 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
848 int, int, struct it *, int, int, int, int));
849 static void compute_line_metrics P_ ((struct it *));
850 static void run_redisplay_end_trigger_hook P_ ((struct it *));
851 static int get_overlay_strings P_ ((struct it *, int));
852 static void next_overlay_string P_ ((struct it *));
853 static void reseat P_ ((struct it *, struct text_pos, int));
854 static void reseat_1 P_ ((struct it *, struct text_pos, int));
855 static void back_to_previous_visible_line_start P_ ((struct it *));
856 static void reseat_at_previous_visible_line_start P_ ((struct it *));
857 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
858 static int next_element_from_display_vector P_ ((struct it *));
859 static int next_element_from_string P_ ((struct it *));
860 static int next_element_from_c_string P_ ((struct it *));
861 static int next_element_from_buffer P_ ((struct it *));
862 static int next_element_from_composition P_ ((struct it *));
863 static int next_element_from_image P_ ((struct it *));
864 static int next_element_from_stretch P_ ((struct it *));
865 static void load_overlay_strings P_ ((struct it *, int));
866 static int init_from_display_pos P_ ((struct it *, struct window *,
867 struct display_pos *));
868 static void reseat_to_string P_ ((struct it *, unsigned char *,
869 Lisp_Object, int, int, int, int));
870 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
871 int, int, int));
872 void move_it_vertically_backward P_ ((struct it *, int));
873 static void init_to_row_start P_ ((struct it *, struct window *,
874 struct glyph_row *));
875 static int init_to_row_end P_ ((struct it *, struct window *,
876 struct glyph_row *));
877 static void back_to_previous_line_start P_ ((struct it *));
878 static int forward_to_next_line_start P_ ((struct it *, int *));
879 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
880 Lisp_Object, int));
881 static struct text_pos string_pos P_ ((int, Lisp_Object));
882 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
883 static int number_of_chars P_ ((unsigned char *, int));
884 static void compute_stop_pos P_ ((struct it *));
885 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
886 Lisp_Object));
887 static int face_before_or_after_it_pos P_ ((struct it *, int));
888 static int next_overlay_change P_ ((int));
889 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
890 Lisp_Object, struct text_pos *,
891 int));
892 static int underlying_face_id P_ ((struct it *));
893 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
894 struct window *));
895
896 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
897 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
898
899 #ifdef HAVE_WINDOW_SYSTEM
900
901 static void update_tool_bar P_ ((struct frame *, int));
902 static void build_desired_tool_bar_string P_ ((struct frame *f));
903 static int redisplay_tool_bar P_ ((struct frame *));
904 static void display_tool_bar_line P_ ((struct it *));
905 static void notice_overwritten_cursor P_ ((struct window *,
906 enum glyph_row_area,
907 int, int, int, int));
908
909
910
911 #endif /* HAVE_WINDOW_SYSTEM */
912
913 \f
914 /***********************************************************************
915 Window display dimensions
916 ***********************************************************************/
917
918 /* Return the bottom boundary y-position for text lines in window W.
919 This is the first y position at which a line cannot start.
920 It is relative to the top of the window.
921
922 This is the height of W minus the height of a mode line, if any. */
923
924 INLINE int
925 window_text_bottom_y (w)
926 struct window *w;
927 {
928 int height = WINDOW_TOTAL_HEIGHT (w);
929
930 if (WINDOW_WANTS_MODELINE_P (w))
931 height -= CURRENT_MODE_LINE_HEIGHT (w);
932 return height;
933 }
934
935 /* Return the pixel width of display area AREA of window W. AREA < 0
936 means return the total width of W, not including fringes to
937 the left and right of the window. */
938
939 INLINE int
940 window_box_width (w, area)
941 struct window *w;
942 int area;
943 {
944 int cols = XFASTINT (w->total_cols);
945 int pixels = 0;
946
947 if (!w->pseudo_window_p)
948 {
949 cols -= WINDOW_SCROLL_BAR_COLS (w);
950
951 if (area == TEXT_AREA)
952 {
953 if (INTEGERP (w->left_margin_cols))
954 cols -= XFASTINT (w->left_margin_cols);
955 if (INTEGERP (w->right_margin_cols))
956 cols -= XFASTINT (w->right_margin_cols);
957 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
958 }
959 else if (area == LEFT_MARGIN_AREA)
960 {
961 cols = (INTEGERP (w->left_margin_cols)
962 ? XFASTINT (w->left_margin_cols) : 0);
963 pixels = 0;
964 }
965 else if (area == RIGHT_MARGIN_AREA)
966 {
967 cols = (INTEGERP (w->right_margin_cols)
968 ? XFASTINT (w->right_margin_cols) : 0);
969 pixels = 0;
970 }
971 }
972
973 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
974 }
975
976
977 /* Return the pixel height of the display area of window W, not
978 including mode lines of W, if any. */
979
980 INLINE int
981 window_box_height (w)
982 struct window *w;
983 {
984 struct frame *f = XFRAME (w->frame);
985 int height = WINDOW_TOTAL_HEIGHT (w);
986
987 xassert (height >= 0);
988
989 /* Note: the code below that determines the mode-line/header-line
990 height is essentially the same as that contained in the macro
991 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
992 the appropriate glyph row has its `mode_line_p' flag set,
993 and if it doesn't, uses estimate_mode_line_height instead. */
994
995 if (WINDOW_WANTS_MODELINE_P (w))
996 {
997 struct glyph_row *ml_row
998 = (w->current_matrix && w->current_matrix->rows
999 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1000 : 0);
1001 if (ml_row && ml_row->mode_line_p)
1002 height -= ml_row->height;
1003 else
1004 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1005 }
1006
1007 if (WINDOW_WANTS_HEADER_LINE_P (w))
1008 {
1009 struct glyph_row *hl_row
1010 = (w->current_matrix && w->current_matrix->rows
1011 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1012 : 0);
1013 if (hl_row && hl_row->mode_line_p)
1014 height -= hl_row->height;
1015 else
1016 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1017 }
1018
1019 /* With a very small font and a mode-line that's taller than
1020 default, we might end up with a negative height. */
1021 return max (0, height);
1022 }
1023
1024 /* Return the window-relative coordinate of the left edge of display
1025 area AREA of window W. AREA < 0 means return the left edge of the
1026 whole window, to the right of the left fringe of W. */
1027
1028 INLINE int
1029 window_box_left_offset (w, area)
1030 struct window *w;
1031 int area;
1032 {
1033 int x;
1034
1035 if (w->pseudo_window_p)
1036 return 0;
1037
1038 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1039
1040 if (area == TEXT_AREA)
1041 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1042 + window_box_width (w, LEFT_MARGIN_AREA));
1043 else if (area == RIGHT_MARGIN_AREA)
1044 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1045 + window_box_width (w, LEFT_MARGIN_AREA)
1046 + window_box_width (w, TEXT_AREA)
1047 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1048 ? 0
1049 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1050 else if (area == LEFT_MARGIN_AREA
1051 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1052 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1053
1054 return x;
1055 }
1056
1057
1058 /* Return the window-relative coordinate of the right edge of display
1059 area AREA of window W. AREA < 0 means return the left edge of the
1060 whole window, to the left of the right fringe of W. */
1061
1062 INLINE int
1063 window_box_right_offset (w, area)
1064 struct window *w;
1065 int area;
1066 {
1067 return window_box_left_offset (w, area) + window_box_width (w, area);
1068 }
1069
1070 /* Return the frame-relative coordinate of the left edge of display
1071 area AREA of window W. AREA < 0 means return the left edge of the
1072 whole window, to the right of the left fringe of W. */
1073
1074 INLINE int
1075 window_box_left (w, area)
1076 struct window *w;
1077 int area;
1078 {
1079 struct frame *f = XFRAME (w->frame);
1080 int x;
1081
1082 if (w->pseudo_window_p)
1083 return FRAME_INTERNAL_BORDER_WIDTH (f);
1084
1085 x = (WINDOW_LEFT_EDGE_X (w)
1086 + window_box_left_offset (w, area));
1087
1088 return x;
1089 }
1090
1091
1092 /* Return the frame-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the left edge of the
1094 whole window, to the left of the right fringe of W. */
1095
1096 INLINE int
1097 window_box_right (w, area)
1098 struct window *w;
1099 int area;
1100 {
1101 return window_box_left (w, area) + window_box_width (w, area);
1102 }
1103
1104 /* Get the bounding box of the display area AREA of window W, without
1105 mode lines, in frame-relative coordinates. AREA < 0 means the
1106 whole window, not including the left and right fringes of
1107 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1108 coordinates of the upper-left corner of the box. Return in
1109 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1110
1111 INLINE void
1112 window_box (w, area, box_x, box_y, box_width, box_height)
1113 struct window *w;
1114 int area;
1115 int *box_x, *box_y, *box_width, *box_height;
1116 {
1117 if (box_width)
1118 *box_width = window_box_width (w, area);
1119 if (box_height)
1120 *box_height = window_box_height (w);
1121 if (box_x)
1122 *box_x = window_box_left (w, area);
1123 if (box_y)
1124 {
1125 *box_y = WINDOW_TOP_EDGE_Y (w);
1126 if (WINDOW_WANTS_HEADER_LINE_P (w))
1127 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1128 }
1129 }
1130
1131
1132 /* Get the bounding box of the display area AREA of window W, without
1133 mode lines. AREA < 0 means the whole window, not including the
1134 left and right fringe of the window. Return in *TOP_LEFT_X
1135 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1136 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1137 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1138 box. */
1139
1140 INLINE void
1141 window_box_edges (w, area, top_left_x, top_left_y,
1142 bottom_right_x, bottom_right_y)
1143 struct window *w;
1144 int area;
1145 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1146 {
1147 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1148 bottom_right_y);
1149 *bottom_right_x += *top_left_x;
1150 *bottom_right_y += *top_left_y;
1151 }
1152
1153
1154 \f
1155 /***********************************************************************
1156 Utilities
1157 ***********************************************************************/
1158
1159 /* Return the bottom y-position of the line the iterator IT is in.
1160 This can modify IT's settings. */
1161
1162 int
1163 line_bottom_y (it)
1164 struct it *it;
1165 {
1166 int line_height = it->max_ascent + it->max_descent;
1167 int line_top_y = it->current_y;
1168
1169 if (line_height == 0)
1170 {
1171 if (last_height)
1172 line_height = last_height;
1173 else if (IT_CHARPOS (*it) < ZV)
1174 {
1175 move_it_by_lines (it, 1, 1);
1176 line_height = (it->max_ascent || it->max_descent
1177 ? it->max_ascent + it->max_descent
1178 : last_height);
1179 }
1180 else
1181 {
1182 struct glyph_row *row = it->glyph_row;
1183
1184 /* Use the default character height. */
1185 it->glyph_row = NULL;
1186 it->what = IT_CHARACTER;
1187 it->c = ' ';
1188 it->len = 1;
1189 PRODUCE_GLYPHS (it);
1190 line_height = it->ascent + it->descent;
1191 it->glyph_row = row;
1192 }
1193 }
1194
1195 return line_top_y + line_height;
1196 }
1197
1198
1199 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1200 1 if POS is visible and the line containing POS is fully visible.
1201 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1202 and header-lines heights. */
1203
1204 int
1205 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1206 struct window *w;
1207 int charpos, *fully, exact_mode_line_heights_p;
1208 {
1209 struct it it;
1210 struct text_pos top;
1211 int visible_p;
1212 struct buffer *old_buffer = NULL;
1213
1214 if (XBUFFER (w->buffer) != current_buffer)
1215 {
1216 old_buffer = current_buffer;
1217 set_buffer_internal_1 (XBUFFER (w->buffer));
1218 }
1219
1220 *fully = visible_p = 0;
1221 SET_TEXT_POS_FROM_MARKER (top, w->start);
1222
1223 /* Compute exact mode line heights, if requested. */
1224 if (exact_mode_line_heights_p)
1225 {
1226 if (WINDOW_WANTS_MODELINE_P (w))
1227 current_mode_line_height
1228 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1229 current_buffer->mode_line_format);
1230
1231 if (WINDOW_WANTS_HEADER_LINE_P (w))
1232 current_header_line_height
1233 = display_mode_line (w, HEADER_LINE_FACE_ID,
1234 current_buffer->header_line_format);
1235 }
1236
1237 start_display (&it, w, top);
1238 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1239 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1240
1241 /* Note that we may overshoot because of invisible text. */
1242 if (IT_CHARPOS (it) >= charpos)
1243 {
1244 int top_y = it.current_y;
1245 int bottom_y = line_bottom_y (&it);
1246 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1247
1248 if (top_y < window_top_y)
1249 visible_p = bottom_y > window_top_y;
1250 else if (top_y < it.last_visible_y)
1251 {
1252 visible_p = 1;
1253 *fully = bottom_y <= it.last_visible_y;
1254 }
1255 }
1256 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1257 {
1258 move_it_by_lines (&it, 1, 0);
1259 if (charpos < IT_CHARPOS (it))
1260 {
1261 visible_p = 1;
1262 *fully = 0;
1263 }
1264 }
1265
1266 if (old_buffer)
1267 set_buffer_internal_1 (old_buffer);
1268
1269 current_header_line_height = current_mode_line_height = -1;
1270 return visible_p;
1271 }
1272
1273
1274 /* Return the next character from STR which is MAXLEN bytes long.
1275 Return in *LEN the length of the character. This is like
1276 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1277 we find one, we return a `?', but with the length of the invalid
1278 character. */
1279
1280 static INLINE int
1281 string_char_and_length (str, maxlen, len)
1282 const unsigned char *str;
1283 int maxlen, *len;
1284 {
1285 int c;
1286
1287 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1288 if (!CHAR_VALID_P (c, 1))
1289 /* We may not change the length here because other places in Emacs
1290 don't use this function, i.e. they silently accept invalid
1291 characters. */
1292 c = '?';
1293
1294 return c;
1295 }
1296
1297
1298
1299 /* Given a position POS containing a valid character and byte position
1300 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1301
1302 static struct text_pos
1303 string_pos_nchars_ahead (pos, string, nchars)
1304 struct text_pos pos;
1305 Lisp_Object string;
1306 int nchars;
1307 {
1308 xassert (STRINGP (string) && nchars >= 0);
1309
1310 if (STRING_MULTIBYTE (string))
1311 {
1312 int rest = SBYTES (string) - BYTEPOS (pos);
1313 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1314 int len;
1315
1316 while (nchars--)
1317 {
1318 string_char_and_length (p, rest, &len);
1319 p += len, rest -= len;
1320 xassert (rest >= 0);
1321 CHARPOS (pos) += 1;
1322 BYTEPOS (pos) += len;
1323 }
1324 }
1325 else
1326 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1327
1328 return pos;
1329 }
1330
1331
1332 /* Value is the text position, i.e. character and byte position,
1333 for character position CHARPOS in STRING. */
1334
1335 static INLINE struct text_pos
1336 string_pos (charpos, string)
1337 int charpos;
1338 Lisp_Object string;
1339 {
1340 struct text_pos pos;
1341 xassert (STRINGP (string));
1342 xassert (charpos >= 0);
1343 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1344 return pos;
1345 }
1346
1347
1348 /* Value is a text position, i.e. character and byte position, for
1349 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1350 means recognize multibyte characters. */
1351
1352 static struct text_pos
1353 c_string_pos (charpos, s, multibyte_p)
1354 int charpos;
1355 unsigned char *s;
1356 int multibyte_p;
1357 {
1358 struct text_pos pos;
1359
1360 xassert (s != NULL);
1361 xassert (charpos >= 0);
1362
1363 if (multibyte_p)
1364 {
1365 int rest = strlen (s), len;
1366
1367 SET_TEXT_POS (pos, 0, 0);
1368 while (charpos--)
1369 {
1370 string_char_and_length (s, rest, &len);
1371 s += len, rest -= len;
1372 xassert (rest >= 0);
1373 CHARPOS (pos) += 1;
1374 BYTEPOS (pos) += len;
1375 }
1376 }
1377 else
1378 SET_TEXT_POS (pos, charpos, charpos);
1379
1380 return pos;
1381 }
1382
1383
1384 /* Value is the number of characters in C string S. MULTIBYTE_P
1385 non-zero means recognize multibyte characters. */
1386
1387 static int
1388 number_of_chars (s, multibyte_p)
1389 unsigned char *s;
1390 int multibyte_p;
1391 {
1392 int nchars;
1393
1394 if (multibyte_p)
1395 {
1396 int rest = strlen (s), len;
1397 unsigned char *p = (unsigned char *) s;
1398
1399 for (nchars = 0; rest > 0; ++nchars)
1400 {
1401 string_char_and_length (p, rest, &len);
1402 rest -= len, p += len;
1403 }
1404 }
1405 else
1406 nchars = strlen (s);
1407
1408 return nchars;
1409 }
1410
1411
1412 /* Compute byte position NEWPOS->bytepos corresponding to
1413 NEWPOS->charpos. POS is a known position in string STRING.
1414 NEWPOS->charpos must be >= POS.charpos. */
1415
1416 static void
1417 compute_string_pos (newpos, pos, string)
1418 struct text_pos *newpos, pos;
1419 Lisp_Object string;
1420 {
1421 xassert (STRINGP (string));
1422 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1423
1424 if (STRING_MULTIBYTE (string))
1425 *newpos = string_pos_nchars_ahead (pos, string,
1426 CHARPOS (*newpos) - CHARPOS (pos));
1427 else
1428 BYTEPOS (*newpos) = CHARPOS (*newpos);
1429 }
1430
1431 /* EXPORT:
1432 Return an estimation of the pixel height of mode or top lines on
1433 frame F. FACE_ID specifies what line's height to estimate. */
1434
1435 int
1436 estimate_mode_line_height (f, face_id)
1437 struct frame *f;
1438 enum face_id face_id;
1439 {
1440 #ifdef HAVE_WINDOW_SYSTEM
1441 if (FRAME_WINDOW_P (f))
1442 {
1443 int height = FONT_HEIGHT (FRAME_FONT (f));
1444
1445 /* This function is called so early when Emacs starts that the face
1446 cache and mode line face are not yet initialized. */
1447 if (FRAME_FACE_CACHE (f))
1448 {
1449 struct face *face = FACE_FROM_ID (f, face_id);
1450 if (face)
1451 {
1452 if (face->font)
1453 height = FONT_HEIGHT (face->font);
1454 if (face->box_line_width > 0)
1455 height += 2 * face->box_line_width;
1456 }
1457 }
1458
1459 return height;
1460 }
1461 #endif
1462
1463 return 1;
1464 }
1465
1466 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1467 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1468 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1469 not force the value into range. */
1470
1471 void
1472 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1473 FRAME_PTR f;
1474 register int pix_x, pix_y;
1475 int *x, *y;
1476 NativeRectangle *bounds;
1477 int noclip;
1478 {
1479
1480 #ifdef HAVE_WINDOW_SYSTEM
1481 if (FRAME_WINDOW_P (f))
1482 {
1483 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1484 even for negative values. */
1485 if (pix_x < 0)
1486 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1487 if (pix_y < 0)
1488 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1489
1490 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1491 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1492
1493 if (bounds)
1494 STORE_NATIVE_RECT (*bounds,
1495 FRAME_COL_TO_PIXEL_X (f, pix_x),
1496 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1497 FRAME_COLUMN_WIDTH (f) - 1,
1498 FRAME_LINE_HEIGHT (f) - 1);
1499
1500 if (!noclip)
1501 {
1502 if (pix_x < 0)
1503 pix_x = 0;
1504 else if (pix_x > FRAME_TOTAL_COLS (f))
1505 pix_x = FRAME_TOTAL_COLS (f);
1506
1507 if (pix_y < 0)
1508 pix_y = 0;
1509 else if (pix_y > FRAME_LINES (f))
1510 pix_y = FRAME_LINES (f);
1511 }
1512 }
1513 #endif
1514
1515 *x = pix_x;
1516 *y = pix_y;
1517 }
1518
1519
1520 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1521 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1522 can't tell the positions because W's display is not up to date,
1523 return 0. */
1524
1525 int
1526 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1527 struct window *w;
1528 int hpos, vpos;
1529 int *frame_x, *frame_y;
1530 {
1531 #ifdef HAVE_WINDOW_SYSTEM
1532 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1533 {
1534 int success_p;
1535
1536 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1537 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1538
1539 if (display_completed)
1540 {
1541 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1542 struct glyph *glyph = row->glyphs[TEXT_AREA];
1543 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1544
1545 hpos = row->x;
1546 vpos = row->y;
1547 while (glyph < end)
1548 {
1549 hpos += glyph->pixel_width;
1550 ++glyph;
1551 }
1552
1553 /* If first glyph is partially visible, its first visible position is still 0. */
1554 if (hpos < 0)
1555 hpos = 0;
1556
1557 success_p = 1;
1558 }
1559 else
1560 {
1561 hpos = vpos = 0;
1562 success_p = 0;
1563 }
1564
1565 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1566 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1567 return success_p;
1568 }
1569 #endif
1570
1571 *frame_x = hpos;
1572 *frame_y = vpos;
1573 return 1;
1574 }
1575
1576
1577 #ifdef HAVE_WINDOW_SYSTEM
1578
1579 /* Find the glyph under window-relative coordinates X/Y in window W.
1580 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1581 strings. Return in *HPOS and *VPOS the row and column number of
1582 the glyph found. Return in *AREA the glyph area containing X.
1583 Value is a pointer to the glyph found or null if X/Y is not on
1584 text, or we can't tell because W's current matrix is not up to
1585 date. */
1586
1587 static struct glyph *
1588 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1589 struct window *w;
1590 int x, y;
1591 int *hpos, *vpos, *dx, *dy, *area;
1592 {
1593 struct glyph *glyph, *end;
1594 struct glyph_row *row = NULL;
1595 int x0, i;
1596
1597 /* Find row containing Y. Give up if some row is not enabled. */
1598 for (i = 0; i < w->current_matrix->nrows; ++i)
1599 {
1600 row = MATRIX_ROW (w->current_matrix, i);
1601 if (!row->enabled_p)
1602 return NULL;
1603 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1604 break;
1605 }
1606
1607 *vpos = i;
1608 *hpos = 0;
1609
1610 /* Give up if Y is not in the window. */
1611 if (i == w->current_matrix->nrows)
1612 return NULL;
1613
1614 /* Get the glyph area containing X. */
1615 if (w->pseudo_window_p)
1616 {
1617 *area = TEXT_AREA;
1618 x0 = 0;
1619 }
1620 else
1621 {
1622 if (x < window_box_left_offset (w, TEXT_AREA))
1623 {
1624 *area = LEFT_MARGIN_AREA;
1625 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1626 }
1627 else if (x < window_box_right_offset (w, TEXT_AREA))
1628 {
1629 *area = TEXT_AREA;
1630 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1631 }
1632 else
1633 {
1634 *area = RIGHT_MARGIN_AREA;
1635 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1636 }
1637 }
1638
1639 /* Find glyph containing X. */
1640 glyph = row->glyphs[*area];
1641 end = glyph + row->used[*area];
1642 x -= x0;
1643 while (glyph < end && x >= glyph->pixel_width)
1644 {
1645 x -= glyph->pixel_width;
1646 ++glyph;
1647 }
1648
1649 if (glyph == end)
1650 return NULL;
1651
1652 if (dx)
1653 {
1654 *dx = x;
1655 *dy = y - (row->y + row->ascent - glyph->ascent);
1656 }
1657
1658 *hpos = glyph - row->glyphs[*area];
1659 return glyph;
1660 }
1661
1662
1663 /* EXPORT:
1664 Convert frame-relative x/y to coordinates relative to window W.
1665 Takes pseudo-windows into account. */
1666
1667 void
1668 frame_to_window_pixel_xy (w, x, y)
1669 struct window *w;
1670 int *x, *y;
1671 {
1672 if (w->pseudo_window_p)
1673 {
1674 /* A pseudo-window is always full-width, and starts at the
1675 left edge of the frame, plus a frame border. */
1676 struct frame *f = XFRAME (w->frame);
1677 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1678 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1679 }
1680 else
1681 {
1682 *x -= WINDOW_LEFT_EDGE_X (w);
1683 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1684 }
1685 }
1686
1687 /* EXPORT:
1688 Return in *R the clipping rectangle for glyph string S. */
1689
1690 void
1691 get_glyph_string_clip_rect (s, nr)
1692 struct glyph_string *s;
1693 NativeRectangle *nr;
1694 {
1695 XRectangle r;
1696
1697 if (s->row->full_width_p)
1698 {
1699 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1700 r.x = WINDOW_LEFT_EDGE_X (s->w);
1701 r.width = WINDOW_TOTAL_WIDTH (s->w);
1702
1703 /* Unless displaying a mode or menu bar line, which are always
1704 fully visible, clip to the visible part of the row. */
1705 if (s->w->pseudo_window_p)
1706 r.height = s->row->visible_height;
1707 else
1708 r.height = s->height;
1709 }
1710 else
1711 {
1712 /* This is a text line that may be partially visible. */
1713 r.x = window_box_left (s->w, s->area);
1714 r.width = window_box_width (s->w, s->area);
1715 r.height = s->row->visible_height;
1716 }
1717
1718 /* If S draws overlapping rows, it's sufficient to use the top and
1719 bottom of the window for clipping because this glyph string
1720 intentionally draws over other lines. */
1721 if (s->for_overlaps_p)
1722 {
1723 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1724 r.height = window_text_bottom_y (s->w) - r.y;
1725 }
1726 else
1727 {
1728 /* Don't use S->y for clipping because it doesn't take partially
1729 visible lines into account. For example, it can be negative for
1730 partially visible lines at the top of a window. */
1731 if (!s->row->full_width_p
1732 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1733 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1734 else
1735 r.y = max (0, s->row->y);
1736
1737 /* If drawing a tool-bar window, draw it over the internal border
1738 at the top of the window. */
1739 if (s->w == XWINDOW (s->f->tool_bar_window))
1740 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1741 }
1742
1743 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1744
1745 /* If drawing the cursor, don't let glyph draw outside its
1746 advertised boundaries. Cleartype does this under some circumstances. */
1747 if (s->hl == DRAW_CURSOR)
1748 {
1749 struct glyph *glyph = s->first_glyph;
1750 int height;
1751
1752 if (s->x > r.x)
1753 {
1754 r.width -= s->x - r.x;
1755 r.x = s->x;
1756 }
1757 r.width = min (r.width, glyph->pixel_width);
1758
1759 /* Don't draw cursor glyph taller than our actual glyph. */
1760 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1761 if (height < r.height)
1762 {
1763 r.y = s->ybase + glyph->descent - height;
1764 r.height = height;
1765 }
1766 }
1767
1768 #ifdef CONVERT_FROM_XRECT
1769 CONVERT_FROM_XRECT (r, *nr);
1770 #else
1771 *nr = r;
1772 #endif
1773 }
1774
1775 #endif /* HAVE_WINDOW_SYSTEM */
1776
1777 \f
1778 /***********************************************************************
1779 Lisp form evaluation
1780 ***********************************************************************/
1781
1782 /* Error handler for safe_eval and safe_call. */
1783
1784 static Lisp_Object
1785 safe_eval_handler (arg)
1786 Lisp_Object arg;
1787 {
1788 add_to_log ("Error during redisplay: %s", arg, Qnil);
1789 return Qnil;
1790 }
1791
1792
1793 /* Evaluate SEXPR and return the result, or nil if something went
1794 wrong. Prevent redisplay during the evaluation. */
1795
1796 Lisp_Object
1797 safe_eval (sexpr)
1798 Lisp_Object sexpr;
1799 {
1800 Lisp_Object val;
1801
1802 if (inhibit_eval_during_redisplay)
1803 val = Qnil;
1804 else
1805 {
1806 int count = SPECPDL_INDEX ();
1807 struct gcpro gcpro1;
1808
1809 GCPRO1 (sexpr);
1810 specbind (Qinhibit_redisplay, Qt);
1811 /* Use Qt to ensure debugger does not run,
1812 so there is no possibility of wanting to redisplay. */
1813 val = internal_condition_case_1 (Feval, sexpr, Qt,
1814 safe_eval_handler);
1815 UNGCPRO;
1816 val = unbind_to (count, val);
1817 }
1818
1819 return val;
1820 }
1821
1822
1823 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1824 Return the result, or nil if something went wrong. Prevent
1825 redisplay during the evaluation. */
1826
1827 Lisp_Object
1828 safe_call (nargs, args)
1829 int nargs;
1830 Lisp_Object *args;
1831 {
1832 Lisp_Object val;
1833
1834 if (inhibit_eval_during_redisplay)
1835 val = Qnil;
1836 else
1837 {
1838 int count = SPECPDL_INDEX ();
1839 struct gcpro gcpro1;
1840
1841 GCPRO1 (args[0]);
1842 gcpro1.nvars = nargs;
1843 specbind (Qinhibit_redisplay, Qt);
1844 /* Use Qt to ensure debugger does not run,
1845 so there is no possibility of wanting to redisplay. */
1846 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1847 safe_eval_handler);
1848 UNGCPRO;
1849 val = unbind_to (count, val);
1850 }
1851
1852 return val;
1853 }
1854
1855
1856 /* Call function FN with one argument ARG.
1857 Return the result, or nil if something went wrong. */
1858
1859 Lisp_Object
1860 safe_call1 (fn, arg)
1861 Lisp_Object fn, arg;
1862 {
1863 Lisp_Object args[2];
1864 args[0] = fn;
1865 args[1] = arg;
1866 return safe_call (2, args);
1867 }
1868
1869
1870 \f
1871 /***********************************************************************
1872 Debugging
1873 ***********************************************************************/
1874
1875 #if 0
1876
1877 /* Define CHECK_IT to perform sanity checks on iterators.
1878 This is for debugging. It is too slow to do unconditionally. */
1879
1880 static void
1881 check_it (it)
1882 struct it *it;
1883 {
1884 if (it->method == next_element_from_string)
1885 {
1886 xassert (STRINGP (it->string));
1887 xassert (IT_STRING_CHARPOS (*it) >= 0);
1888 }
1889 else if (it->method == next_element_from_buffer)
1890 {
1891 /* Check that character and byte positions agree. */
1892 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1893 }
1894
1895 if (it->dpvec)
1896 xassert (it->current.dpvec_index >= 0);
1897 else
1898 xassert (it->current.dpvec_index < 0);
1899 }
1900
1901 #define CHECK_IT(IT) check_it ((IT))
1902
1903 #else /* not 0 */
1904
1905 #define CHECK_IT(IT) (void) 0
1906
1907 #endif /* not 0 */
1908
1909
1910 #if GLYPH_DEBUG
1911
1912 /* Check that the window end of window W is what we expect it
1913 to be---the last row in the current matrix displaying text. */
1914
1915 static void
1916 check_window_end (w)
1917 struct window *w;
1918 {
1919 if (!MINI_WINDOW_P (w)
1920 && !NILP (w->window_end_valid))
1921 {
1922 struct glyph_row *row;
1923 xassert ((row = MATRIX_ROW (w->current_matrix,
1924 XFASTINT (w->window_end_vpos)),
1925 !row->enabled_p
1926 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1927 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1928 }
1929 }
1930
1931 #define CHECK_WINDOW_END(W) check_window_end ((W))
1932
1933 #else /* not GLYPH_DEBUG */
1934
1935 #define CHECK_WINDOW_END(W) (void) 0
1936
1937 #endif /* not GLYPH_DEBUG */
1938
1939
1940 \f
1941 /***********************************************************************
1942 Iterator initialization
1943 ***********************************************************************/
1944
1945 /* Initialize IT for displaying current_buffer in window W, starting
1946 at character position CHARPOS. CHARPOS < 0 means that no buffer
1947 position is specified which is useful when the iterator is assigned
1948 a position later. BYTEPOS is the byte position corresponding to
1949 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1950
1951 If ROW is not null, calls to produce_glyphs with IT as parameter
1952 will produce glyphs in that row.
1953
1954 BASE_FACE_ID is the id of a base face to use. It must be one of
1955 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1956 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1957 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1958
1959 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1960 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1961 will be initialized to use the corresponding mode line glyph row of
1962 the desired matrix of W. */
1963
1964 void
1965 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1966 struct it *it;
1967 struct window *w;
1968 int charpos, bytepos;
1969 struct glyph_row *row;
1970 enum face_id base_face_id;
1971 {
1972 int highlight_region_p;
1973
1974 /* Some precondition checks. */
1975 xassert (w != NULL && it != NULL);
1976 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1977 && charpos <= ZV));
1978
1979 /* If face attributes have been changed since the last redisplay,
1980 free realized faces now because they depend on face definitions
1981 that might have changed. Don't free faces while there might be
1982 desired matrices pending which reference these faces. */
1983 if (face_change_count && !inhibit_free_realized_faces)
1984 {
1985 face_change_count = 0;
1986 free_all_realized_faces (Qnil);
1987 }
1988
1989 /* Use one of the mode line rows of W's desired matrix if
1990 appropriate. */
1991 if (row == NULL)
1992 {
1993 if (base_face_id == MODE_LINE_FACE_ID
1994 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1995 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1996 else if (base_face_id == HEADER_LINE_FACE_ID)
1997 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1998 }
1999
2000 /* Clear IT. */
2001 bzero (it, sizeof *it);
2002 it->current.overlay_string_index = -1;
2003 it->current.dpvec_index = -1;
2004 it->base_face_id = base_face_id;
2005
2006 /* The window in which we iterate over current_buffer: */
2007 XSETWINDOW (it->window, w);
2008 it->w = w;
2009 it->f = XFRAME (w->frame);
2010
2011 /* Extra space between lines (on window systems only). */
2012 if (base_face_id == DEFAULT_FACE_ID
2013 && FRAME_WINDOW_P (it->f))
2014 {
2015 if (NATNUMP (current_buffer->extra_line_spacing))
2016 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2017 else if (it->f->extra_line_spacing > 0)
2018 it->extra_line_spacing = it->f->extra_line_spacing;
2019 }
2020
2021 /* If realized faces have been removed, e.g. because of face
2022 attribute changes of named faces, recompute them. When running
2023 in batch mode, the face cache of the initial frame is null. If
2024 we happen to get called, make a dummy face cache. */
2025 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2026 init_frame_faces (it->f);
2027 if (FRAME_FACE_CACHE (it->f)->used == 0)
2028 recompute_basic_faces (it->f);
2029
2030 /* Current value of the `space-width', and 'height' properties. */
2031 it->space_width = Qnil;
2032 it->font_height = Qnil;
2033
2034 /* Are control characters displayed as `^C'? */
2035 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2036
2037 /* -1 means everything between a CR and the following line end
2038 is invisible. >0 means lines indented more than this value are
2039 invisible. */
2040 it->selective = (INTEGERP (current_buffer->selective_display)
2041 ? XFASTINT (current_buffer->selective_display)
2042 : (!NILP (current_buffer->selective_display)
2043 ? -1 : 0));
2044 it->selective_display_ellipsis_p
2045 = !NILP (current_buffer->selective_display_ellipses);
2046
2047 /* Display table to use. */
2048 it->dp = window_display_table (w);
2049
2050 /* Are multibyte characters enabled in current_buffer? */
2051 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2052
2053 /* Non-zero if we should highlight the region. */
2054 highlight_region_p
2055 = (!NILP (Vtransient_mark_mode)
2056 && !NILP (current_buffer->mark_active)
2057 && XMARKER (current_buffer->mark)->buffer != 0);
2058
2059 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2060 start and end of a visible region in window IT->w. Set both to
2061 -1 to indicate no region. */
2062 if (highlight_region_p
2063 /* Maybe highlight only in selected window. */
2064 && (/* Either show region everywhere. */
2065 highlight_nonselected_windows
2066 /* Or show region in the selected window. */
2067 || w == XWINDOW (selected_window)
2068 /* Or show the region if we are in the mini-buffer and W is
2069 the window the mini-buffer refers to. */
2070 || (MINI_WINDOW_P (XWINDOW (selected_window))
2071 && WINDOWP (minibuf_selected_window)
2072 && w == XWINDOW (minibuf_selected_window))))
2073 {
2074 int charpos = marker_position (current_buffer->mark);
2075 it->region_beg_charpos = min (PT, charpos);
2076 it->region_end_charpos = max (PT, charpos);
2077 }
2078 else
2079 it->region_beg_charpos = it->region_end_charpos = -1;
2080
2081 /* Get the position at which the redisplay_end_trigger hook should
2082 be run, if it is to be run at all. */
2083 if (MARKERP (w->redisplay_end_trigger)
2084 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2085 it->redisplay_end_trigger_charpos
2086 = marker_position (w->redisplay_end_trigger);
2087 else if (INTEGERP (w->redisplay_end_trigger))
2088 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2089
2090 /* Correct bogus values of tab_width. */
2091 it->tab_width = XINT (current_buffer->tab_width);
2092 if (it->tab_width <= 0 || it->tab_width > 1000)
2093 it->tab_width = 8;
2094
2095 /* Are lines in the display truncated? */
2096 it->truncate_lines_p
2097 = (base_face_id != DEFAULT_FACE_ID
2098 || XINT (it->w->hscroll)
2099 || (truncate_partial_width_windows
2100 && !WINDOW_FULL_WIDTH_P (it->w))
2101 || !NILP (current_buffer->truncate_lines));
2102
2103 /* Get dimensions of truncation and continuation glyphs. These are
2104 displayed as fringe bitmaps under X, so we don't need them for such
2105 frames. */
2106 if (!FRAME_WINDOW_P (it->f))
2107 {
2108 if (it->truncate_lines_p)
2109 {
2110 /* We will need the truncation glyph. */
2111 xassert (it->glyph_row == NULL);
2112 produce_special_glyphs (it, IT_TRUNCATION);
2113 it->truncation_pixel_width = it->pixel_width;
2114 }
2115 else
2116 {
2117 /* We will need the continuation glyph. */
2118 xassert (it->glyph_row == NULL);
2119 produce_special_glyphs (it, IT_CONTINUATION);
2120 it->continuation_pixel_width = it->pixel_width;
2121 }
2122
2123 /* Reset these values to zero because the produce_special_glyphs
2124 above has changed them. */
2125 it->pixel_width = it->ascent = it->descent = 0;
2126 it->phys_ascent = it->phys_descent = 0;
2127 }
2128
2129 /* Set this after getting the dimensions of truncation and
2130 continuation glyphs, so that we don't produce glyphs when calling
2131 produce_special_glyphs, above. */
2132 it->glyph_row = row;
2133 it->area = TEXT_AREA;
2134
2135 /* Get the dimensions of the display area. The display area
2136 consists of the visible window area plus a horizontally scrolled
2137 part to the left of the window. All x-values are relative to the
2138 start of this total display area. */
2139 if (base_face_id != DEFAULT_FACE_ID)
2140 {
2141 /* Mode lines, menu bar in terminal frames. */
2142 it->first_visible_x = 0;
2143 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2144 }
2145 else
2146 {
2147 it->first_visible_x
2148 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2149 it->last_visible_x = (it->first_visible_x
2150 + window_box_width (w, TEXT_AREA));
2151
2152 /* If we truncate lines, leave room for the truncator glyph(s) at
2153 the right margin. Otherwise, leave room for the continuation
2154 glyph(s). Truncation and continuation glyphs are not inserted
2155 for window-based redisplay. */
2156 if (!FRAME_WINDOW_P (it->f))
2157 {
2158 if (it->truncate_lines_p)
2159 it->last_visible_x -= it->truncation_pixel_width;
2160 else
2161 it->last_visible_x -= it->continuation_pixel_width;
2162 }
2163
2164 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2165 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2166 }
2167
2168 /* Leave room for a border glyph. */
2169 if (!FRAME_WINDOW_P (it->f)
2170 && !WINDOW_RIGHTMOST_P (it->w))
2171 it->last_visible_x -= 1;
2172
2173 it->last_visible_y = window_text_bottom_y (w);
2174
2175 /* For mode lines and alike, arrange for the first glyph having a
2176 left box line if the face specifies a box. */
2177 if (base_face_id != DEFAULT_FACE_ID)
2178 {
2179 struct face *face;
2180
2181 it->face_id = base_face_id;
2182
2183 /* If we have a boxed mode line, make the first character appear
2184 with a left box line. */
2185 face = FACE_FROM_ID (it->f, base_face_id);
2186 if (face->box != FACE_NO_BOX)
2187 it->start_of_box_run_p = 1;
2188 }
2189
2190 /* If a buffer position was specified, set the iterator there,
2191 getting overlays and face properties from that position. */
2192 if (charpos >= BUF_BEG (current_buffer))
2193 {
2194 it->end_charpos = ZV;
2195 it->face_id = -1;
2196 IT_CHARPOS (*it) = charpos;
2197
2198 /* Compute byte position if not specified. */
2199 if (bytepos < charpos)
2200 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2201 else
2202 IT_BYTEPOS (*it) = bytepos;
2203
2204 it->start = it->current;
2205
2206 /* Compute faces etc. */
2207 reseat (it, it->current.pos, 1);
2208 }
2209
2210 CHECK_IT (it);
2211 }
2212
2213
2214 /* Initialize IT for the display of window W with window start POS. */
2215
2216 void
2217 start_display (it, w, pos)
2218 struct it *it;
2219 struct window *w;
2220 struct text_pos pos;
2221 {
2222 struct glyph_row *row;
2223 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2224
2225 row = w->desired_matrix->rows + first_vpos;
2226 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2227 it->first_vpos = first_vpos;
2228
2229 if (!it->truncate_lines_p)
2230 {
2231 int start_at_line_beg_p;
2232 int first_y = it->current_y;
2233
2234 /* If window start is not at a line start, skip forward to POS to
2235 get the correct continuation lines width. */
2236 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2237 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2238 if (!start_at_line_beg_p)
2239 {
2240 int new_x;
2241
2242 reseat_at_previous_visible_line_start (it);
2243 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2244
2245 new_x = it->current_x + it->pixel_width;
2246
2247 /* If lines are continued, this line may end in the middle
2248 of a multi-glyph character (e.g. a control character
2249 displayed as \003, or in the middle of an overlay
2250 string). In this case move_it_to above will not have
2251 taken us to the start of the continuation line but to the
2252 end of the continued line. */
2253 if (it->current_x > 0
2254 && !it->truncate_lines_p /* Lines are continued. */
2255 && (/* And glyph doesn't fit on the line. */
2256 new_x > it->last_visible_x
2257 /* Or it fits exactly and we're on a window
2258 system frame. */
2259 || (new_x == it->last_visible_x
2260 && FRAME_WINDOW_P (it->f))))
2261 {
2262 if (it->current.dpvec_index >= 0
2263 || it->current.overlay_string_index >= 0)
2264 {
2265 set_iterator_to_next (it, 1);
2266 move_it_in_display_line_to (it, -1, -1, 0);
2267 }
2268
2269 it->continuation_lines_width += it->current_x;
2270 }
2271
2272 /* We're starting a new display line, not affected by the
2273 height of the continued line, so clear the appropriate
2274 fields in the iterator structure. */
2275 it->max_ascent = it->max_descent = 0;
2276 it->max_phys_ascent = it->max_phys_descent = 0;
2277
2278 it->current_y = first_y;
2279 it->vpos = 0;
2280 it->current_x = it->hpos = 0;
2281 }
2282 }
2283
2284 #if 0 /* Don't assert the following because start_display is sometimes
2285 called intentionally with a window start that is not at a
2286 line start. Please leave this code in as a comment. */
2287
2288 /* Window start should be on a line start, now. */
2289 xassert (it->continuation_lines_width
2290 || IT_CHARPOS (it) == BEGV
2291 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2292 #endif /* 0 */
2293 }
2294
2295
2296 /* Return 1 if POS is a position in ellipses displayed for invisible
2297 text. W is the window we display, for text property lookup. */
2298
2299 static int
2300 in_ellipses_for_invisible_text_p (pos, w)
2301 struct display_pos *pos;
2302 struct window *w;
2303 {
2304 Lisp_Object prop, window;
2305 int ellipses_p = 0;
2306 int charpos = CHARPOS (pos->pos);
2307
2308 /* If POS specifies a position in a display vector, this might
2309 be for an ellipsis displayed for invisible text. We won't
2310 get the iterator set up for delivering that ellipsis unless
2311 we make sure that it gets aware of the invisible text. */
2312 if (pos->dpvec_index >= 0
2313 && pos->overlay_string_index < 0
2314 && CHARPOS (pos->string_pos) < 0
2315 && charpos > BEGV
2316 && (XSETWINDOW (window, w),
2317 prop = Fget_char_property (make_number (charpos),
2318 Qinvisible, window),
2319 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2320 {
2321 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2322 window);
2323 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2324 }
2325
2326 return ellipses_p;
2327 }
2328
2329
2330 /* Initialize IT for stepping through current_buffer in window W,
2331 starting at position POS that includes overlay string and display
2332 vector/ control character translation position information. Value
2333 is zero if there are overlay strings with newlines at POS. */
2334
2335 static int
2336 init_from_display_pos (it, w, pos)
2337 struct it *it;
2338 struct window *w;
2339 struct display_pos *pos;
2340 {
2341 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2342 int i, overlay_strings_with_newlines = 0;
2343
2344 /* If POS specifies a position in a display vector, this might
2345 be for an ellipsis displayed for invisible text. We won't
2346 get the iterator set up for delivering that ellipsis unless
2347 we make sure that it gets aware of the invisible text. */
2348 if (in_ellipses_for_invisible_text_p (pos, w))
2349 {
2350 --charpos;
2351 bytepos = 0;
2352 }
2353
2354 /* Keep in mind: the call to reseat in init_iterator skips invisible
2355 text, so we might end up at a position different from POS. This
2356 is only a problem when POS is a row start after a newline and an
2357 overlay starts there with an after-string, and the overlay has an
2358 invisible property. Since we don't skip invisible text in
2359 display_line and elsewhere immediately after consuming the
2360 newline before the row start, such a POS will not be in a string,
2361 but the call to init_iterator below will move us to the
2362 after-string. */
2363 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2364
2365 for (i = 0; i < it->n_overlay_strings; ++i)
2366 {
2367 const char *s = SDATA (it->overlay_strings[i]);
2368 const char *e = s + SBYTES (it->overlay_strings[i]);
2369
2370 while (s < e && *s != '\n')
2371 ++s;
2372
2373 if (s < e)
2374 {
2375 overlay_strings_with_newlines = 1;
2376 break;
2377 }
2378 }
2379
2380 /* If position is within an overlay string, set up IT to the right
2381 overlay string. */
2382 if (pos->overlay_string_index >= 0)
2383 {
2384 int relative_index;
2385
2386 /* If the first overlay string happens to have a `display'
2387 property for an image, the iterator will be set up for that
2388 image, and we have to undo that setup first before we can
2389 correct the overlay string index. */
2390 if (it->method == next_element_from_image)
2391 pop_it (it);
2392
2393 /* We already have the first chunk of overlay strings in
2394 IT->overlay_strings. Load more until the one for
2395 pos->overlay_string_index is in IT->overlay_strings. */
2396 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2397 {
2398 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2399 it->current.overlay_string_index = 0;
2400 while (n--)
2401 {
2402 load_overlay_strings (it, 0);
2403 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2404 }
2405 }
2406
2407 it->current.overlay_string_index = pos->overlay_string_index;
2408 relative_index = (it->current.overlay_string_index
2409 % OVERLAY_STRING_CHUNK_SIZE);
2410 it->string = it->overlay_strings[relative_index];
2411 xassert (STRINGP (it->string));
2412 it->current.string_pos = pos->string_pos;
2413 it->method = next_element_from_string;
2414 }
2415
2416 #if 0 /* This is bogus because POS not having an overlay string
2417 position does not mean it's after the string. Example: A
2418 line starting with a before-string and initialization of IT
2419 to the previous row's end position. */
2420 else if (it->current.overlay_string_index >= 0)
2421 {
2422 /* If POS says we're already after an overlay string ending at
2423 POS, make sure to pop the iterator because it will be in
2424 front of that overlay string. When POS is ZV, we've thereby
2425 also ``processed'' overlay strings at ZV. */
2426 while (it->sp)
2427 pop_it (it);
2428 it->current.overlay_string_index = -1;
2429 it->method = next_element_from_buffer;
2430 if (CHARPOS (pos->pos) == ZV)
2431 it->overlay_strings_at_end_processed_p = 1;
2432 }
2433 #endif /* 0 */
2434
2435 if (CHARPOS (pos->string_pos) >= 0)
2436 {
2437 /* Recorded position is not in an overlay string, but in another
2438 string. This can only be a string from a `display' property.
2439 IT should already be filled with that string. */
2440 it->current.string_pos = pos->string_pos;
2441 xassert (STRINGP (it->string));
2442 }
2443
2444 /* Restore position in display vector translations, control
2445 character translations or ellipses. */
2446 if (pos->dpvec_index >= 0)
2447 {
2448 if (it->dpvec == NULL)
2449 get_next_display_element (it);
2450 xassert (it->dpvec && it->current.dpvec_index == 0);
2451 it->current.dpvec_index = pos->dpvec_index;
2452 }
2453
2454 CHECK_IT (it);
2455 return !overlay_strings_with_newlines;
2456 }
2457
2458
2459 /* Initialize IT for stepping through current_buffer in window W
2460 starting at ROW->start. */
2461
2462 static void
2463 init_to_row_start (it, w, row)
2464 struct it *it;
2465 struct window *w;
2466 struct glyph_row *row;
2467 {
2468 init_from_display_pos (it, w, &row->start);
2469 it->start = row->start;
2470 it->continuation_lines_width = row->continuation_lines_width;
2471 CHECK_IT (it);
2472 }
2473
2474
2475 /* Initialize IT for stepping through current_buffer in window W
2476 starting in the line following ROW, i.e. starting at ROW->end.
2477 Value is zero if there are overlay strings with newlines at ROW's
2478 end position. */
2479
2480 static int
2481 init_to_row_end (it, w, row)
2482 struct it *it;
2483 struct window *w;
2484 struct glyph_row *row;
2485 {
2486 int success = 0;
2487
2488 if (init_from_display_pos (it, w, &row->end))
2489 {
2490 if (row->continued_p)
2491 it->continuation_lines_width
2492 = row->continuation_lines_width + row->pixel_width;
2493 CHECK_IT (it);
2494 success = 1;
2495 }
2496
2497 return success;
2498 }
2499
2500
2501
2502 \f
2503 /***********************************************************************
2504 Text properties
2505 ***********************************************************************/
2506
2507 /* Called when IT reaches IT->stop_charpos. Handle text property and
2508 overlay changes. Set IT->stop_charpos to the next position where
2509 to stop. */
2510
2511 static void
2512 handle_stop (it)
2513 struct it *it;
2514 {
2515 enum prop_handled handled;
2516 int handle_overlay_change_p = 1;
2517 struct props *p;
2518
2519 it->dpvec = NULL;
2520 it->current.dpvec_index = -1;
2521
2522 do
2523 {
2524 handled = HANDLED_NORMALLY;
2525
2526 /* Call text property handlers. */
2527 for (p = it_props; p->handler; ++p)
2528 {
2529 handled = p->handler (it);
2530
2531 if (handled == HANDLED_RECOMPUTE_PROPS)
2532 break;
2533 else if (handled == HANDLED_RETURN)
2534 return;
2535 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2536 handle_overlay_change_p = 0;
2537 }
2538
2539 if (handled != HANDLED_RECOMPUTE_PROPS)
2540 {
2541 /* Don't check for overlay strings below when set to deliver
2542 characters from a display vector. */
2543 if (it->method == next_element_from_display_vector)
2544 handle_overlay_change_p = 0;
2545
2546 /* Handle overlay changes. */
2547 if (handle_overlay_change_p)
2548 handled = handle_overlay_change (it);
2549
2550 /* Determine where to stop next. */
2551 if (handled == HANDLED_NORMALLY)
2552 compute_stop_pos (it);
2553 }
2554 }
2555 while (handled == HANDLED_RECOMPUTE_PROPS);
2556 }
2557
2558
2559 /* Compute IT->stop_charpos from text property and overlay change
2560 information for IT's current position. */
2561
2562 static void
2563 compute_stop_pos (it)
2564 struct it *it;
2565 {
2566 register INTERVAL iv, next_iv;
2567 Lisp_Object object, limit, position;
2568
2569 /* If nowhere else, stop at the end. */
2570 it->stop_charpos = it->end_charpos;
2571
2572 if (STRINGP (it->string))
2573 {
2574 /* Strings are usually short, so don't limit the search for
2575 properties. */
2576 object = it->string;
2577 limit = Qnil;
2578 position = make_number (IT_STRING_CHARPOS (*it));
2579 }
2580 else
2581 {
2582 int charpos;
2583
2584 /* If next overlay change is in front of the current stop pos
2585 (which is IT->end_charpos), stop there. Note: value of
2586 next_overlay_change is point-max if no overlay change
2587 follows. */
2588 charpos = next_overlay_change (IT_CHARPOS (*it));
2589 if (charpos < it->stop_charpos)
2590 it->stop_charpos = charpos;
2591
2592 /* If showing the region, we have to stop at the region
2593 start or end because the face might change there. */
2594 if (it->region_beg_charpos > 0)
2595 {
2596 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2597 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2598 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2599 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2600 }
2601
2602 /* Set up variables for computing the stop position from text
2603 property changes. */
2604 XSETBUFFER (object, current_buffer);
2605 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2606 position = make_number (IT_CHARPOS (*it));
2607
2608 }
2609
2610 /* Get the interval containing IT's position. Value is a null
2611 interval if there isn't such an interval. */
2612 iv = validate_interval_range (object, &position, &position, 0);
2613 if (!NULL_INTERVAL_P (iv))
2614 {
2615 Lisp_Object values_here[LAST_PROP_IDX];
2616 struct props *p;
2617
2618 /* Get properties here. */
2619 for (p = it_props; p->handler; ++p)
2620 values_here[p->idx] = textget (iv->plist, *p->name);
2621
2622 /* Look for an interval following iv that has different
2623 properties. */
2624 for (next_iv = next_interval (iv);
2625 (!NULL_INTERVAL_P (next_iv)
2626 && (NILP (limit)
2627 || XFASTINT (limit) > next_iv->position));
2628 next_iv = next_interval (next_iv))
2629 {
2630 for (p = it_props; p->handler; ++p)
2631 {
2632 Lisp_Object new_value;
2633
2634 new_value = textget (next_iv->plist, *p->name);
2635 if (!EQ (values_here[p->idx], new_value))
2636 break;
2637 }
2638
2639 if (p->handler)
2640 break;
2641 }
2642
2643 if (!NULL_INTERVAL_P (next_iv))
2644 {
2645 if (INTEGERP (limit)
2646 && next_iv->position >= XFASTINT (limit))
2647 /* No text property change up to limit. */
2648 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2649 else
2650 /* Text properties change in next_iv. */
2651 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2652 }
2653 }
2654
2655 xassert (STRINGP (it->string)
2656 || (it->stop_charpos >= BEGV
2657 && it->stop_charpos >= IT_CHARPOS (*it)));
2658 }
2659
2660
2661 /* Return the position of the next overlay change after POS in
2662 current_buffer. Value is point-max if no overlay change
2663 follows. This is like `next-overlay-change' but doesn't use
2664 xmalloc. */
2665
2666 static int
2667 next_overlay_change (pos)
2668 int pos;
2669 {
2670 int noverlays;
2671 int endpos;
2672 Lisp_Object *overlays;
2673 int len;
2674 int i;
2675
2676 /* Get all overlays at the given position. */
2677 len = 10;
2678 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2679 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2680 if (noverlays > len)
2681 {
2682 len = noverlays;
2683 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2684 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2685 }
2686
2687 /* If any of these overlays ends before endpos,
2688 use its ending point instead. */
2689 for (i = 0; i < noverlays; ++i)
2690 {
2691 Lisp_Object oend;
2692 int oendpos;
2693
2694 oend = OVERLAY_END (overlays[i]);
2695 oendpos = OVERLAY_POSITION (oend);
2696 endpos = min (endpos, oendpos);
2697 }
2698
2699 return endpos;
2700 }
2701
2702
2703 \f
2704 /***********************************************************************
2705 Fontification
2706 ***********************************************************************/
2707
2708 /* Handle changes in the `fontified' property of the current buffer by
2709 calling hook functions from Qfontification_functions to fontify
2710 regions of text. */
2711
2712 static enum prop_handled
2713 handle_fontified_prop (it)
2714 struct it *it;
2715 {
2716 Lisp_Object prop, pos;
2717 enum prop_handled handled = HANDLED_NORMALLY;
2718
2719 /* Get the value of the `fontified' property at IT's current buffer
2720 position. (The `fontified' property doesn't have a special
2721 meaning in strings.) If the value is nil, call functions from
2722 Qfontification_functions. */
2723 if (!STRINGP (it->string)
2724 && it->s == NULL
2725 && !NILP (Vfontification_functions)
2726 && !NILP (Vrun_hooks)
2727 && (pos = make_number (IT_CHARPOS (*it)),
2728 prop = Fget_char_property (pos, Qfontified, Qnil),
2729 NILP (prop)))
2730 {
2731 int count = SPECPDL_INDEX ();
2732 Lisp_Object val;
2733
2734 val = Vfontification_functions;
2735 specbind (Qfontification_functions, Qnil);
2736
2737 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2738 safe_call1 (val, pos);
2739 else
2740 {
2741 Lisp_Object globals, fn;
2742 struct gcpro gcpro1, gcpro2;
2743
2744 globals = Qnil;
2745 GCPRO2 (val, globals);
2746
2747 for (; CONSP (val); val = XCDR (val))
2748 {
2749 fn = XCAR (val);
2750
2751 if (EQ (fn, Qt))
2752 {
2753 /* A value of t indicates this hook has a local
2754 binding; it means to run the global binding too.
2755 In a global value, t should not occur. If it
2756 does, we must ignore it to avoid an endless
2757 loop. */
2758 for (globals = Fdefault_value (Qfontification_functions);
2759 CONSP (globals);
2760 globals = XCDR (globals))
2761 {
2762 fn = XCAR (globals);
2763 if (!EQ (fn, Qt))
2764 safe_call1 (fn, pos);
2765 }
2766 }
2767 else
2768 safe_call1 (fn, pos);
2769 }
2770
2771 UNGCPRO;
2772 }
2773
2774 unbind_to (count, Qnil);
2775
2776 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2777 something. This avoids an endless loop if they failed to
2778 fontify the text for which reason ever. */
2779 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2780 handled = HANDLED_RECOMPUTE_PROPS;
2781 }
2782
2783 return handled;
2784 }
2785
2786
2787 \f
2788 /***********************************************************************
2789 Faces
2790 ***********************************************************************/
2791
2792 /* Set up iterator IT from face properties at its current position.
2793 Called from handle_stop. */
2794
2795 static enum prop_handled
2796 handle_face_prop (it)
2797 struct it *it;
2798 {
2799 int new_face_id, next_stop;
2800
2801 if (!STRINGP (it->string))
2802 {
2803 new_face_id
2804 = face_at_buffer_position (it->w,
2805 IT_CHARPOS (*it),
2806 it->region_beg_charpos,
2807 it->region_end_charpos,
2808 &next_stop,
2809 (IT_CHARPOS (*it)
2810 + TEXT_PROP_DISTANCE_LIMIT),
2811 0);
2812
2813 /* Is this a start of a run of characters with box face?
2814 Caveat: this can be called for a freshly initialized
2815 iterator; face_id is -1 in this case. We know that the new
2816 face will not change until limit, i.e. if the new face has a
2817 box, all characters up to limit will have one. But, as
2818 usual, we don't know whether limit is really the end. */
2819 if (new_face_id != it->face_id)
2820 {
2821 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2822
2823 /* If new face has a box but old face has not, this is
2824 the start of a run of characters with box, i.e. it has
2825 a shadow on the left side. The value of face_id of the
2826 iterator will be -1 if this is the initial call that gets
2827 the face. In this case, we have to look in front of IT's
2828 position and see whether there is a face != new_face_id. */
2829 it->start_of_box_run_p
2830 = (new_face->box != FACE_NO_BOX
2831 && (it->face_id >= 0
2832 || IT_CHARPOS (*it) == BEG
2833 || new_face_id != face_before_it_pos (it)));
2834 it->face_box_p = new_face->box != FACE_NO_BOX;
2835 }
2836 }
2837 else
2838 {
2839 int base_face_id, bufpos;
2840
2841 if (it->current.overlay_string_index >= 0)
2842 bufpos = IT_CHARPOS (*it);
2843 else
2844 bufpos = 0;
2845
2846 /* For strings from a buffer, i.e. overlay strings or strings
2847 from a `display' property, use the face at IT's current
2848 buffer position as the base face to merge with, so that
2849 overlay strings appear in the same face as surrounding
2850 text, unless they specify their own faces. */
2851 base_face_id = underlying_face_id (it);
2852
2853 new_face_id = face_at_string_position (it->w,
2854 it->string,
2855 IT_STRING_CHARPOS (*it),
2856 bufpos,
2857 it->region_beg_charpos,
2858 it->region_end_charpos,
2859 &next_stop,
2860 base_face_id, 0);
2861
2862 #if 0 /* This shouldn't be neccessary. Let's check it. */
2863 /* If IT is used to display a mode line we would really like to
2864 use the mode line face instead of the frame's default face. */
2865 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2866 && new_face_id == DEFAULT_FACE_ID)
2867 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2868 #endif
2869
2870 /* Is this a start of a run of characters with box? Caveat:
2871 this can be called for a freshly allocated iterator; face_id
2872 is -1 is this case. We know that the new face will not
2873 change until the next check pos, i.e. if the new face has a
2874 box, all characters up to that position will have a
2875 box. But, as usual, we don't know whether that position
2876 is really the end. */
2877 if (new_face_id != it->face_id)
2878 {
2879 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2880 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2881
2882 /* If new face has a box but old face hasn't, this is the
2883 start of a run of characters with box, i.e. it has a
2884 shadow on the left side. */
2885 it->start_of_box_run_p
2886 = new_face->box && (old_face == NULL || !old_face->box);
2887 it->face_box_p = new_face->box != FACE_NO_BOX;
2888 }
2889 }
2890
2891 it->face_id = new_face_id;
2892 return HANDLED_NORMALLY;
2893 }
2894
2895
2896 /* Return the ID of the face ``underlying'' IT's current position,
2897 which is in a string. If the iterator is associated with a
2898 buffer, return the face at IT's current buffer position.
2899 Otherwise, use the iterator's base_face_id. */
2900
2901 static int
2902 underlying_face_id (it)
2903 struct it *it;
2904 {
2905 int face_id = it->base_face_id, i;
2906
2907 xassert (STRINGP (it->string));
2908
2909 for (i = it->sp - 1; i >= 0; --i)
2910 if (NILP (it->stack[i].string))
2911 face_id = it->stack[i].face_id;
2912
2913 return face_id;
2914 }
2915
2916
2917 /* Compute the face one character before or after the current position
2918 of IT. BEFORE_P non-zero means get the face in front of IT's
2919 position. Value is the id of the face. */
2920
2921 static int
2922 face_before_or_after_it_pos (it, before_p)
2923 struct it *it;
2924 int before_p;
2925 {
2926 int face_id, limit;
2927 int next_check_charpos;
2928 struct text_pos pos;
2929
2930 xassert (it->s == NULL);
2931
2932 if (STRINGP (it->string))
2933 {
2934 int bufpos, base_face_id;
2935
2936 /* No face change past the end of the string (for the case
2937 we are padding with spaces). No face change before the
2938 string start. */
2939 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2940 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2941 return it->face_id;
2942
2943 /* Set pos to the position before or after IT's current position. */
2944 if (before_p)
2945 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2946 else
2947 /* For composition, we must check the character after the
2948 composition. */
2949 pos = (it->what == IT_COMPOSITION
2950 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2951 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2952
2953 if (it->current.overlay_string_index >= 0)
2954 bufpos = IT_CHARPOS (*it);
2955 else
2956 bufpos = 0;
2957
2958 base_face_id = underlying_face_id (it);
2959
2960 /* Get the face for ASCII, or unibyte. */
2961 face_id = face_at_string_position (it->w,
2962 it->string,
2963 CHARPOS (pos),
2964 bufpos,
2965 it->region_beg_charpos,
2966 it->region_end_charpos,
2967 &next_check_charpos,
2968 base_face_id, 0);
2969
2970 /* Correct the face for charsets different from ASCII. Do it
2971 for the multibyte case only. The face returned above is
2972 suitable for unibyte text if IT->string is unibyte. */
2973 if (STRING_MULTIBYTE (it->string))
2974 {
2975 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2976 int rest = SBYTES (it->string) - BYTEPOS (pos);
2977 int c, len;
2978 struct face *face = FACE_FROM_ID (it->f, face_id);
2979
2980 c = string_char_and_length (p, rest, &len);
2981 face_id = FACE_FOR_CHAR (it->f, face, c);
2982 }
2983 }
2984 else
2985 {
2986 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2987 || (IT_CHARPOS (*it) <= BEGV && before_p))
2988 return it->face_id;
2989
2990 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2991 pos = it->current.pos;
2992
2993 if (before_p)
2994 DEC_TEXT_POS (pos, it->multibyte_p);
2995 else
2996 {
2997 if (it->what == IT_COMPOSITION)
2998 /* For composition, we must check the position after the
2999 composition. */
3000 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3001 else
3002 INC_TEXT_POS (pos, it->multibyte_p);
3003 }
3004
3005 /* Determine face for CHARSET_ASCII, or unibyte. */
3006 face_id = face_at_buffer_position (it->w,
3007 CHARPOS (pos),
3008 it->region_beg_charpos,
3009 it->region_end_charpos,
3010 &next_check_charpos,
3011 limit, 0);
3012
3013 /* Correct the face for charsets different from ASCII. Do it
3014 for the multibyte case only. The face returned above is
3015 suitable for unibyte text if current_buffer is unibyte. */
3016 if (it->multibyte_p)
3017 {
3018 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3019 struct face *face = FACE_FROM_ID (it->f, face_id);
3020 face_id = FACE_FOR_CHAR (it->f, face, c);
3021 }
3022 }
3023
3024 return face_id;
3025 }
3026
3027
3028 \f
3029 /***********************************************************************
3030 Invisible text
3031 ***********************************************************************/
3032
3033 /* Set up iterator IT from invisible properties at its current
3034 position. Called from handle_stop. */
3035
3036 static enum prop_handled
3037 handle_invisible_prop (it)
3038 struct it *it;
3039 {
3040 enum prop_handled handled = HANDLED_NORMALLY;
3041
3042 if (STRINGP (it->string))
3043 {
3044 extern Lisp_Object Qinvisible;
3045 Lisp_Object prop, end_charpos, limit, charpos;
3046
3047 /* Get the value of the invisible text property at the
3048 current position. Value will be nil if there is no such
3049 property. */
3050 charpos = make_number (IT_STRING_CHARPOS (*it));
3051 prop = Fget_text_property (charpos, Qinvisible, it->string);
3052
3053 if (!NILP (prop)
3054 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3055 {
3056 handled = HANDLED_RECOMPUTE_PROPS;
3057
3058 /* Get the position at which the next change of the
3059 invisible text property can be found in IT->string.
3060 Value will be nil if the property value is the same for
3061 all the rest of IT->string. */
3062 XSETINT (limit, SCHARS (it->string));
3063 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3064 it->string, limit);
3065
3066 /* Text at current position is invisible. The next
3067 change in the property is at position end_charpos.
3068 Move IT's current position to that position. */
3069 if (INTEGERP (end_charpos)
3070 && XFASTINT (end_charpos) < XFASTINT (limit))
3071 {
3072 struct text_pos old;
3073 old = it->current.string_pos;
3074 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3075 compute_string_pos (&it->current.string_pos, old, it->string);
3076 }
3077 else
3078 {
3079 /* The rest of the string is invisible. If this is an
3080 overlay string, proceed with the next overlay string
3081 or whatever comes and return a character from there. */
3082 if (it->current.overlay_string_index >= 0)
3083 {
3084 next_overlay_string (it);
3085 /* Don't check for overlay strings when we just
3086 finished processing them. */
3087 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3088 }
3089 else
3090 {
3091 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3092 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3093 }
3094 }
3095 }
3096 }
3097 else
3098 {
3099 int invis_p, newpos, next_stop, start_charpos;
3100 Lisp_Object pos, prop, overlay;
3101
3102 /* First of all, is there invisible text at this position? */
3103 start_charpos = IT_CHARPOS (*it);
3104 pos = make_number (IT_CHARPOS (*it));
3105 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3106 &overlay);
3107 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3108
3109 /* If we are on invisible text, skip over it. */
3110 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3111 {
3112 /* Record whether we have to display an ellipsis for the
3113 invisible text. */
3114 int display_ellipsis_p = invis_p == 2;
3115
3116 handled = HANDLED_RECOMPUTE_PROPS;
3117
3118 /* Loop skipping over invisible text. The loop is left at
3119 ZV or with IT on the first char being visible again. */
3120 do
3121 {
3122 /* Try to skip some invisible text. Return value is the
3123 position reached which can be equal to IT's position
3124 if there is nothing invisible here. This skips both
3125 over invisible text properties and overlays with
3126 invisible property. */
3127 newpos = skip_invisible (IT_CHARPOS (*it),
3128 &next_stop, ZV, it->window);
3129
3130 /* If we skipped nothing at all we weren't at invisible
3131 text in the first place. If everything to the end of
3132 the buffer was skipped, end the loop. */
3133 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3134 invis_p = 0;
3135 else
3136 {
3137 /* We skipped some characters but not necessarily
3138 all there are. Check if we ended up on visible
3139 text. Fget_char_property returns the property of
3140 the char before the given position, i.e. if we
3141 get invis_p = 0, this means that the char at
3142 newpos is visible. */
3143 pos = make_number (newpos);
3144 prop = Fget_char_property (pos, Qinvisible, it->window);
3145 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3146 }
3147
3148 /* If we ended up on invisible text, proceed to
3149 skip starting with next_stop. */
3150 if (invis_p)
3151 IT_CHARPOS (*it) = next_stop;
3152 }
3153 while (invis_p);
3154
3155 /* The position newpos is now either ZV or on visible text. */
3156 IT_CHARPOS (*it) = newpos;
3157 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3158
3159 /* If there are before-strings at the start of invisible
3160 text, and the text is invisible because of a text
3161 property, arrange to show before-strings because 20.x did
3162 it that way. (If the text is invisible because of an
3163 overlay property instead of a text property, this is
3164 already handled in the overlay code.) */
3165 if (NILP (overlay)
3166 && get_overlay_strings (it, start_charpos))
3167 {
3168 handled = HANDLED_RECOMPUTE_PROPS;
3169 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3170 }
3171 else if (display_ellipsis_p)
3172 setup_for_ellipsis (it);
3173 }
3174 }
3175
3176 return handled;
3177 }
3178
3179
3180 /* Make iterator IT return `...' next. */
3181
3182 static void
3183 setup_for_ellipsis (it)
3184 struct it *it;
3185 {
3186 if (it->dp
3187 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3188 {
3189 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3190 it->dpvec = v->contents;
3191 it->dpend = v->contents + v->size;
3192 }
3193 else
3194 {
3195 /* Default `...'. */
3196 it->dpvec = default_invis_vector;
3197 it->dpend = default_invis_vector + 3;
3198 }
3199
3200 /* The ellipsis display does not replace the display of the
3201 character at the new position. Indicate this by setting
3202 IT->dpvec_char_len to zero. */
3203 it->dpvec_char_len = 0;
3204
3205 it->current.dpvec_index = 0;
3206 it->method = next_element_from_display_vector;
3207 }
3208
3209
3210 \f
3211 /***********************************************************************
3212 'display' property
3213 ***********************************************************************/
3214
3215 /* Set up iterator IT from `display' property at its current position.
3216 Called from handle_stop. */
3217
3218 static enum prop_handled
3219 handle_display_prop (it)
3220 struct it *it;
3221 {
3222 Lisp_Object prop, object;
3223 struct text_pos *position;
3224 int display_replaced_p = 0;
3225
3226 if (STRINGP (it->string))
3227 {
3228 object = it->string;
3229 position = &it->current.string_pos;
3230 }
3231 else
3232 {
3233 object = it->w->buffer;
3234 position = &it->current.pos;
3235 }
3236
3237 /* Reset those iterator values set from display property values. */
3238 it->font_height = Qnil;
3239 it->space_width = Qnil;
3240 it->voffset = 0;
3241
3242 /* We don't support recursive `display' properties, i.e. string
3243 values that have a string `display' property, that have a string
3244 `display' property etc. */
3245 if (!it->string_from_display_prop_p)
3246 it->area = TEXT_AREA;
3247
3248 prop = Fget_char_property (make_number (position->charpos),
3249 Qdisplay, object);
3250 if (NILP (prop))
3251 return HANDLED_NORMALLY;
3252
3253 if (CONSP (prop)
3254 /* Simple properties. */
3255 && !EQ (XCAR (prop), Qimage)
3256 && !EQ (XCAR (prop), Qspace)
3257 && !EQ (XCAR (prop), Qwhen)
3258 && !EQ (XCAR (prop), Qspace_width)
3259 && !EQ (XCAR (prop), Qheight)
3260 && !EQ (XCAR (prop), Qraise)
3261 /* Marginal area specifications. */
3262 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3263 && !NILP (XCAR (prop)))
3264 {
3265 for (; CONSP (prop); prop = XCDR (prop))
3266 {
3267 if (handle_single_display_prop (it, XCAR (prop), object,
3268 position, display_replaced_p))
3269 display_replaced_p = 1;
3270 }
3271 }
3272 else if (VECTORP (prop))
3273 {
3274 int i;
3275 for (i = 0; i < ASIZE (prop); ++i)
3276 if (handle_single_display_prop (it, AREF (prop, i), object,
3277 position, display_replaced_p))
3278 display_replaced_p = 1;
3279 }
3280 else
3281 {
3282 if (handle_single_display_prop (it, prop, object, position, 0))
3283 display_replaced_p = 1;
3284 }
3285
3286 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3287 }
3288
3289
3290 /* Value is the position of the end of the `display' property starting
3291 at START_POS in OBJECT. */
3292
3293 static struct text_pos
3294 display_prop_end (it, object, start_pos)
3295 struct it *it;
3296 Lisp_Object object;
3297 struct text_pos start_pos;
3298 {
3299 Lisp_Object end;
3300 struct text_pos end_pos;
3301
3302 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3303 Qdisplay, object, Qnil);
3304 CHARPOS (end_pos) = XFASTINT (end);
3305 if (STRINGP (object))
3306 compute_string_pos (&end_pos, start_pos, it->string);
3307 else
3308 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3309
3310 return end_pos;
3311 }
3312
3313
3314 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3315 is the object in which the `display' property was found. *POSITION
3316 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3317 means that we previously saw a display sub-property which already
3318 replaced text display with something else, for example an image;
3319 ignore such properties after the first one has been processed.
3320
3321 If PROP is a `space' or `image' sub-property, set *POSITION to the
3322 end position of the `display' property.
3323
3324 Value is non-zero if something was found which replaces the display
3325 of buffer or string text. */
3326
3327 static int
3328 handle_single_display_prop (it, prop, object, position,
3329 display_replaced_before_p)
3330 struct it *it;
3331 Lisp_Object prop;
3332 Lisp_Object object;
3333 struct text_pos *position;
3334 int display_replaced_before_p;
3335 {
3336 Lisp_Object value;
3337 int replaces_text_display_p = 0;
3338 Lisp_Object form;
3339
3340 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3341 evaluated. If the result is nil, VALUE is ignored. */
3342 form = Qt;
3343 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3344 {
3345 prop = XCDR (prop);
3346 if (!CONSP (prop))
3347 return 0;
3348 form = XCAR (prop);
3349 prop = XCDR (prop);
3350 }
3351
3352 if (!NILP (form) && !EQ (form, Qt))
3353 {
3354 int count = SPECPDL_INDEX ();
3355 struct gcpro gcpro1;
3356
3357 /* Bind `object' to the object having the `display' property, a
3358 buffer or string. Bind `position' to the position in the
3359 object where the property was found, and `buffer-position'
3360 to the current position in the buffer. */
3361 specbind (Qobject, object);
3362 specbind (Qposition, make_number (CHARPOS (*position)));
3363 specbind (Qbuffer_position,
3364 make_number (STRINGP (object)
3365 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3366 GCPRO1 (form);
3367 form = safe_eval (form);
3368 UNGCPRO;
3369 unbind_to (count, Qnil);
3370 }
3371
3372 if (NILP (form))
3373 return 0;
3374
3375 if (CONSP (prop)
3376 && EQ (XCAR (prop), Qheight)
3377 && CONSP (XCDR (prop)))
3378 {
3379 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3380 return 0;
3381
3382 /* `(height HEIGHT)'. */
3383 it->font_height = XCAR (XCDR (prop));
3384 if (!NILP (it->font_height))
3385 {
3386 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3387 int new_height = -1;
3388
3389 if (CONSP (it->font_height)
3390 && (EQ (XCAR (it->font_height), Qplus)
3391 || EQ (XCAR (it->font_height), Qminus))
3392 && CONSP (XCDR (it->font_height))
3393 && INTEGERP (XCAR (XCDR (it->font_height))))
3394 {
3395 /* `(+ N)' or `(- N)' where N is an integer. */
3396 int steps = XINT (XCAR (XCDR (it->font_height)));
3397 if (EQ (XCAR (it->font_height), Qplus))
3398 steps = - steps;
3399 it->face_id = smaller_face (it->f, it->face_id, steps);
3400 }
3401 else if (FUNCTIONP (it->font_height))
3402 {
3403 /* Call function with current height as argument.
3404 Value is the new height. */
3405 Lisp_Object height;
3406 height = safe_call1 (it->font_height,
3407 face->lface[LFACE_HEIGHT_INDEX]);
3408 if (NUMBERP (height))
3409 new_height = XFLOATINT (height);
3410 }
3411 else if (NUMBERP (it->font_height))
3412 {
3413 /* Value is a multiple of the canonical char height. */
3414 struct face *face;
3415
3416 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3417 new_height = (XFLOATINT (it->font_height)
3418 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3419 }
3420 else
3421 {
3422 /* Evaluate IT->font_height with `height' bound to the
3423 current specified height to get the new height. */
3424 Lisp_Object value;
3425 int count = SPECPDL_INDEX ();
3426
3427 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3428 value = safe_eval (it->font_height);
3429 unbind_to (count, Qnil);
3430
3431 if (NUMBERP (value))
3432 new_height = XFLOATINT (value);
3433 }
3434
3435 if (new_height > 0)
3436 it->face_id = face_with_height (it->f, it->face_id, new_height);
3437 }
3438 }
3439 else if (CONSP (prop)
3440 && EQ (XCAR (prop), Qspace_width)
3441 && CONSP (XCDR (prop)))
3442 {
3443 /* `(space_width WIDTH)'. */
3444 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3445 return 0;
3446
3447 value = XCAR (XCDR (prop));
3448 if (NUMBERP (value) && XFLOATINT (value) > 0)
3449 it->space_width = value;
3450 }
3451 else if (CONSP (prop)
3452 && EQ (XCAR (prop), Qraise)
3453 && CONSP (XCDR (prop)))
3454 {
3455 /* `(raise FACTOR)'. */
3456 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3457 return 0;
3458
3459 #ifdef HAVE_WINDOW_SYSTEM
3460 value = XCAR (XCDR (prop));
3461 if (NUMBERP (value))
3462 {
3463 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3464 it->voffset = - (XFLOATINT (value)
3465 * (FONT_HEIGHT (face->font)));
3466 }
3467 #endif /* HAVE_WINDOW_SYSTEM */
3468 }
3469 else if (!it->string_from_display_prop_p)
3470 {
3471 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3472 VALUE) or `((margin nil) VALUE)' or VALUE. */
3473 Lisp_Object location, value;
3474 struct text_pos start_pos;
3475 int valid_p;
3476
3477 /* Characters having this form of property are not displayed, so
3478 we have to find the end of the property. */
3479 start_pos = *position;
3480 *position = display_prop_end (it, object, start_pos);
3481 value = Qnil;
3482
3483 /* Let's stop at the new position and assume that all
3484 text properties change there. */
3485 it->stop_charpos = position->charpos;
3486
3487 location = Qunbound;
3488 if (CONSP (prop) && CONSP (XCAR (prop)))
3489 {
3490 Lisp_Object tem;
3491
3492 value = XCDR (prop);
3493 if (CONSP (value))
3494 value = XCAR (value);
3495
3496 tem = XCAR (prop);
3497 if (EQ (XCAR (tem), Qmargin)
3498 && (tem = XCDR (tem),
3499 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3500 (NILP (tem)
3501 || EQ (tem, Qleft_margin)
3502 || EQ (tem, Qright_margin))))
3503 location = tem;
3504 }
3505
3506 if (EQ (location, Qunbound))
3507 {
3508 location = Qnil;
3509 value = prop;
3510 }
3511
3512 #ifdef HAVE_WINDOW_SYSTEM
3513 if (FRAME_TERMCAP_P (it->f))
3514 valid_p = STRINGP (value);
3515 else
3516 valid_p = (STRINGP (value)
3517 || (CONSP (value) && EQ (XCAR (value), Qspace))
3518 || valid_image_p (value));
3519 #else /* not HAVE_WINDOW_SYSTEM */
3520 valid_p = STRINGP (value);
3521 #endif /* not HAVE_WINDOW_SYSTEM */
3522
3523 if ((EQ (location, Qleft_margin)
3524 || EQ (location, Qright_margin)
3525 || NILP (location))
3526 && valid_p
3527 && !display_replaced_before_p)
3528 {
3529 replaces_text_display_p = 1;
3530
3531 /* Save current settings of IT so that we can restore them
3532 when we are finished with the glyph property value. */
3533 push_it (it);
3534
3535 if (NILP (location))
3536 it->area = TEXT_AREA;
3537 else if (EQ (location, Qleft_margin))
3538 it->area = LEFT_MARGIN_AREA;
3539 else
3540 it->area = RIGHT_MARGIN_AREA;
3541
3542 if (STRINGP (value))
3543 {
3544 it->string = value;
3545 it->multibyte_p = STRING_MULTIBYTE (it->string);
3546 it->current.overlay_string_index = -1;
3547 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3548 it->end_charpos = it->string_nchars = SCHARS (it->string);
3549 it->method = next_element_from_string;
3550 it->stop_charpos = 0;
3551 it->string_from_display_prop_p = 1;
3552 /* Say that we haven't consumed the characters with
3553 `display' property yet. The call to pop_it in
3554 set_iterator_to_next will clean this up. */
3555 *position = start_pos;
3556 }
3557 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3558 {
3559 it->method = next_element_from_stretch;
3560 it->object = value;
3561 it->current.pos = it->position = start_pos;
3562 }
3563 #ifdef HAVE_WINDOW_SYSTEM
3564 else
3565 {
3566 it->what = IT_IMAGE;
3567 it->image_id = lookup_image (it->f, value);
3568 it->position = start_pos;
3569 it->object = NILP (object) ? it->w->buffer : object;
3570 it->method = next_element_from_image;
3571
3572 /* Say that we haven't consumed the characters with
3573 `display' property yet. The call to pop_it in
3574 set_iterator_to_next will clean this up. */
3575 *position = start_pos;
3576 }
3577 #endif /* HAVE_WINDOW_SYSTEM */
3578 }
3579 else
3580 /* Invalid property or property not supported. Restore
3581 the position to what it was before. */
3582 *position = start_pos;
3583 }
3584
3585 return replaces_text_display_p;
3586 }
3587
3588
3589 /* Check if PROP is a display sub-property value whose text should be
3590 treated as intangible. */
3591
3592 static int
3593 single_display_prop_intangible_p (prop)
3594 Lisp_Object prop;
3595 {
3596 /* Skip over `when FORM'. */
3597 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3598 {
3599 prop = XCDR (prop);
3600 if (!CONSP (prop))
3601 return 0;
3602 prop = XCDR (prop);
3603 }
3604
3605 if (STRINGP (prop))
3606 return 1;
3607
3608 if (!CONSP (prop))
3609 return 0;
3610
3611 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3612 we don't need to treat text as intangible. */
3613 if (EQ (XCAR (prop), Qmargin))
3614 {
3615 prop = XCDR (prop);
3616 if (!CONSP (prop))
3617 return 0;
3618
3619 prop = XCDR (prop);
3620 if (!CONSP (prop)
3621 || EQ (XCAR (prop), Qleft_margin)
3622 || EQ (XCAR (prop), Qright_margin))
3623 return 0;
3624 }
3625
3626 return (CONSP (prop)
3627 && (EQ (XCAR (prop), Qimage)
3628 || EQ (XCAR (prop), Qspace)));
3629 }
3630
3631
3632 /* Check if PROP is a display property value whose text should be
3633 treated as intangible. */
3634
3635 int
3636 display_prop_intangible_p (prop)
3637 Lisp_Object prop;
3638 {
3639 if (CONSP (prop)
3640 && CONSP (XCAR (prop))
3641 && !EQ (Qmargin, XCAR (XCAR (prop))))
3642 {
3643 /* A list of sub-properties. */
3644 while (CONSP (prop))
3645 {
3646 if (single_display_prop_intangible_p (XCAR (prop)))
3647 return 1;
3648 prop = XCDR (prop);
3649 }
3650 }
3651 else if (VECTORP (prop))
3652 {
3653 /* A vector of sub-properties. */
3654 int i;
3655 for (i = 0; i < ASIZE (prop); ++i)
3656 if (single_display_prop_intangible_p (AREF (prop, i)))
3657 return 1;
3658 }
3659 else
3660 return single_display_prop_intangible_p (prop);
3661
3662 return 0;
3663 }
3664
3665
3666 /* Return 1 if PROP is a display sub-property value containing STRING. */
3667
3668 static int
3669 single_display_prop_string_p (prop, string)
3670 Lisp_Object prop, string;
3671 {
3672 if (EQ (string, prop))
3673 return 1;
3674
3675 /* Skip over `when FORM'. */
3676 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3677 {
3678 prop = XCDR (prop);
3679 if (!CONSP (prop))
3680 return 0;
3681 prop = XCDR (prop);
3682 }
3683
3684 if (CONSP (prop))
3685 /* Skip over `margin LOCATION'. */
3686 if (EQ (XCAR (prop), Qmargin))
3687 {
3688 prop = XCDR (prop);
3689 if (!CONSP (prop))
3690 return 0;
3691
3692 prop = XCDR (prop);
3693 if (!CONSP (prop))
3694 return 0;
3695 }
3696
3697 return CONSP (prop) && EQ (XCAR (prop), string);
3698 }
3699
3700
3701 /* Return 1 if STRING appears in the `display' property PROP. */
3702
3703 static int
3704 display_prop_string_p (prop, string)
3705 Lisp_Object prop, string;
3706 {
3707 if (CONSP (prop)
3708 && CONSP (XCAR (prop))
3709 && !EQ (Qmargin, XCAR (XCAR (prop))))
3710 {
3711 /* A list of sub-properties. */
3712 while (CONSP (prop))
3713 {
3714 if (single_display_prop_string_p (XCAR (prop), string))
3715 return 1;
3716 prop = XCDR (prop);
3717 }
3718 }
3719 else if (VECTORP (prop))
3720 {
3721 /* A vector of sub-properties. */
3722 int i;
3723 for (i = 0; i < ASIZE (prop); ++i)
3724 if (single_display_prop_string_p (AREF (prop, i), string))
3725 return 1;
3726 }
3727 else
3728 return single_display_prop_string_p (prop, string);
3729
3730 return 0;
3731 }
3732
3733
3734 /* Determine from which buffer position in W's buffer STRING comes
3735 from. AROUND_CHARPOS is an approximate position where it could
3736 be from. Value is the buffer position or 0 if it couldn't be
3737 determined.
3738
3739 W's buffer must be current.
3740
3741 This function is necessary because we don't record buffer positions
3742 in glyphs generated from strings (to keep struct glyph small).
3743 This function may only use code that doesn't eval because it is
3744 called asynchronously from note_mouse_highlight. */
3745
3746 int
3747 string_buffer_position (w, string, around_charpos)
3748 struct window *w;
3749 Lisp_Object string;
3750 int around_charpos;
3751 {
3752 Lisp_Object limit, prop, pos;
3753 const int MAX_DISTANCE = 1000;
3754 int found = 0;
3755
3756 pos = make_number (around_charpos);
3757 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3758 while (!found && !EQ (pos, limit))
3759 {
3760 prop = Fget_char_property (pos, Qdisplay, Qnil);
3761 if (!NILP (prop) && display_prop_string_p (prop, string))
3762 found = 1;
3763 else
3764 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3765 }
3766
3767 if (!found)
3768 {
3769 pos = make_number (around_charpos);
3770 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3771 while (!found && !EQ (pos, limit))
3772 {
3773 prop = Fget_char_property (pos, Qdisplay, Qnil);
3774 if (!NILP (prop) && display_prop_string_p (prop, string))
3775 found = 1;
3776 else
3777 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3778 limit);
3779 }
3780 }
3781
3782 return found ? XINT (pos) : 0;
3783 }
3784
3785
3786 \f
3787 /***********************************************************************
3788 `composition' property
3789 ***********************************************************************/
3790
3791 /* Set up iterator IT from `composition' property at its current
3792 position. Called from handle_stop. */
3793
3794 static enum prop_handled
3795 handle_composition_prop (it)
3796 struct it *it;
3797 {
3798 Lisp_Object prop, string;
3799 int pos, pos_byte, end;
3800 enum prop_handled handled = HANDLED_NORMALLY;
3801
3802 if (STRINGP (it->string))
3803 {
3804 pos = IT_STRING_CHARPOS (*it);
3805 pos_byte = IT_STRING_BYTEPOS (*it);
3806 string = it->string;
3807 }
3808 else
3809 {
3810 pos = IT_CHARPOS (*it);
3811 pos_byte = IT_BYTEPOS (*it);
3812 string = Qnil;
3813 }
3814
3815 /* If there's a valid composition and point is not inside of the
3816 composition (in the case that the composition is from the current
3817 buffer), draw a glyph composed from the composition components. */
3818 if (find_composition (pos, -1, &pos, &end, &prop, string)
3819 && COMPOSITION_VALID_P (pos, end, prop)
3820 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3821 {
3822 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3823
3824 if (id >= 0)
3825 {
3826 it->method = next_element_from_composition;
3827 it->cmp_id = id;
3828 it->cmp_len = COMPOSITION_LENGTH (prop);
3829 /* For a terminal, draw only the first character of the
3830 components. */
3831 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3832 it->len = (STRINGP (it->string)
3833 ? string_char_to_byte (it->string, end)
3834 : CHAR_TO_BYTE (end)) - pos_byte;
3835 it->stop_charpos = end;
3836 handled = HANDLED_RETURN;
3837 }
3838 }
3839
3840 return handled;
3841 }
3842
3843
3844 \f
3845 /***********************************************************************
3846 Overlay strings
3847 ***********************************************************************/
3848
3849 /* The following structure is used to record overlay strings for
3850 later sorting in load_overlay_strings. */
3851
3852 struct overlay_entry
3853 {
3854 Lisp_Object overlay;
3855 Lisp_Object string;
3856 int priority;
3857 int after_string_p;
3858 };
3859
3860
3861 /* Set up iterator IT from overlay strings at its current position.
3862 Called from handle_stop. */
3863
3864 static enum prop_handled
3865 handle_overlay_change (it)
3866 struct it *it;
3867 {
3868 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3869 return HANDLED_RECOMPUTE_PROPS;
3870 else
3871 return HANDLED_NORMALLY;
3872 }
3873
3874
3875 /* Set up the next overlay string for delivery by IT, if there is an
3876 overlay string to deliver. Called by set_iterator_to_next when the
3877 end of the current overlay string is reached. If there are more
3878 overlay strings to display, IT->string and
3879 IT->current.overlay_string_index are set appropriately here.
3880 Otherwise IT->string is set to nil. */
3881
3882 static void
3883 next_overlay_string (it)
3884 struct it *it;
3885 {
3886 ++it->current.overlay_string_index;
3887 if (it->current.overlay_string_index == it->n_overlay_strings)
3888 {
3889 /* No more overlay strings. Restore IT's settings to what
3890 they were before overlay strings were processed, and
3891 continue to deliver from current_buffer. */
3892 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3893
3894 pop_it (it);
3895 xassert (it->stop_charpos >= BEGV
3896 && it->stop_charpos <= it->end_charpos);
3897 it->string = Qnil;
3898 it->current.overlay_string_index = -1;
3899 SET_TEXT_POS (it->current.string_pos, -1, -1);
3900 it->n_overlay_strings = 0;
3901 it->method = next_element_from_buffer;
3902
3903 /* If we're at the end of the buffer, record that we have
3904 processed the overlay strings there already, so that
3905 next_element_from_buffer doesn't try it again. */
3906 if (IT_CHARPOS (*it) >= it->end_charpos)
3907 it->overlay_strings_at_end_processed_p = 1;
3908
3909 /* If we have to display `...' for invisible text, set
3910 the iterator up for that. */
3911 if (display_ellipsis_p)
3912 setup_for_ellipsis (it);
3913 }
3914 else
3915 {
3916 /* There are more overlay strings to process. If
3917 IT->current.overlay_string_index has advanced to a position
3918 where we must load IT->overlay_strings with more strings, do
3919 it. */
3920 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3921
3922 if (it->current.overlay_string_index && i == 0)
3923 load_overlay_strings (it, 0);
3924
3925 /* Initialize IT to deliver display elements from the overlay
3926 string. */
3927 it->string = it->overlay_strings[i];
3928 it->multibyte_p = STRING_MULTIBYTE (it->string);
3929 SET_TEXT_POS (it->current.string_pos, 0, 0);
3930 it->method = next_element_from_string;
3931 it->stop_charpos = 0;
3932 }
3933
3934 CHECK_IT (it);
3935 }
3936
3937
3938 /* Compare two overlay_entry structures E1 and E2. Used as a
3939 comparison function for qsort in load_overlay_strings. Overlay
3940 strings for the same position are sorted so that
3941
3942 1. All after-strings come in front of before-strings, except
3943 when they come from the same overlay.
3944
3945 2. Within after-strings, strings are sorted so that overlay strings
3946 from overlays with higher priorities come first.
3947
3948 2. Within before-strings, strings are sorted so that overlay
3949 strings from overlays with higher priorities come last.
3950
3951 Value is analogous to strcmp. */
3952
3953
3954 static int
3955 compare_overlay_entries (e1, e2)
3956 void *e1, *e2;
3957 {
3958 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3959 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3960 int result;
3961
3962 if (entry1->after_string_p != entry2->after_string_p)
3963 {
3964 /* Let after-strings appear in front of before-strings if
3965 they come from different overlays. */
3966 if (EQ (entry1->overlay, entry2->overlay))
3967 result = entry1->after_string_p ? 1 : -1;
3968 else
3969 result = entry1->after_string_p ? -1 : 1;
3970 }
3971 else if (entry1->after_string_p)
3972 /* After-strings sorted in order of decreasing priority. */
3973 result = entry2->priority - entry1->priority;
3974 else
3975 /* Before-strings sorted in order of increasing priority. */
3976 result = entry1->priority - entry2->priority;
3977
3978 return result;
3979 }
3980
3981
3982 /* Load the vector IT->overlay_strings with overlay strings from IT's
3983 current buffer position, or from CHARPOS if that is > 0. Set
3984 IT->n_overlays to the total number of overlay strings found.
3985
3986 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3987 a time. On entry into load_overlay_strings,
3988 IT->current.overlay_string_index gives the number of overlay
3989 strings that have already been loaded by previous calls to this
3990 function.
3991
3992 IT->add_overlay_start contains an additional overlay start
3993 position to consider for taking overlay strings from, if non-zero.
3994 This position comes into play when the overlay has an `invisible'
3995 property, and both before and after-strings. When we've skipped to
3996 the end of the overlay, because of its `invisible' property, we
3997 nevertheless want its before-string to appear.
3998 IT->add_overlay_start will contain the overlay start position
3999 in this case.
4000
4001 Overlay strings are sorted so that after-string strings come in
4002 front of before-string strings. Within before and after-strings,
4003 strings are sorted by overlay priority. See also function
4004 compare_overlay_entries. */
4005
4006 static void
4007 load_overlay_strings (it, charpos)
4008 struct it *it;
4009 int charpos;
4010 {
4011 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4012 Lisp_Object overlay, window, str, invisible;
4013 struct Lisp_Overlay *ov;
4014 int start, end;
4015 int size = 20;
4016 int n = 0, i, j, invis_p;
4017 struct overlay_entry *entries
4018 = (struct overlay_entry *) alloca (size * sizeof *entries);
4019
4020 if (charpos <= 0)
4021 charpos = IT_CHARPOS (*it);
4022
4023 /* Append the overlay string STRING of overlay OVERLAY to vector
4024 `entries' which has size `size' and currently contains `n'
4025 elements. AFTER_P non-zero means STRING is an after-string of
4026 OVERLAY. */
4027 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4028 do \
4029 { \
4030 Lisp_Object priority; \
4031 \
4032 if (n == size) \
4033 { \
4034 int new_size = 2 * size; \
4035 struct overlay_entry *old = entries; \
4036 entries = \
4037 (struct overlay_entry *) alloca (new_size \
4038 * sizeof *entries); \
4039 bcopy (old, entries, size * sizeof *entries); \
4040 size = new_size; \
4041 } \
4042 \
4043 entries[n].string = (STRING); \
4044 entries[n].overlay = (OVERLAY); \
4045 priority = Foverlay_get ((OVERLAY), Qpriority); \
4046 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4047 entries[n].after_string_p = (AFTER_P); \
4048 ++n; \
4049 } \
4050 while (0)
4051
4052 /* Process overlay before the overlay center. */
4053 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4054 {
4055 XSETMISC (overlay, ov);
4056 xassert (OVERLAYP (overlay));
4057 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4058 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4059
4060 if (end < charpos)
4061 break;
4062
4063 /* Skip this overlay if it doesn't start or end at IT's current
4064 position. */
4065 if (end != charpos && start != charpos)
4066 continue;
4067
4068 /* Skip this overlay if it doesn't apply to IT->w. */
4069 window = Foverlay_get (overlay, Qwindow);
4070 if (WINDOWP (window) && XWINDOW (window) != it->w)
4071 continue;
4072
4073 /* If the text ``under'' the overlay is invisible, both before-
4074 and after-strings from this overlay are visible; start and
4075 end position are indistinguishable. */
4076 invisible = Foverlay_get (overlay, Qinvisible);
4077 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4078
4079 /* If overlay has a non-empty before-string, record it. */
4080 if ((start == charpos || (end == charpos && invis_p))
4081 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4082 && SCHARS (str))
4083 RECORD_OVERLAY_STRING (overlay, str, 0);
4084
4085 /* If overlay has a non-empty after-string, record it. */
4086 if ((end == charpos || (start == charpos && invis_p))
4087 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4088 && SCHARS (str))
4089 RECORD_OVERLAY_STRING (overlay, str, 1);
4090 }
4091
4092 /* Process overlays after the overlay center. */
4093 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4094 {
4095 XSETMISC (overlay, ov);
4096 xassert (OVERLAYP (overlay));
4097 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4098 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4099
4100 if (start > charpos)
4101 break;
4102
4103 /* Skip this overlay if it doesn't start or end at IT's current
4104 position. */
4105 if (end != charpos && start != charpos)
4106 continue;
4107
4108 /* Skip this overlay if it doesn't apply to IT->w. */
4109 window = Foverlay_get (overlay, Qwindow);
4110 if (WINDOWP (window) && XWINDOW (window) != it->w)
4111 continue;
4112
4113 /* If the text ``under'' the overlay is invisible, it has a zero
4114 dimension, and both before- and after-strings apply. */
4115 invisible = Foverlay_get (overlay, Qinvisible);
4116 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4117
4118 /* If overlay has a non-empty before-string, record it. */
4119 if ((start == charpos || (end == charpos && invis_p))
4120 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4121 && SCHARS (str))
4122 RECORD_OVERLAY_STRING (overlay, str, 0);
4123
4124 /* If overlay has a non-empty after-string, record it. */
4125 if ((end == charpos || (start == charpos && invis_p))
4126 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4127 && SCHARS (str))
4128 RECORD_OVERLAY_STRING (overlay, str, 1);
4129 }
4130
4131 #undef RECORD_OVERLAY_STRING
4132
4133 /* Sort entries. */
4134 if (n > 1)
4135 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4136
4137 /* Record the total number of strings to process. */
4138 it->n_overlay_strings = n;
4139
4140 /* IT->current.overlay_string_index is the number of overlay strings
4141 that have already been consumed by IT. Copy some of the
4142 remaining overlay strings to IT->overlay_strings. */
4143 i = 0;
4144 j = it->current.overlay_string_index;
4145 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4146 it->overlay_strings[i++] = entries[j++].string;
4147
4148 CHECK_IT (it);
4149 }
4150
4151
4152 /* Get the first chunk of overlay strings at IT's current buffer
4153 position, or at CHARPOS if that is > 0. Value is non-zero if at
4154 least one overlay string was found. */
4155
4156 static int
4157 get_overlay_strings (it, charpos)
4158 struct it *it;
4159 int charpos;
4160 {
4161 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4162 process. This fills IT->overlay_strings with strings, and sets
4163 IT->n_overlay_strings to the total number of strings to process.
4164 IT->pos.overlay_string_index has to be set temporarily to zero
4165 because load_overlay_strings needs this; it must be set to -1
4166 when no overlay strings are found because a zero value would
4167 indicate a position in the first overlay string. */
4168 it->current.overlay_string_index = 0;
4169 load_overlay_strings (it, charpos);
4170
4171 /* If we found overlay strings, set up IT to deliver display
4172 elements from the first one. Otherwise set up IT to deliver
4173 from current_buffer. */
4174 if (it->n_overlay_strings)
4175 {
4176 /* Make sure we know settings in current_buffer, so that we can
4177 restore meaningful values when we're done with the overlay
4178 strings. */
4179 compute_stop_pos (it);
4180 xassert (it->face_id >= 0);
4181
4182 /* Save IT's settings. They are restored after all overlay
4183 strings have been processed. */
4184 xassert (it->sp == 0);
4185 push_it (it);
4186
4187 /* Set up IT to deliver display elements from the first overlay
4188 string. */
4189 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4190 it->string = it->overlay_strings[0];
4191 it->stop_charpos = 0;
4192 xassert (STRINGP (it->string));
4193 it->end_charpos = SCHARS (it->string);
4194 it->multibyte_p = STRING_MULTIBYTE (it->string);
4195 it->method = next_element_from_string;
4196 }
4197 else
4198 {
4199 it->string = Qnil;
4200 it->current.overlay_string_index = -1;
4201 it->method = next_element_from_buffer;
4202 }
4203
4204 CHECK_IT (it);
4205
4206 /* Value is non-zero if we found at least one overlay string. */
4207 return STRINGP (it->string);
4208 }
4209
4210
4211 \f
4212 /***********************************************************************
4213 Saving and restoring state
4214 ***********************************************************************/
4215
4216 /* Save current settings of IT on IT->stack. Called, for example,
4217 before setting up IT for an overlay string, to be able to restore
4218 IT's settings to what they were after the overlay string has been
4219 processed. */
4220
4221 static void
4222 push_it (it)
4223 struct it *it;
4224 {
4225 struct iterator_stack_entry *p;
4226
4227 xassert (it->sp < 2);
4228 p = it->stack + it->sp;
4229
4230 p->stop_charpos = it->stop_charpos;
4231 xassert (it->face_id >= 0);
4232 p->face_id = it->face_id;
4233 p->string = it->string;
4234 p->pos = it->current;
4235 p->end_charpos = it->end_charpos;
4236 p->string_nchars = it->string_nchars;
4237 p->area = it->area;
4238 p->multibyte_p = it->multibyte_p;
4239 p->space_width = it->space_width;
4240 p->font_height = it->font_height;
4241 p->voffset = it->voffset;
4242 p->string_from_display_prop_p = it->string_from_display_prop_p;
4243 p->display_ellipsis_p = 0;
4244 ++it->sp;
4245 }
4246
4247
4248 /* Restore IT's settings from IT->stack. Called, for example, when no
4249 more overlay strings must be processed, and we return to delivering
4250 display elements from a buffer, or when the end of a string from a
4251 `display' property is reached and we return to delivering display
4252 elements from an overlay string, or from a buffer. */
4253
4254 static void
4255 pop_it (it)
4256 struct it *it;
4257 {
4258 struct iterator_stack_entry *p;
4259
4260 xassert (it->sp > 0);
4261 --it->sp;
4262 p = it->stack + it->sp;
4263 it->stop_charpos = p->stop_charpos;
4264 it->face_id = p->face_id;
4265 it->string = p->string;
4266 it->current = p->pos;
4267 it->end_charpos = p->end_charpos;
4268 it->string_nchars = p->string_nchars;
4269 it->area = p->area;
4270 it->multibyte_p = p->multibyte_p;
4271 it->space_width = p->space_width;
4272 it->font_height = p->font_height;
4273 it->voffset = p->voffset;
4274 it->string_from_display_prop_p = p->string_from_display_prop_p;
4275 }
4276
4277
4278 \f
4279 /***********************************************************************
4280 Moving over lines
4281 ***********************************************************************/
4282
4283 /* Set IT's current position to the previous line start. */
4284
4285 static void
4286 back_to_previous_line_start (it)
4287 struct it *it;
4288 {
4289 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4290 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4291 }
4292
4293
4294 /* Move IT to the next line start.
4295
4296 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4297 we skipped over part of the text (as opposed to moving the iterator
4298 continuously over the text). Otherwise, don't change the value
4299 of *SKIPPED_P.
4300
4301 Newlines may come from buffer text, overlay strings, or strings
4302 displayed via the `display' property. That's the reason we can't
4303 simply use find_next_newline_no_quit.
4304
4305 Note that this function may not skip over invisible text that is so
4306 because of text properties and immediately follows a newline. If
4307 it would, function reseat_at_next_visible_line_start, when called
4308 from set_iterator_to_next, would effectively make invisible
4309 characters following a newline part of the wrong glyph row, which
4310 leads to wrong cursor motion. */
4311
4312 static int
4313 forward_to_next_line_start (it, skipped_p)
4314 struct it *it;
4315 int *skipped_p;
4316 {
4317 int old_selective, newline_found_p, n;
4318 const int MAX_NEWLINE_DISTANCE = 500;
4319
4320 /* If already on a newline, just consume it to avoid unintended
4321 skipping over invisible text below. */
4322 if (it->what == IT_CHARACTER
4323 && it->c == '\n'
4324 && CHARPOS (it->position) == IT_CHARPOS (*it))
4325 {
4326 set_iterator_to_next (it, 0);
4327 it->c = 0;
4328 return 1;
4329 }
4330
4331 /* Don't handle selective display in the following. It's (a)
4332 unnecessary because it's done by the caller, and (b) leads to an
4333 infinite recursion because next_element_from_ellipsis indirectly
4334 calls this function. */
4335 old_selective = it->selective;
4336 it->selective = 0;
4337
4338 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4339 from buffer text. */
4340 for (n = newline_found_p = 0;
4341 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4342 n += STRINGP (it->string) ? 0 : 1)
4343 {
4344 if (!get_next_display_element (it))
4345 return 0;
4346 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4347 set_iterator_to_next (it, 0);
4348 }
4349
4350 /* If we didn't find a newline near enough, see if we can use a
4351 short-cut. */
4352 if (!newline_found_p)
4353 {
4354 int start = IT_CHARPOS (*it);
4355 int limit = find_next_newline_no_quit (start, 1);
4356 Lisp_Object pos;
4357
4358 xassert (!STRINGP (it->string));
4359
4360 /* If there isn't any `display' property in sight, and no
4361 overlays, we can just use the position of the newline in
4362 buffer text. */
4363 if (it->stop_charpos >= limit
4364 || ((pos = Fnext_single_property_change (make_number (start),
4365 Qdisplay,
4366 Qnil, make_number (limit)),
4367 NILP (pos))
4368 && next_overlay_change (start) == ZV))
4369 {
4370 IT_CHARPOS (*it) = limit;
4371 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4372 *skipped_p = newline_found_p = 1;
4373 }
4374 else
4375 {
4376 while (get_next_display_element (it)
4377 && !newline_found_p)
4378 {
4379 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4380 set_iterator_to_next (it, 0);
4381 }
4382 }
4383 }
4384
4385 it->selective = old_selective;
4386 return newline_found_p;
4387 }
4388
4389
4390 /* Set IT's current position to the previous visible line start. Skip
4391 invisible text that is so either due to text properties or due to
4392 selective display. Caution: this does not change IT->current_x and
4393 IT->hpos. */
4394
4395 static void
4396 back_to_previous_visible_line_start (it)
4397 struct it *it;
4398 {
4399 int visible_p = 0;
4400
4401 /* Go back one newline if not on BEGV already. */
4402 if (IT_CHARPOS (*it) > BEGV)
4403 back_to_previous_line_start (it);
4404
4405 /* Move over lines that are invisible because of selective display
4406 or text properties. */
4407 while (IT_CHARPOS (*it) > BEGV
4408 && !visible_p)
4409 {
4410 visible_p = 1;
4411
4412 /* If selective > 0, then lines indented more than that values
4413 are invisible. */
4414 if (it->selective > 0
4415 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4416 (double) it->selective)) /* iftc */
4417 visible_p = 0;
4418 else
4419 {
4420 Lisp_Object prop;
4421
4422 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4423 Qinvisible, it->window);
4424 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4425 visible_p = 0;
4426 }
4427
4428 /* Back one more newline if the current one is invisible. */
4429 if (!visible_p)
4430 back_to_previous_line_start (it);
4431 }
4432
4433 xassert (IT_CHARPOS (*it) >= BEGV);
4434 xassert (IT_CHARPOS (*it) == BEGV
4435 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4436 CHECK_IT (it);
4437 }
4438
4439
4440 /* Reseat iterator IT at the previous visible line start. Skip
4441 invisible text that is so either due to text properties or due to
4442 selective display. At the end, update IT's overlay information,
4443 face information etc. */
4444
4445 static void
4446 reseat_at_previous_visible_line_start (it)
4447 struct it *it;
4448 {
4449 back_to_previous_visible_line_start (it);
4450 reseat (it, it->current.pos, 1);
4451 CHECK_IT (it);
4452 }
4453
4454
4455 /* Reseat iterator IT on the next visible line start in the current
4456 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4457 preceding the line start. Skip over invisible text that is so
4458 because of selective display. Compute faces, overlays etc at the
4459 new position. Note that this function does not skip over text that
4460 is invisible because of text properties. */
4461
4462 static void
4463 reseat_at_next_visible_line_start (it, on_newline_p)
4464 struct it *it;
4465 int on_newline_p;
4466 {
4467 int newline_found_p, skipped_p = 0;
4468
4469 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4470
4471 /* Skip over lines that are invisible because they are indented
4472 more than the value of IT->selective. */
4473 if (it->selective > 0)
4474 while (IT_CHARPOS (*it) < ZV
4475 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4476 (double) it->selective)) /* iftc */
4477 {
4478 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4479 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4480 }
4481
4482 /* Position on the newline if that's what's requested. */
4483 if (on_newline_p && newline_found_p)
4484 {
4485 if (STRINGP (it->string))
4486 {
4487 if (IT_STRING_CHARPOS (*it) > 0)
4488 {
4489 --IT_STRING_CHARPOS (*it);
4490 --IT_STRING_BYTEPOS (*it);
4491 }
4492 }
4493 else if (IT_CHARPOS (*it) > BEGV)
4494 {
4495 --IT_CHARPOS (*it);
4496 --IT_BYTEPOS (*it);
4497 reseat (it, it->current.pos, 0);
4498 }
4499 }
4500 else if (skipped_p)
4501 reseat (it, it->current.pos, 0);
4502
4503 CHECK_IT (it);
4504 }
4505
4506
4507 \f
4508 /***********************************************************************
4509 Changing an iterator's position
4510 ***********************************************************************/
4511
4512 /* Change IT's current position to POS in current_buffer. If FORCE_P
4513 is non-zero, always check for text properties at the new position.
4514 Otherwise, text properties are only looked up if POS >=
4515 IT->check_charpos of a property. */
4516
4517 static void
4518 reseat (it, pos, force_p)
4519 struct it *it;
4520 struct text_pos pos;
4521 int force_p;
4522 {
4523 int original_pos = IT_CHARPOS (*it);
4524
4525 reseat_1 (it, pos, 0);
4526
4527 /* Determine where to check text properties. Avoid doing it
4528 where possible because text property lookup is very expensive. */
4529 if (force_p
4530 || CHARPOS (pos) > it->stop_charpos
4531 || CHARPOS (pos) < original_pos)
4532 handle_stop (it);
4533
4534 CHECK_IT (it);
4535 }
4536
4537
4538 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4539 IT->stop_pos to POS, also. */
4540
4541 static void
4542 reseat_1 (it, pos, set_stop_p)
4543 struct it *it;
4544 struct text_pos pos;
4545 int set_stop_p;
4546 {
4547 /* Don't call this function when scanning a C string. */
4548 xassert (it->s == NULL);
4549
4550 /* POS must be a reasonable value. */
4551 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4552
4553 it->current.pos = it->position = pos;
4554 XSETBUFFER (it->object, current_buffer);
4555 it->end_charpos = ZV;
4556 it->dpvec = NULL;
4557 it->current.dpvec_index = -1;
4558 it->current.overlay_string_index = -1;
4559 IT_STRING_CHARPOS (*it) = -1;
4560 IT_STRING_BYTEPOS (*it) = -1;
4561 it->string = Qnil;
4562 it->method = next_element_from_buffer;
4563 /* RMS: I added this to fix a bug in move_it_vertically_backward
4564 where it->area continued to relate to the starting point
4565 for the backward motion. Bug report from
4566 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4567 However, I am not sure whether reseat still does the right thing
4568 in general after this change. */
4569 it->area = TEXT_AREA;
4570 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4571 it->sp = 0;
4572 it->face_before_selective_p = 0;
4573
4574 if (set_stop_p)
4575 it->stop_charpos = CHARPOS (pos);
4576 }
4577
4578
4579 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4580 If S is non-null, it is a C string to iterate over. Otherwise,
4581 STRING gives a Lisp string to iterate over.
4582
4583 If PRECISION > 0, don't return more then PRECISION number of
4584 characters from the string.
4585
4586 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4587 characters have been returned. FIELD_WIDTH < 0 means an infinite
4588 field width.
4589
4590 MULTIBYTE = 0 means disable processing of multibyte characters,
4591 MULTIBYTE > 0 means enable it,
4592 MULTIBYTE < 0 means use IT->multibyte_p.
4593
4594 IT must be initialized via a prior call to init_iterator before
4595 calling this function. */
4596
4597 static void
4598 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4599 struct it *it;
4600 unsigned char *s;
4601 Lisp_Object string;
4602 int charpos;
4603 int precision, field_width, multibyte;
4604 {
4605 /* No region in strings. */
4606 it->region_beg_charpos = it->region_end_charpos = -1;
4607
4608 /* No text property checks performed by default, but see below. */
4609 it->stop_charpos = -1;
4610
4611 /* Set iterator position and end position. */
4612 bzero (&it->current, sizeof it->current);
4613 it->current.overlay_string_index = -1;
4614 it->current.dpvec_index = -1;
4615 xassert (charpos >= 0);
4616
4617 /* If STRING is specified, use its multibyteness, otherwise use the
4618 setting of MULTIBYTE, if specified. */
4619 if (multibyte >= 0)
4620 it->multibyte_p = multibyte > 0;
4621
4622 if (s == NULL)
4623 {
4624 xassert (STRINGP (string));
4625 it->string = string;
4626 it->s = NULL;
4627 it->end_charpos = it->string_nchars = SCHARS (string);
4628 it->method = next_element_from_string;
4629 it->current.string_pos = string_pos (charpos, string);
4630 }
4631 else
4632 {
4633 it->s = s;
4634 it->string = Qnil;
4635
4636 /* Note that we use IT->current.pos, not it->current.string_pos,
4637 for displaying C strings. */
4638 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4639 if (it->multibyte_p)
4640 {
4641 it->current.pos = c_string_pos (charpos, s, 1);
4642 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4643 }
4644 else
4645 {
4646 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4647 it->end_charpos = it->string_nchars = strlen (s);
4648 }
4649
4650 it->method = next_element_from_c_string;
4651 }
4652
4653 /* PRECISION > 0 means don't return more than PRECISION characters
4654 from the string. */
4655 if (precision > 0 && it->end_charpos - charpos > precision)
4656 it->end_charpos = it->string_nchars = charpos + precision;
4657
4658 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4659 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4660 FIELD_WIDTH < 0 means infinite field width. This is useful for
4661 padding with `-' at the end of a mode line. */
4662 if (field_width < 0)
4663 field_width = INFINITY;
4664 if (field_width > it->end_charpos - charpos)
4665 it->end_charpos = charpos + field_width;
4666
4667 /* Use the standard display table for displaying strings. */
4668 if (DISP_TABLE_P (Vstandard_display_table))
4669 it->dp = XCHAR_TABLE (Vstandard_display_table);
4670
4671 it->stop_charpos = charpos;
4672 CHECK_IT (it);
4673 }
4674
4675
4676 \f
4677 /***********************************************************************
4678 Iteration
4679 ***********************************************************************/
4680
4681 /* Load IT's display element fields with information about the next
4682 display element from the current position of IT. Value is zero if
4683 end of buffer (or C string) is reached. */
4684
4685 int
4686 get_next_display_element (it)
4687 struct it *it;
4688 {
4689 /* Non-zero means that we found a display element. Zero means that
4690 we hit the end of what we iterate over. Performance note: the
4691 function pointer `method' used here turns out to be faster than
4692 using a sequence of if-statements. */
4693 int success_p = (*it->method) (it);
4694
4695 if (it->what == IT_CHARACTER)
4696 {
4697 /* Map via display table or translate control characters.
4698 IT->c, IT->len etc. have been set to the next character by
4699 the function call above. If we have a display table, and it
4700 contains an entry for IT->c, translate it. Don't do this if
4701 IT->c itself comes from a display table, otherwise we could
4702 end up in an infinite recursion. (An alternative could be to
4703 count the recursion depth of this function and signal an
4704 error when a certain maximum depth is reached.) Is it worth
4705 it? */
4706 if (success_p && it->dpvec == NULL)
4707 {
4708 Lisp_Object dv;
4709
4710 if (it->dp
4711 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4712 VECTORP (dv)))
4713 {
4714 struct Lisp_Vector *v = XVECTOR (dv);
4715
4716 /* Return the first character from the display table
4717 entry, if not empty. If empty, don't display the
4718 current character. */
4719 if (v->size)
4720 {
4721 it->dpvec_char_len = it->len;
4722 it->dpvec = v->contents;
4723 it->dpend = v->contents + v->size;
4724 it->current.dpvec_index = 0;
4725 it->method = next_element_from_display_vector;
4726 success_p = get_next_display_element (it);
4727 }
4728 else
4729 {
4730 set_iterator_to_next (it, 0);
4731 success_p = get_next_display_element (it);
4732 }
4733 }
4734
4735 /* Translate control characters into `\003' or `^C' form.
4736 Control characters coming from a display table entry are
4737 currently not translated because we use IT->dpvec to hold
4738 the translation. This could easily be changed but I
4739 don't believe that it is worth doing.
4740
4741 If it->multibyte_p is nonzero, eight-bit characters and
4742 non-printable multibyte characters are also translated to
4743 octal form.
4744
4745 If it->multibyte_p is zero, eight-bit characters that
4746 don't have corresponding multibyte char code are also
4747 translated to octal form. */
4748 else if ((it->c < ' '
4749 && (it->area != TEXT_AREA
4750 || (it->c != '\n' && it->c != '\t')))
4751 || (it->multibyte_p
4752 ? ((it->c >= 127
4753 && it->len == 1)
4754 || !CHAR_PRINTABLE_P (it->c))
4755 : (it->c >= 127
4756 && it->c == unibyte_char_to_multibyte (it->c))))
4757 {
4758 /* IT->c is a control character which must be displayed
4759 either as '\003' or as `^C' where the '\\' and '^'
4760 can be defined in the display table. Fill
4761 IT->ctl_chars with glyphs for what we have to
4762 display. Then, set IT->dpvec to these glyphs. */
4763 GLYPH g;
4764
4765 if (it->c < 128 && it->ctl_arrow_p)
4766 {
4767 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4768 if (it->dp
4769 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4770 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4771 g = XINT (DISP_CTRL_GLYPH (it->dp));
4772 else
4773 g = FAST_MAKE_GLYPH ('^', 0);
4774 XSETINT (it->ctl_chars[0], g);
4775
4776 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4777 XSETINT (it->ctl_chars[1], g);
4778
4779 /* Set up IT->dpvec and return first character from it. */
4780 it->dpvec_char_len = it->len;
4781 it->dpvec = it->ctl_chars;
4782 it->dpend = it->dpvec + 2;
4783 it->current.dpvec_index = 0;
4784 it->method = next_element_from_display_vector;
4785 get_next_display_element (it);
4786 }
4787 else
4788 {
4789 unsigned char str[MAX_MULTIBYTE_LENGTH];
4790 int len;
4791 int i;
4792 GLYPH escape_glyph;
4793
4794 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4795 if (it->dp
4796 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4797 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4798 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4799 else
4800 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4801
4802 if (SINGLE_BYTE_CHAR_P (it->c))
4803 str[0] = it->c, len = 1;
4804 else
4805 {
4806 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4807 if (len < 0)
4808 {
4809 /* It's an invalid character, which
4810 shouldn't happen actually, but due to
4811 bugs it may happen. Let's print the char
4812 as is, there's not much meaningful we can
4813 do with it. */
4814 str[0] = it->c;
4815 str[1] = it->c >> 8;
4816 str[2] = it->c >> 16;
4817 str[3] = it->c >> 24;
4818 len = 4;
4819 }
4820 }
4821
4822 for (i = 0; i < len; i++)
4823 {
4824 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4825 /* Insert three more glyphs into IT->ctl_chars for
4826 the octal display of the character. */
4827 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4828 XSETINT (it->ctl_chars[i * 4 + 1], g);
4829 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4830 XSETINT (it->ctl_chars[i * 4 + 2], g);
4831 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4832 XSETINT (it->ctl_chars[i * 4 + 3], g);
4833 }
4834
4835 /* Set up IT->dpvec and return the first character
4836 from it. */
4837 it->dpvec_char_len = it->len;
4838 it->dpvec = it->ctl_chars;
4839 it->dpend = it->dpvec + len * 4;
4840 it->current.dpvec_index = 0;
4841 it->method = next_element_from_display_vector;
4842 get_next_display_element (it);
4843 }
4844 }
4845 }
4846
4847 /* Adjust face id for a multibyte character. There are no
4848 multibyte character in unibyte text. */
4849 if (it->multibyte_p
4850 && success_p
4851 && FRAME_WINDOW_P (it->f))
4852 {
4853 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4854 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4855 }
4856 }
4857
4858 /* Is this character the last one of a run of characters with
4859 box? If yes, set IT->end_of_box_run_p to 1. */
4860 if (it->face_box_p
4861 && it->s == NULL)
4862 {
4863 int face_id;
4864 struct face *face;
4865
4866 it->end_of_box_run_p
4867 = ((face_id = face_after_it_pos (it),
4868 face_id != it->face_id)
4869 && (face = FACE_FROM_ID (it->f, face_id),
4870 face->box == FACE_NO_BOX));
4871 }
4872
4873 /* Value is 0 if end of buffer or string reached. */
4874 return success_p;
4875 }
4876
4877
4878 /* Move IT to the next display element.
4879
4880 RESEAT_P non-zero means if called on a newline in buffer text,
4881 skip to the next visible line start.
4882
4883 Functions get_next_display_element and set_iterator_to_next are
4884 separate because I find this arrangement easier to handle than a
4885 get_next_display_element function that also increments IT's
4886 position. The way it is we can first look at an iterator's current
4887 display element, decide whether it fits on a line, and if it does,
4888 increment the iterator position. The other way around we probably
4889 would either need a flag indicating whether the iterator has to be
4890 incremented the next time, or we would have to implement a
4891 decrement position function which would not be easy to write. */
4892
4893 void
4894 set_iterator_to_next (it, reseat_p)
4895 struct it *it;
4896 int reseat_p;
4897 {
4898 /* Reset flags indicating start and end of a sequence of characters
4899 with box. Reset them at the start of this function because
4900 moving the iterator to a new position might set them. */
4901 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4902
4903 if (it->method == next_element_from_buffer)
4904 {
4905 /* The current display element of IT is a character from
4906 current_buffer. Advance in the buffer, and maybe skip over
4907 invisible lines that are so because of selective display. */
4908 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4909 reseat_at_next_visible_line_start (it, 0);
4910 else
4911 {
4912 xassert (it->len != 0);
4913 IT_BYTEPOS (*it) += it->len;
4914 IT_CHARPOS (*it) += 1;
4915 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4916 }
4917 }
4918 else if (it->method == next_element_from_composition)
4919 {
4920 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4921 if (STRINGP (it->string))
4922 {
4923 IT_STRING_BYTEPOS (*it) += it->len;
4924 IT_STRING_CHARPOS (*it) += it->cmp_len;
4925 it->method = next_element_from_string;
4926 goto consider_string_end;
4927 }
4928 else
4929 {
4930 IT_BYTEPOS (*it) += it->len;
4931 IT_CHARPOS (*it) += it->cmp_len;
4932 it->method = next_element_from_buffer;
4933 }
4934 }
4935 else if (it->method == next_element_from_c_string)
4936 {
4937 /* Current display element of IT is from a C string. */
4938 IT_BYTEPOS (*it) += it->len;
4939 IT_CHARPOS (*it) += 1;
4940 }
4941 else if (it->method == next_element_from_display_vector)
4942 {
4943 /* Current display element of IT is from a display table entry.
4944 Advance in the display table definition. Reset it to null if
4945 end reached, and continue with characters from buffers/
4946 strings. */
4947 ++it->current.dpvec_index;
4948
4949 /* Restore face of the iterator to what they were before the
4950 display vector entry (these entries may contain faces). */
4951 it->face_id = it->saved_face_id;
4952
4953 if (it->dpvec + it->current.dpvec_index == it->dpend)
4954 {
4955 if (it->s)
4956 it->method = next_element_from_c_string;
4957 else if (STRINGP (it->string))
4958 it->method = next_element_from_string;
4959 else
4960 it->method = next_element_from_buffer;
4961
4962 it->dpvec = NULL;
4963 it->current.dpvec_index = -1;
4964
4965 /* Skip over characters which were displayed via IT->dpvec. */
4966 if (it->dpvec_char_len < 0)
4967 reseat_at_next_visible_line_start (it, 1);
4968 else if (it->dpvec_char_len > 0)
4969 {
4970 it->len = it->dpvec_char_len;
4971 set_iterator_to_next (it, reseat_p);
4972 }
4973 }
4974 }
4975 else if (it->method == next_element_from_string)
4976 {
4977 /* Current display element is a character from a Lisp string. */
4978 xassert (it->s == NULL && STRINGP (it->string));
4979 IT_STRING_BYTEPOS (*it) += it->len;
4980 IT_STRING_CHARPOS (*it) += 1;
4981
4982 consider_string_end:
4983
4984 if (it->current.overlay_string_index >= 0)
4985 {
4986 /* IT->string is an overlay string. Advance to the
4987 next, if there is one. */
4988 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4989 next_overlay_string (it);
4990 }
4991 else
4992 {
4993 /* IT->string is not an overlay string. If we reached
4994 its end, and there is something on IT->stack, proceed
4995 with what is on the stack. This can be either another
4996 string, this time an overlay string, or a buffer. */
4997 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4998 && it->sp > 0)
4999 {
5000 pop_it (it);
5001 if (!STRINGP (it->string))
5002 it->method = next_element_from_buffer;
5003 else
5004 goto consider_string_end;
5005 }
5006 }
5007 }
5008 else if (it->method == next_element_from_image
5009 || it->method == next_element_from_stretch)
5010 {
5011 /* The position etc with which we have to proceed are on
5012 the stack. The position may be at the end of a string,
5013 if the `display' property takes up the whole string. */
5014 pop_it (it);
5015 it->image_id = 0;
5016 if (STRINGP (it->string))
5017 {
5018 it->method = next_element_from_string;
5019 goto consider_string_end;
5020 }
5021 else
5022 it->method = next_element_from_buffer;
5023 }
5024 else
5025 /* There are no other methods defined, so this should be a bug. */
5026 abort ();
5027
5028 xassert (it->method != next_element_from_string
5029 || (STRINGP (it->string)
5030 && IT_STRING_CHARPOS (*it) >= 0));
5031 }
5032
5033
5034 /* Load IT's display element fields with information about the next
5035 display element which comes from a display table entry or from the
5036 result of translating a control character to one of the forms `^C'
5037 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5038
5039 static int
5040 next_element_from_display_vector (it)
5041 struct it *it;
5042 {
5043 /* Precondition. */
5044 xassert (it->dpvec && it->current.dpvec_index >= 0);
5045
5046 /* Remember the current face id in case glyphs specify faces.
5047 IT's face is restored in set_iterator_to_next. */
5048 it->saved_face_id = it->face_id;
5049
5050 if (INTEGERP (*it->dpvec)
5051 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5052 {
5053 int lface_id;
5054 GLYPH g;
5055
5056 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5057 it->c = FAST_GLYPH_CHAR (g);
5058 it->len = CHAR_BYTES (it->c);
5059
5060 /* The entry may contain a face id to use. Such a face id is
5061 the id of a Lisp face, not a realized face. A face id of
5062 zero means no face is specified. */
5063 lface_id = FAST_GLYPH_FACE (g);
5064 if (lface_id)
5065 {
5066 /* The function returns -1 if lface_id is invalid. */
5067 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5068 if (face_id >= 0)
5069 it->face_id = face_id;
5070 }
5071 }
5072 else
5073 /* Display table entry is invalid. Return a space. */
5074 it->c = ' ', it->len = 1;
5075
5076 /* Don't change position and object of the iterator here. They are
5077 still the values of the character that had this display table
5078 entry or was translated, and that's what we want. */
5079 it->what = IT_CHARACTER;
5080 return 1;
5081 }
5082
5083
5084 /* Load IT with the next display element from Lisp string IT->string.
5085 IT->current.string_pos is the current position within the string.
5086 If IT->current.overlay_string_index >= 0, the Lisp string is an
5087 overlay string. */
5088
5089 static int
5090 next_element_from_string (it)
5091 struct it *it;
5092 {
5093 struct text_pos position;
5094
5095 xassert (STRINGP (it->string));
5096 xassert (IT_STRING_CHARPOS (*it) >= 0);
5097 position = it->current.string_pos;
5098
5099 /* Time to check for invisible text? */
5100 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5101 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5102 {
5103 handle_stop (it);
5104
5105 /* Since a handler may have changed IT->method, we must
5106 recurse here. */
5107 return get_next_display_element (it);
5108 }
5109
5110 if (it->current.overlay_string_index >= 0)
5111 {
5112 /* Get the next character from an overlay string. In overlay
5113 strings, There is no field width or padding with spaces to
5114 do. */
5115 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5116 {
5117 it->what = IT_EOB;
5118 return 0;
5119 }
5120 else if (STRING_MULTIBYTE (it->string))
5121 {
5122 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5123 const unsigned char *s = (SDATA (it->string)
5124 + IT_STRING_BYTEPOS (*it));
5125 it->c = string_char_and_length (s, remaining, &it->len);
5126 }
5127 else
5128 {
5129 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5130 it->len = 1;
5131 }
5132 }
5133 else
5134 {
5135 /* Get the next character from a Lisp string that is not an
5136 overlay string. Such strings come from the mode line, for
5137 example. We may have to pad with spaces, or truncate the
5138 string. See also next_element_from_c_string. */
5139 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5140 {
5141 it->what = IT_EOB;
5142 return 0;
5143 }
5144 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5145 {
5146 /* Pad with spaces. */
5147 it->c = ' ', it->len = 1;
5148 CHARPOS (position) = BYTEPOS (position) = -1;
5149 }
5150 else if (STRING_MULTIBYTE (it->string))
5151 {
5152 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5153 const unsigned char *s = (SDATA (it->string)
5154 + IT_STRING_BYTEPOS (*it));
5155 it->c = string_char_and_length (s, maxlen, &it->len);
5156 }
5157 else
5158 {
5159 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5160 it->len = 1;
5161 }
5162 }
5163
5164 /* Record what we have and where it came from. Note that we store a
5165 buffer position in IT->position although it could arguably be a
5166 string position. */
5167 it->what = IT_CHARACTER;
5168 it->object = it->string;
5169 it->position = position;
5170 return 1;
5171 }
5172
5173
5174 /* Load IT with next display element from C string IT->s.
5175 IT->string_nchars is the maximum number of characters to return
5176 from the string. IT->end_charpos may be greater than
5177 IT->string_nchars when this function is called, in which case we
5178 may have to return padding spaces. Value is zero if end of string
5179 reached, including padding spaces. */
5180
5181 static int
5182 next_element_from_c_string (it)
5183 struct it *it;
5184 {
5185 int success_p = 1;
5186
5187 xassert (it->s);
5188 it->what = IT_CHARACTER;
5189 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5190 it->object = Qnil;
5191
5192 /* IT's position can be greater IT->string_nchars in case a field
5193 width or precision has been specified when the iterator was
5194 initialized. */
5195 if (IT_CHARPOS (*it) >= it->end_charpos)
5196 {
5197 /* End of the game. */
5198 it->what = IT_EOB;
5199 success_p = 0;
5200 }
5201 else if (IT_CHARPOS (*it) >= it->string_nchars)
5202 {
5203 /* Pad with spaces. */
5204 it->c = ' ', it->len = 1;
5205 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5206 }
5207 else if (it->multibyte_p)
5208 {
5209 /* Implementation note: The calls to strlen apparently aren't a
5210 performance problem because there is no noticeable performance
5211 difference between Emacs running in unibyte or multibyte mode. */
5212 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5213 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5214 maxlen, &it->len);
5215 }
5216 else
5217 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5218
5219 return success_p;
5220 }
5221
5222
5223 /* Set up IT to return characters from an ellipsis, if appropriate.
5224 The definition of the ellipsis glyphs may come from a display table
5225 entry. This function Fills IT with the first glyph from the
5226 ellipsis if an ellipsis is to be displayed. */
5227
5228 static int
5229 next_element_from_ellipsis (it)
5230 struct it *it;
5231 {
5232 if (it->selective_display_ellipsis_p)
5233 {
5234 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5235 {
5236 /* Use the display table definition for `...'. Invalid glyphs
5237 will be handled by the method returning elements from dpvec. */
5238 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5239 it->dpvec_char_len = it->len;
5240 it->dpvec = v->contents;
5241 it->dpend = v->contents + v->size;
5242 it->current.dpvec_index = 0;
5243 it->method = next_element_from_display_vector;
5244 }
5245 else
5246 {
5247 /* Use default `...' which is stored in default_invis_vector. */
5248 it->dpvec_char_len = it->len;
5249 it->dpvec = default_invis_vector;
5250 it->dpend = default_invis_vector + 3;
5251 it->current.dpvec_index = 0;
5252 it->method = next_element_from_display_vector;
5253 }
5254 }
5255 else
5256 {
5257 /* The face at the current position may be different from the
5258 face we find after the invisible text. Remember what it
5259 was in IT->saved_face_id, and signal that it's there by
5260 setting face_before_selective_p. */
5261 it->saved_face_id = it->face_id;
5262 it->method = next_element_from_buffer;
5263 reseat_at_next_visible_line_start (it, 1);
5264 it->face_before_selective_p = 1;
5265 }
5266
5267 return get_next_display_element (it);
5268 }
5269
5270
5271 /* Deliver an image display element. The iterator IT is already
5272 filled with image information (done in handle_display_prop). Value
5273 is always 1. */
5274
5275
5276 static int
5277 next_element_from_image (it)
5278 struct it *it;
5279 {
5280 it->what = IT_IMAGE;
5281 return 1;
5282 }
5283
5284
5285 /* Fill iterator IT with next display element from a stretch glyph
5286 property. IT->object is the value of the text property. Value is
5287 always 1. */
5288
5289 static int
5290 next_element_from_stretch (it)
5291 struct it *it;
5292 {
5293 it->what = IT_STRETCH;
5294 return 1;
5295 }
5296
5297
5298 /* Load IT with the next display element from current_buffer. Value
5299 is zero if end of buffer reached. IT->stop_charpos is the next
5300 position at which to stop and check for text properties or buffer
5301 end. */
5302
5303 static int
5304 next_element_from_buffer (it)
5305 struct it *it;
5306 {
5307 int success_p = 1;
5308
5309 /* Check this assumption, otherwise, we would never enter the
5310 if-statement, below. */
5311 xassert (IT_CHARPOS (*it) >= BEGV
5312 && IT_CHARPOS (*it) <= it->stop_charpos);
5313
5314 if (IT_CHARPOS (*it) >= it->stop_charpos)
5315 {
5316 if (IT_CHARPOS (*it) >= it->end_charpos)
5317 {
5318 int overlay_strings_follow_p;
5319
5320 /* End of the game, except when overlay strings follow that
5321 haven't been returned yet. */
5322 if (it->overlay_strings_at_end_processed_p)
5323 overlay_strings_follow_p = 0;
5324 else
5325 {
5326 it->overlay_strings_at_end_processed_p = 1;
5327 overlay_strings_follow_p = get_overlay_strings (it, 0);
5328 }
5329
5330 if (overlay_strings_follow_p)
5331 success_p = get_next_display_element (it);
5332 else
5333 {
5334 it->what = IT_EOB;
5335 it->position = it->current.pos;
5336 success_p = 0;
5337 }
5338 }
5339 else
5340 {
5341 handle_stop (it);
5342 return get_next_display_element (it);
5343 }
5344 }
5345 else
5346 {
5347 /* No face changes, overlays etc. in sight, so just return a
5348 character from current_buffer. */
5349 unsigned char *p;
5350
5351 /* Maybe run the redisplay end trigger hook. Performance note:
5352 This doesn't seem to cost measurable time. */
5353 if (it->redisplay_end_trigger_charpos
5354 && it->glyph_row
5355 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5356 run_redisplay_end_trigger_hook (it);
5357
5358 /* Get the next character, maybe multibyte. */
5359 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5360 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5361 {
5362 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5363 - IT_BYTEPOS (*it));
5364 it->c = string_char_and_length (p, maxlen, &it->len);
5365 }
5366 else
5367 it->c = *p, it->len = 1;
5368
5369 /* Record what we have and where it came from. */
5370 it->what = IT_CHARACTER;;
5371 it->object = it->w->buffer;
5372 it->position = it->current.pos;
5373
5374 /* Normally we return the character found above, except when we
5375 really want to return an ellipsis for selective display. */
5376 if (it->selective)
5377 {
5378 if (it->c == '\n')
5379 {
5380 /* A value of selective > 0 means hide lines indented more
5381 than that number of columns. */
5382 if (it->selective > 0
5383 && IT_CHARPOS (*it) + 1 < ZV
5384 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5385 IT_BYTEPOS (*it) + 1,
5386 (double) it->selective)) /* iftc */
5387 {
5388 success_p = next_element_from_ellipsis (it);
5389 it->dpvec_char_len = -1;
5390 }
5391 }
5392 else if (it->c == '\r' && it->selective == -1)
5393 {
5394 /* A value of selective == -1 means that everything from the
5395 CR to the end of the line is invisible, with maybe an
5396 ellipsis displayed for it. */
5397 success_p = next_element_from_ellipsis (it);
5398 it->dpvec_char_len = -1;
5399 }
5400 }
5401 }
5402
5403 /* Value is zero if end of buffer reached. */
5404 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5405 return success_p;
5406 }
5407
5408
5409 /* Run the redisplay end trigger hook for IT. */
5410
5411 static void
5412 run_redisplay_end_trigger_hook (it)
5413 struct it *it;
5414 {
5415 Lisp_Object args[3];
5416
5417 /* IT->glyph_row should be non-null, i.e. we should be actually
5418 displaying something, or otherwise we should not run the hook. */
5419 xassert (it->glyph_row);
5420
5421 /* Set up hook arguments. */
5422 args[0] = Qredisplay_end_trigger_functions;
5423 args[1] = it->window;
5424 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5425 it->redisplay_end_trigger_charpos = 0;
5426
5427 /* Since we are *trying* to run these functions, don't try to run
5428 them again, even if they get an error. */
5429 it->w->redisplay_end_trigger = Qnil;
5430 Frun_hook_with_args (3, args);
5431
5432 /* Notice if it changed the face of the character we are on. */
5433 handle_face_prop (it);
5434 }
5435
5436
5437 /* Deliver a composition display element. The iterator IT is already
5438 filled with composition information (done in
5439 handle_composition_prop). Value is always 1. */
5440
5441 static int
5442 next_element_from_composition (it)
5443 struct it *it;
5444 {
5445 it->what = IT_COMPOSITION;
5446 it->position = (STRINGP (it->string)
5447 ? it->current.string_pos
5448 : it->current.pos);
5449 return 1;
5450 }
5451
5452
5453 \f
5454 /***********************************************************************
5455 Moving an iterator without producing glyphs
5456 ***********************************************************************/
5457
5458 /* Move iterator IT to a specified buffer or X position within one
5459 line on the display without producing glyphs.
5460
5461 OP should be a bit mask including some or all of these bits:
5462 MOVE_TO_X: Stop on reaching x-position TO_X.
5463 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5464 Regardless of OP's value, stop in reaching the end of the display line.
5465
5466 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5467 This means, in particular, that TO_X includes window's horizontal
5468 scroll amount.
5469
5470 The return value has several possible values that
5471 say what condition caused the scan to stop:
5472
5473 MOVE_POS_MATCH_OR_ZV
5474 - when TO_POS or ZV was reached.
5475
5476 MOVE_X_REACHED
5477 -when TO_X was reached before TO_POS or ZV were reached.
5478
5479 MOVE_LINE_CONTINUED
5480 - when we reached the end of the display area and the line must
5481 be continued.
5482
5483 MOVE_LINE_TRUNCATED
5484 - when we reached the end of the display area and the line is
5485 truncated.
5486
5487 MOVE_NEWLINE_OR_CR
5488 - when we stopped at a line end, i.e. a newline or a CR and selective
5489 display is on. */
5490
5491 static enum move_it_result
5492 move_it_in_display_line_to (it, to_charpos, to_x, op)
5493 struct it *it;
5494 int to_charpos, to_x, op;
5495 {
5496 enum move_it_result result = MOVE_UNDEFINED;
5497 struct glyph_row *saved_glyph_row;
5498
5499 /* Don't produce glyphs in produce_glyphs. */
5500 saved_glyph_row = it->glyph_row;
5501 it->glyph_row = NULL;
5502
5503 while (1)
5504 {
5505 int x, i, ascent = 0, descent = 0;
5506
5507 /* Stop when ZV or TO_CHARPOS reached. */
5508 if (!get_next_display_element (it)
5509 || ((op & MOVE_TO_POS) != 0
5510 && BUFFERP (it->object)
5511 && IT_CHARPOS (*it) >= to_charpos))
5512 {
5513 result = MOVE_POS_MATCH_OR_ZV;
5514 break;
5515 }
5516
5517 /* The call to produce_glyphs will get the metrics of the
5518 display element IT is loaded with. We record in x the
5519 x-position before this display element in case it does not
5520 fit on the line. */
5521 x = it->current_x;
5522
5523 /* Remember the line height so far in case the next element doesn't
5524 fit on the line. */
5525 if (!it->truncate_lines_p)
5526 {
5527 ascent = it->max_ascent;
5528 descent = it->max_descent;
5529 }
5530
5531 PRODUCE_GLYPHS (it);
5532
5533 if (it->area != TEXT_AREA)
5534 {
5535 set_iterator_to_next (it, 1);
5536 continue;
5537 }
5538
5539 /* The number of glyphs we get back in IT->nglyphs will normally
5540 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5541 character on a terminal frame, or (iii) a line end. For the
5542 second case, IT->nglyphs - 1 padding glyphs will be present
5543 (on X frames, there is only one glyph produced for a
5544 composite character.
5545
5546 The behavior implemented below means, for continuation lines,
5547 that as many spaces of a TAB as fit on the current line are
5548 displayed there. For terminal frames, as many glyphs of a
5549 multi-glyph character are displayed in the current line, too.
5550 This is what the old redisplay code did, and we keep it that
5551 way. Under X, the whole shape of a complex character must
5552 fit on the line or it will be completely displayed in the
5553 next line.
5554
5555 Note that both for tabs and padding glyphs, all glyphs have
5556 the same width. */
5557 if (it->nglyphs)
5558 {
5559 /* More than one glyph or glyph doesn't fit on line. All
5560 glyphs have the same width. */
5561 int single_glyph_width = it->pixel_width / it->nglyphs;
5562 int new_x;
5563
5564 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5565 {
5566 new_x = x + single_glyph_width;
5567
5568 /* We want to leave anything reaching TO_X to the caller. */
5569 if ((op & MOVE_TO_X) && new_x > to_x)
5570 {
5571 it->current_x = x;
5572 result = MOVE_X_REACHED;
5573 break;
5574 }
5575 else if (/* Lines are continued. */
5576 !it->truncate_lines_p
5577 && (/* And glyph doesn't fit on the line. */
5578 new_x > it->last_visible_x
5579 /* Or it fits exactly and we're on a window
5580 system frame. */
5581 || (new_x == it->last_visible_x
5582 && FRAME_WINDOW_P (it->f))))
5583 {
5584 if (/* IT->hpos == 0 means the very first glyph
5585 doesn't fit on the line, e.g. a wide image. */
5586 it->hpos == 0
5587 || (new_x == it->last_visible_x
5588 && FRAME_WINDOW_P (it->f)))
5589 {
5590 ++it->hpos;
5591 it->current_x = new_x;
5592 if (i == it->nglyphs - 1)
5593 set_iterator_to_next (it, 1);
5594 }
5595 else
5596 {
5597 it->current_x = x;
5598 it->max_ascent = ascent;
5599 it->max_descent = descent;
5600 }
5601
5602 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5603 IT_CHARPOS (*it)));
5604 result = MOVE_LINE_CONTINUED;
5605 break;
5606 }
5607 else if (new_x > it->first_visible_x)
5608 {
5609 /* Glyph is visible. Increment number of glyphs that
5610 would be displayed. */
5611 ++it->hpos;
5612 }
5613 else
5614 {
5615 /* Glyph is completely off the left margin of the display
5616 area. Nothing to do. */
5617 }
5618 }
5619
5620 if (result != MOVE_UNDEFINED)
5621 break;
5622 }
5623 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5624 {
5625 /* Stop when TO_X specified and reached. This check is
5626 necessary here because of lines consisting of a line end,
5627 only. The line end will not produce any glyphs and we
5628 would never get MOVE_X_REACHED. */
5629 xassert (it->nglyphs == 0);
5630 result = MOVE_X_REACHED;
5631 break;
5632 }
5633
5634 /* Is this a line end? If yes, we're done. */
5635 if (ITERATOR_AT_END_OF_LINE_P (it))
5636 {
5637 result = MOVE_NEWLINE_OR_CR;
5638 break;
5639 }
5640
5641 /* The current display element has been consumed. Advance
5642 to the next. */
5643 set_iterator_to_next (it, 1);
5644
5645 /* Stop if lines are truncated and IT's current x-position is
5646 past the right edge of the window now. */
5647 if (it->truncate_lines_p
5648 && it->current_x >= it->last_visible_x)
5649 {
5650 result = MOVE_LINE_TRUNCATED;
5651 break;
5652 }
5653 }
5654
5655 /* Restore the iterator settings altered at the beginning of this
5656 function. */
5657 it->glyph_row = saved_glyph_row;
5658 return result;
5659 }
5660
5661
5662 /* Move IT forward until it satisfies one or more of the criteria in
5663 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5664
5665 OP is a bit-mask that specifies where to stop, and in particular,
5666 which of those four position arguments makes a difference. See the
5667 description of enum move_operation_enum.
5668
5669 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5670 screen line, this function will set IT to the next position >
5671 TO_CHARPOS. */
5672
5673 void
5674 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5675 struct it *it;
5676 int to_charpos, to_x, to_y, to_vpos;
5677 int op;
5678 {
5679 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5680 int line_height;
5681 int reached = 0;
5682
5683 for (;;)
5684 {
5685 if (op & MOVE_TO_VPOS)
5686 {
5687 /* If no TO_CHARPOS and no TO_X specified, stop at the
5688 start of the line TO_VPOS. */
5689 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5690 {
5691 if (it->vpos == to_vpos)
5692 {
5693 reached = 1;
5694 break;
5695 }
5696 else
5697 skip = move_it_in_display_line_to (it, -1, -1, 0);
5698 }
5699 else
5700 {
5701 /* TO_VPOS >= 0 means stop at TO_X in the line at
5702 TO_VPOS, or at TO_POS, whichever comes first. */
5703 if (it->vpos == to_vpos)
5704 {
5705 reached = 2;
5706 break;
5707 }
5708
5709 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5710
5711 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5712 {
5713 reached = 3;
5714 break;
5715 }
5716 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5717 {
5718 /* We have reached TO_X but not in the line we want. */
5719 skip = move_it_in_display_line_to (it, to_charpos,
5720 -1, MOVE_TO_POS);
5721 if (skip == MOVE_POS_MATCH_OR_ZV)
5722 {
5723 reached = 4;
5724 break;
5725 }
5726 }
5727 }
5728 }
5729 else if (op & MOVE_TO_Y)
5730 {
5731 struct it it_backup;
5732
5733 /* TO_Y specified means stop at TO_X in the line containing
5734 TO_Y---or at TO_CHARPOS if this is reached first. The
5735 problem is that we can't really tell whether the line
5736 contains TO_Y before we have completely scanned it, and
5737 this may skip past TO_X. What we do is to first scan to
5738 TO_X.
5739
5740 If TO_X is not specified, use a TO_X of zero. The reason
5741 is to make the outcome of this function more predictable.
5742 If we didn't use TO_X == 0, we would stop at the end of
5743 the line which is probably not what a caller would expect
5744 to happen. */
5745 skip = move_it_in_display_line_to (it, to_charpos,
5746 ((op & MOVE_TO_X)
5747 ? to_x : 0),
5748 (MOVE_TO_X
5749 | (op & MOVE_TO_POS)));
5750
5751 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5752 if (skip == MOVE_POS_MATCH_OR_ZV)
5753 {
5754 reached = 5;
5755 break;
5756 }
5757
5758 /* If TO_X was reached, we would like to know whether TO_Y
5759 is in the line. This can only be said if we know the
5760 total line height which requires us to scan the rest of
5761 the line. */
5762 if (skip == MOVE_X_REACHED)
5763 {
5764 it_backup = *it;
5765 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5766 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5767 op & MOVE_TO_POS);
5768 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5769 }
5770
5771 /* Now, decide whether TO_Y is in this line. */
5772 line_height = it->max_ascent + it->max_descent;
5773 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5774
5775 if (to_y >= it->current_y
5776 && to_y < it->current_y + line_height)
5777 {
5778 if (skip == MOVE_X_REACHED)
5779 /* If TO_Y is in this line and TO_X was reached above,
5780 we scanned too far. We have to restore IT's settings
5781 to the ones before skipping. */
5782 *it = it_backup;
5783 reached = 6;
5784 }
5785 else if (skip == MOVE_X_REACHED)
5786 {
5787 skip = skip2;
5788 if (skip == MOVE_POS_MATCH_OR_ZV)
5789 reached = 7;
5790 }
5791
5792 if (reached)
5793 break;
5794 }
5795 else
5796 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5797
5798 switch (skip)
5799 {
5800 case MOVE_POS_MATCH_OR_ZV:
5801 reached = 8;
5802 goto out;
5803
5804 case MOVE_NEWLINE_OR_CR:
5805 set_iterator_to_next (it, 1);
5806 it->continuation_lines_width = 0;
5807 break;
5808
5809 case MOVE_LINE_TRUNCATED:
5810 it->continuation_lines_width = 0;
5811 reseat_at_next_visible_line_start (it, 0);
5812 if ((op & MOVE_TO_POS) != 0
5813 && IT_CHARPOS (*it) > to_charpos)
5814 {
5815 reached = 9;
5816 goto out;
5817 }
5818 break;
5819
5820 case MOVE_LINE_CONTINUED:
5821 it->continuation_lines_width += it->current_x;
5822 break;
5823
5824 default:
5825 abort ();
5826 }
5827
5828 /* Reset/increment for the next run. */
5829 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5830 it->current_x = it->hpos = 0;
5831 it->current_y += it->max_ascent + it->max_descent;
5832 ++it->vpos;
5833 last_height = it->max_ascent + it->max_descent;
5834 last_max_ascent = it->max_ascent;
5835 it->max_ascent = it->max_descent = 0;
5836 }
5837
5838 out:
5839
5840 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5841 }
5842
5843
5844 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5845
5846 If DY > 0, move IT backward at least that many pixels. DY = 0
5847 means move IT backward to the preceding line start or BEGV. This
5848 function may move over more than DY pixels if IT->current_y - DY
5849 ends up in the middle of a line; in this case IT->current_y will be
5850 set to the top of the line moved to. */
5851
5852 void
5853 move_it_vertically_backward (it, dy)
5854 struct it *it;
5855 int dy;
5856 {
5857 int nlines, h;
5858 struct it it2, it3;
5859 int start_pos = IT_CHARPOS (*it);
5860
5861 xassert (dy >= 0);
5862
5863 /* Estimate how many newlines we must move back. */
5864 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5865
5866 /* Set the iterator's position that many lines back. */
5867 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5868 back_to_previous_visible_line_start (it);
5869
5870 /* Reseat the iterator here. When moving backward, we don't want
5871 reseat to skip forward over invisible text, set up the iterator
5872 to deliver from overlay strings at the new position etc. So,
5873 use reseat_1 here. */
5874 reseat_1 (it, it->current.pos, 1);
5875
5876 /* We are now surely at a line start. */
5877 it->current_x = it->hpos = 0;
5878 it->continuation_lines_width = 0;
5879
5880 /* Move forward and see what y-distance we moved. First move to the
5881 start of the next line so that we get its height. We need this
5882 height to be able to tell whether we reached the specified
5883 y-distance. */
5884 it2 = *it;
5885 it2.max_ascent = it2.max_descent = 0;
5886 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5887 MOVE_TO_POS | MOVE_TO_VPOS);
5888 xassert (IT_CHARPOS (*it) >= BEGV);
5889 it3 = it2;
5890
5891 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5892 xassert (IT_CHARPOS (*it) >= BEGV);
5893 /* H is the actual vertical distance from the position in *IT
5894 and the starting position. */
5895 h = it2.current_y - it->current_y;
5896 /* NLINES is the distance in number of lines. */
5897 nlines = it2.vpos - it->vpos;
5898
5899 /* Correct IT's y and vpos position
5900 so that they are relative to the starting point. */
5901 it->vpos -= nlines;
5902 it->current_y -= h;
5903
5904 if (dy == 0)
5905 {
5906 /* DY == 0 means move to the start of the screen line. The
5907 value of nlines is > 0 if continuation lines were involved. */
5908 if (nlines > 0)
5909 move_it_by_lines (it, nlines, 1);
5910 xassert (IT_CHARPOS (*it) <= start_pos);
5911 }
5912 else
5913 {
5914 /* The y-position we try to reach, relative to *IT.
5915 Note that H has been subtracted in front of the if-statement. */
5916 int target_y = it->current_y + h - dy;
5917 int y0 = it3.current_y;
5918 int y1 = line_bottom_y (&it3);
5919 int line_height = y1 - y0;
5920
5921 /* If we did not reach target_y, try to move further backward if
5922 we can. If we moved too far backward, try to move forward. */
5923 if (target_y < it->current_y
5924 /* This is heuristic. In a window that's 3 lines high, with
5925 a line height of 13 pixels each, recentering with point
5926 on the bottom line will try to move -39/2 = 19 pixels
5927 backward. Try to avoid moving into the first line. */
5928 && it->current_y - target_y > line_height / 3 * 2
5929 && IT_CHARPOS (*it) > BEGV)
5930 {
5931 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5932 target_y - it->current_y));
5933 move_it_vertically (it, target_y - it->current_y);
5934 xassert (IT_CHARPOS (*it) >= BEGV);
5935 }
5936 else if (target_y >= it->current_y + line_height
5937 && IT_CHARPOS (*it) < ZV)
5938 {
5939 /* Should move forward by at least one line, maybe more.
5940
5941 Note: Calling move_it_by_lines can be expensive on
5942 terminal frames, where compute_motion is used (via
5943 vmotion) to do the job, when there are very long lines
5944 and truncate-lines is nil. That's the reason for
5945 treating terminal frames specially here. */
5946
5947 if (!FRAME_WINDOW_P (it->f))
5948 move_it_vertically (it, target_y - (it->current_y + line_height));
5949 else
5950 {
5951 do
5952 {
5953 move_it_by_lines (it, 1, 1);
5954 }
5955 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5956 }
5957
5958 xassert (IT_CHARPOS (*it) >= BEGV);
5959 }
5960 }
5961 }
5962
5963
5964 /* Move IT by a specified amount of pixel lines DY. DY negative means
5965 move backwards. DY = 0 means move to start of screen line. At the
5966 end, IT will be on the start of a screen line. */
5967
5968 void
5969 move_it_vertically (it, dy)
5970 struct it *it;
5971 int dy;
5972 {
5973 if (dy <= 0)
5974 move_it_vertically_backward (it, -dy);
5975 else if (dy > 0)
5976 {
5977 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5978 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5979 MOVE_TO_POS | MOVE_TO_Y);
5980 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5981
5982 /* If buffer ends in ZV without a newline, move to the start of
5983 the line to satisfy the post-condition. */
5984 if (IT_CHARPOS (*it) == ZV
5985 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5986 move_it_by_lines (it, 0, 0);
5987 }
5988 }
5989
5990
5991 /* Move iterator IT past the end of the text line it is in. */
5992
5993 void
5994 move_it_past_eol (it)
5995 struct it *it;
5996 {
5997 enum move_it_result rc;
5998
5999 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6000 if (rc == MOVE_NEWLINE_OR_CR)
6001 set_iterator_to_next (it, 0);
6002 }
6003
6004
6005 #if 0 /* Currently not used. */
6006
6007 /* Return non-zero if some text between buffer positions START_CHARPOS
6008 and END_CHARPOS is invisible. IT->window is the window for text
6009 property lookup. */
6010
6011 static int
6012 invisible_text_between_p (it, start_charpos, end_charpos)
6013 struct it *it;
6014 int start_charpos, end_charpos;
6015 {
6016 Lisp_Object prop, limit;
6017 int invisible_found_p;
6018
6019 xassert (it != NULL && start_charpos <= end_charpos);
6020
6021 /* Is text at START invisible? */
6022 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6023 it->window);
6024 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6025 invisible_found_p = 1;
6026 else
6027 {
6028 limit = Fnext_single_char_property_change (make_number (start_charpos),
6029 Qinvisible, Qnil,
6030 make_number (end_charpos));
6031 invisible_found_p = XFASTINT (limit) < end_charpos;
6032 }
6033
6034 return invisible_found_p;
6035 }
6036
6037 #endif /* 0 */
6038
6039
6040 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6041 negative means move up. DVPOS == 0 means move to the start of the
6042 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6043 NEED_Y_P is zero, IT->current_y will be left unchanged.
6044
6045 Further optimization ideas: If we would know that IT->f doesn't use
6046 a face with proportional font, we could be faster for
6047 truncate-lines nil. */
6048
6049 void
6050 move_it_by_lines (it, dvpos, need_y_p)
6051 struct it *it;
6052 int dvpos, need_y_p;
6053 {
6054 struct position pos;
6055
6056 if (!FRAME_WINDOW_P (it->f))
6057 {
6058 struct text_pos textpos;
6059
6060 /* We can use vmotion on frames without proportional fonts. */
6061 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6062 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6063 reseat (it, textpos, 1);
6064 it->vpos += pos.vpos;
6065 it->current_y += pos.vpos;
6066 }
6067 else if (dvpos == 0)
6068 {
6069 /* DVPOS == 0 means move to the start of the screen line. */
6070 move_it_vertically_backward (it, 0);
6071 xassert (it->current_x == 0 && it->hpos == 0);
6072 }
6073 else if (dvpos > 0)
6074 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6075 else
6076 {
6077 struct it it2;
6078 int start_charpos, i;
6079
6080 /* Start at the beginning of the screen line containing IT's
6081 position. */
6082 move_it_vertically_backward (it, 0);
6083
6084 /* Go back -DVPOS visible lines and reseat the iterator there. */
6085 start_charpos = IT_CHARPOS (*it);
6086 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6087 back_to_previous_visible_line_start (it);
6088 reseat (it, it->current.pos, 1);
6089 it->current_x = it->hpos = 0;
6090
6091 /* Above call may have moved too far if continuation lines
6092 are involved. Scan forward and see if it did. */
6093 it2 = *it;
6094 it2.vpos = it2.current_y = 0;
6095 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6096 it->vpos -= it2.vpos;
6097 it->current_y -= it2.current_y;
6098 it->current_x = it->hpos = 0;
6099
6100 /* If we moved too far, move IT some lines forward. */
6101 if (it2.vpos > -dvpos)
6102 {
6103 int delta = it2.vpos + dvpos;
6104 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6105 }
6106 }
6107 }
6108
6109 /* Return 1 if IT points into the middle of a display vector. */
6110
6111 int
6112 in_display_vector_p (it)
6113 struct it *it;
6114 {
6115 return (it->method == next_element_from_display_vector
6116 && it->current.dpvec_index > 0
6117 && it->dpvec + it->current.dpvec_index != it->dpend);
6118 }
6119
6120 \f
6121 /***********************************************************************
6122 Messages
6123 ***********************************************************************/
6124
6125
6126 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6127 to *Messages*. */
6128
6129 void
6130 add_to_log (format, arg1, arg2)
6131 char *format;
6132 Lisp_Object arg1, arg2;
6133 {
6134 Lisp_Object args[3];
6135 Lisp_Object msg, fmt;
6136 char *buffer;
6137 int len;
6138 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6139
6140 /* Do nothing if called asynchronously. Inserting text into
6141 a buffer may call after-change-functions and alike and
6142 that would means running Lisp asynchronously. */
6143 if (handling_signal)
6144 return;
6145
6146 fmt = msg = Qnil;
6147 GCPRO4 (fmt, msg, arg1, arg2);
6148
6149 args[0] = fmt = build_string (format);
6150 args[1] = arg1;
6151 args[2] = arg2;
6152 msg = Fformat (3, args);
6153
6154 len = SBYTES (msg) + 1;
6155 buffer = (char *) alloca (len);
6156 bcopy (SDATA (msg), buffer, len);
6157
6158 message_dolog (buffer, len - 1, 1, 0);
6159 UNGCPRO;
6160 }
6161
6162
6163 /* Output a newline in the *Messages* buffer if "needs" one. */
6164
6165 void
6166 message_log_maybe_newline ()
6167 {
6168 if (message_log_need_newline)
6169 message_dolog ("", 0, 1, 0);
6170 }
6171
6172
6173 /* Add a string M of length NBYTES to the message log, optionally
6174 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6175 nonzero, means interpret the contents of M as multibyte. This
6176 function calls low-level routines in order to bypass text property
6177 hooks, etc. which might not be safe to run. */
6178
6179 void
6180 message_dolog (m, nbytes, nlflag, multibyte)
6181 const char *m;
6182 int nbytes, nlflag, multibyte;
6183 {
6184 if (!NILP (Vmemory_full))
6185 return;
6186
6187 if (!NILP (Vmessage_log_max))
6188 {
6189 struct buffer *oldbuf;
6190 Lisp_Object oldpoint, oldbegv, oldzv;
6191 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6192 int point_at_end = 0;
6193 int zv_at_end = 0;
6194 Lisp_Object old_deactivate_mark, tem;
6195 struct gcpro gcpro1;
6196
6197 old_deactivate_mark = Vdeactivate_mark;
6198 oldbuf = current_buffer;
6199 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6200 current_buffer->undo_list = Qt;
6201
6202 oldpoint = message_dolog_marker1;
6203 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6204 oldbegv = message_dolog_marker2;
6205 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6206 oldzv = message_dolog_marker3;
6207 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6208 GCPRO1 (old_deactivate_mark);
6209
6210 if (PT == Z)
6211 point_at_end = 1;
6212 if (ZV == Z)
6213 zv_at_end = 1;
6214
6215 BEGV = BEG;
6216 BEGV_BYTE = BEG_BYTE;
6217 ZV = Z;
6218 ZV_BYTE = Z_BYTE;
6219 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6220
6221 /* Insert the string--maybe converting multibyte to single byte
6222 or vice versa, so that all the text fits the buffer. */
6223 if (multibyte
6224 && NILP (current_buffer->enable_multibyte_characters))
6225 {
6226 int i, c, char_bytes;
6227 unsigned char work[1];
6228
6229 /* Convert a multibyte string to single-byte
6230 for the *Message* buffer. */
6231 for (i = 0; i < nbytes; i += char_bytes)
6232 {
6233 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6234 work[0] = (SINGLE_BYTE_CHAR_P (c)
6235 ? c
6236 : multibyte_char_to_unibyte (c, Qnil));
6237 insert_1_both (work, 1, 1, 1, 0, 0);
6238 }
6239 }
6240 else if (! multibyte
6241 && ! NILP (current_buffer->enable_multibyte_characters))
6242 {
6243 int i, c, char_bytes;
6244 unsigned char *msg = (unsigned char *) m;
6245 unsigned char str[MAX_MULTIBYTE_LENGTH];
6246 /* Convert a single-byte string to multibyte
6247 for the *Message* buffer. */
6248 for (i = 0; i < nbytes; i++)
6249 {
6250 c = unibyte_char_to_multibyte (msg[i]);
6251 char_bytes = CHAR_STRING (c, str);
6252 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6253 }
6254 }
6255 else if (nbytes)
6256 insert_1 (m, nbytes, 1, 0, 0);
6257
6258 if (nlflag)
6259 {
6260 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6261 insert_1 ("\n", 1, 1, 0, 0);
6262
6263 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6264 this_bol = PT;
6265 this_bol_byte = PT_BYTE;
6266
6267 /* See if this line duplicates the previous one.
6268 If so, combine duplicates. */
6269 if (this_bol > BEG)
6270 {
6271 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6272 prev_bol = PT;
6273 prev_bol_byte = PT_BYTE;
6274
6275 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6276 this_bol, this_bol_byte);
6277 if (dup)
6278 {
6279 del_range_both (prev_bol, prev_bol_byte,
6280 this_bol, this_bol_byte, 0);
6281 if (dup > 1)
6282 {
6283 char dupstr[40];
6284 int duplen;
6285
6286 /* If you change this format, don't forget to also
6287 change message_log_check_duplicate. */
6288 sprintf (dupstr, " [%d times]", dup);
6289 duplen = strlen (dupstr);
6290 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6291 insert_1 (dupstr, duplen, 1, 0, 1);
6292 }
6293 }
6294 }
6295
6296 /* If we have more than the desired maximum number of lines
6297 in the *Messages* buffer now, delete the oldest ones.
6298 This is safe because we don't have undo in this buffer. */
6299
6300 if (NATNUMP (Vmessage_log_max))
6301 {
6302 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6303 -XFASTINT (Vmessage_log_max) - 1, 0);
6304 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6305 }
6306 }
6307 BEGV = XMARKER (oldbegv)->charpos;
6308 BEGV_BYTE = marker_byte_position (oldbegv);
6309
6310 if (zv_at_end)
6311 {
6312 ZV = Z;
6313 ZV_BYTE = Z_BYTE;
6314 }
6315 else
6316 {
6317 ZV = XMARKER (oldzv)->charpos;
6318 ZV_BYTE = marker_byte_position (oldzv);
6319 }
6320
6321 if (point_at_end)
6322 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6323 else
6324 /* We can't do Fgoto_char (oldpoint) because it will run some
6325 Lisp code. */
6326 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6327 XMARKER (oldpoint)->bytepos);
6328
6329 UNGCPRO;
6330 unchain_marker (XMARKER (oldpoint));
6331 unchain_marker (XMARKER (oldbegv));
6332 unchain_marker (XMARKER (oldzv));
6333
6334 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6335 set_buffer_internal (oldbuf);
6336 if (NILP (tem))
6337 windows_or_buffers_changed = old_windows_or_buffers_changed;
6338 message_log_need_newline = !nlflag;
6339 Vdeactivate_mark = old_deactivate_mark;
6340 }
6341 }
6342
6343
6344 /* We are at the end of the buffer after just having inserted a newline.
6345 (Note: We depend on the fact we won't be crossing the gap.)
6346 Check to see if the most recent message looks a lot like the previous one.
6347 Return 0 if different, 1 if the new one should just replace it, or a
6348 value N > 1 if we should also append " [N times]". */
6349
6350 static int
6351 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6352 int prev_bol, this_bol;
6353 int prev_bol_byte, this_bol_byte;
6354 {
6355 int i;
6356 int len = Z_BYTE - 1 - this_bol_byte;
6357 int seen_dots = 0;
6358 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6359 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6360
6361 for (i = 0; i < len; i++)
6362 {
6363 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6364 seen_dots = 1;
6365 if (p1[i] != p2[i])
6366 return seen_dots;
6367 }
6368 p1 += len;
6369 if (*p1 == '\n')
6370 return 2;
6371 if (*p1++ == ' ' && *p1++ == '[')
6372 {
6373 int n = 0;
6374 while (*p1 >= '0' && *p1 <= '9')
6375 n = n * 10 + *p1++ - '0';
6376 if (strncmp (p1, " times]\n", 8) == 0)
6377 return n+1;
6378 }
6379 return 0;
6380 }
6381
6382
6383 /* Display an echo area message M with a specified length of NBYTES
6384 bytes. The string may include null characters. If M is 0, clear
6385 out any existing message, and let the mini-buffer text show
6386 through.
6387
6388 The buffer M must continue to exist until after the echo area gets
6389 cleared or some other message gets displayed there. This means do
6390 not pass text that is stored in a Lisp string; do not pass text in
6391 a buffer that was alloca'd. */
6392
6393 void
6394 message2 (m, nbytes, multibyte)
6395 const char *m;
6396 int nbytes;
6397 int multibyte;
6398 {
6399 /* First flush out any partial line written with print. */
6400 message_log_maybe_newline ();
6401 if (m)
6402 message_dolog (m, nbytes, 1, multibyte);
6403 message2_nolog (m, nbytes, multibyte);
6404 }
6405
6406
6407 /* The non-logging counterpart of message2. */
6408
6409 void
6410 message2_nolog (m, nbytes, multibyte)
6411 const char *m;
6412 int nbytes, multibyte;
6413 {
6414 struct frame *sf = SELECTED_FRAME ();
6415 message_enable_multibyte = multibyte;
6416
6417 if (noninteractive)
6418 {
6419 if (noninteractive_need_newline)
6420 putc ('\n', stderr);
6421 noninteractive_need_newline = 0;
6422 if (m)
6423 fwrite (m, nbytes, 1, stderr);
6424 if (cursor_in_echo_area == 0)
6425 fprintf (stderr, "\n");
6426 fflush (stderr);
6427 }
6428 /* A null message buffer means that the frame hasn't really been
6429 initialized yet. Error messages get reported properly by
6430 cmd_error, so this must be just an informative message; toss it. */
6431 else if (INTERACTIVE
6432 && sf->glyphs_initialized_p
6433 && FRAME_MESSAGE_BUF (sf))
6434 {
6435 Lisp_Object mini_window;
6436 struct frame *f;
6437
6438 /* Get the frame containing the mini-buffer
6439 that the selected frame is using. */
6440 mini_window = FRAME_MINIBUF_WINDOW (sf);
6441 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6442
6443 FRAME_SAMPLE_VISIBILITY (f);
6444 if (FRAME_VISIBLE_P (sf)
6445 && ! FRAME_VISIBLE_P (f))
6446 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6447
6448 if (m)
6449 {
6450 set_message (m, Qnil, nbytes, multibyte);
6451 if (minibuffer_auto_raise)
6452 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6453 }
6454 else
6455 clear_message (1, 1);
6456
6457 do_pending_window_change (0);
6458 echo_area_display (1);
6459 do_pending_window_change (0);
6460 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6461 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6462 }
6463 }
6464
6465
6466 /* Display an echo area message M with a specified length of NBYTES
6467 bytes. The string may include null characters. If M is not a
6468 string, clear out any existing message, and let the mini-buffer
6469 text show through. */
6470
6471 void
6472 message3 (m, nbytes, multibyte)
6473 Lisp_Object m;
6474 int nbytes;
6475 int multibyte;
6476 {
6477 struct gcpro gcpro1;
6478
6479 GCPRO1 (m);
6480
6481 /* First flush out any partial line written with print. */
6482 message_log_maybe_newline ();
6483 if (STRINGP (m))
6484 message_dolog (SDATA (m), nbytes, 1, multibyte);
6485 message3_nolog (m, nbytes, multibyte);
6486
6487 UNGCPRO;
6488 }
6489
6490
6491 /* The non-logging version of message3. */
6492
6493 void
6494 message3_nolog (m, nbytes, multibyte)
6495 Lisp_Object m;
6496 int nbytes, multibyte;
6497 {
6498 struct frame *sf = SELECTED_FRAME ();
6499 message_enable_multibyte = multibyte;
6500
6501 if (noninteractive)
6502 {
6503 if (noninteractive_need_newline)
6504 putc ('\n', stderr);
6505 noninteractive_need_newline = 0;
6506 if (STRINGP (m))
6507 fwrite (SDATA (m), nbytes, 1, stderr);
6508 if (cursor_in_echo_area == 0)
6509 fprintf (stderr, "\n");
6510 fflush (stderr);
6511 }
6512 /* A null message buffer means that the frame hasn't really been
6513 initialized yet. Error messages get reported properly by
6514 cmd_error, so this must be just an informative message; toss it. */
6515 else if (INTERACTIVE
6516 && sf->glyphs_initialized_p
6517 && FRAME_MESSAGE_BUF (sf))
6518 {
6519 Lisp_Object mini_window;
6520 Lisp_Object frame;
6521 struct frame *f;
6522
6523 /* Get the frame containing the mini-buffer
6524 that the selected frame is using. */
6525 mini_window = FRAME_MINIBUF_WINDOW (sf);
6526 frame = XWINDOW (mini_window)->frame;
6527 f = XFRAME (frame);
6528
6529 FRAME_SAMPLE_VISIBILITY (f);
6530 if (FRAME_VISIBLE_P (sf)
6531 && !FRAME_VISIBLE_P (f))
6532 Fmake_frame_visible (frame);
6533
6534 if (STRINGP (m) && SCHARS (m) > 0)
6535 {
6536 set_message (NULL, m, nbytes, multibyte);
6537 if (minibuffer_auto_raise)
6538 Fraise_frame (frame);
6539 }
6540 else
6541 clear_message (1, 1);
6542
6543 do_pending_window_change (0);
6544 echo_area_display (1);
6545 do_pending_window_change (0);
6546 if (FRAME_DISPLAY (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
6547 (*FRAME_DISPLAY (f)->frame_up_to_date_hook) (f);
6548 }
6549 }
6550
6551
6552 /* Display a null-terminated echo area message M. If M is 0, clear
6553 out any existing message, and let the mini-buffer text show through.
6554
6555 The buffer M must continue to exist until after the echo area gets
6556 cleared or some other message gets displayed there. Do not pass
6557 text that is stored in a Lisp string. Do not pass text in a buffer
6558 that was alloca'd. */
6559
6560 void
6561 message1 (m)
6562 char *m;
6563 {
6564 message2 (m, (m ? strlen (m) : 0), 0);
6565 }
6566
6567
6568 /* The non-logging counterpart of message1. */
6569
6570 void
6571 message1_nolog (m)
6572 char *m;
6573 {
6574 message2_nolog (m, (m ? strlen (m) : 0), 0);
6575 }
6576
6577 /* Display a message M which contains a single %s
6578 which gets replaced with STRING. */
6579
6580 void
6581 message_with_string (m, string, log)
6582 char *m;
6583 Lisp_Object string;
6584 int log;
6585 {
6586 CHECK_STRING (string);
6587
6588 if (noninteractive)
6589 {
6590 if (m)
6591 {
6592 if (noninteractive_need_newline)
6593 putc ('\n', stderr);
6594 noninteractive_need_newline = 0;
6595 fprintf (stderr, m, SDATA (string));
6596 if (cursor_in_echo_area == 0)
6597 fprintf (stderr, "\n");
6598 fflush (stderr);
6599 }
6600 }
6601 else if (INTERACTIVE)
6602 {
6603 /* The frame whose minibuffer we're going to display the message on.
6604 It may be larger than the selected frame, so we need
6605 to use its buffer, not the selected frame's buffer. */
6606 Lisp_Object mini_window;
6607 struct frame *f, *sf = SELECTED_FRAME ();
6608
6609 /* Get the frame containing the minibuffer
6610 that the selected frame is using. */
6611 mini_window = FRAME_MINIBUF_WINDOW (sf);
6612 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6613
6614 /* A null message buffer means that the frame hasn't really been
6615 initialized yet. Error messages get reported properly by
6616 cmd_error, so this must be just an informative message; toss it. */
6617 if (FRAME_MESSAGE_BUF (f))
6618 {
6619 Lisp_Object args[2], message;
6620 struct gcpro gcpro1, gcpro2;
6621
6622 args[0] = build_string (m);
6623 args[1] = message = string;
6624 GCPRO2 (args[0], message);
6625 gcpro1.nvars = 2;
6626
6627 message = Fformat (2, args);
6628
6629 if (log)
6630 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6631 else
6632 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6633
6634 UNGCPRO;
6635
6636 /* Print should start at the beginning of the message
6637 buffer next time. */
6638 message_buf_print = 0;
6639 }
6640 }
6641 }
6642
6643
6644 /* Dump an informative message to the minibuf. If M is 0, clear out
6645 any existing message, and let the mini-buffer text show through. */
6646
6647 /* VARARGS 1 */
6648 void
6649 message (m, a1, a2, a3)
6650 char *m;
6651 EMACS_INT a1, a2, a3;
6652 {
6653 if (noninteractive)
6654 {
6655 if (m)
6656 {
6657 if (noninteractive_need_newline)
6658 putc ('\n', stderr);
6659 noninteractive_need_newline = 0;
6660 fprintf (stderr, m, a1, a2, a3);
6661 if (cursor_in_echo_area == 0)
6662 fprintf (stderr, "\n");
6663 fflush (stderr);
6664 }
6665 }
6666 else if (INTERACTIVE)
6667 {
6668 /* The frame whose mini-buffer we're going to display the message
6669 on. It may be larger than the selected frame, so we need to
6670 use its buffer, not the selected frame's buffer. */
6671 Lisp_Object mini_window;
6672 struct frame *f, *sf = SELECTED_FRAME ();
6673
6674 /* Get the frame containing the mini-buffer
6675 that the selected frame is using. */
6676 mini_window = FRAME_MINIBUF_WINDOW (sf);
6677 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6678
6679 /* A null message buffer means that the frame hasn't really been
6680 initialized yet. Error messages get reported properly by
6681 cmd_error, so this must be just an informative message; toss
6682 it. */
6683 if (FRAME_MESSAGE_BUF (f))
6684 {
6685 if (m)
6686 {
6687 int len;
6688 #ifdef NO_ARG_ARRAY
6689 char *a[3];
6690 a[0] = (char *) a1;
6691 a[1] = (char *) a2;
6692 a[2] = (char *) a3;
6693
6694 len = doprnt (FRAME_MESSAGE_BUF (f),
6695 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6696 #else
6697 len = doprnt (FRAME_MESSAGE_BUF (f),
6698 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6699 (char **) &a1);
6700 #endif /* NO_ARG_ARRAY */
6701
6702 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6703 }
6704 else
6705 message1 (0);
6706
6707 /* Print should start at the beginning of the message
6708 buffer next time. */
6709 message_buf_print = 0;
6710 }
6711 }
6712 }
6713
6714
6715 /* The non-logging version of message. */
6716
6717 void
6718 message_nolog (m, a1, a2, a3)
6719 char *m;
6720 EMACS_INT a1, a2, a3;
6721 {
6722 Lisp_Object old_log_max;
6723 old_log_max = Vmessage_log_max;
6724 Vmessage_log_max = Qnil;
6725 message (m, a1, a2, a3);
6726 Vmessage_log_max = old_log_max;
6727 }
6728
6729
6730 /* Display the current message in the current mini-buffer. This is
6731 only called from error handlers in process.c, and is not time
6732 critical. */
6733
6734 void
6735 update_echo_area ()
6736 {
6737 if (!NILP (echo_area_buffer[0]))
6738 {
6739 Lisp_Object string;
6740 string = Fcurrent_message ();
6741 message3 (string, SBYTES (string),
6742 !NILP (current_buffer->enable_multibyte_characters));
6743 }
6744 }
6745
6746
6747 /* Make sure echo area buffers in `echo_buffers' are live.
6748 If they aren't, make new ones. */
6749
6750 static void
6751 ensure_echo_area_buffers ()
6752 {
6753 int i;
6754
6755 for (i = 0; i < 2; ++i)
6756 if (!BUFFERP (echo_buffer[i])
6757 || NILP (XBUFFER (echo_buffer[i])->name))
6758 {
6759 char name[30];
6760 Lisp_Object old_buffer;
6761 int j;
6762
6763 old_buffer = echo_buffer[i];
6764 sprintf (name, " *Echo Area %d*", i);
6765 echo_buffer[i] = Fget_buffer_create (build_string (name));
6766 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6767
6768 for (j = 0; j < 2; ++j)
6769 if (EQ (old_buffer, echo_area_buffer[j]))
6770 echo_area_buffer[j] = echo_buffer[i];
6771 }
6772 }
6773
6774
6775 /* Call FN with args A1..A4 with either the current or last displayed
6776 echo_area_buffer as current buffer.
6777
6778 WHICH zero means use the current message buffer
6779 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6780 from echo_buffer[] and clear it.
6781
6782 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6783 suitable buffer from echo_buffer[] and clear it.
6784
6785 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6786 that the current message becomes the last displayed one, make
6787 choose a suitable buffer for echo_area_buffer[0], and clear it.
6788
6789 Value is what FN returns. */
6790
6791 static int
6792 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6793 struct window *w;
6794 int which;
6795 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6796 EMACS_INT a1;
6797 Lisp_Object a2;
6798 EMACS_INT a3, a4;
6799 {
6800 Lisp_Object buffer;
6801 int this_one, the_other, clear_buffer_p, rc;
6802 int count = SPECPDL_INDEX ();
6803
6804 /* If buffers aren't live, make new ones. */
6805 ensure_echo_area_buffers ();
6806
6807 clear_buffer_p = 0;
6808
6809 if (which == 0)
6810 this_one = 0, the_other = 1;
6811 else if (which > 0)
6812 this_one = 1, the_other = 0;
6813 else
6814 {
6815 this_one = 0, the_other = 1;
6816 clear_buffer_p = 1;
6817
6818 /* We need a fresh one in case the current echo buffer equals
6819 the one containing the last displayed echo area message. */
6820 if (!NILP (echo_area_buffer[this_one])
6821 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6822 echo_area_buffer[this_one] = Qnil;
6823 }
6824
6825 /* Choose a suitable buffer from echo_buffer[] is we don't
6826 have one. */
6827 if (NILP (echo_area_buffer[this_one]))
6828 {
6829 echo_area_buffer[this_one]
6830 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6831 ? echo_buffer[the_other]
6832 : echo_buffer[this_one]);
6833 clear_buffer_p = 1;
6834 }
6835
6836 buffer = echo_area_buffer[this_one];
6837
6838 /* Don't get confused by reusing the buffer used for echoing
6839 for a different purpose. */
6840 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6841 cancel_echoing ();
6842
6843 record_unwind_protect (unwind_with_echo_area_buffer,
6844 with_echo_area_buffer_unwind_data (w));
6845
6846 /* Make the echo area buffer current. Note that for display
6847 purposes, it is not necessary that the displayed window's buffer
6848 == current_buffer, except for text property lookup. So, let's
6849 only set that buffer temporarily here without doing a full
6850 Fset_window_buffer. We must also change w->pointm, though,
6851 because otherwise an assertions in unshow_buffer fails, and Emacs
6852 aborts. */
6853 set_buffer_internal_1 (XBUFFER (buffer));
6854 if (w)
6855 {
6856 w->buffer = buffer;
6857 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6858 }
6859
6860 current_buffer->undo_list = Qt;
6861 current_buffer->read_only = Qnil;
6862 specbind (Qinhibit_read_only, Qt);
6863 specbind (Qinhibit_modification_hooks, Qt);
6864
6865 if (clear_buffer_p && Z > BEG)
6866 del_range (BEG, Z);
6867
6868 xassert (BEGV >= BEG);
6869 xassert (ZV <= Z && ZV >= BEGV);
6870
6871 rc = fn (a1, a2, a3, a4);
6872
6873 xassert (BEGV >= BEG);
6874 xassert (ZV <= Z && ZV >= BEGV);
6875
6876 unbind_to (count, Qnil);
6877 return rc;
6878 }
6879
6880
6881 /* Save state that should be preserved around the call to the function
6882 FN called in with_echo_area_buffer. */
6883
6884 static Lisp_Object
6885 with_echo_area_buffer_unwind_data (w)
6886 struct window *w;
6887 {
6888 int i = 0;
6889 Lisp_Object vector;
6890
6891 /* Reduce consing by keeping one vector in
6892 Vwith_echo_area_save_vector. */
6893 vector = Vwith_echo_area_save_vector;
6894 Vwith_echo_area_save_vector = Qnil;
6895
6896 if (NILP (vector))
6897 vector = Fmake_vector (make_number (7), Qnil);
6898
6899 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6900 AREF (vector, i) = Vdeactivate_mark, ++i;
6901 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6902
6903 if (w)
6904 {
6905 XSETWINDOW (AREF (vector, i), w); ++i;
6906 AREF (vector, i) = w->buffer; ++i;
6907 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6908 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6909 }
6910 else
6911 {
6912 int end = i + 4;
6913 for (; i < end; ++i)
6914 AREF (vector, i) = Qnil;
6915 }
6916
6917 xassert (i == ASIZE (vector));
6918 return vector;
6919 }
6920
6921
6922 /* Restore global state from VECTOR which was created by
6923 with_echo_area_buffer_unwind_data. */
6924
6925 static Lisp_Object
6926 unwind_with_echo_area_buffer (vector)
6927 Lisp_Object vector;
6928 {
6929 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6930 Vdeactivate_mark = AREF (vector, 1);
6931 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6932
6933 if (WINDOWP (AREF (vector, 3)))
6934 {
6935 struct window *w;
6936 Lisp_Object buffer, charpos, bytepos;
6937
6938 w = XWINDOW (AREF (vector, 3));
6939 buffer = AREF (vector, 4);
6940 charpos = AREF (vector, 5);
6941 bytepos = AREF (vector, 6);
6942
6943 w->buffer = buffer;
6944 set_marker_both (w->pointm, buffer,
6945 XFASTINT (charpos), XFASTINT (bytepos));
6946 }
6947
6948 Vwith_echo_area_save_vector = vector;
6949 return Qnil;
6950 }
6951
6952
6953 /* Set up the echo area for use by print functions. MULTIBYTE_P
6954 non-zero means we will print multibyte. */
6955
6956 void
6957 setup_echo_area_for_printing (multibyte_p)
6958 int multibyte_p;
6959 {
6960 /* If we can't find an echo area any more, exit. */
6961 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6962 Fkill_emacs (Qnil);
6963
6964 ensure_echo_area_buffers ();
6965
6966 if (!message_buf_print)
6967 {
6968 /* A message has been output since the last time we printed.
6969 Choose a fresh echo area buffer. */
6970 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6971 echo_area_buffer[0] = echo_buffer[1];
6972 else
6973 echo_area_buffer[0] = echo_buffer[0];
6974
6975 /* Switch to that buffer and clear it. */
6976 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6977 current_buffer->truncate_lines = Qnil;
6978
6979 if (Z > BEG)
6980 {
6981 int count = SPECPDL_INDEX ();
6982 specbind (Qinhibit_read_only, Qt);
6983 /* Note that undo recording is always disabled. */
6984 del_range (BEG, Z);
6985 unbind_to (count, Qnil);
6986 }
6987 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6988
6989 /* Set up the buffer for the multibyteness we need. */
6990 if (multibyte_p
6991 != !NILP (current_buffer->enable_multibyte_characters))
6992 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6993
6994 /* Raise the frame containing the echo area. */
6995 if (minibuffer_auto_raise)
6996 {
6997 struct frame *sf = SELECTED_FRAME ();
6998 Lisp_Object mini_window;
6999 mini_window = FRAME_MINIBUF_WINDOW (sf);
7000 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7001 }
7002
7003 message_log_maybe_newline ();
7004 message_buf_print = 1;
7005 }
7006 else
7007 {
7008 if (NILP (echo_area_buffer[0]))
7009 {
7010 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7011 echo_area_buffer[0] = echo_buffer[1];
7012 else
7013 echo_area_buffer[0] = echo_buffer[0];
7014 }
7015
7016 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7017 {
7018 /* Someone switched buffers between print requests. */
7019 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7020 current_buffer->truncate_lines = Qnil;
7021 }
7022 }
7023 }
7024
7025
7026 /* Display an echo area message in window W. Value is non-zero if W's
7027 height is changed. If display_last_displayed_message_p is
7028 non-zero, display the message that was last displayed, otherwise
7029 display the current message. */
7030
7031 static int
7032 display_echo_area (w)
7033 struct window *w;
7034 {
7035 int i, no_message_p, window_height_changed_p, count;
7036
7037 /* Temporarily disable garbage collections while displaying the echo
7038 area. This is done because a GC can print a message itself.
7039 That message would modify the echo area buffer's contents while a
7040 redisplay of the buffer is going on, and seriously confuse
7041 redisplay. */
7042 count = inhibit_garbage_collection ();
7043
7044 /* If there is no message, we must call display_echo_area_1
7045 nevertheless because it resizes the window. But we will have to
7046 reset the echo_area_buffer in question to nil at the end because
7047 with_echo_area_buffer will sets it to an empty buffer. */
7048 i = display_last_displayed_message_p ? 1 : 0;
7049 no_message_p = NILP (echo_area_buffer[i]);
7050
7051 window_height_changed_p
7052 = with_echo_area_buffer (w, display_last_displayed_message_p,
7053 display_echo_area_1,
7054 (EMACS_INT) w, Qnil, 0, 0);
7055
7056 if (no_message_p)
7057 echo_area_buffer[i] = Qnil;
7058
7059 unbind_to (count, Qnil);
7060 return window_height_changed_p;
7061 }
7062
7063
7064 /* Helper for display_echo_area. Display the current buffer which
7065 contains the current echo area message in window W, a mini-window,
7066 a pointer to which is passed in A1. A2..A4 are currently not used.
7067 Change the height of W so that all of the message is displayed.
7068 Value is non-zero if height of W was changed. */
7069
7070 static int
7071 display_echo_area_1 (a1, a2, a3, a4)
7072 EMACS_INT a1;
7073 Lisp_Object a2;
7074 EMACS_INT a3, a4;
7075 {
7076 struct window *w = (struct window *) a1;
7077 Lisp_Object window;
7078 struct text_pos start;
7079 int window_height_changed_p = 0;
7080
7081 /* Do this before displaying, so that we have a large enough glyph
7082 matrix for the display. */
7083 window_height_changed_p = resize_mini_window (w, 0);
7084
7085 /* Display. */
7086 clear_glyph_matrix (w->desired_matrix);
7087 XSETWINDOW (window, w);
7088 SET_TEXT_POS (start, BEG, BEG_BYTE);
7089 try_window (window, start);
7090
7091 return window_height_changed_p;
7092 }
7093
7094
7095 /* Resize the echo area window to exactly the size needed for the
7096 currently displayed message, if there is one. If a mini-buffer
7097 is active, don't shrink it. */
7098
7099 void
7100 resize_echo_area_exactly ()
7101 {
7102 if (BUFFERP (echo_area_buffer[0])
7103 && WINDOWP (echo_area_window))
7104 {
7105 struct window *w = XWINDOW (echo_area_window);
7106 int resized_p;
7107 Lisp_Object resize_exactly;
7108
7109 if (minibuf_level == 0)
7110 resize_exactly = Qt;
7111 else
7112 resize_exactly = Qnil;
7113
7114 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7115 (EMACS_INT) w, resize_exactly, 0, 0);
7116 if (resized_p)
7117 {
7118 ++windows_or_buffers_changed;
7119 ++update_mode_lines;
7120 redisplay_internal (0);
7121 }
7122 }
7123 }
7124
7125
7126 /* Callback function for with_echo_area_buffer, when used from
7127 resize_echo_area_exactly. A1 contains a pointer to the window to
7128 resize, EXACTLY non-nil means resize the mini-window exactly to the
7129 size of the text displayed. A3 and A4 are not used. Value is what
7130 resize_mini_window returns. */
7131
7132 static int
7133 resize_mini_window_1 (a1, exactly, a3, a4)
7134 EMACS_INT a1;
7135 Lisp_Object exactly;
7136 EMACS_INT a3, a4;
7137 {
7138 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7139 }
7140
7141
7142 /* Resize mini-window W to fit the size of its contents. EXACT:P
7143 means size the window exactly to the size needed. Otherwise, it's
7144 only enlarged until W's buffer is empty. Value is non-zero if
7145 the window height has been changed. */
7146
7147 int
7148 resize_mini_window (w, exact_p)
7149 struct window *w;
7150 int exact_p;
7151 {
7152 struct frame *f = XFRAME (w->frame);
7153 int window_height_changed_p = 0;
7154
7155 xassert (MINI_WINDOW_P (w));
7156
7157 /* Don't resize windows while redisplaying a window; it would
7158 confuse redisplay functions when the size of the window they are
7159 displaying changes from under them. Such a resizing can happen,
7160 for instance, when which-func prints a long message while
7161 we are running fontification-functions. We're running these
7162 functions with safe_call which binds inhibit-redisplay to t. */
7163 if (!NILP (Vinhibit_redisplay))
7164 return 0;
7165
7166 /* Nil means don't try to resize. */
7167 if (NILP (Vresize_mini_windows)
7168 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7169 return 0;
7170
7171 if (!FRAME_MINIBUF_ONLY_P (f))
7172 {
7173 struct it it;
7174 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7175 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7176 int height, max_height;
7177 int unit = FRAME_LINE_HEIGHT (f);
7178 struct text_pos start;
7179 struct buffer *old_current_buffer = NULL;
7180
7181 if (current_buffer != XBUFFER (w->buffer))
7182 {
7183 old_current_buffer = current_buffer;
7184 set_buffer_internal (XBUFFER (w->buffer));
7185 }
7186
7187 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7188
7189 /* Compute the max. number of lines specified by the user. */
7190 if (FLOATP (Vmax_mini_window_height))
7191 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7192 else if (INTEGERP (Vmax_mini_window_height))
7193 max_height = XINT (Vmax_mini_window_height);
7194 else
7195 max_height = total_height / 4;
7196
7197 /* Correct that max. height if it's bogus. */
7198 max_height = max (1, max_height);
7199 max_height = min (total_height, max_height);
7200
7201 /* Find out the height of the text in the window. */
7202 if (it.truncate_lines_p)
7203 height = 1;
7204 else
7205 {
7206 last_height = 0;
7207 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7208 if (it.max_ascent == 0 && it.max_descent == 0)
7209 height = it.current_y + last_height;
7210 else
7211 height = it.current_y + it.max_ascent + it.max_descent;
7212 height -= it.extra_line_spacing;
7213 height = (height + unit - 1) / unit;
7214 }
7215
7216 /* Compute a suitable window start. */
7217 if (height > max_height)
7218 {
7219 height = max_height;
7220 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7221 move_it_vertically_backward (&it, (height - 1) * unit);
7222 start = it.current.pos;
7223 }
7224 else
7225 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7226 SET_MARKER_FROM_TEXT_POS (w->start, start);
7227
7228 if (EQ (Vresize_mini_windows, Qgrow_only))
7229 {
7230 /* Let it grow only, until we display an empty message, in which
7231 case the window shrinks again. */
7232 if (height > WINDOW_TOTAL_LINES (w))
7233 {
7234 int old_height = WINDOW_TOTAL_LINES (w);
7235 freeze_window_starts (f, 1);
7236 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7237 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7238 }
7239 else if (height < WINDOW_TOTAL_LINES (w)
7240 && (exact_p || BEGV == ZV))
7241 {
7242 int old_height = WINDOW_TOTAL_LINES (w);
7243 freeze_window_starts (f, 0);
7244 shrink_mini_window (w);
7245 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7246 }
7247 }
7248 else
7249 {
7250 /* Always resize to exact size needed. */
7251 if (height > WINDOW_TOTAL_LINES (w))
7252 {
7253 int old_height = WINDOW_TOTAL_LINES (w);
7254 freeze_window_starts (f, 1);
7255 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7256 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7257 }
7258 else if (height < WINDOW_TOTAL_LINES (w))
7259 {
7260 int old_height = WINDOW_TOTAL_LINES (w);
7261 freeze_window_starts (f, 0);
7262 shrink_mini_window (w);
7263
7264 if (height)
7265 {
7266 freeze_window_starts (f, 1);
7267 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7268 }
7269
7270 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7271 }
7272 }
7273
7274 if (old_current_buffer)
7275 set_buffer_internal (old_current_buffer);
7276 }
7277
7278 return window_height_changed_p;
7279 }
7280
7281
7282 /* Value is the current message, a string, or nil if there is no
7283 current message. */
7284
7285 Lisp_Object
7286 current_message ()
7287 {
7288 Lisp_Object msg;
7289
7290 if (NILP (echo_area_buffer[0]))
7291 msg = Qnil;
7292 else
7293 {
7294 with_echo_area_buffer (0, 0, current_message_1,
7295 (EMACS_INT) &msg, Qnil, 0, 0);
7296 if (NILP (msg))
7297 echo_area_buffer[0] = Qnil;
7298 }
7299
7300 return msg;
7301 }
7302
7303
7304 static int
7305 current_message_1 (a1, a2, a3, a4)
7306 EMACS_INT a1;
7307 Lisp_Object a2;
7308 EMACS_INT a3, a4;
7309 {
7310 Lisp_Object *msg = (Lisp_Object *) a1;
7311
7312 if (Z > BEG)
7313 *msg = make_buffer_string (BEG, Z, 1);
7314 else
7315 *msg = Qnil;
7316 return 0;
7317 }
7318
7319
7320 /* Push the current message on Vmessage_stack for later restauration
7321 by restore_message. Value is non-zero if the current message isn't
7322 empty. This is a relatively infrequent operation, so it's not
7323 worth optimizing. */
7324
7325 int
7326 push_message ()
7327 {
7328 Lisp_Object msg;
7329 msg = current_message ();
7330 Vmessage_stack = Fcons (msg, Vmessage_stack);
7331 return STRINGP (msg);
7332 }
7333
7334
7335 /* Restore message display from the top of Vmessage_stack. */
7336
7337 void
7338 restore_message ()
7339 {
7340 Lisp_Object msg;
7341
7342 xassert (CONSP (Vmessage_stack));
7343 msg = XCAR (Vmessage_stack);
7344 if (STRINGP (msg))
7345 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7346 else
7347 message3_nolog (msg, 0, 0);
7348 }
7349
7350
7351 /* Handler for record_unwind_protect calling pop_message. */
7352
7353 Lisp_Object
7354 pop_message_unwind (dummy)
7355 Lisp_Object dummy;
7356 {
7357 pop_message ();
7358 return Qnil;
7359 }
7360
7361 /* Pop the top-most entry off Vmessage_stack. */
7362
7363 void
7364 pop_message ()
7365 {
7366 xassert (CONSP (Vmessage_stack));
7367 Vmessage_stack = XCDR (Vmessage_stack);
7368 }
7369
7370
7371 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7372 exits. If the stack is not empty, we have a missing pop_message
7373 somewhere. */
7374
7375 void
7376 check_message_stack ()
7377 {
7378 if (!NILP (Vmessage_stack))
7379 abort ();
7380 }
7381
7382
7383 /* Truncate to NCHARS what will be displayed in the echo area the next
7384 time we display it---but don't redisplay it now. */
7385
7386 void
7387 truncate_echo_area (nchars)
7388 int nchars;
7389 {
7390 if (nchars == 0)
7391 echo_area_buffer[0] = Qnil;
7392 /* A null message buffer means that the frame hasn't really been
7393 initialized yet. Error messages get reported properly by
7394 cmd_error, so this must be just an informative message; toss it. */
7395 else if (!noninteractive
7396 && INTERACTIVE
7397 && !NILP (echo_area_buffer[0]))
7398 {
7399 struct frame *sf = SELECTED_FRAME ();
7400 if (FRAME_MESSAGE_BUF (sf))
7401 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7402 }
7403 }
7404
7405
7406 /* Helper function for truncate_echo_area. Truncate the current
7407 message to at most NCHARS characters. */
7408
7409 static int
7410 truncate_message_1 (nchars, a2, a3, a4)
7411 EMACS_INT nchars;
7412 Lisp_Object a2;
7413 EMACS_INT a3, a4;
7414 {
7415 if (BEG + nchars < Z)
7416 del_range (BEG + nchars, Z);
7417 if (Z == BEG)
7418 echo_area_buffer[0] = Qnil;
7419 return 0;
7420 }
7421
7422
7423 /* Set the current message to a substring of S or STRING.
7424
7425 If STRING is a Lisp string, set the message to the first NBYTES
7426 bytes from STRING. NBYTES zero means use the whole string. If
7427 STRING is multibyte, the message will be displayed multibyte.
7428
7429 If S is not null, set the message to the first LEN bytes of S. LEN
7430 zero means use the whole string. MULTIBYTE_P non-zero means S is
7431 multibyte. Display the message multibyte in that case. */
7432
7433 void
7434 set_message (s, string, nbytes, multibyte_p)
7435 const char *s;
7436 Lisp_Object string;
7437 int nbytes, multibyte_p;
7438 {
7439 message_enable_multibyte
7440 = ((s && multibyte_p)
7441 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7442
7443 with_echo_area_buffer (0, -1, set_message_1,
7444 (EMACS_INT) s, string, nbytes, multibyte_p);
7445 message_buf_print = 0;
7446 help_echo_showing_p = 0;
7447 }
7448
7449
7450 /* Helper function for set_message. Arguments have the same meaning
7451 as there, with A1 corresponding to S and A2 corresponding to STRING
7452 This function is called with the echo area buffer being
7453 current. */
7454
7455 static int
7456 set_message_1 (a1, a2, nbytes, multibyte_p)
7457 EMACS_INT a1;
7458 Lisp_Object a2;
7459 EMACS_INT nbytes, multibyte_p;
7460 {
7461 const char *s = (const char *) a1;
7462 Lisp_Object string = a2;
7463
7464 xassert (BEG == Z);
7465
7466 /* Change multibyteness of the echo buffer appropriately. */
7467 if (message_enable_multibyte
7468 != !NILP (current_buffer->enable_multibyte_characters))
7469 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7470
7471 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7472
7473 /* Insert new message at BEG. */
7474 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7475
7476 if (STRINGP (string))
7477 {
7478 int nchars;
7479
7480 if (nbytes == 0)
7481 nbytes = SBYTES (string);
7482 nchars = string_byte_to_char (string, nbytes);
7483
7484 /* This function takes care of single/multibyte conversion. We
7485 just have to ensure that the echo area buffer has the right
7486 setting of enable_multibyte_characters. */
7487 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7488 }
7489 else if (s)
7490 {
7491 if (nbytes == 0)
7492 nbytes = strlen (s);
7493
7494 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7495 {
7496 /* Convert from multi-byte to single-byte. */
7497 int i, c, n;
7498 unsigned char work[1];
7499
7500 /* Convert a multibyte string to single-byte. */
7501 for (i = 0; i < nbytes; i += n)
7502 {
7503 c = string_char_and_length (s + i, nbytes - i, &n);
7504 work[0] = (SINGLE_BYTE_CHAR_P (c)
7505 ? c
7506 : multibyte_char_to_unibyte (c, Qnil));
7507 insert_1_both (work, 1, 1, 1, 0, 0);
7508 }
7509 }
7510 else if (!multibyte_p
7511 && !NILP (current_buffer->enable_multibyte_characters))
7512 {
7513 /* Convert from single-byte to multi-byte. */
7514 int i, c, n;
7515 const unsigned char *msg = (const unsigned char *) s;
7516 unsigned char str[MAX_MULTIBYTE_LENGTH];
7517
7518 /* Convert a single-byte string to multibyte. */
7519 for (i = 0; i < nbytes; i++)
7520 {
7521 c = unibyte_char_to_multibyte (msg[i]);
7522 n = CHAR_STRING (c, str);
7523 insert_1_both (str, 1, n, 1, 0, 0);
7524 }
7525 }
7526 else
7527 insert_1 (s, nbytes, 1, 0, 0);
7528 }
7529
7530 return 0;
7531 }
7532
7533
7534 /* Clear messages. CURRENT_P non-zero means clear the current
7535 message. LAST_DISPLAYED_P non-zero means clear the message
7536 last displayed. */
7537
7538 void
7539 clear_message (current_p, last_displayed_p)
7540 int current_p, last_displayed_p;
7541 {
7542 if (current_p)
7543 {
7544 echo_area_buffer[0] = Qnil;
7545 message_cleared_p = 1;
7546 }
7547
7548 if (last_displayed_p)
7549 echo_area_buffer[1] = Qnil;
7550
7551 message_buf_print = 0;
7552 }
7553
7554 /* Clear garbaged frames.
7555
7556 This function is used where the old redisplay called
7557 redraw_garbaged_frames which in turn called redraw_frame which in
7558 turn called clear_frame. The call to clear_frame was a source of
7559 flickering. I believe a clear_frame is not necessary. It should
7560 suffice in the new redisplay to invalidate all current matrices,
7561 and ensure a complete redisplay of all windows. */
7562
7563 static void
7564 clear_garbaged_frames ()
7565 {
7566 if (frame_garbaged)
7567 {
7568 Lisp_Object tail, frame;
7569 int changed_count = 0;
7570
7571 FOR_EACH_FRAME (tail, frame)
7572 {
7573 struct frame *f = XFRAME (frame);
7574
7575 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7576 {
7577 if (f->resized_p)
7578 Fredraw_frame (frame);
7579 clear_current_matrices (f);
7580 changed_count++;
7581 f->garbaged = 0;
7582 f->resized_p = 0;
7583 }
7584 }
7585
7586 frame_garbaged = 0;
7587 if (changed_count)
7588 ++windows_or_buffers_changed;
7589 }
7590 }
7591
7592
7593 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7594 is non-zero update selected_frame. Value is non-zero if the
7595 mini-windows height has been changed. */
7596
7597 static int
7598 echo_area_display (update_frame_p)
7599 int update_frame_p;
7600 {
7601 Lisp_Object mini_window;
7602 struct window *w;
7603 struct frame *f;
7604 int window_height_changed_p = 0;
7605 struct frame *sf = SELECTED_FRAME ();
7606
7607 mini_window = FRAME_MINIBUF_WINDOW (sf);
7608 w = XWINDOW (mini_window);
7609 f = XFRAME (WINDOW_FRAME (w));
7610
7611 /* Don't display if frame is invisible or not yet initialized. */
7612 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7613 return 0;
7614
7615 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7616 #ifndef MAC_OS8
7617 #ifdef HAVE_WINDOW_SYSTEM
7618 /* When Emacs starts, selected_frame may be the initial terminal
7619 frame. If we let this through, a message would be displayed on
7620 the terminal. */
7621 if (FRAME_TERMCAP_P (XFRAME (selected_frame))
7622 && FRAME_TTY (XFRAME (selected_frame))->type == NULL)
7623 return 0;
7624 #endif /* HAVE_WINDOW_SYSTEM */
7625 #endif
7626
7627 /* Redraw garbaged frames. */
7628 if (frame_garbaged)
7629 clear_garbaged_frames ();
7630
7631 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7632 {
7633 echo_area_window = mini_window;
7634 window_height_changed_p = display_echo_area (w);
7635 w->must_be_updated_p = 1;
7636
7637 /* Update the display, unless called from redisplay_internal.
7638 Also don't update the screen during redisplay itself. The
7639 update will happen at the end of redisplay, and an update
7640 here could cause confusion. */
7641 if (update_frame_p && !redisplaying_p)
7642 {
7643 int n = 0;
7644
7645 /* If the display update has been interrupted by pending
7646 input, update mode lines in the frame. Due to the
7647 pending input, it might have been that redisplay hasn't
7648 been called, so that mode lines above the echo area are
7649 garbaged. This looks odd, so we prevent it here. */
7650 if (!display_completed)
7651 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7652
7653 if (window_height_changed_p
7654 /* Don't do this if Emacs is shutting down. Redisplay
7655 needs to run hooks. */
7656 && !NILP (Vrun_hooks))
7657 {
7658 /* Must update other windows. Likewise as in other
7659 cases, don't let this update be interrupted by
7660 pending input. */
7661 int count = SPECPDL_INDEX ();
7662 specbind (Qredisplay_dont_pause, Qt);
7663 windows_or_buffers_changed = 1;
7664 redisplay_internal (0);
7665 unbind_to (count, Qnil);
7666 }
7667 else if (FRAME_WINDOW_P (f) && n == 0)
7668 {
7669 /* Window configuration is the same as before.
7670 Can do with a display update of the echo area,
7671 unless we displayed some mode lines. */
7672 update_single_window (w, 1);
7673 FRAME_RIF (f)->flush_display (f);
7674 }
7675 else
7676 update_frame (f, 1, 1);
7677
7678 /* If cursor is in the echo area, make sure that the next
7679 redisplay displays the minibuffer, so that the cursor will
7680 be replaced with what the minibuffer wants. */
7681 if (cursor_in_echo_area)
7682 ++windows_or_buffers_changed;
7683 }
7684 }
7685 else if (!EQ (mini_window, selected_window))
7686 windows_or_buffers_changed++;
7687
7688 /* Last displayed message is now the current message. */
7689 echo_area_buffer[1] = echo_area_buffer[0];
7690
7691 /* Prevent redisplay optimization in redisplay_internal by resetting
7692 this_line_start_pos. This is done because the mini-buffer now
7693 displays the message instead of its buffer text. */
7694 if (EQ (mini_window, selected_window))
7695 CHARPOS (this_line_start_pos) = 0;
7696
7697 return window_height_changed_p;
7698 }
7699
7700
7701 \f
7702 /***********************************************************************
7703 Frame Titles
7704 ***********************************************************************/
7705
7706
7707 /* The frame title buffering code is also used by Fformat_mode_line.
7708 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7709
7710 /* A buffer for constructing frame titles in it; allocated from the
7711 heap in init_xdisp and resized as needed in store_frame_title_char. */
7712
7713 static char *frame_title_buf;
7714
7715 /* The buffer's end, and a current output position in it. */
7716
7717 static char *frame_title_buf_end;
7718 static char *frame_title_ptr;
7719
7720
7721 /* Store a single character C for the frame title in frame_title_buf.
7722 Re-allocate frame_title_buf if necessary. */
7723
7724 static void
7725 #ifdef PROTOTYPES
7726 store_frame_title_char (char c)
7727 #else
7728 store_frame_title_char (c)
7729 char c;
7730 #endif
7731 {
7732 /* If output position has reached the end of the allocated buffer,
7733 double the buffer's size. */
7734 if (frame_title_ptr == frame_title_buf_end)
7735 {
7736 int len = frame_title_ptr - frame_title_buf;
7737 int new_size = 2 * len * sizeof *frame_title_buf;
7738 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7739 frame_title_buf_end = frame_title_buf + new_size;
7740 frame_title_ptr = frame_title_buf + len;
7741 }
7742
7743 *frame_title_ptr++ = c;
7744 }
7745
7746
7747 /* Store part of a frame title in frame_title_buf, beginning at
7748 frame_title_ptr. STR is the string to store. Do not copy
7749 characters that yield more columns than PRECISION; PRECISION <= 0
7750 means copy the whole string. Pad with spaces until FIELD_WIDTH
7751 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7752 pad. Called from display_mode_element when it is used to build a
7753 frame title. */
7754
7755 static int
7756 store_frame_title (str, field_width, precision)
7757 const unsigned char *str;
7758 int field_width, precision;
7759 {
7760 int n = 0;
7761 int dummy, nbytes;
7762
7763 /* Copy at most PRECISION chars from STR. */
7764 nbytes = strlen (str);
7765 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7766 while (nbytes--)
7767 store_frame_title_char (*str++);
7768
7769 /* Fill up with spaces until FIELD_WIDTH reached. */
7770 while (field_width > 0
7771 && n < field_width)
7772 {
7773 store_frame_title_char (' ');
7774 ++n;
7775 }
7776
7777 return n;
7778 }
7779
7780 #ifdef HAVE_WINDOW_SYSTEM
7781
7782 /* Set the title of FRAME, if it has changed. The title format is
7783 Vicon_title_format if FRAME is iconified, otherwise it is
7784 frame_title_format. */
7785
7786 static void
7787 x_consider_frame_title (frame)
7788 Lisp_Object frame;
7789 {
7790 struct frame *f = XFRAME (frame);
7791
7792 if (FRAME_WINDOW_P (f)
7793 || FRAME_MINIBUF_ONLY_P (f)
7794 || f->explicit_name)
7795 {
7796 /* Do we have more than one visible frame on this X display? */
7797 Lisp_Object tail;
7798 Lisp_Object fmt;
7799 struct buffer *obuf;
7800 int len;
7801 struct it it;
7802
7803 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7804 {
7805 Lisp_Object other_frame = XCAR (tail);
7806 struct frame *tf = XFRAME (other_frame);
7807
7808 if (tf != f
7809 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7810 && !FRAME_MINIBUF_ONLY_P (tf)
7811 && !EQ (other_frame, tip_frame)
7812 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7813 break;
7814 }
7815
7816 /* Set global variable indicating that multiple frames exist. */
7817 multiple_frames = CONSP (tail);
7818
7819 /* Switch to the buffer of selected window of the frame. Set up
7820 frame_title_ptr so that display_mode_element will output into it;
7821 then display the title. */
7822 obuf = current_buffer;
7823 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7824 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7825 frame_title_ptr = frame_title_buf;
7826 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7827 NULL, DEFAULT_FACE_ID);
7828 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7829 len = frame_title_ptr - frame_title_buf;
7830 frame_title_ptr = NULL;
7831 set_buffer_internal_1 (obuf);
7832
7833 /* Set the title only if it's changed. This avoids consing in
7834 the common case where it hasn't. (If it turns out that we've
7835 already wasted too much time by walking through the list with
7836 display_mode_element, then we might need to optimize at a
7837 higher level than this.) */
7838 if (! STRINGP (f->name)
7839 || SBYTES (f->name) != len
7840 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7841 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7842 }
7843 }
7844
7845 #endif /* not HAVE_WINDOW_SYSTEM */
7846
7847
7848
7849 \f
7850 /***********************************************************************
7851 Menu Bars
7852 ***********************************************************************/
7853
7854
7855 /* Prepare for redisplay by updating menu-bar item lists when
7856 appropriate. This can call eval. */
7857
7858 void
7859 prepare_menu_bars ()
7860 {
7861 int all_windows;
7862 struct gcpro gcpro1, gcpro2;
7863 struct frame *f;
7864 Lisp_Object tooltip_frame;
7865
7866 #ifdef HAVE_WINDOW_SYSTEM
7867 tooltip_frame = tip_frame;
7868 #else
7869 tooltip_frame = Qnil;
7870 #endif
7871
7872 /* Update all frame titles based on their buffer names, etc. We do
7873 this before the menu bars so that the buffer-menu will show the
7874 up-to-date frame titles. */
7875 #ifdef HAVE_WINDOW_SYSTEM
7876 if (windows_or_buffers_changed || update_mode_lines)
7877 {
7878 Lisp_Object tail, frame;
7879
7880 FOR_EACH_FRAME (tail, frame)
7881 {
7882 f = XFRAME (frame);
7883 if (!EQ (frame, tooltip_frame)
7884 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7885 x_consider_frame_title (frame);
7886 }
7887 }
7888 #endif /* HAVE_WINDOW_SYSTEM */
7889
7890 /* Update the menu bar item lists, if appropriate. This has to be
7891 done before any actual redisplay or generation of display lines. */
7892 all_windows = (update_mode_lines
7893 || buffer_shared > 1
7894 || windows_or_buffers_changed);
7895 if (all_windows)
7896 {
7897 Lisp_Object tail, frame;
7898 int count = SPECPDL_INDEX ();
7899
7900 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7901
7902 FOR_EACH_FRAME (tail, frame)
7903 {
7904 f = XFRAME (frame);
7905
7906 /* Ignore tooltip frame. */
7907 if (EQ (frame, tooltip_frame))
7908 continue;
7909
7910 /* If a window on this frame changed size, report that to
7911 the user and clear the size-change flag. */
7912 if (FRAME_WINDOW_SIZES_CHANGED (f))
7913 {
7914 Lisp_Object functions;
7915
7916 /* Clear flag first in case we get an error below. */
7917 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7918 functions = Vwindow_size_change_functions;
7919 GCPRO2 (tail, functions);
7920
7921 while (CONSP (functions))
7922 {
7923 call1 (XCAR (functions), frame);
7924 functions = XCDR (functions);
7925 }
7926 UNGCPRO;
7927 }
7928
7929 GCPRO1 (tail);
7930 update_menu_bar (f, 0);
7931 #ifdef HAVE_WINDOW_SYSTEM
7932 update_tool_bar (f, 0);
7933 #endif
7934 UNGCPRO;
7935 }
7936
7937 unbind_to (count, Qnil);
7938 }
7939 else
7940 {
7941 struct frame *sf = SELECTED_FRAME ();
7942 update_menu_bar (sf, 1);
7943 #ifdef HAVE_WINDOW_SYSTEM
7944 update_tool_bar (sf, 1);
7945 #endif
7946 }
7947
7948 /* Motif needs this. See comment in xmenu.c. Turn it off when
7949 pending_menu_activation is not defined. */
7950 #ifdef USE_X_TOOLKIT
7951 pending_menu_activation = 0;
7952 #endif
7953 }
7954
7955
7956 /* Update the menu bar item list for frame F. This has to be done
7957 before we start to fill in any display lines, because it can call
7958 eval.
7959
7960 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7961
7962 static void
7963 update_menu_bar (f, save_match_data)
7964 struct frame *f;
7965 int save_match_data;
7966 {
7967 Lisp_Object window;
7968 register struct window *w;
7969
7970 /* If called recursively during a menu update, do nothing. This can
7971 happen when, for instance, an activate-menubar-hook causes a
7972 redisplay. */
7973 if (inhibit_menubar_update)
7974 return;
7975
7976 window = FRAME_SELECTED_WINDOW (f);
7977 w = XWINDOW (window);
7978
7979 #if 0 /* The if statement below this if statement used to include the
7980 condition !NILP (w->update_mode_line), rather than using
7981 update_mode_lines directly, and this if statement may have
7982 been added to make that condition work. Now the if
7983 statement below matches its comment, this isn't needed. */
7984 if (update_mode_lines)
7985 w->update_mode_line = Qt;
7986 #endif
7987
7988 if (FRAME_WINDOW_P (f)
7989 ?
7990 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7991 || defined (USE_GTK)
7992 FRAME_EXTERNAL_MENU_BAR (f)
7993 #else
7994 FRAME_MENU_BAR_LINES (f) > 0
7995 #endif
7996 : FRAME_MENU_BAR_LINES (f) > 0)
7997 {
7998 /* If the user has switched buffers or windows, we need to
7999 recompute to reflect the new bindings. But we'll
8000 recompute when update_mode_lines is set too; that means
8001 that people can use force-mode-line-update to request
8002 that the menu bar be recomputed. The adverse effect on
8003 the rest of the redisplay algorithm is about the same as
8004 windows_or_buffers_changed anyway. */
8005 if (windows_or_buffers_changed
8006 /* This used to test w->update_mode_line, but we believe
8007 there is no need to recompute the menu in that case. */
8008 || update_mode_lines
8009 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8010 < BUF_MODIFF (XBUFFER (w->buffer)))
8011 != !NILP (w->last_had_star))
8012 || ((!NILP (Vtransient_mark_mode)
8013 && !NILP (XBUFFER (w->buffer)->mark_active))
8014 != !NILP (w->region_showing)))
8015 {
8016 struct buffer *prev = current_buffer;
8017 int count = SPECPDL_INDEX ();
8018
8019 specbind (Qinhibit_menubar_update, Qt);
8020
8021 set_buffer_internal_1 (XBUFFER (w->buffer));
8022 if (save_match_data)
8023 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8024 if (NILP (Voverriding_local_map_menu_flag))
8025 {
8026 specbind (Qoverriding_terminal_local_map, Qnil);
8027 specbind (Qoverriding_local_map, Qnil);
8028 }
8029
8030 /* Run the Lucid hook. */
8031 safe_run_hooks (Qactivate_menubar_hook);
8032
8033 /* If it has changed current-menubar from previous value,
8034 really recompute the menu-bar from the value. */
8035 if (! NILP (Vlucid_menu_bar_dirty_flag))
8036 call0 (Qrecompute_lucid_menubar);
8037
8038 safe_run_hooks (Qmenu_bar_update_hook);
8039 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8040
8041 /* Redisplay the menu bar in case we changed it. */
8042 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8043 || defined (USE_GTK)
8044 if (FRAME_WINDOW_P (f)
8045 #if defined (MAC_OS)
8046 /* All frames on Mac OS share the same menubar. So only the
8047 selected frame should be allowed to set it. */
8048 && f == SELECTED_FRAME ()
8049 #endif
8050 )
8051 set_frame_menubar (f, 0, 0);
8052 else
8053 /* On a terminal screen, the menu bar is an ordinary screen
8054 line, and this makes it get updated. */
8055 w->update_mode_line = Qt;
8056 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8057 /* In the non-toolkit version, the menu bar is an ordinary screen
8058 line, and this makes it get updated. */
8059 w->update_mode_line = Qt;
8060 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8061
8062 unbind_to (count, Qnil);
8063 set_buffer_internal_1 (prev);
8064 }
8065 }
8066 }
8067
8068
8069 \f
8070 /***********************************************************************
8071 Output Cursor
8072 ***********************************************************************/
8073
8074 #ifdef HAVE_WINDOW_SYSTEM
8075
8076 /* EXPORT:
8077 Nominal cursor position -- where to draw output.
8078 HPOS and VPOS are window relative glyph matrix coordinates.
8079 X and Y are window relative pixel coordinates. */
8080
8081 struct cursor_pos output_cursor;
8082
8083
8084 /* EXPORT:
8085 Set the global variable output_cursor to CURSOR. All cursor
8086 positions are relative to updated_window. */
8087
8088 void
8089 set_output_cursor (cursor)
8090 struct cursor_pos *cursor;
8091 {
8092 output_cursor.hpos = cursor->hpos;
8093 output_cursor.vpos = cursor->vpos;
8094 output_cursor.x = cursor->x;
8095 output_cursor.y = cursor->y;
8096 }
8097
8098
8099 /* EXPORT for RIF:
8100 Set a nominal cursor position.
8101
8102 HPOS and VPOS are column/row positions in a window glyph matrix. X
8103 and Y are window text area relative pixel positions.
8104
8105 If this is done during an update, updated_window will contain the
8106 window that is being updated and the position is the future output
8107 cursor position for that window. If updated_window is null, use
8108 selected_window and display the cursor at the given position. */
8109
8110 void
8111 x_cursor_to (vpos, hpos, y, x)
8112 int vpos, hpos, y, x;
8113 {
8114 struct window *w;
8115
8116 /* If updated_window is not set, work on selected_window. */
8117 if (updated_window)
8118 w = updated_window;
8119 else
8120 w = XWINDOW (selected_window);
8121
8122 /* Set the output cursor. */
8123 output_cursor.hpos = hpos;
8124 output_cursor.vpos = vpos;
8125 output_cursor.x = x;
8126 output_cursor.y = y;
8127
8128 /* If not called as part of an update, really display the cursor.
8129 This will also set the cursor position of W. */
8130 if (updated_window == NULL)
8131 {
8132 BLOCK_INPUT;
8133 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8134 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
8135 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
8136 UNBLOCK_INPUT;
8137 }
8138 }
8139
8140 #endif /* HAVE_WINDOW_SYSTEM */
8141
8142 \f
8143 /***********************************************************************
8144 Tool-bars
8145 ***********************************************************************/
8146
8147 #ifdef HAVE_WINDOW_SYSTEM
8148
8149 /* Where the mouse was last time we reported a mouse event. */
8150
8151 FRAME_PTR last_mouse_frame;
8152
8153 /* Tool-bar item index of the item on which a mouse button was pressed
8154 or -1. */
8155
8156 int last_tool_bar_item;
8157
8158
8159 /* Update the tool-bar item list for frame F. This has to be done
8160 before we start to fill in any display lines. Called from
8161 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8162 and restore it here. */
8163
8164 static void
8165 update_tool_bar (f, save_match_data)
8166 struct frame *f;
8167 int save_match_data;
8168 {
8169 #ifdef USE_GTK
8170 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8171 #else
8172 int do_update = WINDOWP (f->tool_bar_window)
8173 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8174 #endif
8175
8176 if (do_update)
8177 {
8178 Lisp_Object window;
8179 struct window *w;
8180
8181 window = FRAME_SELECTED_WINDOW (f);
8182 w = XWINDOW (window);
8183
8184 /* If the user has switched buffers or windows, we need to
8185 recompute to reflect the new bindings. But we'll
8186 recompute when update_mode_lines is set too; that means
8187 that people can use force-mode-line-update to request
8188 that the menu bar be recomputed. The adverse effect on
8189 the rest of the redisplay algorithm is about the same as
8190 windows_or_buffers_changed anyway. */
8191 if (windows_or_buffers_changed
8192 || !NILP (w->update_mode_line)
8193 || update_mode_lines
8194 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8195 < BUF_MODIFF (XBUFFER (w->buffer)))
8196 != !NILP (w->last_had_star))
8197 || ((!NILP (Vtransient_mark_mode)
8198 && !NILP (XBUFFER (w->buffer)->mark_active))
8199 != !NILP (w->region_showing)))
8200 {
8201 struct buffer *prev = current_buffer;
8202 int count = SPECPDL_INDEX ();
8203 Lisp_Object old_tool_bar;
8204 struct gcpro gcpro1;
8205
8206 /* Set current_buffer to the buffer of the selected
8207 window of the frame, so that we get the right local
8208 keymaps. */
8209 set_buffer_internal_1 (XBUFFER (w->buffer));
8210
8211 /* Save match data, if we must. */
8212 if (save_match_data)
8213 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8214
8215 /* Make sure that we don't accidentally use bogus keymaps. */
8216 if (NILP (Voverriding_local_map_menu_flag))
8217 {
8218 specbind (Qoverriding_terminal_local_map, Qnil);
8219 specbind (Qoverriding_local_map, Qnil);
8220 }
8221
8222 old_tool_bar = f->tool_bar_items;
8223 GCPRO1 (old_tool_bar);
8224
8225 /* Build desired tool-bar items from keymaps. */
8226 BLOCK_INPUT;
8227 f->tool_bar_items
8228 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8229 UNBLOCK_INPUT;
8230
8231 /* Redisplay the tool-bar if we changed it. */
8232 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8233 w->update_mode_line = Qt;
8234
8235 UNGCPRO;
8236
8237 unbind_to (count, Qnil);
8238 set_buffer_internal_1 (prev);
8239 }
8240 }
8241 }
8242
8243
8244 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8245 F's desired tool-bar contents. F->tool_bar_items must have
8246 been set up previously by calling prepare_menu_bars. */
8247
8248 static void
8249 build_desired_tool_bar_string (f)
8250 struct frame *f;
8251 {
8252 int i, size, size_needed;
8253 struct gcpro gcpro1, gcpro2, gcpro3;
8254 Lisp_Object image, plist, props;
8255
8256 image = plist = props = Qnil;
8257 GCPRO3 (image, plist, props);
8258
8259 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8260 Otherwise, make a new string. */
8261
8262 /* The size of the string we might be able to reuse. */
8263 size = (STRINGP (f->desired_tool_bar_string)
8264 ? SCHARS (f->desired_tool_bar_string)
8265 : 0);
8266
8267 /* We need one space in the string for each image. */
8268 size_needed = f->n_tool_bar_items;
8269
8270 /* Reuse f->desired_tool_bar_string, if possible. */
8271 if (size < size_needed || NILP (f->desired_tool_bar_string))
8272 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8273 make_number (' '));
8274 else
8275 {
8276 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8277 Fremove_text_properties (make_number (0), make_number (size),
8278 props, f->desired_tool_bar_string);
8279 }
8280
8281 /* Put a `display' property on the string for the images to display,
8282 put a `menu_item' property on tool-bar items with a value that
8283 is the index of the item in F's tool-bar item vector. */
8284 for (i = 0; i < f->n_tool_bar_items; ++i)
8285 {
8286 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8287
8288 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8289 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8290 int hmargin, vmargin, relief, idx, end;
8291 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8292
8293 /* If image is a vector, choose the image according to the
8294 button state. */
8295 image = PROP (TOOL_BAR_ITEM_IMAGES);
8296 if (VECTORP (image))
8297 {
8298 if (enabled_p)
8299 idx = (selected_p
8300 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8301 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8302 else
8303 idx = (selected_p
8304 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8305 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8306
8307 xassert (ASIZE (image) >= idx);
8308 image = AREF (image, idx);
8309 }
8310 else
8311 idx = -1;
8312
8313 /* Ignore invalid image specifications. */
8314 if (!valid_image_p (image))
8315 continue;
8316
8317 /* Display the tool-bar button pressed, or depressed. */
8318 plist = Fcopy_sequence (XCDR (image));
8319
8320 /* Compute margin and relief to draw. */
8321 relief = (tool_bar_button_relief >= 0
8322 ? tool_bar_button_relief
8323 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8324 hmargin = vmargin = relief;
8325
8326 if (INTEGERP (Vtool_bar_button_margin)
8327 && XINT (Vtool_bar_button_margin) > 0)
8328 {
8329 hmargin += XFASTINT (Vtool_bar_button_margin);
8330 vmargin += XFASTINT (Vtool_bar_button_margin);
8331 }
8332 else if (CONSP (Vtool_bar_button_margin))
8333 {
8334 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8335 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8336 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8337
8338 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8339 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8340 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8341 }
8342
8343 if (auto_raise_tool_bar_buttons_p)
8344 {
8345 /* Add a `:relief' property to the image spec if the item is
8346 selected. */
8347 if (selected_p)
8348 {
8349 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8350 hmargin -= relief;
8351 vmargin -= relief;
8352 }
8353 }
8354 else
8355 {
8356 /* If image is selected, display it pressed, i.e. with a
8357 negative relief. If it's not selected, display it with a
8358 raised relief. */
8359 plist = Fplist_put (plist, QCrelief,
8360 (selected_p
8361 ? make_number (-relief)
8362 : make_number (relief)));
8363 hmargin -= relief;
8364 vmargin -= relief;
8365 }
8366
8367 /* Put a margin around the image. */
8368 if (hmargin || vmargin)
8369 {
8370 if (hmargin == vmargin)
8371 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8372 else
8373 plist = Fplist_put (plist, QCmargin,
8374 Fcons (make_number (hmargin),
8375 make_number (vmargin)));
8376 }
8377
8378 /* If button is not enabled, and we don't have special images
8379 for the disabled state, make the image appear disabled by
8380 applying an appropriate algorithm to it. */
8381 if (!enabled_p && idx < 0)
8382 plist = Fplist_put (plist, QCconversion, Qdisabled);
8383
8384 /* Put a `display' text property on the string for the image to
8385 display. Put a `menu-item' property on the string that gives
8386 the start of this item's properties in the tool-bar items
8387 vector. */
8388 image = Fcons (Qimage, plist);
8389 props = list4 (Qdisplay, image,
8390 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8391
8392 /* Let the last image hide all remaining spaces in the tool bar
8393 string. The string can be longer than needed when we reuse a
8394 previous string. */
8395 if (i + 1 == f->n_tool_bar_items)
8396 end = SCHARS (f->desired_tool_bar_string);
8397 else
8398 end = i + 1;
8399 Fadd_text_properties (make_number (i), make_number (end),
8400 props, f->desired_tool_bar_string);
8401 #undef PROP
8402 }
8403
8404 UNGCPRO;
8405 }
8406
8407
8408 /* Display one line of the tool-bar of frame IT->f. */
8409
8410 static void
8411 display_tool_bar_line (it)
8412 struct it *it;
8413 {
8414 struct glyph_row *row = it->glyph_row;
8415 int max_x = it->last_visible_x;
8416 struct glyph *last;
8417
8418 prepare_desired_row (row);
8419 row->y = it->current_y;
8420
8421 /* Note that this isn't made use of if the face hasn't a box,
8422 so there's no need to check the face here. */
8423 it->start_of_box_run_p = 1;
8424
8425 while (it->current_x < max_x)
8426 {
8427 int x_before, x, n_glyphs_before, i, nglyphs;
8428
8429 /* Get the next display element. */
8430 if (!get_next_display_element (it))
8431 break;
8432
8433 /* Produce glyphs. */
8434 x_before = it->current_x;
8435 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8436 PRODUCE_GLYPHS (it);
8437
8438 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8439 i = 0;
8440 x = x_before;
8441 while (i < nglyphs)
8442 {
8443 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8444
8445 if (x + glyph->pixel_width > max_x)
8446 {
8447 /* Glyph doesn't fit on line. */
8448 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8449 it->current_x = x;
8450 goto out;
8451 }
8452
8453 ++it->hpos;
8454 x += glyph->pixel_width;
8455 ++i;
8456 }
8457
8458 /* Stop at line ends. */
8459 if (ITERATOR_AT_END_OF_LINE_P (it))
8460 break;
8461
8462 set_iterator_to_next (it, 1);
8463 }
8464
8465 out:;
8466
8467 row->displays_text_p = row->used[TEXT_AREA] != 0;
8468 extend_face_to_end_of_line (it);
8469 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8470 last->right_box_line_p = 1;
8471 if (last == row->glyphs[TEXT_AREA])
8472 last->left_box_line_p = 1;
8473 compute_line_metrics (it);
8474
8475 /* If line is empty, make it occupy the rest of the tool-bar. */
8476 if (!row->displays_text_p)
8477 {
8478 row->height = row->phys_height = it->last_visible_y - row->y;
8479 row->ascent = row->phys_ascent = 0;
8480 }
8481
8482 row->full_width_p = 1;
8483 row->continued_p = 0;
8484 row->truncated_on_left_p = 0;
8485 row->truncated_on_right_p = 0;
8486
8487 it->current_x = it->hpos = 0;
8488 it->current_y += row->height;
8489 ++it->vpos;
8490 ++it->glyph_row;
8491 }
8492
8493
8494 /* Value is the number of screen lines needed to make all tool-bar
8495 items of frame F visible. */
8496
8497 static int
8498 tool_bar_lines_needed (f)
8499 struct frame *f;
8500 {
8501 struct window *w = XWINDOW (f->tool_bar_window);
8502 struct it it;
8503
8504 /* Initialize an iterator for iteration over
8505 F->desired_tool_bar_string in the tool-bar window of frame F. */
8506 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8507 it.first_visible_x = 0;
8508 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8509 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8510
8511 while (!ITERATOR_AT_END_P (&it))
8512 {
8513 it.glyph_row = w->desired_matrix->rows;
8514 clear_glyph_row (it.glyph_row);
8515 display_tool_bar_line (&it);
8516 }
8517
8518 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8519 }
8520
8521
8522 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8523 0, 1, 0,
8524 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8525 (frame)
8526 Lisp_Object frame;
8527 {
8528 struct frame *f;
8529 struct window *w;
8530 int nlines = 0;
8531
8532 if (NILP (frame))
8533 frame = selected_frame;
8534 else
8535 CHECK_FRAME (frame);
8536 f = XFRAME (frame);
8537
8538 if (WINDOWP (f->tool_bar_window)
8539 || (w = XWINDOW (f->tool_bar_window),
8540 WINDOW_TOTAL_LINES (w) > 0))
8541 {
8542 update_tool_bar (f, 1);
8543 if (f->n_tool_bar_items)
8544 {
8545 build_desired_tool_bar_string (f);
8546 nlines = tool_bar_lines_needed (f);
8547 }
8548 }
8549
8550 return make_number (nlines);
8551 }
8552
8553
8554 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8555 height should be changed. */
8556
8557 static int
8558 redisplay_tool_bar (f)
8559 struct frame *f;
8560 {
8561 struct window *w;
8562 struct it it;
8563 struct glyph_row *row;
8564 int change_height_p = 0;
8565
8566 #ifdef USE_GTK
8567 if (FRAME_EXTERNAL_TOOL_BAR (f))
8568 update_frame_tool_bar (f);
8569 return 0;
8570 #endif
8571
8572 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8573 do anything. This means you must start with tool-bar-lines
8574 non-zero to get the auto-sizing effect. Or in other words, you
8575 can turn off tool-bars by specifying tool-bar-lines zero. */
8576 if (!WINDOWP (f->tool_bar_window)
8577 || (w = XWINDOW (f->tool_bar_window),
8578 WINDOW_TOTAL_LINES (w) == 0))
8579 return 0;
8580
8581 /* Set up an iterator for the tool-bar window. */
8582 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8583 it.first_visible_x = 0;
8584 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8585 row = it.glyph_row;
8586
8587 /* Build a string that represents the contents of the tool-bar. */
8588 build_desired_tool_bar_string (f);
8589 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8590
8591 /* Display as many lines as needed to display all tool-bar items. */
8592 while (it.current_y < it.last_visible_y)
8593 display_tool_bar_line (&it);
8594
8595 /* It doesn't make much sense to try scrolling in the tool-bar
8596 window, so don't do it. */
8597 w->desired_matrix->no_scrolling_p = 1;
8598 w->must_be_updated_p = 1;
8599
8600 if (auto_resize_tool_bars_p)
8601 {
8602 int nlines;
8603
8604 /* If we couldn't display everything, change the tool-bar's
8605 height. */
8606 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8607 change_height_p = 1;
8608
8609 /* If there are blank lines at the end, except for a partially
8610 visible blank line at the end that is smaller than
8611 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8612 row = it.glyph_row - 1;
8613 if (!row->displays_text_p
8614 && row->height >= FRAME_LINE_HEIGHT (f))
8615 change_height_p = 1;
8616
8617 /* If row displays tool-bar items, but is partially visible,
8618 change the tool-bar's height. */
8619 if (row->displays_text_p
8620 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8621 change_height_p = 1;
8622
8623 /* Resize windows as needed by changing the `tool-bar-lines'
8624 frame parameter. */
8625 if (change_height_p
8626 && (nlines = tool_bar_lines_needed (f),
8627 nlines != WINDOW_TOTAL_LINES (w)))
8628 {
8629 extern Lisp_Object Qtool_bar_lines;
8630 Lisp_Object frame;
8631 int old_height = WINDOW_TOTAL_LINES (w);
8632
8633 XSETFRAME (frame, f);
8634 clear_glyph_matrix (w->desired_matrix);
8635 Fmodify_frame_parameters (frame,
8636 Fcons (Fcons (Qtool_bar_lines,
8637 make_number (nlines)),
8638 Qnil));
8639 if (WINDOW_TOTAL_LINES (w) != old_height)
8640 fonts_changed_p = 1;
8641 }
8642 }
8643
8644 return change_height_p;
8645 }
8646
8647
8648 /* Get information about the tool-bar item which is displayed in GLYPH
8649 on frame F. Return in *PROP_IDX the index where tool-bar item
8650 properties start in F->tool_bar_items. Value is zero if
8651 GLYPH doesn't display a tool-bar item. */
8652
8653 static int
8654 tool_bar_item_info (f, glyph, prop_idx)
8655 struct frame *f;
8656 struct glyph *glyph;
8657 int *prop_idx;
8658 {
8659 Lisp_Object prop;
8660 int success_p;
8661 int charpos;
8662
8663 /* This function can be called asynchronously, which means we must
8664 exclude any possibility that Fget_text_property signals an
8665 error. */
8666 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8667 charpos = max (0, charpos);
8668
8669 /* Get the text property `menu-item' at pos. The value of that
8670 property is the start index of this item's properties in
8671 F->tool_bar_items. */
8672 prop = Fget_text_property (make_number (charpos),
8673 Qmenu_item, f->current_tool_bar_string);
8674 if (INTEGERP (prop))
8675 {
8676 *prop_idx = XINT (prop);
8677 success_p = 1;
8678 }
8679 else
8680 success_p = 0;
8681
8682 return success_p;
8683 }
8684
8685 \f
8686 /* Get information about the tool-bar item at position X/Y on frame F.
8687 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8688 the current matrix of the tool-bar window of F, or NULL if not
8689 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8690 item in F->tool_bar_items. Value is
8691
8692 -1 if X/Y is not on a tool-bar item
8693 0 if X/Y is on the same item that was highlighted before.
8694 1 otherwise. */
8695
8696 static int
8697 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8698 struct frame *f;
8699 int x, y;
8700 struct glyph **glyph;
8701 int *hpos, *vpos, *prop_idx;
8702 {
8703 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8704 struct window *w = XWINDOW (f->tool_bar_window);
8705 int area;
8706
8707 /* Find the glyph under X/Y. */
8708 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
8709 if (*glyph == NULL)
8710 return -1;
8711
8712 /* Get the start of this tool-bar item's properties in
8713 f->tool_bar_items. */
8714 if (!tool_bar_item_info (f, *glyph, prop_idx))
8715 return -1;
8716
8717 /* Is mouse on the highlighted item? */
8718 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8719 && *vpos >= dpyinfo->mouse_face_beg_row
8720 && *vpos <= dpyinfo->mouse_face_end_row
8721 && (*vpos > dpyinfo->mouse_face_beg_row
8722 || *hpos >= dpyinfo->mouse_face_beg_col)
8723 && (*vpos < dpyinfo->mouse_face_end_row
8724 || *hpos < dpyinfo->mouse_face_end_col
8725 || dpyinfo->mouse_face_past_end))
8726 return 0;
8727
8728 return 1;
8729 }
8730
8731
8732 /* EXPORT:
8733 Handle mouse button event on the tool-bar of frame F, at
8734 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8735 0 for button release. MODIFIERS is event modifiers for button
8736 release. */
8737
8738 void
8739 handle_tool_bar_click (f, x, y, down_p, modifiers)
8740 struct frame *f;
8741 int x, y, down_p;
8742 unsigned int modifiers;
8743 {
8744 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8745 struct window *w = XWINDOW (f->tool_bar_window);
8746 int hpos, vpos, prop_idx;
8747 struct glyph *glyph;
8748 Lisp_Object enabled_p;
8749
8750 /* If not on the highlighted tool-bar item, return. */
8751 frame_to_window_pixel_xy (w, &x, &y);
8752 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8753 return;
8754
8755 /* If item is disabled, do nothing. */
8756 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8757 if (NILP (enabled_p))
8758 return;
8759
8760 if (down_p)
8761 {
8762 /* Show item in pressed state. */
8763 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8764 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8765 last_tool_bar_item = prop_idx;
8766 }
8767 else
8768 {
8769 Lisp_Object key, frame;
8770 struct input_event event;
8771 EVENT_INIT (event);
8772
8773 /* Show item in released state. */
8774 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8775 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8776
8777 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8778
8779 XSETFRAME (frame, f);
8780 event.kind = TOOL_BAR_EVENT;
8781 event.frame_or_window = frame;
8782 event.arg = frame;
8783 kbd_buffer_store_event (&event);
8784
8785 event.kind = TOOL_BAR_EVENT;
8786 event.frame_or_window = frame;
8787 event.arg = key;
8788 event.modifiers = modifiers;
8789 kbd_buffer_store_event (&event);
8790 last_tool_bar_item = -1;
8791 }
8792 }
8793
8794
8795 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8796 tool-bar window-relative coordinates X/Y. Called from
8797 note_mouse_highlight. */
8798
8799 static void
8800 note_tool_bar_highlight (f, x, y)
8801 struct frame *f;
8802 int x, y;
8803 {
8804 Lisp_Object window = f->tool_bar_window;
8805 struct window *w = XWINDOW (window);
8806 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8807 int hpos, vpos;
8808 struct glyph *glyph;
8809 struct glyph_row *row;
8810 int i;
8811 Lisp_Object enabled_p;
8812 int prop_idx;
8813 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8814 int mouse_down_p, rc;
8815
8816 /* Function note_mouse_highlight is called with negative x(y
8817 values when mouse moves outside of the frame. */
8818 if (x <= 0 || y <= 0)
8819 {
8820 clear_mouse_face (dpyinfo);
8821 return;
8822 }
8823
8824 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8825 if (rc < 0)
8826 {
8827 /* Not on tool-bar item. */
8828 clear_mouse_face (dpyinfo);
8829 return;
8830 }
8831 else if (rc == 0)
8832 /* On same tool-bar item as before. */
8833 goto set_help_echo;
8834
8835 clear_mouse_face (dpyinfo);
8836
8837 /* Mouse is down, but on different tool-bar item? */
8838 mouse_down_p = (dpyinfo->grabbed
8839 && f == last_mouse_frame
8840 && FRAME_LIVE_P (f));
8841 if (mouse_down_p
8842 && last_tool_bar_item != prop_idx)
8843 return;
8844
8845 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8846 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8847
8848 /* If tool-bar item is not enabled, don't highlight it. */
8849 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8850 if (!NILP (enabled_p))
8851 {
8852 /* Compute the x-position of the glyph. In front and past the
8853 image is a space. We include this in the highlighted area. */
8854 row = MATRIX_ROW (w->current_matrix, vpos);
8855 for (i = x = 0; i < hpos; ++i)
8856 x += row->glyphs[TEXT_AREA][i].pixel_width;
8857
8858 /* Record this as the current active region. */
8859 dpyinfo->mouse_face_beg_col = hpos;
8860 dpyinfo->mouse_face_beg_row = vpos;
8861 dpyinfo->mouse_face_beg_x = x;
8862 dpyinfo->mouse_face_beg_y = row->y;
8863 dpyinfo->mouse_face_past_end = 0;
8864
8865 dpyinfo->mouse_face_end_col = hpos + 1;
8866 dpyinfo->mouse_face_end_row = vpos;
8867 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8868 dpyinfo->mouse_face_end_y = row->y;
8869 dpyinfo->mouse_face_window = window;
8870 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8871
8872 /* Display it as active. */
8873 show_mouse_face (dpyinfo, draw);
8874 dpyinfo->mouse_face_image_state = draw;
8875 }
8876
8877 set_help_echo:
8878
8879 /* Set help_echo_string to a help string to display for this tool-bar item.
8880 XTread_socket does the rest. */
8881 help_echo_object = help_echo_window = Qnil;
8882 help_echo_pos = -1;
8883 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8884 if (NILP (help_echo_string))
8885 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8886 }
8887
8888 #endif /* HAVE_WINDOW_SYSTEM */
8889
8890
8891 \f
8892 /***********************************************************************
8893 Fringes
8894 ***********************************************************************/
8895
8896 #ifdef HAVE_WINDOW_SYSTEM
8897
8898 /* An arrow like this: `<-'. */
8899 static unsigned char left_bits[] = {
8900 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8901
8902 /* Right truncation arrow bitmap `->'. */
8903 static unsigned char right_bits[] = {
8904 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8905
8906 /* Marker for continued lines. */
8907 static unsigned char continued_bits[] = {
8908 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8909
8910 /* Marker for continuation lines. */
8911 static unsigned char continuation_bits[] = {
8912 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8913
8914 /* Overlay arrow bitmap. A triangular arrow. */
8915 static unsigned char ov_bits[] = {
8916 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8917
8918 /* Bitmap drawn to indicate lines not displaying text if
8919 `indicate-empty-lines' is non-nil. */
8920 static unsigned char zv_bits[] = {
8921 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8922 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8923 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8924 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8925 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8926 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8927 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8928 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8929
8930 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8931 {
8932 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8933 { 8, sizeof (left_bits), 0, left_bits },
8934 { 8, sizeof (right_bits), 0, right_bits },
8935 { 8, sizeof (continued_bits), 0, continued_bits },
8936 { 8, sizeof (continuation_bits), 0, continuation_bits },
8937 { 8, sizeof (ov_bits), 0, ov_bits },
8938 { 8, sizeof (zv_bits), 3, zv_bits }
8939 };
8940
8941
8942 /* Draw the bitmap WHICH in one of the left or right fringes of
8943 window W. ROW is the glyph row for which to display the bitmap; it
8944 determines the vertical position at which the bitmap has to be
8945 drawn. */
8946
8947 static void
8948 draw_fringe_bitmap (w, row, which, left_p)
8949 struct window *w;
8950 struct glyph_row *row;
8951 enum fringe_bitmap_type which;
8952 int left_p;
8953 {
8954 struct frame *f = XFRAME (WINDOW_FRAME (w));
8955 struct draw_fringe_bitmap_params p;
8956
8957 /* Convert row to frame coordinates. */
8958 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8959
8960 p.which = which;
8961 p.wd = fringe_bitmaps[which].width;
8962
8963 p.h = fringe_bitmaps[which].height;
8964 p.dh = (fringe_bitmaps[which].period
8965 ? (p.y % fringe_bitmaps[which].period)
8966 : 0);
8967 p.h -= p.dh;
8968 /* Clip bitmap if too high. */
8969 if (p.h > row->height)
8970 p.h = row->height;
8971
8972 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8973 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8974
8975 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8976 the fringe. */
8977 p.bx = -1;
8978 if (left_p)
8979 {
8980 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
8981 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8982 ? LEFT_MARGIN_AREA
8983 : TEXT_AREA));
8984 if (p.wd > wd)
8985 p.wd = wd;
8986 p.x = x - p.wd - (wd - p.wd) / 2;
8987
8988 if (p.wd < wd || row->height > p.h)
8989 {
8990 /* If W has a vertical border to its left, don't draw over it. */
8991 wd -= ((!WINDOW_LEFTMOST_P (w)
8992 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
8993 ? 1 : 0);
8994 p.bx = x - wd;
8995 p.nx = wd;
8996 }
8997 }
8998 else
8999 {
9000 int x = window_box_right (w,
9001 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
9002 ? RIGHT_MARGIN_AREA
9003 : TEXT_AREA));
9004 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
9005 if (p.wd > wd)
9006 p.wd = wd;
9007 p.x = x + (wd - p.wd) / 2;
9008 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
9009 the fringe. */
9010 if (p.wd < wd || row->height > p.h)
9011 {
9012 p.bx = x;
9013 p.nx = wd;
9014 }
9015 }
9016
9017 if (p.bx >= 0)
9018 {
9019 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
9020
9021 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
9022 p.ny = row->visible_height;
9023 }
9024
9025 /* Adjust y to the offset in the row to start drawing the bitmap. */
9026 p.y += (row->height - p.h) / 2;
9027
9028 FRAME_RIF (f)->draw_fringe_bitmap (w, row, &p);
9029 }
9030
9031 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
9032 function with input blocked. */
9033
9034 void
9035 draw_row_fringe_bitmaps (w, row)
9036 struct window *w;
9037 struct glyph_row *row;
9038 {
9039 enum fringe_bitmap_type bitmap;
9040
9041 xassert (interrupt_input_blocked);
9042
9043 /* If row is completely invisible, because of vscrolling, we
9044 don't have to draw anything. */
9045 if (row->visible_height <= 0)
9046 return;
9047
9048 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
9049 {
9050 /* Decide which bitmap to draw in the left fringe. */
9051 if (row->overlay_arrow_p)
9052 bitmap = OVERLAY_ARROW_BITMAP;
9053 else if (row->truncated_on_left_p)
9054 bitmap = LEFT_TRUNCATION_BITMAP;
9055 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
9056 bitmap = CONTINUATION_LINE_BITMAP;
9057 else if (row->indicate_empty_line_p)
9058 bitmap = ZV_LINE_BITMAP;
9059 else
9060 bitmap = NO_FRINGE_BITMAP;
9061
9062 draw_fringe_bitmap (w, row, bitmap, 1);
9063 }
9064
9065 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
9066 {
9067 /* Decide which bitmap to draw in the right fringe. */
9068 if (row->truncated_on_right_p)
9069 bitmap = RIGHT_TRUNCATION_BITMAP;
9070 else if (row->continued_p)
9071 bitmap = CONTINUED_LINE_BITMAP;
9072 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
9073 bitmap = ZV_LINE_BITMAP;
9074 else
9075 bitmap = NO_FRINGE_BITMAP;
9076
9077 draw_fringe_bitmap (w, row, bitmap, 0);
9078 }
9079 }
9080
9081
9082 /* Compute actual fringe widths for frame F.
9083
9084 If REDRAW is 1, redraw F if the fringe settings was actually
9085 modified and F is visible.
9086
9087 Since the combined left and right fringe must occupy an integral
9088 number of columns, we may need to add some pixels to each fringe.
9089 Typically, we add an equal amount (+/- 1 pixel) to each fringe,
9090 but a negative width value is taken literally (after negating it).
9091
9092 We never make the fringes narrower than specified. It is planned
9093 to make fringe bitmaps customizable and expandable, and at that
9094 time, the user will typically specify the minimum number of pixels
9095 needed for his bitmaps, so we shouldn't select anything less than
9096 what is specified.
9097 */
9098
9099 void
9100 compute_fringe_widths (f, redraw)
9101 struct frame *f;
9102 int redraw;
9103 {
9104 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9105 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9106 int o_cols = FRAME_FRINGE_COLS (f);
9107
9108 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9109 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9110 int left_fringe_width, right_fringe_width;
9111
9112 if (!NILP (left_fringe))
9113 left_fringe = Fcdr (left_fringe);
9114 if (!NILP (right_fringe))
9115 right_fringe = Fcdr (right_fringe);
9116
9117 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9118 XINT (left_fringe));
9119 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9120 XINT (right_fringe));
9121
9122 if (left_fringe_width || right_fringe_width)
9123 {
9124 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9125 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9126 int conf_wid = left_wid + right_wid;
9127 int font_wid = FRAME_COLUMN_WIDTH (f);
9128 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9129 int real_wid = cols * font_wid;
9130 if (left_wid && right_wid)
9131 {
9132 if (left_fringe_width < 0)
9133 {
9134 /* Left fringe width is fixed, adjust right fringe if necessary */
9135 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9136 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9137 }
9138 else if (right_fringe_width < 0)
9139 {
9140 /* Right fringe width is fixed, adjust left fringe if necessary */
9141 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9142 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
9143 }
9144 else
9145 {
9146 /* Adjust both fringes with an equal amount.
9147 Note that we are doing integer arithmetic here, so don't
9148 lose a pixel if the total width is an odd number. */
9149 int fill = real_wid - conf_wid;
9150 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9151 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9152 }
9153 }
9154 else if (left_fringe_width)
9155 {
9156 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9157 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9158 }
9159 else
9160 {
9161 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9162 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
9163 }
9164 FRAME_FRINGE_COLS (f) = cols;
9165 }
9166 else
9167 {
9168 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9169 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9170 FRAME_FRINGE_COLS (f) = 0;
9171 }
9172
9173 if (redraw && FRAME_VISIBLE_P (f))
9174 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9175 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9176 o_cols != FRAME_FRINGE_COLS (f))
9177 redraw_frame (f);
9178 }
9179
9180 #endif /* HAVE_WINDOW_SYSTEM */
9181
9182
9183 \f
9184 /************************************************************************
9185 Horizontal scrolling
9186 ************************************************************************/
9187
9188 static int hscroll_window_tree P_ ((Lisp_Object));
9189 static int hscroll_windows P_ ((Lisp_Object));
9190
9191 /* For all leaf windows in the window tree rooted at WINDOW, set their
9192 hscroll value so that PT is (i) visible in the window, and (ii) so
9193 that it is not within a certain margin at the window's left and
9194 right border. Value is non-zero if any window's hscroll has been
9195 changed. */
9196
9197 static int
9198 hscroll_window_tree (window)
9199 Lisp_Object window;
9200 {
9201 int hscrolled_p = 0;
9202 int hscroll_relative_p = FLOATP (Vhscroll_step);
9203 int hscroll_step_abs = 0;
9204 double hscroll_step_rel = 0;
9205
9206 if (hscroll_relative_p)
9207 {
9208 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9209 if (hscroll_step_rel < 0)
9210 {
9211 hscroll_relative_p = 0;
9212 hscroll_step_abs = 0;
9213 }
9214 }
9215 else if (INTEGERP (Vhscroll_step))
9216 {
9217 hscroll_step_abs = XINT (Vhscroll_step);
9218 if (hscroll_step_abs < 0)
9219 hscroll_step_abs = 0;
9220 }
9221 else
9222 hscroll_step_abs = 0;
9223
9224 while (WINDOWP (window))
9225 {
9226 struct window *w = XWINDOW (window);
9227
9228 if (WINDOWP (w->hchild))
9229 hscrolled_p |= hscroll_window_tree (w->hchild);
9230 else if (WINDOWP (w->vchild))
9231 hscrolled_p |= hscroll_window_tree (w->vchild);
9232 else if (w->cursor.vpos >= 0)
9233 {
9234 int h_margin;
9235 int text_area_width;
9236 struct glyph_row *current_cursor_row
9237 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9238 struct glyph_row *desired_cursor_row
9239 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9240 struct glyph_row *cursor_row
9241 = (desired_cursor_row->enabled_p
9242 ? desired_cursor_row
9243 : current_cursor_row);
9244
9245 text_area_width = window_box_width (w, TEXT_AREA);
9246
9247 /* Scroll when cursor is inside this scroll margin. */
9248 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9249
9250 if ((XFASTINT (w->hscroll)
9251 && w->cursor.x <= h_margin)
9252 || (cursor_row->enabled_p
9253 && cursor_row->truncated_on_right_p
9254 && (w->cursor.x >= text_area_width - h_margin)))
9255 {
9256 struct it it;
9257 int hscroll;
9258 struct buffer *saved_current_buffer;
9259 int pt;
9260 int wanted_x;
9261
9262 /* Find point in a display of infinite width. */
9263 saved_current_buffer = current_buffer;
9264 current_buffer = XBUFFER (w->buffer);
9265
9266 if (w == XWINDOW (selected_window))
9267 pt = BUF_PT (current_buffer);
9268 else
9269 {
9270 pt = marker_position (w->pointm);
9271 pt = max (BEGV, pt);
9272 pt = min (ZV, pt);
9273 }
9274
9275 /* Move iterator to pt starting at cursor_row->start in
9276 a line with infinite width. */
9277 init_to_row_start (&it, w, cursor_row);
9278 it.last_visible_x = INFINITY;
9279 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9280 current_buffer = saved_current_buffer;
9281
9282 /* Position cursor in window. */
9283 if (!hscroll_relative_p && hscroll_step_abs == 0)
9284 hscroll = max (0, it.current_x - text_area_width / 2)
9285 / FRAME_COLUMN_WIDTH (it.f);
9286 else if (w->cursor.x >= text_area_width - h_margin)
9287 {
9288 if (hscroll_relative_p)
9289 wanted_x = text_area_width * (1 - hscroll_step_rel)
9290 - h_margin;
9291 else
9292 wanted_x = text_area_width
9293 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9294 - h_margin;
9295 hscroll
9296 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9297 }
9298 else
9299 {
9300 if (hscroll_relative_p)
9301 wanted_x = text_area_width * hscroll_step_rel
9302 + h_margin;
9303 else
9304 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9305 + h_margin;
9306 hscroll
9307 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9308 }
9309 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9310
9311 /* Don't call Fset_window_hscroll if value hasn't
9312 changed because it will prevent redisplay
9313 optimizations. */
9314 if (XFASTINT (w->hscroll) != hscroll)
9315 {
9316 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9317 w->hscroll = make_number (hscroll);
9318 hscrolled_p = 1;
9319 }
9320 }
9321 }
9322
9323 window = w->next;
9324 }
9325
9326 /* Value is non-zero if hscroll of any leaf window has been changed. */
9327 return hscrolled_p;
9328 }
9329
9330
9331 /* Set hscroll so that cursor is visible and not inside horizontal
9332 scroll margins for all windows in the tree rooted at WINDOW. See
9333 also hscroll_window_tree above. Value is non-zero if any window's
9334 hscroll has been changed. If it has, desired matrices on the frame
9335 of WINDOW are cleared. */
9336
9337 static int
9338 hscroll_windows (window)
9339 Lisp_Object window;
9340 {
9341 int hscrolled_p;
9342
9343 if (automatic_hscrolling_p)
9344 {
9345 hscrolled_p = hscroll_window_tree (window);
9346 if (hscrolled_p)
9347 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9348 }
9349 else
9350 hscrolled_p = 0;
9351 return hscrolled_p;
9352 }
9353
9354
9355 \f
9356 /************************************************************************
9357 Redisplay
9358 ************************************************************************/
9359
9360 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9361 to a non-zero value. This is sometimes handy to have in a debugger
9362 session. */
9363
9364 #if GLYPH_DEBUG
9365
9366 /* First and last unchanged row for try_window_id. */
9367
9368 int debug_first_unchanged_at_end_vpos;
9369 int debug_last_unchanged_at_beg_vpos;
9370
9371 /* Delta vpos and y. */
9372
9373 int debug_dvpos, debug_dy;
9374
9375 /* Delta in characters and bytes for try_window_id. */
9376
9377 int debug_delta, debug_delta_bytes;
9378
9379 /* Values of window_end_pos and window_end_vpos at the end of
9380 try_window_id. */
9381
9382 EMACS_INT debug_end_pos, debug_end_vpos;
9383
9384 /* Append a string to W->desired_matrix->method. FMT is a printf
9385 format string. A1...A9 are a supplement for a variable-length
9386 argument list. If trace_redisplay_p is non-zero also printf the
9387 resulting string to stderr. */
9388
9389 static void
9390 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9391 struct window *w;
9392 char *fmt;
9393 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9394 {
9395 char buffer[512];
9396 char *method = w->desired_matrix->method;
9397 int len = strlen (method);
9398 int size = sizeof w->desired_matrix->method;
9399 int remaining = size - len - 1;
9400
9401 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9402 if (len && remaining)
9403 {
9404 method[len] = '|';
9405 --remaining, ++len;
9406 }
9407
9408 strncpy (method + len, buffer, remaining);
9409
9410 if (trace_redisplay_p)
9411 fprintf (stderr, "%p (%s): %s\n",
9412 w,
9413 ((BUFFERP (w->buffer)
9414 && STRINGP (XBUFFER (w->buffer)->name))
9415 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9416 : "no buffer"),
9417 buffer);
9418 }
9419
9420 #endif /* GLYPH_DEBUG */
9421
9422
9423 /* Value is non-zero if all changes in window W, which displays
9424 current_buffer, are in the text between START and END. START is a
9425 buffer position, END is given as a distance from Z. Used in
9426 redisplay_internal for display optimization. */
9427
9428 static INLINE int
9429 text_outside_line_unchanged_p (w, start, end)
9430 struct window *w;
9431 int start, end;
9432 {
9433 int unchanged_p = 1;
9434
9435 /* If text or overlays have changed, see where. */
9436 if (XFASTINT (w->last_modified) < MODIFF
9437 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9438 {
9439 /* Gap in the line? */
9440 if (GPT < start || Z - GPT < end)
9441 unchanged_p = 0;
9442
9443 /* Changes start in front of the line, or end after it? */
9444 if (unchanged_p
9445 && (BEG_UNCHANGED < start - 1
9446 || END_UNCHANGED < end))
9447 unchanged_p = 0;
9448
9449 /* If selective display, can't optimize if changes start at the
9450 beginning of the line. */
9451 if (unchanged_p
9452 && INTEGERP (current_buffer->selective_display)
9453 && XINT (current_buffer->selective_display) > 0
9454 && (BEG_UNCHANGED < start || GPT <= start))
9455 unchanged_p = 0;
9456
9457 /* If there are overlays at the start or end of the line, these
9458 may have overlay strings with newlines in them. A change at
9459 START, for instance, may actually concern the display of such
9460 overlay strings as well, and they are displayed on different
9461 lines. So, quickly rule out this case. (For the future, it
9462 might be desirable to implement something more telling than
9463 just BEG/END_UNCHANGED.) */
9464 if (unchanged_p)
9465 {
9466 if (BEG + BEG_UNCHANGED == start
9467 && overlay_touches_p (start))
9468 unchanged_p = 0;
9469 if (END_UNCHANGED == end
9470 && overlay_touches_p (Z - end))
9471 unchanged_p = 0;
9472 }
9473 }
9474
9475 return unchanged_p;
9476 }
9477
9478
9479 /* Do a frame update, taking possible shortcuts into account. This is
9480 the main external entry point for redisplay.
9481
9482 If the last redisplay displayed an echo area message and that message
9483 is no longer requested, we clear the echo area or bring back the
9484 mini-buffer if that is in use. */
9485
9486 void
9487 redisplay ()
9488 {
9489 redisplay_internal (0);
9490 }
9491
9492
9493 /* Return 1 if point moved out of or into a composition. Otherwise
9494 return 0. PREV_BUF and PREV_PT are the last point buffer and
9495 position. BUF and PT are the current point buffer and position. */
9496
9497 int
9498 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9499 struct buffer *prev_buf, *buf;
9500 int prev_pt, pt;
9501 {
9502 int start, end;
9503 Lisp_Object prop;
9504 Lisp_Object buffer;
9505
9506 XSETBUFFER (buffer, buf);
9507 /* Check a composition at the last point if point moved within the
9508 same buffer. */
9509 if (prev_buf == buf)
9510 {
9511 if (prev_pt == pt)
9512 /* Point didn't move. */
9513 return 0;
9514
9515 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9516 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9517 && COMPOSITION_VALID_P (start, end, prop)
9518 && start < prev_pt && end > prev_pt)
9519 /* The last point was within the composition. Return 1 iff
9520 point moved out of the composition. */
9521 return (pt <= start || pt >= end);
9522 }
9523
9524 /* Check a composition at the current point. */
9525 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9526 && find_composition (pt, -1, &start, &end, &prop, buffer)
9527 && COMPOSITION_VALID_P (start, end, prop)
9528 && start < pt && end > pt);
9529 }
9530
9531
9532 /* Reconsider the setting of B->clip_changed which is displayed
9533 in window W. */
9534
9535 static INLINE void
9536 reconsider_clip_changes (w, b)
9537 struct window *w;
9538 struct buffer *b;
9539 {
9540 if (b->clip_changed
9541 && !NILP (w->window_end_valid)
9542 && w->current_matrix->buffer == b
9543 && w->current_matrix->zv == BUF_ZV (b)
9544 && w->current_matrix->begv == BUF_BEGV (b))
9545 b->clip_changed = 0;
9546
9547 /* If display wasn't paused, and W is not a tool bar window, see if
9548 point has been moved into or out of a composition. In that case,
9549 we set b->clip_changed to 1 to force updating the screen. If
9550 b->clip_changed has already been set to 1, we can skip this
9551 check. */
9552 if (!b->clip_changed
9553 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9554 {
9555 int pt;
9556
9557 if (w == XWINDOW (selected_window))
9558 pt = BUF_PT (current_buffer);
9559 else
9560 pt = marker_position (w->pointm);
9561
9562 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9563 || pt != XINT (w->last_point))
9564 && check_point_in_composition (w->current_matrix->buffer,
9565 XINT (w->last_point),
9566 XBUFFER (w->buffer), pt))
9567 b->clip_changed = 1;
9568 }
9569 }
9570 \f
9571
9572 /* Select FRAME to forward the values of frame-local variables into C
9573 variables so that the redisplay routines can access those values
9574 directly. */
9575
9576 static void
9577 select_frame_for_redisplay (frame)
9578 Lisp_Object frame;
9579 {
9580 Lisp_Object tail, sym, val;
9581 Lisp_Object old = selected_frame;
9582
9583 selected_frame = frame;
9584
9585 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9586 if (CONSP (XCAR (tail))
9587 && (sym = XCAR (XCAR (tail)),
9588 SYMBOLP (sym))
9589 && (sym = indirect_variable (sym),
9590 val = SYMBOL_VALUE (sym),
9591 (BUFFER_LOCAL_VALUEP (val)
9592 || SOME_BUFFER_LOCAL_VALUEP (val)))
9593 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9594 Fsymbol_value (sym);
9595
9596 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9597 if (CONSP (XCAR (tail))
9598 && (sym = XCAR (XCAR (tail)),
9599 SYMBOLP (sym))
9600 && (sym = indirect_variable (sym),
9601 val = SYMBOL_VALUE (sym),
9602 (BUFFER_LOCAL_VALUEP (val)
9603 || SOME_BUFFER_LOCAL_VALUEP (val)))
9604 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9605 Fsymbol_value (sym);
9606 }
9607
9608
9609 #define STOP_POLLING \
9610 do { if (! polling_stopped_here) stop_polling (); \
9611 polling_stopped_here = 1; } while (0)
9612
9613 #define RESUME_POLLING \
9614 do { if (polling_stopped_here) start_polling (); \
9615 polling_stopped_here = 0; } while (0)
9616
9617
9618 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9619 response to any user action; therefore, we should preserve the echo
9620 area. (Actually, our caller does that job.) Perhaps in the future
9621 avoid recentering windows if it is not necessary; currently that
9622 causes some problems. */
9623
9624 static void
9625 redisplay_internal (preserve_echo_area)
9626 int preserve_echo_area;
9627 {
9628 struct window *w = XWINDOW (selected_window);
9629 struct frame *f = XFRAME (w->frame);
9630 int pause;
9631 int must_finish = 0;
9632 struct text_pos tlbufpos, tlendpos;
9633 int number_of_visible_frames;
9634 int count;
9635 struct frame *sf = SELECTED_FRAME ();
9636 int polling_stopped_here = 0;
9637
9638 /* Non-zero means redisplay has to consider all windows on all
9639 frames. Zero means, only selected_window is considered. */
9640 int consider_all_windows_p;
9641
9642 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9643
9644 /* No redisplay if running in batch mode or frame is not yet fully
9645 initialized, or redisplay is explicitly turned off by setting
9646 Vinhibit_redisplay. */
9647 if (noninteractive
9648 || !NILP (Vinhibit_redisplay)
9649 || !f->glyphs_initialized_p)
9650 return;
9651
9652 /* The flag redisplay_performed_directly_p is set by
9653 direct_output_for_insert when it already did the whole screen
9654 update necessary. */
9655 if (redisplay_performed_directly_p)
9656 {
9657 redisplay_performed_directly_p = 0;
9658 if (!hscroll_windows (selected_window))
9659 return;
9660 }
9661
9662 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9663 if (popup_activated ())
9664 return;
9665 #endif
9666
9667 /* I don't think this happens but let's be paranoid. */
9668 if (redisplaying_p)
9669 return;
9670
9671 /* Record a function that resets redisplaying_p to its old value
9672 when we leave this function. */
9673 count = SPECPDL_INDEX ();
9674 record_unwind_protect (unwind_redisplay,
9675 Fcons (make_number (redisplaying_p), selected_frame));
9676 ++redisplaying_p;
9677 specbind (Qinhibit_free_realized_faces, Qnil);
9678
9679 retry:
9680 pause = 0;
9681 reconsider_clip_changes (w, current_buffer);
9682
9683 /* If new fonts have been loaded that make a glyph matrix adjustment
9684 necessary, do it. */
9685 if (fonts_changed_p)
9686 {
9687 adjust_glyphs (NULL);
9688 ++windows_or_buffers_changed;
9689 fonts_changed_p = 0;
9690 }
9691
9692 /* If face_change_count is non-zero, init_iterator will free all
9693 realized faces, which includes the faces referenced from current
9694 matrices. So, we can't reuse current matrices in this case. */
9695 if (face_change_count)
9696 ++windows_or_buffers_changed;
9697
9698 if (FRAME_TERMCAP_P (sf)
9699 && FRAME_TTY (sf)->previous_terminal_frame != sf)
9700 {
9701 /* Since frames on a single ASCII terminal share the same
9702 display area, displaying a different frame means redisplay
9703 the whole thing. */
9704 windows_or_buffers_changed++;
9705 SET_FRAME_GARBAGED (sf);
9706 FRAME_TTY (sf)->previous_terminal_frame = sf;
9707 }
9708
9709 /* Set the visible flags for all frames. Do this before checking
9710 for resized or garbaged frames; they want to know if their frames
9711 are visible. See the comment in frame.h for
9712 FRAME_SAMPLE_VISIBILITY. */
9713 {
9714 Lisp_Object tail, frame;
9715
9716 number_of_visible_frames = 0;
9717
9718 FOR_EACH_FRAME (tail, frame)
9719 {
9720 struct frame *f = XFRAME (frame);
9721
9722 FRAME_SAMPLE_VISIBILITY (f);
9723 if (FRAME_VISIBLE_P (f))
9724 ++number_of_visible_frames;
9725 clear_desired_matrices (f);
9726 }
9727 }
9728
9729
9730 /* Notice any pending interrupt request to change frame size. */
9731 do_pending_window_change (1);
9732
9733 /* Clear frames marked as garbaged. */
9734 if (frame_garbaged)
9735 clear_garbaged_frames ();
9736
9737 /* Build menubar and tool-bar items. */
9738 prepare_menu_bars ();
9739
9740 if (windows_or_buffers_changed)
9741 update_mode_lines++;
9742
9743 /* Detect case that we need to write or remove a star in the mode line. */
9744 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9745 {
9746 w->update_mode_line = Qt;
9747 if (buffer_shared > 1)
9748 update_mode_lines++;
9749 }
9750
9751 /* If %c is in the mode line, update it if needed. */
9752 if (!NILP (w->column_number_displayed)
9753 /* This alternative quickly identifies a common case
9754 where no change is needed. */
9755 && !(PT == XFASTINT (w->last_point)
9756 && XFASTINT (w->last_modified) >= MODIFF
9757 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9758 && (XFASTINT (w->column_number_displayed)
9759 != (int) current_column ())) /* iftc */
9760 w->update_mode_line = Qt;
9761
9762 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9763
9764 /* The variable buffer_shared is set in redisplay_window and
9765 indicates that we redisplay a buffer in different windows. See
9766 there. */
9767 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9768 || cursor_type_changed);
9769
9770 /* If specs for an arrow have changed, do thorough redisplay
9771 to ensure we remove any arrow that should no longer exist. */
9772 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9773 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9774 consider_all_windows_p = windows_or_buffers_changed = 1;
9775
9776 /* Normally the message* functions will have already displayed and
9777 updated the echo area, but the frame may have been trashed, or
9778 the update may have been preempted, so display the echo area
9779 again here. Checking message_cleared_p captures the case that
9780 the echo area should be cleared. */
9781 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9782 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9783 || (message_cleared_p
9784 && minibuf_level == 0
9785 /* If the mini-window is currently selected, this means the
9786 echo-area doesn't show through. */
9787 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9788 {
9789 int window_height_changed_p = echo_area_display (0);
9790 must_finish = 1;
9791
9792 /* If we don't display the current message, don't clear the
9793 message_cleared_p flag, because, if we did, we wouldn't clear
9794 the echo area in the next redisplay which doesn't preserve
9795 the echo area. */
9796 if (!display_last_displayed_message_p)
9797 message_cleared_p = 0;
9798
9799 if (fonts_changed_p)
9800 goto retry;
9801 else if (window_height_changed_p)
9802 {
9803 consider_all_windows_p = 1;
9804 ++update_mode_lines;
9805 ++windows_or_buffers_changed;
9806
9807 /* If window configuration was changed, frames may have been
9808 marked garbaged. Clear them or we will experience
9809 surprises wrt scrolling. */
9810 if (frame_garbaged)
9811 clear_garbaged_frames ();
9812 }
9813 }
9814 else if (EQ (selected_window, minibuf_window)
9815 && (current_buffer->clip_changed
9816 || XFASTINT (w->last_modified) < MODIFF
9817 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9818 && resize_mini_window (w, 0))
9819 {
9820 /* Resized active mini-window to fit the size of what it is
9821 showing if its contents might have changed. */
9822 must_finish = 1;
9823 consider_all_windows_p = 1;
9824 ++windows_or_buffers_changed;
9825 ++update_mode_lines;
9826
9827 /* If window configuration was changed, frames may have been
9828 marked garbaged. Clear them or we will experience
9829 surprises wrt scrolling. */
9830 if (frame_garbaged)
9831 clear_garbaged_frames ();
9832 }
9833
9834
9835 /* If showing the region, and mark has changed, we must redisplay
9836 the whole window. The assignment to this_line_start_pos prevents
9837 the optimization directly below this if-statement. */
9838 if (((!NILP (Vtransient_mark_mode)
9839 && !NILP (XBUFFER (w->buffer)->mark_active))
9840 != !NILP (w->region_showing))
9841 || (!NILP (w->region_showing)
9842 && !EQ (w->region_showing,
9843 Fmarker_position (XBUFFER (w->buffer)->mark))))
9844 CHARPOS (this_line_start_pos) = 0;
9845
9846 /* Optimize the case that only the line containing the cursor in the
9847 selected window has changed. Variables starting with this_ are
9848 set in display_line and record information about the line
9849 containing the cursor. */
9850 tlbufpos = this_line_start_pos;
9851 tlendpos = this_line_end_pos;
9852 if (!consider_all_windows_p
9853 && CHARPOS (tlbufpos) > 0
9854 && NILP (w->update_mode_line)
9855 && !current_buffer->clip_changed
9856 && !current_buffer->prevent_redisplay_optimizations_p
9857 && FRAME_VISIBLE_P (XFRAME (w->frame))
9858 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9859 /* Make sure recorded data applies to current buffer, etc. */
9860 && this_line_buffer == current_buffer
9861 && current_buffer == XBUFFER (w->buffer)
9862 && NILP (w->force_start)
9863 && NILP (w->optional_new_start)
9864 /* Point must be on the line that we have info recorded about. */
9865 && PT >= CHARPOS (tlbufpos)
9866 && PT <= Z - CHARPOS (tlendpos)
9867 /* All text outside that line, including its final newline,
9868 must be unchanged */
9869 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9870 CHARPOS (tlendpos)))
9871 {
9872 if (CHARPOS (tlbufpos) > BEGV
9873 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9874 && (CHARPOS (tlbufpos) == ZV
9875 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9876 /* Former continuation line has disappeared by becoming empty */
9877 goto cancel;
9878 else if (XFASTINT (w->last_modified) < MODIFF
9879 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9880 || MINI_WINDOW_P (w))
9881 {
9882 /* We have to handle the case of continuation around a
9883 wide-column character (See the comment in indent.c around
9884 line 885).
9885
9886 For instance, in the following case:
9887
9888 -------- Insert --------
9889 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9890 J_I_ ==> J_I_ `^^' are cursors.
9891 ^^ ^^
9892 -------- --------
9893
9894 As we have to redraw the line above, we should goto cancel. */
9895
9896 struct it it;
9897 int line_height_before = this_line_pixel_height;
9898
9899 /* Note that start_display will handle the case that the
9900 line starting at tlbufpos is a continuation lines. */
9901 start_display (&it, w, tlbufpos);
9902
9903 /* Implementation note: It this still necessary? */
9904 if (it.current_x != this_line_start_x)
9905 goto cancel;
9906
9907 TRACE ((stderr, "trying display optimization 1\n"));
9908 w->cursor.vpos = -1;
9909 overlay_arrow_seen = 0;
9910 it.vpos = this_line_vpos;
9911 it.current_y = this_line_y;
9912 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9913 display_line (&it);
9914
9915 /* If line contains point, is not continued,
9916 and ends at same distance from eob as before, we win */
9917 if (w->cursor.vpos >= 0
9918 /* Line is not continued, otherwise this_line_start_pos
9919 would have been set to 0 in display_line. */
9920 && CHARPOS (this_line_start_pos)
9921 /* Line ends as before. */
9922 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9923 /* Line has same height as before. Otherwise other lines
9924 would have to be shifted up or down. */
9925 && this_line_pixel_height == line_height_before)
9926 {
9927 /* If this is not the window's last line, we must adjust
9928 the charstarts of the lines below. */
9929 if (it.current_y < it.last_visible_y)
9930 {
9931 struct glyph_row *row
9932 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9933 int delta, delta_bytes;
9934
9935 if (Z - CHARPOS (tlendpos) == ZV)
9936 {
9937 /* This line ends at end of (accessible part of)
9938 buffer. There is no newline to count. */
9939 delta = (Z
9940 - CHARPOS (tlendpos)
9941 - MATRIX_ROW_START_CHARPOS (row));
9942 delta_bytes = (Z_BYTE
9943 - BYTEPOS (tlendpos)
9944 - MATRIX_ROW_START_BYTEPOS (row));
9945 }
9946 else
9947 {
9948 /* This line ends in a newline. Must take
9949 account of the newline and the rest of the
9950 text that follows. */
9951 delta = (Z
9952 - CHARPOS (tlendpos)
9953 - MATRIX_ROW_START_CHARPOS (row));
9954 delta_bytes = (Z_BYTE
9955 - BYTEPOS (tlendpos)
9956 - MATRIX_ROW_START_BYTEPOS (row));
9957 }
9958
9959 increment_matrix_positions (w->current_matrix,
9960 this_line_vpos + 1,
9961 w->current_matrix->nrows,
9962 delta, delta_bytes);
9963 }
9964
9965 /* If this row displays text now but previously didn't,
9966 or vice versa, w->window_end_vpos may have to be
9967 adjusted. */
9968 if ((it.glyph_row - 1)->displays_text_p)
9969 {
9970 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9971 XSETINT (w->window_end_vpos, this_line_vpos);
9972 }
9973 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9974 && this_line_vpos > 0)
9975 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9976 w->window_end_valid = Qnil;
9977
9978 /* Update hint: No need to try to scroll in update_window. */
9979 w->desired_matrix->no_scrolling_p = 1;
9980
9981 #if GLYPH_DEBUG
9982 *w->desired_matrix->method = 0;
9983 debug_method_add (w, "optimization 1");
9984 #endif
9985 goto update;
9986 }
9987 else
9988 goto cancel;
9989 }
9990 else if (/* Cursor position hasn't changed. */
9991 PT == XFASTINT (w->last_point)
9992 /* Make sure the cursor was last displayed
9993 in this window. Otherwise we have to reposition it. */
9994 && 0 <= w->cursor.vpos
9995 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
9996 {
9997 if (!must_finish)
9998 {
9999 do_pending_window_change (1);
10000
10001 /* We used to always goto end_of_redisplay here, but this
10002 isn't enough if we have a blinking cursor. */
10003 if (w->cursor_off_p == w->last_cursor_off_p)
10004 goto end_of_redisplay;
10005 }
10006 goto update;
10007 }
10008 /* If highlighting the region, or if the cursor is in the echo area,
10009 then we can't just move the cursor. */
10010 else if (! (!NILP (Vtransient_mark_mode)
10011 && !NILP (current_buffer->mark_active))
10012 && (EQ (selected_window, current_buffer->last_selected_window)
10013 || highlight_nonselected_windows)
10014 && NILP (w->region_showing)
10015 && NILP (Vshow_trailing_whitespace)
10016 && !cursor_in_echo_area)
10017 {
10018 struct it it;
10019 struct glyph_row *row;
10020
10021 /* Skip from tlbufpos to PT and see where it is. Note that
10022 PT may be in invisible text. If so, we will end at the
10023 next visible position. */
10024 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10025 NULL, DEFAULT_FACE_ID);
10026 it.current_x = this_line_start_x;
10027 it.current_y = this_line_y;
10028 it.vpos = this_line_vpos;
10029
10030 /* The call to move_it_to stops in front of PT, but
10031 moves over before-strings. */
10032 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10033
10034 if (it.vpos == this_line_vpos
10035 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10036 row->enabled_p))
10037 {
10038 xassert (this_line_vpos == it.vpos);
10039 xassert (this_line_y == it.current_y);
10040 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10041 #if GLYPH_DEBUG
10042 *w->desired_matrix->method = 0;
10043 debug_method_add (w, "optimization 3");
10044 #endif
10045 goto update;
10046 }
10047 else
10048 goto cancel;
10049 }
10050
10051 cancel:
10052 /* Text changed drastically or point moved off of line. */
10053 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10054 }
10055
10056 CHARPOS (this_line_start_pos) = 0;
10057 consider_all_windows_p |= buffer_shared > 1;
10058 ++clear_face_cache_count;
10059
10060
10061 /* Build desired matrices, and update the display. If
10062 consider_all_windows_p is non-zero, do it for all windows on all
10063 frames. Otherwise do it for selected_window, only. */
10064
10065 if (consider_all_windows_p)
10066 {
10067 Lisp_Object tail, frame;
10068 int i, n = 0, size = 50;
10069 struct frame **updated
10070 = (struct frame **) alloca (size * sizeof *updated);
10071
10072 /* Clear the face cache eventually. */
10073 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10074 {
10075 clear_face_cache (0);
10076 clear_face_cache_count = 0;
10077 }
10078
10079 /* Recompute # windows showing selected buffer. This will be
10080 incremented each time such a window is displayed. */
10081 buffer_shared = 0;
10082
10083 FOR_EACH_FRAME (tail, frame)
10084 {
10085 struct frame *f = XFRAME (frame);
10086
10087 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
10088 {
10089 if (! EQ (frame, selected_frame))
10090 /* Select the frame, for the sake of frame-local
10091 variables. */
10092 select_frame_for_redisplay (frame);
10093
10094 #ifdef HAVE_WINDOW_SYSTEM
10095 if (clear_face_cache_count % 50 == 0
10096 && FRAME_WINDOW_P (f))
10097 clear_image_cache (f, 0);
10098 #endif /* HAVE_WINDOW_SYSTEM */
10099
10100 /* Mark all the scroll bars to be removed; we'll redeem
10101 the ones we want when we redisplay their windows. */
10102 if (FRAME_DISPLAY (f)->condemn_scroll_bars_hook)
10103 FRAME_DISPLAY (f)->condemn_scroll_bars_hook (f);
10104
10105 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10106 redisplay_windows (FRAME_ROOT_WINDOW (f));
10107
10108 /* Any scroll bars which redisplay_windows should have
10109 nuked should now go away. */
10110 if (FRAME_DISPLAY (f)->judge_scroll_bars_hook)
10111 FRAME_DISPLAY (f)->judge_scroll_bars_hook (f);
10112
10113 /* If fonts changed, display again. */
10114 /* ??? rms: I suspect it is a mistake to jump all the way
10115 back to retry here. It should just retry this frame. */
10116 if (fonts_changed_p)
10117 goto retry;
10118
10119 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10120 {
10121 /* See if we have to hscroll. */
10122 if (hscroll_windows (f->root_window))
10123 goto retry;
10124
10125 /* Prevent various kinds of signals during display
10126 update. stdio is not robust about handling
10127 signals, which can cause an apparent I/O
10128 error. */
10129 if (interrupt_input)
10130 unrequest_sigio ();
10131 STOP_POLLING;
10132
10133 /* Update the display. */
10134 set_window_update_flags (XWINDOW (f->root_window), 1);
10135 pause |= update_frame (f, 0, 0);
10136 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10137 if (pause)
10138 break;
10139 #endif
10140
10141 if (n == size)
10142 {
10143 int nbytes = size * sizeof *updated;
10144 struct frame **p = (struct frame **) alloca (2 * nbytes);
10145 bcopy (updated, p, nbytes);
10146 size *= 2;
10147 }
10148
10149 updated[n++] = f;
10150 }
10151 }
10152 }
10153
10154 if (!pause)
10155 {
10156 /* Do the mark_window_display_accurate after all windows have
10157 been redisplayed because this call resets flags in buffers
10158 which are needed for proper redisplay. */
10159 for (i = 0; i < n; ++i)
10160 {
10161 struct frame *f = updated[i];
10162 mark_window_display_accurate (f->root_window, 1);
10163 if (FRAME_DISPLAY (f)->frame_up_to_date_hook)
10164 FRAME_DISPLAY (f)->frame_up_to_date_hook (f);
10165 }
10166 }
10167 }
10168 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10169 {
10170 Lisp_Object mini_window;
10171 struct frame *mini_frame;
10172
10173 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10174 /* Use list_of_error, not Qerror, so that
10175 we catch only errors and don't run the debugger. */
10176 internal_condition_case_1 (redisplay_window_1, selected_window,
10177 list_of_error,
10178 redisplay_window_error);
10179
10180 /* Compare desired and current matrices, perform output. */
10181
10182 update:
10183 /* If fonts changed, display again. */
10184 if (fonts_changed_p)
10185 goto retry;
10186
10187 /* Prevent various kinds of signals during display update.
10188 stdio is not robust about handling signals,
10189 which can cause an apparent I/O error. */
10190 if (interrupt_input)
10191 unrequest_sigio ();
10192 STOP_POLLING;
10193
10194 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10195 {
10196 if (hscroll_windows (selected_window))
10197 goto retry;
10198
10199 XWINDOW (selected_window)->must_be_updated_p = 1;
10200 pause = update_frame (sf, 0, 0);
10201 }
10202
10203 /* We may have called echo_area_display at the top of this
10204 function. If the echo area is on another frame, that may
10205 have put text on a frame other than the selected one, so the
10206 above call to update_frame would not have caught it. Catch
10207 it here. */
10208 mini_window = FRAME_MINIBUF_WINDOW (sf);
10209 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10210
10211 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10212 {
10213 XWINDOW (mini_window)->must_be_updated_p = 1;
10214 pause |= update_frame (mini_frame, 0, 0);
10215 if (!pause && hscroll_windows (mini_window))
10216 goto retry;
10217 }
10218 }
10219
10220 /* If display was paused because of pending input, make sure we do a
10221 thorough update the next time. */
10222 if (pause)
10223 {
10224 /* Prevent the optimization at the beginning of
10225 redisplay_internal that tries a single-line update of the
10226 line containing the cursor in the selected window. */
10227 CHARPOS (this_line_start_pos) = 0;
10228
10229 /* Let the overlay arrow be updated the next time. */
10230 if (!NILP (last_arrow_position))
10231 {
10232 last_arrow_position = Qt;
10233 last_arrow_string = Qt;
10234 }
10235
10236 /* If we pause after scrolling, some rows in the current
10237 matrices of some windows are not valid. */
10238 if (!WINDOW_FULL_WIDTH_P (w)
10239 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10240 update_mode_lines = 1;
10241 }
10242 else
10243 {
10244 if (!consider_all_windows_p)
10245 {
10246 /* This has already been done above if
10247 consider_all_windows_p is set. */
10248 mark_window_display_accurate_1 (w, 1);
10249
10250 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10251 last_arrow_string = Voverlay_arrow_string;
10252
10253 if (FRAME_DISPLAY (sf)->frame_up_to_date_hook != 0)
10254 FRAME_DISPLAY (sf)->frame_up_to_date_hook (sf);
10255 }
10256
10257 update_mode_lines = 0;
10258 windows_or_buffers_changed = 0;
10259 cursor_type_changed = 0;
10260 }
10261
10262 /* Start SIGIO interrupts coming again. Having them off during the
10263 code above makes it less likely one will discard output, but not
10264 impossible, since there might be stuff in the system buffer here.
10265 But it is much hairier to try to do anything about that. */
10266 if (interrupt_input)
10267 request_sigio ();
10268 RESUME_POLLING;
10269
10270 /* If a frame has become visible which was not before, redisplay
10271 again, so that we display it. Expose events for such a frame
10272 (which it gets when becoming visible) don't call the parts of
10273 redisplay constructing glyphs, so simply exposing a frame won't
10274 display anything in this case. So, we have to display these
10275 frames here explicitly. */
10276 if (!pause)
10277 {
10278 Lisp_Object tail, frame;
10279 int new_count = 0;
10280
10281 FOR_EACH_FRAME (tail, frame)
10282 {
10283 int this_is_visible = 0;
10284
10285 if (XFRAME (frame)->visible)
10286 this_is_visible = 1;
10287 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10288 if (XFRAME (frame)->visible)
10289 this_is_visible = 1;
10290
10291 if (this_is_visible)
10292 new_count++;
10293 }
10294
10295 if (new_count != number_of_visible_frames)
10296 windows_or_buffers_changed++;
10297 }
10298
10299 /* Change frame size now if a change is pending. */
10300 do_pending_window_change (1);
10301
10302 /* If we just did a pending size change, or have additional
10303 visible frames, redisplay again. */
10304 if (windows_or_buffers_changed && !pause)
10305 goto retry;
10306
10307 end_of_redisplay:
10308 unbind_to (count, Qnil);
10309 RESUME_POLLING;
10310 }
10311
10312
10313 /* Redisplay, but leave alone any recent echo area message unless
10314 another message has been requested in its place.
10315
10316 This is useful in situations where you need to redisplay but no
10317 user action has occurred, making it inappropriate for the message
10318 area to be cleared. See tracking_off and
10319 wait_reading_process_input for examples of these situations.
10320
10321 FROM_WHERE is an integer saying from where this function was
10322 called. This is useful for debugging. */
10323
10324 void
10325 redisplay_preserve_echo_area (from_where)
10326 int from_where;
10327 {
10328 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10329
10330 if (!NILP (echo_area_buffer[1]))
10331 {
10332 /* We have a previously displayed message, but no current
10333 message. Redisplay the previous message. */
10334 display_last_displayed_message_p = 1;
10335 redisplay_internal (1);
10336 display_last_displayed_message_p = 0;
10337 }
10338 else
10339 redisplay_internal (1);
10340 }
10341
10342
10343 /* Function registered with record_unwind_protect in
10344 redisplay_internal. Reset redisplaying_p to the value it had
10345 before redisplay_internal was called, and clear
10346 prevent_freeing_realized_faces_p. It also selects the previously
10347 selected frame. */
10348
10349 static Lisp_Object
10350 unwind_redisplay (val)
10351 Lisp_Object val;
10352 {
10353 Lisp_Object old_redisplaying_p, old_frame;
10354
10355 old_redisplaying_p = XCAR (val);
10356 redisplaying_p = XFASTINT (old_redisplaying_p);
10357 old_frame = XCDR (val);
10358 if (! EQ (old_frame, selected_frame))
10359 select_frame_for_redisplay (old_frame);
10360 return Qnil;
10361 }
10362
10363
10364 /* Mark the display of window W as accurate or inaccurate. If
10365 ACCURATE_P is non-zero mark display of W as accurate. If
10366 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10367 redisplay_internal is called. */
10368
10369 static void
10370 mark_window_display_accurate_1 (w, accurate_p)
10371 struct window *w;
10372 int accurate_p;
10373 {
10374 if (BUFFERP (w->buffer))
10375 {
10376 struct buffer *b = XBUFFER (w->buffer);
10377
10378 w->last_modified
10379 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10380 w->last_overlay_modified
10381 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10382 w->last_had_star
10383 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10384
10385 if (accurate_p)
10386 {
10387 b->clip_changed = 0;
10388 b->prevent_redisplay_optimizations_p = 0;
10389
10390 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10391 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10392 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10393 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10394
10395 w->current_matrix->buffer = b;
10396 w->current_matrix->begv = BUF_BEGV (b);
10397 w->current_matrix->zv = BUF_ZV (b);
10398
10399 w->last_cursor = w->cursor;
10400 w->last_cursor_off_p = w->cursor_off_p;
10401
10402 if (w == XWINDOW (selected_window))
10403 w->last_point = make_number (BUF_PT (b));
10404 else
10405 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10406 }
10407 }
10408
10409 if (accurate_p)
10410 {
10411 w->window_end_valid = w->buffer;
10412 #if 0 /* This is incorrect with variable-height lines. */
10413 xassert (XINT (w->window_end_vpos)
10414 < (WINDOW_TOTAL_LINES (w)
10415 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10416 #endif
10417 w->update_mode_line = Qnil;
10418 }
10419 }
10420
10421
10422 /* Mark the display of windows in the window tree rooted at WINDOW as
10423 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10424 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10425 be redisplayed the next time redisplay_internal is called. */
10426
10427 void
10428 mark_window_display_accurate (window, accurate_p)
10429 Lisp_Object window;
10430 int accurate_p;
10431 {
10432 struct window *w;
10433
10434 for (; !NILP (window); window = w->next)
10435 {
10436 w = XWINDOW (window);
10437 mark_window_display_accurate_1 (w, accurate_p);
10438
10439 if (!NILP (w->vchild))
10440 mark_window_display_accurate (w->vchild, accurate_p);
10441 if (!NILP (w->hchild))
10442 mark_window_display_accurate (w->hchild, accurate_p);
10443 }
10444
10445 if (accurate_p)
10446 {
10447 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10448 last_arrow_string = Voverlay_arrow_string;
10449 }
10450 else
10451 {
10452 /* Force a thorough redisplay the next time by setting
10453 last_arrow_position and last_arrow_string to t, which is
10454 unequal to any useful value of Voverlay_arrow_... */
10455 last_arrow_position = Qt;
10456 last_arrow_string = Qt;
10457 }
10458 }
10459
10460
10461 /* Return value in display table DP (Lisp_Char_Table *) for character
10462 C. Since a display table doesn't have any parent, we don't have to
10463 follow parent. Do not call this function directly but use the
10464 macro DISP_CHAR_VECTOR. */
10465
10466 Lisp_Object
10467 disp_char_vector (dp, c)
10468 struct Lisp_Char_Table *dp;
10469 int c;
10470 {
10471 int code[4], i;
10472 Lisp_Object val;
10473
10474 if (SINGLE_BYTE_CHAR_P (c))
10475 return (dp->contents[c]);
10476
10477 SPLIT_CHAR (c, code[0], code[1], code[2]);
10478 if (code[1] < 32)
10479 code[1] = -1;
10480 else if (code[2] < 32)
10481 code[2] = -1;
10482
10483 /* Here, the possible range of code[0] (== charset ID) is
10484 128..max_charset. Since the top level char table contains data
10485 for multibyte characters after 256th element, we must increment
10486 code[0] by 128 to get a correct index. */
10487 code[0] += 128;
10488 code[3] = -1; /* anchor */
10489
10490 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10491 {
10492 val = dp->contents[code[i]];
10493 if (!SUB_CHAR_TABLE_P (val))
10494 return (NILP (val) ? dp->defalt : val);
10495 }
10496
10497 /* Here, val is a sub char table. We return the default value of
10498 it. */
10499 return (dp->defalt);
10500 }
10501
10502
10503 \f
10504 /***********************************************************************
10505 Window Redisplay
10506 ***********************************************************************/
10507
10508 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10509
10510 static void
10511 redisplay_windows (window)
10512 Lisp_Object window;
10513 {
10514 while (!NILP (window))
10515 {
10516 struct window *w = XWINDOW (window);
10517
10518 if (!NILP (w->hchild))
10519 redisplay_windows (w->hchild);
10520 else if (!NILP (w->vchild))
10521 redisplay_windows (w->vchild);
10522 else
10523 {
10524 displayed_buffer = XBUFFER (w->buffer);
10525 /* Use list_of_error, not Qerror, so that
10526 we catch only errors and don't run the debugger. */
10527 internal_condition_case_1 (redisplay_window_0, window,
10528 list_of_error,
10529 redisplay_window_error);
10530 }
10531
10532 window = w->next;
10533 }
10534 }
10535
10536 static Lisp_Object
10537 redisplay_window_error ()
10538 {
10539 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10540 return Qnil;
10541 }
10542
10543 static Lisp_Object
10544 redisplay_window_0 (window)
10545 Lisp_Object window;
10546 {
10547 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10548 redisplay_window (window, 0);
10549 return Qnil;
10550 }
10551
10552 static Lisp_Object
10553 redisplay_window_1 (window)
10554 Lisp_Object window;
10555 {
10556 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10557 redisplay_window (window, 1);
10558 return Qnil;
10559 }
10560 \f
10561
10562 /* Increment GLYPH until it reaches END or CONDITION fails while
10563 adding (GLYPH)->pixel_width to X. */
10564
10565 #define SKIP_GLYPHS(glyph, end, x, condition) \
10566 do \
10567 { \
10568 (x) += (glyph)->pixel_width; \
10569 ++(glyph); \
10570 } \
10571 while ((glyph) < (end) && (condition))
10572
10573
10574 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10575 DELTA is the number of bytes by which positions recorded in ROW
10576 differ from current buffer positions. */
10577
10578 void
10579 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10580 struct window *w;
10581 struct glyph_row *row;
10582 struct glyph_matrix *matrix;
10583 int delta, delta_bytes, dy, dvpos;
10584 {
10585 struct glyph *glyph = row->glyphs[TEXT_AREA];
10586 struct glyph *end = glyph + row->used[TEXT_AREA];
10587 /* The first glyph that starts a sequence of glyphs from string. */
10588 struct glyph *string_start;
10589 /* The X coordinate of string_start. */
10590 int string_start_x;
10591 /* The last known character position. */
10592 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10593 /* The last known character position before string_start. */
10594 int string_before_pos;
10595 int x = row->x;
10596 int pt_old = PT - delta;
10597
10598 /* Skip over glyphs not having an object at the start of the row.
10599 These are special glyphs like truncation marks on terminal
10600 frames. */
10601 if (row->displays_text_p)
10602 while (glyph < end
10603 && INTEGERP (glyph->object)
10604 && glyph->charpos < 0)
10605 {
10606 x += glyph->pixel_width;
10607 ++glyph;
10608 }
10609
10610 string_start = NULL;
10611 while (glyph < end
10612 && !INTEGERP (glyph->object)
10613 && (!BUFFERP (glyph->object)
10614 || (last_pos = glyph->charpos) < pt_old))
10615 {
10616 if (! STRINGP (glyph->object))
10617 {
10618 string_start = NULL;
10619 x += glyph->pixel_width;
10620 ++glyph;
10621 }
10622 else
10623 {
10624 string_before_pos = last_pos;
10625 string_start = glyph;
10626 string_start_x = x;
10627 /* Skip all glyphs from string. */
10628 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10629 }
10630 }
10631
10632 if (string_start
10633 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10634 {
10635 /* We may have skipped over point because the previous glyphs
10636 are from string. As there's no easy way to know the
10637 character position of the current glyph, find the correct
10638 glyph on point by scanning from string_start again. */
10639 Lisp_Object limit;
10640 Lisp_Object string;
10641 int pos;
10642
10643 limit = make_number (pt_old + 1);
10644 end = glyph;
10645 glyph = string_start;
10646 x = string_start_x;
10647 string = glyph->object;
10648 pos = string_buffer_position (w, string, string_before_pos);
10649 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10650 because we always put cursor after overlay strings. */
10651 while (pos == 0 && glyph < end)
10652 {
10653 string = glyph->object;
10654 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10655 if (glyph < end)
10656 pos = string_buffer_position (w, glyph->object, string_before_pos);
10657 }
10658
10659 while (glyph < end)
10660 {
10661 pos = XINT (Fnext_single_char_property_change
10662 (make_number (pos), Qdisplay, Qnil, limit));
10663 if (pos > pt_old)
10664 break;
10665 /* Skip glyphs from the same string. */
10666 string = glyph->object;
10667 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10668 /* Skip glyphs from an overlay. */
10669 while (glyph < end
10670 && ! string_buffer_position (w, glyph->object, pos))
10671 {
10672 string = glyph->object;
10673 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10674 }
10675 }
10676 }
10677
10678 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10679 w->cursor.x = x;
10680 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10681 w->cursor.y = row->y + dy;
10682
10683 if (w == XWINDOW (selected_window))
10684 {
10685 if (!row->continued_p
10686 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10687 && row->x == 0)
10688 {
10689 this_line_buffer = XBUFFER (w->buffer);
10690
10691 CHARPOS (this_line_start_pos)
10692 = MATRIX_ROW_START_CHARPOS (row) + delta;
10693 BYTEPOS (this_line_start_pos)
10694 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10695
10696 CHARPOS (this_line_end_pos)
10697 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10698 BYTEPOS (this_line_end_pos)
10699 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10700
10701 this_line_y = w->cursor.y;
10702 this_line_pixel_height = row->height;
10703 this_line_vpos = w->cursor.vpos;
10704 this_line_start_x = row->x;
10705 }
10706 else
10707 CHARPOS (this_line_start_pos) = 0;
10708 }
10709 }
10710
10711
10712 /* Run window scroll functions, if any, for WINDOW with new window
10713 start STARTP. Sets the window start of WINDOW to that position.
10714
10715 We assume that the window's buffer is really current. */
10716
10717 static INLINE struct text_pos
10718 run_window_scroll_functions (window, startp)
10719 Lisp_Object window;
10720 struct text_pos startp;
10721 {
10722 struct window *w = XWINDOW (window);
10723 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10724
10725 if (current_buffer != XBUFFER (w->buffer))
10726 abort ();
10727
10728 if (!NILP (Vwindow_scroll_functions))
10729 {
10730 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10731 make_number (CHARPOS (startp)));
10732 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10733 /* In case the hook functions switch buffers. */
10734 if (current_buffer != XBUFFER (w->buffer))
10735 set_buffer_internal_1 (XBUFFER (w->buffer));
10736 }
10737
10738 return startp;
10739 }
10740
10741
10742 /* Make sure the line containing the cursor is fully visible.
10743 A value of 1 means there is nothing to be done.
10744 (Either the line is fully visible, or it cannot be made so,
10745 or we cannot tell.)
10746 A value of 0 means the caller should do scrolling
10747 as if point had gone off the screen. */
10748
10749 static int
10750 make_cursor_line_fully_visible (w)
10751 struct window *w;
10752 {
10753 struct glyph_matrix *matrix;
10754 struct glyph_row *row;
10755 int window_height;
10756
10757 /* It's not always possible to find the cursor, e.g, when a window
10758 is full of overlay strings. Don't do anything in that case. */
10759 if (w->cursor.vpos < 0)
10760 return 1;
10761
10762 matrix = w->desired_matrix;
10763 row = MATRIX_ROW (matrix, w->cursor.vpos);
10764
10765 /* If the cursor row is not partially visible, there's nothing to do. */
10766 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10767 return 1;
10768
10769 /* If the row the cursor is in is taller than the window's height,
10770 it's not clear what to do, so do nothing. */
10771 window_height = window_box_height (w);
10772 if (row->height >= window_height)
10773 return 1;
10774
10775 return 0;
10776
10777 #if 0
10778 /* This code used to try to scroll the window just enough to make
10779 the line visible. It returned 0 to say that the caller should
10780 allocate larger glyph matrices. */
10781
10782 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10783 {
10784 int dy = row->height - row->visible_height;
10785 w->vscroll = 0;
10786 w->cursor.y += dy;
10787 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10788 }
10789 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10790 {
10791 int dy = - (row->height - row->visible_height);
10792 w->vscroll = dy;
10793 w->cursor.y += dy;
10794 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10795 }
10796
10797 /* When we change the cursor y-position of the selected window,
10798 change this_line_y as well so that the display optimization for
10799 the cursor line of the selected window in redisplay_internal uses
10800 the correct y-position. */
10801 if (w == XWINDOW (selected_window))
10802 this_line_y = w->cursor.y;
10803
10804 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10805 redisplay with larger matrices. */
10806 if (matrix->nrows < required_matrix_height (w))
10807 {
10808 fonts_changed_p = 1;
10809 return 0;
10810 }
10811
10812 return 1;
10813 #endif /* 0 */
10814 }
10815
10816
10817 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10818 non-zero means only WINDOW is redisplayed in redisplay_internal.
10819 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10820 in redisplay_window to bring a partially visible line into view in
10821 the case that only the cursor has moved.
10822
10823 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10824 last screen line's vertical height extends past the end of the screen.
10825
10826 Value is
10827
10828 1 if scrolling succeeded
10829
10830 0 if scrolling didn't find point.
10831
10832 -1 if new fonts have been loaded so that we must interrupt
10833 redisplay, adjust glyph matrices, and try again. */
10834
10835 enum
10836 {
10837 SCROLLING_SUCCESS,
10838 SCROLLING_FAILED,
10839 SCROLLING_NEED_LARGER_MATRICES
10840 };
10841
10842 static int
10843 try_scrolling (window, just_this_one_p, scroll_conservatively,
10844 scroll_step, temp_scroll_step, last_line_misfit)
10845 Lisp_Object window;
10846 int just_this_one_p;
10847 EMACS_INT scroll_conservatively, scroll_step;
10848 int temp_scroll_step;
10849 int last_line_misfit;
10850 {
10851 struct window *w = XWINDOW (window);
10852 struct frame *f = XFRAME (w->frame);
10853 struct text_pos scroll_margin_pos;
10854 struct text_pos pos;
10855 struct text_pos startp;
10856 struct it it;
10857 Lisp_Object window_end;
10858 int this_scroll_margin;
10859 int dy = 0;
10860 int scroll_max;
10861 int rc;
10862 int amount_to_scroll = 0;
10863 Lisp_Object aggressive;
10864 int height;
10865 int end_scroll_margin;
10866
10867 #if GLYPH_DEBUG
10868 debug_method_add (w, "try_scrolling");
10869 #endif
10870
10871 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10872
10873 /* Compute scroll margin height in pixels. We scroll when point is
10874 within this distance from the top or bottom of the window. */
10875 if (scroll_margin > 0)
10876 {
10877 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10878 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10879 }
10880 else
10881 this_scroll_margin = 0;
10882
10883 /* Compute how much we should try to scroll maximally to bring point
10884 into view. */
10885 if (scroll_step || scroll_conservatively || temp_scroll_step)
10886 scroll_max = max (scroll_step,
10887 max (scroll_conservatively, temp_scroll_step));
10888 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10889 || NUMBERP (current_buffer->scroll_up_aggressively))
10890 /* We're trying to scroll because of aggressive scrolling
10891 but no scroll_step is set. Choose an arbitrary one. Maybe
10892 there should be a variable for this. */
10893 scroll_max = 10;
10894 else
10895 scroll_max = 0;
10896 scroll_max *= FRAME_LINE_HEIGHT (f);
10897
10898 /* Decide whether we have to scroll down. Start at the window end
10899 and move this_scroll_margin up to find the position of the scroll
10900 margin. */
10901 window_end = Fwindow_end (window, Qt);
10902
10903 too_near_end:
10904
10905 CHARPOS (scroll_margin_pos) = XINT (window_end);
10906 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10907
10908 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10909 if (end_scroll_margin)
10910 {
10911 start_display (&it, w, scroll_margin_pos);
10912 move_it_vertically (&it, - end_scroll_margin);
10913 scroll_margin_pos = it.current.pos;
10914 }
10915
10916 if (PT >= CHARPOS (scroll_margin_pos))
10917 {
10918 int y0;
10919
10920 /* Point is in the scroll margin at the bottom of the window, or
10921 below. Compute a new window start that makes point visible. */
10922
10923 /* Compute the distance from the scroll margin to PT.
10924 Give up if the distance is greater than scroll_max. */
10925 start_display (&it, w, scroll_margin_pos);
10926 y0 = it.current_y;
10927 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10928 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10929
10930 /* To make point visible, we have to move the window start
10931 down so that the line the cursor is in is visible, which
10932 means we have to add in the height of the cursor line. */
10933 dy = line_bottom_y (&it) - y0;
10934
10935 if (dy > scroll_max)
10936 return SCROLLING_FAILED;
10937
10938 /* Move the window start down. If scrolling conservatively,
10939 move it just enough down to make point visible. If
10940 scroll_step is set, move it down by scroll_step. */
10941 start_display (&it, w, startp);
10942
10943 if (scroll_conservatively)
10944 /* Set AMOUNT_TO_SCROLL to at least one line,
10945 and at most scroll_conservatively lines. */
10946 amount_to_scroll
10947 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10948 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10949 else if (scroll_step || temp_scroll_step)
10950 amount_to_scroll = scroll_max;
10951 else
10952 {
10953 aggressive = current_buffer->scroll_up_aggressively;
10954 height = WINDOW_BOX_TEXT_HEIGHT (w);
10955 if (NUMBERP (aggressive))
10956 amount_to_scroll = XFLOATINT (aggressive) * height;
10957 }
10958
10959 if (amount_to_scroll <= 0)
10960 return SCROLLING_FAILED;
10961
10962 /* If moving by amount_to_scroll leaves STARTP unchanged,
10963 move it down one screen line. */
10964
10965 move_it_vertically (&it, amount_to_scroll);
10966 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10967 move_it_by_lines (&it, 1, 1);
10968 startp = it.current.pos;
10969 }
10970 else
10971 {
10972 /* See if point is inside the scroll margin at the top of the
10973 window. */
10974 scroll_margin_pos = startp;
10975 if (this_scroll_margin)
10976 {
10977 start_display (&it, w, startp);
10978 move_it_vertically (&it, this_scroll_margin);
10979 scroll_margin_pos = it.current.pos;
10980 }
10981
10982 if (PT < CHARPOS (scroll_margin_pos))
10983 {
10984 /* Point is in the scroll margin at the top of the window or
10985 above what is displayed in the window. */
10986 int y0;
10987
10988 /* Compute the vertical distance from PT to the scroll
10989 margin position. Give up if distance is greater than
10990 scroll_max. */
10991 SET_TEXT_POS (pos, PT, PT_BYTE);
10992 start_display (&it, w, pos);
10993 y0 = it.current_y;
10994 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10995 it.last_visible_y, -1,
10996 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10997 dy = it.current_y - y0;
10998 if (dy > scroll_max)
10999 return SCROLLING_FAILED;
11000
11001 /* Compute new window start. */
11002 start_display (&it, w, startp);
11003
11004 if (scroll_conservatively)
11005 amount_to_scroll =
11006 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11007 else if (scroll_step || temp_scroll_step)
11008 amount_to_scroll = scroll_max;
11009 else
11010 {
11011 aggressive = current_buffer->scroll_down_aggressively;
11012 height = WINDOW_BOX_TEXT_HEIGHT (w);
11013 if (NUMBERP (aggressive))
11014 amount_to_scroll = XFLOATINT (aggressive) * height;
11015 }
11016
11017 if (amount_to_scroll <= 0)
11018 return SCROLLING_FAILED;
11019
11020 move_it_vertically (&it, - amount_to_scroll);
11021 startp = it.current.pos;
11022 }
11023 }
11024
11025 /* Run window scroll functions. */
11026 startp = run_window_scroll_functions (window, startp);
11027
11028 /* Display the window. Give up if new fonts are loaded, or if point
11029 doesn't appear. */
11030 if (!try_window (window, startp))
11031 rc = SCROLLING_NEED_LARGER_MATRICES;
11032 else if (w->cursor.vpos < 0)
11033 {
11034 clear_glyph_matrix (w->desired_matrix);
11035 rc = SCROLLING_FAILED;
11036 }
11037 else
11038 {
11039 /* Maybe forget recorded base line for line number display. */
11040 if (!just_this_one_p
11041 || current_buffer->clip_changed
11042 || BEG_UNCHANGED < CHARPOS (startp))
11043 w->base_line_number = Qnil;
11044
11045 /* If cursor ends up on a partially visible line,
11046 treat that as being off the bottom of the screen. */
11047 if (! make_cursor_line_fully_visible (w))
11048 {
11049 clear_glyph_matrix (w->desired_matrix);
11050 last_line_misfit = 1;
11051 goto too_near_end;
11052 }
11053 rc = SCROLLING_SUCCESS;
11054 }
11055
11056 return rc;
11057 }
11058
11059
11060 /* Compute a suitable window start for window W if display of W starts
11061 on a continuation line. Value is non-zero if a new window start
11062 was computed.
11063
11064 The new window start will be computed, based on W's width, starting
11065 from the start of the continued line. It is the start of the
11066 screen line with the minimum distance from the old start W->start. */
11067
11068 static int
11069 compute_window_start_on_continuation_line (w)
11070 struct window *w;
11071 {
11072 struct text_pos pos, start_pos;
11073 int window_start_changed_p = 0;
11074
11075 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11076
11077 /* If window start is on a continuation line... Window start may be
11078 < BEGV in case there's invisible text at the start of the
11079 buffer (M-x rmail, for example). */
11080 if (CHARPOS (start_pos) > BEGV
11081 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11082 {
11083 struct it it;
11084 struct glyph_row *row;
11085
11086 /* Handle the case that the window start is out of range. */
11087 if (CHARPOS (start_pos) < BEGV)
11088 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11089 else if (CHARPOS (start_pos) > ZV)
11090 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11091
11092 /* Find the start of the continued line. This should be fast
11093 because scan_buffer is fast (newline cache). */
11094 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11095 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11096 row, DEFAULT_FACE_ID);
11097 reseat_at_previous_visible_line_start (&it);
11098
11099 /* If the line start is "too far" away from the window start,
11100 say it takes too much time to compute a new window start. */
11101 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11102 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11103 {
11104 int min_distance, distance;
11105
11106 /* Move forward by display lines to find the new window
11107 start. If window width was enlarged, the new start can
11108 be expected to be > the old start. If window width was
11109 decreased, the new window start will be < the old start.
11110 So, we're looking for the display line start with the
11111 minimum distance from the old window start. */
11112 pos = it.current.pos;
11113 min_distance = INFINITY;
11114 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11115 distance < min_distance)
11116 {
11117 min_distance = distance;
11118 pos = it.current.pos;
11119 move_it_by_lines (&it, 1, 0);
11120 }
11121
11122 /* Set the window start there. */
11123 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11124 window_start_changed_p = 1;
11125 }
11126 }
11127
11128 return window_start_changed_p;
11129 }
11130
11131
11132 /* Try cursor movement in case text has not changed in window WINDOW,
11133 with window start STARTP. Value is
11134
11135 CURSOR_MOVEMENT_SUCCESS if successful
11136
11137 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11138
11139 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11140 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11141 we want to scroll as if scroll-step were set to 1. See the code.
11142
11143 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11144 which case we have to abort this redisplay, and adjust matrices
11145 first. */
11146
11147 enum
11148 {
11149 CURSOR_MOVEMENT_SUCCESS,
11150 CURSOR_MOVEMENT_CANNOT_BE_USED,
11151 CURSOR_MOVEMENT_MUST_SCROLL,
11152 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11153 };
11154
11155 static int
11156 try_cursor_movement (window, startp, scroll_step)
11157 Lisp_Object window;
11158 struct text_pos startp;
11159 int *scroll_step;
11160 {
11161 struct window *w = XWINDOW (window);
11162 struct frame *f = XFRAME (w->frame);
11163 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11164
11165 #if GLYPH_DEBUG
11166 if (inhibit_try_cursor_movement)
11167 return rc;
11168 #endif
11169
11170 /* Handle case where text has not changed, only point, and it has
11171 not moved off the frame. */
11172 if (/* Point may be in this window. */
11173 PT >= CHARPOS (startp)
11174 /* Selective display hasn't changed. */
11175 && !current_buffer->clip_changed
11176 /* Function force-mode-line-update is used to force a thorough
11177 redisplay. It sets either windows_or_buffers_changed or
11178 update_mode_lines. So don't take a shortcut here for these
11179 cases. */
11180 && !update_mode_lines
11181 && !windows_or_buffers_changed
11182 && !cursor_type_changed
11183 /* Can't use this case if highlighting a region. When a
11184 region exists, cursor movement has to do more than just
11185 set the cursor. */
11186 && !(!NILP (Vtransient_mark_mode)
11187 && !NILP (current_buffer->mark_active))
11188 && NILP (w->region_showing)
11189 && NILP (Vshow_trailing_whitespace)
11190 /* Right after splitting windows, last_point may be nil. */
11191 && INTEGERP (w->last_point)
11192 /* This code is not used for mini-buffer for the sake of the case
11193 of redisplaying to replace an echo area message; since in
11194 that case the mini-buffer contents per se are usually
11195 unchanged. This code is of no real use in the mini-buffer
11196 since the handling of this_line_start_pos, etc., in redisplay
11197 handles the same cases. */
11198 && !EQ (window, minibuf_window)
11199 /* When splitting windows or for new windows, it happens that
11200 redisplay is called with a nil window_end_vpos or one being
11201 larger than the window. This should really be fixed in
11202 window.c. I don't have this on my list, now, so we do
11203 approximately the same as the old redisplay code. --gerd. */
11204 && INTEGERP (w->window_end_vpos)
11205 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11206 && (FRAME_WINDOW_P (f)
11207 || !MARKERP (Voverlay_arrow_position)
11208 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11209 {
11210 int this_scroll_margin;
11211 struct glyph_row *row = NULL;
11212
11213 #if GLYPH_DEBUG
11214 debug_method_add (w, "cursor movement");
11215 #endif
11216
11217 /* Scroll if point within this distance from the top or bottom
11218 of the window. This is a pixel value. */
11219 this_scroll_margin = max (0, scroll_margin);
11220 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11221 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11222
11223 /* Start with the row the cursor was displayed during the last
11224 not paused redisplay. Give up if that row is not valid. */
11225 if (w->last_cursor.vpos < 0
11226 || w->last_cursor.vpos >= w->current_matrix->nrows)
11227 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11228 else
11229 {
11230 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11231 if (row->mode_line_p)
11232 ++row;
11233 if (!row->enabled_p)
11234 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11235 }
11236
11237 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11238 {
11239 int scroll_p = 0;
11240 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11241
11242 if (PT > XFASTINT (w->last_point))
11243 {
11244 /* Point has moved forward. */
11245 while (MATRIX_ROW_END_CHARPOS (row) < PT
11246 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11247 {
11248 xassert (row->enabled_p);
11249 ++row;
11250 }
11251
11252 /* The end position of a row equals the start position
11253 of the next row. If PT is there, we would rather
11254 display it in the next line. */
11255 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11256 && MATRIX_ROW_END_CHARPOS (row) == PT
11257 && !cursor_row_p (w, row))
11258 ++row;
11259
11260 /* If within the scroll margin, scroll. Note that
11261 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11262 the next line would be drawn, and that
11263 this_scroll_margin can be zero. */
11264 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11265 || PT > MATRIX_ROW_END_CHARPOS (row)
11266 /* Line is completely visible last line in window
11267 and PT is to be set in the next line. */
11268 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11269 && PT == MATRIX_ROW_END_CHARPOS (row)
11270 && !row->ends_at_zv_p
11271 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11272 scroll_p = 1;
11273 }
11274 else if (PT < XFASTINT (w->last_point))
11275 {
11276 /* Cursor has to be moved backward. Note that PT >=
11277 CHARPOS (startp) because of the outer
11278 if-statement. */
11279 while (!row->mode_line_p
11280 && (MATRIX_ROW_START_CHARPOS (row) > PT
11281 || (MATRIX_ROW_START_CHARPOS (row) == PT
11282 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11283 && (row->y > this_scroll_margin
11284 || CHARPOS (startp) == BEGV))
11285 {
11286 xassert (row->enabled_p);
11287 --row;
11288 }
11289
11290 /* Consider the following case: Window starts at BEGV,
11291 there is invisible, intangible text at BEGV, so that
11292 display starts at some point START > BEGV. It can
11293 happen that we are called with PT somewhere between
11294 BEGV and START. Try to handle that case. */
11295 if (row < w->current_matrix->rows
11296 || row->mode_line_p)
11297 {
11298 row = w->current_matrix->rows;
11299 if (row->mode_line_p)
11300 ++row;
11301 }
11302
11303 /* Due to newlines in overlay strings, we may have to
11304 skip forward over overlay strings. */
11305 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11306 && MATRIX_ROW_END_CHARPOS (row) == PT
11307 && !cursor_row_p (w, row))
11308 ++row;
11309
11310 /* If within the scroll margin, scroll. */
11311 if (row->y < this_scroll_margin
11312 && CHARPOS (startp) != BEGV)
11313 scroll_p = 1;
11314 }
11315
11316 if (PT < MATRIX_ROW_START_CHARPOS (row)
11317 || PT > MATRIX_ROW_END_CHARPOS (row))
11318 {
11319 /* if PT is not in the glyph row, give up. */
11320 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11321 }
11322 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11323 {
11324 if (PT == MATRIX_ROW_END_CHARPOS (row)
11325 && !row->ends_at_zv_p
11326 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11327 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11328 else if (row->height > window_box_height (w))
11329 {
11330 /* If we end up in a partially visible line, let's
11331 make it fully visible, except when it's taller
11332 than the window, in which case we can't do much
11333 about it. */
11334 *scroll_step = 1;
11335 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11336 }
11337 else
11338 {
11339 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11340 if (!make_cursor_line_fully_visible (w))
11341 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11342 else
11343 rc = CURSOR_MOVEMENT_SUCCESS;
11344 }
11345 }
11346 else if (scroll_p)
11347 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11348 else
11349 {
11350 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11351 rc = CURSOR_MOVEMENT_SUCCESS;
11352 }
11353 }
11354 }
11355
11356 return rc;
11357 }
11358
11359 void
11360 set_vertical_scroll_bar (w)
11361 struct window *w;
11362 {
11363 int start, end, whole;
11364
11365 /* Calculate the start and end positions for the current window.
11366 At some point, it would be nice to choose between scrollbars
11367 which reflect the whole buffer size, with special markers
11368 indicating narrowing, and scrollbars which reflect only the
11369 visible region.
11370
11371 Note that mini-buffers sometimes aren't displaying any text. */
11372 if (!MINI_WINDOW_P (w)
11373 || (w == XWINDOW (minibuf_window)
11374 && NILP (echo_area_buffer[0])))
11375 {
11376 struct buffer *buf = XBUFFER (w->buffer);
11377 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11378 start = marker_position (w->start) - BUF_BEGV (buf);
11379 /* I don't think this is guaranteed to be right. For the
11380 moment, we'll pretend it is. */
11381 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11382
11383 if (end < start)
11384 end = start;
11385 if (whole < (end - start))
11386 whole = end - start;
11387 }
11388 else
11389 start = end = whole = 0;
11390
11391 /* Indicate what this scroll bar ought to be displaying now. */
11392 if (FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11393 (*FRAME_DISPLAY (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
11394 (w, end - start, whole, start);
11395 }
11396
11397 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11398 selected_window is redisplayed.
11399
11400 We can return without actually redisplaying the window if
11401 fonts_changed_p is nonzero. In that case, redisplay_internal will
11402 retry. */
11403
11404 static void
11405 redisplay_window (window, just_this_one_p)
11406 Lisp_Object window;
11407 int just_this_one_p;
11408 {
11409 struct window *w = XWINDOW (window);
11410 struct frame *f = XFRAME (w->frame);
11411 struct buffer *buffer = XBUFFER (w->buffer);
11412 struct buffer *old = current_buffer;
11413 struct text_pos lpoint, opoint, startp;
11414 int update_mode_line;
11415 int tem;
11416 struct it it;
11417 /* Record it now because it's overwritten. */
11418 int current_matrix_up_to_date_p = 0;
11419 /* This is less strict than current_matrix_up_to_date_p.
11420 It indictes that the buffer contents and narrowing are unchanged. */
11421 int buffer_unchanged_p = 0;
11422 int temp_scroll_step = 0;
11423 int count = SPECPDL_INDEX ();
11424 int rc;
11425 int centering_position;
11426 int last_line_misfit = 0;
11427
11428 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11429 opoint = lpoint;
11430
11431 /* W must be a leaf window here. */
11432 xassert (!NILP (w->buffer));
11433 #if GLYPH_DEBUG
11434 *w->desired_matrix->method = 0;
11435 #endif
11436
11437 specbind (Qinhibit_point_motion_hooks, Qt);
11438
11439 reconsider_clip_changes (w, buffer);
11440
11441 /* Has the mode line to be updated? */
11442 update_mode_line = (!NILP (w->update_mode_line)
11443 || update_mode_lines
11444 || buffer->clip_changed
11445 || buffer->prevent_redisplay_optimizations_p);
11446
11447 if (MINI_WINDOW_P (w))
11448 {
11449 if (w == XWINDOW (echo_area_window)
11450 && !NILP (echo_area_buffer[0]))
11451 {
11452 if (update_mode_line)
11453 /* We may have to update a tty frame's menu bar or a
11454 tool-bar. Example `M-x C-h C-h C-g'. */
11455 goto finish_menu_bars;
11456 else
11457 /* We've already displayed the echo area glyphs in this window. */
11458 goto finish_scroll_bars;
11459 }
11460 else if ((w != XWINDOW (minibuf_window)
11461 || minibuf_level == 0)
11462 /* When buffer is nonempty, redisplay window normally. */
11463 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11464 /* Quail displays non-mini buffers in minibuffer window.
11465 In that case, redisplay the window normally. */
11466 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11467 {
11468 /* W is a mini-buffer window, but it's not active, so clear
11469 it. */
11470 int yb = window_text_bottom_y (w);
11471 struct glyph_row *row;
11472 int y;
11473
11474 for (y = 0, row = w->desired_matrix->rows;
11475 y < yb;
11476 y += row->height, ++row)
11477 blank_row (w, row, y);
11478 goto finish_scroll_bars;
11479 }
11480
11481 clear_glyph_matrix (w->desired_matrix);
11482 }
11483
11484 /* Otherwise set up data on this window; select its buffer and point
11485 value. */
11486 /* Really select the buffer, for the sake of buffer-local
11487 variables. */
11488 set_buffer_internal_1 (XBUFFER (w->buffer));
11489 SET_TEXT_POS (opoint, PT, PT_BYTE);
11490
11491 current_matrix_up_to_date_p
11492 = (!NILP (w->window_end_valid)
11493 && !current_buffer->clip_changed
11494 && !current_buffer->prevent_redisplay_optimizations_p
11495 && XFASTINT (w->last_modified) >= MODIFF
11496 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11497
11498 buffer_unchanged_p
11499 = (!NILP (w->window_end_valid)
11500 && !current_buffer->clip_changed
11501 && XFASTINT (w->last_modified) >= MODIFF
11502 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11503
11504 /* When windows_or_buffers_changed is non-zero, we can't rely on
11505 the window end being valid, so set it to nil there. */
11506 if (windows_or_buffers_changed)
11507 {
11508 /* If window starts on a continuation line, maybe adjust the
11509 window start in case the window's width changed. */
11510 if (XMARKER (w->start)->buffer == current_buffer)
11511 compute_window_start_on_continuation_line (w);
11512
11513 w->window_end_valid = Qnil;
11514 }
11515
11516 /* Some sanity checks. */
11517 CHECK_WINDOW_END (w);
11518 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11519 abort ();
11520 if (BYTEPOS (opoint) < CHARPOS (opoint))
11521 abort ();
11522
11523 /* If %c is in mode line, update it if needed. */
11524 if (!NILP (w->column_number_displayed)
11525 /* This alternative quickly identifies a common case
11526 where no change is needed. */
11527 && !(PT == XFASTINT (w->last_point)
11528 && XFASTINT (w->last_modified) >= MODIFF
11529 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11530 && (XFASTINT (w->column_number_displayed)
11531 != (int) current_column ())) /* iftc */
11532 update_mode_line = 1;
11533
11534 /* Count number of windows showing the selected buffer. An indirect
11535 buffer counts as its base buffer. */
11536 if (!just_this_one_p)
11537 {
11538 struct buffer *current_base, *window_base;
11539 current_base = current_buffer;
11540 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11541 if (current_base->base_buffer)
11542 current_base = current_base->base_buffer;
11543 if (window_base->base_buffer)
11544 window_base = window_base->base_buffer;
11545 if (current_base == window_base)
11546 buffer_shared++;
11547 }
11548
11549 /* Point refers normally to the selected window. For any other
11550 window, set up appropriate value. */
11551 if (!EQ (window, selected_window))
11552 {
11553 int new_pt = XMARKER (w->pointm)->charpos;
11554 int new_pt_byte = marker_byte_position (w->pointm);
11555 if (new_pt < BEGV)
11556 {
11557 new_pt = BEGV;
11558 new_pt_byte = BEGV_BYTE;
11559 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11560 }
11561 else if (new_pt > (ZV - 1))
11562 {
11563 new_pt = ZV;
11564 new_pt_byte = ZV_BYTE;
11565 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11566 }
11567
11568 /* We don't use SET_PT so that the point-motion hooks don't run. */
11569 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11570 }
11571
11572 /* If any of the character widths specified in the display table
11573 have changed, invalidate the width run cache. It's true that
11574 this may be a bit late to catch such changes, but the rest of
11575 redisplay goes (non-fatally) haywire when the display table is
11576 changed, so why should we worry about doing any better? */
11577 if (current_buffer->width_run_cache)
11578 {
11579 struct Lisp_Char_Table *disptab = buffer_display_table ();
11580
11581 if (! disptab_matches_widthtab (disptab,
11582 XVECTOR (current_buffer->width_table)))
11583 {
11584 invalidate_region_cache (current_buffer,
11585 current_buffer->width_run_cache,
11586 BEG, Z);
11587 recompute_width_table (current_buffer, disptab);
11588 }
11589 }
11590
11591 /* If window-start is screwed up, choose a new one. */
11592 if (XMARKER (w->start)->buffer != current_buffer)
11593 goto recenter;
11594
11595 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11596
11597 /* If someone specified a new starting point but did not insist,
11598 check whether it can be used. */
11599 if (!NILP (w->optional_new_start)
11600 && CHARPOS (startp) >= BEGV
11601 && CHARPOS (startp) <= ZV)
11602 {
11603 w->optional_new_start = Qnil;
11604 start_display (&it, w, startp);
11605 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11606 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11607 if (IT_CHARPOS (it) == PT)
11608 w->force_start = Qt;
11609 /* IT may overshoot PT if text at PT is invisible. */
11610 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11611 w->force_start = Qt;
11612
11613
11614 }
11615
11616 /* Handle case where place to start displaying has been specified,
11617 unless the specified location is outside the accessible range. */
11618 if (!NILP (w->force_start)
11619 || w->frozen_window_start_p)
11620 {
11621 /* We set this later on if we have to adjust point. */
11622 int new_vpos = -1;
11623
11624 w->force_start = Qnil;
11625 w->vscroll = 0;
11626 w->window_end_valid = Qnil;
11627
11628 /* Forget any recorded base line for line number display. */
11629 if (!buffer_unchanged_p)
11630 w->base_line_number = Qnil;
11631
11632 /* Redisplay the mode line. Select the buffer properly for that.
11633 Also, run the hook window-scroll-functions
11634 because we have scrolled. */
11635 /* Note, we do this after clearing force_start because
11636 if there's an error, it is better to forget about force_start
11637 than to get into an infinite loop calling the hook functions
11638 and having them get more errors. */
11639 if (!update_mode_line
11640 || ! NILP (Vwindow_scroll_functions))
11641 {
11642 update_mode_line = 1;
11643 w->update_mode_line = Qt;
11644 startp = run_window_scroll_functions (window, startp);
11645 }
11646
11647 w->last_modified = make_number (0);
11648 w->last_overlay_modified = make_number (0);
11649 if (CHARPOS (startp) < BEGV)
11650 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11651 else if (CHARPOS (startp) > ZV)
11652 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11653
11654 /* Redisplay, then check if cursor has been set during the
11655 redisplay. Give up if new fonts were loaded. */
11656 if (!try_window (window, startp))
11657 {
11658 w->force_start = Qt;
11659 clear_glyph_matrix (w->desired_matrix);
11660 goto need_larger_matrices;
11661 }
11662
11663 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11664 {
11665 /* If point does not appear, try to move point so it does
11666 appear. The desired matrix has been built above, so we
11667 can use it here. */
11668 new_vpos = window_box_height (w) / 2;
11669 }
11670
11671 if (!make_cursor_line_fully_visible (w))
11672 {
11673 /* Point does appear, but on a line partly visible at end of window.
11674 Move it back to a fully-visible line. */
11675 new_vpos = window_box_height (w);
11676 }
11677
11678 /* If we need to move point for either of the above reasons,
11679 now actually do it. */
11680 if (new_vpos >= 0)
11681 {
11682 struct glyph_row *row;
11683
11684 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11685 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11686 ++row;
11687
11688 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11689 MATRIX_ROW_START_BYTEPOS (row));
11690
11691 if (w != XWINDOW (selected_window))
11692 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11693 else if (current_buffer == old)
11694 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11695
11696 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11697
11698 /* If we are highlighting the region, then we just changed
11699 the region, so redisplay to show it. */
11700 if (!NILP (Vtransient_mark_mode)
11701 && !NILP (current_buffer->mark_active))
11702 {
11703 clear_glyph_matrix (w->desired_matrix);
11704 if (!try_window (window, startp))
11705 goto need_larger_matrices;
11706 }
11707 }
11708
11709 #if GLYPH_DEBUG
11710 debug_method_add (w, "forced window start");
11711 #endif
11712 goto done;
11713 }
11714
11715 /* Handle case where text has not changed, only point, and it has
11716 not moved off the frame, and we are not retrying after hscroll.
11717 (current_matrix_up_to_date_p is nonzero when retrying.) */
11718 if (current_matrix_up_to_date_p
11719 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11720 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11721 {
11722 switch (rc)
11723 {
11724 case CURSOR_MOVEMENT_SUCCESS:
11725 goto done;
11726
11727 #if 0 /* try_cursor_movement never returns this value. */
11728 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11729 goto need_larger_matrices;
11730 #endif
11731
11732 case CURSOR_MOVEMENT_MUST_SCROLL:
11733 goto try_to_scroll;
11734
11735 default:
11736 abort ();
11737 }
11738 }
11739 /* If current starting point was originally the beginning of a line
11740 but no longer is, find a new starting point. */
11741 else if (!NILP (w->start_at_line_beg)
11742 && !(CHARPOS (startp) <= BEGV
11743 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11744 {
11745 #if GLYPH_DEBUG
11746 debug_method_add (w, "recenter 1");
11747 #endif
11748 goto recenter;
11749 }
11750
11751 /* Try scrolling with try_window_id. Value is > 0 if update has
11752 been done, it is -1 if we know that the same window start will
11753 not work. It is 0 if unsuccessful for some other reason. */
11754 else if ((tem = try_window_id (w)) != 0)
11755 {
11756 #if GLYPH_DEBUG
11757 debug_method_add (w, "try_window_id %d", tem);
11758 #endif
11759
11760 if (fonts_changed_p)
11761 goto need_larger_matrices;
11762 if (tem > 0)
11763 goto done;
11764
11765 /* Otherwise try_window_id has returned -1 which means that we
11766 don't want the alternative below this comment to execute. */
11767 }
11768 else if (CHARPOS (startp) >= BEGV
11769 && CHARPOS (startp) <= ZV
11770 && PT >= CHARPOS (startp)
11771 && (CHARPOS (startp) < ZV
11772 /* Avoid starting at end of buffer. */
11773 || CHARPOS (startp) == BEGV
11774 || (XFASTINT (w->last_modified) >= MODIFF
11775 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11776 {
11777 #if GLYPH_DEBUG
11778 debug_method_add (w, "same window start");
11779 #endif
11780
11781 /* Try to redisplay starting at same place as before.
11782 If point has not moved off frame, accept the results. */
11783 if (!current_matrix_up_to_date_p
11784 /* Don't use try_window_reusing_current_matrix in this case
11785 because a window scroll function can have changed the
11786 buffer. */
11787 || !NILP (Vwindow_scroll_functions)
11788 || MINI_WINDOW_P (w)
11789 || !try_window_reusing_current_matrix (w))
11790 {
11791 IF_DEBUG (debug_method_add (w, "1"));
11792 try_window (window, startp);
11793 }
11794
11795 if (fonts_changed_p)
11796 goto need_larger_matrices;
11797
11798 if (w->cursor.vpos >= 0)
11799 {
11800 if (!just_this_one_p
11801 || current_buffer->clip_changed
11802 || BEG_UNCHANGED < CHARPOS (startp))
11803 /* Forget any recorded base line for line number display. */
11804 w->base_line_number = Qnil;
11805
11806 if (!make_cursor_line_fully_visible (w))
11807 {
11808 clear_glyph_matrix (w->desired_matrix);
11809 last_line_misfit = 1;
11810 }
11811 /* Drop through and scroll. */
11812 else
11813 goto done;
11814 }
11815 else
11816 clear_glyph_matrix (w->desired_matrix);
11817 }
11818
11819 try_to_scroll:
11820
11821 w->last_modified = make_number (0);
11822 w->last_overlay_modified = make_number (0);
11823
11824 /* Redisplay the mode line. Select the buffer properly for that. */
11825 if (!update_mode_line)
11826 {
11827 update_mode_line = 1;
11828 w->update_mode_line = Qt;
11829 }
11830
11831 /* Try to scroll by specified few lines. */
11832 if ((scroll_conservatively
11833 || scroll_step
11834 || temp_scroll_step
11835 || NUMBERP (current_buffer->scroll_up_aggressively)
11836 || NUMBERP (current_buffer->scroll_down_aggressively))
11837 && !current_buffer->clip_changed
11838 && CHARPOS (startp) >= BEGV
11839 && CHARPOS (startp) <= ZV)
11840 {
11841 /* The function returns -1 if new fonts were loaded, 1 if
11842 successful, 0 if not successful. */
11843 int rc = try_scrolling (window, just_this_one_p,
11844 scroll_conservatively,
11845 scroll_step,
11846 temp_scroll_step, last_line_misfit);
11847 switch (rc)
11848 {
11849 case SCROLLING_SUCCESS:
11850 goto done;
11851
11852 case SCROLLING_NEED_LARGER_MATRICES:
11853 goto need_larger_matrices;
11854
11855 case SCROLLING_FAILED:
11856 break;
11857
11858 default:
11859 abort ();
11860 }
11861 }
11862
11863 /* Finally, just choose place to start which centers point */
11864
11865 recenter:
11866 centering_position = window_box_height (w) / 2;
11867
11868 point_at_top:
11869 /* Jump here with centering_position already set to 0. */
11870
11871 #if GLYPH_DEBUG
11872 debug_method_add (w, "recenter");
11873 #endif
11874
11875 /* w->vscroll = 0; */
11876
11877 /* Forget any previously recorded base line for line number display. */
11878 if (!buffer_unchanged_p)
11879 w->base_line_number = Qnil;
11880
11881 /* Move backward half the height of the window. */
11882 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11883 it.current_y = it.last_visible_y;
11884 move_it_vertically_backward (&it, centering_position);
11885 xassert (IT_CHARPOS (it) >= BEGV);
11886
11887 /* The function move_it_vertically_backward may move over more
11888 than the specified y-distance. If it->w is small, e.g. a
11889 mini-buffer window, we may end up in front of the window's
11890 display area. Start displaying at the start of the line
11891 containing PT in this case. */
11892 if (it.current_y <= 0)
11893 {
11894 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11895 move_it_vertically (&it, 0);
11896 xassert (IT_CHARPOS (it) <= PT);
11897 it.current_y = 0;
11898 }
11899
11900 it.current_x = it.hpos = 0;
11901
11902 /* Set startp here explicitly in case that helps avoid an infinite loop
11903 in case the window-scroll-functions functions get errors. */
11904 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11905
11906 /* Run scroll hooks. */
11907 startp = run_window_scroll_functions (window, it.current.pos);
11908
11909 /* Redisplay the window. */
11910 if (!current_matrix_up_to_date_p
11911 || windows_or_buffers_changed
11912 || cursor_type_changed
11913 /* Don't use try_window_reusing_current_matrix in this case
11914 because it can have changed the buffer. */
11915 || !NILP (Vwindow_scroll_functions)
11916 || !just_this_one_p
11917 || MINI_WINDOW_P (w)
11918 || !try_window_reusing_current_matrix (w))
11919 try_window (window, startp);
11920
11921 /* If new fonts have been loaded (due to fontsets), give up. We
11922 have to start a new redisplay since we need to re-adjust glyph
11923 matrices. */
11924 if (fonts_changed_p)
11925 goto need_larger_matrices;
11926
11927 /* If cursor did not appear assume that the middle of the window is
11928 in the first line of the window. Do it again with the next line.
11929 (Imagine a window of height 100, displaying two lines of height
11930 60. Moving back 50 from it->last_visible_y will end in the first
11931 line.) */
11932 if (w->cursor.vpos < 0)
11933 {
11934 if (!NILP (w->window_end_valid)
11935 && PT >= Z - XFASTINT (w->window_end_pos))
11936 {
11937 clear_glyph_matrix (w->desired_matrix);
11938 move_it_by_lines (&it, 1, 0);
11939 try_window (window, it.current.pos);
11940 }
11941 else if (PT < IT_CHARPOS (it))
11942 {
11943 clear_glyph_matrix (w->desired_matrix);
11944 move_it_by_lines (&it, -1, 0);
11945 try_window (window, it.current.pos);
11946 }
11947 else
11948 {
11949 /* Not much we can do about it. */
11950 }
11951 }
11952
11953 /* Consider the following case: Window starts at BEGV, there is
11954 invisible, intangible text at BEGV, so that display starts at
11955 some point START > BEGV. It can happen that we are called with
11956 PT somewhere between BEGV and START. Try to handle that case. */
11957 if (w->cursor.vpos < 0)
11958 {
11959 struct glyph_row *row = w->current_matrix->rows;
11960 if (row->mode_line_p)
11961 ++row;
11962 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11963 }
11964
11965 if (!make_cursor_line_fully_visible (w))
11966 {
11967 /* If vscroll is enabled, disable it and try again. */
11968 if (w->vscroll)
11969 {
11970 w->vscroll = 0;
11971 clear_glyph_matrix (w->desired_matrix);
11972 goto recenter;
11973 }
11974
11975 /* If centering point failed to make the whole line visible,
11976 put point at the top instead. That has to make the whole line
11977 visible, if it can be done. */
11978 centering_position = 0;
11979 goto point_at_top;
11980 }
11981
11982 done:
11983
11984 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11985 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11986 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11987 ? Qt : Qnil);
11988
11989 /* Display the mode line, if we must. */
11990 if ((update_mode_line
11991 /* If window not full width, must redo its mode line
11992 if (a) the window to its side is being redone and
11993 (b) we do a frame-based redisplay. This is a consequence
11994 of how inverted lines are drawn in frame-based redisplay. */
11995 || (!just_this_one_p
11996 && !FRAME_WINDOW_P (f)
11997 && !WINDOW_FULL_WIDTH_P (w))
11998 /* Line number to display. */
11999 || INTEGERP (w->base_line_pos)
12000 /* Column number is displayed and different from the one displayed. */
12001 || (!NILP (w->column_number_displayed)
12002 && (XFASTINT (w->column_number_displayed)
12003 != (int) current_column ()))) /* iftc */
12004 /* This means that the window has a mode line. */
12005 && (WINDOW_WANTS_MODELINE_P (w)
12006 || WINDOW_WANTS_HEADER_LINE_P (w)))
12007 {
12008 display_mode_lines (w);
12009
12010 /* If mode line height has changed, arrange for a thorough
12011 immediate redisplay using the correct mode line height. */
12012 if (WINDOW_WANTS_MODELINE_P (w)
12013 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12014 {
12015 fonts_changed_p = 1;
12016 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12017 = DESIRED_MODE_LINE_HEIGHT (w);
12018 }
12019
12020 /* If top line height has changed, arrange for a thorough
12021 immediate redisplay using the correct mode line height. */
12022 if (WINDOW_WANTS_HEADER_LINE_P (w)
12023 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12024 {
12025 fonts_changed_p = 1;
12026 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12027 = DESIRED_HEADER_LINE_HEIGHT (w);
12028 }
12029
12030 if (fonts_changed_p)
12031 goto need_larger_matrices;
12032 }
12033
12034 if (!line_number_displayed
12035 && !BUFFERP (w->base_line_pos))
12036 {
12037 w->base_line_pos = Qnil;
12038 w->base_line_number = Qnil;
12039 }
12040
12041 finish_menu_bars:
12042
12043 /* When we reach a frame's selected window, redo the frame's menu bar. */
12044 if (update_mode_line
12045 && EQ (FRAME_SELECTED_WINDOW (f), window))
12046 {
12047 int redisplay_menu_p = 0;
12048 int redisplay_tool_bar_p = 0;
12049
12050 if (FRAME_WINDOW_P (f))
12051 {
12052 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12053 || defined (USE_GTK)
12054 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12055 #else
12056 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12057 #endif
12058 }
12059 else
12060 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12061
12062 if (redisplay_menu_p)
12063 display_menu_bar (w);
12064
12065 #ifdef HAVE_WINDOW_SYSTEM
12066 #ifdef USE_GTK
12067 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12068 #else
12069 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12070 && (FRAME_TOOL_BAR_LINES (f) > 0
12071 || auto_resize_tool_bars_p);
12072
12073 #endif
12074
12075 if (redisplay_tool_bar_p)
12076 redisplay_tool_bar (f);
12077 #endif
12078 }
12079
12080 /* We go to this label, with fonts_changed_p nonzero,
12081 if it is necessary to try again using larger glyph matrices.
12082 We have to redeem the scroll bar even in this case,
12083 because the loop in redisplay_internal expects that. */
12084 need_larger_matrices:
12085 ;
12086 finish_scroll_bars:
12087
12088 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12089 {
12090 /* Set the thumb's position and size. */
12091 set_vertical_scroll_bar (w);
12092
12093 /* Note that we actually used the scroll bar attached to this
12094 window, so it shouldn't be deleted at the end of redisplay. */
12095 if (FRAME_DISPLAY (f)->redeem_scroll_bar_hook)
12096 (*FRAME_DISPLAY (f)->redeem_scroll_bar_hook) (w);
12097 }
12098
12099 /* Restore current_buffer and value of point in it. */
12100 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12101 set_buffer_internal_1 (old);
12102 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12103
12104 unbind_to (count, Qnil);
12105 }
12106
12107
12108 /* Build the complete desired matrix of WINDOW with a window start
12109 buffer position POS. Value is non-zero if successful. It is zero
12110 if fonts were loaded during redisplay which makes re-adjusting
12111 glyph matrices necessary. */
12112
12113 int
12114 try_window (window, pos)
12115 Lisp_Object window;
12116 struct text_pos pos;
12117 {
12118 struct window *w = XWINDOW (window);
12119 struct it it;
12120 struct glyph_row *last_text_row = NULL;
12121
12122 /* Make POS the new window start. */
12123 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12124
12125 /* Mark cursor position as unknown. No overlay arrow seen. */
12126 w->cursor.vpos = -1;
12127 overlay_arrow_seen = 0;
12128
12129 /* Initialize iterator and info to start at POS. */
12130 start_display (&it, w, pos);
12131
12132 /* Display all lines of W. */
12133 while (it.current_y < it.last_visible_y)
12134 {
12135 if (display_line (&it))
12136 last_text_row = it.glyph_row - 1;
12137 if (fonts_changed_p)
12138 return 0;
12139 }
12140
12141 /* If bottom moved off end of frame, change mode line percentage. */
12142 if (XFASTINT (w->window_end_pos) <= 0
12143 && Z != IT_CHARPOS (it))
12144 w->update_mode_line = Qt;
12145
12146 /* Set window_end_pos to the offset of the last character displayed
12147 on the window from the end of current_buffer. Set
12148 window_end_vpos to its row number. */
12149 if (last_text_row)
12150 {
12151 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12152 w->window_end_bytepos
12153 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12154 w->window_end_pos
12155 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12156 w->window_end_vpos
12157 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12158 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12159 ->displays_text_p);
12160 }
12161 else
12162 {
12163 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12164 w->window_end_pos = make_number (Z - ZV);
12165 w->window_end_vpos = make_number (0);
12166 }
12167
12168 /* But that is not valid info until redisplay finishes. */
12169 w->window_end_valid = Qnil;
12170 return 1;
12171 }
12172
12173
12174 \f
12175 /************************************************************************
12176 Window redisplay reusing current matrix when buffer has not changed
12177 ************************************************************************/
12178
12179 /* Try redisplay of window W showing an unchanged buffer with a
12180 different window start than the last time it was displayed by
12181 reusing its current matrix. Value is non-zero if successful.
12182 W->start is the new window start. */
12183
12184 static int
12185 try_window_reusing_current_matrix (w)
12186 struct window *w;
12187 {
12188 struct frame *f = XFRAME (w->frame);
12189 struct glyph_row *row, *bottom_row;
12190 struct it it;
12191 struct run run;
12192 struct text_pos start, new_start;
12193 int nrows_scrolled, i;
12194 struct glyph_row *last_text_row;
12195 struct glyph_row *last_reused_text_row;
12196 struct glyph_row *start_row;
12197 int start_vpos, min_y, max_y;
12198
12199 #if GLYPH_DEBUG
12200 if (inhibit_try_window_reusing)
12201 return 0;
12202 #endif
12203
12204 if (/* This function doesn't handle terminal frames. */
12205 !FRAME_WINDOW_P (f)
12206 /* Don't try to reuse the display if windows have been split
12207 or such. */
12208 || windows_or_buffers_changed
12209 || cursor_type_changed)
12210 return 0;
12211
12212 /* Can't do this if region may have changed. */
12213 if ((!NILP (Vtransient_mark_mode)
12214 && !NILP (current_buffer->mark_active))
12215 || !NILP (w->region_showing)
12216 || !NILP (Vshow_trailing_whitespace))
12217 return 0;
12218
12219 /* If top-line visibility has changed, give up. */
12220 if (WINDOW_WANTS_HEADER_LINE_P (w)
12221 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12222 return 0;
12223
12224 /* Give up if old or new display is scrolled vertically. We could
12225 make this function handle this, but right now it doesn't. */
12226 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12227 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12228 return 0;
12229
12230 /* The variable new_start now holds the new window start. The old
12231 start `start' can be determined from the current matrix. */
12232 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12233 start = start_row->start.pos;
12234 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12235
12236 /* Clear the desired matrix for the display below. */
12237 clear_glyph_matrix (w->desired_matrix);
12238
12239 if (CHARPOS (new_start) <= CHARPOS (start))
12240 {
12241 int first_row_y;
12242
12243 /* Don't use this method if the display starts with an ellipsis
12244 displayed for invisible text. It's not easy to handle that case
12245 below, and it's certainly not worth the effort since this is
12246 not a frequent case. */
12247 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12248 return 0;
12249
12250 IF_DEBUG (debug_method_add (w, "twu1"));
12251
12252 /* Display up to a row that can be reused. The variable
12253 last_text_row is set to the last row displayed that displays
12254 text. Note that it.vpos == 0 if or if not there is a
12255 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12256 start_display (&it, w, new_start);
12257 first_row_y = it.current_y;
12258 w->cursor.vpos = -1;
12259 last_text_row = last_reused_text_row = NULL;
12260
12261 while (it.current_y < it.last_visible_y
12262 && IT_CHARPOS (it) < CHARPOS (start)
12263 && !fonts_changed_p)
12264 if (display_line (&it))
12265 last_text_row = it.glyph_row - 1;
12266
12267 /* A value of current_y < last_visible_y means that we stopped
12268 at the previous window start, which in turn means that we
12269 have at least one reusable row. */
12270 if (it.current_y < it.last_visible_y)
12271 {
12272 /* IT.vpos always starts from 0; it counts text lines. */
12273 nrows_scrolled = it.vpos;
12274
12275 /* Find PT if not already found in the lines displayed. */
12276 if (w->cursor.vpos < 0)
12277 {
12278 int dy = it.current_y - first_row_y;
12279
12280 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12281 row = row_containing_pos (w, PT, row, NULL, dy);
12282 if (row)
12283 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12284 dy, nrows_scrolled);
12285 else
12286 {
12287 clear_glyph_matrix (w->desired_matrix);
12288 return 0;
12289 }
12290 }
12291
12292 /* Scroll the display. Do it before the current matrix is
12293 changed. The problem here is that update has not yet
12294 run, i.e. part of the current matrix is not up to date.
12295 scroll_run_hook will clear the cursor, and use the
12296 current matrix to get the height of the row the cursor is
12297 in. */
12298 run.current_y = first_row_y;
12299 run.desired_y = it.current_y;
12300 run.height = it.last_visible_y - it.current_y;
12301
12302 if (run.height > 0 && run.current_y != run.desired_y)
12303 {
12304 update_begin (f);
12305 FRAME_RIF (f)->update_window_begin_hook (w);
12306 FRAME_RIF (f)->clear_window_mouse_face (w);
12307 FRAME_RIF (f)->scroll_run_hook (w, &run);
12308 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12309 update_end (f);
12310 }
12311
12312 /* Shift current matrix down by nrows_scrolled lines. */
12313 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12314 rotate_matrix (w->current_matrix,
12315 start_vpos,
12316 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12317 nrows_scrolled);
12318
12319 /* Disable lines that must be updated. */
12320 for (i = 0; i < it.vpos; ++i)
12321 (start_row + i)->enabled_p = 0;
12322
12323 /* Re-compute Y positions. */
12324 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12325 max_y = it.last_visible_y;
12326 for (row = start_row + nrows_scrolled;
12327 row < bottom_row;
12328 ++row)
12329 {
12330 row->y = it.current_y;
12331 row->visible_height = row->height;
12332
12333 if (row->y < min_y)
12334 row->visible_height -= min_y - row->y;
12335 if (row->y + row->height > max_y)
12336 row->visible_height -= row->y + row->height - max_y;
12337
12338 it.current_y += row->height;
12339
12340 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12341 last_reused_text_row = row;
12342 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12343 break;
12344 }
12345
12346 /* Disable lines in the current matrix which are now
12347 below the window. */
12348 for (++row; row < bottom_row; ++row)
12349 row->enabled_p = 0;
12350 }
12351
12352 /* Update window_end_pos etc.; last_reused_text_row is the last
12353 reused row from the current matrix containing text, if any.
12354 The value of last_text_row is the last displayed line
12355 containing text. */
12356 if (last_reused_text_row)
12357 {
12358 w->window_end_bytepos
12359 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12360 w->window_end_pos
12361 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12362 w->window_end_vpos
12363 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12364 w->current_matrix));
12365 }
12366 else if (last_text_row)
12367 {
12368 w->window_end_bytepos
12369 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12370 w->window_end_pos
12371 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12372 w->window_end_vpos
12373 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12374 }
12375 else
12376 {
12377 /* This window must be completely empty. */
12378 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12379 w->window_end_pos = make_number (Z - ZV);
12380 w->window_end_vpos = make_number (0);
12381 }
12382 w->window_end_valid = Qnil;
12383
12384 /* Update hint: don't try scrolling again in update_window. */
12385 w->desired_matrix->no_scrolling_p = 1;
12386
12387 #if GLYPH_DEBUG
12388 debug_method_add (w, "try_window_reusing_current_matrix 1");
12389 #endif
12390 return 1;
12391 }
12392 else if (CHARPOS (new_start) > CHARPOS (start))
12393 {
12394 struct glyph_row *pt_row, *row;
12395 struct glyph_row *first_reusable_row;
12396 struct glyph_row *first_row_to_display;
12397 int dy;
12398 int yb = window_text_bottom_y (w);
12399
12400 /* Find the row starting at new_start, if there is one. Don't
12401 reuse a partially visible line at the end. */
12402 first_reusable_row = start_row;
12403 while (first_reusable_row->enabled_p
12404 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12405 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12406 < CHARPOS (new_start)))
12407 ++first_reusable_row;
12408
12409 /* Give up if there is no row to reuse. */
12410 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12411 || !first_reusable_row->enabled_p
12412 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12413 != CHARPOS (new_start)))
12414 return 0;
12415
12416 /* We can reuse fully visible rows beginning with
12417 first_reusable_row to the end of the window. Set
12418 first_row_to_display to the first row that cannot be reused.
12419 Set pt_row to the row containing point, if there is any. */
12420 pt_row = NULL;
12421 for (first_row_to_display = first_reusable_row;
12422 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12423 ++first_row_to_display)
12424 {
12425 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12426 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12427 pt_row = first_row_to_display;
12428 }
12429
12430 /* Start displaying at the start of first_row_to_display. */
12431 xassert (first_row_to_display->y < yb);
12432 init_to_row_start (&it, w, first_row_to_display);
12433
12434 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12435 - start_vpos);
12436 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12437 - nrows_scrolled);
12438 it.current_y = (first_row_to_display->y - first_reusable_row->y
12439 + WINDOW_HEADER_LINE_HEIGHT (w));
12440
12441 /* Display lines beginning with first_row_to_display in the
12442 desired matrix. Set last_text_row to the last row displayed
12443 that displays text. */
12444 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12445 if (pt_row == NULL)
12446 w->cursor.vpos = -1;
12447 last_text_row = NULL;
12448 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12449 if (display_line (&it))
12450 last_text_row = it.glyph_row - 1;
12451
12452 /* Give up If point isn't in a row displayed or reused. */
12453 if (w->cursor.vpos < 0)
12454 {
12455 clear_glyph_matrix (w->desired_matrix);
12456 return 0;
12457 }
12458
12459 /* If point is in a reused row, adjust y and vpos of the cursor
12460 position. */
12461 if (pt_row)
12462 {
12463 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12464 w->current_matrix);
12465 w->cursor.y -= first_reusable_row->y;
12466 }
12467
12468 /* Scroll the display. */
12469 run.current_y = first_reusable_row->y;
12470 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12471 run.height = it.last_visible_y - run.current_y;
12472 dy = run.current_y - run.desired_y;
12473
12474 if (run.height)
12475 {
12476 struct frame *f = XFRAME (WINDOW_FRAME (w));
12477 update_begin (f);
12478 FRAME_RIF (f)->update_window_begin_hook (w);
12479 FRAME_RIF (f)->clear_window_mouse_face (w);
12480 FRAME_RIF (f)->scroll_run_hook (w, &run);
12481 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
12482 update_end (f);
12483 }
12484
12485 /* Adjust Y positions of reused rows. */
12486 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12487 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12488 max_y = it.last_visible_y;
12489 for (row = first_reusable_row; row < first_row_to_display; ++row)
12490 {
12491 row->y -= dy;
12492 row->visible_height = row->height;
12493 if (row->y < min_y)
12494 row->visible_height -= min_y - row->y;
12495 if (row->y + row->height > max_y)
12496 row->visible_height -= row->y + row->height - max_y;
12497 }
12498
12499 /* Scroll the current matrix. */
12500 xassert (nrows_scrolled > 0);
12501 rotate_matrix (w->current_matrix,
12502 start_vpos,
12503 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12504 -nrows_scrolled);
12505
12506 /* Disable rows not reused. */
12507 for (row -= nrows_scrolled; row < bottom_row; ++row)
12508 row->enabled_p = 0;
12509
12510 /* Adjust window end. A null value of last_text_row means that
12511 the window end is in reused rows which in turn means that
12512 only its vpos can have changed. */
12513 if (last_text_row)
12514 {
12515 w->window_end_bytepos
12516 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12517 w->window_end_pos
12518 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12519 w->window_end_vpos
12520 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12521 }
12522 else
12523 {
12524 w->window_end_vpos
12525 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12526 }
12527
12528 w->window_end_valid = Qnil;
12529 w->desired_matrix->no_scrolling_p = 1;
12530
12531 #if GLYPH_DEBUG
12532 debug_method_add (w, "try_window_reusing_current_matrix 2");
12533 #endif
12534 return 1;
12535 }
12536
12537 return 0;
12538 }
12539
12540
12541 \f
12542 /************************************************************************
12543 Window redisplay reusing current matrix when buffer has changed
12544 ************************************************************************/
12545
12546 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12547 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12548 int *, int *));
12549 static struct glyph_row *
12550 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12551 struct glyph_row *));
12552
12553
12554 /* Return the last row in MATRIX displaying text. If row START is
12555 non-null, start searching with that row. IT gives the dimensions
12556 of the display. Value is null if matrix is empty; otherwise it is
12557 a pointer to the row found. */
12558
12559 static struct glyph_row *
12560 find_last_row_displaying_text (matrix, it, start)
12561 struct glyph_matrix *matrix;
12562 struct it *it;
12563 struct glyph_row *start;
12564 {
12565 struct glyph_row *row, *row_found;
12566
12567 /* Set row_found to the last row in IT->w's current matrix
12568 displaying text. The loop looks funny but think of partially
12569 visible lines. */
12570 row_found = NULL;
12571 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12572 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12573 {
12574 xassert (row->enabled_p);
12575 row_found = row;
12576 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12577 break;
12578 ++row;
12579 }
12580
12581 return row_found;
12582 }
12583
12584
12585 /* Return the last row in the current matrix of W that is not affected
12586 by changes at the start of current_buffer that occurred since W's
12587 current matrix was built. Value is null if no such row exists.
12588
12589 BEG_UNCHANGED us the number of characters unchanged at the start of
12590 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12591 first changed character in current_buffer. Characters at positions <
12592 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12593 when the current matrix was built. */
12594
12595 static struct glyph_row *
12596 find_last_unchanged_at_beg_row (w)
12597 struct window *w;
12598 {
12599 int first_changed_pos = BEG + BEG_UNCHANGED;
12600 struct glyph_row *row;
12601 struct glyph_row *row_found = NULL;
12602 int yb = window_text_bottom_y (w);
12603
12604 /* Find the last row displaying unchanged text. */
12605 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12606 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12607 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12608 {
12609 if (/* If row ends before first_changed_pos, it is unchanged,
12610 except in some case. */
12611 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12612 /* When row ends in ZV and we write at ZV it is not
12613 unchanged. */
12614 && !row->ends_at_zv_p
12615 /* When first_changed_pos is the end of a continued line,
12616 row is not unchanged because it may be no longer
12617 continued. */
12618 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12619 && row->continued_p))
12620 row_found = row;
12621
12622 /* Stop if last visible row. */
12623 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12624 break;
12625
12626 ++row;
12627 }
12628
12629 return row_found;
12630 }
12631
12632
12633 /* Find the first glyph row in the current matrix of W that is not
12634 affected by changes at the end of current_buffer since the
12635 time W's current matrix was built.
12636
12637 Return in *DELTA the number of chars by which buffer positions in
12638 unchanged text at the end of current_buffer must be adjusted.
12639
12640 Return in *DELTA_BYTES the corresponding number of bytes.
12641
12642 Value is null if no such row exists, i.e. all rows are affected by
12643 changes. */
12644
12645 static struct glyph_row *
12646 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12647 struct window *w;
12648 int *delta, *delta_bytes;
12649 {
12650 struct glyph_row *row;
12651 struct glyph_row *row_found = NULL;
12652
12653 *delta = *delta_bytes = 0;
12654
12655 /* Display must not have been paused, otherwise the current matrix
12656 is not up to date. */
12657 if (NILP (w->window_end_valid))
12658 abort ();
12659
12660 /* A value of window_end_pos >= END_UNCHANGED means that the window
12661 end is in the range of changed text. If so, there is no
12662 unchanged row at the end of W's current matrix. */
12663 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12664 return NULL;
12665
12666 /* Set row to the last row in W's current matrix displaying text. */
12667 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12668
12669 /* If matrix is entirely empty, no unchanged row exists. */
12670 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12671 {
12672 /* The value of row is the last glyph row in the matrix having a
12673 meaningful buffer position in it. The end position of row
12674 corresponds to window_end_pos. This allows us to translate
12675 buffer positions in the current matrix to current buffer
12676 positions for characters not in changed text. */
12677 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12678 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12679 int last_unchanged_pos, last_unchanged_pos_old;
12680 struct glyph_row *first_text_row
12681 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12682
12683 *delta = Z - Z_old;
12684 *delta_bytes = Z_BYTE - Z_BYTE_old;
12685
12686 /* Set last_unchanged_pos to the buffer position of the last
12687 character in the buffer that has not been changed. Z is the
12688 index + 1 of the last character in current_buffer, i.e. by
12689 subtracting END_UNCHANGED we get the index of the last
12690 unchanged character, and we have to add BEG to get its buffer
12691 position. */
12692 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12693 last_unchanged_pos_old = last_unchanged_pos - *delta;
12694
12695 /* Search backward from ROW for a row displaying a line that
12696 starts at a minimum position >= last_unchanged_pos_old. */
12697 for (; row > first_text_row; --row)
12698 {
12699 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12700 abort ();
12701
12702 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12703 row_found = row;
12704 }
12705 }
12706
12707 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12708 abort ();
12709
12710 return row_found;
12711 }
12712
12713
12714 /* Make sure that glyph rows in the current matrix of window W
12715 reference the same glyph memory as corresponding rows in the
12716 frame's frame matrix. This function is called after scrolling W's
12717 current matrix on a terminal frame in try_window_id and
12718 try_window_reusing_current_matrix. */
12719
12720 static void
12721 sync_frame_with_window_matrix_rows (w)
12722 struct window *w;
12723 {
12724 struct frame *f = XFRAME (w->frame);
12725 struct glyph_row *window_row, *window_row_end, *frame_row;
12726
12727 /* Preconditions: W must be a leaf window and full-width. Its frame
12728 must have a frame matrix. */
12729 xassert (NILP (w->hchild) && NILP (w->vchild));
12730 xassert (WINDOW_FULL_WIDTH_P (w));
12731 xassert (!FRAME_WINDOW_P (f));
12732
12733 /* If W is a full-width window, glyph pointers in W's current matrix
12734 have, by definition, to be the same as glyph pointers in the
12735 corresponding frame matrix. Note that frame matrices have no
12736 marginal areas (see build_frame_matrix). */
12737 window_row = w->current_matrix->rows;
12738 window_row_end = window_row + w->current_matrix->nrows;
12739 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12740 while (window_row < window_row_end)
12741 {
12742 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12743 struct glyph *end = window_row->glyphs[LAST_AREA];
12744
12745 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12746 frame_row->glyphs[TEXT_AREA] = start;
12747 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12748 frame_row->glyphs[LAST_AREA] = end;
12749
12750 /* Disable frame rows whose corresponding window rows have
12751 been disabled in try_window_id. */
12752 if (!window_row->enabled_p)
12753 frame_row->enabled_p = 0;
12754
12755 ++window_row, ++frame_row;
12756 }
12757 }
12758
12759
12760 /* Find the glyph row in window W containing CHARPOS. Consider all
12761 rows between START and END (not inclusive). END null means search
12762 all rows to the end of the display area of W. Value is the row
12763 containing CHARPOS or null. */
12764
12765 struct glyph_row *
12766 row_containing_pos (w, charpos, start, end, dy)
12767 struct window *w;
12768 int charpos;
12769 struct glyph_row *start, *end;
12770 int dy;
12771 {
12772 struct glyph_row *row = start;
12773 int last_y;
12774
12775 /* If we happen to start on a header-line, skip that. */
12776 if (row->mode_line_p)
12777 ++row;
12778
12779 if ((end && row >= end) || !row->enabled_p)
12780 return NULL;
12781
12782 last_y = window_text_bottom_y (w) - dy;
12783
12784 while (1)
12785 {
12786 /* Give up if we have gone too far. */
12787 if (end && row >= end)
12788 return NULL;
12789 /* This formerly returned if they were equal.
12790 I think that both quantities are of a "last plus one" type;
12791 if so, when they are equal, the row is within the screen. -- rms. */
12792 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12793 return NULL;
12794
12795 /* If it is in this row, return this row. */
12796 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12797 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12798 /* The end position of a row equals the start
12799 position of the next row. If CHARPOS is there, we
12800 would rather display it in the next line, except
12801 when this line ends in ZV. */
12802 && !row->ends_at_zv_p
12803 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12804 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12805 return row;
12806 ++row;
12807 }
12808 }
12809
12810
12811 /* Try to redisplay window W by reusing its existing display. W's
12812 current matrix must be up to date when this function is called,
12813 i.e. window_end_valid must not be nil.
12814
12815 Value is
12816
12817 1 if display has been updated
12818 0 if otherwise unsuccessful
12819 -1 if redisplay with same window start is known not to succeed
12820
12821 The following steps are performed:
12822
12823 1. Find the last row in the current matrix of W that is not
12824 affected by changes at the start of current_buffer. If no such row
12825 is found, give up.
12826
12827 2. Find the first row in W's current matrix that is not affected by
12828 changes at the end of current_buffer. Maybe there is no such row.
12829
12830 3. Display lines beginning with the row + 1 found in step 1 to the
12831 row found in step 2 or, if step 2 didn't find a row, to the end of
12832 the window.
12833
12834 4. If cursor is not known to appear on the window, give up.
12835
12836 5. If display stopped at the row found in step 2, scroll the
12837 display and current matrix as needed.
12838
12839 6. Maybe display some lines at the end of W, if we must. This can
12840 happen under various circumstances, like a partially visible line
12841 becoming fully visible, or because newly displayed lines are displayed
12842 in smaller font sizes.
12843
12844 7. Update W's window end information. */
12845
12846 static int
12847 try_window_id (w)
12848 struct window *w;
12849 {
12850 struct frame *f = XFRAME (w->frame);
12851 struct glyph_matrix *current_matrix = w->current_matrix;
12852 struct glyph_matrix *desired_matrix = w->desired_matrix;
12853 struct glyph_row *last_unchanged_at_beg_row;
12854 struct glyph_row *first_unchanged_at_end_row;
12855 struct glyph_row *row;
12856 struct glyph_row *bottom_row;
12857 int bottom_vpos;
12858 struct it it;
12859 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12860 struct text_pos start_pos;
12861 struct run run;
12862 int first_unchanged_at_end_vpos = 0;
12863 struct glyph_row *last_text_row, *last_text_row_at_end;
12864 struct text_pos start;
12865 int first_changed_charpos, last_changed_charpos;
12866
12867 #if GLYPH_DEBUG
12868 if (inhibit_try_window_id)
12869 return 0;
12870 #endif
12871
12872 /* This is handy for debugging. */
12873 #if 0
12874 #define GIVE_UP(X) \
12875 do { \
12876 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12877 return 0; \
12878 } while (0)
12879 #else
12880 #define GIVE_UP(X) return 0
12881 #endif
12882
12883 SET_TEXT_POS_FROM_MARKER (start, w->start);
12884
12885 /* Don't use this for mini-windows because these can show
12886 messages and mini-buffers, and we don't handle that here. */
12887 if (MINI_WINDOW_P (w))
12888 GIVE_UP (1);
12889
12890 /* This flag is used to prevent redisplay optimizations. */
12891 if (windows_or_buffers_changed || cursor_type_changed)
12892 GIVE_UP (2);
12893
12894 /* Verify that narrowing has not changed.
12895 Also verify that we were not told to prevent redisplay optimizations.
12896 It would be nice to further
12897 reduce the number of cases where this prevents try_window_id. */
12898 if (current_buffer->clip_changed
12899 || current_buffer->prevent_redisplay_optimizations_p)
12900 GIVE_UP (3);
12901
12902 /* Window must either use window-based redisplay or be full width. */
12903 if (!FRAME_WINDOW_P (f)
12904 && (!FRAME_LINE_INS_DEL_OK (f)
12905 || !WINDOW_FULL_WIDTH_P (w)))
12906 GIVE_UP (4);
12907
12908 /* Give up if point is not known NOT to appear in W. */
12909 if (PT < CHARPOS (start))
12910 GIVE_UP (5);
12911
12912 /* Another way to prevent redisplay optimizations. */
12913 if (XFASTINT (w->last_modified) == 0)
12914 GIVE_UP (6);
12915
12916 /* Verify that window is not hscrolled. */
12917 if (XFASTINT (w->hscroll) != 0)
12918 GIVE_UP (7);
12919
12920 /* Verify that display wasn't paused. */
12921 if (NILP (w->window_end_valid))
12922 GIVE_UP (8);
12923
12924 /* Can't use this if highlighting a region because a cursor movement
12925 will do more than just set the cursor. */
12926 if (!NILP (Vtransient_mark_mode)
12927 && !NILP (current_buffer->mark_active))
12928 GIVE_UP (9);
12929
12930 /* Likewise if highlighting trailing whitespace. */
12931 if (!NILP (Vshow_trailing_whitespace))
12932 GIVE_UP (11);
12933
12934 /* Likewise if showing a region. */
12935 if (!NILP (w->region_showing))
12936 GIVE_UP (10);
12937
12938 /* Can use this if overlay arrow position and or string have changed. */
12939 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12940 || !EQ (last_arrow_string, Voverlay_arrow_string))
12941 GIVE_UP (12);
12942
12943
12944 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12945 only if buffer has really changed. The reason is that the gap is
12946 initially at Z for freshly visited files. The code below would
12947 set end_unchanged to 0 in that case. */
12948 if (MODIFF > SAVE_MODIFF
12949 /* This seems to happen sometimes after saving a buffer. */
12950 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12951 {
12952 if (GPT - BEG < BEG_UNCHANGED)
12953 BEG_UNCHANGED = GPT - BEG;
12954 if (Z - GPT < END_UNCHANGED)
12955 END_UNCHANGED = Z - GPT;
12956 }
12957
12958 /* The position of the first and last character that has been changed. */
12959 first_changed_charpos = BEG + BEG_UNCHANGED;
12960 last_changed_charpos = Z - END_UNCHANGED;
12961
12962 /* If window starts after a line end, and the last change is in
12963 front of that newline, then changes don't affect the display.
12964 This case happens with stealth-fontification. Note that although
12965 the display is unchanged, glyph positions in the matrix have to
12966 be adjusted, of course. */
12967 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12968 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12969 && ((last_changed_charpos < CHARPOS (start)
12970 && CHARPOS (start) == BEGV)
12971 || (last_changed_charpos < CHARPOS (start) - 1
12972 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12973 {
12974 int Z_old, delta, Z_BYTE_old, delta_bytes;
12975 struct glyph_row *r0;
12976
12977 /* Compute how many chars/bytes have been added to or removed
12978 from the buffer. */
12979 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12980 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12981 delta = Z - Z_old;
12982 delta_bytes = Z_BYTE - Z_BYTE_old;
12983
12984 /* Give up if PT is not in the window. Note that it already has
12985 been checked at the start of try_window_id that PT is not in
12986 front of the window start. */
12987 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12988 GIVE_UP (13);
12989
12990 /* If window start is unchanged, we can reuse the whole matrix
12991 as is, after adjusting glyph positions. No need to compute
12992 the window end again, since its offset from Z hasn't changed. */
12993 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12994 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12995 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
12996 /* PT must not be in a partially visible line. */
12997 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
12998 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
12999 {
13000 /* Adjust positions in the glyph matrix. */
13001 if (delta || delta_bytes)
13002 {
13003 struct glyph_row *r1
13004 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13005 increment_matrix_positions (w->current_matrix,
13006 MATRIX_ROW_VPOS (r0, current_matrix),
13007 MATRIX_ROW_VPOS (r1, current_matrix),
13008 delta, delta_bytes);
13009 }
13010
13011 /* Set the cursor. */
13012 row = row_containing_pos (w, PT, r0, NULL, 0);
13013 if (row)
13014 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13015 else
13016 abort ();
13017 return 1;
13018 }
13019 }
13020
13021 /* Handle the case that changes are all below what is displayed in
13022 the window, and that PT is in the window. This shortcut cannot
13023 be taken if ZV is visible in the window, and text has been added
13024 there that is visible in the window. */
13025 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13026 /* ZV is not visible in the window, or there are no
13027 changes at ZV, actually. */
13028 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13029 || first_changed_charpos == last_changed_charpos))
13030 {
13031 struct glyph_row *r0;
13032
13033 /* Give up if PT is not in the window. Note that it already has
13034 been checked at the start of try_window_id that PT is not in
13035 front of the window start. */
13036 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13037 GIVE_UP (14);
13038
13039 /* If window start is unchanged, we can reuse the whole matrix
13040 as is, without changing glyph positions since no text has
13041 been added/removed in front of the window end. */
13042 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13043 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13044 /* PT must not be in a partially visible line. */
13045 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13046 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13047 {
13048 /* We have to compute the window end anew since text
13049 can have been added/removed after it. */
13050 w->window_end_pos
13051 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13052 w->window_end_bytepos
13053 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13054
13055 /* Set the cursor. */
13056 row = row_containing_pos (w, PT, r0, NULL, 0);
13057 if (row)
13058 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13059 else
13060 abort ();
13061 return 2;
13062 }
13063 }
13064
13065 /* Give up if window start is in the changed area.
13066
13067 The condition used to read
13068
13069 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13070
13071 but why that was tested escapes me at the moment. */
13072 if (CHARPOS (start) >= first_changed_charpos
13073 && CHARPOS (start) <= last_changed_charpos)
13074 GIVE_UP (15);
13075
13076 /* Check that window start agrees with the start of the first glyph
13077 row in its current matrix. Check this after we know the window
13078 start is not in changed text, otherwise positions would not be
13079 comparable. */
13080 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13081 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13082 GIVE_UP (16);
13083
13084 /* Give up if the window ends in strings. Overlay strings
13085 at the end are difficult to handle, so don't try. */
13086 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13087 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13088 GIVE_UP (20);
13089
13090 /* Compute the position at which we have to start displaying new
13091 lines. Some of the lines at the top of the window might be
13092 reusable because they are not displaying changed text. Find the
13093 last row in W's current matrix not affected by changes at the
13094 start of current_buffer. Value is null if changes start in the
13095 first line of window. */
13096 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13097 if (last_unchanged_at_beg_row)
13098 {
13099 /* Avoid starting to display in the moddle of a character, a TAB
13100 for instance. This is easier than to set up the iterator
13101 exactly, and it's not a frequent case, so the additional
13102 effort wouldn't really pay off. */
13103 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13104 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13105 && last_unchanged_at_beg_row > w->current_matrix->rows)
13106 --last_unchanged_at_beg_row;
13107
13108 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13109 GIVE_UP (17);
13110
13111 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13112 GIVE_UP (18);
13113 start_pos = it.current.pos;
13114
13115 /* Start displaying new lines in the desired matrix at the same
13116 vpos we would use in the current matrix, i.e. below
13117 last_unchanged_at_beg_row. */
13118 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13119 current_matrix);
13120 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13121 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13122
13123 xassert (it.hpos == 0 && it.current_x == 0);
13124 }
13125 else
13126 {
13127 /* There are no reusable lines at the start of the window.
13128 Start displaying in the first text line. */
13129 start_display (&it, w, start);
13130 it.vpos = it.first_vpos;
13131 start_pos = it.current.pos;
13132 }
13133
13134 /* Find the first row that is not affected by changes at the end of
13135 the buffer. Value will be null if there is no unchanged row, in
13136 which case we must redisplay to the end of the window. delta
13137 will be set to the value by which buffer positions beginning with
13138 first_unchanged_at_end_row have to be adjusted due to text
13139 changes. */
13140 first_unchanged_at_end_row
13141 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13142 IF_DEBUG (debug_delta = delta);
13143 IF_DEBUG (debug_delta_bytes = delta_bytes);
13144
13145 /* Set stop_pos to the buffer position up to which we will have to
13146 display new lines. If first_unchanged_at_end_row != NULL, this
13147 is the buffer position of the start of the line displayed in that
13148 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13149 that we don't stop at a buffer position. */
13150 stop_pos = 0;
13151 if (first_unchanged_at_end_row)
13152 {
13153 xassert (last_unchanged_at_beg_row == NULL
13154 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13155
13156 /* If this is a continuation line, move forward to the next one
13157 that isn't. Changes in lines above affect this line.
13158 Caution: this may move first_unchanged_at_end_row to a row
13159 not displaying text. */
13160 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13161 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13162 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13163 < it.last_visible_y))
13164 ++first_unchanged_at_end_row;
13165
13166 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13167 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13168 >= it.last_visible_y))
13169 first_unchanged_at_end_row = NULL;
13170 else
13171 {
13172 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13173 + delta);
13174 first_unchanged_at_end_vpos
13175 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13176 xassert (stop_pos >= Z - END_UNCHANGED);
13177 }
13178 }
13179 else if (last_unchanged_at_beg_row == NULL)
13180 GIVE_UP (19);
13181
13182
13183 #if GLYPH_DEBUG
13184
13185 /* Either there is no unchanged row at the end, or the one we have
13186 now displays text. This is a necessary condition for the window
13187 end pos calculation at the end of this function. */
13188 xassert (first_unchanged_at_end_row == NULL
13189 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13190
13191 debug_last_unchanged_at_beg_vpos
13192 = (last_unchanged_at_beg_row
13193 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13194 : -1);
13195 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13196
13197 #endif /* GLYPH_DEBUG != 0 */
13198
13199
13200 /* Display new lines. Set last_text_row to the last new line
13201 displayed which has text on it, i.e. might end up as being the
13202 line where the window_end_vpos is. */
13203 w->cursor.vpos = -1;
13204 last_text_row = NULL;
13205 overlay_arrow_seen = 0;
13206 while (it.current_y < it.last_visible_y
13207 && !fonts_changed_p
13208 && (first_unchanged_at_end_row == NULL
13209 || IT_CHARPOS (it) < stop_pos))
13210 {
13211 if (display_line (&it))
13212 last_text_row = it.glyph_row - 1;
13213 }
13214
13215 if (fonts_changed_p)
13216 return -1;
13217
13218
13219 /* Compute differences in buffer positions, y-positions etc. for
13220 lines reused at the bottom of the window. Compute what we can
13221 scroll. */
13222 if (first_unchanged_at_end_row
13223 /* No lines reused because we displayed everything up to the
13224 bottom of the window. */
13225 && it.current_y < it.last_visible_y)
13226 {
13227 dvpos = (it.vpos
13228 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13229 current_matrix));
13230 dy = it.current_y - first_unchanged_at_end_row->y;
13231 run.current_y = first_unchanged_at_end_row->y;
13232 run.desired_y = run.current_y + dy;
13233 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13234 }
13235 else
13236 {
13237 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13238 first_unchanged_at_end_row = NULL;
13239 }
13240 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13241
13242
13243 /* Find the cursor if not already found. We have to decide whether
13244 PT will appear on this window (it sometimes doesn't, but this is
13245 not a very frequent case.) This decision has to be made before
13246 the current matrix is altered. A value of cursor.vpos < 0 means
13247 that PT is either in one of the lines beginning at
13248 first_unchanged_at_end_row or below the window. Don't care for
13249 lines that might be displayed later at the window end; as
13250 mentioned, this is not a frequent case. */
13251 if (w->cursor.vpos < 0)
13252 {
13253 /* Cursor in unchanged rows at the top? */
13254 if (PT < CHARPOS (start_pos)
13255 && last_unchanged_at_beg_row)
13256 {
13257 row = row_containing_pos (w, PT,
13258 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13259 last_unchanged_at_beg_row + 1, 0);
13260 if (row)
13261 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13262 }
13263
13264 /* Start from first_unchanged_at_end_row looking for PT. */
13265 else if (first_unchanged_at_end_row)
13266 {
13267 row = row_containing_pos (w, PT - delta,
13268 first_unchanged_at_end_row, NULL, 0);
13269 if (row)
13270 set_cursor_from_row (w, row, w->current_matrix, delta,
13271 delta_bytes, dy, dvpos);
13272 }
13273
13274 /* Give up if cursor was not found. */
13275 if (w->cursor.vpos < 0)
13276 {
13277 clear_glyph_matrix (w->desired_matrix);
13278 return -1;
13279 }
13280 }
13281
13282 /* Don't let the cursor end in the scroll margins. */
13283 {
13284 int this_scroll_margin, cursor_height;
13285
13286 this_scroll_margin = max (0, scroll_margin);
13287 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13288 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13289 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13290
13291 if ((w->cursor.y < this_scroll_margin
13292 && CHARPOS (start) > BEGV)
13293 /* Don't take scroll margin into account at the bottom because
13294 old redisplay didn't do it either. */
13295 || w->cursor.y + cursor_height > it.last_visible_y)
13296 {
13297 w->cursor.vpos = -1;
13298 clear_glyph_matrix (w->desired_matrix);
13299 return -1;
13300 }
13301 }
13302
13303 /* Scroll the display. Do it before changing the current matrix so
13304 that xterm.c doesn't get confused about where the cursor glyph is
13305 found. */
13306 if (dy && run.height)
13307 {
13308 update_begin (f);
13309
13310 if (FRAME_WINDOW_P (f))
13311 {
13312 FRAME_RIF (f)->update_window_begin_hook (w);
13313 FRAME_RIF (f)->clear_window_mouse_face (w);
13314 FRAME_RIF (f)->scroll_run_hook (w, &run);
13315 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13316 }
13317 else
13318 {
13319 /* Terminal frame. In this case, dvpos gives the number of
13320 lines to scroll by; dvpos < 0 means scroll up. */
13321 int first_unchanged_at_end_vpos
13322 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13323 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13324 int end = (WINDOW_TOP_EDGE_LINE (w)
13325 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13326 + window_internal_height (w));
13327
13328 /* Perform the operation on the screen. */
13329 if (dvpos > 0)
13330 {
13331 /* Scroll last_unchanged_at_beg_row to the end of the
13332 window down dvpos lines. */
13333 set_terminal_window (end);
13334
13335 /* On dumb terminals delete dvpos lines at the end
13336 before inserting dvpos empty lines. */
13337 if (!FRAME_SCROLL_REGION_OK (f))
13338 ins_del_lines (end - dvpos, -dvpos);
13339
13340 /* Insert dvpos empty lines in front of
13341 last_unchanged_at_beg_row. */
13342 ins_del_lines (from, dvpos);
13343 }
13344 else if (dvpos < 0)
13345 {
13346 /* Scroll up last_unchanged_at_beg_vpos to the end of
13347 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13348 set_terminal_window (end);
13349
13350 /* Delete dvpos lines in front of
13351 last_unchanged_at_beg_vpos. ins_del_lines will set
13352 the cursor to the given vpos and emit |dvpos| delete
13353 line sequences. */
13354 ins_del_lines (from + dvpos, dvpos);
13355
13356 /* On a dumb terminal insert dvpos empty lines at the
13357 end. */
13358 if (!FRAME_SCROLL_REGION_OK (f))
13359 ins_del_lines (end + dvpos, -dvpos);
13360 }
13361
13362 set_terminal_window (0);
13363 }
13364
13365 update_end (f);
13366 }
13367
13368 /* Shift reused rows of the current matrix to the right position.
13369 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13370 text. */
13371 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13372 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13373 if (dvpos < 0)
13374 {
13375 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13376 bottom_vpos, dvpos);
13377 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13378 bottom_vpos, 0);
13379 }
13380 else if (dvpos > 0)
13381 {
13382 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13383 bottom_vpos, dvpos);
13384 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13385 first_unchanged_at_end_vpos + dvpos, 0);
13386 }
13387
13388 /* For frame-based redisplay, make sure that current frame and window
13389 matrix are in sync with respect to glyph memory. */
13390 if (!FRAME_WINDOW_P (f))
13391 sync_frame_with_window_matrix_rows (w);
13392
13393 /* Adjust buffer positions in reused rows. */
13394 if (delta)
13395 increment_matrix_positions (current_matrix,
13396 first_unchanged_at_end_vpos + dvpos,
13397 bottom_vpos, delta, delta_bytes);
13398
13399 /* Adjust Y positions. */
13400 if (dy)
13401 shift_glyph_matrix (w, current_matrix,
13402 first_unchanged_at_end_vpos + dvpos,
13403 bottom_vpos, dy);
13404
13405 if (first_unchanged_at_end_row)
13406 first_unchanged_at_end_row += dvpos;
13407
13408 /* If scrolling up, there may be some lines to display at the end of
13409 the window. */
13410 last_text_row_at_end = NULL;
13411 if (dy < 0)
13412 {
13413 /* Scrolling up can leave for example a partially visible line
13414 at the end of the window to be redisplayed. */
13415 /* Set last_row to the glyph row in the current matrix where the
13416 window end line is found. It has been moved up or down in
13417 the matrix by dvpos. */
13418 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13419 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13420
13421 /* If last_row is the window end line, it should display text. */
13422 xassert (last_row->displays_text_p);
13423
13424 /* If window end line was partially visible before, begin
13425 displaying at that line. Otherwise begin displaying with the
13426 line following it. */
13427 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13428 {
13429 init_to_row_start (&it, w, last_row);
13430 it.vpos = last_vpos;
13431 it.current_y = last_row->y;
13432 }
13433 else
13434 {
13435 init_to_row_end (&it, w, last_row);
13436 it.vpos = 1 + last_vpos;
13437 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13438 ++last_row;
13439 }
13440
13441 /* We may start in a continuation line. If so, we have to
13442 get the right continuation_lines_width and current_x. */
13443 it.continuation_lines_width = last_row->continuation_lines_width;
13444 it.hpos = it.current_x = 0;
13445
13446 /* Display the rest of the lines at the window end. */
13447 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13448 while (it.current_y < it.last_visible_y
13449 && !fonts_changed_p)
13450 {
13451 /* Is it always sure that the display agrees with lines in
13452 the current matrix? I don't think so, so we mark rows
13453 displayed invalid in the current matrix by setting their
13454 enabled_p flag to zero. */
13455 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13456 if (display_line (&it))
13457 last_text_row_at_end = it.glyph_row - 1;
13458 }
13459 }
13460
13461 /* Update window_end_pos and window_end_vpos. */
13462 if (first_unchanged_at_end_row
13463 && first_unchanged_at_end_row->y < it.last_visible_y
13464 && !last_text_row_at_end)
13465 {
13466 /* Window end line if one of the preserved rows from the current
13467 matrix. Set row to the last row displaying text in current
13468 matrix starting at first_unchanged_at_end_row, after
13469 scrolling. */
13470 xassert (first_unchanged_at_end_row->displays_text_p);
13471 row = find_last_row_displaying_text (w->current_matrix, &it,
13472 first_unchanged_at_end_row);
13473 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13474
13475 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13476 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13477 w->window_end_vpos
13478 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13479 xassert (w->window_end_bytepos >= 0);
13480 IF_DEBUG (debug_method_add (w, "A"));
13481 }
13482 else if (last_text_row_at_end)
13483 {
13484 w->window_end_pos
13485 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13486 w->window_end_bytepos
13487 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13488 w->window_end_vpos
13489 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13490 xassert (w->window_end_bytepos >= 0);
13491 IF_DEBUG (debug_method_add (w, "B"));
13492 }
13493 else if (last_text_row)
13494 {
13495 /* We have displayed either to the end of the window or at the
13496 end of the window, i.e. the last row with text is to be found
13497 in the desired matrix. */
13498 w->window_end_pos
13499 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13500 w->window_end_bytepos
13501 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13502 w->window_end_vpos
13503 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13504 xassert (w->window_end_bytepos >= 0);
13505 }
13506 else if (first_unchanged_at_end_row == NULL
13507 && last_text_row == NULL
13508 && last_text_row_at_end == NULL)
13509 {
13510 /* Displayed to end of window, but no line containing text was
13511 displayed. Lines were deleted at the end of the window. */
13512 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13513 int vpos = XFASTINT (w->window_end_vpos);
13514 struct glyph_row *current_row = current_matrix->rows + vpos;
13515 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13516
13517 for (row = NULL;
13518 row == NULL && vpos >= first_vpos;
13519 --vpos, --current_row, --desired_row)
13520 {
13521 if (desired_row->enabled_p)
13522 {
13523 if (desired_row->displays_text_p)
13524 row = desired_row;
13525 }
13526 else if (current_row->displays_text_p)
13527 row = current_row;
13528 }
13529
13530 xassert (row != NULL);
13531 w->window_end_vpos = make_number (vpos + 1);
13532 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13533 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13534 xassert (w->window_end_bytepos >= 0);
13535 IF_DEBUG (debug_method_add (w, "C"));
13536 }
13537 else
13538 abort ();
13539
13540 #if 0 /* This leads to problems, for instance when the cursor is
13541 at ZV, and the cursor line displays no text. */
13542 /* Disable rows below what's displayed in the window. This makes
13543 debugging easier. */
13544 enable_glyph_matrix_rows (current_matrix,
13545 XFASTINT (w->window_end_vpos) + 1,
13546 bottom_vpos, 0);
13547 #endif
13548
13549 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13550 debug_end_vpos = XFASTINT (w->window_end_vpos));
13551
13552 /* Record that display has not been completed. */
13553 w->window_end_valid = Qnil;
13554 w->desired_matrix->no_scrolling_p = 1;
13555 return 3;
13556
13557 #undef GIVE_UP
13558 }
13559
13560
13561 \f
13562 /***********************************************************************
13563 More debugging support
13564 ***********************************************************************/
13565
13566 #if GLYPH_DEBUG
13567
13568 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13569 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13570 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13571
13572
13573 /* Dump the contents of glyph matrix MATRIX on stderr.
13574
13575 GLYPHS 0 means don't show glyph contents.
13576 GLYPHS 1 means show glyphs in short form
13577 GLYPHS > 1 means show glyphs in long form. */
13578
13579 void
13580 dump_glyph_matrix (matrix, glyphs)
13581 struct glyph_matrix *matrix;
13582 int glyphs;
13583 {
13584 int i;
13585 for (i = 0; i < matrix->nrows; ++i)
13586 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13587 }
13588
13589
13590 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13591 the glyph row and area where the glyph comes from. */
13592
13593 void
13594 dump_glyph (row, glyph, area)
13595 struct glyph_row *row;
13596 struct glyph *glyph;
13597 int area;
13598 {
13599 if (glyph->type == CHAR_GLYPH)
13600 {
13601 fprintf (stderr,
13602 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13603 glyph - row->glyphs[TEXT_AREA],
13604 'C',
13605 glyph->charpos,
13606 (BUFFERP (glyph->object)
13607 ? 'B'
13608 : (STRINGP (glyph->object)
13609 ? 'S'
13610 : '-')),
13611 glyph->pixel_width,
13612 glyph->u.ch,
13613 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13614 ? glyph->u.ch
13615 : '.'),
13616 glyph->face_id,
13617 glyph->left_box_line_p,
13618 glyph->right_box_line_p);
13619 }
13620 else if (glyph->type == STRETCH_GLYPH)
13621 {
13622 fprintf (stderr,
13623 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13624 glyph - row->glyphs[TEXT_AREA],
13625 'S',
13626 glyph->charpos,
13627 (BUFFERP (glyph->object)
13628 ? 'B'
13629 : (STRINGP (glyph->object)
13630 ? 'S'
13631 : '-')),
13632 glyph->pixel_width,
13633 0,
13634 '.',
13635 glyph->face_id,
13636 glyph->left_box_line_p,
13637 glyph->right_box_line_p);
13638 }
13639 else if (glyph->type == IMAGE_GLYPH)
13640 {
13641 fprintf (stderr,
13642 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13643 glyph - row->glyphs[TEXT_AREA],
13644 'I',
13645 glyph->charpos,
13646 (BUFFERP (glyph->object)
13647 ? 'B'
13648 : (STRINGP (glyph->object)
13649 ? 'S'
13650 : '-')),
13651 glyph->pixel_width,
13652 glyph->u.img_id,
13653 '.',
13654 glyph->face_id,
13655 glyph->left_box_line_p,
13656 glyph->right_box_line_p);
13657 }
13658 }
13659
13660
13661 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13662 GLYPHS 0 means don't show glyph contents.
13663 GLYPHS 1 means show glyphs in short form
13664 GLYPHS > 1 means show glyphs in long form. */
13665
13666 void
13667 dump_glyph_row (row, vpos, glyphs)
13668 struct glyph_row *row;
13669 int vpos, glyphs;
13670 {
13671 if (glyphs != 1)
13672 {
13673 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13674 fprintf (stderr, "=======================================================================\n");
13675
13676 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13677 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13678 vpos,
13679 MATRIX_ROW_START_CHARPOS (row),
13680 MATRIX_ROW_END_CHARPOS (row),
13681 row->used[TEXT_AREA],
13682 row->contains_overlapping_glyphs_p,
13683 row->enabled_p,
13684 row->truncated_on_left_p,
13685 row->truncated_on_right_p,
13686 row->overlay_arrow_p,
13687 row->continued_p,
13688 MATRIX_ROW_CONTINUATION_LINE_P (row),
13689 row->displays_text_p,
13690 row->ends_at_zv_p,
13691 row->fill_line_p,
13692 row->ends_in_middle_of_char_p,
13693 row->starts_in_middle_of_char_p,
13694 row->mouse_face_p,
13695 row->x,
13696 row->y,
13697 row->pixel_width,
13698 row->height,
13699 row->visible_height,
13700 row->ascent,
13701 row->phys_ascent);
13702 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13703 row->end.overlay_string_index,
13704 row->continuation_lines_width);
13705 fprintf (stderr, "%9d %5d\n",
13706 CHARPOS (row->start.string_pos),
13707 CHARPOS (row->end.string_pos));
13708 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13709 row->end.dpvec_index);
13710 }
13711
13712 if (glyphs > 1)
13713 {
13714 int area;
13715
13716 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13717 {
13718 struct glyph *glyph = row->glyphs[area];
13719 struct glyph *glyph_end = glyph + row->used[area];
13720
13721 /* Glyph for a line end in text. */
13722 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13723 ++glyph_end;
13724
13725 if (glyph < glyph_end)
13726 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13727
13728 for (; glyph < glyph_end; ++glyph)
13729 dump_glyph (row, glyph, area);
13730 }
13731 }
13732 else if (glyphs == 1)
13733 {
13734 int area;
13735
13736 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13737 {
13738 char *s = (char *) alloca (row->used[area] + 1);
13739 int i;
13740
13741 for (i = 0; i < row->used[area]; ++i)
13742 {
13743 struct glyph *glyph = row->glyphs[area] + i;
13744 if (glyph->type == CHAR_GLYPH
13745 && glyph->u.ch < 0x80
13746 && glyph->u.ch >= ' ')
13747 s[i] = glyph->u.ch;
13748 else
13749 s[i] = '.';
13750 }
13751
13752 s[i] = '\0';
13753 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13754 }
13755 }
13756 }
13757
13758
13759 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13760 Sdump_glyph_matrix, 0, 1, "p",
13761 doc: /* Dump the current matrix of the selected window to stderr.
13762 Shows contents of glyph row structures. With non-nil
13763 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13764 glyphs in short form, otherwise show glyphs in long form. */)
13765 (glyphs)
13766 Lisp_Object glyphs;
13767 {
13768 struct window *w = XWINDOW (selected_window);
13769 struct buffer *buffer = XBUFFER (w->buffer);
13770
13771 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13772 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13773 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13774 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13775 fprintf (stderr, "=============================================\n");
13776 dump_glyph_matrix (w->current_matrix,
13777 NILP (glyphs) ? 0 : XINT (glyphs));
13778 return Qnil;
13779 }
13780
13781
13782 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13783 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13784 ()
13785 {
13786 struct frame *f = XFRAME (selected_frame);
13787 dump_glyph_matrix (f->current_matrix, 1);
13788 return Qnil;
13789 }
13790
13791
13792 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13793 doc: /* Dump glyph row ROW to stderr.
13794 GLYPH 0 means don't dump glyphs.
13795 GLYPH 1 means dump glyphs in short form.
13796 GLYPH > 1 or omitted means dump glyphs in long form. */)
13797 (row, glyphs)
13798 Lisp_Object row, glyphs;
13799 {
13800 struct glyph_matrix *matrix;
13801 int vpos;
13802
13803 CHECK_NUMBER (row);
13804 matrix = XWINDOW (selected_window)->current_matrix;
13805 vpos = XINT (row);
13806 if (vpos >= 0 && vpos < matrix->nrows)
13807 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13808 vpos,
13809 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13810 return Qnil;
13811 }
13812
13813
13814 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13815 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13816 GLYPH 0 means don't dump glyphs.
13817 GLYPH 1 means dump glyphs in short form.
13818 GLYPH > 1 or omitted means dump glyphs in long form. */)
13819 (row, glyphs)
13820 Lisp_Object row, glyphs;
13821 {
13822 struct frame *sf = SELECTED_FRAME ();
13823 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13824 int vpos;
13825
13826 CHECK_NUMBER (row);
13827 vpos = XINT (row);
13828 if (vpos >= 0 && vpos < m->nrows)
13829 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13830 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13831 return Qnil;
13832 }
13833
13834
13835 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13836 doc: /* Toggle tracing of redisplay.
13837 With ARG, turn tracing on if and only if ARG is positive. */)
13838 (arg)
13839 Lisp_Object arg;
13840 {
13841 if (NILP (arg))
13842 trace_redisplay_p = !trace_redisplay_p;
13843 else
13844 {
13845 arg = Fprefix_numeric_value (arg);
13846 trace_redisplay_p = XINT (arg) > 0;
13847 }
13848
13849 return Qnil;
13850 }
13851
13852
13853 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13854 doc: /* Like `format', but print result to stderr.
13855 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13856 (nargs, args)
13857 int nargs;
13858 Lisp_Object *args;
13859 {
13860 Lisp_Object s = Fformat (nargs, args);
13861 fprintf (stderr, "%s", SDATA (s));
13862 return Qnil;
13863 }
13864
13865 #endif /* GLYPH_DEBUG */
13866
13867
13868 \f
13869 /***********************************************************************
13870 Building Desired Matrix Rows
13871 ***********************************************************************/
13872
13873 /* Return a temporary glyph row holding the glyphs of an overlay
13874 arrow. Only used for non-window-redisplay windows. */
13875
13876 static struct glyph_row *
13877 get_overlay_arrow_glyph_row (w)
13878 struct window *w;
13879 {
13880 struct frame *f = XFRAME (WINDOW_FRAME (w));
13881 struct buffer *buffer = XBUFFER (w->buffer);
13882 struct buffer *old = current_buffer;
13883 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13884 int arrow_len = SCHARS (Voverlay_arrow_string);
13885 const unsigned char *arrow_end = arrow_string + arrow_len;
13886 const unsigned char *p;
13887 struct it it;
13888 int multibyte_p;
13889 int n_glyphs_before;
13890
13891 set_buffer_temp (buffer);
13892 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13893 it.glyph_row->used[TEXT_AREA] = 0;
13894 SET_TEXT_POS (it.position, 0, 0);
13895
13896 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13897 p = arrow_string;
13898 while (p < arrow_end)
13899 {
13900 Lisp_Object face, ilisp;
13901
13902 /* Get the next character. */
13903 if (multibyte_p)
13904 it.c = string_char_and_length (p, arrow_len, &it.len);
13905 else
13906 it.c = *p, it.len = 1;
13907 p += it.len;
13908
13909 /* Get its face. */
13910 ilisp = make_number (p - arrow_string);
13911 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13912 it.face_id = compute_char_face (f, it.c, face);
13913
13914 /* Compute its width, get its glyphs. */
13915 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13916 SET_TEXT_POS (it.position, -1, -1);
13917 PRODUCE_GLYPHS (&it);
13918
13919 /* If this character doesn't fit any more in the line, we have
13920 to remove some glyphs. */
13921 if (it.current_x > it.last_visible_x)
13922 {
13923 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13924 break;
13925 }
13926 }
13927
13928 set_buffer_temp (old);
13929 return it.glyph_row;
13930 }
13931
13932
13933 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13934 glyphs are only inserted for terminal frames since we can't really
13935 win with truncation glyphs when partially visible glyphs are
13936 involved. Which glyphs to insert is determined by
13937 produce_special_glyphs. */
13938
13939 static void
13940 insert_left_trunc_glyphs (it)
13941 struct it *it;
13942 {
13943 struct it truncate_it;
13944 struct glyph *from, *end, *to, *toend;
13945
13946 xassert (!FRAME_WINDOW_P (it->f));
13947
13948 /* Get the truncation glyphs. */
13949 truncate_it = *it;
13950 truncate_it.current_x = 0;
13951 truncate_it.face_id = DEFAULT_FACE_ID;
13952 truncate_it.glyph_row = &scratch_glyph_row;
13953 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13954 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13955 truncate_it.object = make_number (0);
13956 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13957
13958 /* Overwrite glyphs from IT with truncation glyphs. */
13959 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13960 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13961 to = it->glyph_row->glyphs[TEXT_AREA];
13962 toend = to + it->glyph_row->used[TEXT_AREA];
13963
13964 while (from < end)
13965 *to++ = *from++;
13966
13967 /* There may be padding glyphs left over. Overwrite them too. */
13968 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13969 {
13970 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13971 while (from < end)
13972 *to++ = *from++;
13973 }
13974
13975 if (to > toend)
13976 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13977 }
13978
13979
13980 /* Compute the pixel height and width of IT->glyph_row.
13981
13982 Most of the time, ascent and height of a display line will be equal
13983 to the max_ascent and max_height values of the display iterator
13984 structure. This is not the case if
13985
13986 1. We hit ZV without displaying anything. In this case, max_ascent
13987 and max_height will be zero.
13988
13989 2. We have some glyphs that don't contribute to the line height.
13990 (The glyph row flag contributes_to_line_height_p is for future
13991 pixmap extensions).
13992
13993 The first case is easily covered by using default values because in
13994 these cases, the line height does not really matter, except that it
13995 must not be zero. */
13996
13997 static void
13998 compute_line_metrics (it)
13999 struct it *it;
14000 {
14001 struct glyph_row *row = it->glyph_row;
14002 int area, i;
14003
14004 if (FRAME_WINDOW_P (it->f))
14005 {
14006 int i, min_y, max_y;
14007
14008 /* The line may consist of one space only, that was added to
14009 place the cursor on it. If so, the row's height hasn't been
14010 computed yet. */
14011 if (row->height == 0)
14012 {
14013 if (it->max_ascent + it->max_descent == 0)
14014 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14015 row->ascent = it->max_ascent;
14016 row->height = it->max_ascent + it->max_descent;
14017 row->phys_ascent = it->max_phys_ascent;
14018 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14019 }
14020
14021 /* Compute the width of this line. */
14022 row->pixel_width = row->x;
14023 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14024 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14025
14026 xassert (row->pixel_width >= 0);
14027 xassert (row->ascent >= 0 && row->height > 0);
14028
14029 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14030 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14031
14032 /* If first line's physical ascent is larger than its logical
14033 ascent, use the physical ascent, and make the row taller.
14034 This makes accented characters fully visible. */
14035 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14036 && row->phys_ascent > row->ascent)
14037 {
14038 row->height += row->phys_ascent - row->ascent;
14039 row->ascent = row->phys_ascent;
14040 }
14041
14042 /* Compute how much of the line is visible. */
14043 row->visible_height = row->height;
14044
14045 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14046 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14047
14048 if (row->y < min_y)
14049 row->visible_height -= min_y - row->y;
14050 if (row->y + row->height > max_y)
14051 row->visible_height -= row->y + row->height - max_y;
14052 }
14053 else
14054 {
14055 row->pixel_width = row->used[TEXT_AREA];
14056 if (row->continued_p)
14057 row->pixel_width -= it->continuation_pixel_width;
14058 else if (row->truncated_on_right_p)
14059 row->pixel_width -= it->truncation_pixel_width;
14060 row->ascent = row->phys_ascent = 0;
14061 row->height = row->phys_height = row->visible_height = 1;
14062 }
14063
14064 /* Compute a hash code for this row. */
14065 row->hash = 0;
14066 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14067 for (i = 0; i < row->used[area]; ++i)
14068 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14069 + row->glyphs[area][i].u.val
14070 + row->glyphs[area][i].face_id
14071 + row->glyphs[area][i].padding_p
14072 + (row->glyphs[area][i].type << 2));
14073
14074 it->max_ascent = it->max_descent = 0;
14075 it->max_phys_ascent = it->max_phys_descent = 0;
14076 }
14077
14078
14079 /* Append one space to the glyph row of iterator IT if doing a
14080 window-based redisplay. DEFAULT_FACE_P non-zero means let the
14081 space have the default face, otherwise let it have the same face as
14082 IT->face_id. Value is non-zero if a space was added.
14083
14084 This function is called to make sure that there is always one glyph
14085 at the end of a glyph row that the cursor can be set on under
14086 window-systems. (If there weren't such a glyph we would not know
14087 how wide and tall a box cursor should be displayed).
14088
14089 At the same time this space let's a nicely handle clearing to the
14090 end of the line if the row ends in italic text. */
14091
14092 static int
14093 append_space (it, default_face_p)
14094 struct it *it;
14095 int default_face_p;
14096 {
14097 if (FRAME_WINDOW_P (it->f))
14098 {
14099 int n = it->glyph_row->used[TEXT_AREA];
14100
14101 if (it->glyph_row->glyphs[TEXT_AREA] + n
14102 < it->glyph_row->glyphs[1 + TEXT_AREA])
14103 {
14104 /* Save some values that must not be changed.
14105 Must save IT->c and IT->len because otherwise
14106 ITERATOR_AT_END_P wouldn't work anymore after
14107 append_space has been called. */
14108 enum display_element_type saved_what = it->what;
14109 int saved_c = it->c, saved_len = it->len;
14110 int saved_x = it->current_x;
14111 int saved_face_id = it->face_id;
14112 struct text_pos saved_pos;
14113 Lisp_Object saved_object;
14114 struct face *face;
14115
14116 saved_object = it->object;
14117 saved_pos = it->position;
14118
14119 it->what = IT_CHARACTER;
14120 bzero (&it->position, sizeof it->position);
14121 it->object = make_number (0);
14122 it->c = ' ';
14123 it->len = 1;
14124
14125 if (default_face_p)
14126 it->face_id = DEFAULT_FACE_ID;
14127 else if (it->face_before_selective_p)
14128 it->face_id = it->saved_face_id;
14129 face = FACE_FROM_ID (it->f, it->face_id);
14130 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14131
14132 PRODUCE_GLYPHS (it);
14133
14134 it->current_x = saved_x;
14135 it->object = saved_object;
14136 it->position = saved_pos;
14137 it->what = saved_what;
14138 it->face_id = saved_face_id;
14139 it->len = saved_len;
14140 it->c = saved_c;
14141 return 1;
14142 }
14143 }
14144
14145 return 0;
14146 }
14147
14148
14149 /* Extend the face of the last glyph in the text area of IT->glyph_row
14150 to the end of the display line. Called from display_line.
14151 If the glyph row is empty, add a space glyph to it so that we
14152 know the face to draw. Set the glyph row flag fill_line_p. */
14153
14154 static void
14155 extend_face_to_end_of_line (it)
14156 struct it *it;
14157 {
14158 struct face *face;
14159 struct frame *f = it->f;
14160
14161 /* If line is already filled, do nothing. */
14162 if (it->current_x >= it->last_visible_x)
14163 return;
14164
14165 /* Face extension extends the background and box of IT->face_id
14166 to the end of the line. If the background equals the background
14167 of the frame, we don't have to do anything. */
14168 if (it->face_before_selective_p)
14169 face = FACE_FROM_ID (it->f, it->saved_face_id);
14170 else
14171 face = FACE_FROM_ID (f, it->face_id);
14172
14173 if (FRAME_WINDOW_P (f)
14174 && face->box == FACE_NO_BOX
14175 && face->background == FRAME_BACKGROUND_PIXEL (f)
14176 && !face->stipple)
14177 return;
14178
14179 /* Set the glyph row flag indicating that the face of the last glyph
14180 in the text area has to be drawn to the end of the text area. */
14181 it->glyph_row->fill_line_p = 1;
14182
14183 /* If current character of IT is not ASCII, make sure we have the
14184 ASCII face. This will be automatically undone the next time
14185 get_next_display_element returns a multibyte character. Note
14186 that the character will always be single byte in unibyte text. */
14187 if (!SINGLE_BYTE_CHAR_P (it->c))
14188 {
14189 it->face_id = FACE_FOR_CHAR (f, face, 0);
14190 }
14191
14192 if (FRAME_WINDOW_P (f))
14193 {
14194 /* If the row is empty, add a space with the current face of IT,
14195 so that we know which face to draw. */
14196 if (it->glyph_row->used[TEXT_AREA] == 0)
14197 {
14198 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14199 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14200 it->glyph_row->used[TEXT_AREA] = 1;
14201 }
14202 }
14203 else
14204 {
14205 /* Save some values that must not be changed. */
14206 int saved_x = it->current_x;
14207 struct text_pos saved_pos;
14208 Lisp_Object saved_object;
14209 enum display_element_type saved_what = it->what;
14210 int saved_face_id = it->face_id;
14211
14212 saved_object = it->object;
14213 saved_pos = it->position;
14214
14215 it->what = IT_CHARACTER;
14216 bzero (&it->position, sizeof it->position);
14217 it->object = make_number (0);
14218 it->c = ' ';
14219 it->len = 1;
14220 it->face_id = face->id;
14221
14222 PRODUCE_GLYPHS (it);
14223
14224 while (it->current_x <= it->last_visible_x)
14225 PRODUCE_GLYPHS (it);
14226
14227 /* Don't count these blanks really. It would let us insert a left
14228 truncation glyph below and make us set the cursor on them, maybe. */
14229 it->current_x = saved_x;
14230 it->object = saved_object;
14231 it->position = saved_pos;
14232 it->what = saved_what;
14233 it->face_id = saved_face_id;
14234 }
14235 }
14236
14237
14238 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14239 trailing whitespace. */
14240
14241 static int
14242 trailing_whitespace_p (charpos)
14243 int charpos;
14244 {
14245 int bytepos = CHAR_TO_BYTE (charpos);
14246 int c = 0;
14247
14248 while (bytepos < ZV_BYTE
14249 && (c = FETCH_CHAR (bytepos),
14250 c == ' ' || c == '\t'))
14251 ++bytepos;
14252
14253 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14254 {
14255 if (bytepos != PT_BYTE)
14256 return 1;
14257 }
14258 return 0;
14259 }
14260
14261
14262 /* Highlight trailing whitespace, if any, in ROW. */
14263
14264 void
14265 highlight_trailing_whitespace (f, row)
14266 struct frame *f;
14267 struct glyph_row *row;
14268 {
14269 int used = row->used[TEXT_AREA];
14270
14271 if (used)
14272 {
14273 struct glyph *start = row->glyphs[TEXT_AREA];
14274 struct glyph *glyph = start + used - 1;
14275
14276 /* Skip over glyphs inserted to display the cursor at the
14277 end of a line, for extending the face of the last glyph
14278 to the end of the line on terminals, and for truncation
14279 and continuation glyphs. */
14280 while (glyph >= start
14281 && glyph->type == CHAR_GLYPH
14282 && INTEGERP (glyph->object))
14283 --glyph;
14284
14285 /* If last glyph is a space or stretch, and it's trailing
14286 whitespace, set the face of all trailing whitespace glyphs in
14287 IT->glyph_row to `trailing-whitespace'. */
14288 if (glyph >= start
14289 && BUFFERP (glyph->object)
14290 && (glyph->type == STRETCH_GLYPH
14291 || (glyph->type == CHAR_GLYPH
14292 && glyph->u.ch == ' '))
14293 && trailing_whitespace_p (glyph->charpos))
14294 {
14295 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14296
14297 while (glyph >= start
14298 && BUFFERP (glyph->object)
14299 && (glyph->type == STRETCH_GLYPH
14300 || (glyph->type == CHAR_GLYPH
14301 && glyph->u.ch == ' ')))
14302 (glyph--)->face_id = face_id;
14303 }
14304 }
14305 }
14306
14307
14308 /* Value is non-zero if glyph row ROW in window W should be
14309 used to hold the cursor. */
14310
14311 static int
14312 cursor_row_p (w, row)
14313 struct window *w;
14314 struct glyph_row *row;
14315 {
14316 int cursor_row_p = 1;
14317
14318 if (PT == MATRIX_ROW_END_CHARPOS (row))
14319 {
14320 /* If the row ends with a newline from a string, we don't want
14321 the cursor there (if the row is continued it doesn't end in a
14322 newline). */
14323 if (CHARPOS (row->end.string_pos) >= 0
14324 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14325 cursor_row_p = row->continued_p;
14326
14327 /* If the row ends at ZV, display the cursor at the end of that
14328 row instead of at the start of the row below. */
14329 else if (row->ends_at_zv_p)
14330 cursor_row_p = 1;
14331 else
14332 cursor_row_p = 0;
14333 }
14334
14335 return cursor_row_p;
14336 }
14337
14338
14339 /* Construct the glyph row IT->glyph_row in the desired matrix of
14340 IT->w from text at the current position of IT. See dispextern.h
14341 for an overview of struct it. Value is non-zero if
14342 IT->glyph_row displays text, as opposed to a line displaying ZV
14343 only. */
14344
14345 static int
14346 display_line (it)
14347 struct it *it;
14348 {
14349 struct glyph_row *row = it->glyph_row;
14350
14351 /* We always start displaying at hpos zero even if hscrolled. */
14352 xassert (it->hpos == 0 && it->current_x == 0);
14353
14354 /* We must not display in a row that's not a text row. */
14355 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14356 < it->w->desired_matrix->nrows);
14357
14358 /* Is IT->w showing the region? */
14359 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14360
14361 /* Clear the result glyph row and enable it. */
14362 prepare_desired_row (row);
14363
14364 row->y = it->current_y;
14365 row->start = it->start;
14366 row->continuation_lines_width = it->continuation_lines_width;
14367 row->displays_text_p = 1;
14368 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14369 it->starts_in_middle_of_char_p = 0;
14370
14371 /* Arrange the overlays nicely for our purposes. Usually, we call
14372 display_line on only one line at a time, in which case this
14373 can't really hurt too much, or we call it on lines which appear
14374 one after another in the buffer, in which case all calls to
14375 recenter_overlay_lists but the first will be pretty cheap. */
14376 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14377
14378 /* Move over display elements that are not visible because we are
14379 hscrolled. This may stop at an x-position < IT->first_visible_x
14380 if the first glyph is partially visible or if we hit a line end. */
14381 if (it->current_x < it->first_visible_x)
14382 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14383 MOVE_TO_POS | MOVE_TO_X);
14384
14385 /* Get the initial row height. This is either the height of the
14386 text hscrolled, if there is any, or zero. */
14387 row->ascent = it->max_ascent;
14388 row->height = it->max_ascent + it->max_descent;
14389 row->phys_ascent = it->max_phys_ascent;
14390 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14391
14392 /* Loop generating characters. The loop is left with IT on the next
14393 character to display. */
14394 while (1)
14395 {
14396 int n_glyphs_before, hpos_before, x_before;
14397 int x, i, nglyphs;
14398 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14399
14400 /* Retrieve the next thing to display. Value is zero if end of
14401 buffer reached. */
14402 if (!get_next_display_element (it))
14403 {
14404 /* Maybe add a space at the end of this line that is used to
14405 display the cursor there under X. Set the charpos of the
14406 first glyph of blank lines not corresponding to any text
14407 to -1. */
14408 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14409 || row->used[TEXT_AREA] == 0)
14410 {
14411 row->glyphs[TEXT_AREA]->charpos = -1;
14412 row->displays_text_p = 0;
14413
14414 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14415 && (!MINI_WINDOW_P (it->w)
14416 || (minibuf_level && EQ (it->window, minibuf_window))))
14417 row->indicate_empty_line_p = 1;
14418 }
14419
14420 it->continuation_lines_width = 0;
14421 row->ends_at_zv_p = 1;
14422 break;
14423 }
14424
14425 /* Now, get the metrics of what we want to display. This also
14426 generates glyphs in `row' (which is IT->glyph_row). */
14427 n_glyphs_before = row->used[TEXT_AREA];
14428 x = it->current_x;
14429
14430 /* Remember the line height so far in case the next element doesn't
14431 fit on the line. */
14432 if (!it->truncate_lines_p)
14433 {
14434 ascent = it->max_ascent;
14435 descent = it->max_descent;
14436 phys_ascent = it->max_phys_ascent;
14437 phys_descent = it->max_phys_descent;
14438 }
14439
14440 PRODUCE_GLYPHS (it);
14441
14442 /* If this display element was in marginal areas, continue with
14443 the next one. */
14444 if (it->area != TEXT_AREA)
14445 {
14446 row->ascent = max (row->ascent, it->max_ascent);
14447 row->height = max (row->height, it->max_ascent + it->max_descent);
14448 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14449 row->phys_height = max (row->phys_height,
14450 it->max_phys_ascent + it->max_phys_descent);
14451 set_iterator_to_next (it, 1);
14452 continue;
14453 }
14454
14455 /* Does the display element fit on the line? If we truncate
14456 lines, we should draw past the right edge of the window. If
14457 we don't truncate, we want to stop so that we can display the
14458 continuation glyph before the right margin. If lines are
14459 continued, there are two possible strategies for characters
14460 resulting in more than 1 glyph (e.g. tabs): Display as many
14461 glyphs as possible in this line and leave the rest for the
14462 continuation line, or display the whole element in the next
14463 line. Original redisplay did the former, so we do it also. */
14464 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14465 hpos_before = it->hpos;
14466 x_before = x;
14467
14468 if (/* Not a newline. */
14469 nglyphs > 0
14470 /* Glyphs produced fit entirely in the line. */
14471 && it->current_x < it->last_visible_x)
14472 {
14473 it->hpos += nglyphs;
14474 row->ascent = max (row->ascent, it->max_ascent);
14475 row->height = max (row->height, it->max_ascent + it->max_descent);
14476 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14477 row->phys_height = max (row->phys_height,
14478 it->max_phys_ascent + it->max_phys_descent);
14479 if (it->current_x - it->pixel_width < it->first_visible_x)
14480 row->x = x - it->first_visible_x;
14481 }
14482 else
14483 {
14484 int new_x;
14485 struct glyph *glyph;
14486
14487 for (i = 0; i < nglyphs; ++i, x = new_x)
14488 {
14489 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14490 new_x = x + glyph->pixel_width;
14491
14492 if (/* Lines are continued. */
14493 !it->truncate_lines_p
14494 && (/* Glyph doesn't fit on the line. */
14495 new_x > it->last_visible_x
14496 /* Or it fits exactly on a window system frame. */
14497 || (new_x == it->last_visible_x
14498 && FRAME_WINDOW_P (it->f))))
14499 {
14500 /* End of a continued line. */
14501
14502 if (it->hpos == 0
14503 || (new_x == it->last_visible_x
14504 && FRAME_WINDOW_P (it->f)))
14505 {
14506 /* Current glyph is the only one on the line or
14507 fits exactly on the line. We must continue
14508 the line because we can't draw the cursor
14509 after the glyph. */
14510 row->continued_p = 1;
14511 it->current_x = new_x;
14512 it->continuation_lines_width += new_x;
14513 ++it->hpos;
14514 if (i == nglyphs - 1)
14515 set_iterator_to_next (it, 1);
14516 }
14517 else if (CHAR_GLYPH_PADDING_P (*glyph)
14518 && !FRAME_WINDOW_P (it->f))
14519 {
14520 /* A padding glyph that doesn't fit on this line.
14521 This means the whole character doesn't fit
14522 on the line. */
14523 row->used[TEXT_AREA] = n_glyphs_before;
14524
14525 /* Fill the rest of the row with continuation
14526 glyphs like in 20.x. */
14527 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14528 < row->glyphs[1 + TEXT_AREA])
14529 produce_special_glyphs (it, IT_CONTINUATION);
14530
14531 row->continued_p = 1;
14532 it->current_x = x_before;
14533 it->continuation_lines_width += x_before;
14534
14535 /* Restore the height to what it was before the
14536 element not fitting on the line. */
14537 it->max_ascent = ascent;
14538 it->max_descent = descent;
14539 it->max_phys_ascent = phys_ascent;
14540 it->max_phys_descent = phys_descent;
14541 }
14542 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14543 {
14544 /* A TAB that extends past the right edge of the
14545 window. This produces a single glyph on
14546 window system frames. We leave the glyph in
14547 this row and let it fill the row, but don't
14548 consume the TAB. */
14549 it->continuation_lines_width += it->last_visible_x;
14550 row->ends_in_middle_of_char_p = 1;
14551 row->continued_p = 1;
14552 glyph->pixel_width = it->last_visible_x - x;
14553 it->starts_in_middle_of_char_p = 1;
14554 }
14555 else
14556 {
14557 /* Something other than a TAB that draws past
14558 the right edge of the window. Restore
14559 positions to values before the element. */
14560 row->used[TEXT_AREA] = n_glyphs_before + i;
14561
14562 /* Display continuation glyphs. */
14563 if (!FRAME_WINDOW_P (it->f))
14564 produce_special_glyphs (it, IT_CONTINUATION);
14565 row->continued_p = 1;
14566
14567 it->continuation_lines_width += x;
14568
14569 if (nglyphs > 1 && i > 0)
14570 {
14571 row->ends_in_middle_of_char_p = 1;
14572 it->starts_in_middle_of_char_p = 1;
14573 }
14574
14575 /* Restore the height to what it was before the
14576 element not fitting on the line. */
14577 it->max_ascent = ascent;
14578 it->max_descent = descent;
14579 it->max_phys_ascent = phys_ascent;
14580 it->max_phys_descent = phys_descent;
14581 }
14582
14583 break;
14584 }
14585 else if (new_x > it->first_visible_x)
14586 {
14587 /* Increment number of glyphs actually displayed. */
14588 ++it->hpos;
14589
14590 if (x < it->first_visible_x)
14591 /* Glyph is partially visible, i.e. row starts at
14592 negative X position. */
14593 row->x = x - it->first_visible_x;
14594 }
14595 else
14596 {
14597 /* Glyph is completely off the left margin of the
14598 window. This should not happen because of the
14599 move_it_in_display_line at the start of this
14600 function, unless the text display area of the
14601 window is empty. */
14602 xassert (it->first_visible_x <= it->last_visible_x);
14603 }
14604 }
14605
14606 row->ascent = max (row->ascent, it->max_ascent);
14607 row->height = max (row->height, it->max_ascent + it->max_descent);
14608 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14609 row->phys_height = max (row->phys_height,
14610 it->max_phys_ascent + it->max_phys_descent);
14611
14612 /* End of this display line if row is continued. */
14613 if (row->continued_p)
14614 break;
14615 }
14616
14617 /* Is this a line end? If yes, we're also done, after making
14618 sure that a non-default face is extended up to the right
14619 margin of the window. */
14620 if (ITERATOR_AT_END_OF_LINE_P (it))
14621 {
14622 int used_before = row->used[TEXT_AREA];
14623
14624 row->ends_in_newline_from_string_p = STRINGP (it->object);
14625
14626 /* Add a space at the end of the line that is used to
14627 display the cursor there. */
14628 append_space (it, 0);
14629
14630 /* Extend the face to the end of the line. */
14631 extend_face_to_end_of_line (it);
14632
14633 /* Make sure we have the position. */
14634 if (used_before == 0)
14635 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14636
14637 /* Consume the line end. This skips over invisible lines. */
14638 set_iterator_to_next (it, 1);
14639 it->continuation_lines_width = 0;
14640 break;
14641 }
14642
14643 /* Proceed with next display element. Note that this skips
14644 over lines invisible because of selective display. */
14645 set_iterator_to_next (it, 1);
14646
14647 /* If we truncate lines, we are done when the last displayed
14648 glyphs reach past the right margin of the window. */
14649 if (it->truncate_lines_p
14650 && (FRAME_WINDOW_P (it->f)
14651 ? (it->current_x >= it->last_visible_x)
14652 : (it->current_x > it->last_visible_x)))
14653 {
14654 /* Maybe add truncation glyphs. */
14655 if (!FRAME_WINDOW_P (it->f))
14656 {
14657 int i, n;
14658
14659 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14660 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14661 break;
14662
14663 for (n = row->used[TEXT_AREA]; i < n; ++i)
14664 {
14665 row->used[TEXT_AREA] = i;
14666 produce_special_glyphs (it, IT_TRUNCATION);
14667 }
14668 }
14669
14670 row->truncated_on_right_p = 1;
14671 it->continuation_lines_width = 0;
14672 reseat_at_next_visible_line_start (it, 0);
14673 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14674 it->hpos = hpos_before;
14675 it->current_x = x_before;
14676 break;
14677 }
14678 }
14679
14680 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14681 at the left window margin. */
14682 if (it->first_visible_x
14683 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14684 {
14685 if (!FRAME_WINDOW_P (it->f))
14686 insert_left_trunc_glyphs (it);
14687 row->truncated_on_left_p = 1;
14688 }
14689
14690 /* If the start of this line is the overlay arrow-position, then
14691 mark this glyph row as the one containing the overlay arrow.
14692 This is clearly a mess with variable size fonts. It would be
14693 better to let it be displayed like cursors under X. */
14694 if (MARKERP (Voverlay_arrow_position)
14695 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14696 && (MATRIX_ROW_START_CHARPOS (row)
14697 == marker_position (Voverlay_arrow_position))
14698 && STRINGP (Voverlay_arrow_string)
14699 && ! overlay_arrow_seen)
14700 {
14701 /* Overlay arrow in window redisplay is a fringe bitmap. */
14702 if (!FRAME_WINDOW_P (it->f))
14703 {
14704 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14705 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14706 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14707 struct glyph *p = row->glyphs[TEXT_AREA];
14708 struct glyph *p2, *end;
14709
14710 /* Copy the arrow glyphs. */
14711 while (glyph < arrow_end)
14712 *p++ = *glyph++;
14713
14714 /* Throw away padding glyphs. */
14715 p2 = p;
14716 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14717 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14718 ++p2;
14719 if (p2 > p)
14720 {
14721 while (p2 < end)
14722 *p++ = *p2++;
14723 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14724 }
14725 }
14726
14727 overlay_arrow_seen = 1;
14728 row->overlay_arrow_p = 1;
14729 }
14730
14731 /* Compute pixel dimensions of this line. */
14732 compute_line_metrics (it);
14733
14734 /* Remember the position at which this line ends. */
14735 row->end = it->current;
14736
14737 /* Maybe set the cursor. */
14738 if (it->w->cursor.vpos < 0
14739 && PT >= MATRIX_ROW_START_CHARPOS (row)
14740 && PT <= MATRIX_ROW_END_CHARPOS (row)
14741 && cursor_row_p (it->w, row))
14742 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14743
14744 /* Highlight trailing whitespace. */
14745 if (!NILP (Vshow_trailing_whitespace))
14746 highlight_trailing_whitespace (it->f, it->glyph_row);
14747
14748 /* Prepare for the next line. This line starts horizontally at (X
14749 HPOS) = (0 0). Vertical positions are incremented. As a
14750 convenience for the caller, IT->glyph_row is set to the next
14751 row to be used. */
14752 it->current_x = it->hpos = 0;
14753 it->current_y += row->height;
14754 ++it->vpos;
14755 ++it->glyph_row;
14756 it->start = it->current;
14757 return row->displays_text_p;
14758 }
14759
14760
14761 \f
14762 /***********************************************************************
14763 Menu Bar
14764 ***********************************************************************/
14765
14766 /* Redisplay the menu bar in the frame for window W.
14767
14768 The menu bar of X frames that don't have X toolkit support is
14769 displayed in a special window W->frame->menu_bar_window.
14770
14771 The menu bar of terminal frames is treated specially as far as
14772 glyph matrices are concerned. Menu bar lines are not part of
14773 windows, so the update is done directly on the frame matrix rows
14774 for the menu bar. */
14775
14776 static void
14777 display_menu_bar (w)
14778 struct window *w;
14779 {
14780 struct frame *f = XFRAME (WINDOW_FRAME (w));
14781 struct it it;
14782 Lisp_Object items;
14783 int i;
14784
14785 /* Don't do all this for graphical frames. */
14786 #ifdef HAVE_NTGUI
14787 if (!NILP (Vwindow_system))
14788 return;
14789 #endif
14790 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14791 if (FRAME_X_P (f))
14792 return;
14793 #endif
14794 #ifdef MAC_OS
14795 if (FRAME_MAC_P (f))
14796 return;
14797 #endif
14798
14799 #ifdef USE_X_TOOLKIT
14800 xassert (!FRAME_WINDOW_P (f));
14801 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14802 it.first_visible_x = 0;
14803 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14804 #else /* not USE_X_TOOLKIT */
14805 if (FRAME_WINDOW_P (f))
14806 {
14807 /* Menu bar lines are displayed in the desired matrix of the
14808 dummy window menu_bar_window. */
14809 struct window *menu_w;
14810 xassert (WINDOWP (f->menu_bar_window));
14811 menu_w = XWINDOW (f->menu_bar_window);
14812 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14813 MENU_FACE_ID);
14814 it.first_visible_x = 0;
14815 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14816 }
14817 else
14818 {
14819 /* This is a TTY frame, i.e. character hpos/vpos are used as
14820 pixel x/y. */
14821 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14822 MENU_FACE_ID);
14823 it.first_visible_x = 0;
14824 it.last_visible_x = FRAME_COLS (f);
14825 }
14826 #endif /* not USE_X_TOOLKIT */
14827
14828 if (! mode_line_inverse_video)
14829 /* Force the menu-bar to be displayed in the default face. */
14830 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14831
14832 /* Clear all rows of the menu bar. */
14833 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14834 {
14835 struct glyph_row *row = it.glyph_row + i;
14836 clear_glyph_row (row);
14837 row->enabled_p = 1;
14838 row->full_width_p = 1;
14839 }
14840
14841 /* Display all items of the menu bar. */
14842 items = FRAME_MENU_BAR_ITEMS (it.f);
14843 for (i = 0; i < XVECTOR (items)->size; i += 4)
14844 {
14845 Lisp_Object string;
14846
14847 /* Stop at nil string. */
14848 string = AREF (items, i + 1);
14849 if (NILP (string))
14850 break;
14851
14852 /* Remember where item was displayed. */
14853 AREF (items, i + 3) = make_number (it.hpos);
14854
14855 /* Display the item, pad with one space. */
14856 if (it.current_x < it.last_visible_x)
14857 display_string (NULL, string, Qnil, 0, 0, &it,
14858 SCHARS (string) + 1, 0, 0, -1);
14859 }
14860
14861 /* Fill out the line with spaces. */
14862 if (it.current_x < it.last_visible_x)
14863 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14864
14865 /* Compute the total height of the lines. */
14866 compute_line_metrics (&it);
14867 }
14868
14869
14870 \f
14871 /***********************************************************************
14872 Mode Line
14873 ***********************************************************************/
14874
14875 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14876 FORCE is non-zero, redisplay mode lines unconditionally.
14877 Otherwise, redisplay only mode lines that are garbaged. Value is
14878 the number of windows whose mode lines were redisplayed. */
14879
14880 static int
14881 redisplay_mode_lines (window, force)
14882 Lisp_Object window;
14883 int force;
14884 {
14885 int nwindows = 0;
14886
14887 while (!NILP (window))
14888 {
14889 struct window *w = XWINDOW (window);
14890
14891 if (WINDOWP (w->hchild))
14892 nwindows += redisplay_mode_lines (w->hchild, force);
14893 else if (WINDOWP (w->vchild))
14894 nwindows += redisplay_mode_lines (w->vchild, force);
14895 else if (force
14896 || FRAME_GARBAGED_P (XFRAME (w->frame))
14897 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14898 {
14899 struct text_pos lpoint;
14900 struct buffer *old = current_buffer;
14901
14902 /* Set the window's buffer for the mode line display. */
14903 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14904 set_buffer_internal_1 (XBUFFER (w->buffer));
14905
14906 /* Point refers normally to the selected window. For any
14907 other window, set up appropriate value. */
14908 if (!EQ (window, selected_window))
14909 {
14910 struct text_pos pt;
14911
14912 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14913 if (CHARPOS (pt) < BEGV)
14914 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14915 else if (CHARPOS (pt) > (ZV - 1))
14916 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14917 else
14918 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14919 }
14920
14921 /* Display mode lines. */
14922 clear_glyph_matrix (w->desired_matrix);
14923 if (display_mode_lines (w))
14924 {
14925 ++nwindows;
14926 w->must_be_updated_p = 1;
14927 }
14928
14929 /* Restore old settings. */
14930 set_buffer_internal_1 (old);
14931 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14932 }
14933
14934 window = w->next;
14935 }
14936
14937 return nwindows;
14938 }
14939
14940
14941 /* Display the mode and/or top line of window W. Value is the number
14942 of mode lines displayed. */
14943
14944 static int
14945 display_mode_lines (w)
14946 struct window *w;
14947 {
14948 Lisp_Object old_selected_window, old_selected_frame;
14949 int n = 0;
14950
14951 old_selected_frame = selected_frame;
14952 selected_frame = w->frame;
14953 old_selected_window = selected_window;
14954 XSETWINDOW (selected_window, w);
14955
14956 /* These will be set while the mode line specs are processed. */
14957 line_number_displayed = 0;
14958 w->column_number_displayed = Qnil;
14959
14960 if (WINDOW_WANTS_MODELINE_P (w))
14961 {
14962 struct window *sel_w = XWINDOW (old_selected_window);
14963
14964 /* Select mode line face based on the real selected window. */
14965 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14966 current_buffer->mode_line_format);
14967 ++n;
14968 }
14969
14970 if (WINDOW_WANTS_HEADER_LINE_P (w))
14971 {
14972 display_mode_line (w, HEADER_LINE_FACE_ID,
14973 current_buffer->header_line_format);
14974 ++n;
14975 }
14976
14977 selected_frame = old_selected_frame;
14978 selected_window = old_selected_window;
14979 return n;
14980 }
14981
14982
14983 /* Display mode or top line of window W. FACE_ID specifies which line
14984 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14985 FORMAT is the mode line format to display. Value is the pixel
14986 height of the mode line displayed. */
14987
14988 static int
14989 display_mode_line (w, face_id, format)
14990 struct window *w;
14991 enum face_id face_id;
14992 Lisp_Object format;
14993 {
14994 struct it it;
14995 struct face *face;
14996
14997 init_iterator (&it, w, -1, -1, NULL, face_id);
14998 prepare_desired_row (it.glyph_row);
14999
15000 if (! mode_line_inverse_video)
15001 /* Force the mode-line to be displayed in the default face. */
15002 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15003
15004 /* Temporarily make frame's keyboard the current kboard so that
15005 kboard-local variables in the mode_line_format will get the right
15006 values. */
15007 push_frame_kboard (it.f);
15008 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15009 pop_frame_kboard ();
15010
15011 /* Fill up with spaces. */
15012 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15013
15014 compute_line_metrics (&it);
15015 it.glyph_row->full_width_p = 1;
15016 it.glyph_row->mode_line_p = 1;
15017 it.glyph_row->continued_p = 0;
15018 it.glyph_row->truncated_on_left_p = 0;
15019 it.glyph_row->truncated_on_right_p = 0;
15020
15021 /* Make a 3D mode-line have a shadow at its right end. */
15022 face = FACE_FROM_ID (it.f, face_id);
15023 extend_face_to_end_of_line (&it);
15024 if (face->box != FACE_NO_BOX)
15025 {
15026 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15027 + it.glyph_row->used[TEXT_AREA] - 1);
15028 last->right_box_line_p = 1;
15029 }
15030
15031 return it.glyph_row->height;
15032 }
15033
15034 /* Alist that caches the results of :propertize.
15035 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15036 Lisp_Object mode_line_proptrans_alist;
15037
15038 /* List of strings making up the mode-line. */
15039 Lisp_Object mode_line_string_list;
15040
15041 /* Base face property when building propertized mode line string. */
15042 static Lisp_Object mode_line_string_face;
15043 static Lisp_Object mode_line_string_face_prop;
15044
15045
15046 /* Contribute ELT to the mode line for window IT->w. How it
15047 translates into text depends on its data type.
15048
15049 IT describes the display environment in which we display, as usual.
15050
15051 DEPTH is the depth in recursion. It is used to prevent
15052 infinite recursion here.
15053
15054 FIELD_WIDTH is the number of characters the display of ELT should
15055 occupy in the mode line, and PRECISION is the maximum number of
15056 characters to display from ELT's representation. See
15057 display_string for details.
15058
15059 Returns the hpos of the end of the text generated by ELT.
15060
15061 PROPS is a property list to add to any string we encounter.
15062
15063 If RISKY is nonzero, remove (disregard) any properties in any string
15064 we encounter, and ignore :eval and :propertize.
15065
15066 If the global variable `frame_title_ptr' is non-NULL, then the output
15067 is passed to `store_frame_title' instead of `display_string'. */
15068
15069 static int
15070 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15071 struct it *it;
15072 int depth;
15073 int field_width, precision;
15074 Lisp_Object elt, props;
15075 int risky;
15076 {
15077 int n = 0, field, prec;
15078 int literal = 0;
15079
15080 tail_recurse:
15081 if (depth > 100)
15082 elt = build_string ("*too-deep*");
15083
15084 depth++;
15085
15086 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15087 {
15088 case Lisp_String:
15089 {
15090 /* A string: output it and check for %-constructs within it. */
15091 unsigned char c;
15092 const unsigned char *this, *lisp_string;
15093
15094 if (!NILP (props) || risky)
15095 {
15096 Lisp_Object oprops, aelt;
15097 oprops = Ftext_properties_at (make_number (0), elt);
15098
15099 if (NILP (Fequal (props, oprops)) || risky)
15100 {
15101 /* If the starting string has properties,
15102 merge the specified ones onto the existing ones. */
15103 if (! NILP (oprops) && !risky)
15104 {
15105 Lisp_Object tem;
15106
15107 oprops = Fcopy_sequence (oprops);
15108 tem = props;
15109 while (CONSP (tem))
15110 {
15111 oprops = Fplist_put (oprops, XCAR (tem),
15112 XCAR (XCDR (tem)));
15113 tem = XCDR (XCDR (tem));
15114 }
15115 props = oprops;
15116 }
15117
15118 aelt = Fassoc (elt, mode_line_proptrans_alist);
15119 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15120 {
15121 mode_line_proptrans_alist
15122 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15123 elt = XCAR (aelt);
15124 }
15125 else
15126 {
15127 Lisp_Object tem;
15128
15129 elt = Fcopy_sequence (elt);
15130 Fset_text_properties (make_number (0), Flength (elt),
15131 props, elt);
15132 /* Add this item to mode_line_proptrans_alist. */
15133 mode_line_proptrans_alist
15134 = Fcons (Fcons (elt, props),
15135 mode_line_proptrans_alist);
15136 /* Truncate mode_line_proptrans_alist
15137 to at most 50 elements. */
15138 tem = Fnthcdr (make_number (50),
15139 mode_line_proptrans_alist);
15140 if (! NILP (tem))
15141 XSETCDR (tem, Qnil);
15142 }
15143 }
15144 }
15145
15146 this = SDATA (elt);
15147 lisp_string = this;
15148
15149 if (literal)
15150 {
15151 prec = precision - n;
15152 if (frame_title_ptr)
15153 n += store_frame_title (SDATA (elt), -1, prec);
15154 else if (!NILP (mode_line_string_list))
15155 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15156 else
15157 n += display_string (NULL, elt, Qnil, 0, 0, it,
15158 0, prec, 0, STRING_MULTIBYTE (elt));
15159
15160 break;
15161 }
15162
15163 while ((precision <= 0 || n < precision)
15164 && *this
15165 && (frame_title_ptr
15166 || !NILP (mode_line_string_list)
15167 || it->current_x < it->last_visible_x))
15168 {
15169 const unsigned char *last = this;
15170
15171 /* Advance to end of string or next format specifier. */
15172 while ((c = *this++) != '\0' && c != '%')
15173 ;
15174
15175 if (this - 1 != last)
15176 {
15177 /* Output to end of string or up to '%'. Field width
15178 is length of string. Don't output more than
15179 PRECISION allows us. */
15180 --this;
15181
15182 prec = chars_in_text (last, this - last);
15183 if (precision > 0 && prec > precision - n)
15184 prec = precision - n;
15185
15186 if (frame_title_ptr)
15187 n += store_frame_title (last, 0, prec);
15188 else if (!NILP (mode_line_string_list))
15189 {
15190 int bytepos = last - lisp_string;
15191 int charpos = string_byte_to_char (elt, bytepos);
15192 n += store_mode_line_string (NULL,
15193 Fsubstring (elt, make_number (charpos),
15194 make_number (charpos + prec)),
15195 0, 0, 0, Qnil);
15196 }
15197 else
15198 {
15199 int bytepos = last - lisp_string;
15200 int charpos = string_byte_to_char (elt, bytepos);
15201 n += display_string (NULL, elt, Qnil, 0, charpos,
15202 it, 0, prec, 0,
15203 STRING_MULTIBYTE (elt));
15204 }
15205 }
15206 else /* c == '%' */
15207 {
15208 const unsigned char *percent_position = this;
15209
15210 /* Get the specified minimum width. Zero means
15211 don't pad. */
15212 field = 0;
15213 while ((c = *this++) >= '0' && c <= '9')
15214 field = field * 10 + c - '0';
15215
15216 /* Don't pad beyond the total padding allowed. */
15217 if (field_width - n > 0 && field > field_width - n)
15218 field = field_width - n;
15219
15220 /* Note that either PRECISION <= 0 or N < PRECISION. */
15221 prec = precision - n;
15222
15223 if (c == 'M')
15224 n += display_mode_element (it, depth, field, prec,
15225 Vglobal_mode_string, props,
15226 risky);
15227 else if (c != 0)
15228 {
15229 int multibyte;
15230 int bytepos, charpos;
15231 unsigned char *spec;
15232
15233 bytepos = percent_position - lisp_string;
15234 charpos = (STRING_MULTIBYTE (elt)
15235 ? string_byte_to_char (elt, bytepos)
15236 : bytepos);
15237
15238 spec
15239 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15240
15241 if (frame_title_ptr)
15242 n += store_frame_title (spec, field, prec);
15243 else if (!NILP (mode_line_string_list))
15244 {
15245 int len = strlen (spec);
15246 Lisp_Object tem = make_string (spec, len);
15247 props = Ftext_properties_at (make_number (charpos), elt);
15248 /* Should only keep face property in props */
15249 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15250 }
15251 else
15252 {
15253 int nglyphs_before, nwritten;
15254
15255 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15256 nwritten = display_string (spec, Qnil, elt,
15257 charpos, 0, it,
15258 field, prec, 0,
15259 multibyte);
15260
15261 /* Assign to the glyphs written above the
15262 string where the `%x' came from, position
15263 of the `%'. */
15264 if (nwritten > 0)
15265 {
15266 struct glyph *glyph
15267 = (it->glyph_row->glyphs[TEXT_AREA]
15268 + nglyphs_before);
15269 int i;
15270
15271 for (i = 0; i < nwritten; ++i)
15272 {
15273 glyph[i].object = elt;
15274 glyph[i].charpos = charpos;
15275 }
15276
15277 n += nwritten;
15278 }
15279 }
15280 }
15281 else /* c == 0 */
15282 break;
15283 }
15284 }
15285 }
15286 break;
15287
15288 case Lisp_Symbol:
15289 /* A symbol: process the value of the symbol recursively
15290 as if it appeared here directly. Avoid error if symbol void.
15291 Special case: if value of symbol is a string, output the string
15292 literally. */
15293 {
15294 register Lisp_Object tem;
15295
15296 /* If the variable is not marked as risky to set
15297 then its contents are risky to use. */
15298 if (NILP (Fget (elt, Qrisky_local_variable)))
15299 risky = 1;
15300
15301 tem = Fboundp (elt);
15302 if (!NILP (tem))
15303 {
15304 tem = Fsymbol_value (elt);
15305 /* If value is a string, output that string literally:
15306 don't check for % within it. */
15307 if (STRINGP (tem))
15308 literal = 1;
15309
15310 if (!EQ (tem, elt))
15311 {
15312 /* Give up right away for nil or t. */
15313 elt = tem;
15314 goto tail_recurse;
15315 }
15316 }
15317 }
15318 break;
15319
15320 case Lisp_Cons:
15321 {
15322 register Lisp_Object car, tem;
15323
15324 /* A cons cell: five distinct cases.
15325 If first element is :eval or :propertize, do something special.
15326 If first element is a string or a cons, process all the elements
15327 and effectively concatenate them.
15328 If first element is a negative number, truncate displaying cdr to
15329 at most that many characters. If positive, pad (with spaces)
15330 to at least that many characters.
15331 If first element is a symbol, process the cadr or caddr recursively
15332 according to whether the symbol's value is non-nil or nil. */
15333 car = XCAR (elt);
15334 if (EQ (car, QCeval))
15335 {
15336 /* An element of the form (:eval FORM) means evaluate FORM
15337 and use the result as mode line elements. */
15338
15339 if (risky)
15340 break;
15341
15342 if (CONSP (XCDR (elt)))
15343 {
15344 Lisp_Object spec;
15345 spec = safe_eval (XCAR (XCDR (elt)));
15346 n += display_mode_element (it, depth, field_width - n,
15347 precision - n, spec, props,
15348 risky);
15349 }
15350 }
15351 else if (EQ (car, QCpropertize))
15352 {
15353 /* An element of the form (:propertize ELT PROPS...)
15354 means display ELT but applying properties PROPS. */
15355
15356 if (risky)
15357 break;
15358
15359 if (CONSP (XCDR (elt)))
15360 n += display_mode_element (it, depth, field_width - n,
15361 precision - n, XCAR (XCDR (elt)),
15362 XCDR (XCDR (elt)), risky);
15363 }
15364 else if (SYMBOLP (car))
15365 {
15366 tem = Fboundp (car);
15367 elt = XCDR (elt);
15368 if (!CONSP (elt))
15369 goto invalid;
15370 /* elt is now the cdr, and we know it is a cons cell.
15371 Use its car if CAR has a non-nil value. */
15372 if (!NILP (tem))
15373 {
15374 tem = Fsymbol_value (car);
15375 if (!NILP (tem))
15376 {
15377 elt = XCAR (elt);
15378 goto tail_recurse;
15379 }
15380 }
15381 /* Symbol's value is nil (or symbol is unbound)
15382 Get the cddr of the original list
15383 and if possible find the caddr and use that. */
15384 elt = XCDR (elt);
15385 if (NILP (elt))
15386 break;
15387 else if (!CONSP (elt))
15388 goto invalid;
15389 elt = XCAR (elt);
15390 goto tail_recurse;
15391 }
15392 else if (INTEGERP (car))
15393 {
15394 register int lim = XINT (car);
15395 elt = XCDR (elt);
15396 if (lim < 0)
15397 {
15398 /* Negative int means reduce maximum width. */
15399 if (precision <= 0)
15400 precision = -lim;
15401 else
15402 precision = min (precision, -lim);
15403 }
15404 else if (lim > 0)
15405 {
15406 /* Padding specified. Don't let it be more than
15407 current maximum. */
15408 if (precision > 0)
15409 lim = min (precision, lim);
15410
15411 /* If that's more padding than already wanted, queue it.
15412 But don't reduce padding already specified even if
15413 that is beyond the current truncation point. */
15414 field_width = max (lim, field_width);
15415 }
15416 goto tail_recurse;
15417 }
15418 else if (STRINGP (car) || CONSP (car))
15419 {
15420 register int limit = 50;
15421 /* Limit is to protect against circular lists. */
15422 while (CONSP (elt)
15423 && --limit > 0
15424 && (precision <= 0 || n < precision))
15425 {
15426 n += display_mode_element (it, depth, field_width - n,
15427 precision - n, XCAR (elt),
15428 props, risky);
15429 elt = XCDR (elt);
15430 }
15431 }
15432 }
15433 break;
15434
15435 default:
15436 invalid:
15437 elt = build_string ("*invalid*");
15438 goto tail_recurse;
15439 }
15440
15441 /* Pad to FIELD_WIDTH. */
15442 if (field_width > 0 && n < field_width)
15443 {
15444 if (frame_title_ptr)
15445 n += store_frame_title ("", field_width - n, 0);
15446 else if (!NILP (mode_line_string_list))
15447 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15448 else
15449 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15450 0, 0, 0);
15451 }
15452
15453 return n;
15454 }
15455
15456 /* Store a mode-line string element in mode_line_string_list.
15457
15458 If STRING is non-null, display that C string. Otherwise, the Lisp
15459 string LISP_STRING is displayed.
15460
15461 FIELD_WIDTH is the minimum number of output glyphs to produce.
15462 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15463 with spaces. FIELD_WIDTH <= 0 means don't pad.
15464
15465 PRECISION is the maximum number of characters to output from
15466 STRING. PRECISION <= 0 means don't truncate the string.
15467
15468 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15469 properties to the string.
15470
15471 PROPS are the properties to add to the string.
15472 The mode_line_string_face face property is always added to the string.
15473 */
15474
15475 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15476 char *string;
15477 Lisp_Object lisp_string;
15478 int copy_string;
15479 int field_width;
15480 int precision;
15481 Lisp_Object props;
15482 {
15483 int len;
15484 int n = 0;
15485
15486 if (string != NULL)
15487 {
15488 len = strlen (string);
15489 if (precision > 0 && len > precision)
15490 len = precision;
15491 lisp_string = make_string (string, len);
15492 if (NILP (props))
15493 props = mode_line_string_face_prop;
15494 else if (!NILP (mode_line_string_face))
15495 {
15496 Lisp_Object face = Fplist_get (props, Qface);
15497 props = Fcopy_sequence (props);
15498 if (NILP (face))
15499 face = mode_line_string_face;
15500 else
15501 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15502 props = Fplist_put (props, Qface, face);
15503 }
15504 Fadd_text_properties (make_number (0), make_number (len),
15505 props, lisp_string);
15506 }
15507 else
15508 {
15509 len = XFASTINT (Flength (lisp_string));
15510 if (precision > 0 && len > precision)
15511 {
15512 len = precision;
15513 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15514 precision = -1;
15515 }
15516 if (!NILP (mode_line_string_face))
15517 {
15518 Lisp_Object face;
15519 if (NILP (props))
15520 props = Ftext_properties_at (make_number (0), lisp_string);
15521 face = Fplist_get (props, Qface);
15522 if (NILP (face))
15523 face = mode_line_string_face;
15524 else
15525 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15526 props = Fcons (Qface, Fcons (face, Qnil));
15527 if (copy_string)
15528 lisp_string = Fcopy_sequence (lisp_string);
15529 }
15530 if (!NILP (props))
15531 Fadd_text_properties (make_number (0), make_number (len),
15532 props, lisp_string);
15533 }
15534
15535 if (len > 0)
15536 {
15537 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15538 n += len;
15539 }
15540
15541 if (field_width > len)
15542 {
15543 field_width -= len;
15544 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15545 if (!NILP (props))
15546 Fadd_text_properties (make_number (0), make_number (field_width),
15547 props, lisp_string);
15548 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15549 n += field_width;
15550 }
15551
15552 return n;
15553 }
15554
15555
15556 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15557 0, 3, 0,
15558 doc: /* Return the mode-line of selected window as a string.
15559 First optional arg FORMAT specifies a different format string (see
15560 `mode-line-format' for details) to use. If FORMAT is t, return
15561 the buffer's header-line. Second optional arg WINDOW specifies a
15562 different window to use as the context for the formatting.
15563 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15564 (format, window, no_props)
15565 Lisp_Object format, window, no_props;
15566 {
15567 struct it it;
15568 int len;
15569 struct window *w;
15570 struct buffer *old_buffer = NULL;
15571 enum face_id face_id = DEFAULT_FACE_ID;
15572
15573 if (NILP (window))
15574 window = selected_window;
15575 CHECK_WINDOW (window);
15576 w = XWINDOW (window);
15577 CHECK_BUFFER (w->buffer);
15578
15579 if (XBUFFER (w->buffer) != current_buffer)
15580 {
15581 old_buffer = current_buffer;
15582 set_buffer_internal_1 (XBUFFER (w->buffer));
15583 }
15584
15585 if (NILP (format) || EQ (format, Qt))
15586 {
15587 face_id = NILP (format)
15588 ? CURRENT_MODE_LINE_FACE_ID (w) :
15589 HEADER_LINE_FACE_ID;
15590 format = NILP (format)
15591 ? current_buffer->mode_line_format
15592 : current_buffer->header_line_format;
15593 }
15594
15595 init_iterator (&it, w, -1, -1, NULL, face_id);
15596
15597 if (NILP (no_props))
15598 {
15599 mode_line_string_face =
15600 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15601 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15602 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15603
15604 mode_line_string_face_prop =
15605 NILP (mode_line_string_face) ? Qnil :
15606 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15607
15608 /* We need a dummy last element in mode_line_string_list to
15609 indicate we are building the propertized mode-line string.
15610 Using mode_line_string_face_prop here GC protects it. */
15611 mode_line_string_list =
15612 Fcons (mode_line_string_face_prop, Qnil);
15613 frame_title_ptr = NULL;
15614 }
15615 else
15616 {
15617 mode_line_string_face_prop = Qnil;
15618 mode_line_string_list = Qnil;
15619 frame_title_ptr = frame_title_buf;
15620 }
15621
15622 push_frame_kboard (it.f);
15623 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15624 pop_frame_kboard ();
15625
15626 if (old_buffer)
15627 set_buffer_internal_1 (old_buffer);
15628
15629 if (NILP (no_props))
15630 {
15631 Lisp_Object str;
15632 mode_line_string_list = Fnreverse (mode_line_string_list);
15633 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15634 make_string ("", 0));
15635 mode_line_string_face_prop = Qnil;
15636 mode_line_string_list = Qnil;
15637 return str;
15638 }
15639
15640 len = frame_title_ptr - frame_title_buf;
15641 if (len > 0 && frame_title_ptr[-1] == '-')
15642 {
15643 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15644 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15645 ;
15646 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15647 if (len > frame_title_ptr - frame_title_buf)
15648 len = frame_title_ptr - frame_title_buf;
15649 }
15650
15651 frame_title_ptr = NULL;
15652 return make_string (frame_title_buf, len);
15653 }
15654
15655 /* Write a null-terminated, right justified decimal representation of
15656 the positive integer D to BUF using a minimal field width WIDTH. */
15657
15658 static void
15659 pint2str (buf, width, d)
15660 register char *buf;
15661 register int width;
15662 register int d;
15663 {
15664 register char *p = buf;
15665
15666 if (d <= 0)
15667 *p++ = '0';
15668 else
15669 {
15670 while (d > 0)
15671 {
15672 *p++ = d % 10 + '0';
15673 d /= 10;
15674 }
15675 }
15676
15677 for (width -= (int) (p - buf); width > 0; --width)
15678 *p++ = ' ';
15679 *p-- = '\0';
15680 while (p > buf)
15681 {
15682 d = *buf;
15683 *buf++ = *p;
15684 *p-- = d;
15685 }
15686 }
15687
15688 /* Write a null-terminated, right justified decimal and "human
15689 readable" representation of the nonnegative integer D to BUF using
15690 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15691
15692 static const char power_letter[] =
15693 {
15694 0, /* not used */
15695 'k', /* kilo */
15696 'M', /* mega */
15697 'G', /* giga */
15698 'T', /* tera */
15699 'P', /* peta */
15700 'E', /* exa */
15701 'Z', /* zetta */
15702 'Y' /* yotta */
15703 };
15704
15705 static void
15706 pint2hrstr (buf, width, d)
15707 char *buf;
15708 int width;
15709 int d;
15710 {
15711 /* We aim to represent the nonnegative integer D as
15712 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15713 int quotient = d;
15714 int remainder = 0;
15715 /* -1 means: do not use TENTHS. */
15716 int tenths = -1;
15717 int exponent = 0;
15718
15719 /* Length of QUOTIENT.TENTHS as a string. */
15720 int length;
15721
15722 char * psuffix;
15723 char * p;
15724
15725 if (1000 <= quotient)
15726 {
15727 /* Scale to the appropriate EXPONENT. */
15728 do
15729 {
15730 remainder = quotient % 1000;
15731 quotient /= 1000;
15732 exponent++;
15733 }
15734 while (1000 <= quotient);
15735
15736 /* Round to nearest and decide whether to use TENTHS or not. */
15737 if (quotient <= 9)
15738 {
15739 tenths = remainder / 100;
15740 if (50 <= remainder % 100)
15741 if (tenths < 9)
15742 tenths++;
15743 else
15744 {
15745 quotient++;
15746 if (quotient == 10)
15747 tenths = -1;
15748 else
15749 tenths = 0;
15750 }
15751 }
15752 else
15753 if (500 <= remainder)
15754 if (quotient < 999)
15755 quotient++;
15756 else
15757 {
15758 quotient = 1;
15759 exponent++;
15760 tenths = 0;
15761 }
15762 }
15763
15764 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15765 if (tenths == -1 && quotient <= 99)
15766 if (quotient <= 9)
15767 length = 1;
15768 else
15769 length = 2;
15770 else
15771 length = 3;
15772 p = psuffix = buf + max (width, length);
15773
15774 /* Print EXPONENT. */
15775 if (exponent)
15776 *psuffix++ = power_letter[exponent];
15777 *psuffix = '\0';
15778
15779 /* Print TENTHS. */
15780 if (tenths >= 0)
15781 {
15782 *--p = '0' + tenths;
15783 *--p = '.';
15784 }
15785
15786 /* Print QUOTIENT. */
15787 do
15788 {
15789 int digit = quotient % 10;
15790 *--p = '0' + digit;
15791 }
15792 while ((quotient /= 10) != 0);
15793
15794 /* Print leading spaces. */
15795 while (buf < p)
15796 *--p = ' ';
15797 }
15798
15799 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15800 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15801 type of CODING_SYSTEM. Return updated pointer into BUF. */
15802
15803 static unsigned char invalid_eol_type[] = "(*invalid*)";
15804
15805 static char *
15806 decode_mode_spec_coding (coding_system, buf, eol_flag)
15807 Lisp_Object coding_system;
15808 register char *buf;
15809 int eol_flag;
15810 {
15811 Lisp_Object val;
15812 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15813 const unsigned char *eol_str;
15814 int eol_str_len;
15815 /* The EOL conversion we are using. */
15816 Lisp_Object eoltype;
15817
15818 val = Fget (coding_system, Qcoding_system);
15819 eoltype = Qnil;
15820
15821 if (!VECTORP (val)) /* Not yet decided. */
15822 {
15823 if (multibyte)
15824 *buf++ = '-';
15825 if (eol_flag)
15826 eoltype = eol_mnemonic_undecided;
15827 /* Don't mention EOL conversion if it isn't decided. */
15828 }
15829 else
15830 {
15831 Lisp_Object eolvalue;
15832
15833 eolvalue = Fget (coding_system, Qeol_type);
15834
15835 if (multibyte)
15836 *buf++ = XFASTINT (AREF (val, 1));
15837
15838 if (eol_flag)
15839 {
15840 /* The EOL conversion that is normal on this system. */
15841
15842 if (NILP (eolvalue)) /* Not yet decided. */
15843 eoltype = eol_mnemonic_undecided;
15844 else if (VECTORP (eolvalue)) /* Not yet decided. */
15845 eoltype = eol_mnemonic_undecided;
15846 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15847 eoltype = (XFASTINT (eolvalue) == 0
15848 ? eol_mnemonic_unix
15849 : (XFASTINT (eolvalue) == 1
15850 ? eol_mnemonic_dos : eol_mnemonic_mac));
15851 }
15852 }
15853
15854 if (eol_flag)
15855 {
15856 /* Mention the EOL conversion if it is not the usual one. */
15857 if (STRINGP (eoltype))
15858 {
15859 eol_str = SDATA (eoltype);
15860 eol_str_len = SBYTES (eoltype);
15861 }
15862 else if (INTEGERP (eoltype)
15863 && CHAR_VALID_P (XINT (eoltype), 0))
15864 {
15865 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15866 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15867 eol_str = tmp;
15868 }
15869 else
15870 {
15871 eol_str = invalid_eol_type;
15872 eol_str_len = sizeof (invalid_eol_type) - 1;
15873 }
15874 bcopy (eol_str, buf, eol_str_len);
15875 buf += eol_str_len;
15876 }
15877
15878 return buf;
15879 }
15880
15881 /* Return a string for the output of a mode line %-spec for window W,
15882 generated by character C. PRECISION >= 0 means don't return a
15883 string longer than that value. FIELD_WIDTH > 0 means pad the
15884 string returned with spaces to that value. Return 1 in *MULTIBYTE
15885 if the result is multibyte text. */
15886
15887 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15888
15889 static char *
15890 decode_mode_spec (w, c, field_width, precision, multibyte)
15891 struct window *w;
15892 register int c;
15893 int field_width, precision;
15894 int *multibyte;
15895 {
15896 Lisp_Object obj;
15897 struct frame *f = XFRAME (WINDOW_FRAME (w));
15898 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15899 struct buffer *b = XBUFFER (w->buffer);
15900
15901 obj = Qnil;
15902 *multibyte = 0;
15903
15904 switch (c)
15905 {
15906 case '*':
15907 if (!NILP (b->read_only))
15908 return "%";
15909 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15910 return "*";
15911 return "-";
15912
15913 case '+':
15914 /* This differs from %* only for a modified read-only buffer. */
15915 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15916 return "*";
15917 if (!NILP (b->read_only))
15918 return "%";
15919 return "-";
15920
15921 case '&':
15922 /* This differs from %* in ignoring read-only-ness. */
15923 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15924 return "*";
15925 return "-";
15926
15927 case '%':
15928 return "%";
15929
15930 case '[':
15931 {
15932 int i;
15933 char *p;
15934
15935 if (command_loop_level > 5)
15936 return "[[[... ";
15937 p = decode_mode_spec_buf;
15938 for (i = 0; i < command_loop_level; i++)
15939 *p++ = '[';
15940 *p = 0;
15941 return decode_mode_spec_buf;
15942 }
15943
15944 case ']':
15945 {
15946 int i;
15947 char *p;
15948
15949 if (command_loop_level > 5)
15950 return " ...]]]";
15951 p = decode_mode_spec_buf;
15952 for (i = 0; i < command_loop_level; i++)
15953 *p++ = ']';
15954 *p = 0;
15955 return decode_mode_spec_buf;
15956 }
15957
15958 case '-':
15959 {
15960 register int i;
15961
15962 /* Let lots_of_dashes be a string of infinite length. */
15963 if (!NILP (mode_line_string_list))
15964 return "--";
15965 if (field_width <= 0
15966 || field_width > sizeof (lots_of_dashes))
15967 {
15968 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15969 decode_mode_spec_buf[i] = '-';
15970 decode_mode_spec_buf[i] = '\0';
15971 return decode_mode_spec_buf;
15972 }
15973 else
15974 return lots_of_dashes;
15975 }
15976
15977 case 'b':
15978 obj = b->name;
15979 break;
15980
15981 case 'c':
15982 {
15983 int col = (int) current_column (); /* iftc */
15984 w->column_number_displayed = make_number (col);
15985 pint2str (decode_mode_spec_buf, field_width, col);
15986 return decode_mode_spec_buf;
15987 }
15988
15989 case 'F':
15990 /* %F displays the frame name. */
15991 if (!NILP (f->title))
15992 return (char *) SDATA (f->title);
15993 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15994 return (char *) SDATA (f->name);
15995 return "Emacs";
15996
15997 case 'f':
15998 obj = b->filename;
15999 break;
16000
16001 case 'i':
16002 {
16003 int size = ZV - BEGV;
16004 pint2str (decode_mode_spec_buf, field_width, size);
16005 return decode_mode_spec_buf;
16006 }
16007
16008 case 'I':
16009 {
16010 int size = ZV - BEGV;
16011 pint2hrstr (decode_mode_spec_buf, field_width, size);
16012 return decode_mode_spec_buf;
16013 }
16014
16015 case 'l':
16016 {
16017 int startpos = XMARKER (w->start)->charpos;
16018 int startpos_byte = marker_byte_position (w->start);
16019 int line, linepos, linepos_byte, topline;
16020 int nlines, junk;
16021 int height = WINDOW_TOTAL_LINES (w);
16022
16023 /* If we decided that this buffer isn't suitable for line numbers,
16024 don't forget that too fast. */
16025 if (EQ (w->base_line_pos, w->buffer))
16026 goto no_value;
16027 /* But do forget it, if the window shows a different buffer now. */
16028 else if (BUFFERP (w->base_line_pos))
16029 w->base_line_pos = Qnil;
16030
16031 /* If the buffer is very big, don't waste time. */
16032 if (INTEGERP (Vline_number_display_limit)
16033 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16034 {
16035 w->base_line_pos = Qnil;
16036 w->base_line_number = Qnil;
16037 goto no_value;
16038 }
16039
16040 if (!NILP (w->base_line_number)
16041 && !NILP (w->base_line_pos)
16042 && XFASTINT (w->base_line_pos) <= startpos)
16043 {
16044 line = XFASTINT (w->base_line_number);
16045 linepos = XFASTINT (w->base_line_pos);
16046 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16047 }
16048 else
16049 {
16050 line = 1;
16051 linepos = BUF_BEGV (b);
16052 linepos_byte = BUF_BEGV_BYTE (b);
16053 }
16054
16055 /* Count lines from base line to window start position. */
16056 nlines = display_count_lines (linepos, linepos_byte,
16057 startpos_byte,
16058 startpos, &junk);
16059
16060 topline = nlines + line;
16061
16062 /* Determine a new base line, if the old one is too close
16063 or too far away, or if we did not have one.
16064 "Too close" means it's plausible a scroll-down would
16065 go back past it. */
16066 if (startpos == BUF_BEGV (b))
16067 {
16068 w->base_line_number = make_number (topline);
16069 w->base_line_pos = make_number (BUF_BEGV (b));
16070 }
16071 else if (nlines < height + 25 || nlines > height * 3 + 50
16072 || linepos == BUF_BEGV (b))
16073 {
16074 int limit = BUF_BEGV (b);
16075 int limit_byte = BUF_BEGV_BYTE (b);
16076 int position;
16077 int distance = (height * 2 + 30) * line_number_display_limit_width;
16078
16079 if (startpos - distance > limit)
16080 {
16081 limit = startpos - distance;
16082 limit_byte = CHAR_TO_BYTE (limit);
16083 }
16084
16085 nlines = display_count_lines (startpos, startpos_byte,
16086 limit_byte,
16087 - (height * 2 + 30),
16088 &position);
16089 /* If we couldn't find the lines we wanted within
16090 line_number_display_limit_width chars per line,
16091 give up on line numbers for this window. */
16092 if (position == limit_byte && limit == startpos - distance)
16093 {
16094 w->base_line_pos = w->buffer;
16095 w->base_line_number = Qnil;
16096 goto no_value;
16097 }
16098
16099 w->base_line_number = make_number (topline - nlines);
16100 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16101 }
16102
16103 /* Now count lines from the start pos to point. */
16104 nlines = display_count_lines (startpos, startpos_byte,
16105 PT_BYTE, PT, &junk);
16106
16107 /* Record that we did display the line number. */
16108 line_number_displayed = 1;
16109
16110 /* Make the string to show. */
16111 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16112 return decode_mode_spec_buf;
16113 no_value:
16114 {
16115 char* p = decode_mode_spec_buf;
16116 int pad = field_width - 2;
16117 while (pad-- > 0)
16118 *p++ = ' ';
16119 *p++ = '?';
16120 *p++ = '?';
16121 *p = '\0';
16122 return decode_mode_spec_buf;
16123 }
16124 }
16125 break;
16126
16127 case 'm':
16128 obj = b->mode_name;
16129 break;
16130
16131 case 'n':
16132 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16133 return " Narrow";
16134 break;
16135
16136 case 'p':
16137 {
16138 int pos = marker_position (w->start);
16139 int total = BUF_ZV (b) - BUF_BEGV (b);
16140
16141 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16142 {
16143 if (pos <= BUF_BEGV (b))
16144 return "All";
16145 else
16146 return "Bottom";
16147 }
16148 else if (pos <= BUF_BEGV (b))
16149 return "Top";
16150 else
16151 {
16152 if (total > 1000000)
16153 /* Do it differently for a large value, to avoid overflow. */
16154 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16155 else
16156 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16157 /* We can't normally display a 3-digit number,
16158 so get us a 2-digit number that is close. */
16159 if (total == 100)
16160 total = 99;
16161 sprintf (decode_mode_spec_buf, "%2d%%", total);
16162 return decode_mode_spec_buf;
16163 }
16164 }
16165
16166 /* Display percentage of size above the bottom of the screen. */
16167 case 'P':
16168 {
16169 int toppos = marker_position (w->start);
16170 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16171 int total = BUF_ZV (b) - BUF_BEGV (b);
16172
16173 if (botpos >= BUF_ZV (b))
16174 {
16175 if (toppos <= BUF_BEGV (b))
16176 return "All";
16177 else
16178 return "Bottom";
16179 }
16180 else
16181 {
16182 if (total > 1000000)
16183 /* Do it differently for a large value, to avoid overflow. */
16184 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16185 else
16186 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16187 /* We can't normally display a 3-digit number,
16188 so get us a 2-digit number that is close. */
16189 if (total == 100)
16190 total = 99;
16191 if (toppos <= BUF_BEGV (b))
16192 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16193 else
16194 sprintf (decode_mode_spec_buf, "%2d%%", total);
16195 return decode_mode_spec_buf;
16196 }
16197 }
16198
16199 case 's':
16200 /* status of process */
16201 obj = Fget_buffer_process (w->buffer);
16202 if (NILP (obj))
16203 return "no process";
16204 #ifdef subprocesses
16205 obj = Fsymbol_name (Fprocess_status (obj));
16206 #endif
16207 break;
16208
16209 case 't': /* indicate TEXT or BINARY */
16210 #ifdef MODE_LINE_BINARY_TEXT
16211 return MODE_LINE_BINARY_TEXT (b);
16212 #else
16213 return "T";
16214 #endif
16215
16216 case 'T':
16217 /* %T is the frame name on a termcap frame, the empty string otherwise. */
16218 if (! FRAME_TERMCAP_P (f))
16219 return "";
16220 if (!NILP (f->title))
16221 return (char *) SDATA (f->title);
16222 return (char *) SDATA (f->name);
16223
16224 case 'z':
16225 /* coding-system (not including end-of-line format) */
16226 case 'Z':
16227 /* coding-system (including end-of-line type) */
16228 {
16229 int eol_flag = (c == 'Z');
16230 char *p = decode_mode_spec_buf;
16231
16232 if (! FRAME_WINDOW_P (f))
16233 {
16234 /* No need to mention EOL here--the terminal never needs
16235 to do EOL conversion. */
16236 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16237 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16238 }
16239 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16240 p, eol_flag);
16241
16242 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16243 #ifdef subprocesses
16244 obj = Fget_buffer_process (Fcurrent_buffer ());
16245 if (PROCESSP (obj))
16246 {
16247 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16248 p, eol_flag);
16249 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16250 p, eol_flag);
16251 }
16252 #endif /* subprocesses */
16253 #endif /* 0 */
16254 *p = 0;
16255 return decode_mode_spec_buf;
16256 }
16257 }
16258
16259 if (STRINGP (obj))
16260 {
16261 *multibyte = STRING_MULTIBYTE (obj);
16262 return (char *) SDATA (obj);
16263 }
16264 else
16265 return "";
16266 }
16267
16268
16269 /* Count up to COUNT lines starting from START / START_BYTE.
16270 But don't go beyond LIMIT_BYTE.
16271 Return the number of lines thus found (always nonnegative).
16272
16273 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16274
16275 static int
16276 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16277 int start, start_byte, limit_byte, count;
16278 int *byte_pos_ptr;
16279 {
16280 register unsigned char *cursor;
16281 unsigned char *base;
16282
16283 register int ceiling;
16284 register unsigned char *ceiling_addr;
16285 int orig_count = count;
16286
16287 /* If we are not in selective display mode,
16288 check only for newlines. */
16289 int selective_display = (!NILP (current_buffer->selective_display)
16290 && !INTEGERP (current_buffer->selective_display));
16291
16292 if (count > 0)
16293 {
16294 while (start_byte < limit_byte)
16295 {
16296 ceiling = BUFFER_CEILING_OF (start_byte);
16297 ceiling = min (limit_byte - 1, ceiling);
16298 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16299 base = (cursor = BYTE_POS_ADDR (start_byte));
16300 while (1)
16301 {
16302 if (selective_display)
16303 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16304 ;
16305 else
16306 while (*cursor != '\n' && ++cursor != ceiling_addr)
16307 ;
16308
16309 if (cursor != ceiling_addr)
16310 {
16311 if (--count == 0)
16312 {
16313 start_byte += cursor - base + 1;
16314 *byte_pos_ptr = start_byte;
16315 return orig_count;
16316 }
16317 else
16318 if (++cursor == ceiling_addr)
16319 break;
16320 }
16321 else
16322 break;
16323 }
16324 start_byte += cursor - base;
16325 }
16326 }
16327 else
16328 {
16329 while (start_byte > limit_byte)
16330 {
16331 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16332 ceiling = max (limit_byte, ceiling);
16333 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16334 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16335 while (1)
16336 {
16337 if (selective_display)
16338 while (--cursor != ceiling_addr
16339 && *cursor != '\n' && *cursor != 015)
16340 ;
16341 else
16342 while (--cursor != ceiling_addr && *cursor != '\n')
16343 ;
16344
16345 if (cursor != ceiling_addr)
16346 {
16347 if (++count == 0)
16348 {
16349 start_byte += cursor - base + 1;
16350 *byte_pos_ptr = start_byte;
16351 /* When scanning backwards, we should
16352 not count the newline posterior to which we stop. */
16353 return - orig_count - 1;
16354 }
16355 }
16356 else
16357 break;
16358 }
16359 /* Here we add 1 to compensate for the last decrement
16360 of CURSOR, which took it past the valid range. */
16361 start_byte += cursor - base + 1;
16362 }
16363 }
16364
16365 *byte_pos_ptr = limit_byte;
16366
16367 if (count < 0)
16368 return - orig_count + count;
16369 return orig_count - count;
16370
16371 }
16372
16373
16374 \f
16375 /***********************************************************************
16376 Displaying strings
16377 ***********************************************************************/
16378
16379 /* Display a NUL-terminated string, starting with index START.
16380
16381 If STRING is non-null, display that C string. Otherwise, the Lisp
16382 string LISP_STRING is displayed.
16383
16384 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16385 FACE_STRING. Display STRING or LISP_STRING with the face at
16386 FACE_STRING_POS in FACE_STRING:
16387
16388 Display the string in the environment given by IT, but use the
16389 standard display table, temporarily.
16390
16391 FIELD_WIDTH is the minimum number of output glyphs to produce.
16392 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16393 with spaces. If STRING has more characters, more than FIELD_WIDTH
16394 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16395
16396 PRECISION is the maximum number of characters to output from
16397 STRING. PRECISION < 0 means don't truncate the string.
16398
16399 This is roughly equivalent to printf format specifiers:
16400
16401 FIELD_WIDTH PRECISION PRINTF
16402 ----------------------------------------
16403 -1 -1 %s
16404 -1 10 %.10s
16405 10 -1 %10s
16406 20 10 %20.10s
16407
16408 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16409 display them, and < 0 means obey the current buffer's value of
16410 enable_multibyte_characters.
16411
16412 Value is the number of glyphs produced. */
16413
16414 static int
16415 display_string (string, lisp_string, face_string, face_string_pos,
16416 start, it, field_width, precision, max_x, multibyte)
16417 unsigned char *string;
16418 Lisp_Object lisp_string;
16419 Lisp_Object face_string;
16420 int face_string_pos;
16421 int start;
16422 struct it *it;
16423 int field_width, precision, max_x;
16424 int multibyte;
16425 {
16426 int hpos_at_start = it->hpos;
16427 int saved_face_id = it->face_id;
16428 struct glyph_row *row = it->glyph_row;
16429
16430 /* Initialize the iterator IT for iteration over STRING beginning
16431 with index START. */
16432 reseat_to_string (it, string, lisp_string, start,
16433 precision, field_width, multibyte);
16434
16435 /* If displaying STRING, set up the face of the iterator
16436 from LISP_STRING, if that's given. */
16437 if (STRINGP (face_string))
16438 {
16439 int endptr;
16440 struct face *face;
16441
16442 it->face_id
16443 = face_at_string_position (it->w, face_string, face_string_pos,
16444 0, it->region_beg_charpos,
16445 it->region_end_charpos,
16446 &endptr, it->base_face_id, 0);
16447 face = FACE_FROM_ID (it->f, it->face_id);
16448 it->face_box_p = face->box != FACE_NO_BOX;
16449 }
16450
16451 /* Set max_x to the maximum allowed X position. Don't let it go
16452 beyond the right edge of the window. */
16453 if (max_x <= 0)
16454 max_x = it->last_visible_x;
16455 else
16456 max_x = min (max_x, it->last_visible_x);
16457
16458 /* Skip over display elements that are not visible. because IT->w is
16459 hscrolled. */
16460 if (it->current_x < it->first_visible_x)
16461 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16462 MOVE_TO_POS | MOVE_TO_X);
16463
16464 row->ascent = it->max_ascent;
16465 row->height = it->max_ascent + it->max_descent;
16466 row->phys_ascent = it->max_phys_ascent;
16467 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16468
16469 /* This condition is for the case that we are called with current_x
16470 past last_visible_x. */
16471 while (it->current_x < max_x)
16472 {
16473 int x_before, x, n_glyphs_before, i, nglyphs;
16474
16475 /* Get the next display element. */
16476 if (!get_next_display_element (it))
16477 break;
16478
16479 /* Produce glyphs. */
16480 x_before = it->current_x;
16481 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16482 PRODUCE_GLYPHS (it);
16483
16484 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16485 i = 0;
16486 x = x_before;
16487 while (i < nglyphs)
16488 {
16489 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16490
16491 if (!it->truncate_lines_p
16492 && x + glyph->pixel_width > max_x)
16493 {
16494 /* End of continued line or max_x reached. */
16495 if (CHAR_GLYPH_PADDING_P (*glyph))
16496 {
16497 /* A wide character is unbreakable. */
16498 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16499 it->current_x = x_before;
16500 }
16501 else
16502 {
16503 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16504 it->current_x = x;
16505 }
16506 break;
16507 }
16508 else if (x + glyph->pixel_width > it->first_visible_x)
16509 {
16510 /* Glyph is at least partially visible. */
16511 ++it->hpos;
16512 if (x < it->first_visible_x)
16513 it->glyph_row->x = x - it->first_visible_x;
16514 }
16515 else
16516 {
16517 /* Glyph is off the left margin of the display area.
16518 Should not happen. */
16519 abort ();
16520 }
16521
16522 row->ascent = max (row->ascent, it->max_ascent);
16523 row->height = max (row->height, it->max_ascent + it->max_descent);
16524 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16525 row->phys_height = max (row->phys_height,
16526 it->max_phys_ascent + it->max_phys_descent);
16527 x += glyph->pixel_width;
16528 ++i;
16529 }
16530
16531 /* Stop if max_x reached. */
16532 if (i < nglyphs)
16533 break;
16534
16535 /* Stop at line ends. */
16536 if (ITERATOR_AT_END_OF_LINE_P (it))
16537 {
16538 it->continuation_lines_width = 0;
16539 break;
16540 }
16541
16542 set_iterator_to_next (it, 1);
16543
16544 /* Stop if truncating at the right edge. */
16545 if (it->truncate_lines_p
16546 && it->current_x >= it->last_visible_x)
16547 {
16548 /* Add truncation mark, but don't do it if the line is
16549 truncated at a padding space. */
16550 if (IT_CHARPOS (*it) < it->string_nchars)
16551 {
16552 if (!FRAME_WINDOW_P (it->f))
16553 {
16554 int i, n;
16555
16556 if (it->current_x > it->last_visible_x)
16557 {
16558 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16559 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16560 break;
16561 for (n = row->used[TEXT_AREA]; i < n; ++i)
16562 {
16563 row->used[TEXT_AREA] = i;
16564 produce_special_glyphs (it, IT_TRUNCATION);
16565 }
16566 }
16567 produce_special_glyphs (it, IT_TRUNCATION);
16568 }
16569 it->glyph_row->truncated_on_right_p = 1;
16570 }
16571 break;
16572 }
16573 }
16574
16575 /* Maybe insert a truncation at the left. */
16576 if (it->first_visible_x
16577 && IT_CHARPOS (*it) > 0)
16578 {
16579 if (!FRAME_WINDOW_P (it->f))
16580 insert_left_trunc_glyphs (it);
16581 it->glyph_row->truncated_on_left_p = 1;
16582 }
16583
16584 it->face_id = saved_face_id;
16585
16586 /* Value is number of columns displayed. */
16587 return it->hpos - hpos_at_start;
16588 }
16589
16590
16591 \f
16592 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16593 appears as an element of LIST or as the car of an element of LIST.
16594 If PROPVAL is a list, compare each element against LIST in that
16595 way, and return 1/2 if any element of PROPVAL is found in LIST.
16596 Otherwise return 0. This function cannot quit.
16597 The return value is 2 if the text is invisible but with an ellipsis
16598 and 1 if it's invisible and without an ellipsis. */
16599
16600 int
16601 invisible_p (propval, list)
16602 register Lisp_Object propval;
16603 Lisp_Object list;
16604 {
16605 register Lisp_Object tail, proptail;
16606
16607 for (tail = list; CONSP (tail); tail = XCDR (tail))
16608 {
16609 register Lisp_Object tem;
16610 tem = XCAR (tail);
16611 if (EQ (propval, tem))
16612 return 1;
16613 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16614 return NILP (XCDR (tem)) ? 1 : 2;
16615 }
16616
16617 if (CONSP (propval))
16618 {
16619 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16620 {
16621 Lisp_Object propelt;
16622 propelt = XCAR (proptail);
16623 for (tail = list; CONSP (tail); tail = XCDR (tail))
16624 {
16625 register Lisp_Object tem;
16626 tem = XCAR (tail);
16627 if (EQ (propelt, tem))
16628 return 1;
16629 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16630 return NILP (XCDR (tem)) ? 1 : 2;
16631 }
16632 }
16633 }
16634
16635 return 0;
16636 }
16637
16638 \f
16639 /***********************************************************************
16640 Glyph Display
16641 ***********************************************************************/
16642
16643 #ifdef HAVE_WINDOW_SYSTEM
16644
16645 #if GLYPH_DEBUG
16646
16647 void
16648 dump_glyph_string (s)
16649 struct glyph_string *s;
16650 {
16651 fprintf (stderr, "glyph string\n");
16652 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16653 s->x, s->y, s->width, s->height);
16654 fprintf (stderr, " ybase = %d\n", s->ybase);
16655 fprintf (stderr, " hl = %d\n", s->hl);
16656 fprintf (stderr, " left overhang = %d, right = %d\n",
16657 s->left_overhang, s->right_overhang);
16658 fprintf (stderr, " nchars = %d\n", s->nchars);
16659 fprintf (stderr, " extends to end of line = %d\n",
16660 s->extends_to_end_of_line_p);
16661 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16662 fprintf (stderr, " bg width = %d\n", s->background_width);
16663 }
16664
16665 #endif /* GLYPH_DEBUG */
16666
16667 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16668 of XChar2b structures for S; it can't be allocated in
16669 init_glyph_string because it must be allocated via `alloca'. W
16670 is the window on which S is drawn. ROW and AREA are the glyph row
16671 and area within the row from which S is constructed. START is the
16672 index of the first glyph structure covered by S. HL is a
16673 face-override for drawing S. */
16674
16675 #ifdef HAVE_NTGUI
16676 #define OPTIONAL_HDC(hdc) hdc,
16677 #define DECLARE_HDC(hdc) HDC hdc;
16678 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16679 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16680 #endif
16681
16682 #ifndef OPTIONAL_HDC
16683 #define OPTIONAL_HDC(hdc)
16684 #define DECLARE_HDC(hdc)
16685 #define ALLOCATE_HDC(hdc, f)
16686 #define RELEASE_HDC(hdc, f)
16687 #endif
16688
16689 static void
16690 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16691 struct glyph_string *s;
16692 DECLARE_HDC (hdc)
16693 XChar2b *char2b;
16694 struct window *w;
16695 struct glyph_row *row;
16696 enum glyph_row_area area;
16697 int start;
16698 enum draw_glyphs_face hl;
16699 {
16700 bzero (s, sizeof *s);
16701 s->w = w;
16702 s->f = XFRAME (w->frame);
16703 #ifdef HAVE_NTGUI
16704 s->hdc = hdc;
16705 #endif
16706 s->display = FRAME_X_DISPLAY (s->f);
16707 s->window = FRAME_X_WINDOW (s->f);
16708 s->char2b = char2b;
16709 s->hl = hl;
16710 s->row = row;
16711 s->area = area;
16712 s->first_glyph = row->glyphs[area] + start;
16713 s->height = row->height;
16714 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16715
16716 /* Display the internal border below the tool-bar window. */
16717 if (s->w == XWINDOW (s->f->tool_bar_window))
16718 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16719
16720 s->ybase = s->y + row->ascent;
16721 }
16722
16723
16724 /* Append the list of glyph strings with head H and tail T to the list
16725 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16726
16727 static INLINE void
16728 append_glyph_string_lists (head, tail, h, t)
16729 struct glyph_string **head, **tail;
16730 struct glyph_string *h, *t;
16731 {
16732 if (h)
16733 {
16734 if (*head)
16735 (*tail)->next = h;
16736 else
16737 *head = h;
16738 h->prev = *tail;
16739 *tail = t;
16740 }
16741 }
16742
16743
16744 /* Prepend the list of glyph strings with head H and tail T to the
16745 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16746 result. */
16747
16748 static INLINE void
16749 prepend_glyph_string_lists (head, tail, h, t)
16750 struct glyph_string **head, **tail;
16751 struct glyph_string *h, *t;
16752 {
16753 if (h)
16754 {
16755 if (*head)
16756 (*head)->prev = t;
16757 else
16758 *tail = t;
16759 t->next = *head;
16760 *head = h;
16761 }
16762 }
16763
16764
16765 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16766 Set *HEAD and *TAIL to the resulting list. */
16767
16768 static INLINE void
16769 append_glyph_string (head, tail, s)
16770 struct glyph_string **head, **tail;
16771 struct glyph_string *s;
16772 {
16773 s->next = s->prev = NULL;
16774 append_glyph_string_lists (head, tail, s, s);
16775 }
16776
16777
16778 /* Get face and two-byte form of character glyph GLYPH on frame F.
16779 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16780 a pointer to a realized face that is ready for display. */
16781
16782 static INLINE struct face *
16783 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16784 struct frame *f;
16785 struct glyph *glyph;
16786 XChar2b *char2b;
16787 int *two_byte_p;
16788 {
16789 struct face *face;
16790
16791 xassert (glyph->type == CHAR_GLYPH);
16792 face = FACE_FROM_ID (f, glyph->face_id);
16793
16794 if (two_byte_p)
16795 *two_byte_p = 0;
16796
16797 if (!glyph->multibyte_p)
16798 {
16799 /* Unibyte case. We don't have to encode, but we have to make
16800 sure to use a face suitable for unibyte. */
16801 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16802 }
16803 else if (glyph->u.ch < 128
16804 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16805 {
16806 /* Case of ASCII in a face known to fit ASCII. */
16807 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16808 }
16809 else
16810 {
16811 int c1, c2, charset;
16812
16813 /* Split characters into bytes. If c2 is -1 afterwards, C is
16814 really a one-byte character so that byte1 is zero. */
16815 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16816 if (c2 > 0)
16817 STORE_XCHAR2B (char2b, c1, c2);
16818 else
16819 STORE_XCHAR2B (char2b, 0, c1);
16820
16821 /* Maybe encode the character in *CHAR2B. */
16822 if (charset != CHARSET_ASCII)
16823 {
16824 struct font_info *font_info
16825 = FONT_INFO_FROM_ID (f, face->font_info_id);
16826 if (font_info)
16827 glyph->font_type
16828 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16829 }
16830 }
16831
16832 /* Make sure X resources of the face are allocated. */
16833 xassert (face != NULL);
16834 PREPARE_FACE_FOR_DISPLAY (f, face);
16835 return face;
16836 }
16837
16838
16839 /* Fill glyph string S with composition components specified by S->cmp.
16840
16841 FACES is an array of faces for all components of this composition.
16842 S->gidx is the index of the first component for S.
16843 OVERLAPS_P non-zero means S should draw the foreground only, and
16844 use its physical height for clipping.
16845
16846 Value is the index of a component not in S. */
16847
16848 static int
16849 fill_composite_glyph_string (s, faces, overlaps_p)
16850 struct glyph_string *s;
16851 struct face **faces;
16852 int overlaps_p;
16853 {
16854 int i;
16855
16856 xassert (s);
16857
16858 s->for_overlaps_p = overlaps_p;
16859
16860 s->face = faces[s->gidx];
16861 s->font = s->face->font;
16862 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16863
16864 /* For all glyphs of this composition, starting at the offset
16865 S->gidx, until we reach the end of the definition or encounter a
16866 glyph that requires the different face, add it to S. */
16867 ++s->nchars;
16868 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16869 ++s->nchars;
16870
16871 /* All glyph strings for the same composition has the same width,
16872 i.e. the width set for the first component of the composition. */
16873
16874 s->width = s->first_glyph->pixel_width;
16875
16876 /* If the specified font could not be loaded, use the frame's
16877 default font, but record the fact that we couldn't load it in
16878 the glyph string so that we can draw rectangles for the
16879 characters of the glyph string. */
16880 if (s->font == NULL)
16881 {
16882 s->font_not_found_p = 1;
16883 s->font = FRAME_FONT (s->f);
16884 }
16885
16886 /* Adjust base line for subscript/superscript text. */
16887 s->ybase += s->first_glyph->voffset;
16888
16889 xassert (s->face && s->face->gc);
16890
16891 /* This glyph string must always be drawn with 16-bit functions. */
16892 s->two_byte_p = 1;
16893
16894 return s->gidx + s->nchars;
16895 }
16896
16897
16898 /* Fill glyph string S from a sequence of character glyphs.
16899
16900 FACE_ID is the face id of the string. START is the index of the
16901 first glyph to consider, END is the index of the last + 1.
16902 OVERLAPS_P non-zero means S should draw the foreground only, and
16903 use its physical height for clipping.
16904
16905 Value is the index of the first glyph not in S. */
16906
16907 static int
16908 fill_glyph_string (s, face_id, start, end, overlaps_p)
16909 struct glyph_string *s;
16910 int face_id;
16911 int start, end, overlaps_p;
16912 {
16913 struct glyph *glyph, *last;
16914 int voffset;
16915 int glyph_not_available_p;
16916
16917 xassert (s->f == XFRAME (s->w->frame));
16918 xassert (s->nchars == 0);
16919 xassert (start >= 0 && end > start);
16920
16921 s->for_overlaps_p = overlaps_p,
16922 glyph = s->row->glyphs[s->area] + start;
16923 last = s->row->glyphs[s->area] + end;
16924 voffset = glyph->voffset;
16925
16926 glyph_not_available_p = glyph->glyph_not_available_p;
16927
16928 while (glyph < last
16929 && glyph->type == CHAR_GLYPH
16930 && glyph->voffset == voffset
16931 /* Same face id implies same font, nowadays. */
16932 && glyph->face_id == face_id
16933 && glyph->glyph_not_available_p == glyph_not_available_p)
16934 {
16935 int two_byte_p;
16936
16937 s->face = get_glyph_face_and_encoding (s->f, glyph,
16938 s->char2b + s->nchars,
16939 &two_byte_p);
16940 s->two_byte_p = two_byte_p;
16941 ++s->nchars;
16942 xassert (s->nchars <= end - start);
16943 s->width += glyph->pixel_width;
16944 ++glyph;
16945 }
16946
16947 s->font = s->face->font;
16948 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16949
16950 /* If the specified font could not be loaded, use the frame's font,
16951 but record the fact that we couldn't load it in
16952 S->font_not_found_p so that we can draw rectangles for the
16953 characters of the glyph string. */
16954 if (s->font == NULL || glyph_not_available_p)
16955 {
16956 s->font_not_found_p = 1;
16957 s->font = FRAME_FONT (s->f);
16958 }
16959
16960 /* Adjust base line for subscript/superscript text. */
16961 s->ybase += voffset;
16962
16963 xassert (s->face && s->face->gc);
16964 return glyph - s->row->glyphs[s->area];
16965 }
16966
16967
16968 /* Fill glyph string S from image glyph S->first_glyph. */
16969
16970 static void
16971 fill_image_glyph_string (s)
16972 struct glyph_string *s;
16973 {
16974 xassert (s->first_glyph->type == IMAGE_GLYPH);
16975 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16976 xassert (s->img);
16977 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16978 s->font = s->face->font;
16979 s->width = s->first_glyph->pixel_width;
16980
16981 /* Adjust base line for subscript/superscript text. */
16982 s->ybase += s->first_glyph->voffset;
16983 }
16984
16985
16986 /* Fill glyph string S from a sequence of stretch glyphs.
16987
16988 ROW is the glyph row in which the glyphs are found, AREA is the
16989 area within the row. START is the index of the first glyph to
16990 consider, END is the index of the last + 1.
16991
16992 Value is the index of the first glyph not in S. */
16993
16994 static int
16995 fill_stretch_glyph_string (s, row, area, start, end)
16996 struct glyph_string *s;
16997 struct glyph_row *row;
16998 enum glyph_row_area area;
16999 int start, end;
17000 {
17001 struct glyph *glyph, *last;
17002 int voffset, face_id;
17003
17004 xassert (s->first_glyph->type == STRETCH_GLYPH);
17005
17006 glyph = s->row->glyphs[s->area] + start;
17007 last = s->row->glyphs[s->area] + end;
17008 face_id = glyph->face_id;
17009 s->face = FACE_FROM_ID (s->f, face_id);
17010 s->font = s->face->font;
17011 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17012 s->width = glyph->pixel_width;
17013 voffset = glyph->voffset;
17014
17015 for (++glyph;
17016 (glyph < last
17017 && glyph->type == STRETCH_GLYPH
17018 && glyph->voffset == voffset
17019 && glyph->face_id == face_id);
17020 ++glyph)
17021 s->width += glyph->pixel_width;
17022
17023 /* Adjust base line for subscript/superscript text. */
17024 s->ybase += voffset;
17025
17026 /* The case that face->gc == 0 is handled when drawing the glyph
17027 string by calling PREPARE_FACE_FOR_DISPLAY. */
17028 xassert (s->face);
17029 return glyph - s->row->glyphs[s->area];
17030 }
17031
17032
17033 /* EXPORT for RIF:
17034 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17035 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17036 assumed to be zero. */
17037
17038 void
17039 x_get_glyph_overhangs (glyph, f, left, right)
17040 struct glyph *glyph;
17041 struct frame *f;
17042 int *left, *right;
17043 {
17044 *left = *right = 0;
17045
17046 if (glyph->type == CHAR_GLYPH)
17047 {
17048 XFontStruct *font;
17049 struct face *face;
17050 struct font_info *font_info;
17051 XChar2b char2b;
17052 XCharStruct *pcm;
17053
17054 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17055 font = face->font;
17056 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17057 if (font /* ++KFS: Should this be font_info ? */
17058 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
17059 {
17060 if (pcm->rbearing > pcm->width)
17061 *right = pcm->rbearing - pcm->width;
17062 if (pcm->lbearing < 0)
17063 *left = -pcm->lbearing;
17064 }
17065 }
17066 }
17067
17068
17069 /* Return the index of the first glyph preceding glyph string S that
17070 is overwritten by S because of S's left overhang. Value is -1
17071 if no glyphs are overwritten. */
17072
17073 static int
17074 left_overwritten (s)
17075 struct glyph_string *s;
17076 {
17077 int k;
17078
17079 if (s->left_overhang)
17080 {
17081 int x = 0, i;
17082 struct glyph *glyphs = s->row->glyphs[s->area];
17083 int first = s->first_glyph - glyphs;
17084
17085 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17086 x -= glyphs[i].pixel_width;
17087
17088 k = i + 1;
17089 }
17090 else
17091 k = -1;
17092
17093 return k;
17094 }
17095
17096
17097 /* Return the index of the first glyph preceding glyph string S that
17098 is overwriting S because of its right overhang. Value is -1 if no
17099 glyph in front of S overwrites S. */
17100
17101 static int
17102 left_overwriting (s)
17103 struct glyph_string *s;
17104 {
17105 int i, k, x;
17106 struct glyph *glyphs = s->row->glyphs[s->area];
17107 int first = s->first_glyph - glyphs;
17108
17109 k = -1;
17110 x = 0;
17111 for (i = first - 1; i >= 0; --i)
17112 {
17113 int left, right;
17114 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17115 if (x + right > 0)
17116 k = i;
17117 x -= glyphs[i].pixel_width;
17118 }
17119
17120 return k;
17121 }
17122
17123
17124 /* Return the index of the last glyph following glyph string S that is
17125 not overwritten by S because of S's right overhang. Value is -1 if
17126 no such glyph is found. */
17127
17128 static int
17129 right_overwritten (s)
17130 struct glyph_string *s;
17131 {
17132 int k = -1;
17133
17134 if (s->right_overhang)
17135 {
17136 int x = 0, i;
17137 struct glyph *glyphs = s->row->glyphs[s->area];
17138 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17139 int end = s->row->used[s->area];
17140
17141 for (i = first; i < end && s->right_overhang > x; ++i)
17142 x += glyphs[i].pixel_width;
17143
17144 k = i;
17145 }
17146
17147 return k;
17148 }
17149
17150
17151 /* Return the index of the last glyph following glyph string S that
17152 overwrites S because of its left overhang. Value is negative
17153 if no such glyph is found. */
17154
17155 static int
17156 right_overwriting (s)
17157 struct glyph_string *s;
17158 {
17159 int i, k, x;
17160 int end = s->row->used[s->area];
17161 struct glyph *glyphs = s->row->glyphs[s->area];
17162 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17163
17164 k = -1;
17165 x = 0;
17166 for (i = first; i < end; ++i)
17167 {
17168 int left, right;
17169 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17170 if (x - left < 0)
17171 k = i;
17172 x += glyphs[i].pixel_width;
17173 }
17174
17175 return k;
17176 }
17177
17178
17179 /* Get face and two-byte form of character C in face FACE_ID on frame
17180 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17181 means we want to display multibyte text. DISPLAY_P non-zero means
17182 make sure that X resources for the face returned are allocated.
17183 Value is a pointer to a realized face that is ready for display if
17184 DISPLAY_P is non-zero. */
17185
17186 static INLINE struct face *
17187 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17188 struct frame *f;
17189 int c, face_id;
17190 XChar2b *char2b;
17191 int multibyte_p, display_p;
17192 {
17193 struct face *face = FACE_FROM_ID (f, face_id);
17194
17195 if (!multibyte_p)
17196 {
17197 /* Unibyte case. We don't have to encode, but we have to make
17198 sure to use a face suitable for unibyte. */
17199 STORE_XCHAR2B (char2b, 0, c);
17200 face_id = FACE_FOR_CHAR (f, face, c);
17201 face = FACE_FROM_ID (f, face_id);
17202 }
17203 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17204 {
17205 /* Case of ASCII in a face known to fit ASCII. */
17206 STORE_XCHAR2B (char2b, 0, c);
17207 }
17208 else
17209 {
17210 int c1, c2, charset;
17211
17212 /* Split characters into bytes. If c2 is -1 afterwards, C is
17213 really a one-byte character so that byte1 is zero. */
17214 SPLIT_CHAR (c, charset, c1, c2);
17215 if (c2 > 0)
17216 STORE_XCHAR2B (char2b, c1, c2);
17217 else
17218 STORE_XCHAR2B (char2b, 0, c1);
17219
17220 /* Maybe encode the character in *CHAR2B. */
17221 if (face->font != NULL)
17222 {
17223 struct font_info *font_info
17224 = FONT_INFO_FROM_ID (f, face->font_info_id);
17225 if (font_info)
17226 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
17227 }
17228 }
17229
17230 /* Make sure X resources of the face are allocated. */
17231 #ifdef HAVE_X_WINDOWS
17232 if (display_p)
17233 #endif
17234 {
17235 xassert (face != NULL);
17236 PREPARE_FACE_FOR_DISPLAY (f, face);
17237 }
17238
17239 return face;
17240 }
17241
17242
17243 /* Set background width of glyph string S. START is the index of the
17244 first glyph following S. LAST_X is the right-most x-position + 1
17245 in the drawing area. */
17246
17247 static INLINE void
17248 set_glyph_string_background_width (s, start, last_x)
17249 struct glyph_string *s;
17250 int start;
17251 int last_x;
17252 {
17253 /* If the face of this glyph string has to be drawn to the end of
17254 the drawing area, set S->extends_to_end_of_line_p. */
17255 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17256
17257 if (start == s->row->used[s->area]
17258 && s->area == TEXT_AREA
17259 && ((s->hl == DRAW_NORMAL_TEXT
17260 && (s->row->fill_line_p
17261 || s->face->background != default_face->background
17262 || s->face->stipple != default_face->stipple
17263 || s->row->mouse_face_p))
17264 || s->hl == DRAW_MOUSE_FACE
17265 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17266 && s->row->fill_line_p)))
17267 s->extends_to_end_of_line_p = 1;
17268
17269 /* If S extends its face to the end of the line, set its
17270 background_width to the distance to the right edge of the drawing
17271 area. */
17272 if (s->extends_to_end_of_line_p)
17273 s->background_width = last_x - s->x + 1;
17274 else
17275 s->background_width = s->width;
17276 }
17277
17278
17279 /* Compute overhangs and x-positions for glyph string S and its
17280 predecessors, or successors. X is the starting x-position for S.
17281 BACKWARD_P non-zero means process predecessors. */
17282
17283 static void
17284 compute_overhangs_and_x (s, x, backward_p)
17285 struct glyph_string *s;
17286 int x;
17287 int backward_p;
17288 {
17289 if (backward_p)
17290 {
17291 while (s)
17292 {
17293 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17294 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17295 x -= s->width;
17296 s->x = x;
17297 s = s->prev;
17298 }
17299 }
17300 else
17301 {
17302 while (s)
17303 {
17304 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
17305 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
17306 s->x = x;
17307 x += s->width;
17308 s = s->next;
17309 }
17310 }
17311 }
17312
17313
17314
17315 /* The following macros are only called from draw_glyphs below.
17316 They reference the following parameters of that function directly:
17317 `w', `row', `area', and `overlap_p'
17318 as well as the following local variables:
17319 `s', `f', and `hdc' (in W32) */
17320
17321 #ifdef HAVE_NTGUI
17322 /* On W32, silently add local `hdc' variable to argument list of
17323 init_glyph_string. */
17324 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17325 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17326 #else
17327 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17328 init_glyph_string (s, char2b, w, row, area, start, hl)
17329 #endif
17330
17331 /* Add a glyph string for a stretch glyph to the list of strings
17332 between HEAD and TAIL. START is the index of the stretch glyph in
17333 row area AREA of glyph row ROW. END is the index of the last glyph
17334 in that glyph row area. X is the current output position assigned
17335 to the new glyph string constructed. HL overrides that face of the
17336 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17337 is the right-most x-position of the drawing area. */
17338
17339 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17340 and below -- keep them on one line. */
17341 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17342 do \
17343 { \
17344 s = (struct glyph_string *) alloca (sizeof *s); \
17345 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17346 START = fill_stretch_glyph_string (s, row, area, START, END); \
17347 append_glyph_string (&HEAD, &TAIL, s); \
17348 s->x = (X); \
17349 } \
17350 while (0)
17351
17352
17353 /* Add a glyph string for an image glyph to the list of strings
17354 between HEAD and TAIL. START is the index of the image glyph in
17355 row area AREA of glyph row ROW. END is the index of the last glyph
17356 in that glyph row area. X is the current output position assigned
17357 to the new glyph string constructed. HL overrides that face of the
17358 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17359 is the right-most x-position of the drawing area. */
17360
17361 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17362 do \
17363 { \
17364 s = (struct glyph_string *) alloca (sizeof *s); \
17365 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17366 fill_image_glyph_string (s); \
17367 append_glyph_string (&HEAD, &TAIL, s); \
17368 ++START; \
17369 s->x = (X); \
17370 } \
17371 while (0)
17372
17373
17374 /* Add a glyph string for a sequence of character glyphs to the list
17375 of strings between HEAD and TAIL. START is the index of the first
17376 glyph in row area AREA of glyph row ROW that is part of the new
17377 glyph string. END is the index of the last glyph in that glyph row
17378 area. X is the current output position assigned to the new glyph
17379 string constructed. HL overrides that face of the glyph; e.g. it
17380 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17381 right-most x-position of the drawing area. */
17382
17383 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17384 do \
17385 { \
17386 int c, face_id; \
17387 XChar2b *char2b; \
17388 \
17389 c = (row)->glyphs[area][START].u.ch; \
17390 face_id = (row)->glyphs[area][START].face_id; \
17391 \
17392 s = (struct glyph_string *) alloca (sizeof *s); \
17393 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17394 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17395 append_glyph_string (&HEAD, &TAIL, s); \
17396 s->x = (X); \
17397 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17398 } \
17399 while (0)
17400
17401
17402 /* Add a glyph string for a composite sequence to the list of strings
17403 between HEAD and TAIL. START is the index of the first glyph in
17404 row area AREA of glyph row ROW that is part of the new glyph
17405 string. END is the index of the last glyph in that glyph row area.
17406 X is the current output position assigned to the new glyph string
17407 constructed. HL overrides that face of the glyph; e.g. it is
17408 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17409 x-position of the drawing area. */
17410
17411 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17412 do { \
17413 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17414 int face_id = (row)->glyphs[area][START].face_id; \
17415 struct face *base_face = FACE_FROM_ID (f, face_id); \
17416 struct composition *cmp = composition_table[cmp_id]; \
17417 int glyph_len = cmp->glyph_len; \
17418 XChar2b *char2b; \
17419 struct face **faces; \
17420 struct glyph_string *first_s = NULL; \
17421 int n; \
17422 \
17423 base_face = base_face->ascii_face; \
17424 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17425 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17426 /* At first, fill in `char2b' and `faces'. */ \
17427 for (n = 0; n < glyph_len; n++) \
17428 { \
17429 int c = COMPOSITION_GLYPH (cmp, n); \
17430 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17431 faces[n] = FACE_FROM_ID (f, this_face_id); \
17432 get_char_face_and_encoding (f, c, this_face_id, \
17433 char2b + n, 1, 1); \
17434 } \
17435 \
17436 /* Make glyph_strings for each glyph sequence that is drawable by \
17437 the same face, and append them to HEAD/TAIL. */ \
17438 for (n = 0; n < cmp->glyph_len;) \
17439 { \
17440 s = (struct glyph_string *) alloca (sizeof *s); \
17441 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17442 append_glyph_string (&(HEAD), &(TAIL), s); \
17443 s->cmp = cmp; \
17444 s->gidx = n; \
17445 s->x = (X); \
17446 \
17447 if (n == 0) \
17448 first_s = s; \
17449 \
17450 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17451 } \
17452 \
17453 ++START; \
17454 s = first_s; \
17455 } while (0)
17456
17457
17458 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17459 of AREA of glyph row ROW on window W between indices START and END.
17460 HL overrides the face for drawing glyph strings, e.g. it is
17461 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17462 x-positions of the drawing area.
17463
17464 This is an ugly monster macro construct because we must use alloca
17465 to allocate glyph strings (because draw_glyphs can be called
17466 asynchronously). */
17467
17468 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17469 do \
17470 { \
17471 HEAD = TAIL = NULL; \
17472 while (START < END) \
17473 { \
17474 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17475 switch (first_glyph->type) \
17476 { \
17477 case CHAR_GLYPH: \
17478 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17479 HL, X, LAST_X); \
17480 break; \
17481 \
17482 case COMPOSITE_GLYPH: \
17483 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17484 HL, X, LAST_X); \
17485 break; \
17486 \
17487 case STRETCH_GLYPH: \
17488 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17489 HL, X, LAST_X); \
17490 break; \
17491 \
17492 case IMAGE_GLYPH: \
17493 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17494 HL, X, LAST_X); \
17495 break; \
17496 \
17497 default: \
17498 abort (); \
17499 } \
17500 \
17501 set_glyph_string_background_width (s, START, LAST_X); \
17502 (X) += s->width; \
17503 } \
17504 } \
17505 while (0)
17506
17507
17508 /* Draw glyphs between START and END in AREA of ROW on window W,
17509 starting at x-position X. X is relative to AREA in W. HL is a
17510 face-override with the following meaning:
17511
17512 DRAW_NORMAL_TEXT draw normally
17513 DRAW_CURSOR draw in cursor face
17514 DRAW_MOUSE_FACE draw in mouse face.
17515 DRAW_INVERSE_VIDEO draw in mode line face
17516 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17517 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17518
17519 If OVERLAPS_P is non-zero, draw only the foreground of characters
17520 and clip to the physical height of ROW.
17521
17522 Value is the x-position reached, relative to AREA of W. */
17523
17524 static int
17525 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17526 struct window *w;
17527 int x;
17528 struct glyph_row *row;
17529 enum glyph_row_area area;
17530 int start, end;
17531 enum draw_glyphs_face hl;
17532 int overlaps_p;
17533 {
17534 struct glyph_string *head, *tail;
17535 struct glyph_string *s;
17536 int last_x, area_width;
17537 int x_reached;
17538 int i, j;
17539 struct frame *f = XFRAME (WINDOW_FRAME (w));
17540 DECLARE_HDC (hdc);
17541
17542 ALLOCATE_HDC (hdc, f);
17543
17544 /* Let's rather be paranoid than getting a SEGV. */
17545 end = min (end, row->used[area]);
17546 start = max (0, start);
17547 start = min (end, start);
17548
17549 /* Translate X to frame coordinates. Set last_x to the right
17550 end of the drawing area. */
17551 if (row->full_width_p)
17552 {
17553 /* X is relative to the left edge of W, without scroll bars
17554 or fringes. */
17555 x += WINDOW_LEFT_EDGE_X (w);
17556 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17557 }
17558 else
17559 {
17560 int area_left = window_box_left (w, area);
17561 x += area_left;
17562 area_width = window_box_width (w, area);
17563 last_x = area_left + area_width;
17564 }
17565
17566 /* Build a doubly-linked list of glyph_string structures between
17567 head and tail from what we have to draw. Note that the macro
17568 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17569 the reason we use a separate variable `i'. */
17570 i = start;
17571 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17572 if (tail)
17573 x_reached = tail->x + tail->background_width;
17574 else
17575 x_reached = x;
17576
17577 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17578 the row, redraw some glyphs in front or following the glyph
17579 strings built above. */
17580 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17581 {
17582 int dummy_x = 0;
17583 struct glyph_string *h, *t;
17584
17585 /* Compute overhangs for all glyph strings. */
17586 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
17587 for (s = head; s; s = s->next)
17588 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
17589
17590 /* Prepend glyph strings for glyphs in front of the first glyph
17591 string that are overwritten because of the first glyph
17592 string's left overhang. The background of all strings
17593 prepended must be drawn because the first glyph string
17594 draws over it. */
17595 i = left_overwritten (head);
17596 if (i >= 0)
17597 {
17598 j = i;
17599 BUILD_GLYPH_STRINGS (j, start, h, t,
17600 DRAW_NORMAL_TEXT, dummy_x, last_x);
17601 start = i;
17602 compute_overhangs_and_x (t, head->x, 1);
17603 prepend_glyph_string_lists (&head, &tail, h, t);
17604 }
17605
17606 /* Prepend glyph strings for glyphs in front of the first glyph
17607 string that overwrite that glyph string because of their
17608 right overhang. For these strings, only the foreground must
17609 be drawn, because it draws over the glyph string at `head'.
17610 The background must not be drawn because this would overwrite
17611 right overhangs of preceding glyphs for which no glyph
17612 strings exist. */
17613 i = left_overwriting (head);
17614 if (i >= 0)
17615 {
17616 BUILD_GLYPH_STRINGS (i, start, h, t,
17617 DRAW_NORMAL_TEXT, dummy_x, last_x);
17618 for (s = h; s; s = s->next)
17619 s->background_filled_p = 1;
17620 compute_overhangs_and_x (t, head->x, 1);
17621 prepend_glyph_string_lists (&head, &tail, h, t);
17622 }
17623
17624 /* Append glyphs strings for glyphs following the last glyph
17625 string tail that are overwritten by tail. The background of
17626 these strings has to be drawn because tail's foreground draws
17627 over it. */
17628 i = right_overwritten (tail);
17629 if (i >= 0)
17630 {
17631 BUILD_GLYPH_STRINGS (end, i, h, t,
17632 DRAW_NORMAL_TEXT, x, last_x);
17633 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17634 append_glyph_string_lists (&head, &tail, h, t);
17635 }
17636
17637 /* Append glyph strings for glyphs following the last glyph
17638 string tail that overwrite tail. The foreground of such
17639 glyphs has to be drawn because it writes into the background
17640 of tail. The background must not be drawn because it could
17641 paint over the foreground of following glyphs. */
17642 i = right_overwriting (tail);
17643 if (i >= 0)
17644 {
17645 BUILD_GLYPH_STRINGS (end, i, h, t,
17646 DRAW_NORMAL_TEXT, x, last_x);
17647 for (s = h; s; s = s->next)
17648 s->background_filled_p = 1;
17649 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17650 append_glyph_string_lists (&head, &tail, h, t);
17651 }
17652 }
17653
17654 /* Draw all strings. */
17655 for (s = head; s; s = s->next)
17656 FRAME_RIF (f)->draw_glyph_string (s);
17657
17658 if (area == TEXT_AREA
17659 && !row->full_width_p
17660 /* When drawing overlapping rows, only the glyph strings'
17661 foreground is drawn, which doesn't erase a cursor
17662 completely. */
17663 && !overlaps_p)
17664 {
17665 int x0 = head ? head->x : x;
17666 int x1 = tail ? tail->x + tail->background_width : x;
17667
17668 int text_left = window_box_left (w, TEXT_AREA);
17669 x0 -= text_left;
17670 x1 -= text_left;
17671
17672 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
17673 row->y, MATRIX_ROW_BOTTOM_Y (row));
17674 }
17675
17676 /* Value is the x-position up to which drawn, relative to AREA of W.
17677 This doesn't include parts drawn because of overhangs. */
17678 if (row->full_width_p)
17679 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17680 else
17681 x_reached -= window_box_left (w, area);
17682
17683 RELEASE_HDC (hdc, f);
17684
17685 return x_reached;
17686 }
17687
17688
17689 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17690 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17691
17692 static INLINE void
17693 append_glyph (it)
17694 struct it *it;
17695 {
17696 struct glyph *glyph;
17697 enum glyph_row_area area = it->area;
17698
17699 xassert (it->glyph_row);
17700 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17701
17702 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17703 if (glyph < it->glyph_row->glyphs[area + 1])
17704 {
17705 glyph->charpos = CHARPOS (it->position);
17706 glyph->object = it->object;
17707 glyph->pixel_width = it->pixel_width;
17708 glyph->ascent = it->ascent;
17709 glyph->descent = it->descent;
17710 glyph->voffset = it->voffset;
17711 glyph->type = CHAR_GLYPH;
17712 glyph->multibyte_p = it->multibyte_p;
17713 glyph->left_box_line_p = it->start_of_box_run_p;
17714 glyph->right_box_line_p = it->end_of_box_run_p;
17715 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17716 || it->phys_descent > it->descent);
17717 glyph->padding_p = 0;
17718 glyph->glyph_not_available_p = it->glyph_not_available_p;
17719 glyph->face_id = it->face_id;
17720 glyph->u.ch = it->char_to_display;
17721 glyph->font_type = FONT_TYPE_UNKNOWN;
17722 ++it->glyph_row->used[area];
17723 }
17724 }
17725
17726 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17727 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17728
17729 static INLINE void
17730 append_composite_glyph (it)
17731 struct it *it;
17732 {
17733 struct glyph *glyph;
17734 enum glyph_row_area area = it->area;
17735
17736 xassert (it->glyph_row);
17737
17738 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17739 if (glyph < it->glyph_row->glyphs[area + 1])
17740 {
17741 glyph->charpos = CHARPOS (it->position);
17742 glyph->object = it->object;
17743 glyph->pixel_width = it->pixel_width;
17744 glyph->ascent = it->ascent;
17745 glyph->descent = it->descent;
17746 glyph->voffset = it->voffset;
17747 glyph->type = COMPOSITE_GLYPH;
17748 glyph->multibyte_p = it->multibyte_p;
17749 glyph->left_box_line_p = it->start_of_box_run_p;
17750 glyph->right_box_line_p = it->end_of_box_run_p;
17751 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17752 || it->phys_descent > it->descent);
17753 glyph->padding_p = 0;
17754 glyph->glyph_not_available_p = 0;
17755 glyph->face_id = it->face_id;
17756 glyph->u.cmp_id = it->cmp_id;
17757 glyph->font_type = FONT_TYPE_UNKNOWN;
17758 ++it->glyph_row->used[area];
17759 }
17760 }
17761
17762
17763 /* Change IT->ascent and IT->height according to the setting of
17764 IT->voffset. */
17765
17766 static INLINE void
17767 take_vertical_position_into_account (it)
17768 struct it *it;
17769 {
17770 if (it->voffset)
17771 {
17772 if (it->voffset < 0)
17773 /* Increase the ascent so that we can display the text higher
17774 in the line. */
17775 it->ascent += abs (it->voffset);
17776 else
17777 /* Increase the descent so that we can display the text lower
17778 in the line. */
17779 it->descent += it->voffset;
17780 }
17781 }
17782
17783
17784 /* Produce glyphs/get display metrics for the image IT is loaded with.
17785 See the description of struct display_iterator in dispextern.h for
17786 an overview of struct display_iterator. */
17787
17788 static void
17789 produce_image_glyph (it)
17790 struct it *it;
17791 {
17792 struct image *img;
17793 struct face *face;
17794 int face_ascent, glyph_ascent;
17795
17796 xassert (it->what == IT_IMAGE);
17797
17798 face = FACE_FROM_ID (it->f, it->face_id);
17799 img = IMAGE_FROM_ID (it->f, it->image_id);
17800 xassert (img);
17801
17802 /* Make sure X resources of the face and image are loaded. */
17803 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17804 prepare_image_for_display (it->f, img);
17805
17806 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face);
17807 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17808 it->pixel_width = img->width + 2 * img->hmargin;
17809
17810 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
17811 face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
17812 if (face_ascent > it->ascent)
17813 it->ascent = it->phys_ascent = face_ascent;
17814
17815 it->nglyphs = 1;
17816
17817 if (face->box != FACE_NO_BOX)
17818 {
17819 if (face->box_line_width > 0)
17820 {
17821 it->ascent += face->box_line_width;
17822 it->descent += face->box_line_width;
17823 }
17824
17825 if (it->start_of_box_run_p)
17826 it->pixel_width += abs (face->box_line_width);
17827 if (it->end_of_box_run_p)
17828 it->pixel_width += abs (face->box_line_width);
17829 }
17830
17831 take_vertical_position_into_account (it);
17832
17833 if (it->glyph_row)
17834 {
17835 struct glyph *glyph;
17836 enum glyph_row_area area = it->area;
17837
17838 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17839 if (glyph < it->glyph_row->glyphs[area + 1])
17840 {
17841 glyph->charpos = CHARPOS (it->position);
17842 glyph->object = it->object;
17843 glyph->pixel_width = it->pixel_width;
17844 glyph->ascent = glyph_ascent;
17845 glyph->descent = it->descent;
17846 glyph->voffset = it->voffset;
17847 glyph->type = IMAGE_GLYPH;
17848 glyph->multibyte_p = it->multibyte_p;
17849 glyph->left_box_line_p = it->start_of_box_run_p;
17850 glyph->right_box_line_p = it->end_of_box_run_p;
17851 glyph->overlaps_vertically_p = 0;
17852 glyph->padding_p = 0;
17853 glyph->glyph_not_available_p = 0;
17854 glyph->face_id = it->face_id;
17855 glyph->u.img_id = img->id;
17856 glyph->font_type = FONT_TYPE_UNKNOWN;
17857 ++it->glyph_row->used[area];
17858 }
17859 }
17860 }
17861
17862
17863 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17864 of the glyph, WIDTH and HEIGHT are the width and height of the
17865 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
17866
17867 static void
17868 append_stretch_glyph (it, object, width, height, ascent)
17869 struct it *it;
17870 Lisp_Object object;
17871 int width, height;
17872 int ascent;
17873 {
17874 struct glyph *glyph;
17875 enum glyph_row_area area = it->area;
17876
17877 xassert (ascent >= 0 && ascent <= height);
17878
17879 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17880 if (glyph < it->glyph_row->glyphs[area + 1])
17881 {
17882 glyph->charpos = CHARPOS (it->position);
17883 glyph->object = object;
17884 glyph->pixel_width = width;
17885 glyph->ascent = ascent;
17886 glyph->descent = height - ascent;
17887 glyph->voffset = it->voffset;
17888 glyph->type = STRETCH_GLYPH;
17889 glyph->multibyte_p = it->multibyte_p;
17890 glyph->left_box_line_p = it->start_of_box_run_p;
17891 glyph->right_box_line_p = it->end_of_box_run_p;
17892 glyph->overlaps_vertically_p = 0;
17893 glyph->padding_p = 0;
17894 glyph->glyph_not_available_p = 0;
17895 glyph->face_id = it->face_id;
17896 glyph->u.stretch.ascent = ascent;
17897 glyph->u.stretch.height = height;
17898 glyph->font_type = FONT_TYPE_UNKNOWN;
17899 ++it->glyph_row->used[area];
17900 }
17901 }
17902
17903
17904 /* Calculate a width or height in pixels from a specification using
17905 the following elements:
17906
17907 SPEC ::=
17908 NUM - a (fractional) multiple of the default font width/height
17909 (NUM) - specifies exactly NUM pixels
17910 UNIT - a fixed number of pixels, see below.
17911 ELEMENT - size of a display element in pixels, see below.
17912 (NUM . SPEC) - equals NUM * SPEC
17913 (+ SPEC SPEC ...) - add pixel values
17914 (- SPEC SPEC ...) - subtract pixel values
17915 (- SPEC) - negate pixel value
17916
17917 NUM ::=
17918 INT or FLOAT - a number constant
17919 SYMBOL - use symbol's (buffer local) variable binding.
17920
17921 UNIT ::=
17922 in - pixels per inch *)
17923 mm - pixels per 1/1000 meter *)
17924 cm - pixels per 1/100 meter *)
17925 width - width of current font in pixels.
17926 height - height of current font in pixels.
17927
17928 *) using the ratio(s) defined in display-pixels-per-inch.
17929
17930 ELEMENT ::=
17931
17932 left-fringe - left fringe width in pixels
17933 (left-fringe . nil) - left fringe width if inside margins, else 0
17934 (left-fringe . t) - left fringe width if outside margins, else 0
17935
17936 right-fringe - right fringe width in pixels
17937 (right-fringe . nil) - right fringe width if inside margins, else 0
17938 (right-fringe . t) - right fringe width if outside margins, else 0
17939
17940 left-margin - left margin width in pixels
17941 right-margin - right margin width in pixels
17942
17943 scroll-bar - scroll-bar area width in pixels
17944 (scroll-bar . left) - scroll-bar width if on left, else 0
17945 (scroll-bar . right) - scroll-bar width if on right, else 0
17946
17947 Examples:
17948
17949 Pixels corresponding to 5 inches:
17950 (5 . in)
17951
17952 Total width of non-text areas on left side of window:
17953 (+ left-fringe left-margin (scroll-bar . left))
17954
17955 Total width of fringes if inside display margins:
17956 (+ (left-fringe) (right-fringe))
17957
17958 Width of left margin minus width of 1 character in the default font:
17959 (- left-margin 1)
17960
17961 Width of left margin minus width of 2 characters in the current font:
17962 (- left-margin (2 . width))
17963
17964 Width of left fringe plus left margin minus one pixel:
17965 (- (+ left-fringe left-margin) (1))
17966 (+ left-fringe left-margin (- (1)))
17967 (+ left-fringe left-margin (-1))
17968
17969 */
17970
17971 #define NUMVAL(X) \
17972 ((INTEGERP (X) || FLOATP (X)) \
17973 ? XFLOATINT (X) \
17974 : - 1)
17975
17976 static int
17977 calc_pixel_width_or_height (res, it, prop, font, width_p)
17978 double *res;
17979 struct it *it;
17980 Lisp_Object prop;
17981 XFontStruct *font;
17982 int width_p;
17983 {
17984 double pixels;
17985
17986 #define OK_PIXELS(val) ((*res = (val)), 1)
17987
17988 if (SYMBOLP (prop))
17989 {
17990 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17991 {
17992 char *unit = SDATA (SYMBOL_NAME (prop));
17993
17994 if (unit[0] == 'i' && unit[1] == 'n')
17995 pixels = 1.0;
17996 else if (unit[0] == 'm' && unit[1] == 'm')
17997 pixels = 25.4;
17998 else if (unit[0] == 'c' && unit[1] == 'm')
17999 pixels = 2.54;
18000 else
18001 pixels = 0;
18002 if (pixels > 0)
18003 {
18004 double ppi;
18005 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18006 || (CONSP (Vdisplay_pixels_per_inch)
18007 && (ppi = (width_p
18008 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18009 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18010 ppi > 0)))
18011 return OK_PIXELS (ppi / pixels);
18012
18013 return 0;
18014 }
18015 }
18016
18017 if (EQ (prop, Qheight))
18018 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
18019 if (EQ (prop, Qwidth))
18020 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
18021 if (EQ (prop, Qleft_fringe))
18022 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18023 if (EQ (prop, Qright_fringe))
18024 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18025 if (EQ (prop, Qleft_margin))
18026 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18027 if (EQ (prop, Qright_margin))
18028 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18029 if (EQ (prop, Qscroll_bar))
18030 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18031
18032 prop = Fbuffer_local_value (prop, it->w->buffer);
18033 }
18034
18035 if (INTEGERP (prop) || FLOATP (prop))
18036 {
18037 int base_unit = (width_p
18038 ? FRAME_COLUMN_WIDTH (it->f)
18039 : FRAME_LINE_HEIGHT (it->f));
18040 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18041 }
18042
18043 if (CONSP (prop))
18044 {
18045 Lisp_Object car = XCAR (prop);
18046 Lisp_Object cdr = XCDR (prop);
18047
18048 if (SYMBOLP (car))
18049 {
18050 if (EQ (car, Qplus) || EQ (car, Qminus))
18051 {
18052 int first = 1;
18053 double px;
18054
18055 pixels = 0;
18056 while (CONSP (cdr))
18057 {
18058 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr), font, width_p))
18059 return 0;
18060 if (first)
18061 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18062 else
18063 pixels += px;
18064 cdr = XCDR (cdr);
18065 }
18066 if (EQ (car, Qminus))
18067 pixels = -pixels;
18068 return OK_PIXELS (pixels);
18069 }
18070
18071 if (EQ (car, Qleft_fringe))
18072 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18073 == !NILP (cdr))
18074 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
18075 : 0);
18076 if (EQ (car, Qright_fringe))
18077 return OK_PIXELS ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18078 == !NILP (cdr))
18079 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18080 : 0);
18081 if (EQ (car, Qscroll_bar))
18082 return OK_PIXELS ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18083 == EQ (cdr, Qleft))
18084 ? WINDOW_SCROLL_BAR_AREA_WIDTH (it->w)
18085 : 0);
18086
18087 car = Fbuffer_local_value (car, it->w->buffer);
18088 }
18089
18090 if (INTEGERP (car) || FLOATP (car))
18091 {
18092 double fact;
18093 pixels = XFLOATINT (car);
18094 if (NILP (cdr))
18095 return OK_PIXELS (pixels);
18096 if (calc_pixel_width_or_height (&fact, it, cdr, font, width_p))
18097 return OK_PIXELS (pixels * fact);
18098 return 0;
18099 }
18100
18101 return 0;
18102 }
18103
18104 return 0;
18105 }
18106
18107 /* Produce a stretch glyph for iterator IT. IT->object is the value
18108 of the glyph property displayed. The value must be a list
18109 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18110 being recognized:
18111
18112 1. `:width WIDTH' specifies that the space should be WIDTH *
18113 canonical char width wide. WIDTH may be an integer or floating
18114 point number.
18115
18116 2. `:relative-width FACTOR' specifies that the width of the stretch
18117 should be computed from the width of the first character having the
18118 `glyph' property, and should be FACTOR times that width.
18119
18120 3. `:align-to HPOS' specifies that the space should be wide enough
18121 to reach HPOS, a value in canonical character units.
18122
18123 Exactly one of the above pairs must be present.
18124
18125 4. `:height HEIGHT' specifies that the height of the stretch produced
18126 should be HEIGHT, measured in canonical character units.
18127
18128 5. `:relative-height FACTOR' specifies that the height of the
18129 stretch should be FACTOR times the height of the characters having
18130 the glyph property.
18131
18132 Either none or exactly one of 4 or 5 must be present.
18133
18134 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18135 of the stretch should be used for the ascent of the stretch.
18136 ASCENT must be in the range 0 <= ASCENT <= 100. */
18137
18138 static void
18139 produce_stretch_glyph (it)
18140 struct it *it;
18141 {
18142 /* (space :width WIDTH :height HEIGHT ...) */
18143 Lisp_Object prop, plist;
18144 int width = 0, height = 0;
18145 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18146 int ascent = 0;
18147 double tem;
18148 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18149 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18150
18151 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18152
18153 /* List should start with `space'. */
18154 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18155 plist = XCDR (it->object);
18156
18157 /* Compute the width of the stretch. */
18158 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18159 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18160 {
18161 /* Absolute width `:width WIDTH' specified and valid. */
18162 zero_width_ok_p = 1;
18163 width = (int)tem;
18164 }
18165 else if (prop = Fplist_get (plist, QCrelative_width),
18166 NUMVAL (prop) > 0)
18167 {
18168 /* Relative width `:relative-width FACTOR' specified and valid.
18169 Compute the width of the characters having the `glyph'
18170 property. */
18171 struct it it2;
18172 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18173
18174 it2 = *it;
18175 if (it->multibyte_p)
18176 {
18177 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18178 - IT_BYTEPOS (*it));
18179 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18180 }
18181 else
18182 it2.c = *p, it2.len = 1;
18183
18184 it2.glyph_row = NULL;
18185 it2.what = IT_CHARACTER;
18186 x_produce_glyphs (&it2);
18187 width = NUMVAL (prop) * it2.pixel_width;
18188 }
18189 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18190 && calc_pixel_width_or_height (&tem, it, prop, font, 1))
18191 {
18192 width = max (0, (int)tem - it->current_x);
18193 zero_width_ok_p = 1;
18194 }
18195 else
18196 /* Nothing specified -> width defaults to canonical char width. */
18197 width = FRAME_COLUMN_WIDTH (it->f);
18198
18199 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18200 width = 1;
18201
18202 /* Compute height. */
18203 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18204 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18205 {
18206 height = (int)tem;
18207 zero_height_ok_p = 1;
18208 }
18209 else if (prop = Fplist_get (plist, QCrelative_height),
18210 NUMVAL (prop) > 0)
18211 height = FONT_HEIGHT (font) * NUMVAL (prop);
18212 else
18213 height = FONT_HEIGHT (font);
18214
18215 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18216 height = 1;
18217
18218 /* Compute percentage of height used for ascent. If
18219 `:ascent ASCENT' is present and valid, use that. Otherwise,
18220 derive the ascent from the font in use. */
18221 if (prop = Fplist_get (plist, QCascent),
18222 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18223 ascent = height * NUMVAL (prop) / 100.0;
18224 else if (!NILP (prop)
18225 && calc_pixel_width_or_height (&tem, it, prop, font, 0))
18226 ascent = min (max (0, (int)tem), height);
18227 else
18228 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18229
18230 if (width > 0 && height > 0 && it->glyph_row)
18231 {
18232 Lisp_Object object = it->stack[it->sp - 1].string;
18233 if (!STRINGP (object))
18234 object = it->w->buffer;
18235 append_stretch_glyph (it, object, width, height, ascent);
18236 }
18237
18238 it->pixel_width = width;
18239 it->ascent = it->phys_ascent = ascent;
18240 it->descent = it->phys_descent = height - it->ascent;
18241 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18242
18243 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18244 {
18245 if (face->box_line_width > 0)
18246 {
18247 it->ascent += face->box_line_width;
18248 it->descent += face->box_line_width;
18249 }
18250
18251 if (it->start_of_box_run_p)
18252 it->pixel_width += abs (face->box_line_width);
18253 if (it->end_of_box_run_p)
18254 it->pixel_width += abs (face->box_line_width);
18255 }
18256
18257 take_vertical_position_into_account (it);
18258 }
18259
18260 /* RIF:
18261 Produce glyphs/get display metrics for the display element IT is
18262 loaded with. See the description of struct display_iterator in
18263 dispextern.h for an overview of struct display_iterator. */
18264
18265 void
18266 x_produce_glyphs (it)
18267 struct it *it;
18268 {
18269 it->glyph_not_available_p = 0;
18270
18271 if (it->what == IT_CHARACTER)
18272 {
18273 XChar2b char2b;
18274 XFontStruct *font;
18275 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18276 XCharStruct *pcm;
18277 int font_not_found_p;
18278 struct font_info *font_info;
18279 int boff; /* baseline offset */
18280 /* We may change it->multibyte_p upon unibyte<->multibyte
18281 conversion. So, save the current value now and restore it
18282 later.
18283
18284 Note: It seems that we don't have to record multibyte_p in
18285 struct glyph because the character code itself tells if or
18286 not the character is multibyte. Thus, in the future, we must
18287 consider eliminating the field `multibyte_p' in the struct
18288 glyph. */
18289 int saved_multibyte_p = it->multibyte_p;
18290
18291 /* Maybe translate single-byte characters to multibyte, or the
18292 other way. */
18293 it->char_to_display = it->c;
18294 if (!ASCII_BYTE_P (it->c))
18295 {
18296 if (unibyte_display_via_language_environment
18297 && SINGLE_BYTE_CHAR_P (it->c)
18298 && (it->c >= 0240
18299 || !NILP (Vnonascii_translation_table)))
18300 {
18301 it->char_to_display = unibyte_char_to_multibyte (it->c);
18302 it->multibyte_p = 1;
18303 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18304 face = FACE_FROM_ID (it->f, it->face_id);
18305 }
18306 else if (!SINGLE_BYTE_CHAR_P (it->c)
18307 && !it->multibyte_p)
18308 {
18309 it->multibyte_p = 1;
18310 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18311 face = FACE_FROM_ID (it->f, it->face_id);
18312 }
18313 }
18314
18315 /* Get font to use. Encode IT->char_to_display. */
18316 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18317 &char2b, it->multibyte_p, 0);
18318 font = face->font;
18319
18320 /* When no suitable font found, use the default font. */
18321 font_not_found_p = font == NULL;
18322 if (font_not_found_p)
18323 {
18324 font = FRAME_FONT (it->f);
18325 boff = FRAME_BASELINE_OFFSET (it->f);
18326 font_info = NULL;
18327 }
18328 else
18329 {
18330 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18331 boff = font_info->baseline_offset;
18332 if (font_info->vertical_centering)
18333 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18334 }
18335
18336 if (it->char_to_display >= ' '
18337 && (!it->multibyte_p || it->char_to_display < 128))
18338 {
18339 /* Either unibyte or ASCII. */
18340 int stretched_p;
18341
18342 it->nglyphs = 1;
18343
18344 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18345 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18346 it->ascent = FONT_BASE (font) + boff;
18347 it->descent = FONT_DESCENT (font) - boff;
18348
18349 if (pcm)
18350 {
18351 it->phys_ascent = pcm->ascent + boff;
18352 it->phys_descent = pcm->descent - boff;
18353 it->pixel_width = pcm->width;
18354 }
18355 else
18356 {
18357 it->glyph_not_available_p = 1;
18358 it->phys_ascent = FONT_BASE (font) + boff;
18359 it->phys_descent = FONT_DESCENT (font) - boff;
18360 it->pixel_width = FONT_WIDTH (font);
18361 }
18362
18363 /* If this is a space inside a region of text with
18364 `space-width' property, change its width. */
18365 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18366 if (stretched_p)
18367 it->pixel_width *= XFLOATINT (it->space_width);
18368
18369 /* If face has a box, add the box thickness to the character
18370 height. If character has a box line to the left and/or
18371 right, add the box line width to the character's width. */
18372 if (face->box != FACE_NO_BOX)
18373 {
18374 int thick = face->box_line_width;
18375
18376 if (thick > 0)
18377 {
18378 it->ascent += thick;
18379 it->descent += thick;
18380 }
18381 else
18382 thick = -thick;
18383
18384 if (it->start_of_box_run_p)
18385 it->pixel_width += thick;
18386 if (it->end_of_box_run_p)
18387 it->pixel_width += thick;
18388 }
18389
18390 /* If face has an overline, add the height of the overline
18391 (1 pixel) and a 1 pixel margin to the character height. */
18392 if (face->overline_p)
18393 it->ascent += 2;
18394
18395 take_vertical_position_into_account (it);
18396
18397 /* If we have to actually produce glyphs, do it. */
18398 if (it->glyph_row)
18399 {
18400 if (stretched_p)
18401 {
18402 /* Translate a space with a `space-width' property
18403 into a stretch glyph. */
18404 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
18405 / FONT_HEIGHT (font));
18406 append_stretch_glyph (it, it->object, it->pixel_width,
18407 it->ascent + it->descent, ascent);
18408 }
18409 else
18410 append_glyph (it);
18411
18412 /* If characters with lbearing or rbearing are displayed
18413 in this line, record that fact in a flag of the
18414 glyph row. This is used to optimize X output code. */
18415 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18416 it->glyph_row->contains_overlapping_glyphs_p = 1;
18417 }
18418 }
18419 else if (it->char_to_display == '\n')
18420 {
18421 /* A newline has no width but we need the height of the line. */
18422 it->pixel_width = 0;
18423 it->nglyphs = 0;
18424 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18425 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18426
18427 if (face->box != FACE_NO_BOX
18428 && face->box_line_width > 0)
18429 {
18430 it->ascent += face->box_line_width;
18431 it->descent += face->box_line_width;
18432 }
18433 }
18434 else if (it->char_to_display == '\t')
18435 {
18436 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18437 int x = it->current_x + it->continuation_lines_width;
18438 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18439
18440 /* If the distance from the current position to the next tab
18441 stop is less than a canonical character width, use the
18442 tab stop after that. */
18443 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18444 next_tab_x += tab_width;
18445
18446 it->pixel_width = next_tab_x - x;
18447 it->nglyphs = 1;
18448 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18449 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18450
18451 if (it->glyph_row)
18452 {
18453 append_stretch_glyph (it, it->object, it->pixel_width,
18454 it->ascent + it->descent, it->ascent);
18455 }
18456 }
18457 else
18458 {
18459 /* A multi-byte character. Assume that the display width of the
18460 character is the width of the character multiplied by the
18461 width of the font. */
18462
18463 /* If we found a font, this font should give us the right
18464 metrics. If we didn't find a font, use the frame's
18465 default font and calculate the width of the character
18466 from the charset width; this is what old redisplay code
18467 did. */
18468
18469 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18470 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18471
18472 if (font_not_found_p || !pcm)
18473 {
18474 int charset = CHAR_CHARSET (it->char_to_display);
18475
18476 it->glyph_not_available_p = 1;
18477 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18478 * CHARSET_WIDTH (charset));
18479 it->phys_ascent = FONT_BASE (font) + boff;
18480 it->phys_descent = FONT_DESCENT (font) - boff;
18481 }
18482 else
18483 {
18484 it->pixel_width = pcm->width;
18485 it->phys_ascent = pcm->ascent + boff;
18486 it->phys_descent = pcm->descent - boff;
18487 if (it->glyph_row
18488 && (pcm->lbearing < 0
18489 || pcm->rbearing > pcm->width))
18490 it->glyph_row->contains_overlapping_glyphs_p = 1;
18491 }
18492 it->nglyphs = 1;
18493 it->ascent = FONT_BASE (font) + boff;
18494 it->descent = FONT_DESCENT (font) - boff;
18495 if (face->box != FACE_NO_BOX)
18496 {
18497 int thick = face->box_line_width;
18498
18499 if (thick > 0)
18500 {
18501 it->ascent += thick;
18502 it->descent += thick;
18503 }
18504 else
18505 thick = - thick;
18506
18507 if (it->start_of_box_run_p)
18508 it->pixel_width += thick;
18509 if (it->end_of_box_run_p)
18510 it->pixel_width += thick;
18511 }
18512
18513 /* If face has an overline, add the height of the overline
18514 (1 pixel) and a 1 pixel margin to the character height. */
18515 if (face->overline_p)
18516 it->ascent += 2;
18517
18518 take_vertical_position_into_account (it);
18519
18520 if (it->glyph_row)
18521 append_glyph (it);
18522 }
18523 it->multibyte_p = saved_multibyte_p;
18524 }
18525 else if (it->what == IT_COMPOSITION)
18526 {
18527 /* Note: A composition is represented as one glyph in the
18528 glyph matrix. There are no padding glyphs. */
18529 XChar2b char2b;
18530 XFontStruct *font;
18531 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18532 XCharStruct *pcm;
18533 int font_not_found_p;
18534 struct font_info *font_info;
18535 int boff; /* baseline offset */
18536 struct composition *cmp = composition_table[it->cmp_id];
18537
18538 /* Maybe translate single-byte characters to multibyte. */
18539 it->char_to_display = it->c;
18540 if (unibyte_display_via_language_environment
18541 && SINGLE_BYTE_CHAR_P (it->c)
18542 && (it->c >= 0240
18543 || (it->c >= 0200
18544 && !NILP (Vnonascii_translation_table))))
18545 {
18546 it->char_to_display = unibyte_char_to_multibyte (it->c);
18547 }
18548
18549 /* Get face and font to use. Encode IT->char_to_display. */
18550 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18551 face = FACE_FROM_ID (it->f, it->face_id);
18552 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18553 &char2b, it->multibyte_p, 0);
18554 font = face->font;
18555
18556 /* When no suitable font found, use the default font. */
18557 font_not_found_p = font == NULL;
18558 if (font_not_found_p)
18559 {
18560 font = FRAME_FONT (it->f);
18561 boff = FRAME_BASELINE_OFFSET (it->f);
18562 font_info = NULL;
18563 }
18564 else
18565 {
18566 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18567 boff = font_info->baseline_offset;
18568 if (font_info->vertical_centering)
18569 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18570 }
18571
18572 /* There are no padding glyphs, so there is only one glyph to
18573 produce for the composition. Important is that pixel_width,
18574 ascent and descent are the values of what is drawn by
18575 draw_glyphs (i.e. the values of the overall glyphs composed). */
18576 it->nglyphs = 1;
18577
18578 /* If we have not yet calculated pixel size data of glyphs of
18579 the composition for the current face font, calculate them
18580 now. Theoretically, we have to check all fonts for the
18581 glyphs, but that requires much time and memory space. So,
18582 here we check only the font of the first glyph. This leads
18583 to incorrect display very rarely, and C-l (recenter) can
18584 correct the display anyway. */
18585 if (cmp->font != (void *) font)
18586 {
18587 /* Ascent and descent of the font of the first character of
18588 this composition (adjusted by baseline offset). Ascent
18589 and descent of overall glyphs should not be less than
18590 them respectively. */
18591 int font_ascent = FONT_BASE (font) + boff;
18592 int font_descent = FONT_DESCENT (font) - boff;
18593 /* Bounding box of the overall glyphs. */
18594 int leftmost, rightmost, lowest, highest;
18595 int i, width, ascent, descent;
18596
18597 cmp->font = (void *) font;
18598
18599 /* Initialize the bounding box. */
18600 if (font_info
18601 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18602 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18603 {
18604 width = pcm->width;
18605 ascent = pcm->ascent;
18606 descent = pcm->descent;
18607 }
18608 else
18609 {
18610 width = FONT_WIDTH (font);
18611 ascent = FONT_BASE (font);
18612 descent = FONT_DESCENT (font);
18613 }
18614
18615 rightmost = width;
18616 lowest = - descent + boff;
18617 highest = ascent + boff;
18618 leftmost = 0;
18619
18620 if (font_info
18621 && font_info->default_ascent
18622 && CHAR_TABLE_P (Vuse_default_ascent)
18623 && !NILP (Faref (Vuse_default_ascent,
18624 make_number (it->char_to_display))))
18625 highest = font_info->default_ascent + boff;
18626
18627 /* Draw the first glyph at the normal position. It may be
18628 shifted to right later if some other glyphs are drawn at
18629 the left. */
18630 cmp->offsets[0] = 0;
18631 cmp->offsets[1] = boff;
18632
18633 /* Set cmp->offsets for the remaining glyphs. */
18634 for (i = 1; i < cmp->glyph_len; i++)
18635 {
18636 int left, right, btm, top;
18637 int ch = COMPOSITION_GLYPH (cmp, i);
18638 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18639
18640 face = FACE_FROM_ID (it->f, face_id);
18641 get_char_face_and_encoding (it->f, ch, face->id,
18642 &char2b, it->multibyte_p, 0);
18643 font = face->font;
18644 if (font == NULL)
18645 {
18646 font = FRAME_FONT (it->f);
18647 boff = FRAME_BASELINE_OFFSET (it->f);
18648 font_info = NULL;
18649 }
18650 else
18651 {
18652 font_info
18653 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18654 boff = font_info->baseline_offset;
18655 if (font_info->vertical_centering)
18656 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18657 }
18658
18659 if (font_info
18660 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
18661 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18662 {
18663 width = pcm->width;
18664 ascent = pcm->ascent;
18665 descent = pcm->descent;
18666 }
18667 else
18668 {
18669 width = FONT_WIDTH (font);
18670 ascent = 1;
18671 descent = 0;
18672 }
18673
18674 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18675 {
18676 /* Relative composition with or without
18677 alternate chars. */
18678 left = (leftmost + rightmost - width) / 2;
18679 btm = - descent + boff;
18680 if (font_info && font_info->relative_compose
18681 && (! CHAR_TABLE_P (Vignore_relative_composition)
18682 || NILP (Faref (Vignore_relative_composition,
18683 make_number (ch)))))
18684 {
18685
18686 if (- descent >= font_info->relative_compose)
18687 /* One extra pixel between two glyphs. */
18688 btm = highest + 1;
18689 else if (ascent <= 0)
18690 /* One extra pixel between two glyphs. */
18691 btm = lowest - 1 - ascent - descent;
18692 }
18693 }
18694 else
18695 {
18696 /* A composition rule is specified by an integer
18697 value that encodes global and new reference
18698 points (GREF and NREF). GREF and NREF are
18699 specified by numbers as below:
18700
18701 0---1---2 -- ascent
18702 | |
18703 | |
18704 | |
18705 9--10--11 -- center
18706 | |
18707 ---3---4---5--- baseline
18708 | |
18709 6---7---8 -- descent
18710 */
18711 int rule = COMPOSITION_RULE (cmp, i);
18712 int gref, nref, grefx, grefy, nrefx, nrefy;
18713
18714 COMPOSITION_DECODE_RULE (rule, gref, nref);
18715 grefx = gref % 3, nrefx = nref % 3;
18716 grefy = gref / 3, nrefy = nref / 3;
18717
18718 left = (leftmost
18719 + grefx * (rightmost - leftmost) / 2
18720 - nrefx * width / 2);
18721 btm = ((grefy == 0 ? highest
18722 : grefy == 1 ? 0
18723 : grefy == 2 ? lowest
18724 : (highest + lowest) / 2)
18725 - (nrefy == 0 ? ascent + descent
18726 : nrefy == 1 ? descent - boff
18727 : nrefy == 2 ? 0
18728 : (ascent + descent) / 2));
18729 }
18730
18731 cmp->offsets[i * 2] = left;
18732 cmp->offsets[i * 2 + 1] = btm + descent;
18733
18734 /* Update the bounding box of the overall glyphs. */
18735 right = left + width;
18736 top = btm + descent + ascent;
18737 if (left < leftmost)
18738 leftmost = left;
18739 if (right > rightmost)
18740 rightmost = right;
18741 if (top > highest)
18742 highest = top;
18743 if (btm < lowest)
18744 lowest = btm;
18745 }
18746
18747 /* If there are glyphs whose x-offsets are negative,
18748 shift all glyphs to the right and make all x-offsets
18749 non-negative. */
18750 if (leftmost < 0)
18751 {
18752 for (i = 0; i < cmp->glyph_len; i++)
18753 cmp->offsets[i * 2] -= leftmost;
18754 rightmost -= leftmost;
18755 }
18756
18757 cmp->pixel_width = rightmost;
18758 cmp->ascent = highest;
18759 cmp->descent = - lowest;
18760 if (cmp->ascent < font_ascent)
18761 cmp->ascent = font_ascent;
18762 if (cmp->descent < font_descent)
18763 cmp->descent = font_descent;
18764 }
18765
18766 it->pixel_width = cmp->pixel_width;
18767 it->ascent = it->phys_ascent = cmp->ascent;
18768 it->descent = it->phys_descent = cmp->descent;
18769
18770 if (face->box != FACE_NO_BOX)
18771 {
18772 int thick = face->box_line_width;
18773
18774 if (thick > 0)
18775 {
18776 it->ascent += thick;
18777 it->descent += thick;
18778 }
18779 else
18780 thick = - thick;
18781
18782 if (it->start_of_box_run_p)
18783 it->pixel_width += thick;
18784 if (it->end_of_box_run_p)
18785 it->pixel_width += thick;
18786 }
18787
18788 /* If face has an overline, add the height of the overline
18789 (1 pixel) and a 1 pixel margin to the character height. */
18790 if (face->overline_p)
18791 it->ascent += 2;
18792
18793 take_vertical_position_into_account (it);
18794
18795 if (it->glyph_row)
18796 append_composite_glyph (it);
18797 }
18798 else if (it->what == IT_IMAGE)
18799 produce_image_glyph (it);
18800 else if (it->what == IT_STRETCH)
18801 produce_stretch_glyph (it);
18802
18803 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18804 because this isn't true for images with `:ascent 100'. */
18805 xassert (it->ascent >= 0 && it->descent >= 0);
18806 if (it->area == TEXT_AREA)
18807 it->current_x += it->pixel_width;
18808
18809 it->descent += it->extra_line_spacing;
18810
18811 it->max_ascent = max (it->max_ascent, it->ascent);
18812 it->max_descent = max (it->max_descent, it->descent);
18813 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18814 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18815 }
18816
18817 /* EXPORT for RIF:
18818 Output LEN glyphs starting at START at the nominal cursor position.
18819 Advance the nominal cursor over the text. The global variable
18820 updated_window contains the window being updated, updated_row is
18821 the glyph row being updated, and updated_area is the area of that
18822 row being updated. */
18823
18824 void
18825 x_write_glyphs (start, len)
18826 struct glyph *start;
18827 int len;
18828 {
18829 int x, hpos;
18830
18831 xassert (updated_window && updated_row);
18832 BLOCK_INPUT;
18833
18834 /* Write glyphs. */
18835
18836 hpos = start - updated_row->glyphs[updated_area];
18837 x = draw_glyphs (updated_window, output_cursor.x,
18838 updated_row, updated_area,
18839 hpos, hpos + len,
18840 DRAW_NORMAL_TEXT, 0);
18841
18842 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18843 if (updated_area == TEXT_AREA
18844 && updated_window->phys_cursor_on_p
18845 && updated_window->phys_cursor.vpos == output_cursor.vpos
18846 && updated_window->phys_cursor.hpos >= hpos
18847 && updated_window->phys_cursor.hpos < hpos + len)
18848 updated_window->phys_cursor_on_p = 0;
18849
18850 UNBLOCK_INPUT;
18851
18852 /* Advance the output cursor. */
18853 output_cursor.hpos += len;
18854 output_cursor.x = x;
18855 }
18856
18857
18858 /* EXPORT for RIF:
18859 Insert LEN glyphs from START at the nominal cursor position. */
18860
18861 void
18862 x_insert_glyphs (start, len)
18863 struct glyph *start;
18864 int len;
18865 {
18866 struct frame *f;
18867 struct window *w;
18868 int line_height, shift_by_width, shifted_region_width;
18869 struct glyph_row *row;
18870 struct glyph *glyph;
18871 int frame_x, frame_y, hpos;
18872
18873 xassert (updated_window && updated_row);
18874 BLOCK_INPUT;
18875 w = updated_window;
18876 f = XFRAME (WINDOW_FRAME (w));
18877
18878 /* Get the height of the line we are in. */
18879 row = updated_row;
18880 line_height = row->height;
18881
18882 /* Get the width of the glyphs to insert. */
18883 shift_by_width = 0;
18884 for (glyph = start; glyph < start + len; ++glyph)
18885 shift_by_width += glyph->pixel_width;
18886
18887 /* Get the width of the region to shift right. */
18888 shifted_region_width = (window_box_width (w, updated_area)
18889 - output_cursor.x
18890 - shift_by_width);
18891
18892 /* Shift right. */
18893 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18894 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18895
18896 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18897 line_height, shift_by_width);
18898
18899 /* Write the glyphs. */
18900 hpos = start - row->glyphs[updated_area];
18901 draw_glyphs (w, output_cursor.x, row, updated_area,
18902 hpos, hpos + len,
18903 DRAW_NORMAL_TEXT, 0);
18904
18905 /* Advance the output cursor. */
18906 output_cursor.hpos += len;
18907 output_cursor.x += shift_by_width;
18908 UNBLOCK_INPUT;
18909 }
18910
18911
18912 /* EXPORT for RIF:
18913 Erase the current text line from the nominal cursor position
18914 (inclusive) to pixel column TO_X (exclusive). The idea is that
18915 everything from TO_X onward is already erased.
18916
18917 TO_X is a pixel position relative to updated_area of
18918 updated_window. TO_X == -1 means clear to the end of this area. */
18919
18920 void
18921 x_clear_end_of_line (to_x)
18922 int to_x;
18923 {
18924 struct frame *f;
18925 struct window *w = updated_window;
18926 int max_x, min_y, max_y;
18927 int from_x, from_y, to_y;
18928
18929 xassert (updated_window && updated_row);
18930 f = XFRAME (w->frame);
18931
18932 if (updated_row->full_width_p)
18933 max_x = WINDOW_TOTAL_WIDTH (w);
18934 else
18935 max_x = window_box_width (w, updated_area);
18936 max_y = window_text_bottom_y (w);
18937
18938 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18939 of window. For TO_X > 0, truncate to end of drawing area. */
18940 if (to_x == 0)
18941 return;
18942 else if (to_x < 0)
18943 to_x = max_x;
18944 else
18945 to_x = min (to_x, max_x);
18946
18947 to_y = min (max_y, output_cursor.y + updated_row->height);
18948
18949 /* Notice if the cursor will be cleared by this operation. */
18950 if (!updated_row->full_width_p)
18951 notice_overwritten_cursor (w, updated_area,
18952 output_cursor.x, -1,
18953 updated_row->y,
18954 MATRIX_ROW_BOTTOM_Y (updated_row));
18955
18956 from_x = output_cursor.x;
18957
18958 /* Translate to frame coordinates. */
18959 if (updated_row->full_width_p)
18960 {
18961 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18962 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18963 }
18964 else
18965 {
18966 int area_left = window_box_left (w, updated_area);
18967 from_x += area_left;
18968 to_x += area_left;
18969 }
18970
18971 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18972 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18973 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18974
18975 /* Prevent inadvertently clearing to end of the X window. */
18976 if (to_x > from_x && to_y > from_y)
18977 {
18978 BLOCK_INPUT;
18979 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
18980 to_x - from_x, to_y - from_y);
18981 UNBLOCK_INPUT;
18982 }
18983 }
18984
18985 #endif /* HAVE_WINDOW_SYSTEM */
18986
18987
18988 \f
18989 /***********************************************************************
18990 Cursor types
18991 ***********************************************************************/
18992
18993 /* Value is the internal representation of the specified cursor type
18994 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18995 of the bar cursor. */
18996
18997 static enum text_cursor_kinds
18998 get_specified_cursor_type (arg, width)
18999 Lisp_Object arg;
19000 int *width;
19001 {
19002 enum text_cursor_kinds type;
19003
19004 if (NILP (arg))
19005 return NO_CURSOR;
19006
19007 if (EQ (arg, Qbox))
19008 return FILLED_BOX_CURSOR;
19009
19010 if (EQ (arg, Qhollow))
19011 return HOLLOW_BOX_CURSOR;
19012
19013 if (EQ (arg, Qbar))
19014 {
19015 *width = 2;
19016 return BAR_CURSOR;
19017 }
19018
19019 if (CONSP (arg)
19020 && EQ (XCAR (arg), Qbar)
19021 && INTEGERP (XCDR (arg))
19022 && XINT (XCDR (arg)) >= 0)
19023 {
19024 *width = XINT (XCDR (arg));
19025 return BAR_CURSOR;
19026 }
19027
19028 if (EQ (arg, Qhbar))
19029 {
19030 *width = 2;
19031 return HBAR_CURSOR;
19032 }
19033
19034 if (CONSP (arg)
19035 && EQ (XCAR (arg), Qhbar)
19036 && INTEGERP (XCDR (arg))
19037 && XINT (XCDR (arg)) >= 0)
19038 {
19039 *width = XINT (XCDR (arg));
19040 return HBAR_CURSOR;
19041 }
19042
19043 /* Treat anything unknown as "hollow box cursor".
19044 It was bad to signal an error; people have trouble fixing
19045 .Xdefaults with Emacs, when it has something bad in it. */
19046 type = HOLLOW_BOX_CURSOR;
19047
19048 return type;
19049 }
19050
19051 /* Set the default cursor types for specified frame. */
19052 void
19053 set_frame_cursor_types (f, arg)
19054 struct frame *f;
19055 Lisp_Object arg;
19056 {
19057 int width;
19058 Lisp_Object tem;
19059
19060 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19061 FRAME_CURSOR_WIDTH (f) = width;
19062
19063 /* By default, set up the blink-off state depending on the on-state. */
19064
19065 tem = Fassoc (arg, Vblink_cursor_alist);
19066 if (!NILP (tem))
19067 {
19068 FRAME_BLINK_OFF_CURSOR (f)
19069 = get_specified_cursor_type (XCDR (tem), &width);
19070 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19071 }
19072 else
19073 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19074 }
19075
19076
19077 /* Return the cursor we want to be displayed in window W. Return
19078 width of bar/hbar cursor through WIDTH arg. Return with
19079 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19080 (i.e. if the `system caret' should track this cursor).
19081
19082 In a mini-buffer window, we want the cursor only to appear if we
19083 are reading input from this window. For the selected window, we
19084 want the cursor type given by the frame parameter or buffer local
19085 setting of cursor-type. If explicitly marked off, draw no cursor.
19086 In all other cases, we want a hollow box cursor. */
19087
19088 static enum text_cursor_kinds
19089 get_window_cursor_type (w, glyph, width, active_cursor)
19090 struct window *w;
19091 struct glyph *glyph;
19092 int *width;
19093 int *active_cursor;
19094 {
19095 struct frame *f = XFRAME (w->frame);
19096 struct buffer *b = XBUFFER (w->buffer);
19097 int cursor_type = DEFAULT_CURSOR;
19098 Lisp_Object alt_cursor;
19099 int non_selected = 0;
19100
19101 *active_cursor = 1;
19102
19103 /* Echo area */
19104 if (cursor_in_echo_area
19105 && FRAME_HAS_MINIBUF_P (f)
19106 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19107 {
19108 if (w == XWINDOW (echo_area_window))
19109 {
19110 *width = FRAME_CURSOR_WIDTH (f);
19111 return FRAME_DESIRED_CURSOR (f);
19112 }
19113
19114 *active_cursor = 0;
19115 non_selected = 1;
19116 }
19117
19118 /* Nonselected window or nonselected frame. */
19119 else if (w != XWINDOW (f->selected_window)
19120 #ifdef HAVE_WINDOW_SYSTEM
19121 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19122 #endif
19123 )
19124 {
19125 *active_cursor = 0;
19126
19127 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19128 return NO_CURSOR;
19129
19130 non_selected = 1;
19131 }
19132
19133 /* Never display a cursor in a window in which cursor-type is nil. */
19134 if (NILP (b->cursor_type))
19135 return NO_CURSOR;
19136
19137 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19138 if (non_selected)
19139 {
19140 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19141 return get_specified_cursor_type (alt_cursor, width);
19142 }
19143
19144 /* Get the normal cursor type for this window. */
19145 if (EQ (b->cursor_type, Qt))
19146 {
19147 cursor_type = FRAME_DESIRED_CURSOR (f);
19148 *width = FRAME_CURSOR_WIDTH (f);
19149 }
19150 else
19151 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19152
19153 /* Use normal cursor if not blinked off. */
19154 if (!w->cursor_off_p)
19155 {
19156 if (glyph->type == IMAGE_GLYPH) {
19157 if (cursor_type == FILLED_BOX_CURSOR)
19158 cursor_type = HOLLOW_BOX_CURSOR;
19159 }
19160 return cursor_type;
19161 }
19162
19163 /* Cursor is blinked off, so determine how to "toggle" it. */
19164
19165 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19166 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19167 return get_specified_cursor_type (XCDR (alt_cursor), width);
19168
19169 /* Then see if frame has specified a specific blink off cursor type. */
19170 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19171 {
19172 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19173 return FRAME_BLINK_OFF_CURSOR (f);
19174 }
19175
19176 #if 0
19177 /* Some people liked having a permanently visible blinking cursor,
19178 while others had very strong opinions against it. So it was
19179 decided to remove it. KFS 2003-09-03 */
19180
19181 /* Finally perform built-in cursor blinking:
19182 filled box <-> hollow box
19183 wide [h]bar <-> narrow [h]bar
19184 narrow [h]bar <-> no cursor
19185 other type <-> no cursor */
19186
19187 if (cursor_type == FILLED_BOX_CURSOR)
19188 return HOLLOW_BOX_CURSOR;
19189
19190 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19191 {
19192 *width = 1;
19193 return cursor_type;
19194 }
19195 #endif
19196
19197 return NO_CURSOR;
19198 }
19199
19200
19201 #ifdef HAVE_WINDOW_SYSTEM
19202
19203 /* Notice when the text cursor of window W has been completely
19204 overwritten by a drawing operation that outputs glyphs in AREA
19205 starting at X0 and ending at X1 in the line starting at Y0 and
19206 ending at Y1. X coordinates are area-relative. X1 < 0 means all
19207 the rest of the line after X0 has been written. Y coordinates
19208 are window-relative. */
19209
19210 static void
19211 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
19212 struct window *w;
19213 enum glyph_row_area area;
19214 int x0, y0, x1, y1;
19215 {
19216 if (area == TEXT_AREA && w->phys_cursor_on_p)
19217 {
19218 int cx0 = w->phys_cursor.x;
19219 int cx1 = cx0 + w->phys_cursor_width;
19220 int cy0 = w->phys_cursor.y;
19221 int cy1 = cy0 + w->phys_cursor_height;
19222
19223 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
19224 {
19225 /* The cursor image will be completely removed from the
19226 screen if the output area intersects the cursor area in
19227 y-direction. When we draw in [y0 y1[, and some part of
19228 the cursor is at y < y0, that part must have been drawn
19229 before. When scrolling, the cursor is erased before
19230 actually scrolling, so we don't come here. When not
19231 scrolling, the rows above the old cursor row must have
19232 changed, and in this case these rows must have written
19233 over the cursor image.
19234
19235 Likewise if part of the cursor is below y1, with the
19236 exception of the cursor being in the first blank row at
19237 the buffer and window end because update_text_area
19238 doesn't draw that row. (Except when it does, but
19239 that's handled in update_text_area.) */
19240
19241 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
19242 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
19243 w->phys_cursor_on_p = 0;
19244 }
19245 }
19246 }
19247
19248 #endif /* HAVE_WINDOW_SYSTEM */
19249
19250 \f
19251 /************************************************************************
19252 Mouse Face
19253 ************************************************************************/
19254
19255 #ifdef HAVE_WINDOW_SYSTEM
19256
19257 /* EXPORT for RIF:
19258 Fix the display of area AREA of overlapping row ROW in window W. */
19259
19260 void
19261 x_fix_overlapping_area (w, row, area)
19262 struct window *w;
19263 struct glyph_row *row;
19264 enum glyph_row_area area;
19265 {
19266 int i, x;
19267
19268 BLOCK_INPUT;
19269
19270 x = 0;
19271 for (i = 0; i < row->used[area];)
19272 {
19273 if (row->glyphs[area][i].overlaps_vertically_p)
19274 {
19275 int start = i, start_x = x;
19276
19277 do
19278 {
19279 x += row->glyphs[area][i].pixel_width;
19280 ++i;
19281 }
19282 while (i < row->used[area]
19283 && row->glyphs[area][i].overlaps_vertically_p);
19284
19285 draw_glyphs (w, start_x, row, area,
19286 start, i,
19287 DRAW_NORMAL_TEXT, 1);
19288 }
19289 else
19290 {
19291 x += row->glyphs[area][i].pixel_width;
19292 ++i;
19293 }
19294 }
19295
19296 UNBLOCK_INPUT;
19297 }
19298
19299
19300 /* EXPORT:
19301 Draw the cursor glyph of window W in glyph row ROW. See the
19302 comment of draw_glyphs for the meaning of HL. */
19303
19304 void
19305 draw_phys_cursor_glyph (w, row, hl)
19306 struct window *w;
19307 struct glyph_row *row;
19308 enum draw_glyphs_face hl;
19309 {
19310 /* If cursor hpos is out of bounds, don't draw garbage. This can
19311 happen in mini-buffer windows when switching between echo area
19312 glyphs and mini-buffer. */
19313 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
19314 {
19315 int on_p = w->phys_cursor_on_p;
19316 int x1;
19317 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
19318 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
19319 hl, 0);
19320 w->phys_cursor_on_p = on_p;
19321
19322 if (hl == DRAW_CURSOR)
19323 w->phys_cursor_width = x1 - w->phys_cursor.x;
19324 /* When we erase the cursor, and ROW is overlapped by other
19325 rows, make sure that these overlapping parts of other rows
19326 are redrawn. */
19327 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
19328 {
19329 if (row > w->current_matrix->rows
19330 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
19331 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
19332
19333 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
19334 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
19335 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19336 }
19337 }
19338 }
19339
19340
19341 /* EXPORT:
19342 Erase the image of a cursor of window W from the screen. */
19343
19344 void
19345 erase_phys_cursor (w)
19346 struct window *w;
19347 {
19348 struct frame *f = XFRAME (w->frame);
19349 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19350 int hpos = w->phys_cursor.hpos;
19351 int vpos = w->phys_cursor.vpos;
19352 int mouse_face_here_p = 0;
19353 struct glyph_matrix *active_glyphs = w->current_matrix;
19354 struct glyph_row *cursor_row;
19355 struct glyph *cursor_glyph;
19356 enum draw_glyphs_face hl;
19357
19358 /* No cursor displayed or row invalidated => nothing to do on the
19359 screen. */
19360 if (w->phys_cursor_type == NO_CURSOR)
19361 goto mark_cursor_off;
19362
19363 /* VPOS >= active_glyphs->nrows means that window has been resized.
19364 Don't bother to erase the cursor. */
19365 if (vpos >= active_glyphs->nrows)
19366 goto mark_cursor_off;
19367
19368 /* If row containing cursor is marked invalid, there is nothing we
19369 can do. */
19370 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19371 if (!cursor_row->enabled_p)
19372 goto mark_cursor_off;
19373
19374 /* If row is completely invisible, don't attempt to delete a cursor which
19375 isn't there. This can happen if cursor is at top of a window, and
19376 we switch to a buffer with a header line in that window. */
19377 if (cursor_row->visible_height <= 0)
19378 goto mark_cursor_off;
19379
19380 /* This can happen when the new row is shorter than the old one.
19381 In this case, either draw_glyphs or clear_end_of_line
19382 should have cleared the cursor. Note that we wouldn't be
19383 able to erase the cursor in this case because we don't have a
19384 cursor glyph at hand. */
19385 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19386 goto mark_cursor_off;
19387
19388 /* If the cursor is in the mouse face area, redisplay that when
19389 we clear the cursor. */
19390 if (! NILP (dpyinfo->mouse_face_window)
19391 && w == XWINDOW (dpyinfo->mouse_face_window)
19392 && (vpos > dpyinfo->mouse_face_beg_row
19393 || (vpos == dpyinfo->mouse_face_beg_row
19394 && hpos >= dpyinfo->mouse_face_beg_col))
19395 && (vpos < dpyinfo->mouse_face_end_row
19396 || (vpos == dpyinfo->mouse_face_end_row
19397 && hpos < dpyinfo->mouse_face_end_col))
19398 /* Don't redraw the cursor's spot in mouse face if it is at the
19399 end of a line (on a newline). The cursor appears there, but
19400 mouse highlighting does not. */
19401 && cursor_row->used[TEXT_AREA] > hpos)
19402 mouse_face_here_p = 1;
19403
19404 /* Maybe clear the display under the cursor. */
19405 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19406 {
19407 int x, y;
19408 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19409
19410 cursor_glyph = get_phys_cursor_glyph (w);
19411 if (cursor_glyph == NULL)
19412 goto mark_cursor_off;
19413
19414 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19415 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19416
19417 FRAME_RIF (f)->clear_frame_area (f, x, y,
19418 cursor_glyph->pixel_width, cursor_row->visible_height);
19419 }
19420
19421 /* Erase the cursor by redrawing the character underneath it. */
19422 if (mouse_face_here_p)
19423 hl = DRAW_MOUSE_FACE;
19424 else
19425 hl = DRAW_NORMAL_TEXT;
19426 draw_phys_cursor_glyph (w, cursor_row, hl);
19427
19428 mark_cursor_off:
19429 w->phys_cursor_on_p = 0;
19430 w->phys_cursor_type = NO_CURSOR;
19431 }
19432
19433
19434 /* EXPORT:
19435 Display or clear cursor of window W. If ON is zero, clear the
19436 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19437 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19438
19439 void
19440 display_and_set_cursor (w, on, hpos, vpos, x, y)
19441 struct window *w;
19442 int on, hpos, vpos, x, y;
19443 {
19444 struct frame *f = XFRAME (w->frame);
19445 int new_cursor_type;
19446 int new_cursor_width;
19447 int active_cursor;
19448 struct glyph_matrix *current_glyphs;
19449 struct glyph_row *glyph_row;
19450 struct glyph *glyph;
19451
19452 /* This is pointless on invisible frames, and dangerous on garbaged
19453 windows and frames; in the latter case, the frame or window may
19454 be in the midst of changing its size, and x and y may be off the
19455 window. */
19456 if (! FRAME_VISIBLE_P (f)
19457 || FRAME_GARBAGED_P (f)
19458 || vpos >= w->current_matrix->nrows
19459 || hpos >= w->current_matrix->matrix_w)
19460 return;
19461
19462 /* If cursor is off and we want it off, return quickly. */
19463 if (!on && !w->phys_cursor_on_p)
19464 return;
19465
19466 current_glyphs = w->current_matrix;
19467 glyph_row = MATRIX_ROW (current_glyphs, vpos);
19468 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19469
19470 /* If cursor row is not enabled, we don't really know where to
19471 display the cursor. */
19472 if (!glyph_row->enabled_p)
19473 {
19474 w->phys_cursor_on_p = 0;
19475 return;
19476 }
19477
19478 xassert (interrupt_input_blocked);
19479
19480 /* Set new_cursor_type to the cursor we want to be displayed. */
19481 new_cursor_type = get_window_cursor_type (w, glyph,
19482 &new_cursor_width, &active_cursor);
19483
19484 /* If cursor is currently being shown and we don't want it to be or
19485 it is in the wrong place, or the cursor type is not what we want,
19486 erase it. */
19487 if (w->phys_cursor_on_p
19488 && (!on
19489 || w->phys_cursor.x != x
19490 || w->phys_cursor.y != y
19491 || new_cursor_type != w->phys_cursor_type
19492 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19493 && new_cursor_width != w->phys_cursor_width)))
19494 erase_phys_cursor (w);
19495
19496 /* Don't check phys_cursor_on_p here because that flag is only set
19497 to zero in some cases where we know that the cursor has been
19498 completely erased, to avoid the extra work of erasing the cursor
19499 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19500 still not be visible, or it has only been partly erased. */
19501 if (on)
19502 {
19503 w->phys_cursor_ascent = glyph_row->ascent;
19504 w->phys_cursor_height = glyph_row->height;
19505
19506 /* Set phys_cursor_.* before x_draw_.* is called because some
19507 of them may need the information. */
19508 w->phys_cursor.x = x;
19509 w->phys_cursor.y = glyph_row->y;
19510 w->phys_cursor.hpos = hpos;
19511 w->phys_cursor.vpos = vpos;
19512 }
19513
19514 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
19515 new_cursor_type, new_cursor_width,
19516 on, active_cursor);
19517 }
19518
19519
19520 /* Switch the display of W's cursor on or off, according to the value
19521 of ON. */
19522
19523 static void
19524 update_window_cursor (w, on)
19525 struct window *w;
19526 int on;
19527 {
19528 /* Don't update cursor in windows whose frame is in the process
19529 of being deleted. */
19530 if (w->current_matrix)
19531 {
19532 BLOCK_INPUT;
19533 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19534 w->phys_cursor.x, w->phys_cursor.y);
19535 UNBLOCK_INPUT;
19536 }
19537 }
19538
19539
19540 /* Call update_window_cursor with parameter ON_P on all leaf windows
19541 in the window tree rooted at W. */
19542
19543 static void
19544 update_cursor_in_window_tree (w, on_p)
19545 struct window *w;
19546 int on_p;
19547 {
19548 while (w)
19549 {
19550 if (!NILP (w->hchild))
19551 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19552 else if (!NILP (w->vchild))
19553 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19554 else
19555 update_window_cursor (w, on_p);
19556
19557 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19558 }
19559 }
19560
19561
19562 /* EXPORT:
19563 Display the cursor on window W, or clear it, according to ON_P.
19564 Don't change the cursor's position. */
19565
19566 void
19567 x_update_cursor (f, on_p)
19568 struct frame *f;
19569 int on_p;
19570 {
19571 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19572 }
19573
19574
19575 /* EXPORT:
19576 Clear the cursor of window W to background color, and mark the
19577 cursor as not shown. This is used when the text where the cursor
19578 is is about to be rewritten. */
19579
19580 void
19581 x_clear_cursor (w)
19582 struct window *w;
19583 {
19584 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19585 update_window_cursor (w, 0);
19586 }
19587
19588
19589 /* EXPORT:
19590 Display the active region described by mouse_face_* according to DRAW. */
19591
19592 void
19593 show_mouse_face (dpyinfo, draw)
19594 Display_Info *dpyinfo;
19595 enum draw_glyphs_face draw;
19596 {
19597 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19598 struct frame *f = XFRAME (WINDOW_FRAME (w));
19599
19600 if (/* If window is in the process of being destroyed, don't bother
19601 to do anything. */
19602 w->current_matrix != NULL
19603 /* Don't update mouse highlight if hidden */
19604 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19605 /* Recognize when we are called to operate on rows that don't exist
19606 anymore. This can happen when a window is split. */
19607 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19608 {
19609 int phys_cursor_on_p = w->phys_cursor_on_p;
19610 struct glyph_row *row, *first, *last;
19611
19612 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19613 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19614
19615 for (row = first; row <= last && row->enabled_p; ++row)
19616 {
19617 int start_hpos, end_hpos, start_x;
19618
19619 /* For all but the first row, the highlight starts at column 0. */
19620 if (row == first)
19621 {
19622 start_hpos = dpyinfo->mouse_face_beg_col;
19623 start_x = dpyinfo->mouse_face_beg_x;
19624 }
19625 else
19626 {
19627 start_hpos = 0;
19628 start_x = 0;
19629 }
19630
19631 if (row == last)
19632 end_hpos = dpyinfo->mouse_face_end_col;
19633 else
19634 end_hpos = row->used[TEXT_AREA];
19635
19636 if (end_hpos > start_hpos)
19637 {
19638 draw_glyphs (w, start_x, row, TEXT_AREA,
19639 start_hpos, end_hpos,
19640 draw, 0);
19641
19642 row->mouse_face_p
19643 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19644 }
19645 }
19646
19647 /* When we've written over the cursor, arrange for it to
19648 be displayed again. */
19649 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19650 {
19651 BLOCK_INPUT;
19652 display_and_set_cursor (w, 1,
19653 w->phys_cursor.hpos, w->phys_cursor.vpos,
19654 w->phys_cursor.x, w->phys_cursor.y);
19655 UNBLOCK_INPUT;
19656 }
19657 }
19658
19659 /* Change the mouse cursor. */
19660 if (draw == DRAW_NORMAL_TEXT)
19661 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19662 else if (draw == DRAW_MOUSE_FACE)
19663 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19664 else
19665 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19666 }
19667
19668 /* EXPORT:
19669 Clear out the mouse-highlighted active region.
19670 Redraw it un-highlighted first. Value is non-zero if mouse
19671 face was actually drawn unhighlighted. */
19672
19673 int
19674 clear_mouse_face (dpyinfo)
19675 Display_Info *dpyinfo;
19676 {
19677 int cleared = 0;
19678
19679 if (!NILP (dpyinfo->mouse_face_window))
19680 {
19681 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19682 cleared = 1;
19683 }
19684
19685 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19686 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19687 dpyinfo->mouse_face_window = Qnil;
19688 dpyinfo->mouse_face_overlay = Qnil;
19689 return cleared;
19690 }
19691
19692
19693 /* EXPORT:
19694 Non-zero if physical cursor of window W is within mouse face. */
19695
19696 int
19697 cursor_in_mouse_face_p (w)
19698 struct window *w;
19699 {
19700 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19701 int in_mouse_face = 0;
19702
19703 if (WINDOWP (dpyinfo->mouse_face_window)
19704 && XWINDOW (dpyinfo->mouse_face_window) == w)
19705 {
19706 int hpos = w->phys_cursor.hpos;
19707 int vpos = w->phys_cursor.vpos;
19708
19709 if (vpos >= dpyinfo->mouse_face_beg_row
19710 && vpos <= dpyinfo->mouse_face_end_row
19711 && (vpos > dpyinfo->mouse_face_beg_row
19712 || hpos >= dpyinfo->mouse_face_beg_col)
19713 && (vpos < dpyinfo->mouse_face_end_row
19714 || hpos < dpyinfo->mouse_face_end_col
19715 || dpyinfo->mouse_face_past_end))
19716 in_mouse_face = 1;
19717 }
19718
19719 return in_mouse_face;
19720 }
19721
19722
19723
19724 \f
19725 /* Find the glyph matrix position of buffer position CHARPOS in window
19726 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19727 current glyphs must be up to date. If CHARPOS is above window
19728 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19729 of last line in W. In the row containing CHARPOS, stop before glyphs
19730 having STOP as object. */
19731
19732 #if 1 /* This is a version of fast_find_position that's more correct
19733 in the presence of hscrolling, for example. I didn't install
19734 it right away because the problem fixed is minor, it failed
19735 in 20.x as well, and I think it's too risky to install
19736 so near the release of 21.1. 2001-09-25 gerd. */
19737
19738 static int
19739 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19740 struct window *w;
19741 int charpos;
19742 int *hpos, *vpos, *x, *y;
19743 Lisp_Object stop;
19744 {
19745 struct glyph_row *row, *first;
19746 struct glyph *glyph, *end;
19747 int past_end = 0;
19748
19749 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19750 row = row_containing_pos (w, charpos, first, NULL, 0);
19751 if (row == NULL)
19752 {
19753 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19754 {
19755 *x = *y = *hpos = *vpos = 0;
19756 return 0;
19757 }
19758 else
19759 {
19760 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19761 past_end = 1;
19762 }
19763 }
19764
19765 *x = row->x;
19766 *y = row->y;
19767 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19768
19769 glyph = row->glyphs[TEXT_AREA];
19770 end = glyph + row->used[TEXT_AREA];
19771
19772 /* Skip over glyphs not having an object at the start of the row.
19773 These are special glyphs like truncation marks on terminal
19774 frames. */
19775 if (row->displays_text_p)
19776 while (glyph < end
19777 && INTEGERP (glyph->object)
19778 && !EQ (stop, glyph->object)
19779 && glyph->charpos < 0)
19780 {
19781 *x += glyph->pixel_width;
19782 ++glyph;
19783 }
19784
19785 while (glyph < end
19786 && !INTEGERP (glyph->object)
19787 && !EQ (stop, glyph->object)
19788 && (!BUFFERP (glyph->object)
19789 || glyph->charpos < charpos))
19790 {
19791 *x += glyph->pixel_width;
19792 ++glyph;
19793 }
19794
19795 *hpos = glyph - row->glyphs[TEXT_AREA];
19796 return past_end;
19797 }
19798
19799 #else /* not 1 */
19800
19801 static int
19802 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19803 struct window *w;
19804 int pos;
19805 int *hpos, *vpos, *x, *y;
19806 Lisp_Object stop;
19807 {
19808 int i;
19809 int lastcol;
19810 int maybe_next_line_p = 0;
19811 int line_start_position;
19812 int yb = window_text_bottom_y (w);
19813 struct glyph_row *row, *best_row;
19814 int row_vpos, best_row_vpos;
19815 int current_x;
19816
19817 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19818 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19819
19820 while (row->y < yb)
19821 {
19822 if (row->used[TEXT_AREA])
19823 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19824 else
19825 line_start_position = 0;
19826
19827 if (line_start_position > pos)
19828 break;
19829 /* If the position sought is the end of the buffer,
19830 don't include the blank lines at the bottom of the window. */
19831 else if (line_start_position == pos
19832 && pos == BUF_ZV (XBUFFER (w->buffer)))
19833 {
19834 maybe_next_line_p = 1;
19835 break;
19836 }
19837 else if (line_start_position > 0)
19838 {
19839 best_row = row;
19840 best_row_vpos = row_vpos;
19841 }
19842
19843 if (row->y + row->height >= yb)
19844 break;
19845
19846 ++row;
19847 ++row_vpos;
19848 }
19849
19850 /* Find the right column within BEST_ROW. */
19851 lastcol = 0;
19852 current_x = best_row->x;
19853 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19854 {
19855 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19856 int charpos = glyph->charpos;
19857
19858 if (BUFFERP (glyph->object))
19859 {
19860 if (charpos == pos)
19861 {
19862 *hpos = i;
19863 *vpos = best_row_vpos;
19864 *x = current_x;
19865 *y = best_row->y;
19866 return 1;
19867 }
19868 else if (charpos > pos)
19869 break;
19870 }
19871 else if (EQ (glyph->object, stop))
19872 break;
19873
19874 if (charpos > 0)
19875 lastcol = i;
19876 current_x += glyph->pixel_width;
19877 }
19878
19879 /* If we're looking for the end of the buffer,
19880 and we didn't find it in the line we scanned,
19881 use the start of the following line. */
19882 if (maybe_next_line_p)
19883 {
19884 ++best_row;
19885 ++best_row_vpos;
19886 lastcol = 0;
19887 current_x = best_row->x;
19888 }
19889
19890 *vpos = best_row_vpos;
19891 *hpos = lastcol + 1;
19892 *x = current_x;
19893 *y = best_row->y;
19894 return 0;
19895 }
19896
19897 #endif /* not 1 */
19898
19899
19900 /* Find the position of the glyph for position POS in OBJECT in
19901 window W's current matrix, and return in *X, *Y the pixel
19902 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19903
19904 RIGHT_P non-zero means return the position of the right edge of the
19905 glyph, RIGHT_P zero means return the left edge position.
19906
19907 If no glyph for POS exists in the matrix, return the position of
19908 the glyph with the next smaller position that is in the matrix, if
19909 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19910 exists in the matrix, return the position of the glyph with the
19911 next larger position in OBJECT.
19912
19913 Value is non-zero if a glyph was found. */
19914
19915 static int
19916 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19917 struct window *w;
19918 int pos;
19919 Lisp_Object object;
19920 int *hpos, *vpos, *x, *y;
19921 int right_p;
19922 {
19923 int yb = window_text_bottom_y (w);
19924 struct glyph_row *r;
19925 struct glyph *best_glyph = NULL;
19926 struct glyph_row *best_row = NULL;
19927 int best_x = 0;
19928
19929 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19930 r->enabled_p && r->y < yb;
19931 ++r)
19932 {
19933 struct glyph *g = r->glyphs[TEXT_AREA];
19934 struct glyph *e = g + r->used[TEXT_AREA];
19935 int gx;
19936
19937 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19938 if (EQ (g->object, object))
19939 {
19940 if (g->charpos == pos)
19941 {
19942 best_glyph = g;
19943 best_x = gx;
19944 best_row = r;
19945 goto found;
19946 }
19947 else if (best_glyph == NULL
19948 || ((abs (g->charpos - pos)
19949 < abs (best_glyph->charpos - pos))
19950 && (right_p
19951 ? g->charpos < pos
19952 : g->charpos > pos)))
19953 {
19954 best_glyph = g;
19955 best_x = gx;
19956 best_row = r;
19957 }
19958 }
19959 }
19960
19961 found:
19962
19963 if (best_glyph)
19964 {
19965 *x = best_x;
19966 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19967
19968 if (right_p)
19969 {
19970 *x += best_glyph->pixel_width;
19971 ++*hpos;
19972 }
19973
19974 *y = best_row->y;
19975 *vpos = best_row - w->current_matrix->rows;
19976 }
19977
19978 return best_glyph != NULL;
19979 }
19980
19981
19982 /* See if position X, Y is within a hot-spot of an image. */
19983
19984 static int
19985 on_hot_spot_p (hot_spot, x, y)
19986 Lisp_Object hot_spot;
19987 int x, y;
19988 {
19989 if (!CONSP (hot_spot))
19990 return 0;
19991
19992 if (EQ (XCAR (hot_spot), Qrect))
19993 {
19994 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
19995 Lisp_Object rect = XCDR (hot_spot);
19996 Lisp_Object tem;
19997 if (!CONSP (rect))
19998 return 0;
19999 if (!CONSP (XCAR (rect)))
20000 return 0;
20001 if (!CONSP (XCDR (rect)))
20002 return 0;
20003 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20004 return 0;
20005 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20006 return 0;
20007 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20008 return 0;
20009 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20010 return 0;
20011 return 1;
20012 }
20013 else if (EQ (XCAR (hot_spot), Qcircle))
20014 {
20015 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20016 Lisp_Object circ = XCDR (hot_spot);
20017 Lisp_Object lr, lx0, ly0;
20018 if (CONSP (circ)
20019 && CONSP (XCAR (circ))
20020 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20021 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20022 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20023 {
20024 double r = XFLOATINT (lr);
20025 double dx = XINT (lx0) - x;
20026 double dy = XINT (ly0) - y;
20027 return (dx * dx + dy * dy <= r * r);
20028 }
20029 }
20030 else if (EQ (XCAR (hot_spot), Qpoly))
20031 {
20032 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20033 if (VECTORP (XCDR (hot_spot)))
20034 {
20035 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20036 Lisp_Object *poly = v->contents;
20037 int n = v->size;
20038 int i;
20039 int inside = 0;
20040 Lisp_Object lx, ly;
20041 int x0, y0;
20042
20043 /* Need an even number of coordinates, and at least 3 edges. */
20044 if (n < 6 || n & 1)
20045 return 0;
20046
20047 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20048 If count is odd, we are inside polygon. Pixels on edges
20049 may or may not be included depending on actual geometry of the
20050 polygon. */
20051 if ((lx = poly[n-2], !INTEGERP (lx))
20052 || (ly = poly[n-1], !INTEGERP (lx)))
20053 return 0;
20054 x0 = XINT (lx), y0 = XINT (ly);
20055 for (i = 0; i < n; i += 2)
20056 {
20057 int x1 = x0, y1 = y0;
20058 if ((lx = poly[i], !INTEGERP (lx))
20059 || (ly = poly[i+1], !INTEGERP (ly)))
20060 return 0;
20061 x0 = XINT (lx), y0 = XINT (ly);
20062
20063 /* Does this segment cross the X line? */
20064 if (x0 >= x)
20065 {
20066 if (x1 >= x)
20067 continue;
20068 }
20069 else if (x1 < x)
20070 continue;
20071 if (y > y0 && y > y1)
20072 continue;
20073 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20074 inside = !inside;
20075 }
20076 return inside;
20077 }
20078 }
20079 else
20080 return 0;
20081 }
20082
20083 Lisp_Object
20084 find_hot_spot (map, x, y)
20085 Lisp_Object map;
20086 int x, y;
20087 {
20088 while (CONSP (map))
20089 {
20090 if (CONSP (XCAR (map))
20091 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20092 return XCAR (map);
20093 map = XCDR (map);
20094 }
20095
20096 return Qnil;
20097 }
20098
20099 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20100 3, 3, 0,
20101 doc: /* Lookup in image map MAP coordinates X and Y.
20102 An image map is an alist where each element has the format (AREA ID PLIST).
20103 An AREA is specified as either a rectangle, a circle, or a polygon:
20104 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20105 pixel coordinates of the upper left and bottom right corners.
20106 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20107 and the radius of the circle; r may be a float or integer.
20108 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20109 vector describes one corner in the polygon.
20110 Returns the alist element for the first matching AREA in MAP. */)
20111 (map, x, y)
20112 Lisp_Object map;
20113 Lisp_Object x, y;
20114 {
20115 int ix, iy;
20116 if (NILP (map))
20117 return Qnil;
20118
20119 if (!INTEGERP (x))
20120 wrong_type_argument (Qintegerp, x);
20121 if (!INTEGERP (y))
20122 wrong_type_argument (Qintegerp, y);
20123
20124 return find_hot_spot (map, XINT (x), XINT (y));
20125 }
20126
20127
20128 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20129 static void
20130 define_frame_cursor1 (f, cursor, pointer)
20131 struct frame *f;
20132 Cursor cursor;
20133 Lisp_Object pointer;
20134 {
20135 if (!NILP (pointer))
20136 {
20137 if (EQ (pointer, Qarrow))
20138 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20139 else if (EQ (pointer, Qhand))
20140 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20141 else if (EQ (pointer, Qtext))
20142 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20143 else if (EQ (pointer, intern ("hdrag")))
20144 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20145 #ifdef HAVE_X_WINDOWS
20146 else if (EQ (pointer, intern ("vdrag")))
20147 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20148 #endif
20149 else if (EQ (pointer, intern ("hourglass")))
20150 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20151 else if (EQ (pointer, Qmodeline))
20152 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20153 else
20154 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20155 }
20156
20157 #ifndef HAVE_CARBON
20158 if (cursor != No_Cursor)
20159 #else
20160 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20161 #endif
20162 FRAME_RIF (f)->define_frame_cursor (f, cursor);
20163 }
20164
20165 /* Take proper action when mouse has moved to the mode or header line
20166 or marginal area AREA of window W, x-position X and y-position Y.
20167 X is relative to the start of the text display area of W, so the
20168 width of bitmap areas and scroll bars must be subtracted to get a
20169 position relative to the start of the mode line. */
20170
20171 static void
20172 note_mode_line_or_margin_highlight (w, x, y, area)
20173 struct window *w;
20174 int x, y;
20175 enum window_part area;
20176 {
20177 struct frame *f = XFRAME (w->frame);
20178 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20179 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20180 Lisp_Object pointer = Qnil;
20181 int charpos, dx, dy, width, height;
20182 Lisp_Object string, object = Qnil;
20183 Lisp_Object pos, help, image;
20184
20185 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
20186 string = mode_line_string (w, area, &x, &y, &charpos,
20187 &object, &dx, &dy, &width, &height);
20188 else
20189 {
20190 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
20191 string = marginal_area_string (w, area, &x, &y, &charpos,
20192 &object, &dx, &dy, &width, &height);
20193 }
20194
20195 help = Qnil;
20196
20197 if (IMAGEP (object))
20198 {
20199 Lisp_Object image_map, hotspot;
20200 if ((image_map = Fplist_get (XCDR (object), QCmap),
20201 !NILP (image_map))
20202 && (hotspot = find_hot_spot (image_map, dx, dy),
20203 CONSP (hotspot))
20204 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20205 {
20206 Lisp_Object area_id, plist;
20207
20208 area_id = XCAR (hotspot);
20209 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20210 If so, we could look for mouse-enter, mouse-leave
20211 properties in PLIST (and do something...). */
20212 if ((plist = XCDR (hotspot), CONSP (plist)))
20213 {
20214 pointer = Fplist_get (plist, Qpointer);
20215 if (NILP (pointer))
20216 pointer = Qhand;
20217 help = Fplist_get (plist, Qhelp_echo);
20218 if (!NILP (help))
20219 {
20220 help_echo_string = help;
20221 /* Is this correct? ++kfs */
20222 XSETWINDOW (help_echo_window, w);
20223 help_echo_object = w->buffer;
20224 help_echo_pos = charpos;
20225 }
20226 }
20227 if (NILP (pointer))
20228 pointer = Fplist_get (XCDR (object), QCpointer);
20229 }
20230 }
20231
20232 if (STRINGP (string))
20233 {
20234 pos = make_number (charpos);
20235 /* If we're on a string with `help-echo' text property, arrange
20236 for the help to be displayed. This is done by setting the
20237 global variable help_echo_string to the help string. */
20238 help = Fget_text_property (pos, Qhelp_echo, string);
20239 if (!NILP (help))
20240 {
20241 help_echo_string = help;
20242 XSETWINDOW (help_echo_window, w);
20243 help_echo_object = string;
20244 help_echo_pos = charpos;
20245 }
20246
20247 if (NILP (pointer))
20248 pointer = Fget_text_property (pos, Qpointer, string);
20249
20250 /* Change the mouse pointer according to what is under X/Y. */
20251 if (NILP (pointer) && area == ON_MODE_LINE)
20252 {
20253 Lisp_Object map;
20254 map = Fget_text_property (pos, Qlocal_map, string);
20255 if (!KEYMAPP (map))
20256 map = Fget_text_property (pos, Qkeymap, string);
20257 if (!KEYMAPP (map))
20258 cursor = dpyinfo->vertical_scroll_bar_cursor;
20259 }
20260 }
20261
20262 define_frame_cursor1 (f, cursor, pointer);
20263 }
20264
20265
20266 /* EXPORT:
20267 Take proper action when the mouse has moved to position X, Y on
20268 frame F as regards highlighting characters that have mouse-face
20269 properties. Also de-highlighting chars where the mouse was before.
20270 X and Y can be negative or out of range. */
20271
20272 void
20273 note_mouse_highlight (f, x, y)
20274 struct frame *f;
20275 int x, y;
20276 {
20277 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20278 enum window_part part;
20279 Lisp_Object window;
20280 struct window *w;
20281 Cursor cursor = No_Cursor;
20282 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
20283 struct buffer *b;
20284
20285 /* When a menu is active, don't highlight because this looks odd. */
20286 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
20287 if (popup_activated ())
20288 return;
20289 #endif
20290
20291 if (NILP (Vmouse_highlight)
20292 || !f->glyphs_initialized_p)
20293 return;
20294
20295 dpyinfo->mouse_face_mouse_x = x;
20296 dpyinfo->mouse_face_mouse_y = y;
20297 dpyinfo->mouse_face_mouse_frame = f;
20298
20299 if (dpyinfo->mouse_face_defer)
20300 return;
20301
20302 if (gc_in_progress)
20303 {
20304 dpyinfo->mouse_face_deferred_gc = 1;
20305 return;
20306 }
20307
20308 /* Which window is that in? */
20309 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
20310
20311 /* If we were displaying active text in another window, clear that. */
20312 if (! EQ (window, dpyinfo->mouse_face_window))
20313 clear_mouse_face (dpyinfo);
20314
20315 /* Not on a window -> return. */
20316 if (!WINDOWP (window))
20317 return;
20318
20319 /* Reset help_echo_string. It will get recomputed below. */
20320 help_echo_string = Qnil;
20321
20322 /* Convert to window-relative pixel coordinates. */
20323 w = XWINDOW (window);
20324 frame_to_window_pixel_xy (w, &x, &y);
20325
20326 /* Handle tool-bar window differently since it doesn't display a
20327 buffer. */
20328 if (EQ (window, f->tool_bar_window))
20329 {
20330 note_tool_bar_highlight (f, x, y);
20331 return;
20332 }
20333
20334 /* Mouse is on the mode, header line or margin? */
20335 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
20336 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
20337 {
20338 note_mode_line_or_margin_highlight (w, x, y, part);
20339 return;
20340 }
20341
20342 if (part == ON_VERTICAL_BORDER)
20343 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20344 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE)
20345 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20346 else
20347 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20348
20349 /* Are we in a window whose display is up to date?
20350 And verify the buffer's text has not changed. */
20351 b = XBUFFER (w->buffer);
20352 if (part == ON_TEXT
20353 && EQ (w->window_end_valid, w->buffer)
20354 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
20355 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
20356 {
20357 int hpos, vpos, pos, i, dx, dy, area;
20358 struct glyph *glyph;
20359 Lisp_Object object;
20360 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
20361 Lisp_Object *overlay_vec = NULL;
20362 int len, noverlays;
20363 struct buffer *obuf;
20364 int obegv, ozv, same_region;
20365
20366 /* Find the glyph under X/Y. */
20367 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
20368
20369 /* Look for :pointer property on image. */
20370 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
20371 {
20372 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
20373 if (img != NULL && IMAGEP (img->spec))
20374 {
20375 Lisp_Object image_map, hotspot;
20376 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
20377 !NILP (image_map))
20378 && (hotspot = find_hot_spot (image_map, dx, dy),
20379 CONSP (hotspot))
20380 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
20381 {
20382 Lisp_Object area_id, plist;
20383
20384 area_id = XCAR (hotspot);
20385 /* Could check AREA_ID to see if we enter/leave this hot-spot.
20386 If so, we could look for mouse-enter, mouse-leave
20387 properties in PLIST (and do something...). */
20388 if ((plist = XCDR (hotspot), CONSP (plist)))
20389 {
20390 pointer = Fplist_get (plist, Qpointer);
20391 if (NILP (pointer))
20392 pointer = Qhand;
20393 help_echo_string = Fplist_get (plist, Qhelp_echo);
20394 if (!NILP (help_echo_string))
20395 {
20396 help_echo_window = window;
20397 help_echo_object = glyph->object;
20398 help_echo_pos = glyph->charpos;
20399 }
20400 }
20401 }
20402 if (NILP (pointer))
20403 pointer = Fplist_get (XCDR (img->spec), QCpointer);
20404 }
20405 }
20406
20407 /* Clear mouse face if X/Y not over text. */
20408 if (glyph == NULL
20409 || area != TEXT_AREA
20410 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
20411 {
20412 if (clear_mouse_face (dpyinfo))
20413 cursor = No_Cursor;
20414 if (NILP (pointer))
20415 {
20416 if (area != TEXT_AREA)
20417 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20418 else
20419 pointer = Vvoid_text_area_pointer;
20420 }
20421 goto set_cursor;
20422 }
20423
20424 pos = glyph->charpos;
20425 object = glyph->object;
20426 if (!STRINGP (object) && !BUFFERP (object))
20427 goto set_cursor;
20428
20429 /* If we get an out-of-range value, return now; avoid an error. */
20430 if (BUFFERP (object) && pos > BUF_Z (b))
20431 goto set_cursor;
20432
20433 /* Make the window's buffer temporarily current for
20434 overlays_at and compute_char_face. */
20435 obuf = current_buffer;
20436 current_buffer = b;
20437 obegv = BEGV;
20438 ozv = ZV;
20439 BEGV = BEG;
20440 ZV = Z;
20441
20442 /* Is this char mouse-active or does it have help-echo? */
20443 position = make_number (pos);
20444
20445 if (BUFFERP (object))
20446 {
20447 /* Put all the overlays we want in a vector in overlay_vec.
20448 Store the length in len. If there are more than 10, make
20449 enough space for all, and try again. */
20450 len = 10;
20451 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20452 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
20453 if (noverlays > len)
20454 {
20455 len = noverlays;
20456 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
20457 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
20458 }
20459
20460 /* Sort overlays into increasing priority order. */
20461 noverlays = sort_overlays (overlay_vec, noverlays, w);
20462 }
20463 else
20464 noverlays = 0;
20465
20466 same_region = (EQ (window, dpyinfo->mouse_face_window)
20467 && vpos >= dpyinfo->mouse_face_beg_row
20468 && vpos <= dpyinfo->mouse_face_end_row
20469 && (vpos > dpyinfo->mouse_face_beg_row
20470 || hpos >= dpyinfo->mouse_face_beg_col)
20471 && (vpos < dpyinfo->mouse_face_end_row
20472 || hpos < dpyinfo->mouse_face_end_col
20473 || dpyinfo->mouse_face_past_end));
20474
20475 if (same_region)
20476 cursor = No_Cursor;
20477
20478 /* Check mouse-face highlighting. */
20479 if (! same_region
20480 /* If there exists an overlay with mouse-face overlapping
20481 the one we are currently highlighting, we have to
20482 check if we enter the overlapping overlay, and then
20483 highlight only that. */
20484 || (OVERLAYP (dpyinfo->mouse_face_overlay)
20485 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
20486 {
20487 /* Find the highest priority overlay that has a mouse-face
20488 property. */
20489 overlay = Qnil;
20490 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
20491 {
20492 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
20493 if (!NILP (mouse_face))
20494 overlay = overlay_vec[i];
20495 }
20496
20497 /* If we're actually highlighting the same overlay as
20498 before, there's no need to do that again. */
20499 if (!NILP (overlay)
20500 && EQ (overlay, dpyinfo->mouse_face_overlay))
20501 goto check_help_echo;
20502
20503 dpyinfo->mouse_face_overlay = overlay;
20504
20505 /* Clear the display of the old active region, if any. */
20506 if (clear_mouse_face (dpyinfo))
20507 cursor = No_Cursor;
20508
20509 /* If no overlay applies, get a text property. */
20510 if (NILP (overlay))
20511 mouse_face = Fget_text_property (position, Qmouse_face, object);
20512
20513 /* Handle the overlay case. */
20514 if (!NILP (overlay))
20515 {
20516 /* Find the range of text around this char that
20517 should be active. */
20518 Lisp_Object before, after;
20519 int ignore;
20520
20521 before = Foverlay_start (overlay);
20522 after = Foverlay_end (overlay);
20523 /* Record this as the current active region. */
20524 fast_find_position (w, XFASTINT (before),
20525 &dpyinfo->mouse_face_beg_col,
20526 &dpyinfo->mouse_face_beg_row,
20527 &dpyinfo->mouse_face_beg_x,
20528 &dpyinfo->mouse_face_beg_y, Qnil);
20529
20530 dpyinfo->mouse_face_past_end
20531 = !fast_find_position (w, XFASTINT (after),
20532 &dpyinfo->mouse_face_end_col,
20533 &dpyinfo->mouse_face_end_row,
20534 &dpyinfo->mouse_face_end_x,
20535 &dpyinfo->mouse_face_end_y, Qnil);
20536 dpyinfo->mouse_face_window = window;
20537
20538 dpyinfo->mouse_face_face_id
20539 = face_at_buffer_position (w, pos, 0, 0,
20540 &ignore, pos + 1,
20541 !dpyinfo->mouse_face_hidden);
20542
20543 /* Display it as active. */
20544 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20545 cursor = No_Cursor;
20546 }
20547 /* Handle the text property case. */
20548 else if (!NILP (mouse_face) && BUFFERP (object))
20549 {
20550 /* Find the range of text around this char that
20551 should be active. */
20552 Lisp_Object before, after, beginning, end;
20553 int ignore;
20554
20555 beginning = Fmarker_position (w->start);
20556 end = make_number (BUF_Z (XBUFFER (object))
20557 - XFASTINT (w->window_end_pos));
20558 before
20559 = Fprevious_single_property_change (make_number (pos + 1),
20560 Qmouse_face,
20561 object, beginning);
20562 after
20563 = Fnext_single_property_change (position, Qmouse_face,
20564 object, end);
20565
20566 /* Record this as the current active region. */
20567 fast_find_position (w, XFASTINT (before),
20568 &dpyinfo->mouse_face_beg_col,
20569 &dpyinfo->mouse_face_beg_row,
20570 &dpyinfo->mouse_face_beg_x,
20571 &dpyinfo->mouse_face_beg_y, Qnil);
20572 dpyinfo->mouse_face_past_end
20573 = !fast_find_position (w, XFASTINT (after),
20574 &dpyinfo->mouse_face_end_col,
20575 &dpyinfo->mouse_face_end_row,
20576 &dpyinfo->mouse_face_end_x,
20577 &dpyinfo->mouse_face_end_y, Qnil);
20578 dpyinfo->mouse_face_window = window;
20579
20580 if (BUFFERP (object))
20581 dpyinfo->mouse_face_face_id
20582 = face_at_buffer_position (w, pos, 0, 0,
20583 &ignore, pos + 1,
20584 !dpyinfo->mouse_face_hidden);
20585
20586 /* Display it as active. */
20587 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20588 cursor = No_Cursor;
20589 }
20590 else if (!NILP (mouse_face) && STRINGP (object))
20591 {
20592 Lisp_Object b, e;
20593 int ignore;
20594
20595 b = Fprevious_single_property_change (make_number (pos + 1),
20596 Qmouse_face,
20597 object, Qnil);
20598 e = Fnext_single_property_change (position, Qmouse_face,
20599 object, Qnil);
20600 if (NILP (b))
20601 b = make_number (0);
20602 if (NILP (e))
20603 e = make_number (SCHARS (object) - 1);
20604 fast_find_string_pos (w, XINT (b), object,
20605 &dpyinfo->mouse_face_beg_col,
20606 &dpyinfo->mouse_face_beg_row,
20607 &dpyinfo->mouse_face_beg_x,
20608 &dpyinfo->mouse_face_beg_y, 0);
20609 fast_find_string_pos (w, XINT (e), object,
20610 &dpyinfo->mouse_face_end_col,
20611 &dpyinfo->mouse_face_end_row,
20612 &dpyinfo->mouse_face_end_x,
20613 &dpyinfo->mouse_face_end_y, 1);
20614 dpyinfo->mouse_face_past_end = 0;
20615 dpyinfo->mouse_face_window = window;
20616 dpyinfo->mouse_face_face_id
20617 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
20618 glyph->face_id, 1);
20619 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20620 cursor = No_Cursor;
20621 }
20622 else if (STRINGP (object) && NILP (mouse_face))
20623 {
20624 /* A string which doesn't have mouse-face, but
20625 the text ``under'' it might have. */
20626 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
20627 int start = MATRIX_ROW_START_CHARPOS (r);
20628
20629 pos = string_buffer_position (w, object, start);
20630 if (pos > 0)
20631 mouse_face = get_char_property_and_overlay (make_number (pos),
20632 Qmouse_face,
20633 w->buffer,
20634 &overlay);
20635 if (!NILP (mouse_face) && !NILP (overlay))
20636 {
20637 Lisp_Object before = Foverlay_start (overlay);
20638 Lisp_Object after = Foverlay_end (overlay);
20639 int ignore;
20640
20641 /* Note that we might not be able to find position
20642 BEFORE in the glyph matrix if the overlay is
20643 entirely covered by a `display' property. In
20644 this case, we overshoot. So let's stop in
20645 the glyph matrix before glyphs for OBJECT. */
20646 fast_find_position (w, XFASTINT (before),
20647 &dpyinfo->mouse_face_beg_col,
20648 &dpyinfo->mouse_face_beg_row,
20649 &dpyinfo->mouse_face_beg_x,
20650 &dpyinfo->mouse_face_beg_y,
20651 object);
20652
20653 dpyinfo->mouse_face_past_end
20654 = !fast_find_position (w, XFASTINT (after),
20655 &dpyinfo->mouse_face_end_col,
20656 &dpyinfo->mouse_face_end_row,
20657 &dpyinfo->mouse_face_end_x,
20658 &dpyinfo->mouse_face_end_y,
20659 Qnil);
20660 dpyinfo->mouse_face_window = window;
20661 dpyinfo->mouse_face_face_id
20662 = face_at_buffer_position (w, pos, 0, 0,
20663 &ignore, pos + 1,
20664 !dpyinfo->mouse_face_hidden);
20665
20666 /* Display it as active. */
20667 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20668 cursor = No_Cursor;
20669 }
20670 }
20671 }
20672
20673 check_help_echo:
20674
20675 /* Look for a `help-echo' property. */
20676 if (NILP (help_echo_string)) {
20677 Lisp_Object help, overlay;
20678
20679 /* Check overlays first. */
20680 help = overlay = Qnil;
20681 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
20682 {
20683 overlay = overlay_vec[i];
20684 help = Foverlay_get (overlay, Qhelp_echo);
20685 }
20686
20687 if (!NILP (help))
20688 {
20689 help_echo_string = help;
20690 help_echo_window = window;
20691 help_echo_object = overlay;
20692 help_echo_pos = pos;
20693 }
20694 else
20695 {
20696 Lisp_Object object = glyph->object;
20697 int charpos = glyph->charpos;
20698
20699 /* Try text properties. */
20700 if (STRINGP (object)
20701 && charpos >= 0
20702 && charpos < SCHARS (object))
20703 {
20704 help = Fget_text_property (make_number (charpos),
20705 Qhelp_echo, object);
20706 if (NILP (help))
20707 {
20708 /* If the string itself doesn't specify a help-echo,
20709 see if the buffer text ``under'' it does. */
20710 struct glyph_row *r
20711 = MATRIX_ROW (w->current_matrix, vpos);
20712 int start = MATRIX_ROW_START_CHARPOS (r);
20713 int pos = string_buffer_position (w, object, start);
20714 if (pos > 0)
20715 {
20716 help = Fget_char_property (make_number (pos),
20717 Qhelp_echo, w->buffer);
20718 if (!NILP (help))
20719 {
20720 charpos = pos;
20721 object = w->buffer;
20722 }
20723 }
20724 }
20725 }
20726 else if (BUFFERP (object)
20727 && charpos >= BEGV
20728 && charpos < ZV)
20729 help = Fget_text_property (make_number (charpos), Qhelp_echo,
20730 object);
20731
20732 if (!NILP (help))
20733 {
20734 help_echo_string = help;
20735 help_echo_window = window;
20736 help_echo_object = object;
20737 help_echo_pos = charpos;
20738 }
20739 }
20740 }
20741
20742 /* Look for a `pointer' property. */
20743 if (NILP (pointer))
20744 {
20745 /* Check overlays first. */
20746 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
20747 pointer = Foverlay_get (overlay_vec[i], Qpointer);
20748
20749 if (NILP (pointer))
20750 {
20751 Lisp_Object object = glyph->object;
20752 int charpos = glyph->charpos;
20753
20754 /* Try text properties. */
20755 if (STRINGP (object)
20756 && charpos >= 0
20757 && charpos < SCHARS (object))
20758 {
20759 pointer = Fget_text_property (make_number (charpos),
20760 Qpointer, object);
20761 if (NILP (pointer))
20762 {
20763 /* If the string itself doesn't specify a pointer,
20764 see if the buffer text ``under'' it does. */
20765 struct glyph_row *r
20766 = MATRIX_ROW (w->current_matrix, vpos);
20767 int start = MATRIX_ROW_START_CHARPOS (r);
20768 int pos = string_buffer_position (w, object, start);
20769 if (pos > 0)
20770 pointer = Fget_char_property (make_number (pos),
20771 Qpointer, w->buffer);
20772 }
20773 }
20774 else if (BUFFERP (object)
20775 && charpos >= BEGV
20776 && charpos < ZV)
20777 pointer = Fget_text_property (make_number (charpos),
20778 Qpointer, object);
20779 }
20780 }
20781
20782 BEGV = obegv;
20783 ZV = ozv;
20784 current_buffer = obuf;
20785 }
20786
20787 set_cursor:
20788
20789 define_frame_cursor1 (f, cursor, pointer);
20790 }
20791
20792
20793 /* EXPORT for RIF:
20794 Clear any mouse-face on window W. This function is part of the
20795 redisplay interface, and is called from try_window_id and similar
20796 functions to ensure the mouse-highlight is off. */
20797
20798 void
20799 x_clear_window_mouse_face (w)
20800 struct window *w;
20801 {
20802 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20803 Lisp_Object window;
20804
20805 BLOCK_INPUT;
20806 XSETWINDOW (window, w);
20807 if (EQ (window, dpyinfo->mouse_face_window))
20808 clear_mouse_face (dpyinfo);
20809 UNBLOCK_INPUT;
20810 }
20811
20812
20813 /* EXPORT:
20814 Just discard the mouse face information for frame F, if any.
20815 This is used when the size of F is changed. */
20816
20817 void
20818 cancel_mouse_face (f)
20819 struct frame *f;
20820 {
20821 Lisp_Object window;
20822 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20823
20824 window = dpyinfo->mouse_face_window;
20825 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20826 {
20827 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20828 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20829 dpyinfo->mouse_face_window = Qnil;
20830 }
20831 }
20832
20833
20834 #endif /* HAVE_WINDOW_SYSTEM */
20835
20836 \f
20837 /***********************************************************************
20838 Exposure Events
20839 ***********************************************************************/
20840
20841 #ifdef HAVE_WINDOW_SYSTEM
20842
20843 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20844 which intersects rectangle R. R is in window-relative coordinates. */
20845
20846 static void
20847 expose_area (w, row, r, area)
20848 struct window *w;
20849 struct glyph_row *row;
20850 XRectangle *r;
20851 enum glyph_row_area area;
20852 {
20853 struct glyph *first = row->glyphs[area];
20854 struct glyph *end = row->glyphs[area] + row->used[area];
20855 struct glyph *last;
20856 int first_x, start_x, x;
20857
20858 if (area == TEXT_AREA && row->fill_line_p)
20859 /* If row extends face to end of line write the whole line. */
20860 draw_glyphs (w, 0, row, area,
20861 0, row->used[area],
20862 DRAW_NORMAL_TEXT, 0);
20863 else
20864 {
20865 /* Set START_X to the window-relative start position for drawing glyphs of
20866 AREA. The first glyph of the text area can be partially visible.
20867 The first glyphs of other areas cannot. */
20868 start_x = window_box_left_offset (w, area);
20869 x = start_x;
20870 if (area == TEXT_AREA)
20871 x += row->x;
20872
20873 /* Find the first glyph that must be redrawn. */
20874 while (first < end
20875 && x + first->pixel_width < r->x)
20876 {
20877 x += first->pixel_width;
20878 ++first;
20879 }
20880
20881 /* Find the last one. */
20882 last = first;
20883 first_x = x;
20884 while (last < end
20885 && x < r->x + r->width)
20886 {
20887 x += last->pixel_width;
20888 ++last;
20889 }
20890
20891 /* Repaint. */
20892 if (last > first)
20893 draw_glyphs (w, first_x - start_x, row, area,
20894 first - row->glyphs[area], last - row->glyphs[area],
20895 DRAW_NORMAL_TEXT, 0);
20896 }
20897 }
20898
20899
20900 /* Redraw the parts of the glyph row ROW on window W intersecting
20901 rectangle R. R is in window-relative coordinates. Value is
20902 non-zero if mouse-face was overwritten. */
20903
20904 static int
20905 expose_line (w, row, r)
20906 struct window *w;
20907 struct glyph_row *row;
20908 XRectangle *r;
20909 {
20910 xassert (row->enabled_p);
20911
20912 if (row->mode_line_p || w->pseudo_window_p)
20913 draw_glyphs (w, 0, row, TEXT_AREA,
20914 0, row->used[TEXT_AREA],
20915 DRAW_NORMAL_TEXT, 0);
20916 else
20917 {
20918 if (row->used[LEFT_MARGIN_AREA])
20919 expose_area (w, row, r, LEFT_MARGIN_AREA);
20920 if (row->used[TEXT_AREA])
20921 expose_area (w, row, r, TEXT_AREA);
20922 if (row->used[RIGHT_MARGIN_AREA])
20923 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20924 draw_row_fringe_bitmaps (w, row);
20925 }
20926
20927 return row->mouse_face_p;
20928 }
20929
20930
20931 /* Redraw those parts of glyphs rows during expose event handling that
20932 overlap other rows. Redrawing of an exposed line writes over parts
20933 of lines overlapping that exposed line; this function fixes that.
20934
20935 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20936 row in W's current matrix that is exposed and overlaps other rows.
20937 LAST_OVERLAPPING_ROW is the last such row. */
20938
20939 static void
20940 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20941 struct window *w;
20942 struct glyph_row *first_overlapping_row;
20943 struct glyph_row *last_overlapping_row;
20944 {
20945 struct glyph_row *row;
20946
20947 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20948 if (row->overlapping_p)
20949 {
20950 xassert (row->enabled_p && !row->mode_line_p);
20951
20952 if (row->used[LEFT_MARGIN_AREA])
20953 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20954
20955 if (row->used[TEXT_AREA])
20956 x_fix_overlapping_area (w, row, TEXT_AREA);
20957
20958 if (row->used[RIGHT_MARGIN_AREA])
20959 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20960 }
20961 }
20962
20963
20964 /* Return non-zero if W's cursor intersects rectangle R. */
20965
20966 static int
20967 phys_cursor_in_rect_p (w, r)
20968 struct window *w;
20969 XRectangle *r;
20970 {
20971 XRectangle cr, result;
20972 struct glyph *cursor_glyph;
20973
20974 cursor_glyph = get_phys_cursor_glyph (w);
20975 if (cursor_glyph)
20976 {
20977 /* r is relative to W's box, but w->phys_cursor.x is relative
20978 to left edge of W's TEXT area. Adjust it. */
20979 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
20980 cr.y = w->phys_cursor.y;
20981 cr.width = cursor_glyph->pixel_width;
20982 cr.height = w->phys_cursor_height;
20983 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20984 I assume the effect is the same -- and this is portable. */
20985 return x_intersect_rectangles (&cr, r, &result);
20986 }
20987 else
20988 return 0;
20989 }
20990
20991
20992 /* EXPORT:
20993 Draw a vertical window border to the right of window W if W doesn't
20994 have vertical scroll bars. */
20995
20996 void
20997 x_draw_vertical_border (w)
20998 struct window *w;
20999 {
21000 struct frame *f = XFRAME (WINDOW_FRAME (w));
21001
21002 /* We could do better, if we knew what type of scroll-bar the adjacent
21003 windows (on either side) have... But we don't :-(
21004 However, I think this works ok. ++KFS 2003-04-25 */
21005
21006 /* Redraw borders between horizontally adjacent windows. Don't
21007 do it for frames with vertical scroll bars because either the
21008 right scroll bar of a window, or the left scroll bar of its
21009 neighbor will suffice as a border. */
21010 if (!WINDOW_RIGHTMOST_P (w)
21011 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21012 {
21013 int x0, x1, y0, y1;
21014
21015 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21016 y1 -= 1;
21017
21018 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
21019 }
21020 else if (!WINDOW_LEFTMOST_P (w)
21021 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21022 {
21023 int x0, x1, y0, y1;
21024
21025 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21026 y1 -= 1;
21027
21028 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
21029 }
21030 }
21031
21032
21033 /* Redraw the part of window W intersection rectangle FR. Pixel
21034 coordinates in FR are frame-relative. Call this function with
21035 input blocked. Value is non-zero if the exposure overwrites
21036 mouse-face. */
21037
21038 static int
21039 expose_window (w, fr)
21040 struct window *w;
21041 XRectangle *fr;
21042 {
21043 struct frame *f = XFRAME (w->frame);
21044 XRectangle wr, r;
21045 int mouse_face_overwritten_p = 0;
21046
21047 /* If window is not yet fully initialized, do nothing. This can
21048 happen when toolkit scroll bars are used and a window is split.
21049 Reconfiguring the scroll bar will generate an expose for a newly
21050 created window. */
21051 if (w->current_matrix == NULL)
21052 return 0;
21053
21054 /* When we're currently updating the window, display and current
21055 matrix usually don't agree. Arrange for a thorough display
21056 later. */
21057 if (w == updated_window)
21058 {
21059 SET_FRAME_GARBAGED (f);
21060 return 0;
21061 }
21062
21063 /* Frame-relative pixel rectangle of W. */
21064 wr.x = WINDOW_LEFT_EDGE_X (w);
21065 wr.y = WINDOW_TOP_EDGE_Y (w);
21066 wr.width = WINDOW_TOTAL_WIDTH (w);
21067 wr.height = WINDOW_TOTAL_HEIGHT (w);
21068
21069 if (x_intersect_rectangles (fr, &wr, &r))
21070 {
21071 int yb = window_text_bottom_y (w);
21072 struct glyph_row *row;
21073 int cursor_cleared_p;
21074 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21075
21076 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21077 r.x, r.y, r.width, r.height));
21078
21079 /* Convert to window coordinates. */
21080 r.x -= WINDOW_LEFT_EDGE_X (w);
21081 r.y -= WINDOW_TOP_EDGE_Y (w);
21082
21083 /* Turn off the cursor. */
21084 if (!w->pseudo_window_p
21085 && phys_cursor_in_rect_p (w, &r))
21086 {
21087 x_clear_cursor (w);
21088 cursor_cleared_p = 1;
21089 }
21090 else
21091 cursor_cleared_p = 0;
21092
21093 /* Update lines intersecting rectangle R. */
21094 first_overlapping_row = last_overlapping_row = NULL;
21095 for (row = w->current_matrix->rows;
21096 row->enabled_p;
21097 ++row)
21098 {
21099 int y0 = row->y;
21100 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21101
21102 if ((y0 >= r.y && y0 < r.y + r.height)
21103 || (y1 > r.y && y1 < r.y + r.height)
21104 || (r.y >= y0 && r.y < y1)
21105 || (r.y + r.height > y0 && r.y + r.height < y1))
21106 {
21107 if (row->overlapping_p)
21108 {
21109 if (first_overlapping_row == NULL)
21110 first_overlapping_row = row;
21111 last_overlapping_row = row;
21112 }
21113
21114 if (expose_line (w, row, &r))
21115 mouse_face_overwritten_p = 1;
21116 }
21117
21118 if (y1 >= yb)
21119 break;
21120 }
21121
21122 /* Display the mode line if there is one. */
21123 if (WINDOW_WANTS_MODELINE_P (w)
21124 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21125 row->enabled_p)
21126 && row->y < r.y + r.height)
21127 {
21128 if (expose_line (w, row, &r))
21129 mouse_face_overwritten_p = 1;
21130 }
21131
21132 if (!w->pseudo_window_p)
21133 {
21134 /* Fix the display of overlapping rows. */
21135 if (first_overlapping_row)
21136 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21137
21138 /* Draw border between windows. */
21139 x_draw_vertical_border (w);
21140
21141 /* Turn the cursor on again. */
21142 if (cursor_cleared_p)
21143 update_window_cursor (w, 1);
21144 }
21145 }
21146
21147 #ifdef HAVE_CARBON
21148 /* Display scroll bar for this window. */
21149 if (!NILP (w->vertical_scroll_bar))
21150 {
21151 /* ++KFS:
21152 If this doesn't work here (maybe some header files are missing),
21153 make a function in macterm.c and call it to do the job! */
21154 ControlHandle ch
21155 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
21156
21157 Draw1Control (ch);
21158 }
21159 #endif
21160
21161 return mouse_face_overwritten_p;
21162 }
21163
21164
21165
21166 /* Redraw (parts) of all windows in the window tree rooted at W that
21167 intersect R. R contains frame pixel coordinates. Value is
21168 non-zero if the exposure overwrites mouse-face. */
21169
21170 static int
21171 expose_window_tree (w, r)
21172 struct window *w;
21173 XRectangle *r;
21174 {
21175 struct frame *f = XFRAME (w->frame);
21176 int mouse_face_overwritten_p = 0;
21177
21178 while (w && !FRAME_GARBAGED_P (f))
21179 {
21180 if (!NILP (w->hchild))
21181 mouse_face_overwritten_p
21182 |= expose_window_tree (XWINDOW (w->hchild), r);
21183 else if (!NILP (w->vchild))
21184 mouse_face_overwritten_p
21185 |= expose_window_tree (XWINDOW (w->vchild), r);
21186 else
21187 mouse_face_overwritten_p |= expose_window (w, r);
21188
21189 w = NILP (w->next) ? NULL : XWINDOW (w->next);
21190 }
21191
21192 return mouse_face_overwritten_p;
21193 }
21194
21195
21196 /* EXPORT:
21197 Redisplay an exposed area of frame F. X and Y are the upper-left
21198 corner of the exposed rectangle. W and H are width and height of
21199 the exposed area. All are pixel values. W or H zero means redraw
21200 the entire frame. */
21201
21202 void
21203 expose_frame (f, x, y, w, h)
21204 struct frame *f;
21205 int x, y, w, h;
21206 {
21207 XRectangle r;
21208 int mouse_face_overwritten_p = 0;
21209
21210 TRACE ((stderr, "expose_frame "));
21211
21212 /* No need to redraw if frame will be redrawn soon. */
21213 if (FRAME_GARBAGED_P (f))
21214 {
21215 TRACE ((stderr, " garbaged\n"));
21216 return;
21217 }
21218
21219 #ifdef HAVE_CARBON
21220 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
21221 or deactivated here, for unknown reasons, activated scroll bars
21222 are shown in deactivated frames in some instances. */
21223 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
21224 activate_scroll_bars (f);
21225 else
21226 deactivate_scroll_bars (f);
21227 #endif
21228
21229 /* If basic faces haven't been realized yet, there is no point in
21230 trying to redraw anything. This can happen when we get an expose
21231 event while Emacs is starting, e.g. by moving another window. */
21232 if (FRAME_FACE_CACHE (f) == NULL
21233 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
21234 {
21235 TRACE ((stderr, " no faces\n"));
21236 return;
21237 }
21238
21239 if (w == 0 || h == 0)
21240 {
21241 r.x = r.y = 0;
21242 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
21243 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
21244 }
21245 else
21246 {
21247 r.x = x;
21248 r.y = y;
21249 r.width = w;
21250 r.height = h;
21251 }
21252
21253 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
21254 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
21255
21256 if (WINDOWP (f->tool_bar_window))
21257 mouse_face_overwritten_p
21258 |= expose_window (XWINDOW (f->tool_bar_window), &r);
21259
21260 #ifdef HAVE_X_WINDOWS
21261 #ifndef MSDOS
21262 #ifndef USE_X_TOOLKIT
21263 if (WINDOWP (f->menu_bar_window))
21264 mouse_face_overwritten_p
21265 |= expose_window (XWINDOW (f->menu_bar_window), &r);
21266 #endif /* not USE_X_TOOLKIT */
21267 #endif
21268 #endif
21269
21270 /* Some window managers support a focus-follows-mouse style with
21271 delayed raising of frames. Imagine a partially obscured frame,
21272 and moving the mouse into partially obscured mouse-face on that
21273 frame. The visible part of the mouse-face will be highlighted,
21274 then the WM raises the obscured frame. With at least one WM, KDE
21275 2.1, Emacs is not getting any event for the raising of the frame
21276 (even tried with SubstructureRedirectMask), only Expose events.
21277 These expose events will draw text normally, i.e. not
21278 highlighted. Which means we must redo the highlight here.
21279 Subsume it under ``we love X''. --gerd 2001-08-15 */
21280 /* Included in Windows version because Windows most likely does not
21281 do the right thing if any third party tool offers
21282 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
21283 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
21284 {
21285 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21286 if (f == dpyinfo->mouse_face_mouse_frame)
21287 {
21288 int x = dpyinfo->mouse_face_mouse_x;
21289 int y = dpyinfo->mouse_face_mouse_y;
21290 clear_mouse_face (dpyinfo);
21291 note_mouse_highlight (f, x, y);
21292 }
21293 }
21294 }
21295
21296
21297 /* EXPORT:
21298 Determine the intersection of two rectangles R1 and R2. Return
21299 the intersection in *RESULT. Value is non-zero if RESULT is not
21300 empty. */
21301
21302 int
21303 x_intersect_rectangles (r1, r2, result)
21304 XRectangle *r1, *r2, *result;
21305 {
21306 XRectangle *left, *right;
21307 XRectangle *upper, *lower;
21308 int intersection_p = 0;
21309
21310 /* Rearrange so that R1 is the left-most rectangle. */
21311 if (r1->x < r2->x)
21312 left = r1, right = r2;
21313 else
21314 left = r2, right = r1;
21315
21316 /* X0 of the intersection is right.x0, if this is inside R1,
21317 otherwise there is no intersection. */
21318 if (right->x <= left->x + left->width)
21319 {
21320 result->x = right->x;
21321
21322 /* The right end of the intersection is the minimum of the
21323 the right ends of left and right. */
21324 result->width = (min (left->x + left->width, right->x + right->width)
21325 - result->x);
21326
21327 /* Same game for Y. */
21328 if (r1->y < r2->y)
21329 upper = r1, lower = r2;
21330 else
21331 upper = r2, lower = r1;
21332
21333 /* The upper end of the intersection is lower.y0, if this is inside
21334 of upper. Otherwise, there is no intersection. */
21335 if (lower->y <= upper->y + upper->height)
21336 {
21337 result->y = lower->y;
21338
21339 /* The lower end of the intersection is the minimum of the lower
21340 ends of upper and lower. */
21341 result->height = (min (lower->y + lower->height,
21342 upper->y + upper->height)
21343 - result->y);
21344 intersection_p = 1;
21345 }
21346 }
21347
21348 return intersection_p;
21349 }
21350
21351 #endif /* HAVE_WINDOW_SYSTEM */
21352
21353 \f
21354 /***********************************************************************
21355 Initialization
21356 ***********************************************************************/
21357
21358 void
21359 syms_of_xdisp ()
21360 {
21361 Vwith_echo_area_save_vector = Qnil;
21362 staticpro (&Vwith_echo_area_save_vector);
21363
21364 Vmessage_stack = Qnil;
21365 staticpro (&Vmessage_stack);
21366
21367 Qinhibit_redisplay = intern ("inhibit-redisplay");
21368 staticpro (&Qinhibit_redisplay);
21369
21370 message_dolog_marker1 = Fmake_marker ();
21371 staticpro (&message_dolog_marker1);
21372 message_dolog_marker2 = Fmake_marker ();
21373 staticpro (&message_dolog_marker2);
21374 message_dolog_marker3 = Fmake_marker ();
21375 staticpro (&message_dolog_marker3);
21376
21377 #if GLYPH_DEBUG
21378 defsubr (&Sdump_frame_glyph_matrix);
21379 defsubr (&Sdump_glyph_matrix);
21380 defsubr (&Sdump_glyph_row);
21381 defsubr (&Sdump_tool_bar_row);
21382 defsubr (&Strace_redisplay);
21383 defsubr (&Strace_to_stderr);
21384 #endif
21385 #ifdef HAVE_WINDOW_SYSTEM
21386 defsubr (&Stool_bar_lines_needed);
21387 defsubr (&Slookup_image_map);
21388 #endif
21389 defsubr (&Sformat_mode_line);
21390
21391 staticpro (&Qmenu_bar_update_hook);
21392 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
21393
21394 staticpro (&Qoverriding_terminal_local_map);
21395 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
21396
21397 staticpro (&Qoverriding_local_map);
21398 Qoverriding_local_map = intern ("overriding-local-map");
21399
21400 staticpro (&Qwindow_scroll_functions);
21401 Qwindow_scroll_functions = intern ("window-scroll-functions");
21402
21403 staticpro (&Qredisplay_end_trigger_functions);
21404 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
21405
21406 staticpro (&Qinhibit_point_motion_hooks);
21407 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
21408
21409 QCdata = intern (":data");
21410 staticpro (&QCdata);
21411 Qdisplay = intern ("display");
21412 staticpro (&Qdisplay);
21413 Qspace_width = intern ("space-width");
21414 staticpro (&Qspace_width);
21415 Qraise = intern ("raise");
21416 staticpro (&Qraise);
21417 Qspace = intern ("space");
21418 staticpro (&Qspace);
21419 Qmargin = intern ("margin");
21420 staticpro (&Qmargin);
21421 Qpointer = intern ("pointer");
21422 staticpro (&Qpointer);
21423 Qleft_margin = intern ("left-margin");
21424 staticpro (&Qleft_margin);
21425 Qright_margin = intern ("right-margin");
21426 staticpro (&Qright_margin);
21427 QCalign_to = intern (":align-to");
21428 staticpro (&QCalign_to);
21429 QCrelative_width = intern (":relative-width");
21430 staticpro (&QCrelative_width);
21431 QCrelative_height = intern (":relative-height");
21432 staticpro (&QCrelative_height);
21433 QCeval = intern (":eval");
21434 staticpro (&QCeval);
21435 QCpropertize = intern (":propertize");
21436 staticpro (&QCpropertize);
21437 QCfile = intern (":file");
21438 staticpro (&QCfile);
21439 Qfontified = intern ("fontified");
21440 staticpro (&Qfontified);
21441 Qfontification_functions = intern ("fontification-functions");
21442 staticpro (&Qfontification_functions);
21443 Qtrailing_whitespace = intern ("trailing-whitespace");
21444 staticpro (&Qtrailing_whitespace);
21445 Qimage = intern ("image");
21446 staticpro (&Qimage);
21447 QCmap = intern (":map");
21448 staticpro (&QCmap);
21449 QCpointer = intern (":pointer");
21450 staticpro (&QCpointer);
21451 Qrect = intern ("rect");
21452 staticpro (&Qrect);
21453 Qcircle = intern ("circle");
21454 staticpro (&Qcircle);
21455 Qpoly = intern ("poly");
21456 staticpro (&Qpoly);
21457 Qmessage_truncate_lines = intern ("message-truncate-lines");
21458 staticpro (&Qmessage_truncate_lines);
21459 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
21460 staticpro (&Qcursor_in_non_selected_windows);
21461 Qgrow_only = intern ("grow-only");
21462 staticpro (&Qgrow_only);
21463 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
21464 staticpro (&Qinhibit_menubar_update);
21465 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
21466 staticpro (&Qinhibit_eval_during_redisplay);
21467 Qposition = intern ("position");
21468 staticpro (&Qposition);
21469 Qbuffer_position = intern ("buffer-position");
21470 staticpro (&Qbuffer_position);
21471 Qobject = intern ("object");
21472 staticpro (&Qobject);
21473 Qbar = intern ("bar");
21474 staticpro (&Qbar);
21475 Qhbar = intern ("hbar");
21476 staticpro (&Qhbar);
21477 Qbox = intern ("box");
21478 staticpro (&Qbox);
21479 Qhollow = intern ("hollow");
21480 staticpro (&Qhollow);
21481 Qhand = intern ("hand");
21482 staticpro (&Qhand);
21483 Qarrow = intern ("arrow");
21484 staticpro (&Qarrow);
21485 Qtext = intern ("text");
21486 staticpro (&Qtext);
21487 Qrisky_local_variable = intern ("risky-local-variable");
21488 staticpro (&Qrisky_local_variable);
21489 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
21490 staticpro (&Qinhibit_free_realized_faces);
21491
21492 list_of_error = Fcons (intern ("error"), Qnil);
21493 staticpro (&list_of_error);
21494
21495 last_arrow_position = Qnil;
21496 last_arrow_string = Qnil;
21497 staticpro (&last_arrow_position);
21498 staticpro (&last_arrow_string);
21499
21500 echo_buffer[0] = echo_buffer[1] = Qnil;
21501 staticpro (&echo_buffer[0]);
21502 staticpro (&echo_buffer[1]);
21503
21504 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
21505 staticpro (&echo_area_buffer[0]);
21506 staticpro (&echo_area_buffer[1]);
21507
21508 Vmessages_buffer_name = build_string ("*Messages*");
21509 staticpro (&Vmessages_buffer_name);
21510
21511 mode_line_proptrans_alist = Qnil;
21512 staticpro (&mode_line_proptrans_alist);
21513
21514 mode_line_string_list = Qnil;
21515 staticpro (&mode_line_string_list);
21516
21517 help_echo_string = Qnil;
21518 staticpro (&help_echo_string);
21519 help_echo_object = Qnil;
21520 staticpro (&help_echo_object);
21521 help_echo_window = Qnil;
21522 staticpro (&help_echo_window);
21523 previous_help_echo_string = Qnil;
21524 staticpro (&previous_help_echo_string);
21525 help_echo_pos = -1;
21526
21527 #ifdef HAVE_WINDOW_SYSTEM
21528 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
21529 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
21530 For example, if a block cursor is over a tab, it will be drawn as
21531 wide as that tab on the display. */);
21532 x_stretch_cursor_p = 0;
21533 #endif
21534
21535 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
21536 doc: /* Non-nil means highlight trailing whitespace.
21537 The face used for trailing whitespace is `trailing-whitespace'. */);
21538 Vshow_trailing_whitespace = Qnil;
21539
21540 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
21541 doc: /* The pointer shape to show in void text areas.
21542 Nil means to show the text pointer. Other options are `arrow', `text',
21543 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
21544 Vvoid_text_area_pointer = Qarrow;
21545
21546 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
21547 doc: /* Non-nil means don't actually do any redisplay.
21548 This is used for internal purposes. */);
21549 Vinhibit_redisplay = Qnil;
21550
21551 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
21552 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
21553 Vglobal_mode_string = Qnil;
21554
21555 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
21556 doc: /* Marker for where to display an arrow on top of the buffer text.
21557 This must be the beginning of a line in order to work.
21558 See also `overlay-arrow-string'. */);
21559 Voverlay_arrow_position = Qnil;
21560
21561 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
21562 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
21563 Voverlay_arrow_string = Qnil;
21564
21565 DEFVAR_INT ("scroll-step", &scroll_step,
21566 doc: /* *The number of lines to try scrolling a window by when point moves out.
21567 If that fails to bring point back on frame, point is centered instead.
21568 If this is zero, point is always centered after it moves off frame.
21569 If you want scrolling to always be a line at a time, you should set
21570 `scroll-conservatively' to a large value rather than set this to 1. */);
21571
21572 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
21573 doc: /* *Scroll up to this many lines, to bring point back on screen.
21574 A value of zero means to scroll the text to center point vertically
21575 in the window. */);
21576 scroll_conservatively = 0;
21577
21578 DEFVAR_INT ("scroll-margin", &scroll_margin,
21579 doc: /* *Number of lines of margin at the top and bottom of a window.
21580 Recenter the window whenever point gets within this many lines
21581 of the top or bottom of the window. */);
21582 scroll_margin = 0;
21583
21584 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
21585 doc: /* Pixels per inch on current display.
21586 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
21587 Vdisplay_pixels_per_inch = make_float (72.0);
21588
21589 #if GLYPH_DEBUG
21590 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
21591 #endif
21592
21593 DEFVAR_BOOL ("truncate-partial-width-windows",
21594 &truncate_partial_width_windows,
21595 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
21596 truncate_partial_width_windows = 1;
21597
21598 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
21599 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
21600 Any other value means to use the appropriate face, `mode-line',
21601 `header-line', or `menu' respectively. */);
21602 mode_line_inverse_video = 1;
21603
21604 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
21605 doc: /* *Maximum buffer size for which line number should be displayed.
21606 If the buffer is bigger than this, the line number does not appear
21607 in the mode line. A value of nil means no limit. */);
21608 Vline_number_display_limit = Qnil;
21609
21610 DEFVAR_INT ("line-number-display-limit-width",
21611 &line_number_display_limit_width,
21612 doc: /* *Maximum line width (in characters) for line number display.
21613 If the average length of the lines near point is bigger than this, then the
21614 line number may be omitted from the mode line. */);
21615 line_number_display_limit_width = 200;
21616
21617 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
21618 doc: /* *Non-nil means highlight region even in nonselected windows. */);
21619 highlight_nonselected_windows = 0;
21620
21621 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
21622 doc: /* Non-nil if more than one frame is visible on this display.
21623 Minibuffer-only frames don't count, but iconified frames do.
21624 This variable is not guaranteed to be accurate except while processing
21625 `frame-title-format' and `icon-title-format'. */);
21626
21627 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
21628 doc: /* Template for displaying the title bar of visible frames.
21629 \(Assuming the window manager supports this feature.)
21630 This variable has the same structure as `mode-line-format' (which see),
21631 and is used only on frames for which no explicit name has been set
21632 \(see `modify-frame-parameters'). */);
21633
21634 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
21635 doc: /* Template for displaying the title bar of an iconified frame.
21636 \(Assuming the window manager supports this feature.)
21637 This variable has the same structure as `mode-line-format' (which see),
21638 and is used only on frames for which no explicit name has been set
21639 \(see `modify-frame-parameters'). */);
21640 Vicon_title_format
21641 = Vframe_title_format
21642 = Fcons (intern ("multiple-frames"),
21643 Fcons (build_string ("%b"),
21644 Fcons (Fcons (empty_string,
21645 Fcons (intern ("invocation-name"),
21646 Fcons (build_string ("@"),
21647 Fcons (intern ("system-name"),
21648 Qnil)))),
21649 Qnil)));
21650
21651 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
21652 doc: /* Maximum number of lines to keep in the message log buffer.
21653 If nil, disable message logging. If t, log messages but don't truncate
21654 the buffer when it becomes large. */);
21655 Vmessage_log_max = make_number (50);
21656
21657 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
21658 doc: /* Functions called before redisplay, if window sizes have changed.
21659 The value should be a list of functions that take one argument.
21660 Just before redisplay, for each frame, if any of its windows have changed
21661 size since the last redisplay, or have been split or deleted,
21662 all the functions in the list are called, with the frame as argument. */);
21663 Vwindow_size_change_functions = Qnil;
21664
21665 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
21666 doc: /* List of Functions to call before redisplaying a window with scrolling.
21667 Each function is called with two arguments, the window
21668 and its new display-start position. Note that the value of `window-end'
21669 is not valid when these functions are called. */);
21670 Vwindow_scroll_functions = Qnil;
21671
21672 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
21673 doc: /* *Non-nil means autoselect window with mouse pointer. */);
21674 mouse_autoselect_window = 0;
21675
21676 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
21677 doc: /* *Non-nil means automatically resize tool-bars.
21678 This increases a tool-bar's height if not all tool-bar items are visible.
21679 It decreases a tool-bar's height when it would display blank lines
21680 otherwise. */);
21681 auto_resize_tool_bars_p = 1;
21682
21683 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
21684 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
21685 auto_raise_tool_bar_buttons_p = 1;
21686
21687 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
21688 doc: /* *Margin around tool-bar buttons in pixels.
21689 If an integer, use that for both horizontal and vertical margins.
21690 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
21691 HORZ specifying the horizontal margin, and VERT specifying the
21692 vertical margin. */);
21693 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
21694
21695 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
21696 doc: /* *Relief thickness of tool-bar buttons. */);
21697 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
21698
21699 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
21700 doc: /* List of functions to call to fontify regions of text.
21701 Each function is called with one argument POS. Functions must
21702 fontify a region starting at POS in the current buffer, and give
21703 fontified regions the property `fontified'. */);
21704 Vfontification_functions = Qnil;
21705 Fmake_variable_buffer_local (Qfontification_functions);
21706
21707 DEFVAR_BOOL ("unibyte-display-via-language-environment",
21708 &unibyte_display_via_language_environment,
21709 doc: /* *Non-nil means display unibyte text according to language environment.
21710 Specifically this means that unibyte non-ASCII characters
21711 are displayed by converting them to the equivalent multibyte characters
21712 according to the current language environment. As a result, they are
21713 displayed according to the current fontset. */);
21714 unibyte_display_via_language_environment = 0;
21715
21716 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
21717 doc: /* *Maximum height for resizing mini-windows.
21718 If a float, it specifies a fraction of the mini-window frame's height.
21719 If an integer, it specifies a number of lines. */);
21720 Vmax_mini_window_height = make_float (0.25);
21721
21722 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
21723 doc: /* *How to resize mini-windows.
21724 A value of nil means don't automatically resize mini-windows.
21725 A value of t means resize them to fit the text displayed in them.
21726 A value of `grow-only', the default, means let mini-windows grow
21727 only, until their display becomes empty, at which point the windows
21728 go back to their normal size. */);
21729 Vresize_mini_windows = Qgrow_only;
21730
21731 DEFVAR_LISP ("cursor-in-non-selected-windows",
21732 &Vcursor_in_non_selected_windows,
21733 doc: /* *Cursor type to display in non-selected windows.
21734 t means to use hollow box cursor. See `cursor-type' for other values. */);
21735 Vcursor_in_non_selected_windows = Qt;
21736
21737 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
21738 doc: /* Alist specifying how to blink the cursor off.
21739 Each element has the form (ON-STATE . OFF-STATE). Whenever the
21740 `cursor-type' frame-parameter or variable equals ON-STATE,
21741 comparing using `equal', Emacs uses OFF-STATE to specify
21742 how to blink it off. */);
21743 Vblink_cursor_alist = Qnil;
21744
21745 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
21746 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
21747 automatic_hscrolling_p = 1;
21748
21749 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
21750 doc: /* *How many columns away from the window edge point is allowed to get
21751 before automatic hscrolling will horizontally scroll the window. */);
21752 hscroll_margin = 5;
21753
21754 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
21755 doc: /* *How many columns to scroll the window when point gets too close to the edge.
21756 When point is less than `automatic-hscroll-margin' columns from the window
21757 edge, automatic hscrolling will scroll the window by the amount of columns
21758 determined by this variable. If its value is a positive integer, scroll that
21759 many columns. If it's a positive floating-point number, it specifies the
21760 fraction of the window's width to scroll. If it's nil or zero, point will be
21761 centered horizontally after the scroll. Any other value, including negative
21762 numbers, are treated as if the value were zero.
21763
21764 Automatic hscrolling always moves point outside the scroll margin, so if
21765 point was more than scroll step columns inside the margin, the window will
21766 scroll more than the value given by the scroll step.
21767
21768 Note that the lower bound for automatic hscrolling specified by `scroll-left'
21769 and `scroll-right' overrides this variable's effect. */);
21770 Vhscroll_step = make_number (0);
21771
21772 DEFVAR_LISP ("image-types", &Vimage_types,
21773 doc: /* List of supported image types.
21774 Each element of the list is a symbol for a supported image type. */);
21775 Vimage_types = Qnil;
21776
21777 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
21778 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
21779 Bind this around calls to `message' to let it take effect. */);
21780 message_truncate_lines = 0;
21781
21782 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
21783 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
21784 Can be used to update submenus whose contents should vary. */);
21785 Vmenu_bar_update_hook = Qnil;
21786
21787 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
21788 doc: /* Non-nil means don't update menu bars. Internal use only. */);
21789 inhibit_menubar_update = 0;
21790
21791 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
21792 doc: /* Non-nil means don't eval Lisp during redisplay. */);
21793 inhibit_eval_during_redisplay = 0;
21794
21795 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
21796 doc: /* Non-nil means don't free realized faces. Internal use only. */);
21797 inhibit_free_realized_faces = 0;
21798
21799 #if GLYPH_DEBUG
21800 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
21801 doc: /* Inhibit try_window_id display optimization. */);
21802 inhibit_try_window_id = 0;
21803
21804 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
21805 doc: /* Inhibit try_window_reusing display optimization. */);
21806 inhibit_try_window_reusing = 0;
21807
21808 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
21809 doc: /* Inhibit try_cursor_movement display optimization. */);
21810 inhibit_try_cursor_movement = 0;
21811 #endif /* GLYPH_DEBUG */
21812 }
21813
21814
21815 /* Initialize this module when Emacs starts. */
21816
21817 void
21818 init_xdisp ()
21819 {
21820 Lisp_Object root_window;
21821 struct window *mini_w;
21822
21823 current_header_line_height = current_mode_line_height = -1;
21824
21825 CHARPOS (this_line_start_pos) = 0;
21826
21827 mini_w = XWINDOW (minibuf_window);
21828 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21829
21830 if (!noninteractive)
21831 {
21832 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21833 int i;
21834
21835 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
21836 set_window_height (root_window,
21837 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
21838 0);
21839 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
21840 set_window_height (minibuf_window, 1, 0);
21841
21842 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21843 mini_w->total_cols = make_number (FRAME_COLS (f));
21844
21845 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21846 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21847 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21848
21849 /* The default ellipsis glyphs `...'. */
21850 for (i = 0; i < 3; ++i)
21851 default_invis_vector[i] = make_number ('.');
21852 }
21853
21854 {
21855 /* Allocate the buffer for frame titles.
21856 Also used for `format-mode-line'. */
21857 int size = 100;
21858 frame_title_buf = (char *) xmalloc (size);
21859 frame_title_buf_end = frame_title_buf + size;
21860 frame_title_ptr = NULL;
21861 }
21862
21863 help_echo_showing_p = 0;
21864 }
21865
21866
21867 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
21868 (do not change this comment) */