(get_window_cursor_type): Fix last change.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin around tool bar buttons in pixels. */
273
274 Lisp_Object Vtool_bar_button_margin;
275
276 /* Thickness of shadow to draw around tool bar buttons. */
277
278 EMACS_INT tool_bar_button_relief;
279
280 /* Non-zero means automatically resize tool-bars so that all tool-bar
281 items are visible, and no blank lines remain. */
282
283 int auto_resize_tool_bars_p;
284
285 /* Non-zero means draw block and hollow cursor as wide as the glyph
286 under it. For example, if a block cursor is over a tab, it will be
287 drawn as wide as that tab on the display. */
288
289 int x_stretch_cursor_p;
290
291 /* Non-nil means don't actually do any redisplay. */
292
293 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294
295 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296
297 int inhibit_eval_during_redisplay;
298
299 /* Names of text properties relevant for redisplay. */
300
301 Lisp_Object Qdisplay;
302 extern Lisp_Object Qface, Qinvisible, Qwidth;
303
304 /* Symbols used in text property values. */
305
306 Lisp_Object Vdisplay_pixels_per_inch;
307 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
308 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
309 Lisp_Object Qslice;
310 Lisp_Object Qcenter;
311 Lisp_Object Qmargin, Qpointer;
312 Lisp_Object Qline_height;
313 extern Lisp_Object Qheight;
314 extern Lisp_Object QCwidth, QCheight, QCascent;
315 extern Lisp_Object Qscroll_bar;
316 extern Lisp_Object Qcursor;
317
318 /* Non-nil means highlight trailing whitespace. */
319
320 Lisp_Object Vshow_trailing_whitespace;
321
322 /* Non-nil means escape non-break space and hyphens. */
323
324 Lisp_Object Vnobreak_char_display;
325
326 #ifdef HAVE_WINDOW_SYSTEM
327 extern Lisp_Object Voverflow_newline_into_fringe;
328
329 /* Test if overflow newline into fringe. Called with iterator IT
330 at or past right window margin, and with IT->current_x set. */
331
332 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
333 (!NILP (Voverflow_newline_into_fringe) \
334 && FRAME_WINDOW_P (it->f) \
335 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
336 && it->current_x == it->last_visible_x)
337
338 #endif /* HAVE_WINDOW_SYSTEM */
339
340 /* Non-nil means show the text cursor in void text areas
341 i.e. in blank areas after eol and eob. This used to be
342 the default in 21.3. */
343
344 Lisp_Object Vvoid_text_area_pointer;
345
346 /* Name of the face used to highlight trailing whitespace. */
347
348 Lisp_Object Qtrailing_whitespace;
349
350 /* Name and number of the face used to highlight escape glyphs. */
351
352 Lisp_Object Qescape_glyph;
353
354 /* Name and number of the face used to highlight non-breaking spaces. */
355
356 Lisp_Object Qnobreak_space;
357
358 /* The symbol `image' which is the car of the lists used to represent
359 images in Lisp. */
360
361 Lisp_Object Qimage;
362
363 /* The image map types. */
364 Lisp_Object QCmap, QCpointer;
365 Lisp_Object Qrect, Qcircle, Qpoly;
366
367 /* Non-zero means print newline to stdout before next mini-buffer
368 message. */
369
370 int noninteractive_need_newline;
371
372 /* Non-zero means print newline to message log before next message. */
373
374 static int message_log_need_newline;
375
376 /* Three markers that message_dolog uses.
377 It could allocate them itself, but that causes trouble
378 in handling memory-full errors. */
379 static Lisp_Object message_dolog_marker1;
380 static Lisp_Object message_dolog_marker2;
381 static Lisp_Object message_dolog_marker3;
382 \f
383 /* The buffer position of the first character appearing entirely or
384 partially on the line of the selected window which contains the
385 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
386 redisplay optimization in redisplay_internal. */
387
388 static struct text_pos this_line_start_pos;
389
390 /* Number of characters past the end of the line above, including the
391 terminating newline. */
392
393 static struct text_pos this_line_end_pos;
394
395 /* The vertical positions and the height of this line. */
396
397 static int this_line_vpos;
398 static int this_line_y;
399 static int this_line_pixel_height;
400
401 /* X position at which this display line starts. Usually zero;
402 negative if first character is partially visible. */
403
404 static int this_line_start_x;
405
406 /* Buffer that this_line_.* variables are referring to. */
407
408 static struct buffer *this_line_buffer;
409
410 /* Nonzero means truncate lines in all windows less wide than the
411 frame. */
412
413 int truncate_partial_width_windows;
414
415 /* A flag to control how to display unibyte 8-bit character. */
416
417 int unibyte_display_via_language_environment;
418
419 /* Nonzero means we have more than one non-mini-buffer-only frame.
420 Not guaranteed to be accurate except while parsing
421 frame-title-format. */
422
423 int multiple_frames;
424
425 Lisp_Object Vglobal_mode_string;
426
427
428 /* List of variables (symbols) which hold markers for overlay arrows.
429 The symbols on this list are examined during redisplay to determine
430 where to display overlay arrows. */
431
432 Lisp_Object Voverlay_arrow_variable_list;
433
434 /* Marker for where to display an arrow on top of the buffer text. */
435
436 Lisp_Object Voverlay_arrow_position;
437
438 /* String to display for the arrow. Only used on terminal frames. */
439
440 Lisp_Object Voverlay_arrow_string;
441
442 /* Values of those variables at last redisplay are stored as
443 properties on `overlay-arrow-position' symbol. However, if
444 Voverlay_arrow_position is a marker, last-arrow-position is its
445 numerical position. */
446
447 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
448
449 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
450 properties on a symbol in overlay-arrow-variable-list. */
451
452 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
453
454 /* Like mode-line-format, but for the title bar on a visible frame. */
455
456 Lisp_Object Vframe_title_format;
457
458 /* Like mode-line-format, but for the title bar on an iconified frame. */
459
460 Lisp_Object Vicon_title_format;
461
462 /* List of functions to call when a window's size changes. These
463 functions get one arg, a frame on which one or more windows' sizes
464 have changed. */
465
466 static Lisp_Object Vwindow_size_change_functions;
467
468 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
469
470 /* Nonzero if an overlay arrow has been displayed in this window. */
471
472 static int overlay_arrow_seen;
473
474 /* Nonzero means highlight the region even in nonselected windows. */
475
476 int highlight_nonselected_windows;
477
478 /* If cursor motion alone moves point off frame, try scrolling this
479 many lines up or down if that will bring it back. */
480
481 static EMACS_INT scroll_step;
482
483 /* Nonzero means scroll just far enough to bring point back on the
484 screen, when appropriate. */
485
486 static EMACS_INT scroll_conservatively;
487
488 /* Recenter the window whenever point gets within this many lines of
489 the top or bottom of the window. This value is translated into a
490 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
491 that there is really a fixed pixel height scroll margin. */
492
493 EMACS_INT scroll_margin;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* Zero means display the mode-line/header-line/menu-bar in the default face
506 (this slightly odd definition is for compatibility with previous versions
507 of emacs), non-zero means display them using their respective faces.
508
509 This variable is deprecated. */
510
511 int mode_line_inverse_video;
512
513 /* Prompt to display in front of the mini-buffer contents. */
514
515 Lisp_Object minibuf_prompt;
516
517 /* Width of current mini-buffer prompt. Only set after display_line
518 of the line that contains the prompt. */
519
520 int minibuf_prompt_width;
521
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
525
526 Lisp_Object echo_area_window;
527
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
532
533 Lisp_Object Vmessage_stack;
534
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
537
538 int message_enable_multibyte;
539
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541
542 int update_mode_lines;
543
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
546
547 int windows_or_buffers_changed;
548
549 /* Nonzero means a frame's cursor type has been changed. */
550
551 int cursor_type_changed;
552
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
555
556 int line_number_displayed;
557
558 /* Maximum buffer size for which to display line numbers. */
559
560 Lisp_Object Vline_number_display_limit;
561
562 /* Line width to consider when repositioning for line number display. */
563
564 static EMACS_INT line_number_display_limit_width;
565
566 /* Number of lines to keep in the message log buffer. t means
567 infinite. nil means don't log at all. */
568
569 Lisp_Object Vmessage_log_max;
570
571 /* The name of the *Messages* buffer, a string. */
572
573 static Lisp_Object Vmessages_buffer_name;
574
575 /* Index 0 is the buffer that holds the current (desired) echo area message,
576 or nil if none is desired right now.
577
578 Index 1 is the buffer that holds the previously displayed echo area message,
579 or nil to indicate no message. This is normally what's on the screen now.
580
581 These two can point to the same buffer. That happens when the last
582 message output by the user (or made by echoing) has been displayed. */
583
584 Lisp_Object echo_area_buffer[2];
585
586 /* Permanent pointers to the two buffers that are used for echo area
587 purposes. Once the two buffers are made, and their pointers are
588 placed here, these two slots remain unchanged unless those buffers
589 need to be created afresh. */
590
591 static Lisp_Object echo_buffer[2];
592
593 /* A vector saved used in with_area_buffer to reduce consing. */
594
595 static Lisp_Object Vwith_echo_area_save_vector;
596
597 /* Non-zero means display_echo_area should display the last echo area
598 message again. Set by redisplay_preserve_echo_area. */
599
600 static int display_last_displayed_message_p;
601
602 /* Nonzero if echo area is being used by print; zero if being used by
603 message. */
604
605 int message_buf_print;
606
607 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
608
609 Lisp_Object Qinhibit_menubar_update;
610 int inhibit_menubar_update;
611
612 /* Maximum height for resizing mini-windows. Either a float
613 specifying a fraction of the available height, or an integer
614 specifying a number of lines. */
615
616 Lisp_Object Vmax_mini_window_height;
617
618 /* Non-zero means messages should be displayed with truncated
619 lines instead of being continued. */
620
621 int message_truncate_lines;
622 Lisp_Object Qmessage_truncate_lines;
623
624 /* Set to 1 in clear_message to make redisplay_internal aware
625 of an emptied echo area. */
626
627 static int message_cleared_p;
628
629 /* How to blink the default frame cursor off. */
630 Lisp_Object Vblink_cursor_alist;
631
632 /* A scratch glyph row with contents used for generating truncation
633 glyphs. Also used in direct_output_for_insert. */
634
635 #define MAX_SCRATCH_GLYPHS 100
636 struct glyph_row scratch_glyph_row;
637 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
638
639 /* Ascent and height of the last line processed by move_it_to. */
640
641 static int last_max_ascent, last_height;
642
643 /* Non-zero if there's a help-echo in the echo area. */
644
645 int help_echo_showing_p;
646
647 /* If >= 0, computed, exact values of mode-line and header-line height
648 to use in the macros CURRENT_MODE_LINE_HEIGHT and
649 CURRENT_HEADER_LINE_HEIGHT. */
650
651 int current_mode_line_height, current_header_line_height;
652
653 /* The maximum distance to look ahead for text properties. Values
654 that are too small let us call compute_char_face and similar
655 functions too often which is expensive. Values that are too large
656 let us call compute_char_face and alike too often because we
657 might not be interested in text properties that far away. */
658
659 #define TEXT_PROP_DISTANCE_LIMIT 100
660
661 #if GLYPH_DEBUG
662
663 /* Variables to turn off display optimizations from Lisp. */
664
665 int inhibit_try_window_id, inhibit_try_window_reusing;
666 int inhibit_try_cursor_movement;
667
668 /* Non-zero means print traces of redisplay if compiled with
669 GLYPH_DEBUG != 0. */
670
671 int trace_redisplay_p;
672
673 #endif /* GLYPH_DEBUG */
674
675 #ifdef DEBUG_TRACE_MOVE
676 /* Non-zero means trace with TRACE_MOVE to stderr. */
677 int trace_move;
678
679 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
680 #else
681 #define TRACE_MOVE(x) (void) 0
682 #endif
683
684 /* Non-zero means automatically scroll windows horizontally to make
685 point visible. */
686
687 int automatic_hscrolling_p;
688
689 /* How close to the margin can point get before the window is scrolled
690 horizontally. */
691 EMACS_INT hscroll_margin;
692
693 /* How much to scroll horizontally when point is inside the above margin. */
694 Lisp_Object Vhscroll_step;
695
696 /* The variable `resize-mini-windows'. If nil, don't resize
697 mini-windows. If t, always resize them to fit the text they
698 display. If `grow-only', let mini-windows grow only until they
699 become empty. */
700
701 Lisp_Object Vresize_mini_windows;
702
703 /* Buffer being redisplayed -- for redisplay_window_error. */
704
705 struct buffer *displayed_buffer;
706
707 /* Value returned from text property handlers (see below). */
708
709 enum prop_handled
710 {
711 HANDLED_NORMALLY,
712 HANDLED_RECOMPUTE_PROPS,
713 HANDLED_OVERLAY_STRING_CONSUMED,
714 HANDLED_RETURN
715 };
716
717 /* A description of text properties that redisplay is interested
718 in. */
719
720 struct props
721 {
722 /* The name of the property. */
723 Lisp_Object *name;
724
725 /* A unique index for the property. */
726 enum prop_idx idx;
727
728 /* A handler function called to set up iterator IT from the property
729 at IT's current position. Value is used to steer handle_stop. */
730 enum prop_handled (*handler) P_ ((struct it *it));
731 };
732
733 static enum prop_handled handle_face_prop P_ ((struct it *));
734 static enum prop_handled handle_invisible_prop P_ ((struct it *));
735 static enum prop_handled handle_display_prop P_ ((struct it *));
736 static enum prop_handled handle_composition_prop P_ ((struct it *));
737 static enum prop_handled handle_overlay_change P_ ((struct it *));
738 static enum prop_handled handle_fontified_prop P_ ((struct it *));
739
740 /* Properties handled by iterators. */
741
742 static struct props it_props[] =
743 {
744 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
745 /* Handle `face' before `display' because some sub-properties of
746 `display' need to know the face. */
747 {&Qface, FACE_PROP_IDX, handle_face_prop},
748 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
749 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
750 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
751 {NULL, 0, NULL}
752 };
753
754 /* Value is the position described by X. If X is a marker, value is
755 the marker_position of X. Otherwise, value is X. */
756
757 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
758
759 /* Enumeration returned by some move_it_.* functions internally. */
760
761 enum move_it_result
762 {
763 /* Not used. Undefined value. */
764 MOVE_UNDEFINED,
765
766 /* Move ended at the requested buffer position or ZV. */
767 MOVE_POS_MATCH_OR_ZV,
768
769 /* Move ended at the requested X pixel position. */
770 MOVE_X_REACHED,
771
772 /* Move within a line ended at the end of a line that must be
773 continued. */
774 MOVE_LINE_CONTINUED,
775
776 /* Move within a line ended at the end of a line that would
777 be displayed truncated. */
778 MOVE_LINE_TRUNCATED,
779
780 /* Move within a line ended at a line end. */
781 MOVE_NEWLINE_OR_CR
782 };
783
784 /* This counter is used to clear the face cache every once in a while
785 in redisplay_internal. It is incremented for each redisplay.
786 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
787 cleared. */
788
789 #define CLEAR_FACE_CACHE_COUNT 500
790 static int clear_face_cache_count;
791
792 /* Similarly for the image cache. */
793
794 #ifdef HAVE_WINDOW_SYSTEM
795 #define CLEAR_IMAGE_CACHE_COUNT 101
796 static int clear_image_cache_count;
797 #endif
798
799 /* Record the previous terminal frame we displayed. */
800
801 static struct frame *previous_terminal_frame;
802
803 /* Non-zero while redisplay_internal is in progress. */
804
805 int redisplaying_p;
806
807 /* Non-zero means don't free realized faces. Bound while freeing
808 realized faces is dangerous because glyph matrices might still
809 reference them. */
810
811 int inhibit_free_realized_faces;
812 Lisp_Object Qinhibit_free_realized_faces;
813
814 /* If a string, XTread_socket generates an event to display that string.
815 (The display is done in read_char.) */
816
817 Lisp_Object help_echo_string;
818 Lisp_Object help_echo_window;
819 Lisp_Object help_echo_object;
820 int help_echo_pos;
821
822 /* Temporary variable for XTread_socket. */
823
824 Lisp_Object previous_help_echo_string;
825
826 /* Null glyph slice */
827
828 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
829
830 \f
831 /* Function prototypes. */
832
833 static void setup_for_ellipsis P_ ((struct it *, int));
834 static void mark_window_display_accurate_1 P_ ((struct window *, int));
835 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
837 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
838 static int redisplay_mode_lines P_ ((Lisp_Object, int));
839 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
840
841 #if 0
842 static int invisible_text_between_p P_ ((struct it *, int, int));
843 #endif
844
845 static void pint2str P_ ((char *, int, int));
846 static void pint2hrstr P_ ((char *, int, int));
847 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
848 struct text_pos));
849 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
850 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
851 static void store_mode_line_noprop_char P_ ((char));
852 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
853 static void x_consider_frame_title P_ ((Lisp_Object));
854 static void handle_stop P_ ((struct it *));
855 static int tool_bar_lines_needed P_ ((struct frame *));
856 static int single_display_spec_intangible_p P_ ((Lisp_Object));
857 static void ensure_echo_area_buffers P_ ((void));
858 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
859 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
860 static int with_echo_area_buffer P_ ((struct window *, int,
861 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
862 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
863 static void clear_garbaged_frames P_ ((void));
864 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static int display_echo_area P_ ((struct window *));
868 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
871 static int string_char_and_length P_ ((const unsigned char *, int, int *));
872 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
873 struct text_pos));
874 static int compute_window_start_on_continuation_line P_ ((struct window *));
875 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
876 static void insert_left_trunc_glyphs P_ ((struct it *));
877 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
878 Lisp_Object));
879 static void extend_face_to_end_of_line P_ ((struct it *));
880 static int append_space_for_newline P_ ((struct it *, int));
881 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
882 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
883 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
884 static int trailing_whitespace_p P_ ((int));
885 static int message_log_check_duplicate P_ ((int, int, int, int));
886 static void push_it P_ ((struct it *));
887 static void pop_it P_ ((struct it *));
888 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
889 static void select_frame_for_redisplay P_ ((Lisp_Object));
890 static void redisplay_internal P_ ((int));
891 static int echo_area_display P_ ((int));
892 static void redisplay_windows P_ ((Lisp_Object));
893 static void redisplay_window P_ ((Lisp_Object, int));
894 static Lisp_Object redisplay_window_error ();
895 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
896 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
897 static void update_menu_bar P_ ((struct frame *, int));
898 static int try_window_reusing_current_matrix P_ ((struct window *));
899 static int try_window_id P_ ((struct window *));
900 static int display_line P_ ((struct it *));
901 static int display_mode_lines P_ ((struct window *));
902 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
903 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
904 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
905 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
906 static void display_menu_bar P_ ((struct window *));
907 static int display_count_lines P_ ((int, int, int, int, int *));
908 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
909 int, int, struct it *, int, int, int, int));
910 static void compute_line_metrics P_ ((struct it *));
911 static void run_redisplay_end_trigger_hook P_ ((struct it *));
912 static int get_overlay_strings P_ ((struct it *, int));
913 static void next_overlay_string P_ ((struct it *));
914 static void reseat P_ ((struct it *, struct text_pos, int));
915 static void reseat_1 P_ ((struct it *, struct text_pos, int));
916 static void back_to_previous_visible_line_start P_ ((struct it *));
917 void reseat_at_previous_visible_line_start P_ ((struct it *));
918 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
919 static int next_element_from_ellipsis P_ ((struct it *));
920 static int next_element_from_display_vector P_ ((struct it *));
921 static int next_element_from_string P_ ((struct it *));
922 static int next_element_from_c_string P_ ((struct it *));
923 static int next_element_from_buffer P_ ((struct it *));
924 static int next_element_from_composition P_ ((struct it *));
925 static int next_element_from_image P_ ((struct it *));
926 static int next_element_from_stretch P_ ((struct it *));
927 static void load_overlay_strings P_ ((struct it *, int));
928 static int init_from_display_pos P_ ((struct it *, struct window *,
929 struct display_pos *));
930 static void reseat_to_string P_ ((struct it *, unsigned char *,
931 Lisp_Object, int, int, int, int));
932 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
933 int, int, int));
934 void move_it_vertically_backward P_ ((struct it *, int));
935 static void init_to_row_start P_ ((struct it *, struct window *,
936 struct glyph_row *));
937 static int init_to_row_end P_ ((struct it *, struct window *,
938 struct glyph_row *));
939 static void back_to_previous_line_start P_ ((struct it *));
940 static int forward_to_next_line_start P_ ((struct it *, int *));
941 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
942 Lisp_Object, int));
943 static struct text_pos string_pos P_ ((int, Lisp_Object));
944 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
945 static int number_of_chars P_ ((unsigned char *, int));
946 static void compute_stop_pos P_ ((struct it *));
947 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
948 Lisp_Object));
949 static int face_before_or_after_it_pos P_ ((struct it *, int));
950 static int next_overlay_change P_ ((int));
951 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
952 Lisp_Object, struct text_pos *,
953 int));
954 static int underlying_face_id P_ ((struct it *));
955 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
956 struct window *));
957
958 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
960
961 #ifdef HAVE_WINDOW_SYSTEM
962
963 static void update_tool_bar P_ ((struct frame *, int));
964 static void build_desired_tool_bar_string P_ ((struct frame *f));
965 static int redisplay_tool_bar P_ ((struct frame *));
966 static void display_tool_bar_line P_ ((struct it *));
967 static void notice_overwritten_cursor P_ ((struct window *,
968 enum glyph_row_area,
969 int, int, int, int));
970
971
972
973 #endif /* HAVE_WINDOW_SYSTEM */
974
975 \f
976 /***********************************************************************
977 Window display dimensions
978 ***********************************************************************/
979
980 /* Return the bottom boundary y-position for text lines in window W.
981 This is the first y position at which a line cannot start.
982 It is relative to the top of the window.
983
984 This is the height of W minus the height of a mode line, if any. */
985
986 INLINE int
987 window_text_bottom_y (w)
988 struct window *w;
989 {
990 int height = WINDOW_TOTAL_HEIGHT (w);
991
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
995 }
996
997 /* Return the pixel width of display area AREA of window W. AREA < 0
998 means return the total width of W, not including fringes to
999 the left and right of the window. */
1000
1001 INLINE int
1002 window_box_width (w, area)
1003 struct window *w;
1004 int area;
1005 {
1006 int cols = XFASTINT (w->total_cols);
1007 int pixels = 0;
1008
1009 if (!w->pseudo_window_p)
1010 {
1011 cols -= WINDOW_SCROLL_BAR_COLS (w);
1012
1013 if (area == TEXT_AREA)
1014 {
1015 if (INTEGERP (w->left_margin_cols))
1016 cols -= XFASTINT (w->left_margin_cols);
1017 if (INTEGERP (w->right_margin_cols))
1018 cols -= XFASTINT (w->right_margin_cols);
1019 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1020 }
1021 else if (area == LEFT_MARGIN_AREA)
1022 {
1023 cols = (INTEGERP (w->left_margin_cols)
1024 ? XFASTINT (w->left_margin_cols) : 0);
1025 pixels = 0;
1026 }
1027 else if (area == RIGHT_MARGIN_AREA)
1028 {
1029 cols = (INTEGERP (w->right_margin_cols)
1030 ? XFASTINT (w->right_margin_cols) : 0);
1031 pixels = 0;
1032 }
1033 }
1034
1035 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1036 }
1037
1038
1039 /* Return the pixel height of the display area of window W, not
1040 including mode lines of W, if any. */
1041
1042 INLINE int
1043 window_box_height (w)
1044 struct window *w;
1045 {
1046 struct frame *f = XFRAME (w->frame);
1047 int height = WINDOW_TOTAL_HEIGHT (w);
1048
1049 xassert (height >= 0);
1050
1051 /* Note: the code below that determines the mode-line/header-line
1052 height is essentially the same as that contained in the macro
1053 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1054 the appropriate glyph row has its `mode_line_p' flag set,
1055 and if it doesn't, uses estimate_mode_line_height instead. */
1056
1057 if (WINDOW_WANTS_MODELINE_P (w))
1058 {
1059 struct glyph_row *ml_row
1060 = (w->current_matrix && w->current_matrix->rows
1061 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1062 : 0);
1063 if (ml_row && ml_row->mode_line_p)
1064 height -= ml_row->height;
1065 else
1066 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1067 }
1068
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 {
1071 struct glyph_row *hl_row
1072 = (w->current_matrix && w->current_matrix->rows
1073 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1074 : 0);
1075 if (hl_row && hl_row->mode_line_p)
1076 height -= hl_row->height;
1077 else
1078 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1079 }
1080
1081 /* With a very small font and a mode-line that's taller than
1082 default, we might end up with a negative height. */
1083 return max (0, height);
1084 }
1085
1086 /* Return the window-relative coordinate of the left edge of display
1087 area AREA of window W. AREA < 0 means return the left edge of the
1088 whole window, to the right of the left fringe of W. */
1089
1090 INLINE int
1091 window_box_left_offset (w, area)
1092 struct window *w;
1093 int area;
1094 {
1095 int x;
1096
1097 if (w->pseudo_window_p)
1098 return 0;
1099
1100 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1101
1102 if (area == TEXT_AREA)
1103 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1104 + window_box_width (w, LEFT_MARGIN_AREA));
1105 else if (area == RIGHT_MARGIN_AREA)
1106 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1107 + window_box_width (w, LEFT_MARGIN_AREA)
1108 + window_box_width (w, TEXT_AREA)
1109 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1110 ? 0
1111 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1112 else if (area == LEFT_MARGIN_AREA
1113 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1114 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1115
1116 return x;
1117 }
1118
1119
1120 /* Return the window-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the left edge of the
1122 whole window, to the left of the right fringe of W. */
1123
1124 INLINE int
1125 window_box_right_offset (w, area)
1126 struct window *w;
1127 int area;
1128 {
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1135
1136 INLINE int
1137 window_box_left (w, area)
1138 struct window *w;
1139 int area;
1140 {
1141 struct frame *f = XFRAME (w->frame);
1142 int x;
1143
1144 if (w->pseudo_window_p)
1145 return FRAME_INTERNAL_BORDER_WIDTH (f);
1146
1147 x = (WINDOW_LEFT_EDGE_X (w)
1148 + window_box_left_offset (w, area));
1149
1150 return x;
1151 }
1152
1153
1154 /* Return the frame-relative coordinate of the right edge of display
1155 area AREA of window W. AREA < 0 means return the left edge of the
1156 whole window, to the left of the right fringe of W. */
1157
1158 INLINE int
1159 window_box_right (w, area)
1160 struct window *w;
1161 int area;
1162 {
1163 return window_box_left (w, area) + window_box_width (w, area);
1164 }
1165
1166 /* Get the bounding box of the display area AREA of window W, without
1167 mode lines, in frame-relative coordinates. AREA < 0 means the
1168 whole window, not including the left and right fringes of
1169 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1170 coordinates of the upper-left corner of the box. Return in
1171 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1172
1173 INLINE void
1174 window_box (w, area, box_x, box_y, box_width, box_height)
1175 struct window *w;
1176 int area;
1177 int *box_x, *box_y, *box_width, *box_height;
1178 {
1179 if (box_width)
1180 *box_width = window_box_width (w, area);
1181 if (box_height)
1182 *box_height = window_box_height (w);
1183 if (box_x)
1184 *box_x = window_box_left (w, area);
1185 if (box_y)
1186 {
1187 *box_y = WINDOW_TOP_EDGE_Y (w);
1188 if (WINDOW_WANTS_HEADER_LINE_P (w))
1189 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1190 }
1191 }
1192
1193
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines. AREA < 0 means the whole window, not including the
1196 left and right fringe of the window. Return in *TOP_LEFT_X
1197 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1198 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1199 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1200 box. */
1201
1202 INLINE void
1203 window_box_edges (w, area, top_left_x, top_left_y,
1204 bottom_right_x, bottom_right_y)
1205 struct window *w;
1206 int area;
1207 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1208 {
1209 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1210 bottom_right_y);
1211 *bottom_right_x += *top_left_x;
1212 *bottom_right_y += *top_left_y;
1213 }
1214
1215
1216 \f
1217 /***********************************************************************
1218 Utilities
1219 ***********************************************************************/
1220
1221 /* Return the bottom y-position of the line the iterator IT is in.
1222 This can modify IT's settings. */
1223
1224 int
1225 line_bottom_y (it)
1226 struct it *it;
1227 {
1228 int line_height = it->max_ascent + it->max_descent;
1229 int line_top_y = it->current_y;
1230
1231 if (line_height == 0)
1232 {
1233 if (last_height)
1234 line_height = last_height;
1235 else if (IT_CHARPOS (*it) < ZV)
1236 {
1237 move_it_by_lines (it, 1, 1);
1238 line_height = (it->max_ascent || it->max_descent
1239 ? it->max_ascent + it->max_descent
1240 : last_height);
1241 }
1242 else
1243 {
1244 struct glyph_row *row = it->glyph_row;
1245
1246 /* Use the default character height. */
1247 it->glyph_row = NULL;
1248 it->what = IT_CHARACTER;
1249 it->c = ' ';
1250 it->len = 1;
1251 PRODUCE_GLYPHS (it);
1252 line_height = it->ascent + it->descent;
1253 it->glyph_row = row;
1254 }
1255 }
1256
1257 return line_top_y + line_height;
1258 }
1259
1260
1261 /* Return 1 if position CHARPOS is visible in window W.
1262 If visible, set *X and *Y to pixel coordinates of top left corner.
1263 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1264 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1265 and header-lines heights. */
1266
1267 int
1268 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1269 struct window *w;
1270 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1271 {
1272 struct it it;
1273 struct text_pos top;
1274 int visible_p = 0;
1275 struct buffer *old_buffer = NULL;
1276
1277 if (noninteractive)
1278 return visible_p;
1279
1280 if (XBUFFER (w->buffer) != current_buffer)
1281 {
1282 old_buffer = current_buffer;
1283 set_buffer_internal_1 (XBUFFER (w->buffer));
1284 }
1285
1286 SET_TEXT_POS_FROM_MARKER (top, w->start);
1287
1288 /* Compute exact mode line heights, if requested. */
1289 if (exact_mode_line_heights_p)
1290 {
1291 if (WINDOW_WANTS_MODELINE_P (w))
1292 current_mode_line_height
1293 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1294 current_buffer->mode_line_format);
1295
1296 if (WINDOW_WANTS_HEADER_LINE_P (w))
1297 current_header_line_height
1298 = display_mode_line (w, HEADER_LINE_FACE_ID,
1299 current_buffer->header_line_format);
1300 }
1301
1302 start_display (&it, w, top);
1303 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1304 MOVE_TO_POS | MOVE_TO_Y);
1305
1306 /* Note that we may overshoot because of invisible text. */
1307 if (IT_CHARPOS (it) >= charpos)
1308 {
1309 int top_x = it.current_x;
1310 int top_y = it.current_y;
1311 int bottom_y = (last_height = 0, line_bottom_y (&it));
1312 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1313
1314 if (top_y < window_top_y)
1315 visible_p = bottom_y > window_top_y;
1316 else if (top_y < it.last_visible_y)
1317 visible_p = 1;
1318 if (visible_p)
1319 {
1320 *x = top_x;
1321 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1322 *rtop = max (0, window_top_y - top_y);
1323 *rbot = max (0, bottom_y - it.last_visible_y);
1324 }
1325 }
1326 else
1327 {
1328 struct it it2;
1329
1330 it2 = it;
1331 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1332 move_it_by_lines (&it, 1, 0);
1333 if (charpos < IT_CHARPOS (it))
1334 {
1335 visible_p = 1;
1336 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1337 *x = it2.current_x;
1338 *y = it2.current_y + it2.max_ascent - it2.ascent;
1339 *rtop = max (0, -it2.current_y);
1340 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1341 - it.last_visible_y));
1342 }
1343 }
1344
1345 if (old_buffer)
1346 set_buffer_internal_1 (old_buffer);
1347
1348 current_header_line_height = current_mode_line_height = -1;
1349
1350 if (visible_p && XFASTINT (w->hscroll) > 0)
1351 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1352
1353 return visible_p;
1354 }
1355
1356
1357 /* Return the next character from STR which is MAXLEN bytes long.
1358 Return in *LEN the length of the character. This is like
1359 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1360 we find one, we return a `?', but with the length of the invalid
1361 character. */
1362
1363 static INLINE int
1364 string_char_and_length (str, maxlen, len)
1365 const unsigned char *str;
1366 int maxlen, *len;
1367 {
1368 int c;
1369
1370 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1371 if (!CHAR_VALID_P (c, 1))
1372 /* We may not change the length here because other places in Emacs
1373 don't use this function, i.e. they silently accept invalid
1374 characters. */
1375 c = '?';
1376
1377 return c;
1378 }
1379
1380
1381
1382 /* Given a position POS containing a valid character and byte position
1383 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1384
1385 static struct text_pos
1386 string_pos_nchars_ahead (pos, string, nchars)
1387 struct text_pos pos;
1388 Lisp_Object string;
1389 int nchars;
1390 {
1391 xassert (STRINGP (string) && nchars >= 0);
1392
1393 if (STRING_MULTIBYTE (string))
1394 {
1395 int rest = SBYTES (string) - BYTEPOS (pos);
1396 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1397 int len;
1398
1399 while (nchars--)
1400 {
1401 string_char_and_length (p, rest, &len);
1402 p += len, rest -= len;
1403 xassert (rest >= 0);
1404 CHARPOS (pos) += 1;
1405 BYTEPOS (pos) += len;
1406 }
1407 }
1408 else
1409 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1410
1411 return pos;
1412 }
1413
1414
1415 /* Value is the text position, i.e. character and byte position,
1416 for character position CHARPOS in STRING. */
1417
1418 static INLINE struct text_pos
1419 string_pos (charpos, string)
1420 int charpos;
1421 Lisp_Object string;
1422 {
1423 struct text_pos pos;
1424 xassert (STRINGP (string));
1425 xassert (charpos >= 0);
1426 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1427 return pos;
1428 }
1429
1430
1431 /* Value is a text position, i.e. character and byte position, for
1432 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1433 means recognize multibyte characters. */
1434
1435 static struct text_pos
1436 c_string_pos (charpos, s, multibyte_p)
1437 int charpos;
1438 unsigned char *s;
1439 int multibyte_p;
1440 {
1441 struct text_pos pos;
1442
1443 xassert (s != NULL);
1444 xassert (charpos >= 0);
1445
1446 if (multibyte_p)
1447 {
1448 int rest = strlen (s), len;
1449
1450 SET_TEXT_POS (pos, 0, 0);
1451 while (charpos--)
1452 {
1453 string_char_and_length (s, rest, &len);
1454 s += len, rest -= len;
1455 xassert (rest >= 0);
1456 CHARPOS (pos) += 1;
1457 BYTEPOS (pos) += len;
1458 }
1459 }
1460 else
1461 SET_TEXT_POS (pos, charpos, charpos);
1462
1463 return pos;
1464 }
1465
1466
1467 /* Value is the number of characters in C string S. MULTIBYTE_P
1468 non-zero means recognize multibyte characters. */
1469
1470 static int
1471 number_of_chars (s, multibyte_p)
1472 unsigned char *s;
1473 int multibyte_p;
1474 {
1475 int nchars;
1476
1477 if (multibyte_p)
1478 {
1479 int rest = strlen (s), len;
1480 unsigned char *p = (unsigned char *) s;
1481
1482 for (nchars = 0; rest > 0; ++nchars)
1483 {
1484 string_char_and_length (p, rest, &len);
1485 rest -= len, p += len;
1486 }
1487 }
1488 else
1489 nchars = strlen (s);
1490
1491 return nchars;
1492 }
1493
1494
1495 /* Compute byte position NEWPOS->bytepos corresponding to
1496 NEWPOS->charpos. POS is a known position in string STRING.
1497 NEWPOS->charpos must be >= POS.charpos. */
1498
1499 static void
1500 compute_string_pos (newpos, pos, string)
1501 struct text_pos *newpos, pos;
1502 Lisp_Object string;
1503 {
1504 xassert (STRINGP (string));
1505 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1506
1507 if (STRING_MULTIBYTE (string))
1508 *newpos = string_pos_nchars_ahead (pos, string,
1509 CHARPOS (*newpos) - CHARPOS (pos));
1510 else
1511 BYTEPOS (*newpos) = CHARPOS (*newpos);
1512 }
1513
1514 /* EXPORT:
1515 Return an estimation of the pixel height of mode or top lines on
1516 frame F. FACE_ID specifies what line's height to estimate. */
1517
1518 int
1519 estimate_mode_line_height (f, face_id)
1520 struct frame *f;
1521 enum face_id face_id;
1522 {
1523 #ifdef HAVE_WINDOW_SYSTEM
1524 if (FRAME_WINDOW_P (f))
1525 {
1526 int height = FONT_HEIGHT (FRAME_FONT (f));
1527
1528 /* This function is called so early when Emacs starts that the face
1529 cache and mode line face are not yet initialized. */
1530 if (FRAME_FACE_CACHE (f))
1531 {
1532 struct face *face = FACE_FROM_ID (f, face_id);
1533 if (face)
1534 {
1535 if (face->font)
1536 height = FONT_HEIGHT (face->font);
1537 if (face->box_line_width > 0)
1538 height += 2 * face->box_line_width;
1539 }
1540 }
1541
1542 return height;
1543 }
1544 #endif
1545
1546 return 1;
1547 }
1548
1549 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1550 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1551 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1552 not force the value into range. */
1553
1554 void
1555 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1556 FRAME_PTR f;
1557 register int pix_x, pix_y;
1558 int *x, *y;
1559 NativeRectangle *bounds;
1560 int noclip;
1561 {
1562
1563 #ifdef HAVE_WINDOW_SYSTEM
1564 if (FRAME_WINDOW_P (f))
1565 {
1566 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1567 even for negative values. */
1568 if (pix_x < 0)
1569 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1570 if (pix_y < 0)
1571 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1572
1573 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1574 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1575
1576 if (bounds)
1577 STORE_NATIVE_RECT (*bounds,
1578 FRAME_COL_TO_PIXEL_X (f, pix_x),
1579 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1580 FRAME_COLUMN_WIDTH (f) - 1,
1581 FRAME_LINE_HEIGHT (f) - 1);
1582
1583 if (!noclip)
1584 {
1585 if (pix_x < 0)
1586 pix_x = 0;
1587 else if (pix_x > FRAME_TOTAL_COLS (f))
1588 pix_x = FRAME_TOTAL_COLS (f);
1589
1590 if (pix_y < 0)
1591 pix_y = 0;
1592 else if (pix_y > FRAME_LINES (f))
1593 pix_y = FRAME_LINES (f);
1594 }
1595 }
1596 #endif
1597
1598 *x = pix_x;
1599 *y = pix_y;
1600 }
1601
1602
1603 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1604 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1605 can't tell the positions because W's display is not up to date,
1606 return 0. */
1607
1608 int
1609 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1610 struct window *w;
1611 int hpos, vpos;
1612 int *frame_x, *frame_y;
1613 {
1614 #ifdef HAVE_WINDOW_SYSTEM
1615 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1616 {
1617 int success_p;
1618
1619 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1620 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1621
1622 if (display_completed)
1623 {
1624 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1625 struct glyph *glyph = row->glyphs[TEXT_AREA];
1626 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1627
1628 hpos = row->x;
1629 vpos = row->y;
1630 while (glyph < end)
1631 {
1632 hpos += glyph->pixel_width;
1633 ++glyph;
1634 }
1635
1636 /* If first glyph is partially visible, its first visible position is still 0. */
1637 if (hpos < 0)
1638 hpos = 0;
1639
1640 success_p = 1;
1641 }
1642 else
1643 {
1644 hpos = vpos = 0;
1645 success_p = 0;
1646 }
1647
1648 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1649 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1650 return success_p;
1651 }
1652 #endif
1653
1654 *frame_x = hpos;
1655 *frame_y = vpos;
1656 return 1;
1657 }
1658
1659
1660 #ifdef HAVE_WINDOW_SYSTEM
1661
1662 /* Find the glyph under window-relative coordinates X/Y in window W.
1663 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1664 strings. Return in *HPOS and *VPOS the row and column number of
1665 the glyph found. Return in *AREA the glyph area containing X.
1666 Value is a pointer to the glyph found or null if X/Y is not on
1667 text, or we can't tell because W's current matrix is not up to
1668 date. */
1669
1670 static struct glyph *
1671 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1672 struct window *w;
1673 int x, y;
1674 int *hpos, *vpos, *dx, *dy, *area;
1675 {
1676 struct glyph *glyph, *end;
1677 struct glyph_row *row = NULL;
1678 int x0, i;
1679
1680 /* Find row containing Y. Give up if some row is not enabled. */
1681 for (i = 0; i < w->current_matrix->nrows; ++i)
1682 {
1683 row = MATRIX_ROW (w->current_matrix, i);
1684 if (!row->enabled_p)
1685 return NULL;
1686 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1687 break;
1688 }
1689
1690 *vpos = i;
1691 *hpos = 0;
1692
1693 /* Give up if Y is not in the window. */
1694 if (i == w->current_matrix->nrows)
1695 return NULL;
1696
1697 /* Get the glyph area containing X. */
1698 if (w->pseudo_window_p)
1699 {
1700 *area = TEXT_AREA;
1701 x0 = 0;
1702 }
1703 else
1704 {
1705 if (x < window_box_left_offset (w, TEXT_AREA))
1706 {
1707 *area = LEFT_MARGIN_AREA;
1708 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1709 }
1710 else if (x < window_box_right_offset (w, TEXT_AREA))
1711 {
1712 *area = TEXT_AREA;
1713 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1714 }
1715 else
1716 {
1717 *area = RIGHT_MARGIN_AREA;
1718 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1719 }
1720 }
1721
1722 /* Find glyph containing X. */
1723 glyph = row->glyphs[*area];
1724 end = glyph + row->used[*area];
1725 x -= x0;
1726 while (glyph < end && x >= glyph->pixel_width)
1727 {
1728 x -= glyph->pixel_width;
1729 ++glyph;
1730 }
1731
1732 if (glyph == end)
1733 return NULL;
1734
1735 if (dx)
1736 {
1737 *dx = x;
1738 *dy = y - (row->y + row->ascent - glyph->ascent);
1739 }
1740
1741 *hpos = glyph - row->glyphs[*area];
1742 return glyph;
1743 }
1744
1745
1746 /* EXPORT:
1747 Convert frame-relative x/y to coordinates relative to window W.
1748 Takes pseudo-windows into account. */
1749
1750 void
1751 frame_to_window_pixel_xy (w, x, y)
1752 struct window *w;
1753 int *x, *y;
1754 {
1755 if (w->pseudo_window_p)
1756 {
1757 /* A pseudo-window is always full-width, and starts at the
1758 left edge of the frame, plus a frame border. */
1759 struct frame *f = XFRAME (w->frame);
1760 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1761 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1762 }
1763 else
1764 {
1765 *x -= WINDOW_LEFT_EDGE_X (w);
1766 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1767 }
1768 }
1769
1770 /* EXPORT:
1771 Return in RECTS[] at most N clipping rectangles for glyph string S.
1772 Return the number of stored rectangles. */
1773
1774 int
1775 get_glyph_string_clip_rects (s, rects, n)
1776 struct glyph_string *s;
1777 NativeRectangle *rects;
1778 int n;
1779 {
1780 XRectangle r;
1781
1782 if (n <= 0)
1783 return 0;
1784
1785 if (s->row->full_width_p)
1786 {
1787 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1788 r.x = WINDOW_LEFT_EDGE_X (s->w);
1789 r.width = WINDOW_TOTAL_WIDTH (s->w);
1790
1791 /* Unless displaying a mode or menu bar line, which are always
1792 fully visible, clip to the visible part of the row. */
1793 if (s->w->pseudo_window_p)
1794 r.height = s->row->visible_height;
1795 else
1796 r.height = s->height;
1797 }
1798 else
1799 {
1800 /* This is a text line that may be partially visible. */
1801 r.x = window_box_left (s->w, s->area);
1802 r.width = window_box_width (s->w, s->area);
1803 r.height = s->row->visible_height;
1804 }
1805
1806 if (s->clip_head)
1807 if (r.x < s->clip_head->x)
1808 {
1809 if (r.width >= s->clip_head->x - r.x)
1810 r.width -= s->clip_head->x - r.x;
1811 else
1812 r.width = 0;
1813 r.x = s->clip_head->x;
1814 }
1815 if (s->clip_tail)
1816 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1817 {
1818 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1819 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1820 else
1821 r.width = 0;
1822 }
1823
1824 /* If S draws overlapping rows, it's sufficient to use the top and
1825 bottom of the window for clipping because this glyph string
1826 intentionally draws over other lines. */
1827 if (s->for_overlaps)
1828 {
1829 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1830 r.height = window_text_bottom_y (s->w) - r.y;
1831
1832 /* Alas, the above simple strategy does not work for the
1833 environments with anti-aliased text: if the same text is
1834 drawn onto the same place multiple times, it gets thicker.
1835 If the overlap we are processing is for the erased cursor, we
1836 take the intersection with the rectagle of the cursor. */
1837 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1838 {
1839 XRectangle rc, r_save = r;
1840
1841 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1842 rc.y = s->w->phys_cursor.y;
1843 rc.width = s->w->phys_cursor_width;
1844 rc.height = s->w->phys_cursor_height;
1845
1846 x_intersect_rectangles (&r_save, &rc, &r);
1847 }
1848 }
1849 else
1850 {
1851 /* Don't use S->y for clipping because it doesn't take partially
1852 visible lines into account. For example, it can be negative for
1853 partially visible lines at the top of a window. */
1854 if (!s->row->full_width_p
1855 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1856 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1857 else
1858 r.y = max (0, s->row->y);
1859
1860 /* If drawing a tool-bar window, draw it over the internal border
1861 at the top of the window. */
1862 if (WINDOWP (s->f->tool_bar_window)
1863 && s->w == XWINDOW (s->f->tool_bar_window))
1864 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1865 }
1866
1867 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1868
1869 /* If drawing the cursor, don't let glyph draw outside its
1870 advertised boundaries. Cleartype does this under some circumstances. */
1871 if (s->hl == DRAW_CURSOR)
1872 {
1873 struct glyph *glyph = s->first_glyph;
1874 int height, max_y;
1875
1876 if (s->x > r.x)
1877 {
1878 r.width -= s->x - r.x;
1879 r.x = s->x;
1880 }
1881 r.width = min (r.width, glyph->pixel_width);
1882
1883 /* If r.y is below window bottom, ensure that we still see a cursor. */
1884 height = min (glyph->ascent + glyph->descent,
1885 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1886 max_y = window_text_bottom_y (s->w) - height;
1887 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1888 if (s->ybase - glyph->ascent > max_y)
1889 {
1890 r.y = max_y;
1891 r.height = height;
1892 }
1893 else
1894 {
1895 /* Don't draw cursor glyph taller than our actual glyph. */
1896 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1897 if (height < r.height)
1898 {
1899 max_y = r.y + r.height;
1900 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1901 r.height = min (max_y - r.y, height);
1902 }
1903 }
1904 }
1905
1906 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1907 || (s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1)
1908 {
1909 #ifdef CONVERT_FROM_XRECT
1910 CONVERT_FROM_XRECT (r, *rects);
1911 #else
1912 *rects = r;
1913 #endif
1914 return 1;
1915 }
1916 else
1917 {
1918 /* If we are processing overlapping and allowed to return
1919 multiple clipping rectangles, we exclude the row of the glyph
1920 string from the clipping rectangle. This is to avoid drawing
1921 the same text on the environment with anti-aliasing. */
1922 #ifdef CONVERT_FROM_XRECT
1923 XRectangle rs[2];
1924 #else
1925 XRectangle *rs = rects;
1926 #endif
1927 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1928
1929 if (s->for_overlaps & OVERLAPS_PRED)
1930 {
1931 rs[i] = r;
1932 if (r.y + r.height > row_y)
1933 if (r.y < row_y)
1934 rs[i].height = row_y - r.y;
1935 else
1936 rs[i].height = 0;
1937 i++;
1938 }
1939 if (s->for_overlaps & OVERLAPS_SUCC)
1940 {
1941 rs[i] = r;
1942 if (r.y < row_y + s->row->visible_height)
1943 if (r.y + r.height > row_y + s->row->visible_height)
1944 {
1945 rs[i].y = row_y + s->row->visible_height;
1946 rs[i].height = r.y + r.height - rs[i].y;
1947 }
1948 else
1949 rs[i].height = 0;
1950 i++;
1951 }
1952
1953 n = i;
1954 #ifdef CONVERT_FROM_XRECT
1955 for (i = 0; i < n; i++)
1956 CONVERT_FROM_XRECT (rs[i], rects[i]);
1957 #endif
1958 return n;
1959 }
1960 }
1961
1962 /* EXPORT:
1963 Return in *NR the clipping rectangle for glyph string S. */
1964
1965 void
1966 get_glyph_string_clip_rect (s, nr)
1967 struct glyph_string *s;
1968 NativeRectangle *nr;
1969 {
1970 get_glyph_string_clip_rects (s, nr, 1);
1971 }
1972
1973
1974 /* EXPORT:
1975 Return the position and height of the phys cursor in window W.
1976 Set w->phys_cursor_width to width of phys cursor.
1977 */
1978
1979 int
1980 get_phys_cursor_geometry (w, row, glyph, heightp)
1981 struct window *w;
1982 struct glyph_row *row;
1983 struct glyph *glyph;
1984 int *heightp;
1985 {
1986 struct frame *f = XFRAME (WINDOW_FRAME (w));
1987 int y, wd, h, h0, y0;
1988
1989 /* Compute the width of the rectangle to draw. If on a stretch
1990 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1991 rectangle as wide as the glyph, but use a canonical character
1992 width instead. */
1993 wd = glyph->pixel_width - 1;
1994 #ifdef HAVE_NTGUI
1995 wd++; /* Why? */
1996 #endif
1997 if (glyph->type == STRETCH_GLYPH
1998 && !x_stretch_cursor_p)
1999 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2000 w->phys_cursor_width = wd;
2001
2002 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2003
2004 /* If y is below window bottom, ensure that we still see a cursor. */
2005 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2006
2007 h = max (h0, glyph->ascent + glyph->descent);
2008 h0 = min (h0, glyph->ascent + glyph->descent);
2009
2010 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2011 if (y < y0)
2012 {
2013 h = max (h - (y0 - y) + 1, h0);
2014 y = y0 - 1;
2015 }
2016 else
2017 {
2018 y0 = window_text_bottom_y (w) - h0;
2019 if (y > y0)
2020 {
2021 h += y - y0;
2022 y = y0;
2023 }
2024 }
2025
2026 *heightp = h - 1;
2027 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
2028 }
2029
2030 /*
2031 * Remember which glyph the mouse is over.
2032 */
2033
2034 void
2035 remember_mouse_glyph (f, gx, gy, rect)
2036 struct frame *f;
2037 int gx, gy;
2038 NativeRectangle *rect;
2039 {
2040 Lisp_Object window;
2041 struct window *w;
2042 struct glyph_row *r, *gr, *end_row;
2043 enum window_part part;
2044 enum glyph_row_area area;
2045 int x, y, width, height;
2046
2047 /* Try to determine frame pixel position and size of the glyph under
2048 frame pixel coordinates X/Y on frame F. */
2049
2050 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2051 if (NILP (window))
2052 {
2053 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2054 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2055 goto virtual_glyph;
2056 }
2057
2058 w = XWINDOW (window);
2059 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2060 height = WINDOW_FRAME_LINE_HEIGHT (w);
2061
2062 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2063 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2064
2065 if (w->pseudo_window_p)
2066 {
2067 area = TEXT_AREA;
2068 part = ON_MODE_LINE; /* Don't adjust margin. */
2069 goto text_glyph;
2070 }
2071
2072 switch (part)
2073 {
2074 case ON_LEFT_MARGIN:
2075 area = LEFT_MARGIN_AREA;
2076 goto text_glyph;
2077
2078 case ON_RIGHT_MARGIN:
2079 area = RIGHT_MARGIN_AREA;
2080 goto text_glyph;
2081
2082 case ON_HEADER_LINE:
2083 case ON_MODE_LINE:
2084 gr = (part == ON_HEADER_LINE
2085 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2086 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2087 gy = gr->y;
2088 area = TEXT_AREA;
2089 goto text_glyph_row_found;
2090
2091 case ON_TEXT:
2092 area = TEXT_AREA;
2093
2094 text_glyph:
2095 gr = 0; gy = 0;
2096 for (; r <= end_row && r->enabled_p; ++r)
2097 if (r->y + r->height > y)
2098 {
2099 gr = r; gy = r->y;
2100 break;
2101 }
2102
2103 text_glyph_row_found:
2104 if (gr && gy <= y)
2105 {
2106 struct glyph *g = gr->glyphs[area];
2107 struct glyph *end = g + gr->used[area];
2108
2109 height = gr->height;
2110 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2111 if (gx + g->pixel_width > x)
2112 break;
2113
2114 if (g < end)
2115 {
2116 if (g->type == IMAGE_GLYPH)
2117 {
2118 /* Don't remember when mouse is over image, as
2119 image may have hot-spots. */
2120 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2121 return;
2122 }
2123 width = g->pixel_width;
2124 }
2125 else
2126 {
2127 /* Use nominal char spacing at end of line. */
2128 x -= gx;
2129 gx += (x / width) * width;
2130 }
2131
2132 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2133 gx += window_box_left_offset (w, area);
2134 }
2135 else
2136 {
2137 /* Use nominal line height at end of window. */
2138 gx = (x / width) * width;
2139 y -= gy;
2140 gy += (y / height) * height;
2141 }
2142 break;
2143
2144 case ON_LEFT_FRINGE:
2145 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2146 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2147 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2148 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2149 goto row_glyph;
2150
2151 case ON_RIGHT_FRINGE:
2152 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2153 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2154 : window_box_right_offset (w, TEXT_AREA));
2155 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2156 goto row_glyph;
2157
2158 case ON_SCROLL_BAR:
2159 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2160 ? 0
2161 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2162 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2163 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2164 : 0)));
2165 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2166
2167 row_glyph:
2168 gr = 0, gy = 0;
2169 for (; r <= end_row && r->enabled_p; ++r)
2170 if (r->y + r->height > y)
2171 {
2172 gr = r; gy = r->y;
2173 break;
2174 }
2175
2176 if (gr && gy <= y)
2177 height = gr->height;
2178 else
2179 {
2180 /* Use nominal line height at end of window. */
2181 y -= gy;
2182 gy += (y / height) * height;
2183 }
2184 break;
2185
2186 default:
2187 ;
2188 virtual_glyph:
2189 /* If there is no glyph under the mouse, then we divide the screen
2190 into a grid of the smallest glyph in the frame, and use that
2191 as our "glyph". */
2192
2193 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2194 round down even for negative values. */
2195 if (gx < 0)
2196 gx -= width - 1;
2197 if (gy < 0)
2198 gy -= height - 1;
2199
2200 gx = (gx / width) * width;
2201 gy = (gy / height) * height;
2202
2203 goto store_rect;
2204 }
2205
2206 gx += WINDOW_LEFT_EDGE_X (w);
2207 gy += WINDOW_TOP_EDGE_Y (w);
2208
2209 store_rect:
2210 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2211
2212 /* Visible feedback for debugging. */
2213 #if 0
2214 #if HAVE_X_WINDOWS
2215 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2216 f->output_data.x->normal_gc,
2217 gx, gy, width, height);
2218 #endif
2219 #endif
2220 }
2221
2222
2223 #endif /* HAVE_WINDOW_SYSTEM */
2224
2225 \f
2226 /***********************************************************************
2227 Lisp form evaluation
2228 ***********************************************************************/
2229
2230 /* Error handler for safe_eval and safe_call. */
2231
2232 static Lisp_Object
2233 safe_eval_handler (arg)
2234 Lisp_Object arg;
2235 {
2236 add_to_log ("Error during redisplay: %s", arg, Qnil);
2237 return Qnil;
2238 }
2239
2240
2241 /* Evaluate SEXPR and return the result, or nil if something went
2242 wrong. Prevent redisplay during the evaluation. */
2243
2244 Lisp_Object
2245 safe_eval (sexpr)
2246 Lisp_Object sexpr;
2247 {
2248 Lisp_Object val;
2249
2250 if (inhibit_eval_during_redisplay)
2251 val = Qnil;
2252 else
2253 {
2254 int count = SPECPDL_INDEX ();
2255 struct gcpro gcpro1;
2256
2257 GCPRO1 (sexpr);
2258 specbind (Qinhibit_redisplay, Qt);
2259 /* Use Qt to ensure debugger does not run,
2260 so there is no possibility of wanting to redisplay. */
2261 val = internal_condition_case_1 (Feval, sexpr, Qt,
2262 safe_eval_handler);
2263 UNGCPRO;
2264 val = unbind_to (count, val);
2265 }
2266
2267 return val;
2268 }
2269
2270
2271 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2272 Return the result, or nil if something went wrong. Prevent
2273 redisplay during the evaluation. */
2274
2275 Lisp_Object
2276 safe_call (nargs, args)
2277 int nargs;
2278 Lisp_Object *args;
2279 {
2280 Lisp_Object val;
2281
2282 if (inhibit_eval_during_redisplay)
2283 val = Qnil;
2284 else
2285 {
2286 int count = SPECPDL_INDEX ();
2287 struct gcpro gcpro1;
2288
2289 GCPRO1 (args[0]);
2290 gcpro1.nvars = nargs;
2291 specbind (Qinhibit_redisplay, Qt);
2292 /* Use Qt to ensure debugger does not run,
2293 so there is no possibility of wanting to redisplay. */
2294 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2295 safe_eval_handler);
2296 UNGCPRO;
2297 val = unbind_to (count, val);
2298 }
2299
2300 return val;
2301 }
2302
2303
2304 /* Call function FN with one argument ARG.
2305 Return the result, or nil if something went wrong. */
2306
2307 Lisp_Object
2308 safe_call1 (fn, arg)
2309 Lisp_Object fn, arg;
2310 {
2311 Lisp_Object args[2];
2312 args[0] = fn;
2313 args[1] = arg;
2314 return safe_call (2, args);
2315 }
2316
2317
2318 \f
2319 /***********************************************************************
2320 Debugging
2321 ***********************************************************************/
2322
2323 #if 0
2324
2325 /* Define CHECK_IT to perform sanity checks on iterators.
2326 This is for debugging. It is too slow to do unconditionally. */
2327
2328 static void
2329 check_it (it)
2330 struct it *it;
2331 {
2332 if (it->method == GET_FROM_STRING)
2333 {
2334 xassert (STRINGP (it->string));
2335 xassert (IT_STRING_CHARPOS (*it) >= 0);
2336 }
2337 else
2338 {
2339 xassert (IT_STRING_CHARPOS (*it) < 0);
2340 if (it->method == GET_FROM_BUFFER)
2341 {
2342 /* Check that character and byte positions agree. */
2343 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2344 }
2345 }
2346
2347 if (it->dpvec)
2348 xassert (it->current.dpvec_index >= 0);
2349 else
2350 xassert (it->current.dpvec_index < 0);
2351 }
2352
2353 #define CHECK_IT(IT) check_it ((IT))
2354
2355 #else /* not 0 */
2356
2357 #define CHECK_IT(IT) (void) 0
2358
2359 #endif /* not 0 */
2360
2361
2362 #if GLYPH_DEBUG
2363
2364 /* Check that the window end of window W is what we expect it
2365 to be---the last row in the current matrix displaying text. */
2366
2367 static void
2368 check_window_end (w)
2369 struct window *w;
2370 {
2371 if (!MINI_WINDOW_P (w)
2372 && !NILP (w->window_end_valid))
2373 {
2374 struct glyph_row *row;
2375 xassert ((row = MATRIX_ROW (w->current_matrix,
2376 XFASTINT (w->window_end_vpos)),
2377 !row->enabled_p
2378 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2379 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2380 }
2381 }
2382
2383 #define CHECK_WINDOW_END(W) check_window_end ((W))
2384
2385 #else /* not GLYPH_DEBUG */
2386
2387 #define CHECK_WINDOW_END(W) (void) 0
2388
2389 #endif /* not GLYPH_DEBUG */
2390
2391
2392 \f
2393 /***********************************************************************
2394 Iterator initialization
2395 ***********************************************************************/
2396
2397 /* Initialize IT for displaying current_buffer in window W, starting
2398 at character position CHARPOS. CHARPOS < 0 means that no buffer
2399 position is specified which is useful when the iterator is assigned
2400 a position later. BYTEPOS is the byte position corresponding to
2401 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2402
2403 If ROW is not null, calls to produce_glyphs with IT as parameter
2404 will produce glyphs in that row.
2405
2406 BASE_FACE_ID is the id of a base face to use. It must be one of
2407 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2408 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2409 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2410
2411 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2412 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2413 will be initialized to use the corresponding mode line glyph row of
2414 the desired matrix of W. */
2415
2416 void
2417 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2418 struct it *it;
2419 struct window *w;
2420 int charpos, bytepos;
2421 struct glyph_row *row;
2422 enum face_id base_face_id;
2423 {
2424 int highlight_region_p;
2425
2426 /* Some precondition checks. */
2427 xassert (w != NULL && it != NULL);
2428 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2429 && charpos <= ZV));
2430
2431 /* If face attributes have been changed since the last redisplay,
2432 free realized faces now because they depend on face definitions
2433 that might have changed. Don't free faces while there might be
2434 desired matrices pending which reference these faces. */
2435 if (face_change_count && !inhibit_free_realized_faces)
2436 {
2437 face_change_count = 0;
2438 free_all_realized_faces (Qnil);
2439 }
2440
2441 /* Use one of the mode line rows of W's desired matrix if
2442 appropriate. */
2443 if (row == NULL)
2444 {
2445 if (base_face_id == MODE_LINE_FACE_ID
2446 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2447 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2448 else if (base_face_id == HEADER_LINE_FACE_ID)
2449 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2450 }
2451
2452 /* Clear IT. */
2453 bzero (it, sizeof *it);
2454 it->current.overlay_string_index = -1;
2455 it->current.dpvec_index = -1;
2456 it->base_face_id = base_face_id;
2457 it->string = Qnil;
2458 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2459
2460 /* The window in which we iterate over current_buffer: */
2461 XSETWINDOW (it->window, w);
2462 it->w = w;
2463 it->f = XFRAME (w->frame);
2464
2465 /* Extra space between lines (on window systems only). */
2466 if (base_face_id == DEFAULT_FACE_ID
2467 && FRAME_WINDOW_P (it->f))
2468 {
2469 if (NATNUMP (current_buffer->extra_line_spacing))
2470 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2471 else if (FLOATP (current_buffer->extra_line_spacing))
2472 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2473 * FRAME_LINE_HEIGHT (it->f));
2474 else if (it->f->extra_line_spacing > 0)
2475 it->extra_line_spacing = it->f->extra_line_spacing;
2476 it->max_extra_line_spacing = 0;
2477 }
2478
2479 /* If realized faces have been removed, e.g. because of face
2480 attribute changes of named faces, recompute them. When running
2481 in batch mode, the face cache of Vterminal_frame is null. If
2482 we happen to get called, make a dummy face cache. */
2483 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2484 init_frame_faces (it->f);
2485 if (FRAME_FACE_CACHE (it->f)->used == 0)
2486 recompute_basic_faces (it->f);
2487
2488 /* Current value of the `slice', `space-width', and 'height' properties. */
2489 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2490 it->space_width = Qnil;
2491 it->font_height = Qnil;
2492 it->override_ascent = -1;
2493
2494 /* Are control characters displayed as `^C'? */
2495 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2496
2497 /* -1 means everything between a CR and the following line end
2498 is invisible. >0 means lines indented more than this value are
2499 invisible. */
2500 it->selective = (INTEGERP (current_buffer->selective_display)
2501 ? XFASTINT (current_buffer->selective_display)
2502 : (!NILP (current_buffer->selective_display)
2503 ? -1 : 0));
2504 it->selective_display_ellipsis_p
2505 = !NILP (current_buffer->selective_display_ellipses);
2506
2507 /* Display table to use. */
2508 it->dp = window_display_table (w);
2509
2510 /* Are multibyte characters enabled in current_buffer? */
2511 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2512
2513 /* Non-zero if we should highlight the region. */
2514 highlight_region_p
2515 = (!NILP (Vtransient_mark_mode)
2516 && !NILP (current_buffer->mark_active)
2517 && XMARKER (current_buffer->mark)->buffer != 0);
2518
2519 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2520 start and end of a visible region in window IT->w. Set both to
2521 -1 to indicate no region. */
2522 if (highlight_region_p
2523 /* Maybe highlight only in selected window. */
2524 && (/* Either show region everywhere. */
2525 highlight_nonselected_windows
2526 /* Or show region in the selected window. */
2527 || w == XWINDOW (selected_window)
2528 /* Or show the region if we are in the mini-buffer and W is
2529 the window the mini-buffer refers to. */
2530 || (MINI_WINDOW_P (XWINDOW (selected_window))
2531 && WINDOWP (minibuf_selected_window)
2532 && w == XWINDOW (minibuf_selected_window))))
2533 {
2534 int charpos = marker_position (current_buffer->mark);
2535 it->region_beg_charpos = min (PT, charpos);
2536 it->region_end_charpos = max (PT, charpos);
2537 }
2538 else
2539 it->region_beg_charpos = it->region_end_charpos = -1;
2540
2541 /* Get the position at which the redisplay_end_trigger hook should
2542 be run, if it is to be run at all. */
2543 if (MARKERP (w->redisplay_end_trigger)
2544 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2545 it->redisplay_end_trigger_charpos
2546 = marker_position (w->redisplay_end_trigger);
2547 else if (INTEGERP (w->redisplay_end_trigger))
2548 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2549
2550 /* Correct bogus values of tab_width. */
2551 it->tab_width = XINT (current_buffer->tab_width);
2552 if (it->tab_width <= 0 || it->tab_width > 1000)
2553 it->tab_width = 8;
2554
2555 /* Are lines in the display truncated? */
2556 it->truncate_lines_p
2557 = (base_face_id != DEFAULT_FACE_ID
2558 || XINT (it->w->hscroll)
2559 || (truncate_partial_width_windows
2560 && !WINDOW_FULL_WIDTH_P (it->w))
2561 || !NILP (current_buffer->truncate_lines));
2562
2563 /* Get dimensions of truncation and continuation glyphs. These are
2564 displayed as fringe bitmaps under X, so we don't need them for such
2565 frames. */
2566 if (!FRAME_WINDOW_P (it->f))
2567 {
2568 if (it->truncate_lines_p)
2569 {
2570 /* We will need the truncation glyph. */
2571 xassert (it->glyph_row == NULL);
2572 produce_special_glyphs (it, IT_TRUNCATION);
2573 it->truncation_pixel_width = it->pixel_width;
2574 }
2575 else
2576 {
2577 /* We will need the continuation glyph. */
2578 xassert (it->glyph_row == NULL);
2579 produce_special_glyphs (it, IT_CONTINUATION);
2580 it->continuation_pixel_width = it->pixel_width;
2581 }
2582
2583 /* Reset these values to zero because the produce_special_glyphs
2584 above has changed them. */
2585 it->pixel_width = it->ascent = it->descent = 0;
2586 it->phys_ascent = it->phys_descent = 0;
2587 }
2588
2589 /* Set this after getting the dimensions of truncation and
2590 continuation glyphs, so that we don't produce glyphs when calling
2591 produce_special_glyphs, above. */
2592 it->glyph_row = row;
2593 it->area = TEXT_AREA;
2594
2595 /* Get the dimensions of the display area. The display area
2596 consists of the visible window area plus a horizontally scrolled
2597 part to the left of the window. All x-values are relative to the
2598 start of this total display area. */
2599 if (base_face_id != DEFAULT_FACE_ID)
2600 {
2601 /* Mode lines, menu bar in terminal frames. */
2602 it->first_visible_x = 0;
2603 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2604 }
2605 else
2606 {
2607 it->first_visible_x
2608 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2609 it->last_visible_x = (it->first_visible_x
2610 + window_box_width (w, TEXT_AREA));
2611
2612 /* If we truncate lines, leave room for the truncator glyph(s) at
2613 the right margin. Otherwise, leave room for the continuation
2614 glyph(s). Truncation and continuation glyphs are not inserted
2615 for window-based redisplay. */
2616 if (!FRAME_WINDOW_P (it->f))
2617 {
2618 if (it->truncate_lines_p)
2619 it->last_visible_x -= it->truncation_pixel_width;
2620 else
2621 it->last_visible_x -= it->continuation_pixel_width;
2622 }
2623
2624 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2625 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2626 }
2627
2628 /* Leave room for a border glyph. */
2629 if (!FRAME_WINDOW_P (it->f)
2630 && !WINDOW_RIGHTMOST_P (it->w))
2631 it->last_visible_x -= 1;
2632
2633 it->last_visible_y = window_text_bottom_y (w);
2634
2635 /* For mode lines and alike, arrange for the first glyph having a
2636 left box line if the face specifies a box. */
2637 if (base_face_id != DEFAULT_FACE_ID)
2638 {
2639 struct face *face;
2640
2641 it->face_id = base_face_id;
2642
2643 /* If we have a boxed mode line, make the first character appear
2644 with a left box line. */
2645 face = FACE_FROM_ID (it->f, base_face_id);
2646 if (face->box != FACE_NO_BOX)
2647 it->start_of_box_run_p = 1;
2648 }
2649
2650 /* If a buffer position was specified, set the iterator there,
2651 getting overlays and face properties from that position. */
2652 if (charpos >= BUF_BEG (current_buffer))
2653 {
2654 it->end_charpos = ZV;
2655 it->face_id = -1;
2656 IT_CHARPOS (*it) = charpos;
2657
2658 /* Compute byte position if not specified. */
2659 if (bytepos < charpos)
2660 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2661 else
2662 IT_BYTEPOS (*it) = bytepos;
2663
2664 it->start = it->current;
2665
2666 /* Compute faces etc. */
2667 reseat (it, it->current.pos, 1);
2668 }
2669
2670 CHECK_IT (it);
2671 }
2672
2673
2674 /* Initialize IT for the display of window W with window start POS. */
2675
2676 void
2677 start_display (it, w, pos)
2678 struct it *it;
2679 struct window *w;
2680 struct text_pos pos;
2681 {
2682 struct glyph_row *row;
2683 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2684
2685 row = w->desired_matrix->rows + first_vpos;
2686 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2687 it->first_vpos = first_vpos;
2688
2689 /* Don't reseat to previous visible line start if current start
2690 position is in a string or image. */
2691 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2692 {
2693 int start_at_line_beg_p;
2694 int first_y = it->current_y;
2695
2696 /* If window start is not at a line start, skip forward to POS to
2697 get the correct continuation lines width. */
2698 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2699 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2700 if (!start_at_line_beg_p)
2701 {
2702 int new_x;
2703
2704 reseat_at_previous_visible_line_start (it);
2705 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2706
2707 new_x = it->current_x + it->pixel_width;
2708
2709 /* If lines are continued, this line may end in the middle
2710 of a multi-glyph character (e.g. a control character
2711 displayed as \003, or in the middle of an overlay
2712 string). In this case move_it_to above will not have
2713 taken us to the start of the continuation line but to the
2714 end of the continued line. */
2715 if (it->current_x > 0
2716 && !it->truncate_lines_p /* Lines are continued. */
2717 && (/* And glyph doesn't fit on the line. */
2718 new_x > it->last_visible_x
2719 /* Or it fits exactly and we're on a window
2720 system frame. */
2721 || (new_x == it->last_visible_x
2722 && FRAME_WINDOW_P (it->f))))
2723 {
2724 if (it->current.dpvec_index >= 0
2725 || it->current.overlay_string_index >= 0)
2726 {
2727 set_iterator_to_next (it, 1);
2728 move_it_in_display_line_to (it, -1, -1, 0);
2729 }
2730
2731 it->continuation_lines_width += it->current_x;
2732 }
2733
2734 /* We're starting a new display line, not affected by the
2735 height of the continued line, so clear the appropriate
2736 fields in the iterator structure. */
2737 it->max_ascent = it->max_descent = 0;
2738 it->max_phys_ascent = it->max_phys_descent = 0;
2739
2740 it->current_y = first_y;
2741 it->vpos = 0;
2742 it->current_x = it->hpos = 0;
2743 }
2744 }
2745
2746 #if 0 /* Don't assert the following because start_display is sometimes
2747 called intentionally with a window start that is not at a
2748 line start. Please leave this code in as a comment. */
2749
2750 /* Window start should be on a line start, now. */
2751 xassert (it->continuation_lines_width
2752 || IT_CHARPOS (it) == BEGV
2753 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2754 #endif /* 0 */
2755 }
2756
2757
2758 /* Return 1 if POS is a position in ellipses displayed for invisible
2759 text. W is the window we display, for text property lookup. */
2760
2761 static int
2762 in_ellipses_for_invisible_text_p (pos, w)
2763 struct display_pos *pos;
2764 struct window *w;
2765 {
2766 Lisp_Object prop, window;
2767 int ellipses_p = 0;
2768 int charpos = CHARPOS (pos->pos);
2769
2770 /* If POS specifies a position in a display vector, this might
2771 be for an ellipsis displayed for invisible text. We won't
2772 get the iterator set up for delivering that ellipsis unless
2773 we make sure that it gets aware of the invisible text. */
2774 if (pos->dpvec_index >= 0
2775 && pos->overlay_string_index < 0
2776 && CHARPOS (pos->string_pos) < 0
2777 && charpos > BEGV
2778 && (XSETWINDOW (window, w),
2779 prop = Fget_char_property (make_number (charpos),
2780 Qinvisible, window),
2781 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2782 {
2783 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2784 window);
2785 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2786 }
2787
2788 return ellipses_p;
2789 }
2790
2791
2792 /* Initialize IT for stepping through current_buffer in window W,
2793 starting at position POS that includes overlay string and display
2794 vector/ control character translation position information. Value
2795 is zero if there are overlay strings with newlines at POS. */
2796
2797 static int
2798 init_from_display_pos (it, w, pos)
2799 struct it *it;
2800 struct window *w;
2801 struct display_pos *pos;
2802 {
2803 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2804 int i, overlay_strings_with_newlines = 0;
2805
2806 /* If POS specifies a position in a display vector, this might
2807 be for an ellipsis displayed for invisible text. We won't
2808 get the iterator set up for delivering that ellipsis unless
2809 we make sure that it gets aware of the invisible text. */
2810 if (in_ellipses_for_invisible_text_p (pos, w))
2811 {
2812 --charpos;
2813 bytepos = 0;
2814 }
2815
2816 /* Keep in mind: the call to reseat in init_iterator skips invisible
2817 text, so we might end up at a position different from POS. This
2818 is only a problem when POS is a row start after a newline and an
2819 overlay starts there with an after-string, and the overlay has an
2820 invisible property. Since we don't skip invisible text in
2821 display_line and elsewhere immediately after consuming the
2822 newline before the row start, such a POS will not be in a string,
2823 but the call to init_iterator below will move us to the
2824 after-string. */
2825 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2826
2827 /* This only scans the current chunk -- it should scan all chunks.
2828 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2829 to 16 in 22.1 to make this a lesser problem. */
2830 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2831 {
2832 const char *s = SDATA (it->overlay_strings[i]);
2833 const char *e = s + SBYTES (it->overlay_strings[i]);
2834
2835 while (s < e && *s != '\n')
2836 ++s;
2837
2838 if (s < e)
2839 {
2840 overlay_strings_with_newlines = 1;
2841 break;
2842 }
2843 }
2844
2845 /* If position is within an overlay string, set up IT to the right
2846 overlay string. */
2847 if (pos->overlay_string_index >= 0)
2848 {
2849 int relative_index;
2850
2851 /* If the first overlay string happens to have a `display'
2852 property for an image, the iterator will be set up for that
2853 image, and we have to undo that setup first before we can
2854 correct the overlay string index. */
2855 if (it->method == GET_FROM_IMAGE)
2856 pop_it (it);
2857
2858 /* We already have the first chunk of overlay strings in
2859 IT->overlay_strings. Load more until the one for
2860 pos->overlay_string_index is in IT->overlay_strings. */
2861 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2862 {
2863 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2864 it->current.overlay_string_index = 0;
2865 while (n--)
2866 {
2867 load_overlay_strings (it, 0);
2868 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2869 }
2870 }
2871
2872 it->current.overlay_string_index = pos->overlay_string_index;
2873 relative_index = (it->current.overlay_string_index
2874 % OVERLAY_STRING_CHUNK_SIZE);
2875 it->string = it->overlay_strings[relative_index];
2876 xassert (STRINGP (it->string));
2877 it->current.string_pos = pos->string_pos;
2878 it->method = GET_FROM_STRING;
2879 }
2880
2881 #if 0 /* This is bogus because POS not having an overlay string
2882 position does not mean it's after the string. Example: A
2883 line starting with a before-string and initialization of IT
2884 to the previous row's end position. */
2885 else if (it->current.overlay_string_index >= 0)
2886 {
2887 /* If POS says we're already after an overlay string ending at
2888 POS, make sure to pop the iterator because it will be in
2889 front of that overlay string. When POS is ZV, we've thereby
2890 also ``processed'' overlay strings at ZV. */
2891 while (it->sp)
2892 pop_it (it);
2893 it->current.overlay_string_index = -1;
2894 it->method = GET_FROM_BUFFER;
2895 if (CHARPOS (pos->pos) == ZV)
2896 it->overlay_strings_at_end_processed_p = 1;
2897 }
2898 #endif /* 0 */
2899
2900 if (CHARPOS (pos->string_pos) >= 0)
2901 {
2902 /* Recorded position is not in an overlay string, but in another
2903 string. This can only be a string from a `display' property.
2904 IT should already be filled with that string. */
2905 it->current.string_pos = pos->string_pos;
2906 xassert (STRINGP (it->string));
2907 }
2908
2909 /* Restore position in display vector translations, control
2910 character translations or ellipses. */
2911 if (pos->dpvec_index >= 0)
2912 {
2913 if (it->dpvec == NULL)
2914 get_next_display_element (it);
2915 xassert (it->dpvec && it->current.dpvec_index == 0);
2916 it->current.dpvec_index = pos->dpvec_index;
2917 }
2918
2919 CHECK_IT (it);
2920 return !overlay_strings_with_newlines;
2921 }
2922
2923
2924 /* Initialize IT for stepping through current_buffer in window W
2925 starting at ROW->start. */
2926
2927 static void
2928 init_to_row_start (it, w, row)
2929 struct it *it;
2930 struct window *w;
2931 struct glyph_row *row;
2932 {
2933 init_from_display_pos (it, w, &row->start);
2934 it->start = row->start;
2935 it->continuation_lines_width = row->continuation_lines_width;
2936 CHECK_IT (it);
2937 }
2938
2939
2940 /* Initialize IT for stepping through current_buffer in window W
2941 starting in the line following ROW, i.e. starting at ROW->end.
2942 Value is zero if there are overlay strings with newlines at ROW's
2943 end position. */
2944
2945 static int
2946 init_to_row_end (it, w, row)
2947 struct it *it;
2948 struct window *w;
2949 struct glyph_row *row;
2950 {
2951 int success = 0;
2952
2953 if (init_from_display_pos (it, w, &row->end))
2954 {
2955 if (row->continued_p)
2956 it->continuation_lines_width
2957 = row->continuation_lines_width + row->pixel_width;
2958 CHECK_IT (it);
2959 success = 1;
2960 }
2961
2962 return success;
2963 }
2964
2965
2966
2967 \f
2968 /***********************************************************************
2969 Text properties
2970 ***********************************************************************/
2971
2972 /* Called when IT reaches IT->stop_charpos. Handle text property and
2973 overlay changes. Set IT->stop_charpos to the next position where
2974 to stop. */
2975
2976 static void
2977 handle_stop (it)
2978 struct it *it;
2979 {
2980 enum prop_handled handled;
2981 int handle_overlay_change_p;
2982 struct props *p;
2983
2984 it->dpvec = NULL;
2985 it->current.dpvec_index = -1;
2986 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2987 it->ignore_overlay_strings_at_pos_p = 0;
2988
2989 /* Use face of preceding text for ellipsis (if invisible) */
2990 if (it->selective_display_ellipsis_p)
2991 it->saved_face_id = it->face_id;
2992
2993 do
2994 {
2995 handled = HANDLED_NORMALLY;
2996
2997 /* Call text property handlers. */
2998 for (p = it_props; p->handler; ++p)
2999 {
3000 handled = p->handler (it);
3001
3002 if (handled == HANDLED_RECOMPUTE_PROPS)
3003 break;
3004 else if (handled == HANDLED_RETURN)
3005 return;
3006 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3007 handle_overlay_change_p = 0;
3008 }
3009
3010 if (handled != HANDLED_RECOMPUTE_PROPS)
3011 {
3012 /* Don't check for overlay strings below when set to deliver
3013 characters from a display vector. */
3014 if (it->method == GET_FROM_DISPLAY_VECTOR)
3015 handle_overlay_change_p = 0;
3016
3017 /* Handle overlay changes. */
3018 if (handle_overlay_change_p)
3019 handled = handle_overlay_change (it);
3020
3021 /* Determine where to stop next. */
3022 if (handled == HANDLED_NORMALLY)
3023 compute_stop_pos (it);
3024 }
3025 }
3026 while (handled == HANDLED_RECOMPUTE_PROPS);
3027 }
3028
3029
3030 /* Compute IT->stop_charpos from text property and overlay change
3031 information for IT's current position. */
3032
3033 static void
3034 compute_stop_pos (it)
3035 struct it *it;
3036 {
3037 register INTERVAL iv, next_iv;
3038 Lisp_Object object, limit, position;
3039
3040 /* If nowhere else, stop at the end. */
3041 it->stop_charpos = it->end_charpos;
3042
3043 if (STRINGP (it->string))
3044 {
3045 /* Strings are usually short, so don't limit the search for
3046 properties. */
3047 object = it->string;
3048 limit = Qnil;
3049 position = make_number (IT_STRING_CHARPOS (*it));
3050 }
3051 else
3052 {
3053 int charpos;
3054
3055 /* If next overlay change is in front of the current stop pos
3056 (which is IT->end_charpos), stop there. Note: value of
3057 next_overlay_change is point-max if no overlay change
3058 follows. */
3059 charpos = next_overlay_change (IT_CHARPOS (*it));
3060 if (charpos < it->stop_charpos)
3061 it->stop_charpos = charpos;
3062
3063 /* If showing the region, we have to stop at the region
3064 start or end because the face might change there. */
3065 if (it->region_beg_charpos > 0)
3066 {
3067 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3068 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3069 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3070 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3071 }
3072
3073 /* Set up variables for computing the stop position from text
3074 property changes. */
3075 XSETBUFFER (object, current_buffer);
3076 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3077 position = make_number (IT_CHARPOS (*it));
3078
3079 }
3080
3081 /* Get the interval containing IT's position. Value is a null
3082 interval if there isn't such an interval. */
3083 iv = validate_interval_range (object, &position, &position, 0);
3084 if (!NULL_INTERVAL_P (iv))
3085 {
3086 Lisp_Object values_here[LAST_PROP_IDX];
3087 struct props *p;
3088
3089 /* Get properties here. */
3090 for (p = it_props; p->handler; ++p)
3091 values_here[p->idx] = textget (iv->plist, *p->name);
3092
3093 /* Look for an interval following iv that has different
3094 properties. */
3095 for (next_iv = next_interval (iv);
3096 (!NULL_INTERVAL_P (next_iv)
3097 && (NILP (limit)
3098 || XFASTINT (limit) > next_iv->position));
3099 next_iv = next_interval (next_iv))
3100 {
3101 for (p = it_props; p->handler; ++p)
3102 {
3103 Lisp_Object new_value;
3104
3105 new_value = textget (next_iv->plist, *p->name);
3106 if (!EQ (values_here[p->idx], new_value))
3107 break;
3108 }
3109
3110 if (p->handler)
3111 break;
3112 }
3113
3114 if (!NULL_INTERVAL_P (next_iv))
3115 {
3116 if (INTEGERP (limit)
3117 && next_iv->position >= XFASTINT (limit))
3118 /* No text property change up to limit. */
3119 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3120 else
3121 /* Text properties change in next_iv. */
3122 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3123 }
3124 }
3125
3126 xassert (STRINGP (it->string)
3127 || (it->stop_charpos >= BEGV
3128 && it->stop_charpos >= IT_CHARPOS (*it)));
3129 }
3130
3131
3132 /* Return the position of the next overlay change after POS in
3133 current_buffer. Value is point-max if no overlay change
3134 follows. This is like `next-overlay-change' but doesn't use
3135 xmalloc. */
3136
3137 static int
3138 next_overlay_change (pos)
3139 int pos;
3140 {
3141 int noverlays;
3142 int endpos;
3143 Lisp_Object *overlays;
3144 int i;
3145
3146 /* Get all overlays at the given position. */
3147 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3148
3149 /* If any of these overlays ends before endpos,
3150 use its ending point instead. */
3151 for (i = 0; i < noverlays; ++i)
3152 {
3153 Lisp_Object oend;
3154 int oendpos;
3155
3156 oend = OVERLAY_END (overlays[i]);
3157 oendpos = OVERLAY_POSITION (oend);
3158 endpos = min (endpos, oendpos);
3159 }
3160
3161 return endpos;
3162 }
3163
3164
3165 \f
3166 /***********************************************************************
3167 Fontification
3168 ***********************************************************************/
3169
3170 /* Handle changes in the `fontified' property of the current buffer by
3171 calling hook functions from Qfontification_functions to fontify
3172 regions of text. */
3173
3174 static enum prop_handled
3175 handle_fontified_prop (it)
3176 struct it *it;
3177 {
3178 Lisp_Object prop, pos;
3179 enum prop_handled handled = HANDLED_NORMALLY;
3180
3181 if (!NILP (Vmemory_full))
3182 return handled;
3183
3184 /* Get the value of the `fontified' property at IT's current buffer
3185 position. (The `fontified' property doesn't have a special
3186 meaning in strings.) If the value is nil, call functions from
3187 Qfontification_functions. */
3188 if (!STRINGP (it->string)
3189 && it->s == NULL
3190 && !NILP (Vfontification_functions)
3191 && !NILP (Vrun_hooks)
3192 && (pos = make_number (IT_CHARPOS (*it)),
3193 prop = Fget_char_property (pos, Qfontified, Qnil),
3194 NILP (prop)))
3195 {
3196 int count = SPECPDL_INDEX ();
3197 Lisp_Object val;
3198
3199 val = Vfontification_functions;
3200 specbind (Qfontification_functions, Qnil);
3201
3202 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3203 safe_call1 (val, pos);
3204 else
3205 {
3206 Lisp_Object globals, fn;
3207 struct gcpro gcpro1, gcpro2;
3208
3209 globals = Qnil;
3210 GCPRO2 (val, globals);
3211
3212 for (; CONSP (val); val = XCDR (val))
3213 {
3214 fn = XCAR (val);
3215
3216 if (EQ (fn, Qt))
3217 {
3218 /* A value of t indicates this hook has a local
3219 binding; it means to run the global binding too.
3220 In a global value, t should not occur. If it
3221 does, we must ignore it to avoid an endless
3222 loop. */
3223 for (globals = Fdefault_value (Qfontification_functions);
3224 CONSP (globals);
3225 globals = XCDR (globals))
3226 {
3227 fn = XCAR (globals);
3228 if (!EQ (fn, Qt))
3229 safe_call1 (fn, pos);
3230 }
3231 }
3232 else
3233 safe_call1 (fn, pos);
3234 }
3235
3236 UNGCPRO;
3237 }
3238
3239 unbind_to (count, Qnil);
3240
3241 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3242 something. This avoids an endless loop if they failed to
3243 fontify the text for which reason ever. */
3244 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3245 handled = HANDLED_RECOMPUTE_PROPS;
3246 }
3247
3248 return handled;
3249 }
3250
3251
3252 \f
3253 /***********************************************************************
3254 Faces
3255 ***********************************************************************/
3256
3257 /* Set up iterator IT from face properties at its current position.
3258 Called from handle_stop. */
3259
3260 static enum prop_handled
3261 handle_face_prop (it)
3262 struct it *it;
3263 {
3264 int new_face_id, next_stop;
3265
3266 if (!STRINGP (it->string))
3267 {
3268 new_face_id
3269 = face_at_buffer_position (it->w,
3270 IT_CHARPOS (*it),
3271 it->region_beg_charpos,
3272 it->region_end_charpos,
3273 &next_stop,
3274 (IT_CHARPOS (*it)
3275 + TEXT_PROP_DISTANCE_LIMIT),
3276 0);
3277
3278 /* Is this a start of a run of characters with box face?
3279 Caveat: this can be called for a freshly initialized
3280 iterator; face_id is -1 in this case. We know that the new
3281 face will not change until limit, i.e. if the new face has a
3282 box, all characters up to limit will have one. But, as
3283 usual, we don't know whether limit is really the end. */
3284 if (new_face_id != it->face_id)
3285 {
3286 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3287
3288 /* If new face has a box but old face has not, this is
3289 the start of a run of characters with box, i.e. it has
3290 a shadow on the left side. The value of face_id of the
3291 iterator will be -1 if this is the initial call that gets
3292 the face. In this case, we have to look in front of IT's
3293 position and see whether there is a face != new_face_id. */
3294 it->start_of_box_run_p
3295 = (new_face->box != FACE_NO_BOX
3296 && (it->face_id >= 0
3297 || IT_CHARPOS (*it) == BEG
3298 || new_face_id != face_before_it_pos (it)));
3299 it->face_box_p = new_face->box != FACE_NO_BOX;
3300 }
3301 }
3302 else
3303 {
3304 int base_face_id, bufpos;
3305
3306 if (it->current.overlay_string_index >= 0)
3307 bufpos = IT_CHARPOS (*it);
3308 else
3309 bufpos = 0;
3310
3311 /* For strings from a buffer, i.e. overlay strings or strings
3312 from a `display' property, use the face at IT's current
3313 buffer position as the base face to merge with, so that
3314 overlay strings appear in the same face as surrounding
3315 text, unless they specify their own faces. */
3316 base_face_id = underlying_face_id (it);
3317
3318 new_face_id = face_at_string_position (it->w,
3319 it->string,
3320 IT_STRING_CHARPOS (*it),
3321 bufpos,
3322 it->region_beg_charpos,
3323 it->region_end_charpos,
3324 &next_stop,
3325 base_face_id, 0);
3326
3327 #if 0 /* This shouldn't be neccessary. Let's check it. */
3328 /* If IT is used to display a mode line we would really like to
3329 use the mode line face instead of the frame's default face. */
3330 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3331 && new_face_id == DEFAULT_FACE_ID)
3332 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3333 #endif
3334
3335 /* Is this a start of a run of characters with box? Caveat:
3336 this can be called for a freshly allocated iterator; face_id
3337 is -1 is this case. We know that the new face will not
3338 change until the next check pos, i.e. if the new face has a
3339 box, all characters up to that position will have a
3340 box. But, as usual, we don't know whether that position
3341 is really the end. */
3342 if (new_face_id != it->face_id)
3343 {
3344 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3345 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3346
3347 /* If new face has a box but old face hasn't, this is the
3348 start of a run of characters with box, i.e. it has a
3349 shadow on the left side. */
3350 it->start_of_box_run_p
3351 = new_face->box && (old_face == NULL || !old_face->box);
3352 it->face_box_p = new_face->box != FACE_NO_BOX;
3353 }
3354 }
3355
3356 it->face_id = new_face_id;
3357 return HANDLED_NORMALLY;
3358 }
3359
3360
3361 /* Return the ID of the face ``underlying'' IT's current position,
3362 which is in a string. If the iterator is associated with a
3363 buffer, return the face at IT's current buffer position.
3364 Otherwise, use the iterator's base_face_id. */
3365
3366 static int
3367 underlying_face_id (it)
3368 struct it *it;
3369 {
3370 int face_id = it->base_face_id, i;
3371
3372 xassert (STRINGP (it->string));
3373
3374 for (i = it->sp - 1; i >= 0; --i)
3375 if (NILP (it->stack[i].string))
3376 face_id = it->stack[i].face_id;
3377
3378 return face_id;
3379 }
3380
3381
3382 /* Compute the face one character before or after the current position
3383 of IT. BEFORE_P non-zero means get the face in front of IT's
3384 position. Value is the id of the face. */
3385
3386 static int
3387 face_before_or_after_it_pos (it, before_p)
3388 struct it *it;
3389 int before_p;
3390 {
3391 int face_id, limit;
3392 int next_check_charpos;
3393 struct text_pos pos;
3394
3395 xassert (it->s == NULL);
3396
3397 if (STRINGP (it->string))
3398 {
3399 int bufpos, base_face_id;
3400
3401 /* No face change past the end of the string (for the case
3402 we are padding with spaces). No face change before the
3403 string start. */
3404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3405 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3406 return it->face_id;
3407
3408 /* Set pos to the position before or after IT's current position. */
3409 if (before_p)
3410 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3411 else
3412 /* For composition, we must check the character after the
3413 composition. */
3414 pos = (it->what == IT_COMPOSITION
3415 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3416 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3417
3418 if (it->current.overlay_string_index >= 0)
3419 bufpos = IT_CHARPOS (*it);
3420 else
3421 bufpos = 0;
3422
3423 base_face_id = underlying_face_id (it);
3424
3425 /* Get the face for ASCII, or unibyte. */
3426 face_id = face_at_string_position (it->w,
3427 it->string,
3428 CHARPOS (pos),
3429 bufpos,
3430 it->region_beg_charpos,
3431 it->region_end_charpos,
3432 &next_check_charpos,
3433 base_face_id, 0);
3434
3435 /* Correct the face for charsets different from ASCII. Do it
3436 for the multibyte case only. The face returned above is
3437 suitable for unibyte text if IT->string is unibyte. */
3438 if (STRING_MULTIBYTE (it->string))
3439 {
3440 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3441 int rest = SBYTES (it->string) - BYTEPOS (pos);
3442 int c, len;
3443 struct face *face = FACE_FROM_ID (it->f, face_id);
3444
3445 c = string_char_and_length (p, rest, &len);
3446 face_id = FACE_FOR_CHAR (it->f, face, c);
3447 }
3448 }
3449 else
3450 {
3451 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3452 || (IT_CHARPOS (*it) <= BEGV && before_p))
3453 return it->face_id;
3454
3455 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3456 pos = it->current.pos;
3457
3458 if (before_p)
3459 DEC_TEXT_POS (pos, it->multibyte_p);
3460 else
3461 {
3462 if (it->what == IT_COMPOSITION)
3463 /* For composition, we must check the position after the
3464 composition. */
3465 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3466 else
3467 INC_TEXT_POS (pos, it->multibyte_p);
3468 }
3469
3470 /* Determine face for CHARSET_ASCII, or unibyte. */
3471 face_id = face_at_buffer_position (it->w,
3472 CHARPOS (pos),
3473 it->region_beg_charpos,
3474 it->region_end_charpos,
3475 &next_check_charpos,
3476 limit, 0);
3477
3478 /* Correct the face for charsets different from ASCII. Do it
3479 for the multibyte case only. The face returned above is
3480 suitable for unibyte text if current_buffer is unibyte. */
3481 if (it->multibyte_p)
3482 {
3483 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3484 struct face *face = FACE_FROM_ID (it->f, face_id);
3485 face_id = FACE_FOR_CHAR (it->f, face, c);
3486 }
3487 }
3488
3489 return face_id;
3490 }
3491
3492
3493 \f
3494 /***********************************************************************
3495 Invisible text
3496 ***********************************************************************/
3497
3498 /* Set up iterator IT from invisible properties at its current
3499 position. Called from handle_stop. */
3500
3501 static enum prop_handled
3502 handle_invisible_prop (it)
3503 struct it *it;
3504 {
3505 enum prop_handled handled = HANDLED_NORMALLY;
3506
3507 if (STRINGP (it->string))
3508 {
3509 extern Lisp_Object Qinvisible;
3510 Lisp_Object prop, end_charpos, limit, charpos;
3511
3512 /* Get the value of the invisible text property at the
3513 current position. Value will be nil if there is no such
3514 property. */
3515 charpos = make_number (IT_STRING_CHARPOS (*it));
3516 prop = Fget_text_property (charpos, Qinvisible, it->string);
3517
3518 if (!NILP (prop)
3519 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3520 {
3521 handled = HANDLED_RECOMPUTE_PROPS;
3522
3523 /* Get the position at which the next change of the
3524 invisible text property can be found in IT->string.
3525 Value will be nil if the property value is the same for
3526 all the rest of IT->string. */
3527 XSETINT (limit, SCHARS (it->string));
3528 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3529 it->string, limit);
3530
3531 /* Text at current position is invisible. The next
3532 change in the property is at position end_charpos.
3533 Move IT's current position to that position. */
3534 if (INTEGERP (end_charpos)
3535 && XFASTINT (end_charpos) < XFASTINT (limit))
3536 {
3537 struct text_pos old;
3538 old = it->current.string_pos;
3539 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3540 compute_string_pos (&it->current.string_pos, old, it->string);
3541 }
3542 else
3543 {
3544 /* The rest of the string is invisible. If this is an
3545 overlay string, proceed with the next overlay string
3546 or whatever comes and return a character from there. */
3547 if (it->current.overlay_string_index >= 0)
3548 {
3549 next_overlay_string (it);
3550 /* Don't check for overlay strings when we just
3551 finished processing them. */
3552 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3553 }
3554 else
3555 {
3556 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3557 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3558 }
3559 }
3560 }
3561 }
3562 else
3563 {
3564 int invis_p, newpos, next_stop, start_charpos;
3565 Lisp_Object pos, prop, overlay;
3566
3567 /* First of all, is there invisible text at this position? */
3568 start_charpos = IT_CHARPOS (*it);
3569 pos = make_number (IT_CHARPOS (*it));
3570 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3571 &overlay);
3572 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3573
3574 /* If we are on invisible text, skip over it. */
3575 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3576 {
3577 /* Record whether we have to display an ellipsis for the
3578 invisible text. */
3579 int display_ellipsis_p = invis_p == 2;
3580
3581 handled = HANDLED_RECOMPUTE_PROPS;
3582
3583 /* Loop skipping over invisible text. The loop is left at
3584 ZV or with IT on the first char being visible again. */
3585 do
3586 {
3587 /* Try to skip some invisible text. Return value is the
3588 position reached which can be equal to IT's position
3589 if there is nothing invisible here. This skips both
3590 over invisible text properties and overlays with
3591 invisible property. */
3592 newpos = skip_invisible (IT_CHARPOS (*it),
3593 &next_stop, ZV, it->window);
3594
3595 /* If we skipped nothing at all we weren't at invisible
3596 text in the first place. If everything to the end of
3597 the buffer was skipped, end the loop. */
3598 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3599 invis_p = 0;
3600 else
3601 {
3602 /* We skipped some characters but not necessarily
3603 all there are. Check if we ended up on visible
3604 text. Fget_char_property returns the property of
3605 the char before the given position, i.e. if we
3606 get invis_p = 0, this means that the char at
3607 newpos is visible. */
3608 pos = make_number (newpos);
3609 prop = Fget_char_property (pos, Qinvisible, it->window);
3610 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3611 }
3612
3613 /* If we ended up on invisible text, proceed to
3614 skip starting with next_stop. */
3615 if (invis_p)
3616 IT_CHARPOS (*it) = next_stop;
3617 }
3618 while (invis_p);
3619
3620 /* The position newpos is now either ZV or on visible text. */
3621 IT_CHARPOS (*it) = newpos;
3622 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3623
3624 /* If there are before-strings at the start of invisible
3625 text, and the text is invisible because of a text
3626 property, arrange to show before-strings because 20.x did
3627 it that way. (If the text is invisible because of an
3628 overlay property instead of a text property, this is
3629 already handled in the overlay code.) */
3630 if (NILP (overlay)
3631 && get_overlay_strings (it, start_charpos))
3632 {
3633 handled = HANDLED_RECOMPUTE_PROPS;
3634 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3635 }
3636 else if (display_ellipsis_p)
3637 setup_for_ellipsis (it, 0);
3638 }
3639 }
3640
3641 return handled;
3642 }
3643
3644
3645 /* Make iterator IT return `...' next.
3646 Replaces LEN characters from buffer. */
3647
3648 static void
3649 setup_for_ellipsis (it, len)
3650 struct it *it;
3651 int len;
3652 {
3653 /* Use the display table definition for `...'. Invalid glyphs
3654 will be handled by the method returning elements from dpvec. */
3655 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3656 {
3657 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3658 it->dpvec = v->contents;
3659 it->dpend = v->contents + v->size;
3660 }
3661 else
3662 {
3663 /* Default `...'. */
3664 it->dpvec = default_invis_vector;
3665 it->dpend = default_invis_vector + 3;
3666 }
3667
3668 it->dpvec_char_len = len;
3669 it->current.dpvec_index = 0;
3670 it->dpvec_face_id = -1;
3671
3672 /* Remember the current face id in case glyphs specify faces.
3673 IT's face is restored in set_iterator_to_next.
3674 saved_face_id was set to preceding char's face in handle_stop. */
3675 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3676 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3677
3678 it->method = GET_FROM_DISPLAY_VECTOR;
3679 it->ellipsis_p = 1;
3680 }
3681
3682
3683 \f
3684 /***********************************************************************
3685 'display' property
3686 ***********************************************************************/
3687
3688 /* Set up iterator IT from `display' property at its current position.
3689 Called from handle_stop.
3690 We return HANDLED_RETURN if some part of the display property
3691 overrides the display of the buffer text itself.
3692 Otherwise we return HANDLED_NORMALLY. */
3693
3694 static enum prop_handled
3695 handle_display_prop (it)
3696 struct it *it;
3697 {
3698 Lisp_Object prop, object;
3699 struct text_pos *position;
3700 /* Nonzero if some property replaces the display of the text itself. */
3701 int display_replaced_p = 0;
3702
3703 if (STRINGP (it->string))
3704 {
3705 object = it->string;
3706 position = &it->current.string_pos;
3707 }
3708 else
3709 {
3710 XSETWINDOW (object, it->w);
3711 position = &it->current.pos;
3712 }
3713
3714 /* Reset those iterator values set from display property values. */
3715 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3716 it->space_width = Qnil;
3717 it->font_height = Qnil;
3718 it->voffset = 0;
3719
3720 /* We don't support recursive `display' properties, i.e. string
3721 values that have a string `display' property, that have a string
3722 `display' property etc. */
3723 if (!it->string_from_display_prop_p)
3724 it->area = TEXT_AREA;
3725
3726 prop = Fget_char_property (make_number (position->charpos),
3727 Qdisplay, object);
3728 if (NILP (prop))
3729 return HANDLED_NORMALLY;
3730
3731 if (!STRINGP (it->string))
3732 object = it->w->buffer;
3733
3734 if (CONSP (prop)
3735 /* Simple properties. */
3736 && !EQ (XCAR (prop), Qimage)
3737 && !EQ (XCAR (prop), Qspace)
3738 && !EQ (XCAR (prop), Qwhen)
3739 && !EQ (XCAR (prop), Qslice)
3740 && !EQ (XCAR (prop), Qspace_width)
3741 && !EQ (XCAR (prop), Qheight)
3742 && !EQ (XCAR (prop), Qraise)
3743 /* Marginal area specifications. */
3744 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3745 && !EQ (XCAR (prop), Qleft_fringe)
3746 && !EQ (XCAR (prop), Qright_fringe)
3747 && !NILP (XCAR (prop)))
3748 {
3749 for (; CONSP (prop); prop = XCDR (prop))
3750 {
3751 if (handle_single_display_spec (it, XCAR (prop), object,
3752 position, display_replaced_p))
3753 display_replaced_p = 1;
3754 }
3755 }
3756 else if (VECTORP (prop))
3757 {
3758 int i;
3759 for (i = 0; i < ASIZE (prop); ++i)
3760 if (handle_single_display_spec (it, AREF (prop, i), object,
3761 position, display_replaced_p))
3762 display_replaced_p = 1;
3763 }
3764 else
3765 {
3766 int ret = handle_single_display_spec (it, prop, object, position, 0);
3767 if (ret < 0) /* Replaced by "", i.e. nothing. */
3768 return HANDLED_RECOMPUTE_PROPS;
3769 if (ret)
3770 display_replaced_p = 1;
3771 }
3772
3773 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3774 }
3775
3776
3777 /* Value is the position of the end of the `display' property starting
3778 at START_POS in OBJECT. */
3779
3780 static struct text_pos
3781 display_prop_end (it, object, start_pos)
3782 struct it *it;
3783 Lisp_Object object;
3784 struct text_pos start_pos;
3785 {
3786 Lisp_Object end;
3787 struct text_pos end_pos;
3788
3789 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3790 Qdisplay, object, Qnil);
3791 CHARPOS (end_pos) = XFASTINT (end);
3792 if (STRINGP (object))
3793 compute_string_pos (&end_pos, start_pos, it->string);
3794 else
3795 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3796
3797 return end_pos;
3798 }
3799
3800
3801 /* Set up IT from a single `display' specification PROP. OBJECT
3802 is the object in which the `display' property was found. *POSITION
3803 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3804 means that we previously saw a display specification which already
3805 replaced text display with something else, for example an image;
3806 we ignore such properties after the first one has been processed.
3807
3808 If PROP is a `space' or `image' specification, and in some other
3809 cases too, set *POSITION to the position where the `display'
3810 property ends.
3811
3812 Value is non-zero if something was found which replaces the display
3813 of buffer or string text. Specifically, the value is -1 if that
3814 "something" is "nothing". */
3815
3816 static int
3817 handle_single_display_spec (it, spec, object, position,
3818 display_replaced_before_p)
3819 struct it *it;
3820 Lisp_Object spec;
3821 Lisp_Object object;
3822 struct text_pos *position;
3823 int display_replaced_before_p;
3824 {
3825 Lisp_Object form;
3826 Lisp_Object location, value;
3827 struct text_pos start_pos;
3828 int valid_p;
3829
3830 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3831 If the result is non-nil, use VALUE instead of SPEC. */
3832 form = Qt;
3833 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3834 {
3835 spec = XCDR (spec);
3836 if (!CONSP (spec))
3837 return 0;
3838 form = XCAR (spec);
3839 spec = XCDR (spec);
3840 }
3841
3842 if (!NILP (form) && !EQ (form, Qt))
3843 {
3844 int count = SPECPDL_INDEX ();
3845 struct gcpro gcpro1;
3846
3847 /* Bind `object' to the object having the `display' property, a
3848 buffer or string. Bind `position' to the position in the
3849 object where the property was found, and `buffer-position'
3850 to the current position in the buffer. */
3851 specbind (Qobject, object);
3852 specbind (Qposition, make_number (CHARPOS (*position)));
3853 specbind (Qbuffer_position,
3854 make_number (STRINGP (object)
3855 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3856 GCPRO1 (form);
3857 form = safe_eval (form);
3858 UNGCPRO;
3859 unbind_to (count, Qnil);
3860 }
3861
3862 if (NILP (form))
3863 return 0;
3864
3865 /* Handle `(height HEIGHT)' specifications. */
3866 if (CONSP (spec)
3867 && EQ (XCAR (spec), Qheight)
3868 && CONSP (XCDR (spec)))
3869 {
3870 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3871 return 0;
3872
3873 it->font_height = XCAR (XCDR (spec));
3874 if (!NILP (it->font_height))
3875 {
3876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3877 int new_height = -1;
3878
3879 if (CONSP (it->font_height)
3880 && (EQ (XCAR (it->font_height), Qplus)
3881 || EQ (XCAR (it->font_height), Qminus))
3882 && CONSP (XCDR (it->font_height))
3883 && INTEGERP (XCAR (XCDR (it->font_height))))
3884 {
3885 /* `(+ N)' or `(- N)' where N is an integer. */
3886 int steps = XINT (XCAR (XCDR (it->font_height)));
3887 if (EQ (XCAR (it->font_height), Qplus))
3888 steps = - steps;
3889 it->face_id = smaller_face (it->f, it->face_id, steps);
3890 }
3891 else if (FUNCTIONP (it->font_height))
3892 {
3893 /* Call function with current height as argument.
3894 Value is the new height. */
3895 Lisp_Object height;
3896 height = safe_call1 (it->font_height,
3897 face->lface[LFACE_HEIGHT_INDEX]);
3898 if (NUMBERP (height))
3899 new_height = XFLOATINT (height);
3900 }
3901 else if (NUMBERP (it->font_height))
3902 {
3903 /* Value is a multiple of the canonical char height. */
3904 struct face *face;
3905
3906 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3907 new_height = (XFLOATINT (it->font_height)
3908 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3909 }
3910 else
3911 {
3912 /* Evaluate IT->font_height with `height' bound to the
3913 current specified height to get the new height. */
3914 int count = SPECPDL_INDEX ();
3915
3916 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3917 value = safe_eval (it->font_height);
3918 unbind_to (count, Qnil);
3919
3920 if (NUMBERP (value))
3921 new_height = XFLOATINT (value);
3922 }
3923
3924 if (new_height > 0)
3925 it->face_id = face_with_height (it->f, it->face_id, new_height);
3926 }
3927
3928 return 0;
3929 }
3930
3931 /* Handle `(space_width WIDTH)'. */
3932 if (CONSP (spec)
3933 && EQ (XCAR (spec), Qspace_width)
3934 && CONSP (XCDR (spec)))
3935 {
3936 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3937 return 0;
3938
3939 value = XCAR (XCDR (spec));
3940 if (NUMBERP (value) && XFLOATINT (value) > 0)
3941 it->space_width = value;
3942
3943 return 0;
3944 }
3945
3946 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3947 if (CONSP (spec)
3948 && EQ (XCAR (spec), Qslice))
3949 {
3950 Lisp_Object tem;
3951
3952 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3953 return 0;
3954
3955 if (tem = XCDR (spec), CONSP (tem))
3956 {
3957 it->slice.x = XCAR (tem);
3958 if (tem = XCDR (tem), CONSP (tem))
3959 {
3960 it->slice.y = XCAR (tem);
3961 if (tem = XCDR (tem), CONSP (tem))
3962 {
3963 it->slice.width = XCAR (tem);
3964 if (tem = XCDR (tem), CONSP (tem))
3965 it->slice.height = XCAR (tem);
3966 }
3967 }
3968 }
3969
3970 return 0;
3971 }
3972
3973 /* Handle `(raise FACTOR)'. */
3974 if (CONSP (spec)
3975 && EQ (XCAR (spec), Qraise)
3976 && CONSP (XCDR (spec)))
3977 {
3978 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3979 return 0;
3980
3981 #ifdef HAVE_WINDOW_SYSTEM
3982 value = XCAR (XCDR (spec));
3983 if (NUMBERP (value))
3984 {
3985 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3986 it->voffset = - (XFLOATINT (value)
3987 * (FONT_HEIGHT (face->font)));
3988 }
3989 #endif /* HAVE_WINDOW_SYSTEM */
3990
3991 return 0;
3992 }
3993
3994 /* Don't handle the other kinds of display specifications
3995 inside a string that we got from a `display' property. */
3996 if (it->string_from_display_prop_p)
3997 return 0;
3998
3999 /* Characters having this form of property are not displayed, so
4000 we have to find the end of the property. */
4001 start_pos = *position;
4002 *position = display_prop_end (it, object, start_pos);
4003 value = Qnil;
4004
4005 /* Stop the scan at that end position--we assume that all
4006 text properties change there. */
4007 it->stop_charpos = position->charpos;
4008
4009 /* Handle `(left-fringe BITMAP [FACE])'
4010 and `(right-fringe BITMAP [FACE])'. */
4011 if (CONSP (spec)
4012 && (EQ (XCAR (spec), Qleft_fringe)
4013 || EQ (XCAR (spec), Qright_fringe))
4014 && CONSP (XCDR (spec)))
4015 {
4016 int face_id = DEFAULT_FACE_ID;
4017 int fringe_bitmap;
4018
4019 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4020 /* If we return here, POSITION has been advanced
4021 across the text with this property. */
4022 return 0;
4023
4024 #ifdef HAVE_WINDOW_SYSTEM
4025 value = XCAR (XCDR (spec));
4026 if (!SYMBOLP (value)
4027 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4028 /* If we return here, POSITION has been advanced
4029 across the text with this property. */
4030 return 0;
4031
4032 if (CONSP (XCDR (XCDR (spec))))
4033 {
4034 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4035 int face_id2 = lookup_derived_face (it->f, face_name,
4036 'A', FRINGE_FACE_ID, 0);
4037 if (face_id2 >= 0)
4038 face_id = face_id2;
4039 }
4040
4041 /* Save current settings of IT so that we can restore them
4042 when we are finished with the glyph property value. */
4043
4044 push_it (it);
4045
4046 it->area = TEXT_AREA;
4047 it->what = IT_IMAGE;
4048 it->image_id = -1; /* no image */
4049 it->position = start_pos;
4050 it->object = NILP (object) ? it->w->buffer : object;
4051 it->method = GET_FROM_IMAGE;
4052 it->face_id = face_id;
4053
4054 /* Say that we haven't consumed the characters with
4055 `display' property yet. The call to pop_it in
4056 set_iterator_to_next will clean this up. */
4057 *position = start_pos;
4058
4059 if (EQ (XCAR (spec), Qleft_fringe))
4060 {
4061 it->left_user_fringe_bitmap = fringe_bitmap;
4062 it->left_user_fringe_face_id = face_id;
4063 }
4064 else
4065 {
4066 it->right_user_fringe_bitmap = fringe_bitmap;
4067 it->right_user_fringe_face_id = face_id;
4068 }
4069 #endif /* HAVE_WINDOW_SYSTEM */
4070 return 1;
4071 }
4072
4073 /* Prepare to handle `((margin left-margin) ...)',
4074 `((margin right-margin) ...)' and `((margin nil) ...)'
4075 prefixes for display specifications. */
4076 location = Qunbound;
4077 if (CONSP (spec) && CONSP (XCAR (spec)))
4078 {
4079 Lisp_Object tem;
4080
4081 value = XCDR (spec);
4082 if (CONSP (value))
4083 value = XCAR (value);
4084
4085 tem = XCAR (spec);
4086 if (EQ (XCAR (tem), Qmargin)
4087 && (tem = XCDR (tem),
4088 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4089 (NILP (tem)
4090 || EQ (tem, Qleft_margin)
4091 || EQ (tem, Qright_margin))))
4092 location = tem;
4093 }
4094
4095 if (EQ (location, Qunbound))
4096 {
4097 location = Qnil;
4098 value = spec;
4099 }
4100
4101 /* After this point, VALUE is the property after any
4102 margin prefix has been stripped. It must be a string,
4103 an image specification, or `(space ...)'.
4104
4105 LOCATION specifies where to display: `left-margin',
4106 `right-margin' or nil. */
4107
4108 valid_p = (STRINGP (value)
4109 #ifdef HAVE_WINDOW_SYSTEM
4110 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4111 #endif /* not HAVE_WINDOW_SYSTEM */
4112 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4113
4114 if (valid_p && !display_replaced_before_p)
4115 {
4116 /* Save current settings of IT so that we can restore them
4117 when we are finished with the glyph property value. */
4118 push_it (it);
4119
4120 if (NILP (location))
4121 it->area = TEXT_AREA;
4122 else if (EQ (location, Qleft_margin))
4123 it->area = LEFT_MARGIN_AREA;
4124 else
4125 it->area = RIGHT_MARGIN_AREA;
4126
4127 if (STRINGP (value))
4128 {
4129 if (SCHARS (value) == 0)
4130 {
4131 pop_it (it);
4132 return -1; /* Replaced by "", i.e. nothing. */
4133 }
4134 it->string = value;
4135 it->multibyte_p = STRING_MULTIBYTE (it->string);
4136 it->current.overlay_string_index = -1;
4137 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4138 it->end_charpos = it->string_nchars = SCHARS (it->string);
4139 it->method = GET_FROM_STRING;
4140 it->stop_charpos = 0;
4141 it->string_from_display_prop_p = 1;
4142 /* Say that we haven't consumed the characters with
4143 `display' property yet. The call to pop_it in
4144 set_iterator_to_next will clean this up. */
4145 *position = start_pos;
4146 }
4147 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4148 {
4149 it->method = GET_FROM_STRETCH;
4150 it->object = value;
4151 it->current.pos = it->position = start_pos;
4152 }
4153 #ifdef HAVE_WINDOW_SYSTEM
4154 else
4155 {
4156 it->what = IT_IMAGE;
4157 it->image_id = lookup_image (it->f, value);
4158 it->position = start_pos;
4159 it->object = NILP (object) ? it->w->buffer : object;
4160 it->method = GET_FROM_IMAGE;
4161
4162 /* Say that we haven't consumed the characters with
4163 `display' property yet. The call to pop_it in
4164 set_iterator_to_next will clean this up. */
4165 *position = start_pos;
4166 }
4167 #endif /* HAVE_WINDOW_SYSTEM */
4168
4169 return 1;
4170 }
4171
4172 /* Invalid property or property not supported. Restore
4173 POSITION to what it was before. */
4174 *position = start_pos;
4175 return 0;
4176 }
4177
4178
4179 /* Check if SPEC is a display specification value whose text should be
4180 treated as intangible. */
4181
4182 static int
4183 single_display_spec_intangible_p (prop)
4184 Lisp_Object prop;
4185 {
4186 /* Skip over `when FORM'. */
4187 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4188 {
4189 prop = XCDR (prop);
4190 if (!CONSP (prop))
4191 return 0;
4192 prop = XCDR (prop);
4193 }
4194
4195 if (STRINGP (prop))
4196 return 1;
4197
4198 if (!CONSP (prop))
4199 return 0;
4200
4201 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4202 we don't need to treat text as intangible. */
4203 if (EQ (XCAR (prop), Qmargin))
4204 {
4205 prop = XCDR (prop);
4206 if (!CONSP (prop))
4207 return 0;
4208
4209 prop = XCDR (prop);
4210 if (!CONSP (prop)
4211 || EQ (XCAR (prop), Qleft_margin)
4212 || EQ (XCAR (prop), Qright_margin))
4213 return 0;
4214 }
4215
4216 return (CONSP (prop)
4217 && (EQ (XCAR (prop), Qimage)
4218 || EQ (XCAR (prop), Qspace)));
4219 }
4220
4221
4222 /* Check if PROP is a display property value whose text should be
4223 treated as intangible. */
4224
4225 int
4226 display_prop_intangible_p (prop)
4227 Lisp_Object prop;
4228 {
4229 if (CONSP (prop)
4230 && CONSP (XCAR (prop))
4231 && !EQ (Qmargin, XCAR (XCAR (prop))))
4232 {
4233 /* A list of sub-properties. */
4234 while (CONSP (prop))
4235 {
4236 if (single_display_spec_intangible_p (XCAR (prop)))
4237 return 1;
4238 prop = XCDR (prop);
4239 }
4240 }
4241 else if (VECTORP (prop))
4242 {
4243 /* A vector of sub-properties. */
4244 int i;
4245 for (i = 0; i < ASIZE (prop); ++i)
4246 if (single_display_spec_intangible_p (AREF (prop, i)))
4247 return 1;
4248 }
4249 else
4250 return single_display_spec_intangible_p (prop);
4251
4252 return 0;
4253 }
4254
4255
4256 /* Return 1 if PROP is a display sub-property value containing STRING. */
4257
4258 static int
4259 single_display_spec_string_p (prop, string)
4260 Lisp_Object prop, string;
4261 {
4262 if (EQ (string, prop))
4263 return 1;
4264
4265 /* Skip over `when FORM'. */
4266 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4267 {
4268 prop = XCDR (prop);
4269 if (!CONSP (prop))
4270 return 0;
4271 prop = XCDR (prop);
4272 }
4273
4274 if (CONSP (prop))
4275 /* Skip over `margin LOCATION'. */
4276 if (EQ (XCAR (prop), Qmargin))
4277 {
4278 prop = XCDR (prop);
4279 if (!CONSP (prop))
4280 return 0;
4281
4282 prop = XCDR (prop);
4283 if (!CONSP (prop))
4284 return 0;
4285 }
4286
4287 return CONSP (prop) && EQ (XCAR (prop), string);
4288 }
4289
4290
4291 /* Return 1 if STRING appears in the `display' property PROP. */
4292
4293 static int
4294 display_prop_string_p (prop, string)
4295 Lisp_Object prop, string;
4296 {
4297 if (CONSP (prop)
4298 && CONSP (XCAR (prop))
4299 && !EQ (Qmargin, XCAR (XCAR (prop))))
4300 {
4301 /* A list of sub-properties. */
4302 while (CONSP (prop))
4303 {
4304 if (single_display_spec_string_p (XCAR (prop), string))
4305 return 1;
4306 prop = XCDR (prop);
4307 }
4308 }
4309 else if (VECTORP (prop))
4310 {
4311 /* A vector of sub-properties. */
4312 int i;
4313 for (i = 0; i < ASIZE (prop); ++i)
4314 if (single_display_spec_string_p (AREF (prop, i), string))
4315 return 1;
4316 }
4317 else
4318 return single_display_spec_string_p (prop, string);
4319
4320 return 0;
4321 }
4322
4323
4324 /* Determine from which buffer position in W's buffer STRING comes
4325 from. AROUND_CHARPOS is an approximate position where it could
4326 be from. Value is the buffer position or 0 if it couldn't be
4327 determined.
4328
4329 W's buffer must be current.
4330
4331 This function is necessary because we don't record buffer positions
4332 in glyphs generated from strings (to keep struct glyph small).
4333 This function may only use code that doesn't eval because it is
4334 called asynchronously from note_mouse_highlight. */
4335
4336 int
4337 string_buffer_position (w, string, around_charpos)
4338 struct window *w;
4339 Lisp_Object string;
4340 int around_charpos;
4341 {
4342 Lisp_Object limit, prop, pos;
4343 const int MAX_DISTANCE = 1000;
4344 int found = 0;
4345
4346 pos = make_number (around_charpos);
4347 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4348 while (!found && !EQ (pos, limit))
4349 {
4350 prop = Fget_char_property (pos, Qdisplay, Qnil);
4351 if (!NILP (prop) && display_prop_string_p (prop, string))
4352 found = 1;
4353 else
4354 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4355 }
4356
4357 if (!found)
4358 {
4359 pos = make_number (around_charpos);
4360 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4361 while (!found && !EQ (pos, limit))
4362 {
4363 prop = Fget_char_property (pos, Qdisplay, Qnil);
4364 if (!NILP (prop) && display_prop_string_p (prop, string))
4365 found = 1;
4366 else
4367 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4368 limit);
4369 }
4370 }
4371
4372 return found ? XINT (pos) : 0;
4373 }
4374
4375
4376 \f
4377 /***********************************************************************
4378 `composition' property
4379 ***********************************************************************/
4380
4381 /* Set up iterator IT from `composition' property at its current
4382 position. Called from handle_stop. */
4383
4384 static enum prop_handled
4385 handle_composition_prop (it)
4386 struct it *it;
4387 {
4388 Lisp_Object prop, string;
4389 int pos, pos_byte, end;
4390 enum prop_handled handled = HANDLED_NORMALLY;
4391
4392 if (STRINGP (it->string))
4393 {
4394 pos = IT_STRING_CHARPOS (*it);
4395 pos_byte = IT_STRING_BYTEPOS (*it);
4396 string = it->string;
4397 }
4398 else
4399 {
4400 pos = IT_CHARPOS (*it);
4401 pos_byte = IT_BYTEPOS (*it);
4402 string = Qnil;
4403 }
4404
4405 /* If there's a valid composition and point is not inside of the
4406 composition (in the case that the composition is from the current
4407 buffer), draw a glyph composed from the composition components. */
4408 if (find_composition (pos, -1, &pos, &end, &prop, string)
4409 && COMPOSITION_VALID_P (pos, end, prop)
4410 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4411 {
4412 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4413
4414 if (id >= 0)
4415 {
4416 it->method = GET_FROM_COMPOSITION;
4417 it->cmp_id = id;
4418 it->cmp_len = COMPOSITION_LENGTH (prop);
4419 /* For a terminal, draw only the first character of the
4420 components. */
4421 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4422 it->len = (STRINGP (it->string)
4423 ? string_char_to_byte (it->string, end)
4424 : CHAR_TO_BYTE (end)) - pos_byte;
4425 it->stop_charpos = end;
4426 handled = HANDLED_RETURN;
4427 }
4428 }
4429
4430 return handled;
4431 }
4432
4433
4434 \f
4435 /***********************************************************************
4436 Overlay strings
4437 ***********************************************************************/
4438
4439 /* The following structure is used to record overlay strings for
4440 later sorting in load_overlay_strings. */
4441
4442 struct overlay_entry
4443 {
4444 Lisp_Object overlay;
4445 Lisp_Object string;
4446 int priority;
4447 int after_string_p;
4448 };
4449
4450
4451 /* Set up iterator IT from overlay strings at its current position.
4452 Called from handle_stop. */
4453
4454 static enum prop_handled
4455 handle_overlay_change (it)
4456 struct it *it;
4457 {
4458 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4459 return HANDLED_RECOMPUTE_PROPS;
4460 else
4461 return HANDLED_NORMALLY;
4462 }
4463
4464
4465 /* Set up the next overlay string for delivery by IT, if there is an
4466 overlay string to deliver. Called by set_iterator_to_next when the
4467 end of the current overlay string is reached. If there are more
4468 overlay strings to display, IT->string and
4469 IT->current.overlay_string_index are set appropriately here.
4470 Otherwise IT->string is set to nil. */
4471
4472 static void
4473 next_overlay_string (it)
4474 struct it *it;
4475 {
4476 ++it->current.overlay_string_index;
4477 if (it->current.overlay_string_index == it->n_overlay_strings)
4478 {
4479 /* No more overlay strings. Restore IT's settings to what
4480 they were before overlay strings were processed, and
4481 continue to deliver from current_buffer. */
4482 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4483
4484 pop_it (it);
4485 xassert (it->stop_charpos >= BEGV
4486 && it->stop_charpos <= it->end_charpos);
4487 it->string = Qnil;
4488 it->current.overlay_string_index = -1;
4489 SET_TEXT_POS (it->current.string_pos, -1, -1);
4490 it->n_overlay_strings = 0;
4491 it->method = GET_FROM_BUFFER;
4492
4493 /* If we're at the end of the buffer, record that we have
4494 processed the overlay strings there already, so that
4495 next_element_from_buffer doesn't try it again. */
4496 if (IT_CHARPOS (*it) >= it->end_charpos)
4497 it->overlay_strings_at_end_processed_p = 1;
4498
4499 /* If we have to display `...' for invisible text, set
4500 the iterator up for that. */
4501 if (display_ellipsis_p)
4502 setup_for_ellipsis (it, 0);
4503 }
4504 else
4505 {
4506 /* There are more overlay strings to process. If
4507 IT->current.overlay_string_index has advanced to a position
4508 where we must load IT->overlay_strings with more strings, do
4509 it. */
4510 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4511
4512 if (it->current.overlay_string_index && i == 0)
4513 load_overlay_strings (it, 0);
4514
4515 /* Initialize IT to deliver display elements from the overlay
4516 string. */
4517 it->string = it->overlay_strings[i];
4518 it->multibyte_p = STRING_MULTIBYTE (it->string);
4519 SET_TEXT_POS (it->current.string_pos, 0, 0);
4520 it->method = GET_FROM_STRING;
4521 it->stop_charpos = 0;
4522 }
4523
4524 CHECK_IT (it);
4525 }
4526
4527
4528 /* Compare two overlay_entry structures E1 and E2. Used as a
4529 comparison function for qsort in load_overlay_strings. Overlay
4530 strings for the same position are sorted so that
4531
4532 1. All after-strings come in front of before-strings, except
4533 when they come from the same overlay.
4534
4535 2. Within after-strings, strings are sorted so that overlay strings
4536 from overlays with higher priorities come first.
4537
4538 2. Within before-strings, strings are sorted so that overlay
4539 strings from overlays with higher priorities come last.
4540
4541 Value is analogous to strcmp. */
4542
4543
4544 static int
4545 compare_overlay_entries (e1, e2)
4546 void *e1, *e2;
4547 {
4548 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4549 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4550 int result;
4551
4552 if (entry1->after_string_p != entry2->after_string_p)
4553 {
4554 /* Let after-strings appear in front of before-strings if
4555 they come from different overlays. */
4556 if (EQ (entry1->overlay, entry2->overlay))
4557 result = entry1->after_string_p ? 1 : -1;
4558 else
4559 result = entry1->after_string_p ? -1 : 1;
4560 }
4561 else if (entry1->after_string_p)
4562 /* After-strings sorted in order of decreasing priority. */
4563 result = entry2->priority - entry1->priority;
4564 else
4565 /* Before-strings sorted in order of increasing priority. */
4566 result = entry1->priority - entry2->priority;
4567
4568 return result;
4569 }
4570
4571
4572 /* Load the vector IT->overlay_strings with overlay strings from IT's
4573 current buffer position, or from CHARPOS if that is > 0. Set
4574 IT->n_overlays to the total number of overlay strings found.
4575
4576 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4577 a time. On entry into load_overlay_strings,
4578 IT->current.overlay_string_index gives the number of overlay
4579 strings that have already been loaded by previous calls to this
4580 function.
4581
4582 IT->add_overlay_start contains an additional overlay start
4583 position to consider for taking overlay strings from, if non-zero.
4584 This position comes into play when the overlay has an `invisible'
4585 property, and both before and after-strings. When we've skipped to
4586 the end of the overlay, because of its `invisible' property, we
4587 nevertheless want its before-string to appear.
4588 IT->add_overlay_start will contain the overlay start position
4589 in this case.
4590
4591 Overlay strings are sorted so that after-string strings come in
4592 front of before-string strings. Within before and after-strings,
4593 strings are sorted by overlay priority. See also function
4594 compare_overlay_entries. */
4595
4596 static void
4597 load_overlay_strings (it, charpos)
4598 struct it *it;
4599 int charpos;
4600 {
4601 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4602 Lisp_Object overlay, window, str, invisible;
4603 struct Lisp_Overlay *ov;
4604 int start, end;
4605 int size = 20;
4606 int n = 0, i, j, invis_p;
4607 struct overlay_entry *entries
4608 = (struct overlay_entry *) alloca (size * sizeof *entries);
4609
4610 if (charpos <= 0)
4611 charpos = IT_CHARPOS (*it);
4612
4613 /* Append the overlay string STRING of overlay OVERLAY to vector
4614 `entries' which has size `size' and currently contains `n'
4615 elements. AFTER_P non-zero means STRING is an after-string of
4616 OVERLAY. */
4617 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4618 do \
4619 { \
4620 Lisp_Object priority; \
4621 \
4622 if (n == size) \
4623 { \
4624 int new_size = 2 * size; \
4625 struct overlay_entry *old = entries; \
4626 entries = \
4627 (struct overlay_entry *) alloca (new_size \
4628 * sizeof *entries); \
4629 bcopy (old, entries, size * sizeof *entries); \
4630 size = new_size; \
4631 } \
4632 \
4633 entries[n].string = (STRING); \
4634 entries[n].overlay = (OVERLAY); \
4635 priority = Foverlay_get ((OVERLAY), Qpriority); \
4636 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4637 entries[n].after_string_p = (AFTER_P); \
4638 ++n; \
4639 } \
4640 while (0)
4641
4642 /* Process overlay before the overlay center. */
4643 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4644 {
4645 XSETMISC (overlay, ov);
4646 xassert (OVERLAYP (overlay));
4647 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4648 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4649
4650 if (end < charpos)
4651 break;
4652
4653 /* Skip this overlay if it doesn't start or end at IT's current
4654 position. */
4655 if (end != charpos && start != charpos)
4656 continue;
4657
4658 /* Skip this overlay if it doesn't apply to IT->w. */
4659 window = Foverlay_get (overlay, Qwindow);
4660 if (WINDOWP (window) && XWINDOW (window) != it->w)
4661 continue;
4662
4663 /* If the text ``under'' the overlay is invisible, both before-
4664 and after-strings from this overlay are visible; start and
4665 end position are indistinguishable. */
4666 invisible = Foverlay_get (overlay, Qinvisible);
4667 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4668
4669 /* If overlay has a non-empty before-string, record it. */
4670 if ((start == charpos || (end == charpos && invis_p))
4671 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4672 && SCHARS (str))
4673 RECORD_OVERLAY_STRING (overlay, str, 0);
4674
4675 /* If overlay has a non-empty after-string, record it. */
4676 if ((end == charpos || (start == charpos && invis_p))
4677 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4678 && SCHARS (str))
4679 RECORD_OVERLAY_STRING (overlay, str, 1);
4680 }
4681
4682 /* Process overlays after the overlay center. */
4683 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4684 {
4685 XSETMISC (overlay, ov);
4686 xassert (OVERLAYP (overlay));
4687 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4688 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4689
4690 if (start > charpos)
4691 break;
4692
4693 /* Skip this overlay if it doesn't start or end at IT's current
4694 position. */
4695 if (end != charpos && start != charpos)
4696 continue;
4697
4698 /* Skip this overlay if it doesn't apply to IT->w. */
4699 window = Foverlay_get (overlay, Qwindow);
4700 if (WINDOWP (window) && XWINDOW (window) != it->w)
4701 continue;
4702
4703 /* If the text ``under'' the overlay is invisible, it has a zero
4704 dimension, and both before- and after-strings apply. */
4705 invisible = Foverlay_get (overlay, Qinvisible);
4706 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4707
4708 /* If overlay has a non-empty before-string, record it. */
4709 if ((start == charpos || (end == charpos && invis_p))
4710 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4711 && SCHARS (str))
4712 RECORD_OVERLAY_STRING (overlay, str, 0);
4713
4714 /* If overlay has a non-empty after-string, record it. */
4715 if ((end == charpos || (start == charpos && invis_p))
4716 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4717 && SCHARS (str))
4718 RECORD_OVERLAY_STRING (overlay, str, 1);
4719 }
4720
4721 #undef RECORD_OVERLAY_STRING
4722
4723 /* Sort entries. */
4724 if (n > 1)
4725 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4726
4727 /* Record the total number of strings to process. */
4728 it->n_overlay_strings = n;
4729
4730 /* IT->current.overlay_string_index is the number of overlay strings
4731 that have already been consumed by IT. Copy some of the
4732 remaining overlay strings to IT->overlay_strings. */
4733 i = 0;
4734 j = it->current.overlay_string_index;
4735 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4736 it->overlay_strings[i++] = entries[j++].string;
4737
4738 CHECK_IT (it);
4739 }
4740
4741
4742 /* Get the first chunk of overlay strings at IT's current buffer
4743 position, or at CHARPOS if that is > 0. Value is non-zero if at
4744 least one overlay string was found. */
4745
4746 static int
4747 get_overlay_strings (it, charpos)
4748 struct it *it;
4749 int charpos;
4750 {
4751 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4752 process. This fills IT->overlay_strings with strings, and sets
4753 IT->n_overlay_strings to the total number of strings to process.
4754 IT->pos.overlay_string_index has to be set temporarily to zero
4755 because load_overlay_strings needs this; it must be set to -1
4756 when no overlay strings are found because a zero value would
4757 indicate a position in the first overlay string. */
4758 it->current.overlay_string_index = 0;
4759 load_overlay_strings (it, charpos);
4760
4761 /* If we found overlay strings, set up IT to deliver display
4762 elements from the first one. Otherwise set up IT to deliver
4763 from current_buffer. */
4764 if (it->n_overlay_strings)
4765 {
4766 /* Make sure we know settings in current_buffer, so that we can
4767 restore meaningful values when we're done with the overlay
4768 strings. */
4769 compute_stop_pos (it);
4770 xassert (it->face_id >= 0);
4771
4772 /* Save IT's settings. They are restored after all overlay
4773 strings have been processed. */
4774 xassert (it->sp == 0);
4775 push_it (it);
4776
4777 /* Set up IT to deliver display elements from the first overlay
4778 string. */
4779 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4780 it->string = it->overlay_strings[0];
4781 it->stop_charpos = 0;
4782 xassert (STRINGP (it->string));
4783 it->end_charpos = SCHARS (it->string);
4784 it->multibyte_p = STRING_MULTIBYTE (it->string);
4785 it->method = GET_FROM_STRING;
4786 }
4787 else
4788 {
4789 it->string = Qnil;
4790 it->current.overlay_string_index = -1;
4791 it->method = GET_FROM_BUFFER;
4792 }
4793
4794 CHECK_IT (it);
4795
4796 /* Value is non-zero if we found at least one overlay string. */
4797 return STRINGP (it->string);
4798 }
4799
4800
4801 \f
4802 /***********************************************************************
4803 Saving and restoring state
4804 ***********************************************************************/
4805
4806 /* Save current settings of IT on IT->stack. Called, for example,
4807 before setting up IT for an overlay string, to be able to restore
4808 IT's settings to what they were after the overlay string has been
4809 processed. */
4810
4811 static void
4812 push_it (it)
4813 struct it *it;
4814 {
4815 struct iterator_stack_entry *p;
4816
4817 xassert (it->sp < 2);
4818 p = it->stack + it->sp;
4819
4820 p->stop_charpos = it->stop_charpos;
4821 xassert (it->face_id >= 0);
4822 p->face_id = it->face_id;
4823 p->string = it->string;
4824 p->pos = it->current;
4825 p->end_charpos = it->end_charpos;
4826 p->string_nchars = it->string_nchars;
4827 p->area = it->area;
4828 p->multibyte_p = it->multibyte_p;
4829 p->slice = it->slice;
4830 p->space_width = it->space_width;
4831 p->font_height = it->font_height;
4832 p->voffset = it->voffset;
4833 p->string_from_display_prop_p = it->string_from_display_prop_p;
4834 p->display_ellipsis_p = 0;
4835 ++it->sp;
4836 }
4837
4838
4839 /* Restore IT's settings from IT->stack. Called, for example, when no
4840 more overlay strings must be processed, and we return to delivering
4841 display elements from a buffer, or when the end of a string from a
4842 `display' property is reached and we return to delivering display
4843 elements from an overlay string, or from a buffer. */
4844
4845 static void
4846 pop_it (it)
4847 struct it *it;
4848 {
4849 struct iterator_stack_entry *p;
4850
4851 xassert (it->sp > 0);
4852 --it->sp;
4853 p = it->stack + it->sp;
4854 it->stop_charpos = p->stop_charpos;
4855 it->face_id = p->face_id;
4856 it->string = p->string;
4857 it->current = p->pos;
4858 it->end_charpos = p->end_charpos;
4859 it->string_nchars = p->string_nchars;
4860 it->area = p->area;
4861 it->multibyte_p = p->multibyte_p;
4862 it->slice = p->slice;
4863 it->space_width = p->space_width;
4864 it->font_height = p->font_height;
4865 it->voffset = p->voffset;
4866 it->string_from_display_prop_p = p->string_from_display_prop_p;
4867 }
4868
4869
4870 \f
4871 /***********************************************************************
4872 Moving over lines
4873 ***********************************************************************/
4874
4875 /* Set IT's current position to the previous line start. */
4876
4877 static void
4878 back_to_previous_line_start (it)
4879 struct it *it;
4880 {
4881 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4882 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4883 }
4884
4885
4886 /* Move IT to the next line start.
4887
4888 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4889 we skipped over part of the text (as opposed to moving the iterator
4890 continuously over the text). Otherwise, don't change the value
4891 of *SKIPPED_P.
4892
4893 Newlines may come from buffer text, overlay strings, or strings
4894 displayed via the `display' property. That's the reason we can't
4895 simply use find_next_newline_no_quit.
4896
4897 Note that this function may not skip over invisible text that is so
4898 because of text properties and immediately follows a newline. If
4899 it would, function reseat_at_next_visible_line_start, when called
4900 from set_iterator_to_next, would effectively make invisible
4901 characters following a newline part of the wrong glyph row, which
4902 leads to wrong cursor motion. */
4903
4904 static int
4905 forward_to_next_line_start (it, skipped_p)
4906 struct it *it;
4907 int *skipped_p;
4908 {
4909 int old_selective, newline_found_p, n;
4910 const int MAX_NEWLINE_DISTANCE = 500;
4911
4912 /* If already on a newline, just consume it to avoid unintended
4913 skipping over invisible text below. */
4914 if (it->what == IT_CHARACTER
4915 && it->c == '\n'
4916 && CHARPOS (it->position) == IT_CHARPOS (*it))
4917 {
4918 set_iterator_to_next (it, 0);
4919 it->c = 0;
4920 return 1;
4921 }
4922
4923 /* Don't handle selective display in the following. It's (a)
4924 unnecessary because it's done by the caller, and (b) leads to an
4925 infinite recursion because next_element_from_ellipsis indirectly
4926 calls this function. */
4927 old_selective = it->selective;
4928 it->selective = 0;
4929
4930 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4931 from buffer text. */
4932 for (n = newline_found_p = 0;
4933 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4934 n += STRINGP (it->string) ? 0 : 1)
4935 {
4936 if (!get_next_display_element (it))
4937 return 0;
4938 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4939 set_iterator_to_next (it, 0);
4940 }
4941
4942 /* If we didn't find a newline near enough, see if we can use a
4943 short-cut. */
4944 if (!newline_found_p)
4945 {
4946 int start = IT_CHARPOS (*it);
4947 int limit = find_next_newline_no_quit (start, 1);
4948 Lisp_Object pos;
4949
4950 xassert (!STRINGP (it->string));
4951
4952 /* If there isn't any `display' property in sight, and no
4953 overlays, we can just use the position of the newline in
4954 buffer text. */
4955 if (it->stop_charpos >= limit
4956 || ((pos = Fnext_single_property_change (make_number (start),
4957 Qdisplay,
4958 Qnil, make_number (limit)),
4959 NILP (pos))
4960 && next_overlay_change (start) == ZV))
4961 {
4962 IT_CHARPOS (*it) = limit;
4963 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4964 *skipped_p = newline_found_p = 1;
4965 }
4966 else
4967 {
4968 while (get_next_display_element (it)
4969 && !newline_found_p)
4970 {
4971 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4972 set_iterator_to_next (it, 0);
4973 }
4974 }
4975 }
4976
4977 it->selective = old_selective;
4978 return newline_found_p;
4979 }
4980
4981
4982 /* Set IT's current position to the previous visible line start. Skip
4983 invisible text that is so either due to text properties or due to
4984 selective display. Caution: this does not change IT->current_x and
4985 IT->hpos. */
4986
4987 static void
4988 back_to_previous_visible_line_start (it)
4989 struct it *it;
4990 {
4991 while (IT_CHARPOS (*it) > BEGV)
4992 {
4993 back_to_previous_line_start (it);
4994 if (IT_CHARPOS (*it) <= BEGV)
4995 break;
4996
4997 /* If selective > 0, then lines indented more than that values
4998 are invisible. */
4999 if (it->selective > 0
5000 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5001 (double) it->selective)) /* iftc */
5002 continue;
5003
5004 /* Check the newline before point for invisibility. */
5005 {
5006 Lisp_Object prop;
5007 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5008 Qinvisible, it->window);
5009 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5010 continue;
5011 }
5012
5013 /* If newline has a display property that replaces the newline with something
5014 else (image or text), find start of overlay or interval and continue search
5015 from that point. */
5016 if (IT_CHARPOS (*it) > BEGV)
5017 {
5018 struct it it2 = *it;
5019 int pos;
5020 int beg, end;
5021 Lisp_Object val, overlay;
5022
5023 pos = --IT_CHARPOS (it2);
5024 --IT_BYTEPOS (it2);
5025 it2.sp = 0;
5026 if (handle_display_prop (&it2) == HANDLED_RETURN
5027 && !NILP (val = get_char_property_and_overlay
5028 (make_number (pos), Qdisplay, Qnil, &overlay))
5029 && (OVERLAYP (overlay)
5030 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5031 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5032 {
5033 if (beg < BEGV)
5034 beg = BEGV;
5035 IT_CHARPOS (*it) = beg;
5036 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5037 continue;
5038 }
5039 }
5040
5041 break;
5042 }
5043
5044 xassert (IT_CHARPOS (*it) >= BEGV);
5045 xassert (IT_CHARPOS (*it) == BEGV
5046 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5047 CHECK_IT (it);
5048 }
5049
5050
5051 /* Reseat iterator IT at the previous visible line start. Skip
5052 invisible text that is so either due to text properties or due to
5053 selective display. At the end, update IT's overlay information,
5054 face information etc. */
5055
5056 void
5057 reseat_at_previous_visible_line_start (it)
5058 struct it *it;
5059 {
5060 back_to_previous_visible_line_start (it);
5061 reseat (it, it->current.pos, 1);
5062 CHECK_IT (it);
5063 }
5064
5065
5066 /* Reseat iterator IT on the next visible line start in the current
5067 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5068 preceding the line start. Skip over invisible text that is so
5069 because of selective display. Compute faces, overlays etc at the
5070 new position. Note that this function does not skip over text that
5071 is invisible because of text properties. */
5072
5073 static void
5074 reseat_at_next_visible_line_start (it, on_newline_p)
5075 struct it *it;
5076 int on_newline_p;
5077 {
5078 int newline_found_p, skipped_p = 0;
5079
5080 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5081
5082 /* Skip over lines that are invisible because they are indented
5083 more than the value of IT->selective. */
5084 if (it->selective > 0)
5085 while (IT_CHARPOS (*it) < ZV
5086 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5087 (double) it->selective)) /* iftc */
5088 {
5089 xassert (IT_BYTEPOS (*it) == BEGV
5090 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5091 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5092 }
5093
5094 /* Position on the newline if that's what's requested. */
5095 if (on_newline_p && newline_found_p)
5096 {
5097 if (STRINGP (it->string))
5098 {
5099 if (IT_STRING_CHARPOS (*it) > 0)
5100 {
5101 --IT_STRING_CHARPOS (*it);
5102 --IT_STRING_BYTEPOS (*it);
5103 }
5104 }
5105 else if (IT_CHARPOS (*it) > BEGV)
5106 {
5107 --IT_CHARPOS (*it);
5108 --IT_BYTEPOS (*it);
5109 reseat (it, it->current.pos, 0);
5110 }
5111 }
5112 else if (skipped_p)
5113 reseat (it, it->current.pos, 0);
5114
5115 CHECK_IT (it);
5116 }
5117
5118
5119 \f
5120 /***********************************************************************
5121 Changing an iterator's position
5122 ***********************************************************************/
5123
5124 /* Change IT's current position to POS in current_buffer. If FORCE_P
5125 is non-zero, always check for text properties at the new position.
5126 Otherwise, text properties are only looked up if POS >=
5127 IT->check_charpos of a property. */
5128
5129 static void
5130 reseat (it, pos, force_p)
5131 struct it *it;
5132 struct text_pos pos;
5133 int force_p;
5134 {
5135 int original_pos = IT_CHARPOS (*it);
5136
5137 reseat_1 (it, pos, 0);
5138
5139 /* Determine where to check text properties. Avoid doing it
5140 where possible because text property lookup is very expensive. */
5141 if (force_p
5142 || CHARPOS (pos) > it->stop_charpos
5143 || CHARPOS (pos) < original_pos)
5144 handle_stop (it);
5145
5146 CHECK_IT (it);
5147 }
5148
5149
5150 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5151 IT->stop_pos to POS, also. */
5152
5153 static void
5154 reseat_1 (it, pos, set_stop_p)
5155 struct it *it;
5156 struct text_pos pos;
5157 int set_stop_p;
5158 {
5159 /* Don't call this function when scanning a C string. */
5160 xassert (it->s == NULL);
5161
5162 /* POS must be a reasonable value. */
5163 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5164
5165 it->current.pos = it->position = pos;
5166 XSETBUFFER (it->object, current_buffer);
5167 it->end_charpos = ZV;
5168 it->dpvec = NULL;
5169 it->current.dpvec_index = -1;
5170 it->current.overlay_string_index = -1;
5171 IT_STRING_CHARPOS (*it) = -1;
5172 IT_STRING_BYTEPOS (*it) = -1;
5173 it->string = Qnil;
5174 it->method = GET_FROM_BUFFER;
5175 /* RMS: I added this to fix a bug in move_it_vertically_backward
5176 where it->area continued to relate to the starting point
5177 for the backward motion. Bug report from
5178 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
5179 However, I am not sure whether reseat still does the right thing
5180 in general after this change. */
5181 it->area = TEXT_AREA;
5182 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5183 it->sp = 0;
5184 it->face_before_selective_p = 0;
5185
5186 if (set_stop_p)
5187 it->stop_charpos = CHARPOS (pos);
5188 }
5189
5190
5191 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5192 If S is non-null, it is a C string to iterate over. Otherwise,
5193 STRING gives a Lisp string to iterate over.
5194
5195 If PRECISION > 0, don't return more then PRECISION number of
5196 characters from the string.
5197
5198 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5199 characters have been returned. FIELD_WIDTH < 0 means an infinite
5200 field width.
5201
5202 MULTIBYTE = 0 means disable processing of multibyte characters,
5203 MULTIBYTE > 0 means enable it,
5204 MULTIBYTE < 0 means use IT->multibyte_p.
5205
5206 IT must be initialized via a prior call to init_iterator before
5207 calling this function. */
5208
5209 static void
5210 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5211 struct it *it;
5212 unsigned char *s;
5213 Lisp_Object string;
5214 int charpos;
5215 int precision, field_width, multibyte;
5216 {
5217 /* No region in strings. */
5218 it->region_beg_charpos = it->region_end_charpos = -1;
5219
5220 /* No text property checks performed by default, but see below. */
5221 it->stop_charpos = -1;
5222
5223 /* Set iterator position and end position. */
5224 bzero (&it->current, sizeof it->current);
5225 it->current.overlay_string_index = -1;
5226 it->current.dpvec_index = -1;
5227 xassert (charpos >= 0);
5228
5229 /* If STRING is specified, use its multibyteness, otherwise use the
5230 setting of MULTIBYTE, if specified. */
5231 if (multibyte >= 0)
5232 it->multibyte_p = multibyte > 0;
5233
5234 if (s == NULL)
5235 {
5236 xassert (STRINGP (string));
5237 it->string = string;
5238 it->s = NULL;
5239 it->end_charpos = it->string_nchars = SCHARS (string);
5240 it->method = GET_FROM_STRING;
5241 it->current.string_pos = string_pos (charpos, string);
5242 }
5243 else
5244 {
5245 it->s = s;
5246 it->string = Qnil;
5247
5248 /* Note that we use IT->current.pos, not it->current.string_pos,
5249 for displaying C strings. */
5250 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5251 if (it->multibyte_p)
5252 {
5253 it->current.pos = c_string_pos (charpos, s, 1);
5254 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5255 }
5256 else
5257 {
5258 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5259 it->end_charpos = it->string_nchars = strlen (s);
5260 }
5261
5262 it->method = GET_FROM_C_STRING;
5263 }
5264
5265 /* PRECISION > 0 means don't return more than PRECISION characters
5266 from the string. */
5267 if (precision > 0 && it->end_charpos - charpos > precision)
5268 it->end_charpos = it->string_nchars = charpos + precision;
5269
5270 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5271 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5272 FIELD_WIDTH < 0 means infinite field width. This is useful for
5273 padding with `-' at the end of a mode line. */
5274 if (field_width < 0)
5275 field_width = INFINITY;
5276 if (field_width > it->end_charpos - charpos)
5277 it->end_charpos = charpos + field_width;
5278
5279 /* Use the standard display table for displaying strings. */
5280 if (DISP_TABLE_P (Vstandard_display_table))
5281 it->dp = XCHAR_TABLE (Vstandard_display_table);
5282
5283 it->stop_charpos = charpos;
5284 CHECK_IT (it);
5285 }
5286
5287
5288 \f
5289 /***********************************************************************
5290 Iteration
5291 ***********************************************************************/
5292
5293 /* Map enum it_method value to corresponding next_element_from_* function. */
5294
5295 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5296 {
5297 next_element_from_buffer,
5298 next_element_from_display_vector,
5299 next_element_from_composition,
5300 next_element_from_string,
5301 next_element_from_c_string,
5302 next_element_from_image,
5303 next_element_from_stretch
5304 };
5305
5306
5307 /* Load IT's display element fields with information about the next
5308 display element from the current position of IT. Value is zero if
5309 end of buffer (or C string) is reached. */
5310
5311 int
5312 get_next_display_element (it)
5313 struct it *it;
5314 {
5315 /* Non-zero means that we found a display element. Zero means that
5316 we hit the end of what we iterate over. Performance note: the
5317 function pointer `method' used here turns out to be faster than
5318 using a sequence of if-statements. */
5319 int success_p;
5320
5321 get_next:
5322 success_p = (*get_next_element[it->method]) (it);
5323
5324 if (it->what == IT_CHARACTER)
5325 {
5326 /* Map via display table or translate control characters.
5327 IT->c, IT->len etc. have been set to the next character by
5328 the function call above. If we have a display table, and it
5329 contains an entry for IT->c, translate it. Don't do this if
5330 IT->c itself comes from a display table, otherwise we could
5331 end up in an infinite recursion. (An alternative could be to
5332 count the recursion depth of this function and signal an
5333 error when a certain maximum depth is reached.) Is it worth
5334 it? */
5335 if (success_p && it->dpvec == NULL)
5336 {
5337 Lisp_Object dv;
5338
5339 if (it->dp
5340 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5341 VECTORP (dv)))
5342 {
5343 struct Lisp_Vector *v = XVECTOR (dv);
5344
5345 /* Return the first character from the display table
5346 entry, if not empty. If empty, don't display the
5347 current character. */
5348 if (v->size)
5349 {
5350 it->dpvec_char_len = it->len;
5351 it->dpvec = v->contents;
5352 it->dpend = v->contents + v->size;
5353 it->current.dpvec_index = 0;
5354 it->dpvec_face_id = -1;
5355 it->saved_face_id = it->face_id;
5356 it->method = GET_FROM_DISPLAY_VECTOR;
5357 it->ellipsis_p = 0;
5358 }
5359 else
5360 {
5361 set_iterator_to_next (it, 0);
5362 }
5363 goto get_next;
5364 }
5365
5366 /* Translate control characters into `\003' or `^C' form.
5367 Control characters coming from a display table entry are
5368 currently not translated because we use IT->dpvec to hold
5369 the translation. This could easily be changed but I
5370 don't believe that it is worth doing.
5371
5372 If it->multibyte_p is nonzero, eight-bit characters and
5373 non-printable multibyte characters are also translated to
5374 octal form.
5375
5376 If it->multibyte_p is zero, eight-bit characters that
5377 don't have corresponding multibyte char code are also
5378 translated to octal form. */
5379 else if ((it->c < ' '
5380 && (it->area != TEXT_AREA
5381 /* In mode line, treat \n like other crl chars. */
5382 || (it->c != '\t'
5383 && it->glyph_row && it->glyph_row->mode_line_p)
5384 || (it->c != '\n' && it->c != '\t')))
5385 || (it->multibyte_p
5386 ? ((it->c >= 127
5387 && it->len == 1)
5388 || !CHAR_PRINTABLE_P (it->c)
5389 || (!NILP (Vnobreak_char_display)
5390 && (it->c == 0x8a0 || it->c == 0x8ad
5391 || it->c == 0x920 || it->c == 0x92d
5392 || it->c == 0xe20 || it->c == 0xe2d
5393 || it->c == 0xf20 || it->c == 0xf2d)))
5394 : (it->c >= 127
5395 && (!unibyte_display_via_language_environment
5396 || it->c == unibyte_char_to_multibyte (it->c)))))
5397 {
5398 /* IT->c is a control character which must be displayed
5399 either as '\003' or as `^C' where the '\\' and '^'
5400 can be defined in the display table. Fill
5401 IT->ctl_chars with glyphs for what we have to
5402 display. Then, set IT->dpvec to these glyphs. */
5403 GLYPH g;
5404 int ctl_len;
5405 int face_id, lface_id = 0 ;
5406 GLYPH escape_glyph;
5407
5408 /* Handle control characters with ^. */
5409
5410 if (it->c < 128 && it->ctl_arrow_p)
5411 {
5412 g = '^'; /* default glyph for Control */
5413 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5414 if (it->dp
5415 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5416 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5417 {
5418 g = XINT (DISP_CTRL_GLYPH (it->dp));
5419 lface_id = FAST_GLYPH_FACE (g);
5420 }
5421 if (lface_id)
5422 {
5423 g = FAST_GLYPH_CHAR (g);
5424 face_id = merge_faces (it->f, Qt, lface_id,
5425 it->face_id);
5426 }
5427 else
5428 {
5429 /* Merge the escape-glyph face into the current face. */
5430 face_id = merge_faces (it->f, Qescape_glyph, 0,
5431 it->face_id);
5432 }
5433
5434 XSETINT (it->ctl_chars[0], g);
5435 g = it->c ^ 0100;
5436 XSETINT (it->ctl_chars[1], g);
5437 ctl_len = 2;
5438 goto display_control;
5439 }
5440
5441 /* Handle non-break space in the mode where it only gets
5442 highlighting. */
5443
5444 if (EQ (Vnobreak_char_display, Qt)
5445 && (it->c == 0x8a0 || it->c == 0x920
5446 || it->c == 0xe20 || it->c == 0xf20))
5447 {
5448 /* Merge the no-break-space face into the current face. */
5449 face_id = merge_faces (it->f, Qnobreak_space, 0,
5450 it->face_id);
5451
5452 g = it->c = ' ';
5453 XSETINT (it->ctl_chars[0], g);
5454 ctl_len = 1;
5455 goto display_control;
5456 }
5457
5458 /* Handle sequences that start with the "escape glyph". */
5459
5460 /* the default escape glyph is \. */
5461 escape_glyph = '\\';
5462
5463 if (it->dp
5464 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5465 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5466 {
5467 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5468 lface_id = FAST_GLYPH_FACE (escape_glyph);
5469 }
5470 if (lface_id)
5471 {
5472 /* The display table specified a face.
5473 Merge it into face_id and also into escape_glyph. */
5474 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5475 face_id = merge_faces (it->f, Qt, lface_id,
5476 it->face_id);
5477 }
5478 else
5479 {
5480 /* Merge the escape-glyph face into the current face. */
5481 face_id = merge_faces (it->f, Qescape_glyph, 0,
5482 it->face_id);
5483 }
5484
5485 /* Handle soft hyphens in the mode where they only get
5486 highlighting. */
5487
5488 if (EQ (Vnobreak_char_display, Qt)
5489 && (it->c == 0x8ad || it->c == 0x92d
5490 || it->c == 0xe2d || it->c == 0xf2d))
5491 {
5492 g = it->c = '-';
5493 XSETINT (it->ctl_chars[0], g);
5494 ctl_len = 1;
5495 goto display_control;
5496 }
5497
5498 /* Handle non-break space and soft hyphen
5499 with the escape glyph. */
5500
5501 if (it->c == 0x8a0 || it->c == 0x8ad
5502 || it->c == 0x920 || it->c == 0x92d
5503 || it->c == 0xe20 || it->c == 0xe2d
5504 || it->c == 0xf20 || it->c == 0xf2d)
5505 {
5506 XSETINT (it->ctl_chars[0], escape_glyph);
5507 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5508 XSETINT (it->ctl_chars[1], g);
5509 ctl_len = 2;
5510 goto display_control;
5511 }
5512
5513 {
5514 unsigned char str[MAX_MULTIBYTE_LENGTH];
5515 int len;
5516 int i;
5517
5518 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5519 if (SINGLE_BYTE_CHAR_P (it->c))
5520 str[0] = it->c, len = 1;
5521 else
5522 {
5523 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5524 if (len < 0)
5525 {
5526 /* It's an invalid character, which shouldn't
5527 happen actually, but due to bugs it may
5528 happen. Let's print the char as is, there's
5529 not much meaningful we can do with it. */
5530 str[0] = it->c;
5531 str[1] = it->c >> 8;
5532 str[2] = it->c >> 16;
5533 str[3] = it->c >> 24;
5534 len = 4;
5535 }
5536 }
5537
5538 for (i = 0; i < len; i++)
5539 {
5540 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5541 /* Insert three more glyphs into IT->ctl_chars for
5542 the octal display of the character. */
5543 g = ((str[i] >> 6) & 7) + '0';
5544 XSETINT (it->ctl_chars[i * 4 + 1], g);
5545 g = ((str[i] >> 3) & 7) + '0';
5546 XSETINT (it->ctl_chars[i * 4 + 2], g);
5547 g = (str[i] & 7) + '0';
5548 XSETINT (it->ctl_chars[i * 4 + 3], g);
5549 }
5550 ctl_len = len * 4;
5551 }
5552
5553 display_control:
5554 /* Set up IT->dpvec and return first character from it. */
5555 it->dpvec_char_len = it->len;
5556 it->dpvec = it->ctl_chars;
5557 it->dpend = it->dpvec + ctl_len;
5558 it->current.dpvec_index = 0;
5559 it->dpvec_face_id = face_id;
5560 it->saved_face_id = it->face_id;
5561 it->method = GET_FROM_DISPLAY_VECTOR;
5562 it->ellipsis_p = 0;
5563 goto get_next;
5564 }
5565 }
5566
5567 /* Adjust face id for a multibyte character. There are no
5568 multibyte character in unibyte text. */
5569 if (it->multibyte_p
5570 && success_p
5571 && FRAME_WINDOW_P (it->f))
5572 {
5573 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5574 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5575 }
5576 }
5577
5578 /* Is this character the last one of a run of characters with
5579 box? If yes, set IT->end_of_box_run_p to 1. */
5580 if (it->face_box_p
5581 && it->s == NULL)
5582 {
5583 int face_id;
5584 struct face *face;
5585
5586 it->end_of_box_run_p
5587 = ((face_id = face_after_it_pos (it),
5588 face_id != it->face_id)
5589 && (face = FACE_FROM_ID (it->f, face_id),
5590 face->box == FACE_NO_BOX));
5591 }
5592
5593 /* Value is 0 if end of buffer or string reached. */
5594 return success_p;
5595 }
5596
5597
5598 /* Move IT to the next display element.
5599
5600 RESEAT_P non-zero means if called on a newline in buffer text,
5601 skip to the next visible line start.
5602
5603 Functions get_next_display_element and set_iterator_to_next are
5604 separate because I find this arrangement easier to handle than a
5605 get_next_display_element function that also increments IT's
5606 position. The way it is we can first look at an iterator's current
5607 display element, decide whether it fits on a line, and if it does,
5608 increment the iterator position. The other way around we probably
5609 would either need a flag indicating whether the iterator has to be
5610 incremented the next time, or we would have to implement a
5611 decrement position function which would not be easy to write. */
5612
5613 void
5614 set_iterator_to_next (it, reseat_p)
5615 struct it *it;
5616 int reseat_p;
5617 {
5618 /* Reset flags indicating start and end of a sequence of characters
5619 with box. Reset them at the start of this function because
5620 moving the iterator to a new position might set them. */
5621 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5622
5623 switch (it->method)
5624 {
5625 case GET_FROM_BUFFER:
5626 /* The current display element of IT is a character from
5627 current_buffer. Advance in the buffer, and maybe skip over
5628 invisible lines that are so because of selective display. */
5629 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5630 reseat_at_next_visible_line_start (it, 0);
5631 else
5632 {
5633 xassert (it->len != 0);
5634 IT_BYTEPOS (*it) += it->len;
5635 IT_CHARPOS (*it) += 1;
5636 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5637 }
5638 break;
5639
5640 case GET_FROM_COMPOSITION:
5641 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5642 if (STRINGP (it->string))
5643 {
5644 IT_STRING_BYTEPOS (*it) += it->len;
5645 IT_STRING_CHARPOS (*it) += it->cmp_len;
5646 it->method = GET_FROM_STRING;
5647 goto consider_string_end;
5648 }
5649 else
5650 {
5651 IT_BYTEPOS (*it) += it->len;
5652 IT_CHARPOS (*it) += it->cmp_len;
5653 it->method = GET_FROM_BUFFER;
5654 }
5655 break;
5656
5657 case GET_FROM_C_STRING:
5658 /* Current display element of IT is from a C string. */
5659 IT_BYTEPOS (*it) += it->len;
5660 IT_CHARPOS (*it) += 1;
5661 break;
5662
5663 case GET_FROM_DISPLAY_VECTOR:
5664 /* Current display element of IT is from a display table entry.
5665 Advance in the display table definition. Reset it to null if
5666 end reached, and continue with characters from buffers/
5667 strings. */
5668 ++it->current.dpvec_index;
5669
5670 /* Restore face of the iterator to what they were before the
5671 display vector entry (these entries may contain faces). */
5672 it->face_id = it->saved_face_id;
5673
5674 if (it->dpvec + it->current.dpvec_index == it->dpend)
5675 {
5676 if (it->s)
5677 it->method = GET_FROM_C_STRING;
5678 else if (STRINGP (it->string))
5679 it->method = GET_FROM_STRING;
5680 else
5681 it->method = GET_FROM_BUFFER;
5682
5683 it->dpvec = NULL;
5684 it->current.dpvec_index = -1;
5685
5686 /* Skip over characters which were displayed via IT->dpvec. */
5687 if (it->dpvec_char_len < 0)
5688 reseat_at_next_visible_line_start (it, 1);
5689 else if (it->dpvec_char_len > 0)
5690 {
5691 if (it->method == GET_FROM_STRING
5692 && it->n_overlay_strings > 0)
5693 it->ignore_overlay_strings_at_pos_p = 1;
5694 it->len = it->dpvec_char_len;
5695 set_iterator_to_next (it, reseat_p);
5696 }
5697
5698 /* Recheck faces after display vector */
5699 it->stop_charpos = IT_CHARPOS (*it);
5700 }
5701 break;
5702
5703 case GET_FROM_STRING:
5704 /* Current display element is a character from a Lisp string. */
5705 xassert (it->s == NULL && STRINGP (it->string));
5706 IT_STRING_BYTEPOS (*it) += it->len;
5707 IT_STRING_CHARPOS (*it) += 1;
5708
5709 consider_string_end:
5710
5711 if (it->current.overlay_string_index >= 0)
5712 {
5713 /* IT->string is an overlay string. Advance to the
5714 next, if there is one. */
5715 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5716 next_overlay_string (it);
5717 }
5718 else
5719 {
5720 /* IT->string is not an overlay string. If we reached
5721 its end, and there is something on IT->stack, proceed
5722 with what is on the stack. This can be either another
5723 string, this time an overlay string, or a buffer. */
5724 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5725 && it->sp > 0)
5726 {
5727 pop_it (it);
5728 if (STRINGP (it->string))
5729 goto consider_string_end;
5730 it->method = GET_FROM_BUFFER;
5731 }
5732 }
5733 break;
5734
5735 case GET_FROM_IMAGE:
5736 case GET_FROM_STRETCH:
5737 /* The position etc with which we have to proceed are on
5738 the stack. The position may be at the end of a string,
5739 if the `display' property takes up the whole string. */
5740 xassert (it->sp > 0);
5741 pop_it (it);
5742 it->image_id = 0;
5743 if (STRINGP (it->string))
5744 {
5745 it->method = GET_FROM_STRING;
5746 goto consider_string_end;
5747 }
5748 it->method = GET_FROM_BUFFER;
5749 break;
5750
5751 default:
5752 /* There are no other methods defined, so this should be a bug. */
5753 abort ();
5754 }
5755
5756 xassert (it->method != GET_FROM_STRING
5757 || (STRINGP (it->string)
5758 && IT_STRING_CHARPOS (*it) >= 0));
5759 }
5760
5761 /* Load IT's display element fields with information about the next
5762 display element which comes from a display table entry or from the
5763 result of translating a control character to one of the forms `^C'
5764 or `\003'.
5765
5766 IT->dpvec holds the glyphs to return as characters.
5767 IT->saved_face_id holds the face id before the display vector--
5768 it is restored into IT->face_idin set_iterator_to_next. */
5769
5770 static int
5771 next_element_from_display_vector (it)
5772 struct it *it;
5773 {
5774 /* Precondition. */
5775 xassert (it->dpvec && it->current.dpvec_index >= 0);
5776
5777 it->face_id = it->saved_face_id;
5778
5779 if (INTEGERP (*it->dpvec)
5780 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5781 {
5782 GLYPH g;
5783
5784 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5785 it->c = FAST_GLYPH_CHAR (g);
5786 it->len = CHAR_BYTES (it->c);
5787
5788 /* The entry may contain a face id to use. Such a face id is
5789 the id of a Lisp face, not a realized face. A face id of
5790 zero means no face is specified. */
5791 if (it->dpvec_face_id >= 0)
5792 it->face_id = it->dpvec_face_id;
5793 else
5794 {
5795 int lface_id = FAST_GLYPH_FACE (g);
5796 if (lface_id > 0)
5797 it->face_id = merge_faces (it->f, Qt, lface_id,
5798 it->saved_face_id);
5799 }
5800 }
5801 else
5802 /* Display table entry is invalid. Return a space. */
5803 it->c = ' ', it->len = 1;
5804
5805 /* Don't change position and object of the iterator here. They are
5806 still the values of the character that had this display table
5807 entry or was translated, and that's what we want. */
5808 it->what = IT_CHARACTER;
5809 return 1;
5810 }
5811
5812
5813 /* Load IT with the next display element from Lisp string IT->string.
5814 IT->current.string_pos is the current position within the string.
5815 If IT->current.overlay_string_index >= 0, the Lisp string is an
5816 overlay string. */
5817
5818 static int
5819 next_element_from_string (it)
5820 struct it *it;
5821 {
5822 struct text_pos position;
5823
5824 xassert (STRINGP (it->string));
5825 xassert (IT_STRING_CHARPOS (*it) >= 0);
5826 position = it->current.string_pos;
5827
5828 /* Time to check for invisible text? */
5829 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5830 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5831 {
5832 handle_stop (it);
5833
5834 /* Since a handler may have changed IT->method, we must
5835 recurse here. */
5836 return get_next_display_element (it);
5837 }
5838
5839 if (it->current.overlay_string_index >= 0)
5840 {
5841 /* Get the next character from an overlay string. In overlay
5842 strings, There is no field width or padding with spaces to
5843 do. */
5844 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5845 {
5846 it->what = IT_EOB;
5847 return 0;
5848 }
5849 else if (STRING_MULTIBYTE (it->string))
5850 {
5851 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5852 const unsigned char *s = (SDATA (it->string)
5853 + IT_STRING_BYTEPOS (*it));
5854 it->c = string_char_and_length (s, remaining, &it->len);
5855 }
5856 else
5857 {
5858 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5859 it->len = 1;
5860 }
5861 }
5862 else
5863 {
5864 /* Get the next character from a Lisp string that is not an
5865 overlay string. Such strings come from the mode line, for
5866 example. We may have to pad with spaces, or truncate the
5867 string. See also next_element_from_c_string. */
5868 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5869 {
5870 it->what = IT_EOB;
5871 return 0;
5872 }
5873 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5874 {
5875 /* Pad with spaces. */
5876 it->c = ' ', it->len = 1;
5877 CHARPOS (position) = BYTEPOS (position) = -1;
5878 }
5879 else if (STRING_MULTIBYTE (it->string))
5880 {
5881 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5882 const unsigned char *s = (SDATA (it->string)
5883 + IT_STRING_BYTEPOS (*it));
5884 it->c = string_char_and_length (s, maxlen, &it->len);
5885 }
5886 else
5887 {
5888 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5889 it->len = 1;
5890 }
5891 }
5892
5893 /* Record what we have and where it came from. Note that we store a
5894 buffer position in IT->position although it could arguably be a
5895 string position. */
5896 it->what = IT_CHARACTER;
5897 it->object = it->string;
5898 it->position = position;
5899 return 1;
5900 }
5901
5902
5903 /* Load IT with next display element from C string IT->s.
5904 IT->string_nchars is the maximum number of characters to return
5905 from the string. IT->end_charpos may be greater than
5906 IT->string_nchars when this function is called, in which case we
5907 may have to return padding spaces. Value is zero if end of string
5908 reached, including padding spaces. */
5909
5910 static int
5911 next_element_from_c_string (it)
5912 struct it *it;
5913 {
5914 int success_p = 1;
5915
5916 xassert (it->s);
5917 it->what = IT_CHARACTER;
5918 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5919 it->object = Qnil;
5920
5921 /* IT's position can be greater IT->string_nchars in case a field
5922 width or precision has been specified when the iterator was
5923 initialized. */
5924 if (IT_CHARPOS (*it) >= it->end_charpos)
5925 {
5926 /* End of the game. */
5927 it->what = IT_EOB;
5928 success_p = 0;
5929 }
5930 else if (IT_CHARPOS (*it) >= it->string_nchars)
5931 {
5932 /* Pad with spaces. */
5933 it->c = ' ', it->len = 1;
5934 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5935 }
5936 else if (it->multibyte_p)
5937 {
5938 /* Implementation note: The calls to strlen apparently aren't a
5939 performance problem because there is no noticeable performance
5940 difference between Emacs running in unibyte or multibyte mode. */
5941 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5942 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5943 maxlen, &it->len);
5944 }
5945 else
5946 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5947
5948 return success_p;
5949 }
5950
5951
5952 /* Set up IT to return characters from an ellipsis, if appropriate.
5953 The definition of the ellipsis glyphs may come from a display table
5954 entry. This function Fills IT with the first glyph from the
5955 ellipsis if an ellipsis is to be displayed. */
5956
5957 static int
5958 next_element_from_ellipsis (it)
5959 struct it *it;
5960 {
5961 if (it->selective_display_ellipsis_p)
5962 setup_for_ellipsis (it, it->len);
5963 else
5964 {
5965 /* The face at the current position may be different from the
5966 face we find after the invisible text. Remember what it
5967 was in IT->saved_face_id, and signal that it's there by
5968 setting face_before_selective_p. */
5969 it->saved_face_id = it->face_id;
5970 it->method = GET_FROM_BUFFER;
5971 reseat_at_next_visible_line_start (it, 1);
5972 it->face_before_selective_p = 1;
5973 }
5974
5975 return get_next_display_element (it);
5976 }
5977
5978
5979 /* Deliver an image display element. The iterator IT is already
5980 filled with image information (done in handle_display_prop). Value
5981 is always 1. */
5982
5983
5984 static int
5985 next_element_from_image (it)
5986 struct it *it;
5987 {
5988 it->what = IT_IMAGE;
5989 return 1;
5990 }
5991
5992
5993 /* Fill iterator IT with next display element from a stretch glyph
5994 property. IT->object is the value of the text property. Value is
5995 always 1. */
5996
5997 static int
5998 next_element_from_stretch (it)
5999 struct it *it;
6000 {
6001 it->what = IT_STRETCH;
6002 return 1;
6003 }
6004
6005
6006 /* Load IT with the next display element from current_buffer. Value
6007 is zero if end of buffer reached. IT->stop_charpos is the next
6008 position at which to stop and check for text properties or buffer
6009 end. */
6010
6011 static int
6012 next_element_from_buffer (it)
6013 struct it *it;
6014 {
6015 int success_p = 1;
6016
6017 /* Check this assumption, otherwise, we would never enter the
6018 if-statement, below. */
6019 xassert (IT_CHARPOS (*it) >= BEGV
6020 && IT_CHARPOS (*it) <= it->stop_charpos);
6021
6022 if (IT_CHARPOS (*it) >= it->stop_charpos)
6023 {
6024 if (IT_CHARPOS (*it) >= it->end_charpos)
6025 {
6026 int overlay_strings_follow_p;
6027
6028 /* End of the game, except when overlay strings follow that
6029 haven't been returned yet. */
6030 if (it->overlay_strings_at_end_processed_p)
6031 overlay_strings_follow_p = 0;
6032 else
6033 {
6034 it->overlay_strings_at_end_processed_p = 1;
6035 overlay_strings_follow_p = get_overlay_strings (it, 0);
6036 }
6037
6038 if (overlay_strings_follow_p)
6039 success_p = get_next_display_element (it);
6040 else
6041 {
6042 it->what = IT_EOB;
6043 it->position = it->current.pos;
6044 success_p = 0;
6045 }
6046 }
6047 else
6048 {
6049 handle_stop (it);
6050 return get_next_display_element (it);
6051 }
6052 }
6053 else
6054 {
6055 /* No face changes, overlays etc. in sight, so just return a
6056 character from current_buffer. */
6057 unsigned char *p;
6058
6059 /* Maybe run the redisplay end trigger hook. Performance note:
6060 This doesn't seem to cost measurable time. */
6061 if (it->redisplay_end_trigger_charpos
6062 && it->glyph_row
6063 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6064 run_redisplay_end_trigger_hook (it);
6065
6066 /* Get the next character, maybe multibyte. */
6067 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6068 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6069 {
6070 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6071 - IT_BYTEPOS (*it));
6072 it->c = string_char_and_length (p, maxlen, &it->len);
6073 }
6074 else
6075 it->c = *p, it->len = 1;
6076
6077 /* Record what we have and where it came from. */
6078 it->what = IT_CHARACTER;;
6079 it->object = it->w->buffer;
6080 it->position = it->current.pos;
6081
6082 /* Normally we return the character found above, except when we
6083 really want to return an ellipsis for selective display. */
6084 if (it->selective)
6085 {
6086 if (it->c == '\n')
6087 {
6088 /* A value of selective > 0 means hide lines indented more
6089 than that number of columns. */
6090 if (it->selective > 0
6091 && IT_CHARPOS (*it) + 1 < ZV
6092 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6093 IT_BYTEPOS (*it) + 1,
6094 (double) it->selective)) /* iftc */
6095 {
6096 success_p = next_element_from_ellipsis (it);
6097 it->dpvec_char_len = -1;
6098 }
6099 }
6100 else if (it->c == '\r' && it->selective == -1)
6101 {
6102 /* A value of selective == -1 means that everything from the
6103 CR to the end of the line is invisible, with maybe an
6104 ellipsis displayed for it. */
6105 success_p = next_element_from_ellipsis (it);
6106 it->dpvec_char_len = -1;
6107 }
6108 }
6109 }
6110
6111 /* Value is zero if end of buffer reached. */
6112 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6113 return success_p;
6114 }
6115
6116
6117 /* Run the redisplay end trigger hook for IT. */
6118
6119 static void
6120 run_redisplay_end_trigger_hook (it)
6121 struct it *it;
6122 {
6123 Lisp_Object args[3];
6124
6125 /* IT->glyph_row should be non-null, i.e. we should be actually
6126 displaying something, or otherwise we should not run the hook. */
6127 xassert (it->glyph_row);
6128
6129 /* Set up hook arguments. */
6130 args[0] = Qredisplay_end_trigger_functions;
6131 args[1] = it->window;
6132 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6133 it->redisplay_end_trigger_charpos = 0;
6134
6135 /* Since we are *trying* to run these functions, don't try to run
6136 them again, even if they get an error. */
6137 it->w->redisplay_end_trigger = Qnil;
6138 Frun_hook_with_args (3, args);
6139
6140 /* Notice if it changed the face of the character we are on. */
6141 handle_face_prop (it);
6142 }
6143
6144
6145 /* Deliver a composition display element. The iterator IT is already
6146 filled with composition information (done in
6147 handle_composition_prop). Value is always 1. */
6148
6149 static int
6150 next_element_from_composition (it)
6151 struct it *it;
6152 {
6153 it->what = IT_COMPOSITION;
6154 it->position = (STRINGP (it->string)
6155 ? it->current.string_pos
6156 : it->current.pos);
6157 return 1;
6158 }
6159
6160
6161 \f
6162 /***********************************************************************
6163 Moving an iterator without producing glyphs
6164 ***********************************************************************/
6165
6166 /* Check if iterator is at a position corresponding to a valid buffer
6167 position after some move_it_ call. */
6168
6169 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6170 ((it)->method == GET_FROM_STRING \
6171 ? IT_STRING_CHARPOS (*it) == 0 \
6172 : 1)
6173
6174
6175 /* Move iterator IT to a specified buffer or X position within one
6176 line on the display without producing glyphs.
6177
6178 OP should be a bit mask including some or all of these bits:
6179 MOVE_TO_X: Stop on reaching x-position TO_X.
6180 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6181 Regardless of OP's value, stop in reaching the end of the display line.
6182
6183 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6184 This means, in particular, that TO_X includes window's horizontal
6185 scroll amount.
6186
6187 The return value has several possible values that
6188 say what condition caused the scan to stop:
6189
6190 MOVE_POS_MATCH_OR_ZV
6191 - when TO_POS or ZV was reached.
6192
6193 MOVE_X_REACHED
6194 -when TO_X was reached before TO_POS or ZV were reached.
6195
6196 MOVE_LINE_CONTINUED
6197 - when we reached the end of the display area and the line must
6198 be continued.
6199
6200 MOVE_LINE_TRUNCATED
6201 - when we reached the end of the display area and the line is
6202 truncated.
6203
6204 MOVE_NEWLINE_OR_CR
6205 - when we stopped at a line end, i.e. a newline or a CR and selective
6206 display is on. */
6207
6208 static enum move_it_result
6209 move_it_in_display_line_to (it, to_charpos, to_x, op)
6210 struct it *it;
6211 int to_charpos, to_x, op;
6212 {
6213 enum move_it_result result = MOVE_UNDEFINED;
6214 struct glyph_row *saved_glyph_row;
6215
6216 /* Don't produce glyphs in produce_glyphs. */
6217 saved_glyph_row = it->glyph_row;
6218 it->glyph_row = NULL;
6219
6220 #define BUFFER_POS_REACHED_P() \
6221 ((op & MOVE_TO_POS) != 0 \
6222 && BUFFERP (it->object) \
6223 && IT_CHARPOS (*it) >= to_charpos \
6224 && (it->method == GET_FROM_BUFFER \
6225 || (it->method == GET_FROM_DISPLAY_VECTOR \
6226 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6227
6228
6229 while (1)
6230 {
6231 int x, i, ascent = 0, descent = 0;
6232
6233 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6234 if ((op & MOVE_TO_POS) != 0
6235 && BUFFERP (it->object)
6236 && it->method == GET_FROM_BUFFER
6237 && IT_CHARPOS (*it) > to_charpos)
6238 {
6239 result = MOVE_POS_MATCH_OR_ZV;
6240 break;
6241 }
6242
6243 /* Stop when ZV reached.
6244 We used to stop here when TO_CHARPOS reached as well, but that is
6245 too soon if this glyph does not fit on this line. So we handle it
6246 explicitly below. */
6247 if (!get_next_display_element (it)
6248 || (it->truncate_lines_p
6249 && BUFFER_POS_REACHED_P ()))
6250 {
6251 result = MOVE_POS_MATCH_OR_ZV;
6252 break;
6253 }
6254
6255 /* The call to produce_glyphs will get the metrics of the
6256 display element IT is loaded with. We record in x the
6257 x-position before this display element in case it does not
6258 fit on the line. */
6259 x = it->current_x;
6260
6261 /* Remember the line height so far in case the next element doesn't
6262 fit on the line. */
6263 if (!it->truncate_lines_p)
6264 {
6265 ascent = it->max_ascent;
6266 descent = it->max_descent;
6267 }
6268
6269 PRODUCE_GLYPHS (it);
6270
6271 if (it->area != TEXT_AREA)
6272 {
6273 set_iterator_to_next (it, 1);
6274 continue;
6275 }
6276
6277 /* The number of glyphs we get back in IT->nglyphs will normally
6278 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6279 character on a terminal frame, or (iii) a line end. For the
6280 second case, IT->nglyphs - 1 padding glyphs will be present
6281 (on X frames, there is only one glyph produced for a
6282 composite character.
6283
6284 The behavior implemented below means, for continuation lines,
6285 that as many spaces of a TAB as fit on the current line are
6286 displayed there. For terminal frames, as many glyphs of a
6287 multi-glyph character are displayed in the current line, too.
6288 This is what the old redisplay code did, and we keep it that
6289 way. Under X, the whole shape of a complex character must
6290 fit on the line or it will be completely displayed in the
6291 next line.
6292
6293 Note that both for tabs and padding glyphs, all glyphs have
6294 the same width. */
6295 if (it->nglyphs)
6296 {
6297 /* More than one glyph or glyph doesn't fit on line. All
6298 glyphs have the same width. */
6299 int single_glyph_width = it->pixel_width / it->nglyphs;
6300 int new_x;
6301 int x_before_this_char = x;
6302 int hpos_before_this_char = it->hpos;
6303
6304 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6305 {
6306 new_x = x + single_glyph_width;
6307
6308 /* We want to leave anything reaching TO_X to the caller. */
6309 if ((op & MOVE_TO_X) && new_x > to_x)
6310 {
6311 if (BUFFER_POS_REACHED_P ())
6312 goto buffer_pos_reached;
6313 it->current_x = x;
6314 result = MOVE_X_REACHED;
6315 break;
6316 }
6317 else if (/* Lines are continued. */
6318 !it->truncate_lines_p
6319 && (/* And glyph doesn't fit on the line. */
6320 new_x > it->last_visible_x
6321 /* Or it fits exactly and we're on a window
6322 system frame. */
6323 || (new_x == it->last_visible_x
6324 && FRAME_WINDOW_P (it->f))))
6325 {
6326 if (/* IT->hpos == 0 means the very first glyph
6327 doesn't fit on the line, e.g. a wide image. */
6328 it->hpos == 0
6329 || (new_x == it->last_visible_x
6330 && FRAME_WINDOW_P (it->f)))
6331 {
6332 ++it->hpos;
6333 it->current_x = new_x;
6334
6335 /* The character's last glyph just barely fits
6336 in this row. */
6337 if (i == it->nglyphs - 1)
6338 {
6339 /* If this is the destination position,
6340 return a position *before* it in this row,
6341 now that we know it fits in this row. */
6342 if (BUFFER_POS_REACHED_P ())
6343 {
6344 it->hpos = hpos_before_this_char;
6345 it->current_x = x_before_this_char;
6346 result = MOVE_POS_MATCH_OR_ZV;
6347 break;
6348 }
6349
6350 set_iterator_to_next (it, 1);
6351 #ifdef HAVE_WINDOW_SYSTEM
6352 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6353 {
6354 if (!get_next_display_element (it))
6355 {
6356 result = MOVE_POS_MATCH_OR_ZV;
6357 break;
6358 }
6359 if (BUFFER_POS_REACHED_P ())
6360 {
6361 if (ITERATOR_AT_END_OF_LINE_P (it))
6362 result = MOVE_POS_MATCH_OR_ZV;
6363 else
6364 result = MOVE_LINE_CONTINUED;
6365 break;
6366 }
6367 if (ITERATOR_AT_END_OF_LINE_P (it))
6368 {
6369 result = MOVE_NEWLINE_OR_CR;
6370 break;
6371 }
6372 }
6373 #endif /* HAVE_WINDOW_SYSTEM */
6374 }
6375 }
6376 else
6377 {
6378 it->current_x = x;
6379 it->max_ascent = ascent;
6380 it->max_descent = descent;
6381 }
6382
6383 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6384 IT_CHARPOS (*it)));
6385 result = MOVE_LINE_CONTINUED;
6386 break;
6387 }
6388 else if (BUFFER_POS_REACHED_P ())
6389 goto buffer_pos_reached;
6390 else if (new_x > it->first_visible_x)
6391 {
6392 /* Glyph is visible. Increment number of glyphs that
6393 would be displayed. */
6394 ++it->hpos;
6395 }
6396 else
6397 {
6398 /* Glyph is completely off the left margin of the display
6399 area. Nothing to do. */
6400 }
6401 }
6402
6403 if (result != MOVE_UNDEFINED)
6404 break;
6405 }
6406 else if (BUFFER_POS_REACHED_P ())
6407 {
6408 buffer_pos_reached:
6409 it->current_x = x;
6410 it->max_ascent = ascent;
6411 it->max_descent = descent;
6412 result = MOVE_POS_MATCH_OR_ZV;
6413 break;
6414 }
6415 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6416 {
6417 /* Stop when TO_X specified and reached. This check is
6418 necessary here because of lines consisting of a line end,
6419 only. The line end will not produce any glyphs and we
6420 would never get MOVE_X_REACHED. */
6421 xassert (it->nglyphs == 0);
6422 result = MOVE_X_REACHED;
6423 break;
6424 }
6425
6426 /* Is this a line end? If yes, we're done. */
6427 if (ITERATOR_AT_END_OF_LINE_P (it))
6428 {
6429 result = MOVE_NEWLINE_OR_CR;
6430 break;
6431 }
6432
6433 /* The current display element has been consumed. Advance
6434 to the next. */
6435 set_iterator_to_next (it, 1);
6436
6437 /* Stop if lines are truncated and IT's current x-position is
6438 past the right edge of the window now. */
6439 if (it->truncate_lines_p
6440 && it->current_x >= it->last_visible_x)
6441 {
6442 #ifdef HAVE_WINDOW_SYSTEM
6443 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6444 {
6445 if (!get_next_display_element (it)
6446 || BUFFER_POS_REACHED_P ())
6447 {
6448 result = MOVE_POS_MATCH_OR_ZV;
6449 break;
6450 }
6451 if (ITERATOR_AT_END_OF_LINE_P (it))
6452 {
6453 result = MOVE_NEWLINE_OR_CR;
6454 break;
6455 }
6456 }
6457 #endif /* HAVE_WINDOW_SYSTEM */
6458 result = MOVE_LINE_TRUNCATED;
6459 break;
6460 }
6461 }
6462
6463 #undef BUFFER_POS_REACHED_P
6464
6465 /* Restore the iterator settings altered at the beginning of this
6466 function. */
6467 it->glyph_row = saved_glyph_row;
6468 return result;
6469 }
6470
6471
6472 /* Move IT forward until it satisfies one or more of the criteria in
6473 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6474
6475 OP is a bit-mask that specifies where to stop, and in particular,
6476 which of those four position arguments makes a difference. See the
6477 description of enum move_operation_enum.
6478
6479 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6480 screen line, this function will set IT to the next position >
6481 TO_CHARPOS. */
6482
6483 void
6484 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6485 struct it *it;
6486 int to_charpos, to_x, to_y, to_vpos;
6487 int op;
6488 {
6489 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6490 int line_height;
6491 int reached = 0;
6492
6493 for (;;)
6494 {
6495 if (op & MOVE_TO_VPOS)
6496 {
6497 /* If no TO_CHARPOS and no TO_X specified, stop at the
6498 start of the line TO_VPOS. */
6499 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6500 {
6501 if (it->vpos == to_vpos)
6502 {
6503 reached = 1;
6504 break;
6505 }
6506 else
6507 skip = move_it_in_display_line_to (it, -1, -1, 0);
6508 }
6509 else
6510 {
6511 /* TO_VPOS >= 0 means stop at TO_X in the line at
6512 TO_VPOS, or at TO_POS, whichever comes first. */
6513 if (it->vpos == to_vpos)
6514 {
6515 reached = 2;
6516 break;
6517 }
6518
6519 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6520
6521 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6522 {
6523 reached = 3;
6524 break;
6525 }
6526 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6527 {
6528 /* We have reached TO_X but not in the line we want. */
6529 skip = move_it_in_display_line_to (it, to_charpos,
6530 -1, MOVE_TO_POS);
6531 if (skip == MOVE_POS_MATCH_OR_ZV)
6532 {
6533 reached = 4;
6534 break;
6535 }
6536 }
6537 }
6538 }
6539 else if (op & MOVE_TO_Y)
6540 {
6541 struct it it_backup;
6542
6543 /* TO_Y specified means stop at TO_X in the line containing
6544 TO_Y---or at TO_CHARPOS if this is reached first. The
6545 problem is that we can't really tell whether the line
6546 contains TO_Y before we have completely scanned it, and
6547 this may skip past TO_X. What we do is to first scan to
6548 TO_X.
6549
6550 If TO_X is not specified, use a TO_X of zero. The reason
6551 is to make the outcome of this function more predictable.
6552 If we didn't use TO_X == 0, we would stop at the end of
6553 the line which is probably not what a caller would expect
6554 to happen. */
6555 skip = move_it_in_display_line_to (it, to_charpos,
6556 ((op & MOVE_TO_X)
6557 ? to_x : 0),
6558 (MOVE_TO_X
6559 | (op & MOVE_TO_POS)));
6560
6561 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6562 if (skip == MOVE_POS_MATCH_OR_ZV)
6563 {
6564 reached = 5;
6565 break;
6566 }
6567
6568 /* If TO_X was reached, we would like to know whether TO_Y
6569 is in the line. This can only be said if we know the
6570 total line height which requires us to scan the rest of
6571 the line. */
6572 if (skip == MOVE_X_REACHED)
6573 {
6574 it_backup = *it;
6575 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6576 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6577 op & MOVE_TO_POS);
6578 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6579 }
6580
6581 /* Now, decide whether TO_Y is in this line. */
6582 line_height = it->max_ascent + it->max_descent;
6583 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6584
6585 if (to_y >= it->current_y
6586 && to_y < it->current_y + line_height)
6587 {
6588 if (skip == MOVE_X_REACHED)
6589 /* If TO_Y is in this line and TO_X was reached above,
6590 we scanned too far. We have to restore IT's settings
6591 to the ones before skipping. */
6592 *it = it_backup;
6593 reached = 6;
6594 }
6595 else if (skip == MOVE_X_REACHED)
6596 {
6597 skip = skip2;
6598 if (skip == MOVE_POS_MATCH_OR_ZV)
6599 reached = 7;
6600 }
6601
6602 if (reached)
6603 break;
6604 }
6605 else
6606 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6607
6608 switch (skip)
6609 {
6610 case MOVE_POS_MATCH_OR_ZV:
6611 reached = 8;
6612 goto out;
6613
6614 case MOVE_NEWLINE_OR_CR:
6615 set_iterator_to_next (it, 1);
6616 it->continuation_lines_width = 0;
6617 break;
6618
6619 case MOVE_LINE_TRUNCATED:
6620 it->continuation_lines_width = 0;
6621 reseat_at_next_visible_line_start (it, 0);
6622 if ((op & MOVE_TO_POS) != 0
6623 && IT_CHARPOS (*it) > to_charpos)
6624 {
6625 reached = 9;
6626 goto out;
6627 }
6628 break;
6629
6630 case MOVE_LINE_CONTINUED:
6631 it->continuation_lines_width += it->current_x;
6632 break;
6633
6634 default:
6635 abort ();
6636 }
6637
6638 /* Reset/increment for the next run. */
6639 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6640 it->current_x = it->hpos = 0;
6641 it->current_y += it->max_ascent + it->max_descent;
6642 ++it->vpos;
6643 last_height = it->max_ascent + it->max_descent;
6644 last_max_ascent = it->max_ascent;
6645 it->max_ascent = it->max_descent = 0;
6646 }
6647
6648 out:
6649
6650 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6651 }
6652
6653
6654 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6655
6656 If DY > 0, move IT backward at least that many pixels. DY = 0
6657 means move IT backward to the preceding line start or BEGV. This
6658 function may move over more than DY pixels if IT->current_y - DY
6659 ends up in the middle of a line; in this case IT->current_y will be
6660 set to the top of the line moved to. */
6661
6662 void
6663 move_it_vertically_backward (it, dy)
6664 struct it *it;
6665 int dy;
6666 {
6667 int nlines, h;
6668 struct it it2, it3;
6669 int start_pos;
6670
6671 move_further_back:
6672 xassert (dy >= 0);
6673
6674 start_pos = IT_CHARPOS (*it);
6675
6676 /* Estimate how many newlines we must move back. */
6677 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6678
6679 /* Set the iterator's position that many lines back. */
6680 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6681 back_to_previous_visible_line_start (it);
6682
6683 /* Reseat the iterator here. When moving backward, we don't want
6684 reseat to skip forward over invisible text, set up the iterator
6685 to deliver from overlay strings at the new position etc. So,
6686 use reseat_1 here. */
6687 reseat_1 (it, it->current.pos, 1);
6688
6689 /* We are now surely at a line start. */
6690 it->current_x = it->hpos = 0;
6691 it->continuation_lines_width = 0;
6692
6693 /* Move forward and see what y-distance we moved. First move to the
6694 start of the next line so that we get its height. We need this
6695 height to be able to tell whether we reached the specified
6696 y-distance. */
6697 it2 = *it;
6698 it2.max_ascent = it2.max_descent = 0;
6699 do
6700 {
6701 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6702 MOVE_TO_POS | MOVE_TO_VPOS);
6703 }
6704 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6705 xassert (IT_CHARPOS (*it) >= BEGV);
6706 it3 = it2;
6707
6708 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6709 xassert (IT_CHARPOS (*it) >= BEGV);
6710 /* H is the actual vertical distance from the position in *IT
6711 and the starting position. */
6712 h = it2.current_y - it->current_y;
6713 /* NLINES is the distance in number of lines. */
6714 nlines = it2.vpos - it->vpos;
6715
6716 /* Correct IT's y and vpos position
6717 so that they are relative to the starting point. */
6718 it->vpos -= nlines;
6719 it->current_y -= h;
6720
6721 if (dy == 0)
6722 {
6723 /* DY == 0 means move to the start of the screen line. The
6724 value of nlines is > 0 if continuation lines were involved. */
6725 if (nlines > 0)
6726 move_it_by_lines (it, nlines, 1);
6727 #if 0
6728 /* I think this assert is bogus if buffer contains
6729 invisible text or images. KFS. */
6730 xassert (IT_CHARPOS (*it) <= start_pos);
6731 #endif
6732 }
6733 else
6734 {
6735 /* The y-position we try to reach, relative to *IT.
6736 Note that H has been subtracted in front of the if-statement. */
6737 int target_y = it->current_y + h - dy;
6738 int y0 = it3.current_y;
6739 int y1 = line_bottom_y (&it3);
6740 int line_height = y1 - y0;
6741
6742 /* If we did not reach target_y, try to move further backward if
6743 we can. If we moved too far backward, try to move forward. */
6744 if (target_y < it->current_y
6745 /* This is heuristic. In a window that's 3 lines high, with
6746 a line height of 13 pixels each, recentering with point
6747 on the bottom line will try to move -39/2 = 19 pixels
6748 backward. Try to avoid moving into the first line. */
6749 && (it->current_y - target_y
6750 > min (window_box_height (it->w), line_height * 2 / 3))
6751 && IT_CHARPOS (*it) > BEGV)
6752 {
6753 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6754 target_y - it->current_y));
6755 dy = it->current_y - target_y;
6756 goto move_further_back;
6757 }
6758 else if (target_y >= it->current_y + line_height
6759 && IT_CHARPOS (*it) < ZV)
6760 {
6761 /* Should move forward by at least one line, maybe more.
6762
6763 Note: Calling move_it_by_lines can be expensive on
6764 terminal frames, where compute_motion is used (via
6765 vmotion) to do the job, when there are very long lines
6766 and truncate-lines is nil. That's the reason for
6767 treating terminal frames specially here. */
6768
6769 if (!FRAME_WINDOW_P (it->f))
6770 move_it_vertically (it, target_y - (it->current_y + line_height));
6771 else
6772 {
6773 do
6774 {
6775 move_it_by_lines (it, 1, 1);
6776 }
6777 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6778 }
6779
6780 #if 0
6781 /* I think this assert is bogus if buffer contains
6782 invisible text or images. KFS. */
6783 xassert (IT_CHARPOS (*it) >= BEGV);
6784 #endif
6785 }
6786 }
6787 }
6788
6789
6790 /* Move IT by a specified amount of pixel lines DY. DY negative means
6791 move backwards. DY = 0 means move to start of screen line. At the
6792 end, IT will be on the start of a screen line. */
6793
6794 void
6795 move_it_vertically (it, dy)
6796 struct it *it;
6797 int dy;
6798 {
6799 if (dy <= 0)
6800 move_it_vertically_backward (it, -dy);
6801 else
6802 {
6803 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6804 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6805 MOVE_TO_POS | MOVE_TO_Y);
6806 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6807
6808 /* If buffer ends in ZV without a newline, move to the start of
6809 the line to satisfy the post-condition. */
6810 if (IT_CHARPOS (*it) == ZV
6811 && ZV > BEGV
6812 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6813 move_it_by_lines (it, 0, 0);
6814 }
6815 }
6816
6817
6818 /* Move iterator IT past the end of the text line it is in. */
6819
6820 void
6821 move_it_past_eol (it)
6822 struct it *it;
6823 {
6824 enum move_it_result rc;
6825
6826 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6827 if (rc == MOVE_NEWLINE_OR_CR)
6828 set_iterator_to_next (it, 0);
6829 }
6830
6831
6832 #if 0 /* Currently not used. */
6833
6834 /* Return non-zero if some text between buffer positions START_CHARPOS
6835 and END_CHARPOS is invisible. IT->window is the window for text
6836 property lookup. */
6837
6838 static int
6839 invisible_text_between_p (it, start_charpos, end_charpos)
6840 struct it *it;
6841 int start_charpos, end_charpos;
6842 {
6843 Lisp_Object prop, limit;
6844 int invisible_found_p;
6845
6846 xassert (it != NULL && start_charpos <= end_charpos);
6847
6848 /* Is text at START invisible? */
6849 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6850 it->window);
6851 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6852 invisible_found_p = 1;
6853 else
6854 {
6855 limit = Fnext_single_char_property_change (make_number (start_charpos),
6856 Qinvisible, Qnil,
6857 make_number (end_charpos));
6858 invisible_found_p = XFASTINT (limit) < end_charpos;
6859 }
6860
6861 return invisible_found_p;
6862 }
6863
6864 #endif /* 0 */
6865
6866
6867 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6868 negative means move up. DVPOS == 0 means move to the start of the
6869 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6870 NEED_Y_P is zero, IT->current_y will be left unchanged.
6871
6872 Further optimization ideas: If we would know that IT->f doesn't use
6873 a face with proportional font, we could be faster for
6874 truncate-lines nil. */
6875
6876 void
6877 move_it_by_lines (it, dvpos, need_y_p)
6878 struct it *it;
6879 int dvpos, need_y_p;
6880 {
6881 struct position pos;
6882
6883 if (!FRAME_WINDOW_P (it->f))
6884 {
6885 struct text_pos textpos;
6886
6887 /* We can use vmotion on frames without proportional fonts. */
6888 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6889 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6890 reseat (it, textpos, 1);
6891 it->vpos += pos.vpos;
6892 it->current_y += pos.vpos;
6893 }
6894 else if (dvpos == 0)
6895 {
6896 /* DVPOS == 0 means move to the start of the screen line. */
6897 move_it_vertically_backward (it, 0);
6898 xassert (it->current_x == 0 && it->hpos == 0);
6899 /* Let next call to line_bottom_y calculate real line height */
6900 last_height = 0;
6901 }
6902 else if (dvpos > 0)
6903 {
6904 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6905 if (!IT_POS_VALID_AFTER_MOVE_P (it))
6906 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
6907 }
6908 else
6909 {
6910 struct it it2;
6911 int start_charpos, i;
6912
6913 /* Start at the beginning of the screen line containing IT's
6914 position. This may actually move vertically backwards,
6915 in case of overlays, so adjust dvpos accordingly. */
6916 dvpos += it->vpos;
6917 move_it_vertically_backward (it, 0);
6918 dvpos -= it->vpos;
6919
6920 /* Go back -DVPOS visible lines and reseat the iterator there. */
6921 start_charpos = IT_CHARPOS (*it);
6922 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
6923 back_to_previous_visible_line_start (it);
6924 reseat (it, it->current.pos, 1);
6925
6926 /* Move further back if we end up in a string or an image. */
6927 while (!IT_POS_VALID_AFTER_MOVE_P (it))
6928 {
6929 /* First try to move to start of display line. */
6930 dvpos += it->vpos;
6931 move_it_vertically_backward (it, 0);
6932 dvpos -= it->vpos;
6933 if (IT_POS_VALID_AFTER_MOVE_P (it))
6934 break;
6935 /* If start of line is still in string or image,
6936 move further back. */
6937 back_to_previous_visible_line_start (it);
6938 reseat (it, it->current.pos, 1);
6939 dvpos--;
6940 }
6941
6942 it->current_x = it->hpos = 0;
6943
6944 /* Above call may have moved too far if continuation lines
6945 are involved. Scan forward and see if it did. */
6946 it2 = *it;
6947 it2.vpos = it2.current_y = 0;
6948 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6949 it->vpos -= it2.vpos;
6950 it->current_y -= it2.current_y;
6951 it->current_x = it->hpos = 0;
6952
6953 /* If we moved too far back, move IT some lines forward. */
6954 if (it2.vpos > -dvpos)
6955 {
6956 int delta = it2.vpos + dvpos;
6957 it2 = *it;
6958 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6959 /* Move back again if we got too far ahead. */
6960 if (IT_CHARPOS (*it) >= start_charpos)
6961 *it = it2;
6962 }
6963 }
6964 }
6965
6966 /* Return 1 if IT points into the middle of a display vector. */
6967
6968 int
6969 in_display_vector_p (it)
6970 struct it *it;
6971 {
6972 return (it->method == GET_FROM_DISPLAY_VECTOR
6973 && it->current.dpvec_index > 0
6974 && it->dpvec + it->current.dpvec_index != it->dpend);
6975 }
6976
6977 \f
6978 /***********************************************************************
6979 Messages
6980 ***********************************************************************/
6981
6982
6983 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6984 to *Messages*. */
6985
6986 void
6987 add_to_log (format, arg1, arg2)
6988 char *format;
6989 Lisp_Object arg1, arg2;
6990 {
6991 Lisp_Object args[3];
6992 Lisp_Object msg, fmt;
6993 char *buffer;
6994 int len;
6995 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6996 USE_SAFE_ALLOCA;
6997
6998 /* Do nothing if called asynchronously. Inserting text into
6999 a buffer may call after-change-functions and alike and
7000 that would means running Lisp asynchronously. */
7001 if (handling_signal)
7002 return;
7003
7004 fmt = msg = Qnil;
7005 GCPRO4 (fmt, msg, arg1, arg2);
7006
7007 args[0] = fmt = build_string (format);
7008 args[1] = arg1;
7009 args[2] = arg2;
7010 msg = Fformat (3, args);
7011
7012 len = SBYTES (msg) + 1;
7013 SAFE_ALLOCA (buffer, char *, len);
7014 bcopy (SDATA (msg), buffer, len);
7015
7016 message_dolog (buffer, len - 1, 1, 0);
7017 SAFE_FREE ();
7018
7019 UNGCPRO;
7020 }
7021
7022
7023 /* Output a newline in the *Messages* buffer if "needs" one. */
7024
7025 void
7026 message_log_maybe_newline ()
7027 {
7028 if (message_log_need_newline)
7029 message_dolog ("", 0, 1, 0);
7030 }
7031
7032
7033 /* Add a string M of length NBYTES to the message log, optionally
7034 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7035 nonzero, means interpret the contents of M as multibyte. This
7036 function calls low-level routines in order to bypass text property
7037 hooks, etc. which might not be safe to run.
7038
7039 This may GC (insert may run before/after change hooks),
7040 so the buffer M must NOT point to a Lisp string. */
7041
7042 void
7043 message_dolog (m, nbytes, nlflag, multibyte)
7044 const char *m;
7045 int nbytes, nlflag, multibyte;
7046 {
7047 if (!NILP (Vmemory_full))
7048 return;
7049
7050 if (!NILP (Vmessage_log_max))
7051 {
7052 struct buffer *oldbuf;
7053 Lisp_Object oldpoint, oldbegv, oldzv;
7054 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7055 int point_at_end = 0;
7056 int zv_at_end = 0;
7057 Lisp_Object old_deactivate_mark, tem;
7058 struct gcpro gcpro1;
7059
7060 old_deactivate_mark = Vdeactivate_mark;
7061 oldbuf = current_buffer;
7062 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7063 current_buffer->undo_list = Qt;
7064
7065 oldpoint = message_dolog_marker1;
7066 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7067 oldbegv = message_dolog_marker2;
7068 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7069 oldzv = message_dolog_marker3;
7070 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7071 GCPRO1 (old_deactivate_mark);
7072
7073 if (PT == Z)
7074 point_at_end = 1;
7075 if (ZV == Z)
7076 zv_at_end = 1;
7077
7078 BEGV = BEG;
7079 BEGV_BYTE = BEG_BYTE;
7080 ZV = Z;
7081 ZV_BYTE = Z_BYTE;
7082 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7083
7084 /* Insert the string--maybe converting multibyte to single byte
7085 or vice versa, so that all the text fits the buffer. */
7086 if (multibyte
7087 && NILP (current_buffer->enable_multibyte_characters))
7088 {
7089 int i, c, char_bytes;
7090 unsigned char work[1];
7091
7092 /* Convert a multibyte string to single-byte
7093 for the *Message* buffer. */
7094 for (i = 0; i < nbytes; i += char_bytes)
7095 {
7096 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7097 work[0] = (SINGLE_BYTE_CHAR_P (c)
7098 ? c
7099 : multibyte_char_to_unibyte (c, Qnil));
7100 insert_1_both (work, 1, 1, 1, 0, 0);
7101 }
7102 }
7103 else if (! multibyte
7104 && ! NILP (current_buffer->enable_multibyte_characters))
7105 {
7106 int i, c, char_bytes;
7107 unsigned char *msg = (unsigned char *) m;
7108 unsigned char str[MAX_MULTIBYTE_LENGTH];
7109 /* Convert a single-byte string to multibyte
7110 for the *Message* buffer. */
7111 for (i = 0; i < nbytes; i++)
7112 {
7113 c = unibyte_char_to_multibyte (msg[i]);
7114 char_bytes = CHAR_STRING (c, str);
7115 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7116 }
7117 }
7118 else if (nbytes)
7119 insert_1 (m, nbytes, 1, 0, 0);
7120
7121 if (nlflag)
7122 {
7123 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7124 insert_1 ("\n", 1, 1, 0, 0);
7125
7126 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7127 this_bol = PT;
7128 this_bol_byte = PT_BYTE;
7129
7130 /* See if this line duplicates the previous one.
7131 If so, combine duplicates. */
7132 if (this_bol > BEG)
7133 {
7134 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7135 prev_bol = PT;
7136 prev_bol_byte = PT_BYTE;
7137
7138 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7139 this_bol, this_bol_byte);
7140 if (dup)
7141 {
7142 del_range_both (prev_bol, prev_bol_byte,
7143 this_bol, this_bol_byte, 0);
7144 if (dup > 1)
7145 {
7146 char dupstr[40];
7147 int duplen;
7148
7149 /* If you change this format, don't forget to also
7150 change message_log_check_duplicate. */
7151 sprintf (dupstr, " [%d times]", dup);
7152 duplen = strlen (dupstr);
7153 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7154 insert_1 (dupstr, duplen, 1, 0, 1);
7155 }
7156 }
7157 }
7158
7159 /* If we have more than the desired maximum number of lines
7160 in the *Messages* buffer now, delete the oldest ones.
7161 This is safe because we don't have undo in this buffer. */
7162
7163 if (NATNUMP (Vmessage_log_max))
7164 {
7165 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7166 -XFASTINT (Vmessage_log_max) - 1, 0);
7167 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7168 }
7169 }
7170 BEGV = XMARKER (oldbegv)->charpos;
7171 BEGV_BYTE = marker_byte_position (oldbegv);
7172
7173 if (zv_at_end)
7174 {
7175 ZV = Z;
7176 ZV_BYTE = Z_BYTE;
7177 }
7178 else
7179 {
7180 ZV = XMARKER (oldzv)->charpos;
7181 ZV_BYTE = marker_byte_position (oldzv);
7182 }
7183
7184 if (point_at_end)
7185 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7186 else
7187 /* We can't do Fgoto_char (oldpoint) because it will run some
7188 Lisp code. */
7189 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7190 XMARKER (oldpoint)->bytepos);
7191
7192 UNGCPRO;
7193 unchain_marker (XMARKER (oldpoint));
7194 unchain_marker (XMARKER (oldbegv));
7195 unchain_marker (XMARKER (oldzv));
7196
7197 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7198 set_buffer_internal (oldbuf);
7199 if (NILP (tem))
7200 windows_or_buffers_changed = old_windows_or_buffers_changed;
7201 message_log_need_newline = !nlflag;
7202 Vdeactivate_mark = old_deactivate_mark;
7203 }
7204 }
7205
7206
7207 /* We are at the end of the buffer after just having inserted a newline.
7208 (Note: We depend on the fact we won't be crossing the gap.)
7209 Check to see if the most recent message looks a lot like the previous one.
7210 Return 0 if different, 1 if the new one should just replace it, or a
7211 value N > 1 if we should also append " [N times]". */
7212
7213 static int
7214 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7215 int prev_bol, this_bol;
7216 int prev_bol_byte, this_bol_byte;
7217 {
7218 int i;
7219 int len = Z_BYTE - 1 - this_bol_byte;
7220 int seen_dots = 0;
7221 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7222 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7223
7224 for (i = 0; i < len; i++)
7225 {
7226 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7227 seen_dots = 1;
7228 if (p1[i] != p2[i])
7229 return seen_dots;
7230 }
7231 p1 += len;
7232 if (*p1 == '\n')
7233 return 2;
7234 if (*p1++ == ' ' && *p1++ == '[')
7235 {
7236 int n = 0;
7237 while (*p1 >= '0' && *p1 <= '9')
7238 n = n * 10 + *p1++ - '0';
7239 if (strncmp (p1, " times]\n", 8) == 0)
7240 return n+1;
7241 }
7242 return 0;
7243 }
7244 \f
7245
7246 /* Display an echo area message M with a specified length of NBYTES
7247 bytes. The string may include null characters. If M is 0, clear
7248 out any existing message, and let the mini-buffer text show
7249 through.
7250
7251 This may GC, so the buffer M must NOT point to a Lisp string. */
7252
7253 void
7254 message2 (m, nbytes, multibyte)
7255 const char *m;
7256 int nbytes;
7257 int multibyte;
7258 {
7259 /* First flush out any partial line written with print. */
7260 message_log_maybe_newline ();
7261 if (m)
7262 message_dolog (m, nbytes, 1, multibyte);
7263 message2_nolog (m, nbytes, multibyte);
7264 }
7265
7266
7267 /* The non-logging counterpart of message2. */
7268
7269 void
7270 message2_nolog (m, nbytes, multibyte)
7271 const char *m;
7272 int nbytes, multibyte;
7273 {
7274 struct frame *sf = SELECTED_FRAME ();
7275 message_enable_multibyte = multibyte;
7276
7277 if (noninteractive)
7278 {
7279 if (noninteractive_need_newline)
7280 putc ('\n', stderr);
7281 noninteractive_need_newline = 0;
7282 if (m)
7283 fwrite (m, nbytes, 1, stderr);
7284 if (cursor_in_echo_area == 0)
7285 fprintf (stderr, "\n");
7286 fflush (stderr);
7287 }
7288 /* A null message buffer means that the frame hasn't really been
7289 initialized yet. Error messages get reported properly by
7290 cmd_error, so this must be just an informative message; toss it. */
7291 else if (INTERACTIVE
7292 && sf->glyphs_initialized_p
7293 && FRAME_MESSAGE_BUF (sf))
7294 {
7295 Lisp_Object mini_window;
7296 struct frame *f;
7297
7298 /* Get the frame containing the mini-buffer
7299 that the selected frame is using. */
7300 mini_window = FRAME_MINIBUF_WINDOW (sf);
7301 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7302
7303 FRAME_SAMPLE_VISIBILITY (f);
7304 if (FRAME_VISIBLE_P (sf)
7305 && ! FRAME_VISIBLE_P (f))
7306 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7307
7308 if (m)
7309 {
7310 set_message (m, Qnil, nbytes, multibyte);
7311 if (minibuffer_auto_raise)
7312 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7313 }
7314 else
7315 clear_message (1, 1);
7316
7317 do_pending_window_change (0);
7318 echo_area_display (1);
7319 do_pending_window_change (0);
7320 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7321 (*frame_up_to_date_hook) (f);
7322 }
7323 }
7324
7325
7326 /* Display an echo area message M with a specified length of NBYTES
7327 bytes. The string may include null characters. If M is not a
7328 string, clear out any existing message, and let the mini-buffer
7329 text show through.
7330
7331 This function cancels echoing. */
7332
7333 void
7334 message3 (m, nbytes, multibyte)
7335 Lisp_Object m;
7336 int nbytes;
7337 int multibyte;
7338 {
7339 struct gcpro gcpro1;
7340
7341 GCPRO1 (m);
7342 clear_message (1,1);
7343 cancel_echoing ();
7344
7345 /* First flush out any partial line written with print. */
7346 message_log_maybe_newline ();
7347 if (STRINGP (m))
7348 {
7349 char *buffer;
7350 USE_SAFE_ALLOCA;
7351
7352 SAFE_ALLOCA (buffer, char *, nbytes);
7353 bcopy (SDATA (m), buffer, nbytes);
7354 message_dolog (buffer, nbytes, 1, multibyte);
7355 SAFE_FREE ();
7356 }
7357 message3_nolog (m, nbytes, multibyte);
7358
7359 UNGCPRO;
7360 }
7361
7362
7363 /* The non-logging version of message3.
7364 This does not cancel echoing, because it is used for echoing.
7365 Perhaps we need to make a separate function for echoing
7366 and make this cancel echoing. */
7367
7368 void
7369 message3_nolog (m, nbytes, multibyte)
7370 Lisp_Object m;
7371 int nbytes, multibyte;
7372 {
7373 struct frame *sf = SELECTED_FRAME ();
7374 message_enable_multibyte = multibyte;
7375
7376 if (noninteractive)
7377 {
7378 if (noninteractive_need_newline)
7379 putc ('\n', stderr);
7380 noninteractive_need_newline = 0;
7381 if (STRINGP (m))
7382 fwrite (SDATA (m), nbytes, 1, stderr);
7383 if (cursor_in_echo_area == 0)
7384 fprintf (stderr, "\n");
7385 fflush (stderr);
7386 }
7387 /* A null message buffer means that the frame hasn't really been
7388 initialized yet. Error messages get reported properly by
7389 cmd_error, so this must be just an informative message; toss it. */
7390 else if (INTERACTIVE
7391 && sf->glyphs_initialized_p
7392 && FRAME_MESSAGE_BUF (sf))
7393 {
7394 Lisp_Object mini_window;
7395 Lisp_Object frame;
7396 struct frame *f;
7397
7398 /* Get the frame containing the mini-buffer
7399 that the selected frame is using. */
7400 mini_window = FRAME_MINIBUF_WINDOW (sf);
7401 frame = XWINDOW (mini_window)->frame;
7402 f = XFRAME (frame);
7403
7404 FRAME_SAMPLE_VISIBILITY (f);
7405 if (FRAME_VISIBLE_P (sf)
7406 && !FRAME_VISIBLE_P (f))
7407 Fmake_frame_visible (frame);
7408
7409 if (STRINGP (m) && SCHARS (m) > 0)
7410 {
7411 set_message (NULL, m, nbytes, multibyte);
7412 if (minibuffer_auto_raise)
7413 Fraise_frame (frame);
7414 /* Assume we are not echoing.
7415 (If we are, echo_now will override this.) */
7416 echo_message_buffer = Qnil;
7417 }
7418 else
7419 clear_message (1, 1);
7420
7421 do_pending_window_change (0);
7422 echo_area_display (1);
7423 do_pending_window_change (0);
7424 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7425 (*frame_up_to_date_hook) (f);
7426 }
7427 }
7428
7429
7430 /* Display a null-terminated echo area message M. If M is 0, clear
7431 out any existing message, and let the mini-buffer text show through.
7432
7433 The buffer M must continue to exist until after the echo area gets
7434 cleared or some other message gets displayed there. Do not pass
7435 text that is stored in a Lisp string. Do not pass text in a buffer
7436 that was alloca'd. */
7437
7438 void
7439 message1 (m)
7440 char *m;
7441 {
7442 message2 (m, (m ? strlen (m) : 0), 0);
7443 }
7444
7445
7446 /* The non-logging counterpart of message1. */
7447
7448 void
7449 message1_nolog (m)
7450 char *m;
7451 {
7452 message2_nolog (m, (m ? strlen (m) : 0), 0);
7453 }
7454
7455 /* Display a message M which contains a single %s
7456 which gets replaced with STRING. */
7457
7458 void
7459 message_with_string (m, string, log)
7460 char *m;
7461 Lisp_Object string;
7462 int log;
7463 {
7464 CHECK_STRING (string);
7465
7466 if (noninteractive)
7467 {
7468 if (m)
7469 {
7470 if (noninteractive_need_newline)
7471 putc ('\n', stderr);
7472 noninteractive_need_newline = 0;
7473 fprintf (stderr, m, SDATA (string));
7474 if (cursor_in_echo_area == 0)
7475 fprintf (stderr, "\n");
7476 fflush (stderr);
7477 }
7478 }
7479 else if (INTERACTIVE)
7480 {
7481 /* The frame whose minibuffer we're going to display the message on.
7482 It may be larger than the selected frame, so we need
7483 to use its buffer, not the selected frame's buffer. */
7484 Lisp_Object mini_window;
7485 struct frame *f, *sf = SELECTED_FRAME ();
7486
7487 /* Get the frame containing the minibuffer
7488 that the selected frame is using. */
7489 mini_window = FRAME_MINIBUF_WINDOW (sf);
7490 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7491
7492 /* A null message buffer means that the frame hasn't really been
7493 initialized yet. Error messages get reported properly by
7494 cmd_error, so this must be just an informative message; toss it. */
7495 if (FRAME_MESSAGE_BUF (f))
7496 {
7497 Lisp_Object args[2], message;
7498 struct gcpro gcpro1, gcpro2;
7499
7500 args[0] = build_string (m);
7501 args[1] = message = string;
7502 GCPRO2 (args[0], message);
7503 gcpro1.nvars = 2;
7504
7505 message = Fformat (2, args);
7506
7507 if (log)
7508 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7509 else
7510 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7511
7512 UNGCPRO;
7513
7514 /* Print should start at the beginning of the message
7515 buffer next time. */
7516 message_buf_print = 0;
7517 }
7518 }
7519 }
7520
7521
7522 /* Dump an informative message to the minibuf. If M is 0, clear out
7523 any existing message, and let the mini-buffer text show through. */
7524
7525 /* VARARGS 1 */
7526 void
7527 message (m, a1, a2, a3)
7528 char *m;
7529 EMACS_INT a1, a2, a3;
7530 {
7531 if (noninteractive)
7532 {
7533 if (m)
7534 {
7535 if (noninteractive_need_newline)
7536 putc ('\n', stderr);
7537 noninteractive_need_newline = 0;
7538 fprintf (stderr, m, a1, a2, a3);
7539 if (cursor_in_echo_area == 0)
7540 fprintf (stderr, "\n");
7541 fflush (stderr);
7542 }
7543 }
7544 else if (INTERACTIVE)
7545 {
7546 /* The frame whose mini-buffer we're going to display the message
7547 on. It may be larger than the selected frame, so we need to
7548 use its buffer, not the selected frame's buffer. */
7549 Lisp_Object mini_window;
7550 struct frame *f, *sf = SELECTED_FRAME ();
7551
7552 /* Get the frame containing the mini-buffer
7553 that the selected frame is using. */
7554 mini_window = FRAME_MINIBUF_WINDOW (sf);
7555 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7556
7557 /* A null message buffer means that the frame hasn't really been
7558 initialized yet. Error messages get reported properly by
7559 cmd_error, so this must be just an informative message; toss
7560 it. */
7561 if (FRAME_MESSAGE_BUF (f))
7562 {
7563 if (m)
7564 {
7565 int len;
7566 #ifdef NO_ARG_ARRAY
7567 char *a[3];
7568 a[0] = (char *) a1;
7569 a[1] = (char *) a2;
7570 a[2] = (char *) a3;
7571
7572 len = doprnt (FRAME_MESSAGE_BUF (f),
7573 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7574 #else
7575 len = doprnt (FRAME_MESSAGE_BUF (f),
7576 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7577 (char **) &a1);
7578 #endif /* NO_ARG_ARRAY */
7579
7580 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7581 }
7582 else
7583 message1 (0);
7584
7585 /* Print should start at the beginning of the message
7586 buffer next time. */
7587 message_buf_print = 0;
7588 }
7589 }
7590 }
7591
7592
7593 /* The non-logging version of message. */
7594
7595 void
7596 message_nolog (m, a1, a2, a3)
7597 char *m;
7598 EMACS_INT a1, a2, a3;
7599 {
7600 Lisp_Object old_log_max;
7601 old_log_max = Vmessage_log_max;
7602 Vmessage_log_max = Qnil;
7603 message (m, a1, a2, a3);
7604 Vmessage_log_max = old_log_max;
7605 }
7606
7607
7608 /* Display the current message in the current mini-buffer. This is
7609 only called from error handlers in process.c, and is not time
7610 critical. */
7611
7612 void
7613 update_echo_area ()
7614 {
7615 if (!NILP (echo_area_buffer[0]))
7616 {
7617 Lisp_Object string;
7618 string = Fcurrent_message ();
7619 message3 (string, SBYTES (string),
7620 !NILP (current_buffer->enable_multibyte_characters));
7621 }
7622 }
7623
7624
7625 /* Make sure echo area buffers in `echo_buffers' are live.
7626 If they aren't, make new ones. */
7627
7628 static void
7629 ensure_echo_area_buffers ()
7630 {
7631 int i;
7632
7633 for (i = 0; i < 2; ++i)
7634 if (!BUFFERP (echo_buffer[i])
7635 || NILP (XBUFFER (echo_buffer[i])->name))
7636 {
7637 char name[30];
7638 Lisp_Object old_buffer;
7639 int j;
7640
7641 old_buffer = echo_buffer[i];
7642 sprintf (name, " *Echo Area %d*", i);
7643 echo_buffer[i] = Fget_buffer_create (build_string (name));
7644 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7645
7646 for (j = 0; j < 2; ++j)
7647 if (EQ (old_buffer, echo_area_buffer[j]))
7648 echo_area_buffer[j] = echo_buffer[i];
7649 }
7650 }
7651
7652
7653 /* Call FN with args A1..A4 with either the current or last displayed
7654 echo_area_buffer as current buffer.
7655
7656 WHICH zero means use the current message buffer
7657 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7658 from echo_buffer[] and clear it.
7659
7660 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7661 suitable buffer from echo_buffer[] and clear it.
7662
7663 Value is what FN returns. */
7664
7665 static int
7666 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7667 struct window *w;
7668 int which;
7669 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7670 EMACS_INT a1;
7671 Lisp_Object a2;
7672 EMACS_INT a3, a4;
7673 {
7674 Lisp_Object buffer;
7675 int this_one, the_other, clear_buffer_p, rc;
7676 int count = SPECPDL_INDEX ();
7677
7678 /* If buffers aren't live, make new ones. */
7679 ensure_echo_area_buffers ();
7680
7681 clear_buffer_p = 0;
7682
7683 if (which == 0)
7684 this_one = 0, the_other = 1;
7685 else if (which > 0)
7686 this_one = 1, the_other = 0;
7687
7688 /* Choose a suitable buffer from echo_buffer[] is we don't
7689 have one. */
7690 if (NILP (echo_area_buffer[this_one]))
7691 {
7692 echo_area_buffer[this_one]
7693 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7694 ? echo_buffer[the_other]
7695 : echo_buffer[this_one]);
7696 clear_buffer_p = 1;
7697 }
7698
7699 buffer = echo_area_buffer[this_one];
7700
7701 /* Don't get confused by reusing the buffer used for echoing
7702 for a different purpose. */
7703 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7704 cancel_echoing ();
7705
7706 record_unwind_protect (unwind_with_echo_area_buffer,
7707 with_echo_area_buffer_unwind_data (w));
7708
7709 /* Make the echo area buffer current. Note that for display
7710 purposes, it is not necessary that the displayed window's buffer
7711 == current_buffer, except for text property lookup. So, let's
7712 only set that buffer temporarily here without doing a full
7713 Fset_window_buffer. We must also change w->pointm, though,
7714 because otherwise an assertions in unshow_buffer fails, and Emacs
7715 aborts. */
7716 set_buffer_internal_1 (XBUFFER (buffer));
7717 if (w)
7718 {
7719 w->buffer = buffer;
7720 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7721 }
7722
7723 current_buffer->undo_list = Qt;
7724 current_buffer->read_only = Qnil;
7725 specbind (Qinhibit_read_only, Qt);
7726 specbind (Qinhibit_modification_hooks, Qt);
7727
7728 if (clear_buffer_p && Z > BEG)
7729 del_range (BEG, Z);
7730
7731 xassert (BEGV >= BEG);
7732 xassert (ZV <= Z && ZV >= BEGV);
7733
7734 rc = fn (a1, a2, a3, a4);
7735
7736 xassert (BEGV >= BEG);
7737 xassert (ZV <= Z && ZV >= BEGV);
7738
7739 unbind_to (count, Qnil);
7740 return rc;
7741 }
7742
7743
7744 /* Save state that should be preserved around the call to the function
7745 FN called in with_echo_area_buffer. */
7746
7747 static Lisp_Object
7748 with_echo_area_buffer_unwind_data (w)
7749 struct window *w;
7750 {
7751 int i = 0;
7752 Lisp_Object vector;
7753
7754 /* Reduce consing by keeping one vector in
7755 Vwith_echo_area_save_vector. */
7756 vector = Vwith_echo_area_save_vector;
7757 Vwith_echo_area_save_vector = Qnil;
7758
7759 if (NILP (vector))
7760 vector = Fmake_vector (make_number (7), Qnil);
7761
7762 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7763 AREF (vector, i) = Vdeactivate_mark, ++i;
7764 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7765
7766 if (w)
7767 {
7768 XSETWINDOW (AREF (vector, i), w); ++i;
7769 AREF (vector, i) = w->buffer; ++i;
7770 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7771 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7772 }
7773 else
7774 {
7775 int end = i + 4;
7776 for (; i < end; ++i)
7777 AREF (vector, i) = Qnil;
7778 }
7779
7780 xassert (i == ASIZE (vector));
7781 return vector;
7782 }
7783
7784
7785 /* Restore global state from VECTOR which was created by
7786 with_echo_area_buffer_unwind_data. */
7787
7788 static Lisp_Object
7789 unwind_with_echo_area_buffer (vector)
7790 Lisp_Object vector;
7791 {
7792 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7793 Vdeactivate_mark = AREF (vector, 1);
7794 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7795
7796 if (WINDOWP (AREF (vector, 3)))
7797 {
7798 struct window *w;
7799 Lisp_Object buffer, charpos, bytepos;
7800
7801 w = XWINDOW (AREF (vector, 3));
7802 buffer = AREF (vector, 4);
7803 charpos = AREF (vector, 5);
7804 bytepos = AREF (vector, 6);
7805
7806 w->buffer = buffer;
7807 set_marker_both (w->pointm, buffer,
7808 XFASTINT (charpos), XFASTINT (bytepos));
7809 }
7810
7811 Vwith_echo_area_save_vector = vector;
7812 return Qnil;
7813 }
7814
7815
7816 /* Set up the echo area for use by print functions. MULTIBYTE_P
7817 non-zero means we will print multibyte. */
7818
7819 void
7820 setup_echo_area_for_printing (multibyte_p)
7821 int multibyte_p;
7822 {
7823 /* If we can't find an echo area any more, exit. */
7824 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7825 Fkill_emacs (Qnil);
7826
7827 ensure_echo_area_buffers ();
7828
7829 if (!message_buf_print)
7830 {
7831 /* A message has been output since the last time we printed.
7832 Choose a fresh echo area buffer. */
7833 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7834 echo_area_buffer[0] = echo_buffer[1];
7835 else
7836 echo_area_buffer[0] = echo_buffer[0];
7837
7838 /* Switch to that buffer and clear it. */
7839 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7840 current_buffer->truncate_lines = Qnil;
7841
7842 if (Z > BEG)
7843 {
7844 int count = SPECPDL_INDEX ();
7845 specbind (Qinhibit_read_only, Qt);
7846 /* Note that undo recording is always disabled. */
7847 del_range (BEG, Z);
7848 unbind_to (count, Qnil);
7849 }
7850 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7851
7852 /* Set up the buffer for the multibyteness we need. */
7853 if (multibyte_p
7854 != !NILP (current_buffer->enable_multibyte_characters))
7855 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7856
7857 /* Raise the frame containing the echo area. */
7858 if (minibuffer_auto_raise)
7859 {
7860 struct frame *sf = SELECTED_FRAME ();
7861 Lisp_Object mini_window;
7862 mini_window = FRAME_MINIBUF_WINDOW (sf);
7863 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7864 }
7865
7866 message_log_maybe_newline ();
7867 message_buf_print = 1;
7868 }
7869 else
7870 {
7871 if (NILP (echo_area_buffer[0]))
7872 {
7873 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7874 echo_area_buffer[0] = echo_buffer[1];
7875 else
7876 echo_area_buffer[0] = echo_buffer[0];
7877 }
7878
7879 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7880 {
7881 /* Someone switched buffers between print requests. */
7882 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7883 current_buffer->truncate_lines = Qnil;
7884 }
7885 }
7886 }
7887
7888
7889 /* Display an echo area message in window W. Value is non-zero if W's
7890 height is changed. If display_last_displayed_message_p is
7891 non-zero, display the message that was last displayed, otherwise
7892 display the current message. */
7893
7894 static int
7895 display_echo_area (w)
7896 struct window *w;
7897 {
7898 int i, no_message_p, window_height_changed_p, count;
7899
7900 /* Temporarily disable garbage collections while displaying the echo
7901 area. This is done because a GC can print a message itself.
7902 That message would modify the echo area buffer's contents while a
7903 redisplay of the buffer is going on, and seriously confuse
7904 redisplay. */
7905 count = inhibit_garbage_collection ();
7906
7907 /* If there is no message, we must call display_echo_area_1
7908 nevertheless because it resizes the window. But we will have to
7909 reset the echo_area_buffer in question to nil at the end because
7910 with_echo_area_buffer will sets it to an empty buffer. */
7911 i = display_last_displayed_message_p ? 1 : 0;
7912 no_message_p = NILP (echo_area_buffer[i]);
7913
7914 window_height_changed_p
7915 = with_echo_area_buffer (w, display_last_displayed_message_p,
7916 display_echo_area_1,
7917 (EMACS_INT) w, Qnil, 0, 0);
7918
7919 if (no_message_p)
7920 echo_area_buffer[i] = Qnil;
7921
7922 unbind_to (count, Qnil);
7923 return window_height_changed_p;
7924 }
7925
7926
7927 /* Helper for display_echo_area. Display the current buffer which
7928 contains the current echo area message in window W, a mini-window,
7929 a pointer to which is passed in A1. A2..A4 are currently not used.
7930 Change the height of W so that all of the message is displayed.
7931 Value is non-zero if height of W was changed. */
7932
7933 static int
7934 display_echo_area_1 (a1, a2, a3, a4)
7935 EMACS_INT a1;
7936 Lisp_Object a2;
7937 EMACS_INT a3, a4;
7938 {
7939 struct window *w = (struct window *) a1;
7940 Lisp_Object window;
7941 struct text_pos start;
7942 int window_height_changed_p = 0;
7943
7944 /* Do this before displaying, so that we have a large enough glyph
7945 matrix for the display. If we can't get enough space for the
7946 whole text, display the last N lines. That works by setting w->start. */
7947 window_height_changed_p = resize_mini_window (w, 0);
7948
7949 /* Use the starting position chosen by resize_mini_window. */
7950 SET_TEXT_POS_FROM_MARKER (start, w->start);
7951
7952 /* Display. */
7953 clear_glyph_matrix (w->desired_matrix);
7954 XSETWINDOW (window, w);
7955 try_window (window, start, 0);
7956
7957 return window_height_changed_p;
7958 }
7959
7960
7961 /* Resize the echo area window to exactly the size needed for the
7962 currently displayed message, if there is one. If a mini-buffer
7963 is active, don't shrink it. */
7964
7965 void
7966 resize_echo_area_exactly ()
7967 {
7968 if (BUFFERP (echo_area_buffer[0])
7969 && WINDOWP (echo_area_window))
7970 {
7971 struct window *w = XWINDOW (echo_area_window);
7972 int resized_p;
7973 Lisp_Object resize_exactly;
7974
7975 if (minibuf_level == 0)
7976 resize_exactly = Qt;
7977 else
7978 resize_exactly = Qnil;
7979
7980 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7981 (EMACS_INT) w, resize_exactly, 0, 0);
7982 if (resized_p)
7983 {
7984 ++windows_or_buffers_changed;
7985 ++update_mode_lines;
7986 redisplay_internal (0);
7987 }
7988 }
7989 }
7990
7991
7992 /* Callback function for with_echo_area_buffer, when used from
7993 resize_echo_area_exactly. A1 contains a pointer to the window to
7994 resize, EXACTLY non-nil means resize the mini-window exactly to the
7995 size of the text displayed. A3 and A4 are not used. Value is what
7996 resize_mini_window returns. */
7997
7998 static int
7999 resize_mini_window_1 (a1, exactly, a3, a4)
8000 EMACS_INT a1;
8001 Lisp_Object exactly;
8002 EMACS_INT a3, a4;
8003 {
8004 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8005 }
8006
8007
8008 /* Resize mini-window W to fit the size of its contents. EXACT:P
8009 means size the window exactly to the size needed. Otherwise, it's
8010 only enlarged until W's buffer is empty.
8011
8012 Set W->start to the right place to begin display. If the whole
8013 contents fit, start at the beginning. Otherwise, start so as
8014 to make the end of the contents appear. This is particularly
8015 important for y-or-n-p, but seems desirable generally.
8016
8017 Value is non-zero if the window height has been changed. */
8018
8019 int
8020 resize_mini_window (w, exact_p)
8021 struct window *w;
8022 int exact_p;
8023 {
8024 struct frame *f = XFRAME (w->frame);
8025 int window_height_changed_p = 0;
8026
8027 xassert (MINI_WINDOW_P (w));
8028
8029 /* By default, start display at the beginning. */
8030 set_marker_both (w->start, w->buffer,
8031 BUF_BEGV (XBUFFER (w->buffer)),
8032 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8033
8034 /* Don't resize windows while redisplaying a window; it would
8035 confuse redisplay functions when the size of the window they are
8036 displaying changes from under them. Such a resizing can happen,
8037 for instance, when which-func prints a long message while
8038 we are running fontification-functions. We're running these
8039 functions with safe_call which binds inhibit-redisplay to t. */
8040 if (!NILP (Vinhibit_redisplay))
8041 return 0;
8042
8043 /* Nil means don't try to resize. */
8044 if (NILP (Vresize_mini_windows)
8045 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8046 return 0;
8047
8048 if (!FRAME_MINIBUF_ONLY_P (f))
8049 {
8050 struct it it;
8051 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8052 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8053 int height, max_height;
8054 int unit = FRAME_LINE_HEIGHT (f);
8055 struct text_pos start;
8056 struct buffer *old_current_buffer = NULL;
8057
8058 if (current_buffer != XBUFFER (w->buffer))
8059 {
8060 old_current_buffer = current_buffer;
8061 set_buffer_internal (XBUFFER (w->buffer));
8062 }
8063
8064 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8065
8066 /* Compute the max. number of lines specified by the user. */
8067 if (FLOATP (Vmax_mini_window_height))
8068 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8069 else if (INTEGERP (Vmax_mini_window_height))
8070 max_height = XINT (Vmax_mini_window_height);
8071 else
8072 max_height = total_height / 4;
8073
8074 /* Correct that max. height if it's bogus. */
8075 max_height = max (1, max_height);
8076 max_height = min (total_height, max_height);
8077
8078 /* Find out the height of the text in the window. */
8079 if (it.truncate_lines_p)
8080 height = 1;
8081 else
8082 {
8083 last_height = 0;
8084 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8085 if (it.max_ascent == 0 && it.max_descent == 0)
8086 height = it.current_y + last_height;
8087 else
8088 height = it.current_y + it.max_ascent + it.max_descent;
8089 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8090 height = (height + unit - 1) / unit;
8091 }
8092
8093 /* Compute a suitable window start. */
8094 if (height > max_height)
8095 {
8096 height = max_height;
8097 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8098 move_it_vertically_backward (&it, (height - 1) * unit);
8099 start = it.current.pos;
8100 }
8101 else
8102 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8103 SET_MARKER_FROM_TEXT_POS (w->start, start);
8104
8105 if (EQ (Vresize_mini_windows, Qgrow_only))
8106 {
8107 /* Let it grow only, until we display an empty message, in which
8108 case the window shrinks again. */
8109 if (height > WINDOW_TOTAL_LINES (w))
8110 {
8111 int old_height = WINDOW_TOTAL_LINES (w);
8112 freeze_window_starts (f, 1);
8113 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8114 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8115 }
8116 else if (height < WINDOW_TOTAL_LINES (w)
8117 && (exact_p || BEGV == ZV))
8118 {
8119 int old_height = WINDOW_TOTAL_LINES (w);
8120 freeze_window_starts (f, 0);
8121 shrink_mini_window (w);
8122 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8123 }
8124 }
8125 else
8126 {
8127 /* Always resize to exact size needed. */
8128 if (height > WINDOW_TOTAL_LINES (w))
8129 {
8130 int old_height = WINDOW_TOTAL_LINES (w);
8131 freeze_window_starts (f, 1);
8132 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8133 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8134 }
8135 else if (height < WINDOW_TOTAL_LINES (w))
8136 {
8137 int old_height = WINDOW_TOTAL_LINES (w);
8138 freeze_window_starts (f, 0);
8139 shrink_mini_window (w);
8140
8141 if (height)
8142 {
8143 freeze_window_starts (f, 1);
8144 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8145 }
8146
8147 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8148 }
8149 }
8150
8151 if (old_current_buffer)
8152 set_buffer_internal (old_current_buffer);
8153 }
8154
8155 return window_height_changed_p;
8156 }
8157
8158
8159 /* Value is the current message, a string, or nil if there is no
8160 current message. */
8161
8162 Lisp_Object
8163 current_message ()
8164 {
8165 Lisp_Object msg;
8166
8167 if (NILP (echo_area_buffer[0]))
8168 msg = Qnil;
8169 else
8170 {
8171 with_echo_area_buffer (0, 0, current_message_1,
8172 (EMACS_INT) &msg, Qnil, 0, 0);
8173 if (NILP (msg))
8174 echo_area_buffer[0] = Qnil;
8175 }
8176
8177 return msg;
8178 }
8179
8180
8181 static int
8182 current_message_1 (a1, a2, a3, a4)
8183 EMACS_INT a1;
8184 Lisp_Object a2;
8185 EMACS_INT a3, a4;
8186 {
8187 Lisp_Object *msg = (Lisp_Object *) a1;
8188
8189 if (Z > BEG)
8190 *msg = make_buffer_string (BEG, Z, 1);
8191 else
8192 *msg = Qnil;
8193 return 0;
8194 }
8195
8196
8197 /* Push the current message on Vmessage_stack for later restauration
8198 by restore_message. Value is non-zero if the current message isn't
8199 empty. This is a relatively infrequent operation, so it's not
8200 worth optimizing. */
8201
8202 int
8203 push_message ()
8204 {
8205 Lisp_Object msg;
8206 msg = current_message ();
8207 Vmessage_stack = Fcons (msg, Vmessage_stack);
8208 return STRINGP (msg);
8209 }
8210
8211
8212 /* Restore message display from the top of Vmessage_stack. */
8213
8214 void
8215 restore_message ()
8216 {
8217 Lisp_Object msg;
8218
8219 xassert (CONSP (Vmessage_stack));
8220 msg = XCAR (Vmessage_stack);
8221 if (STRINGP (msg))
8222 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8223 else
8224 message3_nolog (msg, 0, 0);
8225 }
8226
8227
8228 /* Handler for record_unwind_protect calling pop_message. */
8229
8230 Lisp_Object
8231 pop_message_unwind (dummy)
8232 Lisp_Object dummy;
8233 {
8234 pop_message ();
8235 return Qnil;
8236 }
8237
8238 /* Pop the top-most entry off Vmessage_stack. */
8239
8240 void
8241 pop_message ()
8242 {
8243 xassert (CONSP (Vmessage_stack));
8244 Vmessage_stack = XCDR (Vmessage_stack);
8245 }
8246
8247
8248 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8249 exits. If the stack is not empty, we have a missing pop_message
8250 somewhere. */
8251
8252 void
8253 check_message_stack ()
8254 {
8255 if (!NILP (Vmessage_stack))
8256 abort ();
8257 }
8258
8259
8260 /* Truncate to NCHARS what will be displayed in the echo area the next
8261 time we display it---but don't redisplay it now. */
8262
8263 void
8264 truncate_echo_area (nchars)
8265 int nchars;
8266 {
8267 if (nchars == 0)
8268 echo_area_buffer[0] = Qnil;
8269 /* A null message buffer means that the frame hasn't really been
8270 initialized yet. Error messages get reported properly by
8271 cmd_error, so this must be just an informative message; toss it. */
8272 else if (!noninteractive
8273 && INTERACTIVE
8274 && !NILP (echo_area_buffer[0]))
8275 {
8276 struct frame *sf = SELECTED_FRAME ();
8277 if (FRAME_MESSAGE_BUF (sf))
8278 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8279 }
8280 }
8281
8282
8283 /* Helper function for truncate_echo_area. Truncate the current
8284 message to at most NCHARS characters. */
8285
8286 static int
8287 truncate_message_1 (nchars, a2, a3, a4)
8288 EMACS_INT nchars;
8289 Lisp_Object a2;
8290 EMACS_INT a3, a4;
8291 {
8292 if (BEG + nchars < Z)
8293 del_range (BEG + nchars, Z);
8294 if (Z == BEG)
8295 echo_area_buffer[0] = Qnil;
8296 return 0;
8297 }
8298
8299
8300 /* Set the current message to a substring of S or STRING.
8301
8302 If STRING is a Lisp string, set the message to the first NBYTES
8303 bytes from STRING. NBYTES zero means use the whole string. If
8304 STRING is multibyte, the message will be displayed multibyte.
8305
8306 If S is not null, set the message to the first LEN bytes of S. LEN
8307 zero means use the whole string. MULTIBYTE_P non-zero means S is
8308 multibyte. Display the message multibyte in that case.
8309
8310 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8311 to t before calling set_message_1 (which calls insert).
8312 */
8313
8314 void
8315 set_message (s, string, nbytes, multibyte_p)
8316 const char *s;
8317 Lisp_Object string;
8318 int nbytes, multibyte_p;
8319 {
8320 message_enable_multibyte
8321 = ((s && multibyte_p)
8322 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8323
8324 with_echo_area_buffer (0, 0, set_message_1,
8325 (EMACS_INT) s, string, nbytes, multibyte_p);
8326 message_buf_print = 0;
8327 help_echo_showing_p = 0;
8328 }
8329
8330
8331 /* Helper function for set_message. Arguments have the same meaning
8332 as there, with A1 corresponding to S and A2 corresponding to STRING
8333 This function is called with the echo area buffer being
8334 current. */
8335
8336 static int
8337 set_message_1 (a1, a2, nbytes, multibyte_p)
8338 EMACS_INT a1;
8339 Lisp_Object a2;
8340 EMACS_INT nbytes, multibyte_p;
8341 {
8342 const char *s = (const char *) a1;
8343 Lisp_Object string = a2;
8344
8345 /* Change multibyteness of the echo buffer appropriately. */
8346 if (message_enable_multibyte
8347 != !NILP (current_buffer->enable_multibyte_characters))
8348 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8349
8350 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8351
8352 /* Insert new message at BEG. */
8353 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8354 Ferase_buffer ();
8355
8356 if (STRINGP (string))
8357 {
8358 int nchars;
8359
8360 if (nbytes == 0)
8361 nbytes = SBYTES (string);
8362 nchars = string_byte_to_char (string, nbytes);
8363
8364 /* This function takes care of single/multibyte conversion. We
8365 just have to ensure that the echo area buffer has the right
8366 setting of enable_multibyte_characters. */
8367 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8368 }
8369 else if (s)
8370 {
8371 if (nbytes == 0)
8372 nbytes = strlen (s);
8373
8374 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8375 {
8376 /* Convert from multi-byte to single-byte. */
8377 int i, c, n;
8378 unsigned char work[1];
8379
8380 /* Convert a multibyte string to single-byte. */
8381 for (i = 0; i < nbytes; i += n)
8382 {
8383 c = string_char_and_length (s + i, nbytes - i, &n);
8384 work[0] = (SINGLE_BYTE_CHAR_P (c)
8385 ? c
8386 : multibyte_char_to_unibyte (c, Qnil));
8387 insert_1_both (work, 1, 1, 1, 0, 0);
8388 }
8389 }
8390 else if (!multibyte_p
8391 && !NILP (current_buffer->enable_multibyte_characters))
8392 {
8393 /* Convert from single-byte to multi-byte. */
8394 int i, c, n;
8395 const unsigned char *msg = (const unsigned char *) s;
8396 unsigned char str[MAX_MULTIBYTE_LENGTH];
8397
8398 /* Convert a single-byte string to multibyte. */
8399 for (i = 0; i < nbytes; i++)
8400 {
8401 c = unibyte_char_to_multibyte (msg[i]);
8402 n = CHAR_STRING (c, str);
8403 insert_1_both (str, 1, n, 1, 0, 0);
8404 }
8405 }
8406 else
8407 insert_1 (s, nbytes, 1, 0, 0);
8408 }
8409
8410 return 0;
8411 }
8412
8413
8414 /* Clear messages. CURRENT_P non-zero means clear the current
8415 message. LAST_DISPLAYED_P non-zero means clear the message
8416 last displayed. */
8417
8418 void
8419 clear_message (current_p, last_displayed_p)
8420 int current_p, last_displayed_p;
8421 {
8422 if (current_p)
8423 {
8424 echo_area_buffer[0] = Qnil;
8425 message_cleared_p = 1;
8426 }
8427
8428 if (last_displayed_p)
8429 echo_area_buffer[1] = Qnil;
8430
8431 message_buf_print = 0;
8432 }
8433
8434 /* Clear garbaged frames.
8435
8436 This function is used where the old redisplay called
8437 redraw_garbaged_frames which in turn called redraw_frame which in
8438 turn called clear_frame. The call to clear_frame was a source of
8439 flickering. I believe a clear_frame is not necessary. It should
8440 suffice in the new redisplay to invalidate all current matrices,
8441 and ensure a complete redisplay of all windows. */
8442
8443 static void
8444 clear_garbaged_frames ()
8445 {
8446 if (frame_garbaged)
8447 {
8448 Lisp_Object tail, frame;
8449 int changed_count = 0;
8450
8451 FOR_EACH_FRAME (tail, frame)
8452 {
8453 struct frame *f = XFRAME (frame);
8454
8455 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8456 {
8457 if (f->resized_p)
8458 {
8459 Fredraw_frame (frame);
8460 f->force_flush_display_p = 1;
8461 }
8462 clear_current_matrices (f);
8463 changed_count++;
8464 f->garbaged = 0;
8465 f->resized_p = 0;
8466 }
8467 }
8468
8469 frame_garbaged = 0;
8470 if (changed_count)
8471 ++windows_or_buffers_changed;
8472 }
8473 }
8474
8475
8476 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8477 is non-zero update selected_frame. Value is non-zero if the
8478 mini-windows height has been changed. */
8479
8480 static int
8481 echo_area_display (update_frame_p)
8482 int update_frame_p;
8483 {
8484 Lisp_Object mini_window;
8485 struct window *w;
8486 struct frame *f;
8487 int window_height_changed_p = 0;
8488 struct frame *sf = SELECTED_FRAME ();
8489
8490 mini_window = FRAME_MINIBUF_WINDOW (sf);
8491 w = XWINDOW (mini_window);
8492 f = XFRAME (WINDOW_FRAME (w));
8493
8494 /* Don't display if frame is invisible or not yet initialized. */
8495 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8496 return 0;
8497
8498 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8499 #ifndef MAC_OS8
8500 #ifdef HAVE_WINDOW_SYSTEM
8501 /* When Emacs starts, selected_frame may be a visible terminal
8502 frame, even if we run under a window system. If we let this
8503 through, a message would be displayed on the terminal. */
8504 if (EQ (selected_frame, Vterminal_frame)
8505 && !NILP (Vwindow_system))
8506 return 0;
8507 #endif /* HAVE_WINDOW_SYSTEM */
8508 #endif
8509
8510 /* Redraw garbaged frames. */
8511 if (frame_garbaged)
8512 clear_garbaged_frames ();
8513
8514 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8515 {
8516 echo_area_window = mini_window;
8517 window_height_changed_p = display_echo_area (w);
8518 w->must_be_updated_p = 1;
8519
8520 /* Update the display, unless called from redisplay_internal.
8521 Also don't update the screen during redisplay itself. The
8522 update will happen at the end of redisplay, and an update
8523 here could cause confusion. */
8524 if (update_frame_p && !redisplaying_p)
8525 {
8526 int n = 0;
8527
8528 /* If the display update has been interrupted by pending
8529 input, update mode lines in the frame. Due to the
8530 pending input, it might have been that redisplay hasn't
8531 been called, so that mode lines above the echo area are
8532 garbaged. This looks odd, so we prevent it here. */
8533 if (!display_completed)
8534 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8535
8536 if (window_height_changed_p
8537 /* Don't do this if Emacs is shutting down. Redisplay
8538 needs to run hooks. */
8539 && !NILP (Vrun_hooks))
8540 {
8541 /* Must update other windows. Likewise as in other
8542 cases, don't let this update be interrupted by
8543 pending input. */
8544 int count = SPECPDL_INDEX ();
8545 specbind (Qredisplay_dont_pause, Qt);
8546 windows_or_buffers_changed = 1;
8547 redisplay_internal (0);
8548 unbind_to (count, Qnil);
8549 }
8550 else if (FRAME_WINDOW_P (f) && n == 0)
8551 {
8552 /* Window configuration is the same as before.
8553 Can do with a display update of the echo area,
8554 unless we displayed some mode lines. */
8555 update_single_window (w, 1);
8556 rif->flush_display (f);
8557 }
8558 else
8559 update_frame (f, 1, 1);
8560
8561 /* If cursor is in the echo area, make sure that the next
8562 redisplay displays the minibuffer, so that the cursor will
8563 be replaced with what the minibuffer wants. */
8564 if (cursor_in_echo_area)
8565 ++windows_or_buffers_changed;
8566 }
8567 }
8568 else if (!EQ (mini_window, selected_window))
8569 windows_or_buffers_changed++;
8570
8571 /* The current message is now also the last one displayed. */
8572 echo_area_buffer[1] = echo_area_buffer[0];
8573
8574 /* Prevent redisplay optimization in redisplay_internal by resetting
8575 this_line_start_pos. This is done because the mini-buffer now
8576 displays the message instead of its buffer text. */
8577 if (EQ (mini_window, selected_window))
8578 CHARPOS (this_line_start_pos) = 0;
8579
8580 return window_height_changed_p;
8581 }
8582
8583
8584 \f
8585 /***********************************************************************
8586 Mode Lines and Frame Titles
8587 ***********************************************************************/
8588
8589 /* A buffer for constructing non-propertized mode-line strings and
8590 frame titles in it; allocated from the heap in init_xdisp and
8591 resized as needed in store_mode_line_noprop_char. */
8592
8593 static char *mode_line_noprop_buf;
8594
8595 /* The buffer's end, and a current output position in it. */
8596
8597 static char *mode_line_noprop_buf_end;
8598 static char *mode_line_noprop_ptr;
8599
8600 #define MODE_LINE_NOPROP_LEN(start) \
8601 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8602
8603 static enum {
8604 MODE_LINE_DISPLAY = 0,
8605 MODE_LINE_TITLE,
8606 MODE_LINE_NOPROP,
8607 MODE_LINE_STRING
8608 } mode_line_target;
8609
8610 /* Alist that caches the results of :propertize.
8611 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8612 static Lisp_Object mode_line_proptrans_alist;
8613
8614 /* List of strings making up the mode-line. */
8615 static Lisp_Object mode_line_string_list;
8616
8617 /* Base face property when building propertized mode line string. */
8618 static Lisp_Object mode_line_string_face;
8619 static Lisp_Object mode_line_string_face_prop;
8620
8621
8622 /* Unwind data for mode line strings */
8623
8624 static Lisp_Object Vmode_line_unwind_vector;
8625
8626 static Lisp_Object
8627 format_mode_line_unwind_data (obuf, save_proptrans)
8628 struct buffer *obuf;
8629 {
8630 Lisp_Object vector;
8631
8632 /* Reduce consing by keeping one vector in
8633 Vwith_echo_area_save_vector. */
8634 vector = Vmode_line_unwind_vector;
8635 Vmode_line_unwind_vector = Qnil;
8636
8637 if (NILP (vector))
8638 vector = Fmake_vector (make_number (7), Qnil);
8639
8640 AREF (vector, 0) = make_number (mode_line_target);
8641 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8642 AREF (vector, 2) = mode_line_string_list;
8643 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8644 AREF (vector, 4) = mode_line_string_face;
8645 AREF (vector, 5) = mode_line_string_face_prop;
8646
8647 if (obuf)
8648 XSETBUFFER (AREF (vector, 6), obuf);
8649 else
8650 AREF (vector, 6) = Qnil;
8651
8652 return vector;
8653 }
8654
8655 static Lisp_Object
8656 unwind_format_mode_line (vector)
8657 Lisp_Object vector;
8658 {
8659 mode_line_target = XINT (AREF (vector, 0));
8660 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8661 mode_line_string_list = AREF (vector, 2);
8662 if (! EQ (AREF (vector, 3), Qt))
8663 mode_line_proptrans_alist = AREF (vector, 3);
8664 mode_line_string_face = AREF (vector, 4);
8665 mode_line_string_face_prop = AREF (vector, 5);
8666
8667 if (!NILP (AREF (vector, 6)))
8668 {
8669 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8670 AREF (vector, 6) = Qnil;
8671 }
8672
8673 Vmode_line_unwind_vector = vector;
8674 return Qnil;
8675 }
8676
8677
8678 /* Store a single character C for the frame title in mode_line_noprop_buf.
8679 Re-allocate mode_line_noprop_buf if necessary. */
8680
8681 static void
8682 #ifdef PROTOTYPES
8683 store_mode_line_noprop_char (char c)
8684 #else
8685 store_mode_line_noprop_char (c)
8686 char c;
8687 #endif
8688 {
8689 /* If output position has reached the end of the allocated buffer,
8690 double the buffer's size. */
8691 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8692 {
8693 int len = MODE_LINE_NOPROP_LEN (0);
8694 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8695 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8696 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8697 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8698 }
8699
8700 *mode_line_noprop_ptr++ = c;
8701 }
8702
8703
8704 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8705 mode_line_noprop_ptr. STR is the string to store. Do not copy
8706 characters that yield more columns than PRECISION; PRECISION <= 0
8707 means copy the whole string. Pad with spaces until FIELD_WIDTH
8708 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8709 pad. Called from display_mode_element when it is used to build a
8710 frame title. */
8711
8712 static int
8713 store_mode_line_noprop (str, field_width, precision)
8714 const unsigned char *str;
8715 int field_width, precision;
8716 {
8717 int n = 0;
8718 int dummy, nbytes;
8719
8720 /* Copy at most PRECISION chars from STR. */
8721 nbytes = strlen (str);
8722 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8723 while (nbytes--)
8724 store_mode_line_noprop_char (*str++);
8725
8726 /* Fill up with spaces until FIELD_WIDTH reached. */
8727 while (field_width > 0
8728 && n < field_width)
8729 {
8730 store_mode_line_noprop_char (' ');
8731 ++n;
8732 }
8733
8734 return n;
8735 }
8736
8737 /***********************************************************************
8738 Frame Titles
8739 ***********************************************************************/
8740
8741 #ifdef HAVE_WINDOW_SYSTEM
8742
8743 /* Set the title of FRAME, if it has changed. The title format is
8744 Vicon_title_format if FRAME is iconified, otherwise it is
8745 frame_title_format. */
8746
8747 static void
8748 x_consider_frame_title (frame)
8749 Lisp_Object frame;
8750 {
8751 struct frame *f = XFRAME (frame);
8752
8753 if (FRAME_WINDOW_P (f)
8754 || FRAME_MINIBUF_ONLY_P (f)
8755 || f->explicit_name)
8756 {
8757 /* Do we have more than one visible frame on this X display? */
8758 Lisp_Object tail;
8759 Lisp_Object fmt;
8760 int title_start;
8761 char *title;
8762 int len;
8763 struct it it;
8764 int count = SPECPDL_INDEX ();
8765
8766 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8767 {
8768 Lisp_Object other_frame = XCAR (tail);
8769 struct frame *tf = XFRAME (other_frame);
8770
8771 if (tf != f
8772 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8773 && !FRAME_MINIBUF_ONLY_P (tf)
8774 && !EQ (other_frame, tip_frame)
8775 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8776 break;
8777 }
8778
8779 /* Set global variable indicating that multiple frames exist. */
8780 multiple_frames = CONSP (tail);
8781
8782 /* Switch to the buffer of selected window of the frame. Set up
8783 mode_line_target so that display_mode_element will output into
8784 mode_line_noprop_buf; then display the title. */
8785 record_unwind_protect (unwind_format_mode_line,
8786 format_mode_line_unwind_data (current_buffer, 0));
8787
8788 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8789 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8790
8791 mode_line_target = MODE_LINE_TITLE;
8792 title_start = MODE_LINE_NOPROP_LEN (0);
8793 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8794 NULL, DEFAULT_FACE_ID);
8795 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8796 len = MODE_LINE_NOPROP_LEN (title_start);
8797 title = mode_line_noprop_buf + title_start;
8798 unbind_to (count, Qnil);
8799
8800 /* Set the title only if it's changed. This avoids consing in
8801 the common case where it hasn't. (If it turns out that we've
8802 already wasted too much time by walking through the list with
8803 display_mode_element, then we might need to optimize at a
8804 higher level than this.) */
8805 if (! STRINGP (f->name)
8806 || SBYTES (f->name) != len
8807 || bcmp (title, SDATA (f->name), len) != 0)
8808 x_implicitly_set_name (f, make_string (title, len), Qnil);
8809 }
8810 }
8811
8812 #endif /* not HAVE_WINDOW_SYSTEM */
8813
8814
8815
8816 \f
8817 /***********************************************************************
8818 Menu Bars
8819 ***********************************************************************/
8820
8821
8822 /* Prepare for redisplay by updating menu-bar item lists when
8823 appropriate. This can call eval. */
8824
8825 void
8826 prepare_menu_bars ()
8827 {
8828 int all_windows;
8829 struct gcpro gcpro1, gcpro2;
8830 struct frame *f;
8831 Lisp_Object tooltip_frame;
8832
8833 #ifdef HAVE_WINDOW_SYSTEM
8834 tooltip_frame = tip_frame;
8835 #else
8836 tooltip_frame = Qnil;
8837 #endif
8838
8839 /* Update all frame titles based on their buffer names, etc. We do
8840 this before the menu bars so that the buffer-menu will show the
8841 up-to-date frame titles. */
8842 #ifdef HAVE_WINDOW_SYSTEM
8843 if (windows_or_buffers_changed || update_mode_lines)
8844 {
8845 Lisp_Object tail, frame;
8846
8847 FOR_EACH_FRAME (tail, frame)
8848 {
8849 f = XFRAME (frame);
8850 if (!EQ (frame, tooltip_frame)
8851 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8852 x_consider_frame_title (frame);
8853 }
8854 }
8855 #endif /* HAVE_WINDOW_SYSTEM */
8856
8857 /* Update the menu bar item lists, if appropriate. This has to be
8858 done before any actual redisplay or generation of display lines. */
8859 all_windows = (update_mode_lines
8860 || buffer_shared > 1
8861 || windows_or_buffers_changed);
8862 if (all_windows)
8863 {
8864 Lisp_Object tail, frame;
8865 int count = SPECPDL_INDEX ();
8866
8867 record_unwind_save_match_data ();
8868
8869 FOR_EACH_FRAME (tail, frame)
8870 {
8871 f = XFRAME (frame);
8872
8873 /* Ignore tooltip frame. */
8874 if (EQ (frame, tooltip_frame))
8875 continue;
8876
8877 /* If a window on this frame changed size, report that to
8878 the user and clear the size-change flag. */
8879 if (FRAME_WINDOW_SIZES_CHANGED (f))
8880 {
8881 Lisp_Object functions;
8882
8883 /* Clear flag first in case we get an error below. */
8884 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8885 functions = Vwindow_size_change_functions;
8886 GCPRO2 (tail, functions);
8887
8888 while (CONSP (functions))
8889 {
8890 call1 (XCAR (functions), frame);
8891 functions = XCDR (functions);
8892 }
8893 UNGCPRO;
8894 }
8895
8896 GCPRO1 (tail);
8897 update_menu_bar (f, 0);
8898 #ifdef HAVE_WINDOW_SYSTEM
8899 update_tool_bar (f, 0);
8900 #endif
8901 UNGCPRO;
8902 }
8903
8904 unbind_to (count, Qnil);
8905 }
8906 else
8907 {
8908 struct frame *sf = SELECTED_FRAME ();
8909 update_menu_bar (sf, 1);
8910 #ifdef HAVE_WINDOW_SYSTEM
8911 update_tool_bar (sf, 1);
8912 #endif
8913 }
8914
8915 /* Motif needs this. See comment in xmenu.c. Turn it off when
8916 pending_menu_activation is not defined. */
8917 #ifdef USE_X_TOOLKIT
8918 pending_menu_activation = 0;
8919 #endif
8920 }
8921
8922
8923 /* Update the menu bar item list for frame F. This has to be done
8924 before we start to fill in any display lines, because it can call
8925 eval.
8926
8927 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8928
8929 static void
8930 update_menu_bar (f, save_match_data)
8931 struct frame *f;
8932 int save_match_data;
8933 {
8934 Lisp_Object window;
8935 register struct window *w;
8936
8937 /* If called recursively during a menu update, do nothing. This can
8938 happen when, for instance, an activate-menubar-hook causes a
8939 redisplay. */
8940 if (inhibit_menubar_update)
8941 return;
8942
8943 window = FRAME_SELECTED_WINDOW (f);
8944 w = XWINDOW (window);
8945
8946 #if 0 /* The if statement below this if statement used to include the
8947 condition !NILP (w->update_mode_line), rather than using
8948 update_mode_lines directly, and this if statement may have
8949 been added to make that condition work. Now the if
8950 statement below matches its comment, this isn't needed. */
8951 if (update_mode_lines)
8952 w->update_mode_line = Qt;
8953 #endif
8954
8955 if (FRAME_WINDOW_P (f)
8956 ?
8957 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8958 || defined (USE_GTK)
8959 FRAME_EXTERNAL_MENU_BAR (f)
8960 #else
8961 FRAME_MENU_BAR_LINES (f) > 0
8962 #endif
8963 : FRAME_MENU_BAR_LINES (f) > 0)
8964 {
8965 /* If the user has switched buffers or windows, we need to
8966 recompute to reflect the new bindings. But we'll
8967 recompute when update_mode_lines is set too; that means
8968 that people can use force-mode-line-update to request
8969 that the menu bar be recomputed. The adverse effect on
8970 the rest of the redisplay algorithm is about the same as
8971 windows_or_buffers_changed anyway. */
8972 if (windows_or_buffers_changed
8973 /* This used to test w->update_mode_line, but we believe
8974 there is no need to recompute the menu in that case. */
8975 || update_mode_lines
8976 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8977 < BUF_MODIFF (XBUFFER (w->buffer)))
8978 != !NILP (w->last_had_star))
8979 || ((!NILP (Vtransient_mark_mode)
8980 && !NILP (XBUFFER (w->buffer)->mark_active))
8981 != !NILP (w->region_showing)))
8982 {
8983 struct buffer *prev = current_buffer;
8984 int count = SPECPDL_INDEX ();
8985
8986 specbind (Qinhibit_menubar_update, Qt);
8987
8988 set_buffer_internal_1 (XBUFFER (w->buffer));
8989 if (save_match_data)
8990 record_unwind_save_match_data ();
8991 if (NILP (Voverriding_local_map_menu_flag))
8992 {
8993 specbind (Qoverriding_terminal_local_map, Qnil);
8994 specbind (Qoverriding_local_map, Qnil);
8995 }
8996
8997 /* Run the Lucid hook. */
8998 safe_run_hooks (Qactivate_menubar_hook);
8999
9000 /* If it has changed current-menubar from previous value,
9001 really recompute the menu-bar from the value. */
9002 if (! NILP (Vlucid_menu_bar_dirty_flag))
9003 call0 (Qrecompute_lucid_menubar);
9004
9005 safe_run_hooks (Qmenu_bar_update_hook);
9006 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9007
9008 /* Redisplay the menu bar in case we changed it. */
9009 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9010 || defined (USE_GTK)
9011 if (FRAME_WINDOW_P (f)
9012 #if defined (MAC_OS)
9013 /* All frames on Mac OS share the same menubar. So only the
9014 selected frame should be allowed to set it. */
9015 && f == SELECTED_FRAME ()
9016 #endif
9017 )
9018 set_frame_menubar (f, 0, 0);
9019 else
9020 /* On a terminal screen, the menu bar is an ordinary screen
9021 line, and this makes it get updated. */
9022 w->update_mode_line = Qt;
9023 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9024 /* In the non-toolkit version, the menu bar is an ordinary screen
9025 line, and this makes it get updated. */
9026 w->update_mode_line = Qt;
9027 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9028
9029 unbind_to (count, Qnil);
9030 set_buffer_internal_1 (prev);
9031 }
9032 }
9033 }
9034
9035
9036 \f
9037 /***********************************************************************
9038 Output Cursor
9039 ***********************************************************************/
9040
9041 #ifdef HAVE_WINDOW_SYSTEM
9042
9043 /* EXPORT:
9044 Nominal cursor position -- where to draw output.
9045 HPOS and VPOS are window relative glyph matrix coordinates.
9046 X and Y are window relative pixel coordinates. */
9047
9048 struct cursor_pos output_cursor;
9049
9050
9051 /* EXPORT:
9052 Set the global variable output_cursor to CURSOR. All cursor
9053 positions are relative to updated_window. */
9054
9055 void
9056 set_output_cursor (cursor)
9057 struct cursor_pos *cursor;
9058 {
9059 output_cursor.hpos = cursor->hpos;
9060 output_cursor.vpos = cursor->vpos;
9061 output_cursor.x = cursor->x;
9062 output_cursor.y = cursor->y;
9063 }
9064
9065
9066 /* EXPORT for RIF:
9067 Set a nominal cursor position.
9068
9069 HPOS and VPOS are column/row positions in a window glyph matrix. X
9070 and Y are window text area relative pixel positions.
9071
9072 If this is done during an update, updated_window will contain the
9073 window that is being updated and the position is the future output
9074 cursor position for that window. If updated_window is null, use
9075 selected_window and display the cursor at the given position. */
9076
9077 void
9078 x_cursor_to (vpos, hpos, y, x)
9079 int vpos, hpos, y, x;
9080 {
9081 struct window *w;
9082
9083 /* If updated_window is not set, work on selected_window. */
9084 if (updated_window)
9085 w = updated_window;
9086 else
9087 w = XWINDOW (selected_window);
9088
9089 /* Set the output cursor. */
9090 output_cursor.hpos = hpos;
9091 output_cursor.vpos = vpos;
9092 output_cursor.x = x;
9093 output_cursor.y = y;
9094
9095 /* If not called as part of an update, really display the cursor.
9096 This will also set the cursor position of W. */
9097 if (updated_window == NULL)
9098 {
9099 BLOCK_INPUT;
9100 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9101 if (rif->flush_display_optional)
9102 rif->flush_display_optional (SELECTED_FRAME ());
9103 UNBLOCK_INPUT;
9104 }
9105 }
9106
9107 #endif /* HAVE_WINDOW_SYSTEM */
9108
9109 \f
9110 /***********************************************************************
9111 Tool-bars
9112 ***********************************************************************/
9113
9114 #ifdef HAVE_WINDOW_SYSTEM
9115
9116 /* Where the mouse was last time we reported a mouse event. */
9117
9118 FRAME_PTR last_mouse_frame;
9119
9120 /* Tool-bar item index of the item on which a mouse button was pressed
9121 or -1. */
9122
9123 int last_tool_bar_item;
9124
9125
9126 /* Update the tool-bar item list for frame F. This has to be done
9127 before we start to fill in any display lines. Called from
9128 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9129 and restore it here. */
9130
9131 static void
9132 update_tool_bar (f, save_match_data)
9133 struct frame *f;
9134 int save_match_data;
9135 {
9136 #ifdef USE_GTK
9137 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9138 #else
9139 int do_update = WINDOWP (f->tool_bar_window)
9140 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9141 #endif
9142
9143 if (do_update)
9144 {
9145 Lisp_Object window;
9146 struct window *w;
9147
9148 window = FRAME_SELECTED_WINDOW (f);
9149 w = XWINDOW (window);
9150
9151 /* If the user has switched buffers or windows, we need to
9152 recompute to reflect the new bindings. But we'll
9153 recompute when update_mode_lines is set too; that means
9154 that people can use force-mode-line-update to request
9155 that the menu bar be recomputed. The adverse effect on
9156 the rest of the redisplay algorithm is about the same as
9157 windows_or_buffers_changed anyway. */
9158 if (windows_or_buffers_changed
9159 || !NILP (w->update_mode_line)
9160 || update_mode_lines
9161 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9162 < BUF_MODIFF (XBUFFER (w->buffer)))
9163 != !NILP (w->last_had_star))
9164 || ((!NILP (Vtransient_mark_mode)
9165 && !NILP (XBUFFER (w->buffer)->mark_active))
9166 != !NILP (w->region_showing)))
9167 {
9168 struct buffer *prev = current_buffer;
9169 int count = SPECPDL_INDEX ();
9170 Lisp_Object new_tool_bar;
9171 int new_n_tool_bar;
9172 struct gcpro gcpro1;
9173
9174 /* Set current_buffer to the buffer of the selected
9175 window of the frame, so that we get the right local
9176 keymaps. */
9177 set_buffer_internal_1 (XBUFFER (w->buffer));
9178
9179 /* Save match data, if we must. */
9180 if (save_match_data)
9181 record_unwind_save_match_data ();
9182
9183 /* Make sure that we don't accidentally use bogus keymaps. */
9184 if (NILP (Voverriding_local_map_menu_flag))
9185 {
9186 specbind (Qoverriding_terminal_local_map, Qnil);
9187 specbind (Qoverriding_local_map, Qnil);
9188 }
9189
9190 GCPRO1 (new_tool_bar);
9191
9192 /* Build desired tool-bar items from keymaps. */
9193 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9194 &new_n_tool_bar);
9195
9196 /* Redisplay the tool-bar if we changed it. */
9197 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9198 {
9199 /* Redisplay that happens asynchronously due to an expose event
9200 may access f->tool_bar_items. Make sure we update both
9201 variables within BLOCK_INPUT so no such event interrupts. */
9202 BLOCK_INPUT;
9203 f->tool_bar_items = new_tool_bar;
9204 f->n_tool_bar_items = new_n_tool_bar;
9205 w->update_mode_line = Qt;
9206 UNBLOCK_INPUT;
9207 }
9208
9209 UNGCPRO;
9210
9211 unbind_to (count, Qnil);
9212 set_buffer_internal_1 (prev);
9213 }
9214 }
9215 }
9216
9217
9218 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9219 F's desired tool-bar contents. F->tool_bar_items must have
9220 been set up previously by calling prepare_menu_bars. */
9221
9222 static void
9223 build_desired_tool_bar_string (f)
9224 struct frame *f;
9225 {
9226 int i, size, size_needed;
9227 struct gcpro gcpro1, gcpro2, gcpro3;
9228 Lisp_Object image, plist, props;
9229
9230 image = plist = props = Qnil;
9231 GCPRO3 (image, plist, props);
9232
9233 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9234 Otherwise, make a new string. */
9235
9236 /* The size of the string we might be able to reuse. */
9237 size = (STRINGP (f->desired_tool_bar_string)
9238 ? SCHARS (f->desired_tool_bar_string)
9239 : 0);
9240
9241 /* We need one space in the string for each image. */
9242 size_needed = f->n_tool_bar_items;
9243
9244 /* Reuse f->desired_tool_bar_string, if possible. */
9245 if (size < size_needed || NILP (f->desired_tool_bar_string))
9246 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9247 make_number (' '));
9248 else
9249 {
9250 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9251 Fremove_text_properties (make_number (0), make_number (size),
9252 props, f->desired_tool_bar_string);
9253 }
9254
9255 /* Put a `display' property on the string for the images to display,
9256 put a `menu_item' property on tool-bar items with a value that
9257 is the index of the item in F's tool-bar item vector. */
9258 for (i = 0; i < f->n_tool_bar_items; ++i)
9259 {
9260 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9261
9262 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9263 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9264 int hmargin, vmargin, relief, idx, end;
9265 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9266
9267 /* If image is a vector, choose the image according to the
9268 button state. */
9269 image = PROP (TOOL_BAR_ITEM_IMAGES);
9270 if (VECTORP (image))
9271 {
9272 if (enabled_p)
9273 idx = (selected_p
9274 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9275 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9276 else
9277 idx = (selected_p
9278 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9279 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9280
9281 xassert (ASIZE (image) >= idx);
9282 image = AREF (image, idx);
9283 }
9284 else
9285 idx = -1;
9286
9287 /* Ignore invalid image specifications. */
9288 if (!valid_image_p (image))
9289 continue;
9290
9291 /* Display the tool-bar button pressed, or depressed. */
9292 plist = Fcopy_sequence (XCDR (image));
9293
9294 /* Compute margin and relief to draw. */
9295 relief = (tool_bar_button_relief >= 0
9296 ? tool_bar_button_relief
9297 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9298 hmargin = vmargin = relief;
9299
9300 if (INTEGERP (Vtool_bar_button_margin)
9301 && XINT (Vtool_bar_button_margin) > 0)
9302 {
9303 hmargin += XFASTINT (Vtool_bar_button_margin);
9304 vmargin += XFASTINT (Vtool_bar_button_margin);
9305 }
9306 else if (CONSP (Vtool_bar_button_margin))
9307 {
9308 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9309 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9310 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9311
9312 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9313 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9314 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9315 }
9316
9317 if (auto_raise_tool_bar_buttons_p)
9318 {
9319 /* Add a `:relief' property to the image spec if the item is
9320 selected. */
9321 if (selected_p)
9322 {
9323 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9324 hmargin -= relief;
9325 vmargin -= relief;
9326 }
9327 }
9328 else
9329 {
9330 /* If image is selected, display it pressed, i.e. with a
9331 negative relief. If it's not selected, display it with a
9332 raised relief. */
9333 plist = Fplist_put (plist, QCrelief,
9334 (selected_p
9335 ? make_number (-relief)
9336 : make_number (relief)));
9337 hmargin -= relief;
9338 vmargin -= relief;
9339 }
9340
9341 /* Put a margin around the image. */
9342 if (hmargin || vmargin)
9343 {
9344 if (hmargin == vmargin)
9345 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9346 else
9347 plist = Fplist_put (plist, QCmargin,
9348 Fcons (make_number (hmargin),
9349 make_number (vmargin)));
9350 }
9351
9352 /* If button is not enabled, and we don't have special images
9353 for the disabled state, make the image appear disabled by
9354 applying an appropriate algorithm to it. */
9355 if (!enabled_p && idx < 0)
9356 plist = Fplist_put (plist, QCconversion, Qdisabled);
9357
9358 /* Put a `display' text property on the string for the image to
9359 display. Put a `menu-item' property on the string that gives
9360 the start of this item's properties in the tool-bar items
9361 vector. */
9362 image = Fcons (Qimage, plist);
9363 props = list4 (Qdisplay, image,
9364 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9365
9366 /* Let the last image hide all remaining spaces in the tool bar
9367 string. The string can be longer than needed when we reuse a
9368 previous string. */
9369 if (i + 1 == f->n_tool_bar_items)
9370 end = SCHARS (f->desired_tool_bar_string);
9371 else
9372 end = i + 1;
9373 Fadd_text_properties (make_number (i), make_number (end),
9374 props, f->desired_tool_bar_string);
9375 #undef PROP
9376 }
9377
9378 UNGCPRO;
9379 }
9380
9381
9382 /* Display one line of the tool-bar of frame IT->f. */
9383
9384 static void
9385 display_tool_bar_line (it)
9386 struct it *it;
9387 {
9388 struct glyph_row *row = it->glyph_row;
9389 int max_x = it->last_visible_x;
9390 struct glyph *last;
9391
9392 prepare_desired_row (row);
9393 row->y = it->current_y;
9394
9395 /* Note that this isn't made use of if the face hasn't a box,
9396 so there's no need to check the face here. */
9397 it->start_of_box_run_p = 1;
9398
9399 while (it->current_x < max_x)
9400 {
9401 int x_before, x, n_glyphs_before, i, nglyphs;
9402
9403 /* Get the next display element. */
9404 if (!get_next_display_element (it))
9405 break;
9406
9407 /* Produce glyphs. */
9408 x_before = it->current_x;
9409 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9410 PRODUCE_GLYPHS (it);
9411
9412 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9413 i = 0;
9414 x = x_before;
9415 while (i < nglyphs)
9416 {
9417 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9418
9419 if (x + glyph->pixel_width > max_x)
9420 {
9421 /* Glyph doesn't fit on line. */
9422 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9423 it->current_x = x;
9424 goto out;
9425 }
9426
9427 ++it->hpos;
9428 x += glyph->pixel_width;
9429 ++i;
9430 }
9431
9432 /* Stop at line ends. */
9433 if (ITERATOR_AT_END_OF_LINE_P (it))
9434 break;
9435
9436 set_iterator_to_next (it, 1);
9437 }
9438
9439 out:;
9440
9441 row->displays_text_p = row->used[TEXT_AREA] != 0;
9442 extend_face_to_end_of_line (it);
9443 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9444 last->right_box_line_p = 1;
9445 if (last == row->glyphs[TEXT_AREA])
9446 last->left_box_line_p = 1;
9447 compute_line_metrics (it);
9448
9449 /* If line is empty, make it occupy the rest of the tool-bar. */
9450 if (!row->displays_text_p)
9451 {
9452 row->height = row->phys_height = it->last_visible_y - row->y;
9453 row->ascent = row->phys_ascent = 0;
9454 row->extra_line_spacing = 0;
9455 }
9456
9457 row->full_width_p = 1;
9458 row->continued_p = 0;
9459 row->truncated_on_left_p = 0;
9460 row->truncated_on_right_p = 0;
9461
9462 it->current_x = it->hpos = 0;
9463 it->current_y += row->height;
9464 ++it->vpos;
9465 ++it->glyph_row;
9466 }
9467
9468
9469 /* Value is the number of screen lines needed to make all tool-bar
9470 items of frame F visible. */
9471
9472 static int
9473 tool_bar_lines_needed (f)
9474 struct frame *f;
9475 {
9476 struct window *w = XWINDOW (f->tool_bar_window);
9477 struct it it;
9478
9479 /* Initialize an iterator for iteration over
9480 F->desired_tool_bar_string in the tool-bar window of frame F. */
9481 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9482 it.first_visible_x = 0;
9483 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9484 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9485
9486 while (!ITERATOR_AT_END_P (&it))
9487 {
9488 it.glyph_row = w->desired_matrix->rows;
9489 clear_glyph_row (it.glyph_row);
9490 display_tool_bar_line (&it);
9491 }
9492
9493 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9494 }
9495
9496
9497 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9498 0, 1, 0,
9499 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9500 (frame)
9501 Lisp_Object frame;
9502 {
9503 struct frame *f;
9504 struct window *w;
9505 int nlines = 0;
9506
9507 if (NILP (frame))
9508 frame = selected_frame;
9509 else
9510 CHECK_FRAME (frame);
9511 f = XFRAME (frame);
9512
9513 if (WINDOWP (f->tool_bar_window)
9514 || (w = XWINDOW (f->tool_bar_window),
9515 WINDOW_TOTAL_LINES (w) > 0))
9516 {
9517 update_tool_bar (f, 1);
9518 if (f->n_tool_bar_items)
9519 {
9520 build_desired_tool_bar_string (f);
9521 nlines = tool_bar_lines_needed (f);
9522 }
9523 }
9524
9525 return make_number (nlines);
9526 }
9527
9528
9529 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9530 height should be changed. */
9531
9532 static int
9533 redisplay_tool_bar (f)
9534 struct frame *f;
9535 {
9536 struct window *w;
9537 struct it it;
9538 struct glyph_row *row;
9539 int change_height_p = 0;
9540
9541 #ifdef USE_GTK
9542 if (FRAME_EXTERNAL_TOOL_BAR (f))
9543 update_frame_tool_bar (f);
9544 return 0;
9545 #endif
9546
9547 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9548 do anything. This means you must start with tool-bar-lines
9549 non-zero to get the auto-sizing effect. Or in other words, you
9550 can turn off tool-bars by specifying tool-bar-lines zero. */
9551 if (!WINDOWP (f->tool_bar_window)
9552 || (w = XWINDOW (f->tool_bar_window),
9553 WINDOW_TOTAL_LINES (w) == 0))
9554 return 0;
9555
9556 /* Set up an iterator for the tool-bar window. */
9557 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9558 it.first_visible_x = 0;
9559 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9560 row = it.glyph_row;
9561
9562 /* Build a string that represents the contents of the tool-bar. */
9563 build_desired_tool_bar_string (f);
9564 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9565
9566 /* Display as many lines as needed to display all tool-bar items. */
9567 while (it.current_y < it.last_visible_y)
9568 display_tool_bar_line (&it);
9569
9570 /* It doesn't make much sense to try scrolling in the tool-bar
9571 window, so don't do it. */
9572 w->desired_matrix->no_scrolling_p = 1;
9573 w->must_be_updated_p = 1;
9574
9575 if (auto_resize_tool_bars_p)
9576 {
9577 int nlines;
9578
9579 /* If we couldn't display everything, change the tool-bar's
9580 height. */
9581 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9582 change_height_p = 1;
9583
9584 /* If there are blank lines at the end, except for a partially
9585 visible blank line at the end that is smaller than
9586 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9587 row = it.glyph_row - 1;
9588 if (!row->displays_text_p
9589 && row->height >= FRAME_LINE_HEIGHT (f))
9590 change_height_p = 1;
9591
9592 /* If row displays tool-bar items, but is partially visible,
9593 change the tool-bar's height. */
9594 if (row->displays_text_p
9595 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9596 change_height_p = 1;
9597
9598 /* Resize windows as needed by changing the `tool-bar-lines'
9599 frame parameter. */
9600 if (change_height_p
9601 && (nlines = tool_bar_lines_needed (f),
9602 nlines != WINDOW_TOTAL_LINES (w)))
9603 {
9604 extern Lisp_Object Qtool_bar_lines;
9605 Lisp_Object frame;
9606 int old_height = WINDOW_TOTAL_LINES (w);
9607
9608 XSETFRAME (frame, f);
9609 clear_glyph_matrix (w->desired_matrix);
9610 Fmodify_frame_parameters (frame,
9611 Fcons (Fcons (Qtool_bar_lines,
9612 make_number (nlines)),
9613 Qnil));
9614 if (WINDOW_TOTAL_LINES (w) != old_height)
9615 fonts_changed_p = 1;
9616 }
9617 }
9618
9619 return change_height_p;
9620 }
9621
9622
9623 /* Get information about the tool-bar item which is displayed in GLYPH
9624 on frame F. Return in *PROP_IDX the index where tool-bar item
9625 properties start in F->tool_bar_items. Value is zero if
9626 GLYPH doesn't display a tool-bar item. */
9627
9628 static int
9629 tool_bar_item_info (f, glyph, prop_idx)
9630 struct frame *f;
9631 struct glyph *glyph;
9632 int *prop_idx;
9633 {
9634 Lisp_Object prop;
9635 int success_p;
9636 int charpos;
9637
9638 /* This function can be called asynchronously, which means we must
9639 exclude any possibility that Fget_text_property signals an
9640 error. */
9641 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9642 charpos = max (0, charpos);
9643
9644 /* Get the text property `menu-item' at pos. The value of that
9645 property is the start index of this item's properties in
9646 F->tool_bar_items. */
9647 prop = Fget_text_property (make_number (charpos),
9648 Qmenu_item, f->current_tool_bar_string);
9649 if (INTEGERP (prop))
9650 {
9651 *prop_idx = XINT (prop);
9652 success_p = 1;
9653 }
9654 else
9655 success_p = 0;
9656
9657 return success_p;
9658 }
9659
9660 \f
9661 /* Get information about the tool-bar item at position X/Y on frame F.
9662 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9663 the current matrix of the tool-bar window of F, or NULL if not
9664 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9665 item in F->tool_bar_items. Value is
9666
9667 -1 if X/Y is not on a tool-bar item
9668 0 if X/Y is on the same item that was highlighted before.
9669 1 otherwise. */
9670
9671 static int
9672 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9673 struct frame *f;
9674 int x, y;
9675 struct glyph **glyph;
9676 int *hpos, *vpos, *prop_idx;
9677 {
9678 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9679 struct window *w = XWINDOW (f->tool_bar_window);
9680 int area;
9681
9682 /* Find the glyph under X/Y. */
9683 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9684 if (*glyph == NULL)
9685 return -1;
9686
9687 /* Get the start of this tool-bar item's properties in
9688 f->tool_bar_items. */
9689 if (!tool_bar_item_info (f, *glyph, prop_idx))
9690 return -1;
9691
9692 /* Is mouse on the highlighted item? */
9693 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9694 && *vpos >= dpyinfo->mouse_face_beg_row
9695 && *vpos <= dpyinfo->mouse_face_end_row
9696 && (*vpos > dpyinfo->mouse_face_beg_row
9697 || *hpos >= dpyinfo->mouse_face_beg_col)
9698 && (*vpos < dpyinfo->mouse_face_end_row
9699 || *hpos < dpyinfo->mouse_face_end_col
9700 || dpyinfo->mouse_face_past_end))
9701 return 0;
9702
9703 return 1;
9704 }
9705
9706
9707 /* EXPORT:
9708 Handle mouse button event on the tool-bar of frame F, at
9709 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9710 0 for button release. MODIFIERS is event modifiers for button
9711 release. */
9712
9713 void
9714 handle_tool_bar_click (f, x, y, down_p, modifiers)
9715 struct frame *f;
9716 int x, y, down_p;
9717 unsigned int modifiers;
9718 {
9719 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9720 struct window *w = XWINDOW (f->tool_bar_window);
9721 int hpos, vpos, prop_idx;
9722 struct glyph *glyph;
9723 Lisp_Object enabled_p;
9724
9725 /* If not on the highlighted tool-bar item, return. */
9726 frame_to_window_pixel_xy (w, &x, &y);
9727 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9728 return;
9729
9730 /* If item is disabled, do nothing. */
9731 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9732 if (NILP (enabled_p))
9733 return;
9734
9735 if (down_p)
9736 {
9737 /* Show item in pressed state. */
9738 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9739 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9740 last_tool_bar_item = prop_idx;
9741 }
9742 else
9743 {
9744 Lisp_Object key, frame;
9745 struct input_event event;
9746 EVENT_INIT (event);
9747
9748 /* Show item in released state. */
9749 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9750 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9751
9752 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9753
9754 XSETFRAME (frame, f);
9755 event.kind = TOOL_BAR_EVENT;
9756 event.frame_or_window = frame;
9757 event.arg = frame;
9758 kbd_buffer_store_event (&event);
9759
9760 event.kind = TOOL_BAR_EVENT;
9761 event.frame_or_window = frame;
9762 event.arg = key;
9763 event.modifiers = modifiers;
9764 kbd_buffer_store_event (&event);
9765 last_tool_bar_item = -1;
9766 }
9767 }
9768
9769
9770 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9771 tool-bar window-relative coordinates X/Y. Called from
9772 note_mouse_highlight. */
9773
9774 static void
9775 note_tool_bar_highlight (f, x, y)
9776 struct frame *f;
9777 int x, y;
9778 {
9779 Lisp_Object window = f->tool_bar_window;
9780 struct window *w = XWINDOW (window);
9781 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9782 int hpos, vpos;
9783 struct glyph *glyph;
9784 struct glyph_row *row;
9785 int i;
9786 Lisp_Object enabled_p;
9787 int prop_idx;
9788 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9789 int mouse_down_p, rc;
9790
9791 /* Function note_mouse_highlight is called with negative x(y
9792 values when mouse moves outside of the frame. */
9793 if (x <= 0 || y <= 0)
9794 {
9795 clear_mouse_face (dpyinfo);
9796 return;
9797 }
9798
9799 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9800 if (rc < 0)
9801 {
9802 /* Not on tool-bar item. */
9803 clear_mouse_face (dpyinfo);
9804 return;
9805 }
9806 else if (rc == 0)
9807 /* On same tool-bar item as before. */
9808 goto set_help_echo;
9809
9810 clear_mouse_face (dpyinfo);
9811
9812 /* Mouse is down, but on different tool-bar item? */
9813 mouse_down_p = (dpyinfo->grabbed
9814 && f == last_mouse_frame
9815 && FRAME_LIVE_P (f));
9816 if (mouse_down_p
9817 && last_tool_bar_item != prop_idx)
9818 return;
9819
9820 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9821 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9822
9823 /* If tool-bar item is not enabled, don't highlight it. */
9824 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9825 if (!NILP (enabled_p))
9826 {
9827 /* Compute the x-position of the glyph. In front and past the
9828 image is a space. We include this in the highlighted area. */
9829 row = MATRIX_ROW (w->current_matrix, vpos);
9830 for (i = x = 0; i < hpos; ++i)
9831 x += row->glyphs[TEXT_AREA][i].pixel_width;
9832
9833 /* Record this as the current active region. */
9834 dpyinfo->mouse_face_beg_col = hpos;
9835 dpyinfo->mouse_face_beg_row = vpos;
9836 dpyinfo->mouse_face_beg_x = x;
9837 dpyinfo->mouse_face_beg_y = row->y;
9838 dpyinfo->mouse_face_past_end = 0;
9839
9840 dpyinfo->mouse_face_end_col = hpos + 1;
9841 dpyinfo->mouse_face_end_row = vpos;
9842 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9843 dpyinfo->mouse_face_end_y = row->y;
9844 dpyinfo->mouse_face_window = window;
9845 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9846
9847 /* Display it as active. */
9848 show_mouse_face (dpyinfo, draw);
9849 dpyinfo->mouse_face_image_state = draw;
9850 }
9851
9852 set_help_echo:
9853
9854 /* Set help_echo_string to a help string to display for this tool-bar item.
9855 XTread_socket does the rest. */
9856 help_echo_object = help_echo_window = Qnil;
9857 help_echo_pos = -1;
9858 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9859 if (NILP (help_echo_string))
9860 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9861 }
9862
9863 #endif /* HAVE_WINDOW_SYSTEM */
9864
9865
9866 \f
9867 /************************************************************************
9868 Horizontal scrolling
9869 ************************************************************************/
9870
9871 static int hscroll_window_tree P_ ((Lisp_Object));
9872 static int hscroll_windows P_ ((Lisp_Object));
9873
9874 /* For all leaf windows in the window tree rooted at WINDOW, set their
9875 hscroll value so that PT is (i) visible in the window, and (ii) so
9876 that it is not within a certain margin at the window's left and
9877 right border. Value is non-zero if any window's hscroll has been
9878 changed. */
9879
9880 static int
9881 hscroll_window_tree (window)
9882 Lisp_Object window;
9883 {
9884 int hscrolled_p = 0;
9885 int hscroll_relative_p = FLOATP (Vhscroll_step);
9886 int hscroll_step_abs = 0;
9887 double hscroll_step_rel = 0;
9888
9889 if (hscroll_relative_p)
9890 {
9891 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9892 if (hscroll_step_rel < 0)
9893 {
9894 hscroll_relative_p = 0;
9895 hscroll_step_abs = 0;
9896 }
9897 }
9898 else if (INTEGERP (Vhscroll_step))
9899 {
9900 hscroll_step_abs = XINT (Vhscroll_step);
9901 if (hscroll_step_abs < 0)
9902 hscroll_step_abs = 0;
9903 }
9904 else
9905 hscroll_step_abs = 0;
9906
9907 while (WINDOWP (window))
9908 {
9909 struct window *w = XWINDOW (window);
9910
9911 if (WINDOWP (w->hchild))
9912 hscrolled_p |= hscroll_window_tree (w->hchild);
9913 else if (WINDOWP (w->vchild))
9914 hscrolled_p |= hscroll_window_tree (w->vchild);
9915 else if (w->cursor.vpos >= 0)
9916 {
9917 int h_margin;
9918 int text_area_width;
9919 struct glyph_row *current_cursor_row
9920 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9921 struct glyph_row *desired_cursor_row
9922 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9923 struct glyph_row *cursor_row
9924 = (desired_cursor_row->enabled_p
9925 ? desired_cursor_row
9926 : current_cursor_row);
9927
9928 text_area_width = window_box_width (w, TEXT_AREA);
9929
9930 /* Scroll when cursor is inside this scroll margin. */
9931 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9932
9933 if ((XFASTINT (w->hscroll)
9934 && w->cursor.x <= h_margin)
9935 || (cursor_row->enabled_p
9936 && cursor_row->truncated_on_right_p
9937 && (w->cursor.x >= text_area_width - h_margin)))
9938 {
9939 struct it it;
9940 int hscroll;
9941 struct buffer *saved_current_buffer;
9942 int pt;
9943 int wanted_x;
9944
9945 /* Find point in a display of infinite width. */
9946 saved_current_buffer = current_buffer;
9947 current_buffer = XBUFFER (w->buffer);
9948
9949 if (w == XWINDOW (selected_window))
9950 pt = BUF_PT (current_buffer);
9951 else
9952 {
9953 pt = marker_position (w->pointm);
9954 pt = max (BEGV, pt);
9955 pt = min (ZV, pt);
9956 }
9957
9958 /* Move iterator to pt starting at cursor_row->start in
9959 a line with infinite width. */
9960 init_to_row_start (&it, w, cursor_row);
9961 it.last_visible_x = INFINITY;
9962 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9963 current_buffer = saved_current_buffer;
9964
9965 /* Position cursor in window. */
9966 if (!hscroll_relative_p && hscroll_step_abs == 0)
9967 hscroll = max (0, (it.current_x
9968 - (ITERATOR_AT_END_OF_LINE_P (&it)
9969 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9970 : (text_area_width / 2))))
9971 / FRAME_COLUMN_WIDTH (it.f);
9972 else if (w->cursor.x >= text_area_width - h_margin)
9973 {
9974 if (hscroll_relative_p)
9975 wanted_x = text_area_width * (1 - hscroll_step_rel)
9976 - h_margin;
9977 else
9978 wanted_x = text_area_width
9979 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9980 - h_margin;
9981 hscroll
9982 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9983 }
9984 else
9985 {
9986 if (hscroll_relative_p)
9987 wanted_x = text_area_width * hscroll_step_rel
9988 + h_margin;
9989 else
9990 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9991 + h_margin;
9992 hscroll
9993 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9994 }
9995 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9996
9997 /* Don't call Fset_window_hscroll if value hasn't
9998 changed because it will prevent redisplay
9999 optimizations. */
10000 if (XFASTINT (w->hscroll) != hscroll)
10001 {
10002 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10003 w->hscroll = make_number (hscroll);
10004 hscrolled_p = 1;
10005 }
10006 }
10007 }
10008
10009 window = w->next;
10010 }
10011
10012 /* Value is non-zero if hscroll of any leaf window has been changed. */
10013 return hscrolled_p;
10014 }
10015
10016
10017 /* Set hscroll so that cursor is visible and not inside horizontal
10018 scroll margins for all windows in the tree rooted at WINDOW. See
10019 also hscroll_window_tree above. Value is non-zero if any window's
10020 hscroll has been changed. If it has, desired matrices on the frame
10021 of WINDOW are cleared. */
10022
10023 static int
10024 hscroll_windows (window)
10025 Lisp_Object window;
10026 {
10027 int hscrolled_p;
10028
10029 if (automatic_hscrolling_p)
10030 {
10031 hscrolled_p = hscroll_window_tree (window);
10032 if (hscrolled_p)
10033 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10034 }
10035 else
10036 hscrolled_p = 0;
10037 return hscrolled_p;
10038 }
10039
10040
10041 \f
10042 /************************************************************************
10043 Redisplay
10044 ************************************************************************/
10045
10046 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10047 to a non-zero value. This is sometimes handy to have in a debugger
10048 session. */
10049
10050 #if GLYPH_DEBUG
10051
10052 /* First and last unchanged row for try_window_id. */
10053
10054 int debug_first_unchanged_at_end_vpos;
10055 int debug_last_unchanged_at_beg_vpos;
10056
10057 /* Delta vpos and y. */
10058
10059 int debug_dvpos, debug_dy;
10060
10061 /* Delta in characters and bytes for try_window_id. */
10062
10063 int debug_delta, debug_delta_bytes;
10064
10065 /* Values of window_end_pos and window_end_vpos at the end of
10066 try_window_id. */
10067
10068 EMACS_INT debug_end_pos, debug_end_vpos;
10069
10070 /* Append a string to W->desired_matrix->method. FMT is a printf
10071 format string. A1...A9 are a supplement for a variable-length
10072 argument list. If trace_redisplay_p is non-zero also printf the
10073 resulting string to stderr. */
10074
10075 static void
10076 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10077 struct window *w;
10078 char *fmt;
10079 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10080 {
10081 char buffer[512];
10082 char *method = w->desired_matrix->method;
10083 int len = strlen (method);
10084 int size = sizeof w->desired_matrix->method;
10085 int remaining = size - len - 1;
10086
10087 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10088 if (len && remaining)
10089 {
10090 method[len] = '|';
10091 --remaining, ++len;
10092 }
10093
10094 strncpy (method + len, buffer, remaining);
10095
10096 if (trace_redisplay_p)
10097 fprintf (stderr, "%p (%s): %s\n",
10098 w,
10099 ((BUFFERP (w->buffer)
10100 && STRINGP (XBUFFER (w->buffer)->name))
10101 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10102 : "no buffer"),
10103 buffer);
10104 }
10105
10106 #endif /* GLYPH_DEBUG */
10107
10108
10109 /* Value is non-zero if all changes in window W, which displays
10110 current_buffer, are in the text between START and END. START is a
10111 buffer position, END is given as a distance from Z. Used in
10112 redisplay_internal for display optimization. */
10113
10114 static INLINE int
10115 text_outside_line_unchanged_p (w, start, end)
10116 struct window *w;
10117 int start, end;
10118 {
10119 int unchanged_p = 1;
10120
10121 /* If text or overlays have changed, see where. */
10122 if (XFASTINT (w->last_modified) < MODIFF
10123 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10124 {
10125 /* Gap in the line? */
10126 if (GPT < start || Z - GPT < end)
10127 unchanged_p = 0;
10128
10129 /* Changes start in front of the line, or end after it? */
10130 if (unchanged_p
10131 && (BEG_UNCHANGED < start - 1
10132 || END_UNCHANGED < end))
10133 unchanged_p = 0;
10134
10135 /* If selective display, can't optimize if changes start at the
10136 beginning of the line. */
10137 if (unchanged_p
10138 && INTEGERP (current_buffer->selective_display)
10139 && XINT (current_buffer->selective_display) > 0
10140 && (BEG_UNCHANGED < start || GPT <= start))
10141 unchanged_p = 0;
10142
10143 /* If there are overlays at the start or end of the line, these
10144 may have overlay strings with newlines in them. A change at
10145 START, for instance, may actually concern the display of such
10146 overlay strings as well, and they are displayed on different
10147 lines. So, quickly rule out this case. (For the future, it
10148 might be desirable to implement something more telling than
10149 just BEG/END_UNCHANGED.) */
10150 if (unchanged_p)
10151 {
10152 if (BEG + BEG_UNCHANGED == start
10153 && overlay_touches_p (start))
10154 unchanged_p = 0;
10155 if (END_UNCHANGED == end
10156 && overlay_touches_p (Z - end))
10157 unchanged_p = 0;
10158 }
10159 }
10160
10161 return unchanged_p;
10162 }
10163
10164
10165 /* Do a frame update, taking possible shortcuts into account. This is
10166 the main external entry point for redisplay.
10167
10168 If the last redisplay displayed an echo area message and that message
10169 is no longer requested, we clear the echo area or bring back the
10170 mini-buffer if that is in use. */
10171
10172 void
10173 redisplay ()
10174 {
10175 redisplay_internal (0);
10176 }
10177
10178
10179 static Lisp_Object
10180 overlay_arrow_string_or_property (var)
10181 Lisp_Object var;
10182 {
10183 Lisp_Object val;
10184
10185 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10186 return val;
10187
10188 return Voverlay_arrow_string;
10189 }
10190
10191 /* Return 1 if there are any overlay-arrows in current_buffer. */
10192 static int
10193 overlay_arrow_in_current_buffer_p ()
10194 {
10195 Lisp_Object vlist;
10196
10197 for (vlist = Voverlay_arrow_variable_list;
10198 CONSP (vlist);
10199 vlist = XCDR (vlist))
10200 {
10201 Lisp_Object var = XCAR (vlist);
10202 Lisp_Object val;
10203
10204 if (!SYMBOLP (var))
10205 continue;
10206 val = find_symbol_value (var);
10207 if (MARKERP (val)
10208 && current_buffer == XMARKER (val)->buffer)
10209 return 1;
10210 }
10211 return 0;
10212 }
10213
10214
10215 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10216 has changed. */
10217
10218 static int
10219 overlay_arrows_changed_p ()
10220 {
10221 Lisp_Object vlist;
10222
10223 for (vlist = Voverlay_arrow_variable_list;
10224 CONSP (vlist);
10225 vlist = XCDR (vlist))
10226 {
10227 Lisp_Object var = XCAR (vlist);
10228 Lisp_Object val, pstr;
10229
10230 if (!SYMBOLP (var))
10231 continue;
10232 val = find_symbol_value (var);
10233 if (!MARKERP (val))
10234 continue;
10235 if (! EQ (COERCE_MARKER (val),
10236 Fget (var, Qlast_arrow_position))
10237 || ! (pstr = overlay_arrow_string_or_property (var),
10238 EQ (pstr, Fget (var, Qlast_arrow_string))))
10239 return 1;
10240 }
10241 return 0;
10242 }
10243
10244 /* Mark overlay arrows to be updated on next redisplay. */
10245
10246 static void
10247 update_overlay_arrows (up_to_date)
10248 int up_to_date;
10249 {
10250 Lisp_Object vlist;
10251
10252 for (vlist = Voverlay_arrow_variable_list;
10253 CONSP (vlist);
10254 vlist = XCDR (vlist))
10255 {
10256 Lisp_Object var = XCAR (vlist);
10257
10258 if (!SYMBOLP (var))
10259 continue;
10260
10261 if (up_to_date > 0)
10262 {
10263 Lisp_Object val = find_symbol_value (var);
10264 Fput (var, Qlast_arrow_position,
10265 COERCE_MARKER (val));
10266 Fput (var, Qlast_arrow_string,
10267 overlay_arrow_string_or_property (var));
10268 }
10269 else if (up_to_date < 0
10270 || !NILP (Fget (var, Qlast_arrow_position)))
10271 {
10272 Fput (var, Qlast_arrow_position, Qt);
10273 Fput (var, Qlast_arrow_string, Qt);
10274 }
10275 }
10276 }
10277
10278
10279 /* Return overlay arrow string to display at row.
10280 Return integer (bitmap number) for arrow bitmap in left fringe.
10281 Return nil if no overlay arrow. */
10282
10283 static Lisp_Object
10284 overlay_arrow_at_row (it, row)
10285 struct it *it;
10286 struct glyph_row *row;
10287 {
10288 Lisp_Object vlist;
10289
10290 for (vlist = Voverlay_arrow_variable_list;
10291 CONSP (vlist);
10292 vlist = XCDR (vlist))
10293 {
10294 Lisp_Object var = XCAR (vlist);
10295 Lisp_Object val;
10296
10297 if (!SYMBOLP (var))
10298 continue;
10299
10300 val = find_symbol_value (var);
10301
10302 if (MARKERP (val)
10303 && current_buffer == XMARKER (val)->buffer
10304 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10305 {
10306 if (FRAME_WINDOW_P (it->f)
10307 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10308 {
10309 #ifdef HAVE_WINDOW_SYSTEM
10310 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10311 {
10312 int fringe_bitmap;
10313 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10314 return make_number (fringe_bitmap);
10315 }
10316 #endif
10317 return make_number (-1); /* Use default arrow bitmap */
10318 }
10319 return overlay_arrow_string_or_property (var);
10320 }
10321 }
10322
10323 return Qnil;
10324 }
10325
10326 /* Return 1 if point moved out of or into a composition. Otherwise
10327 return 0. PREV_BUF and PREV_PT are the last point buffer and
10328 position. BUF and PT are the current point buffer and position. */
10329
10330 int
10331 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10332 struct buffer *prev_buf, *buf;
10333 int prev_pt, pt;
10334 {
10335 int start, end;
10336 Lisp_Object prop;
10337 Lisp_Object buffer;
10338
10339 XSETBUFFER (buffer, buf);
10340 /* Check a composition at the last point if point moved within the
10341 same buffer. */
10342 if (prev_buf == buf)
10343 {
10344 if (prev_pt == pt)
10345 /* Point didn't move. */
10346 return 0;
10347
10348 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10349 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10350 && COMPOSITION_VALID_P (start, end, prop)
10351 && start < prev_pt && end > prev_pt)
10352 /* The last point was within the composition. Return 1 iff
10353 point moved out of the composition. */
10354 return (pt <= start || pt >= end);
10355 }
10356
10357 /* Check a composition at the current point. */
10358 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10359 && find_composition (pt, -1, &start, &end, &prop, buffer)
10360 && COMPOSITION_VALID_P (start, end, prop)
10361 && start < pt && end > pt);
10362 }
10363
10364
10365 /* Reconsider the setting of B->clip_changed which is displayed
10366 in window W. */
10367
10368 static INLINE void
10369 reconsider_clip_changes (w, b)
10370 struct window *w;
10371 struct buffer *b;
10372 {
10373 if (b->clip_changed
10374 && !NILP (w->window_end_valid)
10375 && w->current_matrix->buffer == b
10376 && w->current_matrix->zv == BUF_ZV (b)
10377 && w->current_matrix->begv == BUF_BEGV (b))
10378 b->clip_changed = 0;
10379
10380 /* If display wasn't paused, and W is not a tool bar window, see if
10381 point has been moved into or out of a composition. In that case,
10382 we set b->clip_changed to 1 to force updating the screen. If
10383 b->clip_changed has already been set to 1, we can skip this
10384 check. */
10385 if (!b->clip_changed
10386 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10387 {
10388 int pt;
10389
10390 if (w == XWINDOW (selected_window))
10391 pt = BUF_PT (current_buffer);
10392 else
10393 pt = marker_position (w->pointm);
10394
10395 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10396 || pt != XINT (w->last_point))
10397 && check_point_in_composition (w->current_matrix->buffer,
10398 XINT (w->last_point),
10399 XBUFFER (w->buffer), pt))
10400 b->clip_changed = 1;
10401 }
10402 }
10403 \f
10404
10405 /* Select FRAME to forward the values of frame-local variables into C
10406 variables so that the redisplay routines can access those values
10407 directly. */
10408
10409 static void
10410 select_frame_for_redisplay (frame)
10411 Lisp_Object frame;
10412 {
10413 Lisp_Object tail, sym, val;
10414 Lisp_Object old = selected_frame;
10415
10416 selected_frame = frame;
10417
10418 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10419 if (CONSP (XCAR (tail))
10420 && (sym = XCAR (XCAR (tail)),
10421 SYMBOLP (sym))
10422 && (sym = indirect_variable (sym),
10423 val = SYMBOL_VALUE (sym),
10424 (BUFFER_LOCAL_VALUEP (val)
10425 || SOME_BUFFER_LOCAL_VALUEP (val)))
10426 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10427 /* Use find_symbol_value rather than Fsymbol_value
10428 to avoid an error if it is void. */
10429 find_symbol_value (sym);
10430
10431 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10432 if (CONSP (XCAR (tail))
10433 && (sym = XCAR (XCAR (tail)),
10434 SYMBOLP (sym))
10435 && (sym = indirect_variable (sym),
10436 val = SYMBOL_VALUE (sym),
10437 (BUFFER_LOCAL_VALUEP (val)
10438 || SOME_BUFFER_LOCAL_VALUEP (val)))
10439 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10440 find_symbol_value (sym);
10441 }
10442
10443
10444 #define STOP_POLLING \
10445 do { if (! polling_stopped_here) stop_polling (); \
10446 polling_stopped_here = 1; } while (0)
10447
10448 #define RESUME_POLLING \
10449 do { if (polling_stopped_here) start_polling (); \
10450 polling_stopped_here = 0; } while (0)
10451
10452
10453 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10454 response to any user action; therefore, we should preserve the echo
10455 area. (Actually, our caller does that job.) Perhaps in the future
10456 avoid recentering windows if it is not necessary; currently that
10457 causes some problems. */
10458
10459 static void
10460 redisplay_internal (preserve_echo_area)
10461 int preserve_echo_area;
10462 {
10463 struct window *w = XWINDOW (selected_window);
10464 struct frame *f = XFRAME (w->frame);
10465 int pause;
10466 int must_finish = 0;
10467 struct text_pos tlbufpos, tlendpos;
10468 int number_of_visible_frames;
10469 int count;
10470 struct frame *sf = SELECTED_FRAME ();
10471 int polling_stopped_here = 0;
10472
10473 /* Non-zero means redisplay has to consider all windows on all
10474 frames. Zero means, only selected_window is considered. */
10475 int consider_all_windows_p;
10476
10477 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10478
10479 /* No redisplay if running in batch mode or frame is not yet fully
10480 initialized, or redisplay is explicitly turned off by setting
10481 Vinhibit_redisplay. */
10482 if (noninteractive
10483 || !NILP (Vinhibit_redisplay)
10484 || !f->glyphs_initialized_p)
10485 return;
10486
10487 /* The flag redisplay_performed_directly_p is set by
10488 direct_output_for_insert when it already did the whole screen
10489 update necessary. */
10490 if (redisplay_performed_directly_p)
10491 {
10492 redisplay_performed_directly_p = 0;
10493 if (!hscroll_windows (selected_window))
10494 return;
10495 }
10496
10497 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10498 if (popup_activated ())
10499 return;
10500 #endif
10501
10502 /* I don't think this happens but let's be paranoid. */
10503 if (redisplaying_p)
10504 return;
10505
10506 /* Record a function that resets redisplaying_p to its old value
10507 when we leave this function. */
10508 count = SPECPDL_INDEX ();
10509 record_unwind_protect (unwind_redisplay,
10510 Fcons (make_number (redisplaying_p), selected_frame));
10511 ++redisplaying_p;
10512 specbind (Qinhibit_free_realized_faces, Qnil);
10513
10514 {
10515 Lisp_Object tail, frame;
10516
10517 FOR_EACH_FRAME (tail, frame)
10518 {
10519 struct frame *f = XFRAME (frame);
10520 f->already_hscrolled_p = 0;
10521 }
10522 }
10523
10524 retry:
10525 pause = 0;
10526 reconsider_clip_changes (w, current_buffer);
10527
10528 /* If new fonts have been loaded that make a glyph matrix adjustment
10529 necessary, do it. */
10530 if (fonts_changed_p)
10531 {
10532 adjust_glyphs (NULL);
10533 ++windows_or_buffers_changed;
10534 fonts_changed_p = 0;
10535 }
10536
10537 /* If face_change_count is non-zero, init_iterator will free all
10538 realized faces, which includes the faces referenced from current
10539 matrices. So, we can't reuse current matrices in this case. */
10540 if (face_change_count)
10541 ++windows_or_buffers_changed;
10542
10543 if (! FRAME_WINDOW_P (sf)
10544 && previous_terminal_frame != sf)
10545 {
10546 /* Since frames on an ASCII terminal share the same display
10547 area, displaying a different frame means redisplay the whole
10548 thing. */
10549 windows_or_buffers_changed++;
10550 SET_FRAME_GARBAGED (sf);
10551 XSETFRAME (Vterminal_frame, sf);
10552 }
10553 previous_terminal_frame = sf;
10554
10555 /* Set the visible flags for all frames. Do this before checking
10556 for resized or garbaged frames; they want to know if their frames
10557 are visible. See the comment in frame.h for
10558 FRAME_SAMPLE_VISIBILITY. */
10559 {
10560 Lisp_Object tail, frame;
10561
10562 number_of_visible_frames = 0;
10563
10564 FOR_EACH_FRAME (tail, frame)
10565 {
10566 struct frame *f = XFRAME (frame);
10567
10568 FRAME_SAMPLE_VISIBILITY (f);
10569 if (FRAME_VISIBLE_P (f))
10570 ++number_of_visible_frames;
10571 clear_desired_matrices (f);
10572 }
10573 }
10574
10575 /* Notice any pending interrupt request to change frame size. */
10576 do_pending_window_change (1);
10577
10578 /* Clear frames marked as garbaged. */
10579 if (frame_garbaged)
10580 clear_garbaged_frames ();
10581
10582 /* Build menubar and tool-bar items. */
10583 if (NILP (Vmemory_full))
10584 prepare_menu_bars ();
10585
10586 if (windows_or_buffers_changed)
10587 update_mode_lines++;
10588
10589 /* Detect case that we need to write or remove a star in the mode line. */
10590 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10591 {
10592 w->update_mode_line = Qt;
10593 if (buffer_shared > 1)
10594 update_mode_lines++;
10595 }
10596
10597 /* If %c is in the mode line, update it if needed. */
10598 if (!NILP (w->column_number_displayed)
10599 /* This alternative quickly identifies a common case
10600 where no change is needed. */
10601 && !(PT == XFASTINT (w->last_point)
10602 && XFASTINT (w->last_modified) >= MODIFF
10603 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10604 && (XFASTINT (w->column_number_displayed)
10605 != (int) current_column ())) /* iftc */
10606 w->update_mode_line = Qt;
10607
10608 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10609
10610 /* The variable buffer_shared is set in redisplay_window and
10611 indicates that we redisplay a buffer in different windows. See
10612 there. */
10613 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10614 || cursor_type_changed);
10615
10616 /* If specs for an arrow have changed, do thorough redisplay
10617 to ensure we remove any arrow that should no longer exist. */
10618 if (overlay_arrows_changed_p ())
10619 consider_all_windows_p = windows_or_buffers_changed = 1;
10620
10621 /* Normally the message* functions will have already displayed and
10622 updated the echo area, but the frame may have been trashed, or
10623 the update may have been preempted, so display the echo area
10624 again here. Checking message_cleared_p captures the case that
10625 the echo area should be cleared. */
10626 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10627 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10628 || (message_cleared_p
10629 && minibuf_level == 0
10630 /* If the mini-window is currently selected, this means the
10631 echo-area doesn't show through. */
10632 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10633 {
10634 int window_height_changed_p = echo_area_display (0);
10635 must_finish = 1;
10636
10637 /* If we don't display the current message, don't clear the
10638 message_cleared_p flag, because, if we did, we wouldn't clear
10639 the echo area in the next redisplay which doesn't preserve
10640 the echo area. */
10641 if (!display_last_displayed_message_p)
10642 message_cleared_p = 0;
10643
10644 if (fonts_changed_p)
10645 goto retry;
10646 else if (window_height_changed_p)
10647 {
10648 consider_all_windows_p = 1;
10649 ++update_mode_lines;
10650 ++windows_or_buffers_changed;
10651
10652 /* If window configuration was changed, frames may have been
10653 marked garbaged. Clear them or we will experience
10654 surprises wrt scrolling. */
10655 if (frame_garbaged)
10656 clear_garbaged_frames ();
10657 }
10658 }
10659 else if (EQ (selected_window, minibuf_window)
10660 && (current_buffer->clip_changed
10661 || XFASTINT (w->last_modified) < MODIFF
10662 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10663 && resize_mini_window (w, 0))
10664 {
10665 /* Resized active mini-window to fit the size of what it is
10666 showing if its contents might have changed. */
10667 must_finish = 1;
10668 consider_all_windows_p = 1;
10669 ++windows_or_buffers_changed;
10670 ++update_mode_lines;
10671
10672 /* If window configuration was changed, frames may have been
10673 marked garbaged. Clear them or we will experience
10674 surprises wrt scrolling. */
10675 if (frame_garbaged)
10676 clear_garbaged_frames ();
10677 }
10678
10679
10680 /* If showing the region, and mark has changed, we must redisplay
10681 the whole window. The assignment to this_line_start_pos prevents
10682 the optimization directly below this if-statement. */
10683 if (((!NILP (Vtransient_mark_mode)
10684 && !NILP (XBUFFER (w->buffer)->mark_active))
10685 != !NILP (w->region_showing))
10686 || (!NILP (w->region_showing)
10687 && !EQ (w->region_showing,
10688 Fmarker_position (XBUFFER (w->buffer)->mark))))
10689 CHARPOS (this_line_start_pos) = 0;
10690
10691 /* Optimize the case that only the line containing the cursor in the
10692 selected window has changed. Variables starting with this_ are
10693 set in display_line and record information about the line
10694 containing the cursor. */
10695 tlbufpos = this_line_start_pos;
10696 tlendpos = this_line_end_pos;
10697 if (!consider_all_windows_p
10698 && CHARPOS (tlbufpos) > 0
10699 && NILP (w->update_mode_line)
10700 && !current_buffer->clip_changed
10701 && !current_buffer->prevent_redisplay_optimizations_p
10702 && FRAME_VISIBLE_P (XFRAME (w->frame))
10703 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10704 /* Make sure recorded data applies to current buffer, etc. */
10705 && this_line_buffer == current_buffer
10706 && current_buffer == XBUFFER (w->buffer)
10707 && NILP (w->force_start)
10708 && NILP (w->optional_new_start)
10709 /* Point must be on the line that we have info recorded about. */
10710 && PT >= CHARPOS (tlbufpos)
10711 && PT <= Z - CHARPOS (tlendpos)
10712 /* All text outside that line, including its final newline,
10713 must be unchanged */
10714 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10715 CHARPOS (tlendpos)))
10716 {
10717 if (CHARPOS (tlbufpos) > BEGV
10718 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10719 && (CHARPOS (tlbufpos) == ZV
10720 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10721 /* Former continuation line has disappeared by becoming empty */
10722 goto cancel;
10723 else if (XFASTINT (w->last_modified) < MODIFF
10724 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10725 || MINI_WINDOW_P (w))
10726 {
10727 /* We have to handle the case of continuation around a
10728 wide-column character (See the comment in indent.c around
10729 line 885).
10730
10731 For instance, in the following case:
10732
10733 -------- Insert --------
10734 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10735 J_I_ ==> J_I_ `^^' are cursors.
10736 ^^ ^^
10737 -------- --------
10738
10739 As we have to redraw the line above, we should goto cancel. */
10740
10741 struct it it;
10742 int line_height_before = this_line_pixel_height;
10743
10744 /* Note that start_display will handle the case that the
10745 line starting at tlbufpos is a continuation lines. */
10746 start_display (&it, w, tlbufpos);
10747
10748 /* Implementation note: It this still necessary? */
10749 if (it.current_x != this_line_start_x)
10750 goto cancel;
10751
10752 TRACE ((stderr, "trying display optimization 1\n"));
10753 w->cursor.vpos = -1;
10754 overlay_arrow_seen = 0;
10755 it.vpos = this_line_vpos;
10756 it.current_y = this_line_y;
10757 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10758 display_line (&it);
10759
10760 /* If line contains point, is not continued,
10761 and ends at same distance from eob as before, we win */
10762 if (w->cursor.vpos >= 0
10763 /* Line is not continued, otherwise this_line_start_pos
10764 would have been set to 0 in display_line. */
10765 && CHARPOS (this_line_start_pos)
10766 /* Line ends as before. */
10767 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10768 /* Line has same height as before. Otherwise other lines
10769 would have to be shifted up or down. */
10770 && this_line_pixel_height == line_height_before)
10771 {
10772 /* If this is not the window's last line, we must adjust
10773 the charstarts of the lines below. */
10774 if (it.current_y < it.last_visible_y)
10775 {
10776 struct glyph_row *row
10777 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10778 int delta, delta_bytes;
10779
10780 if (Z - CHARPOS (tlendpos) == ZV)
10781 {
10782 /* This line ends at end of (accessible part of)
10783 buffer. There is no newline to count. */
10784 delta = (Z
10785 - CHARPOS (tlendpos)
10786 - MATRIX_ROW_START_CHARPOS (row));
10787 delta_bytes = (Z_BYTE
10788 - BYTEPOS (tlendpos)
10789 - MATRIX_ROW_START_BYTEPOS (row));
10790 }
10791 else
10792 {
10793 /* This line ends in a newline. Must take
10794 account of the newline and the rest of the
10795 text that follows. */
10796 delta = (Z
10797 - CHARPOS (tlendpos)
10798 - MATRIX_ROW_START_CHARPOS (row));
10799 delta_bytes = (Z_BYTE
10800 - BYTEPOS (tlendpos)
10801 - MATRIX_ROW_START_BYTEPOS (row));
10802 }
10803
10804 increment_matrix_positions (w->current_matrix,
10805 this_line_vpos + 1,
10806 w->current_matrix->nrows,
10807 delta, delta_bytes);
10808 }
10809
10810 /* If this row displays text now but previously didn't,
10811 or vice versa, w->window_end_vpos may have to be
10812 adjusted. */
10813 if ((it.glyph_row - 1)->displays_text_p)
10814 {
10815 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10816 XSETINT (w->window_end_vpos, this_line_vpos);
10817 }
10818 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10819 && this_line_vpos > 0)
10820 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10821 w->window_end_valid = Qnil;
10822
10823 /* Update hint: No need to try to scroll in update_window. */
10824 w->desired_matrix->no_scrolling_p = 1;
10825
10826 #if GLYPH_DEBUG
10827 *w->desired_matrix->method = 0;
10828 debug_method_add (w, "optimization 1");
10829 #endif
10830 #ifdef HAVE_WINDOW_SYSTEM
10831 update_window_fringes (w, 0);
10832 #endif
10833 goto update;
10834 }
10835 else
10836 goto cancel;
10837 }
10838 else if (/* Cursor position hasn't changed. */
10839 PT == XFASTINT (w->last_point)
10840 /* Make sure the cursor was last displayed
10841 in this window. Otherwise we have to reposition it. */
10842 && 0 <= w->cursor.vpos
10843 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10844 {
10845 if (!must_finish)
10846 {
10847 do_pending_window_change (1);
10848
10849 /* We used to always goto end_of_redisplay here, but this
10850 isn't enough if we have a blinking cursor. */
10851 if (w->cursor_off_p == w->last_cursor_off_p)
10852 goto end_of_redisplay;
10853 }
10854 goto update;
10855 }
10856 /* If highlighting the region, or if the cursor is in the echo area,
10857 then we can't just move the cursor. */
10858 else if (! (!NILP (Vtransient_mark_mode)
10859 && !NILP (current_buffer->mark_active))
10860 && (EQ (selected_window, current_buffer->last_selected_window)
10861 || highlight_nonselected_windows)
10862 && NILP (w->region_showing)
10863 && NILP (Vshow_trailing_whitespace)
10864 && !cursor_in_echo_area)
10865 {
10866 struct it it;
10867 struct glyph_row *row;
10868
10869 /* Skip from tlbufpos to PT and see where it is. Note that
10870 PT may be in invisible text. If so, we will end at the
10871 next visible position. */
10872 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10873 NULL, DEFAULT_FACE_ID);
10874 it.current_x = this_line_start_x;
10875 it.current_y = this_line_y;
10876 it.vpos = this_line_vpos;
10877
10878 /* The call to move_it_to stops in front of PT, but
10879 moves over before-strings. */
10880 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10881
10882 if (it.vpos == this_line_vpos
10883 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10884 row->enabled_p))
10885 {
10886 xassert (this_line_vpos == it.vpos);
10887 xassert (this_line_y == it.current_y);
10888 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10889 #if GLYPH_DEBUG
10890 *w->desired_matrix->method = 0;
10891 debug_method_add (w, "optimization 3");
10892 #endif
10893 goto update;
10894 }
10895 else
10896 goto cancel;
10897 }
10898
10899 cancel:
10900 /* Text changed drastically or point moved off of line. */
10901 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10902 }
10903
10904 CHARPOS (this_line_start_pos) = 0;
10905 consider_all_windows_p |= buffer_shared > 1;
10906 ++clear_face_cache_count;
10907 #ifdef HAVE_WINDOW_SYSTEM
10908 ++clear_image_cache_count;
10909 #endif
10910
10911 /* Build desired matrices, and update the display. If
10912 consider_all_windows_p is non-zero, do it for all windows on all
10913 frames. Otherwise do it for selected_window, only. */
10914
10915 if (consider_all_windows_p)
10916 {
10917 Lisp_Object tail, frame;
10918
10919 FOR_EACH_FRAME (tail, frame)
10920 XFRAME (frame)->updated_p = 0;
10921
10922 /* Recompute # windows showing selected buffer. This will be
10923 incremented each time such a window is displayed. */
10924 buffer_shared = 0;
10925
10926 FOR_EACH_FRAME (tail, frame)
10927 {
10928 struct frame *f = XFRAME (frame);
10929
10930 if (FRAME_WINDOW_P (f) || f == sf)
10931 {
10932 if (! EQ (frame, selected_frame))
10933 /* Select the frame, for the sake of frame-local
10934 variables. */
10935 select_frame_for_redisplay (frame);
10936
10937 /* Mark all the scroll bars to be removed; we'll redeem
10938 the ones we want when we redisplay their windows. */
10939 if (condemn_scroll_bars_hook)
10940 condemn_scroll_bars_hook (f);
10941
10942 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10943 redisplay_windows (FRAME_ROOT_WINDOW (f));
10944
10945 /* Any scroll bars which redisplay_windows should have
10946 nuked should now go away. */
10947 if (judge_scroll_bars_hook)
10948 judge_scroll_bars_hook (f);
10949
10950 /* If fonts changed, display again. */
10951 /* ??? rms: I suspect it is a mistake to jump all the way
10952 back to retry here. It should just retry this frame. */
10953 if (fonts_changed_p)
10954 goto retry;
10955
10956 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10957 {
10958 /* See if we have to hscroll. */
10959 if (!f->already_hscrolled_p)
10960 {
10961 f->already_hscrolled_p = 1;
10962 if (hscroll_windows (f->root_window))
10963 goto retry;
10964 }
10965
10966 /* Prevent various kinds of signals during display
10967 update. stdio is not robust about handling
10968 signals, which can cause an apparent I/O
10969 error. */
10970 if (interrupt_input)
10971 unrequest_sigio ();
10972 STOP_POLLING;
10973
10974 /* Update the display. */
10975 set_window_update_flags (XWINDOW (f->root_window), 1);
10976 pause |= update_frame (f, 0, 0);
10977 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10978 if (pause)
10979 break;
10980 #endif
10981
10982 f->updated_p = 1;
10983 }
10984 }
10985 }
10986
10987 if (!pause)
10988 {
10989 /* Do the mark_window_display_accurate after all windows have
10990 been redisplayed because this call resets flags in buffers
10991 which are needed for proper redisplay. */
10992 FOR_EACH_FRAME (tail, frame)
10993 {
10994 struct frame *f = XFRAME (frame);
10995 if (f->updated_p)
10996 {
10997 mark_window_display_accurate (f->root_window, 1);
10998 if (frame_up_to_date_hook)
10999 frame_up_to_date_hook (f);
11000 }
11001 }
11002 }
11003 }
11004 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11005 {
11006 Lisp_Object mini_window;
11007 struct frame *mini_frame;
11008
11009 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11010 /* Use list_of_error, not Qerror, so that
11011 we catch only errors and don't run the debugger. */
11012 internal_condition_case_1 (redisplay_window_1, selected_window,
11013 list_of_error,
11014 redisplay_window_error);
11015
11016 /* Compare desired and current matrices, perform output. */
11017
11018 update:
11019 /* If fonts changed, display again. */
11020 if (fonts_changed_p)
11021 goto retry;
11022
11023 /* Prevent various kinds of signals during display update.
11024 stdio is not robust about handling signals,
11025 which can cause an apparent I/O error. */
11026 if (interrupt_input)
11027 unrequest_sigio ();
11028 STOP_POLLING;
11029
11030 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11031 {
11032 if (hscroll_windows (selected_window))
11033 goto retry;
11034
11035 XWINDOW (selected_window)->must_be_updated_p = 1;
11036 pause = update_frame (sf, 0, 0);
11037 }
11038
11039 /* We may have called echo_area_display at the top of this
11040 function. If the echo area is on another frame, that may
11041 have put text on a frame other than the selected one, so the
11042 above call to update_frame would not have caught it. Catch
11043 it here. */
11044 mini_window = FRAME_MINIBUF_WINDOW (sf);
11045 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11046
11047 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11048 {
11049 XWINDOW (mini_window)->must_be_updated_p = 1;
11050 pause |= update_frame (mini_frame, 0, 0);
11051 if (!pause && hscroll_windows (mini_window))
11052 goto retry;
11053 }
11054 }
11055
11056 /* If display was paused because of pending input, make sure we do a
11057 thorough update the next time. */
11058 if (pause)
11059 {
11060 /* Prevent the optimization at the beginning of
11061 redisplay_internal that tries a single-line update of the
11062 line containing the cursor in the selected window. */
11063 CHARPOS (this_line_start_pos) = 0;
11064
11065 /* Let the overlay arrow be updated the next time. */
11066 update_overlay_arrows (0);
11067
11068 /* If we pause after scrolling, some rows in the current
11069 matrices of some windows are not valid. */
11070 if (!WINDOW_FULL_WIDTH_P (w)
11071 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11072 update_mode_lines = 1;
11073 }
11074 else
11075 {
11076 if (!consider_all_windows_p)
11077 {
11078 /* This has already been done above if
11079 consider_all_windows_p is set. */
11080 mark_window_display_accurate_1 (w, 1);
11081
11082 /* Say overlay arrows are up to date. */
11083 update_overlay_arrows (1);
11084
11085 if (frame_up_to_date_hook != 0)
11086 frame_up_to_date_hook (sf);
11087 }
11088
11089 update_mode_lines = 0;
11090 windows_or_buffers_changed = 0;
11091 cursor_type_changed = 0;
11092 }
11093
11094 /* Start SIGIO interrupts coming again. Having them off during the
11095 code above makes it less likely one will discard output, but not
11096 impossible, since there might be stuff in the system buffer here.
11097 But it is much hairier to try to do anything about that. */
11098 if (interrupt_input)
11099 request_sigio ();
11100 RESUME_POLLING;
11101
11102 /* If a frame has become visible which was not before, redisplay
11103 again, so that we display it. Expose events for such a frame
11104 (which it gets when becoming visible) don't call the parts of
11105 redisplay constructing glyphs, so simply exposing a frame won't
11106 display anything in this case. So, we have to display these
11107 frames here explicitly. */
11108 if (!pause)
11109 {
11110 Lisp_Object tail, frame;
11111 int new_count = 0;
11112
11113 FOR_EACH_FRAME (tail, frame)
11114 {
11115 int this_is_visible = 0;
11116
11117 if (XFRAME (frame)->visible)
11118 this_is_visible = 1;
11119 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11120 if (XFRAME (frame)->visible)
11121 this_is_visible = 1;
11122
11123 if (this_is_visible)
11124 new_count++;
11125 }
11126
11127 if (new_count != number_of_visible_frames)
11128 windows_or_buffers_changed++;
11129 }
11130
11131 /* Change frame size now if a change is pending. */
11132 do_pending_window_change (1);
11133
11134 /* If we just did a pending size change, or have additional
11135 visible frames, redisplay again. */
11136 if (windows_or_buffers_changed && !pause)
11137 goto retry;
11138
11139 /* Clear the face cache eventually. */
11140 if (consider_all_windows_p)
11141 {
11142 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11143 {
11144 clear_face_cache (0);
11145 clear_face_cache_count = 0;
11146 }
11147 #ifdef HAVE_WINDOW_SYSTEM
11148 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11149 {
11150 Lisp_Object tail, frame;
11151 FOR_EACH_FRAME (tail, frame)
11152 {
11153 struct frame *f = XFRAME (frame);
11154 if (FRAME_WINDOW_P (f))
11155 clear_image_cache (f, 0);
11156 }
11157 clear_image_cache_count = 0;
11158 }
11159 #endif /* HAVE_WINDOW_SYSTEM */
11160 }
11161
11162 end_of_redisplay:
11163 unbind_to (count, Qnil);
11164 RESUME_POLLING;
11165 }
11166
11167
11168 /* Redisplay, but leave alone any recent echo area message unless
11169 another message has been requested in its place.
11170
11171 This is useful in situations where you need to redisplay but no
11172 user action has occurred, making it inappropriate for the message
11173 area to be cleared. See tracking_off and
11174 wait_reading_process_output for examples of these situations.
11175
11176 FROM_WHERE is an integer saying from where this function was
11177 called. This is useful for debugging. */
11178
11179 void
11180 redisplay_preserve_echo_area (from_where)
11181 int from_where;
11182 {
11183 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11184
11185 if (!NILP (echo_area_buffer[1]))
11186 {
11187 /* We have a previously displayed message, but no current
11188 message. Redisplay the previous message. */
11189 display_last_displayed_message_p = 1;
11190 redisplay_internal (1);
11191 display_last_displayed_message_p = 0;
11192 }
11193 else
11194 redisplay_internal (1);
11195
11196 if (rif != NULL && rif->flush_display_optional)
11197 rif->flush_display_optional (NULL);
11198 }
11199
11200
11201 /* Function registered with record_unwind_protect in
11202 redisplay_internal. Reset redisplaying_p to the value it had
11203 before redisplay_internal was called, and clear
11204 prevent_freeing_realized_faces_p. It also selects the previously
11205 selected frame. */
11206
11207 static Lisp_Object
11208 unwind_redisplay (val)
11209 Lisp_Object val;
11210 {
11211 Lisp_Object old_redisplaying_p, old_frame;
11212
11213 old_redisplaying_p = XCAR (val);
11214 redisplaying_p = XFASTINT (old_redisplaying_p);
11215 old_frame = XCDR (val);
11216 if (! EQ (old_frame, selected_frame))
11217 select_frame_for_redisplay (old_frame);
11218 return Qnil;
11219 }
11220
11221
11222 /* Mark the display of window W as accurate or inaccurate. If
11223 ACCURATE_P is non-zero mark display of W as accurate. If
11224 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11225 redisplay_internal is called. */
11226
11227 static void
11228 mark_window_display_accurate_1 (w, accurate_p)
11229 struct window *w;
11230 int accurate_p;
11231 {
11232 if (BUFFERP (w->buffer))
11233 {
11234 struct buffer *b = XBUFFER (w->buffer);
11235
11236 w->last_modified
11237 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11238 w->last_overlay_modified
11239 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11240 w->last_had_star
11241 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11242
11243 if (accurate_p)
11244 {
11245 b->clip_changed = 0;
11246 b->prevent_redisplay_optimizations_p = 0;
11247
11248 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11249 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11250 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11251 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11252
11253 w->current_matrix->buffer = b;
11254 w->current_matrix->begv = BUF_BEGV (b);
11255 w->current_matrix->zv = BUF_ZV (b);
11256
11257 w->last_cursor = w->cursor;
11258 w->last_cursor_off_p = w->cursor_off_p;
11259
11260 if (w == XWINDOW (selected_window))
11261 w->last_point = make_number (BUF_PT (b));
11262 else
11263 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11264 }
11265 }
11266
11267 if (accurate_p)
11268 {
11269 w->window_end_valid = w->buffer;
11270 #if 0 /* This is incorrect with variable-height lines. */
11271 xassert (XINT (w->window_end_vpos)
11272 < (WINDOW_TOTAL_LINES (w)
11273 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11274 #endif
11275 w->update_mode_line = Qnil;
11276 }
11277 }
11278
11279
11280 /* Mark the display of windows in the window tree rooted at WINDOW as
11281 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11282 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11283 be redisplayed the next time redisplay_internal is called. */
11284
11285 void
11286 mark_window_display_accurate (window, accurate_p)
11287 Lisp_Object window;
11288 int accurate_p;
11289 {
11290 struct window *w;
11291
11292 for (; !NILP (window); window = w->next)
11293 {
11294 w = XWINDOW (window);
11295 mark_window_display_accurate_1 (w, accurate_p);
11296
11297 if (!NILP (w->vchild))
11298 mark_window_display_accurate (w->vchild, accurate_p);
11299 if (!NILP (w->hchild))
11300 mark_window_display_accurate (w->hchild, accurate_p);
11301 }
11302
11303 if (accurate_p)
11304 {
11305 update_overlay_arrows (1);
11306 }
11307 else
11308 {
11309 /* Force a thorough redisplay the next time by setting
11310 last_arrow_position and last_arrow_string to t, which is
11311 unequal to any useful value of Voverlay_arrow_... */
11312 update_overlay_arrows (-1);
11313 }
11314 }
11315
11316
11317 /* Return value in display table DP (Lisp_Char_Table *) for character
11318 C. Since a display table doesn't have any parent, we don't have to
11319 follow parent. Do not call this function directly but use the
11320 macro DISP_CHAR_VECTOR. */
11321
11322 Lisp_Object
11323 disp_char_vector (dp, c)
11324 struct Lisp_Char_Table *dp;
11325 int c;
11326 {
11327 int code[4], i;
11328 Lisp_Object val;
11329
11330 if (SINGLE_BYTE_CHAR_P (c))
11331 return (dp->contents[c]);
11332
11333 SPLIT_CHAR (c, code[0], code[1], code[2]);
11334 if (code[1] < 32)
11335 code[1] = -1;
11336 else if (code[2] < 32)
11337 code[2] = -1;
11338
11339 /* Here, the possible range of code[0] (== charset ID) is
11340 128..max_charset. Since the top level char table contains data
11341 for multibyte characters after 256th element, we must increment
11342 code[0] by 128 to get a correct index. */
11343 code[0] += 128;
11344 code[3] = -1; /* anchor */
11345
11346 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11347 {
11348 val = dp->contents[code[i]];
11349 if (!SUB_CHAR_TABLE_P (val))
11350 return (NILP (val) ? dp->defalt : val);
11351 }
11352
11353 /* Here, val is a sub char table. We return the default value of
11354 it. */
11355 return (dp->defalt);
11356 }
11357
11358
11359 \f
11360 /***********************************************************************
11361 Window Redisplay
11362 ***********************************************************************/
11363
11364 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11365
11366 static void
11367 redisplay_windows (window)
11368 Lisp_Object window;
11369 {
11370 while (!NILP (window))
11371 {
11372 struct window *w = XWINDOW (window);
11373
11374 if (!NILP (w->hchild))
11375 redisplay_windows (w->hchild);
11376 else if (!NILP (w->vchild))
11377 redisplay_windows (w->vchild);
11378 else
11379 {
11380 displayed_buffer = XBUFFER (w->buffer);
11381 /* Use list_of_error, not Qerror, so that
11382 we catch only errors and don't run the debugger. */
11383 internal_condition_case_1 (redisplay_window_0, window,
11384 list_of_error,
11385 redisplay_window_error);
11386 }
11387
11388 window = w->next;
11389 }
11390 }
11391
11392 static Lisp_Object
11393 redisplay_window_error ()
11394 {
11395 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11396 return Qnil;
11397 }
11398
11399 static Lisp_Object
11400 redisplay_window_0 (window)
11401 Lisp_Object window;
11402 {
11403 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11404 redisplay_window (window, 0);
11405 return Qnil;
11406 }
11407
11408 static Lisp_Object
11409 redisplay_window_1 (window)
11410 Lisp_Object window;
11411 {
11412 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11413 redisplay_window (window, 1);
11414 return Qnil;
11415 }
11416 \f
11417
11418 /* Increment GLYPH until it reaches END or CONDITION fails while
11419 adding (GLYPH)->pixel_width to X. */
11420
11421 #define SKIP_GLYPHS(glyph, end, x, condition) \
11422 do \
11423 { \
11424 (x) += (glyph)->pixel_width; \
11425 ++(glyph); \
11426 } \
11427 while ((glyph) < (end) && (condition))
11428
11429
11430 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11431 DELTA is the number of bytes by which positions recorded in ROW
11432 differ from current buffer positions. */
11433
11434 void
11435 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11436 struct window *w;
11437 struct glyph_row *row;
11438 struct glyph_matrix *matrix;
11439 int delta, delta_bytes, dy, dvpos;
11440 {
11441 struct glyph *glyph = row->glyphs[TEXT_AREA];
11442 struct glyph *end = glyph + row->used[TEXT_AREA];
11443 struct glyph *cursor = NULL;
11444 /* The first glyph that starts a sequence of glyphs from string. */
11445 struct glyph *string_start;
11446 /* The X coordinate of string_start. */
11447 int string_start_x;
11448 /* The last known character position. */
11449 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11450 /* The last known character position before string_start. */
11451 int string_before_pos;
11452 int x = row->x;
11453 int cursor_x = x;
11454 int cursor_from_overlay_pos = 0;
11455 int pt_old = PT - delta;
11456
11457 /* Skip over glyphs not having an object at the start of the row.
11458 These are special glyphs like truncation marks on terminal
11459 frames. */
11460 if (row->displays_text_p)
11461 while (glyph < end
11462 && INTEGERP (glyph->object)
11463 && glyph->charpos < 0)
11464 {
11465 x += glyph->pixel_width;
11466 ++glyph;
11467 }
11468
11469 string_start = NULL;
11470 while (glyph < end
11471 && !INTEGERP (glyph->object)
11472 && (!BUFFERP (glyph->object)
11473 || (last_pos = glyph->charpos) < pt_old))
11474 {
11475 if (! STRINGP (glyph->object))
11476 {
11477 string_start = NULL;
11478 x += glyph->pixel_width;
11479 ++glyph;
11480 if (cursor_from_overlay_pos
11481 && last_pos > cursor_from_overlay_pos)
11482 {
11483 cursor_from_overlay_pos = 0;
11484 cursor = 0;
11485 }
11486 }
11487 else
11488 {
11489 string_before_pos = last_pos;
11490 string_start = glyph;
11491 string_start_x = x;
11492 /* Skip all glyphs from string. */
11493 do
11494 {
11495 int pos;
11496 if ((cursor == NULL || glyph > cursor)
11497 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11498 Qcursor, (glyph)->object))
11499 && (pos = string_buffer_position (w, glyph->object,
11500 string_before_pos),
11501 (pos == 0 /* From overlay */
11502 || pos == pt_old)))
11503 {
11504 /* Estimate overlay buffer position from the buffer
11505 positions of the glyphs before and after the overlay.
11506 Add 1 to last_pos so that if point corresponds to the
11507 glyph right after the overlay, we still use a 'cursor'
11508 property found in that overlay. */
11509 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11510 cursor = glyph;
11511 cursor_x = x;
11512 }
11513 x += glyph->pixel_width;
11514 ++glyph;
11515 }
11516 while (glyph < end && STRINGP (glyph->object));
11517 }
11518 }
11519
11520 if (cursor != NULL)
11521 {
11522 glyph = cursor;
11523 x = cursor_x;
11524 }
11525 else if (row->ends_in_ellipsis_p && glyph == end)
11526 {
11527 /* Scan back over the ellipsis glyphs, decrementing positions. */
11528 while (glyph > row->glyphs[TEXT_AREA]
11529 && (glyph - 1)->charpos == last_pos)
11530 glyph--, x -= glyph->pixel_width;
11531 /* That loop always goes one position too far,
11532 including the glyph before the ellipsis.
11533 So scan forward over that one. */
11534 x += glyph->pixel_width;
11535 glyph++;
11536 }
11537 else if (string_start
11538 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11539 {
11540 /* We may have skipped over point because the previous glyphs
11541 are from string. As there's no easy way to know the
11542 character position of the current glyph, find the correct
11543 glyph on point by scanning from string_start again. */
11544 Lisp_Object limit;
11545 Lisp_Object string;
11546 int pos;
11547
11548 limit = make_number (pt_old + 1);
11549 end = glyph;
11550 glyph = string_start;
11551 x = string_start_x;
11552 string = glyph->object;
11553 pos = string_buffer_position (w, string, string_before_pos);
11554 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11555 because we always put cursor after overlay strings. */
11556 while (pos == 0 && glyph < end)
11557 {
11558 string = glyph->object;
11559 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11560 if (glyph < end)
11561 pos = string_buffer_position (w, glyph->object, string_before_pos);
11562 }
11563
11564 while (glyph < end)
11565 {
11566 pos = XINT (Fnext_single_char_property_change
11567 (make_number (pos), Qdisplay, Qnil, limit));
11568 if (pos > pt_old)
11569 break;
11570 /* Skip glyphs from the same string. */
11571 string = glyph->object;
11572 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11573 /* Skip glyphs from an overlay. */
11574 while (glyph < end
11575 && ! string_buffer_position (w, glyph->object, pos))
11576 {
11577 string = glyph->object;
11578 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11579 }
11580 }
11581 }
11582
11583 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11584 w->cursor.x = x;
11585 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11586 w->cursor.y = row->y + dy;
11587
11588 if (w == XWINDOW (selected_window))
11589 {
11590 if (!row->continued_p
11591 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11592 && row->x == 0)
11593 {
11594 this_line_buffer = XBUFFER (w->buffer);
11595
11596 CHARPOS (this_line_start_pos)
11597 = MATRIX_ROW_START_CHARPOS (row) + delta;
11598 BYTEPOS (this_line_start_pos)
11599 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11600
11601 CHARPOS (this_line_end_pos)
11602 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11603 BYTEPOS (this_line_end_pos)
11604 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11605
11606 this_line_y = w->cursor.y;
11607 this_line_pixel_height = row->height;
11608 this_line_vpos = w->cursor.vpos;
11609 this_line_start_x = row->x;
11610 }
11611 else
11612 CHARPOS (this_line_start_pos) = 0;
11613 }
11614 }
11615
11616
11617 /* Run window scroll functions, if any, for WINDOW with new window
11618 start STARTP. Sets the window start of WINDOW to that position.
11619
11620 We assume that the window's buffer is really current. */
11621
11622 static INLINE struct text_pos
11623 run_window_scroll_functions (window, startp)
11624 Lisp_Object window;
11625 struct text_pos startp;
11626 {
11627 struct window *w = XWINDOW (window);
11628 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11629
11630 if (current_buffer != XBUFFER (w->buffer))
11631 abort ();
11632
11633 if (!NILP (Vwindow_scroll_functions))
11634 {
11635 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11636 make_number (CHARPOS (startp)));
11637 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11638 /* In case the hook functions switch buffers. */
11639 if (current_buffer != XBUFFER (w->buffer))
11640 set_buffer_internal_1 (XBUFFER (w->buffer));
11641 }
11642
11643 return startp;
11644 }
11645
11646
11647 /* Make sure the line containing the cursor is fully visible.
11648 A value of 1 means there is nothing to be done.
11649 (Either the line is fully visible, or it cannot be made so,
11650 or we cannot tell.)
11651
11652 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11653 is higher than window.
11654
11655 A value of 0 means the caller should do scrolling
11656 as if point had gone off the screen. */
11657
11658 static int
11659 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11660 struct window *w;
11661 int force_p;
11662 int current_matrix_p;
11663 {
11664 struct glyph_matrix *matrix;
11665 struct glyph_row *row;
11666 int window_height;
11667
11668 if (!make_cursor_line_fully_visible_p)
11669 return 1;
11670
11671 /* It's not always possible to find the cursor, e.g, when a window
11672 is full of overlay strings. Don't do anything in that case. */
11673 if (w->cursor.vpos < 0)
11674 return 1;
11675
11676 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11677 row = MATRIX_ROW (matrix, w->cursor.vpos);
11678
11679 /* If the cursor row is not partially visible, there's nothing to do. */
11680 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11681 return 1;
11682
11683 /* If the row the cursor is in is taller than the window's height,
11684 it's not clear what to do, so do nothing. */
11685 window_height = window_box_height (w);
11686 if (row->height >= window_height)
11687 {
11688 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11689 return 1;
11690 }
11691 return 0;
11692
11693 #if 0
11694 /* This code used to try to scroll the window just enough to make
11695 the line visible. It returned 0 to say that the caller should
11696 allocate larger glyph matrices. */
11697
11698 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11699 {
11700 int dy = row->height - row->visible_height;
11701 w->vscroll = 0;
11702 w->cursor.y += dy;
11703 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11704 }
11705 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11706 {
11707 int dy = - (row->height - row->visible_height);
11708 w->vscroll = dy;
11709 w->cursor.y += dy;
11710 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11711 }
11712
11713 /* When we change the cursor y-position of the selected window,
11714 change this_line_y as well so that the display optimization for
11715 the cursor line of the selected window in redisplay_internal uses
11716 the correct y-position. */
11717 if (w == XWINDOW (selected_window))
11718 this_line_y = w->cursor.y;
11719
11720 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11721 redisplay with larger matrices. */
11722 if (matrix->nrows < required_matrix_height (w))
11723 {
11724 fonts_changed_p = 1;
11725 return 0;
11726 }
11727
11728 return 1;
11729 #endif /* 0 */
11730 }
11731
11732
11733 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11734 non-zero means only WINDOW is redisplayed in redisplay_internal.
11735 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11736 in redisplay_window to bring a partially visible line into view in
11737 the case that only the cursor has moved.
11738
11739 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11740 last screen line's vertical height extends past the end of the screen.
11741
11742 Value is
11743
11744 1 if scrolling succeeded
11745
11746 0 if scrolling didn't find point.
11747
11748 -1 if new fonts have been loaded so that we must interrupt
11749 redisplay, adjust glyph matrices, and try again. */
11750
11751 enum
11752 {
11753 SCROLLING_SUCCESS,
11754 SCROLLING_FAILED,
11755 SCROLLING_NEED_LARGER_MATRICES
11756 };
11757
11758 static int
11759 try_scrolling (window, just_this_one_p, scroll_conservatively,
11760 scroll_step, temp_scroll_step, last_line_misfit)
11761 Lisp_Object window;
11762 int just_this_one_p;
11763 EMACS_INT scroll_conservatively, scroll_step;
11764 int temp_scroll_step;
11765 int last_line_misfit;
11766 {
11767 struct window *w = XWINDOW (window);
11768 struct frame *f = XFRAME (w->frame);
11769 struct text_pos scroll_margin_pos;
11770 struct text_pos pos;
11771 struct text_pos startp;
11772 struct it it;
11773 Lisp_Object window_end;
11774 int this_scroll_margin;
11775 int dy = 0;
11776 int scroll_max;
11777 int rc;
11778 int amount_to_scroll = 0;
11779 Lisp_Object aggressive;
11780 int height;
11781 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11782
11783 #if GLYPH_DEBUG
11784 debug_method_add (w, "try_scrolling");
11785 #endif
11786
11787 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11788
11789 /* Compute scroll margin height in pixels. We scroll when point is
11790 within this distance from the top or bottom of the window. */
11791 if (scroll_margin > 0)
11792 {
11793 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11794 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11795 }
11796 else
11797 this_scroll_margin = 0;
11798
11799 /* Force scroll_conservatively to have a reasonable value so it doesn't
11800 cause an overflow while computing how much to scroll. */
11801 if (scroll_conservatively)
11802 scroll_conservatively = min (scroll_conservatively,
11803 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11804
11805 /* Compute how much we should try to scroll maximally to bring point
11806 into view. */
11807 if (scroll_step || scroll_conservatively || temp_scroll_step)
11808 scroll_max = max (scroll_step,
11809 max (scroll_conservatively, temp_scroll_step));
11810 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11811 || NUMBERP (current_buffer->scroll_up_aggressively))
11812 /* We're trying to scroll because of aggressive scrolling
11813 but no scroll_step is set. Choose an arbitrary one. Maybe
11814 there should be a variable for this. */
11815 scroll_max = 10;
11816 else
11817 scroll_max = 0;
11818 scroll_max *= FRAME_LINE_HEIGHT (f);
11819
11820 /* Decide whether we have to scroll down. Start at the window end
11821 and move this_scroll_margin up to find the position of the scroll
11822 margin. */
11823 window_end = Fwindow_end (window, Qt);
11824
11825 too_near_end:
11826
11827 CHARPOS (scroll_margin_pos) = XINT (window_end);
11828 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11829
11830 if (this_scroll_margin || extra_scroll_margin_lines)
11831 {
11832 start_display (&it, w, scroll_margin_pos);
11833 if (this_scroll_margin)
11834 move_it_vertically_backward (&it, this_scroll_margin);
11835 if (extra_scroll_margin_lines)
11836 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11837 scroll_margin_pos = it.current.pos;
11838 }
11839
11840 if (PT >= CHARPOS (scroll_margin_pos))
11841 {
11842 int y0;
11843
11844 /* Point is in the scroll margin at the bottom of the window, or
11845 below. Compute a new window start that makes point visible. */
11846
11847 /* Compute the distance from the scroll margin to PT.
11848 Give up if the distance is greater than scroll_max. */
11849 start_display (&it, w, scroll_margin_pos);
11850 y0 = it.current_y;
11851 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11852 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11853
11854 /* To make point visible, we have to move the window start
11855 down so that the line the cursor is in is visible, which
11856 means we have to add in the height of the cursor line. */
11857 dy = line_bottom_y (&it) - y0;
11858
11859 if (dy > scroll_max)
11860 return SCROLLING_FAILED;
11861
11862 /* Move the window start down. If scrolling conservatively,
11863 move it just enough down to make point visible. If
11864 scroll_step is set, move it down by scroll_step. */
11865 start_display (&it, w, startp);
11866
11867 if (scroll_conservatively)
11868 /* Set AMOUNT_TO_SCROLL to at least one line,
11869 and at most scroll_conservatively lines. */
11870 amount_to_scroll
11871 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11872 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11873 else if (scroll_step || temp_scroll_step)
11874 amount_to_scroll = scroll_max;
11875 else
11876 {
11877 aggressive = current_buffer->scroll_up_aggressively;
11878 height = WINDOW_BOX_TEXT_HEIGHT (w);
11879 if (NUMBERP (aggressive))
11880 {
11881 double float_amount = XFLOATINT (aggressive) * height;
11882 amount_to_scroll = float_amount;
11883 if (amount_to_scroll == 0 && float_amount > 0)
11884 amount_to_scroll = 1;
11885 }
11886 }
11887
11888 if (amount_to_scroll <= 0)
11889 return SCROLLING_FAILED;
11890
11891 /* If moving by amount_to_scroll leaves STARTP unchanged,
11892 move it down one screen line. */
11893
11894 move_it_vertically (&it, amount_to_scroll);
11895 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11896 move_it_by_lines (&it, 1, 1);
11897 startp = it.current.pos;
11898 }
11899 else
11900 {
11901 /* See if point is inside the scroll margin at the top of the
11902 window. */
11903 scroll_margin_pos = startp;
11904 if (this_scroll_margin)
11905 {
11906 start_display (&it, w, startp);
11907 move_it_vertically (&it, this_scroll_margin);
11908 scroll_margin_pos = it.current.pos;
11909 }
11910
11911 if (PT < CHARPOS (scroll_margin_pos))
11912 {
11913 /* Point is in the scroll margin at the top of the window or
11914 above what is displayed in the window. */
11915 int y0;
11916
11917 /* Compute the vertical distance from PT to the scroll
11918 margin position. Give up if distance is greater than
11919 scroll_max. */
11920 SET_TEXT_POS (pos, PT, PT_BYTE);
11921 start_display (&it, w, pos);
11922 y0 = it.current_y;
11923 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11924 it.last_visible_y, -1,
11925 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11926 dy = it.current_y - y0;
11927 if (dy > scroll_max)
11928 return SCROLLING_FAILED;
11929
11930 /* Compute new window start. */
11931 start_display (&it, w, startp);
11932
11933 if (scroll_conservatively)
11934 amount_to_scroll
11935 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11936 else if (scroll_step || temp_scroll_step)
11937 amount_to_scroll = scroll_max;
11938 else
11939 {
11940 aggressive = current_buffer->scroll_down_aggressively;
11941 height = WINDOW_BOX_TEXT_HEIGHT (w);
11942 if (NUMBERP (aggressive))
11943 {
11944 double float_amount = XFLOATINT (aggressive) * height;
11945 amount_to_scroll = float_amount;
11946 if (amount_to_scroll == 0 && float_amount > 0)
11947 amount_to_scroll = 1;
11948 }
11949 }
11950
11951 if (amount_to_scroll <= 0)
11952 return SCROLLING_FAILED;
11953
11954 move_it_vertically_backward (&it, amount_to_scroll);
11955 startp = it.current.pos;
11956 }
11957 }
11958
11959 /* Run window scroll functions. */
11960 startp = run_window_scroll_functions (window, startp);
11961
11962 /* Display the window. Give up if new fonts are loaded, or if point
11963 doesn't appear. */
11964 if (!try_window (window, startp, 0))
11965 rc = SCROLLING_NEED_LARGER_MATRICES;
11966 else if (w->cursor.vpos < 0)
11967 {
11968 clear_glyph_matrix (w->desired_matrix);
11969 rc = SCROLLING_FAILED;
11970 }
11971 else
11972 {
11973 /* Maybe forget recorded base line for line number display. */
11974 if (!just_this_one_p
11975 || current_buffer->clip_changed
11976 || BEG_UNCHANGED < CHARPOS (startp))
11977 w->base_line_number = Qnil;
11978
11979 /* If cursor ends up on a partially visible line,
11980 treat that as being off the bottom of the screen. */
11981 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11982 {
11983 clear_glyph_matrix (w->desired_matrix);
11984 ++extra_scroll_margin_lines;
11985 goto too_near_end;
11986 }
11987 rc = SCROLLING_SUCCESS;
11988 }
11989
11990 return rc;
11991 }
11992
11993
11994 /* Compute a suitable window start for window W if display of W starts
11995 on a continuation line. Value is non-zero if a new window start
11996 was computed.
11997
11998 The new window start will be computed, based on W's width, starting
11999 from the start of the continued line. It is the start of the
12000 screen line with the minimum distance from the old start W->start. */
12001
12002 static int
12003 compute_window_start_on_continuation_line (w)
12004 struct window *w;
12005 {
12006 struct text_pos pos, start_pos;
12007 int window_start_changed_p = 0;
12008
12009 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12010
12011 /* If window start is on a continuation line... Window start may be
12012 < BEGV in case there's invisible text at the start of the
12013 buffer (M-x rmail, for example). */
12014 if (CHARPOS (start_pos) > BEGV
12015 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12016 {
12017 struct it it;
12018 struct glyph_row *row;
12019
12020 /* Handle the case that the window start is out of range. */
12021 if (CHARPOS (start_pos) < BEGV)
12022 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12023 else if (CHARPOS (start_pos) > ZV)
12024 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12025
12026 /* Find the start of the continued line. This should be fast
12027 because scan_buffer is fast (newline cache). */
12028 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12029 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12030 row, DEFAULT_FACE_ID);
12031 reseat_at_previous_visible_line_start (&it);
12032
12033 /* If the line start is "too far" away from the window start,
12034 say it takes too much time to compute a new window start. */
12035 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12036 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12037 {
12038 int min_distance, distance;
12039
12040 /* Move forward by display lines to find the new window
12041 start. If window width was enlarged, the new start can
12042 be expected to be > the old start. If window width was
12043 decreased, the new window start will be < the old start.
12044 So, we're looking for the display line start with the
12045 minimum distance from the old window start. */
12046 pos = it.current.pos;
12047 min_distance = INFINITY;
12048 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12049 distance < min_distance)
12050 {
12051 min_distance = distance;
12052 pos = it.current.pos;
12053 move_it_by_lines (&it, 1, 0);
12054 }
12055
12056 /* Set the window start there. */
12057 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12058 window_start_changed_p = 1;
12059 }
12060 }
12061
12062 return window_start_changed_p;
12063 }
12064
12065
12066 /* Try cursor movement in case text has not changed in window WINDOW,
12067 with window start STARTP. Value is
12068
12069 CURSOR_MOVEMENT_SUCCESS if successful
12070
12071 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12072
12073 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12074 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12075 we want to scroll as if scroll-step were set to 1. See the code.
12076
12077 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12078 which case we have to abort this redisplay, and adjust matrices
12079 first. */
12080
12081 enum
12082 {
12083 CURSOR_MOVEMENT_SUCCESS,
12084 CURSOR_MOVEMENT_CANNOT_BE_USED,
12085 CURSOR_MOVEMENT_MUST_SCROLL,
12086 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12087 };
12088
12089 static int
12090 try_cursor_movement (window, startp, scroll_step)
12091 Lisp_Object window;
12092 struct text_pos startp;
12093 int *scroll_step;
12094 {
12095 struct window *w = XWINDOW (window);
12096 struct frame *f = XFRAME (w->frame);
12097 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12098
12099 #if GLYPH_DEBUG
12100 if (inhibit_try_cursor_movement)
12101 return rc;
12102 #endif
12103
12104 /* Handle case where text has not changed, only point, and it has
12105 not moved off the frame. */
12106 if (/* Point may be in this window. */
12107 PT >= CHARPOS (startp)
12108 /* Selective display hasn't changed. */
12109 && !current_buffer->clip_changed
12110 /* Function force-mode-line-update is used to force a thorough
12111 redisplay. It sets either windows_or_buffers_changed or
12112 update_mode_lines. So don't take a shortcut here for these
12113 cases. */
12114 && !update_mode_lines
12115 && !windows_or_buffers_changed
12116 && !cursor_type_changed
12117 /* Can't use this case if highlighting a region. When a
12118 region exists, cursor movement has to do more than just
12119 set the cursor. */
12120 && !(!NILP (Vtransient_mark_mode)
12121 && !NILP (current_buffer->mark_active))
12122 && NILP (w->region_showing)
12123 && NILP (Vshow_trailing_whitespace)
12124 /* Right after splitting windows, last_point may be nil. */
12125 && INTEGERP (w->last_point)
12126 /* This code is not used for mini-buffer for the sake of the case
12127 of redisplaying to replace an echo area message; since in
12128 that case the mini-buffer contents per se are usually
12129 unchanged. This code is of no real use in the mini-buffer
12130 since the handling of this_line_start_pos, etc., in redisplay
12131 handles the same cases. */
12132 && !EQ (window, minibuf_window)
12133 /* When splitting windows or for new windows, it happens that
12134 redisplay is called with a nil window_end_vpos or one being
12135 larger than the window. This should really be fixed in
12136 window.c. I don't have this on my list, now, so we do
12137 approximately the same as the old redisplay code. --gerd. */
12138 && INTEGERP (w->window_end_vpos)
12139 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12140 && (FRAME_WINDOW_P (f)
12141 || !overlay_arrow_in_current_buffer_p ()))
12142 {
12143 int this_scroll_margin, top_scroll_margin;
12144 struct glyph_row *row = NULL;
12145
12146 #if GLYPH_DEBUG
12147 debug_method_add (w, "cursor movement");
12148 #endif
12149
12150 /* Scroll if point within this distance from the top or bottom
12151 of the window. This is a pixel value. */
12152 this_scroll_margin = max (0, scroll_margin);
12153 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12154 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12155
12156 top_scroll_margin = this_scroll_margin;
12157 if (WINDOW_WANTS_HEADER_LINE_P (w))
12158 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12159
12160 /* Start with the row the cursor was displayed during the last
12161 not paused redisplay. Give up if that row is not valid. */
12162 if (w->last_cursor.vpos < 0
12163 || w->last_cursor.vpos >= w->current_matrix->nrows)
12164 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12165 else
12166 {
12167 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12168 if (row->mode_line_p)
12169 ++row;
12170 if (!row->enabled_p)
12171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12172 }
12173
12174 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12175 {
12176 int scroll_p = 0;
12177 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12178
12179 if (PT > XFASTINT (w->last_point))
12180 {
12181 /* Point has moved forward. */
12182 while (MATRIX_ROW_END_CHARPOS (row) < PT
12183 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12184 {
12185 xassert (row->enabled_p);
12186 ++row;
12187 }
12188
12189 /* The end position of a row equals the start position
12190 of the next row. If PT is there, we would rather
12191 display it in the next line. */
12192 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12193 && MATRIX_ROW_END_CHARPOS (row) == PT
12194 && !cursor_row_p (w, row))
12195 ++row;
12196
12197 /* If within the scroll margin, scroll. Note that
12198 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12199 the next line would be drawn, and that
12200 this_scroll_margin can be zero. */
12201 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12202 || PT > MATRIX_ROW_END_CHARPOS (row)
12203 /* Line is completely visible last line in window
12204 and PT is to be set in the next line. */
12205 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12206 && PT == MATRIX_ROW_END_CHARPOS (row)
12207 && !row->ends_at_zv_p
12208 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12209 scroll_p = 1;
12210 }
12211 else if (PT < XFASTINT (w->last_point))
12212 {
12213 /* Cursor has to be moved backward. Note that PT >=
12214 CHARPOS (startp) because of the outer if-statement. */
12215 while (!row->mode_line_p
12216 && (MATRIX_ROW_START_CHARPOS (row) > PT
12217 || (MATRIX_ROW_START_CHARPOS (row) == PT
12218 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12219 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12220 row > w->current_matrix->rows
12221 && (row-1)->ends_in_newline_from_string_p))))
12222 && (row->y > top_scroll_margin
12223 || CHARPOS (startp) == BEGV))
12224 {
12225 xassert (row->enabled_p);
12226 --row;
12227 }
12228
12229 /* Consider the following case: Window starts at BEGV,
12230 there is invisible, intangible text at BEGV, so that
12231 display starts at some point START > BEGV. It can
12232 happen that we are called with PT somewhere between
12233 BEGV and START. Try to handle that case. */
12234 if (row < w->current_matrix->rows
12235 || row->mode_line_p)
12236 {
12237 row = w->current_matrix->rows;
12238 if (row->mode_line_p)
12239 ++row;
12240 }
12241
12242 /* Due to newlines in overlay strings, we may have to
12243 skip forward over overlay strings. */
12244 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12245 && MATRIX_ROW_END_CHARPOS (row) == PT
12246 && !cursor_row_p (w, row))
12247 ++row;
12248
12249 /* If within the scroll margin, scroll. */
12250 if (row->y < top_scroll_margin
12251 && CHARPOS (startp) != BEGV)
12252 scroll_p = 1;
12253 }
12254 else
12255 {
12256 /* Cursor did not move. So don't scroll even if cursor line
12257 is partially visible, as it was so before. */
12258 rc = CURSOR_MOVEMENT_SUCCESS;
12259 }
12260
12261 if (PT < MATRIX_ROW_START_CHARPOS (row)
12262 || PT > MATRIX_ROW_END_CHARPOS (row))
12263 {
12264 /* if PT is not in the glyph row, give up. */
12265 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12266 }
12267 else if (rc != CURSOR_MOVEMENT_SUCCESS
12268 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12269 && make_cursor_line_fully_visible_p)
12270 {
12271 if (PT == MATRIX_ROW_END_CHARPOS (row)
12272 && !row->ends_at_zv_p
12273 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12274 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12275 else if (row->height > window_box_height (w))
12276 {
12277 /* If we end up in a partially visible line, let's
12278 make it fully visible, except when it's taller
12279 than the window, in which case we can't do much
12280 about it. */
12281 *scroll_step = 1;
12282 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12283 }
12284 else
12285 {
12286 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12287 if (!cursor_row_fully_visible_p (w, 0, 1))
12288 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12289 else
12290 rc = CURSOR_MOVEMENT_SUCCESS;
12291 }
12292 }
12293 else if (scroll_p)
12294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12295 else
12296 {
12297 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12298 rc = CURSOR_MOVEMENT_SUCCESS;
12299 }
12300 }
12301 }
12302
12303 return rc;
12304 }
12305
12306 void
12307 set_vertical_scroll_bar (w)
12308 struct window *w;
12309 {
12310 int start, end, whole;
12311
12312 /* Calculate the start and end positions for the current window.
12313 At some point, it would be nice to choose between scrollbars
12314 which reflect the whole buffer size, with special markers
12315 indicating narrowing, and scrollbars which reflect only the
12316 visible region.
12317
12318 Note that mini-buffers sometimes aren't displaying any text. */
12319 if (!MINI_WINDOW_P (w)
12320 || (w == XWINDOW (minibuf_window)
12321 && NILP (echo_area_buffer[0])))
12322 {
12323 struct buffer *buf = XBUFFER (w->buffer);
12324 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12325 start = marker_position (w->start) - BUF_BEGV (buf);
12326 /* I don't think this is guaranteed to be right. For the
12327 moment, we'll pretend it is. */
12328 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12329
12330 if (end < start)
12331 end = start;
12332 if (whole < (end - start))
12333 whole = end - start;
12334 }
12335 else
12336 start = end = whole = 0;
12337
12338 /* Indicate what this scroll bar ought to be displaying now. */
12339 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12340 }
12341
12342
12343 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12344 selected_window is redisplayed.
12345
12346 We can return without actually redisplaying the window if
12347 fonts_changed_p is nonzero. In that case, redisplay_internal will
12348 retry. */
12349
12350 static void
12351 redisplay_window (window, just_this_one_p)
12352 Lisp_Object window;
12353 int just_this_one_p;
12354 {
12355 struct window *w = XWINDOW (window);
12356 struct frame *f = XFRAME (w->frame);
12357 struct buffer *buffer = XBUFFER (w->buffer);
12358 struct buffer *old = current_buffer;
12359 struct text_pos lpoint, opoint, startp;
12360 int update_mode_line;
12361 int tem;
12362 struct it it;
12363 /* Record it now because it's overwritten. */
12364 int current_matrix_up_to_date_p = 0;
12365 int used_current_matrix_p = 0;
12366 /* This is less strict than current_matrix_up_to_date_p.
12367 It indictes that the buffer contents and narrowing are unchanged. */
12368 int buffer_unchanged_p = 0;
12369 int temp_scroll_step = 0;
12370 int count = SPECPDL_INDEX ();
12371 int rc;
12372 int centering_position = -1;
12373 int last_line_misfit = 0;
12374
12375 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12376 opoint = lpoint;
12377
12378 /* W must be a leaf window here. */
12379 xassert (!NILP (w->buffer));
12380 #if GLYPH_DEBUG
12381 *w->desired_matrix->method = 0;
12382 #endif
12383
12384 specbind (Qinhibit_point_motion_hooks, Qt);
12385
12386 reconsider_clip_changes (w, buffer);
12387
12388 /* Has the mode line to be updated? */
12389 update_mode_line = (!NILP (w->update_mode_line)
12390 || update_mode_lines
12391 || buffer->clip_changed
12392 || buffer->prevent_redisplay_optimizations_p);
12393
12394 if (MINI_WINDOW_P (w))
12395 {
12396 if (w == XWINDOW (echo_area_window)
12397 && !NILP (echo_area_buffer[0]))
12398 {
12399 if (update_mode_line)
12400 /* We may have to update a tty frame's menu bar or a
12401 tool-bar. Example `M-x C-h C-h C-g'. */
12402 goto finish_menu_bars;
12403 else
12404 /* We've already displayed the echo area glyphs in this window. */
12405 goto finish_scroll_bars;
12406 }
12407 else if ((w != XWINDOW (minibuf_window)
12408 || minibuf_level == 0)
12409 /* When buffer is nonempty, redisplay window normally. */
12410 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12411 /* Quail displays non-mini buffers in minibuffer window.
12412 In that case, redisplay the window normally. */
12413 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12414 {
12415 /* W is a mini-buffer window, but it's not active, so clear
12416 it. */
12417 int yb = window_text_bottom_y (w);
12418 struct glyph_row *row;
12419 int y;
12420
12421 for (y = 0, row = w->desired_matrix->rows;
12422 y < yb;
12423 y += row->height, ++row)
12424 blank_row (w, row, y);
12425 goto finish_scroll_bars;
12426 }
12427
12428 clear_glyph_matrix (w->desired_matrix);
12429 }
12430
12431 /* Otherwise set up data on this window; select its buffer and point
12432 value. */
12433 /* Really select the buffer, for the sake of buffer-local
12434 variables. */
12435 set_buffer_internal_1 (XBUFFER (w->buffer));
12436 SET_TEXT_POS (opoint, PT, PT_BYTE);
12437
12438 current_matrix_up_to_date_p
12439 = (!NILP (w->window_end_valid)
12440 && !current_buffer->clip_changed
12441 && !current_buffer->prevent_redisplay_optimizations_p
12442 && XFASTINT (w->last_modified) >= MODIFF
12443 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12444
12445 buffer_unchanged_p
12446 = (!NILP (w->window_end_valid)
12447 && !current_buffer->clip_changed
12448 && XFASTINT (w->last_modified) >= MODIFF
12449 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12450
12451 /* When windows_or_buffers_changed is non-zero, we can't rely on
12452 the window end being valid, so set it to nil there. */
12453 if (windows_or_buffers_changed)
12454 {
12455 /* If window starts on a continuation line, maybe adjust the
12456 window start in case the window's width changed. */
12457 if (XMARKER (w->start)->buffer == current_buffer)
12458 compute_window_start_on_continuation_line (w);
12459
12460 w->window_end_valid = Qnil;
12461 }
12462
12463 /* Some sanity checks. */
12464 CHECK_WINDOW_END (w);
12465 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12466 abort ();
12467 if (BYTEPOS (opoint) < CHARPOS (opoint))
12468 abort ();
12469
12470 /* If %c is in mode line, update it if needed. */
12471 if (!NILP (w->column_number_displayed)
12472 /* This alternative quickly identifies a common case
12473 where no change is needed. */
12474 && !(PT == XFASTINT (w->last_point)
12475 && XFASTINT (w->last_modified) >= MODIFF
12476 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12477 && (XFASTINT (w->column_number_displayed)
12478 != (int) current_column ())) /* iftc */
12479 update_mode_line = 1;
12480
12481 /* Count number of windows showing the selected buffer. An indirect
12482 buffer counts as its base buffer. */
12483 if (!just_this_one_p)
12484 {
12485 struct buffer *current_base, *window_base;
12486 current_base = current_buffer;
12487 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12488 if (current_base->base_buffer)
12489 current_base = current_base->base_buffer;
12490 if (window_base->base_buffer)
12491 window_base = window_base->base_buffer;
12492 if (current_base == window_base)
12493 buffer_shared++;
12494 }
12495
12496 /* Point refers normally to the selected window. For any other
12497 window, set up appropriate value. */
12498 if (!EQ (window, selected_window))
12499 {
12500 int new_pt = XMARKER (w->pointm)->charpos;
12501 int new_pt_byte = marker_byte_position (w->pointm);
12502 if (new_pt < BEGV)
12503 {
12504 new_pt = BEGV;
12505 new_pt_byte = BEGV_BYTE;
12506 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12507 }
12508 else if (new_pt > (ZV - 1))
12509 {
12510 new_pt = ZV;
12511 new_pt_byte = ZV_BYTE;
12512 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12513 }
12514
12515 /* We don't use SET_PT so that the point-motion hooks don't run. */
12516 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12517 }
12518
12519 /* If any of the character widths specified in the display table
12520 have changed, invalidate the width run cache. It's true that
12521 this may be a bit late to catch such changes, but the rest of
12522 redisplay goes (non-fatally) haywire when the display table is
12523 changed, so why should we worry about doing any better? */
12524 if (current_buffer->width_run_cache)
12525 {
12526 struct Lisp_Char_Table *disptab = buffer_display_table ();
12527
12528 if (! disptab_matches_widthtab (disptab,
12529 XVECTOR (current_buffer->width_table)))
12530 {
12531 invalidate_region_cache (current_buffer,
12532 current_buffer->width_run_cache,
12533 BEG, Z);
12534 recompute_width_table (current_buffer, disptab);
12535 }
12536 }
12537
12538 /* If window-start is screwed up, choose a new one. */
12539 if (XMARKER (w->start)->buffer != current_buffer)
12540 goto recenter;
12541
12542 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12543
12544 /* If someone specified a new starting point but did not insist,
12545 check whether it can be used. */
12546 if (!NILP (w->optional_new_start)
12547 && CHARPOS (startp) >= BEGV
12548 && CHARPOS (startp) <= ZV)
12549 {
12550 w->optional_new_start = Qnil;
12551 start_display (&it, w, startp);
12552 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12553 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12554 if (IT_CHARPOS (it) == PT)
12555 w->force_start = Qt;
12556 /* IT may overshoot PT if text at PT is invisible. */
12557 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12558 w->force_start = Qt;
12559
12560
12561 }
12562
12563 /* Handle case where place to start displaying has been specified,
12564 unless the specified location is outside the accessible range. */
12565 if (!NILP (w->force_start)
12566 || w->frozen_window_start_p)
12567 {
12568 /* We set this later on if we have to adjust point. */
12569 int new_vpos = -1;
12570 int val;
12571
12572 w->force_start = Qnil;
12573 w->vscroll = 0;
12574 w->window_end_valid = Qnil;
12575
12576 /* Forget any recorded base line for line number display. */
12577 if (!buffer_unchanged_p)
12578 w->base_line_number = Qnil;
12579
12580 /* Redisplay the mode line. Select the buffer properly for that.
12581 Also, run the hook window-scroll-functions
12582 because we have scrolled. */
12583 /* Note, we do this after clearing force_start because
12584 if there's an error, it is better to forget about force_start
12585 than to get into an infinite loop calling the hook functions
12586 and having them get more errors. */
12587 if (!update_mode_line
12588 || ! NILP (Vwindow_scroll_functions))
12589 {
12590 update_mode_line = 1;
12591 w->update_mode_line = Qt;
12592 startp = run_window_scroll_functions (window, startp);
12593 }
12594
12595 w->last_modified = make_number (0);
12596 w->last_overlay_modified = make_number (0);
12597 if (CHARPOS (startp) < BEGV)
12598 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12599 else if (CHARPOS (startp) > ZV)
12600 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12601
12602 /* Redisplay, then check if cursor has been set during the
12603 redisplay. Give up if new fonts were loaded. */
12604 val = try_window (window, startp, 1);
12605 if (!val)
12606 {
12607 w->force_start = Qt;
12608 clear_glyph_matrix (w->desired_matrix);
12609 goto need_larger_matrices;
12610 }
12611 /* Point was outside the scroll margins. */
12612 if (val < 0)
12613 new_vpos = window_box_height (w) / 2;
12614
12615 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12616 {
12617 /* If point does not appear, try to move point so it does
12618 appear. The desired matrix has been built above, so we
12619 can use it here. */
12620 new_vpos = window_box_height (w) / 2;
12621 }
12622
12623 if (!cursor_row_fully_visible_p (w, 0, 0))
12624 {
12625 /* Point does appear, but on a line partly visible at end of window.
12626 Move it back to a fully-visible line. */
12627 new_vpos = window_box_height (w);
12628 }
12629
12630 /* If we need to move point for either of the above reasons,
12631 now actually do it. */
12632 if (new_vpos >= 0)
12633 {
12634 struct glyph_row *row;
12635
12636 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12637 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12638 ++row;
12639
12640 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12641 MATRIX_ROW_START_BYTEPOS (row));
12642
12643 if (w != XWINDOW (selected_window))
12644 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12645 else if (current_buffer == old)
12646 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12647
12648 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12649
12650 /* If we are highlighting the region, then we just changed
12651 the region, so redisplay to show it. */
12652 if (!NILP (Vtransient_mark_mode)
12653 && !NILP (current_buffer->mark_active))
12654 {
12655 clear_glyph_matrix (w->desired_matrix);
12656 if (!try_window (window, startp, 0))
12657 goto need_larger_matrices;
12658 }
12659 }
12660
12661 #if GLYPH_DEBUG
12662 debug_method_add (w, "forced window start");
12663 #endif
12664 goto done;
12665 }
12666
12667 /* Handle case where text has not changed, only point, and it has
12668 not moved off the frame, and we are not retrying after hscroll.
12669 (current_matrix_up_to_date_p is nonzero when retrying.) */
12670 if (current_matrix_up_to_date_p
12671 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12672 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12673 {
12674 switch (rc)
12675 {
12676 case CURSOR_MOVEMENT_SUCCESS:
12677 used_current_matrix_p = 1;
12678 goto done;
12679
12680 #if 0 /* try_cursor_movement never returns this value. */
12681 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12682 goto need_larger_matrices;
12683 #endif
12684
12685 case CURSOR_MOVEMENT_MUST_SCROLL:
12686 goto try_to_scroll;
12687
12688 default:
12689 abort ();
12690 }
12691 }
12692 /* If current starting point was originally the beginning of a line
12693 but no longer is, find a new starting point. */
12694 else if (!NILP (w->start_at_line_beg)
12695 && !(CHARPOS (startp) <= BEGV
12696 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12697 {
12698 #if GLYPH_DEBUG
12699 debug_method_add (w, "recenter 1");
12700 #endif
12701 goto recenter;
12702 }
12703
12704 /* Try scrolling with try_window_id. Value is > 0 if update has
12705 been done, it is -1 if we know that the same window start will
12706 not work. It is 0 if unsuccessful for some other reason. */
12707 else if ((tem = try_window_id (w)) != 0)
12708 {
12709 #if GLYPH_DEBUG
12710 debug_method_add (w, "try_window_id %d", tem);
12711 #endif
12712
12713 if (fonts_changed_p)
12714 goto need_larger_matrices;
12715 if (tem > 0)
12716 goto done;
12717
12718 /* Otherwise try_window_id has returned -1 which means that we
12719 don't want the alternative below this comment to execute. */
12720 }
12721 else if (CHARPOS (startp) >= BEGV
12722 && CHARPOS (startp) <= ZV
12723 && PT >= CHARPOS (startp)
12724 && (CHARPOS (startp) < ZV
12725 /* Avoid starting at end of buffer. */
12726 || CHARPOS (startp) == BEGV
12727 || (XFASTINT (w->last_modified) >= MODIFF
12728 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12729 {
12730 #if GLYPH_DEBUG
12731 debug_method_add (w, "same window start");
12732 #endif
12733
12734 /* Try to redisplay starting at same place as before.
12735 If point has not moved off frame, accept the results. */
12736 if (!current_matrix_up_to_date_p
12737 /* Don't use try_window_reusing_current_matrix in this case
12738 because a window scroll function can have changed the
12739 buffer. */
12740 || !NILP (Vwindow_scroll_functions)
12741 || MINI_WINDOW_P (w)
12742 || !(used_current_matrix_p
12743 = try_window_reusing_current_matrix (w)))
12744 {
12745 IF_DEBUG (debug_method_add (w, "1"));
12746 if (try_window (window, startp, 1) < 0)
12747 /* -1 means we need to scroll.
12748 0 means we need new matrices, but fonts_changed_p
12749 is set in that case, so we will detect it below. */
12750 goto try_to_scroll;
12751 }
12752
12753 if (fonts_changed_p)
12754 goto need_larger_matrices;
12755
12756 if (w->cursor.vpos >= 0)
12757 {
12758 if (!just_this_one_p
12759 || current_buffer->clip_changed
12760 || BEG_UNCHANGED < CHARPOS (startp))
12761 /* Forget any recorded base line for line number display. */
12762 w->base_line_number = Qnil;
12763
12764 if (!cursor_row_fully_visible_p (w, 1, 0))
12765 {
12766 clear_glyph_matrix (w->desired_matrix);
12767 last_line_misfit = 1;
12768 }
12769 /* Drop through and scroll. */
12770 else
12771 goto done;
12772 }
12773 else
12774 clear_glyph_matrix (w->desired_matrix);
12775 }
12776
12777 try_to_scroll:
12778
12779 w->last_modified = make_number (0);
12780 w->last_overlay_modified = make_number (0);
12781
12782 /* Redisplay the mode line. Select the buffer properly for that. */
12783 if (!update_mode_line)
12784 {
12785 update_mode_line = 1;
12786 w->update_mode_line = Qt;
12787 }
12788
12789 /* Try to scroll by specified few lines. */
12790 if ((scroll_conservatively
12791 || scroll_step
12792 || temp_scroll_step
12793 || NUMBERP (current_buffer->scroll_up_aggressively)
12794 || NUMBERP (current_buffer->scroll_down_aggressively))
12795 && !current_buffer->clip_changed
12796 && CHARPOS (startp) >= BEGV
12797 && CHARPOS (startp) <= ZV)
12798 {
12799 /* The function returns -1 if new fonts were loaded, 1 if
12800 successful, 0 if not successful. */
12801 int rc = try_scrolling (window, just_this_one_p,
12802 scroll_conservatively,
12803 scroll_step,
12804 temp_scroll_step, last_line_misfit);
12805 switch (rc)
12806 {
12807 case SCROLLING_SUCCESS:
12808 goto done;
12809
12810 case SCROLLING_NEED_LARGER_MATRICES:
12811 goto need_larger_matrices;
12812
12813 case SCROLLING_FAILED:
12814 break;
12815
12816 default:
12817 abort ();
12818 }
12819 }
12820
12821 /* Finally, just choose place to start which centers point */
12822
12823 recenter:
12824 if (centering_position < 0)
12825 centering_position = window_box_height (w) / 2;
12826
12827 #if GLYPH_DEBUG
12828 debug_method_add (w, "recenter");
12829 #endif
12830
12831 /* w->vscroll = 0; */
12832
12833 /* Forget any previously recorded base line for line number display. */
12834 if (!buffer_unchanged_p)
12835 w->base_line_number = Qnil;
12836
12837 /* Move backward half the height of the window. */
12838 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12839 it.current_y = it.last_visible_y;
12840 move_it_vertically_backward (&it, centering_position);
12841 xassert (IT_CHARPOS (it) >= BEGV);
12842
12843 /* The function move_it_vertically_backward may move over more
12844 than the specified y-distance. If it->w is small, e.g. a
12845 mini-buffer window, we may end up in front of the window's
12846 display area. Start displaying at the start of the line
12847 containing PT in this case. */
12848 if (it.current_y <= 0)
12849 {
12850 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12851 move_it_vertically_backward (&it, 0);
12852 #if 0
12853 /* I think this assert is bogus if buffer contains
12854 invisible text or images. KFS. */
12855 xassert (IT_CHARPOS (it) <= PT);
12856 #endif
12857 it.current_y = 0;
12858 }
12859
12860 it.current_x = it.hpos = 0;
12861
12862 /* Set startp here explicitly in case that helps avoid an infinite loop
12863 in case the window-scroll-functions functions get errors. */
12864 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12865
12866 /* Run scroll hooks. */
12867 startp = run_window_scroll_functions (window, it.current.pos);
12868
12869 /* Redisplay the window. */
12870 if (!current_matrix_up_to_date_p
12871 || windows_or_buffers_changed
12872 || cursor_type_changed
12873 /* Don't use try_window_reusing_current_matrix in this case
12874 because it can have changed the buffer. */
12875 || !NILP (Vwindow_scroll_functions)
12876 || !just_this_one_p
12877 || MINI_WINDOW_P (w)
12878 || !(used_current_matrix_p
12879 = try_window_reusing_current_matrix (w)))
12880 try_window (window, startp, 0);
12881
12882 /* If new fonts have been loaded (due to fontsets), give up. We
12883 have to start a new redisplay since we need to re-adjust glyph
12884 matrices. */
12885 if (fonts_changed_p)
12886 goto need_larger_matrices;
12887
12888 /* If cursor did not appear assume that the middle of the window is
12889 in the first line of the window. Do it again with the next line.
12890 (Imagine a window of height 100, displaying two lines of height
12891 60. Moving back 50 from it->last_visible_y will end in the first
12892 line.) */
12893 if (w->cursor.vpos < 0)
12894 {
12895 if (!NILP (w->window_end_valid)
12896 && PT >= Z - XFASTINT (w->window_end_pos))
12897 {
12898 clear_glyph_matrix (w->desired_matrix);
12899 move_it_by_lines (&it, 1, 0);
12900 try_window (window, it.current.pos, 0);
12901 }
12902 else if (PT < IT_CHARPOS (it))
12903 {
12904 clear_glyph_matrix (w->desired_matrix);
12905 move_it_by_lines (&it, -1, 0);
12906 try_window (window, it.current.pos, 0);
12907 }
12908 else
12909 {
12910 /* Not much we can do about it. */
12911 }
12912 }
12913
12914 /* Consider the following case: Window starts at BEGV, there is
12915 invisible, intangible text at BEGV, so that display starts at
12916 some point START > BEGV. It can happen that we are called with
12917 PT somewhere between BEGV and START. Try to handle that case. */
12918 if (w->cursor.vpos < 0)
12919 {
12920 struct glyph_row *row = w->current_matrix->rows;
12921 if (row->mode_line_p)
12922 ++row;
12923 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12924 }
12925
12926 if (!cursor_row_fully_visible_p (w, 0, 0))
12927 {
12928 /* If vscroll is enabled, disable it and try again. */
12929 if (w->vscroll)
12930 {
12931 w->vscroll = 0;
12932 clear_glyph_matrix (w->desired_matrix);
12933 goto recenter;
12934 }
12935
12936 /* If centering point failed to make the whole line visible,
12937 put point at the top instead. That has to make the whole line
12938 visible, if it can be done. */
12939 if (centering_position == 0)
12940 goto done;
12941
12942 clear_glyph_matrix (w->desired_matrix);
12943 centering_position = 0;
12944 goto recenter;
12945 }
12946
12947 done:
12948
12949 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12950 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12951 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12952 ? Qt : Qnil);
12953
12954 /* Display the mode line, if we must. */
12955 if ((update_mode_line
12956 /* If window not full width, must redo its mode line
12957 if (a) the window to its side is being redone and
12958 (b) we do a frame-based redisplay. This is a consequence
12959 of how inverted lines are drawn in frame-based redisplay. */
12960 || (!just_this_one_p
12961 && !FRAME_WINDOW_P (f)
12962 && !WINDOW_FULL_WIDTH_P (w))
12963 /* Line number to display. */
12964 || INTEGERP (w->base_line_pos)
12965 /* Column number is displayed and different from the one displayed. */
12966 || (!NILP (w->column_number_displayed)
12967 && (XFASTINT (w->column_number_displayed)
12968 != (int) current_column ()))) /* iftc */
12969 /* This means that the window has a mode line. */
12970 && (WINDOW_WANTS_MODELINE_P (w)
12971 || WINDOW_WANTS_HEADER_LINE_P (w)))
12972 {
12973 display_mode_lines (w);
12974
12975 /* If mode line height has changed, arrange for a thorough
12976 immediate redisplay using the correct mode line height. */
12977 if (WINDOW_WANTS_MODELINE_P (w)
12978 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12979 {
12980 fonts_changed_p = 1;
12981 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12982 = DESIRED_MODE_LINE_HEIGHT (w);
12983 }
12984
12985 /* If top line height has changed, arrange for a thorough
12986 immediate redisplay using the correct mode line height. */
12987 if (WINDOW_WANTS_HEADER_LINE_P (w)
12988 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12989 {
12990 fonts_changed_p = 1;
12991 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12992 = DESIRED_HEADER_LINE_HEIGHT (w);
12993 }
12994
12995 if (fonts_changed_p)
12996 goto need_larger_matrices;
12997 }
12998
12999 if (!line_number_displayed
13000 && !BUFFERP (w->base_line_pos))
13001 {
13002 w->base_line_pos = Qnil;
13003 w->base_line_number = Qnil;
13004 }
13005
13006 finish_menu_bars:
13007
13008 /* When we reach a frame's selected window, redo the frame's menu bar. */
13009 if (update_mode_line
13010 && EQ (FRAME_SELECTED_WINDOW (f), window))
13011 {
13012 int redisplay_menu_p = 0;
13013 int redisplay_tool_bar_p = 0;
13014
13015 if (FRAME_WINDOW_P (f))
13016 {
13017 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13018 || defined (USE_GTK)
13019 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13020 #else
13021 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13022 #endif
13023 }
13024 else
13025 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13026
13027 if (redisplay_menu_p)
13028 display_menu_bar (w);
13029
13030 #ifdef HAVE_WINDOW_SYSTEM
13031 #ifdef USE_GTK
13032 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13033 #else
13034 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13035 && (FRAME_TOOL_BAR_LINES (f) > 0
13036 || auto_resize_tool_bars_p);
13037
13038 #endif
13039
13040 if (redisplay_tool_bar_p)
13041 redisplay_tool_bar (f);
13042 #endif
13043 }
13044
13045 #ifdef HAVE_WINDOW_SYSTEM
13046 if (FRAME_WINDOW_P (f)
13047 && update_window_fringes (w, (just_this_one_p
13048 || (!used_current_matrix_p && !overlay_arrow_seen)
13049 || w->pseudo_window_p)))
13050 {
13051 update_begin (f);
13052 BLOCK_INPUT;
13053 if (draw_window_fringes (w, 1))
13054 x_draw_vertical_border (w);
13055 UNBLOCK_INPUT;
13056 update_end (f);
13057 }
13058 #endif /* HAVE_WINDOW_SYSTEM */
13059
13060 /* We go to this label, with fonts_changed_p nonzero,
13061 if it is necessary to try again using larger glyph matrices.
13062 We have to redeem the scroll bar even in this case,
13063 because the loop in redisplay_internal expects that. */
13064 need_larger_matrices:
13065 ;
13066 finish_scroll_bars:
13067
13068 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13069 {
13070 /* Set the thumb's position and size. */
13071 set_vertical_scroll_bar (w);
13072
13073 /* Note that we actually used the scroll bar attached to this
13074 window, so it shouldn't be deleted at the end of redisplay. */
13075 redeem_scroll_bar_hook (w);
13076 }
13077
13078 /* Restore current_buffer and value of point in it. */
13079 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13080 set_buffer_internal_1 (old);
13081 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13082
13083 unbind_to (count, Qnil);
13084 }
13085
13086
13087 /* Build the complete desired matrix of WINDOW with a window start
13088 buffer position POS.
13089
13090 Value is 1 if successful. It is zero if fonts were loaded during
13091 redisplay which makes re-adjusting glyph matrices necessary, and -1
13092 if point would appear in the scroll margins.
13093 (We check that only if CHECK_MARGINS is nonzero. */
13094
13095 int
13096 try_window (window, pos, check_margins)
13097 Lisp_Object window;
13098 struct text_pos pos;
13099 int check_margins;
13100 {
13101 struct window *w = XWINDOW (window);
13102 struct it it;
13103 struct glyph_row *last_text_row = NULL;
13104
13105 /* Make POS the new window start. */
13106 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13107
13108 /* Mark cursor position as unknown. No overlay arrow seen. */
13109 w->cursor.vpos = -1;
13110 overlay_arrow_seen = 0;
13111
13112 /* Initialize iterator and info to start at POS. */
13113 start_display (&it, w, pos);
13114
13115 /* Display all lines of W. */
13116 while (it.current_y < it.last_visible_y)
13117 {
13118 if (display_line (&it))
13119 last_text_row = it.glyph_row - 1;
13120 if (fonts_changed_p)
13121 return 0;
13122 }
13123
13124 /* Don't let the cursor end in the scroll margins. */
13125 if (check_margins
13126 && !MINI_WINDOW_P (w))
13127 {
13128 int this_scroll_margin;
13129
13130 this_scroll_margin = max (0, scroll_margin);
13131 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13132 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13133
13134 if ((w->cursor.y < this_scroll_margin
13135 && CHARPOS (pos) > BEGV
13136 && IT_CHARPOS (it) < ZV)
13137 /* rms: considering make_cursor_line_fully_visible_p here
13138 seems to give wrong results. We don't want to recenter
13139 when the last line is partly visible, we want to allow
13140 that case to be handled in the usual way. */
13141 || (w->cursor.y + 1) > it.last_visible_y)
13142 {
13143 w->cursor.vpos = -1;
13144 clear_glyph_matrix (w->desired_matrix);
13145 return -1;
13146 }
13147 }
13148
13149 /* If bottom moved off end of frame, change mode line percentage. */
13150 if (XFASTINT (w->window_end_pos) <= 0
13151 && Z != IT_CHARPOS (it))
13152 w->update_mode_line = Qt;
13153
13154 /* Set window_end_pos to the offset of the last character displayed
13155 on the window from the end of current_buffer. Set
13156 window_end_vpos to its row number. */
13157 if (last_text_row)
13158 {
13159 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13160 w->window_end_bytepos
13161 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13162 w->window_end_pos
13163 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13164 w->window_end_vpos
13165 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13166 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13167 ->displays_text_p);
13168 }
13169 else
13170 {
13171 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13172 w->window_end_pos = make_number (Z - ZV);
13173 w->window_end_vpos = make_number (0);
13174 }
13175
13176 /* But that is not valid info until redisplay finishes. */
13177 w->window_end_valid = Qnil;
13178 return 1;
13179 }
13180
13181
13182 \f
13183 /************************************************************************
13184 Window redisplay reusing current matrix when buffer has not changed
13185 ************************************************************************/
13186
13187 /* Try redisplay of window W showing an unchanged buffer with a
13188 different window start than the last time it was displayed by
13189 reusing its current matrix. Value is non-zero if successful.
13190 W->start is the new window start. */
13191
13192 static int
13193 try_window_reusing_current_matrix (w)
13194 struct window *w;
13195 {
13196 struct frame *f = XFRAME (w->frame);
13197 struct glyph_row *row, *bottom_row;
13198 struct it it;
13199 struct run run;
13200 struct text_pos start, new_start;
13201 int nrows_scrolled, i;
13202 struct glyph_row *last_text_row;
13203 struct glyph_row *last_reused_text_row;
13204 struct glyph_row *start_row;
13205 int start_vpos, min_y, max_y;
13206
13207 #if GLYPH_DEBUG
13208 if (inhibit_try_window_reusing)
13209 return 0;
13210 #endif
13211
13212 if (/* This function doesn't handle terminal frames. */
13213 !FRAME_WINDOW_P (f)
13214 /* Don't try to reuse the display if windows have been split
13215 or such. */
13216 || windows_or_buffers_changed
13217 || cursor_type_changed)
13218 return 0;
13219
13220 /* Can't do this if region may have changed. */
13221 if ((!NILP (Vtransient_mark_mode)
13222 && !NILP (current_buffer->mark_active))
13223 || !NILP (w->region_showing)
13224 || !NILP (Vshow_trailing_whitespace))
13225 return 0;
13226
13227 /* If top-line visibility has changed, give up. */
13228 if (WINDOW_WANTS_HEADER_LINE_P (w)
13229 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13230 return 0;
13231
13232 /* Give up if old or new display is scrolled vertically. We could
13233 make this function handle this, but right now it doesn't. */
13234 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13235 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13236 return 0;
13237
13238 /* The variable new_start now holds the new window start. The old
13239 start `start' can be determined from the current matrix. */
13240 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13241 start = start_row->start.pos;
13242 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13243
13244 /* Clear the desired matrix for the display below. */
13245 clear_glyph_matrix (w->desired_matrix);
13246
13247 if (CHARPOS (new_start) <= CHARPOS (start))
13248 {
13249 int first_row_y;
13250
13251 /* Don't use this method if the display starts with an ellipsis
13252 displayed for invisible text. It's not easy to handle that case
13253 below, and it's certainly not worth the effort since this is
13254 not a frequent case. */
13255 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13256 return 0;
13257
13258 IF_DEBUG (debug_method_add (w, "twu1"));
13259
13260 /* Display up to a row that can be reused. The variable
13261 last_text_row is set to the last row displayed that displays
13262 text. Note that it.vpos == 0 if or if not there is a
13263 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13264 start_display (&it, w, new_start);
13265 first_row_y = it.current_y;
13266 w->cursor.vpos = -1;
13267 last_text_row = last_reused_text_row = NULL;
13268
13269 while (it.current_y < it.last_visible_y
13270 && !fonts_changed_p)
13271 {
13272 /* If we have reached into the characters in the START row,
13273 that means the line boundaries have changed. So we
13274 can't start copying with the row START. Maybe it will
13275 work to start copying with the following row. */
13276 while (IT_CHARPOS (it) > CHARPOS (start))
13277 {
13278 /* Advance to the next row as the "start". */
13279 start_row++;
13280 start = start_row->start.pos;
13281 /* If there are no more rows to try, or just one, give up. */
13282 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13283 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13284 || CHARPOS (start) == ZV)
13285 {
13286 clear_glyph_matrix (w->desired_matrix);
13287 return 0;
13288 }
13289
13290 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13291 }
13292 /* If we have reached alignment,
13293 we can copy the rest of the rows. */
13294 if (IT_CHARPOS (it) == CHARPOS (start))
13295 break;
13296
13297 if (display_line (&it))
13298 last_text_row = it.glyph_row - 1;
13299 }
13300
13301 /* A value of current_y < last_visible_y means that we stopped
13302 at the previous window start, which in turn means that we
13303 have at least one reusable row. */
13304 if (it.current_y < it.last_visible_y)
13305 {
13306 /* IT.vpos always starts from 0; it counts text lines. */
13307 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13308
13309 /* Find PT if not already found in the lines displayed. */
13310 if (w->cursor.vpos < 0)
13311 {
13312 int dy = it.current_y - start_row->y;
13313
13314 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13315 row = row_containing_pos (w, PT, row, NULL, dy);
13316 if (row)
13317 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13318 dy, nrows_scrolled);
13319 else
13320 {
13321 clear_glyph_matrix (w->desired_matrix);
13322 return 0;
13323 }
13324 }
13325
13326 /* Scroll the display. Do it before the current matrix is
13327 changed. The problem here is that update has not yet
13328 run, i.e. part of the current matrix is not up to date.
13329 scroll_run_hook will clear the cursor, and use the
13330 current matrix to get the height of the row the cursor is
13331 in. */
13332 run.current_y = start_row->y;
13333 run.desired_y = it.current_y;
13334 run.height = it.last_visible_y - it.current_y;
13335
13336 if (run.height > 0 && run.current_y != run.desired_y)
13337 {
13338 update_begin (f);
13339 rif->update_window_begin_hook (w);
13340 rif->clear_window_mouse_face (w);
13341 rif->scroll_run_hook (w, &run);
13342 rif->update_window_end_hook (w, 0, 0);
13343 update_end (f);
13344 }
13345
13346 /* Shift current matrix down by nrows_scrolled lines. */
13347 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13348 rotate_matrix (w->current_matrix,
13349 start_vpos,
13350 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13351 nrows_scrolled);
13352
13353 /* Disable lines that must be updated. */
13354 for (i = 0; i < it.vpos; ++i)
13355 (start_row + i)->enabled_p = 0;
13356
13357 /* Re-compute Y positions. */
13358 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13359 max_y = it.last_visible_y;
13360 for (row = start_row + nrows_scrolled;
13361 row < bottom_row;
13362 ++row)
13363 {
13364 row->y = it.current_y;
13365 row->visible_height = row->height;
13366
13367 if (row->y < min_y)
13368 row->visible_height -= min_y - row->y;
13369 if (row->y + row->height > max_y)
13370 row->visible_height -= row->y + row->height - max_y;
13371 row->redraw_fringe_bitmaps_p = 1;
13372
13373 it.current_y += row->height;
13374
13375 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13376 last_reused_text_row = row;
13377 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13378 break;
13379 }
13380
13381 /* Disable lines in the current matrix which are now
13382 below the window. */
13383 for (++row; row < bottom_row; ++row)
13384 row->enabled_p = row->mode_line_p = 0;
13385 }
13386
13387 /* Update window_end_pos etc.; last_reused_text_row is the last
13388 reused row from the current matrix containing text, if any.
13389 The value of last_text_row is the last displayed line
13390 containing text. */
13391 if (last_reused_text_row)
13392 {
13393 w->window_end_bytepos
13394 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13395 w->window_end_pos
13396 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13397 w->window_end_vpos
13398 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13399 w->current_matrix));
13400 }
13401 else if (last_text_row)
13402 {
13403 w->window_end_bytepos
13404 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13405 w->window_end_pos
13406 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13407 w->window_end_vpos
13408 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13409 }
13410 else
13411 {
13412 /* This window must be completely empty. */
13413 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13414 w->window_end_pos = make_number (Z - ZV);
13415 w->window_end_vpos = make_number (0);
13416 }
13417 w->window_end_valid = Qnil;
13418
13419 /* Update hint: don't try scrolling again in update_window. */
13420 w->desired_matrix->no_scrolling_p = 1;
13421
13422 #if GLYPH_DEBUG
13423 debug_method_add (w, "try_window_reusing_current_matrix 1");
13424 #endif
13425 return 1;
13426 }
13427 else if (CHARPOS (new_start) > CHARPOS (start))
13428 {
13429 struct glyph_row *pt_row, *row;
13430 struct glyph_row *first_reusable_row;
13431 struct glyph_row *first_row_to_display;
13432 int dy;
13433 int yb = window_text_bottom_y (w);
13434
13435 /* Find the row starting at new_start, if there is one. Don't
13436 reuse a partially visible line at the end. */
13437 first_reusable_row = start_row;
13438 while (first_reusable_row->enabled_p
13439 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13440 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13441 < CHARPOS (new_start)))
13442 ++first_reusable_row;
13443
13444 /* Give up if there is no row to reuse. */
13445 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13446 || !first_reusable_row->enabled_p
13447 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13448 != CHARPOS (new_start)))
13449 return 0;
13450
13451 /* We can reuse fully visible rows beginning with
13452 first_reusable_row to the end of the window. Set
13453 first_row_to_display to the first row that cannot be reused.
13454 Set pt_row to the row containing point, if there is any. */
13455 pt_row = NULL;
13456 for (first_row_to_display = first_reusable_row;
13457 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13458 ++first_row_to_display)
13459 {
13460 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13461 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13462 pt_row = first_row_to_display;
13463 }
13464
13465 /* Start displaying at the start of first_row_to_display. */
13466 xassert (first_row_to_display->y < yb);
13467 init_to_row_start (&it, w, first_row_to_display);
13468
13469 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13470 - start_vpos);
13471 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13472 - nrows_scrolled);
13473 it.current_y = (first_row_to_display->y - first_reusable_row->y
13474 + WINDOW_HEADER_LINE_HEIGHT (w));
13475
13476 /* Display lines beginning with first_row_to_display in the
13477 desired matrix. Set last_text_row to the last row displayed
13478 that displays text. */
13479 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13480 if (pt_row == NULL)
13481 w->cursor.vpos = -1;
13482 last_text_row = NULL;
13483 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13484 if (display_line (&it))
13485 last_text_row = it.glyph_row - 1;
13486
13487 /* Give up If point isn't in a row displayed or reused. */
13488 if (w->cursor.vpos < 0)
13489 {
13490 clear_glyph_matrix (w->desired_matrix);
13491 return 0;
13492 }
13493
13494 /* If point is in a reused row, adjust y and vpos of the cursor
13495 position. */
13496 if (pt_row)
13497 {
13498 w->cursor.vpos -= nrows_scrolled;
13499 w->cursor.y -= first_reusable_row->y - start_row->y;
13500 }
13501
13502 /* Scroll the display. */
13503 run.current_y = first_reusable_row->y;
13504 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13505 run.height = it.last_visible_y - run.current_y;
13506 dy = run.current_y - run.desired_y;
13507
13508 if (run.height)
13509 {
13510 update_begin (f);
13511 rif->update_window_begin_hook (w);
13512 rif->clear_window_mouse_face (w);
13513 rif->scroll_run_hook (w, &run);
13514 rif->update_window_end_hook (w, 0, 0);
13515 update_end (f);
13516 }
13517
13518 /* Adjust Y positions of reused rows. */
13519 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13520 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13521 max_y = it.last_visible_y;
13522 for (row = first_reusable_row; row < first_row_to_display; ++row)
13523 {
13524 row->y -= dy;
13525 row->visible_height = row->height;
13526 if (row->y < min_y)
13527 row->visible_height -= min_y - row->y;
13528 if (row->y + row->height > max_y)
13529 row->visible_height -= row->y + row->height - max_y;
13530 row->redraw_fringe_bitmaps_p = 1;
13531 }
13532
13533 /* Scroll the current matrix. */
13534 xassert (nrows_scrolled > 0);
13535 rotate_matrix (w->current_matrix,
13536 start_vpos,
13537 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13538 -nrows_scrolled);
13539
13540 /* Disable rows not reused. */
13541 for (row -= nrows_scrolled; row < bottom_row; ++row)
13542 row->enabled_p = 0;
13543
13544 /* Point may have moved to a different line, so we cannot assume that
13545 the previous cursor position is valid; locate the correct row. */
13546 if (pt_row)
13547 {
13548 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13549 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13550 row++)
13551 {
13552 w->cursor.vpos++;
13553 w->cursor.y = row->y;
13554 }
13555 if (row < bottom_row)
13556 {
13557 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13558 while (glyph->charpos < PT)
13559 {
13560 w->cursor.hpos++;
13561 w->cursor.x += glyph->pixel_width;
13562 glyph++;
13563 }
13564 }
13565 }
13566
13567 /* Adjust window end. A null value of last_text_row means that
13568 the window end is in reused rows which in turn means that
13569 only its vpos can have changed. */
13570 if (last_text_row)
13571 {
13572 w->window_end_bytepos
13573 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13574 w->window_end_pos
13575 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13576 w->window_end_vpos
13577 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13578 }
13579 else
13580 {
13581 w->window_end_vpos
13582 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13583 }
13584
13585 w->window_end_valid = Qnil;
13586 w->desired_matrix->no_scrolling_p = 1;
13587
13588 #if GLYPH_DEBUG
13589 debug_method_add (w, "try_window_reusing_current_matrix 2");
13590 #endif
13591 return 1;
13592 }
13593
13594 return 0;
13595 }
13596
13597
13598 \f
13599 /************************************************************************
13600 Window redisplay reusing current matrix when buffer has changed
13601 ************************************************************************/
13602
13603 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13604 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13605 int *, int *));
13606 static struct glyph_row *
13607 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13608 struct glyph_row *));
13609
13610
13611 /* Return the last row in MATRIX displaying text. If row START is
13612 non-null, start searching with that row. IT gives the dimensions
13613 of the display. Value is null if matrix is empty; otherwise it is
13614 a pointer to the row found. */
13615
13616 static struct glyph_row *
13617 find_last_row_displaying_text (matrix, it, start)
13618 struct glyph_matrix *matrix;
13619 struct it *it;
13620 struct glyph_row *start;
13621 {
13622 struct glyph_row *row, *row_found;
13623
13624 /* Set row_found to the last row in IT->w's current matrix
13625 displaying text. The loop looks funny but think of partially
13626 visible lines. */
13627 row_found = NULL;
13628 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13629 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13630 {
13631 xassert (row->enabled_p);
13632 row_found = row;
13633 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13634 break;
13635 ++row;
13636 }
13637
13638 return row_found;
13639 }
13640
13641
13642 /* Return the last row in the current matrix of W that is not affected
13643 by changes at the start of current_buffer that occurred since W's
13644 current matrix was built. Value is null if no such row exists.
13645
13646 BEG_UNCHANGED us the number of characters unchanged at the start of
13647 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13648 first changed character in current_buffer. Characters at positions <
13649 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13650 when the current matrix was built. */
13651
13652 static struct glyph_row *
13653 find_last_unchanged_at_beg_row (w)
13654 struct window *w;
13655 {
13656 int first_changed_pos = BEG + BEG_UNCHANGED;
13657 struct glyph_row *row;
13658 struct glyph_row *row_found = NULL;
13659 int yb = window_text_bottom_y (w);
13660
13661 /* Find the last row displaying unchanged text. */
13662 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13663 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13664 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13665 {
13666 if (/* If row ends before first_changed_pos, it is unchanged,
13667 except in some case. */
13668 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13669 /* When row ends in ZV and we write at ZV it is not
13670 unchanged. */
13671 && !row->ends_at_zv_p
13672 /* When first_changed_pos is the end of a continued line,
13673 row is not unchanged because it may be no longer
13674 continued. */
13675 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13676 && (row->continued_p
13677 || row->exact_window_width_line_p)))
13678 row_found = row;
13679
13680 /* Stop if last visible row. */
13681 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13682 break;
13683
13684 ++row;
13685 }
13686
13687 return row_found;
13688 }
13689
13690
13691 /* Find the first glyph row in the current matrix of W that is not
13692 affected by changes at the end of current_buffer since the
13693 time W's current matrix was built.
13694
13695 Return in *DELTA the number of chars by which buffer positions in
13696 unchanged text at the end of current_buffer must be adjusted.
13697
13698 Return in *DELTA_BYTES the corresponding number of bytes.
13699
13700 Value is null if no such row exists, i.e. all rows are affected by
13701 changes. */
13702
13703 static struct glyph_row *
13704 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13705 struct window *w;
13706 int *delta, *delta_bytes;
13707 {
13708 struct glyph_row *row;
13709 struct glyph_row *row_found = NULL;
13710
13711 *delta = *delta_bytes = 0;
13712
13713 /* Display must not have been paused, otherwise the current matrix
13714 is not up to date. */
13715 if (NILP (w->window_end_valid))
13716 abort ();
13717
13718 /* A value of window_end_pos >= END_UNCHANGED means that the window
13719 end is in the range of changed text. If so, there is no
13720 unchanged row at the end of W's current matrix. */
13721 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13722 return NULL;
13723
13724 /* Set row to the last row in W's current matrix displaying text. */
13725 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13726
13727 /* If matrix is entirely empty, no unchanged row exists. */
13728 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13729 {
13730 /* The value of row is the last glyph row in the matrix having a
13731 meaningful buffer position in it. The end position of row
13732 corresponds to window_end_pos. This allows us to translate
13733 buffer positions in the current matrix to current buffer
13734 positions for characters not in changed text. */
13735 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13736 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13737 int last_unchanged_pos, last_unchanged_pos_old;
13738 struct glyph_row *first_text_row
13739 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13740
13741 *delta = Z - Z_old;
13742 *delta_bytes = Z_BYTE - Z_BYTE_old;
13743
13744 /* Set last_unchanged_pos to the buffer position of the last
13745 character in the buffer that has not been changed. Z is the
13746 index + 1 of the last character in current_buffer, i.e. by
13747 subtracting END_UNCHANGED we get the index of the last
13748 unchanged character, and we have to add BEG to get its buffer
13749 position. */
13750 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13751 last_unchanged_pos_old = last_unchanged_pos - *delta;
13752
13753 /* Search backward from ROW for a row displaying a line that
13754 starts at a minimum position >= last_unchanged_pos_old. */
13755 for (; row > first_text_row; --row)
13756 {
13757 /* This used to abort, but it can happen.
13758 It is ok to just stop the search instead here. KFS. */
13759 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13760 break;
13761
13762 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13763 row_found = row;
13764 }
13765 }
13766
13767 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13768 abort ();
13769
13770 return row_found;
13771 }
13772
13773
13774 /* Make sure that glyph rows in the current matrix of window W
13775 reference the same glyph memory as corresponding rows in the
13776 frame's frame matrix. This function is called after scrolling W's
13777 current matrix on a terminal frame in try_window_id and
13778 try_window_reusing_current_matrix. */
13779
13780 static void
13781 sync_frame_with_window_matrix_rows (w)
13782 struct window *w;
13783 {
13784 struct frame *f = XFRAME (w->frame);
13785 struct glyph_row *window_row, *window_row_end, *frame_row;
13786
13787 /* Preconditions: W must be a leaf window and full-width. Its frame
13788 must have a frame matrix. */
13789 xassert (NILP (w->hchild) && NILP (w->vchild));
13790 xassert (WINDOW_FULL_WIDTH_P (w));
13791 xassert (!FRAME_WINDOW_P (f));
13792
13793 /* If W is a full-width window, glyph pointers in W's current matrix
13794 have, by definition, to be the same as glyph pointers in the
13795 corresponding frame matrix. Note that frame matrices have no
13796 marginal areas (see build_frame_matrix). */
13797 window_row = w->current_matrix->rows;
13798 window_row_end = window_row + w->current_matrix->nrows;
13799 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13800 while (window_row < window_row_end)
13801 {
13802 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13803 struct glyph *end = window_row->glyphs[LAST_AREA];
13804
13805 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13806 frame_row->glyphs[TEXT_AREA] = start;
13807 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13808 frame_row->glyphs[LAST_AREA] = end;
13809
13810 /* Disable frame rows whose corresponding window rows have
13811 been disabled in try_window_id. */
13812 if (!window_row->enabled_p)
13813 frame_row->enabled_p = 0;
13814
13815 ++window_row, ++frame_row;
13816 }
13817 }
13818
13819
13820 /* Find the glyph row in window W containing CHARPOS. Consider all
13821 rows between START and END (not inclusive). END null means search
13822 all rows to the end of the display area of W. Value is the row
13823 containing CHARPOS or null. */
13824
13825 struct glyph_row *
13826 row_containing_pos (w, charpos, start, end, dy)
13827 struct window *w;
13828 int charpos;
13829 struct glyph_row *start, *end;
13830 int dy;
13831 {
13832 struct glyph_row *row = start;
13833 int last_y;
13834
13835 /* If we happen to start on a header-line, skip that. */
13836 if (row->mode_line_p)
13837 ++row;
13838
13839 if ((end && row >= end) || !row->enabled_p)
13840 return NULL;
13841
13842 last_y = window_text_bottom_y (w) - dy;
13843
13844 while (1)
13845 {
13846 /* Give up if we have gone too far. */
13847 if (end && row >= end)
13848 return NULL;
13849 /* This formerly returned if they were equal.
13850 I think that both quantities are of a "last plus one" type;
13851 if so, when they are equal, the row is within the screen. -- rms. */
13852 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13853 return NULL;
13854
13855 /* If it is in this row, return this row. */
13856 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13857 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13858 /* The end position of a row equals the start
13859 position of the next row. If CHARPOS is there, we
13860 would rather display it in the next line, except
13861 when this line ends in ZV. */
13862 && !row->ends_at_zv_p
13863 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13864 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13865 return row;
13866 ++row;
13867 }
13868 }
13869
13870
13871 /* Try to redisplay window W by reusing its existing display. W's
13872 current matrix must be up to date when this function is called,
13873 i.e. window_end_valid must not be nil.
13874
13875 Value is
13876
13877 1 if display has been updated
13878 0 if otherwise unsuccessful
13879 -1 if redisplay with same window start is known not to succeed
13880
13881 The following steps are performed:
13882
13883 1. Find the last row in the current matrix of W that is not
13884 affected by changes at the start of current_buffer. If no such row
13885 is found, give up.
13886
13887 2. Find the first row in W's current matrix that is not affected by
13888 changes at the end of current_buffer. Maybe there is no such row.
13889
13890 3. Display lines beginning with the row + 1 found in step 1 to the
13891 row found in step 2 or, if step 2 didn't find a row, to the end of
13892 the window.
13893
13894 4. If cursor is not known to appear on the window, give up.
13895
13896 5. If display stopped at the row found in step 2, scroll the
13897 display and current matrix as needed.
13898
13899 6. Maybe display some lines at the end of W, if we must. This can
13900 happen under various circumstances, like a partially visible line
13901 becoming fully visible, or because newly displayed lines are displayed
13902 in smaller font sizes.
13903
13904 7. Update W's window end information. */
13905
13906 static int
13907 try_window_id (w)
13908 struct window *w;
13909 {
13910 struct frame *f = XFRAME (w->frame);
13911 struct glyph_matrix *current_matrix = w->current_matrix;
13912 struct glyph_matrix *desired_matrix = w->desired_matrix;
13913 struct glyph_row *last_unchanged_at_beg_row;
13914 struct glyph_row *first_unchanged_at_end_row;
13915 struct glyph_row *row;
13916 struct glyph_row *bottom_row;
13917 int bottom_vpos;
13918 struct it it;
13919 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13920 struct text_pos start_pos;
13921 struct run run;
13922 int first_unchanged_at_end_vpos = 0;
13923 struct glyph_row *last_text_row, *last_text_row_at_end;
13924 struct text_pos start;
13925 int first_changed_charpos, last_changed_charpos;
13926
13927 #if GLYPH_DEBUG
13928 if (inhibit_try_window_id)
13929 return 0;
13930 #endif
13931
13932 /* This is handy for debugging. */
13933 #if 0
13934 #define GIVE_UP(X) \
13935 do { \
13936 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13937 return 0; \
13938 } while (0)
13939 #else
13940 #define GIVE_UP(X) return 0
13941 #endif
13942
13943 SET_TEXT_POS_FROM_MARKER (start, w->start);
13944
13945 /* Don't use this for mini-windows because these can show
13946 messages and mini-buffers, and we don't handle that here. */
13947 if (MINI_WINDOW_P (w))
13948 GIVE_UP (1);
13949
13950 /* This flag is used to prevent redisplay optimizations. */
13951 if (windows_or_buffers_changed || cursor_type_changed)
13952 GIVE_UP (2);
13953
13954 /* Verify that narrowing has not changed.
13955 Also verify that we were not told to prevent redisplay optimizations.
13956 It would be nice to further
13957 reduce the number of cases where this prevents try_window_id. */
13958 if (current_buffer->clip_changed
13959 || current_buffer->prevent_redisplay_optimizations_p)
13960 GIVE_UP (3);
13961
13962 /* Window must either use window-based redisplay or be full width. */
13963 if (!FRAME_WINDOW_P (f)
13964 && (!line_ins_del_ok
13965 || !WINDOW_FULL_WIDTH_P (w)))
13966 GIVE_UP (4);
13967
13968 /* Give up if point is not known NOT to appear in W. */
13969 if (PT < CHARPOS (start))
13970 GIVE_UP (5);
13971
13972 /* Another way to prevent redisplay optimizations. */
13973 if (XFASTINT (w->last_modified) == 0)
13974 GIVE_UP (6);
13975
13976 /* Verify that window is not hscrolled. */
13977 if (XFASTINT (w->hscroll) != 0)
13978 GIVE_UP (7);
13979
13980 /* Verify that display wasn't paused. */
13981 if (NILP (w->window_end_valid))
13982 GIVE_UP (8);
13983
13984 /* Can't use this if highlighting a region because a cursor movement
13985 will do more than just set the cursor. */
13986 if (!NILP (Vtransient_mark_mode)
13987 && !NILP (current_buffer->mark_active))
13988 GIVE_UP (9);
13989
13990 /* Likewise if highlighting trailing whitespace. */
13991 if (!NILP (Vshow_trailing_whitespace))
13992 GIVE_UP (11);
13993
13994 /* Likewise if showing a region. */
13995 if (!NILP (w->region_showing))
13996 GIVE_UP (10);
13997
13998 /* Can use this if overlay arrow position and or string have changed. */
13999 if (overlay_arrows_changed_p ())
14000 GIVE_UP (12);
14001
14002
14003 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14004 only if buffer has really changed. The reason is that the gap is
14005 initially at Z for freshly visited files. The code below would
14006 set end_unchanged to 0 in that case. */
14007 if (MODIFF > SAVE_MODIFF
14008 /* This seems to happen sometimes after saving a buffer. */
14009 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14010 {
14011 if (GPT - BEG < BEG_UNCHANGED)
14012 BEG_UNCHANGED = GPT - BEG;
14013 if (Z - GPT < END_UNCHANGED)
14014 END_UNCHANGED = Z - GPT;
14015 }
14016
14017 /* The position of the first and last character that has been changed. */
14018 first_changed_charpos = BEG + BEG_UNCHANGED;
14019 last_changed_charpos = Z - END_UNCHANGED;
14020
14021 /* If window starts after a line end, and the last change is in
14022 front of that newline, then changes don't affect the display.
14023 This case happens with stealth-fontification. Note that although
14024 the display is unchanged, glyph positions in the matrix have to
14025 be adjusted, of course. */
14026 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14027 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14028 && ((last_changed_charpos < CHARPOS (start)
14029 && CHARPOS (start) == BEGV)
14030 || (last_changed_charpos < CHARPOS (start) - 1
14031 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14032 {
14033 int Z_old, delta, Z_BYTE_old, delta_bytes;
14034 struct glyph_row *r0;
14035
14036 /* Compute how many chars/bytes have been added to or removed
14037 from the buffer. */
14038 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14039 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14040 delta = Z - Z_old;
14041 delta_bytes = Z_BYTE - Z_BYTE_old;
14042
14043 /* Give up if PT is not in the window. Note that it already has
14044 been checked at the start of try_window_id that PT is not in
14045 front of the window start. */
14046 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14047 GIVE_UP (13);
14048
14049 /* If window start is unchanged, we can reuse the whole matrix
14050 as is, after adjusting glyph positions. No need to compute
14051 the window end again, since its offset from Z hasn't changed. */
14052 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14053 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14054 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14055 /* PT must not be in a partially visible line. */
14056 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14057 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14058 {
14059 /* Adjust positions in the glyph matrix. */
14060 if (delta || delta_bytes)
14061 {
14062 struct glyph_row *r1
14063 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14064 increment_matrix_positions (w->current_matrix,
14065 MATRIX_ROW_VPOS (r0, current_matrix),
14066 MATRIX_ROW_VPOS (r1, current_matrix),
14067 delta, delta_bytes);
14068 }
14069
14070 /* Set the cursor. */
14071 row = row_containing_pos (w, PT, r0, NULL, 0);
14072 if (row)
14073 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14074 else
14075 abort ();
14076 return 1;
14077 }
14078 }
14079
14080 /* Handle the case that changes are all below what is displayed in
14081 the window, and that PT is in the window. This shortcut cannot
14082 be taken if ZV is visible in the window, and text has been added
14083 there that is visible in the window. */
14084 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14085 /* ZV is not visible in the window, or there are no
14086 changes at ZV, actually. */
14087 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14088 || first_changed_charpos == last_changed_charpos))
14089 {
14090 struct glyph_row *r0;
14091
14092 /* Give up if PT is not in the window. Note that it already has
14093 been checked at the start of try_window_id that PT is not in
14094 front of the window start. */
14095 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14096 GIVE_UP (14);
14097
14098 /* If window start is unchanged, we can reuse the whole matrix
14099 as is, without changing glyph positions since no text has
14100 been added/removed in front of the window end. */
14101 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14102 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14103 /* PT must not be in a partially visible line. */
14104 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14105 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14106 {
14107 /* We have to compute the window end anew since text
14108 can have been added/removed after it. */
14109 w->window_end_pos
14110 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14111 w->window_end_bytepos
14112 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14113
14114 /* Set the cursor. */
14115 row = row_containing_pos (w, PT, r0, NULL, 0);
14116 if (row)
14117 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14118 else
14119 abort ();
14120 return 2;
14121 }
14122 }
14123
14124 /* Give up if window start is in the changed area.
14125
14126 The condition used to read
14127
14128 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14129
14130 but why that was tested escapes me at the moment. */
14131 if (CHARPOS (start) >= first_changed_charpos
14132 && CHARPOS (start) <= last_changed_charpos)
14133 GIVE_UP (15);
14134
14135 /* Check that window start agrees with the start of the first glyph
14136 row in its current matrix. Check this after we know the window
14137 start is not in changed text, otherwise positions would not be
14138 comparable. */
14139 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14140 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14141 GIVE_UP (16);
14142
14143 /* Give up if the window ends in strings. Overlay strings
14144 at the end are difficult to handle, so don't try. */
14145 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14146 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14147 GIVE_UP (20);
14148
14149 /* Compute the position at which we have to start displaying new
14150 lines. Some of the lines at the top of the window might be
14151 reusable because they are not displaying changed text. Find the
14152 last row in W's current matrix not affected by changes at the
14153 start of current_buffer. Value is null if changes start in the
14154 first line of window. */
14155 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14156 if (last_unchanged_at_beg_row)
14157 {
14158 /* Avoid starting to display in the moddle of a character, a TAB
14159 for instance. This is easier than to set up the iterator
14160 exactly, and it's not a frequent case, so the additional
14161 effort wouldn't really pay off. */
14162 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14163 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14164 && last_unchanged_at_beg_row > w->current_matrix->rows)
14165 --last_unchanged_at_beg_row;
14166
14167 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14168 GIVE_UP (17);
14169
14170 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14171 GIVE_UP (18);
14172 start_pos = it.current.pos;
14173
14174 /* Start displaying new lines in the desired matrix at the same
14175 vpos we would use in the current matrix, i.e. below
14176 last_unchanged_at_beg_row. */
14177 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14178 current_matrix);
14179 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14180 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14181
14182 xassert (it.hpos == 0 && it.current_x == 0);
14183 }
14184 else
14185 {
14186 /* There are no reusable lines at the start of the window.
14187 Start displaying in the first text line. */
14188 start_display (&it, w, start);
14189 it.vpos = it.first_vpos;
14190 start_pos = it.current.pos;
14191 }
14192
14193 /* Find the first row that is not affected by changes at the end of
14194 the buffer. Value will be null if there is no unchanged row, in
14195 which case we must redisplay to the end of the window. delta
14196 will be set to the value by which buffer positions beginning with
14197 first_unchanged_at_end_row have to be adjusted due to text
14198 changes. */
14199 first_unchanged_at_end_row
14200 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14201 IF_DEBUG (debug_delta = delta);
14202 IF_DEBUG (debug_delta_bytes = delta_bytes);
14203
14204 /* Set stop_pos to the buffer position up to which we will have to
14205 display new lines. If first_unchanged_at_end_row != NULL, this
14206 is the buffer position of the start of the line displayed in that
14207 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14208 that we don't stop at a buffer position. */
14209 stop_pos = 0;
14210 if (first_unchanged_at_end_row)
14211 {
14212 xassert (last_unchanged_at_beg_row == NULL
14213 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14214
14215 /* If this is a continuation line, move forward to the next one
14216 that isn't. Changes in lines above affect this line.
14217 Caution: this may move first_unchanged_at_end_row to a row
14218 not displaying text. */
14219 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14220 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14221 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14222 < it.last_visible_y))
14223 ++first_unchanged_at_end_row;
14224
14225 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14226 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14227 >= it.last_visible_y))
14228 first_unchanged_at_end_row = NULL;
14229 else
14230 {
14231 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14232 + delta);
14233 first_unchanged_at_end_vpos
14234 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14235 xassert (stop_pos >= Z - END_UNCHANGED);
14236 }
14237 }
14238 else if (last_unchanged_at_beg_row == NULL)
14239 GIVE_UP (19);
14240
14241
14242 #if GLYPH_DEBUG
14243
14244 /* Either there is no unchanged row at the end, or the one we have
14245 now displays text. This is a necessary condition for the window
14246 end pos calculation at the end of this function. */
14247 xassert (first_unchanged_at_end_row == NULL
14248 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14249
14250 debug_last_unchanged_at_beg_vpos
14251 = (last_unchanged_at_beg_row
14252 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14253 : -1);
14254 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14255
14256 #endif /* GLYPH_DEBUG != 0 */
14257
14258
14259 /* Display new lines. Set last_text_row to the last new line
14260 displayed which has text on it, i.e. might end up as being the
14261 line where the window_end_vpos is. */
14262 w->cursor.vpos = -1;
14263 last_text_row = NULL;
14264 overlay_arrow_seen = 0;
14265 while (it.current_y < it.last_visible_y
14266 && !fonts_changed_p
14267 && (first_unchanged_at_end_row == NULL
14268 || IT_CHARPOS (it) < stop_pos))
14269 {
14270 if (display_line (&it))
14271 last_text_row = it.glyph_row - 1;
14272 }
14273
14274 if (fonts_changed_p)
14275 return -1;
14276
14277
14278 /* Compute differences in buffer positions, y-positions etc. for
14279 lines reused at the bottom of the window. Compute what we can
14280 scroll. */
14281 if (first_unchanged_at_end_row
14282 /* No lines reused because we displayed everything up to the
14283 bottom of the window. */
14284 && it.current_y < it.last_visible_y)
14285 {
14286 dvpos = (it.vpos
14287 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14288 current_matrix));
14289 dy = it.current_y - first_unchanged_at_end_row->y;
14290 run.current_y = first_unchanged_at_end_row->y;
14291 run.desired_y = run.current_y + dy;
14292 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14293 }
14294 else
14295 {
14296 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14297 first_unchanged_at_end_row = NULL;
14298 }
14299 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14300
14301
14302 /* Find the cursor if not already found. We have to decide whether
14303 PT will appear on this window (it sometimes doesn't, but this is
14304 not a very frequent case.) This decision has to be made before
14305 the current matrix is altered. A value of cursor.vpos < 0 means
14306 that PT is either in one of the lines beginning at
14307 first_unchanged_at_end_row or below the window. Don't care for
14308 lines that might be displayed later at the window end; as
14309 mentioned, this is not a frequent case. */
14310 if (w->cursor.vpos < 0)
14311 {
14312 /* Cursor in unchanged rows at the top? */
14313 if (PT < CHARPOS (start_pos)
14314 && last_unchanged_at_beg_row)
14315 {
14316 row = row_containing_pos (w, PT,
14317 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14318 last_unchanged_at_beg_row + 1, 0);
14319 if (row)
14320 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14321 }
14322
14323 /* Start from first_unchanged_at_end_row looking for PT. */
14324 else if (first_unchanged_at_end_row)
14325 {
14326 row = row_containing_pos (w, PT - delta,
14327 first_unchanged_at_end_row, NULL, 0);
14328 if (row)
14329 set_cursor_from_row (w, row, w->current_matrix, delta,
14330 delta_bytes, dy, dvpos);
14331 }
14332
14333 /* Give up if cursor was not found. */
14334 if (w->cursor.vpos < 0)
14335 {
14336 clear_glyph_matrix (w->desired_matrix);
14337 return -1;
14338 }
14339 }
14340
14341 /* Don't let the cursor end in the scroll margins. */
14342 {
14343 int this_scroll_margin, cursor_height;
14344
14345 this_scroll_margin = max (0, scroll_margin);
14346 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14347 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14348 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14349
14350 if ((w->cursor.y < this_scroll_margin
14351 && CHARPOS (start) > BEGV)
14352 /* Old redisplay didn't take scroll margin into account at the bottom,
14353 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14354 || (w->cursor.y + (make_cursor_line_fully_visible_p
14355 ? cursor_height + this_scroll_margin
14356 : 1)) > it.last_visible_y)
14357 {
14358 w->cursor.vpos = -1;
14359 clear_glyph_matrix (w->desired_matrix);
14360 return -1;
14361 }
14362 }
14363
14364 /* Scroll the display. Do it before changing the current matrix so
14365 that xterm.c doesn't get confused about where the cursor glyph is
14366 found. */
14367 if (dy && run.height)
14368 {
14369 update_begin (f);
14370
14371 if (FRAME_WINDOW_P (f))
14372 {
14373 rif->update_window_begin_hook (w);
14374 rif->clear_window_mouse_face (w);
14375 rif->scroll_run_hook (w, &run);
14376 rif->update_window_end_hook (w, 0, 0);
14377 }
14378 else
14379 {
14380 /* Terminal frame. In this case, dvpos gives the number of
14381 lines to scroll by; dvpos < 0 means scroll up. */
14382 int first_unchanged_at_end_vpos
14383 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14384 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14385 int end = (WINDOW_TOP_EDGE_LINE (w)
14386 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14387 + window_internal_height (w));
14388
14389 /* Perform the operation on the screen. */
14390 if (dvpos > 0)
14391 {
14392 /* Scroll last_unchanged_at_beg_row to the end of the
14393 window down dvpos lines. */
14394 set_terminal_window (end);
14395
14396 /* On dumb terminals delete dvpos lines at the end
14397 before inserting dvpos empty lines. */
14398 if (!scroll_region_ok)
14399 ins_del_lines (end - dvpos, -dvpos);
14400
14401 /* Insert dvpos empty lines in front of
14402 last_unchanged_at_beg_row. */
14403 ins_del_lines (from, dvpos);
14404 }
14405 else if (dvpos < 0)
14406 {
14407 /* Scroll up last_unchanged_at_beg_vpos to the end of
14408 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14409 set_terminal_window (end);
14410
14411 /* Delete dvpos lines in front of
14412 last_unchanged_at_beg_vpos. ins_del_lines will set
14413 the cursor to the given vpos and emit |dvpos| delete
14414 line sequences. */
14415 ins_del_lines (from + dvpos, dvpos);
14416
14417 /* On a dumb terminal insert dvpos empty lines at the
14418 end. */
14419 if (!scroll_region_ok)
14420 ins_del_lines (end + dvpos, -dvpos);
14421 }
14422
14423 set_terminal_window (0);
14424 }
14425
14426 update_end (f);
14427 }
14428
14429 /* Shift reused rows of the current matrix to the right position.
14430 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14431 text. */
14432 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14433 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14434 if (dvpos < 0)
14435 {
14436 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14437 bottom_vpos, dvpos);
14438 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14439 bottom_vpos, 0);
14440 }
14441 else if (dvpos > 0)
14442 {
14443 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14444 bottom_vpos, dvpos);
14445 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14446 first_unchanged_at_end_vpos + dvpos, 0);
14447 }
14448
14449 /* For frame-based redisplay, make sure that current frame and window
14450 matrix are in sync with respect to glyph memory. */
14451 if (!FRAME_WINDOW_P (f))
14452 sync_frame_with_window_matrix_rows (w);
14453
14454 /* Adjust buffer positions in reused rows. */
14455 if (delta)
14456 increment_matrix_positions (current_matrix,
14457 first_unchanged_at_end_vpos + dvpos,
14458 bottom_vpos, delta, delta_bytes);
14459
14460 /* Adjust Y positions. */
14461 if (dy)
14462 shift_glyph_matrix (w, current_matrix,
14463 first_unchanged_at_end_vpos + dvpos,
14464 bottom_vpos, dy);
14465
14466 if (first_unchanged_at_end_row)
14467 {
14468 first_unchanged_at_end_row += dvpos;
14469 if (first_unchanged_at_end_row->y >= it.last_visible_y
14470 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14471 first_unchanged_at_end_row = NULL;
14472 }
14473
14474 /* If scrolling up, there may be some lines to display at the end of
14475 the window. */
14476 last_text_row_at_end = NULL;
14477 if (dy < 0)
14478 {
14479 /* Scrolling up can leave for example a partially visible line
14480 at the end of the window to be redisplayed. */
14481 /* Set last_row to the glyph row in the current matrix where the
14482 window end line is found. It has been moved up or down in
14483 the matrix by dvpos. */
14484 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14485 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14486
14487 /* If last_row is the window end line, it should display text. */
14488 xassert (last_row->displays_text_p);
14489
14490 /* If window end line was partially visible before, begin
14491 displaying at that line. Otherwise begin displaying with the
14492 line following it. */
14493 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14494 {
14495 init_to_row_start (&it, w, last_row);
14496 it.vpos = last_vpos;
14497 it.current_y = last_row->y;
14498 }
14499 else
14500 {
14501 init_to_row_end (&it, w, last_row);
14502 it.vpos = 1 + last_vpos;
14503 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14504 ++last_row;
14505 }
14506
14507 /* We may start in a continuation line. If so, we have to
14508 get the right continuation_lines_width and current_x. */
14509 it.continuation_lines_width = last_row->continuation_lines_width;
14510 it.hpos = it.current_x = 0;
14511
14512 /* Display the rest of the lines at the window end. */
14513 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14514 while (it.current_y < it.last_visible_y
14515 && !fonts_changed_p)
14516 {
14517 /* Is it always sure that the display agrees with lines in
14518 the current matrix? I don't think so, so we mark rows
14519 displayed invalid in the current matrix by setting their
14520 enabled_p flag to zero. */
14521 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14522 if (display_line (&it))
14523 last_text_row_at_end = it.glyph_row - 1;
14524 }
14525 }
14526
14527 /* Update window_end_pos and window_end_vpos. */
14528 if (first_unchanged_at_end_row
14529 && !last_text_row_at_end)
14530 {
14531 /* Window end line if one of the preserved rows from the current
14532 matrix. Set row to the last row displaying text in current
14533 matrix starting at first_unchanged_at_end_row, after
14534 scrolling. */
14535 xassert (first_unchanged_at_end_row->displays_text_p);
14536 row = find_last_row_displaying_text (w->current_matrix, &it,
14537 first_unchanged_at_end_row);
14538 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14539
14540 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14541 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14542 w->window_end_vpos
14543 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14544 xassert (w->window_end_bytepos >= 0);
14545 IF_DEBUG (debug_method_add (w, "A"));
14546 }
14547 else if (last_text_row_at_end)
14548 {
14549 w->window_end_pos
14550 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14551 w->window_end_bytepos
14552 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14553 w->window_end_vpos
14554 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14555 xassert (w->window_end_bytepos >= 0);
14556 IF_DEBUG (debug_method_add (w, "B"));
14557 }
14558 else if (last_text_row)
14559 {
14560 /* We have displayed either to the end of the window or at the
14561 end of the window, i.e. the last row with text is to be found
14562 in the desired matrix. */
14563 w->window_end_pos
14564 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14565 w->window_end_bytepos
14566 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14567 w->window_end_vpos
14568 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14569 xassert (w->window_end_bytepos >= 0);
14570 }
14571 else if (first_unchanged_at_end_row == NULL
14572 && last_text_row == NULL
14573 && last_text_row_at_end == NULL)
14574 {
14575 /* Displayed to end of window, but no line containing text was
14576 displayed. Lines were deleted at the end of the window. */
14577 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14578 int vpos = XFASTINT (w->window_end_vpos);
14579 struct glyph_row *current_row = current_matrix->rows + vpos;
14580 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14581
14582 for (row = NULL;
14583 row == NULL && vpos >= first_vpos;
14584 --vpos, --current_row, --desired_row)
14585 {
14586 if (desired_row->enabled_p)
14587 {
14588 if (desired_row->displays_text_p)
14589 row = desired_row;
14590 }
14591 else if (current_row->displays_text_p)
14592 row = current_row;
14593 }
14594
14595 xassert (row != NULL);
14596 w->window_end_vpos = make_number (vpos + 1);
14597 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14598 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14599 xassert (w->window_end_bytepos >= 0);
14600 IF_DEBUG (debug_method_add (w, "C"));
14601 }
14602 else
14603 abort ();
14604
14605 #if 0 /* This leads to problems, for instance when the cursor is
14606 at ZV, and the cursor line displays no text. */
14607 /* Disable rows below what's displayed in the window. This makes
14608 debugging easier. */
14609 enable_glyph_matrix_rows (current_matrix,
14610 XFASTINT (w->window_end_vpos) + 1,
14611 bottom_vpos, 0);
14612 #endif
14613
14614 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14615 debug_end_vpos = XFASTINT (w->window_end_vpos));
14616
14617 /* Record that display has not been completed. */
14618 w->window_end_valid = Qnil;
14619 w->desired_matrix->no_scrolling_p = 1;
14620 return 3;
14621
14622 #undef GIVE_UP
14623 }
14624
14625
14626 \f
14627 /***********************************************************************
14628 More debugging support
14629 ***********************************************************************/
14630
14631 #if GLYPH_DEBUG
14632
14633 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14634 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14635 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14636
14637
14638 /* Dump the contents of glyph matrix MATRIX on stderr.
14639
14640 GLYPHS 0 means don't show glyph contents.
14641 GLYPHS 1 means show glyphs in short form
14642 GLYPHS > 1 means show glyphs in long form. */
14643
14644 void
14645 dump_glyph_matrix (matrix, glyphs)
14646 struct glyph_matrix *matrix;
14647 int glyphs;
14648 {
14649 int i;
14650 for (i = 0; i < matrix->nrows; ++i)
14651 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14652 }
14653
14654
14655 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14656 the glyph row and area where the glyph comes from. */
14657
14658 void
14659 dump_glyph (row, glyph, area)
14660 struct glyph_row *row;
14661 struct glyph *glyph;
14662 int area;
14663 {
14664 if (glyph->type == CHAR_GLYPH)
14665 {
14666 fprintf (stderr,
14667 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14668 glyph - row->glyphs[TEXT_AREA],
14669 'C',
14670 glyph->charpos,
14671 (BUFFERP (glyph->object)
14672 ? 'B'
14673 : (STRINGP (glyph->object)
14674 ? 'S'
14675 : '-')),
14676 glyph->pixel_width,
14677 glyph->u.ch,
14678 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14679 ? glyph->u.ch
14680 : '.'),
14681 glyph->face_id,
14682 glyph->left_box_line_p,
14683 glyph->right_box_line_p);
14684 }
14685 else if (glyph->type == STRETCH_GLYPH)
14686 {
14687 fprintf (stderr,
14688 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14689 glyph - row->glyphs[TEXT_AREA],
14690 'S',
14691 glyph->charpos,
14692 (BUFFERP (glyph->object)
14693 ? 'B'
14694 : (STRINGP (glyph->object)
14695 ? 'S'
14696 : '-')),
14697 glyph->pixel_width,
14698 0,
14699 '.',
14700 glyph->face_id,
14701 glyph->left_box_line_p,
14702 glyph->right_box_line_p);
14703 }
14704 else if (glyph->type == IMAGE_GLYPH)
14705 {
14706 fprintf (stderr,
14707 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14708 glyph - row->glyphs[TEXT_AREA],
14709 'I',
14710 glyph->charpos,
14711 (BUFFERP (glyph->object)
14712 ? 'B'
14713 : (STRINGP (glyph->object)
14714 ? 'S'
14715 : '-')),
14716 glyph->pixel_width,
14717 glyph->u.img_id,
14718 '.',
14719 glyph->face_id,
14720 glyph->left_box_line_p,
14721 glyph->right_box_line_p);
14722 }
14723 }
14724
14725
14726 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14727 GLYPHS 0 means don't show glyph contents.
14728 GLYPHS 1 means show glyphs in short form
14729 GLYPHS > 1 means show glyphs in long form. */
14730
14731 void
14732 dump_glyph_row (row, vpos, glyphs)
14733 struct glyph_row *row;
14734 int vpos, glyphs;
14735 {
14736 if (glyphs != 1)
14737 {
14738 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14739 fprintf (stderr, "======================================================================\n");
14740
14741 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14742 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14743 vpos,
14744 MATRIX_ROW_START_CHARPOS (row),
14745 MATRIX_ROW_END_CHARPOS (row),
14746 row->used[TEXT_AREA],
14747 row->contains_overlapping_glyphs_p,
14748 row->enabled_p,
14749 row->truncated_on_left_p,
14750 row->truncated_on_right_p,
14751 row->continued_p,
14752 MATRIX_ROW_CONTINUATION_LINE_P (row),
14753 row->displays_text_p,
14754 row->ends_at_zv_p,
14755 row->fill_line_p,
14756 row->ends_in_middle_of_char_p,
14757 row->starts_in_middle_of_char_p,
14758 row->mouse_face_p,
14759 row->x,
14760 row->y,
14761 row->pixel_width,
14762 row->height,
14763 row->visible_height,
14764 row->ascent,
14765 row->phys_ascent);
14766 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14767 row->end.overlay_string_index,
14768 row->continuation_lines_width);
14769 fprintf (stderr, "%9d %5d\n",
14770 CHARPOS (row->start.string_pos),
14771 CHARPOS (row->end.string_pos));
14772 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14773 row->end.dpvec_index);
14774 }
14775
14776 if (glyphs > 1)
14777 {
14778 int area;
14779
14780 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14781 {
14782 struct glyph *glyph = row->glyphs[area];
14783 struct glyph *glyph_end = glyph + row->used[area];
14784
14785 /* Glyph for a line end in text. */
14786 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14787 ++glyph_end;
14788
14789 if (glyph < glyph_end)
14790 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14791
14792 for (; glyph < glyph_end; ++glyph)
14793 dump_glyph (row, glyph, area);
14794 }
14795 }
14796 else if (glyphs == 1)
14797 {
14798 int area;
14799
14800 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14801 {
14802 char *s = (char *) alloca (row->used[area] + 1);
14803 int i;
14804
14805 for (i = 0; i < row->used[area]; ++i)
14806 {
14807 struct glyph *glyph = row->glyphs[area] + i;
14808 if (glyph->type == CHAR_GLYPH
14809 && glyph->u.ch < 0x80
14810 && glyph->u.ch >= ' ')
14811 s[i] = glyph->u.ch;
14812 else
14813 s[i] = '.';
14814 }
14815
14816 s[i] = '\0';
14817 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14818 }
14819 }
14820 }
14821
14822
14823 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14824 Sdump_glyph_matrix, 0, 1, "p",
14825 doc: /* Dump the current matrix of the selected window to stderr.
14826 Shows contents of glyph row structures. With non-nil
14827 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14828 glyphs in short form, otherwise show glyphs in long form. */)
14829 (glyphs)
14830 Lisp_Object glyphs;
14831 {
14832 struct window *w = XWINDOW (selected_window);
14833 struct buffer *buffer = XBUFFER (w->buffer);
14834
14835 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14836 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14837 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14838 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14839 fprintf (stderr, "=============================================\n");
14840 dump_glyph_matrix (w->current_matrix,
14841 NILP (glyphs) ? 0 : XINT (glyphs));
14842 return Qnil;
14843 }
14844
14845
14846 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14847 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14848 ()
14849 {
14850 struct frame *f = XFRAME (selected_frame);
14851 dump_glyph_matrix (f->current_matrix, 1);
14852 return Qnil;
14853 }
14854
14855
14856 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14857 doc: /* Dump glyph row ROW to stderr.
14858 GLYPH 0 means don't dump glyphs.
14859 GLYPH 1 means dump glyphs in short form.
14860 GLYPH > 1 or omitted means dump glyphs in long form. */)
14861 (row, glyphs)
14862 Lisp_Object row, glyphs;
14863 {
14864 struct glyph_matrix *matrix;
14865 int vpos;
14866
14867 CHECK_NUMBER (row);
14868 matrix = XWINDOW (selected_window)->current_matrix;
14869 vpos = XINT (row);
14870 if (vpos >= 0 && vpos < matrix->nrows)
14871 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14872 vpos,
14873 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14874 return Qnil;
14875 }
14876
14877
14878 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14879 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14880 GLYPH 0 means don't dump glyphs.
14881 GLYPH 1 means dump glyphs in short form.
14882 GLYPH > 1 or omitted means dump glyphs in long form. */)
14883 (row, glyphs)
14884 Lisp_Object row, glyphs;
14885 {
14886 struct frame *sf = SELECTED_FRAME ();
14887 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14888 int vpos;
14889
14890 CHECK_NUMBER (row);
14891 vpos = XINT (row);
14892 if (vpos >= 0 && vpos < m->nrows)
14893 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14894 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14895 return Qnil;
14896 }
14897
14898
14899 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14900 doc: /* Toggle tracing of redisplay.
14901 With ARG, turn tracing on if and only if ARG is positive. */)
14902 (arg)
14903 Lisp_Object arg;
14904 {
14905 if (NILP (arg))
14906 trace_redisplay_p = !trace_redisplay_p;
14907 else
14908 {
14909 arg = Fprefix_numeric_value (arg);
14910 trace_redisplay_p = XINT (arg) > 0;
14911 }
14912
14913 return Qnil;
14914 }
14915
14916
14917 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14918 doc: /* Like `format', but print result to stderr.
14919 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14920 (nargs, args)
14921 int nargs;
14922 Lisp_Object *args;
14923 {
14924 Lisp_Object s = Fformat (nargs, args);
14925 fprintf (stderr, "%s", SDATA (s));
14926 return Qnil;
14927 }
14928
14929 #endif /* GLYPH_DEBUG */
14930
14931
14932 \f
14933 /***********************************************************************
14934 Building Desired Matrix Rows
14935 ***********************************************************************/
14936
14937 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14938 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14939
14940 static struct glyph_row *
14941 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14942 struct window *w;
14943 Lisp_Object overlay_arrow_string;
14944 {
14945 struct frame *f = XFRAME (WINDOW_FRAME (w));
14946 struct buffer *buffer = XBUFFER (w->buffer);
14947 struct buffer *old = current_buffer;
14948 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14949 int arrow_len = SCHARS (overlay_arrow_string);
14950 const unsigned char *arrow_end = arrow_string + arrow_len;
14951 const unsigned char *p;
14952 struct it it;
14953 int multibyte_p;
14954 int n_glyphs_before;
14955
14956 set_buffer_temp (buffer);
14957 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14958 it.glyph_row->used[TEXT_AREA] = 0;
14959 SET_TEXT_POS (it.position, 0, 0);
14960
14961 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14962 p = arrow_string;
14963 while (p < arrow_end)
14964 {
14965 Lisp_Object face, ilisp;
14966
14967 /* Get the next character. */
14968 if (multibyte_p)
14969 it.c = string_char_and_length (p, arrow_len, &it.len);
14970 else
14971 it.c = *p, it.len = 1;
14972 p += it.len;
14973
14974 /* Get its face. */
14975 ilisp = make_number (p - arrow_string);
14976 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14977 it.face_id = compute_char_face (f, it.c, face);
14978
14979 /* Compute its width, get its glyphs. */
14980 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14981 SET_TEXT_POS (it.position, -1, -1);
14982 PRODUCE_GLYPHS (&it);
14983
14984 /* If this character doesn't fit any more in the line, we have
14985 to remove some glyphs. */
14986 if (it.current_x > it.last_visible_x)
14987 {
14988 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14989 break;
14990 }
14991 }
14992
14993 set_buffer_temp (old);
14994 return it.glyph_row;
14995 }
14996
14997
14998 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14999 glyphs are only inserted for terminal frames since we can't really
15000 win with truncation glyphs when partially visible glyphs are
15001 involved. Which glyphs to insert is determined by
15002 produce_special_glyphs. */
15003
15004 static void
15005 insert_left_trunc_glyphs (it)
15006 struct it *it;
15007 {
15008 struct it truncate_it;
15009 struct glyph *from, *end, *to, *toend;
15010
15011 xassert (!FRAME_WINDOW_P (it->f));
15012
15013 /* Get the truncation glyphs. */
15014 truncate_it = *it;
15015 truncate_it.current_x = 0;
15016 truncate_it.face_id = DEFAULT_FACE_ID;
15017 truncate_it.glyph_row = &scratch_glyph_row;
15018 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15019 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15020 truncate_it.object = make_number (0);
15021 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15022
15023 /* Overwrite glyphs from IT with truncation glyphs. */
15024 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15025 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15026 to = it->glyph_row->glyphs[TEXT_AREA];
15027 toend = to + it->glyph_row->used[TEXT_AREA];
15028
15029 while (from < end)
15030 *to++ = *from++;
15031
15032 /* There may be padding glyphs left over. Overwrite them too. */
15033 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15034 {
15035 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15036 while (from < end)
15037 *to++ = *from++;
15038 }
15039
15040 if (to > toend)
15041 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15042 }
15043
15044
15045 /* Compute the pixel height and width of IT->glyph_row.
15046
15047 Most of the time, ascent and height of a display line will be equal
15048 to the max_ascent and max_height values of the display iterator
15049 structure. This is not the case if
15050
15051 1. We hit ZV without displaying anything. In this case, max_ascent
15052 and max_height will be zero.
15053
15054 2. We have some glyphs that don't contribute to the line height.
15055 (The glyph row flag contributes_to_line_height_p is for future
15056 pixmap extensions).
15057
15058 The first case is easily covered by using default values because in
15059 these cases, the line height does not really matter, except that it
15060 must not be zero. */
15061
15062 static void
15063 compute_line_metrics (it)
15064 struct it *it;
15065 {
15066 struct glyph_row *row = it->glyph_row;
15067 int area, i;
15068
15069 if (FRAME_WINDOW_P (it->f))
15070 {
15071 int i, min_y, max_y;
15072
15073 /* The line may consist of one space only, that was added to
15074 place the cursor on it. If so, the row's height hasn't been
15075 computed yet. */
15076 if (row->height == 0)
15077 {
15078 if (it->max_ascent + it->max_descent == 0)
15079 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15080 row->ascent = it->max_ascent;
15081 row->height = it->max_ascent + it->max_descent;
15082 row->phys_ascent = it->max_phys_ascent;
15083 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15084 row->extra_line_spacing = it->max_extra_line_spacing;
15085 }
15086
15087 /* Compute the width of this line. */
15088 row->pixel_width = row->x;
15089 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15090 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15091
15092 xassert (row->pixel_width >= 0);
15093 xassert (row->ascent >= 0 && row->height > 0);
15094
15095 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15096 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15097
15098 /* If first line's physical ascent is larger than its logical
15099 ascent, use the physical ascent, and make the row taller.
15100 This makes accented characters fully visible. */
15101 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15102 && row->phys_ascent > row->ascent)
15103 {
15104 row->height += row->phys_ascent - row->ascent;
15105 row->ascent = row->phys_ascent;
15106 }
15107
15108 /* Compute how much of the line is visible. */
15109 row->visible_height = row->height;
15110
15111 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15112 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15113
15114 if (row->y < min_y)
15115 row->visible_height -= min_y - row->y;
15116 if (row->y + row->height > max_y)
15117 row->visible_height -= row->y + row->height - max_y;
15118 }
15119 else
15120 {
15121 row->pixel_width = row->used[TEXT_AREA];
15122 if (row->continued_p)
15123 row->pixel_width -= it->continuation_pixel_width;
15124 else if (row->truncated_on_right_p)
15125 row->pixel_width -= it->truncation_pixel_width;
15126 row->ascent = row->phys_ascent = 0;
15127 row->height = row->phys_height = row->visible_height = 1;
15128 row->extra_line_spacing = 0;
15129 }
15130
15131 /* Compute a hash code for this row. */
15132 row->hash = 0;
15133 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15134 for (i = 0; i < row->used[area]; ++i)
15135 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15136 + row->glyphs[area][i].u.val
15137 + row->glyphs[area][i].face_id
15138 + row->glyphs[area][i].padding_p
15139 + (row->glyphs[area][i].type << 2));
15140
15141 it->max_ascent = it->max_descent = 0;
15142 it->max_phys_ascent = it->max_phys_descent = 0;
15143 }
15144
15145
15146 /* Append one space to the glyph row of iterator IT if doing a
15147 window-based redisplay. The space has the same face as
15148 IT->face_id. Value is non-zero if a space was added.
15149
15150 This function is called to make sure that there is always one glyph
15151 at the end of a glyph row that the cursor can be set on under
15152 window-systems. (If there weren't such a glyph we would not know
15153 how wide and tall a box cursor should be displayed).
15154
15155 At the same time this space let's a nicely handle clearing to the
15156 end of the line if the row ends in italic text. */
15157
15158 static int
15159 append_space_for_newline (it, default_face_p)
15160 struct it *it;
15161 int default_face_p;
15162 {
15163 if (FRAME_WINDOW_P (it->f))
15164 {
15165 int n = it->glyph_row->used[TEXT_AREA];
15166
15167 if (it->glyph_row->glyphs[TEXT_AREA] + n
15168 < it->glyph_row->glyphs[1 + TEXT_AREA])
15169 {
15170 /* Save some values that must not be changed.
15171 Must save IT->c and IT->len because otherwise
15172 ITERATOR_AT_END_P wouldn't work anymore after
15173 append_space_for_newline has been called. */
15174 enum display_element_type saved_what = it->what;
15175 int saved_c = it->c, saved_len = it->len;
15176 int saved_x = it->current_x;
15177 int saved_face_id = it->face_id;
15178 struct text_pos saved_pos;
15179 Lisp_Object saved_object;
15180 struct face *face;
15181
15182 saved_object = it->object;
15183 saved_pos = it->position;
15184
15185 it->what = IT_CHARACTER;
15186 bzero (&it->position, sizeof it->position);
15187 it->object = make_number (0);
15188 it->c = ' ';
15189 it->len = 1;
15190
15191 if (default_face_p)
15192 it->face_id = DEFAULT_FACE_ID;
15193 else if (it->face_before_selective_p)
15194 it->face_id = it->saved_face_id;
15195 face = FACE_FROM_ID (it->f, it->face_id);
15196 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15197
15198 PRODUCE_GLYPHS (it);
15199
15200 it->override_ascent = -1;
15201 it->constrain_row_ascent_descent_p = 0;
15202 it->current_x = saved_x;
15203 it->object = saved_object;
15204 it->position = saved_pos;
15205 it->what = saved_what;
15206 it->face_id = saved_face_id;
15207 it->len = saved_len;
15208 it->c = saved_c;
15209 return 1;
15210 }
15211 }
15212
15213 return 0;
15214 }
15215
15216
15217 /* Extend the face of the last glyph in the text area of IT->glyph_row
15218 to the end of the display line. Called from display_line.
15219 If the glyph row is empty, add a space glyph to it so that we
15220 know the face to draw. Set the glyph row flag fill_line_p. */
15221
15222 static void
15223 extend_face_to_end_of_line (it)
15224 struct it *it;
15225 {
15226 struct face *face;
15227 struct frame *f = it->f;
15228
15229 /* If line is already filled, do nothing. */
15230 if (it->current_x >= it->last_visible_x)
15231 return;
15232
15233 /* Face extension extends the background and box of IT->face_id
15234 to the end of the line. If the background equals the background
15235 of the frame, we don't have to do anything. */
15236 if (it->face_before_selective_p)
15237 face = FACE_FROM_ID (it->f, it->saved_face_id);
15238 else
15239 face = FACE_FROM_ID (f, it->face_id);
15240
15241 if (FRAME_WINDOW_P (f)
15242 && face->box == FACE_NO_BOX
15243 && face->background == FRAME_BACKGROUND_PIXEL (f)
15244 && !face->stipple)
15245 return;
15246
15247 /* Set the glyph row flag indicating that the face of the last glyph
15248 in the text area has to be drawn to the end of the text area. */
15249 it->glyph_row->fill_line_p = 1;
15250
15251 /* If current character of IT is not ASCII, make sure we have the
15252 ASCII face. This will be automatically undone the next time
15253 get_next_display_element returns a multibyte character. Note
15254 that the character will always be single byte in unibyte text. */
15255 if (!SINGLE_BYTE_CHAR_P (it->c))
15256 {
15257 it->face_id = FACE_FOR_CHAR (f, face, 0);
15258 }
15259
15260 if (FRAME_WINDOW_P (f))
15261 {
15262 /* If the row is empty, add a space with the current face of IT,
15263 so that we know which face to draw. */
15264 if (it->glyph_row->used[TEXT_AREA] == 0)
15265 {
15266 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15267 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15268 it->glyph_row->used[TEXT_AREA] = 1;
15269 }
15270 }
15271 else
15272 {
15273 /* Save some values that must not be changed. */
15274 int saved_x = it->current_x;
15275 struct text_pos saved_pos;
15276 Lisp_Object saved_object;
15277 enum display_element_type saved_what = it->what;
15278 int saved_face_id = it->face_id;
15279
15280 saved_object = it->object;
15281 saved_pos = it->position;
15282
15283 it->what = IT_CHARACTER;
15284 bzero (&it->position, sizeof it->position);
15285 it->object = make_number (0);
15286 it->c = ' ';
15287 it->len = 1;
15288 it->face_id = face->id;
15289
15290 PRODUCE_GLYPHS (it);
15291
15292 while (it->current_x <= it->last_visible_x)
15293 PRODUCE_GLYPHS (it);
15294
15295 /* Don't count these blanks really. It would let us insert a left
15296 truncation glyph below and make us set the cursor on them, maybe. */
15297 it->current_x = saved_x;
15298 it->object = saved_object;
15299 it->position = saved_pos;
15300 it->what = saved_what;
15301 it->face_id = saved_face_id;
15302 }
15303 }
15304
15305
15306 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15307 trailing whitespace. */
15308
15309 static int
15310 trailing_whitespace_p (charpos)
15311 int charpos;
15312 {
15313 int bytepos = CHAR_TO_BYTE (charpos);
15314 int c = 0;
15315
15316 while (bytepos < ZV_BYTE
15317 && (c = FETCH_CHAR (bytepos),
15318 c == ' ' || c == '\t'))
15319 ++bytepos;
15320
15321 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15322 {
15323 if (bytepos != PT_BYTE)
15324 return 1;
15325 }
15326 return 0;
15327 }
15328
15329
15330 /* Highlight trailing whitespace, if any, in ROW. */
15331
15332 void
15333 highlight_trailing_whitespace (f, row)
15334 struct frame *f;
15335 struct glyph_row *row;
15336 {
15337 int used = row->used[TEXT_AREA];
15338
15339 if (used)
15340 {
15341 struct glyph *start = row->glyphs[TEXT_AREA];
15342 struct glyph *glyph = start + used - 1;
15343
15344 /* Skip over glyphs inserted to display the cursor at the
15345 end of a line, for extending the face of the last glyph
15346 to the end of the line on terminals, and for truncation
15347 and continuation glyphs. */
15348 while (glyph >= start
15349 && glyph->type == CHAR_GLYPH
15350 && INTEGERP (glyph->object))
15351 --glyph;
15352
15353 /* If last glyph is a space or stretch, and it's trailing
15354 whitespace, set the face of all trailing whitespace glyphs in
15355 IT->glyph_row to `trailing-whitespace'. */
15356 if (glyph >= start
15357 && BUFFERP (glyph->object)
15358 && (glyph->type == STRETCH_GLYPH
15359 || (glyph->type == CHAR_GLYPH
15360 && glyph->u.ch == ' '))
15361 && trailing_whitespace_p (glyph->charpos))
15362 {
15363 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15364 if (face_id < 0)
15365 return;
15366
15367 while (glyph >= start
15368 && BUFFERP (glyph->object)
15369 && (glyph->type == STRETCH_GLYPH
15370 || (glyph->type == CHAR_GLYPH
15371 && glyph->u.ch == ' ')))
15372 (glyph--)->face_id = face_id;
15373 }
15374 }
15375 }
15376
15377
15378 /* Value is non-zero if glyph row ROW in window W should be
15379 used to hold the cursor. */
15380
15381 static int
15382 cursor_row_p (w, row)
15383 struct window *w;
15384 struct glyph_row *row;
15385 {
15386 int cursor_row_p = 1;
15387
15388 if (PT == MATRIX_ROW_END_CHARPOS (row))
15389 {
15390 /* If the row ends with a newline from a string, we don't want
15391 the cursor there, but we still want it at the start of the
15392 string if the string starts in this row.
15393 If the row is continued it doesn't end in a newline. */
15394 if (CHARPOS (row->end.string_pos) >= 0)
15395 cursor_row_p = (row->continued_p
15396 || PT >= MATRIX_ROW_START_CHARPOS (row));
15397 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15398 {
15399 /* If the row ends in middle of a real character,
15400 and the line is continued, we want the cursor here.
15401 That's because MATRIX_ROW_END_CHARPOS would equal
15402 PT if PT is before the character. */
15403 if (!row->ends_in_ellipsis_p)
15404 cursor_row_p = row->continued_p;
15405 else
15406 /* If the row ends in an ellipsis, then
15407 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15408 We want that position to be displayed after the ellipsis. */
15409 cursor_row_p = 0;
15410 }
15411 /* If the row ends at ZV, display the cursor at the end of that
15412 row instead of at the start of the row below. */
15413 else if (row->ends_at_zv_p)
15414 cursor_row_p = 1;
15415 else
15416 cursor_row_p = 0;
15417 }
15418
15419 return cursor_row_p;
15420 }
15421
15422
15423 /* Construct the glyph row IT->glyph_row in the desired matrix of
15424 IT->w from text at the current position of IT. See dispextern.h
15425 for an overview of struct it. Value is non-zero if
15426 IT->glyph_row displays text, as opposed to a line displaying ZV
15427 only. */
15428
15429 static int
15430 display_line (it)
15431 struct it *it;
15432 {
15433 struct glyph_row *row = it->glyph_row;
15434 Lisp_Object overlay_arrow_string;
15435
15436 /* We always start displaying at hpos zero even if hscrolled. */
15437 xassert (it->hpos == 0 && it->current_x == 0);
15438
15439 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15440 >= it->w->desired_matrix->nrows)
15441 {
15442 it->w->nrows_scale_factor++;
15443 fonts_changed_p = 1;
15444 return 0;
15445 }
15446
15447 /* Is IT->w showing the region? */
15448 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15449
15450 /* Clear the result glyph row and enable it. */
15451 prepare_desired_row (row);
15452
15453 row->y = it->current_y;
15454 row->start = it->start;
15455 row->continuation_lines_width = it->continuation_lines_width;
15456 row->displays_text_p = 1;
15457 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15458 it->starts_in_middle_of_char_p = 0;
15459
15460 /* Arrange the overlays nicely for our purposes. Usually, we call
15461 display_line on only one line at a time, in which case this
15462 can't really hurt too much, or we call it on lines which appear
15463 one after another in the buffer, in which case all calls to
15464 recenter_overlay_lists but the first will be pretty cheap. */
15465 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15466
15467 /* Move over display elements that are not visible because we are
15468 hscrolled. This may stop at an x-position < IT->first_visible_x
15469 if the first glyph is partially visible or if we hit a line end. */
15470 if (it->current_x < it->first_visible_x)
15471 {
15472 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15473 MOVE_TO_POS | MOVE_TO_X);
15474 }
15475
15476 /* Get the initial row height. This is either the height of the
15477 text hscrolled, if there is any, or zero. */
15478 row->ascent = it->max_ascent;
15479 row->height = it->max_ascent + it->max_descent;
15480 row->phys_ascent = it->max_phys_ascent;
15481 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15482 row->extra_line_spacing = it->max_extra_line_spacing;
15483
15484 /* Loop generating characters. The loop is left with IT on the next
15485 character to display. */
15486 while (1)
15487 {
15488 int n_glyphs_before, hpos_before, x_before;
15489 int x, i, nglyphs;
15490 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15491
15492 /* Retrieve the next thing to display. Value is zero if end of
15493 buffer reached. */
15494 if (!get_next_display_element (it))
15495 {
15496 /* Maybe add a space at the end of this line that is used to
15497 display the cursor there under X. Set the charpos of the
15498 first glyph of blank lines not corresponding to any text
15499 to -1. */
15500 #ifdef HAVE_WINDOW_SYSTEM
15501 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15502 row->exact_window_width_line_p = 1;
15503 else
15504 #endif /* HAVE_WINDOW_SYSTEM */
15505 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15506 || row->used[TEXT_AREA] == 0)
15507 {
15508 row->glyphs[TEXT_AREA]->charpos = -1;
15509 row->displays_text_p = 0;
15510
15511 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15512 && (!MINI_WINDOW_P (it->w)
15513 || (minibuf_level && EQ (it->window, minibuf_window))))
15514 row->indicate_empty_line_p = 1;
15515 }
15516
15517 it->continuation_lines_width = 0;
15518 row->ends_at_zv_p = 1;
15519 break;
15520 }
15521
15522 /* Now, get the metrics of what we want to display. This also
15523 generates glyphs in `row' (which is IT->glyph_row). */
15524 n_glyphs_before = row->used[TEXT_AREA];
15525 x = it->current_x;
15526
15527 /* Remember the line height so far in case the next element doesn't
15528 fit on the line. */
15529 if (!it->truncate_lines_p)
15530 {
15531 ascent = it->max_ascent;
15532 descent = it->max_descent;
15533 phys_ascent = it->max_phys_ascent;
15534 phys_descent = it->max_phys_descent;
15535 }
15536
15537 PRODUCE_GLYPHS (it);
15538
15539 /* If this display element was in marginal areas, continue with
15540 the next one. */
15541 if (it->area != TEXT_AREA)
15542 {
15543 row->ascent = max (row->ascent, it->max_ascent);
15544 row->height = max (row->height, it->max_ascent + it->max_descent);
15545 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15546 row->phys_height = max (row->phys_height,
15547 it->max_phys_ascent + it->max_phys_descent);
15548 row->extra_line_spacing = max (row->extra_line_spacing,
15549 it->max_extra_line_spacing);
15550 set_iterator_to_next (it, 1);
15551 continue;
15552 }
15553
15554 /* Does the display element fit on the line? If we truncate
15555 lines, we should draw past the right edge of the window. If
15556 we don't truncate, we want to stop so that we can display the
15557 continuation glyph before the right margin. If lines are
15558 continued, there are two possible strategies for characters
15559 resulting in more than 1 glyph (e.g. tabs): Display as many
15560 glyphs as possible in this line and leave the rest for the
15561 continuation line, or display the whole element in the next
15562 line. Original redisplay did the former, so we do it also. */
15563 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15564 hpos_before = it->hpos;
15565 x_before = x;
15566
15567 if (/* Not a newline. */
15568 nglyphs > 0
15569 /* Glyphs produced fit entirely in the line. */
15570 && it->current_x < it->last_visible_x)
15571 {
15572 it->hpos += nglyphs;
15573 row->ascent = max (row->ascent, it->max_ascent);
15574 row->height = max (row->height, it->max_ascent + it->max_descent);
15575 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15576 row->phys_height = max (row->phys_height,
15577 it->max_phys_ascent + it->max_phys_descent);
15578 row->extra_line_spacing = max (row->extra_line_spacing,
15579 it->max_extra_line_spacing);
15580 if (it->current_x - it->pixel_width < it->first_visible_x)
15581 row->x = x - it->first_visible_x;
15582 }
15583 else
15584 {
15585 int new_x;
15586 struct glyph *glyph;
15587
15588 for (i = 0; i < nglyphs; ++i, x = new_x)
15589 {
15590 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15591 new_x = x + glyph->pixel_width;
15592
15593 if (/* Lines are continued. */
15594 !it->truncate_lines_p
15595 && (/* Glyph doesn't fit on the line. */
15596 new_x > it->last_visible_x
15597 /* Or it fits exactly on a window system frame. */
15598 || (new_x == it->last_visible_x
15599 && FRAME_WINDOW_P (it->f))))
15600 {
15601 /* End of a continued line. */
15602
15603 if (it->hpos == 0
15604 || (new_x == it->last_visible_x
15605 && FRAME_WINDOW_P (it->f)))
15606 {
15607 /* Current glyph is the only one on the line or
15608 fits exactly on the line. We must continue
15609 the line because we can't draw the cursor
15610 after the glyph. */
15611 row->continued_p = 1;
15612 it->current_x = new_x;
15613 it->continuation_lines_width += new_x;
15614 ++it->hpos;
15615 if (i == nglyphs - 1)
15616 {
15617 set_iterator_to_next (it, 1);
15618 #ifdef HAVE_WINDOW_SYSTEM
15619 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15620 {
15621 if (!get_next_display_element (it))
15622 {
15623 row->exact_window_width_line_p = 1;
15624 it->continuation_lines_width = 0;
15625 row->continued_p = 0;
15626 row->ends_at_zv_p = 1;
15627 }
15628 else if (ITERATOR_AT_END_OF_LINE_P (it))
15629 {
15630 row->continued_p = 0;
15631 row->exact_window_width_line_p = 1;
15632 }
15633 }
15634 #endif /* HAVE_WINDOW_SYSTEM */
15635 }
15636 }
15637 else if (CHAR_GLYPH_PADDING_P (*glyph)
15638 && !FRAME_WINDOW_P (it->f))
15639 {
15640 /* A padding glyph that doesn't fit on this line.
15641 This means the whole character doesn't fit
15642 on the line. */
15643 row->used[TEXT_AREA] = n_glyphs_before;
15644
15645 /* Fill the rest of the row with continuation
15646 glyphs like in 20.x. */
15647 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15648 < row->glyphs[1 + TEXT_AREA])
15649 produce_special_glyphs (it, IT_CONTINUATION);
15650
15651 row->continued_p = 1;
15652 it->current_x = x_before;
15653 it->continuation_lines_width += x_before;
15654
15655 /* Restore the height to what it was before the
15656 element not fitting on the line. */
15657 it->max_ascent = ascent;
15658 it->max_descent = descent;
15659 it->max_phys_ascent = phys_ascent;
15660 it->max_phys_descent = phys_descent;
15661 }
15662 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15663 {
15664 /* A TAB that extends past the right edge of the
15665 window. This produces a single glyph on
15666 window system frames. We leave the glyph in
15667 this row and let it fill the row, but don't
15668 consume the TAB. */
15669 it->continuation_lines_width += it->last_visible_x;
15670 row->ends_in_middle_of_char_p = 1;
15671 row->continued_p = 1;
15672 glyph->pixel_width = it->last_visible_x - x;
15673 it->starts_in_middle_of_char_p = 1;
15674 }
15675 else
15676 {
15677 /* Something other than a TAB that draws past
15678 the right edge of the window. Restore
15679 positions to values before the element. */
15680 row->used[TEXT_AREA] = n_glyphs_before + i;
15681
15682 /* Display continuation glyphs. */
15683 if (!FRAME_WINDOW_P (it->f))
15684 produce_special_glyphs (it, IT_CONTINUATION);
15685 row->continued_p = 1;
15686
15687 it->current_x = x_before;
15688 it->continuation_lines_width += x;
15689 extend_face_to_end_of_line (it);
15690
15691 if (nglyphs > 1 && i > 0)
15692 {
15693 row->ends_in_middle_of_char_p = 1;
15694 it->starts_in_middle_of_char_p = 1;
15695 }
15696
15697 /* Restore the height to what it was before the
15698 element not fitting on the line. */
15699 it->max_ascent = ascent;
15700 it->max_descent = descent;
15701 it->max_phys_ascent = phys_ascent;
15702 it->max_phys_descent = phys_descent;
15703 }
15704
15705 break;
15706 }
15707 else if (new_x > it->first_visible_x)
15708 {
15709 /* Increment number of glyphs actually displayed. */
15710 ++it->hpos;
15711
15712 if (x < it->first_visible_x)
15713 /* Glyph is partially visible, i.e. row starts at
15714 negative X position. */
15715 row->x = x - it->first_visible_x;
15716 }
15717 else
15718 {
15719 /* Glyph is completely off the left margin of the
15720 window. This should not happen because of the
15721 move_it_in_display_line at the start of this
15722 function, unless the text display area of the
15723 window is empty. */
15724 xassert (it->first_visible_x <= it->last_visible_x);
15725 }
15726 }
15727
15728 row->ascent = max (row->ascent, it->max_ascent);
15729 row->height = max (row->height, it->max_ascent + it->max_descent);
15730 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15731 row->phys_height = max (row->phys_height,
15732 it->max_phys_ascent + it->max_phys_descent);
15733 row->extra_line_spacing = max (row->extra_line_spacing,
15734 it->max_extra_line_spacing);
15735
15736 /* End of this display line if row is continued. */
15737 if (row->continued_p || row->ends_at_zv_p)
15738 break;
15739 }
15740
15741 at_end_of_line:
15742 /* Is this a line end? If yes, we're also done, after making
15743 sure that a non-default face is extended up to the right
15744 margin of the window. */
15745 if (ITERATOR_AT_END_OF_LINE_P (it))
15746 {
15747 int used_before = row->used[TEXT_AREA];
15748
15749 row->ends_in_newline_from_string_p = STRINGP (it->object);
15750
15751 #ifdef HAVE_WINDOW_SYSTEM
15752 /* Add a space at the end of the line that is used to
15753 display the cursor there. */
15754 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15755 append_space_for_newline (it, 0);
15756 #endif /* HAVE_WINDOW_SYSTEM */
15757
15758 /* Extend the face to the end of the line. */
15759 extend_face_to_end_of_line (it);
15760
15761 /* Make sure we have the position. */
15762 if (used_before == 0)
15763 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15764
15765 /* Consume the line end. This skips over invisible lines. */
15766 set_iterator_to_next (it, 1);
15767 it->continuation_lines_width = 0;
15768 break;
15769 }
15770
15771 /* Proceed with next display element. Note that this skips
15772 over lines invisible because of selective display. */
15773 set_iterator_to_next (it, 1);
15774
15775 /* If we truncate lines, we are done when the last displayed
15776 glyphs reach past the right margin of the window. */
15777 if (it->truncate_lines_p
15778 && (FRAME_WINDOW_P (it->f)
15779 ? (it->current_x >= it->last_visible_x)
15780 : (it->current_x > it->last_visible_x)))
15781 {
15782 /* Maybe add truncation glyphs. */
15783 if (!FRAME_WINDOW_P (it->f))
15784 {
15785 int i, n;
15786
15787 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15788 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15789 break;
15790
15791 for (n = row->used[TEXT_AREA]; i < n; ++i)
15792 {
15793 row->used[TEXT_AREA] = i;
15794 produce_special_glyphs (it, IT_TRUNCATION);
15795 }
15796 }
15797 #ifdef HAVE_WINDOW_SYSTEM
15798 else
15799 {
15800 /* Don't truncate if we can overflow newline into fringe. */
15801 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15802 {
15803 if (!get_next_display_element (it))
15804 {
15805 it->continuation_lines_width = 0;
15806 row->ends_at_zv_p = 1;
15807 row->exact_window_width_line_p = 1;
15808 break;
15809 }
15810 if (ITERATOR_AT_END_OF_LINE_P (it))
15811 {
15812 row->exact_window_width_line_p = 1;
15813 goto at_end_of_line;
15814 }
15815 }
15816 }
15817 #endif /* HAVE_WINDOW_SYSTEM */
15818
15819 row->truncated_on_right_p = 1;
15820 it->continuation_lines_width = 0;
15821 reseat_at_next_visible_line_start (it, 0);
15822 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15823 it->hpos = hpos_before;
15824 it->current_x = x_before;
15825 break;
15826 }
15827 }
15828
15829 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15830 at the left window margin. */
15831 if (it->first_visible_x
15832 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15833 {
15834 if (!FRAME_WINDOW_P (it->f))
15835 insert_left_trunc_glyphs (it);
15836 row->truncated_on_left_p = 1;
15837 }
15838
15839 /* If the start of this line is the overlay arrow-position, then
15840 mark this glyph row as the one containing the overlay arrow.
15841 This is clearly a mess with variable size fonts. It would be
15842 better to let it be displayed like cursors under X. */
15843 if ((row->displays_text_p || !overlay_arrow_seen)
15844 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15845 !NILP (overlay_arrow_string)))
15846 {
15847 /* Overlay arrow in window redisplay is a fringe bitmap. */
15848 if (STRINGP (overlay_arrow_string))
15849 {
15850 struct glyph_row *arrow_row
15851 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15852 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15853 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15854 struct glyph *p = row->glyphs[TEXT_AREA];
15855 struct glyph *p2, *end;
15856
15857 /* Copy the arrow glyphs. */
15858 while (glyph < arrow_end)
15859 *p++ = *glyph++;
15860
15861 /* Throw away padding glyphs. */
15862 p2 = p;
15863 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15864 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15865 ++p2;
15866 if (p2 > p)
15867 {
15868 while (p2 < end)
15869 *p++ = *p2++;
15870 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15871 }
15872 }
15873 else
15874 {
15875 xassert (INTEGERP (overlay_arrow_string));
15876 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15877 }
15878 overlay_arrow_seen = 1;
15879 }
15880
15881 /* Compute pixel dimensions of this line. */
15882 compute_line_metrics (it);
15883
15884 /* Remember the position at which this line ends. */
15885 row->end = it->current;
15886
15887 /* Record whether this row ends inside an ellipsis. */
15888 row->ends_in_ellipsis_p
15889 = (it->method == GET_FROM_DISPLAY_VECTOR
15890 && it->ellipsis_p);
15891
15892 /* Save fringe bitmaps in this row. */
15893 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15894 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15895 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15896 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15897
15898 it->left_user_fringe_bitmap = 0;
15899 it->left_user_fringe_face_id = 0;
15900 it->right_user_fringe_bitmap = 0;
15901 it->right_user_fringe_face_id = 0;
15902
15903 /* Maybe set the cursor. */
15904 if (it->w->cursor.vpos < 0
15905 && PT >= MATRIX_ROW_START_CHARPOS (row)
15906 && PT <= MATRIX_ROW_END_CHARPOS (row)
15907 && cursor_row_p (it->w, row))
15908 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15909
15910 /* Highlight trailing whitespace. */
15911 if (!NILP (Vshow_trailing_whitespace))
15912 highlight_trailing_whitespace (it->f, it->glyph_row);
15913
15914 /* Prepare for the next line. This line starts horizontally at (X
15915 HPOS) = (0 0). Vertical positions are incremented. As a
15916 convenience for the caller, IT->glyph_row is set to the next
15917 row to be used. */
15918 it->current_x = it->hpos = 0;
15919 it->current_y += row->height;
15920 ++it->vpos;
15921 ++it->glyph_row;
15922 it->start = it->current;
15923 return row->displays_text_p;
15924 }
15925
15926
15927 \f
15928 /***********************************************************************
15929 Menu Bar
15930 ***********************************************************************/
15931
15932 /* Redisplay the menu bar in the frame for window W.
15933
15934 The menu bar of X frames that don't have X toolkit support is
15935 displayed in a special window W->frame->menu_bar_window.
15936
15937 The menu bar of terminal frames is treated specially as far as
15938 glyph matrices are concerned. Menu bar lines are not part of
15939 windows, so the update is done directly on the frame matrix rows
15940 for the menu bar. */
15941
15942 static void
15943 display_menu_bar (w)
15944 struct window *w;
15945 {
15946 struct frame *f = XFRAME (WINDOW_FRAME (w));
15947 struct it it;
15948 Lisp_Object items;
15949 int i;
15950
15951 /* Don't do all this for graphical frames. */
15952 #ifdef HAVE_NTGUI
15953 if (!NILP (Vwindow_system))
15954 return;
15955 #endif
15956 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15957 if (FRAME_X_P (f))
15958 return;
15959 #endif
15960 #ifdef MAC_OS
15961 if (FRAME_MAC_P (f))
15962 return;
15963 #endif
15964
15965 #ifdef USE_X_TOOLKIT
15966 xassert (!FRAME_WINDOW_P (f));
15967 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15968 it.first_visible_x = 0;
15969 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15970 #else /* not USE_X_TOOLKIT */
15971 if (FRAME_WINDOW_P (f))
15972 {
15973 /* Menu bar lines are displayed in the desired matrix of the
15974 dummy window menu_bar_window. */
15975 struct window *menu_w;
15976 xassert (WINDOWP (f->menu_bar_window));
15977 menu_w = XWINDOW (f->menu_bar_window);
15978 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15979 MENU_FACE_ID);
15980 it.first_visible_x = 0;
15981 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15982 }
15983 else
15984 {
15985 /* This is a TTY frame, i.e. character hpos/vpos are used as
15986 pixel x/y. */
15987 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15988 MENU_FACE_ID);
15989 it.first_visible_x = 0;
15990 it.last_visible_x = FRAME_COLS (f);
15991 }
15992 #endif /* not USE_X_TOOLKIT */
15993
15994 if (! mode_line_inverse_video)
15995 /* Force the menu-bar to be displayed in the default face. */
15996 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15997
15998 /* Clear all rows of the menu bar. */
15999 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16000 {
16001 struct glyph_row *row = it.glyph_row + i;
16002 clear_glyph_row (row);
16003 row->enabled_p = 1;
16004 row->full_width_p = 1;
16005 }
16006
16007 /* Display all items of the menu bar. */
16008 items = FRAME_MENU_BAR_ITEMS (it.f);
16009 for (i = 0; i < XVECTOR (items)->size; i += 4)
16010 {
16011 Lisp_Object string;
16012
16013 /* Stop at nil string. */
16014 string = AREF (items, i + 1);
16015 if (NILP (string))
16016 break;
16017
16018 /* Remember where item was displayed. */
16019 AREF (items, i + 3) = make_number (it.hpos);
16020
16021 /* Display the item, pad with one space. */
16022 if (it.current_x < it.last_visible_x)
16023 display_string (NULL, string, Qnil, 0, 0, &it,
16024 SCHARS (string) + 1, 0, 0, -1);
16025 }
16026
16027 /* Fill out the line with spaces. */
16028 if (it.current_x < it.last_visible_x)
16029 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16030
16031 /* Compute the total height of the lines. */
16032 compute_line_metrics (&it);
16033 }
16034
16035
16036 \f
16037 /***********************************************************************
16038 Mode Line
16039 ***********************************************************************/
16040
16041 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16042 FORCE is non-zero, redisplay mode lines unconditionally.
16043 Otherwise, redisplay only mode lines that are garbaged. Value is
16044 the number of windows whose mode lines were redisplayed. */
16045
16046 static int
16047 redisplay_mode_lines (window, force)
16048 Lisp_Object window;
16049 int force;
16050 {
16051 int nwindows = 0;
16052
16053 while (!NILP (window))
16054 {
16055 struct window *w = XWINDOW (window);
16056
16057 if (WINDOWP (w->hchild))
16058 nwindows += redisplay_mode_lines (w->hchild, force);
16059 else if (WINDOWP (w->vchild))
16060 nwindows += redisplay_mode_lines (w->vchild, force);
16061 else if (force
16062 || FRAME_GARBAGED_P (XFRAME (w->frame))
16063 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16064 {
16065 struct text_pos lpoint;
16066 struct buffer *old = current_buffer;
16067
16068 /* Set the window's buffer for the mode line display. */
16069 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16070 set_buffer_internal_1 (XBUFFER (w->buffer));
16071
16072 /* Point refers normally to the selected window. For any
16073 other window, set up appropriate value. */
16074 if (!EQ (window, selected_window))
16075 {
16076 struct text_pos pt;
16077
16078 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16079 if (CHARPOS (pt) < BEGV)
16080 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16081 else if (CHARPOS (pt) > (ZV - 1))
16082 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16083 else
16084 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16085 }
16086
16087 /* Display mode lines. */
16088 clear_glyph_matrix (w->desired_matrix);
16089 if (display_mode_lines (w))
16090 {
16091 ++nwindows;
16092 w->must_be_updated_p = 1;
16093 }
16094
16095 /* Restore old settings. */
16096 set_buffer_internal_1 (old);
16097 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16098 }
16099
16100 window = w->next;
16101 }
16102
16103 return nwindows;
16104 }
16105
16106
16107 /* Display the mode and/or top line of window W. Value is the number
16108 of mode lines displayed. */
16109
16110 static int
16111 display_mode_lines (w)
16112 struct window *w;
16113 {
16114 Lisp_Object old_selected_window, old_selected_frame;
16115 int n = 0;
16116
16117 old_selected_frame = selected_frame;
16118 selected_frame = w->frame;
16119 old_selected_window = selected_window;
16120 XSETWINDOW (selected_window, w);
16121
16122 /* These will be set while the mode line specs are processed. */
16123 line_number_displayed = 0;
16124 w->column_number_displayed = Qnil;
16125
16126 if (WINDOW_WANTS_MODELINE_P (w))
16127 {
16128 struct window *sel_w = XWINDOW (old_selected_window);
16129
16130 /* Select mode line face based on the real selected window. */
16131 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16132 current_buffer->mode_line_format);
16133 ++n;
16134 }
16135
16136 if (WINDOW_WANTS_HEADER_LINE_P (w))
16137 {
16138 display_mode_line (w, HEADER_LINE_FACE_ID,
16139 current_buffer->header_line_format);
16140 ++n;
16141 }
16142
16143 selected_frame = old_selected_frame;
16144 selected_window = old_selected_window;
16145 return n;
16146 }
16147
16148
16149 /* Display mode or top line of window W. FACE_ID specifies which line
16150 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16151 FORMAT is the mode line format to display. Value is the pixel
16152 height of the mode line displayed. */
16153
16154 static int
16155 display_mode_line (w, face_id, format)
16156 struct window *w;
16157 enum face_id face_id;
16158 Lisp_Object format;
16159 {
16160 struct it it;
16161 struct face *face;
16162 int count = SPECPDL_INDEX ();
16163
16164 init_iterator (&it, w, -1, -1, NULL, face_id);
16165 prepare_desired_row (it.glyph_row);
16166
16167 it.glyph_row->mode_line_p = 1;
16168
16169 if (! mode_line_inverse_video)
16170 /* Force the mode-line to be displayed in the default face. */
16171 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16172
16173 record_unwind_protect (unwind_format_mode_line,
16174 format_mode_line_unwind_data (NULL, 0));
16175
16176 mode_line_target = MODE_LINE_DISPLAY;
16177
16178 /* Temporarily make frame's keyboard the current kboard so that
16179 kboard-local variables in the mode_line_format will get the right
16180 values. */
16181 push_frame_kboard (it.f);
16182 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16183 pop_frame_kboard ();
16184
16185 unbind_to (count, Qnil);
16186
16187 /* Fill up with spaces. */
16188 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16189
16190 compute_line_metrics (&it);
16191 it.glyph_row->full_width_p = 1;
16192 it.glyph_row->continued_p = 0;
16193 it.glyph_row->truncated_on_left_p = 0;
16194 it.glyph_row->truncated_on_right_p = 0;
16195
16196 /* Make a 3D mode-line have a shadow at its right end. */
16197 face = FACE_FROM_ID (it.f, face_id);
16198 extend_face_to_end_of_line (&it);
16199 if (face->box != FACE_NO_BOX)
16200 {
16201 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16202 + it.glyph_row->used[TEXT_AREA] - 1);
16203 last->right_box_line_p = 1;
16204 }
16205
16206 return it.glyph_row->height;
16207 }
16208
16209 /* Move element ELT in LIST to the front of LIST.
16210 Return the updated list. */
16211
16212 static Lisp_Object
16213 move_elt_to_front (elt, list)
16214 Lisp_Object elt, list;
16215 {
16216 register Lisp_Object tail, prev;
16217 register Lisp_Object tem;
16218
16219 tail = list;
16220 prev = Qnil;
16221 while (CONSP (tail))
16222 {
16223 tem = XCAR (tail);
16224
16225 if (EQ (elt, tem))
16226 {
16227 /* Splice out the link TAIL. */
16228 if (NILP (prev))
16229 list = XCDR (tail);
16230 else
16231 Fsetcdr (prev, XCDR (tail));
16232
16233 /* Now make it the first. */
16234 Fsetcdr (tail, list);
16235 return tail;
16236 }
16237 else
16238 prev = tail;
16239 tail = XCDR (tail);
16240 QUIT;
16241 }
16242
16243 /* Not found--return unchanged LIST. */
16244 return list;
16245 }
16246
16247 /* Contribute ELT to the mode line for window IT->w. How it
16248 translates into text depends on its data type.
16249
16250 IT describes the display environment in which we display, as usual.
16251
16252 DEPTH is the depth in recursion. It is used to prevent
16253 infinite recursion here.
16254
16255 FIELD_WIDTH is the number of characters the display of ELT should
16256 occupy in the mode line, and PRECISION is the maximum number of
16257 characters to display from ELT's representation. See
16258 display_string for details.
16259
16260 Returns the hpos of the end of the text generated by ELT.
16261
16262 PROPS is a property list to add to any string we encounter.
16263
16264 If RISKY is nonzero, remove (disregard) any properties in any string
16265 we encounter, and ignore :eval and :propertize.
16266
16267 The global variable `mode_line_target' determines whether the
16268 output is passed to `store_mode_line_noprop',
16269 `store_mode_line_string', or `display_string'. */
16270
16271 static int
16272 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16273 struct it *it;
16274 int depth;
16275 int field_width, precision;
16276 Lisp_Object elt, props;
16277 int risky;
16278 {
16279 int n = 0, field, prec;
16280 int literal = 0;
16281
16282 tail_recurse:
16283 if (depth > 100)
16284 elt = build_string ("*too-deep*");
16285
16286 depth++;
16287
16288 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16289 {
16290 case Lisp_String:
16291 {
16292 /* A string: output it and check for %-constructs within it. */
16293 unsigned char c;
16294 int offset = 0;
16295
16296 if (SCHARS (elt) > 0
16297 && (!NILP (props) || risky))
16298 {
16299 Lisp_Object oprops, aelt;
16300 oprops = Ftext_properties_at (make_number (0), elt);
16301
16302 /* If the starting string's properties are not what
16303 we want, translate the string. Also, if the string
16304 is risky, do that anyway. */
16305
16306 if (NILP (Fequal (props, oprops)) || risky)
16307 {
16308 /* If the starting string has properties,
16309 merge the specified ones onto the existing ones. */
16310 if (! NILP (oprops) && !risky)
16311 {
16312 Lisp_Object tem;
16313
16314 oprops = Fcopy_sequence (oprops);
16315 tem = props;
16316 while (CONSP (tem))
16317 {
16318 oprops = Fplist_put (oprops, XCAR (tem),
16319 XCAR (XCDR (tem)));
16320 tem = XCDR (XCDR (tem));
16321 }
16322 props = oprops;
16323 }
16324
16325 aelt = Fassoc (elt, mode_line_proptrans_alist);
16326 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16327 {
16328 /* AELT is what we want. Move it to the front
16329 without consing. */
16330 elt = XCAR (aelt);
16331 mode_line_proptrans_alist
16332 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16333 }
16334 else
16335 {
16336 Lisp_Object tem;
16337
16338 /* If AELT has the wrong props, it is useless.
16339 so get rid of it. */
16340 if (! NILP (aelt))
16341 mode_line_proptrans_alist
16342 = Fdelq (aelt, mode_line_proptrans_alist);
16343
16344 elt = Fcopy_sequence (elt);
16345 Fset_text_properties (make_number (0), Flength (elt),
16346 props, elt);
16347 /* Add this item to mode_line_proptrans_alist. */
16348 mode_line_proptrans_alist
16349 = Fcons (Fcons (elt, props),
16350 mode_line_proptrans_alist);
16351 /* Truncate mode_line_proptrans_alist
16352 to at most 50 elements. */
16353 tem = Fnthcdr (make_number (50),
16354 mode_line_proptrans_alist);
16355 if (! NILP (tem))
16356 XSETCDR (tem, Qnil);
16357 }
16358 }
16359 }
16360
16361 offset = 0;
16362
16363 if (literal)
16364 {
16365 prec = precision - n;
16366 switch (mode_line_target)
16367 {
16368 case MODE_LINE_NOPROP:
16369 case MODE_LINE_TITLE:
16370 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16371 break;
16372 case MODE_LINE_STRING:
16373 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16374 break;
16375 case MODE_LINE_DISPLAY:
16376 n += display_string (NULL, elt, Qnil, 0, 0, it,
16377 0, prec, 0, STRING_MULTIBYTE (elt));
16378 break;
16379 }
16380
16381 break;
16382 }
16383
16384 /* Handle the non-literal case. */
16385
16386 while ((precision <= 0 || n < precision)
16387 && SREF (elt, offset) != 0
16388 && (mode_line_target != MODE_LINE_DISPLAY
16389 || it->current_x < it->last_visible_x))
16390 {
16391 int last_offset = offset;
16392
16393 /* Advance to end of string or next format specifier. */
16394 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16395 ;
16396
16397 if (offset - 1 != last_offset)
16398 {
16399 int nchars, nbytes;
16400
16401 /* Output to end of string or up to '%'. Field width
16402 is length of string. Don't output more than
16403 PRECISION allows us. */
16404 offset--;
16405
16406 prec = c_string_width (SDATA (elt) + last_offset,
16407 offset - last_offset, precision - n,
16408 &nchars, &nbytes);
16409
16410 switch (mode_line_target)
16411 {
16412 case MODE_LINE_NOPROP:
16413 case MODE_LINE_TITLE:
16414 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16415 break;
16416 case MODE_LINE_STRING:
16417 {
16418 int bytepos = last_offset;
16419 int charpos = string_byte_to_char (elt, bytepos);
16420 int endpos = (precision <= 0
16421 ? string_byte_to_char (elt, offset)
16422 : charpos + nchars);
16423
16424 n += store_mode_line_string (NULL,
16425 Fsubstring (elt, make_number (charpos),
16426 make_number (endpos)),
16427 0, 0, 0, Qnil);
16428 }
16429 break;
16430 case MODE_LINE_DISPLAY:
16431 {
16432 int bytepos = last_offset;
16433 int charpos = string_byte_to_char (elt, bytepos);
16434 n += display_string (NULL, elt, Qnil, 0, charpos,
16435 it, 0, prec, 0,
16436 STRING_MULTIBYTE (elt));
16437 }
16438 break;
16439 }
16440 }
16441 else /* c == '%' */
16442 {
16443 int percent_position = offset;
16444
16445 /* Get the specified minimum width. Zero means
16446 don't pad. */
16447 field = 0;
16448 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16449 field = field * 10 + c - '0';
16450
16451 /* Don't pad beyond the total padding allowed. */
16452 if (field_width - n > 0 && field > field_width - n)
16453 field = field_width - n;
16454
16455 /* Note that either PRECISION <= 0 or N < PRECISION. */
16456 prec = precision - n;
16457
16458 if (c == 'M')
16459 n += display_mode_element (it, depth, field, prec,
16460 Vglobal_mode_string, props,
16461 risky);
16462 else if (c != 0)
16463 {
16464 int multibyte;
16465 int bytepos, charpos;
16466 unsigned char *spec;
16467
16468 bytepos = percent_position;
16469 charpos = (STRING_MULTIBYTE (elt)
16470 ? string_byte_to_char (elt, bytepos)
16471 : bytepos);
16472
16473 spec
16474 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16475
16476 switch (mode_line_target)
16477 {
16478 case MODE_LINE_NOPROP:
16479 case MODE_LINE_TITLE:
16480 n += store_mode_line_noprop (spec, field, prec);
16481 break;
16482 case MODE_LINE_STRING:
16483 {
16484 int len = strlen (spec);
16485 Lisp_Object tem = make_string (spec, len);
16486 props = Ftext_properties_at (make_number (charpos), elt);
16487 /* Should only keep face property in props */
16488 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16489 }
16490 break;
16491 case MODE_LINE_DISPLAY:
16492 {
16493 int nglyphs_before, nwritten;
16494
16495 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16496 nwritten = display_string (spec, Qnil, elt,
16497 charpos, 0, it,
16498 field, prec, 0,
16499 multibyte);
16500
16501 /* Assign to the glyphs written above the
16502 string where the `%x' came from, position
16503 of the `%'. */
16504 if (nwritten > 0)
16505 {
16506 struct glyph *glyph
16507 = (it->glyph_row->glyphs[TEXT_AREA]
16508 + nglyphs_before);
16509 int i;
16510
16511 for (i = 0; i < nwritten; ++i)
16512 {
16513 glyph[i].object = elt;
16514 glyph[i].charpos = charpos;
16515 }
16516
16517 n += nwritten;
16518 }
16519 }
16520 break;
16521 }
16522 }
16523 else /* c == 0 */
16524 break;
16525 }
16526 }
16527 }
16528 break;
16529
16530 case Lisp_Symbol:
16531 /* A symbol: process the value of the symbol recursively
16532 as if it appeared here directly. Avoid error if symbol void.
16533 Special case: if value of symbol is a string, output the string
16534 literally. */
16535 {
16536 register Lisp_Object tem;
16537
16538 /* If the variable is not marked as risky to set
16539 then its contents are risky to use. */
16540 if (NILP (Fget (elt, Qrisky_local_variable)))
16541 risky = 1;
16542
16543 tem = Fboundp (elt);
16544 if (!NILP (tem))
16545 {
16546 tem = Fsymbol_value (elt);
16547 /* If value is a string, output that string literally:
16548 don't check for % within it. */
16549 if (STRINGP (tem))
16550 literal = 1;
16551
16552 if (!EQ (tem, elt))
16553 {
16554 /* Give up right away for nil or t. */
16555 elt = tem;
16556 goto tail_recurse;
16557 }
16558 }
16559 }
16560 break;
16561
16562 case Lisp_Cons:
16563 {
16564 register Lisp_Object car, tem;
16565
16566 /* A cons cell: five distinct cases.
16567 If first element is :eval or :propertize, do something special.
16568 If first element is a string or a cons, process all the elements
16569 and effectively concatenate them.
16570 If first element is a negative number, truncate displaying cdr to
16571 at most that many characters. If positive, pad (with spaces)
16572 to at least that many characters.
16573 If first element is a symbol, process the cadr or caddr recursively
16574 according to whether the symbol's value is non-nil or nil. */
16575 car = XCAR (elt);
16576 if (EQ (car, QCeval))
16577 {
16578 /* An element of the form (:eval FORM) means evaluate FORM
16579 and use the result as mode line elements. */
16580
16581 if (risky)
16582 break;
16583
16584 if (CONSP (XCDR (elt)))
16585 {
16586 Lisp_Object spec;
16587 spec = safe_eval (XCAR (XCDR (elt)));
16588 n += display_mode_element (it, depth, field_width - n,
16589 precision - n, spec, props,
16590 risky);
16591 }
16592 }
16593 else if (EQ (car, QCpropertize))
16594 {
16595 /* An element of the form (:propertize ELT PROPS...)
16596 means display ELT but applying properties PROPS. */
16597
16598 if (risky)
16599 break;
16600
16601 if (CONSP (XCDR (elt)))
16602 n += display_mode_element (it, depth, field_width - n,
16603 precision - n, XCAR (XCDR (elt)),
16604 XCDR (XCDR (elt)), risky);
16605 }
16606 else if (SYMBOLP (car))
16607 {
16608 tem = Fboundp (car);
16609 elt = XCDR (elt);
16610 if (!CONSP (elt))
16611 goto invalid;
16612 /* elt is now the cdr, and we know it is a cons cell.
16613 Use its car if CAR has a non-nil value. */
16614 if (!NILP (tem))
16615 {
16616 tem = Fsymbol_value (car);
16617 if (!NILP (tem))
16618 {
16619 elt = XCAR (elt);
16620 goto tail_recurse;
16621 }
16622 }
16623 /* Symbol's value is nil (or symbol is unbound)
16624 Get the cddr of the original list
16625 and if possible find the caddr and use that. */
16626 elt = XCDR (elt);
16627 if (NILP (elt))
16628 break;
16629 else if (!CONSP (elt))
16630 goto invalid;
16631 elt = XCAR (elt);
16632 goto tail_recurse;
16633 }
16634 else if (INTEGERP (car))
16635 {
16636 register int lim = XINT (car);
16637 elt = XCDR (elt);
16638 if (lim < 0)
16639 {
16640 /* Negative int means reduce maximum width. */
16641 if (precision <= 0)
16642 precision = -lim;
16643 else
16644 precision = min (precision, -lim);
16645 }
16646 else if (lim > 0)
16647 {
16648 /* Padding specified. Don't let it be more than
16649 current maximum. */
16650 if (precision > 0)
16651 lim = min (precision, lim);
16652
16653 /* If that's more padding than already wanted, queue it.
16654 But don't reduce padding already specified even if
16655 that is beyond the current truncation point. */
16656 field_width = max (lim, field_width);
16657 }
16658 goto tail_recurse;
16659 }
16660 else if (STRINGP (car) || CONSP (car))
16661 {
16662 register int limit = 50;
16663 /* Limit is to protect against circular lists. */
16664 while (CONSP (elt)
16665 && --limit > 0
16666 && (precision <= 0 || n < precision))
16667 {
16668 n += display_mode_element (it, depth,
16669 /* Do padding only after the last
16670 element in the list. */
16671 (! CONSP (XCDR (elt))
16672 ? field_width - n
16673 : 0),
16674 precision - n, XCAR (elt),
16675 props, risky);
16676 elt = XCDR (elt);
16677 }
16678 }
16679 }
16680 break;
16681
16682 default:
16683 invalid:
16684 elt = build_string ("*invalid*");
16685 goto tail_recurse;
16686 }
16687
16688 /* Pad to FIELD_WIDTH. */
16689 if (field_width > 0 && n < field_width)
16690 {
16691 switch (mode_line_target)
16692 {
16693 case MODE_LINE_NOPROP:
16694 case MODE_LINE_TITLE:
16695 n += store_mode_line_noprop ("", field_width - n, 0);
16696 break;
16697 case MODE_LINE_STRING:
16698 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16699 break;
16700 case MODE_LINE_DISPLAY:
16701 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16702 0, 0, 0);
16703 break;
16704 }
16705 }
16706
16707 return n;
16708 }
16709
16710 /* Store a mode-line string element in mode_line_string_list.
16711
16712 If STRING is non-null, display that C string. Otherwise, the Lisp
16713 string LISP_STRING is displayed.
16714
16715 FIELD_WIDTH is the minimum number of output glyphs to produce.
16716 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16717 with spaces. FIELD_WIDTH <= 0 means don't pad.
16718
16719 PRECISION is the maximum number of characters to output from
16720 STRING. PRECISION <= 0 means don't truncate the string.
16721
16722 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16723 properties to the string.
16724
16725 PROPS are the properties to add to the string.
16726 The mode_line_string_face face property is always added to the string.
16727 */
16728
16729 static int
16730 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16731 char *string;
16732 Lisp_Object lisp_string;
16733 int copy_string;
16734 int field_width;
16735 int precision;
16736 Lisp_Object props;
16737 {
16738 int len;
16739 int n = 0;
16740
16741 if (string != NULL)
16742 {
16743 len = strlen (string);
16744 if (precision > 0 && len > precision)
16745 len = precision;
16746 lisp_string = make_string (string, len);
16747 if (NILP (props))
16748 props = mode_line_string_face_prop;
16749 else if (!NILP (mode_line_string_face))
16750 {
16751 Lisp_Object face = Fplist_get (props, Qface);
16752 props = Fcopy_sequence (props);
16753 if (NILP (face))
16754 face = mode_line_string_face;
16755 else
16756 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16757 props = Fplist_put (props, Qface, face);
16758 }
16759 Fadd_text_properties (make_number (0), make_number (len),
16760 props, lisp_string);
16761 }
16762 else
16763 {
16764 len = XFASTINT (Flength (lisp_string));
16765 if (precision > 0 && len > precision)
16766 {
16767 len = precision;
16768 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16769 precision = -1;
16770 }
16771 if (!NILP (mode_line_string_face))
16772 {
16773 Lisp_Object face;
16774 if (NILP (props))
16775 props = Ftext_properties_at (make_number (0), lisp_string);
16776 face = Fplist_get (props, Qface);
16777 if (NILP (face))
16778 face = mode_line_string_face;
16779 else
16780 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16781 props = Fcons (Qface, Fcons (face, Qnil));
16782 if (copy_string)
16783 lisp_string = Fcopy_sequence (lisp_string);
16784 }
16785 if (!NILP (props))
16786 Fadd_text_properties (make_number (0), make_number (len),
16787 props, lisp_string);
16788 }
16789
16790 if (len > 0)
16791 {
16792 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16793 n += len;
16794 }
16795
16796 if (field_width > len)
16797 {
16798 field_width -= len;
16799 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16800 if (!NILP (props))
16801 Fadd_text_properties (make_number (0), make_number (field_width),
16802 props, lisp_string);
16803 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16804 n += field_width;
16805 }
16806
16807 return n;
16808 }
16809
16810
16811 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16812 1, 4, 0,
16813 doc: /* Format a string out of a mode line format specification.
16814 First arg FORMAT specifies the mode line format (see `mode-line-format'
16815 for details) to use.
16816
16817 Optional second arg FACE specifies the face property to put
16818 on all characters for which no face is specified.
16819 t means whatever face the window's mode line currently uses
16820 \(either `mode-line' or `mode-line-inactive', depending).
16821 nil means the default is no face property.
16822 If FACE is an integer, the value string has no text properties.
16823
16824 Optional third and fourth args WINDOW and BUFFER specify the window
16825 and buffer to use as the context for the formatting (defaults
16826 are the selected window and the window's buffer). */)
16827 (format, face, window, buffer)
16828 Lisp_Object format, face, window, buffer;
16829 {
16830 struct it it;
16831 int len;
16832 struct window *w;
16833 struct buffer *old_buffer = NULL;
16834 int face_id = -1;
16835 int no_props = INTEGERP (face);
16836 int count = SPECPDL_INDEX ();
16837 Lisp_Object str;
16838 int string_start = 0;
16839
16840 if (NILP (window))
16841 window = selected_window;
16842 CHECK_WINDOW (window);
16843 w = XWINDOW (window);
16844
16845 if (NILP (buffer))
16846 buffer = w->buffer;
16847 CHECK_BUFFER (buffer);
16848
16849 if (NILP (format))
16850 return build_string ("");
16851
16852 if (no_props)
16853 face = Qnil;
16854
16855 if (!NILP (face))
16856 {
16857 if (EQ (face, Qt))
16858 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16859 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16860 }
16861
16862 if (face_id < 0)
16863 face_id = DEFAULT_FACE_ID;
16864
16865 if (XBUFFER (buffer) != current_buffer)
16866 old_buffer = current_buffer;
16867
16868 /* Save things including mode_line_proptrans_alist,
16869 and set that to nil so that we don't alter the outer value. */
16870 record_unwind_protect (unwind_format_mode_line,
16871 format_mode_line_unwind_data (old_buffer, 1));
16872 mode_line_proptrans_alist = Qnil;
16873
16874 if (old_buffer)
16875 set_buffer_internal_1 (XBUFFER (buffer));
16876
16877 init_iterator (&it, w, -1, -1, NULL, face_id);
16878
16879 if (no_props)
16880 {
16881 mode_line_target = MODE_LINE_NOPROP;
16882 mode_line_string_face_prop = Qnil;
16883 mode_line_string_list = Qnil;
16884 string_start = MODE_LINE_NOPROP_LEN (0);
16885 }
16886 else
16887 {
16888 mode_line_target = MODE_LINE_STRING;
16889 mode_line_string_list = Qnil;
16890 mode_line_string_face = face;
16891 mode_line_string_face_prop
16892 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16893 }
16894
16895 push_frame_kboard (it.f);
16896 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16897 pop_frame_kboard ();
16898
16899 if (no_props)
16900 {
16901 len = MODE_LINE_NOPROP_LEN (string_start);
16902 str = make_string (mode_line_noprop_buf + string_start, len);
16903 }
16904 else
16905 {
16906 mode_line_string_list = Fnreverse (mode_line_string_list);
16907 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16908 make_string ("", 0));
16909 }
16910
16911 unbind_to (count, Qnil);
16912 return str;
16913 }
16914
16915 /* Write a null-terminated, right justified decimal representation of
16916 the positive integer D to BUF using a minimal field width WIDTH. */
16917
16918 static void
16919 pint2str (buf, width, d)
16920 register char *buf;
16921 register int width;
16922 register int d;
16923 {
16924 register char *p = buf;
16925
16926 if (d <= 0)
16927 *p++ = '0';
16928 else
16929 {
16930 while (d > 0)
16931 {
16932 *p++ = d % 10 + '0';
16933 d /= 10;
16934 }
16935 }
16936
16937 for (width -= (int) (p - buf); width > 0; --width)
16938 *p++ = ' ';
16939 *p-- = '\0';
16940 while (p > buf)
16941 {
16942 d = *buf;
16943 *buf++ = *p;
16944 *p-- = d;
16945 }
16946 }
16947
16948 /* Write a null-terminated, right justified decimal and "human
16949 readable" representation of the nonnegative integer D to BUF using
16950 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16951
16952 static const char power_letter[] =
16953 {
16954 0, /* not used */
16955 'k', /* kilo */
16956 'M', /* mega */
16957 'G', /* giga */
16958 'T', /* tera */
16959 'P', /* peta */
16960 'E', /* exa */
16961 'Z', /* zetta */
16962 'Y' /* yotta */
16963 };
16964
16965 static void
16966 pint2hrstr (buf, width, d)
16967 char *buf;
16968 int width;
16969 int d;
16970 {
16971 /* We aim to represent the nonnegative integer D as
16972 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16973 int quotient = d;
16974 int remainder = 0;
16975 /* -1 means: do not use TENTHS. */
16976 int tenths = -1;
16977 int exponent = 0;
16978
16979 /* Length of QUOTIENT.TENTHS as a string. */
16980 int length;
16981
16982 char * psuffix;
16983 char * p;
16984
16985 if (1000 <= quotient)
16986 {
16987 /* Scale to the appropriate EXPONENT. */
16988 do
16989 {
16990 remainder = quotient % 1000;
16991 quotient /= 1000;
16992 exponent++;
16993 }
16994 while (1000 <= quotient);
16995
16996 /* Round to nearest and decide whether to use TENTHS or not. */
16997 if (quotient <= 9)
16998 {
16999 tenths = remainder / 100;
17000 if (50 <= remainder % 100)
17001 {
17002 if (tenths < 9)
17003 tenths++;
17004 else
17005 {
17006 quotient++;
17007 if (quotient == 10)
17008 tenths = -1;
17009 else
17010 tenths = 0;
17011 }
17012 }
17013 }
17014 else
17015 if (500 <= remainder)
17016 {
17017 if (quotient < 999)
17018 quotient++;
17019 else
17020 {
17021 quotient = 1;
17022 exponent++;
17023 tenths = 0;
17024 }
17025 }
17026 }
17027
17028 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17029 if (tenths == -1 && quotient <= 99)
17030 if (quotient <= 9)
17031 length = 1;
17032 else
17033 length = 2;
17034 else
17035 length = 3;
17036 p = psuffix = buf + max (width, length);
17037
17038 /* Print EXPONENT. */
17039 if (exponent)
17040 *psuffix++ = power_letter[exponent];
17041 *psuffix = '\0';
17042
17043 /* Print TENTHS. */
17044 if (tenths >= 0)
17045 {
17046 *--p = '0' + tenths;
17047 *--p = '.';
17048 }
17049
17050 /* Print QUOTIENT. */
17051 do
17052 {
17053 int digit = quotient % 10;
17054 *--p = '0' + digit;
17055 }
17056 while ((quotient /= 10) != 0);
17057
17058 /* Print leading spaces. */
17059 while (buf < p)
17060 *--p = ' ';
17061 }
17062
17063 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17064 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17065 type of CODING_SYSTEM. Return updated pointer into BUF. */
17066
17067 static unsigned char invalid_eol_type[] = "(*invalid*)";
17068
17069 static char *
17070 decode_mode_spec_coding (coding_system, buf, eol_flag)
17071 Lisp_Object coding_system;
17072 register char *buf;
17073 int eol_flag;
17074 {
17075 Lisp_Object val;
17076 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17077 const unsigned char *eol_str;
17078 int eol_str_len;
17079 /* The EOL conversion we are using. */
17080 Lisp_Object eoltype;
17081
17082 val = Fget (coding_system, Qcoding_system);
17083 eoltype = Qnil;
17084
17085 if (!VECTORP (val)) /* Not yet decided. */
17086 {
17087 if (multibyte)
17088 *buf++ = '-';
17089 if (eol_flag)
17090 eoltype = eol_mnemonic_undecided;
17091 /* Don't mention EOL conversion if it isn't decided. */
17092 }
17093 else
17094 {
17095 Lisp_Object eolvalue;
17096
17097 eolvalue = Fget (coding_system, Qeol_type);
17098
17099 if (multibyte)
17100 *buf++ = XFASTINT (AREF (val, 1));
17101
17102 if (eol_flag)
17103 {
17104 /* The EOL conversion that is normal on this system. */
17105
17106 if (NILP (eolvalue)) /* Not yet decided. */
17107 eoltype = eol_mnemonic_undecided;
17108 else if (VECTORP (eolvalue)) /* Not yet decided. */
17109 eoltype = eol_mnemonic_undecided;
17110 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17111 eoltype = (XFASTINT (eolvalue) == 0
17112 ? eol_mnemonic_unix
17113 : (XFASTINT (eolvalue) == 1
17114 ? eol_mnemonic_dos : eol_mnemonic_mac));
17115 }
17116 }
17117
17118 if (eol_flag)
17119 {
17120 /* Mention the EOL conversion if it is not the usual one. */
17121 if (STRINGP (eoltype))
17122 {
17123 eol_str = SDATA (eoltype);
17124 eol_str_len = SBYTES (eoltype);
17125 }
17126 else if (INTEGERP (eoltype)
17127 && CHAR_VALID_P (XINT (eoltype), 0))
17128 {
17129 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17130 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17131 eol_str = tmp;
17132 }
17133 else
17134 {
17135 eol_str = invalid_eol_type;
17136 eol_str_len = sizeof (invalid_eol_type) - 1;
17137 }
17138 bcopy (eol_str, buf, eol_str_len);
17139 buf += eol_str_len;
17140 }
17141
17142 return buf;
17143 }
17144
17145 /* Return a string for the output of a mode line %-spec for window W,
17146 generated by character C. PRECISION >= 0 means don't return a
17147 string longer than that value. FIELD_WIDTH > 0 means pad the
17148 string returned with spaces to that value. Return 1 in *MULTIBYTE
17149 if the result is multibyte text.
17150
17151 Note we operate on the current buffer for most purposes,
17152 the exception being w->base_line_pos. */
17153
17154 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17155
17156 static char *
17157 decode_mode_spec (w, c, field_width, precision, multibyte)
17158 struct window *w;
17159 register int c;
17160 int field_width, precision;
17161 int *multibyte;
17162 {
17163 Lisp_Object obj;
17164 struct frame *f = XFRAME (WINDOW_FRAME (w));
17165 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17166 struct buffer *b = current_buffer;
17167
17168 obj = Qnil;
17169 *multibyte = 0;
17170
17171 switch (c)
17172 {
17173 case '*':
17174 if (!NILP (b->read_only))
17175 return "%";
17176 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17177 return "*";
17178 return "-";
17179
17180 case '+':
17181 /* This differs from %* only for a modified read-only buffer. */
17182 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17183 return "*";
17184 if (!NILP (b->read_only))
17185 return "%";
17186 return "-";
17187
17188 case '&':
17189 /* This differs from %* in ignoring read-only-ness. */
17190 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17191 return "*";
17192 return "-";
17193
17194 case '%':
17195 return "%";
17196
17197 case '[':
17198 {
17199 int i;
17200 char *p;
17201
17202 if (command_loop_level > 5)
17203 return "[[[... ";
17204 p = decode_mode_spec_buf;
17205 for (i = 0; i < command_loop_level; i++)
17206 *p++ = '[';
17207 *p = 0;
17208 return decode_mode_spec_buf;
17209 }
17210
17211 case ']':
17212 {
17213 int i;
17214 char *p;
17215
17216 if (command_loop_level > 5)
17217 return " ...]]]";
17218 p = decode_mode_spec_buf;
17219 for (i = 0; i < command_loop_level; i++)
17220 *p++ = ']';
17221 *p = 0;
17222 return decode_mode_spec_buf;
17223 }
17224
17225 case '-':
17226 {
17227 register int i;
17228
17229 /* Let lots_of_dashes be a string of infinite length. */
17230 if (mode_line_target == MODE_LINE_NOPROP ||
17231 mode_line_target == MODE_LINE_STRING)
17232 return "--";
17233 if (field_width <= 0
17234 || field_width > sizeof (lots_of_dashes))
17235 {
17236 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17237 decode_mode_spec_buf[i] = '-';
17238 decode_mode_spec_buf[i] = '\0';
17239 return decode_mode_spec_buf;
17240 }
17241 else
17242 return lots_of_dashes;
17243 }
17244
17245 case 'b':
17246 obj = b->name;
17247 break;
17248
17249 case 'c':
17250 {
17251 int col = (int) current_column (); /* iftc */
17252 w->column_number_displayed = make_number (col);
17253 pint2str (decode_mode_spec_buf, field_width, col);
17254 return decode_mode_spec_buf;
17255 }
17256
17257 case 'e':
17258 #ifndef SYSTEM_MALLOC
17259 {
17260 if (NILP (Vmemory_full))
17261 return "";
17262 else
17263 return "!MEM FULL! ";
17264 }
17265 #else
17266 return "";
17267 #endif
17268
17269 case 'F':
17270 /* %F displays the frame name. */
17271 if (!NILP (f->title))
17272 return (char *) SDATA (f->title);
17273 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17274 return (char *) SDATA (f->name);
17275 return "Emacs";
17276
17277 case 'f':
17278 obj = b->filename;
17279 break;
17280
17281 case 'i':
17282 {
17283 int size = ZV - BEGV;
17284 pint2str (decode_mode_spec_buf, field_width, size);
17285 return decode_mode_spec_buf;
17286 }
17287
17288 case 'I':
17289 {
17290 int size = ZV - BEGV;
17291 pint2hrstr (decode_mode_spec_buf, field_width, size);
17292 return decode_mode_spec_buf;
17293 }
17294
17295 case 'l':
17296 {
17297 int startpos = XMARKER (w->start)->charpos;
17298 int startpos_byte = marker_byte_position (w->start);
17299 int line, linepos, linepos_byte, topline;
17300 int nlines, junk;
17301 int height = WINDOW_TOTAL_LINES (w);
17302
17303 /* If we decided that this buffer isn't suitable for line numbers,
17304 don't forget that too fast. */
17305 if (EQ (w->base_line_pos, w->buffer))
17306 goto no_value;
17307 /* But do forget it, if the window shows a different buffer now. */
17308 else if (BUFFERP (w->base_line_pos))
17309 w->base_line_pos = Qnil;
17310
17311 /* If the buffer is very big, don't waste time. */
17312 if (INTEGERP (Vline_number_display_limit)
17313 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17314 {
17315 w->base_line_pos = Qnil;
17316 w->base_line_number = Qnil;
17317 goto no_value;
17318 }
17319
17320 if (!NILP (w->base_line_number)
17321 && !NILP (w->base_line_pos)
17322 && XFASTINT (w->base_line_pos) <= startpos)
17323 {
17324 line = XFASTINT (w->base_line_number);
17325 linepos = XFASTINT (w->base_line_pos);
17326 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17327 }
17328 else
17329 {
17330 line = 1;
17331 linepos = BUF_BEGV (b);
17332 linepos_byte = BUF_BEGV_BYTE (b);
17333 }
17334
17335 /* Count lines from base line to window start position. */
17336 nlines = display_count_lines (linepos, linepos_byte,
17337 startpos_byte,
17338 startpos, &junk);
17339
17340 topline = nlines + line;
17341
17342 /* Determine a new base line, if the old one is too close
17343 or too far away, or if we did not have one.
17344 "Too close" means it's plausible a scroll-down would
17345 go back past it. */
17346 if (startpos == BUF_BEGV (b))
17347 {
17348 w->base_line_number = make_number (topline);
17349 w->base_line_pos = make_number (BUF_BEGV (b));
17350 }
17351 else if (nlines < height + 25 || nlines > height * 3 + 50
17352 || linepos == BUF_BEGV (b))
17353 {
17354 int limit = BUF_BEGV (b);
17355 int limit_byte = BUF_BEGV_BYTE (b);
17356 int position;
17357 int distance = (height * 2 + 30) * line_number_display_limit_width;
17358
17359 if (startpos - distance > limit)
17360 {
17361 limit = startpos - distance;
17362 limit_byte = CHAR_TO_BYTE (limit);
17363 }
17364
17365 nlines = display_count_lines (startpos, startpos_byte,
17366 limit_byte,
17367 - (height * 2 + 30),
17368 &position);
17369 /* If we couldn't find the lines we wanted within
17370 line_number_display_limit_width chars per line,
17371 give up on line numbers for this window. */
17372 if (position == limit_byte && limit == startpos - distance)
17373 {
17374 w->base_line_pos = w->buffer;
17375 w->base_line_number = Qnil;
17376 goto no_value;
17377 }
17378
17379 w->base_line_number = make_number (topline - nlines);
17380 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17381 }
17382
17383 /* Now count lines from the start pos to point. */
17384 nlines = display_count_lines (startpos, startpos_byte,
17385 PT_BYTE, PT, &junk);
17386
17387 /* Record that we did display the line number. */
17388 line_number_displayed = 1;
17389
17390 /* Make the string to show. */
17391 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17392 return decode_mode_spec_buf;
17393 no_value:
17394 {
17395 char* p = decode_mode_spec_buf;
17396 int pad = field_width - 2;
17397 while (pad-- > 0)
17398 *p++ = ' ';
17399 *p++ = '?';
17400 *p++ = '?';
17401 *p = '\0';
17402 return decode_mode_spec_buf;
17403 }
17404 }
17405 break;
17406
17407 case 'm':
17408 obj = b->mode_name;
17409 break;
17410
17411 case 'n':
17412 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17413 return " Narrow";
17414 break;
17415
17416 case 'p':
17417 {
17418 int pos = marker_position (w->start);
17419 int total = BUF_ZV (b) - BUF_BEGV (b);
17420
17421 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17422 {
17423 if (pos <= BUF_BEGV (b))
17424 return "All";
17425 else
17426 return "Bottom";
17427 }
17428 else if (pos <= BUF_BEGV (b))
17429 return "Top";
17430 else
17431 {
17432 if (total > 1000000)
17433 /* Do it differently for a large value, to avoid overflow. */
17434 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17435 else
17436 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17437 /* We can't normally display a 3-digit number,
17438 so get us a 2-digit number that is close. */
17439 if (total == 100)
17440 total = 99;
17441 sprintf (decode_mode_spec_buf, "%2d%%", total);
17442 return decode_mode_spec_buf;
17443 }
17444 }
17445
17446 /* Display percentage of size above the bottom of the screen. */
17447 case 'P':
17448 {
17449 int toppos = marker_position (w->start);
17450 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17451 int total = BUF_ZV (b) - BUF_BEGV (b);
17452
17453 if (botpos >= BUF_ZV (b))
17454 {
17455 if (toppos <= BUF_BEGV (b))
17456 return "All";
17457 else
17458 return "Bottom";
17459 }
17460 else
17461 {
17462 if (total > 1000000)
17463 /* Do it differently for a large value, to avoid overflow. */
17464 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17465 else
17466 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17467 /* We can't normally display a 3-digit number,
17468 so get us a 2-digit number that is close. */
17469 if (total == 100)
17470 total = 99;
17471 if (toppos <= BUF_BEGV (b))
17472 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17473 else
17474 sprintf (decode_mode_spec_buf, "%2d%%", total);
17475 return decode_mode_spec_buf;
17476 }
17477 }
17478
17479 case 's':
17480 /* status of process */
17481 obj = Fget_buffer_process (Fcurrent_buffer ());
17482 if (NILP (obj))
17483 return "no process";
17484 #ifdef subprocesses
17485 obj = Fsymbol_name (Fprocess_status (obj));
17486 #endif
17487 break;
17488
17489 case 't': /* indicate TEXT or BINARY */
17490 #ifdef MODE_LINE_BINARY_TEXT
17491 return MODE_LINE_BINARY_TEXT (b);
17492 #else
17493 return "T";
17494 #endif
17495
17496 case 'z':
17497 /* coding-system (not including end-of-line format) */
17498 case 'Z':
17499 /* coding-system (including end-of-line type) */
17500 {
17501 int eol_flag = (c == 'Z');
17502 char *p = decode_mode_spec_buf;
17503
17504 if (! FRAME_WINDOW_P (f))
17505 {
17506 /* No need to mention EOL here--the terminal never needs
17507 to do EOL conversion. */
17508 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17509 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17510 }
17511 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17512 p, eol_flag);
17513
17514 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17515 #ifdef subprocesses
17516 obj = Fget_buffer_process (Fcurrent_buffer ());
17517 if (PROCESSP (obj))
17518 {
17519 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17520 p, eol_flag);
17521 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17522 p, eol_flag);
17523 }
17524 #endif /* subprocesses */
17525 #endif /* 0 */
17526 *p = 0;
17527 return decode_mode_spec_buf;
17528 }
17529 }
17530
17531 if (STRINGP (obj))
17532 {
17533 *multibyte = STRING_MULTIBYTE (obj);
17534 return (char *) SDATA (obj);
17535 }
17536 else
17537 return "";
17538 }
17539
17540
17541 /* Count up to COUNT lines starting from START / START_BYTE.
17542 But don't go beyond LIMIT_BYTE.
17543 Return the number of lines thus found (always nonnegative).
17544
17545 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17546
17547 static int
17548 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17549 int start, start_byte, limit_byte, count;
17550 int *byte_pos_ptr;
17551 {
17552 register unsigned char *cursor;
17553 unsigned char *base;
17554
17555 register int ceiling;
17556 register unsigned char *ceiling_addr;
17557 int orig_count = count;
17558
17559 /* If we are not in selective display mode,
17560 check only for newlines. */
17561 int selective_display = (!NILP (current_buffer->selective_display)
17562 && !INTEGERP (current_buffer->selective_display));
17563
17564 if (count > 0)
17565 {
17566 while (start_byte < limit_byte)
17567 {
17568 ceiling = BUFFER_CEILING_OF (start_byte);
17569 ceiling = min (limit_byte - 1, ceiling);
17570 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17571 base = (cursor = BYTE_POS_ADDR (start_byte));
17572 while (1)
17573 {
17574 if (selective_display)
17575 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17576 ;
17577 else
17578 while (*cursor != '\n' && ++cursor != ceiling_addr)
17579 ;
17580
17581 if (cursor != ceiling_addr)
17582 {
17583 if (--count == 0)
17584 {
17585 start_byte += cursor - base + 1;
17586 *byte_pos_ptr = start_byte;
17587 return orig_count;
17588 }
17589 else
17590 if (++cursor == ceiling_addr)
17591 break;
17592 }
17593 else
17594 break;
17595 }
17596 start_byte += cursor - base;
17597 }
17598 }
17599 else
17600 {
17601 while (start_byte > limit_byte)
17602 {
17603 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17604 ceiling = max (limit_byte, ceiling);
17605 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17606 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17607 while (1)
17608 {
17609 if (selective_display)
17610 while (--cursor != ceiling_addr
17611 && *cursor != '\n' && *cursor != 015)
17612 ;
17613 else
17614 while (--cursor != ceiling_addr && *cursor != '\n')
17615 ;
17616
17617 if (cursor != ceiling_addr)
17618 {
17619 if (++count == 0)
17620 {
17621 start_byte += cursor - base + 1;
17622 *byte_pos_ptr = start_byte;
17623 /* When scanning backwards, we should
17624 not count the newline posterior to which we stop. */
17625 return - orig_count - 1;
17626 }
17627 }
17628 else
17629 break;
17630 }
17631 /* Here we add 1 to compensate for the last decrement
17632 of CURSOR, which took it past the valid range. */
17633 start_byte += cursor - base + 1;
17634 }
17635 }
17636
17637 *byte_pos_ptr = limit_byte;
17638
17639 if (count < 0)
17640 return - orig_count + count;
17641 return orig_count - count;
17642
17643 }
17644
17645
17646 \f
17647 /***********************************************************************
17648 Displaying strings
17649 ***********************************************************************/
17650
17651 /* Display a NUL-terminated string, starting with index START.
17652
17653 If STRING is non-null, display that C string. Otherwise, the Lisp
17654 string LISP_STRING is displayed.
17655
17656 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17657 FACE_STRING. Display STRING or LISP_STRING with the face at
17658 FACE_STRING_POS in FACE_STRING:
17659
17660 Display the string in the environment given by IT, but use the
17661 standard display table, temporarily.
17662
17663 FIELD_WIDTH is the minimum number of output glyphs to produce.
17664 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17665 with spaces. If STRING has more characters, more than FIELD_WIDTH
17666 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17667
17668 PRECISION is the maximum number of characters to output from
17669 STRING. PRECISION < 0 means don't truncate the string.
17670
17671 This is roughly equivalent to printf format specifiers:
17672
17673 FIELD_WIDTH PRECISION PRINTF
17674 ----------------------------------------
17675 -1 -1 %s
17676 -1 10 %.10s
17677 10 -1 %10s
17678 20 10 %20.10s
17679
17680 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17681 display them, and < 0 means obey the current buffer's value of
17682 enable_multibyte_characters.
17683
17684 Value is the number of glyphs produced. */
17685
17686 static int
17687 display_string (string, lisp_string, face_string, face_string_pos,
17688 start, it, field_width, precision, max_x, multibyte)
17689 unsigned char *string;
17690 Lisp_Object lisp_string;
17691 Lisp_Object face_string;
17692 int face_string_pos;
17693 int start;
17694 struct it *it;
17695 int field_width, precision, max_x;
17696 int multibyte;
17697 {
17698 int hpos_at_start = it->hpos;
17699 int saved_face_id = it->face_id;
17700 struct glyph_row *row = it->glyph_row;
17701
17702 /* Initialize the iterator IT for iteration over STRING beginning
17703 with index START. */
17704 reseat_to_string (it, string, lisp_string, start,
17705 precision, field_width, multibyte);
17706
17707 /* If displaying STRING, set up the face of the iterator
17708 from LISP_STRING, if that's given. */
17709 if (STRINGP (face_string))
17710 {
17711 int endptr;
17712 struct face *face;
17713
17714 it->face_id
17715 = face_at_string_position (it->w, face_string, face_string_pos,
17716 0, it->region_beg_charpos,
17717 it->region_end_charpos,
17718 &endptr, it->base_face_id, 0);
17719 face = FACE_FROM_ID (it->f, it->face_id);
17720 it->face_box_p = face->box != FACE_NO_BOX;
17721 }
17722
17723 /* Set max_x to the maximum allowed X position. Don't let it go
17724 beyond the right edge of the window. */
17725 if (max_x <= 0)
17726 max_x = it->last_visible_x;
17727 else
17728 max_x = min (max_x, it->last_visible_x);
17729
17730 /* Skip over display elements that are not visible. because IT->w is
17731 hscrolled. */
17732 if (it->current_x < it->first_visible_x)
17733 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17734 MOVE_TO_POS | MOVE_TO_X);
17735
17736 row->ascent = it->max_ascent;
17737 row->height = it->max_ascent + it->max_descent;
17738 row->phys_ascent = it->max_phys_ascent;
17739 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17740 row->extra_line_spacing = it->max_extra_line_spacing;
17741
17742 /* This condition is for the case that we are called with current_x
17743 past last_visible_x. */
17744 while (it->current_x < max_x)
17745 {
17746 int x_before, x, n_glyphs_before, i, nglyphs;
17747
17748 /* Get the next display element. */
17749 if (!get_next_display_element (it))
17750 break;
17751
17752 /* Produce glyphs. */
17753 x_before = it->current_x;
17754 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17755 PRODUCE_GLYPHS (it);
17756
17757 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17758 i = 0;
17759 x = x_before;
17760 while (i < nglyphs)
17761 {
17762 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17763
17764 if (!it->truncate_lines_p
17765 && x + glyph->pixel_width > max_x)
17766 {
17767 /* End of continued line or max_x reached. */
17768 if (CHAR_GLYPH_PADDING_P (*glyph))
17769 {
17770 /* A wide character is unbreakable. */
17771 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17772 it->current_x = x_before;
17773 }
17774 else
17775 {
17776 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17777 it->current_x = x;
17778 }
17779 break;
17780 }
17781 else if (x + glyph->pixel_width > it->first_visible_x)
17782 {
17783 /* Glyph is at least partially visible. */
17784 ++it->hpos;
17785 if (x < it->first_visible_x)
17786 it->glyph_row->x = x - it->first_visible_x;
17787 }
17788 else
17789 {
17790 /* Glyph is off the left margin of the display area.
17791 Should not happen. */
17792 abort ();
17793 }
17794
17795 row->ascent = max (row->ascent, it->max_ascent);
17796 row->height = max (row->height, it->max_ascent + it->max_descent);
17797 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17798 row->phys_height = max (row->phys_height,
17799 it->max_phys_ascent + it->max_phys_descent);
17800 row->extra_line_spacing = max (row->extra_line_spacing,
17801 it->max_extra_line_spacing);
17802 x += glyph->pixel_width;
17803 ++i;
17804 }
17805
17806 /* Stop if max_x reached. */
17807 if (i < nglyphs)
17808 break;
17809
17810 /* Stop at line ends. */
17811 if (ITERATOR_AT_END_OF_LINE_P (it))
17812 {
17813 it->continuation_lines_width = 0;
17814 break;
17815 }
17816
17817 set_iterator_to_next (it, 1);
17818
17819 /* Stop if truncating at the right edge. */
17820 if (it->truncate_lines_p
17821 && it->current_x >= it->last_visible_x)
17822 {
17823 /* Add truncation mark, but don't do it if the line is
17824 truncated at a padding space. */
17825 if (IT_CHARPOS (*it) < it->string_nchars)
17826 {
17827 if (!FRAME_WINDOW_P (it->f))
17828 {
17829 int i, n;
17830
17831 if (it->current_x > it->last_visible_x)
17832 {
17833 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17834 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17835 break;
17836 for (n = row->used[TEXT_AREA]; i < n; ++i)
17837 {
17838 row->used[TEXT_AREA] = i;
17839 produce_special_glyphs (it, IT_TRUNCATION);
17840 }
17841 }
17842 produce_special_glyphs (it, IT_TRUNCATION);
17843 }
17844 it->glyph_row->truncated_on_right_p = 1;
17845 }
17846 break;
17847 }
17848 }
17849
17850 /* Maybe insert a truncation at the left. */
17851 if (it->first_visible_x
17852 && IT_CHARPOS (*it) > 0)
17853 {
17854 if (!FRAME_WINDOW_P (it->f))
17855 insert_left_trunc_glyphs (it);
17856 it->glyph_row->truncated_on_left_p = 1;
17857 }
17858
17859 it->face_id = saved_face_id;
17860
17861 /* Value is number of columns displayed. */
17862 return it->hpos - hpos_at_start;
17863 }
17864
17865
17866 \f
17867 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17868 appears as an element of LIST or as the car of an element of LIST.
17869 If PROPVAL is a list, compare each element against LIST in that
17870 way, and return 1/2 if any element of PROPVAL is found in LIST.
17871 Otherwise return 0. This function cannot quit.
17872 The return value is 2 if the text is invisible but with an ellipsis
17873 and 1 if it's invisible and without an ellipsis. */
17874
17875 int
17876 invisible_p (propval, list)
17877 register Lisp_Object propval;
17878 Lisp_Object list;
17879 {
17880 register Lisp_Object tail, proptail;
17881
17882 for (tail = list; CONSP (tail); tail = XCDR (tail))
17883 {
17884 register Lisp_Object tem;
17885 tem = XCAR (tail);
17886 if (EQ (propval, tem))
17887 return 1;
17888 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17889 return NILP (XCDR (tem)) ? 1 : 2;
17890 }
17891
17892 if (CONSP (propval))
17893 {
17894 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17895 {
17896 Lisp_Object propelt;
17897 propelt = XCAR (proptail);
17898 for (tail = list; CONSP (tail); tail = XCDR (tail))
17899 {
17900 register Lisp_Object tem;
17901 tem = XCAR (tail);
17902 if (EQ (propelt, tem))
17903 return 1;
17904 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17905 return NILP (XCDR (tem)) ? 1 : 2;
17906 }
17907 }
17908 }
17909
17910 return 0;
17911 }
17912
17913 /* Calculate a width or height in pixels from a specification using
17914 the following elements:
17915
17916 SPEC ::=
17917 NUM - a (fractional) multiple of the default font width/height
17918 (NUM) - specifies exactly NUM pixels
17919 UNIT - a fixed number of pixels, see below.
17920 ELEMENT - size of a display element in pixels, see below.
17921 (NUM . SPEC) - equals NUM * SPEC
17922 (+ SPEC SPEC ...) - add pixel values
17923 (- SPEC SPEC ...) - subtract pixel values
17924 (- SPEC) - negate pixel value
17925
17926 NUM ::=
17927 INT or FLOAT - a number constant
17928 SYMBOL - use symbol's (buffer local) variable binding.
17929
17930 UNIT ::=
17931 in - pixels per inch *)
17932 mm - pixels per 1/1000 meter *)
17933 cm - pixels per 1/100 meter *)
17934 width - width of current font in pixels.
17935 height - height of current font in pixels.
17936
17937 *) using the ratio(s) defined in display-pixels-per-inch.
17938
17939 ELEMENT ::=
17940
17941 left-fringe - left fringe width in pixels
17942 right-fringe - right fringe width in pixels
17943
17944 left-margin - left margin width in pixels
17945 right-margin - right margin width in pixels
17946
17947 scroll-bar - scroll-bar area width in pixels
17948
17949 Examples:
17950
17951 Pixels corresponding to 5 inches:
17952 (5 . in)
17953
17954 Total width of non-text areas on left side of window (if scroll-bar is on left):
17955 '(space :width (+ left-fringe left-margin scroll-bar))
17956
17957 Align to first text column (in header line):
17958 '(space :align-to 0)
17959
17960 Align to middle of text area minus half the width of variable `my-image'
17961 containing a loaded image:
17962 '(space :align-to (0.5 . (- text my-image)))
17963
17964 Width of left margin minus width of 1 character in the default font:
17965 '(space :width (- left-margin 1))
17966
17967 Width of left margin minus width of 2 characters in the current font:
17968 '(space :width (- left-margin (2 . width)))
17969
17970 Center 1 character over left-margin (in header line):
17971 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17972
17973 Different ways to express width of left fringe plus left margin minus one pixel:
17974 '(space :width (- (+ left-fringe left-margin) (1)))
17975 '(space :width (+ left-fringe left-margin (- (1))))
17976 '(space :width (+ left-fringe left-margin (-1)))
17977
17978 */
17979
17980 #define NUMVAL(X) \
17981 ((INTEGERP (X) || FLOATP (X)) \
17982 ? XFLOATINT (X) \
17983 : - 1)
17984
17985 int
17986 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17987 double *res;
17988 struct it *it;
17989 Lisp_Object prop;
17990 void *font;
17991 int width_p, *align_to;
17992 {
17993 double pixels;
17994
17995 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17996 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17997
17998 if (NILP (prop))
17999 return OK_PIXELS (0);
18000
18001 if (SYMBOLP (prop))
18002 {
18003 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18004 {
18005 char *unit = SDATA (SYMBOL_NAME (prop));
18006
18007 if (unit[0] == 'i' && unit[1] == 'n')
18008 pixels = 1.0;
18009 else if (unit[0] == 'm' && unit[1] == 'm')
18010 pixels = 25.4;
18011 else if (unit[0] == 'c' && unit[1] == 'm')
18012 pixels = 2.54;
18013 else
18014 pixels = 0;
18015 if (pixels > 0)
18016 {
18017 double ppi;
18018 #ifdef HAVE_WINDOW_SYSTEM
18019 if (FRAME_WINDOW_P (it->f)
18020 && (ppi = (width_p
18021 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18022 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18023 ppi > 0))
18024 return OK_PIXELS (ppi / pixels);
18025 #endif
18026
18027 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18028 || (CONSP (Vdisplay_pixels_per_inch)
18029 && (ppi = (width_p
18030 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18031 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18032 ppi > 0)))
18033 return OK_PIXELS (ppi / pixels);
18034
18035 return 0;
18036 }
18037 }
18038
18039 #ifdef HAVE_WINDOW_SYSTEM
18040 if (EQ (prop, Qheight))
18041 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18042 if (EQ (prop, Qwidth))
18043 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18044 #else
18045 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18046 return OK_PIXELS (1);
18047 #endif
18048
18049 if (EQ (prop, Qtext))
18050 return OK_PIXELS (width_p
18051 ? window_box_width (it->w, TEXT_AREA)
18052 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18053
18054 if (align_to && *align_to < 0)
18055 {
18056 *res = 0;
18057 if (EQ (prop, Qleft))
18058 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18059 if (EQ (prop, Qright))
18060 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18061 if (EQ (prop, Qcenter))
18062 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18063 + window_box_width (it->w, TEXT_AREA) / 2);
18064 if (EQ (prop, Qleft_fringe))
18065 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18066 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18067 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18068 if (EQ (prop, Qright_fringe))
18069 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18070 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18071 : window_box_right_offset (it->w, TEXT_AREA));
18072 if (EQ (prop, Qleft_margin))
18073 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18074 if (EQ (prop, Qright_margin))
18075 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18076 if (EQ (prop, Qscroll_bar))
18077 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18078 ? 0
18079 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18080 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18081 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18082 : 0)));
18083 }
18084 else
18085 {
18086 if (EQ (prop, Qleft_fringe))
18087 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18088 if (EQ (prop, Qright_fringe))
18089 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18090 if (EQ (prop, Qleft_margin))
18091 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18092 if (EQ (prop, Qright_margin))
18093 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18094 if (EQ (prop, Qscroll_bar))
18095 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18096 }
18097
18098 prop = Fbuffer_local_value (prop, it->w->buffer);
18099 }
18100
18101 if (INTEGERP (prop) || FLOATP (prop))
18102 {
18103 int base_unit = (width_p
18104 ? FRAME_COLUMN_WIDTH (it->f)
18105 : FRAME_LINE_HEIGHT (it->f));
18106 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18107 }
18108
18109 if (CONSP (prop))
18110 {
18111 Lisp_Object car = XCAR (prop);
18112 Lisp_Object cdr = XCDR (prop);
18113
18114 if (SYMBOLP (car))
18115 {
18116 #ifdef HAVE_WINDOW_SYSTEM
18117 if (valid_image_p (prop))
18118 {
18119 int id = lookup_image (it->f, prop);
18120 struct image *img = IMAGE_FROM_ID (it->f, id);
18121
18122 return OK_PIXELS (width_p ? img->width : img->height);
18123 }
18124 #endif
18125 if (EQ (car, Qplus) || EQ (car, Qminus))
18126 {
18127 int first = 1;
18128 double px;
18129
18130 pixels = 0;
18131 while (CONSP (cdr))
18132 {
18133 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18134 font, width_p, align_to))
18135 return 0;
18136 if (first)
18137 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18138 else
18139 pixels += px;
18140 cdr = XCDR (cdr);
18141 }
18142 if (EQ (car, Qminus))
18143 pixels = -pixels;
18144 return OK_PIXELS (pixels);
18145 }
18146
18147 car = Fbuffer_local_value (car, it->w->buffer);
18148 }
18149
18150 if (INTEGERP (car) || FLOATP (car))
18151 {
18152 double fact;
18153 pixels = XFLOATINT (car);
18154 if (NILP (cdr))
18155 return OK_PIXELS (pixels);
18156 if (calc_pixel_width_or_height (&fact, it, cdr,
18157 font, width_p, align_to))
18158 return OK_PIXELS (pixels * fact);
18159 return 0;
18160 }
18161
18162 return 0;
18163 }
18164
18165 return 0;
18166 }
18167
18168 \f
18169 /***********************************************************************
18170 Glyph Display
18171 ***********************************************************************/
18172
18173 #ifdef HAVE_WINDOW_SYSTEM
18174
18175 #if GLYPH_DEBUG
18176
18177 void
18178 dump_glyph_string (s)
18179 struct glyph_string *s;
18180 {
18181 fprintf (stderr, "glyph string\n");
18182 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18183 s->x, s->y, s->width, s->height);
18184 fprintf (stderr, " ybase = %d\n", s->ybase);
18185 fprintf (stderr, " hl = %d\n", s->hl);
18186 fprintf (stderr, " left overhang = %d, right = %d\n",
18187 s->left_overhang, s->right_overhang);
18188 fprintf (stderr, " nchars = %d\n", s->nchars);
18189 fprintf (stderr, " extends to end of line = %d\n",
18190 s->extends_to_end_of_line_p);
18191 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18192 fprintf (stderr, " bg width = %d\n", s->background_width);
18193 }
18194
18195 #endif /* GLYPH_DEBUG */
18196
18197 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18198 of XChar2b structures for S; it can't be allocated in
18199 init_glyph_string because it must be allocated via `alloca'. W
18200 is the window on which S is drawn. ROW and AREA are the glyph row
18201 and area within the row from which S is constructed. START is the
18202 index of the first glyph structure covered by S. HL is a
18203 face-override for drawing S. */
18204
18205 #ifdef HAVE_NTGUI
18206 #define OPTIONAL_HDC(hdc) hdc,
18207 #define DECLARE_HDC(hdc) HDC hdc;
18208 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18209 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18210 #endif
18211
18212 #ifndef OPTIONAL_HDC
18213 #define OPTIONAL_HDC(hdc)
18214 #define DECLARE_HDC(hdc)
18215 #define ALLOCATE_HDC(hdc, f)
18216 #define RELEASE_HDC(hdc, f)
18217 #endif
18218
18219 static void
18220 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18221 struct glyph_string *s;
18222 DECLARE_HDC (hdc)
18223 XChar2b *char2b;
18224 struct window *w;
18225 struct glyph_row *row;
18226 enum glyph_row_area area;
18227 int start;
18228 enum draw_glyphs_face hl;
18229 {
18230 bzero (s, sizeof *s);
18231 s->w = w;
18232 s->f = XFRAME (w->frame);
18233 #ifdef HAVE_NTGUI
18234 s->hdc = hdc;
18235 #endif
18236 s->display = FRAME_X_DISPLAY (s->f);
18237 s->window = FRAME_X_WINDOW (s->f);
18238 s->char2b = char2b;
18239 s->hl = hl;
18240 s->row = row;
18241 s->area = area;
18242 s->first_glyph = row->glyphs[area] + start;
18243 s->height = row->height;
18244 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18245
18246 /* Display the internal border below the tool-bar window. */
18247 if (WINDOWP (s->f->tool_bar_window)
18248 && s->w == XWINDOW (s->f->tool_bar_window))
18249 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18250
18251 s->ybase = s->y + row->ascent;
18252 }
18253
18254
18255 /* Append the list of glyph strings with head H and tail T to the list
18256 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18257
18258 static INLINE void
18259 append_glyph_string_lists (head, tail, h, t)
18260 struct glyph_string **head, **tail;
18261 struct glyph_string *h, *t;
18262 {
18263 if (h)
18264 {
18265 if (*head)
18266 (*tail)->next = h;
18267 else
18268 *head = h;
18269 h->prev = *tail;
18270 *tail = t;
18271 }
18272 }
18273
18274
18275 /* Prepend the list of glyph strings with head H and tail T to the
18276 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18277 result. */
18278
18279 static INLINE void
18280 prepend_glyph_string_lists (head, tail, h, t)
18281 struct glyph_string **head, **tail;
18282 struct glyph_string *h, *t;
18283 {
18284 if (h)
18285 {
18286 if (*head)
18287 (*head)->prev = t;
18288 else
18289 *tail = t;
18290 t->next = *head;
18291 *head = h;
18292 }
18293 }
18294
18295
18296 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18297 Set *HEAD and *TAIL to the resulting list. */
18298
18299 static INLINE void
18300 append_glyph_string (head, tail, s)
18301 struct glyph_string **head, **tail;
18302 struct glyph_string *s;
18303 {
18304 s->next = s->prev = NULL;
18305 append_glyph_string_lists (head, tail, s, s);
18306 }
18307
18308
18309 /* Get face and two-byte form of character glyph GLYPH on frame F.
18310 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18311 a pointer to a realized face that is ready for display. */
18312
18313 static INLINE struct face *
18314 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18315 struct frame *f;
18316 struct glyph *glyph;
18317 XChar2b *char2b;
18318 int *two_byte_p;
18319 {
18320 struct face *face;
18321
18322 xassert (glyph->type == CHAR_GLYPH);
18323 face = FACE_FROM_ID (f, glyph->face_id);
18324
18325 if (two_byte_p)
18326 *two_byte_p = 0;
18327
18328 if (!glyph->multibyte_p)
18329 {
18330 /* Unibyte case. We don't have to encode, but we have to make
18331 sure to use a face suitable for unibyte. */
18332 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18333 }
18334 else if (glyph->u.ch < 128
18335 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
18336 {
18337 /* Case of ASCII in a face known to fit ASCII. */
18338 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18339 }
18340 else
18341 {
18342 int c1, c2, charset;
18343
18344 /* Split characters into bytes. If c2 is -1 afterwards, C is
18345 really a one-byte character so that byte1 is zero. */
18346 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18347 if (c2 > 0)
18348 STORE_XCHAR2B (char2b, c1, c2);
18349 else
18350 STORE_XCHAR2B (char2b, 0, c1);
18351
18352 /* Maybe encode the character in *CHAR2B. */
18353 if (charset != CHARSET_ASCII)
18354 {
18355 struct font_info *font_info
18356 = FONT_INFO_FROM_ID (f, face->font_info_id);
18357 if (font_info)
18358 glyph->font_type
18359 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18360 }
18361 }
18362
18363 /* Make sure X resources of the face are allocated. */
18364 xassert (face != NULL);
18365 PREPARE_FACE_FOR_DISPLAY (f, face);
18366 return face;
18367 }
18368
18369
18370 /* Fill glyph string S with composition components specified by S->cmp.
18371
18372 FACES is an array of faces for all components of this composition.
18373 S->gidx is the index of the first component for S.
18374
18375 OVERLAPS non-zero means S should draw the foreground only, and use
18376 its physical height for clipping. See also draw_glyphs.
18377
18378 Value is the index of a component not in S. */
18379
18380 static int
18381 fill_composite_glyph_string (s, faces, overlaps)
18382 struct glyph_string *s;
18383 struct face **faces;
18384 int overlaps;
18385 {
18386 int i;
18387
18388 xassert (s);
18389
18390 s->for_overlaps = overlaps;
18391
18392 s->face = faces[s->gidx];
18393 s->font = s->face->font;
18394 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18395
18396 /* For all glyphs of this composition, starting at the offset
18397 S->gidx, until we reach the end of the definition or encounter a
18398 glyph that requires the different face, add it to S. */
18399 ++s->nchars;
18400 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18401 ++s->nchars;
18402
18403 /* All glyph strings for the same composition has the same width,
18404 i.e. the width set for the first component of the composition. */
18405
18406 s->width = s->first_glyph->pixel_width;
18407
18408 /* If the specified font could not be loaded, use the frame's
18409 default font, but record the fact that we couldn't load it in
18410 the glyph string so that we can draw rectangles for the
18411 characters of the glyph string. */
18412 if (s->font == NULL)
18413 {
18414 s->font_not_found_p = 1;
18415 s->font = FRAME_FONT (s->f);
18416 }
18417
18418 /* Adjust base line for subscript/superscript text. */
18419 s->ybase += s->first_glyph->voffset;
18420
18421 xassert (s->face && s->face->gc);
18422
18423 /* This glyph string must always be drawn with 16-bit functions. */
18424 s->two_byte_p = 1;
18425
18426 return s->gidx + s->nchars;
18427 }
18428
18429
18430 /* Fill glyph string S from a sequence of character glyphs.
18431
18432 FACE_ID is the face id of the string. START is the index of the
18433 first glyph to consider, END is the index of the last + 1.
18434 OVERLAPS non-zero means S should draw the foreground only, and use
18435 its physical height for clipping. See also draw_glyphs.
18436
18437 Value is the index of the first glyph not in S. */
18438
18439 static int
18440 fill_glyph_string (s, face_id, start, end, overlaps)
18441 struct glyph_string *s;
18442 int face_id;
18443 int start, end, overlaps;
18444 {
18445 struct glyph *glyph, *last;
18446 int voffset;
18447 int glyph_not_available_p;
18448
18449 xassert (s->f == XFRAME (s->w->frame));
18450 xassert (s->nchars == 0);
18451 xassert (start >= 0 && end > start);
18452
18453 s->for_overlaps = overlaps,
18454 glyph = s->row->glyphs[s->area] + start;
18455 last = s->row->glyphs[s->area] + end;
18456 voffset = glyph->voffset;
18457
18458 glyph_not_available_p = glyph->glyph_not_available_p;
18459
18460 while (glyph < last
18461 && glyph->type == CHAR_GLYPH
18462 && glyph->voffset == voffset
18463 /* Same face id implies same font, nowadays. */
18464 && glyph->face_id == face_id
18465 && glyph->glyph_not_available_p == glyph_not_available_p)
18466 {
18467 int two_byte_p;
18468
18469 s->face = get_glyph_face_and_encoding (s->f, glyph,
18470 s->char2b + s->nchars,
18471 &two_byte_p);
18472 s->two_byte_p = two_byte_p;
18473 ++s->nchars;
18474 xassert (s->nchars <= end - start);
18475 s->width += glyph->pixel_width;
18476 ++glyph;
18477 }
18478
18479 s->font = s->face->font;
18480 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18481
18482 /* If the specified font could not be loaded, use the frame's font,
18483 but record the fact that we couldn't load it in
18484 S->font_not_found_p so that we can draw rectangles for the
18485 characters of the glyph string. */
18486 if (s->font == NULL || glyph_not_available_p)
18487 {
18488 s->font_not_found_p = 1;
18489 s->font = FRAME_FONT (s->f);
18490 }
18491
18492 /* Adjust base line for subscript/superscript text. */
18493 s->ybase += voffset;
18494
18495 xassert (s->face && s->face->gc);
18496 return glyph - s->row->glyphs[s->area];
18497 }
18498
18499
18500 /* Fill glyph string S from image glyph S->first_glyph. */
18501
18502 static void
18503 fill_image_glyph_string (s)
18504 struct glyph_string *s;
18505 {
18506 xassert (s->first_glyph->type == IMAGE_GLYPH);
18507 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18508 xassert (s->img);
18509 s->slice = s->first_glyph->slice;
18510 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18511 s->font = s->face->font;
18512 s->width = s->first_glyph->pixel_width;
18513
18514 /* Adjust base line for subscript/superscript text. */
18515 s->ybase += s->first_glyph->voffset;
18516 }
18517
18518
18519 /* Fill glyph string S from a sequence of stretch glyphs.
18520
18521 ROW is the glyph row in which the glyphs are found, AREA is the
18522 area within the row. START is the index of the first glyph to
18523 consider, END is the index of the last + 1.
18524
18525 Value is the index of the first glyph not in S. */
18526
18527 static int
18528 fill_stretch_glyph_string (s, row, area, start, end)
18529 struct glyph_string *s;
18530 struct glyph_row *row;
18531 enum glyph_row_area area;
18532 int start, end;
18533 {
18534 struct glyph *glyph, *last;
18535 int voffset, face_id;
18536
18537 xassert (s->first_glyph->type == STRETCH_GLYPH);
18538
18539 glyph = s->row->glyphs[s->area] + start;
18540 last = s->row->glyphs[s->area] + end;
18541 face_id = glyph->face_id;
18542 s->face = FACE_FROM_ID (s->f, face_id);
18543 s->font = s->face->font;
18544 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18545 s->width = glyph->pixel_width;
18546 voffset = glyph->voffset;
18547
18548 for (++glyph;
18549 (glyph < last
18550 && glyph->type == STRETCH_GLYPH
18551 && glyph->voffset == voffset
18552 && glyph->face_id == face_id);
18553 ++glyph)
18554 s->width += glyph->pixel_width;
18555
18556 /* Adjust base line for subscript/superscript text. */
18557 s->ybase += voffset;
18558
18559 /* The case that face->gc == 0 is handled when drawing the glyph
18560 string by calling PREPARE_FACE_FOR_DISPLAY. */
18561 xassert (s->face);
18562 return glyph - s->row->glyphs[s->area];
18563 }
18564
18565
18566 /* EXPORT for RIF:
18567 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18568 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18569 assumed to be zero. */
18570
18571 void
18572 x_get_glyph_overhangs (glyph, f, left, right)
18573 struct glyph *glyph;
18574 struct frame *f;
18575 int *left, *right;
18576 {
18577 *left = *right = 0;
18578
18579 if (glyph->type == CHAR_GLYPH)
18580 {
18581 XFontStruct *font;
18582 struct face *face;
18583 struct font_info *font_info;
18584 XChar2b char2b;
18585 XCharStruct *pcm;
18586
18587 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18588 font = face->font;
18589 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18590 if (font /* ++KFS: Should this be font_info ? */
18591 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18592 {
18593 if (pcm->rbearing > pcm->width)
18594 *right = pcm->rbearing - pcm->width;
18595 if (pcm->lbearing < 0)
18596 *left = -pcm->lbearing;
18597 }
18598 }
18599 }
18600
18601
18602 /* Return the index of the first glyph preceding glyph string S that
18603 is overwritten by S because of S's left overhang. Value is -1
18604 if no glyphs are overwritten. */
18605
18606 static int
18607 left_overwritten (s)
18608 struct glyph_string *s;
18609 {
18610 int k;
18611
18612 if (s->left_overhang)
18613 {
18614 int x = 0, i;
18615 struct glyph *glyphs = s->row->glyphs[s->area];
18616 int first = s->first_glyph - glyphs;
18617
18618 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18619 x -= glyphs[i].pixel_width;
18620
18621 k = i + 1;
18622 }
18623 else
18624 k = -1;
18625
18626 return k;
18627 }
18628
18629
18630 /* Return the index of the first glyph preceding glyph string S that
18631 is overwriting S because of its right overhang. Value is -1 if no
18632 glyph in front of S overwrites S. */
18633
18634 static int
18635 left_overwriting (s)
18636 struct glyph_string *s;
18637 {
18638 int i, k, x;
18639 struct glyph *glyphs = s->row->glyphs[s->area];
18640 int first = s->first_glyph - glyphs;
18641
18642 k = -1;
18643 x = 0;
18644 for (i = first - 1; i >= 0; --i)
18645 {
18646 int left, right;
18647 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18648 if (x + right > 0)
18649 k = i;
18650 x -= glyphs[i].pixel_width;
18651 }
18652
18653 return k;
18654 }
18655
18656
18657 /* Return the index of the last glyph following glyph string S that is
18658 not overwritten by S because of S's right overhang. Value is -1 if
18659 no such glyph is found. */
18660
18661 static int
18662 right_overwritten (s)
18663 struct glyph_string *s;
18664 {
18665 int k = -1;
18666
18667 if (s->right_overhang)
18668 {
18669 int x = 0, i;
18670 struct glyph *glyphs = s->row->glyphs[s->area];
18671 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18672 int end = s->row->used[s->area];
18673
18674 for (i = first; i < end && s->right_overhang > x; ++i)
18675 x += glyphs[i].pixel_width;
18676
18677 k = i;
18678 }
18679
18680 return k;
18681 }
18682
18683
18684 /* Return the index of the last glyph following glyph string S that
18685 overwrites S because of its left overhang. Value is negative
18686 if no such glyph is found. */
18687
18688 static int
18689 right_overwriting (s)
18690 struct glyph_string *s;
18691 {
18692 int i, k, x;
18693 int end = s->row->used[s->area];
18694 struct glyph *glyphs = s->row->glyphs[s->area];
18695 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18696
18697 k = -1;
18698 x = 0;
18699 for (i = first; i < end; ++i)
18700 {
18701 int left, right;
18702 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18703 if (x - left < 0)
18704 k = i;
18705 x += glyphs[i].pixel_width;
18706 }
18707
18708 return k;
18709 }
18710
18711
18712 /* Get face and two-byte form of character C in face FACE_ID on frame
18713 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18714 means we want to display multibyte text. DISPLAY_P non-zero means
18715 make sure that X resources for the face returned are allocated.
18716 Value is a pointer to a realized face that is ready for display if
18717 DISPLAY_P is non-zero. */
18718
18719 static INLINE struct face *
18720 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18721 struct frame *f;
18722 int c, face_id;
18723 XChar2b *char2b;
18724 int multibyte_p, display_p;
18725 {
18726 struct face *face = FACE_FROM_ID (f, face_id);
18727
18728 if (!multibyte_p)
18729 {
18730 /* Unibyte case. We don't have to encode, but we have to make
18731 sure to use a face suitable for unibyte. */
18732 STORE_XCHAR2B (char2b, 0, c);
18733 face_id = FACE_FOR_CHAR (f, face, c);
18734 face = FACE_FROM_ID (f, face_id);
18735 }
18736 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18737 {
18738 /* Case of ASCII in a face known to fit ASCII. */
18739 STORE_XCHAR2B (char2b, 0, c);
18740 }
18741 else
18742 {
18743 int c1, c2, charset;
18744
18745 /* Split characters into bytes. If c2 is -1 afterwards, C is
18746 really a one-byte character so that byte1 is zero. */
18747 SPLIT_CHAR (c, charset, c1, c2);
18748 if (c2 > 0)
18749 STORE_XCHAR2B (char2b, c1, c2);
18750 else
18751 STORE_XCHAR2B (char2b, 0, c1);
18752
18753 /* Maybe encode the character in *CHAR2B. */
18754 if (face->font != NULL)
18755 {
18756 struct font_info *font_info
18757 = FONT_INFO_FROM_ID (f, face->font_info_id);
18758 if (font_info)
18759 rif->encode_char (c, char2b, font_info, 0);
18760 }
18761 }
18762
18763 /* Make sure X resources of the face are allocated. */
18764 #ifdef HAVE_X_WINDOWS
18765 if (display_p)
18766 #endif
18767 {
18768 xassert (face != NULL);
18769 PREPARE_FACE_FOR_DISPLAY (f, face);
18770 }
18771
18772 return face;
18773 }
18774
18775
18776 /* Set background width of glyph string S. START is the index of the
18777 first glyph following S. LAST_X is the right-most x-position + 1
18778 in the drawing area. */
18779
18780 static INLINE void
18781 set_glyph_string_background_width (s, start, last_x)
18782 struct glyph_string *s;
18783 int start;
18784 int last_x;
18785 {
18786 /* If the face of this glyph string has to be drawn to the end of
18787 the drawing area, set S->extends_to_end_of_line_p. */
18788
18789 if (start == s->row->used[s->area]
18790 && s->area == TEXT_AREA
18791 && ((s->row->fill_line_p
18792 && (s->hl == DRAW_NORMAL_TEXT
18793 || s->hl == DRAW_IMAGE_RAISED
18794 || s->hl == DRAW_IMAGE_SUNKEN))
18795 || s->hl == DRAW_MOUSE_FACE))
18796 s->extends_to_end_of_line_p = 1;
18797
18798 /* If S extends its face to the end of the line, set its
18799 background_width to the distance to the right edge of the drawing
18800 area. */
18801 if (s->extends_to_end_of_line_p)
18802 s->background_width = last_x - s->x + 1;
18803 else
18804 s->background_width = s->width;
18805 }
18806
18807
18808 /* Compute overhangs and x-positions for glyph string S and its
18809 predecessors, or successors. X is the starting x-position for S.
18810 BACKWARD_P non-zero means process predecessors. */
18811
18812 static void
18813 compute_overhangs_and_x (s, x, backward_p)
18814 struct glyph_string *s;
18815 int x;
18816 int backward_p;
18817 {
18818 if (backward_p)
18819 {
18820 while (s)
18821 {
18822 if (rif->compute_glyph_string_overhangs)
18823 rif->compute_glyph_string_overhangs (s);
18824 x -= s->width;
18825 s->x = x;
18826 s = s->prev;
18827 }
18828 }
18829 else
18830 {
18831 while (s)
18832 {
18833 if (rif->compute_glyph_string_overhangs)
18834 rif->compute_glyph_string_overhangs (s);
18835 s->x = x;
18836 x += s->width;
18837 s = s->next;
18838 }
18839 }
18840 }
18841
18842
18843
18844 /* The following macros are only called from draw_glyphs below.
18845 They reference the following parameters of that function directly:
18846 `w', `row', `area', and `overlap_p'
18847 as well as the following local variables:
18848 `s', `f', and `hdc' (in W32) */
18849
18850 #ifdef HAVE_NTGUI
18851 /* On W32, silently add local `hdc' variable to argument list of
18852 init_glyph_string. */
18853 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18854 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18855 #else
18856 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18857 init_glyph_string (s, char2b, w, row, area, start, hl)
18858 #endif
18859
18860 /* Add a glyph string for a stretch glyph to the list of strings
18861 between HEAD and TAIL. START is the index of the stretch glyph in
18862 row area AREA of glyph row ROW. END is the index of the last glyph
18863 in that glyph row area. X is the current output position assigned
18864 to the new glyph string constructed. HL overrides that face of the
18865 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18866 is the right-most x-position of the drawing area. */
18867
18868 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18869 and below -- keep them on one line. */
18870 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18871 do \
18872 { \
18873 s = (struct glyph_string *) alloca (sizeof *s); \
18874 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18875 START = fill_stretch_glyph_string (s, row, area, START, END); \
18876 append_glyph_string (&HEAD, &TAIL, s); \
18877 s->x = (X); \
18878 } \
18879 while (0)
18880
18881
18882 /* Add a glyph string for an image glyph to the list of strings
18883 between HEAD and TAIL. START is the index of the image glyph in
18884 row area AREA of glyph row ROW. END is the index of the last glyph
18885 in that glyph row area. X is the current output position assigned
18886 to the new glyph string constructed. HL overrides that face of the
18887 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18888 is the right-most x-position of the drawing area. */
18889
18890 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18891 do \
18892 { \
18893 s = (struct glyph_string *) alloca (sizeof *s); \
18894 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18895 fill_image_glyph_string (s); \
18896 append_glyph_string (&HEAD, &TAIL, s); \
18897 ++START; \
18898 s->x = (X); \
18899 } \
18900 while (0)
18901
18902
18903 /* Add a glyph string for a sequence of character glyphs to the list
18904 of strings between HEAD and TAIL. START is the index of the first
18905 glyph in row area AREA of glyph row ROW that is part of the new
18906 glyph string. END is the index of the last glyph in that glyph row
18907 area. X is the current output position assigned to the new glyph
18908 string constructed. HL overrides that face of the glyph; e.g. it
18909 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18910 right-most x-position of the drawing area. */
18911
18912 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18913 do \
18914 { \
18915 int c, face_id; \
18916 XChar2b *char2b; \
18917 \
18918 c = (row)->glyphs[area][START].u.ch; \
18919 face_id = (row)->glyphs[area][START].face_id; \
18920 \
18921 s = (struct glyph_string *) alloca (sizeof *s); \
18922 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18923 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18924 append_glyph_string (&HEAD, &TAIL, s); \
18925 s->x = (X); \
18926 START = fill_glyph_string (s, face_id, START, END, overlaps); \
18927 } \
18928 while (0)
18929
18930
18931 /* Add a glyph string for a composite sequence to the list of strings
18932 between HEAD and TAIL. START is the index of the first glyph in
18933 row area AREA of glyph row ROW that is part of the new glyph
18934 string. END is the index of the last glyph in that glyph row area.
18935 X is the current output position assigned to the new glyph string
18936 constructed. HL overrides that face of the glyph; e.g. it is
18937 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18938 x-position of the drawing area. */
18939
18940 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18941 do { \
18942 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18943 int face_id = (row)->glyphs[area][START].face_id; \
18944 struct face *base_face = FACE_FROM_ID (f, face_id); \
18945 struct composition *cmp = composition_table[cmp_id]; \
18946 int glyph_len = cmp->glyph_len; \
18947 XChar2b *char2b; \
18948 struct face **faces; \
18949 struct glyph_string *first_s = NULL; \
18950 int n; \
18951 \
18952 base_face = base_face->ascii_face; \
18953 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18954 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18955 /* At first, fill in `char2b' and `faces'. */ \
18956 for (n = 0; n < glyph_len; n++) \
18957 { \
18958 int c = COMPOSITION_GLYPH (cmp, n); \
18959 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18960 faces[n] = FACE_FROM_ID (f, this_face_id); \
18961 get_char_face_and_encoding (f, c, this_face_id, \
18962 char2b + n, 1, 1); \
18963 } \
18964 \
18965 /* Make glyph_strings for each glyph sequence that is drawable by \
18966 the same face, and append them to HEAD/TAIL. */ \
18967 for (n = 0; n < cmp->glyph_len;) \
18968 { \
18969 s = (struct glyph_string *) alloca (sizeof *s); \
18970 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18971 append_glyph_string (&(HEAD), &(TAIL), s); \
18972 s->cmp = cmp; \
18973 s->gidx = n; \
18974 s->x = (X); \
18975 \
18976 if (n == 0) \
18977 first_s = s; \
18978 \
18979 n = fill_composite_glyph_string (s, faces, overlaps); \
18980 } \
18981 \
18982 ++START; \
18983 s = first_s; \
18984 } while (0)
18985
18986
18987 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18988 of AREA of glyph row ROW on window W between indices START and END.
18989 HL overrides the face for drawing glyph strings, e.g. it is
18990 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18991 x-positions of the drawing area.
18992
18993 This is an ugly monster macro construct because we must use alloca
18994 to allocate glyph strings (because draw_glyphs can be called
18995 asynchronously). */
18996
18997 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18998 do \
18999 { \
19000 HEAD = TAIL = NULL; \
19001 while (START < END) \
19002 { \
19003 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19004 switch (first_glyph->type) \
19005 { \
19006 case CHAR_GLYPH: \
19007 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19008 HL, X, LAST_X); \
19009 break; \
19010 \
19011 case COMPOSITE_GLYPH: \
19012 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19013 HL, X, LAST_X); \
19014 break; \
19015 \
19016 case STRETCH_GLYPH: \
19017 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19018 HL, X, LAST_X); \
19019 break; \
19020 \
19021 case IMAGE_GLYPH: \
19022 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19023 HL, X, LAST_X); \
19024 break; \
19025 \
19026 default: \
19027 abort (); \
19028 } \
19029 \
19030 set_glyph_string_background_width (s, START, LAST_X); \
19031 (X) += s->width; \
19032 } \
19033 } \
19034 while (0)
19035
19036
19037 /* Draw glyphs between START and END in AREA of ROW on window W,
19038 starting at x-position X. X is relative to AREA in W. HL is a
19039 face-override with the following meaning:
19040
19041 DRAW_NORMAL_TEXT draw normally
19042 DRAW_CURSOR draw in cursor face
19043 DRAW_MOUSE_FACE draw in mouse face.
19044 DRAW_INVERSE_VIDEO draw in mode line face
19045 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19046 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19047
19048 If OVERLAPS is non-zero, draw only the foreground of characters and
19049 clip to the physical height of ROW. Non-zero value also defines
19050 the overlapping part to be drawn:
19051
19052 OVERLAPS_PRED overlap with preceding rows
19053 OVERLAPS_SUCC overlap with succeeding rows
19054 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19055 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19056
19057 Value is the x-position reached, relative to AREA of W. */
19058
19059 static int
19060 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19061 struct window *w;
19062 int x;
19063 struct glyph_row *row;
19064 enum glyph_row_area area;
19065 int start, end;
19066 enum draw_glyphs_face hl;
19067 int overlaps;
19068 {
19069 struct glyph_string *head, *tail;
19070 struct glyph_string *s;
19071 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19072 int last_x, area_width;
19073 int x_reached;
19074 int i, j;
19075 struct frame *f = XFRAME (WINDOW_FRAME (w));
19076 DECLARE_HDC (hdc);
19077
19078 ALLOCATE_HDC (hdc, f);
19079
19080 /* Let's rather be paranoid than getting a SEGV. */
19081 end = min (end, row->used[area]);
19082 start = max (0, start);
19083 start = min (end, start);
19084
19085 /* Translate X to frame coordinates. Set last_x to the right
19086 end of the drawing area. */
19087 if (row->full_width_p)
19088 {
19089 /* X is relative to the left edge of W, without scroll bars
19090 or fringes. */
19091 x += WINDOW_LEFT_EDGE_X (w);
19092 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19093 }
19094 else
19095 {
19096 int area_left = window_box_left (w, area);
19097 x += area_left;
19098 area_width = window_box_width (w, area);
19099 last_x = area_left + area_width;
19100 }
19101
19102 /* Build a doubly-linked list of glyph_string structures between
19103 head and tail from what we have to draw. Note that the macro
19104 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19105 the reason we use a separate variable `i'. */
19106 i = start;
19107 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19108 if (tail)
19109 x_reached = tail->x + tail->background_width;
19110 else
19111 x_reached = x;
19112
19113 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19114 the row, redraw some glyphs in front or following the glyph
19115 strings built above. */
19116 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19117 {
19118 int dummy_x = 0;
19119 struct glyph_string *h, *t;
19120
19121 /* Compute overhangs for all glyph strings. */
19122 if (rif->compute_glyph_string_overhangs)
19123 for (s = head; s; s = s->next)
19124 rif->compute_glyph_string_overhangs (s);
19125
19126 /* Prepend glyph strings for glyphs in front of the first glyph
19127 string that are overwritten because of the first glyph
19128 string's left overhang. The background of all strings
19129 prepended must be drawn because the first glyph string
19130 draws over it. */
19131 i = left_overwritten (head);
19132 if (i >= 0)
19133 {
19134 j = i;
19135 BUILD_GLYPH_STRINGS (j, start, h, t,
19136 DRAW_NORMAL_TEXT, dummy_x, last_x);
19137 start = i;
19138 compute_overhangs_and_x (t, head->x, 1);
19139 prepend_glyph_string_lists (&head, &tail, h, t);
19140 clip_head = head;
19141 }
19142
19143 /* Prepend glyph strings for glyphs in front of the first glyph
19144 string that overwrite that glyph string because of their
19145 right overhang. For these strings, only the foreground must
19146 be drawn, because it draws over the glyph string at `head'.
19147 The background must not be drawn because this would overwrite
19148 right overhangs of preceding glyphs for which no glyph
19149 strings exist. */
19150 i = left_overwriting (head);
19151 if (i >= 0)
19152 {
19153 clip_head = head;
19154 BUILD_GLYPH_STRINGS (i, start, h, t,
19155 DRAW_NORMAL_TEXT, dummy_x, last_x);
19156 for (s = h; s; s = s->next)
19157 s->background_filled_p = 1;
19158 compute_overhangs_and_x (t, head->x, 1);
19159 prepend_glyph_string_lists (&head, &tail, h, t);
19160 }
19161
19162 /* Append glyphs strings for glyphs following the last glyph
19163 string tail that are overwritten by tail. The background of
19164 these strings has to be drawn because tail's foreground draws
19165 over it. */
19166 i = right_overwritten (tail);
19167 if (i >= 0)
19168 {
19169 BUILD_GLYPH_STRINGS (end, i, h, t,
19170 DRAW_NORMAL_TEXT, x, last_x);
19171 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19172 append_glyph_string_lists (&head, &tail, h, t);
19173 clip_tail = tail;
19174 }
19175
19176 /* Append glyph strings for glyphs following the last glyph
19177 string tail that overwrite tail. The foreground of such
19178 glyphs has to be drawn because it writes into the background
19179 of tail. The background must not be drawn because it could
19180 paint over the foreground of following glyphs. */
19181 i = right_overwriting (tail);
19182 if (i >= 0)
19183 {
19184 clip_tail = tail;
19185 BUILD_GLYPH_STRINGS (end, i, h, t,
19186 DRAW_NORMAL_TEXT, x, last_x);
19187 for (s = h; s; s = s->next)
19188 s->background_filled_p = 1;
19189 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19190 append_glyph_string_lists (&head, &tail, h, t);
19191 }
19192 if (clip_head || clip_tail)
19193 for (s = head; s; s = s->next)
19194 {
19195 s->clip_head = clip_head;
19196 s->clip_tail = clip_tail;
19197 }
19198 }
19199
19200 /* Draw all strings. */
19201 for (s = head; s; s = s->next)
19202 rif->draw_glyph_string (s);
19203
19204 if (area == TEXT_AREA
19205 && !row->full_width_p
19206 /* When drawing overlapping rows, only the glyph strings'
19207 foreground is drawn, which doesn't erase a cursor
19208 completely. */
19209 && !overlaps)
19210 {
19211 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19212 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19213 : (tail ? tail->x + tail->background_width : x));
19214
19215 int text_left = window_box_left (w, TEXT_AREA);
19216 x0 -= text_left;
19217 x1 -= text_left;
19218
19219 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19220 row->y, MATRIX_ROW_BOTTOM_Y (row));
19221 }
19222
19223 /* Value is the x-position up to which drawn, relative to AREA of W.
19224 This doesn't include parts drawn because of overhangs. */
19225 if (row->full_width_p)
19226 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19227 else
19228 x_reached -= window_box_left (w, area);
19229
19230 RELEASE_HDC (hdc, f);
19231
19232 return x_reached;
19233 }
19234
19235 /* Expand row matrix if too narrow. Don't expand if area
19236 is not present. */
19237
19238 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19239 { \
19240 if (!fonts_changed_p \
19241 && (it->glyph_row->glyphs[area] \
19242 < it->glyph_row->glyphs[area + 1])) \
19243 { \
19244 it->w->ncols_scale_factor++; \
19245 fonts_changed_p = 1; \
19246 } \
19247 }
19248
19249 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19250 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19251
19252 static INLINE void
19253 append_glyph (it)
19254 struct it *it;
19255 {
19256 struct glyph *glyph;
19257 enum glyph_row_area area = it->area;
19258
19259 xassert (it->glyph_row);
19260 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19261
19262 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19263 if (glyph < it->glyph_row->glyphs[area + 1])
19264 {
19265 glyph->charpos = CHARPOS (it->position);
19266 glyph->object = it->object;
19267 glyph->pixel_width = it->pixel_width;
19268 glyph->ascent = it->ascent;
19269 glyph->descent = it->descent;
19270 glyph->voffset = it->voffset;
19271 glyph->type = CHAR_GLYPH;
19272 glyph->multibyte_p = it->multibyte_p;
19273 glyph->left_box_line_p = it->start_of_box_run_p;
19274 glyph->right_box_line_p = it->end_of_box_run_p;
19275 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19276 || it->phys_descent > it->descent);
19277 glyph->padding_p = 0;
19278 glyph->glyph_not_available_p = it->glyph_not_available_p;
19279 glyph->face_id = it->face_id;
19280 glyph->u.ch = it->char_to_display;
19281 glyph->slice = null_glyph_slice;
19282 glyph->font_type = FONT_TYPE_UNKNOWN;
19283 ++it->glyph_row->used[area];
19284 }
19285 else
19286 IT_EXPAND_MATRIX_WIDTH (it, area);
19287 }
19288
19289 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19290 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19291
19292 static INLINE void
19293 append_composite_glyph (it)
19294 struct it *it;
19295 {
19296 struct glyph *glyph;
19297 enum glyph_row_area area = it->area;
19298
19299 xassert (it->glyph_row);
19300
19301 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19302 if (glyph < it->glyph_row->glyphs[area + 1])
19303 {
19304 glyph->charpos = CHARPOS (it->position);
19305 glyph->object = it->object;
19306 glyph->pixel_width = it->pixel_width;
19307 glyph->ascent = it->ascent;
19308 glyph->descent = it->descent;
19309 glyph->voffset = it->voffset;
19310 glyph->type = COMPOSITE_GLYPH;
19311 glyph->multibyte_p = it->multibyte_p;
19312 glyph->left_box_line_p = it->start_of_box_run_p;
19313 glyph->right_box_line_p = it->end_of_box_run_p;
19314 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19315 || it->phys_descent > it->descent);
19316 glyph->padding_p = 0;
19317 glyph->glyph_not_available_p = 0;
19318 glyph->face_id = it->face_id;
19319 glyph->u.cmp_id = it->cmp_id;
19320 glyph->slice = null_glyph_slice;
19321 glyph->font_type = FONT_TYPE_UNKNOWN;
19322 ++it->glyph_row->used[area];
19323 }
19324 else
19325 IT_EXPAND_MATRIX_WIDTH (it, area);
19326 }
19327
19328
19329 /* Change IT->ascent and IT->height according to the setting of
19330 IT->voffset. */
19331
19332 static INLINE void
19333 take_vertical_position_into_account (it)
19334 struct it *it;
19335 {
19336 if (it->voffset)
19337 {
19338 if (it->voffset < 0)
19339 /* Increase the ascent so that we can display the text higher
19340 in the line. */
19341 it->ascent -= it->voffset;
19342 else
19343 /* Increase the descent so that we can display the text lower
19344 in the line. */
19345 it->descent += it->voffset;
19346 }
19347 }
19348
19349
19350 /* Produce glyphs/get display metrics for the image IT is loaded with.
19351 See the description of struct display_iterator in dispextern.h for
19352 an overview of struct display_iterator. */
19353
19354 static void
19355 produce_image_glyph (it)
19356 struct it *it;
19357 {
19358 struct image *img;
19359 struct face *face;
19360 int glyph_ascent;
19361 struct glyph_slice slice;
19362
19363 xassert (it->what == IT_IMAGE);
19364
19365 face = FACE_FROM_ID (it->f, it->face_id);
19366 xassert (face);
19367 /* Make sure X resources of the face is loaded. */
19368 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19369
19370 if (it->image_id < 0)
19371 {
19372 /* Fringe bitmap. */
19373 it->ascent = it->phys_ascent = 0;
19374 it->descent = it->phys_descent = 0;
19375 it->pixel_width = 0;
19376 it->nglyphs = 0;
19377 return;
19378 }
19379
19380 img = IMAGE_FROM_ID (it->f, it->image_id);
19381 xassert (img);
19382 /* Make sure X resources of the image is loaded. */
19383 prepare_image_for_display (it->f, img);
19384
19385 slice.x = slice.y = 0;
19386 slice.width = img->width;
19387 slice.height = img->height;
19388
19389 if (INTEGERP (it->slice.x))
19390 slice.x = XINT (it->slice.x);
19391 else if (FLOATP (it->slice.x))
19392 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19393
19394 if (INTEGERP (it->slice.y))
19395 slice.y = XINT (it->slice.y);
19396 else if (FLOATP (it->slice.y))
19397 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19398
19399 if (INTEGERP (it->slice.width))
19400 slice.width = XINT (it->slice.width);
19401 else if (FLOATP (it->slice.width))
19402 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19403
19404 if (INTEGERP (it->slice.height))
19405 slice.height = XINT (it->slice.height);
19406 else if (FLOATP (it->slice.height))
19407 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19408
19409 if (slice.x >= img->width)
19410 slice.x = img->width;
19411 if (slice.y >= img->height)
19412 slice.y = img->height;
19413 if (slice.x + slice.width >= img->width)
19414 slice.width = img->width - slice.x;
19415 if (slice.y + slice.height > img->height)
19416 slice.height = img->height - slice.y;
19417
19418 if (slice.width == 0 || slice.height == 0)
19419 return;
19420
19421 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19422
19423 it->descent = slice.height - glyph_ascent;
19424 if (slice.y == 0)
19425 it->descent += img->vmargin;
19426 if (slice.y + slice.height == img->height)
19427 it->descent += img->vmargin;
19428 it->phys_descent = it->descent;
19429
19430 it->pixel_width = slice.width;
19431 if (slice.x == 0)
19432 it->pixel_width += img->hmargin;
19433 if (slice.x + slice.width == img->width)
19434 it->pixel_width += img->hmargin;
19435
19436 /* It's quite possible for images to have an ascent greater than
19437 their height, so don't get confused in that case. */
19438 if (it->descent < 0)
19439 it->descent = 0;
19440
19441 #if 0 /* this breaks image tiling */
19442 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19443 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19444 if (face_ascent > it->ascent)
19445 it->ascent = it->phys_ascent = face_ascent;
19446 #endif
19447
19448 it->nglyphs = 1;
19449
19450 if (face->box != FACE_NO_BOX)
19451 {
19452 if (face->box_line_width > 0)
19453 {
19454 if (slice.y == 0)
19455 it->ascent += face->box_line_width;
19456 if (slice.y + slice.height == img->height)
19457 it->descent += face->box_line_width;
19458 }
19459
19460 if (it->start_of_box_run_p && slice.x == 0)
19461 it->pixel_width += abs (face->box_line_width);
19462 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19463 it->pixel_width += abs (face->box_line_width);
19464 }
19465
19466 take_vertical_position_into_account (it);
19467
19468 if (it->glyph_row)
19469 {
19470 struct glyph *glyph;
19471 enum glyph_row_area area = it->area;
19472
19473 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19474 if (glyph < it->glyph_row->glyphs[area + 1])
19475 {
19476 glyph->charpos = CHARPOS (it->position);
19477 glyph->object = it->object;
19478 glyph->pixel_width = it->pixel_width;
19479 glyph->ascent = glyph_ascent;
19480 glyph->descent = it->descent;
19481 glyph->voffset = it->voffset;
19482 glyph->type = IMAGE_GLYPH;
19483 glyph->multibyte_p = it->multibyte_p;
19484 glyph->left_box_line_p = it->start_of_box_run_p;
19485 glyph->right_box_line_p = it->end_of_box_run_p;
19486 glyph->overlaps_vertically_p = 0;
19487 glyph->padding_p = 0;
19488 glyph->glyph_not_available_p = 0;
19489 glyph->face_id = it->face_id;
19490 glyph->u.img_id = img->id;
19491 glyph->slice = slice;
19492 glyph->font_type = FONT_TYPE_UNKNOWN;
19493 ++it->glyph_row->used[area];
19494 }
19495 else
19496 IT_EXPAND_MATRIX_WIDTH (it, area);
19497 }
19498 }
19499
19500
19501 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19502 of the glyph, WIDTH and HEIGHT are the width and height of the
19503 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19504
19505 static void
19506 append_stretch_glyph (it, object, width, height, ascent)
19507 struct it *it;
19508 Lisp_Object object;
19509 int width, height;
19510 int ascent;
19511 {
19512 struct glyph *glyph;
19513 enum glyph_row_area area = it->area;
19514
19515 xassert (ascent >= 0 && ascent <= height);
19516
19517 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19518 if (glyph < it->glyph_row->glyphs[area + 1])
19519 {
19520 glyph->charpos = CHARPOS (it->position);
19521 glyph->object = object;
19522 glyph->pixel_width = width;
19523 glyph->ascent = ascent;
19524 glyph->descent = height - ascent;
19525 glyph->voffset = it->voffset;
19526 glyph->type = STRETCH_GLYPH;
19527 glyph->multibyte_p = it->multibyte_p;
19528 glyph->left_box_line_p = it->start_of_box_run_p;
19529 glyph->right_box_line_p = it->end_of_box_run_p;
19530 glyph->overlaps_vertically_p = 0;
19531 glyph->padding_p = 0;
19532 glyph->glyph_not_available_p = 0;
19533 glyph->face_id = it->face_id;
19534 glyph->u.stretch.ascent = ascent;
19535 glyph->u.stretch.height = height;
19536 glyph->slice = null_glyph_slice;
19537 glyph->font_type = FONT_TYPE_UNKNOWN;
19538 ++it->glyph_row->used[area];
19539 }
19540 else
19541 IT_EXPAND_MATRIX_WIDTH (it, area);
19542 }
19543
19544
19545 /* Produce a stretch glyph for iterator IT. IT->object is the value
19546 of the glyph property displayed. The value must be a list
19547 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19548 being recognized:
19549
19550 1. `:width WIDTH' specifies that the space should be WIDTH *
19551 canonical char width wide. WIDTH may be an integer or floating
19552 point number.
19553
19554 2. `:relative-width FACTOR' specifies that the width of the stretch
19555 should be computed from the width of the first character having the
19556 `glyph' property, and should be FACTOR times that width.
19557
19558 3. `:align-to HPOS' specifies that the space should be wide enough
19559 to reach HPOS, a value in canonical character units.
19560
19561 Exactly one of the above pairs must be present.
19562
19563 4. `:height HEIGHT' specifies that the height of the stretch produced
19564 should be HEIGHT, measured in canonical character units.
19565
19566 5. `:relative-height FACTOR' specifies that the height of the
19567 stretch should be FACTOR times the height of the characters having
19568 the glyph property.
19569
19570 Either none or exactly one of 4 or 5 must be present.
19571
19572 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19573 of the stretch should be used for the ascent of the stretch.
19574 ASCENT must be in the range 0 <= ASCENT <= 100. */
19575
19576 static void
19577 produce_stretch_glyph (it)
19578 struct it *it;
19579 {
19580 /* (space :width WIDTH :height HEIGHT ...) */
19581 Lisp_Object prop, plist;
19582 int width = 0, height = 0, align_to = -1;
19583 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19584 int ascent = 0;
19585 double tem;
19586 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19587 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19588
19589 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19590
19591 /* List should start with `space'. */
19592 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19593 plist = XCDR (it->object);
19594
19595 /* Compute the width of the stretch. */
19596 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19597 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19598 {
19599 /* Absolute width `:width WIDTH' specified and valid. */
19600 zero_width_ok_p = 1;
19601 width = (int)tem;
19602 }
19603 else if (prop = Fplist_get (plist, QCrelative_width),
19604 NUMVAL (prop) > 0)
19605 {
19606 /* Relative width `:relative-width FACTOR' specified and valid.
19607 Compute the width of the characters having the `glyph'
19608 property. */
19609 struct it it2;
19610 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19611
19612 it2 = *it;
19613 if (it->multibyte_p)
19614 {
19615 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19616 - IT_BYTEPOS (*it));
19617 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19618 }
19619 else
19620 it2.c = *p, it2.len = 1;
19621
19622 it2.glyph_row = NULL;
19623 it2.what = IT_CHARACTER;
19624 x_produce_glyphs (&it2);
19625 width = NUMVAL (prop) * it2.pixel_width;
19626 }
19627 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19628 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19629 {
19630 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19631 align_to = (align_to < 0
19632 ? 0
19633 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19634 else if (align_to < 0)
19635 align_to = window_box_left_offset (it->w, TEXT_AREA);
19636 width = max (0, (int)tem + align_to - it->current_x);
19637 zero_width_ok_p = 1;
19638 }
19639 else
19640 /* Nothing specified -> width defaults to canonical char width. */
19641 width = FRAME_COLUMN_WIDTH (it->f);
19642
19643 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19644 width = 1;
19645
19646 /* Compute height. */
19647 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19648 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19649 {
19650 height = (int)tem;
19651 zero_height_ok_p = 1;
19652 }
19653 else if (prop = Fplist_get (plist, QCrelative_height),
19654 NUMVAL (prop) > 0)
19655 height = FONT_HEIGHT (font) * NUMVAL (prop);
19656 else
19657 height = FONT_HEIGHT (font);
19658
19659 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19660 height = 1;
19661
19662 /* Compute percentage of height used for ascent. If
19663 `:ascent ASCENT' is present and valid, use that. Otherwise,
19664 derive the ascent from the font in use. */
19665 if (prop = Fplist_get (plist, QCascent),
19666 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19667 ascent = height * NUMVAL (prop) / 100.0;
19668 else if (!NILP (prop)
19669 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19670 ascent = min (max (0, (int)tem), height);
19671 else
19672 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19673
19674 if (width > 0 && height > 0 && it->glyph_row)
19675 {
19676 Lisp_Object object = it->stack[it->sp - 1].string;
19677 if (!STRINGP (object))
19678 object = it->w->buffer;
19679 append_stretch_glyph (it, object, width, height, ascent);
19680 }
19681
19682 it->pixel_width = width;
19683 it->ascent = it->phys_ascent = ascent;
19684 it->descent = it->phys_descent = height - it->ascent;
19685 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19686
19687 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19688 {
19689 if (face->box_line_width > 0)
19690 {
19691 it->ascent += face->box_line_width;
19692 it->descent += face->box_line_width;
19693 }
19694
19695 if (it->start_of_box_run_p)
19696 it->pixel_width += abs (face->box_line_width);
19697 if (it->end_of_box_run_p)
19698 it->pixel_width += abs (face->box_line_width);
19699 }
19700
19701 take_vertical_position_into_account (it);
19702 }
19703
19704 /* Get line-height and line-spacing property at point.
19705 If line-height has format (HEIGHT TOTAL), return TOTAL
19706 in TOTAL_HEIGHT. */
19707
19708 static Lisp_Object
19709 get_line_height_property (it, prop)
19710 struct it *it;
19711 Lisp_Object prop;
19712 {
19713 Lisp_Object position;
19714
19715 if (STRINGP (it->object))
19716 position = make_number (IT_STRING_CHARPOS (*it));
19717 else if (BUFFERP (it->object))
19718 position = make_number (IT_CHARPOS (*it));
19719 else
19720 return Qnil;
19721
19722 return Fget_char_property (position, prop, it->object);
19723 }
19724
19725 /* Calculate line-height and line-spacing properties.
19726 An integer value specifies explicit pixel value.
19727 A float value specifies relative value to current face height.
19728 A cons (float . face-name) specifies relative value to
19729 height of specified face font.
19730
19731 Returns height in pixels, or nil. */
19732
19733
19734 static Lisp_Object
19735 calc_line_height_property (it, val, font, boff, override)
19736 struct it *it;
19737 Lisp_Object val;
19738 XFontStruct *font;
19739 int boff, override;
19740 {
19741 Lisp_Object face_name = Qnil;
19742 int ascent, descent, height;
19743
19744 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19745 return val;
19746
19747 if (CONSP (val))
19748 {
19749 face_name = XCAR (val);
19750 val = XCDR (val);
19751 if (!NUMBERP (val))
19752 val = make_number (1);
19753 if (NILP (face_name))
19754 {
19755 height = it->ascent + it->descent;
19756 goto scale;
19757 }
19758 }
19759
19760 if (NILP (face_name))
19761 {
19762 font = FRAME_FONT (it->f);
19763 boff = FRAME_BASELINE_OFFSET (it->f);
19764 }
19765 else if (EQ (face_name, Qt))
19766 {
19767 override = 0;
19768 }
19769 else
19770 {
19771 int face_id;
19772 struct face *face;
19773 struct font_info *font_info;
19774
19775 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19776 if (face_id < 0)
19777 return make_number (-1);
19778
19779 face = FACE_FROM_ID (it->f, face_id);
19780 font = face->font;
19781 if (font == NULL)
19782 return make_number (-1);
19783
19784 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19785 boff = font_info->baseline_offset;
19786 if (font_info->vertical_centering)
19787 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19788 }
19789
19790 ascent = FONT_BASE (font) + boff;
19791 descent = FONT_DESCENT (font) - boff;
19792
19793 if (override)
19794 {
19795 it->override_ascent = ascent;
19796 it->override_descent = descent;
19797 it->override_boff = boff;
19798 }
19799
19800 height = ascent + descent;
19801
19802 scale:
19803 if (FLOATP (val))
19804 height = (int)(XFLOAT_DATA (val) * height);
19805 else if (INTEGERP (val))
19806 height *= XINT (val);
19807
19808 return make_number (height);
19809 }
19810
19811
19812 /* RIF:
19813 Produce glyphs/get display metrics for the display element IT is
19814 loaded with. See the description of struct display_iterator in
19815 dispextern.h for an overview of struct display_iterator. */
19816
19817 void
19818 x_produce_glyphs (it)
19819 struct it *it;
19820 {
19821 int extra_line_spacing = it->extra_line_spacing;
19822
19823 it->glyph_not_available_p = 0;
19824
19825 if (it->what == IT_CHARACTER)
19826 {
19827 XChar2b char2b;
19828 XFontStruct *font;
19829 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19830 XCharStruct *pcm;
19831 int font_not_found_p;
19832 struct font_info *font_info;
19833 int boff; /* baseline offset */
19834 /* We may change it->multibyte_p upon unibyte<->multibyte
19835 conversion. So, save the current value now and restore it
19836 later.
19837
19838 Note: It seems that we don't have to record multibyte_p in
19839 struct glyph because the character code itself tells if or
19840 not the character is multibyte. Thus, in the future, we must
19841 consider eliminating the field `multibyte_p' in the struct
19842 glyph. */
19843 int saved_multibyte_p = it->multibyte_p;
19844
19845 /* Maybe translate single-byte characters to multibyte, or the
19846 other way. */
19847 it->char_to_display = it->c;
19848 if (!ASCII_BYTE_P (it->c))
19849 {
19850 if (unibyte_display_via_language_environment
19851 && SINGLE_BYTE_CHAR_P (it->c)
19852 && (it->c >= 0240
19853 || !NILP (Vnonascii_translation_table)))
19854 {
19855 it->char_to_display = unibyte_char_to_multibyte (it->c);
19856 it->multibyte_p = 1;
19857 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19858 face = FACE_FROM_ID (it->f, it->face_id);
19859 }
19860 else if (!SINGLE_BYTE_CHAR_P (it->c)
19861 && !it->multibyte_p)
19862 {
19863 it->multibyte_p = 1;
19864 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19865 face = FACE_FROM_ID (it->f, it->face_id);
19866 }
19867 }
19868
19869 /* Get font to use. Encode IT->char_to_display. */
19870 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19871 &char2b, it->multibyte_p, 0);
19872 font = face->font;
19873
19874 /* When no suitable font found, use the default font. */
19875 font_not_found_p = font == NULL;
19876 if (font_not_found_p)
19877 {
19878 font = FRAME_FONT (it->f);
19879 boff = FRAME_BASELINE_OFFSET (it->f);
19880 font_info = NULL;
19881 }
19882 else
19883 {
19884 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19885 boff = font_info->baseline_offset;
19886 if (font_info->vertical_centering)
19887 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19888 }
19889
19890 if (it->char_to_display >= ' '
19891 && (!it->multibyte_p || it->char_to_display < 128))
19892 {
19893 /* Either unibyte or ASCII. */
19894 int stretched_p;
19895
19896 it->nglyphs = 1;
19897
19898 pcm = rif->per_char_metric (font, &char2b,
19899 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19900
19901 if (it->override_ascent >= 0)
19902 {
19903 it->ascent = it->override_ascent;
19904 it->descent = it->override_descent;
19905 boff = it->override_boff;
19906 }
19907 else
19908 {
19909 it->ascent = FONT_BASE (font) + boff;
19910 it->descent = FONT_DESCENT (font) - boff;
19911 }
19912
19913 if (pcm)
19914 {
19915 it->phys_ascent = pcm->ascent + boff;
19916 it->phys_descent = pcm->descent - boff;
19917 it->pixel_width = pcm->width;
19918 }
19919 else
19920 {
19921 it->glyph_not_available_p = 1;
19922 it->phys_ascent = it->ascent;
19923 it->phys_descent = it->descent;
19924 it->pixel_width = FONT_WIDTH (font);
19925 }
19926
19927 if (it->constrain_row_ascent_descent_p)
19928 {
19929 if (it->descent > it->max_descent)
19930 {
19931 it->ascent += it->descent - it->max_descent;
19932 it->descent = it->max_descent;
19933 }
19934 if (it->ascent > it->max_ascent)
19935 {
19936 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19937 it->ascent = it->max_ascent;
19938 }
19939 it->phys_ascent = min (it->phys_ascent, it->ascent);
19940 it->phys_descent = min (it->phys_descent, it->descent);
19941 extra_line_spacing = 0;
19942 }
19943
19944 /* If this is a space inside a region of text with
19945 `space-width' property, change its width. */
19946 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19947 if (stretched_p)
19948 it->pixel_width *= XFLOATINT (it->space_width);
19949
19950 /* If face has a box, add the box thickness to the character
19951 height. If character has a box line to the left and/or
19952 right, add the box line width to the character's width. */
19953 if (face->box != FACE_NO_BOX)
19954 {
19955 int thick = face->box_line_width;
19956
19957 if (thick > 0)
19958 {
19959 it->ascent += thick;
19960 it->descent += thick;
19961 }
19962 else
19963 thick = -thick;
19964
19965 if (it->start_of_box_run_p)
19966 it->pixel_width += thick;
19967 if (it->end_of_box_run_p)
19968 it->pixel_width += thick;
19969 }
19970
19971 /* If face has an overline, add the height of the overline
19972 (1 pixel) and a 1 pixel margin to the character height. */
19973 if (face->overline_p)
19974 it->ascent += 2;
19975
19976 if (it->constrain_row_ascent_descent_p)
19977 {
19978 if (it->ascent > it->max_ascent)
19979 it->ascent = it->max_ascent;
19980 if (it->descent > it->max_descent)
19981 it->descent = it->max_descent;
19982 }
19983
19984 take_vertical_position_into_account (it);
19985
19986 /* If we have to actually produce glyphs, do it. */
19987 if (it->glyph_row)
19988 {
19989 if (stretched_p)
19990 {
19991 /* Translate a space with a `space-width' property
19992 into a stretch glyph. */
19993 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19994 / FONT_HEIGHT (font));
19995 append_stretch_glyph (it, it->object, it->pixel_width,
19996 it->ascent + it->descent, ascent);
19997 }
19998 else
19999 append_glyph (it);
20000
20001 /* If characters with lbearing or rbearing are displayed
20002 in this line, record that fact in a flag of the
20003 glyph row. This is used to optimize X output code. */
20004 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20005 it->glyph_row->contains_overlapping_glyphs_p = 1;
20006 }
20007 }
20008 else if (it->char_to_display == '\n')
20009 {
20010 /* A newline has no width but we need the height of the line.
20011 But if previous part of the line set a height, don't
20012 increase that height */
20013
20014 Lisp_Object height;
20015 Lisp_Object total_height = Qnil;
20016
20017 it->override_ascent = -1;
20018 it->pixel_width = 0;
20019 it->nglyphs = 0;
20020
20021 height = get_line_height_property(it, Qline_height);
20022 /* Split (line-height total-height) list */
20023 if (CONSP (height)
20024 && CONSP (XCDR (height))
20025 && NILP (XCDR (XCDR (height))))
20026 {
20027 total_height = XCAR (XCDR (height));
20028 height = XCAR (height);
20029 }
20030 height = calc_line_height_property(it, height, font, boff, 1);
20031
20032 if (it->override_ascent >= 0)
20033 {
20034 it->ascent = it->override_ascent;
20035 it->descent = it->override_descent;
20036 boff = it->override_boff;
20037 }
20038 else
20039 {
20040 it->ascent = FONT_BASE (font) + boff;
20041 it->descent = FONT_DESCENT (font) - boff;
20042 }
20043
20044 if (EQ (height, Qt))
20045 {
20046 if (it->descent > it->max_descent)
20047 {
20048 it->ascent += it->descent - it->max_descent;
20049 it->descent = it->max_descent;
20050 }
20051 if (it->ascent > it->max_ascent)
20052 {
20053 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20054 it->ascent = it->max_ascent;
20055 }
20056 it->phys_ascent = min (it->phys_ascent, it->ascent);
20057 it->phys_descent = min (it->phys_descent, it->descent);
20058 it->constrain_row_ascent_descent_p = 1;
20059 extra_line_spacing = 0;
20060 }
20061 else
20062 {
20063 Lisp_Object spacing;
20064
20065 it->phys_ascent = it->ascent;
20066 it->phys_descent = it->descent;
20067
20068 if ((it->max_ascent > 0 || it->max_descent > 0)
20069 && face->box != FACE_NO_BOX
20070 && face->box_line_width > 0)
20071 {
20072 it->ascent += face->box_line_width;
20073 it->descent += face->box_line_width;
20074 }
20075 if (!NILP (height)
20076 && XINT (height) > it->ascent + it->descent)
20077 it->ascent = XINT (height) - it->descent;
20078
20079 if (!NILP (total_height))
20080 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20081 else
20082 {
20083 spacing = get_line_height_property(it, Qline_spacing);
20084 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20085 }
20086 if (INTEGERP (spacing))
20087 {
20088 extra_line_spacing = XINT (spacing);
20089 if (!NILP (total_height))
20090 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20091 }
20092 }
20093 }
20094 else if (it->char_to_display == '\t')
20095 {
20096 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20097 int x = it->current_x + it->continuation_lines_width;
20098 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20099
20100 /* If the distance from the current position to the next tab
20101 stop is less than a space character width, use the
20102 tab stop after that. */
20103 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20104 next_tab_x += tab_width;
20105
20106 it->pixel_width = next_tab_x - x;
20107 it->nglyphs = 1;
20108 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20109 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20110
20111 if (it->glyph_row)
20112 {
20113 append_stretch_glyph (it, it->object, it->pixel_width,
20114 it->ascent + it->descent, it->ascent);
20115 }
20116 }
20117 else
20118 {
20119 /* A multi-byte character. Assume that the display width of the
20120 character is the width of the character multiplied by the
20121 width of the font. */
20122
20123 /* If we found a font, this font should give us the right
20124 metrics. If we didn't find a font, use the frame's
20125 default font and calculate the width of the character
20126 from the charset width; this is what old redisplay code
20127 did. */
20128
20129 pcm = rif->per_char_metric (font, &char2b,
20130 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20131
20132 if (font_not_found_p || !pcm)
20133 {
20134 int charset = CHAR_CHARSET (it->char_to_display);
20135
20136 it->glyph_not_available_p = 1;
20137 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20138 * CHARSET_WIDTH (charset));
20139 it->phys_ascent = FONT_BASE (font) + boff;
20140 it->phys_descent = FONT_DESCENT (font) - boff;
20141 }
20142 else
20143 {
20144 it->pixel_width = pcm->width;
20145 it->phys_ascent = pcm->ascent + boff;
20146 it->phys_descent = pcm->descent - boff;
20147 if (it->glyph_row
20148 && (pcm->lbearing < 0
20149 || pcm->rbearing > pcm->width))
20150 it->glyph_row->contains_overlapping_glyphs_p = 1;
20151 }
20152 it->nglyphs = 1;
20153 it->ascent = FONT_BASE (font) + boff;
20154 it->descent = FONT_DESCENT (font) - boff;
20155 if (face->box != FACE_NO_BOX)
20156 {
20157 int thick = face->box_line_width;
20158
20159 if (thick > 0)
20160 {
20161 it->ascent += thick;
20162 it->descent += thick;
20163 }
20164 else
20165 thick = - thick;
20166
20167 if (it->start_of_box_run_p)
20168 it->pixel_width += thick;
20169 if (it->end_of_box_run_p)
20170 it->pixel_width += thick;
20171 }
20172
20173 /* If face has an overline, add the height of the overline
20174 (1 pixel) and a 1 pixel margin to the character height. */
20175 if (face->overline_p)
20176 it->ascent += 2;
20177
20178 take_vertical_position_into_account (it);
20179
20180 if (it->glyph_row)
20181 append_glyph (it);
20182 }
20183 it->multibyte_p = saved_multibyte_p;
20184 }
20185 else if (it->what == IT_COMPOSITION)
20186 {
20187 /* Note: A composition is represented as one glyph in the
20188 glyph matrix. There are no padding glyphs. */
20189 XChar2b char2b;
20190 XFontStruct *font;
20191 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20192 XCharStruct *pcm;
20193 int font_not_found_p;
20194 struct font_info *font_info;
20195 int boff; /* baseline offset */
20196 struct composition *cmp = composition_table[it->cmp_id];
20197
20198 /* Maybe translate single-byte characters to multibyte. */
20199 it->char_to_display = it->c;
20200 if (unibyte_display_via_language_environment
20201 && SINGLE_BYTE_CHAR_P (it->c)
20202 && (it->c >= 0240
20203 || (it->c >= 0200
20204 && !NILP (Vnonascii_translation_table))))
20205 {
20206 it->char_to_display = unibyte_char_to_multibyte (it->c);
20207 }
20208
20209 /* Get face and font to use. Encode IT->char_to_display. */
20210 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20211 face = FACE_FROM_ID (it->f, it->face_id);
20212 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20213 &char2b, it->multibyte_p, 0);
20214 font = face->font;
20215
20216 /* When no suitable font found, use the default font. */
20217 font_not_found_p = font == NULL;
20218 if (font_not_found_p)
20219 {
20220 font = FRAME_FONT (it->f);
20221 boff = FRAME_BASELINE_OFFSET (it->f);
20222 font_info = NULL;
20223 }
20224 else
20225 {
20226 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20227 boff = font_info->baseline_offset;
20228 if (font_info->vertical_centering)
20229 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20230 }
20231
20232 /* There are no padding glyphs, so there is only one glyph to
20233 produce for the composition. Important is that pixel_width,
20234 ascent and descent are the values of what is drawn by
20235 draw_glyphs (i.e. the values of the overall glyphs composed). */
20236 it->nglyphs = 1;
20237
20238 /* If we have not yet calculated pixel size data of glyphs of
20239 the composition for the current face font, calculate them
20240 now. Theoretically, we have to check all fonts for the
20241 glyphs, but that requires much time and memory space. So,
20242 here we check only the font of the first glyph. This leads
20243 to incorrect display very rarely, and C-l (recenter) can
20244 correct the display anyway. */
20245 if (cmp->font != (void *) font)
20246 {
20247 /* Ascent and descent of the font of the first character of
20248 this composition (adjusted by baseline offset). Ascent
20249 and descent of overall glyphs should not be less than
20250 them respectively. */
20251 int font_ascent = FONT_BASE (font) + boff;
20252 int font_descent = FONT_DESCENT (font) - boff;
20253 /* Bounding box of the overall glyphs. */
20254 int leftmost, rightmost, lowest, highest;
20255 int i, width, ascent, descent;
20256
20257 cmp->font = (void *) font;
20258
20259 /* Initialize the bounding box. */
20260 if (font_info
20261 && (pcm = rif->per_char_metric (font, &char2b,
20262 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20263 {
20264 width = pcm->width;
20265 ascent = pcm->ascent;
20266 descent = pcm->descent;
20267 }
20268 else
20269 {
20270 width = FONT_WIDTH (font);
20271 ascent = FONT_BASE (font);
20272 descent = FONT_DESCENT (font);
20273 }
20274
20275 rightmost = width;
20276 lowest = - descent + boff;
20277 highest = ascent + boff;
20278 leftmost = 0;
20279
20280 if (font_info
20281 && font_info->default_ascent
20282 && CHAR_TABLE_P (Vuse_default_ascent)
20283 && !NILP (Faref (Vuse_default_ascent,
20284 make_number (it->char_to_display))))
20285 highest = font_info->default_ascent + boff;
20286
20287 /* Draw the first glyph at the normal position. It may be
20288 shifted to right later if some other glyphs are drawn at
20289 the left. */
20290 cmp->offsets[0] = 0;
20291 cmp->offsets[1] = boff;
20292
20293 /* Set cmp->offsets for the remaining glyphs. */
20294 for (i = 1; i < cmp->glyph_len; i++)
20295 {
20296 int left, right, btm, top;
20297 int ch = COMPOSITION_GLYPH (cmp, i);
20298 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20299
20300 face = FACE_FROM_ID (it->f, face_id);
20301 get_char_face_and_encoding (it->f, ch, face->id,
20302 &char2b, it->multibyte_p, 0);
20303 font = face->font;
20304 if (font == NULL)
20305 {
20306 font = FRAME_FONT (it->f);
20307 boff = FRAME_BASELINE_OFFSET (it->f);
20308 font_info = NULL;
20309 }
20310 else
20311 {
20312 font_info
20313 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20314 boff = font_info->baseline_offset;
20315 if (font_info->vertical_centering)
20316 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20317 }
20318
20319 if (font_info
20320 && (pcm = rif->per_char_metric (font, &char2b,
20321 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20322 {
20323 width = pcm->width;
20324 ascent = pcm->ascent;
20325 descent = pcm->descent;
20326 }
20327 else
20328 {
20329 width = FONT_WIDTH (font);
20330 ascent = 1;
20331 descent = 0;
20332 }
20333
20334 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20335 {
20336 /* Relative composition with or without
20337 alternate chars. */
20338 left = (leftmost + rightmost - width) / 2;
20339 btm = - descent + boff;
20340 if (font_info && font_info->relative_compose
20341 && (! CHAR_TABLE_P (Vignore_relative_composition)
20342 || NILP (Faref (Vignore_relative_composition,
20343 make_number (ch)))))
20344 {
20345
20346 if (- descent >= font_info->relative_compose)
20347 /* One extra pixel between two glyphs. */
20348 btm = highest + 1;
20349 else if (ascent <= 0)
20350 /* One extra pixel between two glyphs. */
20351 btm = lowest - 1 - ascent - descent;
20352 }
20353 }
20354 else
20355 {
20356 /* A composition rule is specified by an integer
20357 value that encodes global and new reference
20358 points (GREF and NREF). GREF and NREF are
20359 specified by numbers as below:
20360
20361 0---1---2 -- ascent
20362 | |
20363 | |
20364 | |
20365 9--10--11 -- center
20366 | |
20367 ---3---4---5--- baseline
20368 | |
20369 6---7---8 -- descent
20370 */
20371 int rule = COMPOSITION_RULE (cmp, i);
20372 int gref, nref, grefx, grefy, nrefx, nrefy;
20373
20374 COMPOSITION_DECODE_RULE (rule, gref, nref);
20375 grefx = gref % 3, nrefx = nref % 3;
20376 grefy = gref / 3, nrefy = nref / 3;
20377
20378 left = (leftmost
20379 + grefx * (rightmost - leftmost) / 2
20380 - nrefx * width / 2);
20381 btm = ((grefy == 0 ? highest
20382 : grefy == 1 ? 0
20383 : grefy == 2 ? lowest
20384 : (highest + lowest) / 2)
20385 - (nrefy == 0 ? ascent + descent
20386 : nrefy == 1 ? descent - boff
20387 : nrefy == 2 ? 0
20388 : (ascent + descent) / 2));
20389 }
20390
20391 cmp->offsets[i * 2] = left;
20392 cmp->offsets[i * 2 + 1] = btm + descent;
20393
20394 /* Update the bounding box of the overall glyphs. */
20395 right = left + width;
20396 top = btm + descent + ascent;
20397 if (left < leftmost)
20398 leftmost = left;
20399 if (right > rightmost)
20400 rightmost = right;
20401 if (top > highest)
20402 highest = top;
20403 if (btm < lowest)
20404 lowest = btm;
20405 }
20406
20407 /* If there are glyphs whose x-offsets are negative,
20408 shift all glyphs to the right and make all x-offsets
20409 non-negative. */
20410 if (leftmost < 0)
20411 {
20412 for (i = 0; i < cmp->glyph_len; i++)
20413 cmp->offsets[i * 2] -= leftmost;
20414 rightmost -= leftmost;
20415 }
20416
20417 cmp->pixel_width = rightmost;
20418 cmp->ascent = highest;
20419 cmp->descent = - lowest;
20420 if (cmp->ascent < font_ascent)
20421 cmp->ascent = font_ascent;
20422 if (cmp->descent < font_descent)
20423 cmp->descent = font_descent;
20424 }
20425
20426 it->pixel_width = cmp->pixel_width;
20427 it->ascent = it->phys_ascent = cmp->ascent;
20428 it->descent = it->phys_descent = cmp->descent;
20429
20430 if (face->box != FACE_NO_BOX)
20431 {
20432 int thick = face->box_line_width;
20433
20434 if (thick > 0)
20435 {
20436 it->ascent += thick;
20437 it->descent += thick;
20438 }
20439 else
20440 thick = - thick;
20441
20442 if (it->start_of_box_run_p)
20443 it->pixel_width += thick;
20444 if (it->end_of_box_run_p)
20445 it->pixel_width += thick;
20446 }
20447
20448 /* If face has an overline, add the height of the overline
20449 (1 pixel) and a 1 pixel margin to the character height. */
20450 if (face->overline_p)
20451 it->ascent += 2;
20452
20453 take_vertical_position_into_account (it);
20454
20455 if (it->glyph_row)
20456 append_composite_glyph (it);
20457 }
20458 else if (it->what == IT_IMAGE)
20459 produce_image_glyph (it);
20460 else if (it->what == IT_STRETCH)
20461 produce_stretch_glyph (it);
20462
20463 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20464 because this isn't true for images with `:ascent 100'. */
20465 xassert (it->ascent >= 0 && it->descent >= 0);
20466 if (it->area == TEXT_AREA)
20467 it->current_x += it->pixel_width;
20468
20469 if (extra_line_spacing > 0)
20470 {
20471 it->descent += extra_line_spacing;
20472 if (extra_line_spacing > it->max_extra_line_spacing)
20473 it->max_extra_line_spacing = extra_line_spacing;
20474 }
20475
20476 it->max_ascent = max (it->max_ascent, it->ascent);
20477 it->max_descent = max (it->max_descent, it->descent);
20478 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20479 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20480 }
20481
20482 /* EXPORT for RIF:
20483 Output LEN glyphs starting at START at the nominal cursor position.
20484 Advance the nominal cursor over the text. The global variable
20485 updated_window contains the window being updated, updated_row is
20486 the glyph row being updated, and updated_area is the area of that
20487 row being updated. */
20488
20489 void
20490 x_write_glyphs (start, len)
20491 struct glyph *start;
20492 int len;
20493 {
20494 int x, hpos;
20495
20496 xassert (updated_window && updated_row);
20497 BLOCK_INPUT;
20498
20499 /* Write glyphs. */
20500
20501 hpos = start - updated_row->glyphs[updated_area];
20502 x = draw_glyphs (updated_window, output_cursor.x,
20503 updated_row, updated_area,
20504 hpos, hpos + len,
20505 DRAW_NORMAL_TEXT, 0);
20506
20507 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20508 if (updated_area == TEXT_AREA
20509 && updated_window->phys_cursor_on_p
20510 && updated_window->phys_cursor.vpos == output_cursor.vpos
20511 && updated_window->phys_cursor.hpos >= hpos
20512 && updated_window->phys_cursor.hpos < hpos + len)
20513 updated_window->phys_cursor_on_p = 0;
20514
20515 UNBLOCK_INPUT;
20516
20517 /* Advance the output cursor. */
20518 output_cursor.hpos += len;
20519 output_cursor.x = x;
20520 }
20521
20522
20523 /* EXPORT for RIF:
20524 Insert LEN glyphs from START at the nominal cursor position. */
20525
20526 void
20527 x_insert_glyphs (start, len)
20528 struct glyph *start;
20529 int len;
20530 {
20531 struct frame *f;
20532 struct window *w;
20533 int line_height, shift_by_width, shifted_region_width;
20534 struct glyph_row *row;
20535 struct glyph *glyph;
20536 int frame_x, frame_y, hpos;
20537
20538 xassert (updated_window && updated_row);
20539 BLOCK_INPUT;
20540 w = updated_window;
20541 f = XFRAME (WINDOW_FRAME (w));
20542
20543 /* Get the height of the line we are in. */
20544 row = updated_row;
20545 line_height = row->height;
20546
20547 /* Get the width of the glyphs to insert. */
20548 shift_by_width = 0;
20549 for (glyph = start; glyph < start + len; ++glyph)
20550 shift_by_width += glyph->pixel_width;
20551
20552 /* Get the width of the region to shift right. */
20553 shifted_region_width = (window_box_width (w, updated_area)
20554 - output_cursor.x
20555 - shift_by_width);
20556
20557 /* Shift right. */
20558 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20559 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20560
20561 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20562 line_height, shift_by_width);
20563
20564 /* Write the glyphs. */
20565 hpos = start - row->glyphs[updated_area];
20566 draw_glyphs (w, output_cursor.x, row, updated_area,
20567 hpos, hpos + len,
20568 DRAW_NORMAL_TEXT, 0);
20569
20570 /* Advance the output cursor. */
20571 output_cursor.hpos += len;
20572 output_cursor.x += shift_by_width;
20573 UNBLOCK_INPUT;
20574 }
20575
20576
20577 /* EXPORT for RIF:
20578 Erase the current text line from the nominal cursor position
20579 (inclusive) to pixel column TO_X (exclusive). The idea is that
20580 everything from TO_X onward is already erased.
20581
20582 TO_X is a pixel position relative to updated_area of
20583 updated_window. TO_X == -1 means clear to the end of this area. */
20584
20585 void
20586 x_clear_end_of_line (to_x)
20587 int to_x;
20588 {
20589 struct frame *f;
20590 struct window *w = updated_window;
20591 int max_x, min_y, max_y;
20592 int from_x, from_y, to_y;
20593
20594 xassert (updated_window && updated_row);
20595 f = XFRAME (w->frame);
20596
20597 if (updated_row->full_width_p)
20598 max_x = WINDOW_TOTAL_WIDTH (w);
20599 else
20600 max_x = window_box_width (w, updated_area);
20601 max_y = window_text_bottom_y (w);
20602
20603 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20604 of window. For TO_X > 0, truncate to end of drawing area. */
20605 if (to_x == 0)
20606 return;
20607 else if (to_x < 0)
20608 to_x = max_x;
20609 else
20610 to_x = min (to_x, max_x);
20611
20612 to_y = min (max_y, output_cursor.y + updated_row->height);
20613
20614 /* Notice if the cursor will be cleared by this operation. */
20615 if (!updated_row->full_width_p)
20616 notice_overwritten_cursor (w, updated_area,
20617 output_cursor.x, -1,
20618 updated_row->y,
20619 MATRIX_ROW_BOTTOM_Y (updated_row));
20620
20621 from_x = output_cursor.x;
20622
20623 /* Translate to frame coordinates. */
20624 if (updated_row->full_width_p)
20625 {
20626 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20627 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20628 }
20629 else
20630 {
20631 int area_left = window_box_left (w, updated_area);
20632 from_x += area_left;
20633 to_x += area_left;
20634 }
20635
20636 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20637 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20638 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20639
20640 /* Prevent inadvertently clearing to end of the X window. */
20641 if (to_x > from_x && to_y > from_y)
20642 {
20643 BLOCK_INPUT;
20644 rif->clear_frame_area (f, from_x, from_y,
20645 to_x - from_x, to_y - from_y);
20646 UNBLOCK_INPUT;
20647 }
20648 }
20649
20650 #endif /* HAVE_WINDOW_SYSTEM */
20651
20652
20653 \f
20654 /***********************************************************************
20655 Cursor types
20656 ***********************************************************************/
20657
20658 /* Value is the internal representation of the specified cursor type
20659 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20660 of the bar cursor. */
20661
20662 static enum text_cursor_kinds
20663 get_specified_cursor_type (arg, width)
20664 Lisp_Object arg;
20665 int *width;
20666 {
20667 enum text_cursor_kinds type;
20668
20669 if (NILP (arg))
20670 return NO_CURSOR;
20671
20672 if (EQ (arg, Qbox))
20673 return FILLED_BOX_CURSOR;
20674
20675 if (EQ (arg, Qhollow))
20676 return HOLLOW_BOX_CURSOR;
20677
20678 if (EQ (arg, Qbar))
20679 {
20680 *width = 2;
20681 return BAR_CURSOR;
20682 }
20683
20684 if (CONSP (arg)
20685 && EQ (XCAR (arg), Qbar)
20686 && INTEGERP (XCDR (arg))
20687 && XINT (XCDR (arg)) >= 0)
20688 {
20689 *width = XINT (XCDR (arg));
20690 return BAR_CURSOR;
20691 }
20692
20693 if (EQ (arg, Qhbar))
20694 {
20695 *width = 2;
20696 return HBAR_CURSOR;
20697 }
20698
20699 if (CONSP (arg)
20700 && EQ (XCAR (arg), Qhbar)
20701 && INTEGERP (XCDR (arg))
20702 && XINT (XCDR (arg)) >= 0)
20703 {
20704 *width = XINT (XCDR (arg));
20705 return HBAR_CURSOR;
20706 }
20707
20708 /* Treat anything unknown as "hollow box cursor".
20709 It was bad to signal an error; people have trouble fixing
20710 .Xdefaults with Emacs, when it has something bad in it. */
20711 type = HOLLOW_BOX_CURSOR;
20712
20713 return type;
20714 }
20715
20716 /* Set the default cursor types for specified frame. */
20717 void
20718 set_frame_cursor_types (f, arg)
20719 struct frame *f;
20720 Lisp_Object arg;
20721 {
20722 int width;
20723 Lisp_Object tem;
20724
20725 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20726 FRAME_CURSOR_WIDTH (f) = width;
20727
20728 /* By default, set up the blink-off state depending on the on-state. */
20729
20730 tem = Fassoc (arg, Vblink_cursor_alist);
20731 if (!NILP (tem))
20732 {
20733 FRAME_BLINK_OFF_CURSOR (f)
20734 = get_specified_cursor_type (XCDR (tem), &width);
20735 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20736 }
20737 else
20738 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20739 }
20740
20741
20742 /* Return the cursor we want to be displayed in window W. Return
20743 width of bar/hbar cursor through WIDTH arg. Return with
20744 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20745 (i.e. if the `system caret' should track this cursor).
20746
20747 In a mini-buffer window, we want the cursor only to appear if we
20748 are reading input from this window. For the selected window, we
20749 want the cursor type given by the frame parameter or buffer local
20750 setting of cursor-type. If explicitly marked off, draw no cursor.
20751 In all other cases, we want a hollow box cursor. */
20752
20753 static enum text_cursor_kinds
20754 get_window_cursor_type (w, glyph, width, active_cursor)
20755 struct window *w;
20756 struct glyph *glyph;
20757 int *width;
20758 int *active_cursor;
20759 {
20760 struct frame *f = XFRAME (w->frame);
20761 struct buffer *b = XBUFFER (w->buffer);
20762 int cursor_type = DEFAULT_CURSOR;
20763 Lisp_Object alt_cursor;
20764 int non_selected = 0;
20765
20766 *active_cursor = 1;
20767
20768 /* Echo area */
20769 if (cursor_in_echo_area
20770 && FRAME_HAS_MINIBUF_P (f)
20771 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20772 {
20773 if (w == XWINDOW (echo_area_window))
20774 {
20775 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
20776 {
20777 *width = FRAME_CURSOR_WIDTH (f);
20778 return FRAME_DESIRED_CURSOR (f);
20779 }
20780 else
20781 return get_specified_cursor_type (b->cursor_type, width);
20782 }
20783
20784 *active_cursor = 0;
20785 non_selected = 1;
20786 }
20787
20788 /* Nonselected window or nonselected frame. */
20789 else if (w != XWINDOW (f->selected_window)
20790 #ifdef HAVE_WINDOW_SYSTEM
20791 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20792 #endif
20793 )
20794 {
20795 *active_cursor = 0;
20796
20797 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20798 return NO_CURSOR;
20799
20800 non_selected = 1;
20801 }
20802
20803 /* Never display a cursor in a window in which cursor-type is nil. */
20804 if (NILP (b->cursor_type))
20805 return NO_CURSOR;
20806
20807 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20808 if (non_selected)
20809 {
20810 alt_cursor = b->cursor_in_non_selected_windows;
20811 return get_specified_cursor_type (alt_cursor, width);
20812 }
20813
20814 /* Get the normal cursor type for this window. */
20815 if (EQ (b->cursor_type, Qt))
20816 {
20817 cursor_type = FRAME_DESIRED_CURSOR (f);
20818 *width = FRAME_CURSOR_WIDTH (f);
20819 }
20820 else
20821 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20822
20823 /* Use normal cursor if not blinked off. */
20824 if (!w->cursor_off_p)
20825 {
20826 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20827 if (cursor_type == FILLED_BOX_CURSOR)
20828 cursor_type = HOLLOW_BOX_CURSOR;
20829 }
20830 return cursor_type;
20831 }
20832
20833 /* Cursor is blinked off, so determine how to "toggle" it. */
20834
20835 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20836 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20837 return get_specified_cursor_type (XCDR (alt_cursor), width);
20838
20839 /* Then see if frame has specified a specific blink off cursor type. */
20840 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20841 {
20842 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20843 return FRAME_BLINK_OFF_CURSOR (f);
20844 }
20845
20846 #if 0
20847 /* Some people liked having a permanently visible blinking cursor,
20848 while others had very strong opinions against it. So it was
20849 decided to remove it. KFS 2003-09-03 */
20850
20851 /* Finally perform built-in cursor blinking:
20852 filled box <-> hollow box
20853 wide [h]bar <-> narrow [h]bar
20854 narrow [h]bar <-> no cursor
20855 other type <-> no cursor */
20856
20857 if (cursor_type == FILLED_BOX_CURSOR)
20858 return HOLLOW_BOX_CURSOR;
20859
20860 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20861 {
20862 *width = 1;
20863 return cursor_type;
20864 }
20865 #endif
20866
20867 return NO_CURSOR;
20868 }
20869
20870
20871 #ifdef HAVE_WINDOW_SYSTEM
20872
20873 /* Notice when the text cursor of window W has been completely
20874 overwritten by a drawing operation that outputs glyphs in AREA
20875 starting at X0 and ending at X1 in the line starting at Y0 and
20876 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20877 the rest of the line after X0 has been written. Y coordinates
20878 are window-relative. */
20879
20880 static void
20881 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20882 struct window *w;
20883 enum glyph_row_area area;
20884 int x0, y0, x1, y1;
20885 {
20886 int cx0, cx1, cy0, cy1;
20887 struct glyph_row *row;
20888
20889 if (!w->phys_cursor_on_p)
20890 return;
20891 if (area != TEXT_AREA)
20892 return;
20893
20894 if (w->phys_cursor.vpos < 0
20895 || w->phys_cursor.vpos >= w->current_matrix->nrows
20896 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20897 !(row->enabled_p && row->displays_text_p)))
20898 return;
20899
20900 if (row->cursor_in_fringe_p)
20901 {
20902 row->cursor_in_fringe_p = 0;
20903 draw_fringe_bitmap (w, row, 0);
20904 w->phys_cursor_on_p = 0;
20905 return;
20906 }
20907
20908 cx0 = w->phys_cursor.x;
20909 cx1 = cx0 + w->phys_cursor_width;
20910 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20911 return;
20912
20913 /* The cursor image will be completely removed from the
20914 screen if the output area intersects the cursor area in
20915 y-direction. When we draw in [y0 y1[, and some part of
20916 the cursor is at y < y0, that part must have been drawn
20917 before. When scrolling, the cursor is erased before
20918 actually scrolling, so we don't come here. When not
20919 scrolling, the rows above the old cursor row must have
20920 changed, and in this case these rows must have written
20921 over the cursor image.
20922
20923 Likewise if part of the cursor is below y1, with the
20924 exception of the cursor being in the first blank row at
20925 the buffer and window end because update_text_area
20926 doesn't draw that row. (Except when it does, but
20927 that's handled in update_text_area.) */
20928
20929 cy0 = w->phys_cursor.y;
20930 cy1 = cy0 + w->phys_cursor_height;
20931 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20932 return;
20933
20934 w->phys_cursor_on_p = 0;
20935 }
20936
20937 #endif /* HAVE_WINDOW_SYSTEM */
20938
20939 \f
20940 /************************************************************************
20941 Mouse Face
20942 ************************************************************************/
20943
20944 #ifdef HAVE_WINDOW_SYSTEM
20945
20946 /* EXPORT for RIF:
20947 Fix the display of area AREA of overlapping row ROW in window W
20948 with respect to the overlapping part OVERLAPS. */
20949
20950 void
20951 x_fix_overlapping_area (w, row, area, overlaps)
20952 struct window *w;
20953 struct glyph_row *row;
20954 enum glyph_row_area area;
20955 int overlaps;
20956 {
20957 int i, x;
20958
20959 BLOCK_INPUT;
20960
20961 x = 0;
20962 for (i = 0; i < row->used[area];)
20963 {
20964 if (row->glyphs[area][i].overlaps_vertically_p)
20965 {
20966 int start = i, start_x = x;
20967
20968 do
20969 {
20970 x += row->glyphs[area][i].pixel_width;
20971 ++i;
20972 }
20973 while (i < row->used[area]
20974 && row->glyphs[area][i].overlaps_vertically_p);
20975
20976 draw_glyphs (w, start_x, row, area,
20977 start, i,
20978 DRAW_NORMAL_TEXT, overlaps);
20979 }
20980 else
20981 {
20982 x += row->glyphs[area][i].pixel_width;
20983 ++i;
20984 }
20985 }
20986
20987 UNBLOCK_INPUT;
20988 }
20989
20990
20991 /* EXPORT:
20992 Draw the cursor glyph of window W in glyph row ROW. See the
20993 comment of draw_glyphs for the meaning of HL. */
20994
20995 void
20996 draw_phys_cursor_glyph (w, row, hl)
20997 struct window *w;
20998 struct glyph_row *row;
20999 enum draw_glyphs_face hl;
21000 {
21001 /* If cursor hpos is out of bounds, don't draw garbage. This can
21002 happen in mini-buffer windows when switching between echo area
21003 glyphs and mini-buffer. */
21004 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21005 {
21006 int on_p = w->phys_cursor_on_p;
21007 int x1;
21008 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21009 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21010 hl, 0);
21011 w->phys_cursor_on_p = on_p;
21012
21013 if (hl == DRAW_CURSOR)
21014 w->phys_cursor_width = x1 - w->phys_cursor.x;
21015 /* When we erase the cursor, and ROW is overlapped by other
21016 rows, make sure that these overlapping parts of other rows
21017 are redrawn. */
21018 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21019 {
21020 w->phys_cursor_width = x1 - w->phys_cursor.x;
21021
21022 if (row > w->current_matrix->rows
21023 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21024 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21025 OVERLAPS_ERASED_CURSOR);
21026
21027 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21028 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21029 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21030 OVERLAPS_ERASED_CURSOR);
21031 }
21032 }
21033 }
21034
21035
21036 /* EXPORT:
21037 Erase the image of a cursor of window W from the screen. */
21038
21039 void
21040 erase_phys_cursor (w)
21041 struct window *w;
21042 {
21043 struct frame *f = XFRAME (w->frame);
21044 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21045 int hpos = w->phys_cursor.hpos;
21046 int vpos = w->phys_cursor.vpos;
21047 int mouse_face_here_p = 0;
21048 struct glyph_matrix *active_glyphs = w->current_matrix;
21049 struct glyph_row *cursor_row;
21050 struct glyph *cursor_glyph;
21051 enum draw_glyphs_face hl;
21052
21053 /* No cursor displayed or row invalidated => nothing to do on the
21054 screen. */
21055 if (w->phys_cursor_type == NO_CURSOR)
21056 goto mark_cursor_off;
21057
21058 /* VPOS >= active_glyphs->nrows means that window has been resized.
21059 Don't bother to erase the cursor. */
21060 if (vpos >= active_glyphs->nrows)
21061 goto mark_cursor_off;
21062
21063 /* If row containing cursor is marked invalid, there is nothing we
21064 can do. */
21065 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21066 if (!cursor_row->enabled_p)
21067 goto mark_cursor_off;
21068
21069 /* If line spacing is > 0, old cursor may only be partially visible in
21070 window after split-window. So adjust visible height. */
21071 cursor_row->visible_height = min (cursor_row->visible_height,
21072 window_text_bottom_y (w) - cursor_row->y);
21073
21074 /* If row is completely invisible, don't attempt to delete a cursor which
21075 isn't there. This can happen if cursor is at top of a window, and
21076 we switch to a buffer with a header line in that window. */
21077 if (cursor_row->visible_height <= 0)
21078 goto mark_cursor_off;
21079
21080 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21081 if (cursor_row->cursor_in_fringe_p)
21082 {
21083 cursor_row->cursor_in_fringe_p = 0;
21084 draw_fringe_bitmap (w, cursor_row, 0);
21085 goto mark_cursor_off;
21086 }
21087
21088 /* This can happen when the new row is shorter than the old one.
21089 In this case, either draw_glyphs or clear_end_of_line
21090 should have cleared the cursor. Note that we wouldn't be
21091 able to erase the cursor in this case because we don't have a
21092 cursor glyph at hand. */
21093 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21094 goto mark_cursor_off;
21095
21096 /* If the cursor is in the mouse face area, redisplay that when
21097 we clear the cursor. */
21098 if (! NILP (dpyinfo->mouse_face_window)
21099 && w == XWINDOW (dpyinfo->mouse_face_window)
21100 && (vpos > dpyinfo->mouse_face_beg_row
21101 || (vpos == dpyinfo->mouse_face_beg_row
21102 && hpos >= dpyinfo->mouse_face_beg_col))
21103 && (vpos < dpyinfo->mouse_face_end_row
21104 || (vpos == dpyinfo->mouse_face_end_row
21105 && hpos < dpyinfo->mouse_face_end_col))
21106 /* Don't redraw the cursor's spot in mouse face if it is at the
21107 end of a line (on a newline). The cursor appears there, but
21108 mouse highlighting does not. */
21109 && cursor_row->used[TEXT_AREA] > hpos)
21110 mouse_face_here_p = 1;
21111
21112 /* Maybe clear the display under the cursor. */
21113 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21114 {
21115 int x, y;
21116 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21117 int width;
21118
21119 cursor_glyph = get_phys_cursor_glyph (w);
21120 if (cursor_glyph == NULL)
21121 goto mark_cursor_off;
21122
21123 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
21124 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21125 width = min (cursor_glyph->pixel_width,
21126 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
21127
21128 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21129 }
21130
21131 /* Erase the cursor by redrawing the character underneath it. */
21132 if (mouse_face_here_p)
21133 hl = DRAW_MOUSE_FACE;
21134 else
21135 hl = DRAW_NORMAL_TEXT;
21136 draw_phys_cursor_glyph (w, cursor_row, hl);
21137
21138 mark_cursor_off:
21139 w->phys_cursor_on_p = 0;
21140 w->phys_cursor_type = NO_CURSOR;
21141 }
21142
21143
21144 /* EXPORT:
21145 Display or clear cursor of window W. If ON is zero, clear the
21146 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21147 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21148
21149 void
21150 display_and_set_cursor (w, on, hpos, vpos, x, y)
21151 struct window *w;
21152 int on, hpos, vpos, x, y;
21153 {
21154 struct frame *f = XFRAME (w->frame);
21155 int new_cursor_type;
21156 int new_cursor_width;
21157 int active_cursor;
21158 struct glyph_row *glyph_row;
21159 struct glyph *glyph;
21160
21161 /* This is pointless on invisible frames, and dangerous on garbaged
21162 windows and frames; in the latter case, the frame or window may
21163 be in the midst of changing its size, and x and y may be off the
21164 window. */
21165 if (! FRAME_VISIBLE_P (f)
21166 || FRAME_GARBAGED_P (f)
21167 || vpos >= w->current_matrix->nrows
21168 || hpos >= w->current_matrix->matrix_w)
21169 return;
21170
21171 /* If cursor is off and we want it off, return quickly. */
21172 if (!on && !w->phys_cursor_on_p)
21173 return;
21174
21175 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21176 /* If cursor row is not enabled, we don't really know where to
21177 display the cursor. */
21178 if (!glyph_row->enabled_p)
21179 {
21180 w->phys_cursor_on_p = 0;
21181 return;
21182 }
21183
21184 glyph = NULL;
21185 if (!glyph_row->exact_window_width_line_p
21186 || hpos < glyph_row->used[TEXT_AREA])
21187 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21188
21189 xassert (interrupt_input_blocked);
21190
21191 /* Set new_cursor_type to the cursor we want to be displayed. */
21192 new_cursor_type = get_window_cursor_type (w, glyph,
21193 &new_cursor_width, &active_cursor);
21194
21195 /* If cursor is currently being shown and we don't want it to be or
21196 it is in the wrong place, or the cursor type is not what we want,
21197 erase it. */
21198 if (w->phys_cursor_on_p
21199 && (!on
21200 || w->phys_cursor.x != x
21201 || w->phys_cursor.y != y
21202 || new_cursor_type != w->phys_cursor_type
21203 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21204 && new_cursor_width != w->phys_cursor_width)))
21205 erase_phys_cursor (w);
21206
21207 /* Don't check phys_cursor_on_p here because that flag is only set
21208 to zero in some cases where we know that the cursor has been
21209 completely erased, to avoid the extra work of erasing the cursor
21210 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21211 still not be visible, or it has only been partly erased. */
21212 if (on)
21213 {
21214 w->phys_cursor_ascent = glyph_row->ascent;
21215 w->phys_cursor_height = glyph_row->height;
21216
21217 /* Set phys_cursor_.* before x_draw_.* is called because some
21218 of them may need the information. */
21219 w->phys_cursor.x = x;
21220 w->phys_cursor.y = glyph_row->y;
21221 w->phys_cursor.hpos = hpos;
21222 w->phys_cursor.vpos = vpos;
21223 }
21224
21225 rif->draw_window_cursor (w, glyph_row, x, y,
21226 new_cursor_type, new_cursor_width,
21227 on, active_cursor);
21228 }
21229
21230
21231 /* Switch the display of W's cursor on or off, according to the value
21232 of ON. */
21233
21234 static void
21235 update_window_cursor (w, on)
21236 struct window *w;
21237 int on;
21238 {
21239 /* Don't update cursor in windows whose frame is in the process
21240 of being deleted. */
21241 if (w->current_matrix)
21242 {
21243 BLOCK_INPUT;
21244 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21245 w->phys_cursor.x, w->phys_cursor.y);
21246 UNBLOCK_INPUT;
21247 }
21248 }
21249
21250
21251 /* Call update_window_cursor with parameter ON_P on all leaf windows
21252 in the window tree rooted at W. */
21253
21254 static void
21255 update_cursor_in_window_tree (w, on_p)
21256 struct window *w;
21257 int on_p;
21258 {
21259 while (w)
21260 {
21261 if (!NILP (w->hchild))
21262 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21263 else if (!NILP (w->vchild))
21264 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21265 else
21266 update_window_cursor (w, on_p);
21267
21268 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21269 }
21270 }
21271
21272
21273 /* EXPORT:
21274 Display the cursor on window W, or clear it, according to ON_P.
21275 Don't change the cursor's position. */
21276
21277 void
21278 x_update_cursor (f, on_p)
21279 struct frame *f;
21280 int on_p;
21281 {
21282 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21283 }
21284
21285
21286 /* EXPORT:
21287 Clear the cursor of window W to background color, and mark the
21288 cursor as not shown. This is used when the text where the cursor
21289 is is about to be rewritten. */
21290
21291 void
21292 x_clear_cursor (w)
21293 struct window *w;
21294 {
21295 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21296 update_window_cursor (w, 0);
21297 }
21298
21299
21300 /* EXPORT:
21301 Display the active region described by mouse_face_* according to DRAW. */
21302
21303 void
21304 show_mouse_face (dpyinfo, draw)
21305 Display_Info *dpyinfo;
21306 enum draw_glyphs_face draw;
21307 {
21308 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21309 struct frame *f = XFRAME (WINDOW_FRAME (w));
21310
21311 if (/* If window is in the process of being destroyed, don't bother
21312 to do anything. */
21313 w->current_matrix != NULL
21314 /* Don't update mouse highlight if hidden */
21315 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21316 /* Recognize when we are called to operate on rows that don't exist
21317 anymore. This can happen when a window is split. */
21318 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21319 {
21320 int phys_cursor_on_p = w->phys_cursor_on_p;
21321 struct glyph_row *row, *first, *last;
21322
21323 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21324 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21325
21326 for (row = first; row <= last && row->enabled_p; ++row)
21327 {
21328 int start_hpos, end_hpos, start_x;
21329
21330 /* For all but the first row, the highlight starts at column 0. */
21331 if (row == first)
21332 {
21333 start_hpos = dpyinfo->mouse_face_beg_col;
21334 start_x = dpyinfo->mouse_face_beg_x;
21335 }
21336 else
21337 {
21338 start_hpos = 0;
21339 start_x = 0;
21340 }
21341
21342 if (row == last)
21343 end_hpos = dpyinfo->mouse_face_end_col;
21344 else
21345 {
21346 end_hpos = row->used[TEXT_AREA];
21347 if (draw == DRAW_NORMAL_TEXT)
21348 row->fill_line_p = 1; /* Clear to end of line */
21349 }
21350
21351 if (end_hpos > start_hpos)
21352 {
21353 draw_glyphs (w, start_x, row, TEXT_AREA,
21354 start_hpos, end_hpos,
21355 draw, 0);
21356
21357 row->mouse_face_p
21358 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21359 }
21360 }
21361
21362 /* When we've written over the cursor, arrange for it to
21363 be displayed again. */
21364 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21365 {
21366 BLOCK_INPUT;
21367 display_and_set_cursor (w, 1,
21368 w->phys_cursor.hpos, w->phys_cursor.vpos,
21369 w->phys_cursor.x, w->phys_cursor.y);
21370 UNBLOCK_INPUT;
21371 }
21372 }
21373
21374 /* Change the mouse cursor. */
21375 if (draw == DRAW_NORMAL_TEXT)
21376 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21377 else if (draw == DRAW_MOUSE_FACE)
21378 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21379 else
21380 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21381 }
21382
21383 /* EXPORT:
21384 Clear out the mouse-highlighted active region.
21385 Redraw it un-highlighted first. Value is non-zero if mouse
21386 face was actually drawn unhighlighted. */
21387
21388 int
21389 clear_mouse_face (dpyinfo)
21390 Display_Info *dpyinfo;
21391 {
21392 int cleared = 0;
21393
21394 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21395 {
21396 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21397 cleared = 1;
21398 }
21399
21400 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21401 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21402 dpyinfo->mouse_face_window = Qnil;
21403 dpyinfo->mouse_face_overlay = Qnil;
21404 return cleared;
21405 }
21406
21407
21408 /* EXPORT:
21409 Non-zero if physical cursor of window W is within mouse face. */
21410
21411 int
21412 cursor_in_mouse_face_p (w)
21413 struct window *w;
21414 {
21415 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21416 int in_mouse_face = 0;
21417
21418 if (WINDOWP (dpyinfo->mouse_face_window)
21419 && XWINDOW (dpyinfo->mouse_face_window) == w)
21420 {
21421 int hpos = w->phys_cursor.hpos;
21422 int vpos = w->phys_cursor.vpos;
21423
21424 if (vpos >= dpyinfo->mouse_face_beg_row
21425 && vpos <= dpyinfo->mouse_face_end_row
21426 && (vpos > dpyinfo->mouse_face_beg_row
21427 || hpos >= dpyinfo->mouse_face_beg_col)
21428 && (vpos < dpyinfo->mouse_face_end_row
21429 || hpos < dpyinfo->mouse_face_end_col
21430 || dpyinfo->mouse_face_past_end))
21431 in_mouse_face = 1;
21432 }
21433
21434 return in_mouse_face;
21435 }
21436
21437
21438
21439 \f
21440 /* Find the glyph matrix position of buffer position CHARPOS in window
21441 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21442 current glyphs must be up to date. If CHARPOS is above window
21443 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21444 of last line in W. In the row containing CHARPOS, stop before glyphs
21445 having STOP as object. */
21446
21447 #if 1 /* This is a version of fast_find_position that's more correct
21448 in the presence of hscrolling, for example. I didn't install
21449 it right away because the problem fixed is minor, it failed
21450 in 20.x as well, and I think it's too risky to install
21451 so near the release of 21.1. 2001-09-25 gerd. */
21452
21453 static int
21454 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21455 struct window *w;
21456 int charpos;
21457 int *hpos, *vpos, *x, *y;
21458 Lisp_Object stop;
21459 {
21460 struct glyph_row *row, *first;
21461 struct glyph *glyph, *end;
21462 int past_end = 0;
21463
21464 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21465 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21466 {
21467 *x = first->x;
21468 *y = first->y;
21469 *hpos = 0;
21470 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21471 return 1;
21472 }
21473
21474 row = row_containing_pos (w, charpos, first, NULL, 0);
21475 if (row == NULL)
21476 {
21477 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21478 past_end = 1;
21479 }
21480
21481 /* If whole rows or last part of a row came from a display overlay,
21482 row_containing_pos will skip over such rows because their end pos
21483 equals the start pos of the overlay or interval.
21484
21485 Move back if we have a STOP object and previous row's
21486 end glyph came from STOP. */
21487 if (!NILP (stop))
21488 {
21489 struct glyph_row *prev;
21490 while ((prev = row - 1, prev >= first)
21491 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21492 && prev->used[TEXT_AREA] > 0)
21493 {
21494 struct glyph *beg = prev->glyphs[TEXT_AREA];
21495 glyph = beg + prev->used[TEXT_AREA];
21496 while (--glyph >= beg
21497 && INTEGERP (glyph->object));
21498 if (glyph < beg
21499 || !EQ (stop, glyph->object))
21500 break;
21501 row = prev;
21502 }
21503 }
21504
21505 *x = row->x;
21506 *y = row->y;
21507 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21508
21509 glyph = row->glyphs[TEXT_AREA];
21510 end = glyph + row->used[TEXT_AREA];
21511
21512 /* Skip over glyphs not having an object at the start of the row.
21513 These are special glyphs like truncation marks on terminal
21514 frames. */
21515 if (row->displays_text_p)
21516 while (glyph < end
21517 && INTEGERP (glyph->object)
21518 && !EQ (stop, glyph->object)
21519 && glyph->charpos < 0)
21520 {
21521 *x += glyph->pixel_width;
21522 ++glyph;
21523 }
21524
21525 while (glyph < end
21526 && !INTEGERP (glyph->object)
21527 && !EQ (stop, glyph->object)
21528 && (!BUFFERP (glyph->object)
21529 || glyph->charpos < charpos))
21530 {
21531 *x += glyph->pixel_width;
21532 ++glyph;
21533 }
21534
21535 *hpos = glyph - row->glyphs[TEXT_AREA];
21536 return !past_end;
21537 }
21538
21539 #else /* not 1 */
21540
21541 static int
21542 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21543 struct window *w;
21544 int pos;
21545 int *hpos, *vpos, *x, *y;
21546 Lisp_Object stop;
21547 {
21548 int i;
21549 int lastcol;
21550 int maybe_next_line_p = 0;
21551 int line_start_position;
21552 int yb = window_text_bottom_y (w);
21553 struct glyph_row *row, *best_row;
21554 int row_vpos, best_row_vpos;
21555 int current_x;
21556
21557 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21558 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21559
21560 while (row->y < yb)
21561 {
21562 if (row->used[TEXT_AREA])
21563 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21564 else
21565 line_start_position = 0;
21566
21567 if (line_start_position > pos)
21568 break;
21569 /* If the position sought is the end of the buffer,
21570 don't include the blank lines at the bottom of the window. */
21571 else if (line_start_position == pos
21572 && pos == BUF_ZV (XBUFFER (w->buffer)))
21573 {
21574 maybe_next_line_p = 1;
21575 break;
21576 }
21577 else if (line_start_position > 0)
21578 {
21579 best_row = row;
21580 best_row_vpos = row_vpos;
21581 }
21582
21583 if (row->y + row->height >= yb)
21584 break;
21585
21586 ++row;
21587 ++row_vpos;
21588 }
21589
21590 /* Find the right column within BEST_ROW. */
21591 lastcol = 0;
21592 current_x = best_row->x;
21593 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21594 {
21595 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21596 int charpos = glyph->charpos;
21597
21598 if (BUFFERP (glyph->object))
21599 {
21600 if (charpos == pos)
21601 {
21602 *hpos = i;
21603 *vpos = best_row_vpos;
21604 *x = current_x;
21605 *y = best_row->y;
21606 return 1;
21607 }
21608 else if (charpos > pos)
21609 break;
21610 }
21611 else if (EQ (glyph->object, stop))
21612 break;
21613
21614 if (charpos > 0)
21615 lastcol = i;
21616 current_x += glyph->pixel_width;
21617 }
21618
21619 /* If we're looking for the end of the buffer,
21620 and we didn't find it in the line we scanned,
21621 use the start of the following line. */
21622 if (maybe_next_line_p)
21623 {
21624 ++best_row;
21625 ++best_row_vpos;
21626 lastcol = 0;
21627 current_x = best_row->x;
21628 }
21629
21630 *vpos = best_row_vpos;
21631 *hpos = lastcol + 1;
21632 *x = current_x;
21633 *y = best_row->y;
21634 return 0;
21635 }
21636
21637 #endif /* not 1 */
21638
21639
21640 /* Find the position of the glyph for position POS in OBJECT in
21641 window W's current matrix, and return in *X, *Y the pixel
21642 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21643
21644 RIGHT_P non-zero means return the position of the right edge of the
21645 glyph, RIGHT_P zero means return the left edge position.
21646
21647 If no glyph for POS exists in the matrix, return the position of
21648 the glyph with the next smaller position that is in the matrix, if
21649 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21650 exists in the matrix, return the position of the glyph with the
21651 next larger position in OBJECT.
21652
21653 Value is non-zero if a glyph was found. */
21654
21655 static int
21656 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21657 struct window *w;
21658 int pos;
21659 Lisp_Object object;
21660 int *hpos, *vpos, *x, *y;
21661 int right_p;
21662 {
21663 int yb = window_text_bottom_y (w);
21664 struct glyph_row *r;
21665 struct glyph *best_glyph = NULL;
21666 struct glyph_row *best_row = NULL;
21667 int best_x = 0;
21668
21669 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21670 r->enabled_p && r->y < yb;
21671 ++r)
21672 {
21673 struct glyph *g = r->glyphs[TEXT_AREA];
21674 struct glyph *e = g + r->used[TEXT_AREA];
21675 int gx;
21676
21677 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21678 if (EQ (g->object, object))
21679 {
21680 if (g->charpos == pos)
21681 {
21682 best_glyph = g;
21683 best_x = gx;
21684 best_row = r;
21685 goto found;
21686 }
21687 else if (best_glyph == NULL
21688 || ((abs (g->charpos - pos)
21689 < abs (best_glyph->charpos - pos))
21690 && (right_p
21691 ? g->charpos < pos
21692 : g->charpos > pos)))
21693 {
21694 best_glyph = g;
21695 best_x = gx;
21696 best_row = r;
21697 }
21698 }
21699 }
21700
21701 found:
21702
21703 if (best_glyph)
21704 {
21705 *x = best_x;
21706 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21707
21708 if (right_p)
21709 {
21710 *x += best_glyph->pixel_width;
21711 ++*hpos;
21712 }
21713
21714 *y = best_row->y;
21715 *vpos = best_row - w->current_matrix->rows;
21716 }
21717
21718 return best_glyph != NULL;
21719 }
21720
21721
21722 /* See if position X, Y is within a hot-spot of an image. */
21723
21724 static int
21725 on_hot_spot_p (hot_spot, x, y)
21726 Lisp_Object hot_spot;
21727 int x, y;
21728 {
21729 if (!CONSP (hot_spot))
21730 return 0;
21731
21732 if (EQ (XCAR (hot_spot), Qrect))
21733 {
21734 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21735 Lisp_Object rect = XCDR (hot_spot);
21736 Lisp_Object tem;
21737 if (!CONSP (rect))
21738 return 0;
21739 if (!CONSP (XCAR (rect)))
21740 return 0;
21741 if (!CONSP (XCDR (rect)))
21742 return 0;
21743 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21744 return 0;
21745 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21746 return 0;
21747 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21748 return 0;
21749 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21750 return 0;
21751 return 1;
21752 }
21753 else if (EQ (XCAR (hot_spot), Qcircle))
21754 {
21755 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21756 Lisp_Object circ = XCDR (hot_spot);
21757 Lisp_Object lr, lx0, ly0;
21758 if (CONSP (circ)
21759 && CONSP (XCAR (circ))
21760 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21761 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21762 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21763 {
21764 double r = XFLOATINT (lr);
21765 double dx = XINT (lx0) - x;
21766 double dy = XINT (ly0) - y;
21767 return (dx * dx + dy * dy <= r * r);
21768 }
21769 }
21770 else if (EQ (XCAR (hot_spot), Qpoly))
21771 {
21772 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21773 if (VECTORP (XCDR (hot_spot)))
21774 {
21775 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21776 Lisp_Object *poly = v->contents;
21777 int n = v->size;
21778 int i;
21779 int inside = 0;
21780 Lisp_Object lx, ly;
21781 int x0, y0;
21782
21783 /* Need an even number of coordinates, and at least 3 edges. */
21784 if (n < 6 || n & 1)
21785 return 0;
21786
21787 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21788 If count is odd, we are inside polygon. Pixels on edges
21789 may or may not be included depending on actual geometry of the
21790 polygon. */
21791 if ((lx = poly[n-2], !INTEGERP (lx))
21792 || (ly = poly[n-1], !INTEGERP (lx)))
21793 return 0;
21794 x0 = XINT (lx), y0 = XINT (ly);
21795 for (i = 0; i < n; i += 2)
21796 {
21797 int x1 = x0, y1 = y0;
21798 if ((lx = poly[i], !INTEGERP (lx))
21799 || (ly = poly[i+1], !INTEGERP (ly)))
21800 return 0;
21801 x0 = XINT (lx), y0 = XINT (ly);
21802
21803 /* Does this segment cross the X line? */
21804 if (x0 >= x)
21805 {
21806 if (x1 >= x)
21807 continue;
21808 }
21809 else if (x1 < x)
21810 continue;
21811 if (y > y0 && y > y1)
21812 continue;
21813 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21814 inside = !inside;
21815 }
21816 return inside;
21817 }
21818 }
21819 /* If we don't understand the format, pretend we're not in the hot-spot. */
21820 return 0;
21821 }
21822
21823 Lisp_Object
21824 find_hot_spot (map, x, y)
21825 Lisp_Object map;
21826 int x, y;
21827 {
21828 while (CONSP (map))
21829 {
21830 if (CONSP (XCAR (map))
21831 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21832 return XCAR (map);
21833 map = XCDR (map);
21834 }
21835
21836 return Qnil;
21837 }
21838
21839 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21840 3, 3, 0,
21841 doc: /* Lookup in image map MAP coordinates X and Y.
21842 An image map is an alist where each element has the format (AREA ID PLIST).
21843 An AREA is specified as either a rectangle, a circle, or a polygon:
21844 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21845 pixel coordinates of the upper left and bottom right corners.
21846 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21847 and the radius of the circle; r may be a float or integer.
21848 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21849 vector describes one corner in the polygon.
21850 Returns the alist element for the first matching AREA in MAP. */)
21851 (map, x, y)
21852 Lisp_Object map;
21853 Lisp_Object x, y;
21854 {
21855 if (NILP (map))
21856 return Qnil;
21857
21858 CHECK_NUMBER (x);
21859 CHECK_NUMBER (y);
21860
21861 return find_hot_spot (map, XINT (x), XINT (y));
21862 }
21863
21864
21865 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21866 static void
21867 define_frame_cursor1 (f, cursor, pointer)
21868 struct frame *f;
21869 Cursor cursor;
21870 Lisp_Object pointer;
21871 {
21872 /* Do not change cursor shape while dragging mouse. */
21873 if (!NILP (do_mouse_tracking))
21874 return;
21875
21876 if (!NILP (pointer))
21877 {
21878 if (EQ (pointer, Qarrow))
21879 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21880 else if (EQ (pointer, Qhand))
21881 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21882 else if (EQ (pointer, Qtext))
21883 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21884 else if (EQ (pointer, intern ("hdrag")))
21885 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21886 #ifdef HAVE_X_WINDOWS
21887 else if (EQ (pointer, intern ("vdrag")))
21888 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21889 #endif
21890 else if (EQ (pointer, intern ("hourglass")))
21891 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21892 else if (EQ (pointer, Qmodeline))
21893 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21894 else
21895 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21896 }
21897
21898 if (cursor != No_Cursor)
21899 rif->define_frame_cursor (f, cursor);
21900 }
21901
21902 /* Take proper action when mouse has moved to the mode or header line
21903 or marginal area AREA of window W, x-position X and y-position Y.
21904 X is relative to the start of the text display area of W, so the
21905 width of bitmap areas and scroll bars must be subtracted to get a
21906 position relative to the start of the mode line. */
21907
21908 static void
21909 note_mode_line_or_margin_highlight (window, x, y, area)
21910 Lisp_Object window;
21911 int x, y;
21912 enum window_part area;
21913 {
21914 struct window *w = XWINDOW (window);
21915 struct frame *f = XFRAME (w->frame);
21916 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21917 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21918 Lisp_Object pointer = Qnil;
21919 int charpos, dx, dy, width, height;
21920 Lisp_Object string, object = Qnil;
21921 Lisp_Object pos, help;
21922
21923 Lisp_Object mouse_face;
21924 int original_x_pixel = x;
21925 struct glyph * glyph = NULL;
21926 struct glyph_row *row;
21927
21928 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21929 {
21930 int x0;
21931 struct glyph *end;
21932
21933 string = mode_line_string (w, area, &x, &y, &charpos,
21934 &object, &dx, &dy, &width, &height);
21935
21936 row = (area == ON_MODE_LINE
21937 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21938 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21939
21940 /* Find glyph */
21941 if (row->mode_line_p && row->enabled_p)
21942 {
21943 glyph = row->glyphs[TEXT_AREA];
21944 end = glyph + row->used[TEXT_AREA];
21945
21946 for (x0 = original_x_pixel;
21947 glyph < end && x0 >= glyph->pixel_width;
21948 ++glyph)
21949 x0 -= glyph->pixel_width;
21950
21951 if (glyph >= end)
21952 glyph = NULL;
21953 }
21954 }
21955 else
21956 {
21957 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21958 string = marginal_area_string (w, area, &x, &y, &charpos,
21959 &object, &dx, &dy, &width, &height);
21960 }
21961
21962 help = Qnil;
21963
21964 if (IMAGEP (object))
21965 {
21966 Lisp_Object image_map, hotspot;
21967 if ((image_map = Fplist_get (XCDR (object), QCmap),
21968 !NILP (image_map))
21969 && (hotspot = find_hot_spot (image_map, dx, dy),
21970 CONSP (hotspot))
21971 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21972 {
21973 Lisp_Object area_id, plist;
21974
21975 area_id = XCAR (hotspot);
21976 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21977 If so, we could look for mouse-enter, mouse-leave
21978 properties in PLIST (and do something...). */
21979 hotspot = XCDR (hotspot);
21980 if (CONSP (hotspot)
21981 && (plist = XCAR (hotspot), CONSP (plist)))
21982 {
21983 pointer = Fplist_get (plist, Qpointer);
21984 if (NILP (pointer))
21985 pointer = Qhand;
21986 help = Fplist_get (plist, Qhelp_echo);
21987 if (!NILP (help))
21988 {
21989 help_echo_string = help;
21990 /* Is this correct? ++kfs */
21991 XSETWINDOW (help_echo_window, w);
21992 help_echo_object = w->buffer;
21993 help_echo_pos = charpos;
21994 }
21995 }
21996 }
21997 if (NILP (pointer))
21998 pointer = Fplist_get (XCDR (object), QCpointer);
21999 }
22000
22001 if (STRINGP (string))
22002 {
22003 pos = make_number (charpos);
22004 /* If we're on a string with `help-echo' text property, arrange
22005 for the help to be displayed. This is done by setting the
22006 global variable help_echo_string to the help string. */
22007 if (NILP (help))
22008 {
22009 help = Fget_text_property (pos, Qhelp_echo, string);
22010 if (!NILP (help))
22011 {
22012 help_echo_string = help;
22013 XSETWINDOW (help_echo_window, w);
22014 help_echo_object = string;
22015 help_echo_pos = charpos;
22016 }
22017 }
22018
22019 if (NILP (pointer))
22020 pointer = Fget_text_property (pos, Qpointer, string);
22021
22022 /* Change the mouse pointer according to what is under X/Y. */
22023 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22024 {
22025 Lisp_Object map;
22026 map = Fget_text_property (pos, Qlocal_map, string);
22027 if (!KEYMAPP (map))
22028 map = Fget_text_property (pos, Qkeymap, string);
22029 if (!KEYMAPP (map))
22030 cursor = dpyinfo->vertical_scroll_bar_cursor;
22031 }
22032
22033 /* Change the mouse face according to what is under X/Y. */
22034 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22035 if (!NILP (mouse_face)
22036 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22037 && glyph)
22038 {
22039 Lisp_Object b, e;
22040
22041 struct glyph * tmp_glyph;
22042
22043 int gpos;
22044 int gseq_length;
22045 int total_pixel_width;
22046 int ignore;
22047
22048 int vpos, hpos;
22049
22050 b = Fprevious_single_property_change (make_number (charpos + 1),
22051 Qmouse_face, string, Qnil);
22052 if (NILP (b))
22053 b = make_number (0);
22054
22055 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22056 if (NILP (e))
22057 e = make_number (SCHARS (string));
22058
22059 /* Calculate the position(glyph position: GPOS) of GLYPH in
22060 displayed string. GPOS is different from CHARPOS.
22061
22062 CHARPOS is the position of glyph in internal string
22063 object. A mode line string format has structures which
22064 is converted to a flatten by emacs lisp interpreter.
22065 The internal string is an element of the structures.
22066 The displayed string is the flatten string. */
22067 for (tmp_glyph = glyph - 1, gpos = 0;
22068 tmp_glyph->charpos >= XINT (b);
22069 tmp_glyph--, gpos++)
22070 {
22071 if (!EQ (tmp_glyph->object, glyph->object))
22072 break;
22073 }
22074
22075 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22076 displayed string holding GLYPH.
22077
22078 GSEQ_LENGTH is different from SCHARS (STRING).
22079 SCHARS (STRING) returns the length of the internal string. */
22080 for (tmp_glyph = glyph, gseq_length = gpos;
22081 tmp_glyph->charpos < XINT (e);
22082 tmp_glyph++, gseq_length++)
22083 {
22084 if (!EQ (tmp_glyph->object, glyph->object))
22085 break;
22086 }
22087
22088 total_pixel_width = 0;
22089 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22090 total_pixel_width += tmp_glyph->pixel_width;
22091
22092 /* Pre calculation of re-rendering position */
22093 vpos = (x - gpos);
22094 hpos = (area == ON_MODE_LINE
22095 ? (w->current_matrix)->nrows - 1
22096 : 0);
22097
22098 /* If the re-rendering position is included in the last
22099 re-rendering area, we should do nothing. */
22100 if ( EQ (window, dpyinfo->mouse_face_window)
22101 && dpyinfo->mouse_face_beg_col <= vpos
22102 && vpos < dpyinfo->mouse_face_end_col
22103 && dpyinfo->mouse_face_beg_row == hpos )
22104 return;
22105
22106 if (clear_mouse_face (dpyinfo))
22107 cursor = No_Cursor;
22108
22109 dpyinfo->mouse_face_beg_col = vpos;
22110 dpyinfo->mouse_face_beg_row = hpos;
22111
22112 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22113 dpyinfo->mouse_face_beg_y = 0;
22114
22115 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22116 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22117
22118 dpyinfo->mouse_face_end_x = 0;
22119 dpyinfo->mouse_face_end_y = 0;
22120
22121 dpyinfo->mouse_face_past_end = 0;
22122 dpyinfo->mouse_face_window = window;
22123
22124 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22125 charpos,
22126 0, 0, 0, &ignore,
22127 glyph->face_id, 1);
22128 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22129
22130 if (NILP (pointer))
22131 pointer = Qhand;
22132 }
22133 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22134 clear_mouse_face (dpyinfo);
22135 }
22136 define_frame_cursor1 (f, cursor, pointer);
22137 }
22138
22139
22140 /* EXPORT:
22141 Take proper action when the mouse has moved to position X, Y on
22142 frame F as regards highlighting characters that have mouse-face
22143 properties. Also de-highlighting chars where the mouse was before.
22144 X and Y can be negative or out of range. */
22145
22146 void
22147 note_mouse_highlight (f, x, y)
22148 struct frame *f;
22149 int x, y;
22150 {
22151 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22152 enum window_part part;
22153 Lisp_Object window;
22154 struct window *w;
22155 Cursor cursor = No_Cursor;
22156 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22157 struct buffer *b;
22158
22159 /* When a menu is active, don't highlight because this looks odd. */
22160 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22161 if (popup_activated ())
22162 return;
22163 #endif
22164
22165 if (NILP (Vmouse_highlight)
22166 || !f->glyphs_initialized_p)
22167 return;
22168
22169 dpyinfo->mouse_face_mouse_x = x;
22170 dpyinfo->mouse_face_mouse_y = y;
22171 dpyinfo->mouse_face_mouse_frame = f;
22172
22173 if (dpyinfo->mouse_face_defer)
22174 return;
22175
22176 if (gc_in_progress)
22177 {
22178 dpyinfo->mouse_face_deferred_gc = 1;
22179 return;
22180 }
22181
22182 /* Which window is that in? */
22183 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22184
22185 /* If we were displaying active text in another window, clear that.
22186 Also clear if we move out of text area in same window. */
22187 if (! EQ (window, dpyinfo->mouse_face_window)
22188 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22189 && !NILP (dpyinfo->mouse_face_window)))
22190 clear_mouse_face (dpyinfo);
22191
22192 /* Not on a window -> return. */
22193 if (!WINDOWP (window))
22194 return;
22195
22196 /* Reset help_echo_string. It will get recomputed below. */
22197 help_echo_string = Qnil;
22198
22199 /* Convert to window-relative pixel coordinates. */
22200 w = XWINDOW (window);
22201 frame_to_window_pixel_xy (w, &x, &y);
22202
22203 /* Handle tool-bar window differently since it doesn't display a
22204 buffer. */
22205 if (EQ (window, f->tool_bar_window))
22206 {
22207 note_tool_bar_highlight (f, x, y);
22208 return;
22209 }
22210
22211 /* Mouse is on the mode, header line or margin? */
22212 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22213 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22214 {
22215 note_mode_line_or_margin_highlight (window, x, y, part);
22216 return;
22217 }
22218
22219 if (part == ON_VERTICAL_BORDER)
22220 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22221 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22222 || part == ON_SCROLL_BAR)
22223 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22224 else
22225 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22226
22227 /* Are we in a window whose display is up to date?
22228 And verify the buffer's text has not changed. */
22229 b = XBUFFER (w->buffer);
22230 if (part == ON_TEXT
22231 && EQ (w->window_end_valid, w->buffer)
22232 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22233 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22234 {
22235 int hpos, vpos, pos, i, dx, dy, area;
22236 struct glyph *glyph;
22237 Lisp_Object object;
22238 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22239 Lisp_Object *overlay_vec = NULL;
22240 int noverlays;
22241 struct buffer *obuf;
22242 int obegv, ozv, same_region;
22243
22244 /* Find the glyph under X/Y. */
22245 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22246
22247 /* Look for :pointer property on image. */
22248 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22249 {
22250 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22251 if (img != NULL && IMAGEP (img->spec))
22252 {
22253 Lisp_Object image_map, hotspot;
22254 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22255 !NILP (image_map))
22256 && (hotspot = find_hot_spot (image_map,
22257 glyph->slice.x + dx,
22258 glyph->slice.y + dy),
22259 CONSP (hotspot))
22260 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22261 {
22262 Lisp_Object area_id, plist;
22263
22264 area_id = XCAR (hotspot);
22265 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22266 If so, we could look for mouse-enter, mouse-leave
22267 properties in PLIST (and do something...). */
22268 hotspot = XCDR (hotspot);
22269 if (CONSP (hotspot)
22270 && (plist = XCAR (hotspot), CONSP (plist)))
22271 {
22272 pointer = Fplist_get (plist, Qpointer);
22273 if (NILP (pointer))
22274 pointer = Qhand;
22275 help_echo_string = Fplist_get (plist, Qhelp_echo);
22276 if (!NILP (help_echo_string))
22277 {
22278 help_echo_window = window;
22279 help_echo_object = glyph->object;
22280 help_echo_pos = glyph->charpos;
22281 }
22282 }
22283 }
22284 if (NILP (pointer))
22285 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22286 }
22287 }
22288
22289 /* Clear mouse face if X/Y not over text. */
22290 if (glyph == NULL
22291 || area != TEXT_AREA
22292 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22293 {
22294 if (clear_mouse_face (dpyinfo))
22295 cursor = No_Cursor;
22296 if (NILP (pointer))
22297 {
22298 if (area != TEXT_AREA)
22299 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22300 else
22301 pointer = Vvoid_text_area_pointer;
22302 }
22303 goto set_cursor;
22304 }
22305
22306 pos = glyph->charpos;
22307 object = glyph->object;
22308 if (!STRINGP (object) && !BUFFERP (object))
22309 goto set_cursor;
22310
22311 /* If we get an out-of-range value, return now; avoid an error. */
22312 if (BUFFERP (object) && pos > BUF_Z (b))
22313 goto set_cursor;
22314
22315 /* Make the window's buffer temporarily current for
22316 overlays_at and compute_char_face. */
22317 obuf = current_buffer;
22318 current_buffer = b;
22319 obegv = BEGV;
22320 ozv = ZV;
22321 BEGV = BEG;
22322 ZV = Z;
22323
22324 /* Is this char mouse-active or does it have help-echo? */
22325 position = make_number (pos);
22326
22327 if (BUFFERP (object))
22328 {
22329 /* Put all the overlays we want in a vector in overlay_vec. */
22330 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22331 /* Sort overlays into increasing priority order. */
22332 noverlays = sort_overlays (overlay_vec, noverlays, w);
22333 }
22334 else
22335 noverlays = 0;
22336
22337 same_region = (EQ (window, dpyinfo->mouse_face_window)
22338 && vpos >= dpyinfo->mouse_face_beg_row
22339 && vpos <= dpyinfo->mouse_face_end_row
22340 && (vpos > dpyinfo->mouse_face_beg_row
22341 || hpos >= dpyinfo->mouse_face_beg_col)
22342 && (vpos < dpyinfo->mouse_face_end_row
22343 || hpos < dpyinfo->mouse_face_end_col
22344 || dpyinfo->mouse_face_past_end));
22345
22346 if (same_region)
22347 cursor = No_Cursor;
22348
22349 /* Check mouse-face highlighting. */
22350 if (! same_region
22351 /* If there exists an overlay with mouse-face overlapping
22352 the one we are currently highlighting, we have to
22353 check if we enter the overlapping overlay, and then
22354 highlight only that. */
22355 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22356 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22357 {
22358 /* Find the highest priority overlay that has a mouse-face
22359 property. */
22360 overlay = Qnil;
22361 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22362 {
22363 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22364 if (!NILP (mouse_face))
22365 overlay = overlay_vec[i];
22366 }
22367
22368 /* If we're actually highlighting the same overlay as
22369 before, there's no need to do that again. */
22370 if (!NILP (overlay)
22371 && EQ (overlay, dpyinfo->mouse_face_overlay))
22372 goto check_help_echo;
22373
22374 dpyinfo->mouse_face_overlay = overlay;
22375
22376 /* Clear the display of the old active region, if any. */
22377 if (clear_mouse_face (dpyinfo))
22378 cursor = No_Cursor;
22379
22380 /* If no overlay applies, get a text property. */
22381 if (NILP (overlay))
22382 mouse_face = Fget_text_property (position, Qmouse_face, object);
22383
22384 /* Handle the overlay case. */
22385 if (!NILP (overlay))
22386 {
22387 /* Find the range of text around this char that
22388 should be active. */
22389 Lisp_Object before, after;
22390 int ignore;
22391
22392 before = Foverlay_start (overlay);
22393 after = Foverlay_end (overlay);
22394 /* Record this as the current active region. */
22395 fast_find_position (w, XFASTINT (before),
22396 &dpyinfo->mouse_face_beg_col,
22397 &dpyinfo->mouse_face_beg_row,
22398 &dpyinfo->mouse_face_beg_x,
22399 &dpyinfo->mouse_face_beg_y, Qnil);
22400
22401 dpyinfo->mouse_face_past_end
22402 = !fast_find_position (w, XFASTINT (after),
22403 &dpyinfo->mouse_face_end_col,
22404 &dpyinfo->mouse_face_end_row,
22405 &dpyinfo->mouse_face_end_x,
22406 &dpyinfo->mouse_face_end_y, Qnil);
22407 dpyinfo->mouse_face_window = window;
22408
22409 dpyinfo->mouse_face_face_id
22410 = face_at_buffer_position (w, pos, 0, 0,
22411 &ignore, pos + 1,
22412 !dpyinfo->mouse_face_hidden);
22413
22414 /* Display it as active. */
22415 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22416 cursor = No_Cursor;
22417 }
22418 /* Handle the text property case. */
22419 else if (!NILP (mouse_face) && BUFFERP (object))
22420 {
22421 /* Find the range of text around this char that
22422 should be active. */
22423 Lisp_Object before, after, beginning, end;
22424 int ignore;
22425
22426 beginning = Fmarker_position (w->start);
22427 end = make_number (BUF_Z (XBUFFER (object))
22428 - XFASTINT (w->window_end_pos));
22429 before
22430 = Fprevious_single_property_change (make_number (pos + 1),
22431 Qmouse_face,
22432 object, beginning);
22433 after
22434 = Fnext_single_property_change (position, Qmouse_face,
22435 object, end);
22436
22437 /* Record this as the current active region. */
22438 fast_find_position (w, XFASTINT (before),
22439 &dpyinfo->mouse_face_beg_col,
22440 &dpyinfo->mouse_face_beg_row,
22441 &dpyinfo->mouse_face_beg_x,
22442 &dpyinfo->mouse_face_beg_y, Qnil);
22443 dpyinfo->mouse_face_past_end
22444 = !fast_find_position (w, XFASTINT (after),
22445 &dpyinfo->mouse_face_end_col,
22446 &dpyinfo->mouse_face_end_row,
22447 &dpyinfo->mouse_face_end_x,
22448 &dpyinfo->mouse_face_end_y, Qnil);
22449 dpyinfo->mouse_face_window = window;
22450
22451 if (BUFFERP (object))
22452 dpyinfo->mouse_face_face_id
22453 = face_at_buffer_position (w, pos, 0, 0,
22454 &ignore, pos + 1,
22455 !dpyinfo->mouse_face_hidden);
22456
22457 /* Display it as active. */
22458 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22459 cursor = No_Cursor;
22460 }
22461 else if (!NILP (mouse_face) && STRINGP (object))
22462 {
22463 Lisp_Object b, e;
22464 int ignore;
22465
22466 b = Fprevious_single_property_change (make_number (pos + 1),
22467 Qmouse_face,
22468 object, Qnil);
22469 e = Fnext_single_property_change (position, Qmouse_face,
22470 object, Qnil);
22471 if (NILP (b))
22472 b = make_number (0);
22473 if (NILP (e))
22474 e = make_number (SCHARS (object) - 1);
22475
22476 fast_find_string_pos (w, XINT (b), object,
22477 &dpyinfo->mouse_face_beg_col,
22478 &dpyinfo->mouse_face_beg_row,
22479 &dpyinfo->mouse_face_beg_x,
22480 &dpyinfo->mouse_face_beg_y, 0);
22481 fast_find_string_pos (w, XINT (e), object,
22482 &dpyinfo->mouse_face_end_col,
22483 &dpyinfo->mouse_face_end_row,
22484 &dpyinfo->mouse_face_end_x,
22485 &dpyinfo->mouse_face_end_y, 1);
22486 dpyinfo->mouse_face_past_end = 0;
22487 dpyinfo->mouse_face_window = window;
22488 dpyinfo->mouse_face_face_id
22489 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22490 glyph->face_id, 1);
22491 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22492 cursor = No_Cursor;
22493 }
22494 else if (STRINGP (object) && NILP (mouse_face))
22495 {
22496 /* A string which doesn't have mouse-face, but
22497 the text ``under'' it might have. */
22498 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22499 int start = MATRIX_ROW_START_CHARPOS (r);
22500
22501 pos = string_buffer_position (w, object, start);
22502 if (pos > 0)
22503 mouse_face = get_char_property_and_overlay (make_number (pos),
22504 Qmouse_face,
22505 w->buffer,
22506 &overlay);
22507 if (!NILP (mouse_face) && !NILP (overlay))
22508 {
22509 Lisp_Object before = Foverlay_start (overlay);
22510 Lisp_Object after = Foverlay_end (overlay);
22511 int ignore;
22512
22513 /* Note that we might not be able to find position
22514 BEFORE in the glyph matrix if the overlay is
22515 entirely covered by a `display' property. In
22516 this case, we overshoot. So let's stop in
22517 the glyph matrix before glyphs for OBJECT. */
22518 fast_find_position (w, XFASTINT (before),
22519 &dpyinfo->mouse_face_beg_col,
22520 &dpyinfo->mouse_face_beg_row,
22521 &dpyinfo->mouse_face_beg_x,
22522 &dpyinfo->mouse_face_beg_y,
22523 object);
22524
22525 dpyinfo->mouse_face_past_end
22526 = !fast_find_position (w, XFASTINT (after),
22527 &dpyinfo->mouse_face_end_col,
22528 &dpyinfo->mouse_face_end_row,
22529 &dpyinfo->mouse_face_end_x,
22530 &dpyinfo->mouse_face_end_y,
22531 Qnil);
22532 dpyinfo->mouse_face_window = window;
22533 dpyinfo->mouse_face_face_id
22534 = face_at_buffer_position (w, pos, 0, 0,
22535 &ignore, pos + 1,
22536 !dpyinfo->mouse_face_hidden);
22537
22538 /* Display it as active. */
22539 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22540 cursor = No_Cursor;
22541 }
22542 }
22543 }
22544
22545 check_help_echo:
22546
22547 /* Look for a `help-echo' property. */
22548 if (NILP (help_echo_string)) {
22549 Lisp_Object help, overlay;
22550
22551 /* Check overlays first. */
22552 help = overlay = Qnil;
22553 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22554 {
22555 overlay = overlay_vec[i];
22556 help = Foverlay_get (overlay, Qhelp_echo);
22557 }
22558
22559 if (!NILP (help))
22560 {
22561 help_echo_string = help;
22562 help_echo_window = window;
22563 help_echo_object = overlay;
22564 help_echo_pos = pos;
22565 }
22566 else
22567 {
22568 Lisp_Object object = glyph->object;
22569 int charpos = glyph->charpos;
22570
22571 /* Try text properties. */
22572 if (STRINGP (object)
22573 && charpos >= 0
22574 && charpos < SCHARS (object))
22575 {
22576 help = Fget_text_property (make_number (charpos),
22577 Qhelp_echo, object);
22578 if (NILP (help))
22579 {
22580 /* If the string itself doesn't specify a help-echo,
22581 see if the buffer text ``under'' it does. */
22582 struct glyph_row *r
22583 = MATRIX_ROW (w->current_matrix, vpos);
22584 int start = MATRIX_ROW_START_CHARPOS (r);
22585 int pos = string_buffer_position (w, object, start);
22586 if (pos > 0)
22587 {
22588 help = Fget_char_property (make_number (pos),
22589 Qhelp_echo, w->buffer);
22590 if (!NILP (help))
22591 {
22592 charpos = pos;
22593 object = w->buffer;
22594 }
22595 }
22596 }
22597 }
22598 else if (BUFFERP (object)
22599 && charpos >= BEGV
22600 && charpos < ZV)
22601 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22602 object);
22603
22604 if (!NILP (help))
22605 {
22606 help_echo_string = help;
22607 help_echo_window = window;
22608 help_echo_object = object;
22609 help_echo_pos = charpos;
22610 }
22611 }
22612 }
22613
22614 /* Look for a `pointer' property. */
22615 if (NILP (pointer))
22616 {
22617 /* Check overlays first. */
22618 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22619 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22620
22621 if (NILP (pointer))
22622 {
22623 Lisp_Object object = glyph->object;
22624 int charpos = glyph->charpos;
22625
22626 /* Try text properties. */
22627 if (STRINGP (object)
22628 && charpos >= 0
22629 && charpos < SCHARS (object))
22630 {
22631 pointer = Fget_text_property (make_number (charpos),
22632 Qpointer, object);
22633 if (NILP (pointer))
22634 {
22635 /* If the string itself doesn't specify a pointer,
22636 see if the buffer text ``under'' it does. */
22637 struct glyph_row *r
22638 = MATRIX_ROW (w->current_matrix, vpos);
22639 int start = MATRIX_ROW_START_CHARPOS (r);
22640 int pos = string_buffer_position (w, object, start);
22641 if (pos > 0)
22642 pointer = Fget_char_property (make_number (pos),
22643 Qpointer, w->buffer);
22644 }
22645 }
22646 else if (BUFFERP (object)
22647 && charpos >= BEGV
22648 && charpos < ZV)
22649 pointer = Fget_text_property (make_number (charpos),
22650 Qpointer, object);
22651 }
22652 }
22653
22654 BEGV = obegv;
22655 ZV = ozv;
22656 current_buffer = obuf;
22657 }
22658
22659 set_cursor:
22660
22661 define_frame_cursor1 (f, cursor, pointer);
22662 }
22663
22664
22665 /* EXPORT for RIF:
22666 Clear any mouse-face on window W. This function is part of the
22667 redisplay interface, and is called from try_window_id and similar
22668 functions to ensure the mouse-highlight is off. */
22669
22670 void
22671 x_clear_window_mouse_face (w)
22672 struct window *w;
22673 {
22674 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22675 Lisp_Object window;
22676
22677 BLOCK_INPUT;
22678 XSETWINDOW (window, w);
22679 if (EQ (window, dpyinfo->mouse_face_window))
22680 clear_mouse_face (dpyinfo);
22681 UNBLOCK_INPUT;
22682 }
22683
22684
22685 /* EXPORT:
22686 Just discard the mouse face information for frame F, if any.
22687 This is used when the size of F is changed. */
22688
22689 void
22690 cancel_mouse_face (f)
22691 struct frame *f;
22692 {
22693 Lisp_Object window;
22694 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22695
22696 window = dpyinfo->mouse_face_window;
22697 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22698 {
22699 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22700 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22701 dpyinfo->mouse_face_window = Qnil;
22702 }
22703 }
22704
22705
22706 #endif /* HAVE_WINDOW_SYSTEM */
22707
22708 \f
22709 /***********************************************************************
22710 Exposure Events
22711 ***********************************************************************/
22712
22713 #ifdef HAVE_WINDOW_SYSTEM
22714
22715 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22716 which intersects rectangle R. R is in window-relative coordinates. */
22717
22718 static void
22719 expose_area (w, row, r, area)
22720 struct window *w;
22721 struct glyph_row *row;
22722 XRectangle *r;
22723 enum glyph_row_area area;
22724 {
22725 struct glyph *first = row->glyphs[area];
22726 struct glyph *end = row->glyphs[area] + row->used[area];
22727 struct glyph *last;
22728 int first_x, start_x, x;
22729
22730 if (area == TEXT_AREA && row->fill_line_p)
22731 /* If row extends face to end of line write the whole line. */
22732 draw_glyphs (w, 0, row, area,
22733 0, row->used[area],
22734 DRAW_NORMAL_TEXT, 0);
22735 else
22736 {
22737 /* Set START_X to the window-relative start position for drawing glyphs of
22738 AREA. The first glyph of the text area can be partially visible.
22739 The first glyphs of other areas cannot. */
22740 start_x = window_box_left_offset (w, area);
22741 x = start_x;
22742 if (area == TEXT_AREA)
22743 x += row->x;
22744
22745 /* Find the first glyph that must be redrawn. */
22746 while (first < end
22747 && x + first->pixel_width < r->x)
22748 {
22749 x += first->pixel_width;
22750 ++first;
22751 }
22752
22753 /* Find the last one. */
22754 last = first;
22755 first_x = x;
22756 while (last < end
22757 && x < r->x + r->width)
22758 {
22759 x += last->pixel_width;
22760 ++last;
22761 }
22762
22763 /* Repaint. */
22764 if (last > first)
22765 draw_glyphs (w, first_x - start_x, row, area,
22766 first - row->glyphs[area], last - row->glyphs[area],
22767 DRAW_NORMAL_TEXT, 0);
22768 }
22769 }
22770
22771
22772 /* Redraw the parts of the glyph row ROW on window W intersecting
22773 rectangle R. R is in window-relative coordinates. Value is
22774 non-zero if mouse-face was overwritten. */
22775
22776 static int
22777 expose_line (w, row, r)
22778 struct window *w;
22779 struct glyph_row *row;
22780 XRectangle *r;
22781 {
22782 xassert (row->enabled_p);
22783
22784 if (row->mode_line_p || w->pseudo_window_p)
22785 draw_glyphs (w, 0, row, TEXT_AREA,
22786 0, row->used[TEXT_AREA],
22787 DRAW_NORMAL_TEXT, 0);
22788 else
22789 {
22790 if (row->used[LEFT_MARGIN_AREA])
22791 expose_area (w, row, r, LEFT_MARGIN_AREA);
22792 if (row->used[TEXT_AREA])
22793 expose_area (w, row, r, TEXT_AREA);
22794 if (row->used[RIGHT_MARGIN_AREA])
22795 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22796 draw_row_fringe_bitmaps (w, row);
22797 }
22798
22799 return row->mouse_face_p;
22800 }
22801
22802
22803 /* Redraw those parts of glyphs rows during expose event handling that
22804 overlap other rows. Redrawing of an exposed line writes over parts
22805 of lines overlapping that exposed line; this function fixes that.
22806
22807 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22808 row in W's current matrix that is exposed and overlaps other rows.
22809 LAST_OVERLAPPING_ROW is the last such row. */
22810
22811 static void
22812 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22813 struct window *w;
22814 struct glyph_row *first_overlapping_row;
22815 struct glyph_row *last_overlapping_row;
22816 {
22817 struct glyph_row *row;
22818
22819 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22820 if (row->overlapping_p)
22821 {
22822 xassert (row->enabled_p && !row->mode_line_p);
22823
22824 if (row->used[LEFT_MARGIN_AREA])
22825 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
22826
22827 if (row->used[TEXT_AREA])
22828 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
22829
22830 if (row->used[RIGHT_MARGIN_AREA])
22831 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
22832 }
22833 }
22834
22835
22836 /* Return non-zero if W's cursor intersects rectangle R. */
22837
22838 static int
22839 phys_cursor_in_rect_p (w, r)
22840 struct window *w;
22841 XRectangle *r;
22842 {
22843 XRectangle cr, result;
22844 struct glyph *cursor_glyph;
22845
22846 cursor_glyph = get_phys_cursor_glyph (w);
22847 if (cursor_glyph)
22848 {
22849 /* r is relative to W's box, but w->phys_cursor.x is relative
22850 to left edge of W's TEXT area. Adjust it. */
22851 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22852 cr.y = w->phys_cursor.y;
22853 cr.width = cursor_glyph->pixel_width;
22854 cr.height = w->phys_cursor_height;
22855 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22856 I assume the effect is the same -- and this is portable. */
22857 return x_intersect_rectangles (&cr, r, &result);
22858 }
22859 else
22860 return 0;
22861 }
22862
22863
22864 /* EXPORT:
22865 Draw a vertical window border to the right of window W if W doesn't
22866 have vertical scroll bars. */
22867
22868 void
22869 x_draw_vertical_border (w)
22870 struct window *w;
22871 {
22872 /* We could do better, if we knew what type of scroll-bar the adjacent
22873 windows (on either side) have... But we don't :-(
22874 However, I think this works ok. ++KFS 2003-04-25 */
22875
22876 /* Redraw borders between horizontally adjacent windows. Don't
22877 do it for frames with vertical scroll bars because either the
22878 right scroll bar of a window, or the left scroll bar of its
22879 neighbor will suffice as a border. */
22880 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22881 return;
22882
22883 if (!WINDOW_RIGHTMOST_P (w)
22884 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22885 {
22886 int x0, x1, y0, y1;
22887
22888 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22889 y1 -= 1;
22890
22891 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22892 x1 -= 1;
22893
22894 rif->draw_vertical_window_border (w, x1, y0, y1);
22895 }
22896 else if (!WINDOW_LEFTMOST_P (w)
22897 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22898 {
22899 int x0, x1, y0, y1;
22900
22901 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22902 y1 -= 1;
22903
22904 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22905 x0 -= 1;
22906
22907 rif->draw_vertical_window_border (w, x0, y0, y1);
22908 }
22909 }
22910
22911
22912 /* Redraw the part of window W intersection rectangle FR. Pixel
22913 coordinates in FR are frame-relative. Call this function with
22914 input blocked. Value is non-zero if the exposure overwrites
22915 mouse-face. */
22916
22917 static int
22918 expose_window (w, fr)
22919 struct window *w;
22920 XRectangle *fr;
22921 {
22922 struct frame *f = XFRAME (w->frame);
22923 XRectangle wr, r;
22924 int mouse_face_overwritten_p = 0;
22925
22926 /* If window is not yet fully initialized, do nothing. This can
22927 happen when toolkit scroll bars are used and a window is split.
22928 Reconfiguring the scroll bar will generate an expose for a newly
22929 created window. */
22930 if (w->current_matrix == NULL)
22931 return 0;
22932
22933 /* When we're currently updating the window, display and current
22934 matrix usually don't agree. Arrange for a thorough display
22935 later. */
22936 if (w == updated_window)
22937 {
22938 SET_FRAME_GARBAGED (f);
22939 return 0;
22940 }
22941
22942 /* Frame-relative pixel rectangle of W. */
22943 wr.x = WINDOW_LEFT_EDGE_X (w);
22944 wr.y = WINDOW_TOP_EDGE_Y (w);
22945 wr.width = WINDOW_TOTAL_WIDTH (w);
22946 wr.height = WINDOW_TOTAL_HEIGHT (w);
22947
22948 if (x_intersect_rectangles (fr, &wr, &r))
22949 {
22950 int yb = window_text_bottom_y (w);
22951 struct glyph_row *row;
22952 int cursor_cleared_p;
22953 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22954
22955 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22956 r.x, r.y, r.width, r.height));
22957
22958 /* Convert to window coordinates. */
22959 r.x -= WINDOW_LEFT_EDGE_X (w);
22960 r.y -= WINDOW_TOP_EDGE_Y (w);
22961
22962 /* Turn off the cursor. */
22963 if (!w->pseudo_window_p
22964 && phys_cursor_in_rect_p (w, &r))
22965 {
22966 x_clear_cursor (w);
22967 cursor_cleared_p = 1;
22968 }
22969 else
22970 cursor_cleared_p = 0;
22971
22972 /* Update lines intersecting rectangle R. */
22973 first_overlapping_row = last_overlapping_row = NULL;
22974 for (row = w->current_matrix->rows;
22975 row->enabled_p;
22976 ++row)
22977 {
22978 int y0 = row->y;
22979 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22980
22981 if ((y0 >= r.y && y0 < r.y + r.height)
22982 || (y1 > r.y && y1 < r.y + r.height)
22983 || (r.y >= y0 && r.y < y1)
22984 || (r.y + r.height > y0 && r.y + r.height < y1))
22985 {
22986 /* A header line may be overlapping, but there is no need
22987 to fix overlapping areas for them. KFS 2005-02-12 */
22988 if (row->overlapping_p && !row->mode_line_p)
22989 {
22990 if (first_overlapping_row == NULL)
22991 first_overlapping_row = row;
22992 last_overlapping_row = row;
22993 }
22994
22995 if (expose_line (w, row, &r))
22996 mouse_face_overwritten_p = 1;
22997 }
22998
22999 if (y1 >= yb)
23000 break;
23001 }
23002
23003 /* Display the mode line if there is one. */
23004 if (WINDOW_WANTS_MODELINE_P (w)
23005 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23006 row->enabled_p)
23007 && row->y < r.y + r.height)
23008 {
23009 if (expose_line (w, row, &r))
23010 mouse_face_overwritten_p = 1;
23011 }
23012
23013 if (!w->pseudo_window_p)
23014 {
23015 /* Fix the display of overlapping rows. */
23016 if (first_overlapping_row)
23017 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23018
23019 /* Draw border between windows. */
23020 x_draw_vertical_border (w);
23021
23022 /* Turn the cursor on again. */
23023 if (cursor_cleared_p)
23024 update_window_cursor (w, 1);
23025 }
23026 }
23027
23028 return mouse_face_overwritten_p;
23029 }
23030
23031
23032
23033 /* Redraw (parts) of all windows in the window tree rooted at W that
23034 intersect R. R contains frame pixel coordinates. Value is
23035 non-zero if the exposure overwrites mouse-face. */
23036
23037 static int
23038 expose_window_tree (w, r)
23039 struct window *w;
23040 XRectangle *r;
23041 {
23042 struct frame *f = XFRAME (w->frame);
23043 int mouse_face_overwritten_p = 0;
23044
23045 while (w && !FRAME_GARBAGED_P (f))
23046 {
23047 if (!NILP (w->hchild))
23048 mouse_face_overwritten_p
23049 |= expose_window_tree (XWINDOW (w->hchild), r);
23050 else if (!NILP (w->vchild))
23051 mouse_face_overwritten_p
23052 |= expose_window_tree (XWINDOW (w->vchild), r);
23053 else
23054 mouse_face_overwritten_p |= expose_window (w, r);
23055
23056 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23057 }
23058
23059 return mouse_face_overwritten_p;
23060 }
23061
23062
23063 /* EXPORT:
23064 Redisplay an exposed area of frame F. X and Y are the upper-left
23065 corner of the exposed rectangle. W and H are width and height of
23066 the exposed area. All are pixel values. W or H zero means redraw
23067 the entire frame. */
23068
23069 void
23070 expose_frame (f, x, y, w, h)
23071 struct frame *f;
23072 int x, y, w, h;
23073 {
23074 XRectangle r;
23075 int mouse_face_overwritten_p = 0;
23076
23077 TRACE ((stderr, "expose_frame "));
23078
23079 /* No need to redraw if frame will be redrawn soon. */
23080 if (FRAME_GARBAGED_P (f))
23081 {
23082 TRACE ((stderr, " garbaged\n"));
23083 return;
23084 }
23085
23086 /* If basic faces haven't been realized yet, there is no point in
23087 trying to redraw anything. This can happen when we get an expose
23088 event while Emacs is starting, e.g. by moving another window. */
23089 if (FRAME_FACE_CACHE (f) == NULL
23090 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23091 {
23092 TRACE ((stderr, " no faces\n"));
23093 return;
23094 }
23095
23096 if (w == 0 || h == 0)
23097 {
23098 r.x = r.y = 0;
23099 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23100 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23101 }
23102 else
23103 {
23104 r.x = x;
23105 r.y = y;
23106 r.width = w;
23107 r.height = h;
23108 }
23109
23110 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23111 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23112
23113 if (WINDOWP (f->tool_bar_window))
23114 mouse_face_overwritten_p
23115 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23116
23117 #ifdef HAVE_X_WINDOWS
23118 #ifndef MSDOS
23119 #ifndef USE_X_TOOLKIT
23120 if (WINDOWP (f->menu_bar_window))
23121 mouse_face_overwritten_p
23122 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23123 #endif /* not USE_X_TOOLKIT */
23124 #endif
23125 #endif
23126
23127 /* Some window managers support a focus-follows-mouse style with
23128 delayed raising of frames. Imagine a partially obscured frame,
23129 and moving the mouse into partially obscured mouse-face on that
23130 frame. The visible part of the mouse-face will be highlighted,
23131 then the WM raises the obscured frame. With at least one WM, KDE
23132 2.1, Emacs is not getting any event for the raising of the frame
23133 (even tried with SubstructureRedirectMask), only Expose events.
23134 These expose events will draw text normally, i.e. not
23135 highlighted. Which means we must redo the highlight here.
23136 Subsume it under ``we love X''. --gerd 2001-08-15 */
23137 /* Included in Windows version because Windows most likely does not
23138 do the right thing if any third party tool offers
23139 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23140 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23141 {
23142 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23143 if (f == dpyinfo->mouse_face_mouse_frame)
23144 {
23145 int x = dpyinfo->mouse_face_mouse_x;
23146 int y = dpyinfo->mouse_face_mouse_y;
23147 clear_mouse_face (dpyinfo);
23148 note_mouse_highlight (f, x, y);
23149 }
23150 }
23151 }
23152
23153
23154 /* EXPORT:
23155 Determine the intersection of two rectangles R1 and R2. Return
23156 the intersection in *RESULT. Value is non-zero if RESULT is not
23157 empty. */
23158
23159 int
23160 x_intersect_rectangles (r1, r2, result)
23161 XRectangle *r1, *r2, *result;
23162 {
23163 XRectangle *left, *right;
23164 XRectangle *upper, *lower;
23165 int intersection_p = 0;
23166
23167 /* Rearrange so that R1 is the left-most rectangle. */
23168 if (r1->x < r2->x)
23169 left = r1, right = r2;
23170 else
23171 left = r2, right = r1;
23172
23173 /* X0 of the intersection is right.x0, if this is inside R1,
23174 otherwise there is no intersection. */
23175 if (right->x <= left->x + left->width)
23176 {
23177 result->x = right->x;
23178
23179 /* The right end of the intersection is the minimum of the
23180 the right ends of left and right. */
23181 result->width = (min (left->x + left->width, right->x + right->width)
23182 - result->x);
23183
23184 /* Same game for Y. */
23185 if (r1->y < r2->y)
23186 upper = r1, lower = r2;
23187 else
23188 upper = r2, lower = r1;
23189
23190 /* The upper end of the intersection is lower.y0, if this is inside
23191 of upper. Otherwise, there is no intersection. */
23192 if (lower->y <= upper->y + upper->height)
23193 {
23194 result->y = lower->y;
23195
23196 /* The lower end of the intersection is the minimum of the lower
23197 ends of upper and lower. */
23198 result->height = (min (lower->y + lower->height,
23199 upper->y + upper->height)
23200 - result->y);
23201 intersection_p = 1;
23202 }
23203 }
23204
23205 return intersection_p;
23206 }
23207
23208 #endif /* HAVE_WINDOW_SYSTEM */
23209
23210 \f
23211 /***********************************************************************
23212 Initialization
23213 ***********************************************************************/
23214
23215 void
23216 syms_of_xdisp ()
23217 {
23218 Vwith_echo_area_save_vector = Qnil;
23219 staticpro (&Vwith_echo_area_save_vector);
23220
23221 Vmessage_stack = Qnil;
23222 staticpro (&Vmessage_stack);
23223
23224 Qinhibit_redisplay = intern ("inhibit-redisplay");
23225 staticpro (&Qinhibit_redisplay);
23226
23227 message_dolog_marker1 = Fmake_marker ();
23228 staticpro (&message_dolog_marker1);
23229 message_dolog_marker2 = Fmake_marker ();
23230 staticpro (&message_dolog_marker2);
23231 message_dolog_marker3 = Fmake_marker ();
23232 staticpro (&message_dolog_marker3);
23233
23234 #if GLYPH_DEBUG
23235 defsubr (&Sdump_frame_glyph_matrix);
23236 defsubr (&Sdump_glyph_matrix);
23237 defsubr (&Sdump_glyph_row);
23238 defsubr (&Sdump_tool_bar_row);
23239 defsubr (&Strace_redisplay);
23240 defsubr (&Strace_to_stderr);
23241 #endif
23242 #ifdef HAVE_WINDOW_SYSTEM
23243 defsubr (&Stool_bar_lines_needed);
23244 defsubr (&Slookup_image_map);
23245 #endif
23246 defsubr (&Sformat_mode_line);
23247
23248 staticpro (&Qmenu_bar_update_hook);
23249 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23250
23251 staticpro (&Qoverriding_terminal_local_map);
23252 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23253
23254 staticpro (&Qoverriding_local_map);
23255 Qoverriding_local_map = intern ("overriding-local-map");
23256
23257 staticpro (&Qwindow_scroll_functions);
23258 Qwindow_scroll_functions = intern ("window-scroll-functions");
23259
23260 staticpro (&Qredisplay_end_trigger_functions);
23261 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23262
23263 staticpro (&Qinhibit_point_motion_hooks);
23264 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23265
23266 QCdata = intern (":data");
23267 staticpro (&QCdata);
23268 Qdisplay = intern ("display");
23269 staticpro (&Qdisplay);
23270 Qspace_width = intern ("space-width");
23271 staticpro (&Qspace_width);
23272 Qraise = intern ("raise");
23273 staticpro (&Qraise);
23274 Qslice = intern ("slice");
23275 staticpro (&Qslice);
23276 Qspace = intern ("space");
23277 staticpro (&Qspace);
23278 Qmargin = intern ("margin");
23279 staticpro (&Qmargin);
23280 Qpointer = intern ("pointer");
23281 staticpro (&Qpointer);
23282 Qleft_margin = intern ("left-margin");
23283 staticpro (&Qleft_margin);
23284 Qright_margin = intern ("right-margin");
23285 staticpro (&Qright_margin);
23286 Qcenter = intern ("center");
23287 staticpro (&Qcenter);
23288 Qline_height = intern ("line-height");
23289 staticpro (&Qline_height);
23290 QCalign_to = intern (":align-to");
23291 staticpro (&QCalign_to);
23292 QCrelative_width = intern (":relative-width");
23293 staticpro (&QCrelative_width);
23294 QCrelative_height = intern (":relative-height");
23295 staticpro (&QCrelative_height);
23296 QCeval = intern (":eval");
23297 staticpro (&QCeval);
23298 QCpropertize = intern (":propertize");
23299 staticpro (&QCpropertize);
23300 QCfile = intern (":file");
23301 staticpro (&QCfile);
23302 Qfontified = intern ("fontified");
23303 staticpro (&Qfontified);
23304 Qfontification_functions = intern ("fontification-functions");
23305 staticpro (&Qfontification_functions);
23306 Qtrailing_whitespace = intern ("trailing-whitespace");
23307 staticpro (&Qtrailing_whitespace);
23308 Qescape_glyph = intern ("escape-glyph");
23309 staticpro (&Qescape_glyph);
23310 Qnobreak_space = intern ("nobreak-space");
23311 staticpro (&Qnobreak_space);
23312 Qimage = intern ("image");
23313 staticpro (&Qimage);
23314 QCmap = intern (":map");
23315 staticpro (&QCmap);
23316 QCpointer = intern (":pointer");
23317 staticpro (&QCpointer);
23318 Qrect = intern ("rect");
23319 staticpro (&Qrect);
23320 Qcircle = intern ("circle");
23321 staticpro (&Qcircle);
23322 Qpoly = intern ("poly");
23323 staticpro (&Qpoly);
23324 Qmessage_truncate_lines = intern ("message-truncate-lines");
23325 staticpro (&Qmessage_truncate_lines);
23326 Qgrow_only = intern ("grow-only");
23327 staticpro (&Qgrow_only);
23328 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23329 staticpro (&Qinhibit_menubar_update);
23330 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23331 staticpro (&Qinhibit_eval_during_redisplay);
23332 Qposition = intern ("position");
23333 staticpro (&Qposition);
23334 Qbuffer_position = intern ("buffer-position");
23335 staticpro (&Qbuffer_position);
23336 Qobject = intern ("object");
23337 staticpro (&Qobject);
23338 Qbar = intern ("bar");
23339 staticpro (&Qbar);
23340 Qhbar = intern ("hbar");
23341 staticpro (&Qhbar);
23342 Qbox = intern ("box");
23343 staticpro (&Qbox);
23344 Qhollow = intern ("hollow");
23345 staticpro (&Qhollow);
23346 Qhand = intern ("hand");
23347 staticpro (&Qhand);
23348 Qarrow = intern ("arrow");
23349 staticpro (&Qarrow);
23350 Qtext = intern ("text");
23351 staticpro (&Qtext);
23352 Qrisky_local_variable = intern ("risky-local-variable");
23353 staticpro (&Qrisky_local_variable);
23354 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23355 staticpro (&Qinhibit_free_realized_faces);
23356
23357 list_of_error = Fcons (Fcons (intern ("error"),
23358 Fcons (intern ("void-variable"), Qnil)),
23359 Qnil);
23360 staticpro (&list_of_error);
23361
23362 Qlast_arrow_position = intern ("last-arrow-position");
23363 staticpro (&Qlast_arrow_position);
23364 Qlast_arrow_string = intern ("last-arrow-string");
23365 staticpro (&Qlast_arrow_string);
23366
23367 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23368 staticpro (&Qoverlay_arrow_string);
23369 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23370 staticpro (&Qoverlay_arrow_bitmap);
23371
23372 echo_buffer[0] = echo_buffer[1] = Qnil;
23373 staticpro (&echo_buffer[0]);
23374 staticpro (&echo_buffer[1]);
23375
23376 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23377 staticpro (&echo_area_buffer[0]);
23378 staticpro (&echo_area_buffer[1]);
23379
23380 Vmessages_buffer_name = build_string ("*Messages*");
23381 staticpro (&Vmessages_buffer_name);
23382
23383 mode_line_proptrans_alist = Qnil;
23384 staticpro (&mode_line_proptrans_alist);
23385 mode_line_string_list = Qnil;
23386 staticpro (&mode_line_string_list);
23387 mode_line_string_face = Qnil;
23388 staticpro (&mode_line_string_face);
23389 mode_line_string_face_prop = Qnil;
23390 staticpro (&mode_line_string_face_prop);
23391 Vmode_line_unwind_vector = Qnil;
23392 staticpro (&Vmode_line_unwind_vector);
23393
23394 help_echo_string = Qnil;
23395 staticpro (&help_echo_string);
23396 help_echo_object = Qnil;
23397 staticpro (&help_echo_object);
23398 help_echo_window = Qnil;
23399 staticpro (&help_echo_window);
23400 previous_help_echo_string = Qnil;
23401 staticpro (&previous_help_echo_string);
23402 help_echo_pos = -1;
23403
23404 #ifdef HAVE_WINDOW_SYSTEM
23405 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23406 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23407 For example, if a block cursor is over a tab, it will be drawn as
23408 wide as that tab on the display. */);
23409 x_stretch_cursor_p = 0;
23410 #endif
23411
23412 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23413 doc: /* *Non-nil means highlight trailing whitespace.
23414 The face used for trailing whitespace is `trailing-whitespace'. */);
23415 Vshow_trailing_whitespace = Qnil;
23416
23417 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23418 doc: /* *Control highlighting of nobreak space and soft hyphen.
23419 A value of t means highlight the character itself (for nobreak space,
23420 use face `nobreak-space').
23421 A value of nil means no highlighting.
23422 Other values mean display the escape glyph followed by an ordinary
23423 space or ordinary hyphen. */);
23424 Vnobreak_char_display = Qt;
23425
23426 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23427 doc: /* *The pointer shape to show in void text areas.
23428 A value of nil means to show the text pointer. Other options are `arrow',
23429 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23430 Vvoid_text_area_pointer = Qarrow;
23431
23432 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23433 doc: /* Non-nil means don't actually do any redisplay.
23434 This is used for internal purposes. */);
23435 Vinhibit_redisplay = Qnil;
23436
23437 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23438 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23439 Vglobal_mode_string = Qnil;
23440
23441 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23442 doc: /* Marker for where to display an arrow on top of the buffer text.
23443 This must be the beginning of a line in order to work.
23444 See also `overlay-arrow-string'. */);
23445 Voverlay_arrow_position = Qnil;
23446
23447 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23448 doc: /* String to display as an arrow in non-window frames.
23449 See also `overlay-arrow-position'. */);
23450 Voverlay_arrow_string = build_string ("=>");
23451
23452 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23453 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23454 The symbols on this list are examined during redisplay to determine
23455 where to display overlay arrows. */);
23456 Voverlay_arrow_variable_list
23457 = Fcons (intern ("overlay-arrow-position"), Qnil);
23458
23459 DEFVAR_INT ("scroll-step", &scroll_step,
23460 doc: /* *The number of lines to try scrolling a window by when point moves out.
23461 If that fails to bring point back on frame, point is centered instead.
23462 If this is zero, point is always centered after it moves off frame.
23463 If you want scrolling to always be a line at a time, you should set
23464 `scroll-conservatively' to a large value rather than set this to 1. */);
23465
23466 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23467 doc: /* *Scroll up to this many lines, to bring point back on screen.
23468 A value of zero means to scroll the text to center point vertically
23469 in the window. */);
23470 scroll_conservatively = 0;
23471
23472 DEFVAR_INT ("scroll-margin", &scroll_margin,
23473 doc: /* *Number of lines of margin at the top and bottom of a window.
23474 Recenter the window whenever point gets within this many lines
23475 of the top or bottom of the window. */);
23476 scroll_margin = 0;
23477
23478 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23479 doc: /* Pixels per inch value for non-window system displays.
23480 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23481 Vdisplay_pixels_per_inch = make_float (72.0);
23482
23483 #if GLYPH_DEBUG
23484 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23485 #endif
23486
23487 DEFVAR_BOOL ("truncate-partial-width-windows",
23488 &truncate_partial_width_windows,
23489 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23490 truncate_partial_width_windows = 1;
23491
23492 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23493 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23494 Any other value means to use the appropriate face, `mode-line',
23495 `header-line', or `menu' respectively. */);
23496 mode_line_inverse_video = 1;
23497
23498 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23499 doc: /* *Maximum buffer size for which line number should be displayed.
23500 If the buffer is bigger than this, the line number does not appear
23501 in the mode line. A value of nil means no limit. */);
23502 Vline_number_display_limit = Qnil;
23503
23504 DEFVAR_INT ("line-number-display-limit-width",
23505 &line_number_display_limit_width,
23506 doc: /* *Maximum line width (in characters) for line number display.
23507 If the average length of the lines near point is bigger than this, then the
23508 line number may be omitted from the mode line. */);
23509 line_number_display_limit_width = 200;
23510
23511 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23512 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23513 highlight_nonselected_windows = 0;
23514
23515 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23516 doc: /* Non-nil if more than one frame is visible on this display.
23517 Minibuffer-only frames don't count, but iconified frames do.
23518 This variable is not guaranteed to be accurate except while processing
23519 `frame-title-format' and `icon-title-format'. */);
23520
23521 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23522 doc: /* Template for displaying the title bar of visible frames.
23523 \(Assuming the window manager supports this feature.)
23524 This variable has the same structure as `mode-line-format' (which see),
23525 and is used only on frames for which no explicit name has been set
23526 \(see `modify-frame-parameters'). */);
23527
23528 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23529 doc: /* Template for displaying the title bar of an iconified frame.
23530 \(Assuming the window manager supports this feature.)
23531 This variable has the same structure as `mode-line-format' (which see),
23532 and is used only on frames for which no explicit name has been set
23533 \(see `modify-frame-parameters'). */);
23534 Vicon_title_format
23535 = Vframe_title_format
23536 = Fcons (intern ("multiple-frames"),
23537 Fcons (build_string ("%b"),
23538 Fcons (Fcons (empty_string,
23539 Fcons (intern ("invocation-name"),
23540 Fcons (build_string ("@"),
23541 Fcons (intern ("system-name"),
23542 Qnil)))),
23543 Qnil)));
23544
23545 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23546 doc: /* Maximum number of lines to keep in the message log buffer.
23547 If nil, disable message logging. If t, log messages but don't truncate
23548 the buffer when it becomes large. */);
23549 Vmessage_log_max = make_number (50);
23550
23551 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23552 doc: /* Functions called before redisplay, if window sizes have changed.
23553 The value should be a list of functions that take one argument.
23554 Just before redisplay, for each frame, if any of its windows have changed
23555 size since the last redisplay, or have been split or deleted,
23556 all the functions in the list are called, with the frame as argument. */);
23557 Vwindow_size_change_functions = Qnil;
23558
23559 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23560 doc: /* List of functions to call before redisplaying a window with scrolling.
23561 Each function is called with two arguments, the window
23562 and its new display-start position. Note that the value of `window-end'
23563 is not valid when these functions are called. */);
23564 Vwindow_scroll_functions = Qnil;
23565
23566 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23567 doc: /* Functions called when redisplay of a window reaches the end trigger.
23568 Each function is called with two arguments, the window and the end trigger value.
23569 See `set-window-redisplay-end-trigger'. */);
23570 Vredisplay_end_trigger_functions = Qnil;
23571
23572 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23573 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23574 mouse_autoselect_window = 0;
23575
23576 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23577 doc: /* *Non-nil means automatically resize tool-bars.
23578 This increases a tool-bar's height if not all tool-bar items are visible.
23579 It decreases a tool-bar's height when it would display blank lines
23580 otherwise. */);
23581 auto_resize_tool_bars_p = 1;
23582
23583 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23584 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23585 auto_raise_tool_bar_buttons_p = 1;
23586
23587 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23588 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23589 make_cursor_line_fully_visible_p = 1;
23590
23591 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23592 doc: /* *Margin around tool-bar buttons in pixels.
23593 If an integer, use that for both horizontal and vertical margins.
23594 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23595 HORZ specifying the horizontal margin, and VERT specifying the
23596 vertical margin. */);
23597 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23598
23599 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23600 doc: /* *Relief thickness of tool-bar buttons. */);
23601 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23602
23603 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23604 doc: /* List of functions to call to fontify regions of text.
23605 Each function is called with one argument POS. Functions must
23606 fontify a region starting at POS in the current buffer, and give
23607 fontified regions the property `fontified'. */);
23608 Vfontification_functions = Qnil;
23609 Fmake_variable_buffer_local (Qfontification_functions);
23610
23611 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23612 &unibyte_display_via_language_environment,
23613 doc: /* *Non-nil means display unibyte text according to language environment.
23614 Specifically this means that unibyte non-ASCII characters
23615 are displayed by converting them to the equivalent multibyte characters
23616 according to the current language environment. As a result, they are
23617 displayed according to the current fontset. */);
23618 unibyte_display_via_language_environment = 0;
23619
23620 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23621 doc: /* *Maximum height for resizing mini-windows.
23622 If a float, it specifies a fraction of the mini-window frame's height.
23623 If an integer, it specifies a number of lines. */);
23624 Vmax_mini_window_height = make_float (0.25);
23625
23626 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23627 doc: /* *How to resize mini-windows.
23628 A value of nil means don't automatically resize mini-windows.
23629 A value of t means resize them to fit the text displayed in them.
23630 A value of `grow-only', the default, means let mini-windows grow
23631 only, until their display becomes empty, at which point the windows
23632 go back to their normal size. */);
23633 Vresize_mini_windows = Qgrow_only;
23634
23635 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23636 doc: /* Alist specifying how to blink the cursor off.
23637 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23638 `cursor-type' frame-parameter or variable equals ON-STATE,
23639 comparing using `equal', Emacs uses OFF-STATE to specify
23640 how to blink it off. ON-STATE and OFF-STATE are values for
23641 the `cursor-type' frame parameter.
23642
23643 If a frame's ON-STATE has no entry in this list,
23644 the frame's other specifications determine how to blink the cursor off. */);
23645 Vblink_cursor_alist = Qnil;
23646
23647 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23648 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23649 automatic_hscrolling_p = 1;
23650
23651 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23652 doc: /* *How many columns away from the window edge point is allowed to get
23653 before automatic hscrolling will horizontally scroll the window. */);
23654 hscroll_margin = 5;
23655
23656 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23657 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23658 When point is less than `automatic-hscroll-margin' columns from the window
23659 edge, automatic hscrolling will scroll the window by the amount of columns
23660 determined by this variable. If its value is a positive integer, scroll that
23661 many columns. If it's a positive floating-point number, it specifies the
23662 fraction of the window's width to scroll. If it's nil or zero, point will be
23663 centered horizontally after the scroll. Any other value, including negative
23664 numbers, are treated as if the value were zero.
23665
23666 Automatic hscrolling always moves point outside the scroll margin, so if
23667 point was more than scroll step columns inside the margin, the window will
23668 scroll more than the value given by the scroll step.
23669
23670 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23671 and `scroll-right' overrides this variable's effect. */);
23672 Vhscroll_step = make_number (0);
23673
23674 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23675 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23676 Bind this around calls to `message' to let it take effect. */);
23677 message_truncate_lines = 0;
23678
23679 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23680 doc: /* Normal hook run to update the menu bar definitions.
23681 Redisplay runs this hook before it redisplays the menu bar.
23682 This is used to update submenus such as Buffers,
23683 whose contents depend on various data. */);
23684 Vmenu_bar_update_hook = Qnil;
23685
23686 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23687 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23688 inhibit_menubar_update = 0;
23689
23690 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23691 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23692 inhibit_eval_during_redisplay = 0;
23693
23694 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23695 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23696 inhibit_free_realized_faces = 0;
23697
23698 #if GLYPH_DEBUG
23699 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23700 doc: /* Inhibit try_window_id display optimization. */);
23701 inhibit_try_window_id = 0;
23702
23703 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23704 doc: /* Inhibit try_window_reusing display optimization. */);
23705 inhibit_try_window_reusing = 0;
23706
23707 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23708 doc: /* Inhibit try_cursor_movement display optimization. */);
23709 inhibit_try_cursor_movement = 0;
23710 #endif /* GLYPH_DEBUG */
23711 }
23712
23713
23714 /* Initialize this module when Emacs starts. */
23715
23716 void
23717 init_xdisp ()
23718 {
23719 Lisp_Object root_window;
23720 struct window *mini_w;
23721
23722 current_header_line_height = current_mode_line_height = -1;
23723
23724 CHARPOS (this_line_start_pos) = 0;
23725
23726 mini_w = XWINDOW (minibuf_window);
23727 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23728
23729 if (!noninteractive)
23730 {
23731 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23732 int i;
23733
23734 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23735 set_window_height (root_window,
23736 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23737 0);
23738 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23739 set_window_height (minibuf_window, 1, 0);
23740
23741 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23742 mini_w->total_cols = make_number (FRAME_COLS (f));
23743
23744 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23745 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23746 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23747
23748 /* The default ellipsis glyphs `...'. */
23749 for (i = 0; i < 3; ++i)
23750 default_invis_vector[i] = make_number ('.');
23751 }
23752
23753 {
23754 /* Allocate the buffer for frame titles.
23755 Also used for `format-mode-line'. */
23756 int size = 100;
23757 mode_line_noprop_buf = (char *) xmalloc (size);
23758 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23759 mode_line_noprop_ptr = mode_line_noprop_buf;
23760 mode_line_target = MODE_LINE_DISPLAY;
23761 }
23762
23763 help_echo_showing_p = 0;
23764 }
23765
23766
23767 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23768 (do not change this comment) */