(move_it_in_display_line_to): Fix last change.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 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 #endif
202
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern Lisp_Object do_mouse_tracking;
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 /* Non-zero means to reposition window if cursor line is only partially visible. */
268
269 int make_cursor_line_fully_visible_p;
270
271 /* Margin around tool bar buttons in pixels. */
272
273 Lisp_Object Vtool_bar_button_margin;
274
275 /* Thickness of shadow to draw around tool bar buttons. */
276
277 EMACS_INT tool_bar_button_relief;
278
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
281
282 int auto_resize_tool_bars_p;
283
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
287
288 int x_stretch_cursor_p;
289
290 /* Non-nil means don't actually do any redisplay. */
291
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
293
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
295
296 int inhibit_eval_during_redisplay;
297
298 /* Names of text properties relevant for redisplay. */
299
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
302
303 /* Symbols used in text property values. */
304
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
316
317 /* Non-nil means highlight trailing whitespace. */
318
319 Lisp_Object Vshow_trailing_whitespace;
320
321 /* Non-nil means escape non-break space and hyphens. */
322
323 Lisp_Object Vshow_nonbreak_escape;
324
325 #ifdef HAVE_WINDOW_SYSTEM
326 extern Lisp_Object Voverflow_newline_into_fringe;
327
328 /* Test if overflow newline into fringe. Called with iterator IT
329 at or past right window margin, and with IT->current_x set. */
330
331 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
332 (!NILP (Voverflow_newline_into_fringe) \
333 && FRAME_WINDOW_P (it->f) \
334 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
335 && it->current_x == it->last_visible_x)
336
337 #endif /* HAVE_WINDOW_SYSTEM */
338
339 /* Non-nil means show the text cursor in void text areas
340 i.e. in blank areas after eol and eob. This used to be
341 the default in 21.3. */
342
343 Lisp_Object Vvoid_text_area_pointer;
344
345 /* Name of the face used to highlight trailing whitespace. */
346
347 Lisp_Object Qtrailing_whitespace;
348
349 /* Name and number of the face used to highlight escape glyphs. */
350
351 Lisp_Object Qescape_glyph;
352
353 /* The symbol `image' which is the car of the lists used to represent
354 images in Lisp. */
355
356 Lisp_Object Qimage;
357
358 /* The image map types. */
359 Lisp_Object QCmap, QCpointer;
360 Lisp_Object Qrect, Qcircle, Qpoly;
361
362 /* Non-zero means print newline to stdout before next mini-buffer
363 message. */
364
365 int noninteractive_need_newline;
366
367 /* Non-zero means print newline to message log before next message. */
368
369 static int message_log_need_newline;
370
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
377 \f
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
382
383 static struct text_pos this_line_start_pos;
384
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
387
388 static struct text_pos this_line_end_pos;
389
390 /* The vertical positions and the height of this line. */
391
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
395
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
398
399 static int this_line_start_x;
400
401 /* Buffer that this_line_.* variables are referring to. */
402
403 static struct buffer *this_line_buffer;
404
405 /* Nonzero means truncate lines in all windows less wide than the
406 frame. */
407
408 int truncate_partial_width_windows;
409
410 /* A flag to control how to display unibyte 8-bit character. */
411
412 int unibyte_display_via_language_environment;
413
414 /* Nonzero means we have more than one non-mini-buffer-only frame.
415 Not guaranteed to be accurate except while parsing
416 frame-title-format. */
417
418 int multiple_frames;
419
420 Lisp_Object Vglobal_mode_string;
421
422
423 /* List of variables (symbols) which hold markers for overlay arrows.
424 The symbols on this list are examined during redisplay to determine
425 where to display overlay arrows. */
426
427 Lisp_Object Voverlay_arrow_variable_list;
428
429 /* Marker for where to display an arrow on top of the buffer text. */
430
431 Lisp_Object Voverlay_arrow_position;
432
433 /* String to display for the arrow. Only used on terminal frames. */
434
435 Lisp_Object Voverlay_arrow_string;
436
437 /* Values of those variables at last redisplay are stored as
438 properties on `overlay-arrow-position' symbol. However, if
439 Voverlay_arrow_position is a marker, last-arrow-position is its
440 numerical position. */
441
442 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
443
444 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
445 properties on a symbol in overlay-arrow-variable-list. */
446
447 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
448
449 /* Like mode-line-format, but for the title bar on a visible frame. */
450
451 Lisp_Object Vframe_title_format;
452
453 /* Like mode-line-format, but for the title bar on an iconified frame. */
454
455 Lisp_Object Vicon_title_format;
456
457 /* List of functions to call when a window's size changes. These
458 functions get one arg, a frame on which one or more windows' sizes
459 have changed. */
460
461 static Lisp_Object Vwindow_size_change_functions;
462
463 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
464
465 /* Nonzero if an overlay arrow has been displayed in this window. */
466
467 static int overlay_arrow_seen;
468
469 /* Nonzero means highlight the region even in nonselected windows. */
470
471 int highlight_nonselected_windows;
472
473 /* If cursor motion alone moves point off frame, try scrolling this
474 many lines up or down if that will bring it back. */
475
476 static EMACS_INT scroll_step;
477
478 /* Nonzero means scroll just far enough to bring point back on the
479 screen, when appropriate. */
480
481 static EMACS_INT scroll_conservatively;
482
483 /* Recenter the window whenever point gets within this many lines of
484 the top or bottom of the window. This value is translated into a
485 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
486 that there is really a fixed pixel height scroll margin. */
487
488 EMACS_INT scroll_margin;
489
490 /* Number of windows showing the buffer of the selected window (or
491 another buffer with the same base buffer). keyboard.c refers to
492 this. */
493
494 int buffer_shared;
495
496 /* Vector containing glyphs for an ellipsis `...'. */
497
498 static Lisp_Object default_invis_vector[3];
499
500 /* Zero means display the mode-line/header-line/menu-bar in the default face
501 (this slightly odd definition is for compatibility with previous versions
502 of emacs), non-zero means display them using their respective faces.
503
504 This variable is deprecated. */
505
506 int mode_line_inverse_video;
507
508 /* Prompt to display in front of the mini-buffer contents. */
509
510 Lisp_Object minibuf_prompt;
511
512 /* Width of current mini-buffer prompt. Only set after display_line
513 of the line that contains the prompt. */
514
515 int minibuf_prompt_width;
516
517 /* This is the window where the echo area message was displayed. It
518 is always a mini-buffer window, but it may not be the same window
519 currently active as a mini-buffer. */
520
521 Lisp_Object echo_area_window;
522
523 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
524 pushes the current message and the value of
525 message_enable_multibyte on the stack, the function restore_message
526 pops the stack and displays MESSAGE again. */
527
528 Lisp_Object Vmessage_stack;
529
530 /* Nonzero means multibyte characters were enabled when the echo area
531 message was specified. */
532
533 int message_enable_multibyte;
534
535 /* Nonzero if we should redraw the mode lines on the next redisplay. */
536
537 int update_mode_lines;
538
539 /* Nonzero if window sizes or contents have changed since last
540 redisplay that finished. */
541
542 int windows_or_buffers_changed;
543
544 /* Nonzero means a frame's cursor type has been changed. */
545
546 int cursor_type_changed;
547
548 /* Nonzero after display_mode_line if %l was used and it displayed a
549 line number. */
550
551 int line_number_displayed;
552
553 /* Maximum buffer size for which to display line numbers. */
554
555 Lisp_Object Vline_number_display_limit;
556
557 /* Line width to consider when repositioning for line number display. */
558
559 static EMACS_INT line_number_display_limit_width;
560
561 /* Number of lines to keep in the message log buffer. t means
562 infinite. nil means don't log at all. */
563
564 Lisp_Object Vmessage_log_max;
565
566 /* The name of the *Messages* buffer, a string. */
567
568 static Lisp_Object Vmessages_buffer_name;
569
570 /* Index 0 is the buffer that holds the current (desired) echo area message,
571 or nil if none is desired right now.
572
573 Index 1 is the buffer that holds the previously displayed echo area message,
574 or nil to indicate no message. This is normally what's on the screen now.
575
576 These two can point to the same buffer. That happens when the last
577 message output by the user (or made by echoing) has been displayed. */
578
579 Lisp_Object echo_area_buffer[2];
580
581 /* Permanent pointers to the two buffers that are used for echo area
582 purposes. Once the two buffers are made, and their pointers are
583 placed here, these two slots remain unchanged unless those buffers
584 need to be created afresh. */
585
586 static Lisp_Object echo_buffer[2];
587
588 /* A vector saved used in with_area_buffer to reduce consing. */
589
590 static Lisp_Object Vwith_echo_area_save_vector;
591
592 /* Non-zero means display_echo_area should display the last echo area
593 message again. Set by redisplay_preserve_echo_area. */
594
595 static int display_last_displayed_message_p;
596
597 /* Nonzero if echo area is being used by print; zero if being used by
598 message. */
599
600 int message_buf_print;
601
602 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
603
604 Lisp_Object Qinhibit_menubar_update;
605 int inhibit_menubar_update;
606
607 /* Maximum height for resizing mini-windows. Either a float
608 specifying a fraction of the available height, or an integer
609 specifying a number of lines. */
610
611 Lisp_Object Vmax_mini_window_height;
612
613 /* Non-zero means messages should be displayed with truncated
614 lines instead of being continued. */
615
616 int message_truncate_lines;
617 Lisp_Object Qmessage_truncate_lines;
618
619 /* Set to 1 in clear_message to make redisplay_internal aware
620 of an emptied echo area. */
621
622 static int message_cleared_p;
623
624 /* Non-zero means we want a hollow cursor in windows that are not
625 selected. Zero means there's no cursor in such windows. */
626
627 Lisp_Object Vcursor_in_non_selected_windows;
628 Lisp_Object Qcursor_in_non_selected_windows;
629
630 /* How to blink the default frame cursor off. */
631 Lisp_Object Vblink_cursor_alist;
632
633 /* A scratch glyph row with contents used for generating truncation
634 glyphs. Also used in direct_output_for_insert. */
635
636 #define MAX_SCRATCH_GLYPHS 100
637 struct glyph_row scratch_glyph_row;
638 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
639
640 /* Ascent and height of the last line processed by move_it_to. */
641
642 static int last_max_ascent, last_height;
643
644 /* Non-zero if there's a help-echo in the echo area. */
645
646 int help_echo_showing_p;
647
648 /* If >= 0, computed, exact values of mode-line and header-line height
649 to use in the macros CURRENT_MODE_LINE_HEIGHT and
650 CURRENT_HEADER_LINE_HEIGHT. */
651
652 int current_mode_line_height, current_header_line_height;
653
654 /* The maximum distance to look ahead for text properties. Values
655 that are too small let us call compute_char_face and similar
656 functions too often which is expensive. Values that are too large
657 let us call compute_char_face and alike too often because we
658 might not be interested in text properties that far away. */
659
660 #define TEXT_PROP_DISTANCE_LIMIT 100
661
662 #if GLYPH_DEBUG
663
664 /* Variables to turn off display optimizations from Lisp. */
665
666 int inhibit_try_window_id, inhibit_try_window_reusing;
667 int inhibit_try_cursor_movement;
668
669 /* Non-zero means print traces of redisplay if compiled with
670 GLYPH_DEBUG != 0. */
671
672 int trace_redisplay_p;
673
674 #endif /* GLYPH_DEBUG */
675
676 #ifdef DEBUG_TRACE_MOVE
677 /* Non-zero means trace with TRACE_MOVE to stderr. */
678 int trace_move;
679
680 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
681 #else
682 #define TRACE_MOVE(x) (void) 0
683 #endif
684
685 /* Non-zero means automatically scroll windows horizontally to make
686 point visible. */
687
688 int automatic_hscrolling_p;
689
690 /* How close to the margin can point get before the window is scrolled
691 horizontally. */
692 EMACS_INT hscroll_margin;
693
694 /* How much to scroll horizontally when point is inside the above margin. */
695 Lisp_Object Vhscroll_step;
696
697 /* The variable `resize-mini-windows'. If nil, don't resize
698 mini-windows. If t, always resize them to fit the text they
699 display. If `grow-only', let mini-windows grow only until they
700 become empty. */
701
702 Lisp_Object Vresize_mini_windows;
703
704 /* Buffer being redisplayed -- for redisplay_window_error. */
705
706 struct buffer *displayed_buffer;
707
708 /* Value returned from text property handlers (see below). */
709
710 enum prop_handled
711 {
712 HANDLED_NORMALLY,
713 HANDLED_RECOMPUTE_PROPS,
714 HANDLED_OVERLAY_STRING_CONSUMED,
715 HANDLED_RETURN
716 };
717
718 /* A description of text properties that redisplay is interested
719 in. */
720
721 struct props
722 {
723 /* The name of the property. */
724 Lisp_Object *name;
725
726 /* A unique index for the property. */
727 enum prop_idx idx;
728
729 /* A handler function called to set up iterator IT from the property
730 at IT's current position. Value is used to steer handle_stop. */
731 enum prop_handled (*handler) P_ ((struct it *it));
732 };
733
734 static enum prop_handled handle_face_prop P_ ((struct it *));
735 static enum prop_handled handle_invisible_prop P_ ((struct it *));
736 static enum prop_handled handle_display_prop P_ ((struct it *));
737 static enum prop_handled handle_composition_prop P_ ((struct it *));
738 static enum prop_handled handle_overlay_change P_ ((struct it *));
739 static enum prop_handled handle_fontified_prop P_ ((struct it *));
740
741 /* Properties handled by iterators. */
742
743 static struct props it_props[] =
744 {
745 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
746 /* Handle `face' before `display' because some sub-properties of
747 `display' need to know the face. */
748 {&Qface, FACE_PROP_IDX, handle_face_prop},
749 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
750 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
751 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
752 {NULL, 0, NULL}
753 };
754
755 /* Value is the position described by X. If X is a marker, value is
756 the marker_position of X. Otherwise, value is X. */
757
758 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
759
760 /* Enumeration returned by some move_it_.* functions internally. */
761
762 enum move_it_result
763 {
764 /* Not used. Undefined value. */
765 MOVE_UNDEFINED,
766
767 /* Move ended at the requested buffer position or ZV. */
768 MOVE_POS_MATCH_OR_ZV,
769
770 /* Move ended at the requested X pixel position. */
771 MOVE_X_REACHED,
772
773 /* Move within a line ended at the end of a line that must be
774 continued. */
775 MOVE_LINE_CONTINUED,
776
777 /* Move within a line ended at the end of a line that would
778 be displayed truncated. */
779 MOVE_LINE_TRUNCATED,
780
781 /* Move within a line ended at a line end. */
782 MOVE_NEWLINE_OR_CR
783 };
784
785 /* This counter is used to clear the face cache every once in a while
786 in redisplay_internal. It is incremented for each redisplay.
787 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
788 cleared. */
789
790 #define CLEAR_FACE_CACHE_COUNT 500
791 static int clear_face_cache_count;
792
793 /* Similarly for the image cache. */
794
795 #ifdef HAVE_WINDOW_SYSTEM
796 #define CLEAR_IMAGE_CACHE_COUNT 101
797 static int clear_image_cache_count;
798 #endif
799
800 /* Record the previous terminal frame we displayed. */
801
802 static struct frame *previous_terminal_frame;
803
804 /* Non-zero while redisplay_internal is in progress. */
805
806 int redisplaying_p;
807
808 /* Non-zero means don't free realized faces. Bound while freeing
809 realized faces is dangerous because glyph matrices might still
810 reference them. */
811
812 int inhibit_free_realized_faces;
813 Lisp_Object Qinhibit_free_realized_faces;
814
815 /* If a string, XTread_socket generates an event to display that string.
816 (The display is done in read_char.) */
817
818 Lisp_Object help_echo_string;
819 Lisp_Object help_echo_window;
820 Lisp_Object help_echo_object;
821 int help_echo_pos;
822
823 /* Temporary variable for XTread_socket. */
824
825 Lisp_Object previous_help_echo_string;
826
827 /* Null glyph slice */
828
829 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
830
831 \f
832 /* Function prototypes. */
833
834 static void setup_for_ellipsis P_ ((struct it *, int));
835 static void mark_window_display_accurate_1 P_ ((struct window *, int));
836 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
837 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
838 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
839 static int redisplay_mode_lines P_ ((Lisp_Object, int));
840 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
841
842 #if 0
843 static int invisible_text_between_p P_ ((struct it *, int, int));
844 #endif
845
846 static void pint2str P_ ((char *, int, int));
847 static void pint2hrstr P_ ((char *, int, int));
848 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
849 struct text_pos));
850 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
851 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
852 static void store_frame_title_char P_ ((char));
853 static int store_frame_title P_ ((const unsigned char *, int, int));
854 static void x_consider_frame_title P_ ((Lisp_Object));
855 static void handle_stop P_ ((struct it *));
856 static int tool_bar_lines_needed P_ ((struct frame *));
857 static int single_display_spec_intangible_p P_ ((Lisp_Object));
858 static void ensure_echo_area_buffers P_ ((void));
859 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
860 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
861 static int with_echo_area_buffer P_ ((struct window *, int,
862 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
863 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
864 static void clear_garbaged_frames P_ ((void));
865 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
868 static int display_echo_area P_ ((struct window *));
869 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
872 static int string_char_and_length P_ ((const unsigned char *, int, int *));
873 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
874 struct text_pos));
875 static int compute_window_start_on_continuation_line P_ ((struct window *));
876 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
877 static void insert_left_trunc_glyphs P_ ((struct it *));
878 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
879 Lisp_Object));
880 static void extend_face_to_end_of_line P_ ((struct it *));
881 static int append_space_for_newline P_ ((struct it *, int));
882 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
883 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
884 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
885 static int trailing_whitespace_p P_ ((int));
886 static int message_log_check_duplicate P_ ((int, int, int, int));
887 static void push_it P_ ((struct it *));
888 static void pop_it P_ ((struct it *));
889 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
890 static void select_frame_for_redisplay P_ ((Lisp_Object));
891 static void redisplay_internal P_ ((int));
892 static int echo_area_display P_ ((int));
893 static void redisplay_windows P_ ((Lisp_Object));
894 static void redisplay_window P_ ((Lisp_Object, int));
895 static Lisp_Object redisplay_window_error ();
896 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
897 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
898 static void update_menu_bar P_ ((struct frame *, int));
899 static int try_window_reusing_current_matrix P_ ((struct window *));
900 static int try_window_id P_ ((struct window *));
901 static int display_line P_ ((struct it *));
902 static int display_mode_lines P_ ((struct window *));
903 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
904 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
905 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
906 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
907 static void display_menu_bar P_ ((struct window *));
908 static int display_count_lines P_ ((int, int, int, int, int *));
909 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
910 int, int, struct it *, int, int, int, int));
911 static void compute_line_metrics P_ ((struct it *));
912 static void run_redisplay_end_trigger_hook P_ ((struct it *));
913 static int get_overlay_strings P_ ((struct it *, int));
914 static void next_overlay_string P_ ((struct it *));
915 static void reseat P_ ((struct it *, struct text_pos, int));
916 static void reseat_1 P_ ((struct it *, struct text_pos, int));
917 static void back_to_previous_visible_line_start P_ ((struct it *));
918 void reseat_at_previous_visible_line_start P_ ((struct it *));
919 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
920 static int next_element_from_ellipsis P_ ((struct it *));
921 static int next_element_from_display_vector P_ ((struct it *));
922 static int next_element_from_string P_ ((struct it *));
923 static int next_element_from_c_string P_ ((struct it *));
924 static int next_element_from_buffer P_ ((struct it *));
925 static int next_element_from_composition P_ ((struct it *));
926 static int next_element_from_image P_ ((struct it *));
927 static int next_element_from_stretch P_ ((struct it *));
928 static void load_overlay_strings P_ ((struct it *, int));
929 static int init_from_display_pos P_ ((struct it *, struct window *,
930 struct display_pos *));
931 static void reseat_to_string P_ ((struct it *, unsigned char *,
932 Lisp_Object, int, int, int, int));
933 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
934 int, int, int));
935 void move_it_vertically_backward P_ ((struct it *, int));
936 static void init_to_row_start P_ ((struct it *, struct window *,
937 struct glyph_row *));
938 static int init_to_row_end P_ ((struct it *, struct window *,
939 struct glyph_row *));
940 static void back_to_previous_line_start P_ ((struct it *));
941 static int forward_to_next_line_start P_ ((struct it *, int *));
942 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
943 Lisp_Object, int));
944 static struct text_pos string_pos P_ ((int, Lisp_Object));
945 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
946 static int number_of_chars P_ ((unsigned char *, int));
947 static void compute_stop_pos P_ ((struct it *));
948 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
949 Lisp_Object));
950 static int face_before_or_after_it_pos P_ ((struct it *, int));
951 static int next_overlay_change P_ ((int));
952 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
953 Lisp_Object, struct text_pos *,
954 int));
955 static int underlying_face_id P_ ((struct it *));
956 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
957 struct window *));
958
959 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
960 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
961
962 #ifdef HAVE_WINDOW_SYSTEM
963
964 static void update_tool_bar P_ ((struct frame *, int));
965 static void build_desired_tool_bar_string P_ ((struct frame *f));
966 static int redisplay_tool_bar P_ ((struct frame *));
967 static void display_tool_bar_line P_ ((struct it *));
968 static void notice_overwritten_cursor P_ ((struct window *,
969 enum glyph_row_area,
970 int, int, int, int));
971
972
973
974 #endif /* HAVE_WINDOW_SYSTEM */
975
976 \f
977 /***********************************************************************
978 Window display dimensions
979 ***********************************************************************/
980
981 /* Return the bottom boundary y-position for text lines in window W.
982 This is the first y position at which a line cannot start.
983 It is relative to the top of the window.
984
985 This is the height of W minus the height of a mode line, if any. */
986
987 INLINE int
988 window_text_bottom_y (w)
989 struct window *w;
990 {
991 int height = WINDOW_TOTAL_HEIGHT (w);
992
993 if (WINDOW_WANTS_MODELINE_P (w))
994 height -= CURRENT_MODE_LINE_HEIGHT (w);
995 return height;
996 }
997
998 /* Return the pixel width of display area AREA of window W. AREA < 0
999 means return the total width of W, not including fringes to
1000 the left and right of the window. */
1001
1002 INLINE int
1003 window_box_width (w, area)
1004 struct window *w;
1005 int area;
1006 {
1007 int cols = XFASTINT (w->total_cols);
1008 int pixels = 0;
1009
1010 if (!w->pseudo_window_p)
1011 {
1012 cols -= WINDOW_SCROLL_BAR_COLS (w);
1013
1014 if (area == TEXT_AREA)
1015 {
1016 if (INTEGERP (w->left_margin_cols))
1017 cols -= XFASTINT (w->left_margin_cols);
1018 if (INTEGERP (w->right_margin_cols))
1019 cols -= XFASTINT (w->right_margin_cols);
1020 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1021 }
1022 else if (area == LEFT_MARGIN_AREA)
1023 {
1024 cols = (INTEGERP (w->left_margin_cols)
1025 ? XFASTINT (w->left_margin_cols) : 0);
1026 pixels = 0;
1027 }
1028 else if (area == RIGHT_MARGIN_AREA)
1029 {
1030 cols = (INTEGERP (w->right_margin_cols)
1031 ? XFASTINT (w->right_margin_cols) : 0);
1032 pixels = 0;
1033 }
1034 }
1035
1036 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1037 }
1038
1039
1040 /* Return the pixel height of the display area of window W, not
1041 including mode lines of W, if any. */
1042
1043 INLINE int
1044 window_box_height (w)
1045 struct window *w;
1046 {
1047 struct frame *f = XFRAME (w->frame);
1048 int height = WINDOW_TOTAL_HEIGHT (w);
1049
1050 xassert (height >= 0);
1051
1052 /* Note: the code below that determines the mode-line/header-line
1053 height is essentially the same as that contained in the macro
1054 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1055 the appropriate glyph row has its `mode_line_p' flag set,
1056 and if it doesn't, uses estimate_mode_line_height instead. */
1057
1058 if (WINDOW_WANTS_MODELINE_P (w))
1059 {
1060 struct glyph_row *ml_row
1061 = (w->current_matrix && w->current_matrix->rows
1062 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1063 : 0);
1064 if (ml_row && ml_row->mode_line_p)
1065 height -= ml_row->height;
1066 else
1067 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1068 }
1069
1070 if (WINDOW_WANTS_HEADER_LINE_P (w))
1071 {
1072 struct glyph_row *hl_row
1073 = (w->current_matrix && w->current_matrix->rows
1074 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1075 : 0);
1076 if (hl_row && hl_row->mode_line_p)
1077 height -= hl_row->height;
1078 else
1079 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1080 }
1081
1082 /* With a very small font and a mode-line that's taller than
1083 default, we might end up with a negative height. */
1084 return max (0, height);
1085 }
1086
1087 /* Return the window-relative coordinate of the left edge of display
1088 area AREA of window W. AREA < 0 means return the left edge of the
1089 whole window, to the right of the left fringe of W. */
1090
1091 INLINE int
1092 window_box_left_offset (w, area)
1093 struct window *w;
1094 int area;
1095 {
1096 int x;
1097
1098 if (w->pseudo_window_p)
1099 return 0;
1100
1101 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1102
1103 if (area == TEXT_AREA)
1104 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1105 + window_box_width (w, LEFT_MARGIN_AREA));
1106 else if (area == RIGHT_MARGIN_AREA)
1107 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1108 + window_box_width (w, LEFT_MARGIN_AREA)
1109 + window_box_width (w, TEXT_AREA)
1110 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1111 ? 0
1112 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1113 else if (area == LEFT_MARGIN_AREA
1114 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1115 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1116
1117 return x;
1118 }
1119
1120
1121 /* Return the window-relative coordinate of the right edge of display
1122 area AREA of window W. AREA < 0 means return the left edge of the
1123 whole window, to the left of the right fringe of W. */
1124
1125 INLINE int
1126 window_box_right_offset (w, area)
1127 struct window *w;
1128 int area;
1129 {
1130 return window_box_left_offset (w, area) + window_box_width (w, area);
1131 }
1132
1133 /* Return the frame-relative coordinate of the left edge of display
1134 area AREA of window W. AREA < 0 means return the left edge of the
1135 whole window, to the right of the left fringe of W. */
1136
1137 INLINE int
1138 window_box_left (w, area)
1139 struct window *w;
1140 int area;
1141 {
1142 struct frame *f = XFRAME (w->frame);
1143 int x;
1144
1145 if (w->pseudo_window_p)
1146 return FRAME_INTERNAL_BORDER_WIDTH (f);
1147
1148 x = (WINDOW_LEFT_EDGE_X (w)
1149 + window_box_left_offset (w, area));
1150
1151 return x;
1152 }
1153
1154
1155 /* Return the frame-relative coordinate of the right edge of display
1156 area AREA of window W. AREA < 0 means return the left edge of the
1157 whole window, to the left of the right fringe of W. */
1158
1159 INLINE int
1160 window_box_right (w, area)
1161 struct window *w;
1162 int area;
1163 {
1164 return window_box_left (w, area) + window_box_width (w, area);
1165 }
1166
1167 /* Get the bounding box of the display area AREA of window W, without
1168 mode lines, in frame-relative coordinates. AREA < 0 means the
1169 whole window, not including the left and right fringes of
1170 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1171 coordinates of the upper-left corner of the box. Return in
1172 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1173
1174 INLINE void
1175 window_box (w, area, box_x, box_y, box_width, box_height)
1176 struct window *w;
1177 int area;
1178 int *box_x, *box_y, *box_width, *box_height;
1179 {
1180 if (box_width)
1181 *box_width = window_box_width (w, area);
1182 if (box_height)
1183 *box_height = window_box_height (w);
1184 if (box_x)
1185 *box_x = window_box_left (w, area);
1186 if (box_y)
1187 {
1188 *box_y = WINDOW_TOP_EDGE_Y (w);
1189 if (WINDOW_WANTS_HEADER_LINE_P (w))
1190 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1191 }
1192 }
1193
1194
1195 /* Get the bounding box of the display area AREA of window W, without
1196 mode lines. AREA < 0 means the whole window, not including the
1197 left and right fringe of the window. Return in *TOP_LEFT_X
1198 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1199 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1200 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1201 box. */
1202
1203 INLINE void
1204 window_box_edges (w, area, top_left_x, top_left_y,
1205 bottom_right_x, bottom_right_y)
1206 struct window *w;
1207 int area;
1208 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1209 {
1210 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1211 bottom_right_y);
1212 *bottom_right_x += *top_left_x;
1213 *bottom_right_y += *top_left_y;
1214 }
1215
1216
1217 \f
1218 /***********************************************************************
1219 Utilities
1220 ***********************************************************************/
1221
1222 /* Return the bottom y-position of the line the iterator IT is in.
1223 This can modify IT's settings. */
1224
1225 int
1226 line_bottom_y (it)
1227 struct it *it;
1228 {
1229 int line_height = it->max_ascent + it->max_descent;
1230 int line_top_y = it->current_y;
1231
1232 if (line_height == 0)
1233 {
1234 if (last_height)
1235 line_height = last_height;
1236 else if (IT_CHARPOS (*it) < ZV)
1237 {
1238 move_it_by_lines (it, 1, 1);
1239 line_height = (it->max_ascent || it->max_descent
1240 ? it->max_ascent + it->max_descent
1241 : last_height);
1242 }
1243 else
1244 {
1245 struct glyph_row *row = it->glyph_row;
1246
1247 /* Use the default character height. */
1248 it->glyph_row = NULL;
1249 it->what = IT_CHARACTER;
1250 it->c = ' ';
1251 it->len = 1;
1252 PRODUCE_GLYPHS (it);
1253 line_height = it->ascent + it->descent;
1254 it->glyph_row = row;
1255 }
1256 }
1257
1258 return line_top_y + line_height;
1259 }
1260
1261
1262 /* Return 1 if position CHARPOS is visible in window W.
1263 If visible, set *X and *Y to pixel coordinates of top left corner.
1264 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1265 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1266 and header-lines heights. */
1267
1268 int
1269 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1270 struct window *w;
1271 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1272 {
1273 struct it it;
1274 struct text_pos top;
1275 int visible_p = 0;
1276 struct buffer *old_buffer = NULL;
1277
1278 if (noninteractive)
1279 return visible_p;
1280
1281 if (XBUFFER (w->buffer) != current_buffer)
1282 {
1283 old_buffer = current_buffer;
1284 set_buffer_internal_1 (XBUFFER (w->buffer));
1285 }
1286
1287 SET_TEXT_POS_FROM_MARKER (top, w->start);
1288
1289 /* Compute exact mode line heights, if requested. */
1290 if (exact_mode_line_heights_p)
1291 {
1292 if (WINDOW_WANTS_MODELINE_P (w))
1293 current_mode_line_height
1294 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1295 current_buffer->mode_line_format);
1296
1297 if (WINDOW_WANTS_HEADER_LINE_P (w))
1298 current_header_line_height
1299 = display_mode_line (w, HEADER_LINE_FACE_ID,
1300 current_buffer->header_line_format);
1301 }
1302
1303 start_display (&it, w, top);
1304 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1305 MOVE_TO_POS | MOVE_TO_Y);
1306
1307 /* Note that we may overshoot because of invisible text. */
1308 if (IT_CHARPOS (it) >= charpos)
1309 {
1310 int top_x = it.current_x;
1311 int top_y = it.current_y;
1312 int bottom_y = (last_height = 0, line_bottom_y (&it));
1313 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1314
1315 if (top_y < window_top_y)
1316 visible_p = bottom_y > window_top_y;
1317 else if (top_y < it.last_visible_y)
1318 visible_p = 1;
1319 if (visible_p)
1320 {
1321 *x = top_x;
1322 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1323 *rtop = max (0, window_top_y - top_y);
1324 *rbot = max (0, bottom_y - it.last_visible_y);
1325 }
1326 }
1327 else
1328 {
1329 struct it it2;
1330
1331 it2 = it;
1332 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1333 move_it_by_lines (&it, 1, 0);
1334 if (charpos < IT_CHARPOS (it))
1335 {
1336 visible_p = 1;
1337 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1338 *x = it2.current_x;
1339 *y = it2.current_y + it2.max_ascent - it2.ascent;
1340 *rtop = max (0, -it2.current_y);
1341 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1342 - it.last_visible_y));
1343 }
1344 }
1345
1346 if (old_buffer)
1347 set_buffer_internal_1 (old_buffer);
1348
1349 current_header_line_height = current_mode_line_height = -1;
1350
1351 return visible_p;
1352 }
1353
1354
1355 /* Return the next character from STR which is MAXLEN bytes long.
1356 Return in *LEN the length of the character. This is like
1357 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1358 we find one, we return a `?', but with the length of the invalid
1359 character. */
1360
1361 static INLINE int
1362 string_char_and_length (str, maxlen, len)
1363 const unsigned char *str;
1364 int maxlen, *len;
1365 {
1366 int c;
1367
1368 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1369 if (!CHAR_VALID_P (c, 1))
1370 /* We may not change the length here because other places in Emacs
1371 don't use this function, i.e. they silently accept invalid
1372 characters. */
1373 c = '?';
1374
1375 return c;
1376 }
1377
1378
1379
1380 /* Given a position POS containing a valid character and byte position
1381 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1382
1383 static struct text_pos
1384 string_pos_nchars_ahead (pos, string, nchars)
1385 struct text_pos pos;
1386 Lisp_Object string;
1387 int nchars;
1388 {
1389 xassert (STRINGP (string) && nchars >= 0);
1390
1391 if (STRING_MULTIBYTE (string))
1392 {
1393 int rest = SBYTES (string) - BYTEPOS (pos);
1394 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1395 int len;
1396
1397 while (nchars--)
1398 {
1399 string_char_and_length (p, rest, &len);
1400 p += len, rest -= len;
1401 xassert (rest >= 0);
1402 CHARPOS (pos) += 1;
1403 BYTEPOS (pos) += len;
1404 }
1405 }
1406 else
1407 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1408
1409 return pos;
1410 }
1411
1412
1413 /* Value is the text position, i.e. character and byte position,
1414 for character position CHARPOS in STRING. */
1415
1416 static INLINE struct text_pos
1417 string_pos (charpos, string)
1418 int charpos;
1419 Lisp_Object string;
1420 {
1421 struct text_pos pos;
1422 xassert (STRINGP (string));
1423 xassert (charpos >= 0);
1424 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1425 return pos;
1426 }
1427
1428
1429 /* Value is a text position, i.e. character and byte position, for
1430 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1431 means recognize multibyte characters. */
1432
1433 static struct text_pos
1434 c_string_pos (charpos, s, multibyte_p)
1435 int charpos;
1436 unsigned char *s;
1437 int multibyte_p;
1438 {
1439 struct text_pos pos;
1440
1441 xassert (s != NULL);
1442 xassert (charpos >= 0);
1443
1444 if (multibyte_p)
1445 {
1446 int rest = strlen (s), len;
1447
1448 SET_TEXT_POS (pos, 0, 0);
1449 while (charpos--)
1450 {
1451 string_char_and_length (s, rest, &len);
1452 s += len, rest -= len;
1453 xassert (rest >= 0);
1454 CHARPOS (pos) += 1;
1455 BYTEPOS (pos) += len;
1456 }
1457 }
1458 else
1459 SET_TEXT_POS (pos, charpos, charpos);
1460
1461 return pos;
1462 }
1463
1464
1465 /* Value is the number of characters in C string S. MULTIBYTE_P
1466 non-zero means recognize multibyte characters. */
1467
1468 static int
1469 number_of_chars (s, multibyte_p)
1470 unsigned char *s;
1471 int multibyte_p;
1472 {
1473 int nchars;
1474
1475 if (multibyte_p)
1476 {
1477 int rest = strlen (s), len;
1478 unsigned char *p = (unsigned char *) s;
1479
1480 for (nchars = 0; rest > 0; ++nchars)
1481 {
1482 string_char_and_length (p, rest, &len);
1483 rest -= len, p += len;
1484 }
1485 }
1486 else
1487 nchars = strlen (s);
1488
1489 return nchars;
1490 }
1491
1492
1493 /* Compute byte position NEWPOS->bytepos corresponding to
1494 NEWPOS->charpos. POS is a known position in string STRING.
1495 NEWPOS->charpos must be >= POS.charpos. */
1496
1497 static void
1498 compute_string_pos (newpos, pos, string)
1499 struct text_pos *newpos, pos;
1500 Lisp_Object string;
1501 {
1502 xassert (STRINGP (string));
1503 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1504
1505 if (STRING_MULTIBYTE (string))
1506 *newpos = string_pos_nchars_ahead (pos, string,
1507 CHARPOS (*newpos) - CHARPOS (pos));
1508 else
1509 BYTEPOS (*newpos) = CHARPOS (*newpos);
1510 }
1511
1512 /* EXPORT:
1513 Return an estimation of the pixel height of mode or top lines on
1514 frame F. FACE_ID specifies what line's height to estimate. */
1515
1516 int
1517 estimate_mode_line_height (f, face_id)
1518 struct frame *f;
1519 enum face_id face_id;
1520 {
1521 #ifdef HAVE_WINDOW_SYSTEM
1522 if (FRAME_WINDOW_P (f))
1523 {
1524 int height = FONT_HEIGHT (FRAME_FONT (f));
1525
1526 /* This function is called so early when Emacs starts that the face
1527 cache and mode line face are not yet initialized. */
1528 if (FRAME_FACE_CACHE (f))
1529 {
1530 struct face *face = FACE_FROM_ID (f, face_id);
1531 if (face)
1532 {
1533 if (face->font)
1534 height = FONT_HEIGHT (face->font);
1535 if (face->box_line_width > 0)
1536 height += 2 * face->box_line_width;
1537 }
1538 }
1539
1540 return height;
1541 }
1542 #endif
1543
1544 return 1;
1545 }
1546
1547 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1548 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1549 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1550 not force the value into range. */
1551
1552 void
1553 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1554 FRAME_PTR f;
1555 register int pix_x, pix_y;
1556 int *x, *y;
1557 NativeRectangle *bounds;
1558 int noclip;
1559 {
1560
1561 #ifdef HAVE_WINDOW_SYSTEM
1562 if (FRAME_WINDOW_P (f))
1563 {
1564 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1565 even for negative values. */
1566 if (pix_x < 0)
1567 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1568 if (pix_y < 0)
1569 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1570
1571 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1572 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1573
1574 if (bounds)
1575 STORE_NATIVE_RECT (*bounds,
1576 FRAME_COL_TO_PIXEL_X (f, pix_x),
1577 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1578 FRAME_COLUMN_WIDTH (f) - 1,
1579 FRAME_LINE_HEIGHT (f) - 1);
1580
1581 if (!noclip)
1582 {
1583 if (pix_x < 0)
1584 pix_x = 0;
1585 else if (pix_x > FRAME_TOTAL_COLS (f))
1586 pix_x = FRAME_TOTAL_COLS (f);
1587
1588 if (pix_y < 0)
1589 pix_y = 0;
1590 else if (pix_y > FRAME_LINES (f))
1591 pix_y = FRAME_LINES (f);
1592 }
1593 }
1594 #endif
1595
1596 *x = pix_x;
1597 *y = pix_y;
1598 }
1599
1600
1601 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1602 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1603 can't tell the positions because W's display is not up to date,
1604 return 0. */
1605
1606 int
1607 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1608 struct window *w;
1609 int hpos, vpos;
1610 int *frame_x, *frame_y;
1611 {
1612 #ifdef HAVE_WINDOW_SYSTEM
1613 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1614 {
1615 int success_p;
1616
1617 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1618 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1619
1620 if (display_completed)
1621 {
1622 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1623 struct glyph *glyph = row->glyphs[TEXT_AREA];
1624 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1625
1626 hpos = row->x;
1627 vpos = row->y;
1628 while (glyph < end)
1629 {
1630 hpos += glyph->pixel_width;
1631 ++glyph;
1632 }
1633
1634 /* If first glyph is partially visible, its first visible position is still 0. */
1635 if (hpos < 0)
1636 hpos = 0;
1637
1638 success_p = 1;
1639 }
1640 else
1641 {
1642 hpos = vpos = 0;
1643 success_p = 0;
1644 }
1645
1646 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1647 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1648 return success_p;
1649 }
1650 #endif
1651
1652 *frame_x = hpos;
1653 *frame_y = vpos;
1654 return 1;
1655 }
1656
1657
1658 #ifdef HAVE_WINDOW_SYSTEM
1659
1660 /* Find the glyph under window-relative coordinates X/Y in window W.
1661 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1662 strings. Return in *HPOS and *VPOS the row and column number of
1663 the glyph found. Return in *AREA the glyph area containing X.
1664 Value is a pointer to the glyph found or null if X/Y is not on
1665 text, or we can't tell because W's current matrix is not up to
1666 date. */
1667
1668 static struct glyph *
1669 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1670 struct window *w;
1671 int x, y;
1672 int *hpos, *vpos, *dx, *dy, *area;
1673 {
1674 struct glyph *glyph, *end;
1675 struct glyph_row *row = NULL;
1676 int x0, i;
1677
1678 /* Find row containing Y. Give up if some row is not enabled. */
1679 for (i = 0; i < w->current_matrix->nrows; ++i)
1680 {
1681 row = MATRIX_ROW (w->current_matrix, i);
1682 if (!row->enabled_p)
1683 return NULL;
1684 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1685 break;
1686 }
1687
1688 *vpos = i;
1689 *hpos = 0;
1690
1691 /* Give up if Y is not in the window. */
1692 if (i == w->current_matrix->nrows)
1693 return NULL;
1694
1695 /* Get the glyph area containing X. */
1696 if (w->pseudo_window_p)
1697 {
1698 *area = TEXT_AREA;
1699 x0 = 0;
1700 }
1701 else
1702 {
1703 if (x < window_box_left_offset (w, TEXT_AREA))
1704 {
1705 *area = LEFT_MARGIN_AREA;
1706 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1707 }
1708 else if (x < window_box_right_offset (w, TEXT_AREA))
1709 {
1710 *area = TEXT_AREA;
1711 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1712 }
1713 else
1714 {
1715 *area = RIGHT_MARGIN_AREA;
1716 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1717 }
1718 }
1719
1720 /* Find glyph containing X. */
1721 glyph = row->glyphs[*area];
1722 end = glyph + row->used[*area];
1723 x -= x0;
1724 while (glyph < end && x >= glyph->pixel_width)
1725 {
1726 x -= glyph->pixel_width;
1727 ++glyph;
1728 }
1729
1730 if (glyph == end)
1731 return NULL;
1732
1733 if (dx)
1734 {
1735 *dx = x;
1736 *dy = y - (row->y + row->ascent - glyph->ascent);
1737 }
1738
1739 *hpos = glyph - row->glyphs[*area];
1740 return glyph;
1741 }
1742
1743
1744 /* EXPORT:
1745 Convert frame-relative x/y to coordinates relative to window W.
1746 Takes pseudo-windows into account. */
1747
1748 void
1749 frame_to_window_pixel_xy (w, x, y)
1750 struct window *w;
1751 int *x, *y;
1752 {
1753 if (w->pseudo_window_p)
1754 {
1755 /* A pseudo-window is always full-width, and starts at the
1756 left edge of the frame, plus a frame border. */
1757 struct frame *f = XFRAME (w->frame);
1758 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1759 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1760 }
1761 else
1762 {
1763 *x -= WINDOW_LEFT_EDGE_X (w);
1764 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1765 }
1766 }
1767
1768 /* EXPORT:
1769 Return in *R the clipping rectangle for glyph string S. */
1770
1771 void
1772 get_glyph_string_clip_rect (s, nr)
1773 struct glyph_string *s;
1774 NativeRectangle *nr;
1775 {
1776 XRectangle r;
1777
1778 if (s->row->full_width_p)
1779 {
1780 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1781 r.x = WINDOW_LEFT_EDGE_X (s->w);
1782 r.width = WINDOW_TOTAL_WIDTH (s->w);
1783
1784 /* Unless displaying a mode or menu bar line, which are always
1785 fully visible, clip to the visible part of the row. */
1786 if (s->w->pseudo_window_p)
1787 r.height = s->row->visible_height;
1788 else
1789 r.height = s->height;
1790 }
1791 else
1792 {
1793 /* This is a text line that may be partially visible. */
1794 r.x = window_box_left (s->w, s->area);
1795 r.width = window_box_width (s->w, s->area);
1796 r.height = s->row->visible_height;
1797 }
1798
1799 if (s->clip_head)
1800 if (r.x < s->clip_head->x)
1801 {
1802 if (r.width >= s->clip_head->x - r.x)
1803 r.width -= s->clip_head->x - r.x;
1804 else
1805 r.width = 0;
1806 r.x = s->clip_head->x;
1807 }
1808 if (s->clip_tail)
1809 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1810 {
1811 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1812 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1813 else
1814 r.width = 0;
1815 }
1816
1817 /* If S draws overlapping rows, it's sufficient to use the top and
1818 bottom of the window for clipping because this glyph string
1819 intentionally draws over other lines. */
1820 if (s->for_overlaps_p)
1821 {
1822 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1823 r.height = window_text_bottom_y (s->w) - r.y;
1824 }
1825 else
1826 {
1827 /* Don't use S->y for clipping because it doesn't take partially
1828 visible lines into account. For example, it can be negative for
1829 partially visible lines at the top of a window. */
1830 if (!s->row->full_width_p
1831 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1832 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1833 else
1834 r.y = max (0, s->row->y);
1835
1836 /* If drawing a tool-bar window, draw it over the internal border
1837 at the top of the window. */
1838 if (WINDOWP (s->f->tool_bar_window)
1839 && s->w == XWINDOW (s->f->tool_bar_window))
1840 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1841 }
1842
1843 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1844
1845 /* If drawing the cursor, don't let glyph draw outside its
1846 advertised boundaries. Cleartype does this under some circumstances. */
1847 if (s->hl == DRAW_CURSOR)
1848 {
1849 struct glyph *glyph = s->first_glyph;
1850 int height, max_y;
1851
1852 if (s->x > r.x)
1853 {
1854 r.width -= s->x - r.x;
1855 r.x = s->x;
1856 }
1857 r.width = min (r.width, glyph->pixel_width);
1858
1859 /* If r.y is below window bottom, ensure that we still see a cursor. */
1860 height = min (glyph->ascent + glyph->descent,
1861 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1862 max_y = window_text_bottom_y (s->w) - height;
1863 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1864 if (s->ybase - glyph->ascent > max_y)
1865 {
1866 r.y = max_y;
1867 r.height = height;
1868 }
1869 else
1870 {
1871 /* Don't draw cursor glyph taller than our actual glyph. */
1872 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1873 if (height < r.height)
1874 {
1875 max_y = r.y + r.height;
1876 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1877 r.height = min (max_y - r.y, height);
1878 }
1879 }
1880 }
1881
1882 #ifdef CONVERT_FROM_XRECT
1883 CONVERT_FROM_XRECT (r, *nr);
1884 #else
1885 *nr = r;
1886 #endif
1887 }
1888
1889
1890 /* EXPORT:
1891 Return the position and height of the phys cursor in window W.
1892 Set w->phys_cursor_width to width of phys cursor.
1893 */
1894
1895 int
1896 get_phys_cursor_geometry (w, row, glyph, heightp)
1897 struct window *w;
1898 struct glyph_row *row;
1899 struct glyph *glyph;
1900 int *heightp;
1901 {
1902 struct frame *f = XFRAME (WINDOW_FRAME (w));
1903 int x, y, wd, h, h0, y0;
1904
1905 /* Compute the width of the rectangle to draw. If on a stretch
1906 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1907 rectangle as wide as the glyph, but use a canonical character
1908 width instead. */
1909 wd = glyph->pixel_width - 1;
1910 #ifdef HAVE_NTGUI
1911 wd++; /* Why? */
1912 #endif
1913 if (glyph->type == STRETCH_GLYPH
1914 && !x_stretch_cursor_p)
1915 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1916 w->phys_cursor_width = wd;
1917
1918 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1919
1920 /* If y is below window bottom, ensure that we still see a cursor. */
1921 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1922
1923 h = max (h0, glyph->ascent + glyph->descent);
1924 h0 = min (h0, glyph->ascent + glyph->descent);
1925
1926 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1927 if (y < y0)
1928 {
1929 h = max (h - (y0 - y) + 1, h0);
1930 y = y0 - 1;
1931 }
1932 else
1933 {
1934 y0 = window_text_bottom_y (w) - h0;
1935 if (y > y0)
1936 {
1937 h += y - y0;
1938 y = y0;
1939 }
1940 }
1941
1942 *heightp = h - 1;
1943 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1944 }
1945
1946
1947 #endif /* HAVE_WINDOW_SYSTEM */
1948
1949 \f
1950 /***********************************************************************
1951 Lisp form evaluation
1952 ***********************************************************************/
1953
1954 /* Error handler for safe_eval and safe_call. */
1955
1956 static Lisp_Object
1957 safe_eval_handler (arg)
1958 Lisp_Object arg;
1959 {
1960 add_to_log ("Error during redisplay: %s", arg, Qnil);
1961 return Qnil;
1962 }
1963
1964
1965 /* Evaluate SEXPR and return the result, or nil if something went
1966 wrong. Prevent redisplay during the evaluation. */
1967
1968 Lisp_Object
1969 safe_eval (sexpr)
1970 Lisp_Object sexpr;
1971 {
1972 Lisp_Object val;
1973
1974 if (inhibit_eval_during_redisplay)
1975 val = Qnil;
1976 else
1977 {
1978 int count = SPECPDL_INDEX ();
1979 struct gcpro gcpro1;
1980
1981 GCPRO1 (sexpr);
1982 specbind (Qinhibit_redisplay, Qt);
1983 /* Use Qt to ensure debugger does not run,
1984 so there is no possibility of wanting to redisplay. */
1985 val = internal_condition_case_1 (Feval, sexpr, Qt,
1986 safe_eval_handler);
1987 UNGCPRO;
1988 val = unbind_to (count, val);
1989 }
1990
1991 return val;
1992 }
1993
1994
1995 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1996 Return the result, or nil if something went wrong. Prevent
1997 redisplay during the evaluation. */
1998
1999 Lisp_Object
2000 safe_call (nargs, args)
2001 int nargs;
2002 Lisp_Object *args;
2003 {
2004 Lisp_Object val;
2005
2006 if (inhibit_eval_during_redisplay)
2007 val = Qnil;
2008 else
2009 {
2010 int count = SPECPDL_INDEX ();
2011 struct gcpro gcpro1;
2012
2013 GCPRO1 (args[0]);
2014 gcpro1.nvars = nargs;
2015 specbind (Qinhibit_redisplay, Qt);
2016 /* Use Qt to ensure debugger does not run,
2017 so there is no possibility of wanting to redisplay. */
2018 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2019 safe_eval_handler);
2020 UNGCPRO;
2021 val = unbind_to (count, val);
2022 }
2023
2024 return val;
2025 }
2026
2027
2028 /* Call function FN with one argument ARG.
2029 Return the result, or nil if something went wrong. */
2030
2031 Lisp_Object
2032 safe_call1 (fn, arg)
2033 Lisp_Object fn, arg;
2034 {
2035 Lisp_Object args[2];
2036 args[0] = fn;
2037 args[1] = arg;
2038 return safe_call (2, args);
2039 }
2040
2041
2042 \f
2043 /***********************************************************************
2044 Debugging
2045 ***********************************************************************/
2046
2047 #if 0
2048
2049 /* Define CHECK_IT to perform sanity checks on iterators.
2050 This is for debugging. It is too slow to do unconditionally. */
2051
2052 static void
2053 check_it (it)
2054 struct it *it;
2055 {
2056 if (it->method == GET_FROM_STRING)
2057 {
2058 xassert (STRINGP (it->string));
2059 xassert (IT_STRING_CHARPOS (*it) >= 0);
2060 }
2061 else
2062 {
2063 xassert (IT_STRING_CHARPOS (*it) < 0);
2064 if (it->method == GET_FROM_BUFFER)
2065 {
2066 /* Check that character and byte positions agree. */
2067 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2068 }
2069 }
2070
2071 if (it->dpvec)
2072 xassert (it->current.dpvec_index >= 0);
2073 else
2074 xassert (it->current.dpvec_index < 0);
2075 }
2076
2077 #define CHECK_IT(IT) check_it ((IT))
2078
2079 #else /* not 0 */
2080
2081 #define CHECK_IT(IT) (void) 0
2082
2083 #endif /* not 0 */
2084
2085
2086 #if GLYPH_DEBUG
2087
2088 /* Check that the window end of window W is what we expect it
2089 to be---the last row in the current matrix displaying text. */
2090
2091 static void
2092 check_window_end (w)
2093 struct window *w;
2094 {
2095 if (!MINI_WINDOW_P (w)
2096 && !NILP (w->window_end_valid))
2097 {
2098 struct glyph_row *row;
2099 xassert ((row = MATRIX_ROW (w->current_matrix,
2100 XFASTINT (w->window_end_vpos)),
2101 !row->enabled_p
2102 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2103 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2104 }
2105 }
2106
2107 #define CHECK_WINDOW_END(W) check_window_end ((W))
2108
2109 #else /* not GLYPH_DEBUG */
2110
2111 #define CHECK_WINDOW_END(W) (void) 0
2112
2113 #endif /* not GLYPH_DEBUG */
2114
2115
2116 \f
2117 /***********************************************************************
2118 Iterator initialization
2119 ***********************************************************************/
2120
2121 /* Initialize IT for displaying current_buffer in window W, starting
2122 at character position CHARPOS. CHARPOS < 0 means that no buffer
2123 position is specified which is useful when the iterator is assigned
2124 a position later. BYTEPOS is the byte position corresponding to
2125 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2126
2127 If ROW is not null, calls to produce_glyphs with IT as parameter
2128 will produce glyphs in that row.
2129
2130 BASE_FACE_ID is the id of a base face to use. It must be one of
2131 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2132 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2133 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2134
2135 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2136 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2137 will be initialized to use the corresponding mode line glyph row of
2138 the desired matrix of W. */
2139
2140 void
2141 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2142 struct it *it;
2143 struct window *w;
2144 int charpos, bytepos;
2145 struct glyph_row *row;
2146 enum face_id base_face_id;
2147 {
2148 int highlight_region_p;
2149
2150 /* Some precondition checks. */
2151 xassert (w != NULL && it != NULL);
2152 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2153 && charpos <= ZV));
2154
2155 /* If face attributes have been changed since the last redisplay,
2156 free realized faces now because they depend on face definitions
2157 that might have changed. Don't free faces while there might be
2158 desired matrices pending which reference these faces. */
2159 if (face_change_count && !inhibit_free_realized_faces)
2160 {
2161 face_change_count = 0;
2162 free_all_realized_faces (Qnil);
2163 }
2164
2165 /* Use one of the mode line rows of W's desired matrix if
2166 appropriate. */
2167 if (row == NULL)
2168 {
2169 if (base_face_id == MODE_LINE_FACE_ID
2170 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2171 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2172 else if (base_face_id == HEADER_LINE_FACE_ID)
2173 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2174 }
2175
2176 /* Clear IT. */
2177 bzero (it, sizeof *it);
2178 it->current.overlay_string_index = -1;
2179 it->current.dpvec_index = -1;
2180 it->base_face_id = base_face_id;
2181 it->string = Qnil;
2182 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2183
2184 /* The window in which we iterate over current_buffer: */
2185 XSETWINDOW (it->window, w);
2186 it->w = w;
2187 it->f = XFRAME (w->frame);
2188
2189 /* Extra space between lines (on window systems only). */
2190 if (base_face_id == DEFAULT_FACE_ID
2191 && FRAME_WINDOW_P (it->f))
2192 {
2193 if (NATNUMP (current_buffer->extra_line_spacing))
2194 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2195 else if (FLOATP (current_buffer->extra_line_spacing))
2196 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2197 * FRAME_LINE_HEIGHT (it->f));
2198 else if (it->f->extra_line_spacing > 0)
2199 it->extra_line_spacing = it->f->extra_line_spacing;
2200 it->max_extra_line_spacing = 0;
2201 }
2202
2203 /* If realized faces have been removed, e.g. because of face
2204 attribute changes of named faces, recompute them. When running
2205 in batch mode, the face cache of Vterminal_frame is null. If
2206 we happen to get called, make a dummy face cache. */
2207 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2208 init_frame_faces (it->f);
2209 if (FRAME_FACE_CACHE (it->f)->used == 0)
2210 recompute_basic_faces (it->f);
2211
2212 /* Current value of the `slice', `space-width', and 'height' properties. */
2213 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2214 it->space_width = Qnil;
2215 it->font_height = Qnil;
2216 it->override_ascent = -1;
2217
2218 /* Are control characters displayed as `^C'? */
2219 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2220
2221 /* -1 means everything between a CR and the following line end
2222 is invisible. >0 means lines indented more than this value are
2223 invisible. */
2224 it->selective = (INTEGERP (current_buffer->selective_display)
2225 ? XFASTINT (current_buffer->selective_display)
2226 : (!NILP (current_buffer->selective_display)
2227 ? -1 : 0));
2228 it->selective_display_ellipsis_p
2229 = !NILP (current_buffer->selective_display_ellipses);
2230
2231 /* Display table to use. */
2232 it->dp = window_display_table (w);
2233
2234 /* Are multibyte characters enabled in current_buffer? */
2235 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2236
2237 /* Non-zero if we should highlight the region. */
2238 highlight_region_p
2239 = (!NILP (Vtransient_mark_mode)
2240 && !NILP (current_buffer->mark_active)
2241 && XMARKER (current_buffer->mark)->buffer != 0);
2242
2243 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2244 start and end of a visible region in window IT->w. Set both to
2245 -1 to indicate no region. */
2246 if (highlight_region_p
2247 /* Maybe highlight only in selected window. */
2248 && (/* Either show region everywhere. */
2249 highlight_nonselected_windows
2250 /* Or show region in the selected window. */
2251 || w == XWINDOW (selected_window)
2252 /* Or show the region if we are in the mini-buffer and W is
2253 the window the mini-buffer refers to. */
2254 || (MINI_WINDOW_P (XWINDOW (selected_window))
2255 && WINDOWP (minibuf_selected_window)
2256 && w == XWINDOW (minibuf_selected_window))))
2257 {
2258 int charpos = marker_position (current_buffer->mark);
2259 it->region_beg_charpos = min (PT, charpos);
2260 it->region_end_charpos = max (PT, charpos);
2261 }
2262 else
2263 it->region_beg_charpos = it->region_end_charpos = -1;
2264
2265 /* Get the position at which the redisplay_end_trigger hook should
2266 be run, if it is to be run at all. */
2267 if (MARKERP (w->redisplay_end_trigger)
2268 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2269 it->redisplay_end_trigger_charpos
2270 = marker_position (w->redisplay_end_trigger);
2271 else if (INTEGERP (w->redisplay_end_trigger))
2272 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2273
2274 /* Correct bogus values of tab_width. */
2275 it->tab_width = XINT (current_buffer->tab_width);
2276 if (it->tab_width <= 0 || it->tab_width > 1000)
2277 it->tab_width = 8;
2278
2279 /* Are lines in the display truncated? */
2280 it->truncate_lines_p
2281 = (base_face_id != DEFAULT_FACE_ID
2282 || XINT (it->w->hscroll)
2283 || (truncate_partial_width_windows
2284 && !WINDOW_FULL_WIDTH_P (it->w))
2285 || !NILP (current_buffer->truncate_lines));
2286
2287 /* Get dimensions of truncation and continuation glyphs. These are
2288 displayed as fringe bitmaps under X, so we don't need them for such
2289 frames. */
2290 if (!FRAME_WINDOW_P (it->f))
2291 {
2292 if (it->truncate_lines_p)
2293 {
2294 /* We will need the truncation glyph. */
2295 xassert (it->glyph_row == NULL);
2296 produce_special_glyphs (it, IT_TRUNCATION);
2297 it->truncation_pixel_width = it->pixel_width;
2298 }
2299 else
2300 {
2301 /* We will need the continuation glyph. */
2302 xassert (it->glyph_row == NULL);
2303 produce_special_glyphs (it, IT_CONTINUATION);
2304 it->continuation_pixel_width = it->pixel_width;
2305 }
2306
2307 /* Reset these values to zero because the produce_special_glyphs
2308 above has changed them. */
2309 it->pixel_width = it->ascent = it->descent = 0;
2310 it->phys_ascent = it->phys_descent = 0;
2311 }
2312
2313 /* Set this after getting the dimensions of truncation and
2314 continuation glyphs, so that we don't produce glyphs when calling
2315 produce_special_glyphs, above. */
2316 it->glyph_row = row;
2317 it->area = TEXT_AREA;
2318
2319 /* Get the dimensions of the display area. The display area
2320 consists of the visible window area plus a horizontally scrolled
2321 part to the left of the window. All x-values are relative to the
2322 start of this total display area. */
2323 if (base_face_id != DEFAULT_FACE_ID)
2324 {
2325 /* Mode lines, menu bar in terminal frames. */
2326 it->first_visible_x = 0;
2327 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2328 }
2329 else
2330 {
2331 it->first_visible_x
2332 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2333 it->last_visible_x = (it->first_visible_x
2334 + window_box_width (w, TEXT_AREA));
2335
2336 /* If we truncate lines, leave room for the truncator glyph(s) at
2337 the right margin. Otherwise, leave room for the continuation
2338 glyph(s). Truncation and continuation glyphs are not inserted
2339 for window-based redisplay. */
2340 if (!FRAME_WINDOW_P (it->f))
2341 {
2342 if (it->truncate_lines_p)
2343 it->last_visible_x -= it->truncation_pixel_width;
2344 else
2345 it->last_visible_x -= it->continuation_pixel_width;
2346 }
2347
2348 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2349 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2350 }
2351
2352 /* Leave room for a border glyph. */
2353 if (!FRAME_WINDOW_P (it->f)
2354 && !WINDOW_RIGHTMOST_P (it->w))
2355 it->last_visible_x -= 1;
2356
2357 it->last_visible_y = window_text_bottom_y (w);
2358
2359 /* For mode lines and alike, arrange for the first glyph having a
2360 left box line if the face specifies a box. */
2361 if (base_face_id != DEFAULT_FACE_ID)
2362 {
2363 struct face *face;
2364
2365 it->face_id = base_face_id;
2366
2367 /* If we have a boxed mode line, make the first character appear
2368 with a left box line. */
2369 face = FACE_FROM_ID (it->f, base_face_id);
2370 if (face->box != FACE_NO_BOX)
2371 it->start_of_box_run_p = 1;
2372 }
2373
2374 /* If a buffer position was specified, set the iterator there,
2375 getting overlays and face properties from that position. */
2376 if (charpos >= BUF_BEG (current_buffer))
2377 {
2378 it->end_charpos = ZV;
2379 it->face_id = -1;
2380 IT_CHARPOS (*it) = charpos;
2381
2382 /* Compute byte position if not specified. */
2383 if (bytepos < charpos)
2384 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2385 else
2386 IT_BYTEPOS (*it) = bytepos;
2387
2388 it->start = it->current;
2389
2390 /* Compute faces etc. */
2391 reseat (it, it->current.pos, 1);
2392 }
2393
2394 CHECK_IT (it);
2395 }
2396
2397
2398 /* Initialize IT for the display of window W with window start POS. */
2399
2400 void
2401 start_display (it, w, pos)
2402 struct it *it;
2403 struct window *w;
2404 struct text_pos pos;
2405 {
2406 struct glyph_row *row;
2407 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2408
2409 row = w->desired_matrix->rows + first_vpos;
2410 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2411 it->first_vpos = first_vpos;
2412
2413 if (!it->truncate_lines_p)
2414 {
2415 int start_at_line_beg_p;
2416 int first_y = it->current_y;
2417
2418 /* If window start is not at a line start, skip forward to POS to
2419 get the correct continuation lines width. */
2420 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2421 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2422 if (!start_at_line_beg_p)
2423 {
2424 int new_x;
2425
2426 reseat_at_previous_visible_line_start (it);
2427 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2428
2429 new_x = it->current_x + it->pixel_width;
2430
2431 /* If lines are continued, this line may end in the middle
2432 of a multi-glyph character (e.g. a control character
2433 displayed as \003, or in the middle of an overlay
2434 string). In this case move_it_to above will not have
2435 taken us to the start of the continuation line but to the
2436 end of the continued line. */
2437 if (it->current_x > 0
2438 && !it->truncate_lines_p /* Lines are continued. */
2439 && (/* And glyph doesn't fit on the line. */
2440 new_x > it->last_visible_x
2441 /* Or it fits exactly and we're on a window
2442 system frame. */
2443 || (new_x == it->last_visible_x
2444 && FRAME_WINDOW_P (it->f))))
2445 {
2446 if (it->current.dpvec_index >= 0
2447 || it->current.overlay_string_index >= 0)
2448 {
2449 set_iterator_to_next (it, 1);
2450 move_it_in_display_line_to (it, -1, -1, 0);
2451 }
2452
2453 it->continuation_lines_width += it->current_x;
2454 }
2455
2456 /* We're starting a new display line, not affected by the
2457 height of the continued line, so clear the appropriate
2458 fields in the iterator structure. */
2459 it->max_ascent = it->max_descent = 0;
2460 it->max_phys_ascent = it->max_phys_descent = 0;
2461
2462 it->current_y = first_y;
2463 it->vpos = 0;
2464 it->current_x = it->hpos = 0;
2465 }
2466 }
2467
2468 #if 0 /* Don't assert the following because start_display is sometimes
2469 called intentionally with a window start that is not at a
2470 line start. Please leave this code in as a comment. */
2471
2472 /* Window start should be on a line start, now. */
2473 xassert (it->continuation_lines_width
2474 || IT_CHARPOS (it) == BEGV
2475 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2476 #endif /* 0 */
2477 }
2478
2479
2480 /* Return 1 if POS is a position in ellipses displayed for invisible
2481 text. W is the window we display, for text property lookup. */
2482
2483 static int
2484 in_ellipses_for_invisible_text_p (pos, w)
2485 struct display_pos *pos;
2486 struct window *w;
2487 {
2488 Lisp_Object prop, window;
2489 int ellipses_p = 0;
2490 int charpos = CHARPOS (pos->pos);
2491
2492 /* If POS specifies a position in a display vector, this might
2493 be for an ellipsis displayed for invisible text. We won't
2494 get the iterator set up for delivering that ellipsis unless
2495 we make sure that it gets aware of the invisible text. */
2496 if (pos->dpvec_index >= 0
2497 && pos->overlay_string_index < 0
2498 && CHARPOS (pos->string_pos) < 0
2499 && charpos > BEGV
2500 && (XSETWINDOW (window, w),
2501 prop = Fget_char_property (make_number (charpos),
2502 Qinvisible, window),
2503 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2504 {
2505 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2506 window);
2507 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2508 }
2509
2510 return ellipses_p;
2511 }
2512
2513
2514 /* Initialize IT for stepping through current_buffer in window W,
2515 starting at position POS that includes overlay string and display
2516 vector/ control character translation position information. Value
2517 is zero if there are overlay strings with newlines at POS. */
2518
2519 static int
2520 init_from_display_pos (it, w, pos)
2521 struct it *it;
2522 struct window *w;
2523 struct display_pos *pos;
2524 {
2525 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2526 int i, overlay_strings_with_newlines = 0;
2527
2528 /* If POS specifies a position in a display vector, this might
2529 be for an ellipsis displayed for invisible text. We won't
2530 get the iterator set up for delivering that ellipsis unless
2531 we make sure that it gets aware of the invisible text. */
2532 if (in_ellipses_for_invisible_text_p (pos, w))
2533 {
2534 --charpos;
2535 bytepos = 0;
2536 }
2537
2538 /* Keep in mind: the call to reseat in init_iterator skips invisible
2539 text, so we might end up at a position different from POS. This
2540 is only a problem when POS is a row start after a newline and an
2541 overlay starts there with an after-string, and the overlay has an
2542 invisible property. Since we don't skip invisible text in
2543 display_line and elsewhere immediately after consuming the
2544 newline before the row start, such a POS will not be in a string,
2545 but the call to init_iterator below will move us to the
2546 after-string. */
2547 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2548
2549 /* This only scans the current chunk -- it should scan all chunks.
2550 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2551 to 16 in 22.1 to make this a lesser problem. */
2552 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2553 {
2554 const char *s = SDATA (it->overlay_strings[i]);
2555 const char *e = s + SBYTES (it->overlay_strings[i]);
2556
2557 while (s < e && *s != '\n')
2558 ++s;
2559
2560 if (s < e)
2561 {
2562 overlay_strings_with_newlines = 1;
2563 break;
2564 }
2565 }
2566
2567 /* If position is within an overlay string, set up IT to the right
2568 overlay string. */
2569 if (pos->overlay_string_index >= 0)
2570 {
2571 int relative_index;
2572
2573 /* If the first overlay string happens to have a `display'
2574 property for an image, the iterator will be set up for that
2575 image, and we have to undo that setup first before we can
2576 correct the overlay string index. */
2577 if (it->method == GET_FROM_IMAGE)
2578 pop_it (it);
2579
2580 /* We already have the first chunk of overlay strings in
2581 IT->overlay_strings. Load more until the one for
2582 pos->overlay_string_index is in IT->overlay_strings. */
2583 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2584 {
2585 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2586 it->current.overlay_string_index = 0;
2587 while (n--)
2588 {
2589 load_overlay_strings (it, 0);
2590 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2591 }
2592 }
2593
2594 it->current.overlay_string_index = pos->overlay_string_index;
2595 relative_index = (it->current.overlay_string_index
2596 % OVERLAY_STRING_CHUNK_SIZE);
2597 it->string = it->overlay_strings[relative_index];
2598 xassert (STRINGP (it->string));
2599 it->current.string_pos = pos->string_pos;
2600 it->method = GET_FROM_STRING;
2601 }
2602
2603 #if 0 /* This is bogus because POS not having an overlay string
2604 position does not mean it's after the string. Example: A
2605 line starting with a before-string and initialization of IT
2606 to the previous row's end position. */
2607 else if (it->current.overlay_string_index >= 0)
2608 {
2609 /* If POS says we're already after an overlay string ending at
2610 POS, make sure to pop the iterator because it will be in
2611 front of that overlay string. When POS is ZV, we've thereby
2612 also ``processed'' overlay strings at ZV. */
2613 while (it->sp)
2614 pop_it (it);
2615 it->current.overlay_string_index = -1;
2616 it->method = GET_FROM_BUFFER;
2617 if (CHARPOS (pos->pos) == ZV)
2618 it->overlay_strings_at_end_processed_p = 1;
2619 }
2620 #endif /* 0 */
2621
2622 if (CHARPOS (pos->string_pos) >= 0)
2623 {
2624 /* Recorded position is not in an overlay string, but in another
2625 string. This can only be a string from a `display' property.
2626 IT should already be filled with that string. */
2627 it->current.string_pos = pos->string_pos;
2628 xassert (STRINGP (it->string));
2629 }
2630
2631 /* Restore position in display vector translations, control
2632 character translations or ellipses. */
2633 if (pos->dpvec_index >= 0)
2634 {
2635 if (it->dpvec == NULL)
2636 get_next_display_element (it);
2637 xassert (it->dpvec && it->current.dpvec_index == 0);
2638 it->current.dpvec_index = pos->dpvec_index;
2639 }
2640
2641 CHECK_IT (it);
2642 return !overlay_strings_with_newlines;
2643 }
2644
2645
2646 /* Initialize IT for stepping through current_buffer in window W
2647 starting at ROW->start. */
2648
2649 static void
2650 init_to_row_start (it, w, row)
2651 struct it *it;
2652 struct window *w;
2653 struct glyph_row *row;
2654 {
2655 init_from_display_pos (it, w, &row->start);
2656 it->start = row->start;
2657 it->continuation_lines_width = row->continuation_lines_width;
2658 CHECK_IT (it);
2659 }
2660
2661
2662 /* Initialize IT for stepping through current_buffer in window W
2663 starting in the line following ROW, i.e. starting at ROW->end.
2664 Value is zero if there are overlay strings with newlines at ROW's
2665 end position. */
2666
2667 static int
2668 init_to_row_end (it, w, row)
2669 struct it *it;
2670 struct window *w;
2671 struct glyph_row *row;
2672 {
2673 int success = 0;
2674
2675 if (init_from_display_pos (it, w, &row->end))
2676 {
2677 if (row->continued_p)
2678 it->continuation_lines_width
2679 = row->continuation_lines_width + row->pixel_width;
2680 CHECK_IT (it);
2681 success = 1;
2682 }
2683
2684 return success;
2685 }
2686
2687
2688
2689 \f
2690 /***********************************************************************
2691 Text properties
2692 ***********************************************************************/
2693
2694 /* Called when IT reaches IT->stop_charpos. Handle text property and
2695 overlay changes. Set IT->stop_charpos to the next position where
2696 to stop. */
2697
2698 static void
2699 handle_stop (it)
2700 struct it *it;
2701 {
2702 enum prop_handled handled;
2703 int handle_overlay_change_p = 1;
2704 struct props *p;
2705
2706 it->dpvec = NULL;
2707 it->current.dpvec_index = -1;
2708
2709 /* Use face of preceding text for ellipsis (if invisible) */
2710 if (it->selective_display_ellipsis_p)
2711 it->saved_face_id = it->face_id;
2712
2713 do
2714 {
2715 handled = HANDLED_NORMALLY;
2716
2717 /* Call text property handlers. */
2718 for (p = it_props; p->handler; ++p)
2719 {
2720 handled = p->handler (it);
2721
2722 if (handled == HANDLED_RECOMPUTE_PROPS)
2723 break;
2724 else if (handled == HANDLED_RETURN)
2725 return;
2726 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2727 handle_overlay_change_p = 0;
2728 }
2729
2730 if (handled != HANDLED_RECOMPUTE_PROPS)
2731 {
2732 /* Don't check for overlay strings below when set to deliver
2733 characters from a display vector. */
2734 if (it->method == GET_FROM_DISPLAY_VECTOR)
2735 handle_overlay_change_p = 0;
2736
2737 /* Handle overlay changes. */
2738 if (handle_overlay_change_p)
2739 handled = handle_overlay_change (it);
2740
2741 /* Determine where to stop next. */
2742 if (handled == HANDLED_NORMALLY)
2743 compute_stop_pos (it);
2744 }
2745 }
2746 while (handled == HANDLED_RECOMPUTE_PROPS);
2747 }
2748
2749
2750 /* Compute IT->stop_charpos from text property and overlay change
2751 information for IT's current position. */
2752
2753 static void
2754 compute_stop_pos (it)
2755 struct it *it;
2756 {
2757 register INTERVAL iv, next_iv;
2758 Lisp_Object object, limit, position;
2759
2760 /* If nowhere else, stop at the end. */
2761 it->stop_charpos = it->end_charpos;
2762
2763 if (STRINGP (it->string))
2764 {
2765 /* Strings are usually short, so don't limit the search for
2766 properties. */
2767 object = it->string;
2768 limit = Qnil;
2769 position = make_number (IT_STRING_CHARPOS (*it));
2770 }
2771 else
2772 {
2773 int charpos;
2774
2775 /* If next overlay change is in front of the current stop pos
2776 (which is IT->end_charpos), stop there. Note: value of
2777 next_overlay_change is point-max if no overlay change
2778 follows. */
2779 charpos = next_overlay_change (IT_CHARPOS (*it));
2780 if (charpos < it->stop_charpos)
2781 it->stop_charpos = charpos;
2782
2783 /* If showing the region, we have to stop at the region
2784 start or end because the face might change there. */
2785 if (it->region_beg_charpos > 0)
2786 {
2787 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2788 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2789 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2790 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2791 }
2792
2793 /* Set up variables for computing the stop position from text
2794 property changes. */
2795 XSETBUFFER (object, current_buffer);
2796 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2797 position = make_number (IT_CHARPOS (*it));
2798
2799 }
2800
2801 /* Get the interval containing IT's position. Value is a null
2802 interval if there isn't such an interval. */
2803 iv = validate_interval_range (object, &position, &position, 0);
2804 if (!NULL_INTERVAL_P (iv))
2805 {
2806 Lisp_Object values_here[LAST_PROP_IDX];
2807 struct props *p;
2808
2809 /* Get properties here. */
2810 for (p = it_props; p->handler; ++p)
2811 values_here[p->idx] = textget (iv->plist, *p->name);
2812
2813 /* Look for an interval following iv that has different
2814 properties. */
2815 for (next_iv = next_interval (iv);
2816 (!NULL_INTERVAL_P (next_iv)
2817 && (NILP (limit)
2818 || XFASTINT (limit) > next_iv->position));
2819 next_iv = next_interval (next_iv))
2820 {
2821 for (p = it_props; p->handler; ++p)
2822 {
2823 Lisp_Object new_value;
2824
2825 new_value = textget (next_iv->plist, *p->name);
2826 if (!EQ (values_here[p->idx], new_value))
2827 break;
2828 }
2829
2830 if (p->handler)
2831 break;
2832 }
2833
2834 if (!NULL_INTERVAL_P (next_iv))
2835 {
2836 if (INTEGERP (limit)
2837 && next_iv->position >= XFASTINT (limit))
2838 /* No text property change up to limit. */
2839 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2840 else
2841 /* Text properties change in next_iv. */
2842 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2843 }
2844 }
2845
2846 xassert (STRINGP (it->string)
2847 || (it->stop_charpos >= BEGV
2848 && it->stop_charpos >= IT_CHARPOS (*it)));
2849 }
2850
2851
2852 /* Return the position of the next overlay change after POS in
2853 current_buffer. Value is point-max if no overlay change
2854 follows. This is like `next-overlay-change' but doesn't use
2855 xmalloc. */
2856
2857 static int
2858 next_overlay_change (pos)
2859 int pos;
2860 {
2861 int noverlays;
2862 int endpos;
2863 Lisp_Object *overlays;
2864 int i;
2865
2866 /* Get all overlays at the given position. */
2867 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2868
2869 /* If any of these overlays ends before endpos,
2870 use its ending point instead. */
2871 for (i = 0; i < noverlays; ++i)
2872 {
2873 Lisp_Object oend;
2874 int oendpos;
2875
2876 oend = OVERLAY_END (overlays[i]);
2877 oendpos = OVERLAY_POSITION (oend);
2878 endpos = min (endpos, oendpos);
2879 }
2880
2881 return endpos;
2882 }
2883
2884
2885 \f
2886 /***********************************************************************
2887 Fontification
2888 ***********************************************************************/
2889
2890 /* Handle changes in the `fontified' property of the current buffer by
2891 calling hook functions from Qfontification_functions to fontify
2892 regions of text. */
2893
2894 static enum prop_handled
2895 handle_fontified_prop (it)
2896 struct it *it;
2897 {
2898 Lisp_Object prop, pos;
2899 enum prop_handled handled = HANDLED_NORMALLY;
2900
2901 /* Get the value of the `fontified' property at IT's current buffer
2902 position. (The `fontified' property doesn't have a special
2903 meaning in strings.) If the value is nil, call functions from
2904 Qfontification_functions. */
2905 if (!STRINGP (it->string)
2906 && it->s == NULL
2907 && !NILP (Vfontification_functions)
2908 && !NILP (Vrun_hooks)
2909 && (pos = make_number (IT_CHARPOS (*it)),
2910 prop = Fget_char_property (pos, Qfontified, Qnil),
2911 NILP (prop)))
2912 {
2913 int count = SPECPDL_INDEX ();
2914 Lisp_Object val;
2915
2916 val = Vfontification_functions;
2917 specbind (Qfontification_functions, Qnil);
2918
2919 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2920 safe_call1 (val, pos);
2921 else
2922 {
2923 Lisp_Object globals, fn;
2924 struct gcpro gcpro1, gcpro2;
2925
2926 globals = Qnil;
2927 GCPRO2 (val, globals);
2928
2929 for (; CONSP (val); val = XCDR (val))
2930 {
2931 fn = XCAR (val);
2932
2933 if (EQ (fn, Qt))
2934 {
2935 /* A value of t indicates this hook has a local
2936 binding; it means to run the global binding too.
2937 In a global value, t should not occur. If it
2938 does, we must ignore it to avoid an endless
2939 loop. */
2940 for (globals = Fdefault_value (Qfontification_functions);
2941 CONSP (globals);
2942 globals = XCDR (globals))
2943 {
2944 fn = XCAR (globals);
2945 if (!EQ (fn, Qt))
2946 safe_call1 (fn, pos);
2947 }
2948 }
2949 else
2950 safe_call1 (fn, pos);
2951 }
2952
2953 UNGCPRO;
2954 }
2955
2956 unbind_to (count, Qnil);
2957
2958 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2959 something. This avoids an endless loop if they failed to
2960 fontify the text for which reason ever. */
2961 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2962 handled = HANDLED_RECOMPUTE_PROPS;
2963 }
2964
2965 return handled;
2966 }
2967
2968
2969 \f
2970 /***********************************************************************
2971 Faces
2972 ***********************************************************************/
2973
2974 /* Set up iterator IT from face properties at its current position.
2975 Called from handle_stop. */
2976
2977 static enum prop_handled
2978 handle_face_prop (it)
2979 struct it *it;
2980 {
2981 int new_face_id, next_stop;
2982
2983 if (!STRINGP (it->string))
2984 {
2985 new_face_id
2986 = face_at_buffer_position (it->w,
2987 IT_CHARPOS (*it),
2988 it->region_beg_charpos,
2989 it->region_end_charpos,
2990 &next_stop,
2991 (IT_CHARPOS (*it)
2992 + TEXT_PROP_DISTANCE_LIMIT),
2993 0);
2994
2995 /* Is this a start of a run of characters with box face?
2996 Caveat: this can be called for a freshly initialized
2997 iterator; face_id is -1 in this case. We know that the new
2998 face will not change until limit, i.e. if the new face has a
2999 box, all characters up to limit will have one. But, as
3000 usual, we don't know whether limit is really the end. */
3001 if (new_face_id != it->face_id)
3002 {
3003 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3004
3005 /* If new face has a box but old face has not, this is
3006 the start of a run of characters with box, i.e. it has
3007 a shadow on the left side. The value of face_id of the
3008 iterator will be -1 if this is the initial call that gets
3009 the face. In this case, we have to look in front of IT's
3010 position and see whether there is a face != new_face_id. */
3011 it->start_of_box_run_p
3012 = (new_face->box != FACE_NO_BOX
3013 && (it->face_id >= 0
3014 || IT_CHARPOS (*it) == BEG
3015 || new_face_id != face_before_it_pos (it)));
3016 it->face_box_p = new_face->box != FACE_NO_BOX;
3017 }
3018 }
3019 else
3020 {
3021 int base_face_id, bufpos;
3022
3023 if (it->current.overlay_string_index >= 0)
3024 bufpos = IT_CHARPOS (*it);
3025 else
3026 bufpos = 0;
3027
3028 /* For strings from a buffer, i.e. overlay strings or strings
3029 from a `display' property, use the face at IT's current
3030 buffer position as the base face to merge with, so that
3031 overlay strings appear in the same face as surrounding
3032 text, unless they specify their own faces. */
3033 base_face_id = underlying_face_id (it);
3034
3035 new_face_id = face_at_string_position (it->w,
3036 it->string,
3037 IT_STRING_CHARPOS (*it),
3038 bufpos,
3039 it->region_beg_charpos,
3040 it->region_end_charpos,
3041 &next_stop,
3042 base_face_id, 0);
3043
3044 #if 0 /* This shouldn't be neccessary. Let's check it. */
3045 /* If IT is used to display a mode line we would really like to
3046 use the mode line face instead of the frame's default face. */
3047 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3048 && new_face_id == DEFAULT_FACE_ID)
3049 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3050 #endif
3051
3052 /* Is this a start of a run of characters with box? Caveat:
3053 this can be called for a freshly allocated iterator; face_id
3054 is -1 is this case. We know that the new face will not
3055 change until the next check pos, i.e. if the new face has a
3056 box, all characters up to that position will have a
3057 box. But, as usual, we don't know whether that position
3058 is really the end. */
3059 if (new_face_id != it->face_id)
3060 {
3061 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3062 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3063
3064 /* If new face has a box but old face hasn't, this is the
3065 start of a run of characters with box, i.e. it has a
3066 shadow on the left side. */
3067 it->start_of_box_run_p
3068 = new_face->box && (old_face == NULL || !old_face->box);
3069 it->face_box_p = new_face->box != FACE_NO_BOX;
3070 }
3071 }
3072
3073 it->face_id = new_face_id;
3074 return HANDLED_NORMALLY;
3075 }
3076
3077
3078 /* Return the ID of the face ``underlying'' IT's current position,
3079 which is in a string. If the iterator is associated with a
3080 buffer, return the face at IT's current buffer position.
3081 Otherwise, use the iterator's base_face_id. */
3082
3083 static int
3084 underlying_face_id (it)
3085 struct it *it;
3086 {
3087 int face_id = it->base_face_id, i;
3088
3089 xassert (STRINGP (it->string));
3090
3091 for (i = it->sp - 1; i >= 0; --i)
3092 if (NILP (it->stack[i].string))
3093 face_id = it->stack[i].face_id;
3094
3095 return face_id;
3096 }
3097
3098
3099 /* Compute the face one character before or after the current position
3100 of IT. BEFORE_P non-zero means get the face in front of IT's
3101 position. Value is the id of the face. */
3102
3103 static int
3104 face_before_or_after_it_pos (it, before_p)
3105 struct it *it;
3106 int before_p;
3107 {
3108 int face_id, limit;
3109 int next_check_charpos;
3110 struct text_pos pos;
3111
3112 xassert (it->s == NULL);
3113
3114 if (STRINGP (it->string))
3115 {
3116 int bufpos, base_face_id;
3117
3118 /* No face change past the end of the string (for the case
3119 we are padding with spaces). No face change before the
3120 string start. */
3121 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3122 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3123 return it->face_id;
3124
3125 /* Set pos to the position before or after IT's current position. */
3126 if (before_p)
3127 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3128 else
3129 /* For composition, we must check the character after the
3130 composition. */
3131 pos = (it->what == IT_COMPOSITION
3132 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3133 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3134
3135 if (it->current.overlay_string_index >= 0)
3136 bufpos = IT_CHARPOS (*it);
3137 else
3138 bufpos = 0;
3139
3140 base_face_id = underlying_face_id (it);
3141
3142 /* Get the face for ASCII, or unibyte. */
3143 face_id = face_at_string_position (it->w,
3144 it->string,
3145 CHARPOS (pos),
3146 bufpos,
3147 it->region_beg_charpos,
3148 it->region_end_charpos,
3149 &next_check_charpos,
3150 base_face_id, 0);
3151
3152 /* Correct the face for charsets different from ASCII. Do it
3153 for the multibyte case only. The face returned above is
3154 suitable for unibyte text if IT->string is unibyte. */
3155 if (STRING_MULTIBYTE (it->string))
3156 {
3157 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3158 int rest = SBYTES (it->string) - BYTEPOS (pos);
3159 int c, len;
3160 struct face *face = FACE_FROM_ID (it->f, face_id);
3161
3162 c = string_char_and_length (p, rest, &len);
3163 face_id = FACE_FOR_CHAR (it->f, face, c);
3164 }
3165 }
3166 else
3167 {
3168 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3169 || (IT_CHARPOS (*it) <= BEGV && before_p))
3170 return it->face_id;
3171
3172 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3173 pos = it->current.pos;
3174
3175 if (before_p)
3176 DEC_TEXT_POS (pos, it->multibyte_p);
3177 else
3178 {
3179 if (it->what == IT_COMPOSITION)
3180 /* For composition, we must check the position after the
3181 composition. */
3182 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3183 else
3184 INC_TEXT_POS (pos, it->multibyte_p);
3185 }
3186
3187 /* Determine face for CHARSET_ASCII, or unibyte. */
3188 face_id = face_at_buffer_position (it->w,
3189 CHARPOS (pos),
3190 it->region_beg_charpos,
3191 it->region_end_charpos,
3192 &next_check_charpos,
3193 limit, 0);
3194
3195 /* Correct the face for charsets different from ASCII. Do it
3196 for the multibyte case only. The face returned above is
3197 suitable for unibyte text if current_buffer is unibyte. */
3198 if (it->multibyte_p)
3199 {
3200 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3201 struct face *face = FACE_FROM_ID (it->f, face_id);
3202 face_id = FACE_FOR_CHAR (it->f, face, c);
3203 }
3204 }
3205
3206 return face_id;
3207 }
3208
3209
3210 \f
3211 /***********************************************************************
3212 Invisible text
3213 ***********************************************************************/
3214
3215 /* Set up iterator IT from invisible properties at its current
3216 position. Called from handle_stop. */
3217
3218 static enum prop_handled
3219 handle_invisible_prop (it)
3220 struct it *it;
3221 {
3222 enum prop_handled handled = HANDLED_NORMALLY;
3223
3224 if (STRINGP (it->string))
3225 {
3226 extern Lisp_Object Qinvisible;
3227 Lisp_Object prop, end_charpos, limit, charpos;
3228
3229 /* Get the value of the invisible text property at the
3230 current position. Value will be nil if there is no such
3231 property. */
3232 charpos = make_number (IT_STRING_CHARPOS (*it));
3233 prop = Fget_text_property (charpos, Qinvisible, it->string);
3234
3235 if (!NILP (prop)
3236 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3237 {
3238 handled = HANDLED_RECOMPUTE_PROPS;
3239
3240 /* Get the position at which the next change of the
3241 invisible text property can be found in IT->string.
3242 Value will be nil if the property value is the same for
3243 all the rest of IT->string. */
3244 XSETINT (limit, SCHARS (it->string));
3245 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3246 it->string, limit);
3247
3248 /* Text at current position is invisible. The next
3249 change in the property is at position end_charpos.
3250 Move IT's current position to that position. */
3251 if (INTEGERP (end_charpos)
3252 && XFASTINT (end_charpos) < XFASTINT (limit))
3253 {
3254 struct text_pos old;
3255 old = it->current.string_pos;
3256 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3257 compute_string_pos (&it->current.string_pos, old, it->string);
3258 }
3259 else
3260 {
3261 /* The rest of the string is invisible. If this is an
3262 overlay string, proceed with the next overlay string
3263 or whatever comes and return a character from there. */
3264 if (it->current.overlay_string_index >= 0)
3265 {
3266 next_overlay_string (it);
3267 /* Don't check for overlay strings when we just
3268 finished processing them. */
3269 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3270 }
3271 else
3272 {
3273 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3274 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3275 }
3276 }
3277 }
3278 }
3279 else
3280 {
3281 int invis_p, newpos, next_stop, start_charpos;
3282 Lisp_Object pos, prop, overlay;
3283
3284 /* First of all, is there invisible text at this position? */
3285 start_charpos = IT_CHARPOS (*it);
3286 pos = make_number (IT_CHARPOS (*it));
3287 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3288 &overlay);
3289 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3290
3291 /* If we are on invisible text, skip over it. */
3292 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3293 {
3294 /* Record whether we have to display an ellipsis for the
3295 invisible text. */
3296 int display_ellipsis_p = invis_p == 2;
3297
3298 handled = HANDLED_RECOMPUTE_PROPS;
3299
3300 /* Loop skipping over invisible text. The loop is left at
3301 ZV or with IT on the first char being visible again. */
3302 do
3303 {
3304 /* Try to skip some invisible text. Return value is the
3305 position reached which can be equal to IT's position
3306 if there is nothing invisible here. This skips both
3307 over invisible text properties and overlays with
3308 invisible property. */
3309 newpos = skip_invisible (IT_CHARPOS (*it),
3310 &next_stop, ZV, it->window);
3311
3312 /* If we skipped nothing at all we weren't at invisible
3313 text in the first place. If everything to the end of
3314 the buffer was skipped, end the loop. */
3315 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3316 invis_p = 0;
3317 else
3318 {
3319 /* We skipped some characters but not necessarily
3320 all there are. Check if we ended up on visible
3321 text. Fget_char_property returns the property of
3322 the char before the given position, i.e. if we
3323 get invis_p = 0, this means that the char at
3324 newpos is visible. */
3325 pos = make_number (newpos);
3326 prop = Fget_char_property (pos, Qinvisible, it->window);
3327 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3328 }
3329
3330 /* If we ended up on invisible text, proceed to
3331 skip starting with next_stop. */
3332 if (invis_p)
3333 IT_CHARPOS (*it) = next_stop;
3334 }
3335 while (invis_p);
3336
3337 /* The position newpos is now either ZV or on visible text. */
3338 IT_CHARPOS (*it) = newpos;
3339 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3340
3341 /* If there are before-strings at the start of invisible
3342 text, and the text is invisible because of a text
3343 property, arrange to show before-strings because 20.x did
3344 it that way. (If the text is invisible because of an
3345 overlay property instead of a text property, this is
3346 already handled in the overlay code.) */
3347 if (NILP (overlay)
3348 && get_overlay_strings (it, start_charpos))
3349 {
3350 handled = HANDLED_RECOMPUTE_PROPS;
3351 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3352 }
3353 else if (display_ellipsis_p)
3354 setup_for_ellipsis (it, 0);
3355 }
3356 }
3357
3358 return handled;
3359 }
3360
3361
3362 /* Make iterator IT return `...' next.
3363 Replaces LEN characters from buffer. */
3364
3365 static void
3366 setup_for_ellipsis (it, len)
3367 struct it *it;
3368 int len;
3369 {
3370 /* Use the display table definition for `...'. Invalid glyphs
3371 will be handled by the method returning elements from dpvec. */
3372 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3373 {
3374 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3375 it->dpvec = v->contents;
3376 it->dpend = v->contents + v->size;
3377 }
3378 else
3379 {
3380 /* Default `...'. */
3381 it->dpvec = default_invis_vector;
3382 it->dpend = default_invis_vector + 3;
3383 }
3384
3385 it->dpvec_char_len = len;
3386 it->current.dpvec_index = 0;
3387 it->dpvec_face_id = -1;
3388
3389 /* Remember the current face id in case glyphs specify faces.
3390 IT's face is restored in set_iterator_to_next.
3391 saved_face_id was set to preceding char's face in handle_stop. */
3392 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3393 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3394
3395 it->method = GET_FROM_DISPLAY_VECTOR;
3396 it->ellipsis_p = 1;
3397 }
3398
3399
3400 \f
3401 /***********************************************************************
3402 'display' property
3403 ***********************************************************************/
3404
3405 /* Set up iterator IT from `display' property at its current position.
3406 Called from handle_stop.
3407 We return HANDLED_RETURN if some part of the display property
3408 overrides the display of the buffer text itself.
3409 Otherwise we return HANDLED_NORMALLY. */
3410
3411 static enum prop_handled
3412 handle_display_prop (it)
3413 struct it *it;
3414 {
3415 Lisp_Object prop, object;
3416 struct text_pos *position;
3417 /* Nonzero if some property replaces the display of the text itself. */
3418 int display_replaced_p = 0;
3419
3420 if (STRINGP (it->string))
3421 {
3422 object = it->string;
3423 position = &it->current.string_pos;
3424 }
3425 else
3426 {
3427 object = it->w->buffer;
3428 position = &it->current.pos;
3429 }
3430
3431 /* Reset those iterator values set from display property values. */
3432 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3433 it->space_width = Qnil;
3434 it->font_height = Qnil;
3435 it->voffset = 0;
3436
3437 /* We don't support recursive `display' properties, i.e. string
3438 values that have a string `display' property, that have a string
3439 `display' property etc. */
3440 if (!it->string_from_display_prop_p)
3441 it->area = TEXT_AREA;
3442
3443 prop = Fget_char_property (make_number (position->charpos),
3444 Qdisplay, object);
3445 if (NILP (prop))
3446 return HANDLED_NORMALLY;
3447
3448 if (CONSP (prop)
3449 /* Simple properties. */
3450 && !EQ (XCAR (prop), Qimage)
3451 && !EQ (XCAR (prop), Qspace)
3452 && !EQ (XCAR (prop), Qwhen)
3453 && !EQ (XCAR (prop), Qslice)
3454 && !EQ (XCAR (prop), Qspace_width)
3455 && !EQ (XCAR (prop), Qheight)
3456 && !EQ (XCAR (prop), Qraise)
3457 /* Marginal area specifications. */
3458 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3459 && !EQ (XCAR (prop), Qleft_fringe)
3460 && !EQ (XCAR (prop), Qright_fringe)
3461 && !NILP (XCAR (prop)))
3462 {
3463 for (; CONSP (prop); prop = XCDR (prop))
3464 {
3465 if (handle_single_display_spec (it, XCAR (prop), object,
3466 position, display_replaced_p))
3467 display_replaced_p = 1;
3468 }
3469 }
3470 else if (VECTORP (prop))
3471 {
3472 int i;
3473 for (i = 0; i < ASIZE (prop); ++i)
3474 if (handle_single_display_spec (it, AREF (prop, i), object,
3475 position, display_replaced_p))
3476 display_replaced_p = 1;
3477 }
3478 else
3479 {
3480 if (handle_single_display_spec (it, prop, object, position, 0))
3481 display_replaced_p = 1;
3482 }
3483
3484 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3485 }
3486
3487
3488 /* Value is the position of the end of the `display' property starting
3489 at START_POS in OBJECT. */
3490
3491 static struct text_pos
3492 display_prop_end (it, object, start_pos)
3493 struct it *it;
3494 Lisp_Object object;
3495 struct text_pos start_pos;
3496 {
3497 Lisp_Object end;
3498 struct text_pos end_pos;
3499
3500 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3501 Qdisplay, object, Qnil);
3502 CHARPOS (end_pos) = XFASTINT (end);
3503 if (STRINGP (object))
3504 compute_string_pos (&end_pos, start_pos, it->string);
3505 else
3506 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3507
3508 return end_pos;
3509 }
3510
3511
3512 /* Set up IT from a single `display' specification PROP. OBJECT
3513 is the object in which the `display' property was found. *POSITION
3514 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3515 means that we previously saw a display specification which already
3516 replaced text display with something else, for example an image;
3517 we ignore such properties after the first one has been processed.
3518
3519 If PROP is a `space' or `image' specification, and in some other
3520 cases too, set *POSITION to the position where the `display'
3521 property ends.
3522
3523 Value is non-zero if something was found which replaces the display
3524 of buffer or string text. */
3525
3526 static int
3527 handle_single_display_spec (it, spec, object, position,
3528 display_replaced_before_p)
3529 struct it *it;
3530 Lisp_Object spec;
3531 Lisp_Object object;
3532 struct text_pos *position;
3533 int display_replaced_before_p;
3534 {
3535 Lisp_Object form;
3536 Lisp_Object location, value;
3537 struct text_pos start_pos;
3538 int valid_p;
3539
3540 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3541 If the result is non-nil, use VALUE instead of SPEC. */
3542 form = Qt;
3543 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3544 {
3545 spec = XCDR (spec);
3546 if (!CONSP (spec))
3547 return 0;
3548 form = XCAR (spec);
3549 spec = XCDR (spec);
3550 }
3551
3552 if (!NILP (form) && !EQ (form, Qt))
3553 {
3554 int count = SPECPDL_INDEX ();
3555 struct gcpro gcpro1;
3556
3557 /* Bind `object' to the object having the `display' property, a
3558 buffer or string. Bind `position' to the position in the
3559 object where the property was found, and `buffer-position'
3560 to the current position in the buffer. */
3561 specbind (Qobject, object);
3562 specbind (Qposition, make_number (CHARPOS (*position)));
3563 specbind (Qbuffer_position,
3564 make_number (STRINGP (object)
3565 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3566 GCPRO1 (form);
3567 form = safe_eval (form);
3568 UNGCPRO;
3569 unbind_to (count, Qnil);
3570 }
3571
3572 if (NILP (form))
3573 return 0;
3574
3575 /* Handle `(height HEIGHT)' specifications. */
3576 if (CONSP (spec)
3577 && EQ (XCAR (spec), Qheight)
3578 && CONSP (XCDR (spec)))
3579 {
3580 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3581 return 0;
3582
3583 it->font_height = XCAR (XCDR (spec));
3584 if (!NILP (it->font_height))
3585 {
3586 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3587 int new_height = -1;
3588
3589 if (CONSP (it->font_height)
3590 && (EQ (XCAR (it->font_height), Qplus)
3591 || EQ (XCAR (it->font_height), Qminus))
3592 && CONSP (XCDR (it->font_height))
3593 && INTEGERP (XCAR (XCDR (it->font_height))))
3594 {
3595 /* `(+ N)' or `(- N)' where N is an integer. */
3596 int steps = XINT (XCAR (XCDR (it->font_height)));
3597 if (EQ (XCAR (it->font_height), Qplus))
3598 steps = - steps;
3599 it->face_id = smaller_face (it->f, it->face_id, steps);
3600 }
3601 else if (FUNCTIONP (it->font_height))
3602 {
3603 /* Call function with current height as argument.
3604 Value is the new height. */
3605 Lisp_Object height;
3606 height = safe_call1 (it->font_height,
3607 face->lface[LFACE_HEIGHT_INDEX]);
3608 if (NUMBERP (height))
3609 new_height = XFLOATINT (height);
3610 }
3611 else if (NUMBERP (it->font_height))
3612 {
3613 /* Value is a multiple of the canonical char height. */
3614 struct face *face;
3615
3616 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3617 new_height = (XFLOATINT (it->font_height)
3618 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3619 }
3620 else
3621 {
3622 /* Evaluate IT->font_height with `height' bound to the
3623 current specified height to get the new height. */
3624 int count = SPECPDL_INDEX ();
3625
3626 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3627 value = safe_eval (it->font_height);
3628 unbind_to (count, Qnil);
3629
3630 if (NUMBERP (value))
3631 new_height = XFLOATINT (value);
3632 }
3633
3634 if (new_height > 0)
3635 it->face_id = face_with_height (it->f, it->face_id, new_height);
3636 }
3637
3638 return 0;
3639 }
3640
3641 /* Handle `(space_width WIDTH)'. */
3642 if (CONSP (spec)
3643 && EQ (XCAR (spec), Qspace_width)
3644 && CONSP (XCDR (spec)))
3645 {
3646 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3647 return 0;
3648
3649 value = XCAR (XCDR (spec));
3650 if (NUMBERP (value) && XFLOATINT (value) > 0)
3651 it->space_width = value;
3652
3653 return 0;
3654 }
3655
3656 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3657 if (CONSP (spec)
3658 && EQ (XCAR (spec), Qslice))
3659 {
3660 Lisp_Object tem;
3661
3662 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3663 return 0;
3664
3665 if (tem = XCDR (spec), CONSP (tem))
3666 {
3667 it->slice.x = XCAR (tem);
3668 if (tem = XCDR (tem), CONSP (tem))
3669 {
3670 it->slice.y = XCAR (tem);
3671 if (tem = XCDR (tem), CONSP (tem))
3672 {
3673 it->slice.width = XCAR (tem);
3674 if (tem = XCDR (tem), CONSP (tem))
3675 it->slice.height = XCAR (tem);
3676 }
3677 }
3678 }
3679
3680 return 0;
3681 }
3682
3683 /* Handle `(raise FACTOR)'. */
3684 if (CONSP (spec)
3685 && EQ (XCAR (spec), Qraise)
3686 && CONSP (XCDR (spec)))
3687 {
3688 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3689 return 0;
3690
3691 #ifdef HAVE_WINDOW_SYSTEM
3692 value = XCAR (XCDR (spec));
3693 if (NUMBERP (value))
3694 {
3695 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3696 it->voffset = - (XFLOATINT (value)
3697 * (FONT_HEIGHT (face->font)));
3698 }
3699 #endif /* HAVE_WINDOW_SYSTEM */
3700
3701 return 0;
3702 }
3703
3704 /* Don't handle the other kinds of display specifications
3705 inside a string that we got from a `display' property. */
3706 if (it->string_from_display_prop_p)
3707 return 0;
3708
3709 /* Characters having this form of property are not displayed, so
3710 we have to find the end of the property. */
3711 start_pos = *position;
3712 *position = display_prop_end (it, object, start_pos);
3713 value = Qnil;
3714
3715 /* Stop the scan at that end position--we assume that all
3716 text properties change there. */
3717 it->stop_charpos = position->charpos;
3718
3719 /* Handle `(left-fringe BITMAP [FACE])'
3720 and `(right-fringe BITMAP [FACE])'. */
3721 if (CONSP (spec)
3722 && (EQ (XCAR (spec), Qleft_fringe)
3723 || EQ (XCAR (spec), Qright_fringe))
3724 && CONSP (XCDR (spec)))
3725 {
3726 int face_id = DEFAULT_FACE_ID;
3727 int fringe_bitmap;
3728
3729 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3730 /* If we return here, POSITION has been advanced
3731 across the text with this property. */
3732 return 0;
3733
3734 #ifdef HAVE_WINDOW_SYSTEM
3735 value = XCAR (XCDR (spec));
3736 if (!SYMBOLP (value)
3737 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3738 /* If we return here, POSITION has been advanced
3739 across the text with this property. */
3740 return 0;
3741
3742 if (CONSP (XCDR (XCDR (spec))))
3743 {
3744 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3745 int face_id2 = lookup_derived_face (it->f, face_name,
3746 'A', FRINGE_FACE_ID, 0);
3747 if (face_id2 >= 0)
3748 face_id = face_id2;
3749 }
3750
3751 /* Save current settings of IT so that we can restore them
3752 when we are finished with the glyph property value. */
3753
3754 push_it (it);
3755
3756 it->area = TEXT_AREA;
3757 it->what = IT_IMAGE;
3758 it->image_id = -1; /* no image */
3759 it->position = start_pos;
3760 it->object = NILP (object) ? it->w->buffer : object;
3761 it->method = GET_FROM_IMAGE;
3762 it->face_id = face_id;
3763
3764 /* Say that we haven't consumed the characters with
3765 `display' property yet. The call to pop_it in
3766 set_iterator_to_next will clean this up. */
3767 *position = start_pos;
3768
3769 if (EQ (XCAR (spec), Qleft_fringe))
3770 {
3771 it->left_user_fringe_bitmap = fringe_bitmap;
3772 it->left_user_fringe_face_id = face_id;
3773 }
3774 else
3775 {
3776 it->right_user_fringe_bitmap = fringe_bitmap;
3777 it->right_user_fringe_face_id = face_id;
3778 }
3779 #endif /* HAVE_WINDOW_SYSTEM */
3780 return 1;
3781 }
3782
3783 /* Prepare to handle `((margin left-margin) ...)',
3784 `((margin right-margin) ...)' and `((margin nil) ...)'
3785 prefixes for display specifications. */
3786 location = Qunbound;
3787 if (CONSP (spec) && CONSP (XCAR (spec)))
3788 {
3789 Lisp_Object tem;
3790
3791 value = XCDR (spec);
3792 if (CONSP (value))
3793 value = XCAR (value);
3794
3795 tem = XCAR (spec);
3796 if (EQ (XCAR (tem), Qmargin)
3797 && (tem = XCDR (tem),
3798 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3799 (NILP (tem)
3800 || EQ (tem, Qleft_margin)
3801 || EQ (tem, Qright_margin))))
3802 location = tem;
3803 }
3804
3805 if (EQ (location, Qunbound))
3806 {
3807 location = Qnil;
3808 value = spec;
3809 }
3810
3811 /* After this point, VALUE is the property after any
3812 margin prefix has been stripped. It must be a string,
3813 an image specification, or `(space ...)'.
3814
3815 LOCATION specifies where to display: `left-margin',
3816 `right-margin' or nil. */
3817
3818 valid_p = (STRINGP (value)
3819 #ifdef HAVE_WINDOW_SYSTEM
3820 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3821 #endif /* not HAVE_WINDOW_SYSTEM */
3822 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3823
3824 if (valid_p && !display_replaced_before_p)
3825 {
3826 /* Save current settings of IT so that we can restore them
3827 when we are finished with the glyph property value. */
3828 push_it (it);
3829
3830 if (NILP (location))
3831 it->area = TEXT_AREA;
3832 else if (EQ (location, Qleft_margin))
3833 it->area = LEFT_MARGIN_AREA;
3834 else
3835 it->area = RIGHT_MARGIN_AREA;
3836
3837 if (STRINGP (value))
3838 {
3839 it->string = value;
3840 it->multibyte_p = STRING_MULTIBYTE (it->string);
3841 it->current.overlay_string_index = -1;
3842 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3843 it->end_charpos = it->string_nchars = SCHARS (it->string);
3844 it->method = GET_FROM_STRING;
3845 it->stop_charpos = 0;
3846 it->string_from_display_prop_p = 1;
3847 /* Say that we haven't consumed the characters with
3848 `display' property yet. The call to pop_it in
3849 set_iterator_to_next will clean this up. */
3850 *position = start_pos;
3851 }
3852 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3853 {
3854 it->method = GET_FROM_STRETCH;
3855 it->object = value;
3856 it->current.pos = it->position = start_pos;
3857 }
3858 #ifdef HAVE_WINDOW_SYSTEM
3859 else
3860 {
3861 it->what = IT_IMAGE;
3862 it->image_id = lookup_image (it->f, value);
3863 it->position = start_pos;
3864 it->object = NILP (object) ? it->w->buffer : object;
3865 it->method = GET_FROM_IMAGE;
3866
3867 /* Say that we haven't consumed the characters with
3868 `display' property yet. The call to pop_it in
3869 set_iterator_to_next will clean this up. */
3870 *position = start_pos;
3871 }
3872 #endif /* HAVE_WINDOW_SYSTEM */
3873
3874 return 1;
3875 }
3876
3877 /* Invalid property or property not supported. Restore
3878 POSITION to what it was before. */
3879 *position = start_pos;
3880 return 0;
3881 }
3882
3883
3884 /* Check if SPEC is a display specification value whose text should be
3885 treated as intangible. */
3886
3887 static int
3888 single_display_spec_intangible_p (prop)
3889 Lisp_Object prop;
3890 {
3891 /* Skip over `when FORM'. */
3892 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3893 {
3894 prop = XCDR (prop);
3895 if (!CONSP (prop))
3896 return 0;
3897 prop = XCDR (prop);
3898 }
3899
3900 if (STRINGP (prop))
3901 return 1;
3902
3903 if (!CONSP (prop))
3904 return 0;
3905
3906 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3907 we don't need to treat text as intangible. */
3908 if (EQ (XCAR (prop), Qmargin))
3909 {
3910 prop = XCDR (prop);
3911 if (!CONSP (prop))
3912 return 0;
3913
3914 prop = XCDR (prop);
3915 if (!CONSP (prop)
3916 || EQ (XCAR (prop), Qleft_margin)
3917 || EQ (XCAR (prop), Qright_margin))
3918 return 0;
3919 }
3920
3921 return (CONSP (prop)
3922 && (EQ (XCAR (prop), Qimage)
3923 || EQ (XCAR (prop), Qspace)));
3924 }
3925
3926
3927 /* Check if PROP is a display property value whose text should be
3928 treated as intangible. */
3929
3930 int
3931 display_prop_intangible_p (prop)
3932 Lisp_Object prop;
3933 {
3934 if (CONSP (prop)
3935 && CONSP (XCAR (prop))
3936 && !EQ (Qmargin, XCAR (XCAR (prop))))
3937 {
3938 /* A list of sub-properties. */
3939 while (CONSP (prop))
3940 {
3941 if (single_display_spec_intangible_p (XCAR (prop)))
3942 return 1;
3943 prop = XCDR (prop);
3944 }
3945 }
3946 else if (VECTORP (prop))
3947 {
3948 /* A vector of sub-properties. */
3949 int i;
3950 for (i = 0; i < ASIZE (prop); ++i)
3951 if (single_display_spec_intangible_p (AREF (prop, i)))
3952 return 1;
3953 }
3954 else
3955 return single_display_spec_intangible_p (prop);
3956
3957 return 0;
3958 }
3959
3960
3961 /* Return 1 if PROP is a display sub-property value containing STRING. */
3962
3963 static int
3964 single_display_spec_string_p (prop, string)
3965 Lisp_Object prop, string;
3966 {
3967 if (EQ (string, prop))
3968 return 1;
3969
3970 /* Skip over `when FORM'. */
3971 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3972 {
3973 prop = XCDR (prop);
3974 if (!CONSP (prop))
3975 return 0;
3976 prop = XCDR (prop);
3977 }
3978
3979 if (CONSP (prop))
3980 /* Skip over `margin LOCATION'. */
3981 if (EQ (XCAR (prop), Qmargin))
3982 {
3983 prop = XCDR (prop);
3984 if (!CONSP (prop))
3985 return 0;
3986
3987 prop = XCDR (prop);
3988 if (!CONSP (prop))
3989 return 0;
3990 }
3991
3992 return CONSP (prop) && EQ (XCAR (prop), string);
3993 }
3994
3995
3996 /* Return 1 if STRING appears in the `display' property PROP. */
3997
3998 static int
3999 display_prop_string_p (prop, string)
4000 Lisp_Object prop, string;
4001 {
4002 if (CONSP (prop)
4003 && CONSP (XCAR (prop))
4004 && !EQ (Qmargin, XCAR (XCAR (prop))))
4005 {
4006 /* A list of sub-properties. */
4007 while (CONSP (prop))
4008 {
4009 if (single_display_spec_string_p (XCAR (prop), string))
4010 return 1;
4011 prop = XCDR (prop);
4012 }
4013 }
4014 else if (VECTORP (prop))
4015 {
4016 /* A vector of sub-properties. */
4017 int i;
4018 for (i = 0; i < ASIZE (prop); ++i)
4019 if (single_display_spec_string_p (AREF (prop, i), string))
4020 return 1;
4021 }
4022 else
4023 return single_display_spec_string_p (prop, string);
4024
4025 return 0;
4026 }
4027
4028
4029 /* Determine from which buffer position in W's buffer STRING comes
4030 from. AROUND_CHARPOS is an approximate position where it could
4031 be from. Value is the buffer position or 0 if it couldn't be
4032 determined.
4033
4034 W's buffer must be current.
4035
4036 This function is necessary because we don't record buffer positions
4037 in glyphs generated from strings (to keep struct glyph small).
4038 This function may only use code that doesn't eval because it is
4039 called asynchronously from note_mouse_highlight. */
4040
4041 int
4042 string_buffer_position (w, string, around_charpos)
4043 struct window *w;
4044 Lisp_Object string;
4045 int around_charpos;
4046 {
4047 Lisp_Object limit, prop, pos;
4048 const int MAX_DISTANCE = 1000;
4049 int found = 0;
4050
4051 pos = make_number (around_charpos);
4052 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4053 while (!found && !EQ (pos, limit))
4054 {
4055 prop = Fget_char_property (pos, Qdisplay, Qnil);
4056 if (!NILP (prop) && display_prop_string_p (prop, string))
4057 found = 1;
4058 else
4059 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4060 }
4061
4062 if (!found)
4063 {
4064 pos = make_number (around_charpos);
4065 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4066 while (!found && !EQ (pos, limit))
4067 {
4068 prop = Fget_char_property (pos, Qdisplay, Qnil);
4069 if (!NILP (prop) && display_prop_string_p (prop, string))
4070 found = 1;
4071 else
4072 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4073 limit);
4074 }
4075 }
4076
4077 return found ? XINT (pos) : 0;
4078 }
4079
4080
4081 \f
4082 /***********************************************************************
4083 `composition' property
4084 ***********************************************************************/
4085
4086 /* Set up iterator IT from `composition' property at its current
4087 position. Called from handle_stop. */
4088
4089 static enum prop_handled
4090 handle_composition_prop (it)
4091 struct it *it;
4092 {
4093 Lisp_Object prop, string;
4094 int pos, pos_byte, end;
4095 enum prop_handled handled = HANDLED_NORMALLY;
4096
4097 if (STRINGP (it->string))
4098 {
4099 pos = IT_STRING_CHARPOS (*it);
4100 pos_byte = IT_STRING_BYTEPOS (*it);
4101 string = it->string;
4102 }
4103 else
4104 {
4105 pos = IT_CHARPOS (*it);
4106 pos_byte = IT_BYTEPOS (*it);
4107 string = Qnil;
4108 }
4109
4110 /* If there's a valid composition and point is not inside of the
4111 composition (in the case that the composition is from the current
4112 buffer), draw a glyph composed from the composition components. */
4113 if (find_composition (pos, -1, &pos, &end, &prop, string)
4114 && COMPOSITION_VALID_P (pos, end, prop)
4115 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4116 {
4117 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4118
4119 if (id >= 0)
4120 {
4121 it->method = GET_FROM_COMPOSITION;
4122 it->cmp_id = id;
4123 it->cmp_len = COMPOSITION_LENGTH (prop);
4124 /* For a terminal, draw only the first character of the
4125 components. */
4126 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4127 it->len = (STRINGP (it->string)
4128 ? string_char_to_byte (it->string, end)
4129 : CHAR_TO_BYTE (end)) - pos_byte;
4130 it->stop_charpos = end;
4131 handled = HANDLED_RETURN;
4132 }
4133 }
4134
4135 return handled;
4136 }
4137
4138
4139 \f
4140 /***********************************************************************
4141 Overlay strings
4142 ***********************************************************************/
4143
4144 /* The following structure is used to record overlay strings for
4145 later sorting in load_overlay_strings. */
4146
4147 struct overlay_entry
4148 {
4149 Lisp_Object overlay;
4150 Lisp_Object string;
4151 int priority;
4152 int after_string_p;
4153 };
4154
4155
4156 /* Set up iterator IT from overlay strings at its current position.
4157 Called from handle_stop. */
4158
4159 static enum prop_handled
4160 handle_overlay_change (it)
4161 struct it *it;
4162 {
4163 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4164 return HANDLED_RECOMPUTE_PROPS;
4165 else
4166 return HANDLED_NORMALLY;
4167 }
4168
4169
4170 /* Set up the next overlay string for delivery by IT, if there is an
4171 overlay string to deliver. Called by set_iterator_to_next when the
4172 end of the current overlay string is reached. If there are more
4173 overlay strings to display, IT->string and
4174 IT->current.overlay_string_index are set appropriately here.
4175 Otherwise IT->string is set to nil. */
4176
4177 static void
4178 next_overlay_string (it)
4179 struct it *it;
4180 {
4181 ++it->current.overlay_string_index;
4182 if (it->current.overlay_string_index == it->n_overlay_strings)
4183 {
4184 /* No more overlay strings. Restore IT's settings to what
4185 they were before overlay strings were processed, and
4186 continue to deliver from current_buffer. */
4187 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4188
4189 pop_it (it);
4190 xassert (it->stop_charpos >= BEGV
4191 && it->stop_charpos <= it->end_charpos);
4192 it->string = Qnil;
4193 it->current.overlay_string_index = -1;
4194 SET_TEXT_POS (it->current.string_pos, -1, -1);
4195 it->n_overlay_strings = 0;
4196 it->method = GET_FROM_BUFFER;
4197
4198 /* If we're at the end of the buffer, record that we have
4199 processed the overlay strings there already, so that
4200 next_element_from_buffer doesn't try it again. */
4201 if (IT_CHARPOS (*it) >= it->end_charpos)
4202 it->overlay_strings_at_end_processed_p = 1;
4203
4204 /* If we have to display `...' for invisible text, set
4205 the iterator up for that. */
4206 if (display_ellipsis_p)
4207 setup_for_ellipsis (it, 0);
4208 }
4209 else
4210 {
4211 /* There are more overlay strings to process. If
4212 IT->current.overlay_string_index has advanced to a position
4213 where we must load IT->overlay_strings with more strings, do
4214 it. */
4215 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4216
4217 if (it->current.overlay_string_index && i == 0)
4218 load_overlay_strings (it, 0);
4219
4220 /* Initialize IT to deliver display elements from the overlay
4221 string. */
4222 it->string = it->overlay_strings[i];
4223 it->multibyte_p = STRING_MULTIBYTE (it->string);
4224 SET_TEXT_POS (it->current.string_pos, 0, 0);
4225 it->method = GET_FROM_STRING;
4226 it->stop_charpos = 0;
4227 }
4228
4229 CHECK_IT (it);
4230 }
4231
4232
4233 /* Compare two overlay_entry structures E1 and E2. Used as a
4234 comparison function for qsort in load_overlay_strings. Overlay
4235 strings for the same position are sorted so that
4236
4237 1. All after-strings come in front of before-strings, except
4238 when they come from the same overlay.
4239
4240 2. Within after-strings, strings are sorted so that overlay strings
4241 from overlays with higher priorities come first.
4242
4243 2. Within before-strings, strings are sorted so that overlay
4244 strings from overlays with higher priorities come last.
4245
4246 Value is analogous to strcmp. */
4247
4248
4249 static int
4250 compare_overlay_entries (e1, e2)
4251 void *e1, *e2;
4252 {
4253 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4254 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4255 int result;
4256
4257 if (entry1->after_string_p != entry2->after_string_p)
4258 {
4259 /* Let after-strings appear in front of before-strings if
4260 they come from different overlays. */
4261 if (EQ (entry1->overlay, entry2->overlay))
4262 result = entry1->after_string_p ? 1 : -1;
4263 else
4264 result = entry1->after_string_p ? -1 : 1;
4265 }
4266 else if (entry1->after_string_p)
4267 /* After-strings sorted in order of decreasing priority. */
4268 result = entry2->priority - entry1->priority;
4269 else
4270 /* Before-strings sorted in order of increasing priority. */
4271 result = entry1->priority - entry2->priority;
4272
4273 return result;
4274 }
4275
4276
4277 /* Load the vector IT->overlay_strings with overlay strings from IT's
4278 current buffer position, or from CHARPOS if that is > 0. Set
4279 IT->n_overlays to the total number of overlay strings found.
4280
4281 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4282 a time. On entry into load_overlay_strings,
4283 IT->current.overlay_string_index gives the number of overlay
4284 strings that have already been loaded by previous calls to this
4285 function.
4286
4287 IT->add_overlay_start contains an additional overlay start
4288 position to consider for taking overlay strings from, if non-zero.
4289 This position comes into play when the overlay has an `invisible'
4290 property, and both before and after-strings. When we've skipped to
4291 the end of the overlay, because of its `invisible' property, we
4292 nevertheless want its before-string to appear.
4293 IT->add_overlay_start will contain the overlay start position
4294 in this case.
4295
4296 Overlay strings are sorted so that after-string strings come in
4297 front of before-string strings. Within before and after-strings,
4298 strings are sorted by overlay priority. See also function
4299 compare_overlay_entries. */
4300
4301 static void
4302 load_overlay_strings (it, charpos)
4303 struct it *it;
4304 int charpos;
4305 {
4306 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4307 Lisp_Object overlay, window, str, invisible;
4308 struct Lisp_Overlay *ov;
4309 int start, end;
4310 int size = 20;
4311 int n = 0, i, j, invis_p;
4312 struct overlay_entry *entries
4313 = (struct overlay_entry *) alloca (size * sizeof *entries);
4314
4315 if (charpos <= 0)
4316 charpos = IT_CHARPOS (*it);
4317
4318 /* Append the overlay string STRING of overlay OVERLAY to vector
4319 `entries' which has size `size' and currently contains `n'
4320 elements. AFTER_P non-zero means STRING is an after-string of
4321 OVERLAY. */
4322 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4323 do \
4324 { \
4325 Lisp_Object priority; \
4326 \
4327 if (n == size) \
4328 { \
4329 int new_size = 2 * size; \
4330 struct overlay_entry *old = entries; \
4331 entries = \
4332 (struct overlay_entry *) alloca (new_size \
4333 * sizeof *entries); \
4334 bcopy (old, entries, size * sizeof *entries); \
4335 size = new_size; \
4336 } \
4337 \
4338 entries[n].string = (STRING); \
4339 entries[n].overlay = (OVERLAY); \
4340 priority = Foverlay_get ((OVERLAY), Qpriority); \
4341 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4342 entries[n].after_string_p = (AFTER_P); \
4343 ++n; \
4344 } \
4345 while (0)
4346
4347 /* Process overlay before the overlay center. */
4348 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4349 {
4350 XSETMISC (overlay, ov);
4351 xassert (OVERLAYP (overlay));
4352 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4353 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4354
4355 if (end < charpos)
4356 break;
4357
4358 /* Skip this overlay if it doesn't start or end at IT's current
4359 position. */
4360 if (end != charpos && start != charpos)
4361 continue;
4362
4363 /* Skip this overlay if it doesn't apply to IT->w. */
4364 window = Foverlay_get (overlay, Qwindow);
4365 if (WINDOWP (window) && XWINDOW (window) != it->w)
4366 continue;
4367
4368 /* If the text ``under'' the overlay is invisible, both before-
4369 and after-strings from this overlay are visible; start and
4370 end position are indistinguishable. */
4371 invisible = Foverlay_get (overlay, Qinvisible);
4372 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4373
4374 /* If overlay has a non-empty before-string, record it. */
4375 if ((start == charpos || (end == charpos && invis_p))
4376 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4377 && SCHARS (str))
4378 RECORD_OVERLAY_STRING (overlay, str, 0);
4379
4380 /* If overlay has a non-empty after-string, record it. */
4381 if ((end == charpos || (start == charpos && invis_p))
4382 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4383 && SCHARS (str))
4384 RECORD_OVERLAY_STRING (overlay, str, 1);
4385 }
4386
4387 /* Process overlays after the overlay center. */
4388 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4389 {
4390 XSETMISC (overlay, ov);
4391 xassert (OVERLAYP (overlay));
4392 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4393 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4394
4395 if (start > charpos)
4396 break;
4397
4398 /* Skip this overlay if it doesn't start or end at IT's current
4399 position. */
4400 if (end != charpos && start != charpos)
4401 continue;
4402
4403 /* Skip this overlay if it doesn't apply to IT->w. */
4404 window = Foverlay_get (overlay, Qwindow);
4405 if (WINDOWP (window) && XWINDOW (window) != it->w)
4406 continue;
4407
4408 /* If the text ``under'' the overlay is invisible, it has a zero
4409 dimension, and both before- and after-strings apply. */
4410 invisible = Foverlay_get (overlay, Qinvisible);
4411 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4412
4413 /* If overlay has a non-empty before-string, record it. */
4414 if ((start == charpos || (end == charpos && invis_p))
4415 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4416 && SCHARS (str))
4417 RECORD_OVERLAY_STRING (overlay, str, 0);
4418
4419 /* If overlay has a non-empty after-string, record it. */
4420 if ((end == charpos || (start == charpos && invis_p))
4421 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4422 && SCHARS (str))
4423 RECORD_OVERLAY_STRING (overlay, str, 1);
4424 }
4425
4426 #undef RECORD_OVERLAY_STRING
4427
4428 /* Sort entries. */
4429 if (n > 1)
4430 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4431
4432 /* Record the total number of strings to process. */
4433 it->n_overlay_strings = n;
4434
4435 /* IT->current.overlay_string_index is the number of overlay strings
4436 that have already been consumed by IT. Copy some of the
4437 remaining overlay strings to IT->overlay_strings. */
4438 i = 0;
4439 j = it->current.overlay_string_index;
4440 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4441 it->overlay_strings[i++] = entries[j++].string;
4442
4443 CHECK_IT (it);
4444 }
4445
4446
4447 /* Get the first chunk of overlay strings at IT's current buffer
4448 position, or at CHARPOS if that is > 0. Value is non-zero if at
4449 least one overlay string was found. */
4450
4451 static int
4452 get_overlay_strings (it, charpos)
4453 struct it *it;
4454 int charpos;
4455 {
4456 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4457 process. This fills IT->overlay_strings with strings, and sets
4458 IT->n_overlay_strings to the total number of strings to process.
4459 IT->pos.overlay_string_index has to be set temporarily to zero
4460 because load_overlay_strings needs this; it must be set to -1
4461 when no overlay strings are found because a zero value would
4462 indicate a position in the first overlay string. */
4463 it->current.overlay_string_index = 0;
4464 load_overlay_strings (it, charpos);
4465
4466 /* If we found overlay strings, set up IT to deliver display
4467 elements from the first one. Otherwise set up IT to deliver
4468 from current_buffer. */
4469 if (it->n_overlay_strings)
4470 {
4471 /* Make sure we know settings in current_buffer, so that we can
4472 restore meaningful values when we're done with the overlay
4473 strings. */
4474 compute_stop_pos (it);
4475 xassert (it->face_id >= 0);
4476
4477 /* Save IT's settings. They are restored after all overlay
4478 strings have been processed. */
4479 xassert (it->sp == 0);
4480 push_it (it);
4481
4482 /* Set up IT to deliver display elements from the first overlay
4483 string. */
4484 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4485 it->string = it->overlay_strings[0];
4486 it->stop_charpos = 0;
4487 xassert (STRINGP (it->string));
4488 it->end_charpos = SCHARS (it->string);
4489 it->multibyte_p = STRING_MULTIBYTE (it->string);
4490 it->method = GET_FROM_STRING;
4491 }
4492 else
4493 {
4494 it->string = Qnil;
4495 it->current.overlay_string_index = -1;
4496 it->method = GET_FROM_BUFFER;
4497 }
4498
4499 CHECK_IT (it);
4500
4501 /* Value is non-zero if we found at least one overlay string. */
4502 return STRINGP (it->string);
4503 }
4504
4505
4506 \f
4507 /***********************************************************************
4508 Saving and restoring state
4509 ***********************************************************************/
4510
4511 /* Save current settings of IT on IT->stack. Called, for example,
4512 before setting up IT for an overlay string, to be able to restore
4513 IT's settings to what they were after the overlay string has been
4514 processed. */
4515
4516 static void
4517 push_it (it)
4518 struct it *it;
4519 {
4520 struct iterator_stack_entry *p;
4521
4522 xassert (it->sp < 2);
4523 p = it->stack + it->sp;
4524
4525 p->stop_charpos = it->stop_charpos;
4526 xassert (it->face_id >= 0);
4527 p->face_id = it->face_id;
4528 p->string = it->string;
4529 p->pos = it->current;
4530 p->end_charpos = it->end_charpos;
4531 p->string_nchars = it->string_nchars;
4532 p->area = it->area;
4533 p->multibyte_p = it->multibyte_p;
4534 p->slice = it->slice;
4535 p->space_width = it->space_width;
4536 p->font_height = it->font_height;
4537 p->voffset = it->voffset;
4538 p->string_from_display_prop_p = it->string_from_display_prop_p;
4539 p->display_ellipsis_p = 0;
4540 ++it->sp;
4541 }
4542
4543
4544 /* Restore IT's settings from IT->stack. Called, for example, when no
4545 more overlay strings must be processed, and we return to delivering
4546 display elements from a buffer, or when the end of a string from a
4547 `display' property is reached and we return to delivering display
4548 elements from an overlay string, or from a buffer. */
4549
4550 static void
4551 pop_it (it)
4552 struct it *it;
4553 {
4554 struct iterator_stack_entry *p;
4555
4556 xassert (it->sp > 0);
4557 --it->sp;
4558 p = it->stack + it->sp;
4559 it->stop_charpos = p->stop_charpos;
4560 it->face_id = p->face_id;
4561 it->string = p->string;
4562 it->current = p->pos;
4563 it->end_charpos = p->end_charpos;
4564 it->string_nchars = p->string_nchars;
4565 it->area = p->area;
4566 it->multibyte_p = p->multibyte_p;
4567 it->slice = p->slice;
4568 it->space_width = p->space_width;
4569 it->font_height = p->font_height;
4570 it->voffset = p->voffset;
4571 it->string_from_display_prop_p = p->string_from_display_prop_p;
4572 }
4573
4574
4575 \f
4576 /***********************************************************************
4577 Moving over lines
4578 ***********************************************************************/
4579
4580 /* Set IT's current position to the previous line start. */
4581
4582 static void
4583 back_to_previous_line_start (it)
4584 struct it *it;
4585 {
4586 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4587 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4588 }
4589
4590
4591 /* Move IT to the next line start.
4592
4593 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4594 we skipped over part of the text (as opposed to moving the iterator
4595 continuously over the text). Otherwise, don't change the value
4596 of *SKIPPED_P.
4597
4598 Newlines may come from buffer text, overlay strings, or strings
4599 displayed via the `display' property. That's the reason we can't
4600 simply use find_next_newline_no_quit.
4601
4602 Note that this function may not skip over invisible text that is so
4603 because of text properties and immediately follows a newline. If
4604 it would, function reseat_at_next_visible_line_start, when called
4605 from set_iterator_to_next, would effectively make invisible
4606 characters following a newline part of the wrong glyph row, which
4607 leads to wrong cursor motion. */
4608
4609 static int
4610 forward_to_next_line_start (it, skipped_p)
4611 struct it *it;
4612 int *skipped_p;
4613 {
4614 int old_selective, newline_found_p, n;
4615 const int MAX_NEWLINE_DISTANCE = 500;
4616
4617 /* If already on a newline, just consume it to avoid unintended
4618 skipping over invisible text below. */
4619 if (it->what == IT_CHARACTER
4620 && it->c == '\n'
4621 && CHARPOS (it->position) == IT_CHARPOS (*it))
4622 {
4623 set_iterator_to_next (it, 0);
4624 it->c = 0;
4625 return 1;
4626 }
4627
4628 /* Don't handle selective display in the following. It's (a)
4629 unnecessary because it's done by the caller, and (b) leads to an
4630 infinite recursion because next_element_from_ellipsis indirectly
4631 calls this function. */
4632 old_selective = it->selective;
4633 it->selective = 0;
4634
4635 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4636 from buffer text. */
4637 for (n = newline_found_p = 0;
4638 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4639 n += STRINGP (it->string) ? 0 : 1)
4640 {
4641 if (!get_next_display_element (it))
4642 return 0;
4643 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4644 set_iterator_to_next (it, 0);
4645 }
4646
4647 /* If we didn't find a newline near enough, see if we can use a
4648 short-cut. */
4649 if (!newline_found_p)
4650 {
4651 int start = IT_CHARPOS (*it);
4652 int limit = find_next_newline_no_quit (start, 1);
4653 Lisp_Object pos;
4654
4655 xassert (!STRINGP (it->string));
4656
4657 /* If there isn't any `display' property in sight, and no
4658 overlays, we can just use the position of the newline in
4659 buffer text. */
4660 if (it->stop_charpos >= limit
4661 || ((pos = Fnext_single_property_change (make_number (start),
4662 Qdisplay,
4663 Qnil, make_number (limit)),
4664 NILP (pos))
4665 && next_overlay_change (start) == ZV))
4666 {
4667 IT_CHARPOS (*it) = limit;
4668 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4669 *skipped_p = newline_found_p = 1;
4670 }
4671 else
4672 {
4673 while (get_next_display_element (it)
4674 && !newline_found_p)
4675 {
4676 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4677 set_iterator_to_next (it, 0);
4678 }
4679 }
4680 }
4681
4682 it->selective = old_selective;
4683 return newline_found_p;
4684 }
4685
4686
4687 /* Set IT's current position to the previous visible line start. Skip
4688 invisible text that is so either due to text properties or due to
4689 selective display. Caution: this does not change IT->current_x and
4690 IT->hpos. */
4691
4692 static void
4693 back_to_previous_visible_line_start (it)
4694 struct it *it;
4695 {
4696 while (IT_CHARPOS (*it) > BEGV)
4697 {
4698 back_to_previous_line_start (it);
4699 if (IT_CHARPOS (*it) <= BEGV)
4700 break;
4701
4702 /* If selective > 0, then lines indented more than that values
4703 are invisible. */
4704 if (it->selective > 0
4705 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4706 (double) it->selective)) /* iftc */
4707 continue;
4708
4709 /* Check the newline before point for invisibility. */
4710 {
4711 Lisp_Object prop;
4712 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4713 Qinvisible, it->window);
4714 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4715 continue;
4716 }
4717
4718 /* If newline has a display property that replaces the newline with something
4719 else (image or text), find start of overlay or interval and continue search
4720 from that point. */
4721 if (IT_CHARPOS (*it) > BEGV)
4722 {
4723 struct it it2 = *it;
4724 int pos;
4725 int beg, end;
4726 Lisp_Object val, overlay;
4727
4728 pos = --IT_CHARPOS (it2);
4729 --IT_BYTEPOS (it2);
4730 it2.sp = 0;
4731 if (handle_display_prop (&it2) == HANDLED_RETURN
4732 && !NILP (val = get_char_property_and_overlay
4733 (make_number (pos), Qdisplay, Qnil, &overlay))
4734 && (OVERLAYP (overlay)
4735 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4736 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4737 {
4738 if (beg < BEGV)
4739 beg = BEGV;
4740 IT_CHARPOS (*it) = beg;
4741 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4742 continue;
4743 }
4744 }
4745
4746 break;
4747 }
4748
4749 xassert (IT_CHARPOS (*it) >= BEGV);
4750 xassert (IT_CHARPOS (*it) == BEGV
4751 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4752 CHECK_IT (it);
4753 }
4754
4755
4756 /* Reseat iterator IT at the previous visible line start. Skip
4757 invisible text that is so either due to text properties or due to
4758 selective display. At the end, update IT's overlay information,
4759 face information etc. */
4760
4761 void
4762 reseat_at_previous_visible_line_start (it)
4763 struct it *it;
4764 {
4765 back_to_previous_visible_line_start (it);
4766 reseat (it, it->current.pos, 1);
4767 CHECK_IT (it);
4768 }
4769
4770
4771 /* Reseat iterator IT on the next visible line start in the current
4772 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4773 preceding the line start. Skip over invisible text that is so
4774 because of selective display. Compute faces, overlays etc at the
4775 new position. Note that this function does not skip over text that
4776 is invisible because of text properties. */
4777
4778 static void
4779 reseat_at_next_visible_line_start (it, on_newline_p)
4780 struct it *it;
4781 int on_newline_p;
4782 {
4783 int newline_found_p, skipped_p = 0;
4784
4785 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4786
4787 /* Skip over lines that are invisible because they are indented
4788 more than the value of IT->selective. */
4789 if (it->selective > 0)
4790 while (IT_CHARPOS (*it) < ZV
4791 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4792 (double) it->selective)) /* iftc */
4793 {
4794 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4795 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4796 }
4797
4798 /* Position on the newline if that's what's requested. */
4799 if (on_newline_p && newline_found_p)
4800 {
4801 if (STRINGP (it->string))
4802 {
4803 if (IT_STRING_CHARPOS (*it) > 0)
4804 {
4805 --IT_STRING_CHARPOS (*it);
4806 --IT_STRING_BYTEPOS (*it);
4807 }
4808 }
4809 else if (IT_CHARPOS (*it) > BEGV)
4810 {
4811 --IT_CHARPOS (*it);
4812 --IT_BYTEPOS (*it);
4813 reseat (it, it->current.pos, 0);
4814 }
4815 }
4816 else if (skipped_p)
4817 reseat (it, it->current.pos, 0);
4818
4819 CHECK_IT (it);
4820 }
4821
4822
4823 \f
4824 /***********************************************************************
4825 Changing an iterator's position
4826 ***********************************************************************/
4827
4828 /* Change IT's current position to POS in current_buffer. If FORCE_P
4829 is non-zero, always check for text properties at the new position.
4830 Otherwise, text properties are only looked up if POS >=
4831 IT->check_charpos of a property. */
4832
4833 static void
4834 reseat (it, pos, force_p)
4835 struct it *it;
4836 struct text_pos pos;
4837 int force_p;
4838 {
4839 int original_pos = IT_CHARPOS (*it);
4840
4841 reseat_1 (it, pos, 0);
4842
4843 /* Determine where to check text properties. Avoid doing it
4844 where possible because text property lookup is very expensive. */
4845 if (force_p
4846 || CHARPOS (pos) > it->stop_charpos
4847 || CHARPOS (pos) < original_pos)
4848 handle_stop (it);
4849
4850 CHECK_IT (it);
4851 }
4852
4853
4854 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4855 IT->stop_pos to POS, also. */
4856
4857 static void
4858 reseat_1 (it, pos, set_stop_p)
4859 struct it *it;
4860 struct text_pos pos;
4861 int set_stop_p;
4862 {
4863 /* Don't call this function when scanning a C string. */
4864 xassert (it->s == NULL);
4865
4866 /* POS must be a reasonable value. */
4867 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4868
4869 it->current.pos = it->position = pos;
4870 XSETBUFFER (it->object, current_buffer);
4871 it->end_charpos = ZV;
4872 it->dpvec = NULL;
4873 it->current.dpvec_index = -1;
4874 it->current.overlay_string_index = -1;
4875 IT_STRING_CHARPOS (*it) = -1;
4876 IT_STRING_BYTEPOS (*it) = -1;
4877 it->string = Qnil;
4878 it->method = GET_FROM_BUFFER;
4879 /* RMS: I added this to fix a bug in move_it_vertically_backward
4880 where it->area continued to relate to the starting point
4881 for the backward motion. Bug report from
4882 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4883 However, I am not sure whether reseat still does the right thing
4884 in general after this change. */
4885 it->area = TEXT_AREA;
4886 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4887 it->sp = 0;
4888 it->face_before_selective_p = 0;
4889
4890 if (set_stop_p)
4891 it->stop_charpos = CHARPOS (pos);
4892 }
4893
4894
4895 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4896 If S is non-null, it is a C string to iterate over. Otherwise,
4897 STRING gives a Lisp string to iterate over.
4898
4899 If PRECISION > 0, don't return more then PRECISION number of
4900 characters from the string.
4901
4902 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4903 characters have been returned. FIELD_WIDTH < 0 means an infinite
4904 field width.
4905
4906 MULTIBYTE = 0 means disable processing of multibyte characters,
4907 MULTIBYTE > 0 means enable it,
4908 MULTIBYTE < 0 means use IT->multibyte_p.
4909
4910 IT must be initialized via a prior call to init_iterator before
4911 calling this function. */
4912
4913 static void
4914 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4915 struct it *it;
4916 unsigned char *s;
4917 Lisp_Object string;
4918 int charpos;
4919 int precision, field_width, multibyte;
4920 {
4921 /* No region in strings. */
4922 it->region_beg_charpos = it->region_end_charpos = -1;
4923
4924 /* No text property checks performed by default, but see below. */
4925 it->stop_charpos = -1;
4926
4927 /* Set iterator position and end position. */
4928 bzero (&it->current, sizeof it->current);
4929 it->current.overlay_string_index = -1;
4930 it->current.dpvec_index = -1;
4931 xassert (charpos >= 0);
4932
4933 /* If STRING is specified, use its multibyteness, otherwise use the
4934 setting of MULTIBYTE, if specified. */
4935 if (multibyte >= 0)
4936 it->multibyte_p = multibyte > 0;
4937
4938 if (s == NULL)
4939 {
4940 xassert (STRINGP (string));
4941 it->string = string;
4942 it->s = NULL;
4943 it->end_charpos = it->string_nchars = SCHARS (string);
4944 it->method = GET_FROM_STRING;
4945 it->current.string_pos = string_pos (charpos, string);
4946 }
4947 else
4948 {
4949 it->s = s;
4950 it->string = Qnil;
4951
4952 /* Note that we use IT->current.pos, not it->current.string_pos,
4953 for displaying C strings. */
4954 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4955 if (it->multibyte_p)
4956 {
4957 it->current.pos = c_string_pos (charpos, s, 1);
4958 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4959 }
4960 else
4961 {
4962 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4963 it->end_charpos = it->string_nchars = strlen (s);
4964 }
4965
4966 it->method = GET_FROM_C_STRING;
4967 }
4968
4969 /* PRECISION > 0 means don't return more than PRECISION characters
4970 from the string. */
4971 if (precision > 0 && it->end_charpos - charpos > precision)
4972 it->end_charpos = it->string_nchars = charpos + precision;
4973
4974 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4975 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4976 FIELD_WIDTH < 0 means infinite field width. This is useful for
4977 padding with `-' at the end of a mode line. */
4978 if (field_width < 0)
4979 field_width = INFINITY;
4980 if (field_width > it->end_charpos - charpos)
4981 it->end_charpos = charpos + field_width;
4982
4983 /* Use the standard display table for displaying strings. */
4984 if (DISP_TABLE_P (Vstandard_display_table))
4985 it->dp = XCHAR_TABLE (Vstandard_display_table);
4986
4987 it->stop_charpos = charpos;
4988 CHECK_IT (it);
4989 }
4990
4991
4992 \f
4993 /***********************************************************************
4994 Iteration
4995 ***********************************************************************/
4996
4997 /* Map enum it_method value to corresponding next_element_from_* function. */
4998
4999 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5000 {
5001 next_element_from_buffer,
5002 next_element_from_display_vector,
5003 next_element_from_composition,
5004 next_element_from_string,
5005 next_element_from_c_string,
5006 next_element_from_image,
5007 next_element_from_stretch
5008 };
5009
5010
5011 /* Load IT's display element fields with information about the next
5012 display element from the current position of IT. Value is zero if
5013 end of buffer (or C string) is reached. */
5014
5015 int
5016 get_next_display_element (it)
5017 struct it *it;
5018 {
5019 /* Non-zero means that we found a display element. Zero means that
5020 we hit the end of what we iterate over. Performance note: the
5021 function pointer `method' used here turns out to be faster than
5022 using a sequence of if-statements. */
5023 int success_p;
5024
5025 get_next:
5026 success_p = (*get_next_element[it->method]) (it);
5027
5028 if (it->what == IT_CHARACTER)
5029 {
5030 /* Map via display table or translate control characters.
5031 IT->c, IT->len etc. have been set to the next character by
5032 the function call above. If we have a display table, and it
5033 contains an entry for IT->c, translate it. Don't do this if
5034 IT->c itself comes from a display table, otherwise we could
5035 end up in an infinite recursion. (An alternative could be to
5036 count the recursion depth of this function and signal an
5037 error when a certain maximum depth is reached.) Is it worth
5038 it? */
5039 if (success_p && it->dpvec == NULL)
5040 {
5041 Lisp_Object dv;
5042
5043 if (it->dp
5044 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5045 VECTORP (dv)))
5046 {
5047 struct Lisp_Vector *v = XVECTOR (dv);
5048
5049 /* Return the first character from the display table
5050 entry, if not empty. If empty, don't display the
5051 current character. */
5052 if (v->size)
5053 {
5054 it->dpvec_char_len = it->len;
5055 it->dpvec = v->contents;
5056 it->dpend = v->contents + v->size;
5057 it->current.dpvec_index = 0;
5058 it->dpvec_face_id = -1;
5059 it->saved_face_id = it->face_id;
5060 it->method = GET_FROM_DISPLAY_VECTOR;
5061 it->ellipsis_p = 0;
5062 }
5063 else
5064 {
5065 set_iterator_to_next (it, 0);
5066 }
5067 goto get_next;
5068 }
5069
5070 /* Translate control characters into `\003' or `^C' form.
5071 Control characters coming from a display table entry are
5072 currently not translated because we use IT->dpvec to hold
5073 the translation. This could easily be changed but I
5074 don't believe that it is worth doing.
5075
5076 If it->multibyte_p is nonzero, eight-bit characters and
5077 non-printable multibyte characters are also translated to
5078 octal form.
5079
5080 If it->multibyte_p is zero, eight-bit characters that
5081 don't have corresponding multibyte char code are also
5082 translated to octal form. */
5083 else if ((it->c < ' '
5084 && (it->area != TEXT_AREA
5085 /* In mode line, treat \n like other crl chars. */
5086 || (it->c != '\t'
5087 && it->glyph_row && it->glyph_row->mode_line_p)
5088 || (it->c != '\n' && it->c != '\t')))
5089 || (it->multibyte_p
5090 ? ((it->c >= 127
5091 && it->len == 1)
5092 || !CHAR_PRINTABLE_P (it->c)
5093 || (!NILP (Vshow_nonbreak_escape)
5094 && (it->c == 0x8ad || it->c == 0x8a0
5095 || it->c == 0xf2d || it->c == 0xf20)))
5096 : (it->c >= 127
5097 && (!unibyte_display_via_language_environment
5098 || it->c == unibyte_char_to_multibyte (it->c)))))
5099 {
5100 /* IT->c is a control character which must be displayed
5101 either as '\003' or as `^C' where the '\\' and '^'
5102 can be defined in the display table. Fill
5103 IT->ctl_chars with glyphs for what we have to
5104 display. Then, set IT->dpvec to these glyphs. */
5105 GLYPH g;
5106 int ctl_len;
5107 int face_id, lface_id = 0 ;
5108 GLYPH escape_glyph;
5109
5110 if (it->c < 128 && it->ctl_arrow_p)
5111 {
5112 g = '^'; /* default glyph for Control */
5113 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5114 if (it->dp
5115 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5116 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5117 {
5118 g = XINT (DISP_CTRL_GLYPH (it->dp));
5119 lface_id = FAST_GLYPH_FACE (g);
5120 }
5121 if (lface_id)
5122 {
5123 g = FAST_GLYPH_CHAR (g);
5124 face_id = merge_faces (it->f, Qt, lface_id,
5125 it->face_id);
5126 }
5127 else
5128 {
5129 /* Merge the escape-glyph face into the current face. */
5130 face_id = merge_faces (it->f, Qescape_glyph, 0,
5131 it->face_id);
5132 }
5133
5134 XSETINT (it->ctl_chars[0], g);
5135 g = it->c ^ 0100;
5136 XSETINT (it->ctl_chars[1], g);
5137 ctl_len = 2;
5138 goto display_control;
5139 }
5140
5141 escape_glyph = '\\'; /* default for Octal display */
5142 if (it->dp
5143 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5144 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5145 {
5146 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5147 lface_id = FAST_GLYPH_FACE (escape_glyph);
5148 }
5149 if (lface_id)
5150 {
5151 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5152 face_id = merge_faces (it->f, Qt, lface_id,
5153 it->face_id);
5154 }
5155 else
5156 {
5157 /* Merge the escape-glyph face into the current face. */
5158 face_id = merge_faces (it->f, Qescape_glyph, 0,
5159 it->face_id);
5160 }
5161
5162 if (it->c == 0x8a0 || it->c == 0x8ad
5163 || it->c == 0xf20 || it->c == 0xf2d)
5164 {
5165 XSETINT (it->ctl_chars[0], escape_glyph);
5166 g = it->c;
5167 XSETINT (it->ctl_chars[1], g);
5168 ctl_len = 2;
5169 goto display_control;
5170 }
5171
5172 {
5173 unsigned char str[MAX_MULTIBYTE_LENGTH];
5174 int len;
5175 int i;
5176
5177 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5178 if (SINGLE_BYTE_CHAR_P (it->c))
5179 str[0] = it->c, len = 1;
5180 else
5181 {
5182 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5183 if (len < 0)
5184 {
5185 /* It's an invalid character, which shouldn't
5186 happen actually, but due to bugs it may
5187 happen. Let's print the char as is, there's
5188 not much meaningful we can do with it. */
5189 str[0] = it->c;
5190 str[1] = it->c >> 8;
5191 str[2] = it->c >> 16;
5192 str[3] = it->c >> 24;
5193 len = 4;
5194 }
5195 }
5196
5197 for (i = 0; i < len; i++)
5198 {
5199 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5200 /* Insert three more glyphs into IT->ctl_chars for
5201 the octal display of the character. */
5202 g = ((str[i] >> 6) & 7) + '0';
5203 XSETINT (it->ctl_chars[i * 4 + 1], g);
5204 g = ((str[i] >> 3) & 7) + '0';
5205 XSETINT (it->ctl_chars[i * 4 + 2], g);
5206 g = (str[i] & 7) + '0';
5207 XSETINT (it->ctl_chars[i * 4 + 3], g);
5208 }
5209 ctl_len = len * 4;
5210 }
5211
5212 display_control:
5213 /* Set up IT->dpvec and return first character from it. */
5214 it->dpvec_char_len = it->len;
5215 it->dpvec = it->ctl_chars;
5216 it->dpend = it->dpvec + ctl_len;
5217 it->current.dpvec_index = 0;
5218 it->dpvec_face_id = face_id;
5219 it->saved_face_id = it->face_id;
5220 it->method = GET_FROM_DISPLAY_VECTOR;
5221 it->ellipsis_p = 0;
5222 goto get_next;
5223 }
5224 }
5225
5226 /* Adjust face id for a multibyte character. There are no
5227 multibyte character in unibyte text. */
5228 if (it->multibyte_p
5229 && success_p
5230 && FRAME_WINDOW_P (it->f))
5231 {
5232 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5233 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5234 }
5235 }
5236
5237 /* Is this character the last one of a run of characters with
5238 box? If yes, set IT->end_of_box_run_p to 1. */
5239 if (it->face_box_p
5240 && it->s == NULL)
5241 {
5242 int face_id;
5243 struct face *face;
5244
5245 it->end_of_box_run_p
5246 = ((face_id = face_after_it_pos (it),
5247 face_id != it->face_id)
5248 && (face = FACE_FROM_ID (it->f, face_id),
5249 face->box == FACE_NO_BOX));
5250 }
5251
5252 /* Value is 0 if end of buffer or string reached. */
5253 return success_p;
5254 }
5255
5256
5257 /* Move IT to the next display element.
5258
5259 RESEAT_P non-zero means if called on a newline in buffer text,
5260 skip to the next visible line start.
5261
5262 Functions get_next_display_element and set_iterator_to_next are
5263 separate because I find this arrangement easier to handle than a
5264 get_next_display_element function that also increments IT's
5265 position. The way it is we can first look at an iterator's current
5266 display element, decide whether it fits on a line, and if it does,
5267 increment the iterator position. The other way around we probably
5268 would either need a flag indicating whether the iterator has to be
5269 incremented the next time, or we would have to implement a
5270 decrement position function which would not be easy to write. */
5271
5272 void
5273 set_iterator_to_next (it, reseat_p)
5274 struct it *it;
5275 int reseat_p;
5276 {
5277 /* Reset flags indicating start and end of a sequence of characters
5278 with box. Reset them at the start of this function because
5279 moving the iterator to a new position might set them. */
5280 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5281
5282 switch (it->method)
5283 {
5284 case GET_FROM_BUFFER:
5285 /* The current display element of IT is a character from
5286 current_buffer. Advance in the buffer, and maybe skip over
5287 invisible lines that are so because of selective display. */
5288 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5289 reseat_at_next_visible_line_start (it, 0);
5290 else
5291 {
5292 xassert (it->len != 0);
5293 IT_BYTEPOS (*it) += it->len;
5294 IT_CHARPOS (*it) += 1;
5295 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5296 }
5297 break;
5298
5299 case GET_FROM_COMPOSITION:
5300 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5301 if (STRINGP (it->string))
5302 {
5303 IT_STRING_BYTEPOS (*it) += it->len;
5304 IT_STRING_CHARPOS (*it) += it->cmp_len;
5305 it->method = GET_FROM_STRING;
5306 goto consider_string_end;
5307 }
5308 else
5309 {
5310 IT_BYTEPOS (*it) += it->len;
5311 IT_CHARPOS (*it) += it->cmp_len;
5312 it->method = GET_FROM_BUFFER;
5313 }
5314 break;
5315
5316 case GET_FROM_C_STRING:
5317 /* Current display element of IT is from a C string. */
5318 IT_BYTEPOS (*it) += it->len;
5319 IT_CHARPOS (*it) += 1;
5320 break;
5321
5322 case GET_FROM_DISPLAY_VECTOR:
5323 /* Current display element of IT is from a display table entry.
5324 Advance in the display table definition. Reset it to null if
5325 end reached, and continue with characters from buffers/
5326 strings. */
5327 ++it->current.dpvec_index;
5328
5329 /* Restore face of the iterator to what they were before the
5330 display vector entry (these entries may contain faces). */
5331 it->face_id = it->saved_face_id;
5332
5333 if (it->dpvec + it->current.dpvec_index == it->dpend)
5334 {
5335 if (it->s)
5336 it->method = GET_FROM_C_STRING;
5337 else if (STRINGP (it->string))
5338 it->method = GET_FROM_STRING;
5339 else
5340 it->method = GET_FROM_BUFFER;
5341
5342 it->dpvec = NULL;
5343 it->current.dpvec_index = -1;
5344
5345 /* Skip over characters which were displayed via IT->dpvec. */
5346 if (it->dpvec_char_len < 0)
5347 reseat_at_next_visible_line_start (it, 1);
5348 else if (it->dpvec_char_len > 0)
5349 {
5350 it->len = it->dpvec_char_len;
5351 set_iterator_to_next (it, reseat_p);
5352 }
5353
5354 /* Recheck faces after display vector */
5355 it->stop_charpos = IT_CHARPOS (*it);
5356 }
5357 break;
5358
5359 case GET_FROM_STRING:
5360 /* Current display element is a character from a Lisp string. */
5361 xassert (it->s == NULL && STRINGP (it->string));
5362 IT_STRING_BYTEPOS (*it) += it->len;
5363 IT_STRING_CHARPOS (*it) += 1;
5364
5365 consider_string_end:
5366
5367 if (it->current.overlay_string_index >= 0)
5368 {
5369 /* IT->string is an overlay string. Advance to the
5370 next, if there is one. */
5371 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5372 next_overlay_string (it);
5373 }
5374 else
5375 {
5376 /* IT->string is not an overlay string. If we reached
5377 its end, and there is something on IT->stack, proceed
5378 with what is on the stack. This can be either another
5379 string, this time an overlay string, or a buffer. */
5380 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5381 && it->sp > 0)
5382 {
5383 pop_it (it);
5384 if (STRINGP (it->string))
5385 goto consider_string_end;
5386 it->method = GET_FROM_BUFFER;
5387 }
5388 }
5389 break;
5390
5391 case GET_FROM_IMAGE:
5392 case GET_FROM_STRETCH:
5393 /* The position etc with which we have to proceed are on
5394 the stack. The position may be at the end of a string,
5395 if the `display' property takes up the whole string. */
5396 xassert (it->sp > 0);
5397 pop_it (it);
5398 it->image_id = 0;
5399 if (STRINGP (it->string))
5400 {
5401 it->method = GET_FROM_STRING;
5402 goto consider_string_end;
5403 }
5404 it->method = GET_FROM_BUFFER;
5405 break;
5406
5407 default:
5408 /* There are no other methods defined, so this should be a bug. */
5409 abort ();
5410 }
5411
5412 xassert (it->method != GET_FROM_STRING
5413 || (STRINGP (it->string)
5414 && IT_STRING_CHARPOS (*it) >= 0));
5415 }
5416
5417 /* Load IT's display element fields with information about the next
5418 display element which comes from a display table entry or from the
5419 result of translating a control character to one of the forms `^C'
5420 or `\003'.
5421
5422 IT->dpvec holds the glyphs to return as characters.
5423 IT->saved_face_id holds the face id before the display vector--
5424 it is restored into IT->face_idin set_iterator_to_next. */
5425
5426 static int
5427 next_element_from_display_vector (it)
5428 struct it *it;
5429 {
5430 /* Precondition. */
5431 xassert (it->dpvec && it->current.dpvec_index >= 0);
5432
5433 it->face_id = it->saved_face_id;
5434
5435 if (INTEGERP (*it->dpvec)
5436 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5437 {
5438 GLYPH g;
5439
5440 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5441 it->c = FAST_GLYPH_CHAR (g);
5442 it->len = CHAR_BYTES (it->c);
5443
5444 /* The entry may contain a face id to use. Such a face id is
5445 the id of a Lisp face, not a realized face. A face id of
5446 zero means no face is specified. */
5447 if (it->dpvec_face_id >= 0)
5448 it->face_id = it->dpvec_face_id;
5449 else
5450 {
5451 int lface_id = FAST_GLYPH_FACE (g);
5452 if (lface_id > 0)
5453 it->face_id = merge_faces (it->f, Qt, lface_id,
5454 it->saved_face_id);
5455 }
5456 }
5457 else
5458 /* Display table entry is invalid. Return a space. */
5459 it->c = ' ', it->len = 1;
5460
5461 /* Don't change position and object of the iterator here. They are
5462 still the values of the character that had this display table
5463 entry or was translated, and that's what we want. */
5464 it->what = IT_CHARACTER;
5465 return 1;
5466 }
5467
5468
5469 /* Load IT with the next display element from Lisp string IT->string.
5470 IT->current.string_pos is the current position within the string.
5471 If IT->current.overlay_string_index >= 0, the Lisp string is an
5472 overlay string. */
5473
5474 static int
5475 next_element_from_string (it)
5476 struct it *it;
5477 {
5478 struct text_pos position;
5479
5480 xassert (STRINGP (it->string));
5481 xassert (IT_STRING_CHARPOS (*it) >= 0);
5482 position = it->current.string_pos;
5483
5484 /* Time to check for invisible text? */
5485 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5486 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5487 {
5488 handle_stop (it);
5489
5490 /* Since a handler may have changed IT->method, we must
5491 recurse here. */
5492 return get_next_display_element (it);
5493 }
5494
5495 if (it->current.overlay_string_index >= 0)
5496 {
5497 /* Get the next character from an overlay string. In overlay
5498 strings, There is no field width or padding with spaces to
5499 do. */
5500 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5501 {
5502 it->what = IT_EOB;
5503 return 0;
5504 }
5505 else if (STRING_MULTIBYTE (it->string))
5506 {
5507 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5508 const unsigned char *s = (SDATA (it->string)
5509 + IT_STRING_BYTEPOS (*it));
5510 it->c = string_char_and_length (s, remaining, &it->len);
5511 }
5512 else
5513 {
5514 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5515 it->len = 1;
5516 }
5517 }
5518 else
5519 {
5520 /* Get the next character from a Lisp string that is not an
5521 overlay string. Such strings come from the mode line, for
5522 example. We may have to pad with spaces, or truncate the
5523 string. See also next_element_from_c_string. */
5524 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5525 {
5526 it->what = IT_EOB;
5527 return 0;
5528 }
5529 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5530 {
5531 /* Pad with spaces. */
5532 it->c = ' ', it->len = 1;
5533 CHARPOS (position) = BYTEPOS (position) = -1;
5534 }
5535 else if (STRING_MULTIBYTE (it->string))
5536 {
5537 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5538 const unsigned char *s = (SDATA (it->string)
5539 + IT_STRING_BYTEPOS (*it));
5540 it->c = string_char_and_length (s, maxlen, &it->len);
5541 }
5542 else
5543 {
5544 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5545 it->len = 1;
5546 }
5547 }
5548
5549 /* Record what we have and where it came from. Note that we store a
5550 buffer position in IT->position although it could arguably be a
5551 string position. */
5552 it->what = IT_CHARACTER;
5553 it->object = it->string;
5554 it->position = position;
5555 return 1;
5556 }
5557
5558
5559 /* Load IT with next display element from C string IT->s.
5560 IT->string_nchars is the maximum number of characters to return
5561 from the string. IT->end_charpos may be greater than
5562 IT->string_nchars when this function is called, in which case we
5563 may have to return padding spaces. Value is zero if end of string
5564 reached, including padding spaces. */
5565
5566 static int
5567 next_element_from_c_string (it)
5568 struct it *it;
5569 {
5570 int success_p = 1;
5571
5572 xassert (it->s);
5573 it->what = IT_CHARACTER;
5574 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5575 it->object = Qnil;
5576
5577 /* IT's position can be greater IT->string_nchars in case a field
5578 width or precision has been specified when the iterator was
5579 initialized. */
5580 if (IT_CHARPOS (*it) >= it->end_charpos)
5581 {
5582 /* End of the game. */
5583 it->what = IT_EOB;
5584 success_p = 0;
5585 }
5586 else if (IT_CHARPOS (*it) >= it->string_nchars)
5587 {
5588 /* Pad with spaces. */
5589 it->c = ' ', it->len = 1;
5590 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5591 }
5592 else if (it->multibyte_p)
5593 {
5594 /* Implementation note: The calls to strlen apparently aren't a
5595 performance problem because there is no noticeable performance
5596 difference between Emacs running in unibyte or multibyte mode. */
5597 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5598 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5599 maxlen, &it->len);
5600 }
5601 else
5602 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5603
5604 return success_p;
5605 }
5606
5607
5608 /* Set up IT to return characters from an ellipsis, if appropriate.
5609 The definition of the ellipsis glyphs may come from a display table
5610 entry. This function Fills IT with the first glyph from the
5611 ellipsis if an ellipsis is to be displayed. */
5612
5613 static int
5614 next_element_from_ellipsis (it)
5615 struct it *it;
5616 {
5617 if (it->selective_display_ellipsis_p)
5618 setup_for_ellipsis (it, it->len);
5619 else
5620 {
5621 /* The face at the current position may be different from the
5622 face we find after the invisible text. Remember what it
5623 was in IT->saved_face_id, and signal that it's there by
5624 setting face_before_selective_p. */
5625 it->saved_face_id = it->face_id;
5626 it->method = GET_FROM_BUFFER;
5627 reseat_at_next_visible_line_start (it, 1);
5628 it->face_before_selective_p = 1;
5629 }
5630
5631 return get_next_display_element (it);
5632 }
5633
5634
5635 /* Deliver an image display element. The iterator IT is already
5636 filled with image information (done in handle_display_prop). Value
5637 is always 1. */
5638
5639
5640 static int
5641 next_element_from_image (it)
5642 struct it *it;
5643 {
5644 it->what = IT_IMAGE;
5645 return 1;
5646 }
5647
5648
5649 /* Fill iterator IT with next display element from a stretch glyph
5650 property. IT->object is the value of the text property. Value is
5651 always 1. */
5652
5653 static int
5654 next_element_from_stretch (it)
5655 struct it *it;
5656 {
5657 it->what = IT_STRETCH;
5658 return 1;
5659 }
5660
5661
5662 /* Load IT with the next display element from current_buffer. Value
5663 is zero if end of buffer reached. IT->stop_charpos is the next
5664 position at which to stop and check for text properties or buffer
5665 end. */
5666
5667 static int
5668 next_element_from_buffer (it)
5669 struct it *it;
5670 {
5671 int success_p = 1;
5672
5673 /* Check this assumption, otherwise, we would never enter the
5674 if-statement, below. */
5675 xassert (IT_CHARPOS (*it) >= BEGV
5676 && IT_CHARPOS (*it) <= it->stop_charpos);
5677
5678 if (IT_CHARPOS (*it) >= it->stop_charpos)
5679 {
5680 if (IT_CHARPOS (*it) >= it->end_charpos)
5681 {
5682 int overlay_strings_follow_p;
5683
5684 /* End of the game, except when overlay strings follow that
5685 haven't been returned yet. */
5686 if (it->overlay_strings_at_end_processed_p)
5687 overlay_strings_follow_p = 0;
5688 else
5689 {
5690 it->overlay_strings_at_end_processed_p = 1;
5691 overlay_strings_follow_p = get_overlay_strings (it, 0);
5692 }
5693
5694 if (overlay_strings_follow_p)
5695 success_p = get_next_display_element (it);
5696 else
5697 {
5698 it->what = IT_EOB;
5699 it->position = it->current.pos;
5700 success_p = 0;
5701 }
5702 }
5703 else
5704 {
5705 handle_stop (it);
5706 return get_next_display_element (it);
5707 }
5708 }
5709 else
5710 {
5711 /* No face changes, overlays etc. in sight, so just return a
5712 character from current_buffer. */
5713 unsigned char *p;
5714
5715 /* Maybe run the redisplay end trigger hook. Performance note:
5716 This doesn't seem to cost measurable time. */
5717 if (it->redisplay_end_trigger_charpos
5718 && it->glyph_row
5719 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5720 run_redisplay_end_trigger_hook (it);
5721
5722 /* Get the next character, maybe multibyte. */
5723 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5724 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5725 {
5726 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5727 - IT_BYTEPOS (*it));
5728 it->c = string_char_and_length (p, maxlen, &it->len);
5729 }
5730 else
5731 it->c = *p, it->len = 1;
5732
5733 /* Record what we have and where it came from. */
5734 it->what = IT_CHARACTER;;
5735 it->object = it->w->buffer;
5736 it->position = it->current.pos;
5737
5738 /* Normally we return the character found above, except when we
5739 really want to return an ellipsis for selective display. */
5740 if (it->selective)
5741 {
5742 if (it->c == '\n')
5743 {
5744 /* A value of selective > 0 means hide lines indented more
5745 than that number of columns. */
5746 if (it->selective > 0
5747 && IT_CHARPOS (*it) + 1 < ZV
5748 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5749 IT_BYTEPOS (*it) + 1,
5750 (double) it->selective)) /* iftc */
5751 {
5752 success_p = next_element_from_ellipsis (it);
5753 it->dpvec_char_len = -1;
5754 }
5755 }
5756 else if (it->c == '\r' && it->selective == -1)
5757 {
5758 /* A value of selective == -1 means that everything from the
5759 CR to the end of the line is invisible, with maybe an
5760 ellipsis displayed for it. */
5761 success_p = next_element_from_ellipsis (it);
5762 it->dpvec_char_len = -1;
5763 }
5764 }
5765 }
5766
5767 /* Value is zero if end of buffer reached. */
5768 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5769 return success_p;
5770 }
5771
5772
5773 /* Run the redisplay end trigger hook for IT. */
5774
5775 static void
5776 run_redisplay_end_trigger_hook (it)
5777 struct it *it;
5778 {
5779 Lisp_Object args[3];
5780
5781 /* IT->glyph_row should be non-null, i.e. we should be actually
5782 displaying something, or otherwise we should not run the hook. */
5783 xassert (it->glyph_row);
5784
5785 /* Set up hook arguments. */
5786 args[0] = Qredisplay_end_trigger_functions;
5787 args[1] = it->window;
5788 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5789 it->redisplay_end_trigger_charpos = 0;
5790
5791 /* Since we are *trying* to run these functions, don't try to run
5792 them again, even if they get an error. */
5793 it->w->redisplay_end_trigger = Qnil;
5794 Frun_hook_with_args (3, args);
5795
5796 /* Notice if it changed the face of the character we are on. */
5797 handle_face_prop (it);
5798 }
5799
5800
5801 /* Deliver a composition display element. The iterator IT is already
5802 filled with composition information (done in
5803 handle_composition_prop). Value is always 1. */
5804
5805 static int
5806 next_element_from_composition (it)
5807 struct it *it;
5808 {
5809 it->what = IT_COMPOSITION;
5810 it->position = (STRINGP (it->string)
5811 ? it->current.string_pos
5812 : it->current.pos);
5813 return 1;
5814 }
5815
5816
5817 \f
5818 /***********************************************************************
5819 Moving an iterator without producing glyphs
5820 ***********************************************************************/
5821
5822 /* Move iterator IT to a specified buffer or X position within one
5823 line on the display without producing glyphs.
5824
5825 OP should be a bit mask including some or all of these bits:
5826 MOVE_TO_X: Stop on reaching x-position TO_X.
5827 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5828 Regardless of OP's value, stop in reaching the end of the display line.
5829
5830 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5831 This means, in particular, that TO_X includes window's horizontal
5832 scroll amount.
5833
5834 The return value has several possible values that
5835 say what condition caused the scan to stop:
5836
5837 MOVE_POS_MATCH_OR_ZV
5838 - when TO_POS or ZV was reached.
5839
5840 MOVE_X_REACHED
5841 -when TO_X was reached before TO_POS or ZV were reached.
5842
5843 MOVE_LINE_CONTINUED
5844 - when we reached the end of the display area and the line must
5845 be continued.
5846
5847 MOVE_LINE_TRUNCATED
5848 - when we reached the end of the display area and the line is
5849 truncated.
5850
5851 MOVE_NEWLINE_OR_CR
5852 - when we stopped at a line end, i.e. a newline or a CR and selective
5853 display is on. */
5854
5855 static enum move_it_result
5856 move_it_in_display_line_to (it, to_charpos, to_x, op)
5857 struct it *it;
5858 int to_charpos, to_x, op;
5859 {
5860 enum move_it_result result = MOVE_UNDEFINED;
5861 struct glyph_row *saved_glyph_row;
5862
5863 /* Don't produce glyphs in produce_glyphs. */
5864 saved_glyph_row = it->glyph_row;
5865 it->glyph_row = NULL;
5866
5867 #define BUFFER_POS_REACHED_P() \
5868 ((op & MOVE_TO_POS) != 0 \
5869 && BUFFERP (it->object) \
5870 && IT_CHARPOS (*it) >= to_charpos \
5871 && (it->method == GET_FROM_BUFFER \
5872 || (it->method == GET_FROM_DISPLAY_VECTOR \
5873 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5874
5875
5876 while (1)
5877 {
5878 int x, i, ascent = 0, descent = 0;
5879
5880 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5881 if ((op & MOVE_TO_POS) != 0
5882 && BUFFERP (it->object)
5883 && it->method == GET_FROM_BUFFER
5884 && IT_CHARPOS (*it) > to_charpos)
5885 {
5886 result = MOVE_POS_MATCH_OR_ZV;
5887 break;
5888 }
5889
5890 /* Stop when ZV reached.
5891 We used to stop here when TO_CHARPOS reached as well, but that is
5892 too soon if this glyph does not fit on this line. So we handle it
5893 explicitly below. */
5894 if (!get_next_display_element (it)
5895 || (it->truncate_lines_p
5896 && BUFFER_POS_REACHED_P ()))
5897 {
5898 result = MOVE_POS_MATCH_OR_ZV;
5899 break;
5900 }
5901
5902 /* The call to produce_glyphs will get the metrics of the
5903 display element IT is loaded with. We record in x the
5904 x-position before this display element in case it does not
5905 fit on the line. */
5906 x = it->current_x;
5907
5908 /* Remember the line height so far in case the next element doesn't
5909 fit on the line. */
5910 if (!it->truncate_lines_p)
5911 {
5912 ascent = it->max_ascent;
5913 descent = it->max_descent;
5914 }
5915
5916 PRODUCE_GLYPHS (it);
5917
5918 if (it->area != TEXT_AREA)
5919 {
5920 set_iterator_to_next (it, 1);
5921 continue;
5922 }
5923
5924 /* The number of glyphs we get back in IT->nglyphs will normally
5925 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5926 character on a terminal frame, or (iii) a line end. For the
5927 second case, IT->nglyphs - 1 padding glyphs will be present
5928 (on X frames, there is only one glyph produced for a
5929 composite character.
5930
5931 The behavior implemented below means, for continuation lines,
5932 that as many spaces of a TAB as fit on the current line are
5933 displayed there. For terminal frames, as many glyphs of a
5934 multi-glyph character are displayed in the current line, too.
5935 This is what the old redisplay code did, and we keep it that
5936 way. Under X, the whole shape of a complex character must
5937 fit on the line or it will be completely displayed in the
5938 next line.
5939
5940 Note that both for tabs and padding glyphs, all glyphs have
5941 the same width. */
5942 if (it->nglyphs)
5943 {
5944 /* More than one glyph or glyph doesn't fit on line. All
5945 glyphs have the same width. */
5946 int single_glyph_width = it->pixel_width / it->nglyphs;
5947 int new_x;
5948
5949 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5950 {
5951 new_x = x + single_glyph_width;
5952
5953 /* We want to leave anything reaching TO_X to the caller. */
5954 if ((op & MOVE_TO_X) && new_x > to_x)
5955 {
5956 if (BUFFER_POS_REACHED_P ())
5957 goto buffer_pos_reached;
5958 it->current_x = x;
5959 result = MOVE_X_REACHED;
5960 break;
5961 }
5962 else if (/* Lines are continued. */
5963 !it->truncate_lines_p
5964 && (/* And glyph doesn't fit on the line. */
5965 new_x > it->last_visible_x
5966 /* Or it fits exactly and we're on a window
5967 system frame. */
5968 || (new_x == it->last_visible_x
5969 && FRAME_WINDOW_P (it->f))))
5970 {
5971 if (/* IT->hpos == 0 means the very first glyph
5972 doesn't fit on the line, e.g. a wide image. */
5973 it->hpos == 0
5974 || (new_x == it->last_visible_x
5975 && FRAME_WINDOW_P (it->f)))
5976 {
5977 ++it->hpos;
5978 it->current_x = new_x;
5979 if (i == it->nglyphs - 1)
5980 {
5981 set_iterator_to_next (it, 1);
5982 #ifdef HAVE_WINDOW_SYSTEM
5983 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5984 {
5985 if (!get_next_display_element (it))
5986 {
5987 result = MOVE_POS_MATCH_OR_ZV;
5988 break;
5989 }
5990 if (BUFFER_POS_REACHED_P ())
5991 {
5992 if (ITERATOR_AT_END_OF_LINE_P (it))
5993 result = MOVE_POS_MATCH_OR_ZV;
5994 else
5995 result = MOVE_LINE_CONTINUED;
5996 break;
5997 }
5998 if (ITERATOR_AT_END_OF_LINE_P (it))
5999 {
6000 result = MOVE_NEWLINE_OR_CR;
6001 break;
6002 }
6003 }
6004 #endif /* HAVE_WINDOW_SYSTEM */
6005 }
6006 }
6007 else
6008 {
6009 it->current_x = x;
6010 it->max_ascent = ascent;
6011 it->max_descent = descent;
6012 }
6013
6014 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6015 IT_CHARPOS (*it)));
6016 result = MOVE_LINE_CONTINUED;
6017 break;
6018 }
6019 else if (BUFFER_POS_REACHED_P ())
6020 goto buffer_pos_reached;
6021 else if (new_x > it->first_visible_x)
6022 {
6023 /* Glyph is visible. Increment number of glyphs that
6024 would be displayed. */
6025 ++it->hpos;
6026 }
6027 else
6028 {
6029 /* Glyph is completely off the left margin of the display
6030 area. Nothing to do. */
6031 }
6032 }
6033
6034 if (result != MOVE_UNDEFINED)
6035 break;
6036 }
6037 else if (BUFFER_POS_REACHED_P ())
6038 {
6039 buffer_pos_reached:
6040 it->current_x = x;
6041 it->max_ascent = ascent;
6042 it->max_descent = descent;
6043 result = MOVE_POS_MATCH_OR_ZV;
6044 break;
6045 }
6046 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6047 {
6048 /* Stop when TO_X specified and reached. This check is
6049 necessary here because of lines consisting of a line end,
6050 only. The line end will not produce any glyphs and we
6051 would never get MOVE_X_REACHED. */
6052 xassert (it->nglyphs == 0);
6053 result = MOVE_X_REACHED;
6054 break;
6055 }
6056
6057 /* Is this a line end? If yes, we're done. */
6058 if (ITERATOR_AT_END_OF_LINE_P (it))
6059 {
6060 result = MOVE_NEWLINE_OR_CR;
6061 break;
6062 }
6063
6064 /* The current display element has been consumed. Advance
6065 to the next. */
6066 set_iterator_to_next (it, 1);
6067
6068 /* Stop if lines are truncated and IT's current x-position is
6069 past the right edge of the window now. */
6070 if (it->truncate_lines_p
6071 && it->current_x >= it->last_visible_x)
6072 {
6073 #ifdef HAVE_WINDOW_SYSTEM
6074 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6075 {
6076 if (!get_next_display_element (it)
6077 || BUFFER_POS_REACHED_P ())
6078 {
6079 result = MOVE_POS_MATCH_OR_ZV;
6080 break;
6081 }
6082 if (ITERATOR_AT_END_OF_LINE_P (it))
6083 {
6084 result = MOVE_NEWLINE_OR_CR;
6085 break;
6086 }
6087 }
6088 #endif /* HAVE_WINDOW_SYSTEM */
6089 result = MOVE_LINE_TRUNCATED;
6090 break;
6091 }
6092 }
6093
6094 #undef BUFFER_POS_REACHED_P
6095
6096 /* Restore the iterator settings altered at the beginning of this
6097 function. */
6098 it->glyph_row = saved_glyph_row;
6099 return result;
6100 }
6101
6102
6103 /* Move IT forward until it satisfies one or more of the criteria in
6104 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6105
6106 OP is a bit-mask that specifies where to stop, and in particular,
6107 which of those four position arguments makes a difference. See the
6108 description of enum move_operation_enum.
6109
6110 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6111 screen line, this function will set IT to the next position >
6112 TO_CHARPOS. */
6113
6114 void
6115 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6116 struct it *it;
6117 int to_charpos, to_x, to_y, to_vpos;
6118 int op;
6119 {
6120 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6121 int line_height;
6122 int reached = 0;
6123
6124 for (;;)
6125 {
6126 if (op & MOVE_TO_VPOS)
6127 {
6128 /* If no TO_CHARPOS and no TO_X specified, stop at the
6129 start of the line TO_VPOS. */
6130 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6131 {
6132 if (it->vpos == to_vpos)
6133 {
6134 reached = 1;
6135 break;
6136 }
6137 else
6138 skip = move_it_in_display_line_to (it, -1, -1, 0);
6139 }
6140 else
6141 {
6142 /* TO_VPOS >= 0 means stop at TO_X in the line at
6143 TO_VPOS, or at TO_POS, whichever comes first. */
6144 if (it->vpos == to_vpos)
6145 {
6146 reached = 2;
6147 break;
6148 }
6149
6150 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6151
6152 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6153 {
6154 reached = 3;
6155 break;
6156 }
6157 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6158 {
6159 /* We have reached TO_X but not in the line we want. */
6160 skip = move_it_in_display_line_to (it, to_charpos,
6161 -1, MOVE_TO_POS);
6162 if (skip == MOVE_POS_MATCH_OR_ZV)
6163 {
6164 reached = 4;
6165 break;
6166 }
6167 }
6168 }
6169 }
6170 else if (op & MOVE_TO_Y)
6171 {
6172 struct it it_backup;
6173
6174 /* TO_Y specified means stop at TO_X in the line containing
6175 TO_Y---or at TO_CHARPOS if this is reached first. The
6176 problem is that we can't really tell whether the line
6177 contains TO_Y before we have completely scanned it, and
6178 this may skip past TO_X. What we do is to first scan to
6179 TO_X.
6180
6181 If TO_X is not specified, use a TO_X of zero. The reason
6182 is to make the outcome of this function more predictable.
6183 If we didn't use TO_X == 0, we would stop at the end of
6184 the line which is probably not what a caller would expect
6185 to happen. */
6186 skip = move_it_in_display_line_to (it, to_charpos,
6187 ((op & MOVE_TO_X)
6188 ? to_x : 0),
6189 (MOVE_TO_X
6190 | (op & MOVE_TO_POS)));
6191
6192 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6193 if (skip == MOVE_POS_MATCH_OR_ZV)
6194 {
6195 reached = 5;
6196 break;
6197 }
6198
6199 /* If TO_X was reached, we would like to know whether TO_Y
6200 is in the line. This can only be said if we know the
6201 total line height which requires us to scan the rest of
6202 the line. */
6203 if (skip == MOVE_X_REACHED)
6204 {
6205 it_backup = *it;
6206 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6207 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6208 op & MOVE_TO_POS);
6209 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6210 }
6211
6212 /* Now, decide whether TO_Y is in this line. */
6213 line_height = it->max_ascent + it->max_descent;
6214 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6215
6216 if (to_y >= it->current_y
6217 && to_y < it->current_y + line_height)
6218 {
6219 if (skip == MOVE_X_REACHED)
6220 /* If TO_Y is in this line and TO_X was reached above,
6221 we scanned too far. We have to restore IT's settings
6222 to the ones before skipping. */
6223 *it = it_backup;
6224 reached = 6;
6225 }
6226 else if (skip == MOVE_X_REACHED)
6227 {
6228 skip = skip2;
6229 if (skip == MOVE_POS_MATCH_OR_ZV)
6230 reached = 7;
6231 }
6232
6233 if (reached)
6234 break;
6235 }
6236 else
6237 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6238
6239 switch (skip)
6240 {
6241 case MOVE_POS_MATCH_OR_ZV:
6242 reached = 8;
6243 goto out;
6244
6245 case MOVE_NEWLINE_OR_CR:
6246 set_iterator_to_next (it, 1);
6247 it->continuation_lines_width = 0;
6248 break;
6249
6250 case MOVE_LINE_TRUNCATED:
6251 it->continuation_lines_width = 0;
6252 reseat_at_next_visible_line_start (it, 0);
6253 if ((op & MOVE_TO_POS) != 0
6254 && IT_CHARPOS (*it) > to_charpos)
6255 {
6256 reached = 9;
6257 goto out;
6258 }
6259 break;
6260
6261 case MOVE_LINE_CONTINUED:
6262 it->continuation_lines_width += it->current_x;
6263 break;
6264
6265 default:
6266 abort ();
6267 }
6268
6269 /* Reset/increment for the next run. */
6270 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6271 it->current_x = it->hpos = 0;
6272 it->current_y += it->max_ascent + it->max_descent;
6273 ++it->vpos;
6274 last_height = it->max_ascent + it->max_descent;
6275 last_max_ascent = it->max_ascent;
6276 it->max_ascent = it->max_descent = 0;
6277 }
6278
6279 out:
6280
6281 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6282 }
6283
6284
6285 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6286
6287 If DY > 0, move IT backward at least that many pixels. DY = 0
6288 means move IT backward to the preceding line start or BEGV. This
6289 function may move over more than DY pixels if IT->current_y - DY
6290 ends up in the middle of a line; in this case IT->current_y will be
6291 set to the top of the line moved to. */
6292
6293 void
6294 move_it_vertically_backward (it, dy)
6295 struct it *it;
6296 int dy;
6297 {
6298 int nlines, h;
6299 struct it it2, it3;
6300 int start_pos;
6301
6302 move_further_back:
6303 xassert (dy >= 0);
6304
6305 start_pos = IT_CHARPOS (*it);
6306
6307 /* Estimate how many newlines we must move back. */
6308 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6309
6310 /* Set the iterator's position that many lines back. */
6311 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6312 back_to_previous_visible_line_start (it);
6313
6314 /* Reseat the iterator here. When moving backward, we don't want
6315 reseat to skip forward over invisible text, set up the iterator
6316 to deliver from overlay strings at the new position etc. So,
6317 use reseat_1 here. */
6318 reseat_1 (it, it->current.pos, 1);
6319
6320 /* We are now surely at a line start. */
6321 it->current_x = it->hpos = 0;
6322 it->continuation_lines_width = 0;
6323
6324 /* Move forward and see what y-distance we moved. First move to the
6325 start of the next line so that we get its height. We need this
6326 height to be able to tell whether we reached the specified
6327 y-distance. */
6328 it2 = *it;
6329 it2.max_ascent = it2.max_descent = 0;
6330 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6331 MOVE_TO_POS | MOVE_TO_VPOS);
6332 xassert (IT_CHARPOS (*it) >= BEGV);
6333 it3 = it2;
6334
6335 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6336 xassert (IT_CHARPOS (*it) >= BEGV);
6337 /* H is the actual vertical distance from the position in *IT
6338 and the starting position. */
6339 h = it2.current_y - it->current_y;
6340 /* NLINES is the distance in number of lines. */
6341 nlines = it2.vpos - it->vpos;
6342
6343 /* Correct IT's y and vpos position
6344 so that they are relative to the starting point. */
6345 it->vpos -= nlines;
6346 it->current_y -= h;
6347
6348 if (dy == 0)
6349 {
6350 /* DY == 0 means move to the start of the screen line. The
6351 value of nlines is > 0 if continuation lines were involved. */
6352 if (nlines > 0)
6353 move_it_by_lines (it, nlines, 1);
6354 #if 0
6355 /* I think this assert is bogus if buffer contains
6356 invisible text or images. KFS. */
6357 xassert (IT_CHARPOS (*it) <= start_pos);
6358 #endif
6359 }
6360 else
6361 {
6362 /* The y-position we try to reach, relative to *IT.
6363 Note that H has been subtracted in front of the if-statement. */
6364 int target_y = it->current_y + h - dy;
6365 int y0 = it3.current_y;
6366 int y1 = line_bottom_y (&it3);
6367 int line_height = y1 - y0;
6368
6369 /* If we did not reach target_y, try to move further backward if
6370 we can. If we moved too far backward, try to move forward. */
6371 if (target_y < it->current_y
6372 /* This is heuristic. In a window that's 3 lines high, with
6373 a line height of 13 pixels each, recentering with point
6374 on the bottom line will try to move -39/2 = 19 pixels
6375 backward. Try to avoid moving into the first line. */
6376 && (it->current_y - target_y
6377 > min (window_box_height (it->w), line_height * 2 / 3))
6378 && IT_CHARPOS (*it) > BEGV)
6379 {
6380 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6381 target_y - it->current_y));
6382 dy = it->current_y - target_y;
6383 goto move_further_back;
6384 }
6385 else if (target_y >= it->current_y + line_height
6386 && IT_CHARPOS (*it) < ZV)
6387 {
6388 /* Should move forward by at least one line, maybe more.
6389
6390 Note: Calling move_it_by_lines can be expensive on
6391 terminal frames, where compute_motion is used (via
6392 vmotion) to do the job, when there are very long lines
6393 and truncate-lines is nil. That's the reason for
6394 treating terminal frames specially here. */
6395
6396 if (!FRAME_WINDOW_P (it->f))
6397 move_it_vertically (it, target_y - (it->current_y + line_height));
6398 else
6399 {
6400 do
6401 {
6402 move_it_by_lines (it, 1, 1);
6403 }
6404 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6405 }
6406
6407 #if 0
6408 /* I think this assert is bogus if buffer contains
6409 invisible text or images. KFS. */
6410 xassert (IT_CHARPOS (*it) >= BEGV);
6411 #endif
6412 }
6413 }
6414 }
6415
6416
6417 /* Move IT by a specified amount of pixel lines DY. DY negative means
6418 move backwards. DY = 0 means move to start of screen line. At the
6419 end, IT will be on the start of a screen line. */
6420
6421 void
6422 move_it_vertically (it, dy)
6423 struct it *it;
6424 int dy;
6425 {
6426 if (dy <= 0)
6427 move_it_vertically_backward (it, -dy);
6428 else
6429 {
6430 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6431 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6432 MOVE_TO_POS | MOVE_TO_Y);
6433 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6434
6435 /* If buffer ends in ZV without a newline, move to the start of
6436 the line to satisfy the post-condition. */
6437 if (IT_CHARPOS (*it) == ZV
6438 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6439 move_it_by_lines (it, 0, 0);
6440 }
6441 }
6442
6443
6444 /* Move iterator IT past the end of the text line it is in. */
6445
6446 void
6447 move_it_past_eol (it)
6448 struct it *it;
6449 {
6450 enum move_it_result rc;
6451
6452 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6453 if (rc == MOVE_NEWLINE_OR_CR)
6454 set_iterator_to_next (it, 0);
6455 }
6456
6457
6458 #if 0 /* Currently not used. */
6459
6460 /* Return non-zero if some text between buffer positions START_CHARPOS
6461 and END_CHARPOS is invisible. IT->window is the window for text
6462 property lookup. */
6463
6464 static int
6465 invisible_text_between_p (it, start_charpos, end_charpos)
6466 struct it *it;
6467 int start_charpos, end_charpos;
6468 {
6469 Lisp_Object prop, limit;
6470 int invisible_found_p;
6471
6472 xassert (it != NULL && start_charpos <= end_charpos);
6473
6474 /* Is text at START invisible? */
6475 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6476 it->window);
6477 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6478 invisible_found_p = 1;
6479 else
6480 {
6481 limit = Fnext_single_char_property_change (make_number (start_charpos),
6482 Qinvisible, Qnil,
6483 make_number (end_charpos));
6484 invisible_found_p = XFASTINT (limit) < end_charpos;
6485 }
6486
6487 return invisible_found_p;
6488 }
6489
6490 #endif /* 0 */
6491
6492
6493 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6494 negative means move up. DVPOS == 0 means move to the start of the
6495 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6496 NEED_Y_P is zero, IT->current_y will be left unchanged.
6497
6498 Further optimization ideas: If we would know that IT->f doesn't use
6499 a face with proportional font, we could be faster for
6500 truncate-lines nil. */
6501
6502 void
6503 move_it_by_lines (it, dvpos, need_y_p)
6504 struct it *it;
6505 int dvpos, need_y_p;
6506 {
6507 struct position pos;
6508
6509 if (!FRAME_WINDOW_P (it->f))
6510 {
6511 struct text_pos textpos;
6512
6513 /* We can use vmotion on frames without proportional fonts. */
6514 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6515 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6516 reseat (it, textpos, 1);
6517 it->vpos += pos.vpos;
6518 it->current_y += pos.vpos;
6519 }
6520 else if (dvpos == 0)
6521 {
6522 /* DVPOS == 0 means move to the start of the screen line. */
6523 move_it_vertically_backward (it, 0);
6524 xassert (it->current_x == 0 && it->hpos == 0);
6525 /* Let next call to line_bottom_y calculate real line height */
6526 last_height = 0;
6527 }
6528 else if (dvpos > 0)
6529 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6530 else
6531 {
6532 struct it it2;
6533 int start_charpos, i;
6534
6535 /* Start at the beginning of the screen line containing IT's
6536 position. */
6537 move_it_vertically_backward (it, 0);
6538
6539 /* Go back -DVPOS visible lines and reseat the iterator there. */
6540 start_charpos = IT_CHARPOS (*it);
6541 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6542 back_to_previous_visible_line_start (it);
6543 reseat (it, it->current.pos, 1);
6544 it->current_x = it->hpos = 0;
6545
6546 /* Above call may have moved too far if continuation lines
6547 are involved. Scan forward and see if it did. */
6548 it2 = *it;
6549 it2.vpos = it2.current_y = 0;
6550 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6551 it->vpos -= it2.vpos;
6552 it->current_y -= it2.current_y;
6553 it->current_x = it->hpos = 0;
6554
6555 /* If we moved too far back, move IT some lines forward. */
6556 if (it2.vpos > -dvpos)
6557 {
6558 int delta = it2.vpos + dvpos;
6559 it2 = *it;
6560 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6561 /* Move back again if we got too far ahead. */
6562 if (IT_CHARPOS (*it) >= start_charpos)
6563 *it = it2;
6564 }
6565 }
6566 }
6567
6568 /* Return 1 if IT points into the middle of a display vector. */
6569
6570 int
6571 in_display_vector_p (it)
6572 struct it *it;
6573 {
6574 return (it->method == GET_FROM_DISPLAY_VECTOR
6575 && it->current.dpvec_index > 0
6576 && it->dpvec + it->current.dpvec_index != it->dpend);
6577 }
6578
6579 \f
6580 /***********************************************************************
6581 Messages
6582 ***********************************************************************/
6583
6584
6585 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6586 to *Messages*. */
6587
6588 void
6589 add_to_log (format, arg1, arg2)
6590 char *format;
6591 Lisp_Object arg1, arg2;
6592 {
6593 Lisp_Object args[3];
6594 Lisp_Object msg, fmt;
6595 char *buffer;
6596 int len;
6597 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6598 USE_SAFE_ALLOCA;
6599
6600 /* Do nothing if called asynchronously. Inserting text into
6601 a buffer may call after-change-functions and alike and
6602 that would means running Lisp asynchronously. */
6603 if (handling_signal)
6604 return;
6605
6606 fmt = msg = Qnil;
6607 GCPRO4 (fmt, msg, arg1, arg2);
6608
6609 args[0] = fmt = build_string (format);
6610 args[1] = arg1;
6611 args[2] = arg2;
6612 msg = Fformat (3, args);
6613
6614 len = SBYTES (msg) + 1;
6615 SAFE_ALLOCA (buffer, char *, len);
6616 bcopy (SDATA (msg), buffer, len);
6617
6618 message_dolog (buffer, len - 1, 1, 0);
6619 SAFE_FREE ();
6620
6621 UNGCPRO;
6622 }
6623
6624
6625 /* Output a newline in the *Messages* buffer if "needs" one. */
6626
6627 void
6628 message_log_maybe_newline ()
6629 {
6630 if (message_log_need_newline)
6631 message_dolog ("", 0, 1, 0);
6632 }
6633
6634
6635 /* Add a string M of length NBYTES to the message log, optionally
6636 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6637 nonzero, means interpret the contents of M as multibyte. This
6638 function calls low-level routines in order to bypass text property
6639 hooks, etc. which might not be safe to run. */
6640
6641 void
6642 message_dolog (m, nbytes, nlflag, multibyte)
6643 const char *m;
6644 int nbytes, nlflag, multibyte;
6645 {
6646 if (!NILP (Vmemory_full))
6647 return;
6648
6649 if (!NILP (Vmessage_log_max))
6650 {
6651 struct buffer *oldbuf;
6652 Lisp_Object oldpoint, oldbegv, oldzv;
6653 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6654 int point_at_end = 0;
6655 int zv_at_end = 0;
6656 Lisp_Object old_deactivate_mark, tem;
6657 struct gcpro gcpro1;
6658
6659 old_deactivate_mark = Vdeactivate_mark;
6660 oldbuf = current_buffer;
6661 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6662 current_buffer->undo_list = Qt;
6663
6664 oldpoint = message_dolog_marker1;
6665 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6666 oldbegv = message_dolog_marker2;
6667 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6668 oldzv = message_dolog_marker3;
6669 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6670 GCPRO1 (old_deactivate_mark);
6671
6672 if (PT == Z)
6673 point_at_end = 1;
6674 if (ZV == Z)
6675 zv_at_end = 1;
6676
6677 BEGV = BEG;
6678 BEGV_BYTE = BEG_BYTE;
6679 ZV = Z;
6680 ZV_BYTE = Z_BYTE;
6681 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6682
6683 /* Insert the string--maybe converting multibyte to single byte
6684 or vice versa, so that all the text fits the buffer. */
6685 if (multibyte
6686 && NILP (current_buffer->enable_multibyte_characters))
6687 {
6688 int i, c, char_bytes;
6689 unsigned char work[1];
6690
6691 /* Convert a multibyte string to single-byte
6692 for the *Message* buffer. */
6693 for (i = 0; i < nbytes; i += char_bytes)
6694 {
6695 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6696 work[0] = (SINGLE_BYTE_CHAR_P (c)
6697 ? c
6698 : multibyte_char_to_unibyte (c, Qnil));
6699 insert_1_both (work, 1, 1, 1, 0, 0);
6700 }
6701 }
6702 else if (! multibyte
6703 && ! NILP (current_buffer->enable_multibyte_characters))
6704 {
6705 int i, c, char_bytes;
6706 unsigned char *msg = (unsigned char *) m;
6707 unsigned char str[MAX_MULTIBYTE_LENGTH];
6708 /* Convert a single-byte string to multibyte
6709 for the *Message* buffer. */
6710 for (i = 0; i < nbytes; i++)
6711 {
6712 c = unibyte_char_to_multibyte (msg[i]);
6713 char_bytes = CHAR_STRING (c, str);
6714 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6715 }
6716 }
6717 else if (nbytes)
6718 insert_1 (m, nbytes, 1, 0, 0);
6719
6720 if (nlflag)
6721 {
6722 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6723 insert_1 ("\n", 1, 1, 0, 0);
6724
6725 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6726 this_bol = PT;
6727 this_bol_byte = PT_BYTE;
6728
6729 /* See if this line duplicates the previous one.
6730 If so, combine duplicates. */
6731 if (this_bol > BEG)
6732 {
6733 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6734 prev_bol = PT;
6735 prev_bol_byte = PT_BYTE;
6736
6737 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6738 this_bol, this_bol_byte);
6739 if (dup)
6740 {
6741 del_range_both (prev_bol, prev_bol_byte,
6742 this_bol, this_bol_byte, 0);
6743 if (dup > 1)
6744 {
6745 char dupstr[40];
6746 int duplen;
6747
6748 /* If you change this format, don't forget to also
6749 change message_log_check_duplicate. */
6750 sprintf (dupstr, " [%d times]", dup);
6751 duplen = strlen (dupstr);
6752 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6753 insert_1 (dupstr, duplen, 1, 0, 1);
6754 }
6755 }
6756 }
6757
6758 /* If we have more than the desired maximum number of lines
6759 in the *Messages* buffer now, delete the oldest ones.
6760 This is safe because we don't have undo in this buffer. */
6761
6762 if (NATNUMP (Vmessage_log_max))
6763 {
6764 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6765 -XFASTINT (Vmessage_log_max) - 1, 0);
6766 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6767 }
6768 }
6769 BEGV = XMARKER (oldbegv)->charpos;
6770 BEGV_BYTE = marker_byte_position (oldbegv);
6771
6772 if (zv_at_end)
6773 {
6774 ZV = Z;
6775 ZV_BYTE = Z_BYTE;
6776 }
6777 else
6778 {
6779 ZV = XMARKER (oldzv)->charpos;
6780 ZV_BYTE = marker_byte_position (oldzv);
6781 }
6782
6783 if (point_at_end)
6784 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6785 else
6786 /* We can't do Fgoto_char (oldpoint) because it will run some
6787 Lisp code. */
6788 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6789 XMARKER (oldpoint)->bytepos);
6790
6791 UNGCPRO;
6792 unchain_marker (XMARKER (oldpoint));
6793 unchain_marker (XMARKER (oldbegv));
6794 unchain_marker (XMARKER (oldzv));
6795
6796 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6797 set_buffer_internal (oldbuf);
6798 if (NILP (tem))
6799 windows_or_buffers_changed = old_windows_or_buffers_changed;
6800 message_log_need_newline = !nlflag;
6801 Vdeactivate_mark = old_deactivate_mark;
6802 }
6803 }
6804
6805
6806 /* We are at the end of the buffer after just having inserted a newline.
6807 (Note: We depend on the fact we won't be crossing the gap.)
6808 Check to see if the most recent message looks a lot like the previous one.
6809 Return 0 if different, 1 if the new one should just replace it, or a
6810 value N > 1 if we should also append " [N times]". */
6811
6812 static int
6813 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6814 int prev_bol, this_bol;
6815 int prev_bol_byte, this_bol_byte;
6816 {
6817 int i;
6818 int len = Z_BYTE - 1 - this_bol_byte;
6819 int seen_dots = 0;
6820 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6821 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6822
6823 for (i = 0; i < len; i++)
6824 {
6825 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6826 seen_dots = 1;
6827 if (p1[i] != p2[i])
6828 return seen_dots;
6829 }
6830 p1 += len;
6831 if (*p1 == '\n')
6832 return 2;
6833 if (*p1++ == ' ' && *p1++ == '[')
6834 {
6835 int n = 0;
6836 while (*p1 >= '0' && *p1 <= '9')
6837 n = n * 10 + *p1++ - '0';
6838 if (strncmp (p1, " times]\n", 8) == 0)
6839 return n+1;
6840 }
6841 return 0;
6842 }
6843 \f
6844
6845 /* Display an echo area message M with a specified length of NBYTES
6846 bytes. The string may include null characters. If M is 0, clear
6847 out any existing message, and let the mini-buffer text show
6848 through.
6849
6850 The buffer M must continue to exist until after the echo area gets
6851 cleared or some other message gets displayed there. This means do
6852 not pass text that is stored in a Lisp string; do not pass text in
6853 a buffer that was alloca'd. */
6854
6855 void
6856 message2 (m, nbytes, multibyte)
6857 const char *m;
6858 int nbytes;
6859 int multibyte;
6860 {
6861 /* First flush out any partial line written with print. */
6862 message_log_maybe_newline ();
6863 if (m)
6864 message_dolog (m, nbytes, 1, multibyte);
6865 message2_nolog (m, nbytes, multibyte);
6866 }
6867
6868
6869 /* The non-logging counterpart of message2. */
6870
6871 void
6872 message2_nolog (m, nbytes, multibyte)
6873 const char *m;
6874 int nbytes, multibyte;
6875 {
6876 struct frame *sf = SELECTED_FRAME ();
6877 message_enable_multibyte = multibyte;
6878
6879 if (noninteractive)
6880 {
6881 if (noninteractive_need_newline)
6882 putc ('\n', stderr);
6883 noninteractive_need_newline = 0;
6884 if (m)
6885 fwrite (m, nbytes, 1, stderr);
6886 if (cursor_in_echo_area == 0)
6887 fprintf (stderr, "\n");
6888 fflush (stderr);
6889 }
6890 /* A null message buffer means that the frame hasn't really been
6891 initialized yet. Error messages get reported properly by
6892 cmd_error, so this must be just an informative message; toss it. */
6893 else if (INTERACTIVE
6894 && sf->glyphs_initialized_p
6895 && FRAME_MESSAGE_BUF (sf))
6896 {
6897 Lisp_Object mini_window;
6898 struct frame *f;
6899
6900 /* Get the frame containing the mini-buffer
6901 that the selected frame is using. */
6902 mini_window = FRAME_MINIBUF_WINDOW (sf);
6903 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6904
6905 FRAME_SAMPLE_VISIBILITY (f);
6906 if (FRAME_VISIBLE_P (sf)
6907 && ! FRAME_VISIBLE_P (f))
6908 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6909
6910 if (m)
6911 {
6912 set_message (m, Qnil, nbytes, multibyte);
6913 if (minibuffer_auto_raise)
6914 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6915 }
6916 else
6917 clear_message (1, 1);
6918
6919 do_pending_window_change (0);
6920 echo_area_display (1);
6921 do_pending_window_change (0);
6922 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6923 (*frame_up_to_date_hook) (f);
6924 }
6925 }
6926
6927
6928 /* Display an echo area message M with a specified length of NBYTES
6929 bytes. The string may include null characters. If M is not a
6930 string, clear out any existing message, and let the mini-buffer
6931 text show through. */
6932
6933 void
6934 message3 (m, nbytes, multibyte)
6935 Lisp_Object m;
6936 int nbytes;
6937 int multibyte;
6938 {
6939 struct gcpro gcpro1;
6940
6941 GCPRO1 (m);
6942 clear_message (1,1);
6943
6944 /* First flush out any partial line written with print. */
6945 message_log_maybe_newline ();
6946 if (STRINGP (m))
6947 message_dolog (SDATA (m), nbytes, 1, multibyte);
6948 message3_nolog (m, nbytes, multibyte);
6949
6950 UNGCPRO;
6951 }
6952
6953
6954 /* The non-logging version of message3. */
6955
6956 void
6957 message3_nolog (m, nbytes, multibyte)
6958 Lisp_Object m;
6959 int nbytes, multibyte;
6960 {
6961 struct frame *sf = SELECTED_FRAME ();
6962 message_enable_multibyte = multibyte;
6963
6964 if (noninteractive)
6965 {
6966 if (noninteractive_need_newline)
6967 putc ('\n', stderr);
6968 noninteractive_need_newline = 0;
6969 if (STRINGP (m))
6970 fwrite (SDATA (m), nbytes, 1, stderr);
6971 if (cursor_in_echo_area == 0)
6972 fprintf (stderr, "\n");
6973 fflush (stderr);
6974 }
6975 /* A null message buffer means that the frame hasn't really been
6976 initialized yet. Error messages get reported properly by
6977 cmd_error, so this must be just an informative message; toss it. */
6978 else if (INTERACTIVE
6979 && sf->glyphs_initialized_p
6980 && FRAME_MESSAGE_BUF (sf))
6981 {
6982 Lisp_Object mini_window;
6983 Lisp_Object frame;
6984 struct frame *f;
6985
6986 /* Get the frame containing the mini-buffer
6987 that the selected frame is using. */
6988 mini_window = FRAME_MINIBUF_WINDOW (sf);
6989 frame = XWINDOW (mini_window)->frame;
6990 f = XFRAME (frame);
6991
6992 FRAME_SAMPLE_VISIBILITY (f);
6993 if (FRAME_VISIBLE_P (sf)
6994 && !FRAME_VISIBLE_P (f))
6995 Fmake_frame_visible (frame);
6996
6997 if (STRINGP (m) && SCHARS (m) > 0)
6998 {
6999 set_message (NULL, m, nbytes, multibyte);
7000 if (minibuffer_auto_raise)
7001 Fraise_frame (frame);
7002 }
7003 else
7004 clear_message (1, 1);
7005
7006 do_pending_window_change (0);
7007 echo_area_display (1);
7008 do_pending_window_change (0);
7009 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7010 (*frame_up_to_date_hook) (f);
7011 }
7012 }
7013
7014
7015 /* Display a null-terminated echo area message M. If M is 0, clear
7016 out any existing message, and let the mini-buffer text show through.
7017
7018 The buffer M must continue to exist until after the echo area gets
7019 cleared or some other message gets displayed there. Do not pass
7020 text that is stored in a Lisp string. Do not pass text in a buffer
7021 that was alloca'd. */
7022
7023 void
7024 message1 (m)
7025 char *m;
7026 {
7027 message2 (m, (m ? strlen (m) : 0), 0);
7028 }
7029
7030
7031 /* The non-logging counterpart of message1. */
7032
7033 void
7034 message1_nolog (m)
7035 char *m;
7036 {
7037 message2_nolog (m, (m ? strlen (m) : 0), 0);
7038 }
7039
7040 /* Display a message M which contains a single %s
7041 which gets replaced with STRING. */
7042
7043 void
7044 message_with_string (m, string, log)
7045 char *m;
7046 Lisp_Object string;
7047 int log;
7048 {
7049 CHECK_STRING (string);
7050
7051 if (noninteractive)
7052 {
7053 if (m)
7054 {
7055 if (noninteractive_need_newline)
7056 putc ('\n', stderr);
7057 noninteractive_need_newline = 0;
7058 fprintf (stderr, m, SDATA (string));
7059 if (cursor_in_echo_area == 0)
7060 fprintf (stderr, "\n");
7061 fflush (stderr);
7062 }
7063 }
7064 else if (INTERACTIVE)
7065 {
7066 /* The frame whose minibuffer we're going to display the message on.
7067 It may be larger than the selected frame, so we need
7068 to use its buffer, not the selected frame's buffer. */
7069 Lisp_Object mini_window;
7070 struct frame *f, *sf = SELECTED_FRAME ();
7071
7072 /* Get the frame containing the minibuffer
7073 that the selected frame is using. */
7074 mini_window = FRAME_MINIBUF_WINDOW (sf);
7075 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7076
7077 /* A null message buffer means that the frame hasn't really been
7078 initialized yet. Error messages get reported properly by
7079 cmd_error, so this must be just an informative message; toss it. */
7080 if (FRAME_MESSAGE_BUF (f))
7081 {
7082 Lisp_Object args[2], message;
7083 struct gcpro gcpro1, gcpro2;
7084
7085 args[0] = build_string (m);
7086 args[1] = message = string;
7087 GCPRO2 (args[0], message);
7088 gcpro1.nvars = 2;
7089
7090 message = Fformat (2, args);
7091
7092 if (log)
7093 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7094 else
7095 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7096
7097 UNGCPRO;
7098
7099 /* Print should start at the beginning of the message
7100 buffer next time. */
7101 message_buf_print = 0;
7102 }
7103 }
7104 }
7105
7106
7107 /* Dump an informative message to the minibuf. If M is 0, clear out
7108 any existing message, and let the mini-buffer text show through. */
7109
7110 /* VARARGS 1 */
7111 void
7112 message (m, a1, a2, a3)
7113 char *m;
7114 EMACS_INT a1, a2, a3;
7115 {
7116 if (noninteractive)
7117 {
7118 if (m)
7119 {
7120 if (noninteractive_need_newline)
7121 putc ('\n', stderr);
7122 noninteractive_need_newline = 0;
7123 fprintf (stderr, m, a1, a2, a3);
7124 if (cursor_in_echo_area == 0)
7125 fprintf (stderr, "\n");
7126 fflush (stderr);
7127 }
7128 }
7129 else if (INTERACTIVE)
7130 {
7131 /* The frame whose mini-buffer we're going to display the message
7132 on. It may be larger than the selected frame, so we need to
7133 use its buffer, not the selected frame's buffer. */
7134 Lisp_Object mini_window;
7135 struct frame *f, *sf = SELECTED_FRAME ();
7136
7137 /* Get the frame containing the mini-buffer
7138 that the selected frame is using. */
7139 mini_window = FRAME_MINIBUF_WINDOW (sf);
7140 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7141
7142 /* A null message buffer means that the frame hasn't really been
7143 initialized yet. Error messages get reported properly by
7144 cmd_error, so this must be just an informative message; toss
7145 it. */
7146 if (FRAME_MESSAGE_BUF (f))
7147 {
7148 if (m)
7149 {
7150 int len;
7151 #ifdef NO_ARG_ARRAY
7152 char *a[3];
7153 a[0] = (char *) a1;
7154 a[1] = (char *) a2;
7155 a[2] = (char *) a3;
7156
7157 len = doprnt (FRAME_MESSAGE_BUF (f),
7158 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7159 #else
7160 len = doprnt (FRAME_MESSAGE_BUF (f),
7161 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7162 (char **) &a1);
7163 #endif /* NO_ARG_ARRAY */
7164
7165 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7166 }
7167 else
7168 message1 (0);
7169
7170 /* Print should start at the beginning of the message
7171 buffer next time. */
7172 message_buf_print = 0;
7173 }
7174 }
7175 }
7176
7177
7178 /* The non-logging version of message. */
7179
7180 void
7181 message_nolog (m, a1, a2, a3)
7182 char *m;
7183 EMACS_INT a1, a2, a3;
7184 {
7185 Lisp_Object old_log_max;
7186 old_log_max = Vmessage_log_max;
7187 Vmessage_log_max = Qnil;
7188 message (m, a1, a2, a3);
7189 Vmessage_log_max = old_log_max;
7190 }
7191
7192
7193 /* Display the current message in the current mini-buffer. This is
7194 only called from error handlers in process.c, and is not time
7195 critical. */
7196
7197 void
7198 update_echo_area ()
7199 {
7200 if (!NILP (echo_area_buffer[0]))
7201 {
7202 Lisp_Object string;
7203 string = Fcurrent_message ();
7204 message3 (string, SBYTES (string),
7205 !NILP (current_buffer->enable_multibyte_characters));
7206 }
7207 }
7208
7209
7210 /* Make sure echo area buffers in `echo_buffers' are live.
7211 If they aren't, make new ones. */
7212
7213 static void
7214 ensure_echo_area_buffers ()
7215 {
7216 int i;
7217
7218 for (i = 0; i < 2; ++i)
7219 if (!BUFFERP (echo_buffer[i])
7220 || NILP (XBUFFER (echo_buffer[i])->name))
7221 {
7222 char name[30];
7223 Lisp_Object old_buffer;
7224 int j;
7225
7226 old_buffer = echo_buffer[i];
7227 sprintf (name, " *Echo Area %d*", i);
7228 echo_buffer[i] = Fget_buffer_create (build_string (name));
7229 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7230
7231 for (j = 0; j < 2; ++j)
7232 if (EQ (old_buffer, echo_area_buffer[j]))
7233 echo_area_buffer[j] = echo_buffer[i];
7234 }
7235 }
7236
7237
7238 /* Call FN with args A1..A4 with either the current or last displayed
7239 echo_area_buffer as current buffer.
7240
7241 WHICH zero means use the current message buffer
7242 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7243 from echo_buffer[] and clear it.
7244
7245 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7246 suitable buffer from echo_buffer[] and clear it.
7247
7248 Value is what FN returns. */
7249
7250 static int
7251 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7252 struct window *w;
7253 int which;
7254 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7255 EMACS_INT a1;
7256 Lisp_Object a2;
7257 EMACS_INT a3, a4;
7258 {
7259 Lisp_Object buffer;
7260 int this_one, the_other, clear_buffer_p, rc;
7261 int count = SPECPDL_INDEX ();
7262
7263 /* If buffers aren't live, make new ones. */
7264 ensure_echo_area_buffers ();
7265
7266 clear_buffer_p = 0;
7267
7268 if (which == 0)
7269 this_one = 0, the_other = 1;
7270 else if (which > 0)
7271 this_one = 1, the_other = 0;
7272
7273 /* Choose a suitable buffer from echo_buffer[] is we don't
7274 have one. */
7275 if (NILP (echo_area_buffer[this_one]))
7276 {
7277 echo_area_buffer[this_one]
7278 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7279 ? echo_buffer[the_other]
7280 : echo_buffer[this_one]);
7281 clear_buffer_p = 1;
7282 }
7283
7284 buffer = echo_area_buffer[this_one];
7285
7286 /* Don't get confused by reusing the buffer used for echoing
7287 for a different purpose. */
7288 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7289 cancel_echoing ();
7290
7291 record_unwind_protect (unwind_with_echo_area_buffer,
7292 with_echo_area_buffer_unwind_data (w));
7293
7294 /* Make the echo area buffer current. Note that for display
7295 purposes, it is not necessary that the displayed window's buffer
7296 == current_buffer, except for text property lookup. So, let's
7297 only set that buffer temporarily here without doing a full
7298 Fset_window_buffer. We must also change w->pointm, though,
7299 because otherwise an assertions in unshow_buffer fails, and Emacs
7300 aborts. */
7301 set_buffer_internal_1 (XBUFFER (buffer));
7302 if (w)
7303 {
7304 w->buffer = buffer;
7305 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7306 }
7307
7308 current_buffer->undo_list = Qt;
7309 current_buffer->read_only = Qnil;
7310 specbind (Qinhibit_read_only, Qt);
7311 specbind (Qinhibit_modification_hooks, Qt);
7312
7313 if (clear_buffer_p && Z > BEG)
7314 del_range (BEG, Z);
7315
7316 xassert (BEGV >= BEG);
7317 xassert (ZV <= Z && ZV >= BEGV);
7318
7319 rc = fn (a1, a2, a3, a4);
7320
7321 xassert (BEGV >= BEG);
7322 xassert (ZV <= Z && ZV >= BEGV);
7323
7324 unbind_to (count, Qnil);
7325 return rc;
7326 }
7327
7328
7329 /* Save state that should be preserved around the call to the function
7330 FN called in with_echo_area_buffer. */
7331
7332 static Lisp_Object
7333 with_echo_area_buffer_unwind_data (w)
7334 struct window *w;
7335 {
7336 int i = 0;
7337 Lisp_Object vector;
7338
7339 /* Reduce consing by keeping one vector in
7340 Vwith_echo_area_save_vector. */
7341 vector = Vwith_echo_area_save_vector;
7342 Vwith_echo_area_save_vector = Qnil;
7343
7344 if (NILP (vector))
7345 vector = Fmake_vector (make_number (7), Qnil);
7346
7347 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7348 AREF (vector, i) = Vdeactivate_mark, ++i;
7349 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7350
7351 if (w)
7352 {
7353 XSETWINDOW (AREF (vector, i), w); ++i;
7354 AREF (vector, i) = w->buffer; ++i;
7355 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7356 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7357 }
7358 else
7359 {
7360 int end = i + 4;
7361 for (; i < end; ++i)
7362 AREF (vector, i) = Qnil;
7363 }
7364
7365 xassert (i == ASIZE (vector));
7366 return vector;
7367 }
7368
7369
7370 /* Restore global state from VECTOR which was created by
7371 with_echo_area_buffer_unwind_data. */
7372
7373 static Lisp_Object
7374 unwind_with_echo_area_buffer (vector)
7375 Lisp_Object vector;
7376 {
7377 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7378 Vdeactivate_mark = AREF (vector, 1);
7379 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7380
7381 if (WINDOWP (AREF (vector, 3)))
7382 {
7383 struct window *w;
7384 Lisp_Object buffer, charpos, bytepos;
7385
7386 w = XWINDOW (AREF (vector, 3));
7387 buffer = AREF (vector, 4);
7388 charpos = AREF (vector, 5);
7389 bytepos = AREF (vector, 6);
7390
7391 w->buffer = buffer;
7392 set_marker_both (w->pointm, buffer,
7393 XFASTINT (charpos), XFASTINT (bytepos));
7394 }
7395
7396 Vwith_echo_area_save_vector = vector;
7397 return Qnil;
7398 }
7399
7400
7401 /* Set up the echo area for use by print functions. MULTIBYTE_P
7402 non-zero means we will print multibyte. */
7403
7404 void
7405 setup_echo_area_for_printing (multibyte_p)
7406 int multibyte_p;
7407 {
7408 /* If we can't find an echo area any more, exit. */
7409 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7410 Fkill_emacs (Qnil);
7411
7412 ensure_echo_area_buffers ();
7413
7414 if (!message_buf_print)
7415 {
7416 /* A message has been output since the last time we printed.
7417 Choose a fresh echo area buffer. */
7418 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7419 echo_area_buffer[0] = echo_buffer[1];
7420 else
7421 echo_area_buffer[0] = echo_buffer[0];
7422
7423 /* Switch to that buffer and clear it. */
7424 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7425 current_buffer->truncate_lines = Qnil;
7426
7427 if (Z > BEG)
7428 {
7429 int count = SPECPDL_INDEX ();
7430 specbind (Qinhibit_read_only, Qt);
7431 /* Note that undo recording is always disabled. */
7432 del_range (BEG, Z);
7433 unbind_to (count, Qnil);
7434 }
7435 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7436
7437 /* Set up the buffer for the multibyteness we need. */
7438 if (multibyte_p
7439 != !NILP (current_buffer->enable_multibyte_characters))
7440 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7441
7442 /* Raise the frame containing the echo area. */
7443 if (minibuffer_auto_raise)
7444 {
7445 struct frame *sf = SELECTED_FRAME ();
7446 Lisp_Object mini_window;
7447 mini_window = FRAME_MINIBUF_WINDOW (sf);
7448 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7449 }
7450
7451 message_log_maybe_newline ();
7452 message_buf_print = 1;
7453 }
7454 else
7455 {
7456 if (NILP (echo_area_buffer[0]))
7457 {
7458 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7459 echo_area_buffer[0] = echo_buffer[1];
7460 else
7461 echo_area_buffer[0] = echo_buffer[0];
7462 }
7463
7464 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7465 {
7466 /* Someone switched buffers between print requests. */
7467 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7468 current_buffer->truncate_lines = Qnil;
7469 }
7470 }
7471 }
7472
7473
7474 /* Display an echo area message in window W. Value is non-zero if W's
7475 height is changed. If display_last_displayed_message_p is
7476 non-zero, display the message that was last displayed, otherwise
7477 display the current message. */
7478
7479 static int
7480 display_echo_area (w)
7481 struct window *w;
7482 {
7483 int i, no_message_p, window_height_changed_p, count;
7484
7485 /* Temporarily disable garbage collections while displaying the echo
7486 area. This is done because a GC can print a message itself.
7487 That message would modify the echo area buffer's contents while a
7488 redisplay of the buffer is going on, and seriously confuse
7489 redisplay. */
7490 count = inhibit_garbage_collection ();
7491
7492 /* If there is no message, we must call display_echo_area_1
7493 nevertheless because it resizes the window. But we will have to
7494 reset the echo_area_buffer in question to nil at the end because
7495 with_echo_area_buffer will sets it to an empty buffer. */
7496 i = display_last_displayed_message_p ? 1 : 0;
7497 no_message_p = NILP (echo_area_buffer[i]);
7498
7499 window_height_changed_p
7500 = with_echo_area_buffer (w, display_last_displayed_message_p,
7501 display_echo_area_1,
7502 (EMACS_INT) w, Qnil, 0, 0);
7503
7504 if (no_message_p)
7505 echo_area_buffer[i] = Qnil;
7506
7507 unbind_to (count, Qnil);
7508 return window_height_changed_p;
7509 }
7510
7511
7512 /* Helper for display_echo_area. Display the current buffer which
7513 contains the current echo area message in window W, a mini-window,
7514 a pointer to which is passed in A1. A2..A4 are currently not used.
7515 Change the height of W so that all of the message is displayed.
7516 Value is non-zero if height of W was changed. */
7517
7518 static int
7519 display_echo_area_1 (a1, a2, a3, a4)
7520 EMACS_INT a1;
7521 Lisp_Object a2;
7522 EMACS_INT a3, a4;
7523 {
7524 struct window *w = (struct window *) a1;
7525 Lisp_Object window;
7526 struct text_pos start;
7527 int window_height_changed_p = 0;
7528
7529 /* Do this before displaying, so that we have a large enough glyph
7530 matrix for the display. */
7531 window_height_changed_p = resize_mini_window (w, 0);
7532
7533 /* Display. */
7534 clear_glyph_matrix (w->desired_matrix);
7535 XSETWINDOW (window, w);
7536 SET_TEXT_POS (start, BEG, BEG_BYTE);
7537 try_window (window, start);
7538
7539 return window_height_changed_p;
7540 }
7541
7542
7543 /* Resize the echo area window to exactly the size needed for the
7544 currently displayed message, if there is one. If a mini-buffer
7545 is active, don't shrink it. */
7546
7547 void
7548 resize_echo_area_exactly ()
7549 {
7550 if (BUFFERP (echo_area_buffer[0])
7551 && WINDOWP (echo_area_window))
7552 {
7553 struct window *w = XWINDOW (echo_area_window);
7554 int resized_p;
7555 Lisp_Object resize_exactly;
7556
7557 if (minibuf_level == 0)
7558 resize_exactly = Qt;
7559 else
7560 resize_exactly = Qnil;
7561
7562 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7563 (EMACS_INT) w, resize_exactly, 0, 0);
7564 if (resized_p)
7565 {
7566 ++windows_or_buffers_changed;
7567 ++update_mode_lines;
7568 redisplay_internal (0);
7569 }
7570 }
7571 }
7572
7573
7574 /* Callback function for with_echo_area_buffer, when used from
7575 resize_echo_area_exactly. A1 contains a pointer to the window to
7576 resize, EXACTLY non-nil means resize the mini-window exactly to the
7577 size of the text displayed. A3 and A4 are not used. Value is what
7578 resize_mini_window returns. */
7579
7580 static int
7581 resize_mini_window_1 (a1, exactly, a3, a4)
7582 EMACS_INT a1;
7583 Lisp_Object exactly;
7584 EMACS_INT a3, a4;
7585 {
7586 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7587 }
7588
7589
7590 /* Resize mini-window W to fit the size of its contents. EXACT:P
7591 means size the window exactly to the size needed. Otherwise, it's
7592 only enlarged until W's buffer is empty. Value is non-zero if
7593 the window height has been changed. */
7594
7595 int
7596 resize_mini_window (w, exact_p)
7597 struct window *w;
7598 int exact_p;
7599 {
7600 struct frame *f = XFRAME (w->frame);
7601 int window_height_changed_p = 0;
7602
7603 xassert (MINI_WINDOW_P (w));
7604
7605 /* Don't resize windows while redisplaying a window; it would
7606 confuse redisplay functions when the size of the window they are
7607 displaying changes from under them. Such a resizing can happen,
7608 for instance, when which-func prints a long message while
7609 we are running fontification-functions. We're running these
7610 functions with safe_call which binds inhibit-redisplay to t. */
7611 if (!NILP (Vinhibit_redisplay))
7612 return 0;
7613
7614 /* Nil means don't try to resize. */
7615 if (NILP (Vresize_mini_windows)
7616 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7617 return 0;
7618
7619 if (!FRAME_MINIBUF_ONLY_P (f))
7620 {
7621 struct it it;
7622 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7623 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7624 int height, max_height;
7625 int unit = FRAME_LINE_HEIGHT (f);
7626 struct text_pos start;
7627 struct buffer *old_current_buffer = NULL;
7628
7629 if (current_buffer != XBUFFER (w->buffer))
7630 {
7631 old_current_buffer = current_buffer;
7632 set_buffer_internal (XBUFFER (w->buffer));
7633 }
7634
7635 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7636
7637 /* Compute the max. number of lines specified by the user. */
7638 if (FLOATP (Vmax_mini_window_height))
7639 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7640 else if (INTEGERP (Vmax_mini_window_height))
7641 max_height = XINT (Vmax_mini_window_height);
7642 else
7643 max_height = total_height / 4;
7644
7645 /* Correct that max. height if it's bogus. */
7646 max_height = max (1, max_height);
7647 max_height = min (total_height, max_height);
7648
7649 /* Find out the height of the text in the window. */
7650 if (it.truncate_lines_p)
7651 height = 1;
7652 else
7653 {
7654 last_height = 0;
7655 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7656 if (it.max_ascent == 0 && it.max_descent == 0)
7657 height = it.current_y + last_height;
7658 else
7659 height = it.current_y + it.max_ascent + it.max_descent;
7660 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7661 height = (height + unit - 1) / unit;
7662 }
7663
7664 /* Compute a suitable window start. */
7665 if (height > max_height)
7666 {
7667 height = max_height;
7668 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7669 move_it_vertically_backward (&it, (height - 1) * unit);
7670 start = it.current.pos;
7671 }
7672 else
7673 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7674 SET_MARKER_FROM_TEXT_POS (w->start, start);
7675
7676 if (EQ (Vresize_mini_windows, Qgrow_only))
7677 {
7678 /* Let it grow only, until we display an empty message, in which
7679 case the window shrinks again. */
7680 if (height > WINDOW_TOTAL_LINES (w))
7681 {
7682 int old_height = WINDOW_TOTAL_LINES (w);
7683 freeze_window_starts (f, 1);
7684 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7685 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7686 }
7687 else if (height < WINDOW_TOTAL_LINES (w)
7688 && (exact_p || BEGV == ZV))
7689 {
7690 int old_height = WINDOW_TOTAL_LINES (w);
7691 freeze_window_starts (f, 0);
7692 shrink_mini_window (w);
7693 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7694 }
7695 }
7696 else
7697 {
7698 /* Always resize to exact size needed. */
7699 if (height > WINDOW_TOTAL_LINES (w))
7700 {
7701 int old_height = WINDOW_TOTAL_LINES (w);
7702 freeze_window_starts (f, 1);
7703 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7704 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7705 }
7706 else if (height < WINDOW_TOTAL_LINES (w))
7707 {
7708 int old_height = WINDOW_TOTAL_LINES (w);
7709 freeze_window_starts (f, 0);
7710 shrink_mini_window (w);
7711
7712 if (height)
7713 {
7714 freeze_window_starts (f, 1);
7715 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7716 }
7717
7718 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7719 }
7720 }
7721
7722 if (old_current_buffer)
7723 set_buffer_internal (old_current_buffer);
7724 }
7725
7726 return window_height_changed_p;
7727 }
7728
7729
7730 /* Value is the current message, a string, or nil if there is no
7731 current message. */
7732
7733 Lisp_Object
7734 current_message ()
7735 {
7736 Lisp_Object msg;
7737
7738 if (NILP (echo_area_buffer[0]))
7739 msg = Qnil;
7740 else
7741 {
7742 with_echo_area_buffer (0, 0, current_message_1,
7743 (EMACS_INT) &msg, Qnil, 0, 0);
7744 if (NILP (msg))
7745 echo_area_buffer[0] = Qnil;
7746 }
7747
7748 return msg;
7749 }
7750
7751
7752 static int
7753 current_message_1 (a1, a2, a3, a4)
7754 EMACS_INT a1;
7755 Lisp_Object a2;
7756 EMACS_INT a3, a4;
7757 {
7758 Lisp_Object *msg = (Lisp_Object *) a1;
7759
7760 if (Z > BEG)
7761 *msg = make_buffer_string (BEG, Z, 1);
7762 else
7763 *msg = Qnil;
7764 return 0;
7765 }
7766
7767
7768 /* Push the current message on Vmessage_stack for later restauration
7769 by restore_message. Value is non-zero if the current message isn't
7770 empty. This is a relatively infrequent operation, so it's not
7771 worth optimizing. */
7772
7773 int
7774 push_message ()
7775 {
7776 Lisp_Object msg;
7777 msg = current_message ();
7778 Vmessage_stack = Fcons (msg, Vmessage_stack);
7779 return STRINGP (msg);
7780 }
7781
7782
7783 /* Restore message display from the top of Vmessage_stack. */
7784
7785 void
7786 restore_message ()
7787 {
7788 Lisp_Object msg;
7789
7790 xassert (CONSP (Vmessage_stack));
7791 msg = XCAR (Vmessage_stack);
7792 if (STRINGP (msg))
7793 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7794 else
7795 message3_nolog (msg, 0, 0);
7796 }
7797
7798
7799 /* Handler for record_unwind_protect calling pop_message. */
7800
7801 Lisp_Object
7802 pop_message_unwind (dummy)
7803 Lisp_Object dummy;
7804 {
7805 pop_message ();
7806 return Qnil;
7807 }
7808
7809 /* Pop the top-most entry off Vmessage_stack. */
7810
7811 void
7812 pop_message ()
7813 {
7814 xassert (CONSP (Vmessage_stack));
7815 Vmessage_stack = XCDR (Vmessage_stack);
7816 }
7817
7818
7819 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7820 exits. If the stack is not empty, we have a missing pop_message
7821 somewhere. */
7822
7823 void
7824 check_message_stack ()
7825 {
7826 if (!NILP (Vmessage_stack))
7827 abort ();
7828 }
7829
7830
7831 /* Truncate to NCHARS what will be displayed in the echo area the next
7832 time we display it---but don't redisplay it now. */
7833
7834 void
7835 truncate_echo_area (nchars)
7836 int nchars;
7837 {
7838 if (nchars == 0)
7839 echo_area_buffer[0] = Qnil;
7840 /* A null message buffer means that the frame hasn't really been
7841 initialized yet. Error messages get reported properly by
7842 cmd_error, so this must be just an informative message; toss it. */
7843 else if (!noninteractive
7844 && INTERACTIVE
7845 && !NILP (echo_area_buffer[0]))
7846 {
7847 struct frame *sf = SELECTED_FRAME ();
7848 if (FRAME_MESSAGE_BUF (sf))
7849 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7850 }
7851 }
7852
7853
7854 /* Helper function for truncate_echo_area. Truncate the current
7855 message to at most NCHARS characters. */
7856
7857 static int
7858 truncate_message_1 (nchars, a2, a3, a4)
7859 EMACS_INT nchars;
7860 Lisp_Object a2;
7861 EMACS_INT a3, a4;
7862 {
7863 if (BEG + nchars < Z)
7864 del_range (BEG + nchars, Z);
7865 if (Z == BEG)
7866 echo_area_buffer[0] = Qnil;
7867 return 0;
7868 }
7869
7870
7871 /* Set the current message to a substring of S or STRING.
7872
7873 If STRING is a Lisp string, set the message to the first NBYTES
7874 bytes from STRING. NBYTES zero means use the whole string. If
7875 STRING is multibyte, the message will be displayed multibyte.
7876
7877 If S is not null, set the message to the first LEN bytes of S. LEN
7878 zero means use the whole string. MULTIBYTE_P non-zero means S is
7879 multibyte. Display the message multibyte in that case. */
7880
7881 void
7882 set_message (s, string, nbytes, multibyte_p)
7883 const char *s;
7884 Lisp_Object string;
7885 int nbytes, multibyte_p;
7886 {
7887 message_enable_multibyte
7888 = ((s && multibyte_p)
7889 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7890
7891 with_echo_area_buffer (0, 0, set_message_1,
7892 (EMACS_INT) s, string, nbytes, multibyte_p);
7893 message_buf_print = 0;
7894 help_echo_showing_p = 0;
7895 }
7896
7897
7898 /* Helper function for set_message. Arguments have the same meaning
7899 as there, with A1 corresponding to S and A2 corresponding to STRING
7900 This function is called with the echo area buffer being
7901 current. */
7902
7903 static int
7904 set_message_1 (a1, a2, nbytes, multibyte_p)
7905 EMACS_INT a1;
7906 Lisp_Object a2;
7907 EMACS_INT nbytes, multibyte_p;
7908 {
7909 const char *s = (const char *) a1;
7910 Lisp_Object string = a2;
7911
7912 /* Change multibyteness of the echo buffer appropriately. */
7913 if (message_enable_multibyte
7914 != !NILP (current_buffer->enable_multibyte_characters))
7915 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7916
7917 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7918
7919 /* Insert new message at BEG. */
7920 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7921 Ferase_buffer ();
7922
7923 if (STRINGP (string))
7924 {
7925 int nchars;
7926
7927 if (nbytes == 0)
7928 nbytes = SBYTES (string);
7929 nchars = string_byte_to_char (string, nbytes);
7930
7931 /* This function takes care of single/multibyte conversion. We
7932 just have to ensure that the echo area buffer has the right
7933 setting of enable_multibyte_characters. */
7934 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7935 }
7936 else if (s)
7937 {
7938 if (nbytes == 0)
7939 nbytes = strlen (s);
7940
7941 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7942 {
7943 /* Convert from multi-byte to single-byte. */
7944 int i, c, n;
7945 unsigned char work[1];
7946
7947 /* Convert a multibyte string to single-byte. */
7948 for (i = 0; i < nbytes; i += n)
7949 {
7950 c = string_char_and_length (s + i, nbytes - i, &n);
7951 work[0] = (SINGLE_BYTE_CHAR_P (c)
7952 ? c
7953 : multibyte_char_to_unibyte (c, Qnil));
7954 insert_1_both (work, 1, 1, 1, 0, 0);
7955 }
7956 }
7957 else if (!multibyte_p
7958 && !NILP (current_buffer->enable_multibyte_characters))
7959 {
7960 /* Convert from single-byte to multi-byte. */
7961 int i, c, n;
7962 const unsigned char *msg = (const unsigned char *) s;
7963 unsigned char str[MAX_MULTIBYTE_LENGTH];
7964
7965 /* Convert a single-byte string to multibyte. */
7966 for (i = 0; i < nbytes; i++)
7967 {
7968 c = unibyte_char_to_multibyte (msg[i]);
7969 n = CHAR_STRING (c, str);
7970 insert_1_both (str, 1, n, 1, 0, 0);
7971 }
7972 }
7973 else
7974 insert_1 (s, nbytes, 1, 0, 0);
7975 }
7976
7977 return 0;
7978 }
7979
7980
7981 /* Clear messages. CURRENT_P non-zero means clear the current
7982 message. LAST_DISPLAYED_P non-zero means clear the message
7983 last displayed. */
7984
7985 void
7986 clear_message (current_p, last_displayed_p)
7987 int current_p, last_displayed_p;
7988 {
7989 if (current_p)
7990 {
7991 echo_area_buffer[0] = Qnil;
7992 message_cleared_p = 1;
7993 }
7994
7995 if (last_displayed_p)
7996 echo_area_buffer[1] = Qnil;
7997
7998 message_buf_print = 0;
7999 }
8000
8001 /* Clear garbaged frames.
8002
8003 This function is used where the old redisplay called
8004 redraw_garbaged_frames which in turn called redraw_frame which in
8005 turn called clear_frame. The call to clear_frame was a source of
8006 flickering. I believe a clear_frame is not necessary. It should
8007 suffice in the new redisplay to invalidate all current matrices,
8008 and ensure a complete redisplay of all windows. */
8009
8010 static void
8011 clear_garbaged_frames ()
8012 {
8013 if (frame_garbaged)
8014 {
8015 Lisp_Object tail, frame;
8016 int changed_count = 0;
8017
8018 FOR_EACH_FRAME (tail, frame)
8019 {
8020 struct frame *f = XFRAME (frame);
8021
8022 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8023 {
8024 if (f->resized_p)
8025 {
8026 Fredraw_frame (frame);
8027 f->force_flush_display_p = 1;
8028 }
8029 clear_current_matrices (f);
8030 changed_count++;
8031 f->garbaged = 0;
8032 f->resized_p = 0;
8033 }
8034 }
8035
8036 frame_garbaged = 0;
8037 if (changed_count)
8038 ++windows_or_buffers_changed;
8039 }
8040 }
8041
8042
8043 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8044 is non-zero update selected_frame. Value is non-zero if the
8045 mini-windows height has been changed. */
8046
8047 static int
8048 echo_area_display (update_frame_p)
8049 int update_frame_p;
8050 {
8051 Lisp_Object mini_window;
8052 struct window *w;
8053 struct frame *f;
8054 int window_height_changed_p = 0;
8055 struct frame *sf = SELECTED_FRAME ();
8056
8057 mini_window = FRAME_MINIBUF_WINDOW (sf);
8058 w = XWINDOW (mini_window);
8059 f = XFRAME (WINDOW_FRAME (w));
8060
8061 /* Don't display if frame is invisible or not yet initialized. */
8062 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8063 return 0;
8064
8065 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8066 #ifndef MAC_OS8
8067 #ifdef HAVE_WINDOW_SYSTEM
8068 /* When Emacs starts, selected_frame may be a visible terminal
8069 frame, even if we run under a window system. If we let this
8070 through, a message would be displayed on the terminal. */
8071 if (EQ (selected_frame, Vterminal_frame)
8072 && !NILP (Vwindow_system))
8073 return 0;
8074 #endif /* HAVE_WINDOW_SYSTEM */
8075 #endif
8076
8077 /* Redraw garbaged frames. */
8078 if (frame_garbaged)
8079 clear_garbaged_frames ();
8080
8081 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8082 {
8083 echo_area_window = mini_window;
8084 window_height_changed_p = display_echo_area (w);
8085 w->must_be_updated_p = 1;
8086
8087 /* Update the display, unless called from redisplay_internal.
8088 Also don't update the screen during redisplay itself. The
8089 update will happen at the end of redisplay, and an update
8090 here could cause confusion. */
8091 if (update_frame_p && !redisplaying_p)
8092 {
8093 int n = 0;
8094
8095 /* If the display update has been interrupted by pending
8096 input, update mode lines in the frame. Due to the
8097 pending input, it might have been that redisplay hasn't
8098 been called, so that mode lines above the echo area are
8099 garbaged. This looks odd, so we prevent it here. */
8100 if (!display_completed)
8101 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8102
8103 if (window_height_changed_p
8104 /* Don't do this if Emacs is shutting down. Redisplay
8105 needs to run hooks. */
8106 && !NILP (Vrun_hooks))
8107 {
8108 /* Must update other windows. Likewise as in other
8109 cases, don't let this update be interrupted by
8110 pending input. */
8111 int count = SPECPDL_INDEX ();
8112 specbind (Qredisplay_dont_pause, Qt);
8113 windows_or_buffers_changed = 1;
8114 redisplay_internal (0);
8115 unbind_to (count, Qnil);
8116 }
8117 else if (FRAME_WINDOW_P (f) && n == 0)
8118 {
8119 /* Window configuration is the same as before.
8120 Can do with a display update of the echo area,
8121 unless we displayed some mode lines. */
8122 update_single_window (w, 1);
8123 rif->flush_display (f);
8124 }
8125 else
8126 update_frame (f, 1, 1);
8127
8128 /* If cursor is in the echo area, make sure that the next
8129 redisplay displays the minibuffer, so that the cursor will
8130 be replaced with what the minibuffer wants. */
8131 if (cursor_in_echo_area)
8132 ++windows_or_buffers_changed;
8133 }
8134 }
8135 else if (!EQ (mini_window, selected_window))
8136 windows_or_buffers_changed++;
8137
8138 /* The current message is now also the last one displayed. */
8139 echo_area_buffer[1] = echo_area_buffer[0];
8140
8141 /* Prevent redisplay optimization in redisplay_internal by resetting
8142 this_line_start_pos. This is done because the mini-buffer now
8143 displays the message instead of its buffer text. */
8144 if (EQ (mini_window, selected_window))
8145 CHARPOS (this_line_start_pos) = 0;
8146
8147 return window_height_changed_p;
8148 }
8149
8150
8151 \f
8152 /***********************************************************************
8153 Frame Titles
8154 ***********************************************************************/
8155
8156
8157 /* The frame title buffering code is also used by Fformat_mode_line.
8158 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8159
8160 /* A buffer for constructing frame titles in it; allocated from the
8161 heap in init_xdisp and resized as needed in store_frame_title_char. */
8162
8163 static char *frame_title_buf;
8164
8165 /* The buffer's end, and a current output position in it. */
8166
8167 static char *frame_title_buf_end;
8168 static char *frame_title_ptr;
8169
8170
8171 /* Store a single character C for the frame title in frame_title_buf.
8172 Re-allocate frame_title_buf if necessary. */
8173
8174 static void
8175 #ifdef PROTOTYPES
8176 store_frame_title_char (char c)
8177 #else
8178 store_frame_title_char (c)
8179 char c;
8180 #endif
8181 {
8182 /* If output position has reached the end of the allocated buffer,
8183 double the buffer's size. */
8184 if (frame_title_ptr == frame_title_buf_end)
8185 {
8186 int len = frame_title_ptr - frame_title_buf;
8187 int new_size = 2 * len * sizeof *frame_title_buf;
8188 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8189 frame_title_buf_end = frame_title_buf + new_size;
8190 frame_title_ptr = frame_title_buf + len;
8191 }
8192
8193 *frame_title_ptr++ = c;
8194 }
8195
8196
8197 /* Store part of a frame title in frame_title_buf, beginning at
8198 frame_title_ptr. STR is the string to store. Do not copy
8199 characters that yield more columns than PRECISION; PRECISION <= 0
8200 means copy the whole string. Pad with spaces until FIELD_WIDTH
8201 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8202 pad. Called from display_mode_element when it is used to build a
8203 frame title. */
8204
8205 static int
8206 store_frame_title (str, field_width, precision)
8207 const unsigned char *str;
8208 int field_width, precision;
8209 {
8210 int n = 0;
8211 int dummy, nbytes;
8212
8213 /* Copy at most PRECISION chars from STR. */
8214 nbytes = strlen (str);
8215 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8216 while (nbytes--)
8217 store_frame_title_char (*str++);
8218
8219 /* Fill up with spaces until FIELD_WIDTH reached. */
8220 while (field_width > 0
8221 && n < field_width)
8222 {
8223 store_frame_title_char (' ');
8224 ++n;
8225 }
8226
8227 return n;
8228 }
8229
8230 #ifdef HAVE_WINDOW_SYSTEM
8231
8232 /* Set the title of FRAME, if it has changed. The title format is
8233 Vicon_title_format if FRAME is iconified, otherwise it is
8234 frame_title_format. */
8235
8236 static void
8237 x_consider_frame_title (frame)
8238 Lisp_Object frame;
8239 {
8240 struct frame *f = XFRAME (frame);
8241
8242 if (FRAME_WINDOW_P (f)
8243 || FRAME_MINIBUF_ONLY_P (f)
8244 || f->explicit_name)
8245 {
8246 /* Do we have more than one visible frame on this X display? */
8247 Lisp_Object tail;
8248 Lisp_Object fmt;
8249 struct buffer *obuf;
8250 int len;
8251 struct it it;
8252
8253 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8254 {
8255 Lisp_Object other_frame = XCAR (tail);
8256 struct frame *tf = XFRAME (other_frame);
8257
8258 if (tf != f
8259 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8260 && !FRAME_MINIBUF_ONLY_P (tf)
8261 && !EQ (other_frame, tip_frame)
8262 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8263 break;
8264 }
8265
8266 /* Set global variable indicating that multiple frames exist. */
8267 multiple_frames = CONSP (tail);
8268
8269 /* Switch to the buffer of selected window of the frame. Set up
8270 frame_title_ptr so that display_mode_element will output into it;
8271 then display the title. */
8272 obuf = current_buffer;
8273 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8274 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8275 frame_title_ptr = frame_title_buf;
8276 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8277 NULL, DEFAULT_FACE_ID);
8278 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8279 len = frame_title_ptr - frame_title_buf;
8280 frame_title_ptr = NULL;
8281 set_buffer_internal_1 (obuf);
8282
8283 /* Set the title only if it's changed. This avoids consing in
8284 the common case where it hasn't. (If it turns out that we've
8285 already wasted too much time by walking through the list with
8286 display_mode_element, then we might need to optimize at a
8287 higher level than this.) */
8288 if (! STRINGP (f->name)
8289 || SBYTES (f->name) != len
8290 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8291 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8292 }
8293 }
8294
8295 #endif /* not HAVE_WINDOW_SYSTEM */
8296
8297
8298
8299 \f
8300 /***********************************************************************
8301 Menu Bars
8302 ***********************************************************************/
8303
8304
8305 /* Prepare for redisplay by updating menu-bar item lists when
8306 appropriate. This can call eval. */
8307
8308 void
8309 prepare_menu_bars ()
8310 {
8311 int all_windows;
8312 struct gcpro gcpro1, gcpro2;
8313 struct frame *f;
8314 Lisp_Object tooltip_frame;
8315
8316 #ifdef HAVE_WINDOW_SYSTEM
8317 tooltip_frame = tip_frame;
8318 #else
8319 tooltip_frame = Qnil;
8320 #endif
8321
8322 /* Update all frame titles based on their buffer names, etc. We do
8323 this before the menu bars so that the buffer-menu will show the
8324 up-to-date frame titles. */
8325 #ifdef HAVE_WINDOW_SYSTEM
8326 if (windows_or_buffers_changed || update_mode_lines)
8327 {
8328 Lisp_Object tail, frame;
8329
8330 FOR_EACH_FRAME (tail, frame)
8331 {
8332 f = XFRAME (frame);
8333 if (!EQ (frame, tooltip_frame)
8334 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8335 x_consider_frame_title (frame);
8336 }
8337 }
8338 #endif /* HAVE_WINDOW_SYSTEM */
8339
8340 /* Update the menu bar item lists, if appropriate. This has to be
8341 done before any actual redisplay or generation of display lines. */
8342 all_windows = (update_mode_lines
8343 || buffer_shared > 1
8344 || windows_or_buffers_changed);
8345 if (all_windows)
8346 {
8347 Lisp_Object tail, frame;
8348 int count = SPECPDL_INDEX ();
8349
8350 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8351
8352 FOR_EACH_FRAME (tail, frame)
8353 {
8354 f = XFRAME (frame);
8355
8356 /* Ignore tooltip frame. */
8357 if (EQ (frame, tooltip_frame))
8358 continue;
8359
8360 /* If a window on this frame changed size, report that to
8361 the user and clear the size-change flag. */
8362 if (FRAME_WINDOW_SIZES_CHANGED (f))
8363 {
8364 Lisp_Object functions;
8365
8366 /* Clear flag first in case we get an error below. */
8367 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8368 functions = Vwindow_size_change_functions;
8369 GCPRO2 (tail, functions);
8370
8371 while (CONSP (functions))
8372 {
8373 call1 (XCAR (functions), frame);
8374 functions = XCDR (functions);
8375 }
8376 UNGCPRO;
8377 }
8378
8379 GCPRO1 (tail);
8380 update_menu_bar (f, 0);
8381 #ifdef HAVE_WINDOW_SYSTEM
8382 update_tool_bar (f, 0);
8383 #endif
8384 UNGCPRO;
8385 }
8386
8387 unbind_to (count, Qnil);
8388 }
8389 else
8390 {
8391 struct frame *sf = SELECTED_FRAME ();
8392 update_menu_bar (sf, 1);
8393 #ifdef HAVE_WINDOW_SYSTEM
8394 update_tool_bar (sf, 1);
8395 #endif
8396 }
8397
8398 /* Motif needs this. See comment in xmenu.c. Turn it off when
8399 pending_menu_activation is not defined. */
8400 #ifdef USE_X_TOOLKIT
8401 pending_menu_activation = 0;
8402 #endif
8403 }
8404
8405
8406 /* Update the menu bar item list for frame F. This has to be done
8407 before we start to fill in any display lines, because it can call
8408 eval.
8409
8410 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8411
8412 static void
8413 update_menu_bar (f, save_match_data)
8414 struct frame *f;
8415 int save_match_data;
8416 {
8417 Lisp_Object window;
8418 register struct window *w;
8419
8420 /* If called recursively during a menu update, do nothing. This can
8421 happen when, for instance, an activate-menubar-hook causes a
8422 redisplay. */
8423 if (inhibit_menubar_update)
8424 return;
8425
8426 window = FRAME_SELECTED_WINDOW (f);
8427 w = XWINDOW (window);
8428
8429 #if 0 /* The if statement below this if statement used to include the
8430 condition !NILP (w->update_mode_line), rather than using
8431 update_mode_lines directly, and this if statement may have
8432 been added to make that condition work. Now the if
8433 statement below matches its comment, this isn't needed. */
8434 if (update_mode_lines)
8435 w->update_mode_line = Qt;
8436 #endif
8437
8438 if (FRAME_WINDOW_P (f)
8439 ?
8440 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8441 || defined (USE_GTK)
8442 FRAME_EXTERNAL_MENU_BAR (f)
8443 #else
8444 FRAME_MENU_BAR_LINES (f) > 0
8445 #endif
8446 : FRAME_MENU_BAR_LINES (f) > 0)
8447 {
8448 /* If the user has switched buffers or windows, we need to
8449 recompute to reflect the new bindings. But we'll
8450 recompute when update_mode_lines is set too; that means
8451 that people can use force-mode-line-update to request
8452 that the menu bar be recomputed. The adverse effect on
8453 the rest of the redisplay algorithm is about the same as
8454 windows_or_buffers_changed anyway. */
8455 if (windows_or_buffers_changed
8456 /* This used to test w->update_mode_line, but we believe
8457 there is no need to recompute the menu in that case. */
8458 || update_mode_lines
8459 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8460 < BUF_MODIFF (XBUFFER (w->buffer)))
8461 != !NILP (w->last_had_star))
8462 || ((!NILP (Vtransient_mark_mode)
8463 && !NILP (XBUFFER (w->buffer)->mark_active))
8464 != !NILP (w->region_showing)))
8465 {
8466 struct buffer *prev = current_buffer;
8467 int count = SPECPDL_INDEX ();
8468
8469 specbind (Qinhibit_menubar_update, Qt);
8470
8471 set_buffer_internal_1 (XBUFFER (w->buffer));
8472 if (save_match_data)
8473 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8474 if (NILP (Voverriding_local_map_menu_flag))
8475 {
8476 specbind (Qoverriding_terminal_local_map, Qnil);
8477 specbind (Qoverriding_local_map, Qnil);
8478 }
8479
8480 /* Run the Lucid hook. */
8481 safe_run_hooks (Qactivate_menubar_hook);
8482
8483 /* If it has changed current-menubar from previous value,
8484 really recompute the menu-bar from the value. */
8485 if (! NILP (Vlucid_menu_bar_dirty_flag))
8486 call0 (Qrecompute_lucid_menubar);
8487
8488 safe_run_hooks (Qmenu_bar_update_hook);
8489 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8490
8491 /* Redisplay the menu bar in case we changed it. */
8492 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8493 || defined (USE_GTK)
8494 if (FRAME_WINDOW_P (f)
8495 #if defined (MAC_OS)
8496 /* All frames on Mac OS share the same menubar. So only the
8497 selected frame should be allowed to set it. */
8498 && f == SELECTED_FRAME ()
8499 #endif
8500 )
8501 set_frame_menubar (f, 0, 0);
8502 else
8503 /* On a terminal screen, the menu bar is an ordinary screen
8504 line, and this makes it get updated. */
8505 w->update_mode_line = Qt;
8506 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8507 /* In the non-toolkit version, the menu bar is an ordinary screen
8508 line, and this makes it get updated. */
8509 w->update_mode_line = Qt;
8510 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8511
8512 unbind_to (count, Qnil);
8513 set_buffer_internal_1 (prev);
8514 }
8515 }
8516 }
8517
8518
8519 \f
8520 /***********************************************************************
8521 Output Cursor
8522 ***********************************************************************/
8523
8524 #ifdef HAVE_WINDOW_SYSTEM
8525
8526 /* EXPORT:
8527 Nominal cursor position -- where to draw output.
8528 HPOS and VPOS are window relative glyph matrix coordinates.
8529 X and Y are window relative pixel coordinates. */
8530
8531 struct cursor_pos output_cursor;
8532
8533
8534 /* EXPORT:
8535 Set the global variable output_cursor to CURSOR. All cursor
8536 positions are relative to updated_window. */
8537
8538 void
8539 set_output_cursor (cursor)
8540 struct cursor_pos *cursor;
8541 {
8542 output_cursor.hpos = cursor->hpos;
8543 output_cursor.vpos = cursor->vpos;
8544 output_cursor.x = cursor->x;
8545 output_cursor.y = cursor->y;
8546 }
8547
8548
8549 /* EXPORT for RIF:
8550 Set a nominal cursor position.
8551
8552 HPOS and VPOS are column/row positions in a window glyph matrix. X
8553 and Y are window text area relative pixel positions.
8554
8555 If this is done during an update, updated_window will contain the
8556 window that is being updated and the position is the future output
8557 cursor position for that window. If updated_window is null, use
8558 selected_window and display the cursor at the given position. */
8559
8560 void
8561 x_cursor_to (vpos, hpos, y, x)
8562 int vpos, hpos, y, x;
8563 {
8564 struct window *w;
8565
8566 /* If updated_window is not set, work on selected_window. */
8567 if (updated_window)
8568 w = updated_window;
8569 else
8570 w = XWINDOW (selected_window);
8571
8572 /* Set the output cursor. */
8573 output_cursor.hpos = hpos;
8574 output_cursor.vpos = vpos;
8575 output_cursor.x = x;
8576 output_cursor.y = y;
8577
8578 /* If not called as part of an update, really display the cursor.
8579 This will also set the cursor position of W. */
8580 if (updated_window == NULL)
8581 {
8582 BLOCK_INPUT;
8583 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8584 if (rif->flush_display_optional)
8585 rif->flush_display_optional (SELECTED_FRAME ());
8586 UNBLOCK_INPUT;
8587 }
8588 }
8589
8590 #endif /* HAVE_WINDOW_SYSTEM */
8591
8592 \f
8593 /***********************************************************************
8594 Tool-bars
8595 ***********************************************************************/
8596
8597 #ifdef HAVE_WINDOW_SYSTEM
8598
8599 /* Where the mouse was last time we reported a mouse event. */
8600
8601 FRAME_PTR last_mouse_frame;
8602
8603 /* Tool-bar item index of the item on which a mouse button was pressed
8604 or -1. */
8605
8606 int last_tool_bar_item;
8607
8608
8609 /* Update the tool-bar item list for frame F. This has to be done
8610 before we start to fill in any display lines. Called from
8611 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8612 and restore it here. */
8613
8614 static void
8615 update_tool_bar (f, save_match_data)
8616 struct frame *f;
8617 int save_match_data;
8618 {
8619 #ifdef USE_GTK
8620 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8621 #else
8622 int do_update = WINDOWP (f->tool_bar_window)
8623 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8624 #endif
8625
8626 if (do_update)
8627 {
8628 Lisp_Object window;
8629 struct window *w;
8630
8631 window = FRAME_SELECTED_WINDOW (f);
8632 w = XWINDOW (window);
8633
8634 /* If the user has switched buffers or windows, we need to
8635 recompute to reflect the new bindings. But we'll
8636 recompute when update_mode_lines is set too; that means
8637 that people can use force-mode-line-update to request
8638 that the menu bar be recomputed. The adverse effect on
8639 the rest of the redisplay algorithm is about the same as
8640 windows_or_buffers_changed anyway. */
8641 if (windows_or_buffers_changed
8642 || !NILP (w->update_mode_line)
8643 || update_mode_lines
8644 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8645 < BUF_MODIFF (XBUFFER (w->buffer)))
8646 != !NILP (w->last_had_star))
8647 || ((!NILP (Vtransient_mark_mode)
8648 && !NILP (XBUFFER (w->buffer)->mark_active))
8649 != !NILP (w->region_showing)))
8650 {
8651 struct buffer *prev = current_buffer;
8652 int count = SPECPDL_INDEX ();
8653 Lisp_Object new_tool_bar;
8654 int new_n_tool_bar;
8655 struct gcpro gcpro1;
8656
8657 /* Set current_buffer to the buffer of the selected
8658 window of the frame, so that we get the right local
8659 keymaps. */
8660 set_buffer_internal_1 (XBUFFER (w->buffer));
8661
8662 /* Save match data, if we must. */
8663 if (save_match_data)
8664 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8665
8666 /* Make sure that we don't accidentally use bogus keymaps. */
8667 if (NILP (Voverriding_local_map_menu_flag))
8668 {
8669 specbind (Qoverriding_terminal_local_map, Qnil);
8670 specbind (Qoverriding_local_map, Qnil);
8671 }
8672
8673 GCPRO1 (new_tool_bar);
8674
8675 /* Build desired tool-bar items from keymaps. */
8676 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8677 &new_n_tool_bar);
8678
8679 /* Redisplay the tool-bar if we changed it. */
8680 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8681 {
8682 /* Redisplay that happens asynchronously due to an expose event
8683 may access f->tool_bar_items. Make sure we update both
8684 variables within BLOCK_INPUT so no such event interrupts. */
8685 BLOCK_INPUT;
8686 f->tool_bar_items = new_tool_bar;
8687 f->n_tool_bar_items = new_n_tool_bar;
8688 w->update_mode_line = Qt;
8689 UNBLOCK_INPUT;
8690 }
8691
8692 UNGCPRO;
8693
8694 unbind_to (count, Qnil);
8695 set_buffer_internal_1 (prev);
8696 }
8697 }
8698 }
8699
8700
8701 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8702 F's desired tool-bar contents. F->tool_bar_items must have
8703 been set up previously by calling prepare_menu_bars. */
8704
8705 static void
8706 build_desired_tool_bar_string (f)
8707 struct frame *f;
8708 {
8709 int i, size, size_needed;
8710 struct gcpro gcpro1, gcpro2, gcpro3;
8711 Lisp_Object image, plist, props;
8712
8713 image = plist = props = Qnil;
8714 GCPRO3 (image, plist, props);
8715
8716 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8717 Otherwise, make a new string. */
8718
8719 /* The size of the string we might be able to reuse. */
8720 size = (STRINGP (f->desired_tool_bar_string)
8721 ? SCHARS (f->desired_tool_bar_string)
8722 : 0);
8723
8724 /* We need one space in the string for each image. */
8725 size_needed = f->n_tool_bar_items;
8726
8727 /* Reuse f->desired_tool_bar_string, if possible. */
8728 if (size < size_needed || NILP (f->desired_tool_bar_string))
8729 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8730 make_number (' '));
8731 else
8732 {
8733 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8734 Fremove_text_properties (make_number (0), make_number (size),
8735 props, f->desired_tool_bar_string);
8736 }
8737
8738 /* Put a `display' property on the string for the images to display,
8739 put a `menu_item' property on tool-bar items with a value that
8740 is the index of the item in F's tool-bar item vector. */
8741 for (i = 0; i < f->n_tool_bar_items; ++i)
8742 {
8743 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8744
8745 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8746 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8747 int hmargin, vmargin, relief, idx, end;
8748 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8749
8750 /* If image is a vector, choose the image according to the
8751 button state. */
8752 image = PROP (TOOL_BAR_ITEM_IMAGES);
8753 if (VECTORP (image))
8754 {
8755 if (enabled_p)
8756 idx = (selected_p
8757 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8758 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8759 else
8760 idx = (selected_p
8761 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8762 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8763
8764 xassert (ASIZE (image) >= idx);
8765 image = AREF (image, idx);
8766 }
8767 else
8768 idx = -1;
8769
8770 /* Ignore invalid image specifications. */
8771 if (!valid_image_p (image))
8772 continue;
8773
8774 /* Display the tool-bar button pressed, or depressed. */
8775 plist = Fcopy_sequence (XCDR (image));
8776
8777 /* Compute margin and relief to draw. */
8778 relief = (tool_bar_button_relief >= 0
8779 ? tool_bar_button_relief
8780 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8781 hmargin = vmargin = relief;
8782
8783 if (INTEGERP (Vtool_bar_button_margin)
8784 && XINT (Vtool_bar_button_margin) > 0)
8785 {
8786 hmargin += XFASTINT (Vtool_bar_button_margin);
8787 vmargin += XFASTINT (Vtool_bar_button_margin);
8788 }
8789 else if (CONSP (Vtool_bar_button_margin))
8790 {
8791 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8792 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8793 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8794
8795 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8796 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8797 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8798 }
8799
8800 if (auto_raise_tool_bar_buttons_p)
8801 {
8802 /* Add a `:relief' property to the image spec if the item is
8803 selected. */
8804 if (selected_p)
8805 {
8806 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8807 hmargin -= relief;
8808 vmargin -= relief;
8809 }
8810 }
8811 else
8812 {
8813 /* If image is selected, display it pressed, i.e. with a
8814 negative relief. If it's not selected, display it with a
8815 raised relief. */
8816 plist = Fplist_put (plist, QCrelief,
8817 (selected_p
8818 ? make_number (-relief)
8819 : make_number (relief)));
8820 hmargin -= relief;
8821 vmargin -= relief;
8822 }
8823
8824 /* Put a margin around the image. */
8825 if (hmargin || vmargin)
8826 {
8827 if (hmargin == vmargin)
8828 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8829 else
8830 plist = Fplist_put (plist, QCmargin,
8831 Fcons (make_number (hmargin),
8832 make_number (vmargin)));
8833 }
8834
8835 /* If button is not enabled, and we don't have special images
8836 for the disabled state, make the image appear disabled by
8837 applying an appropriate algorithm to it. */
8838 if (!enabled_p && idx < 0)
8839 plist = Fplist_put (plist, QCconversion, Qdisabled);
8840
8841 /* Put a `display' text property on the string for the image to
8842 display. Put a `menu-item' property on the string that gives
8843 the start of this item's properties in the tool-bar items
8844 vector. */
8845 image = Fcons (Qimage, plist);
8846 props = list4 (Qdisplay, image,
8847 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8848
8849 /* Let the last image hide all remaining spaces in the tool bar
8850 string. The string can be longer than needed when we reuse a
8851 previous string. */
8852 if (i + 1 == f->n_tool_bar_items)
8853 end = SCHARS (f->desired_tool_bar_string);
8854 else
8855 end = i + 1;
8856 Fadd_text_properties (make_number (i), make_number (end),
8857 props, f->desired_tool_bar_string);
8858 #undef PROP
8859 }
8860
8861 UNGCPRO;
8862 }
8863
8864
8865 /* Display one line of the tool-bar of frame IT->f. */
8866
8867 static void
8868 display_tool_bar_line (it)
8869 struct it *it;
8870 {
8871 struct glyph_row *row = it->glyph_row;
8872 int max_x = it->last_visible_x;
8873 struct glyph *last;
8874
8875 prepare_desired_row (row);
8876 row->y = it->current_y;
8877
8878 /* Note that this isn't made use of if the face hasn't a box,
8879 so there's no need to check the face here. */
8880 it->start_of_box_run_p = 1;
8881
8882 while (it->current_x < max_x)
8883 {
8884 int x_before, x, n_glyphs_before, i, nglyphs;
8885
8886 /* Get the next display element. */
8887 if (!get_next_display_element (it))
8888 break;
8889
8890 /* Produce glyphs. */
8891 x_before = it->current_x;
8892 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8893 PRODUCE_GLYPHS (it);
8894
8895 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8896 i = 0;
8897 x = x_before;
8898 while (i < nglyphs)
8899 {
8900 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8901
8902 if (x + glyph->pixel_width > max_x)
8903 {
8904 /* Glyph doesn't fit on line. */
8905 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8906 it->current_x = x;
8907 goto out;
8908 }
8909
8910 ++it->hpos;
8911 x += glyph->pixel_width;
8912 ++i;
8913 }
8914
8915 /* Stop at line ends. */
8916 if (ITERATOR_AT_END_OF_LINE_P (it))
8917 break;
8918
8919 set_iterator_to_next (it, 1);
8920 }
8921
8922 out:;
8923
8924 row->displays_text_p = row->used[TEXT_AREA] != 0;
8925 extend_face_to_end_of_line (it);
8926 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8927 last->right_box_line_p = 1;
8928 if (last == row->glyphs[TEXT_AREA])
8929 last->left_box_line_p = 1;
8930 compute_line_metrics (it);
8931
8932 /* If line is empty, make it occupy the rest of the tool-bar. */
8933 if (!row->displays_text_p)
8934 {
8935 row->height = row->phys_height = it->last_visible_y - row->y;
8936 row->ascent = row->phys_ascent = 0;
8937 row->extra_line_spacing = 0;
8938 }
8939
8940 row->full_width_p = 1;
8941 row->continued_p = 0;
8942 row->truncated_on_left_p = 0;
8943 row->truncated_on_right_p = 0;
8944
8945 it->current_x = it->hpos = 0;
8946 it->current_y += row->height;
8947 ++it->vpos;
8948 ++it->glyph_row;
8949 }
8950
8951
8952 /* Value is the number of screen lines needed to make all tool-bar
8953 items of frame F visible. */
8954
8955 static int
8956 tool_bar_lines_needed (f)
8957 struct frame *f;
8958 {
8959 struct window *w = XWINDOW (f->tool_bar_window);
8960 struct it it;
8961
8962 /* Initialize an iterator for iteration over
8963 F->desired_tool_bar_string in the tool-bar window of frame F. */
8964 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8965 it.first_visible_x = 0;
8966 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8967 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8968
8969 while (!ITERATOR_AT_END_P (&it))
8970 {
8971 it.glyph_row = w->desired_matrix->rows;
8972 clear_glyph_row (it.glyph_row);
8973 display_tool_bar_line (&it);
8974 }
8975
8976 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8977 }
8978
8979
8980 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8981 0, 1, 0,
8982 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8983 (frame)
8984 Lisp_Object frame;
8985 {
8986 struct frame *f;
8987 struct window *w;
8988 int nlines = 0;
8989
8990 if (NILP (frame))
8991 frame = selected_frame;
8992 else
8993 CHECK_FRAME (frame);
8994 f = XFRAME (frame);
8995
8996 if (WINDOWP (f->tool_bar_window)
8997 || (w = XWINDOW (f->tool_bar_window),
8998 WINDOW_TOTAL_LINES (w) > 0))
8999 {
9000 update_tool_bar (f, 1);
9001 if (f->n_tool_bar_items)
9002 {
9003 build_desired_tool_bar_string (f);
9004 nlines = tool_bar_lines_needed (f);
9005 }
9006 }
9007
9008 return make_number (nlines);
9009 }
9010
9011
9012 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9013 height should be changed. */
9014
9015 static int
9016 redisplay_tool_bar (f)
9017 struct frame *f;
9018 {
9019 struct window *w;
9020 struct it it;
9021 struct glyph_row *row;
9022 int change_height_p = 0;
9023
9024 #ifdef USE_GTK
9025 if (FRAME_EXTERNAL_TOOL_BAR (f))
9026 update_frame_tool_bar (f);
9027 return 0;
9028 #endif
9029
9030 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9031 do anything. This means you must start with tool-bar-lines
9032 non-zero to get the auto-sizing effect. Or in other words, you
9033 can turn off tool-bars by specifying tool-bar-lines zero. */
9034 if (!WINDOWP (f->tool_bar_window)
9035 || (w = XWINDOW (f->tool_bar_window),
9036 WINDOW_TOTAL_LINES (w) == 0))
9037 return 0;
9038
9039 /* Set up an iterator for the tool-bar window. */
9040 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9041 it.first_visible_x = 0;
9042 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9043 row = it.glyph_row;
9044
9045 /* Build a string that represents the contents of the tool-bar. */
9046 build_desired_tool_bar_string (f);
9047 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9048
9049 /* Display as many lines as needed to display all tool-bar items. */
9050 while (it.current_y < it.last_visible_y)
9051 display_tool_bar_line (&it);
9052
9053 /* It doesn't make much sense to try scrolling in the tool-bar
9054 window, so don't do it. */
9055 w->desired_matrix->no_scrolling_p = 1;
9056 w->must_be_updated_p = 1;
9057
9058 if (auto_resize_tool_bars_p)
9059 {
9060 int nlines;
9061
9062 /* If we couldn't display everything, change the tool-bar's
9063 height. */
9064 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9065 change_height_p = 1;
9066
9067 /* If there are blank lines at the end, except for a partially
9068 visible blank line at the end that is smaller than
9069 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9070 row = it.glyph_row - 1;
9071 if (!row->displays_text_p
9072 && row->height >= FRAME_LINE_HEIGHT (f))
9073 change_height_p = 1;
9074
9075 /* If row displays tool-bar items, but is partially visible,
9076 change the tool-bar's height. */
9077 if (row->displays_text_p
9078 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9079 change_height_p = 1;
9080
9081 /* Resize windows as needed by changing the `tool-bar-lines'
9082 frame parameter. */
9083 if (change_height_p
9084 && (nlines = tool_bar_lines_needed (f),
9085 nlines != WINDOW_TOTAL_LINES (w)))
9086 {
9087 extern Lisp_Object Qtool_bar_lines;
9088 Lisp_Object frame;
9089 int old_height = WINDOW_TOTAL_LINES (w);
9090
9091 XSETFRAME (frame, f);
9092 clear_glyph_matrix (w->desired_matrix);
9093 Fmodify_frame_parameters (frame,
9094 Fcons (Fcons (Qtool_bar_lines,
9095 make_number (nlines)),
9096 Qnil));
9097 if (WINDOW_TOTAL_LINES (w) != old_height)
9098 fonts_changed_p = 1;
9099 }
9100 }
9101
9102 return change_height_p;
9103 }
9104
9105
9106 /* Get information about the tool-bar item which is displayed in GLYPH
9107 on frame F. Return in *PROP_IDX the index where tool-bar item
9108 properties start in F->tool_bar_items. Value is zero if
9109 GLYPH doesn't display a tool-bar item. */
9110
9111 static int
9112 tool_bar_item_info (f, glyph, prop_idx)
9113 struct frame *f;
9114 struct glyph *glyph;
9115 int *prop_idx;
9116 {
9117 Lisp_Object prop;
9118 int success_p;
9119 int charpos;
9120
9121 /* This function can be called asynchronously, which means we must
9122 exclude any possibility that Fget_text_property signals an
9123 error. */
9124 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9125 charpos = max (0, charpos);
9126
9127 /* Get the text property `menu-item' at pos. The value of that
9128 property is the start index of this item's properties in
9129 F->tool_bar_items. */
9130 prop = Fget_text_property (make_number (charpos),
9131 Qmenu_item, f->current_tool_bar_string);
9132 if (INTEGERP (prop))
9133 {
9134 *prop_idx = XINT (prop);
9135 success_p = 1;
9136 }
9137 else
9138 success_p = 0;
9139
9140 return success_p;
9141 }
9142
9143 \f
9144 /* Get information about the tool-bar item at position X/Y on frame F.
9145 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9146 the current matrix of the tool-bar window of F, or NULL if not
9147 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9148 item in F->tool_bar_items. Value is
9149
9150 -1 if X/Y is not on a tool-bar item
9151 0 if X/Y is on the same item that was highlighted before.
9152 1 otherwise. */
9153
9154 static int
9155 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9156 struct frame *f;
9157 int x, y;
9158 struct glyph **glyph;
9159 int *hpos, *vpos, *prop_idx;
9160 {
9161 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9162 struct window *w = XWINDOW (f->tool_bar_window);
9163 int area;
9164
9165 /* Find the glyph under X/Y. */
9166 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9167 if (*glyph == NULL)
9168 return -1;
9169
9170 /* Get the start of this tool-bar item's properties in
9171 f->tool_bar_items. */
9172 if (!tool_bar_item_info (f, *glyph, prop_idx))
9173 return -1;
9174
9175 /* Is mouse on the highlighted item? */
9176 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9177 && *vpos >= dpyinfo->mouse_face_beg_row
9178 && *vpos <= dpyinfo->mouse_face_end_row
9179 && (*vpos > dpyinfo->mouse_face_beg_row
9180 || *hpos >= dpyinfo->mouse_face_beg_col)
9181 && (*vpos < dpyinfo->mouse_face_end_row
9182 || *hpos < dpyinfo->mouse_face_end_col
9183 || dpyinfo->mouse_face_past_end))
9184 return 0;
9185
9186 return 1;
9187 }
9188
9189
9190 /* EXPORT:
9191 Handle mouse button event on the tool-bar of frame F, at
9192 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9193 0 for button release. MODIFIERS is event modifiers for button
9194 release. */
9195
9196 void
9197 handle_tool_bar_click (f, x, y, down_p, modifiers)
9198 struct frame *f;
9199 int x, y, down_p;
9200 unsigned int modifiers;
9201 {
9202 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9203 struct window *w = XWINDOW (f->tool_bar_window);
9204 int hpos, vpos, prop_idx;
9205 struct glyph *glyph;
9206 Lisp_Object enabled_p;
9207
9208 /* If not on the highlighted tool-bar item, return. */
9209 frame_to_window_pixel_xy (w, &x, &y);
9210 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9211 return;
9212
9213 /* If item is disabled, do nothing. */
9214 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9215 if (NILP (enabled_p))
9216 return;
9217
9218 if (down_p)
9219 {
9220 /* Show item in pressed state. */
9221 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9222 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9223 last_tool_bar_item = prop_idx;
9224 }
9225 else
9226 {
9227 Lisp_Object key, frame;
9228 struct input_event event;
9229 EVENT_INIT (event);
9230
9231 /* Show item in released state. */
9232 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9233 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9234
9235 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9236
9237 XSETFRAME (frame, f);
9238 event.kind = TOOL_BAR_EVENT;
9239 event.frame_or_window = frame;
9240 event.arg = frame;
9241 kbd_buffer_store_event (&event);
9242
9243 event.kind = TOOL_BAR_EVENT;
9244 event.frame_or_window = frame;
9245 event.arg = key;
9246 event.modifiers = modifiers;
9247 kbd_buffer_store_event (&event);
9248 last_tool_bar_item = -1;
9249 }
9250 }
9251
9252
9253 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9254 tool-bar window-relative coordinates X/Y. Called from
9255 note_mouse_highlight. */
9256
9257 static void
9258 note_tool_bar_highlight (f, x, y)
9259 struct frame *f;
9260 int x, y;
9261 {
9262 Lisp_Object window = f->tool_bar_window;
9263 struct window *w = XWINDOW (window);
9264 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9265 int hpos, vpos;
9266 struct glyph *glyph;
9267 struct glyph_row *row;
9268 int i;
9269 Lisp_Object enabled_p;
9270 int prop_idx;
9271 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9272 int mouse_down_p, rc;
9273
9274 /* Function note_mouse_highlight is called with negative x(y
9275 values when mouse moves outside of the frame. */
9276 if (x <= 0 || y <= 0)
9277 {
9278 clear_mouse_face (dpyinfo);
9279 return;
9280 }
9281
9282 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9283 if (rc < 0)
9284 {
9285 /* Not on tool-bar item. */
9286 clear_mouse_face (dpyinfo);
9287 return;
9288 }
9289 else if (rc == 0)
9290 /* On same tool-bar item as before. */
9291 goto set_help_echo;
9292
9293 clear_mouse_face (dpyinfo);
9294
9295 /* Mouse is down, but on different tool-bar item? */
9296 mouse_down_p = (dpyinfo->grabbed
9297 && f == last_mouse_frame
9298 && FRAME_LIVE_P (f));
9299 if (mouse_down_p
9300 && last_tool_bar_item != prop_idx)
9301 return;
9302
9303 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9304 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9305
9306 /* If tool-bar item is not enabled, don't highlight it. */
9307 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9308 if (!NILP (enabled_p))
9309 {
9310 /* Compute the x-position of the glyph. In front and past the
9311 image is a space. We include this in the highlighted area. */
9312 row = MATRIX_ROW (w->current_matrix, vpos);
9313 for (i = x = 0; i < hpos; ++i)
9314 x += row->glyphs[TEXT_AREA][i].pixel_width;
9315
9316 /* Record this as the current active region. */
9317 dpyinfo->mouse_face_beg_col = hpos;
9318 dpyinfo->mouse_face_beg_row = vpos;
9319 dpyinfo->mouse_face_beg_x = x;
9320 dpyinfo->mouse_face_beg_y = row->y;
9321 dpyinfo->mouse_face_past_end = 0;
9322
9323 dpyinfo->mouse_face_end_col = hpos + 1;
9324 dpyinfo->mouse_face_end_row = vpos;
9325 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9326 dpyinfo->mouse_face_end_y = row->y;
9327 dpyinfo->mouse_face_window = window;
9328 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9329
9330 /* Display it as active. */
9331 show_mouse_face (dpyinfo, draw);
9332 dpyinfo->mouse_face_image_state = draw;
9333 }
9334
9335 set_help_echo:
9336
9337 /* Set help_echo_string to a help string to display for this tool-bar item.
9338 XTread_socket does the rest. */
9339 help_echo_object = help_echo_window = Qnil;
9340 help_echo_pos = -1;
9341 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9342 if (NILP (help_echo_string))
9343 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9344 }
9345
9346 #endif /* HAVE_WINDOW_SYSTEM */
9347
9348
9349 \f
9350 /************************************************************************
9351 Horizontal scrolling
9352 ************************************************************************/
9353
9354 static int hscroll_window_tree P_ ((Lisp_Object));
9355 static int hscroll_windows P_ ((Lisp_Object));
9356
9357 /* For all leaf windows in the window tree rooted at WINDOW, set their
9358 hscroll value so that PT is (i) visible in the window, and (ii) so
9359 that it is not within a certain margin at the window's left and
9360 right border. Value is non-zero if any window's hscroll has been
9361 changed. */
9362
9363 static int
9364 hscroll_window_tree (window)
9365 Lisp_Object window;
9366 {
9367 int hscrolled_p = 0;
9368 int hscroll_relative_p = FLOATP (Vhscroll_step);
9369 int hscroll_step_abs = 0;
9370 double hscroll_step_rel = 0;
9371
9372 if (hscroll_relative_p)
9373 {
9374 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9375 if (hscroll_step_rel < 0)
9376 {
9377 hscroll_relative_p = 0;
9378 hscroll_step_abs = 0;
9379 }
9380 }
9381 else if (INTEGERP (Vhscroll_step))
9382 {
9383 hscroll_step_abs = XINT (Vhscroll_step);
9384 if (hscroll_step_abs < 0)
9385 hscroll_step_abs = 0;
9386 }
9387 else
9388 hscroll_step_abs = 0;
9389
9390 while (WINDOWP (window))
9391 {
9392 struct window *w = XWINDOW (window);
9393
9394 if (WINDOWP (w->hchild))
9395 hscrolled_p |= hscroll_window_tree (w->hchild);
9396 else if (WINDOWP (w->vchild))
9397 hscrolled_p |= hscroll_window_tree (w->vchild);
9398 else if (w->cursor.vpos >= 0)
9399 {
9400 int h_margin;
9401 int text_area_width;
9402 struct glyph_row *current_cursor_row
9403 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9404 struct glyph_row *desired_cursor_row
9405 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9406 struct glyph_row *cursor_row
9407 = (desired_cursor_row->enabled_p
9408 ? desired_cursor_row
9409 : current_cursor_row);
9410
9411 text_area_width = window_box_width (w, TEXT_AREA);
9412
9413 /* Scroll when cursor is inside this scroll margin. */
9414 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9415
9416 if ((XFASTINT (w->hscroll)
9417 && w->cursor.x <= h_margin)
9418 || (cursor_row->enabled_p
9419 && cursor_row->truncated_on_right_p
9420 && (w->cursor.x >= text_area_width - h_margin)))
9421 {
9422 struct it it;
9423 int hscroll;
9424 struct buffer *saved_current_buffer;
9425 int pt;
9426 int wanted_x;
9427
9428 /* Find point in a display of infinite width. */
9429 saved_current_buffer = current_buffer;
9430 current_buffer = XBUFFER (w->buffer);
9431
9432 if (w == XWINDOW (selected_window))
9433 pt = BUF_PT (current_buffer);
9434 else
9435 {
9436 pt = marker_position (w->pointm);
9437 pt = max (BEGV, pt);
9438 pt = min (ZV, pt);
9439 }
9440
9441 /* Move iterator to pt starting at cursor_row->start in
9442 a line with infinite width. */
9443 init_to_row_start (&it, w, cursor_row);
9444 it.last_visible_x = INFINITY;
9445 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9446 current_buffer = saved_current_buffer;
9447
9448 /* Position cursor in window. */
9449 if (!hscroll_relative_p && hscroll_step_abs == 0)
9450 hscroll = max (0, (it.current_x
9451 - (ITERATOR_AT_END_OF_LINE_P (&it)
9452 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9453 : (text_area_width / 2))))
9454 / FRAME_COLUMN_WIDTH (it.f);
9455 else if (w->cursor.x >= text_area_width - h_margin)
9456 {
9457 if (hscroll_relative_p)
9458 wanted_x = text_area_width * (1 - hscroll_step_rel)
9459 - h_margin;
9460 else
9461 wanted_x = text_area_width
9462 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9463 - h_margin;
9464 hscroll
9465 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9466 }
9467 else
9468 {
9469 if (hscroll_relative_p)
9470 wanted_x = text_area_width * hscroll_step_rel
9471 + h_margin;
9472 else
9473 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9474 + h_margin;
9475 hscroll
9476 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9477 }
9478 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9479
9480 /* Don't call Fset_window_hscroll if value hasn't
9481 changed because it will prevent redisplay
9482 optimizations. */
9483 if (XFASTINT (w->hscroll) != hscroll)
9484 {
9485 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9486 w->hscroll = make_number (hscroll);
9487 hscrolled_p = 1;
9488 }
9489 }
9490 }
9491
9492 window = w->next;
9493 }
9494
9495 /* Value is non-zero if hscroll of any leaf window has been changed. */
9496 return hscrolled_p;
9497 }
9498
9499
9500 /* Set hscroll so that cursor is visible and not inside horizontal
9501 scroll margins for all windows in the tree rooted at WINDOW. See
9502 also hscroll_window_tree above. Value is non-zero if any window's
9503 hscroll has been changed. If it has, desired matrices on the frame
9504 of WINDOW are cleared. */
9505
9506 static int
9507 hscroll_windows (window)
9508 Lisp_Object window;
9509 {
9510 int hscrolled_p;
9511
9512 if (automatic_hscrolling_p)
9513 {
9514 hscrolled_p = hscroll_window_tree (window);
9515 if (hscrolled_p)
9516 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9517 }
9518 else
9519 hscrolled_p = 0;
9520 return hscrolled_p;
9521 }
9522
9523
9524 \f
9525 /************************************************************************
9526 Redisplay
9527 ************************************************************************/
9528
9529 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9530 to a non-zero value. This is sometimes handy to have in a debugger
9531 session. */
9532
9533 #if GLYPH_DEBUG
9534
9535 /* First and last unchanged row for try_window_id. */
9536
9537 int debug_first_unchanged_at_end_vpos;
9538 int debug_last_unchanged_at_beg_vpos;
9539
9540 /* Delta vpos and y. */
9541
9542 int debug_dvpos, debug_dy;
9543
9544 /* Delta in characters and bytes for try_window_id. */
9545
9546 int debug_delta, debug_delta_bytes;
9547
9548 /* Values of window_end_pos and window_end_vpos at the end of
9549 try_window_id. */
9550
9551 EMACS_INT debug_end_pos, debug_end_vpos;
9552
9553 /* Append a string to W->desired_matrix->method. FMT is a printf
9554 format string. A1...A9 are a supplement for a variable-length
9555 argument list. If trace_redisplay_p is non-zero also printf the
9556 resulting string to stderr. */
9557
9558 static void
9559 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9560 struct window *w;
9561 char *fmt;
9562 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9563 {
9564 char buffer[512];
9565 char *method = w->desired_matrix->method;
9566 int len = strlen (method);
9567 int size = sizeof w->desired_matrix->method;
9568 int remaining = size - len - 1;
9569
9570 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9571 if (len && remaining)
9572 {
9573 method[len] = '|';
9574 --remaining, ++len;
9575 }
9576
9577 strncpy (method + len, buffer, remaining);
9578
9579 if (trace_redisplay_p)
9580 fprintf (stderr, "%p (%s): %s\n",
9581 w,
9582 ((BUFFERP (w->buffer)
9583 && STRINGP (XBUFFER (w->buffer)->name))
9584 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9585 : "no buffer"),
9586 buffer);
9587 }
9588
9589 #endif /* GLYPH_DEBUG */
9590
9591
9592 /* Value is non-zero if all changes in window W, which displays
9593 current_buffer, are in the text between START and END. START is a
9594 buffer position, END is given as a distance from Z. Used in
9595 redisplay_internal for display optimization. */
9596
9597 static INLINE int
9598 text_outside_line_unchanged_p (w, start, end)
9599 struct window *w;
9600 int start, end;
9601 {
9602 int unchanged_p = 1;
9603
9604 /* If text or overlays have changed, see where. */
9605 if (XFASTINT (w->last_modified) < MODIFF
9606 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9607 {
9608 /* Gap in the line? */
9609 if (GPT < start || Z - GPT < end)
9610 unchanged_p = 0;
9611
9612 /* Changes start in front of the line, or end after it? */
9613 if (unchanged_p
9614 && (BEG_UNCHANGED < start - 1
9615 || END_UNCHANGED < end))
9616 unchanged_p = 0;
9617
9618 /* If selective display, can't optimize if changes start at the
9619 beginning of the line. */
9620 if (unchanged_p
9621 && INTEGERP (current_buffer->selective_display)
9622 && XINT (current_buffer->selective_display) > 0
9623 && (BEG_UNCHANGED < start || GPT <= start))
9624 unchanged_p = 0;
9625
9626 /* If there are overlays at the start or end of the line, these
9627 may have overlay strings with newlines in them. A change at
9628 START, for instance, may actually concern the display of such
9629 overlay strings as well, and they are displayed on different
9630 lines. So, quickly rule out this case. (For the future, it
9631 might be desirable to implement something more telling than
9632 just BEG/END_UNCHANGED.) */
9633 if (unchanged_p)
9634 {
9635 if (BEG + BEG_UNCHANGED == start
9636 && overlay_touches_p (start))
9637 unchanged_p = 0;
9638 if (END_UNCHANGED == end
9639 && overlay_touches_p (Z - end))
9640 unchanged_p = 0;
9641 }
9642 }
9643
9644 return unchanged_p;
9645 }
9646
9647
9648 /* Do a frame update, taking possible shortcuts into account. This is
9649 the main external entry point for redisplay.
9650
9651 If the last redisplay displayed an echo area message and that message
9652 is no longer requested, we clear the echo area or bring back the
9653 mini-buffer if that is in use. */
9654
9655 void
9656 redisplay ()
9657 {
9658 redisplay_internal (0);
9659 }
9660
9661
9662 static Lisp_Object
9663 overlay_arrow_string_or_property (var)
9664 Lisp_Object var;
9665 {
9666 Lisp_Object val;
9667
9668 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9669 return val;
9670
9671 return Voverlay_arrow_string;
9672 }
9673
9674 /* Return 1 if there are any overlay-arrows in current_buffer. */
9675 static int
9676 overlay_arrow_in_current_buffer_p ()
9677 {
9678 Lisp_Object vlist;
9679
9680 for (vlist = Voverlay_arrow_variable_list;
9681 CONSP (vlist);
9682 vlist = XCDR (vlist))
9683 {
9684 Lisp_Object var = XCAR (vlist);
9685 Lisp_Object val;
9686
9687 if (!SYMBOLP (var))
9688 continue;
9689 val = find_symbol_value (var);
9690 if (MARKERP (val)
9691 && current_buffer == XMARKER (val)->buffer)
9692 return 1;
9693 }
9694 return 0;
9695 }
9696
9697
9698 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9699 has changed. */
9700
9701 static int
9702 overlay_arrows_changed_p ()
9703 {
9704 Lisp_Object vlist;
9705
9706 for (vlist = Voverlay_arrow_variable_list;
9707 CONSP (vlist);
9708 vlist = XCDR (vlist))
9709 {
9710 Lisp_Object var = XCAR (vlist);
9711 Lisp_Object val, pstr;
9712
9713 if (!SYMBOLP (var))
9714 continue;
9715 val = find_symbol_value (var);
9716 if (!MARKERP (val))
9717 continue;
9718 if (! EQ (COERCE_MARKER (val),
9719 Fget (var, Qlast_arrow_position))
9720 || ! (pstr = overlay_arrow_string_or_property (var),
9721 EQ (pstr, Fget (var, Qlast_arrow_string))))
9722 return 1;
9723 }
9724 return 0;
9725 }
9726
9727 /* Mark overlay arrows to be updated on next redisplay. */
9728
9729 static void
9730 update_overlay_arrows (up_to_date)
9731 int up_to_date;
9732 {
9733 Lisp_Object vlist;
9734
9735 for (vlist = Voverlay_arrow_variable_list;
9736 CONSP (vlist);
9737 vlist = XCDR (vlist))
9738 {
9739 Lisp_Object var = XCAR (vlist);
9740
9741 if (!SYMBOLP (var))
9742 continue;
9743
9744 if (up_to_date > 0)
9745 {
9746 Lisp_Object val = find_symbol_value (var);
9747 Fput (var, Qlast_arrow_position,
9748 COERCE_MARKER (val));
9749 Fput (var, Qlast_arrow_string,
9750 overlay_arrow_string_or_property (var));
9751 }
9752 else if (up_to_date < 0
9753 || !NILP (Fget (var, Qlast_arrow_position)))
9754 {
9755 Fput (var, Qlast_arrow_position, Qt);
9756 Fput (var, Qlast_arrow_string, Qt);
9757 }
9758 }
9759 }
9760
9761
9762 /* Return overlay arrow string to display at row.
9763 Return integer (bitmap number) for arrow bitmap in left fringe.
9764 Return nil if no overlay arrow. */
9765
9766 static Lisp_Object
9767 overlay_arrow_at_row (it, row)
9768 struct it *it;
9769 struct glyph_row *row;
9770 {
9771 Lisp_Object vlist;
9772
9773 for (vlist = Voverlay_arrow_variable_list;
9774 CONSP (vlist);
9775 vlist = XCDR (vlist))
9776 {
9777 Lisp_Object var = XCAR (vlist);
9778 Lisp_Object val;
9779
9780 if (!SYMBOLP (var))
9781 continue;
9782
9783 val = find_symbol_value (var);
9784
9785 if (MARKERP (val)
9786 && current_buffer == XMARKER (val)->buffer
9787 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9788 {
9789 if (FRAME_WINDOW_P (it->f)
9790 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9791 {
9792 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9793 {
9794 int fringe_bitmap;
9795 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9796 return make_number (fringe_bitmap);
9797 }
9798 return make_number (-1); /* Use default arrow bitmap */
9799 }
9800 return overlay_arrow_string_or_property (var);
9801 }
9802 }
9803
9804 return Qnil;
9805 }
9806
9807 /* Return 1 if point moved out of or into a composition. Otherwise
9808 return 0. PREV_BUF and PREV_PT are the last point buffer and
9809 position. BUF and PT are the current point buffer and position. */
9810
9811 int
9812 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9813 struct buffer *prev_buf, *buf;
9814 int prev_pt, pt;
9815 {
9816 int start, end;
9817 Lisp_Object prop;
9818 Lisp_Object buffer;
9819
9820 XSETBUFFER (buffer, buf);
9821 /* Check a composition at the last point if point moved within the
9822 same buffer. */
9823 if (prev_buf == buf)
9824 {
9825 if (prev_pt == pt)
9826 /* Point didn't move. */
9827 return 0;
9828
9829 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9830 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9831 && COMPOSITION_VALID_P (start, end, prop)
9832 && start < prev_pt && end > prev_pt)
9833 /* The last point was within the composition. Return 1 iff
9834 point moved out of the composition. */
9835 return (pt <= start || pt >= end);
9836 }
9837
9838 /* Check a composition at the current point. */
9839 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9840 && find_composition (pt, -1, &start, &end, &prop, buffer)
9841 && COMPOSITION_VALID_P (start, end, prop)
9842 && start < pt && end > pt);
9843 }
9844
9845
9846 /* Reconsider the setting of B->clip_changed which is displayed
9847 in window W. */
9848
9849 static INLINE void
9850 reconsider_clip_changes (w, b)
9851 struct window *w;
9852 struct buffer *b;
9853 {
9854 if (b->clip_changed
9855 && !NILP (w->window_end_valid)
9856 && w->current_matrix->buffer == b
9857 && w->current_matrix->zv == BUF_ZV (b)
9858 && w->current_matrix->begv == BUF_BEGV (b))
9859 b->clip_changed = 0;
9860
9861 /* If display wasn't paused, and W is not a tool bar window, see if
9862 point has been moved into or out of a composition. In that case,
9863 we set b->clip_changed to 1 to force updating the screen. If
9864 b->clip_changed has already been set to 1, we can skip this
9865 check. */
9866 if (!b->clip_changed
9867 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9868 {
9869 int pt;
9870
9871 if (w == XWINDOW (selected_window))
9872 pt = BUF_PT (current_buffer);
9873 else
9874 pt = marker_position (w->pointm);
9875
9876 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9877 || pt != XINT (w->last_point))
9878 && check_point_in_composition (w->current_matrix->buffer,
9879 XINT (w->last_point),
9880 XBUFFER (w->buffer), pt))
9881 b->clip_changed = 1;
9882 }
9883 }
9884 \f
9885
9886 /* Select FRAME to forward the values of frame-local variables into C
9887 variables so that the redisplay routines can access those values
9888 directly. */
9889
9890 static void
9891 select_frame_for_redisplay (frame)
9892 Lisp_Object frame;
9893 {
9894 Lisp_Object tail, sym, val;
9895 Lisp_Object old = selected_frame;
9896
9897 selected_frame = frame;
9898
9899 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9900 if (CONSP (XCAR (tail))
9901 && (sym = XCAR (XCAR (tail)),
9902 SYMBOLP (sym))
9903 && (sym = indirect_variable (sym),
9904 val = SYMBOL_VALUE (sym),
9905 (BUFFER_LOCAL_VALUEP (val)
9906 || SOME_BUFFER_LOCAL_VALUEP (val)))
9907 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9908 Fsymbol_value (sym);
9909
9910 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9911 if (CONSP (XCAR (tail))
9912 && (sym = XCAR (XCAR (tail)),
9913 SYMBOLP (sym))
9914 && (sym = indirect_variable (sym),
9915 val = SYMBOL_VALUE (sym),
9916 (BUFFER_LOCAL_VALUEP (val)
9917 || SOME_BUFFER_LOCAL_VALUEP (val)))
9918 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9919 Fsymbol_value (sym);
9920 }
9921
9922
9923 #define STOP_POLLING \
9924 do { if (! polling_stopped_here) stop_polling (); \
9925 polling_stopped_here = 1; } while (0)
9926
9927 #define RESUME_POLLING \
9928 do { if (polling_stopped_here) start_polling (); \
9929 polling_stopped_here = 0; } while (0)
9930
9931
9932 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9933 response to any user action; therefore, we should preserve the echo
9934 area. (Actually, our caller does that job.) Perhaps in the future
9935 avoid recentering windows if it is not necessary; currently that
9936 causes some problems. */
9937
9938 static void
9939 redisplay_internal (preserve_echo_area)
9940 int preserve_echo_area;
9941 {
9942 struct window *w = XWINDOW (selected_window);
9943 struct frame *f = XFRAME (w->frame);
9944 int pause;
9945 int must_finish = 0;
9946 struct text_pos tlbufpos, tlendpos;
9947 int number_of_visible_frames;
9948 int count;
9949 struct frame *sf = SELECTED_FRAME ();
9950 int polling_stopped_here = 0;
9951
9952 /* Non-zero means redisplay has to consider all windows on all
9953 frames. Zero means, only selected_window is considered. */
9954 int consider_all_windows_p;
9955
9956 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9957
9958 /* No redisplay if running in batch mode or frame is not yet fully
9959 initialized, or redisplay is explicitly turned off by setting
9960 Vinhibit_redisplay. */
9961 if (noninteractive
9962 || !NILP (Vinhibit_redisplay)
9963 || !f->glyphs_initialized_p)
9964 return;
9965
9966 /* The flag redisplay_performed_directly_p is set by
9967 direct_output_for_insert when it already did the whole screen
9968 update necessary. */
9969 if (redisplay_performed_directly_p)
9970 {
9971 redisplay_performed_directly_p = 0;
9972 if (!hscroll_windows (selected_window))
9973 return;
9974 }
9975
9976 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9977 if (popup_activated ())
9978 return;
9979 #endif
9980
9981 /* I don't think this happens but let's be paranoid. */
9982 if (redisplaying_p)
9983 return;
9984
9985 /* Record a function that resets redisplaying_p to its old value
9986 when we leave this function. */
9987 count = SPECPDL_INDEX ();
9988 record_unwind_protect (unwind_redisplay,
9989 Fcons (make_number (redisplaying_p), selected_frame));
9990 ++redisplaying_p;
9991 specbind (Qinhibit_free_realized_faces, Qnil);
9992
9993 retry:
9994 pause = 0;
9995 reconsider_clip_changes (w, current_buffer);
9996
9997 /* If new fonts have been loaded that make a glyph matrix adjustment
9998 necessary, do it. */
9999 if (fonts_changed_p)
10000 {
10001 adjust_glyphs (NULL);
10002 ++windows_or_buffers_changed;
10003 fonts_changed_p = 0;
10004 }
10005
10006 /* If face_change_count is non-zero, init_iterator will free all
10007 realized faces, which includes the faces referenced from current
10008 matrices. So, we can't reuse current matrices in this case. */
10009 if (face_change_count)
10010 ++windows_or_buffers_changed;
10011
10012 if (! FRAME_WINDOW_P (sf)
10013 && previous_terminal_frame != sf)
10014 {
10015 /* Since frames on an ASCII terminal share the same display
10016 area, displaying a different frame means redisplay the whole
10017 thing. */
10018 windows_or_buffers_changed++;
10019 SET_FRAME_GARBAGED (sf);
10020 XSETFRAME (Vterminal_frame, sf);
10021 }
10022 previous_terminal_frame = sf;
10023
10024 /* Set the visible flags for all frames. Do this before checking
10025 for resized or garbaged frames; they want to know if their frames
10026 are visible. See the comment in frame.h for
10027 FRAME_SAMPLE_VISIBILITY. */
10028 {
10029 Lisp_Object tail, frame;
10030
10031 number_of_visible_frames = 0;
10032
10033 FOR_EACH_FRAME (tail, frame)
10034 {
10035 struct frame *f = XFRAME (frame);
10036
10037 FRAME_SAMPLE_VISIBILITY (f);
10038 if (FRAME_VISIBLE_P (f))
10039 ++number_of_visible_frames;
10040 clear_desired_matrices (f);
10041 }
10042 }
10043
10044 /* Notice any pending interrupt request to change frame size. */
10045 do_pending_window_change (1);
10046
10047 /* Clear frames marked as garbaged. */
10048 if (frame_garbaged)
10049 clear_garbaged_frames ();
10050
10051 /* Build menubar and tool-bar items. */
10052 prepare_menu_bars ();
10053
10054 if (windows_or_buffers_changed)
10055 update_mode_lines++;
10056
10057 /* Detect case that we need to write or remove a star in the mode line. */
10058 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10059 {
10060 w->update_mode_line = Qt;
10061 if (buffer_shared > 1)
10062 update_mode_lines++;
10063 }
10064
10065 /* If %c is in the mode line, update it if needed. */
10066 if (!NILP (w->column_number_displayed)
10067 /* This alternative quickly identifies a common case
10068 where no change is needed. */
10069 && !(PT == XFASTINT (w->last_point)
10070 && XFASTINT (w->last_modified) >= MODIFF
10071 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10072 && (XFASTINT (w->column_number_displayed)
10073 != (int) current_column ())) /* iftc */
10074 w->update_mode_line = Qt;
10075
10076 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10077
10078 /* The variable buffer_shared is set in redisplay_window and
10079 indicates that we redisplay a buffer in different windows. See
10080 there. */
10081 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10082 || cursor_type_changed);
10083
10084 /* If specs for an arrow have changed, do thorough redisplay
10085 to ensure we remove any arrow that should no longer exist. */
10086 if (overlay_arrows_changed_p ())
10087 consider_all_windows_p = windows_or_buffers_changed = 1;
10088
10089 /* Normally the message* functions will have already displayed and
10090 updated the echo area, but the frame may have been trashed, or
10091 the update may have been preempted, so display the echo area
10092 again here. Checking message_cleared_p captures the case that
10093 the echo area should be cleared. */
10094 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10095 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10096 || (message_cleared_p
10097 && minibuf_level == 0
10098 /* If the mini-window is currently selected, this means the
10099 echo-area doesn't show through. */
10100 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10101 {
10102 int window_height_changed_p = echo_area_display (0);
10103 must_finish = 1;
10104
10105 /* If we don't display the current message, don't clear the
10106 message_cleared_p flag, because, if we did, we wouldn't clear
10107 the echo area in the next redisplay which doesn't preserve
10108 the echo area. */
10109 if (!display_last_displayed_message_p)
10110 message_cleared_p = 0;
10111
10112 if (fonts_changed_p)
10113 goto retry;
10114 else if (window_height_changed_p)
10115 {
10116 consider_all_windows_p = 1;
10117 ++update_mode_lines;
10118 ++windows_or_buffers_changed;
10119
10120 /* If window configuration was changed, frames may have been
10121 marked garbaged. Clear them or we will experience
10122 surprises wrt scrolling. */
10123 if (frame_garbaged)
10124 clear_garbaged_frames ();
10125 }
10126 }
10127 else if (EQ (selected_window, minibuf_window)
10128 && (current_buffer->clip_changed
10129 || XFASTINT (w->last_modified) < MODIFF
10130 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10131 && resize_mini_window (w, 0))
10132 {
10133 /* Resized active mini-window to fit the size of what it is
10134 showing if its contents might have changed. */
10135 must_finish = 1;
10136 consider_all_windows_p = 1;
10137 ++windows_or_buffers_changed;
10138 ++update_mode_lines;
10139
10140 /* If window configuration was changed, frames may have been
10141 marked garbaged. Clear them or we will experience
10142 surprises wrt scrolling. */
10143 if (frame_garbaged)
10144 clear_garbaged_frames ();
10145 }
10146
10147
10148 /* If showing the region, and mark has changed, we must redisplay
10149 the whole window. The assignment to this_line_start_pos prevents
10150 the optimization directly below this if-statement. */
10151 if (((!NILP (Vtransient_mark_mode)
10152 && !NILP (XBUFFER (w->buffer)->mark_active))
10153 != !NILP (w->region_showing))
10154 || (!NILP (w->region_showing)
10155 && !EQ (w->region_showing,
10156 Fmarker_position (XBUFFER (w->buffer)->mark))))
10157 CHARPOS (this_line_start_pos) = 0;
10158
10159 /* Optimize the case that only the line containing the cursor in the
10160 selected window has changed. Variables starting with this_ are
10161 set in display_line and record information about the line
10162 containing the cursor. */
10163 tlbufpos = this_line_start_pos;
10164 tlendpos = this_line_end_pos;
10165 if (!consider_all_windows_p
10166 && CHARPOS (tlbufpos) > 0
10167 && NILP (w->update_mode_line)
10168 && !current_buffer->clip_changed
10169 && !current_buffer->prevent_redisplay_optimizations_p
10170 && FRAME_VISIBLE_P (XFRAME (w->frame))
10171 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10172 /* Make sure recorded data applies to current buffer, etc. */
10173 && this_line_buffer == current_buffer
10174 && current_buffer == XBUFFER (w->buffer)
10175 && NILP (w->force_start)
10176 && NILP (w->optional_new_start)
10177 /* Point must be on the line that we have info recorded about. */
10178 && PT >= CHARPOS (tlbufpos)
10179 && PT <= Z - CHARPOS (tlendpos)
10180 /* All text outside that line, including its final newline,
10181 must be unchanged */
10182 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10183 CHARPOS (tlendpos)))
10184 {
10185 if (CHARPOS (tlbufpos) > BEGV
10186 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10187 && (CHARPOS (tlbufpos) == ZV
10188 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10189 /* Former continuation line has disappeared by becoming empty */
10190 goto cancel;
10191 else if (XFASTINT (w->last_modified) < MODIFF
10192 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10193 || MINI_WINDOW_P (w))
10194 {
10195 /* We have to handle the case of continuation around a
10196 wide-column character (See the comment in indent.c around
10197 line 885).
10198
10199 For instance, in the following case:
10200
10201 -------- Insert --------
10202 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10203 J_I_ ==> J_I_ `^^' are cursors.
10204 ^^ ^^
10205 -------- --------
10206
10207 As we have to redraw the line above, we should goto cancel. */
10208
10209 struct it it;
10210 int line_height_before = this_line_pixel_height;
10211
10212 /* Note that start_display will handle the case that the
10213 line starting at tlbufpos is a continuation lines. */
10214 start_display (&it, w, tlbufpos);
10215
10216 /* Implementation note: It this still necessary? */
10217 if (it.current_x != this_line_start_x)
10218 goto cancel;
10219
10220 TRACE ((stderr, "trying display optimization 1\n"));
10221 w->cursor.vpos = -1;
10222 overlay_arrow_seen = 0;
10223 it.vpos = this_line_vpos;
10224 it.current_y = this_line_y;
10225 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10226 display_line (&it);
10227
10228 /* If line contains point, is not continued,
10229 and ends at same distance from eob as before, we win */
10230 if (w->cursor.vpos >= 0
10231 /* Line is not continued, otherwise this_line_start_pos
10232 would have been set to 0 in display_line. */
10233 && CHARPOS (this_line_start_pos)
10234 /* Line ends as before. */
10235 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10236 /* Line has same height as before. Otherwise other lines
10237 would have to be shifted up or down. */
10238 && this_line_pixel_height == line_height_before)
10239 {
10240 /* If this is not the window's last line, we must adjust
10241 the charstarts of the lines below. */
10242 if (it.current_y < it.last_visible_y)
10243 {
10244 struct glyph_row *row
10245 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10246 int delta, delta_bytes;
10247
10248 if (Z - CHARPOS (tlendpos) == ZV)
10249 {
10250 /* This line ends at end of (accessible part of)
10251 buffer. There is no newline to count. */
10252 delta = (Z
10253 - CHARPOS (tlendpos)
10254 - MATRIX_ROW_START_CHARPOS (row));
10255 delta_bytes = (Z_BYTE
10256 - BYTEPOS (tlendpos)
10257 - MATRIX_ROW_START_BYTEPOS (row));
10258 }
10259 else
10260 {
10261 /* This line ends in a newline. Must take
10262 account of the newline and the rest of the
10263 text that follows. */
10264 delta = (Z
10265 - CHARPOS (tlendpos)
10266 - MATRIX_ROW_START_CHARPOS (row));
10267 delta_bytes = (Z_BYTE
10268 - BYTEPOS (tlendpos)
10269 - MATRIX_ROW_START_BYTEPOS (row));
10270 }
10271
10272 increment_matrix_positions (w->current_matrix,
10273 this_line_vpos + 1,
10274 w->current_matrix->nrows,
10275 delta, delta_bytes);
10276 }
10277
10278 /* If this row displays text now but previously didn't,
10279 or vice versa, w->window_end_vpos may have to be
10280 adjusted. */
10281 if ((it.glyph_row - 1)->displays_text_p)
10282 {
10283 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10284 XSETINT (w->window_end_vpos, this_line_vpos);
10285 }
10286 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10287 && this_line_vpos > 0)
10288 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10289 w->window_end_valid = Qnil;
10290
10291 /* Update hint: No need to try to scroll in update_window. */
10292 w->desired_matrix->no_scrolling_p = 1;
10293
10294 #if GLYPH_DEBUG
10295 *w->desired_matrix->method = 0;
10296 debug_method_add (w, "optimization 1");
10297 #endif
10298 #ifdef HAVE_WINDOW_SYSTEM
10299 update_window_fringes (w, 0);
10300 #endif
10301 goto update;
10302 }
10303 else
10304 goto cancel;
10305 }
10306 else if (/* Cursor position hasn't changed. */
10307 PT == XFASTINT (w->last_point)
10308 /* Make sure the cursor was last displayed
10309 in this window. Otherwise we have to reposition it. */
10310 && 0 <= w->cursor.vpos
10311 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10312 {
10313 if (!must_finish)
10314 {
10315 do_pending_window_change (1);
10316
10317 /* We used to always goto end_of_redisplay here, but this
10318 isn't enough if we have a blinking cursor. */
10319 if (w->cursor_off_p == w->last_cursor_off_p)
10320 goto end_of_redisplay;
10321 }
10322 goto update;
10323 }
10324 /* If highlighting the region, or if the cursor is in the echo area,
10325 then we can't just move the cursor. */
10326 else if (! (!NILP (Vtransient_mark_mode)
10327 && !NILP (current_buffer->mark_active))
10328 && (EQ (selected_window, current_buffer->last_selected_window)
10329 || highlight_nonselected_windows)
10330 && NILP (w->region_showing)
10331 && NILP (Vshow_trailing_whitespace)
10332 && !cursor_in_echo_area)
10333 {
10334 struct it it;
10335 struct glyph_row *row;
10336
10337 /* Skip from tlbufpos to PT and see where it is. Note that
10338 PT may be in invisible text. If so, we will end at the
10339 next visible position. */
10340 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10341 NULL, DEFAULT_FACE_ID);
10342 it.current_x = this_line_start_x;
10343 it.current_y = this_line_y;
10344 it.vpos = this_line_vpos;
10345
10346 /* The call to move_it_to stops in front of PT, but
10347 moves over before-strings. */
10348 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10349
10350 if (it.vpos == this_line_vpos
10351 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10352 row->enabled_p))
10353 {
10354 xassert (this_line_vpos == it.vpos);
10355 xassert (this_line_y == it.current_y);
10356 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10357 #if GLYPH_DEBUG
10358 *w->desired_matrix->method = 0;
10359 debug_method_add (w, "optimization 3");
10360 #endif
10361 goto update;
10362 }
10363 else
10364 goto cancel;
10365 }
10366
10367 cancel:
10368 /* Text changed drastically or point moved off of line. */
10369 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10370 }
10371
10372 CHARPOS (this_line_start_pos) = 0;
10373 consider_all_windows_p |= buffer_shared > 1;
10374 ++clear_face_cache_count;
10375 #ifdef HAVE_WINDOW_SYSTEM
10376 ++clear_image_cache_count;
10377 #endif
10378
10379 /* Build desired matrices, and update the display. If
10380 consider_all_windows_p is non-zero, do it for all windows on all
10381 frames. Otherwise do it for selected_window, only. */
10382
10383 if (consider_all_windows_p)
10384 {
10385 Lisp_Object tail, frame;
10386 int i, n = 0, size = 50;
10387 struct frame **updated
10388 = (struct frame **) alloca (size * sizeof *updated);
10389
10390 /* Recompute # windows showing selected buffer. This will be
10391 incremented each time such a window is displayed. */
10392 buffer_shared = 0;
10393
10394 FOR_EACH_FRAME (tail, frame)
10395 {
10396 struct frame *f = XFRAME (frame);
10397
10398 if (FRAME_WINDOW_P (f) || f == sf)
10399 {
10400 if (! EQ (frame, selected_frame))
10401 /* Select the frame, for the sake of frame-local
10402 variables. */
10403 select_frame_for_redisplay (frame);
10404
10405 /* Mark all the scroll bars to be removed; we'll redeem
10406 the ones we want when we redisplay their windows. */
10407 if (condemn_scroll_bars_hook)
10408 condemn_scroll_bars_hook (f);
10409
10410 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10411 redisplay_windows (FRAME_ROOT_WINDOW (f));
10412
10413 /* Any scroll bars which redisplay_windows should have
10414 nuked should now go away. */
10415 if (judge_scroll_bars_hook)
10416 judge_scroll_bars_hook (f);
10417
10418 /* If fonts changed, display again. */
10419 /* ??? rms: I suspect it is a mistake to jump all the way
10420 back to retry here. It should just retry this frame. */
10421 if (fonts_changed_p)
10422 goto retry;
10423
10424 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10425 {
10426 /* See if we have to hscroll. */
10427 if (hscroll_windows (f->root_window))
10428 goto retry;
10429
10430 /* Prevent various kinds of signals during display
10431 update. stdio is not robust about handling
10432 signals, which can cause an apparent I/O
10433 error. */
10434 if (interrupt_input)
10435 unrequest_sigio ();
10436 STOP_POLLING;
10437
10438 /* Update the display. */
10439 set_window_update_flags (XWINDOW (f->root_window), 1);
10440 pause |= update_frame (f, 0, 0);
10441 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10442 if (pause)
10443 break;
10444 #endif
10445
10446 if (n == size)
10447 {
10448 int nbytes = size * sizeof *updated;
10449 struct frame **p = (struct frame **) alloca (2 * nbytes);
10450 bcopy (updated, p, nbytes);
10451 size *= 2;
10452 }
10453
10454 updated[n++] = f;
10455 }
10456 }
10457 }
10458
10459 if (!pause)
10460 {
10461 /* Do the mark_window_display_accurate after all windows have
10462 been redisplayed because this call resets flags in buffers
10463 which are needed for proper redisplay. */
10464 for (i = 0; i < n; ++i)
10465 {
10466 struct frame *f = updated[i];
10467 mark_window_display_accurate (f->root_window, 1);
10468 if (frame_up_to_date_hook)
10469 frame_up_to_date_hook (f);
10470 }
10471 }
10472 }
10473 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10474 {
10475 Lisp_Object mini_window;
10476 struct frame *mini_frame;
10477
10478 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10479 /* Use list_of_error, not Qerror, so that
10480 we catch only errors and don't run the debugger. */
10481 internal_condition_case_1 (redisplay_window_1, selected_window,
10482 list_of_error,
10483 redisplay_window_error);
10484
10485 /* Compare desired and current matrices, perform output. */
10486
10487 update:
10488 /* If fonts changed, display again. */
10489 if (fonts_changed_p)
10490 goto retry;
10491
10492 /* Prevent various kinds of signals during display update.
10493 stdio is not robust about handling signals,
10494 which can cause an apparent I/O error. */
10495 if (interrupt_input)
10496 unrequest_sigio ();
10497 STOP_POLLING;
10498
10499 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10500 {
10501 if (hscroll_windows (selected_window))
10502 goto retry;
10503
10504 XWINDOW (selected_window)->must_be_updated_p = 1;
10505 pause = update_frame (sf, 0, 0);
10506 }
10507
10508 /* We may have called echo_area_display at the top of this
10509 function. If the echo area is on another frame, that may
10510 have put text on a frame other than the selected one, so the
10511 above call to update_frame would not have caught it. Catch
10512 it here. */
10513 mini_window = FRAME_MINIBUF_WINDOW (sf);
10514 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10515
10516 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10517 {
10518 XWINDOW (mini_window)->must_be_updated_p = 1;
10519 pause |= update_frame (mini_frame, 0, 0);
10520 if (!pause && hscroll_windows (mini_window))
10521 goto retry;
10522 }
10523 }
10524
10525 /* If display was paused because of pending input, make sure we do a
10526 thorough update the next time. */
10527 if (pause)
10528 {
10529 /* Prevent the optimization at the beginning of
10530 redisplay_internal that tries a single-line update of the
10531 line containing the cursor in the selected window. */
10532 CHARPOS (this_line_start_pos) = 0;
10533
10534 /* Let the overlay arrow be updated the next time. */
10535 update_overlay_arrows (0);
10536
10537 /* If we pause after scrolling, some rows in the current
10538 matrices of some windows are not valid. */
10539 if (!WINDOW_FULL_WIDTH_P (w)
10540 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10541 update_mode_lines = 1;
10542 }
10543 else
10544 {
10545 if (!consider_all_windows_p)
10546 {
10547 /* This has already been done above if
10548 consider_all_windows_p is set. */
10549 mark_window_display_accurate_1 (w, 1);
10550
10551 /* Say overlay arrows are up to date. */
10552 update_overlay_arrows (1);
10553
10554 if (frame_up_to_date_hook != 0)
10555 frame_up_to_date_hook (sf);
10556 }
10557
10558 update_mode_lines = 0;
10559 windows_or_buffers_changed = 0;
10560 cursor_type_changed = 0;
10561 }
10562
10563 /* Start SIGIO interrupts coming again. Having them off during the
10564 code above makes it less likely one will discard output, but not
10565 impossible, since there might be stuff in the system buffer here.
10566 But it is much hairier to try to do anything about that. */
10567 if (interrupt_input)
10568 request_sigio ();
10569 RESUME_POLLING;
10570
10571 /* If a frame has become visible which was not before, redisplay
10572 again, so that we display it. Expose events for such a frame
10573 (which it gets when becoming visible) don't call the parts of
10574 redisplay constructing glyphs, so simply exposing a frame won't
10575 display anything in this case. So, we have to display these
10576 frames here explicitly. */
10577 if (!pause)
10578 {
10579 Lisp_Object tail, frame;
10580 int new_count = 0;
10581
10582 FOR_EACH_FRAME (tail, frame)
10583 {
10584 int this_is_visible = 0;
10585
10586 if (XFRAME (frame)->visible)
10587 this_is_visible = 1;
10588 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10589 if (XFRAME (frame)->visible)
10590 this_is_visible = 1;
10591
10592 if (this_is_visible)
10593 new_count++;
10594 }
10595
10596 if (new_count != number_of_visible_frames)
10597 windows_or_buffers_changed++;
10598 }
10599
10600 /* Change frame size now if a change is pending. */
10601 do_pending_window_change (1);
10602
10603 /* If we just did a pending size change, or have additional
10604 visible frames, redisplay again. */
10605 if (windows_or_buffers_changed && !pause)
10606 goto retry;
10607
10608 /* Clear the face cache eventually. */
10609 if (consider_all_windows_p)
10610 {
10611 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10612 {
10613 clear_face_cache (0);
10614 clear_face_cache_count = 0;
10615 }
10616 #ifdef HAVE_WINDOW_SYSTEM
10617 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10618 {
10619 Lisp_Object tail, frame;
10620 FOR_EACH_FRAME (tail, frame)
10621 {
10622 struct frame *f = XFRAME (frame);
10623 if (FRAME_WINDOW_P (f))
10624 clear_image_cache (f, 0);
10625 }
10626 clear_image_cache_count = 0;
10627 }
10628 #endif /* HAVE_WINDOW_SYSTEM */
10629 }
10630
10631 end_of_redisplay:
10632 unbind_to (count, Qnil);
10633 RESUME_POLLING;
10634 }
10635
10636
10637 /* Redisplay, but leave alone any recent echo area message unless
10638 another message has been requested in its place.
10639
10640 This is useful in situations where you need to redisplay but no
10641 user action has occurred, making it inappropriate for the message
10642 area to be cleared. See tracking_off and
10643 wait_reading_process_output for examples of these situations.
10644
10645 FROM_WHERE is an integer saying from where this function was
10646 called. This is useful for debugging. */
10647
10648 void
10649 redisplay_preserve_echo_area (from_where)
10650 int from_where;
10651 {
10652 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10653
10654 if (!NILP (echo_area_buffer[1]))
10655 {
10656 /* We have a previously displayed message, but no current
10657 message. Redisplay the previous message. */
10658 display_last_displayed_message_p = 1;
10659 redisplay_internal (1);
10660 display_last_displayed_message_p = 0;
10661 }
10662 else
10663 redisplay_internal (1);
10664
10665 if (rif != NULL && rif->flush_display_optional)
10666 rif->flush_display_optional (NULL);
10667 }
10668
10669
10670 /* Function registered with record_unwind_protect in
10671 redisplay_internal. Reset redisplaying_p to the value it had
10672 before redisplay_internal was called, and clear
10673 prevent_freeing_realized_faces_p. It also selects the previously
10674 selected frame. */
10675
10676 static Lisp_Object
10677 unwind_redisplay (val)
10678 Lisp_Object val;
10679 {
10680 Lisp_Object old_redisplaying_p, old_frame;
10681
10682 old_redisplaying_p = XCAR (val);
10683 redisplaying_p = XFASTINT (old_redisplaying_p);
10684 old_frame = XCDR (val);
10685 if (! EQ (old_frame, selected_frame))
10686 select_frame_for_redisplay (old_frame);
10687 return Qnil;
10688 }
10689
10690
10691 /* Mark the display of window W as accurate or inaccurate. If
10692 ACCURATE_P is non-zero mark display of W as accurate. If
10693 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10694 redisplay_internal is called. */
10695
10696 static void
10697 mark_window_display_accurate_1 (w, accurate_p)
10698 struct window *w;
10699 int accurate_p;
10700 {
10701 if (BUFFERP (w->buffer))
10702 {
10703 struct buffer *b = XBUFFER (w->buffer);
10704
10705 w->last_modified
10706 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10707 w->last_overlay_modified
10708 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10709 w->last_had_star
10710 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10711
10712 if (accurate_p)
10713 {
10714 b->clip_changed = 0;
10715 b->prevent_redisplay_optimizations_p = 0;
10716
10717 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10718 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10719 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10720 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10721
10722 w->current_matrix->buffer = b;
10723 w->current_matrix->begv = BUF_BEGV (b);
10724 w->current_matrix->zv = BUF_ZV (b);
10725
10726 w->last_cursor = w->cursor;
10727 w->last_cursor_off_p = w->cursor_off_p;
10728
10729 if (w == XWINDOW (selected_window))
10730 w->last_point = make_number (BUF_PT (b));
10731 else
10732 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10733 }
10734 }
10735
10736 if (accurate_p)
10737 {
10738 w->window_end_valid = w->buffer;
10739 #if 0 /* This is incorrect with variable-height lines. */
10740 xassert (XINT (w->window_end_vpos)
10741 < (WINDOW_TOTAL_LINES (w)
10742 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10743 #endif
10744 w->update_mode_line = Qnil;
10745 }
10746 }
10747
10748
10749 /* Mark the display of windows in the window tree rooted at WINDOW as
10750 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10751 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10752 be redisplayed the next time redisplay_internal is called. */
10753
10754 void
10755 mark_window_display_accurate (window, accurate_p)
10756 Lisp_Object window;
10757 int accurate_p;
10758 {
10759 struct window *w;
10760
10761 for (; !NILP (window); window = w->next)
10762 {
10763 w = XWINDOW (window);
10764 mark_window_display_accurate_1 (w, accurate_p);
10765
10766 if (!NILP (w->vchild))
10767 mark_window_display_accurate (w->vchild, accurate_p);
10768 if (!NILP (w->hchild))
10769 mark_window_display_accurate (w->hchild, accurate_p);
10770 }
10771
10772 if (accurate_p)
10773 {
10774 update_overlay_arrows (1);
10775 }
10776 else
10777 {
10778 /* Force a thorough redisplay the next time by setting
10779 last_arrow_position and last_arrow_string to t, which is
10780 unequal to any useful value of Voverlay_arrow_... */
10781 update_overlay_arrows (-1);
10782 }
10783 }
10784
10785
10786 /* Return value in display table DP (Lisp_Char_Table *) for character
10787 C. Since a display table doesn't have any parent, we don't have to
10788 follow parent. Do not call this function directly but use the
10789 macro DISP_CHAR_VECTOR. */
10790
10791 Lisp_Object
10792 disp_char_vector (dp, c)
10793 struct Lisp_Char_Table *dp;
10794 int c;
10795 {
10796 int code[4], i;
10797 Lisp_Object val;
10798
10799 if (SINGLE_BYTE_CHAR_P (c))
10800 return (dp->contents[c]);
10801
10802 SPLIT_CHAR (c, code[0], code[1], code[2]);
10803 if (code[1] < 32)
10804 code[1] = -1;
10805 else if (code[2] < 32)
10806 code[2] = -1;
10807
10808 /* Here, the possible range of code[0] (== charset ID) is
10809 128..max_charset. Since the top level char table contains data
10810 for multibyte characters after 256th element, we must increment
10811 code[0] by 128 to get a correct index. */
10812 code[0] += 128;
10813 code[3] = -1; /* anchor */
10814
10815 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10816 {
10817 val = dp->contents[code[i]];
10818 if (!SUB_CHAR_TABLE_P (val))
10819 return (NILP (val) ? dp->defalt : val);
10820 }
10821
10822 /* Here, val is a sub char table. We return the default value of
10823 it. */
10824 return (dp->defalt);
10825 }
10826
10827
10828 \f
10829 /***********************************************************************
10830 Window Redisplay
10831 ***********************************************************************/
10832
10833 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10834
10835 static void
10836 redisplay_windows (window)
10837 Lisp_Object window;
10838 {
10839 while (!NILP (window))
10840 {
10841 struct window *w = XWINDOW (window);
10842
10843 if (!NILP (w->hchild))
10844 redisplay_windows (w->hchild);
10845 else if (!NILP (w->vchild))
10846 redisplay_windows (w->vchild);
10847 else
10848 {
10849 displayed_buffer = XBUFFER (w->buffer);
10850 /* Use list_of_error, not Qerror, so that
10851 we catch only errors and don't run the debugger. */
10852 internal_condition_case_1 (redisplay_window_0, window,
10853 list_of_error,
10854 redisplay_window_error);
10855 }
10856
10857 window = w->next;
10858 }
10859 }
10860
10861 static Lisp_Object
10862 redisplay_window_error ()
10863 {
10864 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10865 return Qnil;
10866 }
10867
10868 static Lisp_Object
10869 redisplay_window_0 (window)
10870 Lisp_Object window;
10871 {
10872 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10873 redisplay_window (window, 0);
10874 return Qnil;
10875 }
10876
10877 static Lisp_Object
10878 redisplay_window_1 (window)
10879 Lisp_Object window;
10880 {
10881 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10882 redisplay_window (window, 1);
10883 return Qnil;
10884 }
10885 \f
10886
10887 /* Increment GLYPH until it reaches END or CONDITION fails while
10888 adding (GLYPH)->pixel_width to X. */
10889
10890 #define SKIP_GLYPHS(glyph, end, x, condition) \
10891 do \
10892 { \
10893 (x) += (glyph)->pixel_width; \
10894 ++(glyph); \
10895 } \
10896 while ((glyph) < (end) && (condition))
10897
10898
10899 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10900 DELTA is the number of bytes by which positions recorded in ROW
10901 differ from current buffer positions. */
10902
10903 void
10904 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10905 struct window *w;
10906 struct glyph_row *row;
10907 struct glyph_matrix *matrix;
10908 int delta, delta_bytes, dy, dvpos;
10909 {
10910 struct glyph *glyph = row->glyphs[TEXT_AREA];
10911 struct glyph *end = glyph + row->used[TEXT_AREA];
10912 struct glyph *cursor = NULL;
10913 /* The first glyph that starts a sequence of glyphs from string. */
10914 struct glyph *string_start;
10915 /* The X coordinate of string_start. */
10916 int string_start_x;
10917 /* The last known character position. */
10918 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10919 /* The last known character position before string_start. */
10920 int string_before_pos;
10921 int x = row->x;
10922 int cursor_x = x;
10923 int cursor_from_overlay_pos = 0;
10924 int pt_old = PT - delta;
10925
10926 /* Skip over glyphs not having an object at the start of the row.
10927 These are special glyphs like truncation marks on terminal
10928 frames. */
10929 if (row->displays_text_p)
10930 while (glyph < end
10931 && INTEGERP (glyph->object)
10932 && glyph->charpos < 0)
10933 {
10934 x += glyph->pixel_width;
10935 ++glyph;
10936 }
10937
10938 string_start = NULL;
10939 while (glyph < end
10940 && !INTEGERP (glyph->object)
10941 && (!BUFFERP (glyph->object)
10942 || (last_pos = glyph->charpos) < pt_old))
10943 {
10944 if (! STRINGP (glyph->object))
10945 {
10946 string_start = NULL;
10947 x += glyph->pixel_width;
10948 ++glyph;
10949 if (cursor_from_overlay_pos
10950 && last_pos > cursor_from_overlay_pos)
10951 {
10952 cursor_from_overlay_pos = 0;
10953 cursor = 0;
10954 }
10955 }
10956 else
10957 {
10958 string_before_pos = last_pos;
10959 string_start = glyph;
10960 string_start_x = x;
10961 /* Skip all glyphs from string. */
10962 do
10963 {
10964 int pos;
10965 if ((cursor == NULL || glyph > cursor)
10966 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10967 Qcursor, (glyph)->object))
10968 && (pos = string_buffer_position (w, glyph->object,
10969 string_before_pos),
10970 (pos == 0 /* From overlay */
10971 || pos == pt_old)))
10972 {
10973 /* Estimate overlay buffer position from the buffer
10974 positions of the glyphs before and after the overlay.
10975 Add 1 to last_pos so that if point corresponds to the
10976 glyph right after the overlay, we still use a 'cursor'
10977 property found in that overlay. */
10978 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10979 cursor = glyph;
10980 cursor_x = x;
10981 }
10982 x += glyph->pixel_width;
10983 ++glyph;
10984 }
10985 while (glyph < end && STRINGP (glyph->object));
10986 }
10987 }
10988
10989 if (cursor != NULL)
10990 {
10991 glyph = cursor;
10992 x = cursor_x;
10993 }
10994 else if (row->ends_in_ellipsis_p && glyph == end)
10995 {
10996 /* Scan back over the ellipsis glyphs, decrementing positions. */
10997 while (glyph > row->glyphs[TEXT_AREA]
10998 && (glyph - 1)->charpos == last_pos)
10999 glyph--, x -= glyph->pixel_width;
11000 /* That loop always goes one position too far,
11001 including the glyph before the ellipsis.
11002 So scan forward over that one. */
11003 x += glyph->pixel_width;
11004 glyph++;
11005 }
11006 else if (string_start
11007 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11008 {
11009 /* We may have skipped over point because the previous glyphs
11010 are from string. As there's no easy way to know the
11011 character position of the current glyph, find the correct
11012 glyph on point by scanning from string_start again. */
11013 Lisp_Object limit;
11014 Lisp_Object string;
11015 int pos;
11016
11017 limit = make_number (pt_old + 1);
11018 end = glyph;
11019 glyph = string_start;
11020 x = string_start_x;
11021 string = glyph->object;
11022 pos = string_buffer_position (w, string, string_before_pos);
11023 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11024 because we always put cursor after overlay strings. */
11025 while (pos == 0 && glyph < end)
11026 {
11027 string = glyph->object;
11028 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11029 if (glyph < end)
11030 pos = string_buffer_position (w, glyph->object, string_before_pos);
11031 }
11032
11033 while (glyph < end)
11034 {
11035 pos = XINT (Fnext_single_char_property_change
11036 (make_number (pos), Qdisplay, Qnil, limit));
11037 if (pos > pt_old)
11038 break;
11039 /* Skip glyphs from the same string. */
11040 string = glyph->object;
11041 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11042 /* Skip glyphs from an overlay. */
11043 while (glyph < end
11044 && ! string_buffer_position (w, glyph->object, pos))
11045 {
11046 string = glyph->object;
11047 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11048 }
11049 }
11050 }
11051
11052 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11053 w->cursor.x = x;
11054 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11055 w->cursor.y = row->y + dy;
11056
11057 if (w == XWINDOW (selected_window))
11058 {
11059 if (!row->continued_p
11060 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11061 && row->x == 0)
11062 {
11063 this_line_buffer = XBUFFER (w->buffer);
11064
11065 CHARPOS (this_line_start_pos)
11066 = MATRIX_ROW_START_CHARPOS (row) + delta;
11067 BYTEPOS (this_line_start_pos)
11068 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11069
11070 CHARPOS (this_line_end_pos)
11071 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11072 BYTEPOS (this_line_end_pos)
11073 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11074
11075 this_line_y = w->cursor.y;
11076 this_line_pixel_height = row->height;
11077 this_line_vpos = w->cursor.vpos;
11078 this_line_start_x = row->x;
11079 }
11080 else
11081 CHARPOS (this_line_start_pos) = 0;
11082 }
11083 }
11084
11085
11086 /* Run window scroll functions, if any, for WINDOW with new window
11087 start STARTP. Sets the window start of WINDOW to that position.
11088
11089 We assume that the window's buffer is really current. */
11090
11091 static INLINE struct text_pos
11092 run_window_scroll_functions (window, startp)
11093 Lisp_Object window;
11094 struct text_pos startp;
11095 {
11096 struct window *w = XWINDOW (window);
11097 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11098
11099 if (current_buffer != XBUFFER (w->buffer))
11100 abort ();
11101
11102 if (!NILP (Vwindow_scroll_functions))
11103 {
11104 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11105 make_number (CHARPOS (startp)));
11106 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11107 /* In case the hook functions switch buffers. */
11108 if (current_buffer != XBUFFER (w->buffer))
11109 set_buffer_internal_1 (XBUFFER (w->buffer));
11110 }
11111
11112 return startp;
11113 }
11114
11115
11116 /* Make sure the line containing the cursor is fully visible.
11117 A value of 1 means there is nothing to be done.
11118 (Either the line is fully visible, or it cannot be made so,
11119 or we cannot tell.)
11120
11121 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11122 is higher than window.
11123
11124 A value of 0 means the caller should do scrolling
11125 as if point had gone off the screen. */
11126
11127 static int
11128 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11129 struct window *w;
11130 int force_p;
11131 {
11132 struct glyph_matrix *matrix;
11133 struct glyph_row *row;
11134 int window_height;
11135
11136 if (!make_cursor_line_fully_visible_p)
11137 return 1;
11138
11139 /* It's not always possible to find the cursor, e.g, when a window
11140 is full of overlay strings. Don't do anything in that case. */
11141 if (w->cursor.vpos < 0)
11142 return 1;
11143
11144 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11145 row = MATRIX_ROW (matrix, w->cursor.vpos);
11146
11147 /* If the cursor row is not partially visible, there's nothing to do. */
11148 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11149 return 1;
11150
11151 /* If the row the cursor is in is taller than the window's height,
11152 it's not clear what to do, so do nothing. */
11153 window_height = window_box_height (w);
11154 if (row->height >= window_height)
11155 {
11156 if (!force_p || w->vscroll)
11157 return 1;
11158 }
11159 return 0;
11160
11161 #if 0
11162 /* This code used to try to scroll the window just enough to make
11163 the line visible. It returned 0 to say that the caller should
11164 allocate larger glyph matrices. */
11165
11166 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11167 {
11168 int dy = row->height - row->visible_height;
11169 w->vscroll = 0;
11170 w->cursor.y += dy;
11171 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11172 }
11173 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11174 {
11175 int dy = - (row->height - row->visible_height);
11176 w->vscroll = dy;
11177 w->cursor.y += dy;
11178 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11179 }
11180
11181 /* When we change the cursor y-position of the selected window,
11182 change this_line_y as well so that the display optimization for
11183 the cursor line of the selected window in redisplay_internal uses
11184 the correct y-position. */
11185 if (w == XWINDOW (selected_window))
11186 this_line_y = w->cursor.y;
11187
11188 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11189 redisplay with larger matrices. */
11190 if (matrix->nrows < required_matrix_height (w))
11191 {
11192 fonts_changed_p = 1;
11193 return 0;
11194 }
11195
11196 return 1;
11197 #endif /* 0 */
11198 }
11199
11200
11201 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11202 non-zero means only WINDOW is redisplayed in redisplay_internal.
11203 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11204 in redisplay_window to bring a partially visible line into view in
11205 the case that only the cursor has moved.
11206
11207 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11208 last screen line's vertical height extends past the end of the screen.
11209
11210 Value is
11211
11212 1 if scrolling succeeded
11213
11214 0 if scrolling didn't find point.
11215
11216 -1 if new fonts have been loaded so that we must interrupt
11217 redisplay, adjust glyph matrices, and try again. */
11218
11219 enum
11220 {
11221 SCROLLING_SUCCESS,
11222 SCROLLING_FAILED,
11223 SCROLLING_NEED_LARGER_MATRICES
11224 };
11225
11226 static int
11227 try_scrolling (window, just_this_one_p, scroll_conservatively,
11228 scroll_step, temp_scroll_step, last_line_misfit)
11229 Lisp_Object window;
11230 int just_this_one_p;
11231 EMACS_INT scroll_conservatively, scroll_step;
11232 int temp_scroll_step;
11233 int last_line_misfit;
11234 {
11235 struct window *w = XWINDOW (window);
11236 struct frame *f = XFRAME (w->frame);
11237 struct text_pos scroll_margin_pos;
11238 struct text_pos pos;
11239 struct text_pos startp;
11240 struct it it;
11241 Lisp_Object window_end;
11242 int this_scroll_margin;
11243 int dy = 0;
11244 int scroll_max;
11245 int rc;
11246 int amount_to_scroll = 0;
11247 Lisp_Object aggressive;
11248 int height;
11249 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11250
11251 #if GLYPH_DEBUG
11252 debug_method_add (w, "try_scrolling");
11253 #endif
11254
11255 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11256
11257 /* Compute scroll margin height in pixels. We scroll when point is
11258 within this distance from the top or bottom of the window. */
11259 if (scroll_margin > 0)
11260 {
11261 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11262 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11263 }
11264 else
11265 this_scroll_margin = 0;
11266
11267 /* Force scroll_conservatively to have a reasonable value so it doesn't
11268 cause an overflow while computing how much to scroll. */
11269 if (scroll_conservatively)
11270 scroll_conservatively = min (scroll_conservatively,
11271 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11272
11273 /* Compute how much we should try to scroll maximally to bring point
11274 into view. */
11275 if (scroll_step || scroll_conservatively || temp_scroll_step)
11276 scroll_max = max (scroll_step,
11277 max (scroll_conservatively, temp_scroll_step));
11278 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11279 || NUMBERP (current_buffer->scroll_up_aggressively))
11280 /* We're trying to scroll because of aggressive scrolling
11281 but no scroll_step is set. Choose an arbitrary one. Maybe
11282 there should be a variable for this. */
11283 scroll_max = 10;
11284 else
11285 scroll_max = 0;
11286 scroll_max *= FRAME_LINE_HEIGHT (f);
11287
11288 /* Decide whether we have to scroll down. Start at the window end
11289 and move this_scroll_margin up to find the position of the scroll
11290 margin. */
11291 window_end = Fwindow_end (window, Qt);
11292
11293 too_near_end:
11294
11295 CHARPOS (scroll_margin_pos) = XINT (window_end);
11296 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11297
11298 if (this_scroll_margin || extra_scroll_margin_lines)
11299 {
11300 start_display (&it, w, scroll_margin_pos);
11301 if (this_scroll_margin)
11302 move_it_vertically_backward (&it, this_scroll_margin);
11303 if (extra_scroll_margin_lines)
11304 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11305 scroll_margin_pos = it.current.pos;
11306 }
11307
11308 if (PT >= CHARPOS (scroll_margin_pos))
11309 {
11310 int y0;
11311
11312 /* Point is in the scroll margin at the bottom of the window, or
11313 below. Compute a new window start that makes point visible. */
11314
11315 /* Compute the distance from the scroll margin to PT.
11316 Give up if the distance is greater than scroll_max. */
11317 start_display (&it, w, scroll_margin_pos);
11318 y0 = it.current_y;
11319 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11320 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11321
11322 /* To make point visible, we have to move the window start
11323 down so that the line the cursor is in is visible, which
11324 means we have to add in the height of the cursor line. */
11325 dy = line_bottom_y (&it) - y0;
11326
11327 if (dy > scroll_max)
11328 return SCROLLING_FAILED;
11329
11330 /* Move the window start down. If scrolling conservatively,
11331 move it just enough down to make point visible. If
11332 scroll_step is set, move it down by scroll_step. */
11333 start_display (&it, w, startp);
11334
11335 if (scroll_conservatively)
11336 /* Set AMOUNT_TO_SCROLL to at least one line,
11337 and at most scroll_conservatively lines. */
11338 amount_to_scroll
11339 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11340 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11341 else if (scroll_step || temp_scroll_step)
11342 amount_to_scroll = scroll_max;
11343 else
11344 {
11345 aggressive = current_buffer->scroll_up_aggressively;
11346 height = WINDOW_BOX_TEXT_HEIGHT (w);
11347 if (NUMBERP (aggressive))
11348 {
11349 double float_amount = XFLOATINT (aggressive) * height;
11350 amount_to_scroll = float_amount;
11351 if (amount_to_scroll == 0 && float_amount > 0)
11352 amount_to_scroll = 1;
11353 }
11354 }
11355
11356 if (amount_to_scroll <= 0)
11357 return SCROLLING_FAILED;
11358
11359 /* If moving by amount_to_scroll leaves STARTP unchanged,
11360 move it down one screen line. */
11361
11362 move_it_vertically (&it, amount_to_scroll);
11363 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11364 move_it_by_lines (&it, 1, 1);
11365 startp = it.current.pos;
11366 }
11367 else
11368 {
11369 /* See if point is inside the scroll margin at the top of the
11370 window. */
11371 scroll_margin_pos = startp;
11372 if (this_scroll_margin)
11373 {
11374 start_display (&it, w, startp);
11375 move_it_vertically (&it, this_scroll_margin);
11376 scroll_margin_pos = it.current.pos;
11377 }
11378
11379 if (PT < CHARPOS (scroll_margin_pos))
11380 {
11381 /* Point is in the scroll margin at the top of the window or
11382 above what is displayed in the window. */
11383 int y0;
11384
11385 /* Compute the vertical distance from PT to the scroll
11386 margin position. Give up if distance is greater than
11387 scroll_max. */
11388 SET_TEXT_POS (pos, PT, PT_BYTE);
11389 start_display (&it, w, pos);
11390 y0 = it.current_y;
11391 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11392 it.last_visible_y, -1,
11393 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11394 dy = it.current_y - y0;
11395 if (dy > scroll_max)
11396 return SCROLLING_FAILED;
11397
11398 /* Compute new window start. */
11399 start_display (&it, w, startp);
11400
11401 if (scroll_conservatively)
11402 amount_to_scroll
11403 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11404 else if (scroll_step || temp_scroll_step)
11405 amount_to_scroll = scroll_max;
11406 else
11407 {
11408 aggressive = current_buffer->scroll_down_aggressively;
11409 height = WINDOW_BOX_TEXT_HEIGHT (w);
11410 if (NUMBERP (aggressive))
11411 {
11412 double float_amount = XFLOATINT (aggressive) * height;
11413 amount_to_scroll = float_amount;
11414 if (amount_to_scroll == 0 && float_amount > 0)
11415 amount_to_scroll = 1;
11416 }
11417 }
11418
11419 if (amount_to_scroll <= 0)
11420 return SCROLLING_FAILED;
11421
11422 move_it_vertically_backward (&it, amount_to_scroll);
11423 startp = it.current.pos;
11424 }
11425 }
11426
11427 /* Run window scroll functions. */
11428 startp = run_window_scroll_functions (window, startp);
11429
11430 /* Display the window. Give up if new fonts are loaded, or if point
11431 doesn't appear. */
11432 if (!try_window (window, startp))
11433 rc = SCROLLING_NEED_LARGER_MATRICES;
11434 else if (w->cursor.vpos < 0)
11435 {
11436 clear_glyph_matrix (w->desired_matrix);
11437 rc = SCROLLING_FAILED;
11438 }
11439 else
11440 {
11441 /* Maybe forget recorded base line for line number display. */
11442 if (!just_this_one_p
11443 || current_buffer->clip_changed
11444 || BEG_UNCHANGED < CHARPOS (startp))
11445 w->base_line_number = Qnil;
11446
11447 /* If cursor ends up on a partially visible line,
11448 treat that as being off the bottom of the screen. */
11449 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11450 {
11451 clear_glyph_matrix (w->desired_matrix);
11452 ++extra_scroll_margin_lines;
11453 goto too_near_end;
11454 }
11455 rc = SCROLLING_SUCCESS;
11456 }
11457
11458 return rc;
11459 }
11460
11461
11462 /* Compute a suitable window start for window W if display of W starts
11463 on a continuation line. Value is non-zero if a new window start
11464 was computed.
11465
11466 The new window start will be computed, based on W's width, starting
11467 from the start of the continued line. It is the start of the
11468 screen line with the minimum distance from the old start W->start. */
11469
11470 static int
11471 compute_window_start_on_continuation_line (w)
11472 struct window *w;
11473 {
11474 struct text_pos pos, start_pos;
11475 int window_start_changed_p = 0;
11476
11477 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11478
11479 /* If window start is on a continuation line... Window start may be
11480 < BEGV in case there's invisible text at the start of the
11481 buffer (M-x rmail, for example). */
11482 if (CHARPOS (start_pos) > BEGV
11483 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11484 {
11485 struct it it;
11486 struct glyph_row *row;
11487
11488 /* Handle the case that the window start is out of range. */
11489 if (CHARPOS (start_pos) < BEGV)
11490 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11491 else if (CHARPOS (start_pos) > ZV)
11492 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11493
11494 /* Find the start of the continued line. This should be fast
11495 because scan_buffer is fast (newline cache). */
11496 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11497 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11498 row, DEFAULT_FACE_ID);
11499 reseat_at_previous_visible_line_start (&it);
11500
11501 /* If the line start is "too far" away from the window start,
11502 say it takes too much time to compute a new window start. */
11503 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11504 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11505 {
11506 int min_distance, distance;
11507
11508 /* Move forward by display lines to find the new window
11509 start. If window width was enlarged, the new start can
11510 be expected to be > the old start. If window width was
11511 decreased, the new window start will be < the old start.
11512 So, we're looking for the display line start with the
11513 minimum distance from the old window start. */
11514 pos = it.current.pos;
11515 min_distance = INFINITY;
11516 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11517 distance < min_distance)
11518 {
11519 min_distance = distance;
11520 pos = it.current.pos;
11521 move_it_by_lines (&it, 1, 0);
11522 }
11523
11524 /* Set the window start there. */
11525 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11526 window_start_changed_p = 1;
11527 }
11528 }
11529
11530 return window_start_changed_p;
11531 }
11532
11533
11534 /* Try cursor movement in case text has not changed in window WINDOW,
11535 with window start STARTP. Value is
11536
11537 CURSOR_MOVEMENT_SUCCESS if successful
11538
11539 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11540
11541 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11542 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11543 we want to scroll as if scroll-step were set to 1. See the code.
11544
11545 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11546 which case we have to abort this redisplay, and adjust matrices
11547 first. */
11548
11549 enum
11550 {
11551 CURSOR_MOVEMENT_SUCCESS,
11552 CURSOR_MOVEMENT_CANNOT_BE_USED,
11553 CURSOR_MOVEMENT_MUST_SCROLL,
11554 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11555 };
11556
11557 static int
11558 try_cursor_movement (window, startp, scroll_step)
11559 Lisp_Object window;
11560 struct text_pos startp;
11561 int *scroll_step;
11562 {
11563 struct window *w = XWINDOW (window);
11564 struct frame *f = XFRAME (w->frame);
11565 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11566
11567 #if GLYPH_DEBUG
11568 if (inhibit_try_cursor_movement)
11569 return rc;
11570 #endif
11571
11572 /* Handle case where text has not changed, only point, and it has
11573 not moved off the frame. */
11574 if (/* Point may be in this window. */
11575 PT >= CHARPOS (startp)
11576 /* Selective display hasn't changed. */
11577 && !current_buffer->clip_changed
11578 /* Function force-mode-line-update is used to force a thorough
11579 redisplay. It sets either windows_or_buffers_changed or
11580 update_mode_lines. So don't take a shortcut here for these
11581 cases. */
11582 && !update_mode_lines
11583 && !windows_or_buffers_changed
11584 && !cursor_type_changed
11585 /* Can't use this case if highlighting a region. When a
11586 region exists, cursor movement has to do more than just
11587 set the cursor. */
11588 && !(!NILP (Vtransient_mark_mode)
11589 && !NILP (current_buffer->mark_active))
11590 && NILP (w->region_showing)
11591 && NILP (Vshow_trailing_whitespace)
11592 /* Right after splitting windows, last_point may be nil. */
11593 && INTEGERP (w->last_point)
11594 /* This code is not used for mini-buffer for the sake of the case
11595 of redisplaying to replace an echo area message; since in
11596 that case the mini-buffer contents per se are usually
11597 unchanged. This code is of no real use in the mini-buffer
11598 since the handling of this_line_start_pos, etc., in redisplay
11599 handles the same cases. */
11600 && !EQ (window, minibuf_window)
11601 /* When splitting windows or for new windows, it happens that
11602 redisplay is called with a nil window_end_vpos or one being
11603 larger than the window. This should really be fixed in
11604 window.c. I don't have this on my list, now, so we do
11605 approximately the same as the old redisplay code. --gerd. */
11606 && INTEGERP (w->window_end_vpos)
11607 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11608 && (FRAME_WINDOW_P (f)
11609 || !overlay_arrow_in_current_buffer_p ()))
11610 {
11611 int this_scroll_margin, top_scroll_margin;
11612 struct glyph_row *row = NULL;
11613
11614 #if GLYPH_DEBUG
11615 debug_method_add (w, "cursor movement");
11616 #endif
11617
11618 /* Scroll if point within this distance from the top or bottom
11619 of the window. This is a pixel value. */
11620 this_scroll_margin = max (0, scroll_margin);
11621 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11622 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11623
11624 top_scroll_margin = this_scroll_margin;
11625 if (WINDOW_WANTS_HEADER_LINE_P (w))
11626 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11627
11628 /* Start with the row the cursor was displayed during the last
11629 not paused redisplay. Give up if that row is not valid. */
11630 if (w->last_cursor.vpos < 0
11631 || w->last_cursor.vpos >= w->current_matrix->nrows)
11632 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11633 else
11634 {
11635 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11636 if (row->mode_line_p)
11637 ++row;
11638 if (!row->enabled_p)
11639 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11640 }
11641
11642 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11643 {
11644 int scroll_p = 0;
11645 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11646
11647 if (PT > XFASTINT (w->last_point))
11648 {
11649 /* Point has moved forward. */
11650 while (MATRIX_ROW_END_CHARPOS (row) < PT
11651 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11652 {
11653 xassert (row->enabled_p);
11654 ++row;
11655 }
11656
11657 /* The end position of a row equals the start position
11658 of the next row. If PT is there, we would rather
11659 display it in the next line. */
11660 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11661 && MATRIX_ROW_END_CHARPOS (row) == PT
11662 && !cursor_row_p (w, row))
11663 ++row;
11664
11665 /* If within the scroll margin, scroll. Note that
11666 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11667 the next line would be drawn, and that
11668 this_scroll_margin can be zero. */
11669 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11670 || PT > MATRIX_ROW_END_CHARPOS (row)
11671 /* Line is completely visible last line in window
11672 and PT is to be set in the next line. */
11673 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11674 && PT == MATRIX_ROW_END_CHARPOS (row)
11675 && !row->ends_at_zv_p
11676 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11677 scroll_p = 1;
11678 }
11679 else if (PT < XFASTINT (w->last_point))
11680 {
11681 /* Cursor has to be moved backward. Note that PT >=
11682 CHARPOS (startp) because of the outer if-statement. */
11683 while (!row->mode_line_p
11684 && (MATRIX_ROW_START_CHARPOS (row) > PT
11685 || (MATRIX_ROW_START_CHARPOS (row) == PT
11686 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11687 && (row->y > top_scroll_margin
11688 || CHARPOS (startp) == BEGV))
11689 {
11690 xassert (row->enabled_p);
11691 --row;
11692 }
11693
11694 /* Consider the following case: Window starts at BEGV,
11695 there is invisible, intangible text at BEGV, so that
11696 display starts at some point START > BEGV. It can
11697 happen that we are called with PT somewhere between
11698 BEGV and START. Try to handle that case. */
11699 if (row < w->current_matrix->rows
11700 || row->mode_line_p)
11701 {
11702 row = w->current_matrix->rows;
11703 if (row->mode_line_p)
11704 ++row;
11705 }
11706
11707 /* Due to newlines in overlay strings, we may have to
11708 skip forward over overlay strings. */
11709 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11710 && MATRIX_ROW_END_CHARPOS (row) == PT
11711 && !cursor_row_p (w, row))
11712 ++row;
11713
11714 /* If within the scroll margin, scroll. */
11715 if (row->y < top_scroll_margin
11716 && CHARPOS (startp) != BEGV)
11717 scroll_p = 1;
11718 }
11719 else
11720 {
11721 /* Cursor did not move. So don't scroll even if cursor line
11722 is partially visible, as it was so before. */
11723 rc = CURSOR_MOVEMENT_SUCCESS;
11724 }
11725
11726 if (PT < MATRIX_ROW_START_CHARPOS (row)
11727 || PT > MATRIX_ROW_END_CHARPOS (row))
11728 {
11729 /* if PT is not in the glyph row, give up. */
11730 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11731 }
11732 else if (rc != CURSOR_MOVEMENT_SUCCESS
11733 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11734 && make_cursor_line_fully_visible_p)
11735 {
11736 if (PT == MATRIX_ROW_END_CHARPOS (row)
11737 && !row->ends_at_zv_p
11738 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11739 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11740 else if (row->height > window_box_height (w))
11741 {
11742 /* If we end up in a partially visible line, let's
11743 make it fully visible, except when it's taller
11744 than the window, in which case we can't do much
11745 about it. */
11746 *scroll_step = 1;
11747 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11748 }
11749 else
11750 {
11751 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11752 if (!cursor_row_fully_visible_p (w, 0, 1))
11753 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11754 else
11755 rc = CURSOR_MOVEMENT_SUCCESS;
11756 }
11757 }
11758 else if (scroll_p)
11759 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11760 else
11761 {
11762 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11763 rc = CURSOR_MOVEMENT_SUCCESS;
11764 }
11765 }
11766 }
11767
11768 return rc;
11769 }
11770
11771 void
11772 set_vertical_scroll_bar (w)
11773 struct window *w;
11774 {
11775 int start, end, whole;
11776
11777 /* Calculate the start and end positions for the current window.
11778 At some point, it would be nice to choose between scrollbars
11779 which reflect the whole buffer size, with special markers
11780 indicating narrowing, and scrollbars which reflect only the
11781 visible region.
11782
11783 Note that mini-buffers sometimes aren't displaying any text. */
11784 if (!MINI_WINDOW_P (w)
11785 || (w == XWINDOW (minibuf_window)
11786 && NILP (echo_area_buffer[0])))
11787 {
11788 struct buffer *buf = XBUFFER (w->buffer);
11789 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11790 start = marker_position (w->start) - BUF_BEGV (buf);
11791 /* I don't think this is guaranteed to be right. For the
11792 moment, we'll pretend it is. */
11793 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11794
11795 if (end < start)
11796 end = start;
11797 if (whole < (end - start))
11798 whole = end - start;
11799 }
11800 else
11801 start = end = whole = 0;
11802
11803 /* Indicate what this scroll bar ought to be displaying now. */
11804 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11805 }
11806
11807
11808 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11809 selected_window is redisplayed.
11810
11811 We can return without actually redisplaying the window if
11812 fonts_changed_p is nonzero. In that case, redisplay_internal will
11813 retry. */
11814
11815 static void
11816 redisplay_window (window, just_this_one_p)
11817 Lisp_Object window;
11818 int just_this_one_p;
11819 {
11820 struct window *w = XWINDOW (window);
11821 struct frame *f = XFRAME (w->frame);
11822 struct buffer *buffer = XBUFFER (w->buffer);
11823 struct buffer *old = current_buffer;
11824 struct text_pos lpoint, opoint, startp;
11825 int update_mode_line;
11826 int tem;
11827 struct it it;
11828 /* Record it now because it's overwritten. */
11829 int current_matrix_up_to_date_p = 0;
11830 int used_current_matrix_p = 0;
11831 /* This is less strict than current_matrix_up_to_date_p.
11832 It indictes that the buffer contents and narrowing are unchanged. */
11833 int buffer_unchanged_p = 0;
11834 int temp_scroll_step = 0;
11835 int count = SPECPDL_INDEX ();
11836 int rc;
11837 int centering_position = -1;
11838 int last_line_misfit = 0;
11839
11840 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11841 opoint = lpoint;
11842
11843 /* W must be a leaf window here. */
11844 xassert (!NILP (w->buffer));
11845 #if GLYPH_DEBUG
11846 *w->desired_matrix->method = 0;
11847 #endif
11848
11849 specbind (Qinhibit_point_motion_hooks, Qt);
11850
11851 reconsider_clip_changes (w, buffer);
11852
11853 /* Has the mode line to be updated? */
11854 update_mode_line = (!NILP (w->update_mode_line)
11855 || update_mode_lines
11856 || buffer->clip_changed
11857 || buffer->prevent_redisplay_optimizations_p);
11858
11859 if (MINI_WINDOW_P (w))
11860 {
11861 if (w == XWINDOW (echo_area_window)
11862 && !NILP (echo_area_buffer[0]))
11863 {
11864 if (update_mode_line)
11865 /* We may have to update a tty frame's menu bar or a
11866 tool-bar. Example `M-x C-h C-h C-g'. */
11867 goto finish_menu_bars;
11868 else
11869 /* We've already displayed the echo area glyphs in this window. */
11870 goto finish_scroll_bars;
11871 }
11872 else if ((w != XWINDOW (minibuf_window)
11873 || minibuf_level == 0)
11874 /* When buffer is nonempty, redisplay window normally. */
11875 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11876 /* Quail displays non-mini buffers in minibuffer window.
11877 In that case, redisplay the window normally. */
11878 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11879 {
11880 /* W is a mini-buffer window, but it's not active, so clear
11881 it. */
11882 int yb = window_text_bottom_y (w);
11883 struct glyph_row *row;
11884 int y;
11885
11886 for (y = 0, row = w->desired_matrix->rows;
11887 y < yb;
11888 y += row->height, ++row)
11889 blank_row (w, row, y);
11890 goto finish_scroll_bars;
11891 }
11892
11893 clear_glyph_matrix (w->desired_matrix);
11894 }
11895
11896 /* Otherwise set up data on this window; select its buffer and point
11897 value. */
11898 /* Really select the buffer, for the sake of buffer-local
11899 variables. */
11900 set_buffer_internal_1 (XBUFFER (w->buffer));
11901 SET_TEXT_POS (opoint, PT, PT_BYTE);
11902
11903 current_matrix_up_to_date_p
11904 = (!NILP (w->window_end_valid)
11905 && !current_buffer->clip_changed
11906 && !current_buffer->prevent_redisplay_optimizations_p
11907 && XFASTINT (w->last_modified) >= MODIFF
11908 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11909
11910 buffer_unchanged_p
11911 = (!NILP (w->window_end_valid)
11912 && !current_buffer->clip_changed
11913 && XFASTINT (w->last_modified) >= MODIFF
11914 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11915
11916 /* When windows_or_buffers_changed is non-zero, we can't rely on
11917 the window end being valid, so set it to nil there. */
11918 if (windows_or_buffers_changed)
11919 {
11920 /* If window starts on a continuation line, maybe adjust the
11921 window start in case the window's width changed. */
11922 if (XMARKER (w->start)->buffer == current_buffer)
11923 compute_window_start_on_continuation_line (w);
11924
11925 w->window_end_valid = Qnil;
11926 }
11927
11928 /* Some sanity checks. */
11929 CHECK_WINDOW_END (w);
11930 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11931 abort ();
11932 if (BYTEPOS (opoint) < CHARPOS (opoint))
11933 abort ();
11934
11935 /* If %c is in mode line, update it if needed. */
11936 if (!NILP (w->column_number_displayed)
11937 /* This alternative quickly identifies a common case
11938 where no change is needed. */
11939 && !(PT == XFASTINT (w->last_point)
11940 && XFASTINT (w->last_modified) >= MODIFF
11941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11942 && (XFASTINT (w->column_number_displayed)
11943 != (int) current_column ())) /* iftc */
11944 update_mode_line = 1;
11945
11946 /* Count number of windows showing the selected buffer. An indirect
11947 buffer counts as its base buffer. */
11948 if (!just_this_one_p)
11949 {
11950 struct buffer *current_base, *window_base;
11951 current_base = current_buffer;
11952 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11953 if (current_base->base_buffer)
11954 current_base = current_base->base_buffer;
11955 if (window_base->base_buffer)
11956 window_base = window_base->base_buffer;
11957 if (current_base == window_base)
11958 buffer_shared++;
11959 }
11960
11961 /* Point refers normally to the selected window. For any other
11962 window, set up appropriate value. */
11963 if (!EQ (window, selected_window))
11964 {
11965 int new_pt = XMARKER (w->pointm)->charpos;
11966 int new_pt_byte = marker_byte_position (w->pointm);
11967 if (new_pt < BEGV)
11968 {
11969 new_pt = BEGV;
11970 new_pt_byte = BEGV_BYTE;
11971 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11972 }
11973 else if (new_pt > (ZV - 1))
11974 {
11975 new_pt = ZV;
11976 new_pt_byte = ZV_BYTE;
11977 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11978 }
11979
11980 /* We don't use SET_PT so that the point-motion hooks don't run. */
11981 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11982 }
11983
11984 /* If any of the character widths specified in the display table
11985 have changed, invalidate the width run cache. It's true that
11986 this may be a bit late to catch such changes, but the rest of
11987 redisplay goes (non-fatally) haywire when the display table is
11988 changed, so why should we worry about doing any better? */
11989 if (current_buffer->width_run_cache)
11990 {
11991 struct Lisp_Char_Table *disptab = buffer_display_table ();
11992
11993 if (! disptab_matches_widthtab (disptab,
11994 XVECTOR (current_buffer->width_table)))
11995 {
11996 invalidate_region_cache (current_buffer,
11997 current_buffer->width_run_cache,
11998 BEG, Z);
11999 recompute_width_table (current_buffer, disptab);
12000 }
12001 }
12002
12003 /* If window-start is screwed up, choose a new one. */
12004 if (XMARKER (w->start)->buffer != current_buffer)
12005 goto recenter;
12006
12007 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12008
12009 /* If someone specified a new starting point but did not insist,
12010 check whether it can be used. */
12011 if (!NILP (w->optional_new_start)
12012 && CHARPOS (startp) >= BEGV
12013 && CHARPOS (startp) <= ZV)
12014 {
12015 w->optional_new_start = Qnil;
12016 start_display (&it, w, startp);
12017 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12018 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12019 if (IT_CHARPOS (it) == PT)
12020 w->force_start = Qt;
12021 /* IT may overshoot PT if text at PT is invisible. */
12022 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12023 w->force_start = Qt;
12024
12025
12026 }
12027
12028 /* Handle case where place to start displaying has been specified,
12029 unless the specified location is outside the accessible range. */
12030 if (!NILP (w->force_start)
12031 || w->frozen_window_start_p)
12032 {
12033 /* We set this later on if we have to adjust point. */
12034 int new_vpos = -1;
12035
12036 w->force_start = Qnil;
12037 w->vscroll = 0;
12038 w->window_end_valid = Qnil;
12039
12040 /* Forget any recorded base line for line number display. */
12041 if (!buffer_unchanged_p)
12042 w->base_line_number = Qnil;
12043
12044 /* Redisplay the mode line. Select the buffer properly for that.
12045 Also, run the hook window-scroll-functions
12046 because we have scrolled. */
12047 /* Note, we do this after clearing force_start because
12048 if there's an error, it is better to forget about force_start
12049 than to get into an infinite loop calling the hook functions
12050 and having them get more errors. */
12051 if (!update_mode_line
12052 || ! NILP (Vwindow_scroll_functions))
12053 {
12054 update_mode_line = 1;
12055 w->update_mode_line = Qt;
12056 startp = run_window_scroll_functions (window, startp);
12057 }
12058
12059 w->last_modified = make_number (0);
12060 w->last_overlay_modified = make_number (0);
12061 if (CHARPOS (startp) < BEGV)
12062 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12063 else if (CHARPOS (startp) > ZV)
12064 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12065
12066 /* Redisplay, then check if cursor has been set during the
12067 redisplay. Give up if new fonts were loaded. */
12068 if (!try_window (window, startp))
12069 {
12070 w->force_start = Qt;
12071 clear_glyph_matrix (w->desired_matrix);
12072 goto need_larger_matrices;
12073 }
12074
12075 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12076 {
12077 /* If point does not appear, try to move point so it does
12078 appear. The desired matrix has been built above, so we
12079 can use it here. */
12080 new_vpos = window_box_height (w) / 2;
12081 }
12082
12083 if (!cursor_row_fully_visible_p (w, 0, 0))
12084 {
12085 /* Point does appear, but on a line partly visible at end of window.
12086 Move it back to a fully-visible line. */
12087 new_vpos = window_box_height (w);
12088 }
12089
12090 /* If we need to move point for either of the above reasons,
12091 now actually do it. */
12092 if (new_vpos >= 0)
12093 {
12094 struct glyph_row *row;
12095
12096 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12097 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12098 ++row;
12099
12100 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12101 MATRIX_ROW_START_BYTEPOS (row));
12102
12103 if (w != XWINDOW (selected_window))
12104 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12105 else if (current_buffer == old)
12106 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12107
12108 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12109
12110 /* If we are highlighting the region, then we just changed
12111 the region, so redisplay to show it. */
12112 if (!NILP (Vtransient_mark_mode)
12113 && !NILP (current_buffer->mark_active))
12114 {
12115 clear_glyph_matrix (w->desired_matrix);
12116 if (!try_window (window, startp))
12117 goto need_larger_matrices;
12118 }
12119 }
12120
12121 #if GLYPH_DEBUG
12122 debug_method_add (w, "forced window start");
12123 #endif
12124 goto done;
12125 }
12126
12127 /* Handle case where text has not changed, only point, and it has
12128 not moved off the frame, and we are not retrying after hscroll.
12129 (current_matrix_up_to_date_p is nonzero when retrying.) */
12130 if (current_matrix_up_to_date_p
12131 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12132 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12133 {
12134 switch (rc)
12135 {
12136 case CURSOR_MOVEMENT_SUCCESS:
12137 used_current_matrix_p = 1;
12138 goto done;
12139
12140 #if 0 /* try_cursor_movement never returns this value. */
12141 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12142 goto need_larger_matrices;
12143 #endif
12144
12145 case CURSOR_MOVEMENT_MUST_SCROLL:
12146 goto try_to_scroll;
12147
12148 default:
12149 abort ();
12150 }
12151 }
12152 /* If current starting point was originally the beginning of a line
12153 but no longer is, find a new starting point. */
12154 else if (!NILP (w->start_at_line_beg)
12155 && !(CHARPOS (startp) <= BEGV
12156 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12157 {
12158 #if GLYPH_DEBUG
12159 debug_method_add (w, "recenter 1");
12160 #endif
12161 goto recenter;
12162 }
12163
12164 /* Try scrolling with try_window_id. Value is > 0 if update has
12165 been done, it is -1 if we know that the same window start will
12166 not work. It is 0 if unsuccessful for some other reason. */
12167 else if ((tem = try_window_id (w)) != 0)
12168 {
12169 #if GLYPH_DEBUG
12170 debug_method_add (w, "try_window_id %d", tem);
12171 #endif
12172
12173 if (fonts_changed_p)
12174 goto need_larger_matrices;
12175 if (tem > 0)
12176 goto done;
12177
12178 /* Otherwise try_window_id has returned -1 which means that we
12179 don't want the alternative below this comment to execute. */
12180 }
12181 else if (CHARPOS (startp) >= BEGV
12182 && CHARPOS (startp) <= ZV
12183 && PT >= CHARPOS (startp)
12184 && (CHARPOS (startp) < ZV
12185 /* Avoid starting at end of buffer. */
12186 || CHARPOS (startp) == BEGV
12187 || (XFASTINT (w->last_modified) >= MODIFF
12188 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12189 {
12190 #if GLYPH_DEBUG
12191 debug_method_add (w, "same window start");
12192 #endif
12193
12194 /* Try to redisplay starting at same place as before.
12195 If point has not moved off frame, accept the results. */
12196 if (!current_matrix_up_to_date_p
12197 /* Don't use try_window_reusing_current_matrix in this case
12198 because a window scroll function can have changed the
12199 buffer. */
12200 || !NILP (Vwindow_scroll_functions)
12201 || MINI_WINDOW_P (w)
12202 || !(used_current_matrix_p
12203 = try_window_reusing_current_matrix (w)))
12204 {
12205 IF_DEBUG (debug_method_add (w, "1"));
12206 try_window (window, startp);
12207 }
12208
12209 if (fonts_changed_p)
12210 goto need_larger_matrices;
12211
12212 if (w->cursor.vpos >= 0)
12213 {
12214 if (!just_this_one_p
12215 || current_buffer->clip_changed
12216 || BEG_UNCHANGED < CHARPOS (startp))
12217 /* Forget any recorded base line for line number display. */
12218 w->base_line_number = Qnil;
12219
12220 if (!cursor_row_fully_visible_p (w, 1, 0))
12221 {
12222 clear_glyph_matrix (w->desired_matrix);
12223 last_line_misfit = 1;
12224 }
12225 /* Drop through and scroll. */
12226 else
12227 goto done;
12228 }
12229 else
12230 clear_glyph_matrix (w->desired_matrix);
12231 }
12232
12233 try_to_scroll:
12234
12235 w->last_modified = make_number (0);
12236 w->last_overlay_modified = make_number (0);
12237
12238 /* Redisplay the mode line. Select the buffer properly for that. */
12239 if (!update_mode_line)
12240 {
12241 update_mode_line = 1;
12242 w->update_mode_line = Qt;
12243 }
12244
12245 /* Try to scroll by specified few lines. */
12246 if ((scroll_conservatively
12247 || scroll_step
12248 || temp_scroll_step
12249 || NUMBERP (current_buffer->scroll_up_aggressively)
12250 || NUMBERP (current_buffer->scroll_down_aggressively))
12251 && !current_buffer->clip_changed
12252 && CHARPOS (startp) >= BEGV
12253 && CHARPOS (startp) <= ZV)
12254 {
12255 /* The function returns -1 if new fonts were loaded, 1 if
12256 successful, 0 if not successful. */
12257 int rc = try_scrolling (window, just_this_one_p,
12258 scroll_conservatively,
12259 scroll_step,
12260 temp_scroll_step, last_line_misfit);
12261 switch (rc)
12262 {
12263 case SCROLLING_SUCCESS:
12264 goto done;
12265
12266 case SCROLLING_NEED_LARGER_MATRICES:
12267 goto need_larger_matrices;
12268
12269 case SCROLLING_FAILED:
12270 break;
12271
12272 default:
12273 abort ();
12274 }
12275 }
12276
12277 /* Finally, just choose place to start which centers point */
12278
12279 recenter:
12280 if (centering_position < 0)
12281 centering_position = window_box_height (w) / 2;
12282
12283 #if GLYPH_DEBUG
12284 debug_method_add (w, "recenter");
12285 #endif
12286
12287 /* w->vscroll = 0; */
12288
12289 /* Forget any previously recorded base line for line number display. */
12290 if (!buffer_unchanged_p)
12291 w->base_line_number = Qnil;
12292
12293 /* Move backward half the height of the window. */
12294 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12295 it.current_y = it.last_visible_y;
12296 move_it_vertically_backward (&it, centering_position);
12297 xassert (IT_CHARPOS (it) >= BEGV);
12298
12299 /* The function move_it_vertically_backward may move over more
12300 than the specified y-distance. If it->w is small, e.g. a
12301 mini-buffer window, we may end up in front of the window's
12302 display area. Start displaying at the start of the line
12303 containing PT in this case. */
12304 if (it.current_y <= 0)
12305 {
12306 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12307 move_it_vertically_backward (&it, 0);
12308 #if 0
12309 /* I think this assert is bogus if buffer contains
12310 invisible text or images. KFS. */
12311 xassert (IT_CHARPOS (it) <= PT);
12312 #endif
12313 it.current_y = 0;
12314 }
12315
12316 it.current_x = it.hpos = 0;
12317
12318 /* Set startp here explicitly in case that helps avoid an infinite loop
12319 in case the window-scroll-functions functions get errors. */
12320 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12321
12322 /* Run scroll hooks. */
12323 startp = run_window_scroll_functions (window, it.current.pos);
12324
12325 /* Redisplay the window. */
12326 if (!current_matrix_up_to_date_p
12327 || windows_or_buffers_changed
12328 || cursor_type_changed
12329 /* Don't use try_window_reusing_current_matrix in this case
12330 because it can have changed the buffer. */
12331 || !NILP (Vwindow_scroll_functions)
12332 || !just_this_one_p
12333 || MINI_WINDOW_P (w)
12334 || !(used_current_matrix_p
12335 = try_window_reusing_current_matrix (w)))
12336 try_window (window, startp);
12337
12338 /* If new fonts have been loaded (due to fontsets), give up. We
12339 have to start a new redisplay since we need to re-adjust glyph
12340 matrices. */
12341 if (fonts_changed_p)
12342 goto need_larger_matrices;
12343
12344 /* If cursor did not appear assume that the middle of the window is
12345 in the first line of the window. Do it again with the next line.
12346 (Imagine a window of height 100, displaying two lines of height
12347 60. Moving back 50 from it->last_visible_y will end in the first
12348 line.) */
12349 if (w->cursor.vpos < 0)
12350 {
12351 if (!NILP (w->window_end_valid)
12352 && PT >= Z - XFASTINT (w->window_end_pos))
12353 {
12354 clear_glyph_matrix (w->desired_matrix);
12355 move_it_by_lines (&it, 1, 0);
12356 try_window (window, it.current.pos);
12357 }
12358 else if (PT < IT_CHARPOS (it))
12359 {
12360 clear_glyph_matrix (w->desired_matrix);
12361 move_it_by_lines (&it, -1, 0);
12362 try_window (window, it.current.pos);
12363 }
12364 else
12365 {
12366 /* Not much we can do about it. */
12367 }
12368 }
12369
12370 /* Consider the following case: Window starts at BEGV, there is
12371 invisible, intangible text at BEGV, so that display starts at
12372 some point START > BEGV. It can happen that we are called with
12373 PT somewhere between BEGV and START. Try to handle that case. */
12374 if (w->cursor.vpos < 0)
12375 {
12376 struct glyph_row *row = w->current_matrix->rows;
12377 if (row->mode_line_p)
12378 ++row;
12379 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12380 }
12381
12382 if (!cursor_row_fully_visible_p (w, 0, 0))
12383 {
12384 /* If vscroll is enabled, disable it and try again. */
12385 if (w->vscroll)
12386 {
12387 w->vscroll = 0;
12388 clear_glyph_matrix (w->desired_matrix);
12389 goto recenter;
12390 }
12391
12392 /* If centering point failed to make the whole line visible,
12393 put point at the top instead. That has to make the whole line
12394 visible, if it can be done. */
12395 if (centering_position == 0)
12396 goto done;
12397
12398 clear_glyph_matrix (w->desired_matrix);
12399 centering_position = 0;
12400 goto recenter;
12401 }
12402
12403 done:
12404
12405 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12406 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12407 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12408 ? Qt : Qnil);
12409
12410 /* Display the mode line, if we must. */
12411 if ((update_mode_line
12412 /* If window not full width, must redo its mode line
12413 if (a) the window to its side is being redone and
12414 (b) we do a frame-based redisplay. This is a consequence
12415 of how inverted lines are drawn in frame-based redisplay. */
12416 || (!just_this_one_p
12417 && !FRAME_WINDOW_P (f)
12418 && !WINDOW_FULL_WIDTH_P (w))
12419 /* Line number to display. */
12420 || INTEGERP (w->base_line_pos)
12421 /* Column number is displayed and different from the one displayed. */
12422 || (!NILP (w->column_number_displayed)
12423 && (XFASTINT (w->column_number_displayed)
12424 != (int) current_column ()))) /* iftc */
12425 /* This means that the window has a mode line. */
12426 && (WINDOW_WANTS_MODELINE_P (w)
12427 || WINDOW_WANTS_HEADER_LINE_P (w)))
12428 {
12429 display_mode_lines (w);
12430
12431 /* If mode line height has changed, arrange for a thorough
12432 immediate redisplay using the correct mode line height. */
12433 if (WINDOW_WANTS_MODELINE_P (w)
12434 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12435 {
12436 fonts_changed_p = 1;
12437 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12438 = DESIRED_MODE_LINE_HEIGHT (w);
12439 }
12440
12441 /* If top line height has changed, arrange for a thorough
12442 immediate redisplay using the correct mode line height. */
12443 if (WINDOW_WANTS_HEADER_LINE_P (w)
12444 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12445 {
12446 fonts_changed_p = 1;
12447 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12448 = DESIRED_HEADER_LINE_HEIGHT (w);
12449 }
12450
12451 if (fonts_changed_p)
12452 goto need_larger_matrices;
12453 }
12454
12455 if (!line_number_displayed
12456 && !BUFFERP (w->base_line_pos))
12457 {
12458 w->base_line_pos = Qnil;
12459 w->base_line_number = Qnil;
12460 }
12461
12462 finish_menu_bars:
12463
12464 /* When we reach a frame's selected window, redo the frame's menu bar. */
12465 if (update_mode_line
12466 && EQ (FRAME_SELECTED_WINDOW (f), window))
12467 {
12468 int redisplay_menu_p = 0;
12469 int redisplay_tool_bar_p = 0;
12470
12471 if (FRAME_WINDOW_P (f))
12472 {
12473 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12474 || defined (USE_GTK)
12475 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12476 #else
12477 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12478 #endif
12479 }
12480 else
12481 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12482
12483 if (redisplay_menu_p)
12484 display_menu_bar (w);
12485
12486 #ifdef HAVE_WINDOW_SYSTEM
12487 #ifdef USE_GTK
12488 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12489 #else
12490 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12491 && (FRAME_TOOL_BAR_LINES (f) > 0
12492 || auto_resize_tool_bars_p);
12493
12494 #endif
12495
12496 if (redisplay_tool_bar_p)
12497 redisplay_tool_bar (f);
12498 #endif
12499 }
12500
12501 #ifdef HAVE_WINDOW_SYSTEM
12502 if (FRAME_WINDOW_P (f)
12503 && update_window_fringes (w, 0)
12504 && !just_this_one_p
12505 && (used_current_matrix_p || overlay_arrow_seen)
12506 && !w->pseudo_window_p)
12507 {
12508 update_begin (f);
12509 BLOCK_INPUT;
12510 if (draw_window_fringes (w, 1))
12511 x_draw_vertical_border (w);
12512 UNBLOCK_INPUT;
12513 update_end (f);
12514 }
12515 #endif /* HAVE_WINDOW_SYSTEM */
12516
12517 /* We go to this label, with fonts_changed_p nonzero,
12518 if it is necessary to try again using larger glyph matrices.
12519 We have to redeem the scroll bar even in this case,
12520 because the loop in redisplay_internal expects that. */
12521 need_larger_matrices:
12522 ;
12523 finish_scroll_bars:
12524
12525 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12526 {
12527 /* Set the thumb's position and size. */
12528 set_vertical_scroll_bar (w);
12529
12530 /* Note that we actually used the scroll bar attached to this
12531 window, so it shouldn't be deleted at the end of redisplay. */
12532 redeem_scroll_bar_hook (w);
12533 }
12534
12535 /* Restore current_buffer and value of point in it. */
12536 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12537 set_buffer_internal_1 (old);
12538 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12539
12540 unbind_to (count, Qnil);
12541 }
12542
12543
12544 /* Build the complete desired matrix of WINDOW with a window start
12545 buffer position POS. Value is non-zero if successful. It is zero
12546 if fonts were loaded during redisplay which makes re-adjusting
12547 glyph matrices necessary. */
12548
12549 int
12550 try_window (window, pos)
12551 Lisp_Object window;
12552 struct text_pos pos;
12553 {
12554 struct window *w = XWINDOW (window);
12555 struct it it;
12556 struct glyph_row *last_text_row = NULL;
12557
12558 /* Make POS the new window start. */
12559 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12560
12561 /* Mark cursor position as unknown. No overlay arrow seen. */
12562 w->cursor.vpos = -1;
12563 overlay_arrow_seen = 0;
12564
12565 /* Initialize iterator and info to start at POS. */
12566 start_display (&it, w, pos);
12567
12568 /* Display all lines of W. */
12569 while (it.current_y < it.last_visible_y)
12570 {
12571 if (display_line (&it))
12572 last_text_row = it.glyph_row - 1;
12573 if (fonts_changed_p)
12574 return 0;
12575 }
12576
12577 /* If bottom moved off end of frame, change mode line percentage. */
12578 if (XFASTINT (w->window_end_pos) <= 0
12579 && Z != IT_CHARPOS (it))
12580 w->update_mode_line = Qt;
12581
12582 /* Set window_end_pos to the offset of the last character displayed
12583 on the window from the end of current_buffer. Set
12584 window_end_vpos to its row number. */
12585 if (last_text_row)
12586 {
12587 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12588 w->window_end_bytepos
12589 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12590 w->window_end_pos
12591 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12592 w->window_end_vpos
12593 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12594 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12595 ->displays_text_p);
12596 }
12597 else
12598 {
12599 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12600 w->window_end_pos = make_number (Z - ZV);
12601 w->window_end_vpos = make_number (0);
12602 }
12603
12604 /* But that is not valid info until redisplay finishes. */
12605 w->window_end_valid = Qnil;
12606 return 1;
12607 }
12608
12609
12610 \f
12611 /************************************************************************
12612 Window redisplay reusing current matrix when buffer has not changed
12613 ************************************************************************/
12614
12615 /* Try redisplay of window W showing an unchanged buffer with a
12616 different window start than the last time it was displayed by
12617 reusing its current matrix. Value is non-zero if successful.
12618 W->start is the new window start. */
12619
12620 static int
12621 try_window_reusing_current_matrix (w)
12622 struct window *w;
12623 {
12624 struct frame *f = XFRAME (w->frame);
12625 struct glyph_row *row, *bottom_row;
12626 struct it it;
12627 struct run run;
12628 struct text_pos start, new_start;
12629 int nrows_scrolled, i;
12630 struct glyph_row *last_text_row;
12631 struct glyph_row *last_reused_text_row;
12632 struct glyph_row *start_row;
12633 int start_vpos, min_y, max_y;
12634
12635 #if GLYPH_DEBUG
12636 if (inhibit_try_window_reusing)
12637 return 0;
12638 #endif
12639
12640 if (/* This function doesn't handle terminal frames. */
12641 !FRAME_WINDOW_P (f)
12642 /* Don't try to reuse the display if windows have been split
12643 or such. */
12644 || windows_or_buffers_changed
12645 || cursor_type_changed)
12646 return 0;
12647
12648 /* Can't do this if region may have changed. */
12649 if ((!NILP (Vtransient_mark_mode)
12650 && !NILP (current_buffer->mark_active))
12651 || !NILP (w->region_showing)
12652 || !NILP (Vshow_trailing_whitespace))
12653 return 0;
12654
12655 /* If top-line visibility has changed, give up. */
12656 if (WINDOW_WANTS_HEADER_LINE_P (w)
12657 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12658 return 0;
12659
12660 /* Give up if old or new display is scrolled vertically. We could
12661 make this function handle this, but right now it doesn't. */
12662 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12663 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12664 return 0;
12665
12666 /* The variable new_start now holds the new window start. The old
12667 start `start' can be determined from the current matrix. */
12668 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12669 start = start_row->start.pos;
12670 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12671
12672 /* Clear the desired matrix for the display below. */
12673 clear_glyph_matrix (w->desired_matrix);
12674
12675 if (CHARPOS (new_start) <= CHARPOS (start))
12676 {
12677 int first_row_y;
12678
12679 /* Don't use this method if the display starts with an ellipsis
12680 displayed for invisible text. It's not easy to handle that case
12681 below, and it's certainly not worth the effort since this is
12682 not a frequent case. */
12683 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12684 return 0;
12685
12686 IF_DEBUG (debug_method_add (w, "twu1"));
12687
12688 /* Display up to a row that can be reused. The variable
12689 last_text_row is set to the last row displayed that displays
12690 text. Note that it.vpos == 0 if or if not there is a
12691 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12692 start_display (&it, w, new_start);
12693 first_row_y = it.current_y;
12694 w->cursor.vpos = -1;
12695 last_text_row = last_reused_text_row = NULL;
12696
12697 while (it.current_y < it.last_visible_y
12698 && !fonts_changed_p)
12699 {
12700 /* If we have reached into the characters in the START row,
12701 that means the line boundaries have changed. So we
12702 can't start copying with the row START. Maybe it will
12703 work to start copying with the following row. */
12704 while (IT_CHARPOS (it) > CHARPOS (start))
12705 {
12706 /* Advance to the next row as the "start". */
12707 start_row++;
12708 start = start_row->start.pos;
12709 /* If there are no more rows to try, or just one, give up. */
12710 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12711 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12712 || CHARPOS (start) == ZV)
12713 {
12714 clear_glyph_matrix (w->desired_matrix);
12715 return 0;
12716 }
12717
12718 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12719 }
12720 /* If we have reached alignment,
12721 we can copy the rest of the rows. */
12722 if (IT_CHARPOS (it) == CHARPOS (start))
12723 break;
12724
12725 if (display_line (&it))
12726 last_text_row = it.glyph_row - 1;
12727 }
12728
12729 /* A value of current_y < last_visible_y means that we stopped
12730 at the previous window start, which in turn means that we
12731 have at least one reusable row. */
12732 if (it.current_y < it.last_visible_y)
12733 {
12734 /* IT.vpos always starts from 0; it counts text lines. */
12735 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12736
12737 /* Find PT if not already found in the lines displayed. */
12738 if (w->cursor.vpos < 0)
12739 {
12740 int dy = it.current_y - start_row->y;
12741
12742 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12743 row = row_containing_pos (w, PT, row, NULL, dy);
12744 if (row)
12745 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12746 dy, nrows_scrolled);
12747 else
12748 {
12749 clear_glyph_matrix (w->desired_matrix);
12750 return 0;
12751 }
12752 }
12753
12754 /* Scroll the display. Do it before the current matrix is
12755 changed. The problem here is that update has not yet
12756 run, i.e. part of the current matrix is not up to date.
12757 scroll_run_hook will clear the cursor, and use the
12758 current matrix to get the height of the row the cursor is
12759 in. */
12760 run.current_y = start_row->y;
12761 run.desired_y = it.current_y;
12762 run.height = it.last_visible_y - it.current_y;
12763
12764 if (run.height > 0 && run.current_y != run.desired_y)
12765 {
12766 update_begin (f);
12767 rif->update_window_begin_hook (w);
12768 rif->clear_window_mouse_face (w);
12769 rif->scroll_run_hook (w, &run);
12770 rif->update_window_end_hook (w, 0, 0);
12771 update_end (f);
12772 }
12773
12774 /* Shift current matrix down by nrows_scrolled lines. */
12775 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12776 rotate_matrix (w->current_matrix,
12777 start_vpos,
12778 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12779 nrows_scrolled);
12780
12781 /* Disable lines that must be updated. */
12782 for (i = 0; i < it.vpos; ++i)
12783 (start_row + i)->enabled_p = 0;
12784
12785 /* Re-compute Y positions. */
12786 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12787 max_y = it.last_visible_y;
12788 for (row = start_row + nrows_scrolled;
12789 row < bottom_row;
12790 ++row)
12791 {
12792 row->y = it.current_y;
12793 row->visible_height = row->height;
12794
12795 if (row->y < min_y)
12796 row->visible_height -= min_y - row->y;
12797 if (row->y + row->height > max_y)
12798 row->visible_height -= row->y + row->height - max_y;
12799 row->redraw_fringe_bitmaps_p = 1;
12800
12801 it.current_y += row->height;
12802
12803 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12804 last_reused_text_row = row;
12805 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12806 break;
12807 }
12808
12809 /* Disable lines in the current matrix which are now
12810 below the window. */
12811 for (++row; row < bottom_row; ++row)
12812 row->enabled_p = 0;
12813 }
12814
12815 /* Update window_end_pos etc.; last_reused_text_row is the last
12816 reused row from the current matrix containing text, if any.
12817 The value of last_text_row is the last displayed line
12818 containing text. */
12819 if (last_reused_text_row)
12820 {
12821 w->window_end_bytepos
12822 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12823 w->window_end_pos
12824 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12825 w->window_end_vpos
12826 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12827 w->current_matrix));
12828 }
12829 else if (last_text_row)
12830 {
12831 w->window_end_bytepos
12832 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12833 w->window_end_pos
12834 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12835 w->window_end_vpos
12836 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12837 }
12838 else
12839 {
12840 /* This window must be completely empty. */
12841 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12842 w->window_end_pos = make_number (Z - ZV);
12843 w->window_end_vpos = make_number (0);
12844 }
12845 w->window_end_valid = Qnil;
12846
12847 /* Update hint: don't try scrolling again in update_window. */
12848 w->desired_matrix->no_scrolling_p = 1;
12849
12850 #if GLYPH_DEBUG
12851 debug_method_add (w, "try_window_reusing_current_matrix 1");
12852 #endif
12853 return 1;
12854 }
12855 else if (CHARPOS (new_start) > CHARPOS (start))
12856 {
12857 struct glyph_row *pt_row, *row;
12858 struct glyph_row *first_reusable_row;
12859 struct glyph_row *first_row_to_display;
12860 int dy;
12861 int yb = window_text_bottom_y (w);
12862
12863 /* Find the row starting at new_start, if there is one. Don't
12864 reuse a partially visible line at the end. */
12865 first_reusable_row = start_row;
12866 while (first_reusable_row->enabled_p
12867 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12868 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12869 < CHARPOS (new_start)))
12870 ++first_reusable_row;
12871
12872 /* Give up if there is no row to reuse. */
12873 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12874 || !first_reusable_row->enabled_p
12875 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12876 != CHARPOS (new_start)))
12877 return 0;
12878
12879 /* We can reuse fully visible rows beginning with
12880 first_reusable_row to the end of the window. Set
12881 first_row_to_display to the first row that cannot be reused.
12882 Set pt_row to the row containing point, if there is any. */
12883 pt_row = NULL;
12884 for (first_row_to_display = first_reusable_row;
12885 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12886 ++first_row_to_display)
12887 {
12888 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12889 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12890 pt_row = first_row_to_display;
12891 }
12892
12893 /* Start displaying at the start of first_row_to_display. */
12894 xassert (first_row_to_display->y < yb);
12895 init_to_row_start (&it, w, first_row_to_display);
12896
12897 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12898 - start_vpos);
12899 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12900 - nrows_scrolled);
12901 it.current_y = (first_row_to_display->y - first_reusable_row->y
12902 + WINDOW_HEADER_LINE_HEIGHT (w));
12903
12904 /* Display lines beginning with first_row_to_display in the
12905 desired matrix. Set last_text_row to the last row displayed
12906 that displays text. */
12907 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12908 if (pt_row == NULL)
12909 w->cursor.vpos = -1;
12910 last_text_row = NULL;
12911 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12912 if (display_line (&it))
12913 last_text_row = it.glyph_row - 1;
12914
12915 /* Give up If point isn't in a row displayed or reused. */
12916 if (w->cursor.vpos < 0)
12917 {
12918 clear_glyph_matrix (w->desired_matrix);
12919 return 0;
12920 }
12921
12922 /* If point is in a reused row, adjust y and vpos of the cursor
12923 position. */
12924 if (pt_row)
12925 {
12926 w->cursor.vpos -= nrows_scrolled;
12927 w->cursor.y -= first_reusable_row->y - start_row->y;
12928 }
12929
12930 /* Scroll the display. */
12931 run.current_y = first_reusable_row->y;
12932 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12933 run.height = it.last_visible_y - run.current_y;
12934 dy = run.current_y - run.desired_y;
12935
12936 if (run.height)
12937 {
12938 update_begin (f);
12939 rif->update_window_begin_hook (w);
12940 rif->clear_window_mouse_face (w);
12941 rif->scroll_run_hook (w, &run);
12942 rif->update_window_end_hook (w, 0, 0);
12943 update_end (f);
12944 }
12945
12946 /* Adjust Y positions of reused rows. */
12947 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12948 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12949 max_y = it.last_visible_y;
12950 for (row = first_reusable_row; row < first_row_to_display; ++row)
12951 {
12952 row->y -= dy;
12953 row->visible_height = row->height;
12954 if (row->y < min_y)
12955 row->visible_height -= min_y - row->y;
12956 if (row->y + row->height > max_y)
12957 row->visible_height -= row->y + row->height - max_y;
12958 row->redraw_fringe_bitmaps_p = 1;
12959 }
12960
12961 /* Scroll the current matrix. */
12962 xassert (nrows_scrolled > 0);
12963 rotate_matrix (w->current_matrix,
12964 start_vpos,
12965 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12966 -nrows_scrolled);
12967
12968 /* Disable rows not reused. */
12969 for (row -= nrows_scrolled; row < bottom_row; ++row)
12970 row->enabled_p = 0;
12971
12972 /* Point may have moved to a different line, so we cannot assume that
12973 the previous cursor position is valid; locate the correct row. */
12974 if (pt_row)
12975 {
12976 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12977 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12978 row++)
12979 {
12980 w->cursor.vpos++;
12981 w->cursor.y = row->y;
12982 }
12983 if (row < bottom_row)
12984 {
12985 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12986 while (glyph->charpos < PT)
12987 {
12988 w->cursor.hpos++;
12989 w->cursor.x += glyph->pixel_width;
12990 glyph++;
12991 }
12992 }
12993 }
12994
12995 /* Adjust window end. A null value of last_text_row means that
12996 the window end is in reused rows which in turn means that
12997 only its vpos can have changed. */
12998 if (last_text_row)
12999 {
13000 w->window_end_bytepos
13001 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13002 w->window_end_pos
13003 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13004 w->window_end_vpos
13005 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13006 }
13007 else
13008 {
13009 w->window_end_vpos
13010 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13011 }
13012
13013 w->window_end_valid = Qnil;
13014 w->desired_matrix->no_scrolling_p = 1;
13015
13016 #if GLYPH_DEBUG
13017 debug_method_add (w, "try_window_reusing_current_matrix 2");
13018 #endif
13019 return 1;
13020 }
13021
13022 return 0;
13023 }
13024
13025
13026 \f
13027 /************************************************************************
13028 Window redisplay reusing current matrix when buffer has changed
13029 ************************************************************************/
13030
13031 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13032 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13033 int *, int *));
13034 static struct glyph_row *
13035 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13036 struct glyph_row *));
13037
13038
13039 /* Return the last row in MATRIX displaying text. If row START is
13040 non-null, start searching with that row. IT gives the dimensions
13041 of the display. Value is null if matrix is empty; otherwise it is
13042 a pointer to the row found. */
13043
13044 static struct glyph_row *
13045 find_last_row_displaying_text (matrix, it, start)
13046 struct glyph_matrix *matrix;
13047 struct it *it;
13048 struct glyph_row *start;
13049 {
13050 struct glyph_row *row, *row_found;
13051
13052 /* Set row_found to the last row in IT->w's current matrix
13053 displaying text. The loop looks funny but think of partially
13054 visible lines. */
13055 row_found = NULL;
13056 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13057 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13058 {
13059 xassert (row->enabled_p);
13060 row_found = row;
13061 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13062 break;
13063 ++row;
13064 }
13065
13066 return row_found;
13067 }
13068
13069
13070 /* Return the last row in the current matrix of W that is not affected
13071 by changes at the start of current_buffer that occurred since W's
13072 current matrix was built. Value is null if no such row exists.
13073
13074 BEG_UNCHANGED us the number of characters unchanged at the start of
13075 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13076 first changed character in current_buffer. Characters at positions <
13077 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13078 when the current matrix was built. */
13079
13080 static struct glyph_row *
13081 find_last_unchanged_at_beg_row (w)
13082 struct window *w;
13083 {
13084 int first_changed_pos = BEG + BEG_UNCHANGED;
13085 struct glyph_row *row;
13086 struct glyph_row *row_found = NULL;
13087 int yb = window_text_bottom_y (w);
13088
13089 /* Find the last row displaying unchanged text. */
13090 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13091 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13092 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13093 {
13094 if (/* If row ends before first_changed_pos, it is unchanged,
13095 except in some case. */
13096 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13097 /* When row ends in ZV and we write at ZV it is not
13098 unchanged. */
13099 && !row->ends_at_zv_p
13100 /* When first_changed_pos is the end of a continued line,
13101 row is not unchanged because it may be no longer
13102 continued. */
13103 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13104 && (row->continued_p
13105 || row->exact_window_width_line_p)))
13106 row_found = row;
13107
13108 /* Stop if last visible row. */
13109 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13110 break;
13111
13112 ++row;
13113 }
13114
13115 return row_found;
13116 }
13117
13118
13119 /* Find the first glyph row in the current matrix of W that is not
13120 affected by changes at the end of current_buffer since the
13121 time W's current matrix was built.
13122
13123 Return in *DELTA the number of chars by which buffer positions in
13124 unchanged text at the end of current_buffer must be adjusted.
13125
13126 Return in *DELTA_BYTES the corresponding number of bytes.
13127
13128 Value is null if no such row exists, i.e. all rows are affected by
13129 changes. */
13130
13131 static struct glyph_row *
13132 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13133 struct window *w;
13134 int *delta, *delta_bytes;
13135 {
13136 struct glyph_row *row;
13137 struct glyph_row *row_found = NULL;
13138
13139 *delta = *delta_bytes = 0;
13140
13141 /* Display must not have been paused, otherwise the current matrix
13142 is not up to date. */
13143 if (NILP (w->window_end_valid))
13144 abort ();
13145
13146 /* A value of window_end_pos >= END_UNCHANGED means that the window
13147 end is in the range of changed text. If so, there is no
13148 unchanged row at the end of W's current matrix. */
13149 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13150 return NULL;
13151
13152 /* Set row to the last row in W's current matrix displaying text. */
13153 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13154
13155 /* If matrix is entirely empty, no unchanged row exists. */
13156 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13157 {
13158 /* The value of row is the last glyph row in the matrix having a
13159 meaningful buffer position in it. The end position of row
13160 corresponds to window_end_pos. This allows us to translate
13161 buffer positions in the current matrix to current buffer
13162 positions for characters not in changed text. */
13163 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13164 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13165 int last_unchanged_pos, last_unchanged_pos_old;
13166 struct glyph_row *first_text_row
13167 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13168
13169 *delta = Z - Z_old;
13170 *delta_bytes = Z_BYTE - Z_BYTE_old;
13171
13172 /* Set last_unchanged_pos to the buffer position of the last
13173 character in the buffer that has not been changed. Z is the
13174 index + 1 of the last character in current_buffer, i.e. by
13175 subtracting END_UNCHANGED we get the index of the last
13176 unchanged character, and we have to add BEG to get its buffer
13177 position. */
13178 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13179 last_unchanged_pos_old = last_unchanged_pos - *delta;
13180
13181 /* Search backward from ROW for a row displaying a line that
13182 starts at a minimum position >= last_unchanged_pos_old. */
13183 for (; row > first_text_row; --row)
13184 {
13185 /* This used to abort, but it can happen.
13186 It is ok to just stop the search instead here. KFS. */
13187 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13188 break;
13189
13190 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13191 row_found = row;
13192 }
13193 }
13194
13195 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13196 abort ();
13197
13198 return row_found;
13199 }
13200
13201
13202 /* Make sure that glyph rows in the current matrix of window W
13203 reference the same glyph memory as corresponding rows in the
13204 frame's frame matrix. This function is called after scrolling W's
13205 current matrix on a terminal frame in try_window_id and
13206 try_window_reusing_current_matrix. */
13207
13208 static void
13209 sync_frame_with_window_matrix_rows (w)
13210 struct window *w;
13211 {
13212 struct frame *f = XFRAME (w->frame);
13213 struct glyph_row *window_row, *window_row_end, *frame_row;
13214
13215 /* Preconditions: W must be a leaf window and full-width. Its frame
13216 must have a frame matrix. */
13217 xassert (NILP (w->hchild) && NILP (w->vchild));
13218 xassert (WINDOW_FULL_WIDTH_P (w));
13219 xassert (!FRAME_WINDOW_P (f));
13220
13221 /* If W is a full-width window, glyph pointers in W's current matrix
13222 have, by definition, to be the same as glyph pointers in the
13223 corresponding frame matrix. Note that frame matrices have no
13224 marginal areas (see build_frame_matrix). */
13225 window_row = w->current_matrix->rows;
13226 window_row_end = window_row + w->current_matrix->nrows;
13227 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13228 while (window_row < window_row_end)
13229 {
13230 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13231 struct glyph *end = window_row->glyphs[LAST_AREA];
13232
13233 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13234 frame_row->glyphs[TEXT_AREA] = start;
13235 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13236 frame_row->glyphs[LAST_AREA] = end;
13237
13238 /* Disable frame rows whose corresponding window rows have
13239 been disabled in try_window_id. */
13240 if (!window_row->enabled_p)
13241 frame_row->enabled_p = 0;
13242
13243 ++window_row, ++frame_row;
13244 }
13245 }
13246
13247
13248 /* Find the glyph row in window W containing CHARPOS. Consider all
13249 rows between START and END (not inclusive). END null means search
13250 all rows to the end of the display area of W. Value is the row
13251 containing CHARPOS or null. */
13252
13253 struct glyph_row *
13254 row_containing_pos (w, charpos, start, end, dy)
13255 struct window *w;
13256 int charpos;
13257 struct glyph_row *start, *end;
13258 int dy;
13259 {
13260 struct glyph_row *row = start;
13261 int last_y;
13262
13263 /* If we happen to start on a header-line, skip that. */
13264 if (row->mode_line_p)
13265 ++row;
13266
13267 if ((end && row >= end) || !row->enabled_p)
13268 return NULL;
13269
13270 last_y = window_text_bottom_y (w) - dy;
13271
13272 while (1)
13273 {
13274 /* Give up if we have gone too far. */
13275 if (end && row >= end)
13276 return NULL;
13277 /* This formerly returned if they were equal.
13278 I think that both quantities are of a "last plus one" type;
13279 if so, when they are equal, the row is within the screen. -- rms. */
13280 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13281 return NULL;
13282
13283 /* If it is in this row, return this row. */
13284 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13285 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13286 /* The end position of a row equals the start
13287 position of the next row. If CHARPOS is there, we
13288 would rather display it in the next line, except
13289 when this line ends in ZV. */
13290 && !row->ends_at_zv_p
13291 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13292 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13293 return row;
13294 ++row;
13295 }
13296 }
13297
13298
13299 /* Try to redisplay window W by reusing its existing display. W's
13300 current matrix must be up to date when this function is called,
13301 i.e. window_end_valid must not be nil.
13302
13303 Value is
13304
13305 1 if display has been updated
13306 0 if otherwise unsuccessful
13307 -1 if redisplay with same window start is known not to succeed
13308
13309 The following steps are performed:
13310
13311 1. Find the last row in the current matrix of W that is not
13312 affected by changes at the start of current_buffer. If no such row
13313 is found, give up.
13314
13315 2. Find the first row in W's current matrix that is not affected by
13316 changes at the end of current_buffer. Maybe there is no such row.
13317
13318 3. Display lines beginning with the row + 1 found in step 1 to the
13319 row found in step 2 or, if step 2 didn't find a row, to the end of
13320 the window.
13321
13322 4. If cursor is not known to appear on the window, give up.
13323
13324 5. If display stopped at the row found in step 2, scroll the
13325 display and current matrix as needed.
13326
13327 6. Maybe display some lines at the end of W, if we must. This can
13328 happen under various circumstances, like a partially visible line
13329 becoming fully visible, or because newly displayed lines are displayed
13330 in smaller font sizes.
13331
13332 7. Update W's window end information. */
13333
13334 static int
13335 try_window_id (w)
13336 struct window *w;
13337 {
13338 struct frame *f = XFRAME (w->frame);
13339 struct glyph_matrix *current_matrix = w->current_matrix;
13340 struct glyph_matrix *desired_matrix = w->desired_matrix;
13341 struct glyph_row *last_unchanged_at_beg_row;
13342 struct glyph_row *first_unchanged_at_end_row;
13343 struct glyph_row *row;
13344 struct glyph_row *bottom_row;
13345 int bottom_vpos;
13346 struct it it;
13347 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13348 struct text_pos start_pos;
13349 struct run run;
13350 int first_unchanged_at_end_vpos = 0;
13351 struct glyph_row *last_text_row, *last_text_row_at_end;
13352 struct text_pos start;
13353 int first_changed_charpos, last_changed_charpos;
13354
13355 #if GLYPH_DEBUG
13356 if (inhibit_try_window_id)
13357 return 0;
13358 #endif
13359
13360 /* This is handy for debugging. */
13361 #if 0
13362 #define GIVE_UP(X) \
13363 do { \
13364 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13365 return 0; \
13366 } while (0)
13367 #else
13368 #define GIVE_UP(X) return 0
13369 #endif
13370
13371 SET_TEXT_POS_FROM_MARKER (start, w->start);
13372
13373 /* Don't use this for mini-windows because these can show
13374 messages and mini-buffers, and we don't handle that here. */
13375 if (MINI_WINDOW_P (w))
13376 GIVE_UP (1);
13377
13378 /* This flag is used to prevent redisplay optimizations. */
13379 if (windows_or_buffers_changed || cursor_type_changed)
13380 GIVE_UP (2);
13381
13382 /* Verify that narrowing has not changed.
13383 Also verify that we were not told to prevent redisplay optimizations.
13384 It would be nice to further
13385 reduce the number of cases where this prevents try_window_id. */
13386 if (current_buffer->clip_changed
13387 || current_buffer->prevent_redisplay_optimizations_p)
13388 GIVE_UP (3);
13389
13390 /* Window must either use window-based redisplay or be full width. */
13391 if (!FRAME_WINDOW_P (f)
13392 && (!line_ins_del_ok
13393 || !WINDOW_FULL_WIDTH_P (w)))
13394 GIVE_UP (4);
13395
13396 /* Give up if point is not known NOT to appear in W. */
13397 if (PT < CHARPOS (start))
13398 GIVE_UP (5);
13399
13400 /* Another way to prevent redisplay optimizations. */
13401 if (XFASTINT (w->last_modified) == 0)
13402 GIVE_UP (6);
13403
13404 /* Verify that window is not hscrolled. */
13405 if (XFASTINT (w->hscroll) != 0)
13406 GIVE_UP (7);
13407
13408 /* Verify that display wasn't paused. */
13409 if (NILP (w->window_end_valid))
13410 GIVE_UP (8);
13411
13412 /* Can't use this if highlighting a region because a cursor movement
13413 will do more than just set the cursor. */
13414 if (!NILP (Vtransient_mark_mode)
13415 && !NILP (current_buffer->mark_active))
13416 GIVE_UP (9);
13417
13418 /* Likewise if highlighting trailing whitespace. */
13419 if (!NILP (Vshow_trailing_whitespace))
13420 GIVE_UP (11);
13421
13422 /* Likewise if showing a region. */
13423 if (!NILP (w->region_showing))
13424 GIVE_UP (10);
13425
13426 /* Can use this if overlay arrow position and or string have changed. */
13427 if (overlay_arrows_changed_p ())
13428 GIVE_UP (12);
13429
13430
13431 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13432 only if buffer has really changed. The reason is that the gap is
13433 initially at Z for freshly visited files. The code below would
13434 set end_unchanged to 0 in that case. */
13435 if (MODIFF > SAVE_MODIFF
13436 /* This seems to happen sometimes after saving a buffer. */
13437 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13438 {
13439 if (GPT - BEG < BEG_UNCHANGED)
13440 BEG_UNCHANGED = GPT - BEG;
13441 if (Z - GPT < END_UNCHANGED)
13442 END_UNCHANGED = Z - GPT;
13443 }
13444
13445 /* The position of the first and last character that has been changed. */
13446 first_changed_charpos = BEG + BEG_UNCHANGED;
13447 last_changed_charpos = Z - END_UNCHANGED;
13448
13449 /* If window starts after a line end, and the last change is in
13450 front of that newline, then changes don't affect the display.
13451 This case happens with stealth-fontification. Note that although
13452 the display is unchanged, glyph positions in the matrix have to
13453 be adjusted, of course. */
13454 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13455 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13456 && ((last_changed_charpos < CHARPOS (start)
13457 && CHARPOS (start) == BEGV)
13458 || (last_changed_charpos < CHARPOS (start) - 1
13459 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13460 {
13461 int Z_old, delta, Z_BYTE_old, delta_bytes;
13462 struct glyph_row *r0;
13463
13464 /* Compute how many chars/bytes have been added to or removed
13465 from the buffer. */
13466 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13467 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13468 delta = Z - Z_old;
13469 delta_bytes = Z_BYTE - Z_BYTE_old;
13470
13471 /* Give up if PT is not in the window. Note that it already has
13472 been checked at the start of try_window_id that PT is not in
13473 front of the window start. */
13474 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13475 GIVE_UP (13);
13476
13477 /* If window start is unchanged, we can reuse the whole matrix
13478 as is, after adjusting glyph positions. No need to compute
13479 the window end again, since its offset from Z hasn't changed. */
13480 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13481 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13482 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13483 /* PT must not be in a partially visible line. */
13484 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13485 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13486 {
13487 /* Adjust positions in the glyph matrix. */
13488 if (delta || delta_bytes)
13489 {
13490 struct glyph_row *r1
13491 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13492 increment_matrix_positions (w->current_matrix,
13493 MATRIX_ROW_VPOS (r0, current_matrix),
13494 MATRIX_ROW_VPOS (r1, current_matrix),
13495 delta, delta_bytes);
13496 }
13497
13498 /* Set the cursor. */
13499 row = row_containing_pos (w, PT, r0, NULL, 0);
13500 if (row)
13501 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13502 else
13503 abort ();
13504 return 1;
13505 }
13506 }
13507
13508 /* Handle the case that changes are all below what is displayed in
13509 the window, and that PT is in the window. This shortcut cannot
13510 be taken if ZV is visible in the window, and text has been added
13511 there that is visible in the window. */
13512 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13513 /* ZV is not visible in the window, or there are no
13514 changes at ZV, actually. */
13515 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13516 || first_changed_charpos == last_changed_charpos))
13517 {
13518 struct glyph_row *r0;
13519
13520 /* Give up if PT is not in the window. Note that it already has
13521 been checked at the start of try_window_id that PT is not in
13522 front of the window start. */
13523 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13524 GIVE_UP (14);
13525
13526 /* If window start is unchanged, we can reuse the whole matrix
13527 as is, without changing glyph positions since no text has
13528 been added/removed in front of the window end. */
13529 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13530 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13531 /* PT must not be in a partially visible line. */
13532 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13533 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13534 {
13535 /* We have to compute the window end anew since text
13536 can have been added/removed after it. */
13537 w->window_end_pos
13538 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13539 w->window_end_bytepos
13540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13541
13542 /* Set the cursor. */
13543 row = row_containing_pos (w, PT, r0, NULL, 0);
13544 if (row)
13545 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13546 else
13547 abort ();
13548 return 2;
13549 }
13550 }
13551
13552 /* Give up if window start is in the changed area.
13553
13554 The condition used to read
13555
13556 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13557
13558 but why that was tested escapes me at the moment. */
13559 if (CHARPOS (start) >= first_changed_charpos
13560 && CHARPOS (start) <= last_changed_charpos)
13561 GIVE_UP (15);
13562
13563 /* Check that window start agrees with the start of the first glyph
13564 row in its current matrix. Check this after we know the window
13565 start is not in changed text, otherwise positions would not be
13566 comparable. */
13567 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13568 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13569 GIVE_UP (16);
13570
13571 /* Give up if the window ends in strings. Overlay strings
13572 at the end are difficult to handle, so don't try. */
13573 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13574 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13575 GIVE_UP (20);
13576
13577 /* Compute the position at which we have to start displaying new
13578 lines. Some of the lines at the top of the window might be
13579 reusable because they are not displaying changed text. Find the
13580 last row in W's current matrix not affected by changes at the
13581 start of current_buffer. Value is null if changes start in the
13582 first line of window. */
13583 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13584 if (last_unchanged_at_beg_row)
13585 {
13586 /* Avoid starting to display in the moddle of a character, a TAB
13587 for instance. This is easier than to set up the iterator
13588 exactly, and it's not a frequent case, so the additional
13589 effort wouldn't really pay off. */
13590 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13591 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13592 && last_unchanged_at_beg_row > w->current_matrix->rows)
13593 --last_unchanged_at_beg_row;
13594
13595 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13596 GIVE_UP (17);
13597
13598 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13599 GIVE_UP (18);
13600 start_pos = it.current.pos;
13601
13602 /* Start displaying new lines in the desired matrix at the same
13603 vpos we would use in the current matrix, i.e. below
13604 last_unchanged_at_beg_row. */
13605 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13606 current_matrix);
13607 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13608 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13609
13610 xassert (it.hpos == 0 && it.current_x == 0);
13611 }
13612 else
13613 {
13614 /* There are no reusable lines at the start of the window.
13615 Start displaying in the first text line. */
13616 start_display (&it, w, start);
13617 it.vpos = it.first_vpos;
13618 start_pos = it.current.pos;
13619 }
13620
13621 /* Find the first row that is not affected by changes at the end of
13622 the buffer. Value will be null if there is no unchanged row, in
13623 which case we must redisplay to the end of the window. delta
13624 will be set to the value by which buffer positions beginning with
13625 first_unchanged_at_end_row have to be adjusted due to text
13626 changes. */
13627 first_unchanged_at_end_row
13628 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13629 IF_DEBUG (debug_delta = delta);
13630 IF_DEBUG (debug_delta_bytes = delta_bytes);
13631
13632 /* Set stop_pos to the buffer position up to which we will have to
13633 display new lines. If first_unchanged_at_end_row != NULL, this
13634 is the buffer position of the start of the line displayed in that
13635 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13636 that we don't stop at a buffer position. */
13637 stop_pos = 0;
13638 if (first_unchanged_at_end_row)
13639 {
13640 xassert (last_unchanged_at_beg_row == NULL
13641 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13642
13643 /* If this is a continuation line, move forward to the next one
13644 that isn't. Changes in lines above affect this line.
13645 Caution: this may move first_unchanged_at_end_row to a row
13646 not displaying text. */
13647 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13648 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13649 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13650 < it.last_visible_y))
13651 ++first_unchanged_at_end_row;
13652
13653 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13654 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13655 >= it.last_visible_y))
13656 first_unchanged_at_end_row = NULL;
13657 else
13658 {
13659 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13660 + delta);
13661 first_unchanged_at_end_vpos
13662 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13663 xassert (stop_pos >= Z - END_UNCHANGED);
13664 }
13665 }
13666 else if (last_unchanged_at_beg_row == NULL)
13667 GIVE_UP (19);
13668
13669
13670 #if GLYPH_DEBUG
13671
13672 /* Either there is no unchanged row at the end, or the one we have
13673 now displays text. This is a necessary condition for the window
13674 end pos calculation at the end of this function. */
13675 xassert (first_unchanged_at_end_row == NULL
13676 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13677
13678 debug_last_unchanged_at_beg_vpos
13679 = (last_unchanged_at_beg_row
13680 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13681 : -1);
13682 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13683
13684 #endif /* GLYPH_DEBUG != 0 */
13685
13686
13687 /* Display new lines. Set last_text_row to the last new line
13688 displayed which has text on it, i.e. might end up as being the
13689 line where the window_end_vpos is. */
13690 w->cursor.vpos = -1;
13691 last_text_row = NULL;
13692 overlay_arrow_seen = 0;
13693 while (it.current_y < it.last_visible_y
13694 && !fonts_changed_p
13695 && (first_unchanged_at_end_row == NULL
13696 || IT_CHARPOS (it) < stop_pos))
13697 {
13698 if (display_line (&it))
13699 last_text_row = it.glyph_row - 1;
13700 }
13701
13702 if (fonts_changed_p)
13703 return -1;
13704
13705
13706 /* Compute differences in buffer positions, y-positions etc. for
13707 lines reused at the bottom of the window. Compute what we can
13708 scroll. */
13709 if (first_unchanged_at_end_row
13710 /* No lines reused because we displayed everything up to the
13711 bottom of the window. */
13712 && it.current_y < it.last_visible_y)
13713 {
13714 dvpos = (it.vpos
13715 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13716 current_matrix));
13717 dy = it.current_y - first_unchanged_at_end_row->y;
13718 run.current_y = first_unchanged_at_end_row->y;
13719 run.desired_y = run.current_y + dy;
13720 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13721 }
13722 else
13723 {
13724 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13725 first_unchanged_at_end_row = NULL;
13726 }
13727 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13728
13729
13730 /* Find the cursor if not already found. We have to decide whether
13731 PT will appear on this window (it sometimes doesn't, but this is
13732 not a very frequent case.) This decision has to be made before
13733 the current matrix is altered. A value of cursor.vpos < 0 means
13734 that PT is either in one of the lines beginning at
13735 first_unchanged_at_end_row or below the window. Don't care for
13736 lines that might be displayed later at the window end; as
13737 mentioned, this is not a frequent case. */
13738 if (w->cursor.vpos < 0)
13739 {
13740 /* Cursor in unchanged rows at the top? */
13741 if (PT < CHARPOS (start_pos)
13742 && last_unchanged_at_beg_row)
13743 {
13744 row = row_containing_pos (w, PT,
13745 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13746 last_unchanged_at_beg_row + 1, 0);
13747 if (row)
13748 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13749 }
13750
13751 /* Start from first_unchanged_at_end_row looking for PT. */
13752 else if (first_unchanged_at_end_row)
13753 {
13754 row = row_containing_pos (w, PT - delta,
13755 first_unchanged_at_end_row, NULL, 0);
13756 if (row)
13757 set_cursor_from_row (w, row, w->current_matrix, delta,
13758 delta_bytes, dy, dvpos);
13759 }
13760
13761 /* Give up if cursor was not found. */
13762 if (w->cursor.vpos < 0)
13763 {
13764 clear_glyph_matrix (w->desired_matrix);
13765 return -1;
13766 }
13767 }
13768
13769 /* Don't let the cursor end in the scroll margins. */
13770 {
13771 int this_scroll_margin, cursor_height;
13772
13773 this_scroll_margin = max (0, scroll_margin);
13774 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13775 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13776 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13777
13778 if ((w->cursor.y < this_scroll_margin
13779 && CHARPOS (start) > BEGV)
13780 /* Old redisplay didn't take scroll margin into account at the bottom,
13781 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13782 || (w->cursor.y + (make_cursor_line_fully_visible_p
13783 ? cursor_height + this_scroll_margin
13784 : 1)) > it.last_visible_y)
13785 {
13786 w->cursor.vpos = -1;
13787 clear_glyph_matrix (w->desired_matrix);
13788 return -1;
13789 }
13790 }
13791
13792 /* Scroll the display. Do it before changing the current matrix so
13793 that xterm.c doesn't get confused about where the cursor glyph is
13794 found. */
13795 if (dy && run.height)
13796 {
13797 update_begin (f);
13798
13799 if (FRAME_WINDOW_P (f))
13800 {
13801 rif->update_window_begin_hook (w);
13802 rif->clear_window_mouse_face (w);
13803 rif->scroll_run_hook (w, &run);
13804 rif->update_window_end_hook (w, 0, 0);
13805 }
13806 else
13807 {
13808 /* Terminal frame. In this case, dvpos gives the number of
13809 lines to scroll by; dvpos < 0 means scroll up. */
13810 int first_unchanged_at_end_vpos
13811 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13812 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13813 int end = (WINDOW_TOP_EDGE_LINE (w)
13814 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13815 + window_internal_height (w));
13816
13817 /* Perform the operation on the screen. */
13818 if (dvpos > 0)
13819 {
13820 /* Scroll last_unchanged_at_beg_row to the end of the
13821 window down dvpos lines. */
13822 set_terminal_window (end);
13823
13824 /* On dumb terminals delete dvpos lines at the end
13825 before inserting dvpos empty lines. */
13826 if (!scroll_region_ok)
13827 ins_del_lines (end - dvpos, -dvpos);
13828
13829 /* Insert dvpos empty lines in front of
13830 last_unchanged_at_beg_row. */
13831 ins_del_lines (from, dvpos);
13832 }
13833 else if (dvpos < 0)
13834 {
13835 /* Scroll up last_unchanged_at_beg_vpos to the end of
13836 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13837 set_terminal_window (end);
13838
13839 /* Delete dvpos lines in front of
13840 last_unchanged_at_beg_vpos. ins_del_lines will set
13841 the cursor to the given vpos and emit |dvpos| delete
13842 line sequences. */
13843 ins_del_lines (from + dvpos, dvpos);
13844
13845 /* On a dumb terminal insert dvpos empty lines at the
13846 end. */
13847 if (!scroll_region_ok)
13848 ins_del_lines (end + dvpos, -dvpos);
13849 }
13850
13851 set_terminal_window (0);
13852 }
13853
13854 update_end (f);
13855 }
13856
13857 /* Shift reused rows of the current matrix to the right position.
13858 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13859 text. */
13860 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13861 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13862 if (dvpos < 0)
13863 {
13864 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13865 bottom_vpos, dvpos);
13866 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13867 bottom_vpos, 0);
13868 }
13869 else if (dvpos > 0)
13870 {
13871 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13872 bottom_vpos, dvpos);
13873 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13874 first_unchanged_at_end_vpos + dvpos, 0);
13875 }
13876
13877 /* For frame-based redisplay, make sure that current frame and window
13878 matrix are in sync with respect to glyph memory. */
13879 if (!FRAME_WINDOW_P (f))
13880 sync_frame_with_window_matrix_rows (w);
13881
13882 /* Adjust buffer positions in reused rows. */
13883 if (delta)
13884 increment_matrix_positions (current_matrix,
13885 first_unchanged_at_end_vpos + dvpos,
13886 bottom_vpos, delta, delta_bytes);
13887
13888 /* Adjust Y positions. */
13889 if (dy)
13890 shift_glyph_matrix (w, current_matrix,
13891 first_unchanged_at_end_vpos + dvpos,
13892 bottom_vpos, dy);
13893
13894 if (first_unchanged_at_end_row)
13895 {
13896 first_unchanged_at_end_row += dvpos;
13897 if (first_unchanged_at_end_row->y >= it.last_visible_y
13898 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
13899 first_unchanged_at_end_row = NULL;
13900 }
13901
13902 /* If scrolling up, there may be some lines to display at the end of
13903 the window. */
13904 last_text_row_at_end = NULL;
13905 if (dy < 0)
13906 {
13907 /* Scrolling up can leave for example a partially visible line
13908 at the end of the window to be redisplayed. */
13909 /* Set last_row to the glyph row in the current matrix where the
13910 window end line is found. It has been moved up or down in
13911 the matrix by dvpos. */
13912 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13913 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13914
13915 /* If last_row is the window end line, it should display text. */
13916 xassert (last_row->displays_text_p);
13917
13918 /* If window end line was partially visible before, begin
13919 displaying at that line. Otherwise begin displaying with the
13920 line following it. */
13921 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13922 {
13923 init_to_row_start (&it, w, last_row);
13924 it.vpos = last_vpos;
13925 it.current_y = last_row->y;
13926 }
13927 else
13928 {
13929 init_to_row_end (&it, w, last_row);
13930 it.vpos = 1 + last_vpos;
13931 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13932 ++last_row;
13933 }
13934
13935 /* We may start in a continuation line. If so, we have to
13936 get the right continuation_lines_width and current_x. */
13937 it.continuation_lines_width = last_row->continuation_lines_width;
13938 it.hpos = it.current_x = 0;
13939
13940 /* Display the rest of the lines at the window end. */
13941 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13942 while (it.current_y < it.last_visible_y
13943 && !fonts_changed_p)
13944 {
13945 /* Is it always sure that the display agrees with lines in
13946 the current matrix? I don't think so, so we mark rows
13947 displayed invalid in the current matrix by setting their
13948 enabled_p flag to zero. */
13949 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13950 if (display_line (&it))
13951 last_text_row_at_end = it.glyph_row - 1;
13952 }
13953 }
13954
13955 /* Update window_end_pos and window_end_vpos. */
13956 if (first_unchanged_at_end_row
13957 && !last_text_row_at_end)
13958 {
13959 /* Window end line if one of the preserved rows from the current
13960 matrix. Set row to the last row displaying text in current
13961 matrix starting at first_unchanged_at_end_row, after
13962 scrolling. */
13963 xassert (first_unchanged_at_end_row->displays_text_p);
13964 row = find_last_row_displaying_text (w->current_matrix, &it,
13965 first_unchanged_at_end_row);
13966 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13967
13968 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13969 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13970 w->window_end_vpos
13971 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13972 xassert (w->window_end_bytepos >= 0);
13973 IF_DEBUG (debug_method_add (w, "A"));
13974 }
13975 else if (last_text_row_at_end)
13976 {
13977 w->window_end_pos
13978 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13979 w->window_end_bytepos
13980 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13981 w->window_end_vpos
13982 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13983 xassert (w->window_end_bytepos >= 0);
13984 IF_DEBUG (debug_method_add (w, "B"));
13985 }
13986 else if (last_text_row)
13987 {
13988 /* We have displayed either to the end of the window or at the
13989 end of the window, i.e. the last row with text is to be found
13990 in the desired matrix. */
13991 w->window_end_pos
13992 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13993 w->window_end_bytepos
13994 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13995 w->window_end_vpos
13996 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13997 xassert (w->window_end_bytepos >= 0);
13998 }
13999 else if (first_unchanged_at_end_row == NULL
14000 && last_text_row == NULL
14001 && last_text_row_at_end == NULL)
14002 {
14003 /* Displayed to end of window, but no line containing text was
14004 displayed. Lines were deleted at the end of the window. */
14005 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14006 int vpos = XFASTINT (w->window_end_vpos);
14007 struct glyph_row *current_row = current_matrix->rows + vpos;
14008 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14009
14010 for (row = NULL;
14011 row == NULL && vpos >= first_vpos;
14012 --vpos, --current_row, --desired_row)
14013 {
14014 if (desired_row->enabled_p)
14015 {
14016 if (desired_row->displays_text_p)
14017 row = desired_row;
14018 }
14019 else if (current_row->displays_text_p)
14020 row = current_row;
14021 }
14022
14023 xassert (row != NULL);
14024 w->window_end_vpos = make_number (vpos + 1);
14025 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14026 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14027 xassert (w->window_end_bytepos >= 0);
14028 IF_DEBUG (debug_method_add (w, "C"));
14029 }
14030 else
14031 abort ();
14032
14033 #if 0 /* This leads to problems, for instance when the cursor is
14034 at ZV, and the cursor line displays no text. */
14035 /* Disable rows below what's displayed in the window. This makes
14036 debugging easier. */
14037 enable_glyph_matrix_rows (current_matrix,
14038 XFASTINT (w->window_end_vpos) + 1,
14039 bottom_vpos, 0);
14040 #endif
14041
14042 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14043 debug_end_vpos = XFASTINT (w->window_end_vpos));
14044
14045 /* Record that display has not been completed. */
14046 w->window_end_valid = Qnil;
14047 w->desired_matrix->no_scrolling_p = 1;
14048 return 3;
14049
14050 #undef GIVE_UP
14051 }
14052
14053
14054 \f
14055 /***********************************************************************
14056 More debugging support
14057 ***********************************************************************/
14058
14059 #if GLYPH_DEBUG
14060
14061 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14062 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14063 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14064
14065
14066 /* Dump the contents of glyph matrix MATRIX on stderr.
14067
14068 GLYPHS 0 means don't show glyph contents.
14069 GLYPHS 1 means show glyphs in short form
14070 GLYPHS > 1 means show glyphs in long form. */
14071
14072 void
14073 dump_glyph_matrix (matrix, glyphs)
14074 struct glyph_matrix *matrix;
14075 int glyphs;
14076 {
14077 int i;
14078 for (i = 0; i < matrix->nrows; ++i)
14079 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14080 }
14081
14082
14083 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14084 the glyph row and area where the glyph comes from. */
14085
14086 void
14087 dump_glyph (row, glyph, area)
14088 struct glyph_row *row;
14089 struct glyph *glyph;
14090 int area;
14091 {
14092 if (glyph->type == CHAR_GLYPH)
14093 {
14094 fprintf (stderr,
14095 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14096 glyph - row->glyphs[TEXT_AREA],
14097 'C',
14098 glyph->charpos,
14099 (BUFFERP (glyph->object)
14100 ? 'B'
14101 : (STRINGP (glyph->object)
14102 ? 'S'
14103 : '-')),
14104 glyph->pixel_width,
14105 glyph->u.ch,
14106 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14107 ? glyph->u.ch
14108 : '.'),
14109 glyph->face_id,
14110 glyph->left_box_line_p,
14111 glyph->right_box_line_p);
14112 }
14113 else if (glyph->type == STRETCH_GLYPH)
14114 {
14115 fprintf (stderr,
14116 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14117 glyph - row->glyphs[TEXT_AREA],
14118 'S',
14119 glyph->charpos,
14120 (BUFFERP (glyph->object)
14121 ? 'B'
14122 : (STRINGP (glyph->object)
14123 ? 'S'
14124 : '-')),
14125 glyph->pixel_width,
14126 0,
14127 '.',
14128 glyph->face_id,
14129 glyph->left_box_line_p,
14130 glyph->right_box_line_p);
14131 }
14132 else if (glyph->type == IMAGE_GLYPH)
14133 {
14134 fprintf (stderr,
14135 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14136 glyph - row->glyphs[TEXT_AREA],
14137 'I',
14138 glyph->charpos,
14139 (BUFFERP (glyph->object)
14140 ? 'B'
14141 : (STRINGP (glyph->object)
14142 ? 'S'
14143 : '-')),
14144 glyph->pixel_width,
14145 glyph->u.img_id,
14146 '.',
14147 glyph->face_id,
14148 glyph->left_box_line_p,
14149 glyph->right_box_line_p);
14150 }
14151 }
14152
14153
14154 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14155 GLYPHS 0 means don't show glyph contents.
14156 GLYPHS 1 means show glyphs in short form
14157 GLYPHS > 1 means show glyphs in long form. */
14158
14159 void
14160 dump_glyph_row (row, vpos, glyphs)
14161 struct glyph_row *row;
14162 int vpos, glyphs;
14163 {
14164 if (glyphs != 1)
14165 {
14166 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14167 fprintf (stderr, "======================================================================\n");
14168
14169 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14170 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14171 vpos,
14172 MATRIX_ROW_START_CHARPOS (row),
14173 MATRIX_ROW_END_CHARPOS (row),
14174 row->used[TEXT_AREA],
14175 row->contains_overlapping_glyphs_p,
14176 row->enabled_p,
14177 row->truncated_on_left_p,
14178 row->truncated_on_right_p,
14179 row->continued_p,
14180 MATRIX_ROW_CONTINUATION_LINE_P (row),
14181 row->displays_text_p,
14182 row->ends_at_zv_p,
14183 row->fill_line_p,
14184 row->ends_in_middle_of_char_p,
14185 row->starts_in_middle_of_char_p,
14186 row->mouse_face_p,
14187 row->x,
14188 row->y,
14189 row->pixel_width,
14190 row->height,
14191 row->visible_height,
14192 row->ascent,
14193 row->phys_ascent);
14194 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14195 row->end.overlay_string_index,
14196 row->continuation_lines_width);
14197 fprintf (stderr, "%9d %5d\n",
14198 CHARPOS (row->start.string_pos),
14199 CHARPOS (row->end.string_pos));
14200 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14201 row->end.dpvec_index);
14202 }
14203
14204 if (glyphs > 1)
14205 {
14206 int area;
14207
14208 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14209 {
14210 struct glyph *glyph = row->glyphs[area];
14211 struct glyph *glyph_end = glyph + row->used[area];
14212
14213 /* Glyph for a line end in text. */
14214 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14215 ++glyph_end;
14216
14217 if (glyph < glyph_end)
14218 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14219
14220 for (; glyph < glyph_end; ++glyph)
14221 dump_glyph (row, glyph, area);
14222 }
14223 }
14224 else if (glyphs == 1)
14225 {
14226 int area;
14227
14228 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14229 {
14230 char *s = (char *) alloca (row->used[area] + 1);
14231 int i;
14232
14233 for (i = 0; i < row->used[area]; ++i)
14234 {
14235 struct glyph *glyph = row->glyphs[area] + i;
14236 if (glyph->type == CHAR_GLYPH
14237 && glyph->u.ch < 0x80
14238 && glyph->u.ch >= ' ')
14239 s[i] = glyph->u.ch;
14240 else
14241 s[i] = '.';
14242 }
14243
14244 s[i] = '\0';
14245 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14246 }
14247 }
14248 }
14249
14250
14251 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14252 Sdump_glyph_matrix, 0, 1, "p",
14253 doc: /* Dump the current matrix of the selected window to stderr.
14254 Shows contents of glyph row structures. With non-nil
14255 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14256 glyphs in short form, otherwise show glyphs in long form. */)
14257 (glyphs)
14258 Lisp_Object glyphs;
14259 {
14260 struct window *w = XWINDOW (selected_window);
14261 struct buffer *buffer = XBUFFER (w->buffer);
14262
14263 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14264 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14265 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14266 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14267 fprintf (stderr, "=============================================\n");
14268 dump_glyph_matrix (w->current_matrix,
14269 NILP (glyphs) ? 0 : XINT (glyphs));
14270 return Qnil;
14271 }
14272
14273
14274 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14275 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14276 ()
14277 {
14278 struct frame *f = XFRAME (selected_frame);
14279 dump_glyph_matrix (f->current_matrix, 1);
14280 return Qnil;
14281 }
14282
14283
14284 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14285 doc: /* Dump glyph row ROW to stderr.
14286 GLYPH 0 means don't dump glyphs.
14287 GLYPH 1 means dump glyphs in short form.
14288 GLYPH > 1 or omitted means dump glyphs in long form. */)
14289 (row, glyphs)
14290 Lisp_Object row, glyphs;
14291 {
14292 struct glyph_matrix *matrix;
14293 int vpos;
14294
14295 CHECK_NUMBER (row);
14296 matrix = XWINDOW (selected_window)->current_matrix;
14297 vpos = XINT (row);
14298 if (vpos >= 0 && vpos < matrix->nrows)
14299 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14300 vpos,
14301 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14302 return Qnil;
14303 }
14304
14305
14306 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14307 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14308 GLYPH 0 means don't dump glyphs.
14309 GLYPH 1 means dump glyphs in short form.
14310 GLYPH > 1 or omitted means dump glyphs in long form. */)
14311 (row, glyphs)
14312 Lisp_Object row, glyphs;
14313 {
14314 struct frame *sf = SELECTED_FRAME ();
14315 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14316 int vpos;
14317
14318 CHECK_NUMBER (row);
14319 vpos = XINT (row);
14320 if (vpos >= 0 && vpos < m->nrows)
14321 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14322 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14323 return Qnil;
14324 }
14325
14326
14327 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14328 doc: /* Toggle tracing of redisplay.
14329 With ARG, turn tracing on if and only if ARG is positive. */)
14330 (arg)
14331 Lisp_Object arg;
14332 {
14333 if (NILP (arg))
14334 trace_redisplay_p = !trace_redisplay_p;
14335 else
14336 {
14337 arg = Fprefix_numeric_value (arg);
14338 trace_redisplay_p = XINT (arg) > 0;
14339 }
14340
14341 return Qnil;
14342 }
14343
14344
14345 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14346 doc: /* Like `format', but print result to stderr.
14347 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14348 (nargs, args)
14349 int nargs;
14350 Lisp_Object *args;
14351 {
14352 Lisp_Object s = Fformat (nargs, args);
14353 fprintf (stderr, "%s", SDATA (s));
14354 return Qnil;
14355 }
14356
14357 #endif /* GLYPH_DEBUG */
14358
14359
14360 \f
14361 /***********************************************************************
14362 Building Desired Matrix Rows
14363 ***********************************************************************/
14364
14365 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14366 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14367
14368 static struct glyph_row *
14369 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14370 struct window *w;
14371 Lisp_Object overlay_arrow_string;
14372 {
14373 struct frame *f = XFRAME (WINDOW_FRAME (w));
14374 struct buffer *buffer = XBUFFER (w->buffer);
14375 struct buffer *old = current_buffer;
14376 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14377 int arrow_len = SCHARS (overlay_arrow_string);
14378 const unsigned char *arrow_end = arrow_string + arrow_len;
14379 const unsigned char *p;
14380 struct it it;
14381 int multibyte_p;
14382 int n_glyphs_before;
14383
14384 set_buffer_temp (buffer);
14385 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14386 it.glyph_row->used[TEXT_AREA] = 0;
14387 SET_TEXT_POS (it.position, 0, 0);
14388
14389 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14390 p = arrow_string;
14391 while (p < arrow_end)
14392 {
14393 Lisp_Object face, ilisp;
14394
14395 /* Get the next character. */
14396 if (multibyte_p)
14397 it.c = string_char_and_length (p, arrow_len, &it.len);
14398 else
14399 it.c = *p, it.len = 1;
14400 p += it.len;
14401
14402 /* Get its face. */
14403 ilisp = make_number (p - arrow_string);
14404 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14405 it.face_id = compute_char_face (f, it.c, face);
14406
14407 /* Compute its width, get its glyphs. */
14408 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14409 SET_TEXT_POS (it.position, -1, -1);
14410 PRODUCE_GLYPHS (&it);
14411
14412 /* If this character doesn't fit any more in the line, we have
14413 to remove some glyphs. */
14414 if (it.current_x > it.last_visible_x)
14415 {
14416 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14417 break;
14418 }
14419 }
14420
14421 set_buffer_temp (old);
14422 return it.glyph_row;
14423 }
14424
14425
14426 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14427 glyphs are only inserted for terminal frames since we can't really
14428 win with truncation glyphs when partially visible glyphs are
14429 involved. Which glyphs to insert is determined by
14430 produce_special_glyphs. */
14431
14432 static void
14433 insert_left_trunc_glyphs (it)
14434 struct it *it;
14435 {
14436 struct it truncate_it;
14437 struct glyph *from, *end, *to, *toend;
14438
14439 xassert (!FRAME_WINDOW_P (it->f));
14440
14441 /* Get the truncation glyphs. */
14442 truncate_it = *it;
14443 truncate_it.current_x = 0;
14444 truncate_it.face_id = DEFAULT_FACE_ID;
14445 truncate_it.glyph_row = &scratch_glyph_row;
14446 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14447 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14448 truncate_it.object = make_number (0);
14449 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14450
14451 /* Overwrite glyphs from IT with truncation glyphs. */
14452 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14453 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14454 to = it->glyph_row->glyphs[TEXT_AREA];
14455 toend = to + it->glyph_row->used[TEXT_AREA];
14456
14457 while (from < end)
14458 *to++ = *from++;
14459
14460 /* There may be padding glyphs left over. Overwrite them too. */
14461 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14462 {
14463 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14464 while (from < end)
14465 *to++ = *from++;
14466 }
14467
14468 if (to > toend)
14469 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14470 }
14471
14472
14473 /* Compute the pixel height and width of IT->glyph_row.
14474
14475 Most of the time, ascent and height of a display line will be equal
14476 to the max_ascent and max_height values of the display iterator
14477 structure. This is not the case if
14478
14479 1. We hit ZV without displaying anything. In this case, max_ascent
14480 and max_height will be zero.
14481
14482 2. We have some glyphs that don't contribute to the line height.
14483 (The glyph row flag contributes_to_line_height_p is for future
14484 pixmap extensions).
14485
14486 The first case is easily covered by using default values because in
14487 these cases, the line height does not really matter, except that it
14488 must not be zero. */
14489
14490 static void
14491 compute_line_metrics (it)
14492 struct it *it;
14493 {
14494 struct glyph_row *row = it->glyph_row;
14495 int area, i;
14496
14497 if (FRAME_WINDOW_P (it->f))
14498 {
14499 int i, min_y, max_y;
14500
14501 /* The line may consist of one space only, that was added to
14502 place the cursor on it. If so, the row's height hasn't been
14503 computed yet. */
14504 if (row->height == 0)
14505 {
14506 if (it->max_ascent + it->max_descent == 0)
14507 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14508 row->ascent = it->max_ascent;
14509 row->height = it->max_ascent + it->max_descent;
14510 row->phys_ascent = it->max_phys_ascent;
14511 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14512 row->extra_line_spacing = it->max_extra_line_spacing;
14513 }
14514
14515 /* Compute the width of this line. */
14516 row->pixel_width = row->x;
14517 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14518 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14519
14520 xassert (row->pixel_width >= 0);
14521 xassert (row->ascent >= 0 && row->height > 0);
14522
14523 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14524 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14525
14526 /* If first line's physical ascent is larger than its logical
14527 ascent, use the physical ascent, and make the row taller.
14528 This makes accented characters fully visible. */
14529 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14530 && row->phys_ascent > row->ascent)
14531 {
14532 row->height += row->phys_ascent - row->ascent;
14533 row->ascent = row->phys_ascent;
14534 }
14535
14536 /* Compute how much of the line is visible. */
14537 row->visible_height = row->height;
14538
14539 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14540 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14541
14542 if (row->y < min_y)
14543 row->visible_height -= min_y - row->y;
14544 if (row->y + row->height > max_y)
14545 row->visible_height -= row->y + row->height - max_y;
14546 }
14547 else
14548 {
14549 row->pixel_width = row->used[TEXT_AREA];
14550 if (row->continued_p)
14551 row->pixel_width -= it->continuation_pixel_width;
14552 else if (row->truncated_on_right_p)
14553 row->pixel_width -= it->truncation_pixel_width;
14554 row->ascent = row->phys_ascent = 0;
14555 row->height = row->phys_height = row->visible_height = 1;
14556 row->extra_line_spacing = 0;
14557 }
14558
14559 /* Compute a hash code for this row. */
14560 row->hash = 0;
14561 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14562 for (i = 0; i < row->used[area]; ++i)
14563 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14564 + row->glyphs[area][i].u.val
14565 + row->glyphs[area][i].face_id
14566 + row->glyphs[area][i].padding_p
14567 + (row->glyphs[area][i].type << 2));
14568
14569 it->max_ascent = it->max_descent = 0;
14570 it->max_phys_ascent = it->max_phys_descent = 0;
14571 }
14572
14573
14574 /* Append one space to the glyph row of iterator IT if doing a
14575 window-based redisplay. The space has the same face as
14576 IT->face_id. Value is non-zero if a space was added.
14577
14578 This function is called to make sure that there is always one glyph
14579 at the end of a glyph row that the cursor can be set on under
14580 window-systems. (If there weren't such a glyph we would not know
14581 how wide and tall a box cursor should be displayed).
14582
14583 At the same time this space let's a nicely handle clearing to the
14584 end of the line if the row ends in italic text. */
14585
14586 static int
14587 append_space_for_newline (it, default_face_p)
14588 struct it *it;
14589 int default_face_p;
14590 {
14591 if (FRAME_WINDOW_P (it->f))
14592 {
14593 int n = it->glyph_row->used[TEXT_AREA];
14594
14595 if (it->glyph_row->glyphs[TEXT_AREA] + n
14596 < it->glyph_row->glyphs[1 + TEXT_AREA])
14597 {
14598 /* Save some values that must not be changed.
14599 Must save IT->c and IT->len because otherwise
14600 ITERATOR_AT_END_P wouldn't work anymore after
14601 append_space_for_newline has been called. */
14602 enum display_element_type saved_what = it->what;
14603 int saved_c = it->c, saved_len = it->len;
14604 int saved_x = it->current_x;
14605 int saved_face_id = it->face_id;
14606 struct text_pos saved_pos;
14607 Lisp_Object saved_object;
14608 struct face *face;
14609
14610 saved_object = it->object;
14611 saved_pos = it->position;
14612
14613 it->what = IT_CHARACTER;
14614 bzero (&it->position, sizeof it->position);
14615 it->object = make_number (0);
14616 it->c = ' ';
14617 it->len = 1;
14618
14619 if (default_face_p)
14620 it->face_id = DEFAULT_FACE_ID;
14621 else if (it->face_before_selective_p)
14622 it->face_id = it->saved_face_id;
14623 face = FACE_FROM_ID (it->f, it->face_id);
14624 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14625
14626 PRODUCE_GLYPHS (it);
14627
14628 it->override_ascent = -1;
14629 it->constrain_row_ascent_descent_p = 0;
14630 it->current_x = saved_x;
14631 it->object = saved_object;
14632 it->position = saved_pos;
14633 it->what = saved_what;
14634 it->face_id = saved_face_id;
14635 it->len = saved_len;
14636 it->c = saved_c;
14637 return 1;
14638 }
14639 }
14640
14641 return 0;
14642 }
14643
14644
14645 /* Extend the face of the last glyph in the text area of IT->glyph_row
14646 to the end of the display line. Called from display_line.
14647 If the glyph row is empty, add a space glyph to it so that we
14648 know the face to draw. Set the glyph row flag fill_line_p. */
14649
14650 static void
14651 extend_face_to_end_of_line (it)
14652 struct it *it;
14653 {
14654 struct face *face;
14655 struct frame *f = it->f;
14656
14657 /* If line is already filled, do nothing. */
14658 if (it->current_x >= it->last_visible_x)
14659 return;
14660
14661 /* Face extension extends the background and box of IT->face_id
14662 to the end of the line. If the background equals the background
14663 of the frame, we don't have to do anything. */
14664 if (it->face_before_selective_p)
14665 face = FACE_FROM_ID (it->f, it->saved_face_id);
14666 else
14667 face = FACE_FROM_ID (f, it->face_id);
14668
14669 if (FRAME_WINDOW_P (f)
14670 && face->box == FACE_NO_BOX
14671 && face->background == FRAME_BACKGROUND_PIXEL (f)
14672 && !face->stipple)
14673 return;
14674
14675 /* Set the glyph row flag indicating that the face of the last glyph
14676 in the text area has to be drawn to the end of the text area. */
14677 it->glyph_row->fill_line_p = 1;
14678
14679 /* If current character of IT is not ASCII, make sure we have the
14680 ASCII face. This will be automatically undone the next time
14681 get_next_display_element returns a multibyte character. Note
14682 that the character will always be single byte in unibyte text. */
14683 if (!SINGLE_BYTE_CHAR_P (it->c))
14684 {
14685 it->face_id = FACE_FOR_CHAR (f, face, 0);
14686 }
14687
14688 if (FRAME_WINDOW_P (f))
14689 {
14690 /* If the row is empty, add a space with the current face of IT,
14691 so that we know which face to draw. */
14692 if (it->glyph_row->used[TEXT_AREA] == 0)
14693 {
14694 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14695 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14696 it->glyph_row->used[TEXT_AREA] = 1;
14697 }
14698 }
14699 else
14700 {
14701 /* Save some values that must not be changed. */
14702 int saved_x = it->current_x;
14703 struct text_pos saved_pos;
14704 Lisp_Object saved_object;
14705 enum display_element_type saved_what = it->what;
14706 int saved_face_id = it->face_id;
14707
14708 saved_object = it->object;
14709 saved_pos = it->position;
14710
14711 it->what = IT_CHARACTER;
14712 bzero (&it->position, sizeof it->position);
14713 it->object = make_number (0);
14714 it->c = ' ';
14715 it->len = 1;
14716 it->face_id = face->id;
14717
14718 PRODUCE_GLYPHS (it);
14719
14720 while (it->current_x <= it->last_visible_x)
14721 PRODUCE_GLYPHS (it);
14722
14723 /* Don't count these blanks really. It would let us insert a left
14724 truncation glyph below and make us set the cursor on them, maybe. */
14725 it->current_x = saved_x;
14726 it->object = saved_object;
14727 it->position = saved_pos;
14728 it->what = saved_what;
14729 it->face_id = saved_face_id;
14730 }
14731 }
14732
14733
14734 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14735 trailing whitespace. */
14736
14737 static int
14738 trailing_whitespace_p (charpos)
14739 int charpos;
14740 {
14741 int bytepos = CHAR_TO_BYTE (charpos);
14742 int c = 0;
14743
14744 while (bytepos < ZV_BYTE
14745 && (c = FETCH_CHAR (bytepos),
14746 c == ' ' || c == '\t'))
14747 ++bytepos;
14748
14749 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14750 {
14751 if (bytepos != PT_BYTE)
14752 return 1;
14753 }
14754 return 0;
14755 }
14756
14757
14758 /* Highlight trailing whitespace, if any, in ROW. */
14759
14760 void
14761 highlight_trailing_whitespace (f, row)
14762 struct frame *f;
14763 struct glyph_row *row;
14764 {
14765 int used = row->used[TEXT_AREA];
14766
14767 if (used)
14768 {
14769 struct glyph *start = row->glyphs[TEXT_AREA];
14770 struct glyph *glyph = start + used - 1;
14771
14772 /* Skip over glyphs inserted to display the cursor at the
14773 end of a line, for extending the face of the last glyph
14774 to the end of the line on terminals, and for truncation
14775 and continuation glyphs. */
14776 while (glyph >= start
14777 && glyph->type == CHAR_GLYPH
14778 && INTEGERP (glyph->object))
14779 --glyph;
14780
14781 /* If last glyph is a space or stretch, and it's trailing
14782 whitespace, set the face of all trailing whitespace glyphs in
14783 IT->glyph_row to `trailing-whitespace'. */
14784 if (glyph >= start
14785 && BUFFERP (glyph->object)
14786 && (glyph->type == STRETCH_GLYPH
14787 || (glyph->type == CHAR_GLYPH
14788 && glyph->u.ch == ' '))
14789 && trailing_whitespace_p (glyph->charpos))
14790 {
14791 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14792 if (face_id < 0)
14793 return;
14794
14795 while (glyph >= start
14796 && BUFFERP (glyph->object)
14797 && (glyph->type == STRETCH_GLYPH
14798 || (glyph->type == CHAR_GLYPH
14799 && glyph->u.ch == ' ')))
14800 (glyph--)->face_id = face_id;
14801 }
14802 }
14803 }
14804
14805
14806 /* Value is non-zero if glyph row ROW in window W should be
14807 used to hold the cursor. */
14808
14809 static int
14810 cursor_row_p (w, row)
14811 struct window *w;
14812 struct glyph_row *row;
14813 {
14814 int cursor_row_p = 1;
14815
14816 if (PT == MATRIX_ROW_END_CHARPOS (row))
14817 {
14818 /* If the row ends with a newline from a string, we don't want
14819 the cursor there (if the row is continued it doesn't end in a
14820 newline). */
14821 if (CHARPOS (row->end.string_pos) >= 0)
14822 cursor_row_p = row->continued_p;
14823 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14824 {
14825 /* If the row ends in middle of a real character,
14826 and the line is continued, we want the cursor here.
14827 That's because MATRIX_ROW_END_CHARPOS would equal
14828 PT if PT is before the character. */
14829 if (!row->ends_in_ellipsis_p)
14830 cursor_row_p = row->continued_p;
14831 else
14832 /* If the row ends in an ellipsis, then
14833 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14834 We want that position to be displayed after the ellipsis. */
14835 cursor_row_p = 0;
14836 }
14837 /* If the row ends at ZV, display the cursor at the end of that
14838 row instead of at the start of the row below. */
14839 else if (row->ends_at_zv_p)
14840 cursor_row_p = 1;
14841 else
14842 cursor_row_p = 0;
14843 }
14844
14845 return cursor_row_p;
14846 }
14847
14848
14849 /* Construct the glyph row IT->glyph_row in the desired matrix of
14850 IT->w from text at the current position of IT. See dispextern.h
14851 for an overview of struct it. Value is non-zero if
14852 IT->glyph_row displays text, as opposed to a line displaying ZV
14853 only. */
14854
14855 static int
14856 display_line (it)
14857 struct it *it;
14858 {
14859 struct glyph_row *row = it->glyph_row;
14860 Lisp_Object overlay_arrow_string;
14861
14862 /* We always start displaying at hpos zero even if hscrolled. */
14863 xassert (it->hpos == 0 && it->current_x == 0);
14864
14865 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14866 >= it->w->desired_matrix->nrows)
14867 {
14868 it->w->nrows_scale_factor++;
14869 fonts_changed_p = 1;
14870 return 0;
14871 }
14872
14873 /* Is IT->w showing the region? */
14874 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14875
14876 /* Clear the result glyph row and enable it. */
14877 prepare_desired_row (row);
14878
14879 row->y = it->current_y;
14880 row->start = it->start;
14881 row->continuation_lines_width = it->continuation_lines_width;
14882 row->displays_text_p = 1;
14883 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14884 it->starts_in_middle_of_char_p = 0;
14885
14886 /* Arrange the overlays nicely for our purposes. Usually, we call
14887 display_line on only one line at a time, in which case this
14888 can't really hurt too much, or we call it on lines which appear
14889 one after another in the buffer, in which case all calls to
14890 recenter_overlay_lists but the first will be pretty cheap. */
14891 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14892
14893 /* Move over display elements that are not visible because we are
14894 hscrolled. This may stop at an x-position < IT->first_visible_x
14895 if the first glyph is partially visible or if we hit a line end. */
14896 if (it->current_x < it->first_visible_x)
14897 {
14898 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14899 MOVE_TO_POS | MOVE_TO_X);
14900 }
14901
14902 /* Get the initial row height. This is either the height of the
14903 text hscrolled, if there is any, or zero. */
14904 row->ascent = it->max_ascent;
14905 row->height = it->max_ascent + it->max_descent;
14906 row->phys_ascent = it->max_phys_ascent;
14907 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14908 row->extra_line_spacing = it->max_extra_line_spacing;
14909
14910 /* Loop generating characters. The loop is left with IT on the next
14911 character to display. */
14912 while (1)
14913 {
14914 int n_glyphs_before, hpos_before, x_before;
14915 int x, i, nglyphs;
14916 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14917
14918 /* Retrieve the next thing to display. Value is zero if end of
14919 buffer reached. */
14920 if (!get_next_display_element (it))
14921 {
14922 /* Maybe add a space at the end of this line that is used to
14923 display the cursor there under X. Set the charpos of the
14924 first glyph of blank lines not corresponding to any text
14925 to -1. */
14926 #ifdef HAVE_WINDOW_SYSTEM
14927 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14928 row->exact_window_width_line_p = 1;
14929 else
14930 #endif /* HAVE_WINDOW_SYSTEM */
14931 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14932 || row->used[TEXT_AREA] == 0)
14933 {
14934 row->glyphs[TEXT_AREA]->charpos = -1;
14935 row->displays_text_p = 0;
14936
14937 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14938 && (!MINI_WINDOW_P (it->w)
14939 || (minibuf_level && EQ (it->window, minibuf_window))))
14940 row->indicate_empty_line_p = 1;
14941 }
14942
14943 it->continuation_lines_width = 0;
14944 row->ends_at_zv_p = 1;
14945 break;
14946 }
14947
14948 /* Now, get the metrics of what we want to display. This also
14949 generates glyphs in `row' (which is IT->glyph_row). */
14950 n_glyphs_before = row->used[TEXT_AREA];
14951 x = it->current_x;
14952
14953 /* Remember the line height so far in case the next element doesn't
14954 fit on the line. */
14955 if (!it->truncate_lines_p)
14956 {
14957 ascent = it->max_ascent;
14958 descent = it->max_descent;
14959 phys_ascent = it->max_phys_ascent;
14960 phys_descent = it->max_phys_descent;
14961 }
14962
14963 PRODUCE_GLYPHS (it);
14964
14965 /* If this display element was in marginal areas, continue with
14966 the next one. */
14967 if (it->area != TEXT_AREA)
14968 {
14969 row->ascent = max (row->ascent, it->max_ascent);
14970 row->height = max (row->height, it->max_ascent + it->max_descent);
14971 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14972 row->phys_height = max (row->phys_height,
14973 it->max_phys_ascent + it->max_phys_descent);
14974 row->extra_line_spacing = max (row->extra_line_spacing,
14975 it->max_extra_line_spacing);
14976 set_iterator_to_next (it, 1);
14977 continue;
14978 }
14979
14980 /* Does the display element fit on the line? If we truncate
14981 lines, we should draw past the right edge of the window. If
14982 we don't truncate, we want to stop so that we can display the
14983 continuation glyph before the right margin. If lines are
14984 continued, there are two possible strategies for characters
14985 resulting in more than 1 glyph (e.g. tabs): Display as many
14986 glyphs as possible in this line and leave the rest for the
14987 continuation line, or display the whole element in the next
14988 line. Original redisplay did the former, so we do it also. */
14989 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14990 hpos_before = it->hpos;
14991 x_before = x;
14992
14993 if (/* Not a newline. */
14994 nglyphs > 0
14995 /* Glyphs produced fit entirely in the line. */
14996 && it->current_x < it->last_visible_x)
14997 {
14998 it->hpos += nglyphs;
14999 row->ascent = max (row->ascent, it->max_ascent);
15000 row->height = max (row->height, it->max_ascent + it->max_descent);
15001 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15002 row->phys_height = max (row->phys_height,
15003 it->max_phys_ascent + it->max_phys_descent);
15004 row->extra_line_spacing = max (row->extra_line_spacing,
15005 it->max_extra_line_spacing);
15006 if (it->current_x - it->pixel_width < it->first_visible_x)
15007 row->x = x - it->first_visible_x;
15008 }
15009 else
15010 {
15011 int new_x;
15012 struct glyph *glyph;
15013
15014 for (i = 0; i < nglyphs; ++i, x = new_x)
15015 {
15016 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15017 new_x = x + glyph->pixel_width;
15018
15019 if (/* Lines are continued. */
15020 !it->truncate_lines_p
15021 && (/* Glyph doesn't fit on the line. */
15022 new_x > it->last_visible_x
15023 /* Or it fits exactly on a window system frame. */
15024 || (new_x == it->last_visible_x
15025 && FRAME_WINDOW_P (it->f))))
15026 {
15027 /* End of a continued line. */
15028
15029 if (it->hpos == 0
15030 || (new_x == it->last_visible_x
15031 && FRAME_WINDOW_P (it->f)))
15032 {
15033 /* Current glyph is the only one on the line or
15034 fits exactly on the line. We must continue
15035 the line because we can't draw the cursor
15036 after the glyph. */
15037 row->continued_p = 1;
15038 it->current_x = new_x;
15039 it->continuation_lines_width += new_x;
15040 ++it->hpos;
15041 if (i == nglyphs - 1)
15042 {
15043 set_iterator_to_next (it, 1);
15044 #ifdef HAVE_WINDOW_SYSTEM
15045 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15046 {
15047 if (!get_next_display_element (it))
15048 {
15049 row->exact_window_width_line_p = 1;
15050 it->continuation_lines_width = 0;
15051 row->continued_p = 0;
15052 row->ends_at_zv_p = 1;
15053 }
15054 else if (ITERATOR_AT_END_OF_LINE_P (it))
15055 {
15056 row->continued_p = 0;
15057 row->exact_window_width_line_p = 1;
15058 }
15059 }
15060 #endif /* HAVE_WINDOW_SYSTEM */
15061 }
15062 }
15063 else if (CHAR_GLYPH_PADDING_P (*glyph)
15064 && !FRAME_WINDOW_P (it->f))
15065 {
15066 /* A padding glyph that doesn't fit on this line.
15067 This means the whole character doesn't fit
15068 on the line. */
15069 row->used[TEXT_AREA] = n_glyphs_before;
15070
15071 /* Fill the rest of the row with continuation
15072 glyphs like in 20.x. */
15073 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15074 < row->glyphs[1 + TEXT_AREA])
15075 produce_special_glyphs (it, IT_CONTINUATION);
15076
15077 row->continued_p = 1;
15078 it->current_x = x_before;
15079 it->continuation_lines_width += x_before;
15080
15081 /* Restore the height to what it was before the
15082 element not fitting on the line. */
15083 it->max_ascent = ascent;
15084 it->max_descent = descent;
15085 it->max_phys_ascent = phys_ascent;
15086 it->max_phys_descent = phys_descent;
15087 }
15088 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15089 {
15090 /* A TAB that extends past the right edge of the
15091 window. This produces a single glyph on
15092 window system frames. We leave the glyph in
15093 this row and let it fill the row, but don't
15094 consume the TAB. */
15095 it->continuation_lines_width += it->last_visible_x;
15096 row->ends_in_middle_of_char_p = 1;
15097 row->continued_p = 1;
15098 glyph->pixel_width = it->last_visible_x - x;
15099 it->starts_in_middle_of_char_p = 1;
15100 }
15101 else
15102 {
15103 /* Something other than a TAB that draws past
15104 the right edge of the window. Restore
15105 positions to values before the element. */
15106 row->used[TEXT_AREA] = n_glyphs_before + i;
15107
15108 /* Display continuation glyphs. */
15109 if (!FRAME_WINDOW_P (it->f))
15110 produce_special_glyphs (it, IT_CONTINUATION);
15111 row->continued_p = 1;
15112
15113 it->continuation_lines_width += x;
15114
15115 if (nglyphs > 1 && i > 0)
15116 {
15117 row->ends_in_middle_of_char_p = 1;
15118 it->starts_in_middle_of_char_p = 1;
15119 }
15120
15121 /* Restore the height to what it was before the
15122 element not fitting on the line. */
15123 it->max_ascent = ascent;
15124 it->max_descent = descent;
15125 it->max_phys_ascent = phys_ascent;
15126 it->max_phys_descent = phys_descent;
15127 }
15128
15129 break;
15130 }
15131 else if (new_x > it->first_visible_x)
15132 {
15133 /* Increment number of glyphs actually displayed. */
15134 ++it->hpos;
15135
15136 if (x < it->first_visible_x)
15137 /* Glyph is partially visible, i.e. row starts at
15138 negative X position. */
15139 row->x = x - it->first_visible_x;
15140 }
15141 else
15142 {
15143 /* Glyph is completely off the left margin of the
15144 window. This should not happen because of the
15145 move_it_in_display_line at the start of this
15146 function, unless the text display area of the
15147 window is empty. */
15148 xassert (it->first_visible_x <= it->last_visible_x);
15149 }
15150 }
15151
15152 row->ascent = max (row->ascent, it->max_ascent);
15153 row->height = max (row->height, it->max_ascent + it->max_descent);
15154 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15155 row->phys_height = max (row->phys_height,
15156 it->max_phys_ascent + it->max_phys_descent);
15157 row->extra_line_spacing = max (row->extra_line_spacing,
15158 it->max_extra_line_spacing);
15159
15160 /* End of this display line if row is continued. */
15161 if (row->continued_p || row->ends_at_zv_p)
15162 break;
15163 }
15164
15165 at_end_of_line:
15166 /* Is this a line end? If yes, we're also done, after making
15167 sure that a non-default face is extended up to the right
15168 margin of the window. */
15169 if (ITERATOR_AT_END_OF_LINE_P (it))
15170 {
15171 int used_before = row->used[TEXT_AREA];
15172
15173 row->ends_in_newline_from_string_p = STRINGP (it->object);
15174
15175 #ifdef HAVE_WINDOW_SYSTEM
15176 /* Add a space at the end of the line that is used to
15177 display the cursor there. */
15178 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15179 append_space_for_newline (it, 0);
15180 #endif /* HAVE_WINDOW_SYSTEM */
15181
15182 /* Extend the face to the end of the line. */
15183 extend_face_to_end_of_line (it);
15184
15185 /* Make sure we have the position. */
15186 if (used_before == 0)
15187 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15188
15189 /* Consume the line end. This skips over invisible lines. */
15190 set_iterator_to_next (it, 1);
15191 it->continuation_lines_width = 0;
15192 break;
15193 }
15194
15195 /* Proceed with next display element. Note that this skips
15196 over lines invisible because of selective display. */
15197 set_iterator_to_next (it, 1);
15198
15199 /* If we truncate lines, we are done when the last displayed
15200 glyphs reach past the right margin of the window. */
15201 if (it->truncate_lines_p
15202 && (FRAME_WINDOW_P (it->f)
15203 ? (it->current_x >= it->last_visible_x)
15204 : (it->current_x > it->last_visible_x)))
15205 {
15206 /* Maybe add truncation glyphs. */
15207 if (!FRAME_WINDOW_P (it->f))
15208 {
15209 int i, n;
15210
15211 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15212 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15213 break;
15214
15215 for (n = row->used[TEXT_AREA]; i < n; ++i)
15216 {
15217 row->used[TEXT_AREA] = i;
15218 produce_special_glyphs (it, IT_TRUNCATION);
15219 }
15220 }
15221 #ifdef HAVE_WINDOW_SYSTEM
15222 else
15223 {
15224 /* Don't truncate if we can overflow newline into fringe. */
15225 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15226 {
15227 if (!get_next_display_element (it))
15228 {
15229 it->continuation_lines_width = 0;
15230 row->ends_at_zv_p = 1;
15231 row->exact_window_width_line_p = 1;
15232 break;
15233 }
15234 if (ITERATOR_AT_END_OF_LINE_P (it))
15235 {
15236 row->exact_window_width_line_p = 1;
15237 goto at_end_of_line;
15238 }
15239 }
15240 }
15241 #endif /* HAVE_WINDOW_SYSTEM */
15242
15243 row->truncated_on_right_p = 1;
15244 it->continuation_lines_width = 0;
15245 reseat_at_next_visible_line_start (it, 0);
15246 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15247 it->hpos = hpos_before;
15248 it->current_x = x_before;
15249 break;
15250 }
15251 }
15252
15253 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15254 at the left window margin. */
15255 if (it->first_visible_x
15256 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15257 {
15258 if (!FRAME_WINDOW_P (it->f))
15259 insert_left_trunc_glyphs (it);
15260 row->truncated_on_left_p = 1;
15261 }
15262
15263 /* If the start of this line is the overlay arrow-position, then
15264 mark this glyph row as the one containing the overlay arrow.
15265 This is clearly a mess with variable size fonts. It would be
15266 better to let it be displayed like cursors under X. */
15267 if ((row->displays_text_p || !overlay_arrow_seen)
15268 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15269 !NILP (overlay_arrow_string)))
15270 {
15271 /* Overlay arrow in window redisplay is a fringe bitmap. */
15272 if (STRINGP (overlay_arrow_string))
15273 {
15274 struct glyph_row *arrow_row
15275 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15276 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15277 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15278 struct glyph *p = row->glyphs[TEXT_AREA];
15279 struct glyph *p2, *end;
15280
15281 /* Copy the arrow glyphs. */
15282 while (glyph < arrow_end)
15283 *p++ = *glyph++;
15284
15285 /* Throw away padding glyphs. */
15286 p2 = p;
15287 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15288 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15289 ++p2;
15290 if (p2 > p)
15291 {
15292 while (p2 < end)
15293 *p++ = *p2++;
15294 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15295 }
15296 }
15297 else
15298 {
15299 xassert (INTEGERP (overlay_arrow_string));
15300 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15301 }
15302 overlay_arrow_seen = 1;
15303 }
15304
15305 /* Compute pixel dimensions of this line. */
15306 compute_line_metrics (it);
15307
15308 /* Remember the position at which this line ends. */
15309 row->end = it->current;
15310
15311 /* Record whether this row ends inside an ellipsis. */
15312 row->ends_in_ellipsis_p
15313 = (it->method == GET_FROM_DISPLAY_VECTOR
15314 && it->ellipsis_p);
15315
15316 /* Save fringe bitmaps in this row. */
15317 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15318 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15319 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15320 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15321
15322 it->left_user_fringe_bitmap = 0;
15323 it->left_user_fringe_face_id = 0;
15324 it->right_user_fringe_bitmap = 0;
15325 it->right_user_fringe_face_id = 0;
15326
15327 /* Maybe set the cursor. */
15328 if (it->w->cursor.vpos < 0
15329 && PT >= MATRIX_ROW_START_CHARPOS (row)
15330 && PT <= MATRIX_ROW_END_CHARPOS (row)
15331 && cursor_row_p (it->w, row))
15332 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15333
15334 /* Highlight trailing whitespace. */
15335 if (!NILP (Vshow_trailing_whitespace))
15336 highlight_trailing_whitespace (it->f, it->glyph_row);
15337
15338 /* Prepare for the next line. This line starts horizontally at (X
15339 HPOS) = (0 0). Vertical positions are incremented. As a
15340 convenience for the caller, IT->glyph_row is set to the next
15341 row to be used. */
15342 it->current_x = it->hpos = 0;
15343 it->current_y += row->height;
15344 ++it->vpos;
15345 ++it->glyph_row;
15346 it->start = it->current;
15347 return row->displays_text_p;
15348 }
15349
15350
15351 \f
15352 /***********************************************************************
15353 Menu Bar
15354 ***********************************************************************/
15355
15356 /* Redisplay the menu bar in the frame for window W.
15357
15358 The menu bar of X frames that don't have X toolkit support is
15359 displayed in a special window W->frame->menu_bar_window.
15360
15361 The menu bar of terminal frames is treated specially as far as
15362 glyph matrices are concerned. Menu bar lines are not part of
15363 windows, so the update is done directly on the frame matrix rows
15364 for the menu bar. */
15365
15366 static void
15367 display_menu_bar (w)
15368 struct window *w;
15369 {
15370 struct frame *f = XFRAME (WINDOW_FRAME (w));
15371 struct it it;
15372 Lisp_Object items;
15373 int i;
15374
15375 /* Don't do all this for graphical frames. */
15376 #ifdef HAVE_NTGUI
15377 if (!NILP (Vwindow_system))
15378 return;
15379 #endif
15380 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15381 if (FRAME_X_P (f))
15382 return;
15383 #endif
15384 #ifdef MAC_OS
15385 if (FRAME_MAC_P (f))
15386 return;
15387 #endif
15388
15389 #ifdef USE_X_TOOLKIT
15390 xassert (!FRAME_WINDOW_P (f));
15391 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15392 it.first_visible_x = 0;
15393 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15394 #else /* not USE_X_TOOLKIT */
15395 if (FRAME_WINDOW_P (f))
15396 {
15397 /* Menu bar lines are displayed in the desired matrix of the
15398 dummy window menu_bar_window. */
15399 struct window *menu_w;
15400 xassert (WINDOWP (f->menu_bar_window));
15401 menu_w = XWINDOW (f->menu_bar_window);
15402 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15403 MENU_FACE_ID);
15404 it.first_visible_x = 0;
15405 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15406 }
15407 else
15408 {
15409 /* This is a TTY frame, i.e. character hpos/vpos are used as
15410 pixel x/y. */
15411 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15412 MENU_FACE_ID);
15413 it.first_visible_x = 0;
15414 it.last_visible_x = FRAME_COLS (f);
15415 }
15416 #endif /* not USE_X_TOOLKIT */
15417
15418 if (! mode_line_inverse_video)
15419 /* Force the menu-bar to be displayed in the default face. */
15420 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15421
15422 /* Clear all rows of the menu bar. */
15423 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15424 {
15425 struct glyph_row *row = it.glyph_row + i;
15426 clear_glyph_row (row);
15427 row->enabled_p = 1;
15428 row->full_width_p = 1;
15429 }
15430
15431 /* Display all items of the menu bar. */
15432 items = FRAME_MENU_BAR_ITEMS (it.f);
15433 for (i = 0; i < XVECTOR (items)->size; i += 4)
15434 {
15435 Lisp_Object string;
15436
15437 /* Stop at nil string. */
15438 string = AREF (items, i + 1);
15439 if (NILP (string))
15440 break;
15441
15442 /* Remember where item was displayed. */
15443 AREF (items, i + 3) = make_number (it.hpos);
15444
15445 /* Display the item, pad with one space. */
15446 if (it.current_x < it.last_visible_x)
15447 display_string (NULL, string, Qnil, 0, 0, &it,
15448 SCHARS (string) + 1, 0, 0, -1);
15449 }
15450
15451 /* Fill out the line with spaces. */
15452 if (it.current_x < it.last_visible_x)
15453 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15454
15455 /* Compute the total height of the lines. */
15456 compute_line_metrics (&it);
15457 }
15458
15459
15460 \f
15461 /***********************************************************************
15462 Mode Line
15463 ***********************************************************************/
15464
15465 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15466 FORCE is non-zero, redisplay mode lines unconditionally.
15467 Otherwise, redisplay only mode lines that are garbaged. Value is
15468 the number of windows whose mode lines were redisplayed. */
15469
15470 static int
15471 redisplay_mode_lines (window, force)
15472 Lisp_Object window;
15473 int force;
15474 {
15475 int nwindows = 0;
15476
15477 while (!NILP (window))
15478 {
15479 struct window *w = XWINDOW (window);
15480
15481 if (WINDOWP (w->hchild))
15482 nwindows += redisplay_mode_lines (w->hchild, force);
15483 else if (WINDOWP (w->vchild))
15484 nwindows += redisplay_mode_lines (w->vchild, force);
15485 else if (force
15486 || FRAME_GARBAGED_P (XFRAME (w->frame))
15487 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15488 {
15489 struct text_pos lpoint;
15490 struct buffer *old = current_buffer;
15491
15492 /* Set the window's buffer for the mode line display. */
15493 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15494 set_buffer_internal_1 (XBUFFER (w->buffer));
15495
15496 /* Point refers normally to the selected window. For any
15497 other window, set up appropriate value. */
15498 if (!EQ (window, selected_window))
15499 {
15500 struct text_pos pt;
15501
15502 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15503 if (CHARPOS (pt) < BEGV)
15504 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15505 else if (CHARPOS (pt) > (ZV - 1))
15506 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15507 else
15508 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15509 }
15510
15511 /* Display mode lines. */
15512 clear_glyph_matrix (w->desired_matrix);
15513 if (display_mode_lines (w))
15514 {
15515 ++nwindows;
15516 w->must_be_updated_p = 1;
15517 }
15518
15519 /* Restore old settings. */
15520 set_buffer_internal_1 (old);
15521 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15522 }
15523
15524 window = w->next;
15525 }
15526
15527 return nwindows;
15528 }
15529
15530
15531 /* Display the mode and/or top line of window W. Value is the number
15532 of mode lines displayed. */
15533
15534 static int
15535 display_mode_lines (w)
15536 struct window *w;
15537 {
15538 Lisp_Object old_selected_window, old_selected_frame;
15539 int n = 0;
15540
15541 old_selected_frame = selected_frame;
15542 selected_frame = w->frame;
15543 old_selected_window = selected_window;
15544 XSETWINDOW (selected_window, w);
15545
15546 /* These will be set while the mode line specs are processed. */
15547 line_number_displayed = 0;
15548 w->column_number_displayed = Qnil;
15549
15550 if (WINDOW_WANTS_MODELINE_P (w))
15551 {
15552 struct window *sel_w = XWINDOW (old_selected_window);
15553
15554 /* Select mode line face based on the real selected window. */
15555 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15556 current_buffer->mode_line_format);
15557 ++n;
15558 }
15559
15560 if (WINDOW_WANTS_HEADER_LINE_P (w))
15561 {
15562 display_mode_line (w, HEADER_LINE_FACE_ID,
15563 current_buffer->header_line_format);
15564 ++n;
15565 }
15566
15567 selected_frame = old_selected_frame;
15568 selected_window = old_selected_window;
15569 return n;
15570 }
15571
15572
15573 /* Display mode or top line of window W. FACE_ID specifies which line
15574 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15575 FORMAT is the mode line format to display. Value is the pixel
15576 height of the mode line displayed. */
15577
15578 static int
15579 display_mode_line (w, face_id, format)
15580 struct window *w;
15581 enum face_id face_id;
15582 Lisp_Object format;
15583 {
15584 struct it it;
15585 struct face *face;
15586
15587 init_iterator (&it, w, -1, -1, NULL, face_id);
15588 prepare_desired_row (it.glyph_row);
15589
15590 it.glyph_row->mode_line_p = 1;
15591
15592 if (! mode_line_inverse_video)
15593 /* Force the mode-line to be displayed in the default face. */
15594 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15595
15596 /* Temporarily make frame's keyboard the current kboard so that
15597 kboard-local variables in the mode_line_format will get the right
15598 values. */
15599 push_frame_kboard (it.f);
15600 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15601 pop_frame_kboard ();
15602
15603 /* Fill up with spaces. */
15604 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15605
15606 compute_line_metrics (&it);
15607 it.glyph_row->full_width_p = 1;
15608 it.glyph_row->continued_p = 0;
15609 it.glyph_row->truncated_on_left_p = 0;
15610 it.glyph_row->truncated_on_right_p = 0;
15611
15612 /* Make a 3D mode-line have a shadow at its right end. */
15613 face = FACE_FROM_ID (it.f, face_id);
15614 extend_face_to_end_of_line (&it);
15615 if (face->box != FACE_NO_BOX)
15616 {
15617 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15618 + it.glyph_row->used[TEXT_AREA] - 1);
15619 last->right_box_line_p = 1;
15620 }
15621
15622 return it.glyph_row->height;
15623 }
15624
15625 /* Alist that caches the results of :propertize.
15626 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15627 Lisp_Object mode_line_proptrans_alist;
15628
15629 /* List of strings making up the mode-line. */
15630 Lisp_Object mode_line_string_list;
15631
15632 /* Base face property when building propertized mode line string. */
15633 static Lisp_Object mode_line_string_face;
15634 static Lisp_Object mode_line_string_face_prop;
15635
15636
15637 /* Contribute ELT to the mode line for window IT->w. How it
15638 translates into text depends on its data type.
15639
15640 IT describes the display environment in which we display, as usual.
15641
15642 DEPTH is the depth in recursion. It is used to prevent
15643 infinite recursion here.
15644
15645 FIELD_WIDTH is the number of characters the display of ELT should
15646 occupy in the mode line, and PRECISION is the maximum number of
15647 characters to display from ELT's representation. See
15648 display_string for details.
15649
15650 Returns the hpos of the end of the text generated by ELT.
15651
15652 PROPS is a property list to add to any string we encounter.
15653
15654 If RISKY is nonzero, remove (disregard) any properties in any string
15655 we encounter, and ignore :eval and :propertize.
15656
15657 If the global variable `frame_title_ptr' is non-NULL, then the output
15658 is passed to `store_frame_title' instead of `display_string'. */
15659
15660 static int
15661 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15662 struct it *it;
15663 int depth;
15664 int field_width, precision;
15665 Lisp_Object elt, props;
15666 int risky;
15667 {
15668 int n = 0, field, prec;
15669 int literal = 0;
15670
15671 tail_recurse:
15672 if (depth > 100)
15673 elt = build_string ("*too-deep*");
15674
15675 depth++;
15676
15677 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15678 {
15679 case Lisp_String:
15680 {
15681 /* A string: output it and check for %-constructs within it. */
15682 unsigned char c;
15683 const unsigned char *this, *lisp_string;
15684
15685 if (!NILP (props) || risky)
15686 {
15687 Lisp_Object oprops, aelt;
15688 oprops = Ftext_properties_at (make_number (0), elt);
15689
15690 /* If the starting string's properties are not what
15691 we want, translate the string. Also, if the string
15692 is risky, do that anyway. */
15693
15694 if (NILP (Fequal (props, oprops)) || risky)
15695 {
15696 /* If the starting string has properties,
15697 merge the specified ones onto the existing ones. */
15698 if (! NILP (oprops) && !risky)
15699 {
15700 Lisp_Object tem;
15701
15702 oprops = Fcopy_sequence (oprops);
15703 tem = props;
15704 while (CONSP (tem))
15705 {
15706 oprops = Fplist_put (oprops, XCAR (tem),
15707 XCAR (XCDR (tem)));
15708 tem = XCDR (XCDR (tem));
15709 }
15710 props = oprops;
15711 }
15712
15713 aelt = Fassoc (elt, mode_line_proptrans_alist);
15714 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15715 {
15716 mode_line_proptrans_alist
15717 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15718 elt = XCAR (aelt);
15719 }
15720 else
15721 {
15722 Lisp_Object tem;
15723
15724 elt = Fcopy_sequence (elt);
15725 Fset_text_properties (make_number (0), Flength (elt),
15726 props, elt);
15727 /* Add this item to mode_line_proptrans_alist. */
15728 mode_line_proptrans_alist
15729 = Fcons (Fcons (elt, props),
15730 mode_line_proptrans_alist);
15731 /* Truncate mode_line_proptrans_alist
15732 to at most 50 elements. */
15733 tem = Fnthcdr (make_number (50),
15734 mode_line_proptrans_alist);
15735 if (! NILP (tem))
15736 XSETCDR (tem, Qnil);
15737 }
15738 }
15739 }
15740
15741 this = SDATA (elt);
15742 lisp_string = this;
15743
15744 if (literal)
15745 {
15746 prec = precision - n;
15747 if (frame_title_ptr)
15748 n += store_frame_title (SDATA (elt), -1, prec);
15749 else if (!NILP (mode_line_string_list))
15750 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15751 else
15752 n += display_string (NULL, elt, Qnil, 0, 0, it,
15753 0, prec, 0, STRING_MULTIBYTE (elt));
15754
15755 break;
15756 }
15757
15758 while ((precision <= 0 || n < precision)
15759 && *this
15760 && (frame_title_ptr
15761 || !NILP (mode_line_string_list)
15762 || it->current_x < it->last_visible_x))
15763 {
15764 const unsigned char *last = this;
15765
15766 /* Advance to end of string or next format specifier. */
15767 while ((c = *this++) != '\0' && c != '%')
15768 ;
15769
15770 if (this - 1 != last)
15771 {
15772 int nchars, nbytes;
15773
15774 /* Output to end of string or up to '%'. Field width
15775 is length of string. Don't output more than
15776 PRECISION allows us. */
15777 --this;
15778
15779 prec = c_string_width (last, this - last, precision - n,
15780 &nchars, &nbytes);
15781
15782 if (frame_title_ptr)
15783 n += store_frame_title (last, 0, prec);
15784 else if (!NILP (mode_line_string_list))
15785 {
15786 int bytepos = last - lisp_string;
15787 int charpos = string_byte_to_char (elt, bytepos);
15788 int endpos = (precision <= 0
15789 ? string_byte_to_char (elt,
15790 this - lisp_string)
15791 : charpos + nchars);
15792
15793 n += store_mode_line_string (NULL,
15794 Fsubstring (elt, make_number (charpos),
15795 make_number (endpos)),
15796 0, 0, 0, Qnil);
15797 }
15798 else
15799 {
15800 int bytepos = last - lisp_string;
15801 int charpos = string_byte_to_char (elt, bytepos);
15802 n += display_string (NULL, elt, Qnil, 0, charpos,
15803 it, 0, prec, 0,
15804 STRING_MULTIBYTE (elt));
15805 }
15806 }
15807 else /* c == '%' */
15808 {
15809 const unsigned char *percent_position = this;
15810
15811 /* Get the specified minimum width. Zero means
15812 don't pad. */
15813 field = 0;
15814 while ((c = *this++) >= '0' && c <= '9')
15815 field = field * 10 + c - '0';
15816
15817 /* Don't pad beyond the total padding allowed. */
15818 if (field_width - n > 0 && field > field_width - n)
15819 field = field_width - n;
15820
15821 /* Note that either PRECISION <= 0 or N < PRECISION. */
15822 prec = precision - n;
15823
15824 if (c == 'M')
15825 n += display_mode_element (it, depth, field, prec,
15826 Vglobal_mode_string, props,
15827 risky);
15828 else if (c != 0)
15829 {
15830 int multibyte;
15831 int bytepos, charpos;
15832 unsigned char *spec;
15833
15834 bytepos = percent_position - lisp_string;
15835 charpos = (STRING_MULTIBYTE (elt)
15836 ? string_byte_to_char (elt, bytepos)
15837 : bytepos);
15838
15839 spec
15840 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15841
15842 if (frame_title_ptr)
15843 n += store_frame_title (spec, field, prec);
15844 else if (!NILP (mode_line_string_list))
15845 {
15846 int len = strlen (spec);
15847 Lisp_Object tem = make_string (spec, len);
15848 props = Ftext_properties_at (make_number (charpos), elt);
15849 /* Should only keep face property in props */
15850 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15851 }
15852 else
15853 {
15854 int nglyphs_before, nwritten;
15855
15856 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15857 nwritten = display_string (spec, Qnil, elt,
15858 charpos, 0, it,
15859 field, prec, 0,
15860 multibyte);
15861
15862 /* Assign to the glyphs written above the
15863 string where the `%x' came from, position
15864 of the `%'. */
15865 if (nwritten > 0)
15866 {
15867 struct glyph *glyph
15868 = (it->glyph_row->glyphs[TEXT_AREA]
15869 + nglyphs_before);
15870 int i;
15871
15872 for (i = 0; i < nwritten; ++i)
15873 {
15874 glyph[i].object = elt;
15875 glyph[i].charpos = charpos;
15876 }
15877
15878 n += nwritten;
15879 }
15880 }
15881 }
15882 else /* c == 0 */
15883 break;
15884 }
15885 }
15886 }
15887 break;
15888
15889 case Lisp_Symbol:
15890 /* A symbol: process the value of the symbol recursively
15891 as if it appeared here directly. Avoid error if symbol void.
15892 Special case: if value of symbol is a string, output the string
15893 literally. */
15894 {
15895 register Lisp_Object tem;
15896
15897 /* If the variable is not marked as risky to set
15898 then its contents are risky to use. */
15899 if (NILP (Fget (elt, Qrisky_local_variable)))
15900 risky = 1;
15901
15902 tem = Fboundp (elt);
15903 if (!NILP (tem))
15904 {
15905 tem = Fsymbol_value (elt);
15906 /* If value is a string, output that string literally:
15907 don't check for % within it. */
15908 if (STRINGP (tem))
15909 literal = 1;
15910
15911 if (!EQ (tem, elt))
15912 {
15913 /* Give up right away for nil or t. */
15914 elt = tem;
15915 goto tail_recurse;
15916 }
15917 }
15918 }
15919 break;
15920
15921 case Lisp_Cons:
15922 {
15923 register Lisp_Object car, tem;
15924
15925 /* A cons cell: five distinct cases.
15926 If first element is :eval or :propertize, do something special.
15927 If first element is a string or a cons, process all the elements
15928 and effectively concatenate them.
15929 If first element is a negative number, truncate displaying cdr to
15930 at most that many characters. If positive, pad (with spaces)
15931 to at least that many characters.
15932 If first element is a symbol, process the cadr or caddr recursively
15933 according to whether the symbol's value is non-nil or nil. */
15934 car = XCAR (elt);
15935 if (EQ (car, QCeval))
15936 {
15937 /* An element of the form (:eval FORM) means evaluate FORM
15938 and use the result as mode line elements. */
15939
15940 if (risky)
15941 break;
15942
15943 if (CONSP (XCDR (elt)))
15944 {
15945 Lisp_Object spec;
15946 spec = safe_eval (XCAR (XCDR (elt)));
15947 n += display_mode_element (it, depth, field_width - n,
15948 precision - n, spec, props,
15949 risky);
15950 }
15951 }
15952 else if (EQ (car, QCpropertize))
15953 {
15954 /* An element of the form (:propertize ELT PROPS...)
15955 means display ELT but applying properties PROPS. */
15956
15957 if (risky)
15958 break;
15959
15960 if (CONSP (XCDR (elt)))
15961 n += display_mode_element (it, depth, field_width - n,
15962 precision - n, XCAR (XCDR (elt)),
15963 XCDR (XCDR (elt)), risky);
15964 }
15965 else if (SYMBOLP (car))
15966 {
15967 tem = Fboundp (car);
15968 elt = XCDR (elt);
15969 if (!CONSP (elt))
15970 goto invalid;
15971 /* elt is now the cdr, and we know it is a cons cell.
15972 Use its car if CAR has a non-nil value. */
15973 if (!NILP (tem))
15974 {
15975 tem = Fsymbol_value (car);
15976 if (!NILP (tem))
15977 {
15978 elt = XCAR (elt);
15979 goto tail_recurse;
15980 }
15981 }
15982 /* Symbol's value is nil (or symbol is unbound)
15983 Get the cddr of the original list
15984 and if possible find the caddr and use that. */
15985 elt = XCDR (elt);
15986 if (NILP (elt))
15987 break;
15988 else if (!CONSP (elt))
15989 goto invalid;
15990 elt = XCAR (elt);
15991 goto tail_recurse;
15992 }
15993 else if (INTEGERP (car))
15994 {
15995 register int lim = XINT (car);
15996 elt = XCDR (elt);
15997 if (lim < 0)
15998 {
15999 /* Negative int means reduce maximum width. */
16000 if (precision <= 0)
16001 precision = -lim;
16002 else
16003 precision = min (precision, -lim);
16004 }
16005 else if (lim > 0)
16006 {
16007 /* Padding specified. Don't let it be more than
16008 current maximum. */
16009 if (precision > 0)
16010 lim = min (precision, lim);
16011
16012 /* If that's more padding than already wanted, queue it.
16013 But don't reduce padding already specified even if
16014 that is beyond the current truncation point. */
16015 field_width = max (lim, field_width);
16016 }
16017 goto tail_recurse;
16018 }
16019 else if (STRINGP (car) || CONSP (car))
16020 {
16021 register int limit = 50;
16022 /* Limit is to protect against circular lists. */
16023 while (CONSP (elt)
16024 && --limit > 0
16025 && (precision <= 0 || n < precision))
16026 {
16027 n += display_mode_element (it, depth, field_width - n,
16028 precision - n, XCAR (elt),
16029 props, risky);
16030 elt = XCDR (elt);
16031 }
16032 }
16033 }
16034 break;
16035
16036 default:
16037 invalid:
16038 elt = build_string ("*invalid*");
16039 goto tail_recurse;
16040 }
16041
16042 /* Pad to FIELD_WIDTH. */
16043 if (field_width > 0 && n < field_width)
16044 {
16045 if (frame_title_ptr)
16046 n += store_frame_title ("", field_width - n, 0);
16047 else if (!NILP (mode_line_string_list))
16048 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16049 else
16050 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16051 0, 0, 0);
16052 }
16053
16054 return n;
16055 }
16056
16057 /* Store a mode-line string element in mode_line_string_list.
16058
16059 If STRING is non-null, display that C string. Otherwise, the Lisp
16060 string LISP_STRING is displayed.
16061
16062 FIELD_WIDTH is the minimum number of output glyphs to produce.
16063 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16064 with spaces. FIELD_WIDTH <= 0 means don't pad.
16065
16066 PRECISION is the maximum number of characters to output from
16067 STRING. PRECISION <= 0 means don't truncate the string.
16068
16069 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16070 properties to the string.
16071
16072 PROPS are the properties to add to the string.
16073 The mode_line_string_face face property is always added to the string.
16074 */
16075
16076 static int
16077 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16078 char *string;
16079 Lisp_Object lisp_string;
16080 int copy_string;
16081 int field_width;
16082 int precision;
16083 Lisp_Object props;
16084 {
16085 int len;
16086 int n = 0;
16087
16088 if (string != NULL)
16089 {
16090 len = strlen (string);
16091 if (precision > 0 && len > precision)
16092 len = precision;
16093 lisp_string = make_string (string, len);
16094 if (NILP (props))
16095 props = mode_line_string_face_prop;
16096 else if (!NILP (mode_line_string_face))
16097 {
16098 Lisp_Object face = Fplist_get (props, Qface);
16099 props = Fcopy_sequence (props);
16100 if (NILP (face))
16101 face = mode_line_string_face;
16102 else
16103 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16104 props = Fplist_put (props, Qface, face);
16105 }
16106 Fadd_text_properties (make_number (0), make_number (len),
16107 props, lisp_string);
16108 }
16109 else
16110 {
16111 len = XFASTINT (Flength (lisp_string));
16112 if (precision > 0 && len > precision)
16113 {
16114 len = precision;
16115 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16116 precision = -1;
16117 }
16118 if (!NILP (mode_line_string_face))
16119 {
16120 Lisp_Object face;
16121 if (NILP (props))
16122 props = Ftext_properties_at (make_number (0), lisp_string);
16123 face = Fplist_get (props, Qface);
16124 if (NILP (face))
16125 face = mode_line_string_face;
16126 else
16127 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16128 props = Fcons (Qface, Fcons (face, Qnil));
16129 if (copy_string)
16130 lisp_string = Fcopy_sequence (lisp_string);
16131 }
16132 if (!NILP (props))
16133 Fadd_text_properties (make_number (0), make_number (len),
16134 props, lisp_string);
16135 }
16136
16137 if (len > 0)
16138 {
16139 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16140 n += len;
16141 }
16142
16143 if (field_width > len)
16144 {
16145 field_width -= len;
16146 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16147 if (!NILP (props))
16148 Fadd_text_properties (make_number (0), make_number (field_width),
16149 props, lisp_string);
16150 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16151 n += field_width;
16152 }
16153
16154 return n;
16155 }
16156
16157
16158 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16159 1, 4, 0,
16160 doc: /* Format a string out of a mode line format specification.
16161 First arg FORMAT specifies the mode line format (see `mode-line-format'
16162 for details) to use.
16163
16164 Optional second arg FACE specifies the face property to put
16165 on all characters for which no face is specified.
16166 t means whatever face the window's mode line currently uses
16167 \(either `mode-line' or `mode-line-inactive', depending).
16168 nil means the default is no face property.
16169 If FACE is an integer, the value string has no text properties.
16170
16171 Optional third and fourth args WINDOW and BUFFER specify the window
16172 and buffer to use as the context for the formatting (defaults
16173 are the selected window and the window's buffer). */)
16174 (format, face, window, buffer)
16175 Lisp_Object format, face, window, buffer;
16176 {
16177 struct it it;
16178 int len;
16179 struct window *w;
16180 struct buffer *old_buffer = NULL;
16181 int face_id = -1;
16182 int no_props = INTEGERP (face);
16183
16184 if (NILP (window))
16185 window = selected_window;
16186 CHECK_WINDOW (window);
16187 w = XWINDOW (window);
16188
16189 if (NILP (buffer))
16190 buffer = w->buffer;
16191 CHECK_BUFFER (buffer);
16192
16193 if (NILP (format))
16194 return build_string ("");
16195
16196 if (no_props)
16197 face = Qnil;
16198
16199 if (!NILP (face))
16200 {
16201 if (EQ (face, Qt))
16202 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16203 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16204 }
16205
16206 if (face_id < 0)
16207 face_id = DEFAULT_FACE_ID;
16208
16209 if (XBUFFER (buffer) != current_buffer)
16210 {
16211 old_buffer = current_buffer;
16212 set_buffer_internal_1 (XBUFFER (buffer));
16213 }
16214
16215 init_iterator (&it, w, -1, -1, NULL, face_id);
16216
16217 if (!no_props)
16218 {
16219 mode_line_string_face = face;
16220 mode_line_string_face_prop
16221 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16222
16223 /* We need a dummy last element in mode_line_string_list to
16224 indicate we are building the propertized mode-line string.
16225 Using mode_line_string_face_prop here GC protects it. */
16226 mode_line_string_list
16227 = Fcons (mode_line_string_face_prop, Qnil);
16228 frame_title_ptr = NULL;
16229 }
16230 else
16231 {
16232 mode_line_string_face_prop = Qnil;
16233 mode_line_string_list = Qnil;
16234 frame_title_ptr = frame_title_buf;
16235 }
16236
16237 push_frame_kboard (it.f);
16238 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16239 pop_frame_kboard ();
16240
16241 if (old_buffer)
16242 set_buffer_internal_1 (old_buffer);
16243
16244 if (!no_props)
16245 {
16246 Lisp_Object str;
16247 mode_line_string_list = Fnreverse (mode_line_string_list);
16248 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16249 make_string ("", 0));
16250 mode_line_string_face_prop = Qnil;
16251 mode_line_string_list = Qnil;
16252 return str;
16253 }
16254
16255 len = frame_title_ptr - frame_title_buf;
16256 if (len > 0 && frame_title_ptr[-1] == '-')
16257 {
16258 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16259 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16260 ;
16261 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16262 if (len > frame_title_ptr - frame_title_buf)
16263 len = frame_title_ptr - frame_title_buf;
16264 }
16265
16266 frame_title_ptr = NULL;
16267 return make_string (frame_title_buf, len);
16268 }
16269
16270 /* Write a null-terminated, right justified decimal representation of
16271 the positive integer D to BUF using a minimal field width WIDTH. */
16272
16273 static void
16274 pint2str (buf, width, d)
16275 register char *buf;
16276 register int width;
16277 register int d;
16278 {
16279 register char *p = buf;
16280
16281 if (d <= 0)
16282 *p++ = '0';
16283 else
16284 {
16285 while (d > 0)
16286 {
16287 *p++ = d % 10 + '0';
16288 d /= 10;
16289 }
16290 }
16291
16292 for (width -= (int) (p - buf); width > 0; --width)
16293 *p++ = ' ';
16294 *p-- = '\0';
16295 while (p > buf)
16296 {
16297 d = *buf;
16298 *buf++ = *p;
16299 *p-- = d;
16300 }
16301 }
16302
16303 /* Write a null-terminated, right justified decimal and "human
16304 readable" representation of the nonnegative integer D to BUF using
16305 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16306
16307 static const char power_letter[] =
16308 {
16309 0, /* not used */
16310 'k', /* kilo */
16311 'M', /* mega */
16312 'G', /* giga */
16313 'T', /* tera */
16314 'P', /* peta */
16315 'E', /* exa */
16316 'Z', /* zetta */
16317 'Y' /* yotta */
16318 };
16319
16320 static void
16321 pint2hrstr (buf, width, d)
16322 char *buf;
16323 int width;
16324 int d;
16325 {
16326 /* We aim to represent the nonnegative integer D as
16327 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16328 int quotient = d;
16329 int remainder = 0;
16330 /* -1 means: do not use TENTHS. */
16331 int tenths = -1;
16332 int exponent = 0;
16333
16334 /* Length of QUOTIENT.TENTHS as a string. */
16335 int length;
16336
16337 char * psuffix;
16338 char * p;
16339
16340 if (1000 <= quotient)
16341 {
16342 /* Scale to the appropriate EXPONENT. */
16343 do
16344 {
16345 remainder = quotient % 1000;
16346 quotient /= 1000;
16347 exponent++;
16348 }
16349 while (1000 <= quotient);
16350
16351 /* Round to nearest and decide whether to use TENTHS or not. */
16352 if (quotient <= 9)
16353 {
16354 tenths = remainder / 100;
16355 if (50 <= remainder % 100)
16356 {
16357 if (tenths < 9)
16358 tenths++;
16359 else
16360 {
16361 quotient++;
16362 if (quotient == 10)
16363 tenths = -1;
16364 else
16365 tenths = 0;
16366 }
16367 }
16368 }
16369 else
16370 if (500 <= remainder)
16371 {
16372 if (quotient < 999)
16373 quotient++;
16374 else
16375 {
16376 quotient = 1;
16377 exponent++;
16378 tenths = 0;
16379 }
16380 }
16381 }
16382
16383 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16384 if (tenths == -1 && quotient <= 99)
16385 if (quotient <= 9)
16386 length = 1;
16387 else
16388 length = 2;
16389 else
16390 length = 3;
16391 p = psuffix = buf + max (width, length);
16392
16393 /* Print EXPONENT. */
16394 if (exponent)
16395 *psuffix++ = power_letter[exponent];
16396 *psuffix = '\0';
16397
16398 /* Print TENTHS. */
16399 if (tenths >= 0)
16400 {
16401 *--p = '0' + tenths;
16402 *--p = '.';
16403 }
16404
16405 /* Print QUOTIENT. */
16406 do
16407 {
16408 int digit = quotient % 10;
16409 *--p = '0' + digit;
16410 }
16411 while ((quotient /= 10) != 0);
16412
16413 /* Print leading spaces. */
16414 while (buf < p)
16415 *--p = ' ';
16416 }
16417
16418 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16419 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16420 type of CODING_SYSTEM. Return updated pointer into BUF. */
16421
16422 static unsigned char invalid_eol_type[] = "(*invalid*)";
16423
16424 static char *
16425 decode_mode_spec_coding (coding_system, buf, eol_flag)
16426 Lisp_Object coding_system;
16427 register char *buf;
16428 int eol_flag;
16429 {
16430 Lisp_Object val;
16431 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16432 const unsigned char *eol_str;
16433 int eol_str_len;
16434 /* The EOL conversion we are using. */
16435 Lisp_Object eoltype;
16436
16437 val = Fget (coding_system, Qcoding_system);
16438 eoltype = Qnil;
16439
16440 if (!VECTORP (val)) /* Not yet decided. */
16441 {
16442 if (multibyte)
16443 *buf++ = '-';
16444 if (eol_flag)
16445 eoltype = eol_mnemonic_undecided;
16446 /* Don't mention EOL conversion if it isn't decided. */
16447 }
16448 else
16449 {
16450 Lisp_Object eolvalue;
16451
16452 eolvalue = Fget (coding_system, Qeol_type);
16453
16454 if (multibyte)
16455 *buf++ = XFASTINT (AREF (val, 1));
16456
16457 if (eol_flag)
16458 {
16459 /* The EOL conversion that is normal on this system. */
16460
16461 if (NILP (eolvalue)) /* Not yet decided. */
16462 eoltype = eol_mnemonic_undecided;
16463 else if (VECTORP (eolvalue)) /* Not yet decided. */
16464 eoltype = eol_mnemonic_undecided;
16465 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16466 eoltype = (XFASTINT (eolvalue) == 0
16467 ? eol_mnemonic_unix
16468 : (XFASTINT (eolvalue) == 1
16469 ? eol_mnemonic_dos : eol_mnemonic_mac));
16470 }
16471 }
16472
16473 if (eol_flag)
16474 {
16475 /* Mention the EOL conversion if it is not the usual one. */
16476 if (STRINGP (eoltype))
16477 {
16478 eol_str = SDATA (eoltype);
16479 eol_str_len = SBYTES (eoltype);
16480 }
16481 else if (INTEGERP (eoltype)
16482 && CHAR_VALID_P (XINT (eoltype), 0))
16483 {
16484 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16485 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16486 eol_str = tmp;
16487 }
16488 else
16489 {
16490 eol_str = invalid_eol_type;
16491 eol_str_len = sizeof (invalid_eol_type) - 1;
16492 }
16493 bcopy (eol_str, buf, eol_str_len);
16494 buf += eol_str_len;
16495 }
16496
16497 return buf;
16498 }
16499
16500 /* Return a string for the output of a mode line %-spec for window W,
16501 generated by character C. PRECISION >= 0 means don't return a
16502 string longer than that value. FIELD_WIDTH > 0 means pad the
16503 string returned with spaces to that value. Return 1 in *MULTIBYTE
16504 if the result is multibyte text.
16505
16506 Note we operate on the current buffer for most purposes,
16507 the exception being w->base_line_pos. */
16508
16509 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16510
16511 static char *
16512 decode_mode_spec (w, c, field_width, precision, multibyte)
16513 struct window *w;
16514 register int c;
16515 int field_width, precision;
16516 int *multibyte;
16517 {
16518 Lisp_Object obj;
16519 struct frame *f = XFRAME (WINDOW_FRAME (w));
16520 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16521 struct buffer *b = current_buffer;
16522
16523 obj = Qnil;
16524 *multibyte = 0;
16525
16526 switch (c)
16527 {
16528 case '*':
16529 if (!NILP (b->read_only))
16530 return "%";
16531 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16532 return "*";
16533 return "-";
16534
16535 case '+':
16536 /* This differs from %* only for a modified read-only buffer. */
16537 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16538 return "*";
16539 if (!NILP (b->read_only))
16540 return "%";
16541 return "-";
16542
16543 case '&':
16544 /* This differs from %* in ignoring read-only-ness. */
16545 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16546 return "*";
16547 return "-";
16548
16549 case '%':
16550 return "%";
16551
16552 case '[':
16553 {
16554 int i;
16555 char *p;
16556
16557 if (command_loop_level > 5)
16558 return "[[[... ";
16559 p = decode_mode_spec_buf;
16560 for (i = 0; i < command_loop_level; i++)
16561 *p++ = '[';
16562 *p = 0;
16563 return decode_mode_spec_buf;
16564 }
16565
16566 case ']':
16567 {
16568 int i;
16569 char *p;
16570
16571 if (command_loop_level > 5)
16572 return " ...]]]";
16573 p = decode_mode_spec_buf;
16574 for (i = 0; i < command_loop_level; i++)
16575 *p++ = ']';
16576 *p = 0;
16577 return decode_mode_spec_buf;
16578 }
16579
16580 case '-':
16581 {
16582 register int i;
16583
16584 /* Let lots_of_dashes be a string of infinite length. */
16585 if (!NILP (mode_line_string_list))
16586 return "--";
16587 if (field_width <= 0
16588 || field_width > sizeof (lots_of_dashes))
16589 {
16590 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16591 decode_mode_spec_buf[i] = '-';
16592 decode_mode_spec_buf[i] = '\0';
16593 return decode_mode_spec_buf;
16594 }
16595 else
16596 return lots_of_dashes;
16597 }
16598
16599 case 'b':
16600 obj = b->name;
16601 break;
16602
16603 case 'c':
16604 {
16605 int col = (int) current_column (); /* iftc */
16606 w->column_number_displayed = make_number (col);
16607 pint2str (decode_mode_spec_buf, field_width, col);
16608 return decode_mode_spec_buf;
16609 }
16610
16611 case 'F':
16612 /* %F displays the frame name. */
16613 if (!NILP (f->title))
16614 return (char *) SDATA (f->title);
16615 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16616 return (char *) SDATA (f->name);
16617 return "Emacs";
16618
16619 case 'f':
16620 obj = b->filename;
16621 break;
16622
16623 case 'i':
16624 {
16625 int size = ZV - BEGV;
16626 pint2str (decode_mode_spec_buf, field_width, size);
16627 return decode_mode_spec_buf;
16628 }
16629
16630 case 'I':
16631 {
16632 int size = ZV - BEGV;
16633 pint2hrstr (decode_mode_spec_buf, field_width, size);
16634 return decode_mode_spec_buf;
16635 }
16636
16637 case 'l':
16638 {
16639 int startpos = XMARKER (w->start)->charpos;
16640 int startpos_byte = marker_byte_position (w->start);
16641 int line, linepos, linepos_byte, topline;
16642 int nlines, junk;
16643 int height = WINDOW_TOTAL_LINES (w);
16644
16645 /* If we decided that this buffer isn't suitable for line numbers,
16646 don't forget that too fast. */
16647 if (EQ (w->base_line_pos, w->buffer))
16648 goto no_value;
16649 /* But do forget it, if the window shows a different buffer now. */
16650 else if (BUFFERP (w->base_line_pos))
16651 w->base_line_pos = Qnil;
16652
16653 /* If the buffer is very big, don't waste time. */
16654 if (INTEGERP (Vline_number_display_limit)
16655 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16656 {
16657 w->base_line_pos = Qnil;
16658 w->base_line_number = Qnil;
16659 goto no_value;
16660 }
16661
16662 if (!NILP (w->base_line_number)
16663 && !NILP (w->base_line_pos)
16664 && XFASTINT (w->base_line_pos) <= startpos)
16665 {
16666 line = XFASTINT (w->base_line_number);
16667 linepos = XFASTINT (w->base_line_pos);
16668 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16669 }
16670 else
16671 {
16672 line = 1;
16673 linepos = BUF_BEGV (b);
16674 linepos_byte = BUF_BEGV_BYTE (b);
16675 }
16676
16677 /* Count lines from base line to window start position. */
16678 nlines = display_count_lines (linepos, linepos_byte,
16679 startpos_byte,
16680 startpos, &junk);
16681
16682 topline = nlines + line;
16683
16684 /* Determine a new base line, if the old one is too close
16685 or too far away, or if we did not have one.
16686 "Too close" means it's plausible a scroll-down would
16687 go back past it. */
16688 if (startpos == BUF_BEGV (b))
16689 {
16690 w->base_line_number = make_number (topline);
16691 w->base_line_pos = make_number (BUF_BEGV (b));
16692 }
16693 else if (nlines < height + 25 || nlines > height * 3 + 50
16694 || linepos == BUF_BEGV (b))
16695 {
16696 int limit = BUF_BEGV (b);
16697 int limit_byte = BUF_BEGV_BYTE (b);
16698 int position;
16699 int distance = (height * 2 + 30) * line_number_display_limit_width;
16700
16701 if (startpos - distance > limit)
16702 {
16703 limit = startpos - distance;
16704 limit_byte = CHAR_TO_BYTE (limit);
16705 }
16706
16707 nlines = display_count_lines (startpos, startpos_byte,
16708 limit_byte,
16709 - (height * 2 + 30),
16710 &position);
16711 /* If we couldn't find the lines we wanted within
16712 line_number_display_limit_width chars per line,
16713 give up on line numbers for this window. */
16714 if (position == limit_byte && limit == startpos - distance)
16715 {
16716 w->base_line_pos = w->buffer;
16717 w->base_line_number = Qnil;
16718 goto no_value;
16719 }
16720
16721 w->base_line_number = make_number (topline - nlines);
16722 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16723 }
16724
16725 /* Now count lines from the start pos to point. */
16726 nlines = display_count_lines (startpos, startpos_byte,
16727 PT_BYTE, PT, &junk);
16728
16729 /* Record that we did display the line number. */
16730 line_number_displayed = 1;
16731
16732 /* Make the string to show. */
16733 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16734 return decode_mode_spec_buf;
16735 no_value:
16736 {
16737 char* p = decode_mode_spec_buf;
16738 int pad = field_width - 2;
16739 while (pad-- > 0)
16740 *p++ = ' ';
16741 *p++ = '?';
16742 *p++ = '?';
16743 *p = '\0';
16744 return decode_mode_spec_buf;
16745 }
16746 }
16747 break;
16748
16749 case 'm':
16750 obj = b->mode_name;
16751 break;
16752
16753 case 'n':
16754 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16755 return " Narrow";
16756 break;
16757
16758 case 'p':
16759 {
16760 int pos = marker_position (w->start);
16761 int total = BUF_ZV (b) - BUF_BEGV (b);
16762
16763 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16764 {
16765 if (pos <= BUF_BEGV (b))
16766 return "All";
16767 else
16768 return "Bottom";
16769 }
16770 else if (pos <= BUF_BEGV (b))
16771 return "Top";
16772 else
16773 {
16774 if (total > 1000000)
16775 /* Do it differently for a large value, to avoid overflow. */
16776 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16777 else
16778 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16779 /* We can't normally display a 3-digit number,
16780 so get us a 2-digit number that is close. */
16781 if (total == 100)
16782 total = 99;
16783 sprintf (decode_mode_spec_buf, "%2d%%", total);
16784 return decode_mode_spec_buf;
16785 }
16786 }
16787
16788 /* Display percentage of size above the bottom of the screen. */
16789 case 'P':
16790 {
16791 int toppos = marker_position (w->start);
16792 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16793 int total = BUF_ZV (b) - BUF_BEGV (b);
16794
16795 if (botpos >= BUF_ZV (b))
16796 {
16797 if (toppos <= BUF_BEGV (b))
16798 return "All";
16799 else
16800 return "Bottom";
16801 }
16802 else
16803 {
16804 if (total > 1000000)
16805 /* Do it differently for a large value, to avoid overflow. */
16806 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16807 else
16808 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16809 /* We can't normally display a 3-digit number,
16810 so get us a 2-digit number that is close. */
16811 if (total == 100)
16812 total = 99;
16813 if (toppos <= BUF_BEGV (b))
16814 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16815 else
16816 sprintf (decode_mode_spec_buf, "%2d%%", total);
16817 return decode_mode_spec_buf;
16818 }
16819 }
16820
16821 case 's':
16822 /* status of process */
16823 obj = Fget_buffer_process (Fcurrent_buffer ());
16824 if (NILP (obj))
16825 return "no process";
16826 #ifdef subprocesses
16827 obj = Fsymbol_name (Fprocess_status (obj));
16828 #endif
16829 break;
16830
16831 case 't': /* indicate TEXT or BINARY */
16832 #ifdef MODE_LINE_BINARY_TEXT
16833 return MODE_LINE_BINARY_TEXT (b);
16834 #else
16835 return "T";
16836 #endif
16837
16838 case 'z':
16839 /* coding-system (not including end-of-line format) */
16840 case 'Z':
16841 /* coding-system (including end-of-line type) */
16842 {
16843 int eol_flag = (c == 'Z');
16844 char *p = decode_mode_spec_buf;
16845
16846 if (! FRAME_WINDOW_P (f))
16847 {
16848 /* No need to mention EOL here--the terminal never needs
16849 to do EOL conversion. */
16850 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16851 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16852 }
16853 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16854 p, eol_flag);
16855
16856 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16857 #ifdef subprocesses
16858 obj = Fget_buffer_process (Fcurrent_buffer ());
16859 if (PROCESSP (obj))
16860 {
16861 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16862 p, eol_flag);
16863 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16864 p, eol_flag);
16865 }
16866 #endif /* subprocesses */
16867 #endif /* 0 */
16868 *p = 0;
16869 return decode_mode_spec_buf;
16870 }
16871 }
16872
16873 if (STRINGP (obj))
16874 {
16875 *multibyte = STRING_MULTIBYTE (obj);
16876 return (char *) SDATA (obj);
16877 }
16878 else
16879 return "";
16880 }
16881
16882
16883 /* Count up to COUNT lines starting from START / START_BYTE.
16884 But don't go beyond LIMIT_BYTE.
16885 Return the number of lines thus found (always nonnegative).
16886
16887 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16888
16889 static int
16890 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16891 int start, start_byte, limit_byte, count;
16892 int *byte_pos_ptr;
16893 {
16894 register unsigned char *cursor;
16895 unsigned char *base;
16896
16897 register int ceiling;
16898 register unsigned char *ceiling_addr;
16899 int orig_count = count;
16900
16901 /* If we are not in selective display mode,
16902 check only for newlines. */
16903 int selective_display = (!NILP (current_buffer->selective_display)
16904 && !INTEGERP (current_buffer->selective_display));
16905
16906 if (count > 0)
16907 {
16908 while (start_byte < limit_byte)
16909 {
16910 ceiling = BUFFER_CEILING_OF (start_byte);
16911 ceiling = min (limit_byte - 1, ceiling);
16912 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16913 base = (cursor = BYTE_POS_ADDR (start_byte));
16914 while (1)
16915 {
16916 if (selective_display)
16917 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16918 ;
16919 else
16920 while (*cursor != '\n' && ++cursor != ceiling_addr)
16921 ;
16922
16923 if (cursor != ceiling_addr)
16924 {
16925 if (--count == 0)
16926 {
16927 start_byte += cursor - base + 1;
16928 *byte_pos_ptr = start_byte;
16929 return orig_count;
16930 }
16931 else
16932 if (++cursor == ceiling_addr)
16933 break;
16934 }
16935 else
16936 break;
16937 }
16938 start_byte += cursor - base;
16939 }
16940 }
16941 else
16942 {
16943 while (start_byte > limit_byte)
16944 {
16945 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16946 ceiling = max (limit_byte, ceiling);
16947 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16948 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16949 while (1)
16950 {
16951 if (selective_display)
16952 while (--cursor != ceiling_addr
16953 && *cursor != '\n' && *cursor != 015)
16954 ;
16955 else
16956 while (--cursor != ceiling_addr && *cursor != '\n')
16957 ;
16958
16959 if (cursor != ceiling_addr)
16960 {
16961 if (++count == 0)
16962 {
16963 start_byte += cursor - base + 1;
16964 *byte_pos_ptr = start_byte;
16965 /* When scanning backwards, we should
16966 not count the newline posterior to which we stop. */
16967 return - orig_count - 1;
16968 }
16969 }
16970 else
16971 break;
16972 }
16973 /* Here we add 1 to compensate for the last decrement
16974 of CURSOR, which took it past the valid range. */
16975 start_byte += cursor - base + 1;
16976 }
16977 }
16978
16979 *byte_pos_ptr = limit_byte;
16980
16981 if (count < 0)
16982 return - orig_count + count;
16983 return orig_count - count;
16984
16985 }
16986
16987
16988 \f
16989 /***********************************************************************
16990 Displaying strings
16991 ***********************************************************************/
16992
16993 /* Display a NUL-terminated string, starting with index START.
16994
16995 If STRING is non-null, display that C string. Otherwise, the Lisp
16996 string LISP_STRING is displayed.
16997
16998 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16999 FACE_STRING. Display STRING or LISP_STRING with the face at
17000 FACE_STRING_POS in FACE_STRING:
17001
17002 Display the string in the environment given by IT, but use the
17003 standard display table, temporarily.
17004
17005 FIELD_WIDTH is the minimum number of output glyphs to produce.
17006 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17007 with spaces. If STRING has more characters, more than FIELD_WIDTH
17008 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17009
17010 PRECISION is the maximum number of characters to output from
17011 STRING. PRECISION < 0 means don't truncate the string.
17012
17013 This is roughly equivalent to printf format specifiers:
17014
17015 FIELD_WIDTH PRECISION PRINTF
17016 ----------------------------------------
17017 -1 -1 %s
17018 -1 10 %.10s
17019 10 -1 %10s
17020 20 10 %20.10s
17021
17022 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17023 display them, and < 0 means obey the current buffer's value of
17024 enable_multibyte_characters.
17025
17026 Value is the number of glyphs produced. */
17027
17028 static int
17029 display_string (string, lisp_string, face_string, face_string_pos,
17030 start, it, field_width, precision, max_x, multibyte)
17031 unsigned char *string;
17032 Lisp_Object lisp_string;
17033 Lisp_Object face_string;
17034 int face_string_pos;
17035 int start;
17036 struct it *it;
17037 int field_width, precision, max_x;
17038 int multibyte;
17039 {
17040 int hpos_at_start = it->hpos;
17041 int saved_face_id = it->face_id;
17042 struct glyph_row *row = it->glyph_row;
17043
17044 /* Initialize the iterator IT for iteration over STRING beginning
17045 with index START. */
17046 reseat_to_string (it, string, lisp_string, start,
17047 precision, field_width, multibyte);
17048
17049 /* If displaying STRING, set up the face of the iterator
17050 from LISP_STRING, if that's given. */
17051 if (STRINGP (face_string))
17052 {
17053 int endptr;
17054 struct face *face;
17055
17056 it->face_id
17057 = face_at_string_position (it->w, face_string, face_string_pos,
17058 0, it->region_beg_charpos,
17059 it->region_end_charpos,
17060 &endptr, it->base_face_id, 0);
17061 face = FACE_FROM_ID (it->f, it->face_id);
17062 it->face_box_p = face->box != FACE_NO_BOX;
17063 }
17064
17065 /* Set max_x to the maximum allowed X position. Don't let it go
17066 beyond the right edge of the window. */
17067 if (max_x <= 0)
17068 max_x = it->last_visible_x;
17069 else
17070 max_x = min (max_x, it->last_visible_x);
17071
17072 /* Skip over display elements that are not visible. because IT->w is
17073 hscrolled. */
17074 if (it->current_x < it->first_visible_x)
17075 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17076 MOVE_TO_POS | MOVE_TO_X);
17077
17078 row->ascent = it->max_ascent;
17079 row->height = it->max_ascent + it->max_descent;
17080 row->phys_ascent = it->max_phys_ascent;
17081 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17082 row->extra_line_spacing = it->max_extra_line_spacing;
17083
17084 /* This condition is for the case that we are called with current_x
17085 past last_visible_x. */
17086 while (it->current_x < max_x)
17087 {
17088 int x_before, x, n_glyphs_before, i, nglyphs;
17089
17090 /* Get the next display element. */
17091 if (!get_next_display_element (it))
17092 break;
17093
17094 /* Produce glyphs. */
17095 x_before = it->current_x;
17096 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17097 PRODUCE_GLYPHS (it);
17098
17099 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17100 i = 0;
17101 x = x_before;
17102 while (i < nglyphs)
17103 {
17104 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17105
17106 if (!it->truncate_lines_p
17107 && x + glyph->pixel_width > max_x)
17108 {
17109 /* End of continued line or max_x reached. */
17110 if (CHAR_GLYPH_PADDING_P (*glyph))
17111 {
17112 /* A wide character is unbreakable. */
17113 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17114 it->current_x = x_before;
17115 }
17116 else
17117 {
17118 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17119 it->current_x = x;
17120 }
17121 break;
17122 }
17123 else if (x + glyph->pixel_width > it->first_visible_x)
17124 {
17125 /* Glyph is at least partially visible. */
17126 ++it->hpos;
17127 if (x < it->first_visible_x)
17128 it->glyph_row->x = x - it->first_visible_x;
17129 }
17130 else
17131 {
17132 /* Glyph is off the left margin of the display area.
17133 Should not happen. */
17134 abort ();
17135 }
17136
17137 row->ascent = max (row->ascent, it->max_ascent);
17138 row->height = max (row->height, it->max_ascent + it->max_descent);
17139 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17140 row->phys_height = max (row->phys_height,
17141 it->max_phys_ascent + it->max_phys_descent);
17142 row->extra_line_spacing = max (row->extra_line_spacing,
17143 it->max_extra_line_spacing);
17144 x += glyph->pixel_width;
17145 ++i;
17146 }
17147
17148 /* Stop if max_x reached. */
17149 if (i < nglyphs)
17150 break;
17151
17152 /* Stop at line ends. */
17153 if (ITERATOR_AT_END_OF_LINE_P (it))
17154 {
17155 it->continuation_lines_width = 0;
17156 break;
17157 }
17158
17159 set_iterator_to_next (it, 1);
17160
17161 /* Stop if truncating at the right edge. */
17162 if (it->truncate_lines_p
17163 && it->current_x >= it->last_visible_x)
17164 {
17165 /* Add truncation mark, but don't do it if the line is
17166 truncated at a padding space. */
17167 if (IT_CHARPOS (*it) < it->string_nchars)
17168 {
17169 if (!FRAME_WINDOW_P (it->f))
17170 {
17171 int i, n;
17172
17173 if (it->current_x > it->last_visible_x)
17174 {
17175 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17176 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17177 break;
17178 for (n = row->used[TEXT_AREA]; i < n; ++i)
17179 {
17180 row->used[TEXT_AREA] = i;
17181 produce_special_glyphs (it, IT_TRUNCATION);
17182 }
17183 }
17184 produce_special_glyphs (it, IT_TRUNCATION);
17185 }
17186 it->glyph_row->truncated_on_right_p = 1;
17187 }
17188 break;
17189 }
17190 }
17191
17192 /* Maybe insert a truncation at the left. */
17193 if (it->first_visible_x
17194 && IT_CHARPOS (*it) > 0)
17195 {
17196 if (!FRAME_WINDOW_P (it->f))
17197 insert_left_trunc_glyphs (it);
17198 it->glyph_row->truncated_on_left_p = 1;
17199 }
17200
17201 it->face_id = saved_face_id;
17202
17203 /* Value is number of columns displayed. */
17204 return it->hpos - hpos_at_start;
17205 }
17206
17207
17208 \f
17209 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17210 appears as an element of LIST or as the car of an element of LIST.
17211 If PROPVAL is a list, compare each element against LIST in that
17212 way, and return 1/2 if any element of PROPVAL is found in LIST.
17213 Otherwise return 0. This function cannot quit.
17214 The return value is 2 if the text is invisible but with an ellipsis
17215 and 1 if it's invisible and without an ellipsis. */
17216
17217 int
17218 invisible_p (propval, list)
17219 register Lisp_Object propval;
17220 Lisp_Object list;
17221 {
17222 register Lisp_Object tail, proptail;
17223
17224 for (tail = list; CONSP (tail); tail = XCDR (tail))
17225 {
17226 register Lisp_Object tem;
17227 tem = XCAR (tail);
17228 if (EQ (propval, tem))
17229 return 1;
17230 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17231 return NILP (XCDR (tem)) ? 1 : 2;
17232 }
17233
17234 if (CONSP (propval))
17235 {
17236 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17237 {
17238 Lisp_Object propelt;
17239 propelt = XCAR (proptail);
17240 for (tail = list; CONSP (tail); tail = XCDR (tail))
17241 {
17242 register Lisp_Object tem;
17243 tem = XCAR (tail);
17244 if (EQ (propelt, tem))
17245 return 1;
17246 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17247 return NILP (XCDR (tem)) ? 1 : 2;
17248 }
17249 }
17250 }
17251
17252 return 0;
17253 }
17254
17255 /* Calculate a width or height in pixels from a specification using
17256 the following elements:
17257
17258 SPEC ::=
17259 NUM - a (fractional) multiple of the default font width/height
17260 (NUM) - specifies exactly NUM pixels
17261 UNIT - a fixed number of pixels, see below.
17262 ELEMENT - size of a display element in pixels, see below.
17263 (NUM . SPEC) - equals NUM * SPEC
17264 (+ SPEC SPEC ...) - add pixel values
17265 (- SPEC SPEC ...) - subtract pixel values
17266 (- SPEC) - negate pixel value
17267
17268 NUM ::=
17269 INT or FLOAT - a number constant
17270 SYMBOL - use symbol's (buffer local) variable binding.
17271
17272 UNIT ::=
17273 in - pixels per inch *)
17274 mm - pixels per 1/1000 meter *)
17275 cm - pixels per 1/100 meter *)
17276 width - width of current font in pixels.
17277 height - height of current font in pixels.
17278
17279 *) using the ratio(s) defined in display-pixels-per-inch.
17280
17281 ELEMENT ::=
17282
17283 left-fringe - left fringe width in pixels
17284 right-fringe - right fringe width in pixels
17285
17286 left-margin - left margin width in pixels
17287 right-margin - right margin width in pixels
17288
17289 scroll-bar - scroll-bar area width in pixels
17290
17291 Examples:
17292
17293 Pixels corresponding to 5 inches:
17294 (5 . in)
17295
17296 Total width of non-text areas on left side of window (if scroll-bar is on left):
17297 '(space :width (+ left-fringe left-margin scroll-bar))
17298
17299 Align to first text column (in header line):
17300 '(space :align-to 0)
17301
17302 Align to middle of text area minus half the width of variable `my-image'
17303 containing a loaded image:
17304 '(space :align-to (0.5 . (- text my-image)))
17305
17306 Width of left margin minus width of 1 character in the default font:
17307 '(space :width (- left-margin 1))
17308
17309 Width of left margin minus width of 2 characters in the current font:
17310 '(space :width (- left-margin (2 . width)))
17311
17312 Center 1 character over left-margin (in header line):
17313 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17314
17315 Different ways to express width of left fringe plus left margin minus one pixel:
17316 '(space :width (- (+ left-fringe left-margin) (1)))
17317 '(space :width (+ left-fringe left-margin (- (1))))
17318 '(space :width (+ left-fringe left-margin (-1)))
17319
17320 */
17321
17322 #define NUMVAL(X) \
17323 ((INTEGERP (X) || FLOATP (X)) \
17324 ? XFLOATINT (X) \
17325 : - 1)
17326
17327 int
17328 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17329 double *res;
17330 struct it *it;
17331 Lisp_Object prop;
17332 void *font;
17333 int width_p, *align_to;
17334 {
17335 double pixels;
17336
17337 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17338 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17339
17340 if (NILP (prop))
17341 return OK_PIXELS (0);
17342
17343 if (SYMBOLP (prop))
17344 {
17345 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17346 {
17347 char *unit = SDATA (SYMBOL_NAME (prop));
17348
17349 if (unit[0] == 'i' && unit[1] == 'n')
17350 pixels = 1.0;
17351 else if (unit[0] == 'm' && unit[1] == 'm')
17352 pixels = 25.4;
17353 else if (unit[0] == 'c' && unit[1] == 'm')
17354 pixels = 2.54;
17355 else
17356 pixels = 0;
17357 if (pixels > 0)
17358 {
17359 double ppi;
17360 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17361 || (CONSP (Vdisplay_pixels_per_inch)
17362 && (ppi = (width_p
17363 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17364 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17365 ppi > 0)))
17366 return OK_PIXELS (ppi / pixels);
17367
17368 return 0;
17369 }
17370 }
17371
17372 #ifdef HAVE_WINDOW_SYSTEM
17373 if (EQ (prop, Qheight))
17374 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17375 if (EQ (prop, Qwidth))
17376 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17377 #else
17378 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17379 return OK_PIXELS (1);
17380 #endif
17381
17382 if (EQ (prop, Qtext))
17383 return OK_PIXELS (width_p
17384 ? window_box_width (it->w, TEXT_AREA)
17385 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17386
17387 if (align_to && *align_to < 0)
17388 {
17389 *res = 0;
17390 if (EQ (prop, Qleft))
17391 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17392 if (EQ (prop, Qright))
17393 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17394 if (EQ (prop, Qcenter))
17395 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17396 + window_box_width (it->w, TEXT_AREA) / 2);
17397 if (EQ (prop, Qleft_fringe))
17398 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17399 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17400 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17401 if (EQ (prop, Qright_fringe))
17402 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17403 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17404 : window_box_right_offset (it->w, TEXT_AREA));
17405 if (EQ (prop, Qleft_margin))
17406 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17407 if (EQ (prop, Qright_margin))
17408 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17409 if (EQ (prop, Qscroll_bar))
17410 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17411 ? 0
17412 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17413 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17414 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17415 : 0)));
17416 }
17417 else
17418 {
17419 if (EQ (prop, Qleft_fringe))
17420 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17421 if (EQ (prop, Qright_fringe))
17422 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17423 if (EQ (prop, Qleft_margin))
17424 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17425 if (EQ (prop, Qright_margin))
17426 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17427 if (EQ (prop, Qscroll_bar))
17428 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17429 }
17430
17431 prop = Fbuffer_local_value (prop, it->w->buffer);
17432 }
17433
17434 if (INTEGERP (prop) || FLOATP (prop))
17435 {
17436 int base_unit = (width_p
17437 ? FRAME_COLUMN_WIDTH (it->f)
17438 : FRAME_LINE_HEIGHT (it->f));
17439 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17440 }
17441
17442 if (CONSP (prop))
17443 {
17444 Lisp_Object car = XCAR (prop);
17445 Lisp_Object cdr = XCDR (prop);
17446
17447 if (SYMBOLP (car))
17448 {
17449 #ifdef HAVE_WINDOW_SYSTEM
17450 if (valid_image_p (prop))
17451 {
17452 int id = lookup_image (it->f, prop);
17453 struct image *img = IMAGE_FROM_ID (it->f, id);
17454
17455 return OK_PIXELS (width_p ? img->width : img->height);
17456 }
17457 #endif
17458 if (EQ (car, Qplus) || EQ (car, Qminus))
17459 {
17460 int first = 1;
17461 double px;
17462
17463 pixels = 0;
17464 while (CONSP (cdr))
17465 {
17466 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17467 font, width_p, align_to))
17468 return 0;
17469 if (first)
17470 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17471 else
17472 pixels += px;
17473 cdr = XCDR (cdr);
17474 }
17475 if (EQ (car, Qminus))
17476 pixels = -pixels;
17477 return OK_PIXELS (pixels);
17478 }
17479
17480 car = Fbuffer_local_value (car, it->w->buffer);
17481 }
17482
17483 if (INTEGERP (car) || FLOATP (car))
17484 {
17485 double fact;
17486 pixels = XFLOATINT (car);
17487 if (NILP (cdr))
17488 return OK_PIXELS (pixels);
17489 if (calc_pixel_width_or_height (&fact, it, cdr,
17490 font, width_p, align_to))
17491 return OK_PIXELS (pixels * fact);
17492 return 0;
17493 }
17494
17495 return 0;
17496 }
17497
17498 return 0;
17499 }
17500
17501 \f
17502 /***********************************************************************
17503 Glyph Display
17504 ***********************************************************************/
17505
17506 #ifdef HAVE_WINDOW_SYSTEM
17507
17508 #if GLYPH_DEBUG
17509
17510 void
17511 dump_glyph_string (s)
17512 struct glyph_string *s;
17513 {
17514 fprintf (stderr, "glyph string\n");
17515 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17516 s->x, s->y, s->width, s->height);
17517 fprintf (stderr, " ybase = %d\n", s->ybase);
17518 fprintf (stderr, " hl = %d\n", s->hl);
17519 fprintf (stderr, " left overhang = %d, right = %d\n",
17520 s->left_overhang, s->right_overhang);
17521 fprintf (stderr, " nchars = %d\n", s->nchars);
17522 fprintf (stderr, " extends to end of line = %d\n",
17523 s->extends_to_end_of_line_p);
17524 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17525 fprintf (stderr, " bg width = %d\n", s->background_width);
17526 }
17527
17528 #endif /* GLYPH_DEBUG */
17529
17530 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17531 of XChar2b structures for S; it can't be allocated in
17532 init_glyph_string because it must be allocated via `alloca'. W
17533 is the window on which S is drawn. ROW and AREA are the glyph row
17534 and area within the row from which S is constructed. START is the
17535 index of the first glyph structure covered by S. HL is a
17536 face-override for drawing S. */
17537
17538 #ifdef HAVE_NTGUI
17539 #define OPTIONAL_HDC(hdc) hdc,
17540 #define DECLARE_HDC(hdc) HDC hdc;
17541 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17542 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17543 #endif
17544
17545 #ifndef OPTIONAL_HDC
17546 #define OPTIONAL_HDC(hdc)
17547 #define DECLARE_HDC(hdc)
17548 #define ALLOCATE_HDC(hdc, f)
17549 #define RELEASE_HDC(hdc, f)
17550 #endif
17551
17552 static void
17553 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17554 struct glyph_string *s;
17555 DECLARE_HDC (hdc)
17556 XChar2b *char2b;
17557 struct window *w;
17558 struct glyph_row *row;
17559 enum glyph_row_area area;
17560 int start;
17561 enum draw_glyphs_face hl;
17562 {
17563 bzero (s, sizeof *s);
17564 s->w = w;
17565 s->f = XFRAME (w->frame);
17566 #ifdef HAVE_NTGUI
17567 s->hdc = hdc;
17568 #endif
17569 s->display = FRAME_X_DISPLAY (s->f);
17570 s->window = FRAME_X_WINDOW (s->f);
17571 s->char2b = char2b;
17572 s->hl = hl;
17573 s->row = row;
17574 s->area = area;
17575 s->first_glyph = row->glyphs[area] + start;
17576 s->height = row->height;
17577 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17578
17579 /* Display the internal border below the tool-bar window. */
17580 if (WINDOWP (s->f->tool_bar_window)
17581 && s->w == XWINDOW (s->f->tool_bar_window))
17582 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17583
17584 s->ybase = s->y + row->ascent;
17585 }
17586
17587
17588 /* Append the list of glyph strings with head H and tail T to the list
17589 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17590
17591 static INLINE void
17592 append_glyph_string_lists (head, tail, h, t)
17593 struct glyph_string **head, **tail;
17594 struct glyph_string *h, *t;
17595 {
17596 if (h)
17597 {
17598 if (*head)
17599 (*tail)->next = h;
17600 else
17601 *head = h;
17602 h->prev = *tail;
17603 *tail = t;
17604 }
17605 }
17606
17607
17608 /* Prepend the list of glyph strings with head H and tail T to the
17609 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17610 result. */
17611
17612 static INLINE void
17613 prepend_glyph_string_lists (head, tail, h, t)
17614 struct glyph_string **head, **tail;
17615 struct glyph_string *h, *t;
17616 {
17617 if (h)
17618 {
17619 if (*head)
17620 (*head)->prev = t;
17621 else
17622 *tail = t;
17623 t->next = *head;
17624 *head = h;
17625 }
17626 }
17627
17628
17629 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17630 Set *HEAD and *TAIL to the resulting list. */
17631
17632 static INLINE void
17633 append_glyph_string (head, tail, s)
17634 struct glyph_string **head, **tail;
17635 struct glyph_string *s;
17636 {
17637 s->next = s->prev = NULL;
17638 append_glyph_string_lists (head, tail, s, s);
17639 }
17640
17641
17642 /* Get face and two-byte form of character glyph GLYPH on frame F.
17643 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17644 a pointer to a realized face that is ready for display. */
17645
17646 static INLINE struct face *
17647 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17648 struct frame *f;
17649 struct glyph *glyph;
17650 XChar2b *char2b;
17651 int *two_byte_p;
17652 {
17653 struct face *face;
17654
17655 xassert (glyph->type == CHAR_GLYPH);
17656 face = FACE_FROM_ID (f, glyph->face_id);
17657
17658 if (two_byte_p)
17659 *two_byte_p = 0;
17660
17661 if (!glyph->multibyte_p)
17662 {
17663 /* Unibyte case. We don't have to encode, but we have to make
17664 sure to use a face suitable for unibyte. */
17665 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17666 }
17667 else if (glyph->u.ch < 128
17668 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17669 {
17670 /* Case of ASCII in a face known to fit ASCII. */
17671 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17672 }
17673 else
17674 {
17675 int c1, c2, charset;
17676
17677 /* Split characters into bytes. If c2 is -1 afterwards, C is
17678 really a one-byte character so that byte1 is zero. */
17679 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17680 if (c2 > 0)
17681 STORE_XCHAR2B (char2b, c1, c2);
17682 else
17683 STORE_XCHAR2B (char2b, 0, c1);
17684
17685 /* Maybe encode the character in *CHAR2B. */
17686 if (charset != CHARSET_ASCII)
17687 {
17688 struct font_info *font_info
17689 = FONT_INFO_FROM_ID (f, face->font_info_id);
17690 if (font_info)
17691 glyph->font_type
17692 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17693 }
17694 }
17695
17696 /* Make sure X resources of the face are allocated. */
17697 xassert (face != NULL);
17698 PREPARE_FACE_FOR_DISPLAY (f, face);
17699 return face;
17700 }
17701
17702
17703 /* Fill glyph string S with composition components specified by S->cmp.
17704
17705 FACES is an array of faces for all components of this composition.
17706 S->gidx is the index of the first component for S.
17707 OVERLAPS_P non-zero means S should draw the foreground only, and
17708 use its physical height for clipping.
17709
17710 Value is the index of a component not in S. */
17711
17712 static int
17713 fill_composite_glyph_string (s, faces, overlaps_p)
17714 struct glyph_string *s;
17715 struct face **faces;
17716 int overlaps_p;
17717 {
17718 int i;
17719
17720 xassert (s);
17721
17722 s->for_overlaps_p = overlaps_p;
17723
17724 s->face = faces[s->gidx];
17725 s->font = s->face->font;
17726 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17727
17728 /* For all glyphs of this composition, starting at the offset
17729 S->gidx, until we reach the end of the definition or encounter a
17730 glyph that requires the different face, add it to S. */
17731 ++s->nchars;
17732 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17733 ++s->nchars;
17734
17735 /* All glyph strings for the same composition has the same width,
17736 i.e. the width set for the first component of the composition. */
17737
17738 s->width = s->first_glyph->pixel_width;
17739
17740 /* If the specified font could not be loaded, use the frame's
17741 default font, but record the fact that we couldn't load it in
17742 the glyph string so that we can draw rectangles for the
17743 characters of the glyph string. */
17744 if (s->font == NULL)
17745 {
17746 s->font_not_found_p = 1;
17747 s->font = FRAME_FONT (s->f);
17748 }
17749
17750 /* Adjust base line for subscript/superscript text. */
17751 s->ybase += s->first_glyph->voffset;
17752
17753 xassert (s->face && s->face->gc);
17754
17755 /* This glyph string must always be drawn with 16-bit functions. */
17756 s->two_byte_p = 1;
17757
17758 return s->gidx + s->nchars;
17759 }
17760
17761
17762 /* Fill glyph string S from a sequence of character glyphs.
17763
17764 FACE_ID is the face id of the string. START is the index of the
17765 first glyph to consider, END is the index of the last + 1.
17766 OVERLAPS_P non-zero means S should draw the foreground only, and
17767 use its physical height for clipping.
17768
17769 Value is the index of the first glyph not in S. */
17770
17771 static int
17772 fill_glyph_string (s, face_id, start, end, overlaps_p)
17773 struct glyph_string *s;
17774 int face_id;
17775 int start, end, overlaps_p;
17776 {
17777 struct glyph *glyph, *last;
17778 int voffset;
17779 int glyph_not_available_p;
17780
17781 xassert (s->f == XFRAME (s->w->frame));
17782 xassert (s->nchars == 0);
17783 xassert (start >= 0 && end > start);
17784
17785 s->for_overlaps_p = overlaps_p,
17786 glyph = s->row->glyphs[s->area] + start;
17787 last = s->row->glyphs[s->area] + end;
17788 voffset = glyph->voffset;
17789
17790 glyph_not_available_p = glyph->glyph_not_available_p;
17791
17792 while (glyph < last
17793 && glyph->type == CHAR_GLYPH
17794 && glyph->voffset == voffset
17795 /* Same face id implies same font, nowadays. */
17796 && glyph->face_id == face_id
17797 && glyph->glyph_not_available_p == glyph_not_available_p)
17798 {
17799 int two_byte_p;
17800
17801 s->face = get_glyph_face_and_encoding (s->f, glyph,
17802 s->char2b + s->nchars,
17803 &two_byte_p);
17804 s->two_byte_p = two_byte_p;
17805 ++s->nchars;
17806 xassert (s->nchars <= end - start);
17807 s->width += glyph->pixel_width;
17808 ++glyph;
17809 }
17810
17811 s->font = s->face->font;
17812 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17813
17814 /* If the specified font could not be loaded, use the frame's font,
17815 but record the fact that we couldn't load it in
17816 S->font_not_found_p so that we can draw rectangles for the
17817 characters of the glyph string. */
17818 if (s->font == NULL || glyph_not_available_p)
17819 {
17820 s->font_not_found_p = 1;
17821 s->font = FRAME_FONT (s->f);
17822 }
17823
17824 /* Adjust base line for subscript/superscript text. */
17825 s->ybase += voffset;
17826
17827 xassert (s->face && s->face->gc);
17828 return glyph - s->row->glyphs[s->area];
17829 }
17830
17831
17832 /* Fill glyph string S from image glyph S->first_glyph. */
17833
17834 static void
17835 fill_image_glyph_string (s)
17836 struct glyph_string *s;
17837 {
17838 xassert (s->first_glyph->type == IMAGE_GLYPH);
17839 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17840 xassert (s->img);
17841 s->slice = s->first_glyph->slice;
17842 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17843 s->font = s->face->font;
17844 s->width = s->first_glyph->pixel_width;
17845
17846 /* Adjust base line for subscript/superscript text. */
17847 s->ybase += s->first_glyph->voffset;
17848 }
17849
17850
17851 /* Fill glyph string S from a sequence of stretch glyphs.
17852
17853 ROW is the glyph row in which the glyphs are found, AREA is the
17854 area within the row. START is the index of the first glyph to
17855 consider, END is the index of the last + 1.
17856
17857 Value is the index of the first glyph not in S. */
17858
17859 static int
17860 fill_stretch_glyph_string (s, row, area, start, end)
17861 struct glyph_string *s;
17862 struct glyph_row *row;
17863 enum glyph_row_area area;
17864 int start, end;
17865 {
17866 struct glyph *glyph, *last;
17867 int voffset, face_id;
17868
17869 xassert (s->first_glyph->type == STRETCH_GLYPH);
17870
17871 glyph = s->row->glyphs[s->area] + start;
17872 last = s->row->glyphs[s->area] + end;
17873 face_id = glyph->face_id;
17874 s->face = FACE_FROM_ID (s->f, face_id);
17875 s->font = s->face->font;
17876 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17877 s->width = glyph->pixel_width;
17878 voffset = glyph->voffset;
17879
17880 for (++glyph;
17881 (glyph < last
17882 && glyph->type == STRETCH_GLYPH
17883 && glyph->voffset == voffset
17884 && glyph->face_id == face_id);
17885 ++glyph)
17886 s->width += glyph->pixel_width;
17887
17888 /* Adjust base line for subscript/superscript text. */
17889 s->ybase += voffset;
17890
17891 /* The case that face->gc == 0 is handled when drawing the glyph
17892 string by calling PREPARE_FACE_FOR_DISPLAY. */
17893 xassert (s->face);
17894 return glyph - s->row->glyphs[s->area];
17895 }
17896
17897
17898 /* EXPORT for RIF:
17899 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17900 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17901 assumed to be zero. */
17902
17903 void
17904 x_get_glyph_overhangs (glyph, f, left, right)
17905 struct glyph *glyph;
17906 struct frame *f;
17907 int *left, *right;
17908 {
17909 *left = *right = 0;
17910
17911 if (glyph->type == CHAR_GLYPH)
17912 {
17913 XFontStruct *font;
17914 struct face *face;
17915 struct font_info *font_info;
17916 XChar2b char2b;
17917 XCharStruct *pcm;
17918
17919 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17920 font = face->font;
17921 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17922 if (font /* ++KFS: Should this be font_info ? */
17923 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17924 {
17925 if (pcm->rbearing > pcm->width)
17926 *right = pcm->rbearing - pcm->width;
17927 if (pcm->lbearing < 0)
17928 *left = -pcm->lbearing;
17929 }
17930 }
17931 }
17932
17933
17934 /* Return the index of the first glyph preceding glyph string S that
17935 is overwritten by S because of S's left overhang. Value is -1
17936 if no glyphs are overwritten. */
17937
17938 static int
17939 left_overwritten (s)
17940 struct glyph_string *s;
17941 {
17942 int k;
17943
17944 if (s->left_overhang)
17945 {
17946 int x = 0, i;
17947 struct glyph *glyphs = s->row->glyphs[s->area];
17948 int first = s->first_glyph - glyphs;
17949
17950 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17951 x -= glyphs[i].pixel_width;
17952
17953 k = i + 1;
17954 }
17955 else
17956 k = -1;
17957
17958 return k;
17959 }
17960
17961
17962 /* Return the index of the first glyph preceding glyph string S that
17963 is overwriting S because of its right overhang. Value is -1 if no
17964 glyph in front of S overwrites S. */
17965
17966 static int
17967 left_overwriting (s)
17968 struct glyph_string *s;
17969 {
17970 int i, k, x;
17971 struct glyph *glyphs = s->row->glyphs[s->area];
17972 int first = s->first_glyph - glyphs;
17973
17974 k = -1;
17975 x = 0;
17976 for (i = first - 1; i >= 0; --i)
17977 {
17978 int left, right;
17979 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17980 if (x + right > 0)
17981 k = i;
17982 x -= glyphs[i].pixel_width;
17983 }
17984
17985 return k;
17986 }
17987
17988
17989 /* Return the index of the last glyph following glyph string S that is
17990 not overwritten by S because of S's right overhang. Value is -1 if
17991 no such glyph is found. */
17992
17993 static int
17994 right_overwritten (s)
17995 struct glyph_string *s;
17996 {
17997 int k = -1;
17998
17999 if (s->right_overhang)
18000 {
18001 int x = 0, i;
18002 struct glyph *glyphs = s->row->glyphs[s->area];
18003 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18004 int end = s->row->used[s->area];
18005
18006 for (i = first; i < end && s->right_overhang > x; ++i)
18007 x += glyphs[i].pixel_width;
18008
18009 k = i;
18010 }
18011
18012 return k;
18013 }
18014
18015
18016 /* Return the index of the last glyph following glyph string S that
18017 overwrites S because of its left overhang. Value is negative
18018 if no such glyph is found. */
18019
18020 static int
18021 right_overwriting (s)
18022 struct glyph_string *s;
18023 {
18024 int i, k, x;
18025 int end = s->row->used[s->area];
18026 struct glyph *glyphs = s->row->glyphs[s->area];
18027 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18028
18029 k = -1;
18030 x = 0;
18031 for (i = first; i < end; ++i)
18032 {
18033 int left, right;
18034 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18035 if (x - left < 0)
18036 k = i;
18037 x += glyphs[i].pixel_width;
18038 }
18039
18040 return k;
18041 }
18042
18043
18044 /* Get face and two-byte form of character C in face FACE_ID on frame
18045 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18046 means we want to display multibyte text. DISPLAY_P non-zero means
18047 make sure that X resources for the face returned are allocated.
18048 Value is a pointer to a realized face that is ready for display if
18049 DISPLAY_P is non-zero. */
18050
18051 static INLINE struct face *
18052 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18053 struct frame *f;
18054 int c, face_id;
18055 XChar2b *char2b;
18056 int multibyte_p, display_p;
18057 {
18058 struct face *face = FACE_FROM_ID (f, face_id);
18059
18060 if (!multibyte_p)
18061 {
18062 /* Unibyte case. We don't have to encode, but we have to make
18063 sure to use a face suitable for unibyte. */
18064 STORE_XCHAR2B (char2b, 0, c);
18065 face_id = FACE_FOR_CHAR (f, face, c);
18066 face = FACE_FROM_ID (f, face_id);
18067 }
18068 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18069 {
18070 /* Case of ASCII in a face known to fit ASCII. */
18071 STORE_XCHAR2B (char2b, 0, c);
18072 }
18073 else
18074 {
18075 int c1, c2, charset;
18076
18077 /* Split characters into bytes. If c2 is -1 afterwards, C is
18078 really a one-byte character so that byte1 is zero. */
18079 SPLIT_CHAR (c, charset, c1, c2);
18080 if (c2 > 0)
18081 STORE_XCHAR2B (char2b, c1, c2);
18082 else
18083 STORE_XCHAR2B (char2b, 0, c1);
18084
18085 /* Maybe encode the character in *CHAR2B. */
18086 if (face->font != NULL)
18087 {
18088 struct font_info *font_info
18089 = FONT_INFO_FROM_ID (f, face->font_info_id);
18090 if (font_info)
18091 rif->encode_char (c, char2b, font_info, 0);
18092 }
18093 }
18094
18095 /* Make sure X resources of the face are allocated. */
18096 #ifdef HAVE_X_WINDOWS
18097 if (display_p)
18098 #endif
18099 {
18100 xassert (face != NULL);
18101 PREPARE_FACE_FOR_DISPLAY (f, face);
18102 }
18103
18104 return face;
18105 }
18106
18107
18108 /* Set background width of glyph string S. START is the index of the
18109 first glyph following S. LAST_X is the right-most x-position + 1
18110 in the drawing area. */
18111
18112 static INLINE void
18113 set_glyph_string_background_width (s, start, last_x)
18114 struct glyph_string *s;
18115 int start;
18116 int last_x;
18117 {
18118 /* If the face of this glyph string has to be drawn to the end of
18119 the drawing area, set S->extends_to_end_of_line_p. */
18120 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18121
18122 if (start == s->row->used[s->area]
18123 && s->area == TEXT_AREA
18124 && ((s->hl == DRAW_NORMAL_TEXT
18125 && (s->row->fill_line_p
18126 || s->face->background != default_face->background
18127 || s->face->stipple != default_face->stipple
18128 || s->row->mouse_face_p))
18129 || s->hl == DRAW_MOUSE_FACE
18130 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18131 && s->row->fill_line_p)))
18132 s->extends_to_end_of_line_p = 1;
18133
18134 /* If S extends its face to the end of the line, set its
18135 background_width to the distance to the right edge of the drawing
18136 area. */
18137 if (s->extends_to_end_of_line_p)
18138 s->background_width = last_x - s->x + 1;
18139 else
18140 s->background_width = s->width;
18141 }
18142
18143
18144 /* Compute overhangs and x-positions for glyph string S and its
18145 predecessors, or successors. X is the starting x-position for S.
18146 BACKWARD_P non-zero means process predecessors. */
18147
18148 static void
18149 compute_overhangs_and_x (s, x, backward_p)
18150 struct glyph_string *s;
18151 int x;
18152 int backward_p;
18153 {
18154 if (backward_p)
18155 {
18156 while (s)
18157 {
18158 if (rif->compute_glyph_string_overhangs)
18159 rif->compute_glyph_string_overhangs (s);
18160 x -= s->width;
18161 s->x = x;
18162 s = s->prev;
18163 }
18164 }
18165 else
18166 {
18167 while (s)
18168 {
18169 if (rif->compute_glyph_string_overhangs)
18170 rif->compute_glyph_string_overhangs (s);
18171 s->x = x;
18172 x += s->width;
18173 s = s->next;
18174 }
18175 }
18176 }
18177
18178
18179
18180 /* The following macros are only called from draw_glyphs below.
18181 They reference the following parameters of that function directly:
18182 `w', `row', `area', and `overlap_p'
18183 as well as the following local variables:
18184 `s', `f', and `hdc' (in W32) */
18185
18186 #ifdef HAVE_NTGUI
18187 /* On W32, silently add local `hdc' variable to argument list of
18188 init_glyph_string. */
18189 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18190 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18191 #else
18192 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18193 init_glyph_string (s, char2b, w, row, area, start, hl)
18194 #endif
18195
18196 /* Add a glyph string for a stretch glyph to the list of strings
18197 between HEAD and TAIL. START is the index of the stretch glyph in
18198 row area AREA of glyph row ROW. END is the index of the last glyph
18199 in that glyph row area. X is the current output position assigned
18200 to the new glyph string constructed. HL overrides that face of the
18201 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18202 is the right-most x-position of the drawing area. */
18203
18204 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18205 and below -- keep them on one line. */
18206 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18207 do \
18208 { \
18209 s = (struct glyph_string *) alloca (sizeof *s); \
18210 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18211 START = fill_stretch_glyph_string (s, row, area, START, END); \
18212 append_glyph_string (&HEAD, &TAIL, s); \
18213 s->x = (X); \
18214 } \
18215 while (0)
18216
18217
18218 /* Add a glyph string for an image glyph to the list of strings
18219 between HEAD and TAIL. START is the index of the image glyph in
18220 row area AREA of glyph row ROW. END is the index of the last glyph
18221 in that glyph row area. X is the current output position assigned
18222 to the new glyph string constructed. HL overrides that face of the
18223 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18224 is the right-most x-position of the drawing area. */
18225
18226 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18227 do \
18228 { \
18229 s = (struct glyph_string *) alloca (sizeof *s); \
18230 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18231 fill_image_glyph_string (s); \
18232 append_glyph_string (&HEAD, &TAIL, s); \
18233 ++START; \
18234 s->x = (X); \
18235 } \
18236 while (0)
18237
18238
18239 /* Add a glyph string for a sequence of character glyphs to the list
18240 of strings between HEAD and TAIL. START is the index of the first
18241 glyph in row area AREA of glyph row ROW that is part of the new
18242 glyph string. END is the index of the last glyph in that glyph row
18243 area. X is the current output position assigned to the new glyph
18244 string constructed. HL overrides that face of the glyph; e.g. it
18245 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18246 right-most x-position of the drawing area. */
18247
18248 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18249 do \
18250 { \
18251 int c, face_id; \
18252 XChar2b *char2b; \
18253 \
18254 c = (row)->glyphs[area][START].u.ch; \
18255 face_id = (row)->glyphs[area][START].face_id; \
18256 \
18257 s = (struct glyph_string *) alloca (sizeof *s); \
18258 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18259 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18260 append_glyph_string (&HEAD, &TAIL, s); \
18261 s->x = (X); \
18262 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18263 } \
18264 while (0)
18265
18266
18267 /* Add a glyph string for a composite sequence to the list of strings
18268 between HEAD and TAIL. START is the index of the first glyph in
18269 row area AREA of glyph row ROW that is part of the new glyph
18270 string. END is the index of the last glyph in that glyph row area.
18271 X is the current output position assigned to the new glyph string
18272 constructed. HL overrides that face of the glyph; e.g. it is
18273 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18274 x-position of the drawing area. */
18275
18276 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18277 do { \
18278 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18279 int face_id = (row)->glyphs[area][START].face_id; \
18280 struct face *base_face = FACE_FROM_ID (f, face_id); \
18281 struct composition *cmp = composition_table[cmp_id]; \
18282 int glyph_len = cmp->glyph_len; \
18283 XChar2b *char2b; \
18284 struct face **faces; \
18285 struct glyph_string *first_s = NULL; \
18286 int n; \
18287 \
18288 base_face = base_face->ascii_face; \
18289 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18290 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18291 /* At first, fill in `char2b' and `faces'. */ \
18292 for (n = 0; n < glyph_len; n++) \
18293 { \
18294 int c = COMPOSITION_GLYPH (cmp, n); \
18295 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18296 faces[n] = FACE_FROM_ID (f, this_face_id); \
18297 get_char_face_and_encoding (f, c, this_face_id, \
18298 char2b + n, 1, 1); \
18299 } \
18300 \
18301 /* Make glyph_strings for each glyph sequence that is drawable by \
18302 the same face, and append them to HEAD/TAIL. */ \
18303 for (n = 0; n < cmp->glyph_len;) \
18304 { \
18305 s = (struct glyph_string *) alloca (sizeof *s); \
18306 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18307 append_glyph_string (&(HEAD), &(TAIL), s); \
18308 s->cmp = cmp; \
18309 s->gidx = n; \
18310 s->x = (X); \
18311 \
18312 if (n == 0) \
18313 first_s = s; \
18314 \
18315 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18316 } \
18317 \
18318 ++START; \
18319 s = first_s; \
18320 } while (0)
18321
18322
18323 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18324 of AREA of glyph row ROW on window W between indices START and END.
18325 HL overrides the face for drawing glyph strings, e.g. it is
18326 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18327 x-positions of the drawing area.
18328
18329 This is an ugly monster macro construct because we must use alloca
18330 to allocate glyph strings (because draw_glyphs can be called
18331 asynchronously). */
18332
18333 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18334 do \
18335 { \
18336 HEAD = TAIL = NULL; \
18337 while (START < END) \
18338 { \
18339 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18340 switch (first_glyph->type) \
18341 { \
18342 case CHAR_GLYPH: \
18343 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18344 HL, X, LAST_X); \
18345 break; \
18346 \
18347 case COMPOSITE_GLYPH: \
18348 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18349 HL, X, LAST_X); \
18350 break; \
18351 \
18352 case STRETCH_GLYPH: \
18353 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18354 HL, X, LAST_X); \
18355 break; \
18356 \
18357 case IMAGE_GLYPH: \
18358 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18359 HL, X, LAST_X); \
18360 break; \
18361 \
18362 default: \
18363 abort (); \
18364 } \
18365 \
18366 set_glyph_string_background_width (s, START, LAST_X); \
18367 (X) += s->width; \
18368 } \
18369 } \
18370 while (0)
18371
18372
18373 /* Draw glyphs between START and END in AREA of ROW on window W,
18374 starting at x-position X. X is relative to AREA in W. HL is a
18375 face-override with the following meaning:
18376
18377 DRAW_NORMAL_TEXT draw normally
18378 DRAW_CURSOR draw in cursor face
18379 DRAW_MOUSE_FACE draw in mouse face.
18380 DRAW_INVERSE_VIDEO draw in mode line face
18381 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18382 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18383
18384 If OVERLAPS_P is non-zero, draw only the foreground of characters
18385 and clip to the physical height of ROW.
18386
18387 Value is the x-position reached, relative to AREA of W. */
18388
18389 static int
18390 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18391 struct window *w;
18392 int x;
18393 struct glyph_row *row;
18394 enum glyph_row_area area;
18395 int start, end;
18396 enum draw_glyphs_face hl;
18397 int overlaps_p;
18398 {
18399 struct glyph_string *head, *tail;
18400 struct glyph_string *s;
18401 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18402 int last_x, area_width;
18403 int x_reached;
18404 int i, j;
18405 struct frame *f = XFRAME (WINDOW_FRAME (w));
18406 DECLARE_HDC (hdc);
18407
18408 ALLOCATE_HDC (hdc, f);
18409
18410 /* Let's rather be paranoid than getting a SEGV. */
18411 end = min (end, row->used[area]);
18412 start = max (0, start);
18413 start = min (end, start);
18414
18415 /* Translate X to frame coordinates. Set last_x to the right
18416 end of the drawing area. */
18417 if (row->full_width_p)
18418 {
18419 /* X is relative to the left edge of W, without scroll bars
18420 or fringes. */
18421 x += WINDOW_LEFT_EDGE_X (w);
18422 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18423 }
18424 else
18425 {
18426 int area_left = window_box_left (w, area);
18427 x += area_left;
18428 area_width = window_box_width (w, area);
18429 last_x = area_left + area_width;
18430 }
18431
18432 /* Build a doubly-linked list of glyph_string structures between
18433 head and tail from what we have to draw. Note that the macro
18434 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18435 the reason we use a separate variable `i'. */
18436 i = start;
18437 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18438 if (tail)
18439 x_reached = tail->x + tail->background_width;
18440 else
18441 x_reached = x;
18442
18443 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18444 the row, redraw some glyphs in front or following the glyph
18445 strings built above. */
18446 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18447 {
18448 int dummy_x = 0;
18449 struct glyph_string *h, *t;
18450
18451 /* Compute overhangs for all glyph strings. */
18452 if (rif->compute_glyph_string_overhangs)
18453 for (s = head; s; s = s->next)
18454 rif->compute_glyph_string_overhangs (s);
18455
18456 /* Prepend glyph strings for glyphs in front of the first glyph
18457 string that are overwritten because of the first glyph
18458 string's left overhang. The background of all strings
18459 prepended must be drawn because the first glyph string
18460 draws over it. */
18461 i = left_overwritten (head);
18462 if (i >= 0)
18463 {
18464 j = i;
18465 BUILD_GLYPH_STRINGS (j, start, h, t,
18466 DRAW_NORMAL_TEXT, dummy_x, last_x);
18467 start = i;
18468 compute_overhangs_and_x (t, head->x, 1);
18469 prepend_glyph_string_lists (&head, &tail, h, t);
18470 clip_head = head;
18471 }
18472
18473 /* Prepend glyph strings for glyphs in front of the first glyph
18474 string that overwrite that glyph string because of their
18475 right overhang. For these strings, only the foreground must
18476 be drawn, because it draws over the glyph string at `head'.
18477 The background must not be drawn because this would overwrite
18478 right overhangs of preceding glyphs for which no glyph
18479 strings exist. */
18480 i = left_overwriting (head);
18481 if (i >= 0)
18482 {
18483 clip_head = head;
18484 BUILD_GLYPH_STRINGS (i, start, h, t,
18485 DRAW_NORMAL_TEXT, dummy_x, last_x);
18486 for (s = h; s; s = s->next)
18487 s->background_filled_p = 1;
18488 compute_overhangs_and_x (t, head->x, 1);
18489 prepend_glyph_string_lists (&head, &tail, h, t);
18490 }
18491
18492 /* Append glyphs strings for glyphs following the last glyph
18493 string tail that are overwritten by tail. The background of
18494 these strings has to be drawn because tail's foreground draws
18495 over it. */
18496 i = right_overwritten (tail);
18497 if (i >= 0)
18498 {
18499 BUILD_GLYPH_STRINGS (end, i, h, t,
18500 DRAW_NORMAL_TEXT, x, last_x);
18501 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18502 append_glyph_string_lists (&head, &tail, h, t);
18503 clip_tail = tail;
18504 }
18505
18506 /* Append glyph strings for glyphs following the last glyph
18507 string tail that overwrite tail. The foreground of such
18508 glyphs has to be drawn because it writes into the background
18509 of tail. The background must not be drawn because it could
18510 paint over the foreground of following glyphs. */
18511 i = right_overwriting (tail);
18512 if (i >= 0)
18513 {
18514 clip_tail = tail;
18515 BUILD_GLYPH_STRINGS (end, i, h, t,
18516 DRAW_NORMAL_TEXT, x, last_x);
18517 for (s = h; s; s = s->next)
18518 s->background_filled_p = 1;
18519 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18520 append_glyph_string_lists (&head, &tail, h, t);
18521 }
18522 if (clip_head || clip_tail)
18523 for (s = head; s; s = s->next)
18524 {
18525 s->clip_head = clip_head;
18526 s->clip_tail = clip_tail;
18527 }
18528 }
18529
18530 /* Draw all strings. */
18531 for (s = head; s; s = s->next)
18532 rif->draw_glyph_string (s);
18533
18534 if (area == TEXT_AREA
18535 && !row->full_width_p
18536 /* When drawing overlapping rows, only the glyph strings'
18537 foreground is drawn, which doesn't erase a cursor
18538 completely. */
18539 && !overlaps_p)
18540 {
18541 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18542 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18543 : (tail ? tail->x + tail->background_width : x));
18544
18545 int text_left = window_box_left (w, TEXT_AREA);
18546 x0 -= text_left;
18547 x1 -= text_left;
18548
18549 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18550 row->y, MATRIX_ROW_BOTTOM_Y (row));
18551 }
18552
18553 /* Value is the x-position up to which drawn, relative to AREA of W.
18554 This doesn't include parts drawn because of overhangs. */
18555 if (row->full_width_p)
18556 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18557 else
18558 x_reached -= window_box_left (w, area);
18559
18560 RELEASE_HDC (hdc, f);
18561
18562 return x_reached;
18563 }
18564
18565 /* Expand row matrix if too narrow. Don't expand if area
18566 is not present. */
18567
18568 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18569 { \
18570 if (!fonts_changed_p \
18571 && (it->glyph_row->glyphs[area] \
18572 < it->glyph_row->glyphs[area + 1])) \
18573 { \
18574 it->w->ncols_scale_factor++; \
18575 fonts_changed_p = 1; \
18576 } \
18577 }
18578
18579 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18580 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18581
18582 static INLINE void
18583 append_glyph (it)
18584 struct it *it;
18585 {
18586 struct glyph *glyph;
18587 enum glyph_row_area area = it->area;
18588
18589 xassert (it->glyph_row);
18590 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18591
18592 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18593 if (glyph < it->glyph_row->glyphs[area + 1])
18594 {
18595 glyph->charpos = CHARPOS (it->position);
18596 glyph->object = it->object;
18597 glyph->pixel_width = it->pixel_width;
18598 glyph->ascent = it->ascent;
18599 glyph->descent = it->descent;
18600 glyph->voffset = it->voffset;
18601 glyph->type = CHAR_GLYPH;
18602 glyph->multibyte_p = it->multibyte_p;
18603 glyph->left_box_line_p = it->start_of_box_run_p;
18604 glyph->right_box_line_p = it->end_of_box_run_p;
18605 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18606 || it->phys_descent > it->descent);
18607 glyph->padding_p = 0;
18608 glyph->glyph_not_available_p = it->glyph_not_available_p;
18609 glyph->face_id = it->face_id;
18610 glyph->u.ch = it->char_to_display;
18611 glyph->slice = null_glyph_slice;
18612 glyph->font_type = FONT_TYPE_UNKNOWN;
18613 ++it->glyph_row->used[area];
18614 }
18615 else
18616 IT_EXPAND_MATRIX_WIDTH (it, area);
18617 }
18618
18619 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18620 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18621
18622 static INLINE void
18623 append_composite_glyph (it)
18624 struct it *it;
18625 {
18626 struct glyph *glyph;
18627 enum glyph_row_area area = it->area;
18628
18629 xassert (it->glyph_row);
18630
18631 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18632 if (glyph < it->glyph_row->glyphs[area + 1])
18633 {
18634 glyph->charpos = CHARPOS (it->position);
18635 glyph->object = it->object;
18636 glyph->pixel_width = it->pixel_width;
18637 glyph->ascent = it->ascent;
18638 glyph->descent = it->descent;
18639 glyph->voffset = it->voffset;
18640 glyph->type = COMPOSITE_GLYPH;
18641 glyph->multibyte_p = it->multibyte_p;
18642 glyph->left_box_line_p = it->start_of_box_run_p;
18643 glyph->right_box_line_p = it->end_of_box_run_p;
18644 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18645 || it->phys_descent > it->descent);
18646 glyph->padding_p = 0;
18647 glyph->glyph_not_available_p = 0;
18648 glyph->face_id = it->face_id;
18649 glyph->u.cmp_id = it->cmp_id;
18650 glyph->slice = null_glyph_slice;
18651 glyph->font_type = FONT_TYPE_UNKNOWN;
18652 ++it->glyph_row->used[area];
18653 }
18654 else
18655 IT_EXPAND_MATRIX_WIDTH (it, area);
18656 }
18657
18658
18659 /* Change IT->ascent and IT->height according to the setting of
18660 IT->voffset. */
18661
18662 static INLINE void
18663 take_vertical_position_into_account (it)
18664 struct it *it;
18665 {
18666 if (it->voffset)
18667 {
18668 if (it->voffset < 0)
18669 /* Increase the ascent so that we can display the text higher
18670 in the line. */
18671 it->ascent -= it->voffset;
18672 else
18673 /* Increase the descent so that we can display the text lower
18674 in the line. */
18675 it->descent += it->voffset;
18676 }
18677 }
18678
18679
18680 /* Produce glyphs/get display metrics for the image IT is loaded with.
18681 See the description of struct display_iterator in dispextern.h for
18682 an overview of struct display_iterator. */
18683
18684 static void
18685 produce_image_glyph (it)
18686 struct it *it;
18687 {
18688 struct image *img;
18689 struct face *face;
18690 int glyph_ascent;
18691 struct glyph_slice slice;
18692
18693 xassert (it->what == IT_IMAGE);
18694
18695 face = FACE_FROM_ID (it->f, it->face_id);
18696 xassert (face);
18697 /* Make sure X resources of the face is loaded. */
18698 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18699
18700 if (it->image_id < 0)
18701 {
18702 /* Fringe bitmap. */
18703 it->ascent = it->phys_ascent = 0;
18704 it->descent = it->phys_descent = 0;
18705 it->pixel_width = 0;
18706 it->nglyphs = 0;
18707 return;
18708 }
18709
18710 img = IMAGE_FROM_ID (it->f, it->image_id);
18711 xassert (img);
18712 /* Make sure X resources of the image is loaded. */
18713 prepare_image_for_display (it->f, img);
18714
18715 slice.x = slice.y = 0;
18716 slice.width = img->width;
18717 slice.height = img->height;
18718
18719 if (INTEGERP (it->slice.x))
18720 slice.x = XINT (it->slice.x);
18721 else if (FLOATP (it->slice.x))
18722 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18723
18724 if (INTEGERP (it->slice.y))
18725 slice.y = XINT (it->slice.y);
18726 else if (FLOATP (it->slice.y))
18727 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18728
18729 if (INTEGERP (it->slice.width))
18730 slice.width = XINT (it->slice.width);
18731 else if (FLOATP (it->slice.width))
18732 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18733
18734 if (INTEGERP (it->slice.height))
18735 slice.height = XINT (it->slice.height);
18736 else if (FLOATP (it->slice.height))
18737 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18738
18739 if (slice.x >= img->width)
18740 slice.x = img->width;
18741 if (slice.y >= img->height)
18742 slice.y = img->height;
18743 if (slice.x + slice.width >= img->width)
18744 slice.width = img->width - slice.x;
18745 if (slice.y + slice.height > img->height)
18746 slice.height = img->height - slice.y;
18747
18748 if (slice.width == 0 || slice.height == 0)
18749 return;
18750
18751 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18752
18753 it->descent = slice.height - glyph_ascent;
18754 if (slice.y == 0)
18755 it->descent += img->vmargin;
18756 if (slice.y + slice.height == img->height)
18757 it->descent += img->vmargin;
18758 it->phys_descent = it->descent;
18759
18760 it->pixel_width = slice.width;
18761 if (slice.x == 0)
18762 it->pixel_width += img->hmargin;
18763 if (slice.x + slice.width == img->width)
18764 it->pixel_width += img->hmargin;
18765
18766 /* It's quite possible for images to have an ascent greater than
18767 their height, so don't get confused in that case. */
18768 if (it->descent < 0)
18769 it->descent = 0;
18770
18771 #if 0 /* this breaks image tiling */
18772 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18773 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18774 if (face_ascent > it->ascent)
18775 it->ascent = it->phys_ascent = face_ascent;
18776 #endif
18777
18778 it->nglyphs = 1;
18779
18780 if (face->box != FACE_NO_BOX)
18781 {
18782 if (face->box_line_width > 0)
18783 {
18784 if (slice.y == 0)
18785 it->ascent += face->box_line_width;
18786 if (slice.y + slice.height == img->height)
18787 it->descent += face->box_line_width;
18788 }
18789
18790 if (it->start_of_box_run_p && slice.x == 0)
18791 it->pixel_width += abs (face->box_line_width);
18792 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18793 it->pixel_width += abs (face->box_line_width);
18794 }
18795
18796 take_vertical_position_into_account (it);
18797
18798 if (it->glyph_row)
18799 {
18800 struct glyph *glyph;
18801 enum glyph_row_area area = it->area;
18802
18803 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18804 if (glyph < it->glyph_row->glyphs[area + 1])
18805 {
18806 glyph->charpos = CHARPOS (it->position);
18807 glyph->object = it->object;
18808 glyph->pixel_width = it->pixel_width;
18809 glyph->ascent = glyph_ascent;
18810 glyph->descent = it->descent;
18811 glyph->voffset = it->voffset;
18812 glyph->type = IMAGE_GLYPH;
18813 glyph->multibyte_p = it->multibyte_p;
18814 glyph->left_box_line_p = it->start_of_box_run_p;
18815 glyph->right_box_line_p = it->end_of_box_run_p;
18816 glyph->overlaps_vertically_p = 0;
18817 glyph->padding_p = 0;
18818 glyph->glyph_not_available_p = 0;
18819 glyph->face_id = it->face_id;
18820 glyph->u.img_id = img->id;
18821 glyph->slice = slice;
18822 glyph->font_type = FONT_TYPE_UNKNOWN;
18823 ++it->glyph_row->used[area];
18824 }
18825 else
18826 IT_EXPAND_MATRIX_WIDTH (it, area);
18827 }
18828 }
18829
18830
18831 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18832 of the glyph, WIDTH and HEIGHT are the width and height of the
18833 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18834
18835 static void
18836 append_stretch_glyph (it, object, width, height, ascent)
18837 struct it *it;
18838 Lisp_Object object;
18839 int width, height;
18840 int ascent;
18841 {
18842 struct glyph *glyph;
18843 enum glyph_row_area area = it->area;
18844
18845 xassert (ascent >= 0 && ascent <= height);
18846
18847 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18848 if (glyph < it->glyph_row->glyphs[area + 1])
18849 {
18850 glyph->charpos = CHARPOS (it->position);
18851 glyph->object = object;
18852 glyph->pixel_width = width;
18853 glyph->ascent = ascent;
18854 glyph->descent = height - ascent;
18855 glyph->voffset = it->voffset;
18856 glyph->type = STRETCH_GLYPH;
18857 glyph->multibyte_p = it->multibyte_p;
18858 glyph->left_box_line_p = it->start_of_box_run_p;
18859 glyph->right_box_line_p = it->end_of_box_run_p;
18860 glyph->overlaps_vertically_p = 0;
18861 glyph->padding_p = 0;
18862 glyph->glyph_not_available_p = 0;
18863 glyph->face_id = it->face_id;
18864 glyph->u.stretch.ascent = ascent;
18865 glyph->u.stretch.height = height;
18866 glyph->slice = null_glyph_slice;
18867 glyph->font_type = FONT_TYPE_UNKNOWN;
18868 ++it->glyph_row->used[area];
18869 }
18870 else
18871 IT_EXPAND_MATRIX_WIDTH (it, area);
18872 }
18873
18874
18875 /* Produce a stretch glyph for iterator IT. IT->object is the value
18876 of the glyph property displayed. The value must be a list
18877 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18878 being recognized:
18879
18880 1. `:width WIDTH' specifies that the space should be WIDTH *
18881 canonical char width wide. WIDTH may be an integer or floating
18882 point number.
18883
18884 2. `:relative-width FACTOR' specifies that the width of the stretch
18885 should be computed from the width of the first character having the
18886 `glyph' property, and should be FACTOR times that width.
18887
18888 3. `:align-to HPOS' specifies that the space should be wide enough
18889 to reach HPOS, a value in canonical character units.
18890
18891 Exactly one of the above pairs must be present.
18892
18893 4. `:height HEIGHT' specifies that the height of the stretch produced
18894 should be HEIGHT, measured in canonical character units.
18895
18896 5. `:relative-height FACTOR' specifies that the height of the
18897 stretch should be FACTOR times the height of the characters having
18898 the glyph property.
18899
18900 Either none or exactly one of 4 or 5 must be present.
18901
18902 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18903 of the stretch should be used for the ascent of the stretch.
18904 ASCENT must be in the range 0 <= ASCENT <= 100. */
18905
18906 static void
18907 produce_stretch_glyph (it)
18908 struct it *it;
18909 {
18910 /* (space :width WIDTH :height HEIGHT ...) */
18911 Lisp_Object prop, plist;
18912 int width = 0, height = 0, align_to = -1;
18913 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18914 int ascent = 0;
18915 double tem;
18916 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18917 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18918
18919 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18920
18921 /* List should start with `space'. */
18922 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18923 plist = XCDR (it->object);
18924
18925 /* Compute the width of the stretch. */
18926 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18927 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18928 {
18929 /* Absolute width `:width WIDTH' specified and valid. */
18930 zero_width_ok_p = 1;
18931 width = (int)tem;
18932 }
18933 else if (prop = Fplist_get (plist, QCrelative_width),
18934 NUMVAL (prop) > 0)
18935 {
18936 /* Relative width `:relative-width FACTOR' specified and valid.
18937 Compute the width of the characters having the `glyph'
18938 property. */
18939 struct it it2;
18940 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18941
18942 it2 = *it;
18943 if (it->multibyte_p)
18944 {
18945 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18946 - IT_BYTEPOS (*it));
18947 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18948 }
18949 else
18950 it2.c = *p, it2.len = 1;
18951
18952 it2.glyph_row = NULL;
18953 it2.what = IT_CHARACTER;
18954 x_produce_glyphs (&it2);
18955 width = NUMVAL (prop) * it2.pixel_width;
18956 }
18957 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18958 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18959 {
18960 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18961 align_to = (align_to < 0
18962 ? 0
18963 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18964 else if (align_to < 0)
18965 align_to = window_box_left_offset (it->w, TEXT_AREA);
18966 width = max (0, (int)tem + align_to - it->current_x);
18967 zero_width_ok_p = 1;
18968 }
18969 else
18970 /* Nothing specified -> width defaults to canonical char width. */
18971 width = FRAME_COLUMN_WIDTH (it->f);
18972
18973 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18974 width = 1;
18975
18976 /* Compute height. */
18977 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18978 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18979 {
18980 height = (int)tem;
18981 zero_height_ok_p = 1;
18982 }
18983 else if (prop = Fplist_get (plist, QCrelative_height),
18984 NUMVAL (prop) > 0)
18985 height = FONT_HEIGHT (font) * NUMVAL (prop);
18986 else
18987 height = FONT_HEIGHT (font);
18988
18989 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18990 height = 1;
18991
18992 /* Compute percentage of height used for ascent. If
18993 `:ascent ASCENT' is present and valid, use that. Otherwise,
18994 derive the ascent from the font in use. */
18995 if (prop = Fplist_get (plist, QCascent),
18996 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18997 ascent = height * NUMVAL (prop) / 100.0;
18998 else if (!NILP (prop)
18999 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19000 ascent = min (max (0, (int)tem), height);
19001 else
19002 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19003
19004 if (width > 0 && height > 0 && it->glyph_row)
19005 {
19006 Lisp_Object object = it->stack[it->sp - 1].string;
19007 if (!STRINGP (object))
19008 object = it->w->buffer;
19009 append_stretch_glyph (it, object, width, height, ascent);
19010 }
19011
19012 it->pixel_width = width;
19013 it->ascent = it->phys_ascent = ascent;
19014 it->descent = it->phys_descent = height - it->ascent;
19015 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19016
19017 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19018 {
19019 if (face->box_line_width > 0)
19020 {
19021 it->ascent += face->box_line_width;
19022 it->descent += face->box_line_width;
19023 }
19024
19025 if (it->start_of_box_run_p)
19026 it->pixel_width += abs (face->box_line_width);
19027 if (it->end_of_box_run_p)
19028 it->pixel_width += abs (face->box_line_width);
19029 }
19030
19031 take_vertical_position_into_account (it);
19032 }
19033
19034 /* Get line-height and line-spacing property at point.
19035 If line-height has format (HEIGHT TOTAL), return TOTAL
19036 in TOTAL_HEIGHT. */
19037
19038 static Lisp_Object
19039 get_line_height_property (it, prop)
19040 struct it *it;
19041 Lisp_Object prop;
19042 {
19043 Lisp_Object position, val;
19044
19045 if (STRINGP (it->object))
19046 position = make_number (IT_STRING_CHARPOS (*it));
19047 else if (BUFFERP (it->object))
19048 position = make_number (IT_CHARPOS (*it));
19049 else
19050 return Qnil;
19051
19052 return Fget_char_property (position, prop, it->object);
19053 }
19054
19055 /* Calculate line-height and line-spacing properties.
19056 An integer value specifies explicit pixel value.
19057 A float value specifies relative value to current face height.
19058 A cons (float . face-name) specifies relative value to
19059 height of specified face font.
19060
19061 Returns height in pixels, or nil. */
19062
19063
19064 static Lisp_Object
19065 calc_line_height_property (it, val, font, boff, override)
19066 struct it *it;
19067 Lisp_Object val;
19068 XFontStruct *font;
19069 int boff, override;
19070 {
19071 Lisp_Object face_name = Qnil;
19072 int ascent, descent, height;
19073
19074 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19075 return val;
19076
19077 if (CONSP (val))
19078 {
19079 face_name = XCAR (val);
19080 val = XCDR (val);
19081 if (!NUMBERP (val))
19082 val = make_number (1);
19083 if (NILP (face_name))
19084 {
19085 height = it->ascent + it->descent;
19086 goto scale;
19087 }
19088 }
19089
19090 if (NILP (face_name))
19091 {
19092 font = FRAME_FONT (it->f);
19093 boff = FRAME_BASELINE_OFFSET (it->f);
19094 }
19095 else if (EQ (face_name, Qt))
19096 {
19097 override = 0;
19098 }
19099 else
19100 {
19101 int face_id;
19102 struct face *face;
19103 struct font_info *font_info;
19104
19105 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19106 if (face_id < 0)
19107 return make_number (-1);
19108
19109 face = FACE_FROM_ID (it->f, face_id);
19110 font = face->font;
19111 if (font == NULL)
19112 return make_number (-1);
19113
19114 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19115 boff = font_info->baseline_offset;
19116 if (font_info->vertical_centering)
19117 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19118 }
19119
19120 ascent = FONT_BASE (font) + boff;
19121 descent = FONT_DESCENT (font) - boff;
19122
19123 if (override)
19124 {
19125 it->override_ascent = ascent;
19126 it->override_descent = descent;
19127 it->override_boff = boff;
19128 }
19129
19130 height = ascent + descent;
19131
19132 scale:
19133 if (FLOATP (val))
19134 height = (int)(XFLOAT_DATA (val) * height);
19135 else if (INTEGERP (val))
19136 height *= XINT (val);
19137
19138 return make_number (height);
19139 }
19140
19141
19142 /* RIF:
19143 Produce glyphs/get display metrics for the display element IT is
19144 loaded with. See the description of struct display_iterator in
19145 dispextern.h for an overview of struct display_iterator. */
19146
19147 void
19148 x_produce_glyphs (it)
19149 struct it *it;
19150 {
19151 int extra_line_spacing = it->extra_line_spacing;
19152
19153 it->glyph_not_available_p = 0;
19154
19155 if (it->what == IT_CHARACTER)
19156 {
19157 XChar2b char2b;
19158 XFontStruct *font;
19159 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19160 XCharStruct *pcm;
19161 int font_not_found_p;
19162 struct font_info *font_info;
19163 int boff; /* baseline offset */
19164 /* We may change it->multibyte_p upon unibyte<->multibyte
19165 conversion. So, save the current value now and restore it
19166 later.
19167
19168 Note: It seems that we don't have to record multibyte_p in
19169 struct glyph because the character code itself tells if or
19170 not the character is multibyte. Thus, in the future, we must
19171 consider eliminating the field `multibyte_p' in the struct
19172 glyph. */
19173 int saved_multibyte_p = it->multibyte_p;
19174
19175 /* Maybe translate single-byte characters to multibyte, or the
19176 other way. */
19177 it->char_to_display = it->c;
19178 if (!ASCII_BYTE_P (it->c))
19179 {
19180 if (unibyte_display_via_language_environment
19181 && SINGLE_BYTE_CHAR_P (it->c)
19182 && (it->c >= 0240
19183 || !NILP (Vnonascii_translation_table)))
19184 {
19185 it->char_to_display = unibyte_char_to_multibyte (it->c);
19186 it->multibyte_p = 1;
19187 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19188 face = FACE_FROM_ID (it->f, it->face_id);
19189 }
19190 else if (!SINGLE_BYTE_CHAR_P (it->c)
19191 && !it->multibyte_p)
19192 {
19193 it->multibyte_p = 1;
19194 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19195 face = FACE_FROM_ID (it->f, it->face_id);
19196 }
19197 }
19198
19199 /* Get font to use. Encode IT->char_to_display. */
19200 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19201 &char2b, it->multibyte_p, 0);
19202 font = face->font;
19203
19204 /* When no suitable font found, use the default font. */
19205 font_not_found_p = font == NULL;
19206 if (font_not_found_p)
19207 {
19208 font = FRAME_FONT (it->f);
19209 boff = FRAME_BASELINE_OFFSET (it->f);
19210 font_info = NULL;
19211 }
19212 else
19213 {
19214 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19215 boff = font_info->baseline_offset;
19216 if (font_info->vertical_centering)
19217 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19218 }
19219
19220 if (it->char_to_display >= ' '
19221 && (!it->multibyte_p || it->char_to_display < 128))
19222 {
19223 /* Either unibyte or ASCII. */
19224 int stretched_p;
19225
19226 it->nglyphs = 1;
19227
19228 pcm = rif->per_char_metric (font, &char2b,
19229 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19230
19231 if (it->override_ascent >= 0)
19232 {
19233 it->ascent = it->override_ascent;
19234 it->descent = it->override_descent;
19235 boff = it->override_boff;
19236 }
19237 else
19238 {
19239 it->ascent = FONT_BASE (font) + boff;
19240 it->descent = FONT_DESCENT (font) - boff;
19241 }
19242
19243 if (pcm)
19244 {
19245 it->phys_ascent = pcm->ascent + boff;
19246 it->phys_descent = pcm->descent - boff;
19247 it->pixel_width = pcm->width;
19248 }
19249 else
19250 {
19251 it->glyph_not_available_p = 1;
19252 it->phys_ascent = it->ascent;
19253 it->phys_descent = it->descent;
19254 it->pixel_width = FONT_WIDTH (font);
19255 }
19256
19257 if (it->constrain_row_ascent_descent_p)
19258 {
19259 if (it->descent > it->max_descent)
19260 {
19261 it->ascent += it->descent - it->max_descent;
19262 it->descent = it->max_descent;
19263 }
19264 if (it->ascent > it->max_ascent)
19265 {
19266 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19267 it->ascent = it->max_ascent;
19268 }
19269 it->phys_ascent = min (it->phys_ascent, it->ascent);
19270 it->phys_descent = min (it->phys_descent, it->descent);
19271 extra_line_spacing = 0;
19272 }
19273
19274 /* If this is a space inside a region of text with
19275 `space-width' property, change its width. */
19276 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19277 if (stretched_p)
19278 it->pixel_width *= XFLOATINT (it->space_width);
19279
19280 /* If face has a box, add the box thickness to the character
19281 height. If character has a box line to the left and/or
19282 right, add the box line width to the character's width. */
19283 if (face->box != FACE_NO_BOX)
19284 {
19285 int thick = face->box_line_width;
19286
19287 if (thick > 0)
19288 {
19289 it->ascent += thick;
19290 it->descent += thick;
19291 }
19292 else
19293 thick = -thick;
19294
19295 if (it->start_of_box_run_p)
19296 it->pixel_width += thick;
19297 if (it->end_of_box_run_p)
19298 it->pixel_width += thick;
19299 }
19300
19301 /* If face has an overline, add the height of the overline
19302 (1 pixel) and a 1 pixel margin to the character height. */
19303 if (face->overline_p)
19304 it->ascent += 2;
19305
19306 if (it->constrain_row_ascent_descent_p)
19307 {
19308 if (it->ascent > it->max_ascent)
19309 it->ascent = it->max_ascent;
19310 if (it->descent > it->max_descent)
19311 it->descent = it->max_descent;
19312 }
19313
19314 take_vertical_position_into_account (it);
19315
19316 /* If we have to actually produce glyphs, do it. */
19317 if (it->glyph_row)
19318 {
19319 if (stretched_p)
19320 {
19321 /* Translate a space with a `space-width' property
19322 into a stretch glyph. */
19323 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19324 / FONT_HEIGHT (font));
19325 append_stretch_glyph (it, it->object, it->pixel_width,
19326 it->ascent + it->descent, ascent);
19327 }
19328 else
19329 append_glyph (it);
19330
19331 /* If characters with lbearing or rbearing are displayed
19332 in this line, record that fact in a flag of the
19333 glyph row. This is used to optimize X output code. */
19334 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19335 it->glyph_row->contains_overlapping_glyphs_p = 1;
19336 }
19337 }
19338 else if (it->char_to_display == '\n')
19339 {
19340 /* A newline has no width but we need the height of the line.
19341 But if previous part of the line set a height, don't
19342 increase that height */
19343
19344 Lisp_Object height;
19345 Lisp_Object total_height = Qnil;
19346
19347 it->override_ascent = -1;
19348 it->pixel_width = 0;
19349 it->nglyphs = 0;
19350
19351 height = get_line_height_property(it, Qline_height);
19352 /* Split (line-height total-height) list */
19353 if (CONSP (height)
19354 && CONSP (XCDR (height))
19355 && NILP (XCDR (XCDR (height))))
19356 {
19357 total_height = XCAR (XCDR (height));
19358 height = XCAR (height);
19359 }
19360 height = calc_line_height_property(it, height, font, boff, 1);
19361
19362 if (it->override_ascent >= 0)
19363 {
19364 it->ascent = it->override_ascent;
19365 it->descent = it->override_descent;
19366 boff = it->override_boff;
19367 }
19368 else
19369 {
19370 it->ascent = FONT_BASE (font) + boff;
19371 it->descent = FONT_DESCENT (font) - boff;
19372 }
19373
19374 if (EQ (height, Qt))
19375 {
19376 if (it->descent > it->max_descent)
19377 {
19378 it->ascent += it->descent - it->max_descent;
19379 it->descent = it->max_descent;
19380 }
19381 if (it->ascent > it->max_ascent)
19382 {
19383 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19384 it->ascent = it->max_ascent;
19385 }
19386 it->phys_ascent = min (it->phys_ascent, it->ascent);
19387 it->phys_descent = min (it->phys_descent, it->descent);
19388 it->constrain_row_ascent_descent_p = 1;
19389 extra_line_spacing = 0;
19390 }
19391 else
19392 {
19393 Lisp_Object spacing;
19394 int total = 0;
19395
19396 it->phys_ascent = it->ascent;
19397 it->phys_descent = it->descent;
19398
19399 if ((it->max_ascent > 0 || it->max_descent > 0)
19400 && face->box != FACE_NO_BOX
19401 && face->box_line_width > 0)
19402 {
19403 it->ascent += face->box_line_width;
19404 it->descent += face->box_line_width;
19405 }
19406 if (!NILP (height)
19407 && XINT (height) > it->ascent + it->descent)
19408 it->ascent = XINT (height) - it->descent;
19409
19410 if (!NILP (total_height))
19411 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19412 else
19413 {
19414 spacing = get_line_height_property(it, Qline_spacing);
19415 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19416 }
19417 if (INTEGERP (spacing))
19418 {
19419 extra_line_spacing = XINT (spacing);
19420 if (!NILP (total_height))
19421 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19422 }
19423 }
19424 }
19425 else if (it->char_to_display == '\t')
19426 {
19427 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19428 int x = it->current_x + it->continuation_lines_width;
19429 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19430
19431 /* If the distance from the current position to the next tab
19432 stop is less than a space character width, use the
19433 tab stop after that. */
19434 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19435 next_tab_x += tab_width;
19436
19437 it->pixel_width = next_tab_x - x;
19438 it->nglyphs = 1;
19439 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19440 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19441
19442 if (it->glyph_row)
19443 {
19444 append_stretch_glyph (it, it->object, it->pixel_width,
19445 it->ascent + it->descent, it->ascent);
19446 }
19447 }
19448 else
19449 {
19450 /* A multi-byte character. Assume that the display width of the
19451 character is the width of the character multiplied by the
19452 width of the font. */
19453
19454 /* If we found a font, this font should give us the right
19455 metrics. If we didn't find a font, use the frame's
19456 default font and calculate the width of the character
19457 from the charset width; this is what old redisplay code
19458 did. */
19459
19460 pcm = rif->per_char_metric (font, &char2b,
19461 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19462
19463 if (font_not_found_p || !pcm)
19464 {
19465 int charset = CHAR_CHARSET (it->char_to_display);
19466
19467 it->glyph_not_available_p = 1;
19468 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19469 * CHARSET_WIDTH (charset));
19470 it->phys_ascent = FONT_BASE (font) + boff;
19471 it->phys_descent = FONT_DESCENT (font) - boff;
19472 }
19473 else
19474 {
19475 it->pixel_width = pcm->width;
19476 it->phys_ascent = pcm->ascent + boff;
19477 it->phys_descent = pcm->descent - boff;
19478 if (it->glyph_row
19479 && (pcm->lbearing < 0
19480 || pcm->rbearing > pcm->width))
19481 it->glyph_row->contains_overlapping_glyphs_p = 1;
19482 }
19483 it->nglyphs = 1;
19484 it->ascent = FONT_BASE (font) + boff;
19485 it->descent = FONT_DESCENT (font) - boff;
19486 if (face->box != FACE_NO_BOX)
19487 {
19488 int thick = face->box_line_width;
19489
19490 if (thick > 0)
19491 {
19492 it->ascent += thick;
19493 it->descent += thick;
19494 }
19495 else
19496 thick = - thick;
19497
19498 if (it->start_of_box_run_p)
19499 it->pixel_width += thick;
19500 if (it->end_of_box_run_p)
19501 it->pixel_width += thick;
19502 }
19503
19504 /* If face has an overline, add the height of the overline
19505 (1 pixel) and a 1 pixel margin to the character height. */
19506 if (face->overline_p)
19507 it->ascent += 2;
19508
19509 take_vertical_position_into_account (it);
19510
19511 if (it->glyph_row)
19512 append_glyph (it);
19513 }
19514 it->multibyte_p = saved_multibyte_p;
19515 }
19516 else if (it->what == IT_COMPOSITION)
19517 {
19518 /* Note: A composition is represented as one glyph in the
19519 glyph matrix. There are no padding glyphs. */
19520 XChar2b char2b;
19521 XFontStruct *font;
19522 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19523 XCharStruct *pcm;
19524 int font_not_found_p;
19525 struct font_info *font_info;
19526 int boff; /* baseline offset */
19527 struct composition *cmp = composition_table[it->cmp_id];
19528
19529 /* Maybe translate single-byte characters to multibyte. */
19530 it->char_to_display = it->c;
19531 if (unibyte_display_via_language_environment
19532 && SINGLE_BYTE_CHAR_P (it->c)
19533 && (it->c >= 0240
19534 || (it->c >= 0200
19535 && !NILP (Vnonascii_translation_table))))
19536 {
19537 it->char_to_display = unibyte_char_to_multibyte (it->c);
19538 }
19539
19540 /* Get face and font to use. Encode IT->char_to_display. */
19541 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19542 face = FACE_FROM_ID (it->f, it->face_id);
19543 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19544 &char2b, it->multibyte_p, 0);
19545 font = face->font;
19546
19547 /* When no suitable font found, use the default font. */
19548 font_not_found_p = font == NULL;
19549 if (font_not_found_p)
19550 {
19551 font = FRAME_FONT (it->f);
19552 boff = FRAME_BASELINE_OFFSET (it->f);
19553 font_info = NULL;
19554 }
19555 else
19556 {
19557 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19558 boff = font_info->baseline_offset;
19559 if (font_info->vertical_centering)
19560 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19561 }
19562
19563 /* There are no padding glyphs, so there is only one glyph to
19564 produce for the composition. Important is that pixel_width,
19565 ascent and descent are the values of what is drawn by
19566 draw_glyphs (i.e. the values of the overall glyphs composed). */
19567 it->nglyphs = 1;
19568
19569 /* If we have not yet calculated pixel size data of glyphs of
19570 the composition for the current face font, calculate them
19571 now. Theoretically, we have to check all fonts for the
19572 glyphs, but that requires much time and memory space. So,
19573 here we check only the font of the first glyph. This leads
19574 to incorrect display very rarely, and C-l (recenter) can
19575 correct the display anyway. */
19576 if (cmp->font != (void *) font)
19577 {
19578 /* Ascent and descent of the font of the first character of
19579 this composition (adjusted by baseline offset). Ascent
19580 and descent of overall glyphs should not be less than
19581 them respectively. */
19582 int font_ascent = FONT_BASE (font) + boff;
19583 int font_descent = FONT_DESCENT (font) - boff;
19584 /* Bounding box of the overall glyphs. */
19585 int leftmost, rightmost, lowest, highest;
19586 int i, width, ascent, descent;
19587
19588 cmp->font = (void *) font;
19589
19590 /* Initialize the bounding box. */
19591 if (font_info
19592 && (pcm = rif->per_char_metric (font, &char2b,
19593 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19594 {
19595 width = pcm->width;
19596 ascent = pcm->ascent;
19597 descent = pcm->descent;
19598 }
19599 else
19600 {
19601 width = FONT_WIDTH (font);
19602 ascent = FONT_BASE (font);
19603 descent = FONT_DESCENT (font);
19604 }
19605
19606 rightmost = width;
19607 lowest = - descent + boff;
19608 highest = ascent + boff;
19609 leftmost = 0;
19610
19611 if (font_info
19612 && font_info->default_ascent
19613 && CHAR_TABLE_P (Vuse_default_ascent)
19614 && !NILP (Faref (Vuse_default_ascent,
19615 make_number (it->char_to_display))))
19616 highest = font_info->default_ascent + boff;
19617
19618 /* Draw the first glyph at the normal position. It may be
19619 shifted to right later if some other glyphs are drawn at
19620 the left. */
19621 cmp->offsets[0] = 0;
19622 cmp->offsets[1] = boff;
19623
19624 /* Set cmp->offsets for the remaining glyphs. */
19625 for (i = 1; i < cmp->glyph_len; i++)
19626 {
19627 int left, right, btm, top;
19628 int ch = COMPOSITION_GLYPH (cmp, i);
19629 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19630
19631 face = FACE_FROM_ID (it->f, face_id);
19632 get_char_face_and_encoding (it->f, ch, face->id,
19633 &char2b, it->multibyte_p, 0);
19634 font = face->font;
19635 if (font == NULL)
19636 {
19637 font = FRAME_FONT (it->f);
19638 boff = FRAME_BASELINE_OFFSET (it->f);
19639 font_info = NULL;
19640 }
19641 else
19642 {
19643 font_info
19644 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19645 boff = font_info->baseline_offset;
19646 if (font_info->vertical_centering)
19647 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19648 }
19649
19650 if (font_info
19651 && (pcm = rif->per_char_metric (font, &char2b,
19652 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19653 {
19654 width = pcm->width;
19655 ascent = pcm->ascent;
19656 descent = pcm->descent;
19657 }
19658 else
19659 {
19660 width = FONT_WIDTH (font);
19661 ascent = 1;
19662 descent = 0;
19663 }
19664
19665 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19666 {
19667 /* Relative composition with or without
19668 alternate chars. */
19669 left = (leftmost + rightmost - width) / 2;
19670 btm = - descent + boff;
19671 if (font_info && font_info->relative_compose
19672 && (! CHAR_TABLE_P (Vignore_relative_composition)
19673 || NILP (Faref (Vignore_relative_composition,
19674 make_number (ch)))))
19675 {
19676
19677 if (- descent >= font_info->relative_compose)
19678 /* One extra pixel between two glyphs. */
19679 btm = highest + 1;
19680 else if (ascent <= 0)
19681 /* One extra pixel between two glyphs. */
19682 btm = lowest - 1 - ascent - descent;
19683 }
19684 }
19685 else
19686 {
19687 /* A composition rule is specified by an integer
19688 value that encodes global and new reference
19689 points (GREF and NREF). GREF and NREF are
19690 specified by numbers as below:
19691
19692 0---1---2 -- ascent
19693 | |
19694 | |
19695 | |
19696 9--10--11 -- center
19697 | |
19698 ---3---4---5--- baseline
19699 | |
19700 6---7---8 -- descent
19701 */
19702 int rule = COMPOSITION_RULE (cmp, i);
19703 int gref, nref, grefx, grefy, nrefx, nrefy;
19704
19705 COMPOSITION_DECODE_RULE (rule, gref, nref);
19706 grefx = gref % 3, nrefx = nref % 3;
19707 grefy = gref / 3, nrefy = nref / 3;
19708
19709 left = (leftmost
19710 + grefx * (rightmost - leftmost) / 2
19711 - nrefx * width / 2);
19712 btm = ((grefy == 0 ? highest
19713 : grefy == 1 ? 0
19714 : grefy == 2 ? lowest
19715 : (highest + lowest) / 2)
19716 - (nrefy == 0 ? ascent + descent
19717 : nrefy == 1 ? descent - boff
19718 : nrefy == 2 ? 0
19719 : (ascent + descent) / 2));
19720 }
19721
19722 cmp->offsets[i * 2] = left;
19723 cmp->offsets[i * 2 + 1] = btm + descent;
19724
19725 /* Update the bounding box of the overall glyphs. */
19726 right = left + width;
19727 top = btm + descent + ascent;
19728 if (left < leftmost)
19729 leftmost = left;
19730 if (right > rightmost)
19731 rightmost = right;
19732 if (top > highest)
19733 highest = top;
19734 if (btm < lowest)
19735 lowest = btm;
19736 }
19737
19738 /* If there are glyphs whose x-offsets are negative,
19739 shift all glyphs to the right and make all x-offsets
19740 non-negative. */
19741 if (leftmost < 0)
19742 {
19743 for (i = 0; i < cmp->glyph_len; i++)
19744 cmp->offsets[i * 2] -= leftmost;
19745 rightmost -= leftmost;
19746 }
19747
19748 cmp->pixel_width = rightmost;
19749 cmp->ascent = highest;
19750 cmp->descent = - lowest;
19751 if (cmp->ascent < font_ascent)
19752 cmp->ascent = font_ascent;
19753 if (cmp->descent < font_descent)
19754 cmp->descent = font_descent;
19755 }
19756
19757 it->pixel_width = cmp->pixel_width;
19758 it->ascent = it->phys_ascent = cmp->ascent;
19759 it->descent = it->phys_descent = cmp->descent;
19760
19761 if (face->box != FACE_NO_BOX)
19762 {
19763 int thick = face->box_line_width;
19764
19765 if (thick > 0)
19766 {
19767 it->ascent += thick;
19768 it->descent += thick;
19769 }
19770 else
19771 thick = - thick;
19772
19773 if (it->start_of_box_run_p)
19774 it->pixel_width += thick;
19775 if (it->end_of_box_run_p)
19776 it->pixel_width += thick;
19777 }
19778
19779 /* If face has an overline, add the height of the overline
19780 (1 pixel) and a 1 pixel margin to the character height. */
19781 if (face->overline_p)
19782 it->ascent += 2;
19783
19784 take_vertical_position_into_account (it);
19785
19786 if (it->glyph_row)
19787 append_composite_glyph (it);
19788 }
19789 else if (it->what == IT_IMAGE)
19790 produce_image_glyph (it);
19791 else if (it->what == IT_STRETCH)
19792 produce_stretch_glyph (it);
19793
19794 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19795 because this isn't true for images with `:ascent 100'. */
19796 xassert (it->ascent >= 0 && it->descent >= 0);
19797 if (it->area == TEXT_AREA)
19798 it->current_x += it->pixel_width;
19799
19800 if (extra_line_spacing > 0)
19801 {
19802 it->descent += extra_line_spacing;
19803 if (extra_line_spacing > it->max_extra_line_spacing)
19804 it->max_extra_line_spacing = extra_line_spacing;
19805 }
19806
19807 it->max_ascent = max (it->max_ascent, it->ascent);
19808 it->max_descent = max (it->max_descent, it->descent);
19809 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19810 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19811 }
19812
19813 /* EXPORT for RIF:
19814 Output LEN glyphs starting at START at the nominal cursor position.
19815 Advance the nominal cursor over the text. The global variable
19816 updated_window contains the window being updated, updated_row is
19817 the glyph row being updated, and updated_area is the area of that
19818 row being updated. */
19819
19820 void
19821 x_write_glyphs (start, len)
19822 struct glyph *start;
19823 int len;
19824 {
19825 int x, hpos;
19826
19827 xassert (updated_window && updated_row);
19828 BLOCK_INPUT;
19829
19830 /* Write glyphs. */
19831
19832 hpos = start - updated_row->glyphs[updated_area];
19833 x = draw_glyphs (updated_window, output_cursor.x,
19834 updated_row, updated_area,
19835 hpos, hpos + len,
19836 DRAW_NORMAL_TEXT, 0);
19837
19838 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19839 if (updated_area == TEXT_AREA
19840 && updated_window->phys_cursor_on_p
19841 && updated_window->phys_cursor.vpos == output_cursor.vpos
19842 && updated_window->phys_cursor.hpos >= hpos
19843 && updated_window->phys_cursor.hpos < hpos + len)
19844 updated_window->phys_cursor_on_p = 0;
19845
19846 UNBLOCK_INPUT;
19847
19848 /* Advance the output cursor. */
19849 output_cursor.hpos += len;
19850 output_cursor.x = x;
19851 }
19852
19853
19854 /* EXPORT for RIF:
19855 Insert LEN glyphs from START at the nominal cursor position. */
19856
19857 void
19858 x_insert_glyphs (start, len)
19859 struct glyph *start;
19860 int len;
19861 {
19862 struct frame *f;
19863 struct window *w;
19864 int line_height, shift_by_width, shifted_region_width;
19865 struct glyph_row *row;
19866 struct glyph *glyph;
19867 int frame_x, frame_y, hpos;
19868
19869 xassert (updated_window && updated_row);
19870 BLOCK_INPUT;
19871 w = updated_window;
19872 f = XFRAME (WINDOW_FRAME (w));
19873
19874 /* Get the height of the line we are in. */
19875 row = updated_row;
19876 line_height = row->height;
19877
19878 /* Get the width of the glyphs to insert. */
19879 shift_by_width = 0;
19880 for (glyph = start; glyph < start + len; ++glyph)
19881 shift_by_width += glyph->pixel_width;
19882
19883 /* Get the width of the region to shift right. */
19884 shifted_region_width = (window_box_width (w, updated_area)
19885 - output_cursor.x
19886 - shift_by_width);
19887
19888 /* Shift right. */
19889 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19890 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19891
19892 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19893 line_height, shift_by_width);
19894
19895 /* Write the glyphs. */
19896 hpos = start - row->glyphs[updated_area];
19897 draw_glyphs (w, output_cursor.x, row, updated_area,
19898 hpos, hpos + len,
19899 DRAW_NORMAL_TEXT, 0);
19900
19901 /* Advance the output cursor. */
19902 output_cursor.hpos += len;
19903 output_cursor.x += shift_by_width;
19904 UNBLOCK_INPUT;
19905 }
19906
19907
19908 /* EXPORT for RIF:
19909 Erase the current text line from the nominal cursor position
19910 (inclusive) to pixel column TO_X (exclusive). The idea is that
19911 everything from TO_X onward is already erased.
19912
19913 TO_X is a pixel position relative to updated_area of
19914 updated_window. TO_X == -1 means clear to the end of this area. */
19915
19916 void
19917 x_clear_end_of_line (to_x)
19918 int to_x;
19919 {
19920 struct frame *f;
19921 struct window *w = updated_window;
19922 int max_x, min_y, max_y;
19923 int from_x, from_y, to_y;
19924
19925 xassert (updated_window && updated_row);
19926 f = XFRAME (w->frame);
19927
19928 if (updated_row->full_width_p)
19929 max_x = WINDOW_TOTAL_WIDTH (w);
19930 else
19931 max_x = window_box_width (w, updated_area);
19932 max_y = window_text_bottom_y (w);
19933
19934 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19935 of window. For TO_X > 0, truncate to end of drawing area. */
19936 if (to_x == 0)
19937 return;
19938 else if (to_x < 0)
19939 to_x = max_x;
19940 else
19941 to_x = min (to_x, max_x);
19942
19943 to_y = min (max_y, output_cursor.y + updated_row->height);
19944
19945 /* Notice if the cursor will be cleared by this operation. */
19946 if (!updated_row->full_width_p)
19947 notice_overwritten_cursor (w, updated_area,
19948 output_cursor.x, -1,
19949 updated_row->y,
19950 MATRIX_ROW_BOTTOM_Y (updated_row));
19951
19952 from_x = output_cursor.x;
19953
19954 /* Translate to frame coordinates. */
19955 if (updated_row->full_width_p)
19956 {
19957 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19958 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19959 }
19960 else
19961 {
19962 int area_left = window_box_left (w, updated_area);
19963 from_x += area_left;
19964 to_x += area_left;
19965 }
19966
19967 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19968 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19969 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19970
19971 /* Prevent inadvertently clearing to end of the X window. */
19972 if (to_x > from_x && to_y > from_y)
19973 {
19974 BLOCK_INPUT;
19975 rif->clear_frame_area (f, from_x, from_y,
19976 to_x - from_x, to_y - from_y);
19977 UNBLOCK_INPUT;
19978 }
19979 }
19980
19981 #endif /* HAVE_WINDOW_SYSTEM */
19982
19983
19984 \f
19985 /***********************************************************************
19986 Cursor types
19987 ***********************************************************************/
19988
19989 /* Value is the internal representation of the specified cursor type
19990 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19991 of the bar cursor. */
19992
19993 static enum text_cursor_kinds
19994 get_specified_cursor_type (arg, width)
19995 Lisp_Object arg;
19996 int *width;
19997 {
19998 enum text_cursor_kinds type;
19999
20000 if (NILP (arg))
20001 return NO_CURSOR;
20002
20003 if (EQ (arg, Qbox))
20004 return FILLED_BOX_CURSOR;
20005
20006 if (EQ (arg, Qhollow))
20007 return HOLLOW_BOX_CURSOR;
20008
20009 if (EQ (arg, Qbar))
20010 {
20011 *width = 2;
20012 return BAR_CURSOR;
20013 }
20014
20015 if (CONSP (arg)
20016 && EQ (XCAR (arg), Qbar)
20017 && INTEGERP (XCDR (arg))
20018 && XINT (XCDR (arg)) >= 0)
20019 {
20020 *width = XINT (XCDR (arg));
20021 return BAR_CURSOR;
20022 }
20023
20024 if (EQ (arg, Qhbar))
20025 {
20026 *width = 2;
20027 return HBAR_CURSOR;
20028 }
20029
20030 if (CONSP (arg)
20031 && EQ (XCAR (arg), Qhbar)
20032 && INTEGERP (XCDR (arg))
20033 && XINT (XCDR (arg)) >= 0)
20034 {
20035 *width = XINT (XCDR (arg));
20036 return HBAR_CURSOR;
20037 }
20038
20039 /* Treat anything unknown as "hollow box cursor".
20040 It was bad to signal an error; people have trouble fixing
20041 .Xdefaults with Emacs, when it has something bad in it. */
20042 type = HOLLOW_BOX_CURSOR;
20043
20044 return type;
20045 }
20046
20047 /* Set the default cursor types for specified frame. */
20048 void
20049 set_frame_cursor_types (f, arg)
20050 struct frame *f;
20051 Lisp_Object arg;
20052 {
20053 int width;
20054 Lisp_Object tem;
20055
20056 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20057 FRAME_CURSOR_WIDTH (f) = width;
20058
20059 /* By default, set up the blink-off state depending on the on-state. */
20060
20061 tem = Fassoc (arg, Vblink_cursor_alist);
20062 if (!NILP (tem))
20063 {
20064 FRAME_BLINK_OFF_CURSOR (f)
20065 = get_specified_cursor_type (XCDR (tem), &width);
20066 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20067 }
20068 else
20069 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20070 }
20071
20072
20073 /* Return the cursor we want to be displayed in window W. Return
20074 width of bar/hbar cursor through WIDTH arg. Return with
20075 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20076 (i.e. if the `system caret' should track this cursor).
20077
20078 In a mini-buffer window, we want the cursor only to appear if we
20079 are reading input from this window. For the selected window, we
20080 want the cursor type given by the frame parameter or buffer local
20081 setting of cursor-type. If explicitly marked off, draw no cursor.
20082 In all other cases, we want a hollow box cursor. */
20083
20084 static enum text_cursor_kinds
20085 get_window_cursor_type (w, glyph, width, active_cursor)
20086 struct window *w;
20087 struct glyph *glyph;
20088 int *width;
20089 int *active_cursor;
20090 {
20091 struct frame *f = XFRAME (w->frame);
20092 struct buffer *b = XBUFFER (w->buffer);
20093 int cursor_type = DEFAULT_CURSOR;
20094 Lisp_Object alt_cursor;
20095 int non_selected = 0;
20096
20097 *active_cursor = 1;
20098
20099 /* Echo area */
20100 if (cursor_in_echo_area
20101 && FRAME_HAS_MINIBUF_P (f)
20102 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20103 {
20104 if (w == XWINDOW (echo_area_window))
20105 {
20106 *width = FRAME_CURSOR_WIDTH (f);
20107 return FRAME_DESIRED_CURSOR (f);
20108 }
20109
20110 *active_cursor = 0;
20111 non_selected = 1;
20112 }
20113
20114 /* Nonselected window or nonselected frame. */
20115 else if (w != XWINDOW (f->selected_window)
20116 #ifdef HAVE_WINDOW_SYSTEM
20117 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20118 #endif
20119 )
20120 {
20121 *active_cursor = 0;
20122
20123 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20124 return NO_CURSOR;
20125
20126 non_selected = 1;
20127 }
20128
20129 /* Never display a cursor in a window in which cursor-type is nil. */
20130 if (NILP (b->cursor_type))
20131 return NO_CURSOR;
20132
20133 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20134 if (non_selected)
20135 {
20136 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
20137 return get_specified_cursor_type (alt_cursor, width);
20138 }
20139
20140 /* Get the normal cursor type for this window. */
20141 if (EQ (b->cursor_type, Qt))
20142 {
20143 cursor_type = FRAME_DESIRED_CURSOR (f);
20144 *width = FRAME_CURSOR_WIDTH (f);
20145 }
20146 else
20147 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20148
20149 /* Use normal cursor if not blinked off. */
20150 if (!w->cursor_off_p)
20151 {
20152 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20153 if (cursor_type == FILLED_BOX_CURSOR)
20154 cursor_type = HOLLOW_BOX_CURSOR;
20155 }
20156 return cursor_type;
20157 }
20158
20159 /* Cursor is blinked off, so determine how to "toggle" it. */
20160
20161 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20162 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20163 return get_specified_cursor_type (XCDR (alt_cursor), width);
20164
20165 /* Then see if frame has specified a specific blink off cursor type. */
20166 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20167 {
20168 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20169 return FRAME_BLINK_OFF_CURSOR (f);
20170 }
20171
20172 #if 0
20173 /* Some people liked having a permanently visible blinking cursor,
20174 while others had very strong opinions against it. So it was
20175 decided to remove it. KFS 2003-09-03 */
20176
20177 /* Finally perform built-in cursor blinking:
20178 filled box <-> hollow box
20179 wide [h]bar <-> narrow [h]bar
20180 narrow [h]bar <-> no cursor
20181 other type <-> no cursor */
20182
20183 if (cursor_type == FILLED_BOX_CURSOR)
20184 return HOLLOW_BOX_CURSOR;
20185
20186 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20187 {
20188 *width = 1;
20189 return cursor_type;
20190 }
20191 #endif
20192
20193 return NO_CURSOR;
20194 }
20195
20196
20197 #ifdef HAVE_WINDOW_SYSTEM
20198
20199 /* Notice when the text cursor of window W has been completely
20200 overwritten by a drawing operation that outputs glyphs in AREA
20201 starting at X0 and ending at X1 in the line starting at Y0 and
20202 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20203 the rest of the line after X0 has been written. Y coordinates
20204 are window-relative. */
20205
20206 static void
20207 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20208 struct window *w;
20209 enum glyph_row_area area;
20210 int x0, y0, x1, y1;
20211 {
20212 int cx0, cx1, cy0, cy1;
20213 struct glyph_row *row;
20214
20215 if (!w->phys_cursor_on_p)
20216 return;
20217 if (area != TEXT_AREA)
20218 return;
20219
20220 if (w->phys_cursor.vpos < 0
20221 || w->phys_cursor.vpos >= w->current_matrix->nrows
20222 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20223 !(row->enabled_p && row->displays_text_p)))
20224 return;
20225
20226 if (row->cursor_in_fringe_p)
20227 {
20228 row->cursor_in_fringe_p = 0;
20229 draw_fringe_bitmap (w, row, 0);
20230 w->phys_cursor_on_p = 0;
20231 return;
20232 }
20233
20234 cx0 = w->phys_cursor.x;
20235 cx1 = cx0 + w->phys_cursor_width;
20236 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20237 return;
20238
20239 /* The cursor image will be completely removed from the
20240 screen if the output area intersects the cursor area in
20241 y-direction. When we draw in [y0 y1[, and some part of
20242 the cursor is at y < y0, that part must have been drawn
20243 before. When scrolling, the cursor is erased before
20244 actually scrolling, so we don't come here. When not
20245 scrolling, the rows above the old cursor row must have
20246 changed, and in this case these rows must have written
20247 over the cursor image.
20248
20249 Likewise if part of the cursor is below y1, with the
20250 exception of the cursor being in the first blank row at
20251 the buffer and window end because update_text_area
20252 doesn't draw that row. (Except when it does, but
20253 that's handled in update_text_area.) */
20254
20255 cy0 = w->phys_cursor.y;
20256 cy1 = cy0 + w->phys_cursor_height;
20257 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20258 return;
20259
20260 w->phys_cursor_on_p = 0;
20261 }
20262
20263 #endif /* HAVE_WINDOW_SYSTEM */
20264
20265 \f
20266 /************************************************************************
20267 Mouse Face
20268 ************************************************************************/
20269
20270 #ifdef HAVE_WINDOW_SYSTEM
20271
20272 /* EXPORT for RIF:
20273 Fix the display of area AREA of overlapping row ROW in window W. */
20274
20275 void
20276 x_fix_overlapping_area (w, row, area)
20277 struct window *w;
20278 struct glyph_row *row;
20279 enum glyph_row_area area;
20280 {
20281 int i, x;
20282
20283 BLOCK_INPUT;
20284
20285 x = 0;
20286 for (i = 0; i < row->used[area];)
20287 {
20288 if (row->glyphs[area][i].overlaps_vertically_p)
20289 {
20290 int start = i, start_x = x;
20291
20292 do
20293 {
20294 x += row->glyphs[area][i].pixel_width;
20295 ++i;
20296 }
20297 while (i < row->used[area]
20298 && row->glyphs[area][i].overlaps_vertically_p);
20299
20300 draw_glyphs (w, start_x, row, area,
20301 start, i,
20302 DRAW_NORMAL_TEXT, 1);
20303 }
20304 else
20305 {
20306 x += row->glyphs[area][i].pixel_width;
20307 ++i;
20308 }
20309 }
20310
20311 UNBLOCK_INPUT;
20312 }
20313
20314
20315 /* EXPORT:
20316 Draw the cursor glyph of window W in glyph row ROW. See the
20317 comment of draw_glyphs for the meaning of HL. */
20318
20319 void
20320 draw_phys_cursor_glyph (w, row, hl)
20321 struct window *w;
20322 struct glyph_row *row;
20323 enum draw_glyphs_face hl;
20324 {
20325 /* If cursor hpos is out of bounds, don't draw garbage. This can
20326 happen in mini-buffer windows when switching between echo area
20327 glyphs and mini-buffer. */
20328 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20329 {
20330 int on_p = w->phys_cursor_on_p;
20331 int x1;
20332 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20333 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20334 hl, 0);
20335 w->phys_cursor_on_p = on_p;
20336
20337 if (hl == DRAW_CURSOR)
20338 w->phys_cursor_width = x1 - w->phys_cursor.x;
20339 /* When we erase the cursor, and ROW is overlapped by other
20340 rows, make sure that these overlapping parts of other rows
20341 are redrawn. */
20342 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20343 {
20344 if (row > w->current_matrix->rows
20345 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20346 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20347
20348 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20349 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20350 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20351 }
20352 }
20353 }
20354
20355
20356 /* EXPORT:
20357 Erase the image of a cursor of window W from the screen. */
20358
20359 void
20360 erase_phys_cursor (w)
20361 struct window *w;
20362 {
20363 struct frame *f = XFRAME (w->frame);
20364 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20365 int hpos = w->phys_cursor.hpos;
20366 int vpos = w->phys_cursor.vpos;
20367 int mouse_face_here_p = 0;
20368 struct glyph_matrix *active_glyphs = w->current_matrix;
20369 struct glyph_row *cursor_row;
20370 struct glyph *cursor_glyph;
20371 enum draw_glyphs_face hl;
20372
20373 /* No cursor displayed or row invalidated => nothing to do on the
20374 screen. */
20375 if (w->phys_cursor_type == NO_CURSOR)
20376 goto mark_cursor_off;
20377
20378 /* VPOS >= active_glyphs->nrows means that window has been resized.
20379 Don't bother to erase the cursor. */
20380 if (vpos >= active_glyphs->nrows)
20381 goto mark_cursor_off;
20382
20383 /* If row containing cursor is marked invalid, there is nothing we
20384 can do. */
20385 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20386 if (!cursor_row->enabled_p)
20387 goto mark_cursor_off;
20388
20389 /* If line spacing is > 0, old cursor may only be partially visible in
20390 window after split-window. So adjust visible height. */
20391 cursor_row->visible_height = min (cursor_row->visible_height,
20392 window_text_bottom_y (w) - cursor_row->y);
20393
20394 /* If row is completely invisible, don't attempt to delete a cursor which
20395 isn't there. This can happen if cursor is at top of a window, and
20396 we switch to a buffer with a header line in that window. */
20397 if (cursor_row->visible_height <= 0)
20398 goto mark_cursor_off;
20399
20400 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20401 if (cursor_row->cursor_in_fringe_p)
20402 {
20403 cursor_row->cursor_in_fringe_p = 0;
20404 draw_fringe_bitmap (w, cursor_row, 0);
20405 goto mark_cursor_off;
20406 }
20407
20408 /* This can happen when the new row is shorter than the old one.
20409 In this case, either draw_glyphs or clear_end_of_line
20410 should have cleared the cursor. Note that we wouldn't be
20411 able to erase the cursor in this case because we don't have a
20412 cursor glyph at hand. */
20413 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20414 goto mark_cursor_off;
20415
20416 /* If the cursor is in the mouse face area, redisplay that when
20417 we clear the cursor. */
20418 if (! NILP (dpyinfo->mouse_face_window)
20419 && w == XWINDOW (dpyinfo->mouse_face_window)
20420 && (vpos > dpyinfo->mouse_face_beg_row
20421 || (vpos == dpyinfo->mouse_face_beg_row
20422 && hpos >= dpyinfo->mouse_face_beg_col))
20423 && (vpos < dpyinfo->mouse_face_end_row
20424 || (vpos == dpyinfo->mouse_face_end_row
20425 && hpos < dpyinfo->mouse_face_end_col))
20426 /* Don't redraw the cursor's spot in mouse face if it is at the
20427 end of a line (on a newline). The cursor appears there, but
20428 mouse highlighting does not. */
20429 && cursor_row->used[TEXT_AREA] > hpos)
20430 mouse_face_here_p = 1;
20431
20432 /* Maybe clear the display under the cursor. */
20433 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20434 {
20435 int x, y;
20436 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20437 int width;
20438
20439 cursor_glyph = get_phys_cursor_glyph (w);
20440 if (cursor_glyph == NULL)
20441 goto mark_cursor_off;
20442
20443 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20444 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20445 width = min (cursor_glyph->pixel_width,
20446 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20447
20448 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20449 }
20450
20451 /* Erase the cursor by redrawing the character underneath it. */
20452 if (mouse_face_here_p)
20453 hl = DRAW_MOUSE_FACE;
20454 else
20455 hl = DRAW_NORMAL_TEXT;
20456 draw_phys_cursor_glyph (w, cursor_row, hl);
20457
20458 mark_cursor_off:
20459 w->phys_cursor_on_p = 0;
20460 w->phys_cursor_type = NO_CURSOR;
20461 }
20462
20463
20464 /* EXPORT:
20465 Display or clear cursor of window W. If ON is zero, clear the
20466 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20467 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20468
20469 void
20470 display_and_set_cursor (w, on, hpos, vpos, x, y)
20471 struct window *w;
20472 int on, hpos, vpos, x, y;
20473 {
20474 struct frame *f = XFRAME (w->frame);
20475 int new_cursor_type;
20476 int new_cursor_width;
20477 int active_cursor;
20478 struct glyph_row *glyph_row;
20479 struct glyph *glyph;
20480
20481 /* This is pointless on invisible frames, and dangerous on garbaged
20482 windows and frames; in the latter case, the frame or window may
20483 be in the midst of changing its size, and x and y may be off the
20484 window. */
20485 if (! FRAME_VISIBLE_P (f)
20486 || FRAME_GARBAGED_P (f)
20487 || vpos >= w->current_matrix->nrows
20488 || hpos >= w->current_matrix->matrix_w)
20489 return;
20490
20491 /* If cursor is off and we want it off, return quickly. */
20492 if (!on && !w->phys_cursor_on_p)
20493 return;
20494
20495 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20496 /* If cursor row is not enabled, we don't really know where to
20497 display the cursor. */
20498 if (!glyph_row->enabled_p)
20499 {
20500 w->phys_cursor_on_p = 0;
20501 return;
20502 }
20503
20504 glyph = NULL;
20505 if (!glyph_row->exact_window_width_line_p
20506 || hpos < glyph_row->used[TEXT_AREA])
20507 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20508
20509 xassert (interrupt_input_blocked);
20510
20511 /* Set new_cursor_type to the cursor we want to be displayed. */
20512 new_cursor_type = get_window_cursor_type (w, glyph,
20513 &new_cursor_width, &active_cursor);
20514
20515 /* If cursor is currently being shown and we don't want it to be or
20516 it is in the wrong place, or the cursor type is not what we want,
20517 erase it. */
20518 if (w->phys_cursor_on_p
20519 && (!on
20520 || w->phys_cursor.x != x
20521 || w->phys_cursor.y != y
20522 || new_cursor_type != w->phys_cursor_type
20523 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20524 && new_cursor_width != w->phys_cursor_width)))
20525 erase_phys_cursor (w);
20526
20527 /* Don't check phys_cursor_on_p here because that flag is only set
20528 to zero in some cases where we know that the cursor has been
20529 completely erased, to avoid the extra work of erasing the cursor
20530 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20531 still not be visible, or it has only been partly erased. */
20532 if (on)
20533 {
20534 w->phys_cursor_ascent = glyph_row->ascent;
20535 w->phys_cursor_height = glyph_row->height;
20536
20537 /* Set phys_cursor_.* before x_draw_.* is called because some
20538 of them may need the information. */
20539 w->phys_cursor.x = x;
20540 w->phys_cursor.y = glyph_row->y;
20541 w->phys_cursor.hpos = hpos;
20542 w->phys_cursor.vpos = vpos;
20543 }
20544
20545 rif->draw_window_cursor (w, glyph_row, x, y,
20546 new_cursor_type, new_cursor_width,
20547 on, active_cursor);
20548 }
20549
20550
20551 /* Switch the display of W's cursor on or off, according to the value
20552 of ON. */
20553
20554 static void
20555 update_window_cursor (w, on)
20556 struct window *w;
20557 int on;
20558 {
20559 /* Don't update cursor in windows whose frame is in the process
20560 of being deleted. */
20561 if (w->current_matrix)
20562 {
20563 BLOCK_INPUT;
20564 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20565 w->phys_cursor.x, w->phys_cursor.y);
20566 UNBLOCK_INPUT;
20567 }
20568 }
20569
20570
20571 /* Call update_window_cursor with parameter ON_P on all leaf windows
20572 in the window tree rooted at W. */
20573
20574 static void
20575 update_cursor_in_window_tree (w, on_p)
20576 struct window *w;
20577 int on_p;
20578 {
20579 while (w)
20580 {
20581 if (!NILP (w->hchild))
20582 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20583 else if (!NILP (w->vchild))
20584 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20585 else
20586 update_window_cursor (w, on_p);
20587
20588 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20589 }
20590 }
20591
20592
20593 /* EXPORT:
20594 Display the cursor on window W, or clear it, according to ON_P.
20595 Don't change the cursor's position. */
20596
20597 void
20598 x_update_cursor (f, on_p)
20599 struct frame *f;
20600 int on_p;
20601 {
20602 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20603 }
20604
20605
20606 /* EXPORT:
20607 Clear the cursor of window W to background color, and mark the
20608 cursor as not shown. This is used when the text where the cursor
20609 is is about to be rewritten. */
20610
20611 void
20612 x_clear_cursor (w)
20613 struct window *w;
20614 {
20615 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20616 update_window_cursor (w, 0);
20617 }
20618
20619
20620 /* EXPORT:
20621 Display the active region described by mouse_face_* according to DRAW. */
20622
20623 void
20624 show_mouse_face (dpyinfo, draw)
20625 Display_Info *dpyinfo;
20626 enum draw_glyphs_face draw;
20627 {
20628 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20629 struct frame *f = XFRAME (WINDOW_FRAME (w));
20630
20631 if (/* If window is in the process of being destroyed, don't bother
20632 to do anything. */
20633 w->current_matrix != NULL
20634 /* Don't update mouse highlight if hidden */
20635 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20636 /* Recognize when we are called to operate on rows that don't exist
20637 anymore. This can happen when a window is split. */
20638 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20639 {
20640 int phys_cursor_on_p = w->phys_cursor_on_p;
20641 struct glyph_row *row, *first, *last;
20642
20643 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20644 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20645
20646 for (row = first; row <= last && row->enabled_p; ++row)
20647 {
20648 int start_hpos, end_hpos, start_x;
20649
20650 /* For all but the first row, the highlight starts at column 0. */
20651 if (row == first)
20652 {
20653 start_hpos = dpyinfo->mouse_face_beg_col;
20654 start_x = dpyinfo->mouse_face_beg_x;
20655 }
20656 else
20657 {
20658 start_hpos = 0;
20659 start_x = 0;
20660 }
20661
20662 if (row == last)
20663 end_hpos = dpyinfo->mouse_face_end_col;
20664 else
20665 end_hpos = row->used[TEXT_AREA];
20666
20667 if (end_hpos > start_hpos)
20668 {
20669 draw_glyphs (w, start_x, row, TEXT_AREA,
20670 start_hpos, end_hpos,
20671 draw, 0);
20672
20673 row->mouse_face_p
20674 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20675 }
20676 }
20677
20678 /* When we've written over the cursor, arrange for it to
20679 be displayed again. */
20680 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20681 {
20682 BLOCK_INPUT;
20683 display_and_set_cursor (w, 1,
20684 w->phys_cursor.hpos, w->phys_cursor.vpos,
20685 w->phys_cursor.x, w->phys_cursor.y);
20686 UNBLOCK_INPUT;
20687 }
20688 }
20689
20690 /* Change the mouse cursor. */
20691 if (draw == DRAW_NORMAL_TEXT)
20692 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20693 else if (draw == DRAW_MOUSE_FACE)
20694 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20695 else
20696 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20697 }
20698
20699 /* EXPORT:
20700 Clear out the mouse-highlighted active region.
20701 Redraw it un-highlighted first. Value is non-zero if mouse
20702 face was actually drawn unhighlighted. */
20703
20704 int
20705 clear_mouse_face (dpyinfo)
20706 Display_Info *dpyinfo;
20707 {
20708 int cleared = 0;
20709
20710 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20711 {
20712 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20713 cleared = 1;
20714 }
20715
20716 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20717 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20718 dpyinfo->mouse_face_window = Qnil;
20719 dpyinfo->mouse_face_overlay = Qnil;
20720 return cleared;
20721 }
20722
20723
20724 /* EXPORT:
20725 Non-zero if physical cursor of window W is within mouse face. */
20726
20727 int
20728 cursor_in_mouse_face_p (w)
20729 struct window *w;
20730 {
20731 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20732 int in_mouse_face = 0;
20733
20734 if (WINDOWP (dpyinfo->mouse_face_window)
20735 && XWINDOW (dpyinfo->mouse_face_window) == w)
20736 {
20737 int hpos = w->phys_cursor.hpos;
20738 int vpos = w->phys_cursor.vpos;
20739
20740 if (vpos >= dpyinfo->mouse_face_beg_row
20741 && vpos <= dpyinfo->mouse_face_end_row
20742 && (vpos > dpyinfo->mouse_face_beg_row
20743 || hpos >= dpyinfo->mouse_face_beg_col)
20744 && (vpos < dpyinfo->mouse_face_end_row
20745 || hpos < dpyinfo->mouse_face_end_col
20746 || dpyinfo->mouse_face_past_end))
20747 in_mouse_face = 1;
20748 }
20749
20750 return in_mouse_face;
20751 }
20752
20753
20754
20755 \f
20756 /* Find the glyph matrix position of buffer position CHARPOS in window
20757 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20758 current glyphs must be up to date. If CHARPOS is above window
20759 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20760 of last line in W. In the row containing CHARPOS, stop before glyphs
20761 having STOP as object. */
20762
20763 #if 1 /* This is a version of fast_find_position that's more correct
20764 in the presence of hscrolling, for example. I didn't install
20765 it right away because the problem fixed is minor, it failed
20766 in 20.x as well, and I think it's too risky to install
20767 so near the release of 21.1. 2001-09-25 gerd. */
20768
20769 static int
20770 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20771 struct window *w;
20772 int charpos;
20773 int *hpos, *vpos, *x, *y;
20774 Lisp_Object stop;
20775 {
20776 struct glyph_row *row, *first;
20777 struct glyph *glyph, *end;
20778 int past_end = 0;
20779
20780 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20781 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20782 {
20783 *x = first->x;
20784 *y = first->y;
20785 *hpos = 0;
20786 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20787 return 1;
20788 }
20789
20790 row = row_containing_pos (w, charpos, first, NULL, 0);
20791 if (row == NULL)
20792 {
20793 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20794 past_end = 1;
20795 }
20796
20797 /* If whole rows or last part of a row came from a display overlay,
20798 row_containing_pos will skip over such rows because their end pos
20799 equals the start pos of the overlay or interval.
20800
20801 Move back if we have a STOP object and previous row's
20802 end glyph came from STOP. */
20803 if (!NILP (stop))
20804 {
20805 struct glyph_row *prev;
20806 while ((prev = row - 1, prev >= first)
20807 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20808 && prev->used[TEXT_AREA] > 0)
20809 {
20810 struct glyph *beg = prev->glyphs[TEXT_AREA];
20811 glyph = beg + prev->used[TEXT_AREA];
20812 while (--glyph >= beg
20813 && INTEGERP (glyph->object));
20814 if (glyph < beg
20815 || !EQ (stop, glyph->object))
20816 break;
20817 row = prev;
20818 }
20819 }
20820
20821 *x = row->x;
20822 *y = row->y;
20823 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20824
20825 glyph = row->glyphs[TEXT_AREA];
20826 end = glyph + row->used[TEXT_AREA];
20827
20828 /* Skip over glyphs not having an object at the start of the row.
20829 These are special glyphs like truncation marks on terminal
20830 frames. */
20831 if (row->displays_text_p)
20832 while (glyph < end
20833 && INTEGERP (glyph->object)
20834 && !EQ (stop, glyph->object)
20835 && glyph->charpos < 0)
20836 {
20837 *x += glyph->pixel_width;
20838 ++glyph;
20839 }
20840
20841 while (glyph < end
20842 && !INTEGERP (glyph->object)
20843 && !EQ (stop, glyph->object)
20844 && (!BUFFERP (glyph->object)
20845 || glyph->charpos < charpos))
20846 {
20847 *x += glyph->pixel_width;
20848 ++glyph;
20849 }
20850
20851 *hpos = glyph - row->glyphs[TEXT_AREA];
20852 return !past_end;
20853 }
20854
20855 #else /* not 1 */
20856
20857 static int
20858 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20859 struct window *w;
20860 int pos;
20861 int *hpos, *vpos, *x, *y;
20862 Lisp_Object stop;
20863 {
20864 int i;
20865 int lastcol;
20866 int maybe_next_line_p = 0;
20867 int line_start_position;
20868 int yb = window_text_bottom_y (w);
20869 struct glyph_row *row, *best_row;
20870 int row_vpos, best_row_vpos;
20871 int current_x;
20872
20873 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20874 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20875
20876 while (row->y < yb)
20877 {
20878 if (row->used[TEXT_AREA])
20879 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20880 else
20881 line_start_position = 0;
20882
20883 if (line_start_position > pos)
20884 break;
20885 /* If the position sought is the end of the buffer,
20886 don't include the blank lines at the bottom of the window. */
20887 else if (line_start_position == pos
20888 && pos == BUF_ZV (XBUFFER (w->buffer)))
20889 {
20890 maybe_next_line_p = 1;
20891 break;
20892 }
20893 else if (line_start_position > 0)
20894 {
20895 best_row = row;
20896 best_row_vpos = row_vpos;
20897 }
20898
20899 if (row->y + row->height >= yb)
20900 break;
20901
20902 ++row;
20903 ++row_vpos;
20904 }
20905
20906 /* Find the right column within BEST_ROW. */
20907 lastcol = 0;
20908 current_x = best_row->x;
20909 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20910 {
20911 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20912 int charpos = glyph->charpos;
20913
20914 if (BUFFERP (glyph->object))
20915 {
20916 if (charpos == pos)
20917 {
20918 *hpos = i;
20919 *vpos = best_row_vpos;
20920 *x = current_x;
20921 *y = best_row->y;
20922 return 1;
20923 }
20924 else if (charpos > pos)
20925 break;
20926 }
20927 else if (EQ (glyph->object, stop))
20928 break;
20929
20930 if (charpos > 0)
20931 lastcol = i;
20932 current_x += glyph->pixel_width;
20933 }
20934
20935 /* If we're looking for the end of the buffer,
20936 and we didn't find it in the line we scanned,
20937 use the start of the following line. */
20938 if (maybe_next_line_p)
20939 {
20940 ++best_row;
20941 ++best_row_vpos;
20942 lastcol = 0;
20943 current_x = best_row->x;
20944 }
20945
20946 *vpos = best_row_vpos;
20947 *hpos = lastcol + 1;
20948 *x = current_x;
20949 *y = best_row->y;
20950 return 0;
20951 }
20952
20953 #endif /* not 1 */
20954
20955
20956 /* Find the position of the glyph for position POS in OBJECT in
20957 window W's current matrix, and return in *X, *Y the pixel
20958 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20959
20960 RIGHT_P non-zero means return the position of the right edge of the
20961 glyph, RIGHT_P zero means return the left edge position.
20962
20963 If no glyph for POS exists in the matrix, return the position of
20964 the glyph with the next smaller position that is in the matrix, if
20965 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20966 exists in the matrix, return the position of the glyph with the
20967 next larger position in OBJECT.
20968
20969 Value is non-zero if a glyph was found. */
20970
20971 static int
20972 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20973 struct window *w;
20974 int pos;
20975 Lisp_Object object;
20976 int *hpos, *vpos, *x, *y;
20977 int right_p;
20978 {
20979 int yb = window_text_bottom_y (w);
20980 struct glyph_row *r;
20981 struct glyph *best_glyph = NULL;
20982 struct glyph_row *best_row = NULL;
20983 int best_x = 0;
20984
20985 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20986 r->enabled_p && r->y < yb;
20987 ++r)
20988 {
20989 struct glyph *g = r->glyphs[TEXT_AREA];
20990 struct glyph *e = g + r->used[TEXT_AREA];
20991 int gx;
20992
20993 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20994 if (EQ (g->object, object))
20995 {
20996 if (g->charpos == pos)
20997 {
20998 best_glyph = g;
20999 best_x = gx;
21000 best_row = r;
21001 goto found;
21002 }
21003 else if (best_glyph == NULL
21004 || ((abs (g->charpos - pos)
21005 < abs (best_glyph->charpos - pos))
21006 && (right_p
21007 ? g->charpos < pos
21008 : g->charpos > pos)))
21009 {
21010 best_glyph = g;
21011 best_x = gx;
21012 best_row = r;
21013 }
21014 }
21015 }
21016
21017 found:
21018
21019 if (best_glyph)
21020 {
21021 *x = best_x;
21022 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21023
21024 if (right_p)
21025 {
21026 *x += best_glyph->pixel_width;
21027 ++*hpos;
21028 }
21029
21030 *y = best_row->y;
21031 *vpos = best_row - w->current_matrix->rows;
21032 }
21033
21034 return best_glyph != NULL;
21035 }
21036
21037
21038 /* See if position X, Y is within a hot-spot of an image. */
21039
21040 static int
21041 on_hot_spot_p (hot_spot, x, y)
21042 Lisp_Object hot_spot;
21043 int x, y;
21044 {
21045 if (!CONSP (hot_spot))
21046 return 0;
21047
21048 if (EQ (XCAR (hot_spot), Qrect))
21049 {
21050 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21051 Lisp_Object rect = XCDR (hot_spot);
21052 Lisp_Object tem;
21053 if (!CONSP (rect))
21054 return 0;
21055 if (!CONSP (XCAR (rect)))
21056 return 0;
21057 if (!CONSP (XCDR (rect)))
21058 return 0;
21059 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21060 return 0;
21061 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21062 return 0;
21063 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21064 return 0;
21065 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21066 return 0;
21067 return 1;
21068 }
21069 else if (EQ (XCAR (hot_spot), Qcircle))
21070 {
21071 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21072 Lisp_Object circ = XCDR (hot_spot);
21073 Lisp_Object lr, lx0, ly0;
21074 if (CONSP (circ)
21075 && CONSP (XCAR (circ))
21076 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21077 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21078 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21079 {
21080 double r = XFLOATINT (lr);
21081 double dx = XINT (lx0) - x;
21082 double dy = XINT (ly0) - y;
21083 return (dx * dx + dy * dy <= r * r);
21084 }
21085 }
21086 else if (EQ (XCAR (hot_spot), Qpoly))
21087 {
21088 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21089 if (VECTORP (XCDR (hot_spot)))
21090 {
21091 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21092 Lisp_Object *poly = v->contents;
21093 int n = v->size;
21094 int i;
21095 int inside = 0;
21096 Lisp_Object lx, ly;
21097 int x0, y0;
21098
21099 /* Need an even number of coordinates, and at least 3 edges. */
21100 if (n < 6 || n & 1)
21101 return 0;
21102
21103 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21104 If count is odd, we are inside polygon. Pixels on edges
21105 may or may not be included depending on actual geometry of the
21106 polygon. */
21107 if ((lx = poly[n-2], !INTEGERP (lx))
21108 || (ly = poly[n-1], !INTEGERP (lx)))
21109 return 0;
21110 x0 = XINT (lx), y0 = XINT (ly);
21111 for (i = 0; i < n; i += 2)
21112 {
21113 int x1 = x0, y1 = y0;
21114 if ((lx = poly[i], !INTEGERP (lx))
21115 || (ly = poly[i+1], !INTEGERP (ly)))
21116 return 0;
21117 x0 = XINT (lx), y0 = XINT (ly);
21118
21119 /* Does this segment cross the X line? */
21120 if (x0 >= x)
21121 {
21122 if (x1 >= x)
21123 continue;
21124 }
21125 else if (x1 < x)
21126 continue;
21127 if (y > y0 && y > y1)
21128 continue;
21129 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21130 inside = !inside;
21131 }
21132 return inside;
21133 }
21134 }
21135 /* If we don't understand the format, pretend we're not in the hot-spot. */
21136 return 0;
21137 }
21138
21139 Lisp_Object
21140 find_hot_spot (map, x, y)
21141 Lisp_Object map;
21142 int x, y;
21143 {
21144 while (CONSP (map))
21145 {
21146 if (CONSP (XCAR (map))
21147 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21148 return XCAR (map);
21149 map = XCDR (map);
21150 }
21151
21152 return Qnil;
21153 }
21154
21155 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21156 3, 3, 0,
21157 doc: /* Lookup in image map MAP coordinates X and Y.
21158 An image map is an alist where each element has the format (AREA ID PLIST).
21159 An AREA is specified as either a rectangle, a circle, or a polygon:
21160 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21161 pixel coordinates of the upper left and bottom right corners.
21162 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21163 and the radius of the circle; r may be a float or integer.
21164 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21165 vector describes one corner in the polygon.
21166 Returns the alist element for the first matching AREA in MAP. */)
21167 (map, x, y)
21168 Lisp_Object map;
21169 Lisp_Object x, y;
21170 {
21171 if (NILP (map))
21172 return Qnil;
21173
21174 CHECK_NUMBER (x);
21175 CHECK_NUMBER (y);
21176
21177 return find_hot_spot (map, XINT (x), XINT (y));
21178 }
21179
21180
21181 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21182 static void
21183 define_frame_cursor1 (f, cursor, pointer)
21184 struct frame *f;
21185 Cursor cursor;
21186 Lisp_Object pointer;
21187 {
21188 /* Do not change cursor shape while dragging mouse. */
21189 if (!NILP (do_mouse_tracking))
21190 return;
21191
21192 if (!NILP (pointer))
21193 {
21194 if (EQ (pointer, Qarrow))
21195 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21196 else if (EQ (pointer, Qhand))
21197 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21198 else if (EQ (pointer, Qtext))
21199 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21200 else if (EQ (pointer, intern ("hdrag")))
21201 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21202 #ifdef HAVE_X_WINDOWS
21203 else if (EQ (pointer, intern ("vdrag")))
21204 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21205 #endif
21206 else if (EQ (pointer, intern ("hourglass")))
21207 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21208 else if (EQ (pointer, Qmodeline))
21209 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21210 else
21211 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21212 }
21213
21214 if (cursor != No_Cursor)
21215 rif->define_frame_cursor (f, cursor);
21216 }
21217
21218 /* Take proper action when mouse has moved to the mode or header line
21219 or marginal area AREA of window W, x-position X and y-position Y.
21220 X is relative to the start of the text display area of W, so the
21221 width of bitmap areas and scroll bars must be subtracted to get a
21222 position relative to the start of the mode line. */
21223
21224 static void
21225 note_mode_line_or_margin_highlight (w, x, y, area)
21226 struct window *w;
21227 int x, y;
21228 enum window_part area;
21229 {
21230 struct frame *f = XFRAME (w->frame);
21231 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21232 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21233 Lisp_Object pointer = Qnil;
21234 int charpos, dx, dy, width, height;
21235 Lisp_Object string, object = Qnil;
21236 Lisp_Object pos, help;
21237
21238 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21239 string = mode_line_string (w, area, &x, &y, &charpos,
21240 &object, &dx, &dy, &width, &height);
21241 else
21242 {
21243 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21244 string = marginal_area_string (w, area, &x, &y, &charpos,
21245 &object, &dx, &dy, &width, &height);
21246 }
21247
21248 help = Qnil;
21249
21250 if (IMAGEP (object))
21251 {
21252 Lisp_Object image_map, hotspot;
21253 if ((image_map = Fplist_get (XCDR (object), QCmap),
21254 !NILP (image_map))
21255 && (hotspot = find_hot_spot (image_map, dx, dy),
21256 CONSP (hotspot))
21257 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21258 {
21259 Lisp_Object area_id, plist;
21260
21261 area_id = XCAR (hotspot);
21262 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21263 If so, we could look for mouse-enter, mouse-leave
21264 properties in PLIST (and do something...). */
21265 hotspot = XCDR (hotspot);
21266 if (CONSP (hotspot)
21267 && (plist = XCAR (hotspot), CONSP (plist)))
21268 {
21269 pointer = Fplist_get (plist, Qpointer);
21270 if (NILP (pointer))
21271 pointer = Qhand;
21272 help = Fplist_get (plist, Qhelp_echo);
21273 if (!NILP (help))
21274 {
21275 help_echo_string = help;
21276 /* Is this correct? ++kfs */
21277 XSETWINDOW (help_echo_window, w);
21278 help_echo_object = w->buffer;
21279 help_echo_pos = charpos;
21280 }
21281 }
21282 }
21283 if (NILP (pointer))
21284 pointer = Fplist_get (XCDR (object), QCpointer);
21285 }
21286
21287 if (STRINGP (string))
21288 {
21289 pos = make_number (charpos);
21290 /* If we're on a string with `help-echo' text property, arrange
21291 for the help to be displayed. This is done by setting the
21292 global variable help_echo_string to the help string. */
21293 if (NILP (help))
21294 {
21295 help = Fget_text_property (pos, Qhelp_echo, string);
21296 if (!NILP (help))
21297 {
21298 help_echo_string = help;
21299 XSETWINDOW (help_echo_window, w);
21300 help_echo_object = string;
21301 help_echo_pos = charpos;
21302 }
21303 }
21304
21305 if (NILP (pointer))
21306 pointer = Fget_text_property (pos, Qpointer, string);
21307
21308 /* Change the mouse pointer according to what is under X/Y. */
21309 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21310 {
21311 Lisp_Object map;
21312 map = Fget_text_property (pos, Qlocal_map, string);
21313 if (!KEYMAPP (map))
21314 map = Fget_text_property (pos, Qkeymap, string);
21315 if (!KEYMAPP (map))
21316 cursor = dpyinfo->vertical_scroll_bar_cursor;
21317 }
21318 }
21319
21320 define_frame_cursor1 (f, cursor, pointer);
21321 }
21322
21323
21324 /* EXPORT:
21325 Take proper action when the mouse has moved to position X, Y on
21326 frame F as regards highlighting characters that have mouse-face
21327 properties. Also de-highlighting chars where the mouse was before.
21328 X and Y can be negative or out of range. */
21329
21330 void
21331 note_mouse_highlight (f, x, y)
21332 struct frame *f;
21333 int x, y;
21334 {
21335 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21336 enum window_part part;
21337 Lisp_Object window;
21338 struct window *w;
21339 Cursor cursor = No_Cursor;
21340 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21341 struct buffer *b;
21342
21343 /* When a menu is active, don't highlight because this looks odd. */
21344 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21345 if (popup_activated ())
21346 return;
21347 #endif
21348
21349 if (NILP (Vmouse_highlight)
21350 || !f->glyphs_initialized_p)
21351 return;
21352
21353 dpyinfo->mouse_face_mouse_x = x;
21354 dpyinfo->mouse_face_mouse_y = y;
21355 dpyinfo->mouse_face_mouse_frame = f;
21356
21357 if (dpyinfo->mouse_face_defer)
21358 return;
21359
21360 if (gc_in_progress)
21361 {
21362 dpyinfo->mouse_face_deferred_gc = 1;
21363 return;
21364 }
21365
21366 /* Which window is that in? */
21367 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21368
21369 /* If we were displaying active text in another window, clear that.
21370 Also clear if we move out of text area in same window. */
21371 if (! EQ (window, dpyinfo->mouse_face_window)
21372 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21373 clear_mouse_face (dpyinfo);
21374
21375 /* Not on a window -> return. */
21376 if (!WINDOWP (window))
21377 return;
21378
21379 /* Reset help_echo_string. It will get recomputed below. */
21380 help_echo_string = Qnil;
21381
21382 /* Convert to window-relative pixel coordinates. */
21383 w = XWINDOW (window);
21384 frame_to_window_pixel_xy (w, &x, &y);
21385
21386 /* Handle tool-bar window differently since it doesn't display a
21387 buffer. */
21388 if (EQ (window, f->tool_bar_window))
21389 {
21390 note_tool_bar_highlight (f, x, y);
21391 return;
21392 }
21393
21394 /* Mouse is on the mode, header line or margin? */
21395 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21396 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21397 {
21398 note_mode_line_or_margin_highlight (w, x, y, part);
21399 return;
21400 }
21401
21402 if (part == ON_VERTICAL_BORDER)
21403 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21404 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21405 || part == ON_SCROLL_BAR)
21406 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21407 else
21408 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21409
21410 /* Are we in a window whose display is up to date?
21411 And verify the buffer's text has not changed. */
21412 b = XBUFFER (w->buffer);
21413 if (part == ON_TEXT
21414 && EQ (w->window_end_valid, w->buffer)
21415 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21416 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21417 {
21418 int hpos, vpos, pos, i, dx, dy, area;
21419 struct glyph *glyph;
21420 Lisp_Object object;
21421 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21422 Lisp_Object *overlay_vec = NULL;
21423 int noverlays;
21424 struct buffer *obuf;
21425 int obegv, ozv, same_region;
21426
21427 /* Find the glyph under X/Y. */
21428 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21429
21430 /* Look for :pointer property on image. */
21431 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21432 {
21433 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21434 if (img != NULL && IMAGEP (img->spec))
21435 {
21436 Lisp_Object image_map, hotspot;
21437 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21438 !NILP (image_map))
21439 && (hotspot = find_hot_spot (image_map,
21440 glyph->slice.x + dx,
21441 glyph->slice.y + dy),
21442 CONSP (hotspot))
21443 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21444 {
21445 Lisp_Object area_id, plist;
21446
21447 area_id = XCAR (hotspot);
21448 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21449 If so, we could look for mouse-enter, mouse-leave
21450 properties in PLIST (and do something...). */
21451 hotspot = XCDR (hotspot);
21452 if (CONSP (hotspot)
21453 && (plist = XCAR (hotspot), CONSP (plist)))
21454 {
21455 pointer = Fplist_get (plist, Qpointer);
21456 if (NILP (pointer))
21457 pointer = Qhand;
21458 help_echo_string = Fplist_get (plist, Qhelp_echo);
21459 if (!NILP (help_echo_string))
21460 {
21461 help_echo_window = window;
21462 help_echo_object = glyph->object;
21463 help_echo_pos = glyph->charpos;
21464 }
21465 }
21466 }
21467 if (NILP (pointer))
21468 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21469 }
21470 }
21471
21472 /* Clear mouse face if X/Y not over text. */
21473 if (glyph == NULL
21474 || area != TEXT_AREA
21475 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21476 {
21477 if (clear_mouse_face (dpyinfo))
21478 cursor = No_Cursor;
21479 if (NILP (pointer))
21480 {
21481 if (area != TEXT_AREA)
21482 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21483 else
21484 pointer = Vvoid_text_area_pointer;
21485 }
21486 goto set_cursor;
21487 }
21488
21489 pos = glyph->charpos;
21490 object = glyph->object;
21491 if (!STRINGP (object) && !BUFFERP (object))
21492 goto set_cursor;
21493
21494 /* If we get an out-of-range value, return now; avoid an error. */
21495 if (BUFFERP (object) && pos > BUF_Z (b))
21496 goto set_cursor;
21497
21498 /* Make the window's buffer temporarily current for
21499 overlays_at and compute_char_face. */
21500 obuf = current_buffer;
21501 current_buffer = b;
21502 obegv = BEGV;
21503 ozv = ZV;
21504 BEGV = BEG;
21505 ZV = Z;
21506
21507 /* Is this char mouse-active or does it have help-echo? */
21508 position = make_number (pos);
21509
21510 if (BUFFERP (object))
21511 {
21512 /* Put all the overlays we want in a vector in overlay_vec. */
21513 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21514 /* Sort overlays into increasing priority order. */
21515 noverlays = sort_overlays (overlay_vec, noverlays, w);
21516 }
21517 else
21518 noverlays = 0;
21519
21520 same_region = (EQ (window, dpyinfo->mouse_face_window)
21521 && vpos >= dpyinfo->mouse_face_beg_row
21522 && vpos <= dpyinfo->mouse_face_end_row
21523 && (vpos > dpyinfo->mouse_face_beg_row
21524 || hpos >= dpyinfo->mouse_face_beg_col)
21525 && (vpos < dpyinfo->mouse_face_end_row
21526 || hpos < dpyinfo->mouse_face_end_col
21527 || dpyinfo->mouse_face_past_end));
21528
21529 if (same_region)
21530 cursor = No_Cursor;
21531
21532 /* Check mouse-face highlighting. */
21533 if (! same_region
21534 /* If there exists an overlay with mouse-face overlapping
21535 the one we are currently highlighting, we have to
21536 check if we enter the overlapping overlay, and then
21537 highlight only that. */
21538 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21539 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21540 {
21541 /* Find the highest priority overlay that has a mouse-face
21542 property. */
21543 overlay = Qnil;
21544 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21545 {
21546 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21547 if (!NILP (mouse_face))
21548 overlay = overlay_vec[i];
21549 }
21550
21551 /* If we're actually highlighting the same overlay as
21552 before, there's no need to do that again. */
21553 if (!NILP (overlay)
21554 && EQ (overlay, dpyinfo->mouse_face_overlay))
21555 goto check_help_echo;
21556
21557 dpyinfo->mouse_face_overlay = overlay;
21558
21559 /* Clear the display of the old active region, if any. */
21560 if (clear_mouse_face (dpyinfo))
21561 cursor = No_Cursor;
21562
21563 /* If no overlay applies, get a text property. */
21564 if (NILP (overlay))
21565 mouse_face = Fget_text_property (position, Qmouse_face, object);
21566
21567 /* Handle the overlay case. */
21568 if (!NILP (overlay))
21569 {
21570 /* Find the range of text around this char that
21571 should be active. */
21572 Lisp_Object before, after;
21573 int ignore;
21574
21575 before = Foverlay_start (overlay);
21576 after = Foverlay_end (overlay);
21577 /* Record this as the current active region. */
21578 fast_find_position (w, XFASTINT (before),
21579 &dpyinfo->mouse_face_beg_col,
21580 &dpyinfo->mouse_face_beg_row,
21581 &dpyinfo->mouse_face_beg_x,
21582 &dpyinfo->mouse_face_beg_y, Qnil);
21583
21584 dpyinfo->mouse_face_past_end
21585 = !fast_find_position (w, XFASTINT (after),
21586 &dpyinfo->mouse_face_end_col,
21587 &dpyinfo->mouse_face_end_row,
21588 &dpyinfo->mouse_face_end_x,
21589 &dpyinfo->mouse_face_end_y, Qnil);
21590 dpyinfo->mouse_face_window = window;
21591
21592 dpyinfo->mouse_face_face_id
21593 = face_at_buffer_position (w, pos, 0, 0,
21594 &ignore, pos + 1,
21595 !dpyinfo->mouse_face_hidden);
21596
21597 /* Display it as active. */
21598 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21599 cursor = No_Cursor;
21600 }
21601 /* Handle the text property case. */
21602 else if (!NILP (mouse_face) && BUFFERP (object))
21603 {
21604 /* Find the range of text around this char that
21605 should be active. */
21606 Lisp_Object before, after, beginning, end;
21607 int ignore;
21608
21609 beginning = Fmarker_position (w->start);
21610 end = make_number (BUF_Z (XBUFFER (object))
21611 - XFASTINT (w->window_end_pos));
21612 before
21613 = Fprevious_single_property_change (make_number (pos + 1),
21614 Qmouse_face,
21615 object, beginning);
21616 after
21617 = Fnext_single_property_change (position, Qmouse_face,
21618 object, end);
21619
21620 /* Record this as the current active region. */
21621 fast_find_position (w, XFASTINT (before),
21622 &dpyinfo->mouse_face_beg_col,
21623 &dpyinfo->mouse_face_beg_row,
21624 &dpyinfo->mouse_face_beg_x,
21625 &dpyinfo->mouse_face_beg_y, Qnil);
21626 dpyinfo->mouse_face_past_end
21627 = !fast_find_position (w, XFASTINT (after),
21628 &dpyinfo->mouse_face_end_col,
21629 &dpyinfo->mouse_face_end_row,
21630 &dpyinfo->mouse_face_end_x,
21631 &dpyinfo->mouse_face_end_y, Qnil);
21632 dpyinfo->mouse_face_window = window;
21633
21634 if (BUFFERP (object))
21635 dpyinfo->mouse_face_face_id
21636 = face_at_buffer_position (w, pos, 0, 0,
21637 &ignore, pos + 1,
21638 !dpyinfo->mouse_face_hidden);
21639
21640 /* Display it as active. */
21641 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21642 cursor = No_Cursor;
21643 }
21644 else if (!NILP (mouse_face) && STRINGP (object))
21645 {
21646 Lisp_Object b, e;
21647 int ignore;
21648
21649 b = Fprevious_single_property_change (make_number (pos + 1),
21650 Qmouse_face,
21651 object, Qnil);
21652 e = Fnext_single_property_change (position, Qmouse_face,
21653 object, Qnil);
21654 if (NILP (b))
21655 b = make_number (0);
21656 if (NILP (e))
21657 e = make_number (SCHARS (object) - 1);
21658 fast_find_string_pos (w, XINT (b), object,
21659 &dpyinfo->mouse_face_beg_col,
21660 &dpyinfo->mouse_face_beg_row,
21661 &dpyinfo->mouse_face_beg_x,
21662 &dpyinfo->mouse_face_beg_y, 0);
21663 fast_find_string_pos (w, XINT (e), object,
21664 &dpyinfo->mouse_face_end_col,
21665 &dpyinfo->mouse_face_end_row,
21666 &dpyinfo->mouse_face_end_x,
21667 &dpyinfo->mouse_face_end_y, 1);
21668 dpyinfo->mouse_face_past_end = 0;
21669 dpyinfo->mouse_face_window = window;
21670 dpyinfo->mouse_face_face_id
21671 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21672 glyph->face_id, 1);
21673 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21674 cursor = No_Cursor;
21675 }
21676 else if (STRINGP (object) && NILP (mouse_face))
21677 {
21678 /* A string which doesn't have mouse-face, but
21679 the text ``under'' it might have. */
21680 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21681 int start = MATRIX_ROW_START_CHARPOS (r);
21682
21683 pos = string_buffer_position (w, object, start);
21684 if (pos > 0)
21685 mouse_face = get_char_property_and_overlay (make_number (pos),
21686 Qmouse_face,
21687 w->buffer,
21688 &overlay);
21689 if (!NILP (mouse_face) && !NILP (overlay))
21690 {
21691 Lisp_Object before = Foverlay_start (overlay);
21692 Lisp_Object after = Foverlay_end (overlay);
21693 int ignore;
21694
21695 /* Note that we might not be able to find position
21696 BEFORE in the glyph matrix if the overlay is
21697 entirely covered by a `display' property. In
21698 this case, we overshoot. So let's stop in
21699 the glyph matrix before glyphs for OBJECT. */
21700 fast_find_position (w, XFASTINT (before),
21701 &dpyinfo->mouse_face_beg_col,
21702 &dpyinfo->mouse_face_beg_row,
21703 &dpyinfo->mouse_face_beg_x,
21704 &dpyinfo->mouse_face_beg_y,
21705 object);
21706
21707 dpyinfo->mouse_face_past_end
21708 = !fast_find_position (w, XFASTINT (after),
21709 &dpyinfo->mouse_face_end_col,
21710 &dpyinfo->mouse_face_end_row,
21711 &dpyinfo->mouse_face_end_x,
21712 &dpyinfo->mouse_face_end_y,
21713 Qnil);
21714 dpyinfo->mouse_face_window = window;
21715 dpyinfo->mouse_face_face_id
21716 = face_at_buffer_position (w, pos, 0, 0,
21717 &ignore, pos + 1,
21718 !dpyinfo->mouse_face_hidden);
21719
21720 /* Display it as active. */
21721 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21722 cursor = No_Cursor;
21723 }
21724 }
21725 }
21726
21727 check_help_echo:
21728
21729 /* Look for a `help-echo' property. */
21730 if (NILP (help_echo_string)) {
21731 Lisp_Object help, overlay;
21732
21733 /* Check overlays first. */
21734 help = overlay = Qnil;
21735 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21736 {
21737 overlay = overlay_vec[i];
21738 help = Foverlay_get (overlay, Qhelp_echo);
21739 }
21740
21741 if (!NILP (help))
21742 {
21743 help_echo_string = help;
21744 help_echo_window = window;
21745 help_echo_object = overlay;
21746 help_echo_pos = pos;
21747 }
21748 else
21749 {
21750 Lisp_Object object = glyph->object;
21751 int charpos = glyph->charpos;
21752
21753 /* Try text properties. */
21754 if (STRINGP (object)
21755 && charpos >= 0
21756 && charpos < SCHARS (object))
21757 {
21758 help = Fget_text_property (make_number (charpos),
21759 Qhelp_echo, object);
21760 if (NILP (help))
21761 {
21762 /* If the string itself doesn't specify a help-echo,
21763 see if the buffer text ``under'' it does. */
21764 struct glyph_row *r
21765 = MATRIX_ROW (w->current_matrix, vpos);
21766 int start = MATRIX_ROW_START_CHARPOS (r);
21767 int pos = string_buffer_position (w, object, start);
21768 if (pos > 0)
21769 {
21770 help = Fget_char_property (make_number (pos),
21771 Qhelp_echo, w->buffer);
21772 if (!NILP (help))
21773 {
21774 charpos = pos;
21775 object = w->buffer;
21776 }
21777 }
21778 }
21779 }
21780 else if (BUFFERP (object)
21781 && charpos >= BEGV
21782 && charpos < ZV)
21783 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21784 object);
21785
21786 if (!NILP (help))
21787 {
21788 help_echo_string = help;
21789 help_echo_window = window;
21790 help_echo_object = object;
21791 help_echo_pos = charpos;
21792 }
21793 }
21794 }
21795
21796 /* Look for a `pointer' property. */
21797 if (NILP (pointer))
21798 {
21799 /* Check overlays first. */
21800 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21801 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21802
21803 if (NILP (pointer))
21804 {
21805 Lisp_Object object = glyph->object;
21806 int charpos = glyph->charpos;
21807
21808 /* Try text properties. */
21809 if (STRINGP (object)
21810 && charpos >= 0
21811 && charpos < SCHARS (object))
21812 {
21813 pointer = Fget_text_property (make_number (charpos),
21814 Qpointer, object);
21815 if (NILP (pointer))
21816 {
21817 /* If the string itself doesn't specify a pointer,
21818 see if the buffer text ``under'' it does. */
21819 struct glyph_row *r
21820 = MATRIX_ROW (w->current_matrix, vpos);
21821 int start = MATRIX_ROW_START_CHARPOS (r);
21822 int pos = string_buffer_position (w, object, start);
21823 if (pos > 0)
21824 pointer = Fget_char_property (make_number (pos),
21825 Qpointer, w->buffer);
21826 }
21827 }
21828 else if (BUFFERP (object)
21829 && charpos >= BEGV
21830 && charpos < ZV)
21831 pointer = Fget_text_property (make_number (charpos),
21832 Qpointer, object);
21833 }
21834 }
21835
21836 BEGV = obegv;
21837 ZV = ozv;
21838 current_buffer = obuf;
21839 }
21840
21841 set_cursor:
21842
21843 define_frame_cursor1 (f, cursor, pointer);
21844 }
21845
21846
21847 /* EXPORT for RIF:
21848 Clear any mouse-face on window W. This function is part of the
21849 redisplay interface, and is called from try_window_id and similar
21850 functions to ensure the mouse-highlight is off. */
21851
21852 void
21853 x_clear_window_mouse_face (w)
21854 struct window *w;
21855 {
21856 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21857 Lisp_Object window;
21858
21859 BLOCK_INPUT;
21860 XSETWINDOW (window, w);
21861 if (EQ (window, dpyinfo->mouse_face_window))
21862 clear_mouse_face (dpyinfo);
21863 UNBLOCK_INPUT;
21864 }
21865
21866
21867 /* EXPORT:
21868 Just discard the mouse face information for frame F, if any.
21869 This is used when the size of F is changed. */
21870
21871 void
21872 cancel_mouse_face (f)
21873 struct frame *f;
21874 {
21875 Lisp_Object window;
21876 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21877
21878 window = dpyinfo->mouse_face_window;
21879 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21880 {
21881 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21882 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21883 dpyinfo->mouse_face_window = Qnil;
21884 }
21885 }
21886
21887
21888 #endif /* HAVE_WINDOW_SYSTEM */
21889
21890 \f
21891 /***********************************************************************
21892 Exposure Events
21893 ***********************************************************************/
21894
21895 #ifdef HAVE_WINDOW_SYSTEM
21896
21897 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21898 which intersects rectangle R. R is in window-relative coordinates. */
21899
21900 static void
21901 expose_area (w, row, r, area)
21902 struct window *w;
21903 struct glyph_row *row;
21904 XRectangle *r;
21905 enum glyph_row_area area;
21906 {
21907 struct glyph *first = row->glyphs[area];
21908 struct glyph *end = row->glyphs[area] + row->used[area];
21909 struct glyph *last;
21910 int first_x, start_x, x;
21911
21912 if (area == TEXT_AREA && row->fill_line_p)
21913 /* If row extends face to end of line write the whole line. */
21914 draw_glyphs (w, 0, row, area,
21915 0, row->used[area],
21916 DRAW_NORMAL_TEXT, 0);
21917 else
21918 {
21919 /* Set START_X to the window-relative start position for drawing glyphs of
21920 AREA. The first glyph of the text area can be partially visible.
21921 The first glyphs of other areas cannot. */
21922 start_x = window_box_left_offset (w, area);
21923 x = start_x;
21924 if (area == TEXT_AREA)
21925 x += row->x;
21926
21927 /* Find the first glyph that must be redrawn. */
21928 while (first < end
21929 && x + first->pixel_width < r->x)
21930 {
21931 x += first->pixel_width;
21932 ++first;
21933 }
21934
21935 /* Find the last one. */
21936 last = first;
21937 first_x = x;
21938 while (last < end
21939 && x < r->x + r->width)
21940 {
21941 x += last->pixel_width;
21942 ++last;
21943 }
21944
21945 /* Repaint. */
21946 if (last > first)
21947 draw_glyphs (w, first_x - start_x, row, area,
21948 first - row->glyphs[area], last - row->glyphs[area],
21949 DRAW_NORMAL_TEXT, 0);
21950 }
21951 }
21952
21953
21954 /* Redraw the parts of the glyph row ROW on window W intersecting
21955 rectangle R. R is in window-relative coordinates. Value is
21956 non-zero if mouse-face was overwritten. */
21957
21958 static int
21959 expose_line (w, row, r)
21960 struct window *w;
21961 struct glyph_row *row;
21962 XRectangle *r;
21963 {
21964 xassert (row->enabled_p);
21965
21966 if (row->mode_line_p || w->pseudo_window_p)
21967 draw_glyphs (w, 0, row, TEXT_AREA,
21968 0, row->used[TEXT_AREA],
21969 DRAW_NORMAL_TEXT, 0);
21970 else
21971 {
21972 if (row->used[LEFT_MARGIN_AREA])
21973 expose_area (w, row, r, LEFT_MARGIN_AREA);
21974 if (row->used[TEXT_AREA])
21975 expose_area (w, row, r, TEXT_AREA);
21976 if (row->used[RIGHT_MARGIN_AREA])
21977 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21978 draw_row_fringe_bitmaps (w, row);
21979 }
21980
21981 return row->mouse_face_p;
21982 }
21983
21984
21985 /* Redraw those parts of glyphs rows during expose event handling that
21986 overlap other rows. Redrawing of an exposed line writes over parts
21987 of lines overlapping that exposed line; this function fixes that.
21988
21989 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21990 row in W's current matrix that is exposed and overlaps other rows.
21991 LAST_OVERLAPPING_ROW is the last such row. */
21992
21993 static void
21994 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21995 struct window *w;
21996 struct glyph_row *first_overlapping_row;
21997 struct glyph_row *last_overlapping_row;
21998 {
21999 struct glyph_row *row;
22000
22001 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22002 if (row->overlapping_p)
22003 {
22004 xassert (row->enabled_p && !row->mode_line_p);
22005
22006 if (row->used[LEFT_MARGIN_AREA])
22007 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22008
22009 if (row->used[TEXT_AREA])
22010 x_fix_overlapping_area (w, row, TEXT_AREA);
22011
22012 if (row->used[RIGHT_MARGIN_AREA])
22013 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22014 }
22015 }
22016
22017
22018 /* Return non-zero if W's cursor intersects rectangle R. */
22019
22020 static int
22021 phys_cursor_in_rect_p (w, r)
22022 struct window *w;
22023 XRectangle *r;
22024 {
22025 XRectangle cr, result;
22026 struct glyph *cursor_glyph;
22027
22028 cursor_glyph = get_phys_cursor_glyph (w);
22029 if (cursor_glyph)
22030 {
22031 /* r is relative to W's box, but w->phys_cursor.x is relative
22032 to left edge of W's TEXT area. Adjust it. */
22033 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22034 cr.y = w->phys_cursor.y;
22035 cr.width = cursor_glyph->pixel_width;
22036 cr.height = w->phys_cursor_height;
22037 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22038 I assume the effect is the same -- and this is portable. */
22039 return x_intersect_rectangles (&cr, r, &result);
22040 }
22041 else
22042 return 0;
22043 }
22044
22045
22046 /* EXPORT:
22047 Draw a vertical window border to the right of window W if W doesn't
22048 have vertical scroll bars. */
22049
22050 void
22051 x_draw_vertical_border (w)
22052 struct window *w;
22053 {
22054 /* We could do better, if we knew what type of scroll-bar the adjacent
22055 windows (on either side) have... But we don't :-(
22056 However, I think this works ok. ++KFS 2003-04-25 */
22057
22058 /* Redraw borders between horizontally adjacent windows. Don't
22059 do it for frames with vertical scroll bars because either the
22060 right scroll bar of a window, or the left scroll bar of its
22061 neighbor will suffice as a border. */
22062 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22063 return;
22064
22065 if (!WINDOW_RIGHTMOST_P (w)
22066 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22067 {
22068 int x0, x1, y0, y1;
22069
22070 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22071 y1 -= 1;
22072
22073 rif->draw_vertical_window_border (w, x1, y0, y1);
22074 }
22075 else if (!WINDOW_LEFTMOST_P (w)
22076 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22077 {
22078 int x0, x1, y0, y1;
22079
22080 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22081 y1 -= 1;
22082
22083 rif->draw_vertical_window_border (w, x0, y0, y1);
22084 }
22085 }
22086
22087
22088 /* Redraw the part of window W intersection rectangle FR. Pixel
22089 coordinates in FR are frame-relative. Call this function with
22090 input blocked. Value is non-zero if the exposure overwrites
22091 mouse-face. */
22092
22093 static int
22094 expose_window (w, fr)
22095 struct window *w;
22096 XRectangle *fr;
22097 {
22098 struct frame *f = XFRAME (w->frame);
22099 XRectangle wr, r;
22100 int mouse_face_overwritten_p = 0;
22101
22102 /* If window is not yet fully initialized, do nothing. This can
22103 happen when toolkit scroll bars are used and a window is split.
22104 Reconfiguring the scroll bar will generate an expose for a newly
22105 created window. */
22106 if (w->current_matrix == NULL)
22107 return 0;
22108
22109 /* When we're currently updating the window, display and current
22110 matrix usually don't agree. Arrange for a thorough display
22111 later. */
22112 if (w == updated_window)
22113 {
22114 SET_FRAME_GARBAGED (f);
22115 return 0;
22116 }
22117
22118 /* Frame-relative pixel rectangle of W. */
22119 wr.x = WINDOW_LEFT_EDGE_X (w);
22120 wr.y = WINDOW_TOP_EDGE_Y (w);
22121 wr.width = WINDOW_TOTAL_WIDTH (w);
22122 wr.height = WINDOW_TOTAL_HEIGHT (w);
22123
22124 if (x_intersect_rectangles (fr, &wr, &r))
22125 {
22126 int yb = window_text_bottom_y (w);
22127 struct glyph_row *row;
22128 int cursor_cleared_p;
22129 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22130
22131 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22132 r.x, r.y, r.width, r.height));
22133
22134 /* Convert to window coordinates. */
22135 r.x -= WINDOW_LEFT_EDGE_X (w);
22136 r.y -= WINDOW_TOP_EDGE_Y (w);
22137
22138 /* Turn off the cursor. */
22139 if (!w->pseudo_window_p
22140 && phys_cursor_in_rect_p (w, &r))
22141 {
22142 x_clear_cursor (w);
22143 cursor_cleared_p = 1;
22144 }
22145 else
22146 cursor_cleared_p = 0;
22147
22148 /* Update lines intersecting rectangle R. */
22149 first_overlapping_row = last_overlapping_row = NULL;
22150 for (row = w->current_matrix->rows;
22151 row->enabled_p;
22152 ++row)
22153 {
22154 int y0 = row->y;
22155 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22156
22157 if ((y0 >= r.y && y0 < r.y + r.height)
22158 || (y1 > r.y && y1 < r.y + r.height)
22159 || (r.y >= y0 && r.y < y1)
22160 || (r.y + r.height > y0 && r.y + r.height < y1))
22161 {
22162 /* A header line may be overlapping, but there is no need
22163 to fix overlapping areas for them. KFS 2005-02-12 */
22164 if (row->overlapping_p && !row->mode_line_p)
22165 {
22166 if (first_overlapping_row == NULL)
22167 first_overlapping_row = row;
22168 last_overlapping_row = row;
22169 }
22170
22171 if (expose_line (w, row, &r))
22172 mouse_face_overwritten_p = 1;
22173 }
22174
22175 if (y1 >= yb)
22176 break;
22177 }
22178
22179 /* Display the mode line if there is one. */
22180 if (WINDOW_WANTS_MODELINE_P (w)
22181 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22182 row->enabled_p)
22183 && row->y < r.y + r.height)
22184 {
22185 if (expose_line (w, row, &r))
22186 mouse_face_overwritten_p = 1;
22187 }
22188
22189 if (!w->pseudo_window_p)
22190 {
22191 /* Fix the display of overlapping rows. */
22192 if (first_overlapping_row)
22193 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22194
22195 /* Draw border between windows. */
22196 x_draw_vertical_border (w);
22197
22198 /* Turn the cursor on again. */
22199 if (cursor_cleared_p)
22200 update_window_cursor (w, 1);
22201 }
22202 }
22203
22204 return mouse_face_overwritten_p;
22205 }
22206
22207
22208
22209 /* Redraw (parts) of all windows in the window tree rooted at W that
22210 intersect R. R contains frame pixel coordinates. Value is
22211 non-zero if the exposure overwrites mouse-face. */
22212
22213 static int
22214 expose_window_tree (w, r)
22215 struct window *w;
22216 XRectangle *r;
22217 {
22218 struct frame *f = XFRAME (w->frame);
22219 int mouse_face_overwritten_p = 0;
22220
22221 while (w && !FRAME_GARBAGED_P (f))
22222 {
22223 if (!NILP (w->hchild))
22224 mouse_face_overwritten_p
22225 |= expose_window_tree (XWINDOW (w->hchild), r);
22226 else if (!NILP (w->vchild))
22227 mouse_face_overwritten_p
22228 |= expose_window_tree (XWINDOW (w->vchild), r);
22229 else
22230 mouse_face_overwritten_p |= expose_window (w, r);
22231
22232 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22233 }
22234
22235 return mouse_face_overwritten_p;
22236 }
22237
22238
22239 /* EXPORT:
22240 Redisplay an exposed area of frame F. X and Y are the upper-left
22241 corner of the exposed rectangle. W and H are width and height of
22242 the exposed area. All are pixel values. W or H zero means redraw
22243 the entire frame. */
22244
22245 void
22246 expose_frame (f, x, y, w, h)
22247 struct frame *f;
22248 int x, y, w, h;
22249 {
22250 XRectangle r;
22251 int mouse_face_overwritten_p = 0;
22252
22253 TRACE ((stderr, "expose_frame "));
22254
22255 /* No need to redraw if frame will be redrawn soon. */
22256 if (FRAME_GARBAGED_P (f))
22257 {
22258 TRACE ((stderr, " garbaged\n"));
22259 return;
22260 }
22261
22262 /* If basic faces haven't been realized yet, there is no point in
22263 trying to redraw anything. This can happen when we get an expose
22264 event while Emacs is starting, e.g. by moving another window. */
22265 if (FRAME_FACE_CACHE (f) == NULL
22266 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22267 {
22268 TRACE ((stderr, " no faces\n"));
22269 return;
22270 }
22271
22272 if (w == 0 || h == 0)
22273 {
22274 r.x = r.y = 0;
22275 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22276 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22277 }
22278 else
22279 {
22280 r.x = x;
22281 r.y = y;
22282 r.width = w;
22283 r.height = h;
22284 }
22285
22286 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22287 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22288
22289 if (WINDOWP (f->tool_bar_window))
22290 mouse_face_overwritten_p
22291 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22292
22293 #ifdef HAVE_X_WINDOWS
22294 #ifndef MSDOS
22295 #ifndef USE_X_TOOLKIT
22296 if (WINDOWP (f->menu_bar_window))
22297 mouse_face_overwritten_p
22298 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22299 #endif /* not USE_X_TOOLKIT */
22300 #endif
22301 #endif
22302
22303 /* Some window managers support a focus-follows-mouse style with
22304 delayed raising of frames. Imagine a partially obscured frame,
22305 and moving the mouse into partially obscured mouse-face on that
22306 frame. The visible part of the mouse-face will be highlighted,
22307 then the WM raises the obscured frame. With at least one WM, KDE
22308 2.1, Emacs is not getting any event for the raising of the frame
22309 (even tried with SubstructureRedirectMask), only Expose events.
22310 These expose events will draw text normally, i.e. not
22311 highlighted. Which means we must redo the highlight here.
22312 Subsume it under ``we love X''. --gerd 2001-08-15 */
22313 /* Included in Windows version because Windows most likely does not
22314 do the right thing if any third party tool offers
22315 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22316 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22317 {
22318 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22319 if (f == dpyinfo->mouse_face_mouse_frame)
22320 {
22321 int x = dpyinfo->mouse_face_mouse_x;
22322 int y = dpyinfo->mouse_face_mouse_y;
22323 clear_mouse_face (dpyinfo);
22324 note_mouse_highlight (f, x, y);
22325 }
22326 }
22327 }
22328
22329
22330 /* EXPORT:
22331 Determine the intersection of two rectangles R1 and R2. Return
22332 the intersection in *RESULT. Value is non-zero if RESULT is not
22333 empty. */
22334
22335 int
22336 x_intersect_rectangles (r1, r2, result)
22337 XRectangle *r1, *r2, *result;
22338 {
22339 XRectangle *left, *right;
22340 XRectangle *upper, *lower;
22341 int intersection_p = 0;
22342
22343 /* Rearrange so that R1 is the left-most rectangle. */
22344 if (r1->x < r2->x)
22345 left = r1, right = r2;
22346 else
22347 left = r2, right = r1;
22348
22349 /* X0 of the intersection is right.x0, if this is inside R1,
22350 otherwise there is no intersection. */
22351 if (right->x <= left->x + left->width)
22352 {
22353 result->x = right->x;
22354
22355 /* The right end of the intersection is the minimum of the
22356 the right ends of left and right. */
22357 result->width = (min (left->x + left->width, right->x + right->width)
22358 - result->x);
22359
22360 /* Same game for Y. */
22361 if (r1->y < r2->y)
22362 upper = r1, lower = r2;
22363 else
22364 upper = r2, lower = r1;
22365
22366 /* The upper end of the intersection is lower.y0, if this is inside
22367 of upper. Otherwise, there is no intersection. */
22368 if (lower->y <= upper->y + upper->height)
22369 {
22370 result->y = lower->y;
22371
22372 /* The lower end of the intersection is the minimum of the lower
22373 ends of upper and lower. */
22374 result->height = (min (lower->y + lower->height,
22375 upper->y + upper->height)
22376 - result->y);
22377 intersection_p = 1;
22378 }
22379 }
22380
22381 return intersection_p;
22382 }
22383
22384 #endif /* HAVE_WINDOW_SYSTEM */
22385
22386 \f
22387 /***********************************************************************
22388 Initialization
22389 ***********************************************************************/
22390
22391 void
22392 syms_of_xdisp ()
22393 {
22394 Vwith_echo_area_save_vector = Qnil;
22395 staticpro (&Vwith_echo_area_save_vector);
22396
22397 Vmessage_stack = Qnil;
22398 staticpro (&Vmessage_stack);
22399
22400 Qinhibit_redisplay = intern ("inhibit-redisplay");
22401 staticpro (&Qinhibit_redisplay);
22402
22403 message_dolog_marker1 = Fmake_marker ();
22404 staticpro (&message_dolog_marker1);
22405 message_dolog_marker2 = Fmake_marker ();
22406 staticpro (&message_dolog_marker2);
22407 message_dolog_marker3 = Fmake_marker ();
22408 staticpro (&message_dolog_marker3);
22409
22410 #if GLYPH_DEBUG
22411 defsubr (&Sdump_frame_glyph_matrix);
22412 defsubr (&Sdump_glyph_matrix);
22413 defsubr (&Sdump_glyph_row);
22414 defsubr (&Sdump_tool_bar_row);
22415 defsubr (&Strace_redisplay);
22416 defsubr (&Strace_to_stderr);
22417 #endif
22418 #ifdef HAVE_WINDOW_SYSTEM
22419 defsubr (&Stool_bar_lines_needed);
22420 defsubr (&Slookup_image_map);
22421 #endif
22422 defsubr (&Sformat_mode_line);
22423
22424 staticpro (&Qmenu_bar_update_hook);
22425 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22426
22427 staticpro (&Qoverriding_terminal_local_map);
22428 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22429
22430 staticpro (&Qoverriding_local_map);
22431 Qoverriding_local_map = intern ("overriding-local-map");
22432
22433 staticpro (&Qwindow_scroll_functions);
22434 Qwindow_scroll_functions = intern ("window-scroll-functions");
22435
22436 staticpro (&Qredisplay_end_trigger_functions);
22437 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22438
22439 staticpro (&Qinhibit_point_motion_hooks);
22440 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22441
22442 QCdata = intern (":data");
22443 staticpro (&QCdata);
22444 Qdisplay = intern ("display");
22445 staticpro (&Qdisplay);
22446 Qspace_width = intern ("space-width");
22447 staticpro (&Qspace_width);
22448 Qraise = intern ("raise");
22449 staticpro (&Qraise);
22450 Qslice = intern ("slice");
22451 staticpro (&Qslice);
22452 Qspace = intern ("space");
22453 staticpro (&Qspace);
22454 Qmargin = intern ("margin");
22455 staticpro (&Qmargin);
22456 Qpointer = intern ("pointer");
22457 staticpro (&Qpointer);
22458 Qleft_margin = intern ("left-margin");
22459 staticpro (&Qleft_margin);
22460 Qright_margin = intern ("right-margin");
22461 staticpro (&Qright_margin);
22462 Qcenter = intern ("center");
22463 staticpro (&Qcenter);
22464 Qline_height = intern ("line-height");
22465 staticpro (&Qline_height);
22466 QCalign_to = intern (":align-to");
22467 staticpro (&QCalign_to);
22468 QCrelative_width = intern (":relative-width");
22469 staticpro (&QCrelative_width);
22470 QCrelative_height = intern (":relative-height");
22471 staticpro (&QCrelative_height);
22472 QCeval = intern (":eval");
22473 staticpro (&QCeval);
22474 QCpropertize = intern (":propertize");
22475 staticpro (&QCpropertize);
22476 QCfile = intern (":file");
22477 staticpro (&QCfile);
22478 Qfontified = intern ("fontified");
22479 staticpro (&Qfontified);
22480 Qfontification_functions = intern ("fontification-functions");
22481 staticpro (&Qfontification_functions);
22482 Qtrailing_whitespace = intern ("trailing-whitespace");
22483 staticpro (&Qtrailing_whitespace);
22484 Qescape_glyph = intern ("escape-glyph");
22485 staticpro (&Qescape_glyph);
22486 Qimage = intern ("image");
22487 staticpro (&Qimage);
22488 QCmap = intern (":map");
22489 staticpro (&QCmap);
22490 QCpointer = intern (":pointer");
22491 staticpro (&QCpointer);
22492 Qrect = intern ("rect");
22493 staticpro (&Qrect);
22494 Qcircle = intern ("circle");
22495 staticpro (&Qcircle);
22496 Qpoly = intern ("poly");
22497 staticpro (&Qpoly);
22498 Qmessage_truncate_lines = intern ("message-truncate-lines");
22499 staticpro (&Qmessage_truncate_lines);
22500 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22501 staticpro (&Qcursor_in_non_selected_windows);
22502 Qgrow_only = intern ("grow-only");
22503 staticpro (&Qgrow_only);
22504 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22505 staticpro (&Qinhibit_menubar_update);
22506 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22507 staticpro (&Qinhibit_eval_during_redisplay);
22508 Qposition = intern ("position");
22509 staticpro (&Qposition);
22510 Qbuffer_position = intern ("buffer-position");
22511 staticpro (&Qbuffer_position);
22512 Qobject = intern ("object");
22513 staticpro (&Qobject);
22514 Qbar = intern ("bar");
22515 staticpro (&Qbar);
22516 Qhbar = intern ("hbar");
22517 staticpro (&Qhbar);
22518 Qbox = intern ("box");
22519 staticpro (&Qbox);
22520 Qhollow = intern ("hollow");
22521 staticpro (&Qhollow);
22522 Qhand = intern ("hand");
22523 staticpro (&Qhand);
22524 Qarrow = intern ("arrow");
22525 staticpro (&Qarrow);
22526 Qtext = intern ("text");
22527 staticpro (&Qtext);
22528 Qrisky_local_variable = intern ("risky-local-variable");
22529 staticpro (&Qrisky_local_variable);
22530 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22531 staticpro (&Qinhibit_free_realized_faces);
22532
22533 list_of_error = Fcons (Fcons (intern ("error"),
22534 Fcons (intern ("void-variable"), Qnil)),
22535 Qnil);
22536 staticpro (&list_of_error);
22537
22538 Qlast_arrow_position = intern ("last-arrow-position");
22539 staticpro (&Qlast_arrow_position);
22540 Qlast_arrow_string = intern ("last-arrow-string");
22541 staticpro (&Qlast_arrow_string);
22542
22543 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22544 staticpro (&Qoverlay_arrow_string);
22545 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22546 staticpro (&Qoverlay_arrow_bitmap);
22547
22548 echo_buffer[0] = echo_buffer[1] = Qnil;
22549 staticpro (&echo_buffer[0]);
22550 staticpro (&echo_buffer[1]);
22551
22552 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22553 staticpro (&echo_area_buffer[0]);
22554 staticpro (&echo_area_buffer[1]);
22555
22556 Vmessages_buffer_name = build_string ("*Messages*");
22557 staticpro (&Vmessages_buffer_name);
22558
22559 mode_line_proptrans_alist = Qnil;
22560 staticpro (&mode_line_proptrans_alist);
22561
22562 mode_line_string_list = Qnil;
22563 staticpro (&mode_line_string_list);
22564
22565 help_echo_string = Qnil;
22566 staticpro (&help_echo_string);
22567 help_echo_object = Qnil;
22568 staticpro (&help_echo_object);
22569 help_echo_window = Qnil;
22570 staticpro (&help_echo_window);
22571 previous_help_echo_string = Qnil;
22572 staticpro (&previous_help_echo_string);
22573 help_echo_pos = -1;
22574
22575 #ifdef HAVE_WINDOW_SYSTEM
22576 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22577 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22578 For example, if a block cursor is over a tab, it will be drawn as
22579 wide as that tab on the display. */);
22580 x_stretch_cursor_p = 0;
22581 #endif
22582
22583 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22584 doc: /* *Non-nil means highlight trailing whitespace.
22585 The face used for trailing whitespace is `trailing-whitespace'. */);
22586 Vshow_trailing_whitespace = Qnil;
22587
22588 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22589 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22590 Vshow_nonbreak_escape = Qt;
22591
22592 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22593 doc: /* *The pointer shape to show in void text areas.
22594 Nil means to show the text pointer. Other options are `arrow', `text',
22595 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22596 Vvoid_text_area_pointer = Qarrow;
22597
22598 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22599 doc: /* Non-nil means don't actually do any redisplay.
22600 This is used for internal purposes. */);
22601 Vinhibit_redisplay = Qnil;
22602
22603 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22604 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22605 Vglobal_mode_string = Qnil;
22606
22607 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22608 doc: /* Marker for where to display an arrow on top of the buffer text.
22609 This must be the beginning of a line in order to work.
22610 See also `overlay-arrow-string'. */);
22611 Voverlay_arrow_position = Qnil;
22612
22613 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22614 doc: /* String to display as an arrow in non-window frames.
22615 See also `overlay-arrow-position'. */);
22616 Voverlay_arrow_string = build_string ("=>");
22617
22618 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22619 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22620 The symbols on this list are examined during redisplay to determine
22621 where to display overlay arrows. */);
22622 Voverlay_arrow_variable_list
22623 = Fcons (intern ("overlay-arrow-position"), Qnil);
22624
22625 DEFVAR_INT ("scroll-step", &scroll_step,
22626 doc: /* *The number of lines to try scrolling a window by when point moves out.
22627 If that fails to bring point back on frame, point is centered instead.
22628 If this is zero, point is always centered after it moves off frame.
22629 If you want scrolling to always be a line at a time, you should set
22630 `scroll-conservatively' to a large value rather than set this to 1. */);
22631
22632 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22633 doc: /* *Scroll up to this many lines, to bring point back on screen.
22634 A value of zero means to scroll the text to center point vertically
22635 in the window. */);
22636 scroll_conservatively = 0;
22637
22638 DEFVAR_INT ("scroll-margin", &scroll_margin,
22639 doc: /* *Number of lines of margin at the top and bottom of a window.
22640 Recenter the window whenever point gets within this many lines
22641 of the top or bottom of the window. */);
22642 scroll_margin = 0;
22643
22644 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22645 doc: /* Pixels per inch on current display.
22646 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22647 Vdisplay_pixels_per_inch = make_float (72.0);
22648
22649 #if GLYPH_DEBUG
22650 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22651 #endif
22652
22653 DEFVAR_BOOL ("truncate-partial-width-windows",
22654 &truncate_partial_width_windows,
22655 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22656 truncate_partial_width_windows = 1;
22657
22658 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22659 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22660 Any other value means to use the appropriate face, `mode-line',
22661 `header-line', or `menu' respectively. */);
22662 mode_line_inverse_video = 1;
22663
22664 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22665 doc: /* *Maximum buffer size for which line number should be displayed.
22666 If the buffer is bigger than this, the line number does not appear
22667 in the mode line. A value of nil means no limit. */);
22668 Vline_number_display_limit = Qnil;
22669
22670 DEFVAR_INT ("line-number-display-limit-width",
22671 &line_number_display_limit_width,
22672 doc: /* *Maximum line width (in characters) for line number display.
22673 If the average length of the lines near point is bigger than this, then the
22674 line number may be omitted from the mode line. */);
22675 line_number_display_limit_width = 200;
22676
22677 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22678 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22679 highlight_nonselected_windows = 0;
22680
22681 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22682 doc: /* Non-nil if more than one frame is visible on this display.
22683 Minibuffer-only frames don't count, but iconified frames do.
22684 This variable is not guaranteed to be accurate except while processing
22685 `frame-title-format' and `icon-title-format'. */);
22686
22687 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22688 doc: /* Template for displaying the title bar of visible frames.
22689 \(Assuming the window manager supports this feature.)
22690 This variable has the same structure as `mode-line-format' (which see),
22691 and is used only on frames for which no explicit name has been set
22692 \(see `modify-frame-parameters'). */);
22693
22694 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22695 doc: /* Template for displaying the title bar of an iconified frame.
22696 \(Assuming the window manager supports this feature.)
22697 This variable has the same structure as `mode-line-format' (which see),
22698 and is used only on frames for which no explicit name has been set
22699 \(see `modify-frame-parameters'). */);
22700 Vicon_title_format
22701 = Vframe_title_format
22702 = Fcons (intern ("multiple-frames"),
22703 Fcons (build_string ("%b"),
22704 Fcons (Fcons (empty_string,
22705 Fcons (intern ("invocation-name"),
22706 Fcons (build_string ("@"),
22707 Fcons (intern ("system-name"),
22708 Qnil)))),
22709 Qnil)));
22710
22711 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22712 doc: /* Maximum number of lines to keep in the message log buffer.
22713 If nil, disable message logging. If t, log messages but don't truncate
22714 the buffer when it becomes large. */);
22715 Vmessage_log_max = make_number (50);
22716
22717 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22718 doc: /* Functions called before redisplay, if window sizes have changed.
22719 The value should be a list of functions that take one argument.
22720 Just before redisplay, for each frame, if any of its windows have changed
22721 size since the last redisplay, or have been split or deleted,
22722 all the functions in the list are called, with the frame as argument. */);
22723 Vwindow_size_change_functions = Qnil;
22724
22725 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22726 doc: /* List of functions to call before redisplaying a window with scrolling.
22727 Each function is called with two arguments, the window
22728 and its new display-start position. Note that the value of `window-end'
22729 is not valid when these functions are called. */);
22730 Vwindow_scroll_functions = Qnil;
22731
22732 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22733 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22734 mouse_autoselect_window = 0;
22735
22736 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22737 doc: /* *Non-nil means automatically resize tool-bars.
22738 This increases a tool-bar's height if not all tool-bar items are visible.
22739 It decreases a tool-bar's height when it would display blank lines
22740 otherwise. */);
22741 auto_resize_tool_bars_p = 1;
22742
22743 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22744 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22745 auto_raise_tool_bar_buttons_p = 1;
22746
22747 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22748 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22749 make_cursor_line_fully_visible_p = 1;
22750
22751 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22752 doc: /* *Margin around tool-bar buttons in pixels.
22753 If an integer, use that for both horizontal and vertical margins.
22754 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22755 HORZ specifying the horizontal margin, and VERT specifying the
22756 vertical margin. */);
22757 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22758
22759 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22760 doc: /* *Relief thickness of tool-bar buttons. */);
22761 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22762
22763 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22764 doc: /* List of functions to call to fontify regions of text.
22765 Each function is called with one argument POS. Functions must
22766 fontify a region starting at POS in the current buffer, and give
22767 fontified regions the property `fontified'. */);
22768 Vfontification_functions = Qnil;
22769 Fmake_variable_buffer_local (Qfontification_functions);
22770
22771 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22772 &unibyte_display_via_language_environment,
22773 doc: /* *Non-nil means display unibyte text according to language environment.
22774 Specifically this means that unibyte non-ASCII characters
22775 are displayed by converting them to the equivalent multibyte characters
22776 according to the current language environment. As a result, they are
22777 displayed according to the current fontset. */);
22778 unibyte_display_via_language_environment = 0;
22779
22780 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22781 doc: /* *Maximum height for resizing mini-windows.
22782 If a float, it specifies a fraction of the mini-window frame's height.
22783 If an integer, it specifies a number of lines. */);
22784 Vmax_mini_window_height = make_float (0.25);
22785
22786 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22787 doc: /* *How to resize mini-windows.
22788 A value of nil means don't automatically resize mini-windows.
22789 A value of t means resize them to fit the text displayed in them.
22790 A value of `grow-only', the default, means let mini-windows grow
22791 only, until their display becomes empty, at which point the windows
22792 go back to their normal size. */);
22793 Vresize_mini_windows = Qgrow_only;
22794
22795 DEFVAR_LISP ("cursor-in-non-selected-windows",
22796 &Vcursor_in_non_selected_windows,
22797 doc: /* *Cursor type to display in non-selected windows.
22798 t means to use hollow box cursor. See `cursor-type' for other values. */);
22799 Vcursor_in_non_selected_windows = Qt;
22800
22801 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22802 doc: /* Alist specifying how to blink the cursor off.
22803 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22804 `cursor-type' frame-parameter or variable equals ON-STATE,
22805 comparing using `equal', Emacs uses OFF-STATE to specify
22806 how to blink it off. */);
22807 Vblink_cursor_alist = Qnil;
22808
22809 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22810 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22811 automatic_hscrolling_p = 1;
22812
22813 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22814 doc: /* *How many columns away from the window edge point is allowed to get
22815 before automatic hscrolling will horizontally scroll the window. */);
22816 hscroll_margin = 5;
22817
22818 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22819 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22820 When point is less than `automatic-hscroll-margin' columns from the window
22821 edge, automatic hscrolling will scroll the window by the amount of columns
22822 determined by this variable. If its value is a positive integer, scroll that
22823 many columns. If it's a positive floating-point number, it specifies the
22824 fraction of the window's width to scroll. If it's nil or zero, point will be
22825 centered horizontally after the scroll. Any other value, including negative
22826 numbers, are treated as if the value were zero.
22827
22828 Automatic hscrolling always moves point outside the scroll margin, so if
22829 point was more than scroll step columns inside the margin, the window will
22830 scroll more than the value given by the scroll step.
22831
22832 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22833 and `scroll-right' overrides this variable's effect. */);
22834 Vhscroll_step = make_number (0);
22835
22836 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22837 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22838 Bind this around calls to `message' to let it take effect. */);
22839 message_truncate_lines = 0;
22840
22841 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22842 doc: /* Normal hook run to update the menu bar definitions.
22843 Redisplay runs this hook before it redisplays the menu bar.
22844 This is used to update submenus such as Buffers,
22845 whose contents depend on various data. */);
22846 Vmenu_bar_update_hook = Qnil;
22847
22848 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22849 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22850 inhibit_menubar_update = 0;
22851
22852 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22853 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22854 inhibit_eval_during_redisplay = 0;
22855
22856 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22857 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22858 inhibit_free_realized_faces = 0;
22859
22860 #if GLYPH_DEBUG
22861 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22862 doc: /* Inhibit try_window_id display optimization. */);
22863 inhibit_try_window_id = 0;
22864
22865 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22866 doc: /* Inhibit try_window_reusing display optimization. */);
22867 inhibit_try_window_reusing = 0;
22868
22869 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22870 doc: /* Inhibit try_cursor_movement display optimization. */);
22871 inhibit_try_cursor_movement = 0;
22872 #endif /* GLYPH_DEBUG */
22873 }
22874
22875
22876 /* Initialize this module when Emacs starts. */
22877
22878 void
22879 init_xdisp ()
22880 {
22881 Lisp_Object root_window;
22882 struct window *mini_w;
22883
22884 current_header_line_height = current_mode_line_height = -1;
22885
22886 CHARPOS (this_line_start_pos) = 0;
22887
22888 mini_w = XWINDOW (minibuf_window);
22889 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22890
22891 if (!noninteractive)
22892 {
22893 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22894 int i;
22895
22896 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22897 set_window_height (root_window,
22898 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22899 0);
22900 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22901 set_window_height (minibuf_window, 1, 0);
22902
22903 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22904 mini_w->total_cols = make_number (FRAME_COLS (f));
22905
22906 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22907 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22908 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22909
22910 /* The default ellipsis glyphs `...'. */
22911 for (i = 0; i < 3; ++i)
22912 default_invis_vector[i] = make_number ('.');
22913 }
22914
22915 {
22916 /* Allocate the buffer for frame titles.
22917 Also used for `format-mode-line'. */
22918 int size = 100;
22919 frame_title_buf = (char *) xmalloc (size);
22920 frame_title_buf_end = frame_title_buf + size;
22921 frame_title_ptr = NULL;
22922 }
22923
22924 help_echo_showing_p = 0;
22925 }
22926
22927
22928 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22929 (do not change this comment) */