]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs--devo--0
[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 3, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "fontset.h"
193 #include "blockinput.h"
194
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef MAC_OS
202 #include "macterm.h"
203 #endif
204
205 #ifdef HAVE_WINDOW_SYSTEM
206 #ifdef USE_FONT_BACKEND
207 #include "font.h"
208 #endif /* USE_FONT_BACKEND */
209 #endif /* HAVE_WINDOW_SYSTEM */
210
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
214
215 #define INFINITY 10000000
216
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
218 || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
222
223 extern int interrupt_input;
224 extern int command_loop_level;
225
226 extern Lisp_Object do_mouse_tracking;
227
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
230
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
243 Lisp_Object Qinhibit_point_motion_hooks;
244 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
245 Lisp_Object Qfontified;
246 Lisp_Object Qgrow_only;
247 Lisp_Object Qinhibit_eval_during_redisplay;
248 Lisp_Object Qbuffer_position, Qposition, Qobject;
249
250 /* Cursor shapes */
251 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
252
253 /* Pointer shapes */
254 Lisp_Object Qarrow, Qhand, Qtext;
255
256 Lisp_Object Qrisky_local_variable;
257
258 /* Holds the list (error). */
259 Lisp_Object list_of_error;
260
261 /* Functions called to fontify regions of text. */
262
263 Lisp_Object Vfontification_functions;
264 Lisp_Object Qfontification_functions;
265
266 /* Non-nil means automatically select any window when the mouse
267 cursor moves into it. */
268 Lisp_Object Vmouse_autoselect_window;
269
270 /* Non-zero means draw tool bar buttons raised when the mouse moves
271 over them. */
272
273 int auto_raise_tool_bar_buttons_p;
274
275 /* Non-zero means to reposition window if cursor line is only partially visible. */
276
277 int make_cursor_line_fully_visible_p;
278
279 /* Margin below tool bar in pixels. 0 or nil means no margin.
280 If value is `internal-border-width' or `border-width',
281 the corresponding frame parameter is used. */
282
283 Lisp_Object Vtool_bar_border;
284
285 /* Margin around tool bar buttons in pixels. */
286
287 Lisp_Object Vtool_bar_button_margin;
288
289 /* Thickness of shadow to draw around tool bar buttons. */
290
291 EMACS_INT tool_bar_button_relief;
292
293 /* Non-nil means automatically resize tool-bars so that all tool-bar
294 items are visible, and no blank lines remain.
295
296 If value is `grow-only', only make tool-bar bigger. */
297
298 Lisp_Object Vauto_resize_tool_bars;
299
300 /* Non-zero means draw block and hollow cursor as wide as the glyph
301 under it. For example, if a block cursor is over a tab, it will be
302 drawn as wide as that tab on the display. */
303
304 int x_stretch_cursor_p;
305
306 /* Non-nil means don't actually do any redisplay. */
307
308 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
309
310 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
311
312 int inhibit_eval_during_redisplay;
313
314 /* Names of text properties relevant for redisplay. */
315
316 Lisp_Object Qdisplay;
317 extern Lisp_Object Qface, Qinvisible, Qwidth;
318
319 /* Symbols used in text property values. */
320
321 Lisp_Object Vdisplay_pixels_per_inch;
322 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
323 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
324 Lisp_Object Qslice;
325 Lisp_Object Qcenter;
326 Lisp_Object Qmargin, Qpointer;
327 Lisp_Object Qline_height;
328 extern Lisp_Object Qheight;
329 extern Lisp_Object QCwidth, QCheight, QCascent;
330 extern Lisp_Object Qscroll_bar;
331 extern Lisp_Object Qcursor;
332
333 /* Non-nil means highlight trailing whitespace. */
334
335 Lisp_Object Vshow_trailing_whitespace;
336
337 /* Non-nil means escape non-break space and hyphens. */
338
339 Lisp_Object Vnobreak_char_display;
340
341 #ifdef HAVE_WINDOW_SYSTEM
342 extern Lisp_Object Voverflow_newline_into_fringe;
343
344 /* Test if overflow newline into fringe. Called with iterator IT
345 at or past right window margin, and with IT->current_x set. */
346
347 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
348 (!NILP (Voverflow_newline_into_fringe) \
349 && FRAME_WINDOW_P (it->f) \
350 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
351 && it->current_x == it->last_visible_x)
352
353 #endif /* HAVE_WINDOW_SYSTEM */
354
355 /* Non-nil means show the text cursor in void text areas
356 i.e. in blank areas after eol and eob. This used to be
357 the default in 21.3. */
358
359 Lisp_Object Vvoid_text_area_pointer;
360
361 /* Name of the face used to highlight trailing whitespace. */
362
363 Lisp_Object Qtrailing_whitespace;
364
365 /* Name and number of the face used to highlight escape glyphs. */
366
367 Lisp_Object Qescape_glyph;
368
369 /* Name and number of the face used to highlight non-breaking spaces. */
370
371 Lisp_Object Qnobreak_space;
372
373 /* The symbol `image' which is the car of the lists used to represent
374 images in Lisp. */
375
376 Lisp_Object Qimage;
377
378 /* The image map types. */
379 Lisp_Object QCmap, QCpointer;
380 Lisp_Object Qrect, Qcircle, Qpoly;
381
382 /* Non-zero means print newline to stdout before next mini-buffer
383 message. */
384
385 int noninteractive_need_newline;
386
387 /* Non-zero means print newline to message log before next message. */
388
389 static int message_log_need_newline;
390
391 /* Three markers that message_dolog uses.
392 It could allocate them itself, but that causes trouble
393 in handling memory-full errors. */
394 static Lisp_Object message_dolog_marker1;
395 static Lisp_Object message_dolog_marker2;
396 static Lisp_Object message_dolog_marker3;
397 \f
398 /* The buffer position of the first character appearing entirely or
399 partially on the line of the selected window which contains the
400 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
401 redisplay optimization in redisplay_internal. */
402
403 static struct text_pos this_line_start_pos;
404
405 /* Number of characters past the end of the line above, including the
406 terminating newline. */
407
408 static struct text_pos this_line_end_pos;
409
410 /* The vertical positions and the height of this line. */
411
412 static int this_line_vpos;
413 static int this_line_y;
414 static int this_line_pixel_height;
415
416 /* X position at which this display line starts. Usually zero;
417 negative if first character is partially visible. */
418
419 static int this_line_start_x;
420
421 /* Buffer that this_line_.* variables are referring to. */
422
423 static struct buffer *this_line_buffer;
424
425 /* Nonzero means truncate lines in all windows less wide than the
426 frame. */
427
428 int truncate_partial_width_windows;
429
430 /* A flag to control how to display unibyte 8-bit character. */
431
432 int unibyte_display_via_language_environment;
433
434 /* Nonzero means we have more than one non-mini-buffer-only frame.
435 Not guaranteed to be accurate except while parsing
436 frame-title-format. */
437
438 int multiple_frames;
439
440 Lisp_Object Vglobal_mode_string;
441
442
443 /* List of variables (symbols) which hold markers for overlay arrows.
444 The symbols on this list are examined during redisplay to determine
445 where to display overlay arrows. */
446
447 Lisp_Object Voverlay_arrow_variable_list;
448
449 /* Marker for where to display an arrow on top of the buffer text. */
450
451 Lisp_Object Voverlay_arrow_position;
452
453 /* String to display for the arrow. Only used on terminal frames. */
454
455 Lisp_Object Voverlay_arrow_string;
456
457 /* Values of those variables at last redisplay are stored as
458 properties on `overlay-arrow-position' symbol. However, if
459 Voverlay_arrow_position is a marker, last-arrow-position is its
460 numerical position. */
461
462 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
463
464 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
465 properties on a symbol in overlay-arrow-variable-list. */
466
467 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
468
469 /* Like mode-line-format, but for the title bar on a visible frame. */
470
471 Lisp_Object Vframe_title_format;
472
473 /* Like mode-line-format, but for the title bar on an iconified frame. */
474
475 Lisp_Object Vicon_title_format;
476
477 /* List of functions to call when a window's size changes. These
478 functions get one arg, a frame on which one or more windows' sizes
479 have changed. */
480
481 static Lisp_Object Vwindow_size_change_functions;
482
483 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
484
485 /* Nonzero if an overlay arrow has been displayed in this window. */
486
487 static int overlay_arrow_seen;
488
489 /* Nonzero means highlight the region even in nonselected windows. */
490
491 int highlight_nonselected_windows;
492
493 /* If cursor motion alone moves point off frame, try scrolling this
494 many lines up or down if that will bring it back. */
495
496 static EMACS_INT scroll_step;
497
498 /* Nonzero means scroll just far enough to bring point back on the
499 screen, when appropriate. */
500
501 static EMACS_INT scroll_conservatively;
502
503 /* Recenter the window whenever point gets within this many lines of
504 the top or bottom of the window. This value is translated into a
505 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
506 that there is really a fixed pixel height scroll margin. */
507
508 EMACS_INT scroll_margin;
509
510 /* Number of windows showing the buffer of the selected window (or
511 another buffer with the same base buffer). keyboard.c refers to
512 this. */
513
514 int buffer_shared;
515
516 /* Vector containing glyphs for an ellipsis `...'. */
517
518 static Lisp_Object default_invis_vector[3];
519
520 /* Zero means display the mode-line/header-line/menu-bar in the default face
521 (this slightly odd definition is for compatibility with previous versions
522 of emacs), non-zero means display them using their respective faces.
523
524 This variable is deprecated. */
525
526 int mode_line_inverse_video;
527
528 /* Prompt to display in front of the mini-buffer contents. */
529
530 Lisp_Object minibuf_prompt;
531
532 /* Width of current mini-buffer prompt. Only set after display_line
533 of the line that contains the prompt. */
534
535 int minibuf_prompt_width;
536
537 /* This is the window where the echo area message was displayed. It
538 is always a mini-buffer window, but it may not be the same window
539 currently active as a mini-buffer. */
540
541 Lisp_Object echo_area_window;
542
543 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
544 pushes the current message and the value of
545 message_enable_multibyte on the stack, the function restore_message
546 pops the stack and displays MESSAGE again. */
547
548 Lisp_Object Vmessage_stack;
549
550 /* Nonzero means multibyte characters were enabled when the echo area
551 message was specified. */
552
553 int message_enable_multibyte;
554
555 /* Nonzero if we should redraw the mode lines on the next redisplay. */
556
557 int update_mode_lines;
558
559 /* Nonzero if window sizes or contents have changed since last
560 redisplay that finished. */
561
562 int windows_or_buffers_changed;
563
564 /* Nonzero means a frame's cursor type has been changed. */
565
566 int cursor_type_changed;
567
568 /* Nonzero after display_mode_line if %l was used and it displayed a
569 line number. */
570
571 int line_number_displayed;
572
573 /* Maximum buffer size for which to display line numbers. */
574
575 Lisp_Object Vline_number_display_limit;
576
577 /* Line width to consider when repositioning for line number display. */
578
579 static EMACS_INT line_number_display_limit_width;
580
581 /* Number of lines to keep in the message log buffer. t means
582 infinite. nil means don't log at all. */
583
584 Lisp_Object Vmessage_log_max;
585
586 /* The name of the *Messages* buffer, a string. */
587
588 static Lisp_Object Vmessages_buffer_name;
589
590 /* Index 0 is the buffer that holds the current (desired) echo area message,
591 or nil if none is desired right now.
592
593 Index 1 is the buffer that holds the previously displayed echo area message,
594 or nil to indicate no message. This is normally what's on the screen now.
595
596 These two can point to the same buffer. That happens when the last
597 message output by the user (or made by echoing) has been displayed. */
598
599 Lisp_Object echo_area_buffer[2];
600
601 /* Permanent pointers to the two buffers that are used for echo area
602 purposes. Once the two buffers are made, and their pointers are
603 placed here, these two slots remain unchanged unless those buffers
604 need to be created afresh. */
605
606 static Lisp_Object echo_buffer[2];
607
608 /* A vector saved used in with_area_buffer to reduce consing. */
609
610 static Lisp_Object Vwith_echo_area_save_vector;
611
612 /* Non-zero means display_echo_area should display the last echo area
613 message again. Set by redisplay_preserve_echo_area. */
614
615 static int display_last_displayed_message_p;
616
617 /* Nonzero if echo area is being used by print; zero if being used by
618 message. */
619
620 int message_buf_print;
621
622 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
623
624 Lisp_Object Qinhibit_menubar_update;
625 int inhibit_menubar_update;
626
627 /* When evaluating expressions from menu bar items (enable conditions,
628 for instance), this is the frame they are being processed for. */
629
630 Lisp_Object Vmenu_updating_frame;
631
632 /* Maximum height for resizing mini-windows. Either a float
633 specifying a fraction of the available height, or an integer
634 specifying a number of lines. */
635
636 Lisp_Object Vmax_mini_window_height;
637
638 /* Non-zero means messages should be displayed with truncated
639 lines instead of being continued. */
640
641 int message_truncate_lines;
642 Lisp_Object Qmessage_truncate_lines;
643
644 /* Set to 1 in clear_message to make redisplay_internal aware
645 of an emptied echo area. */
646
647 static int message_cleared_p;
648
649 /* How to blink the default frame cursor off. */
650 Lisp_Object Vblink_cursor_alist;
651
652 /* A scratch glyph row with contents used for generating truncation
653 glyphs. Also used in direct_output_for_insert. */
654
655 #define MAX_SCRATCH_GLYPHS 100
656 struct glyph_row scratch_glyph_row;
657 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
658
659 /* Ascent and height of the last line processed by move_it_to. */
660
661 static int last_max_ascent, last_height;
662
663 /* Non-zero if there's a help-echo in the echo area. */
664
665 int help_echo_showing_p;
666
667 /* If >= 0, computed, exact values of mode-line and header-line height
668 to use in the macros CURRENT_MODE_LINE_HEIGHT and
669 CURRENT_HEADER_LINE_HEIGHT. */
670
671 int current_mode_line_height, current_header_line_height;
672
673 /* The maximum distance to look ahead for text properties. Values
674 that are too small let us call compute_char_face and similar
675 functions too often which is expensive. Values that are too large
676 let us call compute_char_face and alike too often because we
677 might not be interested in text properties that far away. */
678
679 #define TEXT_PROP_DISTANCE_LIMIT 100
680
681 #if GLYPH_DEBUG
682
683 /* Variables to turn off display optimizations from Lisp. */
684
685 int inhibit_try_window_id, inhibit_try_window_reusing;
686 int inhibit_try_cursor_movement;
687
688 /* Non-zero means print traces of redisplay if compiled with
689 GLYPH_DEBUG != 0. */
690
691 int trace_redisplay_p;
692
693 #endif /* GLYPH_DEBUG */
694
695 #ifdef DEBUG_TRACE_MOVE
696 /* Non-zero means trace with TRACE_MOVE to stderr. */
697 int trace_move;
698
699 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
700 #else
701 #define TRACE_MOVE(x) (void) 0
702 #endif
703
704 /* Non-zero means automatically scroll windows horizontally to make
705 point visible. */
706
707 int automatic_hscrolling_p;
708
709 /* How close to the margin can point get before the window is scrolled
710 horizontally. */
711 EMACS_INT hscroll_margin;
712
713 /* How much to scroll horizontally when point is inside the above margin. */
714 Lisp_Object Vhscroll_step;
715
716 /* The variable `resize-mini-windows'. If nil, don't resize
717 mini-windows. If t, always resize them to fit the text they
718 display. If `grow-only', let mini-windows grow only until they
719 become empty. */
720
721 Lisp_Object Vresize_mini_windows;
722
723 /* Buffer being redisplayed -- for redisplay_window_error. */
724
725 struct buffer *displayed_buffer;
726
727 /* Space between overline and text. */
728
729 EMACS_INT overline_margin;
730
731 /* Value returned from text property handlers (see below). */
732
733 enum prop_handled
734 {
735 HANDLED_NORMALLY,
736 HANDLED_RECOMPUTE_PROPS,
737 HANDLED_OVERLAY_STRING_CONSUMED,
738 HANDLED_RETURN
739 };
740
741 /* A description of text properties that redisplay is interested
742 in. */
743
744 struct props
745 {
746 /* The name of the property. */
747 Lisp_Object *name;
748
749 /* A unique index for the property. */
750 enum prop_idx idx;
751
752 /* A handler function called to set up iterator IT from the property
753 at IT's current position. Value is used to steer handle_stop. */
754 enum prop_handled (*handler) P_ ((struct it *it));
755 };
756
757 static enum prop_handled handle_face_prop P_ ((struct it *));
758 static enum prop_handled handle_invisible_prop P_ ((struct it *));
759 static enum prop_handled handle_display_prop P_ ((struct it *));
760 static enum prop_handled handle_composition_prop P_ ((struct it *));
761 static enum prop_handled handle_overlay_change P_ ((struct it *));
762 static enum prop_handled handle_fontified_prop P_ ((struct it *));
763 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
764
765 /* Properties handled by iterators. */
766
767 static struct props it_props[] =
768 {
769 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
770 /* Handle `face' before `display' because some sub-properties of
771 `display' need to know the face. */
772 {&Qface, FACE_PROP_IDX, handle_face_prop},
773 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
774 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
775 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
776 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
777 {NULL, 0, NULL}
778 };
779
780 /* Value is the position described by X. If X is a marker, value is
781 the marker_position of X. Otherwise, value is X. */
782
783 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
784
785 /* Enumeration returned by some move_it_.* functions internally. */
786
787 enum move_it_result
788 {
789 /* Not used. Undefined value. */
790 MOVE_UNDEFINED,
791
792 /* Move ended at the requested buffer position or ZV. */
793 MOVE_POS_MATCH_OR_ZV,
794
795 /* Move ended at the requested X pixel position. */
796 MOVE_X_REACHED,
797
798 /* Move within a line ended at the end of a line that must be
799 continued. */
800 MOVE_LINE_CONTINUED,
801
802 /* Move within a line ended at the end of a line that would
803 be displayed truncated. */
804 MOVE_LINE_TRUNCATED,
805
806 /* Move within a line ended at a line end. */
807 MOVE_NEWLINE_OR_CR
808 };
809
810 /* This counter is used to clear the face cache every once in a while
811 in redisplay_internal. It is incremented for each redisplay.
812 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
813 cleared. */
814
815 #define CLEAR_FACE_CACHE_COUNT 500
816 static int clear_face_cache_count;
817
818 /* Similarly for the image cache. */
819
820 #ifdef HAVE_WINDOW_SYSTEM
821 #define CLEAR_IMAGE_CACHE_COUNT 101
822 static int clear_image_cache_count;
823 #endif
824
825 /* Record the previous terminal frame we displayed. */
826
827 static struct frame *previous_terminal_frame;
828
829 /* Non-zero while redisplay_internal is in progress. */
830
831 int redisplaying_p;
832
833 /* Non-zero means don't free realized faces. Bound while freeing
834 realized faces is dangerous because glyph matrices might still
835 reference them. */
836
837 int inhibit_free_realized_faces;
838 Lisp_Object Qinhibit_free_realized_faces;
839
840 /* If a string, XTread_socket generates an event to display that string.
841 (The display is done in read_char.) */
842
843 Lisp_Object help_echo_string;
844 Lisp_Object help_echo_window;
845 Lisp_Object help_echo_object;
846 int help_echo_pos;
847
848 /* Temporary variable for XTread_socket. */
849
850 Lisp_Object previous_help_echo_string;
851
852 /* Null glyph slice */
853
854 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
855
856 \f
857 /* Function prototypes. */
858
859 static void setup_for_ellipsis P_ ((struct it *, int));
860 static void mark_window_display_accurate_1 P_ ((struct window *, int));
861 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
862 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
863 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
864 static int redisplay_mode_lines P_ ((Lisp_Object, int));
865 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
866
867 #if 0
868 static int invisible_text_between_p P_ ((struct it *, int, int));
869 #endif
870
871 static void pint2str P_ ((char *, int, int));
872 static void pint2hrstr P_ ((char *, int, int));
873 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
874 struct text_pos));
875 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
876 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
877 static void store_mode_line_noprop_char P_ ((char));
878 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
879 static void x_consider_frame_title P_ ((Lisp_Object));
880 static void handle_stop P_ ((struct it *));
881 static int tool_bar_lines_needed P_ ((struct frame *, int *));
882 static int single_display_spec_intangible_p P_ ((Lisp_Object));
883 static void ensure_echo_area_buffers P_ ((void));
884 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
885 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
886 static int with_echo_area_buffer P_ ((struct window *, int,
887 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
888 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
889 static void clear_garbaged_frames P_ ((void));
890 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
891 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
892 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
893 static int display_echo_area P_ ((struct window *));
894 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
895 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
896 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
897 static int string_char_and_length P_ ((const unsigned char *, int, int *));
898 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
899 struct text_pos));
900 static int compute_window_start_on_continuation_line P_ ((struct window *));
901 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
902 static void insert_left_trunc_glyphs P_ ((struct it *));
903 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
904 Lisp_Object));
905 static void extend_face_to_end_of_line P_ ((struct it *));
906 static int append_space_for_newline P_ ((struct it *, int));
907 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
908 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
909 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
910 static int trailing_whitespace_p P_ ((int));
911 static int message_log_check_duplicate P_ ((int, int, int, int));
912 static void push_it P_ ((struct it *));
913 static void pop_it P_ ((struct it *));
914 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
915 static void select_frame_for_redisplay P_ ((Lisp_Object));
916 static void redisplay_internal P_ ((int));
917 static int echo_area_display P_ ((int));
918 static void redisplay_windows P_ ((Lisp_Object));
919 static void redisplay_window P_ ((Lisp_Object, int));
920 static Lisp_Object redisplay_window_error ();
921 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
922 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
923 static int update_menu_bar P_ ((struct frame *, int, int));
924 static int try_window_reusing_current_matrix P_ ((struct window *));
925 static int try_window_id P_ ((struct window *));
926 static int display_line P_ ((struct it *));
927 static int display_mode_lines P_ ((struct window *));
928 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
929 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
930 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
931 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
932 static void display_menu_bar P_ ((struct window *));
933 static int display_count_lines P_ ((int, int, int, int, int *));
934 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
935 int, int, struct it *, int, int, int, int));
936 static void compute_line_metrics P_ ((struct it *));
937 static void run_redisplay_end_trigger_hook P_ ((struct it *));
938 static int get_overlay_strings P_ ((struct it *, int));
939 static int get_overlay_strings_1 P_ ((struct it *, int, int));
940 static void next_overlay_string P_ ((struct it *));
941 static void reseat P_ ((struct it *, struct text_pos, int));
942 static void reseat_1 P_ ((struct it *, struct text_pos, int));
943 static void back_to_previous_visible_line_start P_ ((struct it *));
944 void reseat_at_previous_visible_line_start P_ ((struct it *));
945 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
946 static int next_element_from_ellipsis P_ ((struct it *));
947 static int next_element_from_display_vector P_ ((struct it *));
948 static int next_element_from_string P_ ((struct it *));
949 static int next_element_from_c_string P_ ((struct it *));
950 static int next_element_from_buffer P_ ((struct it *));
951 static int next_element_from_composition P_ ((struct it *));
952 static int next_element_from_image P_ ((struct it *));
953 static int next_element_from_stretch P_ ((struct it *));
954 static void load_overlay_strings P_ ((struct it *, int));
955 static int init_from_display_pos P_ ((struct it *, struct window *,
956 struct display_pos *));
957 static void reseat_to_string P_ ((struct it *, unsigned char *,
958 Lisp_Object, int, int, int, int));
959 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
960 int, int, int));
961 void move_it_vertically_backward P_ ((struct it *, int));
962 static void init_to_row_start P_ ((struct it *, struct window *,
963 struct glyph_row *));
964 static int init_to_row_end P_ ((struct it *, struct window *,
965 struct glyph_row *));
966 static void back_to_previous_line_start P_ ((struct it *));
967 static int forward_to_next_line_start P_ ((struct it *, int *));
968 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
969 Lisp_Object, int));
970 static struct text_pos string_pos P_ ((int, Lisp_Object));
971 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
972 static int number_of_chars P_ ((unsigned char *, int));
973 static void compute_stop_pos P_ ((struct it *));
974 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
975 Lisp_Object));
976 static int face_before_or_after_it_pos P_ ((struct it *, int));
977 static int next_overlay_change P_ ((int));
978 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
979 Lisp_Object, struct text_pos *,
980 int));
981 static int underlying_face_id P_ ((struct it *));
982 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
983 struct window *));
984
985 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
986 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
987
988 #ifdef HAVE_WINDOW_SYSTEM
989
990 static void update_tool_bar P_ ((struct frame *, int));
991 static void build_desired_tool_bar_string P_ ((struct frame *f));
992 static int redisplay_tool_bar P_ ((struct frame *));
993 static void display_tool_bar_line P_ ((struct it *, int));
994 static void notice_overwritten_cursor P_ ((struct window *,
995 enum glyph_row_area,
996 int, int, int, int));
997
998
999
1000 #endif /* HAVE_WINDOW_SYSTEM */
1001
1002 \f
1003 /***********************************************************************
1004 Window display dimensions
1005 ***********************************************************************/
1006
1007 /* Return the bottom boundary y-position for text lines in window W.
1008 This is the first y position at which a line cannot start.
1009 It is relative to the top of the window.
1010
1011 This is the height of W minus the height of a mode line, if any. */
1012
1013 INLINE int
1014 window_text_bottom_y (w)
1015 struct window *w;
1016 {
1017 int height = WINDOW_TOTAL_HEIGHT (w);
1018
1019 if (WINDOW_WANTS_MODELINE_P (w))
1020 height -= CURRENT_MODE_LINE_HEIGHT (w);
1021 return height;
1022 }
1023
1024 /* Return the pixel width of display area AREA of window W. AREA < 0
1025 means return the total width of W, not including fringes to
1026 the left and right of the window. */
1027
1028 INLINE int
1029 window_box_width (w, area)
1030 struct window *w;
1031 int area;
1032 {
1033 int cols = XFASTINT (w->total_cols);
1034 int pixels = 0;
1035
1036 if (!w->pseudo_window_p)
1037 {
1038 cols -= WINDOW_SCROLL_BAR_COLS (w);
1039
1040 if (area == TEXT_AREA)
1041 {
1042 if (INTEGERP (w->left_margin_cols))
1043 cols -= XFASTINT (w->left_margin_cols);
1044 if (INTEGERP (w->right_margin_cols))
1045 cols -= XFASTINT (w->right_margin_cols);
1046 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1047 }
1048 else if (area == LEFT_MARGIN_AREA)
1049 {
1050 cols = (INTEGERP (w->left_margin_cols)
1051 ? XFASTINT (w->left_margin_cols) : 0);
1052 pixels = 0;
1053 }
1054 else if (area == RIGHT_MARGIN_AREA)
1055 {
1056 cols = (INTEGERP (w->right_margin_cols)
1057 ? XFASTINT (w->right_margin_cols) : 0);
1058 pixels = 0;
1059 }
1060 }
1061
1062 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1063 }
1064
1065
1066 /* Return the pixel height of the display area of window W, not
1067 including mode lines of W, if any. */
1068
1069 INLINE int
1070 window_box_height (w)
1071 struct window *w;
1072 {
1073 struct frame *f = XFRAME (w->frame);
1074 int height = WINDOW_TOTAL_HEIGHT (w);
1075
1076 xassert (height >= 0);
1077
1078 /* Note: the code below that determines the mode-line/header-line
1079 height is essentially the same as that contained in the macro
1080 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1081 the appropriate glyph row has its `mode_line_p' flag set,
1082 and if it doesn't, uses estimate_mode_line_height instead. */
1083
1084 if (WINDOW_WANTS_MODELINE_P (w))
1085 {
1086 struct glyph_row *ml_row
1087 = (w->current_matrix && w->current_matrix->rows
1088 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1089 : 0);
1090 if (ml_row && ml_row->mode_line_p)
1091 height -= ml_row->height;
1092 else
1093 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1094 }
1095
1096 if (WINDOW_WANTS_HEADER_LINE_P (w))
1097 {
1098 struct glyph_row *hl_row
1099 = (w->current_matrix && w->current_matrix->rows
1100 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1101 : 0);
1102 if (hl_row && hl_row->mode_line_p)
1103 height -= hl_row->height;
1104 else
1105 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1106 }
1107
1108 /* With a very small font and a mode-line that's taller than
1109 default, we might end up with a negative height. */
1110 return max (0, height);
1111 }
1112
1113 /* Return the window-relative coordinate of the left edge of display
1114 area AREA of window W. AREA < 0 means return the left edge of the
1115 whole window, to the right of the left fringe of W. */
1116
1117 INLINE int
1118 window_box_left_offset (w, area)
1119 struct window *w;
1120 int area;
1121 {
1122 int x;
1123
1124 if (w->pseudo_window_p)
1125 return 0;
1126
1127 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1128
1129 if (area == TEXT_AREA)
1130 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1131 + window_box_width (w, LEFT_MARGIN_AREA));
1132 else if (area == RIGHT_MARGIN_AREA)
1133 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1134 + window_box_width (w, LEFT_MARGIN_AREA)
1135 + window_box_width (w, TEXT_AREA)
1136 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1137 ? 0
1138 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1139 else if (area == LEFT_MARGIN_AREA
1140 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1141 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1142
1143 return x;
1144 }
1145
1146
1147 /* Return the window-relative coordinate of the right edge of display
1148 area AREA of window W. AREA < 0 means return the left edge of the
1149 whole window, to the left of the right fringe of W. */
1150
1151 INLINE int
1152 window_box_right_offset (w, area)
1153 struct window *w;
1154 int area;
1155 {
1156 return window_box_left_offset (w, area) + window_box_width (w, area);
1157 }
1158
1159 /* Return the frame-relative coordinate of the left edge of display
1160 area AREA of window W. AREA < 0 means return the left edge of the
1161 whole window, to the right of the left fringe of W. */
1162
1163 INLINE int
1164 window_box_left (w, area)
1165 struct window *w;
1166 int area;
1167 {
1168 struct frame *f = XFRAME (w->frame);
1169 int x;
1170
1171 if (w->pseudo_window_p)
1172 return FRAME_INTERNAL_BORDER_WIDTH (f);
1173
1174 x = (WINDOW_LEFT_EDGE_X (w)
1175 + window_box_left_offset (w, area));
1176
1177 return x;
1178 }
1179
1180
1181 /* Return the frame-relative coordinate of the right edge of display
1182 area AREA of window W. AREA < 0 means return the left edge of the
1183 whole window, to the left of the right fringe of W. */
1184
1185 INLINE int
1186 window_box_right (w, area)
1187 struct window *w;
1188 int area;
1189 {
1190 return window_box_left (w, area) + window_box_width (w, area);
1191 }
1192
1193 /* Get the bounding box of the display area AREA of window W, without
1194 mode lines, in frame-relative coordinates. AREA < 0 means the
1195 whole window, not including the left and right fringes of
1196 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1197 coordinates of the upper-left corner of the box. Return in
1198 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1199
1200 INLINE void
1201 window_box (w, area, box_x, box_y, box_width, box_height)
1202 struct window *w;
1203 int area;
1204 int *box_x, *box_y, *box_width, *box_height;
1205 {
1206 if (box_width)
1207 *box_width = window_box_width (w, area);
1208 if (box_height)
1209 *box_height = window_box_height (w);
1210 if (box_x)
1211 *box_x = window_box_left (w, area);
1212 if (box_y)
1213 {
1214 *box_y = WINDOW_TOP_EDGE_Y (w);
1215 if (WINDOW_WANTS_HEADER_LINE_P (w))
1216 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1217 }
1218 }
1219
1220
1221 /* Get the bounding box of the display area AREA of window W, without
1222 mode lines. AREA < 0 means the whole window, not including the
1223 left and right fringe of the window. Return in *TOP_LEFT_X
1224 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1225 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1226 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1227 box. */
1228
1229 INLINE void
1230 window_box_edges (w, area, top_left_x, top_left_y,
1231 bottom_right_x, bottom_right_y)
1232 struct window *w;
1233 int area;
1234 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1235 {
1236 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1237 bottom_right_y);
1238 *bottom_right_x += *top_left_x;
1239 *bottom_right_y += *top_left_y;
1240 }
1241
1242
1243 \f
1244 /***********************************************************************
1245 Utilities
1246 ***********************************************************************/
1247
1248 /* Return the bottom y-position of the line the iterator IT is in.
1249 This can modify IT's settings. */
1250
1251 int
1252 line_bottom_y (it)
1253 struct it *it;
1254 {
1255 int line_height = it->max_ascent + it->max_descent;
1256 int line_top_y = it->current_y;
1257
1258 if (line_height == 0)
1259 {
1260 if (last_height)
1261 line_height = last_height;
1262 else if (IT_CHARPOS (*it) < ZV)
1263 {
1264 move_it_by_lines (it, 1, 1);
1265 line_height = (it->max_ascent || it->max_descent
1266 ? it->max_ascent + it->max_descent
1267 : last_height);
1268 }
1269 else
1270 {
1271 struct glyph_row *row = it->glyph_row;
1272
1273 /* Use the default character height. */
1274 it->glyph_row = NULL;
1275 it->what = IT_CHARACTER;
1276 it->c = ' ';
1277 it->len = 1;
1278 PRODUCE_GLYPHS (it);
1279 line_height = it->ascent + it->descent;
1280 it->glyph_row = row;
1281 }
1282 }
1283
1284 return line_top_y + line_height;
1285 }
1286
1287
1288 /* Return 1 if position CHARPOS is visible in window W.
1289 CHARPOS < 0 means return info about WINDOW_END position.
1290 If visible, set *X and *Y to pixel coordinates of top left corner.
1291 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1292 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1293
1294 int
1295 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1296 struct window *w;
1297 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1298 {
1299 struct it it;
1300 struct text_pos top;
1301 int visible_p = 0;
1302 struct buffer *old_buffer = NULL;
1303
1304 if (noninteractive)
1305 return visible_p;
1306
1307 if (XBUFFER (w->buffer) != current_buffer)
1308 {
1309 old_buffer = current_buffer;
1310 set_buffer_internal_1 (XBUFFER (w->buffer));
1311 }
1312
1313 SET_TEXT_POS_FROM_MARKER (top, w->start);
1314
1315 /* Compute exact mode line heights. */
1316 if (WINDOW_WANTS_MODELINE_P (w))
1317 current_mode_line_height
1318 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1319 current_buffer->mode_line_format);
1320
1321 if (WINDOW_WANTS_HEADER_LINE_P (w))
1322 current_header_line_height
1323 = display_mode_line (w, HEADER_LINE_FACE_ID,
1324 current_buffer->header_line_format);
1325
1326 start_display (&it, w, top);
1327 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1328 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1329
1330 /* Note that we may overshoot because of invisible text. */
1331 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1332 {
1333 int top_x = it.current_x;
1334 int top_y = it.current_y;
1335 int bottom_y = (last_height = 0, line_bottom_y (&it));
1336 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1337
1338 if (top_y < window_top_y)
1339 visible_p = bottom_y > window_top_y;
1340 else if (top_y < it.last_visible_y)
1341 visible_p = 1;
1342 if (visible_p)
1343 {
1344 *x = top_x;
1345 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1346 *rtop = max (0, window_top_y - top_y);
1347 *rbot = max (0, bottom_y - it.last_visible_y);
1348 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1349 - max (top_y, window_top_y)));
1350 *vpos = it.vpos;
1351 }
1352 }
1353 else
1354 {
1355 struct it it2;
1356
1357 it2 = it;
1358 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1359 move_it_by_lines (&it, 1, 0);
1360 if (charpos < IT_CHARPOS (it)
1361 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1362 {
1363 visible_p = 1;
1364 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1365 *x = it2.current_x;
1366 *y = it2.current_y + it2.max_ascent - it2.ascent;
1367 *rtop = max (0, -it2.current_y);
1368 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1369 - it.last_visible_y));
1370 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1371 it.last_visible_y)
1372 - max (it2.current_y,
1373 WINDOW_HEADER_LINE_HEIGHT (w))));
1374 *vpos = it2.vpos;
1375 }
1376 }
1377
1378 if (old_buffer)
1379 set_buffer_internal_1 (old_buffer);
1380
1381 current_header_line_height = current_mode_line_height = -1;
1382
1383 if (visible_p && XFASTINT (w->hscroll) > 0)
1384 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1385
1386 #if 0
1387 /* Debugging code. */
1388 if (visible_p)
1389 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1390 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1391 else
1392 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1393 #endif
1394
1395 return visible_p;
1396 }
1397
1398
1399 /* Return the next character from STR which is MAXLEN bytes long.
1400 Return in *LEN the length of the character. This is like
1401 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1402 we find one, we return a `?', but with the length of the invalid
1403 character. */
1404
1405 static INLINE int
1406 string_char_and_length (str, maxlen, len)
1407 const unsigned char *str;
1408 int maxlen, *len;
1409 {
1410 int c;
1411
1412 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1413 if (!CHAR_VALID_P (c, 1))
1414 /* We may not change the length here because other places in Emacs
1415 don't use this function, i.e. they silently accept invalid
1416 characters. */
1417 c = '?';
1418
1419 return c;
1420 }
1421
1422
1423
1424 /* Given a position POS containing a valid character and byte position
1425 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1426
1427 static struct text_pos
1428 string_pos_nchars_ahead (pos, string, nchars)
1429 struct text_pos pos;
1430 Lisp_Object string;
1431 int nchars;
1432 {
1433 xassert (STRINGP (string) && nchars >= 0);
1434
1435 if (STRING_MULTIBYTE (string))
1436 {
1437 int rest = SBYTES (string) - BYTEPOS (pos);
1438 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1439 int len;
1440
1441 while (nchars--)
1442 {
1443 string_char_and_length (p, rest, &len);
1444 p += len, rest -= len;
1445 xassert (rest >= 0);
1446 CHARPOS (pos) += 1;
1447 BYTEPOS (pos) += len;
1448 }
1449 }
1450 else
1451 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1452
1453 return pos;
1454 }
1455
1456
1457 /* Value is the text position, i.e. character and byte position,
1458 for character position CHARPOS in STRING. */
1459
1460 static INLINE struct text_pos
1461 string_pos (charpos, string)
1462 int charpos;
1463 Lisp_Object string;
1464 {
1465 struct text_pos pos;
1466 xassert (STRINGP (string));
1467 xassert (charpos >= 0);
1468 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1469 return pos;
1470 }
1471
1472
1473 /* Value is a text position, i.e. character and byte position, for
1474 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1475 means recognize multibyte characters. */
1476
1477 static struct text_pos
1478 c_string_pos (charpos, s, multibyte_p)
1479 int charpos;
1480 unsigned char *s;
1481 int multibyte_p;
1482 {
1483 struct text_pos pos;
1484
1485 xassert (s != NULL);
1486 xassert (charpos >= 0);
1487
1488 if (multibyte_p)
1489 {
1490 int rest = strlen (s), len;
1491
1492 SET_TEXT_POS (pos, 0, 0);
1493 while (charpos--)
1494 {
1495 string_char_and_length (s, rest, &len);
1496 s += len, rest -= len;
1497 xassert (rest >= 0);
1498 CHARPOS (pos) += 1;
1499 BYTEPOS (pos) += len;
1500 }
1501 }
1502 else
1503 SET_TEXT_POS (pos, charpos, charpos);
1504
1505 return pos;
1506 }
1507
1508
1509 /* Value is the number of characters in C string S. MULTIBYTE_P
1510 non-zero means recognize multibyte characters. */
1511
1512 static int
1513 number_of_chars (s, multibyte_p)
1514 unsigned char *s;
1515 int multibyte_p;
1516 {
1517 int nchars;
1518
1519 if (multibyte_p)
1520 {
1521 int rest = strlen (s), len;
1522 unsigned char *p = (unsigned char *) s;
1523
1524 for (nchars = 0; rest > 0; ++nchars)
1525 {
1526 string_char_and_length (p, rest, &len);
1527 rest -= len, p += len;
1528 }
1529 }
1530 else
1531 nchars = strlen (s);
1532
1533 return nchars;
1534 }
1535
1536
1537 /* Compute byte position NEWPOS->bytepos corresponding to
1538 NEWPOS->charpos. POS is a known position in string STRING.
1539 NEWPOS->charpos must be >= POS.charpos. */
1540
1541 static void
1542 compute_string_pos (newpos, pos, string)
1543 struct text_pos *newpos, pos;
1544 Lisp_Object string;
1545 {
1546 xassert (STRINGP (string));
1547 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1548
1549 if (STRING_MULTIBYTE (string))
1550 *newpos = string_pos_nchars_ahead (pos, string,
1551 CHARPOS (*newpos) - CHARPOS (pos));
1552 else
1553 BYTEPOS (*newpos) = CHARPOS (*newpos);
1554 }
1555
1556 /* EXPORT:
1557 Return an estimation of the pixel height of mode or top lines on
1558 frame F. FACE_ID specifies what line's height to estimate. */
1559
1560 int
1561 estimate_mode_line_height (f, face_id)
1562 struct frame *f;
1563 enum face_id face_id;
1564 {
1565 #ifdef HAVE_WINDOW_SYSTEM
1566 if (FRAME_WINDOW_P (f))
1567 {
1568 int height = FONT_HEIGHT (FRAME_FONT (f));
1569
1570 /* This function is called so early when Emacs starts that the face
1571 cache and mode line face are not yet initialized. */
1572 if (FRAME_FACE_CACHE (f))
1573 {
1574 struct face *face = FACE_FROM_ID (f, face_id);
1575 if (face)
1576 {
1577 if (face->font)
1578 height = FONT_HEIGHT (face->font);
1579 if (face->box_line_width > 0)
1580 height += 2 * face->box_line_width;
1581 }
1582 }
1583
1584 return height;
1585 }
1586 #endif
1587
1588 return 1;
1589 }
1590
1591 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1592 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1593 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1594 not force the value into range. */
1595
1596 void
1597 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1598 FRAME_PTR f;
1599 register int pix_x, pix_y;
1600 int *x, *y;
1601 NativeRectangle *bounds;
1602 int noclip;
1603 {
1604
1605 #ifdef HAVE_WINDOW_SYSTEM
1606 if (FRAME_WINDOW_P (f))
1607 {
1608 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1609 even for negative values. */
1610 if (pix_x < 0)
1611 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1612 if (pix_y < 0)
1613 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1614
1615 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1616 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1617
1618 if (bounds)
1619 STORE_NATIVE_RECT (*bounds,
1620 FRAME_COL_TO_PIXEL_X (f, pix_x),
1621 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1622 FRAME_COLUMN_WIDTH (f) - 1,
1623 FRAME_LINE_HEIGHT (f) - 1);
1624
1625 if (!noclip)
1626 {
1627 if (pix_x < 0)
1628 pix_x = 0;
1629 else if (pix_x > FRAME_TOTAL_COLS (f))
1630 pix_x = FRAME_TOTAL_COLS (f);
1631
1632 if (pix_y < 0)
1633 pix_y = 0;
1634 else if (pix_y > FRAME_LINES (f))
1635 pix_y = FRAME_LINES (f);
1636 }
1637 }
1638 #endif
1639
1640 *x = pix_x;
1641 *y = pix_y;
1642 }
1643
1644
1645 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1646 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1647 can't tell the positions because W's display is not up to date,
1648 return 0. */
1649
1650 int
1651 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1652 struct window *w;
1653 int hpos, vpos;
1654 int *frame_x, *frame_y;
1655 {
1656 #ifdef HAVE_WINDOW_SYSTEM
1657 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1658 {
1659 int success_p;
1660
1661 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1662 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1663
1664 if (display_completed)
1665 {
1666 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1667 struct glyph *glyph = row->glyphs[TEXT_AREA];
1668 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1669
1670 hpos = row->x;
1671 vpos = row->y;
1672 while (glyph < end)
1673 {
1674 hpos += glyph->pixel_width;
1675 ++glyph;
1676 }
1677
1678 /* If first glyph is partially visible, its first visible position is still 0. */
1679 if (hpos < 0)
1680 hpos = 0;
1681
1682 success_p = 1;
1683 }
1684 else
1685 {
1686 hpos = vpos = 0;
1687 success_p = 0;
1688 }
1689
1690 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1691 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1692 return success_p;
1693 }
1694 #endif
1695
1696 *frame_x = hpos;
1697 *frame_y = vpos;
1698 return 1;
1699 }
1700
1701
1702 #ifdef HAVE_WINDOW_SYSTEM
1703
1704 /* Find the glyph under window-relative coordinates X/Y in window W.
1705 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1706 strings. Return in *HPOS and *VPOS the row and column number of
1707 the glyph found. Return in *AREA the glyph area containing X.
1708 Value is a pointer to the glyph found or null if X/Y is not on
1709 text, or we can't tell because W's current matrix is not up to
1710 date. */
1711
1712 static struct glyph *
1713 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1714 struct window *w;
1715 int x, y;
1716 int *hpos, *vpos, *dx, *dy, *area;
1717 {
1718 struct glyph *glyph, *end;
1719 struct glyph_row *row = NULL;
1720 int x0, i;
1721
1722 /* Find row containing Y. Give up if some row is not enabled. */
1723 for (i = 0; i < w->current_matrix->nrows; ++i)
1724 {
1725 row = MATRIX_ROW (w->current_matrix, i);
1726 if (!row->enabled_p)
1727 return NULL;
1728 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1729 break;
1730 }
1731
1732 *vpos = i;
1733 *hpos = 0;
1734
1735 /* Give up if Y is not in the window. */
1736 if (i == w->current_matrix->nrows)
1737 return NULL;
1738
1739 /* Get the glyph area containing X. */
1740 if (w->pseudo_window_p)
1741 {
1742 *area = TEXT_AREA;
1743 x0 = 0;
1744 }
1745 else
1746 {
1747 if (x < window_box_left_offset (w, TEXT_AREA))
1748 {
1749 *area = LEFT_MARGIN_AREA;
1750 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1751 }
1752 else if (x < window_box_right_offset (w, TEXT_AREA))
1753 {
1754 *area = TEXT_AREA;
1755 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1756 }
1757 else
1758 {
1759 *area = RIGHT_MARGIN_AREA;
1760 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1761 }
1762 }
1763
1764 /* Find glyph containing X. */
1765 glyph = row->glyphs[*area];
1766 end = glyph + row->used[*area];
1767 x -= x0;
1768 while (glyph < end && x >= glyph->pixel_width)
1769 {
1770 x -= glyph->pixel_width;
1771 ++glyph;
1772 }
1773
1774 if (glyph == end)
1775 return NULL;
1776
1777 if (dx)
1778 {
1779 *dx = x;
1780 *dy = y - (row->y + row->ascent - glyph->ascent);
1781 }
1782
1783 *hpos = glyph - row->glyphs[*area];
1784 return glyph;
1785 }
1786
1787
1788 /* EXPORT:
1789 Convert frame-relative x/y to coordinates relative to window W.
1790 Takes pseudo-windows into account. */
1791
1792 void
1793 frame_to_window_pixel_xy (w, x, y)
1794 struct window *w;
1795 int *x, *y;
1796 {
1797 if (w->pseudo_window_p)
1798 {
1799 /* A pseudo-window is always full-width, and starts at the
1800 left edge of the frame, plus a frame border. */
1801 struct frame *f = XFRAME (w->frame);
1802 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1803 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1804 }
1805 else
1806 {
1807 *x -= WINDOW_LEFT_EDGE_X (w);
1808 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1809 }
1810 }
1811
1812 /* EXPORT:
1813 Return in RECTS[] at most N clipping rectangles for glyph string S.
1814 Return the number of stored rectangles. */
1815
1816 int
1817 get_glyph_string_clip_rects (s, rects, n)
1818 struct glyph_string *s;
1819 NativeRectangle *rects;
1820 int n;
1821 {
1822 XRectangle r;
1823
1824 if (n <= 0)
1825 return 0;
1826
1827 if (s->row->full_width_p)
1828 {
1829 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1830 r.x = WINDOW_LEFT_EDGE_X (s->w);
1831 r.width = WINDOW_TOTAL_WIDTH (s->w);
1832
1833 /* Unless displaying a mode or menu bar line, which are always
1834 fully visible, clip to the visible part of the row. */
1835 if (s->w->pseudo_window_p)
1836 r.height = s->row->visible_height;
1837 else
1838 r.height = s->height;
1839 }
1840 else
1841 {
1842 /* This is a text line that may be partially visible. */
1843 r.x = window_box_left (s->w, s->area);
1844 r.width = window_box_width (s->w, s->area);
1845 r.height = s->row->visible_height;
1846 }
1847
1848 if (s->clip_head)
1849 if (r.x < s->clip_head->x)
1850 {
1851 if (r.width >= s->clip_head->x - r.x)
1852 r.width -= s->clip_head->x - r.x;
1853 else
1854 r.width = 0;
1855 r.x = s->clip_head->x;
1856 }
1857 if (s->clip_tail)
1858 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1859 {
1860 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1861 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1862 else
1863 r.width = 0;
1864 }
1865
1866 /* If S draws overlapping rows, it's sufficient to use the top and
1867 bottom of the window for clipping because this glyph string
1868 intentionally draws over other lines. */
1869 if (s->for_overlaps)
1870 {
1871 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1872 r.height = window_text_bottom_y (s->w) - r.y;
1873
1874 /* Alas, the above simple strategy does not work for the
1875 environments with anti-aliased text: if the same text is
1876 drawn onto the same place multiple times, it gets thicker.
1877 If the overlap we are processing is for the erased cursor, we
1878 take the intersection with the rectagle of the cursor. */
1879 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1880 {
1881 XRectangle rc, r_save = r;
1882
1883 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1884 rc.y = s->w->phys_cursor.y;
1885 rc.width = s->w->phys_cursor_width;
1886 rc.height = s->w->phys_cursor_height;
1887
1888 x_intersect_rectangles (&r_save, &rc, &r);
1889 }
1890 }
1891 else
1892 {
1893 /* Don't use S->y for clipping because it doesn't take partially
1894 visible lines into account. For example, it can be negative for
1895 partially visible lines at the top of a window. */
1896 if (!s->row->full_width_p
1897 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1898 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1899 else
1900 r.y = max (0, s->row->y);
1901
1902 /* If drawing a tool-bar window, draw it over the internal border
1903 at the top of the window. */
1904 if (WINDOWP (s->f->tool_bar_window)
1905 && s->w == XWINDOW (s->f->tool_bar_window))
1906 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1907 }
1908
1909 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1910
1911 /* If drawing the cursor, don't let glyph draw outside its
1912 advertised boundaries. Cleartype does this under some circumstances. */
1913 if (s->hl == DRAW_CURSOR)
1914 {
1915 struct glyph *glyph = s->first_glyph;
1916 int height, max_y;
1917
1918 if (s->x > r.x)
1919 {
1920 r.width -= s->x - r.x;
1921 r.x = s->x;
1922 }
1923 r.width = min (r.width, glyph->pixel_width);
1924
1925 /* If r.y is below window bottom, ensure that we still see a cursor. */
1926 height = min (glyph->ascent + glyph->descent,
1927 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1928 max_y = window_text_bottom_y (s->w) - height;
1929 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1930 if (s->ybase - glyph->ascent > max_y)
1931 {
1932 r.y = max_y;
1933 r.height = height;
1934 }
1935 else
1936 {
1937 /* Don't draw cursor glyph taller than our actual glyph. */
1938 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1939 if (height < r.height)
1940 {
1941 max_y = r.y + r.height;
1942 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1943 r.height = min (max_y - r.y, height);
1944 }
1945 }
1946 }
1947
1948 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1949 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1950 {
1951 #ifdef CONVERT_FROM_XRECT
1952 CONVERT_FROM_XRECT (r, *rects);
1953 #else
1954 *rects = r;
1955 #endif
1956 return 1;
1957 }
1958 else
1959 {
1960 /* If we are processing overlapping and allowed to return
1961 multiple clipping rectangles, we exclude the row of the glyph
1962 string from the clipping rectangle. This is to avoid drawing
1963 the same text on the environment with anti-aliasing. */
1964 #ifdef CONVERT_FROM_XRECT
1965 XRectangle rs[2];
1966 #else
1967 XRectangle *rs = rects;
1968 #endif
1969 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1970
1971 if (s->for_overlaps & OVERLAPS_PRED)
1972 {
1973 rs[i] = r;
1974 if (r.y + r.height > row_y)
1975 {
1976 if (r.y < row_y)
1977 rs[i].height = row_y - r.y;
1978 else
1979 rs[i].height = 0;
1980 }
1981 i++;
1982 }
1983 if (s->for_overlaps & OVERLAPS_SUCC)
1984 {
1985 rs[i] = r;
1986 if (r.y < row_y + s->row->visible_height)
1987 {
1988 if (r.y + r.height > row_y + s->row->visible_height)
1989 {
1990 rs[i].y = row_y + s->row->visible_height;
1991 rs[i].height = r.y + r.height - rs[i].y;
1992 }
1993 else
1994 rs[i].height = 0;
1995 }
1996 i++;
1997 }
1998
1999 n = i;
2000 #ifdef CONVERT_FROM_XRECT
2001 for (i = 0; i < n; i++)
2002 CONVERT_FROM_XRECT (rs[i], rects[i]);
2003 #endif
2004 return n;
2005 }
2006 }
2007
2008 /* EXPORT:
2009 Return in *NR the clipping rectangle for glyph string S. */
2010
2011 void
2012 get_glyph_string_clip_rect (s, nr)
2013 struct glyph_string *s;
2014 NativeRectangle *nr;
2015 {
2016 get_glyph_string_clip_rects (s, nr, 1);
2017 }
2018
2019
2020 /* EXPORT:
2021 Return the position and height of the phys cursor in window W.
2022 Set w->phys_cursor_width to width of phys cursor.
2023 */
2024
2025 void
2026 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2027 struct window *w;
2028 struct glyph_row *row;
2029 struct glyph *glyph;
2030 int *xp, *yp, *heightp;
2031 {
2032 struct frame *f = XFRAME (WINDOW_FRAME (w));
2033 int x, y, wd, h, h0, y0;
2034
2035 /* Compute the width of the rectangle to draw. If on a stretch
2036 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2037 rectangle as wide as the glyph, but use a canonical character
2038 width instead. */
2039 wd = glyph->pixel_width - 1;
2040 #ifdef HAVE_NTGUI
2041 wd++; /* Why? */
2042 #endif
2043
2044 x = w->phys_cursor.x;
2045 if (x < 0)
2046 {
2047 wd += x;
2048 x = 0;
2049 }
2050
2051 if (glyph->type == STRETCH_GLYPH
2052 && !x_stretch_cursor_p)
2053 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2054 w->phys_cursor_width = wd;
2055
2056 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2057
2058 /* If y is below window bottom, ensure that we still see a cursor. */
2059 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2060
2061 h = max (h0, glyph->ascent + glyph->descent);
2062 h0 = min (h0, glyph->ascent + glyph->descent);
2063
2064 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2065 if (y < y0)
2066 {
2067 h = max (h - (y0 - y) + 1, h0);
2068 y = y0 - 1;
2069 }
2070 else
2071 {
2072 y0 = window_text_bottom_y (w) - h0;
2073 if (y > y0)
2074 {
2075 h += y - y0;
2076 y = y0;
2077 }
2078 }
2079
2080 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2081 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2082 *heightp = h;
2083 }
2084
2085 /*
2086 * Remember which glyph the mouse is over.
2087 */
2088
2089 void
2090 remember_mouse_glyph (f, gx, gy, rect)
2091 struct frame *f;
2092 int gx, gy;
2093 NativeRectangle *rect;
2094 {
2095 Lisp_Object window;
2096 struct window *w;
2097 struct glyph_row *r, *gr, *end_row;
2098 enum window_part part;
2099 enum glyph_row_area area;
2100 int x, y, width, height;
2101
2102 /* Try to determine frame pixel position and size of the glyph under
2103 frame pixel coordinates X/Y on frame F. */
2104
2105 if (!f->glyphs_initialized_p
2106 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2107 NILP (window)))
2108 {
2109 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2110 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2111 goto virtual_glyph;
2112 }
2113
2114 w = XWINDOW (window);
2115 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2116 height = WINDOW_FRAME_LINE_HEIGHT (w);
2117
2118 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2119 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2120
2121 if (w->pseudo_window_p)
2122 {
2123 area = TEXT_AREA;
2124 part = ON_MODE_LINE; /* Don't adjust margin. */
2125 goto text_glyph;
2126 }
2127
2128 switch (part)
2129 {
2130 case ON_LEFT_MARGIN:
2131 area = LEFT_MARGIN_AREA;
2132 goto text_glyph;
2133
2134 case ON_RIGHT_MARGIN:
2135 area = RIGHT_MARGIN_AREA;
2136 goto text_glyph;
2137
2138 case ON_HEADER_LINE:
2139 case ON_MODE_LINE:
2140 gr = (part == ON_HEADER_LINE
2141 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2142 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2143 gy = gr->y;
2144 area = TEXT_AREA;
2145 goto text_glyph_row_found;
2146
2147 case ON_TEXT:
2148 area = TEXT_AREA;
2149
2150 text_glyph:
2151 gr = 0; gy = 0;
2152 for (; r <= end_row && r->enabled_p; ++r)
2153 if (r->y + r->height > y)
2154 {
2155 gr = r; gy = r->y;
2156 break;
2157 }
2158
2159 text_glyph_row_found:
2160 if (gr && gy <= y)
2161 {
2162 struct glyph *g = gr->glyphs[area];
2163 struct glyph *end = g + gr->used[area];
2164
2165 height = gr->height;
2166 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2167 if (gx + g->pixel_width > x)
2168 break;
2169
2170 if (g < end)
2171 {
2172 if (g->type == IMAGE_GLYPH)
2173 {
2174 /* Don't remember when mouse is over image, as
2175 image may have hot-spots. */
2176 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2177 return;
2178 }
2179 width = g->pixel_width;
2180 }
2181 else
2182 {
2183 /* Use nominal char spacing at end of line. */
2184 x -= gx;
2185 gx += (x / width) * width;
2186 }
2187
2188 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2189 gx += window_box_left_offset (w, area);
2190 }
2191 else
2192 {
2193 /* Use nominal line height at end of window. */
2194 gx = (x / width) * width;
2195 y -= gy;
2196 gy += (y / height) * height;
2197 }
2198 break;
2199
2200 case ON_LEFT_FRINGE:
2201 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2202 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2203 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2204 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2205 goto row_glyph;
2206
2207 case ON_RIGHT_FRINGE:
2208 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2209 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2210 : window_box_right_offset (w, TEXT_AREA));
2211 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2212 goto row_glyph;
2213
2214 case ON_SCROLL_BAR:
2215 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2216 ? 0
2217 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2218 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2219 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2220 : 0)));
2221 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2222
2223 row_glyph:
2224 gr = 0, gy = 0;
2225 for (; r <= end_row && r->enabled_p; ++r)
2226 if (r->y + r->height > y)
2227 {
2228 gr = r; gy = r->y;
2229 break;
2230 }
2231
2232 if (gr && gy <= y)
2233 height = gr->height;
2234 else
2235 {
2236 /* Use nominal line height at end of window. */
2237 y -= gy;
2238 gy += (y / height) * height;
2239 }
2240 break;
2241
2242 default:
2243 ;
2244 virtual_glyph:
2245 /* If there is no glyph under the mouse, then we divide the screen
2246 into a grid of the smallest glyph in the frame, and use that
2247 as our "glyph". */
2248
2249 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2250 round down even for negative values. */
2251 if (gx < 0)
2252 gx -= width - 1;
2253 if (gy < 0)
2254 gy -= height - 1;
2255
2256 gx = (gx / width) * width;
2257 gy = (gy / height) * height;
2258
2259 goto store_rect;
2260 }
2261
2262 gx += WINDOW_LEFT_EDGE_X (w);
2263 gy += WINDOW_TOP_EDGE_Y (w);
2264
2265 store_rect:
2266 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2267
2268 /* Visible feedback for debugging. */
2269 #if 0
2270 #if HAVE_X_WINDOWS
2271 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2272 f->output_data.x->normal_gc,
2273 gx, gy, width, height);
2274 #endif
2275 #endif
2276 }
2277
2278
2279 #endif /* HAVE_WINDOW_SYSTEM */
2280
2281 \f
2282 /***********************************************************************
2283 Lisp form evaluation
2284 ***********************************************************************/
2285
2286 /* Error handler for safe_eval and safe_call. */
2287
2288 static Lisp_Object
2289 safe_eval_handler (arg)
2290 Lisp_Object arg;
2291 {
2292 add_to_log ("Error during redisplay: %s", arg, Qnil);
2293 return Qnil;
2294 }
2295
2296
2297 /* Evaluate SEXPR and return the result, or nil if something went
2298 wrong. Prevent redisplay during the evaluation. */
2299
2300 Lisp_Object
2301 safe_eval (sexpr)
2302 Lisp_Object sexpr;
2303 {
2304 Lisp_Object val;
2305
2306 if (inhibit_eval_during_redisplay)
2307 val = Qnil;
2308 else
2309 {
2310 int count = SPECPDL_INDEX ();
2311 struct gcpro gcpro1;
2312
2313 GCPRO1 (sexpr);
2314 specbind (Qinhibit_redisplay, Qt);
2315 /* Use Qt to ensure debugger does not run,
2316 so there is no possibility of wanting to redisplay. */
2317 val = internal_condition_case_1 (Feval, sexpr, Qt,
2318 safe_eval_handler);
2319 UNGCPRO;
2320 val = unbind_to (count, val);
2321 }
2322
2323 return val;
2324 }
2325
2326
2327 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2328 Return the result, or nil if something went wrong. Prevent
2329 redisplay during the evaluation. */
2330
2331 Lisp_Object
2332 safe_call (nargs, args)
2333 int nargs;
2334 Lisp_Object *args;
2335 {
2336 Lisp_Object val;
2337
2338 if (inhibit_eval_during_redisplay)
2339 val = Qnil;
2340 else
2341 {
2342 int count = SPECPDL_INDEX ();
2343 struct gcpro gcpro1;
2344
2345 GCPRO1 (args[0]);
2346 gcpro1.nvars = nargs;
2347 specbind (Qinhibit_redisplay, Qt);
2348 /* Use Qt to ensure debugger does not run,
2349 so there is no possibility of wanting to redisplay. */
2350 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2351 safe_eval_handler);
2352 UNGCPRO;
2353 val = unbind_to (count, val);
2354 }
2355
2356 return val;
2357 }
2358
2359
2360 /* Call function FN with one argument ARG.
2361 Return the result, or nil if something went wrong. */
2362
2363 Lisp_Object
2364 safe_call1 (fn, arg)
2365 Lisp_Object fn, arg;
2366 {
2367 Lisp_Object args[2];
2368 args[0] = fn;
2369 args[1] = arg;
2370 return safe_call (2, args);
2371 }
2372
2373
2374 \f
2375 /***********************************************************************
2376 Debugging
2377 ***********************************************************************/
2378
2379 #if 0
2380
2381 /* Define CHECK_IT to perform sanity checks on iterators.
2382 This is for debugging. It is too slow to do unconditionally. */
2383
2384 static void
2385 check_it (it)
2386 struct it *it;
2387 {
2388 if (it->method == GET_FROM_STRING)
2389 {
2390 xassert (STRINGP (it->string));
2391 xassert (IT_STRING_CHARPOS (*it) >= 0);
2392 }
2393 else
2394 {
2395 xassert (IT_STRING_CHARPOS (*it) < 0);
2396 if (it->method == GET_FROM_BUFFER)
2397 {
2398 /* Check that character and byte positions agree. */
2399 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2400 }
2401 }
2402
2403 if (it->dpvec)
2404 xassert (it->current.dpvec_index >= 0);
2405 else
2406 xassert (it->current.dpvec_index < 0);
2407 }
2408
2409 #define CHECK_IT(IT) check_it ((IT))
2410
2411 #else /* not 0 */
2412
2413 #define CHECK_IT(IT) (void) 0
2414
2415 #endif /* not 0 */
2416
2417
2418 #if GLYPH_DEBUG
2419
2420 /* Check that the window end of window W is what we expect it
2421 to be---the last row in the current matrix displaying text. */
2422
2423 static void
2424 check_window_end (w)
2425 struct window *w;
2426 {
2427 if (!MINI_WINDOW_P (w)
2428 && !NILP (w->window_end_valid))
2429 {
2430 struct glyph_row *row;
2431 xassert ((row = MATRIX_ROW (w->current_matrix,
2432 XFASTINT (w->window_end_vpos)),
2433 !row->enabled_p
2434 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2435 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2436 }
2437 }
2438
2439 #define CHECK_WINDOW_END(W) check_window_end ((W))
2440
2441 #else /* not GLYPH_DEBUG */
2442
2443 #define CHECK_WINDOW_END(W) (void) 0
2444
2445 #endif /* not GLYPH_DEBUG */
2446
2447
2448 \f
2449 /***********************************************************************
2450 Iterator initialization
2451 ***********************************************************************/
2452
2453 /* Initialize IT for displaying current_buffer in window W, starting
2454 at character position CHARPOS. CHARPOS < 0 means that no buffer
2455 position is specified which is useful when the iterator is assigned
2456 a position later. BYTEPOS is the byte position corresponding to
2457 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2458
2459 If ROW is not null, calls to produce_glyphs with IT as parameter
2460 will produce glyphs in that row.
2461
2462 BASE_FACE_ID is the id of a base face to use. It must be one of
2463 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2464 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2465 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2466
2467 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2468 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2469 will be initialized to use the corresponding mode line glyph row of
2470 the desired matrix of W. */
2471
2472 void
2473 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2474 struct it *it;
2475 struct window *w;
2476 int charpos, bytepos;
2477 struct glyph_row *row;
2478 enum face_id base_face_id;
2479 {
2480 int highlight_region_p;
2481
2482 /* Some precondition checks. */
2483 xassert (w != NULL && it != NULL);
2484 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2485 && charpos <= ZV));
2486
2487 /* If face attributes have been changed since the last redisplay,
2488 free realized faces now because they depend on face definitions
2489 that might have changed. Don't free faces while there might be
2490 desired matrices pending which reference these faces. */
2491 if (face_change_count && !inhibit_free_realized_faces)
2492 {
2493 face_change_count = 0;
2494 free_all_realized_faces (Qnil);
2495 }
2496
2497 /* Use one of the mode line rows of W's desired matrix if
2498 appropriate. */
2499 if (row == NULL)
2500 {
2501 if (base_face_id == MODE_LINE_FACE_ID
2502 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2503 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2504 else if (base_face_id == HEADER_LINE_FACE_ID)
2505 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2506 }
2507
2508 /* Clear IT. */
2509 bzero (it, sizeof *it);
2510 it->current.overlay_string_index = -1;
2511 it->current.dpvec_index = -1;
2512 it->base_face_id = base_face_id;
2513 it->string = Qnil;
2514 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2515
2516 /* The window in which we iterate over current_buffer: */
2517 XSETWINDOW (it->window, w);
2518 it->w = w;
2519 it->f = XFRAME (w->frame);
2520
2521 /* Extra space between lines (on window systems only). */
2522 if (base_face_id == DEFAULT_FACE_ID
2523 && FRAME_WINDOW_P (it->f))
2524 {
2525 if (NATNUMP (current_buffer->extra_line_spacing))
2526 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2527 else if (FLOATP (current_buffer->extra_line_spacing))
2528 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2529 * FRAME_LINE_HEIGHT (it->f));
2530 else if (it->f->extra_line_spacing > 0)
2531 it->extra_line_spacing = it->f->extra_line_spacing;
2532 it->max_extra_line_spacing = 0;
2533 }
2534
2535 /* If realized faces have been removed, e.g. because of face
2536 attribute changes of named faces, recompute them. When running
2537 in batch mode, the face cache of Vterminal_frame is null. If
2538 we happen to get called, make a dummy face cache. */
2539 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2540 init_frame_faces (it->f);
2541 if (FRAME_FACE_CACHE (it->f)->used == 0)
2542 recompute_basic_faces (it->f);
2543
2544 /* Current value of the `slice', `space-width', and 'height' properties. */
2545 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2546 it->space_width = Qnil;
2547 it->font_height = Qnil;
2548 it->override_ascent = -1;
2549
2550 /* Are control characters displayed as `^C'? */
2551 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2552
2553 /* -1 means everything between a CR and the following line end
2554 is invisible. >0 means lines indented more than this value are
2555 invisible. */
2556 it->selective = (INTEGERP (current_buffer->selective_display)
2557 ? XFASTINT (current_buffer->selective_display)
2558 : (!NILP (current_buffer->selective_display)
2559 ? -1 : 0));
2560 it->selective_display_ellipsis_p
2561 = !NILP (current_buffer->selective_display_ellipses);
2562
2563 /* Display table to use. */
2564 it->dp = window_display_table (w);
2565
2566 /* Are multibyte characters enabled in current_buffer? */
2567 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2568
2569 /* Non-zero if we should highlight the region. */
2570 highlight_region_p
2571 = (!NILP (Vtransient_mark_mode)
2572 && !NILP (current_buffer->mark_active)
2573 && XMARKER (current_buffer->mark)->buffer != 0);
2574
2575 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2576 start and end of a visible region in window IT->w. Set both to
2577 -1 to indicate no region. */
2578 if (highlight_region_p
2579 /* Maybe highlight only in selected window. */
2580 && (/* Either show region everywhere. */
2581 highlight_nonselected_windows
2582 /* Or show region in the selected window. */
2583 || w == XWINDOW (selected_window)
2584 /* Or show the region if we are in the mini-buffer and W is
2585 the window the mini-buffer refers to. */
2586 || (MINI_WINDOW_P (XWINDOW (selected_window))
2587 && WINDOWP (minibuf_selected_window)
2588 && w == XWINDOW (minibuf_selected_window))))
2589 {
2590 int charpos = marker_position (current_buffer->mark);
2591 it->region_beg_charpos = min (PT, charpos);
2592 it->region_end_charpos = max (PT, charpos);
2593 }
2594 else
2595 it->region_beg_charpos = it->region_end_charpos = -1;
2596
2597 /* Get the position at which the redisplay_end_trigger hook should
2598 be run, if it is to be run at all. */
2599 if (MARKERP (w->redisplay_end_trigger)
2600 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2601 it->redisplay_end_trigger_charpos
2602 = marker_position (w->redisplay_end_trigger);
2603 else if (INTEGERP (w->redisplay_end_trigger))
2604 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2605
2606 /* Correct bogus values of tab_width. */
2607 it->tab_width = XINT (current_buffer->tab_width);
2608 if (it->tab_width <= 0 || it->tab_width > 1000)
2609 it->tab_width = 8;
2610
2611 /* Are lines in the display truncated? */
2612 it->truncate_lines_p
2613 = (base_face_id != DEFAULT_FACE_ID
2614 || XINT (it->w->hscroll)
2615 || (truncate_partial_width_windows
2616 && !WINDOW_FULL_WIDTH_P (it->w))
2617 || !NILP (current_buffer->truncate_lines));
2618
2619 /* Get dimensions of truncation and continuation glyphs. These are
2620 displayed as fringe bitmaps under X, so we don't need them for such
2621 frames. */
2622 if (!FRAME_WINDOW_P (it->f))
2623 {
2624 if (it->truncate_lines_p)
2625 {
2626 /* We will need the truncation glyph. */
2627 xassert (it->glyph_row == NULL);
2628 produce_special_glyphs (it, IT_TRUNCATION);
2629 it->truncation_pixel_width = it->pixel_width;
2630 }
2631 else
2632 {
2633 /* We will need the continuation glyph. */
2634 xassert (it->glyph_row == NULL);
2635 produce_special_glyphs (it, IT_CONTINUATION);
2636 it->continuation_pixel_width = it->pixel_width;
2637 }
2638
2639 /* Reset these values to zero because the produce_special_glyphs
2640 above has changed them. */
2641 it->pixel_width = it->ascent = it->descent = 0;
2642 it->phys_ascent = it->phys_descent = 0;
2643 }
2644
2645 /* Set this after getting the dimensions of truncation and
2646 continuation glyphs, so that we don't produce glyphs when calling
2647 produce_special_glyphs, above. */
2648 it->glyph_row = row;
2649 it->area = TEXT_AREA;
2650
2651 /* Get the dimensions of the display area. The display area
2652 consists of the visible window area plus a horizontally scrolled
2653 part to the left of the window. All x-values are relative to the
2654 start of this total display area. */
2655 if (base_face_id != DEFAULT_FACE_ID)
2656 {
2657 /* Mode lines, menu bar in terminal frames. */
2658 it->first_visible_x = 0;
2659 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2660 }
2661 else
2662 {
2663 it->first_visible_x
2664 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2665 it->last_visible_x = (it->first_visible_x
2666 + window_box_width (w, TEXT_AREA));
2667
2668 /* If we truncate lines, leave room for the truncator glyph(s) at
2669 the right margin. Otherwise, leave room for the continuation
2670 glyph(s). Truncation and continuation glyphs are not inserted
2671 for window-based redisplay. */
2672 if (!FRAME_WINDOW_P (it->f))
2673 {
2674 if (it->truncate_lines_p)
2675 it->last_visible_x -= it->truncation_pixel_width;
2676 else
2677 it->last_visible_x -= it->continuation_pixel_width;
2678 }
2679
2680 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2681 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2682 }
2683
2684 /* Leave room for a border glyph. */
2685 if (!FRAME_WINDOW_P (it->f)
2686 && !WINDOW_RIGHTMOST_P (it->w))
2687 it->last_visible_x -= 1;
2688
2689 it->last_visible_y = window_text_bottom_y (w);
2690
2691 /* For mode lines and alike, arrange for the first glyph having a
2692 left box line if the face specifies a box. */
2693 if (base_face_id != DEFAULT_FACE_ID)
2694 {
2695 struct face *face;
2696
2697 it->face_id = base_face_id;
2698
2699 /* If we have a boxed mode line, make the first character appear
2700 with a left box line. */
2701 face = FACE_FROM_ID (it->f, base_face_id);
2702 if (face->box != FACE_NO_BOX)
2703 it->start_of_box_run_p = 1;
2704 }
2705
2706 /* If a buffer position was specified, set the iterator there,
2707 getting overlays and face properties from that position. */
2708 if (charpos >= BUF_BEG (current_buffer))
2709 {
2710 it->end_charpos = ZV;
2711 it->face_id = -1;
2712 IT_CHARPOS (*it) = charpos;
2713
2714 /* Compute byte position if not specified. */
2715 if (bytepos < charpos)
2716 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2717 else
2718 IT_BYTEPOS (*it) = bytepos;
2719
2720 it->start = it->current;
2721
2722 /* Compute faces etc. */
2723 reseat (it, it->current.pos, 1);
2724 }
2725
2726 CHECK_IT (it);
2727 }
2728
2729
2730 /* Initialize IT for the display of window W with window start POS. */
2731
2732 void
2733 start_display (it, w, pos)
2734 struct it *it;
2735 struct window *w;
2736 struct text_pos pos;
2737 {
2738 struct glyph_row *row;
2739 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2740
2741 row = w->desired_matrix->rows + first_vpos;
2742 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2743 it->first_vpos = first_vpos;
2744
2745 /* Don't reseat to previous visible line start if current start
2746 position is in a string or image. */
2747 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2748 {
2749 int start_at_line_beg_p;
2750 int first_y = it->current_y;
2751
2752 /* If window start is not at a line start, skip forward to POS to
2753 get the correct continuation lines width. */
2754 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2755 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2756 if (!start_at_line_beg_p)
2757 {
2758 int new_x;
2759
2760 reseat_at_previous_visible_line_start (it);
2761 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2762
2763 new_x = it->current_x + it->pixel_width;
2764
2765 /* If lines are continued, this line may end in the middle
2766 of a multi-glyph character (e.g. a control character
2767 displayed as \003, or in the middle of an overlay
2768 string). In this case move_it_to above will not have
2769 taken us to the start of the continuation line but to the
2770 end of the continued line. */
2771 if (it->current_x > 0
2772 && !it->truncate_lines_p /* Lines are continued. */
2773 && (/* And glyph doesn't fit on the line. */
2774 new_x > it->last_visible_x
2775 /* Or it fits exactly and we're on a window
2776 system frame. */
2777 || (new_x == it->last_visible_x
2778 && FRAME_WINDOW_P (it->f))))
2779 {
2780 if (it->current.dpvec_index >= 0
2781 || it->current.overlay_string_index >= 0)
2782 {
2783 set_iterator_to_next (it, 1);
2784 move_it_in_display_line_to (it, -1, -1, 0);
2785 }
2786
2787 it->continuation_lines_width += it->current_x;
2788 }
2789
2790 /* We're starting a new display line, not affected by the
2791 height of the continued line, so clear the appropriate
2792 fields in the iterator structure. */
2793 it->max_ascent = it->max_descent = 0;
2794 it->max_phys_ascent = it->max_phys_descent = 0;
2795
2796 it->current_y = first_y;
2797 it->vpos = 0;
2798 it->current_x = it->hpos = 0;
2799 }
2800 }
2801
2802 #if 0 /* Don't assert the following because start_display is sometimes
2803 called intentionally with a window start that is not at a
2804 line start. Please leave this code in as a comment. */
2805
2806 /* Window start should be on a line start, now. */
2807 xassert (it->continuation_lines_width
2808 || IT_CHARPOS (it) == BEGV
2809 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2810 #endif /* 0 */
2811 }
2812
2813
2814 /* Return 1 if POS is a position in ellipses displayed for invisible
2815 text. W is the window we display, for text property lookup. */
2816
2817 static int
2818 in_ellipses_for_invisible_text_p (pos, w)
2819 struct display_pos *pos;
2820 struct window *w;
2821 {
2822 Lisp_Object prop, window;
2823 int ellipses_p = 0;
2824 int charpos = CHARPOS (pos->pos);
2825
2826 /* If POS specifies a position in a display vector, this might
2827 be for an ellipsis displayed for invisible text. We won't
2828 get the iterator set up for delivering that ellipsis unless
2829 we make sure that it gets aware of the invisible text. */
2830 if (pos->dpvec_index >= 0
2831 && pos->overlay_string_index < 0
2832 && CHARPOS (pos->string_pos) < 0
2833 && charpos > BEGV
2834 && (XSETWINDOW (window, w),
2835 prop = Fget_char_property (make_number (charpos),
2836 Qinvisible, window),
2837 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2838 {
2839 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2840 window);
2841 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2842 }
2843
2844 return ellipses_p;
2845 }
2846
2847
2848 /* Initialize IT for stepping through current_buffer in window W,
2849 starting at position POS that includes overlay string and display
2850 vector/ control character translation position information. Value
2851 is zero if there are overlay strings with newlines at POS. */
2852
2853 static int
2854 init_from_display_pos (it, w, pos)
2855 struct it *it;
2856 struct window *w;
2857 struct display_pos *pos;
2858 {
2859 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2860 int i, overlay_strings_with_newlines = 0;
2861
2862 /* If POS specifies a position in a display vector, this might
2863 be for an ellipsis displayed for invisible text. We won't
2864 get the iterator set up for delivering that ellipsis unless
2865 we make sure that it gets aware of the invisible text. */
2866 if (in_ellipses_for_invisible_text_p (pos, w))
2867 {
2868 --charpos;
2869 bytepos = 0;
2870 }
2871
2872 /* Keep in mind: the call to reseat in init_iterator skips invisible
2873 text, so we might end up at a position different from POS. This
2874 is only a problem when POS is a row start after a newline and an
2875 overlay starts there with an after-string, and the overlay has an
2876 invisible property. Since we don't skip invisible text in
2877 display_line and elsewhere immediately after consuming the
2878 newline before the row start, such a POS will not be in a string,
2879 but the call to init_iterator below will move us to the
2880 after-string. */
2881 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2882
2883 /* This only scans the current chunk -- it should scan all chunks.
2884 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2885 to 16 in 22.1 to make this a lesser problem. */
2886 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2887 {
2888 const char *s = SDATA (it->overlay_strings[i]);
2889 const char *e = s + SBYTES (it->overlay_strings[i]);
2890
2891 while (s < e && *s != '\n')
2892 ++s;
2893
2894 if (s < e)
2895 {
2896 overlay_strings_with_newlines = 1;
2897 break;
2898 }
2899 }
2900
2901 /* If position is within an overlay string, set up IT to the right
2902 overlay string. */
2903 if (pos->overlay_string_index >= 0)
2904 {
2905 int relative_index;
2906
2907 /* If the first overlay string happens to have a `display'
2908 property for an image, the iterator will be set up for that
2909 image, and we have to undo that setup first before we can
2910 correct the overlay string index. */
2911 if (it->method == GET_FROM_IMAGE)
2912 pop_it (it);
2913
2914 /* We already have the first chunk of overlay strings in
2915 IT->overlay_strings. Load more until the one for
2916 pos->overlay_string_index is in IT->overlay_strings. */
2917 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2918 {
2919 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2920 it->current.overlay_string_index = 0;
2921 while (n--)
2922 {
2923 load_overlay_strings (it, 0);
2924 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2925 }
2926 }
2927
2928 it->current.overlay_string_index = pos->overlay_string_index;
2929 relative_index = (it->current.overlay_string_index
2930 % OVERLAY_STRING_CHUNK_SIZE);
2931 it->string = it->overlay_strings[relative_index];
2932 xassert (STRINGP (it->string));
2933 it->current.string_pos = pos->string_pos;
2934 it->method = GET_FROM_STRING;
2935 }
2936
2937 #if 0 /* This is bogus because POS not having an overlay string
2938 position does not mean it's after the string. Example: A
2939 line starting with a before-string and initialization of IT
2940 to the previous row's end position. */
2941 else if (it->current.overlay_string_index >= 0)
2942 {
2943 /* If POS says we're already after an overlay string ending at
2944 POS, make sure to pop the iterator because it will be in
2945 front of that overlay string. When POS is ZV, we've thereby
2946 also ``processed'' overlay strings at ZV. */
2947 while (it->sp)
2948 pop_it (it);
2949 xassert (it->current.overlay_string_index == -1);
2950 xassert (it->method == GET_FROM_BUFFER);
2951 if (CHARPOS (pos->pos) == ZV)
2952 it->overlay_strings_at_end_processed_p = 1;
2953 }
2954 #endif /* 0 */
2955
2956 if (CHARPOS (pos->string_pos) >= 0)
2957 {
2958 /* Recorded position is not in an overlay string, but in another
2959 string. This can only be a string from a `display' property.
2960 IT should already be filled with that string. */
2961 it->current.string_pos = pos->string_pos;
2962 xassert (STRINGP (it->string));
2963 }
2964
2965 /* Restore position in display vector translations, control
2966 character translations or ellipses. */
2967 if (pos->dpvec_index >= 0)
2968 {
2969 if (it->dpvec == NULL)
2970 get_next_display_element (it);
2971 xassert (it->dpvec && it->current.dpvec_index == 0);
2972 it->current.dpvec_index = pos->dpvec_index;
2973 }
2974
2975 CHECK_IT (it);
2976 return !overlay_strings_with_newlines;
2977 }
2978
2979
2980 /* Initialize IT for stepping through current_buffer in window W
2981 starting at ROW->start. */
2982
2983 static void
2984 init_to_row_start (it, w, row)
2985 struct it *it;
2986 struct window *w;
2987 struct glyph_row *row;
2988 {
2989 init_from_display_pos (it, w, &row->start);
2990 it->start = row->start;
2991 it->continuation_lines_width = row->continuation_lines_width;
2992 CHECK_IT (it);
2993 }
2994
2995
2996 /* Initialize IT for stepping through current_buffer in window W
2997 starting in the line following ROW, i.e. starting at ROW->end.
2998 Value is zero if there are overlay strings with newlines at ROW's
2999 end position. */
3000
3001 static int
3002 init_to_row_end (it, w, row)
3003 struct it *it;
3004 struct window *w;
3005 struct glyph_row *row;
3006 {
3007 int success = 0;
3008
3009 if (init_from_display_pos (it, w, &row->end))
3010 {
3011 if (row->continued_p)
3012 it->continuation_lines_width
3013 = row->continuation_lines_width + row->pixel_width;
3014 CHECK_IT (it);
3015 success = 1;
3016 }
3017
3018 return success;
3019 }
3020
3021
3022
3023 \f
3024 /***********************************************************************
3025 Text properties
3026 ***********************************************************************/
3027
3028 /* Called when IT reaches IT->stop_charpos. Handle text property and
3029 overlay changes. Set IT->stop_charpos to the next position where
3030 to stop. */
3031
3032 static void
3033 handle_stop (it)
3034 struct it *it;
3035 {
3036 enum prop_handled handled;
3037 int handle_overlay_change_p;
3038 struct props *p;
3039
3040 it->dpvec = NULL;
3041 it->current.dpvec_index = -1;
3042 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3043 it->ignore_overlay_strings_at_pos_p = 0;
3044
3045 /* Use face of preceding text for ellipsis (if invisible) */
3046 if (it->selective_display_ellipsis_p)
3047 it->saved_face_id = it->face_id;
3048
3049 do
3050 {
3051 handled = HANDLED_NORMALLY;
3052
3053 /* Call text property handlers. */
3054 for (p = it_props; p->handler; ++p)
3055 {
3056 handled = p->handler (it);
3057
3058 if (handled == HANDLED_RECOMPUTE_PROPS)
3059 break;
3060 else if (handled == HANDLED_RETURN)
3061 {
3062 /* We still want to show before and after strings from
3063 overlays even if the actual buffer text is replaced. */
3064 if (!handle_overlay_change_p || it->sp > 1)
3065 return;
3066 if (!get_overlay_strings_1 (it, 0, 0))
3067 return;
3068 it->ignore_overlay_strings_at_pos_p = 1;
3069 it->string_from_display_prop_p = 0;
3070 handle_overlay_change_p = 0;
3071 handled = HANDLED_RECOMPUTE_PROPS;
3072 break;
3073 }
3074 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3075 handle_overlay_change_p = 0;
3076 }
3077
3078 if (handled != HANDLED_RECOMPUTE_PROPS)
3079 {
3080 /* Don't check for overlay strings below when set to deliver
3081 characters from a display vector. */
3082 if (it->method == GET_FROM_DISPLAY_VECTOR)
3083 handle_overlay_change_p = 0;
3084
3085 /* Handle overlay changes. */
3086 if (handle_overlay_change_p)
3087 handled = handle_overlay_change (it);
3088
3089 /* Determine where to stop next. */
3090 if (handled == HANDLED_NORMALLY)
3091 compute_stop_pos (it);
3092 }
3093 }
3094 while (handled == HANDLED_RECOMPUTE_PROPS);
3095 }
3096
3097
3098 /* Compute IT->stop_charpos from text property and overlay change
3099 information for IT's current position. */
3100
3101 static void
3102 compute_stop_pos (it)
3103 struct it *it;
3104 {
3105 register INTERVAL iv, next_iv;
3106 Lisp_Object object, limit, position;
3107
3108 /* If nowhere else, stop at the end. */
3109 it->stop_charpos = it->end_charpos;
3110
3111 if (STRINGP (it->string))
3112 {
3113 /* Strings are usually short, so don't limit the search for
3114 properties. */
3115 object = it->string;
3116 limit = Qnil;
3117 position = make_number (IT_STRING_CHARPOS (*it));
3118 }
3119 else
3120 {
3121 int charpos;
3122
3123 /* If next overlay change is in front of the current stop pos
3124 (which is IT->end_charpos), stop there. Note: value of
3125 next_overlay_change is point-max if no overlay change
3126 follows. */
3127 charpos = next_overlay_change (IT_CHARPOS (*it));
3128 if (charpos < it->stop_charpos)
3129 it->stop_charpos = charpos;
3130
3131 /* If showing the region, we have to stop at the region
3132 start or end because the face might change there. */
3133 if (it->region_beg_charpos > 0)
3134 {
3135 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3136 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3137 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3138 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3139 }
3140
3141 /* Set up variables for computing the stop position from text
3142 property changes. */
3143 XSETBUFFER (object, current_buffer);
3144 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3145 position = make_number (IT_CHARPOS (*it));
3146
3147 }
3148
3149 /* Get the interval containing IT's position. Value is a null
3150 interval if there isn't such an interval. */
3151 iv = validate_interval_range (object, &position, &position, 0);
3152 if (!NULL_INTERVAL_P (iv))
3153 {
3154 Lisp_Object values_here[LAST_PROP_IDX];
3155 struct props *p;
3156
3157 /* Get properties here. */
3158 for (p = it_props; p->handler; ++p)
3159 values_here[p->idx] = textget (iv->plist, *p->name);
3160
3161 /* Look for an interval following iv that has different
3162 properties. */
3163 for (next_iv = next_interval (iv);
3164 (!NULL_INTERVAL_P (next_iv)
3165 && (NILP (limit)
3166 || XFASTINT (limit) > next_iv->position));
3167 next_iv = next_interval (next_iv))
3168 {
3169 for (p = it_props; p->handler; ++p)
3170 {
3171 Lisp_Object new_value;
3172
3173 new_value = textget (next_iv->plist, *p->name);
3174 if (!EQ (values_here[p->idx], new_value))
3175 break;
3176 }
3177
3178 if (p->handler)
3179 break;
3180 }
3181
3182 if (!NULL_INTERVAL_P (next_iv))
3183 {
3184 if (INTEGERP (limit)
3185 && next_iv->position >= XFASTINT (limit))
3186 /* No text property change up to limit. */
3187 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3188 else
3189 /* Text properties change in next_iv. */
3190 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3191 }
3192 }
3193
3194 xassert (STRINGP (it->string)
3195 || (it->stop_charpos >= BEGV
3196 && it->stop_charpos >= IT_CHARPOS (*it)));
3197 }
3198
3199
3200 /* Return the position of the next overlay change after POS in
3201 current_buffer. Value is point-max if no overlay change
3202 follows. This is like `next-overlay-change' but doesn't use
3203 xmalloc. */
3204
3205 static int
3206 next_overlay_change (pos)
3207 int pos;
3208 {
3209 int noverlays;
3210 int endpos;
3211 Lisp_Object *overlays;
3212 int i;
3213
3214 /* Get all overlays at the given position. */
3215 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3216
3217 /* If any of these overlays ends before endpos,
3218 use its ending point instead. */
3219 for (i = 0; i < noverlays; ++i)
3220 {
3221 Lisp_Object oend;
3222 int oendpos;
3223
3224 oend = OVERLAY_END (overlays[i]);
3225 oendpos = OVERLAY_POSITION (oend);
3226 endpos = min (endpos, oendpos);
3227 }
3228
3229 return endpos;
3230 }
3231
3232
3233 \f
3234 /***********************************************************************
3235 Fontification
3236 ***********************************************************************/
3237
3238 /* Handle changes in the `fontified' property of the current buffer by
3239 calling hook functions from Qfontification_functions to fontify
3240 regions of text. */
3241
3242 static enum prop_handled
3243 handle_fontified_prop (it)
3244 struct it *it;
3245 {
3246 Lisp_Object prop, pos;
3247 enum prop_handled handled = HANDLED_NORMALLY;
3248
3249 if (!NILP (Vmemory_full))
3250 return handled;
3251
3252 /* Get the value of the `fontified' property at IT's current buffer
3253 position. (The `fontified' property doesn't have a special
3254 meaning in strings.) If the value is nil, call functions from
3255 Qfontification_functions. */
3256 if (!STRINGP (it->string)
3257 && it->s == NULL
3258 && !NILP (Vfontification_functions)
3259 && !NILP (Vrun_hooks)
3260 && (pos = make_number (IT_CHARPOS (*it)),
3261 prop = Fget_char_property (pos, Qfontified, Qnil),
3262 /* Ignore the special cased nil value always present at EOB since
3263 no amount of fontifying will be able to change it. */
3264 NILP (prop) && IT_CHARPOS (*it) < Z))
3265 {
3266 int count = SPECPDL_INDEX ();
3267 Lisp_Object val;
3268
3269 val = Vfontification_functions;
3270 specbind (Qfontification_functions, Qnil);
3271
3272 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3273 safe_call1 (val, pos);
3274 else
3275 {
3276 Lisp_Object globals, fn;
3277 struct gcpro gcpro1, gcpro2;
3278
3279 globals = Qnil;
3280 GCPRO2 (val, globals);
3281
3282 for (; CONSP (val); val = XCDR (val))
3283 {
3284 fn = XCAR (val);
3285
3286 if (EQ (fn, Qt))
3287 {
3288 /* A value of t indicates this hook has a local
3289 binding; it means to run the global binding too.
3290 In a global value, t should not occur. If it
3291 does, we must ignore it to avoid an endless
3292 loop. */
3293 for (globals = Fdefault_value (Qfontification_functions);
3294 CONSP (globals);
3295 globals = XCDR (globals))
3296 {
3297 fn = XCAR (globals);
3298 if (!EQ (fn, Qt))
3299 safe_call1 (fn, pos);
3300 }
3301 }
3302 else
3303 safe_call1 (fn, pos);
3304 }
3305
3306 UNGCPRO;
3307 }
3308
3309 unbind_to (count, Qnil);
3310
3311 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3312 something. This avoids an endless loop if they failed to
3313 fontify the text for which reason ever. */
3314 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3315 handled = HANDLED_RECOMPUTE_PROPS;
3316 }
3317
3318 return handled;
3319 }
3320
3321
3322 \f
3323 /***********************************************************************
3324 Faces
3325 ***********************************************************************/
3326
3327 /* Set up iterator IT from face properties at its current position.
3328 Called from handle_stop. */
3329
3330 static enum prop_handled
3331 handle_face_prop (it)
3332 struct it *it;
3333 {
3334 int new_face_id, next_stop;
3335
3336 if (!STRINGP (it->string))
3337 {
3338 new_face_id
3339 = face_at_buffer_position (it->w,
3340 IT_CHARPOS (*it),
3341 it->region_beg_charpos,
3342 it->region_end_charpos,
3343 &next_stop,
3344 (IT_CHARPOS (*it)
3345 + TEXT_PROP_DISTANCE_LIMIT),
3346 0);
3347
3348 /* Is this a start of a run of characters with box face?
3349 Caveat: this can be called for a freshly initialized
3350 iterator; face_id is -1 in this case. We know that the new
3351 face will not change until limit, i.e. if the new face has a
3352 box, all characters up to limit will have one. But, as
3353 usual, we don't know whether limit is really the end. */
3354 if (new_face_id != it->face_id)
3355 {
3356 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3357
3358 /* If new face has a box but old face has not, this is
3359 the start of a run of characters with box, i.e. it has
3360 a shadow on the left side. The value of face_id of the
3361 iterator will be -1 if this is the initial call that gets
3362 the face. In this case, we have to look in front of IT's
3363 position and see whether there is a face != new_face_id. */
3364 it->start_of_box_run_p
3365 = (new_face->box != FACE_NO_BOX
3366 && (it->face_id >= 0
3367 || IT_CHARPOS (*it) == BEG
3368 || new_face_id != face_before_it_pos (it)));
3369 it->face_box_p = new_face->box != FACE_NO_BOX;
3370 }
3371 }
3372 else
3373 {
3374 int base_face_id, bufpos;
3375
3376 if (it->current.overlay_string_index >= 0)
3377 bufpos = IT_CHARPOS (*it);
3378 else
3379 bufpos = 0;
3380
3381 /* For strings from a buffer, i.e. overlay strings or strings
3382 from a `display' property, use the face at IT's current
3383 buffer position as the base face to merge with, so that
3384 overlay strings appear in the same face as surrounding
3385 text, unless they specify their own faces. */
3386 base_face_id = underlying_face_id (it);
3387
3388 new_face_id = face_at_string_position (it->w,
3389 it->string,
3390 IT_STRING_CHARPOS (*it),
3391 bufpos,
3392 it->region_beg_charpos,
3393 it->region_end_charpos,
3394 &next_stop,
3395 base_face_id, 0);
3396
3397 #if 0 /* This shouldn't be neccessary. Let's check it. */
3398 /* If IT is used to display a mode line we would really like to
3399 use the mode line face instead of the frame's default face. */
3400 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3401 && new_face_id == DEFAULT_FACE_ID)
3402 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3403 #endif
3404
3405 /* Is this a start of a run of characters with box? Caveat:
3406 this can be called for a freshly allocated iterator; face_id
3407 is -1 is this case. We know that the new face will not
3408 change until the next check pos, i.e. if the new face has a
3409 box, all characters up to that position will have a
3410 box. But, as usual, we don't know whether that position
3411 is really the end. */
3412 if (new_face_id != it->face_id)
3413 {
3414 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3415 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3416
3417 /* If new face has a box but old face hasn't, this is the
3418 start of a run of characters with box, i.e. it has a
3419 shadow on the left side. */
3420 it->start_of_box_run_p
3421 = new_face->box && (old_face == NULL || !old_face->box);
3422 it->face_box_p = new_face->box != FACE_NO_BOX;
3423 }
3424 }
3425
3426 it->face_id = new_face_id;
3427 return HANDLED_NORMALLY;
3428 }
3429
3430
3431 /* Return the ID of the face ``underlying'' IT's current position,
3432 which is in a string. If the iterator is associated with a
3433 buffer, return the face at IT's current buffer position.
3434 Otherwise, use the iterator's base_face_id. */
3435
3436 static int
3437 underlying_face_id (it)
3438 struct it *it;
3439 {
3440 int face_id = it->base_face_id, i;
3441
3442 xassert (STRINGP (it->string));
3443
3444 for (i = it->sp - 1; i >= 0; --i)
3445 if (NILP (it->stack[i].string))
3446 face_id = it->stack[i].face_id;
3447
3448 return face_id;
3449 }
3450
3451
3452 /* Compute the face one character before or after the current position
3453 of IT. BEFORE_P non-zero means get the face in front of IT's
3454 position. Value is the id of the face. */
3455
3456 static int
3457 face_before_or_after_it_pos (it, before_p)
3458 struct it *it;
3459 int before_p;
3460 {
3461 int face_id, limit;
3462 int next_check_charpos;
3463 struct text_pos pos;
3464
3465 xassert (it->s == NULL);
3466
3467 if (STRINGP (it->string))
3468 {
3469 int bufpos, base_face_id;
3470
3471 /* No face change past the end of the string (for the case
3472 we are padding with spaces). No face change before the
3473 string start. */
3474 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3475 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3476 return it->face_id;
3477
3478 /* Set pos to the position before or after IT's current position. */
3479 if (before_p)
3480 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3481 else
3482 /* For composition, we must check the character after the
3483 composition. */
3484 pos = (it->what == IT_COMPOSITION
3485 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3486 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3487
3488 if (it->current.overlay_string_index >= 0)
3489 bufpos = IT_CHARPOS (*it);
3490 else
3491 bufpos = 0;
3492
3493 base_face_id = underlying_face_id (it);
3494
3495 /* Get the face for ASCII, or unibyte. */
3496 face_id = face_at_string_position (it->w,
3497 it->string,
3498 CHARPOS (pos),
3499 bufpos,
3500 it->region_beg_charpos,
3501 it->region_end_charpos,
3502 &next_check_charpos,
3503 base_face_id, 0);
3504
3505 /* Correct the face for charsets different from ASCII. Do it
3506 for the multibyte case only. The face returned above is
3507 suitable for unibyte text if IT->string is unibyte. */
3508 if (STRING_MULTIBYTE (it->string))
3509 {
3510 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3511 int rest = SBYTES (it->string) - BYTEPOS (pos);
3512 int c, len;
3513 struct face *face = FACE_FROM_ID (it->f, face_id);
3514
3515 c = string_char_and_length (p, rest, &len);
3516 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3517 }
3518 }
3519 else
3520 {
3521 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3522 || (IT_CHARPOS (*it) <= BEGV && before_p))
3523 return it->face_id;
3524
3525 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3526 pos = it->current.pos;
3527
3528 if (before_p)
3529 DEC_TEXT_POS (pos, it->multibyte_p);
3530 else
3531 {
3532 if (it->what == IT_COMPOSITION)
3533 /* For composition, we must check the position after the
3534 composition. */
3535 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3536 else
3537 INC_TEXT_POS (pos, it->multibyte_p);
3538 }
3539
3540 /* Determine face for CHARSET_ASCII, or unibyte. */
3541 face_id = face_at_buffer_position (it->w,
3542 CHARPOS (pos),
3543 it->region_beg_charpos,
3544 it->region_end_charpos,
3545 &next_check_charpos,
3546 limit, 0);
3547
3548 /* Correct the face for charsets different from ASCII. Do it
3549 for the multibyte case only. The face returned above is
3550 suitable for unibyte text if current_buffer is unibyte. */
3551 if (it->multibyte_p)
3552 {
3553 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3554 struct face *face = FACE_FROM_ID (it->f, face_id);
3555 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3556 }
3557 }
3558
3559 return face_id;
3560 }
3561
3562
3563 \f
3564 /***********************************************************************
3565 Invisible text
3566 ***********************************************************************/
3567
3568 /* Set up iterator IT from invisible properties at its current
3569 position. Called from handle_stop. */
3570
3571 static enum prop_handled
3572 handle_invisible_prop (it)
3573 struct it *it;
3574 {
3575 enum prop_handled handled = HANDLED_NORMALLY;
3576
3577 if (STRINGP (it->string))
3578 {
3579 extern Lisp_Object Qinvisible;
3580 Lisp_Object prop, end_charpos, limit, charpos;
3581
3582 /* Get the value of the invisible text property at the
3583 current position. Value will be nil if there is no such
3584 property. */
3585 charpos = make_number (IT_STRING_CHARPOS (*it));
3586 prop = Fget_text_property (charpos, Qinvisible, it->string);
3587
3588 if (!NILP (prop)
3589 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3590 {
3591 handled = HANDLED_RECOMPUTE_PROPS;
3592
3593 /* Get the position at which the next change of the
3594 invisible text property can be found in IT->string.
3595 Value will be nil if the property value is the same for
3596 all the rest of IT->string. */
3597 XSETINT (limit, SCHARS (it->string));
3598 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3599 it->string, limit);
3600
3601 /* Text at current position is invisible. The next
3602 change in the property is at position end_charpos.
3603 Move IT's current position to that position. */
3604 if (INTEGERP (end_charpos)
3605 && XFASTINT (end_charpos) < XFASTINT (limit))
3606 {
3607 struct text_pos old;
3608 old = it->current.string_pos;
3609 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3610 compute_string_pos (&it->current.string_pos, old, it->string);
3611 }
3612 else
3613 {
3614 /* The rest of the string is invisible. If this is an
3615 overlay string, proceed with the next overlay string
3616 or whatever comes and return a character from there. */
3617 if (it->current.overlay_string_index >= 0)
3618 {
3619 next_overlay_string (it);
3620 /* Don't check for overlay strings when we just
3621 finished processing them. */
3622 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3623 }
3624 else
3625 {
3626 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3627 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3628 }
3629 }
3630 }
3631 }
3632 else
3633 {
3634 int invis_p, newpos, next_stop, start_charpos;
3635 Lisp_Object pos, prop, overlay;
3636
3637 /* First of all, is there invisible text at this position? */
3638 start_charpos = IT_CHARPOS (*it);
3639 pos = make_number (IT_CHARPOS (*it));
3640 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3641 &overlay);
3642 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3643
3644 /* If we are on invisible text, skip over it. */
3645 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3646 {
3647 /* Record whether we have to display an ellipsis for the
3648 invisible text. */
3649 int display_ellipsis_p = invis_p == 2;
3650
3651 handled = HANDLED_RECOMPUTE_PROPS;
3652
3653 /* Loop skipping over invisible text. The loop is left at
3654 ZV or with IT on the first char being visible again. */
3655 do
3656 {
3657 /* Try to skip some invisible text. Return value is the
3658 position reached which can be equal to IT's position
3659 if there is nothing invisible here. This skips both
3660 over invisible text properties and overlays with
3661 invisible property. */
3662 newpos = skip_invisible (IT_CHARPOS (*it),
3663 &next_stop, ZV, it->window);
3664
3665 /* If we skipped nothing at all we weren't at invisible
3666 text in the first place. If everything to the end of
3667 the buffer was skipped, end the loop. */
3668 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3669 invis_p = 0;
3670 else
3671 {
3672 /* We skipped some characters but not necessarily
3673 all there are. Check if we ended up on visible
3674 text. Fget_char_property returns the property of
3675 the char before the given position, i.e. if we
3676 get invis_p = 0, this means that the char at
3677 newpos is visible. */
3678 pos = make_number (newpos);
3679 prop = Fget_char_property (pos, Qinvisible, it->window);
3680 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3681 }
3682
3683 /* If we ended up on invisible text, proceed to
3684 skip starting with next_stop. */
3685 if (invis_p)
3686 IT_CHARPOS (*it) = next_stop;
3687
3688 /* If there are adjacent invisible texts, don't lose the
3689 second one's ellipsis. */
3690 if (invis_p == 2)
3691 display_ellipsis_p = 1;
3692 }
3693 while (invis_p);
3694
3695 /* The position newpos is now either ZV or on visible text. */
3696 IT_CHARPOS (*it) = newpos;
3697 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3698
3699 /* If there are before-strings at the start of invisible
3700 text, and the text is invisible because of a text
3701 property, arrange to show before-strings because 20.x did
3702 it that way. (If the text is invisible because of an
3703 overlay property instead of a text property, this is
3704 already handled in the overlay code.) */
3705 if (NILP (overlay)
3706 && get_overlay_strings (it, start_charpos))
3707 {
3708 handled = HANDLED_RECOMPUTE_PROPS;
3709 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3710 }
3711 else if (display_ellipsis_p)
3712 {
3713 /* Make sure that the glyphs of the ellipsis will get
3714 correct `charpos' values. If we would not update
3715 it->position here, the glyphs would belong to the
3716 last visible character _before_ the invisible
3717 text, which confuses `set_cursor_from_row'.
3718
3719 We use the last invisible position instead of the
3720 first because this way the cursor is always drawn on
3721 the first "." of the ellipsis, whenever PT is inside
3722 the invisible text. Otherwise the cursor would be
3723 placed _after_ the ellipsis when the point is after the
3724 first invisible character. */
3725 if (!STRINGP (it->object))
3726 {
3727 it->position.charpos = IT_CHARPOS (*it) - 1;
3728 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3729 }
3730 setup_for_ellipsis (it, 0);
3731 }
3732 }
3733 }
3734
3735 return handled;
3736 }
3737
3738
3739 /* Make iterator IT return `...' next.
3740 Replaces LEN characters from buffer. */
3741
3742 static void
3743 setup_for_ellipsis (it, len)
3744 struct it *it;
3745 int len;
3746 {
3747 /* Use the display table definition for `...'. Invalid glyphs
3748 will be handled by the method returning elements from dpvec. */
3749 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3750 {
3751 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3752 it->dpvec = v->contents;
3753 it->dpend = v->contents + v->size;
3754 }
3755 else
3756 {
3757 /* Default `...'. */
3758 it->dpvec = default_invis_vector;
3759 it->dpend = default_invis_vector + 3;
3760 }
3761
3762 it->dpvec_char_len = len;
3763 it->current.dpvec_index = 0;
3764 it->dpvec_face_id = -1;
3765
3766 /* Remember the current face id in case glyphs specify faces.
3767 IT's face is restored in set_iterator_to_next.
3768 saved_face_id was set to preceding char's face in handle_stop. */
3769 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3770 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3771
3772 it->method = GET_FROM_DISPLAY_VECTOR;
3773 it->ellipsis_p = 1;
3774 }
3775
3776
3777 \f
3778 /***********************************************************************
3779 'display' property
3780 ***********************************************************************/
3781
3782 /* Set up iterator IT from `display' property at its current position.
3783 Called from handle_stop.
3784 We return HANDLED_RETURN if some part of the display property
3785 overrides the display of the buffer text itself.
3786 Otherwise we return HANDLED_NORMALLY. */
3787
3788 static enum prop_handled
3789 handle_display_prop (it)
3790 struct it *it;
3791 {
3792 Lisp_Object prop, object;
3793 struct text_pos *position;
3794 /* Nonzero if some property replaces the display of the text itself. */
3795 int display_replaced_p = 0;
3796
3797 if (STRINGP (it->string))
3798 {
3799 object = it->string;
3800 position = &it->current.string_pos;
3801 }
3802 else
3803 {
3804 XSETWINDOW (object, it->w);
3805 position = &it->current.pos;
3806 }
3807
3808 /* Reset those iterator values set from display property values. */
3809 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3810 it->space_width = Qnil;
3811 it->font_height = Qnil;
3812 it->voffset = 0;
3813
3814 /* We don't support recursive `display' properties, i.e. string
3815 values that have a string `display' property, that have a string
3816 `display' property etc. */
3817 if (!it->string_from_display_prop_p)
3818 it->area = TEXT_AREA;
3819
3820 prop = Fget_char_property (make_number (position->charpos),
3821 Qdisplay, object);
3822 if (NILP (prop))
3823 return HANDLED_NORMALLY;
3824
3825 if (!STRINGP (it->string))
3826 object = it->w->buffer;
3827
3828 if (CONSP (prop)
3829 /* Simple properties. */
3830 && !EQ (XCAR (prop), Qimage)
3831 && !EQ (XCAR (prop), Qspace)
3832 && !EQ (XCAR (prop), Qwhen)
3833 && !EQ (XCAR (prop), Qslice)
3834 && !EQ (XCAR (prop), Qspace_width)
3835 && !EQ (XCAR (prop), Qheight)
3836 && !EQ (XCAR (prop), Qraise)
3837 /* Marginal area specifications. */
3838 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3839 && !EQ (XCAR (prop), Qleft_fringe)
3840 && !EQ (XCAR (prop), Qright_fringe)
3841 && !NILP (XCAR (prop)))
3842 {
3843 for (; CONSP (prop); prop = XCDR (prop))
3844 {
3845 if (handle_single_display_spec (it, XCAR (prop), object,
3846 position, display_replaced_p))
3847 display_replaced_p = 1;
3848 }
3849 }
3850 else if (VECTORP (prop))
3851 {
3852 int i;
3853 for (i = 0; i < ASIZE (prop); ++i)
3854 if (handle_single_display_spec (it, AREF (prop, i), object,
3855 position, display_replaced_p))
3856 display_replaced_p = 1;
3857 }
3858 else
3859 {
3860 int ret = handle_single_display_spec (it, prop, object, position, 0);
3861 if (ret < 0) /* Replaced by "", i.e. nothing. */
3862 return HANDLED_RECOMPUTE_PROPS;
3863 if (ret)
3864 display_replaced_p = 1;
3865 }
3866
3867 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3868 }
3869
3870
3871 /* Value is the position of the end of the `display' property starting
3872 at START_POS in OBJECT. */
3873
3874 static struct text_pos
3875 display_prop_end (it, object, start_pos)
3876 struct it *it;
3877 Lisp_Object object;
3878 struct text_pos start_pos;
3879 {
3880 Lisp_Object end;
3881 struct text_pos end_pos;
3882
3883 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3884 Qdisplay, object, Qnil);
3885 CHARPOS (end_pos) = XFASTINT (end);
3886 if (STRINGP (object))
3887 compute_string_pos (&end_pos, start_pos, it->string);
3888 else
3889 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3890
3891 return end_pos;
3892 }
3893
3894
3895 /* Set up IT from a single `display' specification PROP. OBJECT
3896 is the object in which the `display' property was found. *POSITION
3897 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3898 means that we previously saw a display specification which already
3899 replaced text display with something else, for example an image;
3900 we ignore such properties after the first one has been processed.
3901
3902 If PROP is a `space' or `image' specification, and in some other
3903 cases too, set *POSITION to the position where the `display'
3904 property ends.
3905
3906 Value is non-zero if something was found which replaces the display
3907 of buffer or string text. Specifically, the value is -1 if that
3908 "something" is "nothing". */
3909
3910 static int
3911 handle_single_display_spec (it, spec, object, position,
3912 display_replaced_before_p)
3913 struct it *it;
3914 Lisp_Object spec;
3915 Lisp_Object object;
3916 struct text_pos *position;
3917 int display_replaced_before_p;
3918 {
3919 Lisp_Object form;
3920 Lisp_Object location, value;
3921 struct text_pos start_pos, save_pos;
3922 int valid_p;
3923
3924 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3925 If the result is non-nil, use VALUE instead of SPEC. */
3926 form = Qt;
3927 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3928 {
3929 spec = XCDR (spec);
3930 if (!CONSP (spec))
3931 return 0;
3932 form = XCAR (spec);
3933 spec = XCDR (spec);
3934 }
3935
3936 if (!NILP (form) && !EQ (form, Qt))
3937 {
3938 int count = SPECPDL_INDEX ();
3939 struct gcpro gcpro1;
3940
3941 /* Bind `object' to the object having the `display' property, a
3942 buffer or string. Bind `position' to the position in the
3943 object where the property was found, and `buffer-position'
3944 to the current position in the buffer. */
3945 specbind (Qobject, object);
3946 specbind (Qposition, make_number (CHARPOS (*position)));
3947 specbind (Qbuffer_position,
3948 make_number (STRINGP (object)
3949 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3950 GCPRO1 (form);
3951 form = safe_eval (form);
3952 UNGCPRO;
3953 unbind_to (count, Qnil);
3954 }
3955
3956 if (NILP (form))
3957 return 0;
3958
3959 /* Handle `(height HEIGHT)' specifications. */
3960 if (CONSP (spec)
3961 && EQ (XCAR (spec), Qheight)
3962 && CONSP (XCDR (spec)))
3963 {
3964 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3965 return 0;
3966
3967 it->font_height = XCAR (XCDR (spec));
3968 if (!NILP (it->font_height))
3969 {
3970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3971 int new_height = -1;
3972
3973 if (CONSP (it->font_height)
3974 && (EQ (XCAR (it->font_height), Qplus)
3975 || EQ (XCAR (it->font_height), Qminus))
3976 && CONSP (XCDR (it->font_height))
3977 && INTEGERP (XCAR (XCDR (it->font_height))))
3978 {
3979 /* `(+ N)' or `(- N)' where N is an integer. */
3980 int steps = XINT (XCAR (XCDR (it->font_height)));
3981 if (EQ (XCAR (it->font_height), Qplus))
3982 steps = - steps;
3983 it->face_id = smaller_face (it->f, it->face_id, steps);
3984 }
3985 else if (FUNCTIONP (it->font_height))
3986 {
3987 /* Call function with current height as argument.
3988 Value is the new height. */
3989 Lisp_Object height;
3990 height = safe_call1 (it->font_height,
3991 face->lface[LFACE_HEIGHT_INDEX]);
3992 if (NUMBERP (height))
3993 new_height = XFLOATINT (height);
3994 }
3995 else if (NUMBERP (it->font_height))
3996 {
3997 /* Value is a multiple of the canonical char height. */
3998 struct face *face;
3999
4000 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4001 new_height = (XFLOATINT (it->font_height)
4002 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4003 }
4004 else
4005 {
4006 /* Evaluate IT->font_height with `height' bound to the
4007 current specified height to get the new height. */
4008 int count = SPECPDL_INDEX ();
4009
4010 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4011 value = safe_eval (it->font_height);
4012 unbind_to (count, Qnil);
4013
4014 if (NUMBERP (value))
4015 new_height = XFLOATINT (value);
4016 }
4017
4018 if (new_height > 0)
4019 it->face_id = face_with_height (it->f, it->face_id, new_height);
4020 }
4021
4022 return 0;
4023 }
4024
4025 /* Handle `(space_width WIDTH)'. */
4026 if (CONSP (spec)
4027 && EQ (XCAR (spec), Qspace_width)
4028 && CONSP (XCDR (spec)))
4029 {
4030 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4031 return 0;
4032
4033 value = XCAR (XCDR (spec));
4034 if (NUMBERP (value) && XFLOATINT (value) > 0)
4035 it->space_width = value;
4036
4037 return 0;
4038 }
4039
4040 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4041 if (CONSP (spec)
4042 && EQ (XCAR (spec), Qslice))
4043 {
4044 Lisp_Object tem;
4045
4046 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4047 return 0;
4048
4049 if (tem = XCDR (spec), CONSP (tem))
4050 {
4051 it->slice.x = XCAR (tem);
4052 if (tem = XCDR (tem), CONSP (tem))
4053 {
4054 it->slice.y = XCAR (tem);
4055 if (tem = XCDR (tem), CONSP (tem))
4056 {
4057 it->slice.width = XCAR (tem);
4058 if (tem = XCDR (tem), CONSP (tem))
4059 it->slice.height = XCAR (tem);
4060 }
4061 }
4062 }
4063
4064 return 0;
4065 }
4066
4067 /* Handle `(raise FACTOR)'. */
4068 if (CONSP (spec)
4069 && EQ (XCAR (spec), Qraise)
4070 && CONSP (XCDR (spec)))
4071 {
4072 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4073 return 0;
4074
4075 #ifdef HAVE_WINDOW_SYSTEM
4076 value = XCAR (XCDR (spec));
4077 if (NUMBERP (value))
4078 {
4079 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4080 it->voffset = - (XFLOATINT (value)
4081 * (FONT_HEIGHT (face->font)));
4082 }
4083 #endif /* HAVE_WINDOW_SYSTEM */
4084
4085 return 0;
4086 }
4087
4088 /* Don't handle the other kinds of display specifications
4089 inside a string that we got from a `display' property. */
4090 if (it->string_from_display_prop_p)
4091 return 0;
4092
4093 /* Characters having this form of property are not displayed, so
4094 we have to find the end of the property. */
4095 start_pos = *position;
4096 *position = display_prop_end (it, object, start_pos);
4097 value = Qnil;
4098
4099 /* Stop the scan at that end position--we assume that all
4100 text properties change there. */
4101 it->stop_charpos = position->charpos;
4102
4103 /* Handle `(left-fringe BITMAP [FACE])'
4104 and `(right-fringe BITMAP [FACE])'. */
4105 if (CONSP (spec)
4106 && (EQ (XCAR (spec), Qleft_fringe)
4107 || EQ (XCAR (spec), Qright_fringe))
4108 && CONSP (XCDR (spec)))
4109 {
4110 int face_id = DEFAULT_FACE_ID;
4111 int fringe_bitmap;
4112
4113 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4114 /* If we return here, POSITION has been advanced
4115 across the text with this property. */
4116 return 0;
4117
4118 #ifdef HAVE_WINDOW_SYSTEM
4119 value = XCAR (XCDR (spec));
4120 if (!SYMBOLP (value)
4121 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4122 /* If we return here, POSITION has been advanced
4123 across the text with this property. */
4124 return 0;
4125
4126 if (CONSP (XCDR (XCDR (spec))))
4127 {
4128 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4129 int face_id2 = lookup_derived_face (it->f, face_name,
4130 FRINGE_FACE_ID, 0);
4131 if (face_id2 >= 0)
4132 face_id = face_id2;
4133 }
4134
4135 /* Save current settings of IT so that we can restore them
4136 when we are finished with the glyph property value. */
4137
4138 save_pos = it->position;
4139 it->position = *position;
4140 push_it (it);
4141 it->position = save_pos;
4142
4143 it->area = TEXT_AREA;
4144 it->what = IT_IMAGE;
4145 it->image_id = -1; /* no image */
4146 it->position = start_pos;
4147 it->object = NILP (object) ? it->w->buffer : object;
4148 it->method = GET_FROM_IMAGE;
4149 it->face_id = face_id;
4150
4151 /* Say that we haven't consumed the characters with
4152 `display' property yet. The call to pop_it in
4153 set_iterator_to_next will clean this up. */
4154 *position = start_pos;
4155
4156 if (EQ (XCAR (spec), Qleft_fringe))
4157 {
4158 it->left_user_fringe_bitmap = fringe_bitmap;
4159 it->left_user_fringe_face_id = face_id;
4160 }
4161 else
4162 {
4163 it->right_user_fringe_bitmap = fringe_bitmap;
4164 it->right_user_fringe_face_id = face_id;
4165 }
4166 #endif /* HAVE_WINDOW_SYSTEM */
4167 return 1;
4168 }
4169
4170 /* Prepare to handle `((margin left-margin) ...)',
4171 `((margin right-margin) ...)' and `((margin nil) ...)'
4172 prefixes for display specifications. */
4173 location = Qunbound;
4174 if (CONSP (spec) && CONSP (XCAR (spec)))
4175 {
4176 Lisp_Object tem;
4177
4178 value = XCDR (spec);
4179 if (CONSP (value))
4180 value = XCAR (value);
4181
4182 tem = XCAR (spec);
4183 if (EQ (XCAR (tem), Qmargin)
4184 && (tem = XCDR (tem),
4185 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4186 (NILP (tem)
4187 || EQ (tem, Qleft_margin)
4188 || EQ (tem, Qright_margin))))
4189 location = tem;
4190 }
4191
4192 if (EQ (location, Qunbound))
4193 {
4194 location = Qnil;
4195 value = spec;
4196 }
4197
4198 /* After this point, VALUE is the property after any
4199 margin prefix has been stripped. It must be a string,
4200 an image specification, or `(space ...)'.
4201
4202 LOCATION specifies where to display: `left-margin',
4203 `right-margin' or nil. */
4204
4205 valid_p = (STRINGP (value)
4206 #ifdef HAVE_WINDOW_SYSTEM
4207 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4208 #endif /* not HAVE_WINDOW_SYSTEM */
4209 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4210
4211 if (valid_p && !display_replaced_before_p)
4212 {
4213 /* Save current settings of IT so that we can restore them
4214 when we are finished with the glyph property value. */
4215 save_pos = it->position;
4216 it->position = *position;
4217 push_it (it);
4218 it->position = save_pos;
4219
4220 if (NILP (location))
4221 it->area = TEXT_AREA;
4222 else if (EQ (location, Qleft_margin))
4223 it->area = LEFT_MARGIN_AREA;
4224 else
4225 it->area = RIGHT_MARGIN_AREA;
4226
4227 if (STRINGP (value))
4228 {
4229 if (SCHARS (value) == 0)
4230 {
4231 pop_it (it);
4232 return -1; /* Replaced by "", i.e. nothing. */
4233 }
4234 it->string = value;
4235 it->multibyte_p = STRING_MULTIBYTE (it->string);
4236 it->current.overlay_string_index = -1;
4237 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4238 it->end_charpos = it->string_nchars = SCHARS (it->string);
4239 it->method = GET_FROM_STRING;
4240 it->stop_charpos = 0;
4241 it->string_from_display_prop_p = 1;
4242 /* Say that we haven't consumed the characters with
4243 `display' property yet. The call to pop_it in
4244 set_iterator_to_next will clean this up. */
4245 *position = start_pos;
4246 }
4247 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4248 {
4249 it->method = GET_FROM_STRETCH;
4250 it->object = value;
4251 *position = it->position = start_pos;
4252 }
4253 #ifdef HAVE_WINDOW_SYSTEM
4254 else
4255 {
4256 it->what = IT_IMAGE;
4257 it->image_id = lookup_image (it->f, value);
4258 it->position = start_pos;
4259 it->object = NILP (object) ? it->w->buffer : object;
4260 it->method = GET_FROM_IMAGE;
4261
4262 /* Say that we haven't consumed the characters with
4263 `display' property yet. The call to pop_it in
4264 set_iterator_to_next will clean this up. */
4265 *position = start_pos;
4266 }
4267 #endif /* HAVE_WINDOW_SYSTEM */
4268
4269 return 1;
4270 }
4271
4272 /* Invalid property or property not supported. Restore
4273 POSITION to what it was before. */
4274 *position = start_pos;
4275 return 0;
4276 }
4277
4278
4279 /* Check if SPEC is a display specification value whose text should be
4280 treated as intangible. */
4281
4282 static int
4283 single_display_spec_intangible_p (prop)
4284 Lisp_Object prop;
4285 {
4286 /* Skip over `when FORM'. */
4287 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4288 {
4289 prop = XCDR (prop);
4290 if (!CONSP (prop))
4291 return 0;
4292 prop = XCDR (prop);
4293 }
4294
4295 if (STRINGP (prop))
4296 return 1;
4297
4298 if (!CONSP (prop))
4299 return 0;
4300
4301 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4302 we don't need to treat text as intangible. */
4303 if (EQ (XCAR (prop), Qmargin))
4304 {
4305 prop = XCDR (prop);
4306 if (!CONSP (prop))
4307 return 0;
4308
4309 prop = XCDR (prop);
4310 if (!CONSP (prop)
4311 || EQ (XCAR (prop), Qleft_margin)
4312 || EQ (XCAR (prop), Qright_margin))
4313 return 0;
4314 }
4315
4316 return (CONSP (prop)
4317 && (EQ (XCAR (prop), Qimage)
4318 || EQ (XCAR (prop), Qspace)));
4319 }
4320
4321
4322 /* Check if PROP is a display property value whose text should be
4323 treated as intangible. */
4324
4325 int
4326 display_prop_intangible_p (prop)
4327 Lisp_Object prop;
4328 {
4329 if (CONSP (prop)
4330 && CONSP (XCAR (prop))
4331 && !EQ (Qmargin, XCAR (XCAR (prop))))
4332 {
4333 /* A list of sub-properties. */
4334 while (CONSP (prop))
4335 {
4336 if (single_display_spec_intangible_p (XCAR (prop)))
4337 return 1;
4338 prop = XCDR (prop);
4339 }
4340 }
4341 else if (VECTORP (prop))
4342 {
4343 /* A vector of sub-properties. */
4344 int i;
4345 for (i = 0; i < ASIZE (prop); ++i)
4346 if (single_display_spec_intangible_p (AREF (prop, i)))
4347 return 1;
4348 }
4349 else
4350 return single_display_spec_intangible_p (prop);
4351
4352 return 0;
4353 }
4354
4355
4356 /* Return 1 if PROP is a display sub-property value containing STRING. */
4357
4358 static int
4359 single_display_spec_string_p (prop, string)
4360 Lisp_Object prop, string;
4361 {
4362 if (EQ (string, prop))
4363 return 1;
4364
4365 /* Skip over `when FORM'. */
4366 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4367 {
4368 prop = XCDR (prop);
4369 if (!CONSP (prop))
4370 return 0;
4371 prop = XCDR (prop);
4372 }
4373
4374 if (CONSP (prop))
4375 /* Skip over `margin LOCATION'. */
4376 if (EQ (XCAR (prop), Qmargin))
4377 {
4378 prop = XCDR (prop);
4379 if (!CONSP (prop))
4380 return 0;
4381
4382 prop = XCDR (prop);
4383 if (!CONSP (prop))
4384 return 0;
4385 }
4386
4387 return CONSP (prop) && EQ (XCAR (prop), string);
4388 }
4389
4390
4391 /* Return 1 if STRING appears in the `display' property PROP. */
4392
4393 static int
4394 display_prop_string_p (prop, string)
4395 Lisp_Object prop, string;
4396 {
4397 if (CONSP (prop)
4398 && CONSP (XCAR (prop))
4399 && !EQ (Qmargin, XCAR (XCAR (prop))))
4400 {
4401 /* A list of sub-properties. */
4402 while (CONSP (prop))
4403 {
4404 if (single_display_spec_string_p (XCAR (prop), string))
4405 return 1;
4406 prop = XCDR (prop);
4407 }
4408 }
4409 else if (VECTORP (prop))
4410 {
4411 /* A vector of sub-properties. */
4412 int i;
4413 for (i = 0; i < ASIZE (prop); ++i)
4414 if (single_display_spec_string_p (AREF (prop, i), string))
4415 return 1;
4416 }
4417 else
4418 return single_display_spec_string_p (prop, string);
4419
4420 return 0;
4421 }
4422
4423
4424 /* Determine from which buffer position in W's buffer STRING comes
4425 from. AROUND_CHARPOS is an approximate position where it could
4426 be from. Value is the buffer position or 0 if it couldn't be
4427 determined.
4428
4429 W's buffer must be current.
4430
4431 This function is necessary because we don't record buffer positions
4432 in glyphs generated from strings (to keep struct glyph small).
4433 This function may only use code that doesn't eval because it is
4434 called asynchronously from note_mouse_highlight. */
4435
4436 int
4437 string_buffer_position (w, string, around_charpos)
4438 struct window *w;
4439 Lisp_Object string;
4440 int around_charpos;
4441 {
4442 Lisp_Object limit, prop, pos;
4443 const int MAX_DISTANCE = 1000;
4444 int found = 0;
4445
4446 pos = make_number (around_charpos);
4447 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4448 while (!found && !EQ (pos, limit))
4449 {
4450 prop = Fget_char_property (pos, Qdisplay, Qnil);
4451 if (!NILP (prop) && display_prop_string_p (prop, string))
4452 found = 1;
4453 else
4454 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4455 }
4456
4457 if (!found)
4458 {
4459 pos = make_number (around_charpos);
4460 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4461 while (!found && !EQ (pos, limit))
4462 {
4463 prop = Fget_char_property (pos, Qdisplay, Qnil);
4464 if (!NILP (prop) && display_prop_string_p (prop, string))
4465 found = 1;
4466 else
4467 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4468 limit);
4469 }
4470 }
4471
4472 return found ? XINT (pos) : 0;
4473 }
4474
4475
4476 \f
4477 /***********************************************************************
4478 `composition' property
4479 ***********************************************************************/
4480
4481 static enum prop_handled
4482 handle_auto_composed_prop (it)
4483 struct it *it;
4484 {
4485 enum prop_handled handled = HANDLED_NORMALLY;
4486
4487 if (FUNCTIONP (Vauto_composition_function))
4488 {
4489 Lisp_Object val;
4490 EMACS_INT pos, this_pos;
4491
4492 if (STRINGP (it->string))
4493 pos = IT_STRING_CHARPOS (*it);
4494 else
4495 pos = IT_CHARPOS (*it);
4496 this_pos = pos;
4497
4498 val =Fget_char_property (make_number (pos), Qauto_composed, it->string);
4499 if (! NILP (val))
4500 {
4501 Lisp_Object limit = Qnil, next;
4502
4503 /* As Fnext_single_char_property_change is very slow, we
4504 limit the search to the current line. */
4505 if (STRINGP (it->string))
4506 limit = make_number (SCHARS (it->string));
4507 else
4508 limit = make_number (find_next_newline_no_quit (pos, 1));
4509
4510 next = (Fnext_single_property_change
4511 (make_number (pos), Qauto_composed, it->string, limit));
4512 if (XINT (next) < XINT (limit))
4513 {
4514 /* The current point is auto-composed, but there exist
4515 characters not yet composed beyond the auto-composed
4516 region. There's a possiblity that the last
4517 characters in the region may be newly composed. */
4518 int charpos = XINT (next) - 1, bytepos, c;
4519
4520 if (STRINGP (it->string))
4521 {
4522 bytepos = string_char_to_byte (it->string, charpos);
4523 c = SDATA (it->string)[bytepos];
4524 }
4525 else
4526 {
4527 bytepos = CHAR_TO_BYTE (charpos);
4528 c = FETCH_BYTE (bytepos);
4529 }
4530 if (c != '\n')
4531 /* If the last character is not newline, it may be
4532 composed with the following characters. */
4533 val = Qnil, pos = charpos + 1;
4534 }
4535 }
4536 if (NILP (val))
4537 {
4538 int count = SPECPDL_INDEX ();
4539 Lisp_Object args[4];
4540
4541 args[0] = Vauto_composition_function;
4542 specbind (Qauto_composition_function, Qnil);
4543 args[1] = make_number (pos);
4544 args[2] = it->string;
4545 #ifdef USE_FONT_BACKEND
4546 if (enable_font_backend)
4547 {
4548 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4549 int c;
4550
4551 if (STRINGP (it->string))
4552 {
4553 EMACS_INT pos_byte = IT_STRING_BYTEPOS (*it);
4554 const unsigned char *s = SDATA (it->string) + pos_byte;
4555
4556 if (STRING_MULTIBYTE (it->string))
4557 it->c = STRING_CHAR (s, 0);
4558 else
4559 it->c = *s;
4560 }
4561 else
4562 {
4563 EMACS_INT pos_byte = IT_BYTEPOS (*it);
4564
4565 it->c = FETCH_CHAR (pos_byte);
4566 }
4567 args[3] = font_at (it->c, this_pos, face, it->w, it->string);
4568 }
4569 else
4570 #endif /* USE_FONT_BACKEND */
4571 args[3] = Qnil;
4572 safe_call (4, args);
4573 unbind_to (count, Qnil);
4574
4575 if (this_pos == pos)
4576 {
4577 val = Fget_char_property (args[1], Qauto_composed, it->string);
4578 /* Return HANDLED_RECOMPUTE_PROPS only if function composed
4579 something. This avoids an endless loop if they failed to
4580 fontify the text for which reason ever. */
4581 if (! NILP (val))
4582 handled = HANDLED_RECOMPUTE_PROPS;
4583 }
4584 else
4585 handled = HANDLED_RECOMPUTE_PROPS;
4586 }
4587 }
4588
4589 return handled;
4590 }
4591
4592 /* Set up iterator IT from `composition' property at its current
4593 position. Called from handle_stop. */
4594
4595 static enum prop_handled
4596 handle_composition_prop (it)
4597 struct it *it;
4598 {
4599 Lisp_Object prop, string;
4600 EMACS_INT pos, pos_byte, start, end;
4601 enum prop_handled handled = HANDLED_NORMALLY;
4602
4603 if (STRINGP (it->string))
4604 {
4605 pos = IT_STRING_CHARPOS (*it);
4606 pos_byte = IT_STRING_BYTEPOS (*it);
4607 string = it->string;
4608 }
4609 else
4610 {
4611 pos = IT_CHARPOS (*it);
4612 pos_byte = IT_BYTEPOS (*it);
4613 string = Qnil;
4614 }
4615
4616 /* If there's a valid composition and point is not inside of the
4617 composition (in the case that the composition is from the current
4618 buffer), draw a glyph composed from the composition components. */
4619 if (find_composition (pos, -1, &start, &end, &prop, string)
4620 && COMPOSITION_VALID_P (start, end, prop)
4621 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4622 {
4623 int id;
4624
4625 if (start != pos)
4626 {
4627 if (STRINGP (it->string))
4628 pos_byte = string_char_to_byte (it->string, start);
4629 else
4630 pos_byte = CHAR_TO_BYTE (start);
4631 }
4632 id = get_composition_id (start, pos_byte, end - start, prop, string);
4633
4634 if (id >= 0)
4635 {
4636 struct composition *cmp = composition_table[id];
4637
4638 if (cmp->glyph_len == 0)
4639 {
4640 /* No glyph. */
4641 if (STRINGP (it->string))
4642 {
4643 IT_STRING_CHARPOS (*it) = end;
4644 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4645 end);
4646 }
4647 else
4648 {
4649 IT_CHARPOS (*it) = end;
4650 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4651 }
4652 return HANDLED_RECOMPUTE_PROPS;
4653 }
4654
4655 it->stop_charpos = end;
4656 push_it (it);
4657
4658 it->method = GET_FROM_COMPOSITION;
4659 it->cmp_id = id;
4660 it->cmp_len = COMPOSITION_LENGTH (prop);
4661 /* For a terminal, draw only the first (non-TAB) character
4662 of the components. */
4663 #ifdef USE_FONT_BACKEND
4664 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4665 {
4666 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4667 ->key_and_value,
4668 cmp->hash_index * 2);
4669
4670 it->c = XINT (LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, 0)));
4671 }
4672 else
4673 #endif /* USE_FONT_BACKEND */
4674 {
4675 int i;
4676
4677 for (i = 0; i < cmp->glyph_len; i++)
4678 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4679 != '\t')
4680 break;
4681 }
4682 if (it->c == '\t')
4683 it->c = ' ';
4684 it->len = (STRINGP (it->string)
4685 ? string_char_to_byte (it->string, end)
4686 : CHAR_TO_BYTE (end)) - pos_byte;
4687 handled = HANDLED_RETURN;
4688 }
4689 }
4690
4691 return handled;
4692 }
4693
4694
4695 \f
4696 /***********************************************************************
4697 Overlay strings
4698 ***********************************************************************/
4699
4700 /* The following structure is used to record overlay strings for
4701 later sorting in load_overlay_strings. */
4702
4703 struct overlay_entry
4704 {
4705 Lisp_Object overlay;
4706 Lisp_Object string;
4707 int priority;
4708 int after_string_p;
4709 };
4710
4711
4712 /* Set up iterator IT from overlay strings at its current position.
4713 Called from handle_stop. */
4714
4715 static enum prop_handled
4716 handle_overlay_change (it)
4717 struct it *it;
4718 {
4719 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4720 return HANDLED_RECOMPUTE_PROPS;
4721 else
4722 return HANDLED_NORMALLY;
4723 }
4724
4725
4726 /* Set up the next overlay string for delivery by IT, if there is an
4727 overlay string to deliver. Called by set_iterator_to_next when the
4728 end of the current overlay string is reached. If there are more
4729 overlay strings to display, IT->string and
4730 IT->current.overlay_string_index are set appropriately here.
4731 Otherwise IT->string is set to nil. */
4732
4733 static void
4734 next_overlay_string (it)
4735 struct it *it;
4736 {
4737 ++it->current.overlay_string_index;
4738 if (it->current.overlay_string_index == it->n_overlay_strings)
4739 {
4740 /* No more overlay strings. Restore IT's settings to what
4741 they were before overlay strings were processed, and
4742 continue to deliver from current_buffer. */
4743 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4744
4745 pop_it (it);
4746 xassert (it->sp > 0
4747 || it->method == GET_FROM_COMPOSITION
4748 || (NILP (it->string)
4749 && it->method == GET_FROM_BUFFER
4750 && it->stop_charpos >= BEGV
4751 && it->stop_charpos <= it->end_charpos));
4752 it->current.overlay_string_index = -1;
4753 it->n_overlay_strings = 0;
4754
4755 /* If we're at the end of the buffer, record that we have
4756 processed the overlay strings there already, so that
4757 next_element_from_buffer doesn't try it again. */
4758 if (IT_CHARPOS (*it) >= it->end_charpos)
4759 it->overlay_strings_at_end_processed_p = 1;
4760
4761 /* If we have to display `...' for invisible text, set
4762 the iterator up for that. */
4763 if (display_ellipsis_p)
4764 setup_for_ellipsis (it, 0);
4765 }
4766 else
4767 {
4768 /* There are more overlay strings to process. If
4769 IT->current.overlay_string_index has advanced to a position
4770 where we must load IT->overlay_strings with more strings, do
4771 it. */
4772 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4773
4774 if (it->current.overlay_string_index && i == 0)
4775 load_overlay_strings (it, 0);
4776
4777 /* Initialize IT to deliver display elements from the overlay
4778 string. */
4779 it->string = it->overlay_strings[i];
4780 it->multibyte_p = STRING_MULTIBYTE (it->string);
4781 SET_TEXT_POS (it->current.string_pos, 0, 0);
4782 it->method = GET_FROM_STRING;
4783 it->stop_charpos = 0;
4784 }
4785
4786 CHECK_IT (it);
4787 }
4788
4789
4790 /* Compare two overlay_entry structures E1 and E2. Used as a
4791 comparison function for qsort in load_overlay_strings. Overlay
4792 strings for the same position are sorted so that
4793
4794 1. All after-strings come in front of before-strings, except
4795 when they come from the same overlay.
4796
4797 2. Within after-strings, strings are sorted so that overlay strings
4798 from overlays with higher priorities come first.
4799
4800 2. Within before-strings, strings are sorted so that overlay
4801 strings from overlays with higher priorities come last.
4802
4803 Value is analogous to strcmp. */
4804
4805
4806 static int
4807 compare_overlay_entries (e1, e2)
4808 void *e1, *e2;
4809 {
4810 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4811 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4812 int result;
4813
4814 if (entry1->after_string_p != entry2->after_string_p)
4815 {
4816 /* Let after-strings appear in front of before-strings if
4817 they come from different overlays. */
4818 if (EQ (entry1->overlay, entry2->overlay))
4819 result = entry1->after_string_p ? 1 : -1;
4820 else
4821 result = entry1->after_string_p ? -1 : 1;
4822 }
4823 else if (entry1->after_string_p)
4824 /* After-strings sorted in order of decreasing priority. */
4825 result = entry2->priority - entry1->priority;
4826 else
4827 /* Before-strings sorted in order of increasing priority. */
4828 result = entry1->priority - entry2->priority;
4829
4830 return result;
4831 }
4832
4833
4834 /* Load the vector IT->overlay_strings with overlay strings from IT's
4835 current buffer position, or from CHARPOS if that is > 0. Set
4836 IT->n_overlays to the total number of overlay strings found.
4837
4838 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4839 a time. On entry into load_overlay_strings,
4840 IT->current.overlay_string_index gives the number of overlay
4841 strings that have already been loaded by previous calls to this
4842 function.
4843
4844 IT->add_overlay_start contains an additional overlay start
4845 position to consider for taking overlay strings from, if non-zero.
4846 This position comes into play when the overlay has an `invisible'
4847 property, and both before and after-strings. When we've skipped to
4848 the end of the overlay, because of its `invisible' property, we
4849 nevertheless want its before-string to appear.
4850 IT->add_overlay_start will contain the overlay start position
4851 in this case.
4852
4853 Overlay strings are sorted so that after-string strings come in
4854 front of before-string strings. Within before and after-strings,
4855 strings are sorted by overlay priority. See also function
4856 compare_overlay_entries. */
4857
4858 static void
4859 load_overlay_strings (it, charpos)
4860 struct it *it;
4861 int charpos;
4862 {
4863 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4864 Lisp_Object overlay, window, str, invisible;
4865 struct Lisp_Overlay *ov;
4866 int start, end;
4867 int size = 20;
4868 int n = 0, i, j, invis_p;
4869 struct overlay_entry *entries
4870 = (struct overlay_entry *) alloca (size * sizeof *entries);
4871
4872 if (charpos <= 0)
4873 charpos = IT_CHARPOS (*it);
4874
4875 /* Append the overlay string STRING of overlay OVERLAY to vector
4876 `entries' which has size `size' and currently contains `n'
4877 elements. AFTER_P non-zero means STRING is an after-string of
4878 OVERLAY. */
4879 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4880 do \
4881 { \
4882 Lisp_Object priority; \
4883 \
4884 if (n == size) \
4885 { \
4886 int new_size = 2 * size; \
4887 struct overlay_entry *old = entries; \
4888 entries = \
4889 (struct overlay_entry *) alloca (new_size \
4890 * sizeof *entries); \
4891 bcopy (old, entries, size * sizeof *entries); \
4892 size = new_size; \
4893 } \
4894 \
4895 entries[n].string = (STRING); \
4896 entries[n].overlay = (OVERLAY); \
4897 priority = Foverlay_get ((OVERLAY), Qpriority); \
4898 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4899 entries[n].after_string_p = (AFTER_P); \
4900 ++n; \
4901 } \
4902 while (0)
4903
4904 /* Process overlay before the overlay center. */
4905 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4906 {
4907 XSETMISC (overlay, ov);
4908 xassert (OVERLAYP (overlay));
4909 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4910 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4911
4912 if (end < charpos)
4913 break;
4914
4915 /* Skip this overlay if it doesn't start or end at IT's current
4916 position. */
4917 if (end != charpos && start != charpos)
4918 continue;
4919
4920 /* Skip this overlay if it doesn't apply to IT->w. */
4921 window = Foverlay_get (overlay, Qwindow);
4922 if (WINDOWP (window) && XWINDOW (window) != it->w)
4923 continue;
4924
4925 /* If the text ``under'' the overlay is invisible, both before-
4926 and after-strings from this overlay are visible; start and
4927 end position are indistinguishable. */
4928 invisible = Foverlay_get (overlay, Qinvisible);
4929 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4930
4931 /* If overlay has a non-empty before-string, record it. */
4932 if ((start == charpos || (end == charpos && invis_p))
4933 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4934 && SCHARS (str))
4935 RECORD_OVERLAY_STRING (overlay, str, 0);
4936
4937 /* If overlay has a non-empty after-string, record it. */
4938 if ((end == charpos || (start == charpos && invis_p))
4939 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4940 && SCHARS (str))
4941 RECORD_OVERLAY_STRING (overlay, str, 1);
4942 }
4943
4944 /* Process overlays after the overlay center. */
4945 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4946 {
4947 XSETMISC (overlay, ov);
4948 xassert (OVERLAYP (overlay));
4949 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4950 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4951
4952 if (start > charpos)
4953 break;
4954
4955 /* Skip this overlay if it doesn't start or end at IT's current
4956 position. */
4957 if (end != charpos && start != charpos)
4958 continue;
4959
4960 /* Skip this overlay if it doesn't apply to IT->w. */
4961 window = Foverlay_get (overlay, Qwindow);
4962 if (WINDOWP (window) && XWINDOW (window) != it->w)
4963 continue;
4964
4965 /* If the text ``under'' the overlay is invisible, it has a zero
4966 dimension, and both before- and after-strings apply. */
4967 invisible = Foverlay_get (overlay, Qinvisible);
4968 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4969
4970 /* If overlay has a non-empty before-string, record it. */
4971 if ((start == charpos || (end == charpos && invis_p))
4972 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4973 && SCHARS (str))
4974 RECORD_OVERLAY_STRING (overlay, str, 0);
4975
4976 /* If overlay has a non-empty after-string, record it. */
4977 if ((end == charpos || (start == charpos && invis_p))
4978 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4979 && SCHARS (str))
4980 RECORD_OVERLAY_STRING (overlay, str, 1);
4981 }
4982
4983 #undef RECORD_OVERLAY_STRING
4984
4985 /* Sort entries. */
4986 if (n > 1)
4987 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4988
4989 /* Record the total number of strings to process. */
4990 it->n_overlay_strings = n;
4991
4992 /* IT->current.overlay_string_index is the number of overlay strings
4993 that have already been consumed by IT. Copy some of the
4994 remaining overlay strings to IT->overlay_strings. */
4995 i = 0;
4996 j = it->current.overlay_string_index;
4997 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4998 it->overlay_strings[i++] = entries[j++].string;
4999
5000 CHECK_IT (it);
5001 }
5002
5003
5004 /* Get the first chunk of overlay strings at IT's current buffer
5005 position, or at CHARPOS if that is > 0. Value is non-zero if at
5006 least one overlay string was found. */
5007
5008 static int
5009 get_overlay_strings_1 (it, charpos, compute_stop_p)
5010 struct it *it;
5011 int charpos;
5012 {
5013 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5014 process. This fills IT->overlay_strings with strings, and sets
5015 IT->n_overlay_strings to the total number of strings to process.
5016 IT->pos.overlay_string_index has to be set temporarily to zero
5017 because load_overlay_strings needs this; it must be set to -1
5018 when no overlay strings are found because a zero value would
5019 indicate a position in the first overlay string. */
5020 it->current.overlay_string_index = 0;
5021 load_overlay_strings (it, charpos);
5022
5023 /* If we found overlay strings, set up IT to deliver display
5024 elements from the first one. Otherwise set up IT to deliver
5025 from current_buffer. */
5026 if (it->n_overlay_strings)
5027 {
5028 /* Make sure we know settings in current_buffer, so that we can
5029 restore meaningful values when we're done with the overlay
5030 strings. */
5031 if (compute_stop_p)
5032 compute_stop_pos (it);
5033 xassert (it->face_id >= 0);
5034
5035 /* Save IT's settings. They are restored after all overlay
5036 strings have been processed. */
5037 xassert (!compute_stop_p || it->sp == 0);
5038 push_it (it);
5039
5040 /* Set up IT to deliver display elements from the first overlay
5041 string. */
5042 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5043 it->string = it->overlay_strings[0];
5044 it->stop_charpos = 0;
5045 xassert (STRINGP (it->string));
5046 it->end_charpos = SCHARS (it->string);
5047 it->multibyte_p = STRING_MULTIBYTE (it->string);
5048 it->method = GET_FROM_STRING;
5049 return 1;
5050 }
5051
5052 it->current.overlay_string_index = -1;
5053 return 0;
5054 }
5055
5056 static int
5057 get_overlay_strings (it, charpos)
5058 struct it *it;
5059 int charpos;
5060 {
5061 it->string = Qnil;
5062 it->method = GET_FROM_BUFFER;
5063
5064 (void) get_overlay_strings_1 (it, charpos, 1);
5065
5066 CHECK_IT (it);
5067
5068 /* Value is non-zero if we found at least one overlay string. */
5069 return STRINGP (it->string);
5070 }
5071
5072
5073 \f
5074 /***********************************************************************
5075 Saving and restoring state
5076 ***********************************************************************/
5077
5078 /* Save current settings of IT on IT->stack. Called, for example,
5079 before setting up IT for an overlay string, to be able to restore
5080 IT's settings to what they were after the overlay string has been
5081 processed. */
5082
5083 static void
5084 push_it (it)
5085 struct it *it;
5086 {
5087 struct iterator_stack_entry *p;
5088
5089 xassert (it->sp < IT_STACK_SIZE);
5090 p = it->stack + it->sp;
5091
5092 p->stop_charpos = it->stop_charpos;
5093 xassert (it->face_id >= 0);
5094 p->face_id = it->face_id;
5095 p->string = it->string;
5096 p->method = it->method;
5097 switch (p->method)
5098 {
5099 case GET_FROM_IMAGE:
5100 p->u.image.object = it->object;
5101 p->u.image.image_id = it->image_id;
5102 p->u.image.slice = it->slice;
5103 break;
5104 case GET_FROM_COMPOSITION:
5105 p->u.comp.object = it->object;
5106 p->u.comp.c = it->c;
5107 p->u.comp.len = it->len;
5108 p->u.comp.cmp_id = it->cmp_id;
5109 p->u.comp.cmp_len = it->cmp_len;
5110 break;
5111 case GET_FROM_STRETCH:
5112 p->u.stretch.object = it->object;
5113 break;
5114 }
5115 p->position = it->position;
5116 p->current = it->current;
5117 p->end_charpos = it->end_charpos;
5118 p->string_nchars = it->string_nchars;
5119 p->area = it->area;
5120 p->multibyte_p = it->multibyte_p;
5121 p->space_width = it->space_width;
5122 p->font_height = it->font_height;
5123 p->voffset = it->voffset;
5124 p->string_from_display_prop_p = it->string_from_display_prop_p;
5125 p->display_ellipsis_p = 0;
5126 ++it->sp;
5127 }
5128
5129
5130 /* Restore IT's settings from IT->stack. Called, for example, when no
5131 more overlay strings must be processed, and we return to delivering
5132 display elements from a buffer, or when the end of a string from a
5133 `display' property is reached and we return to delivering display
5134 elements from an overlay string, or from a buffer. */
5135
5136 static void
5137 pop_it (it)
5138 struct it *it;
5139 {
5140 struct iterator_stack_entry *p;
5141
5142 xassert (it->sp > 0);
5143 --it->sp;
5144 p = it->stack + it->sp;
5145 it->stop_charpos = p->stop_charpos;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 if (NILP (it->string))
5151 SET_TEXT_POS (it->current.string_pos, -1, -1);
5152 it->method = p->method;
5153 switch (it->method)
5154 {
5155 case GET_FROM_IMAGE:
5156 it->image_id = p->u.image.image_id;
5157 it->object = p->u.image.object;
5158 it->slice = p->u.image.slice;
5159 break;
5160 case GET_FROM_COMPOSITION:
5161 it->object = p->u.comp.object;
5162 it->c = p->u.comp.c;
5163 it->len = p->u.comp.len;
5164 it->cmp_id = p->u.comp.cmp_id;
5165 it->cmp_len = p->u.comp.cmp_len;
5166 break;
5167 case GET_FROM_STRETCH:
5168 it->object = p->u.comp.object;
5169 break;
5170 case GET_FROM_BUFFER:
5171 it->object = it->w->buffer;
5172 break;
5173 case GET_FROM_STRING:
5174 it->object = it->string;
5175 break;
5176 }
5177 it->end_charpos = p->end_charpos;
5178 it->string_nchars = p->string_nchars;
5179 it->area = p->area;
5180 it->multibyte_p = p->multibyte_p;
5181 it->space_width = p->space_width;
5182 it->font_height = p->font_height;
5183 it->voffset = p->voffset;
5184 it->string_from_display_prop_p = p->string_from_display_prop_p;
5185 }
5186
5187
5188 \f
5189 /***********************************************************************
5190 Moving over lines
5191 ***********************************************************************/
5192
5193 /* Set IT's current position to the previous line start. */
5194
5195 static void
5196 back_to_previous_line_start (it)
5197 struct it *it;
5198 {
5199 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5200 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5201 }
5202
5203
5204 /* Move IT to the next line start.
5205
5206 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5207 we skipped over part of the text (as opposed to moving the iterator
5208 continuously over the text). Otherwise, don't change the value
5209 of *SKIPPED_P.
5210
5211 Newlines may come from buffer text, overlay strings, or strings
5212 displayed via the `display' property. That's the reason we can't
5213 simply use find_next_newline_no_quit.
5214
5215 Note that this function may not skip over invisible text that is so
5216 because of text properties and immediately follows a newline. If
5217 it would, function reseat_at_next_visible_line_start, when called
5218 from set_iterator_to_next, would effectively make invisible
5219 characters following a newline part of the wrong glyph row, which
5220 leads to wrong cursor motion. */
5221
5222 static int
5223 forward_to_next_line_start (it, skipped_p)
5224 struct it *it;
5225 int *skipped_p;
5226 {
5227 int old_selective, newline_found_p, n;
5228 const int MAX_NEWLINE_DISTANCE = 500;
5229
5230 /* If already on a newline, just consume it to avoid unintended
5231 skipping over invisible text below. */
5232 if (it->what == IT_CHARACTER
5233 && it->c == '\n'
5234 && CHARPOS (it->position) == IT_CHARPOS (*it))
5235 {
5236 set_iterator_to_next (it, 0);
5237 it->c = 0;
5238 return 1;
5239 }
5240
5241 /* Don't handle selective display in the following. It's (a)
5242 unnecessary because it's done by the caller, and (b) leads to an
5243 infinite recursion because next_element_from_ellipsis indirectly
5244 calls this function. */
5245 old_selective = it->selective;
5246 it->selective = 0;
5247
5248 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5249 from buffer text. */
5250 for (n = newline_found_p = 0;
5251 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5252 n += STRINGP (it->string) ? 0 : 1)
5253 {
5254 if (!get_next_display_element (it))
5255 return 0;
5256 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5257 set_iterator_to_next (it, 0);
5258 }
5259
5260 /* If we didn't find a newline near enough, see if we can use a
5261 short-cut. */
5262 if (!newline_found_p)
5263 {
5264 int start = IT_CHARPOS (*it);
5265 int limit = find_next_newline_no_quit (start, 1);
5266 Lisp_Object pos;
5267
5268 xassert (!STRINGP (it->string));
5269
5270 /* If there isn't any `display' property in sight, and no
5271 overlays, we can just use the position of the newline in
5272 buffer text. */
5273 if (it->stop_charpos >= limit
5274 || ((pos = Fnext_single_property_change (make_number (start),
5275 Qdisplay,
5276 Qnil, make_number (limit)),
5277 NILP (pos))
5278 && next_overlay_change (start) == ZV))
5279 {
5280 IT_CHARPOS (*it) = limit;
5281 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5282 *skipped_p = newline_found_p = 1;
5283 }
5284 else
5285 {
5286 while (get_next_display_element (it)
5287 && !newline_found_p)
5288 {
5289 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5290 set_iterator_to_next (it, 0);
5291 }
5292 }
5293 }
5294
5295 it->selective = old_selective;
5296 return newline_found_p;
5297 }
5298
5299
5300 /* Set IT's current position to the previous visible line start. Skip
5301 invisible text that is so either due to text properties or due to
5302 selective display. Caution: this does not change IT->current_x and
5303 IT->hpos. */
5304
5305 static void
5306 back_to_previous_visible_line_start (it)
5307 struct it *it;
5308 {
5309 while (IT_CHARPOS (*it) > BEGV)
5310 {
5311 back_to_previous_line_start (it);
5312
5313 if (IT_CHARPOS (*it) <= BEGV)
5314 break;
5315
5316 /* If selective > 0, then lines indented more than that values
5317 are invisible. */
5318 if (it->selective > 0
5319 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5320 (double) it->selective)) /* iftc */
5321 continue;
5322
5323 /* Check the newline before point for invisibility. */
5324 {
5325 Lisp_Object prop;
5326 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5327 Qinvisible, it->window);
5328 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5329 continue;
5330 }
5331
5332 if (IT_CHARPOS (*it) <= BEGV)
5333 break;
5334
5335 {
5336 struct it it2;
5337 int pos;
5338 int beg, end;
5339 Lisp_Object val, overlay;
5340
5341 /* If newline is part of a composition, continue from start of composition */
5342 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5343 && beg < IT_CHARPOS (*it))
5344 goto replaced;
5345
5346 /* If newline is replaced by a display property, find start of overlay
5347 or interval and continue search from that point. */
5348 it2 = *it;
5349 pos = --IT_CHARPOS (it2);
5350 --IT_BYTEPOS (it2);
5351 it2.sp = 0;
5352 if (handle_display_prop (&it2) == HANDLED_RETURN
5353 && !NILP (val = get_char_property_and_overlay
5354 (make_number (pos), Qdisplay, Qnil, &overlay))
5355 && (OVERLAYP (overlay)
5356 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5357 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5358 goto replaced;
5359
5360 /* Newline is not replaced by anything -- so we are done. */
5361 break;
5362
5363 replaced:
5364 if (beg < BEGV)
5365 beg = BEGV;
5366 IT_CHARPOS (*it) = beg;
5367 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5368 }
5369 }
5370
5371 it->continuation_lines_width = 0;
5372
5373 xassert (IT_CHARPOS (*it) >= BEGV);
5374 xassert (IT_CHARPOS (*it) == BEGV
5375 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5376 CHECK_IT (it);
5377 }
5378
5379
5380 /* Reseat iterator IT at the previous visible line start. Skip
5381 invisible text that is so either due to text properties or due to
5382 selective display. At the end, update IT's overlay information,
5383 face information etc. */
5384
5385 void
5386 reseat_at_previous_visible_line_start (it)
5387 struct it *it;
5388 {
5389 back_to_previous_visible_line_start (it);
5390 reseat (it, it->current.pos, 1);
5391 CHECK_IT (it);
5392 }
5393
5394
5395 /* Reseat iterator IT on the next visible line start in the current
5396 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5397 preceding the line start. Skip over invisible text that is so
5398 because of selective display. Compute faces, overlays etc at the
5399 new position. Note that this function does not skip over text that
5400 is invisible because of text properties. */
5401
5402 static void
5403 reseat_at_next_visible_line_start (it, on_newline_p)
5404 struct it *it;
5405 int on_newline_p;
5406 {
5407 int newline_found_p, skipped_p = 0;
5408
5409 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5410
5411 /* Skip over lines that are invisible because they are indented
5412 more than the value of IT->selective. */
5413 if (it->selective > 0)
5414 while (IT_CHARPOS (*it) < ZV
5415 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5416 (double) it->selective)) /* iftc */
5417 {
5418 xassert (IT_BYTEPOS (*it) == BEGV
5419 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5420 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5421 }
5422
5423 /* Position on the newline if that's what's requested. */
5424 if (on_newline_p && newline_found_p)
5425 {
5426 if (STRINGP (it->string))
5427 {
5428 if (IT_STRING_CHARPOS (*it) > 0)
5429 {
5430 --IT_STRING_CHARPOS (*it);
5431 --IT_STRING_BYTEPOS (*it);
5432 }
5433 }
5434 else if (IT_CHARPOS (*it) > BEGV)
5435 {
5436 --IT_CHARPOS (*it);
5437 --IT_BYTEPOS (*it);
5438 reseat (it, it->current.pos, 0);
5439 }
5440 }
5441 else if (skipped_p)
5442 reseat (it, it->current.pos, 0);
5443
5444 CHECK_IT (it);
5445 }
5446
5447
5448 \f
5449 /***********************************************************************
5450 Changing an iterator's position
5451 ***********************************************************************/
5452
5453 /* Change IT's current position to POS in current_buffer. If FORCE_P
5454 is non-zero, always check for text properties at the new position.
5455 Otherwise, text properties are only looked up if POS >=
5456 IT->check_charpos of a property. */
5457
5458 static void
5459 reseat (it, pos, force_p)
5460 struct it *it;
5461 struct text_pos pos;
5462 int force_p;
5463 {
5464 int original_pos = IT_CHARPOS (*it);
5465
5466 reseat_1 (it, pos, 0);
5467
5468 /* Determine where to check text properties. Avoid doing it
5469 where possible because text property lookup is very expensive. */
5470 if (force_p
5471 || CHARPOS (pos) > it->stop_charpos
5472 || CHARPOS (pos) < original_pos)
5473 handle_stop (it);
5474
5475 CHECK_IT (it);
5476 }
5477
5478
5479 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5480 IT->stop_pos to POS, also. */
5481
5482 static void
5483 reseat_1 (it, pos, set_stop_p)
5484 struct it *it;
5485 struct text_pos pos;
5486 int set_stop_p;
5487 {
5488 /* Don't call this function when scanning a C string. */
5489 xassert (it->s == NULL);
5490
5491 /* POS must be a reasonable value. */
5492 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5493
5494 it->current.pos = it->position = pos;
5495 it->end_charpos = ZV;
5496 it->dpvec = NULL;
5497 it->current.dpvec_index = -1;
5498 it->current.overlay_string_index = -1;
5499 IT_STRING_CHARPOS (*it) = -1;
5500 IT_STRING_BYTEPOS (*it) = -1;
5501 it->string = Qnil;
5502 it->method = GET_FROM_BUFFER;
5503 it->object = it->w->buffer;
5504 it->area = TEXT_AREA;
5505 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5506 it->sp = 0;
5507 it->string_from_display_prop_p = 0;
5508 it->face_before_selective_p = 0;
5509
5510 if (set_stop_p)
5511 it->stop_charpos = CHARPOS (pos);
5512 }
5513
5514
5515 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5516 If S is non-null, it is a C string to iterate over. Otherwise,
5517 STRING gives a Lisp string to iterate over.
5518
5519 If PRECISION > 0, don't return more then PRECISION number of
5520 characters from the string.
5521
5522 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5523 characters have been returned. FIELD_WIDTH < 0 means an infinite
5524 field width.
5525
5526 MULTIBYTE = 0 means disable processing of multibyte characters,
5527 MULTIBYTE > 0 means enable it,
5528 MULTIBYTE < 0 means use IT->multibyte_p.
5529
5530 IT must be initialized via a prior call to init_iterator before
5531 calling this function. */
5532
5533 static void
5534 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5535 struct it *it;
5536 unsigned char *s;
5537 Lisp_Object string;
5538 int charpos;
5539 int precision, field_width, multibyte;
5540 {
5541 /* No region in strings. */
5542 it->region_beg_charpos = it->region_end_charpos = -1;
5543
5544 /* No text property checks performed by default, but see below. */
5545 it->stop_charpos = -1;
5546
5547 /* Set iterator position and end position. */
5548 bzero (&it->current, sizeof it->current);
5549 it->current.overlay_string_index = -1;
5550 it->current.dpvec_index = -1;
5551 xassert (charpos >= 0);
5552
5553 /* If STRING is specified, use its multibyteness, otherwise use the
5554 setting of MULTIBYTE, if specified. */
5555 if (multibyte >= 0)
5556 it->multibyte_p = multibyte > 0;
5557
5558 if (s == NULL)
5559 {
5560 xassert (STRINGP (string));
5561 it->string = string;
5562 it->s = NULL;
5563 it->end_charpos = it->string_nchars = SCHARS (string);
5564 it->method = GET_FROM_STRING;
5565 it->current.string_pos = string_pos (charpos, string);
5566 }
5567 else
5568 {
5569 it->s = s;
5570 it->string = Qnil;
5571
5572 /* Note that we use IT->current.pos, not it->current.string_pos,
5573 for displaying C strings. */
5574 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5575 if (it->multibyte_p)
5576 {
5577 it->current.pos = c_string_pos (charpos, s, 1);
5578 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5579 }
5580 else
5581 {
5582 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5583 it->end_charpos = it->string_nchars = strlen (s);
5584 }
5585
5586 it->method = GET_FROM_C_STRING;
5587 }
5588
5589 /* PRECISION > 0 means don't return more than PRECISION characters
5590 from the string. */
5591 if (precision > 0 && it->end_charpos - charpos > precision)
5592 it->end_charpos = it->string_nchars = charpos + precision;
5593
5594 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5595 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5596 FIELD_WIDTH < 0 means infinite field width. This is useful for
5597 padding with `-' at the end of a mode line. */
5598 if (field_width < 0)
5599 field_width = INFINITY;
5600 if (field_width > it->end_charpos - charpos)
5601 it->end_charpos = charpos + field_width;
5602
5603 /* Use the standard display table for displaying strings. */
5604 if (DISP_TABLE_P (Vstandard_display_table))
5605 it->dp = XCHAR_TABLE (Vstandard_display_table);
5606
5607 it->stop_charpos = charpos;
5608 CHECK_IT (it);
5609 }
5610
5611
5612 \f
5613 /***********************************************************************
5614 Iteration
5615 ***********************************************************************/
5616
5617 /* Map enum it_method value to corresponding next_element_from_* function. */
5618
5619 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5620 {
5621 next_element_from_buffer,
5622 next_element_from_display_vector,
5623 next_element_from_composition,
5624 next_element_from_string,
5625 next_element_from_c_string,
5626 next_element_from_image,
5627 next_element_from_stretch
5628 };
5629
5630
5631 /* Load IT's display element fields with information about the next
5632 display element from the current position of IT. Value is zero if
5633 end of buffer (or C string) is reached. */
5634
5635 static struct frame *last_escape_glyph_frame = NULL;
5636 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5637 static int last_escape_glyph_merged_face_id = 0;
5638
5639 int
5640 get_next_display_element (it)
5641 struct it *it;
5642 {
5643 /* Non-zero means that we found a display element. Zero means that
5644 we hit the end of what we iterate over. Performance note: the
5645 function pointer `method' used here turns out to be faster than
5646 using a sequence of if-statements. */
5647 int success_p;
5648
5649 get_next:
5650 success_p = (*get_next_element[it->method]) (it);
5651
5652 if (it->what == IT_CHARACTER)
5653 {
5654 /* Map via display table or translate control characters.
5655 IT->c, IT->len etc. have been set to the next character by
5656 the function call above. If we have a display table, and it
5657 contains an entry for IT->c, translate it. Don't do this if
5658 IT->c itself comes from a display table, otherwise we could
5659 end up in an infinite recursion. (An alternative could be to
5660 count the recursion depth of this function and signal an
5661 error when a certain maximum depth is reached.) Is it worth
5662 it? */
5663 if (success_p && it->dpvec == NULL)
5664 {
5665 Lisp_Object dv;
5666
5667 if (it->dp
5668 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5669 VECTORP (dv)))
5670 {
5671 struct Lisp_Vector *v = XVECTOR (dv);
5672
5673 /* Return the first character from the display table
5674 entry, if not empty. If empty, don't display the
5675 current character. */
5676 if (v->size)
5677 {
5678 it->dpvec_char_len = it->len;
5679 it->dpvec = v->contents;
5680 it->dpend = v->contents + v->size;
5681 it->current.dpvec_index = 0;
5682 it->dpvec_face_id = -1;
5683 it->saved_face_id = it->face_id;
5684 it->method = GET_FROM_DISPLAY_VECTOR;
5685 it->ellipsis_p = 0;
5686 }
5687 else
5688 {
5689 set_iterator_to_next (it, 0);
5690 }
5691 goto get_next;
5692 }
5693
5694 /* Translate control characters into `\003' or `^C' form.
5695 Control characters coming from a display table entry are
5696 currently not translated because we use IT->dpvec to hold
5697 the translation. This could easily be changed but I
5698 don't believe that it is worth doing.
5699
5700 If it->multibyte_p is nonzero, non-printable non-ASCII
5701 characters are also translated to octal form.
5702
5703 If it->multibyte_p is zero, eight-bit characters that
5704 don't have corresponding multibyte char code are also
5705 translated to octal form. */
5706 else if ((it->c < ' '
5707 ? (it->area != TEXT_AREA
5708 /* In mode line, treat \n, \t like other crl chars. */
5709 || (it->c != '\t'
5710 && it->glyph_row && it->glyph_row->mode_line_p)
5711 || (it->c != '\n' && it->c != '\t'))
5712 : (it->multibyte_p
5713 ? (!CHAR_PRINTABLE_P (it->c)
5714 || (!NILP (Vnobreak_char_display)
5715 && (it->c == 0xA0 /* NO-BREAK SPACE */
5716 || it->c == 0xAD /* SOFT HYPHEN */)))
5717 : (it->c >= 127
5718 && (! unibyte_display_via_language_environment
5719 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5720 {
5721 /* IT->c is a control character which must be displayed
5722 either as '\003' or as `^C' where the '\\' and '^'
5723 can be defined in the display table. Fill
5724 IT->ctl_chars with glyphs for what we have to
5725 display. Then, set IT->dpvec to these glyphs. */
5726 GLYPH g;
5727 int ctl_len;
5728 int face_id, lface_id = 0 ;
5729 GLYPH escape_glyph;
5730
5731 /* Handle control characters with ^. */
5732
5733 if (it->c < 128 && it->ctl_arrow_p)
5734 {
5735 g = '^'; /* default glyph for Control */
5736 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5737 if (it->dp
5738 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5739 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5740 {
5741 g = XINT (DISP_CTRL_GLYPH (it->dp));
5742 lface_id = FAST_GLYPH_FACE (g);
5743 }
5744 if (lface_id)
5745 {
5746 g = FAST_GLYPH_CHAR (g);
5747 face_id = merge_faces (it->f, Qt, lface_id,
5748 it->face_id);
5749 }
5750 else if (it->f == last_escape_glyph_frame
5751 && it->face_id == last_escape_glyph_face_id)
5752 {
5753 face_id = last_escape_glyph_merged_face_id;
5754 }
5755 else
5756 {
5757 /* Merge the escape-glyph face into the current face. */
5758 face_id = merge_faces (it->f, Qescape_glyph, 0,
5759 it->face_id);
5760 last_escape_glyph_frame = it->f;
5761 last_escape_glyph_face_id = it->face_id;
5762 last_escape_glyph_merged_face_id = face_id;
5763 }
5764
5765 XSETINT (it->ctl_chars[0], g);
5766 g = it->c ^ 0100;
5767 XSETINT (it->ctl_chars[1], g);
5768 ctl_len = 2;
5769 goto display_control;
5770 }
5771
5772 /* Handle non-break space in the mode where it only gets
5773 highlighting. */
5774
5775 if (EQ (Vnobreak_char_display, Qt)
5776 && it->c == 0xA0)
5777 {
5778 /* Merge the no-break-space face into the current face. */
5779 face_id = merge_faces (it->f, Qnobreak_space, 0,
5780 it->face_id);
5781
5782 g = it->c = ' ';
5783 XSETINT (it->ctl_chars[0], g);
5784 ctl_len = 1;
5785 goto display_control;
5786 }
5787
5788 /* Handle sequences that start with the "escape glyph". */
5789
5790 /* the default escape glyph is \. */
5791 escape_glyph = '\\';
5792
5793 if (it->dp
5794 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5795 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5796 {
5797 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5798 lface_id = FAST_GLYPH_FACE (escape_glyph);
5799 }
5800 if (lface_id)
5801 {
5802 /* The display table specified a face.
5803 Merge it into face_id and also into escape_glyph. */
5804 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5805 face_id = merge_faces (it->f, Qt, lface_id,
5806 it->face_id);
5807 }
5808 else if (it->f == last_escape_glyph_frame
5809 && it->face_id == last_escape_glyph_face_id)
5810 {
5811 face_id = last_escape_glyph_merged_face_id;
5812 }
5813 else
5814 {
5815 /* Merge the escape-glyph face into the current face. */
5816 face_id = merge_faces (it->f, Qescape_glyph, 0,
5817 it->face_id);
5818 last_escape_glyph_frame = it->f;
5819 last_escape_glyph_face_id = it->face_id;
5820 last_escape_glyph_merged_face_id = face_id;
5821 }
5822
5823 /* Handle soft hyphens in the mode where they only get
5824 highlighting. */
5825
5826 if (EQ (Vnobreak_char_display, Qt)
5827 && it->c == 0xAD)
5828 {
5829 g = it->c = '-';
5830 XSETINT (it->ctl_chars[0], g);
5831 ctl_len = 1;
5832 goto display_control;
5833 }
5834
5835 /* Handle non-break space and soft hyphen
5836 with the escape glyph. */
5837
5838 if (it->c == 0xA0 || it->c == 0xAD)
5839 {
5840 XSETINT (it->ctl_chars[0], escape_glyph);
5841 g = it->c = (it->c == 0xA0 ? ' ' : '-');
5842 XSETINT (it->ctl_chars[1], g);
5843 ctl_len = 2;
5844 goto display_control;
5845 }
5846
5847 {
5848 unsigned char str[MAX_MULTIBYTE_LENGTH];
5849 int len;
5850 int i;
5851
5852 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5853 if (CHAR_BYTE8_P (it->c))
5854 {
5855 str[0] = CHAR_TO_BYTE8 (it->c);
5856 len = 1;
5857 }
5858 else if (it->c < 256)
5859 {
5860 str[0] = it->c;
5861 len = 1;
5862 }
5863 else
5864 {
5865 /* It's an invalid character, which shouldn't
5866 happen actually, but due to bugs it may
5867 happen. Let's print the char as is, there's
5868 not much meaningful we can do with it. */
5869 str[0] = it->c;
5870 str[1] = it->c >> 8;
5871 str[2] = it->c >> 16;
5872 str[3] = it->c >> 24;
5873 len = 4;
5874 }
5875
5876 for (i = 0; i < len; i++)
5877 {
5878 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5879 /* Insert three more glyphs into IT->ctl_chars for
5880 the octal display of the character. */
5881 g = ((str[i] >> 6) & 7) + '0';
5882 XSETINT (it->ctl_chars[i * 4 + 1], g);
5883 g = ((str[i] >> 3) & 7) + '0';
5884 XSETINT (it->ctl_chars[i * 4 + 2], g);
5885 g = (str[i] & 7) + '0';
5886 XSETINT (it->ctl_chars[i * 4 + 3], g);
5887 }
5888 ctl_len = len * 4;
5889 }
5890
5891 display_control:
5892 /* Set up IT->dpvec and return first character from it. */
5893 it->dpvec_char_len = it->len;
5894 it->dpvec = it->ctl_chars;
5895 it->dpend = it->dpvec + ctl_len;
5896 it->current.dpvec_index = 0;
5897 it->dpvec_face_id = face_id;
5898 it->saved_face_id = it->face_id;
5899 it->method = GET_FROM_DISPLAY_VECTOR;
5900 it->ellipsis_p = 0;
5901 goto get_next;
5902 }
5903 }
5904 }
5905
5906 /* Adjust face id for a multibyte character. There are no multibyte
5907 character in unibyte text. */
5908 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5909 && it->multibyte_p
5910 && success_p
5911 && FRAME_WINDOW_P (it->f))
5912 {
5913 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5914 int pos = (it->s ? -1
5915 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5916 : IT_CHARPOS (*it));
5917
5918 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5919 }
5920
5921 /* Is this character the last one of a run of characters with
5922 box? If yes, set IT->end_of_box_run_p to 1. */
5923 if (it->face_box_p
5924 && it->s == NULL)
5925 {
5926 int face_id;
5927 struct face *face;
5928
5929 it->end_of_box_run_p
5930 = ((face_id = face_after_it_pos (it),
5931 face_id != it->face_id)
5932 && (face = FACE_FROM_ID (it->f, face_id),
5933 face->box == FACE_NO_BOX));
5934 }
5935
5936 /* Value is 0 if end of buffer or string reached. */
5937 return success_p;
5938 }
5939
5940
5941 /* Move IT to the next display element.
5942
5943 RESEAT_P non-zero means if called on a newline in buffer text,
5944 skip to the next visible line start.
5945
5946 Functions get_next_display_element and set_iterator_to_next are
5947 separate because I find this arrangement easier to handle than a
5948 get_next_display_element function that also increments IT's
5949 position. The way it is we can first look at an iterator's current
5950 display element, decide whether it fits on a line, and if it does,
5951 increment the iterator position. The other way around we probably
5952 would either need a flag indicating whether the iterator has to be
5953 incremented the next time, or we would have to implement a
5954 decrement position function which would not be easy to write. */
5955
5956 void
5957 set_iterator_to_next (it, reseat_p)
5958 struct it *it;
5959 int reseat_p;
5960 {
5961 /* Reset flags indicating start and end of a sequence of characters
5962 with box. Reset them at the start of this function because
5963 moving the iterator to a new position might set them. */
5964 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5965
5966 switch (it->method)
5967 {
5968 case GET_FROM_BUFFER:
5969 /* The current display element of IT is a character from
5970 current_buffer. Advance in the buffer, and maybe skip over
5971 invisible lines that are so because of selective display. */
5972 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5973 reseat_at_next_visible_line_start (it, 0);
5974 else
5975 {
5976 xassert (it->len != 0);
5977 IT_BYTEPOS (*it) += it->len;
5978 IT_CHARPOS (*it) += 1;
5979 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5980 }
5981 break;
5982
5983 case GET_FROM_COMPOSITION:
5984 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5985 xassert (it->sp > 0);
5986 pop_it (it);
5987 if (it->method == GET_FROM_STRING)
5988 {
5989 IT_STRING_BYTEPOS (*it) += it->len;
5990 IT_STRING_CHARPOS (*it) += it->cmp_len;
5991 goto consider_string_end;
5992 }
5993 else if (it->method == GET_FROM_BUFFER)
5994 {
5995 IT_BYTEPOS (*it) += it->len;
5996 IT_CHARPOS (*it) += it->cmp_len;
5997 }
5998 break;
5999
6000 case GET_FROM_C_STRING:
6001 /* Current display element of IT is from a C string. */
6002 IT_BYTEPOS (*it) += it->len;
6003 IT_CHARPOS (*it) += 1;
6004 break;
6005
6006 case GET_FROM_DISPLAY_VECTOR:
6007 /* Current display element of IT is from a display table entry.
6008 Advance in the display table definition. Reset it to null if
6009 end reached, and continue with characters from buffers/
6010 strings. */
6011 ++it->current.dpvec_index;
6012
6013 /* Restore face of the iterator to what they were before the
6014 display vector entry (these entries may contain faces). */
6015 it->face_id = it->saved_face_id;
6016
6017 if (it->dpvec + it->current.dpvec_index == it->dpend)
6018 {
6019 int recheck_faces = it->ellipsis_p;
6020
6021 if (it->s)
6022 it->method = GET_FROM_C_STRING;
6023 else if (STRINGP (it->string))
6024 it->method = GET_FROM_STRING;
6025 else
6026 {
6027 it->method = GET_FROM_BUFFER;
6028 it->object = it->w->buffer;
6029 }
6030
6031 it->dpvec = NULL;
6032 it->current.dpvec_index = -1;
6033
6034 /* Skip over characters which were displayed via IT->dpvec. */
6035 if (it->dpvec_char_len < 0)
6036 reseat_at_next_visible_line_start (it, 1);
6037 else if (it->dpvec_char_len > 0)
6038 {
6039 if (it->method == GET_FROM_STRING
6040 && it->n_overlay_strings > 0)
6041 it->ignore_overlay_strings_at_pos_p = 1;
6042 it->len = it->dpvec_char_len;
6043 set_iterator_to_next (it, reseat_p);
6044 }
6045
6046 /* Maybe recheck faces after display vector */
6047 if (recheck_faces)
6048 it->stop_charpos = IT_CHARPOS (*it);
6049 }
6050 break;
6051
6052 case GET_FROM_STRING:
6053 /* Current display element is a character from a Lisp string. */
6054 xassert (it->s == NULL && STRINGP (it->string));
6055 IT_STRING_BYTEPOS (*it) += it->len;
6056 IT_STRING_CHARPOS (*it) += 1;
6057
6058 consider_string_end:
6059
6060 if (it->current.overlay_string_index >= 0)
6061 {
6062 /* IT->string is an overlay string. Advance to the
6063 next, if there is one. */
6064 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6065 next_overlay_string (it);
6066 }
6067 else
6068 {
6069 /* IT->string is not an overlay string. If we reached
6070 its end, and there is something on IT->stack, proceed
6071 with what is on the stack. This can be either another
6072 string, this time an overlay string, or a buffer. */
6073 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6074 && it->sp > 0)
6075 {
6076 pop_it (it);
6077 if (it->method == GET_FROM_STRING)
6078 goto consider_string_end;
6079 }
6080 }
6081 break;
6082
6083 case GET_FROM_IMAGE:
6084 case GET_FROM_STRETCH:
6085 /* The position etc with which we have to proceed are on
6086 the stack. The position may be at the end of a string,
6087 if the `display' property takes up the whole string. */
6088 xassert (it->sp > 0);
6089 pop_it (it);
6090 if (it->method == GET_FROM_STRING)
6091 goto consider_string_end;
6092 break;
6093
6094 default:
6095 /* There are no other methods defined, so this should be a bug. */
6096 abort ();
6097 }
6098
6099 xassert (it->method != GET_FROM_STRING
6100 || (STRINGP (it->string)
6101 && IT_STRING_CHARPOS (*it) >= 0));
6102 }
6103
6104 /* Load IT's display element fields with information about the next
6105 display element which comes from a display table entry or from the
6106 result of translating a control character to one of the forms `^C'
6107 or `\003'.
6108
6109 IT->dpvec holds the glyphs to return as characters.
6110 IT->saved_face_id holds the face id before the display vector--
6111 it is restored into IT->face_idin set_iterator_to_next. */
6112
6113 static int
6114 next_element_from_display_vector (it)
6115 struct it *it;
6116 {
6117 /* Precondition. */
6118 xassert (it->dpvec && it->current.dpvec_index >= 0);
6119
6120 it->face_id = it->saved_face_id;
6121
6122 if (INTEGERP (*it->dpvec)
6123 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6124 {
6125 GLYPH g;
6126
6127 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6128 it->c = FAST_GLYPH_CHAR (g);
6129 it->len = CHAR_BYTES (it->c);
6130
6131 /* The entry may contain a face id to use. Such a face id is
6132 the id of a Lisp face, not a realized face. A face id of
6133 zero means no face is specified. */
6134 if (it->dpvec_face_id >= 0)
6135 it->face_id = it->dpvec_face_id;
6136 else
6137 {
6138 int lface_id = FAST_GLYPH_FACE (g);
6139 if (lface_id > 0)
6140 it->face_id = merge_faces (it->f, Qt, lface_id,
6141 it->saved_face_id);
6142 }
6143 }
6144 else
6145 /* Display table entry is invalid. Return a space. */
6146 it->c = ' ', it->len = 1;
6147
6148 /* Don't change position and object of the iterator here. They are
6149 still the values of the character that had this display table
6150 entry or was translated, and that's what we want. */
6151 it->what = IT_CHARACTER;
6152 return 1;
6153 }
6154
6155
6156 /* Load IT with the next display element from Lisp string IT->string.
6157 IT->current.string_pos is the current position within the string.
6158 If IT->current.overlay_string_index >= 0, the Lisp string is an
6159 overlay string. */
6160
6161 static int
6162 next_element_from_string (it)
6163 struct it *it;
6164 {
6165 struct text_pos position;
6166
6167 xassert (STRINGP (it->string));
6168 xassert (IT_STRING_CHARPOS (*it) >= 0);
6169 position = it->current.string_pos;
6170
6171 /* Time to check for invisible text? */
6172 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6173 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6174 {
6175 handle_stop (it);
6176
6177 /* Since a handler may have changed IT->method, we must
6178 recurse here. */
6179 return get_next_display_element (it);
6180 }
6181
6182 if (it->current.overlay_string_index >= 0)
6183 {
6184 /* Get the next character from an overlay string. In overlay
6185 strings, There is no field width or padding with spaces to
6186 do. */
6187 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6188 {
6189 it->what = IT_EOB;
6190 return 0;
6191 }
6192 else if (STRING_MULTIBYTE (it->string))
6193 {
6194 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6195 const unsigned char *s = (SDATA (it->string)
6196 + IT_STRING_BYTEPOS (*it));
6197 it->c = string_char_and_length (s, remaining, &it->len);
6198 }
6199 else
6200 {
6201 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6202 it->len = 1;
6203 }
6204 }
6205 else
6206 {
6207 /* Get the next character from a Lisp string that is not an
6208 overlay string. Such strings come from the mode line, for
6209 example. We may have to pad with spaces, or truncate the
6210 string. See also next_element_from_c_string. */
6211 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6212 {
6213 it->what = IT_EOB;
6214 return 0;
6215 }
6216 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6217 {
6218 /* Pad with spaces. */
6219 it->c = ' ', it->len = 1;
6220 CHARPOS (position) = BYTEPOS (position) = -1;
6221 }
6222 else if (STRING_MULTIBYTE (it->string))
6223 {
6224 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6225 const unsigned char *s = (SDATA (it->string)
6226 + IT_STRING_BYTEPOS (*it));
6227 it->c = string_char_and_length (s, maxlen, &it->len);
6228 }
6229 else
6230 {
6231 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6232 it->len = 1;
6233 }
6234 }
6235
6236 /* Record what we have and where it came from. */
6237 it->what = IT_CHARACTER;
6238 it->object = it->string;
6239 it->position = position;
6240 return 1;
6241 }
6242
6243
6244 /* Load IT with next display element from C string IT->s.
6245 IT->string_nchars is the maximum number of characters to return
6246 from the string. IT->end_charpos may be greater than
6247 IT->string_nchars when this function is called, in which case we
6248 may have to return padding spaces. Value is zero if end of string
6249 reached, including padding spaces. */
6250
6251 static int
6252 next_element_from_c_string (it)
6253 struct it *it;
6254 {
6255 int success_p = 1;
6256
6257 xassert (it->s);
6258 it->what = IT_CHARACTER;
6259 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6260 it->object = Qnil;
6261
6262 /* IT's position can be greater IT->string_nchars in case a field
6263 width or precision has been specified when the iterator was
6264 initialized. */
6265 if (IT_CHARPOS (*it) >= it->end_charpos)
6266 {
6267 /* End of the game. */
6268 it->what = IT_EOB;
6269 success_p = 0;
6270 }
6271 else if (IT_CHARPOS (*it) >= it->string_nchars)
6272 {
6273 /* Pad with spaces. */
6274 it->c = ' ', it->len = 1;
6275 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6276 }
6277 else if (it->multibyte_p)
6278 {
6279 /* Implementation note: The calls to strlen apparently aren't a
6280 performance problem because there is no noticeable performance
6281 difference between Emacs running in unibyte or multibyte mode. */
6282 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6283 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6284 maxlen, &it->len);
6285 }
6286 else
6287 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6288
6289 return success_p;
6290 }
6291
6292
6293 /* Set up IT to return characters from an ellipsis, if appropriate.
6294 The definition of the ellipsis glyphs may come from a display table
6295 entry. This function Fills IT with the first glyph from the
6296 ellipsis if an ellipsis is to be displayed. */
6297
6298 static int
6299 next_element_from_ellipsis (it)
6300 struct it *it;
6301 {
6302 if (it->selective_display_ellipsis_p)
6303 setup_for_ellipsis (it, it->len);
6304 else
6305 {
6306 /* The face at the current position may be different from the
6307 face we find after the invisible text. Remember what it
6308 was in IT->saved_face_id, and signal that it's there by
6309 setting face_before_selective_p. */
6310 it->saved_face_id = it->face_id;
6311 it->method = GET_FROM_BUFFER;
6312 it->object = it->w->buffer;
6313 reseat_at_next_visible_line_start (it, 1);
6314 it->face_before_selective_p = 1;
6315 }
6316
6317 return get_next_display_element (it);
6318 }
6319
6320
6321 /* Deliver an image display element. The iterator IT is already
6322 filled with image information (done in handle_display_prop). Value
6323 is always 1. */
6324
6325
6326 static int
6327 next_element_from_image (it)
6328 struct it *it;
6329 {
6330 it->what = IT_IMAGE;
6331 return 1;
6332 }
6333
6334
6335 /* Fill iterator IT with next display element from a stretch glyph
6336 property. IT->object is the value of the text property. Value is
6337 always 1. */
6338
6339 static int
6340 next_element_from_stretch (it)
6341 struct it *it;
6342 {
6343 it->what = IT_STRETCH;
6344 return 1;
6345 }
6346
6347
6348 /* Load IT with the next display element from current_buffer. Value
6349 is zero if end of buffer reached. IT->stop_charpos is the next
6350 position at which to stop and check for text properties or buffer
6351 end. */
6352
6353 static int
6354 next_element_from_buffer (it)
6355 struct it *it;
6356 {
6357 int success_p = 1;
6358
6359 /* Check this assumption, otherwise, we would never enter the
6360 if-statement, below. */
6361 xassert (IT_CHARPOS (*it) >= BEGV
6362 && IT_CHARPOS (*it) <= it->stop_charpos);
6363
6364 if (IT_CHARPOS (*it) >= it->stop_charpos)
6365 {
6366 if (IT_CHARPOS (*it) >= it->end_charpos)
6367 {
6368 int overlay_strings_follow_p;
6369
6370 /* End of the game, except when overlay strings follow that
6371 haven't been returned yet. */
6372 if (it->overlay_strings_at_end_processed_p)
6373 overlay_strings_follow_p = 0;
6374 else
6375 {
6376 it->overlay_strings_at_end_processed_p = 1;
6377 overlay_strings_follow_p = get_overlay_strings (it, 0);
6378 }
6379
6380 if (overlay_strings_follow_p)
6381 success_p = get_next_display_element (it);
6382 else
6383 {
6384 it->what = IT_EOB;
6385 it->position = it->current.pos;
6386 success_p = 0;
6387 }
6388 }
6389 else
6390 {
6391 handle_stop (it);
6392 return get_next_display_element (it);
6393 }
6394 }
6395 else
6396 {
6397 /* No face changes, overlays etc. in sight, so just return a
6398 character from current_buffer. */
6399 unsigned char *p;
6400
6401 /* Maybe run the redisplay end trigger hook. Performance note:
6402 This doesn't seem to cost measurable time. */
6403 if (it->redisplay_end_trigger_charpos
6404 && it->glyph_row
6405 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6406 run_redisplay_end_trigger_hook (it);
6407
6408 /* Get the next character, maybe multibyte. */
6409 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6410 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6411 {
6412 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6413 - IT_BYTEPOS (*it));
6414 it->c = string_char_and_length (p, maxlen, &it->len);
6415 }
6416 else
6417 it->c = *p, it->len = 1;
6418
6419 /* Record what we have and where it came from. */
6420 it->what = IT_CHARACTER;
6421 it->object = it->w->buffer;
6422 it->position = it->current.pos;
6423
6424 /* Normally we return the character found above, except when we
6425 really want to return an ellipsis for selective display. */
6426 if (it->selective)
6427 {
6428 if (it->c == '\n')
6429 {
6430 /* A value of selective > 0 means hide lines indented more
6431 than that number of columns. */
6432 if (it->selective > 0
6433 && IT_CHARPOS (*it) + 1 < ZV
6434 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6435 IT_BYTEPOS (*it) + 1,
6436 (double) it->selective)) /* iftc */
6437 {
6438 success_p = next_element_from_ellipsis (it);
6439 it->dpvec_char_len = -1;
6440 }
6441 }
6442 else if (it->c == '\r' && it->selective == -1)
6443 {
6444 /* A value of selective == -1 means that everything from the
6445 CR to the end of the line is invisible, with maybe an
6446 ellipsis displayed for it. */
6447 success_p = next_element_from_ellipsis (it);
6448 it->dpvec_char_len = -1;
6449 }
6450 }
6451 }
6452
6453 /* Value is zero if end of buffer reached. */
6454 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6455 return success_p;
6456 }
6457
6458
6459 /* Run the redisplay end trigger hook for IT. */
6460
6461 static void
6462 run_redisplay_end_trigger_hook (it)
6463 struct it *it;
6464 {
6465 Lisp_Object args[3];
6466
6467 /* IT->glyph_row should be non-null, i.e. we should be actually
6468 displaying something, or otherwise we should not run the hook. */
6469 xassert (it->glyph_row);
6470
6471 /* Set up hook arguments. */
6472 args[0] = Qredisplay_end_trigger_functions;
6473 args[1] = it->window;
6474 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6475 it->redisplay_end_trigger_charpos = 0;
6476
6477 /* Since we are *trying* to run these functions, don't try to run
6478 them again, even if they get an error. */
6479 it->w->redisplay_end_trigger = Qnil;
6480 Frun_hook_with_args (3, args);
6481
6482 /* Notice if it changed the face of the character we are on. */
6483 handle_face_prop (it);
6484 }
6485
6486
6487 /* Deliver a composition display element. The iterator IT is already
6488 filled with composition information (done in
6489 handle_composition_prop). Value is always 1. */
6490
6491 static int
6492 next_element_from_composition (it)
6493 struct it *it;
6494 {
6495 it->what = IT_COMPOSITION;
6496 it->position = (STRINGP (it->string)
6497 ? it->current.string_pos
6498 : it->current.pos);
6499 if (STRINGP (it->string))
6500 it->object = it->string;
6501 else
6502 it->object = it->w->buffer;
6503 return 1;
6504 }
6505
6506
6507 \f
6508 /***********************************************************************
6509 Moving an iterator without producing glyphs
6510 ***********************************************************************/
6511
6512 /* Check if iterator is at a position corresponding to a valid buffer
6513 position after some move_it_ call. */
6514
6515 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6516 ((it)->method == GET_FROM_STRING \
6517 ? IT_STRING_CHARPOS (*it) == 0 \
6518 : 1)
6519
6520
6521 /* Move iterator IT to a specified buffer or X position within one
6522 line on the display without producing glyphs.
6523
6524 OP should be a bit mask including some or all of these bits:
6525 MOVE_TO_X: Stop on reaching x-position TO_X.
6526 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6527 Regardless of OP's value, stop in reaching the end of the display line.
6528
6529 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6530 This means, in particular, that TO_X includes window's horizontal
6531 scroll amount.
6532
6533 The return value has several possible values that
6534 say what condition caused the scan to stop:
6535
6536 MOVE_POS_MATCH_OR_ZV
6537 - when TO_POS or ZV was reached.
6538
6539 MOVE_X_REACHED
6540 -when TO_X was reached before TO_POS or ZV were reached.
6541
6542 MOVE_LINE_CONTINUED
6543 - when we reached the end of the display area and the line must
6544 be continued.
6545
6546 MOVE_LINE_TRUNCATED
6547 - when we reached the end of the display area and the line is
6548 truncated.
6549
6550 MOVE_NEWLINE_OR_CR
6551 - when we stopped at a line end, i.e. a newline or a CR and selective
6552 display is on. */
6553
6554 static enum move_it_result
6555 move_it_in_display_line_to (it, to_charpos, to_x, op)
6556 struct it *it;
6557 int to_charpos, to_x, op;
6558 {
6559 enum move_it_result result = MOVE_UNDEFINED;
6560 struct glyph_row *saved_glyph_row;
6561
6562 /* Don't produce glyphs in produce_glyphs. */
6563 saved_glyph_row = it->glyph_row;
6564 it->glyph_row = NULL;
6565
6566 #define BUFFER_POS_REACHED_P() \
6567 ((op & MOVE_TO_POS) != 0 \
6568 && BUFFERP (it->object) \
6569 && IT_CHARPOS (*it) >= to_charpos \
6570 && (it->method == GET_FROM_BUFFER \
6571 || (it->method == GET_FROM_DISPLAY_VECTOR \
6572 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6573
6574
6575 while (1)
6576 {
6577 int x, i, ascent = 0, descent = 0;
6578
6579 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6580 if ((op & MOVE_TO_POS) != 0
6581 && BUFFERP (it->object)
6582 && it->method == GET_FROM_BUFFER
6583 && IT_CHARPOS (*it) > to_charpos)
6584 {
6585 result = MOVE_POS_MATCH_OR_ZV;
6586 break;
6587 }
6588
6589 /* Stop when ZV reached.
6590 We used to stop here when TO_CHARPOS reached as well, but that is
6591 too soon if this glyph does not fit on this line. So we handle it
6592 explicitly below. */
6593 if (!get_next_display_element (it)
6594 || (it->truncate_lines_p
6595 && BUFFER_POS_REACHED_P ()))
6596 {
6597 result = MOVE_POS_MATCH_OR_ZV;
6598 break;
6599 }
6600
6601 /* The call to produce_glyphs will get the metrics of the
6602 display element IT is loaded with. We record in x the
6603 x-position before this display element in case it does not
6604 fit on the line. */
6605 x = it->current_x;
6606
6607 /* Remember the line height so far in case the next element doesn't
6608 fit on the line. */
6609 if (!it->truncate_lines_p)
6610 {
6611 ascent = it->max_ascent;
6612 descent = it->max_descent;
6613 }
6614
6615 PRODUCE_GLYPHS (it);
6616
6617 if (it->area != TEXT_AREA)
6618 {
6619 set_iterator_to_next (it, 1);
6620 continue;
6621 }
6622
6623 /* The number of glyphs we get back in IT->nglyphs will normally
6624 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6625 character on a terminal frame, or (iii) a line end. For the
6626 second case, IT->nglyphs - 1 padding glyphs will be present
6627 (on X frames, there is only one glyph produced for a
6628 composite character.
6629
6630 The behavior implemented below means, for continuation lines,
6631 that as many spaces of a TAB as fit on the current line are
6632 displayed there. For terminal frames, as many glyphs of a
6633 multi-glyph character are displayed in the current line, too.
6634 This is what the old redisplay code did, and we keep it that
6635 way. Under X, the whole shape of a complex character must
6636 fit on the line or it will be completely displayed in the
6637 next line.
6638
6639 Note that both for tabs and padding glyphs, all glyphs have
6640 the same width. */
6641 if (it->nglyphs)
6642 {
6643 /* More than one glyph or glyph doesn't fit on line. All
6644 glyphs have the same width. */
6645 int single_glyph_width = it->pixel_width / it->nglyphs;
6646 int new_x;
6647 int x_before_this_char = x;
6648 int hpos_before_this_char = it->hpos;
6649
6650 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6651 {
6652 new_x = x + single_glyph_width;
6653
6654 /* We want to leave anything reaching TO_X to the caller. */
6655 if ((op & MOVE_TO_X) && new_x > to_x)
6656 {
6657 if (BUFFER_POS_REACHED_P ())
6658 goto buffer_pos_reached;
6659 it->current_x = x;
6660 result = MOVE_X_REACHED;
6661 break;
6662 }
6663 else if (/* Lines are continued. */
6664 !it->truncate_lines_p
6665 && (/* And glyph doesn't fit on the line. */
6666 new_x > it->last_visible_x
6667 /* Or it fits exactly and we're on a window
6668 system frame. */
6669 || (new_x == it->last_visible_x
6670 && FRAME_WINDOW_P (it->f))))
6671 {
6672 if (/* IT->hpos == 0 means the very first glyph
6673 doesn't fit on the line, e.g. a wide image. */
6674 it->hpos == 0
6675 || (new_x == it->last_visible_x
6676 && FRAME_WINDOW_P (it->f)))
6677 {
6678 ++it->hpos;
6679 it->current_x = new_x;
6680
6681 /* The character's last glyph just barely fits
6682 in this row. */
6683 if (i == it->nglyphs - 1)
6684 {
6685 /* If this is the destination position,
6686 return a position *before* it in this row,
6687 now that we know it fits in this row. */
6688 if (BUFFER_POS_REACHED_P ())
6689 {
6690 it->hpos = hpos_before_this_char;
6691 it->current_x = x_before_this_char;
6692 result = MOVE_POS_MATCH_OR_ZV;
6693 break;
6694 }
6695
6696 set_iterator_to_next (it, 1);
6697 #ifdef HAVE_WINDOW_SYSTEM
6698 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6699 {
6700 if (!get_next_display_element (it))
6701 {
6702 result = MOVE_POS_MATCH_OR_ZV;
6703 break;
6704 }
6705 if (BUFFER_POS_REACHED_P ())
6706 {
6707 if (ITERATOR_AT_END_OF_LINE_P (it))
6708 result = MOVE_POS_MATCH_OR_ZV;
6709 else
6710 result = MOVE_LINE_CONTINUED;
6711 break;
6712 }
6713 if (ITERATOR_AT_END_OF_LINE_P (it))
6714 {
6715 result = MOVE_NEWLINE_OR_CR;
6716 break;
6717 }
6718 }
6719 #endif /* HAVE_WINDOW_SYSTEM */
6720 }
6721 }
6722 else
6723 {
6724 it->current_x = x;
6725 it->max_ascent = ascent;
6726 it->max_descent = descent;
6727 }
6728
6729 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6730 IT_CHARPOS (*it)));
6731 result = MOVE_LINE_CONTINUED;
6732 break;
6733 }
6734 else if (BUFFER_POS_REACHED_P ())
6735 goto buffer_pos_reached;
6736 else if (new_x > it->first_visible_x)
6737 {
6738 /* Glyph is visible. Increment number of glyphs that
6739 would be displayed. */
6740 ++it->hpos;
6741 }
6742 else
6743 {
6744 /* Glyph is completely off the left margin of the display
6745 area. Nothing to do. */
6746 }
6747 }
6748
6749 if (result != MOVE_UNDEFINED)
6750 break;
6751 }
6752 else if (BUFFER_POS_REACHED_P ())
6753 {
6754 buffer_pos_reached:
6755 it->current_x = x;
6756 it->max_ascent = ascent;
6757 it->max_descent = descent;
6758 result = MOVE_POS_MATCH_OR_ZV;
6759 break;
6760 }
6761 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6762 {
6763 /* Stop when TO_X specified and reached. This check is
6764 necessary here because of lines consisting of a line end,
6765 only. The line end will not produce any glyphs and we
6766 would never get MOVE_X_REACHED. */
6767 xassert (it->nglyphs == 0);
6768 result = MOVE_X_REACHED;
6769 break;
6770 }
6771
6772 /* Is this a line end? If yes, we're done. */
6773 if (ITERATOR_AT_END_OF_LINE_P (it))
6774 {
6775 result = MOVE_NEWLINE_OR_CR;
6776 break;
6777 }
6778
6779 /* The current display element has been consumed. Advance
6780 to the next. */
6781 set_iterator_to_next (it, 1);
6782
6783 /* Stop if lines are truncated and IT's current x-position is
6784 past the right edge of the window now. */
6785 if (it->truncate_lines_p
6786 && it->current_x >= it->last_visible_x)
6787 {
6788 #ifdef HAVE_WINDOW_SYSTEM
6789 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6790 {
6791 if (!get_next_display_element (it)
6792 || BUFFER_POS_REACHED_P ())
6793 {
6794 result = MOVE_POS_MATCH_OR_ZV;
6795 break;
6796 }
6797 if (ITERATOR_AT_END_OF_LINE_P (it))
6798 {
6799 result = MOVE_NEWLINE_OR_CR;
6800 break;
6801 }
6802 }
6803 #endif /* HAVE_WINDOW_SYSTEM */
6804 result = MOVE_LINE_TRUNCATED;
6805 break;
6806 }
6807 }
6808
6809 #undef BUFFER_POS_REACHED_P
6810
6811 /* Restore the iterator settings altered at the beginning of this
6812 function. */
6813 it->glyph_row = saved_glyph_row;
6814 return result;
6815 }
6816
6817
6818 /* Move IT forward until it satisfies one or more of the criteria in
6819 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6820
6821 OP is a bit-mask that specifies where to stop, and in particular,
6822 which of those four position arguments makes a difference. See the
6823 description of enum move_operation_enum.
6824
6825 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6826 screen line, this function will set IT to the next position >
6827 TO_CHARPOS. */
6828
6829 void
6830 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6831 struct it *it;
6832 int to_charpos, to_x, to_y, to_vpos;
6833 int op;
6834 {
6835 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6836 int line_height;
6837 int reached = 0;
6838
6839 for (;;)
6840 {
6841 if (op & MOVE_TO_VPOS)
6842 {
6843 /* If no TO_CHARPOS and no TO_X specified, stop at the
6844 start of the line TO_VPOS. */
6845 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6846 {
6847 if (it->vpos == to_vpos)
6848 {
6849 reached = 1;
6850 break;
6851 }
6852 else
6853 skip = move_it_in_display_line_to (it, -1, -1, 0);
6854 }
6855 else
6856 {
6857 /* TO_VPOS >= 0 means stop at TO_X in the line at
6858 TO_VPOS, or at TO_POS, whichever comes first. */
6859 if (it->vpos == to_vpos)
6860 {
6861 reached = 2;
6862 break;
6863 }
6864
6865 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6866
6867 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6868 {
6869 reached = 3;
6870 break;
6871 }
6872 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6873 {
6874 /* We have reached TO_X but not in the line we want. */
6875 skip = move_it_in_display_line_to (it, to_charpos,
6876 -1, MOVE_TO_POS);
6877 if (skip == MOVE_POS_MATCH_OR_ZV)
6878 {
6879 reached = 4;
6880 break;
6881 }
6882 }
6883 }
6884 }
6885 else if (op & MOVE_TO_Y)
6886 {
6887 struct it it_backup;
6888
6889 /* TO_Y specified means stop at TO_X in the line containing
6890 TO_Y---or at TO_CHARPOS if this is reached first. The
6891 problem is that we can't really tell whether the line
6892 contains TO_Y before we have completely scanned it, and
6893 this may skip past TO_X. What we do is to first scan to
6894 TO_X.
6895
6896 If TO_X is not specified, use a TO_X of zero. The reason
6897 is to make the outcome of this function more predictable.
6898 If we didn't use TO_X == 0, we would stop at the end of
6899 the line which is probably not what a caller would expect
6900 to happen. */
6901 skip = move_it_in_display_line_to (it, to_charpos,
6902 ((op & MOVE_TO_X)
6903 ? to_x : 0),
6904 (MOVE_TO_X
6905 | (op & MOVE_TO_POS)));
6906
6907 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6908 if (skip == MOVE_POS_MATCH_OR_ZV)
6909 {
6910 reached = 5;
6911 break;
6912 }
6913
6914 /* If TO_X was reached, we would like to know whether TO_Y
6915 is in the line. This can only be said if we know the
6916 total line height which requires us to scan the rest of
6917 the line. */
6918 if (skip == MOVE_X_REACHED)
6919 {
6920 /* Wait! We can conclude that TO_Y is in the line if
6921 the already scanned glyphs make the line tall enough
6922 because further scanning doesn't make it shorter. */
6923 line_height = it->max_ascent + it->max_descent;
6924 if (to_y >= it->current_y
6925 && to_y < it->current_y + line_height)
6926 {
6927 reached = 6;
6928 break;
6929 }
6930 it_backup = *it;
6931 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6932 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6933 op & MOVE_TO_POS);
6934 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6935 }
6936
6937 /* Now, decide whether TO_Y is in this line. */
6938 line_height = it->max_ascent + it->max_descent;
6939 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6940
6941 if (to_y >= it->current_y
6942 && to_y < it->current_y + line_height)
6943 {
6944 if (skip == MOVE_X_REACHED)
6945 /* If TO_Y is in this line and TO_X was reached above,
6946 we scanned too far. We have to restore IT's settings
6947 to the ones before skipping. */
6948 *it = it_backup;
6949 reached = 6;
6950 }
6951 else if (skip == MOVE_X_REACHED)
6952 {
6953 skip = skip2;
6954 if (skip == MOVE_POS_MATCH_OR_ZV)
6955 reached = 7;
6956 }
6957
6958 if (reached)
6959 break;
6960 }
6961 else if (BUFFERP (it->object)
6962 && it->method == GET_FROM_BUFFER
6963 && IT_CHARPOS (*it) >= to_charpos)
6964 skip = MOVE_POS_MATCH_OR_ZV;
6965 else
6966 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6967
6968 switch (skip)
6969 {
6970 case MOVE_POS_MATCH_OR_ZV:
6971 reached = 8;
6972 goto out;
6973
6974 case MOVE_NEWLINE_OR_CR:
6975 set_iterator_to_next (it, 1);
6976 it->continuation_lines_width = 0;
6977 break;
6978
6979 case MOVE_LINE_TRUNCATED:
6980 it->continuation_lines_width = 0;
6981 reseat_at_next_visible_line_start (it, 0);
6982 if ((op & MOVE_TO_POS) != 0
6983 && IT_CHARPOS (*it) > to_charpos)
6984 {
6985 reached = 9;
6986 goto out;
6987 }
6988 break;
6989
6990 case MOVE_LINE_CONTINUED:
6991 /* For continued lines ending in a tab, some of the glyphs
6992 associated with the tab are displayed on the current
6993 line. Since it->current_x does not include these glyphs,
6994 we use it->last_visible_x instead. */
6995 it->continuation_lines_width +=
6996 (it->c == '\t') ? it->last_visible_x : it->current_x;
6997 break;
6998
6999 default:
7000 abort ();
7001 }
7002
7003 /* Reset/increment for the next run. */
7004 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7005 it->current_x = it->hpos = 0;
7006 it->current_y += it->max_ascent + it->max_descent;
7007 ++it->vpos;
7008 last_height = it->max_ascent + it->max_descent;
7009 last_max_ascent = it->max_ascent;
7010 it->max_ascent = it->max_descent = 0;
7011 }
7012
7013 out:
7014
7015 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7016 }
7017
7018
7019 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7020
7021 If DY > 0, move IT backward at least that many pixels. DY = 0
7022 means move IT backward to the preceding line start or BEGV. This
7023 function may move over more than DY pixels if IT->current_y - DY
7024 ends up in the middle of a line; in this case IT->current_y will be
7025 set to the top of the line moved to. */
7026
7027 void
7028 move_it_vertically_backward (it, dy)
7029 struct it *it;
7030 int dy;
7031 {
7032 int nlines, h;
7033 struct it it2, it3;
7034 int start_pos;
7035
7036 move_further_back:
7037 xassert (dy >= 0);
7038
7039 start_pos = IT_CHARPOS (*it);
7040
7041 /* Estimate how many newlines we must move back. */
7042 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7043
7044 /* Set the iterator's position that many lines back. */
7045 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7046 back_to_previous_visible_line_start (it);
7047
7048 /* Reseat the iterator here. When moving backward, we don't want
7049 reseat to skip forward over invisible text, set up the iterator
7050 to deliver from overlay strings at the new position etc. So,
7051 use reseat_1 here. */
7052 reseat_1 (it, it->current.pos, 1);
7053
7054 /* We are now surely at a line start. */
7055 it->current_x = it->hpos = 0;
7056 it->continuation_lines_width = 0;
7057
7058 /* Move forward and see what y-distance we moved. First move to the
7059 start of the next line so that we get its height. We need this
7060 height to be able to tell whether we reached the specified
7061 y-distance. */
7062 it2 = *it;
7063 it2.max_ascent = it2.max_descent = 0;
7064 do
7065 {
7066 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7067 MOVE_TO_POS | MOVE_TO_VPOS);
7068 }
7069 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7070 xassert (IT_CHARPOS (*it) >= BEGV);
7071 it3 = it2;
7072
7073 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7074 xassert (IT_CHARPOS (*it) >= BEGV);
7075 /* H is the actual vertical distance from the position in *IT
7076 and the starting position. */
7077 h = it2.current_y - it->current_y;
7078 /* NLINES is the distance in number of lines. */
7079 nlines = it2.vpos - it->vpos;
7080
7081 /* Correct IT's y and vpos position
7082 so that they are relative to the starting point. */
7083 it->vpos -= nlines;
7084 it->current_y -= h;
7085
7086 if (dy == 0)
7087 {
7088 /* DY == 0 means move to the start of the screen line. The
7089 value of nlines is > 0 if continuation lines were involved. */
7090 if (nlines > 0)
7091 move_it_by_lines (it, nlines, 1);
7092 #if 0
7093 /* I think this assert is bogus if buffer contains
7094 invisible text or images. KFS. */
7095 xassert (IT_CHARPOS (*it) <= start_pos);
7096 #endif
7097 }
7098 else
7099 {
7100 /* The y-position we try to reach, relative to *IT.
7101 Note that H has been subtracted in front of the if-statement. */
7102 int target_y = it->current_y + h - dy;
7103 int y0 = it3.current_y;
7104 int y1 = line_bottom_y (&it3);
7105 int line_height = y1 - y0;
7106
7107 /* If we did not reach target_y, try to move further backward if
7108 we can. If we moved too far backward, try to move forward. */
7109 if (target_y < it->current_y
7110 /* This is heuristic. In a window that's 3 lines high, with
7111 a line height of 13 pixels each, recentering with point
7112 on the bottom line will try to move -39/2 = 19 pixels
7113 backward. Try to avoid moving into the first line. */
7114 && (it->current_y - target_y
7115 > min (window_box_height (it->w), line_height * 2 / 3))
7116 && IT_CHARPOS (*it) > BEGV)
7117 {
7118 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7119 target_y - it->current_y));
7120 dy = it->current_y - target_y;
7121 goto move_further_back;
7122 }
7123 else if (target_y >= it->current_y + line_height
7124 && IT_CHARPOS (*it) < ZV)
7125 {
7126 /* Should move forward by at least one line, maybe more.
7127
7128 Note: Calling move_it_by_lines can be expensive on
7129 terminal frames, where compute_motion is used (via
7130 vmotion) to do the job, when there are very long lines
7131 and truncate-lines is nil. That's the reason for
7132 treating terminal frames specially here. */
7133
7134 if (!FRAME_WINDOW_P (it->f))
7135 move_it_vertically (it, target_y - (it->current_y + line_height));
7136 else
7137 {
7138 do
7139 {
7140 move_it_by_lines (it, 1, 1);
7141 }
7142 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7143 }
7144
7145 #if 0
7146 /* I think this assert is bogus if buffer contains
7147 invisible text or images. KFS. */
7148 xassert (IT_CHARPOS (*it) >= BEGV);
7149 #endif
7150 }
7151 }
7152 }
7153
7154
7155 /* Move IT by a specified amount of pixel lines DY. DY negative means
7156 move backwards. DY = 0 means move to start of screen line. At the
7157 end, IT will be on the start of a screen line. */
7158
7159 void
7160 move_it_vertically (it, dy)
7161 struct it *it;
7162 int dy;
7163 {
7164 if (dy <= 0)
7165 move_it_vertically_backward (it, -dy);
7166 else
7167 {
7168 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7169 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7170 MOVE_TO_POS | MOVE_TO_Y);
7171 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7172
7173 /* If buffer ends in ZV without a newline, move to the start of
7174 the line to satisfy the post-condition. */
7175 if (IT_CHARPOS (*it) == ZV
7176 && ZV > BEGV
7177 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7178 move_it_by_lines (it, 0, 0);
7179 }
7180 }
7181
7182
7183 /* Move iterator IT past the end of the text line it is in. */
7184
7185 void
7186 move_it_past_eol (it)
7187 struct it *it;
7188 {
7189 enum move_it_result rc;
7190
7191 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7192 if (rc == MOVE_NEWLINE_OR_CR)
7193 set_iterator_to_next (it, 0);
7194 }
7195
7196
7197 #if 0 /* Currently not used. */
7198
7199 /* Return non-zero if some text between buffer positions START_CHARPOS
7200 and END_CHARPOS is invisible. IT->window is the window for text
7201 property lookup. */
7202
7203 static int
7204 invisible_text_between_p (it, start_charpos, end_charpos)
7205 struct it *it;
7206 int start_charpos, end_charpos;
7207 {
7208 Lisp_Object prop, limit;
7209 int invisible_found_p;
7210
7211 xassert (it != NULL && start_charpos <= end_charpos);
7212
7213 /* Is text at START invisible? */
7214 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7215 it->window);
7216 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7217 invisible_found_p = 1;
7218 else
7219 {
7220 limit = Fnext_single_char_property_change (make_number (start_charpos),
7221 Qinvisible, Qnil,
7222 make_number (end_charpos));
7223 invisible_found_p = XFASTINT (limit) < end_charpos;
7224 }
7225
7226 return invisible_found_p;
7227 }
7228
7229 #endif /* 0 */
7230
7231
7232 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7233 negative means move up. DVPOS == 0 means move to the start of the
7234 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7235 NEED_Y_P is zero, IT->current_y will be left unchanged.
7236
7237 Further optimization ideas: If we would know that IT->f doesn't use
7238 a face with proportional font, we could be faster for
7239 truncate-lines nil. */
7240
7241 void
7242 move_it_by_lines (it, dvpos, need_y_p)
7243 struct it *it;
7244 int dvpos, need_y_p;
7245 {
7246 struct position pos;
7247
7248 /* The commented-out optimization uses vmotion on terminals. This
7249 gives bad results, because elements like it->what, on which
7250 callers such as pos_visible_p rely, aren't updated. */
7251 /* if (!FRAME_WINDOW_P (it->f))
7252 {
7253 struct text_pos textpos;
7254
7255 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7256 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7257 reseat (it, textpos, 1);
7258 it->vpos += pos.vpos;
7259 it->current_y += pos.vpos;
7260 }
7261 else */
7262
7263 if (dvpos == 0)
7264 {
7265 /* DVPOS == 0 means move to the start of the screen line. */
7266 move_it_vertically_backward (it, 0);
7267 xassert (it->current_x == 0 && it->hpos == 0);
7268 /* Let next call to line_bottom_y calculate real line height */
7269 last_height = 0;
7270 }
7271 else if (dvpos > 0)
7272 {
7273 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7274 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7275 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7276 }
7277 else
7278 {
7279 struct it it2;
7280 int start_charpos, i;
7281
7282 /* Start at the beginning of the screen line containing IT's
7283 position. This may actually move vertically backwards,
7284 in case of overlays, so adjust dvpos accordingly. */
7285 dvpos += it->vpos;
7286 move_it_vertically_backward (it, 0);
7287 dvpos -= it->vpos;
7288
7289 /* Go back -DVPOS visible lines and reseat the iterator there. */
7290 start_charpos = IT_CHARPOS (*it);
7291 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7292 back_to_previous_visible_line_start (it);
7293 reseat (it, it->current.pos, 1);
7294
7295 /* Move further back if we end up in a string or an image. */
7296 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7297 {
7298 /* First try to move to start of display line. */
7299 dvpos += it->vpos;
7300 move_it_vertically_backward (it, 0);
7301 dvpos -= it->vpos;
7302 if (IT_POS_VALID_AFTER_MOVE_P (it))
7303 break;
7304 /* If start of line is still in string or image,
7305 move further back. */
7306 back_to_previous_visible_line_start (it);
7307 reseat (it, it->current.pos, 1);
7308 dvpos--;
7309 }
7310
7311 it->current_x = it->hpos = 0;
7312
7313 /* Above call may have moved too far if continuation lines
7314 are involved. Scan forward and see if it did. */
7315 it2 = *it;
7316 it2.vpos = it2.current_y = 0;
7317 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7318 it->vpos -= it2.vpos;
7319 it->current_y -= it2.current_y;
7320 it->current_x = it->hpos = 0;
7321
7322 /* If we moved too far back, move IT some lines forward. */
7323 if (it2.vpos > -dvpos)
7324 {
7325 int delta = it2.vpos + dvpos;
7326 it2 = *it;
7327 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7328 /* Move back again if we got too far ahead. */
7329 if (IT_CHARPOS (*it) >= start_charpos)
7330 *it = it2;
7331 }
7332 }
7333 }
7334
7335 /* Return 1 if IT points into the middle of a display vector. */
7336
7337 int
7338 in_display_vector_p (it)
7339 struct it *it;
7340 {
7341 return (it->method == GET_FROM_DISPLAY_VECTOR
7342 && it->current.dpvec_index > 0
7343 && it->dpvec + it->current.dpvec_index != it->dpend);
7344 }
7345
7346 \f
7347 /***********************************************************************
7348 Messages
7349 ***********************************************************************/
7350
7351
7352 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7353 to *Messages*. */
7354
7355 void
7356 add_to_log (format, arg1, arg2)
7357 char *format;
7358 Lisp_Object arg1, arg2;
7359 {
7360 Lisp_Object args[3];
7361 Lisp_Object msg, fmt;
7362 char *buffer;
7363 int len;
7364 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7365 USE_SAFE_ALLOCA;
7366
7367 /* Do nothing if called asynchronously. Inserting text into
7368 a buffer may call after-change-functions and alike and
7369 that would means running Lisp asynchronously. */
7370 if (handling_signal)
7371 return;
7372
7373 fmt = msg = Qnil;
7374 GCPRO4 (fmt, msg, arg1, arg2);
7375
7376 args[0] = fmt = build_string (format);
7377 args[1] = arg1;
7378 args[2] = arg2;
7379 msg = Fformat (3, args);
7380
7381 len = SBYTES (msg) + 1;
7382 SAFE_ALLOCA (buffer, char *, len);
7383 bcopy (SDATA (msg), buffer, len);
7384
7385 message_dolog (buffer, len - 1, 1, 0);
7386 SAFE_FREE ();
7387
7388 UNGCPRO;
7389 }
7390
7391
7392 /* Output a newline in the *Messages* buffer if "needs" one. */
7393
7394 void
7395 message_log_maybe_newline ()
7396 {
7397 if (message_log_need_newline)
7398 message_dolog ("", 0, 1, 0);
7399 }
7400
7401
7402 /* Add a string M of length NBYTES to the message log, optionally
7403 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7404 nonzero, means interpret the contents of M as multibyte. This
7405 function calls low-level routines in order to bypass text property
7406 hooks, etc. which might not be safe to run.
7407
7408 This may GC (insert may run before/after change hooks),
7409 so the buffer M must NOT point to a Lisp string. */
7410
7411 void
7412 message_dolog (m, nbytes, nlflag, multibyte)
7413 const char *m;
7414 int nbytes, nlflag, multibyte;
7415 {
7416 if (!NILP (Vmemory_full))
7417 return;
7418
7419 if (!NILP (Vmessage_log_max))
7420 {
7421 struct buffer *oldbuf;
7422 Lisp_Object oldpoint, oldbegv, oldzv;
7423 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7424 int point_at_end = 0;
7425 int zv_at_end = 0;
7426 Lisp_Object old_deactivate_mark, tem;
7427 struct gcpro gcpro1;
7428
7429 old_deactivate_mark = Vdeactivate_mark;
7430 oldbuf = current_buffer;
7431 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7432 current_buffer->undo_list = Qt;
7433
7434 oldpoint = message_dolog_marker1;
7435 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7436 oldbegv = message_dolog_marker2;
7437 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7438 oldzv = message_dolog_marker3;
7439 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7440 GCPRO1 (old_deactivate_mark);
7441
7442 if (PT == Z)
7443 point_at_end = 1;
7444 if (ZV == Z)
7445 zv_at_end = 1;
7446
7447 BEGV = BEG;
7448 BEGV_BYTE = BEG_BYTE;
7449 ZV = Z;
7450 ZV_BYTE = Z_BYTE;
7451 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7452
7453 /* Insert the string--maybe converting multibyte to single byte
7454 or vice versa, so that all the text fits the buffer. */
7455 if (multibyte
7456 && NILP (current_buffer->enable_multibyte_characters))
7457 {
7458 int i, c, char_bytes;
7459 unsigned char work[1];
7460
7461 /* Convert a multibyte string to single-byte
7462 for the *Message* buffer. */
7463 for (i = 0; i < nbytes; i += char_bytes)
7464 {
7465 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7466 work[0] = (ASCII_CHAR_P (c)
7467 ? c
7468 : multibyte_char_to_unibyte (c, Qnil));
7469 insert_1_both (work, 1, 1, 1, 0, 0);
7470 }
7471 }
7472 else if (! multibyte
7473 && ! NILP (current_buffer->enable_multibyte_characters))
7474 {
7475 int i, c, char_bytes;
7476 unsigned char *msg = (unsigned char *) m;
7477 unsigned char str[MAX_MULTIBYTE_LENGTH];
7478 /* Convert a single-byte string to multibyte
7479 for the *Message* buffer. */
7480 for (i = 0; i < nbytes; i++)
7481 {
7482 c = msg[i];
7483 c = unibyte_char_to_multibyte (c);
7484 char_bytes = CHAR_STRING (c, str);
7485 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7486 }
7487 }
7488 else if (nbytes)
7489 insert_1 (m, nbytes, 1, 0, 0);
7490
7491 if (nlflag)
7492 {
7493 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7494 insert_1 ("\n", 1, 1, 0, 0);
7495
7496 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7497 this_bol = PT;
7498 this_bol_byte = PT_BYTE;
7499
7500 /* See if this line duplicates the previous one.
7501 If so, combine duplicates. */
7502 if (this_bol > BEG)
7503 {
7504 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7505 prev_bol = PT;
7506 prev_bol_byte = PT_BYTE;
7507
7508 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7509 this_bol, this_bol_byte);
7510 if (dup)
7511 {
7512 del_range_both (prev_bol, prev_bol_byte,
7513 this_bol, this_bol_byte, 0);
7514 if (dup > 1)
7515 {
7516 char dupstr[40];
7517 int duplen;
7518
7519 /* If you change this format, don't forget to also
7520 change message_log_check_duplicate. */
7521 sprintf (dupstr, " [%d times]", dup);
7522 duplen = strlen (dupstr);
7523 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7524 insert_1 (dupstr, duplen, 1, 0, 1);
7525 }
7526 }
7527 }
7528
7529 /* If we have more than the desired maximum number of lines
7530 in the *Messages* buffer now, delete the oldest ones.
7531 This is safe because we don't have undo in this buffer. */
7532
7533 if (NATNUMP (Vmessage_log_max))
7534 {
7535 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7536 -XFASTINT (Vmessage_log_max) - 1, 0);
7537 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7538 }
7539 }
7540 BEGV = XMARKER (oldbegv)->charpos;
7541 BEGV_BYTE = marker_byte_position (oldbegv);
7542
7543 if (zv_at_end)
7544 {
7545 ZV = Z;
7546 ZV_BYTE = Z_BYTE;
7547 }
7548 else
7549 {
7550 ZV = XMARKER (oldzv)->charpos;
7551 ZV_BYTE = marker_byte_position (oldzv);
7552 }
7553
7554 if (point_at_end)
7555 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7556 else
7557 /* We can't do Fgoto_char (oldpoint) because it will run some
7558 Lisp code. */
7559 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7560 XMARKER (oldpoint)->bytepos);
7561
7562 UNGCPRO;
7563 unchain_marker (XMARKER (oldpoint));
7564 unchain_marker (XMARKER (oldbegv));
7565 unchain_marker (XMARKER (oldzv));
7566
7567 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7568 set_buffer_internal (oldbuf);
7569 if (NILP (tem))
7570 windows_or_buffers_changed = old_windows_or_buffers_changed;
7571 message_log_need_newline = !nlflag;
7572 Vdeactivate_mark = old_deactivate_mark;
7573 }
7574 }
7575
7576
7577 /* We are at the end of the buffer after just having inserted a newline.
7578 (Note: We depend on the fact we won't be crossing the gap.)
7579 Check to see if the most recent message looks a lot like the previous one.
7580 Return 0 if different, 1 if the new one should just replace it, or a
7581 value N > 1 if we should also append " [N times]". */
7582
7583 static int
7584 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7585 int prev_bol, this_bol;
7586 int prev_bol_byte, this_bol_byte;
7587 {
7588 int i;
7589 int len = Z_BYTE - 1 - this_bol_byte;
7590 int seen_dots = 0;
7591 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7592 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7593
7594 for (i = 0; i < len; i++)
7595 {
7596 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7597 seen_dots = 1;
7598 if (p1[i] != p2[i])
7599 return seen_dots;
7600 }
7601 p1 += len;
7602 if (*p1 == '\n')
7603 return 2;
7604 if (*p1++ == ' ' && *p1++ == '[')
7605 {
7606 int n = 0;
7607 while (*p1 >= '0' && *p1 <= '9')
7608 n = n * 10 + *p1++ - '0';
7609 if (strncmp (p1, " times]\n", 8) == 0)
7610 return n+1;
7611 }
7612 return 0;
7613 }
7614 \f
7615
7616 /* Display an echo area message M with a specified length of NBYTES
7617 bytes. The string may include null characters. If M is 0, clear
7618 out any existing message, and let the mini-buffer text show
7619 through.
7620
7621 This may GC, so the buffer M must NOT point to a Lisp string. */
7622
7623 void
7624 message2 (m, nbytes, multibyte)
7625 const char *m;
7626 int nbytes;
7627 int multibyte;
7628 {
7629 /* First flush out any partial line written with print. */
7630 message_log_maybe_newline ();
7631 if (m)
7632 message_dolog (m, nbytes, 1, multibyte);
7633 message2_nolog (m, nbytes, multibyte);
7634 }
7635
7636
7637 /* The non-logging counterpart of message2. */
7638
7639 void
7640 message2_nolog (m, nbytes, multibyte)
7641 const char *m;
7642 int nbytes, multibyte;
7643 {
7644 struct frame *sf = SELECTED_FRAME ();
7645 message_enable_multibyte = multibyte;
7646
7647 if (noninteractive)
7648 {
7649 if (noninteractive_need_newline)
7650 putc ('\n', stderr);
7651 noninteractive_need_newline = 0;
7652 if (m)
7653 fwrite (m, nbytes, 1, stderr);
7654 if (cursor_in_echo_area == 0)
7655 fprintf (stderr, "\n");
7656 fflush (stderr);
7657 }
7658 /* A null message buffer means that the frame hasn't really been
7659 initialized yet. Error messages get reported properly by
7660 cmd_error, so this must be just an informative message; toss it. */
7661 else if (INTERACTIVE
7662 && sf->glyphs_initialized_p
7663 && FRAME_MESSAGE_BUF (sf))
7664 {
7665 Lisp_Object mini_window;
7666 struct frame *f;
7667
7668 /* Get the frame containing the mini-buffer
7669 that the selected frame is using. */
7670 mini_window = FRAME_MINIBUF_WINDOW (sf);
7671 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7672
7673 FRAME_SAMPLE_VISIBILITY (f);
7674 if (FRAME_VISIBLE_P (sf)
7675 && ! FRAME_VISIBLE_P (f))
7676 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7677
7678 if (m)
7679 {
7680 set_message (m, Qnil, nbytes, multibyte);
7681 if (minibuffer_auto_raise)
7682 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7683 }
7684 else
7685 clear_message (1, 1);
7686
7687 do_pending_window_change (0);
7688 echo_area_display (1);
7689 do_pending_window_change (0);
7690 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7691 (*frame_up_to_date_hook) (f);
7692 }
7693 }
7694
7695
7696 /* Display an echo area message M with a specified length of NBYTES
7697 bytes. The string may include null characters. If M is not a
7698 string, clear out any existing message, and let the mini-buffer
7699 text show through.
7700
7701 This function cancels echoing. */
7702
7703 void
7704 message3 (m, nbytes, multibyte)
7705 Lisp_Object m;
7706 int nbytes;
7707 int multibyte;
7708 {
7709 struct gcpro gcpro1;
7710
7711 GCPRO1 (m);
7712 clear_message (1,1);
7713 cancel_echoing ();
7714
7715 /* First flush out any partial line written with print. */
7716 message_log_maybe_newline ();
7717 if (STRINGP (m))
7718 {
7719 char *buffer;
7720 USE_SAFE_ALLOCA;
7721
7722 SAFE_ALLOCA (buffer, char *, nbytes);
7723 bcopy (SDATA (m), buffer, nbytes);
7724 message_dolog (buffer, nbytes, 1, multibyte);
7725 SAFE_FREE ();
7726 }
7727 message3_nolog (m, nbytes, multibyte);
7728
7729 UNGCPRO;
7730 }
7731
7732
7733 /* The non-logging version of message3.
7734 This does not cancel echoing, because it is used for echoing.
7735 Perhaps we need to make a separate function for echoing
7736 and make this cancel echoing. */
7737
7738 void
7739 message3_nolog (m, nbytes, multibyte)
7740 Lisp_Object m;
7741 int nbytes, multibyte;
7742 {
7743 struct frame *sf = SELECTED_FRAME ();
7744 message_enable_multibyte = multibyte;
7745
7746 if (noninteractive)
7747 {
7748 if (noninteractive_need_newline)
7749 putc ('\n', stderr);
7750 noninteractive_need_newline = 0;
7751 if (STRINGP (m))
7752 fwrite (SDATA (m), nbytes, 1, stderr);
7753 if (cursor_in_echo_area == 0)
7754 fprintf (stderr, "\n");
7755 fflush (stderr);
7756 }
7757 /* A null message buffer means that the frame hasn't really been
7758 initialized yet. Error messages get reported properly by
7759 cmd_error, so this must be just an informative message; toss it. */
7760 else if (INTERACTIVE
7761 && sf->glyphs_initialized_p
7762 && FRAME_MESSAGE_BUF (sf))
7763 {
7764 Lisp_Object mini_window;
7765 Lisp_Object frame;
7766 struct frame *f;
7767
7768 /* Get the frame containing the mini-buffer
7769 that the selected frame is using. */
7770 mini_window = FRAME_MINIBUF_WINDOW (sf);
7771 frame = XWINDOW (mini_window)->frame;
7772 f = XFRAME (frame);
7773
7774 FRAME_SAMPLE_VISIBILITY (f);
7775 if (FRAME_VISIBLE_P (sf)
7776 && !FRAME_VISIBLE_P (f))
7777 Fmake_frame_visible (frame);
7778
7779 if (STRINGP (m) && SCHARS (m) > 0)
7780 {
7781 set_message (NULL, m, nbytes, multibyte);
7782 if (minibuffer_auto_raise)
7783 Fraise_frame (frame);
7784 /* Assume we are not echoing.
7785 (If we are, echo_now will override this.) */
7786 echo_message_buffer = Qnil;
7787 }
7788 else
7789 clear_message (1, 1);
7790
7791 do_pending_window_change (0);
7792 echo_area_display (1);
7793 do_pending_window_change (0);
7794 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7795 (*frame_up_to_date_hook) (f);
7796 }
7797 }
7798
7799
7800 /* Display a null-terminated echo area message M. If M is 0, clear
7801 out any existing message, and let the mini-buffer text show through.
7802
7803 The buffer M must continue to exist until after the echo area gets
7804 cleared or some other message gets displayed there. Do not pass
7805 text that is stored in a Lisp string. Do not pass text in a buffer
7806 that was alloca'd. */
7807
7808 void
7809 message1 (m)
7810 char *m;
7811 {
7812 message2 (m, (m ? strlen (m) : 0), 0);
7813 }
7814
7815
7816 /* The non-logging counterpart of message1. */
7817
7818 void
7819 message1_nolog (m)
7820 char *m;
7821 {
7822 message2_nolog (m, (m ? strlen (m) : 0), 0);
7823 }
7824
7825 /* Display a message M which contains a single %s
7826 which gets replaced with STRING. */
7827
7828 void
7829 message_with_string (m, string, log)
7830 char *m;
7831 Lisp_Object string;
7832 int log;
7833 {
7834 CHECK_STRING (string);
7835
7836 if (noninteractive)
7837 {
7838 if (m)
7839 {
7840 if (noninteractive_need_newline)
7841 putc ('\n', stderr);
7842 noninteractive_need_newline = 0;
7843 fprintf (stderr, m, SDATA (string));
7844 if (cursor_in_echo_area == 0)
7845 fprintf (stderr, "\n");
7846 fflush (stderr);
7847 }
7848 }
7849 else if (INTERACTIVE)
7850 {
7851 /* The frame whose minibuffer we're going to display the message on.
7852 It may be larger than the selected frame, so we need
7853 to use its buffer, not the selected frame's buffer. */
7854 Lisp_Object mini_window;
7855 struct frame *f, *sf = SELECTED_FRAME ();
7856
7857 /* Get the frame containing the minibuffer
7858 that the selected frame is using. */
7859 mini_window = FRAME_MINIBUF_WINDOW (sf);
7860 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7861
7862 /* A null message buffer means that the frame hasn't really been
7863 initialized yet. Error messages get reported properly by
7864 cmd_error, so this must be just an informative message; toss it. */
7865 if (FRAME_MESSAGE_BUF (f))
7866 {
7867 Lisp_Object args[2], message;
7868 struct gcpro gcpro1, gcpro2;
7869
7870 args[0] = build_string (m);
7871 args[1] = message = string;
7872 GCPRO2 (args[0], message);
7873 gcpro1.nvars = 2;
7874
7875 message = Fformat (2, args);
7876
7877 if (log)
7878 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7879 else
7880 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7881
7882 UNGCPRO;
7883
7884 /* Print should start at the beginning of the message
7885 buffer next time. */
7886 message_buf_print = 0;
7887 }
7888 }
7889 }
7890
7891
7892 /* Dump an informative message to the minibuf. If M is 0, clear out
7893 any existing message, and let the mini-buffer text show through. */
7894
7895 /* VARARGS 1 */
7896 void
7897 message (m, a1, a2, a3)
7898 char *m;
7899 EMACS_INT a1, a2, a3;
7900 {
7901 if (noninteractive)
7902 {
7903 if (m)
7904 {
7905 if (noninteractive_need_newline)
7906 putc ('\n', stderr);
7907 noninteractive_need_newline = 0;
7908 fprintf (stderr, m, a1, a2, a3);
7909 if (cursor_in_echo_area == 0)
7910 fprintf (stderr, "\n");
7911 fflush (stderr);
7912 }
7913 }
7914 else if (INTERACTIVE)
7915 {
7916 /* The frame whose mini-buffer we're going to display the message
7917 on. It may be larger than the selected frame, so we need to
7918 use its buffer, not the selected frame's buffer. */
7919 Lisp_Object mini_window;
7920 struct frame *f, *sf = SELECTED_FRAME ();
7921
7922 /* Get the frame containing the mini-buffer
7923 that the selected frame is using. */
7924 mini_window = FRAME_MINIBUF_WINDOW (sf);
7925 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7926
7927 /* A null message buffer means that the frame hasn't really been
7928 initialized yet. Error messages get reported properly by
7929 cmd_error, so this must be just an informative message; toss
7930 it. */
7931 if (FRAME_MESSAGE_BUF (f))
7932 {
7933 if (m)
7934 {
7935 int len;
7936 #ifdef NO_ARG_ARRAY
7937 char *a[3];
7938 a[0] = (char *) a1;
7939 a[1] = (char *) a2;
7940 a[2] = (char *) a3;
7941
7942 len = doprnt (FRAME_MESSAGE_BUF (f),
7943 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7944 #else
7945 len = doprnt (FRAME_MESSAGE_BUF (f),
7946 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7947 (char **) &a1);
7948 #endif /* NO_ARG_ARRAY */
7949
7950 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7951 }
7952 else
7953 message1 (0);
7954
7955 /* Print should start at the beginning of the message
7956 buffer next time. */
7957 message_buf_print = 0;
7958 }
7959 }
7960 }
7961
7962
7963 /* The non-logging version of message. */
7964
7965 void
7966 message_nolog (m, a1, a2, a3)
7967 char *m;
7968 EMACS_INT a1, a2, a3;
7969 {
7970 Lisp_Object old_log_max;
7971 old_log_max = Vmessage_log_max;
7972 Vmessage_log_max = Qnil;
7973 message (m, a1, a2, a3);
7974 Vmessage_log_max = old_log_max;
7975 }
7976
7977
7978 /* Display the current message in the current mini-buffer. This is
7979 only called from error handlers in process.c, and is not time
7980 critical. */
7981
7982 void
7983 update_echo_area ()
7984 {
7985 if (!NILP (echo_area_buffer[0]))
7986 {
7987 Lisp_Object string;
7988 string = Fcurrent_message ();
7989 message3 (string, SBYTES (string),
7990 !NILP (current_buffer->enable_multibyte_characters));
7991 }
7992 }
7993
7994
7995 /* Make sure echo area buffers in `echo_buffers' are live.
7996 If they aren't, make new ones. */
7997
7998 static void
7999 ensure_echo_area_buffers ()
8000 {
8001 int i;
8002
8003 for (i = 0; i < 2; ++i)
8004 if (!BUFFERP (echo_buffer[i])
8005 || NILP (XBUFFER (echo_buffer[i])->name))
8006 {
8007 char name[30];
8008 Lisp_Object old_buffer;
8009 int j;
8010
8011 old_buffer = echo_buffer[i];
8012 sprintf (name, " *Echo Area %d*", i);
8013 echo_buffer[i] = Fget_buffer_create (build_string (name));
8014 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8015
8016 for (j = 0; j < 2; ++j)
8017 if (EQ (old_buffer, echo_area_buffer[j]))
8018 echo_area_buffer[j] = echo_buffer[i];
8019 }
8020 }
8021
8022
8023 /* Call FN with args A1..A4 with either the current or last displayed
8024 echo_area_buffer as current buffer.
8025
8026 WHICH zero means use the current message buffer
8027 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8028 from echo_buffer[] and clear it.
8029
8030 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8031 suitable buffer from echo_buffer[] and clear it.
8032
8033 Value is what FN returns. */
8034
8035 static int
8036 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8037 struct window *w;
8038 int which;
8039 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8040 EMACS_INT a1;
8041 Lisp_Object a2;
8042 EMACS_INT a3, a4;
8043 {
8044 Lisp_Object buffer;
8045 int this_one, the_other, clear_buffer_p, rc;
8046 int count = SPECPDL_INDEX ();
8047
8048 /* If buffers aren't live, make new ones. */
8049 ensure_echo_area_buffers ();
8050
8051 clear_buffer_p = 0;
8052
8053 if (which == 0)
8054 this_one = 0, the_other = 1;
8055 else if (which > 0)
8056 this_one = 1, the_other = 0;
8057
8058 /* Choose a suitable buffer from echo_buffer[] is we don't
8059 have one. */
8060 if (NILP (echo_area_buffer[this_one]))
8061 {
8062 echo_area_buffer[this_one]
8063 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8064 ? echo_buffer[the_other]
8065 : echo_buffer[this_one]);
8066 clear_buffer_p = 1;
8067 }
8068
8069 buffer = echo_area_buffer[this_one];
8070
8071 /* Don't get confused by reusing the buffer used for echoing
8072 for a different purpose. */
8073 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8074 cancel_echoing ();
8075
8076 record_unwind_protect (unwind_with_echo_area_buffer,
8077 with_echo_area_buffer_unwind_data (w));
8078
8079 /* Make the echo area buffer current. Note that for display
8080 purposes, it is not necessary that the displayed window's buffer
8081 == current_buffer, except for text property lookup. So, let's
8082 only set that buffer temporarily here without doing a full
8083 Fset_window_buffer. We must also change w->pointm, though,
8084 because otherwise an assertions in unshow_buffer fails, and Emacs
8085 aborts. */
8086 set_buffer_internal_1 (XBUFFER (buffer));
8087 if (w)
8088 {
8089 w->buffer = buffer;
8090 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8091 }
8092
8093 current_buffer->undo_list = Qt;
8094 current_buffer->read_only = Qnil;
8095 specbind (Qinhibit_read_only, Qt);
8096 specbind (Qinhibit_modification_hooks, Qt);
8097
8098 if (clear_buffer_p && Z > BEG)
8099 del_range (BEG, Z);
8100
8101 xassert (BEGV >= BEG);
8102 xassert (ZV <= Z && ZV >= BEGV);
8103
8104 rc = fn (a1, a2, a3, a4);
8105
8106 xassert (BEGV >= BEG);
8107 xassert (ZV <= Z && ZV >= BEGV);
8108
8109 unbind_to (count, Qnil);
8110 return rc;
8111 }
8112
8113
8114 /* Save state that should be preserved around the call to the function
8115 FN called in with_echo_area_buffer. */
8116
8117 static Lisp_Object
8118 with_echo_area_buffer_unwind_data (w)
8119 struct window *w;
8120 {
8121 int i = 0;
8122 Lisp_Object vector;
8123
8124 /* Reduce consing by keeping one vector in
8125 Vwith_echo_area_save_vector. */
8126 vector = Vwith_echo_area_save_vector;
8127 Vwith_echo_area_save_vector = Qnil;
8128
8129 if (NILP (vector))
8130 vector = Fmake_vector (make_number (7), Qnil);
8131
8132 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8133 AREF (vector, i) = Vdeactivate_mark, ++i;
8134 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8135
8136 if (w)
8137 {
8138 XSETWINDOW (AREF (vector, i), w); ++i;
8139 AREF (vector, i) = w->buffer; ++i;
8140 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8141 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8142 }
8143 else
8144 {
8145 int end = i + 4;
8146 for (; i < end; ++i)
8147 AREF (vector, i) = Qnil;
8148 }
8149
8150 xassert (i == ASIZE (vector));
8151 return vector;
8152 }
8153
8154
8155 /* Restore global state from VECTOR which was created by
8156 with_echo_area_buffer_unwind_data. */
8157
8158 static Lisp_Object
8159 unwind_with_echo_area_buffer (vector)
8160 Lisp_Object vector;
8161 {
8162 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8163 Vdeactivate_mark = AREF (vector, 1);
8164 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8165
8166 if (WINDOWP (AREF (vector, 3)))
8167 {
8168 struct window *w;
8169 Lisp_Object buffer, charpos, bytepos;
8170
8171 w = XWINDOW (AREF (vector, 3));
8172 buffer = AREF (vector, 4);
8173 charpos = AREF (vector, 5);
8174 bytepos = AREF (vector, 6);
8175
8176 w->buffer = buffer;
8177 set_marker_both (w->pointm, buffer,
8178 XFASTINT (charpos), XFASTINT (bytepos));
8179 }
8180
8181 Vwith_echo_area_save_vector = vector;
8182 return Qnil;
8183 }
8184
8185
8186 /* Set up the echo area for use by print functions. MULTIBYTE_P
8187 non-zero means we will print multibyte. */
8188
8189 void
8190 setup_echo_area_for_printing (multibyte_p)
8191 int multibyte_p;
8192 {
8193 /* If we can't find an echo area any more, exit. */
8194 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8195 Fkill_emacs (Qnil);
8196
8197 ensure_echo_area_buffers ();
8198
8199 if (!message_buf_print)
8200 {
8201 /* A message has been output since the last time we printed.
8202 Choose a fresh echo area buffer. */
8203 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8204 echo_area_buffer[0] = echo_buffer[1];
8205 else
8206 echo_area_buffer[0] = echo_buffer[0];
8207
8208 /* Switch to that buffer and clear it. */
8209 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8210 current_buffer->truncate_lines = Qnil;
8211
8212 if (Z > BEG)
8213 {
8214 int count = SPECPDL_INDEX ();
8215 specbind (Qinhibit_read_only, Qt);
8216 /* Note that undo recording is always disabled. */
8217 del_range (BEG, Z);
8218 unbind_to (count, Qnil);
8219 }
8220 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8221
8222 /* Set up the buffer for the multibyteness we need. */
8223 if (multibyte_p
8224 != !NILP (current_buffer->enable_multibyte_characters))
8225 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8226
8227 /* Raise the frame containing the echo area. */
8228 if (minibuffer_auto_raise)
8229 {
8230 struct frame *sf = SELECTED_FRAME ();
8231 Lisp_Object mini_window;
8232 mini_window = FRAME_MINIBUF_WINDOW (sf);
8233 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8234 }
8235
8236 message_log_maybe_newline ();
8237 message_buf_print = 1;
8238 }
8239 else
8240 {
8241 if (NILP (echo_area_buffer[0]))
8242 {
8243 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8244 echo_area_buffer[0] = echo_buffer[1];
8245 else
8246 echo_area_buffer[0] = echo_buffer[0];
8247 }
8248
8249 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8250 {
8251 /* Someone switched buffers between print requests. */
8252 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8253 current_buffer->truncate_lines = Qnil;
8254 }
8255 }
8256 }
8257
8258
8259 /* Display an echo area message in window W. Value is non-zero if W's
8260 height is changed. If display_last_displayed_message_p is
8261 non-zero, display the message that was last displayed, otherwise
8262 display the current message. */
8263
8264 static int
8265 display_echo_area (w)
8266 struct window *w;
8267 {
8268 int i, no_message_p, window_height_changed_p, count;
8269
8270 /* Temporarily disable garbage collections while displaying the echo
8271 area. This is done because a GC can print a message itself.
8272 That message would modify the echo area buffer's contents while a
8273 redisplay of the buffer is going on, and seriously confuse
8274 redisplay. */
8275 count = inhibit_garbage_collection ();
8276
8277 /* If there is no message, we must call display_echo_area_1
8278 nevertheless because it resizes the window. But we will have to
8279 reset the echo_area_buffer in question to nil at the end because
8280 with_echo_area_buffer will sets it to an empty buffer. */
8281 i = display_last_displayed_message_p ? 1 : 0;
8282 no_message_p = NILP (echo_area_buffer[i]);
8283
8284 window_height_changed_p
8285 = with_echo_area_buffer (w, display_last_displayed_message_p,
8286 display_echo_area_1,
8287 (EMACS_INT) w, Qnil, 0, 0);
8288
8289 if (no_message_p)
8290 echo_area_buffer[i] = Qnil;
8291
8292 unbind_to (count, Qnil);
8293 return window_height_changed_p;
8294 }
8295
8296
8297 /* Helper for display_echo_area. Display the current buffer which
8298 contains the current echo area message in window W, a mini-window,
8299 a pointer to which is passed in A1. A2..A4 are currently not used.
8300 Change the height of W so that all of the message is displayed.
8301 Value is non-zero if height of W was changed. */
8302
8303 static int
8304 display_echo_area_1 (a1, a2, a3, a4)
8305 EMACS_INT a1;
8306 Lisp_Object a2;
8307 EMACS_INT a3, a4;
8308 {
8309 struct window *w = (struct window *) a1;
8310 Lisp_Object window;
8311 struct text_pos start;
8312 int window_height_changed_p = 0;
8313
8314 /* Do this before displaying, so that we have a large enough glyph
8315 matrix for the display. If we can't get enough space for the
8316 whole text, display the last N lines. That works by setting w->start. */
8317 window_height_changed_p = resize_mini_window (w, 0);
8318
8319 /* Use the starting position chosen by resize_mini_window. */
8320 SET_TEXT_POS_FROM_MARKER (start, w->start);
8321
8322 /* Display. */
8323 clear_glyph_matrix (w->desired_matrix);
8324 XSETWINDOW (window, w);
8325 try_window (window, start, 0);
8326
8327 return window_height_changed_p;
8328 }
8329
8330
8331 /* Resize the echo area window to exactly the size needed for the
8332 currently displayed message, if there is one. If a mini-buffer
8333 is active, don't shrink it. */
8334
8335 void
8336 resize_echo_area_exactly ()
8337 {
8338 if (BUFFERP (echo_area_buffer[0])
8339 && WINDOWP (echo_area_window))
8340 {
8341 struct window *w = XWINDOW (echo_area_window);
8342 int resized_p;
8343 Lisp_Object resize_exactly;
8344
8345 if (minibuf_level == 0)
8346 resize_exactly = Qt;
8347 else
8348 resize_exactly = Qnil;
8349
8350 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8351 (EMACS_INT) w, resize_exactly, 0, 0);
8352 if (resized_p)
8353 {
8354 ++windows_or_buffers_changed;
8355 ++update_mode_lines;
8356 redisplay_internal (0);
8357 }
8358 }
8359 }
8360
8361
8362 /* Callback function for with_echo_area_buffer, when used from
8363 resize_echo_area_exactly. A1 contains a pointer to the window to
8364 resize, EXACTLY non-nil means resize the mini-window exactly to the
8365 size of the text displayed. A3 and A4 are not used. Value is what
8366 resize_mini_window returns. */
8367
8368 static int
8369 resize_mini_window_1 (a1, exactly, a3, a4)
8370 EMACS_INT a1;
8371 Lisp_Object exactly;
8372 EMACS_INT a3, a4;
8373 {
8374 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8375 }
8376
8377
8378 /* Resize mini-window W to fit the size of its contents. EXACT:P
8379 means size the window exactly to the size needed. Otherwise, it's
8380 only enlarged until W's buffer is empty.
8381
8382 Set W->start to the right place to begin display. If the whole
8383 contents fit, start at the beginning. Otherwise, start so as
8384 to make the end of the contents appear. This is particularly
8385 important for y-or-n-p, but seems desirable generally.
8386
8387 Value is non-zero if the window height has been changed. */
8388
8389 int
8390 resize_mini_window (w, exact_p)
8391 struct window *w;
8392 int exact_p;
8393 {
8394 struct frame *f = XFRAME (w->frame);
8395 int window_height_changed_p = 0;
8396
8397 xassert (MINI_WINDOW_P (w));
8398
8399 /* By default, start display at the beginning. */
8400 set_marker_both (w->start, w->buffer,
8401 BUF_BEGV (XBUFFER (w->buffer)),
8402 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8403
8404 /* Don't resize windows while redisplaying a window; it would
8405 confuse redisplay functions when the size of the window they are
8406 displaying changes from under them. Such a resizing can happen,
8407 for instance, when which-func prints a long message while
8408 we are running fontification-functions. We're running these
8409 functions with safe_call which binds inhibit-redisplay to t. */
8410 if (!NILP (Vinhibit_redisplay))
8411 return 0;
8412
8413 /* Nil means don't try to resize. */
8414 if (NILP (Vresize_mini_windows)
8415 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8416 return 0;
8417
8418 if (!FRAME_MINIBUF_ONLY_P (f))
8419 {
8420 struct it it;
8421 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8422 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8423 int height, max_height;
8424 int unit = FRAME_LINE_HEIGHT (f);
8425 struct text_pos start;
8426 struct buffer *old_current_buffer = NULL;
8427
8428 if (current_buffer != XBUFFER (w->buffer))
8429 {
8430 old_current_buffer = current_buffer;
8431 set_buffer_internal (XBUFFER (w->buffer));
8432 }
8433
8434 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8435
8436 /* Compute the max. number of lines specified by the user. */
8437 if (FLOATP (Vmax_mini_window_height))
8438 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8439 else if (INTEGERP (Vmax_mini_window_height))
8440 max_height = XINT (Vmax_mini_window_height);
8441 else
8442 max_height = total_height / 4;
8443
8444 /* Correct that max. height if it's bogus. */
8445 max_height = max (1, max_height);
8446 max_height = min (total_height, max_height);
8447
8448 /* Find out the height of the text in the window. */
8449 if (it.truncate_lines_p)
8450 height = 1;
8451 else
8452 {
8453 last_height = 0;
8454 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8455 if (it.max_ascent == 0 && it.max_descent == 0)
8456 height = it.current_y + last_height;
8457 else
8458 height = it.current_y + it.max_ascent + it.max_descent;
8459 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8460 height = (height + unit - 1) / unit;
8461 }
8462
8463 /* Compute a suitable window start. */
8464 if (height > max_height)
8465 {
8466 height = max_height;
8467 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8468 move_it_vertically_backward (&it, (height - 1) * unit);
8469 start = it.current.pos;
8470 }
8471 else
8472 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8473 SET_MARKER_FROM_TEXT_POS (w->start, start);
8474
8475 if (EQ (Vresize_mini_windows, Qgrow_only))
8476 {
8477 /* Let it grow only, until we display an empty message, in which
8478 case the window shrinks again. */
8479 if (height > WINDOW_TOTAL_LINES (w))
8480 {
8481 int old_height = WINDOW_TOTAL_LINES (w);
8482 freeze_window_starts (f, 1);
8483 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8484 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8485 }
8486 else if (height < WINDOW_TOTAL_LINES (w)
8487 && (exact_p || BEGV == ZV))
8488 {
8489 int old_height = WINDOW_TOTAL_LINES (w);
8490 freeze_window_starts (f, 0);
8491 shrink_mini_window (w);
8492 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8493 }
8494 }
8495 else
8496 {
8497 /* Always resize to exact size needed. */
8498 if (height > WINDOW_TOTAL_LINES (w))
8499 {
8500 int old_height = WINDOW_TOTAL_LINES (w);
8501 freeze_window_starts (f, 1);
8502 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8503 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8504 }
8505 else if (height < WINDOW_TOTAL_LINES (w))
8506 {
8507 int old_height = WINDOW_TOTAL_LINES (w);
8508 freeze_window_starts (f, 0);
8509 shrink_mini_window (w);
8510
8511 if (height)
8512 {
8513 freeze_window_starts (f, 1);
8514 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8515 }
8516
8517 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8518 }
8519 }
8520
8521 if (old_current_buffer)
8522 set_buffer_internal (old_current_buffer);
8523 }
8524
8525 return window_height_changed_p;
8526 }
8527
8528
8529 /* Value is the current message, a string, or nil if there is no
8530 current message. */
8531
8532 Lisp_Object
8533 current_message ()
8534 {
8535 Lisp_Object msg;
8536
8537 if (NILP (echo_area_buffer[0]))
8538 msg = Qnil;
8539 else
8540 {
8541 with_echo_area_buffer (0, 0, current_message_1,
8542 (EMACS_INT) &msg, Qnil, 0, 0);
8543 if (NILP (msg))
8544 echo_area_buffer[0] = Qnil;
8545 }
8546
8547 return msg;
8548 }
8549
8550
8551 static int
8552 current_message_1 (a1, a2, a3, a4)
8553 EMACS_INT a1;
8554 Lisp_Object a2;
8555 EMACS_INT a3, a4;
8556 {
8557 Lisp_Object *msg = (Lisp_Object *) a1;
8558
8559 if (Z > BEG)
8560 *msg = make_buffer_string (BEG, Z, 1);
8561 else
8562 *msg = Qnil;
8563 return 0;
8564 }
8565
8566
8567 /* Push the current message on Vmessage_stack for later restauration
8568 by restore_message. Value is non-zero if the current message isn't
8569 empty. This is a relatively infrequent operation, so it's not
8570 worth optimizing. */
8571
8572 int
8573 push_message ()
8574 {
8575 Lisp_Object msg;
8576 msg = current_message ();
8577 Vmessage_stack = Fcons (msg, Vmessage_stack);
8578 return STRINGP (msg);
8579 }
8580
8581
8582 /* Restore message display from the top of Vmessage_stack. */
8583
8584 void
8585 restore_message ()
8586 {
8587 Lisp_Object msg;
8588
8589 xassert (CONSP (Vmessage_stack));
8590 msg = XCAR (Vmessage_stack);
8591 if (STRINGP (msg))
8592 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8593 else
8594 message3_nolog (msg, 0, 0);
8595 }
8596
8597
8598 /* Handler for record_unwind_protect calling pop_message. */
8599
8600 Lisp_Object
8601 pop_message_unwind (dummy)
8602 Lisp_Object dummy;
8603 {
8604 pop_message ();
8605 return Qnil;
8606 }
8607
8608 /* Pop the top-most entry off Vmessage_stack. */
8609
8610 void
8611 pop_message ()
8612 {
8613 xassert (CONSP (Vmessage_stack));
8614 Vmessage_stack = XCDR (Vmessage_stack);
8615 }
8616
8617
8618 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8619 exits. If the stack is not empty, we have a missing pop_message
8620 somewhere. */
8621
8622 void
8623 check_message_stack ()
8624 {
8625 if (!NILP (Vmessage_stack))
8626 abort ();
8627 }
8628
8629
8630 /* Truncate to NCHARS what will be displayed in the echo area the next
8631 time we display it---but don't redisplay it now. */
8632
8633 void
8634 truncate_echo_area (nchars)
8635 int nchars;
8636 {
8637 if (nchars == 0)
8638 echo_area_buffer[0] = Qnil;
8639 /* A null message buffer means that the frame hasn't really been
8640 initialized yet. Error messages get reported properly by
8641 cmd_error, so this must be just an informative message; toss it. */
8642 else if (!noninteractive
8643 && INTERACTIVE
8644 && !NILP (echo_area_buffer[0]))
8645 {
8646 struct frame *sf = SELECTED_FRAME ();
8647 if (FRAME_MESSAGE_BUF (sf))
8648 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8649 }
8650 }
8651
8652
8653 /* Helper function for truncate_echo_area. Truncate the current
8654 message to at most NCHARS characters. */
8655
8656 static int
8657 truncate_message_1 (nchars, a2, a3, a4)
8658 EMACS_INT nchars;
8659 Lisp_Object a2;
8660 EMACS_INT a3, a4;
8661 {
8662 if (BEG + nchars < Z)
8663 del_range (BEG + nchars, Z);
8664 if (Z == BEG)
8665 echo_area_buffer[0] = Qnil;
8666 return 0;
8667 }
8668
8669
8670 /* Set the current message to a substring of S or STRING.
8671
8672 If STRING is a Lisp string, set the message to the first NBYTES
8673 bytes from STRING. NBYTES zero means use the whole string. If
8674 STRING is multibyte, the message will be displayed multibyte.
8675
8676 If S is not null, set the message to the first LEN bytes of S. LEN
8677 zero means use the whole string. MULTIBYTE_P non-zero means S is
8678 multibyte. Display the message multibyte in that case.
8679
8680 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8681 to t before calling set_message_1 (which calls insert).
8682 */
8683
8684 void
8685 set_message (s, string, nbytes, multibyte_p)
8686 const char *s;
8687 Lisp_Object string;
8688 int nbytes, multibyte_p;
8689 {
8690 message_enable_multibyte
8691 = ((s && multibyte_p)
8692 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8693
8694 with_echo_area_buffer (0, 0, set_message_1,
8695 (EMACS_INT) s, string, nbytes, multibyte_p);
8696 message_buf_print = 0;
8697 help_echo_showing_p = 0;
8698 }
8699
8700
8701 /* Helper function for set_message. Arguments have the same meaning
8702 as there, with A1 corresponding to S and A2 corresponding to STRING
8703 This function is called with the echo area buffer being
8704 current. */
8705
8706 static int
8707 set_message_1 (a1, a2, nbytes, multibyte_p)
8708 EMACS_INT a1;
8709 Lisp_Object a2;
8710 EMACS_INT nbytes, multibyte_p;
8711 {
8712 const char *s = (const char *) a1;
8713 Lisp_Object string = a2;
8714
8715 /* Change multibyteness of the echo buffer appropriately. */
8716 if (message_enable_multibyte
8717 != !NILP (current_buffer->enable_multibyte_characters))
8718 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8719
8720 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8721
8722 /* Insert new message at BEG. */
8723 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8724 Ferase_buffer ();
8725
8726 if (STRINGP (string))
8727 {
8728 int nchars;
8729
8730 if (nbytes == 0)
8731 nbytes = SBYTES (string);
8732 nchars = string_byte_to_char (string, nbytes);
8733
8734 /* This function takes care of single/multibyte conversion. We
8735 just have to ensure that the echo area buffer has the right
8736 setting of enable_multibyte_characters. */
8737 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8738 }
8739 else if (s)
8740 {
8741 if (nbytes == 0)
8742 nbytes = strlen (s);
8743
8744 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8745 {
8746 /* Convert from multi-byte to single-byte. */
8747 int i, c, n;
8748 unsigned char work[1];
8749
8750 /* Convert a multibyte string to single-byte. */
8751 for (i = 0; i < nbytes; i += n)
8752 {
8753 c = string_char_and_length (s + i, nbytes - i, &n);
8754 work[0] = (ASCII_CHAR_P (c)
8755 ? c
8756 : multibyte_char_to_unibyte (c, Qnil));
8757 insert_1_both (work, 1, 1, 1, 0, 0);
8758 }
8759 }
8760 else if (!multibyte_p
8761 && !NILP (current_buffer->enable_multibyte_characters))
8762 {
8763 /* Convert from single-byte to multi-byte. */
8764 int i, c, n;
8765 const unsigned char *msg = (const unsigned char *) s;
8766 unsigned char str[MAX_MULTIBYTE_LENGTH];
8767
8768 /* Convert a single-byte string to multibyte. */
8769 for (i = 0; i < nbytes; i++)
8770 {
8771 c = msg[i];
8772 c = unibyte_char_to_multibyte (c);
8773 n = CHAR_STRING (c, str);
8774 insert_1_both (str, 1, n, 1, 0, 0);
8775 }
8776 }
8777 else
8778 insert_1 (s, nbytes, 1, 0, 0);
8779 }
8780
8781 return 0;
8782 }
8783
8784
8785 /* Clear messages. CURRENT_P non-zero means clear the current
8786 message. LAST_DISPLAYED_P non-zero means clear the message
8787 last displayed. */
8788
8789 void
8790 clear_message (current_p, last_displayed_p)
8791 int current_p, last_displayed_p;
8792 {
8793 if (current_p)
8794 {
8795 echo_area_buffer[0] = Qnil;
8796 message_cleared_p = 1;
8797 }
8798
8799 if (last_displayed_p)
8800 echo_area_buffer[1] = Qnil;
8801
8802 message_buf_print = 0;
8803 }
8804
8805 /* Clear garbaged frames.
8806
8807 This function is used where the old redisplay called
8808 redraw_garbaged_frames which in turn called redraw_frame which in
8809 turn called clear_frame. The call to clear_frame was a source of
8810 flickering. I believe a clear_frame is not necessary. It should
8811 suffice in the new redisplay to invalidate all current matrices,
8812 and ensure a complete redisplay of all windows. */
8813
8814 static void
8815 clear_garbaged_frames ()
8816 {
8817 if (frame_garbaged)
8818 {
8819 Lisp_Object tail, frame;
8820 int changed_count = 0;
8821
8822 FOR_EACH_FRAME (tail, frame)
8823 {
8824 struct frame *f = XFRAME (frame);
8825
8826 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8827 {
8828 if (f->resized_p)
8829 {
8830 Fredraw_frame (frame);
8831 f->force_flush_display_p = 1;
8832 }
8833 clear_current_matrices (f);
8834 changed_count++;
8835 f->garbaged = 0;
8836 f->resized_p = 0;
8837 }
8838 }
8839
8840 frame_garbaged = 0;
8841 if (changed_count)
8842 ++windows_or_buffers_changed;
8843 }
8844 }
8845
8846
8847 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8848 is non-zero update selected_frame. Value is non-zero if the
8849 mini-windows height has been changed. */
8850
8851 static int
8852 echo_area_display (update_frame_p)
8853 int update_frame_p;
8854 {
8855 Lisp_Object mini_window;
8856 struct window *w;
8857 struct frame *f;
8858 int window_height_changed_p = 0;
8859 struct frame *sf = SELECTED_FRAME ();
8860
8861 mini_window = FRAME_MINIBUF_WINDOW (sf);
8862 w = XWINDOW (mini_window);
8863 f = XFRAME (WINDOW_FRAME (w));
8864
8865 /* Don't display if frame is invisible or not yet initialized. */
8866 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8867 return 0;
8868
8869 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8870 #ifndef MAC_OS8
8871 #ifdef HAVE_WINDOW_SYSTEM
8872 /* When Emacs starts, selected_frame may be a visible terminal
8873 frame, even if we run under a window system. If we let this
8874 through, a message would be displayed on the terminal. */
8875 if (EQ (selected_frame, Vterminal_frame)
8876 && !NILP (Vwindow_system))
8877 return 0;
8878 #endif /* HAVE_WINDOW_SYSTEM */
8879 #endif
8880
8881 /* Redraw garbaged frames. */
8882 if (frame_garbaged)
8883 clear_garbaged_frames ();
8884
8885 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8886 {
8887 echo_area_window = mini_window;
8888 window_height_changed_p = display_echo_area (w);
8889 w->must_be_updated_p = 1;
8890
8891 /* Update the display, unless called from redisplay_internal.
8892 Also don't update the screen during redisplay itself. The
8893 update will happen at the end of redisplay, and an update
8894 here could cause confusion. */
8895 if (update_frame_p && !redisplaying_p)
8896 {
8897 int n = 0;
8898
8899 /* If the display update has been interrupted by pending
8900 input, update mode lines in the frame. Due to the
8901 pending input, it might have been that redisplay hasn't
8902 been called, so that mode lines above the echo area are
8903 garbaged. This looks odd, so we prevent it here. */
8904 if (!display_completed)
8905 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8906
8907 if (window_height_changed_p
8908 /* Don't do this if Emacs is shutting down. Redisplay
8909 needs to run hooks. */
8910 && !NILP (Vrun_hooks))
8911 {
8912 /* Must update other windows. Likewise as in other
8913 cases, don't let this update be interrupted by
8914 pending input. */
8915 int count = SPECPDL_INDEX ();
8916 specbind (Qredisplay_dont_pause, Qt);
8917 windows_or_buffers_changed = 1;
8918 redisplay_internal (0);
8919 unbind_to (count, Qnil);
8920 }
8921 else if (FRAME_WINDOW_P (f) && n == 0)
8922 {
8923 /* Window configuration is the same as before.
8924 Can do with a display update of the echo area,
8925 unless we displayed some mode lines. */
8926 update_single_window (w, 1);
8927 rif->flush_display (f);
8928 }
8929 else
8930 update_frame (f, 1, 1);
8931
8932 /* If cursor is in the echo area, make sure that the next
8933 redisplay displays the minibuffer, so that the cursor will
8934 be replaced with what the minibuffer wants. */
8935 if (cursor_in_echo_area)
8936 ++windows_or_buffers_changed;
8937 }
8938 }
8939 else if (!EQ (mini_window, selected_window))
8940 windows_or_buffers_changed++;
8941
8942 /* The current message is now also the last one displayed. */
8943 echo_area_buffer[1] = echo_area_buffer[0];
8944
8945 /* Prevent redisplay optimization in redisplay_internal by resetting
8946 this_line_start_pos. This is done because the mini-buffer now
8947 displays the message instead of its buffer text. */
8948 if (EQ (mini_window, selected_window))
8949 CHARPOS (this_line_start_pos) = 0;
8950
8951 return window_height_changed_p;
8952 }
8953
8954
8955 \f
8956 /***********************************************************************
8957 Mode Lines and Frame Titles
8958 ***********************************************************************/
8959
8960 /* A buffer for constructing non-propertized mode-line strings and
8961 frame titles in it; allocated from the heap in init_xdisp and
8962 resized as needed in store_mode_line_noprop_char. */
8963
8964 static char *mode_line_noprop_buf;
8965
8966 /* The buffer's end, and a current output position in it. */
8967
8968 static char *mode_line_noprop_buf_end;
8969 static char *mode_line_noprop_ptr;
8970
8971 #define MODE_LINE_NOPROP_LEN(start) \
8972 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8973
8974 static enum {
8975 MODE_LINE_DISPLAY = 0,
8976 MODE_LINE_TITLE,
8977 MODE_LINE_NOPROP,
8978 MODE_LINE_STRING
8979 } mode_line_target;
8980
8981 /* Alist that caches the results of :propertize.
8982 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8983 static Lisp_Object mode_line_proptrans_alist;
8984
8985 /* List of strings making up the mode-line. */
8986 static Lisp_Object mode_line_string_list;
8987
8988 /* Base face property when building propertized mode line string. */
8989 static Lisp_Object mode_line_string_face;
8990 static Lisp_Object mode_line_string_face_prop;
8991
8992
8993 /* Unwind data for mode line strings */
8994
8995 static Lisp_Object Vmode_line_unwind_vector;
8996
8997 static Lisp_Object
8998 format_mode_line_unwind_data (obuf, save_proptrans)
8999 struct buffer *obuf;
9000 {
9001 Lisp_Object vector;
9002
9003 /* Reduce consing by keeping one vector in
9004 Vwith_echo_area_save_vector. */
9005 vector = Vmode_line_unwind_vector;
9006 Vmode_line_unwind_vector = Qnil;
9007
9008 if (NILP (vector))
9009 vector = Fmake_vector (make_number (7), Qnil);
9010
9011 AREF (vector, 0) = make_number (mode_line_target);
9012 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
9013 AREF (vector, 2) = mode_line_string_list;
9014 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
9015 AREF (vector, 4) = mode_line_string_face;
9016 AREF (vector, 5) = mode_line_string_face_prop;
9017
9018 if (obuf)
9019 XSETBUFFER (AREF (vector, 6), obuf);
9020 else
9021 AREF (vector, 6) = Qnil;
9022
9023 return vector;
9024 }
9025
9026 static Lisp_Object
9027 unwind_format_mode_line (vector)
9028 Lisp_Object vector;
9029 {
9030 mode_line_target = XINT (AREF (vector, 0));
9031 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9032 mode_line_string_list = AREF (vector, 2);
9033 if (! EQ (AREF (vector, 3), Qt))
9034 mode_line_proptrans_alist = AREF (vector, 3);
9035 mode_line_string_face = AREF (vector, 4);
9036 mode_line_string_face_prop = AREF (vector, 5);
9037
9038 if (!NILP (AREF (vector, 6)))
9039 {
9040 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9041 AREF (vector, 6) = Qnil;
9042 }
9043
9044 Vmode_line_unwind_vector = vector;
9045 return Qnil;
9046 }
9047
9048
9049 /* Store a single character C for the frame title in mode_line_noprop_buf.
9050 Re-allocate mode_line_noprop_buf if necessary. */
9051
9052 static void
9053 #ifdef PROTOTYPES
9054 store_mode_line_noprop_char (char c)
9055 #else
9056 store_mode_line_noprop_char (c)
9057 char c;
9058 #endif
9059 {
9060 /* If output position has reached the end of the allocated buffer,
9061 double the buffer's size. */
9062 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9063 {
9064 int len = MODE_LINE_NOPROP_LEN (0);
9065 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9066 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9067 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9068 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9069 }
9070
9071 *mode_line_noprop_ptr++ = c;
9072 }
9073
9074
9075 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9076 mode_line_noprop_ptr. STR is the string to store. Do not copy
9077 characters that yield more columns than PRECISION; PRECISION <= 0
9078 means copy the whole string. Pad with spaces until FIELD_WIDTH
9079 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9080 pad. Called from display_mode_element when it is used to build a
9081 frame title. */
9082
9083 static int
9084 store_mode_line_noprop (str, field_width, precision)
9085 const unsigned char *str;
9086 int field_width, precision;
9087 {
9088 int n = 0;
9089 int dummy, nbytes;
9090
9091 /* Copy at most PRECISION chars from STR. */
9092 nbytes = strlen (str);
9093 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9094 while (nbytes--)
9095 store_mode_line_noprop_char (*str++);
9096
9097 /* Fill up with spaces until FIELD_WIDTH reached. */
9098 while (field_width > 0
9099 && n < field_width)
9100 {
9101 store_mode_line_noprop_char (' ');
9102 ++n;
9103 }
9104
9105 return n;
9106 }
9107
9108 /***********************************************************************
9109 Frame Titles
9110 ***********************************************************************/
9111
9112 #ifdef HAVE_WINDOW_SYSTEM
9113
9114 /* Set the title of FRAME, if it has changed. The title format is
9115 Vicon_title_format if FRAME is iconified, otherwise it is
9116 frame_title_format. */
9117
9118 static void
9119 x_consider_frame_title (frame)
9120 Lisp_Object frame;
9121 {
9122 struct frame *f = XFRAME (frame);
9123
9124 if (FRAME_WINDOW_P (f)
9125 || FRAME_MINIBUF_ONLY_P (f)
9126 || f->explicit_name)
9127 {
9128 /* Do we have more than one visible frame on this X display? */
9129 Lisp_Object tail;
9130 Lisp_Object fmt;
9131 int title_start;
9132 char *title;
9133 int len;
9134 struct it it;
9135 int count = SPECPDL_INDEX ();
9136
9137 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9138 {
9139 Lisp_Object other_frame = XCAR (tail);
9140 struct frame *tf = XFRAME (other_frame);
9141
9142 if (tf != f
9143 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9144 && !FRAME_MINIBUF_ONLY_P (tf)
9145 && !EQ (other_frame, tip_frame)
9146 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9147 break;
9148 }
9149
9150 /* Set global variable indicating that multiple frames exist. */
9151 multiple_frames = CONSP (tail);
9152
9153 /* Switch to the buffer of selected window of the frame. Set up
9154 mode_line_target so that display_mode_element will output into
9155 mode_line_noprop_buf; then display the title. */
9156 record_unwind_protect (unwind_format_mode_line,
9157 format_mode_line_unwind_data (current_buffer, 0));
9158
9159 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9160 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9161
9162 mode_line_target = MODE_LINE_TITLE;
9163 title_start = MODE_LINE_NOPROP_LEN (0);
9164 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9165 NULL, DEFAULT_FACE_ID);
9166 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9167 len = MODE_LINE_NOPROP_LEN (title_start);
9168 title = mode_line_noprop_buf + title_start;
9169 unbind_to (count, Qnil);
9170
9171 /* Set the title only if it's changed. This avoids consing in
9172 the common case where it hasn't. (If it turns out that we've
9173 already wasted too much time by walking through the list with
9174 display_mode_element, then we might need to optimize at a
9175 higher level than this.) */
9176 if (! STRINGP (f->name)
9177 || SBYTES (f->name) != len
9178 || bcmp (title, SDATA (f->name), len) != 0)
9179 x_implicitly_set_name (f, make_string (title, len), Qnil);
9180 }
9181 }
9182
9183 #endif /* not HAVE_WINDOW_SYSTEM */
9184
9185
9186
9187 \f
9188 /***********************************************************************
9189 Menu Bars
9190 ***********************************************************************/
9191
9192
9193 /* Prepare for redisplay by updating menu-bar item lists when
9194 appropriate. This can call eval. */
9195
9196 void
9197 prepare_menu_bars ()
9198 {
9199 int all_windows;
9200 struct gcpro gcpro1, gcpro2;
9201 struct frame *f;
9202 Lisp_Object tooltip_frame;
9203
9204 #ifdef HAVE_WINDOW_SYSTEM
9205 tooltip_frame = tip_frame;
9206 #else
9207 tooltip_frame = Qnil;
9208 #endif
9209
9210 /* Update all frame titles based on their buffer names, etc. We do
9211 this before the menu bars so that the buffer-menu will show the
9212 up-to-date frame titles. */
9213 #ifdef HAVE_WINDOW_SYSTEM
9214 if (windows_or_buffers_changed || update_mode_lines)
9215 {
9216 Lisp_Object tail, frame;
9217
9218 FOR_EACH_FRAME (tail, frame)
9219 {
9220 f = XFRAME (frame);
9221 if (!EQ (frame, tooltip_frame)
9222 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9223 x_consider_frame_title (frame);
9224 }
9225 }
9226 #endif /* HAVE_WINDOW_SYSTEM */
9227
9228 /* Update the menu bar item lists, if appropriate. This has to be
9229 done before any actual redisplay or generation of display lines. */
9230 all_windows = (update_mode_lines
9231 || buffer_shared > 1
9232 || windows_or_buffers_changed);
9233 if (all_windows)
9234 {
9235 Lisp_Object tail, frame;
9236 int count = SPECPDL_INDEX ();
9237 /* 1 means that update_menu_bar has run its hooks
9238 so any further calls to update_menu_bar shouldn't do so again. */
9239 int menu_bar_hooks_run = 0;
9240
9241 record_unwind_save_match_data ();
9242
9243 FOR_EACH_FRAME (tail, frame)
9244 {
9245 f = XFRAME (frame);
9246
9247 /* Ignore tooltip frame. */
9248 if (EQ (frame, tooltip_frame))
9249 continue;
9250
9251 /* If a window on this frame changed size, report that to
9252 the user and clear the size-change flag. */
9253 if (FRAME_WINDOW_SIZES_CHANGED (f))
9254 {
9255 Lisp_Object functions;
9256
9257 /* Clear flag first in case we get an error below. */
9258 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9259 functions = Vwindow_size_change_functions;
9260 GCPRO2 (tail, functions);
9261
9262 while (CONSP (functions))
9263 {
9264 call1 (XCAR (functions), frame);
9265 functions = XCDR (functions);
9266 }
9267 UNGCPRO;
9268 }
9269
9270 GCPRO1 (tail);
9271 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9272 #ifdef HAVE_WINDOW_SYSTEM
9273 update_tool_bar (f, 0);
9274 #ifdef MAC_OS
9275 mac_update_title_bar (f, 0);
9276 #endif
9277 #endif
9278 UNGCPRO;
9279 }
9280
9281 unbind_to (count, Qnil);
9282 }
9283 else
9284 {
9285 struct frame *sf = SELECTED_FRAME ();
9286 update_menu_bar (sf, 1, 0);
9287 #ifdef HAVE_WINDOW_SYSTEM
9288 update_tool_bar (sf, 1);
9289 #ifdef MAC_OS
9290 mac_update_title_bar (sf, 1);
9291 #endif
9292 #endif
9293 }
9294
9295 /* Motif needs this. See comment in xmenu.c. Turn it off when
9296 pending_menu_activation is not defined. */
9297 #ifdef USE_X_TOOLKIT
9298 pending_menu_activation = 0;
9299 #endif
9300 }
9301
9302
9303 /* Update the menu bar item list for frame F. This has to be done
9304 before we start to fill in any display lines, because it can call
9305 eval.
9306
9307 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9308
9309 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9310 already ran the menu bar hooks for this redisplay, so there
9311 is no need to run them again. The return value is the
9312 updated value of this flag, to pass to the next call. */
9313
9314 static int
9315 update_menu_bar (f, save_match_data, hooks_run)
9316 struct frame *f;
9317 int save_match_data;
9318 int hooks_run;
9319 {
9320 Lisp_Object window;
9321 register struct window *w;
9322
9323 /* If called recursively during a menu update, do nothing. This can
9324 happen when, for instance, an activate-menubar-hook causes a
9325 redisplay. */
9326 if (inhibit_menubar_update)
9327 return hooks_run;
9328
9329 window = FRAME_SELECTED_WINDOW (f);
9330 w = XWINDOW (window);
9331
9332 #if 0 /* The if statement below this if statement used to include the
9333 condition !NILP (w->update_mode_line), rather than using
9334 update_mode_lines directly, and this if statement may have
9335 been added to make that condition work. Now the if
9336 statement below matches its comment, this isn't needed. */
9337 if (update_mode_lines)
9338 w->update_mode_line = Qt;
9339 #endif
9340
9341 if (FRAME_WINDOW_P (f)
9342 ?
9343 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9344 || defined (USE_GTK)
9345 FRAME_EXTERNAL_MENU_BAR (f)
9346 #else
9347 FRAME_MENU_BAR_LINES (f) > 0
9348 #endif
9349 : FRAME_MENU_BAR_LINES (f) > 0)
9350 {
9351 /* If the user has switched buffers or windows, we need to
9352 recompute to reflect the new bindings. But we'll
9353 recompute when update_mode_lines is set too; that means
9354 that people can use force-mode-line-update to request
9355 that the menu bar be recomputed. The adverse effect on
9356 the rest of the redisplay algorithm is about the same as
9357 windows_or_buffers_changed anyway. */
9358 if (windows_or_buffers_changed
9359 /* This used to test w->update_mode_line, but we believe
9360 there is no need to recompute the menu in that case. */
9361 || update_mode_lines
9362 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9363 < BUF_MODIFF (XBUFFER (w->buffer)))
9364 != !NILP (w->last_had_star))
9365 || ((!NILP (Vtransient_mark_mode)
9366 && !NILP (XBUFFER (w->buffer)->mark_active))
9367 != !NILP (w->region_showing)))
9368 {
9369 struct buffer *prev = current_buffer;
9370 int count = SPECPDL_INDEX ();
9371
9372 specbind (Qinhibit_menubar_update, Qt);
9373
9374 set_buffer_internal_1 (XBUFFER (w->buffer));
9375 if (save_match_data)
9376 record_unwind_save_match_data ();
9377 if (NILP (Voverriding_local_map_menu_flag))
9378 {
9379 specbind (Qoverriding_terminal_local_map, Qnil);
9380 specbind (Qoverriding_local_map, Qnil);
9381 }
9382
9383 if (!hooks_run)
9384 {
9385 /* Run the Lucid hook. */
9386 safe_run_hooks (Qactivate_menubar_hook);
9387
9388 /* If it has changed current-menubar from previous value,
9389 really recompute the menu-bar from the value. */
9390 if (! NILP (Vlucid_menu_bar_dirty_flag))
9391 call0 (Qrecompute_lucid_menubar);
9392
9393 safe_run_hooks (Qmenu_bar_update_hook);
9394
9395 hooks_run = 1;
9396 }
9397
9398 XSETFRAME (Vmenu_updating_frame, f);
9399 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9400
9401 /* Redisplay the menu bar in case we changed it. */
9402 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9403 || defined (USE_GTK)
9404 if (FRAME_WINDOW_P (f))
9405 {
9406 #ifdef MAC_OS
9407 /* All frames on Mac OS share the same menubar. So only
9408 the selected frame should be allowed to set it. */
9409 if (f == SELECTED_FRAME ())
9410 #endif
9411 set_frame_menubar (f, 0, 0);
9412 }
9413 else
9414 /* On a terminal screen, the menu bar is an ordinary screen
9415 line, and this makes it get updated. */
9416 w->update_mode_line = Qt;
9417 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9418 /* In the non-toolkit version, the menu bar is an ordinary screen
9419 line, and this makes it get updated. */
9420 w->update_mode_line = Qt;
9421 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9422
9423 unbind_to (count, Qnil);
9424 set_buffer_internal_1 (prev);
9425 }
9426 }
9427
9428 return hooks_run;
9429 }
9430
9431
9432 \f
9433 /***********************************************************************
9434 Output Cursor
9435 ***********************************************************************/
9436
9437 #ifdef HAVE_WINDOW_SYSTEM
9438
9439 /* EXPORT:
9440 Nominal cursor position -- where to draw output.
9441 HPOS and VPOS are window relative glyph matrix coordinates.
9442 X and Y are window relative pixel coordinates. */
9443
9444 struct cursor_pos output_cursor;
9445
9446
9447 /* EXPORT:
9448 Set the global variable output_cursor to CURSOR. All cursor
9449 positions are relative to updated_window. */
9450
9451 void
9452 set_output_cursor (cursor)
9453 struct cursor_pos *cursor;
9454 {
9455 output_cursor.hpos = cursor->hpos;
9456 output_cursor.vpos = cursor->vpos;
9457 output_cursor.x = cursor->x;
9458 output_cursor.y = cursor->y;
9459 }
9460
9461
9462 /* EXPORT for RIF:
9463 Set a nominal cursor position.
9464
9465 HPOS and VPOS are column/row positions in a window glyph matrix. X
9466 and Y are window text area relative pixel positions.
9467
9468 If this is done during an update, updated_window will contain the
9469 window that is being updated and the position is the future output
9470 cursor position for that window. If updated_window is null, use
9471 selected_window and display the cursor at the given position. */
9472
9473 void
9474 x_cursor_to (vpos, hpos, y, x)
9475 int vpos, hpos, y, x;
9476 {
9477 struct window *w;
9478
9479 /* If updated_window is not set, work on selected_window. */
9480 if (updated_window)
9481 w = updated_window;
9482 else
9483 w = XWINDOW (selected_window);
9484
9485 /* Set the output cursor. */
9486 output_cursor.hpos = hpos;
9487 output_cursor.vpos = vpos;
9488 output_cursor.x = x;
9489 output_cursor.y = y;
9490
9491 /* If not called as part of an update, really display the cursor.
9492 This will also set the cursor position of W. */
9493 if (updated_window == NULL)
9494 {
9495 BLOCK_INPUT;
9496 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9497 if (rif->flush_display_optional)
9498 rif->flush_display_optional (SELECTED_FRAME ());
9499 UNBLOCK_INPUT;
9500 }
9501 }
9502
9503 #endif /* HAVE_WINDOW_SYSTEM */
9504
9505 \f
9506 /***********************************************************************
9507 Tool-bars
9508 ***********************************************************************/
9509
9510 #ifdef HAVE_WINDOW_SYSTEM
9511
9512 /* Where the mouse was last time we reported a mouse event. */
9513
9514 FRAME_PTR last_mouse_frame;
9515
9516 /* Tool-bar item index of the item on which a mouse button was pressed
9517 or -1. */
9518
9519 int last_tool_bar_item;
9520
9521
9522 /* Update the tool-bar item list for frame F. This has to be done
9523 before we start to fill in any display lines. Called from
9524 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9525 and restore it here. */
9526
9527 static void
9528 update_tool_bar (f, save_match_data)
9529 struct frame *f;
9530 int save_match_data;
9531 {
9532 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9533 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9534 #else
9535 int do_update = WINDOWP (f->tool_bar_window)
9536 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9537 #endif
9538
9539 if (do_update)
9540 {
9541 Lisp_Object window;
9542 struct window *w;
9543
9544 window = FRAME_SELECTED_WINDOW (f);
9545 w = XWINDOW (window);
9546
9547 /* If the user has switched buffers or windows, we need to
9548 recompute to reflect the new bindings. But we'll
9549 recompute when update_mode_lines is set too; that means
9550 that people can use force-mode-line-update to request
9551 that the menu bar be recomputed. The adverse effect on
9552 the rest of the redisplay algorithm is about the same as
9553 windows_or_buffers_changed anyway. */
9554 if (windows_or_buffers_changed
9555 || !NILP (w->update_mode_line)
9556 || update_mode_lines
9557 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9558 < BUF_MODIFF (XBUFFER (w->buffer)))
9559 != !NILP (w->last_had_star))
9560 || ((!NILP (Vtransient_mark_mode)
9561 && !NILP (XBUFFER (w->buffer)->mark_active))
9562 != !NILP (w->region_showing)))
9563 {
9564 struct buffer *prev = current_buffer;
9565 int count = SPECPDL_INDEX ();
9566 Lisp_Object new_tool_bar;
9567 int new_n_tool_bar;
9568 struct gcpro gcpro1;
9569
9570 /* Set current_buffer to the buffer of the selected
9571 window of the frame, so that we get the right local
9572 keymaps. */
9573 set_buffer_internal_1 (XBUFFER (w->buffer));
9574
9575 /* Save match data, if we must. */
9576 if (save_match_data)
9577 record_unwind_save_match_data ();
9578
9579 /* Make sure that we don't accidentally use bogus keymaps. */
9580 if (NILP (Voverriding_local_map_menu_flag))
9581 {
9582 specbind (Qoverriding_terminal_local_map, Qnil);
9583 specbind (Qoverriding_local_map, Qnil);
9584 }
9585
9586 GCPRO1 (new_tool_bar);
9587
9588 /* Build desired tool-bar items from keymaps. */
9589 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9590 &new_n_tool_bar);
9591
9592 /* Redisplay the tool-bar if we changed it. */
9593 if (new_n_tool_bar != f->n_tool_bar_items
9594 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9595 {
9596 /* Redisplay that happens asynchronously due to an expose event
9597 may access f->tool_bar_items. Make sure we update both
9598 variables within BLOCK_INPUT so no such event interrupts. */
9599 BLOCK_INPUT;
9600 f->tool_bar_items = new_tool_bar;
9601 f->n_tool_bar_items = new_n_tool_bar;
9602 w->update_mode_line = Qt;
9603 UNBLOCK_INPUT;
9604 }
9605
9606 UNGCPRO;
9607
9608 unbind_to (count, Qnil);
9609 set_buffer_internal_1 (prev);
9610 }
9611 }
9612 }
9613
9614
9615 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9616 F's desired tool-bar contents. F->tool_bar_items must have
9617 been set up previously by calling prepare_menu_bars. */
9618
9619 static void
9620 build_desired_tool_bar_string (f)
9621 struct frame *f;
9622 {
9623 int i, size, size_needed;
9624 struct gcpro gcpro1, gcpro2, gcpro3;
9625 Lisp_Object image, plist, props;
9626
9627 image = plist = props = Qnil;
9628 GCPRO3 (image, plist, props);
9629
9630 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9631 Otherwise, make a new string. */
9632
9633 /* The size of the string we might be able to reuse. */
9634 size = (STRINGP (f->desired_tool_bar_string)
9635 ? SCHARS (f->desired_tool_bar_string)
9636 : 0);
9637
9638 /* We need one space in the string for each image. */
9639 size_needed = f->n_tool_bar_items;
9640
9641 /* Reuse f->desired_tool_bar_string, if possible. */
9642 if (size < size_needed || NILP (f->desired_tool_bar_string))
9643 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9644 make_number (' '));
9645 else
9646 {
9647 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9648 Fremove_text_properties (make_number (0), make_number (size),
9649 props, f->desired_tool_bar_string);
9650 }
9651
9652 /* Put a `display' property on the string for the images to display,
9653 put a `menu_item' property on tool-bar items with a value that
9654 is the index of the item in F's tool-bar item vector. */
9655 for (i = 0; i < f->n_tool_bar_items; ++i)
9656 {
9657 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9658
9659 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9660 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9661 int hmargin, vmargin, relief, idx, end;
9662 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9663
9664 /* If image is a vector, choose the image according to the
9665 button state. */
9666 image = PROP (TOOL_BAR_ITEM_IMAGES);
9667 if (VECTORP (image))
9668 {
9669 if (enabled_p)
9670 idx = (selected_p
9671 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9672 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9673 else
9674 idx = (selected_p
9675 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9676 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9677
9678 xassert (ASIZE (image) >= idx);
9679 image = AREF (image, idx);
9680 }
9681 else
9682 idx = -1;
9683
9684 /* Ignore invalid image specifications. */
9685 if (!valid_image_p (image))
9686 continue;
9687
9688 /* Display the tool-bar button pressed, or depressed. */
9689 plist = Fcopy_sequence (XCDR (image));
9690
9691 /* Compute margin and relief to draw. */
9692 relief = (tool_bar_button_relief >= 0
9693 ? tool_bar_button_relief
9694 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9695 hmargin = vmargin = relief;
9696
9697 if (INTEGERP (Vtool_bar_button_margin)
9698 && XINT (Vtool_bar_button_margin) > 0)
9699 {
9700 hmargin += XFASTINT (Vtool_bar_button_margin);
9701 vmargin += XFASTINT (Vtool_bar_button_margin);
9702 }
9703 else if (CONSP (Vtool_bar_button_margin))
9704 {
9705 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9706 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9707 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9708
9709 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9710 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9711 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9712 }
9713
9714 if (auto_raise_tool_bar_buttons_p)
9715 {
9716 /* Add a `:relief' property to the image spec if the item is
9717 selected. */
9718 if (selected_p)
9719 {
9720 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9721 hmargin -= relief;
9722 vmargin -= relief;
9723 }
9724 }
9725 else
9726 {
9727 /* If image is selected, display it pressed, i.e. with a
9728 negative relief. If it's not selected, display it with a
9729 raised relief. */
9730 plist = Fplist_put (plist, QCrelief,
9731 (selected_p
9732 ? make_number (-relief)
9733 : make_number (relief)));
9734 hmargin -= relief;
9735 vmargin -= relief;
9736 }
9737
9738 /* Put a margin around the image. */
9739 if (hmargin || vmargin)
9740 {
9741 if (hmargin == vmargin)
9742 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9743 else
9744 plist = Fplist_put (plist, QCmargin,
9745 Fcons (make_number (hmargin),
9746 make_number (vmargin)));
9747 }
9748
9749 /* If button is not enabled, and we don't have special images
9750 for the disabled state, make the image appear disabled by
9751 applying an appropriate algorithm to it. */
9752 if (!enabled_p && idx < 0)
9753 plist = Fplist_put (plist, QCconversion, Qdisabled);
9754
9755 /* Put a `display' text property on the string for the image to
9756 display. Put a `menu-item' property on the string that gives
9757 the start of this item's properties in the tool-bar items
9758 vector. */
9759 image = Fcons (Qimage, plist);
9760 props = list4 (Qdisplay, image,
9761 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9762
9763 /* Let the last image hide all remaining spaces in the tool bar
9764 string. The string can be longer than needed when we reuse a
9765 previous string. */
9766 if (i + 1 == f->n_tool_bar_items)
9767 end = SCHARS (f->desired_tool_bar_string);
9768 else
9769 end = i + 1;
9770 Fadd_text_properties (make_number (i), make_number (end),
9771 props, f->desired_tool_bar_string);
9772 #undef PROP
9773 }
9774
9775 UNGCPRO;
9776 }
9777
9778
9779 /* Display one line of the tool-bar of frame IT->f.
9780
9781 HEIGHT specifies the desired height of the tool-bar line.
9782 If the actual height of the glyph row is less than HEIGHT, the
9783 row's height is increased to HEIGHT, and the icons are centered
9784 vertically in the new height.
9785
9786 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9787 count a final empty row in case the tool-bar width exactly matches
9788 the window width.
9789 */
9790
9791 static void
9792 display_tool_bar_line (it, height)
9793 struct it *it;
9794 int height;
9795 {
9796 struct glyph_row *row = it->glyph_row;
9797 int max_x = it->last_visible_x;
9798 struct glyph *last;
9799
9800 prepare_desired_row (row);
9801 row->y = it->current_y;
9802
9803 /* Note that this isn't made use of if the face hasn't a box,
9804 so there's no need to check the face here. */
9805 it->start_of_box_run_p = 1;
9806
9807 while (it->current_x < max_x)
9808 {
9809 int x, n_glyphs_before, i, nglyphs;
9810 struct it it_before;
9811
9812 /* Get the next display element. */
9813 if (!get_next_display_element (it))
9814 {
9815 /* Don't count empty row if we are counting needed tool-bar lines. */
9816 if (height < 0 && !it->hpos)
9817 return;
9818 break;
9819 }
9820
9821 /* Produce glyphs. */
9822 n_glyphs_before = row->used[TEXT_AREA];
9823 it_before = *it;
9824
9825 PRODUCE_GLYPHS (it);
9826
9827 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9828 i = 0;
9829 x = it_before.current_x;
9830 while (i < nglyphs)
9831 {
9832 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9833
9834 if (x + glyph->pixel_width > max_x)
9835 {
9836 /* Glyph doesn't fit on line. Backtrack. */
9837 row->used[TEXT_AREA] = n_glyphs_before;
9838 *it = it_before;
9839 /* If this is the only glyph on this line, it will never fit on the
9840 toolbar, so skip it. But ensure there is at least one glyph,
9841 so we don't accidentally disable the tool-bar. */
9842 if (n_glyphs_before == 0
9843 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9844 break;
9845 goto out;
9846 }
9847
9848 ++it->hpos;
9849 x += glyph->pixel_width;
9850 ++i;
9851 }
9852
9853 /* Stop at line ends. */
9854 if (ITERATOR_AT_END_OF_LINE_P (it))
9855 break;
9856
9857 set_iterator_to_next (it, 1);
9858 }
9859
9860 out:;
9861
9862 row->displays_text_p = row->used[TEXT_AREA] != 0;
9863
9864 /* Use default face for the border below the tool bar.
9865
9866 FIXME: When auto-resize-tool-bars is grow-only, there is
9867 no additional border below the possibly empty tool-bar lines.
9868 So to make the extra empty lines look "normal", we have to
9869 use the tool-bar face for the border too. */
9870 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9871 it->face_id = DEFAULT_FACE_ID;
9872
9873 extend_face_to_end_of_line (it);
9874 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9875 last->right_box_line_p = 1;
9876 if (last == row->glyphs[TEXT_AREA])
9877 last->left_box_line_p = 1;
9878
9879 /* Make line the desired height and center it vertically. */
9880 if ((height -= it->max_ascent + it->max_descent) > 0)
9881 {
9882 /* Don't add more than one line height. */
9883 height %= FRAME_LINE_HEIGHT (it->f);
9884 it->max_ascent += height / 2;
9885 it->max_descent += (height + 1) / 2;
9886 }
9887
9888 compute_line_metrics (it);
9889
9890 /* If line is empty, make it occupy the rest of the tool-bar. */
9891 if (!row->displays_text_p)
9892 {
9893 row->height = row->phys_height = it->last_visible_y - row->y;
9894 row->visible_height = row->height;
9895 row->ascent = row->phys_ascent = 0;
9896 row->extra_line_spacing = 0;
9897 }
9898
9899 row->full_width_p = 1;
9900 row->continued_p = 0;
9901 row->truncated_on_left_p = 0;
9902 row->truncated_on_right_p = 0;
9903
9904 it->current_x = it->hpos = 0;
9905 it->current_y += row->height;
9906 ++it->vpos;
9907 ++it->glyph_row;
9908 }
9909
9910
9911 /* Max tool-bar height. */
9912
9913 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9914 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9915
9916 /* Value is the number of screen lines needed to make all tool-bar
9917 items of frame F visible. The number of actual rows needed is
9918 returned in *N_ROWS if non-NULL. */
9919
9920 static int
9921 tool_bar_lines_needed (f, n_rows)
9922 struct frame *f;
9923 int *n_rows;
9924 {
9925 struct window *w = XWINDOW (f->tool_bar_window);
9926 struct it it;
9927 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9928 the desired matrix, so use (unused) mode-line row as temporary row to
9929 avoid destroying the first tool-bar row. */
9930 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9931
9932 /* Initialize an iterator for iteration over
9933 F->desired_tool_bar_string in the tool-bar window of frame F. */
9934 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9935 it.first_visible_x = 0;
9936 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9937 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9938
9939 while (!ITERATOR_AT_END_P (&it))
9940 {
9941 clear_glyph_row (temp_row);
9942 it.glyph_row = temp_row;
9943 display_tool_bar_line (&it, -1);
9944 }
9945 clear_glyph_row (temp_row);
9946
9947 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9948 if (n_rows)
9949 *n_rows = it.vpos > 0 ? it.vpos : -1;
9950
9951 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9952 }
9953
9954
9955 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9956 0, 1, 0,
9957 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9958 (frame)
9959 Lisp_Object frame;
9960 {
9961 struct frame *f;
9962 struct window *w;
9963 int nlines = 0;
9964
9965 if (NILP (frame))
9966 frame = selected_frame;
9967 else
9968 CHECK_FRAME (frame);
9969 f = XFRAME (frame);
9970
9971 if (WINDOWP (f->tool_bar_window)
9972 || (w = XWINDOW (f->tool_bar_window),
9973 WINDOW_TOTAL_LINES (w) > 0))
9974 {
9975 update_tool_bar (f, 1);
9976 if (f->n_tool_bar_items)
9977 {
9978 build_desired_tool_bar_string (f);
9979 nlines = tool_bar_lines_needed (f, NULL);
9980 }
9981 }
9982
9983 return make_number (nlines);
9984 }
9985
9986
9987 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9988 height should be changed. */
9989
9990 static int
9991 redisplay_tool_bar (f)
9992 struct frame *f;
9993 {
9994 struct window *w;
9995 struct it it;
9996 struct glyph_row *row;
9997
9998 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9999 if (FRAME_EXTERNAL_TOOL_BAR (f))
10000 update_frame_tool_bar (f);
10001 return 0;
10002 #endif
10003
10004 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10005 do anything. This means you must start with tool-bar-lines
10006 non-zero to get the auto-sizing effect. Or in other words, you
10007 can turn off tool-bars by specifying tool-bar-lines zero. */
10008 if (!WINDOWP (f->tool_bar_window)
10009 || (w = XWINDOW (f->tool_bar_window),
10010 WINDOW_TOTAL_LINES (w) == 0))
10011 return 0;
10012
10013 /* Set up an iterator for the tool-bar window. */
10014 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10015 it.first_visible_x = 0;
10016 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10017 row = it.glyph_row;
10018
10019 /* Build a string that represents the contents of the tool-bar. */
10020 build_desired_tool_bar_string (f);
10021 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10022
10023 if (f->n_tool_bar_rows == 0)
10024 {
10025 int nlines;
10026
10027 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10028 nlines != WINDOW_TOTAL_LINES (w)))
10029 {
10030 extern Lisp_Object Qtool_bar_lines;
10031 Lisp_Object frame;
10032 int old_height = WINDOW_TOTAL_LINES (w);
10033
10034 XSETFRAME (frame, f);
10035 Fmodify_frame_parameters (frame,
10036 Fcons (Fcons (Qtool_bar_lines,
10037 make_number (nlines)),
10038 Qnil));
10039 if (WINDOW_TOTAL_LINES (w) != old_height)
10040 {
10041 clear_glyph_matrix (w->desired_matrix);
10042 fonts_changed_p = 1;
10043 return 1;
10044 }
10045 }
10046 }
10047
10048 /* Display as many lines as needed to display all tool-bar items. */
10049
10050 if (f->n_tool_bar_rows > 0)
10051 {
10052 int border, rows, height, extra;
10053
10054 if (INTEGERP (Vtool_bar_border))
10055 border = XINT (Vtool_bar_border);
10056 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10057 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10058 else if (EQ (Vtool_bar_border, Qborder_width))
10059 border = f->border_width;
10060 else
10061 border = 0;
10062 if (border < 0)
10063 border = 0;
10064
10065 rows = f->n_tool_bar_rows;
10066 height = max (1, (it.last_visible_y - border) / rows);
10067 extra = it.last_visible_y - border - height * rows;
10068
10069 while (it.current_y < it.last_visible_y)
10070 {
10071 int h = 0;
10072 if (extra > 0 && rows-- > 0)
10073 {
10074 h = (extra + rows - 1) / rows;
10075 extra -= h;
10076 }
10077 display_tool_bar_line (&it, height + h);
10078 }
10079 }
10080 else
10081 {
10082 while (it.current_y < it.last_visible_y)
10083 display_tool_bar_line (&it, 0);
10084 }
10085
10086 /* It doesn't make much sense to try scrolling in the tool-bar
10087 window, so don't do it. */
10088 w->desired_matrix->no_scrolling_p = 1;
10089 w->must_be_updated_p = 1;
10090
10091 if (!NILP (Vauto_resize_tool_bars))
10092 {
10093 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10094 int change_height_p = 0;
10095
10096 /* If we couldn't display everything, change the tool-bar's
10097 height if there is room for more. */
10098 if (IT_STRING_CHARPOS (it) < it.end_charpos
10099 && it.current_y < max_tool_bar_height)
10100 change_height_p = 1;
10101
10102 row = it.glyph_row - 1;
10103
10104 /* If there are blank lines at the end, except for a partially
10105 visible blank line at the end that is smaller than
10106 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10107 if (!row->displays_text_p
10108 && row->height >= FRAME_LINE_HEIGHT (f))
10109 change_height_p = 1;
10110
10111 /* If row displays tool-bar items, but is partially visible,
10112 change the tool-bar's height. */
10113 if (row->displays_text_p
10114 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10115 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10116 change_height_p = 1;
10117
10118 /* Resize windows as needed by changing the `tool-bar-lines'
10119 frame parameter. */
10120 if (change_height_p)
10121 {
10122 extern Lisp_Object Qtool_bar_lines;
10123 Lisp_Object frame;
10124 int old_height = WINDOW_TOTAL_LINES (w);
10125 int nrows;
10126 int nlines = tool_bar_lines_needed (f, &nrows);
10127
10128 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10129 && !f->minimize_tool_bar_window_p)
10130 ? (nlines > old_height)
10131 : (nlines != old_height));
10132 f->minimize_tool_bar_window_p = 0;
10133
10134 if (change_height_p)
10135 {
10136 XSETFRAME (frame, f);
10137 Fmodify_frame_parameters (frame,
10138 Fcons (Fcons (Qtool_bar_lines,
10139 make_number (nlines)),
10140 Qnil));
10141 if (WINDOW_TOTAL_LINES (w) != old_height)
10142 {
10143 clear_glyph_matrix (w->desired_matrix);
10144 f->n_tool_bar_rows = nrows;
10145 fonts_changed_p = 1;
10146 return 1;
10147 }
10148 }
10149 }
10150 }
10151
10152 f->minimize_tool_bar_window_p = 0;
10153 return 0;
10154 }
10155
10156
10157 /* Get information about the tool-bar item which is displayed in GLYPH
10158 on frame F. Return in *PROP_IDX the index where tool-bar item
10159 properties start in F->tool_bar_items. Value is zero if
10160 GLYPH doesn't display a tool-bar item. */
10161
10162 static int
10163 tool_bar_item_info (f, glyph, prop_idx)
10164 struct frame *f;
10165 struct glyph *glyph;
10166 int *prop_idx;
10167 {
10168 Lisp_Object prop;
10169 int success_p;
10170 int charpos;
10171
10172 /* This function can be called asynchronously, which means we must
10173 exclude any possibility that Fget_text_property signals an
10174 error. */
10175 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10176 charpos = max (0, charpos);
10177
10178 /* Get the text property `menu-item' at pos. The value of that
10179 property is the start index of this item's properties in
10180 F->tool_bar_items. */
10181 prop = Fget_text_property (make_number (charpos),
10182 Qmenu_item, f->current_tool_bar_string);
10183 if (INTEGERP (prop))
10184 {
10185 *prop_idx = XINT (prop);
10186 success_p = 1;
10187 }
10188 else
10189 success_p = 0;
10190
10191 return success_p;
10192 }
10193
10194 \f
10195 /* Get information about the tool-bar item at position X/Y on frame F.
10196 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10197 the current matrix of the tool-bar window of F, or NULL if not
10198 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10199 item in F->tool_bar_items. Value is
10200
10201 -1 if X/Y is not on a tool-bar item
10202 0 if X/Y is on the same item that was highlighted before.
10203 1 otherwise. */
10204
10205 static int
10206 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10207 struct frame *f;
10208 int x, y;
10209 struct glyph **glyph;
10210 int *hpos, *vpos, *prop_idx;
10211 {
10212 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10213 struct window *w = XWINDOW (f->tool_bar_window);
10214 int area;
10215
10216 /* Find the glyph under X/Y. */
10217 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10218 if (*glyph == NULL)
10219 return -1;
10220
10221 /* Get the start of this tool-bar item's properties in
10222 f->tool_bar_items. */
10223 if (!tool_bar_item_info (f, *glyph, prop_idx))
10224 return -1;
10225
10226 /* Is mouse on the highlighted item? */
10227 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10228 && *vpos >= dpyinfo->mouse_face_beg_row
10229 && *vpos <= dpyinfo->mouse_face_end_row
10230 && (*vpos > dpyinfo->mouse_face_beg_row
10231 || *hpos >= dpyinfo->mouse_face_beg_col)
10232 && (*vpos < dpyinfo->mouse_face_end_row
10233 || *hpos < dpyinfo->mouse_face_end_col
10234 || dpyinfo->mouse_face_past_end))
10235 return 0;
10236
10237 return 1;
10238 }
10239
10240
10241 /* EXPORT:
10242 Handle mouse button event on the tool-bar of frame F, at
10243 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10244 0 for button release. MODIFIERS is event modifiers for button
10245 release. */
10246
10247 void
10248 handle_tool_bar_click (f, x, y, down_p, modifiers)
10249 struct frame *f;
10250 int x, y, down_p;
10251 unsigned int modifiers;
10252 {
10253 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10254 struct window *w = XWINDOW (f->tool_bar_window);
10255 int hpos, vpos, prop_idx;
10256 struct glyph *glyph;
10257 Lisp_Object enabled_p;
10258
10259 /* If not on the highlighted tool-bar item, return. */
10260 frame_to_window_pixel_xy (w, &x, &y);
10261 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10262 return;
10263
10264 /* If item is disabled, do nothing. */
10265 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10266 if (NILP (enabled_p))
10267 return;
10268
10269 if (down_p)
10270 {
10271 /* Show item in pressed state. */
10272 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10273 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10274 last_tool_bar_item = prop_idx;
10275 }
10276 else
10277 {
10278 Lisp_Object key, frame;
10279 struct input_event event;
10280 EVENT_INIT (event);
10281
10282 /* Show item in released state. */
10283 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10284 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10285
10286 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10287
10288 XSETFRAME (frame, f);
10289 event.kind = TOOL_BAR_EVENT;
10290 event.frame_or_window = frame;
10291 event.arg = frame;
10292 kbd_buffer_store_event (&event);
10293
10294 event.kind = TOOL_BAR_EVENT;
10295 event.frame_or_window = frame;
10296 event.arg = key;
10297 event.modifiers = modifiers;
10298 kbd_buffer_store_event (&event);
10299 last_tool_bar_item = -1;
10300 }
10301 }
10302
10303
10304 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10305 tool-bar window-relative coordinates X/Y. Called from
10306 note_mouse_highlight. */
10307
10308 static void
10309 note_tool_bar_highlight (f, x, y)
10310 struct frame *f;
10311 int x, y;
10312 {
10313 Lisp_Object window = f->tool_bar_window;
10314 struct window *w = XWINDOW (window);
10315 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10316 int hpos, vpos;
10317 struct glyph *glyph;
10318 struct glyph_row *row;
10319 int i;
10320 Lisp_Object enabled_p;
10321 int prop_idx;
10322 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10323 int mouse_down_p, rc;
10324
10325 /* Function note_mouse_highlight is called with negative x(y
10326 values when mouse moves outside of the frame. */
10327 if (x <= 0 || y <= 0)
10328 {
10329 clear_mouse_face (dpyinfo);
10330 return;
10331 }
10332
10333 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10334 if (rc < 0)
10335 {
10336 /* Not on tool-bar item. */
10337 clear_mouse_face (dpyinfo);
10338 return;
10339 }
10340 else if (rc == 0)
10341 /* On same tool-bar item as before. */
10342 goto set_help_echo;
10343
10344 clear_mouse_face (dpyinfo);
10345
10346 /* Mouse is down, but on different tool-bar item? */
10347 mouse_down_p = (dpyinfo->grabbed
10348 && f == last_mouse_frame
10349 && FRAME_LIVE_P (f));
10350 if (mouse_down_p
10351 && last_tool_bar_item != prop_idx)
10352 return;
10353
10354 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10355 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10356
10357 /* If tool-bar item is not enabled, don't highlight it. */
10358 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10359 if (!NILP (enabled_p))
10360 {
10361 /* Compute the x-position of the glyph. In front and past the
10362 image is a space. We include this in the highlighted area. */
10363 row = MATRIX_ROW (w->current_matrix, vpos);
10364 for (i = x = 0; i < hpos; ++i)
10365 x += row->glyphs[TEXT_AREA][i].pixel_width;
10366
10367 /* Record this as the current active region. */
10368 dpyinfo->mouse_face_beg_col = hpos;
10369 dpyinfo->mouse_face_beg_row = vpos;
10370 dpyinfo->mouse_face_beg_x = x;
10371 dpyinfo->mouse_face_beg_y = row->y;
10372 dpyinfo->mouse_face_past_end = 0;
10373
10374 dpyinfo->mouse_face_end_col = hpos + 1;
10375 dpyinfo->mouse_face_end_row = vpos;
10376 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10377 dpyinfo->mouse_face_end_y = row->y;
10378 dpyinfo->mouse_face_window = window;
10379 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10380
10381 /* Display it as active. */
10382 show_mouse_face (dpyinfo, draw);
10383 dpyinfo->mouse_face_image_state = draw;
10384 }
10385
10386 set_help_echo:
10387
10388 /* Set help_echo_string to a help string to display for this tool-bar item.
10389 XTread_socket does the rest. */
10390 help_echo_object = help_echo_window = Qnil;
10391 help_echo_pos = -1;
10392 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10393 if (NILP (help_echo_string))
10394 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10395 }
10396
10397 #endif /* HAVE_WINDOW_SYSTEM */
10398
10399
10400 \f
10401 /************************************************************************
10402 Horizontal scrolling
10403 ************************************************************************/
10404
10405 static int hscroll_window_tree P_ ((Lisp_Object));
10406 static int hscroll_windows P_ ((Lisp_Object));
10407
10408 /* For all leaf windows in the window tree rooted at WINDOW, set their
10409 hscroll value so that PT is (i) visible in the window, and (ii) so
10410 that it is not within a certain margin at the window's left and
10411 right border. Value is non-zero if any window's hscroll has been
10412 changed. */
10413
10414 static int
10415 hscroll_window_tree (window)
10416 Lisp_Object window;
10417 {
10418 int hscrolled_p = 0;
10419 int hscroll_relative_p = FLOATP (Vhscroll_step);
10420 int hscroll_step_abs = 0;
10421 double hscroll_step_rel = 0;
10422
10423 if (hscroll_relative_p)
10424 {
10425 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10426 if (hscroll_step_rel < 0)
10427 {
10428 hscroll_relative_p = 0;
10429 hscroll_step_abs = 0;
10430 }
10431 }
10432 else if (INTEGERP (Vhscroll_step))
10433 {
10434 hscroll_step_abs = XINT (Vhscroll_step);
10435 if (hscroll_step_abs < 0)
10436 hscroll_step_abs = 0;
10437 }
10438 else
10439 hscroll_step_abs = 0;
10440
10441 while (WINDOWP (window))
10442 {
10443 struct window *w = XWINDOW (window);
10444
10445 if (WINDOWP (w->hchild))
10446 hscrolled_p |= hscroll_window_tree (w->hchild);
10447 else if (WINDOWP (w->vchild))
10448 hscrolled_p |= hscroll_window_tree (w->vchild);
10449 else if (w->cursor.vpos >= 0)
10450 {
10451 int h_margin;
10452 int text_area_width;
10453 struct glyph_row *current_cursor_row
10454 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10455 struct glyph_row *desired_cursor_row
10456 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10457 struct glyph_row *cursor_row
10458 = (desired_cursor_row->enabled_p
10459 ? desired_cursor_row
10460 : current_cursor_row);
10461
10462 text_area_width = window_box_width (w, TEXT_AREA);
10463
10464 /* Scroll when cursor is inside this scroll margin. */
10465 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10466
10467 if ((XFASTINT (w->hscroll)
10468 && w->cursor.x <= h_margin)
10469 || (cursor_row->enabled_p
10470 && cursor_row->truncated_on_right_p
10471 && (w->cursor.x >= text_area_width - h_margin)))
10472 {
10473 struct it it;
10474 int hscroll;
10475 struct buffer *saved_current_buffer;
10476 int pt;
10477 int wanted_x;
10478
10479 /* Find point in a display of infinite width. */
10480 saved_current_buffer = current_buffer;
10481 current_buffer = XBUFFER (w->buffer);
10482
10483 if (w == XWINDOW (selected_window))
10484 pt = BUF_PT (current_buffer);
10485 else
10486 {
10487 pt = marker_position (w->pointm);
10488 pt = max (BEGV, pt);
10489 pt = min (ZV, pt);
10490 }
10491
10492 /* Move iterator to pt starting at cursor_row->start in
10493 a line with infinite width. */
10494 init_to_row_start (&it, w, cursor_row);
10495 it.last_visible_x = INFINITY;
10496 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10497 current_buffer = saved_current_buffer;
10498
10499 /* Position cursor in window. */
10500 if (!hscroll_relative_p && hscroll_step_abs == 0)
10501 hscroll = max (0, (it.current_x
10502 - (ITERATOR_AT_END_OF_LINE_P (&it)
10503 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10504 : (text_area_width / 2))))
10505 / FRAME_COLUMN_WIDTH (it.f);
10506 else if (w->cursor.x >= text_area_width - h_margin)
10507 {
10508 if (hscroll_relative_p)
10509 wanted_x = text_area_width * (1 - hscroll_step_rel)
10510 - h_margin;
10511 else
10512 wanted_x = text_area_width
10513 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10514 - h_margin;
10515 hscroll
10516 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10517 }
10518 else
10519 {
10520 if (hscroll_relative_p)
10521 wanted_x = text_area_width * hscroll_step_rel
10522 + h_margin;
10523 else
10524 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10525 + h_margin;
10526 hscroll
10527 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10528 }
10529 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10530
10531 /* Don't call Fset_window_hscroll if value hasn't
10532 changed because it will prevent redisplay
10533 optimizations. */
10534 if (XFASTINT (w->hscroll) != hscroll)
10535 {
10536 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10537 w->hscroll = make_number (hscroll);
10538 hscrolled_p = 1;
10539 }
10540 }
10541 }
10542
10543 window = w->next;
10544 }
10545
10546 /* Value is non-zero if hscroll of any leaf window has been changed. */
10547 return hscrolled_p;
10548 }
10549
10550
10551 /* Set hscroll so that cursor is visible and not inside horizontal
10552 scroll margins for all windows in the tree rooted at WINDOW. See
10553 also hscroll_window_tree above. Value is non-zero if any window's
10554 hscroll has been changed. If it has, desired matrices on the frame
10555 of WINDOW are cleared. */
10556
10557 static int
10558 hscroll_windows (window)
10559 Lisp_Object window;
10560 {
10561 int hscrolled_p;
10562
10563 if (automatic_hscrolling_p)
10564 {
10565 hscrolled_p = hscroll_window_tree (window);
10566 if (hscrolled_p)
10567 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10568 }
10569 else
10570 hscrolled_p = 0;
10571 return hscrolled_p;
10572 }
10573
10574
10575 \f
10576 /************************************************************************
10577 Redisplay
10578 ************************************************************************/
10579
10580 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10581 to a non-zero value. This is sometimes handy to have in a debugger
10582 session. */
10583
10584 #if GLYPH_DEBUG
10585
10586 /* First and last unchanged row for try_window_id. */
10587
10588 int debug_first_unchanged_at_end_vpos;
10589 int debug_last_unchanged_at_beg_vpos;
10590
10591 /* Delta vpos and y. */
10592
10593 int debug_dvpos, debug_dy;
10594
10595 /* Delta in characters and bytes for try_window_id. */
10596
10597 int debug_delta, debug_delta_bytes;
10598
10599 /* Values of window_end_pos and window_end_vpos at the end of
10600 try_window_id. */
10601
10602 EMACS_INT debug_end_pos, debug_end_vpos;
10603
10604 /* Append a string to W->desired_matrix->method. FMT is a printf
10605 format string. A1...A9 are a supplement for a variable-length
10606 argument list. If trace_redisplay_p is non-zero also printf the
10607 resulting string to stderr. */
10608
10609 static void
10610 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10611 struct window *w;
10612 char *fmt;
10613 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10614 {
10615 char buffer[512];
10616 char *method = w->desired_matrix->method;
10617 int len = strlen (method);
10618 int size = sizeof w->desired_matrix->method;
10619 int remaining = size - len - 1;
10620
10621 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10622 if (len && remaining)
10623 {
10624 method[len] = '|';
10625 --remaining, ++len;
10626 }
10627
10628 strncpy (method + len, buffer, remaining);
10629
10630 if (trace_redisplay_p)
10631 fprintf (stderr, "%p (%s): %s\n",
10632 w,
10633 ((BUFFERP (w->buffer)
10634 && STRINGP (XBUFFER (w->buffer)->name))
10635 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10636 : "no buffer"),
10637 buffer);
10638 }
10639
10640 #endif /* GLYPH_DEBUG */
10641
10642
10643 /* Value is non-zero if all changes in window W, which displays
10644 current_buffer, are in the text between START and END. START is a
10645 buffer position, END is given as a distance from Z. Used in
10646 redisplay_internal for display optimization. */
10647
10648 static INLINE int
10649 text_outside_line_unchanged_p (w, start, end)
10650 struct window *w;
10651 int start, end;
10652 {
10653 int unchanged_p = 1;
10654
10655 /* If text or overlays have changed, see where. */
10656 if (XFASTINT (w->last_modified) < MODIFF
10657 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10658 {
10659 /* Gap in the line? */
10660 if (GPT < start || Z - GPT < end)
10661 unchanged_p = 0;
10662
10663 /* Changes start in front of the line, or end after it? */
10664 if (unchanged_p
10665 && (BEG_UNCHANGED < start - 1
10666 || END_UNCHANGED < end))
10667 unchanged_p = 0;
10668
10669 /* If selective display, can't optimize if changes start at the
10670 beginning of the line. */
10671 if (unchanged_p
10672 && INTEGERP (current_buffer->selective_display)
10673 && XINT (current_buffer->selective_display) > 0
10674 && (BEG_UNCHANGED < start || GPT <= start))
10675 unchanged_p = 0;
10676
10677 /* If there are overlays at the start or end of the line, these
10678 may have overlay strings with newlines in them. A change at
10679 START, for instance, may actually concern the display of such
10680 overlay strings as well, and they are displayed on different
10681 lines. So, quickly rule out this case. (For the future, it
10682 might be desirable to implement something more telling than
10683 just BEG/END_UNCHANGED.) */
10684 if (unchanged_p)
10685 {
10686 if (BEG + BEG_UNCHANGED == start
10687 && overlay_touches_p (start))
10688 unchanged_p = 0;
10689 if (END_UNCHANGED == end
10690 && overlay_touches_p (Z - end))
10691 unchanged_p = 0;
10692 }
10693 }
10694
10695 return unchanged_p;
10696 }
10697
10698
10699 /* Do a frame update, taking possible shortcuts into account. This is
10700 the main external entry point for redisplay.
10701
10702 If the last redisplay displayed an echo area message and that message
10703 is no longer requested, we clear the echo area or bring back the
10704 mini-buffer if that is in use. */
10705
10706 void
10707 redisplay ()
10708 {
10709 redisplay_internal (0);
10710 }
10711
10712
10713 static Lisp_Object
10714 overlay_arrow_string_or_property (var)
10715 Lisp_Object var;
10716 {
10717 Lisp_Object val;
10718
10719 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10720 return val;
10721
10722 return Voverlay_arrow_string;
10723 }
10724
10725 /* Return 1 if there are any overlay-arrows in current_buffer. */
10726 static int
10727 overlay_arrow_in_current_buffer_p ()
10728 {
10729 Lisp_Object vlist;
10730
10731 for (vlist = Voverlay_arrow_variable_list;
10732 CONSP (vlist);
10733 vlist = XCDR (vlist))
10734 {
10735 Lisp_Object var = XCAR (vlist);
10736 Lisp_Object val;
10737
10738 if (!SYMBOLP (var))
10739 continue;
10740 val = find_symbol_value (var);
10741 if (MARKERP (val)
10742 && current_buffer == XMARKER (val)->buffer)
10743 return 1;
10744 }
10745 return 0;
10746 }
10747
10748
10749 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10750 has changed. */
10751
10752 static int
10753 overlay_arrows_changed_p ()
10754 {
10755 Lisp_Object vlist;
10756
10757 for (vlist = Voverlay_arrow_variable_list;
10758 CONSP (vlist);
10759 vlist = XCDR (vlist))
10760 {
10761 Lisp_Object var = XCAR (vlist);
10762 Lisp_Object val, pstr;
10763
10764 if (!SYMBOLP (var))
10765 continue;
10766 val = find_symbol_value (var);
10767 if (!MARKERP (val))
10768 continue;
10769 if (! EQ (COERCE_MARKER (val),
10770 Fget (var, Qlast_arrow_position))
10771 || ! (pstr = overlay_arrow_string_or_property (var),
10772 EQ (pstr, Fget (var, Qlast_arrow_string))))
10773 return 1;
10774 }
10775 return 0;
10776 }
10777
10778 /* Mark overlay arrows to be updated on next redisplay. */
10779
10780 static void
10781 update_overlay_arrows (up_to_date)
10782 int up_to_date;
10783 {
10784 Lisp_Object vlist;
10785
10786 for (vlist = Voverlay_arrow_variable_list;
10787 CONSP (vlist);
10788 vlist = XCDR (vlist))
10789 {
10790 Lisp_Object var = XCAR (vlist);
10791
10792 if (!SYMBOLP (var))
10793 continue;
10794
10795 if (up_to_date > 0)
10796 {
10797 Lisp_Object val = find_symbol_value (var);
10798 Fput (var, Qlast_arrow_position,
10799 COERCE_MARKER (val));
10800 Fput (var, Qlast_arrow_string,
10801 overlay_arrow_string_or_property (var));
10802 }
10803 else if (up_to_date < 0
10804 || !NILP (Fget (var, Qlast_arrow_position)))
10805 {
10806 Fput (var, Qlast_arrow_position, Qt);
10807 Fput (var, Qlast_arrow_string, Qt);
10808 }
10809 }
10810 }
10811
10812
10813 /* Return overlay arrow string to display at row.
10814 Return integer (bitmap number) for arrow bitmap in left fringe.
10815 Return nil if no overlay arrow. */
10816
10817 static Lisp_Object
10818 overlay_arrow_at_row (it, row)
10819 struct it *it;
10820 struct glyph_row *row;
10821 {
10822 Lisp_Object vlist;
10823
10824 for (vlist = Voverlay_arrow_variable_list;
10825 CONSP (vlist);
10826 vlist = XCDR (vlist))
10827 {
10828 Lisp_Object var = XCAR (vlist);
10829 Lisp_Object val;
10830
10831 if (!SYMBOLP (var))
10832 continue;
10833
10834 val = find_symbol_value (var);
10835
10836 if (MARKERP (val)
10837 && current_buffer == XMARKER (val)->buffer
10838 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10839 {
10840 if (FRAME_WINDOW_P (it->f)
10841 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10842 {
10843 #ifdef HAVE_WINDOW_SYSTEM
10844 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10845 {
10846 int fringe_bitmap;
10847 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10848 return make_number (fringe_bitmap);
10849 }
10850 #endif
10851 return make_number (-1); /* Use default arrow bitmap */
10852 }
10853 return overlay_arrow_string_or_property (var);
10854 }
10855 }
10856
10857 return Qnil;
10858 }
10859
10860 /* Return 1 if point moved out of or into a composition. Otherwise
10861 return 0. PREV_BUF and PREV_PT are the last point buffer and
10862 position. BUF and PT are the current point buffer and position. */
10863
10864 int
10865 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10866 struct buffer *prev_buf, *buf;
10867 int prev_pt, pt;
10868 {
10869 EMACS_INT start, end;
10870 Lisp_Object prop;
10871 Lisp_Object buffer;
10872
10873 XSETBUFFER (buffer, buf);
10874 /* Check a composition at the last point if point moved within the
10875 same buffer. */
10876 if (prev_buf == buf)
10877 {
10878 if (prev_pt == pt)
10879 /* Point didn't move. */
10880 return 0;
10881
10882 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10883 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10884 && COMPOSITION_VALID_P (start, end, prop)
10885 && start < prev_pt && end > prev_pt)
10886 /* The last point was within the composition. Return 1 iff
10887 point moved out of the composition. */
10888 return (pt <= start || pt >= end);
10889 }
10890
10891 /* Check a composition at the current point. */
10892 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10893 && find_composition (pt, -1, &start, &end, &prop, buffer)
10894 && COMPOSITION_VALID_P (start, end, prop)
10895 && start < pt && end > pt);
10896 }
10897
10898
10899 /* Reconsider the setting of B->clip_changed which is displayed
10900 in window W. */
10901
10902 static INLINE void
10903 reconsider_clip_changes (w, b)
10904 struct window *w;
10905 struct buffer *b;
10906 {
10907 if (b->clip_changed
10908 && !NILP (w->window_end_valid)
10909 && w->current_matrix->buffer == b
10910 && w->current_matrix->zv == BUF_ZV (b)
10911 && w->current_matrix->begv == BUF_BEGV (b))
10912 b->clip_changed = 0;
10913
10914 /* If display wasn't paused, and W is not a tool bar window, see if
10915 point has been moved into or out of a composition. In that case,
10916 we set b->clip_changed to 1 to force updating the screen. If
10917 b->clip_changed has already been set to 1, we can skip this
10918 check. */
10919 if (!b->clip_changed
10920 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10921 {
10922 int pt;
10923
10924 if (w == XWINDOW (selected_window))
10925 pt = BUF_PT (current_buffer);
10926 else
10927 pt = marker_position (w->pointm);
10928
10929 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10930 || pt != XINT (w->last_point))
10931 && check_point_in_composition (w->current_matrix->buffer,
10932 XINT (w->last_point),
10933 XBUFFER (w->buffer), pt))
10934 b->clip_changed = 1;
10935 }
10936 }
10937 \f
10938
10939 /* Select FRAME to forward the values of frame-local variables into C
10940 variables so that the redisplay routines can access those values
10941 directly. */
10942
10943 static void
10944 select_frame_for_redisplay (frame)
10945 Lisp_Object frame;
10946 {
10947 Lisp_Object tail, sym, val;
10948 Lisp_Object old = selected_frame;
10949
10950 selected_frame = frame;
10951
10952 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10953 if (CONSP (XCAR (tail))
10954 && (sym = XCAR (XCAR (tail)),
10955 SYMBOLP (sym))
10956 && (sym = indirect_variable (sym),
10957 val = SYMBOL_VALUE (sym),
10958 (BUFFER_LOCAL_VALUEP (val)
10959 || SOME_BUFFER_LOCAL_VALUEP (val)))
10960 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10961 /* Use find_symbol_value rather than Fsymbol_value
10962 to avoid an error if it is void. */
10963 find_symbol_value (sym);
10964
10965 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10966 if (CONSP (XCAR (tail))
10967 && (sym = XCAR (XCAR (tail)),
10968 SYMBOLP (sym))
10969 && (sym = indirect_variable (sym),
10970 val = SYMBOL_VALUE (sym),
10971 (BUFFER_LOCAL_VALUEP (val)
10972 || SOME_BUFFER_LOCAL_VALUEP (val)))
10973 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10974 find_symbol_value (sym);
10975 }
10976
10977
10978 #define STOP_POLLING \
10979 do { if (! polling_stopped_here) stop_polling (); \
10980 polling_stopped_here = 1; } while (0)
10981
10982 #define RESUME_POLLING \
10983 do { if (polling_stopped_here) start_polling (); \
10984 polling_stopped_here = 0; } while (0)
10985
10986
10987 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10988 response to any user action; therefore, we should preserve the echo
10989 area. (Actually, our caller does that job.) Perhaps in the future
10990 avoid recentering windows if it is not necessary; currently that
10991 causes some problems. */
10992
10993 static void
10994 redisplay_internal (preserve_echo_area)
10995 int preserve_echo_area;
10996 {
10997 struct window *w = XWINDOW (selected_window);
10998 struct frame *f;
10999 int pause;
11000 int must_finish = 0;
11001 struct text_pos tlbufpos, tlendpos;
11002 int number_of_visible_frames;
11003 int count, count1;
11004 struct frame *sf;
11005 int polling_stopped_here = 0;
11006
11007 /* Non-zero means redisplay has to consider all windows on all
11008 frames. Zero means, only selected_window is considered. */
11009 int consider_all_windows_p;
11010
11011 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11012
11013 /* No redisplay if running in batch mode or frame is not yet fully
11014 initialized, or redisplay is explicitly turned off by setting
11015 Vinhibit_redisplay. */
11016 if (noninteractive
11017 || !NILP (Vinhibit_redisplay))
11018 return;
11019
11020 /* Don't examine these until after testing Vinhibit_redisplay.
11021 When Emacs is shutting down, perhaps because its connection to
11022 X has dropped, we should not look at them at all. */
11023 f = XFRAME (w->frame);
11024 sf = SELECTED_FRAME ();
11025
11026 if (!f->glyphs_initialized_p)
11027 return;
11028
11029 /* The flag redisplay_performed_directly_p is set by
11030 direct_output_for_insert when it already did the whole screen
11031 update necessary. */
11032 if (redisplay_performed_directly_p)
11033 {
11034 redisplay_performed_directly_p = 0;
11035 if (!hscroll_windows (selected_window))
11036 return;
11037 }
11038
11039 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11040 if (popup_activated ())
11041 return;
11042 #endif
11043
11044 /* I don't think this happens but let's be paranoid. */
11045 if (redisplaying_p)
11046 return;
11047
11048 /* Record a function that resets redisplaying_p to its old value
11049 when we leave this function. */
11050 count = SPECPDL_INDEX ();
11051 record_unwind_protect (unwind_redisplay,
11052 Fcons (make_number (redisplaying_p), selected_frame));
11053 ++redisplaying_p;
11054 specbind (Qinhibit_free_realized_faces, Qnil);
11055
11056 {
11057 Lisp_Object tail, frame;
11058
11059 FOR_EACH_FRAME (tail, frame)
11060 {
11061 struct frame *f = XFRAME (frame);
11062 f->already_hscrolled_p = 0;
11063 }
11064 }
11065
11066 retry:
11067 pause = 0;
11068 reconsider_clip_changes (w, current_buffer);
11069 last_escape_glyph_frame = NULL;
11070 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11071
11072 /* If new fonts have been loaded that make a glyph matrix adjustment
11073 necessary, do it. */
11074 if (fonts_changed_p)
11075 {
11076 adjust_glyphs (NULL);
11077 ++windows_or_buffers_changed;
11078 fonts_changed_p = 0;
11079 }
11080
11081 /* If face_change_count is non-zero, init_iterator will free all
11082 realized faces, which includes the faces referenced from current
11083 matrices. So, we can't reuse current matrices in this case. */
11084 if (face_change_count)
11085 ++windows_or_buffers_changed;
11086
11087 if (! FRAME_WINDOW_P (sf)
11088 && previous_terminal_frame != sf)
11089 {
11090 /* Since frames on an ASCII terminal share the same display
11091 area, displaying a different frame means redisplay the whole
11092 thing. */
11093 windows_or_buffers_changed++;
11094 SET_FRAME_GARBAGED (sf);
11095 XSETFRAME (Vterminal_frame, sf);
11096 }
11097 previous_terminal_frame = sf;
11098
11099 /* Set the visible flags for all frames. Do this before checking
11100 for resized or garbaged frames; they want to know if their frames
11101 are visible. See the comment in frame.h for
11102 FRAME_SAMPLE_VISIBILITY. */
11103 {
11104 Lisp_Object tail, frame;
11105
11106 number_of_visible_frames = 0;
11107
11108 FOR_EACH_FRAME (tail, frame)
11109 {
11110 struct frame *f = XFRAME (frame);
11111
11112 FRAME_SAMPLE_VISIBILITY (f);
11113 if (FRAME_VISIBLE_P (f))
11114 ++number_of_visible_frames;
11115 clear_desired_matrices (f);
11116 }
11117 }
11118
11119 /* Notice any pending interrupt request to change frame size. */
11120 do_pending_window_change (1);
11121
11122 /* Clear frames marked as garbaged. */
11123 if (frame_garbaged)
11124 clear_garbaged_frames ();
11125
11126 /* Build menubar and tool-bar items. */
11127 if (NILP (Vmemory_full))
11128 prepare_menu_bars ();
11129
11130 if (windows_or_buffers_changed)
11131 update_mode_lines++;
11132
11133 /* Detect case that we need to write or remove a star in the mode line. */
11134 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11135 {
11136 w->update_mode_line = Qt;
11137 if (buffer_shared > 1)
11138 update_mode_lines++;
11139 }
11140
11141 /* Avoid invocation of point motion hooks by `current_column' below. */
11142 count1 = SPECPDL_INDEX ();
11143 specbind (Qinhibit_point_motion_hooks, Qt);
11144
11145 /* If %c is in the mode line, update it if needed. */
11146 if (!NILP (w->column_number_displayed)
11147 /* This alternative quickly identifies a common case
11148 where no change is needed. */
11149 && !(PT == XFASTINT (w->last_point)
11150 && XFASTINT (w->last_modified) >= MODIFF
11151 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11152 && (XFASTINT (w->column_number_displayed)
11153 != (int) current_column ())) /* iftc */
11154 w->update_mode_line = Qt;
11155
11156 unbind_to (count1, Qnil);
11157
11158 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11159
11160 /* The variable buffer_shared is set in redisplay_window and
11161 indicates that we redisplay a buffer in different windows. See
11162 there. */
11163 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11164 || cursor_type_changed);
11165
11166 /* If specs for an arrow have changed, do thorough redisplay
11167 to ensure we remove any arrow that should no longer exist. */
11168 if (overlay_arrows_changed_p ())
11169 consider_all_windows_p = windows_or_buffers_changed = 1;
11170
11171 /* Normally the message* functions will have already displayed and
11172 updated the echo area, but the frame may have been trashed, or
11173 the update may have been preempted, so display the echo area
11174 again here. Checking message_cleared_p captures the case that
11175 the echo area should be cleared. */
11176 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11177 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11178 || (message_cleared_p
11179 && minibuf_level == 0
11180 /* If the mini-window is currently selected, this means the
11181 echo-area doesn't show through. */
11182 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11183 {
11184 int window_height_changed_p = echo_area_display (0);
11185 must_finish = 1;
11186
11187 /* If we don't display the current message, don't clear the
11188 message_cleared_p flag, because, if we did, we wouldn't clear
11189 the echo area in the next redisplay which doesn't preserve
11190 the echo area. */
11191 if (!display_last_displayed_message_p)
11192 message_cleared_p = 0;
11193
11194 if (fonts_changed_p)
11195 goto retry;
11196 else if (window_height_changed_p)
11197 {
11198 consider_all_windows_p = 1;
11199 ++update_mode_lines;
11200 ++windows_or_buffers_changed;
11201
11202 /* If window configuration was changed, frames may have been
11203 marked garbaged. Clear them or we will experience
11204 surprises wrt scrolling. */
11205 if (frame_garbaged)
11206 clear_garbaged_frames ();
11207 }
11208 }
11209 else if (EQ (selected_window, minibuf_window)
11210 && (current_buffer->clip_changed
11211 || XFASTINT (w->last_modified) < MODIFF
11212 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11213 && resize_mini_window (w, 0))
11214 {
11215 /* Resized active mini-window to fit the size of what it is
11216 showing if its contents might have changed. */
11217 must_finish = 1;
11218 consider_all_windows_p = 1;
11219 ++windows_or_buffers_changed;
11220 ++update_mode_lines;
11221
11222 /* If window configuration was changed, frames may have been
11223 marked garbaged. Clear them or we will experience
11224 surprises wrt scrolling. */
11225 if (frame_garbaged)
11226 clear_garbaged_frames ();
11227 }
11228
11229
11230 /* If showing the region, and mark has changed, we must redisplay
11231 the whole window. The assignment to this_line_start_pos prevents
11232 the optimization directly below this if-statement. */
11233 if (((!NILP (Vtransient_mark_mode)
11234 && !NILP (XBUFFER (w->buffer)->mark_active))
11235 != !NILP (w->region_showing))
11236 || (!NILP (w->region_showing)
11237 && !EQ (w->region_showing,
11238 Fmarker_position (XBUFFER (w->buffer)->mark))))
11239 CHARPOS (this_line_start_pos) = 0;
11240
11241 /* Optimize the case that only the line containing the cursor in the
11242 selected window has changed. Variables starting with this_ are
11243 set in display_line and record information about the line
11244 containing the cursor. */
11245 tlbufpos = this_line_start_pos;
11246 tlendpos = this_line_end_pos;
11247 if (!consider_all_windows_p
11248 && CHARPOS (tlbufpos) > 0
11249 && NILP (w->update_mode_line)
11250 && !current_buffer->clip_changed
11251 && !current_buffer->prevent_redisplay_optimizations_p
11252 && FRAME_VISIBLE_P (XFRAME (w->frame))
11253 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11254 /* Make sure recorded data applies to current buffer, etc. */
11255 && this_line_buffer == current_buffer
11256 && current_buffer == XBUFFER (w->buffer)
11257 && NILP (w->force_start)
11258 && NILP (w->optional_new_start)
11259 /* Point must be on the line that we have info recorded about. */
11260 && PT >= CHARPOS (tlbufpos)
11261 && PT <= Z - CHARPOS (tlendpos)
11262 /* All text outside that line, including its final newline,
11263 must be unchanged */
11264 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11265 CHARPOS (tlendpos)))
11266 {
11267 if (CHARPOS (tlbufpos) > BEGV
11268 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11269 && (CHARPOS (tlbufpos) == ZV
11270 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11271 /* Former continuation line has disappeared by becoming empty */
11272 goto cancel;
11273 else if (XFASTINT (w->last_modified) < MODIFF
11274 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11275 || MINI_WINDOW_P (w))
11276 {
11277 /* We have to handle the case of continuation around a
11278 wide-column character (See the comment in indent.c around
11279 line 885).
11280
11281 For instance, in the following case:
11282
11283 -------- Insert --------
11284 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11285 J_I_ ==> J_I_ `^^' are cursors.
11286 ^^ ^^
11287 -------- --------
11288
11289 As we have to redraw the line above, we should goto cancel. */
11290
11291 struct it it;
11292 int line_height_before = this_line_pixel_height;
11293
11294 /* Note that start_display will handle the case that the
11295 line starting at tlbufpos is a continuation lines. */
11296 start_display (&it, w, tlbufpos);
11297
11298 /* Implementation note: It this still necessary? */
11299 if (it.current_x != this_line_start_x)
11300 goto cancel;
11301
11302 TRACE ((stderr, "trying display optimization 1\n"));
11303 w->cursor.vpos = -1;
11304 overlay_arrow_seen = 0;
11305 it.vpos = this_line_vpos;
11306 it.current_y = this_line_y;
11307 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11308 display_line (&it);
11309
11310 /* If line contains point, is not continued,
11311 and ends at same distance from eob as before, we win */
11312 if (w->cursor.vpos >= 0
11313 /* Line is not continued, otherwise this_line_start_pos
11314 would have been set to 0 in display_line. */
11315 && CHARPOS (this_line_start_pos)
11316 /* Line ends as before. */
11317 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11318 /* Line has same height as before. Otherwise other lines
11319 would have to be shifted up or down. */
11320 && this_line_pixel_height == line_height_before)
11321 {
11322 /* If this is not the window's last line, we must adjust
11323 the charstarts of the lines below. */
11324 if (it.current_y < it.last_visible_y)
11325 {
11326 struct glyph_row *row
11327 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11328 int delta, delta_bytes;
11329
11330 if (Z - CHARPOS (tlendpos) == ZV)
11331 {
11332 /* This line ends at end of (accessible part of)
11333 buffer. There is no newline to count. */
11334 delta = (Z
11335 - CHARPOS (tlendpos)
11336 - MATRIX_ROW_START_CHARPOS (row));
11337 delta_bytes = (Z_BYTE
11338 - BYTEPOS (tlendpos)
11339 - MATRIX_ROW_START_BYTEPOS (row));
11340 }
11341 else
11342 {
11343 /* This line ends in a newline. Must take
11344 account of the newline and the rest of the
11345 text that follows. */
11346 delta = (Z
11347 - CHARPOS (tlendpos)
11348 - MATRIX_ROW_START_CHARPOS (row));
11349 delta_bytes = (Z_BYTE
11350 - BYTEPOS (tlendpos)
11351 - MATRIX_ROW_START_BYTEPOS (row));
11352 }
11353
11354 increment_matrix_positions (w->current_matrix,
11355 this_line_vpos + 1,
11356 w->current_matrix->nrows,
11357 delta, delta_bytes);
11358 }
11359
11360 /* If this row displays text now but previously didn't,
11361 or vice versa, w->window_end_vpos may have to be
11362 adjusted. */
11363 if ((it.glyph_row - 1)->displays_text_p)
11364 {
11365 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11366 XSETINT (w->window_end_vpos, this_line_vpos);
11367 }
11368 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11369 && this_line_vpos > 0)
11370 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11371 w->window_end_valid = Qnil;
11372
11373 /* Update hint: No need to try to scroll in update_window. */
11374 w->desired_matrix->no_scrolling_p = 1;
11375
11376 #if GLYPH_DEBUG
11377 *w->desired_matrix->method = 0;
11378 debug_method_add (w, "optimization 1");
11379 #endif
11380 #ifdef HAVE_WINDOW_SYSTEM
11381 update_window_fringes (w, 0);
11382 #endif
11383 goto update;
11384 }
11385 else
11386 goto cancel;
11387 }
11388 else if (/* Cursor position hasn't changed. */
11389 PT == XFASTINT (w->last_point)
11390 /* Make sure the cursor was last displayed
11391 in this window. Otherwise we have to reposition it. */
11392 && 0 <= w->cursor.vpos
11393 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11394 {
11395 if (!must_finish)
11396 {
11397 do_pending_window_change (1);
11398
11399 /* We used to always goto end_of_redisplay here, but this
11400 isn't enough if we have a blinking cursor. */
11401 if (w->cursor_off_p == w->last_cursor_off_p)
11402 goto end_of_redisplay;
11403 }
11404 goto update;
11405 }
11406 /* If highlighting the region, or if the cursor is in the echo area,
11407 then we can't just move the cursor. */
11408 else if (! (!NILP (Vtransient_mark_mode)
11409 && !NILP (current_buffer->mark_active))
11410 && (EQ (selected_window, current_buffer->last_selected_window)
11411 || highlight_nonselected_windows)
11412 && NILP (w->region_showing)
11413 && NILP (Vshow_trailing_whitespace)
11414 && !cursor_in_echo_area)
11415 {
11416 struct it it;
11417 struct glyph_row *row;
11418
11419 /* Skip from tlbufpos to PT and see where it is. Note that
11420 PT may be in invisible text. If so, we will end at the
11421 next visible position. */
11422 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11423 NULL, DEFAULT_FACE_ID);
11424 it.current_x = this_line_start_x;
11425 it.current_y = this_line_y;
11426 it.vpos = this_line_vpos;
11427
11428 /* The call to move_it_to stops in front of PT, but
11429 moves over before-strings. */
11430 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11431
11432 if (it.vpos == this_line_vpos
11433 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11434 row->enabled_p))
11435 {
11436 xassert (this_line_vpos == it.vpos);
11437 xassert (this_line_y == it.current_y);
11438 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11439 #if GLYPH_DEBUG
11440 *w->desired_matrix->method = 0;
11441 debug_method_add (w, "optimization 3");
11442 #endif
11443 goto update;
11444 }
11445 else
11446 goto cancel;
11447 }
11448
11449 cancel:
11450 /* Text changed drastically or point moved off of line. */
11451 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11452 }
11453
11454 CHARPOS (this_line_start_pos) = 0;
11455 consider_all_windows_p |= buffer_shared > 1;
11456 ++clear_face_cache_count;
11457 #ifdef HAVE_WINDOW_SYSTEM
11458 ++clear_image_cache_count;
11459 #endif
11460
11461 /* Build desired matrices, and update the display. If
11462 consider_all_windows_p is non-zero, do it for all windows on all
11463 frames. Otherwise do it for selected_window, only. */
11464
11465 if (consider_all_windows_p)
11466 {
11467 Lisp_Object tail, frame;
11468
11469 FOR_EACH_FRAME (tail, frame)
11470 XFRAME (frame)->updated_p = 0;
11471
11472 /* Recompute # windows showing selected buffer. This will be
11473 incremented each time such a window is displayed. */
11474 buffer_shared = 0;
11475
11476 FOR_EACH_FRAME (tail, frame)
11477 {
11478 struct frame *f = XFRAME (frame);
11479
11480 if (FRAME_WINDOW_P (f) || f == sf)
11481 {
11482 if (! EQ (frame, selected_frame))
11483 /* Select the frame, for the sake of frame-local
11484 variables. */
11485 select_frame_for_redisplay (frame);
11486
11487 /* Mark all the scroll bars to be removed; we'll redeem
11488 the ones we want when we redisplay their windows. */
11489 if (condemn_scroll_bars_hook)
11490 condemn_scroll_bars_hook (f);
11491
11492 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11493 redisplay_windows (FRAME_ROOT_WINDOW (f));
11494
11495 /* Any scroll bars which redisplay_windows should have
11496 nuked should now go away. */
11497 if (judge_scroll_bars_hook)
11498 judge_scroll_bars_hook (f);
11499
11500 /* If fonts changed, display again. */
11501 /* ??? rms: I suspect it is a mistake to jump all the way
11502 back to retry here. It should just retry this frame. */
11503 if (fonts_changed_p)
11504 goto retry;
11505
11506 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11507 {
11508 /* See if we have to hscroll. */
11509 if (!f->already_hscrolled_p)
11510 {
11511 f->already_hscrolled_p = 1;
11512 if (hscroll_windows (f->root_window))
11513 goto retry;
11514 }
11515
11516 /* Prevent various kinds of signals during display
11517 update. stdio is not robust about handling
11518 signals, which can cause an apparent I/O
11519 error. */
11520 if (interrupt_input)
11521 unrequest_sigio ();
11522 STOP_POLLING;
11523
11524 /* Update the display. */
11525 set_window_update_flags (XWINDOW (f->root_window), 1);
11526 pause |= update_frame (f, 0, 0);
11527 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11528 if (pause)
11529 break;
11530 #endif
11531
11532 f->updated_p = 1;
11533 }
11534 }
11535 }
11536
11537 if (!pause)
11538 {
11539 /* Do the mark_window_display_accurate after all windows have
11540 been redisplayed because this call resets flags in buffers
11541 which are needed for proper redisplay. */
11542 FOR_EACH_FRAME (tail, frame)
11543 {
11544 struct frame *f = XFRAME (frame);
11545 if (f->updated_p)
11546 {
11547 mark_window_display_accurate (f->root_window, 1);
11548 if (frame_up_to_date_hook)
11549 frame_up_to_date_hook (f);
11550 }
11551 }
11552 }
11553 }
11554 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11555 {
11556 Lisp_Object mini_window;
11557 struct frame *mini_frame;
11558
11559 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11560 /* Use list_of_error, not Qerror, so that
11561 we catch only errors and don't run the debugger. */
11562 internal_condition_case_1 (redisplay_window_1, selected_window,
11563 list_of_error,
11564 redisplay_window_error);
11565
11566 /* Compare desired and current matrices, perform output. */
11567
11568 update:
11569 /* If fonts changed, display again. */
11570 if (fonts_changed_p)
11571 goto retry;
11572
11573 /* Prevent various kinds of signals during display update.
11574 stdio is not robust about handling signals,
11575 which can cause an apparent I/O error. */
11576 if (interrupt_input)
11577 unrequest_sigio ();
11578 STOP_POLLING;
11579
11580 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11581 {
11582 if (hscroll_windows (selected_window))
11583 goto retry;
11584
11585 XWINDOW (selected_window)->must_be_updated_p = 1;
11586 pause = update_frame (sf, 0, 0);
11587 }
11588
11589 /* We may have called echo_area_display at the top of this
11590 function. If the echo area is on another frame, that may
11591 have put text on a frame other than the selected one, so the
11592 above call to update_frame would not have caught it. Catch
11593 it here. */
11594 mini_window = FRAME_MINIBUF_WINDOW (sf);
11595 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11596
11597 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11598 {
11599 XWINDOW (mini_window)->must_be_updated_p = 1;
11600 pause |= update_frame (mini_frame, 0, 0);
11601 if (!pause && hscroll_windows (mini_window))
11602 goto retry;
11603 }
11604 }
11605
11606 /* If display was paused because of pending input, make sure we do a
11607 thorough update the next time. */
11608 if (pause)
11609 {
11610 /* Prevent the optimization at the beginning of
11611 redisplay_internal that tries a single-line update of the
11612 line containing the cursor in the selected window. */
11613 CHARPOS (this_line_start_pos) = 0;
11614
11615 /* Let the overlay arrow be updated the next time. */
11616 update_overlay_arrows (0);
11617
11618 /* If we pause after scrolling, some rows in the current
11619 matrices of some windows are not valid. */
11620 if (!WINDOW_FULL_WIDTH_P (w)
11621 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11622 update_mode_lines = 1;
11623 }
11624 else
11625 {
11626 if (!consider_all_windows_p)
11627 {
11628 /* This has already been done above if
11629 consider_all_windows_p is set. */
11630 mark_window_display_accurate_1 (w, 1);
11631
11632 /* Say overlay arrows are up to date. */
11633 update_overlay_arrows (1);
11634
11635 if (frame_up_to_date_hook != 0)
11636 frame_up_to_date_hook (sf);
11637 }
11638
11639 update_mode_lines = 0;
11640 windows_or_buffers_changed = 0;
11641 cursor_type_changed = 0;
11642 }
11643
11644 /* Start SIGIO interrupts coming again. Having them off during the
11645 code above makes it less likely one will discard output, but not
11646 impossible, since there might be stuff in the system buffer here.
11647 But it is much hairier to try to do anything about that. */
11648 if (interrupt_input)
11649 request_sigio ();
11650 RESUME_POLLING;
11651
11652 /* If a frame has become visible which was not before, redisplay
11653 again, so that we display it. Expose events for such a frame
11654 (which it gets when becoming visible) don't call the parts of
11655 redisplay constructing glyphs, so simply exposing a frame won't
11656 display anything in this case. So, we have to display these
11657 frames here explicitly. */
11658 if (!pause)
11659 {
11660 Lisp_Object tail, frame;
11661 int new_count = 0;
11662
11663 FOR_EACH_FRAME (tail, frame)
11664 {
11665 int this_is_visible = 0;
11666
11667 if (XFRAME (frame)->visible)
11668 this_is_visible = 1;
11669 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11670 if (XFRAME (frame)->visible)
11671 this_is_visible = 1;
11672
11673 if (this_is_visible)
11674 new_count++;
11675 }
11676
11677 if (new_count != number_of_visible_frames)
11678 windows_or_buffers_changed++;
11679 }
11680
11681 /* Change frame size now if a change is pending. */
11682 do_pending_window_change (1);
11683
11684 /* If we just did a pending size change, or have additional
11685 visible frames, redisplay again. */
11686 if (windows_or_buffers_changed && !pause)
11687 goto retry;
11688
11689 /* Clear the face cache eventually. */
11690 if (consider_all_windows_p)
11691 {
11692 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11693 {
11694 clear_face_cache (0);
11695 clear_face_cache_count = 0;
11696 }
11697 #ifdef HAVE_WINDOW_SYSTEM
11698 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11699 {
11700 Lisp_Object tail, frame;
11701 FOR_EACH_FRAME (tail, frame)
11702 {
11703 struct frame *f = XFRAME (frame);
11704 if (FRAME_WINDOW_P (f))
11705 clear_image_cache (f, 0);
11706 }
11707 clear_image_cache_count = 0;
11708 }
11709 #endif /* HAVE_WINDOW_SYSTEM */
11710 }
11711
11712 end_of_redisplay:
11713 unbind_to (count, Qnil);
11714 RESUME_POLLING;
11715 }
11716
11717
11718 /* Redisplay, but leave alone any recent echo area message unless
11719 another message has been requested in its place.
11720
11721 This is useful in situations where you need to redisplay but no
11722 user action has occurred, making it inappropriate for the message
11723 area to be cleared. See tracking_off and
11724 wait_reading_process_output for examples of these situations.
11725
11726 FROM_WHERE is an integer saying from where this function was
11727 called. This is useful for debugging. */
11728
11729 void
11730 redisplay_preserve_echo_area (from_where)
11731 int from_where;
11732 {
11733 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11734
11735 if (!NILP (echo_area_buffer[1]))
11736 {
11737 /* We have a previously displayed message, but no current
11738 message. Redisplay the previous message. */
11739 display_last_displayed_message_p = 1;
11740 redisplay_internal (1);
11741 display_last_displayed_message_p = 0;
11742 }
11743 else
11744 redisplay_internal (1);
11745
11746 if (rif != NULL && rif->flush_display_optional)
11747 rif->flush_display_optional (NULL);
11748 }
11749
11750
11751 /* Function registered with record_unwind_protect in
11752 redisplay_internal. Reset redisplaying_p to the value it had
11753 before redisplay_internal was called, and clear
11754 prevent_freeing_realized_faces_p. It also selects the previously
11755 selected frame. */
11756
11757 static Lisp_Object
11758 unwind_redisplay (val)
11759 Lisp_Object val;
11760 {
11761 Lisp_Object old_redisplaying_p, old_frame;
11762
11763 old_redisplaying_p = XCAR (val);
11764 redisplaying_p = XFASTINT (old_redisplaying_p);
11765 old_frame = XCDR (val);
11766 if (! EQ (old_frame, selected_frame))
11767 select_frame_for_redisplay (old_frame);
11768 return Qnil;
11769 }
11770
11771
11772 /* Mark the display of window W as accurate or inaccurate. If
11773 ACCURATE_P is non-zero mark display of W as accurate. If
11774 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11775 redisplay_internal is called. */
11776
11777 static void
11778 mark_window_display_accurate_1 (w, accurate_p)
11779 struct window *w;
11780 int accurate_p;
11781 {
11782 if (BUFFERP (w->buffer))
11783 {
11784 struct buffer *b = XBUFFER (w->buffer);
11785
11786 w->last_modified
11787 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11788 w->last_overlay_modified
11789 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11790 w->last_had_star
11791 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11792
11793 if (accurate_p)
11794 {
11795 b->clip_changed = 0;
11796 b->prevent_redisplay_optimizations_p = 0;
11797
11798 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11799 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11800 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11801 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11802
11803 w->current_matrix->buffer = b;
11804 w->current_matrix->begv = BUF_BEGV (b);
11805 w->current_matrix->zv = BUF_ZV (b);
11806
11807 w->last_cursor = w->cursor;
11808 w->last_cursor_off_p = w->cursor_off_p;
11809
11810 if (w == XWINDOW (selected_window))
11811 w->last_point = make_number (BUF_PT (b));
11812 else
11813 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11814 }
11815 }
11816
11817 if (accurate_p)
11818 {
11819 w->window_end_valid = w->buffer;
11820 #if 0 /* This is incorrect with variable-height lines. */
11821 xassert (XINT (w->window_end_vpos)
11822 < (WINDOW_TOTAL_LINES (w)
11823 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11824 #endif
11825 w->update_mode_line = Qnil;
11826 }
11827 }
11828
11829
11830 /* Mark the display of windows in the window tree rooted at WINDOW as
11831 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11832 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11833 be redisplayed the next time redisplay_internal is called. */
11834
11835 void
11836 mark_window_display_accurate (window, accurate_p)
11837 Lisp_Object window;
11838 int accurate_p;
11839 {
11840 struct window *w;
11841
11842 for (; !NILP (window); window = w->next)
11843 {
11844 w = XWINDOW (window);
11845 mark_window_display_accurate_1 (w, accurate_p);
11846
11847 if (!NILP (w->vchild))
11848 mark_window_display_accurate (w->vchild, accurate_p);
11849 if (!NILP (w->hchild))
11850 mark_window_display_accurate (w->hchild, accurate_p);
11851 }
11852
11853 if (accurate_p)
11854 {
11855 update_overlay_arrows (1);
11856 }
11857 else
11858 {
11859 /* Force a thorough redisplay the next time by setting
11860 last_arrow_position and last_arrow_string to t, which is
11861 unequal to any useful value of Voverlay_arrow_... */
11862 update_overlay_arrows (-1);
11863 }
11864 }
11865
11866
11867 /* Return value in display table DP (Lisp_Char_Table *) for character
11868 C. Since a display table doesn't have any parent, we don't have to
11869 follow parent. Do not call this function directly but use the
11870 macro DISP_CHAR_VECTOR. */
11871
11872 Lisp_Object
11873 disp_char_vector (dp, c)
11874 struct Lisp_Char_Table *dp;
11875 int c;
11876 {
11877 Lisp_Object val;
11878
11879 if (ASCII_CHAR_P (c))
11880 {
11881 val = dp->ascii;
11882 if (SUB_CHAR_TABLE_P (val))
11883 val = XSUB_CHAR_TABLE (val)->contents[c];
11884 }
11885 else
11886 {
11887 Lisp_Object table;
11888
11889 XSETCHAR_TABLE (table, dp);
11890 val = char_table_ref (table, c);
11891 }
11892 if (NILP (val))
11893 val = dp->defalt;
11894 return val;
11895 }
11896
11897
11898 \f
11899 /***********************************************************************
11900 Window Redisplay
11901 ***********************************************************************/
11902
11903 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11904
11905 static void
11906 redisplay_windows (window)
11907 Lisp_Object window;
11908 {
11909 while (!NILP (window))
11910 {
11911 struct window *w = XWINDOW (window);
11912
11913 if (!NILP (w->hchild))
11914 redisplay_windows (w->hchild);
11915 else if (!NILP (w->vchild))
11916 redisplay_windows (w->vchild);
11917 else
11918 {
11919 displayed_buffer = XBUFFER (w->buffer);
11920 /* Use list_of_error, not Qerror, so that
11921 we catch only errors and don't run the debugger. */
11922 internal_condition_case_1 (redisplay_window_0, window,
11923 list_of_error,
11924 redisplay_window_error);
11925 }
11926
11927 window = w->next;
11928 }
11929 }
11930
11931 static Lisp_Object
11932 redisplay_window_error ()
11933 {
11934 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11935 return Qnil;
11936 }
11937
11938 static Lisp_Object
11939 redisplay_window_0 (window)
11940 Lisp_Object window;
11941 {
11942 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11943 redisplay_window (window, 0);
11944 return Qnil;
11945 }
11946
11947 static Lisp_Object
11948 redisplay_window_1 (window)
11949 Lisp_Object window;
11950 {
11951 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11952 redisplay_window (window, 1);
11953 return Qnil;
11954 }
11955 \f
11956
11957 /* Increment GLYPH until it reaches END or CONDITION fails while
11958 adding (GLYPH)->pixel_width to X. */
11959
11960 #define SKIP_GLYPHS(glyph, end, x, condition) \
11961 do \
11962 { \
11963 (x) += (glyph)->pixel_width; \
11964 ++(glyph); \
11965 } \
11966 while ((glyph) < (end) && (condition))
11967
11968
11969 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11970 DELTA is the number of bytes by which positions recorded in ROW
11971 differ from current buffer positions.
11972
11973 Return 0 if cursor is not on this row. 1 otherwise. */
11974
11975 int
11976 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11977 struct window *w;
11978 struct glyph_row *row;
11979 struct glyph_matrix *matrix;
11980 int delta, delta_bytes, dy, dvpos;
11981 {
11982 struct glyph *glyph = row->glyphs[TEXT_AREA];
11983 struct glyph *end = glyph + row->used[TEXT_AREA];
11984 struct glyph *cursor = NULL;
11985 /* The first glyph that starts a sequence of glyphs from string. */
11986 struct glyph *string_start;
11987 /* The X coordinate of string_start. */
11988 int string_start_x;
11989 /* The last known character position. */
11990 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11991 /* The last known character position before string_start. */
11992 int string_before_pos;
11993 int x = row->x;
11994 int cursor_x = x;
11995 int cursor_from_overlay_pos = 0;
11996 int pt_old = PT - delta;
11997
11998 /* Skip over glyphs not having an object at the start of the row.
11999 These are special glyphs like truncation marks on terminal
12000 frames. */
12001 if (row->displays_text_p)
12002 while (glyph < end
12003 && INTEGERP (glyph->object)
12004 && glyph->charpos < 0)
12005 {
12006 x += glyph->pixel_width;
12007 ++glyph;
12008 }
12009
12010 string_start = NULL;
12011 while (glyph < end
12012 && !INTEGERP (glyph->object)
12013 && (!BUFFERP (glyph->object)
12014 || (last_pos = glyph->charpos) < pt_old))
12015 {
12016 if (! STRINGP (glyph->object))
12017 {
12018 string_start = NULL;
12019 x += glyph->pixel_width;
12020 ++glyph;
12021 if (cursor_from_overlay_pos
12022 && last_pos >= cursor_from_overlay_pos)
12023 {
12024 cursor_from_overlay_pos = 0;
12025 cursor = 0;
12026 }
12027 }
12028 else
12029 {
12030 if (string_start == NULL)
12031 {
12032 string_before_pos = last_pos;
12033 string_start = glyph;
12034 string_start_x = x;
12035 }
12036 /* Skip all glyphs from string. */
12037 do
12038 {
12039 Lisp_Object cprop;
12040 int pos;
12041 if ((cursor == NULL || glyph > cursor)
12042 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12043 Qcursor, (glyph)->object),
12044 !NILP (cprop))
12045 && (pos = string_buffer_position (w, glyph->object,
12046 string_before_pos),
12047 (pos == 0 /* From overlay */
12048 || pos == pt_old)))
12049 {
12050 /* Estimate overlay buffer position from the buffer
12051 positions of the glyphs before and after the overlay.
12052 Add 1 to last_pos so that if point corresponds to the
12053 glyph right after the overlay, we still use a 'cursor'
12054 property found in that overlay. */
12055 cursor_from_overlay_pos = (pos ? 0 : last_pos
12056 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12057 cursor = glyph;
12058 cursor_x = x;
12059 }
12060 x += glyph->pixel_width;
12061 ++glyph;
12062 }
12063 while (glyph < end && EQ (glyph->object, string_start->object));
12064 }
12065 }
12066
12067 if (cursor != NULL)
12068 {
12069 glyph = cursor;
12070 x = cursor_x;
12071 }
12072 else if (row->ends_in_ellipsis_p && glyph == end)
12073 {
12074 /* Scan back over the ellipsis glyphs, decrementing positions. */
12075 while (glyph > row->glyphs[TEXT_AREA]
12076 && (glyph - 1)->charpos == last_pos)
12077 glyph--, x -= glyph->pixel_width;
12078 /* That loop always goes one position too far,
12079 including the glyph before the ellipsis.
12080 So scan forward over that one. */
12081 x += glyph->pixel_width;
12082 glyph++;
12083 }
12084 else if (string_start
12085 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12086 {
12087 /* We may have skipped over point because the previous glyphs
12088 are from string. As there's no easy way to know the
12089 character position of the current glyph, find the correct
12090 glyph on point by scanning from string_start again. */
12091 Lisp_Object limit;
12092 Lisp_Object string;
12093 struct glyph *stop = glyph;
12094 int pos;
12095
12096 limit = make_number (pt_old + 1);
12097 glyph = string_start;
12098 x = string_start_x;
12099 string = glyph->object;
12100 pos = string_buffer_position (w, string, string_before_pos);
12101 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12102 because we always put cursor after overlay strings. */
12103 while (pos == 0 && glyph < stop)
12104 {
12105 string = glyph->object;
12106 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12107 if (glyph < stop)
12108 pos = string_buffer_position (w, glyph->object, string_before_pos);
12109 }
12110
12111 while (glyph < stop)
12112 {
12113 pos = XINT (Fnext_single_char_property_change
12114 (make_number (pos), Qdisplay, Qnil, limit));
12115 if (pos > pt_old)
12116 break;
12117 /* Skip glyphs from the same string. */
12118 string = glyph->object;
12119 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12120 /* Skip glyphs from an overlay. */
12121 while (glyph < stop
12122 && ! string_buffer_position (w, glyph->object, pos))
12123 {
12124 string = glyph->object;
12125 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12126 }
12127 }
12128
12129 /* If we reached the end of the line, and end was from a string,
12130 cursor is not on this line. */
12131 if (glyph == end && row->continued_p)
12132 return 0;
12133 }
12134
12135 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12136 w->cursor.x = x;
12137 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12138 w->cursor.y = row->y + dy;
12139
12140 if (w == XWINDOW (selected_window))
12141 {
12142 if (!row->continued_p
12143 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12144 && row->x == 0)
12145 {
12146 this_line_buffer = XBUFFER (w->buffer);
12147
12148 CHARPOS (this_line_start_pos)
12149 = MATRIX_ROW_START_CHARPOS (row) + delta;
12150 BYTEPOS (this_line_start_pos)
12151 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12152
12153 CHARPOS (this_line_end_pos)
12154 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12155 BYTEPOS (this_line_end_pos)
12156 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12157
12158 this_line_y = w->cursor.y;
12159 this_line_pixel_height = row->height;
12160 this_line_vpos = w->cursor.vpos;
12161 this_line_start_x = row->x;
12162 }
12163 else
12164 CHARPOS (this_line_start_pos) = 0;
12165 }
12166
12167 return 1;
12168 }
12169
12170
12171 /* Run window scroll functions, if any, for WINDOW with new window
12172 start STARTP. Sets the window start of WINDOW to that position.
12173
12174 We assume that the window's buffer is really current. */
12175
12176 static INLINE struct text_pos
12177 run_window_scroll_functions (window, startp)
12178 Lisp_Object window;
12179 struct text_pos startp;
12180 {
12181 struct window *w = XWINDOW (window);
12182 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12183
12184 if (current_buffer != XBUFFER (w->buffer))
12185 abort ();
12186
12187 if (!NILP (Vwindow_scroll_functions))
12188 {
12189 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12190 make_number (CHARPOS (startp)));
12191 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12192 /* In case the hook functions switch buffers. */
12193 if (current_buffer != XBUFFER (w->buffer))
12194 set_buffer_internal_1 (XBUFFER (w->buffer));
12195 }
12196
12197 return startp;
12198 }
12199
12200
12201 /* Make sure the line containing the cursor is fully visible.
12202 A value of 1 means there is nothing to be done.
12203 (Either the line is fully visible, or it cannot be made so,
12204 or we cannot tell.)
12205
12206 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12207 is higher than window.
12208
12209 A value of 0 means the caller should do scrolling
12210 as if point had gone off the screen. */
12211
12212 static int
12213 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12214 struct window *w;
12215 int force_p;
12216 int current_matrix_p;
12217 {
12218 struct glyph_matrix *matrix;
12219 struct glyph_row *row;
12220 int window_height;
12221
12222 if (!make_cursor_line_fully_visible_p)
12223 return 1;
12224
12225 /* It's not always possible to find the cursor, e.g, when a window
12226 is full of overlay strings. Don't do anything in that case. */
12227 if (w->cursor.vpos < 0)
12228 return 1;
12229
12230 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12231 row = MATRIX_ROW (matrix, w->cursor.vpos);
12232
12233 /* If the cursor row is not partially visible, there's nothing to do. */
12234 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12235 return 1;
12236
12237 /* If the row the cursor is in is taller than the window's height,
12238 it's not clear what to do, so do nothing. */
12239 window_height = window_box_height (w);
12240 if (row->height >= window_height)
12241 {
12242 if (!force_p || MINI_WINDOW_P (w)
12243 || w->vscroll || w->cursor.vpos == 0)
12244 return 1;
12245 }
12246 return 0;
12247
12248 #if 0
12249 /* This code used to try to scroll the window just enough to make
12250 the line visible. It returned 0 to say that the caller should
12251 allocate larger glyph matrices. */
12252
12253 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12254 {
12255 int dy = row->height - row->visible_height;
12256 w->vscroll = 0;
12257 w->cursor.y += dy;
12258 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12259 }
12260 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12261 {
12262 int dy = - (row->height - row->visible_height);
12263 w->vscroll = dy;
12264 w->cursor.y += dy;
12265 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12266 }
12267
12268 /* When we change the cursor y-position of the selected window,
12269 change this_line_y as well so that the display optimization for
12270 the cursor line of the selected window in redisplay_internal uses
12271 the correct y-position. */
12272 if (w == XWINDOW (selected_window))
12273 this_line_y = w->cursor.y;
12274
12275 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12276 redisplay with larger matrices. */
12277 if (matrix->nrows < required_matrix_height (w))
12278 {
12279 fonts_changed_p = 1;
12280 return 0;
12281 }
12282
12283 return 1;
12284 #endif /* 0 */
12285 }
12286
12287
12288 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12289 non-zero means only WINDOW is redisplayed in redisplay_internal.
12290 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12291 in redisplay_window to bring a partially visible line into view in
12292 the case that only the cursor has moved.
12293
12294 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12295 last screen line's vertical height extends past the end of the screen.
12296
12297 Value is
12298
12299 1 if scrolling succeeded
12300
12301 0 if scrolling didn't find point.
12302
12303 -1 if new fonts have been loaded so that we must interrupt
12304 redisplay, adjust glyph matrices, and try again. */
12305
12306 enum
12307 {
12308 SCROLLING_SUCCESS,
12309 SCROLLING_FAILED,
12310 SCROLLING_NEED_LARGER_MATRICES
12311 };
12312
12313 static int
12314 try_scrolling (window, just_this_one_p, scroll_conservatively,
12315 scroll_step, temp_scroll_step, last_line_misfit)
12316 Lisp_Object window;
12317 int just_this_one_p;
12318 EMACS_INT scroll_conservatively, scroll_step;
12319 int temp_scroll_step;
12320 int last_line_misfit;
12321 {
12322 struct window *w = XWINDOW (window);
12323 struct frame *f = XFRAME (w->frame);
12324 struct text_pos scroll_margin_pos;
12325 struct text_pos pos;
12326 struct text_pos startp;
12327 struct it it;
12328 Lisp_Object window_end;
12329 int this_scroll_margin;
12330 int dy = 0;
12331 int scroll_max;
12332 int rc;
12333 int amount_to_scroll = 0;
12334 Lisp_Object aggressive;
12335 int height;
12336 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12337
12338 #if GLYPH_DEBUG
12339 debug_method_add (w, "try_scrolling");
12340 #endif
12341
12342 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12343
12344 /* Compute scroll margin height in pixels. We scroll when point is
12345 within this distance from the top or bottom of the window. */
12346 if (scroll_margin > 0)
12347 {
12348 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12349 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12350 }
12351 else
12352 this_scroll_margin = 0;
12353
12354 /* Force scroll_conservatively to have a reasonable value so it doesn't
12355 cause an overflow while computing how much to scroll. */
12356 if (scroll_conservatively)
12357 scroll_conservatively = min (scroll_conservatively,
12358 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12359
12360 /* Compute how much we should try to scroll maximally to bring point
12361 into view. */
12362 if (scroll_step || scroll_conservatively || temp_scroll_step)
12363 scroll_max = max (scroll_step,
12364 max (scroll_conservatively, temp_scroll_step));
12365 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12366 || NUMBERP (current_buffer->scroll_up_aggressively))
12367 /* We're trying to scroll because of aggressive scrolling
12368 but no scroll_step is set. Choose an arbitrary one. Maybe
12369 there should be a variable for this. */
12370 scroll_max = 10;
12371 else
12372 scroll_max = 0;
12373 scroll_max *= FRAME_LINE_HEIGHT (f);
12374
12375 /* Decide whether we have to scroll down. Start at the window end
12376 and move this_scroll_margin up to find the position of the scroll
12377 margin. */
12378 window_end = Fwindow_end (window, Qt);
12379
12380 too_near_end:
12381
12382 CHARPOS (scroll_margin_pos) = XINT (window_end);
12383 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12384
12385 if (this_scroll_margin || extra_scroll_margin_lines)
12386 {
12387 start_display (&it, w, scroll_margin_pos);
12388 if (this_scroll_margin)
12389 move_it_vertically_backward (&it, this_scroll_margin);
12390 if (extra_scroll_margin_lines)
12391 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12392 scroll_margin_pos = it.current.pos;
12393 }
12394
12395 if (PT >= CHARPOS (scroll_margin_pos))
12396 {
12397 int y0;
12398
12399 /* Point is in the scroll margin at the bottom of the window, or
12400 below. Compute a new window start that makes point visible. */
12401
12402 /* Compute the distance from the scroll margin to PT.
12403 Give up if the distance is greater than scroll_max. */
12404 start_display (&it, w, scroll_margin_pos);
12405 y0 = it.current_y;
12406 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12407 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12408
12409 /* To make point visible, we have to move the window start
12410 down so that the line the cursor is in is visible, which
12411 means we have to add in the height of the cursor line. */
12412 dy = line_bottom_y (&it) - y0;
12413
12414 if (dy > scroll_max)
12415 return SCROLLING_FAILED;
12416
12417 /* Move the window start down. If scrolling conservatively,
12418 move it just enough down to make point visible. If
12419 scroll_step is set, move it down by scroll_step. */
12420 start_display (&it, w, startp);
12421
12422 if (scroll_conservatively)
12423 /* Set AMOUNT_TO_SCROLL to at least one line,
12424 and at most scroll_conservatively lines. */
12425 amount_to_scroll
12426 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12427 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12428 else if (scroll_step || temp_scroll_step)
12429 amount_to_scroll = scroll_max;
12430 else
12431 {
12432 aggressive = current_buffer->scroll_up_aggressively;
12433 height = WINDOW_BOX_TEXT_HEIGHT (w);
12434 if (NUMBERP (aggressive))
12435 {
12436 double float_amount = XFLOATINT (aggressive) * height;
12437 amount_to_scroll = float_amount;
12438 if (amount_to_scroll == 0 && float_amount > 0)
12439 amount_to_scroll = 1;
12440 }
12441 }
12442
12443 if (amount_to_scroll <= 0)
12444 return SCROLLING_FAILED;
12445
12446 /* If moving by amount_to_scroll leaves STARTP unchanged,
12447 move it down one screen line. */
12448
12449 move_it_vertically (&it, amount_to_scroll);
12450 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12451 move_it_by_lines (&it, 1, 1);
12452 startp = it.current.pos;
12453 }
12454 else
12455 {
12456 /* See if point is inside the scroll margin at the top of the
12457 window. */
12458 scroll_margin_pos = startp;
12459 if (this_scroll_margin)
12460 {
12461 start_display (&it, w, startp);
12462 move_it_vertically (&it, this_scroll_margin);
12463 scroll_margin_pos = it.current.pos;
12464 }
12465
12466 if (PT < CHARPOS (scroll_margin_pos))
12467 {
12468 /* Point is in the scroll margin at the top of the window or
12469 above what is displayed in the window. */
12470 int y0;
12471
12472 /* Compute the vertical distance from PT to the scroll
12473 margin position. Give up if distance is greater than
12474 scroll_max. */
12475 SET_TEXT_POS (pos, PT, PT_BYTE);
12476 start_display (&it, w, pos);
12477 y0 = it.current_y;
12478 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12479 it.last_visible_y, -1,
12480 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12481 dy = it.current_y - y0;
12482 if (dy > scroll_max)
12483 return SCROLLING_FAILED;
12484
12485 /* Compute new window start. */
12486 start_display (&it, w, startp);
12487
12488 if (scroll_conservatively)
12489 amount_to_scroll
12490 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12491 else if (scroll_step || temp_scroll_step)
12492 amount_to_scroll = scroll_max;
12493 else
12494 {
12495 aggressive = current_buffer->scroll_down_aggressively;
12496 height = WINDOW_BOX_TEXT_HEIGHT (w);
12497 if (NUMBERP (aggressive))
12498 {
12499 double float_amount = XFLOATINT (aggressive) * height;
12500 amount_to_scroll = float_amount;
12501 if (amount_to_scroll == 0 && float_amount > 0)
12502 amount_to_scroll = 1;
12503 }
12504 }
12505
12506 if (amount_to_scroll <= 0)
12507 return SCROLLING_FAILED;
12508
12509 move_it_vertically_backward (&it, amount_to_scroll);
12510 startp = it.current.pos;
12511 }
12512 }
12513
12514 /* Run window scroll functions. */
12515 startp = run_window_scroll_functions (window, startp);
12516
12517 /* Display the window. Give up if new fonts are loaded, or if point
12518 doesn't appear. */
12519 if (!try_window (window, startp, 0))
12520 rc = SCROLLING_NEED_LARGER_MATRICES;
12521 else if (w->cursor.vpos < 0)
12522 {
12523 clear_glyph_matrix (w->desired_matrix);
12524 rc = SCROLLING_FAILED;
12525 }
12526 else
12527 {
12528 /* Maybe forget recorded base line for line number display. */
12529 if (!just_this_one_p
12530 || current_buffer->clip_changed
12531 || BEG_UNCHANGED < CHARPOS (startp))
12532 w->base_line_number = Qnil;
12533
12534 /* If cursor ends up on a partially visible line,
12535 treat that as being off the bottom of the screen. */
12536 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12537 {
12538 clear_glyph_matrix (w->desired_matrix);
12539 ++extra_scroll_margin_lines;
12540 goto too_near_end;
12541 }
12542 rc = SCROLLING_SUCCESS;
12543 }
12544
12545 return rc;
12546 }
12547
12548
12549 /* Compute a suitable window start for window W if display of W starts
12550 on a continuation line. Value is non-zero if a new window start
12551 was computed.
12552
12553 The new window start will be computed, based on W's width, starting
12554 from the start of the continued line. It is the start of the
12555 screen line with the minimum distance from the old start W->start. */
12556
12557 static int
12558 compute_window_start_on_continuation_line (w)
12559 struct window *w;
12560 {
12561 struct text_pos pos, start_pos;
12562 int window_start_changed_p = 0;
12563
12564 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12565
12566 /* If window start is on a continuation line... Window start may be
12567 < BEGV in case there's invisible text at the start of the
12568 buffer (M-x rmail, for example). */
12569 if (CHARPOS (start_pos) > BEGV
12570 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12571 {
12572 struct it it;
12573 struct glyph_row *row;
12574
12575 /* Handle the case that the window start is out of range. */
12576 if (CHARPOS (start_pos) < BEGV)
12577 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12578 else if (CHARPOS (start_pos) > ZV)
12579 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12580
12581 /* Find the start of the continued line. This should be fast
12582 because scan_buffer is fast (newline cache). */
12583 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12584 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12585 row, DEFAULT_FACE_ID);
12586 reseat_at_previous_visible_line_start (&it);
12587
12588 /* If the line start is "too far" away from the window start,
12589 say it takes too much time to compute a new window start. */
12590 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12591 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12592 {
12593 int min_distance, distance;
12594
12595 /* Move forward by display lines to find the new window
12596 start. If window width was enlarged, the new start can
12597 be expected to be > the old start. If window width was
12598 decreased, the new window start will be < the old start.
12599 So, we're looking for the display line start with the
12600 minimum distance from the old window start. */
12601 pos = it.current.pos;
12602 min_distance = INFINITY;
12603 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12604 distance < min_distance)
12605 {
12606 min_distance = distance;
12607 pos = it.current.pos;
12608 move_it_by_lines (&it, 1, 0);
12609 }
12610
12611 /* Set the window start there. */
12612 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12613 window_start_changed_p = 1;
12614 }
12615 }
12616
12617 return window_start_changed_p;
12618 }
12619
12620
12621 /* Try cursor movement in case text has not changed in window WINDOW,
12622 with window start STARTP. Value is
12623
12624 CURSOR_MOVEMENT_SUCCESS if successful
12625
12626 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12627
12628 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12629 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12630 we want to scroll as if scroll-step were set to 1. See the code.
12631
12632 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12633 which case we have to abort this redisplay, and adjust matrices
12634 first. */
12635
12636 enum
12637 {
12638 CURSOR_MOVEMENT_SUCCESS,
12639 CURSOR_MOVEMENT_CANNOT_BE_USED,
12640 CURSOR_MOVEMENT_MUST_SCROLL,
12641 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12642 };
12643
12644 static int
12645 try_cursor_movement (window, startp, scroll_step)
12646 Lisp_Object window;
12647 struct text_pos startp;
12648 int *scroll_step;
12649 {
12650 struct window *w = XWINDOW (window);
12651 struct frame *f = XFRAME (w->frame);
12652 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12653
12654 #if GLYPH_DEBUG
12655 if (inhibit_try_cursor_movement)
12656 return rc;
12657 #endif
12658
12659 /* Handle case where text has not changed, only point, and it has
12660 not moved off the frame. */
12661 if (/* Point may be in this window. */
12662 PT >= CHARPOS (startp)
12663 /* Selective display hasn't changed. */
12664 && !current_buffer->clip_changed
12665 /* Function force-mode-line-update is used to force a thorough
12666 redisplay. It sets either windows_or_buffers_changed or
12667 update_mode_lines. So don't take a shortcut here for these
12668 cases. */
12669 && !update_mode_lines
12670 && !windows_or_buffers_changed
12671 && !cursor_type_changed
12672 /* Can't use this case if highlighting a region. When a
12673 region exists, cursor movement has to do more than just
12674 set the cursor. */
12675 && !(!NILP (Vtransient_mark_mode)
12676 && !NILP (current_buffer->mark_active))
12677 && NILP (w->region_showing)
12678 && NILP (Vshow_trailing_whitespace)
12679 /* Right after splitting windows, last_point may be nil. */
12680 && INTEGERP (w->last_point)
12681 /* This code is not used for mini-buffer for the sake of the case
12682 of redisplaying to replace an echo area message; since in
12683 that case the mini-buffer contents per se are usually
12684 unchanged. This code is of no real use in the mini-buffer
12685 since the handling of this_line_start_pos, etc., in redisplay
12686 handles the same cases. */
12687 && !EQ (window, minibuf_window)
12688 /* When splitting windows or for new windows, it happens that
12689 redisplay is called with a nil window_end_vpos or one being
12690 larger than the window. This should really be fixed in
12691 window.c. I don't have this on my list, now, so we do
12692 approximately the same as the old redisplay code. --gerd. */
12693 && INTEGERP (w->window_end_vpos)
12694 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12695 && (FRAME_WINDOW_P (f)
12696 || !overlay_arrow_in_current_buffer_p ()))
12697 {
12698 int this_scroll_margin, top_scroll_margin;
12699 struct glyph_row *row = NULL;
12700
12701 #if GLYPH_DEBUG
12702 debug_method_add (w, "cursor movement");
12703 #endif
12704
12705 /* Scroll if point within this distance from the top or bottom
12706 of the window. This is a pixel value. */
12707 this_scroll_margin = max (0, scroll_margin);
12708 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12709 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12710
12711 top_scroll_margin = this_scroll_margin;
12712 if (WINDOW_WANTS_HEADER_LINE_P (w))
12713 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12714
12715 /* Start with the row the cursor was displayed during the last
12716 not paused redisplay. Give up if that row is not valid. */
12717 if (w->last_cursor.vpos < 0
12718 || w->last_cursor.vpos >= w->current_matrix->nrows)
12719 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12720 else
12721 {
12722 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12723 if (row->mode_line_p)
12724 ++row;
12725 if (!row->enabled_p)
12726 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12727 }
12728
12729 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12730 {
12731 int scroll_p = 0;
12732 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12733
12734 if (PT > XFASTINT (w->last_point))
12735 {
12736 /* Point has moved forward. */
12737 while (MATRIX_ROW_END_CHARPOS (row) < PT
12738 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12739 {
12740 xassert (row->enabled_p);
12741 ++row;
12742 }
12743
12744 /* The end position of a row equals the start position
12745 of the next row. If PT is there, we would rather
12746 display it in the next line. */
12747 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12748 && MATRIX_ROW_END_CHARPOS (row) == PT
12749 && !cursor_row_p (w, row))
12750 ++row;
12751
12752 /* If within the scroll margin, scroll. Note that
12753 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12754 the next line would be drawn, and that
12755 this_scroll_margin can be zero. */
12756 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12757 || PT > MATRIX_ROW_END_CHARPOS (row)
12758 /* Line is completely visible last line in window
12759 and PT is to be set in the next line. */
12760 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12761 && PT == MATRIX_ROW_END_CHARPOS (row)
12762 && !row->ends_at_zv_p
12763 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12764 scroll_p = 1;
12765 }
12766 else if (PT < XFASTINT (w->last_point))
12767 {
12768 /* Cursor has to be moved backward. Note that PT >=
12769 CHARPOS (startp) because of the outer if-statement. */
12770 while (!row->mode_line_p
12771 && (MATRIX_ROW_START_CHARPOS (row) > PT
12772 || (MATRIX_ROW_START_CHARPOS (row) == PT
12773 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12774 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12775 row > w->current_matrix->rows
12776 && (row-1)->ends_in_newline_from_string_p))))
12777 && (row->y > top_scroll_margin
12778 || CHARPOS (startp) == BEGV))
12779 {
12780 xassert (row->enabled_p);
12781 --row;
12782 }
12783
12784 /* Consider the following case: Window starts at BEGV,
12785 there is invisible, intangible text at BEGV, so that
12786 display starts at some point START > BEGV. It can
12787 happen that we are called with PT somewhere between
12788 BEGV and START. Try to handle that case. */
12789 if (row < w->current_matrix->rows
12790 || row->mode_line_p)
12791 {
12792 row = w->current_matrix->rows;
12793 if (row->mode_line_p)
12794 ++row;
12795 }
12796
12797 /* Due to newlines in overlay strings, we may have to
12798 skip forward over overlay strings. */
12799 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12800 && MATRIX_ROW_END_CHARPOS (row) == PT
12801 && !cursor_row_p (w, row))
12802 ++row;
12803
12804 /* If within the scroll margin, scroll. */
12805 if (row->y < top_scroll_margin
12806 && CHARPOS (startp) != BEGV)
12807 scroll_p = 1;
12808 }
12809 else
12810 {
12811 /* Cursor did not move. So don't scroll even if cursor line
12812 is partially visible, as it was so before. */
12813 rc = CURSOR_MOVEMENT_SUCCESS;
12814 }
12815
12816 if (PT < MATRIX_ROW_START_CHARPOS (row)
12817 || PT > MATRIX_ROW_END_CHARPOS (row))
12818 {
12819 /* if PT is not in the glyph row, give up. */
12820 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12821 }
12822 else if (rc != CURSOR_MOVEMENT_SUCCESS
12823 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12824 && make_cursor_line_fully_visible_p)
12825 {
12826 if (PT == MATRIX_ROW_END_CHARPOS (row)
12827 && !row->ends_at_zv_p
12828 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12829 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12830 else if (row->height > window_box_height (w))
12831 {
12832 /* If we end up in a partially visible line, let's
12833 make it fully visible, except when it's taller
12834 than the window, in which case we can't do much
12835 about it. */
12836 *scroll_step = 1;
12837 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12838 }
12839 else
12840 {
12841 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12842 if (!cursor_row_fully_visible_p (w, 0, 1))
12843 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12844 else
12845 rc = CURSOR_MOVEMENT_SUCCESS;
12846 }
12847 }
12848 else if (scroll_p)
12849 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12850 else
12851 {
12852 do
12853 {
12854 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12855 {
12856 rc = CURSOR_MOVEMENT_SUCCESS;
12857 break;
12858 }
12859 ++row;
12860 }
12861 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12862 && MATRIX_ROW_START_CHARPOS (row) == PT
12863 && cursor_row_p (w, row));
12864 }
12865 }
12866 }
12867
12868 return rc;
12869 }
12870
12871 void
12872 set_vertical_scroll_bar (w)
12873 struct window *w;
12874 {
12875 int start, end, whole;
12876
12877 /* Calculate the start and end positions for the current window.
12878 At some point, it would be nice to choose between scrollbars
12879 which reflect the whole buffer size, with special markers
12880 indicating narrowing, and scrollbars which reflect only the
12881 visible region.
12882
12883 Note that mini-buffers sometimes aren't displaying any text. */
12884 if (!MINI_WINDOW_P (w)
12885 || (w == XWINDOW (minibuf_window)
12886 && NILP (echo_area_buffer[0])))
12887 {
12888 struct buffer *buf = XBUFFER (w->buffer);
12889 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12890 start = marker_position (w->start) - BUF_BEGV (buf);
12891 /* I don't think this is guaranteed to be right. For the
12892 moment, we'll pretend it is. */
12893 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12894
12895 if (end < start)
12896 end = start;
12897 if (whole < (end - start))
12898 whole = end - start;
12899 }
12900 else
12901 start = end = whole = 0;
12902
12903 /* Indicate what this scroll bar ought to be displaying now. */
12904 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12905 }
12906
12907
12908 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12909 selected_window is redisplayed.
12910
12911 We can return without actually redisplaying the window if
12912 fonts_changed_p is nonzero. In that case, redisplay_internal will
12913 retry. */
12914
12915 static void
12916 redisplay_window (window, just_this_one_p)
12917 Lisp_Object window;
12918 int just_this_one_p;
12919 {
12920 struct window *w = XWINDOW (window);
12921 struct frame *f = XFRAME (w->frame);
12922 struct buffer *buffer = XBUFFER (w->buffer);
12923 struct buffer *old = current_buffer;
12924 struct text_pos lpoint, opoint, startp;
12925 int update_mode_line;
12926 int tem;
12927 struct it it;
12928 /* Record it now because it's overwritten. */
12929 int current_matrix_up_to_date_p = 0;
12930 int used_current_matrix_p = 0;
12931 /* This is less strict than current_matrix_up_to_date_p.
12932 It indictes that the buffer contents and narrowing are unchanged. */
12933 int buffer_unchanged_p = 0;
12934 int temp_scroll_step = 0;
12935 int count = SPECPDL_INDEX ();
12936 int rc;
12937 int centering_position = -1;
12938 int last_line_misfit = 0;
12939 int beg_unchanged, end_unchanged;
12940
12941 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12942 opoint = lpoint;
12943
12944 /* W must be a leaf window here. */
12945 xassert (!NILP (w->buffer));
12946 #if GLYPH_DEBUG
12947 *w->desired_matrix->method = 0;
12948 #endif
12949
12950 specbind (Qinhibit_point_motion_hooks, Qt);
12951
12952 reconsider_clip_changes (w, buffer);
12953
12954 /* Has the mode line to be updated? */
12955 update_mode_line = (!NILP (w->update_mode_line)
12956 || update_mode_lines
12957 || buffer->clip_changed
12958 || buffer->prevent_redisplay_optimizations_p);
12959
12960 if (MINI_WINDOW_P (w))
12961 {
12962 if (w == XWINDOW (echo_area_window)
12963 && !NILP (echo_area_buffer[0]))
12964 {
12965 if (update_mode_line)
12966 /* We may have to update a tty frame's menu bar or a
12967 tool-bar. Example `M-x C-h C-h C-g'. */
12968 goto finish_menu_bars;
12969 else
12970 /* We've already displayed the echo area glyphs in this window. */
12971 goto finish_scroll_bars;
12972 }
12973 else if ((w != XWINDOW (minibuf_window)
12974 || minibuf_level == 0)
12975 /* When buffer is nonempty, redisplay window normally. */
12976 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12977 /* Quail displays non-mini buffers in minibuffer window.
12978 In that case, redisplay the window normally. */
12979 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12980 {
12981 /* W is a mini-buffer window, but it's not active, so clear
12982 it. */
12983 int yb = window_text_bottom_y (w);
12984 struct glyph_row *row;
12985 int y;
12986
12987 for (y = 0, row = w->desired_matrix->rows;
12988 y < yb;
12989 y += row->height, ++row)
12990 blank_row (w, row, y);
12991 goto finish_scroll_bars;
12992 }
12993
12994 clear_glyph_matrix (w->desired_matrix);
12995 }
12996
12997 /* Otherwise set up data on this window; select its buffer and point
12998 value. */
12999 /* Really select the buffer, for the sake of buffer-local
13000 variables. */
13001 set_buffer_internal_1 (XBUFFER (w->buffer));
13002 SET_TEXT_POS (opoint, PT, PT_BYTE);
13003
13004 beg_unchanged = BEG_UNCHANGED;
13005 end_unchanged = END_UNCHANGED;
13006
13007 current_matrix_up_to_date_p
13008 = (!NILP (w->window_end_valid)
13009 && !current_buffer->clip_changed
13010 && !current_buffer->prevent_redisplay_optimizations_p
13011 && XFASTINT (w->last_modified) >= MODIFF
13012 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13013
13014 buffer_unchanged_p
13015 = (!NILP (w->window_end_valid)
13016 && !current_buffer->clip_changed
13017 && XFASTINT (w->last_modified) >= MODIFF
13018 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13019
13020 /* When windows_or_buffers_changed is non-zero, we can't rely on
13021 the window end being valid, so set it to nil there. */
13022 if (windows_or_buffers_changed)
13023 {
13024 /* If window starts on a continuation line, maybe adjust the
13025 window start in case the window's width changed. */
13026 if (XMARKER (w->start)->buffer == current_buffer)
13027 compute_window_start_on_continuation_line (w);
13028
13029 w->window_end_valid = Qnil;
13030 }
13031
13032 /* Some sanity checks. */
13033 CHECK_WINDOW_END (w);
13034 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13035 abort ();
13036 if (BYTEPOS (opoint) < CHARPOS (opoint))
13037 abort ();
13038
13039 /* If %c is in mode line, update it if needed. */
13040 if (!NILP (w->column_number_displayed)
13041 /* This alternative quickly identifies a common case
13042 where no change is needed. */
13043 && !(PT == XFASTINT (w->last_point)
13044 && XFASTINT (w->last_modified) >= MODIFF
13045 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13046 && (XFASTINT (w->column_number_displayed)
13047 != (int) current_column ())) /* iftc */
13048 update_mode_line = 1;
13049
13050 /* Count number of windows showing the selected buffer. An indirect
13051 buffer counts as its base buffer. */
13052 if (!just_this_one_p)
13053 {
13054 struct buffer *current_base, *window_base;
13055 current_base = current_buffer;
13056 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13057 if (current_base->base_buffer)
13058 current_base = current_base->base_buffer;
13059 if (window_base->base_buffer)
13060 window_base = window_base->base_buffer;
13061 if (current_base == window_base)
13062 buffer_shared++;
13063 }
13064
13065 /* Point refers normally to the selected window. For any other
13066 window, set up appropriate value. */
13067 if (!EQ (window, selected_window))
13068 {
13069 int new_pt = XMARKER (w->pointm)->charpos;
13070 int new_pt_byte = marker_byte_position (w->pointm);
13071 if (new_pt < BEGV)
13072 {
13073 new_pt = BEGV;
13074 new_pt_byte = BEGV_BYTE;
13075 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13076 }
13077 else if (new_pt > (ZV - 1))
13078 {
13079 new_pt = ZV;
13080 new_pt_byte = ZV_BYTE;
13081 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13082 }
13083
13084 /* We don't use SET_PT so that the point-motion hooks don't run. */
13085 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13086 }
13087
13088 /* If any of the character widths specified in the display table
13089 have changed, invalidate the width run cache. It's true that
13090 this may be a bit late to catch such changes, but the rest of
13091 redisplay goes (non-fatally) haywire when the display table is
13092 changed, so why should we worry about doing any better? */
13093 if (current_buffer->width_run_cache)
13094 {
13095 struct Lisp_Char_Table *disptab = buffer_display_table ();
13096
13097 if (! disptab_matches_widthtab (disptab,
13098 XVECTOR (current_buffer->width_table)))
13099 {
13100 invalidate_region_cache (current_buffer,
13101 current_buffer->width_run_cache,
13102 BEG, Z);
13103 recompute_width_table (current_buffer, disptab);
13104 }
13105 }
13106
13107 /* If window-start is screwed up, choose a new one. */
13108 if (XMARKER (w->start)->buffer != current_buffer)
13109 goto recenter;
13110
13111 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13112
13113 /* If someone specified a new starting point but did not insist,
13114 check whether it can be used. */
13115 if (!NILP (w->optional_new_start)
13116 && CHARPOS (startp) >= BEGV
13117 && CHARPOS (startp) <= ZV)
13118 {
13119 w->optional_new_start = Qnil;
13120 start_display (&it, w, startp);
13121 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13122 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13123 if (IT_CHARPOS (it) == PT)
13124 w->force_start = Qt;
13125 /* IT may overshoot PT if text at PT is invisible. */
13126 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13127 w->force_start = Qt;
13128 }
13129
13130 force_start:
13131
13132 /* Handle case where place to start displaying has been specified,
13133 unless the specified location is outside the accessible range. */
13134 if (!NILP (w->force_start)
13135 || w->frozen_window_start_p)
13136 {
13137 /* We set this later on if we have to adjust point. */
13138 int new_vpos = -1;
13139 int val;
13140
13141 w->force_start = Qnil;
13142 w->vscroll = 0;
13143 w->window_end_valid = Qnil;
13144
13145 /* Forget any recorded base line for line number display. */
13146 if (!buffer_unchanged_p)
13147 w->base_line_number = Qnil;
13148
13149 /* Redisplay the mode line. Select the buffer properly for that.
13150 Also, run the hook window-scroll-functions
13151 because we have scrolled. */
13152 /* Note, we do this after clearing force_start because
13153 if there's an error, it is better to forget about force_start
13154 than to get into an infinite loop calling the hook functions
13155 and having them get more errors. */
13156 if (!update_mode_line
13157 || ! NILP (Vwindow_scroll_functions))
13158 {
13159 update_mode_line = 1;
13160 w->update_mode_line = Qt;
13161 startp = run_window_scroll_functions (window, startp);
13162 }
13163
13164 w->last_modified = make_number (0);
13165 w->last_overlay_modified = make_number (0);
13166 if (CHARPOS (startp) < BEGV)
13167 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13168 else if (CHARPOS (startp) > ZV)
13169 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13170
13171 /* Redisplay, then check if cursor has been set during the
13172 redisplay. Give up if new fonts were loaded. */
13173 val = try_window (window, startp, 1);
13174 if (!val)
13175 {
13176 w->force_start = Qt;
13177 clear_glyph_matrix (w->desired_matrix);
13178 goto need_larger_matrices;
13179 }
13180 /* Point was outside the scroll margins. */
13181 if (val < 0)
13182 new_vpos = window_box_height (w) / 2;
13183
13184 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13185 {
13186 /* If point does not appear, try to move point so it does
13187 appear. The desired matrix has been built above, so we
13188 can use it here. */
13189 new_vpos = window_box_height (w) / 2;
13190 }
13191
13192 if (!cursor_row_fully_visible_p (w, 0, 0))
13193 {
13194 /* Point does appear, but on a line partly visible at end of window.
13195 Move it back to a fully-visible line. */
13196 new_vpos = window_box_height (w);
13197 }
13198
13199 /* If we need to move point for either of the above reasons,
13200 now actually do it. */
13201 if (new_vpos >= 0)
13202 {
13203 struct glyph_row *row;
13204
13205 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13206 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13207 ++row;
13208
13209 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13210 MATRIX_ROW_START_BYTEPOS (row));
13211
13212 if (w != XWINDOW (selected_window))
13213 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13214 else if (current_buffer == old)
13215 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13216
13217 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13218
13219 /* If we are highlighting the region, then we just changed
13220 the region, so redisplay to show it. */
13221 if (!NILP (Vtransient_mark_mode)
13222 && !NILP (current_buffer->mark_active))
13223 {
13224 clear_glyph_matrix (w->desired_matrix);
13225 if (!try_window (window, startp, 0))
13226 goto need_larger_matrices;
13227 }
13228 }
13229
13230 #if GLYPH_DEBUG
13231 debug_method_add (w, "forced window start");
13232 #endif
13233 goto done;
13234 }
13235
13236 /* Handle case where text has not changed, only point, and it has
13237 not moved off the frame, and we are not retrying after hscroll.
13238 (current_matrix_up_to_date_p is nonzero when retrying.) */
13239 if (current_matrix_up_to_date_p
13240 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13241 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13242 {
13243 switch (rc)
13244 {
13245 case CURSOR_MOVEMENT_SUCCESS:
13246 used_current_matrix_p = 1;
13247 goto done;
13248
13249 #if 0 /* try_cursor_movement never returns this value. */
13250 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13251 goto need_larger_matrices;
13252 #endif
13253
13254 case CURSOR_MOVEMENT_MUST_SCROLL:
13255 goto try_to_scroll;
13256
13257 default:
13258 abort ();
13259 }
13260 }
13261 /* If current starting point was originally the beginning of a line
13262 but no longer is, find a new starting point. */
13263 else if (!NILP (w->start_at_line_beg)
13264 && !(CHARPOS (startp) <= BEGV
13265 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13266 {
13267 #if GLYPH_DEBUG
13268 debug_method_add (w, "recenter 1");
13269 #endif
13270 goto recenter;
13271 }
13272
13273 /* Try scrolling with try_window_id. Value is > 0 if update has
13274 been done, it is -1 if we know that the same window start will
13275 not work. It is 0 if unsuccessful for some other reason. */
13276 else if ((tem = try_window_id (w)) != 0)
13277 {
13278 #if GLYPH_DEBUG
13279 debug_method_add (w, "try_window_id %d", tem);
13280 #endif
13281
13282 if (fonts_changed_p)
13283 goto need_larger_matrices;
13284 if (tem > 0)
13285 goto done;
13286
13287 /* Otherwise try_window_id has returned -1 which means that we
13288 don't want the alternative below this comment to execute. */
13289 }
13290 else if (CHARPOS (startp) >= BEGV
13291 && CHARPOS (startp) <= ZV
13292 && PT >= CHARPOS (startp)
13293 && (CHARPOS (startp) < ZV
13294 /* Avoid starting at end of buffer. */
13295 || CHARPOS (startp) == BEGV
13296 || (XFASTINT (w->last_modified) >= MODIFF
13297 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13298 {
13299
13300 /* If first window line is a continuation line, and window start
13301 is inside the modified region, but the first change is before
13302 current window start, we must select a new window start.
13303
13304 However, if this is the result of a down-mouse event (e.g. by
13305 extending the mouse-drag-overlay), we don't want to select a
13306 new window start, since that would change the position under
13307 the mouse, resulting in an unwanted mouse-movement rather
13308 than a simple mouse-click. */
13309 if (NILP (w->start_at_line_beg)
13310 && NILP (do_mouse_tracking)
13311 && CHARPOS (startp) > BEGV
13312 && CHARPOS (startp) > BEG + beg_unchanged
13313 && CHARPOS (startp) <= Z - end_unchanged)
13314 {
13315 w->force_start = Qt;
13316 if (XMARKER (w->start)->buffer == current_buffer)
13317 compute_window_start_on_continuation_line (w);
13318 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13319 goto force_start;
13320 }
13321
13322 #if GLYPH_DEBUG
13323 debug_method_add (w, "same window start");
13324 #endif
13325
13326 /* Try to redisplay starting at same place as before.
13327 If point has not moved off frame, accept the results. */
13328 if (!current_matrix_up_to_date_p
13329 /* Don't use try_window_reusing_current_matrix in this case
13330 because a window scroll function can have changed the
13331 buffer. */
13332 || !NILP (Vwindow_scroll_functions)
13333 || MINI_WINDOW_P (w)
13334 || !(used_current_matrix_p
13335 = try_window_reusing_current_matrix (w)))
13336 {
13337 IF_DEBUG (debug_method_add (w, "1"));
13338 if (try_window (window, startp, 1) < 0)
13339 /* -1 means we need to scroll.
13340 0 means we need new matrices, but fonts_changed_p
13341 is set in that case, so we will detect it below. */
13342 goto try_to_scroll;
13343 }
13344
13345 if (fonts_changed_p)
13346 goto need_larger_matrices;
13347
13348 if (w->cursor.vpos >= 0)
13349 {
13350 if (!just_this_one_p
13351 || current_buffer->clip_changed
13352 || BEG_UNCHANGED < CHARPOS (startp))
13353 /* Forget any recorded base line for line number display. */
13354 w->base_line_number = Qnil;
13355
13356 if (!cursor_row_fully_visible_p (w, 1, 0))
13357 {
13358 clear_glyph_matrix (w->desired_matrix);
13359 last_line_misfit = 1;
13360 }
13361 /* Drop through and scroll. */
13362 else
13363 goto done;
13364 }
13365 else
13366 clear_glyph_matrix (w->desired_matrix);
13367 }
13368
13369 try_to_scroll:
13370
13371 w->last_modified = make_number (0);
13372 w->last_overlay_modified = make_number (0);
13373
13374 /* Redisplay the mode line. Select the buffer properly for that. */
13375 if (!update_mode_line)
13376 {
13377 update_mode_line = 1;
13378 w->update_mode_line = Qt;
13379 }
13380
13381 /* Try to scroll by specified few lines. */
13382 if ((scroll_conservatively
13383 || scroll_step
13384 || temp_scroll_step
13385 || NUMBERP (current_buffer->scroll_up_aggressively)
13386 || NUMBERP (current_buffer->scroll_down_aggressively))
13387 && !current_buffer->clip_changed
13388 && CHARPOS (startp) >= BEGV
13389 && CHARPOS (startp) <= ZV)
13390 {
13391 /* The function returns -1 if new fonts were loaded, 1 if
13392 successful, 0 if not successful. */
13393 int rc = try_scrolling (window, just_this_one_p,
13394 scroll_conservatively,
13395 scroll_step,
13396 temp_scroll_step, last_line_misfit);
13397 switch (rc)
13398 {
13399 case SCROLLING_SUCCESS:
13400 goto done;
13401
13402 case SCROLLING_NEED_LARGER_MATRICES:
13403 goto need_larger_matrices;
13404
13405 case SCROLLING_FAILED:
13406 break;
13407
13408 default:
13409 abort ();
13410 }
13411 }
13412
13413 /* Finally, just choose place to start which centers point */
13414
13415 recenter:
13416 if (centering_position < 0)
13417 centering_position = window_box_height (w) / 2;
13418
13419 #if GLYPH_DEBUG
13420 debug_method_add (w, "recenter");
13421 #endif
13422
13423 /* w->vscroll = 0; */
13424
13425 /* Forget any previously recorded base line for line number display. */
13426 if (!buffer_unchanged_p)
13427 w->base_line_number = Qnil;
13428
13429 /* Move backward half the height of the window. */
13430 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13431 it.current_y = it.last_visible_y;
13432 move_it_vertically_backward (&it, centering_position);
13433 xassert (IT_CHARPOS (it) >= BEGV);
13434
13435 /* The function move_it_vertically_backward may move over more
13436 than the specified y-distance. If it->w is small, e.g. a
13437 mini-buffer window, we may end up in front of the window's
13438 display area. Start displaying at the start of the line
13439 containing PT in this case. */
13440 if (it.current_y <= 0)
13441 {
13442 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13443 move_it_vertically_backward (&it, 0);
13444 #if 0
13445 /* I think this assert is bogus if buffer contains
13446 invisible text or images. KFS. */
13447 xassert (IT_CHARPOS (it) <= PT);
13448 #endif
13449 it.current_y = 0;
13450 }
13451
13452 it.current_x = it.hpos = 0;
13453
13454 /* Set startp here explicitly in case that helps avoid an infinite loop
13455 in case the window-scroll-functions functions get errors. */
13456 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13457
13458 /* Run scroll hooks. */
13459 startp = run_window_scroll_functions (window, it.current.pos);
13460
13461 /* Redisplay the window. */
13462 if (!current_matrix_up_to_date_p
13463 || windows_or_buffers_changed
13464 || cursor_type_changed
13465 /* Don't use try_window_reusing_current_matrix in this case
13466 because it can have changed the buffer. */
13467 || !NILP (Vwindow_scroll_functions)
13468 || !just_this_one_p
13469 || MINI_WINDOW_P (w)
13470 || !(used_current_matrix_p
13471 = try_window_reusing_current_matrix (w)))
13472 try_window (window, startp, 0);
13473
13474 /* If new fonts have been loaded (due to fontsets), give up. We
13475 have to start a new redisplay since we need to re-adjust glyph
13476 matrices. */
13477 if (fonts_changed_p)
13478 goto need_larger_matrices;
13479
13480 /* If cursor did not appear assume that the middle of the window is
13481 in the first line of the window. Do it again with the next line.
13482 (Imagine a window of height 100, displaying two lines of height
13483 60. Moving back 50 from it->last_visible_y will end in the first
13484 line.) */
13485 if (w->cursor.vpos < 0)
13486 {
13487 if (!NILP (w->window_end_valid)
13488 && PT >= Z - XFASTINT (w->window_end_pos))
13489 {
13490 clear_glyph_matrix (w->desired_matrix);
13491 move_it_by_lines (&it, 1, 0);
13492 try_window (window, it.current.pos, 0);
13493 }
13494 else if (PT < IT_CHARPOS (it))
13495 {
13496 clear_glyph_matrix (w->desired_matrix);
13497 move_it_by_lines (&it, -1, 0);
13498 try_window (window, it.current.pos, 0);
13499 }
13500 else
13501 {
13502 /* Not much we can do about it. */
13503 }
13504 }
13505
13506 /* Consider the following case: Window starts at BEGV, there is
13507 invisible, intangible text at BEGV, so that display starts at
13508 some point START > BEGV. It can happen that we are called with
13509 PT somewhere between BEGV and START. Try to handle that case. */
13510 if (w->cursor.vpos < 0)
13511 {
13512 struct glyph_row *row = w->current_matrix->rows;
13513 if (row->mode_line_p)
13514 ++row;
13515 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13516 }
13517
13518 if (!cursor_row_fully_visible_p (w, 0, 0))
13519 {
13520 /* If vscroll is enabled, disable it and try again. */
13521 if (w->vscroll)
13522 {
13523 w->vscroll = 0;
13524 clear_glyph_matrix (w->desired_matrix);
13525 goto recenter;
13526 }
13527
13528 /* If centering point failed to make the whole line visible,
13529 put point at the top instead. That has to make the whole line
13530 visible, if it can be done. */
13531 if (centering_position == 0)
13532 goto done;
13533
13534 clear_glyph_matrix (w->desired_matrix);
13535 centering_position = 0;
13536 goto recenter;
13537 }
13538
13539 done:
13540
13541 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13542 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13543 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13544 ? Qt : Qnil);
13545
13546 /* Display the mode line, if we must. */
13547 if ((update_mode_line
13548 /* If window not full width, must redo its mode line
13549 if (a) the window to its side is being redone and
13550 (b) we do a frame-based redisplay. This is a consequence
13551 of how inverted lines are drawn in frame-based redisplay. */
13552 || (!just_this_one_p
13553 && !FRAME_WINDOW_P (f)
13554 && !WINDOW_FULL_WIDTH_P (w))
13555 /* Line number to display. */
13556 || INTEGERP (w->base_line_pos)
13557 /* Column number is displayed and different from the one displayed. */
13558 || (!NILP (w->column_number_displayed)
13559 && (XFASTINT (w->column_number_displayed)
13560 != (int) current_column ()))) /* iftc */
13561 /* This means that the window has a mode line. */
13562 && (WINDOW_WANTS_MODELINE_P (w)
13563 || WINDOW_WANTS_HEADER_LINE_P (w)))
13564 {
13565 display_mode_lines (w);
13566
13567 /* If mode line height has changed, arrange for a thorough
13568 immediate redisplay using the correct mode line height. */
13569 if (WINDOW_WANTS_MODELINE_P (w)
13570 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13571 {
13572 fonts_changed_p = 1;
13573 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13574 = DESIRED_MODE_LINE_HEIGHT (w);
13575 }
13576
13577 /* If top line height has changed, arrange for a thorough
13578 immediate redisplay using the correct mode line height. */
13579 if (WINDOW_WANTS_HEADER_LINE_P (w)
13580 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13581 {
13582 fonts_changed_p = 1;
13583 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13584 = DESIRED_HEADER_LINE_HEIGHT (w);
13585 }
13586
13587 if (fonts_changed_p)
13588 goto need_larger_matrices;
13589 }
13590
13591 if (!line_number_displayed
13592 && !BUFFERP (w->base_line_pos))
13593 {
13594 w->base_line_pos = Qnil;
13595 w->base_line_number = Qnil;
13596 }
13597
13598 finish_menu_bars:
13599
13600 /* When we reach a frame's selected window, redo the frame's menu bar. */
13601 if (update_mode_line
13602 && EQ (FRAME_SELECTED_WINDOW (f), window))
13603 {
13604 int redisplay_menu_p = 0;
13605 int redisplay_tool_bar_p = 0;
13606
13607 if (FRAME_WINDOW_P (f))
13608 {
13609 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13610 || defined (USE_GTK)
13611 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13612 #else
13613 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13614 #endif
13615 }
13616 else
13617 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13618
13619 if (redisplay_menu_p)
13620 display_menu_bar (w);
13621
13622 #ifdef HAVE_WINDOW_SYSTEM
13623 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13624 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13625 #else
13626 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13627 && (FRAME_TOOL_BAR_LINES (f) > 0
13628 || !NILP (Vauto_resize_tool_bars));
13629
13630 #endif
13631
13632 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13633 {
13634 extern int ignore_mouse_drag_p;
13635 ignore_mouse_drag_p = 1;
13636 }
13637 #endif
13638 }
13639
13640 #ifdef HAVE_WINDOW_SYSTEM
13641 if (FRAME_WINDOW_P (f)
13642 && update_window_fringes (w, (just_this_one_p
13643 || (!used_current_matrix_p && !overlay_arrow_seen)
13644 || w->pseudo_window_p)))
13645 {
13646 update_begin (f);
13647 BLOCK_INPUT;
13648 if (draw_window_fringes (w, 1))
13649 x_draw_vertical_border (w);
13650 UNBLOCK_INPUT;
13651 update_end (f);
13652 }
13653 #endif /* HAVE_WINDOW_SYSTEM */
13654
13655 /* We go to this label, with fonts_changed_p nonzero,
13656 if it is necessary to try again using larger glyph matrices.
13657 We have to redeem the scroll bar even in this case,
13658 because the loop in redisplay_internal expects that. */
13659 need_larger_matrices:
13660 ;
13661 finish_scroll_bars:
13662
13663 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13664 {
13665 /* Set the thumb's position and size. */
13666 set_vertical_scroll_bar (w);
13667
13668 /* Note that we actually used the scroll bar attached to this
13669 window, so it shouldn't be deleted at the end of redisplay. */
13670 redeem_scroll_bar_hook (w);
13671 }
13672
13673 /* Restore current_buffer and value of point in it. */
13674 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13675 set_buffer_internal_1 (old);
13676 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13677 shorter. This can be caused by log truncation in *Messages*. */
13678 if (CHARPOS (lpoint) <= ZV)
13679 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13680
13681 unbind_to (count, Qnil);
13682 }
13683
13684
13685 /* Build the complete desired matrix of WINDOW with a window start
13686 buffer position POS.
13687
13688 Value is 1 if successful. It is zero if fonts were loaded during
13689 redisplay which makes re-adjusting glyph matrices necessary, and -1
13690 if point would appear in the scroll margins.
13691 (We check that only if CHECK_MARGINS is nonzero. */
13692
13693 int
13694 try_window (window, pos, check_margins)
13695 Lisp_Object window;
13696 struct text_pos pos;
13697 int check_margins;
13698 {
13699 struct window *w = XWINDOW (window);
13700 struct it it;
13701 struct glyph_row *last_text_row = NULL;
13702 struct frame *f = XFRAME (w->frame);
13703
13704 /* Make POS the new window start. */
13705 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13706
13707 /* Mark cursor position as unknown. No overlay arrow seen. */
13708 w->cursor.vpos = -1;
13709 overlay_arrow_seen = 0;
13710
13711 /* Initialize iterator and info to start at POS. */
13712 start_display (&it, w, pos);
13713
13714 /* Display all lines of W. */
13715 while (it.current_y < it.last_visible_y)
13716 {
13717 if (display_line (&it))
13718 last_text_row = it.glyph_row - 1;
13719 if (fonts_changed_p)
13720 return 0;
13721 }
13722
13723 /* Don't let the cursor end in the scroll margins. */
13724 if (check_margins
13725 && !MINI_WINDOW_P (w))
13726 {
13727 int this_scroll_margin;
13728
13729 this_scroll_margin = max (0, scroll_margin);
13730 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13731 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13732
13733 if ((w->cursor.y >= 0 /* not vscrolled */
13734 && w->cursor.y < this_scroll_margin
13735 && CHARPOS (pos) > BEGV
13736 && IT_CHARPOS (it) < ZV)
13737 /* rms: considering make_cursor_line_fully_visible_p here
13738 seems to give wrong results. We don't want to recenter
13739 when the last line is partly visible, we want to allow
13740 that case to be handled in the usual way. */
13741 || (w->cursor.y + 1) > it.last_visible_y)
13742 {
13743 w->cursor.vpos = -1;
13744 clear_glyph_matrix (w->desired_matrix);
13745 return -1;
13746 }
13747 }
13748
13749 /* If bottom moved off end of frame, change mode line percentage. */
13750 if (XFASTINT (w->window_end_pos) <= 0
13751 && Z != IT_CHARPOS (it))
13752 w->update_mode_line = Qt;
13753
13754 /* Set window_end_pos to the offset of the last character displayed
13755 on the window from the end of current_buffer. Set
13756 window_end_vpos to its row number. */
13757 if (last_text_row)
13758 {
13759 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13760 w->window_end_bytepos
13761 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13762 w->window_end_pos
13763 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13764 w->window_end_vpos
13765 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13766 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13767 ->displays_text_p);
13768 }
13769 else
13770 {
13771 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13772 w->window_end_pos = make_number (Z - ZV);
13773 w->window_end_vpos = make_number (0);
13774 }
13775
13776 /* But that is not valid info until redisplay finishes. */
13777 w->window_end_valid = Qnil;
13778 return 1;
13779 }
13780
13781
13782 \f
13783 /************************************************************************
13784 Window redisplay reusing current matrix when buffer has not changed
13785 ************************************************************************/
13786
13787 /* Try redisplay of window W showing an unchanged buffer with a
13788 different window start than the last time it was displayed by
13789 reusing its current matrix. Value is non-zero if successful.
13790 W->start is the new window start. */
13791
13792 static int
13793 try_window_reusing_current_matrix (w)
13794 struct window *w;
13795 {
13796 struct frame *f = XFRAME (w->frame);
13797 struct glyph_row *row, *bottom_row;
13798 struct it it;
13799 struct run run;
13800 struct text_pos start, new_start;
13801 int nrows_scrolled, i;
13802 struct glyph_row *last_text_row;
13803 struct glyph_row *last_reused_text_row;
13804 struct glyph_row *start_row;
13805 int start_vpos, min_y, max_y;
13806
13807 #if GLYPH_DEBUG
13808 if (inhibit_try_window_reusing)
13809 return 0;
13810 #endif
13811
13812 if (/* This function doesn't handle terminal frames. */
13813 !FRAME_WINDOW_P (f)
13814 /* Don't try to reuse the display if windows have been split
13815 or such. */
13816 || windows_or_buffers_changed
13817 || cursor_type_changed)
13818 return 0;
13819
13820 /* Can't do this if region may have changed. */
13821 if ((!NILP (Vtransient_mark_mode)
13822 && !NILP (current_buffer->mark_active))
13823 || !NILP (w->region_showing)
13824 || !NILP (Vshow_trailing_whitespace))
13825 return 0;
13826
13827 /* If top-line visibility has changed, give up. */
13828 if (WINDOW_WANTS_HEADER_LINE_P (w)
13829 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13830 return 0;
13831
13832 /* Give up if old or new display is scrolled vertically. We could
13833 make this function handle this, but right now it doesn't. */
13834 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13835 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13836 return 0;
13837
13838 /* The variable new_start now holds the new window start. The old
13839 start `start' can be determined from the current matrix. */
13840 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13841 start = start_row->start.pos;
13842 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13843
13844 /* Clear the desired matrix for the display below. */
13845 clear_glyph_matrix (w->desired_matrix);
13846
13847 if (CHARPOS (new_start) <= CHARPOS (start))
13848 {
13849 int first_row_y;
13850
13851 /* Don't use this method if the display starts with an ellipsis
13852 displayed for invisible text. It's not easy to handle that case
13853 below, and it's certainly not worth the effort since this is
13854 not a frequent case. */
13855 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13856 return 0;
13857
13858 IF_DEBUG (debug_method_add (w, "twu1"));
13859
13860 /* Display up to a row that can be reused. The variable
13861 last_text_row is set to the last row displayed that displays
13862 text. Note that it.vpos == 0 if or if not there is a
13863 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13864 start_display (&it, w, new_start);
13865 first_row_y = it.current_y;
13866 w->cursor.vpos = -1;
13867 last_text_row = last_reused_text_row = NULL;
13868
13869 while (it.current_y < it.last_visible_y
13870 && !fonts_changed_p)
13871 {
13872 /* If we have reached into the characters in the START row,
13873 that means the line boundaries have changed. So we
13874 can't start copying with the row START. Maybe it will
13875 work to start copying with the following row. */
13876 while (IT_CHARPOS (it) > CHARPOS (start))
13877 {
13878 /* Advance to the next row as the "start". */
13879 start_row++;
13880 start = start_row->start.pos;
13881 /* If there are no more rows to try, or just one, give up. */
13882 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13883 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13884 || CHARPOS (start) == ZV)
13885 {
13886 clear_glyph_matrix (w->desired_matrix);
13887 return 0;
13888 }
13889
13890 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13891 }
13892 /* If we have reached alignment,
13893 we can copy the rest of the rows. */
13894 if (IT_CHARPOS (it) == CHARPOS (start))
13895 break;
13896
13897 if (display_line (&it))
13898 last_text_row = it.glyph_row - 1;
13899 }
13900
13901 /* A value of current_y < last_visible_y means that we stopped
13902 at the previous window start, which in turn means that we
13903 have at least one reusable row. */
13904 if (it.current_y < it.last_visible_y)
13905 {
13906 /* IT.vpos always starts from 0; it counts text lines. */
13907 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13908
13909 /* Find PT if not already found in the lines displayed. */
13910 if (w->cursor.vpos < 0)
13911 {
13912 int dy = it.current_y - start_row->y;
13913
13914 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13915 row = row_containing_pos (w, PT, row, NULL, dy);
13916 if (row)
13917 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13918 dy, nrows_scrolled);
13919 else
13920 {
13921 clear_glyph_matrix (w->desired_matrix);
13922 return 0;
13923 }
13924 }
13925
13926 /* Scroll the display. Do it before the current matrix is
13927 changed. The problem here is that update has not yet
13928 run, i.e. part of the current matrix is not up to date.
13929 scroll_run_hook will clear the cursor, and use the
13930 current matrix to get the height of the row the cursor is
13931 in. */
13932 run.current_y = start_row->y;
13933 run.desired_y = it.current_y;
13934 run.height = it.last_visible_y - it.current_y;
13935
13936 if (run.height > 0 && run.current_y != run.desired_y)
13937 {
13938 update_begin (f);
13939 rif->update_window_begin_hook (w);
13940 rif->clear_window_mouse_face (w);
13941 rif->scroll_run_hook (w, &run);
13942 rif->update_window_end_hook (w, 0, 0);
13943 update_end (f);
13944 }
13945
13946 /* Shift current matrix down by nrows_scrolled lines. */
13947 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13948 rotate_matrix (w->current_matrix,
13949 start_vpos,
13950 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13951 nrows_scrolled);
13952
13953 /* Disable lines that must be updated. */
13954 for (i = 0; i < nrows_scrolled; ++i)
13955 (start_row + i)->enabled_p = 0;
13956
13957 /* Re-compute Y positions. */
13958 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13959 max_y = it.last_visible_y;
13960 for (row = start_row + nrows_scrolled;
13961 row < bottom_row;
13962 ++row)
13963 {
13964 row->y = it.current_y;
13965 row->visible_height = row->height;
13966
13967 if (row->y < min_y)
13968 row->visible_height -= min_y - row->y;
13969 if (row->y + row->height > max_y)
13970 row->visible_height -= row->y + row->height - max_y;
13971 row->redraw_fringe_bitmaps_p = 1;
13972
13973 it.current_y += row->height;
13974
13975 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13976 last_reused_text_row = row;
13977 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13978 break;
13979 }
13980
13981 /* Disable lines in the current matrix which are now
13982 below the window. */
13983 for (++row; row < bottom_row; ++row)
13984 row->enabled_p = row->mode_line_p = 0;
13985 }
13986
13987 /* Update window_end_pos etc.; last_reused_text_row is the last
13988 reused row from the current matrix containing text, if any.
13989 The value of last_text_row is the last displayed line
13990 containing text. */
13991 if (last_reused_text_row)
13992 {
13993 w->window_end_bytepos
13994 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13995 w->window_end_pos
13996 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13997 w->window_end_vpos
13998 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13999 w->current_matrix));
14000 }
14001 else if (last_text_row)
14002 {
14003 w->window_end_bytepos
14004 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14005 w->window_end_pos
14006 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14007 w->window_end_vpos
14008 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14009 }
14010 else
14011 {
14012 /* This window must be completely empty. */
14013 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14014 w->window_end_pos = make_number (Z - ZV);
14015 w->window_end_vpos = make_number (0);
14016 }
14017 w->window_end_valid = Qnil;
14018
14019 /* Update hint: don't try scrolling again in update_window. */
14020 w->desired_matrix->no_scrolling_p = 1;
14021
14022 #if GLYPH_DEBUG
14023 debug_method_add (w, "try_window_reusing_current_matrix 1");
14024 #endif
14025 return 1;
14026 }
14027 else if (CHARPOS (new_start) > CHARPOS (start))
14028 {
14029 struct glyph_row *pt_row, *row;
14030 struct glyph_row *first_reusable_row;
14031 struct glyph_row *first_row_to_display;
14032 int dy;
14033 int yb = window_text_bottom_y (w);
14034
14035 /* Find the row starting at new_start, if there is one. Don't
14036 reuse a partially visible line at the end. */
14037 first_reusable_row = start_row;
14038 while (first_reusable_row->enabled_p
14039 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14040 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14041 < CHARPOS (new_start)))
14042 ++first_reusable_row;
14043
14044 /* Give up if there is no row to reuse. */
14045 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14046 || !first_reusable_row->enabled_p
14047 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14048 != CHARPOS (new_start)))
14049 return 0;
14050
14051 /* We can reuse fully visible rows beginning with
14052 first_reusable_row to the end of the window. Set
14053 first_row_to_display to the first row that cannot be reused.
14054 Set pt_row to the row containing point, if there is any. */
14055 pt_row = NULL;
14056 for (first_row_to_display = first_reusable_row;
14057 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14058 ++first_row_to_display)
14059 {
14060 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14061 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14062 pt_row = first_row_to_display;
14063 }
14064
14065 /* Start displaying at the start of first_row_to_display. */
14066 xassert (first_row_to_display->y < yb);
14067 init_to_row_start (&it, w, first_row_to_display);
14068
14069 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14070 - start_vpos);
14071 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14072 - nrows_scrolled);
14073 it.current_y = (first_row_to_display->y - first_reusable_row->y
14074 + WINDOW_HEADER_LINE_HEIGHT (w));
14075
14076 /* Display lines beginning with first_row_to_display in the
14077 desired matrix. Set last_text_row to the last row displayed
14078 that displays text. */
14079 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14080 if (pt_row == NULL)
14081 w->cursor.vpos = -1;
14082 last_text_row = NULL;
14083 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14084 if (display_line (&it))
14085 last_text_row = it.glyph_row - 1;
14086
14087 /* Give up If point isn't in a row displayed or reused. */
14088 if (w->cursor.vpos < 0)
14089 {
14090 clear_glyph_matrix (w->desired_matrix);
14091 return 0;
14092 }
14093
14094 /* If point is in a reused row, adjust y and vpos of the cursor
14095 position. */
14096 if (pt_row)
14097 {
14098 w->cursor.vpos -= nrows_scrolled;
14099 w->cursor.y -= first_reusable_row->y - start_row->y;
14100 }
14101
14102 /* Scroll the display. */
14103 run.current_y = first_reusable_row->y;
14104 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14105 run.height = it.last_visible_y - run.current_y;
14106 dy = run.current_y - run.desired_y;
14107
14108 if (run.height)
14109 {
14110 update_begin (f);
14111 rif->update_window_begin_hook (w);
14112 rif->clear_window_mouse_face (w);
14113 rif->scroll_run_hook (w, &run);
14114 rif->update_window_end_hook (w, 0, 0);
14115 update_end (f);
14116 }
14117
14118 /* Adjust Y positions of reused rows. */
14119 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14120 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14121 max_y = it.last_visible_y;
14122 for (row = first_reusable_row; row < first_row_to_display; ++row)
14123 {
14124 row->y -= dy;
14125 row->visible_height = row->height;
14126 if (row->y < min_y)
14127 row->visible_height -= min_y - row->y;
14128 if (row->y + row->height > max_y)
14129 row->visible_height -= row->y + row->height - max_y;
14130 row->redraw_fringe_bitmaps_p = 1;
14131 }
14132
14133 /* Scroll the current matrix. */
14134 xassert (nrows_scrolled > 0);
14135 rotate_matrix (w->current_matrix,
14136 start_vpos,
14137 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14138 -nrows_scrolled);
14139
14140 /* Disable rows not reused. */
14141 for (row -= nrows_scrolled; row < bottom_row; ++row)
14142 row->enabled_p = 0;
14143
14144 /* Point may have moved to a different line, so we cannot assume that
14145 the previous cursor position is valid; locate the correct row. */
14146 if (pt_row)
14147 {
14148 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14149 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14150 row++)
14151 {
14152 w->cursor.vpos++;
14153 w->cursor.y = row->y;
14154 }
14155 if (row < bottom_row)
14156 {
14157 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14158 while (glyph->charpos < PT)
14159 {
14160 w->cursor.hpos++;
14161 w->cursor.x += glyph->pixel_width;
14162 glyph++;
14163 }
14164 }
14165 }
14166
14167 /* Adjust window end. A null value of last_text_row means that
14168 the window end is in reused rows which in turn means that
14169 only its vpos can have changed. */
14170 if (last_text_row)
14171 {
14172 w->window_end_bytepos
14173 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14174 w->window_end_pos
14175 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14176 w->window_end_vpos
14177 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14178 }
14179 else
14180 {
14181 w->window_end_vpos
14182 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14183 }
14184
14185 w->window_end_valid = Qnil;
14186 w->desired_matrix->no_scrolling_p = 1;
14187
14188 #if GLYPH_DEBUG
14189 debug_method_add (w, "try_window_reusing_current_matrix 2");
14190 #endif
14191 return 1;
14192 }
14193
14194 return 0;
14195 }
14196
14197
14198 \f
14199 /************************************************************************
14200 Window redisplay reusing current matrix when buffer has changed
14201 ************************************************************************/
14202
14203 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14204 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14205 int *, int *));
14206 static struct glyph_row *
14207 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14208 struct glyph_row *));
14209
14210
14211 /* Return the last row in MATRIX displaying text. If row START is
14212 non-null, start searching with that row. IT gives the dimensions
14213 of the display. Value is null if matrix is empty; otherwise it is
14214 a pointer to the row found. */
14215
14216 static struct glyph_row *
14217 find_last_row_displaying_text (matrix, it, start)
14218 struct glyph_matrix *matrix;
14219 struct it *it;
14220 struct glyph_row *start;
14221 {
14222 struct glyph_row *row, *row_found;
14223
14224 /* Set row_found to the last row in IT->w's current matrix
14225 displaying text. The loop looks funny but think of partially
14226 visible lines. */
14227 row_found = NULL;
14228 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14229 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14230 {
14231 xassert (row->enabled_p);
14232 row_found = row;
14233 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14234 break;
14235 ++row;
14236 }
14237
14238 return row_found;
14239 }
14240
14241
14242 /* Return the last row in the current matrix of W that is not affected
14243 by changes at the start of current_buffer that occurred since W's
14244 current matrix was built. Value is null if no such row exists.
14245
14246 BEG_UNCHANGED us the number of characters unchanged at the start of
14247 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14248 first changed character in current_buffer. Characters at positions <
14249 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14250 when the current matrix was built. */
14251
14252 static struct glyph_row *
14253 find_last_unchanged_at_beg_row (w)
14254 struct window *w;
14255 {
14256 int first_changed_pos = BEG + BEG_UNCHANGED;
14257 struct glyph_row *row;
14258 struct glyph_row *row_found = NULL;
14259 int yb = window_text_bottom_y (w);
14260
14261 /* Find the last row displaying unchanged text. */
14262 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14263 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14264 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14265 {
14266 if (/* If row ends before first_changed_pos, it is unchanged,
14267 except in some case. */
14268 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14269 /* When row ends in ZV and we write at ZV it is not
14270 unchanged. */
14271 && !row->ends_at_zv_p
14272 /* When first_changed_pos is the end of a continued line,
14273 row is not unchanged because it may be no longer
14274 continued. */
14275 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14276 && (row->continued_p
14277 || row->exact_window_width_line_p)))
14278 row_found = row;
14279
14280 /* Stop if last visible row. */
14281 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14282 break;
14283
14284 ++row;
14285 }
14286
14287 return row_found;
14288 }
14289
14290
14291 /* Find the first glyph row in the current matrix of W that is not
14292 affected by changes at the end of current_buffer since the
14293 time W's current matrix was built.
14294
14295 Return in *DELTA the number of chars by which buffer positions in
14296 unchanged text at the end of current_buffer must be adjusted.
14297
14298 Return in *DELTA_BYTES the corresponding number of bytes.
14299
14300 Value is null if no such row exists, i.e. all rows are affected by
14301 changes. */
14302
14303 static struct glyph_row *
14304 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14305 struct window *w;
14306 int *delta, *delta_bytes;
14307 {
14308 struct glyph_row *row;
14309 struct glyph_row *row_found = NULL;
14310
14311 *delta = *delta_bytes = 0;
14312
14313 /* Display must not have been paused, otherwise the current matrix
14314 is not up to date. */
14315 if (NILP (w->window_end_valid))
14316 abort ();
14317
14318 /* A value of window_end_pos >= END_UNCHANGED means that the window
14319 end is in the range of changed text. If so, there is no
14320 unchanged row at the end of W's current matrix. */
14321 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14322 return NULL;
14323
14324 /* Set row to the last row in W's current matrix displaying text. */
14325 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14326
14327 /* If matrix is entirely empty, no unchanged row exists. */
14328 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14329 {
14330 /* The value of row is the last glyph row in the matrix having a
14331 meaningful buffer position in it. The end position of row
14332 corresponds to window_end_pos. This allows us to translate
14333 buffer positions in the current matrix to current buffer
14334 positions for characters not in changed text. */
14335 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14336 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14337 int last_unchanged_pos, last_unchanged_pos_old;
14338 struct glyph_row *first_text_row
14339 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14340
14341 *delta = Z - Z_old;
14342 *delta_bytes = Z_BYTE - Z_BYTE_old;
14343
14344 /* Set last_unchanged_pos to the buffer position of the last
14345 character in the buffer that has not been changed. Z is the
14346 index + 1 of the last character in current_buffer, i.e. by
14347 subtracting END_UNCHANGED we get the index of the last
14348 unchanged character, and we have to add BEG to get its buffer
14349 position. */
14350 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14351 last_unchanged_pos_old = last_unchanged_pos - *delta;
14352
14353 /* Search backward from ROW for a row displaying a line that
14354 starts at a minimum position >= last_unchanged_pos_old. */
14355 for (; row > first_text_row; --row)
14356 {
14357 /* This used to abort, but it can happen.
14358 It is ok to just stop the search instead here. KFS. */
14359 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14360 break;
14361
14362 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14363 row_found = row;
14364 }
14365 }
14366
14367 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14368 abort ();
14369
14370 return row_found;
14371 }
14372
14373
14374 /* Make sure that glyph rows in the current matrix of window W
14375 reference the same glyph memory as corresponding rows in the
14376 frame's frame matrix. This function is called after scrolling W's
14377 current matrix on a terminal frame in try_window_id and
14378 try_window_reusing_current_matrix. */
14379
14380 static void
14381 sync_frame_with_window_matrix_rows (w)
14382 struct window *w;
14383 {
14384 struct frame *f = XFRAME (w->frame);
14385 struct glyph_row *window_row, *window_row_end, *frame_row;
14386
14387 /* Preconditions: W must be a leaf window and full-width. Its frame
14388 must have a frame matrix. */
14389 xassert (NILP (w->hchild) && NILP (w->vchild));
14390 xassert (WINDOW_FULL_WIDTH_P (w));
14391 xassert (!FRAME_WINDOW_P (f));
14392
14393 /* If W is a full-width window, glyph pointers in W's current matrix
14394 have, by definition, to be the same as glyph pointers in the
14395 corresponding frame matrix. Note that frame matrices have no
14396 marginal areas (see build_frame_matrix). */
14397 window_row = w->current_matrix->rows;
14398 window_row_end = window_row + w->current_matrix->nrows;
14399 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14400 while (window_row < window_row_end)
14401 {
14402 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14403 struct glyph *end = window_row->glyphs[LAST_AREA];
14404
14405 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14406 frame_row->glyphs[TEXT_AREA] = start;
14407 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14408 frame_row->glyphs[LAST_AREA] = end;
14409
14410 /* Disable frame rows whose corresponding window rows have
14411 been disabled in try_window_id. */
14412 if (!window_row->enabled_p)
14413 frame_row->enabled_p = 0;
14414
14415 ++window_row, ++frame_row;
14416 }
14417 }
14418
14419
14420 /* Find the glyph row in window W containing CHARPOS. Consider all
14421 rows between START and END (not inclusive). END null means search
14422 all rows to the end of the display area of W. Value is the row
14423 containing CHARPOS or null. */
14424
14425 struct glyph_row *
14426 row_containing_pos (w, charpos, start, end, dy)
14427 struct window *w;
14428 int charpos;
14429 struct glyph_row *start, *end;
14430 int dy;
14431 {
14432 struct glyph_row *row = start;
14433 int last_y;
14434
14435 /* If we happen to start on a header-line, skip that. */
14436 if (row->mode_line_p)
14437 ++row;
14438
14439 if ((end && row >= end) || !row->enabled_p)
14440 return NULL;
14441
14442 last_y = window_text_bottom_y (w) - dy;
14443
14444 while (1)
14445 {
14446 /* Give up if we have gone too far. */
14447 if (end && row >= end)
14448 return NULL;
14449 /* This formerly returned if they were equal.
14450 I think that both quantities are of a "last plus one" type;
14451 if so, when they are equal, the row is within the screen. -- rms. */
14452 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14453 return NULL;
14454
14455 /* If it is in this row, return this row. */
14456 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14457 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14458 /* The end position of a row equals the start
14459 position of the next row. If CHARPOS is there, we
14460 would rather display it in the next line, except
14461 when this line ends in ZV. */
14462 && !row->ends_at_zv_p
14463 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14464 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14465 return row;
14466 ++row;
14467 }
14468 }
14469
14470
14471 /* Try to redisplay window W by reusing its existing display. W's
14472 current matrix must be up to date when this function is called,
14473 i.e. window_end_valid must not be nil.
14474
14475 Value is
14476
14477 1 if display has been updated
14478 0 if otherwise unsuccessful
14479 -1 if redisplay with same window start is known not to succeed
14480
14481 The following steps are performed:
14482
14483 1. Find the last row in the current matrix of W that is not
14484 affected by changes at the start of current_buffer. If no such row
14485 is found, give up.
14486
14487 2. Find the first row in W's current matrix that is not affected by
14488 changes at the end of current_buffer. Maybe there is no such row.
14489
14490 3. Display lines beginning with the row + 1 found in step 1 to the
14491 row found in step 2 or, if step 2 didn't find a row, to the end of
14492 the window.
14493
14494 4. If cursor is not known to appear on the window, give up.
14495
14496 5. If display stopped at the row found in step 2, scroll the
14497 display and current matrix as needed.
14498
14499 6. Maybe display some lines at the end of W, if we must. This can
14500 happen under various circumstances, like a partially visible line
14501 becoming fully visible, or because newly displayed lines are displayed
14502 in smaller font sizes.
14503
14504 7. Update W's window end information. */
14505
14506 static int
14507 try_window_id (w)
14508 struct window *w;
14509 {
14510 struct frame *f = XFRAME (w->frame);
14511 struct glyph_matrix *current_matrix = w->current_matrix;
14512 struct glyph_matrix *desired_matrix = w->desired_matrix;
14513 struct glyph_row *last_unchanged_at_beg_row;
14514 struct glyph_row *first_unchanged_at_end_row;
14515 struct glyph_row *row;
14516 struct glyph_row *bottom_row;
14517 int bottom_vpos;
14518 struct it it;
14519 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14520 struct text_pos start_pos;
14521 struct run run;
14522 int first_unchanged_at_end_vpos = 0;
14523 struct glyph_row *last_text_row, *last_text_row_at_end;
14524 struct text_pos start;
14525 int first_changed_charpos, last_changed_charpos;
14526
14527 #if GLYPH_DEBUG
14528 if (inhibit_try_window_id)
14529 return 0;
14530 #endif
14531
14532 /* This is handy for debugging. */
14533 #if 0
14534 #define GIVE_UP(X) \
14535 do { \
14536 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14537 return 0; \
14538 } while (0)
14539 #else
14540 #define GIVE_UP(X) return 0
14541 #endif
14542
14543 SET_TEXT_POS_FROM_MARKER (start, w->start);
14544
14545 /* Don't use this for mini-windows because these can show
14546 messages and mini-buffers, and we don't handle that here. */
14547 if (MINI_WINDOW_P (w))
14548 GIVE_UP (1);
14549
14550 /* This flag is used to prevent redisplay optimizations. */
14551 if (windows_or_buffers_changed || cursor_type_changed)
14552 GIVE_UP (2);
14553
14554 /* Verify that narrowing has not changed.
14555 Also verify that we were not told to prevent redisplay optimizations.
14556 It would be nice to further
14557 reduce the number of cases where this prevents try_window_id. */
14558 if (current_buffer->clip_changed
14559 || current_buffer->prevent_redisplay_optimizations_p)
14560 GIVE_UP (3);
14561
14562 /* Window must either use window-based redisplay or be full width. */
14563 if (!FRAME_WINDOW_P (f)
14564 && (!line_ins_del_ok
14565 || !WINDOW_FULL_WIDTH_P (w)))
14566 GIVE_UP (4);
14567
14568 /* Give up if point is not known NOT to appear in W. */
14569 if (PT < CHARPOS (start))
14570 GIVE_UP (5);
14571
14572 /* Another way to prevent redisplay optimizations. */
14573 if (XFASTINT (w->last_modified) == 0)
14574 GIVE_UP (6);
14575
14576 /* Verify that window is not hscrolled. */
14577 if (XFASTINT (w->hscroll) != 0)
14578 GIVE_UP (7);
14579
14580 /* Verify that display wasn't paused. */
14581 if (NILP (w->window_end_valid))
14582 GIVE_UP (8);
14583
14584 /* Can't use this if highlighting a region because a cursor movement
14585 will do more than just set the cursor. */
14586 if (!NILP (Vtransient_mark_mode)
14587 && !NILP (current_buffer->mark_active))
14588 GIVE_UP (9);
14589
14590 /* Likewise if highlighting trailing whitespace. */
14591 if (!NILP (Vshow_trailing_whitespace))
14592 GIVE_UP (11);
14593
14594 /* Likewise if showing a region. */
14595 if (!NILP (w->region_showing))
14596 GIVE_UP (10);
14597
14598 /* Can use this if overlay arrow position and or string have changed. */
14599 if (overlay_arrows_changed_p ())
14600 GIVE_UP (12);
14601
14602
14603 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14604 only if buffer has really changed. The reason is that the gap is
14605 initially at Z for freshly visited files. The code below would
14606 set end_unchanged to 0 in that case. */
14607 if (MODIFF > SAVE_MODIFF
14608 /* This seems to happen sometimes after saving a buffer. */
14609 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14610 {
14611 if (GPT - BEG < BEG_UNCHANGED)
14612 BEG_UNCHANGED = GPT - BEG;
14613 if (Z - GPT < END_UNCHANGED)
14614 END_UNCHANGED = Z - GPT;
14615 }
14616
14617 /* The position of the first and last character that has been changed. */
14618 first_changed_charpos = BEG + BEG_UNCHANGED;
14619 last_changed_charpos = Z - END_UNCHANGED;
14620
14621 /* If window starts after a line end, and the last change is in
14622 front of that newline, then changes don't affect the display.
14623 This case happens with stealth-fontification. Note that although
14624 the display is unchanged, glyph positions in the matrix have to
14625 be adjusted, of course. */
14626 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14627 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14628 && ((last_changed_charpos < CHARPOS (start)
14629 && CHARPOS (start) == BEGV)
14630 || (last_changed_charpos < CHARPOS (start) - 1
14631 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14632 {
14633 int Z_old, delta, Z_BYTE_old, delta_bytes;
14634 struct glyph_row *r0;
14635
14636 /* Compute how many chars/bytes have been added to or removed
14637 from the buffer. */
14638 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14639 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14640 delta = Z - Z_old;
14641 delta_bytes = Z_BYTE - Z_BYTE_old;
14642
14643 /* Give up if PT is not in the window. Note that it already has
14644 been checked at the start of try_window_id that PT is not in
14645 front of the window start. */
14646 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14647 GIVE_UP (13);
14648
14649 /* If window start is unchanged, we can reuse the whole matrix
14650 as is, after adjusting glyph positions. No need to compute
14651 the window end again, since its offset from Z hasn't changed. */
14652 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14653 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14654 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14655 /* PT must not be in a partially visible line. */
14656 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14657 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14658 {
14659 /* Adjust positions in the glyph matrix. */
14660 if (delta || delta_bytes)
14661 {
14662 struct glyph_row *r1
14663 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14664 increment_matrix_positions (w->current_matrix,
14665 MATRIX_ROW_VPOS (r0, current_matrix),
14666 MATRIX_ROW_VPOS (r1, current_matrix),
14667 delta, delta_bytes);
14668 }
14669
14670 /* Set the cursor. */
14671 row = row_containing_pos (w, PT, r0, NULL, 0);
14672 if (row)
14673 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14674 else
14675 abort ();
14676 return 1;
14677 }
14678 }
14679
14680 /* Handle the case that changes are all below what is displayed in
14681 the window, and that PT is in the window. This shortcut cannot
14682 be taken if ZV is visible in the window, and text has been added
14683 there that is visible in the window. */
14684 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14685 /* ZV is not visible in the window, or there are no
14686 changes at ZV, actually. */
14687 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14688 || first_changed_charpos == last_changed_charpos))
14689 {
14690 struct glyph_row *r0;
14691
14692 /* Give up if PT is not in the window. Note that it already has
14693 been checked at the start of try_window_id that PT is not in
14694 front of the window start. */
14695 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14696 GIVE_UP (14);
14697
14698 /* If window start is unchanged, we can reuse the whole matrix
14699 as is, without changing glyph positions since no text has
14700 been added/removed in front of the window end. */
14701 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14702 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14703 /* PT must not be in a partially visible line. */
14704 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14705 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14706 {
14707 /* We have to compute the window end anew since text
14708 can have been added/removed after it. */
14709 w->window_end_pos
14710 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14711 w->window_end_bytepos
14712 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14713
14714 /* Set the cursor. */
14715 row = row_containing_pos (w, PT, r0, NULL, 0);
14716 if (row)
14717 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14718 else
14719 abort ();
14720 return 2;
14721 }
14722 }
14723
14724 /* Give up if window start is in the changed area.
14725
14726 The condition used to read
14727
14728 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14729
14730 but why that was tested escapes me at the moment. */
14731 if (CHARPOS (start) >= first_changed_charpos
14732 && CHARPOS (start) <= last_changed_charpos)
14733 GIVE_UP (15);
14734
14735 /* Check that window start agrees with the start of the first glyph
14736 row in its current matrix. Check this after we know the window
14737 start is not in changed text, otherwise positions would not be
14738 comparable. */
14739 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14740 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14741 GIVE_UP (16);
14742
14743 /* Give up if the window ends in strings. Overlay strings
14744 at the end are difficult to handle, so don't try. */
14745 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14746 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14747 GIVE_UP (20);
14748
14749 /* Compute the position at which we have to start displaying new
14750 lines. Some of the lines at the top of the window might be
14751 reusable because they are not displaying changed text. Find the
14752 last row in W's current matrix not affected by changes at the
14753 start of current_buffer. Value is null if changes start in the
14754 first line of window. */
14755 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14756 if (last_unchanged_at_beg_row)
14757 {
14758 /* Avoid starting to display in the moddle of a character, a TAB
14759 for instance. This is easier than to set up the iterator
14760 exactly, and it's not a frequent case, so the additional
14761 effort wouldn't really pay off. */
14762 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14763 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14764 && last_unchanged_at_beg_row > w->current_matrix->rows)
14765 --last_unchanged_at_beg_row;
14766
14767 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14768 GIVE_UP (17);
14769
14770 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14771 GIVE_UP (18);
14772 start_pos = it.current.pos;
14773
14774 /* Start displaying new lines in the desired matrix at the same
14775 vpos we would use in the current matrix, i.e. below
14776 last_unchanged_at_beg_row. */
14777 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14778 current_matrix);
14779 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14780 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14781
14782 xassert (it.hpos == 0 && it.current_x == 0);
14783 }
14784 else
14785 {
14786 /* There are no reusable lines at the start of the window.
14787 Start displaying in the first text line. */
14788 start_display (&it, w, start);
14789 it.vpos = it.first_vpos;
14790 start_pos = it.current.pos;
14791 }
14792
14793 /* Find the first row that is not affected by changes at the end of
14794 the buffer. Value will be null if there is no unchanged row, in
14795 which case we must redisplay to the end of the window. delta
14796 will be set to the value by which buffer positions beginning with
14797 first_unchanged_at_end_row have to be adjusted due to text
14798 changes. */
14799 first_unchanged_at_end_row
14800 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14801 IF_DEBUG (debug_delta = delta);
14802 IF_DEBUG (debug_delta_bytes = delta_bytes);
14803
14804 /* Set stop_pos to the buffer position up to which we will have to
14805 display new lines. If first_unchanged_at_end_row != NULL, this
14806 is the buffer position of the start of the line displayed in that
14807 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14808 that we don't stop at a buffer position. */
14809 stop_pos = 0;
14810 if (first_unchanged_at_end_row)
14811 {
14812 xassert (last_unchanged_at_beg_row == NULL
14813 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14814
14815 /* If this is a continuation line, move forward to the next one
14816 that isn't. Changes in lines above affect this line.
14817 Caution: this may move first_unchanged_at_end_row to a row
14818 not displaying text. */
14819 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14820 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14821 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14822 < it.last_visible_y))
14823 ++first_unchanged_at_end_row;
14824
14825 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14826 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14827 >= it.last_visible_y))
14828 first_unchanged_at_end_row = NULL;
14829 else
14830 {
14831 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14832 + delta);
14833 first_unchanged_at_end_vpos
14834 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14835 xassert (stop_pos >= Z - END_UNCHANGED);
14836 }
14837 }
14838 else if (last_unchanged_at_beg_row == NULL)
14839 GIVE_UP (19);
14840
14841
14842 #if GLYPH_DEBUG
14843
14844 /* Either there is no unchanged row at the end, or the one we have
14845 now displays text. This is a necessary condition for the window
14846 end pos calculation at the end of this function. */
14847 xassert (first_unchanged_at_end_row == NULL
14848 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14849
14850 debug_last_unchanged_at_beg_vpos
14851 = (last_unchanged_at_beg_row
14852 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14853 : -1);
14854 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14855
14856 #endif /* GLYPH_DEBUG != 0 */
14857
14858
14859 /* Display new lines. Set last_text_row to the last new line
14860 displayed which has text on it, i.e. might end up as being the
14861 line where the window_end_vpos is. */
14862 w->cursor.vpos = -1;
14863 last_text_row = NULL;
14864 overlay_arrow_seen = 0;
14865 while (it.current_y < it.last_visible_y
14866 && !fonts_changed_p
14867 && (first_unchanged_at_end_row == NULL
14868 || IT_CHARPOS (it) < stop_pos))
14869 {
14870 if (display_line (&it))
14871 last_text_row = it.glyph_row - 1;
14872 }
14873
14874 if (fonts_changed_p)
14875 return -1;
14876
14877
14878 /* Compute differences in buffer positions, y-positions etc. for
14879 lines reused at the bottom of the window. Compute what we can
14880 scroll. */
14881 if (first_unchanged_at_end_row
14882 /* No lines reused because we displayed everything up to the
14883 bottom of the window. */
14884 && it.current_y < it.last_visible_y)
14885 {
14886 dvpos = (it.vpos
14887 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14888 current_matrix));
14889 dy = it.current_y - first_unchanged_at_end_row->y;
14890 run.current_y = first_unchanged_at_end_row->y;
14891 run.desired_y = run.current_y + dy;
14892 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14893 }
14894 else
14895 {
14896 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14897 first_unchanged_at_end_row = NULL;
14898 }
14899 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14900
14901
14902 /* Find the cursor if not already found. We have to decide whether
14903 PT will appear on this window (it sometimes doesn't, but this is
14904 not a very frequent case.) This decision has to be made before
14905 the current matrix is altered. A value of cursor.vpos < 0 means
14906 that PT is either in one of the lines beginning at
14907 first_unchanged_at_end_row or below the window. Don't care for
14908 lines that might be displayed later at the window end; as
14909 mentioned, this is not a frequent case. */
14910 if (w->cursor.vpos < 0)
14911 {
14912 /* Cursor in unchanged rows at the top? */
14913 if (PT < CHARPOS (start_pos)
14914 && last_unchanged_at_beg_row)
14915 {
14916 row = row_containing_pos (w, PT,
14917 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14918 last_unchanged_at_beg_row + 1, 0);
14919 if (row)
14920 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14921 }
14922
14923 /* Start from first_unchanged_at_end_row looking for PT. */
14924 else if (first_unchanged_at_end_row)
14925 {
14926 row = row_containing_pos (w, PT - delta,
14927 first_unchanged_at_end_row, NULL, 0);
14928 if (row)
14929 set_cursor_from_row (w, row, w->current_matrix, delta,
14930 delta_bytes, dy, dvpos);
14931 }
14932
14933 /* Give up if cursor was not found. */
14934 if (w->cursor.vpos < 0)
14935 {
14936 clear_glyph_matrix (w->desired_matrix);
14937 return -1;
14938 }
14939 }
14940
14941 /* Don't let the cursor end in the scroll margins. */
14942 {
14943 int this_scroll_margin, cursor_height;
14944
14945 this_scroll_margin = max (0, scroll_margin);
14946 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14947 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14948 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14949
14950 if ((w->cursor.y < this_scroll_margin
14951 && CHARPOS (start) > BEGV)
14952 /* Old redisplay didn't take scroll margin into account at the bottom,
14953 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14954 || (w->cursor.y + (make_cursor_line_fully_visible_p
14955 ? cursor_height + this_scroll_margin
14956 : 1)) > it.last_visible_y)
14957 {
14958 w->cursor.vpos = -1;
14959 clear_glyph_matrix (w->desired_matrix);
14960 return -1;
14961 }
14962 }
14963
14964 /* Scroll the display. Do it before changing the current matrix so
14965 that xterm.c doesn't get confused about where the cursor glyph is
14966 found. */
14967 if (dy && run.height)
14968 {
14969 update_begin (f);
14970
14971 if (FRAME_WINDOW_P (f))
14972 {
14973 rif->update_window_begin_hook (w);
14974 rif->clear_window_mouse_face (w);
14975 rif->scroll_run_hook (w, &run);
14976 rif->update_window_end_hook (w, 0, 0);
14977 }
14978 else
14979 {
14980 /* Terminal frame. In this case, dvpos gives the number of
14981 lines to scroll by; dvpos < 0 means scroll up. */
14982 int first_unchanged_at_end_vpos
14983 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14984 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14985 int end = (WINDOW_TOP_EDGE_LINE (w)
14986 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14987 + window_internal_height (w));
14988
14989 /* Perform the operation on the screen. */
14990 if (dvpos > 0)
14991 {
14992 /* Scroll last_unchanged_at_beg_row to the end of the
14993 window down dvpos lines. */
14994 set_terminal_window (end);
14995
14996 /* On dumb terminals delete dvpos lines at the end
14997 before inserting dvpos empty lines. */
14998 if (!scroll_region_ok)
14999 ins_del_lines (end - dvpos, -dvpos);
15000
15001 /* Insert dvpos empty lines in front of
15002 last_unchanged_at_beg_row. */
15003 ins_del_lines (from, dvpos);
15004 }
15005 else if (dvpos < 0)
15006 {
15007 /* Scroll up last_unchanged_at_beg_vpos to the end of
15008 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15009 set_terminal_window (end);
15010
15011 /* Delete dvpos lines in front of
15012 last_unchanged_at_beg_vpos. ins_del_lines will set
15013 the cursor to the given vpos and emit |dvpos| delete
15014 line sequences. */
15015 ins_del_lines (from + dvpos, dvpos);
15016
15017 /* On a dumb terminal insert dvpos empty lines at the
15018 end. */
15019 if (!scroll_region_ok)
15020 ins_del_lines (end + dvpos, -dvpos);
15021 }
15022
15023 set_terminal_window (0);
15024 }
15025
15026 update_end (f);
15027 }
15028
15029 /* Shift reused rows of the current matrix to the right position.
15030 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15031 text. */
15032 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15033 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15034 if (dvpos < 0)
15035 {
15036 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15037 bottom_vpos, dvpos);
15038 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15039 bottom_vpos, 0);
15040 }
15041 else if (dvpos > 0)
15042 {
15043 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15044 bottom_vpos, dvpos);
15045 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15046 first_unchanged_at_end_vpos + dvpos, 0);
15047 }
15048
15049 /* For frame-based redisplay, make sure that current frame and window
15050 matrix are in sync with respect to glyph memory. */
15051 if (!FRAME_WINDOW_P (f))
15052 sync_frame_with_window_matrix_rows (w);
15053
15054 /* Adjust buffer positions in reused rows. */
15055 if (delta || delta_bytes)
15056 increment_matrix_positions (current_matrix,
15057 first_unchanged_at_end_vpos + dvpos,
15058 bottom_vpos, delta, delta_bytes);
15059
15060 /* Adjust Y positions. */
15061 if (dy)
15062 shift_glyph_matrix (w, current_matrix,
15063 first_unchanged_at_end_vpos + dvpos,
15064 bottom_vpos, dy);
15065
15066 if (first_unchanged_at_end_row)
15067 {
15068 first_unchanged_at_end_row += dvpos;
15069 if (first_unchanged_at_end_row->y >= it.last_visible_y
15070 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15071 first_unchanged_at_end_row = NULL;
15072 }
15073
15074 /* If scrolling up, there may be some lines to display at the end of
15075 the window. */
15076 last_text_row_at_end = NULL;
15077 if (dy < 0)
15078 {
15079 /* Scrolling up can leave for example a partially visible line
15080 at the end of the window to be redisplayed. */
15081 /* Set last_row to the glyph row in the current matrix where the
15082 window end line is found. It has been moved up or down in
15083 the matrix by dvpos. */
15084 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15085 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15086
15087 /* If last_row is the window end line, it should display text. */
15088 xassert (last_row->displays_text_p);
15089
15090 /* If window end line was partially visible before, begin
15091 displaying at that line. Otherwise begin displaying with the
15092 line following it. */
15093 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15094 {
15095 init_to_row_start (&it, w, last_row);
15096 it.vpos = last_vpos;
15097 it.current_y = last_row->y;
15098 }
15099 else
15100 {
15101 init_to_row_end (&it, w, last_row);
15102 it.vpos = 1 + last_vpos;
15103 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15104 ++last_row;
15105 }
15106
15107 /* We may start in a continuation line. If so, we have to
15108 get the right continuation_lines_width and current_x. */
15109 it.continuation_lines_width = last_row->continuation_lines_width;
15110 it.hpos = it.current_x = 0;
15111
15112 /* Display the rest of the lines at the window end. */
15113 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15114 while (it.current_y < it.last_visible_y
15115 && !fonts_changed_p)
15116 {
15117 /* Is it always sure that the display agrees with lines in
15118 the current matrix? I don't think so, so we mark rows
15119 displayed invalid in the current matrix by setting their
15120 enabled_p flag to zero. */
15121 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15122 if (display_line (&it))
15123 last_text_row_at_end = it.glyph_row - 1;
15124 }
15125 }
15126
15127 /* Update window_end_pos and window_end_vpos. */
15128 if (first_unchanged_at_end_row
15129 && !last_text_row_at_end)
15130 {
15131 /* Window end line if one of the preserved rows from the current
15132 matrix. Set row to the last row displaying text in current
15133 matrix starting at first_unchanged_at_end_row, after
15134 scrolling. */
15135 xassert (first_unchanged_at_end_row->displays_text_p);
15136 row = find_last_row_displaying_text (w->current_matrix, &it,
15137 first_unchanged_at_end_row);
15138 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15139
15140 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15141 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15142 w->window_end_vpos
15143 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15144 xassert (w->window_end_bytepos >= 0);
15145 IF_DEBUG (debug_method_add (w, "A"));
15146 }
15147 else if (last_text_row_at_end)
15148 {
15149 w->window_end_pos
15150 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15151 w->window_end_bytepos
15152 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15153 w->window_end_vpos
15154 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15155 xassert (w->window_end_bytepos >= 0);
15156 IF_DEBUG (debug_method_add (w, "B"));
15157 }
15158 else if (last_text_row)
15159 {
15160 /* We have displayed either to the end of the window or at the
15161 end of the window, i.e. the last row with text is to be found
15162 in the desired matrix. */
15163 w->window_end_pos
15164 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15165 w->window_end_bytepos
15166 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15167 w->window_end_vpos
15168 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15169 xassert (w->window_end_bytepos >= 0);
15170 }
15171 else if (first_unchanged_at_end_row == NULL
15172 && last_text_row == NULL
15173 && last_text_row_at_end == NULL)
15174 {
15175 /* Displayed to end of window, but no line containing text was
15176 displayed. Lines were deleted at the end of the window. */
15177 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15178 int vpos = XFASTINT (w->window_end_vpos);
15179 struct glyph_row *current_row = current_matrix->rows + vpos;
15180 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15181
15182 for (row = NULL;
15183 row == NULL && vpos >= first_vpos;
15184 --vpos, --current_row, --desired_row)
15185 {
15186 if (desired_row->enabled_p)
15187 {
15188 if (desired_row->displays_text_p)
15189 row = desired_row;
15190 }
15191 else if (current_row->displays_text_p)
15192 row = current_row;
15193 }
15194
15195 xassert (row != NULL);
15196 w->window_end_vpos = make_number (vpos + 1);
15197 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15198 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15199 xassert (w->window_end_bytepos >= 0);
15200 IF_DEBUG (debug_method_add (w, "C"));
15201 }
15202 else
15203 abort ();
15204
15205 #if 0 /* This leads to problems, for instance when the cursor is
15206 at ZV, and the cursor line displays no text. */
15207 /* Disable rows below what's displayed in the window. This makes
15208 debugging easier. */
15209 enable_glyph_matrix_rows (current_matrix,
15210 XFASTINT (w->window_end_vpos) + 1,
15211 bottom_vpos, 0);
15212 #endif
15213
15214 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15215 debug_end_vpos = XFASTINT (w->window_end_vpos));
15216
15217 /* Record that display has not been completed. */
15218 w->window_end_valid = Qnil;
15219 w->desired_matrix->no_scrolling_p = 1;
15220 return 3;
15221
15222 #undef GIVE_UP
15223 }
15224
15225
15226 \f
15227 /***********************************************************************
15228 More debugging support
15229 ***********************************************************************/
15230
15231 #if GLYPH_DEBUG
15232
15233 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15234 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15235 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15236
15237
15238 /* Dump the contents of glyph matrix MATRIX on stderr.
15239
15240 GLYPHS 0 means don't show glyph contents.
15241 GLYPHS 1 means show glyphs in short form
15242 GLYPHS > 1 means show glyphs in long form. */
15243
15244 void
15245 dump_glyph_matrix (matrix, glyphs)
15246 struct glyph_matrix *matrix;
15247 int glyphs;
15248 {
15249 int i;
15250 for (i = 0; i < matrix->nrows; ++i)
15251 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15252 }
15253
15254
15255 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15256 the glyph row and area where the glyph comes from. */
15257
15258 void
15259 dump_glyph (row, glyph, area)
15260 struct glyph_row *row;
15261 struct glyph *glyph;
15262 int area;
15263 {
15264 if (glyph->type == CHAR_GLYPH)
15265 {
15266 fprintf (stderr,
15267 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15268 glyph - row->glyphs[TEXT_AREA],
15269 'C',
15270 glyph->charpos,
15271 (BUFFERP (glyph->object)
15272 ? 'B'
15273 : (STRINGP (glyph->object)
15274 ? 'S'
15275 : '-')),
15276 glyph->pixel_width,
15277 glyph->u.ch,
15278 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15279 ? glyph->u.ch
15280 : '.'),
15281 glyph->face_id,
15282 glyph->left_box_line_p,
15283 glyph->right_box_line_p);
15284 }
15285 else if (glyph->type == STRETCH_GLYPH)
15286 {
15287 fprintf (stderr,
15288 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15289 glyph - row->glyphs[TEXT_AREA],
15290 'S',
15291 glyph->charpos,
15292 (BUFFERP (glyph->object)
15293 ? 'B'
15294 : (STRINGP (glyph->object)
15295 ? 'S'
15296 : '-')),
15297 glyph->pixel_width,
15298 0,
15299 '.',
15300 glyph->face_id,
15301 glyph->left_box_line_p,
15302 glyph->right_box_line_p);
15303 }
15304 else if (glyph->type == IMAGE_GLYPH)
15305 {
15306 fprintf (stderr,
15307 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15308 glyph - row->glyphs[TEXT_AREA],
15309 'I',
15310 glyph->charpos,
15311 (BUFFERP (glyph->object)
15312 ? 'B'
15313 : (STRINGP (glyph->object)
15314 ? 'S'
15315 : '-')),
15316 glyph->pixel_width,
15317 glyph->u.img_id,
15318 '.',
15319 glyph->face_id,
15320 glyph->left_box_line_p,
15321 glyph->right_box_line_p);
15322 }
15323 else if (glyph->type == COMPOSITE_GLYPH)
15324 {
15325 fprintf (stderr,
15326 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15327 glyph - row->glyphs[TEXT_AREA],
15328 '+',
15329 glyph->charpos,
15330 (BUFFERP (glyph->object)
15331 ? 'B'
15332 : (STRINGP (glyph->object)
15333 ? 'S'
15334 : '-')),
15335 glyph->pixel_width,
15336 glyph->u.cmp_id,
15337 '.',
15338 glyph->face_id,
15339 glyph->left_box_line_p,
15340 glyph->right_box_line_p);
15341 }
15342 }
15343
15344
15345 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15346 GLYPHS 0 means don't show glyph contents.
15347 GLYPHS 1 means show glyphs in short form
15348 GLYPHS > 1 means show glyphs in long form. */
15349
15350 void
15351 dump_glyph_row (row, vpos, glyphs)
15352 struct glyph_row *row;
15353 int vpos, glyphs;
15354 {
15355 if (glyphs != 1)
15356 {
15357 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15358 fprintf (stderr, "======================================================================\n");
15359
15360 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15361 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15362 vpos,
15363 MATRIX_ROW_START_CHARPOS (row),
15364 MATRIX_ROW_END_CHARPOS (row),
15365 row->used[TEXT_AREA],
15366 row->contains_overlapping_glyphs_p,
15367 row->enabled_p,
15368 row->truncated_on_left_p,
15369 row->truncated_on_right_p,
15370 row->continued_p,
15371 MATRIX_ROW_CONTINUATION_LINE_P (row),
15372 row->displays_text_p,
15373 row->ends_at_zv_p,
15374 row->fill_line_p,
15375 row->ends_in_middle_of_char_p,
15376 row->starts_in_middle_of_char_p,
15377 row->mouse_face_p,
15378 row->x,
15379 row->y,
15380 row->pixel_width,
15381 row->height,
15382 row->visible_height,
15383 row->ascent,
15384 row->phys_ascent);
15385 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15386 row->end.overlay_string_index,
15387 row->continuation_lines_width);
15388 fprintf (stderr, "%9d %5d\n",
15389 CHARPOS (row->start.string_pos),
15390 CHARPOS (row->end.string_pos));
15391 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15392 row->end.dpvec_index);
15393 }
15394
15395 if (glyphs > 1)
15396 {
15397 int area;
15398
15399 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15400 {
15401 struct glyph *glyph = row->glyphs[area];
15402 struct glyph *glyph_end = glyph + row->used[area];
15403
15404 /* Glyph for a line end in text. */
15405 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15406 ++glyph_end;
15407
15408 if (glyph < glyph_end)
15409 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15410
15411 for (; glyph < glyph_end; ++glyph)
15412 dump_glyph (row, glyph, area);
15413 }
15414 }
15415 else if (glyphs == 1)
15416 {
15417 int area;
15418
15419 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15420 {
15421 char *s = (char *) alloca (row->used[area] + 1);
15422 int i;
15423
15424 for (i = 0; i < row->used[area]; ++i)
15425 {
15426 struct glyph *glyph = row->glyphs[area] + i;
15427 if (glyph->type == CHAR_GLYPH
15428 && glyph->u.ch < 0x80
15429 && glyph->u.ch >= ' ')
15430 s[i] = glyph->u.ch;
15431 else
15432 s[i] = '.';
15433 }
15434
15435 s[i] = '\0';
15436 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15437 }
15438 }
15439 }
15440
15441
15442 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15443 Sdump_glyph_matrix, 0, 1, "p",
15444 doc: /* Dump the current matrix of the selected window to stderr.
15445 Shows contents of glyph row structures. With non-nil
15446 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15447 glyphs in short form, otherwise show glyphs in long form. */)
15448 (glyphs)
15449 Lisp_Object glyphs;
15450 {
15451 struct window *w = XWINDOW (selected_window);
15452 struct buffer *buffer = XBUFFER (w->buffer);
15453
15454 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15455 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15456 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15457 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15458 fprintf (stderr, "=============================================\n");
15459 dump_glyph_matrix (w->current_matrix,
15460 NILP (glyphs) ? 0 : XINT (glyphs));
15461 return Qnil;
15462 }
15463
15464
15465 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15466 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15467 ()
15468 {
15469 struct frame *f = XFRAME (selected_frame);
15470 dump_glyph_matrix (f->current_matrix, 1);
15471 return Qnil;
15472 }
15473
15474
15475 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15476 doc: /* Dump glyph row ROW to stderr.
15477 GLYPH 0 means don't dump glyphs.
15478 GLYPH 1 means dump glyphs in short form.
15479 GLYPH > 1 or omitted means dump glyphs in long form. */)
15480 (row, glyphs)
15481 Lisp_Object row, glyphs;
15482 {
15483 struct glyph_matrix *matrix;
15484 int vpos;
15485
15486 CHECK_NUMBER (row);
15487 matrix = XWINDOW (selected_window)->current_matrix;
15488 vpos = XINT (row);
15489 if (vpos >= 0 && vpos < matrix->nrows)
15490 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15491 vpos,
15492 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15493 return Qnil;
15494 }
15495
15496
15497 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15498 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15499 GLYPH 0 means don't dump glyphs.
15500 GLYPH 1 means dump glyphs in short form.
15501 GLYPH > 1 or omitted means dump glyphs in long form. */)
15502 (row, glyphs)
15503 Lisp_Object row, glyphs;
15504 {
15505 struct frame *sf = SELECTED_FRAME ();
15506 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15507 int vpos;
15508
15509 CHECK_NUMBER (row);
15510 vpos = XINT (row);
15511 if (vpos >= 0 && vpos < m->nrows)
15512 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15513 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15514 return Qnil;
15515 }
15516
15517
15518 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15519 doc: /* Toggle tracing of redisplay.
15520 With ARG, turn tracing on if and only if ARG is positive. */)
15521 (arg)
15522 Lisp_Object arg;
15523 {
15524 if (NILP (arg))
15525 trace_redisplay_p = !trace_redisplay_p;
15526 else
15527 {
15528 arg = Fprefix_numeric_value (arg);
15529 trace_redisplay_p = XINT (arg) > 0;
15530 }
15531
15532 return Qnil;
15533 }
15534
15535
15536 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15537 doc: /* Like `format', but print result to stderr.
15538 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15539 (nargs, args)
15540 int nargs;
15541 Lisp_Object *args;
15542 {
15543 Lisp_Object s = Fformat (nargs, args);
15544 fprintf (stderr, "%s", SDATA (s));
15545 return Qnil;
15546 }
15547
15548 #endif /* GLYPH_DEBUG */
15549
15550
15551 \f
15552 /***********************************************************************
15553 Building Desired Matrix Rows
15554 ***********************************************************************/
15555
15556 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15557 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15558
15559 static struct glyph_row *
15560 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15561 struct window *w;
15562 Lisp_Object overlay_arrow_string;
15563 {
15564 struct frame *f = XFRAME (WINDOW_FRAME (w));
15565 struct buffer *buffer = XBUFFER (w->buffer);
15566 struct buffer *old = current_buffer;
15567 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15568 int arrow_len = SCHARS (overlay_arrow_string);
15569 const unsigned char *arrow_end = arrow_string + arrow_len;
15570 const unsigned char *p;
15571 struct it it;
15572 int multibyte_p;
15573 int n_glyphs_before;
15574
15575 set_buffer_temp (buffer);
15576 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15577 it.glyph_row->used[TEXT_AREA] = 0;
15578 SET_TEXT_POS (it.position, 0, 0);
15579
15580 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15581 p = arrow_string;
15582 while (p < arrow_end)
15583 {
15584 Lisp_Object face, ilisp;
15585
15586 /* Get the next character. */
15587 if (multibyte_p)
15588 it.c = string_char_and_length (p, arrow_len, &it.len);
15589 else
15590 it.c = *p, it.len = 1;
15591 p += it.len;
15592
15593 /* Get its face. */
15594 ilisp = make_number (p - arrow_string);
15595 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15596 it.face_id = compute_char_face (f, it.c, face);
15597
15598 /* Compute its width, get its glyphs. */
15599 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15600 SET_TEXT_POS (it.position, -1, -1);
15601 PRODUCE_GLYPHS (&it);
15602
15603 /* If this character doesn't fit any more in the line, we have
15604 to remove some glyphs. */
15605 if (it.current_x > it.last_visible_x)
15606 {
15607 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15608 break;
15609 }
15610 }
15611
15612 set_buffer_temp (old);
15613 return it.glyph_row;
15614 }
15615
15616
15617 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15618 glyphs are only inserted for terminal frames since we can't really
15619 win with truncation glyphs when partially visible glyphs are
15620 involved. Which glyphs to insert is determined by
15621 produce_special_glyphs. */
15622
15623 static void
15624 insert_left_trunc_glyphs (it)
15625 struct it *it;
15626 {
15627 struct it truncate_it;
15628 struct glyph *from, *end, *to, *toend;
15629
15630 xassert (!FRAME_WINDOW_P (it->f));
15631
15632 /* Get the truncation glyphs. */
15633 truncate_it = *it;
15634 truncate_it.current_x = 0;
15635 truncate_it.face_id = DEFAULT_FACE_ID;
15636 truncate_it.glyph_row = &scratch_glyph_row;
15637 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15638 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15639 truncate_it.object = make_number (0);
15640 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15641
15642 /* Overwrite glyphs from IT with truncation glyphs. */
15643 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15644 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15645 to = it->glyph_row->glyphs[TEXT_AREA];
15646 toend = to + it->glyph_row->used[TEXT_AREA];
15647
15648 while (from < end)
15649 *to++ = *from++;
15650
15651 /* There may be padding glyphs left over. Overwrite them too. */
15652 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15653 {
15654 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15655 while (from < end)
15656 *to++ = *from++;
15657 }
15658
15659 if (to > toend)
15660 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15661 }
15662
15663
15664 /* Compute the pixel height and width of IT->glyph_row.
15665
15666 Most of the time, ascent and height of a display line will be equal
15667 to the max_ascent and max_height values of the display iterator
15668 structure. This is not the case if
15669
15670 1. We hit ZV without displaying anything. In this case, max_ascent
15671 and max_height will be zero.
15672
15673 2. We have some glyphs that don't contribute to the line height.
15674 (The glyph row flag contributes_to_line_height_p is for future
15675 pixmap extensions).
15676
15677 The first case is easily covered by using default values because in
15678 these cases, the line height does not really matter, except that it
15679 must not be zero. */
15680
15681 static void
15682 compute_line_metrics (it)
15683 struct it *it;
15684 {
15685 struct glyph_row *row = it->glyph_row;
15686 int area, i;
15687
15688 if (FRAME_WINDOW_P (it->f))
15689 {
15690 int i, min_y, max_y;
15691
15692 /* The line may consist of one space only, that was added to
15693 place the cursor on it. If so, the row's height hasn't been
15694 computed yet. */
15695 if (row->height == 0)
15696 {
15697 if (it->max_ascent + it->max_descent == 0)
15698 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15699 row->ascent = it->max_ascent;
15700 row->height = it->max_ascent + it->max_descent;
15701 row->phys_ascent = it->max_phys_ascent;
15702 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15703 row->extra_line_spacing = it->max_extra_line_spacing;
15704 }
15705
15706 /* Compute the width of this line. */
15707 row->pixel_width = row->x;
15708 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15709 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15710
15711 xassert (row->pixel_width >= 0);
15712 xassert (row->ascent >= 0 && row->height > 0);
15713
15714 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15715 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15716
15717 /* If first line's physical ascent is larger than its logical
15718 ascent, use the physical ascent, and make the row taller.
15719 This makes accented characters fully visible. */
15720 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15721 && row->phys_ascent > row->ascent)
15722 {
15723 row->height += row->phys_ascent - row->ascent;
15724 row->ascent = row->phys_ascent;
15725 }
15726
15727 /* Compute how much of the line is visible. */
15728 row->visible_height = row->height;
15729
15730 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15731 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15732
15733 if (row->y < min_y)
15734 row->visible_height -= min_y - row->y;
15735 if (row->y + row->height > max_y)
15736 row->visible_height -= row->y + row->height - max_y;
15737 }
15738 else
15739 {
15740 row->pixel_width = row->used[TEXT_AREA];
15741 if (row->continued_p)
15742 row->pixel_width -= it->continuation_pixel_width;
15743 else if (row->truncated_on_right_p)
15744 row->pixel_width -= it->truncation_pixel_width;
15745 row->ascent = row->phys_ascent = 0;
15746 row->height = row->phys_height = row->visible_height = 1;
15747 row->extra_line_spacing = 0;
15748 }
15749
15750 /* Compute a hash code for this row. */
15751 row->hash = 0;
15752 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15753 for (i = 0; i < row->used[area]; ++i)
15754 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15755 + row->glyphs[area][i].u.val
15756 + row->glyphs[area][i].face_id
15757 + row->glyphs[area][i].padding_p
15758 + (row->glyphs[area][i].type << 2));
15759
15760 it->max_ascent = it->max_descent = 0;
15761 it->max_phys_ascent = it->max_phys_descent = 0;
15762 }
15763
15764
15765 /* Append one space to the glyph row of iterator IT if doing a
15766 window-based redisplay. The space has the same face as
15767 IT->face_id. Value is non-zero if a space was added.
15768
15769 This function is called to make sure that there is always one glyph
15770 at the end of a glyph row that the cursor can be set on under
15771 window-systems. (If there weren't such a glyph we would not know
15772 how wide and tall a box cursor should be displayed).
15773
15774 At the same time this space let's a nicely handle clearing to the
15775 end of the line if the row ends in italic text. */
15776
15777 static int
15778 append_space_for_newline (it, default_face_p)
15779 struct it *it;
15780 int default_face_p;
15781 {
15782 if (FRAME_WINDOW_P (it->f))
15783 {
15784 int n = it->glyph_row->used[TEXT_AREA];
15785
15786 if (it->glyph_row->glyphs[TEXT_AREA] + n
15787 < it->glyph_row->glyphs[1 + TEXT_AREA])
15788 {
15789 /* Save some values that must not be changed.
15790 Must save IT->c and IT->len because otherwise
15791 ITERATOR_AT_END_P wouldn't work anymore after
15792 append_space_for_newline has been called. */
15793 enum display_element_type saved_what = it->what;
15794 int saved_c = it->c, saved_len = it->len;
15795 int saved_x = it->current_x;
15796 int saved_face_id = it->face_id;
15797 struct text_pos saved_pos;
15798 Lisp_Object saved_object;
15799 struct face *face;
15800
15801 saved_object = it->object;
15802 saved_pos = it->position;
15803
15804 it->what = IT_CHARACTER;
15805 bzero (&it->position, sizeof it->position);
15806 it->object = make_number (0);
15807 it->c = ' ';
15808 it->len = 1;
15809
15810 if (default_face_p)
15811 it->face_id = DEFAULT_FACE_ID;
15812 else if (it->face_before_selective_p)
15813 it->face_id = it->saved_face_id;
15814 face = FACE_FROM_ID (it->f, it->face_id);
15815 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
15816
15817 PRODUCE_GLYPHS (it);
15818
15819 it->override_ascent = -1;
15820 it->constrain_row_ascent_descent_p = 0;
15821 it->current_x = saved_x;
15822 it->object = saved_object;
15823 it->position = saved_pos;
15824 it->what = saved_what;
15825 it->face_id = saved_face_id;
15826 it->len = saved_len;
15827 it->c = saved_c;
15828 return 1;
15829 }
15830 }
15831
15832 return 0;
15833 }
15834
15835
15836 /* Extend the face of the last glyph in the text area of IT->glyph_row
15837 to the end of the display line. Called from display_line.
15838 If the glyph row is empty, add a space glyph to it so that we
15839 know the face to draw. Set the glyph row flag fill_line_p. */
15840
15841 static void
15842 extend_face_to_end_of_line (it)
15843 struct it *it;
15844 {
15845 struct face *face;
15846 struct frame *f = it->f;
15847
15848 /* If line is already filled, do nothing. */
15849 if (it->current_x >= it->last_visible_x)
15850 return;
15851
15852 /* Face extension extends the background and box of IT->face_id
15853 to the end of the line. If the background equals the background
15854 of the frame, we don't have to do anything. */
15855 if (it->face_before_selective_p)
15856 face = FACE_FROM_ID (it->f, it->saved_face_id);
15857 else
15858 face = FACE_FROM_ID (f, it->face_id);
15859
15860 if (FRAME_WINDOW_P (f)
15861 && it->glyph_row->displays_text_p
15862 && face->box == FACE_NO_BOX
15863 && face->background == FRAME_BACKGROUND_PIXEL (f)
15864 && !face->stipple)
15865 return;
15866
15867 /* Set the glyph row flag indicating that the face of the last glyph
15868 in the text area has to be drawn to the end of the text area. */
15869 it->glyph_row->fill_line_p = 1;
15870
15871 /* If current character of IT is not ASCII, make sure we have the
15872 ASCII face. This will be automatically undone the next time
15873 get_next_display_element returns a multibyte character. Note
15874 that the character will always be single byte in unibyte text. */
15875 if (!ASCII_CHAR_P (it->c))
15876 {
15877 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
15878 }
15879
15880 if (FRAME_WINDOW_P (f))
15881 {
15882 /* If the row is empty, add a space with the current face of IT,
15883 so that we know which face to draw. */
15884 if (it->glyph_row->used[TEXT_AREA] == 0)
15885 {
15886 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15887 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15888 it->glyph_row->used[TEXT_AREA] = 1;
15889 }
15890 }
15891 else
15892 {
15893 /* Save some values that must not be changed. */
15894 int saved_x = it->current_x;
15895 struct text_pos saved_pos;
15896 Lisp_Object saved_object;
15897 enum display_element_type saved_what = it->what;
15898 int saved_face_id = it->face_id;
15899
15900 saved_object = it->object;
15901 saved_pos = it->position;
15902
15903 it->what = IT_CHARACTER;
15904 bzero (&it->position, sizeof it->position);
15905 it->object = make_number (0);
15906 it->c = ' ';
15907 it->len = 1;
15908 it->face_id = face->id;
15909
15910 PRODUCE_GLYPHS (it);
15911
15912 while (it->current_x <= it->last_visible_x)
15913 PRODUCE_GLYPHS (it);
15914
15915 /* Don't count these blanks really. It would let us insert a left
15916 truncation glyph below and make us set the cursor on them, maybe. */
15917 it->current_x = saved_x;
15918 it->object = saved_object;
15919 it->position = saved_pos;
15920 it->what = saved_what;
15921 it->face_id = saved_face_id;
15922 }
15923 }
15924
15925
15926 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15927 trailing whitespace. */
15928
15929 static int
15930 trailing_whitespace_p (charpos)
15931 int charpos;
15932 {
15933 int bytepos = CHAR_TO_BYTE (charpos);
15934 int c = 0;
15935
15936 while (bytepos < ZV_BYTE
15937 && (c = FETCH_CHAR (bytepos),
15938 c == ' ' || c == '\t'))
15939 ++bytepos;
15940
15941 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15942 {
15943 if (bytepos != PT_BYTE)
15944 return 1;
15945 }
15946 return 0;
15947 }
15948
15949
15950 /* Highlight trailing whitespace, if any, in ROW. */
15951
15952 void
15953 highlight_trailing_whitespace (f, row)
15954 struct frame *f;
15955 struct glyph_row *row;
15956 {
15957 int used = row->used[TEXT_AREA];
15958
15959 if (used)
15960 {
15961 struct glyph *start = row->glyphs[TEXT_AREA];
15962 struct glyph *glyph = start + used - 1;
15963
15964 /* Skip over glyphs inserted to display the cursor at the
15965 end of a line, for extending the face of the last glyph
15966 to the end of the line on terminals, and for truncation
15967 and continuation glyphs. */
15968 while (glyph >= start
15969 && glyph->type == CHAR_GLYPH
15970 && INTEGERP (glyph->object))
15971 --glyph;
15972
15973 /* If last glyph is a space or stretch, and it's trailing
15974 whitespace, set the face of all trailing whitespace glyphs in
15975 IT->glyph_row to `trailing-whitespace'. */
15976 if (glyph >= start
15977 && BUFFERP (glyph->object)
15978 && (glyph->type == STRETCH_GLYPH
15979 || (glyph->type == CHAR_GLYPH
15980 && glyph->u.ch == ' '))
15981 && trailing_whitespace_p (glyph->charpos))
15982 {
15983 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
15984 if (face_id < 0)
15985 return;
15986
15987 while (glyph >= start
15988 && BUFFERP (glyph->object)
15989 && (glyph->type == STRETCH_GLYPH
15990 || (glyph->type == CHAR_GLYPH
15991 && glyph->u.ch == ' ')))
15992 (glyph--)->face_id = face_id;
15993 }
15994 }
15995 }
15996
15997
15998 /* Value is non-zero if glyph row ROW in window W should be
15999 used to hold the cursor. */
16000
16001 static int
16002 cursor_row_p (w, row)
16003 struct window *w;
16004 struct glyph_row *row;
16005 {
16006 int cursor_row_p = 1;
16007
16008 if (PT == MATRIX_ROW_END_CHARPOS (row))
16009 {
16010 /* Suppose the row ends on a string.
16011 Unless the row is continued, that means it ends on a newline
16012 in the string. If it's anything other than a display string
16013 (e.g. a before-string from an overlay), we don't want the
16014 cursor there. (This heuristic seems to give the optimal
16015 behavior for the various types of multi-line strings.) */
16016 if (CHARPOS (row->end.string_pos) >= 0)
16017 {
16018 if (row->continued_p)
16019 cursor_row_p = 1;
16020 else
16021 {
16022 /* Check for `display' property. */
16023 struct glyph *beg = row->glyphs[TEXT_AREA];
16024 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16025 struct glyph *glyph;
16026
16027 cursor_row_p = 0;
16028 for (glyph = end; glyph >= beg; --glyph)
16029 if (STRINGP (glyph->object))
16030 {
16031 Lisp_Object prop
16032 = Fget_char_property (make_number (PT),
16033 Qdisplay, Qnil);
16034 cursor_row_p =
16035 (!NILP (prop)
16036 && display_prop_string_p (prop, glyph->object));
16037 break;
16038 }
16039 }
16040 }
16041 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16042 {
16043 /* If the row ends in middle of a real character,
16044 and the line is continued, we want the cursor here.
16045 That's because MATRIX_ROW_END_CHARPOS would equal
16046 PT if PT is before the character. */
16047 if (!row->ends_in_ellipsis_p)
16048 cursor_row_p = row->continued_p;
16049 else
16050 /* If the row ends in an ellipsis, then
16051 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16052 We want that position to be displayed after the ellipsis. */
16053 cursor_row_p = 0;
16054 }
16055 /* If the row ends at ZV, display the cursor at the end of that
16056 row instead of at the start of the row below. */
16057 else if (row->ends_at_zv_p)
16058 cursor_row_p = 1;
16059 else
16060 cursor_row_p = 0;
16061 }
16062
16063 return cursor_row_p;
16064 }
16065
16066
16067 /* Construct the glyph row IT->glyph_row in the desired matrix of
16068 IT->w from text at the current position of IT. See dispextern.h
16069 for an overview of struct it. Value is non-zero if
16070 IT->glyph_row displays text, as opposed to a line displaying ZV
16071 only. */
16072
16073 static int
16074 display_line (it)
16075 struct it *it;
16076 {
16077 struct glyph_row *row = it->glyph_row;
16078 Lisp_Object overlay_arrow_string;
16079
16080 /* We always start displaying at hpos zero even if hscrolled. */
16081 xassert (it->hpos == 0 && it->current_x == 0);
16082
16083 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16084 >= it->w->desired_matrix->nrows)
16085 {
16086 it->w->nrows_scale_factor++;
16087 fonts_changed_p = 1;
16088 return 0;
16089 }
16090
16091 /* Is IT->w showing the region? */
16092 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16093
16094 /* Clear the result glyph row and enable it. */
16095 prepare_desired_row (row);
16096
16097 row->y = it->current_y;
16098 row->start = it->start;
16099 row->continuation_lines_width = it->continuation_lines_width;
16100 row->displays_text_p = 1;
16101 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16102 it->starts_in_middle_of_char_p = 0;
16103
16104 /* Arrange the overlays nicely for our purposes. Usually, we call
16105 display_line on only one line at a time, in which case this
16106 can't really hurt too much, or we call it on lines which appear
16107 one after another in the buffer, in which case all calls to
16108 recenter_overlay_lists but the first will be pretty cheap. */
16109 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16110
16111 /* Move over display elements that are not visible because we are
16112 hscrolled. This may stop at an x-position < IT->first_visible_x
16113 if the first glyph is partially visible or if we hit a line end. */
16114 if (it->current_x < it->first_visible_x)
16115 {
16116 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16117 MOVE_TO_POS | MOVE_TO_X);
16118 }
16119
16120 /* Get the initial row height. This is either the height of the
16121 text hscrolled, if there is any, or zero. */
16122 row->ascent = it->max_ascent;
16123 row->height = it->max_ascent + it->max_descent;
16124 row->phys_ascent = it->max_phys_ascent;
16125 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16126 row->extra_line_spacing = it->max_extra_line_spacing;
16127
16128 /* Loop generating characters. The loop is left with IT on the next
16129 character to display. */
16130 while (1)
16131 {
16132 int n_glyphs_before, hpos_before, x_before;
16133 int x, i, nglyphs;
16134 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16135
16136 /* Retrieve the next thing to display. Value is zero if end of
16137 buffer reached. */
16138 if (!get_next_display_element (it))
16139 {
16140 /* Maybe add a space at the end of this line that is used to
16141 display the cursor there under X. Set the charpos of the
16142 first glyph of blank lines not corresponding to any text
16143 to -1. */
16144 #ifdef HAVE_WINDOW_SYSTEM
16145 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16146 row->exact_window_width_line_p = 1;
16147 else
16148 #endif /* HAVE_WINDOW_SYSTEM */
16149 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16150 || row->used[TEXT_AREA] == 0)
16151 {
16152 row->glyphs[TEXT_AREA]->charpos = -1;
16153 row->displays_text_p = 0;
16154
16155 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16156 && (!MINI_WINDOW_P (it->w)
16157 || (minibuf_level && EQ (it->window, minibuf_window))))
16158 row->indicate_empty_line_p = 1;
16159 }
16160
16161 it->continuation_lines_width = 0;
16162 row->ends_at_zv_p = 1;
16163 break;
16164 }
16165
16166 /* Now, get the metrics of what we want to display. This also
16167 generates glyphs in `row' (which is IT->glyph_row). */
16168 n_glyphs_before = row->used[TEXT_AREA];
16169 x = it->current_x;
16170
16171 /* Remember the line height so far in case the next element doesn't
16172 fit on the line. */
16173 if (!it->truncate_lines_p)
16174 {
16175 ascent = it->max_ascent;
16176 descent = it->max_descent;
16177 phys_ascent = it->max_phys_ascent;
16178 phys_descent = it->max_phys_descent;
16179 }
16180
16181 PRODUCE_GLYPHS (it);
16182
16183 /* If this display element was in marginal areas, continue with
16184 the next one. */
16185 if (it->area != TEXT_AREA)
16186 {
16187 row->ascent = max (row->ascent, it->max_ascent);
16188 row->height = max (row->height, it->max_ascent + it->max_descent);
16189 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16190 row->phys_height = max (row->phys_height,
16191 it->max_phys_ascent + it->max_phys_descent);
16192 row->extra_line_spacing = max (row->extra_line_spacing,
16193 it->max_extra_line_spacing);
16194 set_iterator_to_next (it, 1);
16195 continue;
16196 }
16197
16198 /* Does the display element fit on the line? If we truncate
16199 lines, we should draw past the right edge of the window. If
16200 we don't truncate, we want to stop so that we can display the
16201 continuation glyph before the right margin. If lines are
16202 continued, there are two possible strategies for characters
16203 resulting in more than 1 glyph (e.g. tabs): Display as many
16204 glyphs as possible in this line and leave the rest for the
16205 continuation line, or display the whole element in the next
16206 line. Original redisplay did the former, so we do it also. */
16207 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16208 hpos_before = it->hpos;
16209 x_before = x;
16210
16211 if (/* Not a newline. */
16212 nglyphs > 0
16213 /* Glyphs produced fit entirely in the line. */
16214 && it->current_x < it->last_visible_x)
16215 {
16216 it->hpos += nglyphs;
16217 row->ascent = max (row->ascent, it->max_ascent);
16218 row->height = max (row->height, it->max_ascent + it->max_descent);
16219 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16220 row->phys_height = max (row->phys_height,
16221 it->max_phys_ascent + it->max_phys_descent);
16222 row->extra_line_spacing = max (row->extra_line_spacing,
16223 it->max_extra_line_spacing);
16224 if (it->current_x - it->pixel_width < it->first_visible_x)
16225 row->x = x - it->first_visible_x;
16226 }
16227 else
16228 {
16229 int new_x;
16230 struct glyph *glyph;
16231
16232 for (i = 0; i < nglyphs; ++i, x = new_x)
16233 {
16234 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16235 new_x = x + glyph->pixel_width;
16236
16237 if (/* Lines are continued. */
16238 !it->truncate_lines_p
16239 && (/* Glyph doesn't fit on the line. */
16240 new_x > it->last_visible_x
16241 /* Or it fits exactly on a window system frame. */
16242 || (new_x == it->last_visible_x
16243 && FRAME_WINDOW_P (it->f))))
16244 {
16245 /* End of a continued line. */
16246
16247 if (it->hpos == 0
16248 || (new_x == it->last_visible_x
16249 && FRAME_WINDOW_P (it->f)))
16250 {
16251 /* Current glyph is the only one on the line or
16252 fits exactly on the line. We must continue
16253 the line because we can't draw the cursor
16254 after the glyph. */
16255 row->continued_p = 1;
16256 it->current_x = new_x;
16257 it->continuation_lines_width += new_x;
16258 ++it->hpos;
16259 if (i == nglyphs - 1)
16260 {
16261 set_iterator_to_next (it, 1);
16262 #ifdef HAVE_WINDOW_SYSTEM
16263 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16264 {
16265 if (!get_next_display_element (it))
16266 {
16267 row->exact_window_width_line_p = 1;
16268 it->continuation_lines_width = 0;
16269 row->continued_p = 0;
16270 row->ends_at_zv_p = 1;
16271 }
16272 else if (ITERATOR_AT_END_OF_LINE_P (it))
16273 {
16274 row->continued_p = 0;
16275 row->exact_window_width_line_p = 1;
16276 }
16277 }
16278 #endif /* HAVE_WINDOW_SYSTEM */
16279 }
16280 }
16281 else if (CHAR_GLYPH_PADDING_P (*glyph)
16282 && !FRAME_WINDOW_P (it->f))
16283 {
16284 /* A padding glyph that doesn't fit on this line.
16285 This means the whole character doesn't fit
16286 on the line. */
16287 row->used[TEXT_AREA] = n_glyphs_before;
16288
16289 /* Fill the rest of the row with continuation
16290 glyphs like in 20.x. */
16291 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16292 < row->glyphs[1 + TEXT_AREA])
16293 produce_special_glyphs (it, IT_CONTINUATION);
16294
16295 row->continued_p = 1;
16296 it->current_x = x_before;
16297 it->continuation_lines_width += x_before;
16298
16299 /* Restore the height to what it was before the
16300 element not fitting on the line. */
16301 it->max_ascent = ascent;
16302 it->max_descent = descent;
16303 it->max_phys_ascent = phys_ascent;
16304 it->max_phys_descent = phys_descent;
16305 }
16306 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16307 {
16308 /* A TAB that extends past the right edge of the
16309 window. This produces a single glyph on
16310 window system frames. We leave the glyph in
16311 this row and let it fill the row, but don't
16312 consume the TAB. */
16313 it->continuation_lines_width += it->last_visible_x;
16314 row->ends_in_middle_of_char_p = 1;
16315 row->continued_p = 1;
16316 glyph->pixel_width = it->last_visible_x - x;
16317 it->starts_in_middle_of_char_p = 1;
16318 }
16319 else
16320 {
16321 /* Something other than a TAB that draws past
16322 the right edge of the window. Restore
16323 positions to values before the element. */
16324 row->used[TEXT_AREA] = n_glyphs_before + i;
16325
16326 /* Display continuation glyphs. */
16327 if (!FRAME_WINDOW_P (it->f))
16328 produce_special_glyphs (it, IT_CONTINUATION);
16329 row->continued_p = 1;
16330
16331 it->current_x = x_before;
16332 it->continuation_lines_width += x;
16333 extend_face_to_end_of_line (it);
16334
16335 if (nglyphs > 1 && i > 0)
16336 {
16337 row->ends_in_middle_of_char_p = 1;
16338 it->starts_in_middle_of_char_p = 1;
16339 }
16340
16341 /* Restore the height to what it was before the
16342 element not fitting on the line. */
16343 it->max_ascent = ascent;
16344 it->max_descent = descent;
16345 it->max_phys_ascent = phys_ascent;
16346 it->max_phys_descent = phys_descent;
16347 }
16348
16349 break;
16350 }
16351 else if (new_x > it->first_visible_x)
16352 {
16353 /* Increment number of glyphs actually displayed. */
16354 ++it->hpos;
16355
16356 if (x < it->first_visible_x)
16357 /* Glyph is partially visible, i.e. row starts at
16358 negative X position. */
16359 row->x = x - it->first_visible_x;
16360 }
16361 else
16362 {
16363 /* Glyph is completely off the left margin of the
16364 window. This should not happen because of the
16365 move_it_in_display_line at the start of this
16366 function, unless the text display area of the
16367 window is empty. */
16368 xassert (it->first_visible_x <= it->last_visible_x);
16369 }
16370 }
16371
16372 row->ascent = max (row->ascent, it->max_ascent);
16373 row->height = max (row->height, it->max_ascent + it->max_descent);
16374 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16375 row->phys_height = max (row->phys_height,
16376 it->max_phys_ascent + it->max_phys_descent);
16377 row->extra_line_spacing = max (row->extra_line_spacing,
16378 it->max_extra_line_spacing);
16379
16380 /* End of this display line if row is continued. */
16381 if (row->continued_p || row->ends_at_zv_p)
16382 break;
16383 }
16384
16385 at_end_of_line:
16386 /* Is this a line end? If yes, we're also done, after making
16387 sure that a non-default face is extended up to the right
16388 margin of the window. */
16389 if (ITERATOR_AT_END_OF_LINE_P (it))
16390 {
16391 int used_before = row->used[TEXT_AREA];
16392
16393 row->ends_in_newline_from_string_p = STRINGP (it->object);
16394
16395 #ifdef HAVE_WINDOW_SYSTEM
16396 /* Add a space at the end of the line that is used to
16397 display the cursor there. */
16398 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16399 append_space_for_newline (it, 0);
16400 #endif /* HAVE_WINDOW_SYSTEM */
16401
16402 /* Extend the face to the end of the line. */
16403 extend_face_to_end_of_line (it);
16404
16405 /* Make sure we have the position. */
16406 if (used_before == 0)
16407 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16408
16409 /* Consume the line end. This skips over invisible lines. */
16410 set_iterator_to_next (it, 1);
16411 it->continuation_lines_width = 0;
16412 break;
16413 }
16414
16415 /* Proceed with next display element. Note that this skips
16416 over lines invisible because of selective display. */
16417 set_iterator_to_next (it, 1);
16418
16419 /* If we truncate lines, we are done when the last displayed
16420 glyphs reach past the right margin of the window. */
16421 if (it->truncate_lines_p
16422 && (FRAME_WINDOW_P (it->f)
16423 ? (it->current_x >= it->last_visible_x)
16424 : (it->current_x > it->last_visible_x)))
16425 {
16426 /* Maybe add truncation glyphs. */
16427 if (!FRAME_WINDOW_P (it->f))
16428 {
16429 int i, n;
16430
16431 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16432 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16433 break;
16434
16435 for (n = row->used[TEXT_AREA]; i < n; ++i)
16436 {
16437 row->used[TEXT_AREA] = i;
16438 produce_special_glyphs (it, IT_TRUNCATION);
16439 }
16440 }
16441 #ifdef HAVE_WINDOW_SYSTEM
16442 else
16443 {
16444 /* Don't truncate if we can overflow newline into fringe. */
16445 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16446 {
16447 if (!get_next_display_element (it))
16448 {
16449 it->continuation_lines_width = 0;
16450 row->ends_at_zv_p = 1;
16451 row->exact_window_width_line_p = 1;
16452 break;
16453 }
16454 if (ITERATOR_AT_END_OF_LINE_P (it))
16455 {
16456 row->exact_window_width_line_p = 1;
16457 goto at_end_of_line;
16458 }
16459 }
16460 }
16461 #endif /* HAVE_WINDOW_SYSTEM */
16462
16463 row->truncated_on_right_p = 1;
16464 it->continuation_lines_width = 0;
16465 reseat_at_next_visible_line_start (it, 0);
16466 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16467 it->hpos = hpos_before;
16468 it->current_x = x_before;
16469 break;
16470 }
16471 }
16472
16473 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16474 at the left window margin. */
16475 if (it->first_visible_x
16476 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16477 {
16478 if (!FRAME_WINDOW_P (it->f))
16479 insert_left_trunc_glyphs (it);
16480 row->truncated_on_left_p = 1;
16481 }
16482
16483 /* If the start of this line is the overlay arrow-position, then
16484 mark this glyph row as the one containing the overlay arrow.
16485 This is clearly a mess with variable size fonts. It would be
16486 better to let it be displayed like cursors under X. */
16487 if ((row->displays_text_p || !overlay_arrow_seen)
16488 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16489 !NILP (overlay_arrow_string)))
16490 {
16491 /* Overlay arrow in window redisplay is a fringe bitmap. */
16492 if (STRINGP (overlay_arrow_string))
16493 {
16494 struct glyph_row *arrow_row
16495 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16496 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16497 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16498 struct glyph *p = row->glyphs[TEXT_AREA];
16499 struct glyph *p2, *end;
16500
16501 /* Copy the arrow glyphs. */
16502 while (glyph < arrow_end)
16503 *p++ = *glyph++;
16504
16505 /* Throw away padding glyphs. */
16506 p2 = p;
16507 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16508 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16509 ++p2;
16510 if (p2 > p)
16511 {
16512 while (p2 < end)
16513 *p++ = *p2++;
16514 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16515 }
16516 }
16517 else
16518 {
16519 xassert (INTEGERP (overlay_arrow_string));
16520 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16521 }
16522 overlay_arrow_seen = 1;
16523 }
16524
16525 /* Compute pixel dimensions of this line. */
16526 compute_line_metrics (it);
16527
16528 /* Remember the position at which this line ends. */
16529 row->end = it->current;
16530
16531 /* Record whether this row ends inside an ellipsis. */
16532 row->ends_in_ellipsis_p
16533 = (it->method == GET_FROM_DISPLAY_VECTOR
16534 && it->ellipsis_p);
16535
16536 /* Save fringe bitmaps in this row. */
16537 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16538 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16539 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16540 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16541
16542 it->left_user_fringe_bitmap = 0;
16543 it->left_user_fringe_face_id = 0;
16544 it->right_user_fringe_bitmap = 0;
16545 it->right_user_fringe_face_id = 0;
16546
16547 /* Maybe set the cursor. */
16548 if (it->w->cursor.vpos < 0
16549 && PT >= MATRIX_ROW_START_CHARPOS (row)
16550 && PT <= MATRIX_ROW_END_CHARPOS (row)
16551 && cursor_row_p (it->w, row))
16552 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16553
16554 /* Highlight trailing whitespace. */
16555 if (!NILP (Vshow_trailing_whitespace))
16556 highlight_trailing_whitespace (it->f, it->glyph_row);
16557
16558 /* Prepare for the next line. This line starts horizontally at (X
16559 HPOS) = (0 0). Vertical positions are incremented. As a
16560 convenience for the caller, IT->glyph_row is set to the next
16561 row to be used. */
16562 it->current_x = it->hpos = 0;
16563 it->current_y += row->height;
16564 ++it->vpos;
16565 ++it->glyph_row;
16566 it->start = it->current;
16567 return row->displays_text_p;
16568 }
16569
16570
16571 \f
16572 /***********************************************************************
16573 Menu Bar
16574 ***********************************************************************/
16575
16576 /* Redisplay the menu bar in the frame for window W.
16577
16578 The menu bar of X frames that don't have X toolkit support is
16579 displayed in a special window W->frame->menu_bar_window.
16580
16581 The menu bar of terminal frames is treated specially as far as
16582 glyph matrices are concerned. Menu bar lines are not part of
16583 windows, so the update is done directly on the frame matrix rows
16584 for the menu bar. */
16585
16586 static void
16587 display_menu_bar (w)
16588 struct window *w;
16589 {
16590 struct frame *f = XFRAME (WINDOW_FRAME (w));
16591 struct it it;
16592 Lisp_Object items;
16593 int i;
16594
16595 /* Don't do all this for graphical frames. */
16596 #ifdef HAVE_NTGUI
16597 if (!NILP (Vwindow_system))
16598 return;
16599 #endif
16600 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16601 if (FRAME_X_P (f))
16602 return;
16603 #endif
16604 #ifdef MAC_OS
16605 if (FRAME_MAC_P (f))
16606 return;
16607 #endif
16608
16609 #ifdef USE_X_TOOLKIT
16610 xassert (!FRAME_WINDOW_P (f));
16611 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16612 it.first_visible_x = 0;
16613 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16614 #else /* not USE_X_TOOLKIT */
16615 if (FRAME_WINDOW_P (f))
16616 {
16617 /* Menu bar lines are displayed in the desired matrix of the
16618 dummy window menu_bar_window. */
16619 struct window *menu_w;
16620 xassert (WINDOWP (f->menu_bar_window));
16621 menu_w = XWINDOW (f->menu_bar_window);
16622 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16623 MENU_FACE_ID);
16624 it.first_visible_x = 0;
16625 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16626 }
16627 else
16628 {
16629 /* This is a TTY frame, i.e. character hpos/vpos are used as
16630 pixel x/y. */
16631 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16632 MENU_FACE_ID);
16633 it.first_visible_x = 0;
16634 it.last_visible_x = FRAME_COLS (f);
16635 }
16636 #endif /* not USE_X_TOOLKIT */
16637
16638 if (! mode_line_inverse_video)
16639 /* Force the menu-bar to be displayed in the default face. */
16640 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16641
16642 /* Clear all rows of the menu bar. */
16643 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16644 {
16645 struct glyph_row *row = it.glyph_row + i;
16646 clear_glyph_row (row);
16647 row->enabled_p = 1;
16648 row->full_width_p = 1;
16649 }
16650
16651 /* Display all items of the menu bar. */
16652 items = FRAME_MENU_BAR_ITEMS (it.f);
16653 for (i = 0; i < XVECTOR (items)->size; i += 4)
16654 {
16655 Lisp_Object string;
16656
16657 /* Stop at nil string. */
16658 string = AREF (items, i + 1);
16659 if (NILP (string))
16660 break;
16661
16662 /* Remember where item was displayed. */
16663 AREF (items, i + 3) = make_number (it.hpos);
16664
16665 /* Display the item, pad with one space. */
16666 if (it.current_x < it.last_visible_x)
16667 display_string (NULL, string, Qnil, 0, 0, &it,
16668 SCHARS (string) + 1, 0, 0, -1);
16669 }
16670
16671 /* Fill out the line with spaces. */
16672 if (it.current_x < it.last_visible_x)
16673 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16674
16675 /* Compute the total height of the lines. */
16676 compute_line_metrics (&it);
16677 }
16678
16679
16680 \f
16681 /***********************************************************************
16682 Mode Line
16683 ***********************************************************************/
16684
16685 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16686 FORCE is non-zero, redisplay mode lines unconditionally.
16687 Otherwise, redisplay only mode lines that are garbaged. Value is
16688 the number of windows whose mode lines were redisplayed. */
16689
16690 static int
16691 redisplay_mode_lines (window, force)
16692 Lisp_Object window;
16693 int force;
16694 {
16695 int nwindows = 0;
16696
16697 while (!NILP (window))
16698 {
16699 struct window *w = XWINDOW (window);
16700
16701 if (WINDOWP (w->hchild))
16702 nwindows += redisplay_mode_lines (w->hchild, force);
16703 else if (WINDOWP (w->vchild))
16704 nwindows += redisplay_mode_lines (w->vchild, force);
16705 else if (force
16706 || FRAME_GARBAGED_P (XFRAME (w->frame))
16707 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16708 {
16709 struct text_pos lpoint;
16710 struct buffer *old = current_buffer;
16711
16712 /* Set the window's buffer for the mode line display. */
16713 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16714 set_buffer_internal_1 (XBUFFER (w->buffer));
16715
16716 /* Point refers normally to the selected window. For any
16717 other window, set up appropriate value. */
16718 if (!EQ (window, selected_window))
16719 {
16720 struct text_pos pt;
16721
16722 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16723 if (CHARPOS (pt) < BEGV)
16724 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16725 else if (CHARPOS (pt) > (ZV - 1))
16726 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16727 else
16728 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16729 }
16730
16731 /* Display mode lines. */
16732 clear_glyph_matrix (w->desired_matrix);
16733 if (display_mode_lines (w))
16734 {
16735 ++nwindows;
16736 w->must_be_updated_p = 1;
16737 }
16738
16739 /* Restore old settings. */
16740 set_buffer_internal_1 (old);
16741 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16742 }
16743
16744 window = w->next;
16745 }
16746
16747 return nwindows;
16748 }
16749
16750
16751 /* Display the mode and/or top line of window W. Value is the number
16752 of mode lines displayed. */
16753
16754 static int
16755 display_mode_lines (w)
16756 struct window *w;
16757 {
16758 Lisp_Object old_selected_window, old_selected_frame;
16759 int n = 0;
16760
16761 old_selected_frame = selected_frame;
16762 selected_frame = w->frame;
16763 old_selected_window = selected_window;
16764 XSETWINDOW (selected_window, w);
16765
16766 /* These will be set while the mode line specs are processed. */
16767 line_number_displayed = 0;
16768 w->column_number_displayed = Qnil;
16769
16770 if (WINDOW_WANTS_MODELINE_P (w))
16771 {
16772 struct window *sel_w = XWINDOW (old_selected_window);
16773
16774 /* Select mode line face based on the real selected window. */
16775 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16776 current_buffer->mode_line_format);
16777 ++n;
16778 }
16779
16780 if (WINDOW_WANTS_HEADER_LINE_P (w))
16781 {
16782 display_mode_line (w, HEADER_LINE_FACE_ID,
16783 current_buffer->header_line_format);
16784 ++n;
16785 }
16786
16787 selected_frame = old_selected_frame;
16788 selected_window = old_selected_window;
16789 return n;
16790 }
16791
16792
16793 /* Display mode or top line of window W. FACE_ID specifies which line
16794 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16795 FORMAT is the mode line format to display. Value is the pixel
16796 height of the mode line displayed. */
16797
16798 static int
16799 display_mode_line (w, face_id, format)
16800 struct window *w;
16801 enum face_id face_id;
16802 Lisp_Object format;
16803 {
16804 struct it it;
16805 struct face *face;
16806 int count = SPECPDL_INDEX ();
16807
16808 init_iterator (&it, w, -1, -1, NULL, face_id);
16809 /* Don't extend on a previously drawn mode-line.
16810 This may happen if called from pos_visible_p. */
16811 it.glyph_row->enabled_p = 0;
16812 prepare_desired_row (it.glyph_row);
16813
16814 it.glyph_row->mode_line_p = 1;
16815
16816 if (! mode_line_inverse_video)
16817 /* Force the mode-line to be displayed in the default face. */
16818 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16819
16820 record_unwind_protect (unwind_format_mode_line,
16821 format_mode_line_unwind_data (NULL, 0));
16822
16823 mode_line_target = MODE_LINE_DISPLAY;
16824
16825 /* Temporarily make frame's keyboard the current kboard so that
16826 kboard-local variables in the mode_line_format will get the right
16827 values. */
16828 push_frame_kboard (it.f);
16829 record_unwind_save_match_data ();
16830 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16831 pop_frame_kboard ();
16832
16833 unbind_to (count, Qnil);
16834
16835 /* Fill up with spaces. */
16836 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16837
16838 compute_line_metrics (&it);
16839 it.glyph_row->full_width_p = 1;
16840 it.glyph_row->continued_p = 0;
16841 it.glyph_row->truncated_on_left_p = 0;
16842 it.glyph_row->truncated_on_right_p = 0;
16843
16844 /* Make a 3D mode-line have a shadow at its right end. */
16845 face = FACE_FROM_ID (it.f, face_id);
16846 extend_face_to_end_of_line (&it);
16847 if (face->box != FACE_NO_BOX)
16848 {
16849 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16850 + it.glyph_row->used[TEXT_AREA] - 1);
16851 last->right_box_line_p = 1;
16852 }
16853
16854 return it.glyph_row->height;
16855 }
16856
16857 /* Move element ELT in LIST to the front of LIST.
16858 Return the updated list. */
16859
16860 static Lisp_Object
16861 move_elt_to_front (elt, list)
16862 Lisp_Object elt, list;
16863 {
16864 register Lisp_Object tail, prev;
16865 register Lisp_Object tem;
16866
16867 tail = list;
16868 prev = Qnil;
16869 while (CONSP (tail))
16870 {
16871 tem = XCAR (tail);
16872
16873 if (EQ (elt, tem))
16874 {
16875 /* Splice out the link TAIL. */
16876 if (NILP (prev))
16877 list = XCDR (tail);
16878 else
16879 Fsetcdr (prev, XCDR (tail));
16880
16881 /* Now make it the first. */
16882 Fsetcdr (tail, list);
16883 return tail;
16884 }
16885 else
16886 prev = tail;
16887 tail = XCDR (tail);
16888 QUIT;
16889 }
16890
16891 /* Not found--return unchanged LIST. */
16892 return list;
16893 }
16894
16895 /* Contribute ELT to the mode line for window IT->w. How it
16896 translates into text depends on its data type.
16897
16898 IT describes the display environment in which we display, as usual.
16899
16900 DEPTH is the depth in recursion. It is used to prevent
16901 infinite recursion here.
16902
16903 FIELD_WIDTH is the number of characters the display of ELT should
16904 occupy in the mode line, and PRECISION is the maximum number of
16905 characters to display from ELT's representation. See
16906 display_string for details.
16907
16908 Returns the hpos of the end of the text generated by ELT.
16909
16910 PROPS is a property list to add to any string we encounter.
16911
16912 If RISKY is nonzero, remove (disregard) any properties in any string
16913 we encounter, and ignore :eval and :propertize.
16914
16915 The global variable `mode_line_target' determines whether the
16916 output is passed to `store_mode_line_noprop',
16917 `store_mode_line_string', or `display_string'. */
16918
16919 static int
16920 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16921 struct it *it;
16922 int depth;
16923 int field_width, precision;
16924 Lisp_Object elt, props;
16925 int risky;
16926 {
16927 int n = 0, field, prec;
16928 int literal = 0;
16929
16930 tail_recurse:
16931 if (depth > 100)
16932 elt = build_string ("*too-deep*");
16933
16934 depth++;
16935
16936 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16937 {
16938 case Lisp_String:
16939 {
16940 /* A string: output it and check for %-constructs within it. */
16941 unsigned char c;
16942 int offset = 0;
16943
16944 if (SCHARS (elt) > 0
16945 && (!NILP (props) || risky))
16946 {
16947 Lisp_Object oprops, aelt;
16948 oprops = Ftext_properties_at (make_number (0), elt);
16949
16950 /* If the starting string's properties are not what
16951 we want, translate the string. Also, if the string
16952 is risky, do that anyway. */
16953
16954 if (NILP (Fequal (props, oprops)) || risky)
16955 {
16956 /* If the starting string has properties,
16957 merge the specified ones onto the existing ones. */
16958 if (! NILP (oprops) && !risky)
16959 {
16960 Lisp_Object tem;
16961
16962 oprops = Fcopy_sequence (oprops);
16963 tem = props;
16964 while (CONSP (tem))
16965 {
16966 oprops = Fplist_put (oprops, XCAR (tem),
16967 XCAR (XCDR (tem)));
16968 tem = XCDR (XCDR (tem));
16969 }
16970 props = oprops;
16971 }
16972
16973 aelt = Fassoc (elt, mode_line_proptrans_alist);
16974 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16975 {
16976 /* AELT is what we want. Move it to the front
16977 without consing. */
16978 elt = XCAR (aelt);
16979 mode_line_proptrans_alist
16980 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16981 }
16982 else
16983 {
16984 Lisp_Object tem;
16985
16986 /* If AELT has the wrong props, it is useless.
16987 so get rid of it. */
16988 if (! NILP (aelt))
16989 mode_line_proptrans_alist
16990 = Fdelq (aelt, mode_line_proptrans_alist);
16991
16992 elt = Fcopy_sequence (elt);
16993 Fset_text_properties (make_number (0), Flength (elt),
16994 props, elt);
16995 /* Add this item to mode_line_proptrans_alist. */
16996 mode_line_proptrans_alist
16997 = Fcons (Fcons (elt, props),
16998 mode_line_proptrans_alist);
16999 /* Truncate mode_line_proptrans_alist
17000 to at most 50 elements. */
17001 tem = Fnthcdr (make_number (50),
17002 mode_line_proptrans_alist);
17003 if (! NILP (tem))
17004 XSETCDR (tem, Qnil);
17005 }
17006 }
17007 }
17008
17009 offset = 0;
17010
17011 if (literal)
17012 {
17013 prec = precision - n;
17014 switch (mode_line_target)
17015 {
17016 case MODE_LINE_NOPROP:
17017 case MODE_LINE_TITLE:
17018 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17019 break;
17020 case MODE_LINE_STRING:
17021 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17022 break;
17023 case MODE_LINE_DISPLAY:
17024 n += display_string (NULL, elt, Qnil, 0, 0, it,
17025 0, prec, 0, STRING_MULTIBYTE (elt));
17026 break;
17027 }
17028
17029 break;
17030 }
17031
17032 /* Handle the non-literal case. */
17033
17034 while ((precision <= 0 || n < precision)
17035 && SREF (elt, offset) != 0
17036 && (mode_line_target != MODE_LINE_DISPLAY
17037 || it->current_x < it->last_visible_x))
17038 {
17039 int last_offset = offset;
17040
17041 /* Advance to end of string or next format specifier. */
17042 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17043 ;
17044
17045 if (offset - 1 != last_offset)
17046 {
17047 int nchars, nbytes;
17048
17049 /* Output to end of string or up to '%'. Field width
17050 is length of string. Don't output more than
17051 PRECISION allows us. */
17052 offset--;
17053
17054 prec = c_string_width (SDATA (elt) + last_offset,
17055 offset - last_offset, precision - n,
17056 &nchars, &nbytes);
17057
17058 switch (mode_line_target)
17059 {
17060 case MODE_LINE_NOPROP:
17061 case MODE_LINE_TITLE:
17062 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17063 break;
17064 case MODE_LINE_STRING:
17065 {
17066 int bytepos = last_offset;
17067 int charpos = string_byte_to_char (elt, bytepos);
17068 int endpos = (precision <= 0
17069 ? string_byte_to_char (elt, offset)
17070 : charpos + nchars);
17071
17072 n += store_mode_line_string (NULL,
17073 Fsubstring (elt, make_number (charpos),
17074 make_number (endpos)),
17075 0, 0, 0, Qnil);
17076 }
17077 break;
17078 case MODE_LINE_DISPLAY:
17079 {
17080 int bytepos = last_offset;
17081 int charpos = string_byte_to_char (elt, bytepos);
17082
17083 if (precision <= 0)
17084 nchars = string_byte_to_char (elt, offset) - charpos;
17085 n += display_string (NULL, elt, Qnil, 0, charpos,
17086 it, 0, nchars, 0,
17087 STRING_MULTIBYTE (elt));
17088 }
17089 break;
17090 }
17091 }
17092 else /* c == '%' */
17093 {
17094 int percent_position = offset;
17095
17096 /* Get the specified minimum width. Zero means
17097 don't pad. */
17098 field = 0;
17099 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17100 field = field * 10 + c - '0';
17101
17102 /* Don't pad beyond the total padding allowed. */
17103 if (field_width - n > 0 && field > field_width - n)
17104 field = field_width - n;
17105
17106 /* Note that either PRECISION <= 0 or N < PRECISION. */
17107 prec = precision - n;
17108
17109 if (c == 'M')
17110 n += display_mode_element (it, depth, field, prec,
17111 Vglobal_mode_string, props,
17112 risky);
17113 else if (c != 0)
17114 {
17115 int multibyte;
17116 int bytepos, charpos;
17117 unsigned char *spec;
17118
17119 bytepos = percent_position;
17120 charpos = (STRING_MULTIBYTE (elt)
17121 ? string_byte_to_char (elt, bytepos)
17122 : bytepos);
17123
17124 spec
17125 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17126
17127 switch (mode_line_target)
17128 {
17129 case MODE_LINE_NOPROP:
17130 case MODE_LINE_TITLE:
17131 n += store_mode_line_noprop (spec, field, prec);
17132 break;
17133 case MODE_LINE_STRING:
17134 {
17135 int len = strlen (spec);
17136 Lisp_Object tem = make_string (spec, len);
17137 props = Ftext_properties_at (make_number (charpos), elt);
17138 /* Should only keep face property in props */
17139 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17140 }
17141 break;
17142 case MODE_LINE_DISPLAY:
17143 {
17144 int nglyphs_before, nwritten;
17145
17146 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17147 nwritten = display_string (spec, Qnil, elt,
17148 charpos, 0, it,
17149 field, prec, 0,
17150 multibyte);
17151
17152 /* Assign to the glyphs written above the
17153 string where the `%x' came from, position
17154 of the `%'. */
17155 if (nwritten > 0)
17156 {
17157 struct glyph *glyph
17158 = (it->glyph_row->glyphs[TEXT_AREA]
17159 + nglyphs_before);
17160 int i;
17161
17162 for (i = 0; i < nwritten; ++i)
17163 {
17164 glyph[i].object = elt;
17165 glyph[i].charpos = charpos;
17166 }
17167
17168 n += nwritten;
17169 }
17170 }
17171 break;
17172 }
17173 }
17174 else /* c == 0 */
17175 break;
17176 }
17177 }
17178 }
17179 break;
17180
17181 case Lisp_Symbol:
17182 /* A symbol: process the value of the symbol recursively
17183 as if it appeared here directly. Avoid error if symbol void.
17184 Special case: if value of symbol is a string, output the string
17185 literally. */
17186 {
17187 register Lisp_Object tem;
17188
17189 /* If the variable is not marked as risky to set
17190 then its contents are risky to use. */
17191 if (NILP (Fget (elt, Qrisky_local_variable)))
17192 risky = 1;
17193
17194 tem = Fboundp (elt);
17195 if (!NILP (tem))
17196 {
17197 tem = Fsymbol_value (elt);
17198 /* If value is a string, output that string literally:
17199 don't check for % within it. */
17200 if (STRINGP (tem))
17201 literal = 1;
17202
17203 if (!EQ (tem, elt))
17204 {
17205 /* Give up right away for nil or t. */
17206 elt = tem;
17207 goto tail_recurse;
17208 }
17209 }
17210 }
17211 break;
17212
17213 case Lisp_Cons:
17214 {
17215 register Lisp_Object car, tem;
17216
17217 /* A cons cell: five distinct cases.
17218 If first element is :eval or :propertize, do something special.
17219 If first element is a string or a cons, process all the elements
17220 and effectively concatenate them.
17221 If first element is a negative number, truncate displaying cdr to
17222 at most that many characters. If positive, pad (with spaces)
17223 to at least that many characters.
17224 If first element is a symbol, process the cadr or caddr recursively
17225 according to whether the symbol's value is non-nil or nil. */
17226 car = XCAR (elt);
17227 if (EQ (car, QCeval))
17228 {
17229 /* An element of the form (:eval FORM) means evaluate FORM
17230 and use the result as mode line elements. */
17231
17232 if (risky)
17233 break;
17234
17235 if (CONSP (XCDR (elt)))
17236 {
17237 Lisp_Object spec;
17238 spec = safe_eval (XCAR (XCDR (elt)));
17239 n += display_mode_element (it, depth, field_width - n,
17240 precision - n, spec, props,
17241 risky);
17242 }
17243 }
17244 else if (EQ (car, QCpropertize))
17245 {
17246 /* An element of the form (:propertize ELT PROPS...)
17247 means display ELT but applying properties PROPS. */
17248
17249 if (risky)
17250 break;
17251
17252 if (CONSP (XCDR (elt)))
17253 n += display_mode_element (it, depth, field_width - n,
17254 precision - n, XCAR (XCDR (elt)),
17255 XCDR (XCDR (elt)), risky);
17256 }
17257 else if (SYMBOLP (car))
17258 {
17259 tem = Fboundp (car);
17260 elt = XCDR (elt);
17261 if (!CONSP (elt))
17262 goto invalid;
17263 /* elt is now the cdr, and we know it is a cons cell.
17264 Use its car if CAR has a non-nil value. */
17265 if (!NILP (tem))
17266 {
17267 tem = Fsymbol_value (car);
17268 if (!NILP (tem))
17269 {
17270 elt = XCAR (elt);
17271 goto tail_recurse;
17272 }
17273 }
17274 /* Symbol's value is nil (or symbol is unbound)
17275 Get the cddr of the original list
17276 and if possible find the caddr and use that. */
17277 elt = XCDR (elt);
17278 if (NILP (elt))
17279 break;
17280 else if (!CONSP (elt))
17281 goto invalid;
17282 elt = XCAR (elt);
17283 goto tail_recurse;
17284 }
17285 else if (INTEGERP (car))
17286 {
17287 register int lim = XINT (car);
17288 elt = XCDR (elt);
17289 if (lim < 0)
17290 {
17291 /* Negative int means reduce maximum width. */
17292 if (precision <= 0)
17293 precision = -lim;
17294 else
17295 precision = min (precision, -lim);
17296 }
17297 else if (lim > 0)
17298 {
17299 /* Padding specified. Don't let it be more than
17300 current maximum. */
17301 if (precision > 0)
17302 lim = min (precision, lim);
17303
17304 /* If that's more padding than already wanted, queue it.
17305 But don't reduce padding already specified even if
17306 that is beyond the current truncation point. */
17307 field_width = max (lim, field_width);
17308 }
17309 goto tail_recurse;
17310 }
17311 else if (STRINGP (car) || CONSP (car))
17312 {
17313 register int limit = 50;
17314 /* Limit is to protect against circular lists. */
17315 while (CONSP (elt)
17316 && --limit > 0
17317 && (precision <= 0 || n < precision))
17318 {
17319 n += display_mode_element (it, depth,
17320 /* Do padding only after the last
17321 element in the list. */
17322 (! CONSP (XCDR (elt))
17323 ? field_width - n
17324 : 0),
17325 precision - n, XCAR (elt),
17326 props, risky);
17327 elt = XCDR (elt);
17328 }
17329 }
17330 }
17331 break;
17332
17333 default:
17334 invalid:
17335 elt = build_string ("*invalid*");
17336 goto tail_recurse;
17337 }
17338
17339 /* Pad to FIELD_WIDTH. */
17340 if (field_width > 0 && n < field_width)
17341 {
17342 switch (mode_line_target)
17343 {
17344 case MODE_LINE_NOPROP:
17345 case MODE_LINE_TITLE:
17346 n += store_mode_line_noprop ("", field_width - n, 0);
17347 break;
17348 case MODE_LINE_STRING:
17349 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17350 break;
17351 case MODE_LINE_DISPLAY:
17352 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17353 0, 0, 0);
17354 break;
17355 }
17356 }
17357
17358 return n;
17359 }
17360
17361 /* Store a mode-line string element in mode_line_string_list.
17362
17363 If STRING is non-null, display that C string. Otherwise, the Lisp
17364 string LISP_STRING is displayed.
17365
17366 FIELD_WIDTH is the minimum number of output glyphs to produce.
17367 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17368 with spaces. FIELD_WIDTH <= 0 means don't pad.
17369
17370 PRECISION is the maximum number of characters to output from
17371 STRING. PRECISION <= 0 means don't truncate the string.
17372
17373 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17374 properties to the string.
17375
17376 PROPS are the properties to add to the string.
17377 The mode_line_string_face face property is always added to the string.
17378 */
17379
17380 static int
17381 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17382 char *string;
17383 Lisp_Object lisp_string;
17384 int copy_string;
17385 int field_width;
17386 int precision;
17387 Lisp_Object props;
17388 {
17389 int len;
17390 int n = 0;
17391
17392 if (string != NULL)
17393 {
17394 len = strlen (string);
17395 if (precision > 0 && len > precision)
17396 len = precision;
17397 lisp_string = make_string (string, len);
17398 if (NILP (props))
17399 props = mode_line_string_face_prop;
17400 else if (!NILP (mode_line_string_face))
17401 {
17402 Lisp_Object face = Fplist_get (props, Qface);
17403 props = Fcopy_sequence (props);
17404 if (NILP (face))
17405 face = mode_line_string_face;
17406 else
17407 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17408 props = Fplist_put (props, Qface, face);
17409 }
17410 Fadd_text_properties (make_number (0), make_number (len),
17411 props, lisp_string);
17412 }
17413 else
17414 {
17415 len = XFASTINT (Flength (lisp_string));
17416 if (precision > 0 && len > precision)
17417 {
17418 len = precision;
17419 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17420 precision = -1;
17421 }
17422 if (!NILP (mode_line_string_face))
17423 {
17424 Lisp_Object face;
17425 if (NILP (props))
17426 props = Ftext_properties_at (make_number (0), lisp_string);
17427 face = Fplist_get (props, Qface);
17428 if (NILP (face))
17429 face = mode_line_string_face;
17430 else
17431 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17432 props = Fcons (Qface, Fcons (face, Qnil));
17433 if (copy_string)
17434 lisp_string = Fcopy_sequence (lisp_string);
17435 }
17436 if (!NILP (props))
17437 Fadd_text_properties (make_number (0), make_number (len),
17438 props, lisp_string);
17439 }
17440
17441 if (len > 0)
17442 {
17443 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17444 n += len;
17445 }
17446
17447 if (field_width > len)
17448 {
17449 field_width -= len;
17450 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17451 if (!NILP (props))
17452 Fadd_text_properties (make_number (0), make_number (field_width),
17453 props, lisp_string);
17454 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17455 n += field_width;
17456 }
17457
17458 return n;
17459 }
17460
17461
17462 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17463 1, 4, 0,
17464 doc: /* Format a string out of a mode line format specification.
17465 First arg FORMAT specifies the mode line format (see `mode-line-format'
17466 for details) to use.
17467
17468 Optional second arg FACE specifies the face property to put
17469 on all characters for which no face is specified.
17470 The value t means whatever face the window's mode line currently uses
17471 \(either `mode-line' or `mode-line-inactive', depending).
17472 A value of nil means the default is no face property.
17473 If FACE is an integer, the value string has no text properties.
17474
17475 Optional third and fourth args WINDOW and BUFFER specify the window
17476 and buffer to use as the context for the formatting (defaults
17477 are the selected window and the window's buffer). */)
17478 (format, face, window, buffer)
17479 Lisp_Object format, face, window, buffer;
17480 {
17481 struct it it;
17482 int len;
17483 struct window *w;
17484 struct buffer *old_buffer = NULL;
17485 int face_id = -1;
17486 int no_props = INTEGERP (face);
17487 int count = SPECPDL_INDEX ();
17488 Lisp_Object str;
17489 int string_start = 0;
17490
17491 if (NILP (window))
17492 window = selected_window;
17493 CHECK_WINDOW (window);
17494 w = XWINDOW (window);
17495
17496 if (NILP (buffer))
17497 buffer = w->buffer;
17498 CHECK_BUFFER (buffer);
17499
17500 if (NILP (format))
17501 return empty_unibyte_string;
17502
17503 if (no_props)
17504 face = Qnil;
17505
17506 if (!NILP (face))
17507 {
17508 if (EQ (face, Qt))
17509 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17510 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17511 }
17512
17513 if (face_id < 0)
17514 face_id = DEFAULT_FACE_ID;
17515
17516 if (XBUFFER (buffer) != current_buffer)
17517 old_buffer = current_buffer;
17518
17519 /* Save things including mode_line_proptrans_alist,
17520 and set that to nil so that we don't alter the outer value. */
17521 record_unwind_protect (unwind_format_mode_line,
17522 format_mode_line_unwind_data (old_buffer, 1));
17523 mode_line_proptrans_alist = Qnil;
17524
17525 if (old_buffer)
17526 set_buffer_internal_1 (XBUFFER (buffer));
17527
17528 init_iterator (&it, w, -1, -1, NULL, face_id);
17529
17530 if (no_props)
17531 {
17532 mode_line_target = MODE_LINE_NOPROP;
17533 mode_line_string_face_prop = Qnil;
17534 mode_line_string_list = Qnil;
17535 string_start = MODE_LINE_NOPROP_LEN (0);
17536 }
17537 else
17538 {
17539 mode_line_target = MODE_LINE_STRING;
17540 mode_line_string_list = Qnil;
17541 mode_line_string_face = face;
17542 mode_line_string_face_prop
17543 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17544 }
17545
17546 push_frame_kboard (it.f);
17547 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17548 pop_frame_kboard ();
17549
17550 if (no_props)
17551 {
17552 len = MODE_LINE_NOPROP_LEN (string_start);
17553 str = make_string (mode_line_noprop_buf + string_start, len);
17554 }
17555 else
17556 {
17557 mode_line_string_list = Fnreverse (mode_line_string_list);
17558 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17559 empty_unibyte_string);
17560 }
17561
17562 unbind_to (count, Qnil);
17563 return str;
17564 }
17565
17566 /* Write a null-terminated, right justified decimal representation of
17567 the positive integer D to BUF using a minimal field width WIDTH. */
17568
17569 static void
17570 pint2str (buf, width, d)
17571 register char *buf;
17572 register int width;
17573 register int d;
17574 {
17575 register char *p = buf;
17576
17577 if (d <= 0)
17578 *p++ = '0';
17579 else
17580 {
17581 while (d > 0)
17582 {
17583 *p++ = d % 10 + '0';
17584 d /= 10;
17585 }
17586 }
17587
17588 for (width -= (int) (p - buf); width > 0; --width)
17589 *p++ = ' ';
17590 *p-- = '\0';
17591 while (p > buf)
17592 {
17593 d = *buf;
17594 *buf++ = *p;
17595 *p-- = d;
17596 }
17597 }
17598
17599 /* Write a null-terminated, right justified decimal and "human
17600 readable" representation of the nonnegative integer D to BUF using
17601 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17602
17603 static const char power_letter[] =
17604 {
17605 0, /* not used */
17606 'k', /* kilo */
17607 'M', /* mega */
17608 'G', /* giga */
17609 'T', /* tera */
17610 'P', /* peta */
17611 'E', /* exa */
17612 'Z', /* zetta */
17613 'Y' /* yotta */
17614 };
17615
17616 static void
17617 pint2hrstr (buf, width, d)
17618 char *buf;
17619 int width;
17620 int d;
17621 {
17622 /* We aim to represent the nonnegative integer D as
17623 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17624 int quotient = d;
17625 int remainder = 0;
17626 /* -1 means: do not use TENTHS. */
17627 int tenths = -1;
17628 int exponent = 0;
17629
17630 /* Length of QUOTIENT.TENTHS as a string. */
17631 int length;
17632
17633 char * psuffix;
17634 char * p;
17635
17636 if (1000 <= quotient)
17637 {
17638 /* Scale to the appropriate EXPONENT. */
17639 do
17640 {
17641 remainder = quotient % 1000;
17642 quotient /= 1000;
17643 exponent++;
17644 }
17645 while (1000 <= quotient);
17646
17647 /* Round to nearest and decide whether to use TENTHS or not. */
17648 if (quotient <= 9)
17649 {
17650 tenths = remainder / 100;
17651 if (50 <= remainder % 100)
17652 {
17653 if (tenths < 9)
17654 tenths++;
17655 else
17656 {
17657 quotient++;
17658 if (quotient == 10)
17659 tenths = -1;
17660 else
17661 tenths = 0;
17662 }
17663 }
17664 }
17665 else
17666 if (500 <= remainder)
17667 {
17668 if (quotient < 999)
17669 quotient++;
17670 else
17671 {
17672 quotient = 1;
17673 exponent++;
17674 tenths = 0;
17675 }
17676 }
17677 }
17678
17679 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17680 if (tenths == -1 && quotient <= 99)
17681 if (quotient <= 9)
17682 length = 1;
17683 else
17684 length = 2;
17685 else
17686 length = 3;
17687 p = psuffix = buf + max (width, length);
17688
17689 /* Print EXPONENT. */
17690 if (exponent)
17691 *psuffix++ = power_letter[exponent];
17692 *psuffix = '\0';
17693
17694 /* Print TENTHS. */
17695 if (tenths >= 0)
17696 {
17697 *--p = '0' + tenths;
17698 *--p = '.';
17699 }
17700
17701 /* Print QUOTIENT. */
17702 do
17703 {
17704 int digit = quotient % 10;
17705 *--p = '0' + digit;
17706 }
17707 while ((quotient /= 10) != 0);
17708
17709 /* Print leading spaces. */
17710 while (buf < p)
17711 *--p = ' ';
17712 }
17713
17714 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17715 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17716 type of CODING_SYSTEM. Return updated pointer into BUF. */
17717
17718 static unsigned char invalid_eol_type[] = "(*invalid*)";
17719
17720 static char *
17721 decode_mode_spec_coding (coding_system, buf, eol_flag)
17722 Lisp_Object coding_system;
17723 register char *buf;
17724 int eol_flag;
17725 {
17726 Lisp_Object val;
17727 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17728 const unsigned char *eol_str;
17729 int eol_str_len;
17730 /* The EOL conversion we are using. */
17731 Lisp_Object eoltype;
17732
17733 val = CODING_SYSTEM_SPEC (coding_system);
17734 eoltype = Qnil;
17735
17736 if (!VECTORP (val)) /* Not yet decided. */
17737 {
17738 if (multibyte)
17739 *buf++ = '-';
17740 if (eol_flag)
17741 eoltype = eol_mnemonic_undecided;
17742 /* Don't mention EOL conversion if it isn't decided. */
17743 }
17744 else
17745 {
17746 Lisp_Object attrs;
17747 Lisp_Object eolvalue;
17748
17749 attrs = AREF (val, 0);
17750 eolvalue = AREF (val, 2);
17751
17752 if (multibyte)
17753 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
17754
17755 if (eol_flag)
17756 {
17757 /* The EOL conversion that is normal on this system. */
17758
17759 if (NILP (eolvalue)) /* Not yet decided. */
17760 eoltype = eol_mnemonic_undecided;
17761 else if (VECTORP (eolvalue)) /* Not yet decided. */
17762 eoltype = eol_mnemonic_undecided;
17763 else /* eolvalue is Qunix, Qdos, or Qmac. */
17764 eoltype = (EQ (eolvalue, Qunix)
17765 ? eol_mnemonic_unix
17766 : (EQ (eolvalue, Qdos) == 1
17767 ? eol_mnemonic_dos : eol_mnemonic_mac));
17768 }
17769 }
17770
17771 if (eol_flag)
17772 {
17773 /* Mention the EOL conversion if it is not the usual one. */
17774 if (STRINGP (eoltype))
17775 {
17776 eol_str = SDATA (eoltype);
17777 eol_str_len = SBYTES (eoltype);
17778 }
17779 else if (CHARACTERP (eoltype))
17780 {
17781 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17782 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17783 eol_str = tmp;
17784 }
17785 else
17786 {
17787 eol_str = invalid_eol_type;
17788 eol_str_len = sizeof (invalid_eol_type) - 1;
17789 }
17790 bcopy (eol_str, buf, eol_str_len);
17791 buf += eol_str_len;
17792 }
17793
17794 return buf;
17795 }
17796
17797 /* Return a string for the output of a mode line %-spec for window W,
17798 generated by character C. PRECISION >= 0 means don't return a
17799 string longer than that value. FIELD_WIDTH > 0 means pad the
17800 string returned with spaces to that value. Return 1 in *MULTIBYTE
17801 if the result is multibyte text.
17802
17803 Note we operate on the current buffer for most purposes,
17804 the exception being w->base_line_pos. */
17805
17806 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17807
17808 static char *
17809 decode_mode_spec (w, c, field_width, precision, multibyte)
17810 struct window *w;
17811 register int c;
17812 int field_width, precision;
17813 int *multibyte;
17814 {
17815 Lisp_Object obj;
17816 struct frame *f = XFRAME (WINDOW_FRAME (w));
17817 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17818 struct buffer *b = current_buffer;
17819
17820 obj = Qnil;
17821 *multibyte = 0;
17822
17823 switch (c)
17824 {
17825 case '*':
17826 if (!NILP (b->read_only))
17827 return "%";
17828 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17829 return "*";
17830 return "-";
17831
17832 case '+':
17833 /* This differs from %* only for a modified read-only buffer. */
17834 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17835 return "*";
17836 if (!NILP (b->read_only))
17837 return "%";
17838 return "-";
17839
17840 case '&':
17841 /* This differs from %* in ignoring read-only-ness. */
17842 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17843 return "*";
17844 return "-";
17845
17846 case '%':
17847 return "%";
17848
17849 case '[':
17850 {
17851 int i;
17852 char *p;
17853
17854 if (command_loop_level > 5)
17855 return "[[[... ";
17856 p = decode_mode_spec_buf;
17857 for (i = 0; i < command_loop_level; i++)
17858 *p++ = '[';
17859 *p = 0;
17860 return decode_mode_spec_buf;
17861 }
17862
17863 case ']':
17864 {
17865 int i;
17866 char *p;
17867
17868 if (command_loop_level > 5)
17869 return " ...]]]";
17870 p = decode_mode_spec_buf;
17871 for (i = 0; i < command_loop_level; i++)
17872 *p++ = ']';
17873 *p = 0;
17874 return decode_mode_spec_buf;
17875 }
17876
17877 case '-':
17878 {
17879 register int i;
17880
17881 /* Let lots_of_dashes be a string of infinite length. */
17882 if (mode_line_target == MODE_LINE_NOPROP ||
17883 mode_line_target == MODE_LINE_STRING)
17884 return "--";
17885 if (field_width <= 0
17886 || field_width > sizeof (lots_of_dashes))
17887 {
17888 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17889 decode_mode_spec_buf[i] = '-';
17890 decode_mode_spec_buf[i] = '\0';
17891 return decode_mode_spec_buf;
17892 }
17893 else
17894 return lots_of_dashes;
17895 }
17896
17897 case 'b':
17898 obj = b->name;
17899 break;
17900
17901 case 'c':
17902 /* %c and %l are ignored in `frame-title-format'.
17903 (In redisplay_internal, the frame title is drawn _before_ the
17904 windows are updated, so the stuff which depends on actual
17905 window contents (such as %l) may fail to render properly, or
17906 even crash emacs.) */
17907 if (mode_line_target == MODE_LINE_TITLE)
17908 return "";
17909 else
17910 {
17911 int col = (int) current_column (); /* iftc */
17912 w->column_number_displayed = make_number (col);
17913 pint2str (decode_mode_spec_buf, field_width, col);
17914 return decode_mode_spec_buf;
17915 }
17916
17917 case 'e':
17918 #ifndef SYSTEM_MALLOC
17919 {
17920 if (NILP (Vmemory_full))
17921 return "";
17922 else
17923 return "!MEM FULL! ";
17924 }
17925 #else
17926 return "";
17927 #endif
17928
17929 case 'F':
17930 /* %F displays the frame name. */
17931 if (!NILP (f->title))
17932 return (char *) SDATA (f->title);
17933 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17934 return (char *) SDATA (f->name);
17935 return "Emacs";
17936
17937 case 'f':
17938 obj = b->filename;
17939 break;
17940
17941 case 'i':
17942 {
17943 int size = ZV - BEGV;
17944 pint2str (decode_mode_spec_buf, field_width, size);
17945 return decode_mode_spec_buf;
17946 }
17947
17948 case 'I':
17949 {
17950 int size = ZV - BEGV;
17951 pint2hrstr (decode_mode_spec_buf, field_width, size);
17952 return decode_mode_spec_buf;
17953 }
17954
17955 case 'l':
17956 {
17957 int startpos, startpos_byte, line, linepos, linepos_byte;
17958 int topline, nlines, junk, height;
17959
17960 /* %c and %l are ignored in `frame-title-format'. */
17961 if (mode_line_target == MODE_LINE_TITLE)
17962 return "";
17963
17964 startpos = XMARKER (w->start)->charpos;
17965 startpos_byte = marker_byte_position (w->start);
17966 height = WINDOW_TOTAL_LINES (w);
17967
17968 /* If we decided that this buffer isn't suitable for line numbers,
17969 don't forget that too fast. */
17970 if (EQ (w->base_line_pos, w->buffer))
17971 goto no_value;
17972 /* But do forget it, if the window shows a different buffer now. */
17973 else if (BUFFERP (w->base_line_pos))
17974 w->base_line_pos = Qnil;
17975
17976 /* If the buffer is very big, don't waste time. */
17977 if (INTEGERP (Vline_number_display_limit)
17978 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17979 {
17980 w->base_line_pos = Qnil;
17981 w->base_line_number = Qnil;
17982 goto no_value;
17983 }
17984
17985 if (!NILP (w->base_line_number)
17986 && !NILP (w->base_line_pos)
17987 && XFASTINT (w->base_line_pos) <= startpos)
17988 {
17989 line = XFASTINT (w->base_line_number);
17990 linepos = XFASTINT (w->base_line_pos);
17991 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17992 }
17993 else
17994 {
17995 line = 1;
17996 linepos = BUF_BEGV (b);
17997 linepos_byte = BUF_BEGV_BYTE (b);
17998 }
17999
18000 /* Count lines from base line to window start position. */
18001 nlines = display_count_lines (linepos, linepos_byte,
18002 startpos_byte,
18003 startpos, &junk);
18004
18005 topline = nlines + line;
18006
18007 /* Determine a new base line, if the old one is too close
18008 or too far away, or if we did not have one.
18009 "Too close" means it's plausible a scroll-down would
18010 go back past it. */
18011 if (startpos == BUF_BEGV (b))
18012 {
18013 w->base_line_number = make_number (topline);
18014 w->base_line_pos = make_number (BUF_BEGV (b));
18015 }
18016 else if (nlines < height + 25 || nlines > height * 3 + 50
18017 || linepos == BUF_BEGV (b))
18018 {
18019 int limit = BUF_BEGV (b);
18020 int limit_byte = BUF_BEGV_BYTE (b);
18021 int position;
18022 int distance = (height * 2 + 30) * line_number_display_limit_width;
18023
18024 if (startpos - distance > limit)
18025 {
18026 limit = startpos - distance;
18027 limit_byte = CHAR_TO_BYTE (limit);
18028 }
18029
18030 nlines = display_count_lines (startpos, startpos_byte,
18031 limit_byte,
18032 - (height * 2 + 30),
18033 &position);
18034 /* If we couldn't find the lines we wanted within
18035 line_number_display_limit_width chars per line,
18036 give up on line numbers for this window. */
18037 if (position == limit_byte && limit == startpos - distance)
18038 {
18039 w->base_line_pos = w->buffer;
18040 w->base_line_number = Qnil;
18041 goto no_value;
18042 }
18043
18044 w->base_line_number = make_number (topline - nlines);
18045 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18046 }
18047
18048 /* Now count lines from the start pos to point. */
18049 nlines = display_count_lines (startpos, startpos_byte,
18050 PT_BYTE, PT, &junk);
18051
18052 /* Record that we did display the line number. */
18053 line_number_displayed = 1;
18054
18055 /* Make the string to show. */
18056 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18057 return decode_mode_spec_buf;
18058 no_value:
18059 {
18060 char* p = decode_mode_spec_buf;
18061 int pad = field_width - 2;
18062 while (pad-- > 0)
18063 *p++ = ' ';
18064 *p++ = '?';
18065 *p++ = '?';
18066 *p = '\0';
18067 return decode_mode_spec_buf;
18068 }
18069 }
18070 break;
18071
18072 case 'm':
18073 obj = b->mode_name;
18074 break;
18075
18076 case 'n':
18077 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18078 return " Narrow";
18079 break;
18080
18081 case 'p':
18082 {
18083 int pos = marker_position (w->start);
18084 int total = BUF_ZV (b) - BUF_BEGV (b);
18085
18086 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18087 {
18088 if (pos <= BUF_BEGV (b))
18089 return "All";
18090 else
18091 return "Bottom";
18092 }
18093 else if (pos <= BUF_BEGV (b))
18094 return "Top";
18095 else
18096 {
18097 if (total > 1000000)
18098 /* Do it differently for a large value, to avoid overflow. */
18099 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18100 else
18101 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18102 /* We can't normally display a 3-digit number,
18103 so get us a 2-digit number that is close. */
18104 if (total == 100)
18105 total = 99;
18106 sprintf (decode_mode_spec_buf, "%2d%%", total);
18107 return decode_mode_spec_buf;
18108 }
18109 }
18110
18111 /* Display percentage of size above the bottom of the screen. */
18112 case 'P':
18113 {
18114 int toppos = marker_position (w->start);
18115 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18116 int total = BUF_ZV (b) - BUF_BEGV (b);
18117
18118 if (botpos >= BUF_ZV (b))
18119 {
18120 if (toppos <= BUF_BEGV (b))
18121 return "All";
18122 else
18123 return "Bottom";
18124 }
18125 else
18126 {
18127 if (total > 1000000)
18128 /* Do it differently for a large value, to avoid overflow. */
18129 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18130 else
18131 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18132 /* We can't normally display a 3-digit number,
18133 so get us a 2-digit number that is close. */
18134 if (total == 100)
18135 total = 99;
18136 if (toppos <= BUF_BEGV (b))
18137 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18138 else
18139 sprintf (decode_mode_spec_buf, "%2d%%", total);
18140 return decode_mode_spec_buf;
18141 }
18142 }
18143
18144 case 's':
18145 /* status of process */
18146 obj = Fget_buffer_process (Fcurrent_buffer ());
18147 if (NILP (obj))
18148 return "no process";
18149 #ifdef subprocesses
18150 obj = Fsymbol_name (Fprocess_status (obj));
18151 #endif
18152 break;
18153
18154 case '@':
18155 {
18156 Lisp_Object val;
18157 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18158 if (NILP (val))
18159 return "-";
18160 else
18161 return "@";
18162 }
18163
18164 case 't': /* indicate TEXT or BINARY */
18165 #ifdef MODE_LINE_BINARY_TEXT
18166 return MODE_LINE_BINARY_TEXT (b);
18167 #else
18168 return "T";
18169 #endif
18170
18171 case 'z':
18172 /* coding-system (not including end-of-line format) */
18173 case 'Z':
18174 /* coding-system (including end-of-line type) */
18175 {
18176 int eol_flag = (c == 'Z');
18177 char *p = decode_mode_spec_buf;
18178
18179 if (! FRAME_WINDOW_P (f))
18180 {
18181 /* No need to mention EOL here--the terminal never needs
18182 to do EOL conversion. */
18183 p = decode_mode_spec_coding (CODING_ID_NAME (keyboard_coding.id),
18184 p, 0);
18185 p = decode_mode_spec_coding (CODING_ID_NAME (terminal_coding.id),
18186 p, 0);
18187 }
18188 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18189 p, eol_flag);
18190
18191 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18192 #ifdef subprocesses
18193 obj = Fget_buffer_process (Fcurrent_buffer ());
18194 if (PROCESSP (obj))
18195 {
18196 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18197 p, eol_flag);
18198 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18199 p, eol_flag);
18200 }
18201 #endif /* subprocesses */
18202 #endif /* 0 */
18203 *p = 0;
18204 return decode_mode_spec_buf;
18205 }
18206 }
18207
18208 if (STRINGP (obj))
18209 {
18210 *multibyte = STRING_MULTIBYTE (obj);
18211 return (char *) SDATA (obj);
18212 }
18213 else
18214 return "";
18215 }
18216
18217
18218 /* Count up to COUNT lines starting from START / START_BYTE.
18219 But don't go beyond LIMIT_BYTE.
18220 Return the number of lines thus found (always nonnegative).
18221
18222 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18223
18224 static int
18225 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18226 int start, start_byte, limit_byte, count;
18227 int *byte_pos_ptr;
18228 {
18229 register unsigned char *cursor;
18230 unsigned char *base;
18231
18232 register int ceiling;
18233 register unsigned char *ceiling_addr;
18234 int orig_count = count;
18235
18236 /* If we are not in selective display mode,
18237 check only for newlines. */
18238 int selective_display = (!NILP (current_buffer->selective_display)
18239 && !INTEGERP (current_buffer->selective_display));
18240
18241 if (count > 0)
18242 {
18243 while (start_byte < limit_byte)
18244 {
18245 ceiling = BUFFER_CEILING_OF (start_byte);
18246 ceiling = min (limit_byte - 1, ceiling);
18247 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18248 base = (cursor = BYTE_POS_ADDR (start_byte));
18249 while (1)
18250 {
18251 if (selective_display)
18252 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18253 ;
18254 else
18255 while (*cursor != '\n' && ++cursor != ceiling_addr)
18256 ;
18257
18258 if (cursor != ceiling_addr)
18259 {
18260 if (--count == 0)
18261 {
18262 start_byte += cursor - base + 1;
18263 *byte_pos_ptr = start_byte;
18264 return orig_count;
18265 }
18266 else
18267 if (++cursor == ceiling_addr)
18268 break;
18269 }
18270 else
18271 break;
18272 }
18273 start_byte += cursor - base;
18274 }
18275 }
18276 else
18277 {
18278 while (start_byte > limit_byte)
18279 {
18280 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18281 ceiling = max (limit_byte, ceiling);
18282 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18283 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18284 while (1)
18285 {
18286 if (selective_display)
18287 while (--cursor != ceiling_addr
18288 && *cursor != '\n' && *cursor != 015)
18289 ;
18290 else
18291 while (--cursor != ceiling_addr && *cursor != '\n')
18292 ;
18293
18294 if (cursor != ceiling_addr)
18295 {
18296 if (++count == 0)
18297 {
18298 start_byte += cursor - base + 1;
18299 *byte_pos_ptr = start_byte;
18300 /* When scanning backwards, we should
18301 not count the newline posterior to which we stop. */
18302 return - orig_count - 1;
18303 }
18304 }
18305 else
18306 break;
18307 }
18308 /* Here we add 1 to compensate for the last decrement
18309 of CURSOR, which took it past the valid range. */
18310 start_byte += cursor - base + 1;
18311 }
18312 }
18313
18314 *byte_pos_ptr = limit_byte;
18315
18316 if (count < 0)
18317 return - orig_count + count;
18318 return orig_count - count;
18319
18320 }
18321
18322
18323 \f
18324 /***********************************************************************
18325 Displaying strings
18326 ***********************************************************************/
18327
18328 /* Display a NUL-terminated string, starting with index START.
18329
18330 If STRING is non-null, display that C string. Otherwise, the Lisp
18331 string LISP_STRING is displayed.
18332
18333 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18334 FACE_STRING. Display STRING or LISP_STRING with the face at
18335 FACE_STRING_POS in FACE_STRING:
18336
18337 Display the string in the environment given by IT, but use the
18338 standard display table, temporarily.
18339
18340 FIELD_WIDTH is the minimum number of output glyphs to produce.
18341 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18342 with spaces. If STRING has more characters, more than FIELD_WIDTH
18343 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18344
18345 PRECISION is the maximum number of characters to output from
18346 STRING. PRECISION < 0 means don't truncate the string.
18347
18348 This is roughly equivalent to printf format specifiers:
18349
18350 FIELD_WIDTH PRECISION PRINTF
18351 ----------------------------------------
18352 -1 -1 %s
18353 -1 10 %.10s
18354 10 -1 %10s
18355 20 10 %20.10s
18356
18357 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18358 display them, and < 0 means obey the current buffer's value of
18359 enable_multibyte_characters.
18360
18361 Value is the number of columns displayed. */
18362
18363 static int
18364 display_string (string, lisp_string, face_string, face_string_pos,
18365 start, it, field_width, precision, max_x, multibyte)
18366 unsigned char *string;
18367 Lisp_Object lisp_string;
18368 Lisp_Object face_string;
18369 int face_string_pos;
18370 int start;
18371 struct it *it;
18372 int field_width, precision, max_x;
18373 int multibyte;
18374 {
18375 int hpos_at_start = it->hpos;
18376 int saved_face_id = it->face_id;
18377 struct glyph_row *row = it->glyph_row;
18378
18379 /* Initialize the iterator IT for iteration over STRING beginning
18380 with index START. */
18381 reseat_to_string (it, string, lisp_string, start,
18382 precision, field_width, multibyte);
18383
18384 /* If displaying STRING, set up the face of the iterator
18385 from LISP_STRING, if that's given. */
18386 if (STRINGP (face_string))
18387 {
18388 int endptr;
18389 struct face *face;
18390
18391 it->face_id
18392 = face_at_string_position (it->w, face_string, face_string_pos,
18393 0, it->region_beg_charpos,
18394 it->region_end_charpos,
18395 &endptr, it->base_face_id, 0);
18396 face = FACE_FROM_ID (it->f, it->face_id);
18397 it->face_box_p = face->box != FACE_NO_BOX;
18398 }
18399
18400 /* Set max_x to the maximum allowed X position. Don't let it go
18401 beyond the right edge of the window. */
18402 if (max_x <= 0)
18403 max_x = it->last_visible_x;
18404 else
18405 max_x = min (max_x, it->last_visible_x);
18406
18407 /* Skip over display elements that are not visible. because IT->w is
18408 hscrolled. */
18409 if (it->current_x < it->first_visible_x)
18410 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18411 MOVE_TO_POS | MOVE_TO_X);
18412
18413 row->ascent = it->max_ascent;
18414 row->height = it->max_ascent + it->max_descent;
18415 row->phys_ascent = it->max_phys_ascent;
18416 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18417 row->extra_line_spacing = it->max_extra_line_spacing;
18418
18419 /* This condition is for the case that we are called with current_x
18420 past last_visible_x. */
18421 while (it->current_x < max_x)
18422 {
18423 int x_before, x, n_glyphs_before, i, nglyphs;
18424
18425 /* Get the next display element. */
18426 if (!get_next_display_element (it))
18427 break;
18428
18429 /* Produce glyphs. */
18430 x_before = it->current_x;
18431 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18432 PRODUCE_GLYPHS (it);
18433
18434 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18435 i = 0;
18436 x = x_before;
18437 while (i < nglyphs)
18438 {
18439 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18440
18441 if (!it->truncate_lines_p
18442 && x + glyph->pixel_width > max_x)
18443 {
18444 /* End of continued line or max_x reached. */
18445 if (CHAR_GLYPH_PADDING_P (*glyph))
18446 {
18447 /* A wide character is unbreakable. */
18448 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18449 it->current_x = x_before;
18450 }
18451 else
18452 {
18453 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18454 it->current_x = x;
18455 }
18456 break;
18457 }
18458 else if (x + glyph->pixel_width >= it->first_visible_x)
18459 {
18460 /* Glyph is at least partially visible. */
18461 ++it->hpos;
18462 if (x < it->first_visible_x)
18463 it->glyph_row->x = x - it->first_visible_x;
18464 }
18465 else
18466 {
18467 /* Glyph is off the left margin of the display area.
18468 Should not happen. */
18469 abort ();
18470 }
18471
18472 row->ascent = max (row->ascent, it->max_ascent);
18473 row->height = max (row->height, it->max_ascent + it->max_descent);
18474 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18475 row->phys_height = max (row->phys_height,
18476 it->max_phys_ascent + it->max_phys_descent);
18477 row->extra_line_spacing = max (row->extra_line_spacing,
18478 it->max_extra_line_spacing);
18479 x += glyph->pixel_width;
18480 ++i;
18481 }
18482
18483 /* Stop if max_x reached. */
18484 if (i < nglyphs)
18485 break;
18486
18487 /* Stop at line ends. */
18488 if (ITERATOR_AT_END_OF_LINE_P (it))
18489 {
18490 it->continuation_lines_width = 0;
18491 break;
18492 }
18493
18494 set_iterator_to_next (it, 1);
18495
18496 /* Stop if truncating at the right edge. */
18497 if (it->truncate_lines_p
18498 && it->current_x >= it->last_visible_x)
18499 {
18500 /* Add truncation mark, but don't do it if the line is
18501 truncated at a padding space. */
18502 if (IT_CHARPOS (*it) < it->string_nchars)
18503 {
18504 if (!FRAME_WINDOW_P (it->f))
18505 {
18506 int i, n;
18507
18508 if (it->current_x > it->last_visible_x)
18509 {
18510 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18511 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18512 break;
18513 for (n = row->used[TEXT_AREA]; i < n; ++i)
18514 {
18515 row->used[TEXT_AREA] = i;
18516 produce_special_glyphs (it, IT_TRUNCATION);
18517 }
18518 }
18519 produce_special_glyphs (it, IT_TRUNCATION);
18520 }
18521 it->glyph_row->truncated_on_right_p = 1;
18522 }
18523 break;
18524 }
18525 }
18526
18527 /* Maybe insert a truncation at the left. */
18528 if (it->first_visible_x
18529 && IT_CHARPOS (*it) > 0)
18530 {
18531 if (!FRAME_WINDOW_P (it->f))
18532 insert_left_trunc_glyphs (it);
18533 it->glyph_row->truncated_on_left_p = 1;
18534 }
18535
18536 it->face_id = saved_face_id;
18537
18538 /* Value is number of columns displayed. */
18539 return it->hpos - hpos_at_start;
18540 }
18541
18542
18543 \f
18544 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18545 appears as an element of LIST or as the car of an element of LIST.
18546 If PROPVAL is a list, compare each element against LIST in that
18547 way, and return 1/2 if any element of PROPVAL is found in LIST.
18548 Otherwise return 0. This function cannot quit.
18549 The return value is 2 if the text is invisible but with an ellipsis
18550 and 1 if it's invisible and without an ellipsis. */
18551
18552 int
18553 invisible_p (propval, list)
18554 register Lisp_Object propval;
18555 Lisp_Object list;
18556 {
18557 register Lisp_Object tail, proptail;
18558
18559 for (tail = list; CONSP (tail); tail = XCDR (tail))
18560 {
18561 register Lisp_Object tem;
18562 tem = XCAR (tail);
18563 if (EQ (propval, tem))
18564 return 1;
18565 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18566 return NILP (XCDR (tem)) ? 1 : 2;
18567 }
18568
18569 if (CONSP (propval))
18570 {
18571 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18572 {
18573 Lisp_Object propelt;
18574 propelt = XCAR (proptail);
18575 for (tail = list; CONSP (tail); tail = XCDR (tail))
18576 {
18577 register Lisp_Object tem;
18578 tem = XCAR (tail);
18579 if (EQ (propelt, tem))
18580 return 1;
18581 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18582 return NILP (XCDR (tem)) ? 1 : 2;
18583 }
18584 }
18585 }
18586
18587 return 0;
18588 }
18589
18590 /* Calculate a width or height in pixels from a specification using
18591 the following elements:
18592
18593 SPEC ::=
18594 NUM - a (fractional) multiple of the default font width/height
18595 (NUM) - specifies exactly NUM pixels
18596 UNIT - a fixed number of pixels, see below.
18597 ELEMENT - size of a display element in pixels, see below.
18598 (NUM . SPEC) - equals NUM * SPEC
18599 (+ SPEC SPEC ...) - add pixel values
18600 (- SPEC SPEC ...) - subtract pixel values
18601 (- SPEC) - negate pixel value
18602
18603 NUM ::=
18604 INT or FLOAT - a number constant
18605 SYMBOL - use symbol's (buffer local) variable binding.
18606
18607 UNIT ::=
18608 in - pixels per inch *)
18609 mm - pixels per 1/1000 meter *)
18610 cm - pixels per 1/100 meter *)
18611 width - width of current font in pixels.
18612 height - height of current font in pixels.
18613
18614 *) using the ratio(s) defined in display-pixels-per-inch.
18615
18616 ELEMENT ::=
18617
18618 left-fringe - left fringe width in pixels
18619 right-fringe - right fringe width in pixels
18620
18621 left-margin - left margin width in pixels
18622 right-margin - right margin width in pixels
18623
18624 scroll-bar - scroll-bar area width in pixels
18625
18626 Examples:
18627
18628 Pixels corresponding to 5 inches:
18629 (5 . in)
18630
18631 Total width of non-text areas on left side of window (if scroll-bar is on left):
18632 '(space :width (+ left-fringe left-margin scroll-bar))
18633
18634 Align to first text column (in header line):
18635 '(space :align-to 0)
18636
18637 Align to middle of text area minus half the width of variable `my-image'
18638 containing a loaded image:
18639 '(space :align-to (0.5 . (- text my-image)))
18640
18641 Width of left margin minus width of 1 character in the default font:
18642 '(space :width (- left-margin 1))
18643
18644 Width of left margin minus width of 2 characters in the current font:
18645 '(space :width (- left-margin (2 . width)))
18646
18647 Center 1 character over left-margin (in header line):
18648 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18649
18650 Different ways to express width of left fringe plus left margin minus one pixel:
18651 '(space :width (- (+ left-fringe left-margin) (1)))
18652 '(space :width (+ left-fringe left-margin (- (1))))
18653 '(space :width (+ left-fringe left-margin (-1)))
18654
18655 */
18656
18657 #define NUMVAL(X) \
18658 ((INTEGERP (X) || FLOATP (X)) \
18659 ? XFLOATINT (X) \
18660 : - 1)
18661
18662 int
18663 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18664 double *res;
18665 struct it *it;
18666 Lisp_Object prop;
18667 void *font;
18668 int width_p, *align_to;
18669 {
18670 double pixels;
18671
18672 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18673 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18674
18675 if (NILP (prop))
18676 return OK_PIXELS (0);
18677
18678 if (SYMBOLP (prop))
18679 {
18680 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18681 {
18682 char *unit = SDATA (SYMBOL_NAME (prop));
18683
18684 if (unit[0] == 'i' && unit[1] == 'n')
18685 pixels = 1.0;
18686 else if (unit[0] == 'm' && unit[1] == 'm')
18687 pixels = 25.4;
18688 else if (unit[0] == 'c' && unit[1] == 'm')
18689 pixels = 2.54;
18690 else
18691 pixels = 0;
18692 if (pixels > 0)
18693 {
18694 double ppi;
18695 #ifdef HAVE_WINDOW_SYSTEM
18696 if (FRAME_WINDOW_P (it->f)
18697 && (ppi = (width_p
18698 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18699 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18700 ppi > 0))
18701 return OK_PIXELS (ppi / pixels);
18702 #endif
18703
18704 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18705 || (CONSP (Vdisplay_pixels_per_inch)
18706 && (ppi = (width_p
18707 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18708 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18709 ppi > 0)))
18710 return OK_PIXELS (ppi / pixels);
18711
18712 return 0;
18713 }
18714 }
18715
18716 #ifdef HAVE_WINDOW_SYSTEM
18717 if (EQ (prop, Qheight))
18718 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18719 if (EQ (prop, Qwidth))
18720 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18721 #else
18722 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18723 return OK_PIXELS (1);
18724 #endif
18725
18726 if (EQ (prop, Qtext))
18727 return OK_PIXELS (width_p
18728 ? window_box_width (it->w, TEXT_AREA)
18729 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18730
18731 if (align_to && *align_to < 0)
18732 {
18733 *res = 0;
18734 if (EQ (prop, Qleft))
18735 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18736 if (EQ (prop, Qright))
18737 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18738 if (EQ (prop, Qcenter))
18739 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18740 + window_box_width (it->w, TEXT_AREA) / 2);
18741 if (EQ (prop, Qleft_fringe))
18742 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18743 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18744 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18745 if (EQ (prop, Qright_fringe))
18746 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18747 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18748 : window_box_right_offset (it->w, TEXT_AREA));
18749 if (EQ (prop, Qleft_margin))
18750 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18751 if (EQ (prop, Qright_margin))
18752 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18753 if (EQ (prop, Qscroll_bar))
18754 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18755 ? 0
18756 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18757 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18758 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18759 : 0)));
18760 }
18761 else
18762 {
18763 if (EQ (prop, Qleft_fringe))
18764 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18765 if (EQ (prop, Qright_fringe))
18766 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18767 if (EQ (prop, Qleft_margin))
18768 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18769 if (EQ (prop, Qright_margin))
18770 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18771 if (EQ (prop, Qscroll_bar))
18772 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18773 }
18774
18775 prop = Fbuffer_local_value (prop, it->w->buffer);
18776 }
18777
18778 if (INTEGERP (prop) || FLOATP (prop))
18779 {
18780 int base_unit = (width_p
18781 ? FRAME_COLUMN_WIDTH (it->f)
18782 : FRAME_LINE_HEIGHT (it->f));
18783 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18784 }
18785
18786 if (CONSP (prop))
18787 {
18788 Lisp_Object car = XCAR (prop);
18789 Lisp_Object cdr = XCDR (prop);
18790
18791 if (SYMBOLP (car))
18792 {
18793 #ifdef HAVE_WINDOW_SYSTEM
18794 if (valid_image_p (prop))
18795 {
18796 int id = lookup_image (it->f, prop);
18797 struct image *img = IMAGE_FROM_ID (it->f, id);
18798
18799 return OK_PIXELS (width_p ? img->width : img->height);
18800 }
18801 #endif
18802 if (EQ (car, Qplus) || EQ (car, Qminus))
18803 {
18804 int first = 1;
18805 double px;
18806
18807 pixels = 0;
18808 while (CONSP (cdr))
18809 {
18810 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18811 font, width_p, align_to))
18812 return 0;
18813 if (first)
18814 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18815 else
18816 pixels += px;
18817 cdr = XCDR (cdr);
18818 }
18819 if (EQ (car, Qminus))
18820 pixels = -pixels;
18821 return OK_PIXELS (pixels);
18822 }
18823
18824 car = Fbuffer_local_value (car, it->w->buffer);
18825 }
18826
18827 if (INTEGERP (car) || FLOATP (car))
18828 {
18829 double fact;
18830 pixels = XFLOATINT (car);
18831 if (NILP (cdr))
18832 return OK_PIXELS (pixels);
18833 if (calc_pixel_width_or_height (&fact, it, cdr,
18834 font, width_p, align_to))
18835 return OK_PIXELS (pixels * fact);
18836 return 0;
18837 }
18838
18839 return 0;
18840 }
18841
18842 return 0;
18843 }
18844
18845 \f
18846 /***********************************************************************
18847 Glyph Display
18848 ***********************************************************************/
18849
18850 #ifdef HAVE_WINDOW_SYSTEM
18851
18852 #if GLYPH_DEBUG
18853
18854 void
18855 dump_glyph_string (s)
18856 struct glyph_string *s;
18857 {
18858 fprintf (stderr, "glyph string\n");
18859 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18860 s->x, s->y, s->width, s->height);
18861 fprintf (stderr, " ybase = %d\n", s->ybase);
18862 fprintf (stderr, " hl = %d\n", s->hl);
18863 fprintf (stderr, " left overhang = %d, right = %d\n",
18864 s->left_overhang, s->right_overhang);
18865 fprintf (stderr, " nchars = %d\n", s->nchars);
18866 fprintf (stderr, " extends to end of line = %d\n",
18867 s->extends_to_end_of_line_p);
18868 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18869 fprintf (stderr, " bg width = %d\n", s->background_width);
18870 }
18871
18872 #endif /* GLYPH_DEBUG */
18873
18874 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18875 of XChar2b structures for S; it can't be allocated in
18876 init_glyph_string because it must be allocated via `alloca'. W
18877 is the window on which S is drawn. ROW and AREA are the glyph row
18878 and area within the row from which S is constructed. START is the
18879 index of the first glyph structure covered by S. HL is a
18880 face-override for drawing S. */
18881
18882 #ifdef HAVE_NTGUI
18883 #define OPTIONAL_HDC(hdc) hdc,
18884 #define DECLARE_HDC(hdc) HDC hdc;
18885 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18886 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18887 #endif
18888
18889 #ifndef OPTIONAL_HDC
18890 #define OPTIONAL_HDC(hdc)
18891 #define DECLARE_HDC(hdc)
18892 #define ALLOCATE_HDC(hdc, f)
18893 #define RELEASE_HDC(hdc, f)
18894 #endif
18895
18896 static void
18897 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18898 struct glyph_string *s;
18899 DECLARE_HDC (hdc)
18900 XChar2b *char2b;
18901 struct window *w;
18902 struct glyph_row *row;
18903 enum glyph_row_area area;
18904 int start;
18905 enum draw_glyphs_face hl;
18906 {
18907 bzero (s, sizeof *s);
18908 s->w = w;
18909 s->f = XFRAME (w->frame);
18910 #ifdef HAVE_NTGUI
18911 s->hdc = hdc;
18912 #endif
18913 s->display = FRAME_X_DISPLAY (s->f);
18914 s->window = FRAME_X_WINDOW (s->f);
18915 s->char2b = char2b;
18916 s->hl = hl;
18917 s->row = row;
18918 s->area = area;
18919 s->first_glyph = row->glyphs[area] + start;
18920 s->height = row->height;
18921 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18922
18923 /* Display the internal border below the tool-bar window. */
18924 if (WINDOWP (s->f->tool_bar_window)
18925 && s->w == XWINDOW (s->f->tool_bar_window))
18926 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18927
18928 s->ybase = s->y + row->ascent;
18929 }
18930
18931
18932 /* Append the list of glyph strings with head H and tail T to the list
18933 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18934
18935 static INLINE void
18936 append_glyph_string_lists (head, tail, h, t)
18937 struct glyph_string **head, **tail;
18938 struct glyph_string *h, *t;
18939 {
18940 if (h)
18941 {
18942 if (*head)
18943 (*tail)->next = h;
18944 else
18945 *head = h;
18946 h->prev = *tail;
18947 *tail = t;
18948 }
18949 }
18950
18951
18952 /* Prepend the list of glyph strings with head H and tail T to the
18953 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18954 result. */
18955
18956 static INLINE void
18957 prepend_glyph_string_lists (head, tail, h, t)
18958 struct glyph_string **head, **tail;
18959 struct glyph_string *h, *t;
18960 {
18961 if (h)
18962 {
18963 if (*head)
18964 (*head)->prev = t;
18965 else
18966 *tail = t;
18967 t->next = *head;
18968 *head = h;
18969 }
18970 }
18971
18972
18973 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18974 Set *HEAD and *TAIL to the resulting list. */
18975
18976 static INLINE void
18977 append_glyph_string (head, tail, s)
18978 struct glyph_string **head, **tail;
18979 struct glyph_string *s;
18980 {
18981 s->next = s->prev = NULL;
18982 append_glyph_string_lists (head, tail, s, s);
18983 }
18984
18985
18986 /* Get face and two-byte form of character C in face FACE_ID on frame
18987 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18988 means we want to display multibyte text. DISPLAY_P non-zero means
18989 make sure that X resources for the face returned are allocated.
18990 Value is a pointer to a realized face that is ready for display if
18991 DISPLAY_P is non-zero. */
18992
18993 static INLINE struct face *
18994 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18995 struct frame *f;
18996 int c, face_id;
18997 XChar2b *char2b;
18998 int multibyte_p, display_p;
18999 {
19000 struct face *face = FACE_FROM_ID (f, face_id);
19001
19002 #ifdef USE_FONT_BACKEND
19003 if (enable_font_backend)
19004 {
19005 struct font *font = (struct font *) face->font_info;
19006
19007 if (font)
19008 {
19009 unsigned code = font->driver->encode_char (font, c);
19010
19011 if (code != FONT_INVALID_CODE)
19012 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19013 else
19014 STORE_XCHAR2B (char2b, 0, 0);
19015 }
19016 }
19017 else
19018 #endif /* USE_FONT_BACKEND */
19019 if (!multibyte_p)
19020 {
19021 /* Unibyte case. We don't have to encode, but we have to make
19022 sure to use a face suitable for unibyte. */
19023 STORE_XCHAR2B (char2b, 0, c);
19024 face_id = FACE_FOR_CHAR (f, face, c, -1, Qnil);
19025 face = FACE_FROM_ID (f, face_id);
19026 }
19027 else if (c < 128)
19028 {
19029 /* Case of ASCII in a face known to fit ASCII. */
19030 STORE_XCHAR2B (char2b, 0, c);
19031 }
19032 else if (face->font != NULL)
19033 {
19034 struct font_info *font_info
19035 = FONT_INFO_FROM_ID (f, face->font_info_id);
19036 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19037 unsigned code = ENCODE_CHAR (charset, c);
19038
19039 if (CHARSET_DIMENSION (charset) == 1)
19040 STORE_XCHAR2B (char2b, 0, code);
19041 else
19042 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19043 /* Maybe encode the character in *CHAR2B. */
19044 rif->encode_char (c, char2b, font_info, charset, NULL);
19045 }
19046
19047 /* Make sure X resources of the face are allocated. */
19048 #ifdef HAVE_X_WINDOWS
19049 if (display_p)
19050 #endif
19051 {
19052 xassert (face != NULL);
19053 PREPARE_FACE_FOR_DISPLAY (f, face);
19054 }
19055
19056 return face;
19057 }
19058
19059
19060 /* Get face and two-byte form of character glyph GLYPH on frame F.
19061 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19062 a pointer to a realized face that is ready for display. */
19063
19064 static INLINE struct face *
19065 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19066 struct frame *f;
19067 struct glyph *glyph;
19068 XChar2b *char2b;
19069 int *two_byte_p;
19070 {
19071 struct face *face;
19072
19073 xassert (glyph->type == CHAR_GLYPH);
19074 face = FACE_FROM_ID (f, glyph->face_id);
19075
19076 if (two_byte_p)
19077 *two_byte_p = 0;
19078
19079 #ifdef USE_FONT_BACKEND
19080 if (enable_font_backend)
19081 {
19082 struct font *font = (struct font *) face->font_info;
19083
19084 if (font)
19085 {
19086 unsigned code = font->driver->encode_char (font, glyph->u.ch);
19087
19088 if (code != FONT_INVALID_CODE)
19089 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19090 else
19091 STORE_XCHAR2B (char2b, 0, code);
19092 }
19093 }
19094 else
19095 #endif /* USE_FONT_BACKEND */
19096 if (!glyph->multibyte_p)
19097 {
19098 /* Unibyte case. We don't have to encode, but we have to make
19099 sure to use a face suitable for unibyte. */
19100 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19101 }
19102 else if (glyph->u.ch < 128)
19103 {
19104 /* Case of ASCII in a face known to fit ASCII. */
19105 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
19106 }
19107 else
19108 {
19109 struct font_info *font_info
19110 = FONT_INFO_FROM_ID (f, face->font_info_id);
19111 if (font_info)
19112 {
19113 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
19114 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
19115
19116 if (CHARSET_DIMENSION (charset) == 1)
19117 STORE_XCHAR2B (char2b, 0, code);
19118 else
19119 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19120
19121 /* Maybe encode the character in *CHAR2B. */
19122 if (CHARSET_ID (charset) != charset_ascii)
19123 {
19124 glyph->font_type
19125 = rif->encode_char (glyph->u.ch, char2b, font_info, charset,
19126 two_byte_p);
19127 }
19128 }
19129 }
19130
19131 /* Make sure X resources of the face are allocated. */
19132 xassert (face != NULL);
19133 PREPARE_FACE_FOR_DISPLAY (f, face);
19134 return face;
19135 }
19136
19137
19138 /* Fill glyph string S with composition components specified by S->cmp.
19139
19140 BASE_FACE is the base face of the composition.
19141 S->gidx is the index of the first component for S.
19142
19143 OVERLAPS non-zero means S should draw the foreground only, and use
19144 its physical height for clipping. See also draw_glyphs.
19145
19146 Value is the index of a component not in S. */
19147
19148 static int
19149 fill_composite_glyph_string (s, base_face, overlaps)
19150 struct glyph_string *s;
19151 struct face *base_face;
19152 int overlaps;
19153 {
19154 int i;
19155
19156 xassert (s);
19157
19158 s->for_overlaps = overlaps;
19159
19160 #ifdef USE_FONT_BACKEND
19161 if (enable_font_backend && s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19162 {
19163 Lisp_Object gstring
19164 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19165 s->cmp->hash_index * 2);
19166
19167 s->face = base_face;
19168 s->font_info = s->cmp->font;
19169 s->font = s->font_info->font;
19170 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19171 {
19172 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19173 unsigned code;
19174 XChar2b * store_pos;
19175 if (NILP (LGLYPH_FROM (g)))
19176 break;
19177 code = XUINT (LGLYPH_CODE (g));
19178 store_pos = s->char2b + i;
19179 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19180 }
19181 s->width = s->cmp->pixel_width;
19182 }
19183 else
19184 #endif /* USE_FONT_BACKEND */
19185 {
19186 /* For all glyphs of this composition, starting at the offset
19187 S->gidx, until we reach the end of the definition or encounter a
19188 glyph that requires the different face, add it to S. */
19189 struct face *face;
19190
19191 s->face = NULL;
19192 s->font = NULL;
19193 s->font_info = NULL;
19194 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19195 {
19196 int c = COMPOSITION_GLYPH (s->cmp, i);
19197
19198 if (c != '\t')
19199 {
19200 int face_id = FACE_FOR_CHAR (s->f, base_face, c, -1, Qnil);
19201
19202 face = get_char_face_and_encoding (s->f, c, face_id,
19203 s->char2b + i, 1, 1);
19204 if (face)
19205 {
19206 if (! s->face)
19207 {
19208 s->face = face;
19209 s->font = s->face->font;
19210 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19211 }
19212 else if (s->face != face)
19213 break;
19214 }
19215 }
19216 ++s->nchars;
19217 }
19218
19219 /* All glyph strings for the same composition has the same width,
19220 i.e. the width set for the first component of the composition. */
19221 s->width = s->first_glyph->pixel_width;
19222 }
19223
19224 /* If the specified font could not be loaded, use the frame's
19225 default font, but record the fact that we couldn't load it in
19226 the glyph string so that we can draw rectangles for the
19227 characters of the glyph string. */
19228 if (s->font == NULL)
19229 {
19230 s->font_not_found_p = 1;
19231 s->font = FRAME_FONT (s->f);
19232 }
19233
19234 /* Adjust base line for subscript/superscript text. */
19235 s->ybase += s->first_glyph->voffset;
19236
19237 /* This glyph string must always be drawn with 16-bit functions. */
19238 s->two_byte_p = 1;
19239
19240 return s->gidx + s->nchars;
19241 }
19242
19243
19244 /* Fill glyph string S from a sequence of character glyphs.
19245
19246 FACE_ID is the face id of the string. START is the index of the
19247 first glyph to consider, END is the index of the last + 1.
19248 OVERLAPS non-zero means S should draw the foreground only, and use
19249 its physical height for clipping. See also draw_glyphs.
19250
19251 Value is the index of the first glyph not in S. */
19252
19253 static int
19254 fill_glyph_string (s, face_id, start, end, overlaps)
19255 struct glyph_string *s;
19256 int face_id;
19257 int start, end, overlaps;
19258 {
19259 struct glyph *glyph, *last;
19260 int voffset;
19261 int glyph_not_available_p;
19262
19263 xassert (s->f == XFRAME (s->w->frame));
19264 xassert (s->nchars == 0);
19265 xassert (start >= 0 && end > start);
19266
19267 s->for_overlaps = overlaps,
19268 glyph = s->row->glyphs[s->area] + start;
19269 last = s->row->glyphs[s->area] + end;
19270 voffset = glyph->voffset;
19271
19272 glyph_not_available_p = glyph->glyph_not_available_p;
19273
19274 while (glyph < last
19275 && glyph->type == CHAR_GLYPH
19276 && glyph->voffset == voffset
19277 /* Same face id implies same font, nowadays. */
19278 && glyph->face_id == face_id
19279 && glyph->glyph_not_available_p == glyph_not_available_p)
19280 {
19281 int two_byte_p;
19282
19283 s->face = get_glyph_face_and_encoding (s->f, glyph,
19284 s->char2b + s->nchars,
19285 &two_byte_p);
19286 s->two_byte_p = two_byte_p;
19287 ++s->nchars;
19288 xassert (s->nchars <= end - start);
19289 s->width += glyph->pixel_width;
19290 ++glyph;
19291 }
19292
19293 s->font = s->face->font;
19294 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19295
19296 /* If the specified font could not be loaded, use the frame's font,
19297 but record the fact that we couldn't load it in
19298 S->font_not_found_p so that we can draw rectangles for the
19299 characters of the glyph string. */
19300 if (s->font == NULL || glyph_not_available_p)
19301 {
19302 s->font_not_found_p = 1;
19303 s->font = FRAME_FONT (s->f);
19304 }
19305
19306 /* Adjust base line for subscript/superscript text. */
19307 s->ybase += voffset;
19308
19309 xassert (s->face && s->face->gc);
19310 return glyph - s->row->glyphs[s->area];
19311 }
19312
19313
19314 /* Fill glyph string S from image glyph S->first_glyph. */
19315
19316 static void
19317 fill_image_glyph_string (s)
19318 struct glyph_string *s;
19319 {
19320 xassert (s->first_glyph->type == IMAGE_GLYPH);
19321 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19322 xassert (s->img);
19323 s->slice = s->first_glyph->slice;
19324 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19325 s->font = s->face->font;
19326 s->width = s->first_glyph->pixel_width;
19327
19328 /* Adjust base line for subscript/superscript text. */
19329 s->ybase += s->first_glyph->voffset;
19330 }
19331
19332
19333 /* Fill glyph string S from a sequence of stretch glyphs.
19334
19335 ROW is the glyph row in which the glyphs are found, AREA is the
19336 area within the row. START is the index of the first glyph to
19337 consider, END is the index of the last + 1.
19338
19339 Value is the index of the first glyph not in S. */
19340
19341 static int
19342 fill_stretch_glyph_string (s, row, area, start, end)
19343 struct glyph_string *s;
19344 struct glyph_row *row;
19345 enum glyph_row_area area;
19346 int start, end;
19347 {
19348 struct glyph *glyph, *last;
19349 int voffset, face_id;
19350
19351 xassert (s->first_glyph->type == STRETCH_GLYPH);
19352
19353 glyph = s->row->glyphs[s->area] + start;
19354 last = s->row->glyphs[s->area] + end;
19355 face_id = glyph->face_id;
19356 s->face = FACE_FROM_ID (s->f, face_id);
19357 s->font = s->face->font;
19358 s->font_info = FONT_INFO_FROM_FACE (s->f, s->face);
19359 s->width = glyph->pixel_width;
19360 s->nchars = 1;
19361 voffset = glyph->voffset;
19362
19363 for (++glyph;
19364 (glyph < last
19365 && glyph->type == STRETCH_GLYPH
19366 && glyph->voffset == voffset
19367 && glyph->face_id == face_id);
19368 ++glyph)
19369 s->width += glyph->pixel_width;
19370
19371 /* Adjust base line for subscript/superscript text. */
19372 s->ybase += voffset;
19373
19374 /* The case that face->gc == 0 is handled when drawing the glyph
19375 string by calling PREPARE_FACE_FOR_DISPLAY. */
19376 xassert (s->face);
19377 return glyph - s->row->glyphs[s->area];
19378 }
19379
19380 static XCharStruct *
19381 get_per_char_metric (font, font_info, char2b, font_type)
19382 XFontStruct *font;
19383 struct font_info *font_info;
19384 XChar2b *char2b;
19385 int font_type;
19386 {
19387 #ifdef USE_FONT_BACKEND
19388 if (enable_font_backend)
19389 {
19390 static XCharStruct pcm_value;
19391 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19392 struct font *fontp;
19393 struct font_metrics metrics;
19394
19395 if (! font_info || code == FONT_INVALID_CODE)
19396 return NULL;
19397 fontp = (struct font *) font_info;
19398 fontp->driver->text_extents (fontp, &code, 1, &metrics);
19399 pcm_value.lbearing = metrics.lbearing;
19400 pcm_value.rbearing = metrics.rbearing;
19401 pcm_value.ascent = metrics.ascent;
19402 pcm_value.descent = metrics.descent;
19403 pcm_value.width = metrics.width;
19404 return &pcm_value;
19405 }
19406 #endif /* USE_FONT_BACKEND */
19407 return rif->per_char_metric (font, char2b, font_type);
19408 }
19409
19410 /* EXPORT for RIF:
19411 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19412 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19413 assumed to be zero. */
19414
19415 void
19416 x_get_glyph_overhangs (glyph, f, left, right)
19417 struct glyph *glyph;
19418 struct frame *f;
19419 int *left, *right;
19420 {
19421 *left = *right = 0;
19422
19423 if (glyph->type == CHAR_GLYPH)
19424 {
19425 XFontStruct *font;
19426 struct face *face;
19427 struct font_info *font_info;
19428 XChar2b char2b;
19429 XCharStruct *pcm;
19430
19431 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19432 font = face->font;
19433 font_info = FONT_INFO_FROM_FACE (f, face);
19434 if (font /* ++KFS: Should this be font_info ? */
19435 && (pcm = get_per_char_metric (font, font_info, &char2b, glyph->font_type)))
19436 {
19437 if (pcm->rbearing > pcm->width)
19438 *right = pcm->rbearing - pcm->width;
19439 if (pcm->lbearing < 0)
19440 *left = -pcm->lbearing;
19441 }
19442 }
19443 else if (glyph->type == COMPOSITE_GLYPH)
19444 {
19445 struct composition *cmp = composition_table[glyph->u.cmp_id];
19446
19447 *right = cmp->rbearing - cmp->pixel_width;
19448 *left = - cmp->lbearing;
19449 }
19450 }
19451
19452
19453 /* Return the index of the first glyph preceding glyph string S that
19454 is overwritten by S because of S's left overhang. Value is -1
19455 if no glyphs are overwritten. */
19456
19457 static int
19458 left_overwritten (s)
19459 struct glyph_string *s;
19460 {
19461 int k;
19462
19463 if (s->left_overhang)
19464 {
19465 int x = 0, i;
19466 struct glyph *glyphs = s->row->glyphs[s->area];
19467 int first = s->first_glyph - glyphs;
19468
19469 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19470 x -= glyphs[i].pixel_width;
19471
19472 k = i + 1;
19473 }
19474 else
19475 k = -1;
19476
19477 return k;
19478 }
19479
19480
19481 /* Return the index of the first glyph preceding glyph string S that
19482 is overwriting S because of its right overhang. Value is -1 if no
19483 glyph in front of S overwrites S. */
19484
19485 static int
19486 left_overwriting (s)
19487 struct glyph_string *s;
19488 {
19489 int i, k, x;
19490 struct glyph *glyphs = s->row->glyphs[s->area];
19491 int first = s->first_glyph - glyphs;
19492
19493 k = -1;
19494 x = 0;
19495 for (i = first - 1; i >= 0; --i)
19496 {
19497 int left, right;
19498 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19499 if (x + right > 0)
19500 k = i;
19501 x -= glyphs[i].pixel_width;
19502 }
19503
19504 return k;
19505 }
19506
19507
19508 /* Return the index of the last glyph following glyph string S that is
19509 not overwritten by S because of S's right overhang. Value is -1 if
19510 no such glyph is found. */
19511
19512 static int
19513 right_overwritten (s)
19514 struct glyph_string *s;
19515 {
19516 int k = -1;
19517
19518 if (s->right_overhang)
19519 {
19520 int x = 0, i;
19521 struct glyph *glyphs = s->row->glyphs[s->area];
19522 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19523 int end = s->row->used[s->area];
19524
19525 for (i = first; i < end && s->right_overhang > x; ++i)
19526 x += glyphs[i].pixel_width;
19527
19528 k = i;
19529 }
19530
19531 return k;
19532 }
19533
19534
19535 /* Return the index of the last glyph following glyph string S that
19536 overwrites S because of its left overhang. Value is negative
19537 if no such glyph is found. */
19538
19539 static int
19540 right_overwriting (s)
19541 struct glyph_string *s;
19542 {
19543 int i, k, x;
19544 int end = s->row->used[s->area];
19545 struct glyph *glyphs = s->row->glyphs[s->area];
19546 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19547
19548 k = -1;
19549 x = 0;
19550 for (i = first; i < end; ++i)
19551 {
19552 int left, right;
19553 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19554 if (x - left < 0)
19555 k = i;
19556 x += glyphs[i].pixel_width;
19557 }
19558
19559 return k;
19560 }
19561
19562
19563 /* Set background width of glyph string S. START is the index of the
19564 first glyph following S. LAST_X is the right-most x-position + 1
19565 in the drawing area. */
19566
19567 static INLINE void
19568 set_glyph_string_background_width (s, start, last_x)
19569 struct glyph_string *s;
19570 int start;
19571 int last_x;
19572 {
19573 /* If the face of this glyph string has to be drawn to the end of
19574 the drawing area, set S->extends_to_end_of_line_p. */
19575
19576 if (start == s->row->used[s->area]
19577 && s->area == TEXT_AREA
19578 && ((s->row->fill_line_p
19579 && (s->hl == DRAW_NORMAL_TEXT
19580 || s->hl == DRAW_IMAGE_RAISED
19581 || s->hl == DRAW_IMAGE_SUNKEN))
19582 || s->hl == DRAW_MOUSE_FACE))
19583 s->extends_to_end_of_line_p = 1;
19584
19585 /* If S extends its face to the end of the line, set its
19586 background_width to the distance to the right edge of the drawing
19587 area. */
19588 if (s->extends_to_end_of_line_p)
19589 s->background_width = last_x - s->x + 1;
19590 else
19591 s->background_width = s->width;
19592 }
19593
19594
19595 /* Compute overhangs and x-positions for glyph string S and its
19596 predecessors, or successors. X is the starting x-position for S.
19597 BACKWARD_P non-zero means process predecessors. */
19598
19599 static void
19600 compute_overhangs_and_x (s, x, backward_p)
19601 struct glyph_string *s;
19602 int x;
19603 int backward_p;
19604 {
19605 if (backward_p)
19606 {
19607 while (s)
19608 {
19609 if (rif->compute_glyph_string_overhangs)
19610 rif->compute_glyph_string_overhangs (s);
19611 x -= s->width;
19612 s->x = x;
19613 s = s->prev;
19614 }
19615 }
19616 else
19617 {
19618 while (s)
19619 {
19620 if (rif->compute_glyph_string_overhangs)
19621 rif->compute_glyph_string_overhangs (s);
19622 s->x = x;
19623 x += s->width;
19624 s = s->next;
19625 }
19626 }
19627 }
19628
19629
19630
19631 /* The following macros are only called from draw_glyphs below.
19632 They reference the following parameters of that function directly:
19633 `w', `row', `area', and `overlap_p'
19634 as well as the following local variables:
19635 `s', `f', and `hdc' (in W32) */
19636
19637 #ifdef HAVE_NTGUI
19638 /* On W32, silently add local `hdc' variable to argument list of
19639 init_glyph_string. */
19640 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19641 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19642 #else
19643 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19644 init_glyph_string (s, char2b, w, row, area, start, hl)
19645 #endif
19646
19647 /* Add a glyph string for a stretch glyph to the list of strings
19648 between HEAD and TAIL. START is the index of the stretch glyph in
19649 row area AREA of glyph row ROW. END is the index of the last glyph
19650 in that glyph row area. X is the current output position assigned
19651 to the new glyph string constructed. HL overrides that face of the
19652 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19653 is the right-most x-position of the drawing area. */
19654
19655 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19656 and below -- keep them on one line. */
19657 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19658 do \
19659 { \
19660 s = (struct glyph_string *) alloca (sizeof *s); \
19661 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19662 START = fill_stretch_glyph_string (s, row, area, START, END); \
19663 append_glyph_string (&HEAD, &TAIL, s); \
19664 s->x = (X); \
19665 } \
19666 while (0)
19667
19668
19669 /* Add a glyph string for an image glyph to the list of strings
19670 between HEAD and TAIL. START is the index of the image glyph in
19671 row area AREA of glyph row ROW. END is the index of the last glyph
19672 in that glyph row area. X is the current output position assigned
19673 to the new glyph string constructed. HL overrides that face of the
19674 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19675 is the right-most x-position of the drawing area. */
19676
19677 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19678 do \
19679 { \
19680 s = (struct glyph_string *) alloca (sizeof *s); \
19681 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19682 fill_image_glyph_string (s); \
19683 append_glyph_string (&HEAD, &TAIL, s); \
19684 ++START; \
19685 s->x = (X); \
19686 } \
19687 while (0)
19688
19689
19690 /* Add a glyph string for a sequence of character glyphs to the list
19691 of strings between HEAD and TAIL. START is the index of the first
19692 glyph in row area AREA of glyph row ROW that is part of the new
19693 glyph string. END is the index of the last glyph in that glyph row
19694 area. X is the current output position assigned to the new glyph
19695 string constructed. HL overrides that face of the glyph; e.g. it
19696 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19697 right-most x-position of the drawing area. */
19698
19699 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19700 do \
19701 { \
19702 int face_id; \
19703 XChar2b *char2b; \
19704 \
19705 face_id = (row)->glyphs[area][START].face_id; \
19706 \
19707 s = (struct glyph_string *) alloca (sizeof *s); \
19708 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19709 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19710 append_glyph_string (&HEAD, &TAIL, s); \
19711 s->x = (X); \
19712 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19713 } \
19714 while (0)
19715
19716
19717 /* Add a glyph string for a composite sequence to the list of strings
19718 between HEAD and TAIL. START is the index of the first glyph in
19719 row area AREA of glyph row ROW that is part of the new glyph
19720 string. END is the index of the last glyph in that glyph row area.
19721 X is the current output position assigned to the new glyph string
19722 constructed. HL overrides that face of the glyph; e.g. it is
19723 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19724 x-position of the drawing area. */
19725
19726 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19727 do { \
19728 int face_id = (row)->glyphs[area][START].face_id; \
19729 struct face *base_face = FACE_FROM_ID (f, face_id); \
19730 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19731 struct composition *cmp = composition_table[cmp_id]; \
19732 XChar2b *char2b; \
19733 struct glyph_string *first_s; \
19734 int n; \
19735 \
19736 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
19737 base_face = base_face->ascii_face; \
19738 \
19739 /* Make glyph_strings for each glyph sequence that is drawable by \
19740 the same face, and append them to HEAD/TAIL. */ \
19741 for (n = 0; n < cmp->glyph_len;) \
19742 { \
19743 s = (struct glyph_string *) alloca (sizeof *s); \
19744 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19745 append_glyph_string (&(HEAD), &(TAIL), s); \
19746 s->cmp = cmp; \
19747 s->gidx = n; \
19748 s->x = (X); \
19749 if (n == 0) \
19750 first_s = s; \
19751 n = fill_composite_glyph_string (s, base_face, overlaps); \
19752 } \
19753 \
19754 ++START; \
19755 s = first_s; \
19756 } while (0)
19757
19758
19759 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19760 of AREA of glyph row ROW on window W between indices START and END.
19761 HL overrides the face for drawing glyph strings, e.g. it is
19762 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19763 x-positions of the drawing area.
19764
19765 This is an ugly monster macro construct because we must use alloca
19766 to allocate glyph strings (because draw_glyphs can be called
19767 asynchronously). */
19768
19769 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19770 do \
19771 { \
19772 HEAD = TAIL = NULL; \
19773 while (START < END) \
19774 { \
19775 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19776 switch (first_glyph->type) \
19777 { \
19778 case CHAR_GLYPH: \
19779 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19780 HL, X, LAST_X); \
19781 break; \
19782 \
19783 case COMPOSITE_GLYPH: \
19784 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19785 HL, X, LAST_X); \
19786 break; \
19787 \
19788 case STRETCH_GLYPH: \
19789 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19790 HL, X, LAST_X); \
19791 break; \
19792 \
19793 case IMAGE_GLYPH: \
19794 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19795 HL, X, LAST_X); \
19796 break; \
19797 \
19798 default: \
19799 abort (); \
19800 } \
19801 \
19802 if (s) \
19803 { \
19804 set_glyph_string_background_width (s, START, LAST_X); \
19805 (X) += s->width; \
19806 } \
19807 } \
19808 } \
19809 while (0)
19810
19811
19812 /* Draw glyphs between START and END in AREA of ROW on window W,
19813 starting at x-position X. X is relative to AREA in W. HL is a
19814 face-override with the following meaning:
19815
19816 DRAW_NORMAL_TEXT draw normally
19817 DRAW_CURSOR draw in cursor face
19818 DRAW_MOUSE_FACE draw in mouse face.
19819 DRAW_INVERSE_VIDEO draw in mode line face
19820 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19821 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19822
19823 If OVERLAPS is non-zero, draw only the foreground of characters and
19824 clip to the physical height of ROW. Non-zero value also defines
19825 the overlapping part to be drawn:
19826
19827 OVERLAPS_PRED overlap with preceding rows
19828 OVERLAPS_SUCC overlap with succeeding rows
19829 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19830 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19831
19832 Value is the x-position reached, relative to AREA of W. */
19833
19834 static int
19835 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19836 struct window *w;
19837 int x;
19838 struct glyph_row *row;
19839 enum glyph_row_area area;
19840 EMACS_INT start, end;
19841 enum draw_glyphs_face hl;
19842 int overlaps;
19843 {
19844 struct glyph_string *head, *tail;
19845 struct glyph_string *s;
19846 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19847 int last_x, area_width;
19848 int x_reached;
19849 int i, j;
19850 struct frame *f = XFRAME (WINDOW_FRAME (w));
19851 DECLARE_HDC (hdc);
19852
19853 ALLOCATE_HDC (hdc, f);
19854
19855 /* Let's rather be paranoid than getting a SEGV. */
19856 end = min (end, row->used[area]);
19857 start = max (0, start);
19858 start = min (end, start);
19859
19860 /* Translate X to frame coordinates. Set last_x to the right
19861 end of the drawing area. */
19862 if (row->full_width_p)
19863 {
19864 /* X is relative to the left edge of W, without scroll bars
19865 or fringes. */
19866 x += WINDOW_LEFT_EDGE_X (w);
19867 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19868 }
19869 else
19870 {
19871 int area_left = window_box_left (w, area);
19872 x += area_left;
19873 area_width = window_box_width (w, area);
19874 last_x = area_left + area_width;
19875 }
19876
19877 /* Build a doubly-linked list of glyph_string structures between
19878 head and tail from what we have to draw. Note that the macro
19879 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19880 the reason we use a separate variable `i'. */
19881 i = start;
19882 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19883 if (tail)
19884 x_reached = tail->x + tail->background_width;
19885 else
19886 x_reached = x;
19887
19888 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19889 the row, redraw some glyphs in front or following the glyph
19890 strings built above. */
19891 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19892 {
19893 int dummy_x = 0;
19894 struct glyph_string *h, *t;
19895
19896 /* Compute overhangs for all glyph strings. */
19897 if (rif->compute_glyph_string_overhangs)
19898 for (s = head; s; s = s->next)
19899 rif->compute_glyph_string_overhangs (s);
19900
19901 /* Prepend glyph strings for glyphs in front of the first glyph
19902 string that are overwritten because of the first glyph
19903 string's left overhang. The background of all strings
19904 prepended must be drawn because the first glyph string
19905 draws over it. */
19906 i = left_overwritten (head);
19907 if (i >= 0)
19908 {
19909 j = i;
19910 BUILD_GLYPH_STRINGS (j, start, h, t,
19911 DRAW_NORMAL_TEXT, dummy_x, last_x);
19912 start = i;
19913 compute_overhangs_and_x (t, head->x, 1);
19914 prepend_glyph_string_lists (&head, &tail, h, t);
19915 clip_head = head;
19916 }
19917
19918 /* Prepend glyph strings for glyphs in front of the first glyph
19919 string that overwrite that glyph string because of their
19920 right overhang. For these strings, only the foreground must
19921 be drawn, because it draws over the glyph string at `head'.
19922 The background must not be drawn because this would overwrite
19923 right overhangs of preceding glyphs for which no glyph
19924 strings exist. */
19925 i = left_overwriting (head);
19926 if (i >= 0)
19927 {
19928 clip_head = head;
19929 BUILD_GLYPH_STRINGS (i, start, h, t,
19930 DRAW_NORMAL_TEXT, dummy_x, last_x);
19931 for (s = h; s; s = s->next)
19932 s->background_filled_p = 1;
19933 compute_overhangs_and_x (t, head->x, 1);
19934 prepend_glyph_string_lists (&head, &tail, h, t);
19935 }
19936
19937 /* Append glyphs strings for glyphs following the last glyph
19938 string tail that are overwritten by tail. The background of
19939 these strings has to be drawn because tail's foreground draws
19940 over it. */
19941 i = right_overwritten (tail);
19942 if (i >= 0)
19943 {
19944 BUILD_GLYPH_STRINGS (end, i, h, t,
19945 DRAW_NORMAL_TEXT, x, last_x);
19946 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19947 append_glyph_string_lists (&head, &tail, h, t);
19948 clip_tail = tail;
19949 }
19950
19951 /* Append glyph strings for glyphs following the last glyph
19952 string tail that overwrite tail. The foreground of such
19953 glyphs has to be drawn because it writes into the background
19954 of tail. The background must not be drawn because it could
19955 paint over the foreground of following glyphs. */
19956 i = right_overwriting (tail);
19957 if (i >= 0)
19958 {
19959 clip_tail = tail;
19960 i++; /* We must include the Ith glyph. */
19961 BUILD_GLYPH_STRINGS (end, i, h, t,
19962 DRAW_NORMAL_TEXT, x, last_x);
19963 for (s = h; s; s = s->next)
19964 s->background_filled_p = 1;
19965 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19966 append_glyph_string_lists (&head, &tail, h, t);
19967 }
19968 if (clip_head || clip_tail)
19969 for (s = head; s; s = s->next)
19970 {
19971 s->clip_head = clip_head;
19972 s->clip_tail = clip_tail;
19973 }
19974 }
19975
19976 /* Draw all strings. */
19977 for (s = head; s; s = s->next)
19978 rif->draw_glyph_string (s);
19979
19980 if (area == TEXT_AREA
19981 && !row->full_width_p
19982 /* When drawing overlapping rows, only the glyph strings'
19983 foreground is drawn, which doesn't erase a cursor
19984 completely. */
19985 && !overlaps)
19986 {
19987 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19988 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19989 : (tail ? tail->x + tail->background_width : x));
19990
19991 int text_left = window_box_left (w, TEXT_AREA);
19992 x0 -= text_left;
19993 x1 -= text_left;
19994
19995 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19996 row->y, MATRIX_ROW_BOTTOM_Y (row));
19997 }
19998
19999 /* Value is the x-position up to which drawn, relative to AREA of W.
20000 This doesn't include parts drawn because of overhangs. */
20001 if (row->full_width_p)
20002 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20003 else
20004 x_reached -= window_box_left (w, area);
20005
20006 RELEASE_HDC (hdc, f);
20007
20008 return x_reached;
20009 }
20010
20011 /* Expand row matrix if too narrow. Don't expand if area
20012 is not present. */
20013
20014 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20015 { \
20016 if (!fonts_changed_p \
20017 && (it->glyph_row->glyphs[area] \
20018 < it->glyph_row->glyphs[area + 1])) \
20019 { \
20020 it->w->ncols_scale_factor++; \
20021 fonts_changed_p = 1; \
20022 } \
20023 }
20024
20025 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20026 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20027
20028 static INLINE void
20029 append_glyph (it)
20030 struct it *it;
20031 {
20032 struct glyph *glyph;
20033 enum glyph_row_area area = it->area;
20034
20035 xassert (it->glyph_row);
20036 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20037
20038 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20039 if (glyph < it->glyph_row->glyphs[area + 1])
20040 {
20041 glyph->charpos = CHARPOS (it->position);
20042 glyph->object = it->object;
20043 glyph->pixel_width = it->pixel_width;
20044 glyph->ascent = it->ascent;
20045 glyph->descent = it->descent;
20046 glyph->voffset = it->voffset;
20047 glyph->type = CHAR_GLYPH;
20048 glyph->multibyte_p = it->multibyte_p;
20049 glyph->left_box_line_p = it->start_of_box_run_p;
20050 glyph->right_box_line_p = it->end_of_box_run_p;
20051 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20052 || it->phys_descent > it->descent);
20053 glyph->padding_p = 0;
20054 glyph->glyph_not_available_p = it->glyph_not_available_p;
20055 glyph->face_id = it->face_id;
20056 glyph->u.ch = it->char_to_display;
20057 glyph->slice = null_glyph_slice;
20058 glyph->font_type = FONT_TYPE_UNKNOWN;
20059 ++it->glyph_row->used[area];
20060 }
20061 else
20062 IT_EXPAND_MATRIX_WIDTH (it, area);
20063 }
20064
20065 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20066 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20067
20068 static INLINE void
20069 append_composite_glyph (it)
20070 struct it *it;
20071 {
20072 struct glyph *glyph;
20073 enum glyph_row_area area = it->area;
20074
20075 xassert (it->glyph_row);
20076
20077 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20078 if (glyph < it->glyph_row->glyphs[area + 1])
20079 {
20080 glyph->charpos = CHARPOS (it->position);
20081 glyph->object = it->object;
20082 glyph->pixel_width = it->pixel_width;
20083 glyph->ascent = it->ascent;
20084 glyph->descent = it->descent;
20085 glyph->voffset = it->voffset;
20086 glyph->type = COMPOSITE_GLYPH;
20087 glyph->multibyte_p = it->multibyte_p;
20088 glyph->left_box_line_p = it->start_of_box_run_p;
20089 glyph->right_box_line_p = it->end_of_box_run_p;
20090 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20091 || it->phys_descent > it->descent);
20092 glyph->padding_p = 0;
20093 glyph->glyph_not_available_p = 0;
20094 glyph->face_id = it->face_id;
20095 glyph->u.cmp_id = it->cmp_id;
20096 glyph->slice = null_glyph_slice;
20097 glyph->font_type = FONT_TYPE_UNKNOWN;
20098 ++it->glyph_row->used[area];
20099 }
20100 else
20101 IT_EXPAND_MATRIX_WIDTH (it, area);
20102 }
20103
20104
20105 /* Change IT->ascent and IT->height according to the setting of
20106 IT->voffset. */
20107
20108 static INLINE void
20109 take_vertical_position_into_account (it)
20110 struct it *it;
20111 {
20112 if (it->voffset)
20113 {
20114 if (it->voffset < 0)
20115 /* Increase the ascent so that we can display the text higher
20116 in the line. */
20117 it->ascent -= it->voffset;
20118 else
20119 /* Increase the descent so that we can display the text lower
20120 in the line. */
20121 it->descent += it->voffset;
20122 }
20123 }
20124
20125
20126 /* Produce glyphs/get display metrics for the image IT is loaded with.
20127 See the description of struct display_iterator in dispextern.h for
20128 an overview of struct display_iterator. */
20129
20130 static void
20131 produce_image_glyph (it)
20132 struct it *it;
20133 {
20134 struct image *img;
20135 struct face *face;
20136 int glyph_ascent, crop;
20137 struct glyph_slice slice;
20138
20139 xassert (it->what == IT_IMAGE);
20140
20141 face = FACE_FROM_ID (it->f, it->face_id);
20142 xassert (face);
20143 /* Make sure X resources of the face is loaded. */
20144 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20145
20146 if (it->image_id < 0)
20147 {
20148 /* Fringe bitmap. */
20149 it->ascent = it->phys_ascent = 0;
20150 it->descent = it->phys_descent = 0;
20151 it->pixel_width = 0;
20152 it->nglyphs = 0;
20153 return;
20154 }
20155
20156 img = IMAGE_FROM_ID (it->f, it->image_id);
20157 xassert (img);
20158 /* Make sure X resources of the image is loaded. */
20159 prepare_image_for_display (it->f, img);
20160
20161 slice.x = slice.y = 0;
20162 slice.width = img->width;
20163 slice.height = img->height;
20164
20165 if (INTEGERP (it->slice.x))
20166 slice.x = XINT (it->slice.x);
20167 else if (FLOATP (it->slice.x))
20168 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20169
20170 if (INTEGERP (it->slice.y))
20171 slice.y = XINT (it->slice.y);
20172 else if (FLOATP (it->slice.y))
20173 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20174
20175 if (INTEGERP (it->slice.width))
20176 slice.width = XINT (it->slice.width);
20177 else if (FLOATP (it->slice.width))
20178 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20179
20180 if (INTEGERP (it->slice.height))
20181 slice.height = XINT (it->slice.height);
20182 else if (FLOATP (it->slice.height))
20183 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20184
20185 if (slice.x >= img->width)
20186 slice.x = img->width;
20187 if (slice.y >= img->height)
20188 slice.y = img->height;
20189 if (slice.x + slice.width >= img->width)
20190 slice.width = img->width - slice.x;
20191 if (slice.y + slice.height > img->height)
20192 slice.height = img->height - slice.y;
20193
20194 if (slice.width == 0 || slice.height == 0)
20195 return;
20196
20197 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20198
20199 it->descent = slice.height - glyph_ascent;
20200 if (slice.y == 0)
20201 it->descent += img->vmargin;
20202 if (slice.y + slice.height == img->height)
20203 it->descent += img->vmargin;
20204 it->phys_descent = it->descent;
20205
20206 it->pixel_width = slice.width;
20207 if (slice.x == 0)
20208 it->pixel_width += img->hmargin;
20209 if (slice.x + slice.width == img->width)
20210 it->pixel_width += img->hmargin;
20211
20212 /* It's quite possible for images to have an ascent greater than
20213 their height, so don't get confused in that case. */
20214 if (it->descent < 0)
20215 it->descent = 0;
20216
20217 #if 0 /* this breaks image tiling */
20218 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20219 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20220 if (face_ascent > it->ascent)
20221 it->ascent = it->phys_ascent = face_ascent;
20222 #endif
20223
20224 it->nglyphs = 1;
20225
20226 if (face->box != FACE_NO_BOX)
20227 {
20228 if (face->box_line_width > 0)
20229 {
20230 if (slice.y == 0)
20231 it->ascent += face->box_line_width;
20232 if (slice.y + slice.height == img->height)
20233 it->descent += face->box_line_width;
20234 }
20235
20236 if (it->start_of_box_run_p && slice.x == 0)
20237 it->pixel_width += abs (face->box_line_width);
20238 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20239 it->pixel_width += abs (face->box_line_width);
20240 }
20241
20242 take_vertical_position_into_account (it);
20243
20244 /* Automatically crop wide image glyphs at right edge so we can
20245 draw the cursor on same display row. */
20246 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20247 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20248 {
20249 it->pixel_width -= crop;
20250 slice.width -= crop;
20251 }
20252
20253 if (it->glyph_row)
20254 {
20255 struct glyph *glyph;
20256 enum glyph_row_area area = it->area;
20257
20258 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20259 if (glyph < it->glyph_row->glyphs[area + 1])
20260 {
20261 glyph->charpos = CHARPOS (it->position);
20262 glyph->object = it->object;
20263 glyph->pixel_width = it->pixel_width;
20264 glyph->ascent = glyph_ascent;
20265 glyph->descent = it->descent;
20266 glyph->voffset = it->voffset;
20267 glyph->type = IMAGE_GLYPH;
20268 glyph->multibyte_p = it->multibyte_p;
20269 glyph->left_box_line_p = it->start_of_box_run_p;
20270 glyph->right_box_line_p = it->end_of_box_run_p;
20271 glyph->overlaps_vertically_p = 0;
20272 glyph->padding_p = 0;
20273 glyph->glyph_not_available_p = 0;
20274 glyph->face_id = it->face_id;
20275 glyph->u.img_id = img->id;
20276 glyph->slice = slice;
20277 glyph->font_type = FONT_TYPE_UNKNOWN;
20278 ++it->glyph_row->used[area];
20279 }
20280 else
20281 IT_EXPAND_MATRIX_WIDTH (it, area);
20282 }
20283 }
20284
20285
20286 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20287 of the glyph, WIDTH and HEIGHT are the width and height of the
20288 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20289
20290 static void
20291 append_stretch_glyph (it, object, width, height, ascent)
20292 struct it *it;
20293 Lisp_Object object;
20294 int width, height;
20295 int ascent;
20296 {
20297 struct glyph *glyph;
20298 enum glyph_row_area area = it->area;
20299
20300 xassert (ascent >= 0 && ascent <= height);
20301
20302 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20303 if (glyph < it->glyph_row->glyphs[area + 1])
20304 {
20305 glyph->charpos = CHARPOS (it->position);
20306 glyph->object = object;
20307 glyph->pixel_width = width;
20308 glyph->ascent = ascent;
20309 glyph->descent = height - ascent;
20310 glyph->voffset = it->voffset;
20311 glyph->type = STRETCH_GLYPH;
20312 glyph->multibyte_p = it->multibyte_p;
20313 glyph->left_box_line_p = it->start_of_box_run_p;
20314 glyph->right_box_line_p = it->end_of_box_run_p;
20315 glyph->overlaps_vertically_p = 0;
20316 glyph->padding_p = 0;
20317 glyph->glyph_not_available_p = 0;
20318 glyph->face_id = it->face_id;
20319 glyph->u.stretch.ascent = ascent;
20320 glyph->u.stretch.height = height;
20321 glyph->slice = null_glyph_slice;
20322 glyph->font_type = FONT_TYPE_UNKNOWN;
20323 ++it->glyph_row->used[area];
20324 }
20325 else
20326 IT_EXPAND_MATRIX_WIDTH (it, area);
20327 }
20328
20329
20330 /* Produce a stretch glyph for iterator IT. IT->object is the value
20331 of the glyph property displayed. The value must be a list
20332 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20333 being recognized:
20334
20335 1. `:width WIDTH' specifies that the space should be WIDTH *
20336 canonical char width wide. WIDTH may be an integer or floating
20337 point number.
20338
20339 2. `:relative-width FACTOR' specifies that the width of the stretch
20340 should be computed from the width of the first character having the
20341 `glyph' property, and should be FACTOR times that width.
20342
20343 3. `:align-to HPOS' specifies that the space should be wide enough
20344 to reach HPOS, a value in canonical character units.
20345
20346 Exactly one of the above pairs must be present.
20347
20348 4. `:height HEIGHT' specifies that the height of the stretch produced
20349 should be HEIGHT, measured in canonical character units.
20350
20351 5. `:relative-height FACTOR' specifies that the height of the
20352 stretch should be FACTOR times the height of the characters having
20353 the glyph property.
20354
20355 Either none or exactly one of 4 or 5 must be present.
20356
20357 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20358 of the stretch should be used for the ascent of the stretch.
20359 ASCENT must be in the range 0 <= ASCENT <= 100. */
20360
20361 static void
20362 produce_stretch_glyph (it)
20363 struct it *it;
20364 {
20365 /* (space :width WIDTH :height HEIGHT ...) */
20366 Lisp_Object prop, plist;
20367 int width = 0, height = 0, align_to = -1;
20368 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20369 int ascent = 0;
20370 double tem;
20371 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20372 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20373
20374 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20375
20376 /* List should start with `space'. */
20377 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20378 plist = XCDR (it->object);
20379
20380 /* Compute the width of the stretch. */
20381 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20382 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20383 {
20384 /* Absolute width `:width WIDTH' specified and valid. */
20385 zero_width_ok_p = 1;
20386 width = (int)tem;
20387 }
20388 else if (prop = Fplist_get (plist, QCrelative_width),
20389 NUMVAL (prop) > 0)
20390 {
20391 /* Relative width `:relative-width FACTOR' specified and valid.
20392 Compute the width of the characters having the `glyph'
20393 property. */
20394 struct it it2;
20395 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20396
20397 it2 = *it;
20398 if (it->multibyte_p)
20399 {
20400 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20401 - IT_BYTEPOS (*it));
20402 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20403 }
20404 else
20405 it2.c = *p, it2.len = 1;
20406
20407 it2.glyph_row = NULL;
20408 it2.what = IT_CHARACTER;
20409 x_produce_glyphs (&it2);
20410 width = NUMVAL (prop) * it2.pixel_width;
20411 }
20412 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20413 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20414 {
20415 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20416 align_to = (align_to < 0
20417 ? 0
20418 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20419 else if (align_to < 0)
20420 align_to = window_box_left_offset (it->w, TEXT_AREA);
20421 width = max (0, (int)tem + align_to - it->current_x);
20422 zero_width_ok_p = 1;
20423 }
20424 else
20425 /* Nothing specified -> width defaults to canonical char width. */
20426 width = FRAME_COLUMN_WIDTH (it->f);
20427
20428 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20429 width = 1;
20430
20431 /* Compute height. */
20432 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20433 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20434 {
20435 height = (int)tem;
20436 zero_height_ok_p = 1;
20437 }
20438 else if (prop = Fplist_get (plist, QCrelative_height),
20439 NUMVAL (prop) > 0)
20440 height = FONT_HEIGHT (font) * NUMVAL (prop);
20441 else
20442 height = FONT_HEIGHT (font);
20443
20444 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20445 height = 1;
20446
20447 /* Compute percentage of height used for ascent. If
20448 `:ascent ASCENT' is present and valid, use that. Otherwise,
20449 derive the ascent from the font in use. */
20450 if (prop = Fplist_get (plist, QCascent),
20451 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20452 ascent = height * NUMVAL (prop) / 100.0;
20453 else if (!NILP (prop)
20454 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20455 ascent = min (max (0, (int)tem), height);
20456 else
20457 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20458
20459 if (width > 0 && !it->truncate_lines_p
20460 && it->current_x + width > it->last_visible_x)
20461 width = it->last_visible_x - it->current_x - 1;
20462
20463 if (width > 0 && height > 0 && it->glyph_row)
20464 {
20465 Lisp_Object object = it->stack[it->sp - 1].string;
20466 if (!STRINGP (object))
20467 object = it->w->buffer;
20468 append_stretch_glyph (it, object, width, height, ascent);
20469 }
20470
20471 it->pixel_width = width;
20472 it->ascent = it->phys_ascent = ascent;
20473 it->descent = it->phys_descent = height - it->ascent;
20474 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20475
20476 take_vertical_position_into_account (it);
20477 }
20478
20479 /* Get line-height and line-spacing property at point.
20480 If line-height has format (HEIGHT TOTAL), return TOTAL
20481 in TOTAL_HEIGHT. */
20482
20483 static Lisp_Object
20484 get_line_height_property (it, prop)
20485 struct it *it;
20486 Lisp_Object prop;
20487 {
20488 Lisp_Object position;
20489
20490 if (STRINGP (it->object))
20491 position = make_number (IT_STRING_CHARPOS (*it));
20492 else if (BUFFERP (it->object))
20493 position = make_number (IT_CHARPOS (*it));
20494 else
20495 return Qnil;
20496
20497 return Fget_char_property (position, prop, it->object);
20498 }
20499
20500 /* Calculate line-height and line-spacing properties.
20501 An integer value specifies explicit pixel value.
20502 A float value specifies relative value to current face height.
20503 A cons (float . face-name) specifies relative value to
20504 height of specified face font.
20505
20506 Returns height in pixels, or nil. */
20507
20508
20509 static Lisp_Object
20510 calc_line_height_property (it, val, font, boff, override)
20511 struct it *it;
20512 Lisp_Object val;
20513 XFontStruct *font;
20514 int boff, override;
20515 {
20516 Lisp_Object face_name = Qnil;
20517 int ascent, descent, height;
20518
20519 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20520 return val;
20521
20522 if (CONSP (val))
20523 {
20524 face_name = XCAR (val);
20525 val = XCDR (val);
20526 if (!NUMBERP (val))
20527 val = make_number (1);
20528 if (NILP (face_name))
20529 {
20530 height = it->ascent + it->descent;
20531 goto scale;
20532 }
20533 }
20534
20535 if (NILP (face_name))
20536 {
20537 font = FRAME_FONT (it->f);
20538 boff = FRAME_BASELINE_OFFSET (it->f);
20539 }
20540 else if (EQ (face_name, Qt))
20541 {
20542 override = 0;
20543 }
20544 else
20545 {
20546 int face_id;
20547 struct face *face;
20548 struct font_info *font_info;
20549
20550 face_id = lookup_named_face (it->f, face_name, 0);
20551 if (face_id < 0)
20552 return make_number (-1);
20553
20554 face = FACE_FROM_ID (it->f, face_id);
20555 font = face->font;
20556 if (font == NULL)
20557 return make_number (-1);
20558
20559 font_info = FONT_INFO_FROM_FACE (it->f, face);
20560 boff = font_info->baseline_offset;
20561 if (font_info->vertical_centering)
20562 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20563 }
20564
20565 ascent = FONT_BASE (font) + boff;
20566 descent = FONT_DESCENT (font) - boff;
20567
20568 if (override)
20569 {
20570 it->override_ascent = ascent;
20571 it->override_descent = descent;
20572 it->override_boff = boff;
20573 }
20574
20575 height = ascent + descent;
20576
20577 scale:
20578 if (FLOATP (val))
20579 height = (int)(XFLOAT_DATA (val) * height);
20580 else if (INTEGERP (val))
20581 height *= XINT (val);
20582
20583 return make_number (height);
20584 }
20585
20586
20587 /* RIF:
20588 Produce glyphs/get display metrics for the display element IT is
20589 loaded with. See the description of struct it in dispextern.h
20590 for an overview of struct it. */
20591
20592 void
20593 x_produce_glyphs (it)
20594 struct it *it;
20595 {
20596 int extra_line_spacing = it->extra_line_spacing;
20597
20598 it->glyph_not_available_p = 0;
20599
20600 if (it->what == IT_CHARACTER)
20601 {
20602 XChar2b char2b;
20603 XFontStruct *font;
20604 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20605 XCharStruct *pcm;
20606 int font_not_found_p;
20607 struct font_info *font_info;
20608 int boff; /* baseline offset */
20609 /* We may change it->multibyte_p upon unibyte<->multibyte
20610 conversion. So, save the current value now and restore it
20611 later.
20612
20613 Note: It seems that we don't have to record multibyte_p in
20614 struct glyph because the character code itself tells if or
20615 not the character is multibyte. Thus, in the future, we must
20616 consider eliminating the field `multibyte_p' in the struct
20617 glyph. */
20618 int saved_multibyte_p = it->multibyte_p;
20619
20620 /* Maybe translate single-byte characters to multibyte, or the
20621 other way. */
20622 it->char_to_display = it->c;
20623 if (!ASCII_BYTE_P (it->c)
20624 && ! it->multibyte_p)
20625 {
20626 if (SINGLE_BYTE_CHAR_P (it->c)
20627 && unibyte_display_via_language_environment)
20628 it->char_to_display = unibyte_char_to_multibyte (it->c);
20629 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
20630 {
20631 it->multibyte_p = 1;
20632 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
20633 -1, Qnil);
20634 face = FACE_FROM_ID (it->f, it->face_id);
20635 }
20636 }
20637
20638 /* Get font to use. Encode IT->char_to_display. */
20639 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20640 &char2b, it->multibyte_p, 0);
20641 font = face->font;
20642
20643 /* When no suitable font found, use the default font. */
20644 font_not_found_p = font == NULL;
20645 if (font_not_found_p)
20646 {
20647 font = FRAME_FONT (it->f);
20648 boff = FRAME_BASELINE_OFFSET (it->f);
20649 font_info = NULL;
20650 }
20651 else
20652 {
20653 font_info = FONT_INFO_FROM_FACE (it->f, face);
20654 boff = font_info->baseline_offset;
20655 if (font_info->vertical_centering)
20656 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20657 }
20658
20659 if (it->char_to_display >= ' '
20660 && (!it->multibyte_p || it->char_to_display < 128))
20661 {
20662 /* Either unibyte or ASCII. */
20663 int stretched_p;
20664
20665 it->nglyphs = 1;
20666
20667 pcm = get_per_char_metric (font, font_info, &char2b,
20668 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20669
20670 if (it->override_ascent >= 0)
20671 {
20672 it->ascent = it->override_ascent;
20673 it->descent = it->override_descent;
20674 boff = it->override_boff;
20675 }
20676 else
20677 {
20678 it->ascent = FONT_BASE (font) + boff;
20679 it->descent = FONT_DESCENT (font) - boff;
20680 }
20681
20682 if (pcm)
20683 {
20684 it->phys_ascent = pcm->ascent + boff;
20685 it->phys_descent = pcm->descent - boff;
20686 it->pixel_width = pcm->width;
20687 }
20688 else
20689 {
20690 it->glyph_not_available_p = 1;
20691 it->phys_ascent = it->ascent;
20692 it->phys_descent = it->descent;
20693 it->pixel_width = FONT_WIDTH (font);
20694 }
20695
20696 if (it->constrain_row_ascent_descent_p)
20697 {
20698 if (it->descent > it->max_descent)
20699 {
20700 it->ascent += it->descent - it->max_descent;
20701 it->descent = it->max_descent;
20702 }
20703 if (it->ascent > it->max_ascent)
20704 {
20705 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20706 it->ascent = it->max_ascent;
20707 }
20708 it->phys_ascent = min (it->phys_ascent, it->ascent);
20709 it->phys_descent = min (it->phys_descent, it->descent);
20710 extra_line_spacing = 0;
20711 }
20712
20713 /* If this is a space inside a region of text with
20714 `space-width' property, change its width. */
20715 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20716 if (stretched_p)
20717 it->pixel_width *= XFLOATINT (it->space_width);
20718
20719 /* If face has a box, add the box thickness to the character
20720 height. If character has a box line to the left and/or
20721 right, add the box line width to the character's width. */
20722 if (face->box != FACE_NO_BOX)
20723 {
20724 int thick = face->box_line_width;
20725
20726 if (thick > 0)
20727 {
20728 it->ascent += thick;
20729 it->descent += thick;
20730 }
20731 else
20732 thick = -thick;
20733
20734 if (it->start_of_box_run_p)
20735 it->pixel_width += thick;
20736 if (it->end_of_box_run_p)
20737 it->pixel_width += thick;
20738 }
20739
20740 /* If face has an overline, add the height of the overline
20741 (1 pixel) and a 1 pixel margin to the character height. */
20742 if (face->overline_p)
20743 it->ascent += overline_margin;
20744
20745 if (it->constrain_row_ascent_descent_p)
20746 {
20747 if (it->ascent > it->max_ascent)
20748 it->ascent = it->max_ascent;
20749 if (it->descent > it->max_descent)
20750 it->descent = it->max_descent;
20751 }
20752
20753 take_vertical_position_into_account (it);
20754
20755 /* If we have to actually produce glyphs, do it. */
20756 if (it->glyph_row)
20757 {
20758 if (stretched_p)
20759 {
20760 /* Translate a space with a `space-width' property
20761 into a stretch glyph. */
20762 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20763 / FONT_HEIGHT (font));
20764 append_stretch_glyph (it, it->object, it->pixel_width,
20765 it->ascent + it->descent, ascent);
20766 }
20767 else
20768 append_glyph (it);
20769
20770 /* If characters with lbearing or rbearing are displayed
20771 in this line, record that fact in a flag of the
20772 glyph row. This is used to optimize X output code. */
20773 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20774 it->glyph_row->contains_overlapping_glyphs_p = 1;
20775 }
20776 }
20777 else if (it->char_to_display == '\n')
20778 {
20779 /* A newline has no width but we need the height of the line.
20780 But if previous part of the line set a height, don't
20781 increase that height */
20782
20783 Lisp_Object height;
20784 Lisp_Object total_height = Qnil;
20785
20786 it->override_ascent = -1;
20787 it->pixel_width = 0;
20788 it->nglyphs = 0;
20789
20790 height = get_line_height_property(it, Qline_height);
20791 /* Split (line-height total-height) list */
20792 if (CONSP (height)
20793 && CONSP (XCDR (height))
20794 && NILP (XCDR (XCDR (height))))
20795 {
20796 total_height = XCAR (XCDR (height));
20797 height = XCAR (height);
20798 }
20799 height = calc_line_height_property(it, height, font, boff, 1);
20800
20801 if (it->override_ascent >= 0)
20802 {
20803 it->ascent = it->override_ascent;
20804 it->descent = it->override_descent;
20805 boff = it->override_boff;
20806 }
20807 else
20808 {
20809 it->ascent = FONT_BASE (font) + boff;
20810 it->descent = FONT_DESCENT (font) - boff;
20811 }
20812
20813 if (EQ (height, Qt))
20814 {
20815 if (it->descent > it->max_descent)
20816 {
20817 it->ascent += it->descent - it->max_descent;
20818 it->descent = it->max_descent;
20819 }
20820 if (it->ascent > it->max_ascent)
20821 {
20822 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20823 it->ascent = it->max_ascent;
20824 }
20825 it->phys_ascent = min (it->phys_ascent, it->ascent);
20826 it->phys_descent = min (it->phys_descent, it->descent);
20827 it->constrain_row_ascent_descent_p = 1;
20828 extra_line_spacing = 0;
20829 }
20830 else
20831 {
20832 Lisp_Object spacing;
20833
20834 it->phys_ascent = it->ascent;
20835 it->phys_descent = it->descent;
20836
20837 if ((it->max_ascent > 0 || it->max_descent > 0)
20838 && face->box != FACE_NO_BOX
20839 && face->box_line_width > 0)
20840 {
20841 it->ascent += face->box_line_width;
20842 it->descent += face->box_line_width;
20843 }
20844 if (!NILP (height)
20845 && XINT (height) > it->ascent + it->descent)
20846 it->ascent = XINT (height) - it->descent;
20847
20848 if (!NILP (total_height))
20849 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20850 else
20851 {
20852 spacing = get_line_height_property(it, Qline_spacing);
20853 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20854 }
20855 if (INTEGERP (spacing))
20856 {
20857 extra_line_spacing = XINT (spacing);
20858 if (!NILP (total_height))
20859 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20860 }
20861 }
20862 }
20863 else if (it->char_to_display == '\t')
20864 {
20865 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20866 int x = it->current_x + it->continuation_lines_width;
20867 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20868
20869 /* If the distance from the current position to the next tab
20870 stop is less than a space character width, use the
20871 tab stop after that. */
20872 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20873 next_tab_x += tab_width;
20874
20875 it->pixel_width = next_tab_x - x;
20876 it->nglyphs = 1;
20877 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20878 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20879
20880 if (it->glyph_row)
20881 {
20882 append_stretch_glyph (it, it->object, it->pixel_width,
20883 it->ascent + it->descent, it->ascent);
20884 }
20885 }
20886 else
20887 {
20888 /* A multi-byte character. Assume that the display width of the
20889 character is the width of the character multiplied by the
20890 width of the font. */
20891
20892 /* If we found a font, this font should give us the right
20893 metrics. If we didn't find a font, use the frame's
20894 default font and calculate the width of the character by
20895 multiplying the width of font by the width of the
20896 character. */
20897
20898 pcm = get_per_char_metric (font, font_info, &char2b,
20899 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20900
20901 if (font_not_found_p || !pcm)
20902 {
20903 int char_width = CHAR_WIDTH (it->char_to_display);
20904
20905 if (char_width == 0)
20906 /* This is a non spacing character. But, as we are
20907 going to display an empty box, the box must occupy
20908 at least one column. */
20909 char_width = 1;
20910 it->glyph_not_available_p = 1;
20911 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
20912 it->phys_ascent = FONT_BASE (font) + boff;
20913 it->phys_descent = FONT_DESCENT (font) - boff;
20914 }
20915 else
20916 {
20917 it->pixel_width = pcm->width;
20918 it->phys_ascent = pcm->ascent + boff;
20919 it->phys_descent = pcm->descent - boff;
20920 if (it->glyph_row
20921 && (pcm->lbearing < 0
20922 || pcm->rbearing > pcm->width))
20923 it->glyph_row->contains_overlapping_glyphs_p = 1;
20924 }
20925 it->nglyphs = 1;
20926 it->ascent = FONT_BASE (font) + boff;
20927 it->descent = FONT_DESCENT (font) - boff;
20928 if (face->box != FACE_NO_BOX)
20929 {
20930 int thick = face->box_line_width;
20931
20932 if (thick > 0)
20933 {
20934 it->ascent += thick;
20935 it->descent += thick;
20936 }
20937 else
20938 thick = - thick;
20939
20940 if (it->start_of_box_run_p)
20941 it->pixel_width += thick;
20942 if (it->end_of_box_run_p)
20943 it->pixel_width += thick;
20944 }
20945
20946 /* If face has an overline, add the height of the overline
20947 (1 pixel) and a 1 pixel margin to the character height. */
20948 if (face->overline_p)
20949 it->ascent += overline_margin;
20950
20951 take_vertical_position_into_account (it);
20952
20953 if (it->glyph_row)
20954 append_glyph (it);
20955 }
20956 it->multibyte_p = saved_multibyte_p;
20957 }
20958 else if (it->what == IT_COMPOSITION)
20959 {
20960 /* Note: A composition is represented as one glyph in the
20961 glyph matrix. There are no padding glyphs.
20962
20963 Important is that pixel_width, ascent, and descent are the
20964 values of what is drawn by draw_glyphs (i.e. the values of
20965 the overall glyphs composed). */
20966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20967 int boff; /* baseline offset */
20968 struct composition *cmp = composition_table[it->cmp_id];
20969 int glyph_len = cmp->glyph_len;
20970 XFontStruct *font = face->font;
20971
20972 it->nglyphs = 1;
20973
20974 #ifdef USE_FONT_BACKEND
20975 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
20976 {
20977 if (! cmp->font || cmp->font != font)
20978 font_prepare_composition (cmp);
20979 }
20980 else
20981 #endif /* USE_FONT_BACKEND */
20982 /* If we have not yet calculated pixel size data of glyphs of
20983 the composition for the current face font, calculate them
20984 now. Theoretically, we have to check all fonts for the
20985 glyphs, but that requires much time and memory space. So,
20986 here we check only the font of the first glyph. This leads
20987 to incorrect display, but it's very rare, and C-l (recenter)
20988 can correct the display anyway. */
20989 if (! cmp->font || cmp->font != font)
20990 {
20991 /* Ascent and descent of the font of the first character
20992 of this composition (adjusted by baseline offset).
20993 Ascent and descent of overall glyphs should not be less
20994 than them respectively. */
20995 int font_ascent, font_descent, font_height;
20996 /* Bounding box of the overall glyphs. */
20997 int leftmost, rightmost, lowest, highest;
20998 int lbearing, rbearing;
20999 int i, width, ascent, descent;
21000 int left_padded = 0, right_padded = 0;
21001 int face_id;
21002 int c;
21003 XChar2b char2b;
21004 XCharStruct *pcm;
21005 int font_not_found_p;
21006 struct font_info *font_info;
21007 int pos;
21008
21009 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21010 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21011 break;
21012 if (glyph_len < cmp->glyph_len)
21013 right_padded = 1;
21014 for (i = 0; i < glyph_len; i++)
21015 {
21016 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21017 break;
21018 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21019 }
21020 if (i > 0)
21021 left_padded = 1;
21022
21023 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21024 : IT_CHARPOS (*it));
21025 /* When no suitable font found, use the default font. */
21026 font_not_found_p = font == NULL;
21027 if (font_not_found_p)
21028 {
21029 face = face->ascii_face;
21030 font = face->font;
21031 }
21032 font_info = FONT_INFO_FROM_FACE (it->f, face);
21033 boff = font_info->baseline_offset;
21034 if (font_info->vertical_centering)
21035 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21036 font_ascent = FONT_BASE (font) + boff;
21037 font_descent = FONT_DESCENT (font) - boff;
21038 font_height = FONT_HEIGHT (font);
21039
21040 cmp->font = (void *) font;
21041
21042 pcm = NULL;
21043 if (! font_not_found_p)
21044 {
21045 get_char_face_and_encoding (it->f, c, it->face_id,
21046 &char2b, it->multibyte_p, 0);
21047 pcm = get_per_char_metric (font, font_info, &char2b,
21048 FONT_TYPE_FOR_MULTIBYTE (font, c));
21049 }
21050
21051 /* Initialize the bounding box. */
21052 if (pcm)
21053 {
21054 width = pcm->width;
21055 ascent = pcm->ascent;
21056 descent = pcm->descent;
21057 lbearing = pcm->lbearing;
21058 rbearing = pcm->rbearing;
21059 }
21060 else
21061 {
21062 width = FONT_WIDTH (font);
21063 ascent = FONT_BASE (font);
21064 descent = FONT_DESCENT (font);
21065 lbearing = 0;
21066 rbearing = width;
21067 }
21068
21069 rightmost = width;
21070 leftmost = 0;
21071 lowest = - descent + boff;
21072 highest = ascent + boff;
21073
21074 if (! font_not_found_p
21075 && font_info->default_ascent
21076 && CHAR_TABLE_P (Vuse_default_ascent)
21077 && !NILP (Faref (Vuse_default_ascent,
21078 make_number (it->char_to_display))))
21079 highest = font_info->default_ascent + boff;
21080
21081 /* Draw the first glyph at the normal position. It may be
21082 shifted to right later if some other glyphs are drawn
21083 at the left. */
21084 cmp->offsets[i * 2] = 0;
21085 cmp->offsets[i * 2 + 1] = boff;
21086 cmp->lbearing = lbearing;
21087 cmp->rbearing = rbearing;
21088
21089 /* Set cmp->offsets for the remaining glyphs. */
21090 for (i++; i < glyph_len; i++)
21091 {
21092 int left, right, btm, top;
21093 int ch = COMPOSITION_GLYPH (cmp, i);
21094 int face_id;
21095 struct face *this_face;
21096 int this_boff;
21097
21098 if (ch == '\t')
21099 ch = ' ';
21100 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21101 this_face = FACE_FROM_ID (it->f, face_id);
21102 font = this_face->font;
21103
21104 if (font == NULL)
21105 pcm = NULL;
21106 else
21107 {
21108 font_info = FONT_INFO_FROM_FACE (it->f, this_face);
21109 this_boff = font_info->baseline_offset;
21110 if (font_info->vertical_centering)
21111 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21112 get_char_face_and_encoding (it->f, ch, face_id,
21113 &char2b, it->multibyte_p, 0);
21114 pcm = get_per_char_metric (font, font_info, &char2b,
21115 FONT_TYPE_FOR_MULTIBYTE (font,
21116 ch));
21117 }
21118 if (! pcm)
21119 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21120 else
21121 {
21122 width = pcm->width;
21123 ascent = pcm->ascent;
21124 descent = pcm->descent;
21125 lbearing = pcm->lbearing;
21126 rbearing = pcm->rbearing;
21127 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21128 {
21129 /* Relative composition with or without
21130 alternate chars. */
21131 left = (leftmost + rightmost - width) / 2;
21132 btm = - descent + boff;
21133 if (font_info->relative_compose
21134 && (! CHAR_TABLE_P (Vignore_relative_composition)
21135 || NILP (Faref (Vignore_relative_composition,
21136 make_number (ch)))))
21137 {
21138
21139 if (- descent >= font_info->relative_compose)
21140 /* One extra pixel between two glyphs. */
21141 btm = highest + 1;
21142 else if (ascent <= 0)
21143 /* One extra pixel between two glyphs. */
21144 btm = lowest - 1 - ascent - descent;
21145 }
21146 }
21147 else
21148 {
21149 /* A composition rule is specified by an integer
21150 value that encodes global and new reference
21151 points (GREF and NREF). GREF and NREF are
21152 specified by numbers as below:
21153
21154 0---1---2 -- ascent
21155 | |
21156 | |
21157 | |
21158 9--10--11 -- center
21159 | |
21160 ---3---4---5--- baseline
21161 | |
21162 6---7---8 -- descent
21163 */
21164 int rule = COMPOSITION_RULE (cmp, i);
21165 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21166
21167 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21168 grefx = gref % 3, nrefx = nref % 3;
21169 grefy = gref / 3, nrefy = nref / 3;
21170 if (xoff)
21171 xoff = font_height * (xoff - 128) / 256;
21172 if (yoff)
21173 yoff = font_height * (yoff - 128) / 256;
21174
21175 left = (leftmost
21176 + grefx * (rightmost - leftmost) / 2
21177 - nrefx * width / 2
21178 + xoff);
21179
21180 btm = ((grefy == 0 ? highest
21181 : grefy == 1 ? 0
21182 : grefy == 2 ? lowest
21183 : (highest + lowest) / 2)
21184 - (nrefy == 0 ? ascent + descent
21185 : nrefy == 1 ? descent - boff
21186 : nrefy == 2 ? 0
21187 : (ascent + descent) / 2)
21188 + yoff);
21189 }
21190
21191 cmp->offsets[i * 2] = left;
21192 cmp->offsets[i * 2 + 1] = btm + descent;
21193
21194 /* Update the bounding box of the overall glyphs. */
21195 if (width > 0)
21196 {
21197 right = left + width;
21198 if (left < leftmost)
21199 leftmost = left;
21200 if (right > rightmost)
21201 rightmost = right;
21202 }
21203 top = btm + descent + ascent;
21204 if (top > highest)
21205 highest = top;
21206 if (btm < lowest)
21207 lowest = btm;
21208
21209 if (cmp->lbearing > left + lbearing)
21210 cmp->lbearing = left + lbearing;
21211 if (cmp->rbearing < left + rbearing)
21212 cmp->rbearing = left + rbearing;
21213 }
21214 }
21215
21216 /* If there are glyphs whose x-offsets are negative,
21217 shift all glyphs to the right and make all x-offsets
21218 non-negative. */
21219 if (leftmost < 0)
21220 {
21221 for (i = 0; i < cmp->glyph_len; i++)
21222 cmp->offsets[i * 2] -= leftmost;
21223 rightmost -= leftmost;
21224 cmp->lbearing -= leftmost;
21225 cmp->rbearing -= leftmost;
21226 }
21227
21228 if (left_padded && cmp->lbearing < 0)
21229 {
21230 for (i = 0; i < cmp->glyph_len; i++)
21231 cmp->offsets[i * 2] -= cmp->lbearing;
21232 rightmost -= cmp->lbearing;
21233 cmp->rbearing -= cmp->lbearing;
21234 cmp->lbearing = 0;
21235 }
21236 if (right_padded && rightmost < cmp->rbearing)
21237 {
21238 rightmost = cmp->rbearing;
21239 }
21240
21241 cmp->pixel_width = rightmost;
21242 cmp->ascent = highest;
21243 cmp->descent = - lowest;
21244 if (cmp->ascent < font_ascent)
21245 cmp->ascent = font_ascent;
21246 if (cmp->descent < font_descent)
21247 cmp->descent = font_descent;
21248 }
21249
21250 if (it->glyph_row
21251 && (cmp->lbearing < 0
21252 || cmp->rbearing > cmp->pixel_width))
21253 it->glyph_row->contains_overlapping_glyphs_p = 1;
21254
21255 it->pixel_width = cmp->pixel_width;
21256 it->ascent = it->phys_ascent = cmp->ascent;
21257 it->descent = it->phys_descent = cmp->descent;
21258
21259 if (face->box != FACE_NO_BOX)
21260 {
21261 int thick = face->box_line_width;
21262
21263 if (thick > 0)
21264 {
21265 it->ascent += thick;
21266 it->descent += thick;
21267 }
21268 else
21269 thick = - thick;
21270
21271 if (it->start_of_box_run_p)
21272 it->pixel_width += thick;
21273 if (it->end_of_box_run_p)
21274 it->pixel_width += thick;
21275 }
21276
21277 /* If face has an overline, add the height of the overline
21278 (1 pixel) and a 1 pixel margin to the character height. */
21279 if (face->overline_p)
21280 it->ascent += overline_margin;
21281
21282 take_vertical_position_into_account (it);
21283
21284 if (it->glyph_row)
21285 append_composite_glyph (it);
21286 }
21287 else if (it->what == IT_IMAGE)
21288 produce_image_glyph (it);
21289 else if (it->what == IT_STRETCH)
21290 produce_stretch_glyph (it);
21291
21292 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21293 because this isn't true for images with `:ascent 100'. */
21294 xassert (it->ascent >= 0 && it->descent >= 0);
21295 if (it->area == TEXT_AREA)
21296 it->current_x += it->pixel_width;
21297
21298 if (extra_line_spacing > 0)
21299 {
21300 it->descent += extra_line_spacing;
21301 if (extra_line_spacing > it->max_extra_line_spacing)
21302 it->max_extra_line_spacing = extra_line_spacing;
21303 }
21304
21305 it->max_ascent = max (it->max_ascent, it->ascent);
21306 it->max_descent = max (it->max_descent, it->descent);
21307 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21308 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21309 }
21310
21311 /* EXPORT for RIF:
21312 Output LEN glyphs starting at START at the nominal cursor position.
21313 Advance the nominal cursor over the text. The global variable
21314 updated_window contains the window being updated, updated_row is
21315 the glyph row being updated, and updated_area is the area of that
21316 row being updated. */
21317
21318 void
21319 x_write_glyphs (start, len)
21320 struct glyph *start;
21321 int len;
21322 {
21323 int x, hpos;
21324
21325 xassert (updated_window && updated_row);
21326 BLOCK_INPUT;
21327
21328 /* Write glyphs. */
21329
21330 hpos = start - updated_row->glyphs[updated_area];
21331 x = draw_glyphs (updated_window, output_cursor.x,
21332 updated_row, updated_area,
21333 hpos, hpos + len,
21334 DRAW_NORMAL_TEXT, 0);
21335
21336 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21337 if (updated_area == TEXT_AREA
21338 && updated_window->phys_cursor_on_p
21339 && updated_window->phys_cursor.vpos == output_cursor.vpos
21340 && updated_window->phys_cursor.hpos >= hpos
21341 && updated_window->phys_cursor.hpos < hpos + len)
21342 updated_window->phys_cursor_on_p = 0;
21343
21344 UNBLOCK_INPUT;
21345
21346 /* Advance the output cursor. */
21347 output_cursor.hpos += len;
21348 output_cursor.x = x;
21349 }
21350
21351
21352 /* EXPORT for RIF:
21353 Insert LEN glyphs from START at the nominal cursor position. */
21354
21355 void
21356 x_insert_glyphs (start, len)
21357 struct glyph *start;
21358 int len;
21359 {
21360 struct frame *f;
21361 struct window *w;
21362 int line_height, shift_by_width, shifted_region_width;
21363 struct glyph_row *row;
21364 struct glyph *glyph;
21365 int frame_x, frame_y;
21366 EMACS_INT hpos;
21367
21368 xassert (updated_window && updated_row);
21369 BLOCK_INPUT;
21370 w = updated_window;
21371 f = XFRAME (WINDOW_FRAME (w));
21372
21373 /* Get the height of the line we are in. */
21374 row = updated_row;
21375 line_height = row->height;
21376
21377 /* Get the width of the glyphs to insert. */
21378 shift_by_width = 0;
21379 for (glyph = start; glyph < start + len; ++glyph)
21380 shift_by_width += glyph->pixel_width;
21381
21382 /* Get the width of the region to shift right. */
21383 shifted_region_width = (window_box_width (w, updated_area)
21384 - output_cursor.x
21385 - shift_by_width);
21386
21387 /* Shift right. */
21388 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21389 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21390
21391 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21392 line_height, shift_by_width);
21393
21394 /* Write the glyphs. */
21395 hpos = start - row->glyphs[updated_area];
21396 draw_glyphs (w, output_cursor.x, row, updated_area,
21397 hpos, hpos + len,
21398 DRAW_NORMAL_TEXT, 0);
21399
21400 /* Advance the output cursor. */
21401 output_cursor.hpos += len;
21402 output_cursor.x += shift_by_width;
21403 UNBLOCK_INPUT;
21404 }
21405
21406
21407 /* EXPORT for RIF:
21408 Erase the current text line from the nominal cursor position
21409 (inclusive) to pixel column TO_X (exclusive). The idea is that
21410 everything from TO_X onward is already erased.
21411
21412 TO_X is a pixel position relative to updated_area of
21413 updated_window. TO_X == -1 means clear to the end of this area. */
21414
21415 void
21416 x_clear_end_of_line (to_x)
21417 int to_x;
21418 {
21419 struct frame *f;
21420 struct window *w = updated_window;
21421 int max_x, min_y, max_y;
21422 int from_x, from_y, to_y;
21423
21424 xassert (updated_window && updated_row);
21425 f = XFRAME (w->frame);
21426
21427 if (updated_row->full_width_p)
21428 max_x = WINDOW_TOTAL_WIDTH (w);
21429 else
21430 max_x = window_box_width (w, updated_area);
21431 max_y = window_text_bottom_y (w);
21432
21433 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21434 of window. For TO_X > 0, truncate to end of drawing area. */
21435 if (to_x == 0)
21436 return;
21437 else if (to_x < 0)
21438 to_x = max_x;
21439 else
21440 to_x = min (to_x, max_x);
21441
21442 to_y = min (max_y, output_cursor.y + updated_row->height);
21443
21444 /* Notice if the cursor will be cleared by this operation. */
21445 if (!updated_row->full_width_p)
21446 notice_overwritten_cursor (w, updated_area,
21447 output_cursor.x, -1,
21448 updated_row->y,
21449 MATRIX_ROW_BOTTOM_Y (updated_row));
21450
21451 from_x = output_cursor.x;
21452
21453 /* Translate to frame coordinates. */
21454 if (updated_row->full_width_p)
21455 {
21456 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21457 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21458 }
21459 else
21460 {
21461 int area_left = window_box_left (w, updated_area);
21462 from_x += area_left;
21463 to_x += area_left;
21464 }
21465
21466 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21467 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21468 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21469
21470 /* Prevent inadvertently clearing to end of the X window. */
21471 if (to_x > from_x && to_y > from_y)
21472 {
21473 BLOCK_INPUT;
21474 rif->clear_frame_area (f, from_x, from_y,
21475 to_x - from_x, to_y - from_y);
21476 UNBLOCK_INPUT;
21477 }
21478 }
21479
21480 #endif /* HAVE_WINDOW_SYSTEM */
21481
21482
21483 \f
21484 /***********************************************************************
21485 Cursor types
21486 ***********************************************************************/
21487
21488 /* Value is the internal representation of the specified cursor type
21489 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21490 of the bar cursor. */
21491
21492 static enum text_cursor_kinds
21493 get_specified_cursor_type (arg, width)
21494 Lisp_Object arg;
21495 int *width;
21496 {
21497 enum text_cursor_kinds type;
21498
21499 if (NILP (arg))
21500 return NO_CURSOR;
21501
21502 if (EQ (arg, Qbox))
21503 return FILLED_BOX_CURSOR;
21504
21505 if (EQ (arg, Qhollow))
21506 return HOLLOW_BOX_CURSOR;
21507
21508 if (EQ (arg, Qbar))
21509 {
21510 *width = 2;
21511 return BAR_CURSOR;
21512 }
21513
21514 if (CONSP (arg)
21515 && EQ (XCAR (arg), Qbar)
21516 && INTEGERP (XCDR (arg))
21517 && XINT (XCDR (arg)) >= 0)
21518 {
21519 *width = XINT (XCDR (arg));
21520 return BAR_CURSOR;
21521 }
21522
21523 if (EQ (arg, Qhbar))
21524 {
21525 *width = 2;
21526 return HBAR_CURSOR;
21527 }
21528
21529 if (CONSP (arg)
21530 && EQ (XCAR (arg), Qhbar)
21531 && INTEGERP (XCDR (arg))
21532 && XINT (XCDR (arg)) >= 0)
21533 {
21534 *width = XINT (XCDR (arg));
21535 return HBAR_CURSOR;
21536 }
21537
21538 /* Treat anything unknown as "hollow box cursor".
21539 It was bad to signal an error; people have trouble fixing
21540 .Xdefaults with Emacs, when it has something bad in it. */
21541 type = HOLLOW_BOX_CURSOR;
21542
21543 return type;
21544 }
21545
21546 /* Set the default cursor types for specified frame. */
21547 void
21548 set_frame_cursor_types (f, arg)
21549 struct frame *f;
21550 Lisp_Object arg;
21551 {
21552 int width;
21553 Lisp_Object tem;
21554
21555 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21556 FRAME_CURSOR_WIDTH (f) = width;
21557
21558 /* By default, set up the blink-off state depending on the on-state. */
21559
21560 tem = Fassoc (arg, Vblink_cursor_alist);
21561 if (!NILP (tem))
21562 {
21563 FRAME_BLINK_OFF_CURSOR (f)
21564 = get_specified_cursor_type (XCDR (tem), &width);
21565 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21566 }
21567 else
21568 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21569 }
21570
21571
21572 /* Return the cursor we want to be displayed in window W. Return
21573 width of bar/hbar cursor through WIDTH arg. Return with
21574 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21575 (i.e. if the `system caret' should track this cursor).
21576
21577 In a mini-buffer window, we want the cursor only to appear if we
21578 are reading input from this window. For the selected window, we
21579 want the cursor type given by the frame parameter or buffer local
21580 setting of cursor-type. If explicitly marked off, draw no cursor.
21581 In all other cases, we want a hollow box cursor. */
21582
21583 static enum text_cursor_kinds
21584 get_window_cursor_type (w, glyph, width, active_cursor)
21585 struct window *w;
21586 struct glyph *glyph;
21587 int *width;
21588 int *active_cursor;
21589 {
21590 struct frame *f = XFRAME (w->frame);
21591 struct buffer *b = XBUFFER (w->buffer);
21592 int cursor_type = DEFAULT_CURSOR;
21593 Lisp_Object alt_cursor;
21594 int non_selected = 0;
21595
21596 *active_cursor = 1;
21597
21598 /* Echo area */
21599 if (cursor_in_echo_area
21600 && FRAME_HAS_MINIBUF_P (f)
21601 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21602 {
21603 if (w == XWINDOW (echo_area_window))
21604 {
21605 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21606 {
21607 *width = FRAME_CURSOR_WIDTH (f);
21608 return FRAME_DESIRED_CURSOR (f);
21609 }
21610 else
21611 return get_specified_cursor_type (b->cursor_type, width);
21612 }
21613
21614 *active_cursor = 0;
21615 non_selected = 1;
21616 }
21617
21618 /* Nonselected window or nonselected frame. */
21619 else if (w != XWINDOW (f->selected_window)
21620 #ifdef HAVE_WINDOW_SYSTEM
21621 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21622 #endif
21623 )
21624 {
21625 *active_cursor = 0;
21626
21627 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21628 return NO_CURSOR;
21629
21630 non_selected = 1;
21631 }
21632
21633 /* Never display a cursor in a window in which cursor-type is nil. */
21634 if (NILP (b->cursor_type))
21635 return NO_CURSOR;
21636
21637 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21638 if (non_selected)
21639 {
21640 alt_cursor = b->cursor_in_non_selected_windows;
21641 return get_specified_cursor_type (alt_cursor, width);
21642 }
21643
21644 /* Get the normal cursor type for this window. */
21645 if (EQ (b->cursor_type, Qt))
21646 {
21647 cursor_type = FRAME_DESIRED_CURSOR (f);
21648 *width = FRAME_CURSOR_WIDTH (f);
21649 }
21650 else
21651 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21652
21653 /* Use normal cursor if not blinked off. */
21654 if (!w->cursor_off_p)
21655 {
21656 #ifdef HAVE_WINDOW_SYSTEM
21657 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21658 {
21659 if (cursor_type == FILLED_BOX_CURSOR)
21660 {
21661 /* Using a block cursor on large images can be very annoying.
21662 So use a hollow cursor for "large" images.
21663 If image is not transparent (no mask), also use hollow cursor. */
21664 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21665 if (img != NULL && IMAGEP (img->spec))
21666 {
21667 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21668 where N = size of default frame font size.
21669 This should cover most of the "tiny" icons people may use. */
21670 if (!img->mask
21671 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21672 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21673 cursor_type = HOLLOW_BOX_CURSOR;
21674 }
21675 }
21676 else if (cursor_type != NO_CURSOR)
21677 {
21678 /* Display current only supports BOX and HOLLOW cursors for images.
21679 So for now, unconditionally use a HOLLOW cursor when cursor is
21680 not a solid box cursor. */
21681 cursor_type = HOLLOW_BOX_CURSOR;
21682 }
21683 }
21684 #endif
21685 return cursor_type;
21686 }
21687
21688 /* Cursor is blinked off, so determine how to "toggle" it. */
21689
21690 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21691 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21692 return get_specified_cursor_type (XCDR (alt_cursor), width);
21693
21694 /* Then see if frame has specified a specific blink off cursor type. */
21695 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21696 {
21697 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21698 return FRAME_BLINK_OFF_CURSOR (f);
21699 }
21700
21701 #if 0
21702 /* Some people liked having a permanently visible blinking cursor,
21703 while others had very strong opinions against it. So it was
21704 decided to remove it. KFS 2003-09-03 */
21705
21706 /* Finally perform built-in cursor blinking:
21707 filled box <-> hollow box
21708 wide [h]bar <-> narrow [h]bar
21709 narrow [h]bar <-> no cursor
21710 other type <-> no cursor */
21711
21712 if (cursor_type == FILLED_BOX_CURSOR)
21713 return HOLLOW_BOX_CURSOR;
21714
21715 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21716 {
21717 *width = 1;
21718 return cursor_type;
21719 }
21720 #endif
21721
21722 return NO_CURSOR;
21723 }
21724
21725
21726 #ifdef HAVE_WINDOW_SYSTEM
21727
21728 /* Notice when the text cursor of window W has been completely
21729 overwritten by a drawing operation that outputs glyphs in AREA
21730 starting at X0 and ending at X1 in the line starting at Y0 and
21731 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21732 the rest of the line after X0 has been written. Y coordinates
21733 are window-relative. */
21734
21735 static void
21736 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21737 struct window *w;
21738 enum glyph_row_area area;
21739 int x0, y0, x1, y1;
21740 {
21741 int cx0, cx1, cy0, cy1;
21742 struct glyph_row *row;
21743
21744 if (!w->phys_cursor_on_p)
21745 return;
21746 if (area != TEXT_AREA)
21747 return;
21748
21749 if (w->phys_cursor.vpos < 0
21750 || w->phys_cursor.vpos >= w->current_matrix->nrows
21751 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21752 !(row->enabled_p && row->displays_text_p)))
21753 return;
21754
21755 if (row->cursor_in_fringe_p)
21756 {
21757 row->cursor_in_fringe_p = 0;
21758 draw_fringe_bitmap (w, row, 0);
21759 w->phys_cursor_on_p = 0;
21760 return;
21761 }
21762
21763 cx0 = w->phys_cursor.x;
21764 cx1 = cx0 + w->phys_cursor_width;
21765 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21766 return;
21767
21768 /* The cursor image will be completely removed from the
21769 screen if the output area intersects the cursor area in
21770 y-direction. When we draw in [y0 y1[, and some part of
21771 the cursor is at y < y0, that part must have been drawn
21772 before. When scrolling, the cursor is erased before
21773 actually scrolling, so we don't come here. When not
21774 scrolling, the rows above the old cursor row must have
21775 changed, and in this case these rows must have written
21776 over the cursor image.
21777
21778 Likewise if part of the cursor is below y1, with the
21779 exception of the cursor being in the first blank row at
21780 the buffer and window end because update_text_area
21781 doesn't draw that row. (Except when it does, but
21782 that's handled in update_text_area.) */
21783
21784 cy0 = w->phys_cursor.y;
21785 cy1 = cy0 + w->phys_cursor_height;
21786 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21787 return;
21788
21789 w->phys_cursor_on_p = 0;
21790 }
21791
21792 #endif /* HAVE_WINDOW_SYSTEM */
21793
21794 \f
21795 /************************************************************************
21796 Mouse Face
21797 ************************************************************************/
21798
21799 #ifdef HAVE_WINDOW_SYSTEM
21800
21801 /* EXPORT for RIF:
21802 Fix the display of area AREA of overlapping row ROW in window W
21803 with respect to the overlapping part OVERLAPS. */
21804
21805 void
21806 x_fix_overlapping_area (w, row, area, overlaps)
21807 struct window *w;
21808 struct glyph_row *row;
21809 enum glyph_row_area area;
21810 int overlaps;
21811 {
21812 int i, x;
21813
21814 BLOCK_INPUT;
21815
21816 x = 0;
21817 for (i = 0; i < row->used[area];)
21818 {
21819 if (row->glyphs[area][i].overlaps_vertically_p)
21820 {
21821 int start = i, start_x = x;
21822
21823 do
21824 {
21825 x += row->glyphs[area][i].pixel_width;
21826 ++i;
21827 }
21828 while (i < row->used[area]
21829 && row->glyphs[area][i].overlaps_vertically_p);
21830
21831 draw_glyphs (w, start_x, row, area,
21832 start, i,
21833 DRAW_NORMAL_TEXT, overlaps);
21834 }
21835 else
21836 {
21837 x += row->glyphs[area][i].pixel_width;
21838 ++i;
21839 }
21840 }
21841
21842 UNBLOCK_INPUT;
21843 }
21844
21845
21846 /* EXPORT:
21847 Draw the cursor glyph of window W in glyph row ROW. See the
21848 comment of draw_glyphs for the meaning of HL. */
21849
21850 void
21851 draw_phys_cursor_glyph (w, row, hl)
21852 struct window *w;
21853 struct glyph_row *row;
21854 enum draw_glyphs_face hl;
21855 {
21856 /* If cursor hpos is out of bounds, don't draw garbage. This can
21857 happen in mini-buffer windows when switching between echo area
21858 glyphs and mini-buffer. */
21859 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21860 {
21861 int on_p = w->phys_cursor_on_p;
21862 int x1;
21863 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21864 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21865 hl, 0);
21866 w->phys_cursor_on_p = on_p;
21867
21868 if (hl == DRAW_CURSOR)
21869 w->phys_cursor_width = x1 - w->phys_cursor.x;
21870 /* When we erase the cursor, and ROW is overlapped by other
21871 rows, make sure that these overlapping parts of other rows
21872 are redrawn. */
21873 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21874 {
21875 w->phys_cursor_width = x1 - w->phys_cursor.x;
21876
21877 if (row > w->current_matrix->rows
21878 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21879 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21880 OVERLAPS_ERASED_CURSOR);
21881
21882 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21883 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21884 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21885 OVERLAPS_ERASED_CURSOR);
21886 }
21887 }
21888 }
21889
21890
21891 /* EXPORT:
21892 Erase the image of a cursor of window W from the screen. */
21893
21894 void
21895 erase_phys_cursor (w)
21896 struct window *w;
21897 {
21898 struct frame *f = XFRAME (w->frame);
21899 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21900 int hpos = w->phys_cursor.hpos;
21901 int vpos = w->phys_cursor.vpos;
21902 int mouse_face_here_p = 0;
21903 struct glyph_matrix *active_glyphs = w->current_matrix;
21904 struct glyph_row *cursor_row;
21905 struct glyph *cursor_glyph;
21906 enum draw_glyphs_face hl;
21907
21908 /* No cursor displayed or row invalidated => nothing to do on the
21909 screen. */
21910 if (w->phys_cursor_type == NO_CURSOR)
21911 goto mark_cursor_off;
21912
21913 /* VPOS >= active_glyphs->nrows means that window has been resized.
21914 Don't bother to erase the cursor. */
21915 if (vpos >= active_glyphs->nrows)
21916 goto mark_cursor_off;
21917
21918 /* If row containing cursor is marked invalid, there is nothing we
21919 can do. */
21920 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21921 if (!cursor_row->enabled_p)
21922 goto mark_cursor_off;
21923
21924 /* If line spacing is > 0, old cursor may only be partially visible in
21925 window after split-window. So adjust visible height. */
21926 cursor_row->visible_height = min (cursor_row->visible_height,
21927 window_text_bottom_y (w) - cursor_row->y);
21928
21929 /* If row is completely invisible, don't attempt to delete a cursor which
21930 isn't there. This can happen if cursor is at top of a window, and
21931 we switch to a buffer with a header line in that window. */
21932 if (cursor_row->visible_height <= 0)
21933 goto mark_cursor_off;
21934
21935 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21936 if (cursor_row->cursor_in_fringe_p)
21937 {
21938 cursor_row->cursor_in_fringe_p = 0;
21939 draw_fringe_bitmap (w, cursor_row, 0);
21940 goto mark_cursor_off;
21941 }
21942
21943 /* This can happen when the new row is shorter than the old one.
21944 In this case, either draw_glyphs or clear_end_of_line
21945 should have cleared the cursor. Note that we wouldn't be
21946 able to erase the cursor in this case because we don't have a
21947 cursor glyph at hand. */
21948 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21949 goto mark_cursor_off;
21950
21951 /* If the cursor is in the mouse face area, redisplay that when
21952 we clear the cursor. */
21953 if (! NILP (dpyinfo->mouse_face_window)
21954 && w == XWINDOW (dpyinfo->mouse_face_window)
21955 && (vpos > dpyinfo->mouse_face_beg_row
21956 || (vpos == dpyinfo->mouse_face_beg_row
21957 && hpos >= dpyinfo->mouse_face_beg_col))
21958 && (vpos < dpyinfo->mouse_face_end_row
21959 || (vpos == dpyinfo->mouse_face_end_row
21960 && hpos < dpyinfo->mouse_face_end_col))
21961 /* Don't redraw the cursor's spot in mouse face if it is at the
21962 end of a line (on a newline). The cursor appears there, but
21963 mouse highlighting does not. */
21964 && cursor_row->used[TEXT_AREA] > hpos)
21965 mouse_face_here_p = 1;
21966
21967 /* Maybe clear the display under the cursor. */
21968 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21969 {
21970 int x, y, left_x;
21971 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21972 int width;
21973
21974 cursor_glyph = get_phys_cursor_glyph (w);
21975 if (cursor_glyph == NULL)
21976 goto mark_cursor_off;
21977
21978 width = cursor_glyph->pixel_width;
21979 left_x = window_box_left_offset (w, TEXT_AREA);
21980 x = w->phys_cursor.x;
21981 if (x < left_x)
21982 width -= left_x - x;
21983 width = min (width, window_box_width (w, TEXT_AREA) - x);
21984 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21985 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21986
21987 if (width > 0)
21988 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21989 }
21990
21991 /* Erase the cursor by redrawing the character underneath it. */
21992 if (mouse_face_here_p)
21993 hl = DRAW_MOUSE_FACE;
21994 else
21995 hl = DRAW_NORMAL_TEXT;
21996 draw_phys_cursor_glyph (w, cursor_row, hl);
21997
21998 mark_cursor_off:
21999 w->phys_cursor_on_p = 0;
22000 w->phys_cursor_type = NO_CURSOR;
22001 }
22002
22003
22004 /* EXPORT:
22005 Display or clear cursor of window W. If ON is zero, clear the
22006 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22007 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22008
22009 void
22010 display_and_set_cursor (w, on, hpos, vpos, x, y)
22011 struct window *w;
22012 int on, hpos, vpos, x, y;
22013 {
22014 struct frame *f = XFRAME (w->frame);
22015 int new_cursor_type;
22016 int new_cursor_width;
22017 int active_cursor;
22018 struct glyph_row *glyph_row;
22019 struct glyph *glyph;
22020
22021 /* This is pointless on invisible frames, and dangerous on garbaged
22022 windows and frames; in the latter case, the frame or window may
22023 be in the midst of changing its size, and x and y may be off the
22024 window. */
22025 if (! FRAME_VISIBLE_P (f)
22026 || FRAME_GARBAGED_P (f)
22027 || vpos >= w->current_matrix->nrows
22028 || hpos >= w->current_matrix->matrix_w)
22029 return;
22030
22031 /* If cursor is off and we want it off, return quickly. */
22032 if (!on && !w->phys_cursor_on_p)
22033 return;
22034
22035 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22036 /* If cursor row is not enabled, we don't really know where to
22037 display the cursor. */
22038 if (!glyph_row->enabled_p)
22039 {
22040 w->phys_cursor_on_p = 0;
22041 return;
22042 }
22043
22044 glyph = NULL;
22045 if (!glyph_row->exact_window_width_line_p
22046 || hpos < glyph_row->used[TEXT_AREA])
22047 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22048
22049 xassert (interrupt_input_blocked);
22050
22051 /* Set new_cursor_type to the cursor we want to be displayed. */
22052 new_cursor_type = get_window_cursor_type (w, glyph,
22053 &new_cursor_width, &active_cursor);
22054
22055 /* If cursor is currently being shown and we don't want it to be or
22056 it is in the wrong place, or the cursor type is not what we want,
22057 erase it. */
22058 if (w->phys_cursor_on_p
22059 && (!on
22060 || w->phys_cursor.x != x
22061 || w->phys_cursor.y != y
22062 || new_cursor_type != w->phys_cursor_type
22063 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22064 && new_cursor_width != w->phys_cursor_width)))
22065 erase_phys_cursor (w);
22066
22067 /* Don't check phys_cursor_on_p here because that flag is only set
22068 to zero in some cases where we know that the cursor has been
22069 completely erased, to avoid the extra work of erasing the cursor
22070 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22071 still not be visible, or it has only been partly erased. */
22072 if (on)
22073 {
22074 w->phys_cursor_ascent = glyph_row->ascent;
22075 w->phys_cursor_height = glyph_row->height;
22076
22077 /* Set phys_cursor_.* before x_draw_.* is called because some
22078 of them may need the information. */
22079 w->phys_cursor.x = x;
22080 w->phys_cursor.y = glyph_row->y;
22081 w->phys_cursor.hpos = hpos;
22082 w->phys_cursor.vpos = vpos;
22083 }
22084
22085 rif->draw_window_cursor (w, glyph_row, x, y,
22086 new_cursor_type, new_cursor_width,
22087 on, active_cursor);
22088 }
22089
22090
22091 /* Switch the display of W's cursor on or off, according to the value
22092 of ON. */
22093
22094 static void
22095 update_window_cursor (w, on)
22096 struct window *w;
22097 int on;
22098 {
22099 /* Don't update cursor in windows whose frame is in the process
22100 of being deleted. */
22101 if (w->current_matrix)
22102 {
22103 BLOCK_INPUT;
22104 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22105 w->phys_cursor.x, w->phys_cursor.y);
22106 UNBLOCK_INPUT;
22107 }
22108 }
22109
22110
22111 /* Call update_window_cursor with parameter ON_P on all leaf windows
22112 in the window tree rooted at W. */
22113
22114 static void
22115 update_cursor_in_window_tree (w, on_p)
22116 struct window *w;
22117 int on_p;
22118 {
22119 while (w)
22120 {
22121 if (!NILP (w->hchild))
22122 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22123 else if (!NILP (w->vchild))
22124 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22125 else
22126 update_window_cursor (w, on_p);
22127
22128 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22129 }
22130 }
22131
22132
22133 /* EXPORT:
22134 Display the cursor on window W, or clear it, according to ON_P.
22135 Don't change the cursor's position. */
22136
22137 void
22138 x_update_cursor (f, on_p)
22139 struct frame *f;
22140 int on_p;
22141 {
22142 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22143 }
22144
22145
22146 /* EXPORT:
22147 Clear the cursor of window W to background color, and mark the
22148 cursor as not shown. This is used when the text where the cursor
22149 is is about to be rewritten. */
22150
22151 void
22152 x_clear_cursor (w)
22153 struct window *w;
22154 {
22155 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22156 update_window_cursor (w, 0);
22157 }
22158
22159
22160 /* EXPORT:
22161 Display the active region described by mouse_face_* according to DRAW. */
22162
22163 void
22164 show_mouse_face (dpyinfo, draw)
22165 Display_Info *dpyinfo;
22166 enum draw_glyphs_face draw;
22167 {
22168 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22169 struct frame *f = XFRAME (WINDOW_FRAME (w));
22170
22171 if (/* If window is in the process of being destroyed, don't bother
22172 to do anything. */
22173 w->current_matrix != NULL
22174 /* Don't update mouse highlight if hidden */
22175 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22176 /* Recognize when we are called to operate on rows that don't exist
22177 anymore. This can happen when a window is split. */
22178 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22179 {
22180 int phys_cursor_on_p = w->phys_cursor_on_p;
22181 struct glyph_row *row, *first, *last;
22182
22183 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22184 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22185
22186 for (row = first; row <= last && row->enabled_p; ++row)
22187 {
22188 int start_hpos, end_hpos, start_x;
22189
22190 /* For all but the first row, the highlight starts at column 0. */
22191 if (row == first)
22192 {
22193 start_hpos = dpyinfo->mouse_face_beg_col;
22194 start_x = dpyinfo->mouse_face_beg_x;
22195 }
22196 else
22197 {
22198 start_hpos = 0;
22199 start_x = 0;
22200 }
22201
22202 if (row == last)
22203 end_hpos = dpyinfo->mouse_face_end_col;
22204 else
22205 {
22206 end_hpos = row->used[TEXT_AREA];
22207 if (draw == DRAW_NORMAL_TEXT)
22208 row->fill_line_p = 1; /* Clear to end of line */
22209 }
22210
22211 if (end_hpos > start_hpos)
22212 {
22213 draw_glyphs (w, start_x, row, TEXT_AREA,
22214 start_hpos, end_hpos,
22215 draw, 0);
22216
22217 row->mouse_face_p
22218 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22219 }
22220 }
22221
22222 /* When we've written over the cursor, arrange for it to
22223 be displayed again. */
22224 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22225 {
22226 BLOCK_INPUT;
22227 display_and_set_cursor (w, 1,
22228 w->phys_cursor.hpos, w->phys_cursor.vpos,
22229 w->phys_cursor.x, w->phys_cursor.y);
22230 UNBLOCK_INPUT;
22231 }
22232 }
22233
22234 /* Change the mouse cursor. */
22235 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22236 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22237 else if (draw == DRAW_MOUSE_FACE)
22238 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22239 else
22240 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22241 }
22242
22243 /* EXPORT:
22244 Clear out the mouse-highlighted active region.
22245 Redraw it un-highlighted first. Value is non-zero if mouse
22246 face was actually drawn unhighlighted. */
22247
22248 int
22249 clear_mouse_face (dpyinfo)
22250 Display_Info *dpyinfo;
22251 {
22252 int cleared = 0;
22253
22254 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22255 {
22256 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22257 cleared = 1;
22258 }
22259
22260 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22261 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22262 dpyinfo->mouse_face_window = Qnil;
22263 dpyinfo->mouse_face_overlay = Qnil;
22264 return cleared;
22265 }
22266
22267
22268 /* EXPORT:
22269 Non-zero if physical cursor of window W is within mouse face. */
22270
22271 int
22272 cursor_in_mouse_face_p (w)
22273 struct window *w;
22274 {
22275 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22276 int in_mouse_face = 0;
22277
22278 if (WINDOWP (dpyinfo->mouse_face_window)
22279 && XWINDOW (dpyinfo->mouse_face_window) == w)
22280 {
22281 int hpos = w->phys_cursor.hpos;
22282 int vpos = w->phys_cursor.vpos;
22283
22284 if (vpos >= dpyinfo->mouse_face_beg_row
22285 && vpos <= dpyinfo->mouse_face_end_row
22286 && (vpos > dpyinfo->mouse_face_beg_row
22287 || hpos >= dpyinfo->mouse_face_beg_col)
22288 && (vpos < dpyinfo->mouse_face_end_row
22289 || hpos < dpyinfo->mouse_face_end_col
22290 || dpyinfo->mouse_face_past_end))
22291 in_mouse_face = 1;
22292 }
22293
22294 return in_mouse_face;
22295 }
22296
22297
22298
22299 \f
22300 /* Find the glyph matrix position of buffer position CHARPOS in window
22301 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22302 current glyphs must be up to date. If CHARPOS is above window
22303 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22304 of last line in W. In the row containing CHARPOS, stop before glyphs
22305 having STOP as object. */
22306
22307 #if 1 /* This is a version of fast_find_position that's more correct
22308 in the presence of hscrolling, for example. I didn't install
22309 it right away because the problem fixed is minor, it failed
22310 in 20.x as well, and I think it's too risky to install
22311 so near the release of 21.1. 2001-09-25 gerd. */
22312
22313 static int
22314 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22315 struct window *w;
22316 EMACS_INT charpos;
22317 int *hpos, *vpos, *x, *y;
22318 Lisp_Object stop;
22319 {
22320 struct glyph_row *row, *first;
22321 struct glyph *glyph, *end;
22322 int past_end = 0;
22323
22324 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22325 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22326 {
22327 *x = first->x;
22328 *y = first->y;
22329 *hpos = 0;
22330 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22331 return 1;
22332 }
22333
22334 row = row_containing_pos (w, charpos, first, NULL, 0);
22335 if (row == NULL)
22336 {
22337 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22338 past_end = 1;
22339 }
22340
22341 /* If whole rows or last part of a row came from a display overlay,
22342 row_containing_pos will skip over such rows because their end pos
22343 equals the start pos of the overlay or interval.
22344
22345 Move back if we have a STOP object and previous row's
22346 end glyph came from STOP. */
22347 if (!NILP (stop))
22348 {
22349 struct glyph_row *prev;
22350 while ((prev = row - 1, prev >= first)
22351 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22352 && prev->used[TEXT_AREA] > 0)
22353 {
22354 struct glyph *beg = prev->glyphs[TEXT_AREA];
22355 glyph = beg + prev->used[TEXT_AREA];
22356 while (--glyph >= beg
22357 && INTEGERP (glyph->object));
22358 if (glyph < beg
22359 || !EQ (stop, glyph->object))
22360 break;
22361 row = prev;
22362 }
22363 }
22364
22365 *x = row->x;
22366 *y = row->y;
22367 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22368
22369 glyph = row->glyphs[TEXT_AREA];
22370 end = glyph + row->used[TEXT_AREA];
22371
22372 /* Skip over glyphs not having an object at the start of the row.
22373 These are special glyphs like truncation marks on terminal
22374 frames. */
22375 if (row->displays_text_p)
22376 while (glyph < end
22377 && INTEGERP (glyph->object)
22378 && !EQ (stop, glyph->object)
22379 && glyph->charpos < 0)
22380 {
22381 *x += glyph->pixel_width;
22382 ++glyph;
22383 }
22384
22385 while (glyph < end
22386 && !INTEGERP (glyph->object)
22387 && !EQ (stop, glyph->object)
22388 && (!BUFFERP (glyph->object)
22389 || glyph->charpos < charpos))
22390 {
22391 *x += glyph->pixel_width;
22392 ++glyph;
22393 }
22394
22395 *hpos = glyph - row->glyphs[TEXT_AREA];
22396 return !past_end;
22397 }
22398
22399 #else /* not 1 */
22400
22401 static int
22402 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22403 struct window *w;
22404 EMACS_INT pos;
22405 int *hpos, *vpos, *x, *y;
22406 Lisp_Object stop;
22407 {
22408 int i;
22409 int lastcol;
22410 int maybe_next_line_p = 0;
22411 int line_start_position;
22412 int yb = window_text_bottom_y (w);
22413 struct glyph_row *row, *best_row;
22414 int row_vpos, best_row_vpos;
22415 int current_x;
22416
22417 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22418 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22419
22420 while (row->y < yb)
22421 {
22422 if (row->used[TEXT_AREA])
22423 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22424 else
22425 line_start_position = 0;
22426
22427 if (line_start_position > pos)
22428 break;
22429 /* If the position sought is the end of the buffer,
22430 don't include the blank lines at the bottom of the window. */
22431 else if (line_start_position == pos
22432 && pos == BUF_ZV (XBUFFER (w->buffer)))
22433 {
22434 maybe_next_line_p = 1;
22435 break;
22436 }
22437 else if (line_start_position > 0)
22438 {
22439 best_row = row;
22440 best_row_vpos = row_vpos;
22441 }
22442
22443 if (row->y + row->height >= yb)
22444 break;
22445
22446 ++row;
22447 ++row_vpos;
22448 }
22449
22450 /* Find the right column within BEST_ROW. */
22451 lastcol = 0;
22452 current_x = best_row->x;
22453 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22454 {
22455 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22456 int charpos = glyph->charpos;
22457
22458 if (BUFFERP (glyph->object))
22459 {
22460 if (charpos == pos)
22461 {
22462 *hpos = i;
22463 *vpos = best_row_vpos;
22464 *x = current_x;
22465 *y = best_row->y;
22466 return 1;
22467 }
22468 else if (charpos > pos)
22469 break;
22470 }
22471 else if (EQ (glyph->object, stop))
22472 break;
22473
22474 if (charpos > 0)
22475 lastcol = i;
22476 current_x += glyph->pixel_width;
22477 }
22478
22479 /* If we're looking for the end of the buffer,
22480 and we didn't find it in the line we scanned,
22481 use the start of the following line. */
22482 if (maybe_next_line_p)
22483 {
22484 ++best_row;
22485 ++best_row_vpos;
22486 lastcol = 0;
22487 current_x = best_row->x;
22488 }
22489
22490 *vpos = best_row_vpos;
22491 *hpos = lastcol + 1;
22492 *x = current_x;
22493 *y = best_row->y;
22494 return 0;
22495 }
22496
22497 #endif /* not 1 */
22498
22499
22500 /* Find the position of the glyph for position POS in OBJECT in
22501 window W's current matrix, and return in *X, *Y the pixel
22502 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22503
22504 RIGHT_P non-zero means return the position of the right edge of the
22505 glyph, RIGHT_P zero means return the left edge position.
22506
22507 If no glyph for POS exists in the matrix, return the position of
22508 the glyph with the next smaller position that is in the matrix, if
22509 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22510 exists in the matrix, return the position of the glyph with the
22511 next larger position in OBJECT.
22512
22513 Value is non-zero if a glyph was found. */
22514
22515 static int
22516 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22517 struct window *w;
22518 EMACS_INT pos;
22519 Lisp_Object object;
22520 int *hpos, *vpos, *x, *y;
22521 int right_p;
22522 {
22523 int yb = window_text_bottom_y (w);
22524 struct glyph_row *r;
22525 struct glyph *best_glyph = NULL;
22526 struct glyph_row *best_row = NULL;
22527 int best_x = 0;
22528
22529 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22530 r->enabled_p && r->y < yb;
22531 ++r)
22532 {
22533 struct glyph *g = r->glyphs[TEXT_AREA];
22534 struct glyph *e = g + r->used[TEXT_AREA];
22535 int gx;
22536
22537 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22538 if (EQ (g->object, object))
22539 {
22540 if (g->charpos == pos)
22541 {
22542 best_glyph = g;
22543 best_x = gx;
22544 best_row = r;
22545 goto found;
22546 }
22547 else if (best_glyph == NULL
22548 || ((abs (g->charpos - pos)
22549 < abs (best_glyph->charpos - pos))
22550 && (right_p
22551 ? g->charpos < pos
22552 : g->charpos > pos)))
22553 {
22554 best_glyph = g;
22555 best_x = gx;
22556 best_row = r;
22557 }
22558 }
22559 }
22560
22561 found:
22562
22563 if (best_glyph)
22564 {
22565 *x = best_x;
22566 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22567
22568 if (right_p)
22569 {
22570 *x += best_glyph->pixel_width;
22571 ++*hpos;
22572 }
22573
22574 *y = best_row->y;
22575 *vpos = best_row - w->current_matrix->rows;
22576 }
22577
22578 return best_glyph != NULL;
22579 }
22580
22581
22582 /* See if position X, Y is within a hot-spot of an image. */
22583
22584 static int
22585 on_hot_spot_p (hot_spot, x, y)
22586 Lisp_Object hot_spot;
22587 int x, y;
22588 {
22589 if (!CONSP (hot_spot))
22590 return 0;
22591
22592 if (EQ (XCAR (hot_spot), Qrect))
22593 {
22594 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22595 Lisp_Object rect = XCDR (hot_spot);
22596 Lisp_Object tem;
22597 if (!CONSP (rect))
22598 return 0;
22599 if (!CONSP (XCAR (rect)))
22600 return 0;
22601 if (!CONSP (XCDR (rect)))
22602 return 0;
22603 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22604 return 0;
22605 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22606 return 0;
22607 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22608 return 0;
22609 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22610 return 0;
22611 return 1;
22612 }
22613 else if (EQ (XCAR (hot_spot), Qcircle))
22614 {
22615 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22616 Lisp_Object circ = XCDR (hot_spot);
22617 Lisp_Object lr, lx0, ly0;
22618 if (CONSP (circ)
22619 && CONSP (XCAR (circ))
22620 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22621 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22622 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22623 {
22624 double r = XFLOATINT (lr);
22625 double dx = XINT (lx0) - x;
22626 double dy = XINT (ly0) - y;
22627 return (dx * dx + dy * dy <= r * r);
22628 }
22629 }
22630 else if (EQ (XCAR (hot_spot), Qpoly))
22631 {
22632 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22633 if (VECTORP (XCDR (hot_spot)))
22634 {
22635 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22636 Lisp_Object *poly = v->contents;
22637 int n = v->size;
22638 int i;
22639 int inside = 0;
22640 Lisp_Object lx, ly;
22641 int x0, y0;
22642
22643 /* Need an even number of coordinates, and at least 3 edges. */
22644 if (n < 6 || n & 1)
22645 return 0;
22646
22647 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22648 If count is odd, we are inside polygon. Pixels on edges
22649 may or may not be included depending on actual geometry of the
22650 polygon. */
22651 if ((lx = poly[n-2], !INTEGERP (lx))
22652 || (ly = poly[n-1], !INTEGERP (lx)))
22653 return 0;
22654 x0 = XINT (lx), y0 = XINT (ly);
22655 for (i = 0; i < n; i += 2)
22656 {
22657 int x1 = x0, y1 = y0;
22658 if ((lx = poly[i], !INTEGERP (lx))
22659 || (ly = poly[i+1], !INTEGERP (ly)))
22660 return 0;
22661 x0 = XINT (lx), y0 = XINT (ly);
22662
22663 /* Does this segment cross the X line? */
22664 if (x0 >= x)
22665 {
22666 if (x1 >= x)
22667 continue;
22668 }
22669 else if (x1 < x)
22670 continue;
22671 if (y > y0 && y > y1)
22672 continue;
22673 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22674 inside = !inside;
22675 }
22676 return inside;
22677 }
22678 }
22679 /* If we don't understand the format, pretend we're not in the hot-spot. */
22680 return 0;
22681 }
22682
22683 Lisp_Object
22684 find_hot_spot (map, x, y)
22685 Lisp_Object map;
22686 int x, y;
22687 {
22688 while (CONSP (map))
22689 {
22690 if (CONSP (XCAR (map))
22691 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22692 return XCAR (map);
22693 map = XCDR (map);
22694 }
22695
22696 return Qnil;
22697 }
22698
22699 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22700 3, 3, 0,
22701 doc: /* Lookup in image map MAP coordinates X and Y.
22702 An image map is an alist where each element has the format (AREA ID PLIST).
22703 An AREA is specified as either a rectangle, a circle, or a polygon:
22704 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22705 pixel coordinates of the upper left and bottom right corners.
22706 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22707 and the radius of the circle; r may be a float or integer.
22708 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22709 vector describes one corner in the polygon.
22710 Returns the alist element for the first matching AREA in MAP. */)
22711 (map, x, y)
22712 Lisp_Object map;
22713 Lisp_Object x, y;
22714 {
22715 if (NILP (map))
22716 return Qnil;
22717
22718 CHECK_NUMBER (x);
22719 CHECK_NUMBER (y);
22720
22721 return find_hot_spot (map, XINT (x), XINT (y));
22722 }
22723
22724
22725 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22726 static void
22727 define_frame_cursor1 (f, cursor, pointer)
22728 struct frame *f;
22729 Cursor cursor;
22730 Lisp_Object pointer;
22731 {
22732 /* Do not change cursor shape while dragging mouse. */
22733 if (!NILP (do_mouse_tracking))
22734 return;
22735
22736 if (!NILP (pointer))
22737 {
22738 if (EQ (pointer, Qarrow))
22739 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22740 else if (EQ (pointer, Qhand))
22741 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22742 else if (EQ (pointer, Qtext))
22743 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22744 else if (EQ (pointer, intern ("hdrag")))
22745 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22746 #ifdef HAVE_X_WINDOWS
22747 else if (EQ (pointer, intern ("vdrag")))
22748 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22749 #endif
22750 else if (EQ (pointer, intern ("hourglass")))
22751 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22752 else if (EQ (pointer, Qmodeline))
22753 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22754 else
22755 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22756 }
22757
22758 if (cursor != No_Cursor)
22759 rif->define_frame_cursor (f, cursor);
22760 }
22761
22762 /* Take proper action when mouse has moved to the mode or header line
22763 or marginal area AREA of window W, x-position X and y-position Y.
22764 X is relative to the start of the text display area of W, so the
22765 width of bitmap areas and scroll bars must be subtracted to get a
22766 position relative to the start of the mode line. */
22767
22768 static void
22769 note_mode_line_or_margin_highlight (window, x, y, area)
22770 Lisp_Object window;
22771 int x, y;
22772 enum window_part area;
22773 {
22774 struct window *w = XWINDOW (window);
22775 struct frame *f = XFRAME (w->frame);
22776 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22777 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22778 Lisp_Object pointer = Qnil;
22779 int charpos, dx, dy, width, height;
22780 Lisp_Object string, object = Qnil;
22781 Lisp_Object pos, help;
22782
22783 Lisp_Object mouse_face;
22784 int original_x_pixel = x;
22785 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22786 struct glyph_row *row;
22787
22788 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22789 {
22790 int x0;
22791 struct glyph *end;
22792
22793 string = mode_line_string (w, area, &x, &y, &charpos,
22794 &object, &dx, &dy, &width, &height);
22795
22796 row = (area == ON_MODE_LINE
22797 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22798 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22799
22800 /* Find glyph */
22801 if (row->mode_line_p && row->enabled_p)
22802 {
22803 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22804 end = glyph + row->used[TEXT_AREA];
22805
22806 for (x0 = original_x_pixel;
22807 glyph < end && x0 >= glyph->pixel_width;
22808 ++glyph)
22809 x0 -= glyph->pixel_width;
22810
22811 if (glyph >= end)
22812 glyph = NULL;
22813 }
22814 }
22815 else
22816 {
22817 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22818 string = marginal_area_string (w, area, &x, &y, &charpos,
22819 &object, &dx, &dy, &width, &height);
22820 }
22821
22822 help = Qnil;
22823
22824 if (IMAGEP (object))
22825 {
22826 Lisp_Object image_map, hotspot;
22827 if ((image_map = Fplist_get (XCDR (object), QCmap),
22828 !NILP (image_map))
22829 && (hotspot = find_hot_spot (image_map, dx, dy),
22830 CONSP (hotspot))
22831 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22832 {
22833 Lisp_Object area_id, plist;
22834
22835 area_id = XCAR (hotspot);
22836 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22837 If so, we could look for mouse-enter, mouse-leave
22838 properties in PLIST (and do something...). */
22839 hotspot = XCDR (hotspot);
22840 if (CONSP (hotspot)
22841 && (plist = XCAR (hotspot), CONSP (plist)))
22842 {
22843 pointer = Fplist_get (plist, Qpointer);
22844 if (NILP (pointer))
22845 pointer = Qhand;
22846 help = Fplist_get (plist, Qhelp_echo);
22847 if (!NILP (help))
22848 {
22849 help_echo_string = help;
22850 /* Is this correct? ++kfs */
22851 XSETWINDOW (help_echo_window, w);
22852 help_echo_object = w->buffer;
22853 help_echo_pos = charpos;
22854 }
22855 }
22856 }
22857 if (NILP (pointer))
22858 pointer = Fplist_get (XCDR (object), QCpointer);
22859 }
22860
22861 if (STRINGP (string))
22862 {
22863 pos = make_number (charpos);
22864 /* If we're on a string with `help-echo' text property, arrange
22865 for the help to be displayed. This is done by setting the
22866 global variable help_echo_string to the help string. */
22867 if (NILP (help))
22868 {
22869 help = Fget_text_property (pos, Qhelp_echo, string);
22870 if (!NILP (help))
22871 {
22872 help_echo_string = help;
22873 XSETWINDOW (help_echo_window, w);
22874 help_echo_object = string;
22875 help_echo_pos = charpos;
22876 }
22877 }
22878
22879 if (NILP (pointer))
22880 pointer = Fget_text_property (pos, Qpointer, string);
22881
22882 /* Change the mouse pointer according to what is under X/Y. */
22883 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22884 {
22885 Lisp_Object map;
22886 map = Fget_text_property (pos, Qlocal_map, string);
22887 if (!KEYMAPP (map))
22888 map = Fget_text_property (pos, Qkeymap, string);
22889 if (!KEYMAPP (map))
22890 cursor = dpyinfo->vertical_scroll_bar_cursor;
22891 }
22892
22893 /* Change the mouse face according to what is under X/Y. */
22894 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22895 if (!NILP (mouse_face)
22896 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22897 && glyph)
22898 {
22899 Lisp_Object b, e;
22900
22901 struct glyph * tmp_glyph;
22902
22903 int gpos;
22904 int gseq_length;
22905 int total_pixel_width;
22906 int ignore;
22907
22908 int vpos, hpos;
22909
22910 b = Fprevious_single_property_change (make_number (charpos + 1),
22911 Qmouse_face, string, Qnil);
22912 if (NILP (b))
22913 b = make_number (0);
22914
22915 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22916 if (NILP (e))
22917 e = make_number (SCHARS (string));
22918
22919 /* Calculate the position(glyph position: GPOS) of GLYPH in
22920 displayed string. GPOS is different from CHARPOS.
22921
22922 CHARPOS is the position of glyph in internal string
22923 object. A mode line string format has structures which
22924 is converted to a flatten by emacs lisp interpreter.
22925 The internal string is an element of the structures.
22926 The displayed string is the flatten string. */
22927 gpos = 0;
22928 if (glyph > row_start_glyph)
22929 {
22930 tmp_glyph = glyph - 1;
22931 while (tmp_glyph >= row_start_glyph
22932 && tmp_glyph->charpos >= XINT (b)
22933 && EQ (tmp_glyph->object, glyph->object))
22934 {
22935 tmp_glyph--;
22936 gpos++;
22937 }
22938 }
22939
22940 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22941 displayed string holding GLYPH.
22942
22943 GSEQ_LENGTH is different from SCHARS (STRING).
22944 SCHARS (STRING) returns the length of the internal string. */
22945 for (tmp_glyph = glyph, gseq_length = gpos;
22946 tmp_glyph->charpos < XINT (e);
22947 tmp_glyph++, gseq_length++)
22948 {
22949 if (!EQ (tmp_glyph->object, glyph->object))
22950 break;
22951 }
22952
22953 total_pixel_width = 0;
22954 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22955 total_pixel_width += tmp_glyph->pixel_width;
22956
22957 /* Pre calculation of re-rendering position */
22958 vpos = (x - gpos);
22959 hpos = (area == ON_MODE_LINE
22960 ? (w->current_matrix)->nrows - 1
22961 : 0);
22962
22963 /* If the re-rendering position is included in the last
22964 re-rendering area, we should do nothing. */
22965 if ( EQ (window, dpyinfo->mouse_face_window)
22966 && dpyinfo->mouse_face_beg_col <= vpos
22967 && vpos < dpyinfo->mouse_face_end_col
22968 && dpyinfo->mouse_face_beg_row == hpos )
22969 return;
22970
22971 if (clear_mouse_face (dpyinfo))
22972 cursor = No_Cursor;
22973
22974 dpyinfo->mouse_face_beg_col = vpos;
22975 dpyinfo->mouse_face_beg_row = hpos;
22976
22977 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22978 dpyinfo->mouse_face_beg_y = 0;
22979
22980 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22981 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22982
22983 dpyinfo->mouse_face_end_x = 0;
22984 dpyinfo->mouse_face_end_y = 0;
22985
22986 dpyinfo->mouse_face_past_end = 0;
22987 dpyinfo->mouse_face_window = window;
22988
22989 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22990 charpos,
22991 0, 0, 0, &ignore,
22992 glyph->face_id, 1);
22993 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22994
22995 if (NILP (pointer))
22996 pointer = Qhand;
22997 }
22998 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22999 clear_mouse_face (dpyinfo);
23000 }
23001 define_frame_cursor1 (f, cursor, pointer);
23002 }
23003
23004
23005 /* EXPORT:
23006 Take proper action when the mouse has moved to position X, Y on
23007 frame F as regards highlighting characters that have mouse-face
23008 properties. Also de-highlighting chars where the mouse was before.
23009 X and Y can be negative or out of range. */
23010
23011 void
23012 note_mouse_highlight (f, x, y)
23013 struct frame *f;
23014 int x, y;
23015 {
23016 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23017 enum window_part part;
23018 Lisp_Object window;
23019 struct window *w;
23020 Cursor cursor = No_Cursor;
23021 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23022 struct buffer *b;
23023
23024 /* When a menu is active, don't highlight because this looks odd. */
23025 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23026 if (popup_activated ())
23027 return;
23028 #endif
23029
23030 if (NILP (Vmouse_highlight)
23031 || !f->glyphs_initialized_p)
23032 return;
23033
23034 dpyinfo->mouse_face_mouse_x = x;
23035 dpyinfo->mouse_face_mouse_y = y;
23036 dpyinfo->mouse_face_mouse_frame = f;
23037
23038 if (dpyinfo->mouse_face_defer)
23039 return;
23040
23041 if (gc_in_progress)
23042 {
23043 dpyinfo->mouse_face_deferred_gc = 1;
23044 return;
23045 }
23046
23047 /* Which window is that in? */
23048 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23049
23050 /* If we were displaying active text in another window, clear that.
23051 Also clear if we move out of text area in same window. */
23052 if (! EQ (window, dpyinfo->mouse_face_window)
23053 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23054 && !NILP (dpyinfo->mouse_face_window)))
23055 clear_mouse_face (dpyinfo);
23056
23057 /* Not on a window -> return. */
23058 if (!WINDOWP (window))
23059 return;
23060
23061 /* Reset help_echo_string. It will get recomputed below. */
23062 help_echo_string = Qnil;
23063
23064 /* Convert to window-relative pixel coordinates. */
23065 w = XWINDOW (window);
23066 frame_to_window_pixel_xy (w, &x, &y);
23067
23068 /* Handle tool-bar window differently since it doesn't display a
23069 buffer. */
23070 if (EQ (window, f->tool_bar_window))
23071 {
23072 note_tool_bar_highlight (f, x, y);
23073 return;
23074 }
23075
23076 /* Mouse is on the mode, header line or margin? */
23077 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23078 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23079 {
23080 note_mode_line_or_margin_highlight (window, x, y, part);
23081 return;
23082 }
23083
23084 if (part == ON_VERTICAL_BORDER)
23085 {
23086 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23087 help_echo_string = build_string ("drag-mouse-1: resize");
23088 }
23089 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23090 || part == ON_SCROLL_BAR)
23091 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23092 else
23093 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23094
23095 /* Are we in a window whose display is up to date?
23096 And verify the buffer's text has not changed. */
23097 b = XBUFFER (w->buffer);
23098 if (part == ON_TEXT
23099 && EQ (w->window_end_valid, w->buffer)
23100 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23101 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23102 {
23103 int hpos, vpos, pos, i, dx, dy, area;
23104 struct glyph *glyph;
23105 Lisp_Object object;
23106 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23107 Lisp_Object *overlay_vec = NULL;
23108 int noverlays;
23109 struct buffer *obuf;
23110 int obegv, ozv, same_region;
23111
23112 /* Find the glyph under X/Y. */
23113 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23114
23115 /* Look for :pointer property on image. */
23116 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23117 {
23118 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23119 if (img != NULL && IMAGEP (img->spec))
23120 {
23121 Lisp_Object image_map, hotspot;
23122 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23123 !NILP (image_map))
23124 && (hotspot = find_hot_spot (image_map,
23125 glyph->slice.x + dx,
23126 glyph->slice.y + dy),
23127 CONSP (hotspot))
23128 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23129 {
23130 Lisp_Object area_id, plist;
23131
23132 area_id = XCAR (hotspot);
23133 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23134 If so, we could look for mouse-enter, mouse-leave
23135 properties in PLIST (and do something...). */
23136 hotspot = XCDR (hotspot);
23137 if (CONSP (hotspot)
23138 && (plist = XCAR (hotspot), CONSP (plist)))
23139 {
23140 pointer = Fplist_get (plist, Qpointer);
23141 if (NILP (pointer))
23142 pointer = Qhand;
23143 help_echo_string = Fplist_get (plist, Qhelp_echo);
23144 if (!NILP (help_echo_string))
23145 {
23146 help_echo_window = window;
23147 help_echo_object = glyph->object;
23148 help_echo_pos = glyph->charpos;
23149 }
23150 }
23151 }
23152 if (NILP (pointer))
23153 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23154 }
23155 }
23156
23157 /* Clear mouse face if X/Y not over text. */
23158 if (glyph == NULL
23159 || area != TEXT_AREA
23160 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23161 {
23162 if (clear_mouse_face (dpyinfo))
23163 cursor = No_Cursor;
23164 if (NILP (pointer))
23165 {
23166 if (area != TEXT_AREA)
23167 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23168 else
23169 pointer = Vvoid_text_area_pointer;
23170 }
23171 goto set_cursor;
23172 }
23173
23174 pos = glyph->charpos;
23175 object = glyph->object;
23176 if (!STRINGP (object) && !BUFFERP (object))
23177 goto set_cursor;
23178
23179 /* If we get an out-of-range value, return now; avoid an error. */
23180 if (BUFFERP (object) && pos > BUF_Z (b))
23181 goto set_cursor;
23182
23183 /* Make the window's buffer temporarily current for
23184 overlays_at and compute_char_face. */
23185 obuf = current_buffer;
23186 current_buffer = b;
23187 obegv = BEGV;
23188 ozv = ZV;
23189 BEGV = BEG;
23190 ZV = Z;
23191
23192 /* Is this char mouse-active or does it have help-echo? */
23193 position = make_number (pos);
23194
23195 if (BUFFERP (object))
23196 {
23197 /* Put all the overlays we want in a vector in overlay_vec. */
23198 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23199 /* Sort overlays into increasing priority order. */
23200 noverlays = sort_overlays (overlay_vec, noverlays, w);
23201 }
23202 else
23203 noverlays = 0;
23204
23205 same_region = (EQ (window, dpyinfo->mouse_face_window)
23206 && vpos >= dpyinfo->mouse_face_beg_row
23207 && vpos <= dpyinfo->mouse_face_end_row
23208 && (vpos > dpyinfo->mouse_face_beg_row
23209 || hpos >= dpyinfo->mouse_face_beg_col)
23210 && (vpos < dpyinfo->mouse_face_end_row
23211 || hpos < dpyinfo->mouse_face_end_col
23212 || dpyinfo->mouse_face_past_end));
23213
23214 if (same_region)
23215 cursor = No_Cursor;
23216
23217 /* Check mouse-face highlighting. */
23218 if (! same_region
23219 /* If there exists an overlay with mouse-face overlapping
23220 the one we are currently highlighting, we have to
23221 check if we enter the overlapping overlay, and then
23222 highlight only that. */
23223 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23224 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23225 {
23226 /* Find the highest priority overlay that has a mouse-face
23227 property. */
23228 overlay = Qnil;
23229 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23230 {
23231 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23232 if (!NILP (mouse_face))
23233 overlay = overlay_vec[i];
23234 }
23235
23236 /* If we're actually highlighting the same overlay as
23237 before, there's no need to do that again. */
23238 if (!NILP (overlay)
23239 && EQ (overlay, dpyinfo->mouse_face_overlay))
23240 goto check_help_echo;
23241
23242 dpyinfo->mouse_face_overlay = overlay;
23243
23244 /* Clear the display of the old active region, if any. */
23245 if (clear_mouse_face (dpyinfo))
23246 cursor = No_Cursor;
23247
23248 /* If no overlay applies, get a text property. */
23249 if (NILP (overlay))
23250 mouse_face = Fget_text_property (position, Qmouse_face, object);
23251
23252 /* Handle the overlay case. */
23253 if (!NILP (overlay))
23254 {
23255 /* Find the range of text around this char that
23256 should be active. */
23257 Lisp_Object before, after;
23258 int ignore;
23259
23260 before = Foverlay_start (overlay);
23261 after = Foverlay_end (overlay);
23262 /* Record this as the current active region. */
23263 fast_find_position (w, XFASTINT (before),
23264 &dpyinfo->mouse_face_beg_col,
23265 &dpyinfo->mouse_face_beg_row,
23266 &dpyinfo->mouse_face_beg_x,
23267 &dpyinfo->mouse_face_beg_y, Qnil);
23268
23269 dpyinfo->mouse_face_past_end
23270 = !fast_find_position (w, XFASTINT (after),
23271 &dpyinfo->mouse_face_end_col,
23272 &dpyinfo->mouse_face_end_row,
23273 &dpyinfo->mouse_face_end_x,
23274 &dpyinfo->mouse_face_end_y, Qnil);
23275 dpyinfo->mouse_face_window = window;
23276
23277 dpyinfo->mouse_face_face_id
23278 = face_at_buffer_position (w, pos, 0, 0,
23279 &ignore, pos + 1,
23280 !dpyinfo->mouse_face_hidden);
23281
23282 /* Display it as active. */
23283 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23284 cursor = No_Cursor;
23285 }
23286 /* Handle the text property case. */
23287 else if (!NILP (mouse_face) && BUFFERP (object))
23288 {
23289 /* Find the range of text around this char that
23290 should be active. */
23291 Lisp_Object before, after, beginning, end;
23292 int ignore;
23293
23294 beginning = Fmarker_position (w->start);
23295 end = make_number (BUF_Z (XBUFFER (object))
23296 - XFASTINT (w->window_end_pos));
23297 before
23298 = Fprevious_single_property_change (make_number (pos + 1),
23299 Qmouse_face,
23300 object, beginning);
23301 after
23302 = Fnext_single_property_change (position, Qmouse_face,
23303 object, end);
23304
23305 /* Record this as the current active region. */
23306 fast_find_position (w, XFASTINT (before),
23307 &dpyinfo->mouse_face_beg_col,
23308 &dpyinfo->mouse_face_beg_row,
23309 &dpyinfo->mouse_face_beg_x,
23310 &dpyinfo->mouse_face_beg_y, Qnil);
23311 dpyinfo->mouse_face_past_end
23312 = !fast_find_position (w, XFASTINT (after),
23313 &dpyinfo->mouse_face_end_col,
23314 &dpyinfo->mouse_face_end_row,
23315 &dpyinfo->mouse_face_end_x,
23316 &dpyinfo->mouse_face_end_y, Qnil);
23317 dpyinfo->mouse_face_window = window;
23318
23319 if (BUFFERP (object))
23320 dpyinfo->mouse_face_face_id
23321 = face_at_buffer_position (w, pos, 0, 0,
23322 &ignore, pos + 1,
23323 !dpyinfo->mouse_face_hidden);
23324
23325 /* Display it as active. */
23326 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23327 cursor = No_Cursor;
23328 }
23329 else if (!NILP (mouse_face) && STRINGP (object))
23330 {
23331 Lisp_Object b, e;
23332 int ignore;
23333
23334 b = Fprevious_single_property_change (make_number (pos + 1),
23335 Qmouse_face,
23336 object, Qnil);
23337 e = Fnext_single_property_change (position, Qmouse_face,
23338 object, Qnil);
23339 if (NILP (b))
23340 b = make_number (0);
23341 if (NILP (e))
23342 e = make_number (SCHARS (object) - 1);
23343
23344 fast_find_string_pos (w, XINT (b), object,
23345 &dpyinfo->mouse_face_beg_col,
23346 &dpyinfo->mouse_face_beg_row,
23347 &dpyinfo->mouse_face_beg_x,
23348 &dpyinfo->mouse_face_beg_y, 0);
23349 fast_find_string_pos (w, XINT (e), object,
23350 &dpyinfo->mouse_face_end_col,
23351 &dpyinfo->mouse_face_end_row,
23352 &dpyinfo->mouse_face_end_x,
23353 &dpyinfo->mouse_face_end_y, 1);
23354 dpyinfo->mouse_face_past_end = 0;
23355 dpyinfo->mouse_face_window = window;
23356 dpyinfo->mouse_face_face_id
23357 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23358 glyph->face_id, 1);
23359 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23360 cursor = No_Cursor;
23361 }
23362 else if (STRINGP (object) && NILP (mouse_face))
23363 {
23364 /* A string which doesn't have mouse-face, but
23365 the text ``under'' it might have. */
23366 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23367 int start = MATRIX_ROW_START_CHARPOS (r);
23368
23369 pos = string_buffer_position (w, object, start);
23370 if (pos > 0)
23371 mouse_face = get_char_property_and_overlay (make_number (pos),
23372 Qmouse_face,
23373 w->buffer,
23374 &overlay);
23375 if (!NILP (mouse_face) && !NILP (overlay))
23376 {
23377 Lisp_Object before = Foverlay_start (overlay);
23378 Lisp_Object after = Foverlay_end (overlay);
23379 int ignore;
23380
23381 /* Note that we might not be able to find position
23382 BEFORE in the glyph matrix if the overlay is
23383 entirely covered by a `display' property. In
23384 this case, we overshoot. So let's stop in
23385 the glyph matrix before glyphs for OBJECT. */
23386 fast_find_position (w, XFASTINT (before),
23387 &dpyinfo->mouse_face_beg_col,
23388 &dpyinfo->mouse_face_beg_row,
23389 &dpyinfo->mouse_face_beg_x,
23390 &dpyinfo->mouse_face_beg_y,
23391 object);
23392
23393 dpyinfo->mouse_face_past_end
23394 = !fast_find_position (w, XFASTINT (after),
23395 &dpyinfo->mouse_face_end_col,
23396 &dpyinfo->mouse_face_end_row,
23397 &dpyinfo->mouse_face_end_x,
23398 &dpyinfo->mouse_face_end_y,
23399 Qnil);
23400 dpyinfo->mouse_face_window = window;
23401 dpyinfo->mouse_face_face_id
23402 = face_at_buffer_position (w, pos, 0, 0,
23403 &ignore, pos + 1,
23404 !dpyinfo->mouse_face_hidden);
23405
23406 /* Display it as active. */
23407 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23408 cursor = No_Cursor;
23409 }
23410 }
23411 }
23412
23413 check_help_echo:
23414
23415 /* Look for a `help-echo' property. */
23416 if (NILP (help_echo_string)) {
23417 Lisp_Object help, overlay;
23418
23419 /* Check overlays first. */
23420 help = overlay = Qnil;
23421 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23422 {
23423 overlay = overlay_vec[i];
23424 help = Foverlay_get (overlay, Qhelp_echo);
23425 }
23426
23427 if (!NILP (help))
23428 {
23429 help_echo_string = help;
23430 help_echo_window = window;
23431 help_echo_object = overlay;
23432 help_echo_pos = pos;
23433 }
23434 else
23435 {
23436 Lisp_Object object = glyph->object;
23437 int charpos = glyph->charpos;
23438
23439 /* Try text properties. */
23440 if (STRINGP (object)
23441 && charpos >= 0
23442 && charpos < SCHARS (object))
23443 {
23444 help = Fget_text_property (make_number (charpos),
23445 Qhelp_echo, object);
23446 if (NILP (help))
23447 {
23448 /* If the string itself doesn't specify a help-echo,
23449 see if the buffer text ``under'' it does. */
23450 struct glyph_row *r
23451 = MATRIX_ROW (w->current_matrix, vpos);
23452 int start = MATRIX_ROW_START_CHARPOS (r);
23453 int pos = string_buffer_position (w, object, start);
23454 if (pos > 0)
23455 {
23456 help = Fget_char_property (make_number (pos),
23457 Qhelp_echo, w->buffer);
23458 if (!NILP (help))
23459 {
23460 charpos = pos;
23461 object = w->buffer;
23462 }
23463 }
23464 }
23465 }
23466 else if (BUFFERP (object)
23467 && charpos >= BEGV
23468 && charpos < ZV)
23469 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23470 object);
23471
23472 if (!NILP (help))
23473 {
23474 help_echo_string = help;
23475 help_echo_window = window;
23476 help_echo_object = object;
23477 help_echo_pos = charpos;
23478 }
23479 }
23480 }
23481
23482 /* Look for a `pointer' property. */
23483 if (NILP (pointer))
23484 {
23485 /* Check overlays first. */
23486 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23487 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23488
23489 if (NILP (pointer))
23490 {
23491 Lisp_Object object = glyph->object;
23492 int charpos = glyph->charpos;
23493
23494 /* Try text properties. */
23495 if (STRINGP (object)
23496 && charpos >= 0
23497 && charpos < SCHARS (object))
23498 {
23499 pointer = Fget_text_property (make_number (charpos),
23500 Qpointer, object);
23501 if (NILP (pointer))
23502 {
23503 /* If the string itself doesn't specify a pointer,
23504 see if the buffer text ``under'' it does. */
23505 struct glyph_row *r
23506 = MATRIX_ROW (w->current_matrix, vpos);
23507 int start = MATRIX_ROW_START_CHARPOS (r);
23508 int pos = string_buffer_position (w, object, start);
23509 if (pos > 0)
23510 pointer = Fget_char_property (make_number (pos),
23511 Qpointer, w->buffer);
23512 }
23513 }
23514 else if (BUFFERP (object)
23515 && charpos >= BEGV
23516 && charpos < ZV)
23517 pointer = Fget_text_property (make_number (charpos),
23518 Qpointer, object);
23519 }
23520 }
23521
23522 BEGV = obegv;
23523 ZV = ozv;
23524 current_buffer = obuf;
23525 }
23526
23527 set_cursor:
23528
23529 define_frame_cursor1 (f, cursor, pointer);
23530 }
23531
23532
23533 /* EXPORT for RIF:
23534 Clear any mouse-face on window W. This function is part of the
23535 redisplay interface, and is called from try_window_id and similar
23536 functions to ensure the mouse-highlight is off. */
23537
23538 void
23539 x_clear_window_mouse_face (w)
23540 struct window *w;
23541 {
23542 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23543 Lisp_Object window;
23544
23545 BLOCK_INPUT;
23546 XSETWINDOW (window, w);
23547 if (EQ (window, dpyinfo->mouse_face_window))
23548 clear_mouse_face (dpyinfo);
23549 UNBLOCK_INPUT;
23550 }
23551
23552
23553 /* EXPORT:
23554 Just discard the mouse face information for frame F, if any.
23555 This is used when the size of F is changed. */
23556
23557 void
23558 cancel_mouse_face (f)
23559 struct frame *f;
23560 {
23561 Lisp_Object window;
23562 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23563
23564 window = dpyinfo->mouse_face_window;
23565 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23566 {
23567 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23568 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23569 dpyinfo->mouse_face_window = Qnil;
23570 }
23571 }
23572
23573
23574 #endif /* HAVE_WINDOW_SYSTEM */
23575
23576 \f
23577 /***********************************************************************
23578 Exposure Events
23579 ***********************************************************************/
23580
23581 #ifdef HAVE_WINDOW_SYSTEM
23582
23583 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23584 which intersects rectangle R. R is in window-relative coordinates. */
23585
23586 static void
23587 expose_area (w, row, r, area)
23588 struct window *w;
23589 struct glyph_row *row;
23590 XRectangle *r;
23591 enum glyph_row_area area;
23592 {
23593 struct glyph *first = row->glyphs[area];
23594 struct glyph *end = row->glyphs[area] + row->used[area];
23595 struct glyph *last;
23596 int first_x, start_x, x;
23597
23598 if (area == TEXT_AREA && row->fill_line_p)
23599 /* If row extends face to end of line write the whole line. */
23600 draw_glyphs (w, 0, row, area,
23601 0, row->used[area],
23602 DRAW_NORMAL_TEXT, 0);
23603 else
23604 {
23605 /* Set START_X to the window-relative start position for drawing glyphs of
23606 AREA. The first glyph of the text area can be partially visible.
23607 The first glyphs of other areas cannot. */
23608 start_x = window_box_left_offset (w, area);
23609 x = start_x;
23610 if (area == TEXT_AREA)
23611 x += row->x;
23612
23613 /* Find the first glyph that must be redrawn. */
23614 while (first < end
23615 && x + first->pixel_width < r->x)
23616 {
23617 x += first->pixel_width;
23618 ++first;
23619 }
23620
23621 /* Find the last one. */
23622 last = first;
23623 first_x = x;
23624 while (last < end
23625 && x < r->x + r->width)
23626 {
23627 x += last->pixel_width;
23628 ++last;
23629 }
23630
23631 /* Repaint. */
23632 if (last > first)
23633 draw_glyphs (w, first_x - start_x, row, area,
23634 first - row->glyphs[area], last - row->glyphs[area],
23635 DRAW_NORMAL_TEXT, 0);
23636 }
23637 }
23638
23639
23640 /* Redraw the parts of the glyph row ROW on window W intersecting
23641 rectangle R. R is in window-relative coordinates. Value is
23642 non-zero if mouse-face was overwritten. */
23643
23644 static int
23645 expose_line (w, row, r)
23646 struct window *w;
23647 struct glyph_row *row;
23648 XRectangle *r;
23649 {
23650 xassert (row->enabled_p);
23651
23652 if (row->mode_line_p || w->pseudo_window_p)
23653 draw_glyphs (w, 0, row, TEXT_AREA,
23654 0, row->used[TEXT_AREA],
23655 DRAW_NORMAL_TEXT, 0);
23656 else
23657 {
23658 if (row->used[LEFT_MARGIN_AREA])
23659 expose_area (w, row, r, LEFT_MARGIN_AREA);
23660 if (row->used[TEXT_AREA])
23661 expose_area (w, row, r, TEXT_AREA);
23662 if (row->used[RIGHT_MARGIN_AREA])
23663 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23664 draw_row_fringe_bitmaps (w, row);
23665 }
23666
23667 return row->mouse_face_p;
23668 }
23669
23670
23671 /* Redraw those parts of glyphs rows during expose event handling that
23672 overlap other rows. Redrawing of an exposed line writes over parts
23673 of lines overlapping that exposed line; this function fixes that.
23674
23675 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23676 row in W's current matrix that is exposed and overlaps other rows.
23677 LAST_OVERLAPPING_ROW is the last such row. */
23678
23679 static void
23680 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23681 struct window *w;
23682 struct glyph_row *first_overlapping_row;
23683 struct glyph_row *last_overlapping_row;
23684 {
23685 struct glyph_row *row;
23686
23687 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23688 if (row->overlapping_p)
23689 {
23690 xassert (row->enabled_p && !row->mode_line_p);
23691
23692 if (row->used[LEFT_MARGIN_AREA])
23693 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23694
23695 if (row->used[TEXT_AREA])
23696 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23697
23698 if (row->used[RIGHT_MARGIN_AREA])
23699 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23700 }
23701 }
23702
23703
23704 /* Return non-zero if W's cursor intersects rectangle R. */
23705
23706 static int
23707 phys_cursor_in_rect_p (w, r)
23708 struct window *w;
23709 XRectangle *r;
23710 {
23711 XRectangle cr, result;
23712 struct glyph *cursor_glyph;
23713
23714 cursor_glyph = get_phys_cursor_glyph (w);
23715 if (cursor_glyph)
23716 {
23717 /* r is relative to W's box, but w->phys_cursor.x is relative
23718 to left edge of W's TEXT area. Adjust it. */
23719 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23720 cr.y = w->phys_cursor.y;
23721 cr.width = cursor_glyph->pixel_width;
23722 cr.height = w->phys_cursor_height;
23723 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23724 I assume the effect is the same -- and this is portable. */
23725 return x_intersect_rectangles (&cr, r, &result);
23726 }
23727 else
23728 return 0;
23729 }
23730
23731
23732 /* EXPORT:
23733 Draw a vertical window border to the right of window W if W doesn't
23734 have vertical scroll bars. */
23735
23736 void
23737 x_draw_vertical_border (w)
23738 struct window *w;
23739 {
23740 /* We could do better, if we knew what type of scroll-bar the adjacent
23741 windows (on either side) have... But we don't :-(
23742 However, I think this works ok. ++KFS 2003-04-25 */
23743
23744 /* Redraw borders between horizontally adjacent windows. Don't
23745 do it for frames with vertical scroll bars because either the
23746 right scroll bar of a window, or the left scroll bar of its
23747 neighbor will suffice as a border. */
23748 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23749 return;
23750
23751 if (!WINDOW_RIGHTMOST_P (w)
23752 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23753 {
23754 int x0, x1, y0, y1;
23755
23756 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23757 y1 -= 1;
23758
23759 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23760 x1 -= 1;
23761
23762 rif->draw_vertical_window_border (w, x1, y0, y1);
23763 }
23764 else if (!WINDOW_LEFTMOST_P (w)
23765 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23766 {
23767 int x0, x1, y0, y1;
23768
23769 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23770 y1 -= 1;
23771
23772 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23773 x0 -= 1;
23774
23775 rif->draw_vertical_window_border (w, x0, y0, y1);
23776 }
23777 }
23778
23779
23780 /* Redraw the part of window W intersection rectangle FR. Pixel
23781 coordinates in FR are frame-relative. Call this function with
23782 input blocked. Value is non-zero if the exposure overwrites
23783 mouse-face. */
23784
23785 static int
23786 expose_window (w, fr)
23787 struct window *w;
23788 XRectangle *fr;
23789 {
23790 struct frame *f = XFRAME (w->frame);
23791 XRectangle wr, r;
23792 int mouse_face_overwritten_p = 0;
23793
23794 /* If window is not yet fully initialized, do nothing. This can
23795 happen when toolkit scroll bars are used and a window is split.
23796 Reconfiguring the scroll bar will generate an expose for a newly
23797 created window. */
23798 if (w->current_matrix == NULL)
23799 return 0;
23800
23801 /* When we're currently updating the window, display and current
23802 matrix usually don't agree. Arrange for a thorough display
23803 later. */
23804 if (w == updated_window)
23805 {
23806 SET_FRAME_GARBAGED (f);
23807 return 0;
23808 }
23809
23810 /* Frame-relative pixel rectangle of W. */
23811 wr.x = WINDOW_LEFT_EDGE_X (w);
23812 wr.y = WINDOW_TOP_EDGE_Y (w);
23813 wr.width = WINDOW_TOTAL_WIDTH (w);
23814 wr.height = WINDOW_TOTAL_HEIGHT (w);
23815
23816 if (x_intersect_rectangles (fr, &wr, &r))
23817 {
23818 int yb = window_text_bottom_y (w);
23819 struct glyph_row *row;
23820 int cursor_cleared_p;
23821 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23822
23823 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23824 r.x, r.y, r.width, r.height));
23825
23826 /* Convert to window coordinates. */
23827 r.x -= WINDOW_LEFT_EDGE_X (w);
23828 r.y -= WINDOW_TOP_EDGE_Y (w);
23829
23830 /* Turn off the cursor. */
23831 if (!w->pseudo_window_p
23832 && phys_cursor_in_rect_p (w, &r))
23833 {
23834 x_clear_cursor (w);
23835 cursor_cleared_p = 1;
23836 }
23837 else
23838 cursor_cleared_p = 0;
23839
23840 /* Update lines intersecting rectangle R. */
23841 first_overlapping_row = last_overlapping_row = NULL;
23842 for (row = w->current_matrix->rows;
23843 row->enabled_p;
23844 ++row)
23845 {
23846 int y0 = row->y;
23847 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23848
23849 if ((y0 >= r.y && y0 < r.y + r.height)
23850 || (y1 > r.y && y1 < r.y + r.height)
23851 || (r.y >= y0 && r.y < y1)
23852 || (r.y + r.height > y0 && r.y + r.height < y1))
23853 {
23854 /* A header line may be overlapping, but there is no need
23855 to fix overlapping areas for them. KFS 2005-02-12 */
23856 if (row->overlapping_p && !row->mode_line_p)
23857 {
23858 if (first_overlapping_row == NULL)
23859 first_overlapping_row = row;
23860 last_overlapping_row = row;
23861 }
23862
23863 if (expose_line (w, row, &r))
23864 mouse_face_overwritten_p = 1;
23865 }
23866
23867 if (y1 >= yb)
23868 break;
23869 }
23870
23871 /* Display the mode line if there is one. */
23872 if (WINDOW_WANTS_MODELINE_P (w)
23873 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23874 row->enabled_p)
23875 && row->y < r.y + r.height)
23876 {
23877 if (expose_line (w, row, &r))
23878 mouse_face_overwritten_p = 1;
23879 }
23880
23881 if (!w->pseudo_window_p)
23882 {
23883 /* Fix the display of overlapping rows. */
23884 if (first_overlapping_row)
23885 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23886
23887 /* Draw border between windows. */
23888 x_draw_vertical_border (w);
23889
23890 /* Turn the cursor on again. */
23891 if (cursor_cleared_p)
23892 update_window_cursor (w, 1);
23893 }
23894 }
23895
23896 return mouse_face_overwritten_p;
23897 }
23898
23899
23900
23901 /* Redraw (parts) of all windows in the window tree rooted at W that
23902 intersect R. R contains frame pixel coordinates. Value is
23903 non-zero if the exposure overwrites mouse-face. */
23904
23905 static int
23906 expose_window_tree (w, r)
23907 struct window *w;
23908 XRectangle *r;
23909 {
23910 struct frame *f = XFRAME (w->frame);
23911 int mouse_face_overwritten_p = 0;
23912
23913 while (w && !FRAME_GARBAGED_P (f))
23914 {
23915 if (!NILP (w->hchild))
23916 mouse_face_overwritten_p
23917 |= expose_window_tree (XWINDOW (w->hchild), r);
23918 else if (!NILP (w->vchild))
23919 mouse_face_overwritten_p
23920 |= expose_window_tree (XWINDOW (w->vchild), r);
23921 else
23922 mouse_face_overwritten_p |= expose_window (w, r);
23923
23924 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23925 }
23926
23927 return mouse_face_overwritten_p;
23928 }
23929
23930
23931 /* EXPORT:
23932 Redisplay an exposed area of frame F. X and Y are the upper-left
23933 corner of the exposed rectangle. W and H are width and height of
23934 the exposed area. All are pixel values. W or H zero means redraw
23935 the entire frame. */
23936
23937 void
23938 expose_frame (f, x, y, w, h)
23939 struct frame *f;
23940 int x, y, w, h;
23941 {
23942 XRectangle r;
23943 int mouse_face_overwritten_p = 0;
23944
23945 TRACE ((stderr, "expose_frame "));
23946
23947 /* No need to redraw if frame will be redrawn soon. */
23948 if (FRAME_GARBAGED_P (f))
23949 {
23950 TRACE ((stderr, " garbaged\n"));
23951 return;
23952 }
23953
23954 /* If basic faces haven't been realized yet, there is no point in
23955 trying to redraw anything. This can happen when we get an expose
23956 event while Emacs is starting, e.g. by moving another window. */
23957 if (FRAME_FACE_CACHE (f) == NULL
23958 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23959 {
23960 TRACE ((stderr, " no faces\n"));
23961 return;
23962 }
23963
23964 if (w == 0 || h == 0)
23965 {
23966 r.x = r.y = 0;
23967 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23968 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23969 }
23970 else
23971 {
23972 r.x = x;
23973 r.y = y;
23974 r.width = w;
23975 r.height = h;
23976 }
23977
23978 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23979 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23980
23981 if (WINDOWP (f->tool_bar_window))
23982 mouse_face_overwritten_p
23983 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23984
23985 #ifdef HAVE_X_WINDOWS
23986 #ifndef MSDOS
23987 #ifndef USE_X_TOOLKIT
23988 if (WINDOWP (f->menu_bar_window))
23989 mouse_face_overwritten_p
23990 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23991 #endif /* not USE_X_TOOLKIT */
23992 #endif
23993 #endif
23994
23995 /* Some window managers support a focus-follows-mouse style with
23996 delayed raising of frames. Imagine a partially obscured frame,
23997 and moving the mouse into partially obscured mouse-face on that
23998 frame. The visible part of the mouse-face will be highlighted,
23999 then the WM raises the obscured frame. With at least one WM, KDE
24000 2.1, Emacs is not getting any event for the raising of the frame
24001 (even tried with SubstructureRedirectMask), only Expose events.
24002 These expose events will draw text normally, i.e. not
24003 highlighted. Which means we must redo the highlight here.
24004 Subsume it under ``we love X''. --gerd 2001-08-15 */
24005 /* Included in Windows version because Windows most likely does not
24006 do the right thing if any third party tool offers
24007 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24008 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24009 {
24010 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24011 if (f == dpyinfo->mouse_face_mouse_frame)
24012 {
24013 int x = dpyinfo->mouse_face_mouse_x;
24014 int y = dpyinfo->mouse_face_mouse_y;
24015 clear_mouse_face (dpyinfo);
24016 note_mouse_highlight (f, x, y);
24017 }
24018 }
24019 }
24020
24021
24022 /* EXPORT:
24023 Determine the intersection of two rectangles R1 and R2. Return
24024 the intersection in *RESULT. Value is non-zero if RESULT is not
24025 empty. */
24026
24027 int
24028 x_intersect_rectangles (r1, r2, result)
24029 XRectangle *r1, *r2, *result;
24030 {
24031 XRectangle *left, *right;
24032 XRectangle *upper, *lower;
24033 int intersection_p = 0;
24034
24035 /* Rearrange so that R1 is the left-most rectangle. */
24036 if (r1->x < r2->x)
24037 left = r1, right = r2;
24038 else
24039 left = r2, right = r1;
24040
24041 /* X0 of the intersection is right.x0, if this is inside R1,
24042 otherwise there is no intersection. */
24043 if (right->x <= left->x + left->width)
24044 {
24045 result->x = right->x;
24046
24047 /* The right end of the intersection is the minimum of the
24048 the right ends of left and right. */
24049 result->width = (min (left->x + left->width, right->x + right->width)
24050 - result->x);
24051
24052 /* Same game for Y. */
24053 if (r1->y < r2->y)
24054 upper = r1, lower = r2;
24055 else
24056 upper = r2, lower = r1;
24057
24058 /* The upper end of the intersection is lower.y0, if this is inside
24059 of upper. Otherwise, there is no intersection. */
24060 if (lower->y <= upper->y + upper->height)
24061 {
24062 result->y = lower->y;
24063
24064 /* The lower end of the intersection is the minimum of the lower
24065 ends of upper and lower. */
24066 result->height = (min (lower->y + lower->height,
24067 upper->y + upper->height)
24068 - result->y);
24069 intersection_p = 1;
24070 }
24071 }
24072
24073 return intersection_p;
24074 }
24075
24076 #endif /* HAVE_WINDOW_SYSTEM */
24077
24078 \f
24079 /***********************************************************************
24080 Initialization
24081 ***********************************************************************/
24082
24083 void
24084 syms_of_xdisp ()
24085 {
24086 Vwith_echo_area_save_vector = Qnil;
24087 staticpro (&Vwith_echo_area_save_vector);
24088
24089 Vmessage_stack = Qnil;
24090 staticpro (&Vmessage_stack);
24091
24092 Qinhibit_redisplay = intern ("inhibit-redisplay");
24093 staticpro (&Qinhibit_redisplay);
24094
24095 message_dolog_marker1 = Fmake_marker ();
24096 staticpro (&message_dolog_marker1);
24097 message_dolog_marker2 = Fmake_marker ();
24098 staticpro (&message_dolog_marker2);
24099 message_dolog_marker3 = Fmake_marker ();
24100 staticpro (&message_dolog_marker3);
24101
24102 #if GLYPH_DEBUG
24103 defsubr (&Sdump_frame_glyph_matrix);
24104 defsubr (&Sdump_glyph_matrix);
24105 defsubr (&Sdump_glyph_row);
24106 defsubr (&Sdump_tool_bar_row);
24107 defsubr (&Strace_redisplay);
24108 defsubr (&Strace_to_stderr);
24109 #endif
24110 #ifdef HAVE_WINDOW_SYSTEM
24111 defsubr (&Stool_bar_lines_needed);
24112 defsubr (&Slookup_image_map);
24113 #endif
24114 defsubr (&Sformat_mode_line);
24115
24116 staticpro (&Qmenu_bar_update_hook);
24117 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24118
24119 staticpro (&Qoverriding_terminal_local_map);
24120 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24121
24122 staticpro (&Qoverriding_local_map);
24123 Qoverriding_local_map = intern ("overriding-local-map");
24124
24125 staticpro (&Qwindow_scroll_functions);
24126 Qwindow_scroll_functions = intern ("window-scroll-functions");
24127
24128 staticpro (&Qredisplay_end_trigger_functions);
24129 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24130
24131 staticpro (&Qinhibit_point_motion_hooks);
24132 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24133
24134 QCdata = intern (":data");
24135 staticpro (&QCdata);
24136 Qdisplay = intern ("display");
24137 staticpro (&Qdisplay);
24138 Qspace_width = intern ("space-width");
24139 staticpro (&Qspace_width);
24140 Qraise = intern ("raise");
24141 staticpro (&Qraise);
24142 Qslice = intern ("slice");
24143 staticpro (&Qslice);
24144 Qspace = intern ("space");
24145 staticpro (&Qspace);
24146 Qmargin = intern ("margin");
24147 staticpro (&Qmargin);
24148 Qpointer = intern ("pointer");
24149 staticpro (&Qpointer);
24150 Qleft_margin = intern ("left-margin");
24151 staticpro (&Qleft_margin);
24152 Qright_margin = intern ("right-margin");
24153 staticpro (&Qright_margin);
24154 Qcenter = intern ("center");
24155 staticpro (&Qcenter);
24156 Qline_height = intern ("line-height");
24157 staticpro (&Qline_height);
24158 QCalign_to = intern (":align-to");
24159 staticpro (&QCalign_to);
24160 QCrelative_width = intern (":relative-width");
24161 staticpro (&QCrelative_width);
24162 QCrelative_height = intern (":relative-height");
24163 staticpro (&QCrelative_height);
24164 QCeval = intern (":eval");
24165 staticpro (&QCeval);
24166 QCpropertize = intern (":propertize");
24167 staticpro (&QCpropertize);
24168 QCfile = intern (":file");
24169 staticpro (&QCfile);
24170 Qfontified = intern ("fontified");
24171 staticpro (&Qfontified);
24172 Qfontification_functions = intern ("fontification-functions");
24173 staticpro (&Qfontification_functions);
24174 Qtrailing_whitespace = intern ("trailing-whitespace");
24175 staticpro (&Qtrailing_whitespace);
24176 Qescape_glyph = intern ("escape-glyph");
24177 staticpro (&Qescape_glyph);
24178 Qnobreak_space = intern ("nobreak-space");
24179 staticpro (&Qnobreak_space);
24180 Qimage = intern ("image");
24181 staticpro (&Qimage);
24182 QCmap = intern (":map");
24183 staticpro (&QCmap);
24184 QCpointer = intern (":pointer");
24185 staticpro (&QCpointer);
24186 Qrect = intern ("rect");
24187 staticpro (&Qrect);
24188 Qcircle = intern ("circle");
24189 staticpro (&Qcircle);
24190 Qpoly = intern ("poly");
24191 staticpro (&Qpoly);
24192 Qmessage_truncate_lines = intern ("message-truncate-lines");
24193 staticpro (&Qmessage_truncate_lines);
24194 Qgrow_only = intern ("grow-only");
24195 staticpro (&Qgrow_only);
24196 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24197 staticpro (&Qinhibit_menubar_update);
24198 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24199 staticpro (&Qinhibit_eval_during_redisplay);
24200 Qposition = intern ("position");
24201 staticpro (&Qposition);
24202 Qbuffer_position = intern ("buffer-position");
24203 staticpro (&Qbuffer_position);
24204 Qobject = intern ("object");
24205 staticpro (&Qobject);
24206 Qbar = intern ("bar");
24207 staticpro (&Qbar);
24208 Qhbar = intern ("hbar");
24209 staticpro (&Qhbar);
24210 Qbox = intern ("box");
24211 staticpro (&Qbox);
24212 Qhollow = intern ("hollow");
24213 staticpro (&Qhollow);
24214 Qhand = intern ("hand");
24215 staticpro (&Qhand);
24216 Qarrow = intern ("arrow");
24217 staticpro (&Qarrow);
24218 Qtext = intern ("text");
24219 staticpro (&Qtext);
24220 Qrisky_local_variable = intern ("risky-local-variable");
24221 staticpro (&Qrisky_local_variable);
24222 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24223 staticpro (&Qinhibit_free_realized_faces);
24224
24225 list_of_error = Fcons (Fcons (intern ("error"),
24226 Fcons (intern ("void-variable"), Qnil)),
24227 Qnil);
24228 staticpro (&list_of_error);
24229
24230 Qlast_arrow_position = intern ("last-arrow-position");
24231 staticpro (&Qlast_arrow_position);
24232 Qlast_arrow_string = intern ("last-arrow-string");
24233 staticpro (&Qlast_arrow_string);
24234
24235 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24236 staticpro (&Qoverlay_arrow_string);
24237 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24238 staticpro (&Qoverlay_arrow_bitmap);
24239
24240 echo_buffer[0] = echo_buffer[1] = Qnil;
24241 staticpro (&echo_buffer[0]);
24242 staticpro (&echo_buffer[1]);
24243
24244 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24245 staticpro (&echo_area_buffer[0]);
24246 staticpro (&echo_area_buffer[1]);
24247
24248 Vmessages_buffer_name = build_string ("*Messages*");
24249 staticpro (&Vmessages_buffer_name);
24250
24251 mode_line_proptrans_alist = Qnil;
24252 staticpro (&mode_line_proptrans_alist);
24253 mode_line_string_list = Qnil;
24254 staticpro (&mode_line_string_list);
24255 mode_line_string_face = Qnil;
24256 staticpro (&mode_line_string_face);
24257 mode_line_string_face_prop = Qnil;
24258 staticpro (&mode_line_string_face_prop);
24259 Vmode_line_unwind_vector = Qnil;
24260 staticpro (&Vmode_line_unwind_vector);
24261
24262 help_echo_string = Qnil;
24263 staticpro (&help_echo_string);
24264 help_echo_object = Qnil;
24265 staticpro (&help_echo_object);
24266 help_echo_window = Qnil;
24267 staticpro (&help_echo_window);
24268 previous_help_echo_string = Qnil;
24269 staticpro (&previous_help_echo_string);
24270 help_echo_pos = -1;
24271
24272 #ifdef HAVE_WINDOW_SYSTEM
24273 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24274 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24275 For example, if a block cursor is over a tab, it will be drawn as
24276 wide as that tab on the display. */);
24277 x_stretch_cursor_p = 0;
24278 #endif
24279
24280 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24281 doc: /* *Non-nil means highlight trailing whitespace.
24282 The face used for trailing whitespace is `trailing-whitespace'. */);
24283 Vshow_trailing_whitespace = Qnil;
24284
24285 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24286 doc: /* *Control highlighting of nobreak space and soft hyphen.
24287 A value of t means highlight the character itself (for nobreak space,
24288 use face `nobreak-space').
24289 A value of nil means no highlighting.
24290 Other values mean display the escape glyph followed by an ordinary
24291 space or ordinary hyphen. */);
24292 Vnobreak_char_display = Qt;
24293
24294 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24295 doc: /* *The pointer shape to show in void text areas.
24296 A value of nil means to show the text pointer. Other options are `arrow',
24297 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24298 Vvoid_text_area_pointer = Qarrow;
24299
24300 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24301 doc: /* Non-nil means don't actually do any redisplay.
24302 This is used for internal purposes. */);
24303 Vinhibit_redisplay = Qnil;
24304
24305 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24306 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24307 Vglobal_mode_string = Qnil;
24308
24309 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24310 doc: /* Marker for where to display an arrow on top of the buffer text.
24311 This must be the beginning of a line in order to work.
24312 See also `overlay-arrow-string'. */);
24313 Voverlay_arrow_position = Qnil;
24314
24315 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24316 doc: /* String to display as an arrow in non-window frames.
24317 See also `overlay-arrow-position'. */);
24318 Voverlay_arrow_string = build_string ("=>");
24319
24320 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24321 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24322 The symbols on this list are examined during redisplay to determine
24323 where to display overlay arrows. */);
24324 Voverlay_arrow_variable_list
24325 = Fcons (intern ("overlay-arrow-position"), Qnil);
24326
24327 DEFVAR_INT ("scroll-step", &scroll_step,
24328 doc: /* *The number of lines to try scrolling a window by when point moves out.
24329 If that fails to bring point back on frame, point is centered instead.
24330 If this is zero, point is always centered after it moves off frame.
24331 If you want scrolling to always be a line at a time, you should set
24332 `scroll-conservatively' to a large value rather than set this to 1. */);
24333
24334 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24335 doc: /* *Scroll up to this many lines, to bring point back on screen.
24336 A value of zero means to scroll the text to center point vertically
24337 in the window. */);
24338 scroll_conservatively = 0;
24339
24340 DEFVAR_INT ("scroll-margin", &scroll_margin,
24341 doc: /* *Number of lines of margin at the top and bottom of a window.
24342 Recenter the window whenever point gets within this many lines
24343 of the top or bottom of the window. */);
24344 scroll_margin = 0;
24345
24346 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24347 doc: /* Pixels per inch value for non-window system displays.
24348 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24349 Vdisplay_pixels_per_inch = make_float (72.0);
24350
24351 #if GLYPH_DEBUG
24352 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24353 #endif
24354
24355 DEFVAR_BOOL ("truncate-partial-width-windows",
24356 &truncate_partial_width_windows,
24357 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24358 truncate_partial_width_windows = 1;
24359
24360 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24361 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24362 Any other value means to use the appropriate face, `mode-line',
24363 `header-line', or `menu' respectively. */);
24364 mode_line_inverse_video = 1;
24365
24366 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24367 doc: /* *Maximum buffer size for which line number should be displayed.
24368 If the buffer is bigger than this, the line number does not appear
24369 in the mode line. A value of nil means no limit. */);
24370 Vline_number_display_limit = Qnil;
24371
24372 DEFVAR_INT ("line-number-display-limit-width",
24373 &line_number_display_limit_width,
24374 doc: /* *Maximum line width (in characters) for line number display.
24375 If the average length of the lines near point is bigger than this, then the
24376 line number may be omitted from the mode line. */);
24377 line_number_display_limit_width = 200;
24378
24379 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24380 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24381 highlight_nonselected_windows = 0;
24382
24383 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24384 doc: /* Non-nil if more than one frame is visible on this display.
24385 Minibuffer-only frames don't count, but iconified frames do.
24386 This variable is not guaranteed to be accurate except while processing
24387 `frame-title-format' and `icon-title-format'. */);
24388
24389 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24390 doc: /* Template for displaying the title bar of visible frames.
24391 \(Assuming the window manager supports this feature.)
24392
24393 This variable has the same structure as `mode-line-format', except that
24394 the %c and %l constructs are ignored. It is used only on frames for
24395 which no explicit name has been set \(see `modify-frame-parameters'). */);
24396
24397 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24398 doc: /* Template for displaying the title bar of an iconified frame.
24399 \(Assuming the window manager supports this feature.)
24400 This variable has the same structure as `mode-line-format' (which see),
24401 and is used only on frames for which no explicit name has been set
24402 \(see `modify-frame-parameters'). */);
24403 Vicon_title_format
24404 = Vframe_title_format
24405 = Fcons (intern ("multiple-frames"),
24406 Fcons (build_string ("%b"),
24407 Fcons (Fcons (empty_unibyte_string,
24408 Fcons (intern ("invocation-name"),
24409 Fcons (build_string ("@"),
24410 Fcons (intern ("system-name"),
24411 Qnil)))),
24412 Qnil)));
24413
24414 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24415 doc: /* Maximum number of lines to keep in the message log buffer.
24416 If nil, disable message logging. If t, log messages but don't truncate
24417 the buffer when it becomes large. */);
24418 Vmessage_log_max = make_number (100);
24419
24420 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24421 doc: /* Functions called before redisplay, if window sizes have changed.
24422 The value should be a list of functions that take one argument.
24423 Just before redisplay, for each frame, if any of its windows have changed
24424 size since the last redisplay, or have been split or deleted,
24425 all the functions in the list are called, with the frame as argument. */);
24426 Vwindow_size_change_functions = Qnil;
24427
24428 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24429 doc: /* List of functions to call before redisplaying a window with scrolling.
24430 Each function is called with two arguments, the window
24431 and its new display-start position. Note that the value of `window-end'
24432 is not valid when these functions are called. */);
24433 Vwindow_scroll_functions = Qnil;
24434
24435 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24436 doc: /* Functions called when redisplay of a window reaches the end trigger.
24437 Each function is called with two arguments, the window and the end trigger value.
24438 See `set-window-redisplay-end-trigger'. */);
24439 Vredisplay_end_trigger_functions = Qnil;
24440
24441 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24442 doc: /* *Non-nil means autoselect window with mouse pointer.
24443 If nil, do not autoselect windows.
24444 A positive number means delay autoselection by that many seconds: a
24445 window is autoselected only after the mouse has remained in that
24446 window for the duration of the delay.
24447 A negative number has a similar effect, but causes windows to be
24448 autoselected only after the mouse has stopped moving. \(Because of
24449 the way Emacs compares mouse events, you will occasionally wait twice
24450 that time before the window gets selected.\)
24451 Any other value means to autoselect window instantaneously when the
24452 mouse pointer enters it.
24453
24454 Autoselection selects the minibuffer only if it is active, and never
24455 unselects the minibuffer if it is active. */);
24456 Vmouse_autoselect_window = Qnil;
24457
24458 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24459 doc: /* *Non-nil means automatically resize tool-bars.
24460 This dynamically changes the tool-bar's height to the minimum height
24461 that is needed to make all tool-bar items visible.
24462 If value is `grow-only', the tool-bar's height is only increased
24463 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24464 Vauto_resize_tool_bars = Qt;
24465
24466 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24467 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24468 auto_raise_tool_bar_buttons_p = 1;
24469
24470 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24471 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24472 make_cursor_line_fully_visible_p = 1;
24473
24474 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24475 doc: /* *Border below tool-bar in pixels.
24476 If an integer, use it as the height of the border.
24477 If it is one of `internal-border-width' or `border-width', use the
24478 value of the corresponding frame parameter.
24479 Otherwise, no border is added below the tool-bar. */);
24480 Vtool_bar_border = Qinternal_border_width;
24481
24482 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24483 doc: /* *Margin around tool-bar buttons in pixels.
24484 If an integer, use that for both horizontal and vertical margins.
24485 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24486 HORZ specifying the horizontal margin, and VERT specifying the
24487 vertical margin. */);
24488 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24489
24490 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24491 doc: /* *Relief thickness of tool-bar buttons. */);
24492 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24493
24494 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24495 doc: /* List of functions to call to fontify regions of text.
24496 Each function is called with one argument POS. Functions must
24497 fontify a region starting at POS in the current buffer, and give
24498 fontified regions the property `fontified'. */);
24499 Vfontification_functions = Qnil;
24500 Fmake_variable_buffer_local (Qfontification_functions);
24501
24502 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24503 &unibyte_display_via_language_environment,
24504 doc: /* *Non-nil means display unibyte text according to language environment.
24505 Specifically this means that unibyte non-ASCII characters
24506 are displayed by converting them to the equivalent multibyte characters
24507 according to the current language environment. As a result, they are
24508 displayed according to the current fontset. */);
24509 unibyte_display_via_language_environment = 0;
24510
24511 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24512 doc: /* *Maximum height for resizing mini-windows.
24513 If a float, it specifies a fraction of the mini-window frame's height.
24514 If an integer, it specifies a number of lines. */);
24515 Vmax_mini_window_height = make_float (0.25);
24516
24517 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24518 doc: /* *How to resize mini-windows.
24519 A value of nil means don't automatically resize mini-windows.
24520 A value of t means resize them to fit the text displayed in them.
24521 A value of `grow-only', the default, means let mini-windows grow
24522 only, until their display becomes empty, at which point the windows
24523 go back to their normal size. */);
24524 Vresize_mini_windows = Qgrow_only;
24525
24526 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24527 doc: /* Alist specifying how to blink the cursor off.
24528 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24529 `cursor-type' frame-parameter or variable equals ON-STATE,
24530 comparing using `equal', Emacs uses OFF-STATE to specify
24531 how to blink it off. ON-STATE and OFF-STATE are values for
24532 the `cursor-type' frame parameter.
24533
24534 If a frame's ON-STATE has no entry in this list,
24535 the frame's other specifications determine how to blink the cursor off. */);
24536 Vblink_cursor_alist = Qnil;
24537
24538 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24539 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24540 automatic_hscrolling_p = 1;
24541
24542 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24543 doc: /* *How many columns away from the window edge point is allowed to get
24544 before automatic hscrolling will horizontally scroll the window. */);
24545 hscroll_margin = 5;
24546
24547 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24548 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24549 When point is less than `hscroll-margin' columns from the window
24550 edge, automatic hscrolling will scroll the window by the amount of columns
24551 determined by this variable. If its value is a positive integer, scroll that
24552 many columns. If it's a positive floating-point number, it specifies the
24553 fraction of the window's width to scroll. If it's nil or zero, point will be
24554 centered horizontally after the scroll. Any other value, including negative
24555 numbers, are treated as if the value were zero.
24556
24557 Automatic hscrolling always moves point outside the scroll margin, so if
24558 point was more than scroll step columns inside the margin, the window will
24559 scroll more than the value given by the scroll step.
24560
24561 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24562 and `scroll-right' overrides this variable's effect. */);
24563 Vhscroll_step = make_number (0);
24564
24565 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24566 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24567 Bind this around calls to `message' to let it take effect. */);
24568 message_truncate_lines = 0;
24569
24570 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24571 doc: /* Normal hook run to update the menu bar definitions.
24572 Redisplay runs this hook before it redisplays the menu bar.
24573 This is used to update submenus such as Buffers,
24574 whose contents depend on various data. */);
24575 Vmenu_bar_update_hook = Qnil;
24576
24577 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24578 doc: /* Frame for which we are updating a menu.
24579 The enable predicate for a menu binding should check this variable. */);
24580 Vmenu_updating_frame = Qnil;
24581
24582 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24583 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24584 inhibit_menubar_update = 0;
24585
24586 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24587 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24588 inhibit_eval_during_redisplay = 0;
24589
24590 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24591 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24592 inhibit_free_realized_faces = 0;
24593
24594 #if GLYPH_DEBUG
24595 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24596 doc: /* Inhibit try_window_id display optimization. */);
24597 inhibit_try_window_id = 0;
24598
24599 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24600 doc: /* Inhibit try_window_reusing display optimization. */);
24601 inhibit_try_window_reusing = 0;
24602
24603 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24604 doc: /* Inhibit try_cursor_movement display optimization. */);
24605 inhibit_try_cursor_movement = 0;
24606 #endif /* GLYPH_DEBUG */
24607
24608 DEFVAR_INT ("overline-margin", &overline_margin,
24609 doc: /* *Space between overline and text, in pixels.
24610 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24611 margin to the caracter height. */);
24612 overline_margin = 2;
24613 }
24614
24615
24616 /* Initialize this module when Emacs starts. */
24617
24618 void
24619 init_xdisp ()
24620 {
24621 Lisp_Object root_window;
24622 struct window *mini_w;
24623
24624 current_header_line_height = current_mode_line_height = -1;
24625
24626 CHARPOS (this_line_start_pos) = 0;
24627
24628 mini_w = XWINDOW (minibuf_window);
24629 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24630
24631 if (!noninteractive)
24632 {
24633 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24634 int i;
24635
24636 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24637 set_window_height (root_window,
24638 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24639 0);
24640 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24641 set_window_height (minibuf_window, 1, 0);
24642
24643 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24644 mini_w->total_cols = make_number (FRAME_COLS (f));
24645
24646 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24647 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24648 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24649
24650 /* The default ellipsis glyphs `...'. */
24651 for (i = 0; i < 3; ++i)
24652 default_invis_vector[i] = make_number ('.');
24653 }
24654
24655 {
24656 /* Allocate the buffer for frame titles.
24657 Also used for `format-mode-line'. */
24658 int size = 100;
24659 mode_line_noprop_buf = (char *) xmalloc (size);
24660 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24661 mode_line_noprop_ptr = mode_line_noprop_buf;
24662 mode_line_target = MODE_LINE_DISPLAY;
24663 }
24664
24665 help_echo_showing_p = 0;
24666 }
24667
24668
24669 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24670 (do not change this comment) */