]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs--rel--22
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 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-nil means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain.
288
289 If value is `grow-only', only make tool-bar bigger. */
290
291 Lisp_Object Vauto_resize_tool_bars;
292
293 /* Non-zero means draw block and hollow cursor as wide as the glyph
294 under it. For example, if a block cursor is over a tab, it will be
295 drawn as wide as that tab on the display. */
296
297 int x_stretch_cursor_p;
298
299 /* Non-nil means don't actually do any redisplay. */
300
301 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
302
303 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
304
305 int inhibit_eval_during_redisplay;
306
307 /* Names of text properties relevant for redisplay. */
308
309 Lisp_Object Qdisplay;
310 extern Lisp_Object Qface, Qinvisible, Qwidth;
311
312 /* Symbols used in text property values. */
313
314 Lisp_Object Vdisplay_pixels_per_inch;
315 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
316 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
317 Lisp_Object Qslice;
318 Lisp_Object Qcenter;
319 Lisp_Object Qmargin, Qpointer;
320 Lisp_Object Qline_height;
321 extern Lisp_Object Qheight;
322 extern Lisp_Object QCwidth, QCheight, QCascent;
323 extern Lisp_Object Qscroll_bar;
324 extern Lisp_Object Qcursor;
325
326 /* Non-nil means highlight trailing whitespace. */
327
328 Lisp_Object Vshow_trailing_whitespace;
329
330 /* Non-nil means escape non-break space and hyphens. */
331
332 Lisp_Object Vnobreak_char_display;
333
334 #ifdef HAVE_WINDOW_SYSTEM
335 extern Lisp_Object Voverflow_newline_into_fringe;
336
337 /* Test if overflow newline into fringe. Called with iterator IT
338 at or past right window margin, and with IT->current_x set. */
339
340 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
341 (!NILP (Voverflow_newline_into_fringe) \
342 && FRAME_WINDOW_P (it->f) \
343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
344 && it->current_x == it->last_visible_x)
345
346 #endif /* HAVE_WINDOW_SYSTEM */
347
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
351
352 Lisp_Object Vvoid_text_area_pointer;
353
354 /* Name of the face used to highlight trailing whitespace. */
355
356 Lisp_Object Qtrailing_whitespace;
357
358 /* Name and number of the face used to highlight escape glyphs. */
359
360 Lisp_Object Qescape_glyph;
361
362 /* Name and number of the face used to highlight non-breaking spaces. */
363
364 Lisp_Object Qnobreak_space;
365
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
368
369 Lisp_Object Qimage;
370
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
374
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
377
378 int noninteractive_need_newline;
379
380 /* Non-zero means print newline to message log before next message. */
381
382 static int message_log_need_newline;
383
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
390 \f
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
395
396 static struct text_pos this_line_start_pos;
397
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
400
401 static struct text_pos this_line_end_pos;
402
403 /* The vertical positions and the height of this line. */
404
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
408
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
411
412 static int this_line_start_x;
413
414 /* Buffer that this_line_.* variables are referring to. */
415
416 static struct buffer *this_line_buffer;
417
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
420
421 int truncate_partial_width_windows;
422
423 /* A flag to control how to display unibyte 8-bit character. */
424
425 int unibyte_display_via_language_environment;
426
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
430
431 int multiple_frames;
432
433 Lisp_Object Vglobal_mode_string;
434
435
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
439
440 Lisp_Object Voverlay_arrow_variable_list;
441
442 /* Marker for where to display an arrow on top of the buffer text. */
443
444 Lisp_Object Voverlay_arrow_position;
445
446 /* String to display for the arrow. Only used on terminal frames. */
447
448 Lisp_Object Voverlay_arrow_string;
449
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
454
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
456
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
459
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
461
462 /* Like mode-line-format, but for the title bar on a visible frame. */
463
464 Lisp_Object Vframe_title_format;
465
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
467
468 Lisp_Object Vicon_title_format;
469
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
473
474 static Lisp_Object Vwindow_size_change_functions;
475
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
477
478 /* Nonzero if an overlay arrow has been displayed in this window. */
479
480 static int overlay_arrow_seen;
481
482 /* Nonzero means highlight the region even in nonselected windows. */
483
484 int highlight_nonselected_windows;
485
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
488
489 static EMACS_INT scroll_step;
490
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
493
494 static EMACS_INT scroll_conservatively;
495
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
500
501 EMACS_INT scroll_margin;
502
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
506
507 int buffer_shared;
508
509 /* Vector containing glyphs for an ellipsis `...'. */
510
511 static Lisp_Object default_invis_vector[3];
512
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
516
517 This variable is deprecated. */
518
519 int mode_line_inverse_video;
520
521 /* Prompt to display in front of the mini-buffer contents. */
522
523 Lisp_Object minibuf_prompt;
524
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
527
528 int minibuf_prompt_width;
529
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
533
534 Lisp_Object echo_area_window;
535
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
540
541 Lisp_Object Vmessage_stack;
542
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
545
546 int message_enable_multibyte;
547
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
549
550 int update_mode_lines;
551
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
554
555 int windows_or_buffers_changed;
556
557 /* Nonzero means a frame's cursor type has been changed. */
558
559 int cursor_type_changed;
560
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
563
564 int line_number_displayed;
565
566 /* Maximum buffer size for which to display line numbers. */
567
568 Lisp_Object Vline_number_display_limit;
569
570 /* Line width to consider when repositioning for line number display. */
571
572 static EMACS_INT line_number_display_limit_width;
573
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
576
577 Lisp_Object Vmessage_log_max;
578
579 /* The name of the *Messages* buffer, a string. */
580
581 static Lisp_Object Vmessages_buffer_name;
582
583 /* Index 0 is the buffer that holds the current (desired) echo area message,
584 or nil if none is desired right now.
585
586 Index 1 is the buffer that holds the previously displayed echo area message,
587 or nil to indicate no message. This is normally what's on the screen now.
588
589 These two can point to the same buffer. That happens when the last
590 message output by the user (or made by echoing) has been displayed. */
591
592 Lisp_Object echo_area_buffer[2];
593
594 /* Permanent pointers to the two buffers that are used for echo area
595 purposes. Once the two buffers are made, and their pointers are
596 placed here, these two slots remain unchanged unless those buffers
597 need to be created afresh. */
598
599 static Lisp_Object echo_buffer[2];
600
601 /* A vector saved used in with_area_buffer to reduce consing. */
602
603 static Lisp_Object Vwith_echo_area_save_vector;
604
605 /* Non-zero means display_echo_area should display the last echo area
606 message again. Set by redisplay_preserve_echo_area. */
607
608 static int display_last_displayed_message_p;
609
610 /* Nonzero if echo area is being used by print; zero if being used by
611 message. */
612
613 int message_buf_print;
614
615 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
616
617 Lisp_Object Qinhibit_menubar_update;
618 int inhibit_menubar_update;
619
620 /* When evaluating expressions from menu bar items (enable conditions,
621 for instance), this is the frame they are being processed for. */
622
623 Lisp_Object Vmenu_updating_frame;
624
625 /* Maximum height for resizing mini-windows. Either a float
626 specifying a fraction of the available height, or an integer
627 specifying a number of lines. */
628
629 Lisp_Object Vmax_mini_window_height;
630
631 /* Non-zero means messages should be displayed with truncated
632 lines instead of being continued. */
633
634 int message_truncate_lines;
635 Lisp_Object Qmessage_truncate_lines;
636
637 /* Set to 1 in clear_message to make redisplay_internal aware
638 of an emptied echo area. */
639
640 static int message_cleared_p;
641
642 /* How to blink the default frame cursor off. */
643 Lisp_Object Vblink_cursor_alist;
644
645 /* A scratch glyph row with contents used for generating truncation
646 glyphs. Also used in direct_output_for_insert. */
647
648 #define MAX_SCRATCH_GLYPHS 100
649 struct glyph_row scratch_glyph_row;
650 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
651
652 /* Ascent and height of the last line processed by move_it_to. */
653
654 static int last_max_ascent, last_height;
655
656 /* Non-zero if there's a help-echo in the echo area. */
657
658 int help_echo_showing_p;
659
660 /* If >= 0, computed, exact values of mode-line and header-line height
661 to use in the macros CURRENT_MODE_LINE_HEIGHT and
662 CURRENT_HEADER_LINE_HEIGHT. */
663
664 int current_mode_line_height, current_header_line_height;
665
666 /* The maximum distance to look ahead for text properties. Values
667 that are too small let us call compute_char_face and similar
668 functions too often which is expensive. Values that are too large
669 let us call compute_char_face and alike too often because we
670 might not be interested in text properties that far away. */
671
672 #define TEXT_PROP_DISTANCE_LIMIT 100
673
674 #if GLYPH_DEBUG
675
676 /* Variables to turn off display optimizations from Lisp. */
677
678 int inhibit_try_window_id, inhibit_try_window_reusing;
679 int inhibit_try_cursor_movement;
680
681 /* Non-zero means print traces of redisplay if compiled with
682 GLYPH_DEBUG != 0. */
683
684 int trace_redisplay_p;
685
686 #endif /* GLYPH_DEBUG */
687
688 #ifdef DEBUG_TRACE_MOVE
689 /* Non-zero means trace with TRACE_MOVE to stderr. */
690 int trace_move;
691
692 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
693 #else
694 #define TRACE_MOVE(x) (void) 0
695 #endif
696
697 /* Non-zero means automatically scroll windows horizontally to make
698 point visible. */
699
700 int automatic_hscrolling_p;
701
702 /* How close to the margin can point get before the window is scrolled
703 horizontally. */
704 EMACS_INT hscroll_margin;
705
706 /* How much to scroll horizontally when point is inside the above margin. */
707 Lisp_Object Vhscroll_step;
708
709 /* The variable `resize-mini-windows'. If nil, don't resize
710 mini-windows. If t, always resize them to fit the text they
711 display. If `grow-only', let mini-windows grow only until they
712 become empty. */
713
714 Lisp_Object Vresize_mini_windows;
715
716 /* Buffer being redisplayed -- for redisplay_window_error. */
717
718 struct buffer *displayed_buffer;
719
720 /* Space between overline and text. */
721
722 EMACS_INT overline_margin;
723
724 /* Value returned from text property handlers (see below). */
725
726 enum prop_handled
727 {
728 HANDLED_NORMALLY,
729 HANDLED_RECOMPUTE_PROPS,
730 HANDLED_OVERLAY_STRING_CONSUMED,
731 HANDLED_RETURN
732 };
733
734 /* A description of text properties that redisplay is interested
735 in. */
736
737 struct props
738 {
739 /* The name of the property. */
740 Lisp_Object *name;
741
742 /* A unique index for the property. */
743 enum prop_idx idx;
744
745 /* A handler function called to set up iterator IT from the property
746 at IT's current position. Value is used to steer handle_stop. */
747 enum prop_handled (*handler) P_ ((struct it *it));
748 };
749
750 static enum prop_handled handle_face_prop P_ ((struct it *));
751 static enum prop_handled handle_invisible_prop P_ ((struct it *));
752 static enum prop_handled handle_display_prop P_ ((struct it *));
753 static enum prop_handled handle_composition_prop P_ ((struct it *));
754 static enum prop_handled handle_overlay_change P_ ((struct it *));
755 static enum prop_handled handle_fontified_prop P_ ((struct it *));
756
757 /* Properties handled by iterators. */
758
759 static struct props it_props[] =
760 {
761 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
762 /* Handle `face' before `display' because some sub-properties of
763 `display' need to know the face. */
764 {&Qface, FACE_PROP_IDX, handle_face_prop},
765 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
766 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
767 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
768 {NULL, 0, NULL}
769 };
770
771 /* Value is the position described by X. If X is a marker, value is
772 the marker_position of X. Otherwise, value is X. */
773
774 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
775
776 /* Enumeration returned by some move_it_.* functions internally. */
777
778 enum move_it_result
779 {
780 /* Not used. Undefined value. */
781 MOVE_UNDEFINED,
782
783 /* Move ended at the requested buffer position or ZV. */
784 MOVE_POS_MATCH_OR_ZV,
785
786 /* Move ended at the requested X pixel position. */
787 MOVE_X_REACHED,
788
789 /* Move within a line ended at the end of a line that must be
790 continued. */
791 MOVE_LINE_CONTINUED,
792
793 /* Move within a line ended at the end of a line that would
794 be displayed truncated. */
795 MOVE_LINE_TRUNCATED,
796
797 /* Move within a line ended at a line end. */
798 MOVE_NEWLINE_OR_CR
799 };
800
801 /* This counter is used to clear the face cache every once in a while
802 in redisplay_internal. It is incremented for each redisplay.
803 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
804 cleared. */
805
806 #define CLEAR_FACE_CACHE_COUNT 500
807 static int clear_face_cache_count;
808
809 /* Similarly for the image cache. */
810
811 #ifdef HAVE_WINDOW_SYSTEM
812 #define CLEAR_IMAGE_CACHE_COUNT 101
813 static int clear_image_cache_count;
814 #endif
815
816 /* Record the previous terminal frame we displayed. */
817
818 static struct frame *previous_terminal_frame;
819
820 /* Non-zero while redisplay_internal is in progress. */
821
822 int redisplaying_p;
823
824 /* Non-zero means don't free realized faces. Bound while freeing
825 realized faces is dangerous because glyph matrices might still
826 reference them. */
827
828 int inhibit_free_realized_faces;
829 Lisp_Object Qinhibit_free_realized_faces;
830
831 /* If a string, XTread_socket generates an event to display that string.
832 (The display is done in read_char.) */
833
834 Lisp_Object help_echo_string;
835 Lisp_Object help_echo_window;
836 Lisp_Object help_echo_object;
837 int help_echo_pos;
838
839 /* Temporary variable for XTread_socket. */
840
841 Lisp_Object previous_help_echo_string;
842
843 /* Null glyph slice */
844
845 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
846
847 \f
848 /* Function prototypes. */
849
850 static void setup_for_ellipsis P_ ((struct it *, int));
851 static void mark_window_display_accurate_1 P_ ((struct window *, int));
852 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
853 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
854 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
855 static int redisplay_mode_lines P_ ((Lisp_Object, int));
856 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
857
858 #if 0
859 static int invisible_text_between_p P_ ((struct it *, int, int));
860 #endif
861
862 static void pint2str P_ ((char *, int, int));
863 static void pint2hrstr P_ ((char *, int, int));
864 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
865 struct text_pos));
866 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
867 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
868 static void store_mode_line_noprop_char P_ ((char));
869 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
870 static void x_consider_frame_title P_ ((Lisp_Object));
871 static void handle_stop P_ ((struct it *));
872 static int tool_bar_lines_needed P_ ((struct frame *, int *));
873 static int single_display_spec_intangible_p P_ ((Lisp_Object));
874 static void ensure_echo_area_buffers P_ ((void));
875 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
876 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
877 static int with_echo_area_buffer P_ ((struct window *, int,
878 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
879 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static void clear_garbaged_frames P_ ((void));
881 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
883 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int display_echo_area P_ ((struct window *));
885 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
886 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
887 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
888 static int string_char_and_length P_ ((const unsigned char *, int, int *));
889 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
890 struct text_pos));
891 static int compute_window_start_on_continuation_line P_ ((struct window *));
892 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
893 static void insert_left_trunc_glyphs P_ ((struct it *));
894 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
895 Lisp_Object));
896 static void extend_face_to_end_of_line P_ ((struct it *));
897 static int append_space_for_newline P_ ((struct it *, int));
898 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
899 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
900 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
901 static int trailing_whitespace_p P_ ((int));
902 static int message_log_check_duplicate P_ ((int, int, int, int));
903 static void push_it P_ ((struct it *));
904 static void pop_it P_ ((struct it *));
905 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
906 static void select_frame_for_redisplay P_ ((Lisp_Object));
907 static void redisplay_internal P_ ((int));
908 static int echo_area_display P_ ((int));
909 static void redisplay_windows P_ ((Lisp_Object));
910 static void redisplay_window P_ ((Lisp_Object, int));
911 static Lisp_Object redisplay_window_error ();
912 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
913 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
914 static int update_menu_bar P_ ((struct frame *, int, int));
915 static int try_window_reusing_current_matrix P_ ((struct window *));
916 static int try_window_id P_ ((struct window *));
917 static int display_line P_ ((struct it *));
918 static int display_mode_lines P_ ((struct window *));
919 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
920 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
921 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
922 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
923 static void display_menu_bar P_ ((struct window *));
924 static int display_count_lines P_ ((int, int, int, int, int *));
925 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
926 int, int, struct it *, int, int, int, int));
927 static void compute_line_metrics P_ ((struct it *));
928 static void run_redisplay_end_trigger_hook P_ ((struct it *));
929 static int get_overlay_strings P_ ((struct it *, int));
930 static int get_overlay_strings_1 P_ ((struct it *, int, int));
931 static void next_overlay_string P_ ((struct it *));
932 static void reseat P_ ((struct it *, struct text_pos, int));
933 static void reseat_1 P_ ((struct it *, struct text_pos, int));
934 static void back_to_previous_visible_line_start P_ ((struct it *));
935 void reseat_at_previous_visible_line_start P_ ((struct it *));
936 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
937 static int next_element_from_ellipsis P_ ((struct it *));
938 static int next_element_from_display_vector P_ ((struct it *));
939 static int next_element_from_string P_ ((struct it *));
940 static int next_element_from_c_string P_ ((struct it *));
941 static int next_element_from_buffer P_ ((struct it *));
942 static int next_element_from_composition P_ ((struct it *));
943 static int next_element_from_image P_ ((struct it *));
944 static int next_element_from_stretch P_ ((struct it *));
945 static void load_overlay_strings P_ ((struct it *, int));
946 static int init_from_display_pos P_ ((struct it *, struct window *,
947 struct display_pos *));
948 static void reseat_to_string P_ ((struct it *, unsigned char *,
949 Lisp_Object, int, int, int, int));
950 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
951 int, int, int));
952 void move_it_vertically_backward P_ ((struct it *, int));
953 static void init_to_row_start P_ ((struct it *, struct window *,
954 struct glyph_row *));
955 static int init_to_row_end P_ ((struct it *, struct window *,
956 struct glyph_row *));
957 static void back_to_previous_line_start P_ ((struct it *));
958 static int forward_to_next_line_start P_ ((struct it *, int *));
959 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
960 Lisp_Object, int));
961 static struct text_pos string_pos P_ ((int, Lisp_Object));
962 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
963 static int number_of_chars P_ ((unsigned char *, int));
964 static void compute_stop_pos P_ ((struct it *));
965 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
966 Lisp_Object));
967 static int face_before_or_after_it_pos P_ ((struct it *, int));
968 static int next_overlay_change P_ ((int));
969 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
970 Lisp_Object, struct text_pos *,
971 int));
972 static int underlying_face_id P_ ((struct it *));
973 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
974 struct window *));
975
976 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
977 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
978
979 #ifdef HAVE_WINDOW_SYSTEM
980
981 static void update_tool_bar P_ ((struct frame *, int));
982 static void build_desired_tool_bar_string P_ ((struct frame *f));
983 static int redisplay_tool_bar P_ ((struct frame *));
984 static void display_tool_bar_line P_ ((struct it *, int));
985 static void notice_overwritten_cursor P_ ((struct window *,
986 enum glyph_row_area,
987 int, int, int, int));
988
989
990
991 #endif /* HAVE_WINDOW_SYSTEM */
992
993 \f
994 /***********************************************************************
995 Window display dimensions
996 ***********************************************************************/
997
998 /* Return the bottom boundary y-position for text lines in window W.
999 This is the first y position at which a line cannot start.
1000 It is relative to the top of the window.
1001
1002 This is the height of W minus the height of a mode line, if any. */
1003
1004 INLINE int
1005 window_text_bottom_y (w)
1006 struct window *w;
1007 {
1008 int height = WINDOW_TOTAL_HEIGHT (w);
1009
1010 if (WINDOW_WANTS_MODELINE_P (w))
1011 height -= CURRENT_MODE_LINE_HEIGHT (w);
1012 return height;
1013 }
1014
1015 /* Return the pixel width of display area AREA of window W. AREA < 0
1016 means return the total width of W, not including fringes to
1017 the left and right of the window. */
1018
1019 INLINE int
1020 window_box_width (w, area)
1021 struct window *w;
1022 int area;
1023 {
1024 int cols = XFASTINT (w->total_cols);
1025 int pixels = 0;
1026
1027 if (!w->pseudo_window_p)
1028 {
1029 cols -= WINDOW_SCROLL_BAR_COLS (w);
1030
1031 if (area == TEXT_AREA)
1032 {
1033 if (INTEGERP (w->left_margin_cols))
1034 cols -= XFASTINT (w->left_margin_cols);
1035 if (INTEGERP (w->right_margin_cols))
1036 cols -= XFASTINT (w->right_margin_cols);
1037 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1038 }
1039 else if (area == LEFT_MARGIN_AREA)
1040 {
1041 cols = (INTEGERP (w->left_margin_cols)
1042 ? XFASTINT (w->left_margin_cols) : 0);
1043 pixels = 0;
1044 }
1045 else if (area == RIGHT_MARGIN_AREA)
1046 {
1047 cols = (INTEGERP (w->right_margin_cols)
1048 ? XFASTINT (w->right_margin_cols) : 0);
1049 pixels = 0;
1050 }
1051 }
1052
1053 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1054 }
1055
1056
1057 /* Return the pixel height of the display area of window W, not
1058 including mode lines of W, if any. */
1059
1060 INLINE int
1061 window_box_height (w)
1062 struct window *w;
1063 {
1064 struct frame *f = XFRAME (w->frame);
1065 int height = WINDOW_TOTAL_HEIGHT (w);
1066
1067 xassert (height >= 0);
1068
1069 /* Note: the code below that determines the mode-line/header-line
1070 height is essentially the same as that contained in the macro
1071 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1072 the appropriate glyph row has its `mode_line_p' flag set,
1073 and if it doesn't, uses estimate_mode_line_height instead. */
1074
1075 if (WINDOW_WANTS_MODELINE_P (w))
1076 {
1077 struct glyph_row *ml_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (ml_row && ml_row->mode_line_p)
1082 height -= ml_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1085 }
1086
1087 if (WINDOW_WANTS_HEADER_LINE_P (w))
1088 {
1089 struct glyph_row *hl_row
1090 = (w->current_matrix && w->current_matrix->rows
1091 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1092 : 0);
1093 if (hl_row && hl_row->mode_line_p)
1094 height -= hl_row->height;
1095 else
1096 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1097 }
1098
1099 /* With a very small font and a mode-line that's taller than
1100 default, we might end up with a negative height. */
1101 return max (0, height);
1102 }
1103
1104 /* Return the window-relative coordinate of the left edge of display
1105 area AREA of window W. AREA < 0 means return the left edge of the
1106 whole window, to the right of the left fringe of W. */
1107
1108 INLINE int
1109 window_box_left_offset (w, area)
1110 struct window *w;
1111 int area;
1112 {
1113 int x;
1114
1115 if (w->pseudo_window_p)
1116 return 0;
1117
1118 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1119
1120 if (area == TEXT_AREA)
1121 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1122 + window_box_width (w, LEFT_MARGIN_AREA));
1123 else if (area == RIGHT_MARGIN_AREA)
1124 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1125 + window_box_width (w, LEFT_MARGIN_AREA)
1126 + window_box_width (w, TEXT_AREA)
1127 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1128 ? 0
1129 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1130 else if (area == LEFT_MARGIN_AREA
1131 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1132 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1133
1134 return x;
1135 }
1136
1137
1138 /* Return the window-relative coordinate of the right edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the left of the right fringe of W. */
1141
1142 INLINE int
1143 window_box_right_offset (w, area)
1144 struct window *w;
1145 int area;
1146 {
1147 return window_box_left_offset (w, area) + window_box_width (w, area);
1148 }
1149
1150 /* Return the frame-relative coordinate of the left edge of display
1151 area AREA of window W. AREA < 0 means return the left edge of the
1152 whole window, to the right of the left fringe of W. */
1153
1154 INLINE int
1155 window_box_left (w, area)
1156 struct window *w;
1157 int area;
1158 {
1159 struct frame *f = XFRAME (w->frame);
1160 int x;
1161
1162 if (w->pseudo_window_p)
1163 return FRAME_INTERNAL_BORDER_WIDTH (f);
1164
1165 x = (WINDOW_LEFT_EDGE_X (w)
1166 + window_box_left_offset (w, area));
1167
1168 return x;
1169 }
1170
1171
1172 /* Return the frame-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1175
1176 INLINE int
1177 window_box_right (w, area)
1178 struct window *w;
1179 int area;
1180 {
1181 return window_box_left (w, area) + window_box_width (w, area);
1182 }
1183
1184 /* Get the bounding box of the display area AREA of window W, without
1185 mode lines, in frame-relative coordinates. AREA < 0 means the
1186 whole window, not including the left and right fringes of
1187 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1188 coordinates of the upper-left corner of the box. Return in
1189 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1190
1191 INLINE void
1192 window_box (w, area, box_x, box_y, box_width, box_height)
1193 struct window *w;
1194 int area;
1195 int *box_x, *box_y, *box_width, *box_height;
1196 {
1197 if (box_width)
1198 *box_width = window_box_width (w, area);
1199 if (box_height)
1200 *box_height = window_box_height (w);
1201 if (box_x)
1202 *box_x = window_box_left (w, area);
1203 if (box_y)
1204 {
1205 *box_y = WINDOW_TOP_EDGE_Y (w);
1206 if (WINDOW_WANTS_HEADER_LINE_P (w))
1207 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1208 }
1209 }
1210
1211
1212 /* Get the bounding box of the display area AREA of window W, without
1213 mode lines. AREA < 0 means the whole window, not including the
1214 left and right fringe of the window. Return in *TOP_LEFT_X
1215 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1216 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1217 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1218 box. */
1219
1220 INLINE void
1221 window_box_edges (w, area, top_left_x, top_left_y,
1222 bottom_right_x, bottom_right_y)
1223 struct window *w;
1224 int area;
1225 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1226 {
1227 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1228 bottom_right_y);
1229 *bottom_right_x += *top_left_x;
1230 *bottom_right_y += *top_left_y;
1231 }
1232
1233
1234 \f
1235 /***********************************************************************
1236 Utilities
1237 ***********************************************************************/
1238
1239 /* Return the bottom y-position of the line the iterator IT is in.
1240 This can modify IT's settings. */
1241
1242 int
1243 line_bottom_y (it)
1244 struct it *it;
1245 {
1246 int line_height = it->max_ascent + it->max_descent;
1247 int line_top_y = it->current_y;
1248
1249 if (line_height == 0)
1250 {
1251 if (last_height)
1252 line_height = last_height;
1253 else if (IT_CHARPOS (*it) < ZV)
1254 {
1255 move_it_by_lines (it, 1, 1);
1256 line_height = (it->max_ascent || it->max_descent
1257 ? it->max_ascent + it->max_descent
1258 : last_height);
1259 }
1260 else
1261 {
1262 struct glyph_row *row = it->glyph_row;
1263
1264 /* Use the default character height. */
1265 it->glyph_row = NULL;
1266 it->what = IT_CHARACTER;
1267 it->c = ' ';
1268 it->len = 1;
1269 PRODUCE_GLYPHS (it);
1270 line_height = it->ascent + it->descent;
1271 it->glyph_row = row;
1272 }
1273 }
1274
1275 return line_top_y + line_height;
1276 }
1277
1278
1279 /* Return 1 if position CHARPOS is visible in window W.
1280 CHARPOS < 0 means return info about WINDOW_END position.
1281 If visible, set *X and *Y to pixel coordinates of top left corner.
1282 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1283 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1284
1285 int
1286 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1287 struct window *w;
1288 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1289 {
1290 struct it it;
1291 struct text_pos top;
1292 int visible_p = 0;
1293 struct buffer *old_buffer = NULL;
1294
1295 if (noninteractive)
1296 return visible_p;
1297
1298 if (XBUFFER (w->buffer) != current_buffer)
1299 {
1300 old_buffer = current_buffer;
1301 set_buffer_internal_1 (XBUFFER (w->buffer));
1302 }
1303
1304 SET_TEXT_POS_FROM_MARKER (top, w->start);
1305
1306 /* Compute exact mode line heights. */
1307 if (WINDOW_WANTS_MODELINE_P (w))
1308 current_mode_line_height
1309 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1310 current_buffer->mode_line_format);
1311
1312 if (WINDOW_WANTS_HEADER_LINE_P (w))
1313 current_header_line_height
1314 = display_mode_line (w, HEADER_LINE_FACE_ID,
1315 current_buffer->header_line_format);
1316
1317 start_display (&it, w, top);
1318 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1319 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1320
1321 /* Note that we may overshoot because of invisible text. */
1322 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1323 {
1324 int top_x = it.current_x;
1325 int top_y = it.current_y;
1326 int bottom_y = (last_height = 0, line_bottom_y (&it));
1327 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1328
1329 if (top_y < window_top_y)
1330 visible_p = bottom_y > window_top_y;
1331 else if (top_y < it.last_visible_y)
1332 visible_p = 1;
1333 if (visible_p)
1334 {
1335 *x = top_x;
1336 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1337 *rtop = max (0, window_top_y - top_y);
1338 *rbot = max (0, bottom_y - it.last_visible_y);
1339 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1340 - max (top_y, window_top_y)));
1341 *vpos = it.vpos;
1342 }
1343 }
1344 else
1345 {
1346 struct it it2;
1347
1348 it2 = it;
1349 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1350 move_it_by_lines (&it, 1, 0);
1351 if (charpos < IT_CHARPOS (it)
1352 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1353 {
1354 visible_p = 1;
1355 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1356 *x = it2.current_x;
1357 *y = it2.current_y + it2.max_ascent - it2.ascent;
1358 *rtop = max (0, -it2.current_y);
1359 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1360 - it.last_visible_y));
1361 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1362 it.last_visible_y)
1363 - max (it2.current_y,
1364 WINDOW_HEADER_LINE_HEIGHT (w))));
1365 *vpos = it2.vpos;
1366 }
1367 }
1368
1369 if (old_buffer)
1370 set_buffer_internal_1 (old_buffer);
1371
1372 current_header_line_height = current_mode_line_height = -1;
1373
1374 if (visible_p && XFASTINT (w->hscroll) > 0)
1375 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1376
1377 #if 0
1378 /* Debugging code. */
1379 if (visible_p)
1380 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1381 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1382 else
1383 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1384 #endif
1385
1386 return visible_p;
1387 }
1388
1389
1390 /* Return the next character from STR which is MAXLEN bytes long.
1391 Return in *LEN the length of the character. This is like
1392 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1393 we find one, we return a `?', but with the length of the invalid
1394 character. */
1395
1396 static INLINE int
1397 string_char_and_length (str, maxlen, len)
1398 const unsigned char *str;
1399 int maxlen, *len;
1400 {
1401 int c;
1402
1403 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1404 if (!CHAR_VALID_P (c, 1))
1405 /* We may not change the length here because other places in Emacs
1406 don't use this function, i.e. they silently accept invalid
1407 characters. */
1408 c = '?';
1409
1410 return c;
1411 }
1412
1413
1414
1415 /* Given a position POS containing a valid character and byte position
1416 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1417
1418 static struct text_pos
1419 string_pos_nchars_ahead (pos, string, nchars)
1420 struct text_pos pos;
1421 Lisp_Object string;
1422 int nchars;
1423 {
1424 xassert (STRINGP (string) && nchars >= 0);
1425
1426 if (STRING_MULTIBYTE (string))
1427 {
1428 int rest = SBYTES (string) - BYTEPOS (pos);
1429 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1430 int len;
1431
1432 while (nchars--)
1433 {
1434 string_char_and_length (p, rest, &len);
1435 p += len, rest -= len;
1436 xassert (rest >= 0);
1437 CHARPOS (pos) += 1;
1438 BYTEPOS (pos) += len;
1439 }
1440 }
1441 else
1442 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1443
1444 return pos;
1445 }
1446
1447
1448 /* Value is the text position, i.e. character and byte position,
1449 for character position CHARPOS in STRING. */
1450
1451 static INLINE struct text_pos
1452 string_pos (charpos, string)
1453 int charpos;
1454 Lisp_Object string;
1455 {
1456 struct text_pos pos;
1457 xassert (STRINGP (string));
1458 xassert (charpos >= 0);
1459 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1460 return pos;
1461 }
1462
1463
1464 /* Value is a text position, i.e. character and byte position, for
1465 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1466 means recognize multibyte characters. */
1467
1468 static struct text_pos
1469 c_string_pos (charpos, s, multibyte_p)
1470 int charpos;
1471 unsigned char *s;
1472 int multibyte_p;
1473 {
1474 struct text_pos pos;
1475
1476 xassert (s != NULL);
1477 xassert (charpos >= 0);
1478
1479 if (multibyte_p)
1480 {
1481 int rest = strlen (s), len;
1482
1483 SET_TEXT_POS (pos, 0, 0);
1484 while (charpos--)
1485 {
1486 string_char_and_length (s, rest, &len);
1487 s += len, rest -= len;
1488 xassert (rest >= 0);
1489 CHARPOS (pos) += 1;
1490 BYTEPOS (pos) += len;
1491 }
1492 }
1493 else
1494 SET_TEXT_POS (pos, charpos, charpos);
1495
1496 return pos;
1497 }
1498
1499
1500 /* Value is the number of characters in C string S. MULTIBYTE_P
1501 non-zero means recognize multibyte characters. */
1502
1503 static int
1504 number_of_chars (s, multibyte_p)
1505 unsigned char *s;
1506 int multibyte_p;
1507 {
1508 int nchars;
1509
1510 if (multibyte_p)
1511 {
1512 int rest = strlen (s), len;
1513 unsigned char *p = (unsigned char *) s;
1514
1515 for (nchars = 0; rest > 0; ++nchars)
1516 {
1517 string_char_and_length (p, rest, &len);
1518 rest -= len, p += len;
1519 }
1520 }
1521 else
1522 nchars = strlen (s);
1523
1524 return nchars;
1525 }
1526
1527
1528 /* Compute byte position NEWPOS->bytepos corresponding to
1529 NEWPOS->charpos. POS is a known position in string STRING.
1530 NEWPOS->charpos must be >= POS.charpos. */
1531
1532 static void
1533 compute_string_pos (newpos, pos, string)
1534 struct text_pos *newpos, pos;
1535 Lisp_Object string;
1536 {
1537 xassert (STRINGP (string));
1538 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1539
1540 if (STRING_MULTIBYTE (string))
1541 *newpos = string_pos_nchars_ahead (pos, string,
1542 CHARPOS (*newpos) - CHARPOS (pos));
1543 else
1544 BYTEPOS (*newpos) = CHARPOS (*newpos);
1545 }
1546
1547 /* EXPORT:
1548 Return an estimation of the pixel height of mode or top lines on
1549 frame F. FACE_ID specifies what line's height to estimate. */
1550
1551 int
1552 estimate_mode_line_height (f, face_id)
1553 struct frame *f;
1554 enum face_id face_id;
1555 {
1556 #ifdef HAVE_WINDOW_SYSTEM
1557 if (FRAME_WINDOW_P (f))
1558 {
1559 int height = FONT_HEIGHT (FRAME_FONT (f));
1560
1561 /* This function is called so early when Emacs starts that the face
1562 cache and mode line face are not yet initialized. */
1563 if (FRAME_FACE_CACHE (f))
1564 {
1565 struct face *face = FACE_FROM_ID (f, face_id);
1566 if (face)
1567 {
1568 if (face->font)
1569 height = FONT_HEIGHT (face->font);
1570 if (face->box_line_width > 0)
1571 height += 2 * face->box_line_width;
1572 }
1573 }
1574
1575 return height;
1576 }
1577 #endif
1578
1579 return 1;
1580 }
1581
1582 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1583 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1584 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1585 not force the value into range. */
1586
1587 void
1588 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1589 FRAME_PTR f;
1590 register int pix_x, pix_y;
1591 int *x, *y;
1592 NativeRectangle *bounds;
1593 int noclip;
1594 {
1595
1596 #ifdef HAVE_WINDOW_SYSTEM
1597 if (FRAME_WINDOW_P (f))
1598 {
1599 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1600 even for negative values. */
1601 if (pix_x < 0)
1602 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1603 if (pix_y < 0)
1604 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1605
1606 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1607 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1608
1609 if (bounds)
1610 STORE_NATIVE_RECT (*bounds,
1611 FRAME_COL_TO_PIXEL_X (f, pix_x),
1612 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1613 FRAME_COLUMN_WIDTH (f) - 1,
1614 FRAME_LINE_HEIGHT (f) - 1);
1615
1616 if (!noclip)
1617 {
1618 if (pix_x < 0)
1619 pix_x = 0;
1620 else if (pix_x > FRAME_TOTAL_COLS (f))
1621 pix_x = FRAME_TOTAL_COLS (f);
1622
1623 if (pix_y < 0)
1624 pix_y = 0;
1625 else if (pix_y > FRAME_LINES (f))
1626 pix_y = FRAME_LINES (f);
1627 }
1628 }
1629 #endif
1630
1631 *x = pix_x;
1632 *y = pix_y;
1633 }
1634
1635
1636 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1637 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1638 can't tell the positions because W's display is not up to date,
1639 return 0. */
1640
1641 int
1642 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1643 struct window *w;
1644 int hpos, vpos;
1645 int *frame_x, *frame_y;
1646 {
1647 #ifdef HAVE_WINDOW_SYSTEM
1648 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1649 {
1650 int success_p;
1651
1652 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1653 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1654
1655 if (display_completed)
1656 {
1657 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1658 struct glyph *glyph = row->glyphs[TEXT_AREA];
1659 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1660
1661 hpos = row->x;
1662 vpos = row->y;
1663 while (glyph < end)
1664 {
1665 hpos += glyph->pixel_width;
1666 ++glyph;
1667 }
1668
1669 /* If first glyph is partially visible, its first visible position is still 0. */
1670 if (hpos < 0)
1671 hpos = 0;
1672
1673 success_p = 1;
1674 }
1675 else
1676 {
1677 hpos = vpos = 0;
1678 success_p = 0;
1679 }
1680
1681 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1682 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1683 return success_p;
1684 }
1685 #endif
1686
1687 *frame_x = hpos;
1688 *frame_y = vpos;
1689 return 1;
1690 }
1691
1692
1693 #ifdef HAVE_WINDOW_SYSTEM
1694
1695 /* Find the glyph under window-relative coordinates X/Y in window W.
1696 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1697 strings. Return in *HPOS and *VPOS the row and column number of
1698 the glyph found. Return in *AREA the glyph area containing X.
1699 Value is a pointer to the glyph found or null if X/Y is not on
1700 text, or we can't tell because W's current matrix is not up to
1701 date. */
1702
1703 static struct glyph *
1704 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1705 struct window *w;
1706 int x, y;
1707 int *hpos, *vpos, *dx, *dy, *area;
1708 {
1709 struct glyph *glyph, *end;
1710 struct glyph_row *row = NULL;
1711 int x0, i;
1712
1713 /* Find row containing Y. Give up if some row is not enabled. */
1714 for (i = 0; i < w->current_matrix->nrows; ++i)
1715 {
1716 row = MATRIX_ROW (w->current_matrix, i);
1717 if (!row->enabled_p)
1718 return NULL;
1719 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1720 break;
1721 }
1722
1723 *vpos = i;
1724 *hpos = 0;
1725
1726 /* Give up if Y is not in the window. */
1727 if (i == w->current_matrix->nrows)
1728 return NULL;
1729
1730 /* Get the glyph area containing X. */
1731 if (w->pseudo_window_p)
1732 {
1733 *area = TEXT_AREA;
1734 x0 = 0;
1735 }
1736 else
1737 {
1738 if (x < window_box_left_offset (w, TEXT_AREA))
1739 {
1740 *area = LEFT_MARGIN_AREA;
1741 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1742 }
1743 else if (x < window_box_right_offset (w, TEXT_AREA))
1744 {
1745 *area = TEXT_AREA;
1746 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1747 }
1748 else
1749 {
1750 *area = RIGHT_MARGIN_AREA;
1751 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1752 }
1753 }
1754
1755 /* Find glyph containing X. */
1756 glyph = row->glyphs[*area];
1757 end = glyph + row->used[*area];
1758 x -= x0;
1759 while (glyph < end && x >= glyph->pixel_width)
1760 {
1761 x -= glyph->pixel_width;
1762 ++glyph;
1763 }
1764
1765 if (glyph == end)
1766 return NULL;
1767
1768 if (dx)
1769 {
1770 *dx = x;
1771 *dy = y - (row->y + row->ascent - glyph->ascent);
1772 }
1773
1774 *hpos = glyph - row->glyphs[*area];
1775 return glyph;
1776 }
1777
1778
1779 /* EXPORT:
1780 Convert frame-relative x/y to coordinates relative to window W.
1781 Takes pseudo-windows into account. */
1782
1783 void
1784 frame_to_window_pixel_xy (w, x, y)
1785 struct window *w;
1786 int *x, *y;
1787 {
1788 if (w->pseudo_window_p)
1789 {
1790 /* A pseudo-window is always full-width, and starts at the
1791 left edge of the frame, plus a frame border. */
1792 struct frame *f = XFRAME (w->frame);
1793 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1794 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1795 }
1796 else
1797 {
1798 *x -= WINDOW_LEFT_EDGE_X (w);
1799 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1800 }
1801 }
1802
1803 /* EXPORT:
1804 Return in RECTS[] at most N clipping rectangles for glyph string S.
1805 Return the number of stored rectangles. */
1806
1807 int
1808 get_glyph_string_clip_rects (s, rects, n)
1809 struct glyph_string *s;
1810 NativeRectangle *rects;
1811 int n;
1812 {
1813 XRectangle r;
1814
1815 if (n <= 0)
1816 return 0;
1817
1818 if (s->row->full_width_p)
1819 {
1820 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1821 r.x = WINDOW_LEFT_EDGE_X (s->w);
1822 r.width = WINDOW_TOTAL_WIDTH (s->w);
1823
1824 /* Unless displaying a mode or menu bar line, which are always
1825 fully visible, clip to the visible part of the row. */
1826 if (s->w->pseudo_window_p)
1827 r.height = s->row->visible_height;
1828 else
1829 r.height = s->height;
1830 }
1831 else
1832 {
1833 /* This is a text line that may be partially visible. */
1834 r.x = window_box_left (s->w, s->area);
1835 r.width = window_box_width (s->w, s->area);
1836 r.height = s->row->visible_height;
1837 }
1838
1839 if (s->clip_head)
1840 if (r.x < s->clip_head->x)
1841 {
1842 if (r.width >= s->clip_head->x - r.x)
1843 r.width -= s->clip_head->x - r.x;
1844 else
1845 r.width = 0;
1846 r.x = s->clip_head->x;
1847 }
1848 if (s->clip_tail)
1849 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1850 {
1851 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1852 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1853 else
1854 r.width = 0;
1855 }
1856
1857 /* If S draws overlapping rows, it's sufficient to use the top and
1858 bottom of the window for clipping because this glyph string
1859 intentionally draws over other lines. */
1860 if (s->for_overlaps)
1861 {
1862 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1863 r.height = window_text_bottom_y (s->w) - r.y;
1864
1865 /* Alas, the above simple strategy does not work for the
1866 environments with anti-aliased text: if the same text is
1867 drawn onto the same place multiple times, it gets thicker.
1868 If the overlap we are processing is for the erased cursor, we
1869 take the intersection with the rectagle of the cursor. */
1870 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1871 {
1872 XRectangle rc, r_save = r;
1873
1874 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1875 rc.y = s->w->phys_cursor.y;
1876 rc.width = s->w->phys_cursor_width;
1877 rc.height = s->w->phys_cursor_height;
1878
1879 x_intersect_rectangles (&r_save, &rc, &r);
1880 }
1881 }
1882 else
1883 {
1884 /* Don't use S->y for clipping because it doesn't take partially
1885 visible lines into account. For example, it can be negative for
1886 partially visible lines at the top of a window. */
1887 if (!s->row->full_width_p
1888 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1889 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1890 else
1891 r.y = max (0, s->row->y);
1892
1893 /* If drawing a tool-bar window, draw it over the internal border
1894 at the top of the window. */
1895 if (WINDOWP (s->f->tool_bar_window)
1896 && s->w == XWINDOW (s->f->tool_bar_window))
1897 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1898 }
1899
1900 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1901
1902 /* If drawing the cursor, don't let glyph draw outside its
1903 advertised boundaries. Cleartype does this under some circumstances. */
1904 if (s->hl == DRAW_CURSOR)
1905 {
1906 struct glyph *glyph = s->first_glyph;
1907 int height, max_y;
1908
1909 if (s->x > r.x)
1910 {
1911 r.width -= s->x - r.x;
1912 r.x = s->x;
1913 }
1914 r.width = min (r.width, glyph->pixel_width);
1915
1916 /* If r.y is below window bottom, ensure that we still see a cursor. */
1917 height = min (glyph->ascent + glyph->descent,
1918 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1919 max_y = window_text_bottom_y (s->w) - height;
1920 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1921 if (s->ybase - glyph->ascent > max_y)
1922 {
1923 r.y = max_y;
1924 r.height = height;
1925 }
1926 else
1927 {
1928 /* Don't draw cursor glyph taller than our actual glyph. */
1929 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1930 if (height < r.height)
1931 {
1932 max_y = r.y + r.height;
1933 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1934 r.height = min (max_y - r.y, height);
1935 }
1936 }
1937 }
1938
1939 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1940 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1941 {
1942 #ifdef CONVERT_FROM_XRECT
1943 CONVERT_FROM_XRECT (r, *rects);
1944 #else
1945 *rects = r;
1946 #endif
1947 return 1;
1948 }
1949 else
1950 {
1951 /* If we are processing overlapping and allowed to return
1952 multiple clipping rectangles, we exclude the row of the glyph
1953 string from the clipping rectangle. This is to avoid drawing
1954 the same text on the environment with anti-aliasing. */
1955 #ifdef CONVERT_FROM_XRECT
1956 XRectangle rs[2];
1957 #else
1958 XRectangle *rs = rects;
1959 #endif
1960 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1961
1962 if (s->for_overlaps & OVERLAPS_PRED)
1963 {
1964 rs[i] = r;
1965 if (r.y + r.height > row_y)
1966 {
1967 if (r.y < row_y)
1968 rs[i].height = row_y - r.y;
1969 else
1970 rs[i].height = 0;
1971 }
1972 i++;
1973 }
1974 if (s->for_overlaps & OVERLAPS_SUCC)
1975 {
1976 rs[i] = r;
1977 if (r.y < row_y + s->row->visible_height)
1978 {
1979 if (r.y + r.height > row_y + s->row->visible_height)
1980 {
1981 rs[i].y = row_y + s->row->visible_height;
1982 rs[i].height = r.y + r.height - rs[i].y;
1983 }
1984 else
1985 rs[i].height = 0;
1986 }
1987 i++;
1988 }
1989
1990 n = i;
1991 #ifdef CONVERT_FROM_XRECT
1992 for (i = 0; i < n; i++)
1993 CONVERT_FROM_XRECT (rs[i], rects[i]);
1994 #endif
1995 return n;
1996 }
1997 }
1998
1999 /* EXPORT:
2000 Return in *NR the clipping rectangle for glyph string S. */
2001
2002 void
2003 get_glyph_string_clip_rect (s, nr)
2004 struct glyph_string *s;
2005 NativeRectangle *nr;
2006 {
2007 get_glyph_string_clip_rects (s, nr, 1);
2008 }
2009
2010
2011 /* EXPORT:
2012 Return the position and height of the phys cursor in window W.
2013 Set w->phys_cursor_width to width of phys cursor.
2014 */
2015
2016 void
2017 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2018 struct window *w;
2019 struct glyph_row *row;
2020 struct glyph *glyph;
2021 int *xp, *yp, *heightp;
2022 {
2023 struct frame *f = XFRAME (WINDOW_FRAME (w));
2024 int x, y, wd, h, h0, y0;
2025
2026 /* Compute the width of the rectangle to draw. If on a stretch
2027 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2028 rectangle as wide as the glyph, but use a canonical character
2029 width instead. */
2030 wd = glyph->pixel_width - 1;
2031 #ifdef HAVE_NTGUI
2032 wd++; /* Why? */
2033 #endif
2034
2035 x = w->phys_cursor.x;
2036 if (x < 0)
2037 {
2038 wd += x;
2039 x = 0;
2040 }
2041
2042 if (glyph->type == STRETCH_GLYPH
2043 && !x_stretch_cursor_p)
2044 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2045 w->phys_cursor_width = wd;
2046
2047 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2048
2049 /* If y is below window bottom, ensure that we still see a cursor. */
2050 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2051
2052 h = max (h0, glyph->ascent + glyph->descent);
2053 h0 = min (h0, glyph->ascent + glyph->descent);
2054
2055 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2056 if (y < y0)
2057 {
2058 h = max (h - (y0 - y) + 1, h0);
2059 y = y0 - 1;
2060 }
2061 else
2062 {
2063 y0 = window_text_bottom_y (w) - h0;
2064 if (y > y0)
2065 {
2066 h += y - y0;
2067 y = y0;
2068 }
2069 }
2070
2071 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2072 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2073 *heightp = h;
2074 }
2075
2076 /*
2077 * Remember which glyph the mouse is over.
2078 */
2079
2080 void
2081 remember_mouse_glyph (f, gx, gy, rect)
2082 struct frame *f;
2083 int gx, gy;
2084 NativeRectangle *rect;
2085 {
2086 Lisp_Object window;
2087 struct window *w;
2088 struct glyph_row *r, *gr, *end_row;
2089 enum window_part part;
2090 enum glyph_row_area area;
2091 int x, y, width, height;
2092
2093 /* Try to determine frame pixel position and size of the glyph under
2094 frame pixel coordinates X/Y on frame F. */
2095
2096 if (!f->glyphs_initialized_p
2097 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2098 NILP (window)))
2099 {
2100 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2101 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2102 goto virtual_glyph;
2103 }
2104
2105 w = XWINDOW (window);
2106 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2107 height = WINDOW_FRAME_LINE_HEIGHT (w);
2108
2109 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2110 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2111
2112 if (w->pseudo_window_p)
2113 {
2114 area = TEXT_AREA;
2115 part = ON_MODE_LINE; /* Don't adjust margin. */
2116 goto text_glyph;
2117 }
2118
2119 switch (part)
2120 {
2121 case ON_LEFT_MARGIN:
2122 area = LEFT_MARGIN_AREA;
2123 goto text_glyph;
2124
2125 case ON_RIGHT_MARGIN:
2126 area = RIGHT_MARGIN_AREA;
2127 goto text_glyph;
2128
2129 case ON_HEADER_LINE:
2130 case ON_MODE_LINE:
2131 gr = (part == ON_HEADER_LINE
2132 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2133 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2134 gy = gr->y;
2135 area = TEXT_AREA;
2136 goto text_glyph_row_found;
2137
2138 case ON_TEXT:
2139 area = TEXT_AREA;
2140
2141 text_glyph:
2142 gr = 0; gy = 0;
2143 for (; r <= end_row && r->enabled_p; ++r)
2144 if (r->y + r->height > y)
2145 {
2146 gr = r; gy = r->y;
2147 break;
2148 }
2149
2150 text_glyph_row_found:
2151 if (gr && gy <= y)
2152 {
2153 struct glyph *g = gr->glyphs[area];
2154 struct glyph *end = g + gr->used[area];
2155
2156 height = gr->height;
2157 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2158 if (gx + g->pixel_width > x)
2159 break;
2160
2161 if (g < end)
2162 {
2163 if (g->type == IMAGE_GLYPH)
2164 {
2165 /* Don't remember when mouse is over image, as
2166 image may have hot-spots. */
2167 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2168 return;
2169 }
2170 width = g->pixel_width;
2171 }
2172 else
2173 {
2174 /* Use nominal char spacing at end of line. */
2175 x -= gx;
2176 gx += (x / width) * width;
2177 }
2178
2179 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2180 gx += window_box_left_offset (w, area);
2181 }
2182 else
2183 {
2184 /* Use nominal line height at end of window. */
2185 gx = (x / width) * width;
2186 y -= gy;
2187 gy += (y / height) * height;
2188 }
2189 break;
2190
2191 case ON_LEFT_FRINGE:
2192 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2193 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2194 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2195 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2196 goto row_glyph;
2197
2198 case ON_RIGHT_FRINGE:
2199 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2200 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2201 : window_box_right_offset (w, TEXT_AREA));
2202 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2203 goto row_glyph;
2204
2205 case ON_SCROLL_BAR:
2206 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2207 ? 0
2208 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2209 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2210 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2211 : 0)));
2212 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2213
2214 row_glyph:
2215 gr = 0, gy = 0;
2216 for (; r <= end_row && r->enabled_p; ++r)
2217 if (r->y + r->height > y)
2218 {
2219 gr = r; gy = r->y;
2220 break;
2221 }
2222
2223 if (gr && gy <= y)
2224 height = gr->height;
2225 else
2226 {
2227 /* Use nominal line height at end of window. */
2228 y -= gy;
2229 gy += (y / height) * height;
2230 }
2231 break;
2232
2233 default:
2234 ;
2235 virtual_glyph:
2236 /* If there is no glyph under the mouse, then we divide the screen
2237 into a grid of the smallest glyph in the frame, and use that
2238 as our "glyph". */
2239
2240 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2241 round down even for negative values. */
2242 if (gx < 0)
2243 gx -= width - 1;
2244 if (gy < 0)
2245 gy -= height - 1;
2246
2247 gx = (gx / width) * width;
2248 gy = (gy / height) * height;
2249
2250 goto store_rect;
2251 }
2252
2253 gx += WINDOW_LEFT_EDGE_X (w);
2254 gy += WINDOW_TOP_EDGE_Y (w);
2255
2256 store_rect:
2257 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2258
2259 /* Visible feedback for debugging. */
2260 #if 0
2261 #if HAVE_X_WINDOWS
2262 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2263 f->output_data.x->normal_gc,
2264 gx, gy, width, height);
2265 #endif
2266 #endif
2267 }
2268
2269
2270 #endif /* HAVE_WINDOW_SYSTEM */
2271
2272 \f
2273 /***********************************************************************
2274 Lisp form evaluation
2275 ***********************************************************************/
2276
2277 /* Error handler for safe_eval and safe_call. */
2278
2279 static Lisp_Object
2280 safe_eval_handler (arg)
2281 Lisp_Object arg;
2282 {
2283 add_to_log ("Error during redisplay: %s", arg, Qnil);
2284 return Qnil;
2285 }
2286
2287
2288 /* Evaluate SEXPR and return the result, or nil if something went
2289 wrong. Prevent redisplay during the evaluation. */
2290
2291 Lisp_Object
2292 safe_eval (sexpr)
2293 Lisp_Object sexpr;
2294 {
2295 Lisp_Object val;
2296
2297 if (inhibit_eval_during_redisplay)
2298 val = Qnil;
2299 else
2300 {
2301 int count = SPECPDL_INDEX ();
2302 struct gcpro gcpro1;
2303
2304 GCPRO1 (sexpr);
2305 specbind (Qinhibit_redisplay, Qt);
2306 /* Use Qt to ensure debugger does not run,
2307 so there is no possibility of wanting to redisplay. */
2308 val = internal_condition_case_1 (Feval, sexpr, Qt,
2309 safe_eval_handler);
2310 UNGCPRO;
2311 val = unbind_to (count, val);
2312 }
2313
2314 return val;
2315 }
2316
2317
2318 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2319 Return the result, or nil if something went wrong. Prevent
2320 redisplay during the evaluation. */
2321
2322 Lisp_Object
2323 safe_call (nargs, args)
2324 int nargs;
2325 Lisp_Object *args;
2326 {
2327 Lisp_Object val;
2328
2329 if (inhibit_eval_during_redisplay)
2330 val = Qnil;
2331 else
2332 {
2333 int count = SPECPDL_INDEX ();
2334 struct gcpro gcpro1;
2335
2336 GCPRO1 (args[0]);
2337 gcpro1.nvars = nargs;
2338 specbind (Qinhibit_redisplay, Qt);
2339 /* Use Qt to ensure debugger does not run,
2340 so there is no possibility of wanting to redisplay. */
2341 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2342 safe_eval_handler);
2343 UNGCPRO;
2344 val = unbind_to (count, val);
2345 }
2346
2347 return val;
2348 }
2349
2350
2351 /* Call function FN with one argument ARG.
2352 Return the result, or nil if something went wrong. */
2353
2354 Lisp_Object
2355 safe_call1 (fn, arg)
2356 Lisp_Object fn, arg;
2357 {
2358 Lisp_Object args[2];
2359 args[0] = fn;
2360 args[1] = arg;
2361 return safe_call (2, args);
2362 }
2363
2364
2365 \f
2366 /***********************************************************************
2367 Debugging
2368 ***********************************************************************/
2369
2370 #if 0
2371
2372 /* Define CHECK_IT to perform sanity checks on iterators.
2373 This is for debugging. It is too slow to do unconditionally. */
2374
2375 static void
2376 check_it (it)
2377 struct it *it;
2378 {
2379 if (it->method == GET_FROM_STRING)
2380 {
2381 xassert (STRINGP (it->string));
2382 xassert (IT_STRING_CHARPOS (*it) >= 0);
2383 }
2384 else
2385 {
2386 xassert (IT_STRING_CHARPOS (*it) < 0);
2387 if (it->method == GET_FROM_BUFFER)
2388 {
2389 /* Check that character and byte positions agree. */
2390 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2391 }
2392 }
2393
2394 if (it->dpvec)
2395 xassert (it->current.dpvec_index >= 0);
2396 else
2397 xassert (it->current.dpvec_index < 0);
2398 }
2399
2400 #define CHECK_IT(IT) check_it ((IT))
2401
2402 #else /* not 0 */
2403
2404 #define CHECK_IT(IT) (void) 0
2405
2406 #endif /* not 0 */
2407
2408
2409 #if GLYPH_DEBUG
2410
2411 /* Check that the window end of window W is what we expect it
2412 to be---the last row in the current matrix displaying text. */
2413
2414 static void
2415 check_window_end (w)
2416 struct window *w;
2417 {
2418 if (!MINI_WINDOW_P (w)
2419 && !NILP (w->window_end_valid))
2420 {
2421 struct glyph_row *row;
2422 xassert ((row = MATRIX_ROW (w->current_matrix,
2423 XFASTINT (w->window_end_vpos)),
2424 !row->enabled_p
2425 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2426 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2427 }
2428 }
2429
2430 #define CHECK_WINDOW_END(W) check_window_end ((W))
2431
2432 #else /* not GLYPH_DEBUG */
2433
2434 #define CHECK_WINDOW_END(W) (void) 0
2435
2436 #endif /* not GLYPH_DEBUG */
2437
2438
2439 \f
2440 /***********************************************************************
2441 Iterator initialization
2442 ***********************************************************************/
2443
2444 /* Initialize IT for displaying current_buffer in window W, starting
2445 at character position CHARPOS. CHARPOS < 0 means that no buffer
2446 position is specified which is useful when the iterator is assigned
2447 a position later. BYTEPOS is the byte position corresponding to
2448 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2449
2450 If ROW is not null, calls to produce_glyphs with IT as parameter
2451 will produce glyphs in that row.
2452
2453 BASE_FACE_ID is the id of a base face to use. It must be one of
2454 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2455 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2456 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2457
2458 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2459 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2460 will be initialized to use the corresponding mode line glyph row of
2461 the desired matrix of W. */
2462
2463 void
2464 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2465 struct it *it;
2466 struct window *w;
2467 int charpos, bytepos;
2468 struct glyph_row *row;
2469 enum face_id base_face_id;
2470 {
2471 int highlight_region_p;
2472
2473 /* Some precondition checks. */
2474 xassert (w != NULL && it != NULL);
2475 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2476 && charpos <= ZV));
2477
2478 /* If face attributes have been changed since the last redisplay,
2479 free realized faces now because they depend on face definitions
2480 that might have changed. Don't free faces while there might be
2481 desired matrices pending which reference these faces. */
2482 if (face_change_count && !inhibit_free_realized_faces)
2483 {
2484 face_change_count = 0;
2485 free_all_realized_faces (Qnil);
2486 }
2487
2488 /* Use one of the mode line rows of W's desired matrix if
2489 appropriate. */
2490 if (row == NULL)
2491 {
2492 if (base_face_id == MODE_LINE_FACE_ID
2493 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2494 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2495 else if (base_face_id == HEADER_LINE_FACE_ID)
2496 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2497 }
2498
2499 /* Clear IT. */
2500 bzero (it, sizeof *it);
2501 it->current.overlay_string_index = -1;
2502 it->current.dpvec_index = -1;
2503 it->base_face_id = base_face_id;
2504 it->string = Qnil;
2505 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2506
2507 /* The window in which we iterate over current_buffer: */
2508 XSETWINDOW (it->window, w);
2509 it->w = w;
2510 it->f = XFRAME (w->frame);
2511
2512 /* Extra space between lines (on window systems only). */
2513 if (base_face_id == DEFAULT_FACE_ID
2514 && FRAME_WINDOW_P (it->f))
2515 {
2516 if (NATNUMP (current_buffer->extra_line_spacing))
2517 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2518 else if (FLOATP (current_buffer->extra_line_spacing))
2519 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2520 * FRAME_LINE_HEIGHT (it->f));
2521 else if (it->f->extra_line_spacing > 0)
2522 it->extra_line_spacing = it->f->extra_line_spacing;
2523 it->max_extra_line_spacing = 0;
2524 }
2525
2526 /* If realized faces have been removed, e.g. because of face
2527 attribute changes of named faces, recompute them. When running
2528 in batch mode, the face cache of Vterminal_frame is null. If
2529 we happen to get called, make a dummy face cache. */
2530 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2531 init_frame_faces (it->f);
2532 if (FRAME_FACE_CACHE (it->f)->used == 0)
2533 recompute_basic_faces (it->f);
2534
2535 /* Current value of the `slice', `space-width', and 'height' properties. */
2536 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2537 it->space_width = Qnil;
2538 it->font_height = Qnil;
2539 it->override_ascent = -1;
2540
2541 /* Are control characters displayed as `^C'? */
2542 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2543
2544 /* -1 means everything between a CR and the following line end
2545 is invisible. >0 means lines indented more than this value are
2546 invisible. */
2547 it->selective = (INTEGERP (current_buffer->selective_display)
2548 ? XFASTINT (current_buffer->selective_display)
2549 : (!NILP (current_buffer->selective_display)
2550 ? -1 : 0));
2551 it->selective_display_ellipsis_p
2552 = !NILP (current_buffer->selective_display_ellipses);
2553
2554 /* Display table to use. */
2555 it->dp = window_display_table (w);
2556
2557 /* Are multibyte characters enabled in current_buffer? */
2558 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2559
2560 /* Non-zero if we should highlight the region. */
2561 highlight_region_p
2562 = (!NILP (Vtransient_mark_mode)
2563 && !NILP (current_buffer->mark_active)
2564 && XMARKER (current_buffer->mark)->buffer != 0);
2565
2566 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2567 start and end of a visible region in window IT->w. Set both to
2568 -1 to indicate no region. */
2569 if (highlight_region_p
2570 /* Maybe highlight only in selected window. */
2571 && (/* Either show region everywhere. */
2572 highlight_nonselected_windows
2573 /* Or show region in the selected window. */
2574 || w == XWINDOW (selected_window)
2575 /* Or show the region if we are in the mini-buffer and W is
2576 the window the mini-buffer refers to. */
2577 || (MINI_WINDOW_P (XWINDOW (selected_window))
2578 && WINDOWP (minibuf_selected_window)
2579 && w == XWINDOW (minibuf_selected_window))))
2580 {
2581 int charpos = marker_position (current_buffer->mark);
2582 it->region_beg_charpos = min (PT, charpos);
2583 it->region_end_charpos = max (PT, charpos);
2584 }
2585 else
2586 it->region_beg_charpos = it->region_end_charpos = -1;
2587
2588 /* Get the position at which the redisplay_end_trigger hook should
2589 be run, if it is to be run at all. */
2590 if (MARKERP (w->redisplay_end_trigger)
2591 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2592 it->redisplay_end_trigger_charpos
2593 = marker_position (w->redisplay_end_trigger);
2594 else if (INTEGERP (w->redisplay_end_trigger))
2595 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2596
2597 /* Correct bogus values of tab_width. */
2598 it->tab_width = XINT (current_buffer->tab_width);
2599 if (it->tab_width <= 0 || it->tab_width > 1000)
2600 it->tab_width = 8;
2601
2602 /* Are lines in the display truncated? */
2603 it->truncate_lines_p
2604 = (base_face_id != DEFAULT_FACE_ID
2605 || XINT (it->w->hscroll)
2606 || (truncate_partial_width_windows
2607 && !WINDOW_FULL_WIDTH_P (it->w))
2608 || !NILP (current_buffer->truncate_lines));
2609
2610 /* Get dimensions of truncation and continuation glyphs. These are
2611 displayed as fringe bitmaps under X, so we don't need them for such
2612 frames. */
2613 if (!FRAME_WINDOW_P (it->f))
2614 {
2615 if (it->truncate_lines_p)
2616 {
2617 /* We will need the truncation glyph. */
2618 xassert (it->glyph_row == NULL);
2619 produce_special_glyphs (it, IT_TRUNCATION);
2620 it->truncation_pixel_width = it->pixel_width;
2621 }
2622 else
2623 {
2624 /* We will need the continuation glyph. */
2625 xassert (it->glyph_row == NULL);
2626 produce_special_glyphs (it, IT_CONTINUATION);
2627 it->continuation_pixel_width = it->pixel_width;
2628 }
2629
2630 /* Reset these values to zero because the produce_special_glyphs
2631 above has changed them. */
2632 it->pixel_width = it->ascent = it->descent = 0;
2633 it->phys_ascent = it->phys_descent = 0;
2634 }
2635
2636 /* Set this after getting the dimensions of truncation and
2637 continuation glyphs, so that we don't produce glyphs when calling
2638 produce_special_glyphs, above. */
2639 it->glyph_row = row;
2640 it->area = TEXT_AREA;
2641
2642 /* Get the dimensions of the display area. The display area
2643 consists of the visible window area plus a horizontally scrolled
2644 part to the left of the window. All x-values are relative to the
2645 start of this total display area. */
2646 if (base_face_id != DEFAULT_FACE_ID)
2647 {
2648 /* Mode lines, menu bar in terminal frames. */
2649 it->first_visible_x = 0;
2650 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2651 }
2652 else
2653 {
2654 it->first_visible_x
2655 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2656 it->last_visible_x = (it->first_visible_x
2657 + window_box_width (w, TEXT_AREA));
2658
2659 /* If we truncate lines, leave room for the truncator glyph(s) at
2660 the right margin. Otherwise, leave room for the continuation
2661 glyph(s). Truncation and continuation glyphs are not inserted
2662 for window-based redisplay. */
2663 if (!FRAME_WINDOW_P (it->f))
2664 {
2665 if (it->truncate_lines_p)
2666 it->last_visible_x -= it->truncation_pixel_width;
2667 else
2668 it->last_visible_x -= it->continuation_pixel_width;
2669 }
2670
2671 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2672 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2673 }
2674
2675 /* Leave room for a border glyph. */
2676 if (!FRAME_WINDOW_P (it->f)
2677 && !WINDOW_RIGHTMOST_P (it->w))
2678 it->last_visible_x -= 1;
2679
2680 it->last_visible_y = window_text_bottom_y (w);
2681
2682 /* For mode lines and alike, arrange for the first glyph having a
2683 left box line if the face specifies a box. */
2684 if (base_face_id != DEFAULT_FACE_ID)
2685 {
2686 struct face *face;
2687
2688 it->face_id = base_face_id;
2689
2690 /* If we have a boxed mode line, make the first character appear
2691 with a left box line. */
2692 face = FACE_FROM_ID (it->f, base_face_id);
2693 if (face->box != FACE_NO_BOX)
2694 it->start_of_box_run_p = 1;
2695 }
2696
2697 /* If a buffer position was specified, set the iterator there,
2698 getting overlays and face properties from that position. */
2699 if (charpos >= BUF_BEG (current_buffer))
2700 {
2701 it->end_charpos = ZV;
2702 it->face_id = -1;
2703 IT_CHARPOS (*it) = charpos;
2704
2705 /* Compute byte position if not specified. */
2706 if (bytepos < charpos)
2707 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2708 else
2709 IT_BYTEPOS (*it) = bytepos;
2710
2711 it->start = it->current;
2712
2713 /* Compute faces etc. */
2714 reseat (it, it->current.pos, 1);
2715 }
2716
2717 CHECK_IT (it);
2718 }
2719
2720
2721 /* Initialize IT for the display of window W with window start POS. */
2722
2723 void
2724 start_display (it, w, pos)
2725 struct it *it;
2726 struct window *w;
2727 struct text_pos pos;
2728 {
2729 struct glyph_row *row;
2730 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2731
2732 row = w->desired_matrix->rows + first_vpos;
2733 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2734 it->first_vpos = first_vpos;
2735
2736 /* Don't reseat to previous visible line start if current start
2737 position is in a string or image. */
2738 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2739 {
2740 int start_at_line_beg_p;
2741 int first_y = it->current_y;
2742
2743 /* If window start is not at a line start, skip forward to POS to
2744 get the correct continuation lines width. */
2745 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2746 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2747 if (!start_at_line_beg_p)
2748 {
2749 int new_x;
2750
2751 reseat_at_previous_visible_line_start (it);
2752 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2753
2754 new_x = it->current_x + it->pixel_width;
2755
2756 /* If lines are continued, this line may end in the middle
2757 of a multi-glyph character (e.g. a control character
2758 displayed as \003, or in the middle of an overlay
2759 string). In this case move_it_to above will not have
2760 taken us to the start of the continuation line but to the
2761 end of the continued line. */
2762 if (it->current_x > 0
2763 && !it->truncate_lines_p /* Lines are continued. */
2764 && (/* And glyph doesn't fit on the line. */
2765 new_x > it->last_visible_x
2766 /* Or it fits exactly and we're on a window
2767 system frame. */
2768 || (new_x == it->last_visible_x
2769 && FRAME_WINDOW_P (it->f))))
2770 {
2771 if (it->current.dpvec_index >= 0
2772 || it->current.overlay_string_index >= 0)
2773 {
2774 set_iterator_to_next (it, 1);
2775 move_it_in_display_line_to (it, -1, -1, 0);
2776 }
2777
2778 it->continuation_lines_width += it->current_x;
2779 }
2780
2781 /* We're starting a new display line, not affected by the
2782 height of the continued line, so clear the appropriate
2783 fields in the iterator structure. */
2784 it->max_ascent = it->max_descent = 0;
2785 it->max_phys_ascent = it->max_phys_descent = 0;
2786
2787 it->current_y = first_y;
2788 it->vpos = 0;
2789 it->current_x = it->hpos = 0;
2790 }
2791 }
2792
2793 #if 0 /* Don't assert the following because start_display is sometimes
2794 called intentionally with a window start that is not at a
2795 line start. Please leave this code in as a comment. */
2796
2797 /* Window start should be on a line start, now. */
2798 xassert (it->continuation_lines_width
2799 || IT_CHARPOS (it) == BEGV
2800 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2801 #endif /* 0 */
2802 }
2803
2804
2805 /* Return 1 if POS is a position in ellipses displayed for invisible
2806 text. W is the window we display, for text property lookup. */
2807
2808 static int
2809 in_ellipses_for_invisible_text_p (pos, w)
2810 struct display_pos *pos;
2811 struct window *w;
2812 {
2813 Lisp_Object prop, window;
2814 int ellipses_p = 0;
2815 int charpos = CHARPOS (pos->pos);
2816
2817 /* If POS specifies a position in a display vector, this might
2818 be for an ellipsis displayed for invisible text. We won't
2819 get the iterator set up for delivering that ellipsis unless
2820 we make sure that it gets aware of the invisible text. */
2821 if (pos->dpvec_index >= 0
2822 && pos->overlay_string_index < 0
2823 && CHARPOS (pos->string_pos) < 0
2824 && charpos > BEGV
2825 && (XSETWINDOW (window, w),
2826 prop = Fget_char_property (make_number (charpos),
2827 Qinvisible, window),
2828 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2829 {
2830 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2831 window);
2832 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2833 }
2834
2835 return ellipses_p;
2836 }
2837
2838
2839 /* Initialize IT for stepping through current_buffer in window W,
2840 starting at position POS that includes overlay string and display
2841 vector/ control character translation position information. Value
2842 is zero if there are overlay strings with newlines at POS. */
2843
2844 static int
2845 init_from_display_pos (it, w, pos)
2846 struct it *it;
2847 struct window *w;
2848 struct display_pos *pos;
2849 {
2850 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2851 int i, overlay_strings_with_newlines = 0;
2852
2853 /* If POS specifies a position in a display vector, this might
2854 be for an ellipsis displayed for invisible text. We won't
2855 get the iterator set up for delivering that ellipsis unless
2856 we make sure that it gets aware of the invisible text. */
2857 if (in_ellipses_for_invisible_text_p (pos, w))
2858 {
2859 --charpos;
2860 bytepos = 0;
2861 }
2862
2863 /* Keep in mind: the call to reseat in init_iterator skips invisible
2864 text, so we might end up at a position different from POS. This
2865 is only a problem when POS is a row start after a newline and an
2866 overlay starts there with an after-string, and the overlay has an
2867 invisible property. Since we don't skip invisible text in
2868 display_line and elsewhere immediately after consuming the
2869 newline before the row start, such a POS will not be in a string,
2870 but the call to init_iterator below will move us to the
2871 after-string. */
2872 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2873
2874 /* This only scans the current chunk -- it should scan all chunks.
2875 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2876 to 16 in 22.1 to make this a lesser problem. */
2877 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2878 {
2879 const char *s = SDATA (it->overlay_strings[i]);
2880 const char *e = s + SBYTES (it->overlay_strings[i]);
2881
2882 while (s < e && *s != '\n')
2883 ++s;
2884
2885 if (s < e)
2886 {
2887 overlay_strings_with_newlines = 1;
2888 break;
2889 }
2890 }
2891
2892 /* If position is within an overlay string, set up IT to the right
2893 overlay string. */
2894 if (pos->overlay_string_index >= 0)
2895 {
2896 int relative_index;
2897
2898 /* If the first overlay string happens to have a `display'
2899 property for an image, the iterator will be set up for that
2900 image, and we have to undo that setup first before we can
2901 correct the overlay string index. */
2902 if (it->method == GET_FROM_IMAGE)
2903 pop_it (it);
2904
2905 /* We already have the first chunk of overlay strings in
2906 IT->overlay_strings. Load more until the one for
2907 pos->overlay_string_index is in IT->overlay_strings. */
2908 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2909 {
2910 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2911 it->current.overlay_string_index = 0;
2912 while (n--)
2913 {
2914 load_overlay_strings (it, 0);
2915 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2916 }
2917 }
2918
2919 it->current.overlay_string_index = pos->overlay_string_index;
2920 relative_index = (it->current.overlay_string_index
2921 % OVERLAY_STRING_CHUNK_SIZE);
2922 it->string = it->overlay_strings[relative_index];
2923 xassert (STRINGP (it->string));
2924 it->current.string_pos = pos->string_pos;
2925 it->method = GET_FROM_STRING;
2926 }
2927
2928 #if 0 /* This is bogus because POS not having an overlay string
2929 position does not mean it's after the string. Example: A
2930 line starting with a before-string and initialization of IT
2931 to the previous row's end position. */
2932 else if (it->current.overlay_string_index >= 0)
2933 {
2934 /* If POS says we're already after an overlay string ending at
2935 POS, make sure to pop the iterator because it will be in
2936 front of that overlay string. When POS is ZV, we've thereby
2937 also ``processed'' overlay strings at ZV. */
2938 while (it->sp)
2939 pop_it (it);
2940 xassert (it->current.overlay_string_index == -1);
2941 xassert (it->method == GET_FROM_BUFFER);
2942 if (CHARPOS (pos->pos) == ZV)
2943 it->overlay_strings_at_end_processed_p = 1;
2944 }
2945 #endif /* 0 */
2946
2947 if (CHARPOS (pos->string_pos) >= 0)
2948 {
2949 /* Recorded position is not in an overlay string, but in another
2950 string. This can only be a string from a `display' property.
2951 IT should already be filled with that string. */
2952 it->current.string_pos = pos->string_pos;
2953 xassert (STRINGP (it->string));
2954 }
2955
2956 /* Restore position in display vector translations, control
2957 character translations or ellipses. */
2958 if (pos->dpvec_index >= 0)
2959 {
2960 if (it->dpvec == NULL)
2961 get_next_display_element (it);
2962 xassert (it->dpvec && it->current.dpvec_index == 0);
2963 it->current.dpvec_index = pos->dpvec_index;
2964 }
2965
2966 CHECK_IT (it);
2967 return !overlay_strings_with_newlines;
2968 }
2969
2970
2971 /* Initialize IT for stepping through current_buffer in window W
2972 starting at ROW->start. */
2973
2974 static void
2975 init_to_row_start (it, w, row)
2976 struct it *it;
2977 struct window *w;
2978 struct glyph_row *row;
2979 {
2980 init_from_display_pos (it, w, &row->start);
2981 it->start = row->start;
2982 it->continuation_lines_width = row->continuation_lines_width;
2983 CHECK_IT (it);
2984 }
2985
2986
2987 /* Initialize IT for stepping through current_buffer in window W
2988 starting in the line following ROW, i.e. starting at ROW->end.
2989 Value is zero if there are overlay strings with newlines at ROW's
2990 end position. */
2991
2992 static int
2993 init_to_row_end (it, w, row)
2994 struct it *it;
2995 struct window *w;
2996 struct glyph_row *row;
2997 {
2998 int success = 0;
2999
3000 if (init_from_display_pos (it, w, &row->end))
3001 {
3002 if (row->continued_p)
3003 it->continuation_lines_width
3004 = row->continuation_lines_width + row->pixel_width;
3005 CHECK_IT (it);
3006 success = 1;
3007 }
3008
3009 return success;
3010 }
3011
3012
3013
3014 \f
3015 /***********************************************************************
3016 Text properties
3017 ***********************************************************************/
3018
3019 /* Called when IT reaches IT->stop_charpos. Handle text property and
3020 overlay changes. Set IT->stop_charpos to the next position where
3021 to stop. */
3022
3023 static void
3024 handle_stop (it)
3025 struct it *it;
3026 {
3027 enum prop_handled handled;
3028 int handle_overlay_change_p;
3029 struct props *p;
3030
3031 it->dpvec = NULL;
3032 it->current.dpvec_index = -1;
3033 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3034 it->ignore_overlay_strings_at_pos_p = 0;
3035
3036 /* Use face of preceding text for ellipsis (if invisible) */
3037 if (it->selective_display_ellipsis_p)
3038 it->saved_face_id = it->face_id;
3039
3040 do
3041 {
3042 handled = HANDLED_NORMALLY;
3043
3044 /* Call text property handlers. */
3045 for (p = it_props; p->handler; ++p)
3046 {
3047 handled = p->handler (it);
3048
3049 if (handled == HANDLED_RECOMPUTE_PROPS)
3050 break;
3051 else if (handled == HANDLED_RETURN)
3052 {
3053 /* We still want to show before and after strings from
3054 overlays even if the actual buffer text is replaced. */
3055 if (!handle_overlay_change_p || it->sp > 1)
3056 return;
3057 if (!get_overlay_strings_1 (it, 0, 0))
3058 return;
3059 it->ignore_overlay_strings_at_pos_p = 1;
3060 it->string_from_display_prop_p = 0;
3061 handle_overlay_change_p = 0;
3062 handled = HANDLED_RECOMPUTE_PROPS;
3063 break;
3064 }
3065 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3066 handle_overlay_change_p = 0;
3067 }
3068
3069 if (handled != HANDLED_RECOMPUTE_PROPS)
3070 {
3071 /* Don't check for overlay strings below when set to deliver
3072 characters from a display vector. */
3073 if (it->method == GET_FROM_DISPLAY_VECTOR)
3074 handle_overlay_change_p = 0;
3075
3076 /* Handle overlay changes. */
3077 if (handle_overlay_change_p)
3078 handled = handle_overlay_change (it);
3079
3080 /* Determine where to stop next. */
3081 if (handled == HANDLED_NORMALLY)
3082 compute_stop_pos (it);
3083 }
3084 }
3085 while (handled == HANDLED_RECOMPUTE_PROPS);
3086 }
3087
3088
3089 /* Compute IT->stop_charpos from text property and overlay change
3090 information for IT's current position. */
3091
3092 static void
3093 compute_stop_pos (it)
3094 struct it *it;
3095 {
3096 register INTERVAL iv, next_iv;
3097 Lisp_Object object, limit, position;
3098
3099 /* If nowhere else, stop at the end. */
3100 it->stop_charpos = it->end_charpos;
3101
3102 if (STRINGP (it->string))
3103 {
3104 /* Strings are usually short, so don't limit the search for
3105 properties. */
3106 object = it->string;
3107 limit = Qnil;
3108 position = make_number (IT_STRING_CHARPOS (*it));
3109 }
3110 else
3111 {
3112 int charpos;
3113
3114 /* If next overlay change is in front of the current stop pos
3115 (which is IT->end_charpos), stop there. Note: value of
3116 next_overlay_change is point-max if no overlay change
3117 follows. */
3118 charpos = next_overlay_change (IT_CHARPOS (*it));
3119 if (charpos < it->stop_charpos)
3120 it->stop_charpos = charpos;
3121
3122 /* If showing the region, we have to stop at the region
3123 start or end because the face might change there. */
3124 if (it->region_beg_charpos > 0)
3125 {
3126 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3127 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3128 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3129 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3130 }
3131
3132 /* Set up variables for computing the stop position from text
3133 property changes. */
3134 XSETBUFFER (object, current_buffer);
3135 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3136 position = make_number (IT_CHARPOS (*it));
3137
3138 }
3139
3140 /* Get the interval containing IT's position. Value is a null
3141 interval if there isn't such an interval. */
3142 iv = validate_interval_range (object, &position, &position, 0);
3143 if (!NULL_INTERVAL_P (iv))
3144 {
3145 Lisp_Object values_here[LAST_PROP_IDX];
3146 struct props *p;
3147
3148 /* Get properties here. */
3149 for (p = it_props; p->handler; ++p)
3150 values_here[p->idx] = textget (iv->plist, *p->name);
3151
3152 /* Look for an interval following iv that has different
3153 properties. */
3154 for (next_iv = next_interval (iv);
3155 (!NULL_INTERVAL_P (next_iv)
3156 && (NILP (limit)
3157 || XFASTINT (limit) > next_iv->position));
3158 next_iv = next_interval (next_iv))
3159 {
3160 for (p = it_props; p->handler; ++p)
3161 {
3162 Lisp_Object new_value;
3163
3164 new_value = textget (next_iv->plist, *p->name);
3165 if (!EQ (values_here[p->idx], new_value))
3166 break;
3167 }
3168
3169 if (p->handler)
3170 break;
3171 }
3172
3173 if (!NULL_INTERVAL_P (next_iv))
3174 {
3175 if (INTEGERP (limit)
3176 && next_iv->position >= XFASTINT (limit))
3177 /* No text property change up to limit. */
3178 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3179 else
3180 /* Text properties change in next_iv. */
3181 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3182 }
3183 }
3184
3185 xassert (STRINGP (it->string)
3186 || (it->stop_charpos >= BEGV
3187 && it->stop_charpos >= IT_CHARPOS (*it)));
3188 }
3189
3190
3191 /* Return the position of the next overlay change after POS in
3192 current_buffer. Value is point-max if no overlay change
3193 follows. This is like `next-overlay-change' but doesn't use
3194 xmalloc. */
3195
3196 static int
3197 next_overlay_change (pos)
3198 int pos;
3199 {
3200 int noverlays;
3201 int endpos;
3202 Lisp_Object *overlays;
3203 int i;
3204
3205 /* Get all overlays at the given position. */
3206 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3207
3208 /* If any of these overlays ends before endpos,
3209 use its ending point instead. */
3210 for (i = 0; i < noverlays; ++i)
3211 {
3212 Lisp_Object oend;
3213 int oendpos;
3214
3215 oend = OVERLAY_END (overlays[i]);
3216 oendpos = OVERLAY_POSITION (oend);
3217 endpos = min (endpos, oendpos);
3218 }
3219
3220 return endpos;
3221 }
3222
3223
3224 \f
3225 /***********************************************************************
3226 Fontification
3227 ***********************************************************************/
3228
3229 /* Handle changes in the `fontified' property of the current buffer by
3230 calling hook functions from Qfontification_functions to fontify
3231 regions of text. */
3232
3233 static enum prop_handled
3234 handle_fontified_prop (it)
3235 struct it *it;
3236 {
3237 Lisp_Object prop, pos;
3238 enum prop_handled handled = HANDLED_NORMALLY;
3239
3240 if (!NILP (Vmemory_full))
3241 return handled;
3242
3243 /* Get the value of the `fontified' property at IT's current buffer
3244 position. (The `fontified' property doesn't have a special
3245 meaning in strings.) If the value is nil, call functions from
3246 Qfontification_functions. */
3247 if (!STRINGP (it->string)
3248 && it->s == NULL
3249 && !NILP (Vfontification_functions)
3250 && !NILP (Vrun_hooks)
3251 && (pos = make_number (IT_CHARPOS (*it)),
3252 prop = Fget_char_property (pos, Qfontified, Qnil),
3253 /* Ignore the special cased nil value always present at EOB since
3254 no amount of fontifying will be able to change it. */
3255 NILP (prop) && IT_CHARPOS (*it) < Z))
3256 {
3257 int count = SPECPDL_INDEX ();
3258 Lisp_Object val;
3259
3260 val = Vfontification_functions;
3261 specbind (Qfontification_functions, Qnil);
3262
3263 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3264 safe_call1 (val, pos);
3265 else
3266 {
3267 Lisp_Object globals, fn;
3268 struct gcpro gcpro1, gcpro2;
3269
3270 globals = Qnil;
3271 GCPRO2 (val, globals);
3272
3273 for (; CONSP (val); val = XCDR (val))
3274 {
3275 fn = XCAR (val);
3276
3277 if (EQ (fn, Qt))
3278 {
3279 /* A value of t indicates this hook has a local
3280 binding; it means to run the global binding too.
3281 In a global value, t should not occur. If it
3282 does, we must ignore it to avoid an endless
3283 loop. */
3284 for (globals = Fdefault_value (Qfontification_functions);
3285 CONSP (globals);
3286 globals = XCDR (globals))
3287 {
3288 fn = XCAR (globals);
3289 if (!EQ (fn, Qt))
3290 safe_call1 (fn, pos);
3291 }
3292 }
3293 else
3294 safe_call1 (fn, pos);
3295 }
3296
3297 UNGCPRO;
3298 }
3299
3300 unbind_to (count, Qnil);
3301
3302 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3303 something. This avoids an endless loop if they failed to
3304 fontify the text for which reason ever. */
3305 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3306 handled = HANDLED_RECOMPUTE_PROPS;
3307 }
3308
3309 return handled;
3310 }
3311
3312
3313 \f
3314 /***********************************************************************
3315 Faces
3316 ***********************************************************************/
3317
3318 /* Set up iterator IT from face properties at its current position.
3319 Called from handle_stop. */
3320
3321 static enum prop_handled
3322 handle_face_prop (it)
3323 struct it *it;
3324 {
3325 int new_face_id, next_stop;
3326
3327 if (!STRINGP (it->string))
3328 {
3329 new_face_id
3330 = face_at_buffer_position (it->w,
3331 IT_CHARPOS (*it),
3332 it->region_beg_charpos,
3333 it->region_end_charpos,
3334 &next_stop,
3335 (IT_CHARPOS (*it)
3336 + TEXT_PROP_DISTANCE_LIMIT),
3337 0);
3338
3339 /* Is this a start of a run of characters with box face?
3340 Caveat: this can be called for a freshly initialized
3341 iterator; face_id is -1 in this case. We know that the new
3342 face will not change until limit, i.e. if the new face has a
3343 box, all characters up to limit will have one. But, as
3344 usual, we don't know whether limit is really the end. */
3345 if (new_face_id != it->face_id)
3346 {
3347 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3348
3349 /* If new face has a box but old face has not, this is
3350 the start of a run of characters with box, i.e. it has
3351 a shadow on the left side. The value of face_id of the
3352 iterator will be -1 if this is the initial call that gets
3353 the face. In this case, we have to look in front of IT's
3354 position and see whether there is a face != new_face_id. */
3355 it->start_of_box_run_p
3356 = (new_face->box != FACE_NO_BOX
3357 && (it->face_id >= 0
3358 || IT_CHARPOS (*it) == BEG
3359 || new_face_id != face_before_it_pos (it)));
3360 it->face_box_p = new_face->box != FACE_NO_BOX;
3361 }
3362 }
3363 else
3364 {
3365 int base_face_id, bufpos;
3366
3367 if (it->current.overlay_string_index >= 0)
3368 bufpos = IT_CHARPOS (*it);
3369 else
3370 bufpos = 0;
3371
3372 /* For strings from a buffer, i.e. overlay strings or strings
3373 from a `display' property, use the face at IT's current
3374 buffer position as the base face to merge with, so that
3375 overlay strings appear in the same face as surrounding
3376 text, unless they specify their own faces. */
3377 base_face_id = underlying_face_id (it);
3378
3379 new_face_id = face_at_string_position (it->w,
3380 it->string,
3381 IT_STRING_CHARPOS (*it),
3382 bufpos,
3383 it->region_beg_charpos,
3384 it->region_end_charpos,
3385 &next_stop,
3386 base_face_id, 0);
3387
3388 #if 0 /* This shouldn't be neccessary. Let's check it. */
3389 /* If IT is used to display a mode line we would really like to
3390 use the mode line face instead of the frame's default face. */
3391 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3392 && new_face_id == DEFAULT_FACE_ID)
3393 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3394 #endif
3395
3396 /* Is this a start of a run of characters with box? Caveat:
3397 this can be called for a freshly allocated iterator; face_id
3398 is -1 is this case. We know that the new face will not
3399 change until the next check pos, i.e. if the new face has a
3400 box, all characters up to that position will have a
3401 box. But, as usual, we don't know whether that position
3402 is really the end. */
3403 if (new_face_id != it->face_id)
3404 {
3405 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3406 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3407
3408 /* If new face has a box but old face hasn't, this is the
3409 start of a run of characters with box, i.e. it has a
3410 shadow on the left side. */
3411 it->start_of_box_run_p
3412 = new_face->box && (old_face == NULL || !old_face->box);
3413 it->face_box_p = new_face->box != FACE_NO_BOX;
3414 }
3415 }
3416
3417 it->face_id = new_face_id;
3418 return HANDLED_NORMALLY;
3419 }
3420
3421
3422 /* Return the ID of the face ``underlying'' IT's current position,
3423 which is in a string. If the iterator is associated with a
3424 buffer, return the face at IT's current buffer position.
3425 Otherwise, use the iterator's base_face_id. */
3426
3427 static int
3428 underlying_face_id (it)
3429 struct it *it;
3430 {
3431 int face_id = it->base_face_id, i;
3432
3433 xassert (STRINGP (it->string));
3434
3435 for (i = it->sp - 1; i >= 0; --i)
3436 if (NILP (it->stack[i].string))
3437 face_id = it->stack[i].face_id;
3438
3439 return face_id;
3440 }
3441
3442
3443 /* Compute the face one character before or after the current position
3444 of IT. BEFORE_P non-zero means get the face in front of IT's
3445 position. Value is the id of the face. */
3446
3447 static int
3448 face_before_or_after_it_pos (it, before_p)
3449 struct it *it;
3450 int before_p;
3451 {
3452 int face_id, limit;
3453 int next_check_charpos;
3454 struct text_pos pos;
3455
3456 xassert (it->s == NULL);
3457
3458 if (STRINGP (it->string))
3459 {
3460 int bufpos, base_face_id;
3461
3462 /* No face change past the end of the string (for the case
3463 we are padding with spaces). No face change before the
3464 string start. */
3465 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3466 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3467 return it->face_id;
3468
3469 /* Set pos to the position before or after IT's current position. */
3470 if (before_p)
3471 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3472 else
3473 /* For composition, we must check the character after the
3474 composition. */
3475 pos = (it->what == IT_COMPOSITION
3476 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3477 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3478
3479 if (it->current.overlay_string_index >= 0)
3480 bufpos = IT_CHARPOS (*it);
3481 else
3482 bufpos = 0;
3483
3484 base_face_id = underlying_face_id (it);
3485
3486 /* Get the face for ASCII, or unibyte. */
3487 face_id = face_at_string_position (it->w,
3488 it->string,
3489 CHARPOS (pos),
3490 bufpos,
3491 it->region_beg_charpos,
3492 it->region_end_charpos,
3493 &next_check_charpos,
3494 base_face_id, 0);
3495
3496 /* Correct the face for charsets different from ASCII. Do it
3497 for the multibyte case only. The face returned above is
3498 suitable for unibyte text if IT->string is unibyte. */
3499 if (STRING_MULTIBYTE (it->string))
3500 {
3501 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3502 int rest = SBYTES (it->string) - BYTEPOS (pos);
3503 int c, len;
3504 struct face *face = FACE_FROM_ID (it->f, face_id);
3505
3506 c = string_char_and_length (p, rest, &len);
3507 face_id = FACE_FOR_CHAR (it->f, face, c);
3508 }
3509 }
3510 else
3511 {
3512 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3513 || (IT_CHARPOS (*it) <= BEGV && before_p))
3514 return it->face_id;
3515
3516 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3517 pos = it->current.pos;
3518
3519 if (before_p)
3520 DEC_TEXT_POS (pos, it->multibyte_p);
3521 else
3522 {
3523 if (it->what == IT_COMPOSITION)
3524 /* For composition, we must check the position after the
3525 composition. */
3526 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3527 else
3528 INC_TEXT_POS (pos, it->multibyte_p);
3529 }
3530
3531 /* Determine face for CHARSET_ASCII, or unibyte. */
3532 face_id = face_at_buffer_position (it->w,
3533 CHARPOS (pos),
3534 it->region_beg_charpos,
3535 it->region_end_charpos,
3536 &next_check_charpos,
3537 limit, 0);
3538
3539 /* Correct the face for charsets different from ASCII. Do it
3540 for the multibyte case only. The face returned above is
3541 suitable for unibyte text if current_buffer is unibyte. */
3542 if (it->multibyte_p)
3543 {
3544 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3545 struct face *face = FACE_FROM_ID (it->f, face_id);
3546 face_id = FACE_FOR_CHAR (it->f, face, c);
3547 }
3548 }
3549
3550 return face_id;
3551 }
3552
3553
3554 \f
3555 /***********************************************************************
3556 Invisible text
3557 ***********************************************************************/
3558
3559 /* Set up iterator IT from invisible properties at its current
3560 position. Called from handle_stop. */
3561
3562 static enum prop_handled
3563 handle_invisible_prop (it)
3564 struct it *it;
3565 {
3566 enum prop_handled handled = HANDLED_NORMALLY;
3567
3568 if (STRINGP (it->string))
3569 {
3570 extern Lisp_Object Qinvisible;
3571 Lisp_Object prop, end_charpos, limit, charpos;
3572
3573 /* Get the value of the invisible text property at the
3574 current position. Value will be nil if there is no such
3575 property. */
3576 charpos = make_number (IT_STRING_CHARPOS (*it));
3577 prop = Fget_text_property (charpos, Qinvisible, it->string);
3578
3579 if (!NILP (prop)
3580 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3581 {
3582 handled = HANDLED_RECOMPUTE_PROPS;
3583
3584 /* Get the position at which the next change of the
3585 invisible text property can be found in IT->string.
3586 Value will be nil if the property value is the same for
3587 all the rest of IT->string. */
3588 XSETINT (limit, SCHARS (it->string));
3589 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3590 it->string, limit);
3591
3592 /* Text at current position is invisible. The next
3593 change in the property is at position end_charpos.
3594 Move IT's current position to that position. */
3595 if (INTEGERP (end_charpos)
3596 && XFASTINT (end_charpos) < XFASTINT (limit))
3597 {
3598 struct text_pos old;
3599 old = it->current.string_pos;
3600 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3601 compute_string_pos (&it->current.string_pos, old, it->string);
3602 }
3603 else
3604 {
3605 /* The rest of the string is invisible. If this is an
3606 overlay string, proceed with the next overlay string
3607 or whatever comes and return a character from there. */
3608 if (it->current.overlay_string_index >= 0)
3609 {
3610 next_overlay_string (it);
3611 /* Don't check for overlay strings when we just
3612 finished processing them. */
3613 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3614 }
3615 else
3616 {
3617 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3618 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3619 }
3620 }
3621 }
3622 }
3623 else
3624 {
3625 int invis_p, newpos, next_stop, start_charpos;
3626 Lisp_Object pos, prop, overlay;
3627
3628 /* First of all, is there invisible text at this position? */
3629 start_charpos = IT_CHARPOS (*it);
3630 pos = make_number (IT_CHARPOS (*it));
3631 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3632 &overlay);
3633 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3634
3635 /* If we are on invisible text, skip over it. */
3636 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3637 {
3638 /* Record whether we have to display an ellipsis for the
3639 invisible text. */
3640 int display_ellipsis_p = invis_p == 2;
3641
3642 handled = HANDLED_RECOMPUTE_PROPS;
3643
3644 /* Loop skipping over invisible text. The loop is left at
3645 ZV or with IT on the first char being visible again. */
3646 do
3647 {
3648 /* Try to skip some invisible text. Return value is the
3649 position reached which can be equal to IT's position
3650 if there is nothing invisible here. This skips both
3651 over invisible text properties and overlays with
3652 invisible property. */
3653 newpos = skip_invisible (IT_CHARPOS (*it),
3654 &next_stop, ZV, it->window);
3655
3656 /* If we skipped nothing at all we weren't at invisible
3657 text in the first place. If everything to the end of
3658 the buffer was skipped, end the loop. */
3659 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3660 invis_p = 0;
3661 else
3662 {
3663 /* We skipped some characters but not necessarily
3664 all there are. Check if we ended up on visible
3665 text. Fget_char_property returns the property of
3666 the char before the given position, i.e. if we
3667 get invis_p = 0, this means that the char at
3668 newpos is visible. */
3669 pos = make_number (newpos);
3670 prop = Fget_char_property (pos, Qinvisible, it->window);
3671 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3672 }
3673
3674 /* If we ended up on invisible text, proceed to
3675 skip starting with next_stop. */
3676 if (invis_p)
3677 IT_CHARPOS (*it) = next_stop;
3678
3679 /* If there are adjacent invisible texts, don't lose the
3680 second one's ellipsis. */
3681 if (invis_p == 2)
3682 display_ellipsis_p = 1;
3683 }
3684 while (invis_p);
3685
3686 /* The position newpos is now either ZV or on visible text. */
3687 IT_CHARPOS (*it) = newpos;
3688 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3689
3690 /* If there are before-strings at the start of invisible
3691 text, and the text is invisible because of a text
3692 property, arrange to show before-strings because 20.x did
3693 it that way. (If the text is invisible because of an
3694 overlay property instead of a text property, this is
3695 already handled in the overlay code.) */
3696 if (NILP (overlay)
3697 && get_overlay_strings (it, start_charpos))
3698 {
3699 handled = HANDLED_RECOMPUTE_PROPS;
3700 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3701 }
3702 else if (display_ellipsis_p)
3703 {
3704 /* Make sure that the glyphs of the ellipsis will get
3705 correct `charpos' values. If we would not update
3706 it->position here, the glyphs would belong to the
3707 last visible character _before_ the invisible
3708 text, which confuses `set_cursor_from_row'.
3709
3710 We use the last invisible position instead of the
3711 first because this way the cursor is always drawn on
3712 the first "." of the ellipsis, whenever PT is inside
3713 the invisible text. Otherwise the cursor would be
3714 placed _after_ the ellipsis when the point is after the
3715 first invisible character. */
3716 if (!STRINGP (it->object))
3717 {
3718 it->position.charpos = IT_CHARPOS (*it) - 1;
3719 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3720 }
3721 setup_for_ellipsis (it, 0);
3722 }
3723 }
3724 }
3725
3726 return handled;
3727 }
3728
3729
3730 /* Make iterator IT return `...' next.
3731 Replaces LEN characters from buffer. */
3732
3733 static void
3734 setup_for_ellipsis (it, len)
3735 struct it *it;
3736 int len;
3737 {
3738 /* Use the display table definition for `...'. Invalid glyphs
3739 will be handled by the method returning elements from dpvec. */
3740 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3741 {
3742 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3743 it->dpvec = v->contents;
3744 it->dpend = v->contents + v->size;
3745 }
3746 else
3747 {
3748 /* Default `...'. */
3749 it->dpvec = default_invis_vector;
3750 it->dpend = default_invis_vector + 3;
3751 }
3752
3753 it->dpvec_char_len = len;
3754 it->current.dpvec_index = 0;
3755 it->dpvec_face_id = -1;
3756
3757 /* Remember the current face id in case glyphs specify faces.
3758 IT's face is restored in set_iterator_to_next.
3759 saved_face_id was set to preceding char's face in handle_stop. */
3760 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3761 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3762
3763 it->method = GET_FROM_DISPLAY_VECTOR;
3764 it->ellipsis_p = 1;
3765 }
3766
3767
3768 \f
3769 /***********************************************************************
3770 'display' property
3771 ***********************************************************************/
3772
3773 /* Set up iterator IT from `display' property at its current position.
3774 Called from handle_stop.
3775 We return HANDLED_RETURN if some part of the display property
3776 overrides the display of the buffer text itself.
3777 Otherwise we return HANDLED_NORMALLY. */
3778
3779 static enum prop_handled
3780 handle_display_prop (it)
3781 struct it *it;
3782 {
3783 Lisp_Object prop, object;
3784 struct text_pos *position;
3785 /* Nonzero if some property replaces the display of the text itself. */
3786 int display_replaced_p = 0;
3787
3788 if (STRINGP (it->string))
3789 {
3790 object = it->string;
3791 position = &it->current.string_pos;
3792 }
3793 else
3794 {
3795 XSETWINDOW (object, it->w);
3796 position = &it->current.pos;
3797 }
3798
3799 /* Reset those iterator values set from display property values. */
3800 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3801 it->space_width = Qnil;
3802 it->font_height = Qnil;
3803 it->voffset = 0;
3804
3805 /* We don't support recursive `display' properties, i.e. string
3806 values that have a string `display' property, that have a string
3807 `display' property etc. */
3808 if (!it->string_from_display_prop_p)
3809 it->area = TEXT_AREA;
3810
3811 prop = Fget_char_property (make_number (position->charpos),
3812 Qdisplay, object);
3813 if (NILP (prop))
3814 return HANDLED_NORMALLY;
3815
3816 if (!STRINGP (it->string))
3817 object = it->w->buffer;
3818
3819 if (CONSP (prop)
3820 /* Simple properties. */
3821 && !EQ (XCAR (prop), Qimage)
3822 && !EQ (XCAR (prop), Qspace)
3823 && !EQ (XCAR (prop), Qwhen)
3824 && !EQ (XCAR (prop), Qslice)
3825 && !EQ (XCAR (prop), Qspace_width)
3826 && !EQ (XCAR (prop), Qheight)
3827 && !EQ (XCAR (prop), Qraise)
3828 /* Marginal area specifications. */
3829 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3830 && !EQ (XCAR (prop), Qleft_fringe)
3831 && !EQ (XCAR (prop), Qright_fringe)
3832 && !NILP (XCAR (prop)))
3833 {
3834 for (; CONSP (prop); prop = XCDR (prop))
3835 {
3836 if (handle_single_display_spec (it, XCAR (prop), object,
3837 position, display_replaced_p))
3838 display_replaced_p = 1;
3839 }
3840 }
3841 else if (VECTORP (prop))
3842 {
3843 int i;
3844 for (i = 0; i < ASIZE (prop); ++i)
3845 if (handle_single_display_spec (it, AREF (prop, i), object,
3846 position, display_replaced_p))
3847 display_replaced_p = 1;
3848 }
3849 else
3850 {
3851 int ret = handle_single_display_spec (it, prop, object, position, 0);
3852 if (ret < 0) /* Replaced by "", i.e. nothing. */
3853 return HANDLED_RECOMPUTE_PROPS;
3854 if (ret)
3855 display_replaced_p = 1;
3856 }
3857
3858 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3859 }
3860
3861
3862 /* Value is the position of the end of the `display' property starting
3863 at START_POS in OBJECT. */
3864
3865 static struct text_pos
3866 display_prop_end (it, object, start_pos)
3867 struct it *it;
3868 Lisp_Object object;
3869 struct text_pos start_pos;
3870 {
3871 Lisp_Object end;
3872 struct text_pos end_pos;
3873
3874 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3875 Qdisplay, object, Qnil);
3876 CHARPOS (end_pos) = XFASTINT (end);
3877 if (STRINGP (object))
3878 compute_string_pos (&end_pos, start_pos, it->string);
3879 else
3880 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3881
3882 return end_pos;
3883 }
3884
3885
3886 /* Set up IT from a single `display' specification PROP. OBJECT
3887 is the object in which the `display' property was found. *POSITION
3888 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3889 means that we previously saw a display specification which already
3890 replaced text display with something else, for example an image;
3891 we ignore such properties after the first one has been processed.
3892
3893 If PROP is a `space' or `image' specification, and in some other
3894 cases too, set *POSITION to the position where the `display'
3895 property ends.
3896
3897 Value is non-zero if something was found which replaces the display
3898 of buffer or string text. Specifically, the value is -1 if that
3899 "something" is "nothing". */
3900
3901 static int
3902 handle_single_display_spec (it, spec, object, position,
3903 display_replaced_before_p)
3904 struct it *it;
3905 Lisp_Object spec;
3906 Lisp_Object object;
3907 struct text_pos *position;
3908 int display_replaced_before_p;
3909 {
3910 Lisp_Object form;
3911 Lisp_Object location, value;
3912 struct text_pos start_pos, save_pos;
3913 int valid_p;
3914
3915 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3916 If the result is non-nil, use VALUE instead of SPEC. */
3917 form = Qt;
3918 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3919 {
3920 spec = XCDR (spec);
3921 if (!CONSP (spec))
3922 return 0;
3923 form = XCAR (spec);
3924 spec = XCDR (spec);
3925 }
3926
3927 if (!NILP (form) && !EQ (form, Qt))
3928 {
3929 int count = SPECPDL_INDEX ();
3930 struct gcpro gcpro1;
3931
3932 /* Bind `object' to the object having the `display' property, a
3933 buffer or string. Bind `position' to the position in the
3934 object where the property was found, and `buffer-position'
3935 to the current position in the buffer. */
3936 specbind (Qobject, object);
3937 specbind (Qposition, make_number (CHARPOS (*position)));
3938 specbind (Qbuffer_position,
3939 make_number (STRINGP (object)
3940 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3941 GCPRO1 (form);
3942 form = safe_eval (form);
3943 UNGCPRO;
3944 unbind_to (count, Qnil);
3945 }
3946
3947 if (NILP (form))
3948 return 0;
3949
3950 /* Handle `(height HEIGHT)' specifications. */
3951 if (CONSP (spec)
3952 && EQ (XCAR (spec), Qheight)
3953 && CONSP (XCDR (spec)))
3954 {
3955 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3956 return 0;
3957
3958 it->font_height = XCAR (XCDR (spec));
3959 if (!NILP (it->font_height))
3960 {
3961 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3962 int new_height = -1;
3963
3964 if (CONSP (it->font_height)
3965 && (EQ (XCAR (it->font_height), Qplus)
3966 || EQ (XCAR (it->font_height), Qminus))
3967 && CONSP (XCDR (it->font_height))
3968 && INTEGERP (XCAR (XCDR (it->font_height))))
3969 {
3970 /* `(+ N)' or `(- N)' where N is an integer. */
3971 int steps = XINT (XCAR (XCDR (it->font_height)));
3972 if (EQ (XCAR (it->font_height), Qplus))
3973 steps = - steps;
3974 it->face_id = smaller_face (it->f, it->face_id, steps);
3975 }
3976 else if (FUNCTIONP (it->font_height))
3977 {
3978 /* Call function with current height as argument.
3979 Value is the new height. */
3980 Lisp_Object height;
3981 height = safe_call1 (it->font_height,
3982 face->lface[LFACE_HEIGHT_INDEX]);
3983 if (NUMBERP (height))
3984 new_height = XFLOATINT (height);
3985 }
3986 else if (NUMBERP (it->font_height))
3987 {
3988 /* Value is a multiple of the canonical char height. */
3989 struct face *face;
3990
3991 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3992 new_height = (XFLOATINT (it->font_height)
3993 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3994 }
3995 else
3996 {
3997 /* Evaluate IT->font_height with `height' bound to the
3998 current specified height to get the new height. */
3999 int count = SPECPDL_INDEX ();
4000
4001 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4002 value = safe_eval (it->font_height);
4003 unbind_to (count, Qnil);
4004
4005 if (NUMBERP (value))
4006 new_height = XFLOATINT (value);
4007 }
4008
4009 if (new_height > 0)
4010 it->face_id = face_with_height (it->f, it->face_id, new_height);
4011 }
4012
4013 return 0;
4014 }
4015
4016 /* Handle `(space_width WIDTH)'. */
4017 if (CONSP (spec)
4018 && EQ (XCAR (spec), Qspace_width)
4019 && CONSP (XCDR (spec)))
4020 {
4021 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4022 return 0;
4023
4024 value = XCAR (XCDR (spec));
4025 if (NUMBERP (value) && XFLOATINT (value) > 0)
4026 it->space_width = value;
4027
4028 return 0;
4029 }
4030
4031 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4032 if (CONSP (spec)
4033 && EQ (XCAR (spec), Qslice))
4034 {
4035 Lisp_Object tem;
4036
4037 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4038 return 0;
4039
4040 if (tem = XCDR (spec), CONSP (tem))
4041 {
4042 it->slice.x = XCAR (tem);
4043 if (tem = XCDR (tem), CONSP (tem))
4044 {
4045 it->slice.y = XCAR (tem);
4046 if (tem = XCDR (tem), CONSP (tem))
4047 {
4048 it->slice.width = XCAR (tem);
4049 if (tem = XCDR (tem), CONSP (tem))
4050 it->slice.height = XCAR (tem);
4051 }
4052 }
4053 }
4054
4055 return 0;
4056 }
4057
4058 /* Handle `(raise FACTOR)'. */
4059 if (CONSP (spec)
4060 && EQ (XCAR (spec), Qraise)
4061 && CONSP (XCDR (spec)))
4062 {
4063 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4064 return 0;
4065
4066 #ifdef HAVE_WINDOW_SYSTEM
4067 value = XCAR (XCDR (spec));
4068 if (NUMBERP (value))
4069 {
4070 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4071 it->voffset = - (XFLOATINT (value)
4072 * (FONT_HEIGHT (face->font)));
4073 }
4074 #endif /* HAVE_WINDOW_SYSTEM */
4075
4076 return 0;
4077 }
4078
4079 /* Don't handle the other kinds of display specifications
4080 inside a string that we got from a `display' property. */
4081 if (it->string_from_display_prop_p)
4082 return 0;
4083
4084 /* Characters having this form of property are not displayed, so
4085 we have to find the end of the property. */
4086 start_pos = *position;
4087 *position = display_prop_end (it, object, start_pos);
4088 value = Qnil;
4089
4090 /* Stop the scan at that end position--we assume that all
4091 text properties change there. */
4092 it->stop_charpos = position->charpos;
4093
4094 /* Handle `(left-fringe BITMAP [FACE])'
4095 and `(right-fringe BITMAP [FACE])'. */
4096 if (CONSP (spec)
4097 && (EQ (XCAR (spec), Qleft_fringe)
4098 || EQ (XCAR (spec), Qright_fringe))
4099 && CONSP (XCDR (spec)))
4100 {
4101 int face_id = DEFAULT_FACE_ID;
4102 int fringe_bitmap;
4103
4104 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4105 /* If we return here, POSITION has been advanced
4106 across the text with this property. */
4107 return 0;
4108
4109 #ifdef HAVE_WINDOW_SYSTEM
4110 value = XCAR (XCDR (spec));
4111 if (!SYMBOLP (value)
4112 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4113 /* If we return here, POSITION has been advanced
4114 across the text with this property. */
4115 return 0;
4116
4117 if (CONSP (XCDR (XCDR (spec))))
4118 {
4119 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4120 int face_id2 = lookup_derived_face (it->f, face_name,
4121 'A', FRINGE_FACE_ID, 0);
4122 if (face_id2 >= 0)
4123 face_id = face_id2;
4124 }
4125
4126 /* Save current settings of IT so that we can restore them
4127 when we are finished with the glyph property value. */
4128
4129 save_pos = it->position;
4130 it->position = *position;
4131 push_it (it);
4132 it->position = save_pos;
4133
4134 it->area = TEXT_AREA;
4135 it->what = IT_IMAGE;
4136 it->image_id = -1; /* no image */
4137 it->position = start_pos;
4138 it->object = NILP (object) ? it->w->buffer : object;
4139 it->method = GET_FROM_IMAGE;
4140 it->face_id = face_id;
4141
4142 /* Say that we haven't consumed the characters with
4143 `display' property yet. The call to pop_it in
4144 set_iterator_to_next will clean this up. */
4145 *position = start_pos;
4146
4147 if (EQ (XCAR (spec), Qleft_fringe))
4148 {
4149 it->left_user_fringe_bitmap = fringe_bitmap;
4150 it->left_user_fringe_face_id = face_id;
4151 }
4152 else
4153 {
4154 it->right_user_fringe_bitmap = fringe_bitmap;
4155 it->right_user_fringe_face_id = face_id;
4156 }
4157 #endif /* HAVE_WINDOW_SYSTEM */
4158 return 1;
4159 }
4160
4161 /* Prepare to handle `((margin left-margin) ...)',
4162 `((margin right-margin) ...)' and `((margin nil) ...)'
4163 prefixes for display specifications. */
4164 location = Qunbound;
4165 if (CONSP (spec) && CONSP (XCAR (spec)))
4166 {
4167 Lisp_Object tem;
4168
4169 value = XCDR (spec);
4170 if (CONSP (value))
4171 value = XCAR (value);
4172
4173 tem = XCAR (spec);
4174 if (EQ (XCAR (tem), Qmargin)
4175 && (tem = XCDR (tem),
4176 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4177 (NILP (tem)
4178 || EQ (tem, Qleft_margin)
4179 || EQ (tem, Qright_margin))))
4180 location = tem;
4181 }
4182
4183 if (EQ (location, Qunbound))
4184 {
4185 location = Qnil;
4186 value = spec;
4187 }
4188
4189 /* After this point, VALUE is the property after any
4190 margin prefix has been stripped. It must be a string,
4191 an image specification, or `(space ...)'.
4192
4193 LOCATION specifies where to display: `left-margin',
4194 `right-margin' or nil. */
4195
4196 valid_p = (STRINGP (value)
4197 #ifdef HAVE_WINDOW_SYSTEM
4198 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4199 #endif /* not HAVE_WINDOW_SYSTEM */
4200 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4201
4202 if (valid_p && !display_replaced_before_p)
4203 {
4204 /* Save current settings of IT so that we can restore them
4205 when we are finished with the glyph property value. */
4206 save_pos = it->position;
4207 it->position = *position;
4208 push_it (it);
4209 it->position = save_pos;
4210
4211 if (NILP (location))
4212 it->area = TEXT_AREA;
4213 else if (EQ (location, Qleft_margin))
4214 it->area = LEFT_MARGIN_AREA;
4215 else
4216 it->area = RIGHT_MARGIN_AREA;
4217
4218 if (STRINGP (value))
4219 {
4220 if (SCHARS (value) == 0)
4221 {
4222 pop_it (it);
4223 return -1; /* Replaced by "", i.e. nothing. */
4224 }
4225 it->string = value;
4226 it->multibyte_p = STRING_MULTIBYTE (it->string);
4227 it->current.overlay_string_index = -1;
4228 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4229 it->end_charpos = it->string_nchars = SCHARS (it->string);
4230 it->method = GET_FROM_STRING;
4231 it->stop_charpos = 0;
4232 it->string_from_display_prop_p = 1;
4233 /* Say that we haven't consumed the characters with
4234 `display' property yet. The call to pop_it in
4235 set_iterator_to_next will clean this up. */
4236 *position = start_pos;
4237 }
4238 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4239 {
4240 it->method = GET_FROM_STRETCH;
4241 it->object = value;
4242 *position = it->position = start_pos;
4243 }
4244 #ifdef HAVE_WINDOW_SYSTEM
4245 else
4246 {
4247 it->what = IT_IMAGE;
4248 it->image_id = lookup_image (it->f, value);
4249 it->position = start_pos;
4250 it->object = NILP (object) ? it->w->buffer : object;
4251 it->method = GET_FROM_IMAGE;
4252
4253 /* Say that we haven't consumed the characters with
4254 `display' property yet. The call to pop_it in
4255 set_iterator_to_next will clean this up. */
4256 *position = start_pos;
4257 }
4258 #endif /* HAVE_WINDOW_SYSTEM */
4259
4260 return 1;
4261 }
4262
4263 /* Invalid property or property not supported. Restore
4264 POSITION to what it was before. */
4265 *position = start_pos;
4266 return 0;
4267 }
4268
4269
4270 /* Check if SPEC is a display specification value whose text should be
4271 treated as intangible. */
4272
4273 static int
4274 single_display_spec_intangible_p (prop)
4275 Lisp_Object prop;
4276 {
4277 /* Skip over `when FORM'. */
4278 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4279 {
4280 prop = XCDR (prop);
4281 if (!CONSP (prop))
4282 return 0;
4283 prop = XCDR (prop);
4284 }
4285
4286 if (STRINGP (prop))
4287 return 1;
4288
4289 if (!CONSP (prop))
4290 return 0;
4291
4292 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4293 we don't need to treat text as intangible. */
4294 if (EQ (XCAR (prop), Qmargin))
4295 {
4296 prop = XCDR (prop);
4297 if (!CONSP (prop))
4298 return 0;
4299
4300 prop = XCDR (prop);
4301 if (!CONSP (prop)
4302 || EQ (XCAR (prop), Qleft_margin)
4303 || EQ (XCAR (prop), Qright_margin))
4304 return 0;
4305 }
4306
4307 return (CONSP (prop)
4308 && (EQ (XCAR (prop), Qimage)
4309 || EQ (XCAR (prop), Qspace)));
4310 }
4311
4312
4313 /* Check if PROP is a display property value whose text should be
4314 treated as intangible. */
4315
4316 int
4317 display_prop_intangible_p (prop)
4318 Lisp_Object prop;
4319 {
4320 if (CONSP (prop)
4321 && CONSP (XCAR (prop))
4322 && !EQ (Qmargin, XCAR (XCAR (prop))))
4323 {
4324 /* A list of sub-properties. */
4325 while (CONSP (prop))
4326 {
4327 if (single_display_spec_intangible_p (XCAR (prop)))
4328 return 1;
4329 prop = XCDR (prop);
4330 }
4331 }
4332 else if (VECTORP (prop))
4333 {
4334 /* A vector of sub-properties. */
4335 int i;
4336 for (i = 0; i < ASIZE (prop); ++i)
4337 if (single_display_spec_intangible_p (AREF (prop, i)))
4338 return 1;
4339 }
4340 else
4341 return single_display_spec_intangible_p (prop);
4342
4343 return 0;
4344 }
4345
4346
4347 /* Return 1 if PROP is a display sub-property value containing STRING. */
4348
4349 static int
4350 single_display_spec_string_p (prop, string)
4351 Lisp_Object prop, string;
4352 {
4353 if (EQ (string, prop))
4354 return 1;
4355
4356 /* Skip over `when FORM'. */
4357 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4358 {
4359 prop = XCDR (prop);
4360 if (!CONSP (prop))
4361 return 0;
4362 prop = XCDR (prop);
4363 }
4364
4365 if (CONSP (prop))
4366 /* Skip over `margin LOCATION'. */
4367 if (EQ (XCAR (prop), Qmargin))
4368 {
4369 prop = XCDR (prop);
4370 if (!CONSP (prop))
4371 return 0;
4372
4373 prop = XCDR (prop);
4374 if (!CONSP (prop))
4375 return 0;
4376 }
4377
4378 return CONSP (prop) && EQ (XCAR (prop), string);
4379 }
4380
4381
4382 /* Return 1 if STRING appears in the `display' property PROP. */
4383
4384 static int
4385 display_prop_string_p (prop, string)
4386 Lisp_Object prop, string;
4387 {
4388 if (CONSP (prop)
4389 && CONSP (XCAR (prop))
4390 && !EQ (Qmargin, XCAR (XCAR (prop))))
4391 {
4392 /* A list of sub-properties. */
4393 while (CONSP (prop))
4394 {
4395 if (single_display_spec_string_p (XCAR (prop), string))
4396 return 1;
4397 prop = XCDR (prop);
4398 }
4399 }
4400 else if (VECTORP (prop))
4401 {
4402 /* A vector of sub-properties. */
4403 int i;
4404 for (i = 0; i < ASIZE (prop); ++i)
4405 if (single_display_spec_string_p (AREF (prop, i), string))
4406 return 1;
4407 }
4408 else
4409 return single_display_spec_string_p (prop, string);
4410
4411 return 0;
4412 }
4413
4414
4415 /* Determine from which buffer position in W's buffer STRING comes
4416 from. AROUND_CHARPOS is an approximate position where it could
4417 be from. Value is the buffer position or 0 if it couldn't be
4418 determined.
4419
4420 W's buffer must be current.
4421
4422 This function is necessary because we don't record buffer positions
4423 in glyphs generated from strings (to keep struct glyph small).
4424 This function may only use code that doesn't eval because it is
4425 called asynchronously from note_mouse_highlight. */
4426
4427 int
4428 string_buffer_position (w, string, around_charpos)
4429 struct window *w;
4430 Lisp_Object string;
4431 int around_charpos;
4432 {
4433 Lisp_Object limit, prop, pos;
4434 const int MAX_DISTANCE = 1000;
4435 int found = 0;
4436
4437 pos = make_number (around_charpos);
4438 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4439 while (!found && !EQ (pos, limit))
4440 {
4441 prop = Fget_char_property (pos, Qdisplay, Qnil);
4442 if (!NILP (prop) && display_prop_string_p (prop, string))
4443 found = 1;
4444 else
4445 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4446 }
4447
4448 if (!found)
4449 {
4450 pos = make_number (around_charpos);
4451 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4452 while (!found && !EQ (pos, limit))
4453 {
4454 prop = Fget_char_property (pos, Qdisplay, Qnil);
4455 if (!NILP (prop) && display_prop_string_p (prop, string))
4456 found = 1;
4457 else
4458 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4459 limit);
4460 }
4461 }
4462
4463 return found ? XINT (pos) : 0;
4464 }
4465
4466
4467 \f
4468 /***********************************************************************
4469 `composition' property
4470 ***********************************************************************/
4471
4472 /* Set up iterator IT from `composition' property at its current
4473 position. Called from handle_stop. */
4474
4475 static enum prop_handled
4476 handle_composition_prop (it)
4477 struct it *it;
4478 {
4479 Lisp_Object prop, string;
4480 int pos, pos_byte, end;
4481 enum prop_handled handled = HANDLED_NORMALLY;
4482
4483 if (STRINGP (it->string))
4484 {
4485 pos = IT_STRING_CHARPOS (*it);
4486 pos_byte = IT_STRING_BYTEPOS (*it);
4487 string = it->string;
4488 }
4489 else
4490 {
4491 pos = IT_CHARPOS (*it);
4492 pos_byte = IT_BYTEPOS (*it);
4493 string = Qnil;
4494 }
4495
4496 /* If there's a valid composition and point is not inside of the
4497 composition (in the case that the composition is from the current
4498 buffer), draw a glyph composed from the composition components. */
4499 if (find_composition (pos, -1, &pos, &end, &prop, string)
4500 && COMPOSITION_VALID_P (pos, end, prop)
4501 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4502 {
4503 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4504
4505 if (id >= 0)
4506 {
4507 struct composition *cmp = composition_table[id];
4508
4509 if (cmp->glyph_len == 0)
4510 {
4511 /* No glyph. */
4512 if (STRINGP (it->string))
4513 {
4514 IT_STRING_CHARPOS (*it) = end;
4515 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4516 end);
4517 }
4518 else
4519 {
4520 IT_CHARPOS (*it) = end;
4521 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4522 }
4523 return HANDLED_RECOMPUTE_PROPS;
4524 }
4525
4526 it->stop_charpos = end;
4527 push_it (it);
4528
4529 it->method = GET_FROM_COMPOSITION;
4530 it->cmp_id = id;
4531 it->cmp_len = COMPOSITION_LENGTH (prop);
4532 /* For a terminal, draw only the first character of the
4533 components. */
4534 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4535 it->len = (STRINGP (it->string)
4536 ? string_char_to_byte (it->string, end)
4537 : CHAR_TO_BYTE (end)) - pos_byte;
4538 handled = HANDLED_RETURN;
4539 }
4540 }
4541
4542 return handled;
4543 }
4544
4545
4546 \f
4547 /***********************************************************************
4548 Overlay strings
4549 ***********************************************************************/
4550
4551 /* The following structure is used to record overlay strings for
4552 later sorting in load_overlay_strings. */
4553
4554 struct overlay_entry
4555 {
4556 Lisp_Object overlay;
4557 Lisp_Object string;
4558 int priority;
4559 int after_string_p;
4560 };
4561
4562
4563 /* Set up iterator IT from overlay strings at its current position.
4564 Called from handle_stop. */
4565
4566 static enum prop_handled
4567 handle_overlay_change (it)
4568 struct it *it;
4569 {
4570 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4571 return HANDLED_RECOMPUTE_PROPS;
4572 else
4573 return HANDLED_NORMALLY;
4574 }
4575
4576
4577 /* Set up the next overlay string for delivery by IT, if there is an
4578 overlay string to deliver. Called by set_iterator_to_next when the
4579 end of the current overlay string is reached. If there are more
4580 overlay strings to display, IT->string and
4581 IT->current.overlay_string_index are set appropriately here.
4582 Otherwise IT->string is set to nil. */
4583
4584 static void
4585 next_overlay_string (it)
4586 struct it *it;
4587 {
4588 ++it->current.overlay_string_index;
4589 if (it->current.overlay_string_index == it->n_overlay_strings)
4590 {
4591 /* No more overlay strings. Restore IT's settings to what
4592 they were before overlay strings were processed, and
4593 continue to deliver from current_buffer. */
4594 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4595
4596 pop_it (it);
4597 xassert (it->sp > 0
4598 || it->method == GET_FROM_COMPOSITION
4599 || (NILP (it->string)
4600 && it->method == GET_FROM_BUFFER
4601 && it->stop_charpos >= BEGV
4602 && it->stop_charpos <= it->end_charpos));
4603 it->current.overlay_string_index = -1;
4604 it->n_overlay_strings = 0;
4605
4606 /* If we're at the end of the buffer, record that we have
4607 processed the overlay strings there already, so that
4608 next_element_from_buffer doesn't try it again. */
4609 if (IT_CHARPOS (*it) >= it->end_charpos)
4610 it->overlay_strings_at_end_processed_p = 1;
4611
4612 /* If we have to display `...' for invisible text, set
4613 the iterator up for that. */
4614 if (display_ellipsis_p)
4615 setup_for_ellipsis (it, 0);
4616 }
4617 else
4618 {
4619 /* There are more overlay strings to process. If
4620 IT->current.overlay_string_index has advanced to a position
4621 where we must load IT->overlay_strings with more strings, do
4622 it. */
4623 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4624
4625 if (it->current.overlay_string_index && i == 0)
4626 load_overlay_strings (it, 0);
4627
4628 /* Initialize IT to deliver display elements from the overlay
4629 string. */
4630 it->string = it->overlay_strings[i];
4631 it->multibyte_p = STRING_MULTIBYTE (it->string);
4632 SET_TEXT_POS (it->current.string_pos, 0, 0);
4633 it->method = GET_FROM_STRING;
4634 it->stop_charpos = 0;
4635 }
4636
4637 CHECK_IT (it);
4638 }
4639
4640
4641 /* Compare two overlay_entry structures E1 and E2. Used as a
4642 comparison function for qsort in load_overlay_strings. Overlay
4643 strings for the same position are sorted so that
4644
4645 1. All after-strings come in front of before-strings, except
4646 when they come from the same overlay.
4647
4648 2. Within after-strings, strings are sorted so that overlay strings
4649 from overlays with higher priorities come first.
4650
4651 2. Within before-strings, strings are sorted so that overlay
4652 strings from overlays with higher priorities come last.
4653
4654 Value is analogous to strcmp. */
4655
4656
4657 static int
4658 compare_overlay_entries (e1, e2)
4659 void *e1, *e2;
4660 {
4661 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4662 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4663 int result;
4664
4665 if (entry1->after_string_p != entry2->after_string_p)
4666 {
4667 /* Let after-strings appear in front of before-strings if
4668 they come from different overlays. */
4669 if (EQ (entry1->overlay, entry2->overlay))
4670 result = entry1->after_string_p ? 1 : -1;
4671 else
4672 result = entry1->after_string_p ? -1 : 1;
4673 }
4674 else if (entry1->after_string_p)
4675 /* After-strings sorted in order of decreasing priority. */
4676 result = entry2->priority - entry1->priority;
4677 else
4678 /* Before-strings sorted in order of increasing priority. */
4679 result = entry1->priority - entry2->priority;
4680
4681 return result;
4682 }
4683
4684
4685 /* Load the vector IT->overlay_strings with overlay strings from IT's
4686 current buffer position, or from CHARPOS if that is > 0. Set
4687 IT->n_overlays to the total number of overlay strings found.
4688
4689 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4690 a time. On entry into load_overlay_strings,
4691 IT->current.overlay_string_index gives the number of overlay
4692 strings that have already been loaded by previous calls to this
4693 function.
4694
4695 IT->add_overlay_start contains an additional overlay start
4696 position to consider for taking overlay strings from, if non-zero.
4697 This position comes into play when the overlay has an `invisible'
4698 property, and both before and after-strings. When we've skipped to
4699 the end of the overlay, because of its `invisible' property, we
4700 nevertheless want its before-string to appear.
4701 IT->add_overlay_start will contain the overlay start position
4702 in this case.
4703
4704 Overlay strings are sorted so that after-string strings come in
4705 front of before-string strings. Within before and after-strings,
4706 strings are sorted by overlay priority. See also function
4707 compare_overlay_entries. */
4708
4709 static void
4710 load_overlay_strings (it, charpos)
4711 struct it *it;
4712 int charpos;
4713 {
4714 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4715 Lisp_Object overlay, window, str, invisible;
4716 struct Lisp_Overlay *ov;
4717 int start, end;
4718 int size = 20;
4719 int n = 0, i, j, invis_p;
4720 struct overlay_entry *entries
4721 = (struct overlay_entry *) alloca (size * sizeof *entries);
4722
4723 if (charpos <= 0)
4724 charpos = IT_CHARPOS (*it);
4725
4726 /* Append the overlay string STRING of overlay OVERLAY to vector
4727 `entries' which has size `size' and currently contains `n'
4728 elements. AFTER_P non-zero means STRING is an after-string of
4729 OVERLAY. */
4730 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4731 do \
4732 { \
4733 Lisp_Object priority; \
4734 \
4735 if (n == size) \
4736 { \
4737 int new_size = 2 * size; \
4738 struct overlay_entry *old = entries; \
4739 entries = \
4740 (struct overlay_entry *) alloca (new_size \
4741 * sizeof *entries); \
4742 bcopy (old, entries, size * sizeof *entries); \
4743 size = new_size; \
4744 } \
4745 \
4746 entries[n].string = (STRING); \
4747 entries[n].overlay = (OVERLAY); \
4748 priority = Foverlay_get ((OVERLAY), Qpriority); \
4749 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4750 entries[n].after_string_p = (AFTER_P); \
4751 ++n; \
4752 } \
4753 while (0)
4754
4755 /* Process overlay before the overlay center. */
4756 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4757 {
4758 XSETMISC (overlay, ov);
4759 xassert (OVERLAYP (overlay));
4760 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4761 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4762
4763 if (end < charpos)
4764 break;
4765
4766 /* Skip this overlay if it doesn't start or end at IT's current
4767 position. */
4768 if (end != charpos && start != charpos)
4769 continue;
4770
4771 /* Skip this overlay if it doesn't apply to IT->w. */
4772 window = Foverlay_get (overlay, Qwindow);
4773 if (WINDOWP (window) && XWINDOW (window) != it->w)
4774 continue;
4775
4776 /* If the text ``under'' the overlay is invisible, both before-
4777 and after-strings from this overlay are visible; start and
4778 end position are indistinguishable. */
4779 invisible = Foverlay_get (overlay, Qinvisible);
4780 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4781
4782 /* If overlay has a non-empty before-string, record it. */
4783 if ((start == charpos || (end == charpos && invis_p))
4784 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4785 && SCHARS (str))
4786 RECORD_OVERLAY_STRING (overlay, str, 0);
4787
4788 /* If overlay has a non-empty after-string, record it. */
4789 if ((end == charpos || (start == charpos && invis_p))
4790 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4791 && SCHARS (str))
4792 RECORD_OVERLAY_STRING (overlay, str, 1);
4793 }
4794
4795 /* Process overlays after the overlay center. */
4796 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4797 {
4798 XSETMISC (overlay, ov);
4799 xassert (OVERLAYP (overlay));
4800 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4801 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4802
4803 if (start > charpos)
4804 break;
4805
4806 /* Skip this overlay if it doesn't start or end at IT's current
4807 position. */
4808 if (end != charpos && start != charpos)
4809 continue;
4810
4811 /* Skip this overlay if it doesn't apply to IT->w. */
4812 window = Foverlay_get (overlay, Qwindow);
4813 if (WINDOWP (window) && XWINDOW (window) != it->w)
4814 continue;
4815
4816 /* If the text ``under'' the overlay is invisible, it has a zero
4817 dimension, and both before- and after-strings apply. */
4818 invisible = Foverlay_get (overlay, Qinvisible);
4819 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4820
4821 /* If overlay has a non-empty before-string, record it. */
4822 if ((start == charpos || (end == charpos && invis_p))
4823 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4824 && SCHARS (str))
4825 RECORD_OVERLAY_STRING (overlay, str, 0);
4826
4827 /* If overlay has a non-empty after-string, record it. */
4828 if ((end == charpos || (start == charpos && invis_p))
4829 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4830 && SCHARS (str))
4831 RECORD_OVERLAY_STRING (overlay, str, 1);
4832 }
4833
4834 #undef RECORD_OVERLAY_STRING
4835
4836 /* Sort entries. */
4837 if (n > 1)
4838 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4839
4840 /* Record the total number of strings to process. */
4841 it->n_overlay_strings = n;
4842
4843 /* IT->current.overlay_string_index is the number of overlay strings
4844 that have already been consumed by IT. Copy some of the
4845 remaining overlay strings to IT->overlay_strings. */
4846 i = 0;
4847 j = it->current.overlay_string_index;
4848 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4849 it->overlay_strings[i++] = entries[j++].string;
4850
4851 CHECK_IT (it);
4852 }
4853
4854
4855 /* Get the first chunk of overlay strings at IT's current buffer
4856 position, or at CHARPOS if that is > 0. Value is non-zero if at
4857 least one overlay string was found. */
4858
4859 static int
4860 get_overlay_strings_1 (it, charpos, compute_stop_p)
4861 struct it *it;
4862 int charpos;
4863 {
4864 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4865 process. This fills IT->overlay_strings with strings, and sets
4866 IT->n_overlay_strings to the total number of strings to process.
4867 IT->pos.overlay_string_index has to be set temporarily to zero
4868 because load_overlay_strings needs this; it must be set to -1
4869 when no overlay strings are found because a zero value would
4870 indicate a position in the first overlay string. */
4871 it->current.overlay_string_index = 0;
4872 load_overlay_strings (it, charpos);
4873
4874 /* If we found overlay strings, set up IT to deliver display
4875 elements from the first one. Otherwise set up IT to deliver
4876 from current_buffer. */
4877 if (it->n_overlay_strings)
4878 {
4879 /* Make sure we know settings in current_buffer, so that we can
4880 restore meaningful values when we're done with the overlay
4881 strings. */
4882 if (compute_stop_p)
4883 compute_stop_pos (it);
4884 xassert (it->face_id >= 0);
4885
4886 /* Save IT's settings. They are restored after all overlay
4887 strings have been processed. */
4888 xassert (!compute_stop_p || it->sp == 0);
4889 push_it (it);
4890
4891 /* Set up IT to deliver display elements from the first overlay
4892 string. */
4893 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4894 it->string = it->overlay_strings[0];
4895 it->stop_charpos = 0;
4896 xassert (STRINGP (it->string));
4897 it->end_charpos = SCHARS (it->string);
4898 it->multibyte_p = STRING_MULTIBYTE (it->string);
4899 it->method = GET_FROM_STRING;
4900 return 1;
4901 }
4902
4903 it->current.overlay_string_index = -1;
4904 return 0;
4905 }
4906
4907 static int
4908 get_overlay_strings (it, charpos)
4909 struct it *it;
4910 int charpos;
4911 {
4912 it->string = Qnil;
4913 it->method = GET_FROM_BUFFER;
4914
4915 (void) get_overlay_strings_1 (it, charpos, 1);
4916
4917 CHECK_IT (it);
4918
4919 /* Value is non-zero if we found at least one overlay string. */
4920 return STRINGP (it->string);
4921 }
4922
4923
4924 \f
4925 /***********************************************************************
4926 Saving and restoring state
4927 ***********************************************************************/
4928
4929 /* Save current settings of IT on IT->stack. Called, for example,
4930 before setting up IT for an overlay string, to be able to restore
4931 IT's settings to what they were after the overlay string has been
4932 processed. */
4933
4934 static void
4935 push_it (it)
4936 struct it *it;
4937 {
4938 struct iterator_stack_entry *p;
4939
4940 xassert (it->sp < IT_STACK_SIZE);
4941 p = it->stack + it->sp;
4942
4943 p->stop_charpos = it->stop_charpos;
4944 xassert (it->face_id >= 0);
4945 p->face_id = it->face_id;
4946 p->string = it->string;
4947 p->method = it->method;
4948 switch (p->method)
4949 {
4950 case GET_FROM_IMAGE:
4951 p->u.image.object = it->object;
4952 p->u.image.image_id = it->image_id;
4953 p->u.image.slice = it->slice;
4954 break;
4955 case GET_FROM_COMPOSITION:
4956 p->u.comp.object = it->object;
4957 p->u.comp.c = it->c;
4958 p->u.comp.len = it->len;
4959 p->u.comp.cmp_id = it->cmp_id;
4960 p->u.comp.cmp_len = it->cmp_len;
4961 break;
4962 case GET_FROM_STRETCH:
4963 p->u.stretch.object = it->object;
4964 break;
4965 }
4966 p->position = it->position;
4967 p->current = it->current;
4968 p->end_charpos = it->end_charpos;
4969 p->string_nchars = it->string_nchars;
4970 p->area = it->area;
4971 p->multibyte_p = it->multibyte_p;
4972 p->space_width = it->space_width;
4973 p->font_height = it->font_height;
4974 p->voffset = it->voffset;
4975 p->string_from_display_prop_p = it->string_from_display_prop_p;
4976 p->display_ellipsis_p = 0;
4977 ++it->sp;
4978 }
4979
4980
4981 /* Restore IT's settings from IT->stack. Called, for example, when no
4982 more overlay strings must be processed, and we return to delivering
4983 display elements from a buffer, or when the end of a string from a
4984 `display' property is reached and we return to delivering display
4985 elements from an overlay string, or from a buffer. */
4986
4987 static void
4988 pop_it (it)
4989 struct it *it;
4990 {
4991 struct iterator_stack_entry *p;
4992
4993 xassert (it->sp > 0);
4994 --it->sp;
4995 p = it->stack + it->sp;
4996 it->stop_charpos = p->stop_charpos;
4997 it->face_id = p->face_id;
4998 it->current = p->current;
4999 it->position = p->position;
5000 it->string = p->string;
5001 if (NILP (it->string))
5002 SET_TEXT_POS (it->current.string_pos, -1, -1);
5003 it->method = p->method;
5004 switch (it->method)
5005 {
5006 case GET_FROM_IMAGE:
5007 it->image_id = p->u.image.image_id;
5008 it->object = p->u.image.object;
5009 it->slice = p->u.image.slice;
5010 break;
5011 case GET_FROM_COMPOSITION:
5012 it->object = p->u.comp.object;
5013 it->c = p->u.comp.c;
5014 it->len = p->u.comp.len;
5015 it->cmp_id = p->u.comp.cmp_id;
5016 it->cmp_len = p->u.comp.cmp_len;
5017 break;
5018 case GET_FROM_STRETCH:
5019 it->object = p->u.comp.object;
5020 break;
5021 case GET_FROM_BUFFER:
5022 it->object = it->w->buffer;
5023 break;
5024 case GET_FROM_STRING:
5025 it->object = it->string;
5026 break;
5027 }
5028 it->end_charpos = p->end_charpos;
5029 it->string_nchars = p->string_nchars;
5030 it->area = p->area;
5031 it->multibyte_p = p->multibyte_p;
5032 it->space_width = p->space_width;
5033 it->font_height = p->font_height;
5034 it->voffset = p->voffset;
5035 it->string_from_display_prop_p = p->string_from_display_prop_p;
5036 }
5037
5038
5039 \f
5040 /***********************************************************************
5041 Moving over lines
5042 ***********************************************************************/
5043
5044 /* Set IT's current position to the previous line start. */
5045
5046 static void
5047 back_to_previous_line_start (it)
5048 struct it *it;
5049 {
5050 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5051 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5052 }
5053
5054
5055 /* Move IT to the next line start.
5056
5057 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5058 we skipped over part of the text (as opposed to moving the iterator
5059 continuously over the text). Otherwise, don't change the value
5060 of *SKIPPED_P.
5061
5062 Newlines may come from buffer text, overlay strings, or strings
5063 displayed via the `display' property. That's the reason we can't
5064 simply use find_next_newline_no_quit.
5065
5066 Note that this function may not skip over invisible text that is so
5067 because of text properties and immediately follows a newline. If
5068 it would, function reseat_at_next_visible_line_start, when called
5069 from set_iterator_to_next, would effectively make invisible
5070 characters following a newline part of the wrong glyph row, which
5071 leads to wrong cursor motion. */
5072
5073 static int
5074 forward_to_next_line_start (it, skipped_p)
5075 struct it *it;
5076 int *skipped_p;
5077 {
5078 int old_selective, newline_found_p, n;
5079 const int MAX_NEWLINE_DISTANCE = 500;
5080
5081 /* If already on a newline, just consume it to avoid unintended
5082 skipping over invisible text below. */
5083 if (it->what == IT_CHARACTER
5084 && it->c == '\n'
5085 && CHARPOS (it->position) == IT_CHARPOS (*it))
5086 {
5087 set_iterator_to_next (it, 0);
5088 it->c = 0;
5089 return 1;
5090 }
5091
5092 /* Don't handle selective display in the following. It's (a)
5093 unnecessary because it's done by the caller, and (b) leads to an
5094 infinite recursion because next_element_from_ellipsis indirectly
5095 calls this function. */
5096 old_selective = it->selective;
5097 it->selective = 0;
5098
5099 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5100 from buffer text. */
5101 for (n = newline_found_p = 0;
5102 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5103 n += STRINGP (it->string) ? 0 : 1)
5104 {
5105 if (!get_next_display_element (it))
5106 return 0;
5107 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5108 set_iterator_to_next (it, 0);
5109 }
5110
5111 /* If we didn't find a newline near enough, see if we can use a
5112 short-cut. */
5113 if (!newline_found_p)
5114 {
5115 int start = IT_CHARPOS (*it);
5116 int limit = find_next_newline_no_quit (start, 1);
5117 Lisp_Object pos;
5118
5119 xassert (!STRINGP (it->string));
5120
5121 /* If there isn't any `display' property in sight, and no
5122 overlays, we can just use the position of the newline in
5123 buffer text. */
5124 if (it->stop_charpos >= limit
5125 || ((pos = Fnext_single_property_change (make_number (start),
5126 Qdisplay,
5127 Qnil, make_number (limit)),
5128 NILP (pos))
5129 && next_overlay_change (start) == ZV))
5130 {
5131 IT_CHARPOS (*it) = limit;
5132 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5133 *skipped_p = newline_found_p = 1;
5134 }
5135 else
5136 {
5137 while (get_next_display_element (it)
5138 && !newline_found_p)
5139 {
5140 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5141 set_iterator_to_next (it, 0);
5142 }
5143 }
5144 }
5145
5146 it->selective = old_selective;
5147 return newline_found_p;
5148 }
5149
5150
5151 /* Set IT's current position to the previous visible line start. Skip
5152 invisible text that is so either due to text properties or due to
5153 selective display. Caution: this does not change IT->current_x and
5154 IT->hpos. */
5155
5156 static void
5157 back_to_previous_visible_line_start (it)
5158 struct it *it;
5159 {
5160 while (IT_CHARPOS (*it) > BEGV)
5161 {
5162 back_to_previous_line_start (it);
5163
5164 if (IT_CHARPOS (*it) <= BEGV)
5165 break;
5166
5167 /* If selective > 0, then lines indented more than that values
5168 are invisible. */
5169 if (it->selective > 0
5170 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5171 (double) it->selective)) /* iftc */
5172 continue;
5173
5174 /* Check the newline before point for invisibility. */
5175 {
5176 Lisp_Object prop;
5177 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5178 Qinvisible, it->window);
5179 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5180 continue;
5181 }
5182
5183 if (IT_CHARPOS (*it) <= BEGV)
5184 break;
5185
5186 {
5187 struct it it2;
5188 int pos;
5189 int beg, end;
5190 Lisp_Object val, overlay;
5191
5192 /* If newline is part of a composition, continue from start of composition */
5193 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5194 && beg < IT_CHARPOS (*it))
5195 goto replaced;
5196
5197 /* If newline is replaced by a display property, find start of overlay
5198 or interval and continue search from that point. */
5199 it2 = *it;
5200 pos = --IT_CHARPOS (it2);
5201 --IT_BYTEPOS (it2);
5202 it2.sp = 0;
5203 if (handle_display_prop (&it2) == HANDLED_RETURN
5204 && !NILP (val = get_char_property_and_overlay
5205 (make_number (pos), Qdisplay, Qnil, &overlay))
5206 && (OVERLAYP (overlay)
5207 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5208 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5209 goto replaced;
5210
5211 /* Newline is not replaced by anything -- so we are done. */
5212 break;
5213
5214 replaced:
5215 if (beg < BEGV)
5216 beg = BEGV;
5217 IT_CHARPOS (*it) = beg;
5218 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5219 }
5220 }
5221
5222 it->continuation_lines_width = 0;
5223
5224 xassert (IT_CHARPOS (*it) >= BEGV);
5225 xassert (IT_CHARPOS (*it) == BEGV
5226 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5227 CHECK_IT (it);
5228 }
5229
5230
5231 /* Reseat iterator IT at the previous visible line start. Skip
5232 invisible text that is so either due to text properties or due to
5233 selective display. At the end, update IT's overlay information,
5234 face information etc. */
5235
5236 void
5237 reseat_at_previous_visible_line_start (it)
5238 struct it *it;
5239 {
5240 back_to_previous_visible_line_start (it);
5241 reseat (it, it->current.pos, 1);
5242 CHECK_IT (it);
5243 }
5244
5245
5246 /* Reseat iterator IT on the next visible line start in the current
5247 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5248 preceding the line start. Skip over invisible text that is so
5249 because of selective display. Compute faces, overlays etc at the
5250 new position. Note that this function does not skip over text that
5251 is invisible because of text properties. */
5252
5253 static void
5254 reseat_at_next_visible_line_start (it, on_newline_p)
5255 struct it *it;
5256 int on_newline_p;
5257 {
5258 int newline_found_p, skipped_p = 0;
5259
5260 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5261
5262 /* Skip over lines that are invisible because they are indented
5263 more than the value of IT->selective. */
5264 if (it->selective > 0)
5265 while (IT_CHARPOS (*it) < ZV
5266 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5267 (double) it->selective)) /* iftc */
5268 {
5269 xassert (IT_BYTEPOS (*it) == BEGV
5270 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5271 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5272 }
5273
5274 /* Position on the newline if that's what's requested. */
5275 if (on_newline_p && newline_found_p)
5276 {
5277 if (STRINGP (it->string))
5278 {
5279 if (IT_STRING_CHARPOS (*it) > 0)
5280 {
5281 --IT_STRING_CHARPOS (*it);
5282 --IT_STRING_BYTEPOS (*it);
5283 }
5284 }
5285 else if (IT_CHARPOS (*it) > BEGV)
5286 {
5287 --IT_CHARPOS (*it);
5288 --IT_BYTEPOS (*it);
5289 reseat (it, it->current.pos, 0);
5290 }
5291 }
5292 else if (skipped_p)
5293 reseat (it, it->current.pos, 0);
5294
5295 CHECK_IT (it);
5296 }
5297
5298
5299 \f
5300 /***********************************************************************
5301 Changing an iterator's position
5302 ***********************************************************************/
5303
5304 /* Change IT's current position to POS in current_buffer. If FORCE_P
5305 is non-zero, always check for text properties at the new position.
5306 Otherwise, text properties are only looked up if POS >=
5307 IT->check_charpos of a property. */
5308
5309 static void
5310 reseat (it, pos, force_p)
5311 struct it *it;
5312 struct text_pos pos;
5313 int force_p;
5314 {
5315 int original_pos = IT_CHARPOS (*it);
5316
5317 reseat_1 (it, pos, 0);
5318
5319 /* Determine where to check text properties. Avoid doing it
5320 where possible because text property lookup is very expensive. */
5321 if (force_p
5322 || CHARPOS (pos) > it->stop_charpos
5323 || CHARPOS (pos) < original_pos)
5324 handle_stop (it);
5325
5326 CHECK_IT (it);
5327 }
5328
5329
5330 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5331 IT->stop_pos to POS, also. */
5332
5333 static void
5334 reseat_1 (it, pos, set_stop_p)
5335 struct it *it;
5336 struct text_pos pos;
5337 int set_stop_p;
5338 {
5339 /* Don't call this function when scanning a C string. */
5340 xassert (it->s == NULL);
5341
5342 /* POS must be a reasonable value. */
5343 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5344
5345 it->current.pos = it->position = pos;
5346 it->end_charpos = ZV;
5347 it->dpvec = NULL;
5348 it->current.dpvec_index = -1;
5349 it->current.overlay_string_index = -1;
5350 IT_STRING_CHARPOS (*it) = -1;
5351 IT_STRING_BYTEPOS (*it) = -1;
5352 it->string = Qnil;
5353 it->method = GET_FROM_BUFFER;
5354 it->object = it->w->buffer;
5355 it->area = TEXT_AREA;
5356 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5357 it->sp = 0;
5358 it->string_from_display_prop_p = 0;
5359 it->face_before_selective_p = 0;
5360
5361 if (set_stop_p)
5362 it->stop_charpos = CHARPOS (pos);
5363 }
5364
5365
5366 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5367 If S is non-null, it is a C string to iterate over. Otherwise,
5368 STRING gives a Lisp string to iterate over.
5369
5370 If PRECISION > 0, don't return more then PRECISION number of
5371 characters from the string.
5372
5373 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5374 characters have been returned. FIELD_WIDTH < 0 means an infinite
5375 field width.
5376
5377 MULTIBYTE = 0 means disable processing of multibyte characters,
5378 MULTIBYTE > 0 means enable it,
5379 MULTIBYTE < 0 means use IT->multibyte_p.
5380
5381 IT must be initialized via a prior call to init_iterator before
5382 calling this function. */
5383
5384 static void
5385 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5386 struct it *it;
5387 unsigned char *s;
5388 Lisp_Object string;
5389 int charpos;
5390 int precision, field_width, multibyte;
5391 {
5392 /* No region in strings. */
5393 it->region_beg_charpos = it->region_end_charpos = -1;
5394
5395 /* No text property checks performed by default, but see below. */
5396 it->stop_charpos = -1;
5397
5398 /* Set iterator position and end position. */
5399 bzero (&it->current, sizeof it->current);
5400 it->current.overlay_string_index = -1;
5401 it->current.dpvec_index = -1;
5402 xassert (charpos >= 0);
5403
5404 /* If STRING is specified, use its multibyteness, otherwise use the
5405 setting of MULTIBYTE, if specified. */
5406 if (multibyte >= 0)
5407 it->multibyte_p = multibyte > 0;
5408
5409 if (s == NULL)
5410 {
5411 xassert (STRINGP (string));
5412 it->string = string;
5413 it->s = NULL;
5414 it->end_charpos = it->string_nchars = SCHARS (string);
5415 it->method = GET_FROM_STRING;
5416 it->current.string_pos = string_pos (charpos, string);
5417 }
5418 else
5419 {
5420 it->s = s;
5421 it->string = Qnil;
5422
5423 /* Note that we use IT->current.pos, not it->current.string_pos,
5424 for displaying C strings. */
5425 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5426 if (it->multibyte_p)
5427 {
5428 it->current.pos = c_string_pos (charpos, s, 1);
5429 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5430 }
5431 else
5432 {
5433 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5434 it->end_charpos = it->string_nchars = strlen (s);
5435 }
5436
5437 it->method = GET_FROM_C_STRING;
5438 }
5439
5440 /* PRECISION > 0 means don't return more than PRECISION characters
5441 from the string. */
5442 if (precision > 0 && it->end_charpos - charpos > precision)
5443 it->end_charpos = it->string_nchars = charpos + precision;
5444
5445 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5446 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5447 FIELD_WIDTH < 0 means infinite field width. This is useful for
5448 padding with `-' at the end of a mode line. */
5449 if (field_width < 0)
5450 field_width = INFINITY;
5451 if (field_width > it->end_charpos - charpos)
5452 it->end_charpos = charpos + field_width;
5453
5454 /* Use the standard display table for displaying strings. */
5455 if (DISP_TABLE_P (Vstandard_display_table))
5456 it->dp = XCHAR_TABLE (Vstandard_display_table);
5457
5458 it->stop_charpos = charpos;
5459 CHECK_IT (it);
5460 }
5461
5462
5463 \f
5464 /***********************************************************************
5465 Iteration
5466 ***********************************************************************/
5467
5468 /* Map enum it_method value to corresponding next_element_from_* function. */
5469
5470 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5471 {
5472 next_element_from_buffer,
5473 next_element_from_display_vector,
5474 next_element_from_composition,
5475 next_element_from_string,
5476 next_element_from_c_string,
5477 next_element_from_image,
5478 next_element_from_stretch
5479 };
5480
5481
5482 /* Load IT's display element fields with information about the next
5483 display element from the current position of IT. Value is zero if
5484 end of buffer (or C string) is reached. */
5485
5486 static struct frame *last_escape_glyph_frame = NULL;
5487 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5488 static int last_escape_glyph_merged_face_id = 0;
5489
5490 int
5491 get_next_display_element (it)
5492 struct it *it;
5493 {
5494 /* Non-zero means that we found a display element. Zero means that
5495 we hit the end of what we iterate over. Performance note: the
5496 function pointer `method' used here turns out to be faster than
5497 using a sequence of if-statements. */
5498 int success_p;
5499
5500 get_next:
5501 success_p = (*get_next_element[it->method]) (it);
5502
5503 if (it->what == IT_CHARACTER)
5504 {
5505 /* Map via display table or translate control characters.
5506 IT->c, IT->len etc. have been set to the next character by
5507 the function call above. If we have a display table, and it
5508 contains an entry for IT->c, translate it. Don't do this if
5509 IT->c itself comes from a display table, otherwise we could
5510 end up in an infinite recursion. (An alternative could be to
5511 count the recursion depth of this function and signal an
5512 error when a certain maximum depth is reached.) Is it worth
5513 it? */
5514 if (success_p && it->dpvec == NULL)
5515 {
5516 Lisp_Object dv;
5517
5518 if (it->dp
5519 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5520 VECTORP (dv)))
5521 {
5522 struct Lisp_Vector *v = XVECTOR (dv);
5523
5524 /* Return the first character from the display table
5525 entry, if not empty. If empty, don't display the
5526 current character. */
5527 if (v->size)
5528 {
5529 it->dpvec_char_len = it->len;
5530 it->dpvec = v->contents;
5531 it->dpend = v->contents + v->size;
5532 it->current.dpvec_index = 0;
5533 it->dpvec_face_id = -1;
5534 it->saved_face_id = it->face_id;
5535 it->method = GET_FROM_DISPLAY_VECTOR;
5536 it->ellipsis_p = 0;
5537 }
5538 else
5539 {
5540 set_iterator_to_next (it, 0);
5541 }
5542 goto get_next;
5543 }
5544
5545 /* Translate control characters into `\003' or `^C' form.
5546 Control characters coming from a display table entry are
5547 currently not translated because we use IT->dpvec to hold
5548 the translation. This could easily be changed but I
5549 don't believe that it is worth doing.
5550
5551 If it->multibyte_p is nonzero, eight-bit characters and
5552 non-printable multibyte characters are also translated to
5553 octal form.
5554
5555 If it->multibyte_p is zero, eight-bit characters that
5556 don't have corresponding multibyte char code are also
5557 translated to octal form. */
5558 else if ((it->c < ' '
5559 && (it->area != TEXT_AREA
5560 /* In mode line, treat \n like other crl chars. */
5561 || (it->c != '\t'
5562 && it->glyph_row && it->glyph_row->mode_line_p)
5563 || (it->c != '\n' && it->c != '\t')))
5564 || (it->multibyte_p
5565 ? ((it->c >= 127
5566 && it->len == 1)
5567 || !CHAR_PRINTABLE_P (it->c)
5568 || (!NILP (Vnobreak_char_display)
5569 && (it->c == 0x8a0 || it->c == 0x8ad
5570 || it->c == 0x920 || it->c == 0x92d
5571 || it->c == 0xe20 || it->c == 0xe2d
5572 || it->c == 0xf20 || it->c == 0xf2d)))
5573 : (it->c >= 127
5574 && (!unibyte_display_via_language_environment
5575 || it->c == unibyte_char_to_multibyte (it->c)))))
5576 {
5577 /* IT->c is a control character which must be displayed
5578 either as '\003' or as `^C' where the '\\' and '^'
5579 can be defined in the display table. Fill
5580 IT->ctl_chars with glyphs for what we have to
5581 display. Then, set IT->dpvec to these glyphs. */
5582 GLYPH g;
5583 int ctl_len;
5584 int face_id, lface_id = 0 ;
5585 GLYPH escape_glyph;
5586
5587 /* Handle control characters with ^. */
5588
5589 if (it->c < 128 && it->ctl_arrow_p)
5590 {
5591 g = '^'; /* default glyph for Control */
5592 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5593 if (it->dp
5594 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5595 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5596 {
5597 g = XINT (DISP_CTRL_GLYPH (it->dp));
5598 lface_id = FAST_GLYPH_FACE (g);
5599 }
5600 if (lface_id)
5601 {
5602 g = FAST_GLYPH_CHAR (g);
5603 face_id = merge_faces (it->f, Qt, lface_id,
5604 it->face_id);
5605 }
5606 else if (it->f == last_escape_glyph_frame
5607 && it->face_id == last_escape_glyph_face_id)
5608 {
5609 face_id = last_escape_glyph_merged_face_id;
5610 }
5611 else
5612 {
5613 /* Merge the escape-glyph face into the current face. */
5614 face_id = merge_faces (it->f, Qescape_glyph, 0,
5615 it->face_id);
5616 last_escape_glyph_frame = it->f;
5617 last_escape_glyph_face_id = it->face_id;
5618 last_escape_glyph_merged_face_id = face_id;
5619 }
5620
5621 XSETINT (it->ctl_chars[0], g);
5622 g = it->c ^ 0100;
5623 XSETINT (it->ctl_chars[1], g);
5624 ctl_len = 2;
5625 goto display_control;
5626 }
5627
5628 /* Handle non-break space in the mode where it only gets
5629 highlighting. */
5630
5631 if (EQ (Vnobreak_char_display, Qt)
5632 && (it->c == 0x8a0 || it->c == 0x920
5633 || it->c == 0xe20 || it->c == 0xf20))
5634 {
5635 /* Merge the no-break-space face into the current face. */
5636 face_id = merge_faces (it->f, Qnobreak_space, 0,
5637 it->face_id);
5638
5639 g = it->c = ' ';
5640 XSETINT (it->ctl_chars[0], g);
5641 ctl_len = 1;
5642 goto display_control;
5643 }
5644
5645 /* Handle sequences that start with the "escape glyph". */
5646
5647 /* the default escape glyph is \. */
5648 escape_glyph = '\\';
5649
5650 if (it->dp
5651 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5652 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5653 {
5654 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5655 lface_id = FAST_GLYPH_FACE (escape_glyph);
5656 }
5657 if (lface_id)
5658 {
5659 /* The display table specified a face.
5660 Merge it into face_id and also into escape_glyph. */
5661 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5662 face_id = merge_faces (it->f, Qt, lface_id,
5663 it->face_id);
5664 }
5665 else if (it->f == last_escape_glyph_frame
5666 && it->face_id == last_escape_glyph_face_id)
5667 {
5668 face_id = last_escape_glyph_merged_face_id;
5669 }
5670 else
5671 {
5672 /* Merge the escape-glyph face into the current face. */
5673 face_id = merge_faces (it->f, Qescape_glyph, 0,
5674 it->face_id);
5675 last_escape_glyph_frame = it->f;
5676 last_escape_glyph_face_id = it->face_id;
5677 last_escape_glyph_merged_face_id = face_id;
5678 }
5679
5680 /* Handle soft hyphens in the mode where they only get
5681 highlighting. */
5682
5683 if (EQ (Vnobreak_char_display, Qt)
5684 && (it->c == 0x8ad || it->c == 0x92d
5685 || it->c == 0xe2d || it->c == 0xf2d))
5686 {
5687 g = it->c = '-';
5688 XSETINT (it->ctl_chars[0], g);
5689 ctl_len = 1;
5690 goto display_control;
5691 }
5692
5693 /* Handle non-break space and soft hyphen
5694 with the escape glyph. */
5695
5696 if (it->c == 0x8a0 || it->c == 0x8ad
5697 || it->c == 0x920 || it->c == 0x92d
5698 || it->c == 0xe20 || it->c == 0xe2d
5699 || it->c == 0xf20 || it->c == 0xf2d)
5700 {
5701 XSETINT (it->ctl_chars[0], escape_glyph);
5702 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5703 XSETINT (it->ctl_chars[1], g);
5704 ctl_len = 2;
5705 goto display_control;
5706 }
5707
5708 {
5709 unsigned char str[MAX_MULTIBYTE_LENGTH];
5710 int len;
5711 int i;
5712
5713 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5714 if (SINGLE_BYTE_CHAR_P (it->c))
5715 str[0] = it->c, len = 1;
5716 else
5717 {
5718 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5719 if (len < 0)
5720 {
5721 /* It's an invalid character, which shouldn't
5722 happen actually, but due to bugs it may
5723 happen. Let's print the char as is, there's
5724 not much meaningful we can do with it. */
5725 str[0] = it->c;
5726 str[1] = it->c >> 8;
5727 str[2] = it->c >> 16;
5728 str[3] = it->c >> 24;
5729 len = 4;
5730 }
5731 }
5732
5733 for (i = 0; i < len; i++)
5734 {
5735 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5736 /* Insert three more glyphs into IT->ctl_chars for
5737 the octal display of the character. */
5738 g = ((str[i] >> 6) & 7) + '0';
5739 XSETINT (it->ctl_chars[i * 4 + 1], g);
5740 g = ((str[i] >> 3) & 7) + '0';
5741 XSETINT (it->ctl_chars[i * 4 + 2], g);
5742 g = (str[i] & 7) + '0';
5743 XSETINT (it->ctl_chars[i * 4 + 3], g);
5744 }
5745 ctl_len = len * 4;
5746 }
5747
5748 display_control:
5749 /* Set up IT->dpvec and return first character from it. */
5750 it->dpvec_char_len = it->len;
5751 it->dpvec = it->ctl_chars;
5752 it->dpend = it->dpvec + ctl_len;
5753 it->current.dpvec_index = 0;
5754 it->dpvec_face_id = face_id;
5755 it->saved_face_id = it->face_id;
5756 it->method = GET_FROM_DISPLAY_VECTOR;
5757 it->ellipsis_p = 0;
5758 goto get_next;
5759 }
5760 }
5761
5762 /* Adjust face id for a multibyte character. There are no
5763 multibyte character in unibyte text. */
5764 if (it->multibyte_p
5765 && success_p
5766 && FRAME_WINDOW_P (it->f))
5767 {
5768 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5769 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5770 }
5771 }
5772
5773 /* Is this character the last one of a run of characters with
5774 box? If yes, set IT->end_of_box_run_p to 1. */
5775 if (it->face_box_p
5776 && it->s == NULL)
5777 {
5778 int face_id;
5779 struct face *face;
5780
5781 it->end_of_box_run_p
5782 = ((face_id = face_after_it_pos (it),
5783 face_id != it->face_id)
5784 && (face = FACE_FROM_ID (it->f, face_id),
5785 face->box == FACE_NO_BOX));
5786 }
5787
5788 /* Value is 0 if end of buffer or string reached. */
5789 return success_p;
5790 }
5791
5792
5793 /* Move IT to the next display element.
5794
5795 RESEAT_P non-zero means if called on a newline in buffer text,
5796 skip to the next visible line start.
5797
5798 Functions get_next_display_element and set_iterator_to_next are
5799 separate because I find this arrangement easier to handle than a
5800 get_next_display_element function that also increments IT's
5801 position. The way it is we can first look at an iterator's current
5802 display element, decide whether it fits on a line, and if it does,
5803 increment the iterator position. The other way around we probably
5804 would either need a flag indicating whether the iterator has to be
5805 incremented the next time, or we would have to implement a
5806 decrement position function which would not be easy to write. */
5807
5808 void
5809 set_iterator_to_next (it, reseat_p)
5810 struct it *it;
5811 int reseat_p;
5812 {
5813 /* Reset flags indicating start and end of a sequence of characters
5814 with box. Reset them at the start of this function because
5815 moving the iterator to a new position might set them. */
5816 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5817
5818 switch (it->method)
5819 {
5820 case GET_FROM_BUFFER:
5821 /* The current display element of IT is a character from
5822 current_buffer. Advance in the buffer, and maybe skip over
5823 invisible lines that are so because of selective display. */
5824 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5825 reseat_at_next_visible_line_start (it, 0);
5826 else
5827 {
5828 xassert (it->len != 0);
5829 IT_BYTEPOS (*it) += it->len;
5830 IT_CHARPOS (*it) += 1;
5831 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5832 }
5833 break;
5834
5835 case GET_FROM_COMPOSITION:
5836 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5837 xassert (it->sp > 0);
5838 pop_it (it);
5839 if (it->method == GET_FROM_STRING)
5840 {
5841 IT_STRING_BYTEPOS (*it) += it->len;
5842 IT_STRING_CHARPOS (*it) += it->cmp_len;
5843 goto consider_string_end;
5844 }
5845 else if (it->method == GET_FROM_BUFFER)
5846 {
5847 IT_BYTEPOS (*it) += it->len;
5848 IT_CHARPOS (*it) += it->cmp_len;
5849 }
5850 break;
5851
5852 case GET_FROM_C_STRING:
5853 /* Current display element of IT is from a C string. */
5854 IT_BYTEPOS (*it) += it->len;
5855 IT_CHARPOS (*it) += 1;
5856 break;
5857
5858 case GET_FROM_DISPLAY_VECTOR:
5859 /* Current display element of IT is from a display table entry.
5860 Advance in the display table definition. Reset it to null if
5861 end reached, and continue with characters from buffers/
5862 strings. */
5863 ++it->current.dpvec_index;
5864
5865 /* Restore face of the iterator to what they were before the
5866 display vector entry (these entries may contain faces). */
5867 it->face_id = it->saved_face_id;
5868
5869 if (it->dpvec + it->current.dpvec_index == it->dpend)
5870 {
5871 int recheck_faces = it->ellipsis_p;
5872
5873 if (it->s)
5874 it->method = GET_FROM_C_STRING;
5875 else if (STRINGP (it->string))
5876 it->method = GET_FROM_STRING;
5877 else
5878 {
5879 it->method = GET_FROM_BUFFER;
5880 it->object = it->w->buffer;
5881 }
5882
5883 it->dpvec = NULL;
5884 it->current.dpvec_index = -1;
5885
5886 /* Skip over characters which were displayed via IT->dpvec. */
5887 if (it->dpvec_char_len < 0)
5888 reseat_at_next_visible_line_start (it, 1);
5889 else if (it->dpvec_char_len > 0)
5890 {
5891 if (it->method == GET_FROM_STRING
5892 && it->n_overlay_strings > 0)
5893 it->ignore_overlay_strings_at_pos_p = 1;
5894 it->len = it->dpvec_char_len;
5895 set_iterator_to_next (it, reseat_p);
5896 }
5897
5898 /* Maybe recheck faces after display vector */
5899 if (recheck_faces)
5900 it->stop_charpos = IT_CHARPOS (*it);
5901 }
5902 break;
5903
5904 case GET_FROM_STRING:
5905 /* Current display element is a character from a Lisp string. */
5906 xassert (it->s == NULL && STRINGP (it->string));
5907 IT_STRING_BYTEPOS (*it) += it->len;
5908 IT_STRING_CHARPOS (*it) += 1;
5909
5910 consider_string_end:
5911
5912 if (it->current.overlay_string_index >= 0)
5913 {
5914 /* IT->string is an overlay string. Advance to the
5915 next, if there is one. */
5916 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5917 next_overlay_string (it);
5918 }
5919 else
5920 {
5921 /* IT->string is not an overlay string. If we reached
5922 its end, and there is something on IT->stack, proceed
5923 with what is on the stack. This can be either another
5924 string, this time an overlay string, or a buffer. */
5925 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5926 && it->sp > 0)
5927 {
5928 pop_it (it);
5929 if (it->method == GET_FROM_STRING)
5930 goto consider_string_end;
5931 }
5932 }
5933 break;
5934
5935 case GET_FROM_IMAGE:
5936 case GET_FROM_STRETCH:
5937 /* The position etc with which we have to proceed are on
5938 the stack. The position may be at the end of a string,
5939 if the `display' property takes up the whole string. */
5940 xassert (it->sp > 0);
5941 pop_it (it);
5942 if (it->method == GET_FROM_STRING)
5943 goto consider_string_end;
5944 break;
5945
5946 default:
5947 /* There are no other methods defined, so this should be a bug. */
5948 abort ();
5949 }
5950
5951 xassert (it->method != GET_FROM_STRING
5952 || (STRINGP (it->string)
5953 && IT_STRING_CHARPOS (*it) >= 0));
5954 }
5955
5956 /* Load IT's display element fields with information about the next
5957 display element which comes from a display table entry or from the
5958 result of translating a control character to one of the forms `^C'
5959 or `\003'.
5960
5961 IT->dpvec holds the glyphs to return as characters.
5962 IT->saved_face_id holds the face id before the display vector--
5963 it is restored into IT->face_idin set_iterator_to_next. */
5964
5965 static int
5966 next_element_from_display_vector (it)
5967 struct it *it;
5968 {
5969 /* Precondition. */
5970 xassert (it->dpvec && it->current.dpvec_index >= 0);
5971
5972 it->face_id = it->saved_face_id;
5973
5974 if (INTEGERP (*it->dpvec)
5975 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5976 {
5977 GLYPH g;
5978
5979 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5980 it->c = FAST_GLYPH_CHAR (g);
5981 it->len = CHAR_BYTES (it->c);
5982
5983 /* The entry may contain a face id to use. Such a face id is
5984 the id of a Lisp face, not a realized face. A face id of
5985 zero means no face is specified. */
5986 if (it->dpvec_face_id >= 0)
5987 it->face_id = it->dpvec_face_id;
5988 else
5989 {
5990 int lface_id = FAST_GLYPH_FACE (g);
5991 if (lface_id > 0)
5992 it->face_id = merge_faces (it->f, Qt, lface_id,
5993 it->saved_face_id);
5994 }
5995 }
5996 else
5997 /* Display table entry is invalid. Return a space. */
5998 it->c = ' ', it->len = 1;
5999
6000 /* Don't change position and object of the iterator here. They are
6001 still the values of the character that had this display table
6002 entry or was translated, and that's what we want. */
6003 it->what = IT_CHARACTER;
6004 return 1;
6005 }
6006
6007
6008 /* Load IT with the next display element from Lisp string IT->string.
6009 IT->current.string_pos is the current position within the string.
6010 If IT->current.overlay_string_index >= 0, the Lisp string is an
6011 overlay string. */
6012
6013 static int
6014 next_element_from_string (it)
6015 struct it *it;
6016 {
6017 struct text_pos position;
6018
6019 xassert (STRINGP (it->string));
6020 xassert (IT_STRING_CHARPOS (*it) >= 0);
6021 position = it->current.string_pos;
6022
6023 /* Time to check for invisible text? */
6024 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6025 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6026 {
6027 handle_stop (it);
6028
6029 /* Since a handler may have changed IT->method, we must
6030 recurse here. */
6031 return get_next_display_element (it);
6032 }
6033
6034 if (it->current.overlay_string_index >= 0)
6035 {
6036 /* Get the next character from an overlay string. In overlay
6037 strings, There is no field width or padding with spaces to
6038 do. */
6039 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6040 {
6041 it->what = IT_EOB;
6042 return 0;
6043 }
6044 else if (STRING_MULTIBYTE (it->string))
6045 {
6046 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6047 const unsigned char *s = (SDATA (it->string)
6048 + IT_STRING_BYTEPOS (*it));
6049 it->c = string_char_and_length (s, remaining, &it->len);
6050 }
6051 else
6052 {
6053 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6054 it->len = 1;
6055 }
6056 }
6057 else
6058 {
6059 /* Get the next character from a Lisp string that is not an
6060 overlay string. Such strings come from the mode line, for
6061 example. We may have to pad with spaces, or truncate the
6062 string. See also next_element_from_c_string. */
6063 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6064 {
6065 it->what = IT_EOB;
6066 return 0;
6067 }
6068 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6069 {
6070 /* Pad with spaces. */
6071 it->c = ' ', it->len = 1;
6072 CHARPOS (position) = BYTEPOS (position) = -1;
6073 }
6074 else if (STRING_MULTIBYTE (it->string))
6075 {
6076 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6077 const unsigned char *s = (SDATA (it->string)
6078 + IT_STRING_BYTEPOS (*it));
6079 it->c = string_char_and_length (s, maxlen, &it->len);
6080 }
6081 else
6082 {
6083 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6084 it->len = 1;
6085 }
6086 }
6087
6088 /* Record what we have and where it came from. */
6089 it->what = IT_CHARACTER;
6090 it->object = it->string;
6091 it->position = position;
6092 return 1;
6093 }
6094
6095
6096 /* Load IT with next display element from C string IT->s.
6097 IT->string_nchars is the maximum number of characters to return
6098 from the string. IT->end_charpos may be greater than
6099 IT->string_nchars when this function is called, in which case we
6100 may have to return padding spaces. Value is zero if end of string
6101 reached, including padding spaces. */
6102
6103 static int
6104 next_element_from_c_string (it)
6105 struct it *it;
6106 {
6107 int success_p = 1;
6108
6109 xassert (it->s);
6110 it->what = IT_CHARACTER;
6111 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6112 it->object = Qnil;
6113
6114 /* IT's position can be greater IT->string_nchars in case a field
6115 width or precision has been specified when the iterator was
6116 initialized. */
6117 if (IT_CHARPOS (*it) >= it->end_charpos)
6118 {
6119 /* End of the game. */
6120 it->what = IT_EOB;
6121 success_p = 0;
6122 }
6123 else if (IT_CHARPOS (*it) >= it->string_nchars)
6124 {
6125 /* Pad with spaces. */
6126 it->c = ' ', it->len = 1;
6127 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6128 }
6129 else if (it->multibyte_p)
6130 {
6131 /* Implementation note: The calls to strlen apparently aren't a
6132 performance problem because there is no noticeable performance
6133 difference between Emacs running in unibyte or multibyte mode. */
6134 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6135 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6136 maxlen, &it->len);
6137 }
6138 else
6139 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6140
6141 return success_p;
6142 }
6143
6144
6145 /* Set up IT to return characters from an ellipsis, if appropriate.
6146 The definition of the ellipsis glyphs may come from a display table
6147 entry. This function Fills IT with the first glyph from the
6148 ellipsis if an ellipsis is to be displayed. */
6149
6150 static int
6151 next_element_from_ellipsis (it)
6152 struct it *it;
6153 {
6154 if (it->selective_display_ellipsis_p)
6155 setup_for_ellipsis (it, it->len);
6156 else
6157 {
6158 /* The face at the current position may be different from the
6159 face we find after the invisible text. Remember what it
6160 was in IT->saved_face_id, and signal that it's there by
6161 setting face_before_selective_p. */
6162 it->saved_face_id = it->face_id;
6163 it->method = GET_FROM_BUFFER;
6164 it->object = it->w->buffer;
6165 reseat_at_next_visible_line_start (it, 1);
6166 it->face_before_selective_p = 1;
6167 }
6168
6169 return get_next_display_element (it);
6170 }
6171
6172
6173 /* Deliver an image display element. The iterator IT is already
6174 filled with image information (done in handle_display_prop). Value
6175 is always 1. */
6176
6177
6178 static int
6179 next_element_from_image (it)
6180 struct it *it;
6181 {
6182 it->what = IT_IMAGE;
6183 return 1;
6184 }
6185
6186
6187 /* Fill iterator IT with next display element from a stretch glyph
6188 property. IT->object is the value of the text property. Value is
6189 always 1. */
6190
6191 static int
6192 next_element_from_stretch (it)
6193 struct it *it;
6194 {
6195 it->what = IT_STRETCH;
6196 return 1;
6197 }
6198
6199
6200 /* Load IT with the next display element from current_buffer. Value
6201 is zero if end of buffer reached. IT->stop_charpos is the next
6202 position at which to stop and check for text properties or buffer
6203 end. */
6204
6205 static int
6206 next_element_from_buffer (it)
6207 struct it *it;
6208 {
6209 int success_p = 1;
6210
6211 /* Check this assumption, otherwise, we would never enter the
6212 if-statement, below. */
6213 xassert (IT_CHARPOS (*it) >= BEGV
6214 && IT_CHARPOS (*it) <= it->stop_charpos);
6215
6216 if (IT_CHARPOS (*it) >= it->stop_charpos)
6217 {
6218 if (IT_CHARPOS (*it) >= it->end_charpos)
6219 {
6220 int overlay_strings_follow_p;
6221
6222 /* End of the game, except when overlay strings follow that
6223 haven't been returned yet. */
6224 if (it->overlay_strings_at_end_processed_p)
6225 overlay_strings_follow_p = 0;
6226 else
6227 {
6228 it->overlay_strings_at_end_processed_p = 1;
6229 overlay_strings_follow_p = get_overlay_strings (it, 0);
6230 }
6231
6232 if (overlay_strings_follow_p)
6233 success_p = get_next_display_element (it);
6234 else
6235 {
6236 it->what = IT_EOB;
6237 it->position = it->current.pos;
6238 success_p = 0;
6239 }
6240 }
6241 else
6242 {
6243 handle_stop (it);
6244 return get_next_display_element (it);
6245 }
6246 }
6247 else
6248 {
6249 /* No face changes, overlays etc. in sight, so just return a
6250 character from current_buffer. */
6251 unsigned char *p;
6252
6253 /* Maybe run the redisplay end trigger hook. Performance note:
6254 This doesn't seem to cost measurable time. */
6255 if (it->redisplay_end_trigger_charpos
6256 && it->glyph_row
6257 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6258 run_redisplay_end_trigger_hook (it);
6259
6260 /* Get the next character, maybe multibyte. */
6261 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6262 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6263 {
6264 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6265 - IT_BYTEPOS (*it));
6266 it->c = string_char_and_length (p, maxlen, &it->len);
6267 }
6268 else
6269 it->c = *p, it->len = 1;
6270
6271 /* Record what we have and where it came from. */
6272 it->what = IT_CHARACTER;;
6273 it->object = it->w->buffer;
6274 it->position = it->current.pos;
6275
6276 /* Normally we return the character found above, except when we
6277 really want to return an ellipsis for selective display. */
6278 if (it->selective)
6279 {
6280 if (it->c == '\n')
6281 {
6282 /* A value of selective > 0 means hide lines indented more
6283 than that number of columns. */
6284 if (it->selective > 0
6285 && IT_CHARPOS (*it) + 1 < ZV
6286 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6287 IT_BYTEPOS (*it) + 1,
6288 (double) it->selective)) /* iftc */
6289 {
6290 success_p = next_element_from_ellipsis (it);
6291 it->dpvec_char_len = -1;
6292 }
6293 }
6294 else if (it->c == '\r' && it->selective == -1)
6295 {
6296 /* A value of selective == -1 means that everything from the
6297 CR to the end of the line is invisible, with maybe an
6298 ellipsis displayed for it. */
6299 success_p = next_element_from_ellipsis (it);
6300 it->dpvec_char_len = -1;
6301 }
6302 }
6303 }
6304
6305 /* Value is zero if end of buffer reached. */
6306 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6307 return success_p;
6308 }
6309
6310
6311 /* Run the redisplay end trigger hook for IT. */
6312
6313 static void
6314 run_redisplay_end_trigger_hook (it)
6315 struct it *it;
6316 {
6317 Lisp_Object args[3];
6318
6319 /* IT->glyph_row should be non-null, i.e. we should be actually
6320 displaying something, or otherwise we should not run the hook. */
6321 xassert (it->glyph_row);
6322
6323 /* Set up hook arguments. */
6324 args[0] = Qredisplay_end_trigger_functions;
6325 args[1] = it->window;
6326 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6327 it->redisplay_end_trigger_charpos = 0;
6328
6329 /* Since we are *trying* to run these functions, don't try to run
6330 them again, even if they get an error. */
6331 it->w->redisplay_end_trigger = Qnil;
6332 Frun_hook_with_args (3, args);
6333
6334 /* Notice if it changed the face of the character we are on. */
6335 handle_face_prop (it);
6336 }
6337
6338
6339 /* Deliver a composition display element. The iterator IT is already
6340 filled with composition information (done in
6341 handle_composition_prop). Value is always 1. */
6342
6343 static int
6344 next_element_from_composition (it)
6345 struct it *it;
6346 {
6347 it->what = IT_COMPOSITION;
6348 it->position = (STRINGP (it->string)
6349 ? it->current.string_pos
6350 : it->current.pos);
6351 if (STRINGP (it->string))
6352 it->object = it->string;
6353 else
6354 it->object = it->w->buffer;
6355 return 1;
6356 }
6357
6358
6359 \f
6360 /***********************************************************************
6361 Moving an iterator without producing glyphs
6362 ***********************************************************************/
6363
6364 /* Check if iterator is at a position corresponding to a valid buffer
6365 position after some move_it_ call. */
6366
6367 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6368 ((it)->method == GET_FROM_STRING \
6369 ? IT_STRING_CHARPOS (*it) == 0 \
6370 : 1)
6371
6372
6373 /* Move iterator IT to a specified buffer or X position within one
6374 line on the display without producing glyphs.
6375
6376 OP should be a bit mask including some or all of these bits:
6377 MOVE_TO_X: Stop on reaching x-position TO_X.
6378 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6379 Regardless of OP's value, stop in reaching the end of the display line.
6380
6381 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6382 This means, in particular, that TO_X includes window's horizontal
6383 scroll amount.
6384
6385 The return value has several possible values that
6386 say what condition caused the scan to stop:
6387
6388 MOVE_POS_MATCH_OR_ZV
6389 - when TO_POS or ZV was reached.
6390
6391 MOVE_X_REACHED
6392 -when TO_X was reached before TO_POS or ZV were reached.
6393
6394 MOVE_LINE_CONTINUED
6395 - when we reached the end of the display area and the line must
6396 be continued.
6397
6398 MOVE_LINE_TRUNCATED
6399 - when we reached the end of the display area and the line is
6400 truncated.
6401
6402 MOVE_NEWLINE_OR_CR
6403 - when we stopped at a line end, i.e. a newline or a CR and selective
6404 display is on. */
6405
6406 static enum move_it_result
6407 move_it_in_display_line_to (it, to_charpos, to_x, op)
6408 struct it *it;
6409 int to_charpos, to_x, op;
6410 {
6411 enum move_it_result result = MOVE_UNDEFINED;
6412 struct glyph_row *saved_glyph_row;
6413
6414 /* Don't produce glyphs in produce_glyphs. */
6415 saved_glyph_row = it->glyph_row;
6416 it->glyph_row = NULL;
6417
6418 #define BUFFER_POS_REACHED_P() \
6419 ((op & MOVE_TO_POS) != 0 \
6420 && BUFFERP (it->object) \
6421 && IT_CHARPOS (*it) >= to_charpos \
6422 && (it->method == GET_FROM_BUFFER \
6423 || (it->method == GET_FROM_DISPLAY_VECTOR \
6424 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6425
6426
6427 while (1)
6428 {
6429 int x, i, ascent = 0, descent = 0;
6430
6431 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6432 if ((op & MOVE_TO_POS) != 0
6433 && BUFFERP (it->object)
6434 && it->method == GET_FROM_BUFFER
6435 && IT_CHARPOS (*it) > to_charpos)
6436 {
6437 result = MOVE_POS_MATCH_OR_ZV;
6438 break;
6439 }
6440
6441 /* Stop when ZV reached.
6442 We used to stop here when TO_CHARPOS reached as well, but that is
6443 too soon if this glyph does not fit on this line. So we handle it
6444 explicitly below. */
6445 if (!get_next_display_element (it)
6446 || (it->truncate_lines_p
6447 && BUFFER_POS_REACHED_P ()))
6448 {
6449 result = MOVE_POS_MATCH_OR_ZV;
6450 break;
6451 }
6452
6453 /* The call to produce_glyphs will get the metrics of the
6454 display element IT is loaded with. We record in x the
6455 x-position before this display element in case it does not
6456 fit on the line. */
6457 x = it->current_x;
6458
6459 /* Remember the line height so far in case the next element doesn't
6460 fit on the line. */
6461 if (!it->truncate_lines_p)
6462 {
6463 ascent = it->max_ascent;
6464 descent = it->max_descent;
6465 }
6466
6467 PRODUCE_GLYPHS (it);
6468
6469 if (it->area != TEXT_AREA)
6470 {
6471 set_iterator_to_next (it, 1);
6472 continue;
6473 }
6474
6475 /* The number of glyphs we get back in IT->nglyphs will normally
6476 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6477 character on a terminal frame, or (iii) a line end. For the
6478 second case, IT->nglyphs - 1 padding glyphs will be present
6479 (on X frames, there is only one glyph produced for a
6480 composite character.
6481
6482 The behavior implemented below means, for continuation lines,
6483 that as many spaces of a TAB as fit on the current line are
6484 displayed there. For terminal frames, as many glyphs of a
6485 multi-glyph character are displayed in the current line, too.
6486 This is what the old redisplay code did, and we keep it that
6487 way. Under X, the whole shape of a complex character must
6488 fit on the line or it will be completely displayed in the
6489 next line.
6490
6491 Note that both for tabs and padding glyphs, all glyphs have
6492 the same width. */
6493 if (it->nglyphs)
6494 {
6495 /* More than one glyph or glyph doesn't fit on line. All
6496 glyphs have the same width. */
6497 int single_glyph_width = it->pixel_width / it->nglyphs;
6498 int new_x;
6499 int x_before_this_char = x;
6500 int hpos_before_this_char = it->hpos;
6501
6502 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6503 {
6504 new_x = x + single_glyph_width;
6505
6506 /* We want to leave anything reaching TO_X to the caller. */
6507 if ((op & MOVE_TO_X) && new_x > to_x)
6508 {
6509 if (BUFFER_POS_REACHED_P ())
6510 goto buffer_pos_reached;
6511 it->current_x = x;
6512 result = MOVE_X_REACHED;
6513 break;
6514 }
6515 else if (/* Lines are continued. */
6516 !it->truncate_lines_p
6517 && (/* And glyph doesn't fit on the line. */
6518 new_x > it->last_visible_x
6519 /* Or it fits exactly and we're on a window
6520 system frame. */
6521 || (new_x == it->last_visible_x
6522 && FRAME_WINDOW_P (it->f))))
6523 {
6524 if (/* IT->hpos == 0 means the very first glyph
6525 doesn't fit on the line, e.g. a wide image. */
6526 it->hpos == 0
6527 || (new_x == it->last_visible_x
6528 && FRAME_WINDOW_P (it->f)))
6529 {
6530 ++it->hpos;
6531 it->current_x = new_x;
6532
6533 /* The character's last glyph just barely fits
6534 in this row. */
6535 if (i == it->nglyphs - 1)
6536 {
6537 /* If this is the destination position,
6538 return a position *before* it in this row,
6539 now that we know it fits in this row. */
6540 if (BUFFER_POS_REACHED_P ())
6541 {
6542 it->hpos = hpos_before_this_char;
6543 it->current_x = x_before_this_char;
6544 result = MOVE_POS_MATCH_OR_ZV;
6545 break;
6546 }
6547
6548 set_iterator_to_next (it, 1);
6549 #ifdef HAVE_WINDOW_SYSTEM
6550 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6551 {
6552 if (!get_next_display_element (it))
6553 {
6554 result = MOVE_POS_MATCH_OR_ZV;
6555 break;
6556 }
6557 if (BUFFER_POS_REACHED_P ())
6558 {
6559 if (ITERATOR_AT_END_OF_LINE_P (it))
6560 result = MOVE_POS_MATCH_OR_ZV;
6561 else
6562 result = MOVE_LINE_CONTINUED;
6563 break;
6564 }
6565 if (ITERATOR_AT_END_OF_LINE_P (it))
6566 {
6567 result = MOVE_NEWLINE_OR_CR;
6568 break;
6569 }
6570 }
6571 #endif /* HAVE_WINDOW_SYSTEM */
6572 }
6573 }
6574 else
6575 {
6576 it->current_x = x;
6577 it->max_ascent = ascent;
6578 it->max_descent = descent;
6579 }
6580
6581 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6582 IT_CHARPOS (*it)));
6583 result = MOVE_LINE_CONTINUED;
6584 break;
6585 }
6586 else if (BUFFER_POS_REACHED_P ())
6587 goto buffer_pos_reached;
6588 else if (new_x > it->first_visible_x)
6589 {
6590 /* Glyph is visible. Increment number of glyphs that
6591 would be displayed. */
6592 ++it->hpos;
6593 }
6594 else
6595 {
6596 /* Glyph is completely off the left margin of the display
6597 area. Nothing to do. */
6598 }
6599 }
6600
6601 if (result != MOVE_UNDEFINED)
6602 break;
6603 }
6604 else if (BUFFER_POS_REACHED_P ())
6605 {
6606 buffer_pos_reached:
6607 it->current_x = x;
6608 it->max_ascent = ascent;
6609 it->max_descent = descent;
6610 result = MOVE_POS_MATCH_OR_ZV;
6611 break;
6612 }
6613 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6614 {
6615 /* Stop when TO_X specified and reached. This check is
6616 necessary here because of lines consisting of a line end,
6617 only. The line end will not produce any glyphs and we
6618 would never get MOVE_X_REACHED. */
6619 xassert (it->nglyphs == 0);
6620 result = MOVE_X_REACHED;
6621 break;
6622 }
6623
6624 /* Is this a line end? If yes, we're done. */
6625 if (ITERATOR_AT_END_OF_LINE_P (it))
6626 {
6627 result = MOVE_NEWLINE_OR_CR;
6628 break;
6629 }
6630
6631 /* The current display element has been consumed. Advance
6632 to the next. */
6633 set_iterator_to_next (it, 1);
6634
6635 /* Stop if lines are truncated and IT's current x-position is
6636 past the right edge of the window now. */
6637 if (it->truncate_lines_p
6638 && it->current_x >= it->last_visible_x)
6639 {
6640 #ifdef HAVE_WINDOW_SYSTEM
6641 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6642 {
6643 if (!get_next_display_element (it)
6644 || BUFFER_POS_REACHED_P ())
6645 {
6646 result = MOVE_POS_MATCH_OR_ZV;
6647 break;
6648 }
6649 if (ITERATOR_AT_END_OF_LINE_P (it))
6650 {
6651 result = MOVE_NEWLINE_OR_CR;
6652 break;
6653 }
6654 }
6655 #endif /* HAVE_WINDOW_SYSTEM */
6656 result = MOVE_LINE_TRUNCATED;
6657 break;
6658 }
6659 }
6660
6661 #undef BUFFER_POS_REACHED_P
6662
6663 /* Restore the iterator settings altered at the beginning of this
6664 function. */
6665 it->glyph_row = saved_glyph_row;
6666 return result;
6667 }
6668
6669
6670 /* Move IT forward until it satisfies one or more of the criteria in
6671 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6672
6673 OP is a bit-mask that specifies where to stop, and in particular,
6674 which of those four position arguments makes a difference. See the
6675 description of enum move_operation_enum.
6676
6677 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6678 screen line, this function will set IT to the next position >
6679 TO_CHARPOS. */
6680
6681 void
6682 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6683 struct it *it;
6684 int to_charpos, to_x, to_y, to_vpos;
6685 int op;
6686 {
6687 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6688 int line_height;
6689 int reached = 0;
6690
6691 for (;;)
6692 {
6693 if (op & MOVE_TO_VPOS)
6694 {
6695 /* If no TO_CHARPOS and no TO_X specified, stop at the
6696 start of the line TO_VPOS. */
6697 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6698 {
6699 if (it->vpos == to_vpos)
6700 {
6701 reached = 1;
6702 break;
6703 }
6704 else
6705 skip = move_it_in_display_line_to (it, -1, -1, 0);
6706 }
6707 else
6708 {
6709 /* TO_VPOS >= 0 means stop at TO_X in the line at
6710 TO_VPOS, or at TO_POS, whichever comes first. */
6711 if (it->vpos == to_vpos)
6712 {
6713 reached = 2;
6714 break;
6715 }
6716
6717 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6718
6719 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6720 {
6721 reached = 3;
6722 break;
6723 }
6724 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6725 {
6726 /* We have reached TO_X but not in the line we want. */
6727 skip = move_it_in_display_line_to (it, to_charpos,
6728 -1, MOVE_TO_POS);
6729 if (skip == MOVE_POS_MATCH_OR_ZV)
6730 {
6731 reached = 4;
6732 break;
6733 }
6734 }
6735 }
6736 }
6737 else if (op & MOVE_TO_Y)
6738 {
6739 struct it it_backup;
6740
6741 /* TO_Y specified means stop at TO_X in the line containing
6742 TO_Y---or at TO_CHARPOS if this is reached first. The
6743 problem is that we can't really tell whether the line
6744 contains TO_Y before we have completely scanned it, and
6745 this may skip past TO_X. What we do is to first scan to
6746 TO_X.
6747
6748 If TO_X is not specified, use a TO_X of zero. The reason
6749 is to make the outcome of this function more predictable.
6750 If we didn't use TO_X == 0, we would stop at the end of
6751 the line which is probably not what a caller would expect
6752 to happen. */
6753 skip = move_it_in_display_line_to (it, to_charpos,
6754 ((op & MOVE_TO_X)
6755 ? to_x : 0),
6756 (MOVE_TO_X
6757 | (op & MOVE_TO_POS)));
6758
6759 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6760 if (skip == MOVE_POS_MATCH_OR_ZV)
6761 {
6762 reached = 5;
6763 break;
6764 }
6765
6766 /* If TO_X was reached, we would like to know whether TO_Y
6767 is in the line. This can only be said if we know the
6768 total line height which requires us to scan the rest of
6769 the line. */
6770 if (skip == MOVE_X_REACHED)
6771 {
6772 it_backup = *it;
6773 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6774 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6775 op & MOVE_TO_POS);
6776 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6777 }
6778
6779 /* Now, decide whether TO_Y is in this line. */
6780 line_height = it->max_ascent + it->max_descent;
6781 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6782
6783 if (to_y >= it->current_y
6784 && to_y < it->current_y + line_height)
6785 {
6786 if (skip == MOVE_X_REACHED)
6787 /* If TO_Y is in this line and TO_X was reached above,
6788 we scanned too far. We have to restore IT's settings
6789 to the ones before skipping. */
6790 *it = it_backup;
6791 reached = 6;
6792 }
6793 else if (skip == MOVE_X_REACHED)
6794 {
6795 skip = skip2;
6796 if (skip == MOVE_POS_MATCH_OR_ZV)
6797 reached = 7;
6798 }
6799
6800 if (reached)
6801 break;
6802 }
6803 else if (BUFFERP (it->object)
6804 && it->method == GET_FROM_BUFFER
6805 && IT_CHARPOS (*it) >= to_charpos)
6806 skip = MOVE_POS_MATCH_OR_ZV;
6807 else
6808 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6809
6810 switch (skip)
6811 {
6812 case MOVE_POS_MATCH_OR_ZV:
6813 reached = 8;
6814 goto out;
6815
6816 case MOVE_NEWLINE_OR_CR:
6817 set_iterator_to_next (it, 1);
6818 it->continuation_lines_width = 0;
6819 break;
6820
6821 case MOVE_LINE_TRUNCATED:
6822 it->continuation_lines_width = 0;
6823 reseat_at_next_visible_line_start (it, 0);
6824 if ((op & MOVE_TO_POS) != 0
6825 && IT_CHARPOS (*it) > to_charpos)
6826 {
6827 reached = 9;
6828 goto out;
6829 }
6830 break;
6831
6832 case MOVE_LINE_CONTINUED:
6833 /* For continued lines ending in a tab, some of the glyphs
6834 associated with the tab are displayed on the current
6835 line. Since it->current_x does not include these glyphs,
6836 we use it->last_visible_x instead. */
6837 it->continuation_lines_width +=
6838 (it->c == '\t') ? it->last_visible_x : it->current_x;
6839 break;
6840
6841 default:
6842 abort ();
6843 }
6844
6845 /* Reset/increment for the next run. */
6846 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6847 it->current_x = it->hpos = 0;
6848 it->current_y += it->max_ascent + it->max_descent;
6849 ++it->vpos;
6850 last_height = it->max_ascent + it->max_descent;
6851 last_max_ascent = it->max_ascent;
6852 it->max_ascent = it->max_descent = 0;
6853 }
6854
6855 out:
6856
6857 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6858 }
6859
6860
6861 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6862
6863 If DY > 0, move IT backward at least that many pixels. DY = 0
6864 means move IT backward to the preceding line start or BEGV. This
6865 function may move over more than DY pixels if IT->current_y - DY
6866 ends up in the middle of a line; in this case IT->current_y will be
6867 set to the top of the line moved to. */
6868
6869 void
6870 move_it_vertically_backward (it, dy)
6871 struct it *it;
6872 int dy;
6873 {
6874 int nlines, h;
6875 struct it it2, it3;
6876 int start_pos;
6877
6878 move_further_back:
6879 xassert (dy >= 0);
6880
6881 start_pos = IT_CHARPOS (*it);
6882
6883 /* Estimate how many newlines we must move back. */
6884 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6885
6886 /* Set the iterator's position that many lines back. */
6887 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6888 back_to_previous_visible_line_start (it);
6889
6890 /* Reseat the iterator here. When moving backward, we don't want
6891 reseat to skip forward over invisible text, set up the iterator
6892 to deliver from overlay strings at the new position etc. So,
6893 use reseat_1 here. */
6894 reseat_1 (it, it->current.pos, 1);
6895
6896 /* We are now surely at a line start. */
6897 it->current_x = it->hpos = 0;
6898 it->continuation_lines_width = 0;
6899
6900 /* Move forward and see what y-distance we moved. First move to the
6901 start of the next line so that we get its height. We need this
6902 height to be able to tell whether we reached the specified
6903 y-distance. */
6904 it2 = *it;
6905 it2.max_ascent = it2.max_descent = 0;
6906 do
6907 {
6908 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6909 MOVE_TO_POS | MOVE_TO_VPOS);
6910 }
6911 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6912 xassert (IT_CHARPOS (*it) >= BEGV);
6913 it3 = it2;
6914
6915 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6916 xassert (IT_CHARPOS (*it) >= BEGV);
6917 /* H is the actual vertical distance from the position in *IT
6918 and the starting position. */
6919 h = it2.current_y - it->current_y;
6920 /* NLINES is the distance in number of lines. */
6921 nlines = it2.vpos - it->vpos;
6922
6923 /* Correct IT's y and vpos position
6924 so that they are relative to the starting point. */
6925 it->vpos -= nlines;
6926 it->current_y -= h;
6927
6928 if (dy == 0)
6929 {
6930 /* DY == 0 means move to the start of the screen line. The
6931 value of nlines is > 0 if continuation lines were involved. */
6932 if (nlines > 0)
6933 move_it_by_lines (it, nlines, 1);
6934 #if 0
6935 /* I think this assert is bogus if buffer contains
6936 invisible text or images. KFS. */
6937 xassert (IT_CHARPOS (*it) <= start_pos);
6938 #endif
6939 }
6940 else
6941 {
6942 /* The y-position we try to reach, relative to *IT.
6943 Note that H has been subtracted in front of the if-statement. */
6944 int target_y = it->current_y + h - dy;
6945 int y0 = it3.current_y;
6946 int y1 = line_bottom_y (&it3);
6947 int line_height = y1 - y0;
6948
6949 /* If we did not reach target_y, try to move further backward if
6950 we can. If we moved too far backward, try to move forward. */
6951 if (target_y < it->current_y
6952 /* This is heuristic. In a window that's 3 lines high, with
6953 a line height of 13 pixels each, recentering with point
6954 on the bottom line will try to move -39/2 = 19 pixels
6955 backward. Try to avoid moving into the first line. */
6956 && (it->current_y - target_y
6957 > min (window_box_height (it->w), line_height * 2 / 3))
6958 && IT_CHARPOS (*it) > BEGV)
6959 {
6960 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6961 target_y - it->current_y));
6962 dy = it->current_y - target_y;
6963 goto move_further_back;
6964 }
6965 else if (target_y >= it->current_y + line_height
6966 && IT_CHARPOS (*it) < ZV)
6967 {
6968 /* Should move forward by at least one line, maybe more.
6969
6970 Note: Calling move_it_by_lines can be expensive on
6971 terminal frames, where compute_motion is used (via
6972 vmotion) to do the job, when there are very long lines
6973 and truncate-lines is nil. That's the reason for
6974 treating terminal frames specially here. */
6975
6976 if (!FRAME_WINDOW_P (it->f))
6977 move_it_vertically (it, target_y - (it->current_y + line_height));
6978 else
6979 {
6980 do
6981 {
6982 move_it_by_lines (it, 1, 1);
6983 }
6984 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6985 }
6986
6987 #if 0
6988 /* I think this assert is bogus if buffer contains
6989 invisible text or images. KFS. */
6990 xassert (IT_CHARPOS (*it) >= BEGV);
6991 #endif
6992 }
6993 }
6994 }
6995
6996
6997 /* Move IT by a specified amount of pixel lines DY. DY negative means
6998 move backwards. DY = 0 means move to start of screen line. At the
6999 end, IT will be on the start of a screen line. */
7000
7001 void
7002 move_it_vertically (it, dy)
7003 struct it *it;
7004 int dy;
7005 {
7006 if (dy <= 0)
7007 move_it_vertically_backward (it, -dy);
7008 else
7009 {
7010 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7011 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7012 MOVE_TO_POS | MOVE_TO_Y);
7013 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7014
7015 /* If buffer ends in ZV without a newline, move to the start of
7016 the line to satisfy the post-condition. */
7017 if (IT_CHARPOS (*it) == ZV
7018 && ZV > BEGV
7019 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7020 move_it_by_lines (it, 0, 0);
7021 }
7022 }
7023
7024
7025 /* Move iterator IT past the end of the text line it is in. */
7026
7027 void
7028 move_it_past_eol (it)
7029 struct it *it;
7030 {
7031 enum move_it_result rc;
7032
7033 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7034 if (rc == MOVE_NEWLINE_OR_CR)
7035 set_iterator_to_next (it, 0);
7036 }
7037
7038
7039 #if 0 /* Currently not used. */
7040
7041 /* Return non-zero if some text between buffer positions START_CHARPOS
7042 and END_CHARPOS is invisible. IT->window is the window for text
7043 property lookup. */
7044
7045 static int
7046 invisible_text_between_p (it, start_charpos, end_charpos)
7047 struct it *it;
7048 int start_charpos, end_charpos;
7049 {
7050 Lisp_Object prop, limit;
7051 int invisible_found_p;
7052
7053 xassert (it != NULL && start_charpos <= end_charpos);
7054
7055 /* Is text at START invisible? */
7056 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7057 it->window);
7058 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7059 invisible_found_p = 1;
7060 else
7061 {
7062 limit = Fnext_single_char_property_change (make_number (start_charpos),
7063 Qinvisible, Qnil,
7064 make_number (end_charpos));
7065 invisible_found_p = XFASTINT (limit) < end_charpos;
7066 }
7067
7068 return invisible_found_p;
7069 }
7070
7071 #endif /* 0 */
7072
7073
7074 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7075 negative means move up. DVPOS == 0 means move to the start of the
7076 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7077 NEED_Y_P is zero, IT->current_y will be left unchanged.
7078
7079 Further optimization ideas: If we would know that IT->f doesn't use
7080 a face with proportional font, we could be faster for
7081 truncate-lines nil. */
7082
7083 void
7084 move_it_by_lines (it, dvpos, need_y_p)
7085 struct it *it;
7086 int dvpos, need_y_p;
7087 {
7088 struct position pos;
7089
7090 if (!FRAME_WINDOW_P (it->f))
7091 {
7092 struct text_pos textpos;
7093
7094 /* We can use vmotion on frames without proportional fonts. */
7095 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7096 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7097 reseat (it, textpos, 1);
7098 it->vpos += pos.vpos;
7099 it->current_y += pos.vpos;
7100 }
7101 else if (dvpos == 0)
7102 {
7103 /* DVPOS == 0 means move to the start of the screen line. */
7104 move_it_vertically_backward (it, 0);
7105 xassert (it->current_x == 0 && it->hpos == 0);
7106 /* Let next call to line_bottom_y calculate real line height */
7107 last_height = 0;
7108 }
7109 else if (dvpos > 0)
7110 {
7111 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7112 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7113 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7114 }
7115 else
7116 {
7117 struct it it2;
7118 int start_charpos, i;
7119
7120 /* Start at the beginning of the screen line containing IT's
7121 position. This may actually move vertically backwards,
7122 in case of overlays, so adjust dvpos accordingly. */
7123 dvpos += it->vpos;
7124 move_it_vertically_backward (it, 0);
7125 dvpos -= it->vpos;
7126
7127 /* Go back -DVPOS visible lines and reseat the iterator there. */
7128 start_charpos = IT_CHARPOS (*it);
7129 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7130 back_to_previous_visible_line_start (it);
7131 reseat (it, it->current.pos, 1);
7132
7133 /* Move further back if we end up in a string or an image. */
7134 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7135 {
7136 /* First try to move to start of display line. */
7137 dvpos += it->vpos;
7138 move_it_vertically_backward (it, 0);
7139 dvpos -= it->vpos;
7140 if (IT_POS_VALID_AFTER_MOVE_P (it))
7141 break;
7142 /* If start of line is still in string or image,
7143 move further back. */
7144 back_to_previous_visible_line_start (it);
7145 reseat (it, it->current.pos, 1);
7146 dvpos--;
7147 }
7148
7149 it->current_x = it->hpos = 0;
7150
7151 /* Above call may have moved too far if continuation lines
7152 are involved. Scan forward and see if it did. */
7153 it2 = *it;
7154 it2.vpos = it2.current_y = 0;
7155 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7156 it->vpos -= it2.vpos;
7157 it->current_y -= it2.current_y;
7158 it->current_x = it->hpos = 0;
7159
7160 /* If we moved too far back, move IT some lines forward. */
7161 if (it2.vpos > -dvpos)
7162 {
7163 int delta = it2.vpos + dvpos;
7164 it2 = *it;
7165 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7166 /* Move back again if we got too far ahead. */
7167 if (IT_CHARPOS (*it) >= start_charpos)
7168 *it = it2;
7169 }
7170 }
7171 }
7172
7173 /* Return 1 if IT points into the middle of a display vector. */
7174
7175 int
7176 in_display_vector_p (it)
7177 struct it *it;
7178 {
7179 return (it->method == GET_FROM_DISPLAY_VECTOR
7180 && it->current.dpvec_index > 0
7181 && it->dpvec + it->current.dpvec_index != it->dpend);
7182 }
7183
7184 \f
7185 /***********************************************************************
7186 Messages
7187 ***********************************************************************/
7188
7189
7190 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7191 to *Messages*. */
7192
7193 void
7194 add_to_log (format, arg1, arg2)
7195 char *format;
7196 Lisp_Object arg1, arg2;
7197 {
7198 Lisp_Object args[3];
7199 Lisp_Object msg, fmt;
7200 char *buffer;
7201 int len;
7202 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7203 USE_SAFE_ALLOCA;
7204
7205 /* Do nothing if called asynchronously. Inserting text into
7206 a buffer may call after-change-functions and alike and
7207 that would means running Lisp asynchronously. */
7208 if (handling_signal)
7209 return;
7210
7211 fmt = msg = Qnil;
7212 GCPRO4 (fmt, msg, arg1, arg2);
7213
7214 args[0] = fmt = build_string (format);
7215 args[1] = arg1;
7216 args[2] = arg2;
7217 msg = Fformat (3, args);
7218
7219 len = SBYTES (msg) + 1;
7220 SAFE_ALLOCA (buffer, char *, len);
7221 bcopy (SDATA (msg), buffer, len);
7222
7223 message_dolog (buffer, len - 1, 1, 0);
7224 SAFE_FREE ();
7225
7226 UNGCPRO;
7227 }
7228
7229
7230 /* Output a newline in the *Messages* buffer if "needs" one. */
7231
7232 void
7233 message_log_maybe_newline ()
7234 {
7235 if (message_log_need_newline)
7236 message_dolog ("", 0, 1, 0);
7237 }
7238
7239
7240 /* Add a string M of length NBYTES to the message log, optionally
7241 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7242 nonzero, means interpret the contents of M as multibyte. This
7243 function calls low-level routines in order to bypass text property
7244 hooks, etc. which might not be safe to run.
7245
7246 This may GC (insert may run before/after change hooks),
7247 so the buffer M must NOT point to a Lisp string. */
7248
7249 void
7250 message_dolog (m, nbytes, nlflag, multibyte)
7251 const char *m;
7252 int nbytes, nlflag, multibyte;
7253 {
7254 if (!NILP (Vmemory_full))
7255 return;
7256
7257 if (!NILP (Vmessage_log_max))
7258 {
7259 struct buffer *oldbuf;
7260 Lisp_Object oldpoint, oldbegv, oldzv;
7261 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7262 int point_at_end = 0;
7263 int zv_at_end = 0;
7264 Lisp_Object old_deactivate_mark, tem;
7265 struct gcpro gcpro1;
7266
7267 old_deactivate_mark = Vdeactivate_mark;
7268 oldbuf = current_buffer;
7269 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7270 current_buffer->undo_list = Qt;
7271
7272 oldpoint = message_dolog_marker1;
7273 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7274 oldbegv = message_dolog_marker2;
7275 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7276 oldzv = message_dolog_marker3;
7277 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7278 GCPRO1 (old_deactivate_mark);
7279
7280 if (PT == Z)
7281 point_at_end = 1;
7282 if (ZV == Z)
7283 zv_at_end = 1;
7284
7285 BEGV = BEG;
7286 BEGV_BYTE = BEG_BYTE;
7287 ZV = Z;
7288 ZV_BYTE = Z_BYTE;
7289 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7290
7291 /* Insert the string--maybe converting multibyte to single byte
7292 or vice versa, so that all the text fits the buffer. */
7293 if (multibyte
7294 && NILP (current_buffer->enable_multibyte_characters))
7295 {
7296 int i, c, char_bytes;
7297 unsigned char work[1];
7298
7299 /* Convert a multibyte string to single-byte
7300 for the *Message* buffer. */
7301 for (i = 0; i < nbytes; i += char_bytes)
7302 {
7303 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7304 work[0] = (SINGLE_BYTE_CHAR_P (c)
7305 ? c
7306 : multibyte_char_to_unibyte (c, Qnil));
7307 insert_1_both (work, 1, 1, 1, 0, 0);
7308 }
7309 }
7310 else if (! multibyte
7311 && ! NILP (current_buffer->enable_multibyte_characters))
7312 {
7313 int i, c, char_bytes;
7314 unsigned char *msg = (unsigned char *) m;
7315 unsigned char str[MAX_MULTIBYTE_LENGTH];
7316 /* Convert a single-byte string to multibyte
7317 for the *Message* buffer. */
7318 for (i = 0; i < nbytes; i++)
7319 {
7320 c = unibyte_char_to_multibyte (msg[i]);
7321 char_bytes = CHAR_STRING (c, str);
7322 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7323 }
7324 }
7325 else if (nbytes)
7326 insert_1 (m, nbytes, 1, 0, 0);
7327
7328 if (nlflag)
7329 {
7330 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7331 insert_1 ("\n", 1, 1, 0, 0);
7332
7333 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7334 this_bol = PT;
7335 this_bol_byte = PT_BYTE;
7336
7337 /* See if this line duplicates the previous one.
7338 If so, combine duplicates. */
7339 if (this_bol > BEG)
7340 {
7341 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7342 prev_bol = PT;
7343 prev_bol_byte = PT_BYTE;
7344
7345 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7346 this_bol, this_bol_byte);
7347 if (dup)
7348 {
7349 del_range_both (prev_bol, prev_bol_byte,
7350 this_bol, this_bol_byte, 0);
7351 if (dup > 1)
7352 {
7353 char dupstr[40];
7354 int duplen;
7355
7356 /* If you change this format, don't forget to also
7357 change message_log_check_duplicate. */
7358 sprintf (dupstr, " [%d times]", dup);
7359 duplen = strlen (dupstr);
7360 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7361 insert_1 (dupstr, duplen, 1, 0, 1);
7362 }
7363 }
7364 }
7365
7366 /* If we have more than the desired maximum number of lines
7367 in the *Messages* buffer now, delete the oldest ones.
7368 This is safe because we don't have undo in this buffer. */
7369
7370 if (NATNUMP (Vmessage_log_max))
7371 {
7372 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7373 -XFASTINT (Vmessage_log_max) - 1, 0);
7374 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7375 }
7376 }
7377 BEGV = XMARKER (oldbegv)->charpos;
7378 BEGV_BYTE = marker_byte_position (oldbegv);
7379
7380 if (zv_at_end)
7381 {
7382 ZV = Z;
7383 ZV_BYTE = Z_BYTE;
7384 }
7385 else
7386 {
7387 ZV = XMARKER (oldzv)->charpos;
7388 ZV_BYTE = marker_byte_position (oldzv);
7389 }
7390
7391 if (point_at_end)
7392 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7393 else
7394 /* We can't do Fgoto_char (oldpoint) because it will run some
7395 Lisp code. */
7396 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7397 XMARKER (oldpoint)->bytepos);
7398
7399 UNGCPRO;
7400 unchain_marker (XMARKER (oldpoint));
7401 unchain_marker (XMARKER (oldbegv));
7402 unchain_marker (XMARKER (oldzv));
7403
7404 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7405 set_buffer_internal (oldbuf);
7406 if (NILP (tem))
7407 windows_or_buffers_changed = old_windows_or_buffers_changed;
7408 message_log_need_newline = !nlflag;
7409 Vdeactivate_mark = old_deactivate_mark;
7410 }
7411 }
7412
7413
7414 /* We are at the end of the buffer after just having inserted a newline.
7415 (Note: We depend on the fact we won't be crossing the gap.)
7416 Check to see if the most recent message looks a lot like the previous one.
7417 Return 0 if different, 1 if the new one should just replace it, or a
7418 value N > 1 if we should also append " [N times]". */
7419
7420 static int
7421 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7422 int prev_bol, this_bol;
7423 int prev_bol_byte, this_bol_byte;
7424 {
7425 int i;
7426 int len = Z_BYTE - 1 - this_bol_byte;
7427 int seen_dots = 0;
7428 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7429 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7430
7431 for (i = 0; i < len; i++)
7432 {
7433 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7434 seen_dots = 1;
7435 if (p1[i] != p2[i])
7436 return seen_dots;
7437 }
7438 p1 += len;
7439 if (*p1 == '\n')
7440 return 2;
7441 if (*p1++ == ' ' && *p1++ == '[')
7442 {
7443 int n = 0;
7444 while (*p1 >= '0' && *p1 <= '9')
7445 n = n * 10 + *p1++ - '0';
7446 if (strncmp (p1, " times]\n", 8) == 0)
7447 return n+1;
7448 }
7449 return 0;
7450 }
7451 \f
7452
7453 /* Display an echo area message M with a specified length of NBYTES
7454 bytes. The string may include null characters. If M is 0, clear
7455 out any existing message, and let the mini-buffer text show
7456 through.
7457
7458 This may GC, so the buffer M must NOT point to a Lisp string. */
7459
7460 void
7461 message2 (m, nbytes, multibyte)
7462 const char *m;
7463 int nbytes;
7464 int multibyte;
7465 {
7466 /* First flush out any partial line written with print. */
7467 message_log_maybe_newline ();
7468 if (m)
7469 message_dolog (m, nbytes, 1, multibyte);
7470 message2_nolog (m, nbytes, multibyte);
7471 }
7472
7473
7474 /* The non-logging counterpart of message2. */
7475
7476 void
7477 message2_nolog (m, nbytes, multibyte)
7478 const char *m;
7479 int nbytes, multibyte;
7480 {
7481 struct frame *sf = SELECTED_FRAME ();
7482 message_enable_multibyte = multibyte;
7483
7484 if (noninteractive)
7485 {
7486 if (noninteractive_need_newline)
7487 putc ('\n', stderr);
7488 noninteractive_need_newline = 0;
7489 if (m)
7490 fwrite (m, nbytes, 1, stderr);
7491 if (cursor_in_echo_area == 0)
7492 fprintf (stderr, "\n");
7493 fflush (stderr);
7494 }
7495 /* A null message buffer means that the frame hasn't really been
7496 initialized yet. Error messages get reported properly by
7497 cmd_error, so this must be just an informative message; toss it. */
7498 else if (INTERACTIVE
7499 && sf->glyphs_initialized_p
7500 && FRAME_MESSAGE_BUF (sf))
7501 {
7502 Lisp_Object mini_window;
7503 struct frame *f;
7504
7505 /* Get the frame containing the mini-buffer
7506 that the selected frame is using. */
7507 mini_window = FRAME_MINIBUF_WINDOW (sf);
7508 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7509
7510 FRAME_SAMPLE_VISIBILITY (f);
7511 if (FRAME_VISIBLE_P (sf)
7512 && ! FRAME_VISIBLE_P (f))
7513 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7514
7515 if (m)
7516 {
7517 set_message (m, Qnil, nbytes, multibyte);
7518 if (minibuffer_auto_raise)
7519 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7520 }
7521 else
7522 clear_message (1, 1);
7523
7524 do_pending_window_change (0);
7525 echo_area_display (1);
7526 do_pending_window_change (0);
7527 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7528 (*frame_up_to_date_hook) (f);
7529 }
7530 }
7531
7532
7533 /* Display an echo area message M with a specified length of NBYTES
7534 bytes. The string may include null characters. If M is not a
7535 string, clear out any existing message, and let the mini-buffer
7536 text show through.
7537
7538 This function cancels echoing. */
7539
7540 void
7541 message3 (m, nbytes, multibyte)
7542 Lisp_Object m;
7543 int nbytes;
7544 int multibyte;
7545 {
7546 struct gcpro gcpro1;
7547
7548 GCPRO1 (m);
7549 clear_message (1,1);
7550 cancel_echoing ();
7551
7552 /* First flush out any partial line written with print. */
7553 message_log_maybe_newline ();
7554 if (STRINGP (m))
7555 {
7556 char *buffer;
7557 USE_SAFE_ALLOCA;
7558
7559 SAFE_ALLOCA (buffer, char *, nbytes);
7560 bcopy (SDATA (m), buffer, nbytes);
7561 message_dolog (buffer, nbytes, 1, multibyte);
7562 SAFE_FREE ();
7563 }
7564 message3_nolog (m, nbytes, multibyte);
7565
7566 UNGCPRO;
7567 }
7568
7569
7570 /* The non-logging version of message3.
7571 This does not cancel echoing, because it is used for echoing.
7572 Perhaps we need to make a separate function for echoing
7573 and make this cancel echoing. */
7574
7575 void
7576 message3_nolog (m, nbytes, multibyte)
7577 Lisp_Object m;
7578 int nbytes, multibyte;
7579 {
7580 struct frame *sf = SELECTED_FRAME ();
7581 message_enable_multibyte = multibyte;
7582
7583 if (noninteractive)
7584 {
7585 if (noninteractive_need_newline)
7586 putc ('\n', stderr);
7587 noninteractive_need_newline = 0;
7588 if (STRINGP (m))
7589 fwrite (SDATA (m), nbytes, 1, stderr);
7590 if (cursor_in_echo_area == 0)
7591 fprintf (stderr, "\n");
7592 fflush (stderr);
7593 }
7594 /* A null message buffer means that the frame hasn't really been
7595 initialized yet. Error messages get reported properly by
7596 cmd_error, so this must be just an informative message; toss it. */
7597 else if (INTERACTIVE
7598 && sf->glyphs_initialized_p
7599 && FRAME_MESSAGE_BUF (sf))
7600 {
7601 Lisp_Object mini_window;
7602 Lisp_Object frame;
7603 struct frame *f;
7604
7605 /* Get the frame containing the mini-buffer
7606 that the selected frame is using. */
7607 mini_window = FRAME_MINIBUF_WINDOW (sf);
7608 frame = XWINDOW (mini_window)->frame;
7609 f = XFRAME (frame);
7610
7611 FRAME_SAMPLE_VISIBILITY (f);
7612 if (FRAME_VISIBLE_P (sf)
7613 && !FRAME_VISIBLE_P (f))
7614 Fmake_frame_visible (frame);
7615
7616 if (STRINGP (m) && SCHARS (m) > 0)
7617 {
7618 set_message (NULL, m, nbytes, multibyte);
7619 if (minibuffer_auto_raise)
7620 Fraise_frame (frame);
7621 /* Assume we are not echoing.
7622 (If we are, echo_now will override this.) */
7623 echo_message_buffer = Qnil;
7624 }
7625 else
7626 clear_message (1, 1);
7627
7628 do_pending_window_change (0);
7629 echo_area_display (1);
7630 do_pending_window_change (0);
7631 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7632 (*frame_up_to_date_hook) (f);
7633 }
7634 }
7635
7636
7637 /* Display a null-terminated echo area message M. If M is 0, clear
7638 out any existing message, and let the mini-buffer text show through.
7639
7640 The buffer M must continue to exist until after the echo area gets
7641 cleared or some other message gets displayed there. Do not pass
7642 text that is stored in a Lisp string. Do not pass text in a buffer
7643 that was alloca'd. */
7644
7645 void
7646 message1 (m)
7647 char *m;
7648 {
7649 message2 (m, (m ? strlen (m) : 0), 0);
7650 }
7651
7652
7653 /* The non-logging counterpart of message1. */
7654
7655 void
7656 message1_nolog (m)
7657 char *m;
7658 {
7659 message2_nolog (m, (m ? strlen (m) : 0), 0);
7660 }
7661
7662 /* Display a message M which contains a single %s
7663 which gets replaced with STRING. */
7664
7665 void
7666 message_with_string (m, string, log)
7667 char *m;
7668 Lisp_Object string;
7669 int log;
7670 {
7671 CHECK_STRING (string);
7672
7673 if (noninteractive)
7674 {
7675 if (m)
7676 {
7677 if (noninteractive_need_newline)
7678 putc ('\n', stderr);
7679 noninteractive_need_newline = 0;
7680 fprintf (stderr, m, SDATA (string));
7681 if (cursor_in_echo_area == 0)
7682 fprintf (stderr, "\n");
7683 fflush (stderr);
7684 }
7685 }
7686 else if (INTERACTIVE)
7687 {
7688 /* The frame whose minibuffer we're going to display the message on.
7689 It may be larger than the selected frame, so we need
7690 to use its buffer, not the selected frame's buffer. */
7691 Lisp_Object mini_window;
7692 struct frame *f, *sf = SELECTED_FRAME ();
7693
7694 /* Get the frame containing the minibuffer
7695 that the selected frame is using. */
7696 mini_window = FRAME_MINIBUF_WINDOW (sf);
7697 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7698
7699 /* A null message buffer means that the frame hasn't really been
7700 initialized yet. Error messages get reported properly by
7701 cmd_error, so this must be just an informative message; toss it. */
7702 if (FRAME_MESSAGE_BUF (f))
7703 {
7704 Lisp_Object args[2], message;
7705 struct gcpro gcpro1, gcpro2;
7706
7707 args[0] = build_string (m);
7708 args[1] = message = string;
7709 GCPRO2 (args[0], message);
7710 gcpro1.nvars = 2;
7711
7712 message = Fformat (2, args);
7713
7714 if (log)
7715 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7716 else
7717 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7718
7719 UNGCPRO;
7720
7721 /* Print should start at the beginning of the message
7722 buffer next time. */
7723 message_buf_print = 0;
7724 }
7725 }
7726 }
7727
7728
7729 /* Dump an informative message to the minibuf. If M is 0, clear out
7730 any existing message, and let the mini-buffer text show through. */
7731
7732 /* VARARGS 1 */
7733 void
7734 message (m, a1, a2, a3)
7735 char *m;
7736 EMACS_INT a1, a2, a3;
7737 {
7738 if (noninteractive)
7739 {
7740 if (m)
7741 {
7742 if (noninteractive_need_newline)
7743 putc ('\n', stderr);
7744 noninteractive_need_newline = 0;
7745 fprintf (stderr, m, a1, a2, a3);
7746 if (cursor_in_echo_area == 0)
7747 fprintf (stderr, "\n");
7748 fflush (stderr);
7749 }
7750 }
7751 else if (INTERACTIVE)
7752 {
7753 /* The frame whose mini-buffer we're going to display the message
7754 on. It may be larger than the selected frame, so we need to
7755 use its buffer, not the selected frame's buffer. */
7756 Lisp_Object mini_window;
7757 struct frame *f, *sf = SELECTED_FRAME ();
7758
7759 /* Get the frame containing the mini-buffer
7760 that the selected frame is using. */
7761 mini_window = FRAME_MINIBUF_WINDOW (sf);
7762 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7763
7764 /* A null message buffer means that the frame hasn't really been
7765 initialized yet. Error messages get reported properly by
7766 cmd_error, so this must be just an informative message; toss
7767 it. */
7768 if (FRAME_MESSAGE_BUF (f))
7769 {
7770 if (m)
7771 {
7772 int len;
7773 #ifdef NO_ARG_ARRAY
7774 char *a[3];
7775 a[0] = (char *) a1;
7776 a[1] = (char *) a2;
7777 a[2] = (char *) a3;
7778
7779 len = doprnt (FRAME_MESSAGE_BUF (f),
7780 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7781 #else
7782 len = doprnt (FRAME_MESSAGE_BUF (f),
7783 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7784 (char **) &a1);
7785 #endif /* NO_ARG_ARRAY */
7786
7787 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7788 }
7789 else
7790 message1 (0);
7791
7792 /* Print should start at the beginning of the message
7793 buffer next time. */
7794 message_buf_print = 0;
7795 }
7796 }
7797 }
7798
7799
7800 /* The non-logging version of message. */
7801
7802 void
7803 message_nolog (m, a1, a2, a3)
7804 char *m;
7805 EMACS_INT a1, a2, a3;
7806 {
7807 Lisp_Object old_log_max;
7808 old_log_max = Vmessage_log_max;
7809 Vmessage_log_max = Qnil;
7810 message (m, a1, a2, a3);
7811 Vmessage_log_max = old_log_max;
7812 }
7813
7814
7815 /* Display the current message in the current mini-buffer. This is
7816 only called from error handlers in process.c, and is not time
7817 critical. */
7818
7819 void
7820 update_echo_area ()
7821 {
7822 if (!NILP (echo_area_buffer[0]))
7823 {
7824 Lisp_Object string;
7825 string = Fcurrent_message ();
7826 message3 (string, SBYTES (string),
7827 !NILP (current_buffer->enable_multibyte_characters));
7828 }
7829 }
7830
7831
7832 /* Make sure echo area buffers in `echo_buffers' are live.
7833 If they aren't, make new ones. */
7834
7835 static void
7836 ensure_echo_area_buffers ()
7837 {
7838 int i;
7839
7840 for (i = 0; i < 2; ++i)
7841 if (!BUFFERP (echo_buffer[i])
7842 || NILP (XBUFFER (echo_buffer[i])->name))
7843 {
7844 char name[30];
7845 Lisp_Object old_buffer;
7846 int j;
7847
7848 old_buffer = echo_buffer[i];
7849 sprintf (name, " *Echo Area %d*", i);
7850 echo_buffer[i] = Fget_buffer_create (build_string (name));
7851 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7852
7853 for (j = 0; j < 2; ++j)
7854 if (EQ (old_buffer, echo_area_buffer[j]))
7855 echo_area_buffer[j] = echo_buffer[i];
7856 }
7857 }
7858
7859
7860 /* Call FN with args A1..A4 with either the current or last displayed
7861 echo_area_buffer as current buffer.
7862
7863 WHICH zero means use the current message buffer
7864 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7865 from echo_buffer[] and clear it.
7866
7867 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7868 suitable buffer from echo_buffer[] and clear it.
7869
7870 Value is what FN returns. */
7871
7872 static int
7873 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7874 struct window *w;
7875 int which;
7876 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7877 EMACS_INT a1;
7878 Lisp_Object a2;
7879 EMACS_INT a3, a4;
7880 {
7881 Lisp_Object buffer;
7882 int this_one, the_other, clear_buffer_p, rc;
7883 int count = SPECPDL_INDEX ();
7884
7885 /* If buffers aren't live, make new ones. */
7886 ensure_echo_area_buffers ();
7887
7888 clear_buffer_p = 0;
7889
7890 if (which == 0)
7891 this_one = 0, the_other = 1;
7892 else if (which > 0)
7893 this_one = 1, the_other = 0;
7894
7895 /* Choose a suitable buffer from echo_buffer[] is we don't
7896 have one. */
7897 if (NILP (echo_area_buffer[this_one]))
7898 {
7899 echo_area_buffer[this_one]
7900 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7901 ? echo_buffer[the_other]
7902 : echo_buffer[this_one]);
7903 clear_buffer_p = 1;
7904 }
7905
7906 buffer = echo_area_buffer[this_one];
7907
7908 /* Don't get confused by reusing the buffer used for echoing
7909 for a different purpose. */
7910 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7911 cancel_echoing ();
7912
7913 record_unwind_protect (unwind_with_echo_area_buffer,
7914 with_echo_area_buffer_unwind_data (w));
7915
7916 /* Make the echo area buffer current. Note that for display
7917 purposes, it is not necessary that the displayed window's buffer
7918 == current_buffer, except for text property lookup. So, let's
7919 only set that buffer temporarily here without doing a full
7920 Fset_window_buffer. We must also change w->pointm, though,
7921 because otherwise an assertions in unshow_buffer fails, and Emacs
7922 aborts. */
7923 set_buffer_internal_1 (XBUFFER (buffer));
7924 if (w)
7925 {
7926 w->buffer = buffer;
7927 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7928 }
7929
7930 current_buffer->undo_list = Qt;
7931 current_buffer->read_only = Qnil;
7932 specbind (Qinhibit_read_only, Qt);
7933 specbind (Qinhibit_modification_hooks, Qt);
7934
7935 if (clear_buffer_p && Z > BEG)
7936 del_range (BEG, Z);
7937
7938 xassert (BEGV >= BEG);
7939 xassert (ZV <= Z && ZV >= BEGV);
7940
7941 rc = fn (a1, a2, a3, a4);
7942
7943 xassert (BEGV >= BEG);
7944 xassert (ZV <= Z && ZV >= BEGV);
7945
7946 unbind_to (count, Qnil);
7947 return rc;
7948 }
7949
7950
7951 /* Save state that should be preserved around the call to the function
7952 FN called in with_echo_area_buffer. */
7953
7954 static Lisp_Object
7955 with_echo_area_buffer_unwind_data (w)
7956 struct window *w;
7957 {
7958 int i = 0;
7959 Lisp_Object vector;
7960
7961 /* Reduce consing by keeping one vector in
7962 Vwith_echo_area_save_vector. */
7963 vector = Vwith_echo_area_save_vector;
7964 Vwith_echo_area_save_vector = Qnil;
7965
7966 if (NILP (vector))
7967 vector = Fmake_vector (make_number (7), Qnil);
7968
7969 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7970 AREF (vector, i) = Vdeactivate_mark, ++i;
7971 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7972
7973 if (w)
7974 {
7975 XSETWINDOW (AREF (vector, i), w); ++i;
7976 AREF (vector, i) = w->buffer; ++i;
7977 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7978 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7979 }
7980 else
7981 {
7982 int end = i + 4;
7983 for (; i < end; ++i)
7984 AREF (vector, i) = Qnil;
7985 }
7986
7987 xassert (i == ASIZE (vector));
7988 return vector;
7989 }
7990
7991
7992 /* Restore global state from VECTOR which was created by
7993 with_echo_area_buffer_unwind_data. */
7994
7995 static Lisp_Object
7996 unwind_with_echo_area_buffer (vector)
7997 Lisp_Object vector;
7998 {
7999 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8000 Vdeactivate_mark = AREF (vector, 1);
8001 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8002
8003 if (WINDOWP (AREF (vector, 3)))
8004 {
8005 struct window *w;
8006 Lisp_Object buffer, charpos, bytepos;
8007
8008 w = XWINDOW (AREF (vector, 3));
8009 buffer = AREF (vector, 4);
8010 charpos = AREF (vector, 5);
8011 bytepos = AREF (vector, 6);
8012
8013 w->buffer = buffer;
8014 set_marker_both (w->pointm, buffer,
8015 XFASTINT (charpos), XFASTINT (bytepos));
8016 }
8017
8018 Vwith_echo_area_save_vector = vector;
8019 return Qnil;
8020 }
8021
8022
8023 /* Set up the echo area for use by print functions. MULTIBYTE_P
8024 non-zero means we will print multibyte. */
8025
8026 void
8027 setup_echo_area_for_printing (multibyte_p)
8028 int multibyte_p;
8029 {
8030 /* If we can't find an echo area any more, exit. */
8031 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8032 Fkill_emacs (Qnil);
8033
8034 ensure_echo_area_buffers ();
8035
8036 if (!message_buf_print)
8037 {
8038 /* A message has been output since the last time we printed.
8039 Choose a fresh echo area buffer. */
8040 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8041 echo_area_buffer[0] = echo_buffer[1];
8042 else
8043 echo_area_buffer[0] = echo_buffer[0];
8044
8045 /* Switch to that buffer and clear it. */
8046 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8047 current_buffer->truncate_lines = Qnil;
8048
8049 if (Z > BEG)
8050 {
8051 int count = SPECPDL_INDEX ();
8052 specbind (Qinhibit_read_only, Qt);
8053 /* Note that undo recording is always disabled. */
8054 del_range (BEG, Z);
8055 unbind_to (count, Qnil);
8056 }
8057 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8058
8059 /* Set up the buffer for the multibyteness we need. */
8060 if (multibyte_p
8061 != !NILP (current_buffer->enable_multibyte_characters))
8062 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8063
8064 /* Raise the frame containing the echo area. */
8065 if (minibuffer_auto_raise)
8066 {
8067 struct frame *sf = SELECTED_FRAME ();
8068 Lisp_Object mini_window;
8069 mini_window = FRAME_MINIBUF_WINDOW (sf);
8070 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8071 }
8072
8073 message_log_maybe_newline ();
8074 message_buf_print = 1;
8075 }
8076 else
8077 {
8078 if (NILP (echo_area_buffer[0]))
8079 {
8080 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8081 echo_area_buffer[0] = echo_buffer[1];
8082 else
8083 echo_area_buffer[0] = echo_buffer[0];
8084 }
8085
8086 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8087 {
8088 /* Someone switched buffers between print requests. */
8089 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8090 current_buffer->truncate_lines = Qnil;
8091 }
8092 }
8093 }
8094
8095
8096 /* Display an echo area message in window W. Value is non-zero if W's
8097 height is changed. If display_last_displayed_message_p is
8098 non-zero, display the message that was last displayed, otherwise
8099 display the current message. */
8100
8101 static int
8102 display_echo_area (w)
8103 struct window *w;
8104 {
8105 int i, no_message_p, window_height_changed_p, count;
8106
8107 /* Temporarily disable garbage collections while displaying the echo
8108 area. This is done because a GC can print a message itself.
8109 That message would modify the echo area buffer's contents while a
8110 redisplay of the buffer is going on, and seriously confuse
8111 redisplay. */
8112 count = inhibit_garbage_collection ();
8113
8114 /* If there is no message, we must call display_echo_area_1
8115 nevertheless because it resizes the window. But we will have to
8116 reset the echo_area_buffer in question to nil at the end because
8117 with_echo_area_buffer will sets it to an empty buffer. */
8118 i = display_last_displayed_message_p ? 1 : 0;
8119 no_message_p = NILP (echo_area_buffer[i]);
8120
8121 window_height_changed_p
8122 = with_echo_area_buffer (w, display_last_displayed_message_p,
8123 display_echo_area_1,
8124 (EMACS_INT) w, Qnil, 0, 0);
8125
8126 if (no_message_p)
8127 echo_area_buffer[i] = Qnil;
8128
8129 unbind_to (count, Qnil);
8130 return window_height_changed_p;
8131 }
8132
8133
8134 /* Helper for display_echo_area. Display the current buffer which
8135 contains the current echo area message in window W, a mini-window,
8136 a pointer to which is passed in A1. A2..A4 are currently not used.
8137 Change the height of W so that all of the message is displayed.
8138 Value is non-zero if height of W was changed. */
8139
8140 static int
8141 display_echo_area_1 (a1, a2, a3, a4)
8142 EMACS_INT a1;
8143 Lisp_Object a2;
8144 EMACS_INT a3, a4;
8145 {
8146 struct window *w = (struct window *) a1;
8147 Lisp_Object window;
8148 struct text_pos start;
8149 int window_height_changed_p = 0;
8150
8151 /* Do this before displaying, so that we have a large enough glyph
8152 matrix for the display. If we can't get enough space for the
8153 whole text, display the last N lines. That works by setting w->start. */
8154 window_height_changed_p = resize_mini_window (w, 0);
8155
8156 /* Use the starting position chosen by resize_mini_window. */
8157 SET_TEXT_POS_FROM_MARKER (start, w->start);
8158
8159 /* Display. */
8160 clear_glyph_matrix (w->desired_matrix);
8161 XSETWINDOW (window, w);
8162 try_window (window, start, 0);
8163
8164 return window_height_changed_p;
8165 }
8166
8167
8168 /* Resize the echo area window to exactly the size needed for the
8169 currently displayed message, if there is one. If a mini-buffer
8170 is active, don't shrink it. */
8171
8172 void
8173 resize_echo_area_exactly ()
8174 {
8175 if (BUFFERP (echo_area_buffer[0])
8176 && WINDOWP (echo_area_window))
8177 {
8178 struct window *w = XWINDOW (echo_area_window);
8179 int resized_p;
8180 Lisp_Object resize_exactly;
8181
8182 if (minibuf_level == 0)
8183 resize_exactly = Qt;
8184 else
8185 resize_exactly = Qnil;
8186
8187 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8188 (EMACS_INT) w, resize_exactly, 0, 0);
8189 if (resized_p)
8190 {
8191 ++windows_or_buffers_changed;
8192 ++update_mode_lines;
8193 redisplay_internal (0);
8194 }
8195 }
8196 }
8197
8198
8199 /* Callback function for with_echo_area_buffer, when used from
8200 resize_echo_area_exactly. A1 contains a pointer to the window to
8201 resize, EXACTLY non-nil means resize the mini-window exactly to the
8202 size of the text displayed. A3 and A4 are not used. Value is what
8203 resize_mini_window returns. */
8204
8205 static int
8206 resize_mini_window_1 (a1, exactly, a3, a4)
8207 EMACS_INT a1;
8208 Lisp_Object exactly;
8209 EMACS_INT a3, a4;
8210 {
8211 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8212 }
8213
8214
8215 /* Resize mini-window W to fit the size of its contents. EXACT:P
8216 means size the window exactly to the size needed. Otherwise, it's
8217 only enlarged until W's buffer is empty.
8218
8219 Set W->start to the right place to begin display. If the whole
8220 contents fit, start at the beginning. Otherwise, start so as
8221 to make the end of the contents appear. This is particularly
8222 important for y-or-n-p, but seems desirable generally.
8223
8224 Value is non-zero if the window height has been changed. */
8225
8226 int
8227 resize_mini_window (w, exact_p)
8228 struct window *w;
8229 int exact_p;
8230 {
8231 struct frame *f = XFRAME (w->frame);
8232 int window_height_changed_p = 0;
8233
8234 xassert (MINI_WINDOW_P (w));
8235
8236 /* By default, start display at the beginning. */
8237 set_marker_both (w->start, w->buffer,
8238 BUF_BEGV (XBUFFER (w->buffer)),
8239 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8240
8241 /* Don't resize windows while redisplaying a window; it would
8242 confuse redisplay functions when the size of the window they are
8243 displaying changes from under them. Such a resizing can happen,
8244 for instance, when which-func prints a long message while
8245 we are running fontification-functions. We're running these
8246 functions with safe_call which binds inhibit-redisplay to t. */
8247 if (!NILP (Vinhibit_redisplay))
8248 return 0;
8249
8250 /* Nil means don't try to resize. */
8251 if (NILP (Vresize_mini_windows)
8252 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8253 return 0;
8254
8255 if (!FRAME_MINIBUF_ONLY_P (f))
8256 {
8257 struct it it;
8258 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8259 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8260 int height, max_height;
8261 int unit = FRAME_LINE_HEIGHT (f);
8262 struct text_pos start;
8263 struct buffer *old_current_buffer = NULL;
8264
8265 if (current_buffer != XBUFFER (w->buffer))
8266 {
8267 old_current_buffer = current_buffer;
8268 set_buffer_internal (XBUFFER (w->buffer));
8269 }
8270
8271 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8272
8273 /* Compute the max. number of lines specified by the user. */
8274 if (FLOATP (Vmax_mini_window_height))
8275 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8276 else if (INTEGERP (Vmax_mini_window_height))
8277 max_height = XINT (Vmax_mini_window_height);
8278 else
8279 max_height = total_height / 4;
8280
8281 /* Correct that max. height if it's bogus. */
8282 max_height = max (1, max_height);
8283 max_height = min (total_height, max_height);
8284
8285 /* Find out the height of the text in the window. */
8286 if (it.truncate_lines_p)
8287 height = 1;
8288 else
8289 {
8290 last_height = 0;
8291 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8292 if (it.max_ascent == 0 && it.max_descent == 0)
8293 height = it.current_y + last_height;
8294 else
8295 height = it.current_y + it.max_ascent + it.max_descent;
8296 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8297 height = (height + unit - 1) / unit;
8298 }
8299
8300 /* Compute a suitable window start. */
8301 if (height > max_height)
8302 {
8303 height = max_height;
8304 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8305 move_it_vertically_backward (&it, (height - 1) * unit);
8306 start = it.current.pos;
8307 }
8308 else
8309 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8310 SET_MARKER_FROM_TEXT_POS (w->start, start);
8311
8312 if (EQ (Vresize_mini_windows, Qgrow_only))
8313 {
8314 /* Let it grow only, until we display an empty message, in which
8315 case the window shrinks again. */
8316 if (height > WINDOW_TOTAL_LINES (w))
8317 {
8318 int old_height = WINDOW_TOTAL_LINES (w);
8319 freeze_window_starts (f, 1);
8320 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8321 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8322 }
8323 else if (height < WINDOW_TOTAL_LINES (w)
8324 && (exact_p || BEGV == ZV))
8325 {
8326 int old_height = WINDOW_TOTAL_LINES (w);
8327 freeze_window_starts (f, 0);
8328 shrink_mini_window (w);
8329 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8330 }
8331 }
8332 else
8333 {
8334 /* Always resize to exact size needed. */
8335 if (height > WINDOW_TOTAL_LINES (w))
8336 {
8337 int old_height = WINDOW_TOTAL_LINES (w);
8338 freeze_window_starts (f, 1);
8339 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8340 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8341 }
8342 else if (height < WINDOW_TOTAL_LINES (w))
8343 {
8344 int old_height = WINDOW_TOTAL_LINES (w);
8345 freeze_window_starts (f, 0);
8346 shrink_mini_window (w);
8347
8348 if (height)
8349 {
8350 freeze_window_starts (f, 1);
8351 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8352 }
8353
8354 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8355 }
8356 }
8357
8358 if (old_current_buffer)
8359 set_buffer_internal (old_current_buffer);
8360 }
8361
8362 return window_height_changed_p;
8363 }
8364
8365
8366 /* Value is the current message, a string, or nil if there is no
8367 current message. */
8368
8369 Lisp_Object
8370 current_message ()
8371 {
8372 Lisp_Object msg;
8373
8374 if (NILP (echo_area_buffer[0]))
8375 msg = Qnil;
8376 else
8377 {
8378 with_echo_area_buffer (0, 0, current_message_1,
8379 (EMACS_INT) &msg, Qnil, 0, 0);
8380 if (NILP (msg))
8381 echo_area_buffer[0] = Qnil;
8382 }
8383
8384 return msg;
8385 }
8386
8387
8388 static int
8389 current_message_1 (a1, a2, a3, a4)
8390 EMACS_INT a1;
8391 Lisp_Object a2;
8392 EMACS_INT a3, a4;
8393 {
8394 Lisp_Object *msg = (Lisp_Object *) a1;
8395
8396 if (Z > BEG)
8397 *msg = make_buffer_string (BEG, Z, 1);
8398 else
8399 *msg = Qnil;
8400 return 0;
8401 }
8402
8403
8404 /* Push the current message on Vmessage_stack for later restauration
8405 by restore_message. Value is non-zero if the current message isn't
8406 empty. This is a relatively infrequent operation, so it's not
8407 worth optimizing. */
8408
8409 int
8410 push_message ()
8411 {
8412 Lisp_Object msg;
8413 msg = current_message ();
8414 Vmessage_stack = Fcons (msg, Vmessage_stack);
8415 return STRINGP (msg);
8416 }
8417
8418
8419 /* Restore message display from the top of Vmessage_stack. */
8420
8421 void
8422 restore_message ()
8423 {
8424 Lisp_Object msg;
8425
8426 xassert (CONSP (Vmessage_stack));
8427 msg = XCAR (Vmessage_stack);
8428 if (STRINGP (msg))
8429 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8430 else
8431 message3_nolog (msg, 0, 0);
8432 }
8433
8434
8435 /* Handler for record_unwind_protect calling pop_message. */
8436
8437 Lisp_Object
8438 pop_message_unwind (dummy)
8439 Lisp_Object dummy;
8440 {
8441 pop_message ();
8442 return Qnil;
8443 }
8444
8445 /* Pop the top-most entry off Vmessage_stack. */
8446
8447 void
8448 pop_message ()
8449 {
8450 xassert (CONSP (Vmessage_stack));
8451 Vmessage_stack = XCDR (Vmessage_stack);
8452 }
8453
8454
8455 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8456 exits. If the stack is not empty, we have a missing pop_message
8457 somewhere. */
8458
8459 void
8460 check_message_stack ()
8461 {
8462 if (!NILP (Vmessage_stack))
8463 abort ();
8464 }
8465
8466
8467 /* Truncate to NCHARS what will be displayed in the echo area the next
8468 time we display it---but don't redisplay it now. */
8469
8470 void
8471 truncate_echo_area (nchars)
8472 int nchars;
8473 {
8474 if (nchars == 0)
8475 echo_area_buffer[0] = Qnil;
8476 /* A null message buffer means that the frame hasn't really been
8477 initialized yet. Error messages get reported properly by
8478 cmd_error, so this must be just an informative message; toss it. */
8479 else if (!noninteractive
8480 && INTERACTIVE
8481 && !NILP (echo_area_buffer[0]))
8482 {
8483 struct frame *sf = SELECTED_FRAME ();
8484 if (FRAME_MESSAGE_BUF (sf))
8485 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8486 }
8487 }
8488
8489
8490 /* Helper function for truncate_echo_area. Truncate the current
8491 message to at most NCHARS characters. */
8492
8493 static int
8494 truncate_message_1 (nchars, a2, a3, a4)
8495 EMACS_INT nchars;
8496 Lisp_Object a2;
8497 EMACS_INT a3, a4;
8498 {
8499 if (BEG + nchars < Z)
8500 del_range (BEG + nchars, Z);
8501 if (Z == BEG)
8502 echo_area_buffer[0] = Qnil;
8503 return 0;
8504 }
8505
8506
8507 /* Set the current message to a substring of S or STRING.
8508
8509 If STRING is a Lisp string, set the message to the first NBYTES
8510 bytes from STRING. NBYTES zero means use the whole string. If
8511 STRING is multibyte, the message will be displayed multibyte.
8512
8513 If S is not null, set the message to the first LEN bytes of S. LEN
8514 zero means use the whole string. MULTIBYTE_P non-zero means S is
8515 multibyte. Display the message multibyte in that case.
8516
8517 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8518 to t before calling set_message_1 (which calls insert).
8519 */
8520
8521 void
8522 set_message (s, string, nbytes, multibyte_p)
8523 const char *s;
8524 Lisp_Object string;
8525 int nbytes, multibyte_p;
8526 {
8527 message_enable_multibyte
8528 = ((s && multibyte_p)
8529 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8530
8531 with_echo_area_buffer (0, 0, set_message_1,
8532 (EMACS_INT) s, string, nbytes, multibyte_p);
8533 message_buf_print = 0;
8534 help_echo_showing_p = 0;
8535 }
8536
8537
8538 /* Helper function for set_message. Arguments have the same meaning
8539 as there, with A1 corresponding to S and A2 corresponding to STRING
8540 This function is called with the echo area buffer being
8541 current. */
8542
8543 static int
8544 set_message_1 (a1, a2, nbytes, multibyte_p)
8545 EMACS_INT a1;
8546 Lisp_Object a2;
8547 EMACS_INT nbytes, multibyte_p;
8548 {
8549 const char *s = (const char *) a1;
8550 Lisp_Object string = a2;
8551
8552 /* Change multibyteness of the echo buffer appropriately. */
8553 if (message_enable_multibyte
8554 != !NILP (current_buffer->enable_multibyte_characters))
8555 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8556
8557 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8558
8559 /* Insert new message at BEG. */
8560 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8561 Ferase_buffer ();
8562
8563 if (STRINGP (string))
8564 {
8565 int nchars;
8566
8567 if (nbytes == 0)
8568 nbytes = SBYTES (string);
8569 nchars = string_byte_to_char (string, nbytes);
8570
8571 /* This function takes care of single/multibyte conversion. We
8572 just have to ensure that the echo area buffer has the right
8573 setting of enable_multibyte_characters. */
8574 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8575 }
8576 else if (s)
8577 {
8578 if (nbytes == 0)
8579 nbytes = strlen (s);
8580
8581 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8582 {
8583 /* Convert from multi-byte to single-byte. */
8584 int i, c, n;
8585 unsigned char work[1];
8586
8587 /* Convert a multibyte string to single-byte. */
8588 for (i = 0; i < nbytes; i += n)
8589 {
8590 c = string_char_and_length (s + i, nbytes - i, &n);
8591 work[0] = (SINGLE_BYTE_CHAR_P (c)
8592 ? c
8593 : multibyte_char_to_unibyte (c, Qnil));
8594 insert_1_both (work, 1, 1, 1, 0, 0);
8595 }
8596 }
8597 else if (!multibyte_p
8598 && !NILP (current_buffer->enable_multibyte_characters))
8599 {
8600 /* Convert from single-byte to multi-byte. */
8601 int i, c, n;
8602 const unsigned char *msg = (const unsigned char *) s;
8603 unsigned char str[MAX_MULTIBYTE_LENGTH];
8604
8605 /* Convert a single-byte string to multibyte. */
8606 for (i = 0; i < nbytes; i++)
8607 {
8608 c = unibyte_char_to_multibyte (msg[i]);
8609 n = CHAR_STRING (c, str);
8610 insert_1_both (str, 1, n, 1, 0, 0);
8611 }
8612 }
8613 else
8614 insert_1 (s, nbytes, 1, 0, 0);
8615 }
8616
8617 return 0;
8618 }
8619
8620
8621 /* Clear messages. CURRENT_P non-zero means clear the current
8622 message. LAST_DISPLAYED_P non-zero means clear the message
8623 last displayed. */
8624
8625 void
8626 clear_message (current_p, last_displayed_p)
8627 int current_p, last_displayed_p;
8628 {
8629 if (current_p)
8630 {
8631 echo_area_buffer[0] = Qnil;
8632 message_cleared_p = 1;
8633 }
8634
8635 if (last_displayed_p)
8636 echo_area_buffer[1] = Qnil;
8637
8638 message_buf_print = 0;
8639 }
8640
8641 /* Clear garbaged frames.
8642
8643 This function is used where the old redisplay called
8644 redraw_garbaged_frames which in turn called redraw_frame which in
8645 turn called clear_frame. The call to clear_frame was a source of
8646 flickering. I believe a clear_frame is not necessary. It should
8647 suffice in the new redisplay to invalidate all current matrices,
8648 and ensure a complete redisplay of all windows. */
8649
8650 static void
8651 clear_garbaged_frames ()
8652 {
8653 if (frame_garbaged)
8654 {
8655 Lisp_Object tail, frame;
8656 int changed_count = 0;
8657
8658 FOR_EACH_FRAME (tail, frame)
8659 {
8660 struct frame *f = XFRAME (frame);
8661
8662 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8663 {
8664 if (f->resized_p)
8665 {
8666 Fredraw_frame (frame);
8667 f->force_flush_display_p = 1;
8668 }
8669 clear_current_matrices (f);
8670 changed_count++;
8671 f->garbaged = 0;
8672 f->resized_p = 0;
8673 }
8674 }
8675
8676 frame_garbaged = 0;
8677 if (changed_count)
8678 ++windows_or_buffers_changed;
8679 }
8680 }
8681
8682
8683 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8684 is non-zero update selected_frame. Value is non-zero if the
8685 mini-windows height has been changed. */
8686
8687 static int
8688 echo_area_display (update_frame_p)
8689 int update_frame_p;
8690 {
8691 Lisp_Object mini_window;
8692 struct window *w;
8693 struct frame *f;
8694 int window_height_changed_p = 0;
8695 struct frame *sf = SELECTED_FRAME ();
8696
8697 mini_window = FRAME_MINIBUF_WINDOW (sf);
8698 w = XWINDOW (mini_window);
8699 f = XFRAME (WINDOW_FRAME (w));
8700
8701 /* Don't display if frame is invisible or not yet initialized. */
8702 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8703 return 0;
8704
8705 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8706 #ifndef MAC_OS8
8707 #ifdef HAVE_WINDOW_SYSTEM
8708 /* When Emacs starts, selected_frame may be a visible terminal
8709 frame, even if we run under a window system. If we let this
8710 through, a message would be displayed on the terminal. */
8711 if (EQ (selected_frame, Vterminal_frame)
8712 && !NILP (Vwindow_system))
8713 return 0;
8714 #endif /* HAVE_WINDOW_SYSTEM */
8715 #endif
8716
8717 /* Redraw garbaged frames. */
8718 if (frame_garbaged)
8719 clear_garbaged_frames ();
8720
8721 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8722 {
8723 echo_area_window = mini_window;
8724 window_height_changed_p = display_echo_area (w);
8725 w->must_be_updated_p = 1;
8726
8727 /* Update the display, unless called from redisplay_internal.
8728 Also don't update the screen during redisplay itself. The
8729 update will happen at the end of redisplay, and an update
8730 here could cause confusion. */
8731 if (update_frame_p && !redisplaying_p)
8732 {
8733 int n = 0;
8734
8735 /* If the display update has been interrupted by pending
8736 input, update mode lines in the frame. Due to the
8737 pending input, it might have been that redisplay hasn't
8738 been called, so that mode lines above the echo area are
8739 garbaged. This looks odd, so we prevent it here. */
8740 if (!display_completed)
8741 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8742
8743 if (window_height_changed_p
8744 /* Don't do this if Emacs is shutting down. Redisplay
8745 needs to run hooks. */
8746 && !NILP (Vrun_hooks))
8747 {
8748 /* Must update other windows. Likewise as in other
8749 cases, don't let this update be interrupted by
8750 pending input. */
8751 int count = SPECPDL_INDEX ();
8752 specbind (Qredisplay_dont_pause, Qt);
8753 windows_or_buffers_changed = 1;
8754 redisplay_internal (0);
8755 unbind_to (count, Qnil);
8756 }
8757 else if (FRAME_WINDOW_P (f) && n == 0)
8758 {
8759 /* Window configuration is the same as before.
8760 Can do with a display update of the echo area,
8761 unless we displayed some mode lines. */
8762 update_single_window (w, 1);
8763 rif->flush_display (f);
8764 }
8765 else
8766 update_frame (f, 1, 1);
8767
8768 /* If cursor is in the echo area, make sure that the next
8769 redisplay displays the minibuffer, so that the cursor will
8770 be replaced with what the minibuffer wants. */
8771 if (cursor_in_echo_area)
8772 ++windows_or_buffers_changed;
8773 }
8774 }
8775 else if (!EQ (mini_window, selected_window))
8776 windows_or_buffers_changed++;
8777
8778 /* The current message is now also the last one displayed. */
8779 echo_area_buffer[1] = echo_area_buffer[0];
8780
8781 /* Prevent redisplay optimization in redisplay_internal by resetting
8782 this_line_start_pos. This is done because the mini-buffer now
8783 displays the message instead of its buffer text. */
8784 if (EQ (mini_window, selected_window))
8785 CHARPOS (this_line_start_pos) = 0;
8786
8787 return window_height_changed_p;
8788 }
8789
8790
8791 \f
8792 /***********************************************************************
8793 Mode Lines and Frame Titles
8794 ***********************************************************************/
8795
8796 /* A buffer for constructing non-propertized mode-line strings and
8797 frame titles in it; allocated from the heap in init_xdisp and
8798 resized as needed in store_mode_line_noprop_char. */
8799
8800 static char *mode_line_noprop_buf;
8801
8802 /* The buffer's end, and a current output position in it. */
8803
8804 static char *mode_line_noprop_buf_end;
8805 static char *mode_line_noprop_ptr;
8806
8807 #define MODE_LINE_NOPROP_LEN(start) \
8808 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8809
8810 static enum {
8811 MODE_LINE_DISPLAY = 0,
8812 MODE_LINE_TITLE,
8813 MODE_LINE_NOPROP,
8814 MODE_LINE_STRING
8815 } mode_line_target;
8816
8817 /* Alist that caches the results of :propertize.
8818 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8819 static Lisp_Object mode_line_proptrans_alist;
8820
8821 /* List of strings making up the mode-line. */
8822 static Lisp_Object mode_line_string_list;
8823
8824 /* Base face property when building propertized mode line string. */
8825 static Lisp_Object mode_line_string_face;
8826 static Lisp_Object mode_line_string_face_prop;
8827
8828
8829 /* Unwind data for mode line strings */
8830
8831 static Lisp_Object Vmode_line_unwind_vector;
8832
8833 static Lisp_Object
8834 format_mode_line_unwind_data (obuf, save_proptrans)
8835 struct buffer *obuf;
8836 {
8837 Lisp_Object vector;
8838
8839 /* Reduce consing by keeping one vector in
8840 Vwith_echo_area_save_vector. */
8841 vector = Vmode_line_unwind_vector;
8842 Vmode_line_unwind_vector = Qnil;
8843
8844 if (NILP (vector))
8845 vector = Fmake_vector (make_number (7), Qnil);
8846
8847 AREF (vector, 0) = make_number (mode_line_target);
8848 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8849 AREF (vector, 2) = mode_line_string_list;
8850 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8851 AREF (vector, 4) = mode_line_string_face;
8852 AREF (vector, 5) = mode_line_string_face_prop;
8853
8854 if (obuf)
8855 XSETBUFFER (AREF (vector, 6), obuf);
8856 else
8857 AREF (vector, 6) = Qnil;
8858
8859 return vector;
8860 }
8861
8862 static Lisp_Object
8863 unwind_format_mode_line (vector)
8864 Lisp_Object vector;
8865 {
8866 mode_line_target = XINT (AREF (vector, 0));
8867 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8868 mode_line_string_list = AREF (vector, 2);
8869 if (! EQ (AREF (vector, 3), Qt))
8870 mode_line_proptrans_alist = AREF (vector, 3);
8871 mode_line_string_face = AREF (vector, 4);
8872 mode_line_string_face_prop = AREF (vector, 5);
8873
8874 if (!NILP (AREF (vector, 6)))
8875 {
8876 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8877 AREF (vector, 6) = Qnil;
8878 }
8879
8880 Vmode_line_unwind_vector = vector;
8881 return Qnil;
8882 }
8883
8884
8885 /* Store a single character C for the frame title in mode_line_noprop_buf.
8886 Re-allocate mode_line_noprop_buf if necessary. */
8887
8888 static void
8889 #ifdef PROTOTYPES
8890 store_mode_line_noprop_char (char c)
8891 #else
8892 store_mode_line_noprop_char (c)
8893 char c;
8894 #endif
8895 {
8896 /* If output position has reached the end of the allocated buffer,
8897 double the buffer's size. */
8898 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8899 {
8900 int len = MODE_LINE_NOPROP_LEN (0);
8901 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8902 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8903 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8904 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8905 }
8906
8907 *mode_line_noprop_ptr++ = c;
8908 }
8909
8910
8911 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8912 mode_line_noprop_ptr. STR is the string to store. Do not copy
8913 characters that yield more columns than PRECISION; PRECISION <= 0
8914 means copy the whole string. Pad with spaces until FIELD_WIDTH
8915 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8916 pad. Called from display_mode_element when it is used to build a
8917 frame title. */
8918
8919 static int
8920 store_mode_line_noprop (str, field_width, precision)
8921 const unsigned char *str;
8922 int field_width, precision;
8923 {
8924 int n = 0;
8925 int dummy, nbytes;
8926
8927 /* Copy at most PRECISION chars from STR. */
8928 nbytes = strlen (str);
8929 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8930 while (nbytes--)
8931 store_mode_line_noprop_char (*str++);
8932
8933 /* Fill up with spaces until FIELD_WIDTH reached. */
8934 while (field_width > 0
8935 && n < field_width)
8936 {
8937 store_mode_line_noprop_char (' ');
8938 ++n;
8939 }
8940
8941 return n;
8942 }
8943
8944 /***********************************************************************
8945 Frame Titles
8946 ***********************************************************************/
8947
8948 #ifdef HAVE_WINDOW_SYSTEM
8949
8950 /* Set the title of FRAME, if it has changed. The title format is
8951 Vicon_title_format if FRAME is iconified, otherwise it is
8952 frame_title_format. */
8953
8954 static void
8955 x_consider_frame_title (frame)
8956 Lisp_Object frame;
8957 {
8958 struct frame *f = XFRAME (frame);
8959
8960 if (FRAME_WINDOW_P (f)
8961 || FRAME_MINIBUF_ONLY_P (f)
8962 || f->explicit_name)
8963 {
8964 /* Do we have more than one visible frame on this X display? */
8965 Lisp_Object tail;
8966 Lisp_Object fmt;
8967 int title_start;
8968 char *title;
8969 int len;
8970 struct it it;
8971 int count = SPECPDL_INDEX ();
8972
8973 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8974 {
8975 Lisp_Object other_frame = XCAR (tail);
8976 struct frame *tf = XFRAME (other_frame);
8977
8978 if (tf != f
8979 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8980 && !FRAME_MINIBUF_ONLY_P (tf)
8981 && !EQ (other_frame, tip_frame)
8982 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8983 break;
8984 }
8985
8986 /* Set global variable indicating that multiple frames exist. */
8987 multiple_frames = CONSP (tail);
8988
8989 /* Switch to the buffer of selected window of the frame. Set up
8990 mode_line_target so that display_mode_element will output into
8991 mode_line_noprop_buf; then display the title. */
8992 record_unwind_protect (unwind_format_mode_line,
8993 format_mode_line_unwind_data (current_buffer, 0));
8994
8995 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8996 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8997
8998 mode_line_target = MODE_LINE_TITLE;
8999 title_start = MODE_LINE_NOPROP_LEN (0);
9000 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9001 NULL, DEFAULT_FACE_ID);
9002 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9003 len = MODE_LINE_NOPROP_LEN (title_start);
9004 title = mode_line_noprop_buf + title_start;
9005 unbind_to (count, Qnil);
9006
9007 /* Set the title only if it's changed. This avoids consing in
9008 the common case where it hasn't. (If it turns out that we've
9009 already wasted too much time by walking through the list with
9010 display_mode_element, then we might need to optimize at a
9011 higher level than this.) */
9012 if (! STRINGP (f->name)
9013 || SBYTES (f->name) != len
9014 || bcmp (title, SDATA (f->name), len) != 0)
9015 x_implicitly_set_name (f, make_string (title, len), Qnil);
9016 }
9017 }
9018
9019 #endif /* not HAVE_WINDOW_SYSTEM */
9020
9021
9022
9023 \f
9024 /***********************************************************************
9025 Menu Bars
9026 ***********************************************************************/
9027
9028
9029 /* Prepare for redisplay by updating menu-bar item lists when
9030 appropriate. This can call eval. */
9031
9032 void
9033 prepare_menu_bars ()
9034 {
9035 int all_windows;
9036 struct gcpro gcpro1, gcpro2;
9037 struct frame *f;
9038 Lisp_Object tooltip_frame;
9039
9040 #ifdef HAVE_WINDOW_SYSTEM
9041 tooltip_frame = tip_frame;
9042 #else
9043 tooltip_frame = Qnil;
9044 #endif
9045
9046 /* Update all frame titles based on their buffer names, etc. We do
9047 this before the menu bars so that the buffer-menu will show the
9048 up-to-date frame titles. */
9049 #ifdef HAVE_WINDOW_SYSTEM
9050 if (windows_or_buffers_changed || update_mode_lines)
9051 {
9052 Lisp_Object tail, frame;
9053
9054 FOR_EACH_FRAME (tail, frame)
9055 {
9056 f = XFRAME (frame);
9057 if (!EQ (frame, tooltip_frame)
9058 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9059 x_consider_frame_title (frame);
9060 }
9061 }
9062 #endif /* HAVE_WINDOW_SYSTEM */
9063
9064 /* Update the menu bar item lists, if appropriate. This has to be
9065 done before any actual redisplay or generation of display lines. */
9066 all_windows = (update_mode_lines
9067 || buffer_shared > 1
9068 || windows_or_buffers_changed);
9069 if (all_windows)
9070 {
9071 Lisp_Object tail, frame;
9072 int count = SPECPDL_INDEX ();
9073 /* 1 means that update_menu_bar has run its hooks
9074 so any further calls to update_menu_bar shouldn't do so again. */
9075 int menu_bar_hooks_run = 0;
9076
9077 record_unwind_save_match_data ();
9078
9079 FOR_EACH_FRAME (tail, frame)
9080 {
9081 f = XFRAME (frame);
9082
9083 /* Ignore tooltip frame. */
9084 if (EQ (frame, tooltip_frame))
9085 continue;
9086
9087 /* If a window on this frame changed size, report that to
9088 the user and clear the size-change flag. */
9089 if (FRAME_WINDOW_SIZES_CHANGED (f))
9090 {
9091 Lisp_Object functions;
9092
9093 /* Clear flag first in case we get an error below. */
9094 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9095 functions = Vwindow_size_change_functions;
9096 GCPRO2 (tail, functions);
9097
9098 while (CONSP (functions))
9099 {
9100 call1 (XCAR (functions), frame);
9101 functions = XCDR (functions);
9102 }
9103 UNGCPRO;
9104 }
9105
9106 GCPRO1 (tail);
9107 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9108 #ifdef HAVE_WINDOW_SYSTEM
9109 update_tool_bar (f, 0);
9110 #ifdef MAC_OS
9111 mac_update_title_bar (f, 0);
9112 #endif
9113 #endif
9114 UNGCPRO;
9115 }
9116
9117 unbind_to (count, Qnil);
9118 }
9119 else
9120 {
9121 struct frame *sf = SELECTED_FRAME ();
9122 update_menu_bar (sf, 1, 0);
9123 #ifdef HAVE_WINDOW_SYSTEM
9124 update_tool_bar (sf, 1);
9125 #ifdef MAC_OS
9126 mac_update_title_bar (sf, 1);
9127 #endif
9128 #endif
9129 }
9130
9131 /* Motif needs this. See comment in xmenu.c. Turn it off when
9132 pending_menu_activation is not defined. */
9133 #ifdef USE_X_TOOLKIT
9134 pending_menu_activation = 0;
9135 #endif
9136 }
9137
9138
9139 /* Update the menu bar item list for frame F. This has to be done
9140 before we start to fill in any display lines, because it can call
9141 eval.
9142
9143 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9144
9145 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9146 already ran the menu bar hooks for this redisplay, so there
9147 is no need to run them again. The return value is the
9148 updated value of this flag, to pass to the next call. */
9149
9150 static int
9151 update_menu_bar (f, save_match_data, hooks_run)
9152 struct frame *f;
9153 int save_match_data;
9154 int hooks_run;
9155 {
9156 Lisp_Object window;
9157 register struct window *w;
9158
9159 /* If called recursively during a menu update, do nothing. This can
9160 happen when, for instance, an activate-menubar-hook causes a
9161 redisplay. */
9162 if (inhibit_menubar_update)
9163 return hooks_run;
9164
9165 window = FRAME_SELECTED_WINDOW (f);
9166 w = XWINDOW (window);
9167
9168 #if 0 /* The if statement below this if statement used to include the
9169 condition !NILP (w->update_mode_line), rather than using
9170 update_mode_lines directly, and this if statement may have
9171 been added to make that condition work. Now the if
9172 statement below matches its comment, this isn't needed. */
9173 if (update_mode_lines)
9174 w->update_mode_line = Qt;
9175 #endif
9176
9177 if (FRAME_WINDOW_P (f)
9178 ?
9179 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9180 || defined (USE_GTK)
9181 FRAME_EXTERNAL_MENU_BAR (f)
9182 #else
9183 FRAME_MENU_BAR_LINES (f) > 0
9184 #endif
9185 : FRAME_MENU_BAR_LINES (f) > 0)
9186 {
9187 /* If the user has switched buffers or windows, we need to
9188 recompute to reflect the new bindings. But we'll
9189 recompute when update_mode_lines is set too; that means
9190 that people can use force-mode-line-update to request
9191 that the menu bar be recomputed. The adverse effect on
9192 the rest of the redisplay algorithm is about the same as
9193 windows_or_buffers_changed anyway. */
9194 if (windows_or_buffers_changed
9195 /* This used to test w->update_mode_line, but we believe
9196 there is no need to recompute the menu in that case. */
9197 || update_mode_lines
9198 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9199 < BUF_MODIFF (XBUFFER (w->buffer)))
9200 != !NILP (w->last_had_star))
9201 || ((!NILP (Vtransient_mark_mode)
9202 && !NILP (XBUFFER (w->buffer)->mark_active))
9203 != !NILP (w->region_showing)))
9204 {
9205 struct buffer *prev = current_buffer;
9206 int count = SPECPDL_INDEX ();
9207
9208 specbind (Qinhibit_menubar_update, Qt);
9209
9210 set_buffer_internal_1 (XBUFFER (w->buffer));
9211 if (save_match_data)
9212 record_unwind_save_match_data ();
9213 if (NILP (Voverriding_local_map_menu_flag))
9214 {
9215 specbind (Qoverriding_terminal_local_map, Qnil);
9216 specbind (Qoverriding_local_map, Qnil);
9217 }
9218
9219 if (!hooks_run)
9220 {
9221 /* Run the Lucid hook. */
9222 safe_run_hooks (Qactivate_menubar_hook);
9223
9224 /* If it has changed current-menubar from previous value,
9225 really recompute the menu-bar from the value. */
9226 if (! NILP (Vlucid_menu_bar_dirty_flag))
9227 call0 (Qrecompute_lucid_menubar);
9228
9229 safe_run_hooks (Qmenu_bar_update_hook);
9230
9231 hooks_run = 1;
9232 }
9233
9234 XSETFRAME (Vmenu_updating_frame, f);
9235 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9236
9237 /* Redisplay the menu bar in case we changed it. */
9238 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9239 || defined (USE_GTK)
9240 if (FRAME_WINDOW_P (f))
9241 {
9242 #ifdef MAC_OS
9243 /* All frames on Mac OS share the same menubar. So only
9244 the selected frame should be allowed to set it. */
9245 if (f == SELECTED_FRAME ())
9246 #endif
9247 set_frame_menubar (f, 0, 0);
9248 }
9249 else
9250 /* On a terminal screen, the menu bar is an ordinary screen
9251 line, and this makes it get updated. */
9252 w->update_mode_line = Qt;
9253 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9254 /* In the non-toolkit version, the menu bar is an ordinary screen
9255 line, and this makes it get updated. */
9256 w->update_mode_line = Qt;
9257 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9258
9259 unbind_to (count, Qnil);
9260 set_buffer_internal_1 (prev);
9261 }
9262 }
9263
9264 return hooks_run;
9265 }
9266
9267
9268 \f
9269 /***********************************************************************
9270 Output Cursor
9271 ***********************************************************************/
9272
9273 #ifdef HAVE_WINDOW_SYSTEM
9274
9275 /* EXPORT:
9276 Nominal cursor position -- where to draw output.
9277 HPOS and VPOS are window relative glyph matrix coordinates.
9278 X and Y are window relative pixel coordinates. */
9279
9280 struct cursor_pos output_cursor;
9281
9282
9283 /* EXPORT:
9284 Set the global variable output_cursor to CURSOR. All cursor
9285 positions are relative to updated_window. */
9286
9287 void
9288 set_output_cursor (cursor)
9289 struct cursor_pos *cursor;
9290 {
9291 output_cursor.hpos = cursor->hpos;
9292 output_cursor.vpos = cursor->vpos;
9293 output_cursor.x = cursor->x;
9294 output_cursor.y = cursor->y;
9295 }
9296
9297
9298 /* EXPORT for RIF:
9299 Set a nominal cursor position.
9300
9301 HPOS and VPOS are column/row positions in a window glyph matrix. X
9302 and Y are window text area relative pixel positions.
9303
9304 If this is done during an update, updated_window will contain the
9305 window that is being updated and the position is the future output
9306 cursor position for that window. If updated_window is null, use
9307 selected_window and display the cursor at the given position. */
9308
9309 void
9310 x_cursor_to (vpos, hpos, y, x)
9311 int vpos, hpos, y, x;
9312 {
9313 struct window *w;
9314
9315 /* If updated_window is not set, work on selected_window. */
9316 if (updated_window)
9317 w = updated_window;
9318 else
9319 w = XWINDOW (selected_window);
9320
9321 /* Set the output cursor. */
9322 output_cursor.hpos = hpos;
9323 output_cursor.vpos = vpos;
9324 output_cursor.x = x;
9325 output_cursor.y = y;
9326
9327 /* If not called as part of an update, really display the cursor.
9328 This will also set the cursor position of W. */
9329 if (updated_window == NULL)
9330 {
9331 BLOCK_INPUT;
9332 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9333 if (rif->flush_display_optional)
9334 rif->flush_display_optional (SELECTED_FRAME ());
9335 UNBLOCK_INPUT;
9336 }
9337 }
9338
9339 #endif /* HAVE_WINDOW_SYSTEM */
9340
9341 \f
9342 /***********************************************************************
9343 Tool-bars
9344 ***********************************************************************/
9345
9346 #ifdef HAVE_WINDOW_SYSTEM
9347
9348 /* Where the mouse was last time we reported a mouse event. */
9349
9350 FRAME_PTR last_mouse_frame;
9351
9352 /* Tool-bar item index of the item on which a mouse button was pressed
9353 or -1. */
9354
9355 int last_tool_bar_item;
9356
9357
9358 /* Update the tool-bar item list for frame F. This has to be done
9359 before we start to fill in any display lines. Called from
9360 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9361 and restore it here. */
9362
9363 static void
9364 update_tool_bar (f, save_match_data)
9365 struct frame *f;
9366 int save_match_data;
9367 {
9368 #ifdef USE_GTK
9369 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9370 #else
9371 int do_update = WINDOWP (f->tool_bar_window)
9372 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9373 #endif
9374
9375 if (do_update)
9376 {
9377 Lisp_Object window;
9378 struct window *w;
9379
9380 window = FRAME_SELECTED_WINDOW (f);
9381 w = XWINDOW (window);
9382
9383 /* If the user has switched buffers or windows, we need to
9384 recompute to reflect the new bindings. But we'll
9385 recompute when update_mode_lines is set too; that means
9386 that people can use force-mode-line-update to request
9387 that the menu bar be recomputed. The adverse effect on
9388 the rest of the redisplay algorithm is about the same as
9389 windows_or_buffers_changed anyway. */
9390 if (windows_or_buffers_changed
9391 || !NILP (w->update_mode_line)
9392 || update_mode_lines
9393 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9394 < BUF_MODIFF (XBUFFER (w->buffer)))
9395 != !NILP (w->last_had_star))
9396 || ((!NILP (Vtransient_mark_mode)
9397 && !NILP (XBUFFER (w->buffer)->mark_active))
9398 != !NILP (w->region_showing)))
9399 {
9400 struct buffer *prev = current_buffer;
9401 int count = SPECPDL_INDEX ();
9402 Lisp_Object new_tool_bar;
9403 int new_n_tool_bar;
9404 struct gcpro gcpro1;
9405
9406 /* Set current_buffer to the buffer of the selected
9407 window of the frame, so that we get the right local
9408 keymaps. */
9409 set_buffer_internal_1 (XBUFFER (w->buffer));
9410
9411 /* Save match data, if we must. */
9412 if (save_match_data)
9413 record_unwind_save_match_data ();
9414
9415 /* Make sure that we don't accidentally use bogus keymaps. */
9416 if (NILP (Voverriding_local_map_menu_flag))
9417 {
9418 specbind (Qoverriding_terminal_local_map, Qnil);
9419 specbind (Qoverriding_local_map, Qnil);
9420 }
9421
9422 GCPRO1 (new_tool_bar);
9423
9424 /* Build desired tool-bar items from keymaps. */
9425 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9426 &new_n_tool_bar);
9427
9428 /* Redisplay the tool-bar if we changed it. */
9429 if (new_n_tool_bar != f->n_tool_bar_items
9430 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9431 {
9432 /* Redisplay that happens asynchronously due to an expose event
9433 may access f->tool_bar_items. Make sure we update both
9434 variables within BLOCK_INPUT so no such event interrupts. */
9435 BLOCK_INPUT;
9436 f->tool_bar_items = new_tool_bar;
9437 f->n_tool_bar_items = new_n_tool_bar;
9438 w->update_mode_line = Qt;
9439 UNBLOCK_INPUT;
9440 }
9441
9442 UNGCPRO;
9443
9444 unbind_to (count, Qnil);
9445 set_buffer_internal_1 (prev);
9446 }
9447 }
9448 }
9449
9450
9451 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9452 F's desired tool-bar contents. F->tool_bar_items must have
9453 been set up previously by calling prepare_menu_bars. */
9454
9455 static void
9456 build_desired_tool_bar_string (f)
9457 struct frame *f;
9458 {
9459 int i, size, size_needed;
9460 struct gcpro gcpro1, gcpro2, gcpro3;
9461 Lisp_Object image, plist, props;
9462
9463 image = plist = props = Qnil;
9464 GCPRO3 (image, plist, props);
9465
9466 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9467 Otherwise, make a new string. */
9468
9469 /* The size of the string we might be able to reuse. */
9470 size = (STRINGP (f->desired_tool_bar_string)
9471 ? SCHARS (f->desired_tool_bar_string)
9472 : 0);
9473
9474 /* We need one space in the string for each image. */
9475 size_needed = f->n_tool_bar_items;
9476
9477 /* Reuse f->desired_tool_bar_string, if possible. */
9478 if (size < size_needed || NILP (f->desired_tool_bar_string))
9479 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9480 make_number (' '));
9481 else
9482 {
9483 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9484 Fremove_text_properties (make_number (0), make_number (size),
9485 props, f->desired_tool_bar_string);
9486 }
9487
9488 /* Put a `display' property on the string for the images to display,
9489 put a `menu_item' property on tool-bar items with a value that
9490 is the index of the item in F's tool-bar item vector. */
9491 for (i = 0; i < f->n_tool_bar_items; ++i)
9492 {
9493 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9494
9495 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9496 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9497 int hmargin, vmargin, relief, idx, end;
9498 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9499
9500 /* If image is a vector, choose the image according to the
9501 button state. */
9502 image = PROP (TOOL_BAR_ITEM_IMAGES);
9503 if (VECTORP (image))
9504 {
9505 if (enabled_p)
9506 idx = (selected_p
9507 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9508 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9509 else
9510 idx = (selected_p
9511 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9512 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9513
9514 xassert (ASIZE (image) >= idx);
9515 image = AREF (image, idx);
9516 }
9517 else
9518 idx = -1;
9519
9520 /* Ignore invalid image specifications. */
9521 if (!valid_image_p (image))
9522 continue;
9523
9524 /* Display the tool-bar button pressed, or depressed. */
9525 plist = Fcopy_sequence (XCDR (image));
9526
9527 /* Compute margin and relief to draw. */
9528 relief = (tool_bar_button_relief >= 0
9529 ? tool_bar_button_relief
9530 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9531 hmargin = vmargin = relief;
9532
9533 if (INTEGERP (Vtool_bar_button_margin)
9534 && XINT (Vtool_bar_button_margin) > 0)
9535 {
9536 hmargin += XFASTINT (Vtool_bar_button_margin);
9537 vmargin += XFASTINT (Vtool_bar_button_margin);
9538 }
9539 else if (CONSP (Vtool_bar_button_margin))
9540 {
9541 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9542 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9543 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9544
9545 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9546 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9547 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9548 }
9549
9550 if (auto_raise_tool_bar_buttons_p)
9551 {
9552 /* Add a `:relief' property to the image spec if the item is
9553 selected. */
9554 if (selected_p)
9555 {
9556 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9557 hmargin -= relief;
9558 vmargin -= relief;
9559 }
9560 }
9561 else
9562 {
9563 /* If image is selected, display it pressed, i.e. with a
9564 negative relief. If it's not selected, display it with a
9565 raised relief. */
9566 plist = Fplist_put (plist, QCrelief,
9567 (selected_p
9568 ? make_number (-relief)
9569 : make_number (relief)));
9570 hmargin -= relief;
9571 vmargin -= relief;
9572 }
9573
9574 /* Put a margin around the image. */
9575 if (hmargin || vmargin)
9576 {
9577 if (hmargin == vmargin)
9578 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9579 else
9580 plist = Fplist_put (plist, QCmargin,
9581 Fcons (make_number (hmargin),
9582 make_number (vmargin)));
9583 }
9584
9585 /* If button is not enabled, and we don't have special images
9586 for the disabled state, make the image appear disabled by
9587 applying an appropriate algorithm to it. */
9588 if (!enabled_p && idx < 0)
9589 plist = Fplist_put (plist, QCconversion, Qdisabled);
9590
9591 /* Put a `display' text property on the string for the image to
9592 display. Put a `menu-item' property on the string that gives
9593 the start of this item's properties in the tool-bar items
9594 vector. */
9595 image = Fcons (Qimage, plist);
9596 props = list4 (Qdisplay, image,
9597 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9598
9599 /* Let the last image hide all remaining spaces in the tool bar
9600 string. The string can be longer than needed when we reuse a
9601 previous string. */
9602 if (i + 1 == f->n_tool_bar_items)
9603 end = SCHARS (f->desired_tool_bar_string);
9604 else
9605 end = i + 1;
9606 Fadd_text_properties (make_number (i), make_number (end),
9607 props, f->desired_tool_bar_string);
9608 #undef PROP
9609 }
9610
9611 UNGCPRO;
9612 }
9613
9614
9615 /* Display one line of the tool-bar of frame IT->f.
9616
9617 HEIGHT specifies the desired height of the tool-bar line.
9618 If the actual height of the glyph row is less than HEIGHT, the
9619 row's height is increased to HEIGHT, and the icons are centered
9620 vertically in the new height.
9621
9622 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9623 count a final empty row in case the tool-bar width exactly matches
9624 the window width.
9625 */
9626
9627 static void
9628 display_tool_bar_line (it, height)
9629 struct it *it;
9630 int height;
9631 {
9632 struct glyph_row *row = it->glyph_row;
9633 int max_x = it->last_visible_x;
9634 struct glyph *last;
9635
9636 prepare_desired_row (row);
9637 row->y = it->current_y;
9638
9639 /* Note that this isn't made use of if the face hasn't a box,
9640 so there's no need to check the face here. */
9641 it->start_of_box_run_p = 1;
9642
9643 while (it->current_x < max_x)
9644 {
9645 int x, n_glyphs_before, i, nglyphs;
9646 struct it it_before;
9647
9648 /* Get the next display element. */
9649 if (!get_next_display_element (it))
9650 {
9651 /* Don't count empty row if we are counting needed tool-bar lines. */
9652 if (height < 0 && !it->hpos)
9653 return;
9654 break;
9655 }
9656
9657 /* Produce glyphs. */
9658 n_glyphs_before = row->used[TEXT_AREA];
9659 it_before = *it;
9660
9661 PRODUCE_GLYPHS (it);
9662
9663 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9664 i = 0;
9665 x = it_before.current_x;
9666 while (i < nglyphs)
9667 {
9668 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9669
9670 if (x + glyph->pixel_width > max_x)
9671 {
9672 /* Glyph doesn't fit on line. Backtrack. */
9673 row->used[TEXT_AREA] = n_glyphs_before;
9674 *it = it_before;
9675 /* If this is the only glyph on this line, it will never fit on the
9676 toolbar, so skip it. But ensure there is at least one glyph,
9677 so we don't accidentally disable the tool-bar. */
9678 if (n_glyphs_before == 0
9679 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9680 break;
9681 goto out;
9682 }
9683
9684 ++it->hpos;
9685 x += glyph->pixel_width;
9686 ++i;
9687 }
9688
9689 /* Stop at line ends. */
9690 if (ITERATOR_AT_END_OF_LINE_P (it))
9691 break;
9692
9693 set_iterator_to_next (it, 1);
9694 }
9695
9696 out:;
9697
9698 row->displays_text_p = row->used[TEXT_AREA] != 0;
9699
9700 /* Use default face for the border below the tool bar.
9701
9702 FIXME: When auto-resize-tool-bars is grow-only, there is
9703 no additional border below the possibly empty tool-bar lines.
9704 So to make the extra empty lines look "normal", we have to
9705 use the tool-bar face for the border too. */
9706 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9707 it->face_id = DEFAULT_FACE_ID;
9708
9709 extend_face_to_end_of_line (it);
9710 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9711 last->right_box_line_p = 1;
9712 if (last == row->glyphs[TEXT_AREA])
9713 last->left_box_line_p = 1;
9714
9715 /* Make line the desired height and center it vertically. */
9716 if ((height -= it->max_ascent + it->max_descent) > 0)
9717 {
9718 /* Don't add more than one line height. */
9719 height %= FRAME_LINE_HEIGHT (it->f);
9720 it->max_ascent += height / 2;
9721 it->max_descent += (height + 1) / 2;
9722 }
9723
9724 compute_line_metrics (it);
9725
9726 /* If line is empty, make it occupy the rest of the tool-bar. */
9727 if (!row->displays_text_p)
9728 {
9729 row->height = row->phys_height = it->last_visible_y - row->y;
9730 row->visible_height = row->height;
9731 row->ascent = row->phys_ascent = 0;
9732 row->extra_line_spacing = 0;
9733 }
9734
9735 row->full_width_p = 1;
9736 row->continued_p = 0;
9737 row->truncated_on_left_p = 0;
9738 row->truncated_on_right_p = 0;
9739
9740 it->current_x = it->hpos = 0;
9741 it->current_y += row->height;
9742 ++it->vpos;
9743 ++it->glyph_row;
9744 }
9745
9746
9747 /* Max tool-bar height. */
9748
9749 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9750 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9751
9752 /* Value is the number of screen lines needed to make all tool-bar
9753 items of frame F visible. The number of actual rows needed is
9754 returned in *N_ROWS if non-NULL. */
9755
9756 static int
9757 tool_bar_lines_needed (f, n_rows)
9758 struct frame *f;
9759 int *n_rows;
9760 {
9761 struct window *w = XWINDOW (f->tool_bar_window);
9762 struct it it;
9763 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9764 the desired matrix, so use (unused) mode-line row as temporary row to
9765 avoid destroying the first tool-bar row. */
9766 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9767
9768 /* Initialize an iterator for iteration over
9769 F->desired_tool_bar_string in the tool-bar window of frame F. */
9770 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9771 it.first_visible_x = 0;
9772 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9773 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9774
9775 while (!ITERATOR_AT_END_P (&it))
9776 {
9777 clear_glyph_row (temp_row);
9778 it.glyph_row = temp_row;
9779 display_tool_bar_line (&it, -1);
9780 }
9781 clear_glyph_row (temp_row);
9782
9783 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9784 if (n_rows)
9785 *n_rows = it.vpos > 0 ? it.vpos : -1;
9786
9787 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9788 }
9789
9790
9791 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9792 0, 1, 0,
9793 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9794 (frame)
9795 Lisp_Object frame;
9796 {
9797 struct frame *f;
9798 struct window *w;
9799 int nlines = 0;
9800
9801 if (NILP (frame))
9802 frame = selected_frame;
9803 else
9804 CHECK_FRAME (frame);
9805 f = XFRAME (frame);
9806
9807 if (WINDOWP (f->tool_bar_window)
9808 || (w = XWINDOW (f->tool_bar_window),
9809 WINDOW_TOTAL_LINES (w) > 0))
9810 {
9811 update_tool_bar (f, 1);
9812 if (f->n_tool_bar_items)
9813 {
9814 build_desired_tool_bar_string (f);
9815 nlines = tool_bar_lines_needed (f, NULL);
9816 }
9817 }
9818
9819 return make_number (nlines);
9820 }
9821
9822
9823 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9824 height should be changed. */
9825
9826 static int
9827 redisplay_tool_bar (f)
9828 struct frame *f;
9829 {
9830 struct window *w;
9831 struct it it;
9832 struct glyph_row *row;
9833
9834 #ifdef USE_GTK
9835 if (FRAME_EXTERNAL_TOOL_BAR (f))
9836 update_frame_tool_bar (f);
9837 return 0;
9838 #endif
9839
9840 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9841 do anything. This means you must start with tool-bar-lines
9842 non-zero to get the auto-sizing effect. Or in other words, you
9843 can turn off tool-bars by specifying tool-bar-lines zero. */
9844 if (!WINDOWP (f->tool_bar_window)
9845 || (w = XWINDOW (f->tool_bar_window),
9846 WINDOW_TOTAL_LINES (w) == 0))
9847 return 0;
9848
9849 /* Set up an iterator for the tool-bar window. */
9850 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9851 it.first_visible_x = 0;
9852 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9853 row = it.glyph_row;
9854
9855 /* Build a string that represents the contents of the tool-bar. */
9856 build_desired_tool_bar_string (f);
9857 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9858
9859 if (f->n_tool_bar_rows == 0)
9860 {
9861 int nlines;
9862
9863 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9864 nlines != WINDOW_TOTAL_LINES (w)))
9865 {
9866 extern Lisp_Object Qtool_bar_lines;
9867 Lisp_Object frame;
9868 int old_height = WINDOW_TOTAL_LINES (w);
9869
9870 XSETFRAME (frame, f);
9871 Fmodify_frame_parameters (frame,
9872 Fcons (Fcons (Qtool_bar_lines,
9873 make_number (nlines)),
9874 Qnil));
9875 if (WINDOW_TOTAL_LINES (w) != old_height)
9876 {
9877 clear_glyph_matrix (w->desired_matrix);
9878 fonts_changed_p = 1;
9879 return 1;
9880 }
9881 }
9882 }
9883
9884 /* Display as many lines as needed to display all tool-bar items. */
9885
9886 if (f->n_tool_bar_rows > 0)
9887 {
9888 int border, rows, height, extra;
9889
9890 if (INTEGERP (Vtool_bar_border))
9891 border = XINT (Vtool_bar_border);
9892 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9893 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9894 else if (EQ (Vtool_bar_border, Qborder_width))
9895 border = f->border_width;
9896 else
9897 border = 0;
9898 if (border < 0)
9899 border = 0;
9900
9901 rows = f->n_tool_bar_rows;
9902 height = max (1, (it.last_visible_y - border) / rows);
9903 extra = it.last_visible_y - border - height * rows;
9904
9905 while (it.current_y < it.last_visible_y)
9906 {
9907 int h = 0;
9908 if (extra > 0 && rows-- > 0)
9909 {
9910 h = (extra + rows - 1) / rows;
9911 extra -= h;
9912 }
9913 display_tool_bar_line (&it, height + h);
9914 }
9915 }
9916 else
9917 {
9918 while (it.current_y < it.last_visible_y)
9919 display_tool_bar_line (&it, 0);
9920 }
9921
9922 /* It doesn't make much sense to try scrolling in the tool-bar
9923 window, so don't do it. */
9924 w->desired_matrix->no_scrolling_p = 1;
9925 w->must_be_updated_p = 1;
9926
9927 if (!NILP (Vauto_resize_tool_bars))
9928 {
9929 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9930 int change_height_p = 0;
9931
9932 /* If we couldn't display everything, change the tool-bar's
9933 height if there is room for more. */
9934 if (IT_STRING_CHARPOS (it) < it.end_charpos
9935 && it.current_y < max_tool_bar_height)
9936 change_height_p = 1;
9937
9938 row = it.glyph_row - 1;
9939
9940 /* If there are blank lines at the end, except for a partially
9941 visible blank line at the end that is smaller than
9942 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9943 if (!row->displays_text_p
9944 && row->height >= FRAME_LINE_HEIGHT (f))
9945 change_height_p = 1;
9946
9947 /* If row displays tool-bar items, but is partially visible,
9948 change the tool-bar's height. */
9949 if (row->displays_text_p
9950 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9951 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9952 change_height_p = 1;
9953
9954 /* Resize windows as needed by changing the `tool-bar-lines'
9955 frame parameter. */
9956 if (change_height_p)
9957 {
9958 extern Lisp_Object Qtool_bar_lines;
9959 Lisp_Object frame;
9960 int old_height = WINDOW_TOTAL_LINES (w);
9961 int nrows;
9962 int nlines = tool_bar_lines_needed (f, &nrows);
9963
9964 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
9965 && !f->minimize_tool_bar_window_p)
9966 ? (nlines > old_height)
9967 : (nlines != old_height));
9968 f->minimize_tool_bar_window_p = 0;
9969
9970 if (change_height_p)
9971 {
9972 XSETFRAME (frame, f);
9973 Fmodify_frame_parameters (frame,
9974 Fcons (Fcons (Qtool_bar_lines,
9975 make_number (nlines)),
9976 Qnil));
9977 if (WINDOW_TOTAL_LINES (w) != old_height)
9978 {
9979 clear_glyph_matrix (w->desired_matrix);
9980 f->n_tool_bar_rows = nrows;
9981 fonts_changed_p = 1;
9982 return 1;
9983 }
9984 }
9985 }
9986 }
9987
9988 f->minimize_tool_bar_window_p = 0;
9989 return 0;
9990 }
9991
9992
9993 /* Get information about the tool-bar item which is displayed in GLYPH
9994 on frame F. Return in *PROP_IDX the index where tool-bar item
9995 properties start in F->tool_bar_items. Value is zero if
9996 GLYPH doesn't display a tool-bar item. */
9997
9998 static int
9999 tool_bar_item_info (f, glyph, prop_idx)
10000 struct frame *f;
10001 struct glyph *glyph;
10002 int *prop_idx;
10003 {
10004 Lisp_Object prop;
10005 int success_p;
10006 int charpos;
10007
10008 /* This function can be called asynchronously, which means we must
10009 exclude any possibility that Fget_text_property signals an
10010 error. */
10011 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10012 charpos = max (0, charpos);
10013
10014 /* Get the text property `menu-item' at pos. The value of that
10015 property is the start index of this item's properties in
10016 F->tool_bar_items. */
10017 prop = Fget_text_property (make_number (charpos),
10018 Qmenu_item, f->current_tool_bar_string);
10019 if (INTEGERP (prop))
10020 {
10021 *prop_idx = XINT (prop);
10022 success_p = 1;
10023 }
10024 else
10025 success_p = 0;
10026
10027 return success_p;
10028 }
10029
10030 \f
10031 /* Get information about the tool-bar item at position X/Y on frame F.
10032 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10033 the current matrix of the tool-bar window of F, or NULL if not
10034 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10035 item in F->tool_bar_items. Value is
10036
10037 -1 if X/Y is not on a tool-bar item
10038 0 if X/Y is on the same item that was highlighted before.
10039 1 otherwise. */
10040
10041 static int
10042 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10043 struct frame *f;
10044 int x, y;
10045 struct glyph **glyph;
10046 int *hpos, *vpos, *prop_idx;
10047 {
10048 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10049 struct window *w = XWINDOW (f->tool_bar_window);
10050 int area;
10051
10052 /* Find the glyph under X/Y. */
10053 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10054 if (*glyph == NULL)
10055 return -1;
10056
10057 /* Get the start of this tool-bar item's properties in
10058 f->tool_bar_items. */
10059 if (!tool_bar_item_info (f, *glyph, prop_idx))
10060 return -1;
10061
10062 /* Is mouse on the highlighted item? */
10063 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10064 && *vpos >= dpyinfo->mouse_face_beg_row
10065 && *vpos <= dpyinfo->mouse_face_end_row
10066 && (*vpos > dpyinfo->mouse_face_beg_row
10067 || *hpos >= dpyinfo->mouse_face_beg_col)
10068 && (*vpos < dpyinfo->mouse_face_end_row
10069 || *hpos < dpyinfo->mouse_face_end_col
10070 || dpyinfo->mouse_face_past_end))
10071 return 0;
10072
10073 return 1;
10074 }
10075
10076
10077 /* EXPORT:
10078 Handle mouse button event on the tool-bar of frame F, at
10079 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10080 0 for button release. MODIFIERS is event modifiers for button
10081 release. */
10082
10083 void
10084 handle_tool_bar_click (f, x, y, down_p, modifiers)
10085 struct frame *f;
10086 int x, y, down_p;
10087 unsigned int modifiers;
10088 {
10089 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10090 struct window *w = XWINDOW (f->tool_bar_window);
10091 int hpos, vpos, prop_idx;
10092 struct glyph *glyph;
10093 Lisp_Object enabled_p;
10094
10095 /* If not on the highlighted tool-bar item, return. */
10096 frame_to_window_pixel_xy (w, &x, &y);
10097 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10098 return;
10099
10100 /* If item is disabled, do nothing. */
10101 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10102 if (NILP (enabled_p))
10103 return;
10104
10105 if (down_p)
10106 {
10107 /* Show item in pressed state. */
10108 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10109 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10110 last_tool_bar_item = prop_idx;
10111 }
10112 else
10113 {
10114 Lisp_Object key, frame;
10115 struct input_event event;
10116 EVENT_INIT (event);
10117
10118 /* Show item in released state. */
10119 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10120 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10121
10122 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10123
10124 XSETFRAME (frame, f);
10125 event.kind = TOOL_BAR_EVENT;
10126 event.frame_or_window = frame;
10127 event.arg = frame;
10128 kbd_buffer_store_event (&event);
10129
10130 event.kind = TOOL_BAR_EVENT;
10131 event.frame_or_window = frame;
10132 event.arg = key;
10133 event.modifiers = modifiers;
10134 kbd_buffer_store_event (&event);
10135 last_tool_bar_item = -1;
10136 }
10137 }
10138
10139
10140 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10141 tool-bar window-relative coordinates X/Y. Called from
10142 note_mouse_highlight. */
10143
10144 static void
10145 note_tool_bar_highlight (f, x, y)
10146 struct frame *f;
10147 int x, y;
10148 {
10149 Lisp_Object window = f->tool_bar_window;
10150 struct window *w = XWINDOW (window);
10151 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10152 int hpos, vpos;
10153 struct glyph *glyph;
10154 struct glyph_row *row;
10155 int i;
10156 Lisp_Object enabled_p;
10157 int prop_idx;
10158 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10159 int mouse_down_p, rc;
10160
10161 /* Function note_mouse_highlight is called with negative x(y
10162 values when mouse moves outside of the frame. */
10163 if (x <= 0 || y <= 0)
10164 {
10165 clear_mouse_face (dpyinfo);
10166 return;
10167 }
10168
10169 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10170 if (rc < 0)
10171 {
10172 /* Not on tool-bar item. */
10173 clear_mouse_face (dpyinfo);
10174 return;
10175 }
10176 else if (rc == 0)
10177 /* On same tool-bar item as before. */
10178 goto set_help_echo;
10179
10180 clear_mouse_face (dpyinfo);
10181
10182 /* Mouse is down, but on different tool-bar item? */
10183 mouse_down_p = (dpyinfo->grabbed
10184 && f == last_mouse_frame
10185 && FRAME_LIVE_P (f));
10186 if (mouse_down_p
10187 && last_tool_bar_item != prop_idx)
10188 return;
10189
10190 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10191 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10192
10193 /* If tool-bar item is not enabled, don't highlight it. */
10194 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10195 if (!NILP (enabled_p))
10196 {
10197 /* Compute the x-position of the glyph. In front and past the
10198 image is a space. We include this in the highlighted area. */
10199 row = MATRIX_ROW (w->current_matrix, vpos);
10200 for (i = x = 0; i < hpos; ++i)
10201 x += row->glyphs[TEXT_AREA][i].pixel_width;
10202
10203 /* Record this as the current active region. */
10204 dpyinfo->mouse_face_beg_col = hpos;
10205 dpyinfo->mouse_face_beg_row = vpos;
10206 dpyinfo->mouse_face_beg_x = x;
10207 dpyinfo->mouse_face_beg_y = row->y;
10208 dpyinfo->mouse_face_past_end = 0;
10209
10210 dpyinfo->mouse_face_end_col = hpos + 1;
10211 dpyinfo->mouse_face_end_row = vpos;
10212 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10213 dpyinfo->mouse_face_end_y = row->y;
10214 dpyinfo->mouse_face_window = window;
10215 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10216
10217 /* Display it as active. */
10218 show_mouse_face (dpyinfo, draw);
10219 dpyinfo->mouse_face_image_state = draw;
10220 }
10221
10222 set_help_echo:
10223
10224 /* Set help_echo_string to a help string to display for this tool-bar item.
10225 XTread_socket does the rest. */
10226 help_echo_object = help_echo_window = Qnil;
10227 help_echo_pos = -1;
10228 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10229 if (NILP (help_echo_string))
10230 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10231 }
10232
10233 #endif /* HAVE_WINDOW_SYSTEM */
10234
10235
10236 \f
10237 /************************************************************************
10238 Horizontal scrolling
10239 ************************************************************************/
10240
10241 static int hscroll_window_tree P_ ((Lisp_Object));
10242 static int hscroll_windows P_ ((Lisp_Object));
10243
10244 /* For all leaf windows in the window tree rooted at WINDOW, set their
10245 hscroll value so that PT is (i) visible in the window, and (ii) so
10246 that it is not within a certain margin at the window's left and
10247 right border. Value is non-zero if any window's hscroll has been
10248 changed. */
10249
10250 static int
10251 hscroll_window_tree (window)
10252 Lisp_Object window;
10253 {
10254 int hscrolled_p = 0;
10255 int hscroll_relative_p = FLOATP (Vhscroll_step);
10256 int hscroll_step_abs = 0;
10257 double hscroll_step_rel = 0;
10258
10259 if (hscroll_relative_p)
10260 {
10261 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10262 if (hscroll_step_rel < 0)
10263 {
10264 hscroll_relative_p = 0;
10265 hscroll_step_abs = 0;
10266 }
10267 }
10268 else if (INTEGERP (Vhscroll_step))
10269 {
10270 hscroll_step_abs = XINT (Vhscroll_step);
10271 if (hscroll_step_abs < 0)
10272 hscroll_step_abs = 0;
10273 }
10274 else
10275 hscroll_step_abs = 0;
10276
10277 while (WINDOWP (window))
10278 {
10279 struct window *w = XWINDOW (window);
10280
10281 if (WINDOWP (w->hchild))
10282 hscrolled_p |= hscroll_window_tree (w->hchild);
10283 else if (WINDOWP (w->vchild))
10284 hscrolled_p |= hscroll_window_tree (w->vchild);
10285 else if (w->cursor.vpos >= 0)
10286 {
10287 int h_margin;
10288 int text_area_width;
10289 struct glyph_row *current_cursor_row
10290 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10291 struct glyph_row *desired_cursor_row
10292 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10293 struct glyph_row *cursor_row
10294 = (desired_cursor_row->enabled_p
10295 ? desired_cursor_row
10296 : current_cursor_row);
10297
10298 text_area_width = window_box_width (w, TEXT_AREA);
10299
10300 /* Scroll when cursor is inside this scroll margin. */
10301 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10302
10303 if ((XFASTINT (w->hscroll)
10304 && w->cursor.x <= h_margin)
10305 || (cursor_row->enabled_p
10306 && cursor_row->truncated_on_right_p
10307 && (w->cursor.x >= text_area_width - h_margin)))
10308 {
10309 struct it it;
10310 int hscroll;
10311 struct buffer *saved_current_buffer;
10312 int pt;
10313 int wanted_x;
10314
10315 /* Find point in a display of infinite width. */
10316 saved_current_buffer = current_buffer;
10317 current_buffer = XBUFFER (w->buffer);
10318
10319 if (w == XWINDOW (selected_window))
10320 pt = BUF_PT (current_buffer);
10321 else
10322 {
10323 pt = marker_position (w->pointm);
10324 pt = max (BEGV, pt);
10325 pt = min (ZV, pt);
10326 }
10327
10328 /* Move iterator to pt starting at cursor_row->start in
10329 a line with infinite width. */
10330 init_to_row_start (&it, w, cursor_row);
10331 it.last_visible_x = INFINITY;
10332 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10333 current_buffer = saved_current_buffer;
10334
10335 /* Position cursor in window. */
10336 if (!hscroll_relative_p && hscroll_step_abs == 0)
10337 hscroll = max (0, (it.current_x
10338 - (ITERATOR_AT_END_OF_LINE_P (&it)
10339 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10340 : (text_area_width / 2))))
10341 / FRAME_COLUMN_WIDTH (it.f);
10342 else if (w->cursor.x >= text_area_width - h_margin)
10343 {
10344 if (hscroll_relative_p)
10345 wanted_x = text_area_width * (1 - hscroll_step_rel)
10346 - h_margin;
10347 else
10348 wanted_x = text_area_width
10349 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10350 - h_margin;
10351 hscroll
10352 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10353 }
10354 else
10355 {
10356 if (hscroll_relative_p)
10357 wanted_x = text_area_width * hscroll_step_rel
10358 + h_margin;
10359 else
10360 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10361 + h_margin;
10362 hscroll
10363 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10364 }
10365 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10366
10367 /* Don't call Fset_window_hscroll if value hasn't
10368 changed because it will prevent redisplay
10369 optimizations. */
10370 if (XFASTINT (w->hscroll) != hscroll)
10371 {
10372 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10373 w->hscroll = make_number (hscroll);
10374 hscrolled_p = 1;
10375 }
10376 }
10377 }
10378
10379 window = w->next;
10380 }
10381
10382 /* Value is non-zero if hscroll of any leaf window has been changed. */
10383 return hscrolled_p;
10384 }
10385
10386
10387 /* Set hscroll so that cursor is visible and not inside horizontal
10388 scroll margins for all windows in the tree rooted at WINDOW. See
10389 also hscroll_window_tree above. Value is non-zero if any window's
10390 hscroll has been changed. If it has, desired matrices on the frame
10391 of WINDOW are cleared. */
10392
10393 static int
10394 hscroll_windows (window)
10395 Lisp_Object window;
10396 {
10397 int hscrolled_p;
10398
10399 if (automatic_hscrolling_p)
10400 {
10401 hscrolled_p = hscroll_window_tree (window);
10402 if (hscrolled_p)
10403 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10404 }
10405 else
10406 hscrolled_p = 0;
10407 return hscrolled_p;
10408 }
10409
10410
10411 \f
10412 /************************************************************************
10413 Redisplay
10414 ************************************************************************/
10415
10416 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10417 to a non-zero value. This is sometimes handy to have in a debugger
10418 session. */
10419
10420 #if GLYPH_DEBUG
10421
10422 /* First and last unchanged row for try_window_id. */
10423
10424 int debug_first_unchanged_at_end_vpos;
10425 int debug_last_unchanged_at_beg_vpos;
10426
10427 /* Delta vpos and y. */
10428
10429 int debug_dvpos, debug_dy;
10430
10431 /* Delta in characters and bytes for try_window_id. */
10432
10433 int debug_delta, debug_delta_bytes;
10434
10435 /* Values of window_end_pos and window_end_vpos at the end of
10436 try_window_id. */
10437
10438 EMACS_INT debug_end_pos, debug_end_vpos;
10439
10440 /* Append a string to W->desired_matrix->method. FMT is a printf
10441 format string. A1...A9 are a supplement for a variable-length
10442 argument list. If trace_redisplay_p is non-zero also printf the
10443 resulting string to stderr. */
10444
10445 static void
10446 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10447 struct window *w;
10448 char *fmt;
10449 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10450 {
10451 char buffer[512];
10452 char *method = w->desired_matrix->method;
10453 int len = strlen (method);
10454 int size = sizeof w->desired_matrix->method;
10455 int remaining = size - len - 1;
10456
10457 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10458 if (len && remaining)
10459 {
10460 method[len] = '|';
10461 --remaining, ++len;
10462 }
10463
10464 strncpy (method + len, buffer, remaining);
10465
10466 if (trace_redisplay_p)
10467 fprintf (stderr, "%p (%s): %s\n",
10468 w,
10469 ((BUFFERP (w->buffer)
10470 && STRINGP (XBUFFER (w->buffer)->name))
10471 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10472 : "no buffer"),
10473 buffer);
10474 }
10475
10476 #endif /* GLYPH_DEBUG */
10477
10478
10479 /* Value is non-zero if all changes in window W, which displays
10480 current_buffer, are in the text between START and END. START is a
10481 buffer position, END is given as a distance from Z. Used in
10482 redisplay_internal for display optimization. */
10483
10484 static INLINE int
10485 text_outside_line_unchanged_p (w, start, end)
10486 struct window *w;
10487 int start, end;
10488 {
10489 int unchanged_p = 1;
10490
10491 /* If text or overlays have changed, see where. */
10492 if (XFASTINT (w->last_modified) < MODIFF
10493 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10494 {
10495 /* Gap in the line? */
10496 if (GPT < start || Z - GPT < end)
10497 unchanged_p = 0;
10498
10499 /* Changes start in front of the line, or end after it? */
10500 if (unchanged_p
10501 && (BEG_UNCHANGED < start - 1
10502 || END_UNCHANGED < end))
10503 unchanged_p = 0;
10504
10505 /* If selective display, can't optimize if changes start at the
10506 beginning of the line. */
10507 if (unchanged_p
10508 && INTEGERP (current_buffer->selective_display)
10509 && XINT (current_buffer->selective_display) > 0
10510 && (BEG_UNCHANGED < start || GPT <= start))
10511 unchanged_p = 0;
10512
10513 /* If there are overlays at the start or end of the line, these
10514 may have overlay strings with newlines in them. A change at
10515 START, for instance, may actually concern the display of such
10516 overlay strings as well, and they are displayed on different
10517 lines. So, quickly rule out this case. (For the future, it
10518 might be desirable to implement something more telling than
10519 just BEG/END_UNCHANGED.) */
10520 if (unchanged_p)
10521 {
10522 if (BEG + BEG_UNCHANGED == start
10523 && overlay_touches_p (start))
10524 unchanged_p = 0;
10525 if (END_UNCHANGED == end
10526 && overlay_touches_p (Z - end))
10527 unchanged_p = 0;
10528 }
10529 }
10530
10531 return unchanged_p;
10532 }
10533
10534
10535 /* Do a frame update, taking possible shortcuts into account. This is
10536 the main external entry point for redisplay.
10537
10538 If the last redisplay displayed an echo area message and that message
10539 is no longer requested, we clear the echo area or bring back the
10540 mini-buffer if that is in use. */
10541
10542 void
10543 redisplay ()
10544 {
10545 redisplay_internal (0);
10546 }
10547
10548
10549 static Lisp_Object
10550 overlay_arrow_string_or_property (var)
10551 Lisp_Object var;
10552 {
10553 Lisp_Object val;
10554
10555 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10556 return val;
10557
10558 return Voverlay_arrow_string;
10559 }
10560
10561 /* Return 1 if there are any overlay-arrows in current_buffer. */
10562 static int
10563 overlay_arrow_in_current_buffer_p ()
10564 {
10565 Lisp_Object vlist;
10566
10567 for (vlist = Voverlay_arrow_variable_list;
10568 CONSP (vlist);
10569 vlist = XCDR (vlist))
10570 {
10571 Lisp_Object var = XCAR (vlist);
10572 Lisp_Object val;
10573
10574 if (!SYMBOLP (var))
10575 continue;
10576 val = find_symbol_value (var);
10577 if (MARKERP (val)
10578 && current_buffer == XMARKER (val)->buffer)
10579 return 1;
10580 }
10581 return 0;
10582 }
10583
10584
10585 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10586 has changed. */
10587
10588 static int
10589 overlay_arrows_changed_p ()
10590 {
10591 Lisp_Object vlist;
10592
10593 for (vlist = Voverlay_arrow_variable_list;
10594 CONSP (vlist);
10595 vlist = XCDR (vlist))
10596 {
10597 Lisp_Object var = XCAR (vlist);
10598 Lisp_Object val, pstr;
10599
10600 if (!SYMBOLP (var))
10601 continue;
10602 val = find_symbol_value (var);
10603 if (!MARKERP (val))
10604 continue;
10605 if (! EQ (COERCE_MARKER (val),
10606 Fget (var, Qlast_arrow_position))
10607 || ! (pstr = overlay_arrow_string_or_property (var),
10608 EQ (pstr, Fget (var, Qlast_arrow_string))))
10609 return 1;
10610 }
10611 return 0;
10612 }
10613
10614 /* Mark overlay arrows to be updated on next redisplay. */
10615
10616 static void
10617 update_overlay_arrows (up_to_date)
10618 int up_to_date;
10619 {
10620 Lisp_Object vlist;
10621
10622 for (vlist = Voverlay_arrow_variable_list;
10623 CONSP (vlist);
10624 vlist = XCDR (vlist))
10625 {
10626 Lisp_Object var = XCAR (vlist);
10627
10628 if (!SYMBOLP (var))
10629 continue;
10630
10631 if (up_to_date > 0)
10632 {
10633 Lisp_Object val = find_symbol_value (var);
10634 Fput (var, Qlast_arrow_position,
10635 COERCE_MARKER (val));
10636 Fput (var, Qlast_arrow_string,
10637 overlay_arrow_string_or_property (var));
10638 }
10639 else if (up_to_date < 0
10640 || !NILP (Fget (var, Qlast_arrow_position)))
10641 {
10642 Fput (var, Qlast_arrow_position, Qt);
10643 Fput (var, Qlast_arrow_string, Qt);
10644 }
10645 }
10646 }
10647
10648
10649 /* Return overlay arrow string to display at row.
10650 Return integer (bitmap number) for arrow bitmap in left fringe.
10651 Return nil if no overlay arrow. */
10652
10653 static Lisp_Object
10654 overlay_arrow_at_row (it, row)
10655 struct it *it;
10656 struct glyph_row *row;
10657 {
10658 Lisp_Object vlist;
10659
10660 for (vlist = Voverlay_arrow_variable_list;
10661 CONSP (vlist);
10662 vlist = XCDR (vlist))
10663 {
10664 Lisp_Object var = XCAR (vlist);
10665 Lisp_Object val;
10666
10667 if (!SYMBOLP (var))
10668 continue;
10669
10670 val = find_symbol_value (var);
10671
10672 if (MARKERP (val)
10673 && current_buffer == XMARKER (val)->buffer
10674 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10675 {
10676 if (FRAME_WINDOW_P (it->f)
10677 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10678 {
10679 #ifdef HAVE_WINDOW_SYSTEM
10680 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10681 {
10682 int fringe_bitmap;
10683 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10684 return make_number (fringe_bitmap);
10685 }
10686 #endif
10687 return make_number (-1); /* Use default arrow bitmap */
10688 }
10689 return overlay_arrow_string_or_property (var);
10690 }
10691 }
10692
10693 return Qnil;
10694 }
10695
10696 /* Return 1 if point moved out of or into a composition. Otherwise
10697 return 0. PREV_BUF and PREV_PT are the last point buffer and
10698 position. BUF and PT are the current point buffer and position. */
10699
10700 int
10701 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10702 struct buffer *prev_buf, *buf;
10703 int prev_pt, pt;
10704 {
10705 int start, end;
10706 Lisp_Object prop;
10707 Lisp_Object buffer;
10708
10709 XSETBUFFER (buffer, buf);
10710 /* Check a composition at the last point if point moved within the
10711 same buffer. */
10712 if (prev_buf == buf)
10713 {
10714 if (prev_pt == pt)
10715 /* Point didn't move. */
10716 return 0;
10717
10718 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10719 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10720 && COMPOSITION_VALID_P (start, end, prop)
10721 && start < prev_pt && end > prev_pt)
10722 /* The last point was within the composition. Return 1 iff
10723 point moved out of the composition. */
10724 return (pt <= start || pt >= end);
10725 }
10726
10727 /* Check a composition at the current point. */
10728 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10729 && find_composition (pt, -1, &start, &end, &prop, buffer)
10730 && COMPOSITION_VALID_P (start, end, prop)
10731 && start < pt && end > pt);
10732 }
10733
10734
10735 /* Reconsider the setting of B->clip_changed which is displayed
10736 in window W. */
10737
10738 static INLINE void
10739 reconsider_clip_changes (w, b)
10740 struct window *w;
10741 struct buffer *b;
10742 {
10743 if (b->clip_changed
10744 && !NILP (w->window_end_valid)
10745 && w->current_matrix->buffer == b
10746 && w->current_matrix->zv == BUF_ZV (b)
10747 && w->current_matrix->begv == BUF_BEGV (b))
10748 b->clip_changed = 0;
10749
10750 /* If display wasn't paused, and W is not a tool bar window, see if
10751 point has been moved into or out of a composition. In that case,
10752 we set b->clip_changed to 1 to force updating the screen. If
10753 b->clip_changed has already been set to 1, we can skip this
10754 check. */
10755 if (!b->clip_changed
10756 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10757 {
10758 int pt;
10759
10760 if (w == XWINDOW (selected_window))
10761 pt = BUF_PT (current_buffer);
10762 else
10763 pt = marker_position (w->pointm);
10764
10765 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10766 || pt != XINT (w->last_point))
10767 && check_point_in_composition (w->current_matrix->buffer,
10768 XINT (w->last_point),
10769 XBUFFER (w->buffer), pt))
10770 b->clip_changed = 1;
10771 }
10772 }
10773 \f
10774
10775 /* Select FRAME to forward the values of frame-local variables into C
10776 variables so that the redisplay routines can access those values
10777 directly. */
10778
10779 static void
10780 select_frame_for_redisplay (frame)
10781 Lisp_Object frame;
10782 {
10783 Lisp_Object tail, sym, val;
10784 Lisp_Object old = selected_frame;
10785
10786 selected_frame = frame;
10787
10788 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10789 if (CONSP (XCAR (tail))
10790 && (sym = XCAR (XCAR (tail)),
10791 SYMBOLP (sym))
10792 && (sym = indirect_variable (sym),
10793 val = SYMBOL_VALUE (sym),
10794 (BUFFER_LOCAL_VALUEP (val)
10795 || SOME_BUFFER_LOCAL_VALUEP (val)))
10796 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10797 /* Use find_symbol_value rather than Fsymbol_value
10798 to avoid an error if it is void. */
10799 find_symbol_value (sym);
10800
10801 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10802 if (CONSP (XCAR (tail))
10803 && (sym = XCAR (XCAR (tail)),
10804 SYMBOLP (sym))
10805 && (sym = indirect_variable (sym),
10806 val = SYMBOL_VALUE (sym),
10807 (BUFFER_LOCAL_VALUEP (val)
10808 || SOME_BUFFER_LOCAL_VALUEP (val)))
10809 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10810 find_symbol_value (sym);
10811 }
10812
10813
10814 #define STOP_POLLING \
10815 do { if (! polling_stopped_here) stop_polling (); \
10816 polling_stopped_here = 1; } while (0)
10817
10818 #define RESUME_POLLING \
10819 do { if (polling_stopped_here) start_polling (); \
10820 polling_stopped_here = 0; } while (0)
10821
10822
10823 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10824 response to any user action; therefore, we should preserve the echo
10825 area. (Actually, our caller does that job.) Perhaps in the future
10826 avoid recentering windows if it is not necessary; currently that
10827 causes some problems. */
10828
10829 static void
10830 redisplay_internal (preserve_echo_area)
10831 int preserve_echo_area;
10832 {
10833 struct window *w = XWINDOW (selected_window);
10834 struct frame *f;
10835 int pause;
10836 int must_finish = 0;
10837 struct text_pos tlbufpos, tlendpos;
10838 int number_of_visible_frames;
10839 int count;
10840 struct frame *sf;
10841 int polling_stopped_here = 0;
10842
10843 /* Non-zero means redisplay has to consider all windows on all
10844 frames. Zero means, only selected_window is considered. */
10845 int consider_all_windows_p;
10846
10847 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10848
10849 /* No redisplay if running in batch mode or frame is not yet fully
10850 initialized, or redisplay is explicitly turned off by setting
10851 Vinhibit_redisplay. */
10852 if (noninteractive
10853 || !NILP (Vinhibit_redisplay))
10854 return;
10855
10856 /* Don't examine these until after testing Vinhibit_redisplay.
10857 When Emacs is shutting down, perhaps because its connection to
10858 X has dropped, we should not look at them at all. */
10859 f = XFRAME (w->frame);
10860 sf = SELECTED_FRAME ();
10861
10862 if (!f->glyphs_initialized_p)
10863 return;
10864
10865 /* The flag redisplay_performed_directly_p is set by
10866 direct_output_for_insert when it already did the whole screen
10867 update necessary. */
10868 if (redisplay_performed_directly_p)
10869 {
10870 redisplay_performed_directly_p = 0;
10871 if (!hscroll_windows (selected_window))
10872 return;
10873 }
10874
10875 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10876 if (popup_activated ())
10877 return;
10878 #endif
10879
10880 /* I don't think this happens but let's be paranoid. */
10881 if (redisplaying_p)
10882 return;
10883
10884 /* Record a function that resets redisplaying_p to its old value
10885 when we leave this function. */
10886 count = SPECPDL_INDEX ();
10887 record_unwind_protect (unwind_redisplay,
10888 Fcons (make_number (redisplaying_p), selected_frame));
10889 ++redisplaying_p;
10890 specbind (Qinhibit_free_realized_faces, Qnil);
10891
10892 {
10893 Lisp_Object tail, frame;
10894
10895 FOR_EACH_FRAME (tail, frame)
10896 {
10897 struct frame *f = XFRAME (frame);
10898 f->already_hscrolled_p = 0;
10899 }
10900 }
10901
10902 retry:
10903 pause = 0;
10904 reconsider_clip_changes (w, current_buffer);
10905 last_escape_glyph_frame = NULL;
10906 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10907
10908 /* If new fonts have been loaded that make a glyph matrix adjustment
10909 necessary, do it. */
10910 if (fonts_changed_p)
10911 {
10912 adjust_glyphs (NULL);
10913 ++windows_or_buffers_changed;
10914 fonts_changed_p = 0;
10915 }
10916
10917 /* If face_change_count is non-zero, init_iterator will free all
10918 realized faces, which includes the faces referenced from current
10919 matrices. So, we can't reuse current matrices in this case. */
10920 if (face_change_count)
10921 ++windows_or_buffers_changed;
10922
10923 if (! FRAME_WINDOW_P (sf)
10924 && previous_terminal_frame != sf)
10925 {
10926 /* Since frames on an ASCII terminal share the same display
10927 area, displaying a different frame means redisplay the whole
10928 thing. */
10929 windows_or_buffers_changed++;
10930 SET_FRAME_GARBAGED (sf);
10931 XSETFRAME (Vterminal_frame, sf);
10932 }
10933 previous_terminal_frame = sf;
10934
10935 /* Set the visible flags for all frames. Do this before checking
10936 for resized or garbaged frames; they want to know if their frames
10937 are visible. See the comment in frame.h for
10938 FRAME_SAMPLE_VISIBILITY. */
10939 {
10940 Lisp_Object tail, frame;
10941
10942 number_of_visible_frames = 0;
10943
10944 FOR_EACH_FRAME (tail, frame)
10945 {
10946 struct frame *f = XFRAME (frame);
10947
10948 FRAME_SAMPLE_VISIBILITY (f);
10949 if (FRAME_VISIBLE_P (f))
10950 ++number_of_visible_frames;
10951 clear_desired_matrices (f);
10952 }
10953 }
10954
10955 /* Notice any pending interrupt request to change frame size. */
10956 do_pending_window_change (1);
10957
10958 /* Clear frames marked as garbaged. */
10959 if (frame_garbaged)
10960 clear_garbaged_frames ();
10961
10962 /* Build menubar and tool-bar items. */
10963 if (NILP (Vmemory_full))
10964 prepare_menu_bars ();
10965
10966 if (windows_or_buffers_changed)
10967 update_mode_lines++;
10968
10969 /* Detect case that we need to write or remove a star in the mode line. */
10970 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10971 {
10972 w->update_mode_line = Qt;
10973 if (buffer_shared > 1)
10974 update_mode_lines++;
10975 }
10976
10977 /* If %c is in the mode line, update it if needed. */
10978 if (!NILP (w->column_number_displayed)
10979 /* This alternative quickly identifies a common case
10980 where no change is needed. */
10981 && !(PT == XFASTINT (w->last_point)
10982 && XFASTINT (w->last_modified) >= MODIFF
10983 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10984 && (XFASTINT (w->column_number_displayed)
10985 != (int) current_column ())) /* iftc */
10986 w->update_mode_line = Qt;
10987
10988 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10989
10990 /* The variable buffer_shared is set in redisplay_window and
10991 indicates that we redisplay a buffer in different windows. See
10992 there. */
10993 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10994 || cursor_type_changed);
10995
10996 /* If specs for an arrow have changed, do thorough redisplay
10997 to ensure we remove any arrow that should no longer exist. */
10998 if (overlay_arrows_changed_p ())
10999 consider_all_windows_p = windows_or_buffers_changed = 1;
11000
11001 /* Normally the message* functions will have already displayed and
11002 updated the echo area, but the frame may have been trashed, or
11003 the update may have been preempted, so display the echo area
11004 again here. Checking message_cleared_p captures the case that
11005 the echo area should be cleared. */
11006 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11007 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11008 || (message_cleared_p
11009 && minibuf_level == 0
11010 /* If the mini-window is currently selected, this means the
11011 echo-area doesn't show through. */
11012 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11013 {
11014 int window_height_changed_p = echo_area_display (0);
11015 must_finish = 1;
11016
11017 /* If we don't display the current message, don't clear the
11018 message_cleared_p flag, because, if we did, we wouldn't clear
11019 the echo area in the next redisplay which doesn't preserve
11020 the echo area. */
11021 if (!display_last_displayed_message_p)
11022 message_cleared_p = 0;
11023
11024 if (fonts_changed_p)
11025 goto retry;
11026 else if (window_height_changed_p)
11027 {
11028 consider_all_windows_p = 1;
11029 ++update_mode_lines;
11030 ++windows_or_buffers_changed;
11031
11032 /* If window configuration was changed, frames may have been
11033 marked garbaged. Clear them or we will experience
11034 surprises wrt scrolling. */
11035 if (frame_garbaged)
11036 clear_garbaged_frames ();
11037 }
11038 }
11039 else if (EQ (selected_window, minibuf_window)
11040 && (current_buffer->clip_changed
11041 || XFASTINT (w->last_modified) < MODIFF
11042 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11043 && resize_mini_window (w, 0))
11044 {
11045 /* Resized active mini-window to fit the size of what it is
11046 showing if its contents might have changed. */
11047 must_finish = 1;
11048 consider_all_windows_p = 1;
11049 ++windows_or_buffers_changed;
11050 ++update_mode_lines;
11051
11052 /* If window configuration was changed, frames may have been
11053 marked garbaged. Clear them or we will experience
11054 surprises wrt scrolling. */
11055 if (frame_garbaged)
11056 clear_garbaged_frames ();
11057 }
11058
11059
11060 /* If showing the region, and mark has changed, we must redisplay
11061 the whole window. The assignment to this_line_start_pos prevents
11062 the optimization directly below this if-statement. */
11063 if (((!NILP (Vtransient_mark_mode)
11064 && !NILP (XBUFFER (w->buffer)->mark_active))
11065 != !NILP (w->region_showing))
11066 || (!NILP (w->region_showing)
11067 && !EQ (w->region_showing,
11068 Fmarker_position (XBUFFER (w->buffer)->mark))))
11069 CHARPOS (this_line_start_pos) = 0;
11070
11071 /* Optimize the case that only the line containing the cursor in the
11072 selected window has changed. Variables starting with this_ are
11073 set in display_line and record information about the line
11074 containing the cursor. */
11075 tlbufpos = this_line_start_pos;
11076 tlendpos = this_line_end_pos;
11077 if (!consider_all_windows_p
11078 && CHARPOS (tlbufpos) > 0
11079 && NILP (w->update_mode_line)
11080 && !current_buffer->clip_changed
11081 && !current_buffer->prevent_redisplay_optimizations_p
11082 && FRAME_VISIBLE_P (XFRAME (w->frame))
11083 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11084 /* Make sure recorded data applies to current buffer, etc. */
11085 && this_line_buffer == current_buffer
11086 && current_buffer == XBUFFER (w->buffer)
11087 && NILP (w->force_start)
11088 && NILP (w->optional_new_start)
11089 /* Point must be on the line that we have info recorded about. */
11090 && PT >= CHARPOS (tlbufpos)
11091 && PT <= Z - CHARPOS (tlendpos)
11092 /* All text outside that line, including its final newline,
11093 must be unchanged */
11094 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11095 CHARPOS (tlendpos)))
11096 {
11097 if (CHARPOS (tlbufpos) > BEGV
11098 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11099 && (CHARPOS (tlbufpos) == ZV
11100 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11101 /* Former continuation line has disappeared by becoming empty */
11102 goto cancel;
11103 else if (XFASTINT (w->last_modified) < MODIFF
11104 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11105 || MINI_WINDOW_P (w))
11106 {
11107 /* We have to handle the case of continuation around a
11108 wide-column character (See the comment in indent.c around
11109 line 885).
11110
11111 For instance, in the following case:
11112
11113 -------- Insert --------
11114 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11115 J_I_ ==> J_I_ `^^' are cursors.
11116 ^^ ^^
11117 -------- --------
11118
11119 As we have to redraw the line above, we should goto cancel. */
11120
11121 struct it it;
11122 int line_height_before = this_line_pixel_height;
11123
11124 /* Note that start_display will handle the case that the
11125 line starting at tlbufpos is a continuation lines. */
11126 start_display (&it, w, tlbufpos);
11127
11128 /* Implementation note: It this still necessary? */
11129 if (it.current_x != this_line_start_x)
11130 goto cancel;
11131
11132 TRACE ((stderr, "trying display optimization 1\n"));
11133 w->cursor.vpos = -1;
11134 overlay_arrow_seen = 0;
11135 it.vpos = this_line_vpos;
11136 it.current_y = this_line_y;
11137 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11138 display_line (&it);
11139
11140 /* If line contains point, is not continued,
11141 and ends at same distance from eob as before, we win */
11142 if (w->cursor.vpos >= 0
11143 /* Line is not continued, otherwise this_line_start_pos
11144 would have been set to 0 in display_line. */
11145 && CHARPOS (this_line_start_pos)
11146 /* Line ends as before. */
11147 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11148 /* Line has same height as before. Otherwise other lines
11149 would have to be shifted up or down. */
11150 && this_line_pixel_height == line_height_before)
11151 {
11152 /* If this is not the window's last line, we must adjust
11153 the charstarts of the lines below. */
11154 if (it.current_y < it.last_visible_y)
11155 {
11156 struct glyph_row *row
11157 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11158 int delta, delta_bytes;
11159
11160 if (Z - CHARPOS (tlendpos) == ZV)
11161 {
11162 /* This line ends at end of (accessible part of)
11163 buffer. There is no newline to count. */
11164 delta = (Z
11165 - CHARPOS (tlendpos)
11166 - MATRIX_ROW_START_CHARPOS (row));
11167 delta_bytes = (Z_BYTE
11168 - BYTEPOS (tlendpos)
11169 - MATRIX_ROW_START_BYTEPOS (row));
11170 }
11171 else
11172 {
11173 /* This line ends in a newline. Must take
11174 account of the newline and the rest of the
11175 text that follows. */
11176 delta = (Z
11177 - CHARPOS (tlendpos)
11178 - MATRIX_ROW_START_CHARPOS (row));
11179 delta_bytes = (Z_BYTE
11180 - BYTEPOS (tlendpos)
11181 - MATRIX_ROW_START_BYTEPOS (row));
11182 }
11183
11184 increment_matrix_positions (w->current_matrix,
11185 this_line_vpos + 1,
11186 w->current_matrix->nrows,
11187 delta, delta_bytes);
11188 }
11189
11190 /* If this row displays text now but previously didn't,
11191 or vice versa, w->window_end_vpos may have to be
11192 adjusted. */
11193 if ((it.glyph_row - 1)->displays_text_p)
11194 {
11195 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11196 XSETINT (w->window_end_vpos, this_line_vpos);
11197 }
11198 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11199 && this_line_vpos > 0)
11200 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11201 w->window_end_valid = Qnil;
11202
11203 /* Update hint: No need to try to scroll in update_window. */
11204 w->desired_matrix->no_scrolling_p = 1;
11205
11206 #if GLYPH_DEBUG
11207 *w->desired_matrix->method = 0;
11208 debug_method_add (w, "optimization 1");
11209 #endif
11210 #ifdef HAVE_WINDOW_SYSTEM
11211 update_window_fringes (w, 0);
11212 #endif
11213 goto update;
11214 }
11215 else
11216 goto cancel;
11217 }
11218 else if (/* Cursor position hasn't changed. */
11219 PT == XFASTINT (w->last_point)
11220 /* Make sure the cursor was last displayed
11221 in this window. Otherwise we have to reposition it. */
11222 && 0 <= w->cursor.vpos
11223 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11224 {
11225 if (!must_finish)
11226 {
11227 do_pending_window_change (1);
11228
11229 /* We used to always goto end_of_redisplay here, but this
11230 isn't enough if we have a blinking cursor. */
11231 if (w->cursor_off_p == w->last_cursor_off_p)
11232 goto end_of_redisplay;
11233 }
11234 goto update;
11235 }
11236 /* If highlighting the region, or if the cursor is in the echo area,
11237 then we can't just move the cursor. */
11238 else if (! (!NILP (Vtransient_mark_mode)
11239 && !NILP (current_buffer->mark_active))
11240 && (EQ (selected_window, current_buffer->last_selected_window)
11241 || highlight_nonselected_windows)
11242 && NILP (w->region_showing)
11243 && NILP (Vshow_trailing_whitespace)
11244 && !cursor_in_echo_area)
11245 {
11246 struct it it;
11247 struct glyph_row *row;
11248
11249 /* Skip from tlbufpos to PT and see where it is. Note that
11250 PT may be in invisible text. If so, we will end at the
11251 next visible position. */
11252 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11253 NULL, DEFAULT_FACE_ID);
11254 it.current_x = this_line_start_x;
11255 it.current_y = this_line_y;
11256 it.vpos = this_line_vpos;
11257
11258 /* The call to move_it_to stops in front of PT, but
11259 moves over before-strings. */
11260 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11261
11262 if (it.vpos == this_line_vpos
11263 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11264 row->enabled_p))
11265 {
11266 xassert (this_line_vpos == it.vpos);
11267 xassert (this_line_y == it.current_y);
11268 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11269 #if GLYPH_DEBUG
11270 *w->desired_matrix->method = 0;
11271 debug_method_add (w, "optimization 3");
11272 #endif
11273 goto update;
11274 }
11275 else
11276 goto cancel;
11277 }
11278
11279 cancel:
11280 /* Text changed drastically or point moved off of line. */
11281 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11282 }
11283
11284 CHARPOS (this_line_start_pos) = 0;
11285 consider_all_windows_p |= buffer_shared > 1;
11286 ++clear_face_cache_count;
11287 #ifdef HAVE_WINDOW_SYSTEM
11288 ++clear_image_cache_count;
11289 #endif
11290
11291 /* Build desired matrices, and update the display. If
11292 consider_all_windows_p is non-zero, do it for all windows on all
11293 frames. Otherwise do it for selected_window, only. */
11294
11295 if (consider_all_windows_p)
11296 {
11297 Lisp_Object tail, frame;
11298
11299 FOR_EACH_FRAME (tail, frame)
11300 XFRAME (frame)->updated_p = 0;
11301
11302 /* Recompute # windows showing selected buffer. This will be
11303 incremented each time such a window is displayed. */
11304 buffer_shared = 0;
11305
11306 FOR_EACH_FRAME (tail, frame)
11307 {
11308 struct frame *f = XFRAME (frame);
11309
11310 if (FRAME_WINDOW_P (f) || f == sf)
11311 {
11312 if (! EQ (frame, selected_frame))
11313 /* Select the frame, for the sake of frame-local
11314 variables. */
11315 select_frame_for_redisplay (frame);
11316
11317 /* Mark all the scroll bars to be removed; we'll redeem
11318 the ones we want when we redisplay their windows. */
11319 if (condemn_scroll_bars_hook)
11320 condemn_scroll_bars_hook (f);
11321
11322 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11323 redisplay_windows (FRAME_ROOT_WINDOW (f));
11324
11325 /* Any scroll bars which redisplay_windows should have
11326 nuked should now go away. */
11327 if (judge_scroll_bars_hook)
11328 judge_scroll_bars_hook (f);
11329
11330 /* If fonts changed, display again. */
11331 /* ??? rms: I suspect it is a mistake to jump all the way
11332 back to retry here. It should just retry this frame. */
11333 if (fonts_changed_p)
11334 goto retry;
11335
11336 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11337 {
11338 /* See if we have to hscroll. */
11339 if (!f->already_hscrolled_p)
11340 {
11341 f->already_hscrolled_p = 1;
11342 if (hscroll_windows (f->root_window))
11343 goto retry;
11344 }
11345
11346 /* Prevent various kinds of signals during display
11347 update. stdio is not robust about handling
11348 signals, which can cause an apparent I/O
11349 error. */
11350 if (interrupt_input)
11351 unrequest_sigio ();
11352 STOP_POLLING;
11353
11354 /* Update the display. */
11355 set_window_update_flags (XWINDOW (f->root_window), 1);
11356 pause |= update_frame (f, 0, 0);
11357 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11358 if (pause)
11359 break;
11360 #endif
11361
11362 f->updated_p = 1;
11363 }
11364 }
11365 }
11366
11367 if (!pause)
11368 {
11369 /* Do the mark_window_display_accurate after all windows have
11370 been redisplayed because this call resets flags in buffers
11371 which are needed for proper redisplay. */
11372 FOR_EACH_FRAME (tail, frame)
11373 {
11374 struct frame *f = XFRAME (frame);
11375 if (f->updated_p)
11376 {
11377 mark_window_display_accurate (f->root_window, 1);
11378 if (frame_up_to_date_hook)
11379 frame_up_to_date_hook (f);
11380 }
11381 }
11382 }
11383 }
11384 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11385 {
11386 Lisp_Object mini_window;
11387 struct frame *mini_frame;
11388
11389 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11390 /* Use list_of_error, not Qerror, so that
11391 we catch only errors and don't run the debugger. */
11392 internal_condition_case_1 (redisplay_window_1, selected_window,
11393 list_of_error,
11394 redisplay_window_error);
11395
11396 /* Compare desired and current matrices, perform output. */
11397
11398 update:
11399 /* If fonts changed, display again. */
11400 if (fonts_changed_p)
11401 goto retry;
11402
11403 /* Prevent various kinds of signals during display update.
11404 stdio is not robust about handling signals,
11405 which can cause an apparent I/O error. */
11406 if (interrupt_input)
11407 unrequest_sigio ();
11408 STOP_POLLING;
11409
11410 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11411 {
11412 if (hscroll_windows (selected_window))
11413 goto retry;
11414
11415 XWINDOW (selected_window)->must_be_updated_p = 1;
11416 pause = update_frame (sf, 0, 0);
11417 }
11418
11419 /* We may have called echo_area_display at the top of this
11420 function. If the echo area is on another frame, that may
11421 have put text on a frame other than the selected one, so the
11422 above call to update_frame would not have caught it. Catch
11423 it here. */
11424 mini_window = FRAME_MINIBUF_WINDOW (sf);
11425 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11426
11427 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11428 {
11429 XWINDOW (mini_window)->must_be_updated_p = 1;
11430 pause |= update_frame (mini_frame, 0, 0);
11431 if (!pause && hscroll_windows (mini_window))
11432 goto retry;
11433 }
11434 }
11435
11436 /* If display was paused because of pending input, make sure we do a
11437 thorough update the next time. */
11438 if (pause)
11439 {
11440 /* Prevent the optimization at the beginning of
11441 redisplay_internal that tries a single-line update of the
11442 line containing the cursor in the selected window. */
11443 CHARPOS (this_line_start_pos) = 0;
11444
11445 /* Let the overlay arrow be updated the next time. */
11446 update_overlay_arrows (0);
11447
11448 /* If we pause after scrolling, some rows in the current
11449 matrices of some windows are not valid. */
11450 if (!WINDOW_FULL_WIDTH_P (w)
11451 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11452 update_mode_lines = 1;
11453 }
11454 else
11455 {
11456 if (!consider_all_windows_p)
11457 {
11458 /* This has already been done above if
11459 consider_all_windows_p is set. */
11460 mark_window_display_accurate_1 (w, 1);
11461
11462 /* Say overlay arrows are up to date. */
11463 update_overlay_arrows (1);
11464
11465 if (frame_up_to_date_hook != 0)
11466 frame_up_to_date_hook (sf);
11467 }
11468
11469 update_mode_lines = 0;
11470 windows_or_buffers_changed = 0;
11471 cursor_type_changed = 0;
11472 }
11473
11474 /* Start SIGIO interrupts coming again. Having them off during the
11475 code above makes it less likely one will discard output, but not
11476 impossible, since there might be stuff in the system buffer here.
11477 But it is much hairier to try to do anything about that. */
11478 if (interrupt_input)
11479 request_sigio ();
11480 RESUME_POLLING;
11481
11482 /* If a frame has become visible which was not before, redisplay
11483 again, so that we display it. Expose events for such a frame
11484 (which it gets when becoming visible) don't call the parts of
11485 redisplay constructing glyphs, so simply exposing a frame won't
11486 display anything in this case. So, we have to display these
11487 frames here explicitly. */
11488 if (!pause)
11489 {
11490 Lisp_Object tail, frame;
11491 int new_count = 0;
11492
11493 FOR_EACH_FRAME (tail, frame)
11494 {
11495 int this_is_visible = 0;
11496
11497 if (XFRAME (frame)->visible)
11498 this_is_visible = 1;
11499 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11500 if (XFRAME (frame)->visible)
11501 this_is_visible = 1;
11502
11503 if (this_is_visible)
11504 new_count++;
11505 }
11506
11507 if (new_count != number_of_visible_frames)
11508 windows_or_buffers_changed++;
11509 }
11510
11511 /* Change frame size now if a change is pending. */
11512 do_pending_window_change (1);
11513
11514 /* If we just did a pending size change, or have additional
11515 visible frames, redisplay again. */
11516 if (windows_or_buffers_changed && !pause)
11517 goto retry;
11518
11519 /* Clear the face cache eventually. */
11520 if (consider_all_windows_p)
11521 {
11522 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11523 {
11524 clear_face_cache (0);
11525 clear_face_cache_count = 0;
11526 }
11527 #ifdef HAVE_WINDOW_SYSTEM
11528 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11529 {
11530 Lisp_Object tail, frame;
11531 FOR_EACH_FRAME (tail, frame)
11532 {
11533 struct frame *f = XFRAME (frame);
11534 if (FRAME_WINDOW_P (f))
11535 clear_image_cache (f, 0);
11536 }
11537 clear_image_cache_count = 0;
11538 }
11539 #endif /* HAVE_WINDOW_SYSTEM */
11540 }
11541
11542 end_of_redisplay:
11543 unbind_to (count, Qnil);
11544 RESUME_POLLING;
11545 }
11546
11547
11548 /* Redisplay, but leave alone any recent echo area message unless
11549 another message has been requested in its place.
11550
11551 This is useful in situations where you need to redisplay but no
11552 user action has occurred, making it inappropriate for the message
11553 area to be cleared. See tracking_off and
11554 wait_reading_process_output for examples of these situations.
11555
11556 FROM_WHERE is an integer saying from where this function was
11557 called. This is useful for debugging. */
11558
11559 void
11560 redisplay_preserve_echo_area (from_where)
11561 int from_where;
11562 {
11563 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11564
11565 if (!NILP (echo_area_buffer[1]))
11566 {
11567 /* We have a previously displayed message, but no current
11568 message. Redisplay the previous message. */
11569 display_last_displayed_message_p = 1;
11570 redisplay_internal (1);
11571 display_last_displayed_message_p = 0;
11572 }
11573 else
11574 redisplay_internal (1);
11575
11576 if (rif != NULL && rif->flush_display_optional)
11577 rif->flush_display_optional (NULL);
11578 }
11579
11580
11581 /* Function registered with record_unwind_protect in
11582 redisplay_internal. Reset redisplaying_p to the value it had
11583 before redisplay_internal was called, and clear
11584 prevent_freeing_realized_faces_p. It also selects the previously
11585 selected frame. */
11586
11587 static Lisp_Object
11588 unwind_redisplay (val)
11589 Lisp_Object val;
11590 {
11591 Lisp_Object old_redisplaying_p, old_frame;
11592
11593 old_redisplaying_p = XCAR (val);
11594 redisplaying_p = XFASTINT (old_redisplaying_p);
11595 old_frame = XCDR (val);
11596 if (! EQ (old_frame, selected_frame))
11597 select_frame_for_redisplay (old_frame);
11598 return Qnil;
11599 }
11600
11601
11602 /* Mark the display of window W as accurate or inaccurate. If
11603 ACCURATE_P is non-zero mark display of W as accurate. If
11604 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11605 redisplay_internal is called. */
11606
11607 static void
11608 mark_window_display_accurate_1 (w, accurate_p)
11609 struct window *w;
11610 int accurate_p;
11611 {
11612 if (BUFFERP (w->buffer))
11613 {
11614 struct buffer *b = XBUFFER (w->buffer);
11615
11616 w->last_modified
11617 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11618 w->last_overlay_modified
11619 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11620 w->last_had_star
11621 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11622
11623 if (accurate_p)
11624 {
11625 b->clip_changed = 0;
11626 b->prevent_redisplay_optimizations_p = 0;
11627
11628 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11629 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11630 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11631 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11632
11633 w->current_matrix->buffer = b;
11634 w->current_matrix->begv = BUF_BEGV (b);
11635 w->current_matrix->zv = BUF_ZV (b);
11636
11637 w->last_cursor = w->cursor;
11638 w->last_cursor_off_p = w->cursor_off_p;
11639
11640 if (w == XWINDOW (selected_window))
11641 w->last_point = make_number (BUF_PT (b));
11642 else
11643 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11644 }
11645 }
11646
11647 if (accurate_p)
11648 {
11649 w->window_end_valid = w->buffer;
11650 #if 0 /* This is incorrect with variable-height lines. */
11651 xassert (XINT (w->window_end_vpos)
11652 < (WINDOW_TOTAL_LINES (w)
11653 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11654 #endif
11655 w->update_mode_line = Qnil;
11656 }
11657 }
11658
11659
11660 /* Mark the display of windows in the window tree rooted at WINDOW as
11661 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11662 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11663 be redisplayed the next time redisplay_internal is called. */
11664
11665 void
11666 mark_window_display_accurate (window, accurate_p)
11667 Lisp_Object window;
11668 int accurate_p;
11669 {
11670 struct window *w;
11671
11672 for (; !NILP (window); window = w->next)
11673 {
11674 w = XWINDOW (window);
11675 mark_window_display_accurate_1 (w, accurate_p);
11676
11677 if (!NILP (w->vchild))
11678 mark_window_display_accurate (w->vchild, accurate_p);
11679 if (!NILP (w->hchild))
11680 mark_window_display_accurate (w->hchild, accurate_p);
11681 }
11682
11683 if (accurate_p)
11684 {
11685 update_overlay_arrows (1);
11686 }
11687 else
11688 {
11689 /* Force a thorough redisplay the next time by setting
11690 last_arrow_position and last_arrow_string to t, which is
11691 unequal to any useful value of Voverlay_arrow_... */
11692 update_overlay_arrows (-1);
11693 }
11694 }
11695
11696
11697 /* Return value in display table DP (Lisp_Char_Table *) for character
11698 C. Since a display table doesn't have any parent, we don't have to
11699 follow parent. Do not call this function directly but use the
11700 macro DISP_CHAR_VECTOR. */
11701
11702 Lisp_Object
11703 disp_char_vector (dp, c)
11704 struct Lisp_Char_Table *dp;
11705 int c;
11706 {
11707 int code[4], i;
11708 Lisp_Object val;
11709
11710 if (SINGLE_BYTE_CHAR_P (c))
11711 return (dp->contents[c]);
11712
11713 SPLIT_CHAR (c, code[0], code[1], code[2]);
11714 if (code[1] < 32)
11715 code[1] = -1;
11716 else if (code[2] < 32)
11717 code[2] = -1;
11718
11719 /* Here, the possible range of code[0] (== charset ID) is
11720 128..max_charset. Since the top level char table contains data
11721 for multibyte characters after 256th element, we must increment
11722 code[0] by 128 to get a correct index. */
11723 code[0] += 128;
11724 code[3] = -1; /* anchor */
11725
11726 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11727 {
11728 val = dp->contents[code[i]];
11729 if (!SUB_CHAR_TABLE_P (val))
11730 return (NILP (val) ? dp->defalt : val);
11731 }
11732
11733 /* Here, val is a sub char table. We return the default value of
11734 it. */
11735 return (dp->defalt);
11736 }
11737
11738
11739 \f
11740 /***********************************************************************
11741 Window Redisplay
11742 ***********************************************************************/
11743
11744 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11745
11746 static void
11747 redisplay_windows (window)
11748 Lisp_Object window;
11749 {
11750 while (!NILP (window))
11751 {
11752 struct window *w = XWINDOW (window);
11753
11754 if (!NILP (w->hchild))
11755 redisplay_windows (w->hchild);
11756 else if (!NILP (w->vchild))
11757 redisplay_windows (w->vchild);
11758 else
11759 {
11760 displayed_buffer = XBUFFER (w->buffer);
11761 /* Use list_of_error, not Qerror, so that
11762 we catch only errors and don't run the debugger. */
11763 internal_condition_case_1 (redisplay_window_0, window,
11764 list_of_error,
11765 redisplay_window_error);
11766 }
11767
11768 window = w->next;
11769 }
11770 }
11771
11772 static Lisp_Object
11773 redisplay_window_error ()
11774 {
11775 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11776 return Qnil;
11777 }
11778
11779 static Lisp_Object
11780 redisplay_window_0 (window)
11781 Lisp_Object window;
11782 {
11783 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11784 redisplay_window (window, 0);
11785 return Qnil;
11786 }
11787
11788 static Lisp_Object
11789 redisplay_window_1 (window)
11790 Lisp_Object window;
11791 {
11792 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11793 redisplay_window (window, 1);
11794 return Qnil;
11795 }
11796 \f
11797
11798 /* Increment GLYPH until it reaches END or CONDITION fails while
11799 adding (GLYPH)->pixel_width to X. */
11800
11801 #define SKIP_GLYPHS(glyph, end, x, condition) \
11802 do \
11803 { \
11804 (x) += (glyph)->pixel_width; \
11805 ++(glyph); \
11806 } \
11807 while ((glyph) < (end) && (condition))
11808
11809
11810 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11811 DELTA is the number of bytes by which positions recorded in ROW
11812 differ from current buffer positions.
11813
11814 Return 0 if cursor is not on this row. 1 otherwise. */
11815
11816 int
11817 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11818 struct window *w;
11819 struct glyph_row *row;
11820 struct glyph_matrix *matrix;
11821 int delta, delta_bytes, dy, dvpos;
11822 {
11823 struct glyph *glyph = row->glyphs[TEXT_AREA];
11824 struct glyph *end = glyph + row->used[TEXT_AREA];
11825 struct glyph *cursor = NULL;
11826 /* The first glyph that starts a sequence of glyphs from string. */
11827 struct glyph *string_start;
11828 /* The X coordinate of string_start. */
11829 int string_start_x;
11830 /* The last known character position. */
11831 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11832 /* The last known character position before string_start. */
11833 int string_before_pos;
11834 int x = row->x;
11835 int cursor_x = x;
11836 int cursor_from_overlay_pos = 0;
11837 int pt_old = PT - delta;
11838
11839 /* Skip over glyphs not having an object at the start of the row.
11840 These are special glyphs like truncation marks on terminal
11841 frames. */
11842 if (row->displays_text_p)
11843 while (glyph < end
11844 && INTEGERP (glyph->object)
11845 && glyph->charpos < 0)
11846 {
11847 x += glyph->pixel_width;
11848 ++glyph;
11849 }
11850
11851 string_start = NULL;
11852 while (glyph < end
11853 && !INTEGERP (glyph->object)
11854 && (!BUFFERP (glyph->object)
11855 || (last_pos = glyph->charpos) < pt_old))
11856 {
11857 if (! STRINGP (glyph->object))
11858 {
11859 string_start = NULL;
11860 x += glyph->pixel_width;
11861 ++glyph;
11862 if (cursor_from_overlay_pos
11863 && last_pos >= cursor_from_overlay_pos)
11864 {
11865 cursor_from_overlay_pos = 0;
11866 cursor = 0;
11867 }
11868 }
11869 else
11870 {
11871 if (string_start == NULL)
11872 {
11873 string_before_pos = last_pos;
11874 string_start = glyph;
11875 string_start_x = x;
11876 }
11877 /* Skip all glyphs from string. */
11878 do
11879 {
11880 Lisp_Object cprop;
11881 int pos;
11882 if ((cursor == NULL || glyph > cursor)
11883 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11884 Qcursor, (glyph)->object),
11885 !NILP (cprop))
11886 && (pos = string_buffer_position (w, glyph->object,
11887 string_before_pos),
11888 (pos == 0 /* From overlay */
11889 || pos == pt_old)))
11890 {
11891 /* Estimate overlay buffer position from the buffer
11892 positions of the glyphs before and after the overlay.
11893 Add 1 to last_pos so that if point corresponds to the
11894 glyph right after the overlay, we still use a 'cursor'
11895 property found in that overlay. */
11896 cursor_from_overlay_pos = (pos ? 0 : last_pos
11897 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11898 cursor = glyph;
11899 cursor_x = x;
11900 }
11901 x += glyph->pixel_width;
11902 ++glyph;
11903 }
11904 while (glyph < end && EQ (glyph->object, string_start->object));
11905 }
11906 }
11907
11908 if (cursor != NULL)
11909 {
11910 glyph = cursor;
11911 x = cursor_x;
11912 }
11913 else if (row->ends_in_ellipsis_p && glyph == end)
11914 {
11915 /* Scan back over the ellipsis glyphs, decrementing positions. */
11916 while (glyph > row->glyphs[TEXT_AREA]
11917 && (glyph - 1)->charpos == last_pos)
11918 glyph--, x -= glyph->pixel_width;
11919 /* That loop always goes one position too far,
11920 including the glyph before the ellipsis.
11921 So scan forward over that one. */
11922 x += glyph->pixel_width;
11923 glyph++;
11924 }
11925 else if (string_start
11926 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11927 {
11928 /* We may have skipped over point because the previous glyphs
11929 are from string. As there's no easy way to know the
11930 character position of the current glyph, find the correct
11931 glyph on point by scanning from string_start again. */
11932 Lisp_Object limit;
11933 Lisp_Object string;
11934 struct glyph *stop = glyph;
11935 int pos;
11936
11937 limit = make_number (pt_old + 1);
11938 glyph = string_start;
11939 x = string_start_x;
11940 string = glyph->object;
11941 pos = string_buffer_position (w, string, string_before_pos);
11942 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11943 because we always put cursor after overlay strings. */
11944 while (pos == 0 && glyph < stop)
11945 {
11946 string = glyph->object;
11947 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11948 if (glyph < stop)
11949 pos = string_buffer_position (w, glyph->object, string_before_pos);
11950 }
11951
11952 while (glyph < stop)
11953 {
11954 pos = XINT (Fnext_single_char_property_change
11955 (make_number (pos), Qdisplay, Qnil, limit));
11956 if (pos > pt_old)
11957 break;
11958 /* Skip glyphs from the same string. */
11959 string = glyph->object;
11960 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11961 /* Skip glyphs from an overlay. */
11962 while (glyph < stop
11963 && ! string_buffer_position (w, glyph->object, pos))
11964 {
11965 string = glyph->object;
11966 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11967 }
11968 }
11969
11970 /* If we reached the end of the line, and end was from a string,
11971 cursor is not on this line. */
11972 if (glyph == end && row->continued_p)
11973 return 0;
11974 }
11975
11976 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11977 w->cursor.x = x;
11978 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11979 w->cursor.y = row->y + dy;
11980
11981 if (w == XWINDOW (selected_window))
11982 {
11983 if (!row->continued_p
11984 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11985 && row->x == 0)
11986 {
11987 this_line_buffer = XBUFFER (w->buffer);
11988
11989 CHARPOS (this_line_start_pos)
11990 = MATRIX_ROW_START_CHARPOS (row) + delta;
11991 BYTEPOS (this_line_start_pos)
11992 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11993
11994 CHARPOS (this_line_end_pos)
11995 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11996 BYTEPOS (this_line_end_pos)
11997 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11998
11999 this_line_y = w->cursor.y;
12000 this_line_pixel_height = row->height;
12001 this_line_vpos = w->cursor.vpos;
12002 this_line_start_x = row->x;
12003 }
12004 else
12005 CHARPOS (this_line_start_pos) = 0;
12006 }
12007
12008 return 1;
12009 }
12010
12011
12012 /* Run window scroll functions, if any, for WINDOW with new window
12013 start STARTP. Sets the window start of WINDOW to that position.
12014
12015 We assume that the window's buffer is really current. */
12016
12017 static INLINE struct text_pos
12018 run_window_scroll_functions (window, startp)
12019 Lisp_Object window;
12020 struct text_pos startp;
12021 {
12022 struct window *w = XWINDOW (window);
12023 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12024
12025 if (current_buffer != XBUFFER (w->buffer))
12026 abort ();
12027
12028 if (!NILP (Vwindow_scroll_functions))
12029 {
12030 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12031 make_number (CHARPOS (startp)));
12032 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12033 /* In case the hook functions switch buffers. */
12034 if (current_buffer != XBUFFER (w->buffer))
12035 set_buffer_internal_1 (XBUFFER (w->buffer));
12036 }
12037
12038 return startp;
12039 }
12040
12041
12042 /* Make sure the line containing the cursor is fully visible.
12043 A value of 1 means there is nothing to be done.
12044 (Either the line is fully visible, or it cannot be made so,
12045 or we cannot tell.)
12046
12047 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12048 is higher than window.
12049
12050 A value of 0 means the caller should do scrolling
12051 as if point had gone off the screen. */
12052
12053 static int
12054 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12055 struct window *w;
12056 int force_p;
12057 int current_matrix_p;
12058 {
12059 struct glyph_matrix *matrix;
12060 struct glyph_row *row;
12061 int window_height;
12062
12063 if (!make_cursor_line_fully_visible_p)
12064 return 1;
12065
12066 /* It's not always possible to find the cursor, e.g, when a window
12067 is full of overlay strings. Don't do anything in that case. */
12068 if (w->cursor.vpos < 0)
12069 return 1;
12070
12071 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12072 row = MATRIX_ROW (matrix, w->cursor.vpos);
12073
12074 /* If the cursor row is not partially visible, there's nothing to do. */
12075 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12076 return 1;
12077
12078 /* If the row the cursor is in is taller than the window's height,
12079 it's not clear what to do, so do nothing. */
12080 window_height = window_box_height (w);
12081 if (row->height >= window_height)
12082 {
12083 if (!force_p || MINI_WINDOW_P (w)
12084 || w->vscroll || w->cursor.vpos == 0)
12085 return 1;
12086 }
12087 return 0;
12088
12089 #if 0
12090 /* This code used to try to scroll the window just enough to make
12091 the line visible. It returned 0 to say that the caller should
12092 allocate larger glyph matrices. */
12093
12094 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12095 {
12096 int dy = row->height - row->visible_height;
12097 w->vscroll = 0;
12098 w->cursor.y += dy;
12099 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12100 }
12101 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12102 {
12103 int dy = - (row->height - row->visible_height);
12104 w->vscroll = dy;
12105 w->cursor.y += dy;
12106 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12107 }
12108
12109 /* When we change the cursor y-position of the selected window,
12110 change this_line_y as well so that the display optimization for
12111 the cursor line of the selected window in redisplay_internal uses
12112 the correct y-position. */
12113 if (w == XWINDOW (selected_window))
12114 this_line_y = w->cursor.y;
12115
12116 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12117 redisplay with larger matrices. */
12118 if (matrix->nrows < required_matrix_height (w))
12119 {
12120 fonts_changed_p = 1;
12121 return 0;
12122 }
12123
12124 return 1;
12125 #endif /* 0 */
12126 }
12127
12128
12129 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12130 non-zero means only WINDOW is redisplayed in redisplay_internal.
12131 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12132 in redisplay_window to bring a partially visible line into view in
12133 the case that only the cursor has moved.
12134
12135 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12136 last screen line's vertical height extends past the end of the screen.
12137
12138 Value is
12139
12140 1 if scrolling succeeded
12141
12142 0 if scrolling didn't find point.
12143
12144 -1 if new fonts have been loaded so that we must interrupt
12145 redisplay, adjust glyph matrices, and try again. */
12146
12147 enum
12148 {
12149 SCROLLING_SUCCESS,
12150 SCROLLING_FAILED,
12151 SCROLLING_NEED_LARGER_MATRICES
12152 };
12153
12154 static int
12155 try_scrolling (window, just_this_one_p, scroll_conservatively,
12156 scroll_step, temp_scroll_step, last_line_misfit)
12157 Lisp_Object window;
12158 int just_this_one_p;
12159 EMACS_INT scroll_conservatively, scroll_step;
12160 int temp_scroll_step;
12161 int last_line_misfit;
12162 {
12163 struct window *w = XWINDOW (window);
12164 struct frame *f = XFRAME (w->frame);
12165 struct text_pos scroll_margin_pos;
12166 struct text_pos pos;
12167 struct text_pos startp;
12168 struct it it;
12169 Lisp_Object window_end;
12170 int this_scroll_margin;
12171 int dy = 0;
12172 int scroll_max;
12173 int rc;
12174 int amount_to_scroll = 0;
12175 Lisp_Object aggressive;
12176 int height;
12177 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12178
12179 #if GLYPH_DEBUG
12180 debug_method_add (w, "try_scrolling");
12181 #endif
12182
12183 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12184
12185 /* Compute scroll margin height in pixels. We scroll when point is
12186 within this distance from the top or bottom of the window. */
12187 if (scroll_margin > 0)
12188 {
12189 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12190 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12191 }
12192 else
12193 this_scroll_margin = 0;
12194
12195 /* Force scroll_conservatively to have a reasonable value so it doesn't
12196 cause an overflow while computing how much to scroll. */
12197 if (scroll_conservatively)
12198 scroll_conservatively = min (scroll_conservatively,
12199 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12200
12201 /* Compute how much we should try to scroll maximally to bring point
12202 into view. */
12203 if (scroll_step || scroll_conservatively || temp_scroll_step)
12204 scroll_max = max (scroll_step,
12205 max (scroll_conservatively, temp_scroll_step));
12206 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12207 || NUMBERP (current_buffer->scroll_up_aggressively))
12208 /* We're trying to scroll because of aggressive scrolling
12209 but no scroll_step is set. Choose an arbitrary one. Maybe
12210 there should be a variable for this. */
12211 scroll_max = 10;
12212 else
12213 scroll_max = 0;
12214 scroll_max *= FRAME_LINE_HEIGHT (f);
12215
12216 /* Decide whether we have to scroll down. Start at the window end
12217 and move this_scroll_margin up to find the position of the scroll
12218 margin. */
12219 window_end = Fwindow_end (window, Qt);
12220
12221 too_near_end:
12222
12223 CHARPOS (scroll_margin_pos) = XINT (window_end);
12224 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12225
12226 if (this_scroll_margin || extra_scroll_margin_lines)
12227 {
12228 start_display (&it, w, scroll_margin_pos);
12229 if (this_scroll_margin)
12230 move_it_vertically_backward (&it, this_scroll_margin);
12231 if (extra_scroll_margin_lines)
12232 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12233 scroll_margin_pos = it.current.pos;
12234 }
12235
12236 if (PT >= CHARPOS (scroll_margin_pos))
12237 {
12238 int y0;
12239
12240 /* Point is in the scroll margin at the bottom of the window, or
12241 below. Compute a new window start that makes point visible. */
12242
12243 /* Compute the distance from the scroll margin to PT.
12244 Give up if the distance is greater than scroll_max. */
12245 start_display (&it, w, scroll_margin_pos);
12246 y0 = it.current_y;
12247 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12248 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12249
12250 /* To make point visible, we have to move the window start
12251 down so that the line the cursor is in is visible, which
12252 means we have to add in the height of the cursor line. */
12253 dy = line_bottom_y (&it) - y0;
12254
12255 if (dy > scroll_max)
12256 return SCROLLING_FAILED;
12257
12258 /* Move the window start down. If scrolling conservatively,
12259 move it just enough down to make point visible. If
12260 scroll_step is set, move it down by scroll_step. */
12261 start_display (&it, w, startp);
12262
12263 if (scroll_conservatively)
12264 /* Set AMOUNT_TO_SCROLL to at least one line,
12265 and at most scroll_conservatively lines. */
12266 amount_to_scroll
12267 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12268 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12269 else if (scroll_step || temp_scroll_step)
12270 amount_to_scroll = scroll_max;
12271 else
12272 {
12273 aggressive = current_buffer->scroll_up_aggressively;
12274 height = WINDOW_BOX_TEXT_HEIGHT (w);
12275 if (NUMBERP (aggressive))
12276 {
12277 double float_amount = XFLOATINT (aggressive) * height;
12278 amount_to_scroll = float_amount;
12279 if (amount_to_scroll == 0 && float_amount > 0)
12280 amount_to_scroll = 1;
12281 }
12282 }
12283
12284 if (amount_to_scroll <= 0)
12285 return SCROLLING_FAILED;
12286
12287 /* If moving by amount_to_scroll leaves STARTP unchanged,
12288 move it down one screen line. */
12289
12290 move_it_vertically (&it, amount_to_scroll);
12291 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12292 move_it_by_lines (&it, 1, 1);
12293 startp = it.current.pos;
12294 }
12295 else
12296 {
12297 /* See if point is inside the scroll margin at the top of the
12298 window. */
12299 scroll_margin_pos = startp;
12300 if (this_scroll_margin)
12301 {
12302 start_display (&it, w, startp);
12303 move_it_vertically (&it, this_scroll_margin);
12304 scroll_margin_pos = it.current.pos;
12305 }
12306
12307 if (PT < CHARPOS (scroll_margin_pos))
12308 {
12309 /* Point is in the scroll margin at the top of the window or
12310 above what is displayed in the window. */
12311 int y0;
12312
12313 /* Compute the vertical distance from PT to the scroll
12314 margin position. Give up if distance is greater than
12315 scroll_max. */
12316 SET_TEXT_POS (pos, PT, PT_BYTE);
12317 start_display (&it, w, pos);
12318 y0 = it.current_y;
12319 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12320 it.last_visible_y, -1,
12321 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12322 dy = it.current_y - y0;
12323 if (dy > scroll_max)
12324 return SCROLLING_FAILED;
12325
12326 /* Compute new window start. */
12327 start_display (&it, w, startp);
12328
12329 if (scroll_conservatively)
12330 amount_to_scroll
12331 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12332 else if (scroll_step || temp_scroll_step)
12333 amount_to_scroll = scroll_max;
12334 else
12335 {
12336 aggressive = current_buffer->scroll_down_aggressively;
12337 height = WINDOW_BOX_TEXT_HEIGHT (w);
12338 if (NUMBERP (aggressive))
12339 {
12340 double float_amount = XFLOATINT (aggressive) * height;
12341 amount_to_scroll = float_amount;
12342 if (amount_to_scroll == 0 && float_amount > 0)
12343 amount_to_scroll = 1;
12344 }
12345 }
12346
12347 if (amount_to_scroll <= 0)
12348 return SCROLLING_FAILED;
12349
12350 move_it_vertically_backward (&it, amount_to_scroll);
12351 startp = it.current.pos;
12352 }
12353 }
12354
12355 /* Run window scroll functions. */
12356 startp = run_window_scroll_functions (window, startp);
12357
12358 /* Display the window. Give up if new fonts are loaded, or if point
12359 doesn't appear. */
12360 if (!try_window (window, startp, 0))
12361 rc = SCROLLING_NEED_LARGER_MATRICES;
12362 else if (w->cursor.vpos < 0)
12363 {
12364 clear_glyph_matrix (w->desired_matrix);
12365 rc = SCROLLING_FAILED;
12366 }
12367 else
12368 {
12369 /* Maybe forget recorded base line for line number display. */
12370 if (!just_this_one_p
12371 || current_buffer->clip_changed
12372 || BEG_UNCHANGED < CHARPOS (startp))
12373 w->base_line_number = Qnil;
12374
12375 /* If cursor ends up on a partially visible line,
12376 treat that as being off the bottom of the screen. */
12377 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12378 {
12379 clear_glyph_matrix (w->desired_matrix);
12380 ++extra_scroll_margin_lines;
12381 goto too_near_end;
12382 }
12383 rc = SCROLLING_SUCCESS;
12384 }
12385
12386 return rc;
12387 }
12388
12389
12390 /* Compute a suitable window start for window W if display of W starts
12391 on a continuation line. Value is non-zero if a new window start
12392 was computed.
12393
12394 The new window start will be computed, based on W's width, starting
12395 from the start of the continued line. It is the start of the
12396 screen line with the minimum distance from the old start W->start. */
12397
12398 static int
12399 compute_window_start_on_continuation_line (w)
12400 struct window *w;
12401 {
12402 struct text_pos pos, start_pos;
12403 int window_start_changed_p = 0;
12404
12405 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12406
12407 /* If window start is on a continuation line... Window start may be
12408 < BEGV in case there's invisible text at the start of the
12409 buffer (M-x rmail, for example). */
12410 if (CHARPOS (start_pos) > BEGV
12411 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12412 {
12413 struct it it;
12414 struct glyph_row *row;
12415
12416 /* Handle the case that the window start is out of range. */
12417 if (CHARPOS (start_pos) < BEGV)
12418 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12419 else if (CHARPOS (start_pos) > ZV)
12420 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12421
12422 /* Find the start of the continued line. This should be fast
12423 because scan_buffer is fast (newline cache). */
12424 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12425 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12426 row, DEFAULT_FACE_ID);
12427 reseat_at_previous_visible_line_start (&it);
12428
12429 /* If the line start is "too far" away from the window start,
12430 say it takes too much time to compute a new window start. */
12431 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12432 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12433 {
12434 int min_distance, distance;
12435
12436 /* Move forward by display lines to find the new window
12437 start. If window width was enlarged, the new start can
12438 be expected to be > the old start. If window width was
12439 decreased, the new window start will be < the old start.
12440 So, we're looking for the display line start with the
12441 minimum distance from the old window start. */
12442 pos = it.current.pos;
12443 min_distance = INFINITY;
12444 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12445 distance < min_distance)
12446 {
12447 min_distance = distance;
12448 pos = it.current.pos;
12449 move_it_by_lines (&it, 1, 0);
12450 }
12451
12452 /* Set the window start there. */
12453 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12454 window_start_changed_p = 1;
12455 }
12456 }
12457
12458 return window_start_changed_p;
12459 }
12460
12461
12462 /* Try cursor movement in case text has not changed in window WINDOW,
12463 with window start STARTP. Value is
12464
12465 CURSOR_MOVEMENT_SUCCESS if successful
12466
12467 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12468
12469 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12470 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12471 we want to scroll as if scroll-step were set to 1. See the code.
12472
12473 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12474 which case we have to abort this redisplay, and adjust matrices
12475 first. */
12476
12477 enum
12478 {
12479 CURSOR_MOVEMENT_SUCCESS,
12480 CURSOR_MOVEMENT_CANNOT_BE_USED,
12481 CURSOR_MOVEMENT_MUST_SCROLL,
12482 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12483 };
12484
12485 static int
12486 try_cursor_movement (window, startp, scroll_step)
12487 Lisp_Object window;
12488 struct text_pos startp;
12489 int *scroll_step;
12490 {
12491 struct window *w = XWINDOW (window);
12492 struct frame *f = XFRAME (w->frame);
12493 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12494
12495 #if GLYPH_DEBUG
12496 if (inhibit_try_cursor_movement)
12497 return rc;
12498 #endif
12499
12500 /* Handle case where text has not changed, only point, and it has
12501 not moved off the frame. */
12502 if (/* Point may be in this window. */
12503 PT >= CHARPOS (startp)
12504 /* Selective display hasn't changed. */
12505 && !current_buffer->clip_changed
12506 /* Function force-mode-line-update is used to force a thorough
12507 redisplay. It sets either windows_or_buffers_changed or
12508 update_mode_lines. So don't take a shortcut here for these
12509 cases. */
12510 && !update_mode_lines
12511 && !windows_or_buffers_changed
12512 && !cursor_type_changed
12513 /* Can't use this case if highlighting a region. When a
12514 region exists, cursor movement has to do more than just
12515 set the cursor. */
12516 && !(!NILP (Vtransient_mark_mode)
12517 && !NILP (current_buffer->mark_active))
12518 && NILP (w->region_showing)
12519 && NILP (Vshow_trailing_whitespace)
12520 /* Right after splitting windows, last_point may be nil. */
12521 && INTEGERP (w->last_point)
12522 /* This code is not used for mini-buffer for the sake of the case
12523 of redisplaying to replace an echo area message; since in
12524 that case the mini-buffer contents per se are usually
12525 unchanged. This code is of no real use in the mini-buffer
12526 since the handling of this_line_start_pos, etc., in redisplay
12527 handles the same cases. */
12528 && !EQ (window, minibuf_window)
12529 /* When splitting windows or for new windows, it happens that
12530 redisplay is called with a nil window_end_vpos or one being
12531 larger than the window. This should really be fixed in
12532 window.c. I don't have this on my list, now, so we do
12533 approximately the same as the old redisplay code. --gerd. */
12534 && INTEGERP (w->window_end_vpos)
12535 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12536 && (FRAME_WINDOW_P (f)
12537 || !overlay_arrow_in_current_buffer_p ()))
12538 {
12539 int this_scroll_margin, top_scroll_margin;
12540 struct glyph_row *row = NULL;
12541
12542 #if GLYPH_DEBUG
12543 debug_method_add (w, "cursor movement");
12544 #endif
12545
12546 /* Scroll if point within this distance from the top or bottom
12547 of the window. This is a pixel value. */
12548 this_scroll_margin = max (0, scroll_margin);
12549 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12550 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12551
12552 top_scroll_margin = this_scroll_margin;
12553 if (WINDOW_WANTS_HEADER_LINE_P (w))
12554 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12555
12556 /* Start with the row the cursor was displayed during the last
12557 not paused redisplay. Give up if that row is not valid. */
12558 if (w->last_cursor.vpos < 0
12559 || w->last_cursor.vpos >= w->current_matrix->nrows)
12560 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12561 else
12562 {
12563 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12564 if (row->mode_line_p)
12565 ++row;
12566 if (!row->enabled_p)
12567 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12568 }
12569
12570 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12571 {
12572 int scroll_p = 0;
12573 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12574
12575 if (PT > XFASTINT (w->last_point))
12576 {
12577 /* Point has moved forward. */
12578 while (MATRIX_ROW_END_CHARPOS (row) < PT
12579 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12580 {
12581 xassert (row->enabled_p);
12582 ++row;
12583 }
12584
12585 /* The end position of a row equals the start position
12586 of the next row. If PT is there, we would rather
12587 display it in the next line. */
12588 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12589 && MATRIX_ROW_END_CHARPOS (row) == PT
12590 && !cursor_row_p (w, row))
12591 ++row;
12592
12593 /* If within the scroll margin, scroll. Note that
12594 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12595 the next line would be drawn, and that
12596 this_scroll_margin can be zero. */
12597 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12598 || PT > MATRIX_ROW_END_CHARPOS (row)
12599 /* Line is completely visible last line in window
12600 and PT is to be set in the next line. */
12601 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12602 && PT == MATRIX_ROW_END_CHARPOS (row)
12603 && !row->ends_at_zv_p
12604 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12605 scroll_p = 1;
12606 }
12607 else if (PT < XFASTINT (w->last_point))
12608 {
12609 /* Cursor has to be moved backward. Note that PT >=
12610 CHARPOS (startp) because of the outer if-statement. */
12611 while (!row->mode_line_p
12612 && (MATRIX_ROW_START_CHARPOS (row) > PT
12613 || (MATRIX_ROW_START_CHARPOS (row) == PT
12614 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12615 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12616 row > w->current_matrix->rows
12617 && (row-1)->ends_in_newline_from_string_p))))
12618 && (row->y > top_scroll_margin
12619 || CHARPOS (startp) == BEGV))
12620 {
12621 xassert (row->enabled_p);
12622 --row;
12623 }
12624
12625 /* Consider the following case: Window starts at BEGV,
12626 there is invisible, intangible text at BEGV, so that
12627 display starts at some point START > BEGV. It can
12628 happen that we are called with PT somewhere between
12629 BEGV and START. Try to handle that case. */
12630 if (row < w->current_matrix->rows
12631 || row->mode_line_p)
12632 {
12633 row = w->current_matrix->rows;
12634 if (row->mode_line_p)
12635 ++row;
12636 }
12637
12638 /* Due to newlines in overlay strings, we may have to
12639 skip forward over overlay strings. */
12640 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12641 && MATRIX_ROW_END_CHARPOS (row) == PT
12642 && !cursor_row_p (w, row))
12643 ++row;
12644
12645 /* If within the scroll margin, scroll. */
12646 if (row->y < top_scroll_margin
12647 && CHARPOS (startp) != BEGV)
12648 scroll_p = 1;
12649 }
12650 else
12651 {
12652 /* Cursor did not move. So don't scroll even if cursor line
12653 is partially visible, as it was so before. */
12654 rc = CURSOR_MOVEMENT_SUCCESS;
12655 }
12656
12657 if (PT < MATRIX_ROW_START_CHARPOS (row)
12658 || PT > MATRIX_ROW_END_CHARPOS (row))
12659 {
12660 /* if PT is not in the glyph row, give up. */
12661 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12662 }
12663 else if (rc != CURSOR_MOVEMENT_SUCCESS
12664 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12665 && make_cursor_line_fully_visible_p)
12666 {
12667 if (PT == MATRIX_ROW_END_CHARPOS (row)
12668 && !row->ends_at_zv_p
12669 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12670 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12671 else if (row->height > window_box_height (w))
12672 {
12673 /* If we end up in a partially visible line, let's
12674 make it fully visible, except when it's taller
12675 than the window, in which case we can't do much
12676 about it. */
12677 *scroll_step = 1;
12678 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12679 }
12680 else
12681 {
12682 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12683 if (!cursor_row_fully_visible_p (w, 0, 1))
12684 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12685 else
12686 rc = CURSOR_MOVEMENT_SUCCESS;
12687 }
12688 }
12689 else if (scroll_p)
12690 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12691 else
12692 {
12693 do
12694 {
12695 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12696 {
12697 rc = CURSOR_MOVEMENT_SUCCESS;
12698 break;
12699 }
12700 ++row;
12701 }
12702 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12703 && MATRIX_ROW_START_CHARPOS (row) == PT
12704 && cursor_row_p (w, row));
12705 }
12706 }
12707 }
12708
12709 return rc;
12710 }
12711
12712 void
12713 set_vertical_scroll_bar (w)
12714 struct window *w;
12715 {
12716 int start, end, whole;
12717
12718 /* Calculate the start and end positions for the current window.
12719 At some point, it would be nice to choose between scrollbars
12720 which reflect the whole buffer size, with special markers
12721 indicating narrowing, and scrollbars which reflect only the
12722 visible region.
12723
12724 Note that mini-buffers sometimes aren't displaying any text. */
12725 if (!MINI_WINDOW_P (w)
12726 || (w == XWINDOW (minibuf_window)
12727 && NILP (echo_area_buffer[0])))
12728 {
12729 struct buffer *buf = XBUFFER (w->buffer);
12730 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12731 start = marker_position (w->start) - BUF_BEGV (buf);
12732 /* I don't think this is guaranteed to be right. For the
12733 moment, we'll pretend it is. */
12734 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12735
12736 if (end < start)
12737 end = start;
12738 if (whole < (end - start))
12739 whole = end - start;
12740 }
12741 else
12742 start = end = whole = 0;
12743
12744 /* Indicate what this scroll bar ought to be displaying now. */
12745 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12746 }
12747
12748
12749 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12750 selected_window is redisplayed.
12751
12752 We can return without actually redisplaying the window if
12753 fonts_changed_p is nonzero. In that case, redisplay_internal will
12754 retry. */
12755
12756 static void
12757 redisplay_window (window, just_this_one_p)
12758 Lisp_Object window;
12759 int just_this_one_p;
12760 {
12761 struct window *w = XWINDOW (window);
12762 struct frame *f = XFRAME (w->frame);
12763 struct buffer *buffer = XBUFFER (w->buffer);
12764 struct buffer *old = current_buffer;
12765 struct text_pos lpoint, opoint, startp;
12766 int update_mode_line;
12767 int tem;
12768 struct it it;
12769 /* Record it now because it's overwritten. */
12770 int current_matrix_up_to_date_p = 0;
12771 int used_current_matrix_p = 0;
12772 /* This is less strict than current_matrix_up_to_date_p.
12773 It indictes that the buffer contents and narrowing are unchanged. */
12774 int buffer_unchanged_p = 0;
12775 int temp_scroll_step = 0;
12776 int count = SPECPDL_INDEX ();
12777 int rc;
12778 int centering_position = -1;
12779 int last_line_misfit = 0;
12780 int beg_unchanged, end_unchanged;
12781
12782 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12783 opoint = lpoint;
12784
12785 /* W must be a leaf window here. */
12786 xassert (!NILP (w->buffer));
12787 #if GLYPH_DEBUG
12788 *w->desired_matrix->method = 0;
12789 #endif
12790
12791 specbind (Qinhibit_point_motion_hooks, Qt);
12792
12793 reconsider_clip_changes (w, buffer);
12794
12795 /* Has the mode line to be updated? */
12796 update_mode_line = (!NILP (w->update_mode_line)
12797 || update_mode_lines
12798 || buffer->clip_changed
12799 || buffer->prevent_redisplay_optimizations_p);
12800
12801 if (MINI_WINDOW_P (w))
12802 {
12803 if (w == XWINDOW (echo_area_window)
12804 && !NILP (echo_area_buffer[0]))
12805 {
12806 if (update_mode_line)
12807 /* We may have to update a tty frame's menu bar or a
12808 tool-bar. Example `M-x C-h C-h C-g'. */
12809 goto finish_menu_bars;
12810 else
12811 /* We've already displayed the echo area glyphs in this window. */
12812 goto finish_scroll_bars;
12813 }
12814 else if ((w != XWINDOW (minibuf_window)
12815 || minibuf_level == 0)
12816 /* When buffer is nonempty, redisplay window normally. */
12817 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12818 /* Quail displays non-mini buffers in minibuffer window.
12819 In that case, redisplay the window normally. */
12820 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12821 {
12822 /* W is a mini-buffer window, but it's not active, so clear
12823 it. */
12824 int yb = window_text_bottom_y (w);
12825 struct glyph_row *row;
12826 int y;
12827
12828 for (y = 0, row = w->desired_matrix->rows;
12829 y < yb;
12830 y += row->height, ++row)
12831 blank_row (w, row, y);
12832 goto finish_scroll_bars;
12833 }
12834
12835 clear_glyph_matrix (w->desired_matrix);
12836 }
12837
12838 /* Otherwise set up data on this window; select its buffer and point
12839 value. */
12840 /* Really select the buffer, for the sake of buffer-local
12841 variables. */
12842 set_buffer_internal_1 (XBUFFER (w->buffer));
12843 SET_TEXT_POS (opoint, PT, PT_BYTE);
12844
12845 beg_unchanged = BEG_UNCHANGED;
12846 end_unchanged = END_UNCHANGED;
12847
12848 current_matrix_up_to_date_p
12849 = (!NILP (w->window_end_valid)
12850 && !current_buffer->clip_changed
12851 && !current_buffer->prevent_redisplay_optimizations_p
12852 && XFASTINT (w->last_modified) >= MODIFF
12853 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12854
12855 buffer_unchanged_p
12856 = (!NILP (w->window_end_valid)
12857 && !current_buffer->clip_changed
12858 && XFASTINT (w->last_modified) >= MODIFF
12859 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12860
12861 /* When windows_or_buffers_changed is non-zero, we can't rely on
12862 the window end being valid, so set it to nil there. */
12863 if (windows_or_buffers_changed)
12864 {
12865 /* If window starts on a continuation line, maybe adjust the
12866 window start in case the window's width changed. */
12867 if (XMARKER (w->start)->buffer == current_buffer)
12868 compute_window_start_on_continuation_line (w);
12869
12870 w->window_end_valid = Qnil;
12871 }
12872
12873 /* Some sanity checks. */
12874 CHECK_WINDOW_END (w);
12875 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12876 abort ();
12877 if (BYTEPOS (opoint) < CHARPOS (opoint))
12878 abort ();
12879
12880 /* If %c is in mode line, update it if needed. */
12881 if (!NILP (w->column_number_displayed)
12882 /* This alternative quickly identifies a common case
12883 where no change is needed. */
12884 && !(PT == XFASTINT (w->last_point)
12885 && XFASTINT (w->last_modified) >= MODIFF
12886 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12887 && (XFASTINT (w->column_number_displayed)
12888 != (int) current_column ())) /* iftc */
12889 update_mode_line = 1;
12890
12891 /* Count number of windows showing the selected buffer. An indirect
12892 buffer counts as its base buffer. */
12893 if (!just_this_one_p)
12894 {
12895 struct buffer *current_base, *window_base;
12896 current_base = current_buffer;
12897 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12898 if (current_base->base_buffer)
12899 current_base = current_base->base_buffer;
12900 if (window_base->base_buffer)
12901 window_base = window_base->base_buffer;
12902 if (current_base == window_base)
12903 buffer_shared++;
12904 }
12905
12906 /* Point refers normally to the selected window. For any other
12907 window, set up appropriate value. */
12908 if (!EQ (window, selected_window))
12909 {
12910 int new_pt = XMARKER (w->pointm)->charpos;
12911 int new_pt_byte = marker_byte_position (w->pointm);
12912 if (new_pt < BEGV)
12913 {
12914 new_pt = BEGV;
12915 new_pt_byte = BEGV_BYTE;
12916 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12917 }
12918 else if (new_pt > (ZV - 1))
12919 {
12920 new_pt = ZV;
12921 new_pt_byte = ZV_BYTE;
12922 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12923 }
12924
12925 /* We don't use SET_PT so that the point-motion hooks don't run. */
12926 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12927 }
12928
12929 /* If any of the character widths specified in the display table
12930 have changed, invalidate the width run cache. It's true that
12931 this may be a bit late to catch such changes, but the rest of
12932 redisplay goes (non-fatally) haywire when the display table is
12933 changed, so why should we worry about doing any better? */
12934 if (current_buffer->width_run_cache)
12935 {
12936 struct Lisp_Char_Table *disptab = buffer_display_table ();
12937
12938 if (! disptab_matches_widthtab (disptab,
12939 XVECTOR (current_buffer->width_table)))
12940 {
12941 invalidate_region_cache (current_buffer,
12942 current_buffer->width_run_cache,
12943 BEG, Z);
12944 recompute_width_table (current_buffer, disptab);
12945 }
12946 }
12947
12948 /* If window-start is screwed up, choose a new one. */
12949 if (XMARKER (w->start)->buffer != current_buffer)
12950 goto recenter;
12951
12952 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12953
12954 /* If someone specified a new starting point but did not insist,
12955 check whether it can be used. */
12956 if (!NILP (w->optional_new_start)
12957 && CHARPOS (startp) >= BEGV
12958 && CHARPOS (startp) <= ZV)
12959 {
12960 w->optional_new_start = Qnil;
12961 start_display (&it, w, startp);
12962 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12963 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12964 if (IT_CHARPOS (it) == PT)
12965 w->force_start = Qt;
12966 /* IT may overshoot PT if text at PT is invisible. */
12967 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12968 w->force_start = Qt;
12969 }
12970
12971 force_start:
12972
12973 /* Handle case where place to start displaying has been specified,
12974 unless the specified location is outside the accessible range. */
12975 if (!NILP (w->force_start)
12976 || w->frozen_window_start_p)
12977 {
12978 /* We set this later on if we have to adjust point. */
12979 int new_vpos = -1;
12980 int val;
12981
12982 w->force_start = Qnil;
12983 w->vscroll = 0;
12984 w->window_end_valid = Qnil;
12985
12986 /* Forget any recorded base line for line number display. */
12987 if (!buffer_unchanged_p)
12988 w->base_line_number = Qnil;
12989
12990 /* Redisplay the mode line. Select the buffer properly for that.
12991 Also, run the hook window-scroll-functions
12992 because we have scrolled. */
12993 /* Note, we do this after clearing force_start because
12994 if there's an error, it is better to forget about force_start
12995 than to get into an infinite loop calling the hook functions
12996 and having them get more errors. */
12997 if (!update_mode_line
12998 || ! NILP (Vwindow_scroll_functions))
12999 {
13000 update_mode_line = 1;
13001 w->update_mode_line = Qt;
13002 startp = run_window_scroll_functions (window, startp);
13003 }
13004
13005 w->last_modified = make_number (0);
13006 w->last_overlay_modified = make_number (0);
13007 if (CHARPOS (startp) < BEGV)
13008 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13009 else if (CHARPOS (startp) > ZV)
13010 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13011
13012 /* Redisplay, then check if cursor has been set during the
13013 redisplay. Give up if new fonts were loaded. */
13014 val = try_window (window, startp, 1);
13015 if (!val)
13016 {
13017 w->force_start = Qt;
13018 clear_glyph_matrix (w->desired_matrix);
13019 goto need_larger_matrices;
13020 }
13021 /* Point was outside the scroll margins. */
13022 if (val < 0)
13023 new_vpos = window_box_height (w) / 2;
13024
13025 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13026 {
13027 /* If point does not appear, try to move point so it does
13028 appear. The desired matrix has been built above, so we
13029 can use it here. */
13030 new_vpos = window_box_height (w) / 2;
13031 }
13032
13033 if (!cursor_row_fully_visible_p (w, 0, 0))
13034 {
13035 /* Point does appear, but on a line partly visible at end of window.
13036 Move it back to a fully-visible line. */
13037 new_vpos = window_box_height (w);
13038 }
13039
13040 /* If we need to move point for either of the above reasons,
13041 now actually do it. */
13042 if (new_vpos >= 0)
13043 {
13044 struct glyph_row *row;
13045
13046 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13047 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13048 ++row;
13049
13050 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13051 MATRIX_ROW_START_BYTEPOS (row));
13052
13053 if (w != XWINDOW (selected_window))
13054 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13055 else if (current_buffer == old)
13056 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13057
13058 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13059
13060 /* If we are highlighting the region, then we just changed
13061 the region, so redisplay to show it. */
13062 if (!NILP (Vtransient_mark_mode)
13063 && !NILP (current_buffer->mark_active))
13064 {
13065 clear_glyph_matrix (w->desired_matrix);
13066 if (!try_window (window, startp, 0))
13067 goto need_larger_matrices;
13068 }
13069 }
13070
13071 #if GLYPH_DEBUG
13072 debug_method_add (w, "forced window start");
13073 #endif
13074 goto done;
13075 }
13076
13077 /* Handle case where text has not changed, only point, and it has
13078 not moved off the frame, and we are not retrying after hscroll.
13079 (current_matrix_up_to_date_p is nonzero when retrying.) */
13080 if (current_matrix_up_to_date_p
13081 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13082 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13083 {
13084 switch (rc)
13085 {
13086 case CURSOR_MOVEMENT_SUCCESS:
13087 used_current_matrix_p = 1;
13088 goto done;
13089
13090 #if 0 /* try_cursor_movement never returns this value. */
13091 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13092 goto need_larger_matrices;
13093 #endif
13094
13095 case CURSOR_MOVEMENT_MUST_SCROLL:
13096 goto try_to_scroll;
13097
13098 default:
13099 abort ();
13100 }
13101 }
13102 /* If current starting point was originally the beginning of a line
13103 but no longer is, find a new starting point. */
13104 else if (!NILP (w->start_at_line_beg)
13105 && !(CHARPOS (startp) <= BEGV
13106 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13107 {
13108 #if GLYPH_DEBUG
13109 debug_method_add (w, "recenter 1");
13110 #endif
13111 goto recenter;
13112 }
13113
13114 /* Try scrolling with try_window_id. Value is > 0 if update has
13115 been done, it is -1 if we know that the same window start will
13116 not work. It is 0 if unsuccessful for some other reason. */
13117 else if ((tem = try_window_id (w)) != 0)
13118 {
13119 #if GLYPH_DEBUG
13120 debug_method_add (w, "try_window_id %d", tem);
13121 #endif
13122
13123 if (fonts_changed_p)
13124 goto need_larger_matrices;
13125 if (tem > 0)
13126 goto done;
13127
13128 /* Otherwise try_window_id has returned -1 which means that we
13129 don't want the alternative below this comment to execute. */
13130 }
13131 else if (CHARPOS (startp) >= BEGV
13132 && CHARPOS (startp) <= ZV
13133 && PT >= CHARPOS (startp)
13134 && (CHARPOS (startp) < ZV
13135 /* Avoid starting at end of buffer. */
13136 || CHARPOS (startp) == BEGV
13137 || (XFASTINT (w->last_modified) >= MODIFF
13138 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13139 {
13140
13141 /* If first window line is a continuation line, and window start
13142 is inside the modified region, but the first change is before
13143 current window start, we must select a new window start.
13144
13145 However, if this is the result of a down-mouse event (e.g. by
13146 extending the mouse-drag-overlay), we don't want to select a
13147 new window start, since that would change the position under
13148 the mouse, resulting in an unwanted mouse-movement rather
13149 than a simple mouse-click. */
13150 if (NILP (w->start_at_line_beg)
13151 && NILP (do_mouse_tracking)
13152 && CHARPOS (startp) > BEGV
13153 && CHARPOS (startp) > BEG + beg_unchanged
13154 && CHARPOS (startp) <= Z - end_unchanged)
13155 {
13156 w->force_start = Qt;
13157 if (XMARKER (w->start)->buffer == current_buffer)
13158 compute_window_start_on_continuation_line (w);
13159 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13160 goto force_start;
13161 }
13162
13163 #if GLYPH_DEBUG
13164 debug_method_add (w, "same window start");
13165 #endif
13166
13167 /* Try to redisplay starting at same place as before.
13168 If point has not moved off frame, accept the results. */
13169 if (!current_matrix_up_to_date_p
13170 /* Don't use try_window_reusing_current_matrix in this case
13171 because a window scroll function can have changed the
13172 buffer. */
13173 || !NILP (Vwindow_scroll_functions)
13174 || MINI_WINDOW_P (w)
13175 || !(used_current_matrix_p
13176 = try_window_reusing_current_matrix (w)))
13177 {
13178 IF_DEBUG (debug_method_add (w, "1"));
13179 if (try_window (window, startp, 1) < 0)
13180 /* -1 means we need to scroll.
13181 0 means we need new matrices, but fonts_changed_p
13182 is set in that case, so we will detect it below. */
13183 goto try_to_scroll;
13184 }
13185
13186 if (fonts_changed_p)
13187 goto need_larger_matrices;
13188
13189 if (w->cursor.vpos >= 0)
13190 {
13191 if (!just_this_one_p
13192 || current_buffer->clip_changed
13193 || BEG_UNCHANGED < CHARPOS (startp))
13194 /* Forget any recorded base line for line number display. */
13195 w->base_line_number = Qnil;
13196
13197 if (!cursor_row_fully_visible_p (w, 1, 0))
13198 {
13199 clear_glyph_matrix (w->desired_matrix);
13200 last_line_misfit = 1;
13201 }
13202 /* Drop through and scroll. */
13203 else
13204 goto done;
13205 }
13206 else
13207 clear_glyph_matrix (w->desired_matrix);
13208 }
13209
13210 try_to_scroll:
13211
13212 w->last_modified = make_number (0);
13213 w->last_overlay_modified = make_number (0);
13214
13215 /* Redisplay the mode line. Select the buffer properly for that. */
13216 if (!update_mode_line)
13217 {
13218 update_mode_line = 1;
13219 w->update_mode_line = Qt;
13220 }
13221
13222 /* Try to scroll by specified few lines. */
13223 if ((scroll_conservatively
13224 || scroll_step
13225 || temp_scroll_step
13226 || NUMBERP (current_buffer->scroll_up_aggressively)
13227 || NUMBERP (current_buffer->scroll_down_aggressively))
13228 && !current_buffer->clip_changed
13229 && CHARPOS (startp) >= BEGV
13230 && CHARPOS (startp) <= ZV)
13231 {
13232 /* The function returns -1 if new fonts were loaded, 1 if
13233 successful, 0 if not successful. */
13234 int rc = try_scrolling (window, just_this_one_p,
13235 scroll_conservatively,
13236 scroll_step,
13237 temp_scroll_step, last_line_misfit);
13238 switch (rc)
13239 {
13240 case SCROLLING_SUCCESS:
13241 goto done;
13242
13243 case SCROLLING_NEED_LARGER_MATRICES:
13244 goto need_larger_matrices;
13245
13246 case SCROLLING_FAILED:
13247 break;
13248
13249 default:
13250 abort ();
13251 }
13252 }
13253
13254 /* Finally, just choose place to start which centers point */
13255
13256 recenter:
13257 if (centering_position < 0)
13258 centering_position = window_box_height (w) / 2;
13259
13260 #if GLYPH_DEBUG
13261 debug_method_add (w, "recenter");
13262 #endif
13263
13264 /* w->vscroll = 0; */
13265
13266 /* Forget any previously recorded base line for line number display. */
13267 if (!buffer_unchanged_p)
13268 w->base_line_number = Qnil;
13269
13270 /* Move backward half the height of the window. */
13271 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13272 it.current_y = it.last_visible_y;
13273 move_it_vertically_backward (&it, centering_position);
13274 xassert (IT_CHARPOS (it) >= BEGV);
13275
13276 /* The function move_it_vertically_backward may move over more
13277 than the specified y-distance. If it->w is small, e.g. a
13278 mini-buffer window, we may end up in front of the window's
13279 display area. Start displaying at the start of the line
13280 containing PT in this case. */
13281 if (it.current_y <= 0)
13282 {
13283 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13284 move_it_vertically_backward (&it, 0);
13285 #if 0
13286 /* I think this assert is bogus if buffer contains
13287 invisible text or images. KFS. */
13288 xassert (IT_CHARPOS (it) <= PT);
13289 #endif
13290 it.current_y = 0;
13291 }
13292
13293 it.current_x = it.hpos = 0;
13294
13295 /* Set startp here explicitly in case that helps avoid an infinite loop
13296 in case the window-scroll-functions functions get errors. */
13297 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13298
13299 /* Run scroll hooks. */
13300 startp = run_window_scroll_functions (window, it.current.pos);
13301
13302 /* Redisplay the window. */
13303 if (!current_matrix_up_to_date_p
13304 || windows_or_buffers_changed
13305 || cursor_type_changed
13306 /* Don't use try_window_reusing_current_matrix in this case
13307 because it can have changed the buffer. */
13308 || !NILP (Vwindow_scroll_functions)
13309 || !just_this_one_p
13310 || MINI_WINDOW_P (w)
13311 || !(used_current_matrix_p
13312 = try_window_reusing_current_matrix (w)))
13313 try_window (window, startp, 0);
13314
13315 /* If new fonts have been loaded (due to fontsets), give up. We
13316 have to start a new redisplay since we need to re-adjust glyph
13317 matrices. */
13318 if (fonts_changed_p)
13319 goto need_larger_matrices;
13320
13321 /* If cursor did not appear assume that the middle of the window is
13322 in the first line of the window. Do it again with the next line.
13323 (Imagine a window of height 100, displaying two lines of height
13324 60. Moving back 50 from it->last_visible_y will end in the first
13325 line.) */
13326 if (w->cursor.vpos < 0)
13327 {
13328 if (!NILP (w->window_end_valid)
13329 && PT >= Z - XFASTINT (w->window_end_pos))
13330 {
13331 clear_glyph_matrix (w->desired_matrix);
13332 move_it_by_lines (&it, 1, 0);
13333 try_window (window, it.current.pos, 0);
13334 }
13335 else if (PT < IT_CHARPOS (it))
13336 {
13337 clear_glyph_matrix (w->desired_matrix);
13338 move_it_by_lines (&it, -1, 0);
13339 try_window (window, it.current.pos, 0);
13340 }
13341 else
13342 {
13343 /* Not much we can do about it. */
13344 }
13345 }
13346
13347 /* Consider the following case: Window starts at BEGV, there is
13348 invisible, intangible text at BEGV, so that display starts at
13349 some point START > BEGV. It can happen that we are called with
13350 PT somewhere between BEGV and START. Try to handle that case. */
13351 if (w->cursor.vpos < 0)
13352 {
13353 struct glyph_row *row = w->current_matrix->rows;
13354 if (row->mode_line_p)
13355 ++row;
13356 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13357 }
13358
13359 if (!cursor_row_fully_visible_p (w, 0, 0))
13360 {
13361 /* If vscroll is enabled, disable it and try again. */
13362 if (w->vscroll)
13363 {
13364 w->vscroll = 0;
13365 clear_glyph_matrix (w->desired_matrix);
13366 goto recenter;
13367 }
13368
13369 /* If centering point failed to make the whole line visible,
13370 put point at the top instead. That has to make the whole line
13371 visible, if it can be done. */
13372 if (centering_position == 0)
13373 goto done;
13374
13375 clear_glyph_matrix (w->desired_matrix);
13376 centering_position = 0;
13377 goto recenter;
13378 }
13379
13380 done:
13381
13382 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13383 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13384 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13385 ? Qt : Qnil);
13386
13387 /* Display the mode line, if we must. */
13388 if ((update_mode_line
13389 /* If window not full width, must redo its mode line
13390 if (a) the window to its side is being redone and
13391 (b) we do a frame-based redisplay. This is a consequence
13392 of how inverted lines are drawn in frame-based redisplay. */
13393 || (!just_this_one_p
13394 && !FRAME_WINDOW_P (f)
13395 && !WINDOW_FULL_WIDTH_P (w))
13396 /* Line number to display. */
13397 || INTEGERP (w->base_line_pos)
13398 /* Column number is displayed and different from the one displayed. */
13399 || (!NILP (w->column_number_displayed)
13400 && (XFASTINT (w->column_number_displayed)
13401 != (int) current_column ()))) /* iftc */
13402 /* This means that the window has a mode line. */
13403 && (WINDOW_WANTS_MODELINE_P (w)
13404 || WINDOW_WANTS_HEADER_LINE_P (w)))
13405 {
13406 display_mode_lines (w);
13407
13408 /* If mode line height has changed, arrange for a thorough
13409 immediate redisplay using the correct mode line height. */
13410 if (WINDOW_WANTS_MODELINE_P (w)
13411 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13412 {
13413 fonts_changed_p = 1;
13414 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13415 = DESIRED_MODE_LINE_HEIGHT (w);
13416 }
13417
13418 /* If top line height has changed, arrange for a thorough
13419 immediate redisplay using the correct mode line height. */
13420 if (WINDOW_WANTS_HEADER_LINE_P (w)
13421 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13422 {
13423 fonts_changed_p = 1;
13424 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13425 = DESIRED_HEADER_LINE_HEIGHT (w);
13426 }
13427
13428 if (fonts_changed_p)
13429 goto need_larger_matrices;
13430 }
13431
13432 if (!line_number_displayed
13433 && !BUFFERP (w->base_line_pos))
13434 {
13435 w->base_line_pos = Qnil;
13436 w->base_line_number = Qnil;
13437 }
13438
13439 finish_menu_bars:
13440
13441 /* When we reach a frame's selected window, redo the frame's menu bar. */
13442 if (update_mode_line
13443 && EQ (FRAME_SELECTED_WINDOW (f), window))
13444 {
13445 int redisplay_menu_p = 0;
13446 int redisplay_tool_bar_p = 0;
13447
13448 if (FRAME_WINDOW_P (f))
13449 {
13450 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13451 || defined (USE_GTK)
13452 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13453 #else
13454 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13455 #endif
13456 }
13457 else
13458 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13459
13460 if (redisplay_menu_p)
13461 display_menu_bar (w);
13462
13463 #ifdef HAVE_WINDOW_SYSTEM
13464 #ifdef USE_GTK
13465 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13466 #else
13467 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13468 && (FRAME_TOOL_BAR_LINES (f) > 0
13469 || !NILP (Vauto_resize_tool_bars));
13470
13471 #endif
13472
13473 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13474 {
13475 extern int ignore_mouse_drag_p;
13476 ignore_mouse_drag_p = 1;
13477 }
13478 #endif
13479 }
13480
13481 #ifdef HAVE_WINDOW_SYSTEM
13482 if (FRAME_WINDOW_P (f)
13483 && update_window_fringes (w, (just_this_one_p
13484 || (!used_current_matrix_p && !overlay_arrow_seen)
13485 || w->pseudo_window_p)))
13486 {
13487 update_begin (f);
13488 BLOCK_INPUT;
13489 if (draw_window_fringes (w, 1))
13490 x_draw_vertical_border (w);
13491 UNBLOCK_INPUT;
13492 update_end (f);
13493 }
13494 #endif /* HAVE_WINDOW_SYSTEM */
13495
13496 /* We go to this label, with fonts_changed_p nonzero,
13497 if it is necessary to try again using larger glyph matrices.
13498 We have to redeem the scroll bar even in this case,
13499 because the loop in redisplay_internal expects that. */
13500 need_larger_matrices:
13501 ;
13502 finish_scroll_bars:
13503
13504 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13505 {
13506 /* Set the thumb's position and size. */
13507 set_vertical_scroll_bar (w);
13508
13509 /* Note that we actually used the scroll bar attached to this
13510 window, so it shouldn't be deleted at the end of redisplay. */
13511 redeem_scroll_bar_hook (w);
13512 }
13513
13514 /* Restore current_buffer and value of point in it. */
13515 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13516 set_buffer_internal_1 (old);
13517 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13518
13519 unbind_to (count, Qnil);
13520 }
13521
13522
13523 /* Build the complete desired matrix of WINDOW with a window start
13524 buffer position POS.
13525
13526 Value is 1 if successful. It is zero if fonts were loaded during
13527 redisplay which makes re-adjusting glyph matrices necessary, and -1
13528 if point would appear in the scroll margins.
13529 (We check that only if CHECK_MARGINS is nonzero. */
13530
13531 int
13532 try_window (window, pos, check_margins)
13533 Lisp_Object window;
13534 struct text_pos pos;
13535 int check_margins;
13536 {
13537 struct window *w = XWINDOW (window);
13538 struct it it;
13539 struct glyph_row *last_text_row = NULL;
13540 struct frame *f = XFRAME (w->frame);
13541
13542 /* Make POS the new window start. */
13543 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13544
13545 /* Mark cursor position as unknown. No overlay arrow seen. */
13546 w->cursor.vpos = -1;
13547 overlay_arrow_seen = 0;
13548
13549 /* Initialize iterator and info to start at POS. */
13550 start_display (&it, w, pos);
13551
13552 /* Display all lines of W. */
13553 while (it.current_y < it.last_visible_y)
13554 {
13555 if (display_line (&it))
13556 last_text_row = it.glyph_row - 1;
13557 if (fonts_changed_p)
13558 return 0;
13559 }
13560
13561 /* Don't let the cursor end in the scroll margins. */
13562 if (check_margins
13563 && !MINI_WINDOW_P (w))
13564 {
13565 int this_scroll_margin;
13566
13567 this_scroll_margin = max (0, scroll_margin);
13568 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13569 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13570
13571 if ((w->cursor.y >= 0 /* not vscrolled */
13572 && w->cursor.y < this_scroll_margin
13573 && CHARPOS (pos) > BEGV
13574 && IT_CHARPOS (it) < ZV)
13575 /* rms: considering make_cursor_line_fully_visible_p here
13576 seems to give wrong results. We don't want to recenter
13577 when the last line is partly visible, we want to allow
13578 that case to be handled in the usual way. */
13579 || (w->cursor.y + 1) > it.last_visible_y)
13580 {
13581 w->cursor.vpos = -1;
13582 clear_glyph_matrix (w->desired_matrix);
13583 return -1;
13584 }
13585 }
13586
13587 /* If bottom moved off end of frame, change mode line percentage. */
13588 if (XFASTINT (w->window_end_pos) <= 0
13589 && Z != IT_CHARPOS (it))
13590 w->update_mode_line = Qt;
13591
13592 /* Set window_end_pos to the offset of the last character displayed
13593 on the window from the end of current_buffer. Set
13594 window_end_vpos to its row number. */
13595 if (last_text_row)
13596 {
13597 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13598 w->window_end_bytepos
13599 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13600 w->window_end_pos
13601 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13602 w->window_end_vpos
13603 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13604 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13605 ->displays_text_p);
13606 }
13607 else
13608 {
13609 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13610 w->window_end_pos = make_number (Z - ZV);
13611 w->window_end_vpos = make_number (0);
13612 }
13613
13614 /* But that is not valid info until redisplay finishes. */
13615 w->window_end_valid = Qnil;
13616 return 1;
13617 }
13618
13619
13620 \f
13621 /************************************************************************
13622 Window redisplay reusing current matrix when buffer has not changed
13623 ************************************************************************/
13624
13625 /* Try redisplay of window W showing an unchanged buffer with a
13626 different window start than the last time it was displayed by
13627 reusing its current matrix. Value is non-zero if successful.
13628 W->start is the new window start. */
13629
13630 static int
13631 try_window_reusing_current_matrix (w)
13632 struct window *w;
13633 {
13634 struct frame *f = XFRAME (w->frame);
13635 struct glyph_row *row, *bottom_row;
13636 struct it it;
13637 struct run run;
13638 struct text_pos start, new_start;
13639 int nrows_scrolled, i;
13640 struct glyph_row *last_text_row;
13641 struct glyph_row *last_reused_text_row;
13642 struct glyph_row *start_row;
13643 int start_vpos, min_y, max_y;
13644
13645 #if GLYPH_DEBUG
13646 if (inhibit_try_window_reusing)
13647 return 0;
13648 #endif
13649
13650 if (/* This function doesn't handle terminal frames. */
13651 !FRAME_WINDOW_P (f)
13652 /* Don't try to reuse the display if windows have been split
13653 or such. */
13654 || windows_or_buffers_changed
13655 || cursor_type_changed)
13656 return 0;
13657
13658 /* Can't do this if region may have changed. */
13659 if ((!NILP (Vtransient_mark_mode)
13660 && !NILP (current_buffer->mark_active))
13661 || !NILP (w->region_showing)
13662 || !NILP (Vshow_trailing_whitespace))
13663 return 0;
13664
13665 /* If top-line visibility has changed, give up. */
13666 if (WINDOW_WANTS_HEADER_LINE_P (w)
13667 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13668 return 0;
13669
13670 /* Give up if old or new display is scrolled vertically. We could
13671 make this function handle this, but right now it doesn't. */
13672 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13673 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13674 return 0;
13675
13676 /* The variable new_start now holds the new window start. The old
13677 start `start' can be determined from the current matrix. */
13678 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13679 start = start_row->start.pos;
13680 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13681
13682 /* Clear the desired matrix for the display below. */
13683 clear_glyph_matrix (w->desired_matrix);
13684
13685 if (CHARPOS (new_start) <= CHARPOS (start))
13686 {
13687 int first_row_y;
13688
13689 /* Don't use this method if the display starts with an ellipsis
13690 displayed for invisible text. It's not easy to handle that case
13691 below, and it's certainly not worth the effort since this is
13692 not a frequent case. */
13693 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13694 return 0;
13695
13696 IF_DEBUG (debug_method_add (w, "twu1"));
13697
13698 /* Display up to a row that can be reused. The variable
13699 last_text_row is set to the last row displayed that displays
13700 text. Note that it.vpos == 0 if or if not there is a
13701 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13702 start_display (&it, w, new_start);
13703 first_row_y = it.current_y;
13704 w->cursor.vpos = -1;
13705 last_text_row = last_reused_text_row = NULL;
13706
13707 while (it.current_y < it.last_visible_y
13708 && !fonts_changed_p)
13709 {
13710 /* If we have reached into the characters in the START row,
13711 that means the line boundaries have changed. So we
13712 can't start copying with the row START. Maybe it will
13713 work to start copying with the following row. */
13714 while (IT_CHARPOS (it) > CHARPOS (start))
13715 {
13716 /* Advance to the next row as the "start". */
13717 start_row++;
13718 start = start_row->start.pos;
13719 /* If there are no more rows to try, or just one, give up. */
13720 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13721 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13722 || CHARPOS (start) == ZV)
13723 {
13724 clear_glyph_matrix (w->desired_matrix);
13725 return 0;
13726 }
13727
13728 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13729 }
13730 /* If we have reached alignment,
13731 we can copy the rest of the rows. */
13732 if (IT_CHARPOS (it) == CHARPOS (start))
13733 break;
13734
13735 if (display_line (&it))
13736 last_text_row = it.glyph_row - 1;
13737 }
13738
13739 /* A value of current_y < last_visible_y means that we stopped
13740 at the previous window start, which in turn means that we
13741 have at least one reusable row. */
13742 if (it.current_y < it.last_visible_y)
13743 {
13744 /* IT.vpos always starts from 0; it counts text lines. */
13745 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13746
13747 /* Find PT if not already found in the lines displayed. */
13748 if (w->cursor.vpos < 0)
13749 {
13750 int dy = it.current_y - start_row->y;
13751
13752 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13753 row = row_containing_pos (w, PT, row, NULL, dy);
13754 if (row)
13755 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13756 dy, nrows_scrolled);
13757 else
13758 {
13759 clear_glyph_matrix (w->desired_matrix);
13760 return 0;
13761 }
13762 }
13763
13764 /* Scroll the display. Do it before the current matrix is
13765 changed. The problem here is that update has not yet
13766 run, i.e. part of the current matrix is not up to date.
13767 scroll_run_hook will clear the cursor, and use the
13768 current matrix to get the height of the row the cursor is
13769 in. */
13770 run.current_y = start_row->y;
13771 run.desired_y = it.current_y;
13772 run.height = it.last_visible_y - it.current_y;
13773
13774 if (run.height > 0 && run.current_y != run.desired_y)
13775 {
13776 update_begin (f);
13777 rif->update_window_begin_hook (w);
13778 rif->clear_window_mouse_face (w);
13779 rif->scroll_run_hook (w, &run);
13780 rif->update_window_end_hook (w, 0, 0);
13781 update_end (f);
13782 }
13783
13784 /* Shift current matrix down by nrows_scrolled lines. */
13785 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13786 rotate_matrix (w->current_matrix,
13787 start_vpos,
13788 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13789 nrows_scrolled);
13790
13791 /* Disable lines that must be updated. */
13792 for (i = 0; i < nrows_scrolled; ++i)
13793 (start_row + i)->enabled_p = 0;
13794
13795 /* Re-compute Y positions. */
13796 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13797 max_y = it.last_visible_y;
13798 for (row = start_row + nrows_scrolled;
13799 row < bottom_row;
13800 ++row)
13801 {
13802 row->y = it.current_y;
13803 row->visible_height = row->height;
13804
13805 if (row->y < min_y)
13806 row->visible_height -= min_y - row->y;
13807 if (row->y + row->height > max_y)
13808 row->visible_height -= row->y + row->height - max_y;
13809 row->redraw_fringe_bitmaps_p = 1;
13810
13811 it.current_y += row->height;
13812
13813 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13814 last_reused_text_row = row;
13815 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13816 break;
13817 }
13818
13819 /* Disable lines in the current matrix which are now
13820 below the window. */
13821 for (++row; row < bottom_row; ++row)
13822 row->enabled_p = row->mode_line_p = 0;
13823 }
13824
13825 /* Update window_end_pos etc.; last_reused_text_row is the last
13826 reused row from the current matrix containing text, if any.
13827 The value of last_text_row is the last displayed line
13828 containing text. */
13829 if (last_reused_text_row)
13830 {
13831 w->window_end_bytepos
13832 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13833 w->window_end_pos
13834 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13835 w->window_end_vpos
13836 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13837 w->current_matrix));
13838 }
13839 else if (last_text_row)
13840 {
13841 w->window_end_bytepos
13842 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13843 w->window_end_pos
13844 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13845 w->window_end_vpos
13846 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13847 }
13848 else
13849 {
13850 /* This window must be completely empty. */
13851 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13852 w->window_end_pos = make_number (Z - ZV);
13853 w->window_end_vpos = make_number (0);
13854 }
13855 w->window_end_valid = Qnil;
13856
13857 /* Update hint: don't try scrolling again in update_window. */
13858 w->desired_matrix->no_scrolling_p = 1;
13859
13860 #if GLYPH_DEBUG
13861 debug_method_add (w, "try_window_reusing_current_matrix 1");
13862 #endif
13863 return 1;
13864 }
13865 else if (CHARPOS (new_start) > CHARPOS (start))
13866 {
13867 struct glyph_row *pt_row, *row;
13868 struct glyph_row *first_reusable_row;
13869 struct glyph_row *first_row_to_display;
13870 int dy;
13871 int yb = window_text_bottom_y (w);
13872
13873 /* Find the row starting at new_start, if there is one. Don't
13874 reuse a partially visible line at the end. */
13875 first_reusable_row = start_row;
13876 while (first_reusable_row->enabled_p
13877 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13878 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13879 < CHARPOS (new_start)))
13880 ++first_reusable_row;
13881
13882 /* Give up if there is no row to reuse. */
13883 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13884 || !first_reusable_row->enabled_p
13885 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13886 != CHARPOS (new_start)))
13887 return 0;
13888
13889 /* We can reuse fully visible rows beginning with
13890 first_reusable_row to the end of the window. Set
13891 first_row_to_display to the first row that cannot be reused.
13892 Set pt_row to the row containing point, if there is any. */
13893 pt_row = NULL;
13894 for (first_row_to_display = first_reusable_row;
13895 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13896 ++first_row_to_display)
13897 {
13898 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13899 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13900 pt_row = first_row_to_display;
13901 }
13902
13903 /* Start displaying at the start of first_row_to_display. */
13904 xassert (first_row_to_display->y < yb);
13905 init_to_row_start (&it, w, first_row_to_display);
13906
13907 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13908 - start_vpos);
13909 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13910 - nrows_scrolled);
13911 it.current_y = (first_row_to_display->y - first_reusable_row->y
13912 + WINDOW_HEADER_LINE_HEIGHT (w));
13913
13914 /* Display lines beginning with first_row_to_display in the
13915 desired matrix. Set last_text_row to the last row displayed
13916 that displays text. */
13917 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13918 if (pt_row == NULL)
13919 w->cursor.vpos = -1;
13920 last_text_row = NULL;
13921 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13922 if (display_line (&it))
13923 last_text_row = it.glyph_row - 1;
13924
13925 /* Give up If point isn't in a row displayed or reused. */
13926 if (w->cursor.vpos < 0)
13927 {
13928 clear_glyph_matrix (w->desired_matrix);
13929 return 0;
13930 }
13931
13932 /* If point is in a reused row, adjust y and vpos of the cursor
13933 position. */
13934 if (pt_row)
13935 {
13936 w->cursor.vpos -= nrows_scrolled;
13937 w->cursor.y -= first_reusable_row->y - start_row->y;
13938 }
13939
13940 /* Scroll the display. */
13941 run.current_y = first_reusable_row->y;
13942 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13943 run.height = it.last_visible_y - run.current_y;
13944 dy = run.current_y - run.desired_y;
13945
13946 if (run.height)
13947 {
13948 update_begin (f);
13949 rif->update_window_begin_hook (w);
13950 rif->clear_window_mouse_face (w);
13951 rif->scroll_run_hook (w, &run);
13952 rif->update_window_end_hook (w, 0, 0);
13953 update_end (f);
13954 }
13955
13956 /* Adjust Y positions of reused rows. */
13957 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13958 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13959 max_y = it.last_visible_y;
13960 for (row = first_reusable_row; row < first_row_to_display; ++row)
13961 {
13962 row->y -= dy;
13963 row->visible_height = row->height;
13964 if (row->y < min_y)
13965 row->visible_height -= min_y - row->y;
13966 if (row->y + row->height > max_y)
13967 row->visible_height -= row->y + row->height - max_y;
13968 row->redraw_fringe_bitmaps_p = 1;
13969 }
13970
13971 /* Scroll the current matrix. */
13972 xassert (nrows_scrolled > 0);
13973 rotate_matrix (w->current_matrix,
13974 start_vpos,
13975 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13976 -nrows_scrolled);
13977
13978 /* Disable rows not reused. */
13979 for (row -= nrows_scrolled; row < bottom_row; ++row)
13980 row->enabled_p = 0;
13981
13982 /* Point may have moved to a different line, so we cannot assume that
13983 the previous cursor position is valid; locate the correct row. */
13984 if (pt_row)
13985 {
13986 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13987 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13988 row++)
13989 {
13990 w->cursor.vpos++;
13991 w->cursor.y = row->y;
13992 }
13993 if (row < bottom_row)
13994 {
13995 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13996 while (glyph->charpos < PT)
13997 {
13998 w->cursor.hpos++;
13999 w->cursor.x += glyph->pixel_width;
14000 glyph++;
14001 }
14002 }
14003 }
14004
14005 /* Adjust window end. A null value of last_text_row means that
14006 the window end is in reused rows which in turn means that
14007 only its vpos can have changed. */
14008 if (last_text_row)
14009 {
14010 w->window_end_bytepos
14011 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14012 w->window_end_pos
14013 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14014 w->window_end_vpos
14015 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14016 }
14017 else
14018 {
14019 w->window_end_vpos
14020 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14021 }
14022
14023 w->window_end_valid = Qnil;
14024 w->desired_matrix->no_scrolling_p = 1;
14025
14026 #if GLYPH_DEBUG
14027 debug_method_add (w, "try_window_reusing_current_matrix 2");
14028 #endif
14029 return 1;
14030 }
14031
14032 return 0;
14033 }
14034
14035
14036 \f
14037 /************************************************************************
14038 Window redisplay reusing current matrix when buffer has changed
14039 ************************************************************************/
14040
14041 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14042 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14043 int *, int *));
14044 static struct glyph_row *
14045 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14046 struct glyph_row *));
14047
14048
14049 /* Return the last row in MATRIX displaying text. If row START is
14050 non-null, start searching with that row. IT gives the dimensions
14051 of the display. Value is null if matrix is empty; otherwise it is
14052 a pointer to the row found. */
14053
14054 static struct glyph_row *
14055 find_last_row_displaying_text (matrix, it, start)
14056 struct glyph_matrix *matrix;
14057 struct it *it;
14058 struct glyph_row *start;
14059 {
14060 struct glyph_row *row, *row_found;
14061
14062 /* Set row_found to the last row in IT->w's current matrix
14063 displaying text. The loop looks funny but think of partially
14064 visible lines. */
14065 row_found = NULL;
14066 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14067 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14068 {
14069 xassert (row->enabled_p);
14070 row_found = row;
14071 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14072 break;
14073 ++row;
14074 }
14075
14076 return row_found;
14077 }
14078
14079
14080 /* Return the last row in the current matrix of W that is not affected
14081 by changes at the start of current_buffer that occurred since W's
14082 current matrix was built. Value is null if no such row exists.
14083
14084 BEG_UNCHANGED us the number of characters unchanged at the start of
14085 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14086 first changed character in current_buffer. Characters at positions <
14087 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14088 when the current matrix was built. */
14089
14090 static struct glyph_row *
14091 find_last_unchanged_at_beg_row (w)
14092 struct window *w;
14093 {
14094 int first_changed_pos = BEG + BEG_UNCHANGED;
14095 struct glyph_row *row;
14096 struct glyph_row *row_found = NULL;
14097 int yb = window_text_bottom_y (w);
14098
14099 /* Find the last row displaying unchanged text. */
14100 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14101 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14102 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14103 {
14104 if (/* If row ends before first_changed_pos, it is unchanged,
14105 except in some case. */
14106 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14107 /* When row ends in ZV and we write at ZV it is not
14108 unchanged. */
14109 && !row->ends_at_zv_p
14110 /* When first_changed_pos is the end of a continued line,
14111 row is not unchanged because it may be no longer
14112 continued. */
14113 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14114 && (row->continued_p
14115 || row->exact_window_width_line_p)))
14116 row_found = row;
14117
14118 /* Stop if last visible row. */
14119 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14120 break;
14121
14122 ++row;
14123 }
14124
14125 return row_found;
14126 }
14127
14128
14129 /* Find the first glyph row in the current matrix of W that is not
14130 affected by changes at the end of current_buffer since the
14131 time W's current matrix was built.
14132
14133 Return in *DELTA the number of chars by which buffer positions in
14134 unchanged text at the end of current_buffer must be adjusted.
14135
14136 Return in *DELTA_BYTES the corresponding number of bytes.
14137
14138 Value is null if no such row exists, i.e. all rows are affected by
14139 changes. */
14140
14141 static struct glyph_row *
14142 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14143 struct window *w;
14144 int *delta, *delta_bytes;
14145 {
14146 struct glyph_row *row;
14147 struct glyph_row *row_found = NULL;
14148
14149 *delta = *delta_bytes = 0;
14150
14151 /* Display must not have been paused, otherwise the current matrix
14152 is not up to date. */
14153 if (NILP (w->window_end_valid))
14154 abort ();
14155
14156 /* A value of window_end_pos >= END_UNCHANGED means that the window
14157 end is in the range of changed text. If so, there is no
14158 unchanged row at the end of W's current matrix. */
14159 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14160 return NULL;
14161
14162 /* Set row to the last row in W's current matrix displaying text. */
14163 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14164
14165 /* If matrix is entirely empty, no unchanged row exists. */
14166 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14167 {
14168 /* The value of row is the last glyph row in the matrix having a
14169 meaningful buffer position in it. The end position of row
14170 corresponds to window_end_pos. This allows us to translate
14171 buffer positions in the current matrix to current buffer
14172 positions for characters not in changed text. */
14173 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14174 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14175 int last_unchanged_pos, last_unchanged_pos_old;
14176 struct glyph_row *first_text_row
14177 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14178
14179 *delta = Z - Z_old;
14180 *delta_bytes = Z_BYTE - Z_BYTE_old;
14181
14182 /* Set last_unchanged_pos to the buffer position of the last
14183 character in the buffer that has not been changed. Z is the
14184 index + 1 of the last character in current_buffer, i.e. by
14185 subtracting END_UNCHANGED we get the index of the last
14186 unchanged character, and we have to add BEG to get its buffer
14187 position. */
14188 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14189 last_unchanged_pos_old = last_unchanged_pos - *delta;
14190
14191 /* Search backward from ROW for a row displaying a line that
14192 starts at a minimum position >= last_unchanged_pos_old. */
14193 for (; row > first_text_row; --row)
14194 {
14195 /* This used to abort, but it can happen.
14196 It is ok to just stop the search instead here. KFS. */
14197 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14198 break;
14199
14200 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14201 row_found = row;
14202 }
14203 }
14204
14205 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14206 abort ();
14207
14208 return row_found;
14209 }
14210
14211
14212 /* Make sure that glyph rows in the current matrix of window W
14213 reference the same glyph memory as corresponding rows in the
14214 frame's frame matrix. This function is called after scrolling W's
14215 current matrix on a terminal frame in try_window_id and
14216 try_window_reusing_current_matrix. */
14217
14218 static void
14219 sync_frame_with_window_matrix_rows (w)
14220 struct window *w;
14221 {
14222 struct frame *f = XFRAME (w->frame);
14223 struct glyph_row *window_row, *window_row_end, *frame_row;
14224
14225 /* Preconditions: W must be a leaf window and full-width. Its frame
14226 must have a frame matrix. */
14227 xassert (NILP (w->hchild) && NILP (w->vchild));
14228 xassert (WINDOW_FULL_WIDTH_P (w));
14229 xassert (!FRAME_WINDOW_P (f));
14230
14231 /* If W is a full-width window, glyph pointers in W's current matrix
14232 have, by definition, to be the same as glyph pointers in the
14233 corresponding frame matrix. Note that frame matrices have no
14234 marginal areas (see build_frame_matrix). */
14235 window_row = w->current_matrix->rows;
14236 window_row_end = window_row + w->current_matrix->nrows;
14237 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14238 while (window_row < window_row_end)
14239 {
14240 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14241 struct glyph *end = window_row->glyphs[LAST_AREA];
14242
14243 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14244 frame_row->glyphs[TEXT_AREA] = start;
14245 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14246 frame_row->glyphs[LAST_AREA] = end;
14247
14248 /* Disable frame rows whose corresponding window rows have
14249 been disabled in try_window_id. */
14250 if (!window_row->enabled_p)
14251 frame_row->enabled_p = 0;
14252
14253 ++window_row, ++frame_row;
14254 }
14255 }
14256
14257
14258 /* Find the glyph row in window W containing CHARPOS. Consider all
14259 rows between START and END (not inclusive). END null means search
14260 all rows to the end of the display area of W. Value is the row
14261 containing CHARPOS or null. */
14262
14263 struct glyph_row *
14264 row_containing_pos (w, charpos, start, end, dy)
14265 struct window *w;
14266 int charpos;
14267 struct glyph_row *start, *end;
14268 int dy;
14269 {
14270 struct glyph_row *row = start;
14271 int last_y;
14272
14273 /* If we happen to start on a header-line, skip that. */
14274 if (row->mode_line_p)
14275 ++row;
14276
14277 if ((end && row >= end) || !row->enabled_p)
14278 return NULL;
14279
14280 last_y = window_text_bottom_y (w) - dy;
14281
14282 while (1)
14283 {
14284 /* Give up if we have gone too far. */
14285 if (end && row >= end)
14286 return NULL;
14287 /* This formerly returned if they were equal.
14288 I think that both quantities are of a "last plus one" type;
14289 if so, when they are equal, the row is within the screen. -- rms. */
14290 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14291 return NULL;
14292
14293 /* If it is in this row, return this row. */
14294 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14295 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14296 /* The end position of a row equals the start
14297 position of the next row. If CHARPOS is there, we
14298 would rather display it in the next line, except
14299 when this line ends in ZV. */
14300 && !row->ends_at_zv_p
14301 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14302 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14303 return row;
14304 ++row;
14305 }
14306 }
14307
14308
14309 /* Try to redisplay window W by reusing its existing display. W's
14310 current matrix must be up to date when this function is called,
14311 i.e. window_end_valid must not be nil.
14312
14313 Value is
14314
14315 1 if display has been updated
14316 0 if otherwise unsuccessful
14317 -1 if redisplay with same window start is known not to succeed
14318
14319 The following steps are performed:
14320
14321 1. Find the last row in the current matrix of W that is not
14322 affected by changes at the start of current_buffer. If no such row
14323 is found, give up.
14324
14325 2. Find the first row in W's current matrix that is not affected by
14326 changes at the end of current_buffer. Maybe there is no such row.
14327
14328 3. Display lines beginning with the row + 1 found in step 1 to the
14329 row found in step 2 or, if step 2 didn't find a row, to the end of
14330 the window.
14331
14332 4. If cursor is not known to appear on the window, give up.
14333
14334 5. If display stopped at the row found in step 2, scroll the
14335 display and current matrix as needed.
14336
14337 6. Maybe display some lines at the end of W, if we must. This can
14338 happen under various circumstances, like a partially visible line
14339 becoming fully visible, or because newly displayed lines are displayed
14340 in smaller font sizes.
14341
14342 7. Update W's window end information. */
14343
14344 static int
14345 try_window_id (w)
14346 struct window *w;
14347 {
14348 struct frame *f = XFRAME (w->frame);
14349 struct glyph_matrix *current_matrix = w->current_matrix;
14350 struct glyph_matrix *desired_matrix = w->desired_matrix;
14351 struct glyph_row *last_unchanged_at_beg_row;
14352 struct glyph_row *first_unchanged_at_end_row;
14353 struct glyph_row *row;
14354 struct glyph_row *bottom_row;
14355 int bottom_vpos;
14356 struct it it;
14357 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14358 struct text_pos start_pos;
14359 struct run run;
14360 int first_unchanged_at_end_vpos = 0;
14361 struct glyph_row *last_text_row, *last_text_row_at_end;
14362 struct text_pos start;
14363 int first_changed_charpos, last_changed_charpos;
14364
14365 #if GLYPH_DEBUG
14366 if (inhibit_try_window_id)
14367 return 0;
14368 #endif
14369
14370 /* This is handy for debugging. */
14371 #if 0
14372 #define GIVE_UP(X) \
14373 do { \
14374 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14375 return 0; \
14376 } while (0)
14377 #else
14378 #define GIVE_UP(X) return 0
14379 #endif
14380
14381 SET_TEXT_POS_FROM_MARKER (start, w->start);
14382
14383 /* Don't use this for mini-windows because these can show
14384 messages and mini-buffers, and we don't handle that here. */
14385 if (MINI_WINDOW_P (w))
14386 GIVE_UP (1);
14387
14388 /* This flag is used to prevent redisplay optimizations. */
14389 if (windows_or_buffers_changed || cursor_type_changed)
14390 GIVE_UP (2);
14391
14392 /* Verify that narrowing has not changed.
14393 Also verify that we were not told to prevent redisplay optimizations.
14394 It would be nice to further
14395 reduce the number of cases where this prevents try_window_id. */
14396 if (current_buffer->clip_changed
14397 || current_buffer->prevent_redisplay_optimizations_p)
14398 GIVE_UP (3);
14399
14400 /* Window must either use window-based redisplay or be full width. */
14401 if (!FRAME_WINDOW_P (f)
14402 && (!line_ins_del_ok
14403 || !WINDOW_FULL_WIDTH_P (w)))
14404 GIVE_UP (4);
14405
14406 /* Give up if point is not known NOT to appear in W. */
14407 if (PT < CHARPOS (start))
14408 GIVE_UP (5);
14409
14410 /* Another way to prevent redisplay optimizations. */
14411 if (XFASTINT (w->last_modified) == 0)
14412 GIVE_UP (6);
14413
14414 /* Verify that window is not hscrolled. */
14415 if (XFASTINT (w->hscroll) != 0)
14416 GIVE_UP (7);
14417
14418 /* Verify that display wasn't paused. */
14419 if (NILP (w->window_end_valid))
14420 GIVE_UP (8);
14421
14422 /* Can't use this if highlighting a region because a cursor movement
14423 will do more than just set the cursor. */
14424 if (!NILP (Vtransient_mark_mode)
14425 && !NILP (current_buffer->mark_active))
14426 GIVE_UP (9);
14427
14428 /* Likewise if highlighting trailing whitespace. */
14429 if (!NILP (Vshow_trailing_whitespace))
14430 GIVE_UP (11);
14431
14432 /* Likewise if showing a region. */
14433 if (!NILP (w->region_showing))
14434 GIVE_UP (10);
14435
14436 /* Can use this if overlay arrow position and or string have changed. */
14437 if (overlay_arrows_changed_p ())
14438 GIVE_UP (12);
14439
14440
14441 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14442 only if buffer has really changed. The reason is that the gap is
14443 initially at Z for freshly visited files. The code below would
14444 set end_unchanged to 0 in that case. */
14445 if (MODIFF > SAVE_MODIFF
14446 /* This seems to happen sometimes after saving a buffer. */
14447 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14448 {
14449 if (GPT - BEG < BEG_UNCHANGED)
14450 BEG_UNCHANGED = GPT - BEG;
14451 if (Z - GPT < END_UNCHANGED)
14452 END_UNCHANGED = Z - GPT;
14453 }
14454
14455 /* The position of the first and last character that has been changed. */
14456 first_changed_charpos = BEG + BEG_UNCHANGED;
14457 last_changed_charpos = Z - END_UNCHANGED;
14458
14459 /* If window starts after a line end, and the last change is in
14460 front of that newline, then changes don't affect the display.
14461 This case happens with stealth-fontification. Note that although
14462 the display is unchanged, glyph positions in the matrix have to
14463 be adjusted, of course. */
14464 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14465 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14466 && ((last_changed_charpos < CHARPOS (start)
14467 && CHARPOS (start) == BEGV)
14468 || (last_changed_charpos < CHARPOS (start) - 1
14469 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14470 {
14471 int Z_old, delta, Z_BYTE_old, delta_bytes;
14472 struct glyph_row *r0;
14473
14474 /* Compute how many chars/bytes have been added to or removed
14475 from the buffer. */
14476 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14477 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14478 delta = Z - Z_old;
14479 delta_bytes = Z_BYTE - Z_BYTE_old;
14480
14481 /* Give up if PT is not in the window. Note that it already has
14482 been checked at the start of try_window_id that PT is not in
14483 front of the window start. */
14484 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14485 GIVE_UP (13);
14486
14487 /* If window start is unchanged, we can reuse the whole matrix
14488 as is, after adjusting glyph positions. No need to compute
14489 the window end again, since its offset from Z hasn't changed. */
14490 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14491 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14492 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14493 /* PT must not be in a partially visible line. */
14494 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14495 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14496 {
14497 /* Adjust positions in the glyph matrix. */
14498 if (delta || delta_bytes)
14499 {
14500 struct glyph_row *r1
14501 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14502 increment_matrix_positions (w->current_matrix,
14503 MATRIX_ROW_VPOS (r0, current_matrix),
14504 MATRIX_ROW_VPOS (r1, current_matrix),
14505 delta, delta_bytes);
14506 }
14507
14508 /* Set the cursor. */
14509 row = row_containing_pos (w, PT, r0, NULL, 0);
14510 if (row)
14511 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14512 else
14513 abort ();
14514 return 1;
14515 }
14516 }
14517
14518 /* Handle the case that changes are all below what is displayed in
14519 the window, and that PT is in the window. This shortcut cannot
14520 be taken if ZV is visible in the window, and text has been added
14521 there that is visible in the window. */
14522 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14523 /* ZV is not visible in the window, or there are no
14524 changes at ZV, actually. */
14525 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14526 || first_changed_charpos == last_changed_charpos))
14527 {
14528 struct glyph_row *r0;
14529
14530 /* Give up if PT is not in the window. Note that it already has
14531 been checked at the start of try_window_id that PT is not in
14532 front of the window start. */
14533 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14534 GIVE_UP (14);
14535
14536 /* If window start is unchanged, we can reuse the whole matrix
14537 as is, without changing glyph positions since no text has
14538 been added/removed in front of the window end. */
14539 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14540 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14541 /* PT must not be in a partially visible line. */
14542 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14543 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14544 {
14545 /* We have to compute the window end anew since text
14546 can have been added/removed after it. */
14547 w->window_end_pos
14548 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14549 w->window_end_bytepos
14550 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14551
14552 /* Set the cursor. */
14553 row = row_containing_pos (w, PT, r0, NULL, 0);
14554 if (row)
14555 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14556 else
14557 abort ();
14558 return 2;
14559 }
14560 }
14561
14562 /* Give up if window start is in the changed area.
14563
14564 The condition used to read
14565
14566 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14567
14568 but why that was tested escapes me at the moment. */
14569 if (CHARPOS (start) >= first_changed_charpos
14570 && CHARPOS (start) <= last_changed_charpos)
14571 GIVE_UP (15);
14572
14573 /* Check that window start agrees with the start of the first glyph
14574 row in its current matrix. Check this after we know the window
14575 start is not in changed text, otherwise positions would not be
14576 comparable. */
14577 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14578 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14579 GIVE_UP (16);
14580
14581 /* Give up if the window ends in strings. Overlay strings
14582 at the end are difficult to handle, so don't try. */
14583 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14584 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14585 GIVE_UP (20);
14586
14587 /* Compute the position at which we have to start displaying new
14588 lines. Some of the lines at the top of the window might be
14589 reusable because they are not displaying changed text. Find the
14590 last row in W's current matrix not affected by changes at the
14591 start of current_buffer. Value is null if changes start in the
14592 first line of window. */
14593 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14594 if (last_unchanged_at_beg_row)
14595 {
14596 /* Avoid starting to display in the moddle of a character, a TAB
14597 for instance. This is easier than to set up the iterator
14598 exactly, and it's not a frequent case, so the additional
14599 effort wouldn't really pay off. */
14600 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14601 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14602 && last_unchanged_at_beg_row > w->current_matrix->rows)
14603 --last_unchanged_at_beg_row;
14604
14605 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14606 GIVE_UP (17);
14607
14608 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14609 GIVE_UP (18);
14610 start_pos = it.current.pos;
14611
14612 /* Start displaying new lines in the desired matrix at the same
14613 vpos we would use in the current matrix, i.e. below
14614 last_unchanged_at_beg_row. */
14615 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14616 current_matrix);
14617 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14618 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14619
14620 xassert (it.hpos == 0 && it.current_x == 0);
14621 }
14622 else
14623 {
14624 /* There are no reusable lines at the start of the window.
14625 Start displaying in the first text line. */
14626 start_display (&it, w, start);
14627 it.vpos = it.first_vpos;
14628 start_pos = it.current.pos;
14629 }
14630
14631 /* Find the first row that is not affected by changes at the end of
14632 the buffer. Value will be null if there is no unchanged row, in
14633 which case we must redisplay to the end of the window. delta
14634 will be set to the value by which buffer positions beginning with
14635 first_unchanged_at_end_row have to be adjusted due to text
14636 changes. */
14637 first_unchanged_at_end_row
14638 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14639 IF_DEBUG (debug_delta = delta);
14640 IF_DEBUG (debug_delta_bytes = delta_bytes);
14641
14642 /* Set stop_pos to the buffer position up to which we will have to
14643 display new lines. If first_unchanged_at_end_row != NULL, this
14644 is the buffer position of the start of the line displayed in that
14645 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14646 that we don't stop at a buffer position. */
14647 stop_pos = 0;
14648 if (first_unchanged_at_end_row)
14649 {
14650 xassert (last_unchanged_at_beg_row == NULL
14651 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14652
14653 /* If this is a continuation line, move forward to the next one
14654 that isn't. Changes in lines above affect this line.
14655 Caution: this may move first_unchanged_at_end_row to a row
14656 not displaying text. */
14657 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14658 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14659 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14660 < it.last_visible_y))
14661 ++first_unchanged_at_end_row;
14662
14663 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14664 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14665 >= it.last_visible_y))
14666 first_unchanged_at_end_row = NULL;
14667 else
14668 {
14669 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14670 + delta);
14671 first_unchanged_at_end_vpos
14672 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14673 xassert (stop_pos >= Z - END_UNCHANGED);
14674 }
14675 }
14676 else if (last_unchanged_at_beg_row == NULL)
14677 GIVE_UP (19);
14678
14679
14680 #if GLYPH_DEBUG
14681
14682 /* Either there is no unchanged row at the end, or the one we have
14683 now displays text. This is a necessary condition for the window
14684 end pos calculation at the end of this function. */
14685 xassert (first_unchanged_at_end_row == NULL
14686 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14687
14688 debug_last_unchanged_at_beg_vpos
14689 = (last_unchanged_at_beg_row
14690 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14691 : -1);
14692 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14693
14694 #endif /* GLYPH_DEBUG != 0 */
14695
14696
14697 /* Display new lines. Set last_text_row to the last new line
14698 displayed which has text on it, i.e. might end up as being the
14699 line where the window_end_vpos is. */
14700 w->cursor.vpos = -1;
14701 last_text_row = NULL;
14702 overlay_arrow_seen = 0;
14703 while (it.current_y < it.last_visible_y
14704 && !fonts_changed_p
14705 && (first_unchanged_at_end_row == NULL
14706 || IT_CHARPOS (it) < stop_pos))
14707 {
14708 if (display_line (&it))
14709 last_text_row = it.glyph_row - 1;
14710 }
14711
14712 if (fonts_changed_p)
14713 return -1;
14714
14715
14716 /* Compute differences in buffer positions, y-positions etc. for
14717 lines reused at the bottom of the window. Compute what we can
14718 scroll. */
14719 if (first_unchanged_at_end_row
14720 /* No lines reused because we displayed everything up to the
14721 bottom of the window. */
14722 && it.current_y < it.last_visible_y)
14723 {
14724 dvpos = (it.vpos
14725 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14726 current_matrix));
14727 dy = it.current_y - first_unchanged_at_end_row->y;
14728 run.current_y = first_unchanged_at_end_row->y;
14729 run.desired_y = run.current_y + dy;
14730 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14731 }
14732 else
14733 {
14734 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14735 first_unchanged_at_end_row = NULL;
14736 }
14737 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14738
14739
14740 /* Find the cursor if not already found. We have to decide whether
14741 PT will appear on this window (it sometimes doesn't, but this is
14742 not a very frequent case.) This decision has to be made before
14743 the current matrix is altered. A value of cursor.vpos < 0 means
14744 that PT is either in one of the lines beginning at
14745 first_unchanged_at_end_row or below the window. Don't care for
14746 lines that might be displayed later at the window end; as
14747 mentioned, this is not a frequent case. */
14748 if (w->cursor.vpos < 0)
14749 {
14750 /* Cursor in unchanged rows at the top? */
14751 if (PT < CHARPOS (start_pos)
14752 && last_unchanged_at_beg_row)
14753 {
14754 row = row_containing_pos (w, PT,
14755 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14756 last_unchanged_at_beg_row + 1, 0);
14757 if (row)
14758 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14759 }
14760
14761 /* Start from first_unchanged_at_end_row looking for PT. */
14762 else if (first_unchanged_at_end_row)
14763 {
14764 row = row_containing_pos (w, PT - delta,
14765 first_unchanged_at_end_row, NULL, 0);
14766 if (row)
14767 set_cursor_from_row (w, row, w->current_matrix, delta,
14768 delta_bytes, dy, dvpos);
14769 }
14770
14771 /* Give up if cursor was not found. */
14772 if (w->cursor.vpos < 0)
14773 {
14774 clear_glyph_matrix (w->desired_matrix);
14775 return -1;
14776 }
14777 }
14778
14779 /* Don't let the cursor end in the scroll margins. */
14780 {
14781 int this_scroll_margin, cursor_height;
14782
14783 this_scroll_margin = max (0, scroll_margin);
14784 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14785 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14786 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14787
14788 if ((w->cursor.y < this_scroll_margin
14789 && CHARPOS (start) > BEGV)
14790 /* Old redisplay didn't take scroll margin into account at the bottom,
14791 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14792 || (w->cursor.y + (make_cursor_line_fully_visible_p
14793 ? cursor_height + this_scroll_margin
14794 : 1)) > it.last_visible_y)
14795 {
14796 w->cursor.vpos = -1;
14797 clear_glyph_matrix (w->desired_matrix);
14798 return -1;
14799 }
14800 }
14801
14802 /* Scroll the display. Do it before changing the current matrix so
14803 that xterm.c doesn't get confused about where the cursor glyph is
14804 found. */
14805 if (dy && run.height)
14806 {
14807 update_begin (f);
14808
14809 if (FRAME_WINDOW_P (f))
14810 {
14811 rif->update_window_begin_hook (w);
14812 rif->clear_window_mouse_face (w);
14813 rif->scroll_run_hook (w, &run);
14814 rif->update_window_end_hook (w, 0, 0);
14815 }
14816 else
14817 {
14818 /* Terminal frame. In this case, dvpos gives the number of
14819 lines to scroll by; dvpos < 0 means scroll up. */
14820 int first_unchanged_at_end_vpos
14821 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14822 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14823 int end = (WINDOW_TOP_EDGE_LINE (w)
14824 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14825 + window_internal_height (w));
14826
14827 /* Perform the operation on the screen. */
14828 if (dvpos > 0)
14829 {
14830 /* Scroll last_unchanged_at_beg_row to the end of the
14831 window down dvpos lines. */
14832 set_terminal_window (end);
14833
14834 /* On dumb terminals delete dvpos lines at the end
14835 before inserting dvpos empty lines. */
14836 if (!scroll_region_ok)
14837 ins_del_lines (end - dvpos, -dvpos);
14838
14839 /* Insert dvpos empty lines in front of
14840 last_unchanged_at_beg_row. */
14841 ins_del_lines (from, dvpos);
14842 }
14843 else if (dvpos < 0)
14844 {
14845 /* Scroll up last_unchanged_at_beg_vpos to the end of
14846 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14847 set_terminal_window (end);
14848
14849 /* Delete dvpos lines in front of
14850 last_unchanged_at_beg_vpos. ins_del_lines will set
14851 the cursor to the given vpos and emit |dvpos| delete
14852 line sequences. */
14853 ins_del_lines (from + dvpos, dvpos);
14854
14855 /* On a dumb terminal insert dvpos empty lines at the
14856 end. */
14857 if (!scroll_region_ok)
14858 ins_del_lines (end + dvpos, -dvpos);
14859 }
14860
14861 set_terminal_window (0);
14862 }
14863
14864 update_end (f);
14865 }
14866
14867 /* Shift reused rows of the current matrix to the right position.
14868 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14869 text. */
14870 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14871 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14872 if (dvpos < 0)
14873 {
14874 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14875 bottom_vpos, dvpos);
14876 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14877 bottom_vpos, 0);
14878 }
14879 else if (dvpos > 0)
14880 {
14881 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14882 bottom_vpos, dvpos);
14883 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14884 first_unchanged_at_end_vpos + dvpos, 0);
14885 }
14886
14887 /* For frame-based redisplay, make sure that current frame and window
14888 matrix are in sync with respect to glyph memory. */
14889 if (!FRAME_WINDOW_P (f))
14890 sync_frame_with_window_matrix_rows (w);
14891
14892 /* Adjust buffer positions in reused rows. */
14893 if (delta || delta_bytes)
14894 increment_matrix_positions (current_matrix,
14895 first_unchanged_at_end_vpos + dvpos,
14896 bottom_vpos, delta, delta_bytes);
14897
14898 /* Adjust Y positions. */
14899 if (dy)
14900 shift_glyph_matrix (w, current_matrix,
14901 first_unchanged_at_end_vpos + dvpos,
14902 bottom_vpos, dy);
14903
14904 if (first_unchanged_at_end_row)
14905 {
14906 first_unchanged_at_end_row += dvpos;
14907 if (first_unchanged_at_end_row->y >= it.last_visible_y
14908 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14909 first_unchanged_at_end_row = NULL;
14910 }
14911
14912 /* If scrolling up, there may be some lines to display at the end of
14913 the window. */
14914 last_text_row_at_end = NULL;
14915 if (dy < 0)
14916 {
14917 /* Scrolling up can leave for example a partially visible line
14918 at the end of the window to be redisplayed. */
14919 /* Set last_row to the glyph row in the current matrix where the
14920 window end line is found. It has been moved up or down in
14921 the matrix by dvpos. */
14922 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14923 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14924
14925 /* If last_row is the window end line, it should display text. */
14926 xassert (last_row->displays_text_p);
14927
14928 /* If window end line was partially visible before, begin
14929 displaying at that line. Otherwise begin displaying with the
14930 line following it. */
14931 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14932 {
14933 init_to_row_start (&it, w, last_row);
14934 it.vpos = last_vpos;
14935 it.current_y = last_row->y;
14936 }
14937 else
14938 {
14939 init_to_row_end (&it, w, last_row);
14940 it.vpos = 1 + last_vpos;
14941 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14942 ++last_row;
14943 }
14944
14945 /* We may start in a continuation line. If so, we have to
14946 get the right continuation_lines_width and current_x. */
14947 it.continuation_lines_width = last_row->continuation_lines_width;
14948 it.hpos = it.current_x = 0;
14949
14950 /* Display the rest of the lines at the window end. */
14951 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14952 while (it.current_y < it.last_visible_y
14953 && !fonts_changed_p)
14954 {
14955 /* Is it always sure that the display agrees with lines in
14956 the current matrix? I don't think so, so we mark rows
14957 displayed invalid in the current matrix by setting their
14958 enabled_p flag to zero. */
14959 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14960 if (display_line (&it))
14961 last_text_row_at_end = it.glyph_row - 1;
14962 }
14963 }
14964
14965 /* Update window_end_pos and window_end_vpos. */
14966 if (first_unchanged_at_end_row
14967 && !last_text_row_at_end)
14968 {
14969 /* Window end line if one of the preserved rows from the current
14970 matrix. Set row to the last row displaying text in current
14971 matrix starting at first_unchanged_at_end_row, after
14972 scrolling. */
14973 xassert (first_unchanged_at_end_row->displays_text_p);
14974 row = find_last_row_displaying_text (w->current_matrix, &it,
14975 first_unchanged_at_end_row);
14976 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14977
14978 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14979 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14980 w->window_end_vpos
14981 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14982 xassert (w->window_end_bytepos >= 0);
14983 IF_DEBUG (debug_method_add (w, "A"));
14984 }
14985 else if (last_text_row_at_end)
14986 {
14987 w->window_end_pos
14988 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14989 w->window_end_bytepos
14990 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14991 w->window_end_vpos
14992 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14993 xassert (w->window_end_bytepos >= 0);
14994 IF_DEBUG (debug_method_add (w, "B"));
14995 }
14996 else if (last_text_row)
14997 {
14998 /* We have displayed either to the end of the window or at the
14999 end of the window, i.e. the last row with text is to be found
15000 in the desired matrix. */
15001 w->window_end_pos
15002 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15003 w->window_end_bytepos
15004 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15005 w->window_end_vpos
15006 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15007 xassert (w->window_end_bytepos >= 0);
15008 }
15009 else if (first_unchanged_at_end_row == NULL
15010 && last_text_row == NULL
15011 && last_text_row_at_end == NULL)
15012 {
15013 /* Displayed to end of window, but no line containing text was
15014 displayed. Lines were deleted at the end of the window. */
15015 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15016 int vpos = XFASTINT (w->window_end_vpos);
15017 struct glyph_row *current_row = current_matrix->rows + vpos;
15018 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15019
15020 for (row = NULL;
15021 row == NULL && vpos >= first_vpos;
15022 --vpos, --current_row, --desired_row)
15023 {
15024 if (desired_row->enabled_p)
15025 {
15026 if (desired_row->displays_text_p)
15027 row = desired_row;
15028 }
15029 else if (current_row->displays_text_p)
15030 row = current_row;
15031 }
15032
15033 xassert (row != NULL);
15034 w->window_end_vpos = make_number (vpos + 1);
15035 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15036 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15037 xassert (w->window_end_bytepos >= 0);
15038 IF_DEBUG (debug_method_add (w, "C"));
15039 }
15040 else
15041 abort ();
15042
15043 #if 0 /* This leads to problems, for instance when the cursor is
15044 at ZV, and the cursor line displays no text. */
15045 /* Disable rows below what's displayed in the window. This makes
15046 debugging easier. */
15047 enable_glyph_matrix_rows (current_matrix,
15048 XFASTINT (w->window_end_vpos) + 1,
15049 bottom_vpos, 0);
15050 #endif
15051
15052 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15053 debug_end_vpos = XFASTINT (w->window_end_vpos));
15054
15055 /* Record that display has not been completed. */
15056 w->window_end_valid = Qnil;
15057 w->desired_matrix->no_scrolling_p = 1;
15058 return 3;
15059
15060 #undef GIVE_UP
15061 }
15062
15063
15064 \f
15065 /***********************************************************************
15066 More debugging support
15067 ***********************************************************************/
15068
15069 #if GLYPH_DEBUG
15070
15071 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15072 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15073 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15074
15075
15076 /* Dump the contents of glyph matrix MATRIX on stderr.
15077
15078 GLYPHS 0 means don't show glyph contents.
15079 GLYPHS 1 means show glyphs in short form
15080 GLYPHS > 1 means show glyphs in long form. */
15081
15082 void
15083 dump_glyph_matrix (matrix, glyphs)
15084 struct glyph_matrix *matrix;
15085 int glyphs;
15086 {
15087 int i;
15088 for (i = 0; i < matrix->nrows; ++i)
15089 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15090 }
15091
15092
15093 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15094 the glyph row and area where the glyph comes from. */
15095
15096 void
15097 dump_glyph (row, glyph, area)
15098 struct glyph_row *row;
15099 struct glyph *glyph;
15100 int area;
15101 {
15102 if (glyph->type == CHAR_GLYPH)
15103 {
15104 fprintf (stderr,
15105 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15106 glyph - row->glyphs[TEXT_AREA],
15107 'C',
15108 glyph->charpos,
15109 (BUFFERP (glyph->object)
15110 ? 'B'
15111 : (STRINGP (glyph->object)
15112 ? 'S'
15113 : '-')),
15114 glyph->pixel_width,
15115 glyph->u.ch,
15116 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15117 ? glyph->u.ch
15118 : '.'),
15119 glyph->face_id,
15120 glyph->left_box_line_p,
15121 glyph->right_box_line_p);
15122 }
15123 else if (glyph->type == STRETCH_GLYPH)
15124 {
15125 fprintf (stderr,
15126 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15127 glyph - row->glyphs[TEXT_AREA],
15128 'S',
15129 glyph->charpos,
15130 (BUFFERP (glyph->object)
15131 ? 'B'
15132 : (STRINGP (glyph->object)
15133 ? 'S'
15134 : '-')),
15135 glyph->pixel_width,
15136 0,
15137 '.',
15138 glyph->face_id,
15139 glyph->left_box_line_p,
15140 glyph->right_box_line_p);
15141 }
15142 else if (glyph->type == IMAGE_GLYPH)
15143 {
15144 fprintf (stderr,
15145 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15146 glyph - row->glyphs[TEXT_AREA],
15147 'I',
15148 glyph->charpos,
15149 (BUFFERP (glyph->object)
15150 ? 'B'
15151 : (STRINGP (glyph->object)
15152 ? 'S'
15153 : '-')),
15154 glyph->pixel_width,
15155 glyph->u.img_id,
15156 '.',
15157 glyph->face_id,
15158 glyph->left_box_line_p,
15159 glyph->right_box_line_p);
15160 }
15161 else if (glyph->type == COMPOSITE_GLYPH)
15162 {
15163 fprintf (stderr,
15164 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15165 glyph - row->glyphs[TEXT_AREA],
15166 '+',
15167 glyph->charpos,
15168 (BUFFERP (glyph->object)
15169 ? 'B'
15170 : (STRINGP (glyph->object)
15171 ? 'S'
15172 : '-')),
15173 glyph->pixel_width,
15174 glyph->u.cmp_id,
15175 '.',
15176 glyph->face_id,
15177 glyph->left_box_line_p,
15178 glyph->right_box_line_p);
15179 }
15180 }
15181
15182
15183 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15184 GLYPHS 0 means don't show glyph contents.
15185 GLYPHS 1 means show glyphs in short form
15186 GLYPHS > 1 means show glyphs in long form. */
15187
15188 void
15189 dump_glyph_row (row, vpos, glyphs)
15190 struct glyph_row *row;
15191 int vpos, glyphs;
15192 {
15193 if (glyphs != 1)
15194 {
15195 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15196 fprintf (stderr, "======================================================================\n");
15197
15198 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15199 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15200 vpos,
15201 MATRIX_ROW_START_CHARPOS (row),
15202 MATRIX_ROW_END_CHARPOS (row),
15203 row->used[TEXT_AREA],
15204 row->contains_overlapping_glyphs_p,
15205 row->enabled_p,
15206 row->truncated_on_left_p,
15207 row->truncated_on_right_p,
15208 row->continued_p,
15209 MATRIX_ROW_CONTINUATION_LINE_P (row),
15210 row->displays_text_p,
15211 row->ends_at_zv_p,
15212 row->fill_line_p,
15213 row->ends_in_middle_of_char_p,
15214 row->starts_in_middle_of_char_p,
15215 row->mouse_face_p,
15216 row->x,
15217 row->y,
15218 row->pixel_width,
15219 row->height,
15220 row->visible_height,
15221 row->ascent,
15222 row->phys_ascent);
15223 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15224 row->end.overlay_string_index,
15225 row->continuation_lines_width);
15226 fprintf (stderr, "%9d %5d\n",
15227 CHARPOS (row->start.string_pos),
15228 CHARPOS (row->end.string_pos));
15229 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15230 row->end.dpvec_index);
15231 }
15232
15233 if (glyphs > 1)
15234 {
15235 int area;
15236
15237 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15238 {
15239 struct glyph *glyph = row->glyphs[area];
15240 struct glyph *glyph_end = glyph + row->used[area];
15241
15242 /* Glyph for a line end in text. */
15243 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15244 ++glyph_end;
15245
15246 if (glyph < glyph_end)
15247 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15248
15249 for (; glyph < glyph_end; ++glyph)
15250 dump_glyph (row, glyph, area);
15251 }
15252 }
15253 else if (glyphs == 1)
15254 {
15255 int area;
15256
15257 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15258 {
15259 char *s = (char *) alloca (row->used[area] + 1);
15260 int i;
15261
15262 for (i = 0; i < row->used[area]; ++i)
15263 {
15264 struct glyph *glyph = row->glyphs[area] + i;
15265 if (glyph->type == CHAR_GLYPH
15266 && glyph->u.ch < 0x80
15267 && glyph->u.ch >= ' ')
15268 s[i] = glyph->u.ch;
15269 else
15270 s[i] = '.';
15271 }
15272
15273 s[i] = '\0';
15274 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15275 }
15276 }
15277 }
15278
15279
15280 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15281 Sdump_glyph_matrix, 0, 1, "p",
15282 doc: /* Dump the current matrix of the selected window to stderr.
15283 Shows contents of glyph row structures. With non-nil
15284 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15285 glyphs in short form, otherwise show glyphs in long form. */)
15286 (glyphs)
15287 Lisp_Object glyphs;
15288 {
15289 struct window *w = XWINDOW (selected_window);
15290 struct buffer *buffer = XBUFFER (w->buffer);
15291
15292 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15293 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15294 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15295 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15296 fprintf (stderr, "=============================================\n");
15297 dump_glyph_matrix (w->current_matrix,
15298 NILP (glyphs) ? 0 : XINT (glyphs));
15299 return Qnil;
15300 }
15301
15302
15303 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15304 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15305 ()
15306 {
15307 struct frame *f = XFRAME (selected_frame);
15308 dump_glyph_matrix (f->current_matrix, 1);
15309 return Qnil;
15310 }
15311
15312
15313 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15314 doc: /* Dump glyph row ROW to stderr.
15315 GLYPH 0 means don't dump glyphs.
15316 GLYPH 1 means dump glyphs in short form.
15317 GLYPH > 1 or omitted means dump glyphs in long form. */)
15318 (row, glyphs)
15319 Lisp_Object row, glyphs;
15320 {
15321 struct glyph_matrix *matrix;
15322 int vpos;
15323
15324 CHECK_NUMBER (row);
15325 matrix = XWINDOW (selected_window)->current_matrix;
15326 vpos = XINT (row);
15327 if (vpos >= 0 && vpos < matrix->nrows)
15328 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15329 vpos,
15330 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15331 return Qnil;
15332 }
15333
15334
15335 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15336 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15337 GLYPH 0 means don't dump glyphs.
15338 GLYPH 1 means dump glyphs in short form.
15339 GLYPH > 1 or omitted means dump glyphs in long form. */)
15340 (row, glyphs)
15341 Lisp_Object row, glyphs;
15342 {
15343 struct frame *sf = SELECTED_FRAME ();
15344 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15345 int vpos;
15346
15347 CHECK_NUMBER (row);
15348 vpos = XINT (row);
15349 if (vpos >= 0 && vpos < m->nrows)
15350 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15351 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15352 return Qnil;
15353 }
15354
15355
15356 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15357 doc: /* Toggle tracing of redisplay.
15358 With ARG, turn tracing on if and only if ARG is positive. */)
15359 (arg)
15360 Lisp_Object arg;
15361 {
15362 if (NILP (arg))
15363 trace_redisplay_p = !trace_redisplay_p;
15364 else
15365 {
15366 arg = Fprefix_numeric_value (arg);
15367 trace_redisplay_p = XINT (arg) > 0;
15368 }
15369
15370 return Qnil;
15371 }
15372
15373
15374 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15375 doc: /* Like `format', but print result to stderr.
15376 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15377 (nargs, args)
15378 int nargs;
15379 Lisp_Object *args;
15380 {
15381 Lisp_Object s = Fformat (nargs, args);
15382 fprintf (stderr, "%s", SDATA (s));
15383 return Qnil;
15384 }
15385
15386 #endif /* GLYPH_DEBUG */
15387
15388
15389 \f
15390 /***********************************************************************
15391 Building Desired Matrix Rows
15392 ***********************************************************************/
15393
15394 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15395 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15396
15397 static struct glyph_row *
15398 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15399 struct window *w;
15400 Lisp_Object overlay_arrow_string;
15401 {
15402 struct frame *f = XFRAME (WINDOW_FRAME (w));
15403 struct buffer *buffer = XBUFFER (w->buffer);
15404 struct buffer *old = current_buffer;
15405 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15406 int arrow_len = SCHARS (overlay_arrow_string);
15407 const unsigned char *arrow_end = arrow_string + arrow_len;
15408 const unsigned char *p;
15409 struct it it;
15410 int multibyte_p;
15411 int n_glyphs_before;
15412
15413 set_buffer_temp (buffer);
15414 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15415 it.glyph_row->used[TEXT_AREA] = 0;
15416 SET_TEXT_POS (it.position, 0, 0);
15417
15418 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15419 p = arrow_string;
15420 while (p < arrow_end)
15421 {
15422 Lisp_Object face, ilisp;
15423
15424 /* Get the next character. */
15425 if (multibyte_p)
15426 it.c = string_char_and_length (p, arrow_len, &it.len);
15427 else
15428 it.c = *p, it.len = 1;
15429 p += it.len;
15430
15431 /* Get its face. */
15432 ilisp = make_number (p - arrow_string);
15433 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15434 it.face_id = compute_char_face (f, it.c, face);
15435
15436 /* Compute its width, get its glyphs. */
15437 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15438 SET_TEXT_POS (it.position, -1, -1);
15439 PRODUCE_GLYPHS (&it);
15440
15441 /* If this character doesn't fit any more in the line, we have
15442 to remove some glyphs. */
15443 if (it.current_x > it.last_visible_x)
15444 {
15445 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15446 break;
15447 }
15448 }
15449
15450 set_buffer_temp (old);
15451 return it.glyph_row;
15452 }
15453
15454
15455 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15456 glyphs are only inserted for terminal frames since we can't really
15457 win with truncation glyphs when partially visible glyphs are
15458 involved. Which glyphs to insert is determined by
15459 produce_special_glyphs. */
15460
15461 static void
15462 insert_left_trunc_glyphs (it)
15463 struct it *it;
15464 {
15465 struct it truncate_it;
15466 struct glyph *from, *end, *to, *toend;
15467
15468 xassert (!FRAME_WINDOW_P (it->f));
15469
15470 /* Get the truncation glyphs. */
15471 truncate_it = *it;
15472 truncate_it.current_x = 0;
15473 truncate_it.face_id = DEFAULT_FACE_ID;
15474 truncate_it.glyph_row = &scratch_glyph_row;
15475 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15476 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15477 truncate_it.object = make_number (0);
15478 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15479
15480 /* Overwrite glyphs from IT with truncation glyphs. */
15481 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15482 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15483 to = it->glyph_row->glyphs[TEXT_AREA];
15484 toend = to + it->glyph_row->used[TEXT_AREA];
15485
15486 while (from < end)
15487 *to++ = *from++;
15488
15489 /* There may be padding glyphs left over. Overwrite them too. */
15490 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15491 {
15492 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15493 while (from < end)
15494 *to++ = *from++;
15495 }
15496
15497 if (to > toend)
15498 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15499 }
15500
15501
15502 /* Compute the pixel height and width of IT->glyph_row.
15503
15504 Most of the time, ascent and height of a display line will be equal
15505 to the max_ascent and max_height values of the display iterator
15506 structure. This is not the case if
15507
15508 1. We hit ZV without displaying anything. In this case, max_ascent
15509 and max_height will be zero.
15510
15511 2. We have some glyphs that don't contribute to the line height.
15512 (The glyph row flag contributes_to_line_height_p is for future
15513 pixmap extensions).
15514
15515 The first case is easily covered by using default values because in
15516 these cases, the line height does not really matter, except that it
15517 must not be zero. */
15518
15519 static void
15520 compute_line_metrics (it)
15521 struct it *it;
15522 {
15523 struct glyph_row *row = it->glyph_row;
15524 int area, i;
15525
15526 if (FRAME_WINDOW_P (it->f))
15527 {
15528 int i, min_y, max_y;
15529
15530 /* The line may consist of one space only, that was added to
15531 place the cursor on it. If so, the row's height hasn't been
15532 computed yet. */
15533 if (row->height == 0)
15534 {
15535 if (it->max_ascent + it->max_descent == 0)
15536 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15537 row->ascent = it->max_ascent;
15538 row->height = it->max_ascent + it->max_descent;
15539 row->phys_ascent = it->max_phys_ascent;
15540 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15541 row->extra_line_spacing = it->max_extra_line_spacing;
15542 }
15543
15544 /* Compute the width of this line. */
15545 row->pixel_width = row->x;
15546 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15547 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15548
15549 xassert (row->pixel_width >= 0);
15550 xassert (row->ascent >= 0 && row->height > 0);
15551
15552 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15553 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15554
15555 /* If first line's physical ascent is larger than its logical
15556 ascent, use the physical ascent, and make the row taller.
15557 This makes accented characters fully visible. */
15558 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15559 && row->phys_ascent > row->ascent)
15560 {
15561 row->height += row->phys_ascent - row->ascent;
15562 row->ascent = row->phys_ascent;
15563 }
15564
15565 /* Compute how much of the line is visible. */
15566 row->visible_height = row->height;
15567
15568 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15569 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15570
15571 if (row->y < min_y)
15572 row->visible_height -= min_y - row->y;
15573 if (row->y + row->height > max_y)
15574 row->visible_height -= row->y + row->height - max_y;
15575 }
15576 else
15577 {
15578 row->pixel_width = row->used[TEXT_AREA];
15579 if (row->continued_p)
15580 row->pixel_width -= it->continuation_pixel_width;
15581 else if (row->truncated_on_right_p)
15582 row->pixel_width -= it->truncation_pixel_width;
15583 row->ascent = row->phys_ascent = 0;
15584 row->height = row->phys_height = row->visible_height = 1;
15585 row->extra_line_spacing = 0;
15586 }
15587
15588 /* Compute a hash code for this row. */
15589 row->hash = 0;
15590 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15591 for (i = 0; i < row->used[area]; ++i)
15592 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15593 + row->glyphs[area][i].u.val
15594 + row->glyphs[area][i].face_id
15595 + row->glyphs[area][i].padding_p
15596 + (row->glyphs[area][i].type << 2));
15597
15598 it->max_ascent = it->max_descent = 0;
15599 it->max_phys_ascent = it->max_phys_descent = 0;
15600 }
15601
15602
15603 /* Append one space to the glyph row of iterator IT if doing a
15604 window-based redisplay. The space has the same face as
15605 IT->face_id. Value is non-zero if a space was added.
15606
15607 This function is called to make sure that there is always one glyph
15608 at the end of a glyph row that the cursor can be set on under
15609 window-systems. (If there weren't such a glyph we would not know
15610 how wide and tall a box cursor should be displayed).
15611
15612 At the same time this space let's a nicely handle clearing to the
15613 end of the line if the row ends in italic text. */
15614
15615 static int
15616 append_space_for_newline (it, default_face_p)
15617 struct it *it;
15618 int default_face_p;
15619 {
15620 if (FRAME_WINDOW_P (it->f))
15621 {
15622 int n = it->glyph_row->used[TEXT_AREA];
15623
15624 if (it->glyph_row->glyphs[TEXT_AREA] + n
15625 < it->glyph_row->glyphs[1 + TEXT_AREA])
15626 {
15627 /* Save some values that must not be changed.
15628 Must save IT->c and IT->len because otherwise
15629 ITERATOR_AT_END_P wouldn't work anymore after
15630 append_space_for_newline has been called. */
15631 enum display_element_type saved_what = it->what;
15632 int saved_c = it->c, saved_len = it->len;
15633 int saved_x = it->current_x;
15634 int saved_face_id = it->face_id;
15635 struct text_pos saved_pos;
15636 Lisp_Object saved_object;
15637 struct face *face;
15638
15639 saved_object = it->object;
15640 saved_pos = it->position;
15641
15642 it->what = IT_CHARACTER;
15643 bzero (&it->position, sizeof it->position);
15644 it->object = make_number (0);
15645 it->c = ' ';
15646 it->len = 1;
15647
15648 if (default_face_p)
15649 it->face_id = DEFAULT_FACE_ID;
15650 else if (it->face_before_selective_p)
15651 it->face_id = it->saved_face_id;
15652 face = FACE_FROM_ID (it->f, it->face_id);
15653 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15654
15655 PRODUCE_GLYPHS (it);
15656
15657 it->override_ascent = -1;
15658 it->constrain_row_ascent_descent_p = 0;
15659 it->current_x = saved_x;
15660 it->object = saved_object;
15661 it->position = saved_pos;
15662 it->what = saved_what;
15663 it->face_id = saved_face_id;
15664 it->len = saved_len;
15665 it->c = saved_c;
15666 return 1;
15667 }
15668 }
15669
15670 return 0;
15671 }
15672
15673
15674 /* Extend the face of the last glyph in the text area of IT->glyph_row
15675 to the end of the display line. Called from display_line.
15676 If the glyph row is empty, add a space glyph to it so that we
15677 know the face to draw. Set the glyph row flag fill_line_p. */
15678
15679 static void
15680 extend_face_to_end_of_line (it)
15681 struct it *it;
15682 {
15683 struct face *face;
15684 struct frame *f = it->f;
15685
15686 /* If line is already filled, do nothing. */
15687 if (it->current_x >= it->last_visible_x)
15688 return;
15689
15690 /* Face extension extends the background and box of IT->face_id
15691 to the end of the line. If the background equals the background
15692 of the frame, we don't have to do anything. */
15693 if (it->face_before_selective_p)
15694 face = FACE_FROM_ID (it->f, it->saved_face_id);
15695 else
15696 face = FACE_FROM_ID (f, it->face_id);
15697
15698 if (FRAME_WINDOW_P (f)
15699 && it->glyph_row->displays_text_p
15700 && face->box == FACE_NO_BOX
15701 && face->background == FRAME_BACKGROUND_PIXEL (f)
15702 && !face->stipple)
15703 return;
15704
15705 /* Set the glyph row flag indicating that the face of the last glyph
15706 in the text area has to be drawn to the end of the text area. */
15707 it->glyph_row->fill_line_p = 1;
15708
15709 /* If current character of IT is not ASCII, make sure we have the
15710 ASCII face. This will be automatically undone the next time
15711 get_next_display_element returns a multibyte character. Note
15712 that the character will always be single byte in unibyte text. */
15713 if (!SINGLE_BYTE_CHAR_P (it->c))
15714 {
15715 it->face_id = FACE_FOR_CHAR (f, face, 0);
15716 }
15717
15718 if (FRAME_WINDOW_P (f))
15719 {
15720 /* If the row is empty, add a space with the current face of IT,
15721 so that we know which face to draw. */
15722 if (it->glyph_row->used[TEXT_AREA] == 0)
15723 {
15724 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15725 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15726 it->glyph_row->used[TEXT_AREA] = 1;
15727 }
15728 }
15729 else
15730 {
15731 /* Save some values that must not be changed. */
15732 int saved_x = it->current_x;
15733 struct text_pos saved_pos;
15734 Lisp_Object saved_object;
15735 enum display_element_type saved_what = it->what;
15736 int saved_face_id = it->face_id;
15737
15738 saved_object = it->object;
15739 saved_pos = it->position;
15740
15741 it->what = IT_CHARACTER;
15742 bzero (&it->position, sizeof it->position);
15743 it->object = make_number (0);
15744 it->c = ' ';
15745 it->len = 1;
15746 it->face_id = face->id;
15747
15748 PRODUCE_GLYPHS (it);
15749
15750 while (it->current_x <= it->last_visible_x)
15751 PRODUCE_GLYPHS (it);
15752
15753 /* Don't count these blanks really. It would let us insert a left
15754 truncation glyph below and make us set the cursor on them, maybe. */
15755 it->current_x = saved_x;
15756 it->object = saved_object;
15757 it->position = saved_pos;
15758 it->what = saved_what;
15759 it->face_id = saved_face_id;
15760 }
15761 }
15762
15763
15764 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15765 trailing whitespace. */
15766
15767 static int
15768 trailing_whitespace_p (charpos)
15769 int charpos;
15770 {
15771 int bytepos = CHAR_TO_BYTE (charpos);
15772 int c = 0;
15773
15774 while (bytepos < ZV_BYTE
15775 && (c = FETCH_CHAR (bytepos),
15776 c == ' ' || c == '\t'))
15777 ++bytepos;
15778
15779 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15780 {
15781 if (bytepos != PT_BYTE)
15782 return 1;
15783 }
15784 return 0;
15785 }
15786
15787
15788 /* Highlight trailing whitespace, if any, in ROW. */
15789
15790 void
15791 highlight_trailing_whitespace (f, row)
15792 struct frame *f;
15793 struct glyph_row *row;
15794 {
15795 int used = row->used[TEXT_AREA];
15796
15797 if (used)
15798 {
15799 struct glyph *start = row->glyphs[TEXT_AREA];
15800 struct glyph *glyph = start + used - 1;
15801
15802 /* Skip over glyphs inserted to display the cursor at the
15803 end of a line, for extending the face of the last glyph
15804 to the end of the line on terminals, and for truncation
15805 and continuation glyphs. */
15806 while (glyph >= start
15807 && glyph->type == CHAR_GLYPH
15808 && INTEGERP (glyph->object))
15809 --glyph;
15810
15811 /* If last glyph is a space or stretch, and it's trailing
15812 whitespace, set the face of all trailing whitespace glyphs in
15813 IT->glyph_row to `trailing-whitespace'. */
15814 if (glyph >= start
15815 && BUFFERP (glyph->object)
15816 && (glyph->type == STRETCH_GLYPH
15817 || (glyph->type == CHAR_GLYPH
15818 && glyph->u.ch == ' '))
15819 && trailing_whitespace_p (glyph->charpos))
15820 {
15821 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15822 if (face_id < 0)
15823 return;
15824
15825 while (glyph >= start
15826 && BUFFERP (glyph->object)
15827 && (glyph->type == STRETCH_GLYPH
15828 || (glyph->type == CHAR_GLYPH
15829 && glyph->u.ch == ' ')))
15830 (glyph--)->face_id = face_id;
15831 }
15832 }
15833 }
15834
15835
15836 /* Value is non-zero if glyph row ROW in window W should be
15837 used to hold the cursor. */
15838
15839 static int
15840 cursor_row_p (w, row)
15841 struct window *w;
15842 struct glyph_row *row;
15843 {
15844 int cursor_row_p = 1;
15845
15846 if (PT == MATRIX_ROW_END_CHARPOS (row))
15847 {
15848 /* Suppose the row ends on a string.
15849 Unless the row is continued, that means it ends on a newline
15850 in the string. If it's anything other than a display string
15851 (e.g. a before-string from an overlay), we don't want the
15852 cursor there. (This heuristic seems to give the optimal
15853 behavior for the various types of multi-line strings.) */
15854 if (CHARPOS (row->end.string_pos) >= 0)
15855 {
15856 if (row->continued_p)
15857 cursor_row_p = 1;
15858 else
15859 {
15860 /* Check for `display' property. */
15861 struct glyph *beg = row->glyphs[TEXT_AREA];
15862 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15863 struct glyph *glyph;
15864
15865 cursor_row_p = 0;
15866 for (glyph = end; glyph >= beg; --glyph)
15867 if (STRINGP (glyph->object))
15868 {
15869 Lisp_Object prop
15870 = Fget_char_property (make_number (PT),
15871 Qdisplay, Qnil);
15872 cursor_row_p =
15873 (!NILP (prop)
15874 && display_prop_string_p (prop, glyph->object));
15875 break;
15876 }
15877 }
15878 }
15879 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15880 {
15881 /* If the row ends in middle of a real character,
15882 and the line is continued, we want the cursor here.
15883 That's because MATRIX_ROW_END_CHARPOS would equal
15884 PT if PT is before the character. */
15885 if (!row->ends_in_ellipsis_p)
15886 cursor_row_p = row->continued_p;
15887 else
15888 /* If the row ends in an ellipsis, then
15889 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15890 We want that position to be displayed after the ellipsis. */
15891 cursor_row_p = 0;
15892 }
15893 /* If the row ends at ZV, display the cursor at the end of that
15894 row instead of at the start of the row below. */
15895 else if (row->ends_at_zv_p)
15896 cursor_row_p = 1;
15897 else
15898 cursor_row_p = 0;
15899 }
15900
15901 return cursor_row_p;
15902 }
15903
15904
15905 /* Construct the glyph row IT->glyph_row in the desired matrix of
15906 IT->w from text at the current position of IT. See dispextern.h
15907 for an overview of struct it. Value is non-zero if
15908 IT->glyph_row displays text, as opposed to a line displaying ZV
15909 only. */
15910
15911 static int
15912 display_line (it)
15913 struct it *it;
15914 {
15915 struct glyph_row *row = it->glyph_row;
15916 Lisp_Object overlay_arrow_string;
15917
15918 /* We always start displaying at hpos zero even if hscrolled. */
15919 xassert (it->hpos == 0 && it->current_x == 0);
15920
15921 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15922 >= it->w->desired_matrix->nrows)
15923 {
15924 it->w->nrows_scale_factor++;
15925 fonts_changed_p = 1;
15926 return 0;
15927 }
15928
15929 /* Is IT->w showing the region? */
15930 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15931
15932 /* Clear the result glyph row and enable it. */
15933 prepare_desired_row (row);
15934
15935 row->y = it->current_y;
15936 row->start = it->start;
15937 row->continuation_lines_width = it->continuation_lines_width;
15938 row->displays_text_p = 1;
15939 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15940 it->starts_in_middle_of_char_p = 0;
15941
15942 /* Arrange the overlays nicely for our purposes. Usually, we call
15943 display_line on only one line at a time, in which case this
15944 can't really hurt too much, or we call it on lines which appear
15945 one after another in the buffer, in which case all calls to
15946 recenter_overlay_lists but the first will be pretty cheap. */
15947 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15948
15949 /* Move over display elements that are not visible because we are
15950 hscrolled. This may stop at an x-position < IT->first_visible_x
15951 if the first glyph is partially visible or if we hit a line end. */
15952 if (it->current_x < it->first_visible_x)
15953 {
15954 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15955 MOVE_TO_POS | MOVE_TO_X);
15956 }
15957
15958 /* Get the initial row height. This is either the height of the
15959 text hscrolled, if there is any, or zero. */
15960 row->ascent = it->max_ascent;
15961 row->height = it->max_ascent + it->max_descent;
15962 row->phys_ascent = it->max_phys_ascent;
15963 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15964 row->extra_line_spacing = it->max_extra_line_spacing;
15965
15966 /* Loop generating characters. The loop is left with IT on the next
15967 character to display. */
15968 while (1)
15969 {
15970 int n_glyphs_before, hpos_before, x_before;
15971 int x, i, nglyphs;
15972 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15973
15974 /* Retrieve the next thing to display. Value is zero if end of
15975 buffer reached. */
15976 if (!get_next_display_element (it))
15977 {
15978 /* Maybe add a space at the end of this line that is used to
15979 display the cursor there under X. Set the charpos of the
15980 first glyph of blank lines not corresponding to any text
15981 to -1. */
15982 #ifdef HAVE_WINDOW_SYSTEM
15983 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15984 row->exact_window_width_line_p = 1;
15985 else
15986 #endif /* HAVE_WINDOW_SYSTEM */
15987 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15988 || row->used[TEXT_AREA] == 0)
15989 {
15990 row->glyphs[TEXT_AREA]->charpos = -1;
15991 row->displays_text_p = 0;
15992
15993 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15994 && (!MINI_WINDOW_P (it->w)
15995 || (minibuf_level && EQ (it->window, minibuf_window))))
15996 row->indicate_empty_line_p = 1;
15997 }
15998
15999 it->continuation_lines_width = 0;
16000 row->ends_at_zv_p = 1;
16001 break;
16002 }
16003
16004 /* Now, get the metrics of what we want to display. This also
16005 generates glyphs in `row' (which is IT->glyph_row). */
16006 n_glyphs_before = row->used[TEXT_AREA];
16007 x = it->current_x;
16008
16009 /* Remember the line height so far in case the next element doesn't
16010 fit on the line. */
16011 if (!it->truncate_lines_p)
16012 {
16013 ascent = it->max_ascent;
16014 descent = it->max_descent;
16015 phys_ascent = it->max_phys_ascent;
16016 phys_descent = it->max_phys_descent;
16017 }
16018
16019 PRODUCE_GLYPHS (it);
16020
16021 /* If this display element was in marginal areas, continue with
16022 the next one. */
16023 if (it->area != TEXT_AREA)
16024 {
16025 row->ascent = max (row->ascent, it->max_ascent);
16026 row->height = max (row->height, it->max_ascent + it->max_descent);
16027 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16028 row->phys_height = max (row->phys_height,
16029 it->max_phys_ascent + it->max_phys_descent);
16030 row->extra_line_spacing = max (row->extra_line_spacing,
16031 it->max_extra_line_spacing);
16032 set_iterator_to_next (it, 1);
16033 continue;
16034 }
16035
16036 /* Does the display element fit on the line? If we truncate
16037 lines, we should draw past the right edge of the window. If
16038 we don't truncate, we want to stop so that we can display the
16039 continuation glyph before the right margin. If lines are
16040 continued, there are two possible strategies for characters
16041 resulting in more than 1 glyph (e.g. tabs): Display as many
16042 glyphs as possible in this line and leave the rest for the
16043 continuation line, or display the whole element in the next
16044 line. Original redisplay did the former, so we do it also. */
16045 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16046 hpos_before = it->hpos;
16047 x_before = x;
16048
16049 if (/* Not a newline. */
16050 nglyphs > 0
16051 /* Glyphs produced fit entirely in the line. */
16052 && it->current_x < it->last_visible_x)
16053 {
16054 it->hpos += nglyphs;
16055 row->ascent = max (row->ascent, it->max_ascent);
16056 row->height = max (row->height, it->max_ascent + it->max_descent);
16057 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16058 row->phys_height = max (row->phys_height,
16059 it->max_phys_ascent + it->max_phys_descent);
16060 row->extra_line_spacing = max (row->extra_line_spacing,
16061 it->max_extra_line_spacing);
16062 if (it->current_x - it->pixel_width < it->first_visible_x)
16063 row->x = x - it->first_visible_x;
16064 }
16065 else
16066 {
16067 int new_x;
16068 struct glyph *glyph;
16069
16070 for (i = 0; i < nglyphs; ++i, x = new_x)
16071 {
16072 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16073 new_x = x + glyph->pixel_width;
16074
16075 if (/* Lines are continued. */
16076 !it->truncate_lines_p
16077 && (/* Glyph doesn't fit on the line. */
16078 new_x > it->last_visible_x
16079 /* Or it fits exactly on a window system frame. */
16080 || (new_x == it->last_visible_x
16081 && FRAME_WINDOW_P (it->f))))
16082 {
16083 /* End of a continued line. */
16084
16085 if (it->hpos == 0
16086 || (new_x == it->last_visible_x
16087 && FRAME_WINDOW_P (it->f)))
16088 {
16089 /* Current glyph is the only one on the line or
16090 fits exactly on the line. We must continue
16091 the line because we can't draw the cursor
16092 after the glyph. */
16093 row->continued_p = 1;
16094 it->current_x = new_x;
16095 it->continuation_lines_width += new_x;
16096 ++it->hpos;
16097 if (i == nglyphs - 1)
16098 {
16099 set_iterator_to_next (it, 1);
16100 #ifdef HAVE_WINDOW_SYSTEM
16101 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16102 {
16103 if (!get_next_display_element (it))
16104 {
16105 row->exact_window_width_line_p = 1;
16106 it->continuation_lines_width = 0;
16107 row->continued_p = 0;
16108 row->ends_at_zv_p = 1;
16109 }
16110 else if (ITERATOR_AT_END_OF_LINE_P (it))
16111 {
16112 row->continued_p = 0;
16113 row->exact_window_width_line_p = 1;
16114 }
16115 }
16116 #endif /* HAVE_WINDOW_SYSTEM */
16117 }
16118 }
16119 else if (CHAR_GLYPH_PADDING_P (*glyph)
16120 && !FRAME_WINDOW_P (it->f))
16121 {
16122 /* A padding glyph that doesn't fit on this line.
16123 This means the whole character doesn't fit
16124 on the line. */
16125 row->used[TEXT_AREA] = n_glyphs_before;
16126
16127 /* Fill the rest of the row with continuation
16128 glyphs like in 20.x. */
16129 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16130 < row->glyphs[1 + TEXT_AREA])
16131 produce_special_glyphs (it, IT_CONTINUATION);
16132
16133 row->continued_p = 1;
16134 it->current_x = x_before;
16135 it->continuation_lines_width += x_before;
16136
16137 /* Restore the height to what it was before the
16138 element not fitting on the line. */
16139 it->max_ascent = ascent;
16140 it->max_descent = descent;
16141 it->max_phys_ascent = phys_ascent;
16142 it->max_phys_descent = phys_descent;
16143 }
16144 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16145 {
16146 /* A TAB that extends past the right edge of the
16147 window. This produces a single glyph on
16148 window system frames. We leave the glyph in
16149 this row and let it fill the row, but don't
16150 consume the TAB. */
16151 it->continuation_lines_width += it->last_visible_x;
16152 row->ends_in_middle_of_char_p = 1;
16153 row->continued_p = 1;
16154 glyph->pixel_width = it->last_visible_x - x;
16155 it->starts_in_middle_of_char_p = 1;
16156 }
16157 else
16158 {
16159 /* Something other than a TAB that draws past
16160 the right edge of the window. Restore
16161 positions to values before the element. */
16162 row->used[TEXT_AREA] = n_glyphs_before + i;
16163
16164 /* Display continuation glyphs. */
16165 if (!FRAME_WINDOW_P (it->f))
16166 produce_special_glyphs (it, IT_CONTINUATION);
16167 row->continued_p = 1;
16168
16169 it->current_x = x_before;
16170 it->continuation_lines_width += x;
16171 extend_face_to_end_of_line (it);
16172
16173 if (nglyphs > 1 && i > 0)
16174 {
16175 row->ends_in_middle_of_char_p = 1;
16176 it->starts_in_middle_of_char_p = 1;
16177 }
16178
16179 /* Restore the height to what it was before the
16180 element not fitting on the line. */
16181 it->max_ascent = ascent;
16182 it->max_descent = descent;
16183 it->max_phys_ascent = phys_ascent;
16184 it->max_phys_descent = phys_descent;
16185 }
16186
16187 break;
16188 }
16189 else if (new_x > it->first_visible_x)
16190 {
16191 /* Increment number of glyphs actually displayed. */
16192 ++it->hpos;
16193
16194 if (x < it->first_visible_x)
16195 /* Glyph is partially visible, i.e. row starts at
16196 negative X position. */
16197 row->x = x - it->first_visible_x;
16198 }
16199 else
16200 {
16201 /* Glyph is completely off the left margin of the
16202 window. This should not happen because of the
16203 move_it_in_display_line at the start of this
16204 function, unless the text display area of the
16205 window is empty. */
16206 xassert (it->first_visible_x <= it->last_visible_x);
16207 }
16208 }
16209
16210 row->ascent = max (row->ascent, it->max_ascent);
16211 row->height = max (row->height, it->max_ascent + it->max_descent);
16212 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16213 row->phys_height = max (row->phys_height,
16214 it->max_phys_ascent + it->max_phys_descent);
16215 row->extra_line_spacing = max (row->extra_line_spacing,
16216 it->max_extra_line_spacing);
16217
16218 /* End of this display line if row is continued. */
16219 if (row->continued_p || row->ends_at_zv_p)
16220 break;
16221 }
16222
16223 at_end_of_line:
16224 /* Is this a line end? If yes, we're also done, after making
16225 sure that a non-default face is extended up to the right
16226 margin of the window. */
16227 if (ITERATOR_AT_END_OF_LINE_P (it))
16228 {
16229 int used_before = row->used[TEXT_AREA];
16230
16231 row->ends_in_newline_from_string_p = STRINGP (it->object);
16232
16233 #ifdef HAVE_WINDOW_SYSTEM
16234 /* Add a space at the end of the line that is used to
16235 display the cursor there. */
16236 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16237 append_space_for_newline (it, 0);
16238 #endif /* HAVE_WINDOW_SYSTEM */
16239
16240 /* Extend the face to the end of the line. */
16241 extend_face_to_end_of_line (it);
16242
16243 /* Make sure we have the position. */
16244 if (used_before == 0)
16245 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16246
16247 /* Consume the line end. This skips over invisible lines. */
16248 set_iterator_to_next (it, 1);
16249 it->continuation_lines_width = 0;
16250 break;
16251 }
16252
16253 /* Proceed with next display element. Note that this skips
16254 over lines invisible because of selective display. */
16255 set_iterator_to_next (it, 1);
16256
16257 /* If we truncate lines, we are done when the last displayed
16258 glyphs reach past the right margin of the window. */
16259 if (it->truncate_lines_p
16260 && (FRAME_WINDOW_P (it->f)
16261 ? (it->current_x >= it->last_visible_x)
16262 : (it->current_x > it->last_visible_x)))
16263 {
16264 /* Maybe add truncation glyphs. */
16265 if (!FRAME_WINDOW_P (it->f))
16266 {
16267 int i, n;
16268
16269 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16270 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16271 break;
16272
16273 for (n = row->used[TEXT_AREA]; i < n; ++i)
16274 {
16275 row->used[TEXT_AREA] = i;
16276 produce_special_glyphs (it, IT_TRUNCATION);
16277 }
16278 }
16279 #ifdef HAVE_WINDOW_SYSTEM
16280 else
16281 {
16282 /* Don't truncate if we can overflow newline into fringe. */
16283 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16284 {
16285 if (!get_next_display_element (it))
16286 {
16287 it->continuation_lines_width = 0;
16288 row->ends_at_zv_p = 1;
16289 row->exact_window_width_line_p = 1;
16290 break;
16291 }
16292 if (ITERATOR_AT_END_OF_LINE_P (it))
16293 {
16294 row->exact_window_width_line_p = 1;
16295 goto at_end_of_line;
16296 }
16297 }
16298 }
16299 #endif /* HAVE_WINDOW_SYSTEM */
16300
16301 row->truncated_on_right_p = 1;
16302 it->continuation_lines_width = 0;
16303 reseat_at_next_visible_line_start (it, 0);
16304 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16305 it->hpos = hpos_before;
16306 it->current_x = x_before;
16307 break;
16308 }
16309 }
16310
16311 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16312 at the left window margin. */
16313 if (it->first_visible_x
16314 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16315 {
16316 if (!FRAME_WINDOW_P (it->f))
16317 insert_left_trunc_glyphs (it);
16318 row->truncated_on_left_p = 1;
16319 }
16320
16321 /* If the start of this line is the overlay arrow-position, then
16322 mark this glyph row as the one containing the overlay arrow.
16323 This is clearly a mess with variable size fonts. It would be
16324 better to let it be displayed like cursors under X. */
16325 if ((row->displays_text_p || !overlay_arrow_seen)
16326 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16327 !NILP (overlay_arrow_string)))
16328 {
16329 /* Overlay arrow in window redisplay is a fringe bitmap. */
16330 if (STRINGP (overlay_arrow_string))
16331 {
16332 struct glyph_row *arrow_row
16333 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16334 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16335 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16336 struct glyph *p = row->glyphs[TEXT_AREA];
16337 struct glyph *p2, *end;
16338
16339 /* Copy the arrow glyphs. */
16340 while (glyph < arrow_end)
16341 *p++ = *glyph++;
16342
16343 /* Throw away padding glyphs. */
16344 p2 = p;
16345 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16346 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16347 ++p2;
16348 if (p2 > p)
16349 {
16350 while (p2 < end)
16351 *p++ = *p2++;
16352 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16353 }
16354 }
16355 else
16356 {
16357 xassert (INTEGERP (overlay_arrow_string));
16358 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16359 }
16360 overlay_arrow_seen = 1;
16361 }
16362
16363 /* Compute pixel dimensions of this line. */
16364 compute_line_metrics (it);
16365
16366 /* Remember the position at which this line ends. */
16367 row->end = it->current;
16368
16369 /* Record whether this row ends inside an ellipsis. */
16370 row->ends_in_ellipsis_p
16371 = (it->method == GET_FROM_DISPLAY_VECTOR
16372 && it->ellipsis_p);
16373
16374 /* Save fringe bitmaps in this row. */
16375 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16376 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16377 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16378 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16379
16380 it->left_user_fringe_bitmap = 0;
16381 it->left_user_fringe_face_id = 0;
16382 it->right_user_fringe_bitmap = 0;
16383 it->right_user_fringe_face_id = 0;
16384
16385 /* Maybe set the cursor. */
16386 if (it->w->cursor.vpos < 0
16387 && PT >= MATRIX_ROW_START_CHARPOS (row)
16388 && PT <= MATRIX_ROW_END_CHARPOS (row)
16389 && cursor_row_p (it->w, row))
16390 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16391
16392 /* Highlight trailing whitespace. */
16393 if (!NILP (Vshow_trailing_whitespace))
16394 highlight_trailing_whitespace (it->f, it->glyph_row);
16395
16396 /* Prepare for the next line. This line starts horizontally at (X
16397 HPOS) = (0 0). Vertical positions are incremented. As a
16398 convenience for the caller, IT->glyph_row is set to the next
16399 row to be used. */
16400 it->current_x = it->hpos = 0;
16401 it->current_y += row->height;
16402 ++it->vpos;
16403 ++it->glyph_row;
16404 it->start = it->current;
16405 return row->displays_text_p;
16406 }
16407
16408
16409 \f
16410 /***********************************************************************
16411 Menu Bar
16412 ***********************************************************************/
16413
16414 /* Redisplay the menu bar in the frame for window W.
16415
16416 The menu bar of X frames that don't have X toolkit support is
16417 displayed in a special window W->frame->menu_bar_window.
16418
16419 The menu bar of terminal frames is treated specially as far as
16420 glyph matrices are concerned. Menu bar lines are not part of
16421 windows, so the update is done directly on the frame matrix rows
16422 for the menu bar. */
16423
16424 static void
16425 display_menu_bar (w)
16426 struct window *w;
16427 {
16428 struct frame *f = XFRAME (WINDOW_FRAME (w));
16429 struct it it;
16430 Lisp_Object items;
16431 int i;
16432
16433 /* Don't do all this for graphical frames. */
16434 #ifdef HAVE_NTGUI
16435 if (!NILP (Vwindow_system))
16436 return;
16437 #endif
16438 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16439 if (FRAME_X_P (f))
16440 return;
16441 #endif
16442 #ifdef MAC_OS
16443 if (FRAME_MAC_P (f))
16444 return;
16445 #endif
16446
16447 #ifdef USE_X_TOOLKIT
16448 xassert (!FRAME_WINDOW_P (f));
16449 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16450 it.first_visible_x = 0;
16451 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16452 #else /* not USE_X_TOOLKIT */
16453 if (FRAME_WINDOW_P (f))
16454 {
16455 /* Menu bar lines are displayed in the desired matrix of the
16456 dummy window menu_bar_window. */
16457 struct window *menu_w;
16458 xassert (WINDOWP (f->menu_bar_window));
16459 menu_w = XWINDOW (f->menu_bar_window);
16460 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16461 MENU_FACE_ID);
16462 it.first_visible_x = 0;
16463 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16464 }
16465 else
16466 {
16467 /* This is a TTY frame, i.e. character hpos/vpos are used as
16468 pixel x/y. */
16469 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16470 MENU_FACE_ID);
16471 it.first_visible_x = 0;
16472 it.last_visible_x = FRAME_COLS (f);
16473 }
16474 #endif /* not USE_X_TOOLKIT */
16475
16476 if (! mode_line_inverse_video)
16477 /* Force the menu-bar to be displayed in the default face. */
16478 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16479
16480 /* Clear all rows of the menu bar. */
16481 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16482 {
16483 struct glyph_row *row = it.glyph_row + i;
16484 clear_glyph_row (row);
16485 row->enabled_p = 1;
16486 row->full_width_p = 1;
16487 }
16488
16489 /* Display all items of the menu bar. */
16490 items = FRAME_MENU_BAR_ITEMS (it.f);
16491 for (i = 0; i < XVECTOR (items)->size; i += 4)
16492 {
16493 Lisp_Object string;
16494
16495 /* Stop at nil string. */
16496 string = AREF (items, i + 1);
16497 if (NILP (string))
16498 break;
16499
16500 /* Remember where item was displayed. */
16501 AREF (items, i + 3) = make_number (it.hpos);
16502
16503 /* Display the item, pad with one space. */
16504 if (it.current_x < it.last_visible_x)
16505 display_string (NULL, string, Qnil, 0, 0, &it,
16506 SCHARS (string) + 1, 0, 0, -1);
16507 }
16508
16509 /* Fill out the line with spaces. */
16510 if (it.current_x < it.last_visible_x)
16511 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16512
16513 /* Compute the total height of the lines. */
16514 compute_line_metrics (&it);
16515 }
16516
16517
16518 \f
16519 /***********************************************************************
16520 Mode Line
16521 ***********************************************************************/
16522
16523 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16524 FORCE is non-zero, redisplay mode lines unconditionally.
16525 Otherwise, redisplay only mode lines that are garbaged. Value is
16526 the number of windows whose mode lines were redisplayed. */
16527
16528 static int
16529 redisplay_mode_lines (window, force)
16530 Lisp_Object window;
16531 int force;
16532 {
16533 int nwindows = 0;
16534
16535 while (!NILP (window))
16536 {
16537 struct window *w = XWINDOW (window);
16538
16539 if (WINDOWP (w->hchild))
16540 nwindows += redisplay_mode_lines (w->hchild, force);
16541 else if (WINDOWP (w->vchild))
16542 nwindows += redisplay_mode_lines (w->vchild, force);
16543 else if (force
16544 || FRAME_GARBAGED_P (XFRAME (w->frame))
16545 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16546 {
16547 struct text_pos lpoint;
16548 struct buffer *old = current_buffer;
16549
16550 /* Set the window's buffer for the mode line display. */
16551 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16552 set_buffer_internal_1 (XBUFFER (w->buffer));
16553
16554 /* Point refers normally to the selected window. For any
16555 other window, set up appropriate value. */
16556 if (!EQ (window, selected_window))
16557 {
16558 struct text_pos pt;
16559
16560 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16561 if (CHARPOS (pt) < BEGV)
16562 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16563 else if (CHARPOS (pt) > (ZV - 1))
16564 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16565 else
16566 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16567 }
16568
16569 /* Display mode lines. */
16570 clear_glyph_matrix (w->desired_matrix);
16571 if (display_mode_lines (w))
16572 {
16573 ++nwindows;
16574 w->must_be_updated_p = 1;
16575 }
16576
16577 /* Restore old settings. */
16578 set_buffer_internal_1 (old);
16579 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16580 }
16581
16582 window = w->next;
16583 }
16584
16585 return nwindows;
16586 }
16587
16588
16589 /* Display the mode and/or top line of window W. Value is the number
16590 of mode lines displayed. */
16591
16592 static int
16593 display_mode_lines (w)
16594 struct window *w;
16595 {
16596 Lisp_Object old_selected_window, old_selected_frame;
16597 int n = 0;
16598
16599 old_selected_frame = selected_frame;
16600 selected_frame = w->frame;
16601 old_selected_window = selected_window;
16602 XSETWINDOW (selected_window, w);
16603
16604 /* These will be set while the mode line specs are processed. */
16605 line_number_displayed = 0;
16606 w->column_number_displayed = Qnil;
16607
16608 if (WINDOW_WANTS_MODELINE_P (w))
16609 {
16610 struct window *sel_w = XWINDOW (old_selected_window);
16611
16612 /* Select mode line face based on the real selected window. */
16613 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16614 current_buffer->mode_line_format);
16615 ++n;
16616 }
16617
16618 if (WINDOW_WANTS_HEADER_LINE_P (w))
16619 {
16620 display_mode_line (w, HEADER_LINE_FACE_ID,
16621 current_buffer->header_line_format);
16622 ++n;
16623 }
16624
16625 selected_frame = old_selected_frame;
16626 selected_window = old_selected_window;
16627 return n;
16628 }
16629
16630
16631 /* Display mode or top line of window W. FACE_ID specifies which line
16632 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16633 FORMAT is the mode line format to display. Value is the pixel
16634 height of the mode line displayed. */
16635
16636 static int
16637 display_mode_line (w, face_id, format)
16638 struct window *w;
16639 enum face_id face_id;
16640 Lisp_Object format;
16641 {
16642 struct it it;
16643 struct face *face;
16644 int count = SPECPDL_INDEX ();
16645
16646 init_iterator (&it, w, -1, -1, NULL, face_id);
16647 /* Don't extend on a previously drawn mode-line.
16648 This may happen if called from pos_visible_p. */
16649 it.glyph_row->enabled_p = 0;
16650 prepare_desired_row (it.glyph_row);
16651
16652 it.glyph_row->mode_line_p = 1;
16653
16654 if (! mode_line_inverse_video)
16655 /* Force the mode-line to be displayed in the default face. */
16656 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16657
16658 record_unwind_protect (unwind_format_mode_line,
16659 format_mode_line_unwind_data (NULL, 0));
16660
16661 mode_line_target = MODE_LINE_DISPLAY;
16662
16663 /* Temporarily make frame's keyboard the current kboard so that
16664 kboard-local variables in the mode_line_format will get the right
16665 values. */
16666 push_frame_kboard (it.f);
16667 record_unwind_save_match_data ();
16668 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16669 pop_frame_kboard ();
16670
16671 unbind_to (count, Qnil);
16672
16673 /* Fill up with spaces. */
16674 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16675
16676 compute_line_metrics (&it);
16677 it.glyph_row->full_width_p = 1;
16678 it.glyph_row->continued_p = 0;
16679 it.glyph_row->truncated_on_left_p = 0;
16680 it.glyph_row->truncated_on_right_p = 0;
16681
16682 /* Make a 3D mode-line have a shadow at its right end. */
16683 face = FACE_FROM_ID (it.f, face_id);
16684 extend_face_to_end_of_line (&it);
16685 if (face->box != FACE_NO_BOX)
16686 {
16687 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16688 + it.glyph_row->used[TEXT_AREA] - 1);
16689 last->right_box_line_p = 1;
16690 }
16691
16692 return it.glyph_row->height;
16693 }
16694
16695 /* Move element ELT in LIST to the front of LIST.
16696 Return the updated list. */
16697
16698 static Lisp_Object
16699 move_elt_to_front (elt, list)
16700 Lisp_Object elt, list;
16701 {
16702 register Lisp_Object tail, prev;
16703 register Lisp_Object tem;
16704
16705 tail = list;
16706 prev = Qnil;
16707 while (CONSP (tail))
16708 {
16709 tem = XCAR (tail);
16710
16711 if (EQ (elt, tem))
16712 {
16713 /* Splice out the link TAIL. */
16714 if (NILP (prev))
16715 list = XCDR (tail);
16716 else
16717 Fsetcdr (prev, XCDR (tail));
16718
16719 /* Now make it the first. */
16720 Fsetcdr (tail, list);
16721 return tail;
16722 }
16723 else
16724 prev = tail;
16725 tail = XCDR (tail);
16726 QUIT;
16727 }
16728
16729 /* Not found--return unchanged LIST. */
16730 return list;
16731 }
16732
16733 /* Contribute ELT to the mode line for window IT->w. How it
16734 translates into text depends on its data type.
16735
16736 IT describes the display environment in which we display, as usual.
16737
16738 DEPTH is the depth in recursion. It is used to prevent
16739 infinite recursion here.
16740
16741 FIELD_WIDTH is the number of characters the display of ELT should
16742 occupy in the mode line, and PRECISION is the maximum number of
16743 characters to display from ELT's representation. See
16744 display_string for details.
16745
16746 Returns the hpos of the end of the text generated by ELT.
16747
16748 PROPS is a property list to add to any string we encounter.
16749
16750 If RISKY is nonzero, remove (disregard) any properties in any string
16751 we encounter, and ignore :eval and :propertize.
16752
16753 The global variable `mode_line_target' determines whether the
16754 output is passed to `store_mode_line_noprop',
16755 `store_mode_line_string', or `display_string'. */
16756
16757 static int
16758 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16759 struct it *it;
16760 int depth;
16761 int field_width, precision;
16762 Lisp_Object elt, props;
16763 int risky;
16764 {
16765 int n = 0, field, prec;
16766 int literal = 0;
16767
16768 tail_recurse:
16769 if (depth > 100)
16770 elt = build_string ("*too-deep*");
16771
16772 depth++;
16773
16774 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16775 {
16776 case Lisp_String:
16777 {
16778 /* A string: output it and check for %-constructs within it. */
16779 unsigned char c;
16780 int offset = 0;
16781
16782 if (SCHARS (elt) > 0
16783 && (!NILP (props) || risky))
16784 {
16785 Lisp_Object oprops, aelt;
16786 oprops = Ftext_properties_at (make_number (0), elt);
16787
16788 /* If the starting string's properties are not what
16789 we want, translate the string. Also, if the string
16790 is risky, do that anyway. */
16791
16792 if (NILP (Fequal (props, oprops)) || risky)
16793 {
16794 /* If the starting string has properties,
16795 merge the specified ones onto the existing ones. */
16796 if (! NILP (oprops) && !risky)
16797 {
16798 Lisp_Object tem;
16799
16800 oprops = Fcopy_sequence (oprops);
16801 tem = props;
16802 while (CONSP (tem))
16803 {
16804 oprops = Fplist_put (oprops, XCAR (tem),
16805 XCAR (XCDR (tem)));
16806 tem = XCDR (XCDR (tem));
16807 }
16808 props = oprops;
16809 }
16810
16811 aelt = Fassoc (elt, mode_line_proptrans_alist);
16812 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16813 {
16814 /* AELT is what we want. Move it to the front
16815 without consing. */
16816 elt = XCAR (aelt);
16817 mode_line_proptrans_alist
16818 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16819 }
16820 else
16821 {
16822 Lisp_Object tem;
16823
16824 /* If AELT has the wrong props, it is useless.
16825 so get rid of it. */
16826 if (! NILP (aelt))
16827 mode_line_proptrans_alist
16828 = Fdelq (aelt, mode_line_proptrans_alist);
16829
16830 elt = Fcopy_sequence (elt);
16831 Fset_text_properties (make_number (0), Flength (elt),
16832 props, elt);
16833 /* Add this item to mode_line_proptrans_alist. */
16834 mode_line_proptrans_alist
16835 = Fcons (Fcons (elt, props),
16836 mode_line_proptrans_alist);
16837 /* Truncate mode_line_proptrans_alist
16838 to at most 50 elements. */
16839 tem = Fnthcdr (make_number (50),
16840 mode_line_proptrans_alist);
16841 if (! NILP (tem))
16842 XSETCDR (tem, Qnil);
16843 }
16844 }
16845 }
16846
16847 offset = 0;
16848
16849 if (literal)
16850 {
16851 prec = precision - n;
16852 switch (mode_line_target)
16853 {
16854 case MODE_LINE_NOPROP:
16855 case MODE_LINE_TITLE:
16856 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16857 break;
16858 case MODE_LINE_STRING:
16859 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16860 break;
16861 case MODE_LINE_DISPLAY:
16862 n += display_string (NULL, elt, Qnil, 0, 0, it,
16863 0, prec, 0, STRING_MULTIBYTE (elt));
16864 break;
16865 }
16866
16867 break;
16868 }
16869
16870 /* Handle the non-literal case. */
16871
16872 while ((precision <= 0 || n < precision)
16873 && SREF (elt, offset) != 0
16874 && (mode_line_target != MODE_LINE_DISPLAY
16875 || it->current_x < it->last_visible_x))
16876 {
16877 int last_offset = offset;
16878
16879 /* Advance to end of string or next format specifier. */
16880 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16881 ;
16882
16883 if (offset - 1 != last_offset)
16884 {
16885 int nchars, nbytes;
16886
16887 /* Output to end of string or up to '%'. Field width
16888 is length of string. Don't output more than
16889 PRECISION allows us. */
16890 offset--;
16891
16892 prec = c_string_width (SDATA (elt) + last_offset,
16893 offset - last_offset, precision - n,
16894 &nchars, &nbytes);
16895
16896 switch (mode_line_target)
16897 {
16898 case MODE_LINE_NOPROP:
16899 case MODE_LINE_TITLE:
16900 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16901 break;
16902 case MODE_LINE_STRING:
16903 {
16904 int bytepos = last_offset;
16905 int charpos = string_byte_to_char (elt, bytepos);
16906 int endpos = (precision <= 0
16907 ? string_byte_to_char (elt, offset)
16908 : charpos + nchars);
16909
16910 n += store_mode_line_string (NULL,
16911 Fsubstring (elt, make_number (charpos),
16912 make_number (endpos)),
16913 0, 0, 0, Qnil);
16914 }
16915 break;
16916 case MODE_LINE_DISPLAY:
16917 {
16918 int bytepos = last_offset;
16919 int charpos = string_byte_to_char (elt, bytepos);
16920
16921 if (precision <= 0)
16922 nchars = string_byte_to_char (elt, offset) - charpos;
16923 n += display_string (NULL, elt, Qnil, 0, charpos,
16924 it, 0, nchars, 0,
16925 STRING_MULTIBYTE (elt));
16926 }
16927 break;
16928 }
16929 }
16930 else /* c == '%' */
16931 {
16932 int percent_position = offset;
16933
16934 /* Get the specified minimum width. Zero means
16935 don't pad. */
16936 field = 0;
16937 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16938 field = field * 10 + c - '0';
16939
16940 /* Don't pad beyond the total padding allowed. */
16941 if (field_width - n > 0 && field > field_width - n)
16942 field = field_width - n;
16943
16944 /* Note that either PRECISION <= 0 or N < PRECISION. */
16945 prec = precision - n;
16946
16947 if (c == 'M')
16948 n += display_mode_element (it, depth, field, prec,
16949 Vglobal_mode_string, props,
16950 risky);
16951 else if (c != 0)
16952 {
16953 int multibyte;
16954 int bytepos, charpos;
16955 unsigned char *spec;
16956
16957 bytepos = percent_position;
16958 charpos = (STRING_MULTIBYTE (elt)
16959 ? string_byte_to_char (elt, bytepos)
16960 : bytepos);
16961
16962 spec
16963 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16964
16965 switch (mode_line_target)
16966 {
16967 case MODE_LINE_NOPROP:
16968 case MODE_LINE_TITLE:
16969 n += store_mode_line_noprop (spec, field, prec);
16970 break;
16971 case MODE_LINE_STRING:
16972 {
16973 int len = strlen (spec);
16974 Lisp_Object tem = make_string (spec, len);
16975 props = Ftext_properties_at (make_number (charpos), elt);
16976 /* Should only keep face property in props */
16977 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16978 }
16979 break;
16980 case MODE_LINE_DISPLAY:
16981 {
16982 int nglyphs_before, nwritten;
16983
16984 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16985 nwritten = display_string (spec, Qnil, elt,
16986 charpos, 0, it,
16987 field, prec, 0,
16988 multibyte);
16989
16990 /* Assign to the glyphs written above the
16991 string where the `%x' came from, position
16992 of the `%'. */
16993 if (nwritten > 0)
16994 {
16995 struct glyph *glyph
16996 = (it->glyph_row->glyphs[TEXT_AREA]
16997 + nglyphs_before);
16998 int i;
16999
17000 for (i = 0; i < nwritten; ++i)
17001 {
17002 glyph[i].object = elt;
17003 glyph[i].charpos = charpos;
17004 }
17005
17006 n += nwritten;
17007 }
17008 }
17009 break;
17010 }
17011 }
17012 else /* c == 0 */
17013 break;
17014 }
17015 }
17016 }
17017 break;
17018
17019 case Lisp_Symbol:
17020 /* A symbol: process the value of the symbol recursively
17021 as if it appeared here directly. Avoid error if symbol void.
17022 Special case: if value of symbol is a string, output the string
17023 literally. */
17024 {
17025 register Lisp_Object tem;
17026
17027 /* If the variable is not marked as risky to set
17028 then its contents are risky to use. */
17029 if (NILP (Fget (elt, Qrisky_local_variable)))
17030 risky = 1;
17031
17032 tem = Fboundp (elt);
17033 if (!NILP (tem))
17034 {
17035 tem = Fsymbol_value (elt);
17036 /* If value is a string, output that string literally:
17037 don't check for % within it. */
17038 if (STRINGP (tem))
17039 literal = 1;
17040
17041 if (!EQ (tem, elt))
17042 {
17043 /* Give up right away for nil or t. */
17044 elt = tem;
17045 goto tail_recurse;
17046 }
17047 }
17048 }
17049 break;
17050
17051 case Lisp_Cons:
17052 {
17053 register Lisp_Object car, tem;
17054
17055 /* A cons cell: five distinct cases.
17056 If first element is :eval or :propertize, do something special.
17057 If first element is a string or a cons, process all the elements
17058 and effectively concatenate them.
17059 If first element is a negative number, truncate displaying cdr to
17060 at most that many characters. If positive, pad (with spaces)
17061 to at least that many characters.
17062 If first element is a symbol, process the cadr or caddr recursively
17063 according to whether the symbol's value is non-nil or nil. */
17064 car = XCAR (elt);
17065 if (EQ (car, QCeval))
17066 {
17067 /* An element of the form (:eval FORM) means evaluate FORM
17068 and use the result as mode line elements. */
17069
17070 if (risky)
17071 break;
17072
17073 if (CONSP (XCDR (elt)))
17074 {
17075 Lisp_Object spec;
17076 spec = safe_eval (XCAR (XCDR (elt)));
17077 n += display_mode_element (it, depth, field_width - n,
17078 precision - n, spec, props,
17079 risky);
17080 }
17081 }
17082 else if (EQ (car, QCpropertize))
17083 {
17084 /* An element of the form (:propertize ELT PROPS...)
17085 means display ELT but applying properties PROPS. */
17086
17087 if (risky)
17088 break;
17089
17090 if (CONSP (XCDR (elt)))
17091 n += display_mode_element (it, depth, field_width - n,
17092 precision - n, XCAR (XCDR (elt)),
17093 XCDR (XCDR (elt)), risky);
17094 }
17095 else if (SYMBOLP (car))
17096 {
17097 tem = Fboundp (car);
17098 elt = XCDR (elt);
17099 if (!CONSP (elt))
17100 goto invalid;
17101 /* elt is now the cdr, and we know it is a cons cell.
17102 Use its car if CAR has a non-nil value. */
17103 if (!NILP (tem))
17104 {
17105 tem = Fsymbol_value (car);
17106 if (!NILP (tem))
17107 {
17108 elt = XCAR (elt);
17109 goto tail_recurse;
17110 }
17111 }
17112 /* Symbol's value is nil (or symbol is unbound)
17113 Get the cddr of the original list
17114 and if possible find the caddr and use that. */
17115 elt = XCDR (elt);
17116 if (NILP (elt))
17117 break;
17118 else if (!CONSP (elt))
17119 goto invalid;
17120 elt = XCAR (elt);
17121 goto tail_recurse;
17122 }
17123 else if (INTEGERP (car))
17124 {
17125 register int lim = XINT (car);
17126 elt = XCDR (elt);
17127 if (lim < 0)
17128 {
17129 /* Negative int means reduce maximum width. */
17130 if (precision <= 0)
17131 precision = -lim;
17132 else
17133 precision = min (precision, -lim);
17134 }
17135 else if (lim > 0)
17136 {
17137 /* Padding specified. Don't let it be more than
17138 current maximum. */
17139 if (precision > 0)
17140 lim = min (precision, lim);
17141
17142 /* If that's more padding than already wanted, queue it.
17143 But don't reduce padding already specified even if
17144 that is beyond the current truncation point. */
17145 field_width = max (lim, field_width);
17146 }
17147 goto tail_recurse;
17148 }
17149 else if (STRINGP (car) || CONSP (car))
17150 {
17151 register int limit = 50;
17152 /* Limit is to protect against circular lists. */
17153 while (CONSP (elt)
17154 && --limit > 0
17155 && (precision <= 0 || n < precision))
17156 {
17157 n += display_mode_element (it, depth,
17158 /* Do padding only after the last
17159 element in the list. */
17160 (! CONSP (XCDR (elt))
17161 ? field_width - n
17162 : 0),
17163 precision - n, XCAR (elt),
17164 props, risky);
17165 elt = XCDR (elt);
17166 }
17167 }
17168 }
17169 break;
17170
17171 default:
17172 invalid:
17173 elt = build_string ("*invalid*");
17174 goto tail_recurse;
17175 }
17176
17177 /* Pad to FIELD_WIDTH. */
17178 if (field_width > 0 && n < field_width)
17179 {
17180 switch (mode_line_target)
17181 {
17182 case MODE_LINE_NOPROP:
17183 case MODE_LINE_TITLE:
17184 n += store_mode_line_noprop ("", field_width - n, 0);
17185 break;
17186 case MODE_LINE_STRING:
17187 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17188 break;
17189 case MODE_LINE_DISPLAY:
17190 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17191 0, 0, 0);
17192 break;
17193 }
17194 }
17195
17196 return n;
17197 }
17198
17199 /* Store a mode-line string element in mode_line_string_list.
17200
17201 If STRING is non-null, display that C string. Otherwise, the Lisp
17202 string LISP_STRING is displayed.
17203
17204 FIELD_WIDTH is the minimum number of output glyphs to produce.
17205 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17206 with spaces. FIELD_WIDTH <= 0 means don't pad.
17207
17208 PRECISION is the maximum number of characters to output from
17209 STRING. PRECISION <= 0 means don't truncate the string.
17210
17211 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17212 properties to the string.
17213
17214 PROPS are the properties to add to the string.
17215 The mode_line_string_face face property is always added to the string.
17216 */
17217
17218 static int
17219 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17220 char *string;
17221 Lisp_Object lisp_string;
17222 int copy_string;
17223 int field_width;
17224 int precision;
17225 Lisp_Object props;
17226 {
17227 int len;
17228 int n = 0;
17229
17230 if (string != NULL)
17231 {
17232 len = strlen (string);
17233 if (precision > 0 && len > precision)
17234 len = precision;
17235 lisp_string = make_string (string, len);
17236 if (NILP (props))
17237 props = mode_line_string_face_prop;
17238 else if (!NILP (mode_line_string_face))
17239 {
17240 Lisp_Object face = Fplist_get (props, Qface);
17241 props = Fcopy_sequence (props);
17242 if (NILP (face))
17243 face = mode_line_string_face;
17244 else
17245 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17246 props = Fplist_put (props, Qface, face);
17247 }
17248 Fadd_text_properties (make_number (0), make_number (len),
17249 props, lisp_string);
17250 }
17251 else
17252 {
17253 len = XFASTINT (Flength (lisp_string));
17254 if (precision > 0 && len > precision)
17255 {
17256 len = precision;
17257 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17258 precision = -1;
17259 }
17260 if (!NILP (mode_line_string_face))
17261 {
17262 Lisp_Object face;
17263 if (NILP (props))
17264 props = Ftext_properties_at (make_number (0), lisp_string);
17265 face = Fplist_get (props, Qface);
17266 if (NILP (face))
17267 face = mode_line_string_face;
17268 else
17269 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17270 props = Fcons (Qface, Fcons (face, Qnil));
17271 if (copy_string)
17272 lisp_string = Fcopy_sequence (lisp_string);
17273 }
17274 if (!NILP (props))
17275 Fadd_text_properties (make_number (0), make_number (len),
17276 props, lisp_string);
17277 }
17278
17279 if (len > 0)
17280 {
17281 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17282 n += len;
17283 }
17284
17285 if (field_width > len)
17286 {
17287 field_width -= len;
17288 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17289 if (!NILP (props))
17290 Fadd_text_properties (make_number (0), make_number (field_width),
17291 props, lisp_string);
17292 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17293 n += field_width;
17294 }
17295
17296 return n;
17297 }
17298
17299
17300 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17301 1, 4, 0,
17302 doc: /* Format a string out of a mode line format specification.
17303 First arg FORMAT specifies the mode line format (see `mode-line-format'
17304 for details) to use.
17305
17306 Optional second arg FACE specifies the face property to put
17307 on all characters for which no face is specified.
17308 The value t means whatever face the window's mode line currently uses
17309 \(either `mode-line' or `mode-line-inactive', depending).
17310 A value of nil means the default is no face property.
17311 If FACE is an integer, the value string has no text properties.
17312
17313 Optional third and fourth args WINDOW and BUFFER specify the window
17314 and buffer to use as the context for the formatting (defaults
17315 are the selected window and the window's buffer). */)
17316 (format, face, window, buffer)
17317 Lisp_Object format, face, window, buffer;
17318 {
17319 struct it it;
17320 int len;
17321 struct window *w;
17322 struct buffer *old_buffer = NULL;
17323 int face_id = -1;
17324 int no_props = INTEGERP (face);
17325 int count = SPECPDL_INDEX ();
17326 Lisp_Object str;
17327 int string_start = 0;
17328
17329 if (NILP (window))
17330 window = selected_window;
17331 CHECK_WINDOW (window);
17332 w = XWINDOW (window);
17333
17334 if (NILP (buffer))
17335 buffer = w->buffer;
17336 CHECK_BUFFER (buffer);
17337
17338 if (NILP (format))
17339 return build_string ("");
17340
17341 if (no_props)
17342 face = Qnil;
17343
17344 if (!NILP (face))
17345 {
17346 if (EQ (face, Qt))
17347 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17348 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17349 }
17350
17351 if (face_id < 0)
17352 face_id = DEFAULT_FACE_ID;
17353
17354 if (XBUFFER (buffer) != current_buffer)
17355 old_buffer = current_buffer;
17356
17357 /* Save things including mode_line_proptrans_alist,
17358 and set that to nil so that we don't alter the outer value. */
17359 record_unwind_protect (unwind_format_mode_line,
17360 format_mode_line_unwind_data (old_buffer, 1));
17361 mode_line_proptrans_alist = Qnil;
17362
17363 if (old_buffer)
17364 set_buffer_internal_1 (XBUFFER (buffer));
17365
17366 init_iterator (&it, w, -1, -1, NULL, face_id);
17367
17368 if (no_props)
17369 {
17370 mode_line_target = MODE_LINE_NOPROP;
17371 mode_line_string_face_prop = Qnil;
17372 mode_line_string_list = Qnil;
17373 string_start = MODE_LINE_NOPROP_LEN (0);
17374 }
17375 else
17376 {
17377 mode_line_target = MODE_LINE_STRING;
17378 mode_line_string_list = Qnil;
17379 mode_line_string_face = face;
17380 mode_line_string_face_prop
17381 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17382 }
17383
17384 push_frame_kboard (it.f);
17385 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17386 pop_frame_kboard ();
17387
17388 if (no_props)
17389 {
17390 len = MODE_LINE_NOPROP_LEN (string_start);
17391 str = make_string (mode_line_noprop_buf + string_start, len);
17392 }
17393 else
17394 {
17395 mode_line_string_list = Fnreverse (mode_line_string_list);
17396 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17397 make_string ("", 0));
17398 }
17399
17400 unbind_to (count, Qnil);
17401 return str;
17402 }
17403
17404 /* Write a null-terminated, right justified decimal representation of
17405 the positive integer D to BUF using a minimal field width WIDTH. */
17406
17407 static void
17408 pint2str (buf, width, d)
17409 register char *buf;
17410 register int width;
17411 register int d;
17412 {
17413 register char *p = buf;
17414
17415 if (d <= 0)
17416 *p++ = '0';
17417 else
17418 {
17419 while (d > 0)
17420 {
17421 *p++ = d % 10 + '0';
17422 d /= 10;
17423 }
17424 }
17425
17426 for (width -= (int) (p - buf); width > 0; --width)
17427 *p++ = ' ';
17428 *p-- = '\0';
17429 while (p > buf)
17430 {
17431 d = *buf;
17432 *buf++ = *p;
17433 *p-- = d;
17434 }
17435 }
17436
17437 /* Write a null-terminated, right justified decimal and "human
17438 readable" representation of the nonnegative integer D to BUF using
17439 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17440
17441 static const char power_letter[] =
17442 {
17443 0, /* not used */
17444 'k', /* kilo */
17445 'M', /* mega */
17446 'G', /* giga */
17447 'T', /* tera */
17448 'P', /* peta */
17449 'E', /* exa */
17450 'Z', /* zetta */
17451 'Y' /* yotta */
17452 };
17453
17454 static void
17455 pint2hrstr (buf, width, d)
17456 char *buf;
17457 int width;
17458 int d;
17459 {
17460 /* We aim to represent the nonnegative integer D as
17461 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17462 int quotient = d;
17463 int remainder = 0;
17464 /* -1 means: do not use TENTHS. */
17465 int tenths = -1;
17466 int exponent = 0;
17467
17468 /* Length of QUOTIENT.TENTHS as a string. */
17469 int length;
17470
17471 char * psuffix;
17472 char * p;
17473
17474 if (1000 <= quotient)
17475 {
17476 /* Scale to the appropriate EXPONENT. */
17477 do
17478 {
17479 remainder = quotient % 1000;
17480 quotient /= 1000;
17481 exponent++;
17482 }
17483 while (1000 <= quotient);
17484
17485 /* Round to nearest and decide whether to use TENTHS or not. */
17486 if (quotient <= 9)
17487 {
17488 tenths = remainder / 100;
17489 if (50 <= remainder % 100)
17490 {
17491 if (tenths < 9)
17492 tenths++;
17493 else
17494 {
17495 quotient++;
17496 if (quotient == 10)
17497 tenths = -1;
17498 else
17499 tenths = 0;
17500 }
17501 }
17502 }
17503 else
17504 if (500 <= remainder)
17505 {
17506 if (quotient < 999)
17507 quotient++;
17508 else
17509 {
17510 quotient = 1;
17511 exponent++;
17512 tenths = 0;
17513 }
17514 }
17515 }
17516
17517 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17518 if (tenths == -1 && quotient <= 99)
17519 if (quotient <= 9)
17520 length = 1;
17521 else
17522 length = 2;
17523 else
17524 length = 3;
17525 p = psuffix = buf + max (width, length);
17526
17527 /* Print EXPONENT. */
17528 if (exponent)
17529 *psuffix++ = power_letter[exponent];
17530 *psuffix = '\0';
17531
17532 /* Print TENTHS. */
17533 if (tenths >= 0)
17534 {
17535 *--p = '0' + tenths;
17536 *--p = '.';
17537 }
17538
17539 /* Print QUOTIENT. */
17540 do
17541 {
17542 int digit = quotient % 10;
17543 *--p = '0' + digit;
17544 }
17545 while ((quotient /= 10) != 0);
17546
17547 /* Print leading spaces. */
17548 while (buf < p)
17549 *--p = ' ';
17550 }
17551
17552 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17553 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17554 type of CODING_SYSTEM. Return updated pointer into BUF. */
17555
17556 static unsigned char invalid_eol_type[] = "(*invalid*)";
17557
17558 static char *
17559 decode_mode_spec_coding (coding_system, buf, eol_flag)
17560 Lisp_Object coding_system;
17561 register char *buf;
17562 int eol_flag;
17563 {
17564 Lisp_Object val;
17565 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17566 const unsigned char *eol_str;
17567 int eol_str_len;
17568 /* The EOL conversion we are using. */
17569 Lisp_Object eoltype;
17570
17571 val = Fget (coding_system, Qcoding_system);
17572 eoltype = Qnil;
17573
17574 if (!VECTORP (val)) /* Not yet decided. */
17575 {
17576 if (multibyte)
17577 *buf++ = '-';
17578 if (eol_flag)
17579 eoltype = eol_mnemonic_undecided;
17580 /* Don't mention EOL conversion if it isn't decided. */
17581 }
17582 else
17583 {
17584 Lisp_Object eolvalue;
17585
17586 eolvalue = Fget (coding_system, Qeol_type);
17587
17588 if (multibyte)
17589 *buf++ = XFASTINT (AREF (val, 1));
17590
17591 if (eol_flag)
17592 {
17593 /* The EOL conversion that is normal on this system. */
17594
17595 if (NILP (eolvalue)) /* Not yet decided. */
17596 eoltype = eol_mnemonic_undecided;
17597 else if (VECTORP (eolvalue)) /* Not yet decided. */
17598 eoltype = eol_mnemonic_undecided;
17599 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17600 eoltype = (XFASTINT (eolvalue) == 0
17601 ? eol_mnemonic_unix
17602 : (XFASTINT (eolvalue) == 1
17603 ? eol_mnemonic_dos : eol_mnemonic_mac));
17604 }
17605 }
17606
17607 if (eol_flag)
17608 {
17609 /* Mention the EOL conversion if it is not the usual one. */
17610 if (STRINGP (eoltype))
17611 {
17612 eol_str = SDATA (eoltype);
17613 eol_str_len = SBYTES (eoltype);
17614 }
17615 else if (INTEGERP (eoltype)
17616 && CHAR_VALID_P (XINT (eoltype), 0))
17617 {
17618 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17619 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17620 eol_str = tmp;
17621 }
17622 else
17623 {
17624 eol_str = invalid_eol_type;
17625 eol_str_len = sizeof (invalid_eol_type) - 1;
17626 }
17627 bcopy (eol_str, buf, eol_str_len);
17628 buf += eol_str_len;
17629 }
17630
17631 return buf;
17632 }
17633
17634 /* Return a string for the output of a mode line %-spec for window W,
17635 generated by character C. PRECISION >= 0 means don't return a
17636 string longer than that value. FIELD_WIDTH > 0 means pad the
17637 string returned with spaces to that value. Return 1 in *MULTIBYTE
17638 if the result is multibyte text.
17639
17640 Note we operate on the current buffer for most purposes,
17641 the exception being w->base_line_pos. */
17642
17643 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17644
17645 static char *
17646 decode_mode_spec (w, c, field_width, precision, multibyte)
17647 struct window *w;
17648 register int c;
17649 int field_width, precision;
17650 int *multibyte;
17651 {
17652 Lisp_Object obj;
17653 struct frame *f = XFRAME (WINDOW_FRAME (w));
17654 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17655 struct buffer *b = current_buffer;
17656
17657 obj = Qnil;
17658 *multibyte = 0;
17659
17660 switch (c)
17661 {
17662 case '*':
17663 if (!NILP (b->read_only))
17664 return "%";
17665 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17666 return "*";
17667 return "-";
17668
17669 case '+':
17670 /* This differs from %* only for a modified read-only buffer. */
17671 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17672 return "*";
17673 if (!NILP (b->read_only))
17674 return "%";
17675 return "-";
17676
17677 case '&':
17678 /* This differs from %* in ignoring read-only-ness. */
17679 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17680 return "*";
17681 return "-";
17682
17683 case '%':
17684 return "%";
17685
17686 case '[':
17687 {
17688 int i;
17689 char *p;
17690
17691 if (command_loop_level > 5)
17692 return "[[[... ";
17693 p = decode_mode_spec_buf;
17694 for (i = 0; i < command_loop_level; i++)
17695 *p++ = '[';
17696 *p = 0;
17697 return decode_mode_spec_buf;
17698 }
17699
17700 case ']':
17701 {
17702 int i;
17703 char *p;
17704
17705 if (command_loop_level > 5)
17706 return " ...]]]";
17707 p = decode_mode_spec_buf;
17708 for (i = 0; i < command_loop_level; i++)
17709 *p++ = ']';
17710 *p = 0;
17711 return decode_mode_spec_buf;
17712 }
17713
17714 case '-':
17715 {
17716 register int i;
17717
17718 /* Let lots_of_dashes be a string of infinite length. */
17719 if (mode_line_target == MODE_LINE_NOPROP ||
17720 mode_line_target == MODE_LINE_STRING)
17721 return "--";
17722 if (field_width <= 0
17723 || field_width > sizeof (lots_of_dashes))
17724 {
17725 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17726 decode_mode_spec_buf[i] = '-';
17727 decode_mode_spec_buf[i] = '\0';
17728 return decode_mode_spec_buf;
17729 }
17730 else
17731 return lots_of_dashes;
17732 }
17733
17734 case 'b':
17735 obj = b->name;
17736 break;
17737
17738 case 'c':
17739 /* %c and %l are ignored in `frame-title-format'.
17740 (In redisplay_internal, the frame title is drawn _before_ the
17741 windows are updated, so the stuff which depends on actual
17742 window contents (such as %l) may fail to render properly, or
17743 even crash emacs.) */
17744 if (mode_line_target == MODE_LINE_TITLE)
17745 return "";
17746 else
17747 {
17748 int col = (int) current_column (); /* iftc */
17749 w->column_number_displayed = make_number (col);
17750 pint2str (decode_mode_spec_buf, field_width, col);
17751 return decode_mode_spec_buf;
17752 }
17753
17754 case 'e':
17755 #ifndef SYSTEM_MALLOC
17756 {
17757 if (NILP (Vmemory_full))
17758 return "";
17759 else
17760 return "!MEM FULL! ";
17761 }
17762 #else
17763 return "";
17764 #endif
17765
17766 case 'F':
17767 /* %F displays the frame name. */
17768 if (!NILP (f->title))
17769 return (char *) SDATA (f->title);
17770 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17771 return (char *) SDATA (f->name);
17772 return "Emacs";
17773
17774 case 'f':
17775 obj = b->filename;
17776 break;
17777
17778 case 'i':
17779 {
17780 int size = ZV - BEGV;
17781 pint2str (decode_mode_spec_buf, field_width, size);
17782 return decode_mode_spec_buf;
17783 }
17784
17785 case 'I':
17786 {
17787 int size = ZV - BEGV;
17788 pint2hrstr (decode_mode_spec_buf, field_width, size);
17789 return decode_mode_spec_buf;
17790 }
17791
17792 case 'l':
17793 {
17794 int startpos, startpos_byte, line, linepos, linepos_byte;
17795 int topline, nlines, junk, height;
17796
17797 /* %c and %l are ignored in `frame-title-format'. */
17798 if (mode_line_target == MODE_LINE_TITLE)
17799 return "";
17800
17801 startpos = XMARKER (w->start)->charpos;
17802 startpos_byte = marker_byte_position (w->start);
17803 height = WINDOW_TOTAL_LINES (w);
17804
17805 /* If we decided that this buffer isn't suitable for line numbers,
17806 don't forget that too fast. */
17807 if (EQ (w->base_line_pos, w->buffer))
17808 goto no_value;
17809 /* But do forget it, if the window shows a different buffer now. */
17810 else if (BUFFERP (w->base_line_pos))
17811 w->base_line_pos = Qnil;
17812
17813 /* If the buffer is very big, don't waste time. */
17814 if (INTEGERP (Vline_number_display_limit)
17815 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17816 {
17817 w->base_line_pos = Qnil;
17818 w->base_line_number = Qnil;
17819 goto no_value;
17820 }
17821
17822 if (!NILP (w->base_line_number)
17823 && !NILP (w->base_line_pos)
17824 && XFASTINT (w->base_line_pos) <= startpos)
17825 {
17826 line = XFASTINT (w->base_line_number);
17827 linepos = XFASTINT (w->base_line_pos);
17828 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17829 }
17830 else
17831 {
17832 line = 1;
17833 linepos = BUF_BEGV (b);
17834 linepos_byte = BUF_BEGV_BYTE (b);
17835 }
17836
17837 /* Count lines from base line to window start position. */
17838 nlines = display_count_lines (linepos, linepos_byte,
17839 startpos_byte,
17840 startpos, &junk);
17841
17842 topline = nlines + line;
17843
17844 /* Determine a new base line, if the old one is too close
17845 or too far away, or if we did not have one.
17846 "Too close" means it's plausible a scroll-down would
17847 go back past it. */
17848 if (startpos == BUF_BEGV (b))
17849 {
17850 w->base_line_number = make_number (topline);
17851 w->base_line_pos = make_number (BUF_BEGV (b));
17852 }
17853 else if (nlines < height + 25 || nlines > height * 3 + 50
17854 || linepos == BUF_BEGV (b))
17855 {
17856 int limit = BUF_BEGV (b);
17857 int limit_byte = BUF_BEGV_BYTE (b);
17858 int position;
17859 int distance = (height * 2 + 30) * line_number_display_limit_width;
17860
17861 if (startpos - distance > limit)
17862 {
17863 limit = startpos - distance;
17864 limit_byte = CHAR_TO_BYTE (limit);
17865 }
17866
17867 nlines = display_count_lines (startpos, startpos_byte,
17868 limit_byte,
17869 - (height * 2 + 30),
17870 &position);
17871 /* If we couldn't find the lines we wanted within
17872 line_number_display_limit_width chars per line,
17873 give up on line numbers for this window. */
17874 if (position == limit_byte && limit == startpos - distance)
17875 {
17876 w->base_line_pos = w->buffer;
17877 w->base_line_number = Qnil;
17878 goto no_value;
17879 }
17880
17881 w->base_line_number = make_number (topline - nlines);
17882 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17883 }
17884
17885 /* Now count lines from the start pos to point. */
17886 nlines = display_count_lines (startpos, startpos_byte,
17887 PT_BYTE, PT, &junk);
17888
17889 /* Record that we did display the line number. */
17890 line_number_displayed = 1;
17891
17892 /* Make the string to show. */
17893 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17894 return decode_mode_spec_buf;
17895 no_value:
17896 {
17897 char* p = decode_mode_spec_buf;
17898 int pad = field_width - 2;
17899 while (pad-- > 0)
17900 *p++ = ' ';
17901 *p++ = '?';
17902 *p++ = '?';
17903 *p = '\0';
17904 return decode_mode_spec_buf;
17905 }
17906 }
17907 break;
17908
17909 case 'm':
17910 obj = b->mode_name;
17911 break;
17912
17913 case 'n':
17914 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17915 return " Narrow";
17916 break;
17917
17918 case 'p':
17919 {
17920 int pos = marker_position (w->start);
17921 int total = BUF_ZV (b) - BUF_BEGV (b);
17922
17923 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17924 {
17925 if (pos <= BUF_BEGV (b))
17926 return "All";
17927 else
17928 return "Bottom";
17929 }
17930 else if (pos <= BUF_BEGV (b))
17931 return "Top";
17932 else
17933 {
17934 if (total > 1000000)
17935 /* Do it differently for a large value, to avoid overflow. */
17936 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17937 else
17938 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17939 /* We can't normally display a 3-digit number,
17940 so get us a 2-digit number that is close. */
17941 if (total == 100)
17942 total = 99;
17943 sprintf (decode_mode_spec_buf, "%2d%%", total);
17944 return decode_mode_spec_buf;
17945 }
17946 }
17947
17948 /* Display percentage of size above the bottom of the screen. */
17949 case 'P':
17950 {
17951 int toppos = marker_position (w->start);
17952 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17953 int total = BUF_ZV (b) - BUF_BEGV (b);
17954
17955 if (botpos >= BUF_ZV (b))
17956 {
17957 if (toppos <= BUF_BEGV (b))
17958 return "All";
17959 else
17960 return "Bottom";
17961 }
17962 else
17963 {
17964 if (total > 1000000)
17965 /* Do it differently for a large value, to avoid overflow. */
17966 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17967 else
17968 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17969 /* We can't normally display a 3-digit number,
17970 so get us a 2-digit number that is close. */
17971 if (total == 100)
17972 total = 99;
17973 if (toppos <= BUF_BEGV (b))
17974 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17975 else
17976 sprintf (decode_mode_spec_buf, "%2d%%", total);
17977 return decode_mode_spec_buf;
17978 }
17979 }
17980
17981 case 's':
17982 /* status of process */
17983 obj = Fget_buffer_process (Fcurrent_buffer ());
17984 if (NILP (obj))
17985 return "no process";
17986 #ifdef subprocesses
17987 obj = Fsymbol_name (Fprocess_status (obj));
17988 #endif
17989 break;
17990
17991 case 't': /* indicate TEXT or BINARY */
17992 #ifdef MODE_LINE_BINARY_TEXT
17993 return MODE_LINE_BINARY_TEXT (b);
17994 #else
17995 return "T";
17996 #endif
17997
17998 case 'z':
17999 /* coding-system (not including end-of-line format) */
18000 case 'Z':
18001 /* coding-system (including end-of-line type) */
18002 {
18003 int eol_flag = (c == 'Z');
18004 char *p = decode_mode_spec_buf;
18005
18006 if (! FRAME_WINDOW_P (f))
18007 {
18008 /* No need to mention EOL here--the terminal never needs
18009 to do EOL conversion. */
18010 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18011 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18012 }
18013 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18014 p, eol_flag);
18015
18016 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18017 #ifdef subprocesses
18018 obj = Fget_buffer_process (Fcurrent_buffer ());
18019 if (PROCESSP (obj))
18020 {
18021 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18022 p, eol_flag);
18023 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18024 p, eol_flag);
18025 }
18026 #endif /* subprocesses */
18027 #endif /* 0 */
18028 *p = 0;
18029 return decode_mode_spec_buf;
18030 }
18031 }
18032
18033 if (STRINGP (obj))
18034 {
18035 *multibyte = STRING_MULTIBYTE (obj);
18036 return (char *) SDATA (obj);
18037 }
18038 else
18039 return "";
18040 }
18041
18042
18043 /* Count up to COUNT lines starting from START / START_BYTE.
18044 But don't go beyond LIMIT_BYTE.
18045 Return the number of lines thus found (always nonnegative).
18046
18047 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18048
18049 static int
18050 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18051 int start, start_byte, limit_byte, count;
18052 int *byte_pos_ptr;
18053 {
18054 register unsigned char *cursor;
18055 unsigned char *base;
18056
18057 register int ceiling;
18058 register unsigned char *ceiling_addr;
18059 int orig_count = count;
18060
18061 /* If we are not in selective display mode,
18062 check only for newlines. */
18063 int selective_display = (!NILP (current_buffer->selective_display)
18064 && !INTEGERP (current_buffer->selective_display));
18065
18066 if (count > 0)
18067 {
18068 while (start_byte < limit_byte)
18069 {
18070 ceiling = BUFFER_CEILING_OF (start_byte);
18071 ceiling = min (limit_byte - 1, ceiling);
18072 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18073 base = (cursor = BYTE_POS_ADDR (start_byte));
18074 while (1)
18075 {
18076 if (selective_display)
18077 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18078 ;
18079 else
18080 while (*cursor != '\n' && ++cursor != ceiling_addr)
18081 ;
18082
18083 if (cursor != ceiling_addr)
18084 {
18085 if (--count == 0)
18086 {
18087 start_byte += cursor - base + 1;
18088 *byte_pos_ptr = start_byte;
18089 return orig_count;
18090 }
18091 else
18092 if (++cursor == ceiling_addr)
18093 break;
18094 }
18095 else
18096 break;
18097 }
18098 start_byte += cursor - base;
18099 }
18100 }
18101 else
18102 {
18103 while (start_byte > limit_byte)
18104 {
18105 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18106 ceiling = max (limit_byte, ceiling);
18107 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18108 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18109 while (1)
18110 {
18111 if (selective_display)
18112 while (--cursor != ceiling_addr
18113 && *cursor != '\n' && *cursor != 015)
18114 ;
18115 else
18116 while (--cursor != ceiling_addr && *cursor != '\n')
18117 ;
18118
18119 if (cursor != ceiling_addr)
18120 {
18121 if (++count == 0)
18122 {
18123 start_byte += cursor - base + 1;
18124 *byte_pos_ptr = start_byte;
18125 /* When scanning backwards, we should
18126 not count the newline posterior to which we stop. */
18127 return - orig_count - 1;
18128 }
18129 }
18130 else
18131 break;
18132 }
18133 /* Here we add 1 to compensate for the last decrement
18134 of CURSOR, which took it past the valid range. */
18135 start_byte += cursor - base + 1;
18136 }
18137 }
18138
18139 *byte_pos_ptr = limit_byte;
18140
18141 if (count < 0)
18142 return - orig_count + count;
18143 return orig_count - count;
18144
18145 }
18146
18147
18148 \f
18149 /***********************************************************************
18150 Displaying strings
18151 ***********************************************************************/
18152
18153 /* Display a NUL-terminated string, starting with index START.
18154
18155 If STRING is non-null, display that C string. Otherwise, the Lisp
18156 string LISP_STRING is displayed.
18157
18158 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18159 FACE_STRING. Display STRING or LISP_STRING with the face at
18160 FACE_STRING_POS in FACE_STRING:
18161
18162 Display the string in the environment given by IT, but use the
18163 standard display table, temporarily.
18164
18165 FIELD_WIDTH is the minimum number of output glyphs to produce.
18166 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18167 with spaces. If STRING has more characters, more than FIELD_WIDTH
18168 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18169
18170 PRECISION is the maximum number of characters to output from
18171 STRING. PRECISION < 0 means don't truncate the string.
18172
18173 This is roughly equivalent to printf format specifiers:
18174
18175 FIELD_WIDTH PRECISION PRINTF
18176 ----------------------------------------
18177 -1 -1 %s
18178 -1 10 %.10s
18179 10 -1 %10s
18180 20 10 %20.10s
18181
18182 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18183 display them, and < 0 means obey the current buffer's value of
18184 enable_multibyte_characters.
18185
18186 Value is the number of columns displayed. */
18187
18188 static int
18189 display_string (string, lisp_string, face_string, face_string_pos,
18190 start, it, field_width, precision, max_x, multibyte)
18191 unsigned char *string;
18192 Lisp_Object lisp_string;
18193 Lisp_Object face_string;
18194 int face_string_pos;
18195 int start;
18196 struct it *it;
18197 int field_width, precision, max_x;
18198 int multibyte;
18199 {
18200 int hpos_at_start = it->hpos;
18201 int saved_face_id = it->face_id;
18202 struct glyph_row *row = it->glyph_row;
18203
18204 /* Initialize the iterator IT for iteration over STRING beginning
18205 with index START. */
18206 reseat_to_string (it, string, lisp_string, start,
18207 precision, field_width, multibyte);
18208
18209 /* If displaying STRING, set up the face of the iterator
18210 from LISP_STRING, if that's given. */
18211 if (STRINGP (face_string))
18212 {
18213 int endptr;
18214 struct face *face;
18215
18216 it->face_id
18217 = face_at_string_position (it->w, face_string, face_string_pos,
18218 0, it->region_beg_charpos,
18219 it->region_end_charpos,
18220 &endptr, it->base_face_id, 0);
18221 face = FACE_FROM_ID (it->f, it->face_id);
18222 it->face_box_p = face->box != FACE_NO_BOX;
18223 }
18224
18225 /* Set max_x to the maximum allowed X position. Don't let it go
18226 beyond the right edge of the window. */
18227 if (max_x <= 0)
18228 max_x = it->last_visible_x;
18229 else
18230 max_x = min (max_x, it->last_visible_x);
18231
18232 /* Skip over display elements that are not visible. because IT->w is
18233 hscrolled. */
18234 if (it->current_x < it->first_visible_x)
18235 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18236 MOVE_TO_POS | MOVE_TO_X);
18237
18238 row->ascent = it->max_ascent;
18239 row->height = it->max_ascent + it->max_descent;
18240 row->phys_ascent = it->max_phys_ascent;
18241 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18242 row->extra_line_spacing = it->max_extra_line_spacing;
18243
18244 /* This condition is for the case that we are called with current_x
18245 past last_visible_x. */
18246 while (it->current_x < max_x)
18247 {
18248 int x_before, x, n_glyphs_before, i, nglyphs;
18249
18250 /* Get the next display element. */
18251 if (!get_next_display_element (it))
18252 break;
18253
18254 /* Produce glyphs. */
18255 x_before = it->current_x;
18256 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18257 PRODUCE_GLYPHS (it);
18258
18259 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18260 i = 0;
18261 x = x_before;
18262 while (i < nglyphs)
18263 {
18264 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18265
18266 if (!it->truncate_lines_p
18267 && x + glyph->pixel_width > max_x)
18268 {
18269 /* End of continued line or max_x reached. */
18270 if (CHAR_GLYPH_PADDING_P (*glyph))
18271 {
18272 /* A wide character is unbreakable. */
18273 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18274 it->current_x = x_before;
18275 }
18276 else
18277 {
18278 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18279 it->current_x = x;
18280 }
18281 break;
18282 }
18283 else if (x + glyph->pixel_width > it->first_visible_x)
18284 {
18285 /* Glyph is at least partially visible. */
18286 ++it->hpos;
18287 if (x < it->first_visible_x)
18288 it->glyph_row->x = x - it->first_visible_x;
18289 }
18290 else
18291 {
18292 /* Glyph is off the left margin of the display area.
18293 Should not happen. */
18294 abort ();
18295 }
18296
18297 row->ascent = max (row->ascent, it->max_ascent);
18298 row->height = max (row->height, it->max_ascent + it->max_descent);
18299 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18300 row->phys_height = max (row->phys_height,
18301 it->max_phys_ascent + it->max_phys_descent);
18302 row->extra_line_spacing = max (row->extra_line_spacing,
18303 it->max_extra_line_spacing);
18304 x += glyph->pixel_width;
18305 ++i;
18306 }
18307
18308 /* Stop if max_x reached. */
18309 if (i < nglyphs)
18310 break;
18311
18312 /* Stop at line ends. */
18313 if (ITERATOR_AT_END_OF_LINE_P (it))
18314 {
18315 it->continuation_lines_width = 0;
18316 break;
18317 }
18318
18319 set_iterator_to_next (it, 1);
18320
18321 /* Stop if truncating at the right edge. */
18322 if (it->truncate_lines_p
18323 && it->current_x >= it->last_visible_x)
18324 {
18325 /* Add truncation mark, but don't do it if the line is
18326 truncated at a padding space. */
18327 if (IT_CHARPOS (*it) < it->string_nchars)
18328 {
18329 if (!FRAME_WINDOW_P (it->f))
18330 {
18331 int i, n;
18332
18333 if (it->current_x > it->last_visible_x)
18334 {
18335 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18336 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18337 break;
18338 for (n = row->used[TEXT_AREA]; i < n; ++i)
18339 {
18340 row->used[TEXT_AREA] = i;
18341 produce_special_glyphs (it, IT_TRUNCATION);
18342 }
18343 }
18344 produce_special_glyphs (it, IT_TRUNCATION);
18345 }
18346 it->glyph_row->truncated_on_right_p = 1;
18347 }
18348 break;
18349 }
18350 }
18351
18352 /* Maybe insert a truncation at the left. */
18353 if (it->first_visible_x
18354 && IT_CHARPOS (*it) > 0)
18355 {
18356 if (!FRAME_WINDOW_P (it->f))
18357 insert_left_trunc_glyphs (it);
18358 it->glyph_row->truncated_on_left_p = 1;
18359 }
18360
18361 it->face_id = saved_face_id;
18362
18363 /* Value is number of columns displayed. */
18364 return it->hpos - hpos_at_start;
18365 }
18366
18367
18368 \f
18369 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18370 appears as an element of LIST or as the car of an element of LIST.
18371 If PROPVAL is a list, compare each element against LIST in that
18372 way, and return 1/2 if any element of PROPVAL is found in LIST.
18373 Otherwise return 0. This function cannot quit.
18374 The return value is 2 if the text is invisible but with an ellipsis
18375 and 1 if it's invisible and without an ellipsis. */
18376
18377 int
18378 invisible_p (propval, list)
18379 register Lisp_Object propval;
18380 Lisp_Object list;
18381 {
18382 register Lisp_Object tail, proptail;
18383
18384 for (tail = list; CONSP (tail); tail = XCDR (tail))
18385 {
18386 register Lisp_Object tem;
18387 tem = XCAR (tail);
18388 if (EQ (propval, tem))
18389 return 1;
18390 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18391 return NILP (XCDR (tem)) ? 1 : 2;
18392 }
18393
18394 if (CONSP (propval))
18395 {
18396 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18397 {
18398 Lisp_Object propelt;
18399 propelt = XCAR (proptail);
18400 for (tail = list; CONSP (tail); tail = XCDR (tail))
18401 {
18402 register Lisp_Object tem;
18403 tem = XCAR (tail);
18404 if (EQ (propelt, tem))
18405 return 1;
18406 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18407 return NILP (XCDR (tem)) ? 1 : 2;
18408 }
18409 }
18410 }
18411
18412 return 0;
18413 }
18414
18415 /* Calculate a width or height in pixels from a specification using
18416 the following elements:
18417
18418 SPEC ::=
18419 NUM - a (fractional) multiple of the default font width/height
18420 (NUM) - specifies exactly NUM pixels
18421 UNIT - a fixed number of pixels, see below.
18422 ELEMENT - size of a display element in pixels, see below.
18423 (NUM . SPEC) - equals NUM * SPEC
18424 (+ SPEC SPEC ...) - add pixel values
18425 (- SPEC SPEC ...) - subtract pixel values
18426 (- SPEC) - negate pixel value
18427
18428 NUM ::=
18429 INT or FLOAT - a number constant
18430 SYMBOL - use symbol's (buffer local) variable binding.
18431
18432 UNIT ::=
18433 in - pixels per inch *)
18434 mm - pixels per 1/1000 meter *)
18435 cm - pixels per 1/100 meter *)
18436 width - width of current font in pixels.
18437 height - height of current font in pixels.
18438
18439 *) using the ratio(s) defined in display-pixels-per-inch.
18440
18441 ELEMENT ::=
18442
18443 left-fringe - left fringe width in pixels
18444 right-fringe - right fringe width in pixels
18445
18446 left-margin - left margin width in pixels
18447 right-margin - right margin width in pixels
18448
18449 scroll-bar - scroll-bar area width in pixels
18450
18451 Examples:
18452
18453 Pixels corresponding to 5 inches:
18454 (5 . in)
18455
18456 Total width of non-text areas on left side of window (if scroll-bar is on left):
18457 '(space :width (+ left-fringe left-margin scroll-bar))
18458
18459 Align to first text column (in header line):
18460 '(space :align-to 0)
18461
18462 Align to middle of text area minus half the width of variable `my-image'
18463 containing a loaded image:
18464 '(space :align-to (0.5 . (- text my-image)))
18465
18466 Width of left margin minus width of 1 character in the default font:
18467 '(space :width (- left-margin 1))
18468
18469 Width of left margin minus width of 2 characters in the current font:
18470 '(space :width (- left-margin (2 . width)))
18471
18472 Center 1 character over left-margin (in header line):
18473 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18474
18475 Different ways to express width of left fringe plus left margin minus one pixel:
18476 '(space :width (- (+ left-fringe left-margin) (1)))
18477 '(space :width (+ left-fringe left-margin (- (1))))
18478 '(space :width (+ left-fringe left-margin (-1)))
18479
18480 */
18481
18482 #define NUMVAL(X) \
18483 ((INTEGERP (X) || FLOATP (X)) \
18484 ? XFLOATINT (X) \
18485 : - 1)
18486
18487 int
18488 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18489 double *res;
18490 struct it *it;
18491 Lisp_Object prop;
18492 void *font;
18493 int width_p, *align_to;
18494 {
18495 double pixels;
18496
18497 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18498 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18499
18500 if (NILP (prop))
18501 return OK_PIXELS (0);
18502
18503 if (SYMBOLP (prop))
18504 {
18505 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18506 {
18507 char *unit = SDATA (SYMBOL_NAME (prop));
18508
18509 if (unit[0] == 'i' && unit[1] == 'n')
18510 pixels = 1.0;
18511 else if (unit[0] == 'm' && unit[1] == 'm')
18512 pixels = 25.4;
18513 else if (unit[0] == 'c' && unit[1] == 'm')
18514 pixels = 2.54;
18515 else
18516 pixels = 0;
18517 if (pixels > 0)
18518 {
18519 double ppi;
18520 #ifdef HAVE_WINDOW_SYSTEM
18521 if (FRAME_WINDOW_P (it->f)
18522 && (ppi = (width_p
18523 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18524 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18525 ppi > 0))
18526 return OK_PIXELS (ppi / pixels);
18527 #endif
18528
18529 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18530 || (CONSP (Vdisplay_pixels_per_inch)
18531 && (ppi = (width_p
18532 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18533 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18534 ppi > 0)))
18535 return OK_PIXELS (ppi / pixels);
18536
18537 return 0;
18538 }
18539 }
18540
18541 #ifdef HAVE_WINDOW_SYSTEM
18542 if (EQ (prop, Qheight))
18543 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18544 if (EQ (prop, Qwidth))
18545 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18546 #else
18547 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18548 return OK_PIXELS (1);
18549 #endif
18550
18551 if (EQ (prop, Qtext))
18552 return OK_PIXELS (width_p
18553 ? window_box_width (it->w, TEXT_AREA)
18554 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18555
18556 if (align_to && *align_to < 0)
18557 {
18558 *res = 0;
18559 if (EQ (prop, Qleft))
18560 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18561 if (EQ (prop, Qright))
18562 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18563 if (EQ (prop, Qcenter))
18564 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18565 + window_box_width (it->w, TEXT_AREA) / 2);
18566 if (EQ (prop, Qleft_fringe))
18567 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18568 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18569 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18570 if (EQ (prop, Qright_fringe))
18571 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18572 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18573 : window_box_right_offset (it->w, TEXT_AREA));
18574 if (EQ (prop, Qleft_margin))
18575 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18576 if (EQ (prop, Qright_margin))
18577 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18578 if (EQ (prop, Qscroll_bar))
18579 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18580 ? 0
18581 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18582 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18583 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18584 : 0)));
18585 }
18586 else
18587 {
18588 if (EQ (prop, Qleft_fringe))
18589 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18590 if (EQ (prop, Qright_fringe))
18591 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18592 if (EQ (prop, Qleft_margin))
18593 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18594 if (EQ (prop, Qright_margin))
18595 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18596 if (EQ (prop, Qscroll_bar))
18597 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18598 }
18599
18600 prop = Fbuffer_local_value (prop, it->w->buffer);
18601 }
18602
18603 if (INTEGERP (prop) || FLOATP (prop))
18604 {
18605 int base_unit = (width_p
18606 ? FRAME_COLUMN_WIDTH (it->f)
18607 : FRAME_LINE_HEIGHT (it->f));
18608 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18609 }
18610
18611 if (CONSP (prop))
18612 {
18613 Lisp_Object car = XCAR (prop);
18614 Lisp_Object cdr = XCDR (prop);
18615
18616 if (SYMBOLP (car))
18617 {
18618 #ifdef HAVE_WINDOW_SYSTEM
18619 if (valid_image_p (prop))
18620 {
18621 int id = lookup_image (it->f, prop);
18622 struct image *img = IMAGE_FROM_ID (it->f, id);
18623
18624 return OK_PIXELS (width_p ? img->width : img->height);
18625 }
18626 #endif
18627 if (EQ (car, Qplus) || EQ (car, Qminus))
18628 {
18629 int first = 1;
18630 double px;
18631
18632 pixels = 0;
18633 while (CONSP (cdr))
18634 {
18635 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18636 font, width_p, align_to))
18637 return 0;
18638 if (first)
18639 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18640 else
18641 pixels += px;
18642 cdr = XCDR (cdr);
18643 }
18644 if (EQ (car, Qminus))
18645 pixels = -pixels;
18646 return OK_PIXELS (pixels);
18647 }
18648
18649 car = Fbuffer_local_value (car, it->w->buffer);
18650 }
18651
18652 if (INTEGERP (car) || FLOATP (car))
18653 {
18654 double fact;
18655 pixels = XFLOATINT (car);
18656 if (NILP (cdr))
18657 return OK_PIXELS (pixels);
18658 if (calc_pixel_width_or_height (&fact, it, cdr,
18659 font, width_p, align_to))
18660 return OK_PIXELS (pixels * fact);
18661 return 0;
18662 }
18663
18664 return 0;
18665 }
18666
18667 return 0;
18668 }
18669
18670 \f
18671 /***********************************************************************
18672 Glyph Display
18673 ***********************************************************************/
18674
18675 #ifdef HAVE_WINDOW_SYSTEM
18676
18677 #if GLYPH_DEBUG
18678
18679 void
18680 dump_glyph_string (s)
18681 struct glyph_string *s;
18682 {
18683 fprintf (stderr, "glyph string\n");
18684 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18685 s->x, s->y, s->width, s->height);
18686 fprintf (stderr, " ybase = %d\n", s->ybase);
18687 fprintf (stderr, " hl = %d\n", s->hl);
18688 fprintf (stderr, " left overhang = %d, right = %d\n",
18689 s->left_overhang, s->right_overhang);
18690 fprintf (stderr, " nchars = %d\n", s->nchars);
18691 fprintf (stderr, " extends to end of line = %d\n",
18692 s->extends_to_end_of_line_p);
18693 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18694 fprintf (stderr, " bg width = %d\n", s->background_width);
18695 }
18696
18697 #endif /* GLYPH_DEBUG */
18698
18699 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18700 of XChar2b structures for S; it can't be allocated in
18701 init_glyph_string because it must be allocated via `alloca'. W
18702 is the window on which S is drawn. ROW and AREA are the glyph row
18703 and area within the row from which S is constructed. START is the
18704 index of the first glyph structure covered by S. HL is a
18705 face-override for drawing S. */
18706
18707 #ifdef HAVE_NTGUI
18708 #define OPTIONAL_HDC(hdc) hdc,
18709 #define DECLARE_HDC(hdc) HDC hdc;
18710 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18711 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18712 #endif
18713
18714 #ifndef OPTIONAL_HDC
18715 #define OPTIONAL_HDC(hdc)
18716 #define DECLARE_HDC(hdc)
18717 #define ALLOCATE_HDC(hdc, f)
18718 #define RELEASE_HDC(hdc, f)
18719 #endif
18720
18721 static void
18722 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18723 struct glyph_string *s;
18724 DECLARE_HDC (hdc)
18725 XChar2b *char2b;
18726 struct window *w;
18727 struct glyph_row *row;
18728 enum glyph_row_area area;
18729 int start;
18730 enum draw_glyphs_face hl;
18731 {
18732 bzero (s, sizeof *s);
18733 s->w = w;
18734 s->f = XFRAME (w->frame);
18735 #ifdef HAVE_NTGUI
18736 s->hdc = hdc;
18737 #endif
18738 s->display = FRAME_X_DISPLAY (s->f);
18739 s->window = FRAME_X_WINDOW (s->f);
18740 s->char2b = char2b;
18741 s->hl = hl;
18742 s->row = row;
18743 s->area = area;
18744 s->first_glyph = row->glyphs[area] + start;
18745 s->height = row->height;
18746 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18747
18748 /* Display the internal border below the tool-bar window. */
18749 if (WINDOWP (s->f->tool_bar_window)
18750 && s->w == XWINDOW (s->f->tool_bar_window))
18751 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18752
18753 s->ybase = s->y + row->ascent;
18754 }
18755
18756
18757 /* Append the list of glyph strings with head H and tail T to the list
18758 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18759
18760 static INLINE void
18761 append_glyph_string_lists (head, tail, h, t)
18762 struct glyph_string **head, **tail;
18763 struct glyph_string *h, *t;
18764 {
18765 if (h)
18766 {
18767 if (*head)
18768 (*tail)->next = h;
18769 else
18770 *head = h;
18771 h->prev = *tail;
18772 *tail = t;
18773 }
18774 }
18775
18776
18777 /* Prepend the list of glyph strings with head H and tail T to the
18778 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18779 result. */
18780
18781 static INLINE void
18782 prepend_glyph_string_lists (head, tail, h, t)
18783 struct glyph_string **head, **tail;
18784 struct glyph_string *h, *t;
18785 {
18786 if (h)
18787 {
18788 if (*head)
18789 (*head)->prev = t;
18790 else
18791 *tail = t;
18792 t->next = *head;
18793 *head = h;
18794 }
18795 }
18796
18797
18798 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18799 Set *HEAD and *TAIL to the resulting list. */
18800
18801 static INLINE void
18802 append_glyph_string (head, tail, s)
18803 struct glyph_string **head, **tail;
18804 struct glyph_string *s;
18805 {
18806 s->next = s->prev = NULL;
18807 append_glyph_string_lists (head, tail, s, s);
18808 }
18809
18810
18811 /* Get face and two-byte form of character glyph GLYPH on frame F.
18812 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18813 a pointer to a realized face that is ready for display. */
18814
18815 static INLINE struct face *
18816 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18817 struct frame *f;
18818 struct glyph *glyph;
18819 XChar2b *char2b;
18820 int *two_byte_p;
18821 {
18822 struct face *face;
18823
18824 xassert (glyph->type == CHAR_GLYPH);
18825 face = FACE_FROM_ID (f, glyph->face_id);
18826
18827 if (two_byte_p)
18828 *two_byte_p = 0;
18829
18830 if (!glyph->multibyte_p)
18831 {
18832 /* Unibyte case. We don't have to encode, but we have to make
18833 sure to use a face suitable for unibyte. */
18834 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18835 }
18836 else if (glyph->u.ch < 128)
18837 {
18838 /* Case of ASCII in a face known to fit ASCII. */
18839 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18840 }
18841 else
18842 {
18843 int c1, c2, charset;
18844
18845 /* Split characters into bytes. If c2 is -1 afterwards, C is
18846 really a one-byte character so that byte1 is zero. */
18847 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18848 if (c2 > 0)
18849 STORE_XCHAR2B (char2b, c1, c2);
18850 else
18851 STORE_XCHAR2B (char2b, 0, c1);
18852
18853 /* Maybe encode the character in *CHAR2B. */
18854 if (charset != CHARSET_ASCII)
18855 {
18856 struct font_info *font_info
18857 = FONT_INFO_FROM_ID (f, face->font_info_id);
18858 if (font_info)
18859 glyph->font_type
18860 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18861 }
18862 }
18863
18864 /* Make sure X resources of the face are allocated. */
18865 xassert (face != NULL);
18866 PREPARE_FACE_FOR_DISPLAY (f, face);
18867 return face;
18868 }
18869
18870
18871 /* Fill glyph string S with composition components specified by S->cmp.
18872
18873 FACES is an array of faces for all components of this composition.
18874 S->gidx is the index of the first component for S.
18875
18876 OVERLAPS non-zero means S should draw the foreground only, and use
18877 its physical height for clipping. See also draw_glyphs.
18878
18879 Value is the index of a component not in S. */
18880
18881 static int
18882 fill_composite_glyph_string (s, faces, overlaps)
18883 struct glyph_string *s;
18884 struct face **faces;
18885 int overlaps;
18886 {
18887 int i;
18888
18889 xassert (s);
18890
18891 s->for_overlaps = overlaps;
18892
18893 s->face = faces[s->gidx];
18894 s->font = s->face->font;
18895 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18896
18897 /* For all glyphs of this composition, starting at the offset
18898 S->gidx, until we reach the end of the definition or encounter a
18899 glyph that requires the different face, add it to S. */
18900 ++s->nchars;
18901 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18902 ++s->nchars;
18903
18904 /* All glyph strings for the same composition has the same width,
18905 i.e. the width set for the first component of the composition. */
18906
18907 s->width = s->first_glyph->pixel_width;
18908
18909 /* If the specified font could not be loaded, use the frame's
18910 default font, but record the fact that we couldn't load it in
18911 the glyph string so that we can draw rectangles for the
18912 characters of the glyph string. */
18913 if (s->font == NULL)
18914 {
18915 s->font_not_found_p = 1;
18916 s->font = FRAME_FONT (s->f);
18917 }
18918
18919 /* Adjust base line for subscript/superscript text. */
18920 s->ybase += s->first_glyph->voffset;
18921
18922 xassert (s->face && s->face->gc);
18923
18924 /* This glyph string must always be drawn with 16-bit functions. */
18925 s->two_byte_p = 1;
18926
18927 return s->gidx + s->nchars;
18928 }
18929
18930
18931 /* Fill glyph string S from a sequence of character glyphs.
18932
18933 FACE_ID is the face id of the string. START is the index of the
18934 first glyph to consider, END is the index of the last + 1.
18935 OVERLAPS non-zero means S should draw the foreground only, and use
18936 its physical height for clipping. See also draw_glyphs.
18937
18938 Value is the index of the first glyph not in S. */
18939
18940 static int
18941 fill_glyph_string (s, face_id, start, end, overlaps)
18942 struct glyph_string *s;
18943 int face_id;
18944 int start, end, overlaps;
18945 {
18946 struct glyph *glyph, *last;
18947 int voffset;
18948 int glyph_not_available_p;
18949
18950 xassert (s->f == XFRAME (s->w->frame));
18951 xassert (s->nchars == 0);
18952 xassert (start >= 0 && end > start);
18953
18954 s->for_overlaps = overlaps,
18955 glyph = s->row->glyphs[s->area] + start;
18956 last = s->row->glyphs[s->area] + end;
18957 voffset = glyph->voffset;
18958
18959 glyph_not_available_p = glyph->glyph_not_available_p;
18960
18961 while (glyph < last
18962 && glyph->type == CHAR_GLYPH
18963 && glyph->voffset == voffset
18964 /* Same face id implies same font, nowadays. */
18965 && glyph->face_id == face_id
18966 && glyph->glyph_not_available_p == glyph_not_available_p)
18967 {
18968 int two_byte_p;
18969
18970 s->face = get_glyph_face_and_encoding (s->f, glyph,
18971 s->char2b + s->nchars,
18972 &two_byte_p);
18973 s->two_byte_p = two_byte_p;
18974 ++s->nchars;
18975 xassert (s->nchars <= end - start);
18976 s->width += glyph->pixel_width;
18977 ++glyph;
18978 }
18979
18980 s->font = s->face->font;
18981 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18982
18983 /* If the specified font could not be loaded, use the frame's font,
18984 but record the fact that we couldn't load it in
18985 S->font_not_found_p so that we can draw rectangles for the
18986 characters of the glyph string. */
18987 if (s->font == NULL || glyph_not_available_p)
18988 {
18989 s->font_not_found_p = 1;
18990 s->font = FRAME_FONT (s->f);
18991 }
18992
18993 /* Adjust base line for subscript/superscript text. */
18994 s->ybase += voffset;
18995
18996 xassert (s->face && s->face->gc);
18997 return glyph - s->row->glyphs[s->area];
18998 }
18999
19000
19001 /* Fill glyph string S from image glyph S->first_glyph. */
19002
19003 static void
19004 fill_image_glyph_string (s)
19005 struct glyph_string *s;
19006 {
19007 xassert (s->first_glyph->type == IMAGE_GLYPH);
19008 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19009 xassert (s->img);
19010 s->slice = s->first_glyph->slice;
19011 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19012 s->font = s->face->font;
19013 s->width = s->first_glyph->pixel_width;
19014
19015 /* Adjust base line for subscript/superscript text. */
19016 s->ybase += s->first_glyph->voffset;
19017 }
19018
19019
19020 /* Fill glyph string S from a sequence of stretch glyphs.
19021
19022 ROW is the glyph row in which the glyphs are found, AREA is the
19023 area within the row. START is the index of the first glyph to
19024 consider, END is the index of the last + 1.
19025
19026 Value is the index of the first glyph not in S. */
19027
19028 static int
19029 fill_stretch_glyph_string (s, row, area, start, end)
19030 struct glyph_string *s;
19031 struct glyph_row *row;
19032 enum glyph_row_area area;
19033 int start, end;
19034 {
19035 struct glyph *glyph, *last;
19036 int voffset, face_id;
19037
19038 xassert (s->first_glyph->type == STRETCH_GLYPH);
19039
19040 glyph = s->row->glyphs[s->area] + start;
19041 last = s->row->glyphs[s->area] + end;
19042 face_id = glyph->face_id;
19043 s->face = FACE_FROM_ID (s->f, face_id);
19044 s->font = s->face->font;
19045 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19046 s->width = glyph->pixel_width;
19047 s->nchars = 1;
19048 voffset = glyph->voffset;
19049
19050 for (++glyph;
19051 (glyph < last
19052 && glyph->type == STRETCH_GLYPH
19053 && glyph->voffset == voffset
19054 && glyph->face_id == face_id);
19055 ++glyph)
19056 s->width += glyph->pixel_width;
19057
19058 /* Adjust base line for subscript/superscript text. */
19059 s->ybase += voffset;
19060
19061 /* The case that face->gc == 0 is handled when drawing the glyph
19062 string by calling PREPARE_FACE_FOR_DISPLAY. */
19063 xassert (s->face);
19064 return glyph - s->row->glyphs[s->area];
19065 }
19066
19067
19068 /* EXPORT for RIF:
19069 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19070 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19071 assumed to be zero. */
19072
19073 void
19074 x_get_glyph_overhangs (glyph, f, left, right)
19075 struct glyph *glyph;
19076 struct frame *f;
19077 int *left, *right;
19078 {
19079 *left = *right = 0;
19080
19081 if (glyph->type == CHAR_GLYPH)
19082 {
19083 XFontStruct *font;
19084 struct face *face;
19085 struct font_info *font_info;
19086 XChar2b char2b;
19087 XCharStruct *pcm;
19088
19089 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19090 font = face->font;
19091 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19092 if (font /* ++KFS: Should this be font_info ? */
19093 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19094 {
19095 if (pcm->rbearing > pcm->width)
19096 *right = pcm->rbearing - pcm->width;
19097 if (pcm->lbearing < 0)
19098 *left = -pcm->lbearing;
19099 }
19100 }
19101 }
19102
19103
19104 /* Return the index of the first glyph preceding glyph string S that
19105 is overwritten by S because of S's left overhang. Value is -1
19106 if no glyphs are overwritten. */
19107
19108 static int
19109 left_overwritten (s)
19110 struct glyph_string *s;
19111 {
19112 int k;
19113
19114 if (s->left_overhang)
19115 {
19116 int x = 0, i;
19117 struct glyph *glyphs = s->row->glyphs[s->area];
19118 int first = s->first_glyph - glyphs;
19119
19120 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19121 x -= glyphs[i].pixel_width;
19122
19123 k = i + 1;
19124 }
19125 else
19126 k = -1;
19127
19128 return k;
19129 }
19130
19131
19132 /* Return the index of the first glyph preceding glyph string S that
19133 is overwriting S because of its right overhang. Value is -1 if no
19134 glyph in front of S overwrites S. */
19135
19136 static int
19137 left_overwriting (s)
19138 struct glyph_string *s;
19139 {
19140 int i, k, x;
19141 struct glyph *glyphs = s->row->glyphs[s->area];
19142 int first = s->first_glyph - glyphs;
19143
19144 k = -1;
19145 x = 0;
19146 for (i = first - 1; i >= 0; --i)
19147 {
19148 int left, right;
19149 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19150 if (x + right > 0)
19151 k = i;
19152 x -= glyphs[i].pixel_width;
19153 }
19154
19155 return k;
19156 }
19157
19158
19159 /* Return the index of the last glyph following glyph string S that is
19160 not overwritten by S because of S's right overhang. Value is -1 if
19161 no such glyph is found. */
19162
19163 static int
19164 right_overwritten (s)
19165 struct glyph_string *s;
19166 {
19167 int k = -1;
19168
19169 if (s->right_overhang)
19170 {
19171 int x = 0, i;
19172 struct glyph *glyphs = s->row->glyphs[s->area];
19173 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19174 int end = s->row->used[s->area];
19175
19176 for (i = first; i < end && s->right_overhang > x; ++i)
19177 x += glyphs[i].pixel_width;
19178
19179 k = i;
19180 }
19181
19182 return k;
19183 }
19184
19185
19186 /* Return the index of the last glyph following glyph string S that
19187 overwrites S because of its left overhang. Value is negative
19188 if no such glyph is found. */
19189
19190 static int
19191 right_overwriting (s)
19192 struct glyph_string *s;
19193 {
19194 int i, k, x;
19195 int end = s->row->used[s->area];
19196 struct glyph *glyphs = s->row->glyphs[s->area];
19197 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19198
19199 k = -1;
19200 x = 0;
19201 for (i = first; i < end; ++i)
19202 {
19203 int left, right;
19204 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19205 if (x - left < 0)
19206 k = i;
19207 x += glyphs[i].pixel_width;
19208 }
19209
19210 return k;
19211 }
19212
19213
19214 /* Get face and two-byte form of character C in face FACE_ID on frame
19215 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19216 means we want to display multibyte text. DISPLAY_P non-zero means
19217 make sure that X resources for the face returned are allocated.
19218 Value is a pointer to a realized face that is ready for display if
19219 DISPLAY_P is non-zero. */
19220
19221 static INLINE struct face *
19222 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19223 struct frame *f;
19224 int c, face_id;
19225 XChar2b *char2b;
19226 int multibyte_p, display_p;
19227 {
19228 struct face *face = FACE_FROM_ID (f, face_id);
19229
19230 if (!multibyte_p)
19231 {
19232 /* Unibyte case. We don't have to encode, but we have to make
19233 sure to use a face suitable for unibyte. */
19234 STORE_XCHAR2B (char2b, 0, c);
19235 face_id = FACE_FOR_CHAR (f, face, c);
19236 face = FACE_FROM_ID (f, face_id);
19237 }
19238 else if (c < 128)
19239 {
19240 /* Case of ASCII in a face known to fit ASCII. */
19241 STORE_XCHAR2B (char2b, 0, c);
19242 }
19243 else
19244 {
19245 int c1, c2, charset;
19246
19247 /* Split characters into bytes. If c2 is -1 afterwards, C is
19248 really a one-byte character so that byte1 is zero. */
19249 SPLIT_CHAR (c, charset, c1, c2);
19250 if (c2 > 0)
19251 STORE_XCHAR2B (char2b, c1, c2);
19252 else
19253 STORE_XCHAR2B (char2b, 0, c1);
19254
19255 /* Maybe encode the character in *CHAR2B. */
19256 if (face->font != NULL)
19257 {
19258 struct font_info *font_info
19259 = FONT_INFO_FROM_ID (f, face->font_info_id);
19260 if (font_info)
19261 rif->encode_char (c, char2b, font_info, 0);
19262 }
19263 }
19264
19265 /* Make sure X resources of the face are allocated. */
19266 #ifdef HAVE_X_WINDOWS
19267 if (display_p)
19268 #endif
19269 {
19270 xassert (face != NULL);
19271 PREPARE_FACE_FOR_DISPLAY (f, face);
19272 }
19273
19274 return face;
19275 }
19276
19277
19278 /* Set background width of glyph string S. START is the index of the
19279 first glyph following S. LAST_X is the right-most x-position + 1
19280 in the drawing area. */
19281
19282 static INLINE void
19283 set_glyph_string_background_width (s, start, last_x)
19284 struct glyph_string *s;
19285 int start;
19286 int last_x;
19287 {
19288 /* If the face of this glyph string has to be drawn to the end of
19289 the drawing area, set S->extends_to_end_of_line_p. */
19290
19291 if (start == s->row->used[s->area]
19292 && s->area == TEXT_AREA
19293 && ((s->row->fill_line_p
19294 && (s->hl == DRAW_NORMAL_TEXT
19295 || s->hl == DRAW_IMAGE_RAISED
19296 || s->hl == DRAW_IMAGE_SUNKEN))
19297 || s->hl == DRAW_MOUSE_FACE))
19298 s->extends_to_end_of_line_p = 1;
19299
19300 /* If S extends its face to the end of the line, set its
19301 background_width to the distance to the right edge of the drawing
19302 area. */
19303 if (s->extends_to_end_of_line_p)
19304 s->background_width = last_x - s->x + 1;
19305 else
19306 s->background_width = s->width;
19307 }
19308
19309
19310 /* Compute overhangs and x-positions for glyph string S and its
19311 predecessors, or successors. X is the starting x-position for S.
19312 BACKWARD_P non-zero means process predecessors. */
19313
19314 static void
19315 compute_overhangs_and_x (s, x, backward_p)
19316 struct glyph_string *s;
19317 int x;
19318 int backward_p;
19319 {
19320 if (backward_p)
19321 {
19322 while (s)
19323 {
19324 if (rif->compute_glyph_string_overhangs)
19325 rif->compute_glyph_string_overhangs (s);
19326 x -= s->width;
19327 s->x = x;
19328 s = s->prev;
19329 }
19330 }
19331 else
19332 {
19333 while (s)
19334 {
19335 if (rif->compute_glyph_string_overhangs)
19336 rif->compute_glyph_string_overhangs (s);
19337 s->x = x;
19338 x += s->width;
19339 s = s->next;
19340 }
19341 }
19342 }
19343
19344
19345
19346 /* The following macros are only called from draw_glyphs below.
19347 They reference the following parameters of that function directly:
19348 `w', `row', `area', and `overlap_p'
19349 as well as the following local variables:
19350 `s', `f', and `hdc' (in W32) */
19351
19352 #ifdef HAVE_NTGUI
19353 /* On W32, silently add local `hdc' variable to argument list of
19354 init_glyph_string. */
19355 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19356 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19357 #else
19358 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19359 init_glyph_string (s, char2b, w, row, area, start, hl)
19360 #endif
19361
19362 /* Add a glyph string for a stretch glyph to the list of strings
19363 between HEAD and TAIL. START is the index of the stretch glyph in
19364 row area AREA of glyph row ROW. END is the index of the last glyph
19365 in that glyph row area. X is the current output position assigned
19366 to the new glyph string constructed. HL overrides that face of the
19367 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19368 is the right-most x-position of the drawing area. */
19369
19370 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19371 and below -- keep them on one line. */
19372 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19373 do \
19374 { \
19375 s = (struct glyph_string *) alloca (sizeof *s); \
19376 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19377 START = fill_stretch_glyph_string (s, row, area, START, END); \
19378 append_glyph_string (&HEAD, &TAIL, s); \
19379 s->x = (X); \
19380 } \
19381 while (0)
19382
19383
19384 /* Add a glyph string for an image glyph to the list of strings
19385 between HEAD and TAIL. START is the index of the image glyph in
19386 row area AREA of glyph row ROW. END is the index of the last glyph
19387 in that glyph row area. X is the current output position assigned
19388 to the new glyph string constructed. HL overrides that face of the
19389 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19390 is the right-most x-position of the drawing area. */
19391
19392 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19393 do \
19394 { \
19395 s = (struct glyph_string *) alloca (sizeof *s); \
19396 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19397 fill_image_glyph_string (s); \
19398 append_glyph_string (&HEAD, &TAIL, s); \
19399 ++START; \
19400 s->x = (X); \
19401 } \
19402 while (0)
19403
19404
19405 /* Add a glyph string for a sequence of character glyphs to the list
19406 of strings between HEAD and TAIL. START is the index of the first
19407 glyph in row area AREA of glyph row ROW that is part of the new
19408 glyph string. END is the index of the last glyph in that glyph row
19409 area. X is the current output position assigned to the new glyph
19410 string constructed. HL overrides that face of the glyph; e.g. it
19411 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19412 right-most x-position of the drawing area. */
19413
19414 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19415 do \
19416 { \
19417 int c, face_id; \
19418 XChar2b *char2b; \
19419 \
19420 c = (row)->glyphs[area][START].u.ch; \
19421 face_id = (row)->glyphs[area][START].face_id; \
19422 \
19423 s = (struct glyph_string *) alloca (sizeof *s); \
19424 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19425 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19426 append_glyph_string (&HEAD, &TAIL, s); \
19427 s->x = (X); \
19428 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19429 } \
19430 while (0)
19431
19432
19433 /* Add a glyph string for a composite sequence to the list of strings
19434 between HEAD and TAIL. START is the index of the first glyph in
19435 row area AREA of glyph row ROW that is part of the new glyph
19436 string. END is the index of the last glyph in that glyph row area.
19437 X is the current output position assigned to the new glyph string
19438 constructed. HL overrides that face of the glyph; e.g. it is
19439 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19440 x-position of the drawing area. */
19441
19442 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19443 do { \
19444 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19445 int face_id = (row)->glyphs[area][START].face_id; \
19446 struct face *base_face = FACE_FROM_ID (f, face_id); \
19447 struct composition *cmp = composition_table[cmp_id]; \
19448 int glyph_len = cmp->glyph_len; \
19449 XChar2b *char2b; \
19450 struct face **faces; \
19451 struct glyph_string *first_s = NULL; \
19452 int n; \
19453 \
19454 base_face = base_face->ascii_face; \
19455 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19456 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19457 /* At first, fill in `char2b' and `faces'. */ \
19458 for (n = 0; n < glyph_len; n++) \
19459 { \
19460 int c = COMPOSITION_GLYPH (cmp, n); \
19461 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19462 faces[n] = FACE_FROM_ID (f, this_face_id); \
19463 get_char_face_and_encoding (f, c, this_face_id, \
19464 char2b + n, 1, 1); \
19465 } \
19466 \
19467 /* Make glyph_strings for each glyph sequence that is drawable by \
19468 the same face, and append them to HEAD/TAIL. */ \
19469 for (n = 0; n < cmp->glyph_len;) \
19470 { \
19471 s = (struct glyph_string *) alloca (sizeof *s); \
19472 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19473 append_glyph_string (&(HEAD), &(TAIL), s); \
19474 s->cmp = cmp; \
19475 s->gidx = n; \
19476 s->x = (X); \
19477 \
19478 if (n == 0) \
19479 first_s = s; \
19480 \
19481 n = fill_composite_glyph_string (s, faces, overlaps); \
19482 } \
19483 \
19484 ++START; \
19485 s = first_s; \
19486 } while (0)
19487
19488
19489 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19490 of AREA of glyph row ROW on window W between indices START and END.
19491 HL overrides the face for drawing glyph strings, e.g. it is
19492 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19493 x-positions of the drawing area.
19494
19495 This is an ugly monster macro construct because we must use alloca
19496 to allocate glyph strings (because draw_glyphs can be called
19497 asynchronously). */
19498
19499 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19500 do \
19501 { \
19502 HEAD = TAIL = NULL; \
19503 while (START < END) \
19504 { \
19505 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19506 switch (first_glyph->type) \
19507 { \
19508 case CHAR_GLYPH: \
19509 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19510 HL, X, LAST_X); \
19511 break; \
19512 \
19513 case COMPOSITE_GLYPH: \
19514 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19515 HL, X, LAST_X); \
19516 break; \
19517 \
19518 case STRETCH_GLYPH: \
19519 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19520 HL, X, LAST_X); \
19521 break; \
19522 \
19523 case IMAGE_GLYPH: \
19524 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19525 HL, X, LAST_X); \
19526 break; \
19527 \
19528 default: \
19529 abort (); \
19530 } \
19531 \
19532 set_glyph_string_background_width (s, START, LAST_X); \
19533 (X) += s->width; \
19534 } \
19535 } \
19536 while (0)
19537
19538
19539 /* Draw glyphs between START and END in AREA of ROW on window W,
19540 starting at x-position X. X is relative to AREA in W. HL is a
19541 face-override with the following meaning:
19542
19543 DRAW_NORMAL_TEXT draw normally
19544 DRAW_CURSOR draw in cursor face
19545 DRAW_MOUSE_FACE draw in mouse face.
19546 DRAW_INVERSE_VIDEO draw in mode line face
19547 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19548 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19549
19550 If OVERLAPS is non-zero, draw only the foreground of characters and
19551 clip to the physical height of ROW. Non-zero value also defines
19552 the overlapping part to be drawn:
19553
19554 OVERLAPS_PRED overlap with preceding rows
19555 OVERLAPS_SUCC overlap with succeeding rows
19556 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19557 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19558
19559 Value is the x-position reached, relative to AREA of W. */
19560
19561 static int
19562 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19563 struct window *w;
19564 int x;
19565 struct glyph_row *row;
19566 enum glyph_row_area area;
19567 int start, end;
19568 enum draw_glyphs_face hl;
19569 int overlaps;
19570 {
19571 struct glyph_string *head, *tail;
19572 struct glyph_string *s;
19573 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19574 int last_x, area_width;
19575 int x_reached;
19576 int i, j;
19577 struct frame *f = XFRAME (WINDOW_FRAME (w));
19578 DECLARE_HDC (hdc);
19579
19580 ALLOCATE_HDC (hdc, f);
19581
19582 /* Let's rather be paranoid than getting a SEGV. */
19583 end = min (end, row->used[area]);
19584 start = max (0, start);
19585 start = min (end, start);
19586
19587 /* Translate X to frame coordinates. Set last_x to the right
19588 end of the drawing area. */
19589 if (row->full_width_p)
19590 {
19591 /* X is relative to the left edge of W, without scroll bars
19592 or fringes. */
19593 x += WINDOW_LEFT_EDGE_X (w);
19594 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19595 }
19596 else
19597 {
19598 int area_left = window_box_left (w, area);
19599 x += area_left;
19600 area_width = window_box_width (w, area);
19601 last_x = area_left + area_width;
19602 }
19603
19604 /* Build a doubly-linked list of glyph_string structures between
19605 head and tail from what we have to draw. Note that the macro
19606 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19607 the reason we use a separate variable `i'. */
19608 i = start;
19609 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19610 if (tail)
19611 x_reached = tail->x + tail->background_width;
19612 else
19613 x_reached = x;
19614
19615 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19616 the row, redraw some glyphs in front or following the glyph
19617 strings built above. */
19618 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19619 {
19620 int dummy_x = 0;
19621 struct glyph_string *h, *t;
19622
19623 /* Compute overhangs for all glyph strings. */
19624 if (rif->compute_glyph_string_overhangs)
19625 for (s = head; s; s = s->next)
19626 rif->compute_glyph_string_overhangs (s);
19627
19628 /* Prepend glyph strings for glyphs in front of the first glyph
19629 string that are overwritten because of the first glyph
19630 string's left overhang. The background of all strings
19631 prepended must be drawn because the first glyph string
19632 draws over it. */
19633 i = left_overwritten (head);
19634 if (i >= 0)
19635 {
19636 j = i;
19637 BUILD_GLYPH_STRINGS (j, start, h, t,
19638 DRAW_NORMAL_TEXT, dummy_x, last_x);
19639 start = i;
19640 compute_overhangs_and_x (t, head->x, 1);
19641 prepend_glyph_string_lists (&head, &tail, h, t);
19642 clip_head = head;
19643 }
19644
19645 /* Prepend glyph strings for glyphs in front of the first glyph
19646 string that overwrite that glyph string because of their
19647 right overhang. For these strings, only the foreground must
19648 be drawn, because it draws over the glyph string at `head'.
19649 The background must not be drawn because this would overwrite
19650 right overhangs of preceding glyphs for which no glyph
19651 strings exist. */
19652 i = left_overwriting (head);
19653 if (i >= 0)
19654 {
19655 clip_head = head;
19656 BUILD_GLYPH_STRINGS (i, start, h, t,
19657 DRAW_NORMAL_TEXT, dummy_x, last_x);
19658 for (s = h; s; s = s->next)
19659 s->background_filled_p = 1;
19660 compute_overhangs_and_x (t, head->x, 1);
19661 prepend_glyph_string_lists (&head, &tail, h, t);
19662 }
19663
19664 /* Append glyphs strings for glyphs following the last glyph
19665 string tail that are overwritten by tail. The background of
19666 these strings has to be drawn because tail's foreground draws
19667 over it. */
19668 i = right_overwritten (tail);
19669 if (i >= 0)
19670 {
19671 BUILD_GLYPH_STRINGS (end, i, h, t,
19672 DRAW_NORMAL_TEXT, x, last_x);
19673 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19674 append_glyph_string_lists (&head, &tail, h, t);
19675 clip_tail = tail;
19676 }
19677
19678 /* Append glyph strings for glyphs following the last glyph
19679 string tail that overwrite tail. The foreground of such
19680 glyphs has to be drawn because it writes into the background
19681 of tail. The background must not be drawn because it could
19682 paint over the foreground of following glyphs. */
19683 i = right_overwriting (tail);
19684 if (i >= 0)
19685 {
19686 clip_tail = tail;
19687 BUILD_GLYPH_STRINGS (end, i, h, t,
19688 DRAW_NORMAL_TEXT, x, last_x);
19689 for (s = h; s; s = s->next)
19690 s->background_filled_p = 1;
19691 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19692 append_glyph_string_lists (&head, &tail, h, t);
19693 }
19694 if (clip_head || clip_tail)
19695 for (s = head; s; s = s->next)
19696 {
19697 s->clip_head = clip_head;
19698 s->clip_tail = clip_tail;
19699 }
19700 }
19701
19702 /* Draw all strings. */
19703 for (s = head; s; s = s->next)
19704 rif->draw_glyph_string (s);
19705
19706 if (area == TEXT_AREA
19707 && !row->full_width_p
19708 /* When drawing overlapping rows, only the glyph strings'
19709 foreground is drawn, which doesn't erase a cursor
19710 completely. */
19711 && !overlaps)
19712 {
19713 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19714 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19715 : (tail ? tail->x + tail->background_width : x));
19716
19717 int text_left = window_box_left (w, TEXT_AREA);
19718 x0 -= text_left;
19719 x1 -= text_left;
19720
19721 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19722 row->y, MATRIX_ROW_BOTTOM_Y (row));
19723 }
19724
19725 /* Value is the x-position up to which drawn, relative to AREA of W.
19726 This doesn't include parts drawn because of overhangs. */
19727 if (row->full_width_p)
19728 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19729 else
19730 x_reached -= window_box_left (w, area);
19731
19732 RELEASE_HDC (hdc, f);
19733
19734 return x_reached;
19735 }
19736
19737 /* Expand row matrix if too narrow. Don't expand if area
19738 is not present. */
19739
19740 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19741 { \
19742 if (!fonts_changed_p \
19743 && (it->glyph_row->glyphs[area] \
19744 < it->glyph_row->glyphs[area + 1])) \
19745 { \
19746 it->w->ncols_scale_factor++; \
19747 fonts_changed_p = 1; \
19748 } \
19749 }
19750
19751 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19752 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19753
19754 static INLINE void
19755 append_glyph (it)
19756 struct it *it;
19757 {
19758 struct glyph *glyph;
19759 enum glyph_row_area area = it->area;
19760
19761 xassert (it->glyph_row);
19762 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19763
19764 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19765 if (glyph < it->glyph_row->glyphs[area + 1])
19766 {
19767 glyph->charpos = CHARPOS (it->position);
19768 glyph->object = it->object;
19769 glyph->pixel_width = it->pixel_width;
19770 glyph->ascent = it->ascent;
19771 glyph->descent = it->descent;
19772 glyph->voffset = it->voffset;
19773 glyph->type = CHAR_GLYPH;
19774 glyph->multibyte_p = it->multibyte_p;
19775 glyph->left_box_line_p = it->start_of_box_run_p;
19776 glyph->right_box_line_p = it->end_of_box_run_p;
19777 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19778 || it->phys_descent > it->descent);
19779 glyph->padding_p = 0;
19780 glyph->glyph_not_available_p = it->glyph_not_available_p;
19781 glyph->face_id = it->face_id;
19782 glyph->u.ch = it->char_to_display;
19783 glyph->slice = null_glyph_slice;
19784 glyph->font_type = FONT_TYPE_UNKNOWN;
19785 ++it->glyph_row->used[area];
19786 }
19787 else
19788 IT_EXPAND_MATRIX_WIDTH (it, area);
19789 }
19790
19791 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19792 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19793
19794 static INLINE void
19795 append_composite_glyph (it)
19796 struct it *it;
19797 {
19798 struct glyph *glyph;
19799 enum glyph_row_area area = it->area;
19800
19801 xassert (it->glyph_row);
19802
19803 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19804 if (glyph < it->glyph_row->glyphs[area + 1])
19805 {
19806 glyph->charpos = CHARPOS (it->position);
19807 glyph->object = it->object;
19808 glyph->pixel_width = it->pixel_width;
19809 glyph->ascent = it->ascent;
19810 glyph->descent = it->descent;
19811 glyph->voffset = it->voffset;
19812 glyph->type = COMPOSITE_GLYPH;
19813 glyph->multibyte_p = it->multibyte_p;
19814 glyph->left_box_line_p = it->start_of_box_run_p;
19815 glyph->right_box_line_p = it->end_of_box_run_p;
19816 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19817 || it->phys_descent > it->descent);
19818 glyph->padding_p = 0;
19819 glyph->glyph_not_available_p = 0;
19820 glyph->face_id = it->face_id;
19821 glyph->u.cmp_id = it->cmp_id;
19822 glyph->slice = null_glyph_slice;
19823 glyph->font_type = FONT_TYPE_UNKNOWN;
19824 ++it->glyph_row->used[area];
19825 }
19826 else
19827 IT_EXPAND_MATRIX_WIDTH (it, area);
19828 }
19829
19830
19831 /* Change IT->ascent and IT->height according to the setting of
19832 IT->voffset. */
19833
19834 static INLINE void
19835 take_vertical_position_into_account (it)
19836 struct it *it;
19837 {
19838 if (it->voffset)
19839 {
19840 if (it->voffset < 0)
19841 /* Increase the ascent so that we can display the text higher
19842 in the line. */
19843 it->ascent -= it->voffset;
19844 else
19845 /* Increase the descent so that we can display the text lower
19846 in the line. */
19847 it->descent += it->voffset;
19848 }
19849 }
19850
19851
19852 /* Produce glyphs/get display metrics for the image IT is loaded with.
19853 See the description of struct display_iterator in dispextern.h for
19854 an overview of struct display_iterator. */
19855
19856 static void
19857 produce_image_glyph (it)
19858 struct it *it;
19859 {
19860 struct image *img;
19861 struct face *face;
19862 int glyph_ascent, crop;
19863 struct glyph_slice slice;
19864
19865 xassert (it->what == IT_IMAGE);
19866
19867 face = FACE_FROM_ID (it->f, it->face_id);
19868 xassert (face);
19869 /* Make sure X resources of the face is loaded. */
19870 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19871
19872 if (it->image_id < 0)
19873 {
19874 /* Fringe bitmap. */
19875 it->ascent = it->phys_ascent = 0;
19876 it->descent = it->phys_descent = 0;
19877 it->pixel_width = 0;
19878 it->nglyphs = 0;
19879 return;
19880 }
19881
19882 img = IMAGE_FROM_ID (it->f, it->image_id);
19883 xassert (img);
19884 /* Make sure X resources of the image is loaded. */
19885 prepare_image_for_display (it->f, img);
19886
19887 slice.x = slice.y = 0;
19888 slice.width = img->width;
19889 slice.height = img->height;
19890
19891 if (INTEGERP (it->slice.x))
19892 slice.x = XINT (it->slice.x);
19893 else if (FLOATP (it->slice.x))
19894 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19895
19896 if (INTEGERP (it->slice.y))
19897 slice.y = XINT (it->slice.y);
19898 else if (FLOATP (it->slice.y))
19899 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19900
19901 if (INTEGERP (it->slice.width))
19902 slice.width = XINT (it->slice.width);
19903 else if (FLOATP (it->slice.width))
19904 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19905
19906 if (INTEGERP (it->slice.height))
19907 slice.height = XINT (it->slice.height);
19908 else if (FLOATP (it->slice.height))
19909 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19910
19911 if (slice.x >= img->width)
19912 slice.x = img->width;
19913 if (slice.y >= img->height)
19914 slice.y = img->height;
19915 if (slice.x + slice.width >= img->width)
19916 slice.width = img->width - slice.x;
19917 if (slice.y + slice.height > img->height)
19918 slice.height = img->height - slice.y;
19919
19920 if (slice.width == 0 || slice.height == 0)
19921 return;
19922
19923 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19924
19925 it->descent = slice.height - glyph_ascent;
19926 if (slice.y == 0)
19927 it->descent += img->vmargin;
19928 if (slice.y + slice.height == img->height)
19929 it->descent += img->vmargin;
19930 it->phys_descent = it->descent;
19931
19932 it->pixel_width = slice.width;
19933 if (slice.x == 0)
19934 it->pixel_width += img->hmargin;
19935 if (slice.x + slice.width == img->width)
19936 it->pixel_width += img->hmargin;
19937
19938 /* It's quite possible for images to have an ascent greater than
19939 their height, so don't get confused in that case. */
19940 if (it->descent < 0)
19941 it->descent = 0;
19942
19943 #if 0 /* this breaks image tiling */
19944 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19945 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19946 if (face_ascent > it->ascent)
19947 it->ascent = it->phys_ascent = face_ascent;
19948 #endif
19949
19950 it->nglyphs = 1;
19951
19952 if (face->box != FACE_NO_BOX)
19953 {
19954 if (face->box_line_width > 0)
19955 {
19956 if (slice.y == 0)
19957 it->ascent += face->box_line_width;
19958 if (slice.y + slice.height == img->height)
19959 it->descent += face->box_line_width;
19960 }
19961
19962 if (it->start_of_box_run_p && slice.x == 0)
19963 it->pixel_width += abs (face->box_line_width);
19964 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19965 it->pixel_width += abs (face->box_line_width);
19966 }
19967
19968 take_vertical_position_into_account (it);
19969
19970 /* Automatically crop wide image glyphs at right edge so we can
19971 draw the cursor on same display row. */
19972 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
19973 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
19974 {
19975 it->pixel_width -= crop;
19976 slice.width -= crop;
19977 }
19978
19979 if (it->glyph_row)
19980 {
19981 struct glyph *glyph;
19982 enum glyph_row_area area = it->area;
19983
19984 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19985 if (glyph < it->glyph_row->glyphs[area + 1])
19986 {
19987 glyph->charpos = CHARPOS (it->position);
19988 glyph->object = it->object;
19989 glyph->pixel_width = it->pixel_width;
19990 glyph->ascent = glyph_ascent;
19991 glyph->descent = it->descent;
19992 glyph->voffset = it->voffset;
19993 glyph->type = IMAGE_GLYPH;
19994 glyph->multibyte_p = it->multibyte_p;
19995 glyph->left_box_line_p = it->start_of_box_run_p;
19996 glyph->right_box_line_p = it->end_of_box_run_p;
19997 glyph->overlaps_vertically_p = 0;
19998 glyph->padding_p = 0;
19999 glyph->glyph_not_available_p = 0;
20000 glyph->face_id = it->face_id;
20001 glyph->u.img_id = img->id;
20002 glyph->slice = slice;
20003 glyph->font_type = FONT_TYPE_UNKNOWN;
20004 ++it->glyph_row->used[area];
20005 }
20006 else
20007 IT_EXPAND_MATRIX_WIDTH (it, area);
20008 }
20009 }
20010
20011
20012 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20013 of the glyph, WIDTH and HEIGHT are the width and height of the
20014 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20015
20016 static void
20017 append_stretch_glyph (it, object, width, height, ascent)
20018 struct it *it;
20019 Lisp_Object object;
20020 int width, height;
20021 int ascent;
20022 {
20023 struct glyph *glyph;
20024 enum glyph_row_area area = it->area;
20025
20026 xassert (ascent >= 0 && ascent <= height);
20027
20028 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20029 if (glyph < it->glyph_row->glyphs[area + 1])
20030 {
20031 glyph->charpos = CHARPOS (it->position);
20032 glyph->object = object;
20033 glyph->pixel_width = width;
20034 glyph->ascent = ascent;
20035 glyph->descent = height - ascent;
20036 glyph->voffset = it->voffset;
20037 glyph->type = STRETCH_GLYPH;
20038 glyph->multibyte_p = it->multibyte_p;
20039 glyph->left_box_line_p = it->start_of_box_run_p;
20040 glyph->right_box_line_p = it->end_of_box_run_p;
20041 glyph->overlaps_vertically_p = 0;
20042 glyph->padding_p = 0;
20043 glyph->glyph_not_available_p = 0;
20044 glyph->face_id = it->face_id;
20045 glyph->u.stretch.ascent = ascent;
20046 glyph->u.stretch.height = height;
20047 glyph->slice = null_glyph_slice;
20048 glyph->font_type = FONT_TYPE_UNKNOWN;
20049 ++it->glyph_row->used[area];
20050 }
20051 else
20052 IT_EXPAND_MATRIX_WIDTH (it, area);
20053 }
20054
20055
20056 /* Produce a stretch glyph for iterator IT. IT->object is the value
20057 of the glyph property displayed. The value must be a list
20058 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20059 being recognized:
20060
20061 1. `:width WIDTH' specifies that the space should be WIDTH *
20062 canonical char width wide. WIDTH may be an integer or floating
20063 point number.
20064
20065 2. `:relative-width FACTOR' specifies that the width of the stretch
20066 should be computed from the width of the first character having the
20067 `glyph' property, and should be FACTOR times that width.
20068
20069 3. `:align-to HPOS' specifies that the space should be wide enough
20070 to reach HPOS, a value in canonical character units.
20071
20072 Exactly one of the above pairs must be present.
20073
20074 4. `:height HEIGHT' specifies that the height of the stretch produced
20075 should be HEIGHT, measured in canonical character units.
20076
20077 5. `:relative-height FACTOR' specifies that the height of the
20078 stretch should be FACTOR times the height of the characters having
20079 the glyph property.
20080
20081 Either none or exactly one of 4 or 5 must be present.
20082
20083 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20084 of the stretch should be used for the ascent of the stretch.
20085 ASCENT must be in the range 0 <= ASCENT <= 100. */
20086
20087 static void
20088 produce_stretch_glyph (it)
20089 struct it *it;
20090 {
20091 /* (space :width WIDTH :height HEIGHT ...) */
20092 Lisp_Object prop, plist;
20093 int width = 0, height = 0, align_to = -1;
20094 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20095 int ascent = 0;
20096 double tem;
20097 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20098 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20099
20100 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20101
20102 /* List should start with `space'. */
20103 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20104 plist = XCDR (it->object);
20105
20106 /* Compute the width of the stretch. */
20107 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20108 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20109 {
20110 /* Absolute width `:width WIDTH' specified and valid. */
20111 zero_width_ok_p = 1;
20112 width = (int)tem;
20113 }
20114 else if (prop = Fplist_get (plist, QCrelative_width),
20115 NUMVAL (prop) > 0)
20116 {
20117 /* Relative width `:relative-width FACTOR' specified and valid.
20118 Compute the width of the characters having the `glyph'
20119 property. */
20120 struct it it2;
20121 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20122
20123 it2 = *it;
20124 if (it->multibyte_p)
20125 {
20126 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20127 - IT_BYTEPOS (*it));
20128 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20129 }
20130 else
20131 it2.c = *p, it2.len = 1;
20132
20133 it2.glyph_row = NULL;
20134 it2.what = IT_CHARACTER;
20135 x_produce_glyphs (&it2);
20136 width = NUMVAL (prop) * it2.pixel_width;
20137 }
20138 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20139 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20140 {
20141 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20142 align_to = (align_to < 0
20143 ? 0
20144 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20145 else if (align_to < 0)
20146 align_to = window_box_left_offset (it->w, TEXT_AREA);
20147 width = max (0, (int)tem + align_to - it->current_x);
20148 zero_width_ok_p = 1;
20149 }
20150 else
20151 /* Nothing specified -> width defaults to canonical char width. */
20152 width = FRAME_COLUMN_WIDTH (it->f);
20153
20154 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20155 width = 1;
20156
20157 /* Compute height. */
20158 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20159 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20160 {
20161 height = (int)tem;
20162 zero_height_ok_p = 1;
20163 }
20164 else if (prop = Fplist_get (plist, QCrelative_height),
20165 NUMVAL (prop) > 0)
20166 height = FONT_HEIGHT (font) * NUMVAL (prop);
20167 else
20168 height = FONT_HEIGHT (font);
20169
20170 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20171 height = 1;
20172
20173 /* Compute percentage of height used for ascent. If
20174 `:ascent ASCENT' is present and valid, use that. Otherwise,
20175 derive the ascent from the font in use. */
20176 if (prop = Fplist_get (plist, QCascent),
20177 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20178 ascent = height * NUMVAL (prop) / 100.0;
20179 else if (!NILP (prop)
20180 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20181 ascent = min (max (0, (int)tem), height);
20182 else
20183 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20184
20185 if (width > 0 && !it->truncate_lines_p
20186 && it->current_x + width > it->last_visible_x)
20187 width = it->last_visible_x - it->current_x - 1;
20188
20189 if (width > 0 && height > 0 && it->glyph_row)
20190 {
20191 Lisp_Object object = it->stack[it->sp - 1].string;
20192 if (!STRINGP (object))
20193 object = it->w->buffer;
20194 append_stretch_glyph (it, object, width, height, ascent);
20195 }
20196
20197 it->pixel_width = width;
20198 it->ascent = it->phys_ascent = ascent;
20199 it->descent = it->phys_descent = height - it->ascent;
20200 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20201
20202 take_vertical_position_into_account (it);
20203 }
20204
20205 /* Get line-height and line-spacing property at point.
20206 If line-height has format (HEIGHT TOTAL), return TOTAL
20207 in TOTAL_HEIGHT. */
20208
20209 static Lisp_Object
20210 get_line_height_property (it, prop)
20211 struct it *it;
20212 Lisp_Object prop;
20213 {
20214 Lisp_Object position;
20215
20216 if (STRINGP (it->object))
20217 position = make_number (IT_STRING_CHARPOS (*it));
20218 else if (BUFFERP (it->object))
20219 position = make_number (IT_CHARPOS (*it));
20220 else
20221 return Qnil;
20222
20223 return Fget_char_property (position, prop, it->object);
20224 }
20225
20226 /* Calculate line-height and line-spacing properties.
20227 An integer value specifies explicit pixel value.
20228 A float value specifies relative value to current face height.
20229 A cons (float . face-name) specifies relative value to
20230 height of specified face font.
20231
20232 Returns height in pixels, or nil. */
20233
20234
20235 static Lisp_Object
20236 calc_line_height_property (it, val, font, boff, override)
20237 struct it *it;
20238 Lisp_Object val;
20239 XFontStruct *font;
20240 int boff, override;
20241 {
20242 Lisp_Object face_name = Qnil;
20243 int ascent, descent, height;
20244
20245 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20246 return val;
20247
20248 if (CONSP (val))
20249 {
20250 face_name = XCAR (val);
20251 val = XCDR (val);
20252 if (!NUMBERP (val))
20253 val = make_number (1);
20254 if (NILP (face_name))
20255 {
20256 height = it->ascent + it->descent;
20257 goto scale;
20258 }
20259 }
20260
20261 if (NILP (face_name))
20262 {
20263 font = FRAME_FONT (it->f);
20264 boff = FRAME_BASELINE_OFFSET (it->f);
20265 }
20266 else if (EQ (face_name, Qt))
20267 {
20268 override = 0;
20269 }
20270 else
20271 {
20272 int face_id;
20273 struct face *face;
20274 struct font_info *font_info;
20275
20276 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20277 if (face_id < 0)
20278 return make_number (-1);
20279
20280 face = FACE_FROM_ID (it->f, face_id);
20281 font = face->font;
20282 if (font == NULL)
20283 return make_number (-1);
20284
20285 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20286 boff = font_info->baseline_offset;
20287 if (font_info->vertical_centering)
20288 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20289 }
20290
20291 ascent = FONT_BASE (font) + boff;
20292 descent = FONT_DESCENT (font) - boff;
20293
20294 if (override)
20295 {
20296 it->override_ascent = ascent;
20297 it->override_descent = descent;
20298 it->override_boff = boff;
20299 }
20300
20301 height = ascent + descent;
20302
20303 scale:
20304 if (FLOATP (val))
20305 height = (int)(XFLOAT_DATA (val) * height);
20306 else if (INTEGERP (val))
20307 height *= XINT (val);
20308
20309 return make_number (height);
20310 }
20311
20312
20313 /* RIF:
20314 Produce glyphs/get display metrics for the display element IT is
20315 loaded with. See the description of struct it in dispextern.h
20316 for an overview of struct it. */
20317
20318 void
20319 x_produce_glyphs (it)
20320 struct it *it;
20321 {
20322 int extra_line_spacing = it->extra_line_spacing;
20323
20324 it->glyph_not_available_p = 0;
20325
20326 if (it->what == IT_CHARACTER)
20327 {
20328 XChar2b char2b;
20329 XFontStruct *font;
20330 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20331 XCharStruct *pcm;
20332 int font_not_found_p;
20333 struct font_info *font_info;
20334 int boff; /* baseline offset */
20335 /* We may change it->multibyte_p upon unibyte<->multibyte
20336 conversion. So, save the current value now and restore it
20337 later.
20338
20339 Note: It seems that we don't have to record multibyte_p in
20340 struct glyph because the character code itself tells if or
20341 not the character is multibyte. Thus, in the future, we must
20342 consider eliminating the field `multibyte_p' in the struct
20343 glyph. */
20344 int saved_multibyte_p = it->multibyte_p;
20345
20346 /* Maybe translate single-byte characters to multibyte, or the
20347 other way. */
20348 it->char_to_display = it->c;
20349 if (!ASCII_BYTE_P (it->c))
20350 {
20351 if (unibyte_display_via_language_environment
20352 && SINGLE_BYTE_CHAR_P (it->c)
20353 && (it->c >= 0240
20354 || !NILP (Vnonascii_translation_table)))
20355 {
20356 it->char_to_display = unibyte_char_to_multibyte (it->c);
20357 it->multibyte_p = 1;
20358 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20359 face = FACE_FROM_ID (it->f, it->face_id);
20360 }
20361 else if (!SINGLE_BYTE_CHAR_P (it->c)
20362 && !it->multibyte_p)
20363 {
20364 it->multibyte_p = 1;
20365 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20366 face = FACE_FROM_ID (it->f, it->face_id);
20367 }
20368 }
20369
20370 /* Get font to use. Encode IT->char_to_display. */
20371 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20372 &char2b, it->multibyte_p, 0);
20373 font = face->font;
20374
20375 /* When no suitable font found, use the default font. */
20376 font_not_found_p = font == NULL;
20377 if (font_not_found_p)
20378 {
20379 font = FRAME_FONT (it->f);
20380 boff = FRAME_BASELINE_OFFSET (it->f);
20381 font_info = NULL;
20382 }
20383 else
20384 {
20385 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20386 boff = font_info->baseline_offset;
20387 if (font_info->vertical_centering)
20388 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20389 }
20390
20391 if (it->char_to_display >= ' '
20392 && (!it->multibyte_p || it->char_to_display < 128))
20393 {
20394 /* Either unibyte or ASCII. */
20395 int stretched_p;
20396
20397 it->nglyphs = 1;
20398
20399 pcm = rif->per_char_metric (font, &char2b,
20400 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20401
20402 if (it->override_ascent >= 0)
20403 {
20404 it->ascent = it->override_ascent;
20405 it->descent = it->override_descent;
20406 boff = it->override_boff;
20407 }
20408 else
20409 {
20410 it->ascent = FONT_BASE (font) + boff;
20411 it->descent = FONT_DESCENT (font) - boff;
20412 }
20413
20414 if (pcm)
20415 {
20416 it->phys_ascent = pcm->ascent + boff;
20417 it->phys_descent = pcm->descent - boff;
20418 it->pixel_width = pcm->width;
20419 }
20420 else
20421 {
20422 it->glyph_not_available_p = 1;
20423 it->phys_ascent = it->ascent;
20424 it->phys_descent = it->descent;
20425 it->pixel_width = FONT_WIDTH (font);
20426 }
20427
20428 if (it->constrain_row_ascent_descent_p)
20429 {
20430 if (it->descent > it->max_descent)
20431 {
20432 it->ascent += it->descent - it->max_descent;
20433 it->descent = it->max_descent;
20434 }
20435 if (it->ascent > it->max_ascent)
20436 {
20437 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20438 it->ascent = it->max_ascent;
20439 }
20440 it->phys_ascent = min (it->phys_ascent, it->ascent);
20441 it->phys_descent = min (it->phys_descent, it->descent);
20442 extra_line_spacing = 0;
20443 }
20444
20445 /* If this is a space inside a region of text with
20446 `space-width' property, change its width. */
20447 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20448 if (stretched_p)
20449 it->pixel_width *= XFLOATINT (it->space_width);
20450
20451 /* If face has a box, add the box thickness to the character
20452 height. If character has a box line to the left and/or
20453 right, add the box line width to the character's width. */
20454 if (face->box != FACE_NO_BOX)
20455 {
20456 int thick = face->box_line_width;
20457
20458 if (thick > 0)
20459 {
20460 it->ascent += thick;
20461 it->descent += thick;
20462 }
20463 else
20464 thick = -thick;
20465
20466 if (it->start_of_box_run_p)
20467 it->pixel_width += thick;
20468 if (it->end_of_box_run_p)
20469 it->pixel_width += thick;
20470 }
20471
20472 /* If face has an overline, add the height of the overline
20473 (1 pixel) and a 1 pixel margin to the character height. */
20474 if (face->overline_p)
20475 it->ascent += overline_margin;
20476
20477 if (it->constrain_row_ascent_descent_p)
20478 {
20479 if (it->ascent > it->max_ascent)
20480 it->ascent = it->max_ascent;
20481 if (it->descent > it->max_descent)
20482 it->descent = it->max_descent;
20483 }
20484
20485 take_vertical_position_into_account (it);
20486
20487 /* If we have to actually produce glyphs, do it. */
20488 if (it->glyph_row)
20489 {
20490 if (stretched_p)
20491 {
20492 /* Translate a space with a `space-width' property
20493 into a stretch glyph. */
20494 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20495 / FONT_HEIGHT (font));
20496 append_stretch_glyph (it, it->object, it->pixel_width,
20497 it->ascent + it->descent, ascent);
20498 }
20499 else
20500 append_glyph (it);
20501
20502 /* If characters with lbearing or rbearing are displayed
20503 in this line, record that fact in a flag of the
20504 glyph row. This is used to optimize X output code. */
20505 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20506 it->glyph_row->contains_overlapping_glyphs_p = 1;
20507 }
20508 }
20509 else if (it->char_to_display == '\n')
20510 {
20511 /* A newline has no width but we need the height of the line.
20512 But if previous part of the line set a height, don't
20513 increase that height */
20514
20515 Lisp_Object height;
20516 Lisp_Object total_height = Qnil;
20517
20518 it->override_ascent = -1;
20519 it->pixel_width = 0;
20520 it->nglyphs = 0;
20521
20522 height = get_line_height_property(it, Qline_height);
20523 /* Split (line-height total-height) list */
20524 if (CONSP (height)
20525 && CONSP (XCDR (height))
20526 && NILP (XCDR (XCDR (height))))
20527 {
20528 total_height = XCAR (XCDR (height));
20529 height = XCAR (height);
20530 }
20531 height = calc_line_height_property(it, height, font, boff, 1);
20532
20533 if (it->override_ascent >= 0)
20534 {
20535 it->ascent = it->override_ascent;
20536 it->descent = it->override_descent;
20537 boff = it->override_boff;
20538 }
20539 else
20540 {
20541 it->ascent = FONT_BASE (font) + boff;
20542 it->descent = FONT_DESCENT (font) - boff;
20543 }
20544
20545 if (EQ (height, Qt))
20546 {
20547 if (it->descent > it->max_descent)
20548 {
20549 it->ascent += it->descent - it->max_descent;
20550 it->descent = it->max_descent;
20551 }
20552 if (it->ascent > it->max_ascent)
20553 {
20554 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20555 it->ascent = it->max_ascent;
20556 }
20557 it->phys_ascent = min (it->phys_ascent, it->ascent);
20558 it->phys_descent = min (it->phys_descent, it->descent);
20559 it->constrain_row_ascent_descent_p = 1;
20560 extra_line_spacing = 0;
20561 }
20562 else
20563 {
20564 Lisp_Object spacing;
20565
20566 it->phys_ascent = it->ascent;
20567 it->phys_descent = it->descent;
20568
20569 if ((it->max_ascent > 0 || it->max_descent > 0)
20570 && face->box != FACE_NO_BOX
20571 && face->box_line_width > 0)
20572 {
20573 it->ascent += face->box_line_width;
20574 it->descent += face->box_line_width;
20575 }
20576 if (!NILP (height)
20577 && XINT (height) > it->ascent + it->descent)
20578 it->ascent = XINT (height) - it->descent;
20579
20580 if (!NILP (total_height))
20581 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20582 else
20583 {
20584 spacing = get_line_height_property(it, Qline_spacing);
20585 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20586 }
20587 if (INTEGERP (spacing))
20588 {
20589 extra_line_spacing = XINT (spacing);
20590 if (!NILP (total_height))
20591 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20592 }
20593 }
20594 }
20595 else if (it->char_to_display == '\t')
20596 {
20597 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20598 int x = it->current_x + it->continuation_lines_width;
20599 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20600
20601 /* If the distance from the current position to the next tab
20602 stop is less than a space character width, use the
20603 tab stop after that. */
20604 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20605 next_tab_x += tab_width;
20606
20607 it->pixel_width = next_tab_x - x;
20608 it->nglyphs = 1;
20609 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20610 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20611
20612 if (it->glyph_row)
20613 {
20614 append_stretch_glyph (it, it->object, it->pixel_width,
20615 it->ascent + it->descent, it->ascent);
20616 }
20617 }
20618 else
20619 {
20620 /* A multi-byte character. Assume that the display width of the
20621 character is the width of the character multiplied by the
20622 width of the font. */
20623
20624 /* If we found a font, this font should give us the right
20625 metrics. If we didn't find a font, use the frame's
20626 default font and calculate the width of the character
20627 from the charset width; this is what old redisplay code
20628 did. */
20629
20630 pcm = rif->per_char_metric (font, &char2b,
20631 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20632
20633 if (font_not_found_p || !pcm)
20634 {
20635 int charset = CHAR_CHARSET (it->char_to_display);
20636
20637 it->glyph_not_available_p = 1;
20638 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20639 * CHARSET_WIDTH (charset));
20640 it->phys_ascent = FONT_BASE (font) + boff;
20641 it->phys_descent = FONT_DESCENT (font) - boff;
20642 }
20643 else
20644 {
20645 it->pixel_width = pcm->width;
20646 it->phys_ascent = pcm->ascent + boff;
20647 it->phys_descent = pcm->descent - boff;
20648 if (it->glyph_row
20649 && (pcm->lbearing < 0
20650 || pcm->rbearing > pcm->width))
20651 it->glyph_row->contains_overlapping_glyphs_p = 1;
20652 }
20653 it->nglyphs = 1;
20654 it->ascent = FONT_BASE (font) + boff;
20655 it->descent = FONT_DESCENT (font) - boff;
20656 if (face->box != FACE_NO_BOX)
20657 {
20658 int thick = face->box_line_width;
20659
20660 if (thick > 0)
20661 {
20662 it->ascent += thick;
20663 it->descent += thick;
20664 }
20665 else
20666 thick = - thick;
20667
20668 if (it->start_of_box_run_p)
20669 it->pixel_width += thick;
20670 if (it->end_of_box_run_p)
20671 it->pixel_width += thick;
20672 }
20673
20674 /* If face has an overline, add the height of the overline
20675 (1 pixel) and a 1 pixel margin to the character height. */
20676 if (face->overline_p)
20677 it->ascent += overline_margin;
20678
20679 take_vertical_position_into_account (it);
20680
20681 if (it->glyph_row)
20682 append_glyph (it);
20683 }
20684 it->multibyte_p = saved_multibyte_p;
20685 }
20686 else if (it->what == IT_COMPOSITION)
20687 {
20688 /* Note: A composition is represented as one glyph in the
20689 glyph matrix. There are no padding glyphs. */
20690 XChar2b char2b;
20691 XFontStruct *font;
20692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20693 XCharStruct *pcm;
20694 int font_not_found_p;
20695 struct font_info *font_info;
20696 int boff; /* baseline offset */
20697 struct composition *cmp = composition_table[it->cmp_id];
20698
20699 /* Maybe translate single-byte characters to multibyte. */
20700 it->char_to_display = it->c;
20701 if (unibyte_display_via_language_environment
20702 && SINGLE_BYTE_CHAR_P (it->c)
20703 && (it->c >= 0240
20704 || (it->c >= 0200
20705 && !NILP (Vnonascii_translation_table))))
20706 {
20707 it->char_to_display = unibyte_char_to_multibyte (it->c);
20708 }
20709
20710 /* Get face and font to use. Encode IT->char_to_display. */
20711 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20712 face = FACE_FROM_ID (it->f, it->face_id);
20713 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20714 &char2b, it->multibyte_p, 0);
20715 font = face->font;
20716
20717 /* When no suitable font found, use the default font. */
20718 font_not_found_p = font == NULL;
20719 if (font_not_found_p)
20720 {
20721 font = FRAME_FONT (it->f);
20722 boff = FRAME_BASELINE_OFFSET (it->f);
20723 font_info = NULL;
20724 }
20725 else
20726 {
20727 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20728 boff = font_info->baseline_offset;
20729 if (font_info->vertical_centering)
20730 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20731 }
20732
20733 /* There are no padding glyphs, so there is only one glyph to
20734 produce for the composition. Important is that pixel_width,
20735 ascent and descent are the values of what is drawn by
20736 draw_glyphs (i.e. the values of the overall glyphs composed). */
20737 it->nglyphs = 1;
20738
20739 /* If we have not yet calculated pixel size data of glyphs of
20740 the composition for the current face font, calculate them
20741 now. Theoretically, we have to check all fonts for the
20742 glyphs, but that requires much time and memory space. So,
20743 here we check only the font of the first glyph. This leads
20744 to incorrect display very rarely, and C-l (recenter) can
20745 correct the display anyway. */
20746 if (cmp->font != (void *) font)
20747 {
20748 /* Ascent and descent of the font of the first character of
20749 this composition (adjusted by baseline offset). Ascent
20750 and descent of overall glyphs should not be less than
20751 them respectively. */
20752 int font_ascent = FONT_BASE (font) + boff;
20753 int font_descent = FONT_DESCENT (font) - boff;
20754 /* Bounding box of the overall glyphs. */
20755 int leftmost, rightmost, lowest, highest;
20756 int i, width, ascent, descent;
20757
20758 cmp->font = (void *) font;
20759
20760 /* Initialize the bounding box. */
20761 if (font_info
20762 && (pcm = rif->per_char_metric (font, &char2b,
20763 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20764 {
20765 width = pcm->width;
20766 ascent = pcm->ascent;
20767 descent = pcm->descent;
20768 }
20769 else
20770 {
20771 width = FONT_WIDTH (font);
20772 ascent = FONT_BASE (font);
20773 descent = FONT_DESCENT (font);
20774 }
20775
20776 rightmost = width;
20777 lowest = - descent + boff;
20778 highest = ascent + boff;
20779 leftmost = 0;
20780
20781 if (font_info
20782 && font_info->default_ascent
20783 && CHAR_TABLE_P (Vuse_default_ascent)
20784 && !NILP (Faref (Vuse_default_ascent,
20785 make_number (it->char_to_display))))
20786 highest = font_info->default_ascent + boff;
20787
20788 /* Draw the first glyph at the normal position. It may be
20789 shifted to right later if some other glyphs are drawn at
20790 the left. */
20791 cmp->offsets[0] = 0;
20792 cmp->offsets[1] = boff;
20793
20794 /* Set cmp->offsets for the remaining glyphs. */
20795 for (i = 1; i < cmp->glyph_len; i++)
20796 {
20797 int left, right, btm, top;
20798 int ch = COMPOSITION_GLYPH (cmp, i);
20799 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20800
20801 face = FACE_FROM_ID (it->f, face_id);
20802 get_char_face_and_encoding (it->f, ch, face->id,
20803 &char2b, it->multibyte_p, 0);
20804 font = face->font;
20805 if (font == NULL)
20806 {
20807 font = FRAME_FONT (it->f);
20808 boff = FRAME_BASELINE_OFFSET (it->f);
20809 font_info = NULL;
20810 }
20811 else
20812 {
20813 font_info
20814 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20815 boff = font_info->baseline_offset;
20816 if (font_info->vertical_centering)
20817 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20818 }
20819
20820 if (font_info
20821 && (pcm = rif->per_char_metric (font, &char2b,
20822 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20823 {
20824 width = pcm->width;
20825 ascent = pcm->ascent;
20826 descent = pcm->descent;
20827 }
20828 else
20829 {
20830 width = FONT_WIDTH (font);
20831 ascent = 1;
20832 descent = 0;
20833 }
20834
20835 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20836 {
20837 /* Relative composition with or without
20838 alternate chars. */
20839 left = (leftmost + rightmost - width) / 2;
20840 btm = - descent + boff;
20841 if (font_info && font_info->relative_compose
20842 && (! CHAR_TABLE_P (Vignore_relative_composition)
20843 || NILP (Faref (Vignore_relative_composition,
20844 make_number (ch)))))
20845 {
20846
20847 if (- descent >= font_info->relative_compose)
20848 /* One extra pixel between two glyphs. */
20849 btm = highest + 1;
20850 else if (ascent <= 0)
20851 /* One extra pixel between two glyphs. */
20852 btm = lowest - 1 - ascent - descent;
20853 }
20854 }
20855 else
20856 {
20857 /* A composition rule is specified by an integer
20858 value that encodes global and new reference
20859 points (GREF and NREF). GREF and NREF are
20860 specified by numbers as below:
20861
20862 0---1---2 -- ascent
20863 | |
20864 | |
20865 | |
20866 9--10--11 -- center
20867 | |
20868 ---3---4---5--- baseline
20869 | |
20870 6---7---8 -- descent
20871 */
20872 int rule = COMPOSITION_RULE (cmp, i);
20873 int gref, nref, grefx, grefy, nrefx, nrefy;
20874
20875 COMPOSITION_DECODE_RULE (rule, gref, nref);
20876 grefx = gref % 3, nrefx = nref % 3;
20877 grefy = gref / 3, nrefy = nref / 3;
20878
20879 left = (leftmost
20880 + grefx * (rightmost - leftmost) / 2
20881 - nrefx * width / 2);
20882 btm = ((grefy == 0 ? highest
20883 : grefy == 1 ? 0
20884 : grefy == 2 ? lowest
20885 : (highest + lowest) / 2)
20886 - (nrefy == 0 ? ascent + descent
20887 : nrefy == 1 ? descent - boff
20888 : nrefy == 2 ? 0
20889 : (ascent + descent) / 2));
20890 }
20891
20892 cmp->offsets[i * 2] = left;
20893 cmp->offsets[i * 2 + 1] = btm + descent;
20894
20895 /* Update the bounding box of the overall glyphs. */
20896 right = left + width;
20897 top = btm + descent + ascent;
20898 if (left < leftmost)
20899 leftmost = left;
20900 if (right > rightmost)
20901 rightmost = right;
20902 if (top > highest)
20903 highest = top;
20904 if (btm < lowest)
20905 lowest = btm;
20906 }
20907
20908 /* If there are glyphs whose x-offsets are negative,
20909 shift all glyphs to the right and make all x-offsets
20910 non-negative. */
20911 if (leftmost < 0)
20912 {
20913 for (i = 0; i < cmp->glyph_len; i++)
20914 cmp->offsets[i * 2] -= leftmost;
20915 rightmost -= leftmost;
20916 }
20917
20918 cmp->pixel_width = rightmost;
20919 cmp->ascent = highest;
20920 cmp->descent = - lowest;
20921 if (cmp->ascent < font_ascent)
20922 cmp->ascent = font_ascent;
20923 if (cmp->descent < font_descent)
20924 cmp->descent = font_descent;
20925 }
20926
20927 it->pixel_width = cmp->pixel_width;
20928 it->ascent = it->phys_ascent = cmp->ascent;
20929 it->descent = it->phys_descent = cmp->descent;
20930
20931 if (face->box != FACE_NO_BOX)
20932 {
20933 int thick = face->box_line_width;
20934
20935 if (thick > 0)
20936 {
20937 it->ascent += thick;
20938 it->descent += thick;
20939 }
20940 else
20941 thick = - thick;
20942
20943 if (it->start_of_box_run_p)
20944 it->pixel_width += thick;
20945 if (it->end_of_box_run_p)
20946 it->pixel_width += thick;
20947 }
20948
20949 /* If face has an overline, add the height of the overline
20950 (1 pixel) and a 1 pixel margin to the character height. */
20951 if (face->overline_p)
20952 it->ascent += overline_margin;
20953
20954 take_vertical_position_into_account (it);
20955
20956 if (it->glyph_row)
20957 append_composite_glyph (it);
20958 }
20959 else if (it->what == IT_IMAGE)
20960 produce_image_glyph (it);
20961 else if (it->what == IT_STRETCH)
20962 produce_stretch_glyph (it);
20963
20964 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20965 because this isn't true for images with `:ascent 100'. */
20966 xassert (it->ascent >= 0 && it->descent >= 0);
20967 if (it->area == TEXT_AREA)
20968 it->current_x += it->pixel_width;
20969
20970 if (extra_line_spacing > 0)
20971 {
20972 it->descent += extra_line_spacing;
20973 if (extra_line_spacing > it->max_extra_line_spacing)
20974 it->max_extra_line_spacing = extra_line_spacing;
20975 }
20976
20977 it->max_ascent = max (it->max_ascent, it->ascent);
20978 it->max_descent = max (it->max_descent, it->descent);
20979 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20980 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20981 }
20982
20983 /* EXPORT for RIF:
20984 Output LEN glyphs starting at START at the nominal cursor position.
20985 Advance the nominal cursor over the text. The global variable
20986 updated_window contains the window being updated, updated_row is
20987 the glyph row being updated, and updated_area is the area of that
20988 row being updated. */
20989
20990 void
20991 x_write_glyphs (start, len)
20992 struct glyph *start;
20993 int len;
20994 {
20995 int x, hpos;
20996
20997 xassert (updated_window && updated_row);
20998 BLOCK_INPUT;
20999
21000 /* Write glyphs. */
21001
21002 hpos = start - updated_row->glyphs[updated_area];
21003 x = draw_glyphs (updated_window, output_cursor.x,
21004 updated_row, updated_area,
21005 hpos, hpos + len,
21006 DRAW_NORMAL_TEXT, 0);
21007
21008 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21009 if (updated_area == TEXT_AREA
21010 && updated_window->phys_cursor_on_p
21011 && updated_window->phys_cursor.vpos == output_cursor.vpos
21012 && updated_window->phys_cursor.hpos >= hpos
21013 && updated_window->phys_cursor.hpos < hpos + len)
21014 updated_window->phys_cursor_on_p = 0;
21015
21016 UNBLOCK_INPUT;
21017
21018 /* Advance the output cursor. */
21019 output_cursor.hpos += len;
21020 output_cursor.x = x;
21021 }
21022
21023
21024 /* EXPORT for RIF:
21025 Insert LEN glyphs from START at the nominal cursor position. */
21026
21027 void
21028 x_insert_glyphs (start, len)
21029 struct glyph *start;
21030 int len;
21031 {
21032 struct frame *f;
21033 struct window *w;
21034 int line_height, shift_by_width, shifted_region_width;
21035 struct glyph_row *row;
21036 struct glyph *glyph;
21037 int frame_x, frame_y, hpos;
21038
21039 xassert (updated_window && updated_row);
21040 BLOCK_INPUT;
21041 w = updated_window;
21042 f = XFRAME (WINDOW_FRAME (w));
21043
21044 /* Get the height of the line we are in. */
21045 row = updated_row;
21046 line_height = row->height;
21047
21048 /* Get the width of the glyphs to insert. */
21049 shift_by_width = 0;
21050 for (glyph = start; glyph < start + len; ++glyph)
21051 shift_by_width += glyph->pixel_width;
21052
21053 /* Get the width of the region to shift right. */
21054 shifted_region_width = (window_box_width (w, updated_area)
21055 - output_cursor.x
21056 - shift_by_width);
21057
21058 /* Shift right. */
21059 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21060 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21061
21062 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21063 line_height, shift_by_width);
21064
21065 /* Write the glyphs. */
21066 hpos = start - row->glyphs[updated_area];
21067 draw_glyphs (w, output_cursor.x, row, updated_area,
21068 hpos, hpos + len,
21069 DRAW_NORMAL_TEXT, 0);
21070
21071 /* Advance the output cursor. */
21072 output_cursor.hpos += len;
21073 output_cursor.x += shift_by_width;
21074 UNBLOCK_INPUT;
21075 }
21076
21077
21078 /* EXPORT for RIF:
21079 Erase the current text line from the nominal cursor position
21080 (inclusive) to pixel column TO_X (exclusive). The idea is that
21081 everything from TO_X onward is already erased.
21082
21083 TO_X is a pixel position relative to updated_area of
21084 updated_window. TO_X == -1 means clear to the end of this area. */
21085
21086 void
21087 x_clear_end_of_line (to_x)
21088 int to_x;
21089 {
21090 struct frame *f;
21091 struct window *w = updated_window;
21092 int max_x, min_y, max_y;
21093 int from_x, from_y, to_y;
21094
21095 xassert (updated_window && updated_row);
21096 f = XFRAME (w->frame);
21097
21098 if (updated_row->full_width_p)
21099 max_x = WINDOW_TOTAL_WIDTH (w);
21100 else
21101 max_x = window_box_width (w, updated_area);
21102 max_y = window_text_bottom_y (w);
21103
21104 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21105 of window. For TO_X > 0, truncate to end of drawing area. */
21106 if (to_x == 0)
21107 return;
21108 else if (to_x < 0)
21109 to_x = max_x;
21110 else
21111 to_x = min (to_x, max_x);
21112
21113 to_y = min (max_y, output_cursor.y + updated_row->height);
21114
21115 /* Notice if the cursor will be cleared by this operation. */
21116 if (!updated_row->full_width_p)
21117 notice_overwritten_cursor (w, updated_area,
21118 output_cursor.x, -1,
21119 updated_row->y,
21120 MATRIX_ROW_BOTTOM_Y (updated_row));
21121
21122 from_x = output_cursor.x;
21123
21124 /* Translate to frame coordinates. */
21125 if (updated_row->full_width_p)
21126 {
21127 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21128 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21129 }
21130 else
21131 {
21132 int area_left = window_box_left (w, updated_area);
21133 from_x += area_left;
21134 to_x += area_left;
21135 }
21136
21137 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21138 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21139 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21140
21141 /* Prevent inadvertently clearing to end of the X window. */
21142 if (to_x > from_x && to_y > from_y)
21143 {
21144 BLOCK_INPUT;
21145 rif->clear_frame_area (f, from_x, from_y,
21146 to_x - from_x, to_y - from_y);
21147 UNBLOCK_INPUT;
21148 }
21149 }
21150
21151 #endif /* HAVE_WINDOW_SYSTEM */
21152
21153
21154 \f
21155 /***********************************************************************
21156 Cursor types
21157 ***********************************************************************/
21158
21159 /* Value is the internal representation of the specified cursor type
21160 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21161 of the bar cursor. */
21162
21163 static enum text_cursor_kinds
21164 get_specified_cursor_type (arg, width)
21165 Lisp_Object arg;
21166 int *width;
21167 {
21168 enum text_cursor_kinds type;
21169
21170 if (NILP (arg))
21171 return NO_CURSOR;
21172
21173 if (EQ (arg, Qbox))
21174 return FILLED_BOX_CURSOR;
21175
21176 if (EQ (arg, Qhollow))
21177 return HOLLOW_BOX_CURSOR;
21178
21179 if (EQ (arg, Qbar))
21180 {
21181 *width = 2;
21182 return BAR_CURSOR;
21183 }
21184
21185 if (CONSP (arg)
21186 && EQ (XCAR (arg), Qbar)
21187 && INTEGERP (XCDR (arg))
21188 && XINT (XCDR (arg)) >= 0)
21189 {
21190 *width = XINT (XCDR (arg));
21191 return BAR_CURSOR;
21192 }
21193
21194 if (EQ (arg, Qhbar))
21195 {
21196 *width = 2;
21197 return HBAR_CURSOR;
21198 }
21199
21200 if (CONSP (arg)
21201 && EQ (XCAR (arg), Qhbar)
21202 && INTEGERP (XCDR (arg))
21203 && XINT (XCDR (arg)) >= 0)
21204 {
21205 *width = XINT (XCDR (arg));
21206 return HBAR_CURSOR;
21207 }
21208
21209 /* Treat anything unknown as "hollow box cursor".
21210 It was bad to signal an error; people have trouble fixing
21211 .Xdefaults with Emacs, when it has something bad in it. */
21212 type = HOLLOW_BOX_CURSOR;
21213
21214 return type;
21215 }
21216
21217 /* Set the default cursor types for specified frame. */
21218 void
21219 set_frame_cursor_types (f, arg)
21220 struct frame *f;
21221 Lisp_Object arg;
21222 {
21223 int width;
21224 Lisp_Object tem;
21225
21226 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21227 FRAME_CURSOR_WIDTH (f) = width;
21228
21229 /* By default, set up the blink-off state depending on the on-state. */
21230
21231 tem = Fassoc (arg, Vblink_cursor_alist);
21232 if (!NILP (tem))
21233 {
21234 FRAME_BLINK_OFF_CURSOR (f)
21235 = get_specified_cursor_type (XCDR (tem), &width);
21236 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21237 }
21238 else
21239 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21240 }
21241
21242
21243 /* Return the cursor we want to be displayed in window W. Return
21244 width of bar/hbar cursor through WIDTH arg. Return with
21245 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21246 (i.e. if the `system caret' should track this cursor).
21247
21248 In a mini-buffer window, we want the cursor only to appear if we
21249 are reading input from this window. For the selected window, we
21250 want the cursor type given by the frame parameter or buffer local
21251 setting of cursor-type. If explicitly marked off, draw no cursor.
21252 In all other cases, we want a hollow box cursor. */
21253
21254 static enum text_cursor_kinds
21255 get_window_cursor_type (w, glyph, width, active_cursor)
21256 struct window *w;
21257 struct glyph *glyph;
21258 int *width;
21259 int *active_cursor;
21260 {
21261 struct frame *f = XFRAME (w->frame);
21262 struct buffer *b = XBUFFER (w->buffer);
21263 int cursor_type = DEFAULT_CURSOR;
21264 Lisp_Object alt_cursor;
21265 int non_selected = 0;
21266
21267 *active_cursor = 1;
21268
21269 /* Echo area */
21270 if (cursor_in_echo_area
21271 && FRAME_HAS_MINIBUF_P (f)
21272 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21273 {
21274 if (w == XWINDOW (echo_area_window))
21275 {
21276 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21277 {
21278 *width = FRAME_CURSOR_WIDTH (f);
21279 return FRAME_DESIRED_CURSOR (f);
21280 }
21281 else
21282 return get_specified_cursor_type (b->cursor_type, width);
21283 }
21284
21285 *active_cursor = 0;
21286 non_selected = 1;
21287 }
21288
21289 /* Nonselected window or nonselected frame. */
21290 else if (w != XWINDOW (f->selected_window)
21291 #ifdef HAVE_WINDOW_SYSTEM
21292 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21293 #endif
21294 )
21295 {
21296 *active_cursor = 0;
21297
21298 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21299 return NO_CURSOR;
21300
21301 non_selected = 1;
21302 }
21303
21304 /* Never display a cursor in a window in which cursor-type is nil. */
21305 if (NILP (b->cursor_type))
21306 return NO_CURSOR;
21307
21308 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21309 if (non_selected)
21310 {
21311 alt_cursor = b->cursor_in_non_selected_windows;
21312 return get_specified_cursor_type (alt_cursor, width);
21313 }
21314
21315 /* Get the normal cursor type for this window. */
21316 if (EQ (b->cursor_type, Qt))
21317 {
21318 cursor_type = FRAME_DESIRED_CURSOR (f);
21319 *width = FRAME_CURSOR_WIDTH (f);
21320 }
21321 else
21322 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21323
21324 /* Use normal cursor if not blinked off. */
21325 if (!w->cursor_off_p)
21326 {
21327 #ifdef HAVE_WINDOW_SYSTEM
21328 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21329 {
21330 if (cursor_type == FILLED_BOX_CURSOR)
21331 {
21332 /* Using a block cursor on large images can be very annoying.
21333 So use a hollow cursor for "large" images.
21334 If image is not transparent (no mask), also use hollow cursor. */
21335 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21336 if (img != NULL && IMAGEP (img->spec))
21337 {
21338 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21339 where N = size of default frame font size.
21340 This should cover most of the "tiny" icons people may use. */
21341 if (!img->mask
21342 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21343 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21344 cursor_type = HOLLOW_BOX_CURSOR;
21345 }
21346 }
21347 else if (cursor_type != NO_CURSOR)
21348 {
21349 /* Display current only supports BOX and HOLLOW cursors for images.
21350 So for now, unconditionally use a HOLLOW cursor when cursor is
21351 not a solid box cursor. */
21352 cursor_type = HOLLOW_BOX_CURSOR;
21353 }
21354 }
21355 #endif
21356 return cursor_type;
21357 }
21358
21359 /* Cursor is blinked off, so determine how to "toggle" it. */
21360
21361 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21362 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21363 return get_specified_cursor_type (XCDR (alt_cursor), width);
21364
21365 /* Then see if frame has specified a specific blink off cursor type. */
21366 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21367 {
21368 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21369 return FRAME_BLINK_OFF_CURSOR (f);
21370 }
21371
21372 #if 0
21373 /* Some people liked having a permanently visible blinking cursor,
21374 while others had very strong opinions against it. So it was
21375 decided to remove it. KFS 2003-09-03 */
21376
21377 /* Finally perform built-in cursor blinking:
21378 filled box <-> hollow box
21379 wide [h]bar <-> narrow [h]bar
21380 narrow [h]bar <-> no cursor
21381 other type <-> no cursor */
21382
21383 if (cursor_type == FILLED_BOX_CURSOR)
21384 return HOLLOW_BOX_CURSOR;
21385
21386 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21387 {
21388 *width = 1;
21389 return cursor_type;
21390 }
21391 #endif
21392
21393 return NO_CURSOR;
21394 }
21395
21396
21397 #ifdef HAVE_WINDOW_SYSTEM
21398
21399 /* Notice when the text cursor of window W has been completely
21400 overwritten by a drawing operation that outputs glyphs in AREA
21401 starting at X0 and ending at X1 in the line starting at Y0 and
21402 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21403 the rest of the line after X0 has been written. Y coordinates
21404 are window-relative. */
21405
21406 static void
21407 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21408 struct window *w;
21409 enum glyph_row_area area;
21410 int x0, y0, x1, y1;
21411 {
21412 int cx0, cx1, cy0, cy1;
21413 struct glyph_row *row;
21414
21415 if (!w->phys_cursor_on_p)
21416 return;
21417 if (area != TEXT_AREA)
21418 return;
21419
21420 if (w->phys_cursor.vpos < 0
21421 || w->phys_cursor.vpos >= w->current_matrix->nrows
21422 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21423 !(row->enabled_p && row->displays_text_p)))
21424 return;
21425
21426 if (row->cursor_in_fringe_p)
21427 {
21428 row->cursor_in_fringe_p = 0;
21429 draw_fringe_bitmap (w, row, 0);
21430 w->phys_cursor_on_p = 0;
21431 return;
21432 }
21433
21434 cx0 = w->phys_cursor.x;
21435 cx1 = cx0 + w->phys_cursor_width;
21436 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21437 return;
21438
21439 /* The cursor image will be completely removed from the
21440 screen if the output area intersects the cursor area in
21441 y-direction. When we draw in [y0 y1[, and some part of
21442 the cursor is at y < y0, that part must have been drawn
21443 before. When scrolling, the cursor is erased before
21444 actually scrolling, so we don't come here. When not
21445 scrolling, the rows above the old cursor row must have
21446 changed, and in this case these rows must have written
21447 over the cursor image.
21448
21449 Likewise if part of the cursor is below y1, with the
21450 exception of the cursor being in the first blank row at
21451 the buffer and window end because update_text_area
21452 doesn't draw that row. (Except when it does, but
21453 that's handled in update_text_area.) */
21454
21455 cy0 = w->phys_cursor.y;
21456 cy1 = cy0 + w->phys_cursor_height;
21457 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21458 return;
21459
21460 w->phys_cursor_on_p = 0;
21461 }
21462
21463 #endif /* HAVE_WINDOW_SYSTEM */
21464
21465 \f
21466 /************************************************************************
21467 Mouse Face
21468 ************************************************************************/
21469
21470 #ifdef HAVE_WINDOW_SYSTEM
21471
21472 /* EXPORT for RIF:
21473 Fix the display of area AREA of overlapping row ROW in window W
21474 with respect to the overlapping part OVERLAPS. */
21475
21476 void
21477 x_fix_overlapping_area (w, row, area, overlaps)
21478 struct window *w;
21479 struct glyph_row *row;
21480 enum glyph_row_area area;
21481 int overlaps;
21482 {
21483 int i, x;
21484
21485 BLOCK_INPUT;
21486
21487 x = 0;
21488 for (i = 0; i < row->used[area];)
21489 {
21490 if (row->glyphs[area][i].overlaps_vertically_p)
21491 {
21492 int start = i, start_x = x;
21493
21494 do
21495 {
21496 x += row->glyphs[area][i].pixel_width;
21497 ++i;
21498 }
21499 while (i < row->used[area]
21500 && row->glyphs[area][i].overlaps_vertically_p);
21501
21502 draw_glyphs (w, start_x, row, area,
21503 start, i,
21504 DRAW_NORMAL_TEXT, overlaps);
21505 }
21506 else
21507 {
21508 x += row->glyphs[area][i].pixel_width;
21509 ++i;
21510 }
21511 }
21512
21513 UNBLOCK_INPUT;
21514 }
21515
21516
21517 /* EXPORT:
21518 Draw the cursor glyph of window W in glyph row ROW. See the
21519 comment of draw_glyphs for the meaning of HL. */
21520
21521 void
21522 draw_phys_cursor_glyph (w, row, hl)
21523 struct window *w;
21524 struct glyph_row *row;
21525 enum draw_glyphs_face hl;
21526 {
21527 /* If cursor hpos is out of bounds, don't draw garbage. This can
21528 happen in mini-buffer windows when switching between echo area
21529 glyphs and mini-buffer. */
21530 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21531 {
21532 int on_p = w->phys_cursor_on_p;
21533 int x1;
21534 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21535 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21536 hl, 0);
21537 w->phys_cursor_on_p = on_p;
21538
21539 if (hl == DRAW_CURSOR)
21540 w->phys_cursor_width = x1 - w->phys_cursor.x;
21541 /* When we erase the cursor, and ROW is overlapped by other
21542 rows, make sure that these overlapping parts of other rows
21543 are redrawn. */
21544 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21545 {
21546 w->phys_cursor_width = x1 - w->phys_cursor.x;
21547
21548 if (row > w->current_matrix->rows
21549 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21550 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21551 OVERLAPS_ERASED_CURSOR);
21552
21553 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21554 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21555 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21556 OVERLAPS_ERASED_CURSOR);
21557 }
21558 }
21559 }
21560
21561
21562 /* EXPORT:
21563 Erase the image of a cursor of window W from the screen. */
21564
21565 void
21566 erase_phys_cursor (w)
21567 struct window *w;
21568 {
21569 struct frame *f = XFRAME (w->frame);
21570 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21571 int hpos = w->phys_cursor.hpos;
21572 int vpos = w->phys_cursor.vpos;
21573 int mouse_face_here_p = 0;
21574 struct glyph_matrix *active_glyphs = w->current_matrix;
21575 struct glyph_row *cursor_row;
21576 struct glyph *cursor_glyph;
21577 enum draw_glyphs_face hl;
21578
21579 /* No cursor displayed or row invalidated => nothing to do on the
21580 screen. */
21581 if (w->phys_cursor_type == NO_CURSOR)
21582 goto mark_cursor_off;
21583
21584 /* VPOS >= active_glyphs->nrows means that window has been resized.
21585 Don't bother to erase the cursor. */
21586 if (vpos >= active_glyphs->nrows)
21587 goto mark_cursor_off;
21588
21589 /* If row containing cursor is marked invalid, there is nothing we
21590 can do. */
21591 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21592 if (!cursor_row->enabled_p)
21593 goto mark_cursor_off;
21594
21595 /* If line spacing is > 0, old cursor may only be partially visible in
21596 window after split-window. So adjust visible height. */
21597 cursor_row->visible_height = min (cursor_row->visible_height,
21598 window_text_bottom_y (w) - cursor_row->y);
21599
21600 /* If row is completely invisible, don't attempt to delete a cursor which
21601 isn't there. This can happen if cursor is at top of a window, and
21602 we switch to a buffer with a header line in that window. */
21603 if (cursor_row->visible_height <= 0)
21604 goto mark_cursor_off;
21605
21606 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21607 if (cursor_row->cursor_in_fringe_p)
21608 {
21609 cursor_row->cursor_in_fringe_p = 0;
21610 draw_fringe_bitmap (w, cursor_row, 0);
21611 goto mark_cursor_off;
21612 }
21613
21614 /* This can happen when the new row is shorter than the old one.
21615 In this case, either draw_glyphs or clear_end_of_line
21616 should have cleared the cursor. Note that we wouldn't be
21617 able to erase the cursor in this case because we don't have a
21618 cursor glyph at hand. */
21619 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21620 goto mark_cursor_off;
21621
21622 /* If the cursor is in the mouse face area, redisplay that when
21623 we clear the cursor. */
21624 if (! NILP (dpyinfo->mouse_face_window)
21625 && w == XWINDOW (dpyinfo->mouse_face_window)
21626 && (vpos > dpyinfo->mouse_face_beg_row
21627 || (vpos == dpyinfo->mouse_face_beg_row
21628 && hpos >= dpyinfo->mouse_face_beg_col))
21629 && (vpos < dpyinfo->mouse_face_end_row
21630 || (vpos == dpyinfo->mouse_face_end_row
21631 && hpos < dpyinfo->mouse_face_end_col))
21632 /* Don't redraw the cursor's spot in mouse face if it is at the
21633 end of a line (on a newline). The cursor appears there, but
21634 mouse highlighting does not. */
21635 && cursor_row->used[TEXT_AREA] > hpos)
21636 mouse_face_here_p = 1;
21637
21638 /* Maybe clear the display under the cursor. */
21639 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21640 {
21641 int x, y, left_x;
21642 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21643 int width;
21644
21645 cursor_glyph = get_phys_cursor_glyph (w);
21646 if (cursor_glyph == NULL)
21647 goto mark_cursor_off;
21648
21649 width = cursor_glyph->pixel_width;
21650 left_x = window_box_left_offset (w, TEXT_AREA);
21651 x = w->phys_cursor.x;
21652 if (x < left_x)
21653 width -= left_x - x;
21654 width = min (width, window_box_width (w, TEXT_AREA) - x);
21655 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21656 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21657
21658 if (width > 0)
21659 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21660 }
21661
21662 /* Erase the cursor by redrawing the character underneath it. */
21663 if (mouse_face_here_p)
21664 hl = DRAW_MOUSE_FACE;
21665 else
21666 hl = DRAW_NORMAL_TEXT;
21667 draw_phys_cursor_glyph (w, cursor_row, hl);
21668
21669 mark_cursor_off:
21670 w->phys_cursor_on_p = 0;
21671 w->phys_cursor_type = NO_CURSOR;
21672 }
21673
21674
21675 /* EXPORT:
21676 Display or clear cursor of window W. If ON is zero, clear the
21677 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21678 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21679
21680 void
21681 display_and_set_cursor (w, on, hpos, vpos, x, y)
21682 struct window *w;
21683 int on, hpos, vpos, x, y;
21684 {
21685 struct frame *f = XFRAME (w->frame);
21686 int new_cursor_type;
21687 int new_cursor_width;
21688 int active_cursor;
21689 struct glyph_row *glyph_row;
21690 struct glyph *glyph;
21691
21692 /* This is pointless on invisible frames, and dangerous on garbaged
21693 windows and frames; in the latter case, the frame or window may
21694 be in the midst of changing its size, and x and y may be off the
21695 window. */
21696 if (! FRAME_VISIBLE_P (f)
21697 || FRAME_GARBAGED_P (f)
21698 || vpos >= w->current_matrix->nrows
21699 || hpos >= w->current_matrix->matrix_w)
21700 return;
21701
21702 /* If cursor is off and we want it off, return quickly. */
21703 if (!on && !w->phys_cursor_on_p)
21704 return;
21705
21706 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21707 /* If cursor row is not enabled, we don't really know where to
21708 display the cursor. */
21709 if (!glyph_row->enabled_p)
21710 {
21711 w->phys_cursor_on_p = 0;
21712 return;
21713 }
21714
21715 glyph = NULL;
21716 if (!glyph_row->exact_window_width_line_p
21717 || hpos < glyph_row->used[TEXT_AREA])
21718 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21719
21720 xassert (interrupt_input_blocked);
21721
21722 /* Set new_cursor_type to the cursor we want to be displayed. */
21723 new_cursor_type = get_window_cursor_type (w, glyph,
21724 &new_cursor_width, &active_cursor);
21725
21726 /* If cursor is currently being shown and we don't want it to be or
21727 it is in the wrong place, or the cursor type is not what we want,
21728 erase it. */
21729 if (w->phys_cursor_on_p
21730 && (!on
21731 || w->phys_cursor.x != x
21732 || w->phys_cursor.y != y
21733 || new_cursor_type != w->phys_cursor_type
21734 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21735 && new_cursor_width != w->phys_cursor_width)))
21736 erase_phys_cursor (w);
21737
21738 /* Don't check phys_cursor_on_p here because that flag is only set
21739 to zero in some cases where we know that the cursor has been
21740 completely erased, to avoid the extra work of erasing the cursor
21741 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21742 still not be visible, or it has only been partly erased. */
21743 if (on)
21744 {
21745 w->phys_cursor_ascent = glyph_row->ascent;
21746 w->phys_cursor_height = glyph_row->height;
21747
21748 /* Set phys_cursor_.* before x_draw_.* is called because some
21749 of them may need the information. */
21750 w->phys_cursor.x = x;
21751 w->phys_cursor.y = glyph_row->y;
21752 w->phys_cursor.hpos = hpos;
21753 w->phys_cursor.vpos = vpos;
21754 }
21755
21756 rif->draw_window_cursor (w, glyph_row, x, y,
21757 new_cursor_type, new_cursor_width,
21758 on, active_cursor);
21759 }
21760
21761
21762 /* Switch the display of W's cursor on or off, according to the value
21763 of ON. */
21764
21765 static void
21766 update_window_cursor (w, on)
21767 struct window *w;
21768 int on;
21769 {
21770 /* Don't update cursor in windows whose frame is in the process
21771 of being deleted. */
21772 if (w->current_matrix)
21773 {
21774 BLOCK_INPUT;
21775 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21776 w->phys_cursor.x, w->phys_cursor.y);
21777 UNBLOCK_INPUT;
21778 }
21779 }
21780
21781
21782 /* Call update_window_cursor with parameter ON_P on all leaf windows
21783 in the window tree rooted at W. */
21784
21785 static void
21786 update_cursor_in_window_tree (w, on_p)
21787 struct window *w;
21788 int on_p;
21789 {
21790 while (w)
21791 {
21792 if (!NILP (w->hchild))
21793 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21794 else if (!NILP (w->vchild))
21795 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21796 else
21797 update_window_cursor (w, on_p);
21798
21799 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21800 }
21801 }
21802
21803
21804 /* EXPORT:
21805 Display the cursor on window W, or clear it, according to ON_P.
21806 Don't change the cursor's position. */
21807
21808 void
21809 x_update_cursor (f, on_p)
21810 struct frame *f;
21811 int on_p;
21812 {
21813 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21814 }
21815
21816
21817 /* EXPORT:
21818 Clear the cursor of window W to background color, and mark the
21819 cursor as not shown. This is used when the text where the cursor
21820 is is about to be rewritten. */
21821
21822 void
21823 x_clear_cursor (w)
21824 struct window *w;
21825 {
21826 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21827 update_window_cursor (w, 0);
21828 }
21829
21830
21831 /* EXPORT:
21832 Display the active region described by mouse_face_* according to DRAW. */
21833
21834 void
21835 show_mouse_face (dpyinfo, draw)
21836 Display_Info *dpyinfo;
21837 enum draw_glyphs_face draw;
21838 {
21839 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21840 struct frame *f = XFRAME (WINDOW_FRAME (w));
21841
21842 if (/* If window is in the process of being destroyed, don't bother
21843 to do anything. */
21844 w->current_matrix != NULL
21845 /* Don't update mouse highlight if hidden */
21846 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21847 /* Recognize when we are called to operate on rows that don't exist
21848 anymore. This can happen when a window is split. */
21849 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21850 {
21851 int phys_cursor_on_p = w->phys_cursor_on_p;
21852 struct glyph_row *row, *first, *last;
21853
21854 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21855 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21856
21857 for (row = first; row <= last && row->enabled_p; ++row)
21858 {
21859 int start_hpos, end_hpos, start_x;
21860
21861 /* For all but the first row, the highlight starts at column 0. */
21862 if (row == first)
21863 {
21864 start_hpos = dpyinfo->mouse_face_beg_col;
21865 start_x = dpyinfo->mouse_face_beg_x;
21866 }
21867 else
21868 {
21869 start_hpos = 0;
21870 start_x = 0;
21871 }
21872
21873 if (row == last)
21874 end_hpos = dpyinfo->mouse_face_end_col;
21875 else
21876 {
21877 end_hpos = row->used[TEXT_AREA];
21878 if (draw == DRAW_NORMAL_TEXT)
21879 row->fill_line_p = 1; /* Clear to end of line */
21880 }
21881
21882 if (end_hpos > start_hpos)
21883 {
21884 draw_glyphs (w, start_x, row, TEXT_AREA,
21885 start_hpos, end_hpos,
21886 draw, 0);
21887
21888 row->mouse_face_p
21889 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21890 }
21891 }
21892
21893 /* When we've written over the cursor, arrange for it to
21894 be displayed again. */
21895 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21896 {
21897 BLOCK_INPUT;
21898 display_and_set_cursor (w, 1,
21899 w->phys_cursor.hpos, w->phys_cursor.vpos,
21900 w->phys_cursor.x, w->phys_cursor.y);
21901 UNBLOCK_INPUT;
21902 }
21903 }
21904
21905 /* Change the mouse cursor. */
21906 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
21907 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21908 else if (draw == DRAW_MOUSE_FACE)
21909 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21910 else
21911 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21912 }
21913
21914 /* EXPORT:
21915 Clear out the mouse-highlighted active region.
21916 Redraw it un-highlighted first. Value is non-zero if mouse
21917 face was actually drawn unhighlighted. */
21918
21919 int
21920 clear_mouse_face (dpyinfo)
21921 Display_Info *dpyinfo;
21922 {
21923 int cleared = 0;
21924
21925 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21926 {
21927 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21928 cleared = 1;
21929 }
21930
21931 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21932 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21933 dpyinfo->mouse_face_window = Qnil;
21934 dpyinfo->mouse_face_overlay = Qnil;
21935 return cleared;
21936 }
21937
21938
21939 /* EXPORT:
21940 Non-zero if physical cursor of window W is within mouse face. */
21941
21942 int
21943 cursor_in_mouse_face_p (w)
21944 struct window *w;
21945 {
21946 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21947 int in_mouse_face = 0;
21948
21949 if (WINDOWP (dpyinfo->mouse_face_window)
21950 && XWINDOW (dpyinfo->mouse_face_window) == w)
21951 {
21952 int hpos = w->phys_cursor.hpos;
21953 int vpos = w->phys_cursor.vpos;
21954
21955 if (vpos >= dpyinfo->mouse_face_beg_row
21956 && vpos <= dpyinfo->mouse_face_end_row
21957 && (vpos > dpyinfo->mouse_face_beg_row
21958 || hpos >= dpyinfo->mouse_face_beg_col)
21959 && (vpos < dpyinfo->mouse_face_end_row
21960 || hpos < dpyinfo->mouse_face_end_col
21961 || dpyinfo->mouse_face_past_end))
21962 in_mouse_face = 1;
21963 }
21964
21965 return in_mouse_face;
21966 }
21967
21968
21969
21970 \f
21971 /* Find the glyph matrix position of buffer position CHARPOS in window
21972 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21973 current glyphs must be up to date. If CHARPOS is above window
21974 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21975 of last line in W. In the row containing CHARPOS, stop before glyphs
21976 having STOP as object. */
21977
21978 #if 1 /* This is a version of fast_find_position that's more correct
21979 in the presence of hscrolling, for example. I didn't install
21980 it right away because the problem fixed is minor, it failed
21981 in 20.x as well, and I think it's too risky to install
21982 so near the release of 21.1. 2001-09-25 gerd. */
21983
21984 static int
21985 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21986 struct window *w;
21987 int charpos;
21988 int *hpos, *vpos, *x, *y;
21989 Lisp_Object stop;
21990 {
21991 struct glyph_row *row, *first;
21992 struct glyph *glyph, *end;
21993 int past_end = 0;
21994
21995 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21996 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21997 {
21998 *x = first->x;
21999 *y = first->y;
22000 *hpos = 0;
22001 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22002 return 1;
22003 }
22004
22005 row = row_containing_pos (w, charpos, first, NULL, 0);
22006 if (row == NULL)
22007 {
22008 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22009 past_end = 1;
22010 }
22011
22012 /* If whole rows or last part of a row came from a display overlay,
22013 row_containing_pos will skip over such rows because their end pos
22014 equals the start pos of the overlay or interval.
22015
22016 Move back if we have a STOP object and previous row's
22017 end glyph came from STOP. */
22018 if (!NILP (stop))
22019 {
22020 struct glyph_row *prev;
22021 while ((prev = row - 1, prev >= first)
22022 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22023 && prev->used[TEXT_AREA] > 0)
22024 {
22025 struct glyph *beg = prev->glyphs[TEXT_AREA];
22026 glyph = beg + prev->used[TEXT_AREA];
22027 while (--glyph >= beg
22028 && INTEGERP (glyph->object));
22029 if (glyph < beg
22030 || !EQ (stop, glyph->object))
22031 break;
22032 row = prev;
22033 }
22034 }
22035
22036 *x = row->x;
22037 *y = row->y;
22038 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22039
22040 glyph = row->glyphs[TEXT_AREA];
22041 end = glyph + row->used[TEXT_AREA];
22042
22043 /* Skip over glyphs not having an object at the start of the row.
22044 These are special glyphs like truncation marks on terminal
22045 frames. */
22046 if (row->displays_text_p)
22047 while (glyph < end
22048 && INTEGERP (glyph->object)
22049 && !EQ (stop, glyph->object)
22050 && glyph->charpos < 0)
22051 {
22052 *x += glyph->pixel_width;
22053 ++glyph;
22054 }
22055
22056 while (glyph < end
22057 && !INTEGERP (glyph->object)
22058 && !EQ (stop, glyph->object)
22059 && (!BUFFERP (glyph->object)
22060 || glyph->charpos < charpos))
22061 {
22062 *x += glyph->pixel_width;
22063 ++glyph;
22064 }
22065
22066 *hpos = glyph - row->glyphs[TEXT_AREA];
22067 return !past_end;
22068 }
22069
22070 #else /* not 1 */
22071
22072 static int
22073 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22074 struct window *w;
22075 int pos;
22076 int *hpos, *vpos, *x, *y;
22077 Lisp_Object stop;
22078 {
22079 int i;
22080 int lastcol;
22081 int maybe_next_line_p = 0;
22082 int line_start_position;
22083 int yb = window_text_bottom_y (w);
22084 struct glyph_row *row, *best_row;
22085 int row_vpos, best_row_vpos;
22086 int current_x;
22087
22088 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22089 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22090
22091 while (row->y < yb)
22092 {
22093 if (row->used[TEXT_AREA])
22094 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22095 else
22096 line_start_position = 0;
22097
22098 if (line_start_position > pos)
22099 break;
22100 /* If the position sought is the end of the buffer,
22101 don't include the blank lines at the bottom of the window. */
22102 else if (line_start_position == pos
22103 && pos == BUF_ZV (XBUFFER (w->buffer)))
22104 {
22105 maybe_next_line_p = 1;
22106 break;
22107 }
22108 else if (line_start_position > 0)
22109 {
22110 best_row = row;
22111 best_row_vpos = row_vpos;
22112 }
22113
22114 if (row->y + row->height >= yb)
22115 break;
22116
22117 ++row;
22118 ++row_vpos;
22119 }
22120
22121 /* Find the right column within BEST_ROW. */
22122 lastcol = 0;
22123 current_x = best_row->x;
22124 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22125 {
22126 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22127 int charpos = glyph->charpos;
22128
22129 if (BUFFERP (glyph->object))
22130 {
22131 if (charpos == pos)
22132 {
22133 *hpos = i;
22134 *vpos = best_row_vpos;
22135 *x = current_x;
22136 *y = best_row->y;
22137 return 1;
22138 }
22139 else if (charpos > pos)
22140 break;
22141 }
22142 else if (EQ (glyph->object, stop))
22143 break;
22144
22145 if (charpos > 0)
22146 lastcol = i;
22147 current_x += glyph->pixel_width;
22148 }
22149
22150 /* If we're looking for the end of the buffer,
22151 and we didn't find it in the line we scanned,
22152 use the start of the following line. */
22153 if (maybe_next_line_p)
22154 {
22155 ++best_row;
22156 ++best_row_vpos;
22157 lastcol = 0;
22158 current_x = best_row->x;
22159 }
22160
22161 *vpos = best_row_vpos;
22162 *hpos = lastcol + 1;
22163 *x = current_x;
22164 *y = best_row->y;
22165 return 0;
22166 }
22167
22168 #endif /* not 1 */
22169
22170
22171 /* Find the position of the glyph for position POS in OBJECT in
22172 window W's current matrix, and return in *X, *Y the pixel
22173 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22174
22175 RIGHT_P non-zero means return the position of the right edge of the
22176 glyph, RIGHT_P zero means return the left edge position.
22177
22178 If no glyph for POS exists in the matrix, return the position of
22179 the glyph with the next smaller position that is in the matrix, if
22180 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22181 exists in the matrix, return the position of the glyph with the
22182 next larger position in OBJECT.
22183
22184 Value is non-zero if a glyph was found. */
22185
22186 static int
22187 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22188 struct window *w;
22189 int pos;
22190 Lisp_Object object;
22191 int *hpos, *vpos, *x, *y;
22192 int right_p;
22193 {
22194 int yb = window_text_bottom_y (w);
22195 struct glyph_row *r;
22196 struct glyph *best_glyph = NULL;
22197 struct glyph_row *best_row = NULL;
22198 int best_x = 0;
22199
22200 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22201 r->enabled_p && r->y < yb;
22202 ++r)
22203 {
22204 struct glyph *g = r->glyphs[TEXT_AREA];
22205 struct glyph *e = g + r->used[TEXT_AREA];
22206 int gx;
22207
22208 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22209 if (EQ (g->object, object))
22210 {
22211 if (g->charpos == pos)
22212 {
22213 best_glyph = g;
22214 best_x = gx;
22215 best_row = r;
22216 goto found;
22217 }
22218 else if (best_glyph == NULL
22219 || ((abs (g->charpos - pos)
22220 < abs (best_glyph->charpos - pos))
22221 && (right_p
22222 ? g->charpos < pos
22223 : g->charpos > pos)))
22224 {
22225 best_glyph = g;
22226 best_x = gx;
22227 best_row = r;
22228 }
22229 }
22230 }
22231
22232 found:
22233
22234 if (best_glyph)
22235 {
22236 *x = best_x;
22237 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22238
22239 if (right_p)
22240 {
22241 *x += best_glyph->pixel_width;
22242 ++*hpos;
22243 }
22244
22245 *y = best_row->y;
22246 *vpos = best_row - w->current_matrix->rows;
22247 }
22248
22249 return best_glyph != NULL;
22250 }
22251
22252
22253 /* See if position X, Y is within a hot-spot of an image. */
22254
22255 static int
22256 on_hot_spot_p (hot_spot, x, y)
22257 Lisp_Object hot_spot;
22258 int x, y;
22259 {
22260 if (!CONSP (hot_spot))
22261 return 0;
22262
22263 if (EQ (XCAR (hot_spot), Qrect))
22264 {
22265 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22266 Lisp_Object rect = XCDR (hot_spot);
22267 Lisp_Object tem;
22268 if (!CONSP (rect))
22269 return 0;
22270 if (!CONSP (XCAR (rect)))
22271 return 0;
22272 if (!CONSP (XCDR (rect)))
22273 return 0;
22274 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22275 return 0;
22276 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22277 return 0;
22278 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22279 return 0;
22280 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22281 return 0;
22282 return 1;
22283 }
22284 else if (EQ (XCAR (hot_spot), Qcircle))
22285 {
22286 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22287 Lisp_Object circ = XCDR (hot_spot);
22288 Lisp_Object lr, lx0, ly0;
22289 if (CONSP (circ)
22290 && CONSP (XCAR (circ))
22291 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22292 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22293 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22294 {
22295 double r = XFLOATINT (lr);
22296 double dx = XINT (lx0) - x;
22297 double dy = XINT (ly0) - y;
22298 return (dx * dx + dy * dy <= r * r);
22299 }
22300 }
22301 else if (EQ (XCAR (hot_spot), Qpoly))
22302 {
22303 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22304 if (VECTORP (XCDR (hot_spot)))
22305 {
22306 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22307 Lisp_Object *poly = v->contents;
22308 int n = v->size;
22309 int i;
22310 int inside = 0;
22311 Lisp_Object lx, ly;
22312 int x0, y0;
22313
22314 /* Need an even number of coordinates, and at least 3 edges. */
22315 if (n < 6 || n & 1)
22316 return 0;
22317
22318 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22319 If count is odd, we are inside polygon. Pixels on edges
22320 may or may not be included depending on actual geometry of the
22321 polygon. */
22322 if ((lx = poly[n-2], !INTEGERP (lx))
22323 || (ly = poly[n-1], !INTEGERP (lx)))
22324 return 0;
22325 x0 = XINT (lx), y0 = XINT (ly);
22326 for (i = 0; i < n; i += 2)
22327 {
22328 int x1 = x0, y1 = y0;
22329 if ((lx = poly[i], !INTEGERP (lx))
22330 || (ly = poly[i+1], !INTEGERP (ly)))
22331 return 0;
22332 x0 = XINT (lx), y0 = XINT (ly);
22333
22334 /* Does this segment cross the X line? */
22335 if (x0 >= x)
22336 {
22337 if (x1 >= x)
22338 continue;
22339 }
22340 else if (x1 < x)
22341 continue;
22342 if (y > y0 && y > y1)
22343 continue;
22344 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22345 inside = !inside;
22346 }
22347 return inside;
22348 }
22349 }
22350 /* If we don't understand the format, pretend we're not in the hot-spot. */
22351 return 0;
22352 }
22353
22354 Lisp_Object
22355 find_hot_spot (map, x, y)
22356 Lisp_Object map;
22357 int x, y;
22358 {
22359 while (CONSP (map))
22360 {
22361 if (CONSP (XCAR (map))
22362 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22363 return XCAR (map);
22364 map = XCDR (map);
22365 }
22366
22367 return Qnil;
22368 }
22369
22370 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22371 3, 3, 0,
22372 doc: /* Lookup in image map MAP coordinates X and Y.
22373 An image map is an alist where each element has the format (AREA ID PLIST).
22374 An AREA is specified as either a rectangle, a circle, or a polygon:
22375 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22376 pixel coordinates of the upper left and bottom right corners.
22377 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22378 and the radius of the circle; r may be a float or integer.
22379 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22380 vector describes one corner in the polygon.
22381 Returns the alist element for the first matching AREA in MAP. */)
22382 (map, x, y)
22383 Lisp_Object map;
22384 Lisp_Object x, y;
22385 {
22386 if (NILP (map))
22387 return Qnil;
22388
22389 CHECK_NUMBER (x);
22390 CHECK_NUMBER (y);
22391
22392 return find_hot_spot (map, XINT (x), XINT (y));
22393 }
22394
22395
22396 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22397 static void
22398 define_frame_cursor1 (f, cursor, pointer)
22399 struct frame *f;
22400 Cursor cursor;
22401 Lisp_Object pointer;
22402 {
22403 /* Do not change cursor shape while dragging mouse. */
22404 if (!NILP (do_mouse_tracking))
22405 return;
22406
22407 if (!NILP (pointer))
22408 {
22409 if (EQ (pointer, Qarrow))
22410 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22411 else if (EQ (pointer, Qhand))
22412 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22413 else if (EQ (pointer, Qtext))
22414 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22415 else if (EQ (pointer, intern ("hdrag")))
22416 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22417 #ifdef HAVE_X_WINDOWS
22418 else if (EQ (pointer, intern ("vdrag")))
22419 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22420 #endif
22421 else if (EQ (pointer, intern ("hourglass")))
22422 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22423 else if (EQ (pointer, Qmodeline))
22424 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22425 else
22426 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22427 }
22428
22429 if (cursor != No_Cursor)
22430 rif->define_frame_cursor (f, cursor);
22431 }
22432
22433 /* Take proper action when mouse has moved to the mode or header line
22434 or marginal area AREA of window W, x-position X and y-position Y.
22435 X is relative to the start of the text display area of W, so the
22436 width of bitmap areas and scroll bars must be subtracted to get a
22437 position relative to the start of the mode line. */
22438
22439 static void
22440 note_mode_line_or_margin_highlight (window, x, y, area)
22441 Lisp_Object window;
22442 int x, y;
22443 enum window_part area;
22444 {
22445 struct window *w = XWINDOW (window);
22446 struct frame *f = XFRAME (w->frame);
22447 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22448 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22449 Lisp_Object pointer = Qnil;
22450 int charpos, dx, dy, width, height;
22451 Lisp_Object string, object = Qnil;
22452 Lisp_Object pos, help;
22453
22454 Lisp_Object mouse_face;
22455 int original_x_pixel = x;
22456 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22457 struct glyph_row *row;
22458
22459 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22460 {
22461 int x0;
22462 struct glyph *end;
22463
22464 string = mode_line_string (w, area, &x, &y, &charpos,
22465 &object, &dx, &dy, &width, &height);
22466
22467 row = (area == ON_MODE_LINE
22468 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22469 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22470
22471 /* Find glyph */
22472 if (row->mode_line_p && row->enabled_p)
22473 {
22474 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22475 end = glyph + row->used[TEXT_AREA];
22476
22477 for (x0 = original_x_pixel;
22478 glyph < end && x0 >= glyph->pixel_width;
22479 ++glyph)
22480 x0 -= glyph->pixel_width;
22481
22482 if (glyph >= end)
22483 glyph = NULL;
22484 }
22485 }
22486 else
22487 {
22488 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22489 string = marginal_area_string (w, area, &x, &y, &charpos,
22490 &object, &dx, &dy, &width, &height);
22491 }
22492
22493 help = Qnil;
22494
22495 if (IMAGEP (object))
22496 {
22497 Lisp_Object image_map, hotspot;
22498 if ((image_map = Fplist_get (XCDR (object), QCmap),
22499 !NILP (image_map))
22500 && (hotspot = find_hot_spot (image_map, dx, dy),
22501 CONSP (hotspot))
22502 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22503 {
22504 Lisp_Object area_id, plist;
22505
22506 area_id = XCAR (hotspot);
22507 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22508 If so, we could look for mouse-enter, mouse-leave
22509 properties in PLIST (and do something...). */
22510 hotspot = XCDR (hotspot);
22511 if (CONSP (hotspot)
22512 && (plist = XCAR (hotspot), CONSP (plist)))
22513 {
22514 pointer = Fplist_get (plist, Qpointer);
22515 if (NILP (pointer))
22516 pointer = Qhand;
22517 help = Fplist_get (plist, Qhelp_echo);
22518 if (!NILP (help))
22519 {
22520 help_echo_string = help;
22521 /* Is this correct? ++kfs */
22522 XSETWINDOW (help_echo_window, w);
22523 help_echo_object = w->buffer;
22524 help_echo_pos = charpos;
22525 }
22526 }
22527 }
22528 if (NILP (pointer))
22529 pointer = Fplist_get (XCDR (object), QCpointer);
22530 }
22531
22532 if (STRINGP (string))
22533 {
22534 pos = make_number (charpos);
22535 /* If we're on a string with `help-echo' text property, arrange
22536 for the help to be displayed. This is done by setting the
22537 global variable help_echo_string to the help string. */
22538 if (NILP (help))
22539 {
22540 help = Fget_text_property (pos, Qhelp_echo, string);
22541 if (!NILP (help))
22542 {
22543 help_echo_string = help;
22544 XSETWINDOW (help_echo_window, w);
22545 help_echo_object = string;
22546 help_echo_pos = charpos;
22547 }
22548 }
22549
22550 if (NILP (pointer))
22551 pointer = Fget_text_property (pos, Qpointer, string);
22552
22553 /* Change the mouse pointer according to what is under X/Y. */
22554 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22555 {
22556 Lisp_Object map;
22557 map = Fget_text_property (pos, Qlocal_map, string);
22558 if (!KEYMAPP (map))
22559 map = Fget_text_property (pos, Qkeymap, string);
22560 if (!KEYMAPP (map))
22561 cursor = dpyinfo->vertical_scroll_bar_cursor;
22562 }
22563
22564 /* Change the mouse face according to what is under X/Y. */
22565 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22566 if (!NILP (mouse_face)
22567 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22568 && glyph)
22569 {
22570 Lisp_Object b, e;
22571
22572 struct glyph * tmp_glyph;
22573
22574 int gpos;
22575 int gseq_length;
22576 int total_pixel_width;
22577 int ignore;
22578
22579 int vpos, hpos;
22580
22581 b = Fprevious_single_property_change (make_number (charpos + 1),
22582 Qmouse_face, string, Qnil);
22583 if (NILP (b))
22584 b = make_number (0);
22585
22586 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22587 if (NILP (e))
22588 e = make_number (SCHARS (string));
22589
22590 /* Calculate the position(glyph position: GPOS) of GLYPH in
22591 displayed string. GPOS is different from CHARPOS.
22592
22593 CHARPOS is the position of glyph in internal string
22594 object. A mode line string format has structures which
22595 is converted to a flatten by emacs lisp interpreter.
22596 The internal string is an element of the structures.
22597 The displayed string is the flatten string. */
22598 gpos = 0;
22599 if (glyph > row_start_glyph)
22600 {
22601 tmp_glyph = glyph - 1;
22602 while (tmp_glyph >= row_start_glyph
22603 && tmp_glyph->charpos >= XINT (b)
22604 && EQ (tmp_glyph->object, glyph->object))
22605 {
22606 tmp_glyph--;
22607 gpos++;
22608 }
22609 }
22610
22611 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22612 displayed string holding GLYPH.
22613
22614 GSEQ_LENGTH is different from SCHARS (STRING).
22615 SCHARS (STRING) returns the length of the internal string. */
22616 for (tmp_glyph = glyph, gseq_length = gpos;
22617 tmp_glyph->charpos < XINT (e);
22618 tmp_glyph++, gseq_length++)
22619 {
22620 if (!EQ (tmp_glyph->object, glyph->object))
22621 break;
22622 }
22623
22624 total_pixel_width = 0;
22625 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22626 total_pixel_width += tmp_glyph->pixel_width;
22627
22628 /* Pre calculation of re-rendering position */
22629 vpos = (x - gpos);
22630 hpos = (area == ON_MODE_LINE
22631 ? (w->current_matrix)->nrows - 1
22632 : 0);
22633
22634 /* If the re-rendering position is included in the last
22635 re-rendering area, we should do nothing. */
22636 if ( EQ (window, dpyinfo->mouse_face_window)
22637 && dpyinfo->mouse_face_beg_col <= vpos
22638 && vpos < dpyinfo->mouse_face_end_col
22639 && dpyinfo->mouse_face_beg_row == hpos )
22640 return;
22641
22642 if (clear_mouse_face (dpyinfo))
22643 cursor = No_Cursor;
22644
22645 dpyinfo->mouse_face_beg_col = vpos;
22646 dpyinfo->mouse_face_beg_row = hpos;
22647
22648 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22649 dpyinfo->mouse_face_beg_y = 0;
22650
22651 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22652 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22653
22654 dpyinfo->mouse_face_end_x = 0;
22655 dpyinfo->mouse_face_end_y = 0;
22656
22657 dpyinfo->mouse_face_past_end = 0;
22658 dpyinfo->mouse_face_window = window;
22659
22660 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22661 charpos,
22662 0, 0, 0, &ignore,
22663 glyph->face_id, 1);
22664 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22665
22666 if (NILP (pointer))
22667 pointer = Qhand;
22668 }
22669 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22670 clear_mouse_face (dpyinfo);
22671 }
22672 define_frame_cursor1 (f, cursor, pointer);
22673 }
22674
22675
22676 /* EXPORT:
22677 Take proper action when the mouse has moved to position X, Y on
22678 frame F as regards highlighting characters that have mouse-face
22679 properties. Also de-highlighting chars where the mouse was before.
22680 X and Y can be negative or out of range. */
22681
22682 void
22683 note_mouse_highlight (f, x, y)
22684 struct frame *f;
22685 int x, y;
22686 {
22687 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22688 enum window_part part;
22689 Lisp_Object window;
22690 struct window *w;
22691 Cursor cursor = No_Cursor;
22692 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22693 struct buffer *b;
22694
22695 /* When a menu is active, don't highlight because this looks odd. */
22696 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22697 if (popup_activated ())
22698 return;
22699 #endif
22700
22701 if (NILP (Vmouse_highlight)
22702 || !f->glyphs_initialized_p)
22703 return;
22704
22705 dpyinfo->mouse_face_mouse_x = x;
22706 dpyinfo->mouse_face_mouse_y = y;
22707 dpyinfo->mouse_face_mouse_frame = f;
22708
22709 if (dpyinfo->mouse_face_defer)
22710 return;
22711
22712 if (gc_in_progress)
22713 {
22714 dpyinfo->mouse_face_deferred_gc = 1;
22715 return;
22716 }
22717
22718 /* Which window is that in? */
22719 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22720
22721 /* If we were displaying active text in another window, clear that.
22722 Also clear if we move out of text area in same window. */
22723 if (! EQ (window, dpyinfo->mouse_face_window)
22724 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22725 && !NILP (dpyinfo->mouse_face_window)))
22726 clear_mouse_face (dpyinfo);
22727
22728 /* Not on a window -> return. */
22729 if (!WINDOWP (window))
22730 return;
22731
22732 /* Reset help_echo_string. It will get recomputed below. */
22733 help_echo_string = Qnil;
22734
22735 /* Convert to window-relative pixel coordinates. */
22736 w = XWINDOW (window);
22737 frame_to_window_pixel_xy (w, &x, &y);
22738
22739 /* Handle tool-bar window differently since it doesn't display a
22740 buffer. */
22741 if (EQ (window, f->tool_bar_window))
22742 {
22743 note_tool_bar_highlight (f, x, y);
22744 return;
22745 }
22746
22747 /* Mouse is on the mode, header line or margin? */
22748 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22749 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22750 {
22751 note_mode_line_or_margin_highlight (window, x, y, part);
22752 return;
22753 }
22754
22755 if (part == ON_VERTICAL_BORDER)
22756 {
22757 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22758 help_echo_string = build_string ("drag-mouse-1: resize");
22759 }
22760 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22761 || part == ON_SCROLL_BAR)
22762 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22763 else
22764 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22765
22766 /* Are we in a window whose display is up to date?
22767 And verify the buffer's text has not changed. */
22768 b = XBUFFER (w->buffer);
22769 if (part == ON_TEXT
22770 && EQ (w->window_end_valid, w->buffer)
22771 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22772 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22773 {
22774 int hpos, vpos, pos, i, dx, dy, area;
22775 struct glyph *glyph;
22776 Lisp_Object object;
22777 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22778 Lisp_Object *overlay_vec = NULL;
22779 int noverlays;
22780 struct buffer *obuf;
22781 int obegv, ozv, same_region;
22782
22783 /* Find the glyph under X/Y. */
22784 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22785
22786 /* Look for :pointer property on image. */
22787 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22788 {
22789 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22790 if (img != NULL && IMAGEP (img->spec))
22791 {
22792 Lisp_Object image_map, hotspot;
22793 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22794 !NILP (image_map))
22795 && (hotspot = find_hot_spot (image_map,
22796 glyph->slice.x + dx,
22797 glyph->slice.y + dy),
22798 CONSP (hotspot))
22799 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22800 {
22801 Lisp_Object area_id, plist;
22802
22803 area_id = XCAR (hotspot);
22804 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22805 If so, we could look for mouse-enter, mouse-leave
22806 properties in PLIST (and do something...). */
22807 hotspot = XCDR (hotspot);
22808 if (CONSP (hotspot)
22809 && (plist = XCAR (hotspot), CONSP (plist)))
22810 {
22811 pointer = Fplist_get (plist, Qpointer);
22812 if (NILP (pointer))
22813 pointer = Qhand;
22814 help_echo_string = Fplist_get (plist, Qhelp_echo);
22815 if (!NILP (help_echo_string))
22816 {
22817 help_echo_window = window;
22818 help_echo_object = glyph->object;
22819 help_echo_pos = glyph->charpos;
22820 }
22821 }
22822 }
22823 if (NILP (pointer))
22824 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22825 }
22826 }
22827
22828 /* Clear mouse face if X/Y not over text. */
22829 if (glyph == NULL
22830 || area != TEXT_AREA
22831 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22832 {
22833 if (clear_mouse_face (dpyinfo))
22834 cursor = No_Cursor;
22835 if (NILP (pointer))
22836 {
22837 if (area != TEXT_AREA)
22838 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22839 else
22840 pointer = Vvoid_text_area_pointer;
22841 }
22842 goto set_cursor;
22843 }
22844
22845 pos = glyph->charpos;
22846 object = glyph->object;
22847 if (!STRINGP (object) && !BUFFERP (object))
22848 goto set_cursor;
22849
22850 /* If we get an out-of-range value, return now; avoid an error. */
22851 if (BUFFERP (object) && pos > BUF_Z (b))
22852 goto set_cursor;
22853
22854 /* Make the window's buffer temporarily current for
22855 overlays_at and compute_char_face. */
22856 obuf = current_buffer;
22857 current_buffer = b;
22858 obegv = BEGV;
22859 ozv = ZV;
22860 BEGV = BEG;
22861 ZV = Z;
22862
22863 /* Is this char mouse-active or does it have help-echo? */
22864 position = make_number (pos);
22865
22866 if (BUFFERP (object))
22867 {
22868 /* Put all the overlays we want in a vector in overlay_vec. */
22869 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22870 /* Sort overlays into increasing priority order. */
22871 noverlays = sort_overlays (overlay_vec, noverlays, w);
22872 }
22873 else
22874 noverlays = 0;
22875
22876 same_region = (EQ (window, dpyinfo->mouse_face_window)
22877 && vpos >= dpyinfo->mouse_face_beg_row
22878 && vpos <= dpyinfo->mouse_face_end_row
22879 && (vpos > dpyinfo->mouse_face_beg_row
22880 || hpos >= dpyinfo->mouse_face_beg_col)
22881 && (vpos < dpyinfo->mouse_face_end_row
22882 || hpos < dpyinfo->mouse_face_end_col
22883 || dpyinfo->mouse_face_past_end));
22884
22885 if (same_region)
22886 cursor = No_Cursor;
22887
22888 /* Check mouse-face highlighting. */
22889 if (! same_region
22890 /* If there exists an overlay with mouse-face overlapping
22891 the one we are currently highlighting, we have to
22892 check if we enter the overlapping overlay, and then
22893 highlight only that. */
22894 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22895 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22896 {
22897 /* Find the highest priority overlay that has a mouse-face
22898 property. */
22899 overlay = Qnil;
22900 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22901 {
22902 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22903 if (!NILP (mouse_face))
22904 overlay = overlay_vec[i];
22905 }
22906
22907 /* If we're actually highlighting the same overlay as
22908 before, there's no need to do that again. */
22909 if (!NILP (overlay)
22910 && EQ (overlay, dpyinfo->mouse_face_overlay))
22911 goto check_help_echo;
22912
22913 dpyinfo->mouse_face_overlay = overlay;
22914
22915 /* Clear the display of the old active region, if any. */
22916 if (clear_mouse_face (dpyinfo))
22917 cursor = No_Cursor;
22918
22919 /* If no overlay applies, get a text property. */
22920 if (NILP (overlay))
22921 mouse_face = Fget_text_property (position, Qmouse_face, object);
22922
22923 /* Handle the overlay case. */
22924 if (!NILP (overlay))
22925 {
22926 /* Find the range of text around this char that
22927 should be active. */
22928 Lisp_Object before, after;
22929 int ignore;
22930
22931 before = Foverlay_start (overlay);
22932 after = Foverlay_end (overlay);
22933 /* Record this as the current active region. */
22934 fast_find_position (w, XFASTINT (before),
22935 &dpyinfo->mouse_face_beg_col,
22936 &dpyinfo->mouse_face_beg_row,
22937 &dpyinfo->mouse_face_beg_x,
22938 &dpyinfo->mouse_face_beg_y, Qnil);
22939
22940 dpyinfo->mouse_face_past_end
22941 = !fast_find_position (w, XFASTINT (after),
22942 &dpyinfo->mouse_face_end_col,
22943 &dpyinfo->mouse_face_end_row,
22944 &dpyinfo->mouse_face_end_x,
22945 &dpyinfo->mouse_face_end_y, Qnil);
22946 dpyinfo->mouse_face_window = window;
22947
22948 dpyinfo->mouse_face_face_id
22949 = face_at_buffer_position (w, pos, 0, 0,
22950 &ignore, pos + 1,
22951 !dpyinfo->mouse_face_hidden);
22952
22953 /* Display it as active. */
22954 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22955 cursor = No_Cursor;
22956 }
22957 /* Handle the text property case. */
22958 else if (!NILP (mouse_face) && BUFFERP (object))
22959 {
22960 /* Find the range of text around this char that
22961 should be active. */
22962 Lisp_Object before, after, beginning, end;
22963 int ignore;
22964
22965 beginning = Fmarker_position (w->start);
22966 end = make_number (BUF_Z (XBUFFER (object))
22967 - XFASTINT (w->window_end_pos));
22968 before
22969 = Fprevious_single_property_change (make_number (pos + 1),
22970 Qmouse_face,
22971 object, beginning);
22972 after
22973 = Fnext_single_property_change (position, Qmouse_face,
22974 object, end);
22975
22976 /* Record this as the current active region. */
22977 fast_find_position (w, XFASTINT (before),
22978 &dpyinfo->mouse_face_beg_col,
22979 &dpyinfo->mouse_face_beg_row,
22980 &dpyinfo->mouse_face_beg_x,
22981 &dpyinfo->mouse_face_beg_y, Qnil);
22982 dpyinfo->mouse_face_past_end
22983 = !fast_find_position (w, XFASTINT (after),
22984 &dpyinfo->mouse_face_end_col,
22985 &dpyinfo->mouse_face_end_row,
22986 &dpyinfo->mouse_face_end_x,
22987 &dpyinfo->mouse_face_end_y, Qnil);
22988 dpyinfo->mouse_face_window = window;
22989
22990 if (BUFFERP (object))
22991 dpyinfo->mouse_face_face_id
22992 = face_at_buffer_position (w, pos, 0, 0,
22993 &ignore, pos + 1,
22994 !dpyinfo->mouse_face_hidden);
22995
22996 /* Display it as active. */
22997 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22998 cursor = No_Cursor;
22999 }
23000 else if (!NILP (mouse_face) && STRINGP (object))
23001 {
23002 Lisp_Object b, e;
23003 int ignore;
23004
23005 b = Fprevious_single_property_change (make_number (pos + 1),
23006 Qmouse_face,
23007 object, Qnil);
23008 e = Fnext_single_property_change (position, Qmouse_face,
23009 object, Qnil);
23010 if (NILP (b))
23011 b = make_number (0);
23012 if (NILP (e))
23013 e = make_number (SCHARS (object) - 1);
23014
23015 fast_find_string_pos (w, XINT (b), object,
23016 &dpyinfo->mouse_face_beg_col,
23017 &dpyinfo->mouse_face_beg_row,
23018 &dpyinfo->mouse_face_beg_x,
23019 &dpyinfo->mouse_face_beg_y, 0);
23020 fast_find_string_pos (w, XINT (e), object,
23021 &dpyinfo->mouse_face_end_col,
23022 &dpyinfo->mouse_face_end_row,
23023 &dpyinfo->mouse_face_end_x,
23024 &dpyinfo->mouse_face_end_y, 1);
23025 dpyinfo->mouse_face_past_end = 0;
23026 dpyinfo->mouse_face_window = window;
23027 dpyinfo->mouse_face_face_id
23028 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23029 glyph->face_id, 1);
23030 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23031 cursor = No_Cursor;
23032 }
23033 else if (STRINGP (object) && NILP (mouse_face))
23034 {
23035 /* A string which doesn't have mouse-face, but
23036 the text ``under'' it might have. */
23037 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23038 int start = MATRIX_ROW_START_CHARPOS (r);
23039
23040 pos = string_buffer_position (w, object, start);
23041 if (pos > 0)
23042 mouse_face = get_char_property_and_overlay (make_number (pos),
23043 Qmouse_face,
23044 w->buffer,
23045 &overlay);
23046 if (!NILP (mouse_face) && !NILP (overlay))
23047 {
23048 Lisp_Object before = Foverlay_start (overlay);
23049 Lisp_Object after = Foverlay_end (overlay);
23050 int ignore;
23051
23052 /* Note that we might not be able to find position
23053 BEFORE in the glyph matrix if the overlay is
23054 entirely covered by a `display' property. In
23055 this case, we overshoot. So let's stop in
23056 the glyph matrix before glyphs for OBJECT. */
23057 fast_find_position (w, XFASTINT (before),
23058 &dpyinfo->mouse_face_beg_col,
23059 &dpyinfo->mouse_face_beg_row,
23060 &dpyinfo->mouse_face_beg_x,
23061 &dpyinfo->mouse_face_beg_y,
23062 object);
23063
23064 dpyinfo->mouse_face_past_end
23065 = !fast_find_position (w, XFASTINT (after),
23066 &dpyinfo->mouse_face_end_col,
23067 &dpyinfo->mouse_face_end_row,
23068 &dpyinfo->mouse_face_end_x,
23069 &dpyinfo->mouse_face_end_y,
23070 Qnil);
23071 dpyinfo->mouse_face_window = window;
23072 dpyinfo->mouse_face_face_id
23073 = face_at_buffer_position (w, pos, 0, 0,
23074 &ignore, pos + 1,
23075 !dpyinfo->mouse_face_hidden);
23076
23077 /* Display it as active. */
23078 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23079 cursor = No_Cursor;
23080 }
23081 }
23082 }
23083
23084 check_help_echo:
23085
23086 /* Look for a `help-echo' property. */
23087 if (NILP (help_echo_string)) {
23088 Lisp_Object help, overlay;
23089
23090 /* Check overlays first. */
23091 help = overlay = Qnil;
23092 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23093 {
23094 overlay = overlay_vec[i];
23095 help = Foverlay_get (overlay, Qhelp_echo);
23096 }
23097
23098 if (!NILP (help))
23099 {
23100 help_echo_string = help;
23101 help_echo_window = window;
23102 help_echo_object = overlay;
23103 help_echo_pos = pos;
23104 }
23105 else
23106 {
23107 Lisp_Object object = glyph->object;
23108 int charpos = glyph->charpos;
23109
23110 /* Try text properties. */
23111 if (STRINGP (object)
23112 && charpos >= 0
23113 && charpos < SCHARS (object))
23114 {
23115 help = Fget_text_property (make_number (charpos),
23116 Qhelp_echo, object);
23117 if (NILP (help))
23118 {
23119 /* If the string itself doesn't specify a help-echo,
23120 see if the buffer text ``under'' it does. */
23121 struct glyph_row *r
23122 = MATRIX_ROW (w->current_matrix, vpos);
23123 int start = MATRIX_ROW_START_CHARPOS (r);
23124 int pos = string_buffer_position (w, object, start);
23125 if (pos > 0)
23126 {
23127 help = Fget_char_property (make_number (pos),
23128 Qhelp_echo, w->buffer);
23129 if (!NILP (help))
23130 {
23131 charpos = pos;
23132 object = w->buffer;
23133 }
23134 }
23135 }
23136 }
23137 else if (BUFFERP (object)
23138 && charpos >= BEGV
23139 && charpos < ZV)
23140 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23141 object);
23142
23143 if (!NILP (help))
23144 {
23145 help_echo_string = help;
23146 help_echo_window = window;
23147 help_echo_object = object;
23148 help_echo_pos = charpos;
23149 }
23150 }
23151 }
23152
23153 /* Look for a `pointer' property. */
23154 if (NILP (pointer))
23155 {
23156 /* Check overlays first. */
23157 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23158 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23159
23160 if (NILP (pointer))
23161 {
23162 Lisp_Object object = glyph->object;
23163 int charpos = glyph->charpos;
23164
23165 /* Try text properties. */
23166 if (STRINGP (object)
23167 && charpos >= 0
23168 && charpos < SCHARS (object))
23169 {
23170 pointer = Fget_text_property (make_number (charpos),
23171 Qpointer, object);
23172 if (NILP (pointer))
23173 {
23174 /* If the string itself doesn't specify a pointer,
23175 see if the buffer text ``under'' it does. */
23176 struct glyph_row *r
23177 = MATRIX_ROW (w->current_matrix, vpos);
23178 int start = MATRIX_ROW_START_CHARPOS (r);
23179 int pos = string_buffer_position (w, object, start);
23180 if (pos > 0)
23181 pointer = Fget_char_property (make_number (pos),
23182 Qpointer, w->buffer);
23183 }
23184 }
23185 else if (BUFFERP (object)
23186 && charpos >= BEGV
23187 && charpos < ZV)
23188 pointer = Fget_text_property (make_number (charpos),
23189 Qpointer, object);
23190 }
23191 }
23192
23193 BEGV = obegv;
23194 ZV = ozv;
23195 current_buffer = obuf;
23196 }
23197
23198 set_cursor:
23199
23200 define_frame_cursor1 (f, cursor, pointer);
23201 }
23202
23203
23204 /* EXPORT for RIF:
23205 Clear any mouse-face on window W. This function is part of the
23206 redisplay interface, and is called from try_window_id and similar
23207 functions to ensure the mouse-highlight is off. */
23208
23209 void
23210 x_clear_window_mouse_face (w)
23211 struct window *w;
23212 {
23213 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23214 Lisp_Object window;
23215
23216 BLOCK_INPUT;
23217 XSETWINDOW (window, w);
23218 if (EQ (window, dpyinfo->mouse_face_window))
23219 clear_mouse_face (dpyinfo);
23220 UNBLOCK_INPUT;
23221 }
23222
23223
23224 /* EXPORT:
23225 Just discard the mouse face information for frame F, if any.
23226 This is used when the size of F is changed. */
23227
23228 void
23229 cancel_mouse_face (f)
23230 struct frame *f;
23231 {
23232 Lisp_Object window;
23233 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23234
23235 window = dpyinfo->mouse_face_window;
23236 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23237 {
23238 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23239 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23240 dpyinfo->mouse_face_window = Qnil;
23241 }
23242 }
23243
23244
23245 #endif /* HAVE_WINDOW_SYSTEM */
23246
23247 \f
23248 /***********************************************************************
23249 Exposure Events
23250 ***********************************************************************/
23251
23252 #ifdef HAVE_WINDOW_SYSTEM
23253
23254 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23255 which intersects rectangle R. R is in window-relative coordinates. */
23256
23257 static void
23258 expose_area (w, row, r, area)
23259 struct window *w;
23260 struct glyph_row *row;
23261 XRectangle *r;
23262 enum glyph_row_area area;
23263 {
23264 struct glyph *first = row->glyphs[area];
23265 struct glyph *end = row->glyphs[area] + row->used[area];
23266 struct glyph *last;
23267 int first_x, start_x, x;
23268
23269 if (area == TEXT_AREA && row->fill_line_p)
23270 /* If row extends face to end of line write the whole line. */
23271 draw_glyphs (w, 0, row, area,
23272 0, row->used[area],
23273 DRAW_NORMAL_TEXT, 0);
23274 else
23275 {
23276 /* Set START_X to the window-relative start position for drawing glyphs of
23277 AREA. The first glyph of the text area can be partially visible.
23278 The first glyphs of other areas cannot. */
23279 start_x = window_box_left_offset (w, area);
23280 x = start_x;
23281 if (area == TEXT_AREA)
23282 x += row->x;
23283
23284 /* Find the first glyph that must be redrawn. */
23285 while (first < end
23286 && x + first->pixel_width < r->x)
23287 {
23288 x += first->pixel_width;
23289 ++first;
23290 }
23291
23292 /* Find the last one. */
23293 last = first;
23294 first_x = x;
23295 while (last < end
23296 && x < r->x + r->width)
23297 {
23298 x += last->pixel_width;
23299 ++last;
23300 }
23301
23302 /* Repaint. */
23303 if (last > first)
23304 draw_glyphs (w, first_x - start_x, row, area,
23305 first - row->glyphs[area], last - row->glyphs[area],
23306 DRAW_NORMAL_TEXT, 0);
23307 }
23308 }
23309
23310
23311 /* Redraw the parts of the glyph row ROW on window W intersecting
23312 rectangle R. R is in window-relative coordinates. Value is
23313 non-zero if mouse-face was overwritten. */
23314
23315 static int
23316 expose_line (w, row, r)
23317 struct window *w;
23318 struct glyph_row *row;
23319 XRectangle *r;
23320 {
23321 xassert (row->enabled_p);
23322
23323 if (row->mode_line_p || w->pseudo_window_p)
23324 draw_glyphs (w, 0, row, TEXT_AREA,
23325 0, row->used[TEXT_AREA],
23326 DRAW_NORMAL_TEXT, 0);
23327 else
23328 {
23329 if (row->used[LEFT_MARGIN_AREA])
23330 expose_area (w, row, r, LEFT_MARGIN_AREA);
23331 if (row->used[TEXT_AREA])
23332 expose_area (w, row, r, TEXT_AREA);
23333 if (row->used[RIGHT_MARGIN_AREA])
23334 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23335 draw_row_fringe_bitmaps (w, row);
23336 }
23337
23338 return row->mouse_face_p;
23339 }
23340
23341
23342 /* Redraw those parts of glyphs rows during expose event handling that
23343 overlap other rows. Redrawing of an exposed line writes over parts
23344 of lines overlapping that exposed line; this function fixes that.
23345
23346 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23347 row in W's current matrix that is exposed and overlaps other rows.
23348 LAST_OVERLAPPING_ROW is the last such row. */
23349
23350 static void
23351 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23352 struct window *w;
23353 struct glyph_row *first_overlapping_row;
23354 struct glyph_row *last_overlapping_row;
23355 {
23356 struct glyph_row *row;
23357
23358 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23359 if (row->overlapping_p)
23360 {
23361 xassert (row->enabled_p && !row->mode_line_p);
23362
23363 if (row->used[LEFT_MARGIN_AREA])
23364 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23365
23366 if (row->used[TEXT_AREA])
23367 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23368
23369 if (row->used[RIGHT_MARGIN_AREA])
23370 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23371 }
23372 }
23373
23374
23375 /* Return non-zero if W's cursor intersects rectangle R. */
23376
23377 static int
23378 phys_cursor_in_rect_p (w, r)
23379 struct window *w;
23380 XRectangle *r;
23381 {
23382 XRectangle cr, result;
23383 struct glyph *cursor_glyph;
23384
23385 cursor_glyph = get_phys_cursor_glyph (w);
23386 if (cursor_glyph)
23387 {
23388 /* r is relative to W's box, but w->phys_cursor.x is relative
23389 to left edge of W's TEXT area. Adjust it. */
23390 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23391 cr.y = w->phys_cursor.y;
23392 cr.width = cursor_glyph->pixel_width;
23393 cr.height = w->phys_cursor_height;
23394 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23395 I assume the effect is the same -- and this is portable. */
23396 return x_intersect_rectangles (&cr, r, &result);
23397 }
23398 else
23399 return 0;
23400 }
23401
23402
23403 /* EXPORT:
23404 Draw a vertical window border to the right of window W if W doesn't
23405 have vertical scroll bars. */
23406
23407 void
23408 x_draw_vertical_border (w)
23409 struct window *w;
23410 {
23411 /* We could do better, if we knew what type of scroll-bar the adjacent
23412 windows (on either side) have... But we don't :-(
23413 However, I think this works ok. ++KFS 2003-04-25 */
23414
23415 /* Redraw borders between horizontally adjacent windows. Don't
23416 do it for frames with vertical scroll bars because either the
23417 right scroll bar of a window, or the left scroll bar of its
23418 neighbor will suffice as a border. */
23419 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23420 return;
23421
23422 if (!WINDOW_RIGHTMOST_P (w)
23423 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23424 {
23425 int x0, x1, y0, y1;
23426
23427 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23428 y1 -= 1;
23429
23430 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23431 x1 -= 1;
23432
23433 rif->draw_vertical_window_border (w, x1, y0, y1);
23434 }
23435 else if (!WINDOW_LEFTMOST_P (w)
23436 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23437 {
23438 int x0, x1, y0, y1;
23439
23440 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23441 y1 -= 1;
23442
23443 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23444 x0 -= 1;
23445
23446 rif->draw_vertical_window_border (w, x0, y0, y1);
23447 }
23448 }
23449
23450
23451 /* Redraw the part of window W intersection rectangle FR. Pixel
23452 coordinates in FR are frame-relative. Call this function with
23453 input blocked. Value is non-zero if the exposure overwrites
23454 mouse-face. */
23455
23456 static int
23457 expose_window (w, fr)
23458 struct window *w;
23459 XRectangle *fr;
23460 {
23461 struct frame *f = XFRAME (w->frame);
23462 XRectangle wr, r;
23463 int mouse_face_overwritten_p = 0;
23464
23465 /* If window is not yet fully initialized, do nothing. This can
23466 happen when toolkit scroll bars are used and a window is split.
23467 Reconfiguring the scroll bar will generate an expose for a newly
23468 created window. */
23469 if (w->current_matrix == NULL)
23470 return 0;
23471
23472 /* When we're currently updating the window, display and current
23473 matrix usually don't agree. Arrange for a thorough display
23474 later. */
23475 if (w == updated_window)
23476 {
23477 SET_FRAME_GARBAGED (f);
23478 return 0;
23479 }
23480
23481 /* Frame-relative pixel rectangle of W. */
23482 wr.x = WINDOW_LEFT_EDGE_X (w);
23483 wr.y = WINDOW_TOP_EDGE_Y (w);
23484 wr.width = WINDOW_TOTAL_WIDTH (w);
23485 wr.height = WINDOW_TOTAL_HEIGHT (w);
23486
23487 if (x_intersect_rectangles (fr, &wr, &r))
23488 {
23489 int yb = window_text_bottom_y (w);
23490 struct glyph_row *row;
23491 int cursor_cleared_p;
23492 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23493
23494 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23495 r.x, r.y, r.width, r.height));
23496
23497 /* Convert to window coordinates. */
23498 r.x -= WINDOW_LEFT_EDGE_X (w);
23499 r.y -= WINDOW_TOP_EDGE_Y (w);
23500
23501 /* Turn off the cursor. */
23502 if (!w->pseudo_window_p
23503 && phys_cursor_in_rect_p (w, &r))
23504 {
23505 x_clear_cursor (w);
23506 cursor_cleared_p = 1;
23507 }
23508 else
23509 cursor_cleared_p = 0;
23510
23511 /* Update lines intersecting rectangle R. */
23512 first_overlapping_row = last_overlapping_row = NULL;
23513 for (row = w->current_matrix->rows;
23514 row->enabled_p;
23515 ++row)
23516 {
23517 int y0 = row->y;
23518 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23519
23520 if ((y0 >= r.y && y0 < r.y + r.height)
23521 || (y1 > r.y && y1 < r.y + r.height)
23522 || (r.y >= y0 && r.y < y1)
23523 || (r.y + r.height > y0 && r.y + r.height < y1))
23524 {
23525 /* A header line may be overlapping, but there is no need
23526 to fix overlapping areas for them. KFS 2005-02-12 */
23527 if (row->overlapping_p && !row->mode_line_p)
23528 {
23529 if (first_overlapping_row == NULL)
23530 first_overlapping_row = row;
23531 last_overlapping_row = row;
23532 }
23533
23534 if (expose_line (w, row, &r))
23535 mouse_face_overwritten_p = 1;
23536 }
23537
23538 if (y1 >= yb)
23539 break;
23540 }
23541
23542 /* Display the mode line if there is one. */
23543 if (WINDOW_WANTS_MODELINE_P (w)
23544 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23545 row->enabled_p)
23546 && row->y < r.y + r.height)
23547 {
23548 if (expose_line (w, row, &r))
23549 mouse_face_overwritten_p = 1;
23550 }
23551
23552 if (!w->pseudo_window_p)
23553 {
23554 /* Fix the display of overlapping rows. */
23555 if (first_overlapping_row)
23556 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23557
23558 /* Draw border between windows. */
23559 x_draw_vertical_border (w);
23560
23561 /* Turn the cursor on again. */
23562 if (cursor_cleared_p)
23563 update_window_cursor (w, 1);
23564 }
23565 }
23566
23567 return mouse_face_overwritten_p;
23568 }
23569
23570
23571
23572 /* Redraw (parts) of all windows in the window tree rooted at W that
23573 intersect R. R contains frame pixel coordinates. Value is
23574 non-zero if the exposure overwrites mouse-face. */
23575
23576 static int
23577 expose_window_tree (w, r)
23578 struct window *w;
23579 XRectangle *r;
23580 {
23581 struct frame *f = XFRAME (w->frame);
23582 int mouse_face_overwritten_p = 0;
23583
23584 while (w && !FRAME_GARBAGED_P (f))
23585 {
23586 if (!NILP (w->hchild))
23587 mouse_face_overwritten_p
23588 |= expose_window_tree (XWINDOW (w->hchild), r);
23589 else if (!NILP (w->vchild))
23590 mouse_face_overwritten_p
23591 |= expose_window_tree (XWINDOW (w->vchild), r);
23592 else
23593 mouse_face_overwritten_p |= expose_window (w, r);
23594
23595 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23596 }
23597
23598 return mouse_face_overwritten_p;
23599 }
23600
23601
23602 /* EXPORT:
23603 Redisplay an exposed area of frame F. X and Y are the upper-left
23604 corner of the exposed rectangle. W and H are width and height of
23605 the exposed area. All are pixel values. W or H zero means redraw
23606 the entire frame. */
23607
23608 void
23609 expose_frame (f, x, y, w, h)
23610 struct frame *f;
23611 int x, y, w, h;
23612 {
23613 XRectangle r;
23614 int mouse_face_overwritten_p = 0;
23615
23616 TRACE ((stderr, "expose_frame "));
23617
23618 /* No need to redraw if frame will be redrawn soon. */
23619 if (FRAME_GARBAGED_P (f))
23620 {
23621 TRACE ((stderr, " garbaged\n"));
23622 return;
23623 }
23624
23625 /* If basic faces haven't been realized yet, there is no point in
23626 trying to redraw anything. This can happen when we get an expose
23627 event while Emacs is starting, e.g. by moving another window. */
23628 if (FRAME_FACE_CACHE (f) == NULL
23629 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23630 {
23631 TRACE ((stderr, " no faces\n"));
23632 return;
23633 }
23634
23635 if (w == 0 || h == 0)
23636 {
23637 r.x = r.y = 0;
23638 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23639 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23640 }
23641 else
23642 {
23643 r.x = x;
23644 r.y = y;
23645 r.width = w;
23646 r.height = h;
23647 }
23648
23649 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23650 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23651
23652 if (WINDOWP (f->tool_bar_window))
23653 mouse_face_overwritten_p
23654 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23655
23656 #ifdef HAVE_X_WINDOWS
23657 #ifndef MSDOS
23658 #ifndef USE_X_TOOLKIT
23659 if (WINDOWP (f->menu_bar_window))
23660 mouse_face_overwritten_p
23661 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23662 #endif /* not USE_X_TOOLKIT */
23663 #endif
23664 #endif
23665
23666 /* Some window managers support a focus-follows-mouse style with
23667 delayed raising of frames. Imagine a partially obscured frame,
23668 and moving the mouse into partially obscured mouse-face on that
23669 frame. The visible part of the mouse-face will be highlighted,
23670 then the WM raises the obscured frame. With at least one WM, KDE
23671 2.1, Emacs is not getting any event for the raising of the frame
23672 (even tried with SubstructureRedirectMask), only Expose events.
23673 These expose events will draw text normally, i.e. not
23674 highlighted. Which means we must redo the highlight here.
23675 Subsume it under ``we love X''. --gerd 2001-08-15 */
23676 /* Included in Windows version because Windows most likely does not
23677 do the right thing if any third party tool offers
23678 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23679 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23680 {
23681 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23682 if (f == dpyinfo->mouse_face_mouse_frame)
23683 {
23684 int x = dpyinfo->mouse_face_mouse_x;
23685 int y = dpyinfo->mouse_face_mouse_y;
23686 clear_mouse_face (dpyinfo);
23687 note_mouse_highlight (f, x, y);
23688 }
23689 }
23690 }
23691
23692
23693 /* EXPORT:
23694 Determine the intersection of two rectangles R1 and R2. Return
23695 the intersection in *RESULT. Value is non-zero if RESULT is not
23696 empty. */
23697
23698 int
23699 x_intersect_rectangles (r1, r2, result)
23700 XRectangle *r1, *r2, *result;
23701 {
23702 XRectangle *left, *right;
23703 XRectangle *upper, *lower;
23704 int intersection_p = 0;
23705
23706 /* Rearrange so that R1 is the left-most rectangle. */
23707 if (r1->x < r2->x)
23708 left = r1, right = r2;
23709 else
23710 left = r2, right = r1;
23711
23712 /* X0 of the intersection is right.x0, if this is inside R1,
23713 otherwise there is no intersection. */
23714 if (right->x <= left->x + left->width)
23715 {
23716 result->x = right->x;
23717
23718 /* The right end of the intersection is the minimum of the
23719 the right ends of left and right. */
23720 result->width = (min (left->x + left->width, right->x + right->width)
23721 - result->x);
23722
23723 /* Same game for Y. */
23724 if (r1->y < r2->y)
23725 upper = r1, lower = r2;
23726 else
23727 upper = r2, lower = r1;
23728
23729 /* The upper end of the intersection is lower.y0, if this is inside
23730 of upper. Otherwise, there is no intersection. */
23731 if (lower->y <= upper->y + upper->height)
23732 {
23733 result->y = lower->y;
23734
23735 /* The lower end of the intersection is the minimum of the lower
23736 ends of upper and lower. */
23737 result->height = (min (lower->y + lower->height,
23738 upper->y + upper->height)
23739 - result->y);
23740 intersection_p = 1;
23741 }
23742 }
23743
23744 return intersection_p;
23745 }
23746
23747 #endif /* HAVE_WINDOW_SYSTEM */
23748
23749 \f
23750 /***********************************************************************
23751 Initialization
23752 ***********************************************************************/
23753
23754 void
23755 syms_of_xdisp ()
23756 {
23757 Vwith_echo_area_save_vector = Qnil;
23758 staticpro (&Vwith_echo_area_save_vector);
23759
23760 Vmessage_stack = Qnil;
23761 staticpro (&Vmessage_stack);
23762
23763 Qinhibit_redisplay = intern ("inhibit-redisplay");
23764 staticpro (&Qinhibit_redisplay);
23765
23766 message_dolog_marker1 = Fmake_marker ();
23767 staticpro (&message_dolog_marker1);
23768 message_dolog_marker2 = Fmake_marker ();
23769 staticpro (&message_dolog_marker2);
23770 message_dolog_marker3 = Fmake_marker ();
23771 staticpro (&message_dolog_marker3);
23772
23773 #if GLYPH_DEBUG
23774 defsubr (&Sdump_frame_glyph_matrix);
23775 defsubr (&Sdump_glyph_matrix);
23776 defsubr (&Sdump_glyph_row);
23777 defsubr (&Sdump_tool_bar_row);
23778 defsubr (&Strace_redisplay);
23779 defsubr (&Strace_to_stderr);
23780 #endif
23781 #ifdef HAVE_WINDOW_SYSTEM
23782 defsubr (&Stool_bar_lines_needed);
23783 defsubr (&Slookup_image_map);
23784 #endif
23785 defsubr (&Sformat_mode_line);
23786
23787 staticpro (&Qmenu_bar_update_hook);
23788 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23789
23790 staticpro (&Qoverriding_terminal_local_map);
23791 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23792
23793 staticpro (&Qoverriding_local_map);
23794 Qoverriding_local_map = intern ("overriding-local-map");
23795
23796 staticpro (&Qwindow_scroll_functions);
23797 Qwindow_scroll_functions = intern ("window-scroll-functions");
23798
23799 staticpro (&Qredisplay_end_trigger_functions);
23800 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23801
23802 staticpro (&Qinhibit_point_motion_hooks);
23803 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23804
23805 QCdata = intern (":data");
23806 staticpro (&QCdata);
23807 Qdisplay = intern ("display");
23808 staticpro (&Qdisplay);
23809 Qspace_width = intern ("space-width");
23810 staticpro (&Qspace_width);
23811 Qraise = intern ("raise");
23812 staticpro (&Qraise);
23813 Qslice = intern ("slice");
23814 staticpro (&Qslice);
23815 Qspace = intern ("space");
23816 staticpro (&Qspace);
23817 Qmargin = intern ("margin");
23818 staticpro (&Qmargin);
23819 Qpointer = intern ("pointer");
23820 staticpro (&Qpointer);
23821 Qleft_margin = intern ("left-margin");
23822 staticpro (&Qleft_margin);
23823 Qright_margin = intern ("right-margin");
23824 staticpro (&Qright_margin);
23825 Qcenter = intern ("center");
23826 staticpro (&Qcenter);
23827 Qline_height = intern ("line-height");
23828 staticpro (&Qline_height);
23829 QCalign_to = intern (":align-to");
23830 staticpro (&QCalign_to);
23831 QCrelative_width = intern (":relative-width");
23832 staticpro (&QCrelative_width);
23833 QCrelative_height = intern (":relative-height");
23834 staticpro (&QCrelative_height);
23835 QCeval = intern (":eval");
23836 staticpro (&QCeval);
23837 QCpropertize = intern (":propertize");
23838 staticpro (&QCpropertize);
23839 QCfile = intern (":file");
23840 staticpro (&QCfile);
23841 Qfontified = intern ("fontified");
23842 staticpro (&Qfontified);
23843 Qfontification_functions = intern ("fontification-functions");
23844 staticpro (&Qfontification_functions);
23845 Qtrailing_whitespace = intern ("trailing-whitespace");
23846 staticpro (&Qtrailing_whitespace);
23847 Qescape_glyph = intern ("escape-glyph");
23848 staticpro (&Qescape_glyph);
23849 Qnobreak_space = intern ("nobreak-space");
23850 staticpro (&Qnobreak_space);
23851 Qimage = intern ("image");
23852 staticpro (&Qimage);
23853 QCmap = intern (":map");
23854 staticpro (&QCmap);
23855 QCpointer = intern (":pointer");
23856 staticpro (&QCpointer);
23857 Qrect = intern ("rect");
23858 staticpro (&Qrect);
23859 Qcircle = intern ("circle");
23860 staticpro (&Qcircle);
23861 Qpoly = intern ("poly");
23862 staticpro (&Qpoly);
23863 Qmessage_truncate_lines = intern ("message-truncate-lines");
23864 staticpro (&Qmessage_truncate_lines);
23865 Qgrow_only = intern ("grow-only");
23866 staticpro (&Qgrow_only);
23867 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23868 staticpro (&Qinhibit_menubar_update);
23869 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23870 staticpro (&Qinhibit_eval_during_redisplay);
23871 Qposition = intern ("position");
23872 staticpro (&Qposition);
23873 Qbuffer_position = intern ("buffer-position");
23874 staticpro (&Qbuffer_position);
23875 Qobject = intern ("object");
23876 staticpro (&Qobject);
23877 Qbar = intern ("bar");
23878 staticpro (&Qbar);
23879 Qhbar = intern ("hbar");
23880 staticpro (&Qhbar);
23881 Qbox = intern ("box");
23882 staticpro (&Qbox);
23883 Qhollow = intern ("hollow");
23884 staticpro (&Qhollow);
23885 Qhand = intern ("hand");
23886 staticpro (&Qhand);
23887 Qarrow = intern ("arrow");
23888 staticpro (&Qarrow);
23889 Qtext = intern ("text");
23890 staticpro (&Qtext);
23891 Qrisky_local_variable = intern ("risky-local-variable");
23892 staticpro (&Qrisky_local_variable);
23893 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23894 staticpro (&Qinhibit_free_realized_faces);
23895
23896 list_of_error = Fcons (Fcons (intern ("error"),
23897 Fcons (intern ("void-variable"), Qnil)),
23898 Qnil);
23899 staticpro (&list_of_error);
23900
23901 Qlast_arrow_position = intern ("last-arrow-position");
23902 staticpro (&Qlast_arrow_position);
23903 Qlast_arrow_string = intern ("last-arrow-string");
23904 staticpro (&Qlast_arrow_string);
23905
23906 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23907 staticpro (&Qoverlay_arrow_string);
23908 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23909 staticpro (&Qoverlay_arrow_bitmap);
23910
23911 echo_buffer[0] = echo_buffer[1] = Qnil;
23912 staticpro (&echo_buffer[0]);
23913 staticpro (&echo_buffer[1]);
23914
23915 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23916 staticpro (&echo_area_buffer[0]);
23917 staticpro (&echo_area_buffer[1]);
23918
23919 Vmessages_buffer_name = build_string ("*Messages*");
23920 staticpro (&Vmessages_buffer_name);
23921
23922 mode_line_proptrans_alist = Qnil;
23923 staticpro (&mode_line_proptrans_alist);
23924 mode_line_string_list = Qnil;
23925 staticpro (&mode_line_string_list);
23926 mode_line_string_face = Qnil;
23927 staticpro (&mode_line_string_face);
23928 mode_line_string_face_prop = Qnil;
23929 staticpro (&mode_line_string_face_prop);
23930 Vmode_line_unwind_vector = Qnil;
23931 staticpro (&Vmode_line_unwind_vector);
23932
23933 help_echo_string = Qnil;
23934 staticpro (&help_echo_string);
23935 help_echo_object = Qnil;
23936 staticpro (&help_echo_object);
23937 help_echo_window = Qnil;
23938 staticpro (&help_echo_window);
23939 previous_help_echo_string = Qnil;
23940 staticpro (&previous_help_echo_string);
23941 help_echo_pos = -1;
23942
23943 #ifdef HAVE_WINDOW_SYSTEM
23944 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23945 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23946 For example, if a block cursor is over a tab, it will be drawn as
23947 wide as that tab on the display. */);
23948 x_stretch_cursor_p = 0;
23949 #endif
23950
23951 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23952 doc: /* *Non-nil means highlight trailing whitespace.
23953 The face used for trailing whitespace is `trailing-whitespace'. */);
23954 Vshow_trailing_whitespace = Qnil;
23955
23956 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23957 doc: /* *Control highlighting of nobreak space and soft hyphen.
23958 A value of t means highlight the character itself (for nobreak space,
23959 use face `nobreak-space').
23960 A value of nil means no highlighting.
23961 Other values mean display the escape glyph followed by an ordinary
23962 space or ordinary hyphen. */);
23963 Vnobreak_char_display = Qt;
23964
23965 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23966 doc: /* *The pointer shape to show in void text areas.
23967 A value of nil means to show the text pointer. Other options are `arrow',
23968 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23969 Vvoid_text_area_pointer = Qarrow;
23970
23971 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23972 doc: /* Non-nil means don't actually do any redisplay.
23973 This is used for internal purposes. */);
23974 Vinhibit_redisplay = Qnil;
23975
23976 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23977 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23978 Vglobal_mode_string = Qnil;
23979
23980 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23981 doc: /* Marker for where to display an arrow on top of the buffer text.
23982 This must be the beginning of a line in order to work.
23983 See also `overlay-arrow-string'. */);
23984 Voverlay_arrow_position = Qnil;
23985
23986 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23987 doc: /* String to display as an arrow in non-window frames.
23988 See also `overlay-arrow-position'. */);
23989 Voverlay_arrow_string = build_string ("=>");
23990
23991 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23992 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23993 The symbols on this list are examined during redisplay to determine
23994 where to display overlay arrows. */);
23995 Voverlay_arrow_variable_list
23996 = Fcons (intern ("overlay-arrow-position"), Qnil);
23997
23998 DEFVAR_INT ("scroll-step", &scroll_step,
23999 doc: /* *The number of lines to try scrolling a window by when point moves out.
24000 If that fails to bring point back on frame, point is centered instead.
24001 If this is zero, point is always centered after it moves off frame.
24002 If you want scrolling to always be a line at a time, you should set
24003 `scroll-conservatively' to a large value rather than set this to 1. */);
24004
24005 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24006 doc: /* *Scroll up to this many lines, to bring point back on screen.
24007 A value of zero means to scroll the text to center point vertically
24008 in the window. */);
24009 scroll_conservatively = 0;
24010
24011 DEFVAR_INT ("scroll-margin", &scroll_margin,
24012 doc: /* *Number of lines of margin at the top and bottom of a window.
24013 Recenter the window whenever point gets within this many lines
24014 of the top or bottom of the window. */);
24015 scroll_margin = 0;
24016
24017 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24018 doc: /* Pixels per inch value for non-window system displays.
24019 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24020 Vdisplay_pixels_per_inch = make_float (72.0);
24021
24022 #if GLYPH_DEBUG
24023 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24024 #endif
24025
24026 DEFVAR_BOOL ("truncate-partial-width-windows",
24027 &truncate_partial_width_windows,
24028 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24029 truncate_partial_width_windows = 1;
24030
24031 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24032 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24033 Any other value means to use the appropriate face, `mode-line',
24034 `header-line', or `menu' respectively. */);
24035 mode_line_inverse_video = 1;
24036
24037 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24038 doc: /* *Maximum buffer size for which line number should be displayed.
24039 If the buffer is bigger than this, the line number does not appear
24040 in the mode line. A value of nil means no limit. */);
24041 Vline_number_display_limit = Qnil;
24042
24043 DEFVAR_INT ("line-number-display-limit-width",
24044 &line_number_display_limit_width,
24045 doc: /* *Maximum line width (in characters) for line number display.
24046 If the average length of the lines near point is bigger than this, then the
24047 line number may be omitted from the mode line. */);
24048 line_number_display_limit_width = 200;
24049
24050 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24051 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24052 highlight_nonselected_windows = 0;
24053
24054 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24055 doc: /* Non-nil if more than one frame is visible on this display.
24056 Minibuffer-only frames don't count, but iconified frames do.
24057 This variable is not guaranteed to be accurate except while processing
24058 `frame-title-format' and `icon-title-format'. */);
24059
24060 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24061 doc: /* Template for displaying the title bar of visible frames.
24062 \(Assuming the window manager supports this feature.)
24063
24064 This variable has the same structure as `mode-line-format', except that
24065 the %c and %l constructs are ignored. It is used only on frames for
24066 which no explicit name has been set \(see `modify-frame-parameters'). */);
24067
24068 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24069 doc: /* Template for displaying the title bar of an iconified frame.
24070 \(Assuming the window manager supports this feature.)
24071 This variable has the same structure as `mode-line-format' (which see),
24072 and is used only on frames for which no explicit name has been set
24073 \(see `modify-frame-parameters'). */);
24074 Vicon_title_format
24075 = Vframe_title_format
24076 = Fcons (intern ("multiple-frames"),
24077 Fcons (build_string ("%b"),
24078 Fcons (Fcons (empty_string,
24079 Fcons (intern ("invocation-name"),
24080 Fcons (build_string ("@"),
24081 Fcons (intern ("system-name"),
24082 Qnil)))),
24083 Qnil)));
24084
24085 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24086 doc: /* Maximum number of lines to keep in the message log buffer.
24087 If nil, disable message logging. If t, log messages but don't truncate
24088 the buffer when it becomes large. */);
24089 Vmessage_log_max = make_number (100);
24090
24091 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24092 doc: /* Functions called before redisplay, if window sizes have changed.
24093 The value should be a list of functions that take one argument.
24094 Just before redisplay, for each frame, if any of its windows have changed
24095 size since the last redisplay, or have been split or deleted,
24096 all the functions in the list are called, with the frame as argument. */);
24097 Vwindow_size_change_functions = Qnil;
24098
24099 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24100 doc: /* List of functions to call before redisplaying a window with scrolling.
24101 Each function is called with two arguments, the window
24102 and its new display-start position. Note that the value of `window-end'
24103 is not valid when these functions are called. */);
24104 Vwindow_scroll_functions = Qnil;
24105
24106 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24107 doc: /* Functions called when redisplay of a window reaches the end trigger.
24108 Each function is called with two arguments, the window and the end trigger value.
24109 See `set-window-redisplay-end-trigger'. */);
24110 Vredisplay_end_trigger_functions = Qnil;
24111
24112 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24113 doc: /* *Non-nil means autoselect window with mouse pointer.
24114 If nil, do not autoselect windows.
24115 A positive number means delay autoselection by that many seconds: a
24116 window is autoselected only after the mouse has remained in that
24117 window for the duration of the delay.
24118 A negative number has a similar effect, but causes windows to be
24119 autoselected only after the mouse has stopped moving. \(Because of
24120 the way Emacs compares mouse events, you will occasionally wait twice
24121 that time before the window gets selected.\)
24122 Any other value means to autoselect window instantaneously when the
24123 mouse pointer enters it.
24124
24125 Autoselection selects the minibuffer only if it is active, and never
24126 unselects the minibuffer if it is active. */);
24127 Vmouse_autoselect_window = Qnil;
24128
24129 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24130 doc: /* *Non-nil means automatically resize tool-bars.
24131 This dynamically changes the tool-bar's height to the minimum height
24132 that is needed to make all tool-bar items visible.
24133 If value is `grow-only', the tool-bar's height is only increased
24134 automatically; to decreace the tool-bar height, use \\[recenter]. */);
24135 Vauto_resize_tool_bars = Qt;
24136
24137 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24138 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24139 auto_raise_tool_bar_buttons_p = 1;
24140
24141 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24142 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24143 make_cursor_line_fully_visible_p = 1;
24144
24145 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24146 doc: /* *Border below tool-bar in pixels.
24147 If an integer, use it as the height of the border.
24148 If it is one of `internal-border-width' or `border-width', use the
24149 value of the corresponding frame parameter.
24150 Otherwise, no border is added below the tool-bar. */);
24151 Vtool_bar_border = Qinternal_border_width;
24152
24153 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24154 doc: /* *Margin around tool-bar buttons in pixels.
24155 If an integer, use that for both horizontal and vertical margins.
24156 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24157 HORZ specifying the horizontal margin, and VERT specifying the
24158 vertical margin. */);
24159 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24160
24161 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24162 doc: /* *Relief thickness of tool-bar buttons. */);
24163 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24164
24165 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24166 doc: /* List of functions to call to fontify regions of text.
24167 Each function is called with one argument POS. Functions must
24168 fontify a region starting at POS in the current buffer, and give
24169 fontified regions the property `fontified'. */);
24170 Vfontification_functions = Qnil;
24171 Fmake_variable_buffer_local (Qfontification_functions);
24172
24173 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24174 &unibyte_display_via_language_environment,
24175 doc: /* *Non-nil means display unibyte text according to language environment.
24176 Specifically this means that unibyte non-ASCII characters
24177 are displayed by converting them to the equivalent multibyte characters
24178 according to the current language environment. As a result, they are
24179 displayed according to the current fontset. */);
24180 unibyte_display_via_language_environment = 0;
24181
24182 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24183 doc: /* *Maximum height for resizing mini-windows.
24184 If a float, it specifies a fraction of the mini-window frame's height.
24185 If an integer, it specifies a number of lines. */);
24186 Vmax_mini_window_height = make_float (0.25);
24187
24188 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24189 doc: /* *How to resize mini-windows.
24190 A value of nil means don't automatically resize mini-windows.
24191 A value of t means resize them to fit the text displayed in them.
24192 A value of `grow-only', the default, means let mini-windows grow
24193 only, until their display becomes empty, at which point the windows
24194 go back to their normal size. */);
24195 Vresize_mini_windows = Qgrow_only;
24196
24197 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24198 doc: /* Alist specifying how to blink the cursor off.
24199 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24200 `cursor-type' frame-parameter or variable equals ON-STATE,
24201 comparing using `equal', Emacs uses OFF-STATE to specify
24202 how to blink it off. ON-STATE and OFF-STATE are values for
24203 the `cursor-type' frame parameter.
24204
24205 If a frame's ON-STATE has no entry in this list,
24206 the frame's other specifications determine how to blink the cursor off. */);
24207 Vblink_cursor_alist = Qnil;
24208
24209 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24210 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24211 automatic_hscrolling_p = 1;
24212
24213 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24214 doc: /* *How many columns away from the window edge point is allowed to get
24215 before automatic hscrolling will horizontally scroll the window. */);
24216 hscroll_margin = 5;
24217
24218 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24219 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24220 When point is less than `hscroll-margin' columns from the window
24221 edge, automatic hscrolling will scroll the window by the amount of columns
24222 determined by this variable. If its value is a positive integer, scroll that
24223 many columns. If it's a positive floating-point number, it specifies the
24224 fraction of the window's width to scroll. If it's nil or zero, point will be
24225 centered horizontally after the scroll. Any other value, including negative
24226 numbers, are treated as if the value were zero.
24227
24228 Automatic hscrolling always moves point outside the scroll margin, so if
24229 point was more than scroll step columns inside the margin, the window will
24230 scroll more than the value given by the scroll step.
24231
24232 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24233 and `scroll-right' overrides this variable's effect. */);
24234 Vhscroll_step = make_number (0);
24235
24236 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24237 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24238 Bind this around calls to `message' to let it take effect. */);
24239 message_truncate_lines = 0;
24240
24241 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24242 doc: /* Normal hook run to update the menu bar definitions.
24243 Redisplay runs this hook before it redisplays the menu bar.
24244 This is used to update submenus such as Buffers,
24245 whose contents depend on various data. */);
24246 Vmenu_bar_update_hook = Qnil;
24247
24248 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24249 doc: /* Frame for which we are updating a menu.
24250 The enable predicate for a menu binding should check this variable. */);
24251 Vmenu_updating_frame = Qnil;
24252
24253 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24254 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24255 inhibit_menubar_update = 0;
24256
24257 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24258 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24259 inhibit_eval_during_redisplay = 0;
24260
24261 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24262 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24263 inhibit_free_realized_faces = 0;
24264
24265 #if GLYPH_DEBUG
24266 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24267 doc: /* Inhibit try_window_id display optimization. */);
24268 inhibit_try_window_id = 0;
24269
24270 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24271 doc: /* Inhibit try_window_reusing display optimization. */);
24272 inhibit_try_window_reusing = 0;
24273
24274 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24275 doc: /* Inhibit try_cursor_movement display optimization. */);
24276 inhibit_try_cursor_movement = 0;
24277 #endif /* GLYPH_DEBUG */
24278
24279 DEFVAR_INT ("overline-margin", &overline_margin,
24280 doc: /* *Space between overline and text, in pixels.
24281 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24282 margin to the caracter height. */);
24283 overline_margin = 2;
24284 }
24285
24286
24287 /* Initialize this module when Emacs starts. */
24288
24289 void
24290 init_xdisp ()
24291 {
24292 Lisp_Object root_window;
24293 struct window *mini_w;
24294
24295 current_header_line_height = current_mode_line_height = -1;
24296
24297 CHARPOS (this_line_start_pos) = 0;
24298
24299 mini_w = XWINDOW (minibuf_window);
24300 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24301
24302 if (!noninteractive)
24303 {
24304 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24305 int i;
24306
24307 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24308 set_window_height (root_window,
24309 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24310 0);
24311 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24312 set_window_height (minibuf_window, 1, 0);
24313
24314 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24315 mini_w->total_cols = make_number (FRAME_COLS (f));
24316
24317 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24318 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24319 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24320
24321 /* The default ellipsis glyphs `...'. */
24322 for (i = 0; i < 3; ++i)
24323 default_invis_vector[i] = make_number ('.');
24324 }
24325
24326 {
24327 /* Allocate the buffer for frame titles.
24328 Also used for `format-mode-line'. */
24329 int size = 100;
24330 mode_line_noprop_buf = (char *) xmalloc (size);
24331 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24332 mode_line_noprop_ptr = mode_line_noprop_buf;
24333 mode_line_target = MODE_LINE_DISPLAY;
24334 }
24335
24336 help_echo_showing_p = 0;
24337 }
24338
24339
24340 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24341 (do not change this comment) */