]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(remember_mouse_glyph): Don't crash if glyphs are not initialized.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
275
276 Lisp_Object Vtool_bar_border;
277
278 /* Margin around tool bar buttons in pixels. */
279
280 Lisp_Object Vtool_bar_button_margin;
281
282 /* Thickness of shadow to draw around tool bar buttons. */
283
284 EMACS_INT tool_bar_button_relief;
285
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
288
289 int auto_resize_tool_bars_p;
290
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
294
295 int x_stretch_cursor_p;
296
297 /* Non-nil means don't actually do any redisplay. */
298
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
300
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
302
303 int inhibit_eval_during_redisplay;
304
305 /* Names of text properties relevant for redisplay. */
306
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
309
310 /* Symbols used in text property values. */
311
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
323
324 /* Non-nil means highlight trailing whitespace. */
325
326 Lisp_Object Vshow_trailing_whitespace;
327
328 /* Non-nil means escape non-break space and hyphens. */
329
330 Lisp_Object Vnobreak_char_display;
331
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
334
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
337
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
343
344 #endif /* HAVE_WINDOW_SYSTEM */
345
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
349
350 Lisp_Object Vvoid_text_area_pointer;
351
352 /* Name of the face used to highlight trailing whitespace. */
353
354 Lisp_Object Qtrailing_whitespace;
355
356 /* Name and number of the face used to highlight escape glyphs. */
357
358 Lisp_Object Qescape_glyph;
359
360 /* Name and number of the face used to highlight non-breaking spaces. */
361
362 Lisp_Object Qnobreak_space;
363
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
366
367 Lisp_Object Qimage;
368
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
372
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
375
376 int noninteractive_need_newline;
377
378 /* Non-zero means print newline to message log before next message. */
379
380 static int message_log_need_newline;
381
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
388 \f
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
393
394 static struct text_pos this_line_start_pos;
395
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
398
399 static struct text_pos this_line_end_pos;
400
401 /* The vertical positions and the height of this line. */
402
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
406
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
409
410 static int this_line_start_x;
411
412 /* Buffer that this_line_.* variables are referring to. */
413
414 static struct buffer *this_line_buffer;
415
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
418
419 int truncate_partial_width_windows;
420
421 /* A flag to control how to display unibyte 8-bit character. */
422
423 int unibyte_display_via_language_environment;
424
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
428
429 int multiple_frames;
430
431 Lisp_Object Vglobal_mode_string;
432
433
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
437
438 Lisp_Object Voverlay_arrow_variable_list;
439
440 /* Marker for where to display an arrow on top of the buffer text. */
441
442 Lisp_Object Voverlay_arrow_position;
443
444 /* String to display for the arrow. Only used on terminal frames. */
445
446 Lisp_Object Voverlay_arrow_string;
447
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
452
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
454
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
457
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
459
460 /* Like mode-line-format, but for the title bar on a visible frame. */
461
462 Lisp_Object Vframe_title_format;
463
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
465
466 Lisp_Object Vicon_title_format;
467
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
471
472 static Lisp_Object Vwindow_size_change_functions;
473
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
475
476 /* Nonzero if an overlay arrow has been displayed in this window. */
477
478 static int overlay_arrow_seen;
479
480 /* Nonzero means highlight the region even in nonselected windows. */
481
482 int highlight_nonselected_windows;
483
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
486
487 static EMACS_INT scroll_step;
488
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
491
492 static EMACS_INT scroll_conservatively;
493
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
498
499 EMACS_INT scroll_margin;
500
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
504
505 int buffer_shared;
506
507 /* Vector containing glyphs for an ellipsis `...'. */
508
509 static Lisp_Object default_invis_vector[3];
510
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
514
515 This variable is deprecated. */
516
517 int mode_line_inverse_video;
518
519 /* Prompt to display in front of the mini-buffer contents. */
520
521 Lisp_Object minibuf_prompt;
522
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
525
526 int minibuf_prompt_width;
527
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
531
532 Lisp_Object echo_area_window;
533
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
538
539 Lisp_Object Vmessage_stack;
540
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
543
544 int message_enable_multibyte;
545
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
547
548 int update_mode_lines;
549
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
552
553 int windows_or_buffers_changed;
554
555 /* Nonzero means a frame's cursor type has been changed. */
556
557 int cursor_type_changed;
558
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
561
562 int line_number_displayed;
563
564 /* Maximum buffer size for which to display line numbers. */
565
566 Lisp_Object Vline_number_display_limit;
567
568 /* Line width to consider when repositioning for line number display. */
569
570 static EMACS_INT line_number_display_limit_width;
571
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
574
575 Lisp_Object Vmessage_log_max;
576
577 /* The name of the *Messages* buffer, a string. */
578
579 static Lisp_Object Vmessages_buffer_name;
580
581 /* Index 0 is the buffer that holds the current (desired) echo area message,
582 or nil if none is desired right now.
583
584 Index 1 is the buffer that holds the previously displayed echo area message,
585 or nil to indicate no message. This is normally what's on the screen now.
586
587 These two can point to the same buffer. That happens when the last
588 message output by the user (or made by echoing) has been displayed. */
589
590 Lisp_Object echo_area_buffer[2];
591
592 /* Permanent pointers to the two buffers that are used for echo area
593 purposes. Once the two buffers are made, and their pointers are
594 placed here, these two slots remain unchanged unless those buffers
595 need to be created afresh. */
596
597 static Lisp_Object echo_buffer[2];
598
599 /* A vector saved used in with_area_buffer to reduce consing. */
600
601 static Lisp_Object Vwith_echo_area_save_vector;
602
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
605
606 static int display_last_displayed_message_p;
607
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
610
611 int message_buf_print;
612
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
614
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
617
618 /* When evaluating expressions from menu bar items (enable conditions,
619 for instance), this is the frame they are being processed for. */
620
621 Lisp_Object Vmenu_updating_frame;
622
623 /* Maximum height for resizing mini-windows. Either a float
624 specifying a fraction of the available height, or an integer
625 specifying a number of lines. */
626
627 Lisp_Object Vmax_mini_window_height;
628
629 /* Non-zero means messages should be displayed with truncated
630 lines instead of being continued. */
631
632 int message_truncate_lines;
633 Lisp_Object Qmessage_truncate_lines;
634
635 /* Set to 1 in clear_message to make redisplay_internal aware
636 of an emptied echo area. */
637
638 static int message_cleared_p;
639
640 /* How to blink the default frame cursor off. */
641 Lisp_Object Vblink_cursor_alist;
642
643 /* A scratch glyph row with contents used for generating truncation
644 glyphs. Also used in direct_output_for_insert. */
645
646 #define MAX_SCRATCH_GLYPHS 100
647 struct glyph_row scratch_glyph_row;
648 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
649
650 /* Ascent and height of the last line processed by move_it_to. */
651
652 static int last_max_ascent, last_height;
653
654 /* Non-zero if there's a help-echo in the echo area. */
655
656 int help_echo_showing_p;
657
658 /* If >= 0, computed, exact values of mode-line and header-line height
659 to use in the macros CURRENT_MODE_LINE_HEIGHT and
660 CURRENT_HEADER_LINE_HEIGHT. */
661
662 int current_mode_line_height, current_header_line_height;
663
664 /* The maximum distance to look ahead for text properties. Values
665 that are too small let us call compute_char_face and similar
666 functions too often which is expensive. Values that are too large
667 let us call compute_char_face and alike too often because we
668 might not be interested in text properties that far away. */
669
670 #define TEXT_PROP_DISTANCE_LIMIT 100
671
672 #if GLYPH_DEBUG
673
674 /* Variables to turn off display optimizations from Lisp. */
675
676 int inhibit_try_window_id, inhibit_try_window_reusing;
677 int inhibit_try_cursor_movement;
678
679 /* Non-zero means print traces of redisplay if compiled with
680 GLYPH_DEBUG != 0. */
681
682 int trace_redisplay_p;
683
684 #endif /* GLYPH_DEBUG */
685
686 #ifdef DEBUG_TRACE_MOVE
687 /* Non-zero means trace with TRACE_MOVE to stderr. */
688 int trace_move;
689
690 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
691 #else
692 #define TRACE_MOVE(x) (void) 0
693 #endif
694
695 /* Non-zero means automatically scroll windows horizontally to make
696 point visible. */
697
698 int automatic_hscrolling_p;
699
700 /* How close to the margin can point get before the window is scrolled
701 horizontally. */
702 EMACS_INT hscroll_margin;
703
704 /* How much to scroll horizontally when point is inside the above margin. */
705 Lisp_Object Vhscroll_step;
706
707 /* The variable `resize-mini-windows'. If nil, don't resize
708 mini-windows. If t, always resize them to fit the text they
709 display. If `grow-only', let mini-windows grow only until they
710 become empty. */
711
712 Lisp_Object Vresize_mini_windows;
713
714 /* Buffer being redisplayed -- for redisplay_window_error. */
715
716 struct buffer *displayed_buffer;
717
718 /* Space between overline and text. */
719
720 EMACS_INT overline_margin;
721
722 /* Value returned from text property handlers (see below). */
723
724 enum prop_handled
725 {
726 HANDLED_NORMALLY,
727 HANDLED_RECOMPUTE_PROPS,
728 HANDLED_OVERLAY_STRING_CONSUMED,
729 HANDLED_RETURN
730 };
731
732 /* A description of text properties that redisplay is interested
733 in. */
734
735 struct props
736 {
737 /* The name of the property. */
738 Lisp_Object *name;
739
740 /* A unique index for the property. */
741 enum prop_idx idx;
742
743 /* A handler function called to set up iterator IT from the property
744 at IT's current position. Value is used to steer handle_stop. */
745 enum prop_handled (*handler) P_ ((struct it *it));
746 };
747
748 static enum prop_handled handle_face_prop P_ ((struct it *));
749 static enum prop_handled handle_invisible_prop P_ ((struct it *));
750 static enum prop_handled handle_display_prop P_ ((struct it *));
751 static enum prop_handled handle_composition_prop P_ ((struct it *));
752 static enum prop_handled handle_overlay_change P_ ((struct it *));
753 static enum prop_handled handle_fontified_prop P_ ((struct it *));
754
755 /* Properties handled by iterators. */
756
757 static struct props it_props[] =
758 {
759 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
760 /* Handle `face' before `display' because some sub-properties of
761 `display' need to know the face. */
762 {&Qface, FACE_PROP_IDX, handle_face_prop},
763 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
764 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
765 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
766 {NULL, 0, NULL}
767 };
768
769 /* Value is the position described by X. If X is a marker, value is
770 the marker_position of X. Otherwise, value is X. */
771
772 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
773
774 /* Enumeration returned by some move_it_.* functions internally. */
775
776 enum move_it_result
777 {
778 /* Not used. Undefined value. */
779 MOVE_UNDEFINED,
780
781 /* Move ended at the requested buffer position or ZV. */
782 MOVE_POS_MATCH_OR_ZV,
783
784 /* Move ended at the requested X pixel position. */
785 MOVE_X_REACHED,
786
787 /* Move within a line ended at the end of a line that must be
788 continued. */
789 MOVE_LINE_CONTINUED,
790
791 /* Move within a line ended at the end of a line that would
792 be displayed truncated. */
793 MOVE_LINE_TRUNCATED,
794
795 /* Move within a line ended at a line end. */
796 MOVE_NEWLINE_OR_CR
797 };
798
799 /* This counter is used to clear the face cache every once in a while
800 in redisplay_internal. It is incremented for each redisplay.
801 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
802 cleared. */
803
804 #define CLEAR_FACE_CACHE_COUNT 500
805 static int clear_face_cache_count;
806
807 /* Similarly for the image cache. */
808
809 #ifdef HAVE_WINDOW_SYSTEM
810 #define CLEAR_IMAGE_CACHE_COUNT 101
811 static int clear_image_cache_count;
812 #endif
813
814 /* Record the previous terminal frame we displayed. */
815
816 static struct frame *previous_terminal_frame;
817
818 /* Non-zero while redisplay_internal is in progress. */
819
820 int redisplaying_p;
821
822 /* Non-zero means don't free realized faces. Bound while freeing
823 realized faces is dangerous because glyph matrices might still
824 reference them. */
825
826 int inhibit_free_realized_faces;
827 Lisp_Object Qinhibit_free_realized_faces;
828
829 /* If a string, XTread_socket generates an event to display that string.
830 (The display is done in read_char.) */
831
832 Lisp_Object help_echo_string;
833 Lisp_Object help_echo_window;
834 Lisp_Object help_echo_object;
835 int help_echo_pos;
836
837 /* Temporary variable for XTread_socket. */
838
839 Lisp_Object previous_help_echo_string;
840
841 /* Null glyph slice */
842
843 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
844
845 \f
846 /* Function prototypes. */
847
848 static void setup_for_ellipsis P_ ((struct it *, int));
849 static void mark_window_display_accurate_1 P_ ((struct window *, int));
850 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
851 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
852 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
853 static int redisplay_mode_lines P_ ((Lisp_Object, int));
854 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
855
856 #if 0
857 static int invisible_text_between_p P_ ((struct it *, int, int));
858 #endif
859
860 static void pint2str P_ ((char *, int, int));
861 static void pint2hrstr P_ ((char *, int, int));
862 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
863 struct text_pos));
864 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
865 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
866 static void store_mode_line_noprop_char P_ ((char));
867 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
868 static void x_consider_frame_title P_ ((Lisp_Object));
869 static void handle_stop P_ ((struct it *));
870 static int tool_bar_lines_needed P_ ((struct frame *, int *));
871 static int single_display_spec_intangible_p P_ ((Lisp_Object));
872 static void ensure_echo_area_buffers P_ ((void));
873 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
874 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
875 static int with_echo_area_buffer P_ ((struct window *, int,
876 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
877 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static void clear_garbaged_frames P_ ((void));
879 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
881 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int display_echo_area P_ ((struct window *));
883 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
885 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
886 static int string_char_and_length P_ ((const unsigned char *, int, int *));
887 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
888 struct text_pos));
889 static int compute_window_start_on_continuation_line P_ ((struct window *));
890 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
891 static void insert_left_trunc_glyphs P_ ((struct it *));
892 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
893 Lisp_Object));
894 static void extend_face_to_end_of_line P_ ((struct it *));
895 static int append_space_for_newline P_ ((struct it *, int));
896 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
897 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
898 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
899 static int trailing_whitespace_p P_ ((int));
900 static int message_log_check_duplicate P_ ((int, int, int, int));
901 static void push_it P_ ((struct it *));
902 static void pop_it P_ ((struct it *));
903 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
904 static void select_frame_for_redisplay P_ ((Lisp_Object));
905 static void redisplay_internal P_ ((int));
906 static int echo_area_display P_ ((int));
907 static void redisplay_windows P_ ((Lisp_Object));
908 static void redisplay_window P_ ((Lisp_Object, int));
909 static Lisp_Object redisplay_window_error ();
910 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
911 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
912 static int update_menu_bar P_ ((struct frame *, int, int));
913 static int try_window_reusing_current_matrix P_ ((struct window *));
914 static int try_window_id P_ ((struct window *));
915 static int display_line P_ ((struct it *));
916 static int display_mode_lines P_ ((struct window *));
917 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
918 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
919 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
920 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
921 static void display_menu_bar P_ ((struct window *));
922 static int display_count_lines P_ ((int, int, int, int, int *));
923 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
924 int, int, struct it *, int, int, int, int));
925 static void compute_line_metrics P_ ((struct it *));
926 static void run_redisplay_end_trigger_hook P_ ((struct it *));
927 static int get_overlay_strings P_ ((struct it *, int));
928 static int get_overlay_strings_1 P_ ((struct it *, int, int));
929 static void next_overlay_string P_ ((struct it *));
930 static void reseat P_ ((struct it *, struct text_pos, int));
931 static void reseat_1 P_ ((struct it *, struct text_pos, int));
932 static void back_to_previous_visible_line_start P_ ((struct it *));
933 void reseat_at_previous_visible_line_start P_ ((struct it *));
934 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
935 static int next_element_from_ellipsis P_ ((struct it *));
936 static int next_element_from_display_vector P_ ((struct it *));
937 static int next_element_from_string P_ ((struct it *));
938 static int next_element_from_c_string P_ ((struct it *));
939 static int next_element_from_buffer P_ ((struct it *));
940 static int next_element_from_composition P_ ((struct it *));
941 static int next_element_from_image P_ ((struct it *));
942 static int next_element_from_stretch P_ ((struct it *));
943 static void load_overlay_strings P_ ((struct it *, int));
944 static int init_from_display_pos P_ ((struct it *, struct window *,
945 struct display_pos *));
946 static void reseat_to_string P_ ((struct it *, unsigned char *,
947 Lisp_Object, int, int, int, int));
948 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
949 int, int, int));
950 void move_it_vertically_backward P_ ((struct it *, int));
951 static void init_to_row_start P_ ((struct it *, struct window *,
952 struct glyph_row *));
953 static int init_to_row_end P_ ((struct it *, struct window *,
954 struct glyph_row *));
955 static void back_to_previous_line_start P_ ((struct it *));
956 static int forward_to_next_line_start P_ ((struct it *, int *));
957 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
958 Lisp_Object, int));
959 static struct text_pos string_pos P_ ((int, Lisp_Object));
960 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
961 static int number_of_chars P_ ((unsigned char *, int));
962 static void compute_stop_pos P_ ((struct it *));
963 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
964 Lisp_Object));
965 static int face_before_or_after_it_pos P_ ((struct it *, int));
966 static int next_overlay_change P_ ((int));
967 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
968 Lisp_Object, struct text_pos *,
969 int));
970 static int underlying_face_id P_ ((struct it *));
971 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
972 struct window *));
973
974 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
975 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
976
977 #ifdef HAVE_WINDOW_SYSTEM
978
979 static void update_tool_bar P_ ((struct frame *, int));
980 static void build_desired_tool_bar_string P_ ((struct frame *f));
981 static int redisplay_tool_bar P_ ((struct frame *));
982 static void display_tool_bar_line P_ ((struct it *, int));
983 static void notice_overwritten_cursor P_ ((struct window *,
984 enum glyph_row_area,
985 int, int, int, int));
986
987
988
989 #endif /* HAVE_WINDOW_SYSTEM */
990
991 \f
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
995
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
999
1000 This is the height of W minus the height of a mode line, if any. */
1001
1002 INLINE int
1003 window_text_bottom_y (w)
1004 struct window *w;
1005 {
1006 int height = WINDOW_TOTAL_HEIGHT (w);
1007
1008 if (WINDOW_WANTS_MODELINE_P (w))
1009 height -= CURRENT_MODE_LINE_HEIGHT (w);
1010 return height;
1011 }
1012
1013 /* Return the pixel width of display area AREA of window W. AREA < 0
1014 means return the total width of W, not including fringes to
1015 the left and right of the window. */
1016
1017 INLINE int
1018 window_box_width (w, area)
1019 struct window *w;
1020 int area;
1021 {
1022 int cols = XFASTINT (w->total_cols);
1023 int pixels = 0;
1024
1025 if (!w->pseudo_window_p)
1026 {
1027 cols -= WINDOW_SCROLL_BAR_COLS (w);
1028
1029 if (area == TEXT_AREA)
1030 {
1031 if (INTEGERP (w->left_margin_cols))
1032 cols -= XFASTINT (w->left_margin_cols);
1033 if (INTEGERP (w->right_margin_cols))
1034 cols -= XFASTINT (w->right_margin_cols);
1035 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1036 }
1037 else if (area == LEFT_MARGIN_AREA)
1038 {
1039 cols = (INTEGERP (w->left_margin_cols)
1040 ? XFASTINT (w->left_margin_cols) : 0);
1041 pixels = 0;
1042 }
1043 else if (area == RIGHT_MARGIN_AREA)
1044 {
1045 cols = (INTEGERP (w->right_margin_cols)
1046 ? XFASTINT (w->right_margin_cols) : 0);
1047 pixels = 0;
1048 }
1049 }
1050
1051 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1052 }
1053
1054
1055 /* Return the pixel height of the display area of window W, not
1056 including mode lines of W, if any. */
1057
1058 INLINE int
1059 window_box_height (w)
1060 struct window *w;
1061 {
1062 struct frame *f = XFRAME (w->frame);
1063 int height = WINDOW_TOTAL_HEIGHT (w);
1064
1065 xassert (height >= 0);
1066
1067 /* Note: the code below that determines the mode-line/header-line
1068 height is essentially the same as that contained in the macro
1069 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1070 the appropriate glyph row has its `mode_line_p' flag set,
1071 and if it doesn't, uses estimate_mode_line_height instead. */
1072
1073 if (WINDOW_WANTS_MODELINE_P (w))
1074 {
1075 struct glyph_row *ml_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (ml_row && ml_row->mode_line_p)
1080 height -= ml_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1083 }
1084
1085 if (WINDOW_WANTS_HEADER_LINE_P (w))
1086 {
1087 struct glyph_row *hl_row
1088 = (w->current_matrix && w->current_matrix->rows
1089 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1090 : 0);
1091 if (hl_row && hl_row->mode_line_p)
1092 height -= hl_row->height;
1093 else
1094 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1095 }
1096
1097 /* With a very small font and a mode-line that's taller than
1098 default, we might end up with a negative height. */
1099 return max (0, height);
1100 }
1101
1102 /* Return the window-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1105
1106 INLINE int
1107 window_box_left_offset (w, area)
1108 struct window *w;
1109 int area;
1110 {
1111 int x;
1112
1113 if (w->pseudo_window_p)
1114 return 0;
1115
1116 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1117
1118 if (area == TEXT_AREA)
1119 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1120 + window_box_width (w, LEFT_MARGIN_AREA));
1121 else if (area == RIGHT_MARGIN_AREA)
1122 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1123 + window_box_width (w, LEFT_MARGIN_AREA)
1124 + window_box_width (w, TEXT_AREA)
1125 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1126 ? 0
1127 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1128 else if (area == LEFT_MARGIN_AREA
1129 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1130 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1131
1132 return x;
1133 }
1134
1135
1136 /* Return the window-relative coordinate of the right edge of display
1137 area AREA of window W. AREA < 0 means return the left edge of the
1138 whole window, to the left of the right fringe of W. */
1139
1140 INLINE int
1141 window_box_right_offset (w, area)
1142 struct window *w;
1143 int area;
1144 {
1145 return window_box_left_offset (w, area) + window_box_width (w, area);
1146 }
1147
1148 /* Return the frame-relative coordinate of the left edge of display
1149 area AREA of window W. AREA < 0 means return the left edge of the
1150 whole window, to the right of the left fringe of W. */
1151
1152 INLINE int
1153 window_box_left (w, area)
1154 struct window *w;
1155 int area;
1156 {
1157 struct frame *f = XFRAME (w->frame);
1158 int x;
1159
1160 if (w->pseudo_window_p)
1161 return FRAME_INTERNAL_BORDER_WIDTH (f);
1162
1163 x = (WINDOW_LEFT_EDGE_X (w)
1164 + window_box_left_offset (w, area));
1165
1166 return x;
1167 }
1168
1169
1170 /* Return the frame-relative coordinate of the right edge of display
1171 area AREA of window W. AREA < 0 means return the left edge of the
1172 whole window, to the left of the right fringe of W. */
1173
1174 INLINE int
1175 window_box_right (w, area)
1176 struct window *w;
1177 int area;
1178 {
1179 return window_box_left (w, area) + window_box_width (w, area);
1180 }
1181
1182 /* Get the bounding box of the display area AREA of window W, without
1183 mode lines, in frame-relative coordinates. AREA < 0 means the
1184 whole window, not including the left and right fringes of
1185 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1186 coordinates of the upper-left corner of the box. Return in
1187 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1188
1189 INLINE void
1190 window_box (w, area, box_x, box_y, box_width, box_height)
1191 struct window *w;
1192 int area;
1193 int *box_x, *box_y, *box_width, *box_height;
1194 {
1195 if (box_width)
1196 *box_width = window_box_width (w, area);
1197 if (box_height)
1198 *box_height = window_box_height (w);
1199 if (box_x)
1200 *box_x = window_box_left (w, area);
1201 if (box_y)
1202 {
1203 *box_y = WINDOW_TOP_EDGE_Y (w);
1204 if (WINDOW_WANTS_HEADER_LINE_P (w))
1205 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1206 }
1207 }
1208
1209
1210 /* Get the bounding box of the display area AREA of window W, without
1211 mode lines. AREA < 0 means the whole window, not including the
1212 left and right fringe of the window. Return in *TOP_LEFT_X
1213 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1214 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1215 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1216 box. */
1217
1218 INLINE void
1219 window_box_edges (w, area, top_left_x, top_left_y,
1220 bottom_right_x, bottom_right_y)
1221 struct window *w;
1222 int area;
1223 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1224 {
1225 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1226 bottom_right_y);
1227 *bottom_right_x += *top_left_x;
1228 *bottom_right_y += *top_left_y;
1229 }
1230
1231
1232 \f
1233 /***********************************************************************
1234 Utilities
1235 ***********************************************************************/
1236
1237 /* Return the bottom y-position of the line the iterator IT is in.
1238 This can modify IT's settings. */
1239
1240 int
1241 line_bottom_y (it)
1242 struct it *it;
1243 {
1244 int line_height = it->max_ascent + it->max_descent;
1245 int line_top_y = it->current_y;
1246
1247 if (line_height == 0)
1248 {
1249 if (last_height)
1250 line_height = last_height;
1251 else if (IT_CHARPOS (*it) < ZV)
1252 {
1253 move_it_by_lines (it, 1, 1);
1254 line_height = (it->max_ascent || it->max_descent
1255 ? it->max_ascent + it->max_descent
1256 : last_height);
1257 }
1258 else
1259 {
1260 struct glyph_row *row = it->glyph_row;
1261
1262 /* Use the default character height. */
1263 it->glyph_row = NULL;
1264 it->what = IT_CHARACTER;
1265 it->c = ' ';
1266 it->len = 1;
1267 PRODUCE_GLYPHS (it);
1268 line_height = it->ascent + it->descent;
1269 it->glyph_row = row;
1270 }
1271 }
1272
1273 return line_top_y + line_height;
1274 }
1275
1276
1277 /* Return 1 if position CHARPOS is visible in window W.
1278 CHARPOS < 0 means return info about WINDOW_END position.
1279 If visible, set *X and *Y to pixel coordinates of top left corner.
1280 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1281 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1282
1283 int
1284 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1285 struct window *w;
1286 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1287 {
1288 struct it it;
1289 struct text_pos top;
1290 int visible_p = 0;
1291 struct buffer *old_buffer = NULL;
1292
1293 if (noninteractive)
1294 return visible_p;
1295
1296 if (XBUFFER (w->buffer) != current_buffer)
1297 {
1298 old_buffer = current_buffer;
1299 set_buffer_internal_1 (XBUFFER (w->buffer));
1300 }
1301
1302 SET_TEXT_POS_FROM_MARKER (top, w->start);
1303
1304 /* Compute exact mode line heights. */
1305 if (WINDOW_WANTS_MODELINE_P (w))
1306 current_mode_line_height
1307 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1308 current_buffer->mode_line_format);
1309
1310 if (WINDOW_WANTS_HEADER_LINE_P (w))
1311 current_header_line_height
1312 = display_mode_line (w, HEADER_LINE_FACE_ID,
1313 current_buffer->header_line_format);
1314
1315 start_display (&it, w, top);
1316 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1317 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318
1319 /* Note that we may overshoot because of invisible text. */
1320 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1321 {
1322 int top_x = it.current_x;
1323 int top_y = it.current_y;
1324 int bottom_y = (last_height = 0, line_bottom_y (&it));
1325 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1326
1327 if (top_y < window_top_y)
1328 visible_p = bottom_y > window_top_y;
1329 else if (top_y < it.last_visible_y)
1330 visible_p = 1;
1331 if (visible_p)
1332 {
1333 *x = top_x;
1334 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1335 *rtop = max (0, window_top_y - top_y);
1336 *rbot = max (0, bottom_y - it.last_visible_y);
1337 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1338 - max (top_y, window_top_y)));
1339 *vpos = it.vpos;
1340 }
1341 }
1342 else
1343 {
1344 struct it it2;
1345
1346 it2 = it;
1347 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1348 move_it_by_lines (&it, 1, 0);
1349 if (charpos < IT_CHARPOS (it)
1350 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1351 {
1352 visible_p = 1;
1353 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1354 *x = it2.current_x;
1355 *y = it2.current_y + it2.max_ascent - it2.ascent;
1356 *rtop = max (0, -it2.current_y);
1357 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1358 - it.last_visible_y));
1359 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1360 it.last_visible_y)
1361 - max (it2.current_y,
1362 WINDOW_HEADER_LINE_HEIGHT (w))));
1363 *vpos = it2.vpos;
1364 }
1365 }
1366
1367 if (old_buffer)
1368 set_buffer_internal_1 (old_buffer);
1369
1370 current_header_line_height = current_mode_line_height = -1;
1371
1372 if (visible_p && XFASTINT (w->hscroll) > 0)
1373 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1374
1375 #if 0
1376 /* Debugging code. */
1377 if (visible_p)
1378 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1379 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1380 else
1381 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1382 #endif
1383
1384 return visible_p;
1385 }
1386
1387
1388 /* Return the next character from STR which is MAXLEN bytes long.
1389 Return in *LEN the length of the character. This is like
1390 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1391 we find one, we return a `?', but with the length of the invalid
1392 character. */
1393
1394 static INLINE int
1395 string_char_and_length (str, maxlen, len)
1396 const unsigned char *str;
1397 int maxlen, *len;
1398 {
1399 int c;
1400
1401 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1402 if (!CHAR_VALID_P (c, 1))
1403 /* We may not change the length here because other places in Emacs
1404 don't use this function, i.e. they silently accept invalid
1405 characters. */
1406 c = '?';
1407
1408 return c;
1409 }
1410
1411
1412
1413 /* Given a position POS containing a valid character and byte position
1414 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1415
1416 static struct text_pos
1417 string_pos_nchars_ahead (pos, string, nchars)
1418 struct text_pos pos;
1419 Lisp_Object string;
1420 int nchars;
1421 {
1422 xassert (STRINGP (string) && nchars >= 0);
1423
1424 if (STRING_MULTIBYTE (string))
1425 {
1426 int rest = SBYTES (string) - BYTEPOS (pos);
1427 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1428 int len;
1429
1430 while (nchars--)
1431 {
1432 string_char_and_length (p, rest, &len);
1433 p += len, rest -= len;
1434 xassert (rest >= 0);
1435 CHARPOS (pos) += 1;
1436 BYTEPOS (pos) += len;
1437 }
1438 }
1439 else
1440 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1441
1442 return pos;
1443 }
1444
1445
1446 /* Value is the text position, i.e. character and byte position,
1447 for character position CHARPOS in STRING. */
1448
1449 static INLINE struct text_pos
1450 string_pos (charpos, string)
1451 int charpos;
1452 Lisp_Object string;
1453 {
1454 struct text_pos pos;
1455 xassert (STRINGP (string));
1456 xassert (charpos >= 0);
1457 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1458 return pos;
1459 }
1460
1461
1462 /* Value is a text position, i.e. character and byte position, for
1463 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1464 means recognize multibyte characters. */
1465
1466 static struct text_pos
1467 c_string_pos (charpos, s, multibyte_p)
1468 int charpos;
1469 unsigned char *s;
1470 int multibyte_p;
1471 {
1472 struct text_pos pos;
1473
1474 xassert (s != NULL);
1475 xassert (charpos >= 0);
1476
1477 if (multibyte_p)
1478 {
1479 int rest = strlen (s), len;
1480
1481 SET_TEXT_POS (pos, 0, 0);
1482 while (charpos--)
1483 {
1484 string_char_and_length (s, rest, &len);
1485 s += len, rest -= len;
1486 xassert (rest >= 0);
1487 CHARPOS (pos) += 1;
1488 BYTEPOS (pos) += len;
1489 }
1490 }
1491 else
1492 SET_TEXT_POS (pos, charpos, charpos);
1493
1494 return pos;
1495 }
1496
1497
1498 /* Value is the number of characters in C string S. MULTIBYTE_P
1499 non-zero means recognize multibyte characters. */
1500
1501 static int
1502 number_of_chars (s, multibyte_p)
1503 unsigned char *s;
1504 int multibyte_p;
1505 {
1506 int nchars;
1507
1508 if (multibyte_p)
1509 {
1510 int rest = strlen (s), len;
1511 unsigned char *p = (unsigned char *) s;
1512
1513 for (nchars = 0; rest > 0; ++nchars)
1514 {
1515 string_char_and_length (p, rest, &len);
1516 rest -= len, p += len;
1517 }
1518 }
1519 else
1520 nchars = strlen (s);
1521
1522 return nchars;
1523 }
1524
1525
1526 /* Compute byte position NEWPOS->bytepos corresponding to
1527 NEWPOS->charpos. POS is a known position in string STRING.
1528 NEWPOS->charpos must be >= POS.charpos. */
1529
1530 static void
1531 compute_string_pos (newpos, pos, string)
1532 struct text_pos *newpos, pos;
1533 Lisp_Object string;
1534 {
1535 xassert (STRINGP (string));
1536 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1537
1538 if (STRING_MULTIBYTE (string))
1539 *newpos = string_pos_nchars_ahead (pos, string,
1540 CHARPOS (*newpos) - CHARPOS (pos));
1541 else
1542 BYTEPOS (*newpos) = CHARPOS (*newpos);
1543 }
1544
1545 /* EXPORT:
1546 Return an estimation of the pixel height of mode or top lines on
1547 frame F. FACE_ID specifies what line's height to estimate. */
1548
1549 int
1550 estimate_mode_line_height (f, face_id)
1551 struct frame *f;
1552 enum face_id face_id;
1553 {
1554 #ifdef HAVE_WINDOW_SYSTEM
1555 if (FRAME_WINDOW_P (f))
1556 {
1557 int height = FONT_HEIGHT (FRAME_FONT (f));
1558
1559 /* This function is called so early when Emacs starts that the face
1560 cache and mode line face are not yet initialized. */
1561 if (FRAME_FACE_CACHE (f))
1562 {
1563 struct face *face = FACE_FROM_ID (f, face_id);
1564 if (face)
1565 {
1566 if (face->font)
1567 height = FONT_HEIGHT (face->font);
1568 if (face->box_line_width > 0)
1569 height += 2 * face->box_line_width;
1570 }
1571 }
1572
1573 return height;
1574 }
1575 #endif
1576
1577 return 1;
1578 }
1579
1580 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1581 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1582 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1583 not force the value into range. */
1584
1585 void
1586 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1587 FRAME_PTR f;
1588 register int pix_x, pix_y;
1589 int *x, *y;
1590 NativeRectangle *bounds;
1591 int noclip;
1592 {
1593
1594 #ifdef HAVE_WINDOW_SYSTEM
1595 if (FRAME_WINDOW_P (f))
1596 {
1597 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1598 even for negative values. */
1599 if (pix_x < 0)
1600 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1601 if (pix_y < 0)
1602 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1603
1604 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1605 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1606
1607 if (bounds)
1608 STORE_NATIVE_RECT (*bounds,
1609 FRAME_COL_TO_PIXEL_X (f, pix_x),
1610 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1611 FRAME_COLUMN_WIDTH (f) - 1,
1612 FRAME_LINE_HEIGHT (f) - 1);
1613
1614 if (!noclip)
1615 {
1616 if (pix_x < 0)
1617 pix_x = 0;
1618 else if (pix_x > FRAME_TOTAL_COLS (f))
1619 pix_x = FRAME_TOTAL_COLS (f);
1620
1621 if (pix_y < 0)
1622 pix_y = 0;
1623 else if (pix_y > FRAME_LINES (f))
1624 pix_y = FRAME_LINES (f);
1625 }
1626 }
1627 #endif
1628
1629 *x = pix_x;
1630 *y = pix_y;
1631 }
1632
1633
1634 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1635 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1636 can't tell the positions because W's display is not up to date,
1637 return 0. */
1638
1639 int
1640 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1641 struct window *w;
1642 int hpos, vpos;
1643 int *frame_x, *frame_y;
1644 {
1645 #ifdef HAVE_WINDOW_SYSTEM
1646 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1647 {
1648 int success_p;
1649
1650 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1651 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1652
1653 if (display_completed)
1654 {
1655 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1656 struct glyph *glyph = row->glyphs[TEXT_AREA];
1657 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1658
1659 hpos = row->x;
1660 vpos = row->y;
1661 while (glyph < end)
1662 {
1663 hpos += glyph->pixel_width;
1664 ++glyph;
1665 }
1666
1667 /* If first glyph is partially visible, its first visible position is still 0. */
1668 if (hpos < 0)
1669 hpos = 0;
1670
1671 success_p = 1;
1672 }
1673 else
1674 {
1675 hpos = vpos = 0;
1676 success_p = 0;
1677 }
1678
1679 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1680 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1681 return success_p;
1682 }
1683 #endif
1684
1685 *frame_x = hpos;
1686 *frame_y = vpos;
1687 return 1;
1688 }
1689
1690
1691 #ifdef HAVE_WINDOW_SYSTEM
1692
1693 /* Find the glyph under window-relative coordinates X/Y in window W.
1694 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1695 strings. Return in *HPOS and *VPOS the row and column number of
1696 the glyph found. Return in *AREA the glyph area containing X.
1697 Value is a pointer to the glyph found or null if X/Y is not on
1698 text, or we can't tell because W's current matrix is not up to
1699 date. */
1700
1701 static struct glyph *
1702 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1703 struct window *w;
1704 int x, y;
1705 int *hpos, *vpos, *dx, *dy, *area;
1706 {
1707 struct glyph *glyph, *end;
1708 struct glyph_row *row = NULL;
1709 int x0, i;
1710
1711 /* Find row containing Y. Give up if some row is not enabled. */
1712 for (i = 0; i < w->current_matrix->nrows; ++i)
1713 {
1714 row = MATRIX_ROW (w->current_matrix, i);
1715 if (!row->enabled_p)
1716 return NULL;
1717 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1718 break;
1719 }
1720
1721 *vpos = i;
1722 *hpos = 0;
1723
1724 /* Give up if Y is not in the window. */
1725 if (i == w->current_matrix->nrows)
1726 return NULL;
1727
1728 /* Get the glyph area containing X. */
1729 if (w->pseudo_window_p)
1730 {
1731 *area = TEXT_AREA;
1732 x0 = 0;
1733 }
1734 else
1735 {
1736 if (x < window_box_left_offset (w, TEXT_AREA))
1737 {
1738 *area = LEFT_MARGIN_AREA;
1739 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1740 }
1741 else if (x < window_box_right_offset (w, TEXT_AREA))
1742 {
1743 *area = TEXT_AREA;
1744 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1745 }
1746 else
1747 {
1748 *area = RIGHT_MARGIN_AREA;
1749 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1750 }
1751 }
1752
1753 /* Find glyph containing X. */
1754 glyph = row->glyphs[*area];
1755 end = glyph + row->used[*area];
1756 x -= x0;
1757 while (glyph < end && x >= glyph->pixel_width)
1758 {
1759 x -= glyph->pixel_width;
1760 ++glyph;
1761 }
1762
1763 if (glyph == end)
1764 return NULL;
1765
1766 if (dx)
1767 {
1768 *dx = x;
1769 *dy = y - (row->y + row->ascent - glyph->ascent);
1770 }
1771
1772 *hpos = glyph - row->glyphs[*area];
1773 return glyph;
1774 }
1775
1776
1777 /* EXPORT:
1778 Convert frame-relative x/y to coordinates relative to window W.
1779 Takes pseudo-windows into account. */
1780
1781 void
1782 frame_to_window_pixel_xy (w, x, y)
1783 struct window *w;
1784 int *x, *y;
1785 {
1786 if (w->pseudo_window_p)
1787 {
1788 /* A pseudo-window is always full-width, and starts at the
1789 left edge of the frame, plus a frame border. */
1790 struct frame *f = XFRAME (w->frame);
1791 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1792 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1793 }
1794 else
1795 {
1796 *x -= WINDOW_LEFT_EDGE_X (w);
1797 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1798 }
1799 }
1800
1801 /* EXPORT:
1802 Return in RECTS[] at most N clipping rectangles for glyph string S.
1803 Return the number of stored rectangles. */
1804
1805 int
1806 get_glyph_string_clip_rects (s, rects, n)
1807 struct glyph_string *s;
1808 NativeRectangle *rects;
1809 int n;
1810 {
1811 XRectangle r;
1812
1813 if (n <= 0)
1814 return 0;
1815
1816 if (s->row->full_width_p)
1817 {
1818 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1819 r.x = WINDOW_LEFT_EDGE_X (s->w);
1820 r.width = WINDOW_TOTAL_WIDTH (s->w);
1821
1822 /* Unless displaying a mode or menu bar line, which are always
1823 fully visible, clip to the visible part of the row. */
1824 if (s->w->pseudo_window_p)
1825 r.height = s->row->visible_height;
1826 else
1827 r.height = s->height;
1828 }
1829 else
1830 {
1831 /* This is a text line that may be partially visible. */
1832 r.x = window_box_left (s->w, s->area);
1833 r.width = window_box_width (s->w, s->area);
1834 r.height = s->row->visible_height;
1835 }
1836
1837 if (s->clip_head)
1838 if (r.x < s->clip_head->x)
1839 {
1840 if (r.width >= s->clip_head->x - r.x)
1841 r.width -= s->clip_head->x - r.x;
1842 else
1843 r.width = 0;
1844 r.x = s->clip_head->x;
1845 }
1846 if (s->clip_tail)
1847 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1848 {
1849 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1850 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1851 else
1852 r.width = 0;
1853 }
1854
1855 /* If S draws overlapping rows, it's sufficient to use the top and
1856 bottom of the window for clipping because this glyph string
1857 intentionally draws over other lines. */
1858 if (s->for_overlaps)
1859 {
1860 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1861 r.height = window_text_bottom_y (s->w) - r.y;
1862
1863 /* Alas, the above simple strategy does not work for the
1864 environments with anti-aliased text: if the same text is
1865 drawn onto the same place multiple times, it gets thicker.
1866 If the overlap we are processing is for the erased cursor, we
1867 take the intersection with the rectagle of the cursor. */
1868 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1869 {
1870 XRectangle rc, r_save = r;
1871
1872 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1873 rc.y = s->w->phys_cursor.y;
1874 rc.width = s->w->phys_cursor_width;
1875 rc.height = s->w->phys_cursor_height;
1876
1877 x_intersect_rectangles (&r_save, &rc, &r);
1878 }
1879 }
1880 else
1881 {
1882 /* Don't use S->y for clipping because it doesn't take partially
1883 visible lines into account. For example, it can be negative for
1884 partially visible lines at the top of a window. */
1885 if (!s->row->full_width_p
1886 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1887 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1888 else
1889 r.y = max (0, s->row->y);
1890
1891 /* If drawing a tool-bar window, draw it over the internal border
1892 at the top of the window. */
1893 if (WINDOWP (s->f->tool_bar_window)
1894 && s->w == XWINDOW (s->f->tool_bar_window))
1895 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1896 }
1897
1898 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1899
1900 /* If drawing the cursor, don't let glyph draw outside its
1901 advertised boundaries. Cleartype does this under some circumstances. */
1902 if (s->hl == DRAW_CURSOR)
1903 {
1904 struct glyph *glyph = s->first_glyph;
1905 int height, max_y;
1906
1907 if (s->x > r.x)
1908 {
1909 r.width -= s->x - r.x;
1910 r.x = s->x;
1911 }
1912 r.width = min (r.width, glyph->pixel_width);
1913
1914 /* If r.y is below window bottom, ensure that we still see a cursor. */
1915 height = min (glyph->ascent + glyph->descent,
1916 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1917 max_y = window_text_bottom_y (s->w) - height;
1918 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1919 if (s->ybase - glyph->ascent > max_y)
1920 {
1921 r.y = max_y;
1922 r.height = height;
1923 }
1924 else
1925 {
1926 /* Don't draw cursor glyph taller than our actual glyph. */
1927 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1928 if (height < r.height)
1929 {
1930 max_y = r.y + r.height;
1931 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1932 r.height = min (max_y - r.y, height);
1933 }
1934 }
1935 }
1936
1937 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1938 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1939 {
1940 #ifdef CONVERT_FROM_XRECT
1941 CONVERT_FROM_XRECT (r, *rects);
1942 #else
1943 *rects = r;
1944 #endif
1945 return 1;
1946 }
1947 else
1948 {
1949 /* If we are processing overlapping and allowed to return
1950 multiple clipping rectangles, we exclude the row of the glyph
1951 string from the clipping rectangle. This is to avoid drawing
1952 the same text on the environment with anti-aliasing. */
1953 #ifdef CONVERT_FROM_XRECT
1954 XRectangle rs[2];
1955 #else
1956 XRectangle *rs = rects;
1957 #endif
1958 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1959
1960 if (s->for_overlaps & OVERLAPS_PRED)
1961 {
1962 rs[i] = r;
1963 if (r.y + r.height > row_y)
1964 {
1965 if (r.y < row_y)
1966 rs[i].height = row_y - r.y;
1967 else
1968 rs[i].height = 0;
1969 }
1970 i++;
1971 }
1972 if (s->for_overlaps & OVERLAPS_SUCC)
1973 {
1974 rs[i] = r;
1975 if (r.y < row_y + s->row->visible_height)
1976 {
1977 if (r.y + r.height > row_y + s->row->visible_height)
1978 {
1979 rs[i].y = row_y + s->row->visible_height;
1980 rs[i].height = r.y + r.height - rs[i].y;
1981 }
1982 else
1983 rs[i].height = 0;
1984 }
1985 i++;
1986 }
1987
1988 n = i;
1989 #ifdef CONVERT_FROM_XRECT
1990 for (i = 0; i < n; i++)
1991 CONVERT_FROM_XRECT (rs[i], rects[i]);
1992 #endif
1993 return n;
1994 }
1995 }
1996
1997 /* EXPORT:
1998 Return in *NR the clipping rectangle for glyph string S. */
1999
2000 void
2001 get_glyph_string_clip_rect (s, nr)
2002 struct glyph_string *s;
2003 NativeRectangle *nr;
2004 {
2005 get_glyph_string_clip_rects (s, nr, 1);
2006 }
2007
2008
2009 /* EXPORT:
2010 Return the position and height of the phys cursor in window W.
2011 Set w->phys_cursor_width to width of phys cursor.
2012 */
2013
2014 void
2015 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2016 struct window *w;
2017 struct glyph_row *row;
2018 struct glyph *glyph;
2019 int *xp, *yp, *heightp;
2020 {
2021 struct frame *f = XFRAME (WINDOW_FRAME (w));
2022 int x, y, wd, h, h0, y0;
2023
2024 /* Compute the width of the rectangle to draw. If on a stretch
2025 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2026 rectangle as wide as the glyph, but use a canonical character
2027 width instead. */
2028 wd = glyph->pixel_width - 1;
2029 #ifdef HAVE_NTGUI
2030 wd++; /* Why? */
2031 #endif
2032
2033 x = w->phys_cursor.x;
2034 if (x < 0)
2035 {
2036 wd += x;
2037 x = 0;
2038 }
2039
2040 if (glyph->type == STRETCH_GLYPH
2041 && !x_stretch_cursor_p)
2042 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2043 w->phys_cursor_width = wd;
2044
2045 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2046
2047 /* If y is below window bottom, ensure that we still see a cursor. */
2048 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2049
2050 h = max (h0, glyph->ascent + glyph->descent);
2051 h0 = min (h0, glyph->ascent + glyph->descent);
2052
2053 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2054 if (y < y0)
2055 {
2056 h = max (h - (y0 - y) + 1, h0);
2057 y = y0 - 1;
2058 }
2059 else
2060 {
2061 y0 = window_text_bottom_y (w) - h0;
2062 if (y > y0)
2063 {
2064 h += y - y0;
2065 y = y0;
2066 }
2067 }
2068
2069 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2070 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2071 *heightp = h;
2072 }
2073
2074 /*
2075 * Remember which glyph the mouse is over.
2076 */
2077
2078 void
2079 remember_mouse_glyph (f, gx, gy, rect)
2080 struct frame *f;
2081 int gx, gy;
2082 NativeRectangle *rect;
2083 {
2084 Lisp_Object window = Qnil;
2085 struct window *w;
2086 struct glyph_row *r, *gr, *end_row;
2087 enum window_part part;
2088 enum glyph_row_area area;
2089 int x, y, width, height;
2090
2091 /* Try to determine frame pixel position and size of the glyph under
2092 frame pixel coordinates X/Y on frame F. */
2093
2094 if (f->glyphs_initialized_p)
2095 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2096
2097 if (NILP (window))
2098 {
2099 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2100 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2101 goto virtual_glyph;
2102 }
2103
2104 w = XWINDOW (window);
2105 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2106 height = WINDOW_FRAME_LINE_HEIGHT (w);
2107
2108 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2109 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2110
2111 if (w->pseudo_window_p)
2112 {
2113 area = TEXT_AREA;
2114 part = ON_MODE_LINE; /* Don't adjust margin. */
2115 goto text_glyph;
2116 }
2117
2118 switch (part)
2119 {
2120 case ON_LEFT_MARGIN:
2121 area = LEFT_MARGIN_AREA;
2122 goto text_glyph;
2123
2124 case ON_RIGHT_MARGIN:
2125 area = RIGHT_MARGIN_AREA;
2126 goto text_glyph;
2127
2128 case ON_HEADER_LINE:
2129 case ON_MODE_LINE:
2130 gr = (part == ON_HEADER_LINE
2131 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2132 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2133 gy = gr->y;
2134 area = TEXT_AREA;
2135 goto text_glyph_row_found;
2136
2137 case ON_TEXT:
2138 area = TEXT_AREA;
2139
2140 text_glyph:
2141 gr = 0; gy = 0;
2142 for (; r <= end_row && r->enabled_p; ++r)
2143 if (r->y + r->height > y)
2144 {
2145 gr = r; gy = r->y;
2146 break;
2147 }
2148
2149 text_glyph_row_found:
2150 if (gr && gy <= y)
2151 {
2152 struct glyph *g = gr->glyphs[area];
2153 struct glyph *end = g + gr->used[area];
2154
2155 height = gr->height;
2156 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2157 if (gx + g->pixel_width > x)
2158 break;
2159
2160 if (g < end)
2161 {
2162 if (g->type == IMAGE_GLYPH)
2163 {
2164 /* Don't remember when mouse is over image, as
2165 image may have hot-spots. */
2166 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2167 return;
2168 }
2169 width = g->pixel_width;
2170 }
2171 else
2172 {
2173 /* Use nominal char spacing at end of line. */
2174 x -= gx;
2175 gx += (x / width) * width;
2176 }
2177
2178 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2179 gx += window_box_left_offset (w, area);
2180 }
2181 else
2182 {
2183 /* Use nominal line height at end of window. */
2184 gx = (x / width) * width;
2185 y -= gy;
2186 gy += (y / height) * height;
2187 }
2188 break;
2189
2190 case ON_LEFT_FRINGE:
2191 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2192 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2193 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2194 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2195 goto row_glyph;
2196
2197 case ON_RIGHT_FRINGE:
2198 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2199 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2200 : window_box_right_offset (w, TEXT_AREA));
2201 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2202 goto row_glyph;
2203
2204 case ON_SCROLL_BAR:
2205 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2206 ? 0
2207 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2208 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2209 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2210 : 0)));
2211 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2212
2213 row_glyph:
2214 gr = 0, gy = 0;
2215 for (; r <= end_row && r->enabled_p; ++r)
2216 if (r->y + r->height > y)
2217 {
2218 gr = r; gy = r->y;
2219 break;
2220 }
2221
2222 if (gr && gy <= y)
2223 height = gr->height;
2224 else
2225 {
2226 /* Use nominal line height at end of window. */
2227 y -= gy;
2228 gy += (y / height) * height;
2229 }
2230 break;
2231
2232 default:
2233 ;
2234 virtual_glyph:
2235 /* If there is no glyph under the mouse, then we divide the screen
2236 into a grid of the smallest glyph in the frame, and use that
2237 as our "glyph". */
2238
2239 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2240 round down even for negative values. */
2241 if (gx < 0)
2242 gx -= width - 1;
2243 if (gy < 0)
2244 gy -= height - 1;
2245
2246 gx = (gx / width) * width;
2247 gy = (gy / height) * height;
2248
2249 goto store_rect;
2250 }
2251
2252 gx += WINDOW_LEFT_EDGE_X (w);
2253 gy += WINDOW_TOP_EDGE_Y (w);
2254
2255 store_rect:
2256 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2257
2258 /* Visible feedback for debugging. */
2259 #if 0
2260 #if HAVE_X_WINDOWS
2261 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2262 f->output_data.x->normal_gc,
2263 gx, gy, width, height);
2264 #endif
2265 #endif
2266 }
2267
2268
2269 #endif /* HAVE_WINDOW_SYSTEM */
2270
2271 \f
2272 /***********************************************************************
2273 Lisp form evaluation
2274 ***********************************************************************/
2275
2276 /* Error handler for safe_eval and safe_call. */
2277
2278 static Lisp_Object
2279 safe_eval_handler (arg)
2280 Lisp_Object arg;
2281 {
2282 add_to_log ("Error during redisplay: %s", arg, Qnil);
2283 return Qnil;
2284 }
2285
2286
2287 /* Evaluate SEXPR and return the result, or nil if something went
2288 wrong. Prevent redisplay during the evaluation. */
2289
2290 Lisp_Object
2291 safe_eval (sexpr)
2292 Lisp_Object sexpr;
2293 {
2294 Lisp_Object val;
2295
2296 if (inhibit_eval_during_redisplay)
2297 val = Qnil;
2298 else
2299 {
2300 int count = SPECPDL_INDEX ();
2301 struct gcpro gcpro1;
2302
2303 GCPRO1 (sexpr);
2304 specbind (Qinhibit_redisplay, Qt);
2305 /* Use Qt to ensure debugger does not run,
2306 so there is no possibility of wanting to redisplay. */
2307 val = internal_condition_case_1 (Feval, sexpr, Qt,
2308 safe_eval_handler);
2309 UNGCPRO;
2310 val = unbind_to (count, val);
2311 }
2312
2313 return val;
2314 }
2315
2316
2317 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2318 Return the result, or nil if something went wrong. Prevent
2319 redisplay during the evaluation. */
2320
2321 Lisp_Object
2322 safe_call (nargs, args)
2323 int nargs;
2324 Lisp_Object *args;
2325 {
2326 Lisp_Object val;
2327
2328 if (inhibit_eval_during_redisplay)
2329 val = Qnil;
2330 else
2331 {
2332 int count = SPECPDL_INDEX ();
2333 struct gcpro gcpro1;
2334
2335 GCPRO1 (args[0]);
2336 gcpro1.nvars = nargs;
2337 specbind (Qinhibit_redisplay, Qt);
2338 /* Use Qt to ensure debugger does not run,
2339 so there is no possibility of wanting to redisplay. */
2340 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2341 safe_eval_handler);
2342 UNGCPRO;
2343 val = unbind_to (count, val);
2344 }
2345
2346 return val;
2347 }
2348
2349
2350 /* Call function FN with one argument ARG.
2351 Return the result, or nil if something went wrong. */
2352
2353 Lisp_Object
2354 safe_call1 (fn, arg)
2355 Lisp_Object fn, arg;
2356 {
2357 Lisp_Object args[2];
2358 args[0] = fn;
2359 args[1] = arg;
2360 return safe_call (2, args);
2361 }
2362
2363
2364 \f
2365 /***********************************************************************
2366 Debugging
2367 ***********************************************************************/
2368
2369 #if 0
2370
2371 /* Define CHECK_IT to perform sanity checks on iterators.
2372 This is for debugging. It is too slow to do unconditionally. */
2373
2374 static void
2375 check_it (it)
2376 struct it *it;
2377 {
2378 if (it->method == GET_FROM_STRING)
2379 {
2380 xassert (STRINGP (it->string));
2381 xassert (IT_STRING_CHARPOS (*it) >= 0);
2382 }
2383 else
2384 {
2385 xassert (IT_STRING_CHARPOS (*it) < 0);
2386 if (it->method == GET_FROM_BUFFER)
2387 {
2388 /* Check that character and byte positions agree. */
2389 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2390 }
2391 }
2392
2393 if (it->dpvec)
2394 xassert (it->current.dpvec_index >= 0);
2395 else
2396 xassert (it->current.dpvec_index < 0);
2397 }
2398
2399 #define CHECK_IT(IT) check_it ((IT))
2400
2401 #else /* not 0 */
2402
2403 #define CHECK_IT(IT) (void) 0
2404
2405 #endif /* not 0 */
2406
2407
2408 #if GLYPH_DEBUG
2409
2410 /* Check that the window end of window W is what we expect it
2411 to be---the last row in the current matrix displaying text. */
2412
2413 static void
2414 check_window_end (w)
2415 struct window *w;
2416 {
2417 if (!MINI_WINDOW_P (w)
2418 && !NILP (w->window_end_valid))
2419 {
2420 struct glyph_row *row;
2421 xassert ((row = MATRIX_ROW (w->current_matrix,
2422 XFASTINT (w->window_end_vpos)),
2423 !row->enabled_p
2424 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2425 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2426 }
2427 }
2428
2429 #define CHECK_WINDOW_END(W) check_window_end ((W))
2430
2431 #else /* not GLYPH_DEBUG */
2432
2433 #define CHECK_WINDOW_END(W) (void) 0
2434
2435 #endif /* not GLYPH_DEBUG */
2436
2437
2438 \f
2439 /***********************************************************************
2440 Iterator initialization
2441 ***********************************************************************/
2442
2443 /* Initialize IT for displaying current_buffer in window W, starting
2444 at character position CHARPOS. CHARPOS < 0 means that no buffer
2445 position is specified which is useful when the iterator is assigned
2446 a position later. BYTEPOS is the byte position corresponding to
2447 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2448
2449 If ROW is not null, calls to produce_glyphs with IT as parameter
2450 will produce glyphs in that row.
2451
2452 BASE_FACE_ID is the id of a base face to use. It must be one of
2453 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2454 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2455 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2456
2457 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2458 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2459 will be initialized to use the corresponding mode line glyph row of
2460 the desired matrix of W. */
2461
2462 void
2463 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2464 struct it *it;
2465 struct window *w;
2466 int charpos, bytepos;
2467 struct glyph_row *row;
2468 enum face_id base_face_id;
2469 {
2470 int highlight_region_p;
2471
2472 /* Some precondition checks. */
2473 xassert (w != NULL && it != NULL);
2474 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2475 && charpos <= ZV));
2476
2477 /* If face attributes have been changed since the last redisplay,
2478 free realized faces now because they depend on face definitions
2479 that might have changed. Don't free faces while there might be
2480 desired matrices pending which reference these faces. */
2481 if (face_change_count && !inhibit_free_realized_faces)
2482 {
2483 face_change_count = 0;
2484 free_all_realized_faces (Qnil);
2485 }
2486
2487 /* Use one of the mode line rows of W's desired matrix if
2488 appropriate. */
2489 if (row == NULL)
2490 {
2491 if (base_face_id == MODE_LINE_FACE_ID
2492 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2493 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2494 else if (base_face_id == HEADER_LINE_FACE_ID)
2495 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2496 }
2497
2498 /* Clear IT. */
2499 bzero (it, sizeof *it);
2500 it->current.overlay_string_index = -1;
2501 it->current.dpvec_index = -1;
2502 it->base_face_id = base_face_id;
2503 it->string = Qnil;
2504 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2505
2506 /* The window in which we iterate over current_buffer: */
2507 XSETWINDOW (it->window, w);
2508 it->w = w;
2509 it->f = XFRAME (w->frame);
2510
2511 /* Extra space between lines (on window systems only). */
2512 if (base_face_id == DEFAULT_FACE_ID
2513 && FRAME_WINDOW_P (it->f))
2514 {
2515 if (NATNUMP (current_buffer->extra_line_spacing))
2516 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2517 else if (FLOATP (current_buffer->extra_line_spacing))
2518 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2519 * FRAME_LINE_HEIGHT (it->f));
2520 else if (it->f->extra_line_spacing > 0)
2521 it->extra_line_spacing = it->f->extra_line_spacing;
2522 it->max_extra_line_spacing = 0;
2523 }
2524
2525 /* If realized faces have been removed, e.g. because of face
2526 attribute changes of named faces, recompute them. When running
2527 in batch mode, the face cache of Vterminal_frame is null. If
2528 we happen to get called, make a dummy face cache. */
2529 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2530 init_frame_faces (it->f);
2531 if (FRAME_FACE_CACHE (it->f)->used == 0)
2532 recompute_basic_faces (it->f);
2533
2534 /* Current value of the `slice', `space-width', and 'height' properties. */
2535 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2536 it->space_width = Qnil;
2537 it->font_height = Qnil;
2538 it->override_ascent = -1;
2539
2540 /* Are control characters displayed as `^C'? */
2541 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2542
2543 /* -1 means everything between a CR and the following line end
2544 is invisible. >0 means lines indented more than this value are
2545 invisible. */
2546 it->selective = (INTEGERP (current_buffer->selective_display)
2547 ? XFASTINT (current_buffer->selective_display)
2548 : (!NILP (current_buffer->selective_display)
2549 ? -1 : 0));
2550 it->selective_display_ellipsis_p
2551 = !NILP (current_buffer->selective_display_ellipses);
2552
2553 /* Display table to use. */
2554 it->dp = window_display_table (w);
2555
2556 /* Are multibyte characters enabled in current_buffer? */
2557 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2558
2559 /* Non-zero if we should highlight the region. */
2560 highlight_region_p
2561 = (!NILP (Vtransient_mark_mode)
2562 && !NILP (current_buffer->mark_active)
2563 && XMARKER (current_buffer->mark)->buffer != 0);
2564
2565 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2566 start and end of a visible region in window IT->w. Set both to
2567 -1 to indicate no region. */
2568 if (highlight_region_p
2569 /* Maybe highlight only in selected window. */
2570 && (/* Either show region everywhere. */
2571 highlight_nonselected_windows
2572 /* Or show region in the selected window. */
2573 || w == XWINDOW (selected_window)
2574 /* Or show the region if we are in the mini-buffer and W is
2575 the window the mini-buffer refers to. */
2576 || (MINI_WINDOW_P (XWINDOW (selected_window))
2577 && WINDOWP (minibuf_selected_window)
2578 && w == XWINDOW (minibuf_selected_window))))
2579 {
2580 int charpos = marker_position (current_buffer->mark);
2581 it->region_beg_charpos = min (PT, charpos);
2582 it->region_end_charpos = max (PT, charpos);
2583 }
2584 else
2585 it->region_beg_charpos = it->region_end_charpos = -1;
2586
2587 /* Get the position at which the redisplay_end_trigger hook should
2588 be run, if it is to be run at all. */
2589 if (MARKERP (w->redisplay_end_trigger)
2590 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2591 it->redisplay_end_trigger_charpos
2592 = marker_position (w->redisplay_end_trigger);
2593 else if (INTEGERP (w->redisplay_end_trigger))
2594 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2595
2596 /* Correct bogus values of tab_width. */
2597 it->tab_width = XINT (current_buffer->tab_width);
2598 if (it->tab_width <= 0 || it->tab_width > 1000)
2599 it->tab_width = 8;
2600
2601 /* Are lines in the display truncated? */
2602 it->truncate_lines_p
2603 = (base_face_id != DEFAULT_FACE_ID
2604 || XINT (it->w->hscroll)
2605 || (truncate_partial_width_windows
2606 && !WINDOW_FULL_WIDTH_P (it->w))
2607 || !NILP (current_buffer->truncate_lines));
2608
2609 /* Get dimensions of truncation and continuation glyphs. These are
2610 displayed as fringe bitmaps under X, so we don't need them for such
2611 frames. */
2612 if (!FRAME_WINDOW_P (it->f))
2613 {
2614 if (it->truncate_lines_p)
2615 {
2616 /* We will need the truncation glyph. */
2617 xassert (it->glyph_row == NULL);
2618 produce_special_glyphs (it, IT_TRUNCATION);
2619 it->truncation_pixel_width = it->pixel_width;
2620 }
2621 else
2622 {
2623 /* We will need the continuation glyph. */
2624 xassert (it->glyph_row == NULL);
2625 produce_special_glyphs (it, IT_CONTINUATION);
2626 it->continuation_pixel_width = it->pixel_width;
2627 }
2628
2629 /* Reset these values to zero because the produce_special_glyphs
2630 above has changed them. */
2631 it->pixel_width = it->ascent = it->descent = 0;
2632 it->phys_ascent = it->phys_descent = 0;
2633 }
2634
2635 /* Set this after getting the dimensions of truncation and
2636 continuation glyphs, so that we don't produce glyphs when calling
2637 produce_special_glyphs, above. */
2638 it->glyph_row = row;
2639 it->area = TEXT_AREA;
2640
2641 /* Get the dimensions of the display area. The display area
2642 consists of the visible window area plus a horizontally scrolled
2643 part to the left of the window. All x-values are relative to the
2644 start of this total display area. */
2645 if (base_face_id != DEFAULT_FACE_ID)
2646 {
2647 /* Mode lines, menu bar in terminal frames. */
2648 it->first_visible_x = 0;
2649 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2650 }
2651 else
2652 {
2653 it->first_visible_x
2654 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2655 it->last_visible_x = (it->first_visible_x
2656 + window_box_width (w, TEXT_AREA));
2657
2658 /* If we truncate lines, leave room for the truncator glyph(s) at
2659 the right margin. Otherwise, leave room for the continuation
2660 glyph(s). Truncation and continuation glyphs are not inserted
2661 for window-based redisplay. */
2662 if (!FRAME_WINDOW_P (it->f))
2663 {
2664 if (it->truncate_lines_p)
2665 it->last_visible_x -= it->truncation_pixel_width;
2666 else
2667 it->last_visible_x -= it->continuation_pixel_width;
2668 }
2669
2670 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2671 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2672 }
2673
2674 /* Leave room for a border glyph. */
2675 if (!FRAME_WINDOW_P (it->f)
2676 && !WINDOW_RIGHTMOST_P (it->w))
2677 it->last_visible_x -= 1;
2678
2679 it->last_visible_y = window_text_bottom_y (w);
2680
2681 /* For mode lines and alike, arrange for the first glyph having a
2682 left box line if the face specifies a box. */
2683 if (base_face_id != DEFAULT_FACE_ID)
2684 {
2685 struct face *face;
2686
2687 it->face_id = base_face_id;
2688
2689 /* If we have a boxed mode line, make the first character appear
2690 with a left box line. */
2691 face = FACE_FROM_ID (it->f, base_face_id);
2692 if (face->box != FACE_NO_BOX)
2693 it->start_of_box_run_p = 1;
2694 }
2695
2696 /* If a buffer position was specified, set the iterator there,
2697 getting overlays and face properties from that position. */
2698 if (charpos >= BUF_BEG (current_buffer))
2699 {
2700 it->end_charpos = ZV;
2701 it->face_id = -1;
2702 IT_CHARPOS (*it) = charpos;
2703
2704 /* Compute byte position if not specified. */
2705 if (bytepos < charpos)
2706 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2707 else
2708 IT_BYTEPOS (*it) = bytepos;
2709
2710 it->start = it->current;
2711
2712 /* Compute faces etc. */
2713 reseat (it, it->current.pos, 1);
2714 }
2715
2716 CHECK_IT (it);
2717 }
2718
2719
2720 /* Initialize IT for the display of window W with window start POS. */
2721
2722 void
2723 start_display (it, w, pos)
2724 struct it *it;
2725 struct window *w;
2726 struct text_pos pos;
2727 {
2728 struct glyph_row *row;
2729 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2730
2731 row = w->desired_matrix->rows + first_vpos;
2732 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2733 it->first_vpos = first_vpos;
2734
2735 /* Don't reseat to previous visible line start if current start
2736 position is in a string or image. */
2737 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2738 {
2739 int start_at_line_beg_p;
2740 int first_y = it->current_y;
2741
2742 /* If window start is not at a line start, skip forward to POS to
2743 get the correct continuation lines width. */
2744 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2745 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2746 if (!start_at_line_beg_p)
2747 {
2748 int new_x;
2749
2750 reseat_at_previous_visible_line_start (it);
2751 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2752
2753 new_x = it->current_x + it->pixel_width;
2754
2755 /* If lines are continued, this line may end in the middle
2756 of a multi-glyph character (e.g. a control character
2757 displayed as \003, or in the middle of an overlay
2758 string). In this case move_it_to above will not have
2759 taken us to the start of the continuation line but to the
2760 end of the continued line. */
2761 if (it->current_x > 0
2762 && !it->truncate_lines_p /* Lines are continued. */
2763 && (/* And glyph doesn't fit on the line. */
2764 new_x > it->last_visible_x
2765 /* Or it fits exactly and we're on a window
2766 system frame. */
2767 || (new_x == it->last_visible_x
2768 && FRAME_WINDOW_P (it->f))))
2769 {
2770 if (it->current.dpvec_index >= 0
2771 || it->current.overlay_string_index >= 0)
2772 {
2773 set_iterator_to_next (it, 1);
2774 move_it_in_display_line_to (it, -1, -1, 0);
2775 }
2776
2777 it->continuation_lines_width += it->current_x;
2778 }
2779
2780 /* We're starting a new display line, not affected by the
2781 height of the continued line, so clear the appropriate
2782 fields in the iterator structure. */
2783 it->max_ascent = it->max_descent = 0;
2784 it->max_phys_ascent = it->max_phys_descent = 0;
2785
2786 it->current_y = first_y;
2787 it->vpos = 0;
2788 it->current_x = it->hpos = 0;
2789 }
2790 }
2791
2792 #if 0 /* Don't assert the following because start_display is sometimes
2793 called intentionally with a window start that is not at a
2794 line start. Please leave this code in as a comment. */
2795
2796 /* Window start should be on a line start, now. */
2797 xassert (it->continuation_lines_width
2798 || IT_CHARPOS (it) == BEGV
2799 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2800 #endif /* 0 */
2801 }
2802
2803
2804 /* Return 1 if POS is a position in ellipses displayed for invisible
2805 text. W is the window we display, for text property lookup. */
2806
2807 static int
2808 in_ellipses_for_invisible_text_p (pos, w)
2809 struct display_pos *pos;
2810 struct window *w;
2811 {
2812 Lisp_Object prop, window;
2813 int ellipses_p = 0;
2814 int charpos = CHARPOS (pos->pos);
2815
2816 /* If POS specifies a position in a display vector, this might
2817 be for an ellipsis displayed for invisible text. We won't
2818 get the iterator set up for delivering that ellipsis unless
2819 we make sure that it gets aware of the invisible text. */
2820 if (pos->dpvec_index >= 0
2821 && pos->overlay_string_index < 0
2822 && CHARPOS (pos->string_pos) < 0
2823 && charpos > BEGV
2824 && (XSETWINDOW (window, w),
2825 prop = Fget_char_property (make_number (charpos),
2826 Qinvisible, window),
2827 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2828 {
2829 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2830 window);
2831 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2832 }
2833
2834 return ellipses_p;
2835 }
2836
2837
2838 /* Initialize IT for stepping through current_buffer in window W,
2839 starting at position POS that includes overlay string and display
2840 vector/ control character translation position information. Value
2841 is zero if there are overlay strings with newlines at POS. */
2842
2843 static int
2844 init_from_display_pos (it, w, pos)
2845 struct it *it;
2846 struct window *w;
2847 struct display_pos *pos;
2848 {
2849 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2850 int i, overlay_strings_with_newlines = 0;
2851
2852 /* If POS specifies a position in a display vector, this might
2853 be for an ellipsis displayed for invisible text. We won't
2854 get the iterator set up for delivering that ellipsis unless
2855 we make sure that it gets aware of the invisible text. */
2856 if (in_ellipses_for_invisible_text_p (pos, w))
2857 {
2858 --charpos;
2859 bytepos = 0;
2860 }
2861
2862 /* Keep in mind: the call to reseat in init_iterator skips invisible
2863 text, so we might end up at a position different from POS. This
2864 is only a problem when POS is a row start after a newline and an
2865 overlay starts there with an after-string, and the overlay has an
2866 invisible property. Since we don't skip invisible text in
2867 display_line and elsewhere immediately after consuming the
2868 newline before the row start, such a POS will not be in a string,
2869 but the call to init_iterator below will move us to the
2870 after-string. */
2871 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2872
2873 /* This only scans the current chunk -- it should scan all chunks.
2874 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2875 to 16 in 22.1 to make this a lesser problem. */
2876 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2877 {
2878 const char *s = SDATA (it->overlay_strings[i]);
2879 const char *e = s + SBYTES (it->overlay_strings[i]);
2880
2881 while (s < e && *s != '\n')
2882 ++s;
2883
2884 if (s < e)
2885 {
2886 overlay_strings_with_newlines = 1;
2887 break;
2888 }
2889 }
2890
2891 /* If position is within an overlay string, set up IT to the right
2892 overlay string. */
2893 if (pos->overlay_string_index >= 0)
2894 {
2895 int relative_index;
2896
2897 /* If the first overlay string happens to have a `display'
2898 property for an image, the iterator will be set up for that
2899 image, and we have to undo that setup first before we can
2900 correct the overlay string index. */
2901 if (it->method == GET_FROM_IMAGE)
2902 pop_it (it);
2903
2904 /* We already have the first chunk of overlay strings in
2905 IT->overlay_strings. Load more until the one for
2906 pos->overlay_string_index is in IT->overlay_strings. */
2907 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2908 {
2909 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2910 it->current.overlay_string_index = 0;
2911 while (n--)
2912 {
2913 load_overlay_strings (it, 0);
2914 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2915 }
2916 }
2917
2918 it->current.overlay_string_index = pos->overlay_string_index;
2919 relative_index = (it->current.overlay_string_index
2920 % OVERLAY_STRING_CHUNK_SIZE);
2921 it->string = it->overlay_strings[relative_index];
2922 xassert (STRINGP (it->string));
2923 it->current.string_pos = pos->string_pos;
2924 it->method = GET_FROM_STRING;
2925 }
2926
2927 #if 0 /* This is bogus because POS not having an overlay string
2928 position does not mean it's after the string. Example: A
2929 line starting with a before-string and initialization of IT
2930 to the previous row's end position. */
2931 else if (it->current.overlay_string_index >= 0)
2932 {
2933 /* If POS says we're already after an overlay string ending at
2934 POS, make sure to pop the iterator because it will be in
2935 front of that overlay string. When POS is ZV, we've thereby
2936 also ``processed'' overlay strings at ZV. */
2937 while (it->sp)
2938 pop_it (it);
2939 xassert (it->current.overlay_string_index == -1);
2940 xassert (it->method == GET_FROM_BUFFER);
2941 if (CHARPOS (pos->pos) == ZV)
2942 it->overlay_strings_at_end_processed_p = 1;
2943 }
2944 #endif /* 0 */
2945
2946 if (CHARPOS (pos->string_pos) >= 0)
2947 {
2948 /* Recorded position is not in an overlay string, but in another
2949 string. This can only be a string from a `display' property.
2950 IT should already be filled with that string. */
2951 it->current.string_pos = pos->string_pos;
2952 xassert (STRINGP (it->string));
2953 }
2954
2955 /* Restore position in display vector translations, control
2956 character translations or ellipses. */
2957 if (pos->dpvec_index >= 0)
2958 {
2959 if (it->dpvec == NULL)
2960 get_next_display_element (it);
2961 xassert (it->dpvec && it->current.dpvec_index == 0);
2962 it->current.dpvec_index = pos->dpvec_index;
2963 }
2964
2965 CHECK_IT (it);
2966 return !overlay_strings_with_newlines;
2967 }
2968
2969
2970 /* Initialize IT for stepping through current_buffer in window W
2971 starting at ROW->start. */
2972
2973 static void
2974 init_to_row_start (it, w, row)
2975 struct it *it;
2976 struct window *w;
2977 struct glyph_row *row;
2978 {
2979 init_from_display_pos (it, w, &row->start);
2980 it->start = row->start;
2981 it->continuation_lines_width = row->continuation_lines_width;
2982 CHECK_IT (it);
2983 }
2984
2985
2986 /* Initialize IT for stepping through current_buffer in window W
2987 starting in the line following ROW, i.e. starting at ROW->end.
2988 Value is zero if there are overlay strings with newlines at ROW's
2989 end position. */
2990
2991 static int
2992 init_to_row_end (it, w, row)
2993 struct it *it;
2994 struct window *w;
2995 struct glyph_row *row;
2996 {
2997 int success = 0;
2998
2999 if (init_from_display_pos (it, w, &row->end))
3000 {
3001 if (row->continued_p)
3002 it->continuation_lines_width
3003 = row->continuation_lines_width + row->pixel_width;
3004 CHECK_IT (it);
3005 success = 1;
3006 }
3007
3008 return success;
3009 }
3010
3011
3012
3013 \f
3014 /***********************************************************************
3015 Text properties
3016 ***********************************************************************/
3017
3018 /* Called when IT reaches IT->stop_charpos. Handle text property and
3019 overlay changes. Set IT->stop_charpos to the next position where
3020 to stop. */
3021
3022 static void
3023 handle_stop (it)
3024 struct it *it;
3025 {
3026 enum prop_handled handled;
3027 int handle_overlay_change_p;
3028 struct props *p;
3029
3030 it->dpvec = NULL;
3031 it->current.dpvec_index = -1;
3032 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3033 it->ignore_overlay_strings_at_pos_p = 0;
3034
3035 /* Use face of preceding text for ellipsis (if invisible) */
3036 if (it->selective_display_ellipsis_p)
3037 it->saved_face_id = it->face_id;
3038
3039 do
3040 {
3041 handled = HANDLED_NORMALLY;
3042
3043 /* Call text property handlers. */
3044 for (p = it_props; p->handler; ++p)
3045 {
3046 handled = p->handler (it);
3047
3048 if (handled == HANDLED_RECOMPUTE_PROPS)
3049 break;
3050 else if (handled == HANDLED_RETURN)
3051 {
3052 /* We still want to show before and after strings from
3053 overlays even if the actual buffer text is replaced. */
3054 if (!handle_overlay_change_p || it->sp > 1)
3055 return;
3056 if (!get_overlay_strings_1 (it, 0, 0))
3057 return;
3058 it->ignore_overlay_strings_at_pos_p = 1;
3059 it->string_from_display_prop_p = 0;
3060 handle_overlay_change_p = 0;
3061 handled = HANDLED_RECOMPUTE_PROPS;
3062 break;
3063 }
3064 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3065 handle_overlay_change_p = 0;
3066 }
3067
3068 if (handled != HANDLED_RECOMPUTE_PROPS)
3069 {
3070 /* Don't check for overlay strings below when set to deliver
3071 characters from a display vector. */
3072 if (it->method == GET_FROM_DISPLAY_VECTOR)
3073 handle_overlay_change_p = 0;
3074
3075 /* Handle overlay changes. */
3076 if (handle_overlay_change_p)
3077 handled = handle_overlay_change (it);
3078
3079 /* Determine where to stop next. */
3080 if (handled == HANDLED_NORMALLY)
3081 compute_stop_pos (it);
3082 }
3083 }
3084 while (handled == HANDLED_RECOMPUTE_PROPS);
3085 }
3086
3087
3088 /* Compute IT->stop_charpos from text property and overlay change
3089 information for IT's current position. */
3090
3091 static void
3092 compute_stop_pos (it)
3093 struct it *it;
3094 {
3095 register INTERVAL iv, next_iv;
3096 Lisp_Object object, limit, position;
3097
3098 /* If nowhere else, stop at the end. */
3099 it->stop_charpos = it->end_charpos;
3100
3101 if (STRINGP (it->string))
3102 {
3103 /* Strings are usually short, so don't limit the search for
3104 properties. */
3105 object = it->string;
3106 limit = Qnil;
3107 position = make_number (IT_STRING_CHARPOS (*it));
3108 }
3109 else
3110 {
3111 int charpos;
3112
3113 /* If next overlay change is in front of the current stop pos
3114 (which is IT->end_charpos), stop there. Note: value of
3115 next_overlay_change is point-max if no overlay change
3116 follows. */
3117 charpos = next_overlay_change (IT_CHARPOS (*it));
3118 if (charpos < it->stop_charpos)
3119 it->stop_charpos = charpos;
3120
3121 /* If showing the region, we have to stop at the region
3122 start or end because the face might change there. */
3123 if (it->region_beg_charpos > 0)
3124 {
3125 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3126 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3127 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3128 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3129 }
3130
3131 /* Set up variables for computing the stop position from text
3132 property changes. */
3133 XSETBUFFER (object, current_buffer);
3134 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3135 position = make_number (IT_CHARPOS (*it));
3136
3137 }
3138
3139 /* Get the interval containing IT's position. Value is a null
3140 interval if there isn't such an interval. */
3141 iv = validate_interval_range (object, &position, &position, 0);
3142 if (!NULL_INTERVAL_P (iv))
3143 {
3144 Lisp_Object values_here[LAST_PROP_IDX];
3145 struct props *p;
3146
3147 /* Get properties here. */
3148 for (p = it_props; p->handler; ++p)
3149 values_here[p->idx] = textget (iv->plist, *p->name);
3150
3151 /* Look for an interval following iv that has different
3152 properties. */
3153 for (next_iv = next_interval (iv);
3154 (!NULL_INTERVAL_P (next_iv)
3155 && (NILP (limit)
3156 || XFASTINT (limit) > next_iv->position));
3157 next_iv = next_interval (next_iv))
3158 {
3159 for (p = it_props; p->handler; ++p)
3160 {
3161 Lisp_Object new_value;
3162
3163 new_value = textget (next_iv->plist, *p->name);
3164 if (!EQ (values_here[p->idx], new_value))
3165 break;
3166 }
3167
3168 if (p->handler)
3169 break;
3170 }
3171
3172 if (!NULL_INTERVAL_P (next_iv))
3173 {
3174 if (INTEGERP (limit)
3175 && next_iv->position >= XFASTINT (limit))
3176 /* No text property change up to limit. */
3177 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3178 else
3179 /* Text properties change in next_iv. */
3180 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3181 }
3182 }
3183
3184 xassert (STRINGP (it->string)
3185 || (it->stop_charpos >= BEGV
3186 && it->stop_charpos >= IT_CHARPOS (*it)));
3187 }
3188
3189
3190 /* Return the position of the next overlay change after POS in
3191 current_buffer. Value is point-max if no overlay change
3192 follows. This is like `next-overlay-change' but doesn't use
3193 xmalloc. */
3194
3195 static int
3196 next_overlay_change (pos)
3197 int pos;
3198 {
3199 int noverlays;
3200 int endpos;
3201 Lisp_Object *overlays;
3202 int i;
3203
3204 /* Get all overlays at the given position. */
3205 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3206
3207 /* If any of these overlays ends before endpos,
3208 use its ending point instead. */
3209 for (i = 0; i < noverlays; ++i)
3210 {
3211 Lisp_Object oend;
3212 int oendpos;
3213
3214 oend = OVERLAY_END (overlays[i]);
3215 oendpos = OVERLAY_POSITION (oend);
3216 endpos = min (endpos, oendpos);
3217 }
3218
3219 return endpos;
3220 }
3221
3222
3223 \f
3224 /***********************************************************************
3225 Fontification
3226 ***********************************************************************/
3227
3228 /* Handle changes in the `fontified' property of the current buffer by
3229 calling hook functions from Qfontification_functions to fontify
3230 regions of text. */
3231
3232 static enum prop_handled
3233 handle_fontified_prop (it)
3234 struct it *it;
3235 {
3236 Lisp_Object prop, pos;
3237 enum prop_handled handled = HANDLED_NORMALLY;
3238
3239 if (!NILP (Vmemory_full))
3240 return handled;
3241
3242 /* Get the value of the `fontified' property at IT's current buffer
3243 position. (The `fontified' property doesn't have a special
3244 meaning in strings.) If the value is nil, call functions from
3245 Qfontification_functions. */
3246 if (!STRINGP (it->string)
3247 && it->s == NULL
3248 && !NILP (Vfontification_functions)
3249 && !NILP (Vrun_hooks)
3250 && (pos = make_number (IT_CHARPOS (*it)),
3251 prop = Fget_char_property (pos, Qfontified, Qnil),
3252 /* Ignore the special cased nil value always present at EOB since
3253 no amount of fontifying will be able to change it. */
3254 NILP (prop) && IT_CHARPOS (*it) < Z))
3255 {
3256 int count = SPECPDL_INDEX ();
3257 Lisp_Object val;
3258
3259 val = Vfontification_functions;
3260 specbind (Qfontification_functions, Qnil);
3261
3262 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3263 safe_call1 (val, pos);
3264 else
3265 {
3266 Lisp_Object globals, fn;
3267 struct gcpro gcpro1, gcpro2;
3268
3269 globals = Qnil;
3270 GCPRO2 (val, globals);
3271
3272 for (; CONSP (val); val = XCDR (val))
3273 {
3274 fn = XCAR (val);
3275
3276 if (EQ (fn, Qt))
3277 {
3278 /* A value of t indicates this hook has a local
3279 binding; it means to run the global binding too.
3280 In a global value, t should not occur. If it
3281 does, we must ignore it to avoid an endless
3282 loop. */
3283 for (globals = Fdefault_value (Qfontification_functions);
3284 CONSP (globals);
3285 globals = XCDR (globals))
3286 {
3287 fn = XCAR (globals);
3288 if (!EQ (fn, Qt))
3289 safe_call1 (fn, pos);
3290 }
3291 }
3292 else
3293 safe_call1 (fn, pos);
3294 }
3295
3296 UNGCPRO;
3297 }
3298
3299 unbind_to (count, Qnil);
3300
3301 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3302 something. This avoids an endless loop if they failed to
3303 fontify the text for which reason ever. */
3304 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3305 handled = HANDLED_RECOMPUTE_PROPS;
3306 }
3307
3308 return handled;
3309 }
3310
3311
3312 \f
3313 /***********************************************************************
3314 Faces
3315 ***********************************************************************/
3316
3317 /* Set up iterator IT from face properties at its current position.
3318 Called from handle_stop. */
3319
3320 static enum prop_handled
3321 handle_face_prop (it)
3322 struct it *it;
3323 {
3324 int new_face_id, next_stop;
3325
3326 if (!STRINGP (it->string))
3327 {
3328 new_face_id
3329 = face_at_buffer_position (it->w,
3330 IT_CHARPOS (*it),
3331 it->region_beg_charpos,
3332 it->region_end_charpos,
3333 &next_stop,
3334 (IT_CHARPOS (*it)
3335 + TEXT_PROP_DISTANCE_LIMIT),
3336 0);
3337
3338 /* Is this a start of a run of characters with box face?
3339 Caveat: this can be called for a freshly initialized
3340 iterator; face_id is -1 in this case. We know that the new
3341 face will not change until limit, i.e. if the new face has a
3342 box, all characters up to limit will have one. But, as
3343 usual, we don't know whether limit is really the end. */
3344 if (new_face_id != it->face_id)
3345 {
3346 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3347
3348 /* If new face has a box but old face has not, this is
3349 the start of a run of characters with box, i.e. it has
3350 a shadow on the left side. The value of face_id of the
3351 iterator will be -1 if this is the initial call that gets
3352 the face. In this case, we have to look in front of IT's
3353 position and see whether there is a face != new_face_id. */
3354 it->start_of_box_run_p
3355 = (new_face->box != FACE_NO_BOX
3356 && (it->face_id >= 0
3357 || IT_CHARPOS (*it) == BEG
3358 || new_face_id != face_before_it_pos (it)));
3359 it->face_box_p = new_face->box != FACE_NO_BOX;
3360 }
3361 }
3362 else
3363 {
3364 int base_face_id, bufpos;
3365
3366 if (it->current.overlay_string_index >= 0)
3367 bufpos = IT_CHARPOS (*it);
3368 else
3369 bufpos = 0;
3370
3371 /* For strings from a buffer, i.e. overlay strings or strings
3372 from a `display' property, use the face at IT's current
3373 buffer position as the base face to merge with, so that
3374 overlay strings appear in the same face as surrounding
3375 text, unless they specify their own faces. */
3376 base_face_id = underlying_face_id (it);
3377
3378 new_face_id = face_at_string_position (it->w,
3379 it->string,
3380 IT_STRING_CHARPOS (*it),
3381 bufpos,
3382 it->region_beg_charpos,
3383 it->region_end_charpos,
3384 &next_stop,
3385 base_face_id, 0);
3386
3387 #if 0 /* This shouldn't be neccessary. Let's check it. */
3388 /* If IT is used to display a mode line we would really like to
3389 use the mode line face instead of the frame's default face. */
3390 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3391 && new_face_id == DEFAULT_FACE_ID)
3392 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3393 #endif
3394
3395 /* Is this a start of a run of characters with box? Caveat:
3396 this can be called for a freshly allocated iterator; face_id
3397 is -1 is this case. We know that the new face will not
3398 change until the next check pos, i.e. if the new face has a
3399 box, all characters up to that position will have a
3400 box. But, as usual, we don't know whether that position
3401 is really the end. */
3402 if (new_face_id != it->face_id)
3403 {
3404 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3405 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3406
3407 /* If new face has a box but old face hasn't, this is the
3408 start of a run of characters with box, i.e. it has a
3409 shadow on the left side. */
3410 it->start_of_box_run_p
3411 = new_face->box && (old_face == NULL || !old_face->box);
3412 it->face_box_p = new_face->box != FACE_NO_BOX;
3413 }
3414 }
3415
3416 it->face_id = new_face_id;
3417 return HANDLED_NORMALLY;
3418 }
3419
3420
3421 /* Return the ID of the face ``underlying'' IT's current position,
3422 which is in a string. If the iterator is associated with a
3423 buffer, return the face at IT's current buffer position.
3424 Otherwise, use the iterator's base_face_id. */
3425
3426 static int
3427 underlying_face_id (it)
3428 struct it *it;
3429 {
3430 int face_id = it->base_face_id, i;
3431
3432 xassert (STRINGP (it->string));
3433
3434 for (i = it->sp - 1; i >= 0; --i)
3435 if (NILP (it->stack[i].string))
3436 face_id = it->stack[i].face_id;
3437
3438 return face_id;
3439 }
3440
3441
3442 /* Compute the face one character before or after the current position
3443 of IT. BEFORE_P non-zero means get the face in front of IT's
3444 position. Value is the id of the face. */
3445
3446 static int
3447 face_before_or_after_it_pos (it, before_p)
3448 struct it *it;
3449 int before_p;
3450 {
3451 int face_id, limit;
3452 int next_check_charpos;
3453 struct text_pos pos;
3454
3455 xassert (it->s == NULL);
3456
3457 if (STRINGP (it->string))
3458 {
3459 int bufpos, base_face_id;
3460
3461 /* No face change past the end of the string (for the case
3462 we are padding with spaces). No face change before the
3463 string start. */
3464 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3465 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3466 return it->face_id;
3467
3468 /* Set pos to the position before or after IT's current position. */
3469 if (before_p)
3470 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3471 else
3472 /* For composition, we must check the character after the
3473 composition. */
3474 pos = (it->what == IT_COMPOSITION
3475 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3476 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3477
3478 if (it->current.overlay_string_index >= 0)
3479 bufpos = IT_CHARPOS (*it);
3480 else
3481 bufpos = 0;
3482
3483 base_face_id = underlying_face_id (it);
3484
3485 /* Get the face for ASCII, or unibyte. */
3486 face_id = face_at_string_position (it->w,
3487 it->string,
3488 CHARPOS (pos),
3489 bufpos,
3490 it->region_beg_charpos,
3491 it->region_end_charpos,
3492 &next_check_charpos,
3493 base_face_id, 0);
3494
3495 /* Correct the face for charsets different from ASCII. Do it
3496 for the multibyte case only. The face returned above is
3497 suitable for unibyte text if IT->string is unibyte. */
3498 if (STRING_MULTIBYTE (it->string))
3499 {
3500 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3501 int rest = SBYTES (it->string) - BYTEPOS (pos);
3502 int c, len;
3503 struct face *face = FACE_FROM_ID (it->f, face_id);
3504
3505 c = string_char_and_length (p, rest, &len);
3506 face_id = FACE_FOR_CHAR (it->f, face, c);
3507 }
3508 }
3509 else
3510 {
3511 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3512 || (IT_CHARPOS (*it) <= BEGV && before_p))
3513 return it->face_id;
3514
3515 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3516 pos = it->current.pos;
3517
3518 if (before_p)
3519 DEC_TEXT_POS (pos, it->multibyte_p);
3520 else
3521 {
3522 if (it->what == IT_COMPOSITION)
3523 /* For composition, we must check the position after the
3524 composition. */
3525 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3526 else
3527 INC_TEXT_POS (pos, it->multibyte_p);
3528 }
3529
3530 /* Determine face for CHARSET_ASCII, or unibyte. */
3531 face_id = face_at_buffer_position (it->w,
3532 CHARPOS (pos),
3533 it->region_beg_charpos,
3534 it->region_end_charpos,
3535 &next_check_charpos,
3536 limit, 0);
3537
3538 /* Correct the face for charsets different from ASCII. Do it
3539 for the multibyte case only. The face returned above is
3540 suitable for unibyte text if current_buffer is unibyte. */
3541 if (it->multibyte_p)
3542 {
3543 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3544 struct face *face = FACE_FROM_ID (it->f, face_id);
3545 face_id = FACE_FOR_CHAR (it->f, face, c);
3546 }
3547 }
3548
3549 return face_id;
3550 }
3551
3552
3553 \f
3554 /***********************************************************************
3555 Invisible text
3556 ***********************************************************************/
3557
3558 /* Set up iterator IT from invisible properties at its current
3559 position. Called from handle_stop. */
3560
3561 static enum prop_handled
3562 handle_invisible_prop (it)
3563 struct it *it;
3564 {
3565 enum prop_handled handled = HANDLED_NORMALLY;
3566
3567 if (STRINGP (it->string))
3568 {
3569 extern Lisp_Object Qinvisible;
3570 Lisp_Object prop, end_charpos, limit, charpos;
3571
3572 /* Get the value of the invisible text property at the
3573 current position. Value will be nil if there is no such
3574 property. */
3575 charpos = make_number (IT_STRING_CHARPOS (*it));
3576 prop = Fget_text_property (charpos, Qinvisible, it->string);
3577
3578 if (!NILP (prop)
3579 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3580 {
3581 handled = HANDLED_RECOMPUTE_PROPS;
3582
3583 /* Get the position at which the next change of the
3584 invisible text property can be found in IT->string.
3585 Value will be nil if the property value is the same for
3586 all the rest of IT->string. */
3587 XSETINT (limit, SCHARS (it->string));
3588 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3589 it->string, limit);
3590
3591 /* Text at current position is invisible. The next
3592 change in the property is at position end_charpos.
3593 Move IT's current position to that position. */
3594 if (INTEGERP (end_charpos)
3595 && XFASTINT (end_charpos) < XFASTINT (limit))
3596 {
3597 struct text_pos old;
3598 old = it->current.string_pos;
3599 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3600 compute_string_pos (&it->current.string_pos, old, it->string);
3601 }
3602 else
3603 {
3604 /* The rest of the string is invisible. If this is an
3605 overlay string, proceed with the next overlay string
3606 or whatever comes and return a character from there. */
3607 if (it->current.overlay_string_index >= 0)
3608 {
3609 next_overlay_string (it);
3610 /* Don't check for overlay strings when we just
3611 finished processing them. */
3612 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3613 }
3614 else
3615 {
3616 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3617 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3618 }
3619 }
3620 }
3621 }
3622 else
3623 {
3624 int invis_p, newpos, next_stop, start_charpos;
3625 Lisp_Object pos, prop, overlay;
3626
3627 /* First of all, is there invisible text at this position? */
3628 start_charpos = IT_CHARPOS (*it);
3629 pos = make_number (IT_CHARPOS (*it));
3630 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3631 &overlay);
3632 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3633
3634 /* If we are on invisible text, skip over it. */
3635 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3636 {
3637 /* Record whether we have to display an ellipsis for the
3638 invisible text. */
3639 int display_ellipsis_p = invis_p == 2;
3640
3641 handled = HANDLED_RECOMPUTE_PROPS;
3642
3643 /* Loop skipping over invisible text. The loop is left at
3644 ZV or with IT on the first char being visible again. */
3645 do
3646 {
3647 /* Try to skip some invisible text. Return value is the
3648 position reached which can be equal to IT's position
3649 if there is nothing invisible here. This skips both
3650 over invisible text properties and overlays with
3651 invisible property. */
3652 newpos = skip_invisible (IT_CHARPOS (*it),
3653 &next_stop, ZV, it->window);
3654
3655 /* If we skipped nothing at all we weren't at invisible
3656 text in the first place. If everything to the end of
3657 the buffer was skipped, end the loop. */
3658 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3659 invis_p = 0;
3660 else
3661 {
3662 /* We skipped some characters but not necessarily
3663 all there are. Check if we ended up on visible
3664 text. Fget_char_property returns the property of
3665 the char before the given position, i.e. if we
3666 get invis_p = 0, this means that the char at
3667 newpos is visible. */
3668 pos = make_number (newpos);
3669 prop = Fget_char_property (pos, Qinvisible, it->window);
3670 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3671 }
3672
3673 /* If we ended up on invisible text, proceed to
3674 skip starting with next_stop. */
3675 if (invis_p)
3676 IT_CHARPOS (*it) = next_stop;
3677
3678 /* If there are adjacent invisible texts, don't lose the
3679 second one's ellipsis. */
3680 if (invis_p == 2)
3681 display_ellipsis_p = 1;
3682 }
3683 while (invis_p);
3684
3685 /* The position newpos is now either ZV or on visible text. */
3686 IT_CHARPOS (*it) = newpos;
3687 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3688
3689 /* If there are before-strings at the start of invisible
3690 text, and the text is invisible because of a text
3691 property, arrange to show before-strings because 20.x did
3692 it that way. (If the text is invisible because of an
3693 overlay property instead of a text property, this is
3694 already handled in the overlay code.) */
3695 if (NILP (overlay)
3696 && get_overlay_strings (it, start_charpos))
3697 {
3698 handled = HANDLED_RECOMPUTE_PROPS;
3699 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3700 }
3701 else if (display_ellipsis_p)
3702 {
3703 /* Make sure that the glyphs of the ellipsis will get
3704 correct `charpos' values. If we would not update
3705 it->position here, the glyphs would belong to the
3706 last visible character _before_ the invisible
3707 text, which confuses `set_cursor_from_row'.
3708
3709 We use the last invisible position instead of the
3710 first because this way the cursor is always drawn on
3711 the first "." of the ellipsis, whenever PT is inside
3712 the invisible text. Otherwise the cursor would be
3713 placed _after_ the ellipsis when the point is after the
3714 first invisible character. */
3715 if (!STRINGP (it->object))
3716 {
3717 it->position.charpos = IT_CHARPOS (*it) - 1;
3718 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3719 }
3720 setup_for_ellipsis (it, 0);
3721 }
3722 }
3723 }
3724
3725 return handled;
3726 }
3727
3728
3729 /* Make iterator IT return `...' next.
3730 Replaces LEN characters from buffer. */
3731
3732 static void
3733 setup_for_ellipsis (it, len)
3734 struct it *it;
3735 int len;
3736 {
3737 /* Use the display table definition for `...'. Invalid glyphs
3738 will be handled by the method returning elements from dpvec. */
3739 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3740 {
3741 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3742 it->dpvec = v->contents;
3743 it->dpend = v->contents + v->size;
3744 }
3745 else
3746 {
3747 /* Default `...'. */
3748 it->dpvec = default_invis_vector;
3749 it->dpend = default_invis_vector + 3;
3750 }
3751
3752 it->dpvec_char_len = len;
3753 it->current.dpvec_index = 0;
3754 it->dpvec_face_id = -1;
3755
3756 /* Remember the current face id in case glyphs specify faces.
3757 IT's face is restored in set_iterator_to_next.
3758 saved_face_id was set to preceding char's face in handle_stop. */
3759 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3760 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3761
3762 it->method = GET_FROM_DISPLAY_VECTOR;
3763 it->ellipsis_p = 1;
3764 }
3765
3766
3767 \f
3768 /***********************************************************************
3769 'display' property
3770 ***********************************************************************/
3771
3772 /* Set up iterator IT from `display' property at its current position.
3773 Called from handle_stop.
3774 We return HANDLED_RETURN if some part of the display property
3775 overrides the display of the buffer text itself.
3776 Otherwise we return HANDLED_NORMALLY. */
3777
3778 static enum prop_handled
3779 handle_display_prop (it)
3780 struct it *it;
3781 {
3782 Lisp_Object prop, object;
3783 struct text_pos *position;
3784 /* Nonzero if some property replaces the display of the text itself. */
3785 int display_replaced_p = 0;
3786
3787 if (STRINGP (it->string))
3788 {
3789 object = it->string;
3790 position = &it->current.string_pos;
3791 }
3792 else
3793 {
3794 XSETWINDOW (object, it->w);
3795 position = &it->current.pos;
3796 }
3797
3798 /* Reset those iterator values set from display property values. */
3799 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3800 it->space_width = Qnil;
3801 it->font_height = Qnil;
3802 it->voffset = 0;
3803
3804 /* We don't support recursive `display' properties, i.e. string
3805 values that have a string `display' property, that have a string
3806 `display' property etc. */
3807 if (!it->string_from_display_prop_p)
3808 it->area = TEXT_AREA;
3809
3810 prop = Fget_char_property (make_number (position->charpos),
3811 Qdisplay, object);
3812 if (NILP (prop))
3813 return HANDLED_NORMALLY;
3814
3815 if (!STRINGP (it->string))
3816 object = it->w->buffer;
3817
3818 if (CONSP (prop)
3819 /* Simple properties. */
3820 && !EQ (XCAR (prop), Qimage)
3821 && !EQ (XCAR (prop), Qspace)
3822 && !EQ (XCAR (prop), Qwhen)
3823 && !EQ (XCAR (prop), Qslice)
3824 && !EQ (XCAR (prop), Qspace_width)
3825 && !EQ (XCAR (prop), Qheight)
3826 && !EQ (XCAR (prop), Qraise)
3827 /* Marginal area specifications. */
3828 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3829 && !EQ (XCAR (prop), Qleft_fringe)
3830 && !EQ (XCAR (prop), Qright_fringe)
3831 && !NILP (XCAR (prop)))
3832 {
3833 for (; CONSP (prop); prop = XCDR (prop))
3834 {
3835 if (handle_single_display_spec (it, XCAR (prop), object,
3836 position, display_replaced_p))
3837 display_replaced_p = 1;
3838 }
3839 }
3840 else if (VECTORP (prop))
3841 {
3842 int i;
3843 for (i = 0; i < ASIZE (prop); ++i)
3844 if (handle_single_display_spec (it, AREF (prop, i), object,
3845 position, display_replaced_p))
3846 display_replaced_p = 1;
3847 }
3848 else
3849 {
3850 int ret = handle_single_display_spec (it, prop, object, position, 0);
3851 if (ret < 0) /* Replaced by "", i.e. nothing. */
3852 return HANDLED_RECOMPUTE_PROPS;
3853 if (ret)
3854 display_replaced_p = 1;
3855 }
3856
3857 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3858 }
3859
3860
3861 /* Value is the position of the end of the `display' property starting
3862 at START_POS in OBJECT. */
3863
3864 static struct text_pos
3865 display_prop_end (it, object, start_pos)
3866 struct it *it;
3867 Lisp_Object object;
3868 struct text_pos start_pos;
3869 {
3870 Lisp_Object end;
3871 struct text_pos end_pos;
3872
3873 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3874 Qdisplay, object, Qnil);
3875 CHARPOS (end_pos) = XFASTINT (end);
3876 if (STRINGP (object))
3877 compute_string_pos (&end_pos, start_pos, it->string);
3878 else
3879 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3880
3881 return end_pos;
3882 }
3883
3884
3885 /* Set up IT from a single `display' specification PROP. OBJECT
3886 is the object in which the `display' property was found. *POSITION
3887 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3888 means that we previously saw a display specification which already
3889 replaced text display with something else, for example an image;
3890 we ignore such properties after the first one has been processed.
3891
3892 If PROP is a `space' or `image' specification, and in some other
3893 cases too, set *POSITION to the position where the `display'
3894 property ends.
3895
3896 Value is non-zero if something was found which replaces the display
3897 of buffer or string text. Specifically, the value is -1 if that
3898 "something" is "nothing". */
3899
3900 static int
3901 handle_single_display_spec (it, spec, object, position,
3902 display_replaced_before_p)
3903 struct it *it;
3904 Lisp_Object spec;
3905 Lisp_Object object;
3906 struct text_pos *position;
3907 int display_replaced_before_p;
3908 {
3909 Lisp_Object form;
3910 Lisp_Object location, value;
3911 struct text_pos start_pos, save_pos;
3912 int valid_p;
3913
3914 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3915 If the result is non-nil, use VALUE instead of SPEC. */
3916 form = Qt;
3917 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3918 {
3919 spec = XCDR (spec);
3920 if (!CONSP (spec))
3921 return 0;
3922 form = XCAR (spec);
3923 spec = XCDR (spec);
3924 }
3925
3926 if (!NILP (form) && !EQ (form, Qt))
3927 {
3928 int count = SPECPDL_INDEX ();
3929 struct gcpro gcpro1;
3930
3931 /* Bind `object' to the object having the `display' property, a
3932 buffer or string. Bind `position' to the position in the
3933 object where the property was found, and `buffer-position'
3934 to the current position in the buffer. */
3935 specbind (Qobject, object);
3936 specbind (Qposition, make_number (CHARPOS (*position)));
3937 specbind (Qbuffer_position,
3938 make_number (STRINGP (object)
3939 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3940 GCPRO1 (form);
3941 form = safe_eval (form);
3942 UNGCPRO;
3943 unbind_to (count, Qnil);
3944 }
3945
3946 if (NILP (form))
3947 return 0;
3948
3949 /* Handle `(height HEIGHT)' specifications. */
3950 if (CONSP (spec)
3951 && EQ (XCAR (spec), Qheight)
3952 && CONSP (XCDR (spec)))
3953 {
3954 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3955 return 0;
3956
3957 it->font_height = XCAR (XCDR (spec));
3958 if (!NILP (it->font_height))
3959 {
3960 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3961 int new_height = -1;
3962
3963 if (CONSP (it->font_height)
3964 && (EQ (XCAR (it->font_height), Qplus)
3965 || EQ (XCAR (it->font_height), Qminus))
3966 && CONSP (XCDR (it->font_height))
3967 && INTEGERP (XCAR (XCDR (it->font_height))))
3968 {
3969 /* `(+ N)' or `(- N)' where N is an integer. */
3970 int steps = XINT (XCAR (XCDR (it->font_height)));
3971 if (EQ (XCAR (it->font_height), Qplus))
3972 steps = - steps;
3973 it->face_id = smaller_face (it->f, it->face_id, steps);
3974 }
3975 else if (FUNCTIONP (it->font_height))
3976 {
3977 /* Call function with current height as argument.
3978 Value is the new height. */
3979 Lisp_Object height;
3980 height = safe_call1 (it->font_height,
3981 face->lface[LFACE_HEIGHT_INDEX]);
3982 if (NUMBERP (height))
3983 new_height = XFLOATINT (height);
3984 }
3985 else if (NUMBERP (it->font_height))
3986 {
3987 /* Value is a multiple of the canonical char height. */
3988 struct face *face;
3989
3990 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3991 new_height = (XFLOATINT (it->font_height)
3992 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3993 }
3994 else
3995 {
3996 /* Evaluate IT->font_height with `height' bound to the
3997 current specified height to get the new height. */
3998 int count = SPECPDL_INDEX ();
3999
4000 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4001 value = safe_eval (it->font_height);
4002 unbind_to (count, Qnil);
4003
4004 if (NUMBERP (value))
4005 new_height = XFLOATINT (value);
4006 }
4007
4008 if (new_height > 0)
4009 it->face_id = face_with_height (it->f, it->face_id, new_height);
4010 }
4011
4012 return 0;
4013 }
4014
4015 /* Handle `(space_width WIDTH)'. */
4016 if (CONSP (spec)
4017 && EQ (XCAR (spec), Qspace_width)
4018 && CONSP (XCDR (spec)))
4019 {
4020 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4021 return 0;
4022
4023 value = XCAR (XCDR (spec));
4024 if (NUMBERP (value) && XFLOATINT (value) > 0)
4025 it->space_width = value;
4026
4027 return 0;
4028 }
4029
4030 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4031 if (CONSP (spec)
4032 && EQ (XCAR (spec), Qslice))
4033 {
4034 Lisp_Object tem;
4035
4036 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4037 return 0;
4038
4039 if (tem = XCDR (spec), CONSP (tem))
4040 {
4041 it->slice.x = XCAR (tem);
4042 if (tem = XCDR (tem), CONSP (tem))
4043 {
4044 it->slice.y = XCAR (tem);
4045 if (tem = XCDR (tem), CONSP (tem))
4046 {
4047 it->slice.width = XCAR (tem);
4048 if (tem = XCDR (tem), CONSP (tem))
4049 it->slice.height = XCAR (tem);
4050 }
4051 }
4052 }
4053
4054 return 0;
4055 }
4056
4057 /* Handle `(raise FACTOR)'. */
4058 if (CONSP (spec)
4059 && EQ (XCAR (spec), Qraise)
4060 && CONSP (XCDR (spec)))
4061 {
4062 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4063 return 0;
4064
4065 #ifdef HAVE_WINDOW_SYSTEM
4066 value = XCAR (XCDR (spec));
4067 if (NUMBERP (value))
4068 {
4069 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4070 it->voffset = - (XFLOATINT (value)
4071 * (FONT_HEIGHT (face->font)));
4072 }
4073 #endif /* HAVE_WINDOW_SYSTEM */
4074
4075 return 0;
4076 }
4077
4078 /* Don't handle the other kinds of display specifications
4079 inside a string that we got from a `display' property. */
4080 if (it->string_from_display_prop_p)
4081 return 0;
4082
4083 /* Characters having this form of property are not displayed, so
4084 we have to find the end of the property. */
4085 start_pos = *position;
4086 *position = display_prop_end (it, object, start_pos);
4087 value = Qnil;
4088
4089 /* Stop the scan at that end position--we assume that all
4090 text properties change there. */
4091 it->stop_charpos = position->charpos;
4092
4093 /* Handle `(left-fringe BITMAP [FACE])'
4094 and `(right-fringe BITMAP [FACE])'. */
4095 if (CONSP (spec)
4096 && (EQ (XCAR (spec), Qleft_fringe)
4097 || EQ (XCAR (spec), Qright_fringe))
4098 && CONSP (XCDR (spec)))
4099 {
4100 int face_id = DEFAULT_FACE_ID;
4101 int fringe_bitmap;
4102
4103 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4104 /* If we return here, POSITION has been advanced
4105 across the text with this property. */
4106 return 0;
4107
4108 #ifdef HAVE_WINDOW_SYSTEM
4109 value = XCAR (XCDR (spec));
4110 if (!SYMBOLP (value)
4111 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4112 /* If we return here, POSITION has been advanced
4113 across the text with this property. */
4114 return 0;
4115
4116 if (CONSP (XCDR (XCDR (spec))))
4117 {
4118 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4119 int face_id2 = lookup_derived_face (it->f, face_name,
4120 'A', FRINGE_FACE_ID, 0);
4121 if (face_id2 >= 0)
4122 face_id = face_id2;
4123 }
4124
4125 /* Save current settings of IT so that we can restore them
4126 when we are finished with the glyph property value. */
4127
4128 save_pos = it->position;
4129 it->position = *position;
4130 push_it (it);
4131 it->position = save_pos;
4132
4133 it->area = TEXT_AREA;
4134 it->what = IT_IMAGE;
4135 it->image_id = -1; /* no image */
4136 it->position = start_pos;
4137 it->object = NILP (object) ? it->w->buffer : object;
4138 it->method = GET_FROM_IMAGE;
4139 it->face_id = face_id;
4140
4141 /* Say that we haven't consumed the characters with
4142 `display' property yet. The call to pop_it in
4143 set_iterator_to_next will clean this up. */
4144 *position = start_pos;
4145
4146 if (EQ (XCAR (spec), Qleft_fringe))
4147 {
4148 it->left_user_fringe_bitmap = fringe_bitmap;
4149 it->left_user_fringe_face_id = face_id;
4150 }
4151 else
4152 {
4153 it->right_user_fringe_bitmap = fringe_bitmap;
4154 it->right_user_fringe_face_id = face_id;
4155 }
4156 #endif /* HAVE_WINDOW_SYSTEM */
4157 return 1;
4158 }
4159
4160 /* Prepare to handle `((margin left-margin) ...)',
4161 `((margin right-margin) ...)' and `((margin nil) ...)'
4162 prefixes for display specifications. */
4163 location = Qunbound;
4164 if (CONSP (spec) && CONSP (XCAR (spec)))
4165 {
4166 Lisp_Object tem;
4167
4168 value = XCDR (spec);
4169 if (CONSP (value))
4170 value = XCAR (value);
4171
4172 tem = XCAR (spec);
4173 if (EQ (XCAR (tem), Qmargin)
4174 && (tem = XCDR (tem),
4175 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4176 (NILP (tem)
4177 || EQ (tem, Qleft_margin)
4178 || EQ (tem, Qright_margin))))
4179 location = tem;
4180 }
4181
4182 if (EQ (location, Qunbound))
4183 {
4184 location = Qnil;
4185 value = spec;
4186 }
4187
4188 /* After this point, VALUE is the property after any
4189 margin prefix has been stripped. It must be a string,
4190 an image specification, or `(space ...)'.
4191
4192 LOCATION specifies where to display: `left-margin',
4193 `right-margin' or nil. */
4194
4195 valid_p = (STRINGP (value)
4196 #ifdef HAVE_WINDOW_SYSTEM
4197 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4198 #endif /* not HAVE_WINDOW_SYSTEM */
4199 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4200
4201 if (valid_p && !display_replaced_before_p)
4202 {
4203 /* Save current settings of IT so that we can restore them
4204 when we are finished with the glyph property value. */
4205 save_pos = it->position;
4206 it->position = *position;
4207 push_it (it);
4208 it->position = save_pos;
4209
4210 if (NILP (location))
4211 it->area = TEXT_AREA;
4212 else if (EQ (location, Qleft_margin))
4213 it->area = LEFT_MARGIN_AREA;
4214 else
4215 it->area = RIGHT_MARGIN_AREA;
4216
4217 if (STRINGP (value))
4218 {
4219 if (SCHARS (value) == 0)
4220 {
4221 pop_it (it);
4222 return -1; /* Replaced by "", i.e. nothing. */
4223 }
4224 it->string = value;
4225 it->multibyte_p = STRING_MULTIBYTE (it->string);
4226 it->current.overlay_string_index = -1;
4227 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4228 it->end_charpos = it->string_nchars = SCHARS (it->string);
4229 it->method = GET_FROM_STRING;
4230 it->stop_charpos = 0;
4231 it->string_from_display_prop_p = 1;
4232 /* Say that we haven't consumed the characters with
4233 `display' property yet. The call to pop_it in
4234 set_iterator_to_next will clean this up. */
4235 *position = start_pos;
4236 }
4237 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4238 {
4239 it->method = GET_FROM_STRETCH;
4240 it->object = value;
4241 *position = it->position = start_pos;
4242 }
4243 #ifdef HAVE_WINDOW_SYSTEM
4244 else
4245 {
4246 it->what = IT_IMAGE;
4247 it->image_id = lookup_image (it->f, value);
4248 it->position = start_pos;
4249 it->object = NILP (object) ? it->w->buffer : object;
4250 it->method = GET_FROM_IMAGE;
4251
4252 /* Say that we haven't consumed the characters with
4253 `display' property yet. The call to pop_it in
4254 set_iterator_to_next will clean this up. */
4255 *position = start_pos;
4256 }
4257 #endif /* HAVE_WINDOW_SYSTEM */
4258
4259 return 1;
4260 }
4261
4262 /* Invalid property or property not supported. Restore
4263 POSITION to what it was before. */
4264 *position = start_pos;
4265 return 0;
4266 }
4267
4268
4269 /* Check if SPEC is a display specification value whose text should be
4270 treated as intangible. */
4271
4272 static int
4273 single_display_spec_intangible_p (prop)
4274 Lisp_Object prop;
4275 {
4276 /* Skip over `when FORM'. */
4277 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4278 {
4279 prop = XCDR (prop);
4280 if (!CONSP (prop))
4281 return 0;
4282 prop = XCDR (prop);
4283 }
4284
4285 if (STRINGP (prop))
4286 return 1;
4287
4288 if (!CONSP (prop))
4289 return 0;
4290
4291 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4292 we don't need to treat text as intangible. */
4293 if (EQ (XCAR (prop), Qmargin))
4294 {
4295 prop = XCDR (prop);
4296 if (!CONSP (prop))
4297 return 0;
4298
4299 prop = XCDR (prop);
4300 if (!CONSP (prop)
4301 || EQ (XCAR (prop), Qleft_margin)
4302 || EQ (XCAR (prop), Qright_margin))
4303 return 0;
4304 }
4305
4306 return (CONSP (prop)
4307 && (EQ (XCAR (prop), Qimage)
4308 || EQ (XCAR (prop), Qspace)));
4309 }
4310
4311
4312 /* Check if PROP is a display property value whose text should be
4313 treated as intangible. */
4314
4315 int
4316 display_prop_intangible_p (prop)
4317 Lisp_Object prop;
4318 {
4319 if (CONSP (prop)
4320 && CONSP (XCAR (prop))
4321 && !EQ (Qmargin, XCAR (XCAR (prop))))
4322 {
4323 /* A list of sub-properties. */
4324 while (CONSP (prop))
4325 {
4326 if (single_display_spec_intangible_p (XCAR (prop)))
4327 return 1;
4328 prop = XCDR (prop);
4329 }
4330 }
4331 else if (VECTORP (prop))
4332 {
4333 /* A vector of sub-properties. */
4334 int i;
4335 for (i = 0; i < ASIZE (prop); ++i)
4336 if (single_display_spec_intangible_p (AREF (prop, i)))
4337 return 1;
4338 }
4339 else
4340 return single_display_spec_intangible_p (prop);
4341
4342 return 0;
4343 }
4344
4345
4346 /* Return 1 if PROP is a display sub-property value containing STRING. */
4347
4348 static int
4349 single_display_spec_string_p (prop, string)
4350 Lisp_Object prop, string;
4351 {
4352 if (EQ (string, prop))
4353 return 1;
4354
4355 /* Skip over `when FORM'. */
4356 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4357 {
4358 prop = XCDR (prop);
4359 if (!CONSP (prop))
4360 return 0;
4361 prop = XCDR (prop);
4362 }
4363
4364 if (CONSP (prop))
4365 /* Skip over `margin LOCATION'. */
4366 if (EQ (XCAR (prop), Qmargin))
4367 {
4368 prop = XCDR (prop);
4369 if (!CONSP (prop))
4370 return 0;
4371
4372 prop = XCDR (prop);
4373 if (!CONSP (prop))
4374 return 0;
4375 }
4376
4377 return CONSP (prop) && EQ (XCAR (prop), string);
4378 }
4379
4380
4381 /* Return 1 if STRING appears in the `display' property PROP. */
4382
4383 static int
4384 display_prop_string_p (prop, string)
4385 Lisp_Object prop, string;
4386 {
4387 if (CONSP (prop)
4388 && CONSP (XCAR (prop))
4389 && !EQ (Qmargin, XCAR (XCAR (prop))))
4390 {
4391 /* A list of sub-properties. */
4392 while (CONSP (prop))
4393 {
4394 if (single_display_spec_string_p (XCAR (prop), string))
4395 return 1;
4396 prop = XCDR (prop);
4397 }
4398 }
4399 else if (VECTORP (prop))
4400 {
4401 /* A vector of sub-properties. */
4402 int i;
4403 for (i = 0; i < ASIZE (prop); ++i)
4404 if (single_display_spec_string_p (AREF (prop, i), string))
4405 return 1;
4406 }
4407 else
4408 return single_display_spec_string_p (prop, string);
4409
4410 return 0;
4411 }
4412
4413
4414 /* Determine from which buffer position in W's buffer STRING comes
4415 from. AROUND_CHARPOS is an approximate position where it could
4416 be from. Value is the buffer position or 0 if it couldn't be
4417 determined.
4418
4419 W's buffer must be current.
4420
4421 This function is necessary because we don't record buffer positions
4422 in glyphs generated from strings (to keep struct glyph small).
4423 This function may only use code that doesn't eval because it is
4424 called asynchronously from note_mouse_highlight. */
4425
4426 int
4427 string_buffer_position (w, string, around_charpos)
4428 struct window *w;
4429 Lisp_Object string;
4430 int around_charpos;
4431 {
4432 Lisp_Object limit, prop, pos;
4433 const int MAX_DISTANCE = 1000;
4434 int found = 0;
4435
4436 pos = make_number (around_charpos);
4437 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4438 while (!found && !EQ (pos, limit))
4439 {
4440 prop = Fget_char_property (pos, Qdisplay, Qnil);
4441 if (!NILP (prop) && display_prop_string_p (prop, string))
4442 found = 1;
4443 else
4444 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4445 }
4446
4447 if (!found)
4448 {
4449 pos = make_number (around_charpos);
4450 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4451 while (!found && !EQ (pos, limit))
4452 {
4453 prop = Fget_char_property (pos, Qdisplay, Qnil);
4454 if (!NILP (prop) && display_prop_string_p (prop, string))
4455 found = 1;
4456 else
4457 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4458 limit);
4459 }
4460 }
4461
4462 return found ? XINT (pos) : 0;
4463 }
4464
4465
4466 \f
4467 /***********************************************************************
4468 `composition' property
4469 ***********************************************************************/
4470
4471 /* Set up iterator IT from `composition' property at its current
4472 position. Called from handle_stop. */
4473
4474 static enum prop_handled
4475 handle_composition_prop (it)
4476 struct it *it;
4477 {
4478 Lisp_Object prop, string;
4479 int pos, pos_byte, end;
4480 enum prop_handled handled = HANDLED_NORMALLY;
4481
4482 if (STRINGP (it->string))
4483 {
4484 pos = IT_STRING_CHARPOS (*it);
4485 pos_byte = IT_STRING_BYTEPOS (*it);
4486 string = it->string;
4487 }
4488 else
4489 {
4490 pos = IT_CHARPOS (*it);
4491 pos_byte = IT_BYTEPOS (*it);
4492 string = Qnil;
4493 }
4494
4495 /* If there's a valid composition and point is not inside of the
4496 composition (in the case that the composition is from the current
4497 buffer), draw a glyph composed from the composition components. */
4498 if (find_composition (pos, -1, &pos, &end, &prop, string)
4499 && COMPOSITION_VALID_P (pos, end, prop)
4500 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4501 {
4502 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4503
4504 if (id >= 0)
4505 {
4506 struct composition *cmp = composition_table[id];
4507
4508 if (cmp->glyph_len == 0)
4509 {
4510 /* No glyph. */
4511 if (STRINGP (it->string))
4512 {
4513 IT_STRING_CHARPOS (*it) = end;
4514 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4515 end);
4516 }
4517 else
4518 {
4519 IT_CHARPOS (*it) = end;
4520 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4521 }
4522 return HANDLED_RECOMPUTE_PROPS;
4523 }
4524
4525 it->stop_charpos = end;
4526 push_it (it);
4527
4528 it->method = GET_FROM_COMPOSITION;
4529 it->cmp_id = id;
4530 it->cmp_len = COMPOSITION_LENGTH (prop);
4531 /* For a terminal, draw only the first character of the
4532 components. */
4533 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4534 it->len = (STRINGP (it->string)
4535 ? string_char_to_byte (it->string, end)
4536 : CHAR_TO_BYTE (end)) - pos_byte;
4537 handled = HANDLED_RETURN;
4538 }
4539 }
4540
4541 return handled;
4542 }
4543
4544
4545 \f
4546 /***********************************************************************
4547 Overlay strings
4548 ***********************************************************************/
4549
4550 /* The following structure is used to record overlay strings for
4551 later sorting in load_overlay_strings. */
4552
4553 struct overlay_entry
4554 {
4555 Lisp_Object overlay;
4556 Lisp_Object string;
4557 int priority;
4558 int after_string_p;
4559 };
4560
4561
4562 /* Set up iterator IT from overlay strings at its current position.
4563 Called from handle_stop. */
4564
4565 static enum prop_handled
4566 handle_overlay_change (it)
4567 struct it *it;
4568 {
4569 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4570 return HANDLED_RECOMPUTE_PROPS;
4571 else
4572 return HANDLED_NORMALLY;
4573 }
4574
4575
4576 /* Set up the next overlay string for delivery by IT, if there is an
4577 overlay string to deliver. Called by set_iterator_to_next when the
4578 end of the current overlay string is reached. If there are more
4579 overlay strings to display, IT->string and
4580 IT->current.overlay_string_index are set appropriately here.
4581 Otherwise IT->string is set to nil. */
4582
4583 static void
4584 next_overlay_string (it)
4585 struct it *it;
4586 {
4587 ++it->current.overlay_string_index;
4588 if (it->current.overlay_string_index == it->n_overlay_strings)
4589 {
4590 /* No more overlay strings. Restore IT's settings to what
4591 they were before overlay strings were processed, and
4592 continue to deliver from current_buffer. */
4593 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4594
4595 pop_it (it);
4596 xassert (it->sp > 0
4597 || it->method == GET_FROM_COMPOSITION
4598 || (NILP (it->string)
4599 && it->method == GET_FROM_BUFFER
4600 && it->stop_charpos >= BEGV
4601 && it->stop_charpos <= it->end_charpos));
4602 it->current.overlay_string_index = -1;
4603 it->n_overlay_strings = 0;
4604
4605 /* If we're at the end of the buffer, record that we have
4606 processed the overlay strings there already, so that
4607 next_element_from_buffer doesn't try it again. */
4608 if (IT_CHARPOS (*it) >= it->end_charpos)
4609 it->overlay_strings_at_end_processed_p = 1;
4610
4611 /* If we have to display `...' for invisible text, set
4612 the iterator up for that. */
4613 if (display_ellipsis_p)
4614 setup_for_ellipsis (it, 0);
4615 }
4616 else
4617 {
4618 /* There are more overlay strings to process. If
4619 IT->current.overlay_string_index has advanced to a position
4620 where we must load IT->overlay_strings with more strings, do
4621 it. */
4622 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4623
4624 if (it->current.overlay_string_index && i == 0)
4625 load_overlay_strings (it, 0);
4626
4627 /* Initialize IT to deliver display elements from the overlay
4628 string. */
4629 it->string = it->overlay_strings[i];
4630 it->multibyte_p = STRING_MULTIBYTE (it->string);
4631 SET_TEXT_POS (it->current.string_pos, 0, 0);
4632 it->method = GET_FROM_STRING;
4633 it->stop_charpos = 0;
4634 }
4635
4636 CHECK_IT (it);
4637 }
4638
4639
4640 /* Compare two overlay_entry structures E1 and E2. Used as a
4641 comparison function for qsort in load_overlay_strings. Overlay
4642 strings for the same position are sorted so that
4643
4644 1. All after-strings come in front of before-strings, except
4645 when they come from the same overlay.
4646
4647 2. Within after-strings, strings are sorted so that overlay strings
4648 from overlays with higher priorities come first.
4649
4650 2. Within before-strings, strings are sorted so that overlay
4651 strings from overlays with higher priorities come last.
4652
4653 Value is analogous to strcmp. */
4654
4655
4656 static int
4657 compare_overlay_entries (e1, e2)
4658 void *e1, *e2;
4659 {
4660 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4661 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4662 int result;
4663
4664 if (entry1->after_string_p != entry2->after_string_p)
4665 {
4666 /* Let after-strings appear in front of before-strings if
4667 they come from different overlays. */
4668 if (EQ (entry1->overlay, entry2->overlay))
4669 result = entry1->after_string_p ? 1 : -1;
4670 else
4671 result = entry1->after_string_p ? -1 : 1;
4672 }
4673 else if (entry1->after_string_p)
4674 /* After-strings sorted in order of decreasing priority. */
4675 result = entry2->priority - entry1->priority;
4676 else
4677 /* Before-strings sorted in order of increasing priority. */
4678 result = entry1->priority - entry2->priority;
4679
4680 return result;
4681 }
4682
4683
4684 /* Load the vector IT->overlay_strings with overlay strings from IT's
4685 current buffer position, or from CHARPOS if that is > 0. Set
4686 IT->n_overlays to the total number of overlay strings found.
4687
4688 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4689 a time. On entry into load_overlay_strings,
4690 IT->current.overlay_string_index gives the number of overlay
4691 strings that have already been loaded by previous calls to this
4692 function.
4693
4694 IT->add_overlay_start contains an additional overlay start
4695 position to consider for taking overlay strings from, if non-zero.
4696 This position comes into play when the overlay has an `invisible'
4697 property, and both before and after-strings. When we've skipped to
4698 the end of the overlay, because of its `invisible' property, we
4699 nevertheless want its before-string to appear.
4700 IT->add_overlay_start will contain the overlay start position
4701 in this case.
4702
4703 Overlay strings are sorted so that after-string strings come in
4704 front of before-string strings. Within before and after-strings,
4705 strings are sorted by overlay priority. See also function
4706 compare_overlay_entries. */
4707
4708 static void
4709 load_overlay_strings (it, charpos)
4710 struct it *it;
4711 int charpos;
4712 {
4713 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4714 Lisp_Object overlay, window, str, invisible;
4715 struct Lisp_Overlay *ov;
4716 int start, end;
4717 int size = 20;
4718 int n = 0, i, j, invis_p;
4719 struct overlay_entry *entries
4720 = (struct overlay_entry *) alloca (size * sizeof *entries);
4721
4722 if (charpos <= 0)
4723 charpos = IT_CHARPOS (*it);
4724
4725 /* Append the overlay string STRING of overlay OVERLAY to vector
4726 `entries' which has size `size' and currently contains `n'
4727 elements. AFTER_P non-zero means STRING is an after-string of
4728 OVERLAY. */
4729 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4730 do \
4731 { \
4732 Lisp_Object priority; \
4733 \
4734 if (n == size) \
4735 { \
4736 int new_size = 2 * size; \
4737 struct overlay_entry *old = entries; \
4738 entries = \
4739 (struct overlay_entry *) alloca (new_size \
4740 * sizeof *entries); \
4741 bcopy (old, entries, size * sizeof *entries); \
4742 size = new_size; \
4743 } \
4744 \
4745 entries[n].string = (STRING); \
4746 entries[n].overlay = (OVERLAY); \
4747 priority = Foverlay_get ((OVERLAY), Qpriority); \
4748 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4749 entries[n].after_string_p = (AFTER_P); \
4750 ++n; \
4751 } \
4752 while (0)
4753
4754 /* Process overlay before the overlay center. */
4755 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4756 {
4757 XSETMISC (overlay, ov);
4758 xassert (OVERLAYP (overlay));
4759 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4760 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4761
4762 if (end < charpos)
4763 break;
4764
4765 /* Skip this overlay if it doesn't start or end at IT's current
4766 position. */
4767 if (end != charpos && start != charpos)
4768 continue;
4769
4770 /* Skip this overlay if it doesn't apply to IT->w. */
4771 window = Foverlay_get (overlay, Qwindow);
4772 if (WINDOWP (window) && XWINDOW (window) != it->w)
4773 continue;
4774
4775 /* If the text ``under'' the overlay is invisible, both before-
4776 and after-strings from this overlay are visible; start and
4777 end position are indistinguishable. */
4778 invisible = Foverlay_get (overlay, Qinvisible);
4779 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4780
4781 /* If overlay has a non-empty before-string, record it. */
4782 if ((start == charpos || (end == charpos && invis_p))
4783 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4784 && SCHARS (str))
4785 RECORD_OVERLAY_STRING (overlay, str, 0);
4786
4787 /* If overlay has a non-empty after-string, record it. */
4788 if ((end == charpos || (start == charpos && invis_p))
4789 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4790 && SCHARS (str))
4791 RECORD_OVERLAY_STRING (overlay, str, 1);
4792 }
4793
4794 /* Process overlays after the overlay center. */
4795 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4796 {
4797 XSETMISC (overlay, ov);
4798 xassert (OVERLAYP (overlay));
4799 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4800 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4801
4802 if (start > charpos)
4803 break;
4804
4805 /* Skip this overlay if it doesn't start or end at IT's current
4806 position. */
4807 if (end != charpos && start != charpos)
4808 continue;
4809
4810 /* Skip this overlay if it doesn't apply to IT->w. */
4811 window = Foverlay_get (overlay, Qwindow);
4812 if (WINDOWP (window) && XWINDOW (window) != it->w)
4813 continue;
4814
4815 /* If the text ``under'' the overlay is invisible, it has a zero
4816 dimension, and both before- and after-strings apply. */
4817 invisible = Foverlay_get (overlay, Qinvisible);
4818 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4819
4820 /* If overlay has a non-empty before-string, record it. */
4821 if ((start == charpos || (end == charpos && invis_p))
4822 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4823 && SCHARS (str))
4824 RECORD_OVERLAY_STRING (overlay, str, 0);
4825
4826 /* If overlay has a non-empty after-string, record it. */
4827 if ((end == charpos || (start == charpos && invis_p))
4828 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4829 && SCHARS (str))
4830 RECORD_OVERLAY_STRING (overlay, str, 1);
4831 }
4832
4833 #undef RECORD_OVERLAY_STRING
4834
4835 /* Sort entries. */
4836 if (n > 1)
4837 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4838
4839 /* Record the total number of strings to process. */
4840 it->n_overlay_strings = n;
4841
4842 /* IT->current.overlay_string_index is the number of overlay strings
4843 that have already been consumed by IT. Copy some of the
4844 remaining overlay strings to IT->overlay_strings. */
4845 i = 0;
4846 j = it->current.overlay_string_index;
4847 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4848 it->overlay_strings[i++] = entries[j++].string;
4849
4850 CHECK_IT (it);
4851 }
4852
4853
4854 /* Get the first chunk of overlay strings at IT's current buffer
4855 position, or at CHARPOS if that is > 0. Value is non-zero if at
4856 least one overlay string was found. */
4857
4858 static int
4859 get_overlay_strings_1 (it, charpos, compute_stop_p)
4860 struct it *it;
4861 int charpos;
4862 {
4863 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4864 process. This fills IT->overlay_strings with strings, and sets
4865 IT->n_overlay_strings to the total number of strings to process.
4866 IT->pos.overlay_string_index has to be set temporarily to zero
4867 because load_overlay_strings needs this; it must be set to -1
4868 when no overlay strings are found because a zero value would
4869 indicate a position in the first overlay string. */
4870 it->current.overlay_string_index = 0;
4871 load_overlay_strings (it, charpos);
4872
4873 /* If we found overlay strings, set up IT to deliver display
4874 elements from the first one. Otherwise set up IT to deliver
4875 from current_buffer. */
4876 if (it->n_overlay_strings)
4877 {
4878 /* Make sure we know settings in current_buffer, so that we can
4879 restore meaningful values when we're done with the overlay
4880 strings. */
4881 if (compute_stop_p)
4882 compute_stop_pos (it);
4883 xassert (it->face_id >= 0);
4884
4885 /* Save IT's settings. They are restored after all overlay
4886 strings have been processed. */
4887 xassert (!compute_stop_p || it->sp == 0);
4888 push_it (it);
4889
4890 /* Set up IT to deliver display elements from the first overlay
4891 string. */
4892 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4893 it->string = it->overlay_strings[0];
4894 it->stop_charpos = 0;
4895 xassert (STRINGP (it->string));
4896 it->end_charpos = SCHARS (it->string);
4897 it->multibyte_p = STRING_MULTIBYTE (it->string);
4898 it->method = GET_FROM_STRING;
4899 return 1;
4900 }
4901
4902 it->current.overlay_string_index = -1;
4903 return 0;
4904 }
4905
4906 static int
4907 get_overlay_strings (it, charpos)
4908 struct it *it;
4909 int charpos;
4910 {
4911 it->string = Qnil;
4912 it->method = GET_FROM_BUFFER;
4913
4914 (void) get_overlay_strings_1 (it, charpos, 1);
4915
4916 CHECK_IT (it);
4917
4918 /* Value is non-zero if we found at least one overlay string. */
4919 return STRINGP (it->string);
4920 }
4921
4922
4923 \f
4924 /***********************************************************************
4925 Saving and restoring state
4926 ***********************************************************************/
4927
4928 /* Save current settings of IT on IT->stack. Called, for example,
4929 before setting up IT for an overlay string, to be able to restore
4930 IT's settings to what they were after the overlay string has been
4931 processed. */
4932
4933 static void
4934 push_it (it)
4935 struct it *it;
4936 {
4937 struct iterator_stack_entry *p;
4938
4939 xassert (it->sp < IT_STACK_SIZE);
4940 p = it->stack + it->sp;
4941
4942 p->stop_charpos = it->stop_charpos;
4943 xassert (it->face_id >= 0);
4944 p->face_id = it->face_id;
4945 p->string = it->string;
4946 p->method = it->method;
4947 switch (p->method)
4948 {
4949 case GET_FROM_IMAGE:
4950 p->u.image.object = it->object;
4951 p->u.image.image_id = it->image_id;
4952 p->u.image.slice = it->slice;
4953 break;
4954 case GET_FROM_COMPOSITION:
4955 p->u.comp.object = it->object;
4956 p->u.comp.c = it->c;
4957 p->u.comp.len = it->len;
4958 p->u.comp.cmp_id = it->cmp_id;
4959 p->u.comp.cmp_len = it->cmp_len;
4960 break;
4961 case GET_FROM_STRETCH:
4962 p->u.stretch.object = it->object;
4963 break;
4964 }
4965 p->position = it->position;
4966 p->current = it->current;
4967 p->end_charpos = it->end_charpos;
4968 p->string_nchars = it->string_nchars;
4969 p->area = it->area;
4970 p->multibyte_p = it->multibyte_p;
4971 p->space_width = it->space_width;
4972 p->font_height = it->font_height;
4973 p->voffset = it->voffset;
4974 p->string_from_display_prop_p = it->string_from_display_prop_p;
4975 p->display_ellipsis_p = 0;
4976 ++it->sp;
4977 }
4978
4979
4980 /* Restore IT's settings from IT->stack. Called, for example, when no
4981 more overlay strings must be processed, and we return to delivering
4982 display elements from a buffer, or when the end of a string from a
4983 `display' property is reached and we return to delivering display
4984 elements from an overlay string, or from a buffer. */
4985
4986 static void
4987 pop_it (it)
4988 struct it *it;
4989 {
4990 struct iterator_stack_entry *p;
4991
4992 xassert (it->sp > 0);
4993 --it->sp;
4994 p = it->stack + it->sp;
4995 it->stop_charpos = p->stop_charpos;
4996 it->face_id = p->face_id;
4997 it->current = p->current;
4998 it->position = p->position;
4999 it->string = p->string;
5000 if (NILP (it->string))
5001 SET_TEXT_POS (it->current.string_pos, -1, -1);
5002 it->method = p->method;
5003 switch (it->method)
5004 {
5005 case GET_FROM_IMAGE:
5006 it->image_id = p->u.image.image_id;
5007 it->object = p->u.image.object;
5008 it->slice = p->u.image.slice;
5009 break;
5010 case GET_FROM_COMPOSITION:
5011 it->object = p->u.comp.object;
5012 it->c = p->u.comp.c;
5013 it->len = p->u.comp.len;
5014 it->cmp_id = p->u.comp.cmp_id;
5015 it->cmp_len = p->u.comp.cmp_len;
5016 break;
5017 case GET_FROM_STRETCH:
5018 it->object = p->u.comp.object;
5019 break;
5020 case GET_FROM_BUFFER:
5021 it->object = it->w->buffer;
5022 break;
5023 case GET_FROM_STRING:
5024 it->object = it->string;
5025 break;
5026 }
5027 it->end_charpos = p->end_charpos;
5028 it->string_nchars = p->string_nchars;
5029 it->area = p->area;
5030 it->multibyte_p = p->multibyte_p;
5031 it->space_width = p->space_width;
5032 it->font_height = p->font_height;
5033 it->voffset = p->voffset;
5034 it->string_from_display_prop_p = p->string_from_display_prop_p;
5035 }
5036
5037
5038 \f
5039 /***********************************************************************
5040 Moving over lines
5041 ***********************************************************************/
5042
5043 /* Set IT's current position to the previous line start. */
5044
5045 static void
5046 back_to_previous_line_start (it)
5047 struct it *it;
5048 {
5049 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5050 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5051 }
5052
5053
5054 /* Move IT to the next line start.
5055
5056 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5057 we skipped over part of the text (as opposed to moving the iterator
5058 continuously over the text). Otherwise, don't change the value
5059 of *SKIPPED_P.
5060
5061 Newlines may come from buffer text, overlay strings, or strings
5062 displayed via the `display' property. That's the reason we can't
5063 simply use find_next_newline_no_quit.
5064
5065 Note that this function may not skip over invisible text that is so
5066 because of text properties and immediately follows a newline. If
5067 it would, function reseat_at_next_visible_line_start, when called
5068 from set_iterator_to_next, would effectively make invisible
5069 characters following a newline part of the wrong glyph row, which
5070 leads to wrong cursor motion. */
5071
5072 static int
5073 forward_to_next_line_start (it, skipped_p)
5074 struct it *it;
5075 int *skipped_p;
5076 {
5077 int old_selective, newline_found_p, n;
5078 const int MAX_NEWLINE_DISTANCE = 500;
5079
5080 /* If already on a newline, just consume it to avoid unintended
5081 skipping over invisible text below. */
5082 if (it->what == IT_CHARACTER
5083 && it->c == '\n'
5084 && CHARPOS (it->position) == IT_CHARPOS (*it))
5085 {
5086 set_iterator_to_next (it, 0);
5087 it->c = 0;
5088 return 1;
5089 }
5090
5091 /* Don't handle selective display in the following. It's (a)
5092 unnecessary because it's done by the caller, and (b) leads to an
5093 infinite recursion because next_element_from_ellipsis indirectly
5094 calls this function. */
5095 old_selective = it->selective;
5096 it->selective = 0;
5097
5098 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5099 from buffer text. */
5100 for (n = newline_found_p = 0;
5101 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5102 n += STRINGP (it->string) ? 0 : 1)
5103 {
5104 if (!get_next_display_element (it))
5105 return 0;
5106 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5107 set_iterator_to_next (it, 0);
5108 }
5109
5110 /* If we didn't find a newline near enough, see if we can use a
5111 short-cut. */
5112 if (!newline_found_p)
5113 {
5114 int start = IT_CHARPOS (*it);
5115 int limit = find_next_newline_no_quit (start, 1);
5116 Lisp_Object pos;
5117
5118 xassert (!STRINGP (it->string));
5119
5120 /* If there isn't any `display' property in sight, and no
5121 overlays, we can just use the position of the newline in
5122 buffer text. */
5123 if (it->stop_charpos >= limit
5124 || ((pos = Fnext_single_property_change (make_number (start),
5125 Qdisplay,
5126 Qnil, make_number (limit)),
5127 NILP (pos))
5128 && next_overlay_change (start) == ZV))
5129 {
5130 IT_CHARPOS (*it) = limit;
5131 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5132 *skipped_p = newline_found_p = 1;
5133 }
5134 else
5135 {
5136 while (get_next_display_element (it)
5137 && !newline_found_p)
5138 {
5139 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5140 set_iterator_to_next (it, 0);
5141 }
5142 }
5143 }
5144
5145 it->selective = old_selective;
5146 return newline_found_p;
5147 }
5148
5149
5150 /* Set IT's current position to the previous visible line start. Skip
5151 invisible text that is so either due to text properties or due to
5152 selective display. Caution: this does not change IT->current_x and
5153 IT->hpos. */
5154
5155 static void
5156 back_to_previous_visible_line_start (it)
5157 struct it *it;
5158 {
5159 while (IT_CHARPOS (*it) > BEGV)
5160 {
5161 back_to_previous_line_start (it);
5162
5163 if (IT_CHARPOS (*it) <= BEGV)
5164 break;
5165
5166 /* If selective > 0, then lines indented more than that values
5167 are invisible. */
5168 if (it->selective > 0
5169 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5170 (double) it->selective)) /* iftc */
5171 continue;
5172
5173 /* Check the newline before point for invisibility. */
5174 {
5175 Lisp_Object prop;
5176 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5177 Qinvisible, it->window);
5178 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5179 continue;
5180 }
5181
5182 if (IT_CHARPOS (*it) <= BEGV)
5183 break;
5184
5185 {
5186 struct it it2;
5187 int pos;
5188 int beg, end;
5189 Lisp_Object val, overlay;
5190
5191 /* If newline is part of a composition, continue from start of composition */
5192 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5193 && beg < IT_CHARPOS (*it))
5194 goto replaced;
5195
5196 /* If newline is replaced by a display property, find start of overlay
5197 or interval and continue search from that point. */
5198 it2 = *it;
5199 pos = --IT_CHARPOS (it2);
5200 --IT_BYTEPOS (it2);
5201 it2.sp = 0;
5202 if (handle_display_prop (&it2) == HANDLED_RETURN
5203 && !NILP (val = get_char_property_and_overlay
5204 (make_number (pos), Qdisplay, Qnil, &overlay))
5205 && (OVERLAYP (overlay)
5206 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5207 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5208 goto replaced;
5209
5210 /* Newline is not replaced by anything -- so we are done. */
5211 break;
5212
5213 replaced:
5214 if (beg < BEGV)
5215 beg = BEGV;
5216 IT_CHARPOS (*it) = beg;
5217 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5218 }
5219 }
5220
5221 it->continuation_lines_width = 0;
5222
5223 xassert (IT_CHARPOS (*it) >= BEGV);
5224 xassert (IT_CHARPOS (*it) == BEGV
5225 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5226 CHECK_IT (it);
5227 }
5228
5229
5230 /* Reseat iterator IT at the previous visible line start. Skip
5231 invisible text that is so either due to text properties or due to
5232 selective display. At the end, update IT's overlay information,
5233 face information etc. */
5234
5235 void
5236 reseat_at_previous_visible_line_start (it)
5237 struct it *it;
5238 {
5239 back_to_previous_visible_line_start (it);
5240 reseat (it, it->current.pos, 1);
5241 CHECK_IT (it);
5242 }
5243
5244
5245 /* Reseat iterator IT on the next visible line start in the current
5246 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5247 preceding the line start. Skip over invisible text that is so
5248 because of selective display. Compute faces, overlays etc at the
5249 new position. Note that this function does not skip over text that
5250 is invisible because of text properties. */
5251
5252 static void
5253 reseat_at_next_visible_line_start (it, on_newline_p)
5254 struct it *it;
5255 int on_newline_p;
5256 {
5257 int newline_found_p, skipped_p = 0;
5258
5259 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5260
5261 /* Skip over lines that are invisible because they are indented
5262 more than the value of IT->selective. */
5263 if (it->selective > 0)
5264 while (IT_CHARPOS (*it) < ZV
5265 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5266 (double) it->selective)) /* iftc */
5267 {
5268 xassert (IT_BYTEPOS (*it) == BEGV
5269 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5270 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5271 }
5272
5273 /* Position on the newline if that's what's requested. */
5274 if (on_newline_p && newline_found_p)
5275 {
5276 if (STRINGP (it->string))
5277 {
5278 if (IT_STRING_CHARPOS (*it) > 0)
5279 {
5280 --IT_STRING_CHARPOS (*it);
5281 --IT_STRING_BYTEPOS (*it);
5282 }
5283 }
5284 else if (IT_CHARPOS (*it) > BEGV)
5285 {
5286 --IT_CHARPOS (*it);
5287 --IT_BYTEPOS (*it);
5288 reseat (it, it->current.pos, 0);
5289 }
5290 }
5291 else if (skipped_p)
5292 reseat (it, it->current.pos, 0);
5293
5294 CHECK_IT (it);
5295 }
5296
5297
5298 \f
5299 /***********************************************************************
5300 Changing an iterator's position
5301 ***********************************************************************/
5302
5303 /* Change IT's current position to POS in current_buffer. If FORCE_P
5304 is non-zero, always check for text properties at the new position.
5305 Otherwise, text properties are only looked up if POS >=
5306 IT->check_charpos of a property. */
5307
5308 static void
5309 reseat (it, pos, force_p)
5310 struct it *it;
5311 struct text_pos pos;
5312 int force_p;
5313 {
5314 int original_pos = IT_CHARPOS (*it);
5315
5316 reseat_1 (it, pos, 0);
5317
5318 /* Determine where to check text properties. Avoid doing it
5319 where possible because text property lookup is very expensive. */
5320 if (force_p
5321 || CHARPOS (pos) > it->stop_charpos
5322 || CHARPOS (pos) < original_pos)
5323 handle_stop (it);
5324
5325 CHECK_IT (it);
5326 }
5327
5328
5329 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5330 IT->stop_pos to POS, also. */
5331
5332 static void
5333 reseat_1 (it, pos, set_stop_p)
5334 struct it *it;
5335 struct text_pos pos;
5336 int set_stop_p;
5337 {
5338 /* Don't call this function when scanning a C string. */
5339 xassert (it->s == NULL);
5340
5341 /* POS must be a reasonable value. */
5342 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5343
5344 it->current.pos = it->position = pos;
5345 it->end_charpos = ZV;
5346 it->dpvec = NULL;
5347 it->current.dpvec_index = -1;
5348 it->current.overlay_string_index = -1;
5349 IT_STRING_CHARPOS (*it) = -1;
5350 IT_STRING_BYTEPOS (*it) = -1;
5351 it->string = Qnil;
5352 it->method = GET_FROM_BUFFER;
5353 it->object = it->w->buffer;
5354 it->area = TEXT_AREA;
5355 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5356 it->sp = 0;
5357 it->string_from_display_prop_p = 0;
5358 it->face_before_selective_p = 0;
5359
5360 if (set_stop_p)
5361 it->stop_charpos = CHARPOS (pos);
5362 }
5363
5364
5365 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5366 If S is non-null, it is a C string to iterate over. Otherwise,
5367 STRING gives a Lisp string to iterate over.
5368
5369 If PRECISION > 0, don't return more then PRECISION number of
5370 characters from the string.
5371
5372 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5373 characters have been returned. FIELD_WIDTH < 0 means an infinite
5374 field width.
5375
5376 MULTIBYTE = 0 means disable processing of multibyte characters,
5377 MULTIBYTE > 0 means enable it,
5378 MULTIBYTE < 0 means use IT->multibyte_p.
5379
5380 IT must be initialized via a prior call to init_iterator before
5381 calling this function. */
5382
5383 static void
5384 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5385 struct it *it;
5386 unsigned char *s;
5387 Lisp_Object string;
5388 int charpos;
5389 int precision, field_width, multibyte;
5390 {
5391 /* No region in strings. */
5392 it->region_beg_charpos = it->region_end_charpos = -1;
5393
5394 /* No text property checks performed by default, but see below. */
5395 it->stop_charpos = -1;
5396
5397 /* Set iterator position and end position. */
5398 bzero (&it->current, sizeof it->current);
5399 it->current.overlay_string_index = -1;
5400 it->current.dpvec_index = -1;
5401 xassert (charpos >= 0);
5402
5403 /* If STRING is specified, use its multibyteness, otherwise use the
5404 setting of MULTIBYTE, if specified. */
5405 if (multibyte >= 0)
5406 it->multibyte_p = multibyte > 0;
5407
5408 if (s == NULL)
5409 {
5410 xassert (STRINGP (string));
5411 it->string = string;
5412 it->s = NULL;
5413 it->end_charpos = it->string_nchars = SCHARS (string);
5414 it->method = GET_FROM_STRING;
5415 it->current.string_pos = string_pos (charpos, string);
5416 }
5417 else
5418 {
5419 it->s = s;
5420 it->string = Qnil;
5421
5422 /* Note that we use IT->current.pos, not it->current.string_pos,
5423 for displaying C strings. */
5424 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5425 if (it->multibyte_p)
5426 {
5427 it->current.pos = c_string_pos (charpos, s, 1);
5428 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5429 }
5430 else
5431 {
5432 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5433 it->end_charpos = it->string_nchars = strlen (s);
5434 }
5435
5436 it->method = GET_FROM_C_STRING;
5437 }
5438
5439 /* PRECISION > 0 means don't return more than PRECISION characters
5440 from the string. */
5441 if (precision > 0 && it->end_charpos - charpos > precision)
5442 it->end_charpos = it->string_nchars = charpos + precision;
5443
5444 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5445 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5446 FIELD_WIDTH < 0 means infinite field width. This is useful for
5447 padding with `-' at the end of a mode line. */
5448 if (field_width < 0)
5449 field_width = INFINITY;
5450 if (field_width > it->end_charpos - charpos)
5451 it->end_charpos = charpos + field_width;
5452
5453 /* Use the standard display table for displaying strings. */
5454 if (DISP_TABLE_P (Vstandard_display_table))
5455 it->dp = XCHAR_TABLE (Vstandard_display_table);
5456
5457 it->stop_charpos = charpos;
5458 CHECK_IT (it);
5459 }
5460
5461
5462 \f
5463 /***********************************************************************
5464 Iteration
5465 ***********************************************************************/
5466
5467 /* Map enum it_method value to corresponding next_element_from_* function. */
5468
5469 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5470 {
5471 next_element_from_buffer,
5472 next_element_from_display_vector,
5473 next_element_from_composition,
5474 next_element_from_string,
5475 next_element_from_c_string,
5476 next_element_from_image,
5477 next_element_from_stretch
5478 };
5479
5480
5481 /* Load IT's display element fields with information about the next
5482 display element from the current position of IT. Value is zero if
5483 end of buffer (or C string) is reached. */
5484
5485 static struct frame *last_escape_glyph_frame = NULL;
5486 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5487 static int last_escape_glyph_merged_face_id = 0;
5488
5489 int
5490 get_next_display_element (it)
5491 struct it *it;
5492 {
5493 /* Non-zero means that we found a display element. Zero means that
5494 we hit the end of what we iterate over. Performance note: the
5495 function pointer `method' used here turns out to be faster than
5496 using a sequence of if-statements. */
5497 int success_p;
5498
5499 get_next:
5500 success_p = (*get_next_element[it->method]) (it);
5501
5502 if (it->what == IT_CHARACTER)
5503 {
5504 /* Map via display table or translate control characters.
5505 IT->c, IT->len etc. have been set to the next character by
5506 the function call above. If we have a display table, and it
5507 contains an entry for IT->c, translate it. Don't do this if
5508 IT->c itself comes from a display table, otherwise we could
5509 end up in an infinite recursion. (An alternative could be to
5510 count the recursion depth of this function and signal an
5511 error when a certain maximum depth is reached.) Is it worth
5512 it? */
5513 if (success_p && it->dpvec == NULL)
5514 {
5515 Lisp_Object dv;
5516
5517 if (it->dp
5518 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5519 VECTORP (dv)))
5520 {
5521 struct Lisp_Vector *v = XVECTOR (dv);
5522
5523 /* Return the first character from the display table
5524 entry, if not empty. If empty, don't display the
5525 current character. */
5526 if (v->size)
5527 {
5528 it->dpvec_char_len = it->len;
5529 it->dpvec = v->contents;
5530 it->dpend = v->contents + v->size;
5531 it->current.dpvec_index = 0;
5532 it->dpvec_face_id = -1;
5533 it->saved_face_id = it->face_id;
5534 it->method = GET_FROM_DISPLAY_VECTOR;
5535 it->ellipsis_p = 0;
5536 }
5537 else
5538 {
5539 set_iterator_to_next (it, 0);
5540 }
5541 goto get_next;
5542 }
5543
5544 /* Translate control characters into `\003' or `^C' form.
5545 Control characters coming from a display table entry are
5546 currently not translated because we use IT->dpvec to hold
5547 the translation. This could easily be changed but I
5548 don't believe that it is worth doing.
5549
5550 If it->multibyte_p is nonzero, eight-bit characters and
5551 non-printable multibyte characters are also translated to
5552 octal form.
5553
5554 If it->multibyte_p is zero, eight-bit characters that
5555 don't have corresponding multibyte char code are also
5556 translated to octal form. */
5557 else if ((it->c < ' '
5558 && (it->area != TEXT_AREA
5559 /* In mode line, treat \n like other crl chars. */
5560 || (it->c != '\t'
5561 && it->glyph_row && it->glyph_row->mode_line_p)
5562 || (it->c != '\n' && it->c != '\t')))
5563 || (it->multibyte_p
5564 ? ((it->c >= 127
5565 && it->len == 1)
5566 || !CHAR_PRINTABLE_P (it->c)
5567 || (!NILP (Vnobreak_char_display)
5568 && (it->c == 0x8a0 || it->c == 0x8ad
5569 || it->c == 0x920 || it->c == 0x92d
5570 || it->c == 0xe20 || it->c == 0xe2d
5571 || it->c == 0xf20 || it->c == 0xf2d)))
5572 : (it->c >= 127
5573 && (!unibyte_display_via_language_environment
5574 || it->c == unibyte_char_to_multibyte (it->c)))))
5575 {
5576 /* IT->c is a control character which must be displayed
5577 either as '\003' or as `^C' where the '\\' and '^'
5578 can be defined in the display table. Fill
5579 IT->ctl_chars with glyphs for what we have to
5580 display. Then, set IT->dpvec to these glyphs. */
5581 GLYPH g;
5582 int ctl_len;
5583 int face_id, lface_id = 0 ;
5584 GLYPH escape_glyph;
5585
5586 /* Handle control characters with ^. */
5587
5588 if (it->c < 128 && it->ctl_arrow_p)
5589 {
5590 g = '^'; /* default glyph for Control */
5591 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5592 if (it->dp
5593 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5594 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5595 {
5596 g = XINT (DISP_CTRL_GLYPH (it->dp));
5597 lface_id = FAST_GLYPH_FACE (g);
5598 }
5599 if (lface_id)
5600 {
5601 g = FAST_GLYPH_CHAR (g);
5602 face_id = merge_faces (it->f, Qt, lface_id,
5603 it->face_id);
5604 }
5605 else if (it->f == last_escape_glyph_frame
5606 && it->face_id == last_escape_glyph_face_id)
5607 {
5608 face_id = last_escape_glyph_merged_face_id;
5609 }
5610 else
5611 {
5612 /* Merge the escape-glyph face into the current face. */
5613 face_id = merge_faces (it->f, Qescape_glyph, 0,
5614 it->face_id);
5615 last_escape_glyph_frame = it->f;
5616 last_escape_glyph_face_id = it->face_id;
5617 last_escape_glyph_merged_face_id = face_id;
5618 }
5619
5620 XSETINT (it->ctl_chars[0], g);
5621 g = it->c ^ 0100;
5622 XSETINT (it->ctl_chars[1], g);
5623 ctl_len = 2;
5624 goto display_control;
5625 }
5626
5627 /* Handle non-break space in the mode where it only gets
5628 highlighting. */
5629
5630 if (EQ (Vnobreak_char_display, Qt)
5631 && (it->c == 0x8a0 || it->c == 0x920
5632 || it->c == 0xe20 || it->c == 0xf20))
5633 {
5634 /* Merge the no-break-space face into the current face. */
5635 face_id = merge_faces (it->f, Qnobreak_space, 0,
5636 it->face_id);
5637
5638 g = it->c = ' ';
5639 XSETINT (it->ctl_chars[0], g);
5640 ctl_len = 1;
5641 goto display_control;
5642 }
5643
5644 /* Handle sequences that start with the "escape glyph". */
5645
5646 /* the default escape glyph is \. */
5647 escape_glyph = '\\';
5648
5649 if (it->dp
5650 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5651 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5652 {
5653 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5654 lface_id = FAST_GLYPH_FACE (escape_glyph);
5655 }
5656 if (lface_id)
5657 {
5658 /* The display table specified a face.
5659 Merge it into face_id and also into escape_glyph. */
5660 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5661 face_id = merge_faces (it->f, Qt, lface_id,
5662 it->face_id);
5663 }
5664 else if (it->f == last_escape_glyph_frame
5665 && it->face_id == last_escape_glyph_face_id)
5666 {
5667 face_id = last_escape_glyph_merged_face_id;
5668 }
5669 else
5670 {
5671 /* Merge the escape-glyph face into the current face. */
5672 face_id = merge_faces (it->f, Qescape_glyph, 0,
5673 it->face_id);
5674 last_escape_glyph_frame = it->f;
5675 last_escape_glyph_face_id = it->face_id;
5676 last_escape_glyph_merged_face_id = face_id;
5677 }
5678
5679 /* Handle soft hyphens in the mode where they only get
5680 highlighting. */
5681
5682 if (EQ (Vnobreak_char_display, Qt)
5683 && (it->c == 0x8ad || it->c == 0x92d
5684 || it->c == 0xe2d || it->c == 0xf2d))
5685 {
5686 g = it->c = '-';
5687 XSETINT (it->ctl_chars[0], g);
5688 ctl_len = 1;
5689 goto display_control;
5690 }
5691
5692 /* Handle non-break space and soft hyphen
5693 with the escape glyph. */
5694
5695 if (it->c == 0x8a0 || it->c == 0x8ad
5696 || it->c == 0x920 || it->c == 0x92d
5697 || it->c == 0xe20 || it->c == 0xe2d
5698 || it->c == 0xf20 || it->c == 0xf2d)
5699 {
5700 XSETINT (it->ctl_chars[0], escape_glyph);
5701 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5702 XSETINT (it->ctl_chars[1], g);
5703 ctl_len = 2;
5704 goto display_control;
5705 }
5706
5707 {
5708 unsigned char str[MAX_MULTIBYTE_LENGTH];
5709 int len;
5710 int i;
5711
5712 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5713 if (SINGLE_BYTE_CHAR_P (it->c))
5714 str[0] = it->c, len = 1;
5715 else
5716 {
5717 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5718 if (len < 0)
5719 {
5720 /* It's an invalid character, which shouldn't
5721 happen actually, but due to bugs it may
5722 happen. Let's print the char as is, there's
5723 not much meaningful we can do with it. */
5724 str[0] = it->c;
5725 str[1] = it->c >> 8;
5726 str[2] = it->c >> 16;
5727 str[3] = it->c >> 24;
5728 len = 4;
5729 }
5730 }
5731
5732 for (i = 0; i < len; i++)
5733 {
5734 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5735 /* Insert three more glyphs into IT->ctl_chars for
5736 the octal display of the character. */
5737 g = ((str[i] >> 6) & 7) + '0';
5738 XSETINT (it->ctl_chars[i * 4 + 1], g);
5739 g = ((str[i] >> 3) & 7) + '0';
5740 XSETINT (it->ctl_chars[i * 4 + 2], g);
5741 g = (str[i] & 7) + '0';
5742 XSETINT (it->ctl_chars[i * 4 + 3], g);
5743 }
5744 ctl_len = len * 4;
5745 }
5746
5747 display_control:
5748 /* Set up IT->dpvec and return first character from it. */
5749 it->dpvec_char_len = it->len;
5750 it->dpvec = it->ctl_chars;
5751 it->dpend = it->dpvec + ctl_len;
5752 it->current.dpvec_index = 0;
5753 it->dpvec_face_id = face_id;
5754 it->saved_face_id = it->face_id;
5755 it->method = GET_FROM_DISPLAY_VECTOR;
5756 it->ellipsis_p = 0;
5757 goto get_next;
5758 }
5759 }
5760
5761 /* Adjust face id for a multibyte character. There are no
5762 multibyte character in unibyte text. */
5763 if (it->multibyte_p
5764 && success_p
5765 && FRAME_WINDOW_P (it->f))
5766 {
5767 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5768 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5769 }
5770 }
5771
5772 /* Is this character the last one of a run of characters with
5773 box? If yes, set IT->end_of_box_run_p to 1. */
5774 if (it->face_box_p
5775 && it->s == NULL)
5776 {
5777 int face_id;
5778 struct face *face;
5779
5780 it->end_of_box_run_p
5781 = ((face_id = face_after_it_pos (it),
5782 face_id != it->face_id)
5783 && (face = FACE_FROM_ID (it->f, face_id),
5784 face->box == FACE_NO_BOX));
5785 }
5786
5787 /* Value is 0 if end of buffer or string reached. */
5788 return success_p;
5789 }
5790
5791
5792 /* Move IT to the next display element.
5793
5794 RESEAT_P non-zero means if called on a newline in buffer text,
5795 skip to the next visible line start.
5796
5797 Functions get_next_display_element and set_iterator_to_next are
5798 separate because I find this arrangement easier to handle than a
5799 get_next_display_element function that also increments IT's
5800 position. The way it is we can first look at an iterator's current
5801 display element, decide whether it fits on a line, and if it does,
5802 increment the iterator position. The other way around we probably
5803 would either need a flag indicating whether the iterator has to be
5804 incremented the next time, or we would have to implement a
5805 decrement position function which would not be easy to write. */
5806
5807 void
5808 set_iterator_to_next (it, reseat_p)
5809 struct it *it;
5810 int reseat_p;
5811 {
5812 /* Reset flags indicating start and end of a sequence of characters
5813 with box. Reset them at the start of this function because
5814 moving the iterator to a new position might set them. */
5815 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5816
5817 switch (it->method)
5818 {
5819 case GET_FROM_BUFFER:
5820 /* The current display element of IT is a character from
5821 current_buffer. Advance in the buffer, and maybe skip over
5822 invisible lines that are so because of selective display. */
5823 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5824 reseat_at_next_visible_line_start (it, 0);
5825 else
5826 {
5827 xassert (it->len != 0);
5828 IT_BYTEPOS (*it) += it->len;
5829 IT_CHARPOS (*it) += 1;
5830 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5831 }
5832 break;
5833
5834 case GET_FROM_COMPOSITION:
5835 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5836 xassert (it->sp > 0);
5837 pop_it (it);
5838 if (it->method == GET_FROM_STRING)
5839 {
5840 IT_STRING_BYTEPOS (*it) += it->len;
5841 IT_STRING_CHARPOS (*it) += it->cmp_len;
5842 goto consider_string_end;
5843 }
5844 else if (it->method == GET_FROM_BUFFER)
5845 {
5846 IT_BYTEPOS (*it) += it->len;
5847 IT_CHARPOS (*it) += it->cmp_len;
5848 }
5849 break;
5850
5851 case GET_FROM_C_STRING:
5852 /* Current display element of IT is from a C string. */
5853 IT_BYTEPOS (*it) += it->len;
5854 IT_CHARPOS (*it) += 1;
5855 break;
5856
5857 case GET_FROM_DISPLAY_VECTOR:
5858 /* Current display element of IT is from a display table entry.
5859 Advance in the display table definition. Reset it to null if
5860 end reached, and continue with characters from buffers/
5861 strings. */
5862 ++it->current.dpvec_index;
5863
5864 /* Restore face of the iterator to what they were before the
5865 display vector entry (these entries may contain faces). */
5866 it->face_id = it->saved_face_id;
5867
5868 if (it->dpvec + it->current.dpvec_index == it->dpend)
5869 {
5870 int recheck_faces = it->ellipsis_p;
5871
5872 if (it->s)
5873 it->method = GET_FROM_C_STRING;
5874 else if (STRINGP (it->string))
5875 it->method = GET_FROM_STRING;
5876 else
5877 {
5878 it->method = GET_FROM_BUFFER;
5879 it->object = it->w->buffer;
5880 }
5881
5882 it->dpvec = NULL;
5883 it->current.dpvec_index = -1;
5884
5885 /* Skip over characters which were displayed via IT->dpvec. */
5886 if (it->dpvec_char_len < 0)
5887 reseat_at_next_visible_line_start (it, 1);
5888 else if (it->dpvec_char_len > 0)
5889 {
5890 if (it->method == GET_FROM_STRING
5891 && it->n_overlay_strings > 0)
5892 it->ignore_overlay_strings_at_pos_p = 1;
5893 it->len = it->dpvec_char_len;
5894 set_iterator_to_next (it, reseat_p);
5895 }
5896
5897 /* Maybe recheck faces after display vector */
5898 if (recheck_faces)
5899 it->stop_charpos = IT_CHARPOS (*it);
5900 }
5901 break;
5902
5903 case GET_FROM_STRING:
5904 /* Current display element is a character from a Lisp string. */
5905 xassert (it->s == NULL && STRINGP (it->string));
5906 IT_STRING_BYTEPOS (*it) += it->len;
5907 IT_STRING_CHARPOS (*it) += 1;
5908
5909 consider_string_end:
5910
5911 if (it->current.overlay_string_index >= 0)
5912 {
5913 /* IT->string is an overlay string. Advance to the
5914 next, if there is one. */
5915 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5916 next_overlay_string (it);
5917 }
5918 else
5919 {
5920 /* IT->string is not an overlay string. If we reached
5921 its end, and there is something on IT->stack, proceed
5922 with what is on the stack. This can be either another
5923 string, this time an overlay string, or a buffer. */
5924 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5925 && it->sp > 0)
5926 {
5927 pop_it (it);
5928 if (it->method == GET_FROM_STRING)
5929 goto consider_string_end;
5930 }
5931 }
5932 break;
5933
5934 case GET_FROM_IMAGE:
5935 case GET_FROM_STRETCH:
5936 /* The position etc with which we have to proceed are on
5937 the stack. The position may be at the end of a string,
5938 if the `display' property takes up the whole string. */
5939 xassert (it->sp > 0);
5940 pop_it (it);
5941 if (it->method == GET_FROM_STRING)
5942 goto consider_string_end;
5943 break;
5944
5945 default:
5946 /* There are no other methods defined, so this should be a bug. */
5947 abort ();
5948 }
5949
5950 xassert (it->method != GET_FROM_STRING
5951 || (STRINGP (it->string)
5952 && IT_STRING_CHARPOS (*it) >= 0));
5953 }
5954
5955 /* Load IT's display element fields with information about the next
5956 display element which comes from a display table entry or from the
5957 result of translating a control character to one of the forms `^C'
5958 or `\003'.
5959
5960 IT->dpvec holds the glyphs to return as characters.
5961 IT->saved_face_id holds the face id before the display vector--
5962 it is restored into IT->face_idin set_iterator_to_next. */
5963
5964 static int
5965 next_element_from_display_vector (it)
5966 struct it *it;
5967 {
5968 /* Precondition. */
5969 xassert (it->dpvec && it->current.dpvec_index >= 0);
5970
5971 it->face_id = it->saved_face_id;
5972
5973 if (INTEGERP (*it->dpvec)
5974 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5975 {
5976 GLYPH g;
5977
5978 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5979 it->c = FAST_GLYPH_CHAR (g);
5980 it->len = CHAR_BYTES (it->c);
5981
5982 /* The entry may contain a face id to use. Such a face id is
5983 the id of a Lisp face, not a realized face. A face id of
5984 zero means no face is specified. */
5985 if (it->dpvec_face_id >= 0)
5986 it->face_id = it->dpvec_face_id;
5987 else
5988 {
5989 int lface_id = FAST_GLYPH_FACE (g);
5990 if (lface_id > 0)
5991 it->face_id = merge_faces (it->f, Qt, lface_id,
5992 it->saved_face_id);
5993 }
5994 }
5995 else
5996 /* Display table entry is invalid. Return a space. */
5997 it->c = ' ', it->len = 1;
5998
5999 /* Don't change position and object of the iterator here. They are
6000 still the values of the character that had this display table
6001 entry or was translated, and that's what we want. */
6002 it->what = IT_CHARACTER;
6003 return 1;
6004 }
6005
6006
6007 /* Load IT with the next display element from Lisp string IT->string.
6008 IT->current.string_pos is the current position within the string.
6009 If IT->current.overlay_string_index >= 0, the Lisp string is an
6010 overlay string. */
6011
6012 static int
6013 next_element_from_string (it)
6014 struct it *it;
6015 {
6016 struct text_pos position;
6017
6018 xassert (STRINGP (it->string));
6019 xassert (IT_STRING_CHARPOS (*it) >= 0);
6020 position = it->current.string_pos;
6021
6022 /* Time to check for invisible text? */
6023 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6024 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6025 {
6026 handle_stop (it);
6027
6028 /* Since a handler may have changed IT->method, we must
6029 recurse here. */
6030 return get_next_display_element (it);
6031 }
6032
6033 if (it->current.overlay_string_index >= 0)
6034 {
6035 /* Get the next character from an overlay string. In overlay
6036 strings, There is no field width or padding with spaces to
6037 do. */
6038 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6039 {
6040 it->what = IT_EOB;
6041 return 0;
6042 }
6043 else if (STRING_MULTIBYTE (it->string))
6044 {
6045 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6046 const unsigned char *s = (SDATA (it->string)
6047 + IT_STRING_BYTEPOS (*it));
6048 it->c = string_char_and_length (s, remaining, &it->len);
6049 }
6050 else
6051 {
6052 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6053 it->len = 1;
6054 }
6055 }
6056 else
6057 {
6058 /* Get the next character from a Lisp string that is not an
6059 overlay string. Such strings come from the mode line, for
6060 example. We may have to pad with spaces, or truncate the
6061 string. See also next_element_from_c_string. */
6062 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6063 {
6064 it->what = IT_EOB;
6065 return 0;
6066 }
6067 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6068 {
6069 /* Pad with spaces. */
6070 it->c = ' ', it->len = 1;
6071 CHARPOS (position) = BYTEPOS (position) = -1;
6072 }
6073 else if (STRING_MULTIBYTE (it->string))
6074 {
6075 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6076 const unsigned char *s = (SDATA (it->string)
6077 + IT_STRING_BYTEPOS (*it));
6078 it->c = string_char_and_length (s, maxlen, &it->len);
6079 }
6080 else
6081 {
6082 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6083 it->len = 1;
6084 }
6085 }
6086
6087 /* Record what we have and where it came from. */
6088 it->what = IT_CHARACTER;
6089 it->object = it->string;
6090 it->position = position;
6091 return 1;
6092 }
6093
6094
6095 /* Load IT with next display element from C string IT->s.
6096 IT->string_nchars is the maximum number of characters to return
6097 from the string. IT->end_charpos may be greater than
6098 IT->string_nchars when this function is called, in which case we
6099 may have to return padding spaces. Value is zero if end of string
6100 reached, including padding spaces. */
6101
6102 static int
6103 next_element_from_c_string (it)
6104 struct it *it;
6105 {
6106 int success_p = 1;
6107
6108 xassert (it->s);
6109 it->what = IT_CHARACTER;
6110 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6111 it->object = Qnil;
6112
6113 /* IT's position can be greater IT->string_nchars in case a field
6114 width or precision has been specified when the iterator was
6115 initialized. */
6116 if (IT_CHARPOS (*it) >= it->end_charpos)
6117 {
6118 /* End of the game. */
6119 it->what = IT_EOB;
6120 success_p = 0;
6121 }
6122 else if (IT_CHARPOS (*it) >= it->string_nchars)
6123 {
6124 /* Pad with spaces. */
6125 it->c = ' ', it->len = 1;
6126 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6127 }
6128 else if (it->multibyte_p)
6129 {
6130 /* Implementation note: The calls to strlen apparently aren't a
6131 performance problem because there is no noticeable performance
6132 difference between Emacs running in unibyte or multibyte mode. */
6133 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6134 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6135 maxlen, &it->len);
6136 }
6137 else
6138 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6139
6140 return success_p;
6141 }
6142
6143
6144 /* Set up IT to return characters from an ellipsis, if appropriate.
6145 The definition of the ellipsis glyphs may come from a display table
6146 entry. This function Fills IT with the first glyph from the
6147 ellipsis if an ellipsis is to be displayed. */
6148
6149 static int
6150 next_element_from_ellipsis (it)
6151 struct it *it;
6152 {
6153 if (it->selective_display_ellipsis_p)
6154 setup_for_ellipsis (it, it->len);
6155 else
6156 {
6157 /* The face at the current position may be different from the
6158 face we find after the invisible text. Remember what it
6159 was in IT->saved_face_id, and signal that it's there by
6160 setting face_before_selective_p. */
6161 it->saved_face_id = it->face_id;
6162 it->method = GET_FROM_BUFFER;
6163 it->object = it->w->buffer;
6164 reseat_at_next_visible_line_start (it, 1);
6165 it->face_before_selective_p = 1;
6166 }
6167
6168 return get_next_display_element (it);
6169 }
6170
6171
6172 /* Deliver an image display element. The iterator IT is already
6173 filled with image information (done in handle_display_prop). Value
6174 is always 1. */
6175
6176
6177 static int
6178 next_element_from_image (it)
6179 struct it *it;
6180 {
6181 it->what = IT_IMAGE;
6182 return 1;
6183 }
6184
6185
6186 /* Fill iterator IT with next display element from a stretch glyph
6187 property. IT->object is the value of the text property. Value is
6188 always 1. */
6189
6190 static int
6191 next_element_from_stretch (it)
6192 struct it *it;
6193 {
6194 it->what = IT_STRETCH;
6195 return 1;
6196 }
6197
6198
6199 /* Load IT with the next display element from current_buffer. Value
6200 is zero if end of buffer reached. IT->stop_charpos is the next
6201 position at which to stop and check for text properties or buffer
6202 end. */
6203
6204 static int
6205 next_element_from_buffer (it)
6206 struct it *it;
6207 {
6208 int success_p = 1;
6209
6210 /* Check this assumption, otherwise, we would never enter the
6211 if-statement, below. */
6212 xassert (IT_CHARPOS (*it) >= BEGV
6213 && IT_CHARPOS (*it) <= it->stop_charpos);
6214
6215 if (IT_CHARPOS (*it) >= it->stop_charpos)
6216 {
6217 if (IT_CHARPOS (*it) >= it->end_charpos)
6218 {
6219 int overlay_strings_follow_p;
6220
6221 /* End of the game, except when overlay strings follow that
6222 haven't been returned yet. */
6223 if (it->overlay_strings_at_end_processed_p)
6224 overlay_strings_follow_p = 0;
6225 else
6226 {
6227 it->overlay_strings_at_end_processed_p = 1;
6228 overlay_strings_follow_p = get_overlay_strings (it, 0);
6229 }
6230
6231 if (overlay_strings_follow_p)
6232 success_p = get_next_display_element (it);
6233 else
6234 {
6235 it->what = IT_EOB;
6236 it->position = it->current.pos;
6237 success_p = 0;
6238 }
6239 }
6240 else
6241 {
6242 handle_stop (it);
6243 return get_next_display_element (it);
6244 }
6245 }
6246 else
6247 {
6248 /* No face changes, overlays etc. in sight, so just return a
6249 character from current_buffer. */
6250 unsigned char *p;
6251
6252 /* Maybe run the redisplay end trigger hook. Performance note:
6253 This doesn't seem to cost measurable time. */
6254 if (it->redisplay_end_trigger_charpos
6255 && it->glyph_row
6256 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6257 run_redisplay_end_trigger_hook (it);
6258
6259 /* Get the next character, maybe multibyte. */
6260 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6261 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6262 {
6263 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6264 - IT_BYTEPOS (*it));
6265 it->c = string_char_and_length (p, maxlen, &it->len);
6266 }
6267 else
6268 it->c = *p, it->len = 1;
6269
6270 /* Record what we have and where it came from. */
6271 it->what = IT_CHARACTER;;
6272 it->object = it->w->buffer;
6273 it->position = it->current.pos;
6274
6275 /* Normally we return the character found above, except when we
6276 really want to return an ellipsis for selective display. */
6277 if (it->selective)
6278 {
6279 if (it->c == '\n')
6280 {
6281 /* A value of selective > 0 means hide lines indented more
6282 than that number of columns. */
6283 if (it->selective > 0
6284 && IT_CHARPOS (*it) + 1 < ZV
6285 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6286 IT_BYTEPOS (*it) + 1,
6287 (double) it->selective)) /* iftc */
6288 {
6289 success_p = next_element_from_ellipsis (it);
6290 it->dpvec_char_len = -1;
6291 }
6292 }
6293 else if (it->c == '\r' && it->selective == -1)
6294 {
6295 /* A value of selective == -1 means that everything from the
6296 CR to the end of the line is invisible, with maybe an
6297 ellipsis displayed for it. */
6298 success_p = next_element_from_ellipsis (it);
6299 it->dpvec_char_len = -1;
6300 }
6301 }
6302 }
6303
6304 /* Value is zero if end of buffer reached. */
6305 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6306 return success_p;
6307 }
6308
6309
6310 /* Run the redisplay end trigger hook for IT. */
6311
6312 static void
6313 run_redisplay_end_trigger_hook (it)
6314 struct it *it;
6315 {
6316 Lisp_Object args[3];
6317
6318 /* IT->glyph_row should be non-null, i.e. we should be actually
6319 displaying something, or otherwise we should not run the hook. */
6320 xassert (it->glyph_row);
6321
6322 /* Set up hook arguments. */
6323 args[0] = Qredisplay_end_trigger_functions;
6324 args[1] = it->window;
6325 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6326 it->redisplay_end_trigger_charpos = 0;
6327
6328 /* Since we are *trying* to run these functions, don't try to run
6329 them again, even if they get an error. */
6330 it->w->redisplay_end_trigger = Qnil;
6331 Frun_hook_with_args (3, args);
6332
6333 /* Notice if it changed the face of the character we are on. */
6334 handle_face_prop (it);
6335 }
6336
6337
6338 /* Deliver a composition display element. The iterator IT is already
6339 filled with composition information (done in
6340 handle_composition_prop). Value is always 1. */
6341
6342 static int
6343 next_element_from_composition (it)
6344 struct it *it;
6345 {
6346 it->what = IT_COMPOSITION;
6347 it->position = (STRINGP (it->string)
6348 ? it->current.string_pos
6349 : it->current.pos);
6350 if (STRINGP (it->string))
6351 it->object = it->string;
6352 else
6353 it->object = it->w->buffer;
6354 return 1;
6355 }
6356
6357
6358 \f
6359 /***********************************************************************
6360 Moving an iterator without producing glyphs
6361 ***********************************************************************/
6362
6363 /* Check if iterator is at a position corresponding to a valid buffer
6364 position after some move_it_ call. */
6365
6366 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6367 ((it)->method == GET_FROM_STRING \
6368 ? IT_STRING_CHARPOS (*it) == 0 \
6369 : 1)
6370
6371
6372 /* Move iterator IT to a specified buffer or X position within one
6373 line on the display without producing glyphs.
6374
6375 OP should be a bit mask including some or all of these bits:
6376 MOVE_TO_X: Stop on reaching x-position TO_X.
6377 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6378 Regardless of OP's value, stop in reaching the end of the display line.
6379
6380 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6381 This means, in particular, that TO_X includes window's horizontal
6382 scroll amount.
6383
6384 The return value has several possible values that
6385 say what condition caused the scan to stop:
6386
6387 MOVE_POS_MATCH_OR_ZV
6388 - when TO_POS or ZV was reached.
6389
6390 MOVE_X_REACHED
6391 -when TO_X was reached before TO_POS or ZV were reached.
6392
6393 MOVE_LINE_CONTINUED
6394 - when we reached the end of the display area and the line must
6395 be continued.
6396
6397 MOVE_LINE_TRUNCATED
6398 - when we reached the end of the display area and the line is
6399 truncated.
6400
6401 MOVE_NEWLINE_OR_CR
6402 - when we stopped at a line end, i.e. a newline or a CR and selective
6403 display is on. */
6404
6405 static enum move_it_result
6406 move_it_in_display_line_to (it, to_charpos, to_x, op)
6407 struct it *it;
6408 int to_charpos, to_x, op;
6409 {
6410 enum move_it_result result = MOVE_UNDEFINED;
6411 struct glyph_row *saved_glyph_row;
6412
6413 /* Don't produce glyphs in produce_glyphs. */
6414 saved_glyph_row = it->glyph_row;
6415 it->glyph_row = NULL;
6416
6417 #define BUFFER_POS_REACHED_P() \
6418 ((op & MOVE_TO_POS) != 0 \
6419 && BUFFERP (it->object) \
6420 && IT_CHARPOS (*it) >= to_charpos \
6421 && (it->method == GET_FROM_BUFFER \
6422 || (it->method == GET_FROM_DISPLAY_VECTOR \
6423 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6424
6425
6426 while (1)
6427 {
6428 int x, i, ascent = 0, descent = 0;
6429
6430 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6431 if ((op & MOVE_TO_POS) != 0
6432 && BUFFERP (it->object)
6433 && it->method == GET_FROM_BUFFER
6434 && IT_CHARPOS (*it) > to_charpos)
6435 {
6436 result = MOVE_POS_MATCH_OR_ZV;
6437 break;
6438 }
6439
6440 /* Stop when ZV reached.
6441 We used to stop here when TO_CHARPOS reached as well, but that is
6442 too soon if this glyph does not fit on this line. So we handle it
6443 explicitly below. */
6444 if (!get_next_display_element (it)
6445 || (it->truncate_lines_p
6446 && BUFFER_POS_REACHED_P ()))
6447 {
6448 result = MOVE_POS_MATCH_OR_ZV;
6449 break;
6450 }
6451
6452 /* The call to produce_glyphs will get the metrics of the
6453 display element IT is loaded with. We record in x the
6454 x-position before this display element in case it does not
6455 fit on the line. */
6456 x = it->current_x;
6457
6458 /* Remember the line height so far in case the next element doesn't
6459 fit on the line. */
6460 if (!it->truncate_lines_p)
6461 {
6462 ascent = it->max_ascent;
6463 descent = it->max_descent;
6464 }
6465
6466 PRODUCE_GLYPHS (it);
6467
6468 if (it->area != TEXT_AREA)
6469 {
6470 set_iterator_to_next (it, 1);
6471 continue;
6472 }
6473
6474 /* The number of glyphs we get back in IT->nglyphs will normally
6475 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6476 character on a terminal frame, or (iii) a line end. For the
6477 second case, IT->nglyphs - 1 padding glyphs will be present
6478 (on X frames, there is only one glyph produced for a
6479 composite character.
6480
6481 The behavior implemented below means, for continuation lines,
6482 that as many spaces of a TAB as fit on the current line are
6483 displayed there. For terminal frames, as many glyphs of a
6484 multi-glyph character are displayed in the current line, too.
6485 This is what the old redisplay code did, and we keep it that
6486 way. Under X, the whole shape of a complex character must
6487 fit on the line or it will be completely displayed in the
6488 next line.
6489
6490 Note that both for tabs and padding glyphs, all glyphs have
6491 the same width. */
6492 if (it->nglyphs)
6493 {
6494 /* More than one glyph or glyph doesn't fit on line. All
6495 glyphs have the same width. */
6496 int single_glyph_width = it->pixel_width / it->nglyphs;
6497 int new_x;
6498 int x_before_this_char = x;
6499 int hpos_before_this_char = it->hpos;
6500
6501 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6502 {
6503 new_x = x + single_glyph_width;
6504
6505 /* We want to leave anything reaching TO_X to the caller. */
6506 if ((op & MOVE_TO_X) && new_x > to_x)
6507 {
6508 if (BUFFER_POS_REACHED_P ())
6509 goto buffer_pos_reached;
6510 it->current_x = x;
6511 result = MOVE_X_REACHED;
6512 break;
6513 }
6514 else if (/* Lines are continued. */
6515 !it->truncate_lines_p
6516 && (/* And glyph doesn't fit on the line. */
6517 new_x > it->last_visible_x
6518 /* Or it fits exactly and we're on a window
6519 system frame. */
6520 || (new_x == it->last_visible_x
6521 && FRAME_WINDOW_P (it->f))))
6522 {
6523 if (/* IT->hpos == 0 means the very first glyph
6524 doesn't fit on the line, e.g. a wide image. */
6525 it->hpos == 0
6526 || (new_x == it->last_visible_x
6527 && FRAME_WINDOW_P (it->f)))
6528 {
6529 ++it->hpos;
6530 it->current_x = new_x;
6531
6532 /* The character's last glyph just barely fits
6533 in this row. */
6534 if (i == it->nglyphs - 1)
6535 {
6536 /* If this is the destination position,
6537 return a position *before* it in this row,
6538 now that we know it fits in this row. */
6539 if (BUFFER_POS_REACHED_P ())
6540 {
6541 it->hpos = hpos_before_this_char;
6542 it->current_x = x_before_this_char;
6543 result = MOVE_POS_MATCH_OR_ZV;
6544 break;
6545 }
6546
6547 set_iterator_to_next (it, 1);
6548 #ifdef HAVE_WINDOW_SYSTEM
6549 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6550 {
6551 if (!get_next_display_element (it))
6552 {
6553 result = MOVE_POS_MATCH_OR_ZV;
6554 break;
6555 }
6556 if (BUFFER_POS_REACHED_P ())
6557 {
6558 if (ITERATOR_AT_END_OF_LINE_P (it))
6559 result = MOVE_POS_MATCH_OR_ZV;
6560 else
6561 result = MOVE_LINE_CONTINUED;
6562 break;
6563 }
6564 if (ITERATOR_AT_END_OF_LINE_P (it))
6565 {
6566 result = MOVE_NEWLINE_OR_CR;
6567 break;
6568 }
6569 }
6570 #endif /* HAVE_WINDOW_SYSTEM */
6571 }
6572 }
6573 else
6574 {
6575 it->current_x = x;
6576 it->max_ascent = ascent;
6577 it->max_descent = descent;
6578 }
6579
6580 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6581 IT_CHARPOS (*it)));
6582 result = MOVE_LINE_CONTINUED;
6583 break;
6584 }
6585 else if (BUFFER_POS_REACHED_P ())
6586 goto buffer_pos_reached;
6587 else if (new_x > it->first_visible_x)
6588 {
6589 /* Glyph is visible. Increment number of glyphs that
6590 would be displayed. */
6591 ++it->hpos;
6592 }
6593 else
6594 {
6595 /* Glyph is completely off the left margin of the display
6596 area. Nothing to do. */
6597 }
6598 }
6599
6600 if (result != MOVE_UNDEFINED)
6601 break;
6602 }
6603 else if (BUFFER_POS_REACHED_P ())
6604 {
6605 buffer_pos_reached:
6606 it->current_x = x;
6607 it->max_ascent = ascent;
6608 it->max_descent = descent;
6609 result = MOVE_POS_MATCH_OR_ZV;
6610 break;
6611 }
6612 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6613 {
6614 /* Stop when TO_X specified and reached. This check is
6615 necessary here because of lines consisting of a line end,
6616 only. The line end will not produce any glyphs and we
6617 would never get MOVE_X_REACHED. */
6618 xassert (it->nglyphs == 0);
6619 result = MOVE_X_REACHED;
6620 break;
6621 }
6622
6623 /* Is this a line end? If yes, we're done. */
6624 if (ITERATOR_AT_END_OF_LINE_P (it))
6625 {
6626 result = MOVE_NEWLINE_OR_CR;
6627 break;
6628 }
6629
6630 /* The current display element has been consumed. Advance
6631 to the next. */
6632 set_iterator_to_next (it, 1);
6633
6634 /* Stop if lines are truncated and IT's current x-position is
6635 past the right edge of the window now. */
6636 if (it->truncate_lines_p
6637 && it->current_x >= it->last_visible_x)
6638 {
6639 #ifdef HAVE_WINDOW_SYSTEM
6640 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6641 {
6642 if (!get_next_display_element (it)
6643 || BUFFER_POS_REACHED_P ())
6644 {
6645 result = MOVE_POS_MATCH_OR_ZV;
6646 break;
6647 }
6648 if (ITERATOR_AT_END_OF_LINE_P (it))
6649 {
6650 result = MOVE_NEWLINE_OR_CR;
6651 break;
6652 }
6653 }
6654 #endif /* HAVE_WINDOW_SYSTEM */
6655 result = MOVE_LINE_TRUNCATED;
6656 break;
6657 }
6658 }
6659
6660 #undef BUFFER_POS_REACHED_P
6661
6662 /* Restore the iterator settings altered at the beginning of this
6663 function. */
6664 it->glyph_row = saved_glyph_row;
6665 return result;
6666 }
6667
6668
6669 /* Move IT forward until it satisfies one or more of the criteria in
6670 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6671
6672 OP is a bit-mask that specifies where to stop, and in particular,
6673 which of those four position arguments makes a difference. See the
6674 description of enum move_operation_enum.
6675
6676 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6677 screen line, this function will set IT to the next position >
6678 TO_CHARPOS. */
6679
6680 void
6681 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6682 struct it *it;
6683 int to_charpos, to_x, to_y, to_vpos;
6684 int op;
6685 {
6686 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6687 int line_height;
6688 int reached = 0;
6689
6690 for (;;)
6691 {
6692 if (op & MOVE_TO_VPOS)
6693 {
6694 /* If no TO_CHARPOS and no TO_X specified, stop at the
6695 start of the line TO_VPOS. */
6696 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6697 {
6698 if (it->vpos == to_vpos)
6699 {
6700 reached = 1;
6701 break;
6702 }
6703 else
6704 skip = move_it_in_display_line_to (it, -1, -1, 0);
6705 }
6706 else
6707 {
6708 /* TO_VPOS >= 0 means stop at TO_X in the line at
6709 TO_VPOS, or at TO_POS, whichever comes first. */
6710 if (it->vpos == to_vpos)
6711 {
6712 reached = 2;
6713 break;
6714 }
6715
6716 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6717
6718 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6719 {
6720 reached = 3;
6721 break;
6722 }
6723 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6724 {
6725 /* We have reached TO_X but not in the line we want. */
6726 skip = move_it_in_display_line_to (it, to_charpos,
6727 -1, MOVE_TO_POS);
6728 if (skip == MOVE_POS_MATCH_OR_ZV)
6729 {
6730 reached = 4;
6731 break;
6732 }
6733 }
6734 }
6735 }
6736 else if (op & MOVE_TO_Y)
6737 {
6738 struct it it_backup;
6739
6740 /* TO_Y specified means stop at TO_X in the line containing
6741 TO_Y---or at TO_CHARPOS if this is reached first. The
6742 problem is that we can't really tell whether the line
6743 contains TO_Y before we have completely scanned it, and
6744 this may skip past TO_X. What we do is to first scan to
6745 TO_X.
6746
6747 If TO_X is not specified, use a TO_X of zero. The reason
6748 is to make the outcome of this function more predictable.
6749 If we didn't use TO_X == 0, we would stop at the end of
6750 the line which is probably not what a caller would expect
6751 to happen. */
6752 skip = move_it_in_display_line_to (it, to_charpos,
6753 ((op & MOVE_TO_X)
6754 ? to_x : 0),
6755 (MOVE_TO_X
6756 | (op & MOVE_TO_POS)));
6757
6758 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6759 if (skip == MOVE_POS_MATCH_OR_ZV)
6760 {
6761 reached = 5;
6762 break;
6763 }
6764
6765 /* If TO_X was reached, we would like to know whether TO_Y
6766 is in the line. This can only be said if we know the
6767 total line height which requires us to scan the rest of
6768 the line. */
6769 if (skip == MOVE_X_REACHED)
6770 {
6771 it_backup = *it;
6772 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6773 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6774 op & MOVE_TO_POS);
6775 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6776 }
6777
6778 /* Now, decide whether TO_Y is in this line. */
6779 line_height = it->max_ascent + it->max_descent;
6780 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6781
6782 if (to_y >= it->current_y
6783 && to_y < it->current_y + line_height)
6784 {
6785 if (skip == MOVE_X_REACHED)
6786 /* If TO_Y is in this line and TO_X was reached above,
6787 we scanned too far. We have to restore IT's settings
6788 to the ones before skipping. */
6789 *it = it_backup;
6790 reached = 6;
6791 }
6792 else if (skip == MOVE_X_REACHED)
6793 {
6794 skip = skip2;
6795 if (skip == MOVE_POS_MATCH_OR_ZV)
6796 reached = 7;
6797 }
6798
6799 if (reached)
6800 break;
6801 }
6802 else if (BUFFERP (it->object)
6803 && it->method == GET_FROM_BUFFER
6804 && IT_CHARPOS (*it) >= to_charpos)
6805 skip = MOVE_POS_MATCH_OR_ZV;
6806 else
6807 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6808
6809 switch (skip)
6810 {
6811 case MOVE_POS_MATCH_OR_ZV:
6812 reached = 8;
6813 goto out;
6814
6815 case MOVE_NEWLINE_OR_CR:
6816 set_iterator_to_next (it, 1);
6817 it->continuation_lines_width = 0;
6818 break;
6819
6820 case MOVE_LINE_TRUNCATED:
6821 it->continuation_lines_width = 0;
6822 reseat_at_next_visible_line_start (it, 0);
6823 if ((op & MOVE_TO_POS) != 0
6824 && IT_CHARPOS (*it) > to_charpos)
6825 {
6826 reached = 9;
6827 goto out;
6828 }
6829 break;
6830
6831 case MOVE_LINE_CONTINUED:
6832 it->continuation_lines_width += it->current_x;
6833 break;
6834
6835 default:
6836 abort ();
6837 }
6838
6839 /* Reset/increment for the next run. */
6840 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6841 it->current_x = it->hpos = 0;
6842 it->current_y += it->max_ascent + it->max_descent;
6843 ++it->vpos;
6844 last_height = it->max_ascent + it->max_descent;
6845 last_max_ascent = it->max_ascent;
6846 it->max_ascent = it->max_descent = 0;
6847 }
6848
6849 out:
6850
6851 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6852 }
6853
6854
6855 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6856
6857 If DY > 0, move IT backward at least that many pixels. DY = 0
6858 means move IT backward to the preceding line start or BEGV. This
6859 function may move over more than DY pixels if IT->current_y - DY
6860 ends up in the middle of a line; in this case IT->current_y will be
6861 set to the top of the line moved to. */
6862
6863 void
6864 move_it_vertically_backward (it, dy)
6865 struct it *it;
6866 int dy;
6867 {
6868 int nlines, h;
6869 struct it it2, it3;
6870 int start_pos;
6871
6872 move_further_back:
6873 xassert (dy >= 0);
6874
6875 start_pos = IT_CHARPOS (*it);
6876
6877 /* Estimate how many newlines we must move back. */
6878 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6879
6880 /* Set the iterator's position that many lines back. */
6881 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6882 back_to_previous_visible_line_start (it);
6883
6884 /* Reseat the iterator here. When moving backward, we don't want
6885 reseat to skip forward over invisible text, set up the iterator
6886 to deliver from overlay strings at the new position etc. So,
6887 use reseat_1 here. */
6888 reseat_1 (it, it->current.pos, 1);
6889
6890 /* We are now surely at a line start. */
6891 it->current_x = it->hpos = 0;
6892 it->continuation_lines_width = 0;
6893
6894 /* Move forward and see what y-distance we moved. First move to the
6895 start of the next line so that we get its height. We need this
6896 height to be able to tell whether we reached the specified
6897 y-distance. */
6898 it2 = *it;
6899 it2.max_ascent = it2.max_descent = 0;
6900 do
6901 {
6902 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6903 MOVE_TO_POS | MOVE_TO_VPOS);
6904 }
6905 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6906 xassert (IT_CHARPOS (*it) >= BEGV);
6907 it3 = it2;
6908
6909 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6910 xassert (IT_CHARPOS (*it) >= BEGV);
6911 /* H is the actual vertical distance from the position in *IT
6912 and the starting position. */
6913 h = it2.current_y - it->current_y;
6914 /* NLINES is the distance in number of lines. */
6915 nlines = it2.vpos - it->vpos;
6916
6917 /* Correct IT's y and vpos position
6918 so that they are relative to the starting point. */
6919 it->vpos -= nlines;
6920 it->current_y -= h;
6921
6922 if (dy == 0)
6923 {
6924 /* DY == 0 means move to the start of the screen line. The
6925 value of nlines is > 0 if continuation lines were involved. */
6926 if (nlines > 0)
6927 move_it_by_lines (it, nlines, 1);
6928 #if 0
6929 /* I think this assert is bogus if buffer contains
6930 invisible text or images. KFS. */
6931 xassert (IT_CHARPOS (*it) <= start_pos);
6932 #endif
6933 }
6934 else
6935 {
6936 /* The y-position we try to reach, relative to *IT.
6937 Note that H has been subtracted in front of the if-statement. */
6938 int target_y = it->current_y + h - dy;
6939 int y0 = it3.current_y;
6940 int y1 = line_bottom_y (&it3);
6941 int line_height = y1 - y0;
6942
6943 /* If we did not reach target_y, try to move further backward if
6944 we can. If we moved too far backward, try to move forward. */
6945 if (target_y < it->current_y
6946 /* This is heuristic. In a window that's 3 lines high, with
6947 a line height of 13 pixels each, recentering with point
6948 on the bottom line will try to move -39/2 = 19 pixels
6949 backward. Try to avoid moving into the first line. */
6950 && (it->current_y - target_y
6951 > min (window_box_height (it->w), line_height * 2 / 3))
6952 && IT_CHARPOS (*it) > BEGV)
6953 {
6954 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6955 target_y - it->current_y));
6956 dy = it->current_y - target_y;
6957 goto move_further_back;
6958 }
6959 else if (target_y >= it->current_y + line_height
6960 && IT_CHARPOS (*it) < ZV)
6961 {
6962 /* Should move forward by at least one line, maybe more.
6963
6964 Note: Calling move_it_by_lines can be expensive on
6965 terminal frames, where compute_motion is used (via
6966 vmotion) to do the job, when there are very long lines
6967 and truncate-lines is nil. That's the reason for
6968 treating terminal frames specially here. */
6969
6970 if (!FRAME_WINDOW_P (it->f))
6971 move_it_vertically (it, target_y - (it->current_y + line_height));
6972 else
6973 {
6974 do
6975 {
6976 move_it_by_lines (it, 1, 1);
6977 }
6978 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6979 }
6980
6981 #if 0
6982 /* I think this assert is bogus if buffer contains
6983 invisible text or images. KFS. */
6984 xassert (IT_CHARPOS (*it) >= BEGV);
6985 #endif
6986 }
6987 }
6988 }
6989
6990
6991 /* Move IT by a specified amount of pixel lines DY. DY negative means
6992 move backwards. DY = 0 means move to start of screen line. At the
6993 end, IT will be on the start of a screen line. */
6994
6995 void
6996 move_it_vertically (it, dy)
6997 struct it *it;
6998 int dy;
6999 {
7000 if (dy <= 0)
7001 move_it_vertically_backward (it, -dy);
7002 else
7003 {
7004 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7005 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7006 MOVE_TO_POS | MOVE_TO_Y);
7007 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7008
7009 /* If buffer ends in ZV without a newline, move to the start of
7010 the line to satisfy the post-condition. */
7011 if (IT_CHARPOS (*it) == ZV
7012 && ZV > BEGV
7013 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7014 move_it_by_lines (it, 0, 0);
7015 }
7016 }
7017
7018
7019 /* Move iterator IT past the end of the text line it is in. */
7020
7021 void
7022 move_it_past_eol (it)
7023 struct it *it;
7024 {
7025 enum move_it_result rc;
7026
7027 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7028 if (rc == MOVE_NEWLINE_OR_CR)
7029 set_iterator_to_next (it, 0);
7030 }
7031
7032
7033 #if 0 /* Currently not used. */
7034
7035 /* Return non-zero if some text between buffer positions START_CHARPOS
7036 and END_CHARPOS is invisible. IT->window is the window for text
7037 property lookup. */
7038
7039 static int
7040 invisible_text_between_p (it, start_charpos, end_charpos)
7041 struct it *it;
7042 int start_charpos, end_charpos;
7043 {
7044 Lisp_Object prop, limit;
7045 int invisible_found_p;
7046
7047 xassert (it != NULL && start_charpos <= end_charpos);
7048
7049 /* Is text at START invisible? */
7050 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7051 it->window);
7052 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7053 invisible_found_p = 1;
7054 else
7055 {
7056 limit = Fnext_single_char_property_change (make_number (start_charpos),
7057 Qinvisible, Qnil,
7058 make_number (end_charpos));
7059 invisible_found_p = XFASTINT (limit) < end_charpos;
7060 }
7061
7062 return invisible_found_p;
7063 }
7064
7065 #endif /* 0 */
7066
7067
7068 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7069 negative means move up. DVPOS == 0 means move to the start of the
7070 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7071 NEED_Y_P is zero, IT->current_y will be left unchanged.
7072
7073 Further optimization ideas: If we would know that IT->f doesn't use
7074 a face with proportional font, we could be faster for
7075 truncate-lines nil. */
7076
7077 void
7078 move_it_by_lines (it, dvpos, need_y_p)
7079 struct it *it;
7080 int dvpos, need_y_p;
7081 {
7082 struct position pos;
7083
7084 if (!FRAME_WINDOW_P (it->f))
7085 {
7086 struct text_pos textpos;
7087
7088 /* We can use vmotion on frames without proportional fonts. */
7089 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7090 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7091 reseat (it, textpos, 1);
7092 it->vpos += pos.vpos;
7093 it->current_y += pos.vpos;
7094 }
7095 else if (dvpos == 0)
7096 {
7097 /* DVPOS == 0 means move to the start of the screen line. */
7098 move_it_vertically_backward (it, 0);
7099 xassert (it->current_x == 0 && it->hpos == 0);
7100 /* Let next call to line_bottom_y calculate real line height */
7101 last_height = 0;
7102 }
7103 else if (dvpos > 0)
7104 {
7105 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7106 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7107 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7108 }
7109 else
7110 {
7111 struct it it2;
7112 int start_charpos, i;
7113
7114 /* Start at the beginning of the screen line containing IT's
7115 position. This may actually move vertically backwards,
7116 in case of overlays, so adjust dvpos accordingly. */
7117 dvpos += it->vpos;
7118 move_it_vertically_backward (it, 0);
7119 dvpos -= it->vpos;
7120
7121 /* Go back -DVPOS visible lines and reseat the iterator there. */
7122 start_charpos = IT_CHARPOS (*it);
7123 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7124 back_to_previous_visible_line_start (it);
7125 reseat (it, it->current.pos, 1);
7126
7127 /* Move further back if we end up in a string or an image. */
7128 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7129 {
7130 /* First try to move to start of display line. */
7131 dvpos += it->vpos;
7132 move_it_vertically_backward (it, 0);
7133 dvpos -= it->vpos;
7134 if (IT_POS_VALID_AFTER_MOVE_P (it))
7135 break;
7136 /* If start of line is still in string or image,
7137 move further back. */
7138 back_to_previous_visible_line_start (it);
7139 reseat (it, it->current.pos, 1);
7140 dvpos--;
7141 }
7142
7143 it->current_x = it->hpos = 0;
7144
7145 /* Above call may have moved too far if continuation lines
7146 are involved. Scan forward and see if it did. */
7147 it2 = *it;
7148 it2.vpos = it2.current_y = 0;
7149 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7150 it->vpos -= it2.vpos;
7151 it->current_y -= it2.current_y;
7152 it->current_x = it->hpos = 0;
7153
7154 /* If we moved too far back, move IT some lines forward. */
7155 if (it2.vpos > -dvpos)
7156 {
7157 int delta = it2.vpos + dvpos;
7158 it2 = *it;
7159 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7160 /* Move back again if we got too far ahead. */
7161 if (IT_CHARPOS (*it) >= start_charpos)
7162 *it = it2;
7163 }
7164 }
7165 }
7166
7167 /* Return 1 if IT points into the middle of a display vector. */
7168
7169 int
7170 in_display_vector_p (it)
7171 struct it *it;
7172 {
7173 return (it->method == GET_FROM_DISPLAY_VECTOR
7174 && it->current.dpvec_index > 0
7175 && it->dpvec + it->current.dpvec_index != it->dpend);
7176 }
7177
7178 \f
7179 /***********************************************************************
7180 Messages
7181 ***********************************************************************/
7182
7183
7184 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7185 to *Messages*. */
7186
7187 void
7188 add_to_log (format, arg1, arg2)
7189 char *format;
7190 Lisp_Object arg1, arg2;
7191 {
7192 Lisp_Object args[3];
7193 Lisp_Object msg, fmt;
7194 char *buffer;
7195 int len;
7196 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7197 USE_SAFE_ALLOCA;
7198
7199 /* Do nothing if called asynchronously. Inserting text into
7200 a buffer may call after-change-functions and alike and
7201 that would means running Lisp asynchronously. */
7202 if (handling_signal)
7203 return;
7204
7205 fmt = msg = Qnil;
7206 GCPRO4 (fmt, msg, arg1, arg2);
7207
7208 args[0] = fmt = build_string (format);
7209 args[1] = arg1;
7210 args[2] = arg2;
7211 msg = Fformat (3, args);
7212
7213 len = SBYTES (msg) + 1;
7214 SAFE_ALLOCA (buffer, char *, len);
7215 bcopy (SDATA (msg), buffer, len);
7216
7217 message_dolog (buffer, len - 1, 1, 0);
7218 SAFE_FREE ();
7219
7220 UNGCPRO;
7221 }
7222
7223
7224 /* Output a newline in the *Messages* buffer if "needs" one. */
7225
7226 void
7227 message_log_maybe_newline ()
7228 {
7229 if (message_log_need_newline)
7230 message_dolog ("", 0, 1, 0);
7231 }
7232
7233
7234 /* Add a string M of length NBYTES to the message log, optionally
7235 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7236 nonzero, means interpret the contents of M as multibyte. This
7237 function calls low-level routines in order to bypass text property
7238 hooks, etc. which might not be safe to run.
7239
7240 This may GC (insert may run before/after change hooks),
7241 so the buffer M must NOT point to a Lisp string. */
7242
7243 void
7244 message_dolog (m, nbytes, nlflag, multibyte)
7245 const char *m;
7246 int nbytes, nlflag, multibyte;
7247 {
7248 if (!NILP (Vmemory_full))
7249 return;
7250
7251 if (!NILP (Vmessage_log_max))
7252 {
7253 struct buffer *oldbuf;
7254 Lisp_Object oldpoint, oldbegv, oldzv;
7255 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7256 int point_at_end = 0;
7257 int zv_at_end = 0;
7258 Lisp_Object old_deactivate_mark, tem;
7259 struct gcpro gcpro1;
7260
7261 old_deactivate_mark = Vdeactivate_mark;
7262 oldbuf = current_buffer;
7263 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7264 current_buffer->undo_list = Qt;
7265
7266 oldpoint = message_dolog_marker1;
7267 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7268 oldbegv = message_dolog_marker2;
7269 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7270 oldzv = message_dolog_marker3;
7271 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7272 GCPRO1 (old_deactivate_mark);
7273
7274 if (PT == Z)
7275 point_at_end = 1;
7276 if (ZV == Z)
7277 zv_at_end = 1;
7278
7279 BEGV = BEG;
7280 BEGV_BYTE = BEG_BYTE;
7281 ZV = Z;
7282 ZV_BYTE = Z_BYTE;
7283 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7284
7285 /* Insert the string--maybe converting multibyte to single byte
7286 or vice versa, so that all the text fits the buffer. */
7287 if (multibyte
7288 && NILP (current_buffer->enable_multibyte_characters))
7289 {
7290 int i, c, char_bytes;
7291 unsigned char work[1];
7292
7293 /* Convert a multibyte string to single-byte
7294 for the *Message* buffer. */
7295 for (i = 0; i < nbytes; i += char_bytes)
7296 {
7297 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7298 work[0] = (SINGLE_BYTE_CHAR_P (c)
7299 ? c
7300 : multibyte_char_to_unibyte (c, Qnil));
7301 insert_1_both (work, 1, 1, 1, 0, 0);
7302 }
7303 }
7304 else if (! multibyte
7305 && ! NILP (current_buffer->enable_multibyte_characters))
7306 {
7307 int i, c, char_bytes;
7308 unsigned char *msg = (unsigned char *) m;
7309 unsigned char str[MAX_MULTIBYTE_LENGTH];
7310 /* Convert a single-byte string to multibyte
7311 for the *Message* buffer. */
7312 for (i = 0; i < nbytes; i++)
7313 {
7314 c = unibyte_char_to_multibyte (msg[i]);
7315 char_bytes = CHAR_STRING (c, str);
7316 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7317 }
7318 }
7319 else if (nbytes)
7320 insert_1 (m, nbytes, 1, 0, 0);
7321
7322 if (nlflag)
7323 {
7324 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7325 insert_1 ("\n", 1, 1, 0, 0);
7326
7327 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7328 this_bol = PT;
7329 this_bol_byte = PT_BYTE;
7330
7331 /* See if this line duplicates the previous one.
7332 If so, combine duplicates. */
7333 if (this_bol > BEG)
7334 {
7335 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7336 prev_bol = PT;
7337 prev_bol_byte = PT_BYTE;
7338
7339 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7340 this_bol, this_bol_byte);
7341 if (dup)
7342 {
7343 del_range_both (prev_bol, prev_bol_byte,
7344 this_bol, this_bol_byte, 0);
7345 if (dup > 1)
7346 {
7347 char dupstr[40];
7348 int duplen;
7349
7350 /* If you change this format, don't forget to also
7351 change message_log_check_duplicate. */
7352 sprintf (dupstr, " [%d times]", dup);
7353 duplen = strlen (dupstr);
7354 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7355 insert_1 (dupstr, duplen, 1, 0, 1);
7356 }
7357 }
7358 }
7359
7360 /* If we have more than the desired maximum number of lines
7361 in the *Messages* buffer now, delete the oldest ones.
7362 This is safe because we don't have undo in this buffer. */
7363
7364 if (NATNUMP (Vmessage_log_max))
7365 {
7366 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7367 -XFASTINT (Vmessage_log_max) - 1, 0);
7368 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7369 }
7370 }
7371 BEGV = XMARKER (oldbegv)->charpos;
7372 BEGV_BYTE = marker_byte_position (oldbegv);
7373
7374 if (zv_at_end)
7375 {
7376 ZV = Z;
7377 ZV_BYTE = Z_BYTE;
7378 }
7379 else
7380 {
7381 ZV = XMARKER (oldzv)->charpos;
7382 ZV_BYTE = marker_byte_position (oldzv);
7383 }
7384
7385 if (point_at_end)
7386 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7387 else
7388 /* We can't do Fgoto_char (oldpoint) because it will run some
7389 Lisp code. */
7390 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7391 XMARKER (oldpoint)->bytepos);
7392
7393 UNGCPRO;
7394 unchain_marker (XMARKER (oldpoint));
7395 unchain_marker (XMARKER (oldbegv));
7396 unchain_marker (XMARKER (oldzv));
7397
7398 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7399 set_buffer_internal (oldbuf);
7400 if (NILP (tem))
7401 windows_or_buffers_changed = old_windows_or_buffers_changed;
7402 message_log_need_newline = !nlflag;
7403 Vdeactivate_mark = old_deactivate_mark;
7404 }
7405 }
7406
7407
7408 /* We are at the end of the buffer after just having inserted a newline.
7409 (Note: We depend on the fact we won't be crossing the gap.)
7410 Check to see if the most recent message looks a lot like the previous one.
7411 Return 0 if different, 1 if the new one should just replace it, or a
7412 value N > 1 if we should also append " [N times]". */
7413
7414 static int
7415 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7416 int prev_bol, this_bol;
7417 int prev_bol_byte, this_bol_byte;
7418 {
7419 int i;
7420 int len = Z_BYTE - 1 - this_bol_byte;
7421 int seen_dots = 0;
7422 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7423 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7424
7425 for (i = 0; i < len; i++)
7426 {
7427 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7428 seen_dots = 1;
7429 if (p1[i] != p2[i])
7430 return seen_dots;
7431 }
7432 p1 += len;
7433 if (*p1 == '\n')
7434 return 2;
7435 if (*p1++ == ' ' && *p1++ == '[')
7436 {
7437 int n = 0;
7438 while (*p1 >= '0' && *p1 <= '9')
7439 n = n * 10 + *p1++ - '0';
7440 if (strncmp (p1, " times]\n", 8) == 0)
7441 return n+1;
7442 }
7443 return 0;
7444 }
7445 \f
7446
7447 /* Display an echo area message M with a specified length of NBYTES
7448 bytes. The string may include null characters. If M is 0, clear
7449 out any existing message, and let the mini-buffer text show
7450 through.
7451
7452 This may GC, so the buffer M must NOT point to a Lisp string. */
7453
7454 void
7455 message2 (m, nbytes, multibyte)
7456 const char *m;
7457 int nbytes;
7458 int multibyte;
7459 {
7460 /* First flush out any partial line written with print. */
7461 message_log_maybe_newline ();
7462 if (m)
7463 message_dolog (m, nbytes, 1, multibyte);
7464 message2_nolog (m, nbytes, multibyte);
7465 }
7466
7467
7468 /* The non-logging counterpart of message2. */
7469
7470 void
7471 message2_nolog (m, nbytes, multibyte)
7472 const char *m;
7473 int nbytes, multibyte;
7474 {
7475 struct frame *sf = SELECTED_FRAME ();
7476 message_enable_multibyte = multibyte;
7477
7478 if (noninteractive)
7479 {
7480 if (noninteractive_need_newline)
7481 putc ('\n', stderr);
7482 noninteractive_need_newline = 0;
7483 if (m)
7484 fwrite (m, nbytes, 1, stderr);
7485 if (cursor_in_echo_area == 0)
7486 fprintf (stderr, "\n");
7487 fflush (stderr);
7488 }
7489 /* A null message buffer means that the frame hasn't really been
7490 initialized yet. Error messages get reported properly by
7491 cmd_error, so this must be just an informative message; toss it. */
7492 else if (INTERACTIVE
7493 && sf->glyphs_initialized_p
7494 && FRAME_MESSAGE_BUF (sf))
7495 {
7496 Lisp_Object mini_window;
7497 struct frame *f;
7498
7499 /* Get the frame containing the mini-buffer
7500 that the selected frame is using. */
7501 mini_window = FRAME_MINIBUF_WINDOW (sf);
7502 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7503
7504 FRAME_SAMPLE_VISIBILITY (f);
7505 if (FRAME_VISIBLE_P (sf)
7506 && ! FRAME_VISIBLE_P (f))
7507 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7508
7509 if (m)
7510 {
7511 set_message (m, Qnil, nbytes, multibyte);
7512 if (minibuffer_auto_raise)
7513 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7514 }
7515 else
7516 clear_message (1, 1);
7517
7518 do_pending_window_change (0);
7519 echo_area_display (1);
7520 do_pending_window_change (0);
7521 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7522 (*frame_up_to_date_hook) (f);
7523 }
7524 }
7525
7526
7527 /* Display an echo area message M with a specified length of NBYTES
7528 bytes. The string may include null characters. If M is not a
7529 string, clear out any existing message, and let the mini-buffer
7530 text show through.
7531
7532 This function cancels echoing. */
7533
7534 void
7535 message3 (m, nbytes, multibyte)
7536 Lisp_Object m;
7537 int nbytes;
7538 int multibyte;
7539 {
7540 struct gcpro gcpro1;
7541
7542 GCPRO1 (m);
7543 clear_message (1,1);
7544 cancel_echoing ();
7545
7546 /* First flush out any partial line written with print. */
7547 message_log_maybe_newline ();
7548 if (STRINGP (m))
7549 {
7550 char *buffer;
7551 USE_SAFE_ALLOCA;
7552
7553 SAFE_ALLOCA (buffer, char *, nbytes);
7554 bcopy (SDATA (m), buffer, nbytes);
7555 message_dolog (buffer, nbytes, 1, multibyte);
7556 SAFE_FREE ();
7557 }
7558 message3_nolog (m, nbytes, multibyte);
7559
7560 UNGCPRO;
7561 }
7562
7563
7564 /* The non-logging version of message3.
7565 This does not cancel echoing, because it is used for echoing.
7566 Perhaps we need to make a separate function for echoing
7567 and make this cancel echoing. */
7568
7569 void
7570 message3_nolog (m, nbytes, multibyte)
7571 Lisp_Object m;
7572 int nbytes, multibyte;
7573 {
7574 struct frame *sf = SELECTED_FRAME ();
7575 message_enable_multibyte = multibyte;
7576
7577 if (noninteractive)
7578 {
7579 if (noninteractive_need_newline)
7580 putc ('\n', stderr);
7581 noninteractive_need_newline = 0;
7582 if (STRINGP (m))
7583 fwrite (SDATA (m), nbytes, 1, stderr);
7584 if (cursor_in_echo_area == 0)
7585 fprintf (stderr, "\n");
7586 fflush (stderr);
7587 }
7588 /* A null message buffer means that the frame hasn't really been
7589 initialized yet. Error messages get reported properly by
7590 cmd_error, so this must be just an informative message; toss it. */
7591 else if (INTERACTIVE
7592 && sf->glyphs_initialized_p
7593 && FRAME_MESSAGE_BUF (sf))
7594 {
7595 Lisp_Object mini_window;
7596 Lisp_Object frame;
7597 struct frame *f;
7598
7599 /* Get the frame containing the mini-buffer
7600 that the selected frame is using. */
7601 mini_window = FRAME_MINIBUF_WINDOW (sf);
7602 frame = XWINDOW (mini_window)->frame;
7603 f = XFRAME (frame);
7604
7605 FRAME_SAMPLE_VISIBILITY (f);
7606 if (FRAME_VISIBLE_P (sf)
7607 && !FRAME_VISIBLE_P (f))
7608 Fmake_frame_visible (frame);
7609
7610 if (STRINGP (m) && SCHARS (m) > 0)
7611 {
7612 set_message (NULL, m, nbytes, multibyte);
7613 if (minibuffer_auto_raise)
7614 Fraise_frame (frame);
7615 /* Assume we are not echoing.
7616 (If we are, echo_now will override this.) */
7617 echo_message_buffer = Qnil;
7618 }
7619 else
7620 clear_message (1, 1);
7621
7622 do_pending_window_change (0);
7623 echo_area_display (1);
7624 do_pending_window_change (0);
7625 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7626 (*frame_up_to_date_hook) (f);
7627 }
7628 }
7629
7630
7631 /* Display a null-terminated echo area message M. If M is 0, clear
7632 out any existing message, and let the mini-buffer text show through.
7633
7634 The buffer M must continue to exist until after the echo area gets
7635 cleared or some other message gets displayed there. Do not pass
7636 text that is stored in a Lisp string. Do not pass text in a buffer
7637 that was alloca'd. */
7638
7639 void
7640 message1 (m)
7641 char *m;
7642 {
7643 message2 (m, (m ? strlen (m) : 0), 0);
7644 }
7645
7646
7647 /* The non-logging counterpart of message1. */
7648
7649 void
7650 message1_nolog (m)
7651 char *m;
7652 {
7653 message2_nolog (m, (m ? strlen (m) : 0), 0);
7654 }
7655
7656 /* Display a message M which contains a single %s
7657 which gets replaced with STRING. */
7658
7659 void
7660 message_with_string (m, string, log)
7661 char *m;
7662 Lisp_Object string;
7663 int log;
7664 {
7665 CHECK_STRING (string);
7666
7667 if (noninteractive)
7668 {
7669 if (m)
7670 {
7671 if (noninteractive_need_newline)
7672 putc ('\n', stderr);
7673 noninteractive_need_newline = 0;
7674 fprintf (stderr, m, SDATA (string));
7675 if (cursor_in_echo_area == 0)
7676 fprintf (stderr, "\n");
7677 fflush (stderr);
7678 }
7679 }
7680 else if (INTERACTIVE)
7681 {
7682 /* The frame whose minibuffer we're going to display the message on.
7683 It may be larger than the selected frame, so we need
7684 to use its buffer, not the selected frame's buffer. */
7685 Lisp_Object mini_window;
7686 struct frame *f, *sf = SELECTED_FRAME ();
7687
7688 /* Get the frame containing the minibuffer
7689 that the selected frame is using. */
7690 mini_window = FRAME_MINIBUF_WINDOW (sf);
7691 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7692
7693 /* A null message buffer means that the frame hasn't really been
7694 initialized yet. Error messages get reported properly by
7695 cmd_error, so this must be just an informative message; toss it. */
7696 if (FRAME_MESSAGE_BUF (f))
7697 {
7698 Lisp_Object args[2], message;
7699 struct gcpro gcpro1, gcpro2;
7700
7701 args[0] = build_string (m);
7702 args[1] = message = string;
7703 GCPRO2 (args[0], message);
7704 gcpro1.nvars = 2;
7705
7706 message = Fformat (2, args);
7707
7708 if (log)
7709 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7710 else
7711 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7712
7713 UNGCPRO;
7714
7715 /* Print should start at the beginning of the message
7716 buffer next time. */
7717 message_buf_print = 0;
7718 }
7719 }
7720 }
7721
7722
7723 /* Dump an informative message to the minibuf. If M is 0, clear out
7724 any existing message, and let the mini-buffer text show through. */
7725
7726 /* VARARGS 1 */
7727 void
7728 message (m, a1, a2, a3)
7729 char *m;
7730 EMACS_INT a1, a2, a3;
7731 {
7732 if (noninteractive)
7733 {
7734 if (m)
7735 {
7736 if (noninteractive_need_newline)
7737 putc ('\n', stderr);
7738 noninteractive_need_newline = 0;
7739 fprintf (stderr, m, a1, a2, a3);
7740 if (cursor_in_echo_area == 0)
7741 fprintf (stderr, "\n");
7742 fflush (stderr);
7743 }
7744 }
7745 else if (INTERACTIVE)
7746 {
7747 /* The frame whose mini-buffer we're going to display the message
7748 on. It may be larger than the selected frame, so we need to
7749 use its buffer, not the selected frame's buffer. */
7750 Lisp_Object mini_window;
7751 struct frame *f, *sf = SELECTED_FRAME ();
7752
7753 /* Get the frame containing the mini-buffer
7754 that the selected frame is using. */
7755 mini_window = FRAME_MINIBUF_WINDOW (sf);
7756 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7757
7758 /* A null message buffer means that the frame hasn't really been
7759 initialized yet. Error messages get reported properly by
7760 cmd_error, so this must be just an informative message; toss
7761 it. */
7762 if (FRAME_MESSAGE_BUF (f))
7763 {
7764 if (m)
7765 {
7766 int len;
7767 #ifdef NO_ARG_ARRAY
7768 char *a[3];
7769 a[0] = (char *) a1;
7770 a[1] = (char *) a2;
7771 a[2] = (char *) a3;
7772
7773 len = doprnt (FRAME_MESSAGE_BUF (f),
7774 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7775 #else
7776 len = doprnt (FRAME_MESSAGE_BUF (f),
7777 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7778 (char **) &a1);
7779 #endif /* NO_ARG_ARRAY */
7780
7781 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7782 }
7783 else
7784 message1 (0);
7785
7786 /* Print should start at the beginning of the message
7787 buffer next time. */
7788 message_buf_print = 0;
7789 }
7790 }
7791 }
7792
7793
7794 /* The non-logging version of message. */
7795
7796 void
7797 message_nolog (m, a1, a2, a3)
7798 char *m;
7799 EMACS_INT a1, a2, a3;
7800 {
7801 Lisp_Object old_log_max;
7802 old_log_max = Vmessage_log_max;
7803 Vmessage_log_max = Qnil;
7804 message (m, a1, a2, a3);
7805 Vmessage_log_max = old_log_max;
7806 }
7807
7808
7809 /* Display the current message in the current mini-buffer. This is
7810 only called from error handlers in process.c, and is not time
7811 critical. */
7812
7813 void
7814 update_echo_area ()
7815 {
7816 if (!NILP (echo_area_buffer[0]))
7817 {
7818 Lisp_Object string;
7819 string = Fcurrent_message ();
7820 message3 (string, SBYTES (string),
7821 !NILP (current_buffer->enable_multibyte_characters));
7822 }
7823 }
7824
7825
7826 /* Make sure echo area buffers in `echo_buffers' are live.
7827 If they aren't, make new ones. */
7828
7829 static void
7830 ensure_echo_area_buffers ()
7831 {
7832 int i;
7833
7834 for (i = 0; i < 2; ++i)
7835 if (!BUFFERP (echo_buffer[i])
7836 || NILP (XBUFFER (echo_buffer[i])->name))
7837 {
7838 char name[30];
7839 Lisp_Object old_buffer;
7840 int j;
7841
7842 old_buffer = echo_buffer[i];
7843 sprintf (name, " *Echo Area %d*", i);
7844 echo_buffer[i] = Fget_buffer_create (build_string (name));
7845 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7846
7847 for (j = 0; j < 2; ++j)
7848 if (EQ (old_buffer, echo_area_buffer[j]))
7849 echo_area_buffer[j] = echo_buffer[i];
7850 }
7851 }
7852
7853
7854 /* Call FN with args A1..A4 with either the current or last displayed
7855 echo_area_buffer as current buffer.
7856
7857 WHICH zero means use the current message buffer
7858 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7859 from echo_buffer[] and clear it.
7860
7861 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7862 suitable buffer from echo_buffer[] and clear it.
7863
7864 Value is what FN returns. */
7865
7866 static int
7867 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7868 struct window *w;
7869 int which;
7870 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7871 EMACS_INT a1;
7872 Lisp_Object a2;
7873 EMACS_INT a3, a4;
7874 {
7875 Lisp_Object buffer;
7876 int this_one, the_other, clear_buffer_p, rc;
7877 int count = SPECPDL_INDEX ();
7878
7879 /* If buffers aren't live, make new ones. */
7880 ensure_echo_area_buffers ();
7881
7882 clear_buffer_p = 0;
7883
7884 if (which == 0)
7885 this_one = 0, the_other = 1;
7886 else if (which > 0)
7887 this_one = 1, the_other = 0;
7888
7889 /* Choose a suitable buffer from echo_buffer[] is we don't
7890 have one. */
7891 if (NILP (echo_area_buffer[this_one]))
7892 {
7893 echo_area_buffer[this_one]
7894 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7895 ? echo_buffer[the_other]
7896 : echo_buffer[this_one]);
7897 clear_buffer_p = 1;
7898 }
7899
7900 buffer = echo_area_buffer[this_one];
7901
7902 /* Don't get confused by reusing the buffer used for echoing
7903 for a different purpose. */
7904 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7905 cancel_echoing ();
7906
7907 record_unwind_protect (unwind_with_echo_area_buffer,
7908 with_echo_area_buffer_unwind_data (w));
7909
7910 /* Make the echo area buffer current. Note that for display
7911 purposes, it is not necessary that the displayed window's buffer
7912 == current_buffer, except for text property lookup. So, let's
7913 only set that buffer temporarily here without doing a full
7914 Fset_window_buffer. We must also change w->pointm, though,
7915 because otherwise an assertions in unshow_buffer fails, and Emacs
7916 aborts. */
7917 set_buffer_internal_1 (XBUFFER (buffer));
7918 if (w)
7919 {
7920 w->buffer = buffer;
7921 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7922 }
7923
7924 current_buffer->undo_list = Qt;
7925 current_buffer->read_only = Qnil;
7926 specbind (Qinhibit_read_only, Qt);
7927 specbind (Qinhibit_modification_hooks, Qt);
7928
7929 if (clear_buffer_p && Z > BEG)
7930 del_range (BEG, Z);
7931
7932 xassert (BEGV >= BEG);
7933 xassert (ZV <= Z && ZV >= BEGV);
7934
7935 rc = fn (a1, a2, a3, a4);
7936
7937 xassert (BEGV >= BEG);
7938 xassert (ZV <= Z && ZV >= BEGV);
7939
7940 unbind_to (count, Qnil);
7941 return rc;
7942 }
7943
7944
7945 /* Save state that should be preserved around the call to the function
7946 FN called in with_echo_area_buffer. */
7947
7948 static Lisp_Object
7949 with_echo_area_buffer_unwind_data (w)
7950 struct window *w;
7951 {
7952 int i = 0;
7953 Lisp_Object vector;
7954
7955 /* Reduce consing by keeping one vector in
7956 Vwith_echo_area_save_vector. */
7957 vector = Vwith_echo_area_save_vector;
7958 Vwith_echo_area_save_vector = Qnil;
7959
7960 if (NILP (vector))
7961 vector = Fmake_vector (make_number (7), Qnil);
7962
7963 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7964 AREF (vector, i) = Vdeactivate_mark, ++i;
7965 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7966
7967 if (w)
7968 {
7969 XSETWINDOW (AREF (vector, i), w); ++i;
7970 AREF (vector, i) = w->buffer; ++i;
7971 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7972 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7973 }
7974 else
7975 {
7976 int end = i + 4;
7977 for (; i < end; ++i)
7978 AREF (vector, i) = Qnil;
7979 }
7980
7981 xassert (i == ASIZE (vector));
7982 return vector;
7983 }
7984
7985
7986 /* Restore global state from VECTOR which was created by
7987 with_echo_area_buffer_unwind_data. */
7988
7989 static Lisp_Object
7990 unwind_with_echo_area_buffer (vector)
7991 Lisp_Object vector;
7992 {
7993 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7994 Vdeactivate_mark = AREF (vector, 1);
7995 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7996
7997 if (WINDOWP (AREF (vector, 3)))
7998 {
7999 struct window *w;
8000 Lisp_Object buffer, charpos, bytepos;
8001
8002 w = XWINDOW (AREF (vector, 3));
8003 buffer = AREF (vector, 4);
8004 charpos = AREF (vector, 5);
8005 bytepos = AREF (vector, 6);
8006
8007 w->buffer = buffer;
8008 set_marker_both (w->pointm, buffer,
8009 XFASTINT (charpos), XFASTINT (bytepos));
8010 }
8011
8012 Vwith_echo_area_save_vector = vector;
8013 return Qnil;
8014 }
8015
8016
8017 /* Set up the echo area for use by print functions. MULTIBYTE_P
8018 non-zero means we will print multibyte. */
8019
8020 void
8021 setup_echo_area_for_printing (multibyte_p)
8022 int multibyte_p;
8023 {
8024 /* If we can't find an echo area any more, exit. */
8025 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8026 Fkill_emacs (Qnil);
8027
8028 ensure_echo_area_buffers ();
8029
8030 if (!message_buf_print)
8031 {
8032 /* A message has been output since the last time we printed.
8033 Choose a fresh echo area buffer. */
8034 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8035 echo_area_buffer[0] = echo_buffer[1];
8036 else
8037 echo_area_buffer[0] = echo_buffer[0];
8038
8039 /* Switch to that buffer and clear it. */
8040 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8041 current_buffer->truncate_lines = Qnil;
8042
8043 if (Z > BEG)
8044 {
8045 int count = SPECPDL_INDEX ();
8046 specbind (Qinhibit_read_only, Qt);
8047 /* Note that undo recording is always disabled. */
8048 del_range (BEG, Z);
8049 unbind_to (count, Qnil);
8050 }
8051 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8052
8053 /* Set up the buffer for the multibyteness we need. */
8054 if (multibyte_p
8055 != !NILP (current_buffer->enable_multibyte_characters))
8056 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8057
8058 /* Raise the frame containing the echo area. */
8059 if (minibuffer_auto_raise)
8060 {
8061 struct frame *sf = SELECTED_FRAME ();
8062 Lisp_Object mini_window;
8063 mini_window = FRAME_MINIBUF_WINDOW (sf);
8064 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8065 }
8066
8067 message_log_maybe_newline ();
8068 message_buf_print = 1;
8069 }
8070 else
8071 {
8072 if (NILP (echo_area_buffer[0]))
8073 {
8074 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8075 echo_area_buffer[0] = echo_buffer[1];
8076 else
8077 echo_area_buffer[0] = echo_buffer[0];
8078 }
8079
8080 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8081 {
8082 /* Someone switched buffers between print requests. */
8083 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8084 current_buffer->truncate_lines = Qnil;
8085 }
8086 }
8087 }
8088
8089
8090 /* Display an echo area message in window W. Value is non-zero if W's
8091 height is changed. If display_last_displayed_message_p is
8092 non-zero, display the message that was last displayed, otherwise
8093 display the current message. */
8094
8095 static int
8096 display_echo_area (w)
8097 struct window *w;
8098 {
8099 int i, no_message_p, window_height_changed_p, count;
8100
8101 /* Temporarily disable garbage collections while displaying the echo
8102 area. This is done because a GC can print a message itself.
8103 That message would modify the echo area buffer's contents while a
8104 redisplay of the buffer is going on, and seriously confuse
8105 redisplay. */
8106 count = inhibit_garbage_collection ();
8107
8108 /* If there is no message, we must call display_echo_area_1
8109 nevertheless because it resizes the window. But we will have to
8110 reset the echo_area_buffer in question to nil at the end because
8111 with_echo_area_buffer will sets it to an empty buffer. */
8112 i = display_last_displayed_message_p ? 1 : 0;
8113 no_message_p = NILP (echo_area_buffer[i]);
8114
8115 window_height_changed_p
8116 = with_echo_area_buffer (w, display_last_displayed_message_p,
8117 display_echo_area_1,
8118 (EMACS_INT) w, Qnil, 0, 0);
8119
8120 if (no_message_p)
8121 echo_area_buffer[i] = Qnil;
8122
8123 unbind_to (count, Qnil);
8124 return window_height_changed_p;
8125 }
8126
8127
8128 /* Helper for display_echo_area. Display the current buffer which
8129 contains the current echo area message in window W, a mini-window,
8130 a pointer to which is passed in A1. A2..A4 are currently not used.
8131 Change the height of W so that all of the message is displayed.
8132 Value is non-zero if height of W was changed. */
8133
8134 static int
8135 display_echo_area_1 (a1, a2, a3, a4)
8136 EMACS_INT a1;
8137 Lisp_Object a2;
8138 EMACS_INT a3, a4;
8139 {
8140 struct window *w = (struct window *) a1;
8141 Lisp_Object window;
8142 struct text_pos start;
8143 int window_height_changed_p = 0;
8144
8145 /* Do this before displaying, so that we have a large enough glyph
8146 matrix for the display. If we can't get enough space for the
8147 whole text, display the last N lines. That works by setting w->start. */
8148 window_height_changed_p = resize_mini_window (w, 0);
8149
8150 /* Use the starting position chosen by resize_mini_window. */
8151 SET_TEXT_POS_FROM_MARKER (start, w->start);
8152
8153 /* Display. */
8154 clear_glyph_matrix (w->desired_matrix);
8155 XSETWINDOW (window, w);
8156 try_window (window, start, 0);
8157
8158 return window_height_changed_p;
8159 }
8160
8161
8162 /* Resize the echo area window to exactly the size needed for the
8163 currently displayed message, if there is one. If a mini-buffer
8164 is active, don't shrink it. */
8165
8166 void
8167 resize_echo_area_exactly ()
8168 {
8169 if (BUFFERP (echo_area_buffer[0])
8170 && WINDOWP (echo_area_window))
8171 {
8172 struct window *w = XWINDOW (echo_area_window);
8173 int resized_p;
8174 Lisp_Object resize_exactly;
8175
8176 if (minibuf_level == 0)
8177 resize_exactly = Qt;
8178 else
8179 resize_exactly = Qnil;
8180
8181 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8182 (EMACS_INT) w, resize_exactly, 0, 0);
8183 if (resized_p)
8184 {
8185 ++windows_or_buffers_changed;
8186 ++update_mode_lines;
8187 redisplay_internal (0);
8188 }
8189 }
8190 }
8191
8192
8193 /* Callback function for with_echo_area_buffer, when used from
8194 resize_echo_area_exactly. A1 contains a pointer to the window to
8195 resize, EXACTLY non-nil means resize the mini-window exactly to the
8196 size of the text displayed. A3 and A4 are not used. Value is what
8197 resize_mini_window returns. */
8198
8199 static int
8200 resize_mini_window_1 (a1, exactly, a3, a4)
8201 EMACS_INT a1;
8202 Lisp_Object exactly;
8203 EMACS_INT a3, a4;
8204 {
8205 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8206 }
8207
8208
8209 /* Resize mini-window W to fit the size of its contents. EXACT:P
8210 means size the window exactly to the size needed. Otherwise, it's
8211 only enlarged until W's buffer is empty.
8212
8213 Set W->start to the right place to begin display. If the whole
8214 contents fit, start at the beginning. Otherwise, start so as
8215 to make the end of the contents appear. This is particularly
8216 important for y-or-n-p, but seems desirable generally.
8217
8218 Value is non-zero if the window height has been changed. */
8219
8220 int
8221 resize_mini_window (w, exact_p)
8222 struct window *w;
8223 int exact_p;
8224 {
8225 struct frame *f = XFRAME (w->frame);
8226 int window_height_changed_p = 0;
8227
8228 xassert (MINI_WINDOW_P (w));
8229
8230 /* By default, start display at the beginning. */
8231 set_marker_both (w->start, w->buffer,
8232 BUF_BEGV (XBUFFER (w->buffer)),
8233 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8234
8235 /* Don't resize windows while redisplaying a window; it would
8236 confuse redisplay functions when the size of the window they are
8237 displaying changes from under them. Such a resizing can happen,
8238 for instance, when which-func prints a long message while
8239 we are running fontification-functions. We're running these
8240 functions with safe_call which binds inhibit-redisplay to t. */
8241 if (!NILP (Vinhibit_redisplay))
8242 return 0;
8243
8244 /* Nil means don't try to resize. */
8245 if (NILP (Vresize_mini_windows)
8246 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8247 return 0;
8248
8249 if (!FRAME_MINIBUF_ONLY_P (f))
8250 {
8251 struct it it;
8252 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8253 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8254 int height, max_height;
8255 int unit = FRAME_LINE_HEIGHT (f);
8256 struct text_pos start;
8257 struct buffer *old_current_buffer = NULL;
8258
8259 if (current_buffer != XBUFFER (w->buffer))
8260 {
8261 old_current_buffer = current_buffer;
8262 set_buffer_internal (XBUFFER (w->buffer));
8263 }
8264
8265 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8266
8267 /* Compute the max. number of lines specified by the user. */
8268 if (FLOATP (Vmax_mini_window_height))
8269 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8270 else if (INTEGERP (Vmax_mini_window_height))
8271 max_height = XINT (Vmax_mini_window_height);
8272 else
8273 max_height = total_height / 4;
8274
8275 /* Correct that max. height if it's bogus. */
8276 max_height = max (1, max_height);
8277 max_height = min (total_height, max_height);
8278
8279 /* Find out the height of the text in the window. */
8280 if (it.truncate_lines_p)
8281 height = 1;
8282 else
8283 {
8284 last_height = 0;
8285 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8286 if (it.max_ascent == 0 && it.max_descent == 0)
8287 height = it.current_y + last_height;
8288 else
8289 height = it.current_y + it.max_ascent + it.max_descent;
8290 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8291 height = (height + unit - 1) / unit;
8292 }
8293
8294 /* Compute a suitable window start. */
8295 if (height > max_height)
8296 {
8297 height = max_height;
8298 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8299 move_it_vertically_backward (&it, (height - 1) * unit);
8300 start = it.current.pos;
8301 }
8302 else
8303 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8304 SET_MARKER_FROM_TEXT_POS (w->start, start);
8305
8306 if (EQ (Vresize_mini_windows, Qgrow_only))
8307 {
8308 /* Let it grow only, until we display an empty message, in which
8309 case the window shrinks again. */
8310 if (height > WINDOW_TOTAL_LINES (w))
8311 {
8312 int old_height = WINDOW_TOTAL_LINES (w);
8313 freeze_window_starts (f, 1);
8314 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8315 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8316 }
8317 else if (height < WINDOW_TOTAL_LINES (w)
8318 && (exact_p || BEGV == ZV))
8319 {
8320 int old_height = WINDOW_TOTAL_LINES (w);
8321 freeze_window_starts (f, 0);
8322 shrink_mini_window (w);
8323 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8324 }
8325 }
8326 else
8327 {
8328 /* Always resize to exact size needed. */
8329 if (height > WINDOW_TOTAL_LINES (w))
8330 {
8331 int old_height = WINDOW_TOTAL_LINES (w);
8332 freeze_window_starts (f, 1);
8333 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8334 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8335 }
8336 else if (height < WINDOW_TOTAL_LINES (w))
8337 {
8338 int old_height = WINDOW_TOTAL_LINES (w);
8339 freeze_window_starts (f, 0);
8340 shrink_mini_window (w);
8341
8342 if (height)
8343 {
8344 freeze_window_starts (f, 1);
8345 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8346 }
8347
8348 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8349 }
8350 }
8351
8352 if (old_current_buffer)
8353 set_buffer_internal (old_current_buffer);
8354 }
8355
8356 return window_height_changed_p;
8357 }
8358
8359
8360 /* Value is the current message, a string, or nil if there is no
8361 current message. */
8362
8363 Lisp_Object
8364 current_message ()
8365 {
8366 Lisp_Object msg;
8367
8368 if (NILP (echo_area_buffer[0]))
8369 msg = Qnil;
8370 else
8371 {
8372 with_echo_area_buffer (0, 0, current_message_1,
8373 (EMACS_INT) &msg, Qnil, 0, 0);
8374 if (NILP (msg))
8375 echo_area_buffer[0] = Qnil;
8376 }
8377
8378 return msg;
8379 }
8380
8381
8382 static int
8383 current_message_1 (a1, a2, a3, a4)
8384 EMACS_INT a1;
8385 Lisp_Object a2;
8386 EMACS_INT a3, a4;
8387 {
8388 Lisp_Object *msg = (Lisp_Object *) a1;
8389
8390 if (Z > BEG)
8391 *msg = make_buffer_string (BEG, Z, 1);
8392 else
8393 *msg = Qnil;
8394 return 0;
8395 }
8396
8397
8398 /* Push the current message on Vmessage_stack for later restauration
8399 by restore_message. Value is non-zero if the current message isn't
8400 empty. This is a relatively infrequent operation, so it's not
8401 worth optimizing. */
8402
8403 int
8404 push_message ()
8405 {
8406 Lisp_Object msg;
8407 msg = current_message ();
8408 Vmessage_stack = Fcons (msg, Vmessage_stack);
8409 return STRINGP (msg);
8410 }
8411
8412
8413 /* Restore message display from the top of Vmessage_stack. */
8414
8415 void
8416 restore_message ()
8417 {
8418 Lisp_Object msg;
8419
8420 xassert (CONSP (Vmessage_stack));
8421 msg = XCAR (Vmessage_stack);
8422 if (STRINGP (msg))
8423 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8424 else
8425 message3_nolog (msg, 0, 0);
8426 }
8427
8428
8429 /* Handler for record_unwind_protect calling pop_message. */
8430
8431 Lisp_Object
8432 pop_message_unwind (dummy)
8433 Lisp_Object dummy;
8434 {
8435 pop_message ();
8436 return Qnil;
8437 }
8438
8439 /* Pop the top-most entry off Vmessage_stack. */
8440
8441 void
8442 pop_message ()
8443 {
8444 xassert (CONSP (Vmessage_stack));
8445 Vmessage_stack = XCDR (Vmessage_stack);
8446 }
8447
8448
8449 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8450 exits. If the stack is not empty, we have a missing pop_message
8451 somewhere. */
8452
8453 void
8454 check_message_stack ()
8455 {
8456 if (!NILP (Vmessage_stack))
8457 abort ();
8458 }
8459
8460
8461 /* Truncate to NCHARS what will be displayed in the echo area the next
8462 time we display it---but don't redisplay it now. */
8463
8464 void
8465 truncate_echo_area (nchars)
8466 int nchars;
8467 {
8468 if (nchars == 0)
8469 echo_area_buffer[0] = Qnil;
8470 /* A null message buffer means that the frame hasn't really been
8471 initialized yet. Error messages get reported properly by
8472 cmd_error, so this must be just an informative message; toss it. */
8473 else if (!noninteractive
8474 && INTERACTIVE
8475 && !NILP (echo_area_buffer[0]))
8476 {
8477 struct frame *sf = SELECTED_FRAME ();
8478 if (FRAME_MESSAGE_BUF (sf))
8479 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8480 }
8481 }
8482
8483
8484 /* Helper function for truncate_echo_area. Truncate the current
8485 message to at most NCHARS characters. */
8486
8487 static int
8488 truncate_message_1 (nchars, a2, a3, a4)
8489 EMACS_INT nchars;
8490 Lisp_Object a2;
8491 EMACS_INT a3, a4;
8492 {
8493 if (BEG + nchars < Z)
8494 del_range (BEG + nchars, Z);
8495 if (Z == BEG)
8496 echo_area_buffer[0] = Qnil;
8497 return 0;
8498 }
8499
8500
8501 /* Set the current message to a substring of S or STRING.
8502
8503 If STRING is a Lisp string, set the message to the first NBYTES
8504 bytes from STRING. NBYTES zero means use the whole string. If
8505 STRING is multibyte, the message will be displayed multibyte.
8506
8507 If S is not null, set the message to the first LEN bytes of S. LEN
8508 zero means use the whole string. MULTIBYTE_P non-zero means S is
8509 multibyte. Display the message multibyte in that case.
8510
8511 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8512 to t before calling set_message_1 (which calls insert).
8513 */
8514
8515 void
8516 set_message (s, string, nbytes, multibyte_p)
8517 const char *s;
8518 Lisp_Object string;
8519 int nbytes, multibyte_p;
8520 {
8521 message_enable_multibyte
8522 = ((s && multibyte_p)
8523 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8524
8525 with_echo_area_buffer (0, 0, set_message_1,
8526 (EMACS_INT) s, string, nbytes, multibyte_p);
8527 message_buf_print = 0;
8528 help_echo_showing_p = 0;
8529 }
8530
8531
8532 /* Helper function for set_message. Arguments have the same meaning
8533 as there, with A1 corresponding to S and A2 corresponding to STRING
8534 This function is called with the echo area buffer being
8535 current. */
8536
8537 static int
8538 set_message_1 (a1, a2, nbytes, multibyte_p)
8539 EMACS_INT a1;
8540 Lisp_Object a2;
8541 EMACS_INT nbytes, multibyte_p;
8542 {
8543 const char *s = (const char *) a1;
8544 Lisp_Object string = a2;
8545
8546 /* Change multibyteness of the echo buffer appropriately. */
8547 if (message_enable_multibyte
8548 != !NILP (current_buffer->enable_multibyte_characters))
8549 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8550
8551 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8552
8553 /* Insert new message at BEG. */
8554 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8555 Ferase_buffer ();
8556
8557 if (STRINGP (string))
8558 {
8559 int nchars;
8560
8561 if (nbytes == 0)
8562 nbytes = SBYTES (string);
8563 nchars = string_byte_to_char (string, nbytes);
8564
8565 /* This function takes care of single/multibyte conversion. We
8566 just have to ensure that the echo area buffer has the right
8567 setting of enable_multibyte_characters. */
8568 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8569 }
8570 else if (s)
8571 {
8572 if (nbytes == 0)
8573 nbytes = strlen (s);
8574
8575 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8576 {
8577 /* Convert from multi-byte to single-byte. */
8578 int i, c, n;
8579 unsigned char work[1];
8580
8581 /* Convert a multibyte string to single-byte. */
8582 for (i = 0; i < nbytes; i += n)
8583 {
8584 c = string_char_and_length (s + i, nbytes - i, &n);
8585 work[0] = (SINGLE_BYTE_CHAR_P (c)
8586 ? c
8587 : multibyte_char_to_unibyte (c, Qnil));
8588 insert_1_both (work, 1, 1, 1, 0, 0);
8589 }
8590 }
8591 else if (!multibyte_p
8592 && !NILP (current_buffer->enable_multibyte_characters))
8593 {
8594 /* Convert from single-byte to multi-byte. */
8595 int i, c, n;
8596 const unsigned char *msg = (const unsigned char *) s;
8597 unsigned char str[MAX_MULTIBYTE_LENGTH];
8598
8599 /* Convert a single-byte string to multibyte. */
8600 for (i = 0; i < nbytes; i++)
8601 {
8602 c = unibyte_char_to_multibyte (msg[i]);
8603 n = CHAR_STRING (c, str);
8604 insert_1_both (str, 1, n, 1, 0, 0);
8605 }
8606 }
8607 else
8608 insert_1 (s, nbytes, 1, 0, 0);
8609 }
8610
8611 return 0;
8612 }
8613
8614
8615 /* Clear messages. CURRENT_P non-zero means clear the current
8616 message. LAST_DISPLAYED_P non-zero means clear the message
8617 last displayed. */
8618
8619 void
8620 clear_message (current_p, last_displayed_p)
8621 int current_p, last_displayed_p;
8622 {
8623 if (current_p)
8624 {
8625 echo_area_buffer[0] = Qnil;
8626 message_cleared_p = 1;
8627 }
8628
8629 if (last_displayed_p)
8630 echo_area_buffer[1] = Qnil;
8631
8632 message_buf_print = 0;
8633 }
8634
8635 /* Clear garbaged frames.
8636
8637 This function is used where the old redisplay called
8638 redraw_garbaged_frames which in turn called redraw_frame which in
8639 turn called clear_frame. The call to clear_frame was a source of
8640 flickering. I believe a clear_frame is not necessary. It should
8641 suffice in the new redisplay to invalidate all current matrices,
8642 and ensure a complete redisplay of all windows. */
8643
8644 static void
8645 clear_garbaged_frames ()
8646 {
8647 if (frame_garbaged)
8648 {
8649 Lisp_Object tail, frame;
8650 int changed_count = 0;
8651
8652 FOR_EACH_FRAME (tail, frame)
8653 {
8654 struct frame *f = XFRAME (frame);
8655
8656 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8657 {
8658 if (f->resized_p)
8659 {
8660 Fredraw_frame (frame);
8661 f->force_flush_display_p = 1;
8662 }
8663 clear_current_matrices (f);
8664 changed_count++;
8665 f->garbaged = 0;
8666 f->resized_p = 0;
8667 }
8668 }
8669
8670 frame_garbaged = 0;
8671 if (changed_count)
8672 ++windows_or_buffers_changed;
8673 }
8674 }
8675
8676
8677 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8678 is non-zero update selected_frame. Value is non-zero if the
8679 mini-windows height has been changed. */
8680
8681 static int
8682 echo_area_display (update_frame_p)
8683 int update_frame_p;
8684 {
8685 Lisp_Object mini_window;
8686 struct window *w;
8687 struct frame *f;
8688 int window_height_changed_p = 0;
8689 struct frame *sf = SELECTED_FRAME ();
8690
8691 mini_window = FRAME_MINIBUF_WINDOW (sf);
8692 w = XWINDOW (mini_window);
8693 f = XFRAME (WINDOW_FRAME (w));
8694
8695 /* Don't display if frame is invisible or not yet initialized. */
8696 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8697 return 0;
8698
8699 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8700 #ifndef MAC_OS8
8701 #ifdef HAVE_WINDOW_SYSTEM
8702 /* When Emacs starts, selected_frame may be a visible terminal
8703 frame, even if we run under a window system. If we let this
8704 through, a message would be displayed on the terminal. */
8705 if (EQ (selected_frame, Vterminal_frame)
8706 && !NILP (Vwindow_system))
8707 return 0;
8708 #endif /* HAVE_WINDOW_SYSTEM */
8709 #endif
8710
8711 /* Redraw garbaged frames. */
8712 if (frame_garbaged)
8713 clear_garbaged_frames ();
8714
8715 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8716 {
8717 echo_area_window = mini_window;
8718 window_height_changed_p = display_echo_area (w);
8719 w->must_be_updated_p = 1;
8720
8721 /* Update the display, unless called from redisplay_internal.
8722 Also don't update the screen during redisplay itself. The
8723 update will happen at the end of redisplay, and an update
8724 here could cause confusion. */
8725 if (update_frame_p && !redisplaying_p)
8726 {
8727 int n = 0;
8728
8729 /* If the display update has been interrupted by pending
8730 input, update mode lines in the frame. Due to the
8731 pending input, it might have been that redisplay hasn't
8732 been called, so that mode lines above the echo area are
8733 garbaged. This looks odd, so we prevent it here. */
8734 if (!display_completed)
8735 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8736
8737 if (window_height_changed_p
8738 /* Don't do this if Emacs is shutting down. Redisplay
8739 needs to run hooks. */
8740 && !NILP (Vrun_hooks))
8741 {
8742 /* Must update other windows. Likewise as in other
8743 cases, don't let this update be interrupted by
8744 pending input. */
8745 int count = SPECPDL_INDEX ();
8746 specbind (Qredisplay_dont_pause, Qt);
8747 windows_or_buffers_changed = 1;
8748 redisplay_internal (0);
8749 unbind_to (count, Qnil);
8750 }
8751 else if (FRAME_WINDOW_P (f) && n == 0)
8752 {
8753 /* Window configuration is the same as before.
8754 Can do with a display update of the echo area,
8755 unless we displayed some mode lines. */
8756 update_single_window (w, 1);
8757 rif->flush_display (f);
8758 }
8759 else
8760 update_frame (f, 1, 1);
8761
8762 /* If cursor is in the echo area, make sure that the next
8763 redisplay displays the minibuffer, so that the cursor will
8764 be replaced with what the minibuffer wants. */
8765 if (cursor_in_echo_area)
8766 ++windows_or_buffers_changed;
8767 }
8768 }
8769 else if (!EQ (mini_window, selected_window))
8770 windows_or_buffers_changed++;
8771
8772 /* The current message is now also the last one displayed. */
8773 echo_area_buffer[1] = echo_area_buffer[0];
8774
8775 /* Prevent redisplay optimization in redisplay_internal by resetting
8776 this_line_start_pos. This is done because the mini-buffer now
8777 displays the message instead of its buffer text. */
8778 if (EQ (mini_window, selected_window))
8779 CHARPOS (this_line_start_pos) = 0;
8780
8781 return window_height_changed_p;
8782 }
8783
8784
8785 \f
8786 /***********************************************************************
8787 Mode Lines and Frame Titles
8788 ***********************************************************************/
8789
8790 /* A buffer for constructing non-propertized mode-line strings and
8791 frame titles in it; allocated from the heap in init_xdisp and
8792 resized as needed in store_mode_line_noprop_char. */
8793
8794 static char *mode_line_noprop_buf;
8795
8796 /* The buffer's end, and a current output position in it. */
8797
8798 static char *mode_line_noprop_buf_end;
8799 static char *mode_line_noprop_ptr;
8800
8801 #define MODE_LINE_NOPROP_LEN(start) \
8802 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8803
8804 static enum {
8805 MODE_LINE_DISPLAY = 0,
8806 MODE_LINE_TITLE,
8807 MODE_LINE_NOPROP,
8808 MODE_LINE_STRING
8809 } mode_line_target;
8810
8811 /* Alist that caches the results of :propertize.
8812 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8813 static Lisp_Object mode_line_proptrans_alist;
8814
8815 /* List of strings making up the mode-line. */
8816 static Lisp_Object mode_line_string_list;
8817
8818 /* Base face property when building propertized mode line string. */
8819 static Lisp_Object mode_line_string_face;
8820 static Lisp_Object mode_line_string_face_prop;
8821
8822
8823 /* Unwind data for mode line strings */
8824
8825 static Lisp_Object Vmode_line_unwind_vector;
8826
8827 static Lisp_Object
8828 format_mode_line_unwind_data (obuf, save_proptrans)
8829 struct buffer *obuf;
8830 {
8831 Lisp_Object vector;
8832
8833 /* Reduce consing by keeping one vector in
8834 Vwith_echo_area_save_vector. */
8835 vector = Vmode_line_unwind_vector;
8836 Vmode_line_unwind_vector = Qnil;
8837
8838 if (NILP (vector))
8839 vector = Fmake_vector (make_number (7), Qnil);
8840
8841 AREF (vector, 0) = make_number (mode_line_target);
8842 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8843 AREF (vector, 2) = mode_line_string_list;
8844 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8845 AREF (vector, 4) = mode_line_string_face;
8846 AREF (vector, 5) = mode_line_string_face_prop;
8847
8848 if (obuf)
8849 XSETBUFFER (AREF (vector, 6), obuf);
8850 else
8851 AREF (vector, 6) = Qnil;
8852
8853 return vector;
8854 }
8855
8856 static Lisp_Object
8857 unwind_format_mode_line (vector)
8858 Lisp_Object vector;
8859 {
8860 mode_line_target = XINT (AREF (vector, 0));
8861 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8862 mode_line_string_list = AREF (vector, 2);
8863 if (! EQ (AREF (vector, 3), Qt))
8864 mode_line_proptrans_alist = AREF (vector, 3);
8865 mode_line_string_face = AREF (vector, 4);
8866 mode_line_string_face_prop = AREF (vector, 5);
8867
8868 if (!NILP (AREF (vector, 6)))
8869 {
8870 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8871 AREF (vector, 6) = Qnil;
8872 }
8873
8874 Vmode_line_unwind_vector = vector;
8875 return Qnil;
8876 }
8877
8878
8879 /* Store a single character C for the frame title in mode_line_noprop_buf.
8880 Re-allocate mode_line_noprop_buf if necessary. */
8881
8882 static void
8883 #ifdef PROTOTYPES
8884 store_mode_line_noprop_char (char c)
8885 #else
8886 store_mode_line_noprop_char (c)
8887 char c;
8888 #endif
8889 {
8890 /* If output position has reached the end of the allocated buffer,
8891 double the buffer's size. */
8892 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8893 {
8894 int len = MODE_LINE_NOPROP_LEN (0);
8895 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8896 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8897 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8898 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8899 }
8900
8901 *mode_line_noprop_ptr++ = c;
8902 }
8903
8904
8905 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8906 mode_line_noprop_ptr. STR is the string to store. Do not copy
8907 characters that yield more columns than PRECISION; PRECISION <= 0
8908 means copy the whole string. Pad with spaces until FIELD_WIDTH
8909 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8910 pad. Called from display_mode_element when it is used to build a
8911 frame title. */
8912
8913 static int
8914 store_mode_line_noprop (str, field_width, precision)
8915 const unsigned char *str;
8916 int field_width, precision;
8917 {
8918 int n = 0;
8919 int dummy, nbytes;
8920
8921 /* Copy at most PRECISION chars from STR. */
8922 nbytes = strlen (str);
8923 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8924 while (nbytes--)
8925 store_mode_line_noprop_char (*str++);
8926
8927 /* Fill up with spaces until FIELD_WIDTH reached. */
8928 while (field_width > 0
8929 && n < field_width)
8930 {
8931 store_mode_line_noprop_char (' ');
8932 ++n;
8933 }
8934
8935 return n;
8936 }
8937
8938 /***********************************************************************
8939 Frame Titles
8940 ***********************************************************************/
8941
8942 #ifdef HAVE_WINDOW_SYSTEM
8943
8944 /* Set the title of FRAME, if it has changed. The title format is
8945 Vicon_title_format if FRAME is iconified, otherwise it is
8946 frame_title_format. */
8947
8948 static void
8949 x_consider_frame_title (frame)
8950 Lisp_Object frame;
8951 {
8952 struct frame *f = XFRAME (frame);
8953
8954 if (FRAME_WINDOW_P (f)
8955 || FRAME_MINIBUF_ONLY_P (f)
8956 || f->explicit_name)
8957 {
8958 /* Do we have more than one visible frame on this X display? */
8959 Lisp_Object tail;
8960 Lisp_Object fmt;
8961 int title_start;
8962 char *title;
8963 int len;
8964 struct it it;
8965 int count = SPECPDL_INDEX ();
8966
8967 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8968 {
8969 Lisp_Object other_frame = XCAR (tail);
8970 struct frame *tf = XFRAME (other_frame);
8971
8972 if (tf != f
8973 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8974 && !FRAME_MINIBUF_ONLY_P (tf)
8975 && !EQ (other_frame, tip_frame)
8976 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8977 break;
8978 }
8979
8980 /* Set global variable indicating that multiple frames exist. */
8981 multiple_frames = CONSP (tail);
8982
8983 /* Switch to the buffer of selected window of the frame. Set up
8984 mode_line_target so that display_mode_element will output into
8985 mode_line_noprop_buf; then display the title. */
8986 record_unwind_protect (unwind_format_mode_line,
8987 format_mode_line_unwind_data (current_buffer, 0));
8988
8989 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8990 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8991
8992 mode_line_target = MODE_LINE_TITLE;
8993 title_start = MODE_LINE_NOPROP_LEN (0);
8994 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8995 NULL, DEFAULT_FACE_ID);
8996 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8997 len = MODE_LINE_NOPROP_LEN (title_start);
8998 title = mode_line_noprop_buf + title_start;
8999 unbind_to (count, Qnil);
9000
9001 /* Set the title only if it's changed. This avoids consing in
9002 the common case where it hasn't. (If it turns out that we've
9003 already wasted too much time by walking through the list with
9004 display_mode_element, then we might need to optimize at a
9005 higher level than this.) */
9006 if (! STRINGP (f->name)
9007 || SBYTES (f->name) != len
9008 || bcmp (title, SDATA (f->name), len) != 0)
9009 x_implicitly_set_name (f, make_string (title, len), Qnil);
9010 }
9011 }
9012
9013 #endif /* not HAVE_WINDOW_SYSTEM */
9014
9015
9016
9017 \f
9018 /***********************************************************************
9019 Menu Bars
9020 ***********************************************************************/
9021
9022
9023 /* Prepare for redisplay by updating menu-bar item lists when
9024 appropriate. This can call eval. */
9025
9026 void
9027 prepare_menu_bars ()
9028 {
9029 int all_windows;
9030 struct gcpro gcpro1, gcpro2;
9031 struct frame *f;
9032 Lisp_Object tooltip_frame;
9033
9034 #ifdef HAVE_WINDOW_SYSTEM
9035 tooltip_frame = tip_frame;
9036 #else
9037 tooltip_frame = Qnil;
9038 #endif
9039
9040 /* Update all frame titles based on their buffer names, etc. We do
9041 this before the menu bars so that the buffer-menu will show the
9042 up-to-date frame titles. */
9043 #ifdef HAVE_WINDOW_SYSTEM
9044 if (windows_or_buffers_changed || update_mode_lines)
9045 {
9046 Lisp_Object tail, frame;
9047
9048 FOR_EACH_FRAME (tail, frame)
9049 {
9050 f = XFRAME (frame);
9051 if (!EQ (frame, tooltip_frame)
9052 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9053 x_consider_frame_title (frame);
9054 }
9055 }
9056 #endif /* HAVE_WINDOW_SYSTEM */
9057
9058 /* Update the menu bar item lists, if appropriate. This has to be
9059 done before any actual redisplay or generation of display lines. */
9060 all_windows = (update_mode_lines
9061 || buffer_shared > 1
9062 || windows_or_buffers_changed);
9063 if (all_windows)
9064 {
9065 Lisp_Object tail, frame;
9066 int count = SPECPDL_INDEX ();
9067 /* 1 means that update_menu_bar has run its hooks
9068 so any further calls to update_menu_bar shouldn't do so again. */
9069 int menu_bar_hooks_run = 0;
9070
9071 record_unwind_save_match_data ();
9072
9073 FOR_EACH_FRAME (tail, frame)
9074 {
9075 f = XFRAME (frame);
9076
9077 /* Ignore tooltip frame. */
9078 if (EQ (frame, tooltip_frame))
9079 continue;
9080
9081 /* If a window on this frame changed size, report that to
9082 the user and clear the size-change flag. */
9083 if (FRAME_WINDOW_SIZES_CHANGED (f))
9084 {
9085 Lisp_Object functions;
9086
9087 /* Clear flag first in case we get an error below. */
9088 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9089 functions = Vwindow_size_change_functions;
9090 GCPRO2 (tail, functions);
9091
9092 while (CONSP (functions))
9093 {
9094 call1 (XCAR (functions), frame);
9095 functions = XCDR (functions);
9096 }
9097 UNGCPRO;
9098 }
9099
9100 GCPRO1 (tail);
9101 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9102 #ifdef HAVE_WINDOW_SYSTEM
9103 update_tool_bar (f, 0);
9104 #ifdef MAC_OS
9105 mac_update_title_bar (f, 0);
9106 #endif
9107 #endif
9108 UNGCPRO;
9109 }
9110
9111 unbind_to (count, Qnil);
9112 }
9113 else
9114 {
9115 struct frame *sf = SELECTED_FRAME ();
9116 update_menu_bar (sf, 1, 0);
9117 #ifdef HAVE_WINDOW_SYSTEM
9118 update_tool_bar (sf, 1);
9119 #ifdef MAC_OS
9120 mac_update_title_bar (sf, 1);
9121 #endif
9122 #endif
9123 }
9124
9125 /* Motif needs this. See comment in xmenu.c. Turn it off when
9126 pending_menu_activation is not defined. */
9127 #ifdef USE_X_TOOLKIT
9128 pending_menu_activation = 0;
9129 #endif
9130 }
9131
9132
9133 /* Update the menu bar item list for frame F. This has to be done
9134 before we start to fill in any display lines, because it can call
9135 eval.
9136
9137 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9138
9139 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9140 already ran the menu bar hooks for this redisplay, so there
9141 is no need to run them again. The return value is the
9142 updated value of this flag, to pass to the next call. */
9143
9144 static int
9145 update_menu_bar (f, save_match_data, hooks_run)
9146 struct frame *f;
9147 int save_match_data;
9148 int hooks_run;
9149 {
9150 Lisp_Object window;
9151 register struct window *w;
9152
9153 /* If called recursively during a menu update, do nothing. This can
9154 happen when, for instance, an activate-menubar-hook causes a
9155 redisplay. */
9156 if (inhibit_menubar_update)
9157 return hooks_run;
9158
9159 window = FRAME_SELECTED_WINDOW (f);
9160 w = XWINDOW (window);
9161
9162 #if 0 /* The if statement below this if statement used to include the
9163 condition !NILP (w->update_mode_line), rather than using
9164 update_mode_lines directly, and this if statement may have
9165 been added to make that condition work. Now the if
9166 statement below matches its comment, this isn't needed. */
9167 if (update_mode_lines)
9168 w->update_mode_line = Qt;
9169 #endif
9170
9171 if (FRAME_WINDOW_P (f)
9172 ?
9173 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9174 || defined (USE_GTK)
9175 FRAME_EXTERNAL_MENU_BAR (f)
9176 #else
9177 FRAME_MENU_BAR_LINES (f) > 0
9178 #endif
9179 : FRAME_MENU_BAR_LINES (f) > 0)
9180 {
9181 /* If the user has switched buffers or windows, we need to
9182 recompute to reflect the new bindings. But we'll
9183 recompute when update_mode_lines is set too; that means
9184 that people can use force-mode-line-update to request
9185 that the menu bar be recomputed. The adverse effect on
9186 the rest of the redisplay algorithm is about the same as
9187 windows_or_buffers_changed anyway. */
9188 if (windows_or_buffers_changed
9189 /* This used to test w->update_mode_line, but we believe
9190 there is no need to recompute the menu in that case. */
9191 || update_mode_lines
9192 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9193 < BUF_MODIFF (XBUFFER (w->buffer)))
9194 != !NILP (w->last_had_star))
9195 || ((!NILP (Vtransient_mark_mode)
9196 && !NILP (XBUFFER (w->buffer)->mark_active))
9197 != !NILP (w->region_showing)))
9198 {
9199 struct buffer *prev = current_buffer;
9200 int count = SPECPDL_INDEX ();
9201
9202 specbind (Qinhibit_menubar_update, Qt);
9203
9204 set_buffer_internal_1 (XBUFFER (w->buffer));
9205 if (save_match_data)
9206 record_unwind_save_match_data ();
9207 if (NILP (Voverriding_local_map_menu_flag))
9208 {
9209 specbind (Qoverriding_terminal_local_map, Qnil);
9210 specbind (Qoverriding_local_map, Qnil);
9211 }
9212
9213 if (!hooks_run)
9214 {
9215 /* Run the Lucid hook. */
9216 safe_run_hooks (Qactivate_menubar_hook);
9217
9218 /* If it has changed current-menubar from previous value,
9219 really recompute the menu-bar from the value. */
9220 if (! NILP (Vlucid_menu_bar_dirty_flag))
9221 call0 (Qrecompute_lucid_menubar);
9222
9223 safe_run_hooks (Qmenu_bar_update_hook);
9224
9225 hooks_run = 1;
9226 }
9227
9228 XSETFRAME (Vmenu_updating_frame, f);
9229 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9230
9231 /* Redisplay the menu bar in case we changed it. */
9232 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9233 || defined (USE_GTK)
9234 if (FRAME_WINDOW_P (f))
9235 {
9236 #ifdef MAC_OS
9237 /* All frames on Mac OS share the same menubar. So only
9238 the selected frame should be allowed to set it. */
9239 if (f == SELECTED_FRAME ())
9240 #endif
9241 set_frame_menubar (f, 0, 0);
9242 }
9243 else
9244 /* On a terminal screen, the menu bar is an ordinary screen
9245 line, and this makes it get updated. */
9246 w->update_mode_line = Qt;
9247 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9248 /* In the non-toolkit version, the menu bar is an ordinary screen
9249 line, and this makes it get updated. */
9250 w->update_mode_line = Qt;
9251 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9252
9253 unbind_to (count, Qnil);
9254 set_buffer_internal_1 (prev);
9255 }
9256 }
9257
9258 return hooks_run;
9259 }
9260
9261
9262 \f
9263 /***********************************************************************
9264 Output Cursor
9265 ***********************************************************************/
9266
9267 #ifdef HAVE_WINDOW_SYSTEM
9268
9269 /* EXPORT:
9270 Nominal cursor position -- where to draw output.
9271 HPOS and VPOS are window relative glyph matrix coordinates.
9272 X and Y are window relative pixel coordinates. */
9273
9274 struct cursor_pos output_cursor;
9275
9276
9277 /* EXPORT:
9278 Set the global variable output_cursor to CURSOR. All cursor
9279 positions are relative to updated_window. */
9280
9281 void
9282 set_output_cursor (cursor)
9283 struct cursor_pos *cursor;
9284 {
9285 output_cursor.hpos = cursor->hpos;
9286 output_cursor.vpos = cursor->vpos;
9287 output_cursor.x = cursor->x;
9288 output_cursor.y = cursor->y;
9289 }
9290
9291
9292 /* EXPORT for RIF:
9293 Set a nominal cursor position.
9294
9295 HPOS and VPOS are column/row positions in a window glyph matrix. X
9296 and Y are window text area relative pixel positions.
9297
9298 If this is done during an update, updated_window will contain the
9299 window that is being updated and the position is the future output
9300 cursor position for that window. If updated_window is null, use
9301 selected_window and display the cursor at the given position. */
9302
9303 void
9304 x_cursor_to (vpos, hpos, y, x)
9305 int vpos, hpos, y, x;
9306 {
9307 struct window *w;
9308
9309 /* If updated_window is not set, work on selected_window. */
9310 if (updated_window)
9311 w = updated_window;
9312 else
9313 w = XWINDOW (selected_window);
9314
9315 /* Set the output cursor. */
9316 output_cursor.hpos = hpos;
9317 output_cursor.vpos = vpos;
9318 output_cursor.x = x;
9319 output_cursor.y = y;
9320
9321 /* If not called as part of an update, really display the cursor.
9322 This will also set the cursor position of W. */
9323 if (updated_window == NULL)
9324 {
9325 BLOCK_INPUT;
9326 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9327 if (rif->flush_display_optional)
9328 rif->flush_display_optional (SELECTED_FRAME ());
9329 UNBLOCK_INPUT;
9330 }
9331 }
9332
9333 #endif /* HAVE_WINDOW_SYSTEM */
9334
9335 \f
9336 /***********************************************************************
9337 Tool-bars
9338 ***********************************************************************/
9339
9340 #ifdef HAVE_WINDOW_SYSTEM
9341
9342 /* Where the mouse was last time we reported a mouse event. */
9343
9344 FRAME_PTR last_mouse_frame;
9345
9346 /* Tool-bar item index of the item on which a mouse button was pressed
9347 or -1. */
9348
9349 int last_tool_bar_item;
9350
9351
9352 /* Update the tool-bar item list for frame F. This has to be done
9353 before we start to fill in any display lines. Called from
9354 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9355 and restore it here. */
9356
9357 static void
9358 update_tool_bar (f, save_match_data)
9359 struct frame *f;
9360 int save_match_data;
9361 {
9362 #ifdef USE_GTK
9363 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9364 #else
9365 int do_update = WINDOWP (f->tool_bar_window)
9366 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9367 #endif
9368
9369 if (do_update)
9370 {
9371 Lisp_Object window;
9372 struct window *w;
9373
9374 window = FRAME_SELECTED_WINDOW (f);
9375 w = XWINDOW (window);
9376
9377 /* If the user has switched buffers or windows, we need to
9378 recompute to reflect the new bindings. But we'll
9379 recompute when update_mode_lines is set too; that means
9380 that people can use force-mode-line-update to request
9381 that the menu bar be recomputed. The adverse effect on
9382 the rest of the redisplay algorithm is about the same as
9383 windows_or_buffers_changed anyway. */
9384 if (windows_or_buffers_changed
9385 || !NILP (w->update_mode_line)
9386 || update_mode_lines
9387 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9388 < BUF_MODIFF (XBUFFER (w->buffer)))
9389 != !NILP (w->last_had_star))
9390 || ((!NILP (Vtransient_mark_mode)
9391 && !NILP (XBUFFER (w->buffer)->mark_active))
9392 != !NILP (w->region_showing)))
9393 {
9394 struct buffer *prev = current_buffer;
9395 int count = SPECPDL_INDEX ();
9396 Lisp_Object new_tool_bar;
9397 int new_n_tool_bar;
9398 struct gcpro gcpro1;
9399
9400 /* Set current_buffer to the buffer of the selected
9401 window of the frame, so that we get the right local
9402 keymaps. */
9403 set_buffer_internal_1 (XBUFFER (w->buffer));
9404
9405 /* Save match data, if we must. */
9406 if (save_match_data)
9407 record_unwind_save_match_data ();
9408
9409 /* Make sure that we don't accidentally use bogus keymaps. */
9410 if (NILP (Voverriding_local_map_menu_flag))
9411 {
9412 specbind (Qoverriding_terminal_local_map, Qnil);
9413 specbind (Qoverriding_local_map, Qnil);
9414 }
9415
9416 GCPRO1 (new_tool_bar);
9417
9418 /* Build desired tool-bar items from keymaps. */
9419 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9420 &new_n_tool_bar);
9421
9422 /* Redisplay the tool-bar if we changed it. */
9423 if (new_n_tool_bar != f->n_tool_bar_items
9424 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9425 {
9426 /* Redisplay that happens asynchronously due to an expose event
9427 may access f->tool_bar_items. Make sure we update both
9428 variables within BLOCK_INPUT so no such event interrupts. */
9429 BLOCK_INPUT;
9430 f->tool_bar_items = new_tool_bar;
9431 f->n_tool_bar_items = new_n_tool_bar;
9432 w->update_mode_line = Qt;
9433 UNBLOCK_INPUT;
9434 }
9435
9436 UNGCPRO;
9437
9438 unbind_to (count, Qnil);
9439 set_buffer_internal_1 (prev);
9440 }
9441 }
9442 }
9443
9444
9445 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9446 F's desired tool-bar contents. F->tool_bar_items must have
9447 been set up previously by calling prepare_menu_bars. */
9448
9449 static void
9450 build_desired_tool_bar_string (f)
9451 struct frame *f;
9452 {
9453 int i, size, size_needed;
9454 struct gcpro gcpro1, gcpro2, gcpro3;
9455 Lisp_Object image, plist, props;
9456
9457 image = plist = props = Qnil;
9458 GCPRO3 (image, plist, props);
9459
9460 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9461 Otherwise, make a new string. */
9462
9463 /* The size of the string we might be able to reuse. */
9464 size = (STRINGP (f->desired_tool_bar_string)
9465 ? SCHARS (f->desired_tool_bar_string)
9466 : 0);
9467
9468 /* We need one space in the string for each image. */
9469 size_needed = f->n_tool_bar_items;
9470
9471 /* Reuse f->desired_tool_bar_string, if possible. */
9472 if (size < size_needed || NILP (f->desired_tool_bar_string))
9473 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9474 make_number (' '));
9475 else
9476 {
9477 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9478 Fremove_text_properties (make_number (0), make_number (size),
9479 props, f->desired_tool_bar_string);
9480 }
9481
9482 /* Put a `display' property on the string for the images to display,
9483 put a `menu_item' property on tool-bar items with a value that
9484 is the index of the item in F's tool-bar item vector. */
9485 for (i = 0; i < f->n_tool_bar_items; ++i)
9486 {
9487 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9488
9489 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9490 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9491 int hmargin, vmargin, relief, idx, end;
9492 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9493
9494 /* If image is a vector, choose the image according to the
9495 button state. */
9496 image = PROP (TOOL_BAR_ITEM_IMAGES);
9497 if (VECTORP (image))
9498 {
9499 if (enabled_p)
9500 idx = (selected_p
9501 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9502 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9503 else
9504 idx = (selected_p
9505 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9506 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9507
9508 xassert (ASIZE (image) >= idx);
9509 image = AREF (image, idx);
9510 }
9511 else
9512 idx = -1;
9513
9514 /* Ignore invalid image specifications. */
9515 if (!valid_image_p (image))
9516 continue;
9517
9518 /* Display the tool-bar button pressed, or depressed. */
9519 plist = Fcopy_sequence (XCDR (image));
9520
9521 /* Compute margin and relief to draw. */
9522 relief = (tool_bar_button_relief >= 0
9523 ? tool_bar_button_relief
9524 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9525 hmargin = vmargin = relief;
9526
9527 if (INTEGERP (Vtool_bar_button_margin)
9528 && XINT (Vtool_bar_button_margin) > 0)
9529 {
9530 hmargin += XFASTINT (Vtool_bar_button_margin);
9531 vmargin += XFASTINT (Vtool_bar_button_margin);
9532 }
9533 else if (CONSP (Vtool_bar_button_margin))
9534 {
9535 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9536 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9537 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9538
9539 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9540 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9541 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9542 }
9543
9544 if (auto_raise_tool_bar_buttons_p)
9545 {
9546 /* Add a `:relief' property to the image spec if the item is
9547 selected. */
9548 if (selected_p)
9549 {
9550 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9551 hmargin -= relief;
9552 vmargin -= relief;
9553 }
9554 }
9555 else
9556 {
9557 /* If image is selected, display it pressed, i.e. with a
9558 negative relief. If it's not selected, display it with a
9559 raised relief. */
9560 plist = Fplist_put (plist, QCrelief,
9561 (selected_p
9562 ? make_number (-relief)
9563 : make_number (relief)));
9564 hmargin -= relief;
9565 vmargin -= relief;
9566 }
9567
9568 /* Put a margin around the image. */
9569 if (hmargin || vmargin)
9570 {
9571 if (hmargin == vmargin)
9572 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9573 else
9574 plist = Fplist_put (plist, QCmargin,
9575 Fcons (make_number (hmargin),
9576 make_number (vmargin)));
9577 }
9578
9579 /* If button is not enabled, and we don't have special images
9580 for the disabled state, make the image appear disabled by
9581 applying an appropriate algorithm to it. */
9582 if (!enabled_p && idx < 0)
9583 plist = Fplist_put (plist, QCconversion, Qdisabled);
9584
9585 /* Put a `display' text property on the string for the image to
9586 display. Put a `menu-item' property on the string that gives
9587 the start of this item's properties in the tool-bar items
9588 vector. */
9589 image = Fcons (Qimage, plist);
9590 props = list4 (Qdisplay, image,
9591 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9592
9593 /* Let the last image hide all remaining spaces in the tool bar
9594 string. The string can be longer than needed when we reuse a
9595 previous string. */
9596 if (i + 1 == f->n_tool_bar_items)
9597 end = SCHARS (f->desired_tool_bar_string);
9598 else
9599 end = i + 1;
9600 Fadd_text_properties (make_number (i), make_number (end),
9601 props, f->desired_tool_bar_string);
9602 #undef PROP
9603 }
9604
9605 UNGCPRO;
9606 }
9607
9608
9609 /* Display one line of the tool-bar of frame IT->f.
9610
9611 HEIGHT specifies the desired height of the tool-bar line.
9612 If the actual height of the glyph row is less than HEIGHT, the
9613 row's height is increased to HEIGHT, and the icons are centered
9614 vertically in the new height.
9615
9616 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9617 count a final empty row in case the tool-bar width exactly matches
9618 the window width.
9619 */
9620
9621 static void
9622 display_tool_bar_line (it, height)
9623 struct it *it;
9624 int height;
9625 {
9626 struct glyph_row *row = it->glyph_row;
9627 int max_x = it->last_visible_x;
9628 struct glyph *last;
9629
9630 prepare_desired_row (row);
9631 row->y = it->current_y;
9632
9633 /* Note that this isn't made use of if the face hasn't a box,
9634 so there's no need to check the face here. */
9635 it->start_of_box_run_p = 1;
9636
9637 while (it->current_x < max_x)
9638 {
9639 int x, n_glyphs_before, i, nglyphs;
9640 struct it it_before;
9641
9642 /* Get the next display element. */
9643 if (!get_next_display_element (it))
9644 {
9645 /* Don't count empty row if we are counting needed tool-bar lines. */
9646 if (height < 0 && !it->hpos)
9647 return;
9648 break;
9649 }
9650
9651 /* Produce glyphs. */
9652 n_glyphs_before = row->used[TEXT_AREA];
9653 it_before = *it;
9654
9655 PRODUCE_GLYPHS (it);
9656
9657 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9658 i = 0;
9659 x = it_before.current_x;
9660 while (i < nglyphs)
9661 {
9662 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9663
9664 if (x + glyph->pixel_width > max_x)
9665 {
9666 /* Glyph doesn't fit on line. Backtrack. */
9667 row->used[TEXT_AREA] = n_glyphs_before;
9668 *it = it_before;
9669 /* If this is the only glyph on this line, it will never fit on the
9670 toolbar, so skip it. But ensure there is at least one glyph,
9671 so we don't accidentally disable the tool-bar. */
9672 if (n_glyphs_before == 0
9673 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9674 break;
9675 goto out;
9676 }
9677
9678 ++it->hpos;
9679 x += glyph->pixel_width;
9680 ++i;
9681 }
9682
9683 /* Stop at line ends. */
9684 if (ITERATOR_AT_END_OF_LINE_P (it))
9685 break;
9686
9687 set_iterator_to_next (it, 1);
9688 }
9689
9690 out:;
9691
9692 row->displays_text_p = row->used[TEXT_AREA] != 0;
9693 /* Use default face for the border below the tool bar. */
9694 if (!row->displays_text_p)
9695 it->face_id = DEFAULT_FACE_ID;
9696 extend_face_to_end_of_line (it);
9697 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9698 last->right_box_line_p = 1;
9699 if (last == row->glyphs[TEXT_AREA])
9700 last->left_box_line_p = 1;
9701
9702 /* Make line the desired height and center it vertically. */
9703 if ((height -= it->max_ascent + it->max_descent) > 0)
9704 {
9705 /* Don't add more than one line height. */
9706 height %= FRAME_LINE_HEIGHT (it->f);
9707 it->max_ascent += height / 2;
9708 it->max_descent += (height + 1) / 2;
9709 }
9710
9711 compute_line_metrics (it);
9712
9713 /* If line is empty, make it occupy the rest of the tool-bar. */
9714 if (!row->displays_text_p)
9715 {
9716 row->height = row->phys_height = it->last_visible_y - row->y;
9717 row->ascent = row->phys_ascent = 0;
9718 row->extra_line_spacing = 0;
9719 }
9720
9721 row->full_width_p = 1;
9722 row->continued_p = 0;
9723 row->truncated_on_left_p = 0;
9724 row->truncated_on_right_p = 0;
9725
9726 it->current_x = it->hpos = 0;
9727 it->current_y += row->height;
9728 ++it->vpos;
9729 ++it->glyph_row;
9730 }
9731
9732
9733 /* Max tool-bar height. */
9734
9735 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9736 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9737
9738 /* Value is the number of screen lines needed to make all tool-bar
9739 items of frame F visible. The number of actual rows needed is
9740 returned in *N_ROWS if non-NULL. */
9741
9742 static int
9743 tool_bar_lines_needed (f, n_rows)
9744 struct frame *f;
9745 int *n_rows;
9746 {
9747 struct window *w = XWINDOW (f->tool_bar_window);
9748 struct it it;
9749 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9750 the desired matrix, so use (unused) mode-line row as temporary row to
9751 avoid destroying the first tool-bar row. */
9752 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9753
9754 /* Initialize an iterator for iteration over
9755 F->desired_tool_bar_string in the tool-bar window of frame F. */
9756 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9757 it.first_visible_x = 0;
9758 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9759 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9760
9761 while (!ITERATOR_AT_END_P (&it))
9762 {
9763 clear_glyph_row (temp_row);
9764 it.glyph_row = temp_row;
9765 display_tool_bar_line (&it, -1);
9766 }
9767 clear_glyph_row (temp_row);
9768
9769 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9770 if (n_rows)
9771 *n_rows = it.vpos > 0 ? it.vpos : -1;
9772
9773 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9774 }
9775
9776
9777 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9778 0, 1, 0,
9779 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9780 (frame)
9781 Lisp_Object frame;
9782 {
9783 struct frame *f;
9784 struct window *w;
9785 int nlines = 0;
9786
9787 if (NILP (frame))
9788 frame = selected_frame;
9789 else
9790 CHECK_FRAME (frame);
9791 f = XFRAME (frame);
9792
9793 if (WINDOWP (f->tool_bar_window)
9794 || (w = XWINDOW (f->tool_bar_window),
9795 WINDOW_TOTAL_LINES (w) > 0))
9796 {
9797 update_tool_bar (f, 1);
9798 if (f->n_tool_bar_items)
9799 {
9800 build_desired_tool_bar_string (f);
9801 nlines = tool_bar_lines_needed (f, NULL);
9802 }
9803 }
9804
9805 return make_number (nlines);
9806 }
9807
9808
9809 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9810 height should be changed. */
9811
9812 static int
9813 redisplay_tool_bar (f)
9814 struct frame *f;
9815 {
9816 struct window *w;
9817 struct it it;
9818 struct glyph_row *row;
9819 int change_height_p = 0;
9820
9821 #ifdef USE_GTK
9822 if (FRAME_EXTERNAL_TOOL_BAR (f))
9823 update_frame_tool_bar (f);
9824 return 0;
9825 #endif
9826
9827 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9828 do anything. This means you must start with tool-bar-lines
9829 non-zero to get the auto-sizing effect. Or in other words, you
9830 can turn off tool-bars by specifying tool-bar-lines zero. */
9831 if (!WINDOWP (f->tool_bar_window)
9832 || (w = XWINDOW (f->tool_bar_window),
9833 WINDOW_TOTAL_LINES (w) == 0))
9834 return 0;
9835
9836 /* Set up an iterator for the tool-bar window. */
9837 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9838 it.first_visible_x = 0;
9839 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9840 row = it.glyph_row;
9841
9842 /* Build a string that represents the contents of the tool-bar. */
9843 build_desired_tool_bar_string (f);
9844 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9845
9846 if (f->n_tool_bar_rows == 0)
9847 {
9848 int nlines;
9849
9850 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9851 nlines != WINDOW_TOTAL_LINES (w)))
9852 {
9853 extern Lisp_Object Qtool_bar_lines;
9854 Lisp_Object frame;
9855 int old_height = WINDOW_TOTAL_LINES (w);
9856
9857 XSETFRAME (frame, f);
9858 Fmodify_frame_parameters (frame,
9859 Fcons (Fcons (Qtool_bar_lines,
9860 make_number (nlines)),
9861 Qnil));
9862 if (WINDOW_TOTAL_LINES (w) != old_height)
9863 {
9864 clear_glyph_matrix (w->desired_matrix);
9865 fonts_changed_p = 1;
9866 return 1;
9867 }
9868 }
9869 }
9870
9871 /* Display as many lines as needed to display all tool-bar items. */
9872
9873 if (f->n_tool_bar_rows > 0)
9874 {
9875 int border, rows, height, extra;
9876
9877 if (INTEGERP (Vtool_bar_border))
9878 border = XINT (Vtool_bar_border);
9879 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9880 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9881 else if (EQ (Vtool_bar_border, Qborder_width))
9882 border = f->border_width;
9883 else
9884 border = 0;
9885 if (border < 0)
9886 border = 0;
9887
9888 rows = f->n_tool_bar_rows;
9889 height = max (1, (it.last_visible_y - border) / rows);
9890 extra = it.last_visible_y - border - height * rows;
9891
9892 while (it.current_y < it.last_visible_y)
9893 {
9894 int h = 0;
9895 if (extra > 0 && rows-- > 0)
9896 {
9897 h = (extra + rows - 1) / rows;
9898 extra -= h;
9899 }
9900 display_tool_bar_line (&it, height + h);
9901 }
9902 }
9903 else
9904 {
9905 while (it.current_y < it.last_visible_y)
9906 display_tool_bar_line (&it, 0);
9907 }
9908
9909 /* It doesn't make much sense to try scrolling in the tool-bar
9910 window, so don't do it. */
9911 w->desired_matrix->no_scrolling_p = 1;
9912 w->must_be_updated_p = 1;
9913
9914 if (auto_resize_tool_bars_p)
9915 {
9916 int nlines, nrows;
9917 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9918
9919 /* If we couldn't display everything, change the tool-bar's
9920 height if there is room for more. */
9921 if (IT_STRING_CHARPOS (it) < it.end_charpos
9922 && it.current_y < max_tool_bar_height)
9923 change_height_p = 1;
9924
9925 row = it.glyph_row - 1;
9926
9927 /* If there are blank lines at the end, except for a partially
9928 visible blank line at the end that is smaller than
9929 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9930 if (!row->displays_text_p
9931 && row->height >= FRAME_LINE_HEIGHT (f))
9932 change_height_p = 1;
9933
9934 /* If row displays tool-bar items, but is partially visible,
9935 change the tool-bar's height. */
9936 if (row->displays_text_p
9937 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9938 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9939 change_height_p = 1;
9940
9941 /* Resize windows as needed by changing the `tool-bar-lines'
9942 frame parameter. */
9943 if (change_height_p
9944 && (nlines = tool_bar_lines_needed (f, &nrows),
9945 nlines != WINDOW_TOTAL_LINES (w)))
9946 {
9947 extern Lisp_Object Qtool_bar_lines;
9948 Lisp_Object frame;
9949 int old_height = WINDOW_TOTAL_LINES (w);
9950
9951 XSETFRAME (frame, f);
9952 Fmodify_frame_parameters (frame,
9953 Fcons (Fcons (Qtool_bar_lines,
9954 make_number (nlines)),
9955 Qnil));
9956 if (WINDOW_TOTAL_LINES (w) != old_height)
9957 {
9958 clear_glyph_matrix (w->desired_matrix);
9959 f->n_tool_bar_rows = nrows;
9960 fonts_changed_p = 1;
9961 }
9962 }
9963 }
9964
9965 return change_height_p;
9966 }
9967
9968
9969 /* Get information about the tool-bar item which is displayed in GLYPH
9970 on frame F. Return in *PROP_IDX the index where tool-bar item
9971 properties start in F->tool_bar_items. Value is zero if
9972 GLYPH doesn't display a tool-bar item. */
9973
9974 static int
9975 tool_bar_item_info (f, glyph, prop_idx)
9976 struct frame *f;
9977 struct glyph *glyph;
9978 int *prop_idx;
9979 {
9980 Lisp_Object prop;
9981 int success_p;
9982 int charpos;
9983
9984 /* This function can be called asynchronously, which means we must
9985 exclude any possibility that Fget_text_property signals an
9986 error. */
9987 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9988 charpos = max (0, charpos);
9989
9990 /* Get the text property `menu-item' at pos. The value of that
9991 property is the start index of this item's properties in
9992 F->tool_bar_items. */
9993 prop = Fget_text_property (make_number (charpos),
9994 Qmenu_item, f->current_tool_bar_string);
9995 if (INTEGERP (prop))
9996 {
9997 *prop_idx = XINT (prop);
9998 success_p = 1;
9999 }
10000 else
10001 success_p = 0;
10002
10003 return success_p;
10004 }
10005
10006 \f
10007 /* Get information about the tool-bar item at position X/Y on frame F.
10008 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10009 the current matrix of the tool-bar window of F, or NULL if not
10010 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10011 item in F->tool_bar_items. Value is
10012
10013 -1 if X/Y is not on a tool-bar item
10014 0 if X/Y is on the same item that was highlighted before.
10015 1 otherwise. */
10016
10017 static int
10018 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10019 struct frame *f;
10020 int x, y;
10021 struct glyph **glyph;
10022 int *hpos, *vpos, *prop_idx;
10023 {
10024 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10025 struct window *w = XWINDOW (f->tool_bar_window);
10026 int area;
10027
10028 /* Find the glyph under X/Y. */
10029 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10030 if (*glyph == NULL)
10031 return -1;
10032
10033 /* Get the start of this tool-bar item's properties in
10034 f->tool_bar_items. */
10035 if (!tool_bar_item_info (f, *glyph, prop_idx))
10036 return -1;
10037
10038 /* Is mouse on the highlighted item? */
10039 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10040 && *vpos >= dpyinfo->mouse_face_beg_row
10041 && *vpos <= dpyinfo->mouse_face_end_row
10042 && (*vpos > dpyinfo->mouse_face_beg_row
10043 || *hpos >= dpyinfo->mouse_face_beg_col)
10044 && (*vpos < dpyinfo->mouse_face_end_row
10045 || *hpos < dpyinfo->mouse_face_end_col
10046 || dpyinfo->mouse_face_past_end))
10047 return 0;
10048
10049 return 1;
10050 }
10051
10052
10053 /* EXPORT:
10054 Handle mouse button event on the tool-bar of frame F, at
10055 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10056 0 for button release. MODIFIERS is event modifiers for button
10057 release. */
10058
10059 void
10060 handle_tool_bar_click (f, x, y, down_p, modifiers)
10061 struct frame *f;
10062 int x, y, down_p;
10063 unsigned int modifiers;
10064 {
10065 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10066 struct window *w = XWINDOW (f->tool_bar_window);
10067 int hpos, vpos, prop_idx;
10068 struct glyph *glyph;
10069 Lisp_Object enabled_p;
10070
10071 /* If not on the highlighted tool-bar item, return. */
10072 frame_to_window_pixel_xy (w, &x, &y);
10073 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10074 return;
10075
10076 /* If item is disabled, do nothing. */
10077 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10078 if (NILP (enabled_p))
10079 return;
10080
10081 if (down_p)
10082 {
10083 /* Show item in pressed state. */
10084 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10085 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10086 last_tool_bar_item = prop_idx;
10087 }
10088 else
10089 {
10090 Lisp_Object key, frame;
10091 struct input_event event;
10092 EVENT_INIT (event);
10093
10094 /* Show item in released state. */
10095 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10096 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10097
10098 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10099
10100 XSETFRAME (frame, f);
10101 event.kind = TOOL_BAR_EVENT;
10102 event.frame_or_window = frame;
10103 event.arg = frame;
10104 kbd_buffer_store_event (&event);
10105
10106 event.kind = TOOL_BAR_EVENT;
10107 event.frame_or_window = frame;
10108 event.arg = key;
10109 event.modifiers = modifiers;
10110 kbd_buffer_store_event (&event);
10111 last_tool_bar_item = -1;
10112 }
10113 }
10114
10115
10116 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10117 tool-bar window-relative coordinates X/Y. Called from
10118 note_mouse_highlight. */
10119
10120 static void
10121 note_tool_bar_highlight (f, x, y)
10122 struct frame *f;
10123 int x, y;
10124 {
10125 Lisp_Object window = f->tool_bar_window;
10126 struct window *w = XWINDOW (window);
10127 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10128 int hpos, vpos;
10129 struct glyph *glyph;
10130 struct glyph_row *row;
10131 int i;
10132 Lisp_Object enabled_p;
10133 int prop_idx;
10134 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10135 int mouse_down_p, rc;
10136
10137 /* Function note_mouse_highlight is called with negative x(y
10138 values when mouse moves outside of the frame. */
10139 if (x <= 0 || y <= 0)
10140 {
10141 clear_mouse_face (dpyinfo);
10142 return;
10143 }
10144
10145 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10146 if (rc < 0)
10147 {
10148 /* Not on tool-bar item. */
10149 clear_mouse_face (dpyinfo);
10150 return;
10151 }
10152 else if (rc == 0)
10153 /* On same tool-bar item as before. */
10154 goto set_help_echo;
10155
10156 clear_mouse_face (dpyinfo);
10157
10158 /* Mouse is down, but on different tool-bar item? */
10159 mouse_down_p = (dpyinfo->grabbed
10160 && f == last_mouse_frame
10161 && FRAME_LIVE_P (f));
10162 if (mouse_down_p
10163 && last_tool_bar_item != prop_idx)
10164 return;
10165
10166 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10167 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10168
10169 /* If tool-bar item is not enabled, don't highlight it. */
10170 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10171 if (!NILP (enabled_p))
10172 {
10173 /* Compute the x-position of the glyph. In front and past the
10174 image is a space. We include this in the highlighted area. */
10175 row = MATRIX_ROW (w->current_matrix, vpos);
10176 for (i = x = 0; i < hpos; ++i)
10177 x += row->glyphs[TEXT_AREA][i].pixel_width;
10178
10179 /* Record this as the current active region. */
10180 dpyinfo->mouse_face_beg_col = hpos;
10181 dpyinfo->mouse_face_beg_row = vpos;
10182 dpyinfo->mouse_face_beg_x = x;
10183 dpyinfo->mouse_face_beg_y = row->y;
10184 dpyinfo->mouse_face_past_end = 0;
10185
10186 dpyinfo->mouse_face_end_col = hpos + 1;
10187 dpyinfo->mouse_face_end_row = vpos;
10188 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10189 dpyinfo->mouse_face_end_y = row->y;
10190 dpyinfo->mouse_face_window = window;
10191 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10192
10193 /* Display it as active. */
10194 show_mouse_face (dpyinfo, draw);
10195 dpyinfo->mouse_face_image_state = draw;
10196 }
10197
10198 set_help_echo:
10199
10200 /* Set help_echo_string to a help string to display for this tool-bar item.
10201 XTread_socket does the rest. */
10202 help_echo_object = help_echo_window = Qnil;
10203 help_echo_pos = -1;
10204 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10205 if (NILP (help_echo_string))
10206 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10207 }
10208
10209 #endif /* HAVE_WINDOW_SYSTEM */
10210
10211
10212 \f
10213 /************************************************************************
10214 Horizontal scrolling
10215 ************************************************************************/
10216
10217 static int hscroll_window_tree P_ ((Lisp_Object));
10218 static int hscroll_windows P_ ((Lisp_Object));
10219
10220 /* For all leaf windows in the window tree rooted at WINDOW, set their
10221 hscroll value so that PT is (i) visible in the window, and (ii) so
10222 that it is not within a certain margin at the window's left and
10223 right border. Value is non-zero if any window's hscroll has been
10224 changed. */
10225
10226 static int
10227 hscroll_window_tree (window)
10228 Lisp_Object window;
10229 {
10230 int hscrolled_p = 0;
10231 int hscroll_relative_p = FLOATP (Vhscroll_step);
10232 int hscroll_step_abs = 0;
10233 double hscroll_step_rel = 0;
10234
10235 if (hscroll_relative_p)
10236 {
10237 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10238 if (hscroll_step_rel < 0)
10239 {
10240 hscroll_relative_p = 0;
10241 hscroll_step_abs = 0;
10242 }
10243 }
10244 else if (INTEGERP (Vhscroll_step))
10245 {
10246 hscroll_step_abs = XINT (Vhscroll_step);
10247 if (hscroll_step_abs < 0)
10248 hscroll_step_abs = 0;
10249 }
10250 else
10251 hscroll_step_abs = 0;
10252
10253 while (WINDOWP (window))
10254 {
10255 struct window *w = XWINDOW (window);
10256
10257 if (WINDOWP (w->hchild))
10258 hscrolled_p |= hscroll_window_tree (w->hchild);
10259 else if (WINDOWP (w->vchild))
10260 hscrolled_p |= hscroll_window_tree (w->vchild);
10261 else if (w->cursor.vpos >= 0)
10262 {
10263 int h_margin;
10264 int text_area_width;
10265 struct glyph_row *current_cursor_row
10266 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10267 struct glyph_row *desired_cursor_row
10268 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10269 struct glyph_row *cursor_row
10270 = (desired_cursor_row->enabled_p
10271 ? desired_cursor_row
10272 : current_cursor_row);
10273
10274 text_area_width = window_box_width (w, TEXT_AREA);
10275
10276 /* Scroll when cursor is inside this scroll margin. */
10277 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10278
10279 if ((XFASTINT (w->hscroll)
10280 && w->cursor.x <= h_margin)
10281 || (cursor_row->enabled_p
10282 && cursor_row->truncated_on_right_p
10283 && (w->cursor.x >= text_area_width - h_margin)))
10284 {
10285 struct it it;
10286 int hscroll;
10287 struct buffer *saved_current_buffer;
10288 int pt;
10289 int wanted_x;
10290
10291 /* Find point in a display of infinite width. */
10292 saved_current_buffer = current_buffer;
10293 current_buffer = XBUFFER (w->buffer);
10294
10295 if (w == XWINDOW (selected_window))
10296 pt = BUF_PT (current_buffer);
10297 else
10298 {
10299 pt = marker_position (w->pointm);
10300 pt = max (BEGV, pt);
10301 pt = min (ZV, pt);
10302 }
10303
10304 /* Move iterator to pt starting at cursor_row->start in
10305 a line with infinite width. */
10306 init_to_row_start (&it, w, cursor_row);
10307 it.last_visible_x = INFINITY;
10308 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10309 current_buffer = saved_current_buffer;
10310
10311 /* Position cursor in window. */
10312 if (!hscroll_relative_p && hscroll_step_abs == 0)
10313 hscroll = max (0, (it.current_x
10314 - (ITERATOR_AT_END_OF_LINE_P (&it)
10315 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10316 : (text_area_width / 2))))
10317 / FRAME_COLUMN_WIDTH (it.f);
10318 else if (w->cursor.x >= text_area_width - h_margin)
10319 {
10320 if (hscroll_relative_p)
10321 wanted_x = text_area_width * (1 - hscroll_step_rel)
10322 - h_margin;
10323 else
10324 wanted_x = text_area_width
10325 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10326 - h_margin;
10327 hscroll
10328 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10329 }
10330 else
10331 {
10332 if (hscroll_relative_p)
10333 wanted_x = text_area_width * hscroll_step_rel
10334 + h_margin;
10335 else
10336 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10337 + h_margin;
10338 hscroll
10339 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10340 }
10341 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10342
10343 /* Don't call Fset_window_hscroll if value hasn't
10344 changed because it will prevent redisplay
10345 optimizations. */
10346 if (XFASTINT (w->hscroll) != hscroll)
10347 {
10348 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10349 w->hscroll = make_number (hscroll);
10350 hscrolled_p = 1;
10351 }
10352 }
10353 }
10354
10355 window = w->next;
10356 }
10357
10358 /* Value is non-zero if hscroll of any leaf window has been changed. */
10359 return hscrolled_p;
10360 }
10361
10362
10363 /* Set hscroll so that cursor is visible and not inside horizontal
10364 scroll margins for all windows in the tree rooted at WINDOW. See
10365 also hscroll_window_tree above. Value is non-zero if any window's
10366 hscroll has been changed. If it has, desired matrices on the frame
10367 of WINDOW are cleared. */
10368
10369 static int
10370 hscroll_windows (window)
10371 Lisp_Object window;
10372 {
10373 int hscrolled_p;
10374
10375 if (automatic_hscrolling_p)
10376 {
10377 hscrolled_p = hscroll_window_tree (window);
10378 if (hscrolled_p)
10379 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10380 }
10381 else
10382 hscrolled_p = 0;
10383 return hscrolled_p;
10384 }
10385
10386
10387 \f
10388 /************************************************************************
10389 Redisplay
10390 ************************************************************************/
10391
10392 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10393 to a non-zero value. This is sometimes handy to have in a debugger
10394 session. */
10395
10396 #if GLYPH_DEBUG
10397
10398 /* First and last unchanged row for try_window_id. */
10399
10400 int debug_first_unchanged_at_end_vpos;
10401 int debug_last_unchanged_at_beg_vpos;
10402
10403 /* Delta vpos and y. */
10404
10405 int debug_dvpos, debug_dy;
10406
10407 /* Delta in characters and bytes for try_window_id. */
10408
10409 int debug_delta, debug_delta_bytes;
10410
10411 /* Values of window_end_pos and window_end_vpos at the end of
10412 try_window_id. */
10413
10414 EMACS_INT debug_end_pos, debug_end_vpos;
10415
10416 /* Append a string to W->desired_matrix->method. FMT is a printf
10417 format string. A1...A9 are a supplement for a variable-length
10418 argument list. If trace_redisplay_p is non-zero also printf the
10419 resulting string to stderr. */
10420
10421 static void
10422 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10423 struct window *w;
10424 char *fmt;
10425 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10426 {
10427 char buffer[512];
10428 char *method = w->desired_matrix->method;
10429 int len = strlen (method);
10430 int size = sizeof w->desired_matrix->method;
10431 int remaining = size - len - 1;
10432
10433 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10434 if (len && remaining)
10435 {
10436 method[len] = '|';
10437 --remaining, ++len;
10438 }
10439
10440 strncpy (method + len, buffer, remaining);
10441
10442 if (trace_redisplay_p)
10443 fprintf (stderr, "%p (%s): %s\n",
10444 w,
10445 ((BUFFERP (w->buffer)
10446 && STRINGP (XBUFFER (w->buffer)->name))
10447 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10448 : "no buffer"),
10449 buffer);
10450 }
10451
10452 #endif /* GLYPH_DEBUG */
10453
10454
10455 /* Value is non-zero if all changes in window W, which displays
10456 current_buffer, are in the text between START and END. START is a
10457 buffer position, END is given as a distance from Z. Used in
10458 redisplay_internal for display optimization. */
10459
10460 static INLINE int
10461 text_outside_line_unchanged_p (w, start, end)
10462 struct window *w;
10463 int start, end;
10464 {
10465 int unchanged_p = 1;
10466
10467 /* If text or overlays have changed, see where. */
10468 if (XFASTINT (w->last_modified) < MODIFF
10469 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10470 {
10471 /* Gap in the line? */
10472 if (GPT < start || Z - GPT < end)
10473 unchanged_p = 0;
10474
10475 /* Changes start in front of the line, or end after it? */
10476 if (unchanged_p
10477 && (BEG_UNCHANGED < start - 1
10478 || END_UNCHANGED < end))
10479 unchanged_p = 0;
10480
10481 /* If selective display, can't optimize if changes start at the
10482 beginning of the line. */
10483 if (unchanged_p
10484 && INTEGERP (current_buffer->selective_display)
10485 && XINT (current_buffer->selective_display) > 0
10486 && (BEG_UNCHANGED < start || GPT <= start))
10487 unchanged_p = 0;
10488
10489 /* If there are overlays at the start or end of the line, these
10490 may have overlay strings with newlines in them. A change at
10491 START, for instance, may actually concern the display of such
10492 overlay strings as well, and they are displayed on different
10493 lines. So, quickly rule out this case. (For the future, it
10494 might be desirable to implement something more telling than
10495 just BEG/END_UNCHANGED.) */
10496 if (unchanged_p)
10497 {
10498 if (BEG + BEG_UNCHANGED == start
10499 && overlay_touches_p (start))
10500 unchanged_p = 0;
10501 if (END_UNCHANGED == end
10502 && overlay_touches_p (Z - end))
10503 unchanged_p = 0;
10504 }
10505 }
10506
10507 return unchanged_p;
10508 }
10509
10510
10511 /* Do a frame update, taking possible shortcuts into account. This is
10512 the main external entry point for redisplay.
10513
10514 If the last redisplay displayed an echo area message and that message
10515 is no longer requested, we clear the echo area or bring back the
10516 mini-buffer if that is in use. */
10517
10518 void
10519 redisplay ()
10520 {
10521 redisplay_internal (0);
10522 }
10523
10524
10525 static Lisp_Object
10526 overlay_arrow_string_or_property (var)
10527 Lisp_Object var;
10528 {
10529 Lisp_Object val;
10530
10531 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10532 return val;
10533
10534 return Voverlay_arrow_string;
10535 }
10536
10537 /* Return 1 if there are any overlay-arrows in current_buffer. */
10538 static int
10539 overlay_arrow_in_current_buffer_p ()
10540 {
10541 Lisp_Object vlist;
10542
10543 for (vlist = Voverlay_arrow_variable_list;
10544 CONSP (vlist);
10545 vlist = XCDR (vlist))
10546 {
10547 Lisp_Object var = XCAR (vlist);
10548 Lisp_Object val;
10549
10550 if (!SYMBOLP (var))
10551 continue;
10552 val = find_symbol_value (var);
10553 if (MARKERP (val)
10554 && current_buffer == XMARKER (val)->buffer)
10555 return 1;
10556 }
10557 return 0;
10558 }
10559
10560
10561 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10562 has changed. */
10563
10564 static int
10565 overlay_arrows_changed_p ()
10566 {
10567 Lisp_Object vlist;
10568
10569 for (vlist = Voverlay_arrow_variable_list;
10570 CONSP (vlist);
10571 vlist = XCDR (vlist))
10572 {
10573 Lisp_Object var = XCAR (vlist);
10574 Lisp_Object val, pstr;
10575
10576 if (!SYMBOLP (var))
10577 continue;
10578 val = find_symbol_value (var);
10579 if (!MARKERP (val))
10580 continue;
10581 if (! EQ (COERCE_MARKER (val),
10582 Fget (var, Qlast_arrow_position))
10583 || ! (pstr = overlay_arrow_string_or_property (var),
10584 EQ (pstr, Fget (var, Qlast_arrow_string))))
10585 return 1;
10586 }
10587 return 0;
10588 }
10589
10590 /* Mark overlay arrows to be updated on next redisplay. */
10591
10592 static void
10593 update_overlay_arrows (up_to_date)
10594 int up_to_date;
10595 {
10596 Lisp_Object vlist;
10597
10598 for (vlist = Voverlay_arrow_variable_list;
10599 CONSP (vlist);
10600 vlist = XCDR (vlist))
10601 {
10602 Lisp_Object var = XCAR (vlist);
10603
10604 if (!SYMBOLP (var))
10605 continue;
10606
10607 if (up_to_date > 0)
10608 {
10609 Lisp_Object val = find_symbol_value (var);
10610 Fput (var, Qlast_arrow_position,
10611 COERCE_MARKER (val));
10612 Fput (var, Qlast_arrow_string,
10613 overlay_arrow_string_or_property (var));
10614 }
10615 else if (up_to_date < 0
10616 || !NILP (Fget (var, Qlast_arrow_position)))
10617 {
10618 Fput (var, Qlast_arrow_position, Qt);
10619 Fput (var, Qlast_arrow_string, Qt);
10620 }
10621 }
10622 }
10623
10624
10625 /* Return overlay arrow string to display at row.
10626 Return integer (bitmap number) for arrow bitmap in left fringe.
10627 Return nil if no overlay arrow. */
10628
10629 static Lisp_Object
10630 overlay_arrow_at_row (it, row)
10631 struct it *it;
10632 struct glyph_row *row;
10633 {
10634 Lisp_Object vlist;
10635
10636 for (vlist = Voverlay_arrow_variable_list;
10637 CONSP (vlist);
10638 vlist = XCDR (vlist))
10639 {
10640 Lisp_Object var = XCAR (vlist);
10641 Lisp_Object val;
10642
10643 if (!SYMBOLP (var))
10644 continue;
10645
10646 val = find_symbol_value (var);
10647
10648 if (MARKERP (val)
10649 && current_buffer == XMARKER (val)->buffer
10650 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10651 {
10652 if (FRAME_WINDOW_P (it->f)
10653 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10654 {
10655 #ifdef HAVE_WINDOW_SYSTEM
10656 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10657 {
10658 int fringe_bitmap;
10659 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10660 return make_number (fringe_bitmap);
10661 }
10662 #endif
10663 return make_number (-1); /* Use default arrow bitmap */
10664 }
10665 return overlay_arrow_string_or_property (var);
10666 }
10667 }
10668
10669 return Qnil;
10670 }
10671
10672 /* Return 1 if point moved out of or into a composition. Otherwise
10673 return 0. PREV_BUF and PREV_PT are the last point buffer and
10674 position. BUF and PT are the current point buffer and position. */
10675
10676 int
10677 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10678 struct buffer *prev_buf, *buf;
10679 int prev_pt, pt;
10680 {
10681 int start, end;
10682 Lisp_Object prop;
10683 Lisp_Object buffer;
10684
10685 XSETBUFFER (buffer, buf);
10686 /* Check a composition at the last point if point moved within the
10687 same buffer. */
10688 if (prev_buf == buf)
10689 {
10690 if (prev_pt == pt)
10691 /* Point didn't move. */
10692 return 0;
10693
10694 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10695 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10696 && COMPOSITION_VALID_P (start, end, prop)
10697 && start < prev_pt && end > prev_pt)
10698 /* The last point was within the composition. Return 1 iff
10699 point moved out of the composition. */
10700 return (pt <= start || pt >= end);
10701 }
10702
10703 /* Check a composition at the current point. */
10704 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10705 && find_composition (pt, -1, &start, &end, &prop, buffer)
10706 && COMPOSITION_VALID_P (start, end, prop)
10707 && start < pt && end > pt);
10708 }
10709
10710
10711 /* Reconsider the setting of B->clip_changed which is displayed
10712 in window W. */
10713
10714 static INLINE void
10715 reconsider_clip_changes (w, b)
10716 struct window *w;
10717 struct buffer *b;
10718 {
10719 if (b->clip_changed
10720 && !NILP (w->window_end_valid)
10721 && w->current_matrix->buffer == b
10722 && w->current_matrix->zv == BUF_ZV (b)
10723 && w->current_matrix->begv == BUF_BEGV (b))
10724 b->clip_changed = 0;
10725
10726 /* If display wasn't paused, and W is not a tool bar window, see if
10727 point has been moved into or out of a composition. In that case,
10728 we set b->clip_changed to 1 to force updating the screen. If
10729 b->clip_changed has already been set to 1, we can skip this
10730 check. */
10731 if (!b->clip_changed
10732 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10733 {
10734 int pt;
10735
10736 if (w == XWINDOW (selected_window))
10737 pt = BUF_PT (current_buffer);
10738 else
10739 pt = marker_position (w->pointm);
10740
10741 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10742 || pt != XINT (w->last_point))
10743 && check_point_in_composition (w->current_matrix->buffer,
10744 XINT (w->last_point),
10745 XBUFFER (w->buffer), pt))
10746 b->clip_changed = 1;
10747 }
10748 }
10749 \f
10750
10751 /* Select FRAME to forward the values of frame-local variables into C
10752 variables so that the redisplay routines can access those values
10753 directly. */
10754
10755 static void
10756 select_frame_for_redisplay (frame)
10757 Lisp_Object frame;
10758 {
10759 Lisp_Object tail, sym, val;
10760 Lisp_Object old = selected_frame;
10761
10762 selected_frame = frame;
10763
10764 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10765 if (CONSP (XCAR (tail))
10766 && (sym = XCAR (XCAR (tail)),
10767 SYMBOLP (sym))
10768 && (sym = indirect_variable (sym),
10769 val = SYMBOL_VALUE (sym),
10770 (BUFFER_LOCAL_VALUEP (val)
10771 || SOME_BUFFER_LOCAL_VALUEP (val)))
10772 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10773 /* Use find_symbol_value rather than Fsymbol_value
10774 to avoid an error if it is void. */
10775 find_symbol_value (sym);
10776
10777 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10778 if (CONSP (XCAR (tail))
10779 && (sym = XCAR (XCAR (tail)),
10780 SYMBOLP (sym))
10781 && (sym = indirect_variable (sym),
10782 val = SYMBOL_VALUE (sym),
10783 (BUFFER_LOCAL_VALUEP (val)
10784 || SOME_BUFFER_LOCAL_VALUEP (val)))
10785 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10786 find_symbol_value (sym);
10787 }
10788
10789
10790 #define STOP_POLLING \
10791 do { if (! polling_stopped_here) stop_polling (); \
10792 polling_stopped_here = 1; } while (0)
10793
10794 #define RESUME_POLLING \
10795 do { if (polling_stopped_here) start_polling (); \
10796 polling_stopped_here = 0; } while (0)
10797
10798
10799 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10800 response to any user action; therefore, we should preserve the echo
10801 area. (Actually, our caller does that job.) Perhaps in the future
10802 avoid recentering windows if it is not necessary; currently that
10803 causes some problems. */
10804
10805 static void
10806 redisplay_internal (preserve_echo_area)
10807 int preserve_echo_area;
10808 {
10809 struct window *w = XWINDOW (selected_window);
10810 struct frame *f;
10811 int pause;
10812 int must_finish = 0;
10813 struct text_pos tlbufpos, tlendpos;
10814 int number_of_visible_frames;
10815 int count;
10816 struct frame *sf;
10817 int polling_stopped_here = 0;
10818
10819 /* Non-zero means redisplay has to consider all windows on all
10820 frames. Zero means, only selected_window is considered. */
10821 int consider_all_windows_p;
10822
10823 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10824
10825 /* No redisplay if running in batch mode or frame is not yet fully
10826 initialized, or redisplay is explicitly turned off by setting
10827 Vinhibit_redisplay. */
10828 if (noninteractive
10829 || !NILP (Vinhibit_redisplay))
10830 return;
10831
10832 /* Don't examine these until after testing Vinhibit_redisplay.
10833 When Emacs is shutting down, perhaps because its connection to
10834 X has dropped, we should not look at them at all. */
10835 f = XFRAME (w->frame);
10836 sf = SELECTED_FRAME ();
10837
10838 if (!f->glyphs_initialized_p)
10839 return;
10840
10841 /* The flag redisplay_performed_directly_p is set by
10842 direct_output_for_insert when it already did the whole screen
10843 update necessary. */
10844 if (redisplay_performed_directly_p)
10845 {
10846 redisplay_performed_directly_p = 0;
10847 if (!hscroll_windows (selected_window))
10848 return;
10849 }
10850
10851 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10852 if (popup_activated ())
10853 return;
10854 #endif
10855
10856 /* I don't think this happens but let's be paranoid. */
10857 if (redisplaying_p)
10858 return;
10859
10860 /* Record a function that resets redisplaying_p to its old value
10861 when we leave this function. */
10862 count = SPECPDL_INDEX ();
10863 record_unwind_protect (unwind_redisplay,
10864 Fcons (make_number (redisplaying_p), selected_frame));
10865 ++redisplaying_p;
10866 specbind (Qinhibit_free_realized_faces, Qnil);
10867
10868 {
10869 Lisp_Object tail, frame;
10870
10871 FOR_EACH_FRAME (tail, frame)
10872 {
10873 struct frame *f = XFRAME (frame);
10874 f->already_hscrolled_p = 0;
10875 }
10876 }
10877
10878 retry:
10879 pause = 0;
10880 reconsider_clip_changes (w, current_buffer);
10881 last_escape_glyph_frame = NULL;
10882 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10883
10884 /* If new fonts have been loaded that make a glyph matrix adjustment
10885 necessary, do it. */
10886 if (fonts_changed_p)
10887 {
10888 adjust_glyphs (NULL);
10889 ++windows_or_buffers_changed;
10890 fonts_changed_p = 0;
10891 }
10892
10893 /* If face_change_count is non-zero, init_iterator will free all
10894 realized faces, which includes the faces referenced from current
10895 matrices. So, we can't reuse current matrices in this case. */
10896 if (face_change_count)
10897 ++windows_or_buffers_changed;
10898
10899 if (! FRAME_WINDOW_P (sf)
10900 && previous_terminal_frame != sf)
10901 {
10902 /* Since frames on an ASCII terminal share the same display
10903 area, displaying a different frame means redisplay the whole
10904 thing. */
10905 windows_or_buffers_changed++;
10906 SET_FRAME_GARBAGED (sf);
10907 XSETFRAME (Vterminal_frame, sf);
10908 }
10909 previous_terminal_frame = sf;
10910
10911 /* Set the visible flags for all frames. Do this before checking
10912 for resized or garbaged frames; they want to know if their frames
10913 are visible. See the comment in frame.h for
10914 FRAME_SAMPLE_VISIBILITY. */
10915 {
10916 Lisp_Object tail, frame;
10917
10918 number_of_visible_frames = 0;
10919
10920 FOR_EACH_FRAME (tail, frame)
10921 {
10922 struct frame *f = XFRAME (frame);
10923
10924 FRAME_SAMPLE_VISIBILITY (f);
10925 if (FRAME_VISIBLE_P (f))
10926 ++number_of_visible_frames;
10927 clear_desired_matrices (f);
10928 }
10929 }
10930
10931 /* Notice any pending interrupt request to change frame size. */
10932 do_pending_window_change (1);
10933
10934 /* Clear frames marked as garbaged. */
10935 if (frame_garbaged)
10936 clear_garbaged_frames ();
10937
10938 /* Build menubar and tool-bar items. */
10939 if (NILP (Vmemory_full))
10940 prepare_menu_bars ();
10941
10942 if (windows_or_buffers_changed)
10943 update_mode_lines++;
10944
10945 /* Detect case that we need to write or remove a star in the mode line. */
10946 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10947 {
10948 w->update_mode_line = Qt;
10949 if (buffer_shared > 1)
10950 update_mode_lines++;
10951 }
10952
10953 /* If %c is in the mode line, update it if needed. */
10954 if (!NILP (w->column_number_displayed)
10955 /* This alternative quickly identifies a common case
10956 where no change is needed. */
10957 && !(PT == XFASTINT (w->last_point)
10958 && XFASTINT (w->last_modified) >= MODIFF
10959 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10960 && (XFASTINT (w->column_number_displayed)
10961 != (int) current_column ())) /* iftc */
10962 w->update_mode_line = Qt;
10963
10964 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10965
10966 /* The variable buffer_shared is set in redisplay_window and
10967 indicates that we redisplay a buffer in different windows. See
10968 there. */
10969 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10970 || cursor_type_changed);
10971
10972 /* If specs for an arrow have changed, do thorough redisplay
10973 to ensure we remove any arrow that should no longer exist. */
10974 if (overlay_arrows_changed_p ())
10975 consider_all_windows_p = windows_or_buffers_changed = 1;
10976
10977 /* Normally the message* functions will have already displayed and
10978 updated the echo area, but the frame may have been trashed, or
10979 the update may have been preempted, so display the echo area
10980 again here. Checking message_cleared_p captures the case that
10981 the echo area should be cleared. */
10982 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10983 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10984 || (message_cleared_p
10985 && minibuf_level == 0
10986 /* If the mini-window is currently selected, this means the
10987 echo-area doesn't show through. */
10988 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10989 {
10990 int window_height_changed_p = echo_area_display (0);
10991 must_finish = 1;
10992
10993 /* If we don't display the current message, don't clear the
10994 message_cleared_p flag, because, if we did, we wouldn't clear
10995 the echo area in the next redisplay which doesn't preserve
10996 the echo area. */
10997 if (!display_last_displayed_message_p)
10998 message_cleared_p = 0;
10999
11000 if (fonts_changed_p)
11001 goto retry;
11002 else if (window_height_changed_p)
11003 {
11004 consider_all_windows_p = 1;
11005 ++update_mode_lines;
11006 ++windows_or_buffers_changed;
11007
11008 /* If window configuration was changed, frames may have been
11009 marked garbaged. Clear them or we will experience
11010 surprises wrt scrolling. */
11011 if (frame_garbaged)
11012 clear_garbaged_frames ();
11013 }
11014 }
11015 else if (EQ (selected_window, minibuf_window)
11016 && (current_buffer->clip_changed
11017 || XFASTINT (w->last_modified) < MODIFF
11018 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11019 && resize_mini_window (w, 0))
11020 {
11021 /* Resized active mini-window to fit the size of what it is
11022 showing if its contents might have changed. */
11023 must_finish = 1;
11024 consider_all_windows_p = 1;
11025 ++windows_or_buffers_changed;
11026 ++update_mode_lines;
11027
11028 /* If window configuration was changed, frames may have been
11029 marked garbaged. Clear them or we will experience
11030 surprises wrt scrolling. */
11031 if (frame_garbaged)
11032 clear_garbaged_frames ();
11033 }
11034
11035
11036 /* If showing the region, and mark has changed, we must redisplay
11037 the whole window. The assignment to this_line_start_pos prevents
11038 the optimization directly below this if-statement. */
11039 if (((!NILP (Vtransient_mark_mode)
11040 && !NILP (XBUFFER (w->buffer)->mark_active))
11041 != !NILP (w->region_showing))
11042 || (!NILP (w->region_showing)
11043 && !EQ (w->region_showing,
11044 Fmarker_position (XBUFFER (w->buffer)->mark))))
11045 CHARPOS (this_line_start_pos) = 0;
11046
11047 /* Optimize the case that only the line containing the cursor in the
11048 selected window has changed. Variables starting with this_ are
11049 set in display_line and record information about the line
11050 containing the cursor. */
11051 tlbufpos = this_line_start_pos;
11052 tlendpos = this_line_end_pos;
11053 if (!consider_all_windows_p
11054 && CHARPOS (tlbufpos) > 0
11055 && NILP (w->update_mode_line)
11056 && !current_buffer->clip_changed
11057 && !current_buffer->prevent_redisplay_optimizations_p
11058 && FRAME_VISIBLE_P (XFRAME (w->frame))
11059 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11060 /* Make sure recorded data applies to current buffer, etc. */
11061 && this_line_buffer == current_buffer
11062 && current_buffer == XBUFFER (w->buffer)
11063 && NILP (w->force_start)
11064 && NILP (w->optional_new_start)
11065 /* Point must be on the line that we have info recorded about. */
11066 && PT >= CHARPOS (tlbufpos)
11067 && PT <= Z - CHARPOS (tlendpos)
11068 /* All text outside that line, including its final newline,
11069 must be unchanged */
11070 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11071 CHARPOS (tlendpos)))
11072 {
11073 if (CHARPOS (tlbufpos) > BEGV
11074 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11075 && (CHARPOS (tlbufpos) == ZV
11076 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11077 /* Former continuation line has disappeared by becoming empty */
11078 goto cancel;
11079 else if (XFASTINT (w->last_modified) < MODIFF
11080 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11081 || MINI_WINDOW_P (w))
11082 {
11083 /* We have to handle the case of continuation around a
11084 wide-column character (See the comment in indent.c around
11085 line 885).
11086
11087 For instance, in the following case:
11088
11089 -------- Insert --------
11090 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11091 J_I_ ==> J_I_ `^^' are cursors.
11092 ^^ ^^
11093 -------- --------
11094
11095 As we have to redraw the line above, we should goto cancel. */
11096
11097 struct it it;
11098 int line_height_before = this_line_pixel_height;
11099
11100 /* Note that start_display will handle the case that the
11101 line starting at tlbufpos is a continuation lines. */
11102 start_display (&it, w, tlbufpos);
11103
11104 /* Implementation note: It this still necessary? */
11105 if (it.current_x != this_line_start_x)
11106 goto cancel;
11107
11108 TRACE ((stderr, "trying display optimization 1\n"));
11109 w->cursor.vpos = -1;
11110 overlay_arrow_seen = 0;
11111 it.vpos = this_line_vpos;
11112 it.current_y = this_line_y;
11113 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11114 display_line (&it);
11115
11116 /* If line contains point, is not continued,
11117 and ends at same distance from eob as before, we win */
11118 if (w->cursor.vpos >= 0
11119 /* Line is not continued, otherwise this_line_start_pos
11120 would have been set to 0 in display_line. */
11121 && CHARPOS (this_line_start_pos)
11122 /* Line ends as before. */
11123 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11124 /* Line has same height as before. Otherwise other lines
11125 would have to be shifted up or down. */
11126 && this_line_pixel_height == line_height_before)
11127 {
11128 /* If this is not the window's last line, we must adjust
11129 the charstarts of the lines below. */
11130 if (it.current_y < it.last_visible_y)
11131 {
11132 struct glyph_row *row
11133 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11134 int delta, delta_bytes;
11135
11136 if (Z - CHARPOS (tlendpos) == ZV)
11137 {
11138 /* This line ends at end of (accessible part of)
11139 buffer. There is no newline to count. */
11140 delta = (Z
11141 - CHARPOS (tlendpos)
11142 - MATRIX_ROW_START_CHARPOS (row));
11143 delta_bytes = (Z_BYTE
11144 - BYTEPOS (tlendpos)
11145 - MATRIX_ROW_START_BYTEPOS (row));
11146 }
11147 else
11148 {
11149 /* This line ends in a newline. Must take
11150 account of the newline and the rest of the
11151 text that follows. */
11152 delta = (Z
11153 - CHARPOS (tlendpos)
11154 - MATRIX_ROW_START_CHARPOS (row));
11155 delta_bytes = (Z_BYTE
11156 - BYTEPOS (tlendpos)
11157 - MATRIX_ROW_START_BYTEPOS (row));
11158 }
11159
11160 increment_matrix_positions (w->current_matrix,
11161 this_line_vpos + 1,
11162 w->current_matrix->nrows,
11163 delta, delta_bytes);
11164 }
11165
11166 /* If this row displays text now but previously didn't,
11167 or vice versa, w->window_end_vpos may have to be
11168 adjusted. */
11169 if ((it.glyph_row - 1)->displays_text_p)
11170 {
11171 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11172 XSETINT (w->window_end_vpos, this_line_vpos);
11173 }
11174 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11175 && this_line_vpos > 0)
11176 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11177 w->window_end_valid = Qnil;
11178
11179 /* Update hint: No need to try to scroll in update_window. */
11180 w->desired_matrix->no_scrolling_p = 1;
11181
11182 #if GLYPH_DEBUG
11183 *w->desired_matrix->method = 0;
11184 debug_method_add (w, "optimization 1");
11185 #endif
11186 #ifdef HAVE_WINDOW_SYSTEM
11187 update_window_fringes (w, 0);
11188 #endif
11189 goto update;
11190 }
11191 else
11192 goto cancel;
11193 }
11194 else if (/* Cursor position hasn't changed. */
11195 PT == XFASTINT (w->last_point)
11196 /* Make sure the cursor was last displayed
11197 in this window. Otherwise we have to reposition it. */
11198 && 0 <= w->cursor.vpos
11199 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11200 {
11201 if (!must_finish)
11202 {
11203 do_pending_window_change (1);
11204
11205 /* We used to always goto end_of_redisplay here, but this
11206 isn't enough if we have a blinking cursor. */
11207 if (w->cursor_off_p == w->last_cursor_off_p)
11208 goto end_of_redisplay;
11209 }
11210 goto update;
11211 }
11212 /* If highlighting the region, or if the cursor is in the echo area,
11213 then we can't just move the cursor. */
11214 else if (! (!NILP (Vtransient_mark_mode)
11215 && !NILP (current_buffer->mark_active))
11216 && (EQ (selected_window, current_buffer->last_selected_window)
11217 || highlight_nonselected_windows)
11218 && NILP (w->region_showing)
11219 && NILP (Vshow_trailing_whitespace)
11220 && !cursor_in_echo_area)
11221 {
11222 struct it it;
11223 struct glyph_row *row;
11224
11225 /* Skip from tlbufpos to PT and see where it is. Note that
11226 PT may be in invisible text. If so, we will end at the
11227 next visible position. */
11228 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11229 NULL, DEFAULT_FACE_ID);
11230 it.current_x = this_line_start_x;
11231 it.current_y = this_line_y;
11232 it.vpos = this_line_vpos;
11233
11234 /* The call to move_it_to stops in front of PT, but
11235 moves over before-strings. */
11236 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11237
11238 if (it.vpos == this_line_vpos
11239 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11240 row->enabled_p))
11241 {
11242 xassert (this_line_vpos == it.vpos);
11243 xassert (this_line_y == it.current_y);
11244 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11245 #if GLYPH_DEBUG
11246 *w->desired_matrix->method = 0;
11247 debug_method_add (w, "optimization 3");
11248 #endif
11249 goto update;
11250 }
11251 else
11252 goto cancel;
11253 }
11254
11255 cancel:
11256 /* Text changed drastically or point moved off of line. */
11257 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11258 }
11259
11260 CHARPOS (this_line_start_pos) = 0;
11261 consider_all_windows_p |= buffer_shared > 1;
11262 ++clear_face_cache_count;
11263 #ifdef HAVE_WINDOW_SYSTEM
11264 ++clear_image_cache_count;
11265 #endif
11266
11267 /* Build desired matrices, and update the display. If
11268 consider_all_windows_p is non-zero, do it for all windows on all
11269 frames. Otherwise do it for selected_window, only. */
11270
11271 if (consider_all_windows_p)
11272 {
11273 Lisp_Object tail, frame;
11274
11275 FOR_EACH_FRAME (tail, frame)
11276 XFRAME (frame)->updated_p = 0;
11277
11278 /* Recompute # windows showing selected buffer. This will be
11279 incremented each time such a window is displayed. */
11280 buffer_shared = 0;
11281
11282 FOR_EACH_FRAME (tail, frame)
11283 {
11284 struct frame *f = XFRAME (frame);
11285
11286 if (FRAME_WINDOW_P (f) || f == sf)
11287 {
11288 if (! EQ (frame, selected_frame))
11289 /* Select the frame, for the sake of frame-local
11290 variables. */
11291 select_frame_for_redisplay (frame);
11292
11293 /* Mark all the scroll bars to be removed; we'll redeem
11294 the ones we want when we redisplay their windows. */
11295 if (condemn_scroll_bars_hook)
11296 condemn_scroll_bars_hook (f);
11297
11298 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11299 redisplay_windows (FRAME_ROOT_WINDOW (f));
11300
11301 /* Any scroll bars which redisplay_windows should have
11302 nuked should now go away. */
11303 if (judge_scroll_bars_hook)
11304 judge_scroll_bars_hook (f);
11305
11306 /* If fonts changed, display again. */
11307 /* ??? rms: I suspect it is a mistake to jump all the way
11308 back to retry here. It should just retry this frame. */
11309 if (fonts_changed_p)
11310 goto retry;
11311
11312 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11313 {
11314 /* See if we have to hscroll. */
11315 if (!f->already_hscrolled_p)
11316 {
11317 f->already_hscrolled_p = 1;
11318 if (hscroll_windows (f->root_window))
11319 goto retry;
11320 }
11321
11322 /* Prevent various kinds of signals during display
11323 update. stdio is not robust about handling
11324 signals, which can cause an apparent I/O
11325 error. */
11326 if (interrupt_input)
11327 unrequest_sigio ();
11328 STOP_POLLING;
11329
11330 /* Update the display. */
11331 set_window_update_flags (XWINDOW (f->root_window), 1);
11332 pause |= update_frame (f, 0, 0);
11333 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11334 if (pause)
11335 break;
11336 #endif
11337
11338 f->updated_p = 1;
11339 }
11340 }
11341 }
11342
11343 if (!pause)
11344 {
11345 /* Do the mark_window_display_accurate after all windows have
11346 been redisplayed because this call resets flags in buffers
11347 which are needed for proper redisplay. */
11348 FOR_EACH_FRAME (tail, frame)
11349 {
11350 struct frame *f = XFRAME (frame);
11351 if (f->updated_p)
11352 {
11353 mark_window_display_accurate (f->root_window, 1);
11354 if (frame_up_to_date_hook)
11355 frame_up_to_date_hook (f);
11356 }
11357 }
11358 }
11359 }
11360 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11361 {
11362 Lisp_Object mini_window;
11363 struct frame *mini_frame;
11364
11365 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11366 /* Use list_of_error, not Qerror, so that
11367 we catch only errors and don't run the debugger. */
11368 internal_condition_case_1 (redisplay_window_1, selected_window,
11369 list_of_error,
11370 redisplay_window_error);
11371
11372 /* Compare desired and current matrices, perform output. */
11373
11374 update:
11375 /* If fonts changed, display again. */
11376 if (fonts_changed_p)
11377 goto retry;
11378
11379 /* Prevent various kinds of signals during display update.
11380 stdio is not robust about handling signals,
11381 which can cause an apparent I/O error. */
11382 if (interrupt_input)
11383 unrequest_sigio ();
11384 STOP_POLLING;
11385
11386 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11387 {
11388 if (hscroll_windows (selected_window))
11389 goto retry;
11390
11391 XWINDOW (selected_window)->must_be_updated_p = 1;
11392 pause = update_frame (sf, 0, 0);
11393 }
11394
11395 /* We may have called echo_area_display at the top of this
11396 function. If the echo area is on another frame, that may
11397 have put text on a frame other than the selected one, so the
11398 above call to update_frame would not have caught it. Catch
11399 it here. */
11400 mini_window = FRAME_MINIBUF_WINDOW (sf);
11401 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11402
11403 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11404 {
11405 XWINDOW (mini_window)->must_be_updated_p = 1;
11406 pause |= update_frame (mini_frame, 0, 0);
11407 if (!pause && hscroll_windows (mini_window))
11408 goto retry;
11409 }
11410 }
11411
11412 /* If display was paused because of pending input, make sure we do a
11413 thorough update the next time. */
11414 if (pause)
11415 {
11416 /* Prevent the optimization at the beginning of
11417 redisplay_internal that tries a single-line update of the
11418 line containing the cursor in the selected window. */
11419 CHARPOS (this_line_start_pos) = 0;
11420
11421 /* Let the overlay arrow be updated the next time. */
11422 update_overlay_arrows (0);
11423
11424 /* If we pause after scrolling, some rows in the current
11425 matrices of some windows are not valid. */
11426 if (!WINDOW_FULL_WIDTH_P (w)
11427 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11428 update_mode_lines = 1;
11429 }
11430 else
11431 {
11432 if (!consider_all_windows_p)
11433 {
11434 /* This has already been done above if
11435 consider_all_windows_p is set. */
11436 mark_window_display_accurate_1 (w, 1);
11437
11438 /* Say overlay arrows are up to date. */
11439 update_overlay_arrows (1);
11440
11441 if (frame_up_to_date_hook != 0)
11442 frame_up_to_date_hook (sf);
11443 }
11444
11445 update_mode_lines = 0;
11446 windows_or_buffers_changed = 0;
11447 cursor_type_changed = 0;
11448 }
11449
11450 /* Start SIGIO interrupts coming again. Having them off during the
11451 code above makes it less likely one will discard output, but not
11452 impossible, since there might be stuff in the system buffer here.
11453 But it is much hairier to try to do anything about that. */
11454 if (interrupt_input)
11455 request_sigio ();
11456 RESUME_POLLING;
11457
11458 /* If a frame has become visible which was not before, redisplay
11459 again, so that we display it. Expose events for such a frame
11460 (which it gets when becoming visible) don't call the parts of
11461 redisplay constructing glyphs, so simply exposing a frame won't
11462 display anything in this case. So, we have to display these
11463 frames here explicitly. */
11464 if (!pause)
11465 {
11466 Lisp_Object tail, frame;
11467 int new_count = 0;
11468
11469 FOR_EACH_FRAME (tail, frame)
11470 {
11471 int this_is_visible = 0;
11472
11473 if (XFRAME (frame)->visible)
11474 this_is_visible = 1;
11475 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11476 if (XFRAME (frame)->visible)
11477 this_is_visible = 1;
11478
11479 if (this_is_visible)
11480 new_count++;
11481 }
11482
11483 if (new_count != number_of_visible_frames)
11484 windows_or_buffers_changed++;
11485 }
11486
11487 /* Change frame size now if a change is pending. */
11488 do_pending_window_change (1);
11489
11490 /* If we just did a pending size change, or have additional
11491 visible frames, redisplay again. */
11492 if (windows_or_buffers_changed && !pause)
11493 goto retry;
11494
11495 /* Clear the face cache eventually. */
11496 if (consider_all_windows_p)
11497 {
11498 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11499 {
11500 clear_face_cache (0);
11501 clear_face_cache_count = 0;
11502 }
11503 #ifdef HAVE_WINDOW_SYSTEM
11504 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11505 {
11506 Lisp_Object tail, frame;
11507 FOR_EACH_FRAME (tail, frame)
11508 {
11509 struct frame *f = XFRAME (frame);
11510 if (FRAME_WINDOW_P (f))
11511 clear_image_cache (f, 0);
11512 }
11513 clear_image_cache_count = 0;
11514 }
11515 #endif /* HAVE_WINDOW_SYSTEM */
11516 }
11517
11518 end_of_redisplay:
11519 unbind_to (count, Qnil);
11520 RESUME_POLLING;
11521 }
11522
11523
11524 /* Redisplay, but leave alone any recent echo area message unless
11525 another message has been requested in its place.
11526
11527 This is useful in situations where you need to redisplay but no
11528 user action has occurred, making it inappropriate for the message
11529 area to be cleared. See tracking_off and
11530 wait_reading_process_output for examples of these situations.
11531
11532 FROM_WHERE is an integer saying from where this function was
11533 called. This is useful for debugging. */
11534
11535 void
11536 redisplay_preserve_echo_area (from_where)
11537 int from_where;
11538 {
11539 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11540
11541 if (!NILP (echo_area_buffer[1]))
11542 {
11543 /* We have a previously displayed message, but no current
11544 message. Redisplay the previous message. */
11545 display_last_displayed_message_p = 1;
11546 redisplay_internal (1);
11547 display_last_displayed_message_p = 0;
11548 }
11549 else
11550 redisplay_internal (1);
11551
11552 if (rif != NULL && rif->flush_display_optional)
11553 rif->flush_display_optional (NULL);
11554 }
11555
11556
11557 /* Function registered with record_unwind_protect in
11558 redisplay_internal. Reset redisplaying_p to the value it had
11559 before redisplay_internal was called, and clear
11560 prevent_freeing_realized_faces_p. It also selects the previously
11561 selected frame. */
11562
11563 static Lisp_Object
11564 unwind_redisplay (val)
11565 Lisp_Object val;
11566 {
11567 Lisp_Object old_redisplaying_p, old_frame;
11568
11569 old_redisplaying_p = XCAR (val);
11570 redisplaying_p = XFASTINT (old_redisplaying_p);
11571 old_frame = XCDR (val);
11572 if (! EQ (old_frame, selected_frame))
11573 select_frame_for_redisplay (old_frame);
11574 return Qnil;
11575 }
11576
11577
11578 /* Mark the display of window W as accurate or inaccurate. If
11579 ACCURATE_P is non-zero mark display of W as accurate. If
11580 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11581 redisplay_internal is called. */
11582
11583 static void
11584 mark_window_display_accurate_1 (w, accurate_p)
11585 struct window *w;
11586 int accurate_p;
11587 {
11588 if (BUFFERP (w->buffer))
11589 {
11590 struct buffer *b = XBUFFER (w->buffer);
11591
11592 w->last_modified
11593 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11594 w->last_overlay_modified
11595 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11596 w->last_had_star
11597 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11598
11599 if (accurate_p)
11600 {
11601 b->clip_changed = 0;
11602 b->prevent_redisplay_optimizations_p = 0;
11603
11604 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11605 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11606 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11607 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11608
11609 w->current_matrix->buffer = b;
11610 w->current_matrix->begv = BUF_BEGV (b);
11611 w->current_matrix->zv = BUF_ZV (b);
11612
11613 w->last_cursor = w->cursor;
11614 w->last_cursor_off_p = w->cursor_off_p;
11615
11616 if (w == XWINDOW (selected_window))
11617 w->last_point = make_number (BUF_PT (b));
11618 else
11619 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11620 }
11621 }
11622
11623 if (accurate_p)
11624 {
11625 w->window_end_valid = w->buffer;
11626 #if 0 /* This is incorrect with variable-height lines. */
11627 xassert (XINT (w->window_end_vpos)
11628 < (WINDOW_TOTAL_LINES (w)
11629 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11630 #endif
11631 w->update_mode_line = Qnil;
11632 }
11633 }
11634
11635
11636 /* Mark the display of windows in the window tree rooted at WINDOW as
11637 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11638 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11639 be redisplayed the next time redisplay_internal is called. */
11640
11641 void
11642 mark_window_display_accurate (window, accurate_p)
11643 Lisp_Object window;
11644 int accurate_p;
11645 {
11646 struct window *w;
11647
11648 for (; !NILP (window); window = w->next)
11649 {
11650 w = XWINDOW (window);
11651 mark_window_display_accurate_1 (w, accurate_p);
11652
11653 if (!NILP (w->vchild))
11654 mark_window_display_accurate (w->vchild, accurate_p);
11655 if (!NILP (w->hchild))
11656 mark_window_display_accurate (w->hchild, accurate_p);
11657 }
11658
11659 if (accurate_p)
11660 {
11661 update_overlay_arrows (1);
11662 }
11663 else
11664 {
11665 /* Force a thorough redisplay the next time by setting
11666 last_arrow_position and last_arrow_string to t, which is
11667 unequal to any useful value of Voverlay_arrow_... */
11668 update_overlay_arrows (-1);
11669 }
11670 }
11671
11672
11673 /* Return value in display table DP (Lisp_Char_Table *) for character
11674 C. Since a display table doesn't have any parent, we don't have to
11675 follow parent. Do not call this function directly but use the
11676 macro DISP_CHAR_VECTOR. */
11677
11678 Lisp_Object
11679 disp_char_vector (dp, c)
11680 struct Lisp_Char_Table *dp;
11681 int c;
11682 {
11683 int code[4], i;
11684 Lisp_Object val;
11685
11686 if (SINGLE_BYTE_CHAR_P (c))
11687 return (dp->contents[c]);
11688
11689 SPLIT_CHAR (c, code[0], code[1], code[2]);
11690 if (code[1] < 32)
11691 code[1] = -1;
11692 else if (code[2] < 32)
11693 code[2] = -1;
11694
11695 /* Here, the possible range of code[0] (== charset ID) is
11696 128..max_charset. Since the top level char table contains data
11697 for multibyte characters after 256th element, we must increment
11698 code[0] by 128 to get a correct index. */
11699 code[0] += 128;
11700 code[3] = -1; /* anchor */
11701
11702 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11703 {
11704 val = dp->contents[code[i]];
11705 if (!SUB_CHAR_TABLE_P (val))
11706 return (NILP (val) ? dp->defalt : val);
11707 }
11708
11709 /* Here, val is a sub char table. We return the default value of
11710 it. */
11711 return (dp->defalt);
11712 }
11713
11714
11715 \f
11716 /***********************************************************************
11717 Window Redisplay
11718 ***********************************************************************/
11719
11720 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11721
11722 static void
11723 redisplay_windows (window)
11724 Lisp_Object window;
11725 {
11726 while (!NILP (window))
11727 {
11728 struct window *w = XWINDOW (window);
11729
11730 if (!NILP (w->hchild))
11731 redisplay_windows (w->hchild);
11732 else if (!NILP (w->vchild))
11733 redisplay_windows (w->vchild);
11734 else
11735 {
11736 displayed_buffer = XBUFFER (w->buffer);
11737 /* Use list_of_error, not Qerror, so that
11738 we catch only errors and don't run the debugger. */
11739 internal_condition_case_1 (redisplay_window_0, window,
11740 list_of_error,
11741 redisplay_window_error);
11742 }
11743
11744 window = w->next;
11745 }
11746 }
11747
11748 static Lisp_Object
11749 redisplay_window_error ()
11750 {
11751 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11752 return Qnil;
11753 }
11754
11755 static Lisp_Object
11756 redisplay_window_0 (window)
11757 Lisp_Object window;
11758 {
11759 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11760 redisplay_window (window, 0);
11761 return Qnil;
11762 }
11763
11764 static Lisp_Object
11765 redisplay_window_1 (window)
11766 Lisp_Object window;
11767 {
11768 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11769 redisplay_window (window, 1);
11770 return Qnil;
11771 }
11772 \f
11773
11774 /* Increment GLYPH until it reaches END or CONDITION fails while
11775 adding (GLYPH)->pixel_width to X. */
11776
11777 #define SKIP_GLYPHS(glyph, end, x, condition) \
11778 do \
11779 { \
11780 (x) += (glyph)->pixel_width; \
11781 ++(glyph); \
11782 } \
11783 while ((glyph) < (end) && (condition))
11784
11785
11786 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11787 DELTA is the number of bytes by which positions recorded in ROW
11788 differ from current buffer positions.
11789
11790 Return 0 if cursor is not on this row. 1 otherwise. */
11791
11792 int
11793 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11794 struct window *w;
11795 struct glyph_row *row;
11796 struct glyph_matrix *matrix;
11797 int delta, delta_bytes, dy, dvpos;
11798 {
11799 struct glyph *glyph = row->glyphs[TEXT_AREA];
11800 struct glyph *end = glyph + row->used[TEXT_AREA];
11801 struct glyph *cursor = NULL;
11802 /* The first glyph that starts a sequence of glyphs from string. */
11803 struct glyph *string_start;
11804 /* The X coordinate of string_start. */
11805 int string_start_x;
11806 /* The last known character position. */
11807 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11808 /* The last known character position before string_start. */
11809 int string_before_pos;
11810 int x = row->x;
11811 int cursor_x = x;
11812 int cursor_from_overlay_pos = 0;
11813 int pt_old = PT - delta;
11814
11815 /* Skip over glyphs not having an object at the start of the row.
11816 These are special glyphs like truncation marks on terminal
11817 frames. */
11818 if (row->displays_text_p)
11819 while (glyph < end
11820 && INTEGERP (glyph->object)
11821 && glyph->charpos < 0)
11822 {
11823 x += glyph->pixel_width;
11824 ++glyph;
11825 }
11826
11827 string_start = NULL;
11828 while (glyph < end
11829 && !INTEGERP (glyph->object)
11830 && (!BUFFERP (glyph->object)
11831 || (last_pos = glyph->charpos) < pt_old))
11832 {
11833 if (! STRINGP (glyph->object))
11834 {
11835 string_start = NULL;
11836 x += glyph->pixel_width;
11837 ++glyph;
11838 if (cursor_from_overlay_pos
11839 && last_pos >= cursor_from_overlay_pos)
11840 {
11841 cursor_from_overlay_pos = 0;
11842 cursor = 0;
11843 }
11844 }
11845 else
11846 {
11847 if (string_start == NULL)
11848 {
11849 string_before_pos = last_pos;
11850 string_start = glyph;
11851 string_start_x = x;
11852 }
11853 /* Skip all glyphs from string. */
11854 do
11855 {
11856 Lisp_Object cprop;
11857 int pos;
11858 if ((cursor == NULL || glyph > cursor)
11859 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11860 Qcursor, (glyph)->object),
11861 !NILP (cprop))
11862 && (pos = string_buffer_position (w, glyph->object,
11863 string_before_pos),
11864 (pos == 0 /* From overlay */
11865 || pos == pt_old)))
11866 {
11867 /* Estimate overlay buffer position from the buffer
11868 positions of the glyphs before and after the overlay.
11869 Add 1 to last_pos so that if point corresponds to the
11870 glyph right after the overlay, we still use a 'cursor'
11871 property found in that overlay. */
11872 cursor_from_overlay_pos = (pos ? 0 : last_pos
11873 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11874 cursor = glyph;
11875 cursor_x = x;
11876 }
11877 x += glyph->pixel_width;
11878 ++glyph;
11879 }
11880 while (glyph < end && EQ (glyph->object, string_start->object));
11881 }
11882 }
11883
11884 if (cursor != NULL)
11885 {
11886 glyph = cursor;
11887 x = cursor_x;
11888 }
11889 else if (row->ends_in_ellipsis_p && glyph == end)
11890 {
11891 /* Scan back over the ellipsis glyphs, decrementing positions. */
11892 while (glyph > row->glyphs[TEXT_AREA]
11893 && (glyph - 1)->charpos == last_pos)
11894 glyph--, x -= glyph->pixel_width;
11895 /* That loop always goes one position too far,
11896 including the glyph before the ellipsis.
11897 So scan forward over that one. */
11898 x += glyph->pixel_width;
11899 glyph++;
11900 }
11901 else if (string_start
11902 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11903 {
11904 /* We may have skipped over point because the previous glyphs
11905 are from string. As there's no easy way to know the
11906 character position of the current glyph, find the correct
11907 glyph on point by scanning from string_start again. */
11908 Lisp_Object limit;
11909 Lisp_Object string;
11910 struct glyph *stop = glyph;
11911 int pos;
11912
11913 limit = make_number (pt_old + 1);
11914 glyph = string_start;
11915 x = string_start_x;
11916 string = glyph->object;
11917 pos = string_buffer_position (w, string, string_before_pos);
11918 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11919 because we always put cursor after overlay strings. */
11920 while (pos == 0 && glyph < stop)
11921 {
11922 string = glyph->object;
11923 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11924 if (glyph < stop)
11925 pos = string_buffer_position (w, glyph->object, string_before_pos);
11926 }
11927
11928 while (glyph < stop)
11929 {
11930 pos = XINT (Fnext_single_char_property_change
11931 (make_number (pos), Qdisplay, Qnil, limit));
11932 if (pos > pt_old)
11933 break;
11934 /* Skip glyphs from the same string. */
11935 string = glyph->object;
11936 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11937 /* Skip glyphs from an overlay. */
11938 while (glyph < stop
11939 && ! string_buffer_position (w, glyph->object, pos))
11940 {
11941 string = glyph->object;
11942 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11943 }
11944 }
11945
11946 /* If we reached the end of the line, and end was from a string,
11947 cursor is not on this line. */
11948 if (glyph == end && row->continued_p)
11949 return 0;
11950 }
11951
11952 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11953 w->cursor.x = x;
11954 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11955 w->cursor.y = row->y + dy;
11956
11957 if (w == XWINDOW (selected_window))
11958 {
11959 if (!row->continued_p
11960 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11961 && row->x == 0)
11962 {
11963 this_line_buffer = XBUFFER (w->buffer);
11964
11965 CHARPOS (this_line_start_pos)
11966 = MATRIX_ROW_START_CHARPOS (row) + delta;
11967 BYTEPOS (this_line_start_pos)
11968 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11969
11970 CHARPOS (this_line_end_pos)
11971 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11972 BYTEPOS (this_line_end_pos)
11973 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11974
11975 this_line_y = w->cursor.y;
11976 this_line_pixel_height = row->height;
11977 this_line_vpos = w->cursor.vpos;
11978 this_line_start_x = row->x;
11979 }
11980 else
11981 CHARPOS (this_line_start_pos) = 0;
11982 }
11983
11984 return 1;
11985 }
11986
11987
11988 /* Run window scroll functions, if any, for WINDOW with new window
11989 start STARTP. Sets the window start of WINDOW to that position.
11990
11991 We assume that the window's buffer is really current. */
11992
11993 static INLINE struct text_pos
11994 run_window_scroll_functions (window, startp)
11995 Lisp_Object window;
11996 struct text_pos startp;
11997 {
11998 struct window *w = XWINDOW (window);
11999 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12000
12001 if (current_buffer != XBUFFER (w->buffer))
12002 abort ();
12003
12004 if (!NILP (Vwindow_scroll_functions))
12005 {
12006 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12007 make_number (CHARPOS (startp)));
12008 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12009 /* In case the hook functions switch buffers. */
12010 if (current_buffer != XBUFFER (w->buffer))
12011 set_buffer_internal_1 (XBUFFER (w->buffer));
12012 }
12013
12014 return startp;
12015 }
12016
12017
12018 /* Make sure the line containing the cursor is fully visible.
12019 A value of 1 means there is nothing to be done.
12020 (Either the line is fully visible, or it cannot be made so,
12021 or we cannot tell.)
12022
12023 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12024 is higher than window.
12025
12026 A value of 0 means the caller should do scrolling
12027 as if point had gone off the screen. */
12028
12029 static int
12030 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12031 struct window *w;
12032 int force_p;
12033 int current_matrix_p;
12034 {
12035 struct glyph_matrix *matrix;
12036 struct glyph_row *row;
12037 int window_height;
12038
12039 if (!make_cursor_line_fully_visible_p)
12040 return 1;
12041
12042 /* It's not always possible to find the cursor, e.g, when a window
12043 is full of overlay strings. Don't do anything in that case. */
12044 if (w->cursor.vpos < 0)
12045 return 1;
12046
12047 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12048 row = MATRIX_ROW (matrix, w->cursor.vpos);
12049
12050 /* If the cursor row is not partially visible, there's nothing to do. */
12051 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12052 return 1;
12053
12054 /* If the row the cursor is in is taller than the window's height,
12055 it's not clear what to do, so do nothing. */
12056 window_height = window_box_height (w);
12057 if (row->height >= window_height)
12058 {
12059 if (!force_p || MINI_WINDOW_P (w)
12060 || w->vscroll || w->cursor.vpos == 0)
12061 return 1;
12062 }
12063 return 0;
12064
12065 #if 0
12066 /* This code used to try to scroll the window just enough to make
12067 the line visible. It returned 0 to say that the caller should
12068 allocate larger glyph matrices. */
12069
12070 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12071 {
12072 int dy = row->height - row->visible_height;
12073 w->vscroll = 0;
12074 w->cursor.y += dy;
12075 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12076 }
12077 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12078 {
12079 int dy = - (row->height - row->visible_height);
12080 w->vscroll = dy;
12081 w->cursor.y += dy;
12082 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12083 }
12084
12085 /* When we change the cursor y-position of the selected window,
12086 change this_line_y as well so that the display optimization for
12087 the cursor line of the selected window in redisplay_internal uses
12088 the correct y-position. */
12089 if (w == XWINDOW (selected_window))
12090 this_line_y = w->cursor.y;
12091
12092 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12093 redisplay with larger matrices. */
12094 if (matrix->nrows < required_matrix_height (w))
12095 {
12096 fonts_changed_p = 1;
12097 return 0;
12098 }
12099
12100 return 1;
12101 #endif /* 0 */
12102 }
12103
12104
12105 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12106 non-zero means only WINDOW is redisplayed in redisplay_internal.
12107 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12108 in redisplay_window to bring a partially visible line into view in
12109 the case that only the cursor has moved.
12110
12111 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12112 last screen line's vertical height extends past the end of the screen.
12113
12114 Value is
12115
12116 1 if scrolling succeeded
12117
12118 0 if scrolling didn't find point.
12119
12120 -1 if new fonts have been loaded so that we must interrupt
12121 redisplay, adjust glyph matrices, and try again. */
12122
12123 enum
12124 {
12125 SCROLLING_SUCCESS,
12126 SCROLLING_FAILED,
12127 SCROLLING_NEED_LARGER_MATRICES
12128 };
12129
12130 static int
12131 try_scrolling (window, just_this_one_p, scroll_conservatively,
12132 scroll_step, temp_scroll_step, last_line_misfit)
12133 Lisp_Object window;
12134 int just_this_one_p;
12135 EMACS_INT scroll_conservatively, scroll_step;
12136 int temp_scroll_step;
12137 int last_line_misfit;
12138 {
12139 struct window *w = XWINDOW (window);
12140 struct frame *f = XFRAME (w->frame);
12141 struct text_pos scroll_margin_pos;
12142 struct text_pos pos;
12143 struct text_pos startp;
12144 struct it it;
12145 Lisp_Object window_end;
12146 int this_scroll_margin;
12147 int dy = 0;
12148 int scroll_max;
12149 int rc;
12150 int amount_to_scroll = 0;
12151 Lisp_Object aggressive;
12152 int height;
12153 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12154
12155 #if GLYPH_DEBUG
12156 debug_method_add (w, "try_scrolling");
12157 #endif
12158
12159 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12160
12161 /* Compute scroll margin height in pixels. We scroll when point is
12162 within this distance from the top or bottom of the window. */
12163 if (scroll_margin > 0)
12164 {
12165 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12166 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12167 }
12168 else
12169 this_scroll_margin = 0;
12170
12171 /* Force scroll_conservatively to have a reasonable value so it doesn't
12172 cause an overflow while computing how much to scroll. */
12173 if (scroll_conservatively)
12174 scroll_conservatively = min (scroll_conservatively,
12175 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12176
12177 /* Compute how much we should try to scroll maximally to bring point
12178 into view. */
12179 if (scroll_step || scroll_conservatively || temp_scroll_step)
12180 scroll_max = max (scroll_step,
12181 max (scroll_conservatively, temp_scroll_step));
12182 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12183 || NUMBERP (current_buffer->scroll_up_aggressively))
12184 /* We're trying to scroll because of aggressive scrolling
12185 but no scroll_step is set. Choose an arbitrary one. Maybe
12186 there should be a variable for this. */
12187 scroll_max = 10;
12188 else
12189 scroll_max = 0;
12190 scroll_max *= FRAME_LINE_HEIGHT (f);
12191
12192 /* Decide whether we have to scroll down. Start at the window end
12193 and move this_scroll_margin up to find the position of the scroll
12194 margin. */
12195 window_end = Fwindow_end (window, Qt);
12196
12197 too_near_end:
12198
12199 CHARPOS (scroll_margin_pos) = XINT (window_end);
12200 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12201
12202 if (this_scroll_margin || extra_scroll_margin_lines)
12203 {
12204 start_display (&it, w, scroll_margin_pos);
12205 if (this_scroll_margin)
12206 move_it_vertically_backward (&it, this_scroll_margin);
12207 if (extra_scroll_margin_lines)
12208 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12209 scroll_margin_pos = it.current.pos;
12210 }
12211
12212 if (PT >= CHARPOS (scroll_margin_pos))
12213 {
12214 int y0;
12215
12216 /* Point is in the scroll margin at the bottom of the window, or
12217 below. Compute a new window start that makes point visible. */
12218
12219 /* Compute the distance from the scroll margin to PT.
12220 Give up if the distance is greater than scroll_max. */
12221 start_display (&it, w, scroll_margin_pos);
12222 y0 = it.current_y;
12223 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12224 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12225
12226 /* To make point visible, we have to move the window start
12227 down so that the line the cursor is in is visible, which
12228 means we have to add in the height of the cursor line. */
12229 dy = line_bottom_y (&it) - y0;
12230
12231 if (dy > scroll_max)
12232 return SCROLLING_FAILED;
12233
12234 /* Move the window start down. If scrolling conservatively,
12235 move it just enough down to make point visible. If
12236 scroll_step is set, move it down by scroll_step. */
12237 start_display (&it, w, startp);
12238
12239 if (scroll_conservatively)
12240 /* Set AMOUNT_TO_SCROLL to at least one line,
12241 and at most scroll_conservatively lines. */
12242 amount_to_scroll
12243 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12244 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12245 else if (scroll_step || temp_scroll_step)
12246 amount_to_scroll = scroll_max;
12247 else
12248 {
12249 aggressive = current_buffer->scroll_up_aggressively;
12250 height = WINDOW_BOX_TEXT_HEIGHT (w);
12251 if (NUMBERP (aggressive))
12252 {
12253 double float_amount = XFLOATINT (aggressive) * height;
12254 amount_to_scroll = float_amount;
12255 if (amount_to_scroll == 0 && float_amount > 0)
12256 amount_to_scroll = 1;
12257 }
12258 }
12259
12260 if (amount_to_scroll <= 0)
12261 return SCROLLING_FAILED;
12262
12263 /* If moving by amount_to_scroll leaves STARTP unchanged,
12264 move it down one screen line. */
12265
12266 move_it_vertically (&it, amount_to_scroll);
12267 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12268 move_it_by_lines (&it, 1, 1);
12269 startp = it.current.pos;
12270 }
12271 else
12272 {
12273 /* See if point is inside the scroll margin at the top of the
12274 window. */
12275 scroll_margin_pos = startp;
12276 if (this_scroll_margin)
12277 {
12278 start_display (&it, w, startp);
12279 move_it_vertically (&it, this_scroll_margin);
12280 scroll_margin_pos = it.current.pos;
12281 }
12282
12283 if (PT < CHARPOS (scroll_margin_pos))
12284 {
12285 /* Point is in the scroll margin at the top of the window or
12286 above what is displayed in the window. */
12287 int y0;
12288
12289 /* Compute the vertical distance from PT to the scroll
12290 margin position. Give up if distance is greater than
12291 scroll_max. */
12292 SET_TEXT_POS (pos, PT, PT_BYTE);
12293 start_display (&it, w, pos);
12294 y0 = it.current_y;
12295 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12296 it.last_visible_y, -1,
12297 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12298 dy = it.current_y - y0;
12299 if (dy > scroll_max)
12300 return SCROLLING_FAILED;
12301
12302 /* Compute new window start. */
12303 start_display (&it, w, startp);
12304
12305 if (scroll_conservatively)
12306 amount_to_scroll
12307 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12308 else if (scroll_step || temp_scroll_step)
12309 amount_to_scroll = scroll_max;
12310 else
12311 {
12312 aggressive = current_buffer->scroll_down_aggressively;
12313 height = WINDOW_BOX_TEXT_HEIGHT (w);
12314 if (NUMBERP (aggressive))
12315 {
12316 double float_amount = XFLOATINT (aggressive) * height;
12317 amount_to_scroll = float_amount;
12318 if (amount_to_scroll == 0 && float_amount > 0)
12319 amount_to_scroll = 1;
12320 }
12321 }
12322
12323 if (amount_to_scroll <= 0)
12324 return SCROLLING_FAILED;
12325
12326 move_it_vertically_backward (&it, amount_to_scroll);
12327 startp = it.current.pos;
12328 }
12329 }
12330
12331 /* Run window scroll functions. */
12332 startp = run_window_scroll_functions (window, startp);
12333
12334 /* Display the window. Give up if new fonts are loaded, or if point
12335 doesn't appear. */
12336 if (!try_window (window, startp, 0))
12337 rc = SCROLLING_NEED_LARGER_MATRICES;
12338 else if (w->cursor.vpos < 0)
12339 {
12340 clear_glyph_matrix (w->desired_matrix);
12341 rc = SCROLLING_FAILED;
12342 }
12343 else
12344 {
12345 /* Maybe forget recorded base line for line number display. */
12346 if (!just_this_one_p
12347 || current_buffer->clip_changed
12348 || BEG_UNCHANGED < CHARPOS (startp))
12349 w->base_line_number = Qnil;
12350
12351 /* If cursor ends up on a partially visible line,
12352 treat that as being off the bottom of the screen. */
12353 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12354 {
12355 clear_glyph_matrix (w->desired_matrix);
12356 ++extra_scroll_margin_lines;
12357 goto too_near_end;
12358 }
12359 rc = SCROLLING_SUCCESS;
12360 }
12361
12362 return rc;
12363 }
12364
12365
12366 /* Compute a suitable window start for window W if display of W starts
12367 on a continuation line. Value is non-zero if a new window start
12368 was computed.
12369
12370 The new window start will be computed, based on W's width, starting
12371 from the start of the continued line. It is the start of the
12372 screen line with the minimum distance from the old start W->start. */
12373
12374 static int
12375 compute_window_start_on_continuation_line (w)
12376 struct window *w;
12377 {
12378 struct text_pos pos, start_pos;
12379 int window_start_changed_p = 0;
12380
12381 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12382
12383 /* If window start is on a continuation line... Window start may be
12384 < BEGV in case there's invisible text at the start of the
12385 buffer (M-x rmail, for example). */
12386 if (CHARPOS (start_pos) > BEGV
12387 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12388 {
12389 struct it it;
12390 struct glyph_row *row;
12391
12392 /* Handle the case that the window start is out of range. */
12393 if (CHARPOS (start_pos) < BEGV)
12394 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12395 else if (CHARPOS (start_pos) > ZV)
12396 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12397
12398 /* Find the start of the continued line. This should be fast
12399 because scan_buffer is fast (newline cache). */
12400 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12401 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12402 row, DEFAULT_FACE_ID);
12403 reseat_at_previous_visible_line_start (&it);
12404
12405 /* If the line start is "too far" away from the window start,
12406 say it takes too much time to compute a new window start. */
12407 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12408 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12409 {
12410 int min_distance, distance;
12411
12412 /* Move forward by display lines to find the new window
12413 start. If window width was enlarged, the new start can
12414 be expected to be > the old start. If window width was
12415 decreased, the new window start will be < the old start.
12416 So, we're looking for the display line start with the
12417 minimum distance from the old window start. */
12418 pos = it.current.pos;
12419 min_distance = INFINITY;
12420 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12421 distance < min_distance)
12422 {
12423 min_distance = distance;
12424 pos = it.current.pos;
12425 move_it_by_lines (&it, 1, 0);
12426 }
12427
12428 /* Set the window start there. */
12429 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12430 window_start_changed_p = 1;
12431 }
12432 }
12433
12434 return window_start_changed_p;
12435 }
12436
12437
12438 /* Try cursor movement in case text has not changed in window WINDOW,
12439 with window start STARTP. Value is
12440
12441 CURSOR_MOVEMENT_SUCCESS if successful
12442
12443 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12444
12445 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12446 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12447 we want to scroll as if scroll-step were set to 1. See the code.
12448
12449 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12450 which case we have to abort this redisplay, and adjust matrices
12451 first. */
12452
12453 enum
12454 {
12455 CURSOR_MOVEMENT_SUCCESS,
12456 CURSOR_MOVEMENT_CANNOT_BE_USED,
12457 CURSOR_MOVEMENT_MUST_SCROLL,
12458 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12459 };
12460
12461 static int
12462 try_cursor_movement (window, startp, scroll_step)
12463 Lisp_Object window;
12464 struct text_pos startp;
12465 int *scroll_step;
12466 {
12467 struct window *w = XWINDOW (window);
12468 struct frame *f = XFRAME (w->frame);
12469 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12470
12471 #if GLYPH_DEBUG
12472 if (inhibit_try_cursor_movement)
12473 return rc;
12474 #endif
12475
12476 /* Handle case where text has not changed, only point, and it has
12477 not moved off the frame. */
12478 if (/* Point may be in this window. */
12479 PT >= CHARPOS (startp)
12480 /* Selective display hasn't changed. */
12481 && !current_buffer->clip_changed
12482 /* Function force-mode-line-update is used to force a thorough
12483 redisplay. It sets either windows_or_buffers_changed or
12484 update_mode_lines. So don't take a shortcut here for these
12485 cases. */
12486 && !update_mode_lines
12487 && !windows_or_buffers_changed
12488 && !cursor_type_changed
12489 /* Can't use this case if highlighting a region. When a
12490 region exists, cursor movement has to do more than just
12491 set the cursor. */
12492 && !(!NILP (Vtransient_mark_mode)
12493 && !NILP (current_buffer->mark_active))
12494 && NILP (w->region_showing)
12495 && NILP (Vshow_trailing_whitespace)
12496 /* Right after splitting windows, last_point may be nil. */
12497 && INTEGERP (w->last_point)
12498 /* This code is not used for mini-buffer for the sake of the case
12499 of redisplaying to replace an echo area message; since in
12500 that case the mini-buffer contents per se are usually
12501 unchanged. This code is of no real use in the mini-buffer
12502 since the handling of this_line_start_pos, etc., in redisplay
12503 handles the same cases. */
12504 && !EQ (window, minibuf_window)
12505 /* When splitting windows or for new windows, it happens that
12506 redisplay is called with a nil window_end_vpos or one being
12507 larger than the window. This should really be fixed in
12508 window.c. I don't have this on my list, now, so we do
12509 approximately the same as the old redisplay code. --gerd. */
12510 && INTEGERP (w->window_end_vpos)
12511 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12512 && (FRAME_WINDOW_P (f)
12513 || !overlay_arrow_in_current_buffer_p ()))
12514 {
12515 int this_scroll_margin, top_scroll_margin;
12516 struct glyph_row *row = NULL;
12517
12518 #if GLYPH_DEBUG
12519 debug_method_add (w, "cursor movement");
12520 #endif
12521
12522 /* Scroll if point within this distance from the top or bottom
12523 of the window. This is a pixel value. */
12524 this_scroll_margin = max (0, scroll_margin);
12525 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12526 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12527
12528 top_scroll_margin = this_scroll_margin;
12529 if (WINDOW_WANTS_HEADER_LINE_P (w))
12530 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12531
12532 /* Start with the row the cursor was displayed during the last
12533 not paused redisplay. Give up if that row is not valid. */
12534 if (w->last_cursor.vpos < 0
12535 || w->last_cursor.vpos >= w->current_matrix->nrows)
12536 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12537 else
12538 {
12539 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12540 if (row->mode_line_p)
12541 ++row;
12542 if (!row->enabled_p)
12543 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12544 }
12545
12546 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12547 {
12548 int scroll_p = 0;
12549 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12550
12551 if (PT > XFASTINT (w->last_point))
12552 {
12553 /* Point has moved forward. */
12554 while (MATRIX_ROW_END_CHARPOS (row) < PT
12555 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12556 {
12557 xassert (row->enabled_p);
12558 ++row;
12559 }
12560
12561 /* The end position of a row equals the start position
12562 of the next row. If PT is there, we would rather
12563 display it in the next line. */
12564 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12565 && MATRIX_ROW_END_CHARPOS (row) == PT
12566 && !cursor_row_p (w, row))
12567 ++row;
12568
12569 /* If within the scroll margin, scroll. Note that
12570 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12571 the next line would be drawn, and that
12572 this_scroll_margin can be zero. */
12573 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12574 || PT > MATRIX_ROW_END_CHARPOS (row)
12575 /* Line is completely visible last line in window
12576 and PT is to be set in the next line. */
12577 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12578 && PT == MATRIX_ROW_END_CHARPOS (row)
12579 && !row->ends_at_zv_p
12580 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12581 scroll_p = 1;
12582 }
12583 else if (PT < XFASTINT (w->last_point))
12584 {
12585 /* Cursor has to be moved backward. Note that PT >=
12586 CHARPOS (startp) because of the outer if-statement. */
12587 while (!row->mode_line_p
12588 && (MATRIX_ROW_START_CHARPOS (row) > PT
12589 || (MATRIX_ROW_START_CHARPOS (row) == PT
12590 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12591 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12592 row > w->current_matrix->rows
12593 && (row-1)->ends_in_newline_from_string_p))))
12594 && (row->y > top_scroll_margin
12595 || CHARPOS (startp) == BEGV))
12596 {
12597 xassert (row->enabled_p);
12598 --row;
12599 }
12600
12601 /* Consider the following case: Window starts at BEGV,
12602 there is invisible, intangible text at BEGV, so that
12603 display starts at some point START > BEGV. It can
12604 happen that we are called with PT somewhere between
12605 BEGV and START. Try to handle that case. */
12606 if (row < w->current_matrix->rows
12607 || row->mode_line_p)
12608 {
12609 row = w->current_matrix->rows;
12610 if (row->mode_line_p)
12611 ++row;
12612 }
12613
12614 /* Due to newlines in overlay strings, we may have to
12615 skip forward over overlay strings. */
12616 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12617 && MATRIX_ROW_END_CHARPOS (row) == PT
12618 && !cursor_row_p (w, row))
12619 ++row;
12620
12621 /* If within the scroll margin, scroll. */
12622 if (row->y < top_scroll_margin
12623 && CHARPOS (startp) != BEGV)
12624 scroll_p = 1;
12625 }
12626 else
12627 {
12628 /* Cursor did not move. So don't scroll even if cursor line
12629 is partially visible, as it was so before. */
12630 rc = CURSOR_MOVEMENT_SUCCESS;
12631 }
12632
12633 if (PT < MATRIX_ROW_START_CHARPOS (row)
12634 || PT > MATRIX_ROW_END_CHARPOS (row))
12635 {
12636 /* if PT is not in the glyph row, give up. */
12637 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12638 }
12639 else if (rc != CURSOR_MOVEMENT_SUCCESS
12640 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12641 && make_cursor_line_fully_visible_p)
12642 {
12643 if (PT == MATRIX_ROW_END_CHARPOS (row)
12644 && !row->ends_at_zv_p
12645 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12646 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12647 else if (row->height > window_box_height (w))
12648 {
12649 /* If we end up in a partially visible line, let's
12650 make it fully visible, except when it's taller
12651 than the window, in which case we can't do much
12652 about it. */
12653 *scroll_step = 1;
12654 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12655 }
12656 else
12657 {
12658 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12659 if (!cursor_row_fully_visible_p (w, 0, 1))
12660 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12661 else
12662 rc = CURSOR_MOVEMENT_SUCCESS;
12663 }
12664 }
12665 else if (scroll_p)
12666 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12667 else
12668 {
12669 do
12670 {
12671 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12672 {
12673 rc = CURSOR_MOVEMENT_SUCCESS;
12674 break;
12675 }
12676 ++row;
12677 }
12678 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12679 && MATRIX_ROW_START_CHARPOS (row) == PT
12680 && cursor_row_p (w, row));
12681 }
12682 }
12683 }
12684
12685 return rc;
12686 }
12687
12688 void
12689 set_vertical_scroll_bar (w)
12690 struct window *w;
12691 {
12692 int start, end, whole;
12693
12694 /* Calculate the start and end positions for the current window.
12695 At some point, it would be nice to choose between scrollbars
12696 which reflect the whole buffer size, with special markers
12697 indicating narrowing, and scrollbars which reflect only the
12698 visible region.
12699
12700 Note that mini-buffers sometimes aren't displaying any text. */
12701 if (!MINI_WINDOW_P (w)
12702 || (w == XWINDOW (minibuf_window)
12703 && NILP (echo_area_buffer[0])))
12704 {
12705 struct buffer *buf = XBUFFER (w->buffer);
12706 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12707 start = marker_position (w->start) - BUF_BEGV (buf);
12708 /* I don't think this is guaranteed to be right. For the
12709 moment, we'll pretend it is. */
12710 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12711
12712 if (end < start)
12713 end = start;
12714 if (whole < (end - start))
12715 whole = end - start;
12716 }
12717 else
12718 start = end = whole = 0;
12719
12720 /* Indicate what this scroll bar ought to be displaying now. */
12721 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12722 }
12723
12724
12725 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12726 selected_window is redisplayed.
12727
12728 We can return without actually redisplaying the window if
12729 fonts_changed_p is nonzero. In that case, redisplay_internal will
12730 retry. */
12731
12732 static void
12733 redisplay_window (window, just_this_one_p)
12734 Lisp_Object window;
12735 int just_this_one_p;
12736 {
12737 struct window *w = XWINDOW (window);
12738 struct frame *f = XFRAME (w->frame);
12739 struct buffer *buffer = XBUFFER (w->buffer);
12740 struct buffer *old = current_buffer;
12741 struct text_pos lpoint, opoint, startp;
12742 int update_mode_line;
12743 int tem;
12744 struct it it;
12745 /* Record it now because it's overwritten. */
12746 int current_matrix_up_to_date_p = 0;
12747 int used_current_matrix_p = 0;
12748 /* This is less strict than current_matrix_up_to_date_p.
12749 It indictes that the buffer contents and narrowing are unchanged. */
12750 int buffer_unchanged_p = 0;
12751 int temp_scroll_step = 0;
12752 int count = SPECPDL_INDEX ();
12753 int rc;
12754 int centering_position = -1;
12755 int last_line_misfit = 0;
12756
12757 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12758 opoint = lpoint;
12759
12760 /* W must be a leaf window here. */
12761 xassert (!NILP (w->buffer));
12762 #if GLYPH_DEBUG
12763 *w->desired_matrix->method = 0;
12764 #endif
12765
12766 specbind (Qinhibit_point_motion_hooks, Qt);
12767
12768 reconsider_clip_changes (w, buffer);
12769
12770 /* Has the mode line to be updated? */
12771 update_mode_line = (!NILP (w->update_mode_line)
12772 || update_mode_lines
12773 || buffer->clip_changed
12774 || buffer->prevent_redisplay_optimizations_p);
12775
12776 if (MINI_WINDOW_P (w))
12777 {
12778 if (w == XWINDOW (echo_area_window)
12779 && !NILP (echo_area_buffer[0]))
12780 {
12781 if (update_mode_line)
12782 /* We may have to update a tty frame's menu bar or a
12783 tool-bar. Example `M-x C-h C-h C-g'. */
12784 goto finish_menu_bars;
12785 else
12786 /* We've already displayed the echo area glyphs in this window. */
12787 goto finish_scroll_bars;
12788 }
12789 else if ((w != XWINDOW (minibuf_window)
12790 || minibuf_level == 0)
12791 /* When buffer is nonempty, redisplay window normally. */
12792 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12793 /* Quail displays non-mini buffers in minibuffer window.
12794 In that case, redisplay the window normally. */
12795 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12796 {
12797 /* W is a mini-buffer window, but it's not active, so clear
12798 it. */
12799 int yb = window_text_bottom_y (w);
12800 struct glyph_row *row;
12801 int y;
12802
12803 for (y = 0, row = w->desired_matrix->rows;
12804 y < yb;
12805 y += row->height, ++row)
12806 blank_row (w, row, y);
12807 goto finish_scroll_bars;
12808 }
12809
12810 clear_glyph_matrix (w->desired_matrix);
12811 }
12812
12813 /* Otherwise set up data on this window; select its buffer and point
12814 value. */
12815 /* Really select the buffer, for the sake of buffer-local
12816 variables. */
12817 set_buffer_internal_1 (XBUFFER (w->buffer));
12818 SET_TEXT_POS (opoint, PT, PT_BYTE);
12819
12820 current_matrix_up_to_date_p
12821 = (!NILP (w->window_end_valid)
12822 && !current_buffer->clip_changed
12823 && !current_buffer->prevent_redisplay_optimizations_p
12824 && XFASTINT (w->last_modified) >= MODIFF
12825 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12826
12827 buffer_unchanged_p
12828 = (!NILP (w->window_end_valid)
12829 && !current_buffer->clip_changed
12830 && XFASTINT (w->last_modified) >= MODIFF
12831 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12832
12833 /* When windows_or_buffers_changed is non-zero, we can't rely on
12834 the window end being valid, so set it to nil there. */
12835 if (windows_or_buffers_changed)
12836 {
12837 /* If window starts on a continuation line, maybe adjust the
12838 window start in case the window's width changed. */
12839 if (XMARKER (w->start)->buffer == current_buffer)
12840 compute_window_start_on_continuation_line (w);
12841
12842 w->window_end_valid = Qnil;
12843 }
12844
12845 /* Some sanity checks. */
12846 CHECK_WINDOW_END (w);
12847 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12848 abort ();
12849 if (BYTEPOS (opoint) < CHARPOS (opoint))
12850 abort ();
12851
12852 /* If %c is in mode line, update it if needed. */
12853 if (!NILP (w->column_number_displayed)
12854 /* This alternative quickly identifies a common case
12855 where no change is needed. */
12856 && !(PT == XFASTINT (w->last_point)
12857 && XFASTINT (w->last_modified) >= MODIFF
12858 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12859 && (XFASTINT (w->column_number_displayed)
12860 != (int) current_column ())) /* iftc */
12861 update_mode_line = 1;
12862
12863 /* Count number of windows showing the selected buffer. An indirect
12864 buffer counts as its base buffer. */
12865 if (!just_this_one_p)
12866 {
12867 struct buffer *current_base, *window_base;
12868 current_base = current_buffer;
12869 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12870 if (current_base->base_buffer)
12871 current_base = current_base->base_buffer;
12872 if (window_base->base_buffer)
12873 window_base = window_base->base_buffer;
12874 if (current_base == window_base)
12875 buffer_shared++;
12876 }
12877
12878 /* Point refers normally to the selected window. For any other
12879 window, set up appropriate value. */
12880 if (!EQ (window, selected_window))
12881 {
12882 int new_pt = XMARKER (w->pointm)->charpos;
12883 int new_pt_byte = marker_byte_position (w->pointm);
12884 if (new_pt < BEGV)
12885 {
12886 new_pt = BEGV;
12887 new_pt_byte = BEGV_BYTE;
12888 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12889 }
12890 else if (new_pt > (ZV - 1))
12891 {
12892 new_pt = ZV;
12893 new_pt_byte = ZV_BYTE;
12894 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12895 }
12896
12897 /* We don't use SET_PT so that the point-motion hooks don't run. */
12898 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12899 }
12900
12901 /* If any of the character widths specified in the display table
12902 have changed, invalidate the width run cache. It's true that
12903 this may be a bit late to catch such changes, but the rest of
12904 redisplay goes (non-fatally) haywire when the display table is
12905 changed, so why should we worry about doing any better? */
12906 if (current_buffer->width_run_cache)
12907 {
12908 struct Lisp_Char_Table *disptab = buffer_display_table ();
12909
12910 if (! disptab_matches_widthtab (disptab,
12911 XVECTOR (current_buffer->width_table)))
12912 {
12913 invalidate_region_cache (current_buffer,
12914 current_buffer->width_run_cache,
12915 BEG, Z);
12916 recompute_width_table (current_buffer, disptab);
12917 }
12918 }
12919
12920 /* If window-start is screwed up, choose a new one. */
12921 if (XMARKER (w->start)->buffer != current_buffer)
12922 goto recenter;
12923
12924 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12925
12926 /* If someone specified a new starting point but did not insist,
12927 check whether it can be used. */
12928 if (!NILP (w->optional_new_start)
12929 && CHARPOS (startp) >= BEGV
12930 && CHARPOS (startp) <= ZV)
12931 {
12932 w->optional_new_start = Qnil;
12933 start_display (&it, w, startp);
12934 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12935 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12936 if (IT_CHARPOS (it) == PT)
12937 w->force_start = Qt;
12938 /* IT may overshoot PT if text at PT is invisible. */
12939 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12940 w->force_start = Qt;
12941 }
12942
12943 /* Handle case where place to start displaying has been specified,
12944 unless the specified location is outside the accessible range. */
12945 if (!NILP (w->force_start)
12946 || w->frozen_window_start_p)
12947 {
12948 /* We set this later on if we have to adjust point. */
12949 int new_vpos = -1;
12950 int val;
12951
12952 w->force_start = Qnil;
12953 w->vscroll = 0;
12954 w->window_end_valid = Qnil;
12955
12956 /* Forget any recorded base line for line number display. */
12957 if (!buffer_unchanged_p)
12958 w->base_line_number = Qnil;
12959
12960 /* Redisplay the mode line. Select the buffer properly for that.
12961 Also, run the hook window-scroll-functions
12962 because we have scrolled. */
12963 /* Note, we do this after clearing force_start because
12964 if there's an error, it is better to forget about force_start
12965 than to get into an infinite loop calling the hook functions
12966 and having them get more errors. */
12967 if (!update_mode_line
12968 || ! NILP (Vwindow_scroll_functions))
12969 {
12970 update_mode_line = 1;
12971 w->update_mode_line = Qt;
12972 startp = run_window_scroll_functions (window, startp);
12973 }
12974
12975 w->last_modified = make_number (0);
12976 w->last_overlay_modified = make_number (0);
12977 if (CHARPOS (startp) < BEGV)
12978 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12979 else if (CHARPOS (startp) > ZV)
12980 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12981
12982 /* Redisplay, then check if cursor has been set during the
12983 redisplay. Give up if new fonts were loaded. */
12984 val = try_window (window, startp, 1);
12985 if (!val)
12986 {
12987 w->force_start = Qt;
12988 clear_glyph_matrix (w->desired_matrix);
12989 goto need_larger_matrices;
12990 }
12991 /* Point was outside the scroll margins. */
12992 if (val < 0)
12993 new_vpos = window_box_height (w) / 2;
12994
12995 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12996 {
12997 /* If point does not appear, try to move point so it does
12998 appear. The desired matrix has been built above, so we
12999 can use it here. */
13000 new_vpos = window_box_height (w) / 2;
13001 }
13002
13003 if (!cursor_row_fully_visible_p (w, 0, 0))
13004 {
13005 /* Point does appear, but on a line partly visible at end of window.
13006 Move it back to a fully-visible line. */
13007 new_vpos = window_box_height (w);
13008 }
13009
13010 /* If we need to move point for either of the above reasons,
13011 now actually do it. */
13012 if (new_vpos >= 0)
13013 {
13014 struct glyph_row *row;
13015
13016 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13017 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13018 ++row;
13019
13020 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13021 MATRIX_ROW_START_BYTEPOS (row));
13022
13023 if (w != XWINDOW (selected_window))
13024 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13025 else if (current_buffer == old)
13026 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13027
13028 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13029
13030 /* If we are highlighting the region, then we just changed
13031 the region, so redisplay to show it. */
13032 if (!NILP (Vtransient_mark_mode)
13033 && !NILP (current_buffer->mark_active))
13034 {
13035 clear_glyph_matrix (w->desired_matrix);
13036 if (!try_window (window, startp, 0))
13037 goto need_larger_matrices;
13038 }
13039 }
13040
13041 #if GLYPH_DEBUG
13042 debug_method_add (w, "forced window start");
13043 #endif
13044 goto done;
13045 }
13046
13047 /* Handle case where text has not changed, only point, and it has
13048 not moved off the frame, and we are not retrying after hscroll.
13049 (current_matrix_up_to_date_p is nonzero when retrying.) */
13050 if (current_matrix_up_to_date_p
13051 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13052 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13053 {
13054 switch (rc)
13055 {
13056 case CURSOR_MOVEMENT_SUCCESS:
13057 used_current_matrix_p = 1;
13058 goto done;
13059
13060 #if 0 /* try_cursor_movement never returns this value. */
13061 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13062 goto need_larger_matrices;
13063 #endif
13064
13065 case CURSOR_MOVEMENT_MUST_SCROLL:
13066 goto try_to_scroll;
13067
13068 default:
13069 abort ();
13070 }
13071 }
13072 /* If current starting point was originally the beginning of a line
13073 but no longer is, find a new starting point. */
13074 else if (!NILP (w->start_at_line_beg)
13075 && !(CHARPOS (startp) <= BEGV
13076 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13077 {
13078 #if GLYPH_DEBUG
13079 debug_method_add (w, "recenter 1");
13080 #endif
13081 goto recenter;
13082 }
13083
13084 /* Try scrolling with try_window_id. Value is > 0 if update has
13085 been done, it is -1 if we know that the same window start will
13086 not work. It is 0 if unsuccessful for some other reason. */
13087 else if ((tem = try_window_id (w)) != 0)
13088 {
13089 #if GLYPH_DEBUG
13090 debug_method_add (w, "try_window_id %d", tem);
13091 #endif
13092
13093 if (fonts_changed_p)
13094 goto need_larger_matrices;
13095 if (tem > 0)
13096 goto done;
13097
13098 /* Otherwise try_window_id has returned -1 which means that we
13099 don't want the alternative below this comment to execute. */
13100 }
13101 else if (CHARPOS (startp) >= BEGV
13102 && CHARPOS (startp) <= ZV
13103 && PT >= CHARPOS (startp)
13104 && (CHARPOS (startp) < ZV
13105 /* Avoid starting at end of buffer. */
13106 || CHARPOS (startp) == BEGV
13107 || (XFASTINT (w->last_modified) >= MODIFF
13108 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13109 {
13110
13111 /* If first window line is a continuation line, and window start
13112 is inside the modified region, but the first change is before
13113 current window start, we must select a new window start.*/
13114 if (NILP (w->start_at_line_beg)
13115 && CHARPOS (startp) > BEGV)
13116 {
13117 /* Make sure beg_unchanged and end_unchanged are up to date.
13118 Do it only if buffer has really changed. This may or may
13119 not have been done by try_window_id (see which) already. */
13120 if (MODIFF > SAVE_MODIFF
13121 /* This seems to happen sometimes after saving a buffer. */
13122 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13123 {
13124 if (GPT - BEG < BEG_UNCHANGED)
13125 BEG_UNCHANGED = GPT - BEG;
13126 if (Z - GPT < END_UNCHANGED)
13127 END_UNCHANGED = Z - GPT;
13128 }
13129
13130 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13131 && CHARPOS (startp) <= Z - END_UNCHANGED)
13132 {
13133 /* There doesn't seems to be a simple way to find a new
13134 window start that is near the old window start, so
13135 we just recenter. */
13136 goto recenter;
13137 }
13138 }
13139
13140 #if GLYPH_DEBUG
13141 debug_method_add (w, "same window start");
13142 #endif
13143
13144 /* Try to redisplay starting at same place as before.
13145 If point has not moved off frame, accept the results. */
13146 if (!current_matrix_up_to_date_p
13147 /* Don't use try_window_reusing_current_matrix in this case
13148 because a window scroll function can have changed the
13149 buffer. */
13150 || !NILP (Vwindow_scroll_functions)
13151 || MINI_WINDOW_P (w)
13152 || !(used_current_matrix_p
13153 = try_window_reusing_current_matrix (w)))
13154 {
13155 IF_DEBUG (debug_method_add (w, "1"));
13156 if (try_window (window, startp, 1) < 0)
13157 /* -1 means we need to scroll.
13158 0 means we need new matrices, but fonts_changed_p
13159 is set in that case, so we will detect it below. */
13160 goto try_to_scroll;
13161 }
13162
13163 if (fonts_changed_p)
13164 goto need_larger_matrices;
13165
13166 if (w->cursor.vpos >= 0)
13167 {
13168 if (!just_this_one_p
13169 || current_buffer->clip_changed
13170 || BEG_UNCHANGED < CHARPOS (startp))
13171 /* Forget any recorded base line for line number display. */
13172 w->base_line_number = Qnil;
13173
13174 if (!cursor_row_fully_visible_p (w, 1, 0))
13175 {
13176 clear_glyph_matrix (w->desired_matrix);
13177 last_line_misfit = 1;
13178 }
13179 /* Drop through and scroll. */
13180 else
13181 goto done;
13182 }
13183 else
13184 clear_glyph_matrix (w->desired_matrix);
13185 }
13186
13187 try_to_scroll:
13188
13189 w->last_modified = make_number (0);
13190 w->last_overlay_modified = make_number (0);
13191
13192 /* Redisplay the mode line. Select the buffer properly for that. */
13193 if (!update_mode_line)
13194 {
13195 update_mode_line = 1;
13196 w->update_mode_line = Qt;
13197 }
13198
13199 /* Try to scroll by specified few lines. */
13200 if ((scroll_conservatively
13201 || scroll_step
13202 || temp_scroll_step
13203 || NUMBERP (current_buffer->scroll_up_aggressively)
13204 || NUMBERP (current_buffer->scroll_down_aggressively))
13205 && !current_buffer->clip_changed
13206 && CHARPOS (startp) >= BEGV
13207 && CHARPOS (startp) <= ZV)
13208 {
13209 /* The function returns -1 if new fonts were loaded, 1 if
13210 successful, 0 if not successful. */
13211 int rc = try_scrolling (window, just_this_one_p,
13212 scroll_conservatively,
13213 scroll_step,
13214 temp_scroll_step, last_line_misfit);
13215 switch (rc)
13216 {
13217 case SCROLLING_SUCCESS:
13218 goto done;
13219
13220 case SCROLLING_NEED_LARGER_MATRICES:
13221 goto need_larger_matrices;
13222
13223 case SCROLLING_FAILED:
13224 break;
13225
13226 default:
13227 abort ();
13228 }
13229 }
13230
13231 /* Finally, just choose place to start which centers point */
13232
13233 recenter:
13234 if (centering_position < 0)
13235 centering_position = window_box_height (w) / 2;
13236
13237 #if GLYPH_DEBUG
13238 debug_method_add (w, "recenter");
13239 #endif
13240
13241 /* w->vscroll = 0; */
13242
13243 /* Forget any previously recorded base line for line number display. */
13244 if (!buffer_unchanged_p)
13245 w->base_line_number = Qnil;
13246
13247 /* Move backward half the height of the window. */
13248 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13249 it.current_y = it.last_visible_y;
13250 move_it_vertically_backward (&it, centering_position);
13251 xassert (IT_CHARPOS (it) >= BEGV);
13252
13253 /* The function move_it_vertically_backward may move over more
13254 than the specified y-distance. If it->w is small, e.g. a
13255 mini-buffer window, we may end up in front of the window's
13256 display area. Start displaying at the start of the line
13257 containing PT in this case. */
13258 if (it.current_y <= 0)
13259 {
13260 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13261 move_it_vertically_backward (&it, 0);
13262 #if 0
13263 /* I think this assert is bogus if buffer contains
13264 invisible text or images. KFS. */
13265 xassert (IT_CHARPOS (it) <= PT);
13266 #endif
13267 it.current_y = 0;
13268 }
13269
13270 it.current_x = it.hpos = 0;
13271
13272 /* Set startp here explicitly in case that helps avoid an infinite loop
13273 in case the window-scroll-functions functions get errors. */
13274 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13275
13276 /* Run scroll hooks. */
13277 startp = run_window_scroll_functions (window, it.current.pos);
13278
13279 /* Redisplay the window. */
13280 if (!current_matrix_up_to_date_p
13281 || windows_or_buffers_changed
13282 || cursor_type_changed
13283 /* Don't use try_window_reusing_current_matrix in this case
13284 because it can have changed the buffer. */
13285 || !NILP (Vwindow_scroll_functions)
13286 || !just_this_one_p
13287 || MINI_WINDOW_P (w)
13288 || !(used_current_matrix_p
13289 = try_window_reusing_current_matrix (w)))
13290 try_window (window, startp, 0);
13291
13292 /* If new fonts have been loaded (due to fontsets), give up. We
13293 have to start a new redisplay since we need to re-adjust glyph
13294 matrices. */
13295 if (fonts_changed_p)
13296 goto need_larger_matrices;
13297
13298 /* If cursor did not appear assume that the middle of the window is
13299 in the first line of the window. Do it again with the next line.
13300 (Imagine a window of height 100, displaying two lines of height
13301 60. Moving back 50 from it->last_visible_y will end in the first
13302 line.) */
13303 if (w->cursor.vpos < 0)
13304 {
13305 if (!NILP (w->window_end_valid)
13306 && PT >= Z - XFASTINT (w->window_end_pos))
13307 {
13308 clear_glyph_matrix (w->desired_matrix);
13309 move_it_by_lines (&it, 1, 0);
13310 try_window (window, it.current.pos, 0);
13311 }
13312 else if (PT < IT_CHARPOS (it))
13313 {
13314 clear_glyph_matrix (w->desired_matrix);
13315 move_it_by_lines (&it, -1, 0);
13316 try_window (window, it.current.pos, 0);
13317 }
13318 else
13319 {
13320 /* Not much we can do about it. */
13321 }
13322 }
13323
13324 /* Consider the following case: Window starts at BEGV, there is
13325 invisible, intangible text at BEGV, so that display starts at
13326 some point START > BEGV. It can happen that we are called with
13327 PT somewhere between BEGV and START. Try to handle that case. */
13328 if (w->cursor.vpos < 0)
13329 {
13330 struct glyph_row *row = w->current_matrix->rows;
13331 if (row->mode_line_p)
13332 ++row;
13333 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13334 }
13335
13336 if (!cursor_row_fully_visible_p (w, 0, 0))
13337 {
13338 /* If vscroll is enabled, disable it and try again. */
13339 if (w->vscroll)
13340 {
13341 w->vscroll = 0;
13342 clear_glyph_matrix (w->desired_matrix);
13343 goto recenter;
13344 }
13345
13346 /* If centering point failed to make the whole line visible,
13347 put point at the top instead. That has to make the whole line
13348 visible, if it can be done. */
13349 if (centering_position == 0)
13350 goto done;
13351
13352 clear_glyph_matrix (w->desired_matrix);
13353 centering_position = 0;
13354 goto recenter;
13355 }
13356
13357 done:
13358
13359 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13360 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13361 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13362 ? Qt : Qnil);
13363
13364 /* Display the mode line, if we must. */
13365 if ((update_mode_line
13366 /* If window not full width, must redo its mode line
13367 if (a) the window to its side is being redone and
13368 (b) we do a frame-based redisplay. This is a consequence
13369 of how inverted lines are drawn in frame-based redisplay. */
13370 || (!just_this_one_p
13371 && !FRAME_WINDOW_P (f)
13372 && !WINDOW_FULL_WIDTH_P (w))
13373 /* Line number to display. */
13374 || INTEGERP (w->base_line_pos)
13375 /* Column number is displayed and different from the one displayed. */
13376 || (!NILP (w->column_number_displayed)
13377 && (XFASTINT (w->column_number_displayed)
13378 != (int) current_column ()))) /* iftc */
13379 /* This means that the window has a mode line. */
13380 && (WINDOW_WANTS_MODELINE_P (w)
13381 || WINDOW_WANTS_HEADER_LINE_P (w)))
13382 {
13383 display_mode_lines (w);
13384
13385 /* If mode line height has changed, arrange for a thorough
13386 immediate redisplay using the correct mode line height. */
13387 if (WINDOW_WANTS_MODELINE_P (w)
13388 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13389 {
13390 fonts_changed_p = 1;
13391 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13392 = DESIRED_MODE_LINE_HEIGHT (w);
13393 }
13394
13395 /* If top line height has changed, arrange for a thorough
13396 immediate redisplay using the correct mode line height. */
13397 if (WINDOW_WANTS_HEADER_LINE_P (w)
13398 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13399 {
13400 fonts_changed_p = 1;
13401 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13402 = DESIRED_HEADER_LINE_HEIGHT (w);
13403 }
13404
13405 if (fonts_changed_p)
13406 goto need_larger_matrices;
13407 }
13408
13409 if (!line_number_displayed
13410 && !BUFFERP (w->base_line_pos))
13411 {
13412 w->base_line_pos = Qnil;
13413 w->base_line_number = Qnil;
13414 }
13415
13416 finish_menu_bars:
13417
13418 /* When we reach a frame's selected window, redo the frame's menu bar. */
13419 if (update_mode_line
13420 && EQ (FRAME_SELECTED_WINDOW (f), window))
13421 {
13422 int redisplay_menu_p = 0;
13423 int redisplay_tool_bar_p = 0;
13424
13425 if (FRAME_WINDOW_P (f))
13426 {
13427 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13428 || defined (USE_GTK)
13429 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13430 #else
13431 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13432 #endif
13433 }
13434 else
13435 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13436
13437 if (redisplay_menu_p)
13438 display_menu_bar (w);
13439
13440 #ifdef HAVE_WINDOW_SYSTEM
13441 #ifdef USE_GTK
13442 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13443 #else
13444 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13445 && (FRAME_TOOL_BAR_LINES (f) > 0
13446 || auto_resize_tool_bars_p);
13447
13448 #endif
13449
13450 if (redisplay_tool_bar_p)
13451 redisplay_tool_bar (f);
13452 #endif
13453 }
13454
13455 #ifdef HAVE_WINDOW_SYSTEM
13456 if (FRAME_WINDOW_P (f)
13457 && update_window_fringes (w, (just_this_one_p
13458 || (!used_current_matrix_p && !overlay_arrow_seen)
13459 || w->pseudo_window_p)))
13460 {
13461 update_begin (f);
13462 BLOCK_INPUT;
13463 if (draw_window_fringes (w, 1))
13464 x_draw_vertical_border (w);
13465 UNBLOCK_INPUT;
13466 update_end (f);
13467 }
13468 #endif /* HAVE_WINDOW_SYSTEM */
13469
13470 /* We go to this label, with fonts_changed_p nonzero,
13471 if it is necessary to try again using larger glyph matrices.
13472 We have to redeem the scroll bar even in this case,
13473 because the loop in redisplay_internal expects that. */
13474 need_larger_matrices:
13475 ;
13476 finish_scroll_bars:
13477
13478 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13479 {
13480 /* Set the thumb's position and size. */
13481 set_vertical_scroll_bar (w);
13482
13483 /* Note that we actually used the scroll bar attached to this
13484 window, so it shouldn't be deleted at the end of redisplay. */
13485 redeem_scroll_bar_hook (w);
13486 }
13487
13488 /* Restore current_buffer and value of point in it. */
13489 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13490 set_buffer_internal_1 (old);
13491 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13492
13493 unbind_to (count, Qnil);
13494 }
13495
13496
13497 /* Build the complete desired matrix of WINDOW with a window start
13498 buffer position POS.
13499
13500 Value is 1 if successful. It is zero if fonts were loaded during
13501 redisplay which makes re-adjusting glyph matrices necessary, and -1
13502 if point would appear in the scroll margins.
13503 (We check that only if CHECK_MARGINS is nonzero. */
13504
13505 int
13506 try_window (window, pos, check_margins)
13507 Lisp_Object window;
13508 struct text_pos pos;
13509 int check_margins;
13510 {
13511 struct window *w = XWINDOW (window);
13512 struct it it;
13513 struct glyph_row *last_text_row = NULL;
13514
13515 /* Make POS the new window start. */
13516 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13517
13518 /* Mark cursor position as unknown. No overlay arrow seen. */
13519 w->cursor.vpos = -1;
13520 overlay_arrow_seen = 0;
13521
13522 /* Initialize iterator and info to start at POS. */
13523 start_display (&it, w, pos);
13524
13525 /* Display all lines of W. */
13526 while (it.current_y < it.last_visible_y)
13527 {
13528 if (display_line (&it))
13529 last_text_row = it.glyph_row - 1;
13530 if (fonts_changed_p)
13531 return 0;
13532 }
13533
13534 /* Don't let the cursor end in the scroll margins. */
13535 if (check_margins
13536 && !MINI_WINDOW_P (w))
13537 {
13538 int this_scroll_margin;
13539
13540 this_scroll_margin = max (0, scroll_margin);
13541 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13542 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13543
13544 if ((w->cursor.y >= 0 /* not vscrolled */
13545 && w->cursor.y < this_scroll_margin
13546 && CHARPOS (pos) > BEGV
13547 && IT_CHARPOS (it) < ZV)
13548 /* rms: considering make_cursor_line_fully_visible_p here
13549 seems to give wrong results. We don't want to recenter
13550 when the last line is partly visible, we want to allow
13551 that case to be handled in the usual way. */
13552 || (w->cursor.y + 1) > it.last_visible_y)
13553 {
13554 w->cursor.vpos = -1;
13555 clear_glyph_matrix (w->desired_matrix);
13556 return -1;
13557 }
13558 }
13559
13560 /* If bottom moved off end of frame, change mode line percentage. */
13561 if (XFASTINT (w->window_end_pos) <= 0
13562 && Z != IT_CHARPOS (it))
13563 w->update_mode_line = Qt;
13564
13565 /* Set window_end_pos to the offset of the last character displayed
13566 on the window from the end of current_buffer. Set
13567 window_end_vpos to its row number. */
13568 if (last_text_row)
13569 {
13570 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13571 w->window_end_bytepos
13572 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13573 w->window_end_pos
13574 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13575 w->window_end_vpos
13576 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13577 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13578 ->displays_text_p);
13579 }
13580 else
13581 {
13582 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13583 w->window_end_pos = make_number (Z - ZV);
13584 w->window_end_vpos = make_number (0);
13585 }
13586
13587 /* But that is not valid info until redisplay finishes. */
13588 w->window_end_valid = Qnil;
13589 return 1;
13590 }
13591
13592
13593 \f
13594 /************************************************************************
13595 Window redisplay reusing current matrix when buffer has not changed
13596 ************************************************************************/
13597
13598 /* Try redisplay of window W showing an unchanged buffer with a
13599 different window start than the last time it was displayed by
13600 reusing its current matrix. Value is non-zero if successful.
13601 W->start is the new window start. */
13602
13603 static int
13604 try_window_reusing_current_matrix (w)
13605 struct window *w;
13606 {
13607 struct frame *f = XFRAME (w->frame);
13608 struct glyph_row *row, *bottom_row;
13609 struct it it;
13610 struct run run;
13611 struct text_pos start, new_start;
13612 int nrows_scrolled, i;
13613 struct glyph_row *last_text_row;
13614 struct glyph_row *last_reused_text_row;
13615 struct glyph_row *start_row;
13616 int start_vpos, min_y, max_y;
13617
13618 #if GLYPH_DEBUG
13619 if (inhibit_try_window_reusing)
13620 return 0;
13621 #endif
13622
13623 if (/* This function doesn't handle terminal frames. */
13624 !FRAME_WINDOW_P (f)
13625 /* Don't try to reuse the display if windows have been split
13626 or such. */
13627 || windows_or_buffers_changed
13628 || cursor_type_changed)
13629 return 0;
13630
13631 /* Can't do this if region may have changed. */
13632 if ((!NILP (Vtransient_mark_mode)
13633 && !NILP (current_buffer->mark_active))
13634 || !NILP (w->region_showing)
13635 || !NILP (Vshow_trailing_whitespace))
13636 return 0;
13637
13638 /* If top-line visibility has changed, give up. */
13639 if (WINDOW_WANTS_HEADER_LINE_P (w)
13640 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13641 return 0;
13642
13643 /* Give up if old or new display is scrolled vertically. We could
13644 make this function handle this, but right now it doesn't. */
13645 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13646 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13647 return 0;
13648
13649 /* The variable new_start now holds the new window start. The old
13650 start `start' can be determined from the current matrix. */
13651 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13652 start = start_row->start.pos;
13653 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13654
13655 /* Clear the desired matrix for the display below. */
13656 clear_glyph_matrix (w->desired_matrix);
13657
13658 if (CHARPOS (new_start) <= CHARPOS (start))
13659 {
13660 int first_row_y;
13661
13662 /* Don't use this method if the display starts with an ellipsis
13663 displayed for invisible text. It's not easy to handle that case
13664 below, and it's certainly not worth the effort since this is
13665 not a frequent case. */
13666 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13667 return 0;
13668
13669 IF_DEBUG (debug_method_add (w, "twu1"));
13670
13671 /* Display up to a row that can be reused. The variable
13672 last_text_row is set to the last row displayed that displays
13673 text. Note that it.vpos == 0 if or if not there is a
13674 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13675 start_display (&it, w, new_start);
13676 first_row_y = it.current_y;
13677 w->cursor.vpos = -1;
13678 last_text_row = last_reused_text_row = NULL;
13679
13680 while (it.current_y < it.last_visible_y
13681 && !fonts_changed_p)
13682 {
13683 /* If we have reached into the characters in the START row,
13684 that means the line boundaries have changed. So we
13685 can't start copying with the row START. Maybe it will
13686 work to start copying with the following row. */
13687 while (IT_CHARPOS (it) > CHARPOS (start))
13688 {
13689 /* Advance to the next row as the "start". */
13690 start_row++;
13691 start = start_row->start.pos;
13692 /* If there are no more rows to try, or just one, give up. */
13693 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13694 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13695 || CHARPOS (start) == ZV)
13696 {
13697 clear_glyph_matrix (w->desired_matrix);
13698 return 0;
13699 }
13700
13701 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13702 }
13703 /* If we have reached alignment,
13704 we can copy the rest of the rows. */
13705 if (IT_CHARPOS (it) == CHARPOS (start))
13706 break;
13707
13708 if (display_line (&it))
13709 last_text_row = it.glyph_row - 1;
13710 }
13711
13712 /* A value of current_y < last_visible_y means that we stopped
13713 at the previous window start, which in turn means that we
13714 have at least one reusable row. */
13715 if (it.current_y < it.last_visible_y)
13716 {
13717 /* IT.vpos always starts from 0; it counts text lines. */
13718 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13719
13720 /* Find PT if not already found in the lines displayed. */
13721 if (w->cursor.vpos < 0)
13722 {
13723 int dy = it.current_y - start_row->y;
13724
13725 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13726 row = row_containing_pos (w, PT, row, NULL, dy);
13727 if (row)
13728 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13729 dy, nrows_scrolled);
13730 else
13731 {
13732 clear_glyph_matrix (w->desired_matrix);
13733 return 0;
13734 }
13735 }
13736
13737 /* Scroll the display. Do it before the current matrix is
13738 changed. The problem here is that update has not yet
13739 run, i.e. part of the current matrix is not up to date.
13740 scroll_run_hook will clear the cursor, and use the
13741 current matrix to get the height of the row the cursor is
13742 in. */
13743 run.current_y = start_row->y;
13744 run.desired_y = it.current_y;
13745 run.height = it.last_visible_y - it.current_y;
13746
13747 if (run.height > 0 && run.current_y != run.desired_y)
13748 {
13749 update_begin (f);
13750 rif->update_window_begin_hook (w);
13751 rif->clear_window_mouse_face (w);
13752 rif->scroll_run_hook (w, &run);
13753 rif->update_window_end_hook (w, 0, 0);
13754 update_end (f);
13755 }
13756
13757 /* Shift current matrix down by nrows_scrolled lines. */
13758 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13759 rotate_matrix (w->current_matrix,
13760 start_vpos,
13761 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13762 nrows_scrolled);
13763
13764 /* Disable lines that must be updated. */
13765 for (i = 0; i < it.vpos; ++i)
13766 (start_row + i)->enabled_p = 0;
13767
13768 /* Re-compute Y positions. */
13769 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13770 max_y = it.last_visible_y;
13771 for (row = start_row + nrows_scrolled;
13772 row < bottom_row;
13773 ++row)
13774 {
13775 row->y = it.current_y;
13776 row->visible_height = row->height;
13777
13778 if (row->y < min_y)
13779 row->visible_height -= min_y - row->y;
13780 if (row->y + row->height > max_y)
13781 row->visible_height -= row->y + row->height - max_y;
13782 row->redraw_fringe_bitmaps_p = 1;
13783
13784 it.current_y += row->height;
13785
13786 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13787 last_reused_text_row = row;
13788 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13789 break;
13790 }
13791
13792 /* Disable lines in the current matrix which are now
13793 below the window. */
13794 for (++row; row < bottom_row; ++row)
13795 row->enabled_p = row->mode_line_p = 0;
13796 }
13797
13798 /* Update window_end_pos etc.; last_reused_text_row is the last
13799 reused row from the current matrix containing text, if any.
13800 The value of last_text_row is the last displayed line
13801 containing text. */
13802 if (last_reused_text_row)
13803 {
13804 w->window_end_bytepos
13805 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13806 w->window_end_pos
13807 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13808 w->window_end_vpos
13809 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13810 w->current_matrix));
13811 }
13812 else if (last_text_row)
13813 {
13814 w->window_end_bytepos
13815 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13816 w->window_end_pos
13817 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13818 w->window_end_vpos
13819 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13820 }
13821 else
13822 {
13823 /* This window must be completely empty. */
13824 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13825 w->window_end_pos = make_number (Z - ZV);
13826 w->window_end_vpos = make_number (0);
13827 }
13828 w->window_end_valid = Qnil;
13829
13830 /* Update hint: don't try scrolling again in update_window. */
13831 w->desired_matrix->no_scrolling_p = 1;
13832
13833 #if GLYPH_DEBUG
13834 debug_method_add (w, "try_window_reusing_current_matrix 1");
13835 #endif
13836 return 1;
13837 }
13838 else if (CHARPOS (new_start) > CHARPOS (start))
13839 {
13840 struct glyph_row *pt_row, *row;
13841 struct glyph_row *first_reusable_row;
13842 struct glyph_row *first_row_to_display;
13843 int dy;
13844 int yb = window_text_bottom_y (w);
13845
13846 /* Find the row starting at new_start, if there is one. Don't
13847 reuse a partially visible line at the end. */
13848 first_reusable_row = start_row;
13849 while (first_reusable_row->enabled_p
13850 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13851 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13852 < CHARPOS (new_start)))
13853 ++first_reusable_row;
13854
13855 /* Give up if there is no row to reuse. */
13856 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13857 || !first_reusable_row->enabled_p
13858 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13859 != CHARPOS (new_start)))
13860 return 0;
13861
13862 /* We can reuse fully visible rows beginning with
13863 first_reusable_row to the end of the window. Set
13864 first_row_to_display to the first row that cannot be reused.
13865 Set pt_row to the row containing point, if there is any. */
13866 pt_row = NULL;
13867 for (first_row_to_display = first_reusable_row;
13868 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13869 ++first_row_to_display)
13870 {
13871 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13872 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13873 pt_row = first_row_to_display;
13874 }
13875
13876 /* Start displaying at the start of first_row_to_display. */
13877 xassert (first_row_to_display->y < yb);
13878 init_to_row_start (&it, w, first_row_to_display);
13879
13880 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13881 - start_vpos);
13882 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13883 - nrows_scrolled);
13884 it.current_y = (first_row_to_display->y - first_reusable_row->y
13885 + WINDOW_HEADER_LINE_HEIGHT (w));
13886
13887 /* Display lines beginning with first_row_to_display in the
13888 desired matrix. Set last_text_row to the last row displayed
13889 that displays text. */
13890 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13891 if (pt_row == NULL)
13892 w->cursor.vpos = -1;
13893 last_text_row = NULL;
13894 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13895 if (display_line (&it))
13896 last_text_row = it.glyph_row - 1;
13897
13898 /* Give up If point isn't in a row displayed or reused. */
13899 if (w->cursor.vpos < 0)
13900 {
13901 clear_glyph_matrix (w->desired_matrix);
13902 return 0;
13903 }
13904
13905 /* If point is in a reused row, adjust y and vpos of the cursor
13906 position. */
13907 if (pt_row)
13908 {
13909 w->cursor.vpos -= nrows_scrolled;
13910 w->cursor.y -= first_reusable_row->y - start_row->y;
13911 }
13912
13913 /* Scroll the display. */
13914 run.current_y = first_reusable_row->y;
13915 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13916 run.height = it.last_visible_y - run.current_y;
13917 dy = run.current_y - run.desired_y;
13918
13919 if (run.height)
13920 {
13921 update_begin (f);
13922 rif->update_window_begin_hook (w);
13923 rif->clear_window_mouse_face (w);
13924 rif->scroll_run_hook (w, &run);
13925 rif->update_window_end_hook (w, 0, 0);
13926 update_end (f);
13927 }
13928
13929 /* Adjust Y positions of reused rows. */
13930 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13931 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13932 max_y = it.last_visible_y;
13933 for (row = first_reusable_row; row < first_row_to_display; ++row)
13934 {
13935 row->y -= dy;
13936 row->visible_height = row->height;
13937 if (row->y < min_y)
13938 row->visible_height -= min_y - row->y;
13939 if (row->y + row->height > max_y)
13940 row->visible_height -= row->y + row->height - max_y;
13941 row->redraw_fringe_bitmaps_p = 1;
13942 }
13943
13944 /* Scroll the current matrix. */
13945 xassert (nrows_scrolled > 0);
13946 rotate_matrix (w->current_matrix,
13947 start_vpos,
13948 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13949 -nrows_scrolled);
13950
13951 /* Disable rows not reused. */
13952 for (row -= nrows_scrolled; row < bottom_row; ++row)
13953 row->enabled_p = 0;
13954
13955 /* Point may have moved to a different line, so we cannot assume that
13956 the previous cursor position is valid; locate the correct row. */
13957 if (pt_row)
13958 {
13959 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13960 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13961 row++)
13962 {
13963 w->cursor.vpos++;
13964 w->cursor.y = row->y;
13965 }
13966 if (row < bottom_row)
13967 {
13968 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13969 while (glyph->charpos < PT)
13970 {
13971 w->cursor.hpos++;
13972 w->cursor.x += glyph->pixel_width;
13973 glyph++;
13974 }
13975 }
13976 }
13977
13978 /* Adjust window end. A null value of last_text_row means that
13979 the window end is in reused rows which in turn means that
13980 only its vpos can have changed. */
13981 if (last_text_row)
13982 {
13983 w->window_end_bytepos
13984 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13985 w->window_end_pos
13986 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13987 w->window_end_vpos
13988 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13989 }
13990 else
13991 {
13992 w->window_end_vpos
13993 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13994 }
13995
13996 w->window_end_valid = Qnil;
13997 w->desired_matrix->no_scrolling_p = 1;
13998
13999 #if GLYPH_DEBUG
14000 debug_method_add (w, "try_window_reusing_current_matrix 2");
14001 #endif
14002 return 1;
14003 }
14004
14005 return 0;
14006 }
14007
14008
14009 \f
14010 /************************************************************************
14011 Window redisplay reusing current matrix when buffer has changed
14012 ************************************************************************/
14013
14014 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14015 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14016 int *, int *));
14017 static struct glyph_row *
14018 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14019 struct glyph_row *));
14020
14021
14022 /* Return the last row in MATRIX displaying text. If row START is
14023 non-null, start searching with that row. IT gives the dimensions
14024 of the display. Value is null if matrix is empty; otherwise it is
14025 a pointer to the row found. */
14026
14027 static struct glyph_row *
14028 find_last_row_displaying_text (matrix, it, start)
14029 struct glyph_matrix *matrix;
14030 struct it *it;
14031 struct glyph_row *start;
14032 {
14033 struct glyph_row *row, *row_found;
14034
14035 /* Set row_found to the last row in IT->w's current matrix
14036 displaying text. The loop looks funny but think of partially
14037 visible lines. */
14038 row_found = NULL;
14039 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14040 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14041 {
14042 xassert (row->enabled_p);
14043 row_found = row;
14044 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14045 break;
14046 ++row;
14047 }
14048
14049 return row_found;
14050 }
14051
14052
14053 /* Return the last row in the current matrix of W that is not affected
14054 by changes at the start of current_buffer that occurred since W's
14055 current matrix was built. Value is null if no such row exists.
14056
14057 BEG_UNCHANGED us the number of characters unchanged at the start of
14058 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14059 first changed character in current_buffer. Characters at positions <
14060 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14061 when the current matrix was built. */
14062
14063 static struct glyph_row *
14064 find_last_unchanged_at_beg_row (w)
14065 struct window *w;
14066 {
14067 int first_changed_pos = BEG + BEG_UNCHANGED;
14068 struct glyph_row *row;
14069 struct glyph_row *row_found = NULL;
14070 int yb = window_text_bottom_y (w);
14071
14072 /* Find the last row displaying unchanged text. */
14073 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14074 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14075 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14076 {
14077 if (/* If row ends before first_changed_pos, it is unchanged,
14078 except in some case. */
14079 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14080 /* When row ends in ZV and we write at ZV it is not
14081 unchanged. */
14082 && !row->ends_at_zv_p
14083 /* When first_changed_pos is the end of a continued line,
14084 row is not unchanged because it may be no longer
14085 continued. */
14086 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14087 && (row->continued_p
14088 || row->exact_window_width_line_p)))
14089 row_found = row;
14090
14091 /* Stop if last visible row. */
14092 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14093 break;
14094
14095 ++row;
14096 }
14097
14098 return row_found;
14099 }
14100
14101
14102 /* Find the first glyph row in the current matrix of W that is not
14103 affected by changes at the end of current_buffer since the
14104 time W's current matrix was built.
14105
14106 Return in *DELTA the number of chars by which buffer positions in
14107 unchanged text at the end of current_buffer must be adjusted.
14108
14109 Return in *DELTA_BYTES the corresponding number of bytes.
14110
14111 Value is null if no such row exists, i.e. all rows are affected by
14112 changes. */
14113
14114 static struct glyph_row *
14115 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14116 struct window *w;
14117 int *delta, *delta_bytes;
14118 {
14119 struct glyph_row *row;
14120 struct glyph_row *row_found = NULL;
14121
14122 *delta = *delta_bytes = 0;
14123
14124 /* Display must not have been paused, otherwise the current matrix
14125 is not up to date. */
14126 if (NILP (w->window_end_valid))
14127 abort ();
14128
14129 /* A value of window_end_pos >= END_UNCHANGED means that the window
14130 end is in the range of changed text. If so, there is no
14131 unchanged row at the end of W's current matrix. */
14132 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14133 return NULL;
14134
14135 /* Set row to the last row in W's current matrix displaying text. */
14136 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14137
14138 /* If matrix is entirely empty, no unchanged row exists. */
14139 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14140 {
14141 /* The value of row is the last glyph row in the matrix having a
14142 meaningful buffer position in it. The end position of row
14143 corresponds to window_end_pos. This allows us to translate
14144 buffer positions in the current matrix to current buffer
14145 positions for characters not in changed text. */
14146 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14147 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14148 int last_unchanged_pos, last_unchanged_pos_old;
14149 struct glyph_row *first_text_row
14150 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14151
14152 *delta = Z - Z_old;
14153 *delta_bytes = Z_BYTE - Z_BYTE_old;
14154
14155 /* Set last_unchanged_pos to the buffer position of the last
14156 character in the buffer that has not been changed. Z is the
14157 index + 1 of the last character in current_buffer, i.e. by
14158 subtracting END_UNCHANGED we get the index of the last
14159 unchanged character, and we have to add BEG to get its buffer
14160 position. */
14161 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14162 last_unchanged_pos_old = last_unchanged_pos - *delta;
14163
14164 /* Search backward from ROW for a row displaying a line that
14165 starts at a minimum position >= last_unchanged_pos_old. */
14166 for (; row > first_text_row; --row)
14167 {
14168 /* This used to abort, but it can happen.
14169 It is ok to just stop the search instead here. KFS. */
14170 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14171 break;
14172
14173 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14174 row_found = row;
14175 }
14176 }
14177
14178 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14179 abort ();
14180
14181 return row_found;
14182 }
14183
14184
14185 /* Make sure that glyph rows in the current matrix of window W
14186 reference the same glyph memory as corresponding rows in the
14187 frame's frame matrix. This function is called after scrolling W's
14188 current matrix on a terminal frame in try_window_id and
14189 try_window_reusing_current_matrix. */
14190
14191 static void
14192 sync_frame_with_window_matrix_rows (w)
14193 struct window *w;
14194 {
14195 struct frame *f = XFRAME (w->frame);
14196 struct glyph_row *window_row, *window_row_end, *frame_row;
14197
14198 /* Preconditions: W must be a leaf window and full-width. Its frame
14199 must have a frame matrix. */
14200 xassert (NILP (w->hchild) && NILP (w->vchild));
14201 xassert (WINDOW_FULL_WIDTH_P (w));
14202 xassert (!FRAME_WINDOW_P (f));
14203
14204 /* If W is a full-width window, glyph pointers in W's current matrix
14205 have, by definition, to be the same as glyph pointers in the
14206 corresponding frame matrix. Note that frame matrices have no
14207 marginal areas (see build_frame_matrix). */
14208 window_row = w->current_matrix->rows;
14209 window_row_end = window_row + w->current_matrix->nrows;
14210 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14211 while (window_row < window_row_end)
14212 {
14213 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14214 struct glyph *end = window_row->glyphs[LAST_AREA];
14215
14216 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14217 frame_row->glyphs[TEXT_AREA] = start;
14218 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14219 frame_row->glyphs[LAST_AREA] = end;
14220
14221 /* Disable frame rows whose corresponding window rows have
14222 been disabled in try_window_id. */
14223 if (!window_row->enabled_p)
14224 frame_row->enabled_p = 0;
14225
14226 ++window_row, ++frame_row;
14227 }
14228 }
14229
14230
14231 /* Find the glyph row in window W containing CHARPOS. Consider all
14232 rows between START and END (not inclusive). END null means search
14233 all rows to the end of the display area of W. Value is the row
14234 containing CHARPOS or null. */
14235
14236 struct glyph_row *
14237 row_containing_pos (w, charpos, start, end, dy)
14238 struct window *w;
14239 int charpos;
14240 struct glyph_row *start, *end;
14241 int dy;
14242 {
14243 struct glyph_row *row = start;
14244 int last_y;
14245
14246 /* If we happen to start on a header-line, skip that. */
14247 if (row->mode_line_p)
14248 ++row;
14249
14250 if ((end && row >= end) || !row->enabled_p)
14251 return NULL;
14252
14253 last_y = window_text_bottom_y (w) - dy;
14254
14255 while (1)
14256 {
14257 /* Give up if we have gone too far. */
14258 if (end && row >= end)
14259 return NULL;
14260 /* This formerly returned if they were equal.
14261 I think that both quantities are of a "last plus one" type;
14262 if so, when they are equal, the row is within the screen. -- rms. */
14263 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14264 return NULL;
14265
14266 /* If it is in this row, return this row. */
14267 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14268 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14269 /* The end position of a row equals the start
14270 position of the next row. If CHARPOS is there, we
14271 would rather display it in the next line, except
14272 when this line ends in ZV. */
14273 && !row->ends_at_zv_p
14274 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14275 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14276 return row;
14277 ++row;
14278 }
14279 }
14280
14281
14282 /* Try to redisplay window W by reusing its existing display. W's
14283 current matrix must be up to date when this function is called,
14284 i.e. window_end_valid must not be nil.
14285
14286 Value is
14287
14288 1 if display has been updated
14289 0 if otherwise unsuccessful
14290 -1 if redisplay with same window start is known not to succeed
14291
14292 The following steps are performed:
14293
14294 1. Find the last row in the current matrix of W that is not
14295 affected by changes at the start of current_buffer. If no such row
14296 is found, give up.
14297
14298 2. Find the first row in W's current matrix that is not affected by
14299 changes at the end of current_buffer. Maybe there is no such row.
14300
14301 3. Display lines beginning with the row + 1 found in step 1 to the
14302 row found in step 2 or, if step 2 didn't find a row, to the end of
14303 the window.
14304
14305 4. If cursor is not known to appear on the window, give up.
14306
14307 5. If display stopped at the row found in step 2, scroll the
14308 display and current matrix as needed.
14309
14310 6. Maybe display some lines at the end of W, if we must. This can
14311 happen under various circumstances, like a partially visible line
14312 becoming fully visible, or because newly displayed lines are displayed
14313 in smaller font sizes.
14314
14315 7. Update W's window end information. */
14316
14317 static int
14318 try_window_id (w)
14319 struct window *w;
14320 {
14321 struct frame *f = XFRAME (w->frame);
14322 struct glyph_matrix *current_matrix = w->current_matrix;
14323 struct glyph_matrix *desired_matrix = w->desired_matrix;
14324 struct glyph_row *last_unchanged_at_beg_row;
14325 struct glyph_row *first_unchanged_at_end_row;
14326 struct glyph_row *row;
14327 struct glyph_row *bottom_row;
14328 int bottom_vpos;
14329 struct it it;
14330 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14331 struct text_pos start_pos;
14332 struct run run;
14333 int first_unchanged_at_end_vpos = 0;
14334 struct glyph_row *last_text_row, *last_text_row_at_end;
14335 struct text_pos start;
14336 int first_changed_charpos, last_changed_charpos;
14337
14338 #if GLYPH_DEBUG
14339 if (inhibit_try_window_id)
14340 return 0;
14341 #endif
14342
14343 /* This is handy for debugging. */
14344 #if 0
14345 #define GIVE_UP(X) \
14346 do { \
14347 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14348 return 0; \
14349 } while (0)
14350 #else
14351 #define GIVE_UP(X) return 0
14352 #endif
14353
14354 SET_TEXT_POS_FROM_MARKER (start, w->start);
14355
14356 /* Don't use this for mini-windows because these can show
14357 messages and mini-buffers, and we don't handle that here. */
14358 if (MINI_WINDOW_P (w))
14359 GIVE_UP (1);
14360
14361 /* This flag is used to prevent redisplay optimizations. */
14362 if (windows_or_buffers_changed || cursor_type_changed)
14363 GIVE_UP (2);
14364
14365 /* Verify that narrowing has not changed.
14366 Also verify that we were not told to prevent redisplay optimizations.
14367 It would be nice to further
14368 reduce the number of cases where this prevents try_window_id. */
14369 if (current_buffer->clip_changed
14370 || current_buffer->prevent_redisplay_optimizations_p)
14371 GIVE_UP (3);
14372
14373 /* Window must either use window-based redisplay or be full width. */
14374 if (!FRAME_WINDOW_P (f)
14375 && (!line_ins_del_ok
14376 || !WINDOW_FULL_WIDTH_P (w)))
14377 GIVE_UP (4);
14378
14379 /* Give up if point is not known NOT to appear in W. */
14380 if (PT < CHARPOS (start))
14381 GIVE_UP (5);
14382
14383 /* Another way to prevent redisplay optimizations. */
14384 if (XFASTINT (w->last_modified) == 0)
14385 GIVE_UP (6);
14386
14387 /* Verify that window is not hscrolled. */
14388 if (XFASTINT (w->hscroll) != 0)
14389 GIVE_UP (7);
14390
14391 /* Verify that display wasn't paused. */
14392 if (NILP (w->window_end_valid))
14393 GIVE_UP (8);
14394
14395 /* Can't use this if highlighting a region because a cursor movement
14396 will do more than just set the cursor. */
14397 if (!NILP (Vtransient_mark_mode)
14398 && !NILP (current_buffer->mark_active))
14399 GIVE_UP (9);
14400
14401 /* Likewise if highlighting trailing whitespace. */
14402 if (!NILP (Vshow_trailing_whitespace))
14403 GIVE_UP (11);
14404
14405 /* Likewise if showing a region. */
14406 if (!NILP (w->region_showing))
14407 GIVE_UP (10);
14408
14409 /* Can use this if overlay arrow position and or string have changed. */
14410 if (overlay_arrows_changed_p ())
14411 GIVE_UP (12);
14412
14413
14414 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14415 only if buffer has really changed. The reason is that the gap is
14416 initially at Z for freshly visited files. The code below would
14417 set end_unchanged to 0 in that case. */
14418 if (MODIFF > SAVE_MODIFF
14419 /* This seems to happen sometimes after saving a buffer. */
14420 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14421 {
14422 if (GPT - BEG < BEG_UNCHANGED)
14423 BEG_UNCHANGED = GPT - BEG;
14424 if (Z - GPT < END_UNCHANGED)
14425 END_UNCHANGED = Z - GPT;
14426 }
14427
14428 /* The position of the first and last character that has been changed. */
14429 first_changed_charpos = BEG + BEG_UNCHANGED;
14430 last_changed_charpos = Z - END_UNCHANGED;
14431
14432 /* If window starts after a line end, and the last change is in
14433 front of that newline, then changes don't affect the display.
14434 This case happens with stealth-fontification. Note that although
14435 the display is unchanged, glyph positions in the matrix have to
14436 be adjusted, of course. */
14437 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14438 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14439 && ((last_changed_charpos < CHARPOS (start)
14440 && CHARPOS (start) == BEGV)
14441 || (last_changed_charpos < CHARPOS (start) - 1
14442 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14443 {
14444 int Z_old, delta, Z_BYTE_old, delta_bytes;
14445 struct glyph_row *r0;
14446
14447 /* Compute how many chars/bytes have been added to or removed
14448 from the buffer. */
14449 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14450 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14451 delta = Z - Z_old;
14452 delta_bytes = Z_BYTE - Z_BYTE_old;
14453
14454 /* Give up if PT is not in the window. Note that it already has
14455 been checked at the start of try_window_id that PT is not in
14456 front of the window start. */
14457 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14458 GIVE_UP (13);
14459
14460 /* If window start is unchanged, we can reuse the whole matrix
14461 as is, after adjusting glyph positions. No need to compute
14462 the window end again, since its offset from Z hasn't changed. */
14463 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14464 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14465 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14466 /* PT must not be in a partially visible line. */
14467 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14468 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14469 {
14470 /* Adjust positions in the glyph matrix. */
14471 if (delta || delta_bytes)
14472 {
14473 struct glyph_row *r1
14474 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14475 increment_matrix_positions (w->current_matrix,
14476 MATRIX_ROW_VPOS (r0, current_matrix),
14477 MATRIX_ROW_VPOS (r1, current_matrix),
14478 delta, delta_bytes);
14479 }
14480
14481 /* Set the cursor. */
14482 row = row_containing_pos (w, PT, r0, NULL, 0);
14483 if (row)
14484 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14485 else
14486 abort ();
14487 return 1;
14488 }
14489 }
14490
14491 /* Handle the case that changes are all below what is displayed in
14492 the window, and that PT is in the window. This shortcut cannot
14493 be taken if ZV is visible in the window, and text has been added
14494 there that is visible in the window. */
14495 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14496 /* ZV is not visible in the window, or there are no
14497 changes at ZV, actually. */
14498 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14499 || first_changed_charpos == last_changed_charpos))
14500 {
14501 struct glyph_row *r0;
14502
14503 /* Give up if PT is not in the window. Note that it already has
14504 been checked at the start of try_window_id that PT is not in
14505 front of the window start. */
14506 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14507 GIVE_UP (14);
14508
14509 /* If window start is unchanged, we can reuse the whole matrix
14510 as is, without changing glyph positions since no text has
14511 been added/removed in front of the window end. */
14512 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14513 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14514 /* PT must not be in a partially visible line. */
14515 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14516 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14517 {
14518 /* We have to compute the window end anew since text
14519 can have been added/removed after it. */
14520 w->window_end_pos
14521 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14522 w->window_end_bytepos
14523 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14524
14525 /* Set the cursor. */
14526 row = row_containing_pos (w, PT, r0, NULL, 0);
14527 if (row)
14528 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14529 else
14530 abort ();
14531 return 2;
14532 }
14533 }
14534
14535 /* Give up if window start is in the changed area.
14536
14537 The condition used to read
14538
14539 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14540
14541 but why that was tested escapes me at the moment. */
14542 if (CHARPOS (start) >= first_changed_charpos
14543 && CHARPOS (start) <= last_changed_charpos)
14544 GIVE_UP (15);
14545
14546 /* Check that window start agrees with the start of the first glyph
14547 row in its current matrix. Check this after we know the window
14548 start is not in changed text, otherwise positions would not be
14549 comparable. */
14550 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14551 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14552 GIVE_UP (16);
14553
14554 /* Give up if the window ends in strings. Overlay strings
14555 at the end are difficult to handle, so don't try. */
14556 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14557 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14558 GIVE_UP (20);
14559
14560 /* Compute the position at which we have to start displaying new
14561 lines. Some of the lines at the top of the window might be
14562 reusable because they are not displaying changed text. Find the
14563 last row in W's current matrix not affected by changes at the
14564 start of current_buffer. Value is null if changes start in the
14565 first line of window. */
14566 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14567 if (last_unchanged_at_beg_row)
14568 {
14569 /* Avoid starting to display in the moddle of a character, a TAB
14570 for instance. This is easier than to set up the iterator
14571 exactly, and it's not a frequent case, so the additional
14572 effort wouldn't really pay off. */
14573 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14574 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14575 && last_unchanged_at_beg_row > w->current_matrix->rows)
14576 --last_unchanged_at_beg_row;
14577
14578 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14579 GIVE_UP (17);
14580
14581 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14582 GIVE_UP (18);
14583 start_pos = it.current.pos;
14584
14585 /* Start displaying new lines in the desired matrix at the same
14586 vpos we would use in the current matrix, i.e. below
14587 last_unchanged_at_beg_row. */
14588 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14589 current_matrix);
14590 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14591 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14592
14593 xassert (it.hpos == 0 && it.current_x == 0);
14594 }
14595 else
14596 {
14597 /* There are no reusable lines at the start of the window.
14598 Start displaying in the first text line. */
14599 start_display (&it, w, start);
14600 it.vpos = it.first_vpos;
14601 start_pos = it.current.pos;
14602 }
14603
14604 /* Find the first row that is not affected by changes at the end of
14605 the buffer. Value will be null if there is no unchanged row, in
14606 which case we must redisplay to the end of the window. delta
14607 will be set to the value by which buffer positions beginning with
14608 first_unchanged_at_end_row have to be adjusted due to text
14609 changes. */
14610 first_unchanged_at_end_row
14611 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14612 IF_DEBUG (debug_delta = delta);
14613 IF_DEBUG (debug_delta_bytes = delta_bytes);
14614
14615 /* Set stop_pos to the buffer position up to which we will have to
14616 display new lines. If first_unchanged_at_end_row != NULL, this
14617 is the buffer position of the start of the line displayed in that
14618 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14619 that we don't stop at a buffer position. */
14620 stop_pos = 0;
14621 if (first_unchanged_at_end_row)
14622 {
14623 xassert (last_unchanged_at_beg_row == NULL
14624 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14625
14626 /* If this is a continuation line, move forward to the next one
14627 that isn't. Changes in lines above affect this line.
14628 Caution: this may move first_unchanged_at_end_row to a row
14629 not displaying text. */
14630 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14631 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14632 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14633 < it.last_visible_y))
14634 ++first_unchanged_at_end_row;
14635
14636 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14637 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14638 >= it.last_visible_y))
14639 first_unchanged_at_end_row = NULL;
14640 else
14641 {
14642 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14643 + delta);
14644 first_unchanged_at_end_vpos
14645 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14646 xassert (stop_pos >= Z - END_UNCHANGED);
14647 }
14648 }
14649 else if (last_unchanged_at_beg_row == NULL)
14650 GIVE_UP (19);
14651
14652
14653 #if GLYPH_DEBUG
14654
14655 /* Either there is no unchanged row at the end, or the one we have
14656 now displays text. This is a necessary condition for the window
14657 end pos calculation at the end of this function. */
14658 xassert (first_unchanged_at_end_row == NULL
14659 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14660
14661 debug_last_unchanged_at_beg_vpos
14662 = (last_unchanged_at_beg_row
14663 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14664 : -1);
14665 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14666
14667 #endif /* GLYPH_DEBUG != 0 */
14668
14669
14670 /* Display new lines. Set last_text_row to the last new line
14671 displayed which has text on it, i.e. might end up as being the
14672 line where the window_end_vpos is. */
14673 w->cursor.vpos = -1;
14674 last_text_row = NULL;
14675 overlay_arrow_seen = 0;
14676 while (it.current_y < it.last_visible_y
14677 && !fonts_changed_p
14678 && (first_unchanged_at_end_row == NULL
14679 || IT_CHARPOS (it) < stop_pos))
14680 {
14681 if (display_line (&it))
14682 last_text_row = it.glyph_row - 1;
14683 }
14684
14685 if (fonts_changed_p)
14686 return -1;
14687
14688
14689 /* Compute differences in buffer positions, y-positions etc. for
14690 lines reused at the bottom of the window. Compute what we can
14691 scroll. */
14692 if (first_unchanged_at_end_row
14693 /* No lines reused because we displayed everything up to the
14694 bottom of the window. */
14695 && it.current_y < it.last_visible_y)
14696 {
14697 dvpos = (it.vpos
14698 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14699 current_matrix));
14700 dy = it.current_y - first_unchanged_at_end_row->y;
14701 run.current_y = first_unchanged_at_end_row->y;
14702 run.desired_y = run.current_y + dy;
14703 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14704 }
14705 else
14706 {
14707 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14708 first_unchanged_at_end_row = NULL;
14709 }
14710 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14711
14712
14713 /* Find the cursor if not already found. We have to decide whether
14714 PT will appear on this window (it sometimes doesn't, but this is
14715 not a very frequent case.) This decision has to be made before
14716 the current matrix is altered. A value of cursor.vpos < 0 means
14717 that PT is either in one of the lines beginning at
14718 first_unchanged_at_end_row or below the window. Don't care for
14719 lines that might be displayed later at the window end; as
14720 mentioned, this is not a frequent case. */
14721 if (w->cursor.vpos < 0)
14722 {
14723 /* Cursor in unchanged rows at the top? */
14724 if (PT < CHARPOS (start_pos)
14725 && last_unchanged_at_beg_row)
14726 {
14727 row = row_containing_pos (w, PT,
14728 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14729 last_unchanged_at_beg_row + 1, 0);
14730 if (row)
14731 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14732 }
14733
14734 /* Start from first_unchanged_at_end_row looking for PT. */
14735 else if (first_unchanged_at_end_row)
14736 {
14737 row = row_containing_pos (w, PT - delta,
14738 first_unchanged_at_end_row, NULL, 0);
14739 if (row)
14740 set_cursor_from_row (w, row, w->current_matrix, delta,
14741 delta_bytes, dy, dvpos);
14742 }
14743
14744 /* Give up if cursor was not found. */
14745 if (w->cursor.vpos < 0)
14746 {
14747 clear_glyph_matrix (w->desired_matrix);
14748 return -1;
14749 }
14750 }
14751
14752 /* Don't let the cursor end in the scroll margins. */
14753 {
14754 int this_scroll_margin, cursor_height;
14755
14756 this_scroll_margin = max (0, scroll_margin);
14757 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14758 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14759 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14760
14761 if ((w->cursor.y < this_scroll_margin
14762 && CHARPOS (start) > BEGV)
14763 /* Old redisplay didn't take scroll margin into account at the bottom,
14764 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14765 || (w->cursor.y + (make_cursor_line_fully_visible_p
14766 ? cursor_height + this_scroll_margin
14767 : 1)) > it.last_visible_y)
14768 {
14769 w->cursor.vpos = -1;
14770 clear_glyph_matrix (w->desired_matrix);
14771 return -1;
14772 }
14773 }
14774
14775 /* Scroll the display. Do it before changing the current matrix so
14776 that xterm.c doesn't get confused about where the cursor glyph is
14777 found. */
14778 if (dy && run.height)
14779 {
14780 update_begin (f);
14781
14782 if (FRAME_WINDOW_P (f))
14783 {
14784 rif->update_window_begin_hook (w);
14785 rif->clear_window_mouse_face (w);
14786 rif->scroll_run_hook (w, &run);
14787 rif->update_window_end_hook (w, 0, 0);
14788 }
14789 else
14790 {
14791 /* Terminal frame. In this case, dvpos gives the number of
14792 lines to scroll by; dvpos < 0 means scroll up. */
14793 int first_unchanged_at_end_vpos
14794 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14795 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14796 int end = (WINDOW_TOP_EDGE_LINE (w)
14797 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14798 + window_internal_height (w));
14799
14800 /* Perform the operation on the screen. */
14801 if (dvpos > 0)
14802 {
14803 /* Scroll last_unchanged_at_beg_row to the end of the
14804 window down dvpos lines. */
14805 set_terminal_window (end);
14806
14807 /* On dumb terminals delete dvpos lines at the end
14808 before inserting dvpos empty lines. */
14809 if (!scroll_region_ok)
14810 ins_del_lines (end - dvpos, -dvpos);
14811
14812 /* Insert dvpos empty lines in front of
14813 last_unchanged_at_beg_row. */
14814 ins_del_lines (from, dvpos);
14815 }
14816 else if (dvpos < 0)
14817 {
14818 /* Scroll up last_unchanged_at_beg_vpos to the end of
14819 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14820 set_terminal_window (end);
14821
14822 /* Delete dvpos lines in front of
14823 last_unchanged_at_beg_vpos. ins_del_lines will set
14824 the cursor to the given vpos and emit |dvpos| delete
14825 line sequences. */
14826 ins_del_lines (from + dvpos, dvpos);
14827
14828 /* On a dumb terminal insert dvpos empty lines at the
14829 end. */
14830 if (!scroll_region_ok)
14831 ins_del_lines (end + dvpos, -dvpos);
14832 }
14833
14834 set_terminal_window (0);
14835 }
14836
14837 update_end (f);
14838 }
14839
14840 /* Shift reused rows of the current matrix to the right position.
14841 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14842 text. */
14843 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14844 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14845 if (dvpos < 0)
14846 {
14847 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14848 bottom_vpos, dvpos);
14849 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14850 bottom_vpos, 0);
14851 }
14852 else if (dvpos > 0)
14853 {
14854 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14855 bottom_vpos, dvpos);
14856 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14857 first_unchanged_at_end_vpos + dvpos, 0);
14858 }
14859
14860 /* For frame-based redisplay, make sure that current frame and window
14861 matrix are in sync with respect to glyph memory. */
14862 if (!FRAME_WINDOW_P (f))
14863 sync_frame_with_window_matrix_rows (w);
14864
14865 /* Adjust buffer positions in reused rows. */
14866 if (delta)
14867 increment_matrix_positions (current_matrix,
14868 first_unchanged_at_end_vpos + dvpos,
14869 bottom_vpos, delta, delta_bytes);
14870
14871 /* Adjust Y positions. */
14872 if (dy)
14873 shift_glyph_matrix (w, current_matrix,
14874 first_unchanged_at_end_vpos + dvpos,
14875 bottom_vpos, dy);
14876
14877 if (first_unchanged_at_end_row)
14878 {
14879 first_unchanged_at_end_row += dvpos;
14880 if (first_unchanged_at_end_row->y >= it.last_visible_y
14881 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14882 first_unchanged_at_end_row = NULL;
14883 }
14884
14885 /* If scrolling up, there may be some lines to display at the end of
14886 the window. */
14887 last_text_row_at_end = NULL;
14888 if (dy < 0)
14889 {
14890 /* Scrolling up can leave for example a partially visible line
14891 at the end of the window to be redisplayed. */
14892 /* Set last_row to the glyph row in the current matrix where the
14893 window end line is found. It has been moved up or down in
14894 the matrix by dvpos. */
14895 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14896 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14897
14898 /* If last_row is the window end line, it should display text. */
14899 xassert (last_row->displays_text_p);
14900
14901 /* If window end line was partially visible before, begin
14902 displaying at that line. Otherwise begin displaying with the
14903 line following it. */
14904 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14905 {
14906 init_to_row_start (&it, w, last_row);
14907 it.vpos = last_vpos;
14908 it.current_y = last_row->y;
14909 }
14910 else
14911 {
14912 init_to_row_end (&it, w, last_row);
14913 it.vpos = 1 + last_vpos;
14914 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14915 ++last_row;
14916 }
14917
14918 /* We may start in a continuation line. If so, we have to
14919 get the right continuation_lines_width and current_x. */
14920 it.continuation_lines_width = last_row->continuation_lines_width;
14921 it.hpos = it.current_x = 0;
14922
14923 /* Display the rest of the lines at the window end. */
14924 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14925 while (it.current_y < it.last_visible_y
14926 && !fonts_changed_p)
14927 {
14928 /* Is it always sure that the display agrees with lines in
14929 the current matrix? I don't think so, so we mark rows
14930 displayed invalid in the current matrix by setting their
14931 enabled_p flag to zero. */
14932 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14933 if (display_line (&it))
14934 last_text_row_at_end = it.glyph_row - 1;
14935 }
14936 }
14937
14938 /* Update window_end_pos and window_end_vpos. */
14939 if (first_unchanged_at_end_row
14940 && !last_text_row_at_end)
14941 {
14942 /* Window end line if one of the preserved rows from the current
14943 matrix. Set row to the last row displaying text in current
14944 matrix starting at first_unchanged_at_end_row, after
14945 scrolling. */
14946 xassert (first_unchanged_at_end_row->displays_text_p);
14947 row = find_last_row_displaying_text (w->current_matrix, &it,
14948 first_unchanged_at_end_row);
14949 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14950
14951 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14952 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14953 w->window_end_vpos
14954 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14955 xassert (w->window_end_bytepos >= 0);
14956 IF_DEBUG (debug_method_add (w, "A"));
14957 }
14958 else if (last_text_row_at_end)
14959 {
14960 w->window_end_pos
14961 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14962 w->window_end_bytepos
14963 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14964 w->window_end_vpos
14965 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14966 xassert (w->window_end_bytepos >= 0);
14967 IF_DEBUG (debug_method_add (w, "B"));
14968 }
14969 else if (last_text_row)
14970 {
14971 /* We have displayed either to the end of the window or at the
14972 end of the window, i.e. the last row with text is to be found
14973 in the desired matrix. */
14974 w->window_end_pos
14975 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14976 w->window_end_bytepos
14977 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14978 w->window_end_vpos
14979 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14980 xassert (w->window_end_bytepos >= 0);
14981 }
14982 else if (first_unchanged_at_end_row == NULL
14983 && last_text_row == NULL
14984 && last_text_row_at_end == NULL)
14985 {
14986 /* Displayed to end of window, but no line containing text was
14987 displayed. Lines were deleted at the end of the window. */
14988 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14989 int vpos = XFASTINT (w->window_end_vpos);
14990 struct glyph_row *current_row = current_matrix->rows + vpos;
14991 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14992
14993 for (row = NULL;
14994 row == NULL && vpos >= first_vpos;
14995 --vpos, --current_row, --desired_row)
14996 {
14997 if (desired_row->enabled_p)
14998 {
14999 if (desired_row->displays_text_p)
15000 row = desired_row;
15001 }
15002 else if (current_row->displays_text_p)
15003 row = current_row;
15004 }
15005
15006 xassert (row != NULL);
15007 w->window_end_vpos = make_number (vpos + 1);
15008 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15009 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15010 xassert (w->window_end_bytepos >= 0);
15011 IF_DEBUG (debug_method_add (w, "C"));
15012 }
15013 else
15014 abort ();
15015
15016 #if 0 /* This leads to problems, for instance when the cursor is
15017 at ZV, and the cursor line displays no text. */
15018 /* Disable rows below what's displayed in the window. This makes
15019 debugging easier. */
15020 enable_glyph_matrix_rows (current_matrix,
15021 XFASTINT (w->window_end_vpos) + 1,
15022 bottom_vpos, 0);
15023 #endif
15024
15025 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15026 debug_end_vpos = XFASTINT (w->window_end_vpos));
15027
15028 /* Record that display has not been completed. */
15029 w->window_end_valid = Qnil;
15030 w->desired_matrix->no_scrolling_p = 1;
15031 return 3;
15032
15033 #undef GIVE_UP
15034 }
15035
15036
15037 \f
15038 /***********************************************************************
15039 More debugging support
15040 ***********************************************************************/
15041
15042 #if GLYPH_DEBUG
15043
15044 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15045 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15046 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15047
15048
15049 /* Dump the contents of glyph matrix MATRIX on stderr.
15050
15051 GLYPHS 0 means don't show glyph contents.
15052 GLYPHS 1 means show glyphs in short form
15053 GLYPHS > 1 means show glyphs in long form. */
15054
15055 void
15056 dump_glyph_matrix (matrix, glyphs)
15057 struct glyph_matrix *matrix;
15058 int glyphs;
15059 {
15060 int i;
15061 for (i = 0; i < matrix->nrows; ++i)
15062 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15063 }
15064
15065
15066 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15067 the glyph row and area where the glyph comes from. */
15068
15069 void
15070 dump_glyph (row, glyph, area)
15071 struct glyph_row *row;
15072 struct glyph *glyph;
15073 int area;
15074 {
15075 if (glyph->type == CHAR_GLYPH)
15076 {
15077 fprintf (stderr,
15078 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15079 glyph - row->glyphs[TEXT_AREA],
15080 'C',
15081 glyph->charpos,
15082 (BUFFERP (glyph->object)
15083 ? 'B'
15084 : (STRINGP (glyph->object)
15085 ? 'S'
15086 : '-')),
15087 glyph->pixel_width,
15088 glyph->u.ch,
15089 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15090 ? glyph->u.ch
15091 : '.'),
15092 glyph->face_id,
15093 glyph->left_box_line_p,
15094 glyph->right_box_line_p);
15095 }
15096 else if (glyph->type == STRETCH_GLYPH)
15097 {
15098 fprintf (stderr,
15099 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15100 glyph - row->glyphs[TEXT_AREA],
15101 'S',
15102 glyph->charpos,
15103 (BUFFERP (glyph->object)
15104 ? 'B'
15105 : (STRINGP (glyph->object)
15106 ? 'S'
15107 : '-')),
15108 glyph->pixel_width,
15109 0,
15110 '.',
15111 glyph->face_id,
15112 glyph->left_box_line_p,
15113 glyph->right_box_line_p);
15114 }
15115 else if (glyph->type == IMAGE_GLYPH)
15116 {
15117 fprintf (stderr,
15118 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15119 glyph - row->glyphs[TEXT_AREA],
15120 'I',
15121 glyph->charpos,
15122 (BUFFERP (glyph->object)
15123 ? 'B'
15124 : (STRINGP (glyph->object)
15125 ? 'S'
15126 : '-')),
15127 glyph->pixel_width,
15128 glyph->u.img_id,
15129 '.',
15130 glyph->face_id,
15131 glyph->left_box_line_p,
15132 glyph->right_box_line_p);
15133 }
15134 else if (glyph->type == COMPOSITE_GLYPH)
15135 {
15136 fprintf (stderr,
15137 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15138 glyph - row->glyphs[TEXT_AREA],
15139 '+',
15140 glyph->charpos,
15141 (BUFFERP (glyph->object)
15142 ? 'B'
15143 : (STRINGP (glyph->object)
15144 ? 'S'
15145 : '-')),
15146 glyph->pixel_width,
15147 glyph->u.cmp_id,
15148 '.',
15149 glyph->face_id,
15150 glyph->left_box_line_p,
15151 glyph->right_box_line_p);
15152 }
15153 }
15154
15155
15156 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15157 GLYPHS 0 means don't show glyph contents.
15158 GLYPHS 1 means show glyphs in short form
15159 GLYPHS > 1 means show glyphs in long form. */
15160
15161 void
15162 dump_glyph_row (row, vpos, glyphs)
15163 struct glyph_row *row;
15164 int vpos, glyphs;
15165 {
15166 if (glyphs != 1)
15167 {
15168 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15169 fprintf (stderr, "======================================================================\n");
15170
15171 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15172 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15173 vpos,
15174 MATRIX_ROW_START_CHARPOS (row),
15175 MATRIX_ROW_END_CHARPOS (row),
15176 row->used[TEXT_AREA],
15177 row->contains_overlapping_glyphs_p,
15178 row->enabled_p,
15179 row->truncated_on_left_p,
15180 row->truncated_on_right_p,
15181 row->continued_p,
15182 MATRIX_ROW_CONTINUATION_LINE_P (row),
15183 row->displays_text_p,
15184 row->ends_at_zv_p,
15185 row->fill_line_p,
15186 row->ends_in_middle_of_char_p,
15187 row->starts_in_middle_of_char_p,
15188 row->mouse_face_p,
15189 row->x,
15190 row->y,
15191 row->pixel_width,
15192 row->height,
15193 row->visible_height,
15194 row->ascent,
15195 row->phys_ascent);
15196 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15197 row->end.overlay_string_index,
15198 row->continuation_lines_width);
15199 fprintf (stderr, "%9d %5d\n",
15200 CHARPOS (row->start.string_pos),
15201 CHARPOS (row->end.string_pos));
15202 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15203 row->end.dpvec_index);
15204 }
15205
15206 if (glyphs > 1)
15207 {
15208 int area;
15209
15210 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15211 {
15212 struct glyph *glyph = row->glyphs[area];
15213 struct glyph *glyph_end = glyph + row->used[area];
15214
15215 /* Glyph for a line end in text. */
15216 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15217 ++glyph_end;
15218
15219 if (glyph < glyph_end)
15220 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15221
15222 for (; glyph < glyph_end; ++glyph)
15223 dump_glyph (row, glyph, area);
15224 }
15225 }
15226 else if (glyphs == 1)
15227 {
15228 int area;
15229
15230 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15231 {
15232 char *s = (char *) alloca (row->used[area] + 1);
15233 int i;
15234
15235 for (i = 0; i < row->used[area]; ++i)
15236 {
15237 struct glyph *glyph = row->glyphs[area] + i;
15238 if (glyph->type == CHAR_GLYPH
15239 && glyph->u.ch < 0x80
15240 && glyph->u.ch >= ' ')
15241 s[i] = glyph->u.ch;
15242 else
15243 s[i] = '.';
15244 }
15245
15246 s[i] = '\0';
15247 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15248 }
15249 }
15250 }
15251
15252
15253 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15254 Sdump_glyph_matrix, 0, 1, "p",
15255 doc: /* Dump the current matrix of the selected window to stderr.
15256 Shows contents of glyph row structures. With non-nil
15257 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15258 glyphs in short form, otherwise show glyphs in long form. */)
15259 (glyphs)
15260 Lisp_Object glyphs;
15261 {
15262 struct window *w = XWINDOW (selected_window);
15263 struct buffer *buffer = XBUFFER (w->buffer);
15264
15265 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15266 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15267 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15268 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15269 fprintf (stderr, "=============================================\n");
15270 dump_glyph_matrix (w->current_matrix,
15271 NILP (glyphs) ? 0 : XINT (glyphs));
15272 return Qnil;
15273 }
15274
15275
15276 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15277 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15278 ()
15279 {
15280 struct frame *f = XFRAME (selected_frame);
15281 dump_glyph_matrix (f->current_matrix, 1);
15282 return Qnil;
15283 }
15284
15285
15286 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15287 doc: /* Dump glyph row ROW to stderr.
15288 GLYPH 0 means don't dump glyphs.
15289 GLYPH 1 means dump glyphs in short form.
15290 GLYPH > 1 or omitted means dump glyphs in long form. */)
15291 (row, glyphs)
15292 Lisp_Object row, glyphs;
15293 {
15294 struct glyph_matrix *matrix;
15295 int vpos;
15296
15297 CHECK_NUMBER (row);
15298 matrix = XWINDOW (selected_window)->current_matrix;
15299 vpos = XINT (row);
15300 if (vpos >= 0 && vpos < matrix->nrows)
15301 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15302 vpos,
15303 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15304 return Qnil;
15305 }
15306
15307
15308 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15309 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15310 GLYPH 0 means don't dump glyphs.
15311 GLYPH 1 means dump glyphs in short form.
15312 GLYPH > 1 or omitted means dump glyphs in long form. */)
15313 (row, glyphs)
15314 Lisp_Object row, glyphs;
15315 {
15316 struct frame *sf = SELECTED_FRAME ();
15317 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15318 int vpos;
15319
15320 CHECK_NUMBER (row);
15321 vpos = XINT (row);
15322 if (vpos >= 0 && vpos < m->nrows)
15323 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15324 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15325 return Qnil;
15326 }
15327
15328
15329 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15330 doc: /* Toggle tracing of redisplay.
15331 With ARG, turn tracing on if and only if ARG is positive. */)
15332 (arg)
15333 Lisp_Object arg;
15334 {
15335 if (NILP (arg))
15336 trace_redisplay_p = !trace_redisplay_p;
15337 else
15338 {
15339 arg = Fprefix_numeric_value (arg);
15340 trace_redisplay_p = XINT (arg) > 0;
15341 }
15342
15343 return Qnil;
15344 }
15345
15346
15347 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15348 doc: /* Like `format', but print result to stderr.
15349 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15350 (nargs, args)
15351 int nargs;
15352 Lisp_Object *args;
15353 {
15354 Lisp_Object s = Fformat (nargs, args);
15355 fprintf (stderr, "%s", SDATA (s));
15356 return Qnil;
15357 }
15358
15359 #endif /* GLYPH_DEBUG */
15360
15361
15362 \f
15363 /***********************************************************************
15364 Building Desired Matrix Rows
15365 ***********************************************************************/
15366
15367 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15368 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15369
15370 static struct glyph_row *
15371 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15372 struct window *w;
15373 Lisp_Object overlay_arrow_string;
15374 {
15375 struct frame *f = XFRAME (WINDOW_FRAME (w));
15376 struct buffer *buffer = XBUFFER (w->buffer);
15377 struct buffer *old = current_buffer;
15378 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15379 int arrow_len = SCHARS (overlay_arrow_string);
15380 const unsigned char *arrow_end = arrow_string + arrow_len;
15381 const unsigned char *p;
15382 struct it it;
15383 int multibyte_p;
15384 int n_glyphs_before;
15385
15386 set_buffer_temp (buffer);
15387 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15388 it.glyph_row->used[TEXT_AREA] = 0;
15389 SET_TEXT_POS (it.position, 0, 0);
15390
15391 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15392 p = arrow_string;
15393 while (p < arrow_end)
15394 {
15395 Lisp_Object face, ilisp;
15396
15397 /* Get the next character. */
15398 if (multibyte_p)
15399 it.c = string_char_and_length (p, arrow_len, &it.len);
15400 else
15401 it.c = *p, it.len = 1;
15402 p += it.len;
15403
15404 /* Get its face. */
15405 ilisp = make_number (p - arrow_string);
15406 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15407 it.face_id = compute_char_face (f, it.c, face);
15408
15409 /* Compute its width, get its glyphs. */
15410 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15411 SET_TEXT_POS (it.position, -1, -1);
15412 PRODUCE_GLYPHS (&it);
15413
15414 /* If this character doesn't fit any more in the line, we have
15415 to remove some glyphs. */
15416 if (it.current_x > it.last_visible_x)
15417 {
15418 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15419 break;
15420 }
15421 }
15422
15423 set_buffer_temp (old);
15424 return it.glyph_row;
15425 }
15426
15427
15428 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15429 glyphs are only inserted for terminal frames since we can't really
15430 win with truncation glyphs when partially visible glyphs are
15431 involved. Which glyphs to insert is determined by
15432 produce_special_glyphs. */
15433
15434 static void
15435 insert_left_trunc_glyphs (it)
15436 struct it *it;
15437 {
15438 struct it truncate_it;
15439 struct glyph *from, *end, *to, *toend;
15440
15441 xassert (!FRAME_WINDOW_P (it->f));
15442
15443 /* Get the truncation glyphs. */
15444 truncate_it = *it;
15445 truncate_it.current_x = 0;
15446 truncate_it.face_id = DEFAULT_FACE_ID;
15447 truncate_it.glyph_row = &scratch_glyph_row;
15448 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15449 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15450 truncate_it.object = make_number (0);
15451 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15452
15453 /* Overwrite glyphs from IT with truncation glyphs. */
15454 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15455 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15456 to = it->glyph_row->glyphs[TEXT_AREA];
15457 toend = to + it->glyph_row->used[TEXT_AREA];
15458
15459 while (from < end)
15460 *to++ = *from++;
15461
15462 /* There may be padding glyphs left over. Overwrite them too. */
15463 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15464 {
15465 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15466 while (from < end)
15467 *to++ = *from++;
15468 }
15469
15470 if (to > toend)
15471 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15472 }
15473
15474
15475 /* Compute the pixel height and width of IT->glyph_row.
15476
15477 Most of the time, ascent and height of a display line will be equal
15478 to the max_ascent and max_height values of the display iterator
15479 structure. This is not the case if
15480
15481 1. We hit ZV without displaying anything. In this case, max_ascent
15482 and max_height will be zero.
15483
15484 2. We have some glyphs that don't contribute to the line height.
15485 (The glyph row flag contributes_to_line_height_p is for future
15486 pixmap extensions).
15487
15488 The first case is easily covered by using default values because in
15489 these cases, the line height does not really matter, except that it
15490 must not be zero. */
15491
15492 static void
15493 compute_line_metrics (it)
15494 struct it *it;
15495 {
15496 struct glyph_row *row = it->glyph_row;
15497 int area, i;
15498
15499 if (FRAME_WINDOW_P (it->f))
15500 {
15501 int i, min_y, max_y;
15502
15503 /* The line may consist of one space only, that was added to
15504 place the cursor on it. If so, the row's height hasn't been
15505 computed yet. */
15506 if (row->height == 0)
15507 {
15508 if (it->max_ascent + it->max_descent == 0)
15509 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15510 row->ascent = it->max_ascent;
15511 row->height = it->max_ascent + it->max_descent;
15512 row->phys_ascent = it->max_phys_ascent;
15513 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15514 row->extra_line_spacing = it->max_extra_line_spacing;
15515 }
15516
15517 /* Compute the width of this line. */
15518 row->pixel_width = row->x;
15519 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15520 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15521
15522 xassert (row->pixel_width >= 0);
15523 xassert (row->ascent >= 0 && row->height > 0);
15524
15525 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15526 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15527
15528 /* If first line's physical ascent is larger than its logical
15529 ascent, use the physical ascent, and make the row taller.
15530 This makes accented characters fully visible. */
15531 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15532 && row->phys_ascent > row->ascent)
15533 {
15534 row->height += row->phys_ascent - row->ascent;
15535 row->ascent = row->phys_ascent;
15536 }
15537
15538 /* Compute how much of the line is visible. */
15539 row->visible_height = row->height;
15540
15541 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15542 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15543
15544 if (row->y < min_y)
15545 row->visible_height -= min_y - row->y;
15546 if (row->y + row->height > max_y)
15547 row->visible_height -= row->y + row->height - max_y;
15548 }
15549 else
15550 {
15551 row->pixel_width = row->used[TEXT_AREA];
15552 if (row->continued_p)
15553 row->pixel_width -= it->continuation_pixel_width;
15554 else if (row->truncated_on_right_p)
15555 row->pixel_width -= it->truncation_pixel_width;
15556 row->ascent = row->phys_ascent = 0;
15557 row->height = row->phys_height = row->visible_height = 1;
15558 row->extra_line_spacing = 0;
15559 }
15560
15561 /* Compute a hash code for this row. */
15562 row->hash = 0;
15563 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15564 for (i = 0; i < row->used[area]; ++i)
15565 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15566 + row->glyphs[area][i].u.val
15567 + row->glyphs[area][i].face_id
15568 + row->glyphs[area][i].padding_p
15569 + (row->glyphs[area][i].type << 2));
15570
15571 it->max_ascent = it->max_descent = 0;
15572 it->max_phys_ascent = it->max_phys_descent = 0;
15573 }
15574
15575
15576 /* Append one space to the glyph row of iterator IT if doing a
15577 window-based redisplay. The space has the same face as
15578 IT->face_id. Value is non-zero if a space was added.
15579
15580 This function is called to make sure that there is always one glyph
15581 at the end of a glyph row that the cursor can be set on under
15582 window-systems. (If there weren't such a glyph we would not know
15583 how wide and tall a box cursor should be displayed).
15584
15585 At the same time this space let's a nicely handle clearing to the
15586 end of the line if the row ends in italic text. */
15587
15588 static int
15589 append_space_for_newline (it, default_face_p)
15590 struct it *it;
15591 int default_face_p;
15592 {
15593 if (FRAME_WINDOW_P (it->f))
15594 {
15595 int n = it->glyph_row->used[TEXT_AREA];
15596
15597 if (it->glyph_row->glyphs[TEXT_AREA] + n
15598 < it->glyph_row->glyphs[1 + TEXT_AREA])
15599 {
15600 /* Save some values that must not be changed.
15601 Must save IT->c and IT->len because otherwise
15602 ITERATOR_AT_END_P wouldn't work anymore after
15603 append_space_for_newline has been called. */
15604 enum display_element_type saved_what = it->what;
15605 int saved_c = it->c, saved_len = it->len;
15606 int saved_x = it->current_x;
15607 int saved_face_id = it->face_id;
15608 struct text_pos saved_pos;
15609 Lisp_Object saved_object;
15610 struct face *face;
15611
15612 saved_object = it->object;
15613 saved_pos = it->position;
15614
15615 it->what = IT_CHARACTER;
15616 bzero (&it->position, sizeof it->position);
15617 it->object = make_number (0);
15618 it->c = ' ';
15619 it->len = 1;
15620
15621 if (default_face_p)
15622 it->face_id = DEFAULT_FACE_ID;
15623 else if (it->face_before_selective_p)
15624 it->face_id = it->saved_face_id;
15625 face = FACE_FROM_ID (it->f, it->face_id);
15626 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15627
15628 PRODUCE_GLYPHS (it);
15629
15630 it->override_ascent = -1;
15631 it->constrain_row_ascent_descent_p = 0;
15632 it->current_x = saved_x;
15633 it->object = saved_object;
15634 it->position = saved_pos;
15635 it->what = saved_what;
15636 it->face_id = saved_face_id;
15637 it->len = saved_len;
15638 it->c = saved_c;
15639 return 1;
15640 }
15641 }
15642
15643 return 0;
15644 }
15645
15646
15647 /* Extend the face of the last glyph in the text area of IT->glyph_row
15648 to the end of the display line. Called from display_line.
15649 If the glyph row is empty, add a space glyph to it so that we
15650 know the face to draw. Set the glyph row flag fill_line_p. */
15651
15652 static void
15653 extend_face_to_end_of_line (it)
15654 struct it *it;
15655 {
15656 struct face *face;
15657 struct frame *f = it->f;
15658
15659 /* If line is already filled, do nothing. */
15660 if (it->current_x >= it->last_visible_x)
15661 return;
15662
15663 /* Face extension extends the background and box of IT->face_id
15664 to the end of the line. If the background equals the background
15665 of the frame, we don't have to do anything. */
15666 if (it->face_before_selective_p)
15667 face = FACE_FROM_ID (it->f, it->saved_face_id);
15668 else
15669 face = FACE_FROM_ID (f, it->face_id);
15670
15671 if (FRAME_WINDOW_P (f)
15672 && it->glyph_row->displays_text_p
15673 && face->box == FACE_NO_BOX
15674 && face->background == FRAME_BACKGROUND_PIXEL (f)
15675 && !face->stipple)
15676 return;
15677
15678 /* Set the glyph row flag indicating that the face of the last glyph
15679 in the text area has to be drawn to the end of the text area. */
15680 it->glyph_row->fill_line_p = 1;
15681
15682 /* If current character of IT is not ASCII, make sure we have the
15683 ASCII face. This will be automatically undone the next time
15684 get_next_display_element returns a multibyte character. Note
15685 that the character will always be single byte in unibyte text. */
15686 if (!SINGLE_BYTE_CHAR_P (it->c))
15687 {
15688 it->face_id = FACE_FOR_CHAR (f, face, 0);
15689 }
15690
15691 if (FRAME_WINDOW_P (f))
15692 {
15693 /* If the row is empty, add a space with the current face of IT,
15694 so that we know which face to draw. */
15695 if (it->glyph_row->used[TEXT_AREA] == 0)
15696 {
15697 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15698 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15699 it->glyph_row->used[TEXT_AREA] = 1;
15700 }
15701 }
15702 else
15703 {
15704 /* Save some values that must not be changed. */
15705 int saved_x = it->current_x;
15706 struct text_pos saved_pos;
15707 Lisp_Object saved_object;
15708 enum display_element_type saved_what = it->what;
15709 int saved_face_id = it->face_id;
15710
15711 saved_object = it->object;
15712 saved_pos = it->position;
15713
15714 it->what = IT_CHARACTER;
15715 bzero (&it->position, sizeof it->position);
15716 it->object = make_number (0);
15717 it->c = ' ';
15718 it->len = 1;
15719 it->face_id = face->id;
15720
15721 PRODUCE_GLYPHS (it);
15722
15723 while (it->current_x <= it->last_visible_x)
15724 PRODUCE_GLYPHS (it);
15725
15726 /* Don't count these blanks really. It would let us insert a left
15727 truncation glyph below and make us set the cursor on them, maybe. */
15728 it->current_x = saved_x;
15729 it->object = saved_object;
15730 it->position = saved_pos;
15731 it->what = saved_what;
15732 it->face_id = saved_face_id;
15733 }
15734 }
15735
15736
15737 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15738 trailing whitespace. */
15739
15740 static int
15741 trailing_whitespace_p (charpos)
15742 int charpos;
15743 {
15744 int bytepos = CHAR_TO_BYTE (charpos);
15745 int c = 0;
15746
15747 while (bytepos < ZV_BYTE
15748 && (c = FETCH_CHAR (bytepos),
15749 c == ' ' || c == '\t'))
15750 ++bytepos;
15751
15752 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15753 {
15754 if (bytepos != PT_BYTE)
15755 return 1;
15756 }
15757 return 0;
15758 }
15759
15760
15761 /* Highlight trailing whitespace, if any, in ROW. */
15762
15763 void
15764 highlight_trailing_whitespace (f, row)
15765 struct frame *f;
15766 struct glyph_row *row;
15767 {
15768 int used = row->used[TEXT_AREA];
15769
15770 if (used)
15771 {
15772 struct glyph *start = row->glyphs[TEXT_AREA];
15773 struct glyph *glyph = start + used - 1;
15774
15775 /* Skip over glyphs inserted to display the cursor at the
15776 end of a line, for extending the face of the last glyph
15777 to the end of the line on terminals, and for truncation
15778 and continuation glyphs. */
15779 while (glyph >= start
15780 && glyph->type == CHAR_GLYPH
15781 && INTEGERP (glyph->object))
15782 --glyph;
15783
15784 /* If last glyph is a space or stretch, and it's trailing
15785 whitespace, set the face of all trailing whitespace glyphs in
15786 IT->glyph_row to `trailing-whitespace'. */
15787 if (glyph >= start
15788 && BUFFERP (glyph->object)
15789 && (glyph->type == STRETCH_GLYPH
15790 || (glyph->type == CHAR_GLYPH
15791 && glyph->u.ch == ' '))
15792 && trailing_whitespace_p (glyph->charpos))
15793 {
15794 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15795 if (face_id < 0)
15796 return;
15797
15798 while (glyph >= start
15799 && BUFFERP (glyph->object)
15800 && (glyph->type == STRETCH_GLYPH
15801 || (glyph->type == CHAR_GLYPH
15802 && glyph->u.ch == ' ')))
15803 (glyph--)->face_id = face_id;
15804 }
15805 }
15806 }
15807
15808
15809 /* Value is non-zero if glyph row ROW in window W should be
15810 used to hold the cursor. */
15811
15812 static int
15813 cursor_row_p (w, row)
15814 struct window *w;
15815 struct glyph_row *row;
15816 {
15817 int cursor_row_p = 1;
15818
15819 if (PT == MATRIX_ROW_END_CHARPOS (row))
15820 {
15821 /* If the row ends with a newline from a string, we don't want
15822 the cursor there, but we still want it at the start of the
15823 string if the string starts in this row.
15824 If the row is continued it doesn't end in a newline. */
15825 if (CHARPOS (row->end.string_pos) >= 0)
15826 cursor_row_p = (row->continued_p
15827 || PT >= MATRIX_ROW_START_CHARPOS (row));
15828 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15829 {
15830 /* If the row ends in middle of a real character,
15831 and the line is continued, we want the cursor here.
15832 That's because MATRIX_ROW_END_CHARPOS would equal
15833 PT if PT is before the character. */
15834 if (!row->ends_in_ellipsis_p)
15835 cursor_row_p = row->continued_p;
15836 else
15837 /* If the row ends in an ellipsis, then
15838 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15839 We want that position to be displayed after the ellipsis. */
15840 cursor_row_p = 0;
15841 }
15842 /* If the row ends at ZV, display the cursor at the end of that
15843 row instead of at the start of the row below. */
15844 else if (row->ends_at_zv_p)
15845 cursor_row_p = 1;
15846 else
15847 cursor_row_p = 0;
15848 }
15849
15850 return cursor_row_p;
15851 }
15852
15853
15854 /* Construct the glyph row IT->glyph_row in the desired matrix of
15855 IT->w from text at the current position of IT. See dispextern.h
15856 for an overview of struct it. Value is non-zero if
15857 IT->glyph_row displays text, as opposed to a line displaying ZV
15858 only. */
15859
15860 static int
15861 display_line (it)
15862 struct it *it;
15863 {
15864 struct glyph_row *row = it->glyph_row;
15865 Lisp_Object overlay_arrow_string;
15866
15867 /* We always start displaying at hpos zero even if hscrolled. */
15868 xassert (it->hpos == 0 && it->current_x == 0);
15869
15870 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15871 >= it->w->desired_matrix->nrows)
15872 {
15873 it->w->nrows_scale_factor++;
15874 fonts_changed_p = 1;
15875 return 0;
15876 }
15877
15878 /* Is IT->w showing the region? */
15879 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15880
15881 /* Clear the result glyph row and enable it. */
15882 prepare_desired_row (row);
15883
15884 row->y = it->current_y;
15885 row->start = it->start;
15886 row->continuation_lines_width = it->continuation_lines_width;
15887 row->displays_text_p = 1;
15888 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15889 it->starts_in_middle_of_char_p = 0;
15890
15891 /* Arrange the overlays nicely for our purposes. Usually, we call
15892 display_line on only one line at a time, in which case this
15893 can't really hurt too much, or we call it on lines which appear
15894 one after another in the buffer, in which case all calls to
15895 recenter_overlay_lists but the first will be pretty cheap. */
15896 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15897
15898 /* Move over display elements that are not visible because we are
15899 hscrolled. This may stop at an x-position < IT->first_visible_x
15900 if the first glyph is partially visible or if we hit a line end. */
15901 if (it->current_x < it->first_visible_x)
15902 {
15903 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15904 MOVE_TO_POS | MOVE_TO_X);
15905 }
15906
15907 /* Get the initial row height. This is either the height of the
15908 text hscrolled, if there is any, or zero. */
15909 row->ascent = it->max_ascent;
15910 row->height = it->max_ascent + it->max_descent;
15911 row->phys_ascent = it->max_phys_ascent;
15912 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15913 row->extra_line_spacing = it->max_extra_line_spacing;
15914
15915 /* Loop generating characters. The loop is left with IT on the next
15916 character to display. */
15917 while (1)
15918 {
15919 int n_glyphs_before, hpos_before, x_before;
15920 int x, i, nglyphs;
15921 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15922
15923 /* Retrieve the next thing to display. Value is zero if end of
15924 buffer reached. */
15925 if (!get_next_display_element (it))
15926 {
15927 /* Maybe add a space at the end of this line that is used to
15928 display the cursor there under X. Set the charpos of the
15929 first glyph of blank lines not corresponding to any text
15930 to -1. */
15931 #ifdef HAVE_WINDOW_SYSTEM
15932 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15933 row->exact_window_width_line_p = 1;
15934 else
15935 #endif /* HAVE_WINDOW_SYSTEM */
15936 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15937 || row->used[TEXT_AREA] == 0)
15938 {
15939 row->glyphs[TEXT_AREA]->charpos = -1;
15940 row->displays_text_p = 0;
15941
15942 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15943 && (!MINI_WINDOW_P (it->w)
15944 || (minibuf_level && EQ (it->window, minibuf_window))))
15945 row->indicate_empty_line_p = 1;
15946 }
15947
15948 it->continuation_lines_width = 0;
15949 row->ends_at_zv_p = 1;
15950 break;
15951 }
15952
15953 /* Now, get the metrics of what we want to display. This also
15954 generates glyphs in `row' (which is IT->glyph_row). */
15955 n_glyphs_before = row->used[TEXT_AREA];
15956 x = it->current_x;
15957
15958 /* Remember the line height so far in case the next element doesn't
15959 fit on the line. */
15960 if (!it->truncate_lines_p)
15961 {
15962 ascent = it->max_ascent;
15963 descent = it->max_descent;
15964 phys_ascent = it->max_phys_ascent;
15965 phys_descent = it->max_phys_descent;
15966 }
15967
15968 PRODUCE_GLYPHS (it);
15969
15970 /* If this display element was in marginal areas, continue with
15971 the next one. */
15972 if (it->area != TEXT_AREA)
15973 {
15974 row->ascent = max (row->ascent, it->max_ascent);
15975 row->height = max (row->height, it->max_ascent + it->max_descent);
15976 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15977 row->phys_height = max (row->phys_height,
15978 it->max_phys_ascent + it->max_phys_descent);
15979 row->extra_line_spacing = max (row->extra_line_spacing,
15980 it->max_extra_line_spacing);
15981 set_iterator_to_next (it, 1);
15982 continue;
15983 }
15984
15985 /* Does the display element fit on the line? If we truncate
15986 lines, we should draw past the right edge of the window. If
15987 we don't truncate, we want to stop so that we can display the
15988 continuation glyph before the right margin. If lines are
15989 continued, there are two possible strategies for characters
15990 resulting in more than 1 glyph (e.g. tabs): Display as many
15991 glyphs as possible in this line and leave the rest for the
15992 continuation line, or display the whole element in the next
15993 line. Original redisplay did the former, so we do it also. */
15994 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15995 hpos_before = it->hpos;
15996 x_before = x;
15997
15998 if (/* Not a newline. */
15999 nglyphs > 0
16000 /* Glyphs produced fit entirely in the line. */
16001 && it->current_x < it->last_visible_x)
16002 {
16003 it->hpos += nglyphs;
16004 row->ascent = max (row->ascent, it->max_ascent);
16005 row->height = max (row->height, it->max_ascent + it->max_descent);
16006 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16007 row->phys_height = max (row->phys_height,
16008 it->max_phys_ascent + it->max_phys_descent);
16009 row->extra_line_spacing = max (row->extra_line_spacing,
16010 it->max_extra_line_spacing);
16011 if (it->current_x - it->pixel_width < it->first_visible_x)
16012 row->x = x - it->first_visible_x;
16013 }
16014 else
16015 {
16016 int new_x;
16017 struct glyph *glyph;
16018
16019 for (i = 0; i < nglyphs; ++i, x = new_x)
16020 {
16021 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16022 new_x = x + glyph->pixel_width;
16023
16024 if (/* Lines are continued. */
16025 !it->truncate_lines_p
16026 && (/* Glyph doesn't fit on the line. */
16027 new_x > it->last_visible_x
16028 /* Or it fits exactly on a window system frame. */
16029 || (new_x == it->last_visible_x
16030 && FRAME_WINDOW_P (it->f))))
16031 {
16032 /* End of a continued line. */
16033
16034 if (it->hpos == 0
16035 || (new_x == it->last_visible_x
16036 && FRAME_WINDOW_P (it->f)))
16037 {
16038 /* Current glyph is the only one on the line or
16039 fits exactly on the line. We must continue
16040 the line because we can't draw the cursor
16041 after the glyph. */
16042 row->continued_p = 1;
16043 it->current_x = new_x;
16044 it->continuation_lines_width += new_x;
16045 ++it->hpos;
16046 if (i == nglyphs - 1)
16047 {
16048 set_iterator_to_next (it, 1);
16049 #ifdef HAVE_WINDOW_SYSTEM
16050 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16051 {
16052 if (!get_next_display_element (it))
16053 {
16054 row->exact_window_width_line_p = 1;
16055 it->continuation_lines_width = 0;
16056 row->continued_p = 0;
16057 row->ends_at_zv_p = 1;
16058 }
16059 else if (ITERATOR_AT_END_OF_LINE_P (it))
16060 {
16061 row->continued_p = 0;
16062 row->exact_window_width_line_p = 1;
16063 }
16064 }
16065 #endif /* HAVE_WINDOW_SYSTEM */
16066 }
16067 }
16068 else if (CHAR_GLYPH_PADDING_P (*glyph)
16069 && !FRAME_WINDOW_P (it->f))
16070 {
16071 /* A padding glyph that doesn't fit on this line.
16072 This means the whole character doesn't fit
16073 on the line. */
16074 row->used[TEXT_AREA] = n_glyphs_before;
16075
16076 /* Fill the rest of the row with continuation
16077 glyphs like in 20.x. */
16078 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16079 < row->glyphs[1 + TEXT_AREA])
16080 produce_special_glyphs (it, IT_CONTINUATION);
16081
16082 row->continued_p = 1;
16083 it->current_x = x_before;
16084 it->continuation_lines_width += x_before;
16085
16086 /* Restore the height to what it was before the
16087 element not fitting on the line. */
16088 it->max_ascent = ascent;
16089 it->max_descent = descent;
16090 it->max_phys_ascent = phys_ascent;
16091 it->max_phys_descent = phys_descent;
16092 }
16093 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16094 {
16095 /* A TAB that extends past the right edge of the
16096 window. This produces a single glyph on
16097 window system frames. We leave the glyph in
16098 this row and let it fill the row, but don't
16099 consume the TAB. */
16100 it->continuation_lines_width += it->last_visible_x;
16101 row->ends_in_middle_of_char_p = 1;
16102 row->continued_p = 1;
16103 glyph->pixel_width = it->last_visible_x - x;
16104 it->starts_in_middle_of_char_p = 1;
16105 }
16106 else
16107 {
16108 /* Something other than a TAB that draws past
16109 the right edge of the window. Restore
16110 positions to values before the element. */
16111 row->used[TEXT_AREA] = n_glyphs_before + i;
16112
16113 /* Display continuation glyphs. */
16114 if (!FRAME_WINDOW_P (it->f))
16115 produce_special_glyphs (it, IT_CONTINUATION);
16116 row->continued_p = 1;
16117
16118 it->current_x = x_before;
16119 it->continuation_lines_width += x;
16120 extend_face_to_end_of_line (it);
16121
16122 if (nglyphs > 1 && i > 0)
16123 {
16124 row->ends_in_middle_of_char_p = 1;
16125 it->starts_in_middle_of_char_p = 1;
16126 }
16127
16128 /* Restore the height to what it was before the
16129 element not fitting on the line. */
16130 it->max_ascent = ascent;
16131 it->max_descent = descent;
16132 it->max_phys_ascent = phys_ascent;
16133 it->max_phys_descent = phys_descent;
16134 }
16135
16136 break;
16137 }
16138 else if (new_x > it->first_visible_x)
16139 {
16140 /* Increment number of glyphs actually displayed. */
16141 ++it->hpos;
16142
16143 if (x < it->first_visible_x)
16144 /* Glyph is partially visible, i.e. row starts at
16145 negative X position. */
16146 row->x = x - it->first_visible_x;
16147 }
16148 else
16149 {
16150 /* Glyph is completely off the left margin of the
16151 window. This should not happen because of the
16152 move_it_in_display_line at the start of this
16153 function, unless the text display area of the
16154 window is empty. */
16155 xassert (it->first_visible_x <= it->last_visible_x);
16156 }
16157 }
16158
16159 row->ascent = max (row->ascent, it->max_ascent);
16160 row->height = max (row->height, it->max_ascent + it->max_descent);
16161 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16162 row->phys_height = max (row->phys_height,
16163 it->max_phys_ascent + it->max_phys_descent);
16164 row->extra_line_spacing = max (row->extra_line_spacing,
16165 it->max_extra_line_spacing);
16166
16167 /* End of this display line if row is continued. */
16168 if (row->continued_p || row->ends_at_zv_p)
16169 break;
16170 }
16171
16172 at_end_of_line:
16173 /* Is this a line end? If yes, we're also done, after making
16174 sure that a non-default face is extended up to the right
16175 margin of the window. */
16176 if (ITERATOR_AT_END_OF_LINE_P (it))
16177 {
16178 int used_before = row->used[TEXT_AREA];
16179
16180 row->ends_in_newline_from_string_p = STRINGP (it->object);
16181
16182 #ifdef HAVE_WINDOW_SYSTEM
16183 /* Add a space at the end of the line that is used to
16184 display the cursor there. */
16185 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16186 append_space_for_newline (it, 0);
16187 #endif /* HAVE_WINDOW_SYSTEM */
16188
16189 /* Extend the face to the end of the line. */
16190 extend_face_to_end_of_line (it);
16191
16192 /* Make sure we have the position. */
16193 if (used_before == 0)
16194 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16195
16196 /* Consume the line end. This skips over invisible lines. */
16197 set_iterator_to_next (it, 1);
16198 it->continuation_lines_width = 0;
16199 break;
16200 }
16201
16202 /* Proceed with next display element. Note that this skips
16203 over lines invisible because of selective display. */
16204 set_iterator_to_next (it, 1);
16205
16206 /* If we truncate lines, we are done when the last displayed
16207 glyphs reach past the right margin of the window. */
16208 if (it->truncate_lines_p
16209 && (FRAME_WINDOW_P (it->f)
16210 ? (it->current_x >= it->last_visible_x)
16211 : (it->current_x > it->last_visible_x)))
16212 {
16213 /* Maybe add truncation glyphs. */
16214 if (!FRAME_WINDOW_P (it->f))
16215 {
16216 int i, n;
16217
16218 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16219 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16220 break;
16221
16222 for (n = row->used[TEXT_AREA]; i < n; ++i)
16223 {
16224 row->used[TEXT_AREA] = i;
16225 produce_special_glyphs (it, IT_TRUNCATION);
16226 }
16227 }
16228 #ifdef HAVE_WINDOW_SYSTEM
16229 else
16230 {
16231 /* Don't truncate if we can overflow newline into fringe. */
16232 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16233 {
16234 if (!get_next_display_element (it))
16235 {
16236 it->continuation_lines_width = 0;
16237 row->ends_at_zv_p = 1;
16238 row->exact_window_width_line_p = 1;
16239 break;
16240 }
16241 if (ITERATOR_AT_END_OF_LINE_P (it))
16242 {
16243 row->exact_window_width_line_p = 1;
16244 goto at_end_of_line;
16245 }
16246 }
16247 }
16248 #endif /* HAVE_WINDOW_SYSTEM */
16249
16250 row->truncated_on_right_p = 1;
16251 it->continuation_lines_width = 0;
16252 reseat_at_next_visible_line_start (it, 0);
16253 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16254 it->hpos = hpos_before;
16255 it->current_x = x_before;
16256 break;
16257 }
16258 }
16259
16260 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16261 at the left window margin. */
16262 if (it->first_visible_x
16263 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16264 {
16265 if (!FRAME_WINDOW_P (it->f))
16266 insert_left_trunc_glyphs (it);
16267 row->truncated_on_left_p = 1;
16268 }
16269
16270 /* If the start of this line is the overlay arrow-position, then
16271 mark this glyph row as the one containing the overlay arrow.
16272 This is clearly a mess with variable size fonts. It would be
16273 better to let it be displayed like cursors under X. */
16274 if ((row->displays_text_p || !overlay_arrow_seen)
16275 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16276 !NILP (overlay_arrow_string)))
16277 {
16278 /* Overlay arrow in window redisplay is a fringe bitmap. */
16279 if (STRINGP (overlay_arrow_string))
16280 {
16281 struct glyph_row *arrow_row
16282 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16283 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16284 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16285 struct glyph *p = row->glyphs[TEXT_AREA];
16286 struct glyph *p2, *end;
16287
16288 /* Copy the arrow glyphs. */
16289 while (glyph < arrow_end)
16290 *p++ = *glyph++;
16291
16292 /* Throw away padding glyphs. */
16293 p2 = p;
16294 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16295 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16296 ++p2;
16297 if (p2 > p)
16298 {
16299 while (p2 < end)
16300 *p++ = *p2++;
16301 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16302 }
16303 }
16304 else
16305 {
16306 xassert (INTEGERP (overlay_arrow_string));
16307 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16308 }
16309 overlay_arrow_seen = 1;
16310 }
16311
16312 /* Compute pixel dimensions of this line. */
16313 compute_line_metrics (it);
16314
16315 /* Remember the position at which this line ends. */
16316 row->end = it->current;
16317
16318 /* Record whether this row ends inside an ellipsis. */
16319 row->ends_in_ellipsis_p
16320 = (it->method == GET_FROM_DISPLAY_VECTOR
16321 && it->ellipsis_p);
16322
16323 /* Save fringe bitmaps in this row. */
16324 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16325 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16326 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16327 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16328
16329 it->left_user_fringe_bitmap = 0;
16330 it->left_user_fringe_face_id = 0;
16331 it->right_user_fringe_bitmap = 0;
16332 it->right_user_fringe_face_id = 0;
16333
16334 /* Maybe set the cursor. */
16335 if (it->w->cursor.vpos < 0
16336 && PT >= MATRIX_ROW_START_CHARPOS (row)
16337 && PT <= MATRIX_ROW_END_CHARPOS (row)
16338 && cursor_row_p (it->w, row))
16339 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16340
16341 /* Highlight trailing whitespace. */
16342 if (!NILP (Vshow_trailing_whitespace))
16343 highlight_trailing_whitespace (it->f, it->glyph_row);
16344
16345 /* Prepare for the next line. This line starts horizontally at (X
16346 HPOS) = (0 0). Vertical positions are incremented. As a
16347 convenience for the caller, IT->glyph_row is set to the next
16348 row to be used. */
16349 it->current_x = it->hpos = 0;
16350 it->current_y += row->height;
16351 ++it->vpos;
16352 ++it->glyph_row;
16353 it->start = it->current;
16354 return row->displays_text_p;
16355 }
16356
16357
16358 \f
16359 /***********************************************************************
16360 Menu Bar
16361 ***********************************************************************/
16362
16363 /* Redisplay the menu bar in the frame for window W.
16364
16365 The menu bar of X frames that don't have X toolkit support is
16366 displayed in a special window W->frame->menu_bar_window.
16367
16368 The menu bar of terminal frames is treated specially as far as
16369 glyph matrices are concerned. Menu bar lines are not part of
16370 windows, so the update is done directly on the frame matrix rows
16371 for the menu bar. */
16372
16373 static void
16374 display_menu_bar (w)
16375 struct window *w;
16376 {
16377 struct frame *f = XFRAME (WINDOW_FRAME (w));
16378 struct it it;
16379 Lisp_Object items;
16380 int i;
16381
16382 /* Don't do all this for graphical frames. */
16383 #ifdef HAVE_NTGUI
16384 if (!NILP (Vwindow_system))
16385 return;
16386 #endif
16387 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16388 if (FRAME_X_P (f))
16389 return;
16390 #endif
16391 #ifdef MAC_OS
16392 if (FRAME_MAC_P (f))
16393 return;
16394 #endif
16395
16396 #ifdef USE_X_TOOLKIT
16397 xassert (!FRAME_WINDOW_P (f));
16398 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16399 it.first_visible_x = 0;
16400 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16401 #else /* not USE_X_TOOLKIT */
16402 if (FRAME_WINDOW_P (f))
16403 {
16404 /* Menu bar lines are displayed in the desired matrix of the
16405 dummy window menu_bar_window. */
16406 struct window *menu_w;
16407 xassert (WINDOWP (f->menu_bar_window));
16408 menu_w = XWINDOW (f->menu_bar_window);
16409 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16410 MENU_FACE_ID);
16411 it.first_visible_x = 0;
16412 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16413 }
16414 else
16415 {
16416 /* This is a TTY frame, i.e. character hpos/vpos are used as
16417 pixel x/y. */
16418 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16419 MENU_FACE_ID);
16420 it.first_visible_x = 0;
16421 it.last_visible_x = FRAME_COLS (f);
16422 }
16423 #endif /* not USE_X_TOOLKIT */
16424
16425 if (! mode_line_inverse_video)
16426 /* Force the menu-bar to be displayed in the default face. */
16427 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16428
16429 /* Clear all rows of the menu bar. */
16430 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16431 {
16432 struct glyph_row *row = it.glyph_row + i;
16433 clear_glyph_row (row);
16434 row->enabled_p = 1;
16435 row->full_width_p = 1;
16436 }
16437
16438 /* Display all items of the menu bar. */
16439 items = FRAME_MENU_BAR_ITEMS (it.f);
16440 for (i = 0; i < XVECTOR (items)->size; i += 4)
16441 {
16442 Lisp_Object string;
16443
16444 /* Stop at nil string. */
16445 string = AREF (items, i + 1);
16446 if (NILP (string))
16447 break;
16448
16449 /* Remember where item was displayed. */
16450 AREF (items, i + 3) = make_number (it.hpos);
16451
16452 /* Display the item, pad with one space. */
16453 if (it.current_x < it.last_visible_x)
16454 display_string (NULL, string, Qnil, 0, 0, &it,
16455 SCHARS (string) + 1, 0, 0, -1);
16456 }
16457
16458 /* Fill out the line with spaces. */
16459 if (it.current_x < it.last_visible_x)
16460 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16461
16462 /* Compute the total height of the lines. */
16463 compute_line_metrics (&it);
16464 }
16465
16466
16467 \f
16468 /***********************************************************************
16469 Mode Line
16470 ***********************************************************************/
16471
16472 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16473 FORCE is non-zero, redisplay mode lines unconditionally.
16474 Otherwise, redisplay only mode lines that are garbaged. Value is
16475 the number of windows whose mode lines were redisplayed. */
16476
16477 static int
16478 redisplay_mode_lines (window, force)
16479 Lisp_Object window;
16480 int force;
16481 {
16482 int nwindows = 0;
16483
16484 while (!NILP (window))
16485 {
16486 struct window *w = XWINDOW (window);
16487
16488 if (WINDOWP (w->hchild))
16489 nwindows += redisplay_mode_lines (w->hchild, force);
16490 else if (WINDOWP (w->vchild))
16491 nwindows += redisplay_mode_lines (w->vchild, force);
16492 else if (force
16493 || FRAME_GARBAGED_P (XFRAME (w->frame))
16494 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16495 {
16496 struct text_pos lpoint;
16497 struct buffer *old = current_buffer;
16498
16499 /* Set the window's buffer for the mode line display. */
16500 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16501 set_buffer_internal_1 (XBUFFER (w->buffer));
16502
16503 /* Point refers normally to the selected window. For any
16504 other window, set up appropriate value. */
16505 if (!EQ (window, selected_window))
16506 {
16507 struct text_pos pt;
16508
16509 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16510 if (CHARPOS (pt) < BEGV)
16511 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16512 else if (CHARPOS (pt) > (ZV - 1))
16513 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16514 else
16515 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16516 }
16517
16518 /* Display mode lines. */
16519 clear_glyph_matrix (w->desired_matrix);
16520 if (display_mode_lines (w))
16521 {
16522 ++nwindows;
16523 w->must_be_updated_p = 1;
16524 }
16525
16526 /* Restore old settings. */
16527 set_buffer_internal_1 (old);
16528 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16529 }
16530
16531 window = w->next;
16532 }
16533
16534 return nwindows;
16535 }
16536
16537
16538 /* Display the mode and/or top line of window W. Value is the number
16539 of mode lines displayed. */
16540
16541 static int
16542 display_mode_lines (w)
16543 struct window *w;
16544 {
16545 Lisp_Object old_selected_window, old_selected_frame;
16546 int n = 0;
16547
16548 old_selected_frame = selected_frame;
16549 selected_frame = w->frame;
16550 old_selected_window = selected_window;
16551 XSETWINDOW (selected_window, w);
16552
16553 /* These will be set while the mode line specs are processed. */
16554 line_number_displayed = 0;
16555 w->column_number_displayed = Qnil;
16556
16557 if (WINDOW_WANTS_MODELINE_P (w))
16558 {
16559 struct window *sel_w = XWINDOW (old_selected_window);
16560
16561 /* Select mode line face based on the real selected window. */
16562 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16563 current_buffer->mode_line_format);
16564 ++n;
16565 }
16566
16567 if (WINDOW_WANTS_HEADER_LINE_P (w))
16568 {
16569 display_mode_line (w, HEADER_LINE_FACE_ID,
16570 current_buffer->header_line_format);
16571 ++n;
16572 }
16573
16574 selected_frame = old_selected_frame;
16575 selected_window = old_selected_window;
16576 return n;
16577 }
16578
16579
16580 /* Display mode or top line of window W. FACE_ID specifies which line
16581 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16582 FORMAT is the mode line format to display. Value is the pixel
16583 height of the mode line displayed. */
16584
16585 static int
16586 display_mode_line (w, face_id, format)
16587 struct window *w;
16588 enum face_id face_id;
16589 Lisp_Object format;
16590 {
16591 struct it it;
16592 struct face *face;
16593 int count = SPECPDL_INDEX ();
16594
16595 init_iterator (&it, w, -1, -1, NULL, face_id);
16596 /* Don't extend on a previously drawn mode-line.
16597 This may happen if called from pos_visible_p. */
16598 it.glyph_row->enabled_p = 0;
16599 prepare_desired_row (it.glyph_row);
16600
16601 it.glyph_row->mode_line_p = 1;
16602
16603 if (! mode_line_inverse_video)
16604 /* Force the mode-line to be displayed in the default face. */
16605 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16606
16607 record_unwind_protect (unwind_format_mode_line,
16608 format_mode_line_unwind_data (NULL, 0));
16609
16610 mode_line_target = MODE_LINE_DISPLAY;
16611
16612 /* Temporarily make frame's keyboard the current kboard so that
16613 kboard-local variables in the mode_line_format will get the right
16614 values. */
16615 push_frame_kboard (it.f);
16616 record_unwind_save_match_data ();
16617 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16618 pop_frame_kboard ();
16619
16620 unbind_to (count, Qnil);
16621
16622 /* Fill up with spaces. */
16623 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16624
16625 compute_line_metrics (&it);
16626 it.glyph_row->full_width_p = 1;
16627 it.glyph_row->continued_p = 0;
16628 it.glyph_row->truncated_on_left_p = 0;
16629 it.glyph_row->truncated_on_right_p = 0;
16630
16631 /* Make a 3D mode-line have a shadow at its right end. */
16632 face = FACE_FROM_ID (it.f, face_id);
16633 extend_face_to_end_of_line (&it);
16634 if (face->box != FACE_NO_BOX)
16635 {
16636 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16637 + it.glyph_row->used[TEXT_AREA] - 1);
16638 last->right_box_line_p = 1;
16639 }
16640
16641 return it.glyph_row->height;
16642 }
16643
16644 /* Move element ELT in LIST to the front of LIST.
16645 Return the updated list. */
16646
16647 static Lisp_Object
16648 move_elt_to_front (elt, list)
16649 Lisp_Object elt, list;
16650 {
16651 register Lisp_Object tail, prev;
16652 register Lisp_Object tem;
16653
16654 tail = list;
16655 prev = Qnil;
16656 while (CONSP (tail))
16657 {
16658 tem = XCAR (tail);
16659
16660 if (EQ (elt, tem))
16661 {
16662 /* Splice out the link TAIL. */
16663 if (NILP (prev))
16664 list = XCDR (tail);
16665 else
16666 Fsetcdr (prev, XCDR (tail));
16667
16668 /* Now make it the first. */
16669 Fsetcdr (tail, list);
16670 return tail;
16671 }
16672 else
16673 prev = tail;
16674 tail = XCDR (tail);
16675 QUIT;
16676 }
16677
16678 /* Not found--return unchanged LIST. */
16679 return list;
16680 }
16681
16682 /* Contribute ELT to the mode line for window IT->w. How it
16683 translates into text depends on its data type.
16684
16685 IT describes the display environment in which we display, as usual.
16686
16687 DEPTH is the depth in recursion. It is used to prevent
16688 infinite recursion here.
16689
16690 FIELD_WIDTH is the number of characters the display of ELT should
16691 occupy in the mode line, and PRECISION is the maximum number of
16692 characters to display from ELT's representation. See
16693 display_string for details.
16694
16695 Returns the hpos of the end of the text generated by ELT.
16696
16697 PROPS is a property list to add to any string we encounter.
16698
16699 If RISKY is nonzero, remove (disregard) any properties in any string
16700 we encounter, and ignore :eval and :propertize.
16701
16702 The global variable `mode_line_target' determines whether the
16703 output is passed to `store_mode_line_noprop',
16704 `store_mode_line_string', or `display_string'. */
16705
16706 static int
16707 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16708 struct it *it;
16709 int depth;
16710 int field_width, precision;
16711 Lisp_Object elt, props;
16712 int risky;
16713 {
16714 int n = 0, field, prec;
16715 int literal = 0;
16716
16717 tail_recurse:
16718 if (depth > 100)
16719 elt = build_string ("*too-deep*");
16720
16721 depth++;
16722
16723 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16724 {
16725 case Lisp_String:
16726 {
16727 /* A string: output it and check for %-constructs within it. */
16728 unsigned char c;
16729 int offset = 0;
16730
16731 if (SCHARS (elt) > 0
16732 && (!NILP (props) || risky))
16733 {
16734 Lisp_Object oprops, aelt;
16735 oprops = Ftext_properties_at (make_number (0), elt);
16736
16737 /* If the starting string's properties are not what
16738 we want, translate the string. Also, if the string
16739 is risky, do that anyway. */
16740
16741 if (NILP (Fequal (props, oprops)) || risky)
16742 {
16743 /* If the starting string has properties,
16744 merge the specified ones onto the existing ones. */
16745 if (! NILP (oprops) && !risky)
16746 {
16747 Lisp_Object tem;
16748
16749 oprops = Fcopy_sequence (oprops);
16750 tem = props;
16751 while (CONSP (tem))
16752 {
16753 oprops = Fplist_put (oprops, XCAR (tem),
16754 XCAR (XCDR (tem)));
16755 tem = XCDR (XCDR (tem));
16756 }
16757 props = oprops;
16758 }
16759
16760 aelt = Fassoc (elt, mode_line_proptrans_alist);
16761 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16762 {
16763 /* AELT is what we want. Move it to the front
16764 without consing. */
16765 elt = XCAR (aelt);
16766 mode_line_proptrans_alist
16767 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16768 }
16769 else
16770 {
16771 Lisp_Object tem;
16772
16773 /* If AELT has the wrong props, it is useless.
16774 so get rid of it. */
16775 if (! NILP (aelt))
16776 mode_line_proptrans_alist
16777 = Fdelq (aelt, mode_line_proptrans_alist);
16778
16779 elt = Fcopy_sequence (elt);
16780 Fset_text_properties (make_number (0), Flength (elt),
16781 props, elt);
16782 /* Add this item to mode_line_proptrans_alist. */
16783 mode_line_proptrans_alist
16784 = Fcons (Fcons (elt, props),
16785 mode_line_proptrans_alist);
16786 /* Truncate mode_line_proptrans_alist
16787 to at most 50 elements. */
16788 tem = Fnthcdr (make_number (50),
16789 mode_line_proptrans_alist);
16790 if (! NILP (tem))
16791 XSETCDR (tem, Qnil);
16792 }
16793 }
16794 }
16795
16796 offset = 0;
16797
16798 if (literal)
16799 {
16800 prec = precision - n;
16801 switch (mode_line_target)
16802 {
16803 case MODE_LINE_NOPROP:
16804 case MODE_LINE_TITLE:
16805 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16806 break;
16807 case MODE_LINE_STRING:
16808 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16809 break;
16810 case MODE_LINE_DISPLAY:
16811 n += display_string (NULL, elt, Qnil, 0, 0, it,
16812 0, prec, 0, STRING_MULTIBYTE (elt));
16813 break;
16814 }
16815
16816 break;
16817 }
16818
16819 /* Handle the non-literal case. */
16820
16821 while ((precision <= 0 || n < precision)
16822 && SREF (elt, offset) != 0
16823 && (mode_line_target != MODE_LINE_DISPLAY
16824 || it->current_x < it->last_visible_x))
16825 {
16826 int last_offset = offset;
16827
16828 /* Advance to end of string or next format specifier. */
16829 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16830 ;
16831
16832 if (offset - 1 != last_offset)
16833 {
16834 int nchars, nbytes;
16835
16836 /* Output to end of string or up to '%'. Field width
16837 is length of string. Don't output more than
16838 PRECISION allows us. */
16839 offset--;
16840
16841 prec = c_string_width (SDATA (elt) + last_offset,
16842 offset - last_offset, precision - n,
16843 &nchars, &nbytes);
16844
16845 switch (mode_line_target)
16846 {
16847 case MODE_LINE_NOPROP:
16848 case MODE_LINE_TITLE:
16849 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16850 break;
16851 case MODE_LINE_STRING:
16852 {
16853 int bytepos = last_offset;
16854 int charpos = string_byte_to_char (elt, bytepos);
16855 int endpos = (precision <= 0
16856 ? string_byte_to_char (elt, offset)
16857 : charpos + nchars);
16858
16859 n += store_mode_line_string (NULL,
16860 Fsubstring (elt, make_number (charpos),
16861 make_number (endpos)),
16862 0, 0, 0, Qnil);
16863 }
16864 break;
16865 case MODE_LINE_DISPLAY:
16866 {
16867 int bytepos = last_offset;
16868 int charpos = string_byte_to_char (elt, bytepos);
16869
16870 if (precision <= 0)
16871 nchars = string_byte_to_char (elt, offset) - charpos;
16872 n += display_string (NULL, elt, Qnil, 0, charpos,
16873 it, 0, nchars, 0,
16874 STRING_MULTIBYTE (elt));
16875 }
16876 break;
16877 }
16878 }
16879 else /* c == '%' */
16880 {
16881 int percent_position = offset;
16882
16883 /* Get the specified minimum width. Zero means
16884 don't pad. */
16885 field = 0;
16886 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16887 field = field * 10 + c - '0';
16888
16889 /* Don't pad beyond the total padding allowed. */
16890 if (field_width - n > 0 && field > field_width - n)
16891 field = field_width - n;
16892
16893 /* Note that either PRECISION <= 0 or N < PRECISION. */
16894 prec = precision - n;
16895
16896 if (c == 'M')
16897 n += display_mode_element (it, depth, field, prec,
16898 Vglobal_mode_string, props,
16899 risky);
16900 else if (c != 0)
16901 {
16902 int multibyte;
16903 int bytepos, charpos;
16904 unsigned char *spec;
16905
16906 bytepos = percent_position;
16907 charpos = (STRING_MULTIBYTE (elt)
16908 ? string_byte_to_char (elt, bytepos)
16909 : bytepos);
16910
16911 spec
16912 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16913
16914 switch (mode_line_target)
16915 {
16916 case MODE_LINE_NOPROP:
16917 case MODE_LINE_TITLE:
16918 n += store_mode_line_noprop (spec, field, prec);
16919 break;
16920 case MODE_LINE_STRING:
16921 {
16922 int len = strlen (spec);
16923 Lisp_Object tem = make_string (spec, len);
16924 props = Ftext_properties_at (make_number (charpos), elt);
16925 /* Should only keep face property in props */
16926 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16927 }
16928 break;
16929 case MODE_LINE_DISPLAY:
16930 {
16931 int nglyphs_before, nwritten;
16932
16933 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16934 nwritten = display_string (spec, Qnil, elt,
16935 charpos, 0, it,
16936 field, prec, 0,
16937 multibyte);
16938
16939 /* Assign to the glyphs written above the
16940 string where the `%x' came from, position
16941 of the `%'. */
16942 if (nwritten > 0)
16943 {
16944 struct glyph *glyph
16945 = (it->glyph_row->glyphs[TEXT_AREA]
16946 + nglyphs_before);
16947 int i;
16948
16949 for (i = 0; i < nwritten; ++i)
16950 {
16951 glyph[i].object = elt;
16952 glyph[i].charpos = charpos;
16953 }
16954
16955 n += nwritten;
16956 }
16957 }
16958 break;
16959 }
16960 }
16961 else /* c == 0 */
16962 break;
16963 }
16964 }
16965 }
16966 break;
16967
16968 case Lisp_Symbol:
16969 /* A symbol: process the value of the symbol recursively
16970 as if it appeared here directly. Avoid error if symbol void.
16971 Special case: if value of symbol is a string, output the string
16972 literally. */
16973 {
16974 register Lisp_Object tem;
16975
16976 /* If the variable is not marked as risky to set
16977 then its contents are risky to use. */
16978 if (NILP (Fget (elt, Qrisky_local_variable)))
16979 risky = 1;
16980
16981 tem = Fboundp (elt);
16982 if (!NILP (tem))
16983 {
16984 tem = Fsymbol_value (elt);
16985 /* If value is a string, output that string literally:
16986 don't check for % within it. */
16987 if (STRINGP (tem))
16988 literal = 1;
16989
16990 if (!EQ (tem, elt))
16991 {
16992 /* Give up right away for nil or t. */
16993 elt = tem;
16994 goto tail_recurse;
16995 }
16996 }
16997 }
16998 break;
16999
17000 case Lisp_Cons:
17001 {
17002 register Lisp_Object car, tem;
17003
17004 /* A cons cell: five distinct cases.
17005 If first element is :eval or :propertize, do something special.
17006 If first element is a string or a cons, process all the elements
17007 and effectively concatenate them.
17008 If first element is a negative number, truncate displaying cdr to
17009 at most that many characters. If positive, pad (with spaces)
17010 to at least that many characters.
17011 If first element is a symbol, process the cadr or caddr recursively
17012 according to whether the symbol's value is non-nil or nil. */
17013 car = XCAR (elt);
17014 if (EQ (car, QCeval))
17015 {
17016 /* An element of the form (:eval FORM) means evaluate FORM
17017 and use the result as mode line elements. */
17018
17019 if (risky)
17020 break;
17021
17022 if (CONSP (XCDR (elt)))
17023 {
17024 Lisp_Object spec;
17025 spec = safe_eval (XCAR (XCDR (elt)));
17026 n += display_mode_element (it, depth, field_width - n,
17027 precision - n, spec, props,
17028 risky);
17029 }
17030 }
17031 else if (EQ (car, QCpropertize))
17032 {
17033 /* An element of the form (:propertize ELT PROPS...)
17034 means display ELT but applying properties PROPS. */
17035
17036 if (risky)
17037 break;
17038
17039 if (CONSP (XCDR (elt)))
17040 n += display_mode_element (it, depth, field_width - n,
17041 precision - n, XCAR (XCDR (elt)),
17042 XCDR (XCDR (elt)), risky);
17043 }
17044 else if (SYMBOLP (car))
17045 {
17046 tem = Fboundp (car);
17047 elt = XCDR (elt);
17048 if (!CONSP (elt))
17049 goto invalid;
17050 /* elt is now the cdr, and we know it is a cons cell.
17051 Use its car if CAR has a non-nil value. */
17052 if (!NILP (tem))
17053 {
17054 tem = Fsymbol_value (car);
17055 if (!NILP (tem))
17056 {
17057 elt = XCAR (elt);
17058 goto tail_recurse;
17059 }
17060 }
17061 /* Symbol's value is nil (or symbol is unbound)
17062 Get the cddr of the original list
17063 and if possible find the caddr and use that. */
17064 elt = XCDR (elt);
17065 if (NILP (elt))
17066 break;
17067 else if (!CONSP (elt))
17068 goto invalid;
17069 elt = XCAR (elt);
17070 goto tail_recurse;
17071 }
17072 else if (INTEGERP (car))
17073 {
17074 register int lim = XINT (car);
17075 elt = XCDR (elt);
17076 if (lim < 0)
17077 {
17078 /* Negative int means reduce maximum width. */
17079 if (precision <= 0)
17080 precision = -lim;
17081 else
17082 precision = min (precision, -lim);
17083 }
17084 else if (lim > 0)
17085 {
17086 /* Padding specified. Don't let it be more than
17087 current maximum. */
17088 if (precision > 0)
17089 lim = min (precision, lim);
17090
17091 /* If that's more padding than already wanted, queue it.
17092 But don't reduce padding already specified even if
17093 that is beyond the current truncation point. */
17094 field_width = max (lim, field_width);
17095 }
17096 goto tail_recurse;
17097 }
17098 else if (STRINGP (car) || CONSP (car))
17099 {
17100 register int limit = 50;
17101 /* Limit is to protect against circular lists. */
17102 while (CONSP (elt)
17103 && --limit > 0
17104 && (precision <= 0 || n < precision))
17105 {
17106 n += display_mode_element (it, depth,
17107 /* Do padding only after the last
17108 element in the list. */
17109 (! CONSP (XCDR (elt))
17110 ? field_width - n
17111 : 0),
17112 precision - n, XCAR (elt),
17113 props, risky);
17114 elt = XCDR (elt);
17115 }
17116 }
17117 }
17118 break;
17119
17120 default:
17121 invalid:
17122 elt = build_string ("*invalid*");
17123 goto tail_recurse;
17124 }
17125
17126 /* Pad to FIELD_WIDTH. */
17127 if (field_width > 0 && n < field_width)
17128 {
17129 switch (mode_line_target)
17130 {
17131 case MODE_LINE_NOPROP:
17132 case MODE_LINE_TITLE:
17133 n += store_mode_line_noprop ("", field_width - n, 0);
17134 break;
17135 case MODE_LINE_STRING:
17136 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17137 break;
17138 case MODE_LINE_DISPLAY:
17139 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17140 0, 0, 0);
17141 break;
17142 }
17143 }
17144
17145 return n;
17146 }
17147
17148 /* Store a mode-line string element in mode_line_string_list.
17149
17150 If STRING is non-null, display that C string. Otherwise, the Lisp
17151 string LISP_STRING is displayed.
17152
17153 FIELD_WIDTH is the minimum number of output glyphs to produce.
17154 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17155 with spaces. FIELD_WIDTH <= 0 means don't pad.
17156
17157 PRECISION is the maximum number of characters to output from
17158 STRING. PRECISION <= 0 means don't truncate the string.
17159
17160 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17161 properties to the string.
17162
17163 PROPS are the properties to add to the string.
17164 The mode_line_string_face face property is always added to the string.
17165 */
17166
17167 static int
17168 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17169 char *string;
17170 Lisp_Object lisp_string;
17171 int copy_string;
17172 int field_width;
17173 int precision;
17174 Lisp_Object props;
17175 {
17176 int len;
17177 int n = 0;
17178
17179 if (string != NULL)
17180 {
17181 len = strlen (string);
17182 if (precision > 0 && len > precision)
17183 len = precision;
17184 lisp_string = make_string (string, len);
17185 if (NILP (props))
17186 props = mode_line_string_face_prop;
17187 else if (!NILP (mode_line_string_face))
17188 {
17189 Lisp_Object face = Fplist_get (props, Qface);
17190 props = Fcopy_sequence (props);
17191 if (NILP (face))
17192 face = mode_line_string_face;
17193 else
17194 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17195 props = Fplist_put (props, Qface, face);
17196 }
17197 Fadd_text_properties (make_number (0), make_number (len),
17198 props, lisp_string);
17199 }
17200 else
17201 {
17202 len = XFASTINT (Flength (lisp_string));
17203 if (precision > 0 && len > precision)
17204 {
17205 len = precision;
17206 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17207 precision = -1;
17208 }
17209 if (!NILP (mode_line_string_face))
17210 {
17211 Lisp_Object face;
17212 if (NILP (props))
17213 props = Ftext_properties_at (make_number (0), lisp_string);
17214 face = Fplist_get (props, Qface);
17215 if (NILP (face))
17216 face = mode_line_string_face;
17217 else
17218 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17219 props = Fcons (Qface, Fcons (face, Qnil));
17220 if (copy_string)
17221 lisp_string = Fcopy_sequence (lisp_string);
17222 }
17223 if (!NILP (props))
17224 Fadd_text_properties (make_number (0), make_number (len),
17225 props, lisp_string);
17226 }
17227
17228 if (len > 0)
17229 {
17230 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17231 n += len;
17232 }
17233
17234 if (field_width > len)
17235 {
17236 field_width -= len;
17237 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17238 if (!NILP (props))
17239 Fadd_text_properties (make_number (0), make_number (field_width),
17240 props, lisp_string);
17241 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17242 n += field_width;
17243 }
17244
17245 return n;
17246 }
17247
17248
17249 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17250 1, 4, 0,
17251 doc: /* Format a string out of a mode line format specification.
17252 First arg FORMAT specifies the mode line format (see `mode-line-format'
17253 for details) to use.
17254
17255 Optional second arg FACE specifies the face property to put
17256 on all characters for which no face is specified.
17257 t means whatever face the window's mode line currently uses
17258 \(either `mode-line' or `mode-line-inactive', depending).
17259 nil means the default is no face property.
17260 If FACE is an integer, the value string has no text properties.
17261
17262 Optional third and fourth args WINDOW and BUFFER specify the window
17263 and buffer to use as the context for the formatting (defaults
17264 are the selected window and the window's buffer). */)
17265 (format, face, window, buffer)
17266 Lisp_Object format, face, window, buffer;
17267 {
17268 struct it it;
17269 int len;
17270 struct window *w;
17271 struct buffer *old_buffer = NULL;
17272 int face_id = -1;
17273 int no_props = INTEGERP (face);
17274 int count = SPECPDL_INDEX ();
17275 Lisp_Object str;
17276 int string_start = 0;
17277
17278 if (NILP (window))
17279 window = selected_window;
17280 CHECK_WINDOW (window);
17281 w = XWINDOW (window);
17282
17283 if (NILP (buffer))
17284 buffer = w->buffer;
17285 CHECK_BUFFER (buffer);
17286
17287 if (NILP (format))
17288 return build_string ("");
17289
17290 if (no_props)
17291 face = Qnil;
17292
17293 if (!NILP (face))
17294 {
17295 if (EQ (face, Qt))
17296 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17297 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17298 }
17299
17300 if (face_id < 0)
17301 face_id = DEFAULT_FACE_ID;
17302
17303 if (XBUFFER (buffer) != current_buffer)
17304 old_buffer = current_buffer;
17305
17306 /* Save things including mode_line_proptrans_alist,
17307 and set that to nil so that we don't alter the outer value. */
17308 record_unwind_protect (unwind_format_mode_line,
17309 format_mode_line_unwind_data (old_buffer, 1));
17310 mode_line_proptrans_alist = Qnil;
17311
17312 if (old_buffer)
17313 set_buffer_internal_1 (XBUFFER (buffer));
17314
17315 init_iterator (&it, w, -1, -1, NULL, face_id);
17316
17317 if (no_props)
17318 {
17319 mode_line_target = MODE_LINE_NOPROP;
17320 mode_line_string_face_prop = Qnil;
17321 mode_line_string_list = Qnil;
17322 string_start = MODE_LINE_NOPROP_LEN (0);
17323 }
17324 else
17325 {
17326 mode_line_target = MODE_LINE_STRING;
17327 mode_line_string_list = Qnil;
17328 mode_line_string_face = face;
17329 mode_line_string_face_prop
17330 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17331 }
17332
17333 push_frame_kboard (it.f);
17334 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17335 pop_frame_kboard ();
17336
17337 if (no_props)
17338 {
17339 len = MODE_LINE_NOPROP_LEN (string_start);
17340 str = make_string (mode_line_noprop_buf + string_start, len);
17341 }
17342 else
17343 {
17344 mode_line_string_list = Fnreverse (mode_line_string_list);
17345 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17346 make_string ("", 0));
17347 }
17348
17349 unbind_to (count, Qnil);
17350 return str;
17351 }
17352
17353 /* Write a null-terminated, right justified decimal representation of
17354 the positive integer D to BUF using a minimal field width WIDTH. */
17355
17356 static void
17357 pint2str (buf, width, d)
17358 register char *buf;
17359 register int width;
17360 register int d;
17361 {
17362 register char *p = buf;
17363
17364 if (d <= 0)
17365 *p++ = '0';
17366 else
17367 {
17368 while (d > 0)
17369 {
17370 *p++ = d % 10 + '0';
17371 d /= 10;
17372 }
17373 }
17374
17375 for (width -= (int) (p - buf); width > 0; --width)
17376 *p++ = ' ';
17377 *p-- = '\0';
17378 while (p > buf)
17379 {
17380 d = *buf;
17381 *buf++ = *p;
17382 *p-- = d;
17383 }
17384 }
17385
17386 /* Write a null-terminated, right justified decimal and "human
17387 readable" representation of the nonnegative integer D to BUF using
17388 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17389
17390 static const char power_letter[] =
17391 {
17392 0, /* not used */
17393 'k', /* kilo */
17394 'M', /* mega */
17395 'G', /* giga */
17396 'T', /* tera */
17397 'P', /* peta */
17398 'E', /* exa */
17399 'Z', /* zetta */
17400 'Y' /* yotta */
17401 };
17402
17403 static void
17404 pint2hrstr (buf, width, d)
17405 char *buf;
17406 int width;
17407 int d;
17408 {
17409 /* We aim to represent the nonnegative integer D as
17410 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17411 int quotient = d;
17412 int remainder = 0;
17413 /* -1 means: do not use TENTHS. */
17414 int tenths = -1;
17415 int exponent = 0;
17416
17417 /* Length of QUOTIENT.TENTHS as a string. */
17418 int length;
17419
17420 char * psuffix;
17421 char * p;
17422
17423 if (1000 <= quotient)
17424 {
17425 /* Scale to the appropriate EXPONENT. */
17426 do
17427 {
17428 remainder = quotient % 1000;
17429 quotient /= 1000;
17430 exponent++;
17431 }
17432 while (1000 <= quotient);
17433
17434 /* Round to nearest and decide whether to use TENTHS or not. */
17435 if (quotient <= 9)
17436 {
17437 tenths = remainder / 100;
17438 if (50 <= remainder % 100)
17439 {
17440 if (tenths < 9)
17441 tenths++;
17442 else
17443 {
17444 quotient++;
17445 if (quotient == 10)
17446 tenths = -1;
17447 else
17448 tenths = 0;
17449 }
17450 }
17451 }
17452 else
17453 if (500 <= remainder)
17454 {
17455 if (quotient < 999)
17456 quotient++;
17457 else
17458 {
17459 quotient = 1;
17460 exponent++;
17461 tenths = 0;
17462 }
17463 }
17464 }
17465
17466 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17467 if (tenths == -1 && quotient <= 99)
17468 if (quotient <= 9)
17469 length = 1;
17470 else
17471 length = 2;
17472 else
17473 length = 3;
17474 p = psuffix = buf + max (width, length);
17475
17476 /* Print EXPONENT. */
17477 if (exponent)
17478 *psuffix++ = power_letter[exponent];
17479 *psuffix = '\0';
17480
17481 /* Print TENTHS. */
17482 if (tenths >= 0)
17483 {
17484 *--p = '0' + tenths;
17485 *--p = '.';
17486 }
17487
17488 /* Print QUOTIENT. */
17489 do
17490 {
17491 int digit = quotient % 10;
17492 *--p = '0' + digit;
17493 }
17494 while ((quotient /= 10) != 0);
17495
17496 /* Print leading spaces. */
17497 while (buf < p)
17498 *--p = ' ';
17499 }
17500
17501 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17502 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17503 type of CODING_SYSTEM. Return updated pointer into BUF. */
17504
17505 static unsigned char invalid_eol_type[] = "(*invalid*)";
17506
17507 static char *
17508 decode_mode_spec_coding (coding_system, buf, eol_flag)
17509 Lisp_Object coding_system;
17510 register char *buf;
17511 int eol_flag;
17512 {
17513 Lisp_Object val;
17514 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17515 const unsigned char *eol_str;
17516 int eol_str_len;
17517 /* The EOL conversion we are using. */
17518 Lisp_Object eoltype;
17519
17520 val = Fget (coding_system, Qcoding_system);
17521 eoltype = Qnil;
17522
17523 if (!VECTORP (val)) /* Not yet decided. */
17524 {
17525 if (multibyte)
17526 *buf++ = '-';
17527 if (eol_flag)
17528 eoltype = eol_mnemonic_undecided;
17529 /* Don't mention EOL conversion if it isn't decided. */
17530 }
17531 else
17532 {
17533 Lisp_Object eolvalue;
17534
17535 eolvalue = Fget (coding_system, Qeol_type);
17536
17537 if (multibyte)
17538 *buf++ = XFASTINT (AREF (val, 1));
17539
17540 if (eol_flag)
17541 {
17542 /* The EOL conversion that is normal on this system. */
17543
17544 if (NILP (eolvalue)) /* Not yet decided. */
17545 eoltype = eol_mnemonic_undecided;
17546 else if (VECTORP (eolvalue)) /* Not yet decided. */
17547 eoltype = eol_mnemonic_undecided;
17548 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17549 eoltype = (XFASTINT (eolvalue) == 0
17550 ? eol_mnemonic_unix
17551 : (XFASTINT (eolvalue) == 1
17552 ? eol_mnemonic_dos : eol_mnemonic_mac));
17553 }
17554 }
17555
17556 if (eol_flag)
17557 {
17558 /* Mention the EOL conversion if it is not the usual one. */
17559 if (STRINGP (eoltype))
17560 {
17561 eol_str = SDATA (eoltype);
17562 eol_str_len = SBYTES (eoltype);
17563 }
17564 else if (INTEGERP (eoltype)
17565 && CHAR_VALID_P (XINT (eoltype), 0))
17566 {
17567 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17568 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17569 eol_str = tmp;
17570 }
17571 else
17572 {
17573 eol_str = invalid_eol_type;
17574 eol_str_len = sizeof (invalid_eol_type) - 1;
17575 }
17576 bcopy (eol_str, buf, eol_str_len);
17577 buf += eol_str_len;
17578 }
17579
17580 return buf;
17581 }
17582
17583 /* Return a string for the output of a mode line %-spec for window W,
17584 generated by character C. PRECISION >= 0 means don't return a
17585 string longer than that value. FIELD_WIDTH > 0 means pad the
17586 string returned with spaces to that value. Return 1 in *MULTIBYTE
17587 if the result is multibyte text.
17588
17589 Note we operate on the current buffer for most purposes,
17590 the exception being w->base_line_pos. */
17591
17592 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17593
17594 static char *
17595 decode_mode_spec (w, c, field_width, precision, multibyte)
17596 struct window *w;
17597 register int c;
17598 int field_width, precision;
17599 int *multibyte;
17600 {
17601 Lisp_Object obj;
17602 struct frame *f = XFRAME (WINDOW_FRAME (w));
17603 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17604 struct buffer *b = current_buffer;
17605
17606 obj = Qnil;
17607 *multibyte = 0;
17608
17609 switch (c)
17610 {
17611 case '*':
17612 if (!NILP (b->read_only))
17613 return "%";
17614 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17615 return "*";
17616 return "-";
17617
17618 case '+':
17619 /* This differs from %* only for a modified read-only buffer. */
17620 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17621 return "*";
17622 if (!NILP (b->read_only))
17623 return "%";
17624 return "-";
17625
17626 case '&':
17627 /* This differs from %* in ignoring read-only-ness. */
17628 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17629 return "*";
17630 return "-";
17631
17632 case '%':
17633 return "%";
17634
17635 case '[':
17636 {
17637 int i;
17638 char *p;
17639
17640 if (command_loop_level > 5)
17641 return "[[[... ";
17642 p = decode_mode_spec_buf;
17643 for (i = 0; i < command_loop_level; i++)
17644 *p++ = '[';
17645 *p = 0;
17646 return decode_mode_spec_buf;
17647 }
17648
17649 case ']':
17650 {
17651 int i;
17652 char *p;
17653
17654 if (command_loop_level > 5)
17655 return " ...]]]";
17656 p = decode_mode_spec_buf;
17657 for (i = 0; i < command_loop_level; i++)
17658 *p++ = ']';
17659 *p = 0;
17660 return decode_mode_spec_buf;
17661 }
17662
17663 case '-':
17664 {
17665 register int i;
17666
17667 /* Let lots_of_dashes be a string of infinite length. */
17668 if (mode_line_target == MODE_LINE_NOPROP ||
17669 mode_line_target == MODE_LINE_STRING)
17670 return "--";
17671 if (field_width <= 0
17672 || field_width > sizeof (lots_of_dashes))
17673 {
17674 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17675 decode_mode_spec_buf[i] = '-';
17676 decode_mode_spec_buf[i] = '\0';
17677 return decode_mode_spec_buf;
17678 }
17679 else
17680 return lots_of_dashes;
17681 }
17682
17683 case 'b':
17684 obj = b->name;
17685 break;
17686
17687 case 'c':
17688 /* %c and %l are ignored in `frame-title-format'.
17689 (In redisplay_internal, the frame title is drawn _before_ the
17690 windows are updated, so the stuff which depends on actual
17691 window contents (such as %l) may fail to render properly, or
17692 even crash emacs.) */
17693 if (mode_line_target == MODE_LINE_TITLE)
17694 return "";
17695 else
17696 {
17697 int col = (int) current_column (); /* iftc */
17698 w->column_number_displayed = make_number (col);
17699 pint2str (decode_mode_spec_buf, field_width, col);
17700 return decode_mode_spec_buf;
17701 }
17702
17703 case 'e':
17704 #ifndef SYSTEM_MALLOC
17705 {
17706 if (NILP (Vmemory_full))
17707 return "";
17708 else
17709 return "!MEM FULL! ";
17710 }
17711 #else
17712 return "";
17713 #endif
17714
17715 case 'F':
17716 /* %F displays the frame name. */
17717 if (!NILP (f->title))
17718 return (char *) SDATA (f->title);
17719 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17720 return (char *) SDATA (f->name);
17721 return "Emacs";
17722
17723 case 'f':
17724 obj = b->filename;
17725 break;
17726
17727 case 'i':
17728 {
17729 int size = ZV - BEGV;
17730 pint2str (decode_mode_spec_buf, field_width, size);
17731 return decode_mode_spec_buf;
17732 }
17733
17734 case 'I':
17735 {
17736 int size = ZV - BEGV;
17737 pint2hrstr (decode_mode_spec_buf, field_width, size);
17738 return decode_mode_spec_buf;
17739 }
17740
17741 case 'l':
17742 {
17743 int startpos, startpos_byte, line, linepos, linepos_byte;
17744 int topline, nlines, junk, height;
17745
17746 /* %c and %l are ignored in `frame-title-format'. */
17747 if (mode_line_target == MODE_LINE_TITLE)
17748 return "";
17749
17750 startpos = XMARKER (w->start)->charpos;
17751 startpos_byte = marker_byte_position (w->start);
17752 height = WINDOW_TOTAL_LINES (w);
17753
17754 /* If we decided that this buffer isn't suitable for line numbers,
17755 don't forget that too fast. */
17756 if (EQ (w->base_line_pos, w->buffer))
17757 goto no_value;
17758 /* But do forget it, if the window shows a different buffer now. */
17759 else if (BUFFERP (w->base_line_pos))
17760 w->base_line_pos = Qnil;
17761
17762 /* If the buffer is very big, don't waste time. */
17763 if (INTEGERP (Vline_number_display_limit)
17764 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17765 {
17766 w->base_line_pos = Qnil;
17767 w->base_line_number = Qnil;
17768 goto no_value;
17769 }
17770
17771 if (!NILP (w->base_line_number)
17772 && !NILP (w->base_line_pos)
17773 && XFASTINT (w->base_line_pos) <= startpos)
17774 {
17775 line = XFASTINT (w->base_line_number);
17776 linepos = XFASTINT (w->base_line_pos);
17777 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17778 }
17779 else
17780 {
17781 line = 1;
17782 linepos = BUF_BEGV (b);
17783 linepos_byte = BUF_BEGV_BYTE (b);
17784 }
17785
17786 /* Count lines from base line to window start position. */
17787 nlines = display_count_lines (linepos, linepos_byte,
17788 startpos_byte,
17789 startpos, &junk);
17790
17791 topline = nlines + line;
17792
17793 /* Determine a new base line, if the old one is too close
17794 or too far away, or if we did not have one.
17795 "Too close" means it's plausible a scroll-down would
17796 go back past it. */
17797 if (startpos == BUF_BEGV (b))
17798 {
17799 w->base_line_number = make_number (topline);
17800 w->base_line_pos = make_number (BUF_BEGV (b));
17801 }
17802 else if (nlines < height + 25 || nlines > height * 3 + 50
17803 || linepos == BUF_BEGV (b))
17804 {
17805 int limit = BUF_BEGV (b);
17806 int limit_byte = BUF_BEGV_BYTE (b);
17807 int position;
17808 int distance = (height * 2 + 30) * line_number_display_limit_width;
17809
17810 if (startpos - distance > limit)
17811 {
17812 limit = startpos - distance;
17813 limit_byte = CHAR_TO_BYTE (limit);
17814 }
17815
17816 nlines = display_count_lines (startpos, startpos_byte,
17817 limit_byte,
17818 - (height * 2 + 30),
17819 &position);
17820 /* If we couldn't find the lines we wanted within
17821 line_number_display_limit_width chars per line,
17822 give up on line numbers for this window. */
17823 if (position == limit_byte && limit == startpos - distance)
17824 {
17825 w->base_line_pos = w->buffer;
17826 w->base_line_number = Qnil;
17827 goto no_value;
17828 }
17829
17830 w->base_line_number = make_number (topline - nlines);
17831 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17832 }
17833
17834 /* Now count lines from the start pos to point. */
17835 nlines = display_count_lines (startpos, startpos_byte,
17836 PT_BYTE, PT, &junk);
17837
17838 /* Record that we did display the line number. */
17839 line_number_displayed = 1;
17840
17841 /* Make the string to show. */
17842 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17843 return decode_mode_spec_buf;
17844 no_value:
17845 {
17846 char* p = decode_mode_spec_buf;
17847 int pad = field_width - 2;
17848 while (pad-- > 0)
17849 *p++ = ' ';
17850 *p++ = '?';
17851 *p++ = '?';
17852 *p = '\0';
17853 return decode_mode_spec_buf;
17854 }
17855 }
17856 break;
17857
17858 case 'm':
17859 obj = b->mode_name;
17860 break;
17861
17862 case 'n':
17863 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17864 return " Narrow";
17865 break;
17866
17867 case 'p':
17868 {
17869 int pos = marker_position (w->start);
17870 int total = BUF_ZV (b) - BUF_BEGV (b);
17871
17872 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17873 {
17874 if (pos <= BUF_BEGV (b))
17875 return "All";
17876 else
17877 return "Bottom";
17878 }
17879 else if (pos <= BUF_BEGV (b))
17880 return "Top";
17881 else
17882 {
17883 if (total > 1000000)
17884 /* Do it differently for a large value, to avoid overflow. */
17885 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17886 else
17887 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17888 /* We can't normally display a 3-digit number,
17889 so get us a 2-digit number that is close. */
17890 if (total == 100)
17891 total = 99;
17892 sprintf (decode_mode_spec_buf, "%2d%%", total);
17893 return decode_mode_spec_buf;
17894 }
17895 }
17896
17897 /* Display percentage of size above the bottom of the screen. */
17898 case 'P':
17899 {
17900 int toppos = marker_position (w->start);
17901 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17902 int total = BUF_ZV (b) - BUF_BEGV (b);
17903
17904 if (botpos >= BUF_ZV (b))
17905 {
17906 if (toppos <= BUF_BEGV (b))
17907 return "All";
17908 else
17909 return "Bottom";
17910 }
17911 else
17912 {
17913 if (total > 1000000)
17914 /* Do it differently for a large value, to avoid overflow. */
17915 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17916 else
17917 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17918 /* We can't normally display a 3-digit number,
17919 so get us a 2-digit number that is close. */
17920 if (total == 100)
17921 total = 99;
17922 if (toppos <= BUF_BEGV (b))
17923 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17924 else
17925 sprintf (decode_mode_spec_buf, "%2d%%", total);
17926 return decode_mode_spec_buf;
17927 }
17928 }
17929
17930 case 's':
17931 /* status of process */
17932 obj = Fget_buffer_process (Fcurrent_buffer ());
17933 if (NILP (obj))
17934 return "no process";
17935 #ifdef subprocesses
17936 obj = Fsymbol_name (Fprocess_status (obj));
17937 #endif
17938 break;
17939
17940 case 't': /* indicate TEXT or BINARY */
17941 #ifdef MODE_LINE_BINARY_TEXT
17942 return MODE_LINE_BINARY_TEXT (b);
17943 #else
17944 return "T";
17945 #endif
17946
17947 case 'z':
17948 /* coding-system (not including end-of-line format) */
17949 case 'Z':
17950 /* coding-system (including end-of-line type) */
17951 {
17952 int eol_flag = (c == 'Z');
17953 char *p = decode_mode_spec_buf;
17954
17955 if (! FRAME_WINDOW_P (f))
17956 {
17957 /* No need to mention EOL here--the terminal never needs
17958 to do EOL conversion. */
17959 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17960 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17961 }
17962 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17963 p, eol_flag);
17964
17965 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17966 #ifdef subprocesses
17967 obj = Fget_buffer_process (Fcurrent_buffer ());
17968 if (PROCESSP (obj))
17969 {
17970 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17971 p, eol_flag);
17972 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17973 p, eol_flag);
17974 }
17975 #endif /* subprocesses */
17976 #endif /* 0 */
17977 *p = 0;
17978 return decode_mode_spec_buf;
17979 }
17980 }
17981
17982 if (STRINGP (obj))
17983 {
17984 *multibyte = STRING_MULTIBYTE (obj);
17985 return (char *) SDATA (obj);
17986 }
17987 else
17988 return "";
17989 }
17990
17991
17992 /* Count up to COUNT lines starting from START / START_BYTE.
17993 But don't go beyond LIMIT_BYTE.
17994 Return the number of lines thus found (always nonnegative).
17995
17996 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17997
17998 static int
17999 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18000 int start, start_byte, limit_byte, count;
18001 int *byte_pos_ptr;
18002 {
18003 register unsigned char *cursor;
18004 unsigned char *base;
18005
18006 register int ceiling;
18007 register unsigned char *ceiling_addr;
18008 int orig_count = count;
18009
18010 /* If we are not in selective display mode,
18011 check only for newlines. */
18012 int selective_display = (!NILP (current_buffer->selective_display)
18013 && !INTEGERP (current_buffer->selective_display));
18014
18015 if (count > 0)
18016 {
18017 while (start_byte < limit_byte)
18018 {
18019 ceiling = BUFFER_CEILING_OF (start_byte);
18020 ceiling = min (limit_byte - 1, ceiling);
18021 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18022 base = (cursor = BYTE_POS_ADDR (start_byte));
18023 while (1)
18024 {
18025 if (selective_display)
18026 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18027 ;
18028 else
18029 while (*cursor != '\n' && ++cursor != ceiling_addr)
18030 ;
18031
18032 if (cursor != ceiling_addr)
18033 {
18034 if (--count == 0)
18035 {
18036 start_byte += cursor - base + 1;
18037 *byte_pos_ptr = start_byte;
18038 return orig_count;
18039 }
18040 else
18041 if (++cursor == ceiling_addr)
18042 break;
18043 }
18044 else
18045 break;
18046 }
18047 start_byte += cursor - base;
18048 }
18049 }
18050 else
18051 {
18052 while (start_byte > limit_byte)
18053 {
18054 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18055 ceiling = max (limit_byte, ceiling);
18056 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18057 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18058 while (1)
18059 {
18060 if (selective_display)
18061 while (--cursor != ceiling_addr
18062 && *cursor != '\n' && *cursor != 015)
18063 ;
18064 else
18065 while (--cursor != ceiling_addr && *cursor != '\n')
18066 ;
18067
18068 if (cursor != ceiling_addr)
18069 {
18070 if (++count == 0)
18071 {
18072 start_byte += cursor - base + 1;
18073 *byte_pos_ptr = start_byte;
18074 /* When scanning backwards, we should
18075 not count the newline posterior to which we stop. */
18076 return - orig_count - 1;
18077 }
18078 }
18079 else
18080 break;
18081 }
18082 /* Here we add 1 to compensate for the last decrement
18083 of CURSOR, which took it past the valid range. */
18084 start_byte += cursor - base + 1;
18085 }
18086 }
18087
18088 *byte_pos_ptr = limit_byte;
18089
18090 if (count < 0)
18091 return - orig_count + count;
18092 return orig_count - count;
18093
18094 }
18095
18096
18097 \f
18098 /***********************************************************************
18099 Displaying strings
18100 ***********************************************************************/
18101
18102 /* Display a NUL-terminated string, starting with index START.
18103
18104 If STRING is non-null, display that C string. Otherwise, the Lisp
18105 string LISP_STRING is displayed.
18106
18107 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18108 FACE_STRING. Display STRING or LISP_STRING with the face at
18109 FACE_STRING_POS in FACE_STRING:
18110
18111 Display the string in the environment given by IT, but use the
18112 standard display table, temporarily.
18113
18114 FIELD_WIDTH is the minimum number of output glyphs to produce.
18115 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18116 with spaces. If STRING has more characters, more than FIELD_WIDTH
18117 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18118
18119 PRECISION is the maximum number of characters to output from
18120 STRING. PRECISION < 0 means don't truncate the string.
18121
18122 This is roughly equivalent to printf format specifiers:
18123
18124 FIELD_WIDTH PRECISION PRINTF
18125 ----------------------------------------
18126 -1 -1 %s
18127 -1 10 %.10s
18128 10 -1 %10s
18129 20 10 %20.10s
18130
18131 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18132 display them, and < 0 means obey the current buffer's value of
18133 enable_multibyte_characters.
18134
18135 Value is the number of columns displayed. */
18136
18137 static int
18138 display_string (string, lisp_string, face_string, face_string_pos,
18139 start, it, field_width, precision, max_x, multibyte)
18140 unsigned char *string;
18141 Lisp_Object lisp_string;
18142 Lisp_Object face_string;
18143 int face_string_pos;
18144 int start;
18145 struct it *it;
18146 int field_width, precision, max_x;
18147 int multibyte;
18148 {
18149 int hpos_at_start = it->hpos;
18150 int saved_face_id = it->face_id;
18151 struct glyph_row *row = it->glyph_row;
18152
18153 /* Initialize the iterator IT for iteration over STRING beginning
18154 with index START. */
18155 reseat_to_string (it, string, lisp_string, start,
18156 precision, field_width, multibyte);
18157
18158 /* If displaying STRING, set up the face of the iterator
18159 from LISP_STRING, if that's given. */
18160 if (STRINGP (face_string))
18161 {
18162 int endptr;
18163 struct face *face;
18164
18165 it->face_id
18166 = face_at_string_position (it->w, face_string, face_string_pos,
18167 0, it->region_beg_charpos,
18168 it->region_end_charpos,
18169 &endptr, it->base_face_id, 0);
18170 face = FACE_FROM_ID (it->f, it->face_id);
18171 it->face_box_p = face->box != FACE_NO_BOX;
18172 }
18173
18174 /* Set max_x to the maximum allowed X position. Don't let it go
18175 beyond the right edge of the window. */
18176 if (max_x <= 0)
18177 max_x = it->last_visible_x;
18178 else
18179 max_x = min (max_x, it->last_visible_x);
18180
18181 /* Skip over display elements that are not visible. because IT->w is
18182 hscrolled. */
18183 if (it->current_x < it->first_visible_x)
18184 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18185 MOVE_TO_POS | MOVE_TO_X);
18186
18187 row->ascent = it->max_ascent;
18188 row->height = it->max_ascent + it->max_descent;
18189 row->phys_ascent = it->max_phys_ascent;
18190 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18191 row->extra_line_spacing = it->max_extra_line_spacing;
18192
18193 /* This condition is for the case that we are called with current_x
18194 past last_visible_x. */
18195 while (it->current_x < max_x)
18196 {
18197 int x_before, x, n_glyphs_before, i, nglyphs;
18198
18199 /* Get the next display element. */
18200 if (!get_next_display_element (it))
18201 break;
18202
18203 /* Produce glyphs. */
18204 x_before = it->current_x;
18205 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18206 PRODUCE_GLYPHS (it);
18207
18208 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18209 i = 0;
18210 x = x_before;
18211 while (i < nglyphs)
18212 {
18213 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18214
18215 if (!it->truncate_lines_p
18216 && x + glyph->pixel_width > max_x)
18217 {
18218 /* End of continued line or max_x reached. */
18219 if (CHAR_GLYPH_PADDING_P (*glyph))
18220 {
18221 /* A wide character is unbreakable. */
18222 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18223 it->current_x = x_before;
18224 }
18225 else
18226 {
18227 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18228 it->current_x = x;
18229 }
18230 break;
18231 }
18232 else if (x + glyph->pixel_width > it->first_visible_x)
18233 {
18234 /* Glyph is at least partially visible. */
18235 ++it->hpos;
18236 if (x < it->first_visible_x)
18237 it->glyph_row->x = x - it->first_visible_x;
18238 }
18239 else
18240 {
18241 /* Glyph is off the left margin of the display area.
18242 Should not happen. */
18243 abort ();
18244 }
18245
18246 row->ascent = max (row->ascent, it->max_ascent);
18247 row->height = max (row->height, it->max_ascent + it->max_descent);
18248 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18249 row->phys_height = max (row->phys_height,
18250 it->max_phys_ascent + it->max_phys_descent);
18251 row->extra_line_spacing = max (row->extra_line_spacing,
18252 it->max_extra_line_spacing);
18253 x += glyph->pixel_width;
18254 ++i;
18255 }
18256
18257 /* Stop if max_x reached. */
18258 if (i < nglyphs)
18259 break;
18260
18261 /* Stop at line ends. */
18262 if (ITERATOR_AT_END_OF_LINE_P (it))
18263 {
18264 it->continuation_lines_width = 0;
18265 break;
18266 }
18267
18268 set_iterator_to_next (it, 1);
18269
18270 /* Stop if truncating at the right edge. */
18271 if (it->truncate_lines_p
18272 && it->current_x >= it->last_visible_x)
18273 {
18274 /* Add truncation mark, but don't do it if the line is
18275 truncated at a padding space. */
18276 if (IT_CHARPOS (*it) < it->string_nchars)
18277 {
18278 if (!FRAME_WINDOW_P (it->f))
18279 {
18280 int i, n;
18281
18282 if (it->current_x > it->last_visible_x)
18283 {
18284 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18285 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18286 break;
18287 for (n = row->used[TEXT_AREA]; i < n; ++i)
18288 {
18289 row->used[TEXT_AREA] = i;
18290 produce_special_glyphs (it, IT_TRUNCATION);
18291 }
18292 }
18293 produce_special_glyphs (it, IT_TRUNCATION);
18294 }
18295 it->glyph_row->truncated_on_right_p = 1;
18296 }
18297 break;
18298 }
18299 }
18300
18301 /* Maybe insert a truncation at the left. */
18302 if (it->first_visible_x
18303 && IT_CHARPOS (*it) > 0)
18304 {
18305 if (!FRAME_WINDOW_P (it->f))
18306 insert_left_trunc_glyphs (it);
18307 it->glyph_row->truncated_on_left_p = 1;
18308 }
18309
18310 it->face_id = saved_face_id;
18311
18312 /* Value is number of columns displayed. */
18313 return it->hpos - hpos_at_start;
18314 }
18315
18316
18317 \f
18318 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18319 appears as an element of LIST or as the car of an element of LIST.
18320 If PROPVAL is a list, compare each element against LIST in that
18321 way, and return 1/2 if any element of PROPVAL is found in LIST.
18322 Otherwise return 0. This function cannot quit.
18323 The return value is 2 if the text is invisible but with an ellipsis
18324 and 1 if it's invisible and without an ellipsis. */
18325
18326 int
18327 invisible_p (propval, list)
18328 register Lisp_Object propval;
18329 Lisp_Object list;
18330 {
18331 register Lisp_Object tail, proptail;
18332
18333 for (tail = list; CONSP (tail); tail = XCDR (tail))
18334 {
18335 register Lisp_Object tem;
18336 tem = XCAR (tail);
18337 if (EQ (propval, tem))
18338 return 1;
18339 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18340 return NILP (XCDR (tem)) ? 1 : 2;
18341 }
18342
18343 if (CONSP (propval))
18344 {
18345 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18346 {
18347 Lisp_Object propelt;
18348 propelt = XCAR (proptail);
18349 for (tail = list; CONSP (tail); tail = XCDR (tail))
18350 {
18351 register Lisp_Object tem;
18352 tem = XCAR (tail);
18353 if (EQ (propelt, tem))
18354 return 1;
18355 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18356 return NILP (XCDR (tem)) ? 1 : 2;
18357 }
18358 }
18359 }
18360
18361 return 0;
18362 }
18363
18364 /* Calculate a width or height in pixels from a specification using
18365 the following elements:
18366
18367 SPEC ::=
18368 NUM - a (fractional) multiple of the default font width/height
18369 (NUM) - specifies exactly NUM pixels
18370 UNIT - a fixed number of pixels, see below.
18371 ELEMENT - size of a display element in pixels, see below.
18372 (NUM . SPEC) - equals NUM * SPEC
18373 (+ SPEC SPEC ...) - add pixel values
18374 (- SPEC SPEC ...) - subtract pixel values
18375 (- SPEC) - negate pixel value
18376
18377 NUM ::=
18378 INT or FLOAT - a number constant
18379 SYMBOL - use symbol's (buffer local) variable binding.
18380
18381 UNIT ::=
18382 in - pixels per inch *)
18383 mm - pixels per 1/1000 meter *)
18384 cm - pixels per 1/100 meter *)
18385 width - width of current font in pixels.
18386 height - height of current font in pixels.
18387
18388 *) using the ratio(s) defined in display-pixels-per-inch.
18389
18390 ELEMENT ::=
18391
18392 left-fringe - left fringe width in pixels
18393 right-fringe - right fringe width in pixels
18394
18395 left-margin - left margin width in pixels
18396 right-margin - right margin width in pixels
18397
18398 scroll-bar - scroll-bar area width in pixels
18399
18400 Examples:
18401
18402 Pixels corresponding to 5 inches:
18403 (5 . in)
18404
18405 Total width of non-text areas on left side of window (if scroll-bar is on left):
18406 '(space :width (+ left-fringe left-margin scroll-bar))
18407
18408 Align to first text column (in header line):
18409 '(space :align-to 0)
18410
18411 Align to middle of text area minus half the width of variable `my-image'
18412 containing a loaded image:
18413 '(space :align-to (0.5 . (- text my-image)))
18414
18415 Width of left margin minus width of 1 character in the default font:
18416 '(space :width (- left-margin 1))
18417
18418 Width of left margin minus width of 2 characters in the current font:
18419 '(space :width (- left-margin (2 . width)))
18420
18421 Center 1 character over left-margin (in header line):
18422 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18423
18424 Different ways to express width of left fringe plus left margin minus one pixel:
18425 '(space :width (- (+ left-fringe left-margin) (1)))
18426 '(space :width (+ left-fringe left-margin (- (1))))
18427 '(space :width (+ left-fringe left-margin (-1)))
18428
18429 */
18430
18431 #define NUMVAL(X) \
18432 ((INTEGERP (X) || FLOATP (X)) \
18433 ? XFLOATINT (X) \
18434 : - 1)
18435
18436 int
18437 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18438 double *res;
18439 struct it *it;
18440 Lisp_Object prop;
18441 void *font;
18442 int width_p, *align_to;
18443 {
18444 double pixels;
18445
18446 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18447 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18448
18449 if (NILP (prop))
18450 return OK_PIXELS (0);
18451
18452 if (SYMBOLP (prop))
18453 {
18454 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18455 {
18456 char *unit = SDATA (SYMBOL_NAME (prop));
18457
18458 if (unit[0] == 'i' && unit[1] == 'n')
18459 pixels = 1.0;
18460 else if (unit[0] == 'm' && unit[1] == 'm')
18461 pixels = 25.4;
18462 else if (unit[0] == 'c' && unit[1] == 'm')
18463 pixels = 2.54;
18464 else
18465 pixels = 0;
18466 if (pixels > 0)
18467 {
18468 double ppi;
18469 #ifdef HAVE_WINDOW_SYSTEM
18470 if (FRAME_WINDOW_P (it->f)
18471 && (ppi = (width_p
18472 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18473 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18474 ppi > 0))
18475 return OK_PIXELS (ppi / pixels);
18476 #endif
18477
18478 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18479 || (CONSP (Vdisplay_pixels_per_inch)
18480 && (ppi = (width_p
18481 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18482 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18483 ppi > 0)))
18484 return OK_PIXELS (ppi / pixels);
18485
18486 return 0;
18487 }
18488 }
18489
18490 #ifdef HAVE_WINDOW_SYSTEM
18491 if (EQ (prop, Qheight))
18492 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18493 if (EQ (prop, Qwidth))
18494 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18495 #else
18496 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18497 return OK_PIXELS (1);
18498 #endif
18499
18500 if (EQ (prop, Qtext))
18501 return OK_PIXELS (width_p
18502 ? window_box_width (it->w, TEXT_AREA)
18503 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18504
18505 if (align_to && *align_to < 0)
18506 {
18507 *res = 0;
18508 if (EQ (prop, Qleft))
18509 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18510 if (EQ (prop, Qright))
18511 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18512 if (EQ (prop, Qcenter))
18513 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18514 + window_box_width (it->w, TEXT_AREA) / 2);
18515 if (EQ (prop, Qleft_fringe))
18516 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18517 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18518 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18519 if (EQ (prop, Qright_fringe))
18520 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18521 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18522 : window_box_right_offset (it->w, TEXT_AREA));
18523 if (EQ (prop, Qleft_margin))
18524 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18525 if (EQ (prop, Qright_margin))
18526 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18527 if (EQ (prop, Qscroll_bar))
18528 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18529 ? 0
18530 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18531 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18532 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18533 : 0)));
18534 }
18535 else
18536 {
18537 if (EQ (prop, Qleft_fringe))
18538 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18539 if (EQ (prop, Qright_fringe))
18540 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18541 if (EQ (prop, Qleft_margin))
18542 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18543 if (EQ (prop, Qright_margin))
18544 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18545 if (EQ (prop, Qscroll_bar))
18546 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18547 }
18548
18549 prop = Fbuffer_local_value (prop, it->w->buffer);
18550 }
18551
18552 if (INTEGERP (prop) || FLOATP (prop))
18553 {
18554 int base_unit = (width_p
18555 ? FRAME_COLUMN_WIDTH (it->f)
18556 : FRAME_LINE_HEIGHT (it->f));
18557 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18558 }
18559
18560 if (CONSP (prop))
18561 {
18562 Lisp_Object car = XCAR (prop);
18563 Lisp_Object cdr = XCDR (prop);
18564
18565 if (SYMBOLP (car))
18566 {
18567 #ifdef HAVE_WINDOW_SYSTEM
18568 if (valid_image_p (prop))
18569 {
18570 int id = lookup_image (it->f, prop);
18571 struct image *img = IMAGE_FROM_ID (it->f, id);
18572
18573 return OK_PIXELS (width_p ? img->width : img->height);
18574 }
18575 #endif
18576 if (EQ (car, Qplus) || EQ (car, Qminus))
18577 {
18578 int first = 1;
18579 double px;
18580
18581 pixels = 0;
18582 while (CONSP (cdr))
18583 {
18584 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18585 font, width_p, align_to))
18586 return 0;
18587 if (first)
18588 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18589 else
18590 pixels += px;
18591 cdr = XCDR (cdr);
18592 }
18593 if (EQ (car, Qminus))
18594 pixels = -pixels;
18595 return OK_PIXELS (pixels);
18596 }
18597
18598 car = Fbuffer_local_value (car, it->w->buffer);
18599 }
18600
18601 if (INTEGERP (car) || FLOATP (car))
18602 {
18603 double fact;
18604 pixels = XFLOATINT (car);
18605 if (NILP (cdr))
18606 return OK_PIXELS (pixels);
18607 if (calc_pixel_width_or_height (&fact, it, cdr,
18608 font, width_p, align_to))
18609 return OK_PIXELS (pixels * fact);
18610 return 0;
18611 }
18612
18613 return 0;
18614 }
18615
18616 return 0;
18617 }
18618
18619 \f
18620 /***********************************************************************
18621 Glyph Display
18622 ***********************************************************************/
18623
18624 #ifdef HAVE_WINDOW_SYSTEM
18625
18626 #if GLYPH_DEBUG
18627
18628 void
18629 dump_glyph_string (s)
18630 struct glyph_string *s;
18631 {
18632 fprintf (stderr, "glyph string\n");
18633 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18634 s->x, s->y, s->width, s->height);
18635 fprintf (stderr, " ybase = %d\n", s->ybase);
18636 fprintf (stderr, " hl = %d\n", s->hl);
18637 fprintf (stderr, " left overhang = %d, right = %d\n",
18638 s->left_overhang, s->right_overhang);
18639 fprintf (stderr, " nchars = %d\n", s->nchars);
18640 fprintf (stderr, " extends to end of line = %d\n",
18641 s->extends_to_end_of_line_p);
18642 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18643 fprintf (stderr, " bg width = %d\n", s->background_width);
18644 }
18645
18646 #endif /* GLYPH_DEBUG */
18647
18648 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18649 of XChar2b structures for S; it can't be allocated in
18650 init_glyph_string because it must be allocated via `alloca'. W
18651 is the window on which S is drawn. ROW and AREA are the glyph row
18652 and area within the row from which S is constructed. START is the
18653 index of the first glyph structure covered by S. HL is a
18654 face-override for drawing S. */
18655
18656 #ifdef HAVE_NTGUI
18657 #define OPTIONAL_HDC(hdc) hdc,
18658 #define DECLARE_HDC(hdc) HDC hdc;
18659 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18660 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18661 #endif
18662
18663 #ifndef OPTIONAL_HDC
18664 #define OPTIONAL_HDC(hdc)
18665 #define DECLARE_HDC(hdc)
18666 #define ALLOCATE_HDC(hdc, f)
18667 #define RELEASE_HDC(hdc, f)
18668 #endif
18669
18670 static void
18671 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18672 struct glyph_string *s;
18673 DECLARE_HDC (hdc)
18674 XChar2b *char2b;
18675 struct window *w;
18676 struct glyph_row *row;
18677 enum glyph_row_area area;
18678 int start;
18679 enum draw_glyphs_face hl;
18680 {
18681 bzero (s, sizeof *s);
18682 s->w = w;
18683 s->f = XFRAME (w->frame);
18684 #ifdef HAVE_NTGUI
18685 s->hdc = hdc;
18686 #endif
18687 s->display = FRAME_X_DISPLAY (s->f);
18688 s->window = FRAME_X_WINDOW (s->f);
18689 s->char2b = char2b;
18690 s->hl = hl;
18691 s->row = row;
18692 s->area = area;
18693 s->first_glyph = row->glyphs[area] + start;
18694 s->height = row->height;
18695 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18696
18697 /* Display the internal border below the tool-bar window. */
18698 if (WINDOWP (s->f->tool_bar_window)
18699 && s->w == XWINDOW (s->f->tool_bar_window))
18700 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18701
18702 s->ybase = s->y + row->ascent;
18703 }
18704
18705
18706 /* Append the list of glyph strings with head H and tail T to the list
18707 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18708
18709 static INLINE void
18710 append_glyph_string_lists (head, tail, h, t)
18711 struct glyph_string **head, **tail;
18712 struct glyph_string *h, *t;
18713 {
18714 if (h)
18715 {
18716 if (*head)
18717 (*tail)->next = h;
18718 else
18719 *head = h;
18720 h->prev = *tail;
18721 *tail = t;
18722 }
18723 }
18724
18725
18726 /* Prepend the list of glyph strings with head H and tail T to the
18727 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18728 result. */
18729
18730 static INLINE void
18731 prepend_glyph_string_lists (head, tail, h, t)
18732 struct glyph_string **head, **tail;
18733 struct glyph_string *h, *t;
18734 {
18735 if (h)
18736 {
18737 if (*head)
18738 (*head)->prev = t;
18739 else
18740 *tail = t;
18741 t->next = *head;
18742 *head = h;
18743 }
18744 }
18745
18746
18747 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18748 Set *HEAD and *TAIL to the resulting list. */
18749
18750 static INLINE void
18751 append_glyph_string (head, tail, s)
18752 struct glyph_string **head, **tail;
18753 struct glyph_string *s;
18754 {
18755 s->next = s->prev = NULL;
18756 append_glyph_string_lists (head, tail, s, s);
18757 }
18758
18759
18760 /* Get face and two-byte form of character glyph GLYPH on frame F.
18761 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18762 a pointer to a realized face that is ready for display. */
18763
18764 static INLINE struct face *
18765 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18766 struct frame *f;
18767 struct glyph *glyph;
18768 XChar2b *char2b;
18769 int *two_byte_p;
18770 {
18771 struct face *face;
18772
18773 xassert (glyph->type == CHAR_GLYPH);
18774 face = FACE_FROM_ID (f, glyph->face_id);
18775
18776 if (two_byte_p)
18777 *two_byte_p = 0;
18778
18779 if (!glyph->multibyte_p)
18780 {
18781 /* Unibyte case. We don't have to encode, but we have to make
18782 sure to use a face suitable for unibyte. */
18783 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18784 }
18785 else if (glyph->u.ch < 128)
18786 {
18787 /* Case of ASCII in a face known to fit ASCII. */
18788 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18789 }
18790 else
18791 {
18792 int c1, c2, charset;
18793
18794 /* Split characters into bytes. If c2 is -1 afterwards, C is
18795 really a one-byte character so that byte1 is zero. */
18796 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18797 if (c2 > 0)
18798 STORE_XCHAR2B (char2b, c1, c2);
18799 else
18800 STORE_XCHAR2B (char2b, 0, c1);
18801
18802 /* Maybe encode the character in *CHAR2B. */
18803 if (charset != CHARSET_ASCII)
18804 {
18805 struct font_info *font_info
18806 = FONT_INFO_FROM_ID (f, face->font_info_id);
18807 if (font_info)
18808 glyph->font_type
18809 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18810 }
18811 }
18812
18813 /* Make sure X resources of the face are allocated. */
18814 xassert (face != NULL);
18815 PREPARE_FACE_FOR_DISPLAY (f, face);
18816 return face;
18817 }
18818
18819
18820 /* Fill glyph string S with composition components specified by S->cmp.
18821
18822 FACES is an array of faces for all components of this composition.
18823 S->gidx is the index of the first component for S.
18824
18825 OVERLAPS non-zero means S should draw the foreground only, and use
18826 its physical height for clipping. See also draw_glyphs.
18827
18828 Value is the index of a component not in S. */
18829
18830 static int
18831 fill_composite_glyph_string (s, faces, overlaps)
18832 struct glyph_string *s;
18833 struct face **faces;
18834 int overlaps;
18835 {
18836 int i;
18837
18838 xassert (s);
18839
18840 s->for_overlaps = overlaps;
18841
18842 s->face = faces[s->gidx];
18843 s->font = s->face->font;
18844 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18845
18846 /* For all glyphs of this composition, starting at the offset
18847 S->gidx, until we reach the end of the definition or encounter a
18848 glyph that requires the different face, add it to S. */
18849 ++s->nchars;
18850 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18851 ++s->nchars;
18852
18853 /* All glyph strings for the same composition has the same width,
18854 i.e. the width set for the first component of the composition. */
18855
18856 s->width = s->first_glyph->pixel_width;
18857
18858 /* If the specified font could not be loaded, use the frame's
18859 default font, but record the fact that we couldn't load it in
18860 the glyph string so that we can draw rectangles for the
18861 characters of the glyph string. */
18862 if (s->font == NULL)
18863 {
18864 s->font_not_found_p = 1;
18865 s->font = FRAME_FONT (s->f);
18866 }
18867
18868 /* Adjust base line for subscript/superscript text. */
18869 s->ybase += s->first_glyph->voffset;
18870
18871 xassert (s->face && s->face->gc);
18872
18873 /* This glyph string must always be drawn with 16-bit functions. */
18874 s->two_byte_p = 1;
18875
18876 return s->gidx + s->nchars;
18877 }
18878
18879
18880 /* Fill glyph string S from a sequence of character glyphs.
18881
18882 FACE_ID is the face id of the string. START is the index of the
18883 first glyph to consider, END is the index of the last + 1.
18884 OVERLAPS non-zero means S should draw the foreground only, and use
18885 its physical height for clipping. See also draw_glyphs.
18886
18887 Value is the index of the first glyph not in S. */
18888
18889 static int
18890 fill_glyph_string (s, face_id, start, end, overlaps)
18891 struct glyph_string *s;
18892 int face_id;
18893 int start, end, overlaps;
18894 {
18895 struct glyph *glyph, *last;
18896 int voffset;
18897 int glyph_not_available_p;
18898
18899 xassert (s->f == XFRAME (s->w->frame));
18900 xassert (s->nchars == 0);
18901 xassert (start >= 0 && end > start);
18902
18903 s->for_overlaps = overlaps,
18904 glyph = s->row->glyphs[s->area] + start;
18905 last = s->row->glyphs[s->area] + end;
18906 voffset = glyph->voffset;
18907
18908 glyph_not_available_p = glyph->glyph_not_available_p;
18909
18910 while (glyph < last
18911 && glyph->type == CHAR_GLYPH
18912 && glyph->voffset == voffset
18913 /* Same face id implies same font, nowadays. */
18914 && glyph->face_id == face_id
18915 && glyph->glyph_not_available_p == glyph_not_available_p)
18916 {
18917 int two_byte_p;
18918
18919 s->face = get_glyph_face_and_encoding (s->f, glyph,
18920 s->char2b + s->nchars,
18921 &two_byte_p);
18922 s->two_byte_p = two_byte_p;
18923 ++s->nchars;
18924 xassert (s->nchars <= end - start);
18925 s->width += glyph->pixel_width;
18926 ++glyph;
18927 }
18928
18929 s->font = s->face->font;
18930 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18931
18932 /* If the specified font could not be loaded, use the frame's font,
18933 but record the fact that we couldn't load it in
18934 S->font_not_found_p so that we can draw rectangles for the
18935 characters of the glyph string. */
18936 if (s->font == NULL || glyph_not_available_p)
18937 {
18938 s->font_not_found_p = 1;
18939 s->font = FRAME_FONT (s->f);
18940 }
18941
18942 /* Adjust base line for subscript/superscript text. */
18943 s->ybase += voffset;
18944
18945 xassert (s->face && s->face->gc);
18946 return glyph - s->row->glyphs[s->area];
18947 }
18948
18949
18950 /* Fill glyph string S from image glyph S->first_glyph. */
18951
18952 static void
18953 fill_image_glyph_string (s)
18954 struct glyph_string *s;
18955 {
18956 xassert (s->first_glyph->type == IMAGE_GLYPH);
18957 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18958 xassert (s->img);
18959 s->slice = s->first_glyph->slice;
18960 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18961 s->font = s->face->font;
18962 s->width = s->first_glyph->pixel_width;
18963
18964 /* Adjust base line for subscript/superscript text. */
18965 s->ybase += s->first_glyph->voffset;
18966 }
18967
18968
18969 /* Fill glyph string S from a sequence of stretch glyphs.
18970
18971 ROW is the glyph row in which the glyphs are found, AREA is the
18972 area within the row. START is the index of the first glyph to
18973 consider, END is the index of the last + 1.
18974
18975 Value is the index of the first glyph not in S. */
18976
18977 static int
18978 fill_stretch_glyph_string (s, row, area, start, end)
18979 struct glyph_string *s;
18980 struct glyph_row *row;
18981 enum glyph_row_area area;
18982 int start, end;
18983 {
18984 struct glyph *glyph, *last;
18985 int voffset, face_id;
18986
18987 xassert (s->first_glyph->type == STRETCH_GLYPH);
18988
18989 glyph = s->row->glyphs[s->area] + start;
18990 last = s->row->glyphs[s->area] + end;
18991 face_id = glyph->face_id;
18992 s->face = FACE_FROM_ID (s->f, face_id);
18993 s->font = s->face->font;
18994 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18995 s->width = glyph->pixel_width;
18996 s->nchars = 1;
18997 voffset = glyph->voffset;
18998
18999 for (++glyph;
19000 (glyph < last
19001 && glyph->type == STRETCH_GLYPH
19002 && glyph->voffset == voffset
19003 && glyph->face_id == face_id);
19004 ++glyph)
19005 s->width += glyph->pixel_width;
19006
19007 /* Adjust base line for subscript/superscript text. */
19008 s->ybase += voffset;
19009
19010 /* The case that face->gc == 0 is handled when drawing the glyph
19011 string by calling PREPARE_FACE_FOR_DISPLAY. */
19012 xassert (s->face);
19013 return glyph - s->row->glyphs[s->area];
19014 }
19015
19016
19017 /* EXPORT for RIF:
19018 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19019 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19020 assumed to be zero. */
19021
19022 void
19023 x_get_glyph_overhangs (glyph, f, left, right)
19024 struct glyph *glyph;
19025 struct frame *f;
19026 int *left, *right;
19027 {
19028 *left = *right = 0;
19029
19030 if (glyph->type == CHAR_GLYPH)
19031 {
19032 XFontStruct *font;
19033 struct face *face;
19034 struct font_info *font_info;
19035 XChar2b char2b;
19036 XCharStruct *pcm;
19037
19038 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19039 font = face->font;
19040 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19041 if (font /* ++KFS: Should this be font_info ? */
19042 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19043 {
19044 if (pcm->rbearing > pcm->width)
19045 *right = pcm->rbearing - pcm->width;
19046 if (pcm->lbearing < 0)
19047 *left = -pcm->lbearing;
19048 }
19049 }
19050 }
19051
19052
19053 /* Return the index of the first glyph preceding glyph string S that
19054 is overwritten by S because of S's left overhang. Value is -1
19055 if no glyphs are overwritten. */
19056
19057 static int
19058 left_overwritten (s)
19059 struct glyph_string *s;
19060 {
19061 int k;
19062
19063 if (s->left_overhang)
19064 {
19065 int x = 0, i;
19066 struct glyph *glyphs = s->row->glyphs[s->area];
19067 int first = s->first_glyph - glyphs;
19068
19069 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19070 x -= glyphs[i].pixel_width;
19071
19072 k = i + 1;
19073 }
19074 else
19075 k = -1;
19076
19077 return k;
19078 }
19079
19080
19081 /* Return the index of the first glyph preceding glyph string S that
19082 is overwriting S because of its right overhang. Value is -1 if no
19083 glyph in front of S overwrites S. */
19084
19085 static int
19086 left_overwriting (s)
19087 struct glyph_string *s;
19088 {
19089 int i, k, x;
19090 struct glyph *glyphs = s->row->glyphs[s->area];
19091 int first = s->first_glyph - glyphs;
19092
19093 k = -1;
19094 x = 0;
19095 for (i = first - 1; i >= 0; --i)
19096 {
19097 int left, right;
19098 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19099 if (x + right > 0)
19100 k = i;
19101 x -= glyphs[i].pixel_width;
19102 }
19103
19104 return k;
19105 }
19106
19107
19108 /* Return the index of the last glyph following glyph string S that is
19109 not overwritten by S because of S's right overhang. Value is -1 if
19110 no such glyph is found. */
19111
19112 static int
19113 right_overwritten (s)
19114 struct glyph_string *s;
19115 {
19116 int k = -1;
19117
19118 if (s->right_overhang)
19119 {
19120 int x = 0, i;
19121 struct glyph *glyphs = s->row->glyphs[s->area];
19122 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19123 int end = s->row->used[s->area];
19124
19125 for (i = first; i < end && s->right_overhang > x; ++i)
19126 x += glyphs[i].pixel_width;
19127
19128 k = i;
19129 }
19130
19131 return k;
19132 }
19133
19134
19135 /* Return the index of the last glyph following glyph string S that
19136 overwrites S because of its left overhang. Value is negative
19137 if no such glyph is found. */
19138
19139 static int
19140 right_overwriting (s)
19141 struct glyph_string *s;
19142 {
19143 int i, k, x;
19144 int end = s->row->used[s->area];
19145 struct glyph *glyphs = s->row->glyphs[s->area];
19146 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19147
19148 k = -1;
19149 x = 0;
19150 for (i = first; i < end; ++i)
19151 {
19152 int left, right;
19153 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19154 if (x - left < 0)
19155 k = i;
19156 x += glyphs[i].pixel_width;
19157 }
19158
19159 return k;
19160 }
19161
19162
19163 /* Get face and two-byte form of character C in face FACE_ID on frame
19164 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19165 means we want to display multibyte text. DISPLAY_P non-zero means
19166 make sure that X resources for the face returned are allocated.
19167 Value is a pointer to a realized face that is ready for display if
19168 DISPLAY_P is non-zero. */
19169
19170 static INLINE struct face *
19171 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19172 struct frame *f;
19173 int c, face_id;
19174 XChar2b *char2b;
19175 int multibyte_p, display_p;
19176 {
19177 struct face *face = FACE_FROM_ID (f, face_id);
19178
19179 if (!multibyte_p)
19180 {
19181 /* Unibyte case. We don't have to encode, but we have to make
19182 sure to use a face suitable for unibyte. */
19183 STORE_XCHAR2B (char2b, 0, c);
19184 face_id = FACE_FOR_CHAR (f, face, c);
19185 face = FACE_FROM_ID (f, face_id);
19186 }
19187 else if (c < 128)
19188 {
19189 /* Case of ASCII in a face known to fit ASCII. */
19190 STORE_XCHAR2B (char2b, 0, c);
19191 }
19192 else
19193 {
19194 int c1, c2, charset;
19195
19196 /* Split characters into bytes. If c2 is -1 afterwards, C is
19197 really a one-byte character so that byte1 is zero. */
19198 SPLIT_CHAR (c, charset, c1, c2);
19199 if (c2 > 0)
19200 STORE_XCHAR2B (char2b, c1, c2);
19201 else
19202 STORE_XCHAR2B (char2b, 0, c1);
19203
19204 /* Maybe encode the character in *CHAR2B. */
19205 if (face->font != NULL)
19206 {
19207 struct font_info *font_info
19208 = FONT_INFO_FROM_ID (f, face->font_info_id);
19209 if (font_info)
19210 rif->encode_char (c, char2b, font_info, 0);
19211 }
19212 }
19213
19214 /* Make sure X resources of the face are allocated. */
19215 #ifdef HAVE_X_WINDOWS
19216 if (display_p)
19217 #endif
19218 {
19219 xassert (face != NULL);
19220 PREPARE_FACE_FOR_DISPLAY (f, face);
19221 }
19222
19223 return face;
19224 }
19225
19226
19227 /* Set background width of glyph string S. START is the index of the
19228 first glyph following S. LAST_X is the right-most x-position + 1
19229 in the drawing area. */
19230
19231 static INLINE void
19232 set_glyph_string_background_width (s, start, last_x)
19233 struct glyph_string *s;
19234 int start;
19235 int last_x;
19236 {
19237 /* If the face of this glyph string has to be drawn to the end of
19238 the drawing area, set S->extends_to_end_of_line_p. */
19239
19240 if (start == s->row->used[s->area]
19241 && s->area == TEXT_AREA
19242 && ((s->row->fill_line_p
19243 && (s->hl == DRAW_NORMAL_TEXT
19244 || s->hl == DRAW_IMAGE_RAISED
19245 || s->hl == DRAW_IMAGE_SUNKEN))
19246 || s->hl == DRAW_MOUSE_FACE))
19247 s->extends_to_end_of_line_p = 1;
19248
19249 /* If S extends its face to the end of the line, set its
19250 background_width to the distance to the right edge of the drawing
19251 area. */
19252 if (s->extends_to_end_of_line_p)
19253 s->background_width = last_x - s->x + 1;
19254 else
19255 s->background_width = s->width;
19256 }
19257
19258
19259 /* Compute overhangs and x-positions for glyph string S and its
19260 predecessors, or successors. X is the starting x-position for S.
19261 BACKWARD_P non-zero means process predecessors. */
19262
19263 static void
19264 compute_overhangs_and_x (s, x, backward_p)
19265 struct glyph_string *s;
19266 int x;
19267 int backward_p;
19268 {
19269 if (backward_p)
19270 {
19271 while (s)
19272 {
19273 if (rif->compute_glyph_string_overhangs)
19274 rif->compute_glyph_string_overhangs (s);
19275 x -= s->width;
19276 s->x = x;
19277 s = s->prev;
19278 }
19279 }
19280 else
19281 {
19282 while (s)
19283 {
19284 if (rif->compute_glyph_string_overhangs)
19285 rif->compute_glyph_string_overhangs (s);
19286 s->x = x;
19287 x += s->width;
19288 s = s->next;
19289 }
19290 }
19291 }
19292
19293
19294
19295 /* The following macros are only called from draw_glyphs below.
19296 They reference the following parameters of that function directly:
19297 `w', `row', `area', and `overlap_p'
19298 as well as the following local variables:
19299 `s', `f', and `hdc' (in W32) */
19300
19301 #ifdef HAVE_NTGUI
19302 /* On W32, silently add local `hdc' variable to argument list of
19303 init_glyph_string. */
19304 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19305 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19306 #else
19307 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19308 init_glyph_string (s, char2b, w, row, area, start, hl)
19309 #endif
19310
19311 /* Add a glyph string for a stretch glyph to the list of strings
19312 between HEAD and TAIL. START is the index of the stretch glyph in
19313 row area AREA of glyph row ROW. END is the index of the last glyph
19314 in that glyph row area. X is the current output position assigned
19315 to the new glyph string constructed. HL overrides that face of the
19316 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19317 is the right-most x-position of the drawing area. */
19318
19319 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19320 and below -- keep them on one line. */
19321 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19322 do \
19323 { \
19324 s = (struct glyph_string *) alloca (sizeof *s); \
19325 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19326 START = fill_stretch_glyph_string (s, row, area, START, END); \
19327 append_glyph_string (&HEAD, &TAIL, s); \
19328 s->x = (X); \
19329 } \
19330 while (0)
19331
19332
19333 /* Add a glyph string for an image glyph to the list of strings
19334 between HEAD and TAIL. START is the index of the image glyph in
19335 row area AREA of glyph row ROW. END is the index of the last glyph
19336 in that glyph row area. X is the current output position assigned
19337 to the new glyph string constructed. HL overrides that face of the
19338 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19339 is the right-most x-position of the drawing area. */
19340
19341 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19342 do \
19343 { \
19344 s = (struct glyph_string *) alloca (sizeof *s); \
19345 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19346 fill_image_glyph_string (s); \
19347 append_glyph_string (&HEAD, &TAIL, s); \
19348 ++START; \
19349 s->x = (X); \
19350 } \
19351 while (0)
19352
19353
19354 /* Add a glyph string for a sequence of character glyphs to the list
19355 of strings between HEAD and TAIL. START is the index of the first
19356 glyph in row area AREA of glyph row ROW that is part of the new
19357 glyph string. END is the index of the last glyph in that glyph row
19358 area. X is the current output position assigned to the new glyph
19359 string constructed. HL overrides that face of the glyph; e.g. it
19360 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19361 right-most x-position of the drawing area. */
19362
19363 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19364 do \
19365 { \
19366 int c, face_id; \
19367 XChar2b *char2b; \
19368 \
19369 c = (row)->glyphs[area][START].u.ch; \
19370 face_id = (row)->glyphs[area][START].face_id; \
19371 \
19372 s = (struct glyph_string *) alloca (sizeof *s); \
19373 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19374 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19375 append_glyph_string (&HEAD, &TAIL, s); \
19376 s->x = (X); \
19377 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19378 } \
19379 while (0)
19380
19381
19382 /* Add a glyph string for a composite sequence to the list of strings
19383 between HEAD and TAIL. START is the index of the first glyph in
19384 row area AREA of glyph row ROW that is part of the new glyph
19385 string. END is the index of the last glyph in that glyph row area.
19386 X is the current output position assigned to the new glyph string
19387 constructed. HL overrides that face of the glyph; e.g. it is
19388 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19389 x-position of the drawing area. */
19390
19391 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19392 do { \
19393 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19394 int face_id = (row)->glyphs[area][START].face_id; \
19395 struct face *base_face = FACE_FROM_ID (f, face_id); \
19396 struct composition *cmp = composition_table[cmp_id]; \
19397 int glyph_len = cmp->glyph_len; \
19398 XChar2b *char2b; \
19399 struct face **faces; \
19400 struct glyph_string *first_s = NULL; \
19401 int n; \
19402 \
19403 base_face = base_face->ascii_face; \
19404 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19405 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19406 /* At first, fill in `char2b' and `faces'. */ \
19407 for (n = 0; n < glyph_len; n++) \
19408 { \
19409 int c = COMPOSITION_GLYPH (cmp, n); \
19410 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19411 faces[n] = FACE_FROM_ID (f, this_face_id); \
19412 get_char_face_and_encoding (f, c, this_face_id, \
19413 char2b + n, 1, 1); \
19414 } \
19415 \
19416 /* Make glyph_strings for each glyph sequence that is drawable by \
19417 the same face, and append them to HEAD/TAIL. */ \
19418 for (n = 0; n < cmp->glyph_len;) \
19419 { \
19420 s = (struct glyph_string *) alloca (sizeof *s); \
19421 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19422 append_glyph_string (&(HEAD), &(TAIL), s); \
19423 s->cmp = cmp; \
19424 s->gidx = n; \
19425 s->x = (X); \
19426 \
19427 if (n == 0) \
19428 first_s = s; \
19429 \
19430 n = fill_composite_glyph_string (s, faces, overlaps); \
19431 } \
19432 \
19433 ++START; \
19434 s = first_s; \
19435 } while (0)
19436
19437
19438 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19439 of AREA of glyph row ROW on window W between indices START and END.
19440 HL overrides the face for drawing glyph strings, e.g. it is
19441 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19442 x-positions of the drawing area.
19443
19444 This is an ugly monster macro construct because we must use alloca
19445 to allocate glyph strings (because draw_glyphs can be called
19446 asynchronously). */
19447
19448 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19449 do \
19450 { \
19451 HEAD = TAIL = NULL; \
19452 while (START < END) \
19453 { \
19454 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19455 switch (first_glyph->type) \
19456 { \
19457 case CHAR_GLYPH: \
19458 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19459 HL, X, LAST_X); \
19460 break; \
19461 \
19462 case COMPOSITE_GLYPH: \
19463 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19464 HL, X, LAST_X); \
19465 break; \
19466 \
19467 case STRETCH_GLYPH: \
19468 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19469 HL, X, LAST_X); \
19470 break; \
19471 \
19472 case IMAGE_GLYPH: \
19473 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19474 HL, X, LAST_X); \
19475 break; \
19476 \
19477 default: \
19478 abort (); \
19479 } \
19480 \
19481 set_glyph_string_background_width (s, START, LAST_X); \
19482 (X) += s->width; \
19483 } \
19484 } \
19485 while (0)
19486
19487
19488 /* Draw glyphs between START and END in AREA of ROW on window W,
19489 starting at x-position X. X is relative to AREA in W. HL is a
19490 face-override with the following meaning:
19491
19492 DRAW_NORMAL_TEXT draw normally
19493 DRAW_CURSOR draw in cursor face
19494 DRAW_MOUSE_FACE draw in mouse face.
19495 DRAW_INVERSE_VIDEO draw in mode line face
19496 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19497 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19498
19499 If OVERLAPS is non-zero, draw only the foreground of characters and
19500 clip to the physical height of ROW. Non-zero value also defines
19501 the overlapping part to be drawn:
19502
19503 OVERLAPS_PRED overlap with preceding rows
19504 OVERLAPS_SUCC overlap with succeeding rows
19505 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19506 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19507
19508 Value is the x-position reached, relative to AREA of W. */
19509
19510 static int
19511 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19512 struct window *w;
19513 int x;
19514 struct glyph_row *row;
19515 enum glyph_row_area area;
19516 int start, end;
19517 enum draw_glyphs_face hl;
19518 int overlaps;
19519 {
19520 struct glyph_string *head, *tail;
19521 struct glyph_string *s;
19522 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19523 int last_x, area_width;
19524 int x_reached;
19525 int i, j;
19526 struct frame *f = XFRAME (WINDOW_FRAME (w));
19527 DECLARE_HDC (hdc);
19528
19529 ALLOCATE_HDC (hdc, f);
19530
19531 /* Let's rather be paranoid than getting a SEGV. */
19532 end = min (end, row->used[area]);
19533 start = max (0, start);
19534 start = min (end, start);
19535
19536 /* Translate X to frame coordinates. Set last_x to the right
19537 end of the drawing area. */
19538 if (row->full_width_p)
19539 {
19540 /* X is relative to the left edge of W, without scroll bars
19541 or fringes. */
19542 x += WINDOW_LEFT_EDGE_X (w);
19543 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19544 }
19545 else
19546 {
19547 int area_left = window_box_left (w, area);
19548 x += area_left;
19549 area_width = window_box_width (w, area);
19550 last_x = area_left + area_width;
19551 }
19552
19553 /* Build a doubly-linked list of glyph_string structures between
19554 head and tail from what we have to draw. Note that the macro
19555 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19556 the reason we use a separate variable `i'. */
19557 i = start;
19558 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19559 if (tail)
19560 x_reached = tail->x + tail->background_width;
19561 else
19562 x_reached = x;
19563
19564 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19565 the row, redraw some glyphs in front or following the glyph
19566 strings built above. */
19567 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19568 {
19569 int dummy_x = 0;
19570 struct glyph_string *h, *t;
19571
19572 /* Compute overhangs for all glyph strings. */
19573 if (rif->compute_glyph_string_overhangs)
19574 for (s = head; s; s = s->next)
19575 rif->compute_glyph_string_overhangs (s);
19576
19577 /* Prepend glyph strings for glyphs in front of the first glyph
19578 string that are overwritten because of the first glyph
19579 string's left overhang. The background of all strings
19580 prepended must be drawn because the first glyph string
19581 draws over it. */
19582 i = left_overwritten (head);
19583 if (i >= 0)
19584 {
19585 j = i;
19586 BUILD_GLYPH_STRINGS (j, start, h, t,
19587 DRAW_NORMAL_TEXT, dummy_x, last_x);
19588 start = i;
19589 compute_overhangs_and_x (t, head->x, 1);
19590 prepend_glyph_string_lists (&head, &tail, h, t);
19591 clip_head = head;
19592 }
19593
19594 /* Prepend glyph strings for glyphs in front of the first glyph
19595 string that overwrite that glyph string because of their
19596 right overhang. For these strings, only the foreground must
19597 be drawn, because it draws over the glyph string at `head'.
19598 The background must not be drawn because this would overwrite
19599 right overhangs of preceding glyphs for which no glyph
19600 strings exist. */
19601 i = left_overwriting (head);
19602 if (i >= 0)
19603 {
19604 clip_head = head;
19605 BUILD_GLYPH_STRINGS (i, start, h, t,
19606 DRAW_NORMAL_TEXT, dummy_x, last_x);
19607 for (s = h; s; s = s->next)
19608 s->background_filled_p = 1;
19609 compute_overhangs_and_x (t, head->x, 1);
19610 prepend_glyph_string_lists (&head, &tail, h, t);
19611 }
19612
19613 /* Append glyphs strings for glyphs following the last glyph
19614 string tail that are overwritten by tail. The background of
19615 these strings has to be drawn because tail's foreground draws
19616 over it. */
19617 i = right_overwritten (tail);
19618 if (i >= 0)
19619 {
19620 BUILD_GLYPH_STRINGS (end, i, h, t,
19621 DRAW_NORMAL_TEXT, x, last_x);
19622 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19623 append_glyph_string_lists (&head, &tail, h, t);
19624 clip_tail = tail;
19625 }
19626
19627 /* Append glyph strings for glyphs following the last glyph
19628 string tail that overwrite tail. The foreground of such
19629 glyphs has to be drawn because it writes into the background
19630 of tail. The background must not be drawn because it could
19631 paint over the foreground of following glyphs. */
19632 i = right_overwriting (tail);
19633 if (i >= 0)
19634 {
19635 clip_tail = tail;
19636 BUILD_GLYPH_STRINGS (end, i, h, t,
19637 DRAW_NORMAL_TEXT, x, last_x);
19638 for (s = h; s; s = s->next)
19639 s->background_filled_p = 1;
19640 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19641 append_glyph_string_lists (&head, &tail, h, t);
19642 }
19643 if (clip_head || clip_tail)
19644 for (s = head; s; s = s->next)
19645 {
19646 s->clip_head = clip_head;
19647 s->clip_tail = clip_tail;
19648 }
19649 }
19650
19651 /* Draw all strings. */
19652 for (s = head; s; s = s->next)
19653 rif->draw_glyph_string (s);
19654
19655 if (area == TEXT_AREA
19656 && !row->full_width_p
19657 /* When drawing overlapping rows, only the glyph strings'
19658 foreground is drawn, which doesn't erase a cursor
19659 completely. */
19660 && !overlaps)
19661 {
19662 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19663 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19664 : (tail ? tail->x + tail->background_width : x));
19665
19666 int text_left = window_box_left (w, TEXT_AREA);
19667 x0 -= text_left;
19668 x1 -= text_left;
19669
19670 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19671 row->y, MATRIX_ROW_BOTTOM_Y (row));
19672 }
19673
19674 /* Value is the x-position up to which drawn, relative to AREA of W.
19675 This doesn't include parts drawn because of overhangs. */
19676 if (row->full_width_p)
19677 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19678 else
19679 x_reached -= window_box_left (w, area);
19680
19681 RELEASE_HDC (hdc, f);
19682
19683 return x_reached;
19684 }
19685
19686 /* Expand row matrix if too narrow. Don't expand if area
19687 is not present. */
19688
19689 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19690 { \
19691 if (!fonts_changed_p \
19692 && (it->glyph_row->glyphs[area] \
19693 < it->glyph_row->glyphs[area + 1])) \
19694 { \
19695 it->w->ncols_scale_factor++; \
19696 fonts_changed_p = 1; \
19697 } \
19698 }
19699
19700 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19701 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19702
19703 static INLINE void
19704 append_glyph (it)
19705 struct it *it;
19706 {
19707 struct glyph *glyph;
19708 enum glyph_row_area area = it->area;
19709
19710 xassert (it->glyph_row);
19711 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19712
19713 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19714 if (glyph < it->glyph_row->glyphs[area + 1])
19715 {
19716 glyph->charpos = CHARPOS (it->position);
19717 glyph->object = it->object;
19718 glyph->pixel_width = it->pixel_width;
19719 glyph->ascent = it->ascent;
19720 glyph->descent = it->descent;
19721 glyph->voffset = it->voffset;
19722 glyph->type = CHAR_GLYPH;
19723 glyph->multibyte_p = it->multibyte_p;
19724 glyph->left_box_line_p = it->start_of_box_run_p;
19725 glyph->right_box_line_p = it->end_of_box_run_p;
19726 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19727 || it->phys_descent > it->descent);
19728 glyph->padding_p = 0;
19729 glyph->glyph_not_available_p = it->glyph_not_available_p;
19730 glyph->face_id = it->face_id;
19731 glyph->u.ch = it->char_to_display;
19732 glyph->slice = null_glyph_slice;
19733 glyph->font_type = FONT_TYPE_UNKNOWN;
19734 ++it->glyph_row->used[area];
19735 }
19736 else
19737 IT_EXPAND_MATRIX_WIDTH (it, area);
19738 }
19739
19740 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19741 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19742
19743 static INLINE void
19744 append_composite_glyph (it)
19745 struct it *it;
19746 {
19747 struct glyph *glyph;
19748 enum glyph_row_area area = it->area;
19749
19750 xassert (it->glyph_row);
19751
19752 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19753 if (glyph < it->glyph_row->glyphs[area + 1])
19754 {
19755 glyph->charpos = CHARPOS (it->position);
19756 glyph->object = it->object;
19757 glyph->pixel_width = it->pixel_width;
19758 glyph->ascent = it->ascent;
19759 glyph->descent = it->descent;
19760 glyph->voffset = it->voffset;
19761 glyph->type = COMPOSITE_GLYPH;
19762 glyph->multibyte_p = it->multibyte_p;
19763 glyph->left_box_line_p = it->start_of_box_run_p;
19764 glyph->right_box_line_p = it->end_of_box_run_p;
19765 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19766 || it->phys_descent > it->descent);
19767 glyph->padding_p = 0;
19768 glyph->glyph_not_available_p = 0;
19769 glyph->face_id = it->face_id;
19770 glyph->u.cmp_id = it->cmp_id;
19771 glyph->slice = null_glyph_slice;
19772 glyph->font_type = FONT_TYPE_UNKNOWN;
19773 ++it->glyph_row->used[area];
19774 }
19775 else
19776 IT_EXPAND_MATRIX_WIDTH (it, area);
19777 }
19778
19779
19780 /* Change IT->ascent and IT->height according to the setting of
19781 IT->voffset. */
19782
19783 static INLINE void
19784 take_vertical_position_into_account (it)
19785 struct it *it;
19786 {
19787 if (it->voffset)
19788 {
19789 if (it->voffset < 0)
19790 /* Increase the ascent so that we can display the text higher
19791 in the line. */
19792 it->ascent -= it->voffset;
19793 else
19794 /* Increase the descent so that we can display the text lower
19795 in the line. */
19796 it->descent += it->voffset;
19797 }
19798 }
19799
19800
19801 /* Produce glyphs/get display metrics for the image IT is loaded with.
19802 See the description of struct display_iterator in dispextern.h for
19803 an overview of struct display_iterator. */
19804
19805 static void
19806 produce_image_glyph (it)
19807 struct it *it;
19808 {
19809 struct image *img;
19810 struct face *face;
19811 int glyph_ascent, crop;
19812 struct glyph_slice slice;
19813
19814 xassert (it->what == IT_IMAGE);
19815
19816 face = FACE_FROM_ID (it->f, it->face_id);
19817 xassert (face);
19818 /* Make sure X resources of the face is loaded. */
19819 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19820
19821 if (it->image_id < 0)
19822 {
19823 /* Fringe bitmap. */
19824 it->ascent = it->phys_ascent = 0;
19825 it->descent = it->phys_descent = 0;
19826 it->pixel_width = 0;
19827 it->nglyphs = 0;
19828 return;
19829 }
19830
19831 img = IMAGE_FROM_ID (it->f, it->image_id);
19832 xassert (img);
19833 /* Make sure X resources of the image is loaded. */
19834 prepare_image_for_display (it->f, img);
19835
19836 slice.x = slice.y = 0;
19837 slice.width = img->width;
19838 slice.height = img->height;
19839
19840 if (INTEGERP (it->slice.x))
19841 slice.x = XINT (it->slice.x);
19842 else if (FLOATP (it->slice.x))
19843 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19844
19845 if (INTEGERP (it->slice.y))
19846 slice.y = XINT (it->slice.y);
19847 else if (FLOATP (it->slice.y))
19848 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19849
19850 if (INTEGERP (it->slice.width))
19851 slice.width = XINT (it->slice.width);
19852 else if (FLOATP (it->slice.width))
19853 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19854
19855 if (INTEGERP (it->slice.height))
19856 slice.height = XINT (it->slice.height);
19857 else if (FLOATP (it->slice.height))
19858 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19859
19860 if (slice.x >= img->width)
19861 slice.x = img->width;
19862 if (slice.y >= img->height)
19863 slice.y = img->height;
19864 if (slice.x + slice.width >= img->width)
19865 slice.width = img->width - slice.x;
19866 if (slice.y + slice.height > img->height)
19867 slice.height = img->height - slice.y;
19868
19869 if (slice.width == 0 || slice.height == 0)
19870 return;
19871
19872 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19873
19874 it->descent = slice.height - glyph_ascent;
19875 if (slice.y == 0)
19876 it->descent += img->vmargin;
19877 if (slice.y + slice.height == img->height)
19878 it->descent += img->vmargin;
19879 it->phys_descent = it->descent;
19880
19881 it->pixel_width = slice.width;
19882 if (slice.x == 0)
19883 it->pixel_width += img->hmargin;
19884 if (slice.x + slice.width == img->width)
19885 it->pixel_width += img->hmargin;
19886
19887 /* It's quite possible for images to have an ascent greater than
19888 their height, so don't get confused in that case. */
19889 if (it->descent < 0)
19890 it->descent = 0;
19891
19892 #if 0 /* this breaks image tiling */
19893 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19894 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19895 if (face_ascent > it->ascent)
19896 it->ascent = it->phys_ascent = face_ascent;
19897 #endif
19898
19899 it->nglyphs = 1;
19900
19901 if (face->box != FACE_NO_BOX)
19902 {
19903 if (face->box_line_width > 0)
19904 {
19905 if (slice.y == 0)
19906 it->ascent += face->box_line_width;
19907 if (slice.y + slice.height == img->height)
19908 it->descent += face->box_line_width;
19909 }
19910
19911 if (it->start_of_box_run_p && slice.x == 0)
19912 it->pixel_width += abs (face->box_line_width);
19913 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19914 it->pixel_width += abs (face->box_line_width);
19915 }
19916
19917 take_vertical_position_into_account (it);
19918
19919 /* Automatically crop wide image glyphs at right edge so we can
19920 draw the cursor on same display row. */
19921 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
19922 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
19923 {
19924 it->pixel_width -= crop;
19925 slice.width -= crop;
19926 }
19927
19928 if (it->glyph_row)
19929 {
19930 struct glyph *glyph;
19931 enum glyph_row_area area = it->area;
19932
19933 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19934 if (glyph < it->glyph_row->glyphs[area + 1])
19935 {
19936 glyph->charpos = CHARPOS (it->position);
19937 glyph->object = it->object;
19938 glyph->pixel_width = it->pixel_width;
19939 glyph->ascent = glyph_ascent;
19940 glyph->descent = it->descent;
19941 glyph->voffset = it->voffset;
19942 glyph->type = IMAGE_GLYPH;
19943 glyph->multibyte_p = it->multibyte_p;
19944 glyph->left_box_line_p = it->start_of_box_run_p;
19945 glyph->right_box_line_p = it->end_of_box_run_p;
19946 glyph->overlaps_vertically_p = 0;
19947 glyph->padding_p = 0;
19948 glyph->glyph_not_available_p = 0;
19949 glyph->face_id = it->face_id;
19950 glyph->u.img_id = img->id;
19951 glyph->slice = slice;
19952 glyph->font_type = FONT_TYPE_UNKNOWN;
19953 ++it->glyph_row->used[area];
19954 }
19955 else
19956 IT_EXPAND_MATRIX_WIDTH (it, area);
19957 }
19958 }
19959
19960
19961 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19962 of the glyph, WIDTH and HEIGHT are the width and height of the
19963 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19964
19965 static void
19966 append_stretch_glyph (it, object, width, height, ascent)
19967 struct it *it;
19968 Lisp_Object object;
19969 int width, height;
19970 int ascent;
19971 {
19972 struct glyph *glyph;
19973 enum glyph_row_area area = it->area;
19974
19975 xassert (ascent >= 0 && ascent <= height);
19976
19977 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19978 if (glyph < it->glyph_row->glyphs[area + 1])
19979 {
19980 glyph->charpos = CHARPOS (it->position);
19981 glyph->object = object;
19982 glyph->pixel_width = width;
19983 glyph->ascent = ascent;
19984 glyph->descent = height - ascent;
19985 glyph->voffset = it->voffset;
19986 glyph->type = STRETCH_GLYPH;
19987 glyph->multibyte_p = it->multibyte_p;
19988 glyph->left_box_line_p = it->start_of_box_run_p;
19989 glyph->right_box_line_p = it->end_of_box_run_p;
19990 glyph->overlaps_vertically_p = 0;
19991 glyph->padding_p = 0;
19992 glyph->glyph_not_available_p = 0;
19993 glyph->face_id = it->face_id;
19994 glyph->u.stretch.ascent = ascent;
19995 glyph->u.stretch.height = height;
19996 glyph->slice = null_glyph_slice;
19997 glyph->font_type = FONT_TYPE_UNKNOWN;
19998 ++it->glyph_row->used[area];
19999 }
20000 else
20001 IT_EXPAND_MATRIX_WIDTH (it, area);
20002 }
20003
20004
20005 /* Produce a stretch glyph for iterator IT. IT->object is the value
20006 of the glyph property displayed. The value must be a list
20007 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20008 being recognized:
20009
20010 1. `:width WIDTH' specifies that the space should be WIDTH *
20011 canonical char width wide. WIDTH may be an integer or floating
20012 point number.
20013
20014 2. `:relative-width FACTOR' specifies that the width of the stretch
20015 should be computed from the width of the first character having the
20016 `glyph' property, and should be FACTOR times that width.
20017
20018 3. `:align-to HPOS' specifies that the space should be wide enough
20019 to reach HPOS, a value in canonical character units.
20020
20021 Exactly one of the above pairs must be present.
20022
20023 4. `:height HEIGHT' specifies that the height of the stretch produced
20024 should be HEIGHT, measured in canonical character units.
20025
20026 5. `:relative-height FACTOR' specifies that the height of the
20027 stretch should be FACTOR times the height of the characters having
20028 the glyph property.
20029
20030 Either none or exactly one of 4 or 5 must be present.
20031
20032 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20033 of the stretch should be used for the ascent of the stretch.
20034 ASCENT must be in the range 0 <= ASCENT <= 100. */
20035
20036 static void
20037 produce_stretch_glyph (it)
20038 struct it *it;
20039 {
20040 /* (space :width WIDTH :height HEIGHT ...) */
20041 Lisp_Object prop, plist;
20042 int width = 0, height = 0, align_to = -1;
20043 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20044 int ascent = 0;
20045 double tem;
20046 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20047 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20048
20049 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20050
20051 /* List should start with `space'. */
20052 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20053 plist = XCDR (it->object);
20054
20055 /* Compute the width of the stretch. */
20056 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20057 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20058 {
20059 /* Absolute width `:width WIDTH' specified and valid. */
20060 zero_width_ok_p = 1;
20061 width = (int)tem;
20062 }
20063 else if (prop = Fplist_get (plist, QCrelative_width),
20064 NUMVAL (prop) > 0)
20065 {
20066 /* Relative width `:relative-width FACTOR' specified and valid.
20067 Compute the width of the characters having the `glyph'
20068 property. */
20069 struct it it2;
20070 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20071
20072 it2 = *it;
20073 if (it->multibyte_p)
20074 {
20075 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20076 - IT_BYTEPOS (*it));
20077 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20078 }
20079 else
20080 it2.c = *p, it2.len = 1;
20081
20082 it2.glyph_row = NULL;
20083 it2.what = IT_CHARACTER;
20084 x_produce_glyphs (&it2);
20085 width = NUMVAL (prop) * it2.pixel_width;
20086 }
20087 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20088 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20089 {
20090 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20091 align_to = (align_to < 0
20092 ? 0
20093 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20094 else if (align_to < 0)
20095 align_to = window_box_left_offset (it->w, TEXT_AREA);
20096 width = max (0, (int)tem + align_to - it->current_x);
20097 zero_width_ok_p = 1;
20098 }
20099 else
20100 /* Nothing specified -> width defaults to canonical char width. */
20101 width = FRAME_COLUMN_WIDTH (it->f);
20102
20103 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20104 width = 1;
20105
20106 /* Compute height. */
20107 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20108 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20109 {
20110 height = (int)tem;
20111 zero_height_ok_p = 1;
20112 }
20113 else if (prop = Fplist_get (plist, QCrelative_height),
20114 NUMVAL (prop) > 0)
20115 height = FONT_HEIGHT (font) * NUMVAL (prop);
20116 else
20117 height = FONT_HEIGHT (font);
20118
20119 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20120 height = 1;
20121
20122 /* Compute percentage of height used for ascent. If
20123 `:ascent ASCENT' is present and valid, use that. Otherwise,
20124 derive the ascent from the font in use. */
20125 if (prop = Fplist_get (plist, QCascent),
20126 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20127 ascent = height * NUMVAL (prop) / 100.0;
20128 else if (!NILP (prop)
20129 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20130 ascent = min (max (0, (int)tem), height);
20131 else
20132 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20133
20134 if (width > 0 && !it->truncate_lines_p
20135 && it->current_x + width > it->last_visible_x)
20136 width = it->last_visible_x - it->current_x - 1;
20137
20138 if (width > 0 && height > 0 && it->glyph_row)
20139 {
20140 Lisp_Object object = it->stack[it->sp - 1].string;
20141 if (!STRINGP (object))
20142 object = it->w->buffer;
20143 append_stretch_glyph (it, object, width, height, ascent);
20144 }
20145
20146 it->pixel_width = width;
20147 it->ascent = it->phys_ascent = ascent;
20148 it->descent = it->phys_descent = height - it->ascent;
20149 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20150
20151 take_vertical_position_into_account (it);
20152 }
20153
20154 /* Get line-height and line-spacing property at point.
20155 If line-height has format (HEIGHT TOTAL), return TOTAL
20156 in TOTAL_HEIGHT. */
20157
20158 static Lisp_Object
20159 get_line_height_property (it, prop)
20160 struct it *it;
20161 Lisp_Object prop;
20162 {
20163 Lisp_Object position;
20164
20165 if (STRINGP (it->object))
20166 position = make_number (IT_STRING_CHARPOS (*it));
20167 else if (BUFFERP (it->object))
20168 position = make_number (IT_CHARPOS (*it));
20169 else
20170 return Qnil;
20171
20172 return Fget_char_property (position, prop, it->object);
20173 }
20174
20175 /* Calculate line-height and line-spacing properties.
20176 An integer value specifies explicit pixel value.
20177 A float value specifies relative value to current face height.
20178 A cons (float . face-name) specifies relative value to
20179 height of specified face font.
20180
20181 Returns height in pixels, or nil. */
20182
20183
20184 static Lisp_Object
20185 calc_line_height_property (it, val, font, boff, override)
20186 struct it *it;
20187 Lisp_Object val;
20188 XFontStruct *font;
20189 int boff, override;
20190 {
20191 Lisp_Object face_name = Qnil;
20192 int ascent, descent, height;
20193
20194 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20195 return val;
20196
20197 if (CONSP (val))
20198 {
20199 face_name = XCAR (val);
20200 val = XCDR (val);
20201 if (!NUMBERP (val))
20202 val = make_number (1);
20203 if (NILP (face_name))
20204 {
20205 height = it->ascent + it->descent;
20206 goto scale;
20207 }
20208 }
20209
20210 if (NILP (face_name))
20211 {
20212 font = FRAME_FONT (it->f);
20213 boff = FRAME_BASELINE_OFFSET (it->f);
20214 }
20215 else if (EQ (face_name, Qt))
20216 {
20217 override = 0;
20218 }
20219 else
20220 {
20221 int face_id;
20222 struct face *face;
20223 struct font_info *font_info;
20224
20225 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20226 if (face_id < 0)
20227 return make_number (-1);
20228
20229 face = FACE_FROM_ID (it->f, face_id);
20230 font = face->font;
20231 if (font == NULL)
20232 return make_number (-1);
20233
20234 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20235 boff = font_info->baseline_offset;
20236 if (font_info->vertical_centering)
20237 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20238 }
20239
20240 ascent = FONT_BASE (font) + boff;
20241 descent = FONT_DESCENT (font) - boff;
20242
20243 if (override)
20244 {
20245 it->override_ascent = ascent;
20246 it->override_descent = descent;
20247 it->override_boff = boff;
20248 }
20249
20250 height = ascent + descent;
20251
20252 scale:
20253 if (FLOATP (val))
20254 height = (int)(XFLOAT_DATA (val) * height);
20255 else if (INTEGERP (val))
20256 height *= XINT (val);
20257
20258 return make_number (height);
20259 }
20260
20261
20262 /* RIF:
20263 Produce glyphs/get display metrics for the display element IT is
20264 loaded with. See the description of struct it in dispextern.h
20265 for an overview of struct it. */
20266
20267 void
20268 x_produce_glyphs (it)
20269 struct it *it;
20270 {
20271 int extra_line_spacing = it->extra_line_spacing;
20272
20273 it->glyph_not_available_p = 0;
20274
20275 if (it->what == IT_CHARACTER)
20276 {
20277 XChar2b char2b;
20278 XFontStruct *font;
20279 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20280 XCharStruct *pcm;
20281 int font_not_found_p;
20282 struct font_info *font_info;
20283 int boff; /* baseline offset */
20284 /* We may change it->multibyte_p upon unibyte<->multibyte
20285 conversion. So, save the current value now and restore it
20286 later.
20287
20288 Note: It seems that we don't have to record multibyte_p in
20289 struct glyph because the character code itself tells if or
20290 not the character is multibyte. Thus, in the future, we must
20291 consider eliminating the field `multibyte_p' in the struct
20292 glyph. */
20293 int saved_multibyte_p = it->multibyte_p;
20294
20295 /* Maybe translate single-byte characters to multibyte, or the
20296 other way. */
20297 it->char_to_display = it->c;
20298 if (!ASCII_BYTE_P (it->c))
20299 {
20300 if (unibyte_display_via_language_environment
20301 && SINGLE_BYTE_CHAR_P (it->c)
20302 && (it->c >= 0240
20303 || !NILP (Vnonascii_translation_table)))
20304 {
20305 it->char_to_display = unibyte_char_to_multibyte (it->c);
20306 it->multibyte_p = 1;
20307 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20308 face = FACE_FROM_ID (it->f, it->face_id);
20309 }
20310 else if (!SINGLE_BYTE_CHAR_P (it->c)
20311 && !it->multibyte_p)
20312 {
20313 it->multibyte_p = 1;
20314 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20315 face = FACE_FROM_ID (it->f, it->face_id);
20316 }
20317 }
20318
20319 /* Get font to use. Encode IT->char_to_display. */
20320 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20321 &char2b, it->multibyte_p, 0);
20322 font = face->font;
20323
20324 /* When no suitable font found, use the default font. */
20325 font_not_found_p = font == NULL;
20326 if (font_not_found_p)
20327 {
20328 font = FRAME_FONT (it->f);
20329 boff = FRAME_BASELINE_OFFSET (it->f);
20330 font_info = NULL;
20331 }
20332 else
20333 {
20334 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20335 boff = font_info->baseline_offset;
20336 if (font_info->vertical_centering)
20337 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20338 }
20339
20340 if (it->char_to_display >= ' '
20341 && (!it->multibyte_p || it->char_to_display < 128))
20342 {
20343 /* Either unibyte or ASCII. */
20344 int stretched_p;
20345
20346 it->nglyphs = 1;
20347
20348 pcm = rif->per_char_metric (font, &char2b,
20349 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20350
20351 if (it->override_ascent >= 0)
20352 {
20353 it->ascent = it->override_ascent;
20354 it->descent = it->override_descent;
20355 boff = it->override_boff;
20356 }
20357 else
20358 {
20359 it->ascent = FONT_BASE (font) + boff;
20360 it->descent = FONT_DESCENT (font) - boff;
20361 }
20362
20363 if (pcm)
20364 {
20365 it->phys_ascent = pcm->ascent + boff;
20366 it->phys_descent = pcm->descent - boff;
20367 it->pixel_width = pcm->width;
20368 }
20369 else
20370 {
20371 it->glyph_not_available_p = 1;
20372 it->phys_ascent = it->ascent;
20373 it->phys_descent = it->descent;
20374 it->pixel_width = FONT_WIDTH (font);
20375 }
20376
20377 if (it->constrain_row_ascent_descent_p)
20378 {
20379 if (it->descent > it->max_descent)
20380 {
20381 it->ascent += it->descent - it->max_descent;
20382 it->descent = it->max_descent;
20383 }
20384 if (it->ascent > it->max_ascent)
20385 {
20386 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20387 it->ascent = it->max_ascent;
20388 }
20389 it->phys_ascent = min (it->phys_ascent, it->ascent);
20390 it->phys_descent = min (it->phys_descent, it->descent);
20391 extra_line_spacing = 0;
20392 }
20393
20394 /* If this is a space inside a region of text with
20395 `space-width' property, change its width. */
20396 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20397 if (stretched_p)
20398 it->pixel_width *= XFLOATINT (it->space_width);
20399
20400 /* If face has a box, add the box thickness to the character
20401 height. If character has a box line to the left and/or
20402 right, add the box line width to the character's width. */
20403 if (face->box != FACE_NO_BOX)
20404 {
20405 int thick = face->box_line_width;
20406
20407 if (thick > 0)
20408 {
20409 it->ascent += thick;
20410 it->descent += thick;
20411 }
20412 else
20413 thick = -thick;
20414
20415 if (it->start_of_box_run_p)
20416 it->pixel_width += thick;
20417 if (it->end_of_box_run_p)
20418 it->pixel_width += thick;
20419 }
20420
20421 /* If face has an overline, add the height of the overline
20422 (1 pixel) and a 1 pixel margin to the character height. */
20423 if (face->overline_p)
20424 it->ascent += overline_margin;
20425
20426 if (it->constrain_row_ascent_descent_p)
20427 {
20428 if (it->ascent > it->max_ascent)
20429 it->ascent = it->max_ascent;
20430 if (it->descent > it->max_descent)
20431 it->descent = it->max_descent;
20432 }
20433
20434 take_vertical_position_into_account (it);
20435
20436 /* If we have to actually produce glyphs, do it. */
20437 if (it->glyph_row)
20438 {
20439 if (stretched_p)
20440 {
20441 /* Translate a space with a `space-width' property
20442 into a stretch glyph. */
20443 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20444 / FONT_HEIGHT (font));
20445 append_stretch_glyph (it, it->object, it->pixel_width,
20446 it->ascent + it->descent, ascent);
20447 }
20448 else
20449 append_glyph (it);
20450
20451 /* If characters with lbearing or rbearing are displayed
20452 in this line, record that fact in a flag of the
20453 glyph row. This is used to optimize X output code. */
20454 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20455 it->glyph_row->contains_overlapping_glyphs_p = 1;
20456 }
20457 }
20458 else if (it->char_to_display == '\n')
20459 {
20460 /* A newline has no width but we need the height of the line.
20461 But if previous part of the line set a height, don't
20462 increase that height */
20463
20464 Lisp_Object height;
20465 Lisp_Object total_height = Qnil;
20466
20467 it->override_ascent = -1;
20468 it->pixel_width = 0;
20469 it->nglyphs = 0;
20470
20471 height = get_line_height_property(it, Qline_height);
20472 /* Split (line-height total-height) list */
20473 if (CONSP (height)
20474 && CONSP (XCDR (height))
20475 && NILP (XCDR (XCDR (height))))
20476 {
20477 total_height = XCAR (XCDR (height));
20478 height = XCAR (height);
20479 }
20480 height = calc_line_height_property(it, height, font, boff, 1);
20481
20482 if (it->override_ascent >= 0)
20483 {
20484 it->ascent = it->override_ascent;
20485 it->descent = it->override_descent;
20486 boff = it->override_boff;
20487 }
20488 else
20489 {
20490 it->ascent = FONT_BASE (font) + boff;
20491 it->descent = FONT_DESCENT (font) - boff;
20492 }
20493
20494 if (EQ (height, Qt))
20495 {
20496 if (it->descent > it->max_descent)
20497 {
20498 it->ascent += it->descent - it->max_descent;
20499 it->descent = it->max_descent;
20500 }
20501 if (it->ascent > it->max_ascent)
20502 {
20503 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20504 it->ascent = it->max_ascent;
20505 }
20506 it->phys_ascent = min (it->phys_ascent, it->ascent);
20507 it->phys_descent = min (it->phys_descent, it->descent);
20508 it->constrain_row_ascent_descent_p = 1;
20509 extra_line_spacing = 0;
20510 }
20511 else
20512 {
20513 Lisp_Object spacing;
20514
20515 it->phys_ascent = it->ascent;
20516 it->phys_descent = it->descent;
20517
20518 if ((it->max_ascent > 0 || it->max_descent > 0)
20519 && face->box != FACE_NO_BOX
20520 && face->box_line_width > 0)
20521 {
20522 it->ascent += face->box_line_width;
20523 it->descent += face->box_line_width;
20524 }
20525 if (!NILP (height)
20526 && XINT (height) > it->ascent + it->descent)
20527 it->ascent = XINT (height) - it->descent;
20528
20529 if (!NILP (total_height))
20530 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20531 else
20532 {
20533 spacing = get_line_height_property(it, Qline_spacing);
20534 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20535 }
20536 if (INTEGERP (spacing))
20537 {
20538 extra_line_spacing = XINT (spacing);
20539 if (!NILP (total_height))
20540 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20541 }
20542 }
20543 }
20544 else if (it->char_to_display == '\t')
20545 {
20546 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20547 int x = it->current_x + it->continuation_lines_width;
20548 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20549
20550 /* If the distance from the current position to the next tab
20551 stop is less than a space character width, use the
20552 tab stop after that. */
20553 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20554 next_tab_x += tab_width;
20555
20556 it->pixel_width = next_tab_x - x;
20557 it->nglyphs = 1;
20558 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20559 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20560
20561 if (it->glyph_row)
20562 {
20563 append_stretch_glyph (it, it->object, it->pixel_width,
20564 it->ascent + it->descent, it->ascent);
20565 }
20566 }
20567 else
20568 {
20569 /* A multi-byte character. Assume that the display width of the
20570 character is the width of the character multiplied by the
20571 width of the font. */
20572
20573 /* If we found a font, this font should give us the right
20574 metrics. If we didn't find a font, use the frame's
20575 default font and calculate the width of the character
20576 from the charset width; this is what old redisplay code
20577 did. */
20578
20579 pcm = rif->per_char_metric (font, &char2b,
20580 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20581
20582 if (font_not_found_p || !pcm)
20583 {
20584 int charset = CHAR_CHARSET (it->char_to_display);
20585
20586 it->glyph_not_available_p = 1;
20587 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20588 * CHARSET_WIDTH (charset));
20589 it->phys_ascent = FONT_BASE (font) + boff;
20590 it->phys_descent = FONT_DESCENT (font) - boff;
20591 }
20592 else
20593 {
20594 it->pixel_width = pcm->width;
20595 it->phys_ascent = pcm->ascent + boff;
20596 it->phys_descent = pcm->descent - boff;
20597 if (it->glyph_row
20598 && (pcm->lbearing < 0
20599 || pcm->rbearing > pcm->width))
20600 it->glyph_row->contains_overlapping_glyphs_p = 1;
20601 }
20602 it->nglyphs = 1;
20603 it->ascent = FONT_BASE (font) + boff;
20604 it->descent = FONT_DESCENT (font) - boff;
20605 if (face->box != FACE_NO_BOX)
20606 {
20607 int thick = face->box_line_width;
20608
20609 if (thick > 0)
20610 {
20611 it->ascent += thick;
20612 it->descent += thick;
20613 }
20614 else
20615 thick = - thick;
20616
20617 if (it->start_of_box_run_p)
20618 it->pixel_width += thick;
20619 if (it->end_of_box_run_p)
20620 it->pixel_width += thick;
20621 }
20622
20623 /* If face has an overline, add the height of the overline
20624 (1 pixel) and a 1 pixel margin to the character height. */
20625 if (face->overline_p)
20626 it->ascent += overline_margin;
20627
20628 take_vertical_position_into_account (it);
20629
20630 if (it->glyph_row)
20631 append_glyph (it);
20632 }
20633 it->multibyte_p = saved_multibyte_p;
20634 }
20635 else if (it->what == IT_COMPOSITION)
20636 {
20637 /* Note: A composition is represented as one glyph in the
20638 glyph matrix. There are no padding glyphs. */
20639 XChar2b char2b;
20640 XFontStruct *font;
20641 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20642 XCharStruct *pcm;
20643 int font_not_found_p;
20644 struct font_info *font_info;
20645 int boff; /* baseline offset */
20646 struct composition *cmp = composition_table[it->cmp_id];
20647
20648 /* Maybe translate single-byte characters to multibyte. */
20649 it->char_to_display = it->c;
20650 if (unibyte_display_via_language_environment
20651 && SINGLE_BYTE_CHAR_P (it->c)
20652 && (it->c >= 0240
20653 || (it->c >= 0200
20654 && !NILP (Vnonascii_translation_table))))
20655 {
20656 it->char_to_display = unibyte_char_to_multibyte (it->c);
20657 }
20658
20659 /* Get face and font to use. Encode IT->char_to_display. */
20660 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20661 face = FACE_FROM_ID (it->f, it->face_id);
20662 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20663 &char2b, it->multibyte_p, 0);
20664 font = face->font;
20665
20666 /* When no suitable font found, use the default font. */
20667 font_not_found_p = font == NULL;
20668 if (font_not_found_p)
20669 {
20670 font = FRAME_FONT (it->f);
20671 boff = FRAME_BASELINE_OFFSET (it->f);
20672 font_info = NULL;
20673 }
20674 else
20675 {
20676 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20677 boff = font_info->baseline_offset;
20678 if (font_info->vertical_centering)
20679 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20680 }
20681
20682 /* There are no padding glyphs, so there is only one glyph to
20683 produce for the composition. Important is that pixel_width,
20684 ascent and descent are the values of what is drawn by
20685 draw_glyphs (i.e. the values of the overall glyphs composed). */
20686 it->nglyphs = 1;
20687
20688 /* If we have not yet calculated pixel size data of glyphs of
20689 the composition for the current face font, calculate them
20690 now. Theoretically, we have to check all fonts for the
20691 glyphs, but that requires much time and memory space. So,
20692 here we check only the font of the first glyph. This leads
20693 to incorrect display very rarely, and C-l (recenter) can
20694 correct the display anyway. */
20695 if (cmp->font != (void *) font)
20696 {
20697 /* Ascent and descent of the font of the first character of
20698 this composition (adjusted by baseline offset). Ascent
20699 and descent of overall glyphs should not be less than
20700 them respectively. */
20701 int font_ascent = FONT_BASE (font) + boff;
20702 int font_descent = FONT_DESCENT (font) - boff;
20703 /* Bounding box of the overall glyphs. */
20704 int leftmost, rightmost, lowest, highest;
20705 int i, width, ascent, descent;
20706
20707 cmp->font = (void *) font;
20708
20709 /* Initialize the bounding box. */
20710 if (font_info
20711 && (pcm = rif->per_char_metric (font, &char2b,
20712 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20713 {
20714 width = pcm->width;
20715 ascent = pcm->ascent;
20716 descent = pcm->descent;
20717 }
20718 else
20719 {
20720 width = FONT_WIDTH (font);
20721 ascent = FONT_BASE (font);
20722 descent = FONT_DESCENT (font);
20723 }
20724
20725 rightmost = width;
20726 lowest = - descent + boff;
20727 highest = ascent + boff;
20728 leftmost = 0;
20729
20730 if (font_info
20731 && font_info->default_ascent
20732 && CHAR_TABLE_P (Vuse_default_ascent)
20733 && !NILP (Faref (Vuse_default_ascent,
20734 make_number (it->char_to_display))))
20735 highest = font_info->default_ascent + boff;
20736
20737 /* Draw the first glyph at the normal position. It may be
20738 shifted to right later if some other glyphs are drawn at
20739 the left. */
20740 cmp->offsets[0] = 0;
20741 cmp->offsets[1] = boff;
20742
20743 /* Set cmp->offsets for the remaining glyphs. */
20744 for (i = 1; i < cmp->glyph_len; i++)
20745 {
20746 int left, right, btm, top;
20747 int ch = COMPOSITION_GLYPH (cmp, i);
20748 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20749
20750 face = FACE_FROM_ID (it->f, face_id);
20751 get_char_face_and_encoding (it->f, ch, face->id,
20752 &char2b, it->multibyte_p, 0);
20753 font = face->font;
20754 if (font == NULL)
20755 {
20756 font = FRAME_FONT (it->f);
20757 boff = FRAME_BASELINE_OFFSET (it->f);
20758 font_info = NULL;
20759 }
20760 else
20761 {
20762 font_info
20763 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20764 boff = font_info->baseline_offset;
20765 if (font_info->vertical_centering)
20766 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20767 }
20768
20769 if (font_info
20770 && (pcm = rif->per_char_metric (font, &char2b,
20771 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20772 {
20773 width = pcm->width;
20774 ascent = pcm->ascent;
20775 descent = pcm->descent;
20776 }
20777 else
20778 {
20779 width = FONT_WIDTH (font);
20780 ascent = 1;
20781 descent = 0;
20782 }
20783
20784 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20785 {
20786 /* Relative composition with or without
20787 alternate chars. */
20788 left = (leftmost + rightmost - width) / 2;
20789 btm = - descent + boff;
20790 if (font_info && font_info->relative_compose
20791 && (! CHAR_TABLE_P (Vignore_relative_composition)
20792 || NILP (Faref (Vignore_relative_composition,
20793 make_number (ch)))))
20794 {
20795
20796 if (- descent >= font_info->relative_compose)
20797 /* One extra pixel between two glyphs. */
20798 btm = highest + 1;
20799 else if (ascent <= 0)
20800 /* One extra pixel between two glyphs. */
20801 btm = lowest - 1 - ascent - descent;
20802 }
20803 }
20804 else
20805 {
20806 /* A composition rule is specified by an integer
20807 value that encodes global and new reference
20808 points (GREF and NREF). GREF and NREF are
20809 specified by numbers as below:
20810
20811 0---1---2 -- ascent
20812 | |
20813 | |
20814 | |
20815 9--10--11 -- center
20816 | |
20817 ---3---4---5--- baseline
20818 | |
20819 6---7---8 -- descent
20820 */
20821 int rule = COMPOSITION_RULE (cmp, i);
20822 int gref, nref, grefx, grefy, nrefx, nrefy;
20823
20824 COMPOSITION_DECODE_RULE (rule, gref, nref);
20825 grefx = gref % 3, nrefx = nref % 3;
20826 grefy = gref / 3, nrefy = nref / 3;
20827
20828 left = (leftmost
20829 + grefx * (rightmost - leftmost) / 2
20830 - nrefx * width / 2);
20831 btm = ((grefy == 0 ? highest
20832 : grefy == 1 ? 0
20833 : grefy == 2 ? lowest
20834 : (highest + lowest) / 2)
20835 - (nrefy == 0 ? ascent + descent
20836 : nrefy == 1 ? descent - boff
20837 : nrefy == 2 ? 0
20838 : (ascent + descent) / 2));
20839 }
20840
20841 cmp->offsets[i * 2] = left;
20842 cmp->offsets[i * 2 + 1] = btm + descent;
20843
20844 /* Update the bounding box of the overall glyphs. */
20845 right = left + width;
20846 top = btm + descent + ascent;
20847 if (left < leftmost)
20848 leftmost = left;
20849 if (right > rightmost)
20850 rightmost = right;
20851 if (top > highest)
20852 highest = top;
20853 if (btm < lowest)
20854 lowest = btm;
20855 }
20856
20857 /* If there are glyphs whose x-offsets are negative,
20858 shift all glyphs to the right and make all x-offsets
20859 non-negative. */
20860 if (leftmost < 0)
20861 {
20862 for (i = 0; i < cmp->glyph_len; i++)
20863 cmp->offsets[i * 2] -= leftmost;
20864 rightmost -= leftmost;
20865 }
20866
20867 cmp->pixel_width = rightmost;
20868 cmp->ascent = highest;
20869 cmp->descent = - lowest;
20870 if (cmp->ascent < font_ascent)
20871 cmp->ascent = font_ascent;
20872 if (cmp->descent < font_descent)
20873 cmp->descent = font_descent;
20874 }
20875
20876 it->pixel_width = cmp->pixel_width;
20877 it->ascent = it->phys_ascent = cmp->ascent;
20878 it->descent = it->phys_descent = cmp->descent;
20879
20880 if (face->box != FACE_NO_BOX)
20881 {
20882 int thick = face->box_line_width;
20883
20884 if (thick > 0)
20885 {
20886 it->ascent += thick;
20887 it->descent += thick;
20888 }
20889 else
20890 thick = - thick;
20891
20892 if (it->start_of_box_run_p)
20893 it->pixel_width += thick;
20894 if (it->end_of_box_run_p)
20895 it->pixel_width += thick;
20896 }
20897
20898 /* If face has an overline, add the height of the overline
20899 (1 pixel) and a 1 pixel margin to the character height. */
20900 if (face->overline_p)
20901 it->ascent += overline_margin;
20902
20903 take_vertical_position_into_account (it);
20904
20905 if (it->glyph_row)
20906 append_composite_glyph (it);
20907 }
20908 else if (it->what == IT_IMAGE)
20909 produce_image_glyph (it);
20910 else if (it->what == IT_STRETCH)
20911 produce_stretch_glyph (it);
20912
20913 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20914 because this isn't true for images with `:ascent 100'. */
20915 xassert (it->ascent >= 0 && it->descent >= 0);
20916 if (it->area == TEXT_AREA)
20917 it->current_x += it->pixel_width;
20918
20919 if (extra_line_spacing > 0)
20920 {
20921 it->descent += extra_line_spacing;
20922 if (extra_line_spacing > it->max_extra_line_spacing)
20923 it->max_extra_line_spacing = extra_line_spacing;
20924 }
20925
20926 it->max_ascent = max (it->max_ascent, it->ascent);
20927 it->max_descent = max (it->max_descent, it->descent);
20928 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20929 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20930 }
20931
20932 /* EXPORT for RIF:
20933 Output LEN glyphs starting at START at the nominal cursor position.
20934 Advance the nominal cursor over the text. The global variable
20935 updated_window contains the window being updated, updated_row is
20936 the glyph row being updated, and updated_area is the area of that
20937 row being updated. */
20938
20939 void
20940 x_write_glyphs (start, len)
20941 struct glyph *start;
20942 int len;
20943 {
20944 int x, hpos;
20945
20946 xassert (updated_window && updated_row);
20947 BLOCK_INPUT;
20948
20949 /* Write glyphs. */
20950
20951 hpos = start - updated_row->glyphs[updated_area];
20952 x = draw_glyphs (updated_window, output_cursor.x,
20953 updated_row, updated_area,
20954 hpos, hpos + len,
20955 DRAW_NORMAL_TEXT, 0);
20956
20957 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20958 if (updated_area == TEXT_AREA
20959 && updated_window->phys_cursor_on_p
20960 && updated_window->phys_cursor.vpos == output_cursor.vpos
20961 && updated_window->phys_cursor.hpos >= hpos
20962 && updated_window->phys_cursor.hpos < hpos + len)
20963 updated_window->phys_cursor_on_p = 0;
20964
20965 UNBLOCK_INPUT;
20966
20967 /* Advance the output cursor. */
20968 output_cursor.hpos += len;
20969 output_cursor.x = x;
20970 }
20971
20972
20973 /* EXPORT for RIF:
20974 Insert LEN glyphs from START at the nominal cursor position. */
20975
20976 void
20977 x_insert_glyphs (start, len)
20978 struct glyph *start;
20979 int len;
20980 {
20981 struct frame *f;
20982 struct window *w;
20983 int line_height, shift_by_width, shifted_region_width;
20984 struct glyph_row *row;
20985 struct glyph *glyph;
20986 int frame_x, frame_y, hpos;
20987
20988 xassert (updated_window && updated_row);
20989 BLOCK_INPUT;
20990 w = updated_window;
20991 f = XFRAME (WINDOW_FRAME (w));
20992
20993 /* Get the height of the line we are in. */
20994 row = updated_row;
20995 line_height = row->height;
20996
20997 /* Get the width of the glyphs to insert. */
20998 shift_by_width = 0;
20999 for (glyph = start; glyph < start + len; ++glyph)
21000 shift_by_width += glyph->pixel_width;
21001
21002 /* Get the width of the region to shift right. */
21003 shifted_region_width = (window_box_width (w, updated_area)
21004 - output_cursor.x
21005 - shift_by_width);
21006
21007 /* Shift right. */
21008 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21009 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21010
21011 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21012 line_height, shift_by_width);
21013
21014 /* Write the glyphs. */
21015 hpos = start - row->glyphs[updated_area];
21016 draw_glyphs (w, output_cursor.x, row, updated_area,
21017 hpos, hpos + len,
21018 DRAW_NORMAL_TEXT, 0);
21019
21020 /* Advance the output cursor. */
21021 output_cursor.hpos += len;
21022 output_cursor.x += shift_by_width;
21023 UNBLOCK_INPUT;
21024 }
21025
21026
21027 /* EXPORT for RIF:
21028 Erase the current text line from the nominal cursor position
21029 (inclusive) to pixel column TO_X (exclusive). The idea is that
21030 everything from TO_X onward is already erased.
21031
21032 TO_X is a pixel position relative to updated_area of
21033 updated_window. TO_X == -1 means clear to the end of this area. */
21034
21035 void
21036 x_clear_end_of_line (to_x)
21037 int to_x;
21038 {
21039 struct frame *f;
21040 struct window *w = updated_window;
21041 int max_x, min_y, max_y;
21042 int from_x, from_y, to_y;
21043
21044 xassert (updated_window && updated_row);
21045 f = XFRAME (w->frame);
21046
21047 if (updated_row->full_width_p)
21048 max_x = WINDOW_TOTAL_WIDTH (w);
21049 else
21050 max_x = window_box_width (w, updated_area);
21051 max_y = window_text_bottom_y (w);
21052
21053 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21054 of window. For TO_X > 0, truncate to end of drawing area. */
21055 if (to_x == 0)
21056 return;
21057 else if (to_x < 0)
21058 to_x = max_x;
21059 else
21060 to_x = min (to_x, max_x);
21061
21062 to_y = min (max_y, output_cursor.y + updated_row->height);
21063
21064 /* Notice if the cursor will be cleared by this operation. */
21065 if (!updated_row->full_width_p)
21066 notice_overwritten_cursor (w, updated_area,
21067 output_cursor.x, -1,
21068 updated_row->y,
21069 MATRIX_ROW_BOTTOM_Y (updated_row));
21070
21071 from_x = output_cursor.x;
21072
21073 /* Translate to frame coordinates. */
21074 if (updated_row->full_width_p)
21075 {
21076 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21077 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21078 }
21079 else
21080 {
21081 int area_left = window_box_left (w, updated_area);
21082 from_x += area_left;
21083 to_x += area_left;
21084 }
21085
21086 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21087 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21088 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21089
21090 /* Prevent inadvertently clearing to end of the X window. */
21091 if (to_x > from_x && to_y > from_y)
21092 {
21093 BLOCK_INPUT;
21094 rif->clear_frame_area (f, from_x, from_y,
21095 to_x - from_x, to_y - from_y);
21096 UNBLOCK_INPUT;
21097 }
21098 }
21099
21100 #endif /* HAVE_WINDOW_SYSTEM */
21101
21102
21103 \f
21104 /***********************************************************************
21105 Cursor types
21106 ***********************************************************************/
21107
21108 /* Value is the internal representation of the specified cursor type
21109 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21110 of the bar cursor. */
21111
21112 static enum text_cursor_kinds
21113 get_specified_cursor_type (arg, width)
21114 Lisp_Object arg;
21115 int *width;
21116 {
21117 enum text_cursor_kinds type;
21118
21119 if (NILP (arg))
21120 return NO_CURSOR;
21121
21122 if (EQ (arg, Qbox))
21123 return FILLED_BOX_CURSOR;
21124
21125 if (EQ (arg, Qhollow))
21126 return HOLLOW_BOX_CURSOR;
21127
21128 if (EQ (arg, Qbar))
21129 {
21130 *width = 2;
21131 return BAR_CURSOR;
21132 }
21133
21134 if (CONSP (arg)
21135 && EQ (XCAR (arg), Qbar)
21136 && INTEGERP (XCDR (arg))
21137 && XINT (XCDR (arg)) >= 0)
21138 {
21139 *width = XINT (XCDR (arg));
21140 return BAR_CURSOR;
21141 }
21142
21143 if (EQ (arg, Qhbar))
21144 {
21145 *width = 2;
21146 return HBAR_CURSOR;
21147 }
21148
21149 if (CONSP (arg)
21150 && EQ (XCAR (arg), Qhbar)
21151 && INTEGERP (XCDR (arg))
21152 && XINT (XCDR (arg)) >= 0)
21153 {
21154 *width = XINT (XCDR (arg));
21155 return HBAR_CURSOR;
21156 }
21157
21158 /* Treat anything unknown as "hollow box cursor".
21159 It was bad to signal an error; people have trouble fixing
21160 .Xdefaults with Emacs, when it has something bad in it. */
21161 type = HOLLOW_BOX_CURSOR;
21162
21163 return type;
21164 }
21165
21166 /* Set the default cursor types for specified frame. */
21167 void
21168 set_frame_cursor_types (f, arg)
21169 struct frame *f;
21170 Lisp_Object arg;
21171 {
21172 int width;
21173 Lisp_Object tem;
21174
21175 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21176 FRAME_CURSOR_WIDTH (f) = width;
21177
21178 /* By default, set up the blink-off state depending on the on-state. */
21179
21180 tem = Fassoc (arg, Vblink_cursor_alist);
21181 if (!NILP (tem))
21182 {
21183 FRAME_BLINK_OFF_CURSOR (f)
21184 = get_specified_cursor_type (XCDR (tem), &width);
21185 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21186 }
21187 else
21188 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21189 }
21190
21191
21192 /* Return the cursor we want to be displayed in window W. Return
21193 width of bar/hbar cursor through WIDTH arg. Return with
21194 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21195 (i.e. if the `system caret' should track this cursor).
21196
21197 In a mini-buffer window, we want the cursor only to appear if we
21198 are reading input from this window. For the selected window, we
21199 want the cursor type given by the frame parameter or buffer local
21200 setting of cursor-type. If explicitly marked off, draw no cursor.
21201 In all other cases, we want a hollow box cursor. */
21202
21203 static enum text_cursor_kinds
21204 get_window_cursor_type (w, glyph, width, active_cursor)
21205 struct window *w;
21206 struct glyph *glyph;
21207 int *width;
21208 int *active_cursor;
21209 {
21210 struct frame *f = XFRAME (w->frame);
21211 struct buffer *b = XBUFFER (w->buffer);
21212 int cursor_type = DEFAULT_CURSOR;
21213 Lisp_Object alt_cursor;
21214 int non_selected = 0;
21215
21216 *active_cursor = 1;
21217
21218 /* Echo area */
21219 if (cursor_in_echo_area
21220 && FRAME_HAS_MINIBUF_P (f)
21221 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21222 {
21223 if (w == XWINDOW (echo_area_window))
21224 {
21225 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21226 {
21227 *width = FRAME_CURSOR_WIDTH (f);
21228 return FRAME_DESIRED_CURSOR (f);
21229 }
21230 else
21231 return get_specified_cursor_type (b->cursor_type, width);
21232 }
21233
21234 *active_cursor = 0;
21235 non_selected = 1;
21236 }
21237
21238 /* Nonselected window or nonselected frame. */
21239 else if (w != XWINDOW (f->selected_window)
21240 #ifdef HAVE_WINDOW_SYSTEM
21241 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21242 #endif
21243 )
21244 {
21245 *active_cursor = 0;
21246
21247 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21248 return NO_CURSOR;
21249
21250 non_selected = 1;
21251 }
21252
21253 /* Never display a cursor in a window in which cursor-type is nil. */
21254 if (NILP (b->cursor_type))
21255 return NO_CURSOR;
21256
21257 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21258 if (non_selected)
21259 {
21260 alt_cursor = b->cursor_in_non_selected_windows;
21261 return get_specified_cursor_type (alt_cursor, width);
21262 }
21263
21264 /* Get the normal cursor type for this window. */
21265 if (EQ (b->cursor_type, Qt))
21266 {
21267 cursor_type = FRAME_DESIRED_CURSOR (f);
21268 *width = FRAME_CURSOR_WIDTH (f);
21269 }
21270 else
21271 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21272
21273 /* Use normal cursor if not blinked off. */
21274 if (!w->cursor_off_p)
21275 {
21276 #ifdef HAVE_WINDOW_SYSTEM
21277 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21278 {
21279 if (cursor_type == FILLED_BOX_CURSOR)
21280 {
21281 /* Using a block cursor on large images can be very annoying.
21282 So use a hollow cursor for "large" images.
21283 If image is not transparent (no mask), also use hollow cursor. */
21284 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21285 if (img != NULL && IMAGEP (img->spec))
21286 {
21287 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21288 where N = size of default frame font size.
21289 This should cover most of the "tiny" icons people may use. */
21290 if (!img->mask
21291 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21292 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21293 cursor_type = HOLLOW_BOX_CURSOR;
21294 }
21295 }
21296 else if (cursor_type != NO_CURSOR)
21297 {
21298 /* Display current only supports BOX and HOLLOW cursors for images.
21299 So for now, unconditionally use a HOLLOW cursor when cursor is
21300 not a solid box cursor. */
21301 cursor_type = HOLLOW_BOX_CURSOR;
21302 }
21303 }
21304 #endif
21305 return cursor_type;
21306 }
21307
21308 /* Cursor is blinked off, so determine how to "toggle" it. */
21309
21310 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21311 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21312 return get_specified_cursor_type (XCDR (alt_cursor), width);
21313
21314 /* Then see if frame has specified a specific blink off cursor type. */
21315 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21316 {
21317 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21318 return FRAME_BLINK_OFF_CURSOR (f);
21319 }
21320
21321 #if 0
21322 /* Some people liked having a permanently visible blinking cursor,
21323 while others had very strong opinions against it. So it was
21324 decided to remove it. KFS 2003-09-03 */
21325
21326 /* Finally perform built-in cursor blinking:
21327 filled box <-> hollow box
21328 wide [h]bar <-> narrow [h]bar
21329 narrow [h]bar <-> no cursor
21330 other type <-> no cursor */
21331
21332 if (cursor_type == FILLED_BOX_CURSOR)
21333 return HOLLOW_BOX_CURSOR;
21334
21335 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21336 {
21337 *width = 1;
21338 return cursor_type;
21339 }
21340 #endif
21341
21342 return NO_CURSOR;
21343 }
21344
21345
21346 #ifdef HAVE_WINDOW_SYSTEM
21347
21348 /* Notice when the text cursor of window W has been completely
21349 overwritten by a drawing operation that outputs glyphs in AREA
21350 starting at X0 and ending at X1 in the line starting at Y0 and
21351 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21352 the rest of the line after X0 has been written. Y coordinates
21353 are window-relative. */
21354
21355 static void
21356 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21357 struct window *w;
21358 enum glyph_row_area area;
21359 int x0, y0, x1, y1;
21360 {
21361 int cx0, cx1, cy0, cy1;
21362 struct glyph_row *row;
21363
21364 if (!w->phys_cursor_on_p)
21365 return;
21366 if (area != TEXT_AREA)
21367 return;
21368
21369 if (w->phys_cursor.vpos < 0
21370 || w->phys_cursor.vpos >= w->current_matrix->nrows
21371 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21372 !(row->enabled_p && row->displays_text_p)))
21373 return;
21374
21375 if (row->cursor_in_fringe_p)
21376 {
21377 row->cursor_in_fringe_p = 0;
21378 draw_fringe_bitmap (w, row, 0);
21379 w->phys_cursor_on_p = 0;
21380 return;
21381 }
21382
21383 cx0 = w->phys_cursor.x;
21384 cx1 = cx0 + w->phys_cursor_width;
21385 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21386 return;
21387
21388 /* The cursor image will be completely removed from the
21389 screen if the output area intersects the cursor area in
21390 y-direction. When we draw in [y0 y1[, and some part of
21391 the cursor is at y < y0, that part must have been drawn
21392 before. When scrolling, the cursor is erased before
21393 actually scrolling, so we don't come here. When not
21394 scrolling, the rows above the old cursor row must have
21395 changed, and in this case these rows must have written
21396 over the cursor image.
21397
21398 Likewise if part of the cursor is below y1, with the
21399 exception of the cursor being in the first blank row at
21400 the buffer and window end because update_text_area
21401 doesn't draw that row. (Except when it does, but
21402 that's handled in update_text_area.) */
21403
21404 cy0 = w->phys_cursor.y;
21405 cy1 = cy0 + w->phys_cursor_height;
21406 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21407 return;
21408
21409 w->phys_cursor_on_p = 0;
21410 }
21411
21412 #endif /* HAVE_WINDOW_SYSTEM */
21413
21414 \f
21415 /************************************************************************
21416 Mouse Face
21417 ************************************************************************/
21418
21419 #ifdef HAVE_WINDOW_SYSTEM
21420
21421 /* EXPORT for RIF:
21422 Fix the display of area AREA of overlapping row ROW in window W
21423 with respect to the overlapping part OVERLAPS. */
21424
21425 void
21426 x_fix_overlapping_area (w, row, area, overlaps)
21427 struct window *w;
21428 struct glyph_row *row;
21429 enum glyph_row_area area;
21430 int overlaps;
21431 {
21432 int i, x;
21433
21434 BLOCK_INPUT;
21435
21436 x = 0;
21437 for (i = 0; i < row->used[area];)
21438 {
21439 if (row->glyphs[area][i].overlaps_vertically_p)
21440 {
21441 int start = i, start_x = x;
21442
21443 do
21444 {
21445 x += row->glyphs[area][i].pixel_width;
21446 ++i;
21447 }
21448 while (i < row->used[area]
21449 && row->glyphs[area][i].overlaps_vertically_p);
21450
21451 draw_glyphs (w, start_x, row, area,
21452 start, i,
21453 DRAW_NORMAL_TEXT, overlaps);
21454 }
21455 else
21456 {
21457 x += row->glyphs[area][i].pixel_width;
21458 ++i;
21459 }
21460 }
21461
21462 UNBLOCK_INPUT;
21463 }
21464
21465
21466 /* EXPORT:
21467 Draw the cursor glyph of window W in glyph row ROW. See the
21468 comment of draw_glyphs for the meaning of HL. */
21469
21470 void
21471 draw_phys_cursor_glyph (w, row, hl)
21472 struct window *w;
21473 struct glyph_row *row;
21474 enum draw_glyphs_face hl;
21475 {
21476 /* If cursor hpos is out of bounds, don't draw garbage. This can
21477 happen in mini-buffer windows when switching between echo area
21478 glyphs and mini-buffer. */
21479 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21480 {
21481 int on_p = w->phys_cursor_on_p;
21482 int x1;
21483 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21484 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21485 hl, 0);
21486 w->phys_cursor_on_p = on_p;
21487
21488 if (hl == DRAW_CURSOR)
21489 w->phys_cursor_width = x1 - w->phys_cursor.x;
21490 /* When we erase the cursor, and ROW is overlapped by other
21491 rows, make sure that these overlapping parts of other rows
21492 are redrawn. */
21493 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21494 {
21495 w->phys_cursor_width = x1 - w->phys_cursor.x;
21496
21497 if (row > w->current_matrix->rows
21498 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21499 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21500 OVERLAPS_ERASED_CURSOR);
21501
21502 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21503 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21504 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21505 OVERLAPS_ERASED_CURSOR);
21506 }
21507 }
21508 }
21509
21510
21511 /* EXPORT:
21512 Erase the image of a cursor of window W from the screen. */
21513
21514 void
21515 erase_phys_cursor (w)
21516 struct window *w;
21517 {
21518 struct frame *f = XFRAME (w->frame);
21519 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21520 int hpos = w->phys_cursor.hpos;
21521 int vpos = w->phys_cursor.vpos;
21522 int mouse_face_here_p = 0;
21523 struct glyph_matrix *active_glyphs = w->current_matrix;
21524 struct glyph_row *cursor_row;
21525 struct glyph *cursor_glyph;
21526 enum draw_glyphs_face hl;
21527
21528 /* No cursor displayed or row invalidated => nothing to do on the
21529 screen. */
21530 if (w->phys_cursor_type == NO_CURSOR)
21531 goto mark_cursor_off;
21532
21533 /* VPOS >= active_glyphs->nrows means that window has been resized.
21534 Don't bother to erase the cursor. */
21535 if (vpos >= active_glyphs->nrows)
21536 goto mark_cursor_off;
21537
21538 /* If row containing cursor is marked invalid, there is nothing we
21539 can do. */
21540 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21541 if (!cursor_row->enabled_p)
21542 goto mark_cursor_off;
21543
21544 /* If line spacing is > 0, old cursor may only be partially visible in
21545 window after split-window. So adjust visible height. */
21546 cursor_row->visible_height = min (cursor_row->visible_height,
21547 window_text_bottom_y (w) - cursor_row->y);
21548
21549 /* If row is completely invisible, don't attempt to delete a cursor which
21550 isn't there. This can happen if cursor is at top of a window, and
21551 we switch to a buffer with a header line in that window. */
21552 if (cursor_row->visible_height <= 0)
21553 goto mark_cursor_off;
21554
21555 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21556 if (cursor_row->cursor_in_fringe_p)
21557 {
21558 cursor_row->cursor_in_fringe_p = 0;
21559 draw_fringe_bitmap (w, cursor_row, 0);
21560 goto mark_cursor_off;
21561 }
21562
21563 /* This can happen when the new row is shorter than the old one.
21564 In this case, either draw_glyphs or clear_end_of_line
21565 should have cleared the cursor. Note that we wouldn't be
21566 able to erase the cursor in this case because we don't have a
21567 cursor glyph at hand. */
21568 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21569 goto mark_cursor_off;
21570
21571 /* If the cursor is in the mouse face area, redisplay that when
21572 we clear the cursor. */
21573 if (! NILP (dpyinfo->mouse_face_window)
21574 && w == XWINDOW (dpyinfo->mouse_face_window)
21575 && (vpos > dpyinfo->mouse_face_beg_row
21576 || (vpos == dpyinfo->mouse_face_beg_row
21577 && hpos >= dpyinfo->mouse_face_beg_col))
21578 && (vpos < dpyinfo->mouse_face_end_row
21579 || (vpos == dpyinfo->mouse_face_end_row
21580 && hpos < dpyinfo->mouse_face_end_col))
21581 /* Don't redraw the cursor's spot in mouse face if it is at the
21582 end of a line (on a newline). The cursor appears there, but
21583 mouse highlighting does not. */
21584 && cursor_row->used[TEXT_AREA] > hpos)
21585 mouse_face_here_p = 1;
21586
21587 /* Maybe clear the display under the cursor. */
21588 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21589 {
21590 int x, y, left_x;
21591 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21592 int width;
21593
21594 cursor_glyph = get_phys_cursor_glyph (w);
21595 if (cursor_glyph == NULL)
21596 goto mark_cursor_off;
21597
21598 width = cursor_glyph->pixel_width;
21599 left_x = window_box_left_offset (w, TEXT_AREA);
21600 x = w->phys_cursor.x;
21601 if (x < left_x)
21602 width -= left_x - x;
21603 width = min (width, window_box_width (w, TEXT_AREA) - x);
21604 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21605 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21606
21607 if (width > 0)
21608 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21609 }
21610
21611 /* Erase the cursor by redrawing the character underneath it. */
21612 if (mouse_face_here_p)
21613 hl = DRAW_MOUSE_FACE;
21614 else
21615 hl = DRAW_NORMAL_TEXT;
21616 draw_phys_cursor_glyph (w, cursor_row, hl);
21617
21618 mark_cursor_off:
21619 w->phys_cursor_on_p = 0;
21620 w->phys_cursor_type = NO_CURSOR;
21621 }
21622
21623
21624 /* EXPORT:
21625 Display or clear cursor of window W. If ON is zero, clear the
21626 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21627 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21628
21629 void
21630 display_and_set_cursor (w, on, hpos, vpos, x, y)
21631 struct window *w;
21632 int on, hpos, vpos, x, y;
21633 {
21634 struct frame *f = XFRAME (w->frame);
21635 int new_cursor_type;
21636 int new_cursor_width;
21637 int active_cursor;
21638 struct glyph_row *glyph_row;
21639 struct glyph *glyph;
21640
21641 /* This is pointless on invisible frames, and dangerous on garbaged
21642 windows and frames; in the latter case, the frame or window may
21643 be in the midst of changing its size, and x and y may be off the
21644 window. */
21645 if (! FRAME_VISIBLE_P (f)
21646 || FRAME_GARBAGED_P (f)
21647 || vpos >= w->current_matrix->nrows
21648 || hpos >= w->current_matrix->matrix_w)
21649 return;
21650
21651 /* If cursor is off and we want it off, return quickly. */
21652 if (!on && !w->phys_cursor_on_p)
21653 return;
21654
21655 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21656 /* If cursor row is not enabled, we don't really know where to
21657 display the cursor. */
21658 if (!glyph_row->enabled_p)
21659 {
21660 w->phys_cursor_on_p = 0;
21661 return;
21662 }
21663
21664 glyph = NULL;
21665 if (!glyph_row->exact_window_width_line_p
21666 || hpos < glyph_row->used[TEXT_AREA])
21667 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21668
21669 xassert (interrupt_input_blocked);
21670
21671 /* Set new_cursor_type to the cursor we want to be displayed. */
21672 new_cursor_type = get_window_cursor_type (w, glyph,
21673 &new_cursor_width, &active_cursor);
21674
21675 /* If cursor is currently being shown and we don't want it to be or
21676 it is in the wrong place, or the cursor type is not what we want,
21677 erase it. */
21678 if (w->phys_cursor_on_p
21679 && (!on
21680 || w->phys_cursor.x != x
21681 || w->phys_cursor.y != y
21682 || new_cursor_type != w->phys_cursor_type
21683 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21684 && new_cursor_width != w->phys_cursor_width)))
21685 erase_phys_cursor (w);
21686
21687 /* Don't check phys_cursor_on_p here because that flag is only set
21688 to zero in some cases where we know that the cursor has been
21689 completely erased, to avoid the extra work of erasing the cursor
21690 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21691 still not be visible, or it has only been partly erased. */
21692 if (on)
21693 {
21694 w->phys_cursor_ascent = glyph_row->ascent;
21695 w->phys_cursor_height = glyph_row->height;
21696
21697 /* Set phys_cursor_.* before x_draw_.* is called because some
21698 of them may need the information. */
21699 w->phys_cursor.x = x;
21700 w->phys_cursor.y = glyph_row->y;
21701 w->phys_cursor.hpos = hpos;
21702 w->phys_cursor.vpos = vpos;
21703 }
21704
21705 rif->draw_window_cursor (w, glyph_row, x, y,
21706 new_cursor_type, new_cursor_width,
21707 on, active_cursor);
21708 }
21709
21710
21711 /* Switch the display of W's cursor on or off, according to the value
21712 of ON. */
21713
21714 static void
21715 update_window_cursor (w, on)
21716 struct window *w;
21717 int on;
21718 {
21719 /* Don't update cursor in windows whose frame is in the process
21720 of being deleted. */
21721 if (w->current_matrix)
21722 {
21723 BLOCK_INPUT;
21724 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21725 w->phys_cursor.x, w->phys_cursor.y);
21726 UNBLOCK_INPUT;
21727 }
21728 }
21729
21730
21731 /* Call update_window_cursor with parameter ON_P on all leaf windows
21732 in the window tree rooted at W. */
21733
21734 static void
21735 update_cursor_in_window_tree (w, on_p)
21736 struct window *w;
21737 int on_p;
21738 {
21739 while (w)
21740 {
21741 if (!NILP (w->hchild))
21742 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21743 else if (!NILP (w->vchild))
21744 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21745 else
21746 update_window_cursor (w, on_p);
21747
21748 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21749 }
21750 }
21751
21752
21753 /* EXPORT:
21754 Display the cursor on window W, or clear it, according to ON_P.
21755 Don't change the cursor's position. */
21756
21757 void
21758 x_update_cursor (f, on_p)
21759 struct frame *f;
21760 int on_p;
21761 {
21762 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21763 }
21764
21765
21766 /* EXPORT:
21767 Clear the cursor of window W to background color, and mark the
21768 cursor as not shown. This is used when the text where the cursor
21769 is is about to be rewritten. */
21770
21771 void
21772 x_clear_cursor (w)
21773 struct window *w;
21774 {
21775 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21776 update_window_cursor (w, 0);
21777 }
21778
21779
21780 /* EXPORT:
21781 Display the active region described by mouse_face_* according to DRAW. */
21782
21783 void
21784 show_mouse_face (dpyinfo, draw)
21785 Display_Info *dpyinfo;
21786 enum draw_glyphs_face draw;
21787 {
21788 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21789 struct frame *f = XFRAME (WINDOW_FRAME (w));
21790
21791 if (/* If window is in the process of being destroyed, don't bother
21792 to do anything. */
21793 w->current_matrix != NULL
21794 /* Don't update mouse highlight if hidden */
21795 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21796 /* Recognize when we are called to operate on rows that don't exist
21797 anymore. This can happen when a window is split. */
21798 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21799 {
21800 int phys_cursor_on_p = w->phys_cursor_on_p;
21801 struct glyph_row *row, *first, *last;
21802
21803 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21804 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21805
21806 for (row = first; row <= last && row->enabled_p; ++row)
21807 {
21808 int start_hpos, end_hpos, start_x;
21809
21810 /* For all but the first row, the highlight starts at column 0. */
21811 if (row == first)
21812 {
21813 start_hpos = dpyinfo->mouse_face_beg_col;
21814 start_x = dpyinfo->mouse_face_beg_x;
21815 }
21816 else
21817 {
21818 start_hpos = 0;
21819 start_x = 0;
21820 }
21821
21822 if (row == last)
21823 end_hpos = dpyinfo->mouse_face_end_col;
21824 else
21825 {
21826 end_hpos = row->used[TEXT_AREA];
21827 if (draw == DRAW_NORMAL_TEXT)
21828 row->fill_line_p = 1; /* Clear to end of line */
21829 }
21830
21831 if (end_hpos > start_hpos)
21832 {
21833 draw_glyphs (w, start_x, row, TEXT_AREA,
21834 start_hpos, end_hpos,
21835 draw, 0);
21836
21837 row->mouse_face_p
21838 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21839 }
21840 }
21841
21842 /* When we've written over the cursor, arrange for it to
21843 be displayed again. */
21844 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21845 {
21846 BLOCK_INPUT;
21847 display_and_set_cursor (w, 1,
21848 w->phys_cursor.hpos, w->phys_cursor.vpos,
21849 w->phys_cursor.x, w->phys_cursor.y);
21850 UNBLOCK_INPUT;
21851 }
21852 }
21853
21854 /* Change the mouse cursor. */
21855 if (draw == DRAW_NORMAL_TEXT)
21856 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21857 else if (draw == DRAW_MOUSE_FACE)
21858 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21859 else
21860 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21861 }
21862
21863 /* EXPORT:
21864 Clear out the mouse-highlighted active region.
21865 Redraw it un-highlighted first. Value is non-zero if mouse
21866 face was actually drawn unhighlighted. */
21867
21868 int
21869 clear_mouse_face (dpyinfo)
21870 Display_Info *dpyinfo;
21871 {
21872 int cleared = 0;
21873
21874 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21875 {
21876 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21877 cleared = 1;
21878 }
21879
21880 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21881 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21882 dpyinfo->mouse_face_window = Qnil;
21883 dpyinfo->mouse_face_overlay = Qnil;
21884 return cleared;
21885 }
21886
21887
21888 /* EXPORT:
21889 Non-zero if physical cursor of window W is within mouse face. */
21890
21891 int
21892 cursor_in_mouse_face_p (w)
21893 struct window *w;
21894 {
21895 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21896 int in_mouse_face = 0;
21897
21898 if (WINDOWP (dpyinfo->mouse_face_window)
21899 && XWINDOW (dpyinfo->mouse_face_window) == w)
21900 {
21901 int hpos = w->phys_cursor.hpos;
21902 int vpos = w->phys_cursor.vpos;
21903
21904 if (vpos >= dpyinfo->mouse_face_beg_row
21905 && vpos <= dpyinfo->mouse_face_end_row
21906 && (vpos > dpyinfo->mouse_face_beg_row
21907 || hpos >= dpyinfo->mouse_face_beg_col)
21908 && (vpos < dpyinfo->mouse_face_end_row
21909 || hpos < dpyinfo->mouse_face_end_col
21910 || dpyinfo->mouse_face_past_end))
21911 in_mouse_face = 1;
21912 }
21913
21914 return in_mouse_face;
21915 }
21916
21917
21918
21919 \f
21920 /* Find the glyph matrix position of buffer position CHARPOS in window
21921 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21922 current glyphs must be up to date. If CHARPOS is above window
21923 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21924 of last line in W. In the row containing CHARPOS, stop before glyphs
21925 having STOP as object. */
21926
21927 #if 1 /* This is a version of fast_find_position that's more correct
21928 in the presence of hscrolling, for example. I didn't install
21929 it right away because the problem fixed is minor, it failed
21930 in 20.x as well, and I think it's too risky to install
21931 so near the release of 21.1. 2001-09-25 gerd. */
21932
21933 static int
21934 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21935 struct window *w;
21936 int charpos;
21937 int *hpos, *vpos, *x, *y;
21938 Lisp_Object stop;
21939 {
21940 struct glyph_row *row, *first;
21941 struct glyph *glyph, *end;
21942 int past_end = 0;
21943
21944 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21945 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21946 {
21947 *x = first->x;
21948 *y = first->y;
21949 *hpos = 0;
21950 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21951 return 1;
21952 }
21953
21954 row = row_containing_pos (w, charpos, first, NULL, 0);
21955 if (row == NULL)
21956 {
21957 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21958 past_end = 1;
21959 }
21960
21961 /* If whole rows or last part of a row came from a display overlay,
21962 row_containing_pos will skip over such rows because their end pos
21963 equals the start pos of the overlay or interval.
21964
21965 Move back if we have a STOP object and previous row's
21966 end glyph came from STOP. */
21967 if (!NILP (stop))
21968 {
21969 struct glyph_row *prev;
21970 while ((prev = row - 1, prev >= first)
21971 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21972 && prev->used[TEXT_AREA] > 0)
21973 {
21974 struct glyph *beg = prev->glyphs[TEXT_AREA];
21975 glyph = beg + prev->used[TEXT_AREA];
21976 while (--glyph >= beg
21977 && INTEGERP (glyph->object));
21978 if (glyph < beg
21979 || !EQ (stop, glyph->object))
21980 break;
21981 row = prev;
21982 }
21983 }
21984
21985 *x = row->x;
21986 *y = row->y;
21987 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21988
21989 glyph = row->glyphs[TEXT_AREA];
21990 end = glyph + row->used[TEXT_AREA];
21991
21992 /* Skip over glyphs not having an object at the start of the row.
21993 These are special glyphs like truncation marks on terminal
21994 frames. */
21995 if (row->displays_text_p)
21996 while (glyph < end
21997 && INTEGERP (glyph->object)
21998 && !EQ (stop, glyph->object)
21999 && glyph->charpos < 0)
22000 {
22001 *x += glyph->pixel_width;
22002 ++glyph;
22003 }
22004
22005 while (glyph < end
22006 && !INTEGERP (glyph->object)
22007 && !EQ (stop, glyph->object)
22008 && (!BUFFERP (glyph->object)
22009 || glyph->charpos < charpos))
22010 {
22011 *x += glyph->pixel_width;
22012 ++glyph;
22013 }
22014
22015 *hpos = glyph - row->glyphs[TEXT_AREA];
22016 return !past_end;
22017 }
22018
22019 #else /* not 1 */
22020
22021 static int
22022 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22023 struct window *w;
22024 int pos;
22025 int *hpos, *vpos, *x, *y;
22026 Lisp_Object stop;
22027 {
22028 int i;
22029 int lastcol;
22030 int maybe_next_line_p = 0;
22031 int line_start_position;
22032 int yb = window_text_bottom_y (w);
22033 struct glyph_row *row, *best_row;
22034 int row_vpos, best_row_vpos;
22035 int current_x;
22036
22037 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22038 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22039
22040 while (row->y < yb)
22041 {
22042 if (row->used[TEXT_AREA])
22043 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22044 else
22045 line_start_position = 0;
22046
22047 if (line_start_position > pos)
22048 break;
22049 /* If the position sought is the end of the buffer,
22050 don't include the blank lines at the bottom of the window. */
22051 else if (line_start_position == pos
22052 && pos == BUF_ZV (XBUFFER (w->buffer)))
22053 {
22054 maybe_next_line_p = 1;
22055 break;
22056 }
22057 else if (line_start_position > 0)
22058 {
22059 best_row = row;
22060 best_row_vpos = row_vpos;
22061 }
22062
22063 if (row->y + row->height >= yb)
22064 break;
22065
22066 ++row;
22067 ++row_vpos;
22068 }
22069
22070 /* Find the right column within BEST_ROW. */
22071 lastcol = 0;
22072 current_x = best_row->x;
22073 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22074 {
22075 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22076 int charpos = glyph->charpos;
22077
22078 if (BUFFERP (glyph->object))
22079 {
22080 if (charpos == pos)
22081 {
22082 *hpos = i;
22083 *vpos = best_row_vpos;
22084 *x = current_x;
22085 *y = best_row->y;
22086 return 1;
22087 }
22088 else if (charpos > pos)
22089 break;
22090 }
22091 else if (EQ (glyph->object, stop))
22092 break;
22093
22094 if (charpos > 0)
22095 lastcol = i;
22096 current_x += glyph->pixel_width;
22097 }
22098
22099 /* If we're looking for the end of the buffer,
22100 and we didn't find it in the line we scanned,
22101 use the start of the following line. */
22102 if (maybe_next_line_p)
22103 {
22104 ++best_row;
22105 ++best_row_vpos;
22106 lastcol = 0;
22107 current_x = best_row->x;
22108 }
22109
22110 *vpos = best_row_vpos;
22111 *hpos = lastcol + 1;
22112 *x = current_x;
22113 *y = best_row->y;
22114 return 0;
22115 }
22116
22117 #endif /* not 1 */
22118
22119
22120 /* Find the position of the glyph for position POS in OBJECT in
22121 window W's current matrix, and return in *X, *Y the pixel
22122 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22123
22124 RIGHT_P non-zero means return the position of the right edge of the
22125 glyph, RIGHT_P zero means return the left edge position.
22126
22127 If no glyph for POS exists in the matrix, return the position of
22128 the glyph with the next smaller position that is in the matrix, if
22129 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22130 exists in the matrix, return the position of the glyph with the
22131 next larger position in OBJECT.
22132
22133 Value is non-zero if a glyph was found. */
22134
22135 static int
22136 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22137 struct window *w;
22138 int pos;
22139 Lisp_Object object;
22140 int *hpos, *vpos, *x, *y;
22141 int right_p;
22142 {
22143 int yb = window_text_bottom_y (w);
22144 struct glyph_row *r;
22145 struct glyph *best_glyph = NULL;
22146 struct glyph_row *best_row = NULL;
22147 int best_x = 0;
22148
22149 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22150 r->enabled_p && r->y < yb;
22151 ++r)
22152 {
22153 struct glyph *g = r->glyphs[TEXT_AREA];
22154 struct glyph *e = g + r->used[TEXT_AREA];
22155 int gx;
22156
22157 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22158 if (EQ (g->object, object))
22159 {
22160 if (g->charpos == pos)
22161 {
22162 best_glyph = g;
22163 best_x = gx;
22164 best_row = r;
22165 goto found;
22166 }
22167 else if (best_glyph == NULL
22168 || ((abs (g->charpos - pos)
22169 < abs (best_glyph->charpos - pos))
22170 && (right_p
22171 ? g->charpos < pos
22172 : g->charpos > pos)))
22173 {
22174 best_glyph = g;
22175 best_x = gx;
22176 best_row = r;
22177 }
22178 }
22179 }
22180
22181 found:
22182
22183 if (best_glyph)
22184 {
22185 *x = best_x;
22186 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22187
22188 if (right_p)
22189 {
22190 *x += best_glyph->pixel_width;
22191 ++*hpos;
22192 }
22193
22194 *y = best_row->y;
22195 *vpos = best_row - w->current_matrix->rows;
22196 }
22197
22198 return best_glyph != NULL;
22199 }
22200
22201
22202 /* See if position X, Y is within a hot-spot of an image. */
22203
22204 static int
22205 on_hot_spot_p (hot_spot, x, y)
22206 Lisp_Object hot_spot;
22207 int x, y;
22208 {
22209 if (!CONSP (hot_spot))
22210 return 0;
22211
22212 if (EQ (XCAR (hot_spot), Qrect))
22213 {
22214 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22215 Lisp_Object rect = XCDR (hot_spot);
22216 Lisp_Object tem;
22217 if (!CONSP (rect))
22218 return 0;
22219 if (!CONSP (XCAR (rect)))
22220 return 0;
22221 if (!CONSP (XCDR (rect)))
22222 return 0;
22223 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22224 return 0;
22225 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22226 return 0;
22227 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22228 return 0;
22229 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22230 return 0;
22231 return 1;
22232 }
22233 else if (EQ (XCAR (hot_spot), Qcircle))
22234 {
22235 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22236 Lisp_Object circ = XCDR (hot_spot);
22237 Lisp_Object lr, lx0, ly0;
22238 if (CONSP (circ)
22239 && CONSP (XCAR (circ))
22240 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22241 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22242 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22243 {
22244 double r = XFLOATINT (lr);
22245 double dx = XINT (lx0) - x;
22246 double dy = XINT (ly0) - y;
22247 return (dx * dx + dy * dy <= r * r);
22248 }
22249 }
22250 else if (EQ (XCAR (hot_spot), Qpoly))
22251 {
22252 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22253 if (VECTORP (XCDR (hot_spot)))
22254 {
22255 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22256 Lisp_Object *poly = v->contents;
22257 int n = v->size;
22258 int i;
22259 int inside = 0;
22260 Lisp_Object lx, ly;
22261 int x0, y0;
22262
22263 /* Need an even number of coordinates, and at least 3 edges. */
22264 if (n < 6 || n & 1)
22265 return 0;
22266
22267 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22268 If count is odd, we are inside polygon. Pixels on edges
22269 may or may not be included depending on actual geometry of the
22270 polygon. */
22271 if ((lx = poly[n-2], !INTEGERP (lx))
22272 || (ly = poly[n-1], !INTEGERP (lx)))
22273 return 0;
22274 x0 = XINT (lx), y0 = XINT (ly);
22275 for (i = 0; i < n; i += 2)
22276 {
22277 int x1 = x0, y1 = y0;
22278 if ((lx = poly[i], !INTEGERP (lx))
22279 || (ly = poly[i+1], !INTEGERP (ly)))
22280 return 0;
22281 x0 = XINT (lx), y0 = XINT (ly);
22282
22283 /* Does this segment cross the X line? */
22284 if (x0 >= x)
22285 {
22286 if (x1 >= x)
22287 continue;
22288 }
22289 else if (x1 < x)
22290 continue;
22291 if (y > y0 && y > y1)
22292 continue;
22293 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22294 inside = !inside;
22295 }
22296 return inside;
22297 }
22298 }
22299 /* If we don't understand the format, pretend we're not in the hot-spot. */
22300 return 0;
22301 }
22302
22303 Lisp_Object
22304 find_hot_spot (map, x, y)
22305 Lisp_Object map;
22306 int x, y;
22307 {
22308 while (CONSP (map))
22309 {
22310 if (CONSP (XCAR (map))
22311 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22312 return XCAR (map);
22313 map = XCDR (map);
22314 }
22315
22316 return Qnil;
22317 }
22318
22319 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22320 3, 3, 0,
22321 doc: /* Lookup in image map MAP coordinates X and Y.
22322 An image map is an alist where each element has the format (AREA ID PLIST).
22323 An AREA is specified as either a rectangle, a circle, or a polygon:
22324 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22325 pixel coordinates of the upper left and bottom right corners.
22326 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22327 and the radius of the circle; r may be a float or integer.
22328 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22329 vector describes one corner in the polygon.
22330 Returns the alist element for the first matching AREA in MAP. */)
22331 (map, x, y)
22332 Lisp_Object map;
22333 Lisp_Object x, y;
22334 {
22335 if (NILP (map))
22336 return Qnil;
22337
22338 CHECK_NUMBER (x);
22339 CHECK_NUMBER (y);
22340
22341 return find_hot_spot (map, XINT (x), XINT (y));
22342 }
22343
22344
22345 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22346 static void
22347 define_frame_cursor1 (f, cursor, pointer)
22348 struct frame *f;
22349 Cursor cursor;
22350 Lisp_Object pointer;
22351 {
22352 /* Do not change cursor shape while dragging mouse. */
22353 if (!NILP (do_mouse_tracking))
22354 return;
22355
22356 if (!NILP (pointer))
22357 {
22358 if (EQ (pointer, Qarrow))
22359 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22360 else if (EQ (pointer, Qhand))
22361 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22362 else if (EQ (pointer, Qtext))
22363 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22364 else if (EQ (pointer, intern ("hdrag")))
22365 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22366 #ifdef HAVE_X_WINDOWS
22367 else if (EQ (pointer, intern ("vdrag")))
22368 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22369 #endif
22370 else if (EQ (pointer, intern ("hourglass")))
22371 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22372 else if (EQ (pointer, Qmodeline))
22373 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22374 else
22375 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22376 }
22377
22378 if (cursor != No_Cursor)
22379 rif->define_frame_cursor (f, cursor);
22380 }
22381
22382 /* Take proper action when mouse has moved to the mode or header line
22383 or marginal area AREA of window W, x-position X and y-position Y.
22384 X is relative to the start of the text display area of W, so the
22385 width of bitmap areas and scroll bars must be subtracted to get a
22386 position relative to the start of the mode line. */
22387
22388 static void
22389 note_mode_line_or_margin_highlight (window, x, y, area)
22390 Lisp_Object window;
22391 int x, y;
22392 enum window_part area;
22393 {
22394 struct window *w = XWINDOW (window);
22395 struct frame *f = XFRAME (w->frame);
22396 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22397 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22398 Lisp_Object pointer = Qnil;
22399 int charpos, dx, dy, width, height;
22400 Lisp_Object string, object = Qnil;
22401 Lisp_Object pos, help;
22402
22403 Lisp_Object mouse_face;
22404 int original_x_pixel = x;
22405 struct glyph * glyph = NULL;
22406 struct glyph_row *row;
22407
22408 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22409 {
22410 int x0;
22411 struct glyph *end;
22412
22413 string = mode_line_string (w, area, &x, &y, &charpos,
22414 &object, &dx, &dy, &width, &height);
22415
22416 row = (area == ON_MODE_LINE
22417 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22418 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22419
22420 /* Find glyph */
22421 if (row->mode_line_p && row->enabled_p)
22422 {
22423 glyph = row->glyphs[TEXT_AREA];
22424 end = glyph + row->used[TEXT_AREA];
22425
22426 for (x0 = original_x_pixel;
22427 glyph < end && x0 >= glyph->pixel_width;
22428 ++glyph)
22429 x0 -= glyph->pixel_width;
22430
22431 if (glyph >= end)
22432 glyph = NULL;
22433 }
22434 }
22435 else
22436 {
22437 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22438 string = marginal_area_string (w, area, &x, &y, &charpos,
22439 &object, &dx, &dy, &width, &height);
22440 }
22441
22442 help = Qnil;
22443
22444 if (IMAGEP (object))
22445 {
22446 Lisp_Object image_map, hotspot;
22447 if ((image_map = Fplist_get (XCDR (object), QCmap),
22448 !NILP (image_map))
22449 && (hotspot = find_hot_spot (image_map, dx, dy),
22450 CONSP (hotspot))
22451 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22452 {
22453 Lisp_Object area_id, plist;
22454
22455 area_id = XCAR (hotspot);
22456 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22457 If so, we could look for mouse-enter, mouse-leave
22458 properties in PLIST (and do something...). */
22459 hotspot = XCDR (hotspot);
22460 if (CONSP (hotspot)
22461 && (plist = XCAR (hotspot), CONSP (plist)))
22462 {
22463 pointer = Fplist_get (plist, Qpointer);
22464 if (NILP (pointer))
22465 pointer = Qhand;
22466 help = Fplist_get (plist, Qhelp_echo);
22467 if (!NILP (help))
22468 {
22469 help_echo_string = help;
22470 /* Is this correct? ++kfs */
22471 XSETWINDOW (help_echo_window, w);
22472 help_echo_object = w->buffer;
22473 help_echo_pos = charpos;
22474 }
22475 }
22476 }
22477 if (NILP (pointer))
22478 pointer = Fplist_get (XCDR (object), QCpointer);
22479 }
22480
22481 if (STRINGP (string))
22482 {
22483 pos = make_number (charpos);
22484 /* If we're on a string with `help-echo' text property, arrange
22485 for the help to be displayed. This is done by setting the
22486 global variable help_echo_string to the help string. */
22487 if (NILP (help))
22488 {
22489 help = Fget_text_property (pos, Qhelp_echo, string);
22490 if (!NILP (help))
22491 {
22492 help_echo_string = help;
22493 XSETWINDOW (help_echo_window, w);
22494 help_echo_object = string;
22495 help_echo_pos = charpos;
22496 }
22497 }
22498
22499 if (NILP (pointer))
22500 pointer = Fget_text_property (pos, Qpointer, string);
22501
22502 /* Change the mouse pointer according to what is under X/Y. */
22503 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22504 {
22505 Lisp_Object map;
22506 map = Fget_text_property (pos, Qlocal_map, string);
22507 if (!KEYMAPP (map))
22508 map = Fget_text_property (pos, Qkeymap, string);
22509 if (!KEYMAPP (map))
22510 cursor = dpyinfo->vertical_scroll_bar_cursor;
22511 }
22512
22513 /* Change the mouse face according to what is under X/Y. */
22514 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22515 if (!NILP (mouse_face)
22516 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22517 && glyph)
22518 {
22519 Lisp_Object b, e;
22520
22521 struct glyph * tmp_glyph;
22522
22523 int gpos;
22524 int gseq_length;
22525 int total_pixel_width;
22526 int ignore;
22527
22528 int vpos, hpos;
22529
22530 b = Fprevious_single_property_change (make_number (charpos + 1),
22531 Qmouse_face, string, Qnil);
22532 if (NILP (b))
22533 b = make_number (0);
22534
22535 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22536 if (NILP (e))
22537 e = make_number (SCHARS (string));
22538
22539 /* Calculate the position(glyph position: GPOS) of GLYPH in
22540 displayed string. GPOS is different from CHARPOS.
22541
22542 CHARPOS is the position of glyph in internal string
22543 object. A mode line string format has structures which
22544 is converted to a flatten by emacs lisp interpreter.
22545 The internal string is an element of the structures.
22546 The displayed string is the flatten string. */
22547 for (tmp_glyph = glyph - 1, gpos = 0;
22548 tmp_glyph->charpos >= XINT (b);
22549 tmp_glyph--, gpos++)
22550 {
22551 if (!EQ (tmp_glyph->object, glyph->object))
22552 break;
22553 }
22554
22555 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22556 displayed string holding GLYPH.
22557
22558 GSEQ_LENGTH is different from SCHARS (STRING).
22559 SCHARS (STRING) returns the length of the internal string. */
22560 for (tmp_glyph = glyph, gseq_length = gpos;
22561 tmp_glyph->charpos < XINT (e);
22562 tmp_glyph++, gseq_length++)
22563 {
22564 if (!EQ (tmp_glyph->object, glyph->object))
22565 break;
22566 }
22567
22568 total_pixel_width = 0;
22569 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22570 total_pixel_width += tmp_glyph->pixel_width;
22571
22572 /* Pre calculation of re-rendering position */
22573 vpos = (x - gpos);
22574 hpos = (area == ON_MODE_LINE
22575 ? (w->current_matrix)->nrows - 1
22576 : 0);
22577
22578 /* If the re-rendering position is included in the last
22579 re-rendering area, we should do nothing. */
22580 if ( EQ (window, dpyinfo->mouse_face_window)
22581 && dpyinfo->mouse_face_beg_col <= vpos
22582 && vpos < dpyinfo->mouse_face_end_col
22583 && dpyinfo->mouse_face_beg_row == hpos )
22584 return;
22585
22586 if (clear_mouse_face (dpyinfo))
22587 cursor = No_Cursor;
22588
22589 dpyinfo->mouse_face_beg_col = vpos;
22590 dpyinfo->mouse_face_beg_row = hpos;
22591
22592 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22593 dpyinfo->mouse_face_beg_y = 0;
22594
22595 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22596 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22597
22598 dpyinfo->mouse_face_end_x = 0;
22599 dpyinfo->mouse_face_end_y = 0;
22600
22601 dpyinfo->mouse_face_past_end = 0;
22602 dpyinfo->mouse_face_window = window;
22603
22604 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22605 charpos,
22606 0, 0, 0, &ignore,
22607 glyph->face_id, 1);
22608 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22609
22610 if (NILP (pointer))
22611 pointer = Qhand;
22612 }
22613 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22614 clear_mouse_face (dpyinfo);
22615 }
22616 define_frame_cursor1 (f, cursor, pointer);
22617 }
22618
22619
22620 /* EXPORT:
22621 Take proper action when the mouse has moved to position X, Y on
22622 frame F as regards highlighting characters that have mouse-face
22623 properties. Also de-highlighting chars where the mouse was before.
22624 X and Y can be negative or out of range. */
22625
22626 void
22627 note_mouse_highlight (f, x, y)
22628 struct frame *f;
22629 int x, y;
22630 {
22631 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22632 enum window_part part;
22633 Lisp_Object window;
22634 struct window *w;
22635 Cursor cursor = No_Cursor;
22636 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22637 struct buffer *b;
22638
22639 /* When a menu is active, don't highlight because this looks odd. */
22640 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22641 if (popup_activated ())
22642 return;
22643 #endif
22644
22645 if (NILP (Vmouse_highlight)
22646 || !f->glyphs_initialized_p)
22647 return;
22648
22649 dpyinfo->mouse_face_mouse_x = x;
22650 dpyinfo->mouse_face_mouse_y = y;
22651 dpyinfo->mouse_face_mouse_frame = f;
22652
22653 if (dpyinfo->mouse_face_defer)
22654 return;
22655
22656 if (gc_in_progress)
22657 {
22658 dpyinfo->mouse_face_deferred_gc = 1;
22659 return;
22660 }
22661
22662 /* Which window is that in? */
22663 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22664
22665 /* If we were displaying active text in another window, clear that.
22666 Also clear if we move out of text area in same window. */
22667 if (! EQ (window, dpyinfo->mouse_face_window)
22668 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22669 && !NILP (dpyinfo->mouse_face_window)))
22670 clear_mouse_face (dpyinfo);
22671
22672 /* Not on a window -> return. */
22673 if (!WINDOWP (window))
22674 return;
22675
22676 /* Reset help_echo_string. It will get recomputed below. */
22677 help_echo_string = Qnil;
22678
22679 /* Convert to window-relative pixel coordinates. */
22680 w = XWINDOW (window);
22681 frame_to_window_pixel_xy (w, &x, &y);
22682
22683 /* Handle tool-bar window differently since it doesn't display a
22684 buffer. */
22685 if (EQ (window, f->tool_bar_window))
22686 {
22687 note_tool_bar_highlight (f, x, y);
22688 return;
22689 }
22690
22691 /* Mouse is on the mode, header line or margin? */
22692 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22693 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22694 {
22695 note_mode_line_or_margin_highlight (window, x, y, part);
22696 return;
22697 }
22698
22699 if (part == ON_VERTICAL_BORDER)
22700 {
22701 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22702 help_echo_string = build_string ("drag-mouse-1: resize");
22703 }
22704 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22705 || part == ON_SCROLL_BAR)
22706 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22707 else
22708 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22709
22710 /* Are we in a window whose display is up to date?
22711 And verify the buffer's text has not changed. */
22712 b = XBUFFER (w->buffer);
22713 if (part == ON_TEXT
22714 && EQ (w->window_end_valid, w->buffer)
22715 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22716 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22717 {
22718 int hpos, vpos, pos, i, dx, dy, area;
22719 struct glyph *glyph;
22720 Lisp_Object object;
22721 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22722 Lisp_Object *overlay_vec = NULL;
22723 int noverlays;
22724 struct buffer *obuf;
22725 int obegv, ozv, same_region;
22726
22727 /* Find the glyph under X/Y. */
22728 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22729
22730 /* Look for :pointer property on image. */
22731 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22732 {
22733 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22734 if (img != NULL && IMAGEP (img->spec))
22735 {
22736 Lisp_Object image_map, hotspot;
22737 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22738 !NILP (image_map))
22739 && (hotspot = find_hot_spot (image_map,
22740 glyph->slice.x + dx,
22741 glyph->slice.y + dy),
22742 CONSP (hotspot))
22743 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22744 {
22745 Lisp_Object area_id, plist;
22746
22747 area_id = XCAR (hotspot);
22748 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22749 If so, we could look for mouse-enter, mouse-leave
22750 properties in PLIST (and do something...). */
22751 hotspot = XCDR (hotspot);
22752 if (CONSP (hotspot)
22753 && (plist = XCAR (hotspot), CONSP (plist)))
22754 {
22755 pointer = Fplist_get (plist, Qpointer);
22756 if (NILP (pointer))
22757 pointer = Qhand;
22758 help_echo_string = Fplist_get (plist, Qhelp_echo);
22759 if (!NILP (help_echo_string))
22760 {
22761 help_echo_window = window;
22762 help_echo_object = glyph->object;
22763 help_echo_pos = glyph->charpos;
22764 }
22765 }
22766 }
22767 if (NILP (pointer))
22768 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22769 }
22770 }
22771
22772 /* Clear mouse face if X/Y not over text. */
22773 if (glyph == NULL
22774 || area != TEXT_AREA
22775 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22776 {
22777 if (clear_mouse_face (dpyinfo))
22778 cursor = No_Cursor;
22779 if (NILP (pointer))
22780 {
22781 if (area != TEXT_AREA)
22782 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22783 else
22784 pointer = Vvoid_text_area_pointer;
22785 }
22786 goto set_cursor;
22787 }
22788
22789 pos = glyph->charpos;
22790 object = glyph->object;
22791 if (!STRINGP (object) && !BUFFERP (object))
22792 goto set_cursor;
22793
22794 /* If we get an out-of-range value, return now; avoid an error. */
22795 if (BUFFERP (object) && pos > BUF_Z (b))
22796 goto set_cursor;
22797
22798 /* Make the window's buffer temporarily current for
22799 overlays_at and compute_char_face. */
22800 obuf = current_buffer;
22801 current_buffer = b;
22802 obegv = BEGV;
22803 ozv = ZV;
22804 BEGV = BEG;
22805 ZV = Z;
22806
22807 /* Is this char mouse-active or does it have help-echo? */
22808 position = make_number (pos);
22809
22810 if (BUFFERP (object))
22811 {
22812 /* Put all the overlays we want in a vector in overlay_vec. */
22813 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22814 /* Sort overlays into increasing priority order. */
22815 noverlays = sort_overlays (overlay_vec, noverlays, w);
22816 }
22817 else
22818 noverlays = 0;
22819
22820 same_region = (EQ (window, dpyinfo->mouse_face_window)
22821 && vpos >= dpyinfo->mouse_face_beg_row
22822 && vpos <= dpyinfo->mouse_face_end_row
22823 && (vpos > dpyinfo->mouse_face_beg_row
22824 || hpos >= dpyinfo->mouse_face_beg_col)
22825 && (vpos < dpyinfo->mouse_face_end_row
22826 || hpos < dpyinfo->mouse_face_end_col
22827 || dpyinfo->mouse_face_past_end));
22828
22829 if (same_region)
22830 cursor = No_Cursor;
22831
22832 /* Check mouse-face highlighting. */
22833 if (! same_region
22834 /* If there exists an overlay with mouse-face overlapping
22835 the one we are currently highlighting, we have to
22836 check if we enter the overlapping overlay, and then
22837 highlight only that. */
22838 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22839 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22840 {
22841 /* Find the highest priority overlay that has a mouse-face
22842 property. */
22843 overlay = Qnil;
22844 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22845 {
22846 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22847 if (!NILP (mouse_face))
22848 overlay = overlay_vec[i];
22849 }
22850
22851 /* If we're actually highlighting the same overlay as
22852 before, there's no need to do that again. */
22853 if (!NILP (overlay)
22854 && EQ (overlay, dpyinfo->mouse_face_overlay))
22855 goto check_help_echo;
22856
22857 dpyinfo->mouse_face_overlay = overlay;
22858
22859 /* Clear the display of the old active region, if any. */
22860 if (clear_mouse_face (dpyinfo))
22861 cursor = No_Cursor;
22862
22863 /* If no overlay applies, get a text property. */
22864 if (NILP (overlay))
22865 mouse_face = Fget_text_property (position, Qmouse_face, object);
22866
22867 /* Handle the overlay case. */
22868 if (!NILP (overlay))
22869 {
22870 /* Find the range of text around this char that
22871 should be active. */
22872 Lisp_Object before, after;
22873 int ignore;
22874
22875 before = Foverlay_start (overlay);
22876 after = Foverlay_end (overlay);
22877 /* Record this as the current active region. */
22878 fast_find_position (w, XFASTINT (before),
22879 &dpyinfo->mouse_face_beg_col,
22880 &dpyinfo->mouse_face_beg_row,
22881 &dpyinfo->mouse_face_beg_x,
22882 &dpyinfo->mouse_face_beg_y, Qnil);
22883
22884 dpyinfo->mouse_face_past_end
22885 = !fast_find_position (w, XFASTINT (after),
22886 &dpyinfo->mouse_face_end_col,
22887 &dpyinfo->mouse_face_end_row,
22888 &dpyinfo->mouse_face_end_x,
22889 &dpyinfo->mouse_face_end_y, Qnil);
22890 dpyinfo->mouse_face_window = window;
22891
22892 dpyinfo->mouse_face_face_id
22893 = face_at_buffer_position (w, pos, 0, 0,
22894 &ignore, pos + 1,
22895 !dpyinfo->mouse_face_hidden);
22896
22897 /* Display it as active. */
22898 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22899 cursor = No_Cursor;
22900 }
22901 /* Handle the text property case. */
22902 else if (!NILP (mouse_face) && BUFFERP (object))
22903 {
22904 /* Find the range of text around this char that
22905 should be active. */
22906 Lisp_Object before, after, beginning, end;
22907 int ignore;
22908
22909 beginning = Fmarker_position (w->start);
22910 end = make_number (BUF_Z (XBUFFER (object))
22911 - XFASTINT (w->window_end_pos));
22912 before
22913 = Fprevious_single_property_change (make_number (pos + 1),
22914 Qmouse_face,
22915 object, beginning);
22916 after
22917 = Fnext_single_property_change (position, Qmouse_face,
22918 object, end);
22919
22920 /* Record this as the current active region. */
22921 fast_find_position (w, XFASTINT (before),
22922 &dpyinfo->mouse_face_beg_col,
22923 &dpyinfo->mouse_face_beg_row,
22924 &dpyinfo->mouse_face_beg_x,
22925 &dpyinfo->mouse_face_beg_y, Qnil);
22926 dpyinfo->mouse_face_past_end
22927 = !fast_find_position (w, XFASTINT (after),
22928 &dpyinfo->mouse_face_end_col,
22929 &dpyinfo->mouse_face_end_row,
22930 &dpyinfo->mouse_face_end_x,
22931 &dpyinfo->mouse_face_end_y, Qnil);
22932 dpyinfo->mouse_face_window = window;
22933
22934 if (BUFFERP (object))
22935 dpyinfo->mouse_face_face_id
22936 = face_at_buffer_position (w, pos, 0, 0,
22937 &ignore, pos + 1,
22938 !dpyinfo->mouse_face_hidden);
22939
22940 /* Display it as active. */
22941 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22942 cursor = No_Cursor;
22943 }
22944 else if (!NILP (mouse_face) && STRINGP (object))
22945 {
22946 Lisp_Object b, e;
22947 int ignore;
22948
22949 b = Fprevious_single_property_change (make_number (pos + 1),
22950 Qmouse_face,
22951 object, Qnil);
22952 e = Fnext_single_property_change (position, Qmouse_face,
22953 object, Qnil);
22954 if (NILP (b))
22955 b = make_number (0);
22956 if (NILP (e))
22957 e = make_number (SCHARS (object) - 1);
22958
22959 fast_find_string_pos (w, XINT (b), object,
22960 &dpyinfo->mouse_face_beg_col,
22961 &dpyinfo->mouse_face_beg_row,
22962 &dpyinfo->mouse_face_beg_x,
22963 &dpyinfo->mouse_face_beg_y, 0);
22964 fast_find_string_pos (w, XINT (e), object,
22965 &dpyinfo->mouse_face_end_col,
22966 &dpyinfo->mouse_face_end_row,
22967 &dpyinfo->mouse_face_end_x,
22968 &dpyinfo->mouse_face_end_y, 1);
22969 dpyinfo->mouse_face_past_end = 0;
22970 dpyinfo->mouse_face_window = window;
22971 dpyinfo->mouse_face_face_id
22972 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22973 glyph->face_id, 1);
22974 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22975 cursor = No_Cursor;
22976 }
22977 else if (STRINGP (object) && NILP (mouse_face))
22978 {
22979 /* A string which doesn't have mouse-face, but
22980 the text ``under'' it might have. */
22981 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22982 int start = MATRIX_ROW_START_CHARPOS (r);
22983
22984 pos = string_buffer_position (w, object, start);
22985 if (pos > 0)
22986 mouse_face = get_char_property_and_overlay (make_number (pos),
22987 Qmouse_face,
22988 w->buffer,
22989 &overlay);
22990 if (!NILP (mouse_face) && !NILP (overlay))
22991 {
22992 Lisp_Object before = Foverlay_start (overlay);
22993 Lisp_Object after = Foverlay_end (overlay);
22994 int ignore;
22995
22996 /* Note that we might not be able to find position
22997 BEFORE in the glyph matrix if the overlay is
22998 entirely covered by a `display' property. In
22999 this case, we overshoot. So let's stop in
23000 the glyph matrix before glyphs for OBJECT. */
23001 fast_find_position (w, XFASTINT (before),
23002 &dpyinfo->mouse_face_beg_col,
23003 &dpyinfo->mouse_face_beg_row,
23004 &dpyinfo->mouse_face_beg_x,
23005 &dpyinfo->mouse_face_beg_y,
23006 object);
23007
23008 dpyinfo->mouse_face_past_end
23009 = !fast_find_position (w, XFASTINT (after),
23010 &dpyinfo->mouse_face_end_col,
23011 &dpyinfo->mouse_face_end_row,
23012 &dpyinfo->mouse_face_end_x,
23013 &dpyinfo->mouse_face_end_y,
23014 Qnil);
23015 dpyinfo->mouse_face_window = window;
23016 dpyinfo->mouse_face_face_id
23017 = face_at_buffer_position (w, pos, 0, 0,
23018 &ignore, pos + 1,
23019 !dpyinfo->mouse_face_hidden);
23020
23021 /* Display it as active. */
23022 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23023 cursor = No_Cursor;
23024 }
23025 }
23026 }
23027
23028 check_help_echo:
23029
23030 /* Look for a `help-echo' property. */
23031 if (NILP (help_echo_string)) {
23032 Lisp_Object help, overlay;
23033
23034 /* Check overlays first. */
23035 help = overlay = Qnil;
23036 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23037 {
23038 overlay = overlay_vec[i];
23039 help = Foverlay_get (overlay, Qhelp_echo);
23040 }
23041
23042 if (!NILP (help))
23043 {
23044 help_echo_string = help;
23045 help_echo_window = window;
23046 help_echo_object = overlay;
23047 help_echo_pos = pos;
23048 }
23049 else
23050 {
23051 Lisp_Object object = glyph->object;
23052 int charpos = glyph->charpos;
23053
23054 /* Try text properties. */
23055 if (STRINGP (object)
23056 && charpos >= 0
23057 && charpos < SCHARS (object))
23058 {
23059 help = Fget_text_property (make_number (charpos),
23060 Qhelp_echo, object);
23061 if (NILP (help))
23062 {
23063 /* If the string itself doesn't specify a help-echo,
23064 see if the buffer text ``under'' it does. */
23065 struct glyph_row *r
23066 = MATRIX_ROW (w->current_matrix, vpos);
23067 int start = MATRIX_ROW_START_CHARPOS (r);
23068 int pos = string_buffer_position (w, object, start);
23069 if (pos > 0)
23070 {
23071 help = Fget_char_property (make_number (pos),
23072 Qhelp_echo, w->buffer);
23073 if (!NILP (help))
23074 {
23075 charpos = pos;
23076 object = w->buffer;
23077 }
23078 }
23079 }
23080 }
23081 else if (BUFFERP (object)
23082 && charpos >= BEGV
23083 && charpos < ZV)
23084 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23085 object);
23086
23087 if (!NILP (help))
23088 {
23089 help_echo_string = help;
23090 help_echo_window = window;
23091 help_echo_object = object;
23092 help_echo_pos = charpos;
23093 }
23094 }
23095 }
23096
23097 /* Look for a `pointer' property. */
23098 if (NILP (pointer))
23099 {
23100 /* Check overlays first. */
23101 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23102 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23103
23104 if (NILP (pointer))
23105 {
23106 Lisp_Object object = glyph->object;
23107 int charpos = glyph->charpos;
23108
23109 /* Try text properties. */
23110 if (STRINGP (object)
23111 && charpos >= 0
23112 && charpos < SCHARS (object))
23113 {
23114 pointer = Fget_text_property (make_number (charpos),
23115 Qpointer, object);
23116 if (NILP (pointer))
23117 {
23118 /* If the string itself doesn't specify a pointer,
23119 see if the buffer text ``under'' it does. */
23120 struct glyph_row *r
23121 = MATRIX_ROW (w->current_matrix, vpos);
23122 int start = MATRIX_ROW_START_CHARPOS (r);
23123 int pos = string_buffer_position (w, object, start);
23124 if (pos > 0)
23125 pointer = Fget_char_property (make_number (pos),
23126 Qpointer, w->buffer);
23127 }
23128 }
23129 else if (BUFFERP (object)
23130 && charpos >= BEGV
23131 && charpos < ZV)
23132 pointer = Fget_text_property (make_number (charpos),
23133 Qpointer, object);
23134 }
23135 }
23136
23137 BEGV = obegv;
23138 ZV = ozv;
23139 current_buffer = obuf;
23140 }
23141
23142 set_cursor:
23143
23144 define_frame_cursor1 (f, cursor, pointer);
23145 }
23146
23147
23148 /* EXPORT for RIF:
23149 Clear any mouse-face on window W. This function is part of the
23150 redisplay interface, and is called from try_window_id and similar
23151 functions to ensure the mouse-highlight is off. */
23152
23153 void
23154 x_clear_window_mouse_face (w)
23155 struct window *w;
23156 {
23157 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23158 Lisp_Object window;
23159
23160 BLOCK_INPUT;
23161 XSETWINDOW (window, w);
23162 if (EQ (window, dpyinfo->mouse_face_window))
23163 clear_mouse_face (dpyinfo);
23164 UNBLOCK_INPUT;
23165 }
23166
23167
23168 /* EXPORT:
23169 Just discard the mouse face information for frame F, if any.
23170 This is used when the size of F is changed. */
23171
23172 void
23173 cancel_mouse_face (f)
23174 struct frame *f;
23175 {
23176 Lisp_Object window;
23177 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23178
23179 window = dpyinfo->mouse_face_window;
23180 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23181 {
23182 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23183 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23184 dpyinfo->mouse_face_window = Qnil;
23185 }
23186 }
23187
23188
23189 #endif /* HAVE_WINDOW_SYSTEM */
23190
23191 \f
23192 /***********************************************************************
23193 Exposure Events
23194 ***********************************************************************/
23195
23196 #ifdef HAVE_WINDOW_SYSTEM
23197
23198 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23199 which intersects rectangle R. R is in window-relative coordinates. */
23200
23201 static void
23202 expose_area (w, row, r, area)
23203 struct window *w;
23204 struct glyph_row *row;
23205 XRectangle *r;
23206 enum glyph_row_area area;
23207 {
23208 struct glyph *first = row->glyphs[area];
23209 struct glyph *end = row->glyphs[area] + row->used[area];
23210 struct glyph *last;
23211 int first_x, start_x, x;
23212
23213 if (area == TEXT_AREA && row->fill_line_p)
23214 /* If row extends face to end of line write the whole line. */
23215 draw_glyphs (w, 0, row, area,
23216 0, row->used[area],
23217 DRAW_NORMAL_TEXT, 0);
23218 else
23219 {
23220 /* Set START_X to the window-relative start position for drawing glyphs of
23221 AREA. The first glyph of the text area can be partially visible.
23222 The first glyphs of other areas cannot. */
23223 start_x = window_box_left_offset (w, area);
23224 x = start_x;
23225 if (area == TEXT_AREA)
23226 x += row->x;
23227
23228 /* Find the first glyph that must be redrawn. */
23229 while (first < end
23230 && x + first->pixel_width < r->x)
23231 {
23232 x += first->pixel_width;
23233 ++first;
23234 }
23235
23236 /* Find the last one. */
23237 last = first;
23238 first_x = x;
23239 while (last < end
23240 && x < r->x + r->width)
23241 {
23242 x += last->pixel_width;
23243 ++last;
23244 }
23245
23246 /* Repaint. */
23247 if (last > first)
23248 draw_glyphs (w, first_x - start_x, row, area,
23249 first - row->glyphs[area], last - row->glyphs[area],
23250 DRAW_NORMAL_TEXT, 0);
23251 }
23252 }
23253
23254
23255 /* Redraw the parts of the glyph row ROW on window W intersecting
23256 rectangle R. R is in window-relative coordinates. Value is
23257 non-zero if mouse-face was overwritten. */
23258
23259 static int
23260 expose_line (w, row, r)
23261 struct window *w;
23262 struct glyph_row *row;
23263 XRectangle *r;
23264 {
23265 xassert (row->enabled_p);
23266
23267 if (row->mode_line_p || w->pseudo_window_p)
23268 draw_glyphs (w, 0, row, TEXT_AREA,
23269 0, row->used[TEXT_AREA],
23270 DRAW_NORMAL_TEXT, 0);
23271 else
23272 {
23273 if (row->used[LEFT_MARGIN_AREA])
23274 expose_area (w, row, r, LEFT_MARGIN_AREA);
23275 if (row->used[TEXT_AREA])
23276 expose_area (w, row, r, TEXT_AREA);
23277 if (row->used[RIGHT_MARGIN_AREA])
23278 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23279 draw_row_fringe_bitmaps (w, row);
23280 }
23281
23282 return row->mouse_face_p;
23283 }
23284
23285
23286 /* Redraw those parts of glyphs rows during expose event handling that
23287 overlap other rows. Redrawing of an exposed line writes over parts
23288 of lines overlapping that exposed line; this function fixes that.
23289
23290 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23291 row in W's current matrix that is exposed and overlaps other rows.
23292 LAST_OVERLAPPING_ROW is the last such row. */
23293
23294 static void
23295 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23296 struct window *w;
23297 struct glyph_row *first_overlapping_row;
23298 struct glyph_row *last_overlapping_row;
23299 {
23300 struct glyph_row *row;
23301
23302 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23303 if (row->overlapping_p)
23304 {
23305 xassert (row->enabled_p && !row->mode_line_p);
23306
23307 if (row->used[LEFT_MARGIN_AREA])
23308 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23309
23310 if (row->used[TEXT_AREA])
23311 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23312
23313 if (row->used[RIGHT_MARGIN_AREA])
23314 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23315 }
23316 }
23317
23318
23319 /* Return non-zero if W's cursor intersects rectangle R. */
23320
23321 static int
23322 phys_cursor_in_rect_p (w, r)
23323 struct window *w;
23324 XRectangle *r;
23325 {
23326 XRectangle cr, result;
23327 struct glyph *cursor_glyph;
23328
23329 cursor_glyph = get_phys_cursor_glyph (w);
23330 if (cursor_glyph)
23331 {
23332 /* r is relative to W's box, but w->phys_cursor.x is relative
23333 to left edge of W's TEXT area. Adjust it. */
23334 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23335 cr.y = w->phys_cursor.y;
23336 cr.width = cursor_glyph->pixel_width;
23337 cr.height = w->phys_cursor_height;
23338 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23339 I assume the effect is the same -- and this is portable. */
23340 return x_intersect_rectangles (&cr, r, &result);
23341 }
23342 else
23343 return 0;
23344 }
23345
23346
23347 /* EXPORT:
23348 Draw a vertical window border to the right of window W if W doesn't
23349 have vertical scroll bars. */
23350
23351 void
23352 x_draw_vertical_border (w)
23353 struct window *w;
23354 {
23355 /* We could do better, if we knew what type of scroll-bar the adjacent
23356 windows (on either side) have... But we don't :-(
23357 However, I think this works ok. ++KFS 2003-04-25 */
23358
23359 /* Redraw borders between horizontally adjacent windows. Don't
23360 do it for frames with vertical scroll bars because either the
23361 right scroll bar of a window, or the left scroll bar of its
23362 neighbor will suffice as a border. */
23363 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23364 return;
23365
23366 if (!WINDOW_RIGHTMOST_P (w)
23367 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23368 {
23369 int x0, x1, y0, y1;
23370
23371 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23372 y1 -= 1;
23373
23374 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23375 x1 -= 1;
23376
23377 rif->draw_vertical_window_border (w, x1, y0, y1);
23378 }
23379 else if (!WINDOW_LEFTMOST_P (w)
23380 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23381 {
23382 int x0, x1, y0, y1;
23383
23384 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23385 y1 -= 1;
23386
23387 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23388 x0 -= 1;
23389
23390 rif->draw_vertical_window_border (w, x0, y0, y1);
23391 }
23392 }
23393
23394
23395 /* Redraw the part of window W intersection rectangle FR. Pixel
23396 coordinates in FR are frame-relative. Call this function with
23397 input blocked. Value is non-zero if the exposure overwrites
23398 mouse-face. */
23399
23400 static int
23401 expose_window (w, fr)
23402 struct window *w;
23403 XRectangle *fr;
23404 {
23405 struct frame *f = XFRAME (w->frame);
23406 XRectangle wr, r;
23407 int mouse_face_overwritten_p = 0;
23408
23409 /* If window is not yet fully initialized, do nothing. This can
23410 happen when toolkit scroll bars are used and a window is split.
23411 Reconfiguring the scroll bar will generate an expose for a newly
23412 created window. */
23413 if (w->current_matrix == NULL)
23414 return 0;
23415
23416 /* When we're currently updating the window, display and current
23417 matrix usually don't agree. Arrange for a thorough display
23418 later. */
23419 if (w == updated_window)
23420 {
23421 SET_FRAME_GARBAGED (f);
23422 return 0;
23423 }
23424
23425 /* Frame-relative pixel rectangle of W. */
23426 wr.x = WINDOW_LEFT_EDGE_X (w);
23427 wr.y = WINDOW_TOP_EDGE_Y (w);
23428 wr.width = WINDOW_TOTAL_WIDTH (w);
23429 wr.height = WINDOW_TOTAL_HEIGHT (w);
23430
23431 if (x_intersect_rectangles (fr, &wr, &r))
23432 {
23433 int yb = window_text_bottom_y (w);
23434 struct glyph_row *row;
23435 int cursor_cleared_p;
23436 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23437
23438 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23439 r.x, r.y, r.width, r.height));
23440
23441 /* Convert to window coordinates. */
23442 r.x -= WINDOW_LEFT_EDGE_X (w);
23443 r.y -= WINDOW_TOP_EDGE_Y (w);
23444
23445 /* Turn off the cursor. */
23446 if (!w->pseudo_window_p
23447 && phys_cursor_in_rect_p (w, &r))
23448 {
23449 x_clear_cursor (w);
23450 cursor_cleared_p = 1;
23451 }
23452 else
23453 cursor_cleared_p = 0;
23454
23455 /* Update lines intersecting rectangle R. */
23456 first_overlapping_row = last_overlapping_row = NULL;
23457 for (row = w->current_matrix->rows;
23458 row->enabled_p;
23459 ++row)
23460 {
23461 int y0 = row->y;
23462 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23463
23464 if ((y0 >= r.y && y0 < r.y + r.height)
23465 || (y1 > r.y && y1 < r.y + r.height)
23466 || (r.y >= y0 && r.y < y1)
23467 || (r.y + r.height > y0 && r.y + r.height < y1))
23468 {
23469 /* A header line may be overlapping, but there is no need
23470 to fix overlapping areas for them. KFS 2005-02-12 */
23471 if (row->overlapping_p && !row->mode_line_p)
23472 {
23473 if (first_overlapping_row == NULL)
23474 first_overlapping_row = row;
23475 last_overlapping_row = row;
23476 }
23477
23478 if (expose_line (w, row, &r))
23479 mouse_face_overwritten_p = 1;
23480 }
23481
23482 if (y1 >= yb)
23483 break;
23484 }
23485
23486 /* Display the mode line if there is one. */
23487 if (WINDOW_WANTS_MODELINE_P (w)
23488 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23489 row->enabled_p)
23490 && row->y < r.y + r.height)
23491 {
23492 if (expose_line (w, row, &r))
23493 mouse_face_overwritten_p = 1;
23494 }
23495
23496 if (!w->pseudo_window_p)
23497 {
23498 /* Fix the display of overlapping rows. */
23499 if (first_overlapping_row)
23500 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23501
23502 /* Draw border between windows. */
23503 x_draw_vertical_border (w);
23504
23505 /* Turn the cursor on again. */
23506 if (cursor_cleared_p)
23507 update_window_cursor (w, 1);
23508 }
23509 }
23510
23511 return mouse_face_overwritten_p;
23512 }
23513
23514
23515
23516 /* Redraw (parts) of all windows in the window tree rooted at W that
23517 intersect R. R contains frame pixel coordinates. Value is
23518 non-zero if the exposure overwrites mouse-face. */
23519
23520 static int
23521 expose_window_tree (w, r)
23522 struct window *w;
23523 XRectangle *r;
23524 {
23525 struct frame *f = XFRAME (w->frame);
23526 int mouse_face_overwritten_p = 0;
23527
23528 while (w && !FRAME_GARBAGED_P (f))
23529 {
23530 if (!NILP (w->hchild))
23531 mouse_face_overwritten_p
23532 |= expose_window_tree (XWINDOW (w->hchild), r);
23533 else if (!NILP (w->vchild))
23534 mouse_face_overwritten_p
23535 |= expose_window_tree (XWINDOW (w->vchild), r);
23536 else
23537 mouse_face_overwritten_p |= expose_window (w, r);
23538
23539 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23540 }
23541
23542 return mouse_face_overwritten_p;
23543 }
23544
23545
23546 /* EXPORT:
23547 Redisplay an exposed area of frame F. X and Y are the upper-left
23548 corner of the exposed rectangle. W and H are width and height of
23549 the exposed area. All are pixel values. W or H zero means redraw
23550 the entire frame. */
23551
23552 void
23553 expose_frame (f, x, y, w, h)
23554 struct frame *f;
23555 int x, y, w, h;
23556 {
23557 XRectangle r;
23558 int mouse_face_overwritten_p = 0;
23559
23560 TRACE ((stderr, "expose_frame "));
23561
23562 /* No need to redraw if frame will be redrawn soon. */
23563 if (FRAME_GARBAGED_P (f))
23564 {
23565 TRACE ((stderr, " garbaged\n"));
23566 return;
23567 }
23568
23569 /* If basic faces haven't been realized yet, there is no point in
23570 trying to redraw anything. This can happen when we get an expose
23571 event while Emacs is starting, e.g. by moving another window. */
23572 if (FRAME_FACE_CACHE (f) == NULL
23573 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23574 {
23575 TRACE ((stderr, " no faces\n"));
23576 return;
23577 }
23578
23579 if (w == 0 || h == 0)
23580 {
23581 r.x = r.y = 0;
23582 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23583 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23584 }
23585 else
23586 {
23587 r.x = x;
23588 r.y = y;
23589 r.width = w;
23590 r.height = h;
23591 }
23592
23593 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23594 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23595
23596 if (WINDOWP (f->tool_bar_window))
23597 mouse_face_overwritten_p
23598 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23599
23600 #ifdef HAVE_X_WINDOWS
23601 #ifndef MSDOS
23602 #ifndef USE_X_TOOLKIT
23603 if (WINDOWP (f->menu_bar_window))
23604 mouse_face_overwritten_p
23605 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23606 #endif /* not USE_X_TOOLKIT */
23607 #endif
23608 #endif
23609
23610 /* Some window managers support a focus-follows-mouse style with
23611 delayed raising of frames. Imagine a partially obscured frame,
23612 and moving the mouse into partially obscured mouse-face on that
23613 frame. The visible part of the mouse-face will be highlighted,
23614 then the WM raises the obscured frame. With at least one WM, KDE
23615 2.1, Emacs is not getting any event for the raising of the frame
23616 (even tried with SubstructureRedirectMask), only Expose events.
23617 These expose events will draw text normally, i.e. not
23618 highlighted. Which means we must redo the highlight here.
23619 Subsume it under ``we love X''. --gerd 2001-08-15 */
23620 /* Included in Windows version because Windows most likely does not
23621 do the right thing if any third party tool offers
23622 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23623 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23624 {
23625 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23626 if (f == dpyinfo->mouse_face_mouse_frame)
23627 {
23628 int x = dpyinfo->mouse_face_mouse_x;
23629 int y = dpyinfo->mouse_face_mouse_y;
23630 clear_mouse_face (dpyinfo);
23631 note_mouse_highlight (f, x, y);
23632 }
23633 }
23634 }
23635
23636
23637 /* EXPORT:
23638 Determine the intersection of two rectangles R1 and R2. Return
23639 the intersection in *RESULT. Value is non-zero if RESULT is not
23640 empty. */
23641
23642 int
23643 x_intersect_rectangles (r1, r2, result)
23644 XRectangle *r1, *r2, *result;
23645 {
23646 XRectangle *left, *right;
23647 XRectangle *upper, *lower;
23648 int intersection_p = 0;
23649
23650 /* Rearrange so that R1 is the left-most rectangle. */
23651 if (r1->x < r2->x)
23652 left = r1, right = r2;
23653 else
23654 left = r2, right = r1;
23655
23656 /* X0 of the intersection is right.x0, if this is inside R1,
23657 otherwise there is no intersection. */
23658 if (right->x <= left->x + left->width)
23659 {
23660 result->x = right->x;
23661
23662 /* The right end of the intersection is the minimum of the
23663 the right ends of left and right. */
23664 result->width = (min (left->x + left->width, right->x + right->width)
23665 - result->x);
23666
23667 /* Same game for Y. */
23668 if (r1->y < r2->y)
23669 upper = r1, lower = r2;
23670 else
23671 upper = r2, lower = r1;
23672
23673 /* The upper end of the intersection is lower.y0, if this is inside
23674 of upper. Otherwise, there is no intersection. */
23675 if (lower->y <= upper->y + upper->height)
23676 {
23677 result->y = lower->y;
23678
23679 /* The lower end of the intersection is the minimum of the lower
23680 ends of upper and lower. */
23681 result->height = (min (lower->y + lower->height,
23682 upper->y + upper->height)
23683 - result->y);
23684 intersection_p = 1;
23685 }
23686 }
23687
23688 return intersection_p;
23689 }
23690
23691 #endif /* HAVE_WINDOW_SYSTEM */
23692
23693 \f
23694 /***********************************************************************
23695 Initialization
23696 ***********************************************************************/
23697
23698 void
23699 syms_of_xdisp ()
23700 {
23701 Vwith_echo_area_save_vector = Qnil;
23702 staticpro (&Vwith_echo_area_save_vector);
23703
23704 Vmessage_stack = Qnil;
23705 staticpro (&Vmessage_stack);
23706
23707 Qinhibit_redisplay = intern ("inhibit-redisplay");
23708 staticpro (&Qinhibit_redisplay);
23709
23710 message_dolog_marker1 = Fmake_marker ();
23711 staticpro (&message_dolog_marker1);
23712 message_dolog_marker2 = Fmake_marker ();
23713 staticpro (&message_dolog_marker2);
23714 message_dolog_marker3 = Fmake_marker ();
23715 staticpro (&message_dolog_marker3);
23716
23717 #if GLYPH_DEBUG
23718 defsubr (&Sdump_frame_glyph_matrix);
23719 defsubr (&Sdump_glyph_matrix);
23720 defsubr (&Sdump_glyph_row);
23721 defsubr (&Sdump_tool_bar_row);
23722 defsubr (&Strace_redisplay);
23723 defsubr (&Strace_to_stderr);
23724 #endif
23725 #ifdef HAVE_WINDOW_SYSTEM
23726 defsubr (&Stool_bar_lines_needed);
23727 defsubr (&Slookup_image_map);
23728 #endif
23729 defsubr (&Sformat_mode_line);
23730
23731 staticpro (&Qmenu_bar_update_hook);
23732 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23733
23734 staticpro (&Qoverriding_terminal_local_map);
23735 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23736
23737 staticpro (&Qoverriding_local_map);
23738 Qoverriding_local_map = intern ("overriding-local-map");
23739
23740 staticpro (&Qwindow_scroll_functions);
23741 Qwindow_scroll_functions = intern ("window-scroll-functions");
23742
23743 staticpro (&Qredisplay_end_trigger_functions);
23744 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23745
23746 staticpro (&Qinhibit_point_motion_hooks);
23747 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23748
23749 QCdata = intern (":data");
23750 staticpro (&QCdata);
23751 Qdisplay = intern ("display");
23752 staticpro (&Qdisplay);
23753 Qspace_width = intern ("space-width");
23754 staticpro (&Qspace_width);
23755 Qraise = intern ("raise");
23756 staticpro (&Qraise);
23757 Qslice = intern ("slice");
23758 staticpro (&Qslice);
23759 Qspace = intern ("space");
23760 staticpro (&Qspace);
23761 Qmargin = intern ("margin");
23762 staticpro (&Qmargin);
23763 Qpointer = intern ("pointer");
23764 staticpro (&Qpointer);
23765 Qleft_margin = intern ("left-margin");
23766 staticpro (&Qleft_margin);
23767 Qright_margin = intern ("right-margin");
23768 staticpro (&Qright_margin);
23769 Qcenter = intern ("center");
23770 staticpro (&Qcenter);
23771 Qline_height = intern ("line-height");
23772 staticpro (&Qline_height);
23773 QCalign_to = intern (":align-to");
23774 staticpro (&QCalign_to);
23775 QCrelative_width = intern (":relative-width");
23776 staticpro (&QCrelative_width);
23777 QCrelative_height = intern (":relative-height");
23778 staticpro (&QCrelative_height);
23779 QCeval = intern (":eval");
23780 staticpro (&QCeval);
23781 QCpropertize = intern (":propertize");
23782 staticpro (&QCpropertize);
23783 QCfile = intern (":file");
23784 staticpro (&QCfile);
23785 Qfontified = intern ("fontified");
23786 staticpro (&Qfontified);
23787 Qfontification_functions = intern ("fontification-functions");
23788 staticpro (&Qfontification_functions);
23789 Qtrailing_whitespace = intern ("trailing-whitespace");
23790 staticpro (&Qtrailing_whitespace);
23791 Qescape_glyph = intern ("escape-glyph");
23792 staticpro (&Qescape_glyph);
23793 Qnobreak_space = intern ("nobreak-space");
23794 staticpro (&Qnobreak_space);
23795 Qimage = intern ("image");
23796 staticpro (&Qimage);
23797 QCmap = intern (":map");
23798 staticpro (&QCmap);
23799 QCpointer = intern (":pointer");
23800 staticpro (&QCpointer);
23801 Qrect = intern ("rect");
23802 staticpro (&Qrect);
23803 Qcircle = intern ("circle");
23804 staticpro (&Qcircle);
23805 Qpoly = intern ("poly");
23806 staticpro (&Qpoly);
23807 Qmessage_truncate_lines = intern ("message-truncate-lines");
23808 staticpro (&Qmessage_truncate_lines);
23809 Qgrow_only = intern ("grow-only");
23810 staticpro (&Qgrow_only);
23811 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23812 staticpro (&Qinhibit_menubar_update);
23813 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23814 staticpro (&Qinhibit_eval_during_redisplay);
23815 Qposition = intern ("position");
23816 staticpro (&Qposition);
23817 Qbuffer_position = intern ("buffer-position");
23818 staticpro (&Qbuffer_position);
23819 Qobject = intern ("object");
23820 staticpro (&Qobject);
23821 Qbar = intern ("bar");
23822 staticpro (&Qbar);
23823 Qhbar = intern ("hbar");
23824 staticpro (&Qhbar);
23825 Qbox = intern ("box");
23826 staticpro (&Qbox);
23827 Qhollow = intern ("hollow");
23828 staticpro (&Qhollow);
23829 Qhand = intern ("hand");
23830 staticpro (&Qhand);
23831 Qarrow = intern ("arrow");
23832 staticpro (&Qarrow);
23833 Qtext = intern ("text");
23834 staticpro (&Qtext);
23835 Qrisky_local_variable = intern ("risky-local-variable");
23836 staticpro (&Qrisky_local_variable);
23837 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23838 staticpro (&Qinhibit_free_realized_faces);
23839
23840 list_of_error = Fcons (Fcons (intern ("error"),
23841 Fcons (intern ("void-variable"), Qnil)),
23842 Qnil);
23843 staticpro (&list_of_error);
23844
23845 Qlast_arrow_position = intern ("last-arrow-position");
23846 staticpro (&Qlast_arrow_position);
23847 Qlast_arrow_string = intern ("last-arrow-string");
23848 staticpro (&Qlast_arrow_string);
23849
23850 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23851 staticpro (&Qoverlay_arrow_string);
23852 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23853 staticpro (&Qoverlay_arrow_bitmap);
23854
23855 echo_buffer[0] = echo_buffer[1] = Qnil;
23856 staticpro (&echo_buffer[0]);
23857 staticpro (&echo_buffer[1]);
23858
23859 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23860 staticpro (&echo_area_buffer[0]);
23861 staticpro (&echo_area_buffer[1]);
23862
23863 Vmessages_buffer_name = build_string ("*Messages*");
23864 staticpro (&Vmessages_buffer_name);
23865
23866 mode_line_proptrans_alist = Qnil;
23867 staticpro (&mode_line_proptrans_alist);
23868 mode_line_string_list = Qnil;
23869 staticpro (&mode_line_string_list);
23870 mode_line_string_face = Qnil;
23871 staticpro (&mode_line_string_face);
23872 mode_line_string_face_prop = Qnil;
23873 staticpro (&mode_line_string_face_prop);
23874 Vmode_line_unwind_vector = Qnil;
23875 staticpro (&Vmode_line_unwind_vector);
23876
23877 help_echo_string = Qnil;
23878 staticpro (&help_echo_string);
23879 help_echo_object = Qnil;
23880 staticpro (&help_echo_object);
23881 help_echo_window = Qnil;
23882 staticpro (&help_echo_window);
23883 previous_help_echo_string = Qnil;
23884 staticpro (&previous_help_echo_string);
23885 help_echo_pos = -1;
23886
23887 #ifdef HAVE_WINDOW_SYSTEM
23888 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23889 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23890 For example, if a block cursor is over a tab, it will be drawn as
23891 wide as that tab on the display. */);
23892 x_stretch_cursor_p = 0;
23893 #endif
23894
23895 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23896 doc: /* *Non-nil means highlight trailing whitespace.
23897 The face used for trailing whitespace is `trailing-whitespace'. */);
23898 Vshow_trailing_whitespace = Qnil;
23899
23900 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23901 doc: /* *Control highlighting of nobreak space and soft hyphen.
23902 A value of t means highlight the character itself (for nobreak space,
23903 use face `nobreak-space').
23904 A value of nil means no highlighting.
23905 Other values mean display the escape glyph followed by an ordinary
23906 space or ordinary hyphen. */);
23907 Vnobreak_char_display = Qt;
23908
23909 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23910 doc: /* *The pointer shape to show in void text areas.
23911 A value of nil means to show the text pointer. Other options are `arrow',
23912 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23913 Vvoid_text_area_pointer = Qarrow;
23914
23915 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23916 doc: /* Non-nil means don't actually do any redisplay.
23917 This is used for internal purposes. */);
23918 Vinhibit_redisplay = Qnil;
23919
23920 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23921 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23922 Vglobal_mode_string = Qnil;
23923
23924 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23925 doc: /* Marker for where to display an arrow on top of the buffer text.
23926 This must be the beginning of a line in order to work.
23927 See also `overlay-arrow-string'. */);
23928 Voverlay_arrow_position = Qnil;
23929
23930 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23931 doc: /* String to display as an arrow in non-window frames.
23932 See also `overlay-arrow-position'. */);
23933 Voverlay_arrow_string = build_string ("=>");
23934
23935 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23936 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23937 The symbols on this list are examined during redisplay to determine
23938 where to display overlay arrows. */);
23939 Voverlay_arrow_variable_list
23940 = Fcons (intern ("overlay-arrow-position"), Qnil);
23941
23942 DEFVAR_INT ("scroll-step", &scroll_step,
23943 doc: /* *The number of lines to try scrolling a window by when point moves out.
23944 If that fails to bring point back on frame, point is centered instead.
23945 If this is zero, point is always centered after it moves off frame.
23946 If you want scrolling to always be a line at a time, you should set
23947 `scroll-conservatively' to a large value rather than set this to 1. */);
23948
23949 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23950 doc: /* *Scroll up to this many lines, to bring point back on screen.
23951 A value of zero means to scroll the text to center point vertically
23952 in the window. */);
23953 scroll_conservatively = 0;
23954
23955 DEFVAR_INT ("scroll-margin", &scroll_margin,
23956 doc: /* *Number of lines of margin at the top and bottom of a window.
23957 Recenter the window whenever point gets within this many lines
23958 of the top or bottom of the window. */);
23959 scroll_margin = 0;
23960
23961 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23962 doc: /* Pixels per inch value for non-window system displays.
23963 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23964 Vdisplay_pixels_per_inch = make_float (72.0);
23965
23966 #if GLYPH_DEBUG
23967 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23968 #endif
23969
23970 DEFVAR_BOOL ("truncate-partial-width-windows",
23971 &truncate_partial_width_windows,
23972 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23973 truncate_partial_width_windows = 1;
23974
23975 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23976 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23977 Any other value means to use the appropriate face, `mode-line',
23978 `header-line', or `menu' respectively. */);
23979 mode_line_inverse_video = 1;
23980
23981 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23982 doc: /* *Maximum buffer size for which line number should be displayed.
23983 If the buffer is bigger than this, the line number does not appear
23984 in the mode line. A value of nil means no limit. */);
23985 Vline_number_display_limit = Qnil;
23986
23987 DEFVAR_INT ("line-number-display-limit-width",
23988 &line_number_display_limit_width,
23989 doc: /* *Maximum line width (in characters) for line number display.
23990 If the average length of the lines near point is bigger than this, then the
23991 line number may be omitted from the mode line. */);
23992 line_number_display_limit_width = 200;
23993
23994 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23995 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23996 highlight_nonselected_windows = 0;
23997
23998 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23999 doc: /* Non-nil if more than one frame is visible on this display.
24000 Minibuffer-only frames don't count, but iconified frames do.
24001 This variable is not guaranteed to be accurate except while processing
24002 `frame-title-format' and `icon-title-format'. */);
24003
24004 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24005 doc: /* Template for displaying the title bar of visible frames.
24006 \(Assuming the window manager supports this feature.)
24007
24008 This variable has the same structure as `mode-line-format', except that
24009 the %c and %l constructs are ignored. It is used only on frames for
24010 which no explicit name has been set \(see `modify-frame-parameters'). */);
24011
24012 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24013 doc: /* Template for displaying the title bar of an iconified frame.
24014 \(Assuming the window manager supports this feature.)
24015 This variable has the same structure as `mode-line-format' (which see),
24016 and is used only on frames for which no explicit name has been set
24017 \(see `modify-frame-parameters'). */);
24018 Vicon_title_format
24019 = Vframe_title_format
24020 = Fcons (intern ("multiple-frames"),
24021 Fcons (build_string ("%b"),
24022 Fcons (Fcons (empty_string,
24023 Fcons (intern ("invocation-name"),
24024 Fcons (build_string ("@"),
24025 Fcons (intern ("system-name"),
24026 Qnil)))),
24027 Qnil)));
24028
24029 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24030 doc: /* Maximum number of lines to keep in the message log buffer.
24031 If nil, disable message logging. If t, log messages but don't truncate
24032 the buffer when it becomes large. */);
24033 Vmessage_log_max = make_number (50);
24034
24035 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24036 doc: /* Functions called before redisplay, if window sizes have changed.
24037 The value should be a list of functions that take one argument.
24038 Just before redisplay, for each frame, if any of its windows have changed
24039 size since the last redisplay, or have been split or deleted,
24040 all the functions in the list are called, with the frame as argument. */);
24041 Vwindow_size_change_functions = Qnil;
24042
24043 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24044 doc: /* List of functions to call before redisplaying a window with scrolling.
24045 Each function is called with two arguments, the window
24046 and its new display-start position. Note that the value of `window-end'
24047 is not valid when these functions are called. */);
24048 Vwindow_scroll_functions = Qnil;
24049
24050 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24051 doc: /* Functions called when redisplay of a window reaches the end trigger.
24052 Each function is called with two arguments, the window and the end trigger value.
24053 See `set-window-redisplay-end-trigger'. */);
24054 Vredisplay_end_trigger_functions = Qnil;
24055
24056 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24057 doc: /* *Non-nil means autoselect window with mouse pointer.
24058 If nil, do not autoselect windows.
24059 A positive number means delay autoselection by that many seconds: a
24060 window is autoselected only after the mouse has remained in that
24061 window for the duration of the delay.
24062 A negative number has a similar effect, but causes windows to be
24063 autoselected only after the mouse has stopped moving. \(Because of
24064 the way Emacs compares mouse events, you will occasionally wait twice
24065 that time before the window gets selected.\)
24066 Any other value means to autoselect window instantaneously when the
24067 mouse pointer enters it.
24068
24069 Autoselection selects the minibuffer only if it is active, and never
24070 unselects the minibuffer if it is active. */);
24071 Vmouse_autoselect_window = Qnil;
24072
24073 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
24074 doc: /* *Non-nil means automatically resize tool-bars.
24075 This increases a tool-bar's height if not all tool-bar items are visible.
24076 It decreases a tool-bar's height when it would display blank lines
24077 otherwise. */);
24078 auto_resize_tool_bars_p = 1;
24079
24080 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24081 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24082 auto_raise_tool_bar_buttons_p = 1;
24083
24084 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24085 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24086 make_cursor_line_fully_visible_p = 1;
24087
24088 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24089 doc: /* *Border below tool-bar in pixels.
24090 If an integer, use it as the height of the border.
24091 If it is one of `internal-border-width' or `border-width', use the
24092 value of the corresponding frame parameter.
24093 Otherwise, no border is added below the tool-bar. */);
24094 Vtool_bar_border = Qinternal_border_width;
24095
24096 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24097 doc: /* *Margin around tool-bar buttons in pixels.
24098 If an integer, use that for both horizontal and vertical margins.
24099 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24100 HORZ specifying the horizontal margin, and VERT specifying the
24101 vertical margin. */);
24102 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24103
24104 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24105 doc: /* *Relief thickness of tool-bar buttons. */);
24106 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24107
24108 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24109 doc: /* List of functions to call to fontify regions of text.
24110 Each function is called with one argument POS. Functions must
24111 fontify a region starting at POS in the current buffer, and give
24112 fontified regions the property `fontified'. */);
24113 Vfontification_functions = Qnil;
24114 Fmake_variable_buffer_local (Qfontification_functions);
24115
24116 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24117 &unibyte_display_via_language_environment,
24118 doc: /* *Non-nil means display unibyte text according to language environment.
24119 Specifically this means that unibyte non-ASCII characters
24120 are displayed by converting them to the equivalent multibyte characters
24121 according to the current language environment. As a result, they are
24122 displayed according to the current fontset. */);
24123 unibyte_display_via_language_environment = 0;
24124
24125 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24126 doc: /* *Maximum height for resizing mini-windows.
24127 If a float, it specifies a fraction of the mini-window frame's height.
24128 If an integer, it specifies a number of lines. */);
24129 Vmax_mini_window_height = make_float (0.25);
24130
24131 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24132 doc: /* *How to resize mini-windows.
24133 A value of nil means don't automatically resize mini-windows.
24134 A value of t means resize them to fit the text displayed in them.
24135 A value of `grow-only', the default, means let mini-windows grow
24136 only, until their display becomes empty, at which point the windows
24137 go back to their normal size. */);
24138 Vresize_mini_windows = Qgrow_only;
24139
24140 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24141 doc: /* Alist specifying how to blink the cursor off.
24142 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24143 `cursor-type' frame-parameter or variable equals ON-STATE,
24144 comparing using `equal', Emacs uses OFF-STATE to specify
24145 how to blink it off. ON-STATE and OFF-STATE are values for
24146 the `cursor-type' frame parameter.
24147
24148 If a frame's ON-STATE has no entry in this list,
24149 the frame's other specifications determine how to blink the cursor off. */);
24150 Vblink_cursor_alist = Qnil;
24151
24152 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24153 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24154 automatic_hscrolling_p = 1;
24155
24156 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24157 doc: /* *How many columns away from the window edge point is allowed to get
24158 before automatic hscrolling will horizontally scroll the window. */);
24159 hscroll_margin = 5;
24160
24161 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24162 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24163 When point is less than `hscroll-margin' columns from the window
24164 edge, automatic hscrolling will scroll the window by the amount of columns
24165 determined by this variable. If its value is a positive integer, scroll that
24166 many columns. If it's a positive floating-point number, it specifies the
24167 fraction of the window's width to scroll. If it's nil or zero, point will be
24168 centered horizontally after the scroll. Any other value, including negative
24169 numbers, are treated as if the value were zero.
24170
24171 Automatic hscrolling always moves point outside the scroll margin, so if
24172 point was more than scroll step columns inside the margin, the window will
24173 scroll more than the value given by the scroll step.
24174
24175 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24176 and `scroll-right' overrides this variable's effect. */);
24177 Vhscroll_step = make_number (0);
24178
24179 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24180 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24181 Bind this around calls to `message' to let it take effect. */);
24182 message_truncate_lines = 0;
24183
24184 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24185 doc: /* Normal hook run to update the menu bar definitions.
24186 Redisplay runs this hook before it redisplays the menu bar.
24187 This is used to update submenus such as Buffers,
24188 whose contents depend on various data. */);
24189 Vmenu_bar_update_hook = Qnil;
24190
24191 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24192 doc: /* Frame for which we are updating a menu.
24193 The enable predicate for a menu binding should check this variable. */);
24194 Vmenu_updating_frame = Qnil;
24195
24196 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24197 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24198 inhibit_menubar_update = 0;
24199
24200 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24201 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24202 inhibit_eval_during_redisplay = 0;
24203
24204 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24205 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24206 inhibit_free_realized_faces = 0;
24207
24208 #if GLYPH_DEBUG
24209 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24210 doc: /* Inhibit try_window_id display optimization. */);
24211 inhibit_try_window_id = 0;
24212
24213 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24214 doc: /* Inhibit try_window_reusing display optimization. */);
24215 inhibit_try_window_reusing = 0;
24216
24217 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24218 doc: /* Inhibit try_cursor_movement display optimization. */);
24219 inhibit_try_cursor_movement = 0;
24220 #endif /* GLYPH_DEBUG */
24221
24222 DEFVAR_INT ("overline-margin", &overline_margin,
24223 doc: /* *Space between overline and text, in pixels.
24224 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24225 margin to the caracter height. */);
24226 overline_margin = 2;
24227 }
24228
24229
24230 /* Initialize this module when Emacs starts. */
24231
24232 void
24233 init_xdisp ()
24234 {
24235 Lisp_Object root_window;
24236 struct window *mini_w;
24237
24238 current_header_line_height = current_mode_line_height = -1;
24239
24240 CHARPOS (this_line_start_pos) = 0;
24241
24242 mini_w = XWINDOW (minibuf_window);
24243 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24244
24245 if (!noninteractive)
24246 {
24247 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24248 int i;
24249
24250 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24251 set_window_height (root_window,
24252 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24253 0);
24254 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24255 set_window_height (minibuf_window, 1, 0);
24256
24257 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24258 mini_w->total_cols = make_number (FRAME_COLS (f));
24259
24260 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24261 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24262 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24263
24264 /* The default ellipsis glyphs `...'. */
24265 for (i = 0; i < 3; ++i)
24266 default_invis_vector[i] = make_number ('.');
24267 }
24268
24269 {
24270 /* Allocate the buffer for frame titles.
24271 Also used for `format-mode-line'. */
24272 int size = 100;
24273 mode_line_noprop_buf = (char *) xmalloc (size);
24274 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24275 mode_line_noprop_ptr = mode_line_noprop_buf;
24276 mode_line_target = MODE_LINE_DISPLAY;
24277 }
24278
24279 help_echo_showing_p = 0;
24280 }
24281
24282
24283 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24284 (do not change this comment) */