]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(handle_invisible_prop): After setting up an ellipsis, return HANDLED_RETURN.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
275
276 Lisp_Object Vtool_bar_border;
277
278 /* Margin around tool bar buttons in pixels. */
279
280 Lisp_Object Vtool_bar_button_margin;
281
282 /* Thickness of shadow to draw around tool bar buttons. */
283
284 EMACS_INT tool_bar_button_relief;
285
286 /* Non-nil means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain.
288
289 If value is `grow-only', only make tool-bar bigger. */
290
291 Lisp_Object Vauto_resize_tool_bars;
292
293 /* Non-zero means draw block and hollow cursor as wide as the glyph
294 under it. For example, if a block cursor is over a tab, it will be
295 drawn as wide as that tab on the display. */
296
297 int x_stretch_cursor_p;
298
299 /* Non-nil means don't actually do any redisplay. */
300
301 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
302
303 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
304
305 int inhibit_eval_during_redisplay;
306
307 /* Names of text properties relevant for redisplay. */
308
309 Lisp_Object Qdisplay;
310 extern Lisp_Object Qface, Qinvisible, Qwidth;
311
312 /* Symbols used in text property values. */
313
314 Lisp_Object Vdisplay_pixels_per_inch;
315 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
316 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
317 Lisp_Object Qslice;
318 Lisp_Object Qcenter;
319 Lisp_Object Qmargin, Qpointer;
320 Lisp_Object Qline_height;
321 extern Lisp_Object Qheight;
322 extern Lisp_Object QCwidth, QCheight, QCascent;
323 extern Lisp_Object Qscroll_bar;
324 extern Lisp_Object Qcursor;
325
326 /* Non-nil means highlight trailing whitespace. */
327
328 Lisp_Object Vshow_trailing_whitespace;
329
330 /* Non-nil means escape non-break space and hyphens. */
331
332 Lisp_Object Vnobreak_char_display;
333
334 #ifdef HAVE_WINDOW_SYSTEM
335 extern Lisp_Object Voverflow_newline_into_fringe;
336
337 /* Test if overflow newline into fringe. Called with iterator IT
338 at or past right window margin, and with IT->current_x set. */
339
340 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
341 (!NILP (Voverflow_newline_into_fringe) \
342 && FRAME_WINDOW_P (it->f) \
343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
344 && it->current_x == it->last_visible_x)
345
346 #endif /* HAVE_WINDOW_SYSTEM */
347
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
351
352 Lisp_Object Vvoid_text_area_pointer;
353
354 /* Name of the face used to highlight trailing whitespace. */
355
356 Lisp_Object Qtrailing_whitespace;
357
358 /* Name and number of the face used to highlight escape glyphs. */
359
360 Lisp_Object Qescape_glyph;
361
362 /* Name and number of the face used to highlight non-breaking spaces. */
363
364 Lisp_Object Qnobreak_space;
365
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
368
369 Lisp_Object Qimage;
370
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
374
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
377
378 int noninteractive_need_newline;
379
380 /* Non-zero means print newline to message log before next message. */
381
382 static int message_log_need_newline;
383
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
390 \f
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
395
396 static struct text_pos this_line_start_pos;
397
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
400
401 static struct text_pos this_line_end_pos;
402
403 /* The vertical positions and the height of this line. */
404
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
408
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
411
412 static int this_line_start_x;
413
414 /* Buffer that this_line_.* variables are referring to. */
415
416 static struct buffer *this_line_buffer;
417
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
420
421 int truncate_partial_width_windows;
422
423 /* A flag to control how to display unibyte 8-bit character. */
424
425 int unibyte_display_via_language_environment;
426
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
430
431 int multiple_frames;
432
433 Lisp_Object Vglobal_mode_string;
434
435
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
439
440 Lisp_Object Voverlay_arrow_variable_list;
441
442 /* Marker for where to display an arrow on top of the buffer text. */
443
444 Lisp_Object Voverlay_arrow_position;
445
446 /* String to display for the arrow. Only used on terminal frames. */
447
448 Lisp_Object Voverlay_arrow_string;
449
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
454
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
456
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
459
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
461
462 /* Like mode-line-format, but for the title bar on a visible frame. */
463
464 Lisp_Object Vframe_title_format;
465
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
467
468 Lisp_Object Vicon_title_format;
469
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
473
474 static Lisp_Object Vwindow_size_change_functions;
475
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
477
478 /* Nonzero if an overlay arrow has been displayed in this window. */
479
480 static int overlay_arrow_seen;
481
482 /* Nonzero means highlight the region even in nonselected windows. */
483
484 int highlight_nonselected_windows;
485
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
488
489 static EMACS_INT scroll_step;
490
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
493
494 static EMACS_INT scroll_conservatively;
495
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
500
501 EMACS_INT scroll_margin;
502
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
506
507 int buffer_shared;
508
509 /* Vector containing glyphs for an ellipsis `...'. */
510
511 static Lisp_Object default_invis_vector[3];
512
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
516
517 This variable is deprecated. */
518
519 int mode_line_inverse_video;
520
521 /* Prompt to display in front of the mini-buffer contents. */
522
523 Lisp_Object minibuf_prompt;
524
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
527
528 int minibuf_prompt_width;
529
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
533
534 Lisp_Object echo_area_window;
535
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
540
541 Lisp_Object Vmessage_stack;
542
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
545
546 int message_enable_multibyte;
547
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
549
550 int update_mode_lines;
551
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
554
555 int windows_or_buffers_changed;
556
557 /* Nonzero means a frame's cursor type has been changed. */
558
559 int cursor_type_changed;
560
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
563
564 int line_number_displayed;
565
566 /* Maximum buffer size for which to display line numbers. */
567
568 Lisp_Object Vline_number_display_limit;
569
570 /* Line width to consider when repositioning for line number display. */
571
572 static EMACS_INT line_number_display_limit_width;
573
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
576
577 Lisp_Object Vmessage_log_max;
578
579 /* The name of the *Messages* buffer, a string. */
580
581 static Lisp_Object Vmessages_buffer_name;
582
583 /* Index 0 is the buffer that holds the current (desired) echo area message,
584 or nil if none is desired right now.
585
586 Index 1 is the buffer that holds the previously displayed echo area message,
587 or nil to indicate no message. This is normally what's on the screen now.
588
589 These two can point to the same buffer. That happens when the last
590 message output by the user (or made by echoing) has been displayed. */
591
592 Lisp_Object echo_area_buffer[2];
593
594 /* Permanent pointers to the two buffers that are used for echo area
595 purposes. Once the two buffers are made, and their pointers are
596 placed here, these two slots remain unchanged unless those buffers
597 need to be created afresh. */
598
599 static Lisp_Object echo_buffer[2];
600
601 /* A vector saved used in with_area_buffer to reduce consing. */
602
603 static Lisp_Object Vwith_echo_area_save_vector;
604
605 /* Non-zero means display_echo_area should display the last echo area
606 message again. Set by redisplay_preserve_echo_area. */
607
608 static int display_last_displayed_message_p;
609
610 /* Nonzero if echo area is being used by print; zero if being used by
611 message. */
612
613 int message_buf_print;
614
615 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
616
617 Lisp_Object Qinhibit_menubar_update;
618 int inhibit_menubar_update;
619
620 /* When evaluating expressions from menu bar items (enable conditions,
621 for instance), this is the frame they are being processed for. */
622
623 Lisp_Object Vmenu_updating_frame;
624
625 /* Maximum height for resizing mini-windows. Either a float
626 specifying a fraction of the available height, or an integer
627 specifying a number of lines. */
628
629 Lisp_Object Vmax_mini_window_height;
630
631 /* Non-zero means messages should be displayed with truncated
632 lines instead of being continued. */
633
634 int message_truncate_lines;
635 Lisp_Object Qmessage_truncate_lines;
636
637 /* Set to 1 in clear_message to make redisplay_internal aware
638 of an emptied echo area. */
639
640 static int message_cleared_p;
641
642 /* How to blink the default frame cursor off. */
643 Lisp_Object Vblink_cursor_alist;
644
645 /* A scratch glyph row with contents used for generating truncation
646 glyphs. Also used in direct_output_for_insert. */
647
648 #define MAX_SCRATCH_GLYPHS 100
649 struct glyph_row scratch_glyph_row;
650 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
651
652 /* Ascent and height of the last line processed by move_it_to. */
653
654 static int last_max_ascent, last_height;
655
656 /* Non-zero if there's a help-echo in the echo area. */
657
658 int help_echo_showing_p;
659
660 /* If >= 0, computed, exact values of mode-line and header-line height
661 to use in the macros CURRENT_MODE_LINE_HEIGHT and
662 CURRENT_HEADER_LINE_HEIGHT. */
663
664 int current_mode_line_height, current_header_line_height;
665
666 /* The maximum distance to look ahead for text properties. Values
667 that are too small let us call compute_char_face and similar
668 functions too often which is expensive. Values that are too large
669 let us call compute_char_face and alike too often because we
670 might not be interested in text properties that far away. */
671
672 #define TEXT_PROP_DISTANCE_LIMIT 100
673
674 #if GLYPH_DEBUG
675
676 /* Variables to turn off display optimizations from Lisp. */
677
678 int inhibit_try_window_id, inhibit_try_window_reusing;
679 int inhibit_try_cursor_movement;
680
681 /* Non-zero means print traces of redisplay if compiled with
682 GLYPH_DEBUG != 0. */
683
684 int trace_redisplay_p;
685
686 #endif /* GLYPH_DEBUG */
687
688 #ifdef DEBUG_TRACE_MOVE
689 /* Non-zero means trace with TRACE_MOVE to stderr. */
690 int trace_move;
691
692 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
693 #else
694 #define TRACE_MOVE(x) (void) 0
695 #endif
696
697 /* Non-zero means automatically scroll windows horizontally to make
698 point visible. */
699
700 int automatic_hscrolling_p;
701
702 /* How close to the margin can point get before the window is scrolled
703 horizontally. */
704 EMACS_INT hscroll_margin;
705
706 /* How much to scroll horizontally when point is inside the above margin. */
707 Lisp_Object Vhscroll_step;
708
709 /* The variable `resize-mini-windows'. If nil, don't resize
710 mini-windows. If t, always resize them to fit the text they
711 display. If `grow-only', let mini-windows grow only until they
712 become empty. */
713
714 Lisp_Object Vresize_mini_windows;
715
716 /* Buffer being redisplayed -- for redisplay_window_error. */
717
718 struct buffer *displayed_buffer;
719
720 /* Space between overline and text. */
721
722 EMACS_INT overline_margin;
723
724 /* Value returned from text property handlers (see below). */
725
726 enum prop_handled
727 {
728 HANDLED_NORMALLY,
729 HANDLED_RECOMPUTE_PROPS,
730 HANDLED_OVERLAY_STRING_CONSUMED,
731 HANDLED_RETURN
732 };
733
734 /* A description of text properties that redisplay is interested
735 in. */
736
737 struct props
738 {
739 /* The name of the property. */
740 Lisp_Object *name;
741
742 /* A unique index for the property. */
743 enum prop_idx idx;
744
745 /* A handler function called to set up iterator IT from the property
746 at IT's current position. Value is used to steer handle_stop. */
747 enum prop_handled (*handler) P_ ((struct it *it));
748 };
749
750 static enum prop_handled handle_face_prop P_ ((struct it *));
751 static enum prop_handled handle_invisible_prop P_ ((struct it *));
752 static enum prop_handled handle_display_prop P_ ((struct it *));
753 static enum prop_handled handle_composition_prop P_ ((struct it *));
754 static enum prop_handled handle_overlay_change P_ ((struct it *));
755 static enum prop_handled handle_fontified_prop P_ ((struct it *));
756
757 /* Properties handled by iterators. */
758
759 static struct props it_props[] =
760 {
761 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
762 /* Handle `face' before `display' because some sub-properties of
763 `display' need to know the face. */
764 {&Qface, FACE_PROP_IDX, handle_face_prop},
765 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
766 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
767 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
768 {NULL, 0, NULL}
769 };
770
771 /* Value is the position described by X. If X is a marker, value is
772 the marker_position of X. Otherwise, value is X. */
773
774 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
775
776 /* Enumeration returned by some move_it_.* functions internally. */
777
778 enum move_it_result
779 {
780 /* Not used. Undefined value. */
781 MOVE_UNDEFINED,
782
783 /* Move ended at the requested buffer position or ZV. */
784 MOVE_POS_MATCH_OR_ZV,
785
786 /* Move ended at the requested X pixel position. */
787 MOVE_X_REACHED,
788
789 /* Move within a line ended at the end of a line that must be
790 continued. */
791 MOVE_LINE_CONTINUED,
792
793 /* Move within a line ended at the end of a line that would
794 be displayed truncated. */
795 MOVE_LINE_TRUNCATED,
796
797 /* Move within a line ended at a line end. */
798 MOVE_NEWLINE_OR_CR
799 };
800
801 /* This counter is used to clear the face cache every once in a while
802 in redisplay_internal. It is incremented for each redisplay.
803 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
804 cleared. */
805
806 #define CLEAR_FACE_CACHE_COUNT 500
807 static int clear_face_cache_count;
808
809 /* Similarly for the image cache. */
810
811 #ifdef HAVE_WINDOW_SYSTEM
812 #define CLEAR_IMAGE_CACHE_COUNT 101
813 static int clear_image_cache_count;
814 #endif
815
816 /* Record the previous terminal frame we displayed. */
817
818 static struct frame *previous_terminal_frame;
819
820 /* Non-zero while redisplay_internal is in progress. */
821
822 int redisplaying_p;
823
824 /* Non-zero means don't free realized faces. Bound while freeing
825 realized faces is dangerous because glyph matrices might still
826 reference them. */
827
828 int inhibit_free_realized_faces;
829 Lisp_Object Qinhibit_free_realized_faces;
830
831 /* If a string, XTread_socket generates an event to display that string.
832 (The display is done in read_char.) */
833
834 Lisp_Object help_echo_string;
835 Lisp_Object help_echo_window;
836 Lisp_Object help_echo_object;
837 int help_echo_pos;
838
839 /* Temporary variable for XTread_socket. */
840
841 Lisp_Object previous_help_echo_string;
842
843 /* Null glyph slice */
844
845 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
846
847 \f
848 /* Function prototypes. */
849
850 static void setup_for_ellipsis P_ ((struct it *, int));
851 static void mark_window_display_accurate_1 P_ ((struct window *, int));
852 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
853 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
854 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
855 static int redisplay_mode_lines P_ ((Lisp_Object, int));
856 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
857
858 #if 0
859 static int invisible_text_between_p P_ ((struct it *, int, int));
860 #endif
861
862 static void pint2str P_ ((char *, int, int));
863 static void pint2hrstr P_ ((char *, int, int));
864 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
865 struct text_pos));
866 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
867 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
868 static void store_mode_line_noprop_char P_ ((char));
869 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
870 static void x_consider_frame_title P_ ((Lisp_Object));
871 static void handle_stop P_ ((struct it *));
872 static int tool_bar_lines_needed P_ ((struct frame *, int *));
873 static int single_display_spec_intangible_p P_ ((Lisp_Object));
874 static void ensure_echo_area_buffers P_ ((void));
875 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
876 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
877 static int with_echo_area_buffer P_ ((struct window *, int,
878 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
879 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static void clear_garbaged_frames P_ ((void));
881 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
883 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int display_echo_area P_ ((struct window *));
885 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
886 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
887 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
888 static int string_char_and_length P_ ((const unsigned char *, int, int *));
889 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
890 struct text_pos));
891 static int compute_window_start_on_continuation_line P_ ((struct window *));
892 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
893 static void insert_left_trunc_glyphs P_ ((struct it *));
894 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
895 Lisp_Object));
896 static void extend_face_to_end_of_line P_ ((struct it *));
897 static int append_space_for_newline P_ ((struct it *, int));
898 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
899 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
900 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
901 static int trailing_whitespace_p P_ ((int));
902 static int message_log_check_duplicate P_ ((int, int, int, int));
903 static void push_it P_ ((struct it *));
904 static void pop_it P_ ((struct it *));
905 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
906 static void select_frame_for_redisplay P_ ((Lisp_Object));
907 static void redisplay_internal P_ ((int));
908 static int echo_area_display P_ ((int));
909 static void redisplay_windows P_ ((Lisp_Object));
910 static void redisplay_window P_ ((Lisp_Object, int));
911 static Lisp_Object redisplay_window_error ();
912 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
913 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
914 static int update_menu_bar P_ ((struct frame *, int, int));
915 static int try_window_reusing_current_matrix P_ ((struct window *));
916 static int try_window_id P_ ((struct window *));
917 static int display_line P_ ((struct it *));
918 static int display_mode_lines P_ ((struct window *));
919 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
920 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
921 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
922 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
923 static void display_menu_bar P_ ((struct window *));
924 static int display_count_lines P_ ((int, int, int, int, int *));
925 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
926 int, int, struct it *, int, int, int, int));
927 static void compute_line_metrics P_ ((struct it *));
928 static void run_redisplay_end_trigger_hook P_ ((struct it *));
929 static int get_overlay_strings P_ ((struct it *, int));
930 static int get_overlay_strings_1 P_ ((struct it *, int, int));
931 static void next_overlay_string P_ ((struct it *));
932 static void reseat P_ ((struct it *, struct text_pos, int));
933 static void reseat_1 P_ ((struct it *, struct text_pos, int));
934 static void back_to_previous_visible_line_start P_ ((struct it *));
935 void reseat_at_previous_visible_line_start P_ ((struct it *));
936 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
937 static int next_element_from_ellipsis P_ ((struct it *));
938 static int next_element_from_display_vector P_ ((struct it *));
939 static int next_element_from_string P_ ((struct it *));
940 static int next_element_from_c_string P_ ((struct it *));
941 static int next_element_from_buffer P_ ((struct it *));
942 static int next_element_from_composition P_ ((struct it *));
943 static int next_element_from_image P_ ((struct it *));
944 static int next_element_from_stretch P_ ((struct it *));
945 static void load_overlay_strings P_ ((struct it *, int));
946 static int init_from_display_pos P_ ((struct it *, struct window *,
947 struct display_pos *));
948 static void reseat_to_string P_ ((struct it *, unsigned char *,
949 Lisp_Object, int, int, int, int));
950 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
951 int, int, int));
952 void move_it_vertically_backward P_ ((struct it *, int));
953 static void init_to_row_start P_ ((struct it *, struct window *,
954 struct glyph_row *));
955 static int init_to_row_end P_ ((struct it *, struct window *,
956 struct glyph_row *));
957 static void back_to_previous_line_start P_ ((struct it *));
958 static int forward_to_next_line_start P_ ((struct it *, int *));
959 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
960 Lisp_Object, int));
961 static struct text_pos string_pos P_ ((int, Lisp_Object));
962 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
963 static int number_of_chars P_ ((unsigned char *, int));
964 static void compute_stop_pos P_ ((struct it *));
965 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
966 Lisp_Object));
967 static int face_before_or_after_it_pos P_ ((struct it *, int));
968 static int next_overlay_change P_ ((int));
969 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
970 Lisp_Object, struct text_pos *,
971 int));
972 static int underlying_face_id P_ ((struct it *));
973 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
974 struct window *));
975
976 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
977 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
978
979 #ifdef HAVE_WINDOW_SYSTEM
980
981 static void update_tool_bar P_ ((struct frame *, int));
982 static void build_desired_tool_bar_string P_ ((struct frame *f));
983 static int redisplay_tool_bar P_ ((struct frame *));
984 static void display_tool_bar_line P_ ((struct it *, int));
985 static void notice_overwritten_cursor P_ ((struct window *,
986 enum glyph_row_area,
987 int, int, int, int));
988
989
990
991 #endif /* HAVE_WINDOW_SYSTEM */
992
993 \f
994 /***********************************************************************
995 Window display dimensions
996 ***********************************************************************/
997
998 /* Return the bottom boundary y-position for text lines in window W.
999 This is the first y position at which a line cannot start.
1000 It is relative to the top of the window.
1001
1002 This is the height of W minus the height of a mode line, if any. */
1003
1004 INLINE int
1005 window_text_bottom_y (w)
1006 struct window *w;
1007 {
1008 int height = WINDOW_TOTAL_HEIGHT (w);
1009
1010 if (WINDOW_WANTS_MODELINE_P (w))
1011 height -= CURRENT_MODE_LINE_HEIGHT (w);
1012 return height;
1013 }
1014
1015 /* Return the pixel width of display area AREA of window W. AREA < 0
1016 means return the total width of W, not including fringes to
1017 the left and right of the window. */
1018
1019 INLINE int
1020 window_box_width (w, area)
1021 struct window *w;
1022 int area;
1023 {
1024 int cols = XFASTINT (w->total_cols);
1025 int pixels = 0;
1026
1027 if (!w->pseudo_window_p)
1028 {
1029 cols -= WINDOW_SCROLL_BAR_COLS (w);
1030
1031 if (area == TEXT_AREA)
1032 {
1033 if (INTEGERP (w->left_margin_cols))
1034 cols -= XFASTINT (w->left_margin_cols);
1035 if (INTEGERP (w->right_margin_cols))
1036 cols -= XFASTINT (w->right_margin_cols);
1037 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1038 }
1039 else if (area == LEFT_MARGIN_AREA)
1040 {
1041 cols = (INTEGERP (w->left_margin_cols)
1042 ? XFASTINT (w->left_margin_cols) : 0);
1043 pixels = 0;
1044 }
1045 else if (area == RIGHT_MARGIN_AREA)
1046 {
1047 cols = (INTEGERP (w->right_margin_cols)
1048 ? XFASTINT (w->right_margin_cols) : 0);
1049 pixels = 0;
1050 }
1051 }
1052
1053 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1054 }
1055
1056
1057 /* Return the pixel height of the display area of window W, not
1058 including mode lines of W, if any. */
1059
1060 INLINE int
1061 window_box_height (w)
1062 struct window *w;
1063 {
1064 struct frame *f = XFRAME (w->frame);
1065 int height = WINDOW_TOTAL_HEIGHT (w);
1066
1067 xassert (height >= 0);
1068
1069 /* Note: the code below that determines the mode-line/header-line
1070 height is essentially the same as that contained in the macro
1071 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1072 the appropriate glyph row has its `mode_line_p' flag set,
1073 and if it doesn't, uses estimate_mode_line_height instead. */
1074
1075 if (WINDOW_WANTS_MODELINE_P (w))
1076 {
1077 struct glyph_row *ml_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (ml_row && ml_row->mode_line_p)
1082 height -= ml_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1085 }
1086
1087 if (WINDOW_WANTS_HEADER_LINE_P (w))
1088 {
1089 struct glyph_row *hl_row
1090 = (w->current_matrix && w->current_matrix->rows
1091 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1092 : 0);
1093 if (hl_row && hl_row->mode_line_p)
1094 height -= hl_row->height;
1095 else
1096 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1097 }
1098
1099 /* With a very small font and a mode-line that's taller than
1100 default, we might end up with a negative height. */
1101 return max (0, height);
1102 }
1103
1104 /* Return the window-relative coordinate of the left edge of display
1105 area AREA of window W. AREA < 0 means return the left edge of the
1106 whole window, to the right of the left fringe of W. */
1107
1108 INLINE int
1109 window_box_left_offset (w, area)
1110 struct window *w;
1111 int area;
1112 {
1113 int x;
1114
1115 if (w->pseudo_window_p)
1116 return 0;
1117
1118 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1119
1120 if (area == TEXT_AREA)
1121 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1122 + window_box_width (w, LEFT_MARGIN_AREA));
1123 else if (area == RIGHT_MARGIN_AREA)
1124 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1125 + window_box_width (w, LEFT_MARGIN_AREA)
1126 + window_box_width (w, TEXT_AREA)
1127 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1128 ? 0
1129 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1130 else if (area == LEFT_MARGIN_AREA
1131 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1132 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1133
1134 return x;
1135 }
1136
1137
1138 /* Return the window-relative coordinate of the right edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the left of the right fringe of W. */
1141
1142 INLINE int
1143 window_box_right_offset (w, area)
1144 struct window *w;
1145 int area;
1146 {
1147 return window_box_left_offset (w, area) + window_box_width (w, area);
1148 }
1149
1150 /* Return the frame-relative coordinate of the left edge of display
1151 area AREA of window W. AREA < 0 means return the left edge of the
1152 whole window, to the right of the left fringe of W. */
1153
1154 INLINE int
1155 window_box_left (w, area)
1156 struct window *w;
1157 int area;
1158 {
1159 struct frame *f = XFRAME (w->frame);
1160 int x;
1161
1162 if (w->pseudo_window_p)
1163 return FRAME_INTERNAL_BORDER_WIDTH (f);
1164
1165 x = (WINDOW_LEFT_EDGE_X (w)
1166 + window_box_left_offset (w, area));
1167
1168 return x;
1169 }
1170
1171
1172 /* Return the frame-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1175
1176 INLINE int
1177 window_box_right (w, area)
1178 struct window *w;
1179 int area;
1180 {
1181 return window_box_left (w, area) + window_box_width (w, area);
1182 }
1183
1184 /* Get the bounding box of the display area AREA of window W, without
1185 mode lines, in frame-relative coordinates. AREA < 0 means the
1186 whole window, not including the left and right fringes of
1187 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1188 coordinates of the upper-left corner of the box. Return in
1189 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1190
1191 INLINE void
1192 window_box (w, area, box_x, box_y, box_width, box_height)
1193 struct window *w;
1194 int area;
1195 int *box_x, *box_y, *box_width, *box_height;
1196 {
1197 if (box_width)
1198 *box_width = window_box_width (w, area);
1199 if (box_height)
1200 *box_height = window_box_height (w);
1201 if (box_x)
1202 *box_x = window_box_left (w, area);
1203 if (box_y)
1204 {
1205 *box_y = WINDOW_TOP_EDGE_Y (w);
1206 if (WINDOW_WANTS_HEADER_LINE_P (w))
1207 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1208 }
1209 }
1210
1211
1212 /* Get the bounding box of the display area AREA of window W, without
1213 mode lines. AREA < 0 means the whole window, not including the
1214 left and right fringe of the window. Return in *TOP_LEFT_X
1215 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1216 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1217 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1218 box. */
1219
1220 INLINE void
1221 window_box_edges (w, area, top_left_x, top_left_y,
1222 bottom_right_x, bottom_right_y)
1223 struct window *w;
1224 int area;
1225 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1226 {
1227 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1228 bottom_right_y);
1229 *bottom_right_x += *top_left_x;
1230 *bottom_right_y += *top_left_y;
1231 }
1232
1233
1234 \f
1235 /***********************************************************************
1236 Utilities
1237 ***********************************************************************/
1238
1239 /* Return the bottom y-position of the line the iterator IT is in.
1240 This can modify IT's settings. */
1241
1242 int
1243 line_bottom_y (it)
1244 struct it *it;
1245 {
1246 int line_height = it->max_ascent + it->max_descent;
1247 int line_top_y = it->current_y;
1248
1249 if (line_height == 0)
1250 {
1251 if (last_height)
1252 line_height = last_height;
1253 else if (IT_CHARPOS (*it) < ZV)
1254 {
1255 move_it_by_lines (it, 1, 1);
1256 line_height = (it->max_ascent || it->max_descent
1257 ? it->max_ascent + it->max_descent
1258 : last_height);
1259 }
1260 else
1261 {
1262 struct glyph_row *row = it->glyph_row;
1263
1264 /* Use the default character height. */
1265 it->glyph_row = NULL;
1266 it->what = IT_CHARACTER;
1267 it->c = ' ';
1268 it->len = 1;
1269 PRODUCE_GLYPHS (it);
1270 line_height = it->ascent + it->descent;
1271 it->glyph_row = row;
1272 }
1273 }
1274
1275 return line_top_y + line_height;
1276 }
1277
1278
1279 /* Return 1 if position CHARPOS is visible in window W.
1280 CHARPOS < 0 means return info about WINDOW_END position.
1281 If visible, set *X and *Y to pixel coordinates of top left corner.
1282 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1283 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1284
1285 int
1286 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1287 struct window *w;
1288 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1289 {
1290 struct it it;
1291 struct text_pos top;
1292 int visible_p = 0;
1293 struct buffer *old_buffer = NULL;
1294
1295 if (noninteractive)
1296 return visible_p;
1297
1298 if (XBUFFER (w->buffer) != current_buffer)
1299 {
1300 old_buffer = current_buffer;
1301 set_buffer_internal_1 (XBUFFER (w->buffer));
1302 }
1303
1304 SET_TEXT_POS_FROM_MARKER (top, w->start);
1305
1306 /* Compute exact mode line heights. */
1307 if (WINDOW_WANTS_MODELINE_P (w))
1308 current_mode_line_height
1309 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1310 current_buffer->mode_line_format);
1311
1312 if (WINDOW_WANTS_HEADER_LINE_P (w))
1313 current_header_line_height
1314 = display_mode_line (w, HEADER_LINE_FACE_ID,
1315 current_buffer->header_line_format);
1316
1317 start_display (&it, w, top);
1318 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1319 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1320
1321 /* Note that we may overshoot because of invisible text. */
1322 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1323 {
1324 int top_x = it.current_x;
1325 int top_y = it.current_y;
1326 int bottom_y = (last_height = 0, line_bottom_y (&it));
1327 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1328
1329 if (top_y < window_top_y)
1330 visible_p = bottom_y > window_top_y;
1331 else if (top_y < it.last_visible_y)
1332 visible_p = 1;
1333 if (visible_p)
1334 {
1335 *x = top_x;
1336 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1337 *rtop = max (0, window_top_y - top_y);
1338 *rbot = max (0, bottom_y - it.last_visible_y);
1339 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1340 - max (top_y, window_top_y)));
1341 *vpos = it.vpos;
1342 }
1343 }
1344 else
1345 {
1346 struct it it2;
1347
1348 it2 = it;
1349 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1350 move_it_by_lines (&it, 1, 0);
1351 if (charpos < IT_CHARPOS (it)
1352 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1353 {
1354 visible_p = 1;
1355 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1356 *x = it2.current_x;
1357 *y = it2.current_y + it2.max_ascent - it2.ascent;
1358 *rtop = max (0, -it2.current_y);
1359 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1360 - it.last_visible_y));
1361 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1362 it.last_visible_y)
1363 - max (it2.current_y,
1364 WINDOW_HEADER_LINE_HEIGHT (w))));
1365 *vpos = it2.vpos;
1366 }
1367 }
1368
1369 if (old_buffer)
1370 set_buffer_internal_1 (old_buffer);
1371
1372 current_header_line_height = current_mode_line_height = -1;
1373
1374 if (visible_p && XFASTINT (w->hscroll) > 0)
1375 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1376
1377 #if 0
1378 /* Debugging code. */
1379 if (visible_p)
1380 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1381 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1382 else
1383 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1384 #endif
1385
1386 return visible_p;
1387 }
1388
1389
1390 /* Return the next character from STR which is MAXLEN bytes long.
1391 Return in *LEN the length of the character. This is like
1392 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1393 we find one, we return a `?', but with the length of the invalid
1394 character. */
1395
1396 static INLINE int
1397 string_char_and_length (str, maxlen, len)
1398 const unsigned char *str;
1399 int maxlen, *len;
1400 {
1401 int c;
1402
1403 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1404 if (!CHAR_VALID_P (c, 1))
1405 /* We may not change the length here because other places in Emacs
1406 don't use this function, i.e. they silently accept invalid
1407 characters. */
1408 c = '?';
1409
1410 return c;
1411 }
1412
1413
1414
1415 /* Given a position POS containing a valid character and byte position
1416 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1417
1418 static struct text_pos
1419 string_pos_nchars_ahead (pos, string, nchars)
1420 struct text_pos pos;
1421 Lisp_Object string;
1422 int nchars;
1423 {
1424 xassert (STRINGP (string) && nchars >= 0);
1425
1426 if (STRING_MULTIBYTE (string))
1427 {
1428 int rest = SBYTES (string) - BYTEPOS (pos);
1429 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1430 int len;
1431
1432 while (nchars--)
1433 {
1434 string_char_and_length (p, rest, &len);
1435 p += len, rest -= len;
1436 xassert (rest >= 0);
1437 CHARPOS (pos) += 1;
1438 BYTEPOS (pos) += len;
1439 }
1440 }
1441 else
1442 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1443
1444 return pos;
1445 }
1446
1447
1448 /* Value is the text position, i.e. character and byte position,
1449 for character position CHARPOS in STRING. */
1450
1451 static INLINE struct text_pos
1452 string_pos (charpos, string)
1453 int charpos;
1454 Lisp_Object string;
1455 {
1456 struct text_pos pos;
1457 xassert (STRINGP (string));
1458 xassert (charpos >= 0);
1459 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1460 return pos;
1461 }
1462
1463
1464 /* Value is a text position, i.e. character and byte position, for
1465 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1466 means recognize multibyte characters. */
1467
1468 static struct text_pos
1469 c_string_pos (charpos, s, multibyte_p)
1470 int charpos;
1471 unsigned char *s;
1472 int multibyte_p;
1473 {
1474 struct text_pos pos;
1475
1476 xassert (s != NULL);
1477 xassert (charpos >= 0);
1478
1479 if (multibyte_p)
1480 {
1481 int rest = strlen (s), len;
1482
1483 SET_TEXT_POS (pos, 0, 0);
1484 while (charpos--)
1485 {
1486 string_char_and_length (s, rest, &len);
1487 s += len, rest -= len;
1488 xassert (rest >= 0);
1489 CHARPOS (pos) += 1;
1490 BYTEPOS (pos) += len;
1491 }
1492 }
1493 else
1494 SET_TEXT_POS (pos, charpos, charpos);
1495
1496 return pos;
1497 }
1498
1499
1500 /* Value is the number of characters in C string S. MULTIBYTE_P
1501 non-zero means recognize multibyte characters. */
1502
1503 static int
1504 number_of_chars (s, multibyte_p)
1505 unsigned char *s;
1506 int multibyte_p;
1507 {
1508 int nchars;
1509
1510 if (multibyte_p)
1511 {
1512 int rest = strlen (s), len;
1513 unsigned char *p = (unsigned char *) s;
1514
1515 for (nchars = 0; rest > 0; ++nchars)
1516 {
1517 string_char_and_length (p, rest, &len);
1518 rest -= len, p += len;
1519 }
1520 }
1521 else
1522 nchars = strlen (s);
1523
1524 return nchars;
1525 }
1526
1527
1528 /* Compute byte position NEWPOS->bytepos corresponding to
1529 NEWPOS->charpos. POS is a known position in string STRING.
1530 NEWPOS->charpos must be >= POS.charpos. */
1531
1532 static void
1533 compute_string_pos (newpos, pos, string)
1534 struct text_pos *newpos, pos;
1535 Lisp_Object string;
1536 {
1537 xassert (STRINGP (string));
1538 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1539
1540 if (STRING_MULTIBYTE (string))
1541 *newpos = string_pos_nchars_ahead (pos, string,
1542 CHARPOS (*newpos) - CHARPOS (pos));
1543 else
1544 BYTEPOS (*newpos) = CHARPOS (*newpos);
1545 }
1546
1547 /* EXPORT:
1548 Return an estimation of the pixel height of mode or top lines on
1549 frame F. FACE_ID specifies what line's height to estimate. */
1550
1551 int
1552 estimate_mode_line_height (f, face_id)
1553 struct frame *f;
1554 enum face_id face_id;
1555 {
1556 #ifdef HAVE_WINDOW_SYSTEM
1557 if (FRAME_WINDOW_P (f))
1558 {
1559 int height = FONT_HEIGHT (FRAME_FONT (f));
1560
1561 /* This function is called so early when Emacs starts that the face
1562 cache and mode line face are not yet initialized. */
1563 if (FRAME_FACE_CACHE (f))
1564 {
1565 struct face *face = FACE_FROM_ID (f, face_id);
1566 if (face)
1567 {
1568 if (face->font)
1569 height = FONT_HEIGHT (face->font);
1570 if (face->box_line_width > 0)
1571 height += 2 * face->box_line_width;
1572 }
1573 }
1574
1575 return height;
1576 }
1577 #endif
1578
1579 return 1;
1580 }
1581
1582 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1583 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1584 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1585 not force the value into range. */
1586
1587 void
1588 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1589 FRAME_PTR f;
1590 register int pix_x, pix_y;
1591 int *x, *y;
1592 NativeRectangle *bounds;
1593 int noclip;
1594 {
1595
1596 #ifdef HAVE_WINDOW_SYSTEM
1597 if (FRAME_WINDOW_P (f))
1598 {
1599 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1600 even for negative values. */
1601 if (pix_x < 0)
1602 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1603 if (pix_y < 0)
1604 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1605
1606 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1607 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1608
1609 if (bounds)
1610 STORE_NATIVE_RECT (*bounds,
1611 FRAME_COL_TO_PIXEL_X (f, pix_x),
1612 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1613 FRAME_COLUMN_WIDTH (f) - 1,
1614 FRAME_LINE_HEIGHT (f) - 1);
1615
1616 if (!noclip)
1617 {
1618 if (pix_x < 0)
1619 pix_x = 0;
1620 else if (pix_x > FRAME_TOTAL_COLS (f))
1621 pix_x = FRAME_TOTAL_COLS (f);
1622
1623 if (pix_y < 0)
1624 pix_y = 0;
1625 else if (pix_y > FRAME_LINES (f))
1626 pix_y = FRAME_LINES (f);
1627 }
1628 }
1629 #endif
1630
1631 *x = pix_x;
1632 *y = pix_y;
1633 }
1634
1635
1636 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1637 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1638 can't tell the positions because W's display is not up to date,
1639 return 0. */
1640
1641 int
1642 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1643 struct window *w;
1644 int hpos, vpos;
1645 int *frame_x, *frame_y;
1646 {
1647 #ifdef HAVE_WINDOW_SYSTEM
1648 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1649 {
1650 int success_p;
1651
1652 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1653 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1654
1655 if (display_completed)
1656 {
1657 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1658 struct glyph *glyph = row->glyphs[TEXT_AREA];
1659 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1660
1661 hpos = row->x;
1662 vpos = row->y;
1663 while (glyph < end)
1664 {
1665 hpos += glyph->pixel_width;
1666 ++glyph;
1667 }
1668
1669 /* If first glyph is partially visible, its first visible position is still 0. */
1670 if (hpos < 0)
1671 hpos = 0;
1672
1673 success_p = 1;
1674 }
1675 else
1676 {
1677 hpos = vpos = 0;
1678 success_p = 0;
1679 }
1680
1681 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1682 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1683 return success_p;
1684 }
1685 #endif
1686
1687 *frame_x = hpos;
1688 *frame_y = vpos;
1689 return 1;
1690 }
1691
1692
1693 #ifdef HAVE_WINDOW_SYSTEM
1694
1695 /* Find the glyph under window-relative coordinates X/Y in window W.
1696 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1697 strings. Return in *HPOS and *VPOS the row and column number of
1698 the glyph found. Return in *AREA the glyph area containing X.
1699 Value is a pointer to the glyph found or null if X/Y is not on
1700 text, or we can't tell because W's current matrix is not up to
1701 date. */
1702
1703 static struct glyph *
1704 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1705 struct window *w;
1706 int x, y;
1707 int *hpos, *vpos, *dx, *dy, *area;
1708 {
1709 struct glyph *glyph, *end;
1710 struct glyph_row *row = NULL;
1711 int x0, i;
1712
1713 /* Find row containing Y. Give up if some row is not enabled. */
1714 for (i = 0; i < w->current_matrix->nrows; ++i)
1715 {
1716 row = MATRIX_ROW (w->current_matrix, i);
1717 if (!row->enabled_p)
1718 return NULL;
1719 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1720 break;
1721 }
1722
1723 *vpos = i;
1724 *hpos = 0;
1725
1726 /* Give up if Y is not in the window. */
1727 if (i == w->current_matrix->nrows)
1728 return NULL;
1729
1730 /* Get the glyph area containing X. */
1731 if (w->pseudo_window_p)
1732 {
1733 *area = TEXT_AREA;
1734 x0 = 0;
1735 }
1736 else
1737 {
1738 if (x < window_box_left_offset (w, TEXT_AREA))
1739 {
1740 *area = LEFT_MARGIN_AREA;
1741 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1742 }
1743 else if (x < window_box_right_offset (w, TEXT_AREA))
1744 {
1745 *area = TEXT_AREA;
1746 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1747 }
1748 else
1749 {
1750 *area = RIGHT_MARGIN_AREA;
1751 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1752 }
1753 }
1754
1755 /* Find glyph containing X. */
1756 glyph = row->glyphs[*area];
1757 end = glyph + row->used[*area];
1758 x -= x0;
1759 while (glyph < end && x >= glyph->pixel_width)
1760 {
1761 x -= glyph->pixel_width;
1762 ++glyph;
1763 }
1764
1765 if (glyph == end)
1766 return NULL;
1767
1768 if (dx)
1769 {
1770 *dx = x;
1771 *dy = y - (row->y + row->ascent - glyph->ascent);
1772 }
1773
1774 *hpos = glyph - row->glyphs[*area];
1775 return glyph;
1776 }
1777
1778
1779 /* EXPORT:
1780 Convert frame-relative x/y to coordinates relative to window W.
1781 Takes pseudo-windows into account. */
1782
1783 void
1784 frame_to_window_pixel_xy (w, x, y)
1785 struct window *w;
1786 int *x, *y;
1787 {
1788 if (w->pseudo_window_p)
1789 {
1790 /* A pseudo-window is always full-width, and starts at the
1791 left edge of the frame, plus a frame border. */
1792 struct frame *f = XFRAME (w->frame);
1793 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1794 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1795 }
1796 else
1797 {
1798 *x -= WINDOW_LEFT_EDGE_X (w);
1799 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1800 }
1801 }
1802
1803 /* EXPORT:
1804 Return in RECTS[] at most N clipping rectangles for glyph string S.
1805 Return the number of stored rectangles. */
1806
1807 int
1808 get_glyph_string_clip_rects (s, rects, n)
1809 struct glyph_string *s;
1810 NativeRectangle *rects;
1811 int n;
1812 {
1813 XRectangle r;
1814
1815 if (n <= 0)
1816 return 0;
1817
1818 if (s->row->full_width_p)
1819 {
1820 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1821 r.x = WINDOW_LEFT_EDGE_X (s->w);
1822 r.width = WINDOW_TOTAL_WIDTH (s->w);
1823
1824 /* Unless displaying a mode or menu bar line, which are always
1825 fully visible, clip to the visible part of the row. */
1826 if (s->w->pseudo_window_p)
1827 r.height = s->row->visible_height;
1828 else
1829 r.height = s->height;
1830 }
1831 else
1832 {
1833 /* This is a text line that may be partially visible. */
1834 r.x = window_box_left (s->w, s->area);
1835 r.width = window_box_width (s->w, s->area);
1836 r.height = s->row->visible_height;
1837 }
1838
1839 if (s->clip_head)
1840 if (r.x < s->clip_head->x)
1841 {
1842 if (r.width >= s->clip_head->x - r.x)
1843 r.width -= s->clip_head->x - r.x;
1844 else
1845 r.width = 0;
1846 r.x = s->clip_head->x;
1847 }
1848 if (s->clip_tail)
1849 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1850 {
1851 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1852 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1853 else
1854 r.width = 0;
1855 }
1856
1857 /* If S draws overlapping rows, it's sufficient to use the top and
1858 bottom of the window for clipping because this glyph string
1859 intentionally draws over other lines. */
1860 if (s->for_overlaps)
1861 {
1862 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1863 r.height = window_text_bottom_y (s->w) - r.y;
1864
1865 /* Alas, the above simple strategy does not work for the
1866 environments with anti-aliased text: if the same text is
1867 drawn onto the same place multiple times, it gets thicker.
1868 If the overlap we are processing is for the erased cursor, we
1869 take the intersection with the rectagle of the cursor. */
1870 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1871 {
1872 XRectangle rc, r_save = r;
1873
1874 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1875 rc.y = s->w->phys_cursor.y;
1876 rc.width = s->w->phys_cursor_width;
1877 rc.height = s->w->phys_cursor_height;
1878
1879 x_intersect_rectangles (&r_save, &rc, &r);
1880 }
1881 }
1882 else
1883 {
1884 /* Don't use S->y for clipping because it doesn't take partially
1885 visible lines into account. For example, it can be negative for
1886 partially visible lines at the top of a window. */
1887 if (!s->row->full_width_p
1888 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1889 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1890 else
1891 r.y = max (0, s->row->y);
1892
1893 /* If drawing a tool-bar window, draw it over the internal border
1894 at the top of the window. */
1895 if (WINDOWP (s->f->tool_bar_window)
1896 && s->w == XWINDOW (s->f->tool_bar_window))
1897 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1898 }
1899
1900 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1901
1902 /* If drawing the cursor, don't let glyph draw outside its
1903 advertised boundaries. Cleartype does this under some circumstances. */
1904 if (s->hl == DRAW_CURSOR)
1905 {
1906 struct glyph *glyph = s->first_glyph;
1907 int height, max_y;
1908
1909 if (s->x > r.x)
1910 {
1911 r.width -= s->x - r.x;
1912 r.x = s->x;
1913 }
1914 r.width = min (r.width, glyph->pixel_width);
1915
1916 /* If r.y is below window bottom, ensure that we still see a cursor. */
1917 height = min (glyph->ascent + glyph->descent,
1918 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1919 max_y = window_text_bottom_y (s->w) - height;
1920 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1921 if (s->ybase - glyph->ascent > max_y)
1922 {
1923 r.y = max_y;
1924 r.height = height;
1925 }
1926 else
1927 {
1928 /* Don't draw cursor glyph taller than our actual glyph. */
1929 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1930 if (height < r.height)
1931 {
1932 max_y = r.y + r.height;
1933 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1934 r.height = min (max_y - r.y, height);
1935 }
1936 }
1937 }
1938
1939 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1940 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1941 {
1942 #ifdef CONVERT_FROM_XRECT
1943 CONVERT_FROM_XRECT (r, *rects);
1944 #else
1945 *rects = r;
1946 #endif
1947 return 1;
1948 }
1949 else
1950 {
1951 /* If we are processing overlapping and allowed to return
1952 multiple clipping rectangles, we exclude the row of the glyph
1953 string from the clipping rectangle. This is to avoid drawing
1954 the same text on the environment with anti-aliasing. */
1955 #ifdef CONVERT_FROM_XRECT
1956 XRectangle rs[2];
1957 #else
1958 XRectangle *rs = rects;
1959 #endif
1960 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1961
1962 if (s->for_overlaps & OVERLAPS_PRED)
1963 {
1964 rs[i] = r;
1965 if (r.y + r.height > row_y)
1966 {
1967 if (r.y < row_y)
1968 rs[i].height = row_y - r.y;
1969 else
1970 rs[i].height = 0;
1971 }
1972 i++;
1973 }
1974 if (s->for_overlaps & OVERLAPS_SUCC)
1975 {
1976 rs[i] = r;
1977 if (r.y < row_y + s->row->visible_height)
1978 {
1979 if (r.y + r.height > row_y + s->row->visible_height)
1980 {
1981 rs[i].y = row_y + s->row->visible_height;
1982 rs[i].height = r.y + r.height - rs[i].y;
1983 }
1984 else
1985 rs[i].height = 0;
1986 }
1987 i++;
1988 }
1989
1990 n = i;
1991 #ifdef CONVERT_FROM_XRECT
1992 for (i = 0; i < n; i++)
1993 CONVERT_FROM_XRECT (rs[i], rects[i]);
1994 #endif
1995 return n;
1996 }
1997 }
1998
1999 /* EXPORT:
2000 Return in *NR the clipping rectangle for glyph string S. */
2001
2002 void
2003 get_glyph_string_clip_rect (s, nr)
2004 struct glyph_string *s;
2005 NativeRectangle *nr;
2006 {
2007 get_glyph_string_clip_rects (s, nr, 1);
2008 }
2009
2010
2011 /* EXPORT:
2012 Return the position and height of the phys cursor in window W.
2013 Set w->phys_cursor_width to width of phys cursor.
2014 */
2015
2016 void
2017 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2018 struct window *w;
2019 struct glyph_row *row;
2020 struct glyph *glyph;
2021 int *xp, *yp, *heightp;
2022 {
2023 struct frame *f = XFRAME (WINDOW_FRAME (w));
2024 int x, y, wd, h, h0, y0;
2025
2026 /* Compute the width of the rectangle to draw. If on a stretch
2027 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2028 rectangle as wide as the glyph, but use a canonical character
2029 width instead. */
2030 wd = glyph->pixel_width - 1;
2031 #ifdef HAVE_NTGUI
2032 wd++; /* Why? */
2033 #endif
2034
2035 x = w->phys_cursor.x;
2036 if (x < 0)
2037 {
2038 wd += x;
2039 x = 0;
2040 }
2041
2042 if (glyph->type == STRETCH_GLYPH
2043 && !x_stretch_cursor_p)
2044 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2045 w->phys_cursor_width = wd;
2046
2047 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2048
2049 /* If y is below window bottom, ensure that we still see a cursor. */
2050 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2051
2052 h = max (h0, glyph->ascent + glyph->descent);
2053 h0 = min (h0, glyph->ascent + glyph->descent);
2054
2055 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2056 if (y < y0)
2057 {
2058 h = max (h - (y0 - y) + 1, h0);
2059 y = y0 - 1;
2060 }
2061 else
2062 {
2063 y0 = window_text_bottom_y (w) - h0;
2064 if (y > y0)
2065 {
2066 h += y - y0;
2067 y = y0;
2068 }
2069 }
2070
2071 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2072 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2073 *heightp = h;
2074 }
2075
2076 /*
2077 * Remember which glyph the mouse is over.
2078 */
2079
2080 void
2081 remember_mouse_glyph (f, gx, gy, rect)
2082 struct frame *f;
2083 int gx, gy;
2084 NativeRectangle *rect;
2085 {
2086 Lisp_Object window;
2087 struct window *w;
2088 struct glyph_row *r, *gr, *end_row;
2089 enum window_part part;
2090 enum glyph_row_area area;
2091 int x, y, width, height;
2092
2093 /* Try to determine frame pixel position and size of the glyph under
2094 frame pixel coordinates X/Y on frame F. */
2095
2096 if (!f->glyphs_initialized_p
2097 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2098 NILP (window)))
2099 {
2100 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2101 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2102 goto virtual_glyph;
2103 }
2104
2105 w = XWINDOW (window);
2106 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2107 height = WINDOW_FRAME_LINE_HEIGHT (w);
2108
2109 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2110 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2111
2112 if (w->pseudo_window_p)
2113 {
2114 area = TEXT_AREA;
2115 part = ON_MODE_LINE; /* Don't adjust margin. */
2116 goto text_glyph;
2117 }
2118
2119 switch (part)
2120 {
2121 case ON_LEFT_MARGIN:
2122 area = LEFT_MARGIN_AREA;
2123 goto text_glyph;
2124
2125 case ON_RIGHT_MARGIN:
2126 area = RIGHT_MARGIN_AREA;
2127 goto text_glyph;
2128
2129 case ON_HEADER_LINE:
2130 case ON_MODE_LINE:
2131 gr = (part == ON_HEADER_LINE
2132 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2133 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2134 gy = gr->y;
2135 area = TEXT_AREA;
2136 goto text_glyph_row_found;
2137
2138 case ON_TEXT:
2139 area = TEXT_AREA;
2140
2141 text_glyph:
2142 gr = 0; gy = 0;
2143 for (; r <= end_row && r->enabled_p; ++r)
2144 if (r->y + r->height > y)
2145 {
2146 gr = r; gy = r->y;
2147 break;
2148 }
2149
2150 text_glyph_row_found:
2151 if (gr && gy <= y)
2152 {
2153 struct glyph *g = gr->glyphs[area];
2154 struct glyph *end = g + gr->used[area];
2155
2156 height = gr->height;
2157 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2158 if (gx + g->pixel_width > x)
2159 break;
2160
2161 if (g < end)
2162 {
2163 if (g->type == IMAGE_GLYPH)
2164 {
2165 /* Don't remember when mouse is over image, as
2166 image may have hot-spots. */
2167 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2168 return;
2169 }
2170 width = g->pixel_width;
2171 }
2172 else
2173 {
2174 /* Use nominal char spacing at end of line. */
2175 x -= gx;
2176 gx += (x / width) * width;
2177 }
2178
2179 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2180 gx += window_box_left_offset (w, area);
2181 }
2182 else
2183 {
2184 /* Use nominal line height at end of window. */
2185 gx = (x / width) * width;
2186 y -= gy;
2187 gy += (y / height) * height;
2188 }
2189 break;
2190
2191 case ON_LEFT_FRINGE:
2192 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2193 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2194 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2195 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2196 goto row_glyph;
2197
2198 case ON_RIGHT_FRINGE:
2199 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2200 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2201 : window_box_right_offset (w, TEXT_AREA));
2202 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2203 goto row_glyph;
2204
2205 case ON_SCROLL_BAR:
2206 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2207 ? 0
2208 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2209 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2210 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2211 : 0)));
2212 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2213
2214 row_glyph:
2215 gr = 0, gy = 0;
2216 for (; r <= end_row && r->enabled_p; ++r)
2217 if (r->y + r->height > y)
2218 {
2219 gr = r; gy = r->y;
2220 break;
2221 }
2222
2223 if (gr && gy <= y)
2224 height = gr->height;
2225 else
2226 {
2227 /* Use nominal line height at end of window. */
2228 y -= gy;
2229 gy += (y / height) * height;
2230 }
2231 break;
2232
2233 default:
2234 ;
2235 virtual_glyph:
2236 /* If there is no glyph under the mouse, then we divide the screen
2237 into a grid of the smallest glyph in the frame, and use that
2238 as our "glyph". */
2239
2240 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2241 round down even for negative values. */
2242 if (gx < 0)
2243 gx -= width - 1;
2244 if (gy < 0)
2245 gy -= height - 1;
2246
2247 gx = (gx / width) * width;
2248 gy = (gy / height) * height;
2249
2250 goto store_rect;
2251 }
2252
2253 gx += WINDOW_LEFT_EDGE_X (w);
2254 gy += WINDOW_TOP_EDGE_Y (w);
2255
2256 store_rect:
2257 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2258
2259 /* Visible feedback for debugging. */
2260 #if 0
2261 #if HAVE_X_WINDOWS
2262 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2263 f->output_data.x->normal_gc,
2264 gx, gy, width, height);
2265 #endif
2266 #endif
2267 }
2268
2269
2270 #endif /* HAVE_WINDOW_SYSTEM */
2271
2272 \f
2273 /***********************************************************************
2274 Lisp form evaluation
2275 ***********************************************************************/
2276
2277 /* Error handler for safe_eval and safe_call. */
2278
2279 static Lisp_Object
2280 safe_eval_handler (arg)
2281 Lisp_Object arg;
2282 {
2283 add_to_log ("Error during redisplay: %s", arg, Qnil);
2284 return Qnil;
2285 }
2286
2287
2288 /* Evaluate SEXPR and return the result, or nil if something went
2289 wrong. Prevent redisplay during the evaluation. */
2290
2291 Lisp_Object
2292 safe_eval (sexpr)
2293 Lisp_Object sexpr;
2294 {
2295 Lisp_Object val;
2296
2297 if (inhibit_eval_during_redisplay)
2298 val = Qnil;
2299 else
2300 {
2301 int count = SPECPDL_INDEX ();
2302 struct gcpro gcpro1;
2303
2304 GCPRO1 (sexpr);
2305 specbind (Qinhibit_redisplay, Qt);
2306 /* Use Qt to ensure debugger does not run,
2307 so there is no possibility of wanting to redisplay. */
2308 val = internal_condition_case_1 (Feval, sexpr, Qt,
2309 safe_eval_handler);
2310 UNGCPRO;
2311 val = unbind_to (count, val);
2312 }
2313
2314 return val;
2315 }
2316
2317
2318 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2319 Return the result, or nil if something went wrong. Prevent
2320 redisplay during the evaluation. */
2321
2322 Lisp_Object
2323 safe_call (nargs, args)
2324 int nargs;
2325 Lisp_Object *args;
2326 {
2327 Lisp_Object val;
2328
2329 if (inhibit_eval_during_redisplay)
2330 val = Qnil;
2331 else
2332 {
2333 int count = SPECPDL_INDEX ();
2334 struct gcpro gcpro1;
2335
2336 GCPRO1 (args[0]);
2337 gcpro1.nvars = nargs;
2338 specbind (Qinhibit_redisplay, Qt);
2339 /* Use Qt to ensure debugger does not run,
2340 so there is no possibility of wanting to redisplay. */
2341 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2342 safe_eval_handler);
2343 UNGCPRO;
2344 val = unbind_to (count, val);
2345 }
2346
2347 return val;
2348 }
2349
2350
2351 /* Call function FN with one argument ARG.
2352 Return the result, or nil if something went wrong. */
2353
2354 Lisp_Object
2355 safe_call1 (fn, arg)
2356 Lisp_Object fn, arg;
2357 {
2358 Lisp_Object args[2];
2359 args[0] = fn;
2360 args[1] = arg;
2361 return safe_call (2, args);
2362 }
2363
2364
2365 \f
2366 /***********************************************************************
2367 Debugging
2368 ***********************************************************************/
2369
2370 #if 0
2371
2372 /* Define CHECK_IT to perform sanity checks on iterators.
2373 This is for debugging. It is too slow to do unconditionally. */
2374
2375 static void
2376 check_it (it)
2377 struct it *it;
2378 {
2379 if (it->method == GET_FROM_STRING)
2380 {
2381 xassert (STRINGP (it->string));
2382 xassert (IT_STRING_CHARPOS (*it) >= 0);
2383 }
2384 else
2385 {
2386 xassert (IT_STRING_CHARPOS (*it) < 0);
2387 if (it->method == GET_FROM_BUFFER)
2388 {
2389 /* Check that character and byte positions agree. */
2390 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2391 }
2392 }
2393
2394 if (it->dpvec)
2395 xassert (it->current.dpvec_index >= 0);
2396 else
2397 xassert (it->current.dpvec_index < 0);
2398 }
2399
2400 #define CHECK_IT(IT) check_it ((IT))
2401
2402 #else /* not 0 */
2403
2404 #define CHECK_IT(IT) (void) 0
2405
2406 #endif /* not 0 */
2407
2408
2409 #if GLYPH_DEBUG
2410
2411 /* Check that the window end of window W is what we expect it
2412 to be---the last row in the current matrix displaying text. */
2413
2414 static void
2415 check_window_end (w)
2416 struct window *w;
2417 {
2418 if (!MINI_WINDOW_P (w)
2419 && !NILP (w->window_end_valid))
2420 {
2421 struct glyph_row *row;
2422 xassert ((row = MATRIX_ROW (w->current_matrix,
2423 XFASTINT (w->window_end_vpos)),
2424 !row->enabled_p
2425 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2426 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2427 }
2428 }
2429
2430 #define CHECK_WINDOW_END(W) check_window_end ((W))
2431
2432 #else /* not GLYPH_DEBUG */
2433
2434 #define CHECK_WINDOW_END(W) (void) 0
2435
2436 #endif /* not GLYPH_DEBUG */
2437
2438
2439 \f
2440 /***********************************************************************
2441 Iterator initialization
2442 ***********************************************************************/
2443
2444 /* Initialize IT for displaying current_buffer in window W, starting
2445 at character position CHARPOS. CHARPOS < 0 means that no buffer
2446 position is specified which is useful when the iterator is assigned
2447 a position later. BYTEPOS is the byte position corresponding to
2448 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2449
2450 If ROW is not null, calls to produce_glyphs with IT as parameter
2451 will produce glyphs in that row.
2452
2453 BASE_FACE_ID is the id of a base face to use. It must be one of
2454 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2455 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2456 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2457
2458 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2459 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2460 will be initialized to use the corresponding mode line glyph row of
2461 the desired matrix of W. */
2462
2463 void
2464 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2465 struct it *it;
2466 struct window *w;
2467 int charpos, bytepos;
2468 struct glyph_row *row;
2469 enum face_id base_face_id;
2470 {
2471 int highlight_region_p;
2472
2473 /* Some precondition checks. */
2474 xassert (w != NULL && it != NULL);
2475 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2476 && charpos <= ZV));
2477
2478 /* If face attributes have been changed since the last redisplay,
2479 free realized faces now because they depend on face definitions
2480 that might have changed. Don't free faces while there might be
2481 desired matrices pending which reference these faces. */
2482 if (face_change_count && !inhibit_free_realized_faces)
2483 {
2484 face_change_count = 0;
2485 free_all_realized_faces (Qnil);
2486 }
2487
2488 /* Use one of the mode line rows of W's desired matrix if
2489 appropriate. */
2490 if (row == NULL)
2491 {
2492 if (base_face_id == MODE_LINE_FACE_ID
2493 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2494 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2495 else if (base_face_id == HEADER_LINE_FACE_ID)
2496 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2497 }
2498
2499 /* Clear IT. */
2500 bzero (it, sizeof *it);
2501 it->current.overlay_string_index = -1;
2502 it->current.dpvec_index = -1;
2503 it->base_face_id = base_face_id;
2504 it->string = Qnil;
2505 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2506
2507 /* The window in which we iterate over current_buffer: */
2508 XSETWINDOW (it->window, w);
2509 it->w = w;
2510 it->f = XFRAME (w->frame);
2511
2512 /* Extra space between lines (on window systems only). */
2513 if (base_face_id == DEFAULT_FACE_ID
2514 && FRAME_WINDOW_P (it->f))
2515 {
2516 if (NATNUMP (current_buffer->extra_line_spacing))
2517 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2518 else if (FLOATP (current_buffer->extra_line_spacing))
2519 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2520 * FRAME_LINE_HEIGHT (it->f));
2521 else if (it->f->extra_line_spacing > 0)
2522 it->extra_line_spacing = it->f->extra_line_spacing;
2523 it->max_extra_line_spacing = 0;
2524 }
2525
2526 /* If realized faces have been removed, e.g. because of face
2527 attribute changes of named faces, recompute them. When running
2528 in batch mode, the face cache of Vterminal_frame is null. If
2529 we happen to get called, make a dummy face cache. */
2530 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2531 init_frame_faces (it->f);
2532 if (FRAME_FACE_CACHE (it->f)->used == 0)
2533 recompute_basic_faces (it->f);
2534
2535 /* Current value of the `slice', `space-width', and 'height' properties. */
2536 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2537 it->space_width = Qnil;
2538 it->font_height = Qnil;
2539 it->override_ascent = -1;
2540
2541 /* Are control characters displayed as `^C'? */
2542 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2543
2544 /* -1 means everything between a CR and the following line end
2545 is invisible. >0 means lines indented more than this value are
2546 invisible. */
2547 it->selective = (INTEGERP (current_buffer->selective_display)
2548 ? XFASTINT (current_buffer->selective_display)
2549 : (!NILP (current_buffer->selective_display)
2550 ? -1 : 0));
2551 it->selective_display_ellipsis_p
2552 = !NILP (current_buffer->selective_display_ellipses);
2553
2554 /* Display table to use. */
2555 it->dp = window_display_table (w);
2556
2557 /* Are multibyte characters enabled in current_buffer? */
2558 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2559
2560 /* Non-zero if we should highlight the region. */
2561 highlight_region_p
2562 = (!NILP (Vtransient_mark_mode)
2563 && !NILP (current_buffer->mark_active)
2564 && XMARKER (current_buffer->mark)->buffer != 0);
2565
2566 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2567 start and end of a visible region in window IT->w. Set both to
2568 -1 to indicate no region. */
2569 if (highlight_region_p
2570 /* Maybe highlight only in selected window. */
2571 && (/* Either show region everywhere. */
2572 highlight_nonselected_windows
2573 /* Or show region in the selected window. */
2574 || w == XWINDOW (selected_window)
2575 /* Or show the region if we are in the mini-buffer and W is
2576 the window the mini-buffer refers to. */
2577 || (MINI_WINDOW_P (XWINDOW (selected_window))
2578 && WINDOWP (minibuf_selected_window)
2579 && w == XWINDOW (minibuf_selected_window))))
2580 {
2581 int charpos = marker_position (current_buffer->mark);
2582 it->region_beg_charpos = min (PT, charpos);
2583 it->region_end_charpos = max (PT, charpos);
2584 }
2585 else
2586 it->region_beg_charpos = it->region_end_charpos = -1;
2587
2588 /* Get the position at which the redisplay_end_trigger hook should
2589 be run, if it is to be run at all. */
2590 if (MARKERP (w->redisplay_end_trigger)
2591 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2592 it->redisplay_end_trigger_charpos
2593 = marker_position (w->redisplay_end_trigger);
2594 else if (INTEGERP (w->redisplay_end_trigger))
2595 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2596
2597 /* Correct bogus values of tab_width. */
2598 it->tab_width = XINT (current_buffer->tab_width);
2599 if (it->tab_width <= 0 || it->tab_width > 1000)
2600 it->tab_width = 8;
2601
2602 /* Are lines in the display truncated? */
2603 it->truncate_lines_p
2604 = (base_face_id != DEFAULT_FACE_ID
2605 || XINT (it->w->hscroll)
2606 || (truncate_partial_width_windows
2607 && !WINDOW_FULL_WIDTH_P (it->w))
2608 || !NILP (current_buffer->truncate_lines));
2609
2610 /* Get dimensions of truncation and continuation glyphs. These are
2611 displayed as fringe bitmaps under X, so we don't need them for such
2612 frames. */
2613 if (!FRAME_WINDOW_P (it->f))
2614 {
2615 if (it->truncate_lines_p)
2616 {
2617 /* We will need the truncation glyph. */
2618 xassert (it->glyph_row == NULL);
2619 produce_special_glyphs (it, IT_TRUNCATION);
2620 it->truncation_pixel_width = it->pixel_width;
2621 }
2622 else
2623 {
2624 /* We will need the continuation glyph. */
2625 xassert (it->glyph_row == NULL);
2626 produce_special_glyphs (it, IT_CONTINUATION);
2627 it->continuation_pixel_width = it->pixel_width;
2628 }
2629
2630 /* Reset these values to zero because the produce_special_glyphs
2631 above has changed them. */
2632 it->pixel_width = it->ascent = it->descent = 0;
2633 it->phys_ascent = it->phys_descent = 0;
2634 }
2635
2636 /* Set this after getting the dimensions of truncation and
2637 continuation glyphs, so that we don't produce glyphs when calling
2638 produce_special_glyphs, above. */
2639 it->glyph_row = row;
2640 it->area = TEXT_AREA;
2641
2642 /* Get the dimensions of the display area. The display area
2643 consists of the visible window area plus a horizontally scrolled
2644 part to the left of the window. All x-values are relative to the
2645 start of this total display area. */
2646 if (base_face_id != DEFAULT_FACE_ID)
2647 {
2648 /* Mode lines, menu bar in terminal frames. */
2649 it->first_visible_x = 0;
2650 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2651 }
2652 else
2653 {
2654 it->first_visible_x
2655 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2656 it->last_visible_x = (it->first_visible_x
2657 + window_box_width (w, TEXT_AREA));
2658
2659 /* If we truncate lines, leave room for the truncator glyph(s) at
2660 the right margin. Otherwise, leave room for the continuation
2661 glyph(s). Truncation and continuation glyphs are not inserted
2662 for window-based redisplay. */
2663 if (!FRAME_WINDOW_P (it->f))
2664 {
2665 if (it->truncate_lines_p)
2666 it->last_visible_x -= it->truncation_pixel_width;
2667 else
2668 it->last_visible_x -= it->continuation_pixel_width;
2669 }
2670
2671 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2672 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2673 }
2674
2675 /* Leave room for a border glyph. */
2676 if (!FRAME_WINDOW_P (it->f)
2677 && !WINDOW_RIGHTMOST_P (it->w))
2678 it->last_visible_x -= 1;
2679
2680 it->last_visible_y = window_text_bottom_y (w);
2681
2682 /* For mode lines and alike, arrange for the first glyph having a
2683 left box line if the face specifies a box. */
2684 if (base_face_id != DEFAULT_FACE_ID)
2685 {
2686 struct face *face;
2687
2688 it->face_id = base_face_id;
2689
2690 /* If we have a boxed mode line, make the first character appear
2691 with a left box line. */
2692 face = FACE_FROM_ID (it->f, base_face_id);
2693 if (face->box != FACE_NO_BOX)
2694 it->start_of_box_run_p = 1;
2695 }
2696
2697 /* If a buffer position was specified, set the iterator there,
2698 getting overlays and face properties from that position. */
2699 if (charpos >= BUF_BEG (current_buffer))
2700 {
2701 it->end_charpos = ZV;
2702 it->face_id = -1;
2703 IT_CHARPOS (*it) = charpos;
2704
2705 /* Compute byte position if not specified. */
2706 if (bytepos < charpos)
2707 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2708 else
2709 IT_BYTEPOS (*it) = bytepos;
2710
2711 it->start = it->current;
2712
2713 /* Compute faces etc. */
2714 reseat (it, it->current.pos, 1);
2715 }
2716
2717 CHECK_IT (it);
2718 }
2719
2720
2721 /* Initialize IT for the display of window W with window start POS. */
2722
2723 void
2724 start_display (it, w, pos)
2725 struct it *it;
2726 struct window *w;
2727 struct text_pos pos;
2728 {
2729 struct glyph_row *row;
2730 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2731
2732 row = w->desired_matrix->rows + first_vpos;
2733 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2734 it->first_vpos = first_vpos;
2735
2736 /* Don't reseat to previous visible line start if current start
2737 position is in a string or image. */
2738 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2739 {
2740 int start_at_line_beg_p;
2741 int first_y = it->current_y;
2742
2743 /* If window start is not at a line start, skip forward to POS to
2744 get the correct continuation lines width. */
2745 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2746 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2747 if (!start_at_line_beg_p)
2748 {
2749 int new_x;
2750
2751 reseat_at_previous_visible_line_start (it);
2752 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2753
2754 new_x = it->current_x + it->pixel_width;
2755
2756 /* If lines are continued, this line may end in the middle
2757 of a multi-glyph character (e.g. a control character
2758 displayed as \003, or in the middle of an overlay
2759 string). In this case move_it_to above will not have
2760 taken us to the start of the continuation line but to the
2761 end of the continued line. */
2762 if (it->current_x > 0
2763 && !it->truncate_lines_p /* Lines are continued. */
2764 && (/* And glyph doesn't fit on the line. */
2765 new_x > it->last_visible_x
2766 /* Or it fits exactly and we're on a window
2767 system frame. */
2768 || (new_x == it->last_visible_x
2769 && FRAME_WINDOW_P (it->f))))
2770 {
2771 if (it->current.dpvec_index >= 0
2772 || it->current.overlay_string_index >= 0)
2773 {
2774 set_iterator_to_next (it, 1);
2775 move_it_in_display_line_to (it, -1, -1, 0);
2776 }
2777
2778 it->continuation_lines_width += it->current_x;
2779 }
2780
2781 /* We're starting a new display line, not affected by the
2782 height of the continued line, so clear the appropriate
2783 fields in the iterator structure. */
2784 it->max_ascent = it->max_descent = 0;
2785 it->max_phys_ascent = it->max_phys_descent = 0;
2786
2787 it->current_y = first_y;
2788 it->vpos = 0;
2789 it->current_x = it->hpos = 0;
2790 }
2791 }
2792
2793 #if 0 /* Don't assert the following because start_display is sometimes
2794 called intentionally with a window start that is not at a
2795 line start. Please leave this code in as a comment. */
2796
2797 /* Window start should be on a line start, now. */
2798 xassert (it->continuation_lines_width
2799 || IT_CHARPOS (it) == BEGV
2800 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2801 #endif /* 0 */
2802 }
2803
2804
2805 /* Return 1 if POS is a position in ellipses displayed for invisible
2806 text. W is the window we display, for text property lookup. */
2807
2808 static int
2809 in_ellipses_for_invisible_text_p (pos, w)
2810 struct display_pos *pos;
2811 struct window *w;
2812 {
2813 Lisp_Object prop, window;
2814 int ellipses_p = 0;
2815 int charpos = CHARPOS (pos->pos);
2816
2817 /* If POS specifies a position in a display vector, this might
2818 be for an ellipsis displayed for invisible text. We won't
2819 get the iterator set up for delivering that ellipsis unless
2820 we make sure that it gets aware of the invisible text. */
2821 if (pos->dpvec_index >= 0
2822 && pos->overlay_string_index < 0
2823 && CHARPOS (pos->string_pos) < 0
2824 && charpos > BEGV
2825 && (XSETWINDOW (window, w),
2826 prop = Fget_char_property (make_number (charpos),
2827 Qinvisible, window),
2828 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2829 {
2830 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2831 window);
2832 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2833 }
2834
2835 return ellipses_p;
2836 }
2837
2838
2839 /* Initialize IT for stepping through current_buffer in window W,
2840 starting at position POS that includes overlay string and display
2841 vector/ control character translation position information. Value
2842 is zero if there are overlay strings with newlines at POS. */
2843
2844 static int
2845 init_from_display_pos (it, w, pos)
2846 struct it *it;
2847 struct window *w;
2848 struct display_pos *pos;
2849 {
2850 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2851 int i, overlay_strings_with_newlines = 0;
2852
2853 /* If POS specifies a position in a display vector, this might
2854 be for an ellipsis displayed for invisible text. We won't
2855 get the iterator set up for delivering that ellipsis unless
2856 we make sure that it gets aware of the invisible text. */
2857 if (in_ellipses_for_invisible_text_p (pos, w))
2858 {
2859 --charpos;
2860 bytepos = 0;
2861 }
2862
2863 /* Keep in mind: the call to reseat in init_iterator skips invisible
2864 text, so we might end up at a position different from POS. This
2865 is only a problem when POS is a row start after a newline and an
2866 overlay starts there with an after-string, and the overlay has an
2867 invisible property. Since we don't skip invisible text in
2868 display_line and elsewhere immediately after consuming the
2869 newline before the row start, such a POS will not be in a string,
2870 but the call to init_iterator below will move us to the
2871 after-string. */
2872 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2873
2874 /* This only scans the current chunk -- it should scan all chunks.
2875 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2876 to 16 in 22.1 to make this a lesser problem. */
2877 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2878 {
2879 const char *s = SDATA (it->overlay_strings[i]);
2880 const char *e = s + SBYTES (it->overlay_strings[i]);
2881
2882 while (s < e && *s != '\n')
2883 ++s;
2884
2885 if (s < e)
2886 {
2887 overlay_strings_with_newlines = 1;
2888 break;
2889 }
2890 }
2891
2892 /* If position is within an overlay string, set up IT to the right
2893 overlay string. */
2894 if (pos->overlay_string_index >= 0)
2895 {
2896 int relative_index;
2897
2898 /* If the first overlay string happens to have a `display'
2899 property for an image, the iterator will be set up for that
2900 image, and we have to undo that setup first before we can
2901 correct the overlay string index. */
2902 if (it->method == GET_FROM_IMAGE)
2903 pop_it (it);
2904
2905 /* We already have the first chunk of overlay strings in
2906 IT->overlay_strings. Load more until the one for
2907 pos->overlay_string_index is in IT->overlay_strings. */
2908 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2909 {
2910 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2911 it->current.overlay_string_index = 0;
2912 while (n--)
2913 {
2914 load_overlay_strings (it, 0);
2915 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2916 }
2917 }
2918
2919 it->current.overlay_string_index = pos->overlay_string_index;
2920 relative_index = (it->current.overlay_string_index
2921 % OVERLAY_STRING_CHUNK_SIZE);
2922 it->string = it->overlay_strings[relative_index];
2923 xassert (STRINGP (it->string));
2924 it->current.string_pos = pos->string_pos;
2925 it->method = GET_FROM_STRING;
2926 }
2927
2928 #if 0 /* This is bogus because POS not having an overlay string
2929 position does not mean it's after the string. Example: A
2930 line starting with a before-string and initialization of IT
2931 to the previous row's end position. */
2932 else if (it->current.overlay_string_index >= 0)
2933 {
2934 /* If POS says we're already after an overlay string ending at
2935 POS, make sure to pop the iterator because it will be in
2936 front of that overlay string. When POS is ZV, we've thereby
2937 also ``processed'' overlay strings at ZV. */
2938 while (it->sp)
2939 pop_it (it);
2940 xassert (it->current.overlay_string_index == -1);
2941 xassert (it->method == GET_FROM_BUFFER);
2942 if (CHARPOS (pos->pos) == ZV)
2943 it->overlay_strings_at_end_processed_p = 1;
2944 }
2945 #endif /* 0 */
2946
2947 if (CHARPOS (pos->string_pos) >= 0)
2948 {
2949 /* Recorded position is not in an overlay string, but in another
2950 string. This can only be a string from a `display' property.
2951 IT should already be filled with that string. */
2952 it->current.string_pos = pos->string_pos;
2953 xassert (STRINGP (it->string));
2954 }
2955
2956 /* Restore position in display vector translations, control
2957 character translations or ellipses. */
2958 if (pos->dpvec_index >= 0)
2959 {
2960 if (it->dpvec == NULL)
2961 get_next_display_element (it);
2962 xassert (it->dpvec && it->current.dpvec_index == 0);
2963 it->current.dpvec_index = pos->dpvec_index;
2964 }
2965
2966 CHECK_IT (it);
2967 return !overlay_strings_with_newlines;
2968 }
2969
2970
2971 /* Initialize IT for stepping through current_buffer in window W
2972 starting at ROW->start. */
2973
2974 static void
2975 init_to_row_start (it, w, row)
2976 struct it *it;
2977 struct window *w;
2978 struct glyph_row *row;
2979 {
2980 init_from_display_pos (it, w, &row->start);
2981 it->start = row->start;
2982 it->continuation_lines_width = row->continuation_lines_width;
2983 CHECK_IT (it);
2984 }
2985
2986
2987 /* Initialize IT for stepping through current_buffer in window W
2988 starting in the line following ROW, i.e. starting at ROW->end.
2989 Value is zero if there are overlay strings with newlines at ROW's
2990 end position. */
2991
2992 static int
2993 init_to_row_end (it, w, row)
2994 struct it *it;
2995 struct window *w;
2996 struct glyph_row *row;
2997 {
2998 int success = 0;
2999
3000 if (init_from_display_pos (it, w, &row->end))
3001 {
3002 if (row->continued_p)
3003 it->continuation_lines_width
3004 = row->continuation_lines_width + row->pixel_width;
3005 CHECK_IT (it);
3006 success = 1;
3007 }
3008
3009 return success;
3010 }
3011
3012
3013
3014 \f
3015 /***********************************************************************
3016 Text properties
3017 ***********************************************************************/
3018
3019 /* Called when IT reaches IT->stop_charpos. Handle text property and
3020 overlay changes. Set IT->stop_charpos to the next position where
3021 to stop. */
3022
3023 static void
3024 handle_stop (it)
3025 struct it *it;
3026 {
3027 enum prop_handled handled;
3028 int handle_overlay_change_p;
3029 struct props *p;
3030
3031 it->dpvec = NULL;
3032 it->current.dpvec_index = -1;
3033 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3034 it->ignore_overlay_strings_at_pos_p = 0;
3035
3036 /* Use face of preceding text for ellipsis (if invisible) */
3037 if (it->selective_display_ellipsis_p)
3038 it->saved_face_id = it->face_id;
3039
3040 do
3041 {
3042 handled = HANDLED_NORMALLY;
3043
3044 /* Call text property handlers. */
3045 for (p = it_props; p->handler; ++p)
3046 {
3047 handled = p->handler (it);
3048
3049 if (handled == HANDLED_RECOMPUTE_PROPS)
3050 break;
3051 else if (handled == HANDLED_RETURN)
3052 {
3053 /* We still want to show before and after strings from
3054 overlays even if the actual buffer text is replaced. */
3055 if (!handle_overlay_change_p || it->sp > 1)
3056 return;
3057 if (!get_overlay_strings_1 (it, 0, 0))
3058 return;
3059 it->ignore_overlay_strings_at_pos_p = 1;
3060 it->string_from_display_prop_p = 0;
3061 handle_overlay_change_p = 0;
3062 handled = HANDLED_RECOMPUTE_PROPS;
3063 break;
3064 }
3065 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3066 handle_overlay_change_p = 0;
3067 }
3068
3069 if (handled != HANDLED_RECOMPUTE_PROPS)
3070 {
3071 /* Don't check for overlay strings below when set to deliver
3072 characters from a display vector. */
3073 if (it->method == GET_FROM_DISPLAY_VECTOR)
3074 handle_overlay_change_p = 0;
3075
3076 /* Handle overlay changes. */
3077 if (handle_overlay_change_p)
3078 handled = handle_overlay_change (it);
3079
3080 /* Determine where to stop next. */
3081 if (handled == HANDLED_NORMALLY)
3082 compute_stop_pos (it);
3083 }
3084 }
3085 while (handled == HANDLED_RECOMPUTE_PROPS);
3086 }
3087
3088
3089 /* Compute IT->stop_charpos from text property and overlay change
3090 information for IT's current position. */
3091
3092 static void
3093 compute_stop_pos (it)
3094 struct it *it;
3095 {
3096 register INTERVAL iv, next_iv;
3097 Lisp_Object object, limit, position;
3098
3099 /* If nowhere else, stop at the end. */
3100 it->stop_charpos = it->end_charpos;
3101
3102 if (STRINGP (it->string))
3103 {
3104 /* Strings are usually short, so don't limit the search for
3105 properties. */
3106 object = it->string;
3107 limit = Qnil;
3108 position = make_number (IT_STRING_CHARPOS (*it));
3109 }
3110 else
3111 {
3112 int charpos;
3113
3114 /* If next overlay change is in front of the current stop pos
3115 (which is IT->end_charpos), stop there. Note: value of
3116 next_overlay_change is point-max if no overlay change
3117 follows. */
3118 charpos = next_overlay_change (IT_CHARPOS (*it));
3119 if (charpos < it->stop_charpos)
3120 it->stop_charpos = charpos;
3121
3122 /* If showing the region, we have to stop at the region
3123 start or end because the face might change there. */
3124 if (it->region_beg_charpos > 0)
3125 {
3126 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3127 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3128 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3129 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3130 }
3131
3132 /* Set up variables for computing the stop position from text
3133 property changes. */
3134 XSETBUFFER (object, current_buffer);
3135 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3136 position = make_number (IT_CHARPOS (*it));
3137
3138 }
3139
3140 /* Get the interval containing IT's position. Value is a null
3141 interval if there isn't such an interval. */
3142 iv = validate_interval_range (object, &position, &position, 0);
3143 if (!NULL_INTERVAL_P (iv))
3144 {
3145 Lisp_Object values_here[LAST_PROP_IDX];
3146 struct props *p;
3147
3148 /* Get properties here. */
3149 for (p = it_props; p->handler; ++p)
3150 values_here[p->idx] = textget (iv->plist, *p->name);
3151
3152 /* Look for an interval following iv that has different
3153 properties. */
3154 for (next_iv = next_interval (iv);
3155 (!NULL_INTERVAL_P (next_iv)
3156 && (NILP (limit)
3157 || XFASTINT (limit) > next_iv->position));
3158 next_iv = next_interval (next_iv))
3159 {
3160 for (p = it_props; p->handler; ++p)
3161 {
3162 Lisp_Object new_value;
3163
3164 new_value = textget (next_iv->plist, *p->name);
3165 if (!EQ (values_here[p->idx], new_value))
3166 break;
3167 }
3168
3169 if (p->handler)
3170 break;
3171 }
3172
3173 if (!NULL_INTERVAL_P (next_iv))
3174 {
3175 if (INTEGERP (limit)
3176 && next_iv->position >= XFASTINT (limit))
3177 /* No text property change up to limit. */
3178 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3179 else
3180 /* Text properties change in next_iv. */
3181 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3182 }
3183 }
3184
3185 xassert (STRINGP (it->string)
3186 || (it->stop_charpos >= BEGV
3187 && it->stop_charpos >= IT_CHARPOS (*it)));
3188 }
3189
3190
3191 /* Return the position of the next overlay change after POS in
3192 current_buffer. Value is point-max if no overlay change
3193 follows. This is like `next-overlay-change' but doesn't use
3194 xmalloc. */
3195
3196 static int
3197 next_overlay_change (pos)
3198 int pos;
3199 {
3200 int noverlays;
3201 int endpos;
3202 Lisp_Object *overlays;
3203 int i;
3204
3205 /* Get all overlays at the given position. */
3206 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3207
3208 /* If any of these overlays ends before endpos,
3209 use its ending point instead. */
3210 for (i = 0; i < noverlays; ++i)
3211 {
3212 Lisp_Object oend;
3213 int oendpos;
3214
3215 oend = OVERLAY_END (overlays[i]);
3216 oendpos = OVERLAY_POSITION (oend);
3217 endpos = min (endpos, oendpos);
3218 }
3219
3220 return endpos;
3221 }
3222
3223
3224 \f
3225 /***********************************************************************
3226 Fontification
3227 ***********************************************************************/
3228
3229 /* Handle changes in the `fontified' property of the current buffer by
3230 calling hook functions from Qfontification_functions to fontify
3231 regions of text. */
3232
3233 static enum prop_handled
3234 handle_fontified_prop (it)
3235 struct it *it;
3236 {
3237 Lisp_Object prop, pos;
3238 enum prop_handled handled = HANDLED_NORMALLY;
3239
3240 if (!NILP (Vmemory_full))
3241 return handled;
3242
3243 /* Get the value of the `fontified' property at IT's current buffer
3244 position. (The `fontified' property doesn't have a special
3245 meaning in strings.) If the value is nil, call functions from
3246 Qfontification_functions. */
3247 if (!STRINGP (it->string)
3248 && it->s == NULL
3249 && !NILP (Vfontification_functions)
3250 && !NILP (Vrun_hooks)
3251 && (pos = make_number (IT_CHARPOS (*it)),
3252 prop = Fget_char_property (pos, Qfontified, Qnil),
3253 /* Ignore the special cased nil value always present at EOB since
3254 no amount of fontifying will be able to change it. */
3255 NILP (prop) && IT_CHARPOS (*it) < Z))
3256 {
3257 int count = SPECPDL_INDEX ();
3258 Lisp_Object val;
3259
3260 val = Vfontification_functions;
3261 specbind (Qfontification_functions, Qnil);
3262
3263 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3264 safe_call1 (val, pos);
3265 else
3266 {
3267 Lisp_Object globals, fn;
3268 struct gcpro gcpro1, gcpro2;
3269
3270 globals = Qnil;
3271 GCPRO2 (val, globals);
3272
3273 for (; CONSP (val); val = XCDR (val))
3274 {
3275 fn = XCAR (val);
3276
3277 if (EQ (fn, Qt))
3278 {
3279 /* A value of t indicates this hook has a local
3280 binding; it means to run the global binding too.
3281 In a global value, t should not occur. If it
3282 does, we must ignore it to avoid an endless
3283 loop. */
3284 for (globals = Fdefault_value (Qfontification_functions);
3285 CONSP (globals);
3286 globals = XCDR (globals))
3287 {
3288 fn = XCAR (globals);
3289 if (!EQ (fn, Qt))
3290 safe_call1 (fn, pos);
3291 }
3292 }
3293 else
3294 safe_call1 (fn, pos);
3295 }
3296
3297 UNGCPRO;
3298 }
3299
3300 unbind_to (count, Qnil);
3301
3302 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3303 something. This avoids an endless loop if they failed to
3304 fontify the text for which reason ever. */
3305 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3306 handled = HANDLED_RECOMPUTE_PROPS;
3307 }
3308
3309 return handled;
3310 }
3311
3312
3313 \f
3314 /***********************************************************************
3315 Faces
3316 ***********************************************************************/
3317
3318 /* Set up iterator IT from face properties at its current position.
3319 Called from handle_stop. */
3320
3321 static enum prop_handled
3322 handle_face_prop (it)
3323 struct it *it;
3324 {
3325 int new_face_id, next_stop;
3326
3327 if (!STRINGP (it->string))
3328 {
3329 new_face_id
3330 = face_at_buffer_position (it->w,
3331 IT_CHARPOS (*it),
3332 it->region_beg_charpos,
3333 it->region_end_charpos,
3334 &next_stop,
3335 (IT_CHARPOS (*it)
3336 + TEXT_PROP_DISTANCE_LIMIT),
3337 0);
3338
3339 /* Is this a start of a run of characters with box face?
3340 Caveat: this can be called for a freshly initialized
3341 iterator; face_id is -1 in this case. We know that the new
3342 face will not change until limit, i.e. if the new face has a
3343 box, all characters up to limit will have one. But, as
3344 usual, we don't know whether limit is really the end. */
3345 if (new_face_id != it->face_id)
3346 {
3347 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3348
3349 /* If new face has a box but old face has not, this is
3350 the start of a run of characters with box, i.e. it has
3351 a shadow on the left side. The value of face_id of the
3352 iterator will be -1 if this is the initial call that gets
3353 the face. In this case, we have to look in front of IT's
3354 position and see whether there is a face != new_face_id. */
3355 it->start_of_box_run_p
3356 = (new_face->box != FACE_NO_BOX
3357 && (it->face_id >= 0
3358 || IT_CHARPOS (*it) == BEG
3359 || new_face_id != face_before_it_pos (it)));
3360 it->face_box_p = new_face->box != FACE_NO_BOX;
3361 }
3362 }
3363 else
3364 {
3365 int base_face_id, bufpos;
3366
3367 if (it->current.overlay_string_index >= 0)
3368 bufpos = IT_CHARPOS (*it);
3369 else
3370 bufpos = 0;
3371
3372 /* For strings from a buffer, i.e. overlay strings or strings
3373 from a `display' property, use the face at IT's current
3374 buffer position as the base face to merge with, so that
3375 overlay strings appear in the same face as surrounding
3376 text, unless they specify their own faces. */
3377 base_face_id = underlying_face_id (it);
3378
3379 new_face_id = face_at_string_position (it->w,
3380 it->string,
3381 IT_STRING_CHARPOS (*it),
3382 bufpos,
3383 it->region_beg_charpos,
3384 it->region_end_charpos,
3385 &next_stop,
3386 base_face_id, 0);
3387
3388 #if 0 /* This shouldn't be neccessary. Let's check it. */
3389 /* If IT is used to display a mode line we would really like to
3390 use the mode line face instead of the frame's default face. */
3391 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3392 && new_face_id == DEFAULT_FACE_ID)
3393 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3394 #endif
3395
3396 /* Is this a start of a run of characters with box? Caveat:
3397 this can be called for a freshly allocated iterator; face_id
3398 is -1 is this case. We know that the new face will not
3399 change until the next check pos, i.e. if the new face has a
3400 box, all characters up to that position will have a
3401 box. But, as usual, we don't know whether that position
3402 is really the end. */
3403 if (new_face_id != it->face_id)
3404 {
3405 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3406 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3407
3408 /* If new face has a box but old face hasn't, this is the
3409 start of a run of characters with box, i.e. it has a
3410 shadow on the left side. */
3411 it->start_of_box_run_p
3412 = new_face->box && (old_face == NULL || !old_face->box);
3413 it->face_box_p = new_face->box != FACE_NO_BOX;
3414 }
3415 }
3416
3417 it->face_id = new_face_id;
3418 return HANDLED_NORMALLY;
3419 }
3420
3421
3422 /* Return the ID of the face ``underlying'' IT's current position,
3423 which is in a string. If the iterator is associated with a
3424 buffer, return the face at IT's current buffer position.
3425 Otherwise, use the iterator's base_face_id. */
3426
3427 static int
3428 underlying_face_id (it)
3429 struct it *it;
3430 {
3431 int face_id = it->base_face_id, i;
3432
3433 xassert (STRINGP (it->string));
3434
3435 for (i = it->sp - 1; i >= 0; --i)
3436 if (NILP (it->stack[i].string))
3437 face_id = it->stack[i].face_id;
3438
3439 return face_id;
3440 }
3441
3442
3443 /* Compute the face one character before or after the current position
3444 of IT. BEFORE_P non-zero means get the face in front of IT's
3445 position. Value is the id of the face. */
3446
3447 static int
3448 face_before_or_after_it_pos (it, before_p)
3449 struct it *it;
3450 int before_p;
3451 {
3452 int face_id, limit;
3453 int next_check_charpos;
3454 struct text_pos pos;
3455
3456 xassert (it->s == NULL);
3457
3458 if (STRINGP (it->string))
3459 {
3460 int bufpos, base_face_id;
3461
3462 /* No face change past the end of the string (for the case
3463 we are padding with spaces). No face change before the
3464 string start. */
3465 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3466 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3467 return it->face_id;
3468
3469 /* Set pos to the position before or after IT's current position. */
3470 if (before_p)
3471 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3472 else
3473 /* For composition, we must check the character after the
3474 composition. */
3475 pos = (it->what == IT_COMPOSITION
3476 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3477 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3478
3479 if (it->current.overlay_string_index >= 0)
3480 bufpos = IT_CHARPOS (*it);
3481 else
3482 bufpos = 0;
3483
3484 base_face_id = underlying_face_id (it);
3485
3486 /* Get the face for ASCII, or unibyte. */
3487 face_id = face_at_string_position (it->w,
3488 it->string,
3489 CHARPOS (pos),
3490 bufpos,
3491 it->region_beg_charpos,
3492 it->region_end_charpos,
3493 &next_check_charpos,
3494 base_face_id, 0);
3495
3496 /* Correct the face for charsets different from ASCII. Do it
3497 for the multibyte case only. The face returned above is
3498 suitable for unibyte text if IT->string is unibyte. */
3499 if (STRING_MULTIBYTE (it->string))
3500 {
3501 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3502 int rest = SBYTES (it->string) - BYTEPOS (pos);
3503 int c, len;
3504 struct face *face = FACE_FROM_ID (it->f, face_id);
3505
3506 c = string_char_and_length (p, rest, &len);
3507 face_id = FACE_FOR_CHAR (it->f, face, c);
3508 }
3509 }
3510 else
3511 {
3512 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3513 || (IT_CHARPOS (*it) <= BEGV && before_p))
3514 return it->face_id;
3515
3516 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3517 pos = it->current.pos;
3518
3519 if (before_p)
3520 DEC_TEXT_POS (pos, it->multibyte_p);
3521 else
3522 {
3523 if (it->what == IT_COMPOSITION)
3524 /* For composition, we must check the position after the
3525 composition. */
3526 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3527 else
3528 INC_TEXT_POS (pos, it->multibyte_p);
3529 }
3530
3531 /* Determine face for CHARSET_ASCII, or unibyte. */
3532 face_id = face_at_buffer_position (it->w,
3533 CHARPOS (pos),
3534 it->region_beg_charpos,
3535 it->region_end_charpos,
3536 &next_check_charpos,
3537 limit, 0);
3538
3539 /* Correct the face for charsets different from ASCII. Do it
3540 for the multibyte case only. The face returned above is
3541 suitable for unibyte text if current_buffer is unibyte. */
3542 if (it->multibyte_p)
3543 {
3544 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3545 struct face *face = FACE_FROM_ID (it->f, face_id);
3546 face_id = FACE_FOR_CHAR (it->f, face, c);
3547 }
3548 }
3549
3550 return face_id;
3551 }
3552
3553
3554 \f
3555 /***********************************************************************
3556 Invisible text
3557 ***********************************************************************/
3558
3559 /* Set up iterator IT from invisible properties at its current
3560 position. Called from handle_stop. */
3561
3562 static enum prop_handled
3563 handle_invisible_prop (it)
3564 struct it *it;
3565 {
3566 enum prop_handled handled = HANDLED_NORMALLY;
3567
3568 if (STRINGP (it->string))
3569 {
3570 extern Lisp_Object Qinvisible;
3571 Lisp_Object prop, end_charpos, limit, charpos;
3572
3573 /* Get the value of the invisible text property at the
3574 current position. Value will be nil if there is no such
3575 property. */
3576 charpos = make_number (IT_STRING_CHARPOS (*it));
3577 prop = Fget_text_property (charpos, Qinvisible, it->string);
3578
3579 if (!NILP (prop)
3580 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3581 {
3582 handled = HANDLED_RECOMPUTE_PROPS;
3583
3584 /* Get the position at which the next change of the
3585 invisible text property can be found in IT->string.
3586 Value will be nil if the property value is the same for
3587 all the rest of IT->string. */
3588 XSETINT (limit, SCHARS (it->string));
3589 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3590 it->string, limit);
3591
3592 /* Text at current position is invisible. The next
3593 change in the property is at position end_charpos.
3594 Move IT's current position to that position. */
3595 if (INTEGERP (end_charpos)
3596 && XFASTINT (end_charpos) < XFASTINT (limit))
3597 {
3598 struct text_pos old;
3599 old = it->current.string_pos;
3600 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3601 compute_string_pos (&it->current.string_pos, old, it->string);
3602 }
3603 else
3604 {
3605 /* The rest of the string is invisible. If this is an
3606 overlay string, proceed with the next overlay string
3607 or whatever comes and return a character from there. */
3608 if (it->current.overlay_string_index >= 0)
3609 {
3610 next_overlay_string (it);
3611 /* Don't check for overlay strings when we just
3612 finished processing them. */
3613 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3614 }
3615 else
3616 {
3617 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3618 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3619 }
3620 }
3621 }
3622 }
3623 else
3624 {
3625 int invis_p, newpos, next_stop, start_charpos;
3626 Lisp_Object pos, prop, overlay;
3627
3628 /* First of all, is there invisible text at this position? */
3629 start_charpos = IT_CHARPOS (*it);
3630 pos = make_number (IT_CHARPOS (*it));
3631 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3632 &overlay);
3633 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3634
3635 /* If we are on invisible text, skip over it. */
3636 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3637 {
3638 /* Record whether we have to display an ellipsis for the
3639 invisible text. */
3640 int display_ellipsis_p = invis_p == 2;
3641
3642 handled = HANDLED_RECOMPUTE_PROPS;
3643
3644 /* Loop skipping over invisible text. The loop is left at
3645 ZV or with IT on the first char being visible again. */
3646 do
3647 {
3648 /* Try to skip some invisible text. Return value is the
3649 position reached which can be equal to IT's position
3650 if there is nothing invisible here. This skips both
3651 over invisible text properties and overlays with
3652 invisible property. */
3653 newpos = skip_invisible (IT_CHARPOS (*it),
3654 &next_stop, ZV, it->window);
3655
3656 /* If we skipped nothing at all we weren't at invisible
3657 text in the first place. If everything to the end of
3658 the buffer was skipped, end the loop. */
3659 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3660 invis_p = 0;
3661 else
3662 {
3663 /* We skipped some characters but not necessarily
3664 all there are. Check if we ended up on visible
3665 text. Fget_char_property returns the property of
3666 the char before the given position, i.e. if we
3667 get invis_p = 0, this means that the char at
3668 newpos is visible. */
3669 pos = make_number (newpos);
3670 prop = Fget_char_property (pos, Qinvisible, it->window);
3671 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3672 }
3673
3674 /* If we ended up on invisible text, proceed to
3675 skip starting with next_stop. */
3676 if (invis_p)
3677 IT_CHARPOS (*it) = next_stop;
3678
3679 /* If there are adjacent invisible texts, don't lose the
3680 second one's ellipsis. */
3681 if (invis_p == 2)
3682 display_ellipsis_p = 1;
3683 }
3684 while (invis_p);
3685
3686 /* The position newpos is now either ZV or on visible text. */
3687 IT_CHARPOS (*it) = newpos;
3688 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3689
3690 /* If there are before-strings at the start of invisible
3691 text, and the text is invisible because of a text
3692 property, arrange to show before-strings because 20.x did
3693 it that way. (If the text is invisible because of an
3694 overlay property instead of a text property, this is
3695 already handled in the overlay code.) */
3696 if (NILP (overlay)
3697 && get_overlay_strings (it, start_charpos))
3698 {
3699 handled = HANDLED_RECOMPUTE_PROPS;
3700 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3701 }
3702 else if (display_ellipsis_p)
3703 {
3704 /* Make sure that the glyphs of the ellipsis will get
3705 correct `charpos' values. If we would not update
3706 it->position here, the glyphs would belong to the
3707 last visible character _before_ the invisible
3708 text, which confuses `set_cursor_from_row'.
3709
3710 We use the last invisible position instead of the
3711 first because this way the cursor is always drawn on
3712 the first "." of the ellipsis, whenever PT is inside
3713 the invisible text. Otherwise the cursor would be
3714 placed _after_ the ellipsis when the point is after the
3715 first invisible character. */
3716 if (!STRINGP (it->object))
3717 {
3718 it->position.charpos = IT_CHARPOS (*it) - 1;
3719 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3720 }
3721 setup_for_ellipsis (it, 0);
3722 /* Let the ellipsis display before
3723 considering any properties of the following char.
3724 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3725 handled = HANDLED_RETURN;
3726 }
3727 }
3728 }
3729
3730 return handled;
3731 }
3732
3733
3734 /* Make iterator IT return `...' next.
3735 Replaces LEN characters from buffer. */
3736
3737 static void
3738 setup_for_ellipsis (it, len)
3739 struct it *it;
3740 int len;
3741 {
3742 /* Use the display table definition for `...'. Invalid glyphs
3743 will be handled by the method returning elements from dpvec. */
3744 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3745 {
3746 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3747 it->dpvec = v->contents;
3748 it->dpend = v->contents + v->size;
3749 }
3750 else
3751 {
3752 /* Default `...'. */
3753 it->dpvec = default_invis_vector;
3754 it->dpend = default_invis_vector + 3;
3755 }
3756
3757 it->dpvec_char_len = len;
3758 it->current.dpvec_index = 0;
3759 it->dpvec_face_id = -1;
3760
3761 /* Remember the current face id in case glyphs specify faces.
3762 IT's face is restored in set_iterator_to_next.
3763 saved_face_id was set to preceding char's face in handle_stop. */
3764 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3765 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3766
3767 it->method = GET_FROM_DISPLAY_VECTOR;
3768 it->ellipsis_p = 1;
3769 }
3770
3771
3772 \f
3773 /***********************************************************************
3774 'display' property
3775 ***********************************************************************/
3776
3777 /* Set up iterator IT from `display' property at its current position.
3778 Called from handle_stop.
3779 We return HANDLED_RETURN if some part of the display property
3780 overrides the display of the buffer text itself.
3781 Otherwise we return HANDLED_NORMALLY. */
3782
3783 static enum prop_handled
3784 handle_display_prop (it)
3785 struct it *it;
3786 {
3787 Lisp_Object prop, object;
3788 struct text_pos *position;
3789 /* Nonzero if some property replaces the display of the text itself. */
3790 int display_replaced_p = 0;
3791
3792 if (STRINGP (it->string))
3793 {
3794 object = it->string;
3795 position = &it->current.string_pos;
3796 }
3797 else
3798 {
3799 XSETWINDOW (object, it->w);
3800 position = &it->current.pos;
3801 }
3802
3803 /* Reset those iterator values set from display property values. */
3804 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3805 it->space_width = Qnil;
3806 it->font_height = Qnil;
3807 it->voffset = 0;
3808
3809 /* We don't support recursive `display' properties, i.e. string
3810 values that have a string `display' property, that have a string
3811 `display' property etc. */
3812 if (!it->string_from_display_prop_p)
3813 it->area = TEXT_AREA;
3814
3815 prop = Fget_char_property (make_number (position->charpos),
3816 Qdisplay, object);
3817 if (NILP (prop))
3818 return HANDLED_NORMALLY;
3819
3820 if (!STRINGP (it->string))
3821 object = it->w->buffer;
3822
3823 if (CONSP (prop)
3824 /* Simple properties. */
3825 && !EQ (XCAR (prop), Qimage)
3826 && !EQ (XCAR (prop), Qspace)
3827 && !EQ (XCAR (prop), Qwhen)
3828 && !EQ (XCAR (prop), Qslice)
3829 && !EQ (XCAR (prop), Qspace_width)
3830 && !EQ (XCAR (prop), Qheight)
3831 && !EQ (XCAR (prop), Qraise)
3832 /* Marginal area specifications. */
3833 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3834 && !EQ (XCAR (prop), Qleft_fringe)
3835 && !EQ (XCAR (prop), Qright_fringe)
3836 && !NILP (XCAR (prop)))
3837 {
3838 for (; CONSP (prop); prop = XCDR (prop))
3839 {
3840 if (handle_single_display_spec (it, XCAR (prop), object,
3841 position, display_replaced_p))
3842 display_replaced_p = 1;
3843 }
3844 }
3845 else if (VECTORP (prop))
3846 {
3847 int i;
3848 for (i = 0; i < ASIZE (prop); ++i)
3849 if (handle_single_display_spec (it, AREF (prop, i), object,
3850 position, display_replaced_p))
3851 display_replaced_p = 1;
3852 }
3853 else
3854 {
3855 int ret = handle_single_display_spec (it, prop, object, position, 0);
3856 if (ret < 0) /* Replaced by "", i.e. nothing. */
3857 return HANDLED_RECOMPUTE_PROPS;
3858 if (ret)
3859 display_replaced_p = 1;
3860 }
3861
3862 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3863 }
3864
3865
3866 /* Value is the position of the end of the `display' property starting
3867 at START_POS in OBJECT. */
3868
3869 static struct text_pos
3870 display_prop_end (it, object, start_pos)
3871 struct it *it;
3872 Lisp_Object object;
3873 struct text_pos start_pos;
3874 {
3875 Lisp_Object end;
3876 struct text_pos end_pos;
3877
3878 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3879 Qdisplay, object, Qnil);
3880 CHARPOS (end_pos) = XFASTINT (end);
3881 if (STRINGP (object))
3882 compute_string_pos (&end_pos, start_pos, it->string);
3883 else
3884 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3885
3886 return end_pos;
3887 }
3888
3889
3890 /* Set up IT from a single `display' specification PROP. OBJECT
3891 is the object in which the `display' property was found. *POSITION
3892 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3893 means that we previously saw a display specification which already
3894 replaced text display with something else, for example an image;
3895 we ignore such properties after the first one has been processed.
3896
3897 If PROP is a `space' or `image' specification, and in some other
3898 cases too, set *POSITION to the position where the `display'
3899 property ends.
3900
3901 Value is non-zero if something was found which replaces the display
3902 of buffer or string text. Specifically, the value is -1 if that
3903 "something" is "nothing". */
3904
3905 static int
3906 handle_single_display_spec (it, spec, object, position,
3907 display_replaced_before_p)
3908 struct it *it;
3909 Lisp_Object spec;
3910 Lisp_Object object;
3911 struct text_pos *position;
3912 int display_replaced_before_p;
3913 {
3914 Lisp_Object form;
3915 Lisp_Object location, value;
3916 struct text_pos start_pos, save_pos;
3917 int valid_p;
3918
3919 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3920 If the result is non-nil, use VALUE instead of SPEC. */
3921 form = Qt;
3922 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3923 {
3924 spec = XCDR (spec);
3925 if (!CONSP (spec))
3926 return 0;
3927 form = XCAR (spec);
3928 spec = XCDR (spec);
3929 }
3930
3931 if (!NILP (form) && !EQ (form, Qt))
3932 {
3933 int count = SPECPDL_INDEX ();
3934 struct gcpro gcpro1;
3935
3936 /* Bind `object' to the object having the `display' property, a
3937 buffer or string. Bind `position' to the position in the
3938 object where the property was found, and `buffer-position'
3939 to the current position in the buffer. */
3940 specbind (Qobject, object);
3941 specbind (Qposition, make_number (CHARPOS (*position)));
3942 specbind (Qbuffer_position,
3943 make_number (STRINGP (object)
3944 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3945 GCPRO1 (form);
3946 form = safe_eval (form);
3947 UNGCPRO;
3948 unbind_to (count, Qnil);
3949 }
3950
3951 if (NILP (form))
3952 return 0;
3953
3954 /* Handle `(height HEIGHT)' specifications. */
3955 if (CONSP (spec)
3956 && EQ (XCAR (spec), Qheight)
3957 && CONSP (XCDR (spec)))
3958 {
3959 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3960 return 0;
3961
3962 it->font_height = XCAR (XCDR (spec));
3963 if (!NILP (it->font_height))
3964 {
3965 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3966 int new_height = -1;
3967
3968 if (CONSP (it->font_height)
3969 && (EQ (XCAR (it->font_height), Qplus)
3970 || EQ (XCAR (it->font_height), Qminus))
3971 && CONSP (XCDR (it->font_height))
3972 && INTEGERP (XCAR (XCDR (it->font_height))))
3973 {
3974 /* `(+ N)' or `(- N)' where N is an integer. */
3975 int steps = XINT (XCAR (XCDR (it->font_height)));
3976 if (EQ (XCAR (it->font_height), Qplus))
3977 steps = - steps;
3978 it->face_id = smaller_face (it->f, it->face_id, steps);
3979 }
3980 else if (FUNCTIONP (it->font_height))
3981 {
3982 /* Call function with current height as argument.
3983 Value is the new height. */
3984 Lisp_Object height;
3985 height = safe_call1 (it->font_height,
3986 face->lface[LFACE_HEIGHT_INDEX]);
3987 if (NUMBERP (height))
3988 new_height = XFLOATINT (height);
3989 }
3990 else if (NUMBERP (it->font_height))
3991 {
3992 /* Value is a multiple of the canonical char height. */
3993 struct face *face;
3994
3995 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3996 new_height = (XFLOATINT (it->font_height)
3997 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3998 }
3999 else
4000 {
4001 /* Evaluate IT->font_height with `height' bound to the
4002 current specified height to get the new height. */
4003 int count = SPECPDL_INDEX ();
4004
4005 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4006 value = safe_eval (it->font_height);
4007 unbind_to (count, Qnil);
4008
4009 if (NUMBERP (value))
4010 new_height = XFLOATINT (value);
4011 }
4012
4013 if (new_height > 0)
4014 it->face_id = face_with_height (it->f, it->face_id, new_height);
4015 }
4016
4017 return 0;
4018 }
4019
4020 /* Handle `(space_width WIDTH)'. */
4021 if (CONSP (spec)
4022 && EQ (XCAR (spec), Qspace_width)
4023 && CONSP (XCDR (spec)))
4024 {
4025 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4026 return 0;
4027
4028 value = XCAR (XCDR (spec));
4029 if (NUMBERP (value) && XFLOATINT (value) > 0)
4030 it->space_width = value;
4031
4032 return 0;
4033 }
4034
4035 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4036 if (CONSP (spec)
4037 && EQ (XCAR (spec), Qslice))
4038 {
4039 Lisp_Object tem;
4040
4041 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4042 return 0;
4043
4044 if (tem = XCDR (spec), CONSP (tem))
4045 {
4046 it->slice.x = XCAR (tem);
4047 if (tem = XCDR (tem), CONSP (tem))
4048 {
4049 it->slice.y = XCAR (tem);
4050 if (tem = XCDR (tem), CONSP (tem))
4051 {
4052 it->slice.width = XCAR (tem);
4053 if (tem = XCDR (tem), CONSP (tem))
4054 it->slice.height = XCAR (tem);
4055 }
4056 }
4057 }
4058
4059 return 0;
4060 }
4061
4062 /* Handle `(raise FACTOR)'. */
4063 if (CONSP (spec)
4064 && EQ (XCAR (spec), Qraise)
4065 && CONSP (XCDR (spec)))
4066 {
4067 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4068 return 0;
4069
4070 #ifdef HAVE_WINDOW_SYSTEM
4071 value = XCAR (XCDR (spec));
4072 if (NUMBERP (value))
4073 {
4074 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4075 it->voffset = - (XFLOATINT (value)
4076 * (FONT_HEIGHT (face->font)));
4077 }
4078 #endif /* HAVE_WINDOW_SYSTEM */
4079
4080 return 0;
4081 }
4082
4083 /* Don't handle the other kinds of display specifications
4084 inside a string that we got from a `display' property. */
4085 if (it->string_from_display_prop_p)
4086 return 0;
4087
4088 /* Characters having this form of property are not displayed, so
4089 we have to find the end of the property. */
4090 start_pos = *position;
4091 *position = display_prop_end (it, object, start_pos);
4092 value = Qnil;
4093
4094 /* Stop the scan at that end position--we assume that all
4095 text properties change there. */
4096 it->stop_charpos = position->charpos;
4097
4098 /* Handle `(left-fringe BITMAP [FACE])'
4099 and `(right-fringe BITMAP [FACE])'. */
4100 if (CONSP (spec)
4101 && (EQ (XCAR (spec), Qleft_fringe)
4102 || EQ (XCAR (spec), Qright_fringe))
4103 && CONSP (XCDR (spec)))
4104 {
4105 int face_id = DEFAULT_FACE_ID;
4106 int fringe_bitmap;
4107
4108 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4109 /* If we return here, POSITION has been advanced
4110 across the text with this property. */
4111 return 0;
4112
4113 #ifdef HAVE_WINDOW_SYSTEM
4114 value = XCAR (XCDR (spec));
4115 if (!SYMBOLP (value)
4116 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4117 /* If we return here, POSITION has been advanced
4118 across the text with this property. */
4119 return 0;
4120
4121 if (CONSP (XCDR (XCDR (spec))))
4122 {
4123 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4124 int face_id2 = lookup_derived_face (it->f, face_name,
4125 'A', FRINGE_FACE_ID, 0);
4126 if (face_id2 >= 0)
4127 face_id = face_id2;
4128 }
4129
4130 /* Save current settings of IT so that we can restore them
4131 when we are finished with the glyph property value. */
4132
4133 save_pos = it->position;
4134 it->position = *position;
4135 push_it (it);
4136 it->position = save_pos;
4137
4138 it->area = TEXT_AREA;
4139 it->what = IT_IMAGE;
4140 it->image_id = -1; /* no image */
4141 it->position = start_pos;
4142 it->object = NILP (object) ? it->w->buffer : object;
4143 it->method = GET_FROM_IMAGE;
4144 it->face_id = face_id;
4145
4146 /* Say that we haven't consumed the characters with
4147 `display' property yet. The call to pop_it in
4148 set_iterator_to_next will clean this up. */
4149 *position = start_pos;
4150
4151 if (EQ (XCAR (spec), Qleft_fringe))
4152 {
4153 it->left_user_fringe_bitmap = fringe_bitmap;
4154 it->left_user_fringe_face_id = face_id;
4155 }
4156 else
4157 {
4158 it->right_user_fringe_bitmap = fringe_bitmap;
4159 it->right_user_fringe_face_id = face_id;
4160 }
4161 #endif /* HAVE_WINDOW_SYSTEM */
4162 return 1;
4163 }
4164
4165 /* Prepare to handle `((margin left-margin) ...)',
4166 `((margin right-margin) ...)' and `((margin nil) ...)'
4167 prefixes for display specifications. */
4168 location = Qunbound;
4169 if (CONSP (spec) && CONSP (XCAR (spec)))
4170 {
4171 Lisp_Object tem;
4172
4173 value = XCDR (spec);
4174 if (CONSP (value))
4175 value = XCAR (value);
4176
4177 tem = XCAR (spec);
4178 if (EQ (XCAR (tem), Qmargin)
4179 && (tem = XCDR (tem),
4180 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4181 (NILP (tem)
4182 || EQ (tem, Qleft_margin)
4183 || EQ (tem, Qright_margin))))
4184 location = tem;
4185 }
4186
4187 if (EQ (location, Qunbound))
4188 {
4189 location = Qnil;
4190 value = spec;
4191 }
4192
4193 /* After this point, VALUE is the property after any
4194 margin prefix has been stripped. It must be a string,
4195 an image specification, or `(space ...)'.
4196
4197 LOCATION specifies where to display: `left-margin',
4198 `right-margin' or nil. */
4199
4200 valid_p = (STRINGP (value)
4201 #ifdef HAVE_WINDOW_SYSTEM
4202 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4203 #endif /* not HAVE_WINDOW_SYSTEM */
4204 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4205
4206 if (valid_p && !display_replaced_before_p)
4207 {
4208 /* Save current settings of IT so that we can restore them
4209 when we are finished with the glyph property value. */
4210 save_pos = it->position;
4211 it->position = *position;
4212 push_it (it);
4213 it->position = save_pos;
4214
4215 if (NILP (location))
4216 it->area = TEXT_AREA;
4217 else if (EQ (location, Qleft_margin))
4218 it->area = LEFT_MARGIN_AREA;
4219 else
4220 it->area = RIGHT_MARGIN_AREA;
4221
4222 if (STRINGP (value))
4223 {
4224 if (SCHARS (value) == 0)
4225 {
4226 pop_it (it);
4227 return -1; /* Replaced by "", i.e. nothing. */
4228 }
4229 it->string = value;
4230 it->multibyte_p = STRING_MULTIBYTE (it->string);
4231 it->current.overlay_string_index = -1;
4232 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4233 it->end_charpos = it->string_nchars = SCHARS (it->string);
4234 it->method = GET_FROM_STRING;
4235 it->stop_charpos = 0;
4236 it->string_from_display_prop_p = 1;
4237 /* Say that we haven't consumed the characters with
4238 `display' property yet. The call to pop_it in
4239 set_iterator_to_next will clean this up. */
4240 *position = start_pos;
4241 }
4242 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4243 {
4244 it->method = GET_FROM_STRETCH;
4245 it->object = value;
4246 *position = it->position = start_pos;
4247 }
4248 #ifdef HAVE_WINDOW_SYSTEM
4249 else
4250 {
4251 it->what = IT_IMAGE;
4252 it->image_id = lookup_image (it->f, value);
4253 it->position = start_pos;
4254 it->object = NILP (object) ? it->w->buffer : object;
4255 it->method = GET_FROM_IMAGE;
4256
4257 /* Say that we haven't consumed the characters with
4258 `display' property yet. The call to pop_it in
4259 set_iterator_to_next will clean this up. */
4260 *position = start_pos;
4261 }
4262 #endif /* HAVE_WINDOW_SYSTEM */
4263
4264 return 1;
4265 }
4266
4267 /* Invalid property or property not supported. Restore
4268 POSITION to what it was before. */
4269 *position = start_pos;
4270 return 0;
4271 }
4272
4273
4274 /* Check if SPEC is a display specification value whose text should be
4275 treated as intangible. */
4276
4277 static int
4278 single_display_spec_intangible_p (prop)
4279 Lisp_Object prop;
4280 {
4281 /* Skip over `when FORM'. */
4282 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4283 {
4284 prop = XCDR (prop);
4285 if (!CONSP (prop))
4286 return 0;
4287 prop = XCDR (prop);
4288 }
4289
4290 if (STRINGP (prop))
4291 return 1;
4292
4293 if (!CONSP (prop))
4294 return 0;
4295
4296 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4297 we don't need to treat text as intangible. */
4298 if (EQ (XCAR (prop), Qmargin))
4299 {
4300 prop = XCDR (prop);
4301 if (!CONSP (prop))
4302 return 0;
4303
4304 prop = XCDR (prop);
4305 if (!CONSP (prop)
4306 || EQ (XCAR (prop), Qleft_margin)
4307 || EQ (XCAR (prop), Qright_margin))
4308 return 0;
4309 }
4310
4311 return (CONSP (prop)
4312 && (EQ (XCAR (prop), Qimage)
4313 || EQ (XCAR (prop), Qspace)));
4314 }
4315
4316
4317 /* Check if PROP is a display property value whose text should be
4318 treated as intangible. */
4319
4320 int
4321 display_prop_intangible_p (prop)
4322 Lisp_Object prop;
4323 {
4324 if (CONSP (prop)
4325 && CONSP (XCAR (prop))
4326 && !EQ (Qmargin, XCAR (XCAR (prop))))
4327 {
4328 /* A list of sub-properties. */
4329 while (CONSP (prop))
4330 {
4331 if (single_display_spec_intangible_p (XCAR (prop)))
4332 return 1;
4333 prop = XCDR (prop);
4334 }
4335 }
4336 else if (VECTORP (prop))
4337 {
4338 /* A vector of sub-properties. */
4339 int i;
4340 for (i = 0; i < ASIZE (prop); ++i)
4341 if (single_display_spec_intangible_p (AREF (prop, i)))
4342 return 1;
4343 }
4344 else
4345 return single_display_spec_intangible_p (prop);
4346
4347 return 0;
4348 }
4349
4350
4351 /* Return 1 if PROP is a display sub-property value containing STRING. */
4352
4353 static int
4354 single_display_spec_string_p (prop, string)
4355 Lisp_Object prop, string;
4356 {
4357 if (EQ (string, prop))
4358 return 1;
4359
4360 /* Skip over `when FORM'. */
4361 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4362 {
4363 prop = XCDR (prop);
4364 if (!CONSP (prop))
4365 return 0;
4366 prop = XCDR (prop);
4367 }
4368
4369 if (CONSP (prop))
4370 /* Skip over `margin LOCATION'. */
4371 if (EQ (XCAR (prop), Qmargin))
4372 {
4373 prop = XCDR (prop);
4374 if (!CONSP (prop))
4375 return 0;
4376
4377 prop = XCDR (prop);
4378 if (!CONSP (prop))
4379 return 0;
4380 }
4381
4382 return CONSP (prop) && EQ (XCAR (prop), string);
4383 }
4384
4385
4386 /* Return 1 if STRING appears in the `display' property PROP. */
4387
4388 static int
4389 display_prop_string_p (prop, string)
4390 Lisp_Object prop, string;
4391 {
4392 if (CONSP (prop)
4393 && CONSP (XCAR (prop))
4394 && !EQ (Qmargin, XCAR (XCAR (prop))))
4395 {
4396 /* A list of sub-properties. */
4397 while (CONSP (prop))
4398 {
4399 if (single_display_spec_string_p (XCAR (prop), string))
4400 return 1;
4401 prop = XCDR (prop);
4402 }
4403 }
4404 else if (VECTORP (prop))
4405 {
4406 /* A vector of sub-properties. */
4407 int i;
4408 for (i = 0; i < ASIZE (prop); ++i)
4409 if (single_display_spec_string_p (AREF (prop, i), string))
4410 return 1;
4411 }
4412 else
4413 return single_display_spec_string_p (prop, string);
4414
4415 return 0;
4416 }
4417
4418
4419 /* Determine from which buffer position in W's buffer STRING comes
4420 from. AROUND_CHARPOS is an approximate position where it could
4421 be from. Value is the buffer position or 0 if it couldn't be
4422 determined.
4423
4424 W's buffer must be current.
4425
4426 This function is necessary because we don't record buffer positions
4427 in glyphs generated from strings (to keep struct glyph small).
4428 This function may only use code that doesn't eval because it is
4429 called asynchronously from note_mouse_highlight. */
4430
4431 int
4432 string_buffer_position (w, string, around_charpos)
4433 struct window *w;
4434 Lisp_Object string;
4435 int around_charpos;
4436 {
4437 Lisp_Object limit, prop, pos;
4438 const int MAX_DISTANCE = 1000;
4439 int found = 0;
4440
4441 pos = make_number (around_charpos);
4442 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4443 while (!found && !EQ (pos, limit))
4444 {
4445 prop = Fget_char_property (pos, Qdisplay, Qnil);
4446 if (!NILP (prop) && display_prop_string_p (prop, string))
4447 found = 1;
4448 else
4449 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4450 }
4451
4452 if (!found)
4453 {
4454 pos = make_number (around_charpos);
4455 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4456 while (!found && !EQ (pos, limit))
4457 {
4458 prop = Fget_char_property (pos, Qdisplay, Qnil);
4459 if (!NILP (prop) && display_prop_string_p (prop, string))
4460 found = 1;
4461 else
4462 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4463 limit);
4464 }
4465 }
4466
4467 return found ? XINT (pos) : 0;
4468 }
4469
4470
4471 \f
4472 /***********************************************************************
4473 `composition' property
4474 ***********************************************************************/
4475
4476 /* Set up iterator IT from `composition' property at its current
4477 position. Called from handle_stop. */
4478
4479 static enum prop_handled
4480 handle_composition_prop (it)
4481 struct it *it;
4482 {
4483 Lisp_Object prop, string;
4484 int pos, pos_byte, end;
4485 enum prop_handled handled = HANDLED_NORMALLY;
4486
4487 if (STRINGP (it->string))
4488 {
4489 pos = IT_STRING_CHARPOS (*it);
4490 pos_byte = IT_STRING_BYTEPOS (*it);
4491 string = it->string;
4492 }
4493 else
4494 {
4495 pos = IT_CHARPOS (*it);
4496 pos_byte = IT_BYTEPOS (*it);
4497 string = Qnil;
4498 }
4499
4500 /* If there's a valid composition and point is not inside of the
4501 composition (in the case that the composition is from the current
4502 buffer), draw a glyph composed from the composition components. */
4503 if (find_composition (pos, -1, &pos, &end, &prop, string)
4504 && COMPOSITION_VALID_P (pos, end, prop)
4505 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4506 {
4507 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4508
4509 if (id >= 0)
4510 {
4511 struct composition *cmp = composition_table[id];
4512
4513 if (cmp->glyph_len == 0)
4514 {
4515 /* No glyph. */
4516 if (STRINGP (it->string))
4517 {
4518 IT_STRING_CHARPOS (*it) = end;
4519 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4520 end);
4521 }
4522 else
4523 {
4524 IT_CHARPOS (*it) = end;
4525 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4526 }
4527 return HANDLED_RECOMPUTE_PROPS;
4528 }
4529
4530 it->stop_charpos = end;
4531 push_it (it);
4532
4533 it->method = GET_FROM_COMPOSITION;
4534 it->cmp_id = id;
4535 it->cmp_len = COMPOSITION_LENGTH (prop);
4536 /* For a terminal, draw only the first character of the
4537 components. */
4538 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4539 it->len = (STRINGP (it->string)
4540 ? string_char_to_byte (it->string, end)
4541 : CHAR_TO_BYTE (end)) - pos_byte;
4542 handled = HANDLED_RETURN;
4543 }
4544 }
4545
4546 return handled;
4547 }
4548
4549
4550 \f
4551 /***********************************************************************
4552 Overlay strings
4553 ***********************************************************************/
4554
4555 /* The following structure is used to record overlay strings for
4556 later sorting in load_overlay_strings. */
4557
4558 struct overlay_entry
4559 {
4560 Lisp_Object overlay;
4561 Lisp_Object string;
4562 int priority;
4563 int after_string_p;
4564 };
4565
4566
4567 /* Set up iterator IT from overlay strings at its current position.
4568 Called from handle_stop. */
4569
4570 static enum prop_handled
4571 handle_overlay_change (it)
4572 struct it *it;
4573 {
4574 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4575 return HANDLED_RECOMPUTE_PROPS;
4576 else
4577 return HANDLED_NORMALLY;
4578 }
4579
4580
4581 /* Set up the next overlay string for delivery by IT, if there is an
4582 overlay string to deliver. Called by set_iterator_to_next when the
4583 end of the current overlay string is reached. If there are more
4584 overlay strings to display, IT->string and
4585 IT->current.overlay_string_index are set appropriately here.
4586 Otherwise IT->string is set to nil. */
4587
4588 static void
4589 next_overlay_string (it)
4590 struct it *it;
4591 {
4592 ++it->current.overlay_string_index;
4593 if (it->current.overlay_string_index == it->n_overlay_strings)
4594 {
4595 /* No more overlay strings. Restore IT's settings to what
4596 they were before overlay strings were processed, and
4597 continue to deliver from current_buffer. */
4598 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4599
4600 pop_it (it);
4601 xassert (it->sp > 0
4602 || it->method == GET_FROM_COMPOSITION
4603 || (NILP (it->string)
4604 && it->method == GET_FROM_BUFFER
4605 && it->stop_charpos >= BEGV
4606 && it->stop_charpos <= it->end_charpos));
4607 it->current.overlay_string_index = -1;
4608 it->n_overlay_strings = 0;
4609
4610 /* If we're at the end of the buffer, record that we have
4611 processed the overlay strings there already, so that
4612 next_element_from_buffer doesn't try it again. */
4613 if (IT_CHARPOS (*it) >= it->end_charpos)
4614 it->overlay_strings_at_end_processed_p = 1;
4615
4616 /* If we have to display `...' for invisible text, set
4617 the iterator up for that. */
4618 if (display_ellipsis_p)
4619 setup_for_ellipsis (it, 0);
4620 }
4621 else
4622 {
4623 /* There are more overlay strings to process. If
4624 IT->current.overlay_string_index has advanced to a position
4625 where we must load IT->overlay_strings with more strings, do
4626 it. */
4627 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4628
4629 if (it->current.overlay_string_index && i == 0)
4630 load_overlay_strings (it, 0);
4631
4632 /* Initialize IT to deliver display elements from the overlay
4633 string. */
4634 it->string = it->overlay_strings[i];
4635 it->multibyte_p = STRING_MULTIBYTE (it->string);
4636 SET_TEXT_POS (it->current.string_pos, 0, 0);
4637 it->method = GET_FROM_STRING;
4638 it->stop_charpos = 0;
4639 }
4640
4641 CHECK_IT (it);
4642 }
4643
4644
4645 /* Compare two overlay_entry structures E1 and E2. Used as a
4646 comparison function for qsort in load_overlay_strings. Overlay
4647 strings for the same position are sorted so that
4648
4649 1. All after-strings come in front of before-strings, except
4650 when they come from the same overlay.
4651
4652 2. Within after-strings, strings are sorted so that overlay strings
4653 from overlays with higher priorities come first.
4654
4655 2. Within before-strings, strings are sorted so that overlay
4656 strings from overlays with higher priorities come last.
4657
4658 Value is analogous to strcmp. */
4659
4660
4661 static int
4662 compare_overlay_entries (e1, e2)
4663 void *e1, *e2;
4664 {
4665 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4666 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4667 int result;
4668
4669 if (entry1->after_string_p != entry2->after_string_p)
4670 {
4671 /* Let after-strings appear in front of before-strings if
4672 they come from different overlays. */
4673 if (EQ (entry1->overlay, entry2->overlay))
4674 result = entry1->after_string_p ? 1 : -1;
4675 else
4676 result = entry1->after_string_p ? -1 : 1;
4677 }
4678 else if (entry1->after_string_p)
4679 /* After-strings sorted in order of decreasing priority. */
4680 result = entry2->priority - entry1->priority;
4681 else
4682 /* Before-strings sorted in order of increasing priority. */
4683 result = entry1->priority - entry2->priority;
4684
4685 return result;
4686 }
4687
4688
4689 /* Load the vector IT->overlay_strings with overlay strings from IT's
4690 current buffer position, or from CHARPOS if that is > 0. Set
4691 IT->n_overlays to the total number of overlay strings found.
4692
4693 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4694 a time. On entry into load_overlay_strings,
4695 IT->current.overlay_string_index gives the number of overlay
4696 strings that have already been loaded by previous calls to this
4697 function.
4698
4699 IT->add_overlay_start contains an additional overlay start
4700 position to consider for taking overlay strings from, if non-zero.
4701 This position comes into play when the overlay has an `invisible'
4702 property, and both before and after-strings. When we've skipped to
4703 the end of the overlay, because of its `invisible' property, we
4704 nevertheless want its before-string to appear.
4705 IT->add_overlay_start will contain the overlay start position
4706 in this case.
4707
4708 Overlay strings are sorted so that after-string strings come in
4709 front of before-string strings. Within before and after-strings,
4710 strings are sorted by overlay priority. See also function
4711 compare_overlay_entries. */
4712
4713 static void
4714 load_overlay_strings (it, charpos)
4715 struct it *it;
4716 int charpos;
4717 {
4718 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4719 Lisp_Object overlay, window, str, invisible;
4720 struct Lisp_Overlay *ov;
4721 int start, end;
4722 int size = 20;
4723 int n = 0, i, j, invis_p;
4724 struct overlay_entry *entries
4725 = (struct overlay_entry *) alloca (size * sizeof *entries);
4726
4727 if (charpos <= 0)
4728 charpos = IT_CHARPOS (*it);
4729
4730 /* Append the overlay string STRING of overlay OVERLAY to vector
4731 `entries' which has size `size' and currently contains `n'
4732 elements. AFTER_P non-zero means STRING is an after-string of
4733 OVERLAY. */
4734 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4735 do \
4736 { \
4737 Lisp_Object priority; \
4738 \
4739 if (n == size) \
4740 { \
4741 int new_size = 2 * size; \
4742 struct overlay_entry *old = entries; \
4743 entries = \
4744 (struct overlay_entry *) alloca (new_size \
4745 * sizeof *entries); \
4746 bcopy (old, entries, size * sizeof *entries); \
4747 size = new_size; \
4748 } \
4749 \
4750 entries[n].string = (STRING); \
4751 entries[n].overlay = (OVERLAY); \
4752 priority = Foverlay_get ((OVERLAY), Qpriority); \
4753 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4754 entries[n].after_string_p = (AFTER_P); \
4755 ++n; \
4756 } \
4757 while (0)
4758
4759 /* Process overlay before the overlay center. */
4760 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4761 {
4762 XSETMISC (overlay, ov);
4763 xassert (OVERLAYP (overlay));
4764 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4765 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4766
4767 if (end < charpos)
4768 break;
4769
4770 /* Skip this overlay if it doesn't start or end at IT's current
4771 position. */
4772 if (end != charpos && start != charpos)
4773 continue;
4774
4775 /* Skip this overlay if it doesn't apply to IT->w. */
4776 window = Foverlay_get (overlay, Qwindow);
4777 if (WINDOWP (window) && XWINDOW (window) != it->w)
4778 continue;
4779
4780 /* If the text ``under'' the overlay is invisible, both before-
4781 and after-strings from this overlay are visible; start and
4782 end position are indistinguishable. */
4783 invisible = Foverlay_get (overlay, Qinvisible);
4784 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4785
4786 /* If overlay has a non-empty before-string, record it. */
4787 if ((start == charpos || (end == charpos && invis_p))
4788 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4789 && SCHARS (str))
4790 RECORD_OVERLAY_STRING (overlay, str, 0);
4791
4792 /* If overlay has a non-empty after-string, record it. */
4793 if ((end == charpos || (start == charpos && invis_p))
4794 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4795 && SCHARS (str))
4796 RECORD_OVERLAY_STRING (overlay, str, 1);
4797 }
4798
4799 /* Process overlays after the overlay center. */
4800 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4801 {
4802 XSETMISC (overlay, ov);
4803 xassert (OVERLAYP (overlay));
4804 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4805 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4806
4807 if (start > charpos)
4808 break;
4809
4810 /* Skip this overlay if it doesn't start or end at IT's current
4811 position. */
4812 if (end != charpos && start != charpos)
4813 continue;
4814
4815 /* Skip this overlay if it doesn't apply to IT->w. */
4816 window = Foverlay_get (overlay, Qwindow);
4817 if (WINDOWP (window) && XWINDOW (window) != it->w)
4818 continue;
4819
4820 /* If the text ``under'' the overlay is invisible, it has a zero
4821 dimension, and both before- and after-strings apply. */
4822 invisible = Foverlay_get (overlay, Qinvisible);
4823 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4824
4825 /* If overlay has a non-empty before-string, record it. */
4826 if ((start == charpos || (end == charpos && invis_p))
4827 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4828 && SCHARS (str))
4829 RECORD_OVERLAY_STRING (overlay, str, 0);
4830
4831 /* If overlay has a non-empty after-string, record it. */
4832 if ((end == charpos || (start == charpos && invis_p))
4833 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4834 && SCHARS (str))
4835 RECORD_OVERLAY_STRING (overlay, str, 1);
4836 }
4837
4838 #undef RECORD_OVERLAY_STRING
4839
4840 /* Sort entries. */
4841 if (n > 1)
4842 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4843
4844 /* Record the total number of strings to process. */
4845 it->n_overlay_strings = n;
4846
4847 /* IT->current.overlay_string_index is the number of overlay strings
4848 that have already been consumed by IT. Copy some of the
4849 remaining overlay strings to IT->overlay_strings. */
4850 i = 0;
4851 j = it->current.overlay_string_index;
4852 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4853 it->overlay_strings[i++] = entries[j++].string;
4854
4855 CHECK_IT (it);
4856 }
4857
4858
4859 /* Get the first chunk of overlay strings at IT's current buffer
4860 position, or at CHARPOS if that is > 0. Value is non-zero if at
4861 least one overlay string was found. */
4862
4863 static int
4864 get_overlay_strings_1 (it, charpos, compute_stop_p)
4865 struct it *it;
4866 int charpos;
4867 {
4868 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4869 process. This fills IT->overlay_strings with strings, and sets
4870 IT->n_overlay_strings to the total number of strings to process.
4871 IT->pos.overlay_string_index has to be set temporarily to zero
4872 because load_overlay_strings needs this; it must be set to -1
4873 when no overlay strings are found because a zero value would
4874 indicate a position in the first overlay string. */
4875 it->current.overlay_string_index = 0;
4876 load_overlay_strings (it, charpos);
4877
4878 /* If we found overlay strings, set up IT to deliver display
4879 elements from the first one. Otherwise set up IT to deliver
4880 from current_buffer. */
4881 if (it->n_overlay_strings)
4882 {
4883 /* Make sure we know settings in current_buffer, so that we can
4884 restore meaningful values when we're done with the overlay
4885 strings. */
4886 if (compute_stop_p)
4887 compute_stop_pos (it);
4888 xassert (it->face_id >= 0);
4889
4890 /* Save IT's settings. They are restored after all overlay
4891 strings have been processed. */
4892 xassert (!compute_stop_p || it->sp == 0);
4893 push_it (it);
4894
4895 /* Set up IT to deliver display elements from the first overlay
4896 string. */
4897 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4898 it->string = it->overlay_strings[0];
4899 it->stop_charpos = 0;
4900 xassert (STRINGP (it->string));
4901 it->end_charpos = SCHARS (it->string);
4902 it->multibyte_p = STRING_MULTIBYTE (it->string);
4903 it->method = GET_FROM_STRING;
4904 return 1;
4905 }
4906
4907 it->current.overlay_string_index = -1;
4908 return 0;
4909 }
4910
4911 static int
4912 get_overlay_strings (it, charpos)
4913 struct it *it;
4914 int charpos;
4915 {
4916 it->string = Qnil;
4917 it->method = GET_FROM_BUFFER;
4918
4919 (void) get_overlay_strings_1 (it, charpos, 1);
4920
4921 CHECK_IT (it);
4922
4923 /* Value is non-zero if we found at least one overlay string. */
4924 return STRINGP (it->string);
4925 }
4926
4927
4928 \f
4929 /***********************************************************************
4930 Saving and restoring state
4931 ***********************************************************************/
4932
4933 /* Save current settings of IT on IT->stack. Called, for example,
4934 before setting up IT for an overlay string, to be able to restore
4935 IT's settings to what they were after the overlay string has been
4936 processed. */
4937
4938 static void
4939 push_it (it)
4940 struct it *it;
4941 {
4942 struct iterator_stack_entry *p;
4943
4944 xassert (it->sp < IT_STACK_SIZE);
4945 p = it->stack + it->sp;
4946
4947 p->stop_charpos = it->stop_charpos;
4948 xassert (it->face_id >= 0);
4949 p->face_id = it->face_id;
4950 p->string = it->string;
4951 p->method = it->method;
4952 switch (p->method)
4953 {
4954 case GET_FROM_IMAGE:
4955 p->u.image.object = it->object;
4956 p->u.image.image_id = it->image_id;
4957 p->u.image.slice = it->slice;
4958 break;
4959 case GET_FROM_COMPOSITION:
4960 p->u.comp.object = it->object;
4961 p->u.comp.c = it->c;
4962 p->u.comp.len = it->len;
4963 p->u.comp.cmp_id = it->cmp_id;
4964 p->u.comp.cmp_len = it->cmp_len;
4965 break;
4966 case GET_FROM_STRETCH:
4967 p->u.stretch.object = it->object;
4968 break;
4969 }
4970 p->position = it->position;
4971 p->current = it->current;
4972 p->end_charpos = it->end_charpos;
4973 p->string_nchars = it->string_nchars;
4974 p->area = it->area;
4975 p->multibyte_p = it->multibyte_p;
4976 p->space_width = it->space_width;
4977 p->font_height = it->font_height;
4978 p->voffset = it->voffset;
4979 p->string_from_display_prop_p = it->string_from_display_prop_p;
4980 p->display_ellipsis_p = 0;
4981 ++it->sp;
4982 }
4983
4984
4985 /* Restore IT's settings from IT->stack. Called, for example, when no
4986 more overlay strings must be processed, and we return to delivering
4987 display elements from a buffer, or when the end of a string from a
4988 `display' property is reached and we return to delivering display
4989 elements from an overlay string, or from a buffer. */
4990
4991 static void
4992 pop_it (it)
4993 struct it *it;
4994 {
4995 struct iterator_stack_entry *p;
4996
4997 xassert (it->sp > 0);
4998 --it->sp;
4999 p = it->stack + it->sp;
5000 it->stop_charpos = p->stop_charpos;
5001 it->face_id = p->face_id;
5002 it->current = p->current;
5003 it->position = p->position;
5004 it->string = p->string;
5005 if (NILP (it->string))
5006 SET_TEXT_POS (it->current.string_pos, -1, -1);
5007 it->method = p->method;
5008 switch (it->method)
5009 {
5010 case GET_FROM_IMAGE:
5011 it->image_id = p->u.image.image_id;
5012 it->object = p->u.image.object;
5013 it->slice = p->u.image.slice;
5014 break;
5015 case GET_FROM_COMPOSITION:
5016 it->object = p->u.comp.object;
5017 it->c = p->u.comp.c;
5018 it->len = p->u.comp.len;
5019 it->cmp_id = p->u.comp.cmp_id;
5020 it->cmp_len = p->u.comp.cmp_len;
5021 break;
5022 case GET_FROM_STRETCH:
5023 it->object = p->u.comp.object;
5024 break;
5025 case GET_FROM_BUFFER:
5026 it->object = it->w->buffer;
5027 break;
5028 case GET_FROM_STRING:
5029 it->object = it->string;
5030 break;
5031 }
5032 it->end_charpos = p->end_charpos;
5033 it->string_nchars = p->string_nchars;
5034 it->area = p->area;
5035 it->multibyte_p = p->multibyte_p;
5036 it->space_width = p->space_width;
5037 it->font_height = p->font_height;
5038 it->voffset = p->voffset;
5039 it->string_from_display_prop_p = p->string_from_display_prop_p;
5040 }
5041
5042
5043 \f
5044 /***********************************************************************
5045 Moving over lines
5046 ***********************************************************************/
5047
5048 /* Set IT's current position to the previous line start. */
5049
5050 static void
5051 back_to_previous_line_start (it)
5052 struct it *it;
5053 {
5054 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5055 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5056 }
5057
5058
5059 /* Move IT to the next line start.
5060
5061 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5062 we skipped over part of the text (as opposed to moving the iterator
5063 continuously over the text). Otherwise, don't change the value
5064 of *SKIPPED_P.
5065
5066 Newlines may come from buffer text, overlay strings, or strings
5067 displayed via the `display' property. That's the reason we can't
5068 simply use find_next_newline_no_quit.
5069
5070 Note that this function may not skip over invisible text that is so
5071 because of text properties and immediately follows a newline. If
5072 it would, function reseat_at_next_visible_line_start, when called
5073 from set_iterator_to_next, would effectively make invisible
5074 characters following a newline part of the wrong glyph row, which
5075 leads to wrong cursor motion. */
5076
5077 static int
5078 forward_to_next_line_start (it, skipped_p)
5079 struct it *it;
5080 int *skipped_p;
5081 {
5082 int old_selective, newline_found_p, n;
5083 const int MAX_NEWLINE_DISTANCE = 500;
5084
5085 /* If already on a newline, just consume it to avoid unintended
5086 skipping over invisible text below. */
5087 if (it->what == IT_CHARACTER
5088 && it->c == '\n'
5089 && CHARPOS (it->position) == IT_CHARPOS (*it))
5090 {
5091 set_iterator_to_next (it, 0);
5092 it->c = 0;
5093 return 1;
5094 }
5095
5096 /* Don't handle selective display in the following. It's (a)
5097 unnecessary because it's done by the caller, and (b) leads to an
5098 infinite recursion because next_element_from_ellipsis indirectly
5099 calls this function. */
5100 old_selective = it->selective;
5101 it->selective = 0;
5102
5103 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5104 from buffer text. */
5105 for (n = newline_found_p = 0;
5106 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5107 n += STRINGP (it->string) ? 0 : 1)
5108 {
5109 if (!get_next_display_element (it))
5110 return 0;
5111 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5112 set_iterator_to_next (it, 0);
5113 }
5114
5115 /* If we didn't find a newline near enough, see if we can use a
5116 short-cut. */
5117 if (!newline_found_p)
5118 {
5119 int start = IT_CHARPOS (*it);
5120 int limit = find_next_newline_no_quit (start, 1);
5121 Lisp_Object pos;
5122
5123 xassert (!STRINGP (it->string));
5124
5125 /* If there isn't any `display' property in sight, and no
5126 overlays, we can just use the position of the newline in
5127 buffer text. */
5128 if (it->stop_charpos >= limit
5129 || ((pos = Fnext_single_property_change (make_number (start),
5130 Qdisplay,
5131 Qnil, make_number (limit)),
5132 NILP (pos))
5133 && next_overlay_change (start) == ZV))
5134 {
5135 IT_CHARPOS (*it) = limit;
5136 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5137 *skipped_p = newline_found_p = 1;
5138 }
5139 else
5140 {
5141 while (get_next_display_element (it)
5142 && !newline_found_p)
5143 {
5144 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5145 set_iterator_to_next (it, 0);
5146 }
5147 }
5148 }
5149
5150 it->selective = old_selective;
5151 return newline_found_p;
5152 }
5153
5154
5155 /* Set IT's current position to the previous visible line start. Skip
5156 invisible text that is so either due to text properties or due to
5157 selective display. Caution: this does not change IT->current_x and
5158 IT->hpos. */
5159
5160 static void
5161 back_to_previous_visible_line_start (it)
5162 struct it *it;
5163 {
5164 while (IT_CHARPOS (*it) > BEGV)
5165 {
5166 back_to_previous_line_start (it);
5167
5168 if (IT_CHARPOS (*it) <= BEGV)
5169 break;
5170
5171 /* If selective > 0, then lines indented more than that values
5172 are invisible. */
5173 if (it->selective > 0
5174 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5175 (double) it->selective)) /* iftc */
5176 continue;
5177
5178 /* Check the newline before point for invisibility. */
5179 {
5180 Lisp_Object prop;
5181 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5182 Qinvisible, it->window);
5183 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5184 continue;
5185 }
5186
5187 if (IT_CHARPOS (*it) <= BEGV)
5188 break;
5189
5190 {
5191 struct it it2;
5192 int pos;
5193 int beg, end;
5194 Lisp_Object val, overlay;
5195
5196 /* If newline is part of a composition, continue from start of composition */
5197 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5198 && beg < IT_CHARPOS (*it))
5199 goto replaced;
5200
5201 /* If newline is replaced by a display property, find start of overlay
5202 or interval and continue search from that point. */
5203 it2 = *it;
5204 pos = --IT_CHARPOS (it2);
5205 --IT_BYTEPOS (it2);
5206 it2.sp = 0;
5207 if (handle_display_prop (&it2) == HANDLED_RETURN
5208 && !NILP (val = get_char_property_and_overlay
5209 (make_number (pos), Qdisplay, Qnil, &overlay))
5210 && (OVERLAYP (overlay)
5211 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5212 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5213 goto replaced;
5214
5215 /* Newline is not replaced by anything -- so we are done. */
5216 break;
5217
5218 replaced:
5219 if (beg < BEGV)
5220 beg = BEGV;
5221 IT_CHARPOS (*it) = beg;
5222 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5223 }
5224 }
5225
5226 it->continuation_lines_width = 0;
5227
5228 xassert (IT_CHARPOS (*it) >= BEGV);
5229 xassert (IT_CHARPOS (*it) == BEGV
5230 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5231 CHECK_IT (it);
5232 }
5233
5234
5235 /* Reseat iterator IT at the previous visible line start. Skip
5236 invisible text that is so either due to text properties or due to
5237 selective display. At the end, update IT's overlay information,
5238 face information etc. */
5239
5240 void
5241 reseat_at_previous_visible_line_start (it)
5242 struct it *it;
5243 {
5244 back_to_previous_visible_line_start (it);
5245 reseat (it, it->current.pos, 1);
5246 CHECK_IT (it);
5247 }
5248
5249
5250 /* Reseat iterator IT on the next visible line start in the current
5251 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5252 preceding the line start. Skip over invisible text that is so
5253 because of selective display. Compute faces, overlays etc at the
5254 new position. Note that this function does not skip over text that
5255 is invisible because of text properties. */
5256
5257 static void
5258 reseat_at_next_visible_line_start (it, on_newline_p)
5259 struct it *it;
5260 int on_newline_p;
5261 {
5262 int newline_found_p, skipped_p = 0;
5263
5264 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5265
5266 /* Skip over lines that are invisible because they are indented
5267 more than the value of IT->selective. */
5268 if (it->selective > 0)
5269 while (IT_CHARPOS (*it) < ZV
5270 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5271 (double) it->selective)) /* iftc */
5272 {
5273 xassert (IT_BYTEPOS (*it) == BEGV
5274 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5275 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5276 }
5277
5278 /* Position on the newline if that's what's requested. */
5279 if (on_newline_p && newline_found_p)
5280 {
5281 if (STRINGP (it->string))
5282 {
5283 if (IT_STRING_CHARPOS (*it) > 0)
5284 {
5285 --IT_STRING_CHARPOS (*it);
5286 --IT_STRING_BYTEPOS (*it);
5287 }
5288 }
5289 else if (IT_CHARPOS (*it) > BEGV)
5290 {
5291 --IT_CHARPOS (*it);
5292 --IT_BYTEPOS (*it);
5293 reseat (it, it->current.pos, 0);
5294 }
5295 }
5296 else if (skipped_p)
5297 reseat (it, it->current.pos, 0);
5298
5299 CHECK_IT (it);
5300 }
5301
5302
5303 \f
5304 /***********************************************************************
5305 Changing an iterator's position
5306 ***********************************************************************/
5307
5308 /* Change IT's current position to POS in current_buffer. If FORCE_P
5309 is non-zero, always check for text properties at the new position.
5310 Otherwise, text properties are only looked up if POS >=
5311 IT->check_charpos of a property. */
5312
5313 static void
5314 reseat (it, pos, force_p)
5315 struct it *it;
5316 struct text_pos pos;
5317 int force_p;
5318 {
5319 int original_pos = IT_CHARPOS (*it);
5320
5321 reseat_1 (it, pos, 0);
5322
5323 /* Determine where to check text properties. Avoid doing it
5324 where possible because text property lookup is very expensive. */
5325 if (force_p
5326 || CHARPOS (pos) > it->stop_charpos
5327 || CHARPOS (pos) < original_pos)
5328 handle_stop (it);
5329
5330 CHECK_IT (it);
5331 }
5332
5333
5334 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5335 IT->stop_pos to POS, also. */
5336
5337 static void
5338 reseat_1 (it, pos, set_stop_p)
5339 struct it *it;
5340 struct text_pos pos;
5341 int set_stop_p;
5342 {
5343 /* Don't call this function when scanning a C string. */
5344 xassert (it->s == NULL);
5345
5346 /* POS must be a reasonable value. */
5347 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5348
5349 it->current.pos = it->position = pos;
5350 it->end_charpos = ZV;
5351 it->dpvec = NULL;
5352 it->current.dpvec_index = -1;
5353 it->current.overlay_string_index = -1;
5354 IT_STRING_CHARPOS (*it) = -1;
5355 IT_STRING_BYTEPOS (*it) = -1;
5356 it->string = Qnil;
5357 it->method = GET_FROM_BUFFER;
5358 it->object = it->w->buffer;
5359 it->area = TEXT_AREA;
5360 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5361 it->sp = 0;
5362 it->string_from_display_prop_p = 0;
5363 it->face_before_selective_p = 0;
5364
5365 if (set_stop_p)
5366 it->stop_charpos = CHARPOS (pos);
5367 }
5368
5369
5370 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5371 If S is non-null, it is a C string to iterate over. Otherwise,
5372 STRING gives a Lisp string to iterate over.
5373
5374 If PRECISION > 0, don't return more then PRECISION number of
5375 characters from the string.
5376
5377 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5378 characters have been returned. FIELD_WIDTH < 0 means an infinite
5379 field width.
5380
5381 MULTIBYTE = 0 means disable processing of multibyte characters,
5382 MULTIBYTE > 0 means enable it,
5383 MULTIBYTE < 0 means use IT->multibyte_p.
5384
5385 IT must be initialized via a prior call to init_iterator before
5386 calling this function. */
5387
5388 static void
5389 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5390 struct it *it;
5391 unsigned char *s;
5392 Lisp_Object string;
5393 int charpos;
5394 int precision, field_width, multibyte;
5395 {
5396 /* No region in strings. */
5397 it->region_beg_charpos = it->region_end_charpos = -1;
5398
5399 /* No text property checks performed by default, but see below. */
5400 it->stop_charpos = -1;
5401
5402 /* Set iterator position and end position. */
5403 bzero (&it->current, sizeof it->current);
5404 it->current.overlay_string_index = -1;
5405 it->current.dpvec_index = -1;
5406 xassert (charpos >= 0);
5407
5408 /* If STRING is specified, use its multibyteness, otherwise use the
5409 setting of MULTIBYTE, if specified. */
5410 if (multibyte >= 0)
5411 it->multibyte_p = multibyte > 0;
5412
5413 if (s == NULL)
5414 {
5415 xassert (STRINGP (string));
5416 it->string = string;
5417 it->s = NULL;
5418 it->end_charpos = it->string_nchars = SCHARS (string);
5419 it->method = GET_FROM_STRING;
5420 it->current.string_pos = string_pos (charpos, string);
5421 }
5422 else
5423 {
5424 it->s = s;
5425 it->string = Qnil;
5426
5427 /* Note that we use IT->current.pos, not it->current.string_pos,
5428 for displaying C strings. */
5429 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5430 if (it->multibyte_p)
5431 {
5432 it->current.pos = c_string_pos (charpos, s, 1);
5433 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5434 }
5435 else
5436 {
5437 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5438 it->end_charpos = it->string_nchars = strlen (s);
5439 }
5440
5441 it->method = GET_FROM_C_STRING;
5442 }
5443
5444 /* PRECISION > 0 means don't return more than PRECISION characters
5445 from the string. */
5446 if (precision > 0 && it->end_charpos - charpos > precision)
5447 it->end_charpos = it->string_nchars = charpos + precision;
5448
5449 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5450 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5451 FIELD_WIDTH < 0 means infinite field width. This is useful for
5452 padding with `-' at the end of a mode line. */
5453 if (field_width < 0)
5454 field_width = INFINITY;
5455 if (field_width > it->end_charpos - charpos)
5456 it->end_charpos = charpos + field_width;
5457
5458 /* Use the standard display table for displaying strings. */
5459 if (DISP_TABLE_P (Vstandard_display_table))
5460 it->dp = XCHAR_TABLE (Vstandard_display_table);
5461
5462 it->stop_charpos = charpos;
5463 CHECK_IT (it);
5464 }
5465
5466
5467 \f
5468 /***********************************************************************
5469 Iteration
5470 ***********************************************************************/
5471
5472 /* Map enum it_method value to corresponding next_element_from_* function. */
5473
5474 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5475 {
5476 next_element_from_buffer,
5477 next_element_from_display_vector,
5478 next_element_from_composition,
5479 next_element_from_string,
5480 next_element_from_c_string,
5481 next_element_from_image,
5482 next_element_from_stretch
5483 };
5484
5485
5486 /* Load IT's display element fields with information about the next
5487 display element from the current position of IT. Value is zero if
5488 end of buffer (or C string) is reached. */
5489
5490 static struct frame *last_escape_glyph_frame = NULL;
5491 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5492 static int last_escape_glyph_merged_face_id = 0;
5493
5494 int
5495 get_next_display_element (it)
5496 struct it *it;
5497 {
5498 /* Non-zero means that we found a display element. Zero means that
5499 we hit the end of what we iterate over. Performance note: the
5500 function pointer `method' used here turns out to be faster than
5501 using a sequence of if-statements. */
5502 int success_p;
5503
5504 get_next:
5505 success_p = (*get_next_element[it->method]) (it);
5506
5507 if (it->what == IT_CHARACTER)
5508 {
5509 /* Map via display table or translate control characters.
5510 IT->c, IT->len etc. have been set to the next character by
5511 the function call above. If we have a display table, and it
5512 contains an entry for IT->c, translate it. Don't do this if
5513 IT->c itself comes from a display table, otherwise we could
5514 end up in an infinite recursion. (An alternative could be to
5515 count the recursion depth of this function and signal an
5516 error when a certain maximum depth is reached.) Is it worth
5517 it? */
5518 if (success_p && it->dpvec == NULL)
5519 {
5520 Lisp_Object dv;
5521
5522 if (it->dp
5523 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5524 VECTORP (dv)))
5525 {
5526 struct Lisp_Vector *v = XVECTOR (dv);
5527
5528 /* Return the first character from the display table
5529 entry, if not empty. If empty, don't display the
5530 current character. */
5531 if (v->size)
5532 {
5533 it->dpvec_char_len = it->len;
5534 it->dpvec = v->contents;
5535 it->dpend = v->contents + v->size;
5536 it->current.dpvec_index = 0;
5537 it->dpvec_face_id = -1;
5538 it->saved_face_id = it->face_id;
5539 it->method = GET_FROM_DISPLAY_VECTOR;
5540 it->ellipsis_p = 0;
5541 }
5542 else
5543 {
5544 set_iterator_to_next (it, 0);
5545 }
5546 goto get_next;
5547 }
5548
5549 /* Translate control characters into `\003' or `^C' form.
5550 Control characters coming from a display table entry are
5551 currently not translated because we use IT->dpvec to hold
5552 the translation. This could easily be changed but I
5553 don't believe that it is worth doing.
5554
5555 If it->multibyte_p is nonzero, eight-bit characters and
5556 non-printable multibyte characters are also translated to
5557 octal form.
5558
5559 If it->multibyte_p is zero, eight-bit characters that
5560 don't have corresponding multibyte char code are also
5561 translated to octal form. */
5562 else if ((it->c < ' '
5563 && (it->area != TEXT_AREA
5564 /* In mode line, treat \n like other crl chars. */
5565 || (it->c != '\t'
5566 && it->glyph_row && it->glyph_row->mode_line_p)
5567 || (it->c != '\n' && it->c != '\t')))
5568 || (it->multibyte_p
5569 ? ((it->c >= 127
5570 && it->len == 1)
5571 || !CHAR_PRINTABLE_P (it->c)
5572 || (!NILP (Vnobreak_char_display)
5573 && (it->c == 0x8a0 || it->c == 0x8ad
5574 || it->c == 0x920 || it->c == 0x92d
5575 || it->c == 0xe20 || it->c == 0xe2d
5576 || it->c == 0xf20 || it->c == 0xf2d)))
5577 : (it->c >= 127
5578 && (!unibyte_display_via_language_environment
5579 || it->c == unibyte_char_to_multibyte (it->c)))))
5580 {
5581 /* IT->c is a control character which must be displayed
5582 either as '\003' or as `^C' where the '\\' and '^'
5583 can be defined in the display table. Fill
5584 IT->ctl_chars with glyphs for what we have to
5585 display. Then, set IT->dpvec to these glyphs. */
5586 GLYPH g;
5587 int ctl_len;
5588 int face_id, lface_id = 0 ;
5589 GLYPH escape_glyph;
5590
5591 /* Handle control characters with ^. */
5592
5593 if (it->c < 128 && it->ctl_arrow_p)
5594 {
5595 g = '^'; /* default glyph for Control */
5596 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5597 if (it->dp
5598 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5599 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5600 {
5601 g = XINT (DISP_CTRL_GLYPH (it->dp));
5602 lface_id = FAST_GLYPH_FACE (g);
5603 }
5604 if (lface_id)
5605 {
5606 g = FAST_GLYPH_CHAR (g);
5607 face_id = merge_faces (it->f, Qt, lface_id,
5608 it->face_id);
5609 }
5610 else if (it->f == last_escape_glyph_frame
5611 && it->face_id == last_escape_glyph_face_id)
5612 {
5613 face_id = last_escape_glyph_merged_face_id;
5614 }
5615 else
5616 {
5617 /* Merge the escape-glyph face into the current face. */
5618 face_id = merge_faces (it->f, Qescape_glyph, 0,
5619 it->face_id);
5620 last_escape_glyph_frame = it->f;
5621 last_escape_glyph_face_id = it->face_id;
5622 last_escape_glyph_merged_face_id = face_id;
5623 }
5624
5625 XSETINT (it->ctl_chars[0], g);
5626 g = it->c ^ 0100;
5627 XSETINT (it->ctl_chars[1], g);
5628 ctl_len = 2;
5629 goto display_control;
5630 }
5631
5632 /* Handle non-break space in the mode where it only gets
5633 highlighting. */
5634
5635 if (EQ (Vnobreak_char_display, Qt)
5636 && (it->c == 0x8a0 || it->c == 0x920
5637 || it->c == 0xe20 || it->c == 0xf20))
5638 {
5639 /* Merge the no-break-space face into the current face. */
5640 face_id = merge_faces (it->f, Qnobreak_space, 0,
5641 it->face_id);
5642
5643 g = it->c = ' ';
5644 XSETINT (it->ctl_chars[0], g);
5645 ctl_len = 1;
5646 goto display_control;
5647 }
5648
5649 /* Handle sequences that start with the "escape glyph". */
5650
5651 /* the default escape glyph is \. */
5652 escape_glyph = '\\';
5653
5654 if (it->dp
5655 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5656 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5657 {
5658 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5659 lface_id = FAST_GLYPH_FACE (escape_glyph);
5660 }
5661 if (lface_id)
5662 {
5663 /* The display table specified a face.
5664 Merge it into face_id and also into escape_glyph. */
5665 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5666 face_id = merge_faces (it->f, Qt, lface_id,
5667 it->face_id);
5668 }
5669 else if (it->f == last_escape_glyph_frame
5670 && it->face_id == last_escape_glyph_face_id)
5671 {
5672 face_id = last_escape_glyph_merged_face_id;
5673 }
5674 else
5675 {
5676 /* Merge the escape-glyph face into the current face. */
5677 face_id = merge_faces (it->f, Qescape_glyph, 0,
5678 it->face_id);
5679 last_escape_glyph_frame = it->f;
5680 last_escape_glyph_face_id = it->face_id;
5681 last_escape_glyph_merged_face_id = face_id;
5682 }
5683
5684 /* Handle soft hyphens in the mode where they only get
5685 highlighting. */
5686
5687 if (EQ (Vnobreak_char_display, Qt)
5688 && (it->c == 0x8ad || it->c == 0x92d
5689 || it->c == 0xe2d || it->c == 0xf2d))
5690 {
5691 g = it->c = '-';
5692 XSETINT (it->ctl_chars[0], g);
5693 ctl_len = 1;
5694 goto display_control;
5695 }
5696
5697 /* Handle non-break space and soft hyphen
5698 with the escape glyph. */
5699
5700 if (it->c == 0x8a0 || it->c == 0x8ad
5701 || it->c == 0x920 || it->c == 0x92d
5702 || it->c == 0xe20 || it->c == 0xe2d
5703 || it->c == 0xf20 || it->c == 0xf2d)
5704 {
5705 XSETINT (it->ctl_chars[0], escape_glyph);
5706 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5707 XSETINT (it->ctl_chars[1], g);
5708 ctl_len = 2;
5709 goto display_control;
5710 }
5711
5712 {
5713 unsigned char str[MAX_MULTIBYTE_LENGTH];
5714 int len;
5715 int i;
5716
5717 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5718 if (SINGLE_BYTE_CHAR_P (it->c))
5719 str[0] = it->c, len = 1;
5720 else
5721 {
5722 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5723 if (len < 0)
5724 {
5725 /* It's an invalid character, which shouldn't
5726 happen actually, but due to bugs it may
5727 happen. Let's print the char as is, there's
5728 not much meaningful we can do with it. */
5729 str[0] = it->c;
5730 str[1] = it->c >> 8;
5731 str[2] = it->c >> 16;
5732 str[3] = it->c >> 24;
5733 len = 4;
5734 }
5735 }
5736
5737 for (i = 0; i < len; i++)
5738 {
5739 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5740 /* Insert three more glyphs into IT->ctl_chars for
5741 the octal display of the character. */
5742 g = ((str[i] >> 6) & 7) + '0';
5743 XSETINT (it->ctl_chars[i * 4 + 1], g);
5744 g = ((str[i] >> 3) & 7) + '0';
5745 XSETINT (it->ctl_chars[i * 4 + 2], g);
5746 g = (str[i] & 7) + '0';
5747 XSETINT (it->ctl_chars[i * 4 + 3], g);
5748 }
5749 ctl_len = len * 4;
5750 }
5751
5752 display_control:
5753 /* Set up IT->dpvec and return first character from it. */
5754 it->dpvec_char_len = it->len;
5755 it->dpvec = it->ctl_chars;
5756 it->dpend = it->dpvec + ctl_len;
5757 it->current.dpvec_index = 0;
5758 it->dpvec_face_id = face_id;
5759 it->saved_face_id = it->face_id;
5760 it->method = GET_FROM_DISPLAY_VECTOR;
5761 it->ellipsis_p = 0;
5762 goto get_next;
5763 }
5764 }
5765
5766 /* Adjust face id for a multibyte character. There are no
5767 multibyte character in unibyte text. */
5768 if (it->multibyte_p
5769 && success_p
5770 && FRAME_WINDOW_P (it->f))
5771 {
5772 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5773 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5774 }
5775 }
5776
5777 /* Is this character the last one of a run of characters with
5778 box? If yes, set IT->end_of_box_run_p to 1. */
5779 if (it->face_box_p
5780 && it->s == NULL)
5781 {
5782 int face_id;
5783 struct face *face;
5784
5785 it->end_of_box_run_p
5786 = ((face_id = face_after_it_pos (it),
5787 face_id != it->face_id)
5788 && (face = FACE_FROM_ID (it->f, face_id),
5789 face->box == FACE_NO_BOX));
5790 }
5791
5792 /* Value is 0 if end of buffer or string reached. */
5793 return success_p;
5794 }
5795
5796
5797 /* Move IT to the next display element.
5798
5799 RESEAT_P non-zero means if called on a newline in buffer text,
5800 skip to the next visible line start.
5801
5802 Functions get_next_display_element and set_iterator_to_next are
5803 separate because I find this arrangement easier to handle than a
5804 get_next_display_element function that also increments IT's
5805 position. The way it is we can first look at an iterator's current
5806 display element, decide whether it fits on a line, and if it does,
5807 increment the iterator position. The other way around we probably
5808 would either need a flag indicating whether the iterator has to be
5809 incremented the next time, or we would have to implement a
5810 decrement position function which would not be easy to write. */
5811
5812 void
5813 set_iterator_to_next (it, reseat_p)
5814 struct it *it;
5815 int reseat_p;
5816 {
5817 /* Reset flags indicating start and end of a sequence of characters
5818 with box. Reset them at the start of this function because
5819 moving the iterator to a new position might set them. */
5820 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5821
5822 switch (it->method)
5823 {
5824 case GET_FROM_BUFFER:
5825 /* The current display element of IT is a character from
5826 current_buffer. Advance in the buffer, and maybe skip over
5827 invisible lines that are so because of selective display. */
5828 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5829 reseat_at_next_visible_line_start (it, 0);
5830 else
5831 {
5832 xassert (it->len != 0);
5833 IT_BYTEPOS (*it) += it->len;
5834 IT_CHARPOS (*it) += 1;
5835 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5836 }
5837 break;
5838
5839 case GET_FROM_COMPOSITION:
5840 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5841 xassert (it->sp > 0);
5842 pop_it (it);
5843 if (it->method == GET_FROM_STRING)
5844 {
5845 IT_STRING_BYTEPOS (*it) += it->len;
5846 IT_STRING_CHARPOS (*it) += it->cmp_len;
5847 goto consider_string_end;
5848 }
5849 else if (it->method == GET_FROM_BUFFER)
5850 {
5851 IT_BYTEPOS (*it) += it->len;
5852 IT_CHARPOS (*it) += it->cmp_len;
5853 }
5854 break;
5855
5856 case GET_FROM_C_STRING:
5857 /* Current display element of IT is from a C string. */
5858 IT_BYTEPOS (*it) += it->len;
5859 IT_CHARPOS (*it) += 1;
5860 break;
5861
5862 case GET_FROM_DISPLAY_VECTOR:
5863 /* Current display element of IT is from a display table entry.
5864 Advance in the display table definition. Reset it to null if
5865 end reached, and continue with characters from buffers/
5866 strings. */
5867 ++it->current.dpvec_index;
5868
5869 /* Restore face of the iterator to what they were before the
5870 display vector entry (these entries may contain faces). */
5871 it->face_id = it->saved_face_id;
5872
5873 if (it->dpvec + it->current.dpvec_index == it->dpend)
5874 {
5875 int recheck_faces = it->ellipsis_p;
5876
5877 if (it->s)
5878 it->method = GET_FROM_C_STRING;
5879 else if (STRINGP (it->string))
5880 it->method = GET_FROM_STRING;
5881 else
5882 {
5883 it->method = GET_FROM_BUFFER;
5884 it->object = it->w->buffer;
5885 }
5886
5887 it->dpvec = NULL;
5888 it->current.dpvec_index = -1;
5889
5890 /* Skip over characters which were displayed via IT->dpvec. */
5891 if (it->dpvec_char_len < 0)
5892 reseat_at_next_visible_line_start (it, 1);
5893 else if (it->dpvec_char_len > 0)
5894 {
5895 if (it->method == GET_FROM_STRING
5896 && it->n_overlay_strings > 0)
5897 it->ignore_overlay_strings_at_pos_p = 1;
5898 it->len = it->dpvec_char_len;
5899 set_iterator_to_next (it, reseat_p);
5900 }
5901
5902 /* Maybe recheck faces after display vector */
5903 if (recheck_faces)
5904 it->stop_charpos = IT_CHARPOS (*it);
5905 }
5906 break;
5907
5908 case GET_FROM_STRING:
5909 /* Current display element is a character from a Lisp string. */
5910 xassert (it->s == NULL && STRINGP (it->string));
5911 IT_STRING_BYTEPOS (*it) += it->len;
5912 IT_STRING_CHARPOS (*it) += 1;
5913
5914 consider_string_end:
5915
5916 if (it->current.overlay_string_index >= 0)
5917 {
5918 /* IT->string is an overlay string. Advance to the
5919 next, if there is one. */
5920 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5921 next_overlay_string (it);
5922 }
5923 else
5924 {
5925 /* IT->string is not an overlay string. If we reached
5926 its end, and there is something on IT->stack, proceed
5927 with what is on the stack. This can be either another
5928 string, this time an overlay string, or a buffer. */
5929 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5930 && it->sp > 0)
5931 {
5932 pop_it (it);
5933 if (it->method == GET_FROM_STRING)
5934 goto consider_string_end;
5935 }
5936 }
5937 break;
5938
5939 case GET_FROM_IMAGE:
5940 case GET_FROM_STRETCH:
5941 /* The position etc with which we have to proceed are on
5942 the stack. The position may be at the end of a string,
5943 if the `display' property takes up the whole string. */
5944 xassert (it->sp > 0);
5945 pop_it (it);
5946 if (it->method == GET_FROM_STRING)
5947 goto consider_string_end;
5948 break;
5949
5950 default:
5951 /* There are no other methods defined, so this should be a bug. */
5952 abort ();
5953 }
5954
5955 xassert (it->method != GET_FROM_STRING
5956 || (STRINGP (it->string)
5957 && IT_STRING_CHARPOS (*it) >= 0));
5958 }
5959
5960 /* Load IT's display element fields with information about the next
5961 display element which comes from a display table entry or from the
5962 result of translating a control character to one of the forms `^C'
5963 or `\003'.
5964
5965 IT->dpvec holds the glyphs to return as characters.
5966 IT->saved_face_id holds the face id before the display vector--
5967 it is restored into IT->face_idin set_iterator_to_next. */
5968
5969 static int
5970 next_element_from_display_vector (it)
5971 struct it *it;
5972 {
5973 /* Precondition. */
5974 xassert (it->dpvec && it->current.dpvec_index >= 0);
5975
5976 it->face_id = it->saved_face_id;
5977
5978 if (INTEGERP (*it->dpvec)
5979 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5980 {
5981 GLYPH g;
5982
5983 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5984 it->c = FAST_GLYPH_CHAR (g);
5985 it->len = CHAR_BYTES (it->c);
5986
5987 /* The entry may contain a face id to use. Such a face id is
5988 the id of a Lisp face, not a realized face. A face id of
5989 zero means no face is specified. */
5990 if (it->dpvec_face_id >= 0)
5991 it->face_id = it->dpvec_face_id;
5992 else
5993 {
5994 int lface_id = FAST_GLYPH_FACE (g);
5995 if (lface_id > 0)
5996 it->face_id = merge_faces (it->f, Qt, lface_id,
5997 it->saved_face_id);
5998 }
5999 }
6000 else
6001 /* Display table entry is invalid. Return a space. */
6002 it->c = ' ', it->len = 1;
6003
6004 /* Don't change position and object of the iterator here. They are
6005 still the values of the character that had this display table
6006 entry or was translated, and that's what we want. */
6007 it->what = IT_CHARACTER;
6008 return 1;
6009 }
6010
6011
6012 /* Load IT with the next display element from Lisp string IT->string.
6013 IT->current.string_pos is the current position within the string.
6014 If IT->current.overlay_string_index >= 0, the Lisp string is an
6015 overlay string. */
6016
6017 static int
6018 next_element_from_string (it)
6019 struct it *it;
6020 {
6021 struct text_pos position;
6022
6023 xassert (STRINGP (it->string));
6024 xassert (IT_STRING_CHARPOS (*it) >= 0);
6025 position = it->current.string_pos;
6026
6027 /* Time to check for invisible text? */
6028 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6029 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6030 {
6031 handle_stop (it);
6032
6033 /* Since a handler may have changed IT->method, we must
6034 recurse here. */
6035 return get_next_display_element (it);
6036 }
6037
6038 if (it->current.overlay_string_index >= 0)
6039 {
6040 /* Get the next character from an overlay string. In overlay
6041 strings, There is no field width or padding with spaces to
6042 do. */
6043 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6044 {
6045 it->what = IT_EOB;
6046 return 0;
6047 }
6048 else if (STRING_MULTIBYTE (it->string))
6049 {
6050 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6051 const unsigned char *s = (SDATA (it->string)
6052 + IT_STRING_BYTEPOS (*it));
6053 it->c = string_char_and_length (s, remaining, &it->len);
6054 }
6055 else
6056 {
6057 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6058 it->len = 1;
6059 }
6060 }
6061 else
6062 {
6063 /* Get the next character from a Lisp string that is not an
6064 overlay string. Such strings come from the mode line, for
6065 example. We may have to pad with spaces, or truncate the
6066 string. See also next_element_from_c_string. */
6067 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6068 {
6069 it->what = IT_EOB;
6070 return 0;
6071 }
6072 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6073 {
6074 /* Pad with spaces. */
6075 it->c = ' ', it->len = 1;
6076 CHARPOS (position) = BYTEPOS (position) = -1;
6077 }
6078 else if (STRING_MULTIBYTE (it->string))
6079 {
6080 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6081 const unsigned char *s = (SDATA (it->string)
6082 + IT_STRING_BYTEPOS (*it));
6083 it->c = string_char_and_length (s, maxlen, &it->len);
6084 }
6085 else
6086 {
6087 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6088 it->len = 1;
6089 }
6090 }
6091
6092 /* Record what we have and where it came from. */
6093 it->what = IT_CHARACTER;
6094 it->object = it->string;
6095 it->position = position;
6096 return 1;
6097 }
6098
6099
6100 /* Load IT with next display element from C string IT->s.
6101 IT->string_nchars is the maximum number of characters to return
6102 from the string. IT->end_charpos may be greater than
6103 IT->string_nchars when this function is called, in which case we
6104 may have to return padding spaces. Value is zero if end of string
6105 reached, including padding spaces. */
6106
6107 static int
6108 next_element_from_c_string (it)
6109 struct it *it;
6110 {
6111 int success_p = 1;
6112
6113 xassert (it->s);
6114 it->what = IT_CHARACTER;
6115 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6116 it->object = Qnil;
6117
6118 /* IT's position can be greater IT->string_nchars in case a field
6119 width or precision has been specified when the iterator was
6120 initialized. */
6121 if (IT_CHARPOS (*it) >= it->end_charpos)
6122 {
6123 /* End of the game. */
6124 it->what = IT_EOB;
6125 success_p = 0;
6126 }
6127 else if (IT_CHARPOS (*it) >= it->string_nchars)
6128 {
6129 /* Pad with spaces. */
6130 it->c = ' ', it->len = 1;
6131 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6132 }
6133 else if (it->multibyte_p)
6134 {
6135 /* Implementation note: The calls to strlen apparently aren't a
6136 performance problem because there is no noticeable performance
6137 difference between Emacs running in unibyte or multibyte mode. */
6138 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6139 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6140 maxlen, &it->len);
6141 }
6142 else
6143 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6144
6145 return success_p;
6146 }
6147
6148
6149 /* Set up IT to return characters from an ellipsis, if appropriate.
6150 The definition of the ellipsis glyphs may come from a display table
6151 entry. This function Fills IT with the first glyph from the
6152 ellipsis if an ellipsis is to be displayed. */
6153
6154 static int
6155 next_element_from_ellipsis (it)
6156 struct it *it;
6157 {
6158 if (it->selective_display_ellipsis_p)
6159 setup_for_ellipsis (it, it->len);
6160 else
6161 {
6162 /* The face at the current position may be different from the
6163 face we find after the invisible text. Remember what it
6164 was in IT->saved_face_id, and signal that it's there by
6165 setting face_before_selective_p. */
6166 it->saved_face_id = it->face_id;
6167 it->method = GET_FROM_BUFFER;
6168 it->object = it->w->buffer;
6169 reseat_at_next_visible_line_start (it, 1);
6170 it->face_before_selective_p = 1;
6171 }
6172
6173 return get_next_display_element (it);
6174 }
6175
6176
6177 /* Deliver an image display element. The iterator IT is already
6178 filled with image information (done in handle_display_prop). Value
6179 is always 1. */
6180
6181
6182 static int
6183 next_element_from_image (it)
6184 struct it *it;
6185 {
6186 it->what = IT_IMAGE;
6187 return 1;
6188 }
6189
6190
6191 /* Fill iterator IT with next display element from a stretch glyph
6192 property. IT->object is the value of the text property. Value is
6193 always 1. */
6194
6195 static int
6196 next_element_from_stretch (it)
6197 struct it *it;
6198 {
6199 it->what = IT_STRETCH;
6200 return 1;
6201 }
6202
6203
6204 /* Load IT with the next display element from current_buffer. Value
6205 is zero if end of buffer reached. IT->stop_charpos is the next
6206 position at which to stop and check for text properties or buffer
6207 end. */
6208
6209 static int
6210 next_element_from_buffer (it)
6211 struct it *it;
6212 {
6213 int success_p = 1;
6214
6215 /* Check this assumption, otherwise, we would never enter the
6216 if-statement, below. */
6217 xassert (IT_CHARPOS (*it) >= BEGV
6218 && IT_CHARPOS (*it) <= it->stop_charpos);
6219
6220 if (IT_CHARPOS (*it) >= it->stop_charpos)
6221 {
6222 if (IT_CHARPOS (*it) >= it->end_charpos)
6223 {
6224 int overlay_strings_follow_p;
6225
6226 /* End of the game, except when overlay strings follow that
6227 haven't been returned yet. */
6228 if (it->overlay_strings_at_end_processed_p)
6229 overlay_strings_follow_p = 0;
6230 else
6231 {
6232 it->overlay_strings_at_end_processed_p = 1;
6233 overlay_strings_follow_p = get_overlay_strings (it, 0);
6234 }
6235
6236 if (overlay_strings_follow_p)
6237 success_p = get_next_display_element (it);
6238 else
6239 {
6240 it->what = IT_EOB;
6241 it->position = it->current.pos;
6242 success_p = 0;
6243 }
6244 }
6245 else
6246 {
6247 handle_stop (it);
6248 return get_next_display_element (it);
6249 }
6250 }
6251 else
6252 {
6253 /* No face changes, overlays etc. in sight, so just return a
6254 character from current_buffer. */
6255 unsigned char *p;
6256
6257 /* Maybe run the redisplay end trigger hook. Performance note:
6258 This doesn't seem to cost measurable time. */
6259 if (it->redisplay_end_trigger_charpos
6260 && it->glyph_row
6261 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6262 run_redisplay_end_trigger_hook (it);
6263
6264 /* Get the next character, maybe multibyte. */
6265 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6266 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6267 {
6268 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6269 - IT_BYTEPOS (*it));
6270 it->c = string_char_and_length (p, maxlen, &it->len);
6271 }
6272 else
6273 it->c = *p, it->len = 1;
6274
6275 /* Record what we have and where it came from. */
6276 it->what = IT_CHARACTER;
6277 it->object = it->w->buffer;
6278 it->position = it->current.pos;
6279
6280 /* Normally we return the character found above, except when we
6281 really want to return an ellipsis for selective display. */
6282 if (it->selective)
6283 {
6284 if (it->c == '\n')
6285 {
6286 /* A value of selective > 0 means hide lines indented more
6287 than that number of columns. */
6288 if (it->selective > 0
6289 && IT_CHARPOS (*it) + 1 < ZV
6290 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6291 IT_BYTEPOS (*it) + 1,
6292 (double) it->selective)) /* iftc */
6293 {
6294 success_p = next_element_from_ellipsis (it);
6295 it->dpvec_char_len = -1;
6296 }
6297 }
6298 else if (it->c == '\r' && it->selective == -1)
6299 {
6300 /* A value of selective == -1 means that everything from the
6301 CR to the end of the line is invisible, with maybe an
6302 ellipsis displayed for it. */
6303 success_p = next_element_from_ellipsis (it);
6304 it->dpvec_char_len = -1;
6305 }
6306 }
6307 }
6308
6309 /* Value is zero if end of buffer reached. */
6310 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6311 return success_p;
6312 }
6313
6314
6315 /* Run the redisplay end trigger hook for IT. */
6316
6317 static void
6318 run_redisplay_end_trigger_hook (it)
6319 struct it *it;
6320 {
6321 Lisp_Object args[3];
6322
6323 /* IT->glyph_row should be non-null, i.e. we should be actually
6324 displaying something, or otherwise we should not run the hook. */
6325 xassert (it->glyph_row);
6326
6327 /* Set up hook arguments. */
6328 args[0] = Qredisplay_end_trigger_functions;
6329 args[1] = it->window;
6330 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6331 it->redisplay_end_trigger_charpos = 0;
6332
6333 /* Since we are *trying* to run these functions, don't try to run
6334 them again, even if they get an error. */
6335 it->w->redisplay_end_trigger = Qnil;
6336 Frun_hook_with_args (3, args);
6337
6338 /* Notice if it changed the face of the character we are on. */
6339 handle_face_prop (it);
6340 }
6341
6342
6343 /* Deliver a composition display element. The iterator IT is already
6344 filled with composition information (done in
6345 handle_composition_prop). Value is always 1. */
6346
6347 static int
6348 next_element_from_composition (it)
6349 struct it *it;
6350 {
6351 it->what = IT_COMPOSITION;
6352 it->position = (STRINGP (it->string)
6353 ? it->current.string_pos
6354 : it->current.pos);
6355 if (STRINGP (it->string))
6356 it->object = it->string;
6357 else
6358 it->object = it->w->buffer;
6359 return 1;
6360 }
6361
6362
6363 \f
6364 /***********************************************************************
6365 Moving an iterator without producing glyphs
6366 ***********************************************************************/
6367
6368 /* Check if iterator is at a position corresponding to a valid buffer
6369 position after some move_it_ call. */
6370
6371 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6372 ((it)->method == GET_FROM_STRING \
6373 ? IT_STRING_CHARPOS (*it) == 0 \
6374 : 1)
6375
6376
6377 /* Move iterator IT to a specified buffer or X position within one
6378 line on the display without producing glyphs.
6379
6380 OP should be a bit mask including some or all of these bits:
6381 MOVE_TO_X: Stop on reaching x-position TO_X.
6382 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6383 Regardless of OP's value, stop in reaching the end of the display line.
6384
6385 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6386 This means, in particular, that TO_X includes window's horizontal
6387 scroll amount.
6388
6389 The return value has several possible values that
6390 say what condition caused the scan to stop:
6391
6392 MOVE_POS_MATCH_OR_ZV
6393 - when TO_POS or ZV was reached.
6394
6395 MOVE_X_REACHED
6396 -when TO_X was reached before TO_POS or ZV were reached.
6397
6398 MOVE_LINE_CONTINUED
6399 - when we reached the end of the display area and the line must
6400 be continued.
6401
6402 MOVE_LINE_TRUNCATED
6403 - when we reached the end of the display area and the line is
6404 truncated.
6405
6406 MOVE_NEWLINE_OR_CR
6407 - when we stopped at a line end, i.e. a newline or a CR and selective
6408 display is on. */
6409
6410 static enum move_it_result
6411 move_it_in_display_line_to (it, to_charpos, to_x, op)
6412 struct it *it;
6413 int to_charpos, to_x, op;
6414 {
6415 enum move_it_result result = MOVE_UNDEFINED;
6416 struct glyph_row *saved_glyph_row;
6417
6418 /* Don't produce glyphs in produce_glyphs. */
6419 saved_glyph_row = it->glyph_row;
6420 it->glyph_row = NULL;
6421
6422 #define BUFFER_POS_REACHED_P() \
6423 ((op & MOVE_TO_POS) != 0 \
6424 && BUFFERP (it->object) \
6425 && IT_CHARPOS (*it) >= to_charpos \
6426 && (it->method == GET_FROM_BUFFER \
6427 || (it->method == GET_FROM_DISPLAY_VECTOR \
6428 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6429
6430
6431 while (1)
6432 {
6433 int x, i, ascent = 0, descent = 0;
6434
6435 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6436 if ((op & MOVE_TO_POS) != 0
6437 && BUFFERP (it->object)
6438 && it->method == GET_FROM_BUFFER
6439 && IT_CHARPOS (*it) > to_charpos)
6440 {
6441 result = MOVE_POS_MATCH_OR_ZV;
6442 break;
6443 }
6444
6445 /* Stop when ZV reached.
6446 We used to stop here when TO_CHARPOS reached as well, but that is
6447 too soon if this glyph does not fit on this line. So we handle it
6448 explicitly below. */
6449 if (!get_next_display_element (it)
6450 || (it->truncate_lines_p
6451 && BUFFER_POS_REACHED_P ()))
6452 {
6453 result = MOVE_POS_MATCH_OR_ZV;
6454 break;
6455 }
6456
6457 /* The call to produce_glyphs will get the metrics of the
6458 display element IT is loaded with. We record in x the
6459 x-position before this display element in case it does not
6460 fit on the line. */
6461 x = it->current_x;
6462
6463 /* Remember the line height so far in case the next element doesn't
6464 fit on the line. */
6465 if (!it->truncate_lines_p)
6466 {
6467 ascent = it->max_ascent;
6468 descent = it->max_descent;
6469 }
6470
6471 PRODUCE_GLYPHS (it);
6472
6473 if (it->area != TEXT_AREA)
6474 {
6475 set_iterator_to_next (it, 1);
6476 continue;
6477 }
6478
6479 /* The number of glyphs we get back in IT->nglyphs will normally
6480 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6481 character on a terminal frame, or (iii) a line end. For the
6482 second case, IT->nglyphs - 1 padding glyphs will be present
6483 (on X frames, there is only one glyph produced for a
6484 composite character.
6485
6486 The behavior implemented below means, for continuation lines,
6487 that as many spaces of a TAB as fit on the current line are
6488 displayed there. For terminal frames, as many glyphs of a
6489 multi-glyph character are displayed in the current line, too.
6490 This is what the old redisplay code did, and we keep it that
6491 way. Under X, the whole shape of a complex character must
6492 fit on the line or it will be completely displayed in the
6493 next line.
6494
6495 Note that both for tabs and padding glyphs, all glyphs have
6496 the same width. */
6497 if (it->nglyphs)
6498 {
6499 /* More than one glyph or glyph doesn't fit on line. All
6500 glyphs have the same width. */
6501 int single_glyph_width = it->pixel_width / it->nglyphs;
6502 int new_x;
6503 int x_before_this_char = x;
6504 int hpos_before_this_char = it->hpos;
6505
6506 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6507 {
6508 new_x = x + single_glyph_width;
6509
6510 /* We want to leave anything reaching TO_X to the caller. */
6511 if ((op & MOVE_TO_X) && new_x > to_x)
6512 {
6513 if (BUFFER_POS_REACHED_P ())
6514 goto buffer_pos_reached;
6515 it->current_x = x;
6516 result = MOVE_X_REACHED;
6517 break;
6518 }
6519 else if (/* Lines are continued. */
6520 !it->truncate_lines_p
6521 && (/* And glyph doesn't fit on the line. */
6522 new_x > it->last_visible_x
6523 /* Or it fits exactly and we're on a window
6524 system frame. */
6525 || (new_x == it->last_visible_x
6526 && FRAME_WINDOW_P (it->f))))
6527 {
6528 if (/* IT->hpos == 0 means the very first glyph
6529 doesn't fit on the line, e.g. a wide image. */
6530 it->hpos == 0
6531 || (new_x == it->last_visible_x
6532 && FRAME_WINDOW_P (it->f)))
6533 {
6534 ++it->hpos;
6535 it->current_x = new_x;
6536
6537 /* The character's last glyph just barely fits
6538 in this row. */
6539 if (i == it->nglyphs - 1)
6540 {
6541 /* If this is the destination position,
6542 return a position *before* it in this row,
6543 now that we know it fits in this row. */
6544 if (BUFFER_POS_REACHED_P ())
6545 {
6546 it->hpos = hpos_before_this_char;
6547 it->current_x = x_before_this_char;
6548 result = MOVE_POS_MATCH_OR_ZV;
6549 break;
6550 }
6551
6552 set_iterator_to_next (it, 1);
6553 #ifdef HAVE_WINDOW_SYSTEM
6554 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6555 {
6556 if (!get_next_display_element (it))
6557 {
6558 result = MOVE_POS_MATCH_OR_ZV;
6559 break;
6560 }
6561 if (BUFFER_POS_REACHED_P ())
6562 {
6563 if (ITERATOR_AT_END_OF_LINE_P (it))
6564 result = MOVE_POS_MATCH_OR_ZV;
6565 else
6566 result = MOVE_LINE_CONTINUED;
6567 break;
6568 }
6569 if (ITERATOR_AT_END_OF_LINE_P (it))
6570 {
6571 result = MOVE_NEWLINE_OR_CR;
6572 break;
6573 }
6574 }
6575 #endif /* HAVE_WINDOW_SYSTEM */
6576 }
6577 }
6578 else
6579 {
6580 it->current_x = x;
6581 it->max_ascent = ascent;
6582 it->max_descent = descent;
6583 }
6584
6585 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6586 IT_CHARPOS (*it)));
6587 result = MOVE_LINE_CONTINUED;
6588 break;
6589 }
6590 else if (BUFFER_POS_REACHED_P ())
6591 goto buffer_pos_reached;
6592 else if (new_x > it->first_visible_x)
6593 {
6594 /* Glyph is visible. Increment number of glyphs that
6595 would be displayed. */
6596 ++it->hpos;
6597 }
6598 else
6599 {
6600 /* Glyph is completely off the left margin of the display
6601 area. Nothing to do. */
6602 }
6603 }
6604
6605 if (result != MOVE_UNDEFINED)
6606 break;
6607 }
6608 else if (BUFFER_POS_REACHED_P ())
6609 {
6610 buffer_pos_reached:
6611 it->current_x = x;
6612 it->max_ascent = ascent;
6613 it->max_descent = descent;
6614 result = MOVE_POS_MATCH_OR_ZV;
6615 break;
6616 }
6617 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6618 {
6619 /* Stop when TO_X specified and reached. This check is
6620 necessary here because of lines consisting of a line end,
6621 only. The line end will not produce any glyphs and we
6622 would never get MOVE_X_REACHED. */
6623 xassert (it->nglyphs == 0);
6624 result = MOVE_X_REACHED;
6625 break;
6626 }
6627
6628 /* Is this a line end? If yes, we're done. */
6629 if (ITERATOR_AT_END_OF_LINE_P (it))
6630 {
6631 result = MOVE_NEWLINE_OR_CR;
6632 break;
6633 }
6634
6635 /* The current display element has been consumed. Advance
6636 to the next. */
6637 set_iterator_to_next (it, 1);
6638
6639 /* Stop if lines are truncated and IT's current x-position is
6640 past the right edge of the window now. */
6641 if (it->truncate_lines_p
6642 && it->current_x >= it->last_visible_x)
6643 {
6644 #ifdef HAVE_WINDOW_SYSTEM
6645 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6646 {
6647 if (!get_next_display_element (it)
6648 || BUFFER_POS_REACHED_P ())
6649 {
6650 result = MOVE_POS_MATCH_OR_ZV;
6651 break;
6652 }
6653 if (ITERATOR_AT_END_OF_LINE_P (it))
6654 {
6655 result = MOVE_NEWLINE_OR_CR;
6656 break;
6657 }
6658 }
6659 #endif /* HAVE_WINDOW_SYSTEM */
6660 result = MOVE_LINE_TRUNCATED;
6661 break;
6662 }
6663 }
6664
6665 #undef BUFFER_POS_REACHED_P
6666
6667 /* Restore the iterator settings altered at the beginning of this
6668 function. */
6669 it->glyph_row = saved_glyph_row;
6670 return result;
6671 }
6672
6673
6674 /* Move IT forward until it satisfies one or more of the criteria in
6675 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6676
6677 OP is a bit-mask that specifies where to stop, and in particular,
6678 which of those four position arguments makes a difference. See the
6679 description of enum move_operation_enum.
6680
6681 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6682 screen line, this function will set IT to the next position >
6683 TO_CHARPOS. */
6684
6685 void
6686 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6687 struct it *it;
6688 int to_charpos, to_x, to_y, to_vpos;
6689 int op;
6690 {
6691 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6692 int line_height;
6693 int reached = 0;
6694
6695 for (;;)
6696 {
6697 if (op & MOVE_TO_VPOS)
6698 {
6699 /* If no TO_CHARPOS and no TO_X specified, stop at the
6700 start of the line TO_VPOS. */
6701 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6702 {
6703 if (it->vpos == to_vpos)
6704 {
6705 reached = 1;
6706 break;
6707 }
6708 else
6709 skip = move_it_in_display_line_to (it, -1, -1, 0);
6710 }
6711 else
6712 {
6713 /* TO_VPOS >= 0 means stop at TO_X in the line at
6714 TO_VPOS, or at TO_POS, whichever comes first. */
6715 if (it->vpos == to_vpos)
6716 {
6717 reached = 2;
6718 break;
6719 }
6720
6721 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6722
6723 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6724 {
6725 reached = 3;
6726 break;
6727 }
6728 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6729 {
6730 /* We have reached TO_X but not in the line we want. */
6731 skip = move_it_in_display_line_to (it, to_charpos,
6732 -1, MOVE_TO_POS);
6733 if (skip == MOVE_POS_MATCH_OR_ZV)
6734 {
6735 reached = 4;
6736 break;
6737 }
6738 }
6739 }
6740 }
6741 else if (op & MOVE_TO_Y)
6742 {
6743 struct it it_backup;
6744
6745 /* TO_Y specified means stop at TO_X in the line containing
6746 TO_Y---or at TO_CHARPOS if this is reached first. The
6747 problem is that we can't really tell whether the line
6748 contains TO_Y before we have completely scanned it, and
6749 this may skip past TO_X. What we do is to first scan to
6750 TO_X.
6751
6752 If TO_X is not specified, use a TO_X of zero. The reason
6753 is to make the outcome of this function more predictable.
6754 If we didn't use TO_X == 0, we would stop at the end of
6755 the line which is probably not what a caller would expect
6756 to happen. */
6757 skip = move_it_in_display_line_to (it, to_charpos,
6758 ((op & MOVE_TO_X)
6759 ? to_x : 0),
6760 (MOVE_TO_X
6761 | (op & MOVE_TO_POS)));
6762
6763 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6764 if (skip == MOVE_POS_MATCH_OR_ZV)
6765 {
6766 reached = 5;
6767 break;
6768 }
6769
6770 /* If TO_X was reached, we would like to know whether TO_Y
6771 is in the line. This can only be said if we know the
6772 total line height which requires us to scan the rest of
6773 the line. */
6774 if (skip == MOVE_X_REACHED)
6775 {
6776 it_backup = *it;
6777 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6778 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6779 op & MOVE_TO_POS);
6780 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6781 }
6782
6783 /* Now, decide whether TO_Y is in this line. */
6784 line_height = it->max_ascent + it->max_descent;
6785 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6786
6787 if (to_y >= it->current_y
6788 && to_y < it->current_y + line_height)
6789 {
6790 if (skip == MOVE_X_REACHED)
6791 /* If TO_Y is in this line and TO_X was reached above,
6792 we scanned too far. We have to restore IT's settings
6793 to the ones before skipping. */
6794 *it = it_backup;
6795 reached = 6;
6796 }
6797 else if (skip == MOVE_X_REACHED)
6798 {
6799 skip = skip2;
6800 if (skip == MOVE_POS_MATCH_OR_ZV)
6801 reached = 7;
6802 }
6803
6804 if (reached)
6805 break;
6806 }
6807 else if (BUFFERP (it->object)
6808 && it->method == GET_FROM_BUFFER
6809 && IT_CHARPOS (*it) >= to_charpos)
6810 skip = MOVE_POS_MATCH_OR_ZV;
6811 else
6812 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6813
6814 switch (skip)
6815 {
6816 case MOVE_POS_MATCH_OR_ZV:
6817 reached = 8;
6818 goto out;
6819
6820 case MOVE_NEWLINE_OR_CR:
6821 set_iterator_to_next (it, 1);
6822 it->continuation_lines_width = 0;
6823 break;
6824
6825 case MOVE_LINE_TRUNCATED:
6826 it->continuation_lines_width = 0;
6827 reseat_at_next_visible_line_start (it, 0);
6828 if ((op & MOVE_TO_POS) != 0
6829 && IT_CHARPOS (*it) > to_charpos)
6830 {
6831 reached = 9;
6832 goto out;
6833 }
6834 break;
6835
6836 case MOVE_LINE_CONTINUED:
6837 /* For continued lines ending in a tab, some of the glyphs
6838 associated with the tab are displayed on the current
6839 line. Since it->current_x does not include these glyphs,
6840 we use it->last_visible_x instead. */
6841 it->continuation_lines_width +=
6842 (it->c == '\t') ? it->last_visible_x : it->current_x;
6843 break;
6844
6845 default:
6846 abort ();
6847 }
6848
6849 /* Reset/increment for the next run. */
6850 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6851 it->current_x = it->hpos = 0;
6852 it->current_y += it->max_ascent + it->max_descent;
6853 ++it->vpos;
6854 last_height = it->max_ascent + it->max_descent;
6855 last_max_ascent = it->max_ascent;
6856 it->max_ascent = it->max_descent = 0;
6857 }
6858
6859 out:
6860
6861 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6862 }
6863
6864
6865 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6866
6867 If DY > 0, move IT backward at least that many pixels. DY = 0
6868 means move IT backward to the preceding line start or BEGV. This
6869 function may move over more than DY pixels if IT->current_y - DY
6870 ends up in the middle of a line; in this case IT->current_y will be
6871 set to the top of the line moved to. */
6872
6873 void
6874 move_it_vertically_backward (it, dy)
6875 struct it *it;
6876 int dy;
6877 {
6878 int nlines, h;
6879 struct it it2, it3;
6880 int start_pos;
6881
6882 move_further_back:
6883 xassert (dy >= 0);
6884
6885 start_pos = IT_CHARPOS (*it);
6886
6887 /* Estimate how many newlines we must move back. */
6888 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6889
6890 /* Set the iterator's position that many lines back. */
6891 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6892 back_to_previous_visible_line_start (it);
6893
6894 /* Reseat the iterator here. When moving backward, we don't want
6895 reseat to skip forward over invisible text, set up the iterator
6896 to deliver from overlay strings at the new position etc. So,
6897 use reseat_1 here. */
6898 reseat_1 (it, it->current.pos, 1);
6899
6900 /* We are now surely at a line start. */
6901 it->current_x = it->hpos = 0;
6902 it->continuation_lines_width = 0;
6903
6904 /* Move forward and see what y-distance we moved. First move to the
6905 start of the next line so that we get its height. We need this
6906 height to be able to tell whether we reached the specified
6907 y-distance. */
6908 it2 = *it;
6909 it2.max_ascent = it2.max_descent = 0;
6910 do
6911 {
6912 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6913 MOVE_TO_POS | MOVE_TO_VPOS);
6914 }
6915 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6916 xassert (IT_CHARPOS (*it) >= BEGV);
6917 it3 = it2;
6918
6919 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6920 xassert (IT_CHARPOS (*it) >= BEGV);
6921 /* H is the actual vertical distance from the position in *IT
6922 and the starting position. */
6923 h = it2.current_y - it->current_y;
6924 /* NLINES is the distance in number of lines. */
6925 nlines = it2.vpos - it->vpos;
6926
6927 /* Correct IT's y and vpos position
6928 so that they are relative to the starting point. */
6929 it->vpos -= nlines;
6930 it->current_y -= h;
6931
6932 if (dy == 0)
6933 {
6934 /* DY == 0 means move to the start of the screen line. The
6935 value of nlines is > 0 if continuation lines were involved. */
6936 if (nlines > 0)
6937 move_it_by_lines (it, nlines, 1);
6938 #if 0
6939 /* I think this assert is bogus if buffer contains
6940 invisible text or images. KFS. */
6941 xassert (IT_CHARPOS (*it) <= start_pos);
6942 #endif
6943 }
6944 else
6945 {
6946 /* The y-position we try to reach, relative to *IT.
6947 Note that H has been subtracted in front of the if-statement. */
6948 int target_y = it->current_y + h - dy;
6949 int y0 = it3.current_y;
6950 int y1 = line_bottom_y (&it3);
6951 int line_height = y1 - y0;
6952
6953 /* If we did not reach target_y, try to move further backward if
6954 we can. If we moved too far backward, try to move forward. */
6955 if (target_y < it->current_y
6956 /* This is heuristic. In a window that's 3 lines high, with
6957 a line height of 13 pixels each, recentering with point
6958 on the bottom line will try to move -39/2 = 19 pixels
6959 backward. Try to avoid moving into the first line. */
6960 && (it->current_y - target_y
6961 > min (window_box_height (it->w), line_height * 2 / 3))
6962 && IT_CHARPOS (*it) > BEGV)
6963 {
6964 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6965 target_y - it->current_y));
6966 dy = it->current_y - target_y;
6967 goto move_further_back;
6968 }
6969 else if (target_y >= it->current_y + line_height
6970 && IT_CHARPOS (*it) < ZV)
6971 {
6972 /* Should move forward by at least one line, maybe more.
6973
6974 Note: Calling move_it_by_lines can be expensive on
6975 terminal frames, where compute_motion is used (via
6976 vmotion) to do the job, when there are very long lines
6977 and truncate-lines is nil. That's the reason for
6978 treating terminal frames specially here. */
6979
6980 if (!FRAME_WINDOW_P (it->f))
6981 move_it_vertically (it, target_y - (it->current_y + line_height));
6982 else
6983 {
6984 do
6985 {
6986 move_it_by_lines (it, 1, 1);
6987 }
6988 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6989 }
6990
6991 #if 0
6992 /* I think this assert is bogus if buffer contains
6993 invisible text or images. KFS. */
6994 xassert (IT_CHARPOS (*it) >= BEGV);
6995 #endif
6996 }
6997 }
6998 }
6999
7000
7001 /* Move IT by a specified amount of pixel lines DY. DY negative means
7002 move backwards. DY = 0 means move to start of screen line. At the
7003 end, IT will be on the start of a screen line. */
7004
7005 void
7006 move_it_vertically (it, dy)
7007 struct it *it;
7008 int dy;
7009 {
7010 if (dy <= 0)
7011 move_it_vertically_backward (it, -dy);
7012 else
7013 {
7014 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7015 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7016 MOVE_TO_POS | MOVE_TO_Y);
7017 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7018
7019 /* If buffer ends in ZV without a newline, move to the start of
7020 the line to satisfy the post-condition. */
7021 if (IT_CHARPOS (*it) == ZV
7022 && ZV > BEGV
7023 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7024 move_it_by_lines (it, 0, 0);
7025 }
7026 }
7027
7028
7029 /* Move iterator IT past the end of the text line it is in. */
7030
7031 void
7032 move_it_past_eol (it)
7033 struct it *it;
7034 {
7035 enum move_it_result rc;
7036
7037 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7038 if (rc == MOVE_NEWLINE_OR_CR)
7039 set_iterator_to_next (it, 0);
7040 }
7041
7042
7043 #if 0 /* Currently not used. */
7044
7045 /* Return non-zero if some text between buffer positions START_CHARPOS
7046 and END_CHARPOS is invisible. IT->window is the window for text
7047 property lookup. */
7048
7049 static int
7050 invisible_text_between_p (it, start_charpos, end_charpos)
7051 struct it *it;
7052 int start_charpos, end_charpos;
7053 {
7054 Lisp_Object prop, limit;
7055 int invisible_found_p;
7056
7057 xassert (it != NULL && start_charpos <= end_charpos);
7058
7059 /* Is text at START invisible? */
7060 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7061 it->window);
7062 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7063 invisible_found_p = 1;
7064 else
7065 {
7066 limit = Fnext_single_char_property_change (make_number (start_charpos),
7067 Qinvisible, Qnil,
7068 make_number (end_charpos));
7069 invisible_found_p = XFASTINT (limit) < end_charpos;
7070 }
7071
7072 return invisible_found_p;
7073 }
7074
7075 #endif /* 0 */
7076
7077
7078 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7079 negative means move up. DVPOS == 0 means move to the start of the
7080 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7081 NEED_Y_P is zero, IT->current_y will be left unchanged.
7082
7083 Further optimization ideas: If we would know that IT->f doesn't use
7084 a face with proportional font, we could be faster for
7085 truncate-lines nil. */
7086
7087 void
7088 move_it_by_lines (it, dvpos, need_y_p)
7089 struct it *it;
7090 int dvpos, need_y_p;
7091 {
7092 struct position pos;
7093
7094 /* The commented-out optimization uses vmotion on terminals. This
7095 gives bad results, because elements like it->what, on which
7096 callers such as pos_visible_p rely, aren't updated. */
7097 /* if (!FRAME_WINDOW_P (it->f))
7098 {
7099 struct text_pos textpos;
7100
7101 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7102 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7103 reseat (it, textpos, 1);
7104 it->vpos += pos.vpos;
7105 it->current_y += pos.vpos;
7106 }
7107 else */
7108
7109 if (dvpos == 0)
7110 {
7111 /* DVPOS == 0 means move to the start of the screen line. */
7112 move_it_vertically_backward (it, 0);
7113 xassert (it->current_x == 0 && it->hpos == 0);
7114 /* Let next call to line_bottom_y calculate real line height */
7115 last_height = 0;
7116 }
7117 else if (dvpos > 0)
7118 {
7119 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7120 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7121 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7122 }
7123 else
7124 {
7125 struct it it2;
7126 int start_charpos, i;
7127
7128 /* Start at the beginning of the screen line containing IT's
7129 position. This may actually move vertically backwards,
7130 in case of overlays, so adjust dvpos accordingly. */
7131 dvpos += it->vpos;
7132 move_it_vertically_backward (it, 0);
7133 dvpos -= it->vpos;
7134
7135 /* Go back -DVPOS visible lines and reseat the iterator there. */
7136 start_charpos = IT_CHARPOS (*it);
7137 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7138 back_to_previous_visible_line_start (it);
7139 reseat (it, it->current.pos, 1);
7140
7141 /* Move further back if we end up in a string or an image. */
7142 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7143 {
7144 /* First try to move to start of display line. */
7145 dvpos += it->vpos;
7146 move_it_vertically_backward (it, 0);
7147 dvpos -= it->vpos;
7148 if (IT_POS_VALID_AFTER_MOVE_P (it))
7149 break;
7150 /* If start of line is still in string or image,
7151 move further back. */
7152 back_to_previous_visible_line_start (it);
7153 reseat (it, it->current.pos, 1);
7154 dvpos--;
7155 }
7156
7157 it->current_x = it->hpos = 0;
7158
7159 /* Above call may have moved too far if continuation lines
7160 are involved. Scan forward and see if it did. */
7161 it2 = *it;
7162 it2.vpos = it2.current_y = 0;
7163 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7164 it->vpos -= it2.vpos;
7165 it->current_y -= it2.current_y;
7166 it->current_x = it->hpos = 0;
7167
7168 /* If we moved too far back, move IT some lines forward. */
7169 if (it2.vpos > -dvpos)
7170 {
7171 int delta = it2.vpos + dvpos;
7172 it2 = *it;
7173 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7174 /* Move back again if we got too far ahead. */
7175 if (IT_CHARPOS (*it) >= start_charpos)
7176 *it = it2;
7177 }
7178 }
7179 }
7180
7181 /* Return 1 if IT points into the middle of a display vector. */
7182
7183 int
7184 in_display_vector_p (it)
7185 struct it *it;
7186 {
7187 return (it->method == GET_FROM_DISPLAY_VECTOR
7188 && it->current.dpvec_index > 0
7189 && it->dpvec + it->current.dpvec_index != it->dpend);
7190 }
7191
7192 \f
7193 /***********************************************************************
7194 Messages
7195 ***********************************************************************/
7196
7197
7198 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7199 to *Messages*. */
7200
7201 void
7202 add_to_log (format, arg1, arg2)
7203 char *format;
7204 Lisp_Object arg1, arg2;
7205 {
7206 Lisp_Object args[3];
7207 Lisp_Object msg, fmt;
7208 char *buffer;
7209 int len;
7210 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7211 USE_SAFE_ALLOCA;
7212
7213 /* Do nothing if called asynchronously. Inserting text into
7214 a buffer may call after-change-functions and alike and
7215 that would means running Lisp asynchronously. */
7216 if (handling_signal)
7217 return;
7218
7219 fmt = msg = Qnil;
7220 GCPRO4 (fmt, msg, arg1, arg2);
7221
7222 args[0] = fmt = build_string (format);
7223 args[1] = arg1;
7224 args[2] = arg2;
7225 msg = Fformat (3, args);
7226
7227 len = SBYTES (msg) + 1;
7228 SAFE_ALLOCA (buffer, char *, len);
7229 bcopy (SDATA (msg), buffer, len);
7230
7231 message_dolog (buffer, len - 1, 1, 0);
7232 SAFE_FREE ();
7233
7234 UNGCPRO;
7235 }
7236
7237
7238 /* Output a newline in the *Messages* buffer if "needs" one. */
7239
7240 void
7241 message_log_maybe_newline ()
7242 {
7243 if (message_log_need_newline)
7244 message_dolog ("", 0, 1, 0);
7245 }
7246
7247
7248 /* Add a string M of length NBYTES to the message log, optionally
7249 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7250 nonzero, means interpret the contents of M as multibyte. This
7251 function calls low-level routines in order to bypass text property
7252 hooks, etc. which might not be safe to run.
7253
7254 This may GC (insert may run before/after change hooks),
7255 so the buffer M must NOT point to a Lisp string. */
7256
7257 void
7258 message_dolog (m, nbytes, nlflag, multibyte)
7259 const char *m;
7260 int nbytes, nlflag, multibyte;
7261 {
7262 if (!NILP (Vmemory_full))
7263 return;
7264
7265 if (!NILP (Vmessage_log_max))
7266 {
7267 struct buffer *oldbuf;
7268 Lisp_Object oldpoint, oldbegv, oldzv;
7269 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7270 int point_at_end = 0;
7271 int zv_at_end = 0;
7272 Lisp_Object old_deactivate_mark, tem;
7273 struct gcpro gcpro1;
7274
7275 old_deactivate_mark = Vdeactivate_mark;
7276 oldbuf = current_buffer;
7277 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7278 current_buffer->undo_list = Qt;
7279
7280 oldpoint = message_dolog_marker1;
7281 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7282 oldbegv = message_dolog_marker2;
7283 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7284 oldzv = message_dolog_marker3;
7285 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7286 GCPRO1 (old_deactivate_mark);
7287
7288 if (PT == Z)
7289 point_at_end = 1;
7290 if (ZV == Z)
7291 zv_at_end = 1;
7292
7293 BEGV = BEG;
7294 BEGV_BYTE = BEG_BYTE;
7295 ZV = Z;
7296 ZV_BYTE = Z_BYTE;
7297 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7298
7299 /* Insert the string--maybe converting multibyte to single byte
7300 or vice versa, so that all the text fits the buffer. */
7301 if (multibyte
7302 && NILP (current_buffer->enable_multibyte_characters))
7303 {
7304 int i, c, char_bytes;
7305 unsigned char work[1];
7306
7307 /* Convert a multibyte string to single-byte
7308 for the *Message* buffer. */
7309 for (i = 0; i < nbytes; i += char_bytes)
7310 {
7311 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7312 work[0] = (SINGLE_BYTE_CHAR_P (c)
7313 ? c
7314 : multibyte_char_to_unibyte (c, Qnil));
7315 insert_1_both (work, 1, 1, 1, 0, 0);
7316 }
7317 }
7318 else if (! multibyte
7319 && ! NILP (current_buffer->enable_multibyte_characters))
7320 {
7321 int i, c, char_bytes;
7322 unsigned char *msg = (unsigned char *) m;
7323 unsigned char str[MAX_MULTIBYTE_LENGTH];
7324 /* Convert a single-byte string to multibyte
7325 for the *Message* buffer. */
7326 for (i = 0; i < nbytes; i++)
7327 {
7328 c = unibyte_char_to_multibyte (msg[i]);
7329 char_bytes = CHAR_STRING (c, str);
7330 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7331 }
7332 }
7333 else if (nbytes)
7334 insert_1 (m, nbytes, 1, 0, 0);
7335
7336 if (nlflag)
7337 {
7338 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7339 insert_1 ("\n", 1, 1, 0, 0);
7340
7341 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7342 this_bol = PT;
7343 this_bol_byte = PT_BYTE;
7344
7345 /* See if this line duplicates the previous one.
7346 If so, combine duplicates. */
7347 if (this_bol > BEG)
7348 {
7349 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7350 prev_bol = PT;
7351 prev_bol_byte = PT_BYTE;
7352
7353 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7354 this_bol, this_bol_byte);
7355 if (dup)
7356 {
7357 del_range_both (prev_bol, prev_bol_byte,
7358 this_bol, this_bol_byte, 0);
7359 if (dup > 1)
7360 {
7361 char dupstr[40];
7362 int duplen;
7363
7364 /* If you change this format, don't forget to also
7365 change message_log_check_duplicate. */
7366 sprintf (dupstr, " [%d times]", dup);
7367 duplen = strlen (dupstr);
7368 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7369 insert_1 (dupstr, duplen, 1, 0, 1);
7370 }
7371 }
7372 }
7373
7374 /* If we have more than the desired maximum number of lines
7375 in the *Messages* buffer now, delete the oldest ones.
7376 This is safe because we don't have undo in this buffer. */
7377
7378 if (NATNUMP (Vmessage_log_max))
7379 {
7380 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7381 -XFASTINT (Vmessage_log_max) - 1, 0);
7382 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7383 }
7384 }
7385 BEGV = XMARKER (oldbegv)->charpos;
7386 BEGV_BYTE = marker_byte_position (oldbegv);
7387
7388 if (zv_at_end)
7389 {
7390 ZV = Z;
7391 ZV_BYTE = Z_BYTE;
7392 }
7393 else
7394 {
7395 ZV = XMARKER (oldzv)->charpos;
7396 ZV_BYTE = marker_byte_position (oldzv);
7397 }
7398
7399 if (point_at_end)
7400 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7401 else
7402 /* We can't do Fgoto_char (oldpoint) because it will run some
7403 Lisp code. */
7404 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7405 XMARKER (oldpoint)->bytepos);
7406
7407 UNGCPRO;
7408 unchain_marker (XMARKER (oldpoint));
7409 unchain_marker (XMARKER (oldbegv));
7410 unchain_marker (XMARKER (oldzv));
7411
7412 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7413 set_buffer_internal (oldbuf);
7414 if (NILP (tem))
7415 windows_or_buffers_changed = old_windows_or_buffers_changed;
7416 message_log_need_newline = !nlflag;
7417 Vdeactivate_mark = old_deactivate_mark;
7418 }
7419 }
7420
7421
7422 /* We are at the end of the buffer after just having inserted a newline.
7423 (Note: We depend on the fact we won't be crossing the gap.)
7424 Check to see if the most recent message looks a lot like the previous one.
7425 Return 0 if different, 1 if the new one should just replace it, or a
7426 value N > 1 if we should also append " [N times]". */
7427
7428 static int
7429 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7430 int prev_bol, this_bol;
7431 int prev_bol_byte, this_bol_byte;
7432 {
7433 int i;
7434 int len = Z_BYTE - 1 - this_bol_byte;
7435 int seen_dots = 0;
7436 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7437 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7438
7439 for (i = 0; i < len; i++)
7440 {
7441 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7442 seen_dots = 1;
7443 if (p1[i] != p2[i])
7444 return seen_dots;
7445 }
7446 p1 += len;
7447 if (*p1 == '\n')
7448 return 2;
7449 if (*p1++ == ' ' && *p1++ == '[')
7450 {
7451 int n = 0;
7452 while (*p1 >= '0' && *p1 <= '9')
7453 n = n * 10 + *p1++ - '0';
7454 if (strncmp (p1, " times]\n", 8) == 0)
7455 return n+1;
7456 }
7457 return 0;
7458 }
7459 \f
7460
7461 /* Display an echo area message M with a specified length of NBYTES
7462 bytes. The string may include null characters. If M is 0, clear
7463 out any existing message, and let the mini-buffer text show
7464 through.
7465
7466 This may GC, so the buffer M must NOT point to a Lisp string. */
7467
7468 void
7469 message2 (m, nbytes, multibyte)
7470 const char *m;
7471 int nbytes;
7472 int multibyte;
7473 {
7474 /* First flush out any partial line written with print. */
7475 message_log_maybe_newline ();
7476 if (m)
7477 message_dolog (m, nbytes, 1, multibyte);
7478 message2_nolog (m, nbytes, multibyte);
7479 }
7480
7481
7482 /* The non-logging counterpart of message2. */
7483
7484 void
7485 message2_nolog (m, nbytes, multibyte)
7486 const char *m;
7487 int nbytes, multibyte;
7488 {
7489 struct frame *sf = SELECTED_FRAME ();
7490 message_enable_multibyte = multibyte;
7491
7492 if (noninteractive)
7493 {
7494 if (noninteractive_need_newline)
7495 putc ('\n', stderr);
7496 noninteractive_need_newline = 0;
7497 if (m)
7498 fwrite (m, nbytes, 1, stderr);
7499 if (cursor_in_echo_area == 0)
7500 fprintf (stderr, "\n");
7501 fflush (stderr);
7502 }
7503 /* A null message buffer means that the frame hasn't really been
7504 initialized yet. Error messages get reported properly by
7505 cmd_error, so this must be just an informative message; toss it. */
7506 else if (INTERACTIVE
7507 && sf->glyphs_initialized_p
7508 && FRAME_MESSAGE_BUF (sf))
7509 {
7510 Lisp_Object mini_window;
7511 struct frame *f;
7512
7513 /* Get the frame containing the mini-buffer
7514 that the selected frame is using. */
7515 mini_window = FRAME_MINIBUF_WINDOW (sf);
7516 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7517
7518 FRAME_SAMPLE_VISIBILITY (f);
7519 if (FRAME_VISIBLE_P (sf)
7520 && ! FRAME_VISIBLE_P (f))
7521 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7522
7523 if (m)
7524 {
7525 set_message (m, Qnil, nbytes, multibyte);
7526 if (minibuffer_auto_raise)
7527 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7528 }
7529 else
7530 clear_message (1, 1);
7531
7532 do_pending_window_change (0);
7533 echo_area_display (1);
7534 do_pending_window_change (0);
7535 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7536 (*frame_up_to_date_hook) (f);
7537 }
7538 }
7539
7540
7541 /* Display an echo area message M with a specified length of NBYTES
7542 bytes. The string may include null characters. If M is not a
7543 string, clear out any existing message, and let the mini-buffer
7544 text show through.
7545
7546 This function cancels echoing. */
7547
7548 void
7549 message3 (m, nbytes, multibyte)
7550 Lisp_Object m;
7551 int nbytes;
7552 int multibyte;
7553 {
7554 struct gcpro gcpro1;
7555
7556 GCPRO1 (m);
7557 clear_message (1,1);
7558 cancel_echoing ();
7559
7560 /* First flush out any partial line written with print. */
7561 message_log_maybe_newline ();
7562 if (STRINGP (m))
7563 {
7564 char *buffer;
7565 USE_SAFE_ALLOCA;
7566
7567 SAFE_ALLOCA (buffer, char *, nbytes);
7568 bcopy (SDATA (m), buffer, nbytes);
7569 message_dolog (buffer, nbytes, 1, multibyte);
7570 SAFE_FREE ();
7571 }
7572 message3_nolog (m, nbytes, multibyte);
7573
7574 UNGCPRO;
7575 }
7576
7577
7578 /* The non-logging version of message3.
7579 This does not cancel echoing, because it is used for echoing.
7580 Perhaps we need to make a separate function for echoing
7581 and make this cancel echoing. */
7582
7583 void
7584 message3_nolog (m, nbytes, multibyte)
7585 Lisp_Object m;
7586 int nbytes, multibyte;
7587 {
7588 struct frame *sf = SELECTED_FRAME ();
7589 message_enable_multibyte = multibyte;
7590
7591 if (noninteractive)
7592 {
7593 if (noninteractive_need_newline)
7594 putc ('\n', stderr);
7595 noninteractive_need_newline = 0;
7596 if (STRINGP (m))
7597 fwrite (SDATA (m), nbytes, 1, stderr);
7598 if (cursor_in_echo_area == 0)
7599 fprintf (stderr, "\n");
7600 fflush (stderr);
7601 }
7602 /* A null message buffer means that the frame hasn't really been
7603 initialized yet. Error messages get reported properly by
7604 cmd_error, so this must be just an informative message; toss it. */
7605 else if (INTERACTIVE
7606 && sf->glyphs_initialized_p
7607 && FRAME_MESSAGE_BUF (sf))
7608 {
7609 Lisp_Object mini_window;
7610 Lisp_Object frame;
7611 struct frame *f;
7612
7613 /* Get the frame containing the mini-buffer
7614 that the selected frame is using. */
7615 mini_window = FRAME_MINIBUF_WINDOW (sf);
7616 frame = XWINDOW (mini_window)->frame;
7617 f = XFRAME (frame);
7618
7619 FRAME_SAMPLE_VISIBILITY (f);
7620 if (FRAME_VISIBLE_P (sf)
7621 && !FRAME_VISIBLE_P (f))
7622 Fmake_frame_visible (frame);
7623
7624 if (STRINGP (m) && SCHARS (m) > 0)
7625 {
7626 set_message (NULL, m, nbytes, multibyte);
7627 if (minibuffer_auto_raise)
7628 Fraise_frame (frame);
7629 /* Assume we are not echoing.
7630 (If we are, echo_now will override this.) */
7631 echo_message_buffer = Qnil;
7632 }
7633 else
7634 clear_message (1, 1);
7635
7636 do_pending_window_change (0);
7637 echo_area_display (1);
7638 do_pending_window_change (0);
7639 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7640 (*frame_up_to_date_hook) (f);
7641 }
7642 }
7643
7644
7645 /* Display a null-terminated echo area message M. If M is 0, clear
7646 out any existing message, and let the mini-buffer text show through.
7647
7648 The buffer M must continue to exist until after the echo area gets
7649 cleared or some other message gets displayed there. Do not pass
7650 text that is stored in a Lisp string. Do not pass text in a buffer
7651 that was alloca'd. */
7652
7653 void
7654 message1 (m)
7655 char *m;
7656 {
7657 message2 (m, (m ? strlen (m) : 0), 0);
7658 }
7659
7660
7661 /* The non-logging counterpart of message1. */
7662
7663 void
7664 message1_nolog (m)
7665 char *m;
7666 {
7667 message2_nolog (m, (m ? strlen (m) : 0), 0);
7668 }
7669
7670 /* Display a message M which contains a single %s
7671 which gets replaced with STRING. */
7672
7673 void
7674 message_with_string (m, string, log)
7675 char *m;
7676 Lisp_Object string;
7677 int log;
7678 {
7679 CHECK_STRING (string);
7680
7681 if (noninteractive)
7682 {
7683 if (m)
7684 {
7685 if (noninteractive_need_newline)
7686 putc ('\n', stderr);
7687 noninteractive_need_newline = 0;
7688 fprintf (stderr, m, SDATA (string));
7689 if (cursor_in_echo_area == 0)
7690 fprintf (stderr, "\n");
7691 fflush (stderr);
7692 }
7693 }
7694 else if (INTERACTIVE)
7695 {
7696 /* The frame whose minibuffer we're going to display the message on.
7697 It may be larger than the selected frame, so we need
7698 to use its buffer, not the selected frame's buffer. */
7699 Lisp_Object mini_window;
7700 struct frame *f, *sf = SELECTED_FRAME ();
7701
7702 /* Get the frame containing the minibuffer
7703 that the selected frame is using. */
7704 mini_window = FRAME_MINIBUF_WINDOW (sf);
7705 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7706
7707 /* A null message buffer means that the frame hasn't really been
7708 initialized yet. Error messages get reported properly by
7709 cmd_error, so this must be just an informative message; toss it. */
7710 if (FRAME_MESSAGE_BUF (f))
7711 {
7712 Lisp_Object args[2], message;
7713 struct gcpro gcpro1, gcpro2;
7714
7715 args[0] = build_string (m);
7716 args[1] = message = string;
7717 GCPRO2 (args[0], message);
7718 gcpro1.nvars = 2;
7719
7720 message = Fformat (2, args);
7721
7722 if (log)
7723 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7724 else
7725 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7726
7727 UNGCPRO;
7728
7729 /* Print should start at the beginning of the message
7730 buffer next time. */
7731 message_buf_print = 0;
7732 }
7733 }
7734 }
7735
7736
7737 /* Dump an informative message to the minibuf. If M is 0, clear out
7738 any existing message, and let the mini-buffer text show through. */
7739
7740 /* VARARGS 1 */
7741 void
7742 message (m, a1, a2, a3)
7743 char *m;
7744 EMACS_INT a1, a2, a3;
7745 {
7746 if (noninteractive)
7747 {
7748 if (m)
7749 {
7750 if (noninteractive_need_newline)
7751 putc ('\n', stderr);
7752 noninteractive_need_newline = 0;
7753 fprintf (stderr, m, a1, a2, a3);
7754 if (cursor_in_echo_area == 0)
7755 fprintf (stderr, "\n");
7756 fflush (stderr);
7757 }
7758 }
7759 else if (INTERACTIVE)
7760 {
7761 /* The frame whose mini-buffer we're going to display the message
7762 on. It may be larger than the selected frame, so we need to
7763 use its buffer, not the selected frame's buffer. */
7764 Lisp_Object mini_window;
7765 struct frame *f, *sf = SELECTED_FRAME ();
7766
7767 /* Get the frame containing the mini-buffer
7768 that the selected frame is using. */
7769 mini_window = FRAME_MINIBUF_WINDOW (sf);
7770 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7771
7772 /* A null message buffer means that the frame hasn't really been
7773 initialized yet. Error messages get reported properly by
7774 cmd_error, so this must be just an informative message; toss
7775 it. */
7776 if (FRAME_MESSAGE_BUF (f))
7777 {
7778 if (m)
7779 {
7780 int len;
7781 #ifdef NO_ARG_ARRAY
7782 char *a[3];
7783 a[0] = (char *) a1;
7784 a[1] = (char *) a2;
7785 a[2] = (char *) a3;
7786
7787 len = doprnt (FRAME_MESSAGE_BUF (f),
7788 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7789 #else
7790 len = doprnt (FRAME_MESSAGE_BUF (f),
7791 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7792 (char **) &a1);
7793 #endif /* NO_ARG_ARRAY */
7794
7795 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7796 }
7797 else
7798 message1 (0);
7799
7800 /* Print should start at the beginning of the message
7801 buffer next time. */
7802 message_buf_print = 0;
7803 }
7804 }
7805 }
7806
7807
7808 /* The non-logging version of message. */
7809
7810 void
7811 message_nolog (m, a1, a2, a3)
7812 char *m;
7813 EMACS_INT a1, a2, a3;
7814 {
7815 Lisp_Object old_log_max;
7816 old_log_max = Vmessage_log_max;
7817 Vmessage_log_max = Qnil;
7818 message (m, a1, a2, a3);
7819 Vmessage_log_max = old_log_max;
7820 }
7821
7822
7823 /* Display the current message in the current mini-buffer. This is
7824 only called from error handlers in process.c, and is not time
7825 critical. */
7826
7827 void
7828 update_echo_area ()
7829 {
7830 if (!NILP (echo_area_buffer[0]))
7831 {
7832 Lisp_Object string;
7833 string = Fcurrent_message ();
7834 message3 (string, SBYTES (string),
7835 !NILP (current_buffer->enable_multibyte_characters));
7836 }
7837 }
7838
7839
7840 /* Make sure echo area buffers in `echo_buffers' are live.
7841 If they aren't, make new ones. */
7842
7843 static void
7844 ensure_echo_area_buffers ()
7845 {
7846 int i;
7847
7848 for (i = 0; i < 2; ++i)
7849 if (!BUFFERP (echo_buffer[i])
7850 || NILP (XBUFFER (echo_buffer[i])->name))
7851 {
7852 char name[30];
7853 Lisp_Object old_buffer;
7854 int j;
7855
7856 old_buffer = echo_buffer[i];
7857 sprintf (name, " *Echo Area %d*", i);
7858 echo_buffer[i] = Fget_buffer_create (build_string (name));
7859 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7860
7861 for (j = 0; j < 2; ++j)
7862 if (EQ (old_buffer, echo_area_buffer[j]))
7863 echo_area_buffer[j] = echo_buffer[i];
7864 }
7865 }
7866
7867
7868 /* Call FN with args A1..A4 with either the current or last displayed
7869 echo_area_buffer as current buffer.
7870
7871 WHICH zero means use the current message buffer
7872 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7873 from echo_buffer[] and clear it.
7874
7875 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7876 suitable buffer from echo_buffer[] and clear it.
7877
7878 Value is what FN returns. */
7879
7880 static int
7881 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7882 struct window *w;
7883 int which;
7884 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7885 EMACS_INT a1;
7886 Lisp_Object a2;
7887 EMACS_INT a3, a4;
7888 {
7889 Lisp_Object buffer;
7890 int this_one, the_other, clear_buffer_p, rc;
7891 int count = SPECPDL_INDEX ();
7892
7893 /* If buffers aren't live, make new ones. */
7894 ensure_echo_area_buffers ();
7895
7896 clear_buffer_p = 0;
7897
7898 if (which == 0)
7899 this_one = 0, the_other = 1;
7900 else if (which > 0)
7901 this_one = 1, the_other = 0;
7902
7903 /* Choose a suitable buffer from echo_buffer[] is we don't
7904 have one. */
7905 if (NILP (echo_area_buffer[this_one]))
7906 {
7907 echo_area_buffer[this_one]
7908 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7909 ? echo_buffer[the_other]
7910 : echo_buffer[this_one]);
7911 clear_buffer_p = 1;
7912 }
7913
7914 buffer = echo_area_buffer[this_one];
7915
7916 /* Don't get confused by reusing the buffer used for echoing
7917 for a different purpose. */
7918 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7919 cancel_echoing ();
7920
7921 record_unwind_protect (unwind_with_echo_area_buffer,
7922 with_echo_area_buffer_unwind_data (w));
7923
7924 /* Make the echo area buffer current. Note that for display
7925 purposes, it is not necessary that the displayed window's buffer
7926 == current_buffer, except for text property lookup. So, let's
7927 only set that buffer temporarily here without doing a full
7928 Fset_window_buffer. We must also change w->pointm, though,
7929 because otherwise an assertions in unshow_buffer fails, and Emacs
7930 aborts. */
7931 set_buffer_internal_1 (XBUFFER (buffer));
7932 if (w)
7933 {
7934 w->buffer = buffer;
7935 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7936 }
7937
7938 current_buffer->undo_list = Qt;
7939 current_buffer->read_only = Qnil;
7940 specbind (Qinhibit_read_only, Qt);
7941 specbind (Qinhibit_modification_hooks, Qt);
7942
7943 if (clear_buffer_p && Z > BEG)
7944 del_range (BEG, Z);
7945
7946 xassert (BEGV >= BEG);
7947 xassert (ZV <= Z && ZV >= BEGV);
7948
7949 rc = fn (a1, a2, a3, a4);
7950
7951 xassert (BEGV >= BEG);
7952 xassert (ZV <= Z && ZV >= BEGV);
7953
7954 unbind_to (count, Qnil);
7955 return rc;
7956 }
7957
7958
7959 /* Save state that should be preserved around the call to the function
7960 FN called in with_echo_area_buffer. */
7961
7962 static Lisp_Object
7963 with_echo_area_buffer_unwind_data (w)
7964 struct window *w;
7965 {
7966 int i = 0;
7967 Lisp_Object vector;
7968
7969 /* Reduce consing by keeping one vector in
7970 Vwith_echo_area_save_vector. */
7971 vector = Vwith_echo_area_save_vector;
7972 Vwith_echo_area_save_vector = Qnil;
7973
7974 if (NILP (vector))
7975 vector = Fmake_vector (make_number (7), Qnil);
7976
7977 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7978 AREF (vector, i) = Vdeactivate_mark, ++i;
7979 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7980
7981 if (w)
7982 {
7983 XSETWINDOW (AREF (vector, i), w); ++i;
7984 AREF (vector, i) = w->buffer; ++i;
7985 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7986 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7987 }
7988 else
7989 {
7990 int end = i + 4;
7991 for (; i < end; ++i)
7992 AREF (vector, i) = Qnil;
7993 }
7994
7995 xassert (i == ASIZE (vector));
7996 return vector;
7997 }
7998
7999
8000 /* Restore global state from VECTOR which was created by
8001 with_echo_area_buffer_unwind_data. */
8002
8003 static Lisp_Object
8004 unwind_with_echo_area_buffer (vector)
8005 Lisp_Object vector;
8006 {
8007 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8008 Vdeactivate_mark = AREF (vector, 1);
8009 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8010
8011 if (WINDOWP (AREF (vector, 3)))
8012 {
8013 struct window *w;
8014 Lisp_Object buffer, charpos, bytepos;
8015
8016 w = XWINDOW (AREF (vector, 3));
8017 buffer = AREF (vector, 4);
8018 charpos = AREF (vector, 5);
8019 bytepos = AREF (vector, 6);
8020
8021 w->buffer = buffer;
8022 set_marker_both (w->pointm, buffer,
8023 XFASTINT (charpos), XFASTINT (bytepos));
8024 }
8025
8026 Vwith_echo_area_save_vector = vector;
8027 return Qnil;
8028 }
8029
8030
8031 /* Set up the echo area for use by print functions. MULTIBYTE_P
8032 non-zero means we will print multibyte. */
8033
8034 void
8035 setup_echo_area_for_printing (multibyte_p)
8036 int multibyte_p;
8037 {
8038 /* If we can't find an echo area any more, exit. */
8039 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8040 Fkill_emacs (Qnil);
8041
8042 ensure_echo_area_buffers ();
8043
8044 if (!message_buf_print)
8045 {
8046 /* A message has been output since the last time we printed.
8047 Choose a fresh echo area buffer. */
8048 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8049 echo_area_buffer[0] = echo_buffer[1];
8050 else
8051 echo_area_buffer[0] = echo_buffer[0];
8052
8053 /* Switch to that buffer and clear it. */
8054 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8055 current_buffer->truncate_lines = Qnil;
8056
8057 if (Z > BEG)
8058 {
8059 int count = SPECPDL_INDEX ();
8060 specbind (Qinhibit_read_only, Qt);
8061 /* Note that undo recording is always disabled. */
8062 del_range (BEG, Z);
8063 unbind_to (count, Qnil);
8064 }
8065 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8066
8067 /* Set up the buffer for the multibyteness we need. */
8068 if (multibyte_p
8069 != !NILP (current_buffer->enable_multibyte_characters))
8070 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8071
8072 /* Raise the frame containing the echo area. */
8073 if (minibuffer_auto_raise)
8074 {
8075 struct frame *sf = SELECTED_FRAME ();
8076 Lisp_Object mini_window;
8077 mini_window = FRAME_MINIBUF_WINDOW (sf);
8078 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8079 }
8080
8081 message_log_maybe_newline ();
8082 message_buf_print = 1;
8083 }
8084 else
8085 {
8086 if (NILP (echo_area_buffer[0]))
8087 {
8088 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8089 echo_area_buffer[0] = echo_buffer[1];
8090 else
8091 echo_area_buffer[0] = echo_buffer[0];
8092 }
8093
8094 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8095 {
8096 /* Someone switched buffers between print requests. */
8097 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8098 current_buffer->truncate_lines = Qnil;
8099 }
8100 }
8101 }
8102
8103
8104 /* Display an echo area message in window W. Value is non-zero if W's
8105 height is changed. If display_last_displayed_message_p is
8106 non-zero, display the message that was last displayed, otherwise
8107 display the current message. */
8108
8109 static int
8110 display_echo_area (w)
8111 struct window *w;
8112 {
8113 int i, no_message_p, window_height_changed_p, count;
8114
8115 /* Temporarily disable garbage collections while displaying the echo
8116 area. This is done because a GC can print a message itself.
8117 That message would modify the echo area buffer's contents while a
8118 redisplay of the buffer is going on, and seriously confuse
8119 redisplay. */
8120 count = inhibit_garbage_collection ();
8121
8122 /* If there is no message, we must call display_echo_area_1
8123 nevertheless because it resizes the window. But we will have to
8124 reset the echo_area_buffer in question to nil at the end because
8125 with_echo_area_buffer will sets it to an empty buffer. */
8126 i = display_last_displayed_message_p ? 1 : 0;
8127 no_message_p = NILP (echo_area_buffer[i]);
8128
8129 window_height_changed_p
8130 = with_echo_area_buffer (w, display_last_displayed_message_p,
8131 display_echo_area_1,
8132 (EMACS_INT) w, Qnil, 0, 0);
8133
8134 if (no_message_p)
8135 echo_area_buffer[i] = Qnil;
8136
8137 unbind_to (count, Qnil);
8138 return window_height_changed_p;
8139 }
8140
8141
8142 /* Helper for display_echo_area. Display the current buffer which
8143 contains the current echo area message in window W, a mini-window,
8144 a pointer to which is passed in A1. A2..A4 are currently not used.
8145 Change the height of W so that all of the message is displayed.
8146 Value is non-zero if height of W was changed. */
8147
8148 static int
8149 display_echo_area_1 (a1, a2, a3, a4)
8150 EMACS_INT a1;
8151 Lisp_Object a2;
8152 EMACS_INT a3, a4;
8153 {
8154 struct window *w = (struct window *) a1;
8155 Lisp_Object window;
8156 struct text_pos start;
8157 int window_height_changed_p = 0;
8158
8159 /* Do this before displaying, so that we have a large enough glyph
8160 matrix for the display. If we can't get enough space for the
8161 whole text, display the last N lines. That works by setting w->start. */
8162 window_height_changed_p = resize_mini_window (w, 0);
8163
8164 /* Use the starting position chosen by resize_mini_window. */
8165 SET_TEXT_POS_FROM_MARKER (start, w->start);
8166
8167 /* Display. */
8168 clear_glyph_matrix (w->desired_matrix);
8169 XSETWINDOW (window, w);
8170 try_window (window, start, 0);
8171
8172 return window_height_changed_p;
8173 }
8174
8175
8176 /* Resize the echo area window to exactly the size needed for the
8177 currently displayed message, if there is one. If a mini-buffer
8178 is active, don't shrink it. */
8179
8180 void
8181 resize_echo_area_exactly ()
8182 {
8183 if (BUFFERP (echo_area_buffer[0])
8184 && WINDOWP (echo_area_window))
8185 {
8186 struct window *w = XWINDOW (echo_area_window);
8187 int resized_p;
8188 Lisp_Object resize_exactly;
8189
8190 if (minibuf_level == 0)
8191 resize_exactly = Qt;
8192 else
8193 resize_exactly = Qnil;
8194
8195 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8196 (EMACS_INT) w, resize_exactly, 0, 0);
8197 if (resized_p)
8198 {
8199 ++windows_or_buffers_changed;
8200 ++update_mode_lines;
8201 redisplay_internal (0);
8202 }
8203 }
8204 }
8205
8206
8207 /* Callback function for with_echo_area_buffer, when used from
8208 resize_echo_area_exactly. A1 contains a pointer to the window to
8209 resize, EXACTLY non-nil means resize the mini-window exactly to the
8210 size of the text displayed. A3 and A4 are not used. Value is what
8211 resize_mini_window returns. */
8212
8213 static int
8214 resize_mini_window_1 (a1, exactly, a3, a4)
8215 EMACS_INT a1;
8216 Lisp_Object exactly;
8217 EMACS_INT a3, a4;
8218 {
8219 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8220 }
8221
8222
8223 /* Resize mini-window W to fit the size of its contents. EXACT:P
8224 means size the window exactly to the size needed. Otherwise, it's
8225 only enlarged until W's buffer is empty.
8226
8227 Set W->start to the right place to begin display. If the whole
8228 contents fit, start at the beginning. Otherwise, start so as
8229 to make the end of the contents appear. This is particularly
8230 important for y-or-n-p, but seems desirable generally.
8231
8232 Value is non-zero if the window height has been changed. */
8233
8234 int
8235 resize_mini_window (w, exact_p)
8236 struct window *w;
8237 int exact_p;
8238 {
8239 struct frame *f = XFRAME (w->frame);
8240 int window_height_changed_p = 0;
8241
8242 xassert (MINI_WINDOW_P (w));
8243
8244 /* By default, start display at the beginning. */
8245 set_marker_both (w->start, w->buffer,
8246 BUF_BEGV (XBUFFER (w->buffer)),
8247 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8248
8249 /* Don't resize windows while redisplaying a window; it would
8250 confuse redisplay functions when the size of the window they are
8251 displaying changes from under them. Such a resizing can happen,
8252 for instance, when which-func prints a long message while
8253 we are running fontification-functions. We're running these
8254 functions with safe_call which binds inhibit-redisplay to t. */
8255 if (!NILP (Vinhibit_redisplay))
8256 return 0;
8257
8258 /* Nil means don't try to resize. */
8259 if (NILP (Vresize_mini_windows)
8260 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8261 return 0;
8262
8263 if (!FRAME_MINIBUF_ONLY_P (f))
8264 {
8265 struct it it;
8266 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8267 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8268 int height, max_height;
8269 int unit = FRAME_LINE_HEIGHT (f);
8270 struct text_pos start;
8271 struct buffer *old_current_buffer = NULL;
8272
8273 if (current_buffer != XBUFFER (w->buffer))
8274 {
8275 old_current_buffer = current_buffer;
8276 set_buffer_internal (XBUFFER (w->buffer));
8277 }
8278
8279 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8280
8281 /* Compute the max. number of lines specified by the user. */
8282 if (FLOATP (Vmax_mini_window_height))
8283 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8284 else if (INTEGERP (Vmax_mini_window_height))
8285 max_height = XINT (Vmax_mini_window_height);
8286 else
8287 max_height = total_height / 4;
8288
8289 /* Correct that max. height if it's bogus. */
8290 max_height = max (1, max_height);
8291 max_height = min (total_height, max_height);
8292
8293 /* Find out the height of the text in the window. */
8294 if (it.truncate_lines_p)
8295 height = 1;
8296 else
8297 {
8298 last_height = 0;
8299 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8300 if (it.max_ascent == 0 && it.max_descent == 0)
8301 height = it.current_y + last_height;
8302 else
8303 height = it.current_y + it.max_ascent + it.max_descent;
8304 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8305 height = (height + unit - 1) / unit;
8306 }
8307
8308 /* Compute a suitable window start. */
8309 if (height > max_height)
8310 {
8311 height = max_height;
8312 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8313 move_it_vertically_backward (&it, (height - 1) * unit);
8314 start = it.current.pos;
8315 }
8316 else
8317 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8318 SET_MARKER_FROM_TEXT_POS (w->start, start);
8319
8320 if (EQ (Vresize_mini_windows, Qgrow_only))
8321 {
8322 /* Let it grow only, until we display an empty message, in which
8323 case the window shrinks again. */
8324 if (height > WINDOW_TOTAL_LINES (w))
8325 {
8326 int old_height = WINDOW_TOTAL_LINES (w);
8327 freeze_window_starts (f, 1);
8328 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8329 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8330 }
8331 else if (height < WINDOW_TOTAL_LINES (w)
8332 && (exact_p || BEGV == ZV))
8333 {
8334 int old_height = WINDOW_TOTAL_LINES (w);
8335 freeze_window_starts (f, 0);
8336 shrink_mini_window (w);
8337 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8338 }
8339 }
8340 else
8341 {
8342 /* Always resize to exact size needed. */
8343 if (height > WINDOW_TOTAL_LINES (w))
8344 {
8345 int old_height = WINDOW_TOTAL_LINES (w);
8346 freeze_window_starts (f, 1);
8347 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8348 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8349 }
8350 else if (height < WINDOW_TOTAL_LINES (w))
8351 {
8352 int old_height = WINDOW_TOTAL_LINES (w);
8353 freeze_window_starts (f, 0);
8354 shrink_mini_window (w);
8355
8356 if (height)
8357 {
8358 freeze_window_starts (f, 1);
8359 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8360 }
8361
8362 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8363 }
8364 }
8365
8366 if (old_current_buffer)
8367 set_buffer_internal (old_current_buffer);
8368 }
8369
8370 return window_height_changed_p;
8371 }
8372
8373
8374 /* Value is the current message, a string, or nil if there is no
8375 current message. */
8376
8377 Lisp_Object
8378 current_message ()
8379 {
8380 Lisp_Object msg;
8381
8382 if (NILP (echo_area_buffer[0]))
8383 msg = Qnil;
8384 else
8385 {
8386 with_echo_area_buffer (0, 0, current_message_1,
8387 (EMACS_INT) &msg, Qnil, 0, 0);
8388 if (NILP (msg))
8389 echo_area_buffer[0] = Qnil;
8390 }
8391
8392 return msg;
8393 }
8394
8395
8396 static int
8397 current_message_1 (a1, a2, a3, a4)
8398 EMACS_INT a1;
8399 Lisp_Object a2;
8400 EMACS_INT a3, a4;
8401 {
8402 Lisp_Object *msg = (Lisp_Object *) a1;
8403
8404 if (Z > BEG)
8405 *msg = make_buffer_string (BEG, Z, 1);
8406 else
8407 *msg = Qnil;
8408 return 0;
8409 }
8410
8411
8412 /* Push the current message on Vmessage_stack for later restauration
8413 by restore_message. Value is non-zero if the current message isn't
8414 empty. This is a relatively infrequent operation, so it's not
8415 worth optimizing. */
8416
8417 int
8418 push_message ()
8419 {
8420 Lisp_Object msg;
8421 msg = current_message ();
8422 Vmessage_stack = Fcons (msg, Vmessage_stack);
8423 return STRINGP (msg);
8424 }
8425
8426
8427 /* Restore message display from the top of Vmessage_stack. */
8428
8429 void
8430 restore_message ()
8431 {
8432 Lisp_Object msg;
8433
8434 xassert (CONSP (Vmessage_stack));
8435 msg = XCAR (Vmessage_stack);
8436 if (STRINGP (msg))
8437 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8438 else
8439 message3_nolog (msg, 0, 0);
8440 }
8441
8442
8443 /* Handler for record_unwind_protect calling pop_message. */
8444
8445 Lisp_Object
8446 pop_message_unwind (dummy)
8447 Lisp_Object dummy;
8448 {
8449 pop_message ();
8450 return Qnil;
8451 }
8452
8453 /* Pop the top-most entry off Vmessage_stack. */
8454
8455 void
8456 pop_message ()
8457 {
8458 xassert (CONSP (Vmessage_stack));
8459 Vmessage_stack = XCDR (Vmessage_stack);
8460 }
8461
8462
8463 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8464 exits. If the stack is not empty, we have a missing pop_message
8465 somewhere. */
8466
8467 void
8468 check_message_stack ()
8469 {
8470 if (!NILP (Vmessage_stack))
8471 abort ();
8472 }
8473
8474
8475 /* Truncate to NCHARS what will be displayed in the echo area the next
8476 time we display it---but don't redisplay it now. */
8477
8478 void
8479 truncate_echo_area (nchars)
8480 int nchars;
8481 {
8482 if (nchars == 0)
8483 echo_area_buffer[0] = Qnil;
8484 /* A null message buffer means that the frame hasn't really been
8485 initialized yet. Error messages get reported properly by
8486 cmd_error, so this must be just an informative message; toss it. */
8487 else if (!noninteractive
8488 && INTERACTIVE
8489 && !NILP (echo_area_buffer[0]))
8490 {
8491 struct frame *sf = SELECTED_FRAME ();
8492 if (FRAME_MESSAGE_BUF (sf))
8493 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8494 }
8495 }
8496
8497
8498 /* Helper function for truncate_echo_area. Truncate the current
8499 message to at most NCHARS characters. */
8500
8501 static int
8502 truncate_message_1 (nchars, a2, a3, a4)
8503 EMACS_INT nchars;
8504 Lisp_Object a2;
8505 EMACS_INT a3, a4;
8506 {
8507 if (BEG + nchars < Z)
8508 del_range (BEG + nchars, Z);
8509 if (Z == BEG)
8510 echo_area_buffer[0] = Qnil;
8511 return 0;
8512 }
8513
8514
8515 /* Set the current message to a substring of S or STRING.
8516
8517 If STRING is a Lisp string, set the message to the first NBYTES
8518 bytes from STRING. NBYTES zero means use the whole string. If
8519 STRING is multibyte, the message will be displayed multibyte.
8520
8521 If S is not null, set the message to the first LEN bytes of S. LEN
8522 zero means use the whole string. MULTIBYTE_P non-zero means S is
8523 multibyte. Display the message multibyte in that case.
8524
8525 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8526 to t before calling set_message_1 (which calls insert).
8527 */
8528
8529 void
8530 set_message (s, string, nbytes, multibyte_p)
8531 const char *s;
8532 Lisp_Object string;
8533 int nbytes, multibyte_p;
8534 {
8535 message_enable_multibyte
8536 = ((s && multibyte_p)
8537 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8538
8539 with_echo_area_buffer (0, 0, set_message_1,
8540 (EMACS_INT) s, string, nbytes, multibyte_p);
8541 message_buf_print = 0;
8542 help_echo_showing_p = 0;
8543 }
8544
8545
8546 /* Helper function for set_message. Arguments have the same meaning
8547 as there, with A1 corresponding to S and A2 corresponding to STRING
8548 This function is called with the echo area buffer being
8549 current. */
8550
8551 static int
8552 set_message_1 (a1, a2, nbytes, multibyte_p)
8553 EMACS_INT a1;
8554 Lisp_Object a2;
8555 EMACS_INT nbytes, multibyte_p;
8556 {
8557 const char *s = (const char *) a1;
8558 Lisp_Object string = a2;
8559
8560 /* Change multibyteness of the echo buffer appropriately. */
8561 if (message_enable_multibyte
8562 != !NILP (current_buffer->enable_multibyte_characters))
8563 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8564
8565 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8566
8567 /* Insert new message at BEG. */
8568 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8569 Ferase_buffer ();
8570
8571 if (STRINGP (string))
8572 {
8573 int nchars;
8574
8575 if (nbytes == 0)
8576 nbytes = SBYTES (string);
8577 nchars = string_byte_to_char (string, nbytes);
8578
8579 /* This function takes care of single/multibyte conversion. We
8580 just have to ensure that the echo area buffer has the right
8581 setting of enable_multibyte_characters. */
8582 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8583 }
8584 else if (s)
8585 {
8586 if (nbytes == 0)
8587 nbytes = strlen (s);
8588
8589 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8590 {
8591 /* Convert from multi-byte to single-byte. */
8592 int i, c, n;
8593 unsigned char work[1];
8594
8595 /* Convert a multibyte string to single-byte. */
8596 for (i = 0; i < nbytes; i += n)
8597 {
8598 c = string_char_and_length (s + i, nbytes - i, &n);
8599 work[0] = (SINGLE_BYTE_CHAR_P (c)
8600 ? c
8601 : multibyte_char_to_unibyte (c, Qnil));
8602 insert_1_both (work, 1, 1, 1, 0, 0);
8603 }
8604 }
8605 else if (!multibyte_p
8606 && !NILP (current_buffer->enable_multibyte_characters))
8607 {
8608 /* Convert from single-byte to multi-byte. */
8609 int i, c, n;
8610 const unsigned char *msg = (const unsigned char *) s;
8611 unsigned char str[MAX_MULTIBYTE_LENGTH];
8612
8613 /* Convert a single-byte string to multibyte. */
8614 for (i = 0; i < nbytes; i++)
8615 {
8616 c = unibyte_char_to_multibyte (msg[i]);
8617 n = CHAR_STRING (c, str);
8618 insert_1_both (str, 1, n, 1, 0, 0);
8619 }
8620 }
8621 else
8622 insert_1 (s, nbytes, 1, 0, 0);
8623 }
8624
8625 return 0;
8626 }
8627
8628
8629 /* Clear messages. CURRENT_P non-zero means clear the current
8630 message. LAST_DISPLAYED_P non-zero means clear the message
8631 last displayed. */
8632
8633 void
8634 clear_message (current_p, last_displayed_p)
8635 int current_p, last_displayed_p;
8636 {
8637 if (current_p)
8638 {
8639 echo_area_buffer[0] = Qnil;
8640 message_cleared_p = 1;
8641 }
8642
8643 if (last_displayed_p)
8644 echo_area_buffer[1] = Qnil;
8645
8646 message_buf_print = 0;
8647 }
8648
8649 /* Clear garbaged frames.
8650
8651 This function is used where the old redisplay called
8652 redraw_garbaged_frames which in turn called redraw_frame which in
8653 turn called clear_frame. The call to clear_frame was a source of
8654 flickering. I believe a clear_frame is not necessary. It should
8655 suffice in the new redisplay to invalidate all current matrices,
8656 and ensure a complete redisplay of all windows. */
8657
8658 static void
8659 clear_garbaged_frames ()
8660 {
8661 if (frame_garbaged)
8662 {
8663 Lisp_Object tail, frame;
8664 int changed_count = 0;
8665
8666 FOR_EACH_FRAME (tail, frame)
8667 {
8668 struct frame *f = XFRAME (frame);
8669
8670 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8671 {
8672 if (f->resized_p)
8673 {
8674 Fredraw_frame (frame);
8675 f->force_flush_display_p = 1;
8676 }
8677 clear_current_matrices (f);
8678 changed_count++;
8679 f->garbaged = 0;
8680 f->resized_p = 0;
8681 }
8682 }
8683
8684 frame_garbaged = 0;
8685 if (changed_count)
8686 ++windows_or_buffers_changed;
8687 }
8688 }
8689
8690
8691 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8692 is non-zero update selected_frame. Value is non-zero if the
8693 mini-windows height has been changed. */
8694
8695 static int
8696 echo_area_display (update_frame_p)
8697 int update_frame_p;
8698 {
8699 Lisp_Object mini_window;
8700 struct window *w;
8701 struct frame *f;
8702 int window_height_changed_p = 0;
8703 struct frame *sf = SELECTED_FRAME ();
8704
8705 mini_window = FRAME_MINIBUF_WINDOW (sf);
8706 w = XWINDOW (mini_window);
8707 f = XFRAME (WINDOW_FRAME (w));
8708
8709 /* Don't display if frame is invisible or not yet initialized. */
8710 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8711 return 0;
8712
8713 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8714 #ifndef MAC_OS8
8715 #ifdef HAVE_WINDOW_SYSTEM
8716 /* When Emacs starts, selected_frame may be a visible terminal
8717 frame, even if we run under a window system. If we let this
8718 through, a message would be displayed on the terminal. */
8719 if (EQ (selected_frame, Vterminal_frame)
8720 && !NILP (Vwindow_system))
8721 return 0;
8722 #endif /* HAVE_WINDOW_SYSTEM */
8723 #endif
8724
8725 /* Redraw garbaged frames. */
8726 if (frame_garbaged)
8727 clear_garbaged_frames ();
8728
8729 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8730 {
8731 echo_area_window = mini_window;
8732 window_height_changed_p = display_echo_area (w);
8733 w->must_be_updated_p = 1;
8734
8735 /* Update the display, unless called from redisplay_internal.
8736 Also don't update the screen during redisplay itself. The
8737 update will happen at the end of redisplay, and an update
8738 here could cause confusion. */
8739 if (update_frame_p && !redisplaying_p)
8740 {
8741 int n = 0;
8742
8743 /* If the display update has been interrupted by pending
8744 input, update mode lines in the frame. Due to the
8745 pending input, it might have been that redisplay hasn't
8746 been called, so that mode lines above the echo area are
8747 garbaged. This looks odd, so we prevent it here. */
8748 if (!display_completed)
8749 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8750
8751 if (window_height_changed_p
8752 /* Don't do this if Emacs is shutting down. Redisplay
8753 needs to run hooks. */
8754 && !NILP (Vrun_hooks))
8755 {
8756 /* Must update other windows. Likewise as in other
8757 cases, don't let this update be interrupted by
8758 pending input. */
8759 int count = SPECPDL_INDEX ();
8760 specbind (Qredisplay_dont_pause, Qt);
8761 windows_or_buffers_changed = 1;
8762 redisplay_internal (0);
8763 unbind_to (count, Qnil);
8764 }
8765 else if (FRAME_WINDOW_P (f) && n == 0)
8766 {
8767 /* Window configuration is the same as before.
8768 Can do with a display update of the echo area,
8769 unless we displayed some mode lines. */
8770 update_single_window (w, 1);
8771 rif->flush_display (f);
8772 }
8773 else
8774 update_frame (f, 1, 1);
8775
8776 /* If cursor is in the echo area, make sure that the next
8777 redisplay displays the minibuffer, so that the cursor will
8778 be replaced with what the minibuffer wants. */
8779 if (cursor_in_echo_area)
8780 ++windows_or_buffers_changed;
8781 }
8782 }
8783 else if (!EQ (mini_window, selected_window))
8784 windows_or_buffers_changed++;
8785
8786 /* The current message is now also the last one displayed. */
8787 echo_area_buffer[1] = echo_area_buffer[0];
8788
8789 /* Prevent redisplay optimization in redisplay_internal by resetting
8790 this_line_start_pos. This is done because the mini-buffer now
8791 displays the message instead of its buffer text. */
8792 if (EQ (mini_window, selected_window))
8793 CHARPOS (this_line_start_pos) = 0;
8794
8795 return window_height_changed_p;
8796 }
8797
8798
8799 \f
8800 /***********************************************************************
8801 Mode Lines and Frame Titles
8802 ***********************************************************************/
8803
8804 /* A buffer for constructing non-propertized mode-line strings and
8805 frame titles in it; allocated from the heap in init_xdisp and
8806 resized as needed in store_mode_line_noprop_char. */
8807
8808 static char *mode_line_noprop_buf;
8809
8810 /* The buffer's end, and a current output position in it. */
8811
8812 static char *mode_line_noprop_buf_end;
8813 static char *mode_line_noprop_ptr;
8814
8815 #define MODE_LINE_NOPROP_LEN(start) \
8816 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8817
8818 static enum {
8819 MODE_LINE_DISPLAY = 0,
8820 MODE_LINE_TITLE,
8821 MODE_LINE_NOPROP,
8822 MODE_LINE_STRING
8823 } mode_line_target;
8824
8825 /* Alist that caches the results of :propertize.
8826 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8827 static Lisp_Object mode_line_proptrans_alist;
8828
8829 /* List of strings making up the mode-line. */
8830 static Lisp_Object mode_line_string_list;
8831
8832 /* Base face property when building propertized mode line string. */
8833 static Lisp_Object mode_line_string_face;
8834 static Lisp_Object mode_line_string_face_prop;
8835
8836
8837 /* Unwind data for mode line strings */
8838
8839 static Lisp_Object Vmode_line_unwind_vector;
8840
8841 static Lisp_Object
8842 format_mode_line_unwind_data (obuf, save_proptrans)
8843 struct buffer *obuf;
8844 {
8845 Lisp_Object vector;
8846
8847 /* Reduce consing by keeping one vector in
8848 Vwith_echo_area_save_vector. */
8849 vector = Vmode_line_unwind_vector;
8850 Vmode_line_unwind_vector = Qnil;
8851
8852 if (NILP (vector))
8853 vector = Fmake_vector (make_number (7), Qnil);
8854
8855 AREF (vector, 0) = make_number (mode_line_target);
8856 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8857 AREF (vector, 2) = mode_line_string_list;
8858 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8859 AREF (vector, 4) = mode_line_string_face;
8860 AREF (vector, 5) = mode_line_string_face_prop;
8861
8862 if (obuf)
8863 XSETBUFFER (AREF (vector, 6), obuf);
8864 else
8865 AREF (vector, 6) = Qnil;
8866
8867 return vector;
8868 }
8869
8870 static Lisp_Object
8871 unwind_format_mode_line (vector)
8872 Lisp_Object vector;
8873 {
8874 mode_line_target = XINT (AREF (vector, 0));
8875 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8876 mode_line_string_list = AREF (vector, 2);
8877 if (! EQ (AREF (vector, 3), Qt))
8878 mode_line_proptrans_alist = AREF (vector, 3);
8879 mode_line_string_face = AREF (vector, 4);
8880 mode_line_string_face_prop = AREF (vector, 5);
8881
8882 if (!NILP (AREF (vector, 6)))
8883 {
8884 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8885 AREF (vector, 6) = Qnil;
8886 }
8887
8888 Vmode_line_unwind_vector = vector;
8889 return Qnil;
8890 }
8891
8892
8893 /* Store a single character C for the frame title in mode_line_noprop_buf.
8894 Re-allocate mode_line_noprop_buf if necessary. */
8895
8896 static void
8897 #ifdef PROTOTYPES
8898 store_mode_line_noprop_char (char c)
8899 #else
8900 store_mode_line_noprop_char (c)
8901 char c;
8902 #endif
8903 {
8904 /* If output position has reached the end of the allocated buffer,
8905 double the buffer's size. */
8906 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8907 {
8908 int len = MODE_LINE_NOPROP_LEN (0);
8909 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8910 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8911 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8912 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8913 }
8914
8915 *mode_line_noprop_ptr++ = c;
8916 }
8917
8918
8919 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8920 mode_line_noprop_ptr. STR is the string to store. Do not copy
8921 characters that yield more columns than PRECISION; PRECISION <= 0
8922 means copy the whole string. Pad with spaces until FIELD_WIDTH
8923 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8924 pad. Called from display_mode_element when it is used to build a
8925 frame title. */
8926
8927 static int
8928 store_mode_line_noprop (str, field_width, precision)
8929 const unsigned char *str;
8930 int field_width, precision;
8931 {
8932 int n = 0;
8933 int dummy, nbytes;
8934
8935 /* Copy at most PRECISION chars from STR. */
8936 nbytes = strlen (str);
8937 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8938 while (nbytes--)
8939 store_mode_line_noprop_char (*str++);
8940
8941 /* Fill up with spaces until FIELD_WIDTH reached. */
8942 while (field_width > 0
8943 && n < field_width)
8944 {
8945 store_mode_line_noprop_char (' ');
8946 ++n;
8947 }
8948
8949 return n;
8950 }
8951
8952 /***********************************************************************
8953 Frame Titles
8954 ***********************************************************************/
8955
8956 #ifdef HAVE_WINDOW_SYSTEM
8957
8958 /* Set the title of FRAME, if it has changed. The title format is
8959 Vicon_title_format if FRAME is iconified, otherwise it is
8960 frame_title_format. */
8961
8962 static void
8963 x_consider_frame_title (frame)
8964 Lisp_Object frame;
8965 {
8966 struct frame *f = XFRAME (frame);
8967
8968 if (FRAME_WINDOW_P (f)
8969 || FRAME_MINIBUF_ONLY_P (f)
8970 || f->explicit_name)
8971 {
8972 /* Do we have more than one visible frame on this X display? */
8973 Lisp_Object tail;
8974 Lisp_Object fmt;
8975 int title_start;
8976 char *title;
8977 int len;
8978 struct it it;
8979 int count = SPECPDL_INDEX ();
8980
8981 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8982 {
8983 Lisp_Object other_frame = XCAR (tail);
8984 struct frame *tf = XFRAME (other_frame);
8985
8986 if (tf != f
8987 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8988 && !FRAME_MINIBUF_ONLY_P (tf)
8989 && !EQ (other_frame, tip_frame)
8990 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8991 break;
8992 }
8993
8994 /* Set global variable indicating that multiple frames exist. */
8995 multiple_frames = CONSP (tail);
8996
8997 /* Switch to the buffer of selected window of the frame. Set up
8998 mode_line_target so that display_mode_element will output into
8999 mode_line_noprop_buf; then display the title. */
9000 record_unwind_protect (unwind_format_mode_line,
9001 format_mode_line_unwind_data (current_buffer, 0));
9002
9003 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9004 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9005
9006 mode_line_target = MODE_LINE_TITLE;
9007 title_start = MODE_LINE_NOPROP_LEN (0);
9008 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9009 NULL, DEFAULT_FACE_ID);
9010 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9011 len = MODE_LINE_NOPROP_LEN (title_start);
9012 title = mode_line_noprop_buf + title_start;
9013 unbind_to (count, Qnil);
9014
9015 /* Set the title only if it's changed. This avoids consing in
9016 the common case where it hasn't. (If it turns out that we've
9017 already wasted too much time by walking through the list with
9018 display_mode_element, then we might need to optimize at a
9019 higher level than this.) */
9020 if (! STRINGP (f->name)
9021 || SBYTES (f->name) != len
9022 || bcmp (title, SDATA (f->name), len) != 0)
9023 x_implicitly_set_name (f, make_string (title, len), Qnil);
9024 }
9025 }
9026
9027 #endif /* not HAVE_WINDOW_SYSTEM */
9028
9029
9030
9031 \f
9032 /***********************************************************************
9033 Menu Bars
9034 ***********************************************************************/
9035
9036
9037 /* Prepare for redisplay by updating menu-bar item lists when
9038 appropriate. This can call eval. */
9039
9040 void
9041 prepare_menu_bars ()
9042 {
9043 int all_windows;
9044 struct gcpro gcpro1, gcpro2;
9045 struct frame *f;
9046 Lisp_Object tooltip_frame;
9047
9048 #ifdef HAVE_WINDOW_SYSTEM
9049 tooltip_frame = tip_frame;
9050 #else
9051 tooltip_frame = Qnil;
9052 #endif
9053
9054 /* Update all frame titles based on their buffer names, etc. We do
9055 this before the menu bars so that the buffer-menu will show the
9056 up-to-date frame titles. */
9057 #ifdef HAVE_WINDOW_SYSTEM
9058 if (windows_or_buffers_changed || update_mode_lines)
9059 {
9060 Lisp_Object tail, frame;
9061
9062 FOR_EACH_FRAME (tail, frame)
9063 {
9064 f = XFRAME (frame);
9065 if (!EQ (frame, tooltip_frame)
9066 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9067 x_consider_frame_title (frame);
9068 }
9069 }
9070 #endif /* HAVE_WINDOW_SYSTEM */
9071
9072 /* Update the menu bar item lists, if appropriate. This has to be
9073 done before any actual redisplay or generation of display lines. */
9074 all_windows = (update_mode_lines
9075 || buffer_shared > 1
9076 || windows_or_buffers_changed);
9077 if (all_windows)
9078 {
9079 Lisp_Object tail, frame;
9080 int count = SPECPDL_INDEX ();
9081 /* 1 means that update_menu_bar has run its hooks
9082 so any further calls to update_menu_bar shouldn't do so again. */
9083 int menu_bar_hooks_run = 0;
9084
9085 record_unwind_save_match_data ();
9086
9087 FOR_EACH_FRAME (tail, frame)
9088 {
9089 f = XFRAME (frame);
9090
9091 /* Ignore tooltip frame. */
9092 if (EQ (frame, tooltip_frame))
9093 continue;
9094
9095 /* If a window on this frame changed size, report that to
9096 the user and clear the size-change flag. */
9097 if (FRAME_WINDOW_SIZES_CHANGED (f))
9098 {
9099 Lisp_Object functions;
9100
9101 /* Clear flag first in case we get an error below. */
9102 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9103 functions = Vwindow_size_change_functions;
9104 GCPRO2 (tail, functions);
9105
9106 while (CONSP (functions))
9107 {
9108 call1 (XCAR (functions), frame);
9109 functions = XCDR (functions);
9110 }
9111 UNGCPRO;
9112 }
9113
9114 GCPRO1 (tail);
9115 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9116 #ifdef HAVE_WINDOW_SYSTEM
9117 update_tool_bar (f, 0);
9118 #ifdef MAC_OS
9119 mac_update_title_bar (f, 0);
9120 #endif
9121 #endif
9122 UNGCPRO;
9123 }
9124
9125 unbind_to (count, Qnil);
9126 }
9127 else
9128 {
9129 struct frame *sf = SELECTED_FRAME ();
9130 update_menu_bar (sf, 1, 0);
9131 #ifdef HAVE_WINDOW_SYSTEM
9132 update_tool_bar (sf, 1);
9133 #ifdef MAC_OS
9134 mac_update_title_bar (sf, 1);
9135 #endif
9136 #endif
9137 }
9138
9139 /* Motif needs this. See comment in xmenu.c. Turn it off when
9140 pending_menu_activation is not defined. */
9141 #ifdef USE_X_TOOLKIT
9142 pending_menu_activation = 0;
9143 #endif
9144 }
9145
9146
9147 /* Update the menu bar item list for frame F. This has to be done
9148 before we start to fill in any display lines, because it can call
9149 eval.
9150
9151 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9152
9153 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9154 already ran the menu bar hooks for this redisplay, so there
9155 is no need to run them again. The return value is the
9156 updated value of this flag, to pass to the next call. */
9157
9158 static int
9159 update_menu_bar (f, save_match_data, hooks_run)
9160 struct frame *f;
9161 int save_match_data;
9162 int hooks_run;
9163 {
9164 Lisp_Object window;
9165 register struct window *w;
9166
9167 /* If called recursively during a menu update, do nothing. This can
9168 happen when, for instance, an activate-menubar-hook causes a
9169 redisplay. */
9170 if (inhibit_menubar_update)
9171 return hooks_run;
9172
9173 window = FRAME_SELECTED_WINDOW (f);
9174 w = XWINDOW (window);
9175
9176 #if 0 /* The if statement below this if statement used to include the
9177 condition !NILP (w->update_mode_line), rather than using
9178 update_mode_lines directly, and this if statement may have
9179 been added to make that condition work. Now the if
9180 statement below matches its comment, this isn't needed. */
9181 if (update_mode_lines)
9182 w->update_mode_line = Qt;
9183 #endif
9184
9185 if (FRAME_WINDOW_P (f)
9186 ?
9187 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9188 || defined (USE_GTK)
9189 FRAME_EXTERNAL_MENU_BAR (f)
9190 #else
9191 FRAME_MENU_BAR_LINES (f) > 0
9192 #endif
9193 : FRAME_MENU_BAR_LINES (f) > 0)
9194 {
9195 /* If the user has switched buffers or windows, we need to
9196 recompute to reflect the new bindings. But we'll
9197 recompute when update_mode_lines is set too; that means
9198 that people can use force-mode-line-update to request
9199 that the menu bar be recomputed. The adverse effect on
9200 the rest of the redisplay algorithm is about the same as
9201 windows_or_buffers_changed anyway. */
9202 if (windows_or_buffers_changed
9203 /* This used to test w->update_mode_line, but we believe
9204 there is no need to recompute the menu in that case. */
9205 || update_mode_lines
9206 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9207 < BUF_MODIFF (XBUFFER (w->buffer)))
9208 != !NILP (w->last_had_star))
9209 || ((!NILP (Vtransient_mark_mode)
9210 && !NILP (XBUFFER (w->buffer)->mark_active))
9211 != !NILP (w->region_showing)))
9212 {
9213 struct buffer *prev = current_buffer;
9214 int count = SPECPDL_INDEX ();
9215
9216 specbind (Qinhibit_menubar_update, Qt);
9217
9218 set_buffer_internal_1 (XBUFFER (w->buffer));
9219 if (save_match_data)
9220 record_unwind_save_match_data ();
9221 if (NILP (Voverriding_local_map_menu_flag))
9222 {
9223 specbind (Qoverriding_terminal_local_map, Qnil);
9224 specbind (Qoverriding_local_map, Qnil);
9225 }
9226
9227 if (!hooks_run)
9228 {
9229 /* Run the Lucid hook. */
9230 safe_run_hooks (Qactivate_menubar_hook);
9231
9232 /* If it has changed current-menubar from previous value,
9233 really recompute the menu-bar from the value. */
9234 if (! NILP (Vlucid_menu_bar_dirty_flag))
9235 call0 (Qrecompute_lucid_menubar);
9236
9237 safe_run_hooks (Qmenu_bar_update_hook);
9238
9239 hooks_run = 1;
9240 }
9241
9242 XSETFRAME (Vmenu_updating_frame, f);
9243 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9244
9245 /* Redisplay the menu bar in case we changed it. */
9246 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9247 || defined (USE_GTK)
9248 if (FRAME_WINDOW_P (f))
9249 {
9250 #ifdef MAC_OS
9251 /* All frames on Mac OS share the same menubar. So only
9252 the selected frame should be allowed to set it. */
9253 if (f == SELECTED_FRAME ())
9254 #endif
9255 set_frame_menubar (f, 0, 0);
9256 }
9257 else
9258 /* On a terminal screen, the menu bar is an ordinary screen
9259 line, and this makes it get updated. */
9260 w->update_mode_line = Qt;
9261 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9262 /* In the non-toolkit version, the menu bar is an ordinary screen
9263 line, and this makes it get updated. */
9264 w->update_mode_line = Qt;
9265 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9266
9267 unbind_to (count, Qnil);
9268 set_buffer_internal_1 (prev);
9269 }
9270 }
9271
9272 return hooks_run;
9273 }
9274
9275
9276 \f
9277 /***********************************************************************
9278 Output Cursor
9279 ***********************************************************************/
9280
9281 #ifdef HAVE_WINDOW_SYSTEM
9282
9283 /* EXPORT:
9284 Nominal cursor position -- where to draw output.
9285 HPOS and VPOS are window relative glyph matrix coordinates.
9286 X and Y are window relative pixel coordinates. */
9287
9288 struct cursor_pos output_cursor;
9289
9290
9291 /* EXPORT:
9292 Set the global variable output_cursor to CURSOR. All cursor
9293 positions are relative to updated_window. */
9294
9295 void
9296 set_output_cursor (cursor)
9297 struct cursor_pos *cursor;
9298 {
9299 output_cursor.hpos = cursor->hpos;
9300 output_cursor.vpos = cursor->vpos;
9301 output_cursor.x = cursor->x;
9302 output_cursor.y = cursor->y;
9303 }
9304
9305
9306 /* EXPORT for RIF:
9307 Set a nominal cursor position.
9308
9309 HPOS and VPOS are column/row positions in a window glyph matrix. X
9310 and Y are window text area relative pixel positions.
9311
9312 If this is done during an update, updated_window will contain the
9313 window that is being updated and the position is the future output
9314 cursor position for that window. If updated_window is null, use
9315 selected_window and display the cursor at the given position. */
9316
9317 void
9318 x_cursor_to (vpos, hpos, y, x)
9319 int vpos, hpos, y, x;
9320 {
9321 struct window *w;
9322
9323 /* If updated_window is not set, work on selected_window. */
9324 if (updated_window)
9325 w = updated_window;
9326 else
9327 w = XWINDOW (selected_window);
9328
9329 /* Set the output cursor. */
9330 output_cursor.hpos = hpos;
9331 output_cursor.vpos = vpos;
9332 output_cursor.x = x;
9333 output_cursor.y = y;
9334
9335 /* If not called as part of an update, really display the cursor.
9336 This will also set the cursor position of W. */
9337 if (updated_window == NULL)
9338 {
9339 BLOCK_INPUT;
9340 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9341 if (rif->flush_display_optional)
9342 rif->flush_display_optional (SELECTED_FRAME ());
9343 UNBLOCK_INPUT;
9344 }
9345 }
9346
9347 #endif /* HAVE_WINDOW_SYSTEM */
9348
9349 \f
9350 /***********************************************************************
9351 Tool-bars
9352 ***********************************************************************/
9353
9354 #ifdef HAVE_WINDOW_SYSTEM
9355
9356 /* Where the mouse was last time we reported a mouse event. */
9357
9358 FRAME_PTR last_mouse_frame;
9359
9360 /* Tool-bar item index of the item on which a mouse button was pressed
9361 or -1. */
9362
9363 int last_tool_bar_item;
9364
9365
9366 /* Update the tool-bar item list for frame F. This has to be done
9367 before we start to fill in any display lines. Called from
9368 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9369 and restore it here. */
9370
9371 static void
9372 update_tool_bar (f, save_match_data)
9373 struct frame *f;
9374 int save_match_data;
9375 {
9376 #ifdef USE_GTK
9377 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9378 #else
9379 int do_update = WINDOWP (f->tool_bar_window)
9380 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9381 #endif
9382
9383 if (do_update)
9384 {
9385 Lisp_Object window;
9386 struct window *w;
9387
9388 window = FRAME_SELECTED_WINDOW (f);
9389 w = XWINDOW (window);
9390
9391 /* If the user has switched buffers or windows, we need to
9392 recompute to reflect the new bindings. But we'll
9393 recompute when update_mode_lines is set too; that means
9394 that people can use force-mode-line-update to request
9395 that the menu bar be recomputed. The adverse effect on
9396 the rest of the redisplay algorithm is about the same as
9397 windows_or_buffers_changed anyway. */
9398 if (windows_or_buffers_changed
9399 || !NILP (w->update_mode_line)
9400 || update_mode_lines
9401 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9402 < BUF_MODIFF (XBUFFER (w->buffer)))
9403 != !NILP (w->last_had_star))
9404 || ((!NILP (Vtransient_mark_mode)
9405 && !NILP (XBUFFER (w->buffer)->mark_active))
9406 != !NILP (w->region_showing)))
9407 {
9408 struct buffer *prev = current_buffer;
9409 int count = SPECPDL_INDEX ();
9410 Lisp_Object new_tool_bar;
9411 int new_n_tool_bar;
9412 struct gcpro gcpro1;
9413
9414 /* Set current_buffer to the buffer of the selected
9415 window of the frame, so that we get the right local
9416 keymaps. */
9417 set_buffer_internal_1 (XBUFFER (w->buffer));
9418
9419 /* Save match data, if we must. */
9420 if (save_match_data)
9421 record_unwind_save_match_data ();
9422
9423 /* Make sure that we don't accidentally use bogus keymaps. */
9424 if (NILP (Voverriding_local_map_menu_flag))
9425 {
9426 specbind (Qoverriding_terminal_local_map, Qnil);
9427 specbind (Qoverriding_local_map, Qnil);
9428 }
9429
9430 GCPRO1 (new_tool_bar);
9431
9432 /* Build desired tool-bar items from keymaps. */
9433 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9434 &new_n_tool_bar);
9435
9436 /* Redisplay the tool-bar if we changed it. */
9437 if (new_n_tool_bar != f->n_tool_bar_items
9438 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9439 {
9440 /* Redisplay that happens asynchronously due to an expose event
9441 may access f->tool_bar_items. Make sure we update both
9442 variables within BLOCK_INPUT so no such event interrupts. */
9443 BLOCK_INPUT;
9444 f->tool_bar_items = new_tool_bar;
9445 f->n_tool_bar_items = new_n_tool_bar;
9446 w->update_mode_line = Qt;
9447 UNBLOCK_INPUT;
9448 }
9449
9450 UNGCPRO;
9451
9452 unbind_to (count, Qnil);
9453 set_buffer_internal_1 (prev);
9454 }
9455 }
9456 }
9457
9458
9459 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9460 F's desired tool-bar contents. F->tool_bar_items must have
9461 been set up previously by calling prepare_menu_bars. */
9462
9463 static void
9464 build_desired_tool_bar_string (f)
9465 struct frame *f;
9466 {
9467 int i, size, size_needed;
9468 struct gcpro gcpro1, gcpro2, gcpro3;
9469 Lisp_Object image, plist, props;
9470
9471 image = plist = props = Qnil;
9472 GCPRO3 (image, plist, props);
9473
9474 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9475 Otherwise, make a new string. */
9476
9477 /* The size of the string we might be able to reuse. */
9478 size = (STRINGP (f->desired_tool_bar_string)
9479 ? SCHARS (f->desired_tool_bar_string)
9480 : 0);
9481
9482 /* We need one space in the string for each image. */
9483 size_needed = f->n_tool_bar_items;
9484
9485 /* Reuse f->desired_tool_bar_string, if possible. */
9486 if (size < size_needed || NILP (f->desired_tool_bar_string))
9487 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9488 make_number (' '));
9489 else
9490 {
9491 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9492 Fremove_text_properties (make_number (0), make_number (size),
9493 props, f->desired_tool_bar_string);
9494 }
9495
9496 /* Put a `display' property on the string for the images to display,
9497 put a `menu_item' property on tool-bar items with a value that
9498 is the index of the item in F's tool-bar item vector. */
9499 for (i = 0; i < f->n_tool_bar_items; ++i)
9500 {
9501 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9502
9503 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9504 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9505 int hmargin, vmargin, relief, idx, end;
9506 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9507
9508 /* If image is a vector, choose the image according to the
9509 button state. */
9510 image = PROP (TOOL_BAR_ITEM_IMAGES);
9511 if (VECTORP (image))
9512 {
9513 if (enabled_p)
9514 idx = (selected_p
9515 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9516 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9517 else
9518 idx = (selected_p
9519 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9520 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9521
9522 xassert (ASIZE (image) >= idx);
9523 image = AREF (image, idx);
9524 }
9525 else
9526 idx = -1;
9527
9528 /* Ignore invalid image specifications. */
9529 if (!valid_image_p (image))
9530 continue;
9531
9532 /* Display the tool-bar button pressed, or depressed. */
9533 plist = Fcopy_sequence (XCDR (image));
9534
9535 /* Compute margin and relief to draw. */
9536 relief = (tool_bar_button_relief >= 0
9537 ? tool_bar_button_relief
9538 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9539 hmargin = vmargin = relief;
9540
9541 if (INTEGERP (Vtool_bar_button_margin)
9542 && XINT (Vtool_bar_button_margin) > 0)
9543 {
9544 hmargin += XFASTINT (Vtool_bar_button_margin);
9545 vmargin += XFASTINT (Vtool_bar_button_margin);
9546 }
9547 else if (CONSP (Vtool_bar_button_margin))
9548 {
9549 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9550 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9551 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9552
9553 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9554 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9555 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9556 }
9557
9558 if (auto_raise_tool_bar_buttons_p)
9559 {
9560 /* Add a `:relief' property to the image spec if the item is
9561 selected. */
9562 if (selected_p)
9563 {
9564 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9565 hmargin -= relief;
9566 vmargin -= relief;
9567 }
9568 }
9569 else
9570 {
9571 /* If image is selected, display it pressed, i.e. with a
9572 negative relief. If it's not selected, display it with a
9573 raised relief. */
9574 plist = Fplist_put (plist, QCrelief,
9575 (selected_p
9576 ? make_number (-relief)
9577 : make_number (relief)));
9578 hmargin -= relief;
9579 vmargin -= relief;
9580 }
9581
9582 /* Put a margin around the image. */
9583 if (hmargin || vmargin)
9584 {
9585 if (hmargin == vmargin)
9586 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9587 else
9588 plist = Fplist_put (plist, QCmargin,
9589 Fcons (make_number (hmargin),
9590 make_number (vmargin)));
9591 }
9592
9593 /* If button is not enabled, and we don't have special images
9594 for the disabled state, make the image appear disabled by
9595 applying an appropriate algorithm to it. */
9596 if (!enabled_p && idx < 0)
9597 plist = Fplist_put (plist, QCconversion, Qdisabled);
9598
9599 /* Put a `display' text property on the string for the image to
9600 display. Put a `menu-item' property on the string that gives
9601 the start of this item's properties in the tool-bar items
9602 vector. */
9603 image = Fcons (Qimage, plist);
9604 props = list4 (Qdisplay, image,
9605 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9606
9607 /* Let the last image hide all remaining spaces in the tool bar
9608 string. The string can be longer than needed when we reuse a
9609 previous string. */
9610 if (i + 1 == f->n_tool_bar_items)
9611 end = SCHARS (f->desired_tool_bar_string);
9612 else
9613 end = i + 1;
9614 Fadd_text_properties (make_number (i), make_number (end),
9615 props, f->desired_tool_bar_string);
9616 #undef PROP
9617 }
9618
9619 UNGCPRO;
9620 }
9621
9622
9623 /* Display one line of the tool-bar of frame IT->f.
9624
9625 HEIGHT specifies the desired height of the tool-bar line.
9626 If the actual height of the glyph row is less than HEIGHT, the
9627 row's height is increased to HEIGHT, and the icons are centered
9628 vertically in the new height.
9629
9630 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9631 count a final empty row in case the tool-bar width exactly matches
9632 the window width.
9633 */
9634
9635 static void
9636 display_tool_bar_line (it, height)
9637 struct it *it;
9638 int height;
9639 {
9640 struct glyph_row *row = it->glyph_row;
9641 int max_x = it->last_visible_x;
9642 struct glyph *last;
9643
9644 prepare_desired_row (row);
9645 row->y = it->current_y;
9646
9647 /* Note that this isn't made use of if the face hasn't a box,
9648 so there's no need to check the face here. */
9649 it->start_of_box_run_p = 1;
9650
9651 while (it->current_x < max_x)
9652 {
9653 int x, n_glyphs_before, i, nglyphs;
9654 struct it it_before;
9655
9656 /* Get the next display element. */
9657 if (!get_next_display_element (it))
9658 {
9659 /* Don't count empty row if we are counting needed tool-bar lines. */
9660 if (height < 0 && !it->hpos)
9661 return;
9662 break;
9663 }
9664
9665 /* Produce glyphs. */
9666 n_glyphs_before = row->used[TEXT_AREA];
9667 it_before = *it;
9668
9669 PRODUCE_GLYPHS (it);
9670
9671 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9672 i = 0;
9673 x = it_before.current_x;
9674 while (i < nglyphs)
9675 {
9676 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9677
9678 if (x + glyph->pixel_width > max_x)
9679 {
9680 /* Glyph doesn't fit on line. Backtrack. */
9681 row->used[TEXT_AREA] = n_glyphs_before;
9682 *it = it_before;
9683 /* If this is the only glyph on this line, it will never fit on the
9684 toolbar, so skip it. But ensure there is at least one glyph,
9685 so we don't accidentally disable the tool-bar. */
9686 if (n_glyphs_before == 0
9687 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9688 break;
9689 goto out;
9690 }
9691
9692 ++it->hpos;
9693 x += glyph->pixel_width;
9694 ++i;
9695 }
9696
9697 /* Stop at line ends. */
9698 if (ITERATOR_AT_END_OF_LINE_P (it))
9699 break;
9700
9701 set_iterator_to_next (it, 1);
9702 }
9703
9704 out:;
9705
9706 row->displays_text_p = row->used[TEXT_AREA] != 0;
9707
9708 /* Use default face for the border below the tool bar.
9709
9710 FIXME: When auto-resize-tool-bars is grow-only, there is
9711 no additional border below the possibly empty tool-bar lines.
9712 So to make the extra empty lines look "normal", we have to
9713 use the tool-bar face for the border too. */
9714 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9715 it->face_id = DEFAULT_FACE_ID;
9716
9717 extend_face_to_end_of_line (it);
9718 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9719 last->right_box_line_p = 1;
9720 if (last == row->glyphs[TEXT_AREA])
9721 last->left_box_line_p = 1;
9722
9723 /* Make line the desired height and center it vertically. */
9724 if ((height -= it->max_ascent + it->max_descent) > 0)
9725 {
9726 /* Don't add more than one line height. */
9727 height %= FRAME_LINE_HEIGHT (it->f);
9728 it->max_ascent += height / 2;
9729 it->max_descent += (height + 1) / 2;
9730 }
9731
9732 compute_line_metrics (it);
9733
9734 /* If line is empty, make it occupy the rest of the tool-bar. */
9735 if (!row->displays_text_p)
9736 {
9737 row->height = row->phys_height = it->last_visible_y - row->y;
9738 row->visible_height = row->height;
9739 row->ascent = row->phys_ascent = 0;
9740 row->extra_line_spacing = 0;
9741 }
9742
9743 row->full_width_p = 1;
9744 row->continued_p = 0;
9745 row->truncated_on_left_p = 0;
9746 row->truncated_on_right_p = 0;
9747
9748 it->current_x = it->hpos = 0;
9749 it->current_y += row->height;
9750 ++it->vpos;
9751 ++it->glyph_row;
9752 }
9753
9754
9755 /* Max tool-bar height. */
9756
9757 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9758 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9759
9760 /* Value is the number of screen lines needed to make all tool-bar
9761 items of frame F visible. The number of actual rows needed is
9762 returned in *N_ROWS if non-NULL. */
9763
9764 static int
9765 tool_bar_lines_needed (f, n_rows)
9766 struct frame *f;
9767 int *n_rows;
9768 {
9769 struct window *w = XWINDOW (f->tool_bar_window);
9770 struct it it;
9771 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9772 the desired matrix, so use (unused) mode-line row as temporary row to
9773 avoid destroying the first tool-bar row. */
9774 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9775
9776 /* Initialize an iterator for iteration over
9777 F->desired_tool_bar_string in the tool-bar window of frame F. */
9778 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9779 it.first_visible_x = 0;
9780 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9781 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9782
9783 while (!ITERATOR_AT_END_P (&it))
9784 {
9785 clear_glyph_row (temp_row);
9786 it.glyph_row = temp_row;
9787 display_tool_bar_line (&it, -1);
9788 }
9789 clear_glyph_row (temp_row);
9790
9791 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9792 if (n_rows)
9793 *n_rows = it.vpos > 0 ? it.vpos : -1;
9794
9795 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9796 }
9797
9798
9799 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9800 0, 1, 0,
9801 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9802 (frame)
9803 Lisp_Object frame;
9804 {
9805 struct frame *f;
9806 struct window *w;
9807 int nlines = 0;
9808
9809 if (NILP (frame))
9810 frame = selected_frame;
9811 else
9812 CHECK_FRAME (frame);
9813 f = XFRAME (frame);
9814
9815 if (WINDOWP (f->tool_bar_window)
9816 || (w = XWINDOW (f->tool_bar_window),
9817 WINDOW_TOTAL_LINES (w) > 0))
9818 {
9819 update_tool_bar (f, 1);
9820 if (f->n_tool_bar_items)
9821 {
9822 build_desired_tool_bar_string (f);
9823 nlines = tool_bar_lines_needed (f, NULL);
9824 }
9825 }
9826
9827 return make_number (nlines);
9828 }
9829
9830
9831 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9832 height should be changed. */
9833
9834 static int
9835 redisplay_tool_bar (f)
9836 struct frame *f;
9837 {
9838 struct window *w;
9839 struct it it;
9840 struct glyph_row *row;
9841
9842 #ifdef USE_GTK
9843 if (FRAME_EXTERNAL_TOOL_BAR (f))
9844 update_frame_tool_bar (f);
9845 return 0;
9846 #endif
9847
9848 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9849 do anything. This means you must start with tool-bar-lines
9850 non-zero to get the auto-sizing effect. Or in other words, you
9851 can turn off tool-bars by specifying tool-bar-lines zero. */
9852 if (!WINDOWP (f->tool_bar_window)
9853 || (w = XWINDOW (f->tool_bar_window),
9854 WINDOW_TOTAL_LINES (w) == 0))
9855 return 0;
9856
9857 /* Set up an iterator for the tool-bar window. */
9858 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9859 it.first_visible_x = 0;
9860 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9861 row = it.glyph_row;
9862
9863 /* Build a string that represents the contents of the tool-bar. */
9864 build_desired_tool_bar_string (f);
9865 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9866
9867 if (f->n_tool_bar_rows == 0)
9868 {
9869 int nlines;
9870
9871 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9872 nlines != WINDOW_TOTAL_LINES (w)))
9873 {
9874 extern Lisp_Object Qtool_bar_lines;
9875 Lisp_Object frame;
9876 int old_height = WINDOW_TOTAL_LINES (w);
9877
9878 XSETFRAME (frame, f);
9879 Fmodify_frame_parameters (frame,
9880 Fcons (Fcons (Qtool_bar_lines,
9881 make_number (nlines)),
9882 Qnil));
9883 if (WINDOW_TOTAL_LINES (w) != old_height)
9884 {
9885 clear_glyph_matrix (w->desired_matrix);
9886 fonts_changed_p = 1;
9887 return 1;
9888 }
9889 }
9890 }
9891
9892 /* Display as many lines as needed to display all tool-bar items. */
9893
9894 if (f->n_tool_bar_rows > 0)
9895 {
9896 int border, rows, height, extra;
9897
9898 if (INTEGERP (Vtool_bar_border))
9899 border = XINT (Vtool_bar_border);
9900 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9901 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9902 else if (EQ (Vtool_bar_border, Qborder_width))
9903 border = f->border_width;
9904 else
9905 border = 0;
9906 if (border < 0)
9907 border = 0;
9908
9909 rows = f->n_tool_bar_rows;
9910 height = max (1, (it.last_visible_y - border) / rows);
9911 extra = it.last_visible_y - border - height * rows;
9912
9913 while (it.current_y < it.last_visible_y)
9914 {
9915 int h = 0;
9916 if (extra > 0 && rows-- > 0)
9917 {
9918 h = (extra + rows - 1) / rows;
9919 extra -= h;
9920 }
9921 display_tool_bar_line (&it, height + h);
9922 }
9923 }
9924 else
9925 {
9926 while (it.current_y < it.last_visible_y)
9927 display_tool_bar_line (&it, 0);
9928 }
9929
9930 /* It doesn't make much sense to try scrolling in the tool-bar
9931 window, so don't do it. */
9932 w->desired_matrix->no_scrolling_p = 1;
9933 w->must_be_updated_p = 1;
9934
9935 if (!NILP (Vauto_resize_tool_bars))
9936 {
9937 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9938 int change_height_p = 0;
9939
9940 /* If we couldn't display everything, change the tool-bar's
9941 height if there is room for more. */
9942 if (IT_STRING_CHARPOS (it) < it.end_charpos
9943 && it.current_y < max_tool_bar_height)
9944 change_height_p = 1;
9945
9946 row = it.glyph_row - 1;
9947
9948 /* If there are blank lines at the end, except for a partially
9949 visible blank line at the end that is smaller than
9950 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9951 if (!row->displays_text_p
9952 && row->height >= FRAME_LINE_HEIGHT (f))
9953 change_height_p = 1;
9954
9955 /* If row displays tool-bar items, but is partially visible,
9956 change the tool-bar's height. */
9957 if (row->displays_text_p
9958 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9959 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9960 change_height_p = 1;
9961
9962 /* Resize windows as needed by changing the `tool-bar-lines'
9963 frame parameter. */
9964 if (change_height_p)
9965 {
9966 extern Lisp_Object Qtool_bar_lines;
9967 Lisp_Object frame;
9968 int old_height = WINDOW_TOTAL_LINES (w);
9969 int nrows;
9970 int nlines = tool_bar_lines_needed (f, &nrows);
9971
9972 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
9973 && !f->minimize_tool_bar_window_p)
9974 ? (nlines > old_height)
9975 : (nlines != old_height));
9976 f->minimize_tool_bar_window_p = 0;
9977
9978 if (change_height_p)
9979 {
9980 XSETFRAME (frame, f);
9981 Fmodify_frame_parameters (frame,
9982 Fcons (Fcons (Qtool_bar_lines,
9983 make_number (nlines)),
9984 Qnil));
9985 if (WINDOW_TOTAL_LINES (w) != old_height)
9986 {
9987 clear_glyph_matrix (w->desired_matrix);
9988 f->n_tool_bar_rows = nrows;
9989 fonts_changed_p = 1;
9990 return 1;
9991 }
9992 }
9993 }
9994 }
9995
9996 f->minimize_tool_bar_window_p = 0;
9997 return 0;
9998 }
9999
10000
10001 /* Get information about the tool-bar item which is displayed in GLYPH
10002 on frame F. Return in *PROP_IDX the index where tool-bar item
10003 properties start in F->tool_bar_items. Value is zero if
10004 GLYPH doesn't display a tool-bar item. */
10005
10006 static int
10007 tool_bar_item_info (f, glyph, prop_idx)
10008 struct frame *f;
10009 struct glyph *glyph;
10010 int *prop_idx;
10011 {
10012 Lisp_Object prop;
10013 int success_p;
10014 int charpos;
10015
10016 /* This function can be called asynchronously, which means we must
10017 exclude any possibility that Fget_text_property signals an
10018 error. */
10019 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10020 charpos = max (0, charpos);
10021
10022 /* Get the text property `menu-item' at pos. The value of that
10023 property is the start index of this item's properties in
10024 F->tool_bar_items. */
10025 prop = Fget_text_property (make_number (charpos),
10026 Qmenu_item, f->current_tool_bar_string);
10027 if (INTEGERP (prop))
10028 {
10029 *prop_idx = XINT (prop);
10030 success_p = 1;
10031 }
10032 else
10033 success_p = 0;
10034
10035 return success_p;
10036 }
10037
10038 \f
10039 /* Get information about the tool-bar item at position X/Y on frame F.
10040 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10041 the current matrix of the tool-bar window of F, or NULL if not
10042 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10043 item in F->tool_bar_items. Value is
10044
10045 -1 if X/Y is not on a tool-bar item
10046 0 if X/Y is on the same item that was highlighted before.
10047 1 otherwise. */
10048
10049 static int
10050 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10051 struct frame *f;
10052 int x, y;
10053 struct glyph **glyph;
10054 int *hpos, *vpos, *prop_idx;
10055 {
10056 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10057 struct window *w = XWINDOW (f->tool_bar_window);
10058 int area;
10059
10060 /* Find the glyph under X/Y. */
10061 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10062 if (*glyph == NULL)
10063 return -1;
10064
10065 /* Get the start of this tool-bar item's properties in
10066 f->tool_bar_items. */
10067 if (!tool_bar_item_info (f, *glyph, prop_idx))
10068 return -1;
10069
10070 /* Is mouse on the highlighted item? */
10071 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10072 && *vpos >= dpyinfo->mouse_face_beg_row
10073 && *vpos <= dpyinfo->mouse_face_end_row
10074 && (*vpos > dpyinfo->mouse_face_beg_row
10075 || *hpos >= dpyinfo->mouse_face_beg_col)
10076 && (*vpos < dpyinfo->mouse_face_end_row
10077 || *hpos < dpyinfo->mouse_face_end_col
10078 || dpyinfo->mouse_face_past_end))
10079 return 0;
10080
10081 return 1;
10082 }
10083
10084
10085 /* EXPORT:
10086 Handle mouse button event on the tool-bar of frame F, at
10087 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10088 0 for button release. MODIFIERS is event modifiers for button
10089 release. */
10090
10091 void
10092 handle_tool_bar_click (f, x, y, down_p, modifiers)
10093 struct frame *f;
10094 int x, y, down_p;
10095 unsigned int modifiers;
10096 {
10097 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10098 struct window *w = XWINDOW (f->tool_bar_window);
10099 int hpos, vpos, prop_idx;
10100 struct glyph *glyph;
10101 Lisp_Object enabled_p;
10102
10103 /* If not on the highlighted tool-bar item, return. */
10104 frame_to_window_pixel_xy (w, &x, &y);
10105 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10106 return;
10107
10108 /* If item is disabled, do nothing. */
10109 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10110 if (NILP (enabled_p))
10111 return;
10112
10113 if (down_p)
10114 {
10115 /* Show item in pressed state. */
10116 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10117 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10118 last_tool_bar_item = prop_idx;
10119 }
10120 else
10121 {
10122 Lisp_Object key, frame;
10123 struct input_event event;
10124 EVENT_INIT (event);
10125
10126 /* Show item in released state. */
10127 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10128 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10129
10130 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10131
10132 XSETFRAME (frame, f);
10133 event.kind = TOOL_BAR_EVENT;
10134 event.frame_or_window = frame;
10135 event.arg = frame;
10136 kbd_buffer_store_event (&event);
10137
10138 event.kind = TOOL_BAR_EVENT;
10139 event.frame_or_window = frame;
10140 event.arg = key;
10141 event.modifiers = modifiers;
10142 kbd_buffer_store_event (&event);
10143 last_tool_bar_item = -1;
10144 }
10145 }
10146
10147
10148 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10149 tool-bar window-relative coordinates X/Y. Called from
10150 note_mouse_highlight. */
10151
10152 static void
10153 note_tool_bar_highlight (f, x, y)
10154 struct frame *f;
10155 int x, y;
10156 {
10157 Lisp_Object window = f->tool_bar_window;
10158 struct window *w = XWINDOW (window);
10159 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10160 int hpos, vpos;
10161 struct glyph *glyph;
10162 struct glyph_row *row;
10163 int i;
10164 Lisp_Object enabled_p;
10165 int prop_idx;
10166 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10167 int mouse_down_p, rc;
10168
10169 /* Function note_mouse_highlight is called with negative x(y
10170 values when mouse moves outside of the frame. */
10171 if (x <= 0 || y <= 0)
10172 {
10173 clear_mouse_face (dpyinfo);
10174 return;
10175 }
10176
10177 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10178 if (rc < 0)
10179 {
10180 /* Not on tool-bar item. */
10181 clear_mouse_face (dpyinfo);
10182 return;
10183 }
10184 else if (rc == 0)
10185 /* On same tool-bar item as before. */
10186 goto set_help_echo;
10187
10188 clear_mouse_face (dpyinfo);
10189
10190 /* Mouse is down, but on different tool-bar item? */
10191 mouse_down_p = (dpyinfo->grabbed
10192 && f == last_mouse_frame
10193 && FRAME_LIVE_P (f));
10194 if (mouse_down_p
10195 && last_tool_bar_item != prop_idx)
10196 return;
10197
10198 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10199 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10200
10201 /* If tool-bar item is not enabled, don't highlight it. */
10202 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10203 if (!NILP (enabled_p))
10204 {
10205 /* Compute the x-position of the glyph. In front and past the
10206 image is a space. We include this in the highlighted area. */
10207 row = MATRIX_ROW (w->current_matrix, vpos);
10208 for (i = x = 0; i < hpos; ++i)
10209 x += row->glyphs[TEXT_AREA][i].pixel_width;
10210
10211 /* Record this as the current active region. */
10212 dpyinfo->mouse_face_beg_col = hpos;
10213 dpyinfo->mouse_face_beg_row = vpos;
10214 dpyinfo->mouse_face_beg_x = x;
10215 dpyinfo->mouse_face_beg_y = row->y;
10216 dpyinfo->mouse_face_past_end = 0;
10217
10218 dpyinfo->mouse_face_end_col = hpos + 1;
10219 dpyinfo->mouse_face_end_row = vpos;
10220 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10221 dpyinfo->mouse_face_end_y = row->y;
10222 dpyinfo->mouse_face_window = window;
10223 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10224
10225 /* Display it as active. */
10226 show_mouse_face (dpyinfo, draw);
10227 dpyinfo->mouse_face_image_state = draw;
10228 }
10229
10230 set_help_echo:
10231
10232 /* Set help_echo_string to a help string to display for this tool-bar item.
10233 XTread_socket does the rest. */
10234 help_echo_object = help_echo_window = Qnil;
10235 help_echo_pos = -1;
10236 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10237 if (NILP (help_echo_string))
10238 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10239 }
10240
10241 #endif /* HAVE_WINDOW_SYSTEM */
10242
10243
10244 \f
10245 /************************************************************************
10246 Horizontal scrolling
10247 ************************************************************************/
10248
10249 static int hscroll_window_tree P_ ((Lisp_Object));
10250 static int hscroll_windows P_ ((Lisp_Object));
10251
10252 /* For all leaf windows in the window tree rooted at WINDOW, set their
10253 hscroll value so that PT is (i) visible in the window, and (ii) so
10254 that it is not within a certain margin at the window's left and
10255 right border. Value is non-zero if any window's hscroll has been
10256 changed. */
10257
10258 static int
10259 hscroll_window_tree (window)
10260 Lisp_Object window;
10261 {
10262 int hscrolled_p = 0;
10263 int hscroll_relative_p = FLOATP (Vhscroll_step);
10264 int hscroll_step_abs = 0;
10265 double hscroll_step_rel = 0;
10266
10267 if (hscroll_relative_p)
10268 {
10269 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10270 if (hscroll_step_rel < 0)
10271 {
10272 hscroll_relative_p = 0;
10273 hscroll_step_abs = 0;
10274 }
10275 }
10276 else if (INTEGERP (Vhscroll_step))
10277 {
10278 hscroll_step_abs = XINT (Vhscroll_step);
10279 if (hscroll_step_abs < 0)
10280 hscroll_step_abs = 0;
10281 }
10282 else
10283 hscroll_step_abs = 0;
10284
10285 while (WINDOWP (window))
10286 {
10287 struct window *w = XWINDOW (window);
10288
10289 if (WINDOWP (w->hchild))
10290 hscrolled_p |= hscroll_window_tree (w->hchild);
10291 else if (WINDOWP (w->vchild))
10292 hscrolled_p |= hscroll_window_tree (w->vchild);
10293 else if (w->cursor.vpos >= 0)
10294 {
10295 int h_margin;
10296 int text_area_width;
10297 struct glyph_row *current_cursor_row
10298 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10299 struct glyph_row *desired_cursor_row
10300 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10301 struct glyph_row *cursor_row
10302 = (desired_cursor_row->enabled_p
10303 ? desired_cursor_row
10304 : current_cursor_row);
10305
10306 text_area_width = window_box_width (w, TEXT_AREA);
10307
10308 /* Scroll when cursor is inside this scroll margin. */
10309 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10310
10311 if ((XFASTINT (w->hscroll)
10312 && w->cursor.x <= h_margin)
10313 || (cursor_row->enabled_p
10314 && cursor_row->truncated_on_right_p
10315 && (w->cursor.x >= text_area_width - h_margin)))
10316 {
10317 struct it it;
10318 int hscroll;
10319 struct buffer *saved_current_buffer;
10320 int pt;
10321 int wanted_x;
10322
10323 /* Find point in a display of infinite width. */
10324 saved_current_buffer = current_buffer;
10325 current_buffer = XBUFFER (w->buffer);
10326
10327 if (w == XWINDOW (selected_window))
10328 pt = BUF_PT (current_buffer);
10329 else
10330 {
10331 pt = marker_position (w->pointm);
10332 pt = max (BEGV, pt);
10333 pt = min (ZV, pt);
10334 }
10335
10336 /* Move iterator to pt starting at cursor_row->start in
10337 a line with infinite width. */
10338 init_to_row_start (&it, w, cursor_row);
10339 it.last_visible_x = INFINITY;
10340 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10341 current_buffer = saved_current_buffer;
10342
10343 /* Position cursor in window. */
10344 if (!hscroll_relative_p && hscroll_step_abs == 0)
10345 hscroll = max (0, (it.current_x
10346 - (ITERATOR_AT_END_OF_LINE_P (&it)
10347 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10348 : (text_area_width / 2))))
10349 / FRAME_COLUMN_WIDTH (it.f);
10350 else if (w->cursor.x >= text_area_width - h_margin)
10351 {
10352 if (hscroll_relative_p)
10353 wanted_x = text_area_width * (1 - hscroll_step_rel)
10354 - h_margin;
10355 else
10356 wanted_x = text_area_width
10357 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10358 - h_margin;
10359 hscroll
10360 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10361 }
10362 else
10363 {
10364 if (hscroll_relative_p)
10365 wanted_x = text_area_width * hscroll_step_rel
10366 + h_margin;
10367 else
10368 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10369 + h_margin;
10370 hscroll
10371 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10372 }
10373 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10374
10375 /* Don't call Fset_window_hscroll if value hasn't
10376 changed because it will prevent redisplay
10377 optimizations. */
10378 if (XFASTINT (w->hscroll) != hscroll)
10379 {
10380 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10381 w->hscroll = make_number (hscroll);
10382 hscrolled_p = 1;
10383 }
10384 }
10385 }
10386
10387 window = w->next;
10388 }
10389
10390 /* Value is non-zero if hscroll of any leaf window has been changed. */
10391 return hscrolled_p;
10392 }
10393
10394
10395 /* Set hscroll so that cursor is visible and not inside horizontal
10396 scroll margins for all windows in the tree rooted at WINDOW. See
10397 also hscroll_window_tree above. Value is non-zero if any window's
10398 hscroll has been changed. If it has, desired matrices on the frame
10399 of WINDOW are cleared. */
10400
10401 static int
10402 hscroll_windows (window)
10403 Lisp_Object window;
10404 {
10405 int hscrolled_p;
10406
10407 if (automatic_hscrolling_p)
10408 {
10409 hscrolled_p = hscroll_window_tree (window);
10410 if (hscrolled_p)
10411 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10412 }
10413 else
10414 hscrolled_p = 0;
10415 return hscrolled_p;
10416 }
10417
10418
10419 \f
10420 /************************************************************************
10421 Redisplay
10422 ************************************************************************/
10423
10424 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10425 to a non-zero value. This is sometimes handy to have in a debugger
10426 session. */
10427
10428 #if GLYPH_DEBUG
10429
10430 /* First and last unchanged row for try_window_id. */
10431
10432 int debug_first_unchanged_at_end_vpos;
10433 int debug_last_unchanged_at_beg_vpos;
10434
10435 /* Delta vpos and y. */
10436
10437 int debug_dvpos, debug_dy;
10438
10439 /* Delta in characters and bytes for try_window_id. */
10440
10441 int debug_delta, debug_delta_bytes;
10442
10443 /* Values of window_end_pos and window_end_vpos at the end of
10444 try_window_id. */
10445
10446 EMACS_INT debug_end_pos, debug_end_vpos;
10447
10448 /* Append a string to W->desired_matrix->method. FMT is a printf
10449 format string. A1...A9 are a supplement for a variable-length
10450 argument list. If trace_redisplay_p is non-zero also printf the
10451 resulting string to stderr. */
10452
10453 static void
10454 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10455 struct window *w;
10456 char *fmt;
10457 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10458 {
10459 char buffer[512];
10460 char *method = w->desired_matrix->method;
10461 int len = strlen (method);
10462 int size = sizeof w->desired_matrix->method;
10463 int remaining = size - len - 1;
10464
10465 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10466 if (len && remaining)
10467 {
10468 method[len] = '|';
10469 --remaining, ++len;
10470 }
10471
10472 strncpy (method + len, buffer, remaining);
10473
10474 if (trace_redisplay_p)
10475 fprintf (stderr, "%p (%s): %s\n",
10476 w,
10477 ((BUFFERP (w->buffer)
10478 && STRINGP (XBUFFER (w->buffer)->name))
10479 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10480 : "no buffer"),
10481 buffer);
10482 }
10483
10484 #endif /* GLYPH_DEBUG */
10485
10486
10487 /* Value is non-zero if all changes in window W, which displays
10488 current_buffer, are in the text between START and END. START is a
10489 buffer position, END is given as a distance from Z. Used in
10490 redisplay_internal for display optimization. */
10491
10492 static INLINE int
10493 text_outside_line_unchanged_p (w, start, end)
10494 struct window *w;
10495 int start, end;
10496 {
10497 int unchanged_p = 1;
10498
10499 /* If text or overlays have changed, see where. */
10500 if (XFASTINT (w->last_modified) < MODIFF
10501 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10502 {
10503 /* Gap in the line? */
10504 if (GPT < start || Z - GPT < end)
10505 unchanged_p = 0;
10506
10507 /* Changes start in front of the line, or end after it? */
10508 if (unchanged_p
10509 && (BEG_UNCHANGED < start - 1
10510 || END_UNCHANGED < end))
10511 unchanged_p = 0;
10512
10513 /* If selective display, can't optimize if changes start at the
10514 beginning of the line. */
10515 if (unchanged_p
10516 && INTEGERP (current_buffer->selective_display)
10517 && XINT (current_buffer->selective_display) > 0
10518 && (BEG_UNCHANGED < start || GPT <= start))
10519 unchanged_p = 0;
10520
10521 /* If there are overlays at the start or end of the line, these
10522 may have overlay strings with newlines in them. A change at
10523 START, for instance, may actually concern the display of such
10524 overlay strings as well, and they are displayed on different
10525 lines. So, quickly rule out this case. (For the future, it
10526 might be desirable to implement something more telling than
10527 just BEG/END_UNCHANGED.) */
10528 if (unchanged_p)
10529 {
10530 if (BEG + BEG_UNCHANGED == start
10531 && overlay_touches_p (start))
10532 unchanged_p = 0;
10533 if (END_UNCHANGED == end
10534 && overlay_touches_p (Z - end))
10535 unchanged_p = 0;
10536 }
10537 }
10538
10539 return unchanged_p;
10540 }
10541
10542
10543 /* Do a frame update, taking possible shortcuts into account. This is
10544 the main external entry point for redisplay.
10545
10546 If the last redisplay displayed an echo area message and that message
10547 is no longer requested, we clear the echo area or bring back the
10548 mini-buffer if that is in use. */
10549
10550 void
10551 redisplay ()
10552 {
10553 redisplay_internal (0);
10554 }
10555
10556
10557 static Lisp_Object
10558 overlay_arrow_string_or_property (var)
10559 Lisp_Object var;
10560 {
10561 Lisp_Object val;
10562
10563 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10564 return val;
10565
10566 return Voverlay_arrow_string;
10567 }
10568
10569 /* Return 1 if there are any overlay-arrows in current_buffer. */
10570 static int
10571 overlay_arrow_in_current_buffer_p ()
10572 {
10573 Lisp_Object vlist;
10574
10575 for (vlist = Voverlay_arrow_variable_list;
10576 CONSP (vlist);
10577 vlist = XCDR (vlist))
10578 {
10579 Lisp_Object var = XCAR (vlist);
10580 Lisp_Object val;
10581
10582 if (!SYMBOLP (var))
10583 continue;
10584 val = find_symbol_value (var);
10585 if (MARKERP (val)
10586 && current_buffer == XMARKER (val)->buffer)
10587 return 1;
10588 }
10589 return 0;
10590 }
10591
10592
10593 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10594 has changed. */
10595
10596 static int
10597 overlay_arrows_changed_p ()
10598 {
10599 Lisp_Object vlist;
10600
10601 for (vlist = Voverlay_arrow_variable_list;
10602 CONSP (vlist);
10603 vlist = XCDR (vlist))
10604 {
10605 Lisp_Object var = XCAR (vlist);
10606 Lisp_Object val, pstr;
10607
10608 if (!SYMBOLP (var))
10609 continue;
10610 val = find_symbol_value (var);
10611 if (!MARKERP (val))
10612 continue;
10613 if (! EQ (COERCE_MARKER (val),
10614 Fget (var, Qlast_arrow_position))
10615 || ! (pstr = overlay_arrow_string_or_property (var),
10616 EQ (pstr, Fget (var, Qlast_arrow_string))))
10617 return 1;
10618 }
10619 return 0;
10620 }
10621
10622 /* Mark overlay arrows to be updated on next redisplay. */
10623
10624 static void
10625 update_overlay_arrows (up_to_date)
10626 int up_to_date;
10627 {
10628 Lisp_Object vlist;
10629
10630 for (vlist = Voverlay_arrow_variable_list;
10631 CONSP (vlist);
10632 vlist = XCDR (vlist))
10633 {
10634 Lisp_Object var = XCAR (vlist);
10635
10636 if (!SYMBOLP (var))
10637 continue;
10638
10639 if (up_to_date > 0)
10640 {
10641 Lisp_Object val = find_symbol_value (var);
10642 Fput (var, Qlast_arrow_position,
10643 COERCE_MARKER (val));
10644 Fput (var, Qlast_arrow_string,
10645 overlay_arrow_string_or_property (var));
10646 }
10647 else if (up_to_date < 0
10648 || !NILP (Fget (var, Qlast_arrow_position)))
10649 {
10650 Fput (var, Qlast_arrow_position, Qt);
10651 Fput (var, Qlast_arrow_string, Qt);
10652 }
10653 }
10654 }
10655
10656
10657 /* Return overlay arrow string to display at row.
10658 Return integer (bitmap number) for arrow bitmap in left fringe.
10659 Return nil if no overlay arrow. */
10660
10661 static Lisp_Object
10662 overlay_arrow_at_row (it, row)
10663 struct it *it;
10664 struct glyph_row *row;
10665 {
10666 Lisp_Object vlist;
10667
10668 for (vlist = Voverlay_arrow_variable_list;
10669 CONSP (vlist);
10670 vlist = XCDR (vlist))
10671 {
10672 Lisp_Object var = XCAR (vlist);
10673 Lisp_Object val;
10674
10675 if (!SYMBOLP (var))
10676 continue;
10677
10678 val = find_symbol_value (var);
10679
10680 if (MARKERP (val)
10681 && current_buffer == XMARKER (val)->buffer
10682 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10683 {
10684 if (FRAME_WINDOW_P (it->f)
10685 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10686 {
10687 #ifdef HAVE_WINDOW_SYSTEM
10688 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10689 {
10690 int fringe_bitmap;
10691 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10692 return make_number (fringe_bitmap);
10693 }
10694 #endif
10695 return make_number (-1); /* Use default arrow bitmap */
10696 }
10697 return overlay_arrow_string_or_property (var);
10698 }
10699 }
10700
10701 return Qnil;
10702 }
10703
10704 /* Return 1 if point moved out of or into a composition. Otherwise
10705 return 0. PREV_BUF and PREV_PT are the last point buffer and
10706 position. BUF and PT are the current point buffer and position. */
10707
10708 int
10709 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10710 struct buffer *prev_buf, *buf;
10711 int prev_pt, pt;
10712 {
10713 int start, end;
10714 Lisp_Object prop;
10715 Lisp_Object buffer;
10716
10717 XSETBUFFER (buffer, buf);
10718 /* Check a composition at the last point if point moved within the
10719 same buffer. */
10720 if (prev_buf == buf)
10721 {
10722 if (prev_pt == pt)
10723 /* Point didn't move. */
10724 return 0;
10725
10726 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10727 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10728 && COMPOSITION_VALID_P (start, end, prop)
10729 && start < prev_pt && end > prev_pt)
10730 /* The last point was within the composition. Return 1 iff
10731 point moved out of the composition. */
10732 return (pt <= start || pt >= end);
10733 }
10734
10735 /* Check a composition at the current point. */
10736 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10737 && find_composition (pt, -1, &start, &end, &prop, buffer)
10738 && COMPOSITION_VALID_P (start, end, prop)
10739 && start < pt && end > pt);
10740 }
10741
10742
10743 /* Reconsider the setting of B->clip_changed which is displayed
10744 in window W. */
10745
10746 static INLINE void
10747 reconsider_clip_changes (w, b)
10748 struct window *w;
10749 struct buffer *b;
10750 {
10751 if (b->clip_changed
10752 && !NILP (w->window_end_valid)
10753 && w->current_matrix->buffer == b
10754 && w->current_matrix->zv == BUF_ZV (b)
10755 && w->current_matrix->begv == BUF_BEGV (b))
10756 b->clip_changed = 0;
10757
10758 /* If display wasn't paused, and W is not a tool bar window, see if
10759 point has been moved into or out of a composition. In that case,
10760 we set b->clip_changed to 1 to force updating the screen. If
10761 b->clip_changed has already been set to 1, we can skip this
10762 check. */
10763 if (!b->clip_changed
10764 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10765 {
10766 int pt;
10767
10768 if (w == XWINDOW (selected_window))
10769 pt = BUF_PT (current_buffer);
10770 else
10771 pt = marker_position (w->pointm);
10772
10773 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10774 || pt != XINT (w->last_point))
10775 && check_point_in_composition (w->current_matrix->buffer,
10776 XINT (w->last_point),
10777 XBUFFER (w->buffer), pt))
10778 b->clip_changed = 1;
10779 }
10780 }
10781 \f
10782
10783 /* Select FRAME to forward the values of frame-local variables into C
10784 variables so that the redisplay routines can access those values
10785 directly. */
10786
10787 static void
10788 select_frame_for_redisplay (frame)
10789 Lisp_Object frame;
10790 {
10791 Lisp_Object tail, sym, val;
10792 Lisp_Object old = selected_frame;
10793
10794 selected_frame = frame;
10795
10796 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10797 if (CONSP (XCAR (tail))
10798 && (sym = XCAR (XCAR (tail)),
10799 SYMBOLP (sym))
10800 && (sym = indirect_variable (sym),
10801 val = SYMBOL_VALUE (sym),
10802 (BUFFER_LOCAL_VALUEP (val)
10803 || SOME_BUFFER_LOCAL_VALUEP (val)))
10804 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10805 /* Use find_symbol_value rather than Fsymbol_value
10806 to avoid an error if it is void. */
10807 find_symbol_value (sym);
10808
10809 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10810 if (CONSP (XCAR (tail))
10811 && (sym = XCAR (XCAR (tail)),
10812 SYMBOLP (sym))
10813 && (sym = indirect_variable (sym),
10814 val = SYMBOL_VALUE (sym),
10815 (BUFFER_LOCAL_VALUEP (val)
10816 || SOME_BUFFER_LOCAL_VALUEP (val)))
10817 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10818 find_symbol_value (sym);
10819 }
10820
10821
10822 #define STOP_POLLING \
10823 do { if (! polling_stopped_here) stop_polling (); \
10824 polling_stopped_here = 1; } while (0)
10825
10826 #define RESUME_POLLING \
10827 do { if (polling_stopped_here) start_polling (); \
10828 polling_stopped_here = 0; } while (0)
10829
10830
10831 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10832 response to any user action; therefore, we should preserve the echo
10833 area. (Actually, our caller does that job.) Perhaps in the future
10834 avoid recentering windows if it is not necessary; currently that
10835 causes some problems. */
10836
10837 static void
10838 redisplay_internal (preserve_echo_area)
10839 int preserve_echo_area;
10840 {
10841 struct window *w = XWINDOW (selected_window);
10842 struct frame *f;
10843 int pause;
10844 int must_finish = 0;
10845 struct text_pos tlbufpos, tlendpos;
10846 int number_of_visible_frames;
10847 int count, count1;
10848 struct frame *sf;
10849 int polling_stopped_here = 0;
10850
10851 /* Non-zero means redisplay has to consider all windows on all
10852 frames. Zero means, only selected_window is considered. */
10853 int consider_all_windows_p;
10854
10855 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10856
10857 /* No redisplay if running in batch mode or frame is not yet fully
10858 initialized, or redisplay is explicitly turned off by setting
10859 Vinhibit_redisplay. */
10860 if (noninteractive
10861 || !NILP (Vinhibit_redisplay))
10862 return;
10863
10864 /* Don't examine these until after testing Vinhibit_redisplay.
10865 When Emacs is shutting down, perhaps because its connection to
10866 X has dropped, we should not look at them at all. */
10867 f = XFRAME (w->frame);
10868 sf = SELECTED_FRAME ();
10869
10870 if (!f->glyphs_initialized_p)
10871 return;
10872
10873 /* The flag redisplay_performed_directly_p is set by
10874 direct_output_for_insert when it already did the whole screen
10875 update necessary. */
10876 if (redisplay_performed_directly_p)
10877 {
10878 redisplay_performed_directly_p = 0;
10879 if (!hscroll_windows (selected_window))
10880 return;
10881 }
10882
10883 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10884 if (popup_activated ())
10885 return;
10886 #endif
10887
10888 /* I don't think this happens but let's be paranoid. */
10889 if (redisplaying_p)
10890 return;
10891
10892 /* Record a function that resets redisplaying_p to its old value
10893 when we leave this function. */
10894 count = SPECPDL_INDEX ();
10895 record_unwind_protect (unwind_redisplay,
10896 Fcons (make_number (redisplaying_p), selected_frame));
10897 ++redisplaying_p;
10898 specbind (Qinhibit_free_realized_faces, Qnil);
10899
10900 {
10901 Lisp_Object tail, frame;
10902
10903 FOR_EACH_FRAME (tail, frame)
10904 {
10905 struct frame *f = XFRAME (frame);
10906 f->already_hscrolled_p = 0;
10907 }
10908 }
10909
10910 retry:
10911 pause = 0;
10912 reconsider_clip_changes (w, current_buffer);
10913 last_escape_glyph_frame = NULL;
10914 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10915
10916 /* If new fonts have been loaded that make a glyph matrix adjustment
10917 necessary, do it. */
10918 if (fonts_changed_p)
10919 {
10920 adjust_glyphs (NULL);
10921 ++windows_or_buffers_changed;
10922 fonts_changed_p = 0;
10923 }
10924
10925 /* If face_change_count is non-zero, init_iterator will free all
10926 realized faces, which includes the faces referenced from current
10927 matrices. So, we can't reuse current matrices in this case. */
10928 if (face_change_count)
10929 ++windows_or_buffers_changed;
10930
10931 if (! FRAME_WINDOW_P (sf)
10932 && previous_terminal_frame != sf)
10933 {
10934 /* Since frames on an ASCII terminal share the same display
10935 area, displaying a different frame means redisplay the whole
10936 thing. */
10937 windows_or_buffers_changed++;
10938 SET_FRAME_GARBAGED (sf);
10939 XSETFRAME (Vterminal_frame, sf);
10940 }
10941 previous_terminal_frame = sf;
10942
10943 /* Set the visible flags for all frames. Do this before checking
10944 for resized or garbaged frames; they want to know if their frames
10945 are visible. See the comment in frame.h for
10946 FRAME_SAMPLE_VISIBILITY. */
10947 {
10948 Lisp_Object tail, frame;
10949
10950 number_of_visible_frames = 0;
10951
10952 FOR_EACH_FRAME (tail, frame)
10953 {
10954 struct frame *f = XFRAME (frame);
10955
10956 FRAME_SAMPLE_VISIBILITY (f);
10957 if (FRAME_VISIBLE_P (f))
10958 ++number_of_visible_frames;
10959 clear_desired_matrices (f);
10960 }
10961 }
10962
10963 /* Notice any pending interrupt request to change frame size. */
10964 do_pending_window_change (1);
10965
10966 /* Clear frames marked as garbaged. */
10967 if (frame_garbaged)
10968 clear_garbaged_frames ();
10969
10970 /* Build menubar and tool-bar items. */
10971 if (NILP (Vmemory_full))
10972 prepare_menu_bars ();
10973
10974 if (windows_or_buffers_changed)
10975 update_mode_lines++;
10976
10977 /* Detect case that we need to write or remove a star in the mode line. */
10978 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10979 {
10980 w->update_mode_line = Qt;
10981 if (buffer_shared > 1)
10982 update_mode_lines++;
10983 }
10984
10985 /* Avoid invocation of point motion hooks by `current_column' below. */
10986 count1 = SPECPDL_INDEX ();
10987 specbind (Qinhibit_point_motion_hooks, Qt);
10988
10989 /* If %c is in the mode line, update it if needed. */
10990 if (!NILP (w->column_number_displayed)
10991 /* This alternative quickly identifies a common case
10992 where no change is needed. */
10993 && !(PT == XFASTINT (w->last_point)
10994 && XFASTINT (w->last_modified) >= MODIFF
10995 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10996 && (XFASTINT (w->column_number_displayed)
10997 != (int) current_column ())) /* iftc */
10998 w->update_mode_line = Qt;
10999
11000 unbind_to (count1, Qnil);
11001
11002 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11003
11004 /* The variable buffer_shared is set in redisplay_window and
11005 indicates that we redisplay a buffer in different windows. See
11006 there. */
11007 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11008 || cursor_type_changed);
11009
11010 /* If specs for an arrow have changed, do thorough redisplay
11011 to ensure we remove any arrow that should no longer exist. */
11012 if (overlay_arrows_changed_p ())
11013 consider_all_windows_p = windows_or_buffers_changed = 1;
11014
11015 /* Normally the message* functions will have already displayed and
11016 updated the echo area, but the frame may have been trashed, or
11017 the update may have been preempted, so display the echo area
11018 again here. Checking message_cleared_p captures the case that
11019 the echo area should be cleared. */
11020 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11021 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11022 || (message_cleared_p
11023 && minibuf_level == 0
11024 /* If the mini-window is currently selected, this means the
11025 echo-area doesn't show through. */
11026 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11027 {
11028 int window_height_changed_p = echo_area_display (0);
11029 must_finish = 1;
11030
11031 /* If we don't display the current message, don't clear the
11032 message_cleared_p flag, because, if we did, we wouldn't clear
11033 the echo area in the next redisplay which doesn't preserve
11034 the echo area. */
11035 if (!display_last_displayed_message_p)
11036 message_cleared_p = 0;
11037
11038 if (fonts_changed_p)
11039 goto retry;
11040 else if (window_height_changed_p)
11041 {
11042 consider_all_windows_p = 1;
11043 ++update_mode_lines;
11044 ++windows_or_buffers_changed;
11045
11046 /* If window configuration was changed, frames may have been
11047 marked garbaged. Clear them or we will experience
11048 surprises wrt scrolling. */
11049 if (frame_garbaged)
11050 clear_garbaged_frames ();
11051 }
11052 }
11053 else if (EQ (selected_window, minibuf_window)
11054 && (current_buffer->clip_changed
11055 || XFASTINT (w->last_modified) < MODIFF
11056 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11057 && resize_mini_window (w, 0))
11058 {
11059 /* Resized active mini-window to fit the size of what it is
11060 showing if its contents might have changed. */
11061 must_finish = 1;
11062 consider_all_windows_p = 1;
11063 ++windows_or_buffers_changed;
11064 ++update_mode_lines;
11065
11066 /* If window configuration was changed, frames may have been
11067 marked garbaged. Clear them or we will experience
11068 surprises wrt scrolling. */
11069 if (frame_garbaged)
11070 clear_garbaged_frames ();
11071 }
11072
11073
11074 /* If showing the region, and mark has changed, we must redisplay
11075 the whole window. The assignment to this_line_start_pos prevents
11076 the optimization directly below this if-statement. */
11077 if (((!NILP (Vtransient_mark_mode)
11078 && !NILP (XBUFFER (w->buffer)->mark_active))
11079 != !NILP (w->region_showing))
11080 || (!NILP (w->region_showing)
11081 && !EQ (w->region_showing,
11082 Fmarker_position (XBUFFER (w->buffer)->mark))))
11083 CHARPOS (this_line_start_pos) = 0;
11084
11085 /* Optimize the case that only the line containing the cursor in the
11086 selected window has changed. Variables starting with this_ are
11087 set in display_line and record information about the line
11088 containing the cursor. */
11089 tlbufpos = this_line_start_pos;
11090 tlendpos = this_line_end_pos;
11091 if (!consider_all_windows_p
11092 && CHARPOS (tlbufpos) > 0
11093 && NILP (w->update_mode_line)
11094 && !current_buffer->clip_changed
11095 && !current_buffer->prevent_redisplay_optimizations_p
11096 && FRAME_VISIBLE_P (XFRAME (w->frame))
11097 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11098 /* Make sure recorded data applies to current buffer, etc. */
11099 && this_line_buffer == current_buffer
11100 && current_buffer == XBUFFER (w->buffer)
11101 && NILP (w->force_start)
11102 && NILP (w->optional_new_start)
11103 /* Point must be on the line that we have info recorded about. */
11104 && PT >= CHARPOS (tlbufpos)
11105 && PT <= Z - CHARPOS (tlendpos)
11106 /* All text outside that line, including its final newline,
11107 must be unchanged */
11108 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11109 CHARPOS (tlendpos)))
11110 {
11111 if (CHARPOS (tlbufpos) > BEGV
11112 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11113 && (CHARPOS (tlbufpos) == ZV
11114 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11115 /* Former continuation line has disappeared by becoming empty */
11116 goto cancel;
11117 else if (XFASTINT (w->last_modified) < MODIFF
11118 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11119 || MINI_WINDOW_P (w))
11120 {
11121 /* We have to handle the case of continuation around a
11122 wide-column character (See the comment in indent.c around
11123 line 885).
11124
11125 For instance, in the following case:
11126
11127 -------- Insert --------
11128 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11129 J_I_ ==> J_I_ `^^' are cursors.
11130 ^^ ^^
11131 -------- --------
11132
11133 As we have to redraw the line above, we should goto cancel. */
11134
11135 struct it it;
11136 int line_height_before = this_line_pixel_height;
11137
11138 /* Note that start_display will handle the case that the
11139 line starting at tlbufpos is a continuation lines. */
11140 start_display (&it, w, tlbufpos);
11141
11142 /* Implementation note: It this still necessary? */
11143 if (it.current_x != this_line_start_x)
11144 goto cancel;
11145
11146 TRACE ((stderr, "trying display optimization 1\n"));
11147 w->cursor.vpos = -1;
11148 overlay_arrow_seen = 0;
11149 it.vpos = this_line_vpos;
11150 it.current_y = this_line_y;
11151 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11152 display_line (&it);
11153
11154 /* If line contains point, is not continued,
11155 and ends at same distance from eob as before, we win */
11156 if (w->cursor.vpos >= 0
11157 /* Line is not continued, otherwise this_line_start_pos
11158 would have been set to 0 in display_line. */
11159 && CHARPOS (this_line_start_pos)
11160 /* Line ends as before. */
11161 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11162 /* Line has same height as before. Otherwise other lines
11163 would have to be shifted up or down. */
11164 && this_line_pixel_height == line_height_before)
11165 {
11166 /* If this is not the window's last line, we must adjust
11167 the charstarts of the lines below. */
11168 if (it.current_y < it.last_visible_y)
11169 {
11170 struct glyph_row *row
11171 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11172 int delta, delta_bytes;
11173
11174 if (Z - CHARPOS (tlendpos) == ZV)
11175 {
11176 /* This line ends at end of (accessible part of)
11177 buffer. There is no newline to count. */
11178 delta = (Z
11179 - CHARPOS (tlendpos)
11180 - MATRIX_ROW_START_CHARPOS (row));
11181 delta_bytes = (Z_BYTE
11182 - BYTEPOS (tlendpos)
11183 - MATRIX_ROW_START_BYTEPOS (row));
11184 }
11185 else
11186 {
11187 /* This line ends in a newline. Must take
11188 account of the newline and the rest of the
11189 text that follows. */
11190 delta = (Z
11191 - CHARPOS (tlendpos)
11192 - MATRIX_ROW_START_CHARPOS (row));
11193 delta_bytes = (Z_BYTE
11194 - BYTEPOS (tlendpos)
11195 - MATRIX_ROW_START_BYTEPOS (row));
11196 }
11197
11198 increment_matrix_positions (w->current_matrix,
11199 this_line_vpos + 1,
11200 w->current_matrix->nrows,
11201 delta, delta_bytes);
11202 }
11203
11204 /* If this row displays text now but previously didn't,
11205 or vice versa, w->window_end_vpos may have to be
11206 adjusted. */
11207 if ((it.glyph_row - 1)->displays_text_p)
11208 {
11209 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11210 XSETINT (w->window_end_vpos, this_line_vpos);
11211 }
11212 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11213 && this_line_vpos > 0)
11214 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11215 w->window_end_valid = Qnil;
11216
11217 /* Update hint: No need to try to scroll in update_window. */
11218 w->desired_matrix->no_scrolling_p = 1;
11219
11220 #if GLYPH_DEBUG
11221 *w->desired_matrix->method = 0;
11222 debug_method_add (w, "optimization 1");
11223 #endif
11224 #ifdef HAVE_WINDOW_SYSTEM
11225 update_window_fringes (w, 0);
11226 #endif
11227 goto update;
11228 }
11229 else
11230 goto cancel;
11231 }
11232 else if (/* Cursor position hasn't changed. */
11233 PT == XFASTINT (w->last_point)
11234 /* Make sure the cursor was last displayed
11235 in this window. Otherwise we have to reposition it. */
11236 && 0 <= w->cursor.vpos
11237 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11238 {
11239 if (!must_finish)
11240 {
11241 do_pending_window_change (1);
11242
11243 /* We used to always goto end_of_redisplay here, but this
11244 isn't enough if we have a blinking cursor. */
11245 if (w->cursor_off_p == w->last_cursor_off_p)
11246 goto end_of_redisplay;
11247 }
11248 goto update;
11249 }
11250 /* If highlighting the region, or if the cursor is in the echo area,
11251 then we can't just move the cursor. */
11252 else if (! (!NILP (Vtransient_mark_mode)
11253 && !NILP (current_buffer->mark_active))
11254 && (EQ (selected_window, current_buffer->last_selected_window)
11255 || highlight_nonselected_windows)
11256 && NILP (w->region_showing)
11257 && NILP (Vshow_trailing_whitespace)
11258 && !cursor_in_echo_area)
11259 {
11260 struct it it;
11261 struct glyph_row *row;
11262
11263 /* Skip from tlbufpos to PT and see where it is. Note that
11264 PT may be in invisible text. If so, we will end at the
11265 next visible position. */
11266 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11267 NULL, DEFAULT_FACE_ID);
11268 it.current_x = this_line_start_x;
11269 it.current_y = this_line_y;
11270 it.vpos = this_line_vpos;
11271
11272 /* The call to move_it_to stops in front of PT, but
11273 moves over before-strings. */
11274 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11275
11276 if (it.vpos == this_line_vpos
11277 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11278 row->enabled_p))
11279 {
11280 xassert (this_line_vpos == it.vpos);
11281 xassert (this_line_y == it.current_y);
11282 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11283 #if GLYPH_DEBUG
11284 *w->desired_matrix->method = 0;
11285 debug_method_add (w, "optimization 3");
11286 #endif
11287 goto update;
11288 }
11289 else
11290 goto cancel;
11291 }
11292
11293 cancel:
11294 /* Text changed drastically or point moved off of line. */
11295 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11296 }
11297
11298 CHARPOS (this_line_start_pos) = 0;
11299 consider_all_windows_p |= buffer_shared > 1;
11300 ++clear_face_cache_count;
11301 #ifdef HAVE_WINDOW_SYSTEM
11302 ++clear_image_cache_count;
11303 #endif
11304
11305 /* Build desired matrices, and update the display. If
11306 consider_all_windows_p is non-zero, do it for all windows on all
11307 frames. Otherwise do it for selected_window, only. */
11308
11309 if (consider_all_windows_p)
11310 {
11311 Lisp_Object tail, frame;
11312
11313 FOR_EACH_FRAME (tail, frame)
11314 XFRAME (frame)->updated_p = 0;
11315
11316 /* Recompute # windows showing selected buffer. This will be
11317 incremented each time such a window is displayed. */
11318 buffer_shared = 0;
11319
11320 FOR_EACH_FRAME (tail, frame)
11321 {
11322 struct frame *f = XFRAME (frame);
11323
11324 if (FRAME_WINDOW_P (f) || f == sf)
11325 {
11326 if (! EQ (frame, selected_frame))
11327 /* Select the frame, for the sake of frame-local
11328 variables. */
11329 select_frame_for_redisplay (frame);
11330
11331 /* Mark all the scroll bars to be removed; we'll redeem
11332 the ones we want when we redisplay their windows. */
11333 if (condemn_scroll_bars_hook)
11334 condemn_scroll_bars_hook (f);
11335
11336 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11337 redisplay_windows (FRAME_ROOT_WINDOW (f));
11338
11339 /* Any scroll bars which redisplay_windows should have
11340 nuked should now go away. */
11341 if (judge_scroll_bars_hook)
11342 judge_scroll_bars_hook (f);
11343
11344 /* If fonts changed, display again. */
11345 /* ??? rms: I suspect it is a mistake to jump all the way
11346 back to retry here. It should just retry this frame. */
11347 if (fonts_changed_p)
11348 goto retry;
11349
11350 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11351 {
11352 /* See if we have to hscroll. */
11353 if (!f->already_hscrolled_p)
11354 {
11355 f->already_hscrolled_p = 1;
11356 if (hscroll_windows (f->root_window))
11357 goto retry;
11358 }
11359
11360 /* Prevent various kinds of signals during display
11361 update. stdio is not robust about handling
11362 signals, which can cause an apparent I/O
11363 error. */
11364 if (interrupt_input)
11365 unrequest_sigio ();
11366 STOP_POLLING;
11367
11368 /* Update the display. */
11369 set_window_update_flags (XWINDOW (f->root_window), 1);
11370 pause |= update_frame (f, 0, 0);
11371 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11372 if (pause)
11373 break;
11374 #endif
11375
11376 f->updated_p = 1;
11377 }
11378 }
11379 }
11380
11381 if (!pause)
11382 {
11383 /* Do the mark_window_display_accurate after all windows have
11384 been redisplayed because this call resets flags in buffers
11385 which are needed for proper redisplay. */
11386 FOR_EACH_FRAME (tail, frame)
11387 {
11388 struct frame *f = XFRAME (frame);
11389 if (f->updated_p)
11390 {
11391 mark_window_display_accurate (f->root_window, 1);
11392 if (frame_up_to_date_hook)
11393 frame_up_to_date_hook (f);
11394 }
11395 }
11396 }
11397 }
11398 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11399 {
11400 Lisp_Object mini_window;
11401 struct frame *mini_frame;
11402
11403 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11404 /* Use list_of_error, not Qerror, so that
11405 we catch only errors and don't run the debugger. */
11406 internal_condition_case_1 (redisplay_window_1, selected_window,
11407 list_of_error,
11408 redisplay_window_error);
11409
11410 /* Compare desired and current matrices, perform output. */
11411
11412 update:
11413 /* If fonts changed, display again. */
11414 if (fonts_changed_p)
11415 goto retry;
11416
11417 /* Prevent various kinds of signals during display update.
11418 stdio is not robust about handling signals,
11419 which can cause an apparent I/O error. */
11420 if (interrupt_input)
11421 unrequest_sigio ();
11422 STOP_POLLING;
11423
11424 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11425 {
11426 if (hscroll_windows (selected_window))
11427 goto retry;
11428
11429 XWINDOW (selected_window)->must_be_updated_p = 1;
11430 pause = update_frame (sf, 0, 0);
11431 }
11432
11433 /* We may have called echo_area_display at the top of this
11434 function. If the echo area is on another frame, that may
11435 have put text on a frame other than the selected one, so the
11436 above call to update_frame would not have caught it. Catch
11437 it here. */
11438 mini_window = FRAME_MINIBUF_WINDOW (sf);
11439 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11440
11441 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11442 {
11443 XWINDOW (mini_window)->must_be_updated_p = 1;
11444 pause |= update_frame (mini_frame, 0, 0);
11445 if (!pause && hscroll_windows (mini_window))
11446 goto retry;
11447 }
11448 }
11449
11450 /* If display was paused because of pending input, make sure we do a
11451 thorough update the next time. */
11452 if (pause)
11453 {
11454 /* Prevent the optimization at the beginning of
11455 redisplay_internal that tries a single-line update of the
11456 line containing the cursor in the selected window. */
11457 CHARPOS (this_line_start_pos) = 0;
11458
11459 /* Let the overlay arrow be updated the next time. */
11460 update_overlay_arrows (0);
11461
11462 /* If we pause after scrolling, some rows in the current
11463 matrices of some windows are not valid. */
11464 if (!WINDOW_FULL_WIDTH_P (w)
11465 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11466 update_mode_lines = 1;
11467 }
11468 else
11469 {
11470 if (!consider_all_windows_p)
11471 {
11472 /* This has already been done above if
11473 consider_all_windows_p is set. */
11474 mark_window_display_accurate_1 (w, 1);
11475
11476 /* Say overlay arrows are up to date. */
11477 update_overlay_arrows (1);
11478
11479 if (frame_up_to_date_hook != 0)
11480 frame_up_to_date_hook (sf);
11481 }
11482
11483 update_mode_lines = 0;
11484 windows_or_buffers_changed = 0;
11485 cursor_type_changed = 0;
11486 }
11487
11488 /* Start SIGIO interrupts coming again. Having them off during the
11489 code above makes it less likely one will discard output, but not
11490 impossible, since there might be stuff in the system buffer here.
11491 But it is much hairier to try to do anything about that. */
11492 if (interrupt_input)
11493 request_sigio ();
11494 RESUME_POLLING;
11495
11496 /* If a frame has become visible which was not before, redisplay
11497 again, so that we display it. Expose events for such a frame
11498 (which it gets when becoming visible) don't call the parts of
11499 redisplay constructing glyphs, so simply exposing a frame won't
11500 display anything in this case. So, we have to display these
11501 frames here explicitly. */
11502 if (!pause)
11503 {
11504 Lisp_Object tail, frame;
11505 int new_count = 0;
11506
11507 FOR_EACH_FRAME (tail, frame)
11508 {
11509 int this_is_visible = 0;
11510
11511 if (XFRAME (frame)->visible)
11512 this_is_visible = 1;
11513 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11514 if (XFRAME (frame)->visible)
11515 this_is_visible = 1;
11516
11517 if (this_is_visible)
11518 new_count++;
11519 }
11520
11521 if (new_count != number_of_visible_frames)
11522 windows_or_buffers_changed++;
11523 }
11524
11525 /* Change frame size now if a change is pending. */
11526 do_pending_window_change (1);
11527
11528 /* If we just did a pending size change, or have additional
11529 visible frames, redisplay again. */
11530 if (windows_or_buffers_changed && !pause)
11531 goto retry;
11532
11533 /* Clear the face cache eventually. */
11534 if (consider_all_windows_p)
11535 {
11536 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11537 {
11538 clear_face_cache (0);
11539 clear_face_cache_count = 0;
11540 }
11541 #ifdef HAVE_WINDOW_SYSTEM
11542 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11543 {
11544 Lisp_Object tail, frame;
11545 FOR_EACH_FRAME (tail, frame)
11546 {
11547 struct frame *f = XFRAME (frame);
11548 if (FRAME_WINDOW_P (f))
11549 clear_image_cache (f, 0);
11550 }
11551 clear_image_cache_count = 0;
11552 }
11553 #endif /* HAVE_WINDOW_SYSTEM */
11554 }
11555
11556 end_of_redisplay:
11557 unbind_to (count, Qnil);
11558 RESUME_POLLING;
11559 }
11560
11561
11562 /* Redisplay, but leave alone any recent echo area message unless
11563 another message has been requested in its place.
11564
11565 This is useful in situations where you need to redisplay but no
11566 user action has occurred, making it inappropriate for the message
11567 area to be cleared. See tracking_off and
11568 wait_reading_process_output for examples of these situations.
11569
11570 FROM_WHERE is an integer saying from where this function was
11571 called. This is useful for debugging. */
11572
11573 void
11574 redisplay_preserve_echo_area (from_where)
11575 int from_where;
11576 {
11577 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11578
11579 if (!NILP (echo_area_buffer[1]))
11580 {
11581 /* We have a previously displayed message, but no current
11582 message. Redisplay the previous message. */
11583 display_last_displayed_message_p = 1;
11584 redisplay_internal (1);
11585 display_last_displayed_message_p = 0;
11586 }
11587 else
11588 redisplay_internal (1);
11589
11590 if (rif != NULL && rif->flush_display_optional)
11591 rif->flush_display_optional (NULL);
11592 }
11593
11594
11595 /* Function registered with record_unwind_protect in
11596 redisplay_internal. Reset redisplaying_p to the value it had
11597 before redisplay_internal was called, and clear
11598 prevent_freeing_realized_faces_p. It also selects the previously
11599 selected frame. */
11600
11601 static Lisp_Object
11602 unwind_redisplay (val)
11603 Lisp_Object val;
11604 {
11605 Lisp_Object old_redisplaying_p, old_frame;
11606
11607 old_redisplaying_p = XCAR (val);
11608 redisplaying_p = XFASTINT (old_redisplaying_p);
11609 old_frame = XCDR (val);
11610 if (! EQ (old_frame, selected_frame))
11611 select_frame_for_redisplay (old_frame);
11612 return Qnil;
11613 }
11614
11615
11616 /* Mark the display of window W as accurate or inaccurate. If
11617 ACCURATE_P is non-zero mark display of W as accurate. If
11618 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11619 redisplay_internal is called. */
11620
11621 static void
11622 mark_window_display_accurate_1 (w, accurate_p)
11623 struct window *w;
11624 int accurate_p;
11625 {
11626 if (BUFFERP (w->buffer))
11627 {
11628 struct buffer *b = XBUFFER (w->buffer);
11629
11630 w->last_modified
11631 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11632 w->last_overlay_modified
11633 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11634 w->last_had_star
11635 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11636
11637 if (accurate_p)
11638 {
11639 b->clip_changed = 0;
11640 b->prevent_redisplay_optimizations_p = 0;
11641
11642 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11643 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11644 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11645 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11646
11647 w->current_matrix->buffer = b;
11648 w->current_matrix->begv = BUF_BEGV (b);
11649 w->current_matrix->zv = BUF_ZV (b);
11650
11651 w->last_cursor = w->cursor;
11652 w->last_cursor_off_p = w->cursor_off_p;
11653
11654 if (w == XWINDOW (selected_window))
11655 w->last_point = make_number (BUF_PT (b));
11656 else
11657 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11658 }
11659 }
11660
11661 if (accurate_p)
11662 {
11663 w->window_end_valid = w->buffer;
11664 #if 0 /* This is incorrect with variable-height lines. */
11665 xassert (XINT (w->window_end_vpos)
11666 < (WINDOW_TOTAL_LINES (w)
11667 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11668 #endif
11669 w->update_mode_line = Qnil;
11670 }
11671 }
11672
11673
11674 /* Mark the display of windows in the window tree rooted at WINDOW as
11675 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11676 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11677 be redisplayed the next time redisplay_internal is called. */
11678
11679 void
11680 mark_window_display_accurate (window, accurate_p)
11681 Lisp_Object window;
11682 int accurate_p;
11683 {
11684 struct window *w;
11685
11686 for (; !NILP (window); window = w->next)
11687 {
11688 w = XWINDOW (window);
11689 mark_window_display_accurate_1 (w, accurate_p);
11690
11691 if (!NILP (w->vchild))
11692 mark_window_display_accurate (w->vchild, accurate_p);
11693 if (!NILP (w->hchild))
11694 mark_window_display_accurate (w->hchild, accurate_p);
11695 }
11696
11697 if (accurate_p)
11698 {
11699 update_overlay_arrows (1);
11700 }
11701 else
11702 {
11703 /* Force a thorough redisplay the next time by setting
11704 last_arrow_position and last_arrow_string to t, which is
11705 unequal to any useful value of Voverlay_arrow_... */
11706 update_overlay_arrows (-1);
11707 }
11708 }
11709
11710
11711 /* Return value in display table DP (Lisp_Char_Table *) for character
11712 C. Since a display table doesn't have any parent, we don't have to
11713 follow parent. Do not call this function directly but use the
11714 macro DISP_CHAR_VECTOR. */
11715
11716 Lisp_Object
11717 disp_char_vector (dp, c)
11718 struct Lisp_Char_Table *dp;
11719 int c;
11720 {
11721 int code[4], i;
11722 Lisp_Object val;
11723
11724 if (SINGLE_BYTE_CHAR_P (c))
11725 return (dp->contents[c]);
11726
11727 SPLIT_CHAR (c, code[0], code[1], code[2]);
11728 if (code[1] < 32)
11729 code[1] = -1;
11730 else if (code[2] < 32)
11731 code[2] = -1;
11732
11733 /* Here, the possible range of code[0] (== charset ID) is
11734 128..max_charset. Since the top level char table contains data
11735 for multibyte characters after 256th element, we must increment
11736 code[0] by 128 to get a correct index. */
11737 code[0] += 128;
11738 code[3] = -1; /* anchor */
11739
11740 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11741 {
11742 val = dp->contents[code[i]];
11743 if (!SUB_CHAR_TABLE_P (val))
11744 return (NILP (val) ? dp->defalt : val);
11745 }
11746
11747 /* Here, val is a sub char table. We return the default value of
11748 it. */
11749 return (dp->defalt);
11750 }
11751
11752
11753 \f
11754 /***********************************************************************
11755 Window Redisplay
11756 ***********************************************************************/
11757
11758 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11759
11760 static void
11761 redisplay_windows (window)
11762 Lisp_Object window;
11763 {
11764 while (!NILP (window))
11765 {
11766 struct window *w = XWINDOW (window);
11767
11768 if (!NILP (w->hchild))
11769 redisplay_windows (w->hchild);
11770 else if (!NILP (w->vchild))
11771 redisplay_windows (w->vchild);
11772 else
11773 {
11774 displayed_buffer = XBUFFER (w->buffer);
11775 /* Use list_of_error, not Qerror, so that
11776 we catch only errors and don't run the debugger. */
11777 internal_condition_case_1 (redisplay_window_0, window,
11778 list_of_error,
11779 redisplay_window_error);
11780 }
11781
11782 window = w->next;
11783 }
11784 }
11785
11786 static Lisp_Object
11787 redisplay_window_error ()
11788 {
11789 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11790 return Qnil;
11791 }
11792
11793 static Lisp_Object
11794 redisplay_window_0 (window)
11795 Lisp_Object window;
11796 {
11797 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11798 redisplay_window (window, 0);
11799 return Qnil;
11800 }
11801
11802 static Lisp_Object
11803 redisplay_window_1 (window)
11804 Lisp_Object window;
11805 {
11806 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11807 redisplay_window (window, 1);
11808 return Qnil;
11809 }
11810 \f
11811
11812 /* Increment GLYPH until it reaches END or CONDITION fails while
11813 adding (GLYPH)->pixel_width to X. */
11814
11815 #define SKIP_GLYPHS(glyph, end, x, condition) \
11816 do \
11817 { \
11818 (x) += (glyph)->pixel_width; \
11819 ++(glyph); \
11820 } \
11821 while ((glyph) < (end) && (condition))
11822
11823
11824 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11825 DELTA is the number of bytes by which positions recorded in ROW
11826 differ from current buffer positions.
11827
11828 Return 0 if cursor is not on this row. 1 otherwise. */
11829
11830 int
11831 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11832 struct window *w;
11833 struct glyph_row *row;
11834 struct glyph_matrix *matrix;
11835 int delta, delta_bytes, dy, dvpos;
11836 {
11837 struct glyph *glyph = row->glyphs[TEXT_AREA];
11838 struct glyph *end = glyph + row->used[TEXT_AREA];
11839 struct glyph *cursor = NULL;
11840 /* The first glyph that starts a sequence of glyphs from string. */
11841 struct glyph *string_start;
11842 /* The X coordinate of string_start. */
11843 int string_start_x;
11844 /* The last known character position. */
11845 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11846 /* The last known character position before string_start. */
11847 int string_before_pos;
11848 int x = row->x;
11849 int cursor_x = x;
11850 int cursor_from_overlay_pos = 0;
11851 int pt_old = PT - delta;
11852
11853 /* Skip over glyphs not having an object at the start of the row.
11854 These are special glyphs like truncation marks on terminal
11855 frames. */
11856 if (row->displays_text_p)
11857 while (glyph < end
11858 && INTEGERP (glyph->object)
11859 && glyph->charpos < 0)
11860 {
11861 x += glyph->pixel_width;
11862 ++glyph;
11863 }
11864
11865 string_start = NULL;
11866 while (glyph < end
11867 && !INTEGERP (glyph->object)
11868 && (!BUFFERP (glyph->object)
11869 || (last_pos = glyph->charpos) < pt_old))
11870 {
11871 if (! STRINGP (glyph->object))
11872 {
11873 string_start = NULL;
11874 x += glyph->pixel_width;
11875 ++glyph;
11876 if (cursor_from_overlay_pos
11877 && last_pos >= cursor_from_overlay_pos)
11878 {
11879 cursor_from_overlay_pos = 0;
11880 cursor = 0;
11881 }
11882 }
11883 else
11884 {
11885 if (string_start == NULL)
11886 {
11887 string_before_pos = last_pos;
11888 string_start = glyph;
11889 string_start_x = x;
11890 }
11891 /* Skip all glyphs from string. */
11892 do
11893 {
11894 Lisp_Object cprop;
11895 int pos;
11896 if ((cursor == NULL || glyph > cursor)
11897 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11898 Qcursor, (glyph)->object),
11899 !NILP (cprop))
11900 && (pos = string_buffer_position (w, glyph->object,
11901 string_before_pos),
11902 (pos == 0 /* From overlay */
11903 || pos == pt_old)))
11904 {
11905 /* Estimate overlay buffer position from the buffer
11906 positions of the glyphs before and after the overlay.
11907 Add 1 to last_pos so that if point corresponds to the
11908 glyph right after the overlay, we still use a 'cursor'
11909 property found in that overlay. */
11910 cursor_from_overlay_pos = (pos ? 0 : last_pos
11911 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11912 cursor = glyph;
11913 cursor_x = x;
11914 }
11915 x += glyph->pixel_width;
11916 ++glyph;
11917 }
11918 while (glyph < end && EQ (glyph->object, string_start->object));
11919 }
11920 }
11921
11922 if (cursor != NULL)
11923 {
11924 glyph = cursor;
11925 x = cursor_x;
11926 }
11927 else if (row->ends_in_ellipsis_p && glyph == end)
11928 {
11929 /* Scan back over the ellipsis glyphs, decrementing positions. */
11930 while (glyph > row->glyphs[TEXT_AREA]
11931 && (glyph - 1)->charpos == last_pos)
11932 glyph--, x -= glyph->pixel_width;
11933 /* That loop always goes one position too far,
11934 including the glyph before the ellipsis.
11935 So scan forward over that one. */
11936 x += glyph->pixel_width;
11937 glyph++;
11938 }
11939 else if (string_start
11940 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11941 {
11942 /* We may have skipped over point because the previous glyphs
11943 are from string. As there's no easy way to know the
11944 character position of the current glyph, find the correct
11945 glyph on point by scanning from string_start again. */
11946 Lisp_Object limit;
11947 Lisp_Object string;
11948 struct glyph *stop = glyph;
11949 int pos;
11950
11951 limit = make_number (pt_old + 1);
11952 glyph = string_start;
11953 x = string_start_x;
11954 string = glyph->object;
11955 pos = string_buffer_position (w, string, string_before_pos);
11956 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11957 because we always put cursor after overlay strings. */
11958 while (pos == 0 && glyph < stop)
11959 {
11960 string = glyph->object;
11961 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11962 if (glyph < stop)
11963 pos = string_buffer_position (w, glyph->object, string_before_pos);
11964 }
11965
11966 while (glyph < stop)
11967 {
11968 pos = XINT (Fnext_single_char_property_change
11969 (make_number (pos), Qdisplay, Qnil, limit));
11970 if (pos > pt_old)
11971 break;
11972 /* Skip glyphs from the same string. */
11973 string = glyph->object;
11974 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11975 /* Skip glyphs from an overlay. */
11976 while (glyph < stop
11977 && ! string_buffer_position (w, glyph->object, pos))
11978 {
11979 string = glyph->object;
11980 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11981 }
11982 }
11983
11984 /* If we reached the end of the line, and end was from a string,
11985 cursor is not on this line. */
11986 if (glyph == end && row->continued_p)
11987 return 0;
11988 }
11989
11990 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11991 w->cursor.x = x;
11992 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11993 w->cursor.y = row->y + dy;
11994
11995 if (w == XWINDOW (selected_window))
11996 {
11997 if (!row->continued_p
11998 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11999 && row->x == 0)
12000 {
12001 this_line_buffer = XBUFFER (w->buffer);
12002
12003 CHARPOS (this_line_start_pos)
12004 = MATRIX_ROW_START_CHARPOS (row) + delta;
12005 BYTEPOS (this_line_start_pos)
12006 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12007
12008 CHARPOS (this_line_end_pos)
12009 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12010 BYTEPOS (this_line_end_pos)
12011 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12012
12013 this_line_y = w->cursor.y;
12014 this_line_pixel_height = row->height;
12015 this_line_vpos = w->cursor.vpos;
12016 this_line_start_x = row->x;
12017 }
12018 else
12019 CHARPOS (this_line_start_pos) = 0;
12020 }
12021
12022 return 1;
12023 }
12024
12025
12026 /* Run window scroll functions, if any, for WINDOW with new window
12027 start STARTP. Sets the window start of WINDOW to that position.
12028
12029 We assume that the window's buffer is really current. */
12030
12031 static INLINE struct text_pos
12032 run_window_scroll_functions (window, startp)
12033 Lisp_Object window;
12034 struct text_pos startp;
12035 {
12036 struct window *w = XWINDOW (window);
12037 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12038
12039 if (current_buffer != XBUFFER (w->buffer))
12040 abort ();
12041
12042 if (!NILP (Vwindow_scroll_functions))
12043 {
12044 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12045 make_number (CHARPOS (startp)));
12046 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12047 /* In case the hook functions switch buffers. */
12048 if (current_buffer != XBUFFER (w->buffer))
12049 set_buffer_internal_1 (XBUFFER (w->buffer));
12050 }
12051
12052 return startp;
12053 }
12054
12055
12056 /* Make sure the line containing the cursor is fully visible.
12057 A value of 1 means there is nothing to be done.
12058 (Either the line is fully visible, or it cannot be made so,
12059 or we cannot tell.)
12060
12061 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12062 is higher than window.
12063
12064 A value of 0 means the caller should do scrolling
12065 as if point had gone off the screen. */
12066
12067 static int
12068 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12069 struct window *w;
12070 int force_p;
12071 int current_matrix_p;
12072 {
12073 struct glyph_matrix *matrix;
12074 struct glyph_row *row;
12075 int window_height;
12076
12077 if (!make_cursor_line_fully_visible_p)
12078 return 1;
12079
12080 /* It's not always possible to find the cursor, e.g, when a window
12081 is full of overlay strings. Don't do anything in that case. */
12082 if (w->cursor.vpos < 0)
12083 return 1;
12084
12085 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12086 row = MATRIX_ROW (matrix, w->cursor.vpos);
12087
12088 /* If the cursor row is not partially visible, there's nothing to do. */
12089 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12090 return 1;
12091
12092 /* If the row the cursor is in is taller than the window's height,
12093 it's not clear what to do, so do nothing. */
12094 window_height = window_box_height (w);
12095 if (row->height >= window_height)
12096 {
12097 if (!force_p || MINI_WINDOW_P (w)
12098 || w->vscroll || w->cursor.vpos == 0)
12099 return 1;
12100 }
12101 return 0;
12102
12103 #if 0
12104 /* This code used to try to scroll the window just enough to make
12105 the line visible. It returned 0 to say that the caller should
12106 allocate larger glyph matrices. */
12107
12108 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12109 {
12110 int dy = row->height - row->visible_height;
12111 w->vscroll = 0;
12112 w->cursor.y += dy;
12113 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12114 }
12115 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12116 {
12117 int dy = - (row->height - row->visible_height);
12118 w->vscroll = dy;
12119 w->cursor.y += dy;
12120 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12121 }
12122
12123 /* When we change the cursor y-position of the selected window,
12124 change this_line_y as well so that the display optimization for
12125 the cursor line of the selected window in redisplay_internal uses
12126 the correct y-position. */
12127 if (w == XWINDOW (selected_window))
12128 this_line_y = w->cursor.y;
12129
12130 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12131 redisplay with larger matrices. */
12132 if (matrix->nrows < required_matrix_height (w))
12133 {
12134 fonts_changed_p = 1;
12135 return 0;
12136 }
12137
12138 return 1;
12139 #endif /* 0 */
12140 }
12141
12142
12143 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12144 non-zero means only WINDOW is redisplayed in redisplay_internal.
12145 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12146 in redisplay_window to bring a partially visible line into view in
12147 the case that only the cursor has moved.
12148
12149 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12150 last screen line's vertical height extends past the end of the screen.
12151
12152 Value is
12153
12154 1 if scrolling succeeded
12155
12156 0 if scrolling didn't find point.
12157
12158 -1 if new fonts have been loaded so that we must interrupt
12159 redisplay, adjust glyph matrices, and try again. */
12160
12161 enum
12162 {
12163 SCROLLING_SUCCESS,
12164 SCROLLING_FAILED,
12165 SCROLLING_NEED_LARGER_MATRICES
12166 };
12167
12168 static int
12169 try_scrolling (window, just_this_one_p, scroll_conservatively,
12170 scroll_step, temp_scroll_step, last_line_misfit)
12171 Lisp_Object window;
12172 int just_this_one_p;
12173 EMACS_INT scroll_conservatively, scroll_step;
12174 int temp_scroll_step;
12175 int last_line_misfit;
12176 {
12177 struct window *w = XWINDOW (window);
12178 struct frame *f = XFRAME (w->frame);
12179 struct text_pos scroll_margin_pos;
12180 struct text_pos pos;
12181 struct text_pos startp;
12182 struct it it;
12183 Lisp_Object window_end;
12184 int this_scroll_margin;
12185 int dy = 0;
12186 int scroll_max;
12187 int rc;
12188 int amount_to_scroll = 0;
12189 Lisp_Object aggressive;
12190 int height;
12191 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12192
12193 #if GLYPH_DEBUG
12194 debug_method_add (w, "try_scrolling");
12195 #endif
12196
12197 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12198
12199 /* Compute scroll margin height in pixels. We scroll when point is
12200 within this distance from the top or bottom of the window. */
12201 if (scroll_margin > 0)
12202 {
12203 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12204 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12205 }
12206 else
12207 this_scroll_margin = 0;
12208
12209 /* Force scroll_conservatively to have a reasonable value so it doesn't
12210 cause an overflow while computing how much to scroll. */
12211 if (scroll_conservatively)
12212 scroll_conservatively = min (scroll_conservatively,
12213 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12214
12215 /* Compute how much we should try to scroll maximally to bring point
12216 into view. */
12217 if (scroll_step || scroll_conservatively || temp_scroll_step)
12218 scroll_max = max (scroll_step,
12219 max (scroll_conservatively, temp_scroll_step));
12220 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12221 || NUMBERP (current_buffer->scroll_up_aggressively))
12222 /* We're trying to scroll because of aggressive scrolling
12223 but no scroll_step is set. Choose an arbitrary one. Maybe
12224 there should be a variable for this. */
12225 scroll_max = 10;
12226 else
12227 scroll_max = 0;
12228 scroll_max *= FRAME_LINE_HEIGHT (f);
12229
12230 /* Decide whether we have to scroll down. Start at the window end
12231 and move this_scroll_margin up to find the position of the scroll
12232 margin. */
12233 window_end = Fwindow_end (window, Qt);
12234
12235 too_near_end:
12236
12237 CHARPOS (scroll_margin_pos) = XINT (window_end);
12238 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12239
12240 if (this_scroll_margin || extra_scroll_margin_lines)
12241 {
12242 start_display (&it, w, scroll_margin_pos);
12243 if (this_scroll_margin)
12244 move_it_vertically_backward (&it, this_scroll_margin);
12245 if (extra_scroll_margin_lines)
12246 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12247 scroll_margin_pos = it.current.pos;
12248 }
12249
12250 if (PT >= CHARPOS (scroll_margin_pos))
12251 {
12252 int y0;
12253
12254 /* Point is in the scroll margin at the bottom of the window, or
12255 below. Compute a new window start that makes point visible. */
12256
12257 /* Compute the distance from the scroll margin to PT.
12258 Give up if the distance is greater than scroll_max. */
12259 start_display (&it, w, scroll_margin_pos);
12260 y0 = it.current_y;
12261 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12262 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12263
12264 /* To make point visible, we have to move the window start
12265 down so that the line the cursor is in is visible, which
12266 means we have to add in the height of the cursor line. */
12267 dy = line_bottom_y (&it) - y0;
12268
12269 if (dy > scroll_max)
12270 return SCROLLING_FAILED;
12271
12272 /* Move the window start down. If scrolling conservatively,
12273 move it just enough down to make point visible. If
12274 scroll_step is set, move it down by scroll_step. */
12275 start_display (&it, w, startp);
12276
12277 if (scroll_conservatively)
12278 /* Set AMOUNT_TO_SCROLL to at least one line,
12279 and at most scroll_conservatively lines. */
12280 amount_to_scroll
12281 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12282 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12283 else if (scroll_step || temp_scroll_step)
12284 amount_to_scroll = scroll_max;
12285 else
12286 {
12287 aggressive = current_buffer->scroll_up_aggressively;
12288 height = WINDOW_BOX_TEXT_HEIGHT (w);
12289 if (NUMBERP (aggressive))
12290 {
12291 double float_amount = XFLOATINT (aggressive) * height;
12292 amount_to_scroll = float_amount;
12293 if (amount_to_scroll == 0 && float_amount > 0)
12294 amount_to_scroll = 1;
12295 }
12296 }
12297
12298 if (amount_to_scroll <= 0)
12299 return SCROLLING_FAILED;
12300
12301 /* If moving by amount_to_scroll leaves STARTP unchanged,
12302 move it down one screen line. */
12303
12304 move_it_vertically (&it, amount_to_scroll);
12305 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12306 move_it_by_lines (&it, 1, 1);
12307 startp = it.current.pos;
12308 }
12309 else
12310 {
12311 /* See if point is inside the scroll margin at the top of the
12312 window. */
12313 scroll_margin_pos = startp;
12314 if (this_scroll_margin)
12315 {
12316 start_display (&it, w, startp);
12317 move_it_vertically (&it, this_scroll_margin);
12318 scroll_margin_pos = it.current.pos;
12319 }
12320
12321 if (PT < CHARPOS (scroll_margin_pos))
12322 {
12323 /* Point is in the scroll margin at the top of the window or
12324 above what is displayed in the window. */
12325 int y0;
12326
12327 /* Compute the vertical distance from PT to the scroll
12328 margin position. Give up if distance is greater than
12329 scroll_max. */
12330 SET_TEXT_POS (pos, PT, PT_BYTE);
12331 start_display (&it, w, pos);
12332 y0 = it.current_y;
12333 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12334 it.last_visible_y, -1,
12335 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12336 dy = it.current_y - y0;
12337 if (dy > scroll_max)
12338 return SCROLLING_FAILED;
12339
12340 /* Compute new window start. */
12341 start_display (&it, w, startp);
12342
12343 if (scroll_conservatively)
12344 amount_to_scroll
12345 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12346 else if (scroll_step || temp_scroll_step)
12347 amount_to_scroll = scroll_max;
12348 else
12349 {
12350 aggressive = current_buffer->scroll_down_aggressively;
12351 height = WINDOW_BOX_TEXT_HEIGHT (w);
12352 if (NUMBERP (aggressive))
12353 {
12354 double float_amount = XFLOATINT (aggressive) * height;
12355 amount_to_scroll = float_amount;
12356 if (amount_to_scroll == 0 && float_amount > 0)
12357 amount_to_scroll = 1;
12358 }
12359 }
12360
12361 if (amount_to_scroll <= 0)
12362 return SCROLLING_FAILED;
12363
12364 move_it_vertically_backward (&it, amount_to_scroll);
12365 startp = it.current.pos;
12366 }
12367 }
12368
12369 /* Run window scroll functions. */
12370 startp = run_window_scroll_functions (window, startp);
12371
12372 /* Display the window. Give up if new fonts are loaded, or if point
12373 doesn't appear. */
12374 if (!try_window (window, startp, 0))
12375 rc = SCROLLING_NEED_LARGER_MATRICES;
12376 else if (w->cursor.vpos < 0)
12377 {
12378 clear_glyph_matrix (w->desired_matrix);
12379 rc = SCROLLING_FAILED;
12380 }
12381 else
12382 {
12383 /* Maybe forget recorded base line for line number display. */
12384 if (!just_this_one_p
12385 || current_buffer->clip_changed
12386 || BEG_UNCHANGED < CHARPOS (startp))
12387 w->base_line_number = Qnil;
12388
12389 /* If cursor ends up on a partially visible line,
12390 treat that as being off the bottom of the screen. */
12391 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12392 {
12393 clear_glyph_matrix (w->desired_matrix);
12394 ++extra_scroll_margin_lines;
12395 goto too_near_end;
12396 }
12397 rc = SCROLLING_SUCCESS;
12398 }
12399
12400 return rc;
12401 }
12402
12403
12404 /* Compute a suitable window start for window W if display of W starts
12405 on a continuation line. Value is non-zero if a new window start
12406 was computed.
12407
12408 The new window start will be computed, based on W's width, starting
12409 from the start of the continued line. It is the start of the
12410 screen line with the minimum distance from the old start W->start. */
12411
12412 static int
12413 compute_window_start_on_continuation_line (w)
12414 struct window *w;
12415 {
12416 struct text_pos pos, start_pos;
12417 int window_start_changed_p = 0;
12418
12419 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12420
12421 /* If window start is on a continuation line... Window start may be
12422 < BEGV in case there's invisible text at the start of the
12423 buffer (M-x rmail, for example). */
12424 if (CHARPOS (start_pos) > BEGV
12425 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12426 {
12427 struct it it;
12428 struct glyph_row *row;
12429
12430 /* Handle the case that the window start is out of range. */
12431 if (CHARPOS (start_pos) < BEGV)
12432 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12433 else if (CHARPOS (start_pos) > ZV)
12434 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12435
12436 /* Find the start of the continued line. This should be fast
12437 because scan_buffer is fast (newline cache). */
12438 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12439 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12440 row, DEFAULT_FACE_ID);
12441 reseat_at_previous_visible_line_start (&it);
12442
12443 /* If the line start is "too far" away from the window start,
12444 say it takes too much time to compute a new window start. */
12445 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12446 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12447 {
12448 int min_distance, distance;
12449
12450 /* Move forward by display lines to find the new window
12451 start. If window width was enlarged, the new start can
12452 be expected to be > the old start. If window width was
12453 decreased, the new window start will be < the old start.
12454 So, we're looking for the display line start with the
12455 minimum distance from the old window start. */
12456 pos = it.current.pos;
12457 min_distance = INFINITY;
12458 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12459 distance < min_distance)
12460 {
12461 min_distance = distance;
12462 pos = it.current.pos;
12463 move_it_by_lines (&it, 1, 0);
12464 }
12465
12466 /* Set the window start there. */
12467 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12468 window_start_changed_p = 1;
12469 }
12470 }
12471
12472 return window_start_changed_p;
12473 }
12474
12475
12476 /* Try cursor movement in case text has not changed in window WINDOW,
12477 with window start STARTP. Value is
12478
12479 CURSOR_MOVEMENT_SUCCESS if successful
12480
12481 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12482
12483 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12484 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12485 we want to scroll as if scroll-step were set to 1. See the code.
12486
12487 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12488 which case we have to abort this redisplay, and adjust matrices
12489 first. */
12490
12491 enum
12492 {
12493 CURSOR_MOVEMENT_SUCCESS,
12494 CURSOR_MOVEMENT_CANNOT_BE_USED,
12495 CURSOR_MOVEMENT_MUST_SCROLL,
12496 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12497 };
12498
12499 static int
12500 try_cursor_movement (window, startp, scroll_step)
12501 Lisp_Object window;
12502 struct text_pos startp;
12503 int *scroll_step;
12504 {
12505 struct window *w = XWINDOW (window);
12506 struct frame *f = XFRAME (w->frame);
12507 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12508
12509 #if GLYPH_DEBUG
12510 if (inhibit_try_cursor_movement)
12511 return rc;
12512 #endif
12513
12514 /* Handle case where text has not changed, only point, and it has
12515 not moved off the frame. */
12516 if (/* Point may be in this window. */
12517 PT >= CHARPOS (startp)
12518 /* Selective display hasn't changed. */
12519 && !current_buffer->clip_changed
12520 /* Function force-mode-line-update is used to force a thorough
12521 redisplay. It sets either windows_or_buffers_changed or
12522 update_mode_lines. So don't take a shortcut here for these
12523 cases. */
12524 && !update_mode_lines
12525 && !windows_or_buffers_changed
12526 && !cursor_type_changed
12527 /* Can't use this case if highlighting a region. When a
12528 region exists, cursor movement has to do more than just
12529 set the cursor. */
12530 && !(!NILP (Vtransient_mark_mode)
12531 && !NILP (current_buffer->mark_active))
12532 && NILP (w->region_showing)
12533 && NILP (Vshow_trailing_whitespace)
12534 /* Right after splitting windows, last_point may be nil. */
12535 && INTEGERP (w->last_point)
12536 /* This code is not used for mini-buffer for the sake of the case
12537 of redisplaying to replace an echo area message; since in
12538 that case the mini-buffer contents per se are usually
12539 unchanged. This code is of no real use in the mini-buffer
12540 since the handling of this_line_start_pos, etc., in redisplay
12541 handles the same cases. */
12542 && !EQ (window, minibuf_window)
12543 /* When splitting windows or for new windows, it happens that
12544 redisplay is called with a nil window_end_vpos or one being
12545 larger than the window. This should really be fixed in
12546 window.c. I don't have this on my list, now, so we do
12547 approximately the same as the old redisplay code. --gerd. */
12548 && INTEGERP (w->window_end_vpos)
12549 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12550 && (FRAME_WINDOW_P (f)
12551 || !overlay_arrow_in_current_buffer_p ()))
12552 {
12553 int this_scroll_margin, top_scroll_margin;
12554 struct glyph_row *row = NULL;
12555
12556 #if GLYPH_DEBUG
12557 debug_method_add (w, "cursor movement");
12558 #endif
12559
12560 /* Scroll if point within this distance from the top or bottom
12561 of the window. This is a pixel value. */
12562 this_scroll_margin = max (0, scroll_margin);
12563 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12564 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12565
12566 top_scroll_margin = this_scroll_margin;
12567 if (WINDOW_WANTS_HEADER_LINE_P (w))
12568 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12569
12570 /* Start with the row the cursor was displayed during the last
12571 not paused redisplay. Give up if that row is not valid. */
12572 if (w->last_cursor.vpos < 0
12573 || w->last_cursor.vpos >= w->current_matrix->nrows)
12574 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12575 else
12576 {
12577 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12578 if (row->mode_line_p)
12579 ++row;
12580 if (!row->enabled_p)
12581 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12582 }
12583
12584 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12585 {
12586 int scroll_p = 0;
12587 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12588
12589 if (PT > XFASTINT (w->last_point))
12590 {
12591 /* Point has moved forward. */
12592 while (MATRIX_ROW_END_CHARPOS (row) < PT
12593 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12594 {
12595 xassert (row->enabled_p);
12596 ++row;
12597 }
12598
12599 /* The end position of a row equals the start position
12600 of the next row. If PT is there, we would rather
12601 display it in the next line. */
12602 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12603 && MATRIX_ROW_END_CHARPOS (row) == PT
12604 && !cursor_row_p (w, row))
12605 ++row;
12606
12607 /* If within the scroll margin, scroll. Note that
12608 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12609 the next line would be drawn, and that
12610 this_scroll_margin can be zero. */
12611 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12612 || PT > MATRIX_ROW_END_CHARPOS (row)
12613 /* Line is completely visible last line in window
12614 and PT is to be set in the next line. */
12615 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12616 && PT == MATRIX_ROW_END_CHARPOS (row)
12617 && !row->ends_at_zv_p
12618 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12619 scroll_p = 1;
12620 }
12621 else if (PT < XFASTINT (w->last_point))
12622 {
12623 /* Cursor has to be moved backward. Note that PT >=
12624 CHARPOS (startp) because of the outer if-statement. */
12625 while (!row->mode_line_p
12626 && (MATRIX_ROW_START_CHARPOS (row) > PT
12627 || (MATRIX_ROW_START_CHARPOS (row) == PT
12628 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12629 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12630 row > w->current_matrix->rows
12631 && (row-1)->ends_in_newline_from_string_p))))
12632 && (row->y > top_scroll_margin
12633 || CHARPOS (startp) == BEGV))
12634 {
12635 xassert (row->enabled_p);
12636 --row;
12637 }
12638
12639 /* Consider the following case: Window starts at BEGV,
12640 there is invisible, intangible text at BEGV, so that
12641 display starts at some point START > BEGV. It can
12642 happen that we are called with PT somewhere between
12643 BEGV and START. Try to handle that case. */
12644 if (row < w->current_matrix->rows
12645 || row->mode_line_p)
12646 {
12647 row = w->current_matrix->rows;
12648 if (row->mode_line_p)
12649 ++row;
12650 }
12651
12652 /* Due to newlines in overlay strings, we may have to
12653 skip forward over overlay strings. */
12654 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12655 && MATRIX_ROW_END_CHARPOS (row) == PT
12656 && !cursor_row_p (w, row))
12657 ++row;
12658
12659 /* If within the scroll margin, scroll. */
12660 if (row->y < top_scroll_margin
12661 && CHARPOS (startp) != BEGV)
12662 scroll_p = 1;
12663 }
12664 else
12665 {
12666 /* Cursor did not move. So don't scroll even if cursor line
12667 is partially visible, as it was so before. */
12668 rc = CURSOR_MOVEMENT_SUCCESS;
12669 }
12670
12671 if (PT < MATRIX_ROW_START_CHARPOS (row)
12672 || PT > MATRIX_ROW_END_CHARPOS (row))
12673 {
12674 /* if PT is not in the glyph row, give up. */
12675 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12676 }
12677 else if (rc != CURSOR_MOVEMENT_SUCCESS
12678 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12679 && make_cursor_line_fully_visible_p)
12680 {
12681 if (PT == MATRIX_ROW_END_CHARPOS (row)
12682 && !row->ends_at_zv_p
12683 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12684 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12685 else if (row->height > window_box_height (w))
12686 {
12687 /* If we end up in a partially visible line, let's
12688 make it fully visible, except when it's taller
12689 than the window, in which case we can't do much
12690 about it. */
12691 *scroll_step = 1;
12692 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12693 }
12694 else
12695 {
12696 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12697 if (!cursor_row_fully_visible_p (w, 0, 1))
12698 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12699 else
12700 rc = CURSOR_MOVEMENT_SUCCESS;
12701 }
12702 }
12703 else if (scroll_p)
12704 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12705 else
12706 {
12707 do
12708 {
12709 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12710 {
12711 rc = CURSOR_MOVEMENT_SUCCESS;
12712 break;
12713 }
12714 ++row;
12715 }
12716 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12717 && MATRIX_ROW_START_CHARPOS (row) == PT
12718 && cursor_row_p (w, row));
12719 }
12720 }
12721 }
12722
12723 return rc;
12724 }
12725
12726 void
12727 set_vertical_scroll_bar (w)
12728 struct window *w;
12729 {
12730 int start, end, whole;
12731
12732 /* Calculate the start and end positions for the current window.
12733 At some point, it would be nice to choose between scrollbars
12734 which reflect the whole buffer size, with special markers
12735 indicating narrowing, and scrollbars which reflect only the
12736 visible region.
12737
12738 Note that mini-buffers sometimes aren't displaying any text. */
12739 if (!MINI_WINDOW_P (w)
12740 || (w == XWINDOW (minibuf_window)
12741 && NILP (echo_area_buffer[0])))
12742 {
12743 struct buffer *buf = XBUFFER (w->buffer);
12744 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12745 start = marker_position (w->start) - BUF_BEGV (buf);
12746 /* I don't think this is guaranteed to be right. For the
12747 moment, we'll pretend it is. */
12748 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12749
12750 if (end < start)
12751 end = start;
12752 if (whole < (end - start))
12753 whole = end - start;
12754 }
12755 else
12756 start = end = whole = 0;
12757
12758 /* Indicate what this scroll bar ought to be displaying now. */
12759 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12760 }
12761
12762
12763 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12764 selected_window is redisplayed.
12765
12766 We can return without actually redisplaying the window if
12767 fonts_changed_p is nonzero. In that case, redisplay_internal will
12768 retry. */
12769
12770 static void
12771 redisplay_window (window, just_this_one_p)
12772 Lisp_Object window;
12773 int just_this_one_p;
12774 {
12775 struct window *w = XWINDOW (window);
12776 struct frame *f = XFRAME (w->frame);
12777 struct buffer *buffer = XBUFFER (w->buffer);
12778 struct buffer *old = current_buffer;
12779 struct text_pos lpoint, opoint, startp;
12780 int update_mode_line;
12781 int tem;
12782 struct it it;
12783 /* Record it now because it's overwritten. */
12784 int current_matrix_up_to_date_p = 0;
12785 int used_current_matrix_p = 0;
12786 /* This is less strict than current_matrix_up_to_date_p.
12787 It indictes that the buffer contents and narrowing are unchanged. */
12788 int buffer_unchanged_p = 0;
12789 int temp_scroll_step = 0;
12790 int count = SPECPDL_INDEX ();
12791 int rc;
12792 int centering_position = -1;
12793 int last_line_misfit = 0;
12794 int save_beg_unchanged, save_end_unchanged;
12795
12796 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12797 opoint = lpoint;
12798
12799 /* W must be a leaf window here. */
12800 xassert (!NILP (w->buffer));
12801 #if GLYPH_DEBUG
12802 *w->desired_matrix->method = 0;
12803 #endif
12804
12805 specbind (Qinhibit_point_motion_hooks, Qt);
12806
12807 reconsider_clip_changes (w, buffer);
12808
12809 /* Has the mode line to be updated? */
12810 update_mode_line = (!NILP (w->update_mode_line)
12811 || update_mode_lines
12812 || buffer->clip_changed
12813 || buffer->prevent_redisplay_optimizations_p);
12814
12815 if (MINI_WINDOW_P (w))
12816 {
12817 if (w == XWINDOW (echo_area_window)
12818 && !NILP (echo_area_buffer[0]))
12819 {
12820 if (update_mode_line)
12821 /* We may have to update a tty frame's menu bar or a
12822 tool-bar. Example `M-x C-h C-h C-g'. */
12823 goto finish_menu_bars;
12824 else
12825 /* We've already displayed the echo area glyphs in this window. */
12826 goto finish_scroll_bars;
12827 }
12828 else if ((w != XWINDOW (minibuf_window)
12829 || minibuf_level == 0)
12830 /* When buffer is nonempty, redisplay window normally. */
12831 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12832 /* Quail displays non-mini buffers in minibuffer window.
12833 In that case, redisplay the window normally. */
12834 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12835 {
12836 /* W is a mini-buffer window, but it's not active, so clear
12837 it. */
12838 int yb = window_text_bottom_y (w);
12839 struct glyph_row *row;
12840 int y;
12841
12842 for (y = 0, row = w->desired_matrix->rows;
12843 y < yb;
12844 y += row->height, ++row)
12845 blank_row (w, row, y);
12846 goto finish_scroll_bars;
12847 }
12848
12849 clear_glyph_matrix (w->desired_matrix);
12850 }
12851
12852 /* Otherwise set up data on this window; select its buffer and point
12853 value. */
12854 /* Really select the buffer, for the sake of buffer-local
12855 variables. */
12856 set_buffer_internal_1 (XBUFFER (w->buffer));
12857 SET_TEXT_POS (opoint, PT, PT_BYTE);
12858
12859 save_beg_unchanged = BEG_UNCHANGED;
12860 save_end_unchanged = END_UNCHANGED;
12861
12862 current_matrix_up_to_date_p
12863 = (!NILP (w->window_end_valid)
12864 && !current_buffer->clip_changed
12865 && !current_buffer->prevent_redisplay_optimizations_p
12866 && XFASTINT (w->last_modified) >= MODIFF
12867 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12868
12869 buffer_unchanged_p
12870 = (!NILP (w->window_end_valid)
12871 && !current_buffer->clip_changed
12872 && XFASTINT (w->last_modified) >= MODIFF
12873 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12874
12875 /* When windows_or_buffers_changed is non-zero, we can't rely on
12876 the window end being valid, so set it to nil there. */
12877 if (windows_or_buffers_changed)
12878 {
12879 /* If window starts on a continuation line, maybe adjust the
12880 window start in case the window's width changed. */
12881 if (XMARKER (w->start)->buffer == current_buffer)
12882 compute_window_start_on_continuation_line (w);
12883
12884 w->window_end_valid = Qnil;
12885 }
12886
12887 /* Some sanity checks. */
12888 CHECK_WINDOW_END (w);
12889 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12890 abort ();
12891 if (BYTEPOS (opoint) < CHARPOS (opoint))
12892 abort ();
12893
12894 /* If %c is in mode line, update it if needed. */
12895 if (!NILP (w->column_number_displayed)
12896 /* This alternative quickly identifies a common case
12897 where no change is needed. */
12898 && !(PT == XFASTINT (w->last_point)
12899 && XFASTINT (w->last_modified) >= MODIFF
12900 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12901 && (XFASTINT (w->column_number_displayed)
12902 != (int) current_column ())) /* iftc */
12903 update_mode_line = 1;
12904
12905 /* Count number of windows showing the selected buffer. An indirect
12906 buffer counts as its base buffer. */
12907 if (!just_this_one_p)
12908 {
12909 struct buffer *current_base, *window_base;
12910 current_base = current_buffer;
12911 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12912 if (current_base->base_buffer)
12913 current_base = current_base->base_buffer;
12914 if (window_base->base_buffer)
12915 window_base = window_base->base_buffer;
12916 if (current_base == window_base)
12917 buffer_shared++;
12918 }
12919
12920 /* Point refers normally to the selected window. For any other
12921 window, set up appropriate value. */
12922 if (!EQ (window, selected_window))
12923 {
12924 int new_pt = XMARKER (w->pointm)->charpos;
12925 int new_pt_byte = marker_byte_position (w->pointm);
12926 if (new_pt < BEGV)
12927 {
12928 new_pt = BEGV;
12929 new_pt_byte = BEGV_BYTE;
12930 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12931 }
12932 else if (new_pt > (ZV - 1))
12933 {
12934 new_pt = ZV;
12935 new_pt_byte = ZV_BYTE;
12936 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12937 }
12938
12939 /* We don't use SET_PT so that the point-motion hooks don't run. */
12940 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12941 }
12942
12943 /* If any of the character widths specified in the display table
12944 have changed, invalidate the width run cache. It's true that
12945 this may be a bit late to catch such changes, but the rest of
12946 redisplay goes (non-fatally) haywire when the display table is
12947 changed, so why should we worry about doing any better? */
12948 if (current_buffer->width_run_cache)
12949 {
12950 struct Lisp_Char_Table *disptab = buffer_display_table ();
12951
12952 if (! disptab_matches_widthtab (disptab,
12953 XVECTOR (current_buffer->width_table)))
12954 {
12955 invalidate_region_cache (current_buffer,
12956 current_buffer->width_run_cache,
12957 BEG, Z);
12958 recompute_width_table (current_buffer, disptab);
12959 }
12960 }
12961
12962 /* If window-start is screwed up, choose a new one. */
12963 if (XMARKER (w->start)->buffer != current_buffer)
12964 goto recenter;
12965
12966 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12967
12968 /* If someone specified a new starting point but did not insist,
12969 check whether it can be used. */
12970 if (!NILP (w->optional_new_start)
12971 && CHARPOS (startp) >= BEGV
12972 && CHARPOS (startp) <= ZV)
12973 {
12974 w->optional_new_start = Qnil;
12975 start_display (&it, w, startp);
12976 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12977 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12978 if (IT_CHARPOS (it) == PT)
12979 w->force_start = Qt;
12980 /* IT may overshoot PT if text at PT is invisible. */
12981 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12982 w->force_start = Qt;
12983 }
12984
12985 force_start:
12986
12987 /* Handle case where place to start displaying has been specified,
12988 unless the specified location is outside the accessible range. */
12989 if (!NILP (w->force_start)
12990 || w->frozen_window_start_p)
12991 {
12992 /* We set this later on if we have to adjust point. */
12993 int new_vpos = -1;
12994 int val;
12995
12996 w->force_start = Qnil;
12997 w->vscroll = 0;
12998 w->window_end_valid = Qnil;
12999
13000 /* Forget any recorded base line for line number display. */
13001 if (!buffer_unchanged_p)
13002 w->base_line_number = Qnil;
13003
13004 /* Redisplay the mode line. Select the buffer properly for that.
13005 Also, run the hook window-scroll-functions
13006 because we have scrolled. */
13007 /* Note, we do this after clearing force_start because
13008 if there's an error, it is better to forget about force_start
13009 than to get into an infinite loop calling the hook functions
13010 and having them get more errors. */
13011 if (!update_mode_line
13012 || ! NILP (Vwindow_scroll_functions))
13013 {
13014 update_mode_line = 1;
13015 w->update_mode_line = Qt;
13016 startp = run_window_scroll_functions (window, startp);
13017 }
13018
13019 w->last_modified = make_number (0);
13020 w->last_overlay_modified = make_number (0);
13021 if (CHARPOS (startp) < BEGV)
13022 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13023 else if (CHARPOS (startp) > ZV)
13024 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13025
13026 /* Redisplay, then check if cursor has been set during the
13027 redisplay. Give up if new fonts were loaded. */
13028 val = try_window (window, startp, 1);
13029 if (!val)
13030 {
13031 w->force_start = Qt;
13032 clear_glyph_matrix (w->desired_matrix);
13033 goto need_larger_matrices;
13034 }
13035 /* Point was outside the scroll margins. */
13036 if (val < 0)
13037 new_vpos = window_box_height (w) / 2;
13038
13039 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13040 {
13041 /* If point does not appear, try to move point so it does
13042 appear. The desired matrix has been built above, so we
13043 can use it here. */
13044 new_vpos = window_box_height (w) / 2;
13045 }
13046
13047 if (!cursor_row_fully_visible_p (w, 0, 0))
13048 {
13049 /* Point does appear, but on a line partly visible at end of window.
13050 Move it back to a fully-visible line. */
13051 new_vpos = window_box_height (w);
13052 }
13053
13054 /* If we need to move point for either of the above reasons,
13055 now actually do it. */
13056 if (new_vpos >= 0)
13057 {
13058 struct glyph_row *row;
13059
13060 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13061 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13062 ++row;
13063
13064 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13065 MATRIX_ROW_START_BYTEPOS (row));
13066
13067 if (w != XWINDOW (selected_window))
13068 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13069 else if (current_buffer == old)
13070 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13071
13072 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13073
13074 /* If we are highlighting the region, then we just changed
13075 the region, so redisplay to show it. */
13076 if (!NILP (Vtransient_mark_mode)
13077 && !NILP (current_buffer->mark_active))
13078 {
13079 clear_glyph_matrix (w->desired_matrix);
13080 if (!try_window (window, startp, 0))
13081 goto need_larger_matrices;
13082 }
13083 }
13084
13085 #if GLYPH_DEBUG
13086 debug_method_add (w, "forced window start");
13087 #endif
13088 goto done;
13089 }
13090
13091 /* Handle case where text has not changed, only point, and it has
13092 not moved off the frame, and we are not retrying after hscroll.
13093 (current_matrix_up_to_date_p is nonzero when retrying.) */
13094 if (current_matrix_up_to_date_p
13095 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13096 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13097 {
13098 switch (rc)
13099 {
13100 case CURSOR_MOVEMENT_SUCCESS:
13101 used_current_matrix_p = 1;
13102 goto done;
13103
13104 #if 0 /* try_cursor_movement never returns this value. */
13105 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13106 goto need_larger_matrices;
13107 #endif
13108
13109 case CURSOR_MOVEMENT_MUST_SCROLL:
13110 goto try_to_scroll;
13111
13112 default:
13113 abort ();
13114 }
13115 }
13116 /* If current starting point was originally the beginning of a line
13117 but no longer is, find a new starting point. */
13118 else if (!NILP (w->start_at_line_beg)
13119 && !(CHARPOS (startp) <= BEGV
13120 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13121 {
13122 #if GLYPH_DEBUG
13123 debug_method_add (w, "recenter 1");
13124 #endif
13125 goto recenter;
13126 }
13127
13128 /* Try scrolling with try_window_id. Value is > 0 if update has
13129 been done, it is -1 if we know that the same window start will
13130 not work. It is 0 if unsuccessful for some other reason. */
13131 else if ((tem = try_window_id (w)) != 0)
13132 {
13133 #if GLYPH_DEBUG
13134 debug_method_add (w, "try_window_id %d", tem);
13135 #endif
13136
13137 if (fonts_changed_p)
13138 goto need_larger_matrices;
13139 if (tem > 0)
13140 goto done;
13141
13142 /* Otherwise try_window_id has returned -1 which means that we
13143 don't want the alternative below this comment to execute. */
13144 }
13145 else if (CHARPOS (startp) >= BEGV
13146 && CHARPOS (startp) <= ZV
13147 && PT >= CHARPOS (startp)
13148 && (CHARPOS (startp) < ZV
13149 /* Avoid starting at end of buffer. */
13150 || CHARPOS (startp) == BEGV
13151 || (XFASTINT (w->last_modified) >= MODIFF
13152 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13153 {
13154
13155 /* If first window line is a continuation line, and window start
13156 is inside the modified region, but the first change is before
13157 current window start, we must select a new window start.
13158
13159 However, if this is the result of a down-mouse event (e.g. by
13160 extending the mouse-drag-overlay), we don't want to select a
13161 new window start, since that would change the position under
13162 the mouse, resulting in an unwanted mouse-movement rather
13163 than a simple mouse-click. */
13164 if (NILP (w->start_at_line_beg)
13165 && NILP (do_mouse_tracking)
13166 && CHARPOS (startp) > BEGV
13167 && CHARPOS (startp) > BEG + save_beg_unchanged
13168 && CHARPOS (startp) <= Z - save_end_unchanged)
13169 {
13170 w->force_start = Qt;
13171 if (XMARKER (w->start)->buffer == current_buffer)
13172 compute_window_start_on_continuation_line (w);
13173 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13174 goto force_start;
13175 }
13176
13177 #if GLYPH_DEBUG
13178 debug_method_add (w, "same window start");
13179 #endif
13180
13181 /* Try to redisplay starting at same place as before.
13182 If point has not moved off frame, accept the results. */
13183 if (!current_matrix_up_to_date_p
13184 /* Don't use try_window_reusing_current_matrix in this case
13185 because a window scroll function can have changed the
13186 buffer. */
13187 || !NILP (Vwindow_scroll_functions)
13188 || MINI_WINDOW_P (w)
13189 || !(used_current_matrix_p
13190 = try_window_reusing_current_matrix (w)))
13191 {
13192 IF_DEBUG (debug_method_add (w, "1"));
13193 if (try_window (window, startp, 1) < 0)
13194 /* -1 means we need to scroll.
13195 0 means we need new matrices, but fonts_changed_p
13196 is set in that case, so we will detect it below. */
13197 goto try_to_scroll;
13198 }
13199
13200 if (fonts_changed_p)
13201 goto need_larger_matrices;
13202
13203 if (w->cursor.vpos >= 0)
13204 {
13205 if (!just_this_one_p
13206 || current_buffer->clip_changed
13207 || BEG_UNCHANGED < CHARPOS (startp))
13208 /* Forget any recorded base line for line number display. */
13209 w->base_line_number = Qnil;
13210
13211 if (!cursor_row_fully_visible_p (w, 1, 0))
13212 {
13213 clear_glyph_matrix (w->desired_matrix);
13214 last_line_misfit = 1;
13215 }
13216 /* Drop through and scroll. */
13217 else
13218 goto done;
13219 }
13220 else
13221 clear_glyph_matrix (w->desired_matrix);
13222 }
13223
13224 try_to_scroll:
13225
13226 w->last_modified = make_number (0);
13227 w->last_overlay_modified = make_number (0);
13228
13229 /* Redisplay the mode line. Select the buffer properly for that. */
13230 if (!update_mode_line)
13231 {
13232 update_mode_line = 1;
13233 w->update_mode_line = Qt;
13234 }
13235
13236 /* Try to scroll by specified few lines. */
13237 if ((scroll_conservatively
13238 || scroll_step
13239 || temp_scroll_step
13240 || NUMBERP (current_buffer->scroll_up_aggressively)
13241 || NUMBERP (current_buffer->scroll_down_aggressively))
13242 && !current_buffer->clip_changed
13243 && CHARPOS (startp) >= BEGV
13244 && CHARPOS (startp) <= ZV)
13245 {
13246 /* The function returns -1 if new fonts were loaded, 1 if
13247 successful, 0 if not successful. */
13248 int rc = try_scrolling (window, just_this_one_p,
13249 scroll_conservatively,
13250 scroll_step,
13251 temp_scroll_step, last_line_misfit);
13252 switch (rc)
13253 {
13254 case SCROLLING_SUCCESS:
13255 goto done;
13256
13257 case SCROLLING_NEED_LARGER_MATRICES:
13258 goto need_larger_matrices;
13259
13260 case SCROLLING_FAILED:
13261 break;
13262
13263 default:
13264 abort ();
13265 }
13266 }
13267
13268 /* Finally, just choose place to start which centers point */
13269
13270 recenter:
13271 if (centering_position < 0)
13272 centering_position = window_box_height (w) / 2;
13273
13274 #if GLYPH_DEBUG
13275 debug_method_add (w, "recenter");
13276 #endif
13277
13278 /* w->vscroll = 0; */
13279
13280 /* Forget any previously recorded base line for line number display. */
13281 if (!buffer_unchanged_p)
13282 w->base_line_number = Qnil;
13283
13284 /* Move backward half the height of the window. */
13285 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13286 it.current_y = it.last_visible_y;
13287 move_it_vertically_backward (&it, centering_position);
13288 xassert (IT_CHARPOS (it) >= BEGV);
13289
13290 /* The function move_it_vertically_backward may move over more
13291 than the specified y-distance. If it->w is small, e.g. a
13292 mini-buffer window, we may end up in front of the window's
13293 display area. Start displaying at the start of the line
13294 containing PT in this case. */
13295 if (it.current_y <= 0)
13296 {
13297 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13298 move_it_vertically_backward (&it, 0);
13299 #if 0
13300 /* I think this assert is bogus if buffer contains
13301 invisible text or images. KFS. */
13302 xassert (IT_CHARPOS (it) <= PT);
13303 #endif
13304 it.current_y = 0;
13305 }
13306
13307 it.current_x = it.hpos = 0;
13308
13309 /* Set startp here explicitly in case that helps avoid an infinite loop
13310 in case the window-scroll-functions functions get errors. */
13311 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13312
13313 /* Run scroll hooks. */
13314 startp = run_window_scroll_functions (window, it.current.pos);
13315
13316 /* Redisplay the window. */
13317 if (!current_matrix_up_to_date_p
13318 || windows_or_buffers_changed
13319 || cursor_type_changed
13320 /* Don't use try_window_reusing_current_matrix in this case
13321 because it can have changed the buffer. */
13322 || !NILP (Vwindow_scroll_functions)
13323 || !just_this_one_p
13324 || MINI_WINDOW_P (w)
13325 || !(used_current_matrix_p
13326 = try_window_reusing_current_matrix (w)))
13327 try_window (window, startp, 0);
13328
13329 /* If new fonts have been loaded (due to fontsets), give up. We
13330 have to start a new redisplay since we need to re-adjust glyph
13331 matrices. */
13332 if (fonts_changed_p)
13333 goto need_larger_matrices;
13334
13335 /* If cursor did not appear assume that the middle of the window is
13336 in the first line of the window. Do it again with the next line.
13337 (Imagine a window of height 100, displaying two lines of height
13338 60. Moving back 50 from it->last_visible_y will end in the first
13339 line.) */
13340 if (w->cursor.vpos < 0)
13341 {
13342 if (!NILP (w->window_end_valid)
13343 && PT >= Z - XFASTINT (w->window_end_pos))
13344 {
13345 clear_glyph_matrix (w->desired_matrix);
13346 move_it_by_lines (&it, 1, 0);
13347 try_window (window, it.current.pos, 0);
13348 }
13349 else if (PT < IT_CHARPOS (it))
13350 {
13351 clear_glyph_matrix (w->desired_matrix);
13352 move_it_by_lines (&it, -1, 0);
13353 try_window (window, it.current.pos, 0);
13354 }
13355 else
13356 {
13357 /* Not much we can do about it. */
13358 }
13359 }
13360
13361 /* Consider the following case: Window starts at BEGV, there is
13362 invisible, intangible text at BEGV, so that display starts at
13363 some point START > BEGV. It can happen that we are called with
13364 PT somewhere between BEGV and START. Try to handle that case. */
13365 if (w->cursor.vpos < 0)
13366 {
13367 struct glyph_row *row = w->current_matrix->rows;
13368 if (row->mode_line_p)
13369 ++row;
13370 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13371 }
13372
13373 if (!cursor_row_fully_visible_p (w, 0, 0))
13374 {
13375 /* If vscroll is enabled, disable it and try again. */
13376 if (w->vscroll)
13377 {
13378 w->vscroll = 0;
13379 clear_glyph_matrix (w->desired_matrix);
13380 goto recenter;
13381 }
13382
13383 /* If centering point failed to make the whole line visible,
13384 put point at the top instead. That has to make the whole line
13385 visible, if it can be done. */
13386 if (centering_position == 0)
13387 goto done;
13388
13389 clear_glyph_matrix (w->desired_matrix);
13390 centering_position = 0;
13391 goto recenter;
13392 }
13393
13394 done:
13395
13396 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13397 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13398 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13399 ? Qt : Qnil);
13400
13401 /* Display the mode line, if we must. */
13402 if ((update_mode_line
13403 /* If window not full width, must redo its mode line
13404 if (a) the window to its side is being redone and
13405 (b) we do a frame-based redisplay. This is a consequence
13406 of how inverted lines are drawn in frame-based redisplay. */
13407 || (!just_this_one_p
13408 && !FRAME_WINDOW_P (f)
13409 && !WINDOW_FULL_WIDTH_P (w))
13410 /* Line number to display. */
13411 || INTEGERP (w->base_line_pos)
13412 /* Column number is displayed and different from the one displayed. */
13413 || (!NILP (w->column_number_displayed)
13414 && (XFASTINT (w->column_number_displayed)
13415 != (int) current_column ()))) /* iftc */
13416 /* This means that the window has a mode line. */
13417 && (WINDOW_WANTS_MODELINE_P (w)
13418 || WINDOW_WANTS_HEADER_LINE_P (w)))
13419 {
13420 display_mode_lines (w);
13421
13422 /* If mode line height has changed, arrange for a thorough
13423 immediate redisplay using the correct mode line height. */
13424 if (WINDOW_WANTS_MODELINE_P (w)
13425 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13426 {
13427 fonts_changed_p = 1;
13428 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13429 = DESIRED_MODE_LINE_HEIGHT (w);
13430 }
13431
13432 /* If top line height has changed, arrange for a thorough
13433 immediate redisplay using the correct mode line height. */
13434 if (WINDOW_WANTS_HEADER_LINE_P (w)
13435 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13436 {
13437 fonts_changed_p = 1;
13438 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13439 = DESIRED_HEADER_LINE_HEIGHT (w);
13440 }
13441
13442 if (fonts_changed_p)
13443 goto need_larger_matrices;
13444 }
13445
13446 if (!line_number_displayed
13447 && !BUFFERP (w->base_line_pos))
13448 {
13449 w->base_line_pos = Qnil;
13450 w->base_line_number = Qnil;
13451 }
13452
13453 finish_menu_bars:
13454
13455 /* When we reach a frame's selected window, redo the frame's menu bar. */
13456 if (update_mode_line
13457 && EQ (FRAME_SELECTED_WINDOW (f), window))
13458 {
13459 int redisplay_menu_p = 0;
13460 int redisplay_tool_bar_p = 0;
13461
13462 if (FRAME_WINDOW_P (f))
13463 {
13464 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13465 || defined (USE_GTK)
13466 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13467 #else
13468 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13469 #endif
13470 }
13471 else
13472 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13473
13474 if (redisplay_menu_p)
13475 display_menu_bar (w);
13476
13477 #ifdef HAVE_WINDOW_SYSTEM
13478 #ifdef USE_GTK
13479 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13480 #else
13481 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13482 && (FRAME_TOOL_BAR_LINES (f) > 0
13483 || !NILP (Vauto_resize_tool_bars));
13484
13485 #endif
13486
13487 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13488 {
13489 extern int ignore_mouse_drag_p;
13490 ignore_mouse_drag_p = 1;
13491 }
13492 #endif
13493 }
13494
13495 #ifdef HAVE_WINDOW_SYSTEM
13496 if (FRAME_WINDOW_P (f)
13497 && update_window_fringes (w, (just_this_one_p
13498 || (!used_current_matrix_p && !overlay_arrow_seen)
13499 || w->pseudo_window_p)))
13500 {
13501 update_begin (f);
13502 BLOCK_INPUT;
13503 if (draw_window_fringes (w, 1))
13504 x_draw_vertical_border (w);
13505 UNBLOCK_INPUT;
13506 update_end (f);
13507 }
13508 #endif /* HAVE_WINDOW_SYSTEM */
13509
13510 /* We go to this label, with fonts_changed_p nonzero,
13511 if it is necessary to try again using larger glyph matrices.
13512 We have to redeem the scroll bar even in this case,
13513 because the loop in redisplay_internal expects that. */
13514 need_larger_matrices:
13515 ;
13516 finish_scroll_bars:
13517
13518 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13519 {
13520 /* Set the thumb's position and size. */
13521 set_vertical_scroll_bar (w);
13522
13523 /* Note that we actually used the scroll bar attached to this
13524 window, so it shouldn't be deleted at the end of redisplay. */
13525 redeem_scroll_bar_hook (w);
13526 }
13527
13528 /* Restore current_buffer and value of point in it. */
13529 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13530 set_buffer_internal_1 (old);
13531 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13532 shorter. This can be caused by log truncation in *Messages*. */
13533 if (CHARPOS (lpoint) <= ZV)
13534 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13535
13536 unbind_to (count, Qnil);
13537 }
13538
13539
13540 /* Build the complete desired matrix of WINDOW with a window start
13541 buffer position POS.
13542
13543 Value is 1 if successful. It is zero if fonts were loaded during
13544 redisplay which makes re-adjusting glyph matrices necessary, and -1
13545 if point would appear in the scroll margins.
13546 (We check that only if CHECK_MARGINS is nonzero. */
13547
13548 int
13549 try_window (window, pos, check_margins)
13550 Lisp_Object window;
13551 struct text_pos pos;
13552 int check_margins;
13553 {
13554 struct window *w = XWINDOW (window);
13555 struct it it;
13556 struct glyph_row *last_text_row = NULL;
13557 struct frame *f = XFRAME (w->frame);
13558
13559 /* Make POS the new window start. */
13560 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13561
13562 /* Mark cursor position as unknown. No overlay arrow seen. */
13563 w->cursor.vpos = -1;
13564 overlay_arrow_seen = 0;
13565
13566 /* Initialize iterator and info to start at POS. */
13567 start_display (&it, w, pos);
13568
13569 /* Display all lines of W. */
13570 while (it.current_y < it.last_visible_y)
13571 {
13572 if (display_line (&it))
13573 last_text_row = it.glyph_row - 1;
13574 if (fonts_changed_p)
13575 return 0;
13576 }
13577
13578 /* Don't let the cursor end in the scroll margins. */
13579 if (check_margins
13580 && !MINI_WINDOW_P (w))
13581 {
13582 int this_scroll_margin;
13583
13584 this_scroll_margin = max (0, scroll_margin);
13585 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13586 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13587
13588 if ((w->cursor.y >= 0 /* not vscrolled */
13589 && w->cursor.y < this_scroll_margin
13590 && CHARPOS (pos) > BEGV
13591 && IT_CHARPOS (it) < ZV)
13592 /* rms: considering make_cursor_line_fully_visible_p here
13593 seems to give wrong results. We don't want to recenter
13594 when the last line is partly visible, we want to allow
13595 that case to be handled in the usual way. */
13596 || (w->cursor.y + 1) > it.last_visible_y)
13597 {
13598 w->cursor.vpos = -1;
13599 clear_glyph_matrix (w->desired_matrix);
13600 return -1;
13601 }
13602 }
13603
13604 /* If bottom moved off end of frame, change mode line percentage. */
13605 if (XFASTINT (w->window_end_pos) <= 0
13606 && Z != IT_CHARPOS (it))
13607 w->update_mode_line = Qt;
13608
13609 /* Set window_end_pos to the offset of the last character displayed
13610 on the window from the end of current_buffer. Set
13611 window_end_vpos to its row number. */
13612 if (last_text_row)
13613 {
13614 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13615 w->window_end_bytepos
13616 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13617 w->window_end_pos
13618 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13619 w->window_end_vpos
13620 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13621 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13622 ->displays_text_p);
13623 }
13624 else
13625 {
13626 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13627 w->window_end_pos = make_number (Z - ZV);
13628 w->window_end_vpos = make_number (0);
13629 }
13630
13631 /* But that is not valid info until redisplay finishes. */
13632 w->window_end_valid = Qnil;
13633 return 1;
13634 }
13635
13636
13637 \f
13638 /************************************************************************
13639 Window redisplay reusing current matrix when buffer has not changed
13640 ************************************************************************/
13641
13642 /* Try redisplay of window W showing an unchanged buffer with a
13643 different window start than the last time it was displayed by
13644 reusing its current matrix. Value is non-zero if successful.
13645 W->start is the new window start. */
13646
13647 static int
13648 try_window_reusing_current_matrix (w)
13649 struct window *w;
13650 {
13651 struct frame *f = XFRAME (w->frame);
13652 struct glyph_row *row, *bottom_row;
13653 struct it it;
13654 struct run run;
13655 struct text_pos start, new_start;
13656 int nrows_scrolled, i;
13657 struct glyph_row *last_text_row;
13658 struct glyph_row *last_reused_text_row;
13659 struct glyph_row *start_row;
13660 int start_vpos, min_y, max_y;
13661
13662 #if GLYPH_DEBUG
13663 if (inhibit_try_window_reusing)
13664 return 0;
13665 #endif
13666
13667 if (/* This function doesn't handle terminal frames. */
13668 !FRAME_WINDOW_P (f)
13669 /* Don't try to reuse the display if windows have been split
13670 or such. */
13671 || windows_or_buffers_changed
13672 || cursor_type_changed)
13673 return 0;
13674
13675 /* Can't do this if region may have changed. */
13676 if ((!NILP (Vtransient_mark_mode)
13677 && !NILP (current_buffer->mark_active))
13678 || !NILP (w->region_showing)
13679 || !NILP (Vshow_trailing_whitespace))
13680 return 0;
13681
13682 /* If top-line visibility has changed, give up. */
13683 if (WINDOW_WANTS_HEADER_LINE_P (w)
13684 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13685 return 0;
13686
13687 /* Give up if old or new display is scrolled vertically. We could
13688 make this function handle this, but right now it doesn't. */
13689 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13690 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13691 return 0;
13692
13693 /* The variable new_start now holds the new window start. The old
13694 start `start' can be determined from the current matrix. */
13695 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13696 start = start_row->start.pos;
13697 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13698
13699 /* Clear the desired matrix for the display below. */
13700 clear_glyph_matrix (w->desired_matrix);
13701
13702 if (CHARPOS (new_start) <= CHARPOS (start))
13703 {
13704 int first_row_y;
13705
13706 /* Don't use this method if the display starts with an ellipsis
13707 displayed for invisible text. It's not easy to handle that case
13708 below, and it's certainly not worth the effort since this is
13709 not a frequent case. */
13710 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13711 return 0;
13712
13713 IF_DEBUG (debug_method_add (w, "twu1"));
13714
13715 /* Display up to a row that can be reused. The variable
13716 last_text_row is set to the last row displayed that displays
13717 text. Note that it.vpos == 0 if or if not there is a
13718 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13719 start_display (&it, w, new_start);
13720 first_row_y = it.current_y;
13721 w->cursor.vpos = -1;
13722 last_text_row = last_reused_text_row = NULL;
13723
13724 while (it.current_y < it.last_visible_y
13725 && !fonts_changed_p)
13726 {
13727 /* If we have reached into the characters in the START row,
13728 that means the line boundaries have changed. So we
13729 can't start copying with the row START. Maybe it will
13730 work to start copying with the following row. */
13731 while (IT_CHARPOS (it) > CHARPOS (start))
13732 {
13733 /* Advance to the next row as the "start". */
13734 start_row++;
13735 start = start_row->start.pos;
13736 /* If there are no more rows to try, or just one, give up. */
13737 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13738 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13739 || CHARPOS (start) == ZV)
13740 {
13741 clear_glyph_matrix (w->desired_matrix);
13742 return 0;
13743 }
13744
13745 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13746 }
13747 /* If we have reached alignment,
13748 we can copy the rest of the rows. */
13749 if (IT_CHARPOS (it) == CHARPOS (start))
13750 break;
13751
13752 if (display_line (&it))
13753 last_text_row = it.glyph_row - 1;
13754 }
13755
13756 /* A value of current_y < last_visible_y means that we stopped
13757 at the previous window start, which in turn means that we
13758 have at least one reusable row. */
13759 if (it.current_y < it.last_visible_y)
13760 {
13761 /* IT.vpos always starts from 0; it counts text lines. */
13762 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13763
13764 /* Find PT if not already found in the lines displayed. */
13765 if (w->cursor.vpos < 0)
13766 {
13767 int dy = it.current_y - start_row->y;
13768
13769 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13770 row = row_containing_pos (w, PT, row, NULL, dy);
13771 if (row)
13772 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13773 dy, nrows_scrolled);
13774 else
13775 {
13776 clear_glyph_matrix (w->desired_matrix);
13777 return 0;
13778 }
13779 }
13780
13781 /* Scroll the display. Do it before the current matrix is
13782 changed. The problem here is that update has not yet
13783 run, i.e. part of the current matrix is not up to date.
13784 scroll_run_hook will clear the cursor, and use the
13785 current matrix to get the height of the row the cursor is
13786 in. */
13787 run.current_y = start_row->y;
13788 run.desired_y = it.current_y;
13789 run.height = it.last_visible_y - it.current_y;
13790
13791 if (run.height > 0 && run.current_y != run.desired_y)
13792 {
13793 update_begin (f);
13794 rif->update_window_begin_hook (w);
13795 rif->clear_window_mouse_face (w);
13796 rif->scroll_run_hook (w, &run);
13797 rif->update_window_end_hook (w, 0, 0);
13798 update_end (f);
13799 }
13800
13801 /* Shift current matrix down by nrows_scrolled lines. */
13802 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13803 rotate_matrix (w->current_matrix,
13804 start_vpos,
13805 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13806 nrows_scrolled);
13807
13808 /* Disable lines that must be updated. */
13809 for (i = 0; i < nrows_scrolled; ++i)
13810 (start_row + i)->enabled_p = 0;
13811
13812 /* Re-compute Y positions. */
13813 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13814 max_y = it.last_visible_y;
13815 for (row = start_row + nrows_scrolled;
13816 row < bottom_row;
13817 ++row)
13818 {
13819 row->y = it.current_y;
13820 row->visible_height = row->height;
13821
13822 if (row->y < min_y)
13823 row->visible_height -= min_y - row->y;
13824 if (row->y + row->height > max_y)
13825 row->visible_height -= row->y + row->height - max_y;
13826 row->redraw_fringe_bitmaps_p = 1;
13827
13828 it.current_y += row->height;
13829
13830 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13831 last_reused_text_row = row;
13832 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13833 break;
13834 }
13835
13836 /* Disable lines in the current matrix which are now
13837 below the window. */
13838 for (++row; row < bottom_row; ++row)
13839 row->enabled_p = row->mode_line_p = 0;
13840 }
13841
13842 /* Update window_end_pos etc.; last_reused_text_row is the last
13843 reused row from the current matrix containing text, if any.
13844 The value of last_text_row is the last displayed line
13845 containing text. */
13846 if (last_reused_text_row)
13847 {
13848 w->window_end_bytepos
13849 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13850 w->window_end_pos
13851 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13852 w->window_end_vpos
13853 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13854 w->current_matrix));
13855 }
13856 else if (last_text_row)
13857 {
13858 w->window_end_bytepos
13859 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13860 w->window_end_pos
13861 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13862 w->window_end_vpos
13863 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13864 }
13865 else
13866 {
13867 /* This window must be completely empty. */
13868 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13869 w->window_end_pos = make_number (Z - ZV);
13870 w->window_end_vpos = make_number (0);
13871 }
13872 w->window_end_valid = Qnil;
13873
13874 /* Update hint: don't try scrolling again in update_window. */
13875 w->desired_matrix->no_scrolling_p = 1;
13876
13877 #if GLYPH_DEBUG
13878 debug_method_add (w, "try_window_reusing_current_matrix 1");
13879 #endif
13880 return 1;
13881 }
13882 else if (CHARPOS (new_start) > CHARPOS (start))
13883 {
13884 struct glyph_row *pt_row, *row;
13885 struct glyph_row *first_reusable_row;
13886 struct glyph_row *first_row_to_display;
13887 int dy;
13888 int yb = window_text_bottom_y (w);
13889
13890 /* Find the row starting at new_start, if there is one. Don't
13891 reuse a partially visible line at the end. */
13892 first_reusable_row = start_row;
13893 while (first_reusable_row->enabled_p
13894 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13895 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13896 < CHARPOS (new_start)))
13897 ++first_reusable_row;
13898
13899 /* Give up if there is no row to reuse. */
13900 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13901 || !first_reusable_row->enabled_p
13902 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13903 != CHARPOS (new_start)))
13904 return 0;
13905
13906 /* We can reuse fully visible rows beginning with
13907 first_reusable_row to the end of the window. Set
13908 first_row_to_display to the first row that cannot be reused.
13909 Set pt_row to the row containing point, if there is any. */
13910 pt_row = NULL;
13911 for (first_row_to_display = first_reusable_row;
13912 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13913 ++first_row_to_display)
13914 {
13915 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13916 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13917 pt_row = first_row_to_display;
13918 }
13919
13920 /* Start displaying at the start of first_row_to_display. */
13921 xassert (first_row_to_display->y < yb);
13922 init_to_row_start (&it, w, first_row_to_display);
13923
13924 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13925 - start_vpos);
13926 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13927 - nrows_scrolled);
13928 it.current_y = (first_row_to_display->y - first_reusable_row->y
13929 + WINDOW_HEADER_LINE_HEIGHT (w));
13930
13931 /* Display lines beginning with first_row_to_display in the
13932 desired matrix. Set last_text_row to the last row displayed
13933 that displays text. */
13934 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13935 if (pt_row == NULL)
13936 w->cursor.vpos = -1;
13937 last_text_row = NULL;
13938 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13939 if (display_line (&it))
13940 last_text_row = it.glyph_row - 1;
13941
13942 /* Give up If point isn't in a row displayed or reused. */
13943 if (w->cursor.vpos < 0)
13944 {
13945 clear_glyph_matrix (w->desired_matrix);
13946 return 0;
13947 }
13948
13949 /* If point is in a reused row, adjust y and vpos of the cursor
13950 position. */
13951 if (pt_row)
13952 {
13953 w->cursor.vpos -= nrows_scrolled;
13954 w->cursor.y -= first_reusable_row->y - start_row->y;
13955 }
13956
13957 /* Scroll the display. */
13958 run.current_y = first_reusable_row->y;
13959 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13960 run.height = it.last_visible_y - run.current_y;
13961 dy = run.current_y - run.desired_y;
13962
13963 if (run.height)
13964 {
13965 update_begin (f);
13966 rif->update_window_begin_hook (w);
13967 rif->clear_window_mouse_face (w);
13968 rif->scroll_run_hook (w, &run);
13969 rif->update_window_end_hook (w, 0, 0);
13970 update_end (f);
13971 }
13972
13973 /* Adjust Y positions of reused rows. */
13974 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13975 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13976 max_y = it.last_visible_y;
13977 for (row = first_reusable_row; row < first_row_to_display; ++row)
13978 {
13979 row->y -= dy;
13980 row->visible_height = row->height;
13981 if (row->y < min_y)
13982 row->visible_height -= min_y - row->y;
13983 if (row->y + row->height > max_y)
13984 row->visible_height -= row->y + row->height - max_y;
13985 row->redraw_fringe_bitmaps_p = 1;
13986 }
13987
13988 /* Scroll the current matrix. */
13989 xassert (nrows_scrolled > 0);
13990 rotate_matrix (w->current_matrix,
13991 start_vpos,
13992 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13993 -nrows_scrolled);
13994
13995 /* Disable rows not reused. */
13996 for (row -= nrows_scrolled; row < bottom_row; ++row)
13997 row->enabled_p = 0;
13998
13999 /* Point may have moved to a different line, so we cannot assume that
14000 the previous cursor position is valid; locate the correct row. */
14001 if (pt_row)
14002 {
14003 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14004 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14005 row++)
14006 {
14007 w->cursor.vpos++;
14008 w->cursor.y = row->y;
14009 }
14010 if (row < bottom_row)
14011 {
14012 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14013 while (glyph->charpos < PT)
14014 {
14015 w->cursor.hpos++;
14016 w->cursor.x += glyph->pixel_width;
14017 glyph++;
14018 }
14019 }
14020 }
14021
14022 /* Adjust window end. A null value of last_text_row means that
14023 the window end is in reused rows which in turn means that
14024 only its vpos can have changed. */
14025 if (last_text_row)
14026 {
14027 w->window_end_bytepos
14028 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14029 w->window_end_pos
14030 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14031 w->window_end_vpos
14032 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14033 }
14034 else
14035 {
14036 w->window_end_vpos
14037 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14038 }
14039
14040 w->window_end_valid = Qnil;
14041 w->desired_matrix->no_scrolling_p = 1;
14042
14043 #if GLYPH_DEBUG
14044 debug_method_add (w, "try_window_reusing_current_matrix 2");
14045 #endif
14046 return 1;
14047 }
14048
14049 return 0;
14050 }
14051
14052
14053 \f
14054 /************************************************************************
14055 Window redisplay reusing current matrix when buffer has changed
14056 ************************************************************************/
14057
14058 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14059 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14060 int *, int *));
14061 static struct glyph_row *
14062 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14063 struct glyph_row *));
14064
14065
14066 /* Return the last row in MATRIX displaying text. If row START is
14067 non-null, start searching with that row. IT gives the dimensions
14068 of the display. Value is null if matrix is empty; otherwise it is
14069 a pointer to the row found. */
14070
14071 static struct glyph_row *
14072 find_last_row_displaying_text (matrix, it, start)
14073 struct glyph_matrix *matrix;
14074 struct it *it;
14075 struct glyph_row *start;
14076 {
14077 struct glyph_row *row, *row_found;
14078
14079 /* Set row_found to the last row in IT->w's current matrix
14080 displaying text. The loop looks funny but think of partially
14081 visible lines. */
14082 row_found = NULL;
14083 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14084 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14085 {
14086 xassert (row->enabled_p);
14087 row_found = row;
14088 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14089 break;
14090 ++row;
14091 }
14092
14093 return row_found;
14094 }
14095
14096
14097 /* Return the last row in the current matrix of W that is not affected
14098 by changes at the start of current_buffer that occurred since W's
14099 current matrix was built. Value is null if no such row exists.
14100
14101 BEG_UNCHANGED us the number of characters unchanged at the start of
14102 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14103 first changed character in current_buffer. Characters at positions <
14104 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14105 when the current matrix was built. */
14106
14107 static struct glyph_row *
14108 find_last_unchanged_at_beg_row (w)
14109 struct window *w;
14110 {
14111 int first_changed_pos = BEG + BEG_UNCHANGED;
14112 struct glyph_row *row;
14113 struct glyph_row *row_found = NULL;
14114 int yb = window_text_bottom_y (w);
14115
14116 /* Find the last row displaying unchanged text. */
14117 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14118 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14119 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14120 {
14121 if (/* If row ends before first_changed_pos, it is unchanged,
14122 except in some case. */
14123 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14124 /* When row ends in ZV and we write at ZV it is not
14125 unchanged. */
14126 && !row->ends_at_zv_p
14127 /* When first_changed_pos is the end of a continued line,
14128 row is not unchanged because it may be no longer
14129 continued. */
14130 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14131 && (row->continued_p
14132 || row->exact_window_width_line_p)))
14133 row_found = row;
14134
14135 /* Stop if last visible row. */
14136 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14137 break;
14138
14139 ++row;
14140 }
14141
14142 return row_found;
14143 }
14144
14145
14146 /* Find the first glyph row in the current matrix of W that is not
14147 affected by changes at the end of current_buffer since the
14148 time W's current matrix was built.
14149
14150 Return in *DELTA the number of chars by which buffer positions in
14151 unchanged text at the end of current_buffer must be adjusted.
14152
14153 Return in *DELTA_BYTES the corresponding number of bytes.
14154
14155 Value is null if no such row exists, i.e. all rows are affected by
14156 changes. */
14157
14158 static struct glyph_row *
14159 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14160 struct window *w;
14161 int *delta, *delta_bytes;
14162 {
14163 struct glyph_row *row;
14164 struct glyph_row *row_found = NULL;
14165
14166 *delta = *delta_bytes = 0;
14167
14168 /* Display must not have been paused, otherwise the current matrix
14169 is not up to date. */
14170 if (NILP (w->window_end_valid))
14171 abort ();
14172
14173 /* A value of window_end_pos >= END_UNCHANGED means that the window
14174 end is in the range of changed text. If so, there is no
14175 unchanged row at the end of W's current matrix. */
14176 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14177 return NULL;
14178
14179 /* Set row to the last row in W's current matrix displaying text. */
14180 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14181
14182 /* If matrix is entirely empty, no unchanged row exists. */
14183 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14184 {
14185 /* The value of row is the last glyph row in the matrix having a
14186 meaningful buffer position in it. The end position of row
14187 corresponds to window_end_pos. This allows us to translate
14188 buffer positions in the current matrix to current buffer
14189 positions for characters not in changed text. */
14190 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14191 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14192 int last_unchanged_pos, last_unchanged_pos_old;
14193 struct glyph_row *first_text_row
14194 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14195
14196 *delta = Z - Z_old;
14197 *delta_bytes = Z_BYTE - Z_BYTE_old;
14198
14199 /* Set last_unchanged_pos to the buffer position of the last
14200 character in the buffer that has not been changed. Z is the
14201 index + 1 of the last character in current_buffer, i.e. by
14202 subtracting END_UNCHANGED we get the index of the last
14203 unchanged character, and we have to add BEG to get its buffer
14204 position. */
14205 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14206 last_unchanged_pos_old = last_unchanged_pos - *delta;
14207
14208 /* Search backward from ROW for a row displaying a line that
14209 starts at a minimum position >= last_unchanged_pos_old. */
14210 for (; row > first_text_row; --row)
14211 {
14212 /* This used to abort, but it can happen.
14213 It is ok to just stop the search instead here. KFS. */
14214 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14215 break;
14216
14217 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14218 row_found = row;
14219 }
14220 }
14221
14222 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14223 abort ();
14224
14225 return row_found;
14226 }
14227
14228
14229 /* Make sure that glyph rows in the current matrix of window W
14230 reference the same glyph memory as corresponding rows in the
14231 frame's frame matrix. This function is called after scrolling W's
14232 current matrix on a terminal frame in try_window_id and
14233 try_window_reusing_current_matrix. */
14234
14235 static void
14236 sync_frame_with_window_matrix_rows (w)
14237 struct window *w;
14238 {
14239 struct frame *f = XFRAME (w->frame);
14240 struct glyph_row *window_row, *window_row_end, *frame_row;
14241
14242 /* Preconditions: W must be a leaf window and full-width. Its frame
14243 must have a frame matrix. */
14244 xassert (NILP (w->hchild) && NILP (w->vchild));
14245 xassert (WINDOW_FULL_WIDTH_P (w));
14246 xassert (!FRAME_WINDOW_P (f));
14247
14248 /* If W is a full-width window, glyph pointers in W's current matrix
14249 have, by definition, to be the same as glyph pointers in the
14250 corresponding frame matrix. Note that frame matrices have no
14251 marginal areas (see build_frame_matrix). */
14252 window_row = w->current_matrix->rows;
14253 window_row_end = window_row + w->current_matrix->nrows;
14254 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14255 while (window_row < window_row_end)
14256 {
14257 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14258 struct glyph *end = window_row->glyphs[LAST_AREA];
14259
14260 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14261 frame_row->glyphs[TEXT_AREA] = start;
14262 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14263 frame_row->glyphs[LAST_AREA] = end;
14264
14265 /* Disable frame rows whose corresponding window rows have
14266 been disabled in try_window_id. */
14267 if (!window_row->enabled_p)
14268 frame_row->enabled_p = 0;
14269
14270 ++window_row, ++frame_row;
14271 }
14272 }
14273
14274
14275 /* Find the glyph row in window W containing CHARPOS. Consider all
14276 rows between START and END (not inclusive). END null means search
14277 all rows to the end of the display area of W. Value is the row
14278 containing CHARPOS or null. */
14279
14280 struct glyph_row *
14281 row_containing_pos (w, charpos, start, end, dy)
14282 struct window *w;
14283 int charpos;
14284 struct glyph_row *start, *end;
14285 int dy;
14286 {
14287 struct glyph_row *row = start;
14288 int last_y;
14289
14290 /* If we happen to start on a header-line, skip that. */
14291 if (row->mode_line_p)
14292 ++row;
14293
14294 if ((end && row >= end) || !row->enabled_p)
14295 return NULL;
14296
14297 last_y = window_text_bottom_y (w) - dy;
14298
14299 while (1)
14300 {
14301 /* Give up if we have gone too far. */
14302 if (end && row >= end)
14303 return NULL;
14304 /* This formerly returned if they were equal.
14305 I think that both quantities are of a "last plus one" type;
14306 if so, when they are equal, the row is within the screen. -- rms. */
14307 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14308 return NULL;
14309
14310 /* If it is in this row, return this row. */
14311 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14312 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14313 /* The end position of a row equals the start
14314 position of the next row. If CHARPOS is there, we
14315 would rather display it in the next line, except
14316 when this line ends in ZV. */
14317 && !row->ends_at_zv_p
14318 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14319 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14320 return row;
14321 ++row;
14322 }
14323 }
14324
14325
14326 /* Try to redisplay window W by reusing its existing display. W's
14327 current matrix must be up to date when this function is called,
14328 i.e. window_end_valid must not be nil.
14329
14330 Value is
14331
14332 1 if display has been updated
14333 0 if otherwise unsuccessful
14334 -1 if redisplay with same window start is known not to succeed
14335
14336 The following steps are performed:
14337
14338 1. Find the last row in the current matrix of W that is not
14339 affected by changes at the start of current_buffer. If no such row
14340 is found, give up.
14341
14342 2. Find the first row in W's current matrix that is not affected by
14343 changes at the end of current_buffer. Maybe there is no such row.
14344
14345 3. Display lines beginning with the row + 1 found in step 1 to the
14346 row found in step 2 or, if step 2 didn't find a row, to the end of
14347 the window.
14348
14349 4. If cursor is not known to appear on the window, give up.
14350
14351 5. If display stopped at the row found in step 2, scroll the
14352 display and current matrix as needed.
14353
14354 6. Maybe display some lines at the end of W, if we must. This can
14355 happen under various circumstances, like a partially visible line
14356 becoming fully visible, or because newly displayed lines are displayed
14357 in smaller font sizes.
14358
14359 7. Update W's window end information. */
14360
14361 static int
14362 try_window_id (w)
14363 struct window *w;
14364 {
14365 struct frame *f = XFRAME (w->frame);
14366 struct glyph_matrix *current_matrix = w->current_matrix;
14367 struct glyph_matrix *desired_matrix = w->desired_matrix;
14368 struct glyph_row *last_unchanged_at_beg_row;
14369 struct glyph_row *first_unchanged_at_end_row;
14370 struct glyph_row *row;
14371 struct glyph_row *bottom_row;
14372 int bottom_vpos;
14373 struct it it;
14374 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14375 struct text_pos start_pos;
14376 struct run run;
14377 int first_unchanged_at_end_vpos = 0;
14378 struct glyph_row *last_text_row, *last_text_row_at_end;
14379 struct text_pos start;
14380 int first_changed_charpos, last_changed_charpos;
14381
14382 #if GLYPH_DEBUG
14383 if (inhibit_try_window_id)
14384 return 0;
14385 #endif
14386
14387 /* This is handy for debugging. */
14388 #if 0
14389 #define GIVE_UP(X) \
14390 do { \
14391 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14392 return 0; \
14393 } while (0)
14394 #else
14395 #define GIVE_UP(X) return 0
14396 #endif
14397
14398 SET_TEXT_POS_FROM_MARKER (start, w->start);
14399
14400 /* Don't use this for mini-windows because these can show
14401 messages and mini-buffers, and we don't handle that here. */
14402 if (MINI_WINDOW_P (w))
14403 GIVE_UP (1);
14404
14405 /* This flag is used to prevent redisplay optimizations. */
14406 if (windows_or_buffers_changed || cursor_type_changed)
14407 GIVE_UP (2);
14408
14409 /* Verify that narrowing has not changed.
14410 Also verify that we were not told to prevent redisplay optimizations.
14411 It would be nice to further
14412 reduce the number of cases where this prevents try_window_id. */
14413 if (current_buffer->clip_changed
14414 || current_buffer->prevent_redisplay_optimizations_p)
14415 GIVE_UP (3);
14416
14417 /* Window must either use window-based redisplay or be full width. */
14418 if (!FRAME_WINDOW_P (f)
14419 && (!line_ins_del_ok
14420 || !WINDOW_FULL_WIDTH_P (w)))
14421 GIVE_UP (4);
14422
14423 /* Give up if point is not known NOT to appear in W. */
14424 if (PT < CHARPOS (start))
14425 GIVE_UP (5);
14426
14427 /* Another way to prevent redisplay optimizations. */
14428 if (XFASTINT (w->last_modified) == 0)
14429 GIVE_UP (6);
14430
14431 /* Verify that window is not hscrolled. */
14432 if (XFASTINT (w->hscroll) != 0)
14433 GIVE_UP (7);
14434
14435 /* Verify that display wasn't paused. */
14436 if (NILP (w->window_end_valid))
14437 GIVE_UP (8);
14438
14439 /* Can't use this if highlighting a region because a cursor movement
14440 will do more than just set the cursor. */
14441 if (!NILP (Vtransient_mark_mode)
14442 && !NILP (current_buffer->mark_active))
14443 GIVE_UP (9);
14444
14445 /* Likewise if highlighting trailing whitespace. */
14446 if (!NILP (Vshow_trailing_whitespace))
14447 GIVE_UP (11);
14448
14449 /* Likewise if showing a region. */
14450 if (!NILP (w->region_showing))
14451 GIVE_UP (10);
14452
14453 /* Can use this if overlay arrow position and or string have changed. */
14454 if (overlay_arrows_changed_p ())
14455 GIVE_UP (12);
14456
14457
14458 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14459 only if buffer has really changed. The reason is that the gap is
14460 initially at Z for freshly visited files. The code below would
14461 set end_unchanged to 0 in that case. */
14462 if (MODIFF > SAVE_MODIFF
14463 /* This seems to happen sometimes after saving a buffer. */
14464 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14465 {
14466 if (GPT - BEG < BEG_UNCHANGED)
14467 BEG_UNCHANGED = GPT - BEG;
14468 if (Z - GPT < END_UNCHANGED)
14469 END_UNCHANGED = Z - GPT;
14470 }
14471
14472 /* The position of the first and last character that has been changed. */
14473 first_changed_charpos = BEG + BEG_UNCHANGED;
14474 last_changed_charpos = Z - END_UNCHANGED;
14475
14476 /* If window starts after a line end, and the last change is in
14477 front of that newline, then changes don't affect the display.
14478 This case happens with stealth-fontification. Note that although
14479 the display is unchanged, glyph positions in the matrix have to
14480 be adjusted, of course. */
14481 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14482 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14483 && ((last_changed_charpos < CHARPOS (start)
14484 && CHARPOS (start) == BEGV)
14485 || (last_changed_charpos < CHARPOS (start) - 1
14486 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14487 {
14488 int Z_old, delta, Z_BYTE_old, delta_bytes;
14489 struct glyph_row *r0;
14490
14491 /* Compute how many chars/bytes have been added to or removed
14492 from the buffer. */
14493 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14494 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14495 delta = Z - Z_old;
14496 delta_bytes = Z_BYTE - Z_BYTE_old;
14497
14498 /* Give up if PT is not in the window. Note that it already has
14499 been checked at the start of try_window_id that PT is not in
14500 front of the window start. */
14501 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14502 GIVE_UP (13);
14503
14504 /* If window start is unchanged, we can reuse the whole matrix
14505 as is, after adjusting glyph positions. No need to compute
14506 the window end again, since its offset from Z hasn't changed. */
14507 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14508 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14509 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14510 /* PT must not be in a partially visible line. */
14511 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14512 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14513 {
14514 /* Adjust positions in the glyph matrix. */
14515 if (delta || delta_bytes)
14516 {
14517 struct glyph_row *r1
14518 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14519 increment_matrix_positions (w->current_matrix,
14520 MATRIX_ROW_VPOS (r0, current_matrix),
14521 MATRIX_ROW_VPOS (r1, current_matrix),
14522 delta, delta_bytes);
14523 }
14524
14525 /* Set the cursor. */
14526 row = row_containing_pos (w, PT, r0, NULL, 0);
14527 if (row)
14528 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14529 else
14530 abort ();
14531 return 1;
14532 }
14533 }
14534
14535 /* Handle the case that changes are all below what is displayed in
14536 the window, and that PT is in the window. This shortcut cannot
14537 be taken if ZV is visible in the window, and text has been added
14538 there that is visible in the window. */
14539 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14540 /* ZV is not visible in the window, or there are no
14541 changes at ZV, actually. */
14542 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14543 || first_changed_charpos == last_changed_charpos))
14544 {
14545 struct glyph_row *r0;
14546
14547 /* Give up if PT is not in the window. Note that it already has
14548 been checked at the start of try_window_id that PT is not in
14549 front of the window start. */
14550 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14551 GIVE_UP (14);
14552
14553 /* If window start is unchanged, we can reuse the whole matrix
14554 as is, without changing glyph positions since no text has
14555 been added/removed in front of the window end. */
14556 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14557 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14558 /* PT must not be in a partially visible line. */
14559 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14560 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14561 {
14562 /* We have to compute the window end anew since text
14563 can have been added/removed after it. */
14564 w->window_end_pos
14565 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14566 w->window_end_bytepos
14567 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14568
14569 /* Set the cursor. */
14570 row = row_containing_pos (w, PT, r0, NULL, 0);
14571 if (row)
14572 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14573 else
14574 abort ();
14575 return 2;
14576 }
14577 }
14578
14579 /* Give up if window start is in the changed area.
14580
14581 The condition used to read
14582
14583 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14584
14585 but why that was tested escapes me at the moment. */
14586 if (CHARPOS (start) >= first_changed_charpos
14587 && CHARPOS (start) <= last_changed_charpos)
14588 GIVE_UP (15);
14589
14590 /* Check that window start agrees with the start of the first glyph
14591 row in its current matrix. Check this after we know the window
14592 start is not in changed text, otherwise positions would not be
14593 comparable. */
14594 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14595 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14596 GIVE_UP (16);
14597
14598 /* Give up if the window ends in strings. Overlay strings
14599 at the end are difficult to handle, so don't try. */
14600 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14601 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14602 GIVE_UP (20);
14603
14604 /* Compute the position at which we have to start displaying new
14605 lines. Some of the lines at the top of the window might be
14606 reusable because they are not displaying changed text. Find the
14607 last row in W's current matrix not affected by changes at the
14608 start of current_buffer. Value is null if changes start in the
14609 first line of window. */
14610 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14611 if (last_unchanged_at_beg_row)
14612 {
14613 /* Avoid starting to display in the moddle of a character, a TAB
14614 for instance. This is easier than to set up the iterator
14615 exactly, and it's not a frequent case, so the additional
14616 effort wouldn't really pay off. */
14617 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14618 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14619 && last_unchanged_at_beg_row > w->current_matrix->rows)
14620 --last_unchanged_at_beg_row;
14621
14622 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14623 GIVE_UP (17);
14624
14625 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14626 GIVE_UP (18);
14627 start_pos = it.current.pos;
14628
14629 /* Start displaying new lines in the desired matrix at the same
14630 vpos we would use in the current matrix, i.e. below
14631 last_unchanged_at_beg_row. */
14632 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14633 current_matrix);
14634 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14635 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14636
14637 xassert (it.hpos == 0 && it.current_x == 0);
14638 }
14639 else
14640 {
14641 /* There are no reusable lines at the start of the window.
14642 Start displaying in the first text line. */
14643 start_display (&it, w, start);
14644 it.vpos = it.first_vpos;
14645 start_pos = it.current.pos;
14646 }
14647
14648 /* Find the first row that is not affected by changes at the end of
14649 the buffer. Value will be null if there is no unchanged row, in
14650 which case we must redisplay to the end of the window. delta
14651 will be set to the value by which buffer positions beginning with
14652 first_unchanged_at_end_row have to be adjusted due to text
14653 changes. */
14654 first_unchanged_at_end_row
14655 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14656 IF_DEBUG (debug_delta = delta);
14657 IF_DEBUG (debug_delta_bytes = delta_bytes);
14658
14659 /* Set stop_pos to the buffer position up to which we will have to
14660 display new lines. If first_unchanged_at_end_row != NULL, this
14661 is the buffer position of the start of the line displayed in that
14662 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14663 that we don't stop at a buffer position. */
14664 stop_pos = 0;
14665 if (first_unchanged_at_end_row)
14666 {
14667 xassert (last_unchanged_at_beg_row == NULL
14668 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14669
14670 /* If this is a continuation line, move forward to the next one
14671 that isn't. Changes in lines above affect this line.
14672 Caution: this may move first_unchanged_at_end_row to a row
14673 not displaying text. */
14674 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14675 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14676 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14677 < it.last_visible_y))
14678 ++first_unchanged_at_end_row;
14679
14680 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14681 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14682 >= it.last_visible_y))
14683 first_unchanged_at_end_row = NULL;
14684 else
14685 {
14686 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14687 + delta);
14688 first_unchanged_at_end_vpos
14689 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14690 xassert (stop_pos >= Z - END_UNCHANGED);
14691 }
14692 }
14693 else if (last_unchanged_at_beg_row == NULL)
14694 GIVE_UP (19);
14695
14696
14697 #if GLYPH_DEBUG
14698
14699 /* Either there is no unchanged row at the end, or the one we have
14700 now displays text. This is a necessary condition for the window
14701 end pos calculation at the end of this function. */
14702 xassert (first_unchanged_at_end_row == NULL
14703 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14704
14705 debug_last_unchanged_at_beg_vpos
14706 = (last_unchanged_at_beg_row
14707 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14708 : -1);
14709 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14710
14711 #endif /* GLYPH_DEBUG != 0 */
14712
14713
14714 /* Display new lines. Set last_text_row to the last new line
14715 displayed which has text on it, i.e. might end up as being the
14716 line where the window_end_vpos is. */
14717 w->cursor.vpos = -1;
14718 last_text_row = NULL;
14719 overlay_arrow_seen = 0;
14720 while (it.current_y < it.last_visible_y
14721 && !fonts_changed_p
14722 && (first_unchanged_at_end_row == NULL
14723 || IT_CHARPOS (it) < stop_pos))
14724 {
14725 if (display_line (&it))
14726 last_text_row = it.glyph_row - 1;
14727 }
14728
14729 if (fonts_changed_p)
14730 return -1;
14731
14732
14733 /* Compute differences in buffer positions, y-positions etc. for
14734 lines reused at the bottom of the window. Compute what we can
14735 scroll. */
14736 if (first_unchanged_at_end_row
14737 /* No lines reused because we displayed everything up to the
14738 bottom of the window. */
14739 && it.current_y < it.last_visible_y)
14740 {
14741 dvpos = (it.vpos
14742 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14743 current_matrix));
14744 dy = it.current_y - first_unchanged_at_end_row->y;
14745 run.current_y = first_unchanged_at_end_row->y;
14746 run.desired_y = run.current_y + dy;
14747 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14748 }
14749 else
14750 {
14751 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14752 first_unchanged_at_end_row = NULL;
14753 }
14754 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14755
14756
14757 /* Find the cursor if not already found. We have to decide whether
14758 PT will appear on this window (it sometimes doesn't, but this is
14759 not a very frequent case.) This decision has to be made before
14760 the current matrix is altered. A value of cursor.vpos < 0 means
14761 that PT is either in one of the lines beginning at
14762 first_unchanged_at_end_row or below the window. Don't care for
14763 lines that might be displayed later at the window end; as
14764 mentioned, this is not a frequent case. */
14765 if (w->cursor.vpos < 0)
14766 {
14767 /* Cursor in unchanged rows at the top? */
14768 if (PT < CHARPOS (start_pos)
14769 && last_unchanged_at_beg_row)
14770 {
14771 row = row_containing_pos (w, PT,
14772 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14773 last_unchanged_at_beg_row + 1, 0);
14774 if (row)
14775 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14776 }
14777
14778 /* Start from first_unchanged_at_end_row looking for PT. */
14779 else if (first_unchanged_at_end_row)
14780 {
14781 row = row_containing_pos (w, PT - delta,
14782 first_unchanged_at_end_row, NULL, 0);
14783 if (row)
14784 set_cursor_from_row (w, row, w->current_matrix, delta,
14785 delta_bytes, dy, dvpos);
14786 }
14787
14788 /* Give up if cursor was not found. */
14789 if (w->cursor.vpos < 0)
14790 {
14791 clear_glyph_matrix (w->desired_matrix);
14792 return -1;
14793 }
14794 }
14795
14796 /* Don't let the cursor end in the scroll margins. */
14797 {
14798 int this_scroll_margin, cursor_height;
14799
14800 this_scroll_margin = max (0, scroll_margin);
14801 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14802 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14803 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14804
14805 if ((w->cursor.y < this_scroll_margin
14806 && CHARPOS (start) > BEGV)
14807 /* Old redisplay didn't take scroll margin into account at the bottom,
14808 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14809 || (w->cursor.y + (make_cursor_line_fully_visible_p
14810 ? cursor_height + this_scroll_margin
14811 : 1)) > it.last_visible_y)
14812 {
14813 w->cursor.vpos = -1;
14814 clear_glyph_matrix (w->desired_matrix);
14815 return -1;
14816 }
14817 }
14818
14819 /* Scroll the display. Do it before changing the current matrix so
14820 that xterm.c doesn't get confused about where the cursor glyph is
14821 found. */
14822 if (dy && run.height)
14823 {
14824 update_begin (f);
14825
14826 if (FRAME_WINDOW_P (f))
14827 {
14828 rif->update_window_begin_hook (w);
14829 rif->clear_window_mouse_face (w);
14830 rif->scroll_run_hook (w, &run);
14831 rif->update_window_end_hook (w, 0, 0);
14832 }
14833 else
14834 {
14835 /* Terminal frame. In this case, dvpos gives the number of
14836 lines to scroll by; dvpos < 0 means scroll up. */
14837 int first_unchanged_at_end_vpos
14838 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14839 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14840 int end = (WINDOW_TOP_EDGE_LINE (w)
14841 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14842 + window_internal_height (w));
14843
14844 /* Perform the operation on the screen. */
14845 if (dvpos > 0)
14846 {
14847 /* Scroll last_unchanged_at_beg_row to the end of the
14848 window down dvpos lines. */
14849 set_terminal_window (end);
14850
14851 /* On dumb terminals delete dvpos lines at the end
14852 before inserting dvpos empty lines. */
14853 if (!scroll_region_ok)
14854 ins_del_lines (end - dvpos, -dvpos);
14855
14856 /* Insert dvpos empty lines in front of
14857 last_unchanged_at_beg_row. */
14858 ins_del_lines (from, dvpos);
14859 }
14860 else if (dvpos < 0)
14861 {
14862 /* Scroll up last_unchanged_at_beg_vpos to the end of
14863 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14864 set_terminal_window (end);
14865
14866 /* Delete dvpos lines in front of
14867 last_unchanged_at_beg_vpos. ins_del_lines will set
14868 the cursor to the given vpos and emit |dvpos| delete
14869 line sequences. */
14870 ins_del_lines (from + dvpos, dvpos);
14871
14872 /* On a dumb terminal insert dvpos empty lines at the
14873 end. */
14874 if (!scroll_region_ok)
14875 ins_del_lines (end + dvpos, -dvpos);
14876 }
14877
14878 set_terminal_window (0);
14879 }
14880
14881 update_end (f);
14882 }
14883
14884 /* Shift reused rows of the current matrix to the right position.
14885 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14886 text. */
14887 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14888 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14889 if (dvpos < 0)
14890 {
14891 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14892 bottom_vpos, dvpos);
14893 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14894 bottom_vpos, 0);
14895 }
14896 else if (dvpos > 0)
14897 {
14898 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14899 bottom_vpos, dvpos);
14900 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14901 first_unchanged_at_end_vpos + dvpos, 0);
14902 }
14903
14904 /* For frame-based redisplay, make sure that current frame and window
14905 matrix are in sync with respect to glyph memory. */
14906 if (!FRAME_WINDOW_P (f))
14907 sync_frame_with_window_matrix_rows (w);
14908
14909 /* Adjust buffer positions in reused rows. */
14910 if (delta || delta_bytes)
14911 increment_matrix_positions (current_matrix,
14912 first_unchanged_at_end_vpos + dvpos,
14913 bottom_vpos, delta, delta_bytes);
14914
14915 /* Adjust Y positions. */
14916 if (dy)
14917 shift_glyph_matrix (w, current_matrix,
14918 first_unchanged_at_end_vpos + dvpos,
14919 bottom_vpos, dy);
14920
14921 if (first_unchanged_at_end_row)
14922 {
14923 first_unchanged_at_end_row += dvpos;
14924 if (first_unchanged_at_end_row->y >= it.last_visible_y
14925 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14926 first_unchanged_at_end_row = NULL;
14927 }
14928
14929 /* If scrolling up, there may be some lines to display at the end of
14930 the window. */
14931 last_text_row_at_end = NULL;
14932 if (dy < 0)
14933 {
14934 /* Scrolling up can leave for example a partially visible line
14935 at the end of the window to be redisplayed. */
14936 /* Set last_row to the glyph row in the current matrix where the
14937 window end line is found. It has been moved up or down in
14938 the matrix by dvpos. */
14939 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14940 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14941
14942 /* If last_row is the window end line, it should display text. */
14943 xassert (last_row->displays_text_p);
14944
14945 /* If window end line was partially visible before, begin
14946 displaying at that line. Otherwise begin displaying with the
14947 line following it. */
14948 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14949 {
14950 init_to_row_start (&it, w, last_row);
14951 it.vpos = last_vpos;
14952 it.current_y = last_row->y;
14953 }
14954 else
14955 {
14956 init_to_row_end (&it, w, last_row);
14957 it.vpos = 1 + last_vpos;
14958 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14959 ++last_row;
14960 }
14961
14962 /* We may start in a continuation line. If so, we have to
14963 get the right continuation_lines_width and current_x. */
14964 it.continuation_lines_width = last_row->continuation_lines_width;
14965 it.hpos = it.current_x = 0;
14966
14967 /* Display the rest of the lines at the window end. */
14968 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14969 while (it.current_y < it.last_visible_y
14970 && !fonts_changed_p)
14971 {
14972 /* Is it always sure that the display agrees with lines in
14973 the current matrix? I don't think so, so we mark rows
14974 displayed invalid in the current matrix by setting their
14975 enabled_p flag to zero. */
14976 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14977 if (display_line (&it))
14978 last_text_row_at_end = it.glyph_row - 1;
14979 }
14980 }
14981
14982 /* Update window_end_pos and window_end_vpos. */
14983 if (first_unchanged_at_end_row
14984 && !last_text_row_at_end)
14985 {
14986 /* Window end line if one of the preserved rows from the current
14987 matrix. Set row to the last row displaying text in current
14988 matrix starting at first_unchanged_at_end_row, after
14989 scrolling. */
14990 xassert (first_unchanged_at_end_row->displays_text_p);
14991 row = find_last_row_displaying_text (w->current_matrix, &it,
14992 first_unchanged_at_end_row);
14993 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14994
14995 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14996 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14997 w->window_end_vpos
14998 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14999 xassert (w->window_end_bytepos >= 0);
15000 IF_DEBUG (debug_method_add (w, "A"));
15001 }
15002 else if (last_text_row_at_end)
15003 {
15004 w->window_end_pos
15005 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15006 w->window_end_bytepos
15007 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15008 w->window_end_vpos
15009 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15010 xassert (w->window_end_bytepos >= 0);
15011 IF_DEBUG (debug_method_add (w, "B"));
15012 }
15013 else if (last_text_row)
15014 {
15015 /* We have displayed either to the end of the window or at the
15016 end of the window, i.e. the last row with text is to be found
15017 in the desired matrix. */
15018 w->window_end_pos
15019 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15020 w->window_end_bytepos
15021 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15022 w->window_end_vpos
15023 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15024 xassert (w->window_end_bytepos >= 0);
15025 }
15026 else if (first_unchanged_at_end_row == NULL
15027 && last_text_row == NULL
15028 && last_text_row_at_end == NULL)
15029 {
15030 /* Displayed to end of window, but no line containing text was
15031 displayed. Lines were deleted at the end of the window. */
15032 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15033 int vpos = XFASTINT (w->window_end_vpos);
15034 struct glyph_row *current_row = current_matrix->rows + vpos;
15035 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15036
15037 for (row = NULL;
15038 row == NULL && vpos >= first_vpos;
15039 --vpos, --current_row, --desired_row)
15040 {
15041 if (desired_row->enabled_p)
15042 {
15043 if (desired_row->displays_text_p)
15044 row = desired_row;
15045 }
15046 else if (current_row->displays_text_p)
15047 row = current_row;
15048 }
15049
15050 xassert (row != NULL);
15051 w->window_end_vpos = make_number (vpos + 1);
15052 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15053 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15054 xassert (w->window_end_bytepos >= 0);
15055 IF_DEBUG (debug_method_add (w, "C"));
15056 }
15057 else
15058 abort ();
15059
15060 #if 0 /* This leads to problems, for instance when the cursor is
15061 at ZV, and the cursor line displays no text. */
15062 /* Disable rows below what's displayed in the window. This makes
15063 debugging easier. */
15064 enable_glyph_matrix_rows (current_matrix,
15065 XFASTINT (w->window_end_vpos) + 1,
15066 bottom_vpos, 0);
15067 #endif
15068
15069 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15070 debug_end_vpos = XFASTINT (w->window_end_vpos));
15071
15072 /* Record that display has not been completed. */
15073 w->window_end_valid = Qnil;
15074 w->desired_matrix->no_scrolling_p = 1;
15075 return 3;
15076
15077 #undef GIVE_UP
15078 }
15079
15080
15081 \f
15082 /***********************************************************************
15083 More debugging support
15084 ***********************************************************************/
15085
15086 #if GLYPH_DEBUG
15087
15088 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15089 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15090 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15091
15092
15093 /* Dump the contents of glyph matrix MATRIX on stderr.
15094
15095 GLYPHS 0 means don't show glyph contents.
15096 GLYPHS 1 means show glyphs in short form
15097 GLYPHS > 1 means show glyphs in long form. */
15098
15099 void
15100 dump_glyph_matrix (matrix, glyphs)
15101 struct glyph_matrix *matrix;
15102 int glyphs;
15103 {
15104 int i;
15105 for (i = 0; i < matrix->nrows; ++i)
15106 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15107 }
15108
15109
15110 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15111 the glyph row and area where the glyph comes from. */
15112
15113 void
15114 dump_glyph (row, glyph, area)
15115 struct glyph_row *row;
15116 struct glyph *glyph;
15117 int area;
15118 {
15119 if (glyph->type == CHAR_GLYPH)
15120 {
15121 fprintf (stderr,
15122 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15123 glyph - row->glyphs[TEXT_AREA],
15124 'C',
15125 glyph->charpos,
15126 (BUFFERP (glyph->object)
15127 ? 'B'
15128 : (STRINGP (glyph->object)
15129 ? 'S'
15130 : '-')),
15131 glyph->pixel_width,
15132 glyph->u.ch,
15133 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15134 ? glyph->u.ch
15135 : '.'),
15136 glyph->face_id,
15137 glyph->left_box_line_p,
15138 glyph->right_box_line_p);
15139 }
15140 else if (glyph->type == STRETCH_GLYPH)
15141 {
15142 fprintf (stderr,
15143 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15144 glyph - row->glyphs[TEXT_AREA],
15145 'S',
15146 glyph->charpos,
15147 (BUFFERP (glyph->object)
15148 ? 'B'
15149 : (STRINGP (glyph->object)
15150 ? 'S'
15151 : '-')),
15152 glyph->pixel_width,
15153 0,
15154 '.',
15155 glyph->face_id,
15156 glyph->left_box_line_p,
15157 glyph->right_box_line_p);
15158 }
15159 else if (glyph->type == IMAGE_GLYPH)
15160 {
15161 fprintf (stderr,
15162 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15163 glyph - row->glyphs[TEXT_AREA],
15164 'I',
15165 glyph->charpos,
15166 (BUFFERP (glyph->object)
15167 ? 'B'
15168 : (STRINGP (glyph->object)
15169 ? 'S'
15170 : '-')),
15171 glyph->pixel_width,
15172 glyph->u.img_id,
15173 '.',
15174 glyph->face_id,
15175 glyph->left_box_line_p,
15176 glyph->right_box_line_p);
15177 }
15178 else if (glyph->type == COMPOSITE_GLYPH)
15179 {
15180 fprintf (stderr,
15181 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15182 glyph - row->glyphs[TEXT_AREA],
15183 '+',
15184 glyph->charpos,
15185 (BUFFERP (glyph->object)
15186 ? 'B'
15187 : (STRINGP (glyph->object)
15188 ? 'S'
15189 : '-')),
15190 glyph->pixel_width,
15191 glyph->u.cmp_id,
15192 '.',
15193 glyph->face_id,
15194 glyph->left_box_line_p,
15195 glyph->right_box_line_p);
15196 }
15197 }
15198
15199
15200 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15201 GLYPHS 0 means don't show glyph contents.
15202 GLYPHS 1 means show glyphs in short form
15203 GLYPHS > 1 means show glyphs in long form. */
15204
15205 void
15206 dump_glyph_row (row, vpos, glyphs)
15207 struct glyph_row *row;
15208 int vpos, glyphs;
15209 {
15210 if (glyphs != 1)
15211 {
15212 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15213 fprintf (stderr, "======================================================================\n");
15214
15215 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15216 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15217 vpos,
15218 MATRIX_ROW_START_CHARPOS (row),
15219 MATRIX_ROW_END_CHARPOS (row),
15220 row->used[TEXT_AREA],
15221 row->contains_overlapping_glyphs_p,
15222 row->enabled_p,
15223 row->truncated_on_left_p,
15224 row->truncated_on_right_p,
15225 row->continued_p,
15226 MATRIX_ROW_CONTINUATION_LINE_P (row),
15227 row->displays_text_p,
15228 row->ends_at_zv_p,
15229 row->fill_line_p,
15230 row->ends_in_middle_of_char_p,
15231 row->starts_in_middle_of_char_p,
15232 row->mouse_face_p,
15233 row->x,
15234 row->y,
15235 row->pixel_width,
15236 row->height,
15237 row->visible_height,
15238 row->ascent,
15239 row->phys_ascent);
15240 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15241 row->end.overlay_string_index,
15242 row->continuation_lines_width);
15243 fprintf (stderr, "%9d %5d\n",
15244 CHARPOS (row->start.string_pos),
15245 CHARPOS (row->end.string_pos));
15246 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15247 row->end.dpvec_index);
15248 }
15249
15250 if (glyphs > 1)
15251 {
15252 int area;
15253
15254 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15255 {
15256 struct glyph *glyph = row->glyphs[area];
15257 struct glyph *glyph_end = glyph + row->used[area];
15258
15259 /* Glyph for a line end in text. */
15260 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15261 ++glyph_end;
15262
15263 if (glyph < glyph_end)
15264 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15265
15266 for (; glyph < glyph_end; ++glyph)
15267 dump_glyph (row, glyph, area);
15268 }
15269 }
15270 else if (glyphs == 1)
15271 {
15272 int area;
15273
15274 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15275 {
15276 char *s = (char *) alloca (row->used[area] + 1);
15277 int i;
15278
15279 for (i = 0; i < row->used[area]; ++i)
15280 {
15281 struct glyph *glyph = row->glyphs[area] + i;
15282 if (glyph->type == CHAR_GLYPH
15283 && glyph->u.ch < 0x80
15284 && glyph->u.ch >= ' ')
15285 s[i] = glyph->u.ch;
15286 else
15287 s[i] = '.';
15288 }
15289
15290 s[i] = '\0';
15291 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15292 }
15293 }
15294 }
15295
15296
15297 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15298 Sdump_glyph_matrix, 0, 1, "p",
15299 doc: /* Dump the current matrix of the selected window to stderr.
15300 Shows contents of glyph row structures. With non-nil
15301 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15302 glyphs in short form, otherwise show glyphs in long form. */)
15303 (glyphs)
15304 Lisp_Object glyphs;
15305 {
15306 struct window *w = XWINDOW (selected_window);
15307 struct buffer *buffer = XBUFFER (w->buffer);
15308
15309 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15310 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15311 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15312 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15313 fprintf (stderr, "=============================================\n");
15314 dump_glyph_matrix (w->current_matrix,
15315 NILP (glyphs) ? 0 : XINT (glyphs));
15316 return Qnil;
15317 }
15318
15319
15320 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15321 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15322 ()
15323 {
15324 struct frame *f = XFRAME (selected_frame);
15325 dump_glyph_matrix (f->current_matrix, 1);
15326 return Qnil;
15327 }
15328
15329
15330 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15331 doc: /* Dump glyph row ROW to stderr.
15332 GLYPH 0 means don't dump glyphs.
15333 GLYPH 1 means dump glyphs in short form.
15334 GLYPH > 1 or omitted means dump glyphs in long form. */)
15335 (row, glyphs)
15336 Lisp_Object row, glyphs;
15337 {
15338 struct glyph_matrix *matrix;
15339 int vpos;
15340
15341 CHECK_NUMBER (row);
15342 matrix = XWINDOW (selected_window)->current_matrix;
15343 vpos = XINT (row);
15344 if (vpos >= 0 && vpos < matrix->nrows)
15345 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15346 vpos,
15347 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15348 return Qnil;
15349 }
15350
15351
15352 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15353 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15354 GLYPH 0 means don't dump glyphs.
15355 GLYPH 1 means dump glyphs in short form.
15356 GLYPH > 1 or omitted means dump glyphs in long form. */)
15357 (row, glyphs)
15358 Lisp_Object row, glyphs;
15359 {
15360 struct frame *sf = SELECTED_FRAME ();
15361 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15362 int vpos;
15363
15364 CHECK_NUMBER (row);
15365 vpos = XINT (row);
15366 if (vpos >= 0 && vpos < m->nrows)
15367 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15368 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15369 return Qnil;
15370 }
15371
15372
15373 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15374 doc: /* Toggle tracing of redisplay.
15375 With ARG, turn tracing on if and only if ARG is positive. */)
15376 (arg)
15377 Lisp_Object arg;
15378 {
15379 if (NILP (arg))
15380 trace_redisplay_p = !trace_redisplay_p;
15381 else
15382 {
15383 arg = Fprefix_numeric_value (arg);
15384 trace_redisplay_p = XINT (arg) > 0;
15385 }
15386
15387 return Qnil;
15388 }
15389
15390
15391 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15392 doc: /* Like `format', but print result to stderr.
15393 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15394 (nargs, args)
15395 int nargs;
15396 Lisp_Object *args;
15397 {
15398 Lisp_Object s = Fformat (nargs, args);
15399 fprintf (stderr, "%s", SDATA (s));
15400 return Qnil;
15401 }
15402
15403 #endif /* GLYPH_DEBUG */
15404
15405
15406 \f
15407 /***********************************************************************
15408 Building Desired Matrix Rows
15409 ***********************************************************************/
15410
15411 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15412 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15413
15414 static struct glyph_row *
15415 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15416 struct window *w;
15417 Lisp_Object overlay_arrow_string;
15418 {
15419 struct frame *f = XFRAME (WINDOW_FRAME (w));
15420 struct buffer *buffer = XBUFFER (w->buffer);
15421 struct buffer *old = current_buffer;
15422 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15423 int arrow_len = SCHARS (overlay_arrow_string);
15424 const unsigned char *arrow_end = arrow_string + arrow_len;
15425 const unsigned char *p;
15426 struct it it;
15427 int multibyte_p;
15428 int n_glyphs_before;
15429
15430 set_buffer_temp (buffer);
15431 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15432 it.glyph_row->used[TEXT_AREA] = 0;
15433 SET_TEXT_POS (it.position, 0, 0);
15434
15435 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15436 p = arrow_string;
15437 while (p < arrow_end)
15438 {
15439 Lisp_Object face, ilisp;
15440
15441 /* Get the next character. */
15442 if (multibyte_p)
15443 it.c = string_char_and_length (p, arrow_len, &it.len);
15444 else
15445 it.c = *p, it.len = 1;
15446 p += it.len;
15447
15448 /* Get its face. */
15449 ilisp = make_number (p - arrow_string);
15450 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15451 it.face_id = compute_char_face (f, it.c, face);
15452
15453 /* Compute its width, get its glyphs. */
15454 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15455 SET_TEXT_POS (it.position, -1, -1);
15456 PRODUCE_GLYPHS (&it);
15457
15458 /* If this character doesn't fit any more in the line, we have
15459 to remove some glyphs. */
15460 if (it.current_x > it.last_visible_x)
15461 {
15462 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15463 break;
15464 }
15465 }
15466
15467 set_buffer_temp (old);
15468 return it.glyph_row;
15469 }
15470
15471
15472 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15473 glyphs are only inserted for terminal frames since we can't really
15474 win with truncation glyphs when partially visible glyphs are
15475 involved. Which glyphs to insert is determined by
15476 produce_special_glyphs. */
15477
15478 static void
15479 insert_left_trunc_glyphs (it)
15480 struct it *it;
15481 {
15482 struct it truncate_it;
15483 struct glyph *from, *end, *to, *toend;
15484
15485 xassert (!FRAME_WINDOW_P (it->f));
15486
15487 /* Get the truncation glyphs. */
15488 truncate_it = *it;
15489 truncate_it.current_x = 0;
15490 truncate_it.face_id = DEFAULT_FACE_ID;
15491 truncate_it.glyph_row = &scratch_glyph_row;
15492 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15493 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15494 truncate_it.object = make_number (0);
15495 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15496
15497 /* Overwrite glyphs from IT with truncation glyphs. */
15498 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15499 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15500 to = it->glyph_row->glyphs[TEXT_AREA];
15501 toend = to + it->glyph_row->used[TEXT_AREA];
15502
15503 while (from < end)
15504 *to++ = *from++;
15505
15506 /* There may be padding glyphs left over. Overwrite them too. */
15507 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15508 {
15509 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15510 while (from < end)
15511 *to++ = *from++;
15512 }
15513
15514 if (to > toend)
15515 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15516 }
15517
15518
15519 /* Compute the pixel height and width of IT->glyph_row.
15520
15521 Most of the time, ascent and height of a display line will be equal
15522 to the max_ascent and max_height values of the display iterator
15523 structure. This is not the case if
15524
15525 1. We hit ZV without displaying anything. In this case, max_ascent
15526 and max_height will be zero.
15527
15528 2. We have some glyphs that don't contribute to the line height.
15529 (The glyph row flag contributes_to_line_height_p is for future
15530 pixmap extensions).
15531
15532 The first case is easily covered by using default values because in
15533 these cases, the line height does not really matter, except that it
15534 must not be zero. */
15535
15536 static void
15537 compute_line_metrics (it)
15538 struct it *it;
15539 {
15540 struct glyph_row *row = it->glyph_row;
15541 int area, i;
15542
15543 if (FRAME_WINDOW_P (it->f))
15544 {
15545 int i, min_y, max_y;
15546
15547 /* The line may consist of one space only, that was added to
15548 place the cursor on it. If so, the row's height hasn't been
15549 computed yet. */
15550 if (row->height == 0)
15551 {
15552 if (it->max_ascent + it->max_descent == 0)
15553 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15554 row->ascent = it->max_ascent;
15555 row->height = it->max_ascent + it->max_descent;
15556 row->phys_ascent = it->max_phys_ascent;
15557 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15558 row->extra_line_spacing = it->max_extra_line_spacing;
15559 }
15560
15561 /* Compute the width of this line. */
15562 row->pixel_width = row->x;
15563 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15564 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15565
15566 xassert (row->pixel_width >= 0);
15567 xassert (row->ascent >= 0 && row->height > 0);
15568
15569 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15570 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15571
15572 /* If first line's physical ascent is larger than its logical
15573 ascent, use the physical ascent, and make the row taller.
15574 This makes accented characters fully visible. */
15575 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15576 && row->phys_ascent > row->ascent)
15577 {
15578 row->height += row->phys_ascent - row->ascent;
15579 row->ascent = row->phys_ascent;
15580 }
15581
15582 /* Compute how much of the line is visible. */
15583 row->visible_height = row->height;
15584
15585 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15586 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15587
15588 if (row->y < min_y)
15589 row->visible_height -= min_y - row->y;
15590 if (row->y + row->height > max_y)
15591 row->visible_height -= row->y + row->height - max_y;
15592 }
15593 else
15594 {
15595 row->pixel_width = row->used[TEXT_AREA];
15596 if (row->continued_p)
15597 row->pixel_width -= it->continuation_pixel_width;
15598 else if (row->truncated_on_right_p)
15599 row->pixel_width -= it->truncation_pixel_width;
15600 row->ascent = row->phys_ascent = 0;
15601 row->height = row->phys_height = row->visible_height = 1;
15602 row->extra_line_spacing = 0;
15603 }
15604
15605 /* Compute a hash code for this row. */
15606 row->hash = 0;
15607 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15608 for (i = 0; i < row->used[area]; ++i)
15609 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15610 + row->glyphs[area][i].u.val
15611 + row->glyphs[area][i].face_id
15612 + row->glyphs[area][i].padding_p
15613 + (row->glyphs[area][i].type << 2));
15614
15615 it->max_ascent = it->max_descent = 0;
15616 it->max_phys_ascent = it->max_phys_descent = 0;
15617 }
15618
15619
15620 /* Append one space to the glyph row of iterator IT if doing a
15621 window-based redisplay. The space has the same face as
15622 IT->face_id. Value is non-zero if a space was added.
15623
15624 This function is called to make sure that there is always one glyph
15625 at the end of a glyph row that the cursor can be set on under
15626 window-systems. (If there weren't such a glyph we would not know
15627 how wide and tall a box cursor should be displayed).
15628
15629 At the same time this space let's a nicely handle clearing to the
15630 end of the line if the row ends in italic text. */
15631
15632 static int
15633 append_space_for_newline (it, default_face_p)
15634 struct it *it;
15635 int default_face_p;
15636 {
15637 if (FRAME_WINDOW_P (it->f))
15638 {
15639 int n = it->glyph_row->used[TEXT_AREA];
15640
15641 if (it->glyph_row->glyphs[TEXT_AREA] + n
15642 < it->glyph_row->glyphs[1 + TEXT_AREA])
15643 {
15644 /* Save some values that must not be changed.
15645 Must save IT->c and IT->len because otherwise
15646 ITERATOR_AT_END_P wouldn't work anymore after
15647 append_space_for_newline has been called. */
15648 enum display_element_type saved_what = it->what;
15649 int saved_c = it->c, saved_len = it->len;
15650 int saved_x = it->current_x;
15651 int saved_face_id = it->face_id;
15652 struct text_pos saved_pos;
15653 Lisp_Object saved_object;
15654 struct face *face;
15655
15656 saved_object = it->object;
15657 saved_pos = it->position;
15658
15659 it->what = IT_CHARACTER;
15660 bzero (&it->position, sizeof it->position);
15661 it->object = make_number (0);
15662 it->c = ' ';
15663 it->len = 1;
15664
15665 if (default_face_p)
15666 it->face_id = DEFAULT_FACE_ID;
15667 else if (it->face_before_selective_p)
15668 it->face_id = it->saved_face_id;
15669 face = FACE_FROM_ID (it->f, it->face_id);
15670 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15671
15672 PRODUCE_GLYPHS (it);
15673
15674 it->override_ascent = -1;
15675 it->constrain_row_ascent_descent_p = 0;
15676 it->current_x = saved_x;
15677 it->object = saved_object;
15678 it->position = saved_pos;
15679 it->what = saved_what;
15680 it->face_id = saved_face_id;
15681 it->len = saved_len;
15682 it->c = saved_c;
15683 return 1;
15684 }
15685 }
15686
15687 return 0;
15688 }
15689
15690
15691 /* Extend the face of the last glyph in the text area of IT->glyph_row
15692 to the end of the display line. Called from display_line.
15693 If the glyph row is empty, add a space glyph to it so that we
15694 know the face to draw. Set the glyph row flag fill_line_p. */
15695
15696 static void
15697 extend_face_to_end_of_line (it)
15698 struct it *it;
15699 {
15700 struct face *face;
15701 struct frame *f = it->f;
15702
15703 /* If line is already filled, do nothing. */
15704 if (it->current_x >= it->last_visible_x)
15705 return;
15706
15707 /* Face extension extends the background and box of IT->face_id
15708 to the end of the line. If the background equals the background
15709 of the frame, we don't have to do anything. */
15710 if (it->face_before_selective_p)
15711 face = FACE_FROM_ID (it->f, it->saved_face_id);
15712 else
15713 face = FACE_FROM_ID (f, it->face_id);
15714
15715 if (FRAME_WINDOW_P (f)
15716 && it->glyph_row->displays_text_p
15717 && face->box == FACE_NO_BOX
15718 && face->background == FRAME_BACKGROUND_PIXEL (f)
15719 && !face->stipple)
15720 return;
15721
15722 /* Set the glyph row flag indicating that the face of the last glyph
15723 in the text area has to be drawn to the end of the text area. */
15724 it->glyph_row->fill_line_p = 1;
15725
15726 /* If current character of IT is not ASCII, make sure we have the
15727 ASCII face. This will be automatically undone the next time
15728 get_next_display_element returns a multibyte character. Note
15729 that the character will always be single byte in unibyte text. */
15730 if (!SINGLE_BYTE_CHAR_P (it->c))
15731 {
15732 it->face_id = FACE_FOR_CHAR (f, face, 0);
15733 }
15734
15735 if (FRAME_WINDOW_P (f))
15736 {
15737 /* If the row is empty, add a space with the current face of IT,
15738 so that we know which face to draw. */
15739 if (it->glyph_row->used[TEXT_AREA] == 0)
15740 {
15741 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15742 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15743 it->glyph_row->used[TEXT_AREA] = 1;
15744 }
15745 }
15746 else
15747 {
15748 /* Save some values that must not be changed. */
15749 int saved_x = it->current_x;
15750 struct text_pos saved_pos;
15751 Lisp_Object saved_object;
15752 enum display_element_type saved_what = it->what;
15753 int saved_face_id = it->face_id;
15754
15755 saved_object = it->object;
15756 saved_pos = it->position;
15757
15758 it->what = IT_CHARACTER;
15759 bzero (&it->position, sizeof it->position);
15760 it->object = make_number (0);
15761 it->c = ' ';
15762 it->len = 1;
15763 it->face_id = face->id;
15764
15765 PRODUCE_GLYPHS (it);
15766
15767 while (it->current_x <= it->last_visible_x)
15768 PRODUCE_GLYPHS (it);
15769
15770 /* Don't count these blanks really. It would let us insert a left
15771 truncation glyph below and make us set the cursor on them, maybe. */
15772 it->current_x = saved_x;
15773 it->object = saved_object;
15774 it->position = saved_pos;
15775 it->what = saved_what;
15776 it->face_id = saved_face_id;
15777 }
15778 }
15779
15780
15781 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15782 trailing whitespace. */
15783
15784 static int
15785 trailing_whitespace_p (charpos)
15786 int charpos;
15787 {
15788 int bytepos = CHAR_TO_BYTE (charpos);
15789 int c = 0;
15790
15791 while (bytepos < ZV_BYTE
15792 && (c = FETCH_CHAR (bytepos),
15793 c == ' ' || c == '\t'))
15794 ++bytepos;
15795
15796 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15797 {
15798 if (bytepos != PT_BYTE)
15799 return 1;
15800 }
15801 return 0;
15802 }
15803
15804
15805 /* Highlight trailing whitespace, if any, in ROW. */
15806
15807 void
15808 highlight_trailing_whitespace (f, row)
15809 struct frame *f;
15810 struct glyph_row *row;
15811 {
15812 int used = row->used[TEXT_AREA];
15813
15814 if (used)
15815 {
15816 struct glyph *start = row->glyphs[TEXT_AREA];
15817 struct glyph *glyph = start + used - 1;
15818
15819 /* Skip over glyphs inserted to display the cursor at the
15820 end of a line, for extending the face of the last glyph
15821 to the end of the line on terminals, and for truncation
15822 and continuation glyphs. */
15823 while (glyph >= start
15824 && glyph->type == CHAR_GLYPH
15825 && INTEGERP (glyph->object))
15826 --glyph;
15827
15828 /* If last glyph is a space or stretch, and it's trailing
15829 whitespace, set the face of all trailing whitespace glyphs in
15830 IT->glyph_row to `trailing-whitespace'. */
15831 if (glyph >= start
15832 && BUFFERP (glyph->object)
15833 && (glyph->type == STRETCH_GLYPH
15834 || (glyph->type == CHAR_GLYPH
15835 && glyph->u.ch == ' '))
15836 && trailing_whitespace_p (glyph->charpos))
15837 {
15838 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15839 if (face_id < 0)
15840 return;
15841
15842 while (glyph >= start
15843 && BUFFERP (glyph->object)
15844 && (glyph->type == STRETCH_GLYPH
15845 || (glyph->type == CHAR_GLYPH
15846 && glyph->u.ch == ' ')))
15847 (glyph--)->face_id = face_id;
15848 }
15849 }
15850 }
15851
15852
15853 /* Value is non-zero if glyph row ROW in window W should be
15854 used to hold the cursor. */
15855
15856 static int
15857 cursor_row_p (w, row)
15858 struct window *w;
15859 struct glyph_row *row;
15860 {
15861 int cursor_row_p = 1;
15862
15863 if (PT == MATRIX_ROW_END_CHARPOS (row))
15864 {
15865 /* Suppose the row ends on a string.
15866 Unless the row is continued, that means it ends on a newline
15867 in the string. If it's anything other than a display string
15868 (e.g. a before-string from an overlay), we don't want the
15869 cursor there. (This heuristic seems to give the optimal
15870 behavior for the various types of multi-line strings.) */
15871 if (CHARPOS (row->end.string_pos) >= 0)
15872 {
15873 if (row->continued_p)
15874 cursor_row_p = 1;
15875 else
15876 {
15877 /* Check for `display' property. */
15878 struct glyph *beg = row->glyphs[TEXT_AREA];
15879 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15880 struct glyph *glyph;
15881
15882 cursor_row_p = 0;
15883 for (glyph = end; glyph >= beg; --glyph)
15884 if (STRINGP (glyph->object))
15885 {
15886 Lisp_Object prop
15887 = Fget_char_property (make_number (PT),
15888 Qdisplay, Qnil);
15889 cursor_row_p =
15890 (!NILP (prop)
15891 && display_prop_string_p (prop, glyph->object));
15892 break;
15893 }
15894 }
15895 }
15896 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15897 {
15898 /* If the row ends in middle of a real character,
15899 and the line is continued, we want the cursor here.
15900 That's because MATRIX_ROW_END_CHARPOS would equal
15901 PT if PT is before the character. */
15902 if (!row->ends_in_ellipsis_p)
15903 cursor_row_p = row->continued_p;
15904 else
15905 /* If the row ends in an ellipsis, then
15906 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15907 We want that position to be displayed after the ellipsis. */
15908 cursor_row_p = 0;
15909 }
15910 /* If the row ends at ZV, display the cursor at the end of that
15911 row instead of at the start of the row below. */
15912 else if (row->ends_at_zv_p)
15913 cursor_row_p = 1;
15914 else
15915 cursor_row_p = 0;
15916 }
15917
15918 return cursor_row_p;
15919 }
15920
15921
15922 /* Construct the glyph row IT->glyph_row in the desired matrix of
15923 IT->w from text at the current position of IT. See dispextern.h
15924 for an overview of struct it. Value is non-zero if
15925 IT->glyph_row displays text, as opposed to a line displaying ZV
15926 only. */
15927
15928 static int
15929 display_line (it)
15930 struct it *it;
15931 {
15932 struct glyph_row *row = it->glyph_row;
15933 Lisp_Object overlay_arrow_string;
15934
15935 /* We always start displaying at hpos zero even if hscrolled. */
15936 xassert (it->hpos == 0 && it->current_x == 0);
15937
15938 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15939 >= it->w->desired_matrix->nrows)
15940 {
15941 it->w->nrows_scale_factor++;
15942 fonts_changed_p = 1;
15943 return 0;
15944 }
15945
15946 /* Is IT->w showing the region? */
15947 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15948
15949 /* Clear the result glyph row and enable it. */
15950 prepare_desired_row (row);
15951
15952 row->y = it->current_y;
15953 row->start = it->start;
15954 row->continuation_lines_width = it->continuation_lines_width;
15955 row->displays_text_p = 1;
15956 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15957 it->starts_in_middle_of_char_p = 0;
15958
15959 /* Arrange the overlays nicely for our purposes. Usually, we call
15960 display_line on only one line at a time, in which case this
15961 can't really hurt too much, or we call it on lines which appear
15962 one after another in the buffer, in which case all calls to
15963 recenter_overlay_lists but the first will be pretty cheap. */
15964 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15965
15966 /* Move over display elements that are not visible because we are
15967 hscrolled. This may stop at an x-position < IT->first_visible_x
15968 if the first glyph is partially visible or if we hit a line end. */
15969 if (it->current_x < it->first_visible_x)
15970 {
15971 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15972 MOVE_TO_POS | MOVE_TO_X);
15973 }
15974
15975 /* Get the initial row height. This is either the height of the
15976 text hscrolled, if there is any, or zero. */
15977 row->ascent = it->max_ascent;
15978 row->height = it->max_ascent + it->max_descent;
15979 row->phys_ascent = it->max_phys_ascent;
15980 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15981 row->extra_line_spacing = it->max_extra_line_spacing;
15982
15983 /* Loop generating characters. The loop is left with IT on the next
15984 character to display. */
15985 while (1)
15986 {
15987 int n_glyphs_before, hpos_before, x_before;
15988 int x, i, nglyphs;
15989 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15990
15991 /* Retrieve the next thing to display. Value is zero if end of
15992 buffer reached. */
15993 if (!get_next_display_element (it))
15994 {
15995 /* Maybe add a space at the end of this line that is used to
15996 display the cursor there under X. Set the charpos of the
15997 first glyph of blank lines not corresponding to any text
15998 to -1. */
15999 #ifdef HAVE_WINDOW_SYSTEM
16000 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16001 row->exact_window_width_line_p = 1;
16002 else
16003 #endif /* HAVE_WINDOW_SYSTEM */
16004 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16005 || row->used[TEXT_AREA] == 0)
16006 {
16007 row->glyphs[TEXT_AREA]->charpos = -1;
16008 row->displays_text_p = 0;
16009
16010 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16011 && (!MINI_WINDOW_P (it->w)
16012 || (minibuf_level && EQ (it->window, minibuf_window))))
16013 row->indicate_empty_line_p = 1;
16014 }
16015
16016 it->continuation_lines_width = 0;
16017 row->ends_at_zv_p = 1;
16018 break;
16019 }
16020
16021 /* Now, get the metrics of what we want to display. This also
16022 generates glyphs in `row' (which is IT->glyph_row). */
16023 n_glyphs_before = row->used[TEXT_AREA];
16024 x = it->current_x;
16025
16026 /* Remember the line height so far in case the next element doesn't
16027 fit on the line. */
16028 if (!it->truncate_lines_p)
16029 {
16030 ascent = it->max_ascent;
16031 descent = it->max_descent;
16032 phys_ascent = it->max_phys_ascent;
16033 phys_descent = it->max_phys_descent;
16034 }
16035
16036 PRODUCE_GLYPHS (it);
16037
16038 /* If this display element was in marginal areas, continue with
16039 the next one. */
16040 if (it->area != TEXT_AREA)
16041 {
16042 row->ascent = max (row->ascent, it->max_ascent);
16043 row->height = max (row->height, it->max_ascent + it->max_descent);
16044 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16045 row->phys_height = max (row->phys_height,
16046 it->max_phys_ascent + it->max_phys_descent);
16047 row->extra_line_spacing = max (row->extra_line_spacing,
16048 it->max_extra_line_spacing);
16049 set_iterator_to_next (it, 1);
16050 continue;
16051 }
16052
16053 /* Does the display element fit on the line? If we truncate
16054 lines, we should draw past the right edge of the window. If
16055 we don't truncate, we want to stop so that we can display the
16056 continuation glyph before the right margin. If lines are
16057 continued, there are two possible strategies for characters
16058 resulting in more than 1 glyph (e.g. tabs): Display as many
16059 glyphs as possible in this line and leave the rest for the
16060 continuation line, or display the whole element in the next
16061 line. Original redisplay did the former, so we do it also. */
16062 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16063 hpos_before = it->hpos;
16064 x_before = x;
16065
16066 if (/* Not a newline. */
16067 nglyphs > 0
16068 /* Glyphs produced fit entirely in the line. */
16069 && it->current_x < it->last_visible_x)
16070 {
16071 it->hpos += nglyphs;
16072 row->ascent = max (row->ascent, it->max_ascent);
16073 row->height = max (row->height, it->max_ascent + it->max_descent);
16074 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16075 row->phys_height = max (row->phys_height,
16076 it->max_phys_ascent + it->max_phys_descent);
16077 row->extra_line_spacing = max (row->extra_line_spacing,
16078 it->max_extra_line_spacing);
16079 if (it->current_x - it->pixel_width < it->first_visible_x)
16080 row->x = x - it->first_visible_x;
16081 }
16082 else
16083 {
16084 int new_x;
16085 struct glyph *glyph;
16086
16087 for (i = 0; i < nglyphs; ++i, x = new_x)
16088 {
16089 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16090 new_x = x + glyph->pixel_width;
16091
16092 if (/* Lines are continued. */
16093 !it->truncate_lines_p
16094 && (/* Glyph doesn't fit on the line. */
16095 new_x > it->last_visible_x
16096 /* Or it fits exactly on a window system frame. */
16097 || (new_x == it->last_visible_x
16098 && FRAME_WINDOW_P (it->f))))
16099 {
16100 /* End of a continued line. */
16101
16102 if (it->hpos == 0
16103 || (new_x == it->last_visible_x
16104 && FRAME_WINDOW_P (it->f)))
16105 {
16106 /* Current glyph is the only one on the line or
16107 fits exactly on the line. We must continue
16108 the line because we can't draw the cursor
16109 after the glyph. */
16110 row->continued_p = 1;
16111 it->current_x = new_x;
16112 it->continuation_lines_width += new_x;
16113 ++it->hpos;
16114 if (i == nglyphs - 1)
16115 {
16116 set_iterator_to_next (it, 1);
16117 #ifdef HAVE_WINDOW_SYSTEM
16118 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16119 {
16120 if (!get_next_display_element (it))
16121 {
16122 row->exact_window_width_line_p = 1;
16123 it->continuation_lines_width = 0;
16124 row->continued_p = 0;
16125 row->ends_at_zv_p = 1;
16126 }
16127 else if (ITERATOR_AT_END_OF_LINE_P (it))
16128 {
16129 row->continued_p = 0;
16130 row->exact_window_width_line_p = 1;
16131 }
16132 }
16133 #endif /* HAVE_WINDOW_SYSTEM */
16134 }
16135 }
16136 else if (CHAR_GLYPH_PADDING_P (*glyph)
16137 && !FRAME_WINDOW_P (it->f))
16138 {
16139 /* A padding glyph that doesn't fit on this line.
16140 This means the whole character doesn't fit
16141 on the line. */
16142 row->used[TEXT_AREA] = n_glyphs_before;
16143
16144 /* Fill the rest of the row with continuation
16145 glyphs like in 20.x. */
16146 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16147 < row->glyphs[1 + TEXT_AREA])
16148 produce_special_glyphs (it, IT_CONTINUATION);
16149
16150 row->continued_p = 1;
16151 it->current_x = x_before;
16152 it->continuation_lines_width += x_before;
16153
16154 /* Restore the height to what it was before the
16155 element not fitting on the line. */
16156 it->max_ascent = ascent;
16157 it->max_descent = descent;
16158 it->max_phys_ascent = phys_ascent;
16159 it->max_phys_descent = phys_descent;
16160 }
16161 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16162 {
16163 /* A TAB that extends past the right edge of the
16164 window. This produces a single glyph on
16165 window system frames. We leave the glyph in
16166 this row and let it fill the row, but don't
16167 consume the TAB. */
16168 it->continuation_lines_width += it->last_visible_x;
16169 row->ends_in_middle_of_char_p = 1;
16170 row->continued_p = 1;
16171 glyph->pixel_width = it->last_visible_x - x;
16172 it->starts_in_middle_of_char_p = 1;
16173 }
16174 else
16175 {
16176 /* Something other than a TAB that draws past
16177 the right edge of the window. Restore
16178 positions to values before the element. */
16179 row->used[TEXT_AREA] = n_glyphs_before + i;
16180
16181 /* Display continuation glyphs. */
16182 if (!FRAME_WINDOW_P (it->f))
16183 produce_special_glyphs (it, IT_CONTINUATION);
16184 row->continued_p = 1;
16185
16186 it->current_x = x_before;
16187 it->continuation_lines_width += x;
16188 extend_face_to_end_of_line (it);
16189
16190 if (nglyphs > 1 && i > 0)
16191 {
16192 row->ends_in_middle_of_char_p = 1;
16193 it->starts_in_middle_of_char_p = 1;
16194 }
16195
16196 /* Restore the height to what it was before the
16197 element not fitting on the line. */
16198 it->max_ascent = ascent;
16199 it->max_descent = descent;
16200 it->max_phys_ascent = phys_ascent;
16201 it->max_phys_descent = phys_descent;
16202 }
16203
16204 break;
16205 }
16206 else if (new_x > it->first_visible_x)
16207 {
16208 /* Increment number of glyphs actually displayed. */
16209 ++it->hpos;
16210
16211 if (x < it->first_visible_x)
16212 /* Glyph is partially visible, i.e. row starts at
16213 negative X position. */
16214 row->x = x - it->first_visible_x;
16215 }
16216 else
16217 {
16218 /* Glyph is completely off the left margin of the
16219 window. This should not happen because of the
16220 move_it_in_display_line at the start of this
16221 function, unless the text display area of the
16222 window is empty. */
16223 xassert (it->first_visible_x <= it->last_visible_x);
16224 }
16225 }
16226
16227 row->ascent = max (row->ascent, it->max_ascent);
16228 row->height = max (row->height, it->max_ascent + it->max_descent);
16229 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16230 row->phys_height = max (row->phys_height,
16231 it->max_phys_ascent + it->max_phys_descent);
16232 row->extra_line_spacing = max (row->extra_line_spacing,
16233 it->max_extra_line_spacing);
16234
16235 /* End of this display line if row is continued. */
16236 if (row->continued_p || row->ends_at_zv_p)
16237 break;
16238 }
16239
16240 at_end_of_line:
16241 /* Is this a line end? If yes, we're also done, after making
16242 sure that a non-default face is extended up to the right
16243 margin of the window. */
16244 if (ITERATOR_AT_END_OF_LINE_P (it))
16245 {
16246 int used_before = row->used[TEXT_AREA];
16247
16248 row->ends_in_newline_from_string_p = STRINGP (it->object);
16249
16250 #ifdef HAVE_WINDOW_SYSTEM
16251 /* Add a space at the end of the line that is used to
16252 display the cursor there. */
16253 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16254 append_space_for_newline (it, 0);
16255 #endif /* HAVE_WINDOW_SYSTEM */
16256
16257 /* Extend the face to the end of the line. */
16258 extend_face_to_end_of_line (it);
16259
16260 /* Make sure we have the position. */
16261 if (used_before == 0)
16262 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16263
16264 /* Consume the line end. This skips over invisible lines. */
16265 set_iterator_to_next (it, 1);
16266 it->continuation_lines_width = 0;
16267 break;
16268 }
16269
16270 /* Proceed with next display element. Note that this skips
16271 over lines invisible because of selective display. */
16272 set_iterator_to_next (it, 1);
16273
16274 /* If we truncate lines, we are done when the last displayed
16275 glyphs reach past the right margin of the window. */
16276 if (it->truncate_lines_p
16277 && (FRAME_WINDOW_P (it->f)
16278 ? (it->current_x >= it->last_visible_x)
16279 : (it->current_x > it->last_visible_x)))
16280 {
16281 /* Maybe add truncation glyphs. */
16282 if (!FRAME_WINDOW_P (it->f))
16283 {
16284 int i, n;
16285
16286 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16287 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16288 break;
16289
16290 for (n = row->used[TEXT_AREA]; i < n; ++i)
16291 {
16292 row->used[TEXT_AREA] = i;
16293 produce_special_glyphs (it, IT_TRUNCATION);
16294 }
16295 }
16296 #ifdef HAVE_WINDOW_SYSTEM
16297 else
16298 {
16299 /* Don't truncate if we can overflow newline into fringe. */
16300 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16301 {
16302 if (!get_next_display_element (it))
16303 {
16304 it->continuation_lines_width = 0;
16305 row->ends_at_zv_p = 1;
16306 row->exact_window_width_line_p = 1;
16307 break;
16308 }
16309 if (ITERATOR_AT_END_OF_LINE_P (it))
16310 {
16311 row->exact_window_width_line_p = 1;
16312 goto at_end_of_line;
16313 }
16314 }
16315 }
16316 #endif /* HAVE_WINDOW_SYSTEM */
16317
16318 row->truncated_on_right_p = 1;
16319 it->continuation_lines_width = 0;
16320 reseat_at_next_visible_line_start (it, 0);
16321 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16322 it->hpos = hpos_before;
16323 it->current_x = x_before;
16324 break;
16325 }
16326 }
16327
16328 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16329 at the left window margin. */
16330 if (it->first_visible_x
16331 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16332 {
16333 if (!FRAME_WINDOW_P (it->f))
16334 insert_left_trunc_glyphs (it);
16335 row->truncated_on_left_p = 1;
16336 }
16337
16338 /* If the start of this line is the overlay arrow-position, then
16339 mark this glyph row as the one containing the overlay arrow.
16340 This is clearly a mess with variable size fonts. It would be
16341 better to let it be displayed like cursors under X. */
16342 if ((row->displays_text_p || !overlay_arrow_seen)
16343 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16344 !NILP (overlay_arrow_string)))
16345 {
16346 /* Overlay arrow in window redisplay is a fringe bitmap. */
16347 if (STRINGP (overlay_arrow_string))
16348 {
16349 struct glyph_row *arrow_row
16350 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16351 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16352 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16353 struct glyph *p = row->glyphs[TEXT_AREA];
16354 struct glyph *p2, *end;
16355
16356 /* Copy the arrow glyphs. */
16357 while (glyph < arrow_end)
16358 *p++ = *glyph++;
16359
16360 /* Throw away padding glyphs. */
16361 p2 = p;
16362 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16363 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16364 ++p2;
16365 if (p2 > p)
16366 {
16367 while (p2 < end)
16368 *p++ = *p2++;
16369 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16370 }
16371 }
16372 else
16373 {
16374 xassert (INTEGERP (overlay_arrow_string));
16375 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16376 }
16377 overlay_arrow_seen = 1;
16378 }
16379
16380 /* Compute pixel dimensions of this line. */
16381 compute_line_metrics (it);
16382
16383 /* Remember the position at which this line ends. */
16384 row->end = it->current;
16385
16386 /* Record whether this row ends inside an ellipsis. */
16387 row->ends_in_ellipsis_p
16388 = (it->method == GET_FROM_DISPLAY_VECTOR
16389 && it->ellipsis_p);
16390
16391 /* Save fringe bitmaps in this row. */
16392 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16393 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16394 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16395 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16396
16397 it->left_user_fringe_bitmap = 0;
16398 it->left_user_fringe_face_id = 0;
16399 it->right_user_fringe_bitmap = 0;
16400 it->right_user_fringe_face_id = 0;
16401
16402 /* Maybe set the cursor. */
16403 if (it->w->cursor.vpos < 0
16404 && PT >= MATRIX_ROW_START_CHARPOS (row)
16405 && PT <= MATRIX_ROW_END_CHARPOS (row)
16406 && cursor_row_p (it->w, row))
16407 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16408
16409 /* Highlight trailing whitespace. */
16410 if (!NILP (Vshow_trailing_whitespace))
16411 highlight_trailing_whitespace (it->f, it->glyph_row);
16412
16413 /* Prepare for the next line. This line starts horizontally at (X
16414 HPOS) = (0 0). Vertical positions are incremented. As a
16415 convenience for the caller, IT->glyph_row is set to the next
16416 row to be used. */
16417 it->current_x = it->hpos = 0;
16418 it->current_y += row->height;
16419 ++it->vpos;
16420 ++it->glyph_row;
16421 it->start = it->current;
16422 return row->displays_text_p;
16423 }
16424
16425
16426 \f
16427 /***********************************************************************
16428 Menu Bar
16429 ***********************************************************************/
16430
16431 /* Redisplay the menu bar in the frame for window W.
16432
16433 The menu bar of X frames that don't have X toolkit support is
16434 displayed in a special window W->frame->menu_bar_window.
16435
16436 The menu bar of terminal frames is treated specially as far as
16437 glyph matrices are concerned. Menu bar lines are not part of
16438 windows, so the update is done directly on the frame matrix rows
16439 for the menu bar. */
16440
16441 static void
16442 display_menu_bar (w)
16443 struct window *w;
16444 {
16445 struct frame *f = XFRAME (WINDOW_FRAME (w));
16446 struct it it;
16447 Lisp_Object items;
16448 int i;
16449
16450 /* Don't do all this for graphical frames. */
16451 #ifdef HAVE_NTGUI
16452 if (!NILP (Vwindow_system))
16453 return;
16454 #endif
16455 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16456 if (FRAME_X_P (f))
16457 return;
16458 #endif
16459 #ifdef MAC_OS
16460 if (FRAME_MAC_P (f))
16461 return;
16462 #endif
16463
16464 #ifdef USE_X_TOOLKIT
16465 xassert (!FRAME_WINDOW_P (f));
16466 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16467 it.first_visible_x = 0;
16468 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16469 #else /* not USE_X_TOOLKIT */
16470 if (FRAME_WINDOW_P (f))
16471 {
16472 /* Menu bar lines are displayed in the desired matrix of the
16473 dummy window menu_bar_window. */
16474 struct window *menu_w;
16475 xassert (WINDOWP (f->menu_bar_window));
16476 menu_w = XWINDOW (f->menu_bar_window);
16477 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16478 MENU_FACE_ID);
16479 it.first_visible_x = 0;
16480 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16481 }
16482 else
16483 {
16484 /* This is a TTY frame, i.e. character hpos/vpos are used as
16485 pixel x/y. */
16486 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16487 MENU_FACE_ID);
16488 it.first_visible_x = 0;
16489 it.last_visible_x = FRAME_COLS (f);
16490 }
16491 #endif /* not USE_X_TOOLKIT */
16492
16493 if (! mode_line_inverse_video)
16494 /* Force the menu-bar to be displayed in the default face. */
16495 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16496
16497 /* Clear all rows of the menu bar. */
16498 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16499 {
16500 struct glyph_row *row = it.glyph_row + i;
16501 clear_glyph_row (row);
16502 row->enabled_p = 1;
16503 row->full_width_p = 1;
16504 }
16505
16506 /* Display all items of the menu bar. */
16507 items = FRAME_MENU_BAR_ITEMS (it.f);
16508 for (i = 0; i < XVECTOR (items)->size; i += 4)
16509 {
16510 Lisp_Object string;
16511
16512 /* Stop at nil string. */
16513 string = AREF (items, i + 1);
16514 if (NILP (string))
16515 break;
16516
16517 /* Remember where item was displayed. */
16518 AREF (items, i + 3) = make_number (it.hpos);
16519
16520 /* Display the item, pad with one space. */
16521 if (it.current_x < it.last_visible_x)
16522 display_string (NULL, string, Qnil, 0, 0, &it,
16523 SCHARS (string) + 1, 0, 0, -1);
16524 }
16525
16526 /* Fill out the line with spaces. */
16527 if (it.current_x < it.last_visible_x)
16528 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16529
16530 /* Compute the total height of the lines. */
16531 compute_line_metrics (&it);
16532 }
16533
16534
16535 \f
16536 /***********************************************************************
16537 Mode Line
16538 ***********************************************************************/
16539
16540 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16541 FORCE is non-zero, redisplay mode lines unconditionally.
16542 Otherwise, redisplay only mode lines that are garbaged. Value is
16543 the number of windows whose mode lines were redisplayed. */
16544
16545 static int
16546 redisplay_mode_lines (window, force)
16547 Lisp_Object window;
16548 int force;
16549 {
16550 int nwindows = 0;
16551
16552 while (!NILP (window))
16553 {
16554 struct window *w = XWINDOW (window);
16555
16556 if (WINDOWP (w->hchild))
16557 nwindows += redisplay_mode_lines (w->hchild, force);
16558 else if (WINDOWP (w->vchild))
16559 nwindows += redisplay_mode_lines (w->vchild, force);
16560 else if (force
16561 || FRAME_GARBAGED_P (XFRAME (w->frame))
16562 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16563 {
16564 struct text_pos lpoint;
16565 struct buffer *old = current_buffer;
16566
16567 /* Set the window's buffer for the mode line display. */
16568 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16569 set_buffer_internal_1 (XBUFFER (w->buffer));
16570
16571 /* Point refers normally to the selected window. For any
16572 other window, set up appropriate value. */
16573 if (!EQ (window, selected_window))
16574 {
16575 struct text_pos pt;
16576
16577 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16578 if (CHARPOS (pt) < BEGV)
16579 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16580 else if (CHARPOS (pt) > (ZV - 1))
16581 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16582 else
16583 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16584 }
16585
16586 /* Display mode lines. */
16587 clear_glyph_matrix (w->desired_matrix);
16588 if (display_mode_lines (w))
16589 {
16590 ++nwindows;
16591 w->must_be_updated_p = 1;
16592 }
16593
16594 /* Restore old settings. */
16595 set_buffer_internal_1 (old);
16596 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16597 }
16598
16599 window = w->next;
16600 }
16601
16602 return nwindows;
16603 }
16604
16605
16606 /* Display the mode and/or top line of window W. Value is the number
16607 of mode lines displayed. */
16608
16609 static int
16610 display_mode_lines (w)
16611 struct window *w;
16612 {
16613 Lisp_Object old_selected_window, old_selected_frame;
16614 int n = 0;
16615
16616 old_selected_frame = selected_frame;
16617 selected_frame = w->frame;
16618 old_selected_window = selected_window;
16619 XSETWINDOW (selected_window, w);
16620
16621 /* These will be set while the mode line specs are processed. */
16622 line_number_displayed = 0;
16623 w->column_number_displayed = Qnil;
16624
16625 if (WINDOW_WANTS_MODELINE_P (w))
16626 {
16627 struct window *sel_w = XWINDOW (old_selected_window);
16628
16629 /* Select mode line face based on the real selected window. */
16630 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16631 current_buffer->mode_line_format);
16632 ++n;
16633 }
16634
16635 if (WINDOW_WANTS_HEADER_LINE_P (w))
16636 {
16637 display_mode_line (w, HEADER_LINE_FACE_ID,
16638 current_buffer->header_line_format);
16639 ++n;
16640 }
16641
16642 selected_frame = old_selected_frame;
16643 selected_window = old_selected_window;
16644 return n;
16645 }
16646
16647
16648 /* Display mode or top line of window W. FACE_ID specifies which line
16649 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16650 FORMAT is the mode line format to display. Value is the pixel
16651 height of the mode line displayed. */
16652
16653 static int
16654 display_mode_line (w, face_id, format)
16655 struct window *w;
16656 enum face_id face_id;
16657 Lisp_Object format;
16658 {
16659 struct it it;
16660 struct face *face;
16661 int count = SPECPDL_INDEX ();
16662
16663 init_iterator (&it, w, -1, -1, NULL, face_id);
16664 /* Don't extend on a previously drawn mode-line.
16665 This may happen if called from pos_visible_p. */
16666 it.glyph_row->enabled_p = 0;
16667 prepare_desired_row (it.glyph_row);
16668
16669 it.glyph_row->mode_line_p = 1;
16670
16671 if (! mode_line_inverse_video)
16672 /* Force the mode-line to be displayed in the default face. */
16673 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16674
16675 record_unwind_protect (unwind_format_mode_line,
16676 format_mode_line_unwind_data (NULL, 0));
16677
16678 mode_line_target = MODE_LINE_DISPLAY;
16679
16680 /* Temporarily make frame's keyboard the current kboard so that
16681 kboard-local variables in the mode_line_format will get the right
16682 values. */
16683 push_frame_kboard (it.f);
16684 record_unwind_save_match_data ();
16685 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16686 pop_frame_kboard ();
16687
16688 unbind_to (count, Qnil);
16689
16690 /* Fill up with spaces. */
16691 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16692
16693 compute_line_metrics (&it);
16694 it.glyph_row->full_width_p = 1;
16695 it.glyph_row->continued_p = 0;
16696 it.glyph_row->truncated_on_left_p = 0;
16697 it.glyph_row->truncated_on_right_p = 0;
16698
16699 /* Make a 3D mode-line have a shadow at its right end. */
16700 face = FACE_FROM_ID (it.f, face_id);
16701 extend_face_to_end_of_line (&it);
16702 if (face->box != FACE_NO_BOX)
16703 {
16704 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16705 + it.glyph_row->used[TEXT_AREA] - 1);
16706 last->right_box_line_p = 1;
16707 }
16708
16709 return it.glyph_row->height;
16710 }
16711
16712 /* Move element ELT in LIST to the front of LIST.
16713 Return the updated list. */
16714
16715 static Lisp_Object
16716 move_elt_to_front (elt, list)
16717 Lisp_Object elt, list;
16718 {
16719 register Lisp_Object tail, prev;
16720 register Lisp_Object tem;
16721
16722 tail = list;
16723 prev = Qnil;
16724 while (CONSP (tail))
16725 {
16726 tem = XCAR (tail);
16727
16728 if (EQ (elt, tem))
16729 {
16730 /* Splice out the link TAIL. */
16731 if (NILP (prev))
16732 list = XCDR (tail);
16733 else
16734 Fsetcdr (prev, XCDR (tail));
16735
16736 /* Now make it the first. */
16737 Fsetcdr (tail, list);
16738 return tail;
16739 }
16740 else
16741 prev = tail;
16742 tail = XCDR (tail);
16743 QUIT;
16744 }
16745
16746 /* Not found--return unchanged LIST. */
16747 return list;
16748 }
16749
16750 /* Contribute ELT to the mode line for window IT->w. How it
16751 translates into text depends on its data type.
16752
16753 IT describes the display environment in which we display, as usual.
16754
16755 DEPTH is the depth in recursion. It is used to prevent
16756 infinite recursion here.
16757
16758 FIELD_WIDTH is the number of characters the display of ELT should
16759 occupy in the mode line, and PRECISION is the maximum number of
16760 characters to display from ELT's representation. See
16761 display_string for details.
16762
16763 Returns the hpos of the end of the text generated by ELT.
16764
16765 PROPS is a property list to add to any string we encounter.
16766
16767 If RISKY is nonzero, remove (disregard) any properties in any string
16768 we encounter, and ignore :eval and :propertize.
16769
16770 The global variable `mode_line_target' determines whether the
16771 output is passed to `store_mode_line_noprop',
16772 `store_mode_line_string', or `display_string'. */
16773
16774 static int
16775 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16776 struct it *it;
16777 int depth;
16778 int field_width, precision;
16779 Lisp_Object elt, props;
16780 int risky;
16781 {
16782 int n = 0, field, prec;
16783 int literal = 0;
16784
16785 tail_recurse:
16786 if (depth > 100)
16787 elt = build_string ("*too-deep*");
16788
16789 depth++;
16790
16791 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16792 {
16793 case Lisp_String:
16794 {
16795 /* A string: output it and check for %-constructs within it. */
16796 unsigned char c;
16797 int offset = 0;
16798
16799 if (SCHARS (elt) > 0
16800 && (!NILP (props) || risky))
16801 {
16802 Lisp_Object oprops, aelt;
16803 oprops = Ftext_properties_at (make_number (0), elt);
16804
16805 /* If the starting string's properties are not what
16806 we want, translate the string. Also, if the string
16807 is risky, do that anyway. */
16808
16809 if (NILP (Fequal (props, oprops)) || risky)
16810 {
16811 /* If the starting string has properties,
16812 merge the specified ones onto the existing ones. */
16813 if (! NILP (oprops) && !risky)
16814 {
16815 Lisp_Object tem;
16816
16817 oprops = Fcopy_sequence (oprops);
16818 tem = props;
16819 while (CONSP (tem))
16820 {
16821 oprops = Fplist_put (oprops, XCAR (tem),
16822 XCAR (XCDR (tem)));
16823 tem = XCDR (XCDR (tem));
16824 }
16825 props = oprops;
16826 }
16827
16828 aelt = Fassoc (elt, mode_line_proptrans_alist);
16829 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16830 {
16831 /* AELT is what we want. Move it to the front
16832 without consing. */
16833 elt = XCAR (aelt);
16834 mode_line_proptrans_alist
16835 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16836 }
16837 else
16838 {
16839 Lisp_Object tem;
16840
16841 /* If AELT has the wrong props, it is useless.
16842 so get rid of it. */
16843 if (! NILP (aelt))
16844 mode_line_proptrans_alist
16845 = Fdelq (aelt, mode_line_proptrans_alist);
16846
16847 elt = Fcopy_sequence (elt);
16848 Fset_text_properties (make_number (0), Flength (elt),
16849 props, elt);
16850 /* Add this item to mode_line_proptrans_alist. */
16851 mode_line_proptrans_alist
16852 = Fcons (Fcons (elt, props),
16853 mode_line_proptrans_alist);
16854 /* Truncate mode_line_proptrans_alist
16855 to at most 50 elements. */
16856 tem = Fnthcdr (make_number (50),
16857 mode_line_proptrans_alist);
16858 if (! NILP (tem))
16859 XSETCDR (tem, Qnil);
16860 }
16861 }
16862 }
16863
16864 offset = 0;
16865
16866 if (literal)
16867 {
16868 prec = precision - n;
16869 switch (mode_line_target)
16870 {
16871 case MODE_LINE_NOPROP:
16872 case MODE_LINE_TITLE:
16873 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16874 break;
16875 case MODE_LINE_STRING:
16876 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16877 break;
16878 case MODE_LINE_DISPLAY:
16879 n += display_string (NULL, elt, Qnil, 0, 0, it,
16880 0, prec, 0, STRING_MULTIBYTE (elt));
16881 break;
16882 }
16883
16884 break;
16885 }
16886
16887 /* Handle the non-literal case. */
16888
16889 while ((precision <= 0 || n < precision)
16890 && SREF (elt, offset) != 0
16891 && (mode_line_target != MODE_LINE_DISPLAY
16892 || it->current_x < it->last_visible_x))
16893 {
16894 int last_offset = offset;
16895
16896 /* Advance to end of string or next format specifier. */
16897 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16898 ;
16899
16900 if (offset - 1 != last_offset)
16901 {
16902 int nchars, nbytes;
16903
16904 /* Output to end of string or up to '%'. Field width
16905 is length of string. Don't output more than
16906 PRECISION allows us. */
16907 offset--;
16908
16909 prec = c_string_width (SDATA (elt) + last_offset,
16910 offset - last_offset, precision - n,
16911 &nchars, &nbytes);
16912
16913 switch (mode_line_target)
16914 {
16915 case MODE_LINE_NOPROP:
16916 case MODE_LINE_TITLE:
16917 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16918 break;
16919 case MODE_LINE_STRING:
16920 {
16921 int bytepos = last_offset;
16922 int charpos = string_byte_to_char (elt, bytepos);
16923 int endpos = (precision <= 0
16924 ? string_byte_to_char (elt, offset)
16925 : charpos + nchars);
16926
16927 n += store_mode_line_string (NULL,
16928 Fsubstring (elt, make_number (charpos),
16929 make_number (endpos)),
16930 0, 0, 0, Qnil);
16931 }
16932 break;
16933 case MODE_LINE_DISPLAY:
16934 {
16935 int bytepos = last_offset;
16936 int charpos = string_byte_to_char (elt, bytepos);
16937
16938 if (precision <= 0)
16939 nchars = string_byte_to_char (elt, offset) - charpos;
16940 n += display_string (NULL, elt, Qnil, 0, charpos,
16941 it, 0, nchars, 0,
16942 STRING_MULTIBYTE (elt));
16943 }
16944 break;
16945 }
16946 }
16947 else /* c == '%' */
16948 {
16949 int percent_position = offset;
16950
16951 /* Get the specified minimum width. Zero means
16952 don't pad. */
16953 field = 0;
16954 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16955 field = field * 10 + c - '0';
16956
16957 /* Don't pad beyond the total padding allowed. */
16958 if (field_width - n > 0 && field > field_width - n)
16959 field = field_width - n;
16960
16961 /* Note that either PRECISION <= 0 or N < PRECISION. */
16962 prec = precision - n;
16963
16964 if (c == 'M')
16965 n += display_mode_element (it, depth, field, prec,
16966 Vglobal_mode_string, props,
16967 risky);
16968 else if (c != 0)
16969 {
16970 int multibyte;
16971 int bytepos, charpos;
16972 unsigned char *spec;
16973
16974 bytepos = percent_position;
16975 charpos = (STRING_MULTIBYTE (elt)
16976 ? string_byte_to_char (elt, bytepos)
16977 : bytepos);
16978
16979 spec
16980 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16981
16982 switch (mode_line_target)
16983 {
16984 case MODE_LINE_NOPROP:
16985 case MODE_LINE_TITLE:
16986 n += store_mode_line_noprop (spec, field, prec);
16987 break;
16988 case MODE_LINE_STRING:
16989 {
16990 int len = strlen (spec);
16991 Lisp_Object tem = make_string (spec, len);
16992 props = Ftext_properties_at (make_number (charpos), elt);
16993 /* Should only keep face property in props */
16994 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16995 }
16996 break;
16997 case MODE_LINE_DISPLAY:
16998 {
16999 int nglyphs_before, nwritten;
17000
17001 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17002 nwritten = display_string (spec, Qnil, elt,
17003 charpos, 0, it,
17004 field, prec, 0,
17005 multibyte);
17006
17007 /* Assign to the glyphs written above the
17008 string where the `%x' came from, position
17009 of the `%'. */
17010 if (nwritten > 0)
17011 {
17012 struct glyph *glyph
17013 = (it->glyph_row->glyphs[TEXT_AREA]
17014 + nglyphs_before);
17015 int i;
17016
17017 for (i = 0; i < nwritten; ++i)
17018 {
17019 glyph[i].object = elt;
17020 glyph[i].charpos = charpos;
17021 }
17022
17023 n += nwritten;
17024 }
17025 }
17026 break;
17027 }
17028 }
17029 else /* c == 0 */
17030 break;
17031 }
17032 }
17033 }
17034 break;
17035
17036 case Lisp_Symbol:
17037 /* A symbol: process the value of the symbol recursively
17038 as if it appeared here directly. Avoid error if symbol void.
17039 Special case: if value of symbol is a string, output the string
17040 literally. */
17041 {
17042 register Lisp_Object tem;
17043
17044 /* If the variable is not marked as risky to set
17045 then its contents are risky to use. */
17046 if (NILP (Fget (elt, Qrisky_local_variable)))
17047 risky = 1;
17048
17049 tem = Fboundp (elt);
17050 if (!NILP (tem))
17051 {
17052 tem = Fsymbol_value (elt);
17053 /* If value is a string, output that string literally:
17054 don't check for % within it. */
17055 if (STRINGP (tem))
17056 literal = 1;
17057
17058 if (!EQ (tem, elt))
17059 {
17060 /* Give up right away for nil or t. */
17061 elt = tem;
17062 goto tail_recurse;
17063 }
17064 }
17065 }
17066 break;
17067
17068 case Lisp_Cons:
17069 {
17070 register Lisp_Object car, tem;
17071
17072 /* A cons cell: five distinct cases.
17073 If first element is :eval or :propertize, do something special.
17074 If first element is a string or a cons, process all the elements
17075 and effectively concatenate them.
17076 If first element is a negative number, truncate displaying cdr to
17077 at most that many characters. If positive, pad (with spaces)
17078 to at least that many characters.
17079 If first element is a symbol, process the cadr or caddr recursively
17080 according to whether the symbol's value is non-nil or nil. */
17081 car = XCAR (elt);
17082 if (EQ (car, QCeval))
17083 {
17084 /* An element of the form (:eval FORM) means evaluate FORM
17085 and use the result as mode line elements. */
17086
17087 if (risky)
17088 break;
17089
17090 if (CONSP (XCDR (elt)))
17091 {
17092 Lisp_Object spec;
17093 spec = safe_eval (XCAR (XCDR (elt)));
17094 n += display_mode_element (it, depth, field_width - n,
17095 precision - n, spec, props,
17096 risky);
17097 }
17098 }
17099 else if (EQ (car, QCpropertize))
17100 {
17101 /* An element of the form (:propertize ELT PROPS...)
17102 means display ELT but applying properties PROPS. */
17103
17104 if (risky)
17105 break;
17106
17107 if (CONSP (XCDR (elt)))
17108 n += display_mode_element (it, depth, field_width - n,
17109 precision - n, XCAR (XCDR (elt)),
17110 XCDR (XCDR (elt)), risky);
17111 }
17112 else if (SYMBOLP (car))
17113 {
17114 tem = Fboundp (car);
17115 elt = XCDR (elt);
17116 if (!CONSP (elt))
17117 goto invalid;
17118 /* elt is now the cdr, and we know it is a cons cell.
17119 Use its car if CAR has a non-nil value. */
17120 if (!NILP (tem))
17121 {
17122 tem = Fsymbol_value (car);
17123 if (!NILP (tem))
17124 {
17125 elt = XCAR (elt);
17126 goto tail_recurse;
17127 }
17128 }
17129 /* Symbol's value is nil (or symbol is unbound)
17130 Get the cddr of the original list
17131 and if possible find the caddr and use that. */
17132 elt = XCDR (elt);
17133 if (NILP (elt))
17134 break;
17135 else if (!CONSP (elt))
17136 goto invalid;
17137 elt = XCAR (elt);
17138 goto tail_recurse;
17139 }
17140 else if (INTEGERP (car))
17141 {
17142 register int lim = XINT (car);
17143 elt = XCDR (elt);
17144 if (lim < 0)
17145 {
17146 /* Negative int means reduce maximum width. */
17147 if (precision <= 0)
17148 precision = -lim;
17149 else
17150 precision = min (precision, -lim);
17151 }
17152 else if (lim > 0)
17153 {
17154 /* Padding specified. Don't let it be more than
17155 current maximum. */
17156 if (precision > 0)
17157 lim = min (precision, lim);
17158
17159 /* If that's more padding than already wanted, queue it.
17160 But don't reduce padding already specified even if
17161 that is beyond the current truncation point. */
17162 field_width = max (lim, field_width);
17163 }
17164 goto tail_recurse;
17165 }
17166 else if (STRINGP (car) || CONSP (car))
17167 {
17168 register int limit = 50;
17169 /* Limit is to protect against circular lists. */
17170 while (CONSP (elt)
17171 && --limit > 0
17172 && (precision <= 0 || n < precision))
17173 {
17174 n += display_mode_element (it, depth,
17175 /* Do padding only after the last
17176 element in the list. */
17177 (! CONSP (XCDR (elt))
17178 ? field_width - n
17179 : 0),
17180 precision - n, XCAR (elt),
17181 props, risky);
17182 elt = XCDR (elt);
17183 }
17184 }
17185 }
17186 break;
17187
17188 default:
17189 invalid:
17190 elt = build_string ("*invalid*");
17191 goto tail_recurse;
17192 }
17193
17194 /* Pad to FIELD_WIDTH. */
17195 if (field_width > 0 && n < field_width)
17196 {
17197 switch (mode_line_target)
17198 {
17199 case MODE_LINE_NOPROP:
17200 case MODE_LINE_TITLE:
17201 n += store_mode_line_noprop ("", field_width - n, 0);
17202 break;
17203 case MODE_LINE_STRING:
17204 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17205 break;
17206 case MODE_LINE_DISPLAY:
17207 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17208 0, 0, 0);
17209 break;
17210 }
17211 }
17212
17213 return n;
17214 }
17215
17216 /* Store a mode-line string element in mode_line_string_list.
17217
17218 If STRING is non-null, display that C string. Otherwise, the Lisp
17219 string LISP_STRING is displayed.
17220
17221 FIELD_WIDTH is the minimum number of output glyphs to produce.
17222 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17223 with spaces. FIELD_WIDTH <= 0 means don't pad.
17224
17225 PRECISION is the maximum number of characters to output from
17226 STRING. PRECISION <= 0 means don't truncate the string.
17227
17228 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17229 properties to the string.
17230
17231 PROPS are the properties to add to the string.
17232 The mode_line_string_face face property is always added to the string.
17233 */
17234
17235 static int
17236 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17237 char *string;
17238 Lisp_Object lisp_string;
17239 int copy_string;
17240 int field_width;
17241 int precision;
17242 Lisp_Object props;
17243 {
17244 int len;
17245 int n = 0;
17246
17247 if (string != NULL)
17248 {
17249 len = strlen (string);
17250 if (precision > 0 && len > precision)
17251 len = precision;
17252 lisp_string = make_string (string, len);
17253 if (NILP (props))
17254 props = mode_line_string_face_prop;
17255 else if (!NILP (mode_line_string_face))
17256 {
17257 Lisp_Object face = Fplist_get (props, Qface);
17258 props = Fcopy_sequence (props);
17259 if (NILP (face))
17260 face = mode_line_string_face;
17261 else
17262 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17263 props = Fplist_put (props, Qface, face);
17264 }
17265 Fadd_text_properties (make_number (0), make_number (len),
17266 props, lisp_string);
17267 }
17268 else
17269 {
17270 len = XFASTINT (Flength (lisp_string));
17271 if (precision > 0 && len > precision)
17272 {
17273 len = precision;
17274 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17275 precision = -1;
17276 }
17277 if (!NILP (mode_line_string_face))
17278 {
17279 Lisp_Object face;
17280 if (NILP (props))
17281 props = Ftext_properties_at (make_number (0), lisp_string);
17282 face = Fplist_get (props, Qface);
17283 if (NILP (face))
17284 face = mode_line_string_face;
17285 else
17286 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17287 props = Fcons (Qface, Fcons (face, Qnil));
17288 if (copy_string)
17289 lisp_string = Fcopy_sequence (lisp_string);
17290 }
17291 if (!NILP (props))
17292 Fadd_text_properties (make_number (0), make_number (len),
17293 props, lisp_string);
17294 }
17295
17296 if (len > 0)
17297 {
17298 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17299 n += len;
17300 }
17301
17302 if (field_width > len)
17303 {
17304 field_width -= len;
17305 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17306 if (!NILP (props))
17307 Fadd_text_properties (make_number (0), make_number (field_width),
17308 props, lisp_string);
17309 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17310 n += field_width;
17311 }
17312
17313 return n;
17314 }
17315
17316
17317 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17318 1, 4, 0,
17319 doc: /* Format a string out of a mode line format specification.
17320 First arg FORMAT specifies the mode line format (see `mode-line-format'
17321 for details) to use.
17322
17323 Optional second arg FACE specifies the face property to put
17324 on all characters for which no face is specified.
17325 The value t means whatever face the window's mode line currently uses
17326 \(either `mode-line' or `mode-line-inactive', depending).
17327 A value of nil means the default is no face property.
17328 If FACE is an integer, the value string has no text properties.
17329
17330 Optional third and fourth args WINDOW and BUFFER specify the window
17331 and buffer to use as the context for the formatting (defaults
17332 are the selected window and the window's buffer). */)
17333 (format, face, window, buffer)
17334 Lisp_Object format, face, window, buffer;
17335 {
17336 struct it it;
17337 int len;
17338 struct window *w;
17339 struct buffer *old_buffer = NULL;
17340 int face_id = -1;
17341 int no_props = INTEGERP (face);
17342 int count = SPECPDL_INDEX ();
17343 Lisp_Object str;
17344 int string_start = 0;
17345
17346 if (NILP (window))
17347 window = selected_window;
17348 CHECK_WINDOW (window);
17349 w = XWINDOW (window);
17350
17351 if (NILP (buffer))
17352 buffer = w->buffer;
17353 CHECK_BUFFER (buffer);
17354
17355 if (NILP (format))
17356 return build_string ("");
17357
17358 if (no_props)
17359 face = Qnil;
17360
17361 if (!NILP (face))
17362 {
17363 if (EQ (face, Qt))
17364 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17365 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17366 }
17367
17368 if (face_id < 0)
17369 face_id = DEFAULT_FACE_ID;
17370
17371 if (XBUFFER (buffer) != current_buffer)
17372 old_buffer = current_buffer;
17373
17374 /* Save things including mode_line_proptrans_alist,
17375 and set that to nil so that we don't alter the outer value. */
17376 record_unwind_protect (unwind_format_mode_line,
17377 format_mode_line_unwind_data (old_buffer, 1));
17378 mode_line_proptrans_alist = Qnil;
17379
17380 if (old_buffer)
17381 set_buffer_internal_1 (XBUFFER (buffer));
17382
17383 init_iterator (&it, w, -1, -1, NULL, face_id);
17384
17385 if (no_props)
17386 {
17387 mode_line_target = MODE_LINE_NOPROP;
17388 mode_line_string_face_prop = Qnil;
17389 mode_line_string_list = Qnil;
17390 string_start = MODE_LINE_NOPROP_LEN (0);
17391 }
17392 else
17393 {
17394 mode_line_target = MODE_LINE_STRING;
17395 mode_line_string_list = Qnil;
17396 mode_line_string_face = face;
17397 mode_line_string_face_prop
17398 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17399 }
17400
17401 push_frame_kboard (it.f);
17402 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17403 pop_frame_kboard ();
17404
17405 if (no_props)
17406 {
17407 len = MODE_LINE_NOPROP_LEN (string_start);
17408 str = make_string (mode_line_noprop_buf + string_start, len);
17409 }
17410 else
17411 {
17412 mode_line_string_list = Fnreverse (mode_line_string_list);
17413 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17414 make_string ("", 0));
17415 }
17416
17417 unbind_to (count, Qnil);
17418 return str;
17419 }
17420
17421 /* Write a null-terminated, right justified decimal representation of
17422 the positive integer D to BUF using a minimal field width WIDTH. */
17423
17424 static void
17425 pint2str (buf, width, d)
17426 register char *buf;
17427 register int width;
17428 register int d;
17429 {
17430 register char *p = buf;
17431
17432 if (d <= 0)
17433 *p++ = '0';
17434 else
17435 {
17436 while (d > 0)
17437 {
17438 *p++ = d % 10 + '0';
17439 d /= 10;
17440 }
17441 }
17442
17443 for (width -= (int) (p - buf); width > 0; --width)
17444 *p++ = ' ';
17445 *p-- = '\0';
17446 while (p > buf)
17447 {
17448 d = *buf;
17449 *buf++ = *p;
17450 *p-- = d;
17451 }
17452 }
17453
17454 /* Write a null-terminated, right justified decimal and "human
17455 readable" representation of the nonnegative integer D to BUF using
17456 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17457
17458 static const char power_letter[] =
17459 {
17460 0, /* not used */
17461 'k', /* kilo */
17462 'M', /* mega */
17463 'G', /* giga */
17464 'T', /* tera */
17465 'P', /* peta */
17466 'E', /* exa */
17467 'Z', /* zetta */
17468 'Y' /* yotta */
17469 };
17470
17471 static void
17472 pint2hrstr (buf, width, d)
17473 char *buf;
17474 int width;
17475 int d;
17476 {
17477 /* We aim to represent the nonnegative integer D as
17478 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17479 int quotient = d;
17480 int remainder = 0;
17481 /* -1 means: do not use TENTHS. */
17482 int tenths = -1;
17483 int exponent = 0;
17484
17485 /* Length of QUOTIENT.TENTHS as a string. */
17486 int length;
17487
17488 char * psuffix;
17489 char * p;
17490
17491 if (1000 <= quotient)
17492 {
17493 /* Scale to the appropriate EXPONENT. */
17494 do
17495 {
17496 remainder = quotient % 1000;
17497 quotient /= 1000;
17498 exponent++;
17499 }
17500 while (1000 <= quotient);
17501
17502 /* Round to nearest and decide whether to use TENTHS or not. */
17503 if (quotient <= 9)
17504 {
17505 tenths = remainder / 100;
17506 if (50 <= remainder % 100)
17507 {
17508 if (tenths < 9)
17509 tenths++;
17510 else
17511 {
17512 quotient++;
17513 if (quotient == 10)
17514 tenths = -1;
17515 else
17516 tenths = 0;
17517 }
17518 }
17519 }
17520 else
17521 if (500 <= remainder)
17522 {
17523 if (quotient < 999)
17524 quotient++;
17525 else
17526 {
17527 quotient = 1;
17528 exponent++;
17529 tenths = 0;
17530 }
17531 }
17532 }
17533
17534 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17535 if (tenths == -1 && quotient <= 99)
17536 if (quotient <= 9)
17537 length = 1;
17538 else
17539 length = 2;
17540 else
17541 length = 3;
17542 p = psuffix = buf + max (width, length);
17543
17544 /* Print EXPONENT. */
17545 if (exponent)
17546 *psuffix++ = power_letter[exponent];
17547 *psuffix = '\0';
17548
17549 /* Print TENTHS. */
17550 if (tenths >= 0)
17551 {
17552 *--p = '0' + tenths;
17553 *--p = '.';
17554 }
17555
17556 /* Print QUOTIENT. */
17557 do
17558 {
17559 int digit = quotient % 10;
17560 *--p = '0' + digit;
17561 }
17562 while ((quotient /= 10) != 0);
17563
17564 /* Print leading spaces. */
17565 while (buf < p)
17566 *--p = ' ';
17567 }
17568
17569 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17570 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17571 type of CODING_SYSTEM. Return updated pointer into BUF. */
17572
17573 static unsigned char invalid_eol_type[] = "(*invalid*)";
17574
17575 static char *
17576 decode_mode_spec_coding (coding_system, buf, eol_flag)
17577 Lisp_Object coding_system;
17578 register char *buf;
17579 int eol_flag;
17580 {
17581 Lisp_Object val;
17582 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17583 const unsigned char *eol_str;
17584 int eol_str_len;
17585 /* The EOL conversion we are using. */
17586 Lisp_Object eoltype;
17587
17588 val = Fget (coding_system, Qcoding_system);
17589 eoltype = Qnil;
17590
17591 if (!VECTORP (val)) /* Not yet decided. */
17592 {
17593 if (multibyte)
17594 *buf++ = '-';
17595 if (eol_flag)
17596 eoltype = eol_mnemonic_undecided;
17597 /* Don't mention EOL conversion if it isn't decided. */
17598 }
17599 else
17600 {
17601 Lisp_Object eolvalue;
17602
17603 eolvalue = Fget (coding_system, Qeol_type);
17604
17605 if (multibyte)
17606 *buf++ = XFASTINT (AREF (val, 1));
17607
17608 if (eol_flag)
17609 {
17610 /* The EOL conversion that is normal on this system. */
17611
17612 if (NILP (eolvalue)) /* Not yet decided. */
17613 eoltype = eol_mnemonic_undecided;
17614 else if (VECTORP (eolvalue)) /* Not yet decided. */
17615 eoltype = eol_mnemonic_undecided;
17616 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17617 eoltype = (XFASTINT (eolvalue) == 0
17618 ? eol_mnemonic_unix
17619 : (XFASTINT (eolvalue) == 1
17620 ? eol_mnemonic_dos : eol_mnemonic_mac));
17621 }
17622 }
17623
17624 if (eol_flag)
17625 {
17626 /* Mention the EOL conversion if it is not the usual one. */
17627 if (STRINGP (eoltype))
17628 {
17629 eol_str = SDATA (eoltype);
17630 eol_str_len = SBYTES (eoltype);
17631 }
17632 else if (INTEGERP (eoltype)
17633 && CHAR_VALID_P (XINT (eoltype), 0))
17634 {
17635 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17636 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17637 eol_str = tmp;
17638 }
17639 else
17640 {
17641 eol_str = invalid_eol_type;
17642 eol_str_len = sizeof (invalid_eol_type) - 1;
17643 }
17644 bcopy (eol_str, buf, eol_str_len);
17645 buf += eol_str_len;
17646 }
17647
17648 return buf;
17649 }
17650
17651 /* Return a string for the output of a mode line %-spec for window W,
17652 generated by character C. PRECISION >= 0 means don't return a
17653 string longer than that value. FIELD_WIDTH > 0 means pad the
17654 string returned with spaces to that value. Return 1 in *MULTIBYTE
17655 if the result is multibyte text.
17656
17657 Note we operate on the current buffer for most purposes,
17658 the exception being w->base_line_pos. */
17659
17660 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17661
17662 static char *
17663 decode_mode_spec (w, c, field_width, precision, multibyte)
17664 struct window *w;
17665 register int c;
17666 int field_width, precision;
17667 int *multibyte;
17668 {
17669 Lisp_Object obj;
17670 struct frame *f = XFRAME (WINDOW_FRAME (w));
17671 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17672 struct buffer *b = current_buffer;
17673
17674 obj = Qnil;
17675 *multibyte = 0;
17676
17677 switch (c)
17678 {
17679 case '*':
17680 if (!NILP (b->read_only))
17681 return "%";
17682 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17683 return "*";
17684 return "-";
17685
17686 case '+':
17687 /* This differs from %* only for a modified read-only buffer. */
17688 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17689 return "*";
17690 if (!NILP (b->read_only))
17691 return "%";
17692 return "-";
17693
17694 case '&':
17695 /* This differs from %* in ignoring read-only-ness. */
17696 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17697 return "*";
17698 return "-";
17699
17700 case '%':
17701 return "%";
17702
17703 case '[':
17704 {
17705 int i;
17706 char *p;
17707
17708 if (command_loop_level > 5)
17709 return "[[[... ";
17710 p = decode_mode_spec_buf;
17711 for (i = 0; i < command_loop_level; i++)
17712 *p++ = '[';
17713 *p = 0;
17714 return decode_mode_spec_buf;
17715 }
17716
17717 case ']':
17718 {
17719 int i;
17720 char *p;
17721
17722 if (command_loop_level > 5)
17723 return " ...]]]";
17724 p = decode_mode_spec_buf;
17725 for (i = 0; i < command_loop_level; i++)
17726 *p++ = ']';
17727 *p = 0;
17728 return decode_mode_spec_buf;
17729 }
17730
17731 case '-':
17732 {
17733 register int i;
17734
17735 /* Let lots_of_dashes be a string of infinite length. */
17736 if (mode_line_target == MODE_LINE_NOPROP ||
17737 mode_line_target == MODE_LINE_STRING)
17738 return "--";
17739 if (field_width <= 0
17740 || field_width > sizeof (lots_of_dashes))
17741 {
17742 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17743 decode_mode_spec_buf[i] = '-';
17744 decode_mode_spec_buf[i] = '\0';
17745 return decode_mode_spec_buf;
17746 }
17747 else
17748 return lots_of_dashes;
17749 }
17750
17751 case 'b':
17752 obj = b->name;
17753 break;
17754
17755 case 'c':
17756 /* %c and %l are ignored in `frame-title-format'.
17757 (In redisplay_internal, the frame title is drawn _before_ the
17758 windows are updated, so the stuff which depends on actual
17759 window contents (such as %l) may fail to render properly, or
17760 even crash emacs.) */
17761 if (mode_line_target == MODE_LINE_TITLE)
17762 return "";
17763 else
17764 {
17765 int col = (int) current_column (); /* iftc */
17766 w->column_number_displayed = make_number (col);
17767 pint2str (decode_mode_spec_buf, field_width, col);
17768 return decode_mode_spec_buf;
17769 }
17770
17771 case 'e':
17772 #ifndef SYSTEM_MALLOC
17773 {
17774 if (NILP (Vmemory_full))
17775 return "";
17776 else
17777 return "!MEM FULL! ";
17778 }
17779 #else
17780 return "";
17781 #endif
17782
17783 case 'F':
17784 /* %F displays the frame name. */
17785 if (!NILP (f->title))
17786 return (char *) SDATA (f->title);
17787 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17788 return (char *) SDATA (f->name);
17789 return "Emacs";
17790
17791 case 'f':
17792 obj = b->filename;
17793 break;
17794
17795 case 'i':
17796 {
17797 int size = ZV - BEGV;
17798 pint2str (decode_mode_spec_buf, field_width, size);
17799 return decode_mode_spec_buf;
17800 }
17801
17802 case 'I':
17803 {
17804 int size = ZV - BEGV;
17805 pint2hrstr (decode_mode_spec_buf, field_width, size);
17806 return decode_mode_spec_buf;
17807 }
17808
17809 case 'l':
17810 {
17811 int startpos, startpos_byte, line, linepos, linepos_byte;
17812 int topline, nlines, junk, height;
17813
17814 /* %c and %l are ignored in `frame-title-format'. */
17815 if (mode_line_target == MODE_LINE_TITLE)
17816 return "";
17817
17818 startpos = XMARKER (w->start)->charpos;
17819 startpos_byte = marker_byte_position (w->start);
17820 height = WINDOW_TOTAL_LINES (w);
17821
17822 /* If we decided that this buffer isn't suitable for line numbers,
17823 don't forget that too fast. */
17824 if (EQ (w->base_line_pos, w->buffer))
17825 goto no_value;
17826 /* But do forget it, if the window shows a different buffer now. */
17827 else if (BUFFERP (w->base_line_pos))
17828 w->base_line_pos = Qnil;
17829
17830 /* If the buffer is very big, don't waste time. */
17831 if (INTEGERP (Vline_number_display_limit)
17832 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17833 {
17834 w->base_line_pos = Qnil;
17835 w->base_line_number = Qnil;
17836 goto no_value;
17837 }
17838
17839 if (!NILP (w->base_line_number)
17840 && !NILP (w->base_line_pos)
17841 && XFASTINT (w->base_line_pos) <= startpos)
17842 {
17843 line = XFASTINT (w->base_line_number);
17844 linepos = XFASTINT (w->base_line_pos);
17845 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17846 }
17847 else
17848 {
17849 line = 1;
17850 linepos = BUF_BEGV (b);
17851 linepos_byte = BUF_BEGV_BYTE (b);
17852 }
17853
17854 /* Count lines from base line to window start position. */
17855 nlines = display_count_lines (linepos, linepos_byte,
17856 startpos_byte,
17857 startpos, &junk);
17858
17859 topline = nlines + line;
17860
17861 /* Determine a new base line, if the old one is too close
17862 or too far away, or if we did not have one.
17863 "Too close" means it's plausible a scroll-down would
17864 go back past it. */
17865 if (startpos == BUF_BEGV (b))
17866 {
17867 w->base_line_number = make_number (topline);
17868 w->base_line_pos = make_number (BUF_BEGV (b));
17869 }
17870 else if (nlines < height + 25 || nlines > height * 3 + 50
17871 || linepos == BUF_BEGV (b))
17872 {
17873 int limit = BUF_BEGV (b);
17874 int limit_byte = BUF_BEGV_BYTE (b);
17875 int position;
17876 int distance = (height * 2 + 30) * line_number_display_limit_width;
17877
17878 if (startpos - distance > limit)
17879 {
17880 limit = startpos - distance;
17881 limit_byte = CHAR_TO_BYTE (limit);
17882 }
17883
17884 nlines = display_count_lines (startpos, startpos_byte,
17885 limit_byte,
17886 - (height * 2 + 30),
17887 &position);
17888 /* If we couldn't find the lines we wanted within
17889 line_number_display_limit_width chars per line,
17890 give up on line numbers for this window. */
17891 if (position == limit_byte && limit == startpos - distance)
17892 {
17893 w->base_line_pos = w->buffer;
17894 w->base_line_number = Qnil;
17895 goto no_value;
17896 }
17897
17898 w->base_line_number = make_number (topline - nlines);
17899 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17900 }
17901
17902 /* Now count lines from the start pos to point. */
17903 nlines = display_count_lines (startpos, startpos_byte,
17904 PT_BYTE, PT, &junk);
17905
17906 /* Record that we did display the line number. */
17907 line_number_displayed = 1;
17908
17909 /* Make the string to show. */
17910 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17911 return decode_mode_spec_buf;
17912 no_value:
17913 {
17914 char* p = decode_mode_spec_buf;
17915 int pad = field_width - 2;
17916 while (pad-- > 0)
17917 *p++ = ' ';
17918 *p++ = '?';
17919 *p++ = '?';
17920 *p = '\0';
17921 return decode_mode_spec_buf;
17922 }
17923 }
17924 break;
17925
17926 case 'm':
17927 obj = b->mode_name;
17928 break;
17929
17930 case 'n':
17931 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17932 return " Narrow";
17933 break;
17934
17935 case 'p':
17936 {
17937 int pos = marker_position (w->start);
17938 int total = BUF_ZV (b) - BUF_BEGV (b);
17939
17940 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17941 {
17942 if (pos <= BUF_BEGV (b))
17943 return "All";
17944 else
17945 return "Bottom";
17946 }
17947 else if (pos <= BUF_BEGV (b))
17948 return "Top";
17949 else
17950 {
17951 if (total > 1000000)
17952 /* Do it differently for a large value, to avoid overflow. */
17953 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17954 else
17955 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17956 /* We can't normally display a 3-digit number,
17957 so get us a 2-digit number that is close. */
17958 if (total == 100)
17959 total = 99;
17960 sprintf (decode_mode_spec_buf, "%2d%%", total);
17961 return decode_mode_spec_buf;
17962 }
17963 }
17964
17965 /* Display percentage of size above the bottom of the screen. */
17966 case 'P':
17967 {
17968 int toppos = marker_position (w->start);
17969 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17970 int total = BUF_ZV (b) - BUF_BEGV (b);
17971
17972 if (botpos >= BUF_ZV (b))
17973 {
17974 if (toppos <= BUF_BEGV (b))
17975 return "All";
17976 else
17977 return "Bottom";
17978 }
17979 else
17980 {
17981 if (total > 1000000)
17982 /* Do it differently for a large value, to avoid overflow. */
17983 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17984 else
17985 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17986 /* We can't normally display a 3-digit number,
17987 so get us a 2-digit number that is close. */
17988 if (total == 100)
17989 total = 99;
17990 if (toppos <= BUF_BEGV (b))
17991 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17992 else
17993 sprintf (decode_mode_spec_buf, "%2d%%", total);
17994 return decode_mode_spec_buf;
17995 }
17996 }
17997
17998 case 's':
17999 /* status of process */
18000 obj = Fget_buffer_process (Fcurrent_buffer ());
18001 if (NILP (obj))
18002 return "no process";
18003 #ifdef subprocesses
18004 obj = Fsymbol_name (Fprocess_status (obj));
18005 #endif
18006 break;
18007
18008 case 't': /* indicate TEXT or BINARY */
18009 #ifdef MODE_LINE_BINARY_TEXT
18010 return MODE_LINE_BINARY_TEXT (b);
18011 #else
18012 return "T";
18013 #endif
18014
18015 case 'z':
18016 /* coding-system (not including end-of-line format) */
18017 case 'Z':
18018 /* coding-system (including end-of-line type) */
18019 {
18020 int eol_flag = (c == 'Z');
18021 char *p = decode_mode_spec_buf;
18022
18023 if (! FRAME_WINDOW_P (f))
18024 {
18025 /* No need to mention EOL here--the terminal never needs
18026 to do EOL conversion. */
18027 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18028 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18029 }
18030 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18031 p, eol_flag);
18032
18033 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18034 #ifdef subprocesses
18035 obj = Fget_buffer_process (Fcurrent_buffer ());
18036 if (PROCESSP (obj))
18037 {
18038 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18039 p, eol_flag);
18040 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18041 p, eol_flag);
18042 }
18043 #endif /* subprocesses */
18044 #endif /* 0 */
18045 *p = 0;
18046 return decode_mode_spec_buf;
18047 }
18048 }
18049
18050 if (STRINGP (obj))
18051 {
18052 *multibyte = STRING_MULTIBYTE (obj);
18053 return (char *) SDATA (obj);
18054 }
18055 else
18056 return "";
18057 }
18058
18059
18060 /* Count up to COUNT lines starting from START / START_BYTE.
18061 But don't go beyond LIMIT_BYTE.
18062 Return the number of lines thus found (always nonnegative).
18063
18064 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18065
18066 static int
18067 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18068 int start, start_byte, limit_byte, count;
18069 int *byte_pos_ptr;
18070 {
18071 register unsigned char *cursor;
18072 unsigned char *base;
18073
18074 register int ceiling;
18075 register unsigned char *ceiling_addr;
18076 int orig_count = count;
18077
18078 /* If we are not in selective display mode,
18079 check only for newlines. */
18080 int selective_display = (!NILP (current_buffer->selective_display)
18081 && !INTEGERP (current_buffer->selective_display));
18082
18083 if (count > 0)
18084 {
18085 while (start_byte < limit_byte)
18086 {
18087 ceiling = BUFFER_CEILING_OF (start_byte);
18088 ceiling = min (limit_byte - 1, ceiling);
18089 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18090 base = (cursor = BYTE_POS_ADDR (start_byte));
18091 while (1)
18092 {
18093 if (selective_display)
18094 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18095 ;
18096 else
18097 while (*cursor != '\n' && ++cursor != ceiling_addr)
18098 ;
18099
18100 if (cursor != ceiling_addr)
18101 {
18102 if (--count == 0)
18103 {
18104 start_byte += cursor - base + 1;
18105 *byte_pos_ptr = start_byte;
18106 return orig_count;
18107 }
18108 else
18109 if (++cursor == ceiling_addr)
18110 break;
18111 }
18112 else
18113 break;
18114 }
18115 start_byte += cursor - base;
18116 }
18117 }
18118 else
18119 {
18120 while (start_byte > limit_byte)
18121 {
18122 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18123 ceiling = max (limit_byte, ceiling);
18124 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18125 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18126 while (1)
18127 {
18128 if (selective_display)
18129 while (--cursor != ceiling_addr
18130 && *cursor != '\n' && *cursor != 015)
18131 ;
18132 else
18133 while (--cursor != ceiling_addr && *cursor != '\n')
18134 ;
18135
18136 if (cursor != ceiling_addr)
18137 {
18138 if (++count == 0)
18139 {
18140 start_byte += cursor - base + 1;
18141 *byte_pos_ptr = start_byte;
18142 /* When scanning backwards, we should
18143 not count the newline posterior to which we stop. */
18144 return - orig_count - 1;
18145 }
18146 }
18147 else
18148 break;
18149 }
18150 /* Here we add 1 to compensate for the last decrement
18151 of CURSOR, which took it past the valid range. */
18152 start_byte += cursor - base + 1;
18153 }
18154 }
18155
18156 *byte_pos_ptr = limit_byte;
18157
18158 if (count < 0)
18159 return - orig_count + count;
18160 return orig_count - count;
18161
18162 }
18163
18164
18165 \f
18166 /***********************************************************************
18167 Displaying strings
18168 ***********************************************************************/
18169
18170 /* Display a NUL-terminated string, starting with index START.
18171
18172 If STRING is non-null, display that C string. Otherwise, the Lisp
18173 string LISP_STRING is displayed.
18174
18175 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18176 FACE_STRING. Display STRING or LISP_STRING with the face at
18177 FACE_STRING_POS in FACE_STRING:
18178
18179 Display the string in the environment given by IT, but use the
18180 standard display table, temporarily.
18181
18182 FIELD_WIDTH is the minimum number of output glyphs to produce.
18183 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18184 with spaces. If STRING has more characters, more than FIELD_WIDTH
18185 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18186
18187 PRECISION is the maximum number of characters to output from
18188 STRING. PRECISION < 0 means don't truncate the string.
18189
18190 This is roughly equivalent to printf format specifiers:
18191
18192 FIELD_WIDTH PRECISION PRINTF
18193 ----------------------------------------
18194 -1 -1 %s
18195 -1 10 %.10s
18196 10 -1 %10s
18197 20 10 %20.10s
18198
18199 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18200 display them, and < 0 means obey the current buffer's value of
18201 enable_multibyte_characters.
18202
18203 Value is the number of columns displayed. */
18204
18205 static int
18206 display_string (string, lisp_string, face_string, face_string_pos,
18207 start, it, field_width, precision, max_x, multibyte)
18208 unsigned char *string;
18209 Lisp_Object lisp_string;
18210 Lisp_Object face_string;
18211 int face_string_pos;
18212 int start;
18213 struct it *it;
18214 int field_width, precision, max_x;
18215 int multibyte;
18216 {
18217 int hpos_at_start = it->hpos;
18218 int saved_face_id = it->face_id;
18219 struct glyph_row *row = it->glyph_row;
18220
18221 /* Initialize the iterator IT for iteration over STRING beginning
18222 with index START. */
18223 reseat_to_string (it, string, lisp_string, start,
18224 precision, field_width, multibyte);
18225
18226 /* If displaying STRING, set up the face of the iterator
18227 from LISP_STRING, if that's given. */
18228 if (STRINGP (face_string))
18229 {
18230 int endptr;
18231 struct face *face;
18232
18233 it->face_id
18234 = face_at_string_position (it->w, face_string, face_string_pos,
18235 0, it->region_beg_charpos,
18236 it->region_end_charpos,
18237 &endptr, it->base_face_id, 0);
18238 face = FACE_FROM_ID (it->f, it->face_id);
18239 it->face_box_p = face->box != FACE_NO_BOX;
18240 }
18241
18242 /* Set max_x to the maximum allowed X position. Don't let it go
18243 beyond the right edge of the window. */
18244 if (max_x <= 0)
18245 max_x = it->last_visible_x;
18246 else
18247 max_x = min (max_x, it->last_visible_x);
18248
18249 /* Skip over display elements that are not visible. because IT->w is
18250 hscrolled. */
18251 if (it->current_x < it->first_visible_x)
18252 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18253 MOVE_TO_POS | MOVE_TO_X);
18254
18255 row->ascent = it->max_ascent;
18256 row->height = it->max_ascent + it->max_descent;
18257 row->phys_ascent = it->max_phys_ascent;
18258 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18259 row->extra_line_spacing = it->max_extra_line_spacing;
18260
18261 /* This condition is for the case that we are called with current_x
18262 past last_visible_x. */
18263 while (it->current_x < max_x)
18264 {
18265 int x_before, x, n_glyphs_before, i, nglyphs;
18266
18267 /* Get the next display element. */
18268 if (!get_next_display_element (it))
18269 break;
18270
18271 /* Produce glyphs. */
18272 x_before = it->current_x;
18273 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18274 PRODUCE_GLYPHS (it);
18275
18276 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18277 i = 0;
18278 x = x_before;
18279 while (i < nglyphs)
18280 {
18281 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18282
18283 if (!it->truncate_lines_p
18284 && x + glyph->pixel_width > max_x)
18285 {
18286 /* End of continued line or max_x reached. */
18287 if (CHAR_GLYPH_PADDING_P (*glyph))
18288 {
18289 /* A wide character is unbreakable. */
18290 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18291 it->current_x = x_before;
18292 }
18293 else
18294 {
18295 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18296 it->current_x = x;
18297 }
18298 break;
18299 }
18300 else if (x + glyph->pixel_width > it->first_visible_x)
18301 {
18302 /* Glyph is at least partially visible. */
18303 ++it->hpos;
18304 if (x < it->first_visible_x)
18305 it->glyph_row->x = x - it->first_visible_x;
18306 }
18307 else
18308 {
18309 /* Glyph is off the left margin of the display area.
18310 Should not happen. */
18311 abort ();
18312 }
18313
18314 row->ascent = max (row->ascent, it->max_ascent);
18315 row->height = max (row->height, it->max_ascent + it->max_descent);
18316 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18317 row->phys_height = max (row->phys_height,
18318 it->max_phys_ascent + it->max_phys_descent);
18319 row->extra_line_spacing = max (row->extra_line_spacing,
18320 it->max_extra_line_spacing);
18321 x += glyph->pixel_width;
18322 ++i;
18323 }
18324
18325 /* Stop if max_x reached. */
18326 if (i < nglyphs)
18327 break;
18328
18329 /* Stop at line ends. */
18330 if (ITERATOR_AT_END_OF_LINE_P (it))
18331 {
18332 it->continuation_lines_width = 0;
18333 break;
18334 }
18335
18336 set_iterator_to_next (it, 1);
18337
18338 /* Stop if truncating at the right edge. */
18339 if (it->truncate_lines_p
18340 && it->current_x >= it->last_visible_x)
18341 {
18342 /* Add truncation mark, but don't do it if the line is
18343 truncated at a padding space. */
18344 if (IT_CHARPOS (*it) < it->string_nchars)
18345 {
18346 if (!FRAME_WINDOW_P (it->f))
18347 {
18348 int i, n;
18349
18350 if (it->current_x > it->last_visible_x)
18351 {
18352 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18353 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18354 break;
18355 for (n = row->used[TEXT_AREA]; i < n; ++i)
18356 {
18357 row->used[TEXT_AREA] = i;
18358 produce_special_glyphs (it, IT_TRUNCATION);
18359 }
18360 }
18361 produce_special_glyphs (it, IT_TRUNCATION);
18362 }
18363 it->glyph_row->truncated_on_right_p = 1;
18364 }
18365 break;
18366 }
18367 }
18368
18369 /* Maybe insert a truncation at the left. */
18370 if (it->first_visible_x
18371 && IT_CHARPOS (*it) > 0)
18372 {
18373 if (!FRAME_WINDOW_P (it->f))
18374 insert_left_trunc_glyphs (it);
18375 it->glyph_row->truncated_on_left_p = 1;
18376 }
18377
18378 it->face_id = saved_face_id;
18379
18380 /* Value is number of columns displayed. */
18381 return it->hpos - hpos_at_start;
18382 }
18383
18384
18385 \f
18386 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18387 appears as an element of LIST or as the car of an element of LIST.
18388 If PROPVAL is a list, compare each element against LIST in that
18389 way, and return 1/2 if any element of PROPVAL is found in LIST.
18390 Otherwise return 0. This function cannot quit.
18391 The return value is 2 if the text is invisible but with an ellipsis
18392 and 1 if it's invisible and without an ellipsis. */
18393
18394 int
18395 invisible_p (propval, list)
18396 register Lisp_Object propval;
18397 Lisp_Object list;
18398 {
18399 register Lisp_Object tail, proptail;
18400
18401 for (tail = list; CONSP (tail); tail = XCDR (tail))
18402 {
18403 register Lisp_Object tem;
18404 tem = XCAR (tail);
18405 if (EQ (propval, tem))
18406 return 1;
18407 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18408 return NILP (XCDR (tem)) ? 1 : 2;
18409 }
18410
18411 if (CONSP (propval))
18412 {
18413 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18414 {
18415 Lisp_Object propelt;
18416 propelt = XCAR (proptail);
18417 for (tail = list; CONSP (tail); tail = XCDR (tail))
18418 {
18419 register Lisp_Object tem;
18420 tem = XCAR (tail);
18421 if (EQ (propelt, tem))
18422 return 1;
18423 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18424 return NILP (XCDR (tem)) ? 1 : 2;
18425 }
18426 }
18427 }
18428
18429 return 0;
18430 }
18431
18432 /* Calculate a width or height in pixels from a specification using
18433 the following elements:
18434
18435 SPEC ::=
18436 NUM - a (fractional) multiple of the default font width/height
18437 (NUM) - specifies exactly NUM pixels
18438 UNIT - a fixed number of pixels, see below.
18439 ELEMENT - size of a display element in pixels, see below.
18440 (NUM . SPEC) - equals NUM * SPEC
18441 (+ SPEC SPEC ...) - add pixel values
18442 (- SPEC SPEC ...) - subtract pixel values
18443 (- SPEC) - negate pixel value
18444
18445 NUM ::=
18446 INT or FLOAT - a number constant
18447 SYMBOL - use symbol's (buffer local) variable binding.
18448
18449 UNIT ::=
18450 in - pixels per inch *)
18451 mm - pixels per 1/1000 meter *)
18452 cm - pixels per 1/100 meter *)
18453 width - width of current font in pixels.
18454 height - height of current font in pixels.
18455
18456 *) using the ratio(s) defined in display-pixels-per-inch.
18457
18458 ELEMENT ::=
18459
18460 left-fringe - left fringe width in pixels
18461 right-fringe - right fringe width in pixels
18462
18463 left-margin - left margin width in pixels
18464 right-margin - right margin width in pixels
18465
18466 scroll-bar - scroll-bar area width in pixels
18467
18468 Examples:
18469
18470 Pixels corresponding to 5 inches:
18471 (5 . in)
18472
18473 Total width of non-text areas on left side of window (if scroll-bar is on left):
18474 '(space :width (+ left-fringe left-margin scroll-bar))
18475
18476 Align to first text column (in header line):
18477 '(space :align-to 0)
18478
18479 Align to middle of text area minus half the width of variable `my-image'
18480 containing a loaded image:
18481 '(space :align-to (0.5 . (- text my-image)))
18482
18483 Width of left margin minus width of 1 character in the default font:
18484 '(space :width (- left-margin 1))
18485
18486 Width of left margin minus width of 2 characters in the current font:
18487 '(space :width (- left-margin (2 . width)))
18488
18489 Center 1 character over left-margin (in header line):
18490 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18491
18492 Different ways to express width of left fringe plus left margin minus one pixel:
18493 '(space :width (- (+ left-fringe left-margin) (1)))
18494 '(space :width (+ left-fringe left-margin (- (1))))
18495 '(space :width (+ left-fringe left-margin (-1)))
18496
18497 */
18498
18499 #define NUMVAL(X) \
18500 ((INTEGERP (X) || FLOATP (X)) \
18501 ? XFLOATINT (X) \
18502 : - 1)
18503
18504 int
18505 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18506 double *res;
18507 struct it *it;
18508 Lisp_Object prop;
18509 void *font;
18510 int width_p, *align_to;
18511 {
18512 double pixels;
18513
18514 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18515 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18516
18517 if (NILP (prop))
18518 return OK_PIXELS (0);
18519
18520 if (SYMBOLP (prop))
18521 {
18522 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18523 {
18524 char *unit = SDATA (SYMBOL_NAME (prop));
18525
18526 if (unit[0] == 'i' && unit[1] == 'n')
18527 pixels = 1.0;
18528 else if (unit[0] == 'm' && unit[1] == 'm')
18529 pixels = 25.4;
18530 else if (unit[0] == 'c' && unit[1] == 'm')
18531 pixels = 2.54;
18532 else
18533 pixels = 0;
18534 if (pixels > 0)
18535 {
18536 double ppi;
18537 #ifdef HAVE_WINDOW_SYSTEM
18538 if (FRAME_WINDOW_P (it->f)
18539 && (ppi = (width_p
18540 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18541 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18542 ppi > 0))
18543 return OK_PIXELS (ppi / pixels);
18544 #endif
18545
18546 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18547 || (CONSP (Vdisplay_pixels_per_inch)
18548 && (ppi = (width_p
18549 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18550 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18551 ppi > 0)))
18552 return OK_PIXELS (ppi / pixels);
18553
18554 return 0;
18555 }
18556 }
18557
18558 #ifdef HAVE_WINDOW_SYSTEM
18559 if (EQ (prop, Qheight))
18560 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18561 if (EQ (prop, Qwidth))
18562 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18563 #else
18564 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18565 return OK_PIXELS (1);
18566 #endif
18567
18568 if (EQ (prop, Qtext))
18569 return OK_PIXELS (width_p
18570 ? window_box_width (it->w, TEXT_AREA)
18571 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18572
18573 if (align_to && *align_to < 0)
18574 {
18575 *res = 0;
18576 if (EQ (prop, Qleft))
18577 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18578 if (EQ (prop, Qright))
18579 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18580 if (EQ (prop, Qcenter))
18581 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18582 + window_box_width (it->w, TEXT_AREA) / 2);
18583 if (EQ (prop, Qleft_fringe))
18584 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18585 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18586 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18587 if (EQ (prop, Qright_fringe))
18588 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18589 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18590 : window_box_right_offset (it->w, TEXT_AREA));
18591 if (EQ (prop, Qleft_margin))
18592 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18593 if (EQ (prop, Qright_margin))
18594 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18595 if (EQ (prop, Qscroll_bar))
18596 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18597 ? 0
18598 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18599 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18600 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18601 : 0)));
18602 }
18603 else
18604 {
18605 if (EQ (prop, Qleft_fringe))
18606 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18607 if (EQ (prop, Qright_fringe))
18608 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18609 if (EQ (prop, Qleft_margin))
18610 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18611 if (EQ (prop, Qright_margin))
18612 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18613 if (EQ (prop, Qscroll_bar))
18614 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18615 }
18616
18617 prop = Fbuffer_local_value (prop, it->w->buffer);
18618 }
18619
18620 if (INTEGERP (prop) || FLOATP (prop))
18621 {
18622 int base_unit = (width_p
18623 ? FRAME_COLUMN_WIDTH (it->f)
18624 : FRAME_LINE_HEIGHT (it->f));
18625 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18626 }
18627
18628 if (CONSP (prop))
18629 {
18630 Lisp_Object car = XCAR (prop);
18631 Lisp_Object cdr = XCDR (prop);
18632
18633 if (SYMBOLP (car))
18634 {
18635 #ifdef HAVE_WINDOW_SYSTEM
18636 if (valid_image_p (prop))
18637 {
18638 int id = lookup_image (it->f, prop);
18639 struct image *img = IMAGE_FROM_ID (it->f, id);
18640
18641 return OK_PIXELS (width_p ? img->width : img->height);
18642 }
18643 #endif
18644 if (EQ (car, Qplus) || EQ (car, Qminus))
18645 {
18646 int first = 1;
18647 double px;
18648
18649 pixels = 0;
18650 while (CONSP (cdr))
18651 {
18652 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18653 font, width_p, align_to))
18654 return 0;
18655 if (first)
18656 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18657 else
18658 pixels += px;
18659 cdr = XCDR (cdr);
18660 }
18661 if (EQ (car, Qminus))
18662 pixels = -pixels;
18663 return OK_PIXELS (pixels);
18664 }
18665
18666 car = Fbuffer_local_value (car, it->w->buffer);
18667 }
18668
18669 if (INTEGERP (car) || FLOATP (car))
18670 {
18671 double fact;
18672 pixels = XFLOATINT (car);
18673 if (NILP (cdr))
18674 return OK_PIXELS (pixels);
18675 if (calc_pixel_width_or_height (&fact, it, cdr,
18676 font, width_p, align_to))
18677 return OK_PIXELS (pixels * fact);
18678 return 0;
18679 }
18680
18681 return 0;
18682 }
18683
18684 return 0;
18685 }
18686
18687 \f
18688 /***********************************************************************
18689 Glyph Display
18690 ***********************************************************************/
18691
18692 #ifdef HAVE_WINDOW_SYSTEM
18693
18694 #if GLYPH_DEBUG
18695
18696 void
18697 dump_glyph_string (s)
18698 struct glyph_string *s;
18699 {
18700 fprintf (stderr, "glyph string\n");
18701 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18702 s->x, s->y, s->width, s->height);
18703 fprintf (stderr, " ybase = %d\n", s->ybase);
18704 fprintf (stderr, " hl = %d\n", s->hl);
18705 fprintf (stderr, " left overhang = %d, right = %d\n",
18706 s->left_overhang, s->right_overhang);
18707 fprintf (stderr, " nchars = %d\n", s->nchars);
18708 fprintf (stderr, " extends to end of line = %d\n",
18709 s->extends_to_end_of_line_p);
18710 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18711 fprintf (stderr, " bg width = %d\n", s->background_width);
18712 }
18713
18714 #endif /* GLYPH_DEBUG */
18715
18716 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18717 of XChar2b structures for S; it can't be allocated in
18718 init_glyph_string because it must be allocated via `alloca'. W
18719 is the window on which S is drawn. ROW and AREA are the glyph row
18720 and area within the row from which S is constructed. START is the
18721 index of the first glyph structure covered by S. HL is a
18722 face-override for drawing S. */
18723
18724 #ifdef HAVE_NTGUI
18725 #define OPTIONAL_HDC(hdc) hdc,
18726 #define DECLARE_HDC(hdc) HDC hdc;
18727 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18728 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18729 #endif
18730
18731 #ifndef OPTIONAL_HDC
18732 #define OPTIONAL_HDC(hdc)
18733 #define DECLARE_HDC(hdc)
18734 #define ALLOCATE_HDC(hdc, f)
18735 #define RELEASE_HDC(hdc, f)
18736 #endif
18737
18738 static void
18739 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18740 struct glyph_string *s;
18741 DECLARE_HDC (hdc)
18742 XChar2b *char2b;
18743 struct window *w;
18744 struct glyph_row *row;
18745 enum glyph_row_area area;
18746 int start;
18747 enum draw_glyphs_face hl;
18748 {
18749 bzero (s, sizeof *s);
18750 s->w = w;
18751 s->f = XFRAME (w->frame);
18752 #ifdef HAVE_NTGUI
18753 s->hdc = hdc;
18754 #endif
18755 s->display = FRAME_X_DISPLAY (s->f);
18756 s->window = FRAME_X_WINDOW (s->f);
18757 s->char2b = char2b;
18758 s->hl = hl;
18759 s->row = row;
18760 s->area = area;
18761 s->first_glyph = row->glyphs[area] + start;
18762 s->height = row->height;
18763 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18764
18765 /* Display the internal border below the tool-bar window. */
18766 if (WINDOWP (s->f->tool_bar_window)
18767 && s->w == XWINDOW (s->f->tool_bar_window))
18768 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18769
18770 s->ybase = s->y + row->ascent;
18771 }
18772
18773
18774 /* Append the list of glyph strings with head H and tail T to the list
18775 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18776
18777 static INLINE void
18778 append_glyph_string_lists (head, tail, h, t)
18779 struct glyph_string **head, **tail;
18780 struct glyph_string *h, *t;
18781 {
18782 if (h)
18783 {
18784 if (*head)
18785 (*tail)->next = h;
18786 else
18787 *head = h;
18788 h->prev = *tail;
18789 *tail = t;
18790 }
18791 }
18792
18793
18794 /* Prepend the list of glyph strings with head H and tail T to the
18795 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18796 result. */
18797
18798 static INLINE void
18799 prepend_glyph_string_lists (head, tail, h, t)
18800 struct glyph_string **head, **tail;
18801 struct glyph_string *h, *t;
18802 {
18803 if (h)
18804 {
18805 if (*head)
18806 (*head)->prev = t;
18807 else
18808 *tail = t;
18809 t->next = *head;
18810 *head = h;
18811 }
18812 }
18813
18814
18815 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18816 Set *HEAD and *TAIL to the resulting list. */
18817
18818 static INLINE void
18819 append_glyph_string (head, tail, s)
18820 struct glyph_string **head, **tail;
18821 struct glyph_string *s;
18822 {
18823 s->next = s->prev = NULL;
18824 append_glyph_string_lists (head, tail, s, s);
18825 }
18826
18827
18828 /* Get face and two-byte form of character glyph GLYPH on frame F.
18829 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18830 a pointer to a realized face that is ready for display. */
18831
18832 static INLINE struct face *
18833 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18834 struct frame *f;
18835 struct glyph *glyph;
18836 XChar2b *char2b;
18837 int *two_byte_p;
18838 {
18839 struct face *face;
18840
18841 xassert (glyph->type == CHAR_GLYPH);
18842 face = FACE_FROM_ID (f, glyph->face_id);
18843
18844 if (two_byte_p)
18845 *two_byte_p = 0;
18846
18847 if (!glyph->multibyte_p)
18848 {
18849 /* Unibyte case. We don't have to encode, but we have to make
18850 sure to use a face suitable for unibyte. */
18851 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18852 }
18853 else if (glyph->u.ch < 128)
18854 {
18855 /* Case of ASCII in a face known to fit ASCII. */
18856 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18857 }
18858 else
18859 {
18860 int c1, c2, charset;
18861
18862 /* Split characters into bytes. If c2 is -1 afterwards, C is
18863 really a one-byte character so that byte1 is zero. */
18864 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18865 if (c2 > 0)
18866 STORE_XCHAR2B (char2b, c1, c2);
18867 else
18868 STORE_XCHAR2B (char2b, 0, c1);
18869
18870 /* Maybe encode the character in *CHAR2B. */
18871 if (charset != CHARSET_ASCII)
18872 {
18873 struct font_info *font_info
18874 = FONT_INFO_FROM_ID (f, face->font_info_id);
18875 if (font_info)
18876 glyph->font_type
18877 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18878 }
18879 }
18880
18881 /* Make sure X resources of the face are allocated. */
18882 xassert (face != NULL);
18883 PREPARE_FACE_FOR_DISPLAY (f, face);
18884 return face;
18885 }
18886
18887
18888 /* Fill glyph string S with composition components specified by S->cmp.
18889
18890 FACES is an array of faces for all components of this composition.
18891 S->gidx is the index of the first component for S.
18892
18893 OVERLAPS non-zero means S should draw the foreground only, and use
18894 its physical height for clipping. See also draw_glyphs.
18895
18896 Value is the index of a component not in S. */
18897
18898 static int
18899 fill_composite_glyph_string (s, faces, overlaps)
18900 struct glyph_string *s;
18901 struct face **faces;
18902 int overlaps;
18903 {
18904 int i;
18905
18906 xassert (s);
18907
18908 s->for_overlaps = overlaps;
18909
18910 s->face = faces[s->gidx];
18911 s->font = s->face->font;
18912 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18913
18914 /* For all glyphs of this composition, starting at the offset
18915 S->gidx, until we reach the end of the definition or encounter a
18916 glyph that requires the different face, add it to S. */
18917 ++s->nchars;
18918 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18919 ++s->nchars;
18920
18921 /* All glyph strings for the same composition has the same width,
18922 i.e. the width set for the first component of the composition. */
18923
18924 s->width = s->first_glyph->pixel_width;
18925
18926 /* If the specified font could not be loaded, use the frame's
18927 default font, but record the fact that we couldn't load it in
18928 the glyph string so that we can draw rectangles for the
18929 characters of the glyph string. */
18930 if (s->font == NULL)
18931 {
18932 s->font_not_found_p = 1;
18933 s->font = FRAME_FONT (s->f);
18934 }
18935
18936 /* Adjust base line for subscript/superscript text. */
18937 s->ybase += s->first_glyph->voffset;
18938
18939 xassert (s->face && s->face->gc);
18940
18941 /* This glyph string must always be drawn with 16-bit functions. */
18942 s->two_byte_p = 1;
18943
18944 return s->gidx + s->nchars;
18945 }
18946
18947
18948 /* Fill glyph string S from a sequence of character glyphs.
18949
18950 FACE_ID is the face id of the string. START is the index of the
18951 first glyph to consider, END is the index of the last + 1.
18952 OVERLAPS non-zero means S should draw the foreground only, and use
18953 its physical height for clipping. See also draw_glyphs.
18954
18955 Value is the index of the first glyph not in S. */
18956
18957 static int
18958 fill_glyph_string (s, face_id, start, end, overlaps)
18959 struct glyph_string *s;
18960 int face_id;
18961 int start, end, overlaps;
18962 {
18963 struct glyph *glyph, *last;
18964 int voffset;
18965 int glyph_not_available_p;
18966
18967 xassert (s->f == XFRAME (s->w->frame));
18968 xassert (s->nchars == 0);
18969 xassert (start >= 0 && end > start);
18970
18971 s->for_overlaps = overlaps,
18972 glyph = s->row->glyphs[s->area] + start;
18973 last = s->row->glyphs[s->area] + end;
18974 voffset = glyph->voffset;
18975
18976 glyph_not_available_p = glyph->glyph_not_available_p;
18977
18978 while (glyph < last
18979 && glyph->type == CHAR_GLYPH
18980 && glyph->voffset == voffset
18981 /* Same face id implies same font, nowadays. */
18982 && glyph->face_id == face_id
18983 && glyph->glyph_not_available_p == glyph_not_available_p)
18984 {
18985 int two_byte_p;
18986
18987 s->face = get_glyph_face_and_encoding (s->f, glyph,
18988 s->char2b + s->nchars,
18989 &two_byte_p);
18990 s->two_byte_p = two_byte_p;
18991 ++s->nchars;
18992 xassert (s->nchars <= end - start);
18993 s->width += glyph->pixel_width;
18994 ++glyph;
18995 }
18996
18997 s->font = s->face->font;
18998 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18999
19000 /* If the specified font could not be loaded, use the frame's font,
19001 but record the fact that we couldn't load it in
19002 S->font_not_found_p so that we can draw rectangles for the
19003 characters of the glyph string. */
19004 if (s->font == NULL || glyph_not_available_p)
19005 {
19006 s->font_not_found_p = 1;
19007 s->font = FRAME_FONT (s->f);
19008 }
19009
19010 /* Adjust base line for subscript/superscript text. */
19011 s->ybase += voffset;
19012
19013 xassert (s->face && s->face->gc);
19014 return glyph - s->row->glyphs[s->area];
19015 }
19016
19017
19018 /* Fill glyph string S from image glyph S->first_glyph. */
19019
19020 static void
19021 fill_image_glyph_string (s)
19022 struct glyph_string *s;
19023 {
19024 xassert (s->first_glyph->type == IMAGE_GLYPH);
19025 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19026 xassert (s->img);
19027 s->slice = s->first_glyph->slice;
19028 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19029 s->font = s->face->font;
19030 s->width = s->first_glyph->pixel_width;
19031
19032 /* Adjust base line for subscript/superscript text. */
19033 s->ybase += s->first_glyph->voffset;
19034 }
19035
19036
19037 /* Fill glyph string S from a sequence of stretch glyphs.
19038
19039 ROW is the glyph row in which the glyphs are found, AREA is the
19040 area within the row. START is the index of the first glyph to
19041 consider, END is the index of the last + 1.
19042
19043 Value is the index of the first glyph not in S. */
19044
19045 static int
19046 fill_stretch_glyph_string (s, row, area, start, end)
19047 struct glyph_string *s;
19048 struct glyph_row *row;
19049 enum glyph_row_area area;
19050 int start, end;
19051 {
19052 struct glyph *glyph, *last;
19053 int voffset, face_id;
19054
19055 xassert (s->first_glyph->type == STRETCH_GLYPH);
19056
19057 glyph = s->row->glyphs[s->area] + start;
19058 last = s->row->glyphs[s->area] + end;
19059 face_id = glyph->face_id;
19060 s->face = FACE_FROM_ID (s->f, face_id);
19061 s->font = s->face->font;
19062 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19063 s->width = glyph->pixel_width;
19064 s->nchars = 1;
19065 voffset = glyph->voffset;
19066
19067 for (++glyph;
19068 (glyph < last
19069 && glyph->type == STRETCH_GLYPH
19070 && glyph->voffset == voffset
19071 && glyph->face_id == face_id);
19072 ++glyph)
19073 s->width += glyph->pixel_width;
19074
19075 /* Adjust base line for subscript/superscript text. */
19076 s->ybase += voffset;
19077
19078 /* The case that face->gc == 0 is handled when drawing the glyph
19079 string by calling PREPARE_FACE_FOR_DISPLAY. */
19080 xassert (s->face);
19081 return glyph - s->row->glyphs[s->area];
19082 }
19083
19084
19085 /* EXPORT for RIF:
19086 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19087 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19088 assumed to be zero. */
19089
19090 void
19091 x_get_glyph_overhangs (glyph, f, left, right)
19092 struct glyph *glyph;
19093 struct frame *f;
19094 int *left, *right;
19095 {
19096 *left = *right = 0;
19097
19098 if (glyph->type == CHAR_GLYPH)
19099 {
19100 XFontStruct *font;
19101 struct face *face;
19102 struct font_info *font_info;
19103 XChar2b char2b;
19104 XCharStruct *pcm;
19105
19106 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19107 font = face->font;
19108 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19109 if (font /* ++KFS: Should this be font_info ? */
19110 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19111 {
19112 if (pcm->rbearing > pcm->width)
19113 *right = pcm->rbearing - pcm->width;
19114 if (pcm->lbearing < 0)
19115 *left = -pcm->lbearing;
19116 }
19117 }
19118 }
19119
19120
19121 /* Return the index of the first glyph preceding glyph string S that
19122 is overwritten by S because of S's left overhang. Value is -1
19123 if no glyphs are overwritten. */
19124
19125 static int
19126 left_overwritten (s)
19127 struct glyph_string *s;
19128 {
19129 int k;
19130
19131 if (s->left_overhang)
19132 {
19133 int x = 0, i;
19134 struct glyph *glyphs = s->row->glyphs[s->area];
19135 int first = s->first_glyph - glyphs;
19136
19137 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19138 x -= glyphs[i].pixel_width;
19139
19140 k = i + 1;
19141 }
19142 else
19143 k = -1;
19144
19145 return k;
19146 }
19147
19148
19149 /* Return the index of the first glyph preceding glyph string S that
19150 is overwriting S because of its right overhang. Value is -1 if no
19151 glyph in front of S overwrites S. */
19152
19153 static int
19154 left_overwriting (s)
19155 struct glyph_string *s;
19156 {
19157 int i, k, x;
19158 struct glyph *glyphs = s->row->glyphs[s->area];
19159 int first = s->first_glyph - glyphs;
19160
19161 k = -1;
19162 x = 0;
19163 for (i = first - 1; i >= 0; --i)
19164 {
19165 int left, right;
19166 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19167 if (x + right > 0)
19168 k = i;
19169 x -= glyphs[i].pixel_width;
19170 }
19171
19172 return k;
19173 }
19174
19175
19176 /* Return the index of the last glyph following glyph string S that is
19177 not overwritten by S because of S's right overhang. Value is -1 if
19178 no such glyph is found. */
19179
19180 static int
19181 right_overwritten (s)
19182 struct glyph_string *s;
19183 {
19184 int k = -1;
19185
19186 if (s->right_overhang)
19187 {
19188 int x = 0, i;
19189 struct glyph *glyphs = s->row->glyphs[s->area];
19190 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19191 int end = s->row->used[s->area];
19192
19193 for (i = first; i < end && s->right_overhang > x; ++i)
19194 x += glyphs[i].pixel_width;
19195
19196 k = i;
19197 }
19198
19199 return k;
19200 }
19201
19202
19203 /* Return the index of the last glyph following glyph string S that
19204 overwrites S because of its left overhang. Value is negative
19205 if no such glyph is found. */
19206
19207 static int
19208 right_overwriting (s)
19209 struct glyph_string *s;
19210 {
19211 int i, k, x;
19212 int end = s->row->used[s->area];
19213 struct glyph *glyphs = s->row->glyphs[s->area];
19214 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19215
19216 k = -1;
19217 x = 0;
19218 for (i = first; i < end; ++i)
19219 {
19220 int left, right;
19221 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19222 if (x - left < 0)
19223 k = i;
19224 x += glyphs[i].pixel_width;
19225 }
19226
19227 return k;
19228 }
19229
19230
19231 /* Get face and two-byte form of character C in face FACE_ID on frame
19232 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19233 means we want to display multibyte text. DISPLAY_P non-zero means
19234 make sure that X resources for the face returned are allocated.
19235 Value is a pointer to a realized face that is ready for display if
19236 DISPLAY_P is non-zero. */
19237
19238 static INLINE struct face *
19239 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19240 struct frame *f;
19241 int c, face_id;
19242 XChar2b *char2b;
19243 int multibyte_p, display_p;
19244 {
19245 struct face *face = FACE_FROM_ID (f, face_id);
19246
19247 if (!multibyte_p)
19248 {
19249 /* Unibyte case. We don't have to encode, but we have to make
19250 sure to use a face suitable for unibyte. */
19251 STORE_XCHAR2B (char2b, 0, c);
19252 face_id = FACE_FOR_CHAR (f, face, c);
19253 face = FACE_FROM_ID (f, face_id);
19254 }
19255 else if (c < 128)
19256 {
19257 /* Case of ASCII in a face known to fit ASCII. */
19258 STORE_XCHAR2B (char2b, 0, c);
19259 }
19260 else
19261 {
19262 int c1, c2, charset;
19263
19264 /* Split characters into bytes. If c2 is -1 afterwards, C is
19265 really a one-byte character so that byte1 is zero. */
19266 SPLIT_CHAR (c, charset, c1, c2);
19267 if (c2 > 0)
19268 STORE_XCHAR2B (char2b, c1, c2);
19269 else
19270 STORE_XCHAR2B (char2b, 0, c1);
19271
19272 /* Maybe encode the character in *CHAR2B. */
19273 if (face->font != NULL)
19274 {
19275 struct font_info *font_info
19276 = FONT_INFO_FROM_ID (f, face->font_info_id);
19277 if (font_info)
19278 rif->encode_char (c, char2b, font_info, 0);
19279 }
19280 }
19281
19282 /* Make sure X resources of the face are allocated. */
19283 #ifdef HAVE_X_WINDOWS
19284 if (display_p)
19285 #endif
19286 {
19287 xassert (face != NULL);
19288 PREPARE_FACE_FOR_DISPLAY (f, face);
19289 }
19290
19291 return face;
19292 }
19293
19294
19295 /* Set background width of glyph string S. START is the index of the
19296 first glyph following S. LAST_X is the right-most x-position + 1
19297 in the drawing area. */
19298
19299 static INLINE void
19300 set_glyph_string_background_width (s, start, last_x)
19301 struct glyph_string *s;
19302 int start;
19303 int last_x;
19304 {
19305 /* If the face of this glyph string has to be drawn to the end of
19306 the drawing area, set S->extends_to_end_of_line_p. */
19307
19308 if (start == s->row->used[s->area]
19309 && s->area == TEXT_AREA
19310 && ((s->row->fill_line_p
19311 && (s->hl == DRAW_NORMAL_TEXT
19312 || s->hl == DRAW_IMAGE_RAISED
19313 || s->hl == DRAW_IMAGE_SUNKEN))
19314 || s->hl == DRAW_MOUSE_FACE))
19315 s->extends_to_end_of_line_p = 1;
19316
19317 /* If S extends its face to the end of the line, set its
19318 background_width to the distance to the right edge of the drawing
19319 area. */
19320 if (s->extends_to_end_of_line_p)
19321 s->background_width = last_x - s->x + 1;
19322 else
19323 s->background_width = s->width;
19324 }
19325
19326
19327 /* Compute overhangs and x-positions for glyph string S and its
19328 predecessors, or successors. X is the starting x-position for S.
19329 BACKWARD_P non-zero means process predecessors. */
19330
19331 static void
19332 compute_overhangs_and_x (s, x, backward_p)
19333 struct glyph_string *s;
19334 int x;
19335 int backward_p;
19336 {
19337 if (backward_p)
19338 {
19339 while (s)
19340 {
19341 if (rif->compute_glyph_string_overhangs)
19342 rif->compute_glyph_string_overhangs (s);
19343 x -= s->width;
19344 s->x = x;
19345 s = s->prev;
19346 }
19347 }
19348 else
19349 {
19350 while (s)
19351 {
19352 if (rif->compute_glyph_string_overhangs)
19353 rif->compute_glyph_string_overhangs (s);
19354 s->x = x;
19355 x += s->width;
19356 s = s->next;
19357 }
19358 }
19359 }
19360
19361
19362
19363 /* The following macros are only called from draw_glyphs below.
19364 They reference the following parameters of that function directly:
19365 `w', `row', `area', and `overlap_p'
19366 as well as the following local variables:
19367 `s', `f', and `hdc' (in W32) */
19368
19369 #ifdef HAVE_NTGUI
19370 /* On W32, silently add local `hdc' variable to argument list of
19371 init_glyph_string. */
19372 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19373 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19374 #else
19375 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19376 init_glyph_string (s, char2b, w, row, area, start, hl)
19377 #endif
19378
19379 /* Add a glyph string for a stretch glyph to the list of strings
19380 between HEAD and TAIL. START is the index of the stretch glyph in
19381 row area AREA of glyph row ROW. END is the index of the last glyph
19382 in that glyph row area. X is the current output position assigned
19383 to the new glyph string constructed. HL overrides that face of the
19384 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19385 is the right-most x-position of the drawing area. */
19386
19387 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19388 and below -- keep them on one line. */
19389 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19390 do \
19391 { \
19392 s = (struct glyph_string *) alloca (sizeof *s); \
19393 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19394 START = fill_stretch_glyph_string (s, row, area, START, END); \
19395 append_glyph_string (&HEAD, &TAIL, s); \
19396 s->x = (X); \
19397 } \
19398 while (0)
19399
19400
19401 /* Add a glyph string for an image glyph to the list of strings
19402 between HEAD and TAIL. START is the index of the image glyph in
19403 row area AREA of glyph row ROW. END is the index of the last glyph
19404 in that glyph row area. X is the current output position assigned
19405 to the new glyph string constructed. HL overrides that face of the
19406 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19407 is the right-most x-position of the drawing area. */
19408
19409 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19410 do \
19411 { \
19412 s = (struct glyph_string *) alloca (sizeof *s); \
19413 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19414 fill_image_glyph_string (s); \
19415 append_glyph_string (&HEAD, &TAIL, s); \
19416 ++START; \
19417 s->x = (X); \
19418 } \
19419 while (0)
19420
19421
19422 /* Add a glyph string for a sequence of character glyphs to the list
19423 of strings between HEAD and TAIL. START is the index of the first
19424 glyph in row area AREA of glyph row ROW that is part of the new
19425 glyph string. END is the index of the last glyph in that glyph row
19426 area. X is the current output position assigned to the new glyph
19427 string constructed. HL overrides that face of the glyph; e.g. it
19428 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19429 right-most x-position of the drawing area. */
19430
19431 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19432 do \
19433 { \
19434 int c, face_id; \
19435 XChar2b *char2b; \
19436 \
19437 c = (row)->glyphs[area][START].u.ch; \
19438 face_id = (row)->glyphs[area][START].face_id; \
19439 \
19440 s = (struct glyph_string *) alloca (sizeof *s); \
19441 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19442 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19443 append_glyph_string (&HEAD, &TAIL, s); \
19444 s->x = (X); \
19445 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19446 } \
19447 while (0)
19448
19449
19450 /* Add a glyph string for a composite sequence to the list of strings
19451 between HEAD and TAIL. START is the index of the first glyph in
19452 row area AREA of glyph row ROW that is part of the new glyph
19453 string. END is the index of the last glyph in that glyph row area.
19454 X is the current output position assigned to the new glyph string
19455 constructed. HL overrides that face of the glyph; e.g. it is
19456 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19457 x-position of the drawing area. */
19458
19459 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19460 do { \
19461 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19462 int face_id = (row)->glyphs[area][START].face_id; \
19463 struct face *base_face = FACE_FROM_ID (f, face_id); \
19464 struct composition *cmp = composition_table[cmp_id]; \
19465 int glyph_len = cmp->glyph_len; \
19466 XChar2b *char2b; \
19467 struct face **faces; \
19468 struct glyph_string *first_s = NULL; \
19469 int n; \
19470 \
19471 base_face = base_face->ascii_face; \
19472 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19473 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19474 /* At first, fill in `char2b' and `faces'. */ \
19475 for (n = 0; n < glyph_len; n++) \
19476 { \
19477 int c = COMPOSITION_GLYPH (cmp, n); \
19478 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19479 faces[n] = FACE_FROM_ID (f, this_face_id); \
19480 get_char_face_and_encoding (f, c, this_face_id, \
19481 char2b + n, 1, 1); \
19482 } \
19483 \
19484 /* Make glyph_strings for each glyph sequence that is drawable by \
19485 the same face, and append them to HEAD/TAIL. */ \
19486 for (n = 0; n < cmp->glyph_len;) \
19487 { \
19488 s = (struct glyph_string *) alloca (sizeof *s); \
19489 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19490 append_glyph_string (&(HEAD), &(TAIL), s); \
19491 s->cmp = cmp; \
19492 s->gidx = n; \
19493 s->x = (X); \
19494 \
19495 if (n == 0) \
19496 first_s = s; \
19497 \
19498 n = fill_composite_glyph_string (s, faces, overlaps); \
19499 } \
19500 \
19501 ++START; \
19502 s = first_s; \
19503 } while (0)
19504
19505
19506 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19507 of AREA of glyph row ROW on window W between indices START and END.
19508 HL overrides the face for drawing glyph strings, e.g. it is
19509 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19510 x-positions of the drawing area.
19511
19512 This is an ugly monster macro construct because we must use alloca
19513 to allocate glyph strings (because draw_glyphs can be called
19514 asynchronously). */
19515
19516 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19517 do \
19518 { \
19519 HEAD = TAIL = NULL; \
19520 while (START < END) \
19521 { \
19522 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19523 switch (first_glyph->type) \
19524 { \
19525 case CHAR_GLYPH: \
19526 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19527 HL, X, LAST_X); \
19528 break; \
19529 \
19530 case COMPOSITE_GLYPH: \
19531 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19532 HL, X, LAST_X); \
19533 break; \
19534 \
19535 case STRETCH_GLYPH: \
19536 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19537 HL, X, LAST_X); \
19538 break; \
19539 \
19540 case IMAGE_GLYPH: \
19541 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19542 HL, X, LAST_X); \
19543 break; \
19544 \
19545 default: \
19546 abort (); \
19547 } \
19548 \
19549 set_glyph_string_background_width (s, START, LAST_X); \
19550 (X) += s->width; \
19551 } \
19552 } \
19553 while (0)
19554
19555
19556 /* Draw glyphs between START and END in AREA of ROW on window W,
19557 starting at x-position X. X is relative to AREA in W. HL is a
19558 face-override with the following meaning:
19559
19560 DRAW_NORMAL_TEXT draw normally
19561 DRAW_CURSOR draw in cursor face
19562 DRAW_MOUSE_FACE draw in mouse face.
19563 DRAW_INVERSE_VIDEO draw in mode line face
19564 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19565 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19566
19567 If OVERLAPS is non-zero, draw only the foreground of characters and
19568 clip to the physical height of ROW. Non-zero value also defines
19569 the overlapping part to be drawn:
19570
19571 OVERLAPS_PRED overlap with preceding rows
19572 OVERLAPS_SUCC overlap with succeeding rows
19573 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19574 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19575
19576 Value is the x-position reached, relative to AREA of W. */
19577
19578 static int
19579 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19580 struct window *w;
19581 int x;
19582 struct glyph_row *row;
19583 enum glyph_row_area area;
19584 int start, end;
19585 enum draw_glyphs_face hl;
19586 int overlaps;
19587 {
19588 struct glyph_string *head, *tail;
19589 struct glyph_string *s;
19590 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19591 int last_x, area_width;
19592 int x_reached;
19593 int i, j;
19594 struct frame *f = XFRAME (WINDOW_FRAME (w));
19595 DECLARE_HDC (hdc);
19596
19597 ALLOCATE_HDC (hdc, f);
19598
19599 /* Let's rather be paranoid than getting a SEGV. */
19600 end = min (end, row->used[area]);
19601 start = max (0, start);
19602 start = min (end, start);
19603
19604 /* Translate X to frame coordinates. Set last_x to the right
19605 end of the drawing area. */
19606 if (row->full_width_p)
19607 {
19608 /* X is relative to the left edge of W, without scroll bars
19609 or fringes. */
19610 x += WINDOW_LEFT_EDGE_X (w);
19611 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19612 }
19613 else
19614 {
19615 int area_left = window_box_left (w, area);
19616 x += area_left;
19617 area_width = window_box_width (w, area);
19618 last_x = area_left + area_width;
19619 }
19620
19621 /* Build a doubly-linked list of glyph_string structures between
19622 head and tail from what we have to draw. Note that the macro
19623 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19624 the reason we use a separate variable `i'. */
19625 i = start;
19626 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19627 if (tail)
19628 x_reached = tail->x + tail->background_width;
19629 else
19630 x_reached = x;
19631
19632 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19633 the row, redraw some glyphs in front or following the glyph
19634 strings built above. */
19635 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19636 {
19637 int dummy_x = 0;
19638 struct glyph_string *h, *t;
19639
19640 /* Compute overhangs for all glyph strings. */
19641 if (rif->compute_glyph_string_overhangs)
19642 for (s = head; s; s = s->next)
19643 rif->compute_glyph_string_overhangs (s);
19644
19645 /* Prepend glyph strings for glyphs in front of the first glyph
19646 string that are overwritten because of the first glyph
19647 string's left overhang. The background of all strings
19648 prepended must be drawn because the first glyph string
19649 draws over it. */
19650 i = left_overwritten (head);
19651 if (i >= 0)
19652 {
19653 j = i;
19654 BUILD_GLYPH_STRINGS (j, start, h, t,
19655 DRAW_NORMAL_TEXT, dummy_x, last_x);
19656 start = i;
19657 compute_overhangs_and_x (t, head->x, 1);
19658 prepend_glyph_string_lists (&head, &tail, h, t);
19659 clip_head = head;
19660 }
19661
19662 /* Prepend glyph strings for glyphs in front of the first glyph
19663 string that overwrite that glyph string because of their
19664 right overhang. For these strings, only the foreground must
19665 be drawn, because it draws over the glyph string at `head'.
19666 The background must not be drawn because this would overwrite
19667 right overhangs of preceding glyphs for which no glyph
19668 strings exist. */
19669 i = left_overwriting (head);
19670 if (i >= 0)
19671 {
19672 clip_head = head;
19673 BUILD_GLYPH_STRINGS (i, start, h, t,
19674 DRAW_NORMAL_TEXT, dummy_x, last_x);
19675 for (s = h; s; s = s->next)
19676 s->background_filled_p = 1;
19677 compute_overhangs_and_x (t, head->x, 1);
19678 prepend_glyph_string_lists (&head, &tail, h, t);
19679 }
19680
19681 /* Append glyphs strings for glyphs following the last glyph
19682 string tail that are overwritten by tail. The background of
19683 these strings has to be drawn because tail's foreground draws
19684 over it. */
19685 i = right_overwritten (tail);
19686 if (i >= 0)
19687 {
19688 BUILD_GLYPH_STRINGS (end, i, h, t,
19689 DRAW_NORMAL_TEXT, x, last_x);
19690 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19691 append_glyph_string_lists (&head, &tail, h, t);
19692 clip_tail = tail;
19693 }
19694
19695 /* Append glyph strings for glyphs following the last glyph
19696 string tail that overwrite tail. The foreground of such
19697 glyphs has to be drawn because it writes into the background
19698 of tail. The background must not be drawn because it could
19699 paint over the foreground of following glyphs. */
19700 i = right_overwriting (tail);
19701 if (i >= 0)
19702 {
19703 clip_tail = tail;
19704 BUILD_GLYPH_STRINGS (end, i, h, t,
19705 DRAW_NORMAL_TEXT, x, last_x);
19706 for (s = h; s; s = s->next)
19707 s->background_filled_p = 1;
19708 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19709 append_glyph_string_lists (&head, &tail, h, t);
19710 }
19711 if (clip_head || clip_tail)
19712 for (s = head; s; s = s->next)
19713 {
19714 s->clip_head = clip_head;
19715 s->clip_tail = clip_tail;
19716 }
19717 }
19718
19719 /* Draw all strings. */
19720 for (s = head; s; s = s->next)
19721 rif->draw_glyph_string (s);
19722
19723 if (area == TEXT_AREA
19724 && !row->full_width_p
19725 /* When drawing overlapping rows, only the glyph strings'
19726 foreground is drawn, which doesn't erase a cursor
19727 completely. */
19728 && !overlaps)
19729 {
19730 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19731 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19732 : (tail ? tail->x + tail->background_width : x));
19733
19734 int text_left = window_box_left (w, TEXT_AREA);
19735 x0 -= text_left;
19736 x1 -= text_left;
19737
19738 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19739 row->y, MATRIX_ROW_BOTTOM_Y (row));
19740 }
19741
19742 /* Value is the x-position up to which drawn, relative to AREA of W.
19743 This doesn't include parts drawn because of overhangs. */
19744 if (row->full_width_p)
19745 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19746 else
19747 x_reached -= window_box_left (w, area);
19748
19749 RELEASE_HDC (hdc, f);
19750
19751 return x_reached;
19752 }
19753
19754 /* Expand row matrix if too narrow. Don't expand if area
19755 is not present. */
19756
19757 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19758 { \
19759 if (!fonts_changed_p \
19760 && (it->glyph_row->glyphs[area] \
19761 < it->glyph_row->glyphs[area + 1])) \
19762 { \
19763 it->w->ncols_scale_factor++; \
19764 fonts_changed_p = 1; \
19765 } \
19766 }
19767
19768 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19769 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19770
19771 static INLINE void
19772 append_glyph (it)
19773 struct it *it;
19774 {
19775 struct glyph *glyph;
19776 enum glyph_row_area area = it->area;
19777
19778 xassert (it->glyph_row);
19779 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19780
19781 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19782 if (glyph < it->glyph_row->glyphs[area + 1])
19783 {
19784 glyph->charpos = CHARPOS (it->position);
19785 glyph->object = it->object;
19786 glyph->pixel_width = it->pixel_width;
19787 glyph->ascent = it->ascent;
19788 glyph->descent = it->descent;
19789 glyph->voffset = it->voffset;
19790 glyph->type = CHAR_GLYPH;
19791 glyph->multibyte_p = it->multibyte_p;
19792 glyph->left_box_line_p = it->start_of_box_run_p;
19793 glyph->right_box_line_p = it->end_of_box_run_p;
19794 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19795 || it->phys_descent > it->descent);
19796 glyph->padding_p = 0;
19797 glyph->glyph_not_available_p = it->glyph_not_available_p;
19798 glyph->face_id = it->face_id;
19799 glyph->u.ch = it->char_to_display;
19800 glyph->slice = null_glyph_slice;
19801 glyph->font_type = FONT_TYPE_UNKNOWN;
19802 ++it->glyph_row->used[area];
19803 }
19804 else
19805 IT_EXPAND_MATRIX_WIDTH (it, area);
19806 }
19807
19808 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19809 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19810
19811 static INLINE void
19812 append_composite_glyph (it)
19813 struct it *it;
19814 {
19815 struct glyph *glyph;
19816 enum glyph_row_area area = it->area;
19817
19818 xassert (it->glyph_row);
19819
19820 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19821 if (glyph < it->glyph_row->glyphs[area + 1])
19822 {
19823 glyph->charpos = CHARPOS (it->position);
19824 glyph->object = it->object;
19825 glyph->pixel_width = it->pixel_width;
19826 glyph->ascent = it->ascent;
19827 glyph->descent = it->descent;
19828 glyph->voffset = it->voffset;
19829 glyph->type = COMPOSITE_GLYPH;
19830 glyph->multibyte_p = it->multibyte_p;
19831 glyph->left_box_line_p = it->start_of_box_run_p;
19832 glyph->right_box_line_p = it->end_of_box_run_p;
19833 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19834 || it->phys_descent > it->descent);
19835 glyph->padding_p = 0;
19836 glyph->glyph_not_available_p = 0;
19837 glyph->face_id = it->face_id;
19838 glyph->u.cmp_id = it->cmp_id;
19839 glyph->slice = null_glyph_slice;
19840 glyph->font_type = FONT_TYPE_UNKNOWN;
19841 ++it->glyph_row->used[area];
19842 }
19843 else
19844 IT_EXPAND_MATRIX_WIDTH (it, area);
19845 }
19846
19847
19848 /* Change IT->ascent and IT->height according to the setting of
19849 IT->voffset. */
19850
19851 static INLINE void
19852 take_vertical_position_into_account (it)
19853 struct it *it;
19854 {
19855 if (it->voffset)
19856 {
19857 if (it->voffset < 0)
19858 /* Increase the ascent so that we can display the text higher
19859 in the line. */
19860 it->ascent -= it->voffset;
19861 else
19862 /* Increase the descent so that we can display the text lower
19863 in the line. */
19864 it->descent += it->voffset;
19865 }
19866 }
19867
19868
19869 /* Produce glyphs/get display metrics for the image IT is loaded with.
19870 See the description of struct display_iterator in dispextern.h for
19871 an overview of struct display_iterator. */
19872
19873 static void
19874 produce_image_glyph (it)
19875 struct it *it;
19876 {
19877 struct image *img;
19878 struct face *face;
19879 int glyph_ascent, crop;
19880 struct glyph_slice slice;
19881
19882 xassert (it->what == IT_IMAGE);
19883
19884 face = FACE_FROM_ID (it->f, it->face_id);
19885 xassert (face);
19886 /* Make sure X resources of the face is loaded. */
19887 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19888
19889 if (it->image_id < 0)
19890 {
19891 /* Fringe bitmap. */
19892 it->ascent = it->phys_ascent = 0;
19893 it->descent = it->phys_descent = 0;
19894 it->pixel_width = 0;
19895 it->nglyphs = 0;
19896 return;
19897 }
19898
19899 img = IMAGE_FROM_ID (it->f, it->image_id);
19900 xassert (img);
19901 /* Make sure X resources of the image is loaded. */
19902 prepare_image_for_display (it->f, img);
19903
19904 slice.x = slice.y = 0;
19905 slice.width = img->width;
19906 slice.height = img->height;
19907
19908 if (INTEGERP (it->slice.x))
19909 slice.x = XINT (it->slice.x);
19910 else if (FLOATP (it->slice.x))
19911 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19912
19913 if (INTEGERP (it->slice.y))
19914 slice.y = XINT (it->slice.y);
19915 else if (FLOATP (it->slice.y))
19916 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19917
19918 if (INTEGERP (it->slice.width))
19919 slice.width = XINT (it->slice.width);
19920 else if (FLOATP (it->slice.width))
19921 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19922
19923 if (INTEGERP (it->slice.height))
19924 slice.height = XINT (it->slice.height);
19925 else if (FLOATP (it->slice.height))
19926 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19927
19928 if (slice.x >= img->width)
19929 slice.x = img->width;
19930 if (slice.y >= img->height)
19931 slice.y = img->height;
19932 if (slice.x + slice.width >= img->width)
19933 slice.width = img->width - slice.x;
19934 if (slice.y + slice.height > img->height)
19935 slice.height = img->height - slice.y;
19936
19937 if (slice.width == 0 || slice.height == 0)
19938 return;
19939
19940 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19941
19942 it->descent = slice.height - glyph_ascent;
19943 if (slice.y == 0)
19944 it->descent += img->vmargin;
19945 if (slice.y + slice.height == img->height)
19946 it->descent += img->vmargin;
19947 it->phys_descent = it->descent;
19948
19949 it->pixel_width = slice.width;
19950 if (slice.x == 0)
19951 it->pixel_width += img->hmargin;
19952 if (slice.x + slice.width == img->width)
19953 it->pixel_width += img->hmargin;
19954
19955 /* It's quite possible for images to have an ascent greater than
19956 their height, so don't get confused in that case. */
19957 if (it->descent < 0)
19958 it->descent = 0;
19959
19960 #if 0 /* this breaks image tiling */
19961 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19962 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19963 if (face_ascent > it->ascent)
19964 it->ascent = it->phys_ascent = face_ascent;
19965 #endif
19966
19967 it->nglyphs = 1;
19968
19969 if (face->box != FACE_NO_BOX)
19970 {
19971 if (face->box_line_width > 0)
19972 {
19973 if (slice.y == 0)
19974 it->ascent += face->box_line_width;
19975 if (slice.y + slice.height == img->height)
19976 it->descent += face->box_line_width;
19977 }
19978
19979 if (it->start_of_box_run_p && slice.x == 0)
19980 it->pixel_width += abs (face->box_line_width);
19981 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19982 it->pixel_width += abs (face->box_line_width);
19983 }
19984
19985 take_vertical_position_into_account (it);
19986
19987 /* Automatically crop wide image glyphs at right edge so we can
19988 draw the cursor on same display row. */
19989 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
19990 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
19991 {
19992 it->pixel_width -= crop;
19993 slice.width -= crop;
19994 }
19995
19996 if (it->glyph_row)
19997 {
19998 struct glyph *glyph;
19999 enum glyph_row_area area = it->area;
20000
20001 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20002 if (glyph < it->glyph_row->glyphs[area + 1])
20003 {
20004 glyph->charpos = CHARPOS (it->position);
20005 glyph->object = it->object;
20006 glyph->pixel_width = it->pixel_width;
20007 glyph->ascent = glyph_ascent;
20008 glyph->descent = it->descent;
20009 glyph->voffset = it->voffset;
20010 glyph->type = IMAGE_GLYPH;
20011 glyph->multibyte_p = it->multibyte_p;
20012 glyph->left_box_line_p = it->start_of_box_run_p;
20013 glyph->right_box_line_p = it->end_of_box_run_p;
20014 glyph->overlaps_vertically_p = 0;
20015 glyph->padding_p = 0;
20016 glyph->glyph_not_available_p = 0;
20017 glyph->face_id = it->face_id;
20018 glyph->u.img_id = img->id;
20019 glyph->slice = slice;
20020 glyph->font_type = FONT_TYPE_UNKNOWN;
20021 ++it->glyph_row->used[area];
20022 }
20023 else
20024 IT_EXPAND_MATRIX_WIDTH (it, area);
20025 }
20026 }
20027
20028
20029 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20030 of the glyph, WIDTH and HEIGHT are the width and height of the
20031 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20032
20033 static void
20034 append_stretch_glyph (it, object, width, height, ascent)
20035 struct it *it;
20036 Lisp_Object object;
20037 int width, height;
20038 int ascent;
20039 {
20040 struct glyph *glyph;
20041 enum glyph_row_area area = it->area;
20042
20043 xassert (ascent >= 0 && ascent <= height);
20044
20045 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20046 if (glyph < it->glyph_row->glyphs[area + 1])
20047 {
20048 glyph->charpos = CHARPOS (it->position);
20049 glyph->object = object;
20050 glyph->pixel_width = width;
20051 glyph->ascent = ascent;
20052 glyph->descent = height - ascent;
20053 glyph->voffset = it->voffset;
20054 glyph->type = STRETCH_GLYPH;
20055 glyph->multibyte_p = it->multibyte_p;
20056 glyph->left_box_line_p = it->start_of_box_run_p;
20057 glyph->right_box_line_p = it->end_of_box_run_p;
20058 glyph->overlaps_vertically_p = 0;
20059 glyph->padding_p = 0;
20060 glyph->glyph_not_available_p = 0;
20061 glyph->face_id = it->face_id;
20062 glyph->u.stretch.ascent = ascent;
20063 glyph->u.stretch.height = height;
20064 glyph->slice = null_glyph_slice;
20065 glyph->font_type = FONT_TYPE_UNKNOWN;
20066 ++it->glyph_row->used[area];
20067 }
20068 else
20069 IT_EXPAND_MATRIX_WIDTH (it, area);
20070 }
20071
20072
20073 /* Produce a stretch glyph for iterator IT. IT->object is the value
20074 of the glyph property displayed. The value must be a list
20075 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20076 being recognized:
20077
20078 1. `:width WIDTH' specifies that the space should be WIDTH *
20079 canonical char width wide. WIDTH may be an integer or floating
20080 point number.
20081
20082 2. `:relative-width FACTOR' specifies that the width of the stretch
20083 should be computed from the width of the first character having the
20084 `glyph' property, and should be FACTOR times that width.
20085
20086 3. `:align-to HPOS' specifies that the space should be wide enough
20087 to reach HPOS, a value in canonical character units.
20088
20089 Exactly one of the above pairs must be present.
20090
20091 4. `:height HEIGHT' specifies that the height of the stretch produced
20092 should be HEIGHT, measured in canonical character units.
20093
20094 5. `:relative-height FACTOR' specifies that the height of the
20095 stretch should be FACTOR times the height of the characters having
20096 the glyph property.
20097
20098 Either none or exactly one of 4 or 5 must be present.
20099
20100 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20101 of the stretch should be used for the ascent of the stretch.
20102 ASCENT must be in the range 0 <= ASCENT <= 100. */
20103
20104 static void
20105 produce_stretch_glyph (it)
20106 struct it *it;
20107 {
20108 /* (space :width WIDTH :height HEIGHT ...) */
20109 Lisp_Object prop, plist;
20110 int width = 0, height = 0, align_to = -1;
20111 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20112 int ascent = 0;
20113 double tem;
20114 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20115 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20116
20117 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20118
20119 /* List should start with `space'. */
20120 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20121 plist = XCDR (it->object);
20122
20123 /* Compute the width of the stretch. */
20124 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20125 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20126 {
20127 /* Absolute width `:width WIDTH' specified and valid. */
20128 zero_width_ok_p = 1;
20129 width = (int)tem;
20130 }
20131 else if (prop = Fplist_get (plist, QCrelative_width),
20132 NUMVAL (prop) > 0)
20133 {
20134 /* Relative width `:relative-width FACTOR' specified and valid.
20135 Compute the width of the characters having the `glyph'
20136 property. */
20137 struct it it2;
20138 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20139
20140 it2 = *it;
20141 if (it->multibyte_p)
20142 {
20143 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20144 - IT_BYTEPOS (*it));
20145 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20146 }
20147 else
20148 it2.c = *p, it2.len = 1;
20149
20150 it2.glyph_row = NULL;
20151 it2.what = IT_CHARACTER;
20152 x_produce_glyphs (&it2);
20153 width = NUMVAL (prop) * it2.pixel_width;
20154 }
20155 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20156 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20157 {
20158 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20159 align_to = (align_to < 0
20160 ? 0
20161 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20162 else if (align_to < 0)
20163 align_to = window_box_left_offset (it->w, TEXT_AREA);
20164 width = max (0, (int)tem + align_to - it->current_x);
20165 zero_width_ok_p = 1;
20166 }
20167 else
20168 /* Nothing specified -> width defaults to canonical char width. */
20169 width = FRAME_COLUMN_WIDTH (it->f);
20170
20171 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20172 width = 1;
20173
20174 /* Compute height. */
20175 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20176 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20177 {
20178 height = (int)tem;
20179 zero_height_ok_p = 1;
20180 }
20181 else if (prop = Fplist_get (plist, QCrelative_height),
20182 NUMVAL (prop) > 0)
20183 height = FONT_HEIGHT (font) * NUMVAL (prop);
20184 else
20185 height = FONT_HEIGHT (font);
20186
20187 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20188 height = 1;
20189
20190 /* Compute percentage of height used for ascent. If
20191 `:ascent ASCENT' is present and valid, use that. Otherwise,
20192 derive the ascent from the font in use. */
20193 if (prop = Fplist_get (plist, QCascent),
20194 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20195 ascent = height * NUMVAL (prop) / 100.0;
20196 else if (!NILP (prop)
20197 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20198 ascent = min (max (0, (int)tem), height);
20199 else
20200 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20201
20202 if (width > 0 && !it->truncate_lines_p
20203 && it->current_x + width > it->last_visible_x)
20204 width = it->last_visible_x - it->current_x - 1;
20205
20206 if (width > 0 && height > 0 && it->glyph_row)
20207 {
20208 Lisp_Object object = it->stack[it->sp - 1].string;
20209 if (!STRINGP (object))
20210 object = it->w->buffer;
20211 append_stretch_glyph (it, object, width, height, ascent);
20212 }
20213
20214 it->pixel_width = width;
20215 it->ascent = it->phys_ascent = ascent;
20216 it->descent = it->phys_descent = height - it->ascent;
20217 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20218
20219 take_vertical_position_into_account (it);
20220 }
20221
20222 /* Get line-height and line-spacing property at point.
20223 If line-height has format (HEIGHT TOTAL), return TOTAL
20224 in TOTAL_HEIGHT. */
20225
20226 static Lisp_Object
20227 get_line_height_property (it, prop)
20228 struct it *it;
20229 Lisp_Object prop;
20230 {
20231 Lisp_Object position;
20232
20233 if (STRINGP (it->object))
20234 position = make_number (IT_STRING_CHARPOS (*it));
20235 else if (BUFFERP (it->object))
20236 position = make_number (IT_CHARPOS (*it));
20237 else
20238 return Qnil;
20239
20240 return Fget_char_property (position, prop, it->object);
20241 }
20242
20243 /* Calculate line-height and line-spacing properties.
20244 An integer value specifies explicit pixel value.
20245 A float value specifies relative value to current face height.
20246 A cons (float . face-name) specifies relative value to
20247 height of specified face font.
20248
20249 Returns height in pixels, or nil. */
20250
20251
20252 static Lisp_Object
20253 calc_line_height_property (it, val, font, boff, override)
20254 struct it *it;
20255 Lisp_Object val;
20256 XFontStruct *font;
20257 int boff, override;
20258 {
20259 Lisp_Object face_name = Qnil;
20260 int ascent, descent, height;
20261
20262 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20263 return val;
20264
20265 if (CONSP (val))
20266 {
20267 face_name = XCAR (val);
20268 val = XCDR (val);
20269 if (!NUMBERP (val))
20270 val = make_number (1);
20271 if (NILP (face_name))
20272 {
20273 height = it->ascent + it->descent;
20274 goto scale;
20275 }
20276 }
20277
20278 if (NILP (face_name))
20279 {
20280 font = FRAME_FONT (it->f);
20281 boff = FRAME_BASELINE_OFFSET (it->f);
20282 }
20283 else if (EQ (face_name, Qt))
20284 {
20285 override = 0;
20286 }
20287 else
20288 {
20289 int face_id;
20290 struct face *face;
20291 struct font_info *font_info;
20292
20293 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20294 if (face_id < 0)
20295 return make_number (-1);
20296
20297 face = FACE_FROM_ID (it->f, face_id);
20298 font = face->font;
20299 if (font == NULL)
20300 return make_number (-1);
20301
20302 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20303 boff = font_info->baseline_offset;
20304 if (font_info->vertical_centering)
20305 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20306 }
20307
20308 ascent = FONT_BASE (font) + boff;
20309 descent = FONT_DESCENT (font) - boff;
20310
20311 if (override)
20312 {
20313 it->override_ascent = ascent;
20314 it->override_descent = descent;
20315 it->override_boff = boff;
20316 }
20317
20318 height = ascent + descent;
20319
20320 scale:
20321 if (FLOATP (val))
20322 height = (int)(XFLOAT_DATA (val) * height);
20323 else if (INTEGERP (val))
20324 height *= XINT (val);
20325
20326 return make_number (height);
20327 }
20328
20329
20330 /* RIF:
20331 Produce glyphs/get display metrics for the display element IT is
20332 loaded with. See the description of struct it in dispextern.h
20333 for an overview of struct it. */
20334
20335 void
20336 x_produce_glyphs (it)
20337 struct it *it;
20338 {
20339 int extra_line_spacing = it->extra_line_spacing;
20340
20341 it->glyph_not_available_p = 0;
20342
20343 if (it->what == IT_CHARACTER)
20344 {
20345 XChar2b char2b;
20346 XFontStruct *font;
20347 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20348 XCharStruct *pcm;
20349 int font_not_found_p;
20350 struct font_info *font_info;
20351 int boff; /* baseline offset */
20352 /* We may change it->multibyte_p upon unibyte<->multibyte
20353 conversion. So, save the current value now and restore it
20354 later.
20355
20356 Note: It seems that we don't have to record multibyte_p in
20357 struct glyph because the character code itself tells if or
20358 not the character is multibyte. Thus, in the future, we must
20359 consider eliminating the field `multibyte_p' in the struct
20360 glyph. */
20361 int saved_multibyte_p = it->multibyte_p;
20362
20363 /* Maybe translate single-byte characters to multibyte, or the
20364 other way. */
20365 it->char_to_display = it->c;
20366 if (!ASCII_BYTE_P (it->c))
20367 {
20368 if (unibyte_display_via_language_environment
20369 && SINGLE_BYTE_CHAR_P (it->c)
20370 && (it->c >= 0240
20371 || !NILP (Vnonascii_translation_table)))
20372 {
20373 it->char_to_display = unibyte_char_to_multibyte (it->c);
20374 it->multibyte_p = 1;
20375 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20376 face = FACE_FROM_ID (it->f, it->face_id);
20377 }
20378 else if (!SINGLE_BYTE_CHAR_P (it->c)
20379 && !it->multibyte_p)
20380 {
20381 it->multibyte_p = 1;
20382 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20383 face = FACE_FROM_ID (it->f, it->face_id);
20384 }
20385 }
20386
20387 /* Get font to use. Encode IT->char_to_display. */
20388 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20389 &char2b, it->multibyte_p, 0);
20390 font = face->font;
20391
20392 /* When no suitable font found, use the default font. */
20393 font_not_found_p = font == NULL;
20394 if (font_not_found_p)
20395 {
20396 font = FRAME_FONT (it->f);
20397 boff = FRAME_BASELINE_OFFSET (it->f);
20398 font_info = NULL;
20399 }
20400 else
20401 {
20402 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20403 boff = font_info->baseline_offset;
20404 if (font_info->vertical_centering)
20405 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20406 }
20407
20408 if (it->char_to_display >= ' '
20409 && (!it->multibyte_p || it->char_to_display < 128))
20410 {
20411 /* Either unibyte or ASCII. */
20412 int stretched_p;
20413
20414 it->nglyphs = 1;
20415
20416 pcm = rif->per_char_metric (font, &char2b,
20417 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20418
20419 if (it->override_ascent >= 0)
20420 {
20421 it->ascent = it->override_ascent;
20422 it->descent = it->override_descent;
20423 boff = it->override_boff;
20424 }
20425 else
20426 {
20427 it->ascent = FONT_BASE (font) + boff;
20428 it->descent = FONT_DESCENT (font) - boff;
20429 }
20430
20431 if (pcm)
20432 {
20433 it->phys_ascent = pcm->ascent + boff;
20434 it->phys_descent = pcm->descent - boff;
20435 it->pixel_width = pcm->width;
20436 }
20437 else
20438 {
20439 it->glyph_not_available_p = 1;
20440 it->phys_ascent = it->ascent;
20441 it->phys_descent = it->descent;
20442 it->pixel_width = FONT_WIDTH (font);
20443 }
20444
20445 if (it->constrain_row_ascent_descent_p)
20446 {
20447 if (it->descent > it->max_descent)
20448 {
20449 it->ascent += it->descent - it->max_descent;
20450 it->descent = it->max_descent;
20451 }
20452 if (it->ascent > it->max_ascent)
20453 {
20454 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20455 it->ascent = it->max_ascent;
20456 }
20457 it->phys_ascent = min (it->phys_ascent, it->ascent);
20458 it->phys_descent = min (it->phys_descent, it->descent);
20459 extra_line_spacing = 0;
20460 }
20461
20462 /* If this is a space inside a region of text with
20463 `space-width' property, change its width. */
20464 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20465 if (stretched_p)
20466 it->pixel_width *= XFLOATINT (it->space_width);
20467
20468 /* If face has a box, add the box thickness to the character
20469 height. If character has a box line to the left and/or
20470 right, add the box line width to the character's width. */
20471 if (face->box != FACE_NO_BOX)
20472 {
20473 int thick = face->box_line_width;
20474
20475 if (thick > 0)
20476 {
20477 it->ascent += thick;
20478 it->descent += thick;
20479 }
20480 else
20481 thick = -thick;
20482
20483 if (it->start_of_box_run_p)
20484 it->pixel_width += thick;
20485 if (it->end_of_box_run_p)
20486 it->pixel_width += thick;
20487 }
20488
20489 /* If face has an overline, add the height of the overline
20490 (1 pixel) and a 1 pixel margin to the character height. */
20491 if (face->overline_p)
20492 it->ascent += overline_margin;
20493
20494 if (it->constrain_row_ascent_descent_p)
20495 {
20496 if (it->ascent > it->max_ascent)
20497 it->ascent = it->max_ascent;
20498 if (it->descent > it->max_descent)
20499 it->descent = it->max_descent;
20500 }
20501
20502 take_vertical_position_into_account (it);
20503
20504 /* If we have to actually produce glyphs, do it. */
20505 if (it->glyph_row)
20506 {
20507 if (stretched_p)
20508 {
20509 /* Translate a space with a `space-width' property
20510 into a stretch glyph. */
20511 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20512 / FONT_HEIGHT (font));
20513 append_stretch_glyph (it, it->object, it->pixel_width,
20514 it->ascent + it->descent, ascent);
20515 }
20516 else
20517 append_glyph (it);
20518
20519 /* If characters with lbearing or rbearing are displayed
20520 in this line, record that fact in a flag of the
20521 glyph row. This is used to optimize X output code. */
20522 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20523 it->glyph_row->contains_overlapping_glyphs_p = 1;
20524 }
20525 }
20526 else if (it->char_to_display == '\n')
20527 {
20528 /* A newline has no width but we need the height of the line.
20529 But if previous part of the line set a height, don't
20530 increase that height */
20531
20532 Lisp_Object height;
20533 Lisp_Object total_height = Qnil;
20534
20535 it->override_ascent = -1;
20536 it->pixel_width = 0;
20537 it->nglyphs = 0;
20538
20539 height = get_line_height_property(it, Qline_height);
20540 /* Split (line-height total-height) list */
20541 if (CONSP (height)
20542 && CONSP (XCDR (height))
20543 && NILP (XCDR (XCDR (height))))
20544 {
20545 total_height = XCAR (XCDR (height));
20546 height = XCAR (height);
20547 }
20548 height = calc_line_height_property(it, height, font, boff, 1);
20549
20550 if (it->override_ascent >= 0)
20551 {
20552 it->ascent = it->override_ascent;
20553 it->descent = it->override_descent;
20554 boff = it->override_boff;
20555 }
20556 else
20557 {
20558 it->ascent = FONT_BASE (font) + boff;
20559 it->descent = FONT_DESCENT (font) - boff;
20560 }
20561
20562 if (EQ (height, Qt))
20563 {
20564 if (it->descent > it->max_descent)
20565 {
20566 it->ascent += it->descent - it->max_descent;
20567 it->descent = it->max_descent;
20568 }
20569 if (it->ascent > it->max_ascent)
20570 {
20571 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20572 it->ascent = it->max_ascent;
20573 }
20574 it->phys_ascent = min (it->phys_ascent, it->ascent);
20575 it->phys_descent = min (it->phys_descent, it->descent);
20576 it->constrain_row_ascent_descent_p = 1;
20577 extra_line_spacing = 0;
20578 }
20579 else
20580 {
20581 Lisp_Object spacing;
20582
20583 it->phys_ascent = it->ascent;
20584 it->phys_descent = it->descent;
20585
20586 if ((it->max_ascent > 0 || it->max_descent > 0)
20587 && face->box != FACE_NO_BOX
20588 && face->box_line_width > 0)
20589 {
20590 it->ascent += face->box_line_width;
20591 it->descent += face->box_line_width;
20592 }
20593 if (!NILP (height)
20594 && XINT (height) > it->ascent + it->descent)
20595 it->ascent = XINT (height) - it->descent;
20596
20597 if (!NILP (total_height))
20598 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20599 else
20600 {
20601 spacing = get_line_height_property(it, Qline_spacing);
20602 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20603 }
20604 if (INTEGERP (spacing))
20605 {
20606 extra_line_spacing = XINT (spacing);
20607 if (!NILP (total_height))
20608 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20609 }
20610 }
20611 }
20612 else if (it->char_to_display == '\t')
20613 {
20614 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20615 int x = it->current_x + it->continuation_lines_width;
20616 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20617
20618 /* If the distance from the current position to the next tab
20619 stop is less than a space character width, use the
20620 tab stop after that. */
20621 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20622 next_tab_x += tab_width;
20623
20624 it->pixel_width = next_tab_x - x;
20625 it->nglyphs = 1;
20626 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20627 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20628
20629 if (it->glyph_row)
20630 {
20631 append_stretch_glyph (it, it->object, it->pixel_width,
20632 it->ascent + it->descent, it->ascent);
20633 }
20634 }
20635 else
20636 {
20637 /* A multi-byte character. Assume that the display width of the
20638 character is the width of the character multiplied by the
20639 width of the font. */
20640
20641 /* If we found a font, this font should give us the right
20642 metrics. If we didn't find a font, use the frame's
20643 default font and calculate the width of the character
20644 from the charset width; this is what old redisplay code
20645 did. */
20646
20647 pcm = rif->per_char_metric (font, &char2b,
20648 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20649
20650 if (font_not_found_p || !pcm)
20651 {
20652 int charset = CHAR_CHARSET (it->char_to_display);
20653
20654 it->glyph_not_available_p = 1;
20655 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20656 * CHARSET_WIDTH (charset));
20657 it->phys_ascent = FONT_BASE (font) + boff;
20658 it->phys_descent = FONT_DESCENT (font) - boff;
20659 }
20660 else
20661 {
20662 it->pixel_width = pcm->width;
20663 it->phys_ascent = pcm->ascent + boff;
20664 it->phys_descent = pcm->descent - boff;
20665 if (it->glyph_row
20666 && (pcm->lbearing < 0
20667 || pcm->rbearing > pcm->width))
20668 it->glyph_row->contains_overlapping_glyphs_p = 1;
20669 }
20670 it->nglyphs = 1;
20671 it->ascent = FONT_BASE (font) + boff;
20672 it->descent = FONT_DESCENT (font) - boff;
20673 if (face->box != FACE_NO_BOX)
20674 {
20675 int thick = face->box_line_width;
20676
20677 if (thick > 0)
20678 {
20679 it->ascent += thick;
20680 it->descent += thick;
20681 }
20682 else
20683 thick = - thick;
20684
20685 if (it->start_of_box_run_p)
20686 it->pixel_width += thick;
20687 if (it->end_of_box_run_p)
20688 it->pixel_width += thick;
20689 }
20690
20691 /* If face has an overline, add the height of the overline
20692 (1 pixel) and a 1 pixel margin to the character height. */
20693 if (face->overline_p)
20694 it->ascent += overline_margin;
20695
20696 take_vertical_position_into_account (it);
20697
20698 if (it->glyph_row)
20699 append_glyph (it);
20700 }
20701 it->multibyte_p = saved_multibyte_p;
20702 }
20703 else if (it->what == IT_COMPOSITION)
20704 {
20705 /* Note: A composition is represented as one glyph in the
20706 glyph matrix. There are no padding glyphs. */
20707 XChar2b char2b;
20708 XFontStruct *font;
20709 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20710 XCharStruct *pcm;
20711 int font_not_found_p;
20712 struct font_info *font_info;
20713 int boff; /* baseline offset */
20714 struct composition *cmp = composition_table[it->cmp_id];
20715
20716 /* Maybe translate single-byte characters to multibyte. */
20717 it->char_to_display = it->c;
20718 if (unibyte_display_via_language_environment
20719 && SINGLE_BYTE_CHAR_P (it->c)
20720 && (it->c >= 0240
20721 || (it->c >= 0200
20722 && !NILP (Vnonascii_translation_table))))
20723 {
20724 it->char_to_display = unibyte_char_to_multibyte (it->c);
20725 }
20726
20727 /* Get face and font to use. Encode IT->char_to_display. */
20728 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20729 face = FACE_FROM_ID (it->f, it->face_id);
20730 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20731 &char2b, it->multibyte_p, 0);
20732 font = face->font;
20733
20734 /* When no suitable font found, use the default font. */
20735 font_not_found_p = font == NULL;
20736 if (font_not_found_p)
20737 {
20738 font = FRAME_FONT (it->f);
20739 boff = FRAME_BASELINE_OFFSET (it->f);
20740 font_info = NULL;
20741 }
20742 else
20743 {
20744 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20745 boff = font_info->baseline_offset;
20746 if (font_info->vertical_centering)
20747 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20748 }
20749
20750 /* There are no padding glyphs, so there is only one glyph to
20751 produce for the composition. Important is that pixel_width,
20752 ascent and descent are the values of what is drawn by
20753 draw_glyphs (i.e. the values of the overall glyphs composed). */
20754 it->nglyphs = 1;
20755
20756 /* If we have not yet calculated pixel size data of glyphs of
20757 the composition for the current face font, calculate them
20758 now. Theoretically, we have to check all fonts for the
20759 glyphs, but that requires much time and memory space. So,
20760 here we check only the font of the first glyph. This leads
20761 to incorrect display very rarely, and C-l (recenter) can
20762 correct the display anyway. */
20763 if (cmp->font != (void *) font)
20764 {
20765 /* Ascent and descent of the font of the first character of
20766 this composition (adjusted by baseline offset). Ascent
20767 and descent of overall glyphs should not be less than
20768 them respectively. */
20769 int font_ascent = FONT_BASE (font) + boff;
20770 int font_descent = FONT_DESCENT (font) - boff;
20771 /* Bounding box of the overall glyphs. */
20772 int leftmost, rightmost, lowest, highest;
20773 int i, width, ascent, descent;
20774
20775 cmp->font = (void *) font;
20776
20777 /* Initialize the bounding box. */
20778 if (font_info
20779 && (pcm = rif->per_char_metric (font, &char2b,
20780 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20781 {
20782 width = pcm->width;
20783 ascent = pcm->ascent;
20784 descent = pcm->descent;
20785 }
20786 else
20787 {
20788 width = FONT_WIDTH (font);
20789 ascent = FONT_BASE (font);
20790 descent = FONT_DESCENT (font);
20791 }
20792
20793 rightmost = width;
20794 lowest = - descent + boff;
20795 highest = ascent + boff;
20796 leftmost = 0;
20797
20798 if (font_info
20799 && font_info->default_ascent
20800 && CHAR_TABLE_P (Vuse_default_ascent)
20801 && !NILP (Faref (Vuse_default_ascent,
20802 make_number (it->char_to_display))))
20803 highest = font_info->default_ascent + boff;
20804
20805 /* Draw the first glyph at the normal position. It may be
20806 shifted to right later if some other glyphs are drawn at
20807 the left. */
20808 cmp->offsets[0] = 0;
20809 cmp->offsets[1] = boff;
20810
20811 /* Set cmp->offsets for the remaining glyphs. */
20812 for (i = 1; i < cmp->glyph_len; i++)
20813 {
20814 int left, right, btm, top;
20815 int ch = COMPOSITION_GLYPH (cmp, i);
20816 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20817
20818 face = FACE_FROM_ID (it->f, face_id);
20819 get_char_face_and_encoding (it->f, ch, face->id,
20820 &char2b, it->multibyte_p, 0);
20821 font = face->font;
20822 if (font == NULL)
20823 {
20824 font = FRAME_FONT (it->f);
20825 boff = FRAME_BASELINE_OFFSET (it->f);
20826 font_info = NULL;
20827 }
20828 else
20829 {
20830 font_info
20831 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20832 boff = font_info->baseline_offset;
20833 if (font_info->vertical_centering)
20834 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20835 }
20836
20837 if (font_info
20838 && (pcm = rif->per_char_metric (font, &char2b,
20839 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20840 {
20841 width = pcm->width;
20842 ascent = pcm->ascent;
20843 descent = pcm->descent;
20844 }
20845 else
20846 {
20847 width = FONT_WIDTH (font);
20848 ascent = 1;
20849 descent = 0;
20850 }
20851
20852 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20853 {
20854 /* Relative composition with or without
20855 alternate chars. */
20856 left = (leftmost + rightmost - width) / 2;
20857 btm = - descent + boff;
20858 if (font_info && font_info->relative_compose
20859 && (! CHAR_TABLE_P (Vignore_relative_composition)
20860 || NILP (Faref (Vignore_relative_composition,
20861 make_number (ch)))))
20862 {
20863
20864 if (- descent >= font_info->relative_compose)
20865 /* One extra pixel between two glyphs. */
20866 btm = highest + 1;
20867 else if (ascent <= 0)
20868 /* One extra pixel between two glyphs. */
20869 btm = lowest - 1 - ascent - descent;
20870 }
20871 }
20872 else
20873 {
20874 /* A composition rule is specified by an integer
20875 value that encodes global and new reference
20876 points (GREF and NREF). GREF and NREF are
20877 specified by numbers as below:
20878
20879 0---1---2 -- ascent
20880 | |
20881 | |
20882 | |
20883 9--10--11 -- center
20884 | |
20885 ---3---4---5--- baseline
20886 | |
20887 6---7---8 -- descent
20888 */
20889 int rule = COMPOSITION_RULE (cmp, i);
20890 int gref, nref, grefx, grefy, nrefx, nrefy;
20891
20892 COMPOSITION_DECODE_RULE (rule, gref, nref);
20893 grefx = gref % 3, nrefx = nref % 3;
20894 grefy = gref / 3, nrefy = nref / 3;
20895
20896 left = (leftmost
20897 + grefx * (rightmost - leftmost) / 2
20898 - nrefx * width / 2);
20899 btm = ((grefy == 0 ? highest
20900 : grefy == 1 ? 0
20901 : grefy == 2 ? lowest
20902 : (highest + lowest) / 2)
20903 - (nrefy == 0 ? ascent + descent
20904 : nrefy == 1 ? descent - boff
20905 : nrefy == 2 ? 0
20906 : (ascent + descent) / 2));
20907 }
20908
20909 cmp->offsets[i * 2] = left;
20910 cmp->offsets[i * 2 + 1] = btm + descent;
20911
20912 /* Update the bounding box of the overall glyphs. */
20913 right = left + width;
20914 top = btm + descent + ascent;
20915 if (left < leftmost)
20916 leftmost = left;
20917 if (right > rightmost)
20918 rightmost = right;
20919 if (top > highest)
20920 highest = top;
20921 if (btm < lowest)
20922 lowest = btm;
20923 }
20924
20925 /* If there are glyphs whose x-offsets are negative,
20926 shift all glyphs to the right and make all x-offsets
20927 non-negative. */
20928 if (leftmost < 0)
20929 {
20930 for (i = 0; i < cmp->glyph_len; i++)
20931 cmp->offsets[i * 2] -= leftmost;
20932 rightmost -= leftmost;
20933 }
20934
20935 cmp->pixel_width = rightmost;
20936 cmp->ascent = highest;
20937 cmp->descent = - lowest;
20938 if (cmp->ascent < font_ascent)
20939 cmp->ascent = font_ascent;
20940 if (cmp->descent < font_descent)
20941 cmp->descent = font_descent;
20942 }
20943
20944 it->pixel_width = cmp->pixel_width;
20945 it->ascent = it->phys_ascent = cmp->ascent;
20946 it->descent = it->phys_descent = cmp->descent;
20947
20948 if (face->box != FACE_NO_BOX)
20949 {
20950 int thick = face->box_line_width;
20951
20952 if (thick > 0)
20953 {
20954 it->ascent += thick;
20955 it->descent += thick;
20956 }
20957 else
20958 thick = - thick;
20959
20960 if (it->start_of_box_run_p)
20961 it->pixel_width += thick;
20962 if (it->end_of_box_run_p)
20963 it->pixel_width += thick;
20964 }
20965
20966 /* If face has an overline, add the height of the overline
20967 (1 pixel) and a 1 pixel margin to the character height. */
20968 if (face->overline_p)
20969 it->ascent += overline_margin;
20970
20971 take_vertical_position_into_account (it);
20972
20973 if (it->glyph_row)
20974 append_composite_glyph (it);
20975 }
20976 else if (it->what == IT_IMAGE)
20977 produce_image_glyph (it);
20978 else if (it->what == IT_STRETCH)
20979 produce_stretch_glyph (it);
20980
20981 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20982 because this isn't true for images with `:ascent 100'. */
20983 xassert (it->ascent >= 0 && it->descent >= 0);
20984 if (it->area == TEXT_AREA)
20985 it->current_x += it->pixel_width;
20986
20987 if (extra_line_spacing > 0)
20988 {
20989 it->descent += extra_line_spacing;
20990 if (extra_line_spacing > it->max_extra_line_spacing)
20991 it->max_extra_line_spacing = extra_line_spacing;
20992 }
20993
20994 it->max_ascent = max (it->max_ascent, it->ascent);
20995 it->max_descent = max (it->max_descent, it->descent);
20996 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20997 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20998 }
20999
21000 /* EXPORT for RIF:
21001 Output LEN glyphs starting at START at the nominal cursor position.
21002 Advance the nominal cursor over the text. The global variable
21003 updated_window contains the window being updated, updated_row is
21004 the glyph row being updated, and updated_area is the area of that
21005 row being updated. */
21006
21007 void
21008 x_write_glyphs (start, len)
21009 struct glyph *start;
21010 int len;
21011 {
21012 int x, hpos;
21013
21014 xassert (updated_window && updated_row);
21015 BLOCK_INPUT;
21016
21017 /* Write glyphs. */
21018
21019 hpos = start - updated_row->glyphs[updated_area];
21020 x = draw_glyphs (updated_window, output_cursor.x,
21021 updated_row, updated_area,
21022 hpos, hpos + len,
21023 DRAW_NORMAL_TEXT, 0);
21024
21025 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21026 if (updated_area == TEXT_AREA
21027 && updated_window->phys_cursor_on_p
21028 && updated_window->phys_cursor.vpos == output_cursor.vpos
21029 && updated_window->phys_cursor.hpos >= hpos
21030 && updated_window->phys_cursor.hpos < hpos + len)
21031 updated_window->phys_cursor_on_p = 0;
21032
21033 UNBLOCK_INPUT;
21034
21035 /* Advance the output cursor. */
21036 output_cursor.hpos += len;
21037 output_cursor.x = x;
21038 }
21039
21040
21041 /* EXPORT for RIF:
21042 Insert LEN glyphs from START at the nominal cursor position. */
21043
21044 void
21045 x_insert_glyphs (start, len)
21046 struct glyph *start;
21047 int len;
21048 {
21049 struct frame *f;
21050 struct window *w;
21051 int line_height, shift_by_width, shifted_region_width;
21052 struct glyph_row *row;
21053 struct glyph *glyph;
21054 int frame_x, frame_y, hpos;
21055
21056 xassert (updated_window && updated_row);
21057 BLOCK_INPUT;
21058 w = updated_window;
21059 f = XFRAME (WINDOW_FRAME (w));
21060
21061 /* Get the height of the line we are in. */
21062 row = updated_row;
21063 line_height = row->height;
21064
21065 /* Get the width of the glyphs to insert. */
21066 shift_by_width = 0;
21067 for (glyph = start; glyph < start + len; ++glyph)
21068 shift_by_width += glyph->pixel_width;
21069
21070 /* Get the width of the region to shift right. */
21071 shifted_region_width = (window_box_width (w, updated_area)
21072 - output_cursor.x
21073 - shift_by_width);
21074
21075 /* Shift right. */
21076 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21077 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21078
21079 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21080 line_height, shift_by_width);
21081
21082 /* Write the glyphs. */
21083 hpos = start - row->glyphs[updated_area];
21084 draw_glyphs (w, output_cursor.x, row, updated_area,
21085 hpos, hpos + len,
21086 DRAW_NORMAL_TEXT, 0);
21087
21088 /* Advance the output cursor. */
21089 output_cursor.hpos += len;
21090 output_cursor.x += shift_by_width;
21091 UNBLOCK_INPUT;
21092 }
21093
21094
21095 /* EXPORT for RIF:
21096 Erase the current text line from the nominal cursor position
21097 (inclusive) to pixel column TO_X (exclusive). The idea is that
21098 everything from TO_X onward is already erased.
21099
21100 TO_X is a pixel position relative to updated_area of
21101 updated_window. TO_X == -1 means clear to the end of this area. */
21102
21103 void
21104 x_clear_end_of_line (to_x)
21105 int to_x;
21106 {
21107 struct frame *f;
21108 struct window *w = updated_window;
21109 int max_x, min_y, max_y;
21110 int from_x, from_y, to_y;
21111
21112 xassert (updated_window && updated_row);
21113 f = XFRAME (w->frame);
21114
21115 if (updated_row->full_width_p)
21116 max_x = WINDOW_TOTAL_WIDTH (w);
21117 else
21118 max_x = window_box_width (w, updated_area);
21119 max_y = window_text_bottom_y (w);
21120
21121 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21122 of window. For TO_X > 0, truncate to end of drawing area. */
21123 if (to_x == 0)
21124 return;
21125 else if (to_x < 0)
21126 to_x = max_x;
21127 else
21128 to_x = min (to_x, max_x);
21129
21130 to_y = min (max_y, output_cursor.y + updated_row->height);
21131
21132 /* Notice if the cursor will be cleared by this operation. */
21133 if (!updated_row->full_width_p)
21134 notice_overwritten_cursor (w, updated_area,
21135 output_cursor.x, -1,
21136 updated_row->y,
21137 MATRIX_ROW_BOTTOM_Y (updated_row));
21138
21139 from_x = output_cursor.x;
21140
21141 /* Translate to frame coordinates. */
21142 if (updated_row->full_width_p)
21143 {
21144 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21145 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21146 }
21147 else
21148 {
21149 int area_left = window_box_left (w, updated_area);
21150 from_x += area_left;
21151 to_x += area_left;
21152 }
21153
21154 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21155 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21156 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21157
21158 /* Prevent inadvertently clearing to end of the X window. */
21159 if (to_x > from_x && to_y > from_y)
21160 {
21161 BLOCK_INPUT;
21162 rif->clear_frame_area (f, from_x, from_y,
21163 to_x - from_x, to_y - from_y);
21164 UNBLOCK_INPUT;
21165 }
21166 }
21167
21168 #endif /* HAVE_WINDOW_SYSTEM */
21169
21170
21171 \f
21172 /***********************************************************************
21173 Cursor types
21174 ***********************************************************************/
21175
21176 /* Value is the internal representation of the specified cursor type
21177 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21178 of the bar cursor. */
21179
21180 static enum text_cursor_kinds
21181 get_specified_cursor_type (arg, width)
21182 Lisp_Object arg;
21183 int *width;
21184 {
21185 enum text_cursor_kinds type;
21186
21187 if (NILP (arg))
21188 return NO_CURSOR;
21189
21190 if (EQ (arg, Qbox))
21191 return FILLED_BOX_CURSOR;
21192
21193 if (EQ (arg, Qhollow))
21194 return HOLLOW_BOX_CURSOR;
21195
21196 if (EQ (arg, Qbar))
21197 {
21198 *width = 2;
21199 return BAR_CURSOR;
21200 }
21201
21202 if (CONSP (arg)
21203 && EQ (XCAR (arg), Qbar)
21204 && INTEGERP (XCDR (arg))
21205 && XINT (XCDR (arg)) >= 0)
21206 {
21207 *width = XINT (XCDR (arg));
21208 return BAR_CURSOR;
21209 }
21210
21211 if (EQ (arg, Qhbar))
21212 {
21213 *width = 2;
21214 return HBAR_CURSOR;
21215 }
21216
21217 if (CONSP (arg)
21218 && EQ (XCAR (arg), Qhbar)
21219 && INTEGERP (XCDR (arg))
21220 && XINT (XCDR (arg)) >= 0)
21221 {
21222 *width = XINT (XCDR (arg));
21223 return HBAR_CURSOR;
21224 }
21225
21226 /* Treat anything unknown as "hollow box cursor".
21227 It was bad to signal an error; people have trouble fixing
21228 .Xdefaults with Emacs, when it has something bad in it. */
21229 type = HOLLOW_BOX_CURSOR;
21230
21231 return type;
21232 }
21233
21234 /* Set the default cursor types for specified frame. */
21235 void
21236 set_frame_cursor_types (f, arg)
21237 struct frame *f;
21238 Lisp_Object arg;
21239 {
21240 int width;
21241 Lisp_Object tem;
21242
21243 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21244 FRAME_CURSOR_WIDTH (f) = width;
21245
21246 /* By default, set up the blink-off state depending on the on-state. */
21247
21248 tem = Fassoc (arg, Vblink_cursor_alist);
21249 if (!NILP (tem))
21250 {
21251 FRAME_BLINK_OFF_CURSOR (f)
21252 = get_specified_cursor_type (XCDR (tem), &width);
21253 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21254 }
21255 else
21256 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21257 }
21258
21259
21260 /* Return the cursor we want to be displayed in window W. Return
21261 width of bar/hbar cursor through WIDTH arg. Return with
21262 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21263 (i.e. if the `system caret' should track this cursor).
21264
21265 In a mini-buffer window, we want the cursor only to appear if we
21266 are reading input from this window. For the selected window, we
21267 want the cursor type given by the frame parameter or buffer local
21268 setting of cursor-type. If explicitly marked off, draw no cursor.
21269 In all other cases, we want a hollow box cursor. */
21270
21271 static enum text_cursor_kinds
21272 get_window_cursor_type (w, glyph, width, active_cursor)
21273 struct window *w;
21274 struct glyph *glyph;
21275 int *width;
21276 int *active_cursor;
21277 {
21278 struct frame *f = XFRAME (w->frame);
21279 struct buffer *b = XBUFFER (w->buffer);
21280 int cursor_type = DEFAULT_CURSOR;
21281 Lisp_Object alt_cursor;
21282 int non_selected = 0;
21283
21284 *active_cursor = 1;
21285
21286 /* Echo area */
21287 if (cursor_in_echo_area
21288 && FRAME_HAS_MINIBUF_P (f)
21289 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21290 {
21291 if (w == XWINDOW (echo_area_window))
21292 {
21293 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21294 {
21295 *width = FRAME_CURSOR_WIDTH (f);
21296 return FRAME_DESIRED_CURSOR (f);
21297 }
21298 else
21299 return get_specified_cursor_type (b->cursor_type, width);
21300 }
21301
21302 *active_cursor = 0;
21303 non_selected = 1;
21304 }
21305
21306 /* Detect a nonselected window or nonselected frame. */
21307 else if (w != XWINDOW (f->selected_window)
21308 #ifdef HAVE_WINDOW_SYSTEM
21309 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21310 #endif
21311 )
21312 {
21313 *active_cursor = 0;
21314
21315 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21316 return NO_CURSOR;
21317
21318 non_selected = 1;
21319 }
21320
21321 /* Never display a cursor in a window in which cursor-type is nil. */
21322 if (NILP (b->cursor_type))
21323 return NO_CURSOR;
21324
21325 /* Get the normal cursor type for this window. */
21326 if (EQ (b->cursor_type, Qt))
21327 {
21328 cursor_type = FRAME_DESIRED_CURSOR (f);
21329 *width = FRAME_CURSOR_WIDTH (f);
21330 }
21331 else
21332 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21333
21334 /* Use cursor-in-non-selected-windows instead
21335 for non-selected window or frame. */
21336 if (non_selected)
21337 {
21338 alt_cursor = b->cursor_in_non_selected_windows;
21339 if (!EQ (Qt, alt_cursor))
21340 return get_specified_cursor_type (alt_cursor, width);
21341 /* t means modify the normal cursor type. */
21342 if (cursor_type == FILLED_BOX_CURSOR)
21343 cursor_type = HOLLOW_BOX_CURSOR;
21344 else if (cursor_type == BAR_CURSOR && *width > 1)
21345 --*width;
21346 return cursor_type;
21347 }
21348
21349 /* Use normal cursor if not blinked off. */
21350 if (!w->cursor_off_p)
21351 {
21352 #ifdef HAVE_WINDOW_SYSTEM
21353 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21354 {
21355 if (cursor_type == FILLED_BOX_CURSOR)
21356 {
21357 /* Using a block cursor on large images can be very annoying.
21358 So use a hollow cursor for "large" images.
21359 If image is not transparent (no mask), also use hollow cursor. */
21360 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21361 if (img != NULL && IMAGEP (img->spec))
21362 {
21363 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21364 where N = size of default frame font size.
21365 This should cover most of the "tiny" icons people may use. */
21366 if (!img->mask
21367 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21368 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21369 cursor_type = HOLLOW_BOX_CURSOR;
21370 }
21371 }
21372 else if (cursor_type != NO_CURSOR)
21373 {
21374 /* Display current only supports BOX and HOLLOW cursors for images.
21375 So for now, unconditionally use a HOLLOW cursor when cursor is
21376 not a solid box cursor. */
21377 cursor_type = HOLLOW_BOX_CURSOR;
21378 }
21379 }
21380 #endif
21381 return cursor_type;
21382 }
21383
21384 /* Cursor is blinked off, so determine how to "toggle" it. */
21385
21386 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21387 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21388 return get_specified_cursor_type (XCDR (alt_cursor), width);
21389
21390 /* Then see if frame has specified a specific blink off cursor type. */
21391 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21392 {
21393 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21394 return FRAME_BLINK_OFF_CURSOR (f);
21395 }
21396
21397 #if 0
21398 /* Some people liked having a permanently visible blinking cursor,
21399 while others had very strong opinions against it. So it was
21400 decided to remove it. KFS 2003-09-03 */
21401
21402 /* Finally perform built-in cursor blinking:
21403 filled box <-> hollow box
21404 wide [h]bar <-> narrow [h]bar
21405 narrow [h]bar <-> no cursor
21406 other type <-> no cursor */
21407
21408 if (cursor_type == FILLED_BOX_CURSOR)
21409 return HOLLOW_BOX_CURSOR;
21410
21411 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21412 {
21413 *width = 1;
21414 return cursor_type;
21415 }
21416 #endif
21417
21418 return NO_CURSOR;
21419 }
21420
21421
21422 #ifdef HAVE_WINDOW_SYSTEM
21423
21424 /* Notice when the text cursor of window W has been completely
21425 overwritten by a drawing operation that outputs glyphs in AREA
21426 starting at X0 and ending at X1 in the line starting at Y0 and
21427 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21428 the rest of the line after X0 has been written. Y coordinates
21429 are window-relative. */
21430
21431 static void
21432 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21433 struct window *w;
21434 enum glyph_row_area area;
21435 int x0, y0, x1, y1;
21436 {
21437 int cx0, cx1, cy0, cy1;
21438 struct glyph_row *row;
21439
21440 if (!w->phys_cursor_on_p)
21441 return;
21442 if (area != TEXT_AREA)
21443 return;
21444
21445 if (w->phys_cursor.vpos < 0
21446 || w->phys_cursor.vpos >= w->current_matrix->nrows
21447 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21448 !(row->enabled_p && row->displays_text_p)))
21449 return;
21450
21451 if (row->cursor_in_fringe_p)
21452 {
21453 row->cursor_in_fringe_p = 0;
21454 draw_fringe_bitmap (w, row, 0);
21455 w->phys_cursor_on_p = 0;
21456 return;
21457 }
21458
21459 cx0 = w->phys_cursor.x;
21460 cx1 = cx0 + w->phys_cursor_width;
21461 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21462 return;
21463
21464 /* The cursor image will be completely removed from the
21465 screen if the output area intersects the cursor area in
21466 y-direction. When we draw in [y0 y1[, and some part of
21467 the cursor is at y < y0, that part must have been drawn
21468 before. When scrolling, the cursor is erased before
21469 actually scrolling, so we don't come here. When not
21470 scrolling, the rows above the old cursor row must have
21471 changed, and in this case these rows must have written
21472 over the cursor image.
21473
21474 Likewise if part of the cursor is below y1, with the
21475 exception of the cursor being in the first blank row at
21476 the buffer and window end because update_text_area
21477 doesn't draw that row. (Except when it does, but
21478 that's handled in update_text_area.) */
21479
21480 cy0 = w->phys_cursor.y;
21481 cy1 = cy0 + w->phys_cursor_height;
21482 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21483 return;
21484
21485 w->phys_cursor_on_p = 0;
21486 }
21487
21488 #endif /* HAVE_WINDOW_SYSTEM */
21489
21490 \f
21491 /************************************************************************
21492 Mouse Face
21493 ************************************************************************/
21494
21495 #ifdef HAVE_WINDOW_SYSTEM
21496
21497 /* EXPORT for RIF:
21498 Fix the display of area AREA of overlapping row ROW in window W
21499 with respect to the overlapping part OVERLAPS. */
21500
21501 void
21502 x_fix_overlapping_area (w, row, area, overlaps)
21503 struct window *w;
21504 struct glyph_row *row;
21505 enum glyph_row_area area;
21506 int overlaps;
21507 {
21508 int i, x;
21509
21510 BLOCK_INPUT;
21511
21512 x = 0;
21513 for (i = 0; i < row->used[area];)
21514 {
21515 if (row->glyphs[area][i].overlaps_vertically_p)
21516 {
21517 int start = i, start_x = x;
21518
21519 do
21520 {
21521 x += row->glyphs[area][i].pixel_width;
21522 ++i;
21523 }
21524 while (i < row->used[area]
21525 && row->glyphs[area][i].overlaps_vertically_p);
21526
21527 draw_glyphs (w, start_x, row, area,
21528 start, i,
21529 DRAW_NORMAL_TEXT, overlaps);
21530 }
21531 else
21532 {
21533 x += row->glyphs[area][i].pixel_width;
21534 ++i;
21535 }
21536 }
21537
21538 UNBLOCK_INPUT;
21539 }
21540
21541
21542 /* EXPORT:
21543 Draw the cursor glyph of window W in glyph row ROW. See the
21544 comment of draw_glyphs for the meaning of HL. */
21545
21546 void
21547 draw_phys_cursor_glyph (w, row, hl)
21548 struct window *w;
21549 struct glyph_row *row;
21550 enum draw_glyphs_face hl;
21551 {
21552 /* If cursor hpos is out of bounds, don't draw garbage. This can
21553 happen in mini-buffer windows when switching between echo area
21554 glyphs and mini-buffer. */
21555 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21556 {
21557 int on_p = w->phys_cursor_on_p;
21558 int x1;
21559 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21560 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21561 hl, 0);
21562 w->phys_cursor_on_p = on_p;
21563
21564 if (hl == DRAW_CURSOR)
21565 w->phys_cursor_width = x1 - w->phys_cursor.x;
21566 /* When we erase the cursor, and ROW is overlapped by other
21567 rows, make sure that these overlapping parts of other rows
21568 are redrawn. */
21569 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21570 {
21571 w->phys_cursor_width = x1 - w->phys_cursor.x;
21572
21573 if (row > w->current_matrix->rows
21574 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21575 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21576 OVERLAPS_ERASED_CURSOR);
21577
21578 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21579 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21580 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21581 OVERLAPS_ERASED_CURSOR);
21582 }
21583 }
21584 }
21585
21586
21587 /* EXPORT:
21588 Erase the image of a cursor of window W from the screen. */
21589
21590 void
21591 erase_phys_cursor (w)
21592 struct window *w;
21593 {
21594 struct frame *f = XFRAME (w->frame);
21595 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21596 int hpos = w->phys_cursor.hpos;
21597 int vpos = w->phys_cursor.vpos;
21598 int mouse_face_here_p = 0;
21599 struct glyph_matrix *active_glyphs = w->current_matrix;
21600 struct glyph_row *cursor_row;
21601 struct glyph *cursor_glyph;
21602 enum draw_glyphs_face hl;
21603
21604 /* No cursor displayed or row invalidated => nothing to do on the
21605 screen. */
21606 if (w->phys_cursor_type == NO_CURSOR)
21607 goto mark_cursor_off;
21608
21609 /* VPOS >= active_glyphs->nrows means that window has been resized.
21610 Don't bother to erase the cursor. */
21611 if (vpos >= active_glyphs->nrows)
21612 goto mark_cursor_off;
21613
21614 /* If row containing cursor is marked invalid, there is nothing we
21615 can do. */
21616 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21617 if (!cursor_row->enabled_p)
21618 goto mark_cursor_off;
21619
21620 /* If line spacing is > 0, old cursor may only be partially visible in
21621 window after split-window. So adjust visible height. */
21622 cursor_row->visible_height = min (cursor_row->visible_height,
21623 window_text_bottom_y (w) - cursor_row->y);
21624
21625 /* If row is completely invisible, don't attempt to delete a cursor which
21626 isn't there. This can happen if cursor is at top of a window, and
21627 we switch to a buffer with a header line in that window. */
21628 if (cursor_row->visible_height <= 0)
21629 goto mark_cursor_off;
21630
21631 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21632 if (cursor_row->cursor_in_fringe_p)
21633 {
21634 cursor_row->cursor_in_fringe_p = 0;
21635 draw_fringe_bitmap (w, cursor_row, 0);
21636 goto mark_cursor_off;
21637 }
21638
21639 /* This can happen when the new row is shorter than the old one.
21640 In this case, either draw_glyphs or clear_end_of_line
21641 should have cleared the cursor. Note that we wouldn't be
21642 able to erase the cursor in this case because we don't have a
21643 cursor glyph at hand. */
21644 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21645 goto mark_cursor_off;
21646
21647 /* If the cursor is in the mouse face area, redisplay that when
21648 we clear the cursor. */
21649 if (! NILP (dpyinfo->mouse_face_window)
21650 && w == XWINDOW (dpyinfo->mouse_face_window)
21651 && (vpos > dpyinfo->mouse_face_beg_row
21652 || (vpos == dpyinfo->mouse_face_beg_row
21653 && hpos >= dpyinfo->mouse_face_beg_col))
21654 && (vpos < dpyinfo->mouse_face_end_row
21655 || (vpos == dpyinfo->mouse_face_end_row
21656 && hpos < dpyinfo->mouse_face_end_col))
21657 /* Don't redraw the cursor's spot in mouse face if it is at the
21658 end of a line (on a newline). The cursor appears there, but
21659 mouse highlighting does not. */
21660 && cursor_row->used[TEXT_AREA] > hpos)
21661 mouse_face_here_p = 1;
21662
21663 /* Maybe clear the display under the cursor. */
21664 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21665 {
21666 int x, y, left_x;
21667 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21668 int width;
21669
21670 cursor_glyph = get_phys_cursor_glyph (w);
21671 if (cursor_glyph == NULL)
21672 goto mark_cursor_off;
21673
21674 width = cursor_glyph->pixel_width;
21675 left_x = window_box_left_offset (w, TEXT_AREA);
21676 x = w->phys_cursor.x;
21677 if (x < left_x)
21678 width -= left_x - x;
21679 width = min (width, window_box_width (w, TEXT_AREA) - x);
21680 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21681 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21682
21683 if (width > 0)
21684 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21685 }
21686
21687 /* Erase the cursor by redrawing the character underneath it. */
21688 if (mouse_face_here_p)
21689 hl = DRAW_MOUSE_FACE;
21690 else
21691 hl = DRAW_NORMAL_TEXT;
21692 draw_phys_cursor_glyph (w, cursor_row, hl);
21693
21694 mark_cursor_off:
21695 w->phys_cursor_on_p = 0;
21696 w->phys_cursor_type = NO_CURSOR;
21697 }
21698
21699
21700 /* EXPORT:
21701 Display or clear cursor of window W. If ON is zero, clear the
21702 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21703 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21704
21705 void
21706 display_and_set_cursor (w, on, hpos, vpos, x, y)
21707 struct window *w;
21708 int on, hpos, vpos, x, y;
21709 {
21710 struct frame *f = XFRAME (w->frame);
21711 int new_cursor_type;
21712 int new_cursor_width;
21713 int active_cursor;
21714 struct glyph_row *glyph_row;
21715 struct glyph *glyph;
21716
21717 /* This is pointless on invisible frames, and dangerous on garbaged
21718 windows and frames; in the latter case, the frame or window may
21719 be in the midst of changing its size, and x and y may be off the
21720 window. */
21721 if (! FRAME_VISIBLE_P (f)
21722 || FRAME_GARBAGED_P (f)
21723 || vpos >= w->current_matrix->nrows
21724 || hpos >= w->current_matrix->matrix_w)
21725 return;
21726
21727 /* If cursor is off and we want it off, return quickly. */
21728 if (!on && !w->phys_cursor_on_p)
21729 return;
21730
21731 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21732 /* If cursor row is not enabled, we don't really know where to
21733 display the cursor. */
21734 if (!glyph_row->enabled_p)
21735 {
21736 w->phys_cursor_on_p = 0;
21737 return;
21738 }
21739
21740 glyph = NULL;
21741 if (!glyph_row->exact_window_width_line_p
21742 || hpos < glyph_row->used[TEXT_AREA])
21743 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21744
21745 xassert (interrupt_input_blocked);
21746
21747 /* Set new_cursor_type to the cursor we want to be displayed. */
21748 new_cursor_type = get_window_cursor_type (w, glyph,
21749 &new_cursor_width, &active_cursor);
21750
21751 /* If cursor is currently being shown and we don't want it to be or
21752 it is in the wrong place, or the cursor type is not what we want,
21753 erase it. */
21754 if (w->phys_cursor_on_p
21755 && (!on
21756 || w->phys_cursor.x != x
21757 || w->phys_cursor.y != y
21758 || new_cursor_type != w->phys_cursor_type
21759 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21760 && new_cursor_width != w->phys_cursor_width)))
21761 erase_phys_cursor (w);
21762
21763 /* Don't check phys_cursor_on_p here because that flag is only set
21764 to zero in some cases where we know that the cursor has been
21765 completely erased, to avoid the extra work of erasing the cursor
21766 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21767 still not be visible, or it has only been partly erased. */
21768 if (on)
21769 {
21770 w->phys_cursor_ascent = glyph_row->ascent;
21771 w->phys_cursor_height = glyph_row->height;
21772
21773 /* Set phys_cursor_.* before x_draw_.* is called because some
21774 of them may need the information. */
21775 w->phys_cursor.x = x;
21776 w->phys_cursor.y = glyph_row->y;
21777 w->phys_cursor.hpos = hpos;
21778 w->phys_cursor.vpos = vpos;
21779 }
21780
21781 rif->draw_window_cursor (w, glyph_row, x, y,
21782 new_cursor_type, new_cursor_width,
21783 on, active_cursor);
21784 }
21785
21786
21787 /* Switch the display of W's cursor on or off, according to the value
21788 of ON. */
21789
21790 static void
21791 update_window_cursor (w, on)
21792 struct window *w;
21793 int on;
21794 {
21795 /* Don't update cursor in windows whose frame is in the process
21796 of being deleted. */
21797 if (w->current_matrix)
21798 {
21799 BLOCK_INPUT;
21800 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21801 w->phys_cursor.x, w->phys_cursor.y);
21802 UNBLOCK_INPUT;
21803 }
21804 }
21805
21806
21807 /* Call update_window_cursor with parameter ON_P on all leaf windows
21808 in the window tree rooted at W. */
21809
21810 static void
21811 update_cursor_in_window_tree (w, on_p)
21812 struct window *w;
21813 int on_p;
21814 {
21815 while (w)
21816 {
21817 if (!NILP (w->hchild))
21818 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21819 else if (!NILP (w->vchild))
21820 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21821 else
21822 update_window_cursor (w, on_p);
21823
21824 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21825 }
21826 }
21827
21828
21829 /* EXPORT:
21830 Display the cursor on window W, or clear it, according to ON_P.
21831 Don't change the cursor's position. */
21832
21833 void
21834 x_update_cursor (f, on_p)
21835 struct frame *f;
21836 int on_p;
21837 {
21838 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21839 }
21840
21841
21842 /* EXPORT:
21843 Clear the cursor of window W to background color, and mark the
21844 cursor as not shown. This is used when the text where the cursor
21845 is is about to be rewritten. */
21846
21847 void
21848 x_clear_cursor (w)
21849 struct window *w;
21850 {
21851 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21852 update_window_cursor (w, 0);
21853 }
21854
21855
21856 /* EXPORT:
21857 Display the active region described by mouse_face_* according to DRAW. */
21858
21859 void
21860 show_mouse_face (dpyinfo, draw)
21861 Display_Info *dpyinfo;
21862 enum draw_glyphs_face draw;
21863 {
21864 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21865 struct frame *f = XFRAME (WINDOW_FRAME (w));
21866
21867 if (/* If window is in the process of being destroyed, don't bother
21868 to do anything. */
21869 w->current_matrix != NULL
21870 /* Don't update mouse highlight if hidden */
21871 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21872 /* Recognize when we are called to operate on rows that don't exist
21873 anymore. This can happen when a window is split. */
21874 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21875 {
21876 int phys_cursor_on_p = w->phys_cursor_on_p;
21877 struct glyph_row *row, *first, *last;
21878
21879 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21880 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21881
21882 for (row = first; row <= last && row->enabled_p; ++row)
21883 {
21884 int start_hpos, end_hpos, start_x;
21885
21886 /* For all but the first row, the highlight starts at column 0. */
21887 if (row == first)
21888 {
21889 start_hpos = dpyinfo->mouse_face_beg_col;
21890 start_x = dpyinfo->mouse_face_beg_x;
21891 }
21892 else
21893 {
21894 start_hpos = 0;
21895 start_x = 0;
21896 }
21897
21898 if (row == last)
21899 end_hpos = dpyinfo->mouse_face_end_col;
21900 else
21901 {
21902 end_hpos = row->used[TEXT_AREA];
21903 if (draw == DRAW_NORMAL_TEXT)
21904 row->fill_line_p = 1; /* Clear to end of line */
21905 }
21906
21907 if (end_hpos > start_hpos)
21908 {
21909 draw_glyphs (w, start_x, row, TEXT_AREA,
21910 start_hpos, end_hpos,
21911 draw, 0);
21912
21913 row->mouse_face_p
21914 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21915 }
21916 }
21917
21918 /* When we've written over the cursor, arrange for it to
21919 be displayed again. */
21920 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21921 {
21922 BLOCK_INPUT;
21923 display_and_set_cursor (w, 1,
21924 w->phys_cursor.hpos, w->phys_cursor.vpos,
21925 w->phys_cursor.x, w->phys_cursor.y);
21926 UNBLOCK_INPUT;
21927 }
21928 }
21929
21930 /* Change the mouse cursor. */
21931 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
21932 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21933 else if (draw == DRAW_MOUSE_FACE)
21934 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21935 else
21936 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21937 }
21938
21939 /* EXPORT:
21940 Clear out the mouse-highlighted active region.
21941 Redraw it un-highlighted first. Value is non-zero if mouse
21942 face was actually drawn unhighlighted. */
21943
21944 int
21945 clear_mouse_face (dpyinfo)
21946 Display_Info *dpyinfo;
21947 {
21948 int cleared = 0;
21949
21950 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21951 {
21952 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21953 cleared = 1;
21954 }
21955
21956 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21957 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21958 dpyinfo->mouse_face_window = Qnil;
21959 dpyinfo->mouse_face_overlay = Qnil;
21960 return cleared;
21961 }
21962
21963
21964 /* EXPORT:
21965 Non-zero if physical cursor of window W is within mouse face. */
21966
21967 int
21968 cursor_in_mouse_face_p (w)
21969 struct window *w;
21970 {
21971 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21972 int in_mouse_face = 0;
21973
21974 if (WINDOWP (dpyinfo->mouse_face_window)
21975 && XWINDOW (dpyinfo->mouse_face_window) == w)
21976 {
21977 int hpos = w->phys_cursor.hpos;
21978 int vpos = w->phys_cursor.vpos;
21979
21980 if (vpos >= dpyinfo->mouse_face_beg_row
21981 && vpos <= dpyinfo->mouse_face_end_row
21982 && (vpos > dpyinfo->mouse_face_beg_row
21983 || hpos >= dpyinfo->mouse_face_beg_col)
21984 && (vpos < dpyinfo->mouse_face_end_row
21985 || hpos < dpyinfo->mouse_face_end_col
21986 || dpyinfo->mouse_face_past_end))
21987 in_mouse_face = 1;
21988 }
21989
21990 return in_mouse_face;
21991 }
21992
21993
21994
21995 \f
21996 /* Find the glyph matrix position of buffer position CHARPOS in window
21997 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21998 current glyphs must be up to date. If CHARPOS is above window
21999 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22000 of last line in W. In the row containing CHARPOS, stop before glyphs
22001 having STOP as object. */
22002
22003 #if 1 /* This is a version of fast_find_position that's more correct
22004 in the presence of hscrolling, for example. I didn't install
22005 it right away because the problem fixed is minor, it failed
22006 in 20.x as well, and I think it's too risky to install
22007 so near the release of 21.1. 2001-09-25 gerd. */
22008
22009 static int
22010 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22011 struct window *w;
22012 int charpos;
22013 int *hpos, *vpos, *x, *y;
22014 Lisp_Object stop;
22015 {
22016 struct glyph_row *row, *first;
22017 struct glyph *glyph, *end;
22018 int past_end = 0;
22019
22020 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22021 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22022 {
22023 *x = first->x;
22024 *y = first->y;
22025 *hpos = 0;
22026 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22027 return 1;
22028 }
22029
22030 row = row_containing_pos (w, charpos, first, NULL, 0);
22031 if (row == NULL)
22032 {
22033 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22034 past_end = 1;
22035 }
22036
22037 /* If whole rows or last part of a row came from a display overlay,
22038 row_containing_pos will skip over such rows because their end pos
22039 equals the start pos of the overlay or interval.
22040
22041 Move back if we have a STOP object and previous row's
22042 end glyph came from STOP. */
22043 if (!NILP (stop))
22044 {
22045 struct glyph_row *prev;
22046 while ((prev = row - 1, prev >= first)
22047 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22048 && prev->used[TEXT_AREA] > 0)
22049 {
22050 struct glyph *beg = prev->glyphs[TEXT_AREA];
22051 glyph = beg + prev->used[TEXT_AREA];
22052 while (--glyph >= beg
22053 && INTEGERP (glyph->object));
22054 if (glyph < beg
22055 || !EQ (stop, glyph->object))
22056 break;
22057 row = prev;
22058 }
22059 }
22060
22061 *x = row->x;
22062 *y = row->y;
22063 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22064
22065 glyph = row->glyphs[TEXT_AREA];
22066 end = glyph + row->used[TEXT_AREA];
22067
22068 /* Skip over glyphs not having an object at the start of the row.
22069 These are special glyphs like truncation marks on terminal
22070 frames. */
22071 if (row->displays_text_p)
22072 while (glyph < end
22073 && INTEGERP (glyph->object)
22074 && !EQ (stop, glyph->object)
22075 && glyph->charpos < 0)
22076 {
22077 *x += glyph->pixel_width;
22078 ++glyph;
22079 }
22080
22081 while (glyph < end
22082 && !INTEGERP (glyph->object)
22083 && !EQ (stop, glyph->object)
22084 && (!BUFFERP (glyph->object)
22085 || glyph->charpos < charpos))
22086 {
22087 *x += glyph->pixel_width;
22088 ++glyph;
22089 }
22090
22091 *hpos = glyph - row->glyphs[TEXT_AREA];
22092 return !past_end;
22093 }
22094
22095 #else /* not 1 */
22096
22097 static int
22098 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22099 struct window *w;
22100 int pos;
22101 int *hpos, *vpos, *x, *y;
22102 Lisp_Object stop;
22103 {
22104 int i;
22105 int lastcol;
22106 int maybe_next_line_p = 0;
22107 int line_start_position;
22108 int yb = window_text_bottom_y (w);
22109 struct glyph_row *row, *best_row;
22110 int row_vpos, best_row_vpos;
22111 int current_x;
22112
22113 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22114 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22115
22116 while (row->y < yb)
22117 {
22118 if (row->used[TEXT_AREA])
22119 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22120 else
22121 line_start_position = 0;
22122
22123 if (line_start_position > pos)
22124 break;
22125 /* If the position sought is the end of the buffer,
22126 don't include the blank lines at the bottom of the window. */
22127 else if (line_start_position == pos
22128 && pos == BUF_ZV (XBUFFER (w->buffer)))
22129 {
22130 maybe_next_line_p = 1;
22131 break;
22132 }
22133 else if (line_start_position > 0)
22134 {
22135 best_row = row;
22136 best_row_vpos = row_vpos;
22137 }
22138
22139 if (row->y + row->height >= yb)
22140 break;
22141
22142 ++row;
22143 ++row_vpos;
22144 }
22145
22146 /* Find the right column within BEST_ROW. */
22147 lastcol = 0;
22148 current_x = best_row->x;
22149 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22150 {
22151 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22152 int charpos = glyph->charpos;
22153
22154 if (BUFFERP (glyph->object))
22155 {
22156 if (charpos == pos)
22157 {
22158 *hpos = i;
22159 *vpos = best_row_vpos;
22160 *x = current_x;
22161 *y = best_row->y;
22162 return 1;
22163 }
22164 else if (charpos > pos)
22165 break;
22166 }
22167 else if (EQ (glyph->object, stop))
22168 break;
22169
22170 if (charpos > 0)
22171 lastcol = i;
22172 current_x += glyph->pixel_width;
22173 }
22174
22175 /* If we're looking for the end of the buffer,
22176 and we didn't find it in the line we scanned,
22177 use the start of the following line. */
22178 if (maybe_next_line_p)
22179 {
22180 ++best_row;
22181 ++best_row_vpos;
22182 lastcol = 0;
22183 current_x = best_row->x;
22184 }
22185
22186 *vpos = best_row_vpos;
22187 *hpos = lastcol + 1;
22188 *x = current_x;
22189 *y = best_row->y;
22190 return 0;
22191 }
22192
22193 #endif /* not 1 */
22194
22195
22196 /* Find the position of the glyph for position POS in OBJECT in
22197 window W's current matrix, and return in *X, *Y the pixel
22198 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22199
22200 RIGHT_P non-zero means return the position of the right edge of the
22201 glyph, RIGHT_P zero means return the left edge position.
22202
22203 If no glyph for POS exists in the matrix, return the position of
22204 the glyph with the next smaller position that is in the matrix, if
22205 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22206 exists in the matrix, return the position of the glyph with the
22207 next larger position in OBJECT.
22208
22209 Value is non-zero if a glyph was found. */
22210
22211 static int
22212 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22213 struct window *w;
22214 int pos;
22215 Lisp_Object object;
22216 int *hpos, *vpos, *x, *y;
22217 int right_p;
22218 {
22219 int yb = window_text_bottom_y (w);
22220 struct glyph_row *r;
22221 struct glyph *best_glyph = NULL;
22222 struct glyph_row *best_row = NULL;
22223 int best_x = 0;
22224
22225 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22226 r->enabled_p && r->y < yb;
22227 ++r)
22228 {
22229 struct glyph *g = r->glyphs[TEXT_AREA];
22230 struct glyph *e = g + r->used[TEXT_AREA];
22231 int gx;
22232
22233 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22234 if (EQ (g->object, object))
22235 {
22236 if (g->charpos == pos)
22237 {
22238 best_glyph = g;
22239 best_x = gx;
22240 best_row = r;
22241 goto found;
22242 }
22243 else if (best_glyph == NULL
22244 || ((abs (g->charpos - pos)
22245 < abs (best_glyph->charpos - pos))
22246 && (right_p
22247 ? g->charpos < pos
22248 : g->charpos > pos)))
22249 {
22250 best_glyph = g;
22251 best_x = gx;
22252 best_row = r;
22253 }
22254 }
22255 }
22256
22257 found:
22258
22259 if (best_glyph)
22260 {
22261 *x = best_x;
22262 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22263
22264 if (right_p)
22265 {
22266 *x += best_glyph->pixel_width;
22267 ++*hpos;
22268 }
22269
22270 *y = best_row->y;
22271 *vpos = best_row - w->current_matrix->rows;
22272 }
22273
22274 return best_glyph != NULL;
22275 }
22276
22277
22278 /* See if position X, Y is within a hot-spot of an image. */
22279
22280 static int
22281 on_hot_spot_p (hot_spot, x, y)
22282 Lisp_Object hot_spot;
22283 int x, y;
22284 {
22285 if (!CONSP (hot_spot))
22286 return 0;
22287
22288 if (EQ (XCAR (hot_spot), Qrect))
22289 {
22290 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22291 Lisp_Object rect = XCDR (hot_spot);
22292 Lisp_Object tem;
22293 if (!CONSP (rect))
22294 return 0;
22295 if (!CONSP (XCAR (rect)))
22296 return 0;
22297 if (!CONSP (XCDR (rect)))
22298 return 0;
22299 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22300 return 0;
22301 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22302 return 0;
22303 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22304 return 0;
22305 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22306 return 0;
22307 return 1;
22308 }
22309 else if (EQ (XCAR (hot_spot), Qcircle))
22310 {
22311 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22312 Lisp_Object circ = XCDR (hot_spot);
22313 Lisp_Object lr, lx0, ly0;
22314 if (CONSP (circ)
22315 && CONSP (XCAR (circ))
22316 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22317 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22318 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22319 {
22320 double r = XFLOATINT (lr);
22321 double dx = XINT (lx0) - x;
22322 double dy = XINT (ly0) - y;
22323 return (dx * dx + dy * dy <= r * r);
22324 }
22325 }
22326 else if (EQ (XCAR (hot_spot), Qpoly))
22327 {
22328 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22329 if (VECTORP (XCDR (hot_spot)))
22330 {
22331 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22332 Lisp_Object *poly = v->contents;
22333 int n = v->size;
22334 int i;
22335 int inside = 0;
22336 Lisp_Object lx, ly;
22337 int x0, y0;
22338
22339 /* Need an even number of coordinates, and at least 3 edges. */
22340 if (n < 6 || n & 1)
22341 return 0;
22342
22343 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22344 If count is odd, we are inside polygon. Pixels on edges
22345 may or may not be included depending on actual geometry of the
22346 polygon. */
22347 if ((lx = poly[n-2], !INTEGERP (lx))
22348 || (ly = poly[n-1], !INTEGERP (lx)))
22349 return 0;
22350 x0 = XINT (lx), y0 = XINT (ly);
22351 for (i = 0; i < n; i += 2)
22352 {
22353 int x1 = x0, y1 = y0;
22354 if ((lx = poly[i], !INTEGERP (lx))
22355 || (ly = poly[i+1], !INTEGERP (ly)))
22356 return 0;
22357 x0 = XINT (lx), y0 = XINT (ly);
22358
22359 /* Does this segment cross the X line? */
22360 if (x0 >= x)
22361 {
22362 if (x1 >= x)
22363 continue;
22364 }
22365 else if (x1 < x)
22366 continue;
22367 if (y > y0 && y > y1)
22368 continue;
22369 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22370 inside = !inside;
22371 }
22372 return inside;
22373 }
22374 }
22375 /* If we don't understand the format, pretend we're not in the hot-spot. */
22376 return 0;
22377 }
22378
22379 Lisp_Object
22380 find_hot_spot (map, x, y)
22381 Lisp_Object map;
22382 int x, y;
22383 {
22384 while (CONSP (map))
22385 {
22386 if (CONSP (XCAR (map))
22387 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22388 return XCAR (map);
22389 map = XCDR (map);
22390 }
22391
22392 return Qnil;
22393 }
22394
22395 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22396 3, 3, 0,
22397 doc: /* Lookup in image map MAP coordinates X and Y.
22398 An image map is an alist where each element has the format (AREA ID PLIST).
22399 An AREA is specified as either a rectangle, a circle, or a polygon:
22400 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22401 pixel coordinates of the upper left and bottom right corners.
22402 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22403 and the radius of the circle; r may be a float or integer.
22404 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22405 vector describes one corner in the polygon.
22406 Returns the alist element for the first matching AREA in MAP. */)
22407 (map, x, y)
22408 Lisp_Object map;
22409 Lisp_Object x, y;
22410 {
22411 if (NILP (map))
22412 return Qnil;
22413
22414 CHECK_NUMBER (x);
22415 CHECK_NUMBER (y);
22416
22417 return find_hot_spot (map, XINT (x), XINT (y));
22418 }
22419
22420
22421 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22422 static void
22423 define_frame_cursor1 (f, cursor, pointer)
22424 struct frame *f;
22425 Cursor cursor;
22426 Lisp_Object pointer;
22427 {
22428 /* Do not change cursor shape while dragging mouse. */
22429 if (!NILP (do_mouse_tracking))
22430 return;
22431
22432 if (!NILP (pointer))
22433 {
22434 if (EQ (pointer, Qarrow))
22435 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22436 else if (EQ (pointer, Qhand))
22437 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22438 else if (EQ (pointer, Qtext))
22439 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22440 else if (EQ (pointer, intern ("hdrag")))
22441 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22442 #ifdef HAVE_X_WINDOWS
22443 else if (EQ (pointer, intern ("vdrag")))
22444 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22445 #endif
22446 else if (EQ (pointer, intern ("hourglass")))
22447 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22448 else if (EQ (pointer, Qmodeline))
22449 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22450 else
22451 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22452 }
22453
22454 if (cursor != No_Cursor)
22455 rif->define_frame_cursor (f, cursor);
22456 }
22457
22458 /* Take proper action when mouse has moved to the mode or header line
22459 or marginal area AREA of window W, x-position X and y-position Y.
22460 X is relative to the start of the text display area of W, so the
22461 width of bitmap areas and scroll bars must be subtracted to get a
22462 position relative to the start of the mode line. */
22463
22464 static void
22465 note_mode_line_or_margin_highlight (window, x, y, area)
22466 Lisp_Object window;
22467 int x, y;
22468 enum window_part area;
22469 {
22470 struct window *w = XWINDOW (window);
22471 struct frame *f = XFRAME (w->frame);
22472 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22473 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22474 Lisp_Object pointer = Qnil;
22475 int charpos, dx, dy, width, height;
22476 Lisp_Object string, object = Qnil;
22477 Lisp_Object pos, help;
22478
22479 Lisp_Object mouse_face;
22480 int original_x_pixel = x;
22481 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22482 struct glyph_row *row;
22483
22484 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22485 {
22486 int x0;
22487 struct glyph *end;
22488
22489 string = mode_line_string (w, area, &x, &y, &charpos,
22490 &object, &dx, &dy, &width, &height);
22491
22492 row = (area == ON_MODE_LINE
22493 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22494 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22495
22496 /* Find glyph */
22497 if (row->mode_line_p && row->enabled_p)
22498 {
22499 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22500 end = glyph + row->used[TEXT_AREA];
22501
22502 for (x0 = original_x_pixel;
22503 glyph < end && x0 >= glyph->pixel_width;
22504 ++glyph)
22505 x0 -= glyph->pixel_width;
22506
22507 if (glyph >= end)
22508 glyph = NULL;
22509 }
22510 }
22511 else
22512 {
22513 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22514 string = marginal_area_string (w, area, &x, &y, &charpos,
22515 &object, &dx, &dy, &width, &height);
22516 }
22517
22518 help = Qnil;
22519
22520 if (IMAGEP (object))
22521 {
22522 Lisp_Object image_map, hotspot;
22523 if ((image_map = Fplist_get (XCDR (object), QCmap),
22524 !NILP (image_map))
22525 && (hotspot = find_hot_spot (image_map, dx, dy),
22526 CONSP (hotspot))
22527 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22528 {
22529 Lisp_Object area_id, plist;
22530
22531 area_id = XCAR (hotspot);
22532 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22533 If so, we could look for mouse-enter, mouse-leave
22534 properties in PLIST (and do something...). */
22535 hotspot = XCDR (hotspot);
22536 if (CONSP (hotspot)
22537 && (plist = XCAR (hotspot), CONSP (plist)))
22538 {
22539 pointer = Fplist_get (plist, Qpointer);
22540 if (NILP (pointer))
22541 pointer = Qhand;
22542 help = Fplist_get (plist, Qhelp_echo);
22543 if (!NILP (help))
22544 {
22545 help_echo_string = help;
22546 /* Is this correct? ++kfs */
22547 XSETWINDOW (help_echo_window, w);
22548 help_echo_object = w->buffer;
22549 help_echo_pos = charpos;
22550 }
22551 }
22552 }
22553 if (NILP (pointer))
22554 pointer = Fplist_get (XCDR (object), QCpointer);
22555 }
22556
22557 if (STRINGP (string))
22558 {
22559 pos = make_number (charpos);
22560 /* If we're on a string with `help-echo' text property, arrange
22561 for the help to be displayed. This is done by setting the
22562 global variable help_echo_string to the help string. */
22563 if (NILP (help))
22564 {
22565 help = Fget_text_property (pos, Qhelp_echo, string);
22566 if (!NILP (help))
22567 {
22568 help_echo_string = help;
22569 XSETWINDOW (help_echo_window, w);
22570 help_echo_object = string;
22571 help_echo_pos = charpos;
22572 }
22573 }
22574
22575 if (NILP (pointer))
22576 pointer = Fget_text_property (pos, Qpointer, string);
22577
22578 /* Change the mouse pointer according to what is under X/Y. */
22579 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22580 {
22581 Lisp_Object map;
22582 map = Fget_text_property (pos, Qlocal_map, string);
22583 if (!KEYMAPP (map))
22584 map = Fget_text_property (pos, Qkeymap, string);
22585 if (!KEYMAPP (map))
22586 cursor = dpyinfo->vertical_scroll_bar_cursor;
22587 }
22588
22589 /* Change the mouse face according to what is under X/Y. */
22590 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22591 if (!NILP (mouse_face)
22592 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22593 && glyph)
22594 {
22595 Lisp_Object b, e;
22596
22597 struct glyph * tmp_glyph;
22598
22599 int gpos;
22600 int gseq_length;
22601 int total_pixel_width;
22602 int ignore;
22603
22604 int vpos, hpos;
22605
22606 b = Fprevious_single_property_change (make_number (charpos + 1),
22607 Qmouse_face, string, Qnil);
22608 if (NILP (b))
22609 b = make_number (0);
22610
22611 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22612 if (NILP (e))
22613 e = make_number (SCHARS (string));
22614
22615 /* Calculate the position(glyph position: GPOS) of GLYPH in
22616 displayed string. GPOS is different from CHARPOS.
22617
22618 CHARPOS is the position of glyph in internal string
22619 object. A mode line string format has structures which
22620 is converted to a flatten by emacs lisp interpreter.
22621 The internal string is an element of the structures.
22622 The displayed string is the flatten string. */
22623 gpos = 0;
22624 if (glyph > row_start_glyph)
22625 {
22626 tmp_glyph = glyph - 1;
22627 while (tmp_glyph >= row_start_glyph
22628 && tmp_glyph->charpos >= XINT (b)
22629 && EQ (tmp_glyph->object, glyph->object))
22630 {
22631 tmp_glyph--;
22632 gpos++;
22633 }
22634 }
22635
22636 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22637 displayed string holding GLYPH.
22638
22639 GSEQ_LENGTH is different from SCHARS (STRING).
22640 SCHARS (STRING) returns the length of the internal string. */
22641 for (tmp_glyph = glyph, gseq_length = gpos;
22642 tmp_glyph->charpos < XINT (e);
22643 tmp_glyph++, gseq_length++)
22644 {
22645 if (!EQ (tmp_glyph->object, glyph->object))
22646 break;
22647 }
22648
22649 total_pixel_width = 0;
22650 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22651 total_pixel_width += tmp_glyph->pixel_width;
22652
22653 /* Pre calculation of re-rendering position */
22654 vpos = (x - gpos);
22655 hpos = (area == ON_MODE_LINE
22656 ? (w->current_matrix)->nrows - 1
22657 : 0);
22658
22659 /* If the re-rendering position is included in the last
22660 re-rendering area, we should do nothing. */
22661 if ( EQ (window, dpyinfo->mouse_face_window)
22662 && dpyinfo->mouse_face_beg_col <= vpos
22663 && vpos < dpyinfo->mouse_face_end_col
22664 && dpyinfo->mouse_face_beg_row == hpos )
22665 return;
22666
22667 if (clear_mouse_face (dpyinfo))
22668 cursor = No_Cursor;
22669
22670 dpyinfo->mouse_face_beg_col = vpos;
22671 dpyinfo->mouse_face_beg_row = hpos;
22672
22673 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22674 dpyinfo->mouse_face_beg_y = 0;
22675
22676 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22677 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22678
22679 dpyinfo->mouse_face_end_x = 0;
22680 dpyinfo->mouse_face_end_y = 0;
22681
22682 dpyinfo->mouse_face_past_end = 0;
22683 dpyinfo->mouse_face_window = window;
22684
22685 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22686 charpos,
22687 0, 0, 0, &ignore,
22688 glyph->face_id, 1);
22689 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22690
22691 if (NILP (pointer))
22692 pointer = Qhand;
22693 }
22694 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22695 clear_mouse_face (dpyinfo);
22696 }
22697 define_frame_cursor1 (f, cursor, pointer);
22698 }
22699
22700
22701 /* EXPORT:
22702 Take proper action when the mouse has moved to position X, Y on
22703 frame F as regards highlighting characters that have mouse-face
22704 properties. Also de-highlighting chars where the mouse was before.
22705 X and Y can be negative or out of range. */
22706
22707 void
22708 note_mouse_highlight (f, x, y)
22709 struct frame *f;
22710 int x, y;
22711 {
22712 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22713 enum window_part part;
22714 Lisp_Object window;
22715 struct window *w;
22716 Cursor cursor = No_Cursor;
22717 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22718 struct buffer *b;
22719
22720 /* When a menu is active, don't highlight because this looks odd. */
22721 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22722 if (popup_activated ())
22723 return;
22724 #endif
22725
22726 if (NILP (Vmouse_highlight)
22727 || !f->glyphs_initialized_p)
22728 return;
22729
22730 dpyinfo->mouse_face_mouse_x = x;
22731 dpyinfo->mouse_face_mouse_y = y;
22732 dpyinfo->mouse_face_mouse_frame = f;
22733
22734 if (dpyinfo->mouse_face_defer)
22735 return;
22736
22737 if (gc_in_progress)
22738 {
22739 dpyinfo->mouse_face_deferred_gc = 1;
22740 return;
22741 }
22742
22743 /* Which window is that in? */
22744 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22745
22746 /* If we were displaying active text in another window, clear that.
22747 Also clear if we move out of text area in same window. */
22748 if (! EQ (window, dpyinfo->mouse_face_window)
22749 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22750 && !NILP (dpyinfo->mouse_face_window)))
22751 clear_mouse_face (dpyinfo);
22752
22753 /* Not on a window -> return. */
22754 if (!WINDOWP (window))
22755 return;
22756
22757 /* Reset help_echo_string. It will get recomputed below. */
22758 help_echo_string = Qnil;
22759
22760 /* Convert to window-relative pixel coordinates. */
22761 w = XWINDOW (window);
22762 frame_to_window_pixel_xy (w, &x, &y);
22763
22764 /* Handle tool-bar window differently since it doesn't display a
22765 buffer. */
22766 if (EQ (window, f->tool_bar_window))
22767 {
22768 note_tool_bar_highlight (f, x, y);
22769 return;
22770 }
22771
22772 /* Mouse is on the mode, header line or margin? */
22773 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22774 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22775 {
22776 note_mode_line_or_margin_highlight (window, x, y, part);
22777 return;
22778 }
22779
22780 if (part == ON_VERTICAL_BORDER)
22781 {
22782 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22783 help_echo_string = build_string ("drag-mouse-1: resize");
22784 }
22785 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22786 || part == ON_SCROLL_BAR)
22787 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22788 else
22789 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22790
22791 /* Are we in a window whose display is up to date?
22792 And verify the buffer's text has not changed. */
22793 b = XBUFFER (w->buffer);
22794 if (part == ON_TEXT
22795 && EQ (w->window_end_valid, w->buffer)
22796 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22797 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22798 {
22799 int hpos, vpos, pos, i, dx, dy, area;
22800 struct glyph *glyph;
22801 Lisp_Object object;
22802 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22803 Lisp_Object *overlay_vec = NULL;
22804 int noverlays;
22805 struct buffer *obuf;
22806 int obegv, ozv, same_region;
22807
22808 /* Find the glyph under X/Y. */
22809 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22810
22811 /* Look for :pointer property on image. */
22812 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22813 {
22814 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22815 if (img != NULL && IMAGEP (img->spec))
22816 {
22817 Lisp_Object image_map, hotspot;
22818 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22819 !NILP (image_map))
22820 && (hotspot = find_hot_spot (image_map,
22821 glyph->slice.x + dx,
22822 glyph->slice.y + dy),
22823 CONSP (hotspot))
22824 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22825 {
22826 Lisp_Object area_id, plist;
22827
22828 area_id = XCAR (hotspot);
22829 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22830 If so, we could look for mouse-enter, mouse-leave
22831 properties in PLIST (and do something...). */
22832 hotspot = XCDR (hotspot);
22833 if (CONSP (hotspot)
22834 && (plist = XCAR (hotspot), CONSP (plist)))
22835 {
22836 pointer = Fplist_get (plist, Qpointer);
22837 if (NILP (pointer))
22838 pointer = Qhand;
22839 help_echo_string = Fplist_get (plist, Qhelp_echo);
22840 if (!NILP (help_echo_string))
22841 {
22842 help_echo_window = window;
22843 help_echo_object = glyph->object;
22844 help_echo_pos = glyph->charpos;
22845 }
22846 }
22847 }
22848 if (NILP (pointer))
22849 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22850 }
22851 }
22852
22853 /* Clear mouse face if X/Y not over text. */
22854 if (glyph == NULL
22855 || area != TEXT_AREA
22856 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22857 {
22858 if (clear_mouse_face (dpyinfo))
22859 cursor = No_Cursor;
22860 if (NILP (pointer))
22861 {
22862 if (area != TEXT_AREA)
22863 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22864 else
22865 pointer = Vvoid_text_area_pointer;
22866 }
22867 goto set_cursor;
22868 }
22869
22870 pos = glyph->charpos;
22871 object = glyph->object;
22872 if (!STRINGP (object) && !BUFFERP (object))
22873 goto set_cursor;
22874
22875 /* If we get an out-of-range value, return now; avoid an error. */
22876 if (BUFFERP (object) && pos > BUF_Z (b))
22877 goto set_cursor;
22878
22879 /* Make the window's buffer temporarily current for
22880 overlays_at and compute_char_face. */
22881 obuf = current_buffer;
22882 current_buffer = b;
22883 obegv = BEGV;
22884 ozv = ZV;
22885 BEGV = BEG;
22886 ZV = Z;
22887
22888 /* Is this char mouse-active or does it have help-echo? */
22889 position = make_number (pos);
22890
22891 if (BUFFERP (object))
22892 {
22893 /* Put all the overlays we want in a vector in overlay_vec. */
22894 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22895 /* Sort overlays into increasing priority order. */
22896 noverlays = sort_overlays (overlay_vec, noverlays, w);
22897 }
22898 else
22899 noverlays = 0;
22900
22901 same_region = (EQ (window, dpyinfo->mouse_face_window)
22902 && vpos >= dpyinfo->mouse_face_beg_row
22903 && vpos <= dpyinfo->mouse_face_end_row
22904 && (vpos > dpyinfo->mouse_face_beg_row
22905 || hpos >= dpyinfo->mouse_face_beg_col)
22906 && (vpos < dpyinfo->mouse_face_end_row
22907 || hpos < dpyinfo->mouse_face_end_col
22908 || dpyinfo->mouse_face_past_end));
22909
22910 if (same_region)
22911 cursor = No_Cursor;
22912
22913 /* Check mouse-face highlighting. */
22914 if (! same_region
22915 /* If there exists an overlay with mouse-face overlapping
22916 the one we are currently highlighting, we have to
22917 check if we enter the overlapping overlay, and then
22918 highlight only that. */
22919 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22920 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22921 {
22922 /* Find the highest priority overlay that has a mouse-face
22923 property. */
22924 overlay = Qnil;
22925 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22926 {
22927 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22928 if (!NILP (mouse_face))
22929 overlay = overlay_vec[i];
22930 }
22931
22932 /* If we're actually highlighting the same overlay as
22933 before, there's no need to do that again. */
22934 if (!NILP (overlay)
22935 && EQ (overlay, dpyinfo->mouse_face_overlay))
22936 goto check_help_echo;
22937
22938 dpyinfo->mouse_face_overlay = overlay;
22939
22940 /* Clear the display of the old active region, if any. */
22941 if (clear_mouse_face (dpyinfo))
22942 cursor = No_Cursor;
22943
22944 /* If no overlay applies, get a text property. */
22945 if (NILP (overlay))
22946 mouse_face = Fget_text_property (position, Qmouse_face, object);
22947
22948 /* Handle the overlay case. */
22949 if (!NILP (overlay))
22950 {
22951 /* Find the range of text around this char that
22952 should be active. */
22953 Lisp_Object before, after;
22954 int ignore;
22955
22956 before = Foverlay_start (overlay);
22957 after = Foverlay_end (overlay);
22958 /* Record this as the current active region. */
22959 fast_find_position (w, XFASTINT (before),
22960 &dpyinfo->mouse_face_beg_col,
22961 &dpyinfo->mouse_face_beg_row,
22962 &dpyinfo->mouse_face_beg_x,
22963 &dpyinfo->mouse_face_beg_y, Qnil);
22964
22965 dpyinfo->mouse_face_past_end
22966 = !fast_find_position (w, XFASTINT (after),
22967 &dpyinfo->mouse_face_end_col,
22968 &dpyinfo->mouse_face_end_row,
22969 &dpyinfo->mouse_face_end_x,
22970 &dpyinfo->mouse_face_end_y, Qnil);
22971 dpyinfo->mouse_face_window = window;
22972
22973 dpyinfo->mouse_face_face_id
22974 = face_at_buffer_position (w, pos, 0, 0,
22975 &ignore, pos + 1,
22976 !dpyinfo->mouse_face_hidden);
22977
22978 /* Display it as active. */
22979 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22980 cursor = No_Cursor;
22981 }
22982 /* Handle the text property case. */
22983 else if (!NILP (mouse_face) && BUFFERP (object))
22984 {
22985 /* Find the range of text around this char that
22986 should be active. */
22987 Lisp_Object before, after, beginning, end;
22988 int ignore;
22989
22990 beginning = Fmarker_position (w->start);
22991 end = make_number (BUF_Z (XBUFFER (object))
22992 - XFASTINT (w->window_end_pos));
22993 before
22994 = Fprevious_single_property_change (make_number (pos + 1),
22995 Qmouse_face,
22996 object, beginning);
22997 after
22998 = Fnext_single_property_change (position, Qmouse_face,
22999 object, end);
23000
23001 /* Record this as the current active region. */
23002 fast_find_position (w, XFASTINT (before),
23003 &dpyinfo->mouse_face_beg_col,
23004 &dpyinfo->mouse_face_beg_row,
23005 &dpyinfo->mouse_face_beg_x,
23006 &dpyinfo->mouse_face_beg_y, Qnil);
23007 dpyinfo->mouse_face_past_end
23008 = !fast_find_position (w, XFASTINT (after),
23009 &dpyinfo->mouse_face_end_col,
23010 &dpyinfo->mouse_face_end_row,
23011 &dpyinfo->mouse_face_end_x,
23012 &dpyinfo->mouse_face_end_y, Qnil);
23013 dpyinfo->mouse_face_window = window;
23014
23015 if (BUFFERP (object))
23016 dpyinfo->mouse_face_face_id
23017 = face_at_buffer_position (w, pos, 0, 0,
23018 &ignore, pos + 1,
23019 !dpyinfo->mouse_face_hidden);
23020
23021 /* Display it as active. */
23022 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23023 cursor = No_Cursor;
23024 }
23025 else if (!NILP (mouse_face) && STRINGP (object))
23026 {
23027 Lisp_Object b, e;
23028 int ignore;
23029
23030 b = Fprevious_single_property_change (make_number (pos + 1),
23031 Qmouse_face,
23032 object, Qnil);
23033 e = Fnext_single_property_change (position, Qmouse_face,
23034 object, Qnil);
23035 if (NILP (b))
23036 b = make_number (0);
23037 if (NILP (e))
23038 e = make_number (SCHARS (object) - 1);
23039
23040 fast_find_string_pos (w, XINT (b), object,
23041 &dpyinfo->mouse_face_beg_col,
23042 &dpyinfo->mouse_face_beg_row,
23043 &dpyinfo->mouse_face_beg_x,
23044 &dpyinfo->mouse_face_beg_y, 0);
23045 fast_find_string_pos (w, XINT (e), object,
23046 &dpyinfo->mouse_face_end_col,
23047 &dpyinfo->mouse_face_end_row,
23048 &dpyinfo->mouse_face_end_x,
23049 &dpyinfo->mouse_face_end_y, 1);
23050 dpyinfo->mouse_face_past_end = 0;
23051 dpyinfo->mouse_face_window = window;
23052 dpyinfo->mouse_face_face_id
23053 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23054 glyph->face_id, 1);
23055 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23056 cursor = No_Cursor;
23057 }
23058 else if (STRINGP (object) && NILP (mouse_face))
23059 {
23060 /* A string which doesn't have mouse-face, but
23061 the text ``under'' it might have. */
23062 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23063 int start = MATRIX_ROW_START_CHARPOS (r);
23064
23065 pos = string_buffer_position (w, object, start);
23066 if (pos > 0)
23067 mouse_face = get_char_property_and_overlay (make_number (pos),
23068 Qmouse_face,
23069 w->buffer,
23070 &overlay);
23071 if (!NILP (mouse_face) && !NILP (overlay))
23072 {
23073 Lisp_Object before = Foverlay_start (overlay);
23074 Lisp_Object after = Foverlay_end (overlay);
23075 int ignore;
23076
23077 /* Note that we might not be able to find position
23078 BEFORE in the glyph matrix if the overlay is
23079 entirely covered by a `display' property. In
23080 this case, we overshoot. So let's stop in
23081 the glyph matrix before glyphs for OBJECT. */
23082 fast_find_position (w, XFASTINT (before),
23083 &dpyinfo->mouse_face_beg_col,
23084 &dpyinfo->mouse_face_beg_row,
23085 &dpyinfo->mouse_face_beg_x,
23086 &dpyinfo->mouse_face_beg_y,
23087 object);
23088
23089 dpyinfo->mouse_face_past_end
23090 = !fast_find_position (w, XFASTINT (after),
23091 &dpyinfo->mouse_face_end_col,
23092 &dpyinfo->mouse_face_end_row,
23093 &dpyinfo->mouse_face_end_x,
23094 &dpyinfo->mouse_face_end_y,
23095 Qnil);
23096 dpyinfo->mouse_face_window = window;
23097 dpyinfo->mouse_face_face_id
23098 = face_at_buffer_position (w, pos, 0, 0,
23099 &ignore, pos + 1,
23100 !dpyinfo->mouse_face_hidden);
23101
23102 /* Display it as active. */
23103 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23104 cursor = No_Cursor;
23105 }
23106 }
23107 }
23108
23109 check_help_echo:
23110
23111 /* Look for a `help-echo' property. */
23112 if (NILP (help_echo_string)) {
23113 Lisp_Object help, overlay;
23114
23115 /* Check overlays first. */
23116 help = overlay = Qnil;
23117 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23118 {
23119 overlay = overlay_vec[i];
23120 help = Foverlay_get (overlay, Qhelp_echo);
23121 }
23122
23123 if (!NILP (help))
23124 {
23125 help_echo_string = help;
23126 help_echo_window = window;
23127 help_echo_object = overlay;
23128 help_echo_pos = pos;
23129 }
23130 else
23131 {
23132 Lisp_Object object = glyph->object;
23133 int charpos = glyph->charpos;
23134
23135 /* Try text properties. */
23136 if (STRINGP (object)
23137 && charpos >= 0
23138 && charpos < SCHARS (object))
23139 {
23140 help = Fget_text_property (make_number (charpos),
23141 Qhelp_echo, object);
23142 if (NILP (help))
23143 {
23144 /* If the string itself doesn't specify a help-echo,
23145 see if the buffer text ``under'' it does. */
23146 struct glyph_row *r
23147 = MATRIX_ROW (w->current_matrix, vpos);
23148 int start = MATRIX_ROW_START_CHARPOS (r);
23149 int pos = string_buffer_position (w, object, start);
23150 if (pos > 0)
23151 {
23152 help = Fget_char_property (make_number (pos),
23153 Qhelp_echo, w->buffer);
23154 if (!NILP (help))
23155 {
23156 charpos = pos;
23157 object = w->buffer;
23158 }
23159 }
23160 }
23161 }
23162 else if (BUFFERP (object)
23163 && charpos >= BEGV
23164 && charpos < ZV)
23165 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23166 object);
23167
23168 if (!NILP (help))
23169 {
23170 help_echo_string = help;
23171 help_echo_window = window;
23172 help_echo_object = object;
23173 help_echo_pos = charpos;
23174 }
23175 }
23176 }
23177
23178 /* Look for a `pointer' property. */
23179 if (NILP (pointer))
23180 {
23181 /* Check overlays first. */
23182 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23183 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23184
23185 if (NILP (pointer))
23186 {
23187 Lisp_Object object = glyph->object;
23188 int charpos = glyph->charpos;
23189
23190 /* Try text properties. */
23191 if (STRINGP (object)
23192 && charpos >= 0
23193 && charpos < SCHARS (object))
23194 {
23195 pointer = Fget_text_property (make_number (charpos),
23196 Qpointer, object);
23197 if (NILP (pointer))
23198 {
23199 /* If the string itself doesn't specify a pointer,
23200 see if the buffer text ``under'' it does. */
23201 struct glyph_row *r
23202 = MATRIX_ROW (w->current_matrix, vpos);
23203 int start = MATRIX_ROW_START_CHARPOS (r);
23204 int pos = string_buffer_position (w, object, start);
23205 if (pos > 0)
23206 pointer = Fget_char_property (make_number (pos),
23207 Qpointer, w->buffer);
23208 }
23209 }
23210 else if (BUFFERP (object)
23211 && charpos >= BEGV
23212 && charpos < ZV)
23213 pointer = Fget_text_property (make_number (charpos),
23214 Qpointer, object);
23215 }
23216 }
23217
23218 BEGV = obegv;
23219 ZV = ozv;
23220 current_buffer = obuf;
23221 }
23222
23223 set_cursor:
23224
23225 define_frame_cursor1 (f, cursor, pointer);
23226 }
23227
23228
23229 /* EXPORT for RIF:
23230 Clear any mouse-face on window W. This function is part of the
23231 redisplay interface, and is called from try_window_id and similar
23232 functions to ensure the mouse-highlight is off. */
23233
23234 void
23235 x_clear_window_mouse_face (w)
23236 struct window *w;
23237 {
23238 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23239 Lisp_Object window;
23240
23241 BLOCK_INPUT;
23242 XSETWINDOW (window, w);
23243 if (EQ (window, dpyinfo->mouse_face_window))
23244 clear_mouse_face (dpyinfo);
23245 UNBLOCK_INPUT;
23246 }
23247
23248
23249 /* EXPORT:
23250 Just discard the mouse face information for frame F, if any.
23251 This is used when the size of F is changed. */
23252
23253 void
23254 cancel_mouse_face (f)
23255 struct frame *f;
23256 {
23257 Lisp_Object window;
23258 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23259
23260 window = dpyinfo->mouse_face_window;
23261 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23262 {
23263 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23264 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23265 dpyinfo->mouse_face_window = Qnil;
23266 }
23267 }
23268
23269
23270 #endif /* HAVE_WINDOW_SYSTEM */
23271
23272 \f
23273 /***********************************************************************
23274 Exposure Events
23275 ***********************************************************************/
23276
23277 #ifdef HAVE_WINDOW_SYSTEM
23278
23279 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23280 which intersects rectangle R. R is in window-relative coordinates. */
23281
23282 static void
23283 expose_area (w, row, r, area)
23284 struct window *w;
23285 struct glyph_row *row;
23286 XRectangle *r;
23287 enum glyph_row_area area;
23288 {
23289 struct glyph *first = row->glyphs[area];
23290 struct glyph *end = row->glyphs[area] + row->used[area];
23291 struct glyph *last;
23292 int first_x, start_x, x;
23293
23294 if (area == TEXT_AREA && row->fill_line_p)
23295 /* If row extends face to end of line write the whole line. */
23296 draw_glyphs (w, 0, row, area,
23297 0, row->used[area],
23298 DRAW_NORMAL_TEXT, 0);
23299 else
23300 {
23301 /* Set START_X to the window-relative start position for drawing glyphs of
23302 AREA. The first glyph of the text area can be partially visible.
23303 The first glyphs of other areas cannot. */
23304 start_x = window_box_left_offset (w, area);
23305 x = start_x;
23306 if (area == TEXT_AREA)
23307 x += row->x;
23308
23309 /* Find the first glyph that must be redrawn. */
23310 while (first < end
23311 && x + first->pixel_width < r->x)
23312 {
23313 x += first->pixel_width;
23314 ++first;
23315 }
23316
23317 /* Find the last one. */
23318 last = first;
23319 first_x = x;
23320 while (last < end
23321 && x < r->x + r->width)
23322 {
23323 x += last->pixel_width;
23324 ++last;
23325 }
23326
23327 /* Repaint. */
23328 if (last > first)
23329 draw_glyphs (w, first_x - start_x, row, area,
23330 first - row->glyphs[area], last - row->glyphs[area],
23331 DRAW_NORMAL_TEXT, 0);
23332 }
23333 }
23334
23335
23336 /* Redraw the parts of the glyph row ROW on window W intersecting
23337 rectangle R. R is in window-relative coordinates. Value is
23338 non-zero if mouse-face was overwritten. */
23339
23340 static int
23341 expose_line (w, row, r)
23342 struct window *w;
23343 struct glyph_row *row;
23344 XRectangle *r;
23345 {
23346 xassert (row->enabled_p);
23347
23348 if (row->mode_line_p || w->pseudo_window_p)
23349 draw_glyphs (w, 0, row, TEXT_AREA,
23350 0, row->used[TEXT_AREA],
23351 DRAW_NORMAL_TEXT, 0);
23352 else
23353 {
23354 if (row->used[LEFT_MARGIN_AREA])
23355 expose_area (w, row, r, LEFT_MARGIN_AREA);
23356 if (row->used[TEXT_AREA])
23357 expose_area (w, row, r, TEXT_AREA);
23358 if (row->used[RIGHT_MARGIN_AREA])
23359 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23360 draw_row_fringe_bitmaps (w, row);
23361 }
23362
23363 return row->mouse_face_p;
23364 }
23365
23366
23367 /* Redraw those parts of glyphs rows during expose event handling that
23368 overlap other rows. Redrawing of an exposed line writes over parts
23369 of lines overlapping that exposed line; this function fixes that.
23370
23371 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23372 row in W's current matrix that is exposed and overlaps other rows.
23373 LAST_OVERLAPPING_ROW is the last such row. */
23374
23375 static void
23376 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23377 struct window *w;
23378 struct glyph_row *first_overlapping_row;
23379 struct glyph_row *last_overlapping_row;
23380 {
23381 struct glyph_row *row;
23382
23383 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23384 if (row->overlapping_p)
23385 {
23386 xassert (row->enabled_p && !row->mode_line_p);
23387
23388 if (row->used[LEFT_MARGIN_AREA])
23389 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23390
23391 if (row->used[TEXT_AREA])
23392 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23393
23394 if (row->used[RIGHT_MARGIN_AREA])
23395 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23396 }
23397 }
23398
23399
23400 /* Return non-zero if W's cursor intersects rectangle R. */
23401
23402 static int
23403 phys_cursor_in_rect_p (w, r)
23404 struct window *w;
23405 XRectangle *r;
23406 {
23407 XRectangle cr, result;
23408 struct glyph *cursor_glyph;
23409
23410 cursor_glyph = get_phys_cursor_glyph (w);
23411 if (cursor_glyph)
23412 {
23413 /* r is relative to W's box, but w->phys_cursor.x is relative
23414 to left edge of W's TEXT area. Adjust it. */
23415 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23416 cr.y = w->phys_cursor.y;
23417 cr.width = cursor_glyph->pixel_width;
23418 cr.height = w->phys_cursor_height;
23419 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23420 I assume the effect is the same -- and this is portable. */
23421 return x_intersect_rectangles (&cr, r, &result);
23422 }
23423 else
23424 return 0;
23425 }
23426
23427
23428 /* EXPORT:
23429 Draw a vertical window border to the right of window W if W doesn't
23430 have vertical scroll bars. */
23431
23432 void
23433 x_draw_vertical_border (w)
23434 struct window *w;
23435 {
23436 /* We could do better, if we knew what type of scroll-bar the adjacent
23437 windows (on either side) have... But we don't :-(
23438 However, I think this works ok. ++KFS 2003-04-25 */
23439
23440 /* Redraw borders between horizontally adjacent windows. Don't
23441 do it for frames with vertical scroll bars because either the
23442 right scroll bar of a window, or the left scroll bar of its
23443 neighbor will suffice as a border. */
23444 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23445 return;
23446
23447 if (!WINDOW_RIGHTMOST_P (w)
23448 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23449 {
23450 int x0, x1, y0, y1;
23451
23452 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23453 y1 -= 1;
23454
23455 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23456 x1 -= 1;
23457
23458 rif->draw_vertical_window_border (w, x1, y0, y1);
23459 }
23460 else if (!WINDOW_LEFTMOST_P (w)
23461 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23462 {
23463 int x0, x1, y0, y1;
23464
23465 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23466 y1 -= 1;
23467
23468 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23469 x0 -= 1;
23470
23471 rif->draw_vertical_window_border (w, x0, y0, y1);
23472 }
23473 }
23474
23475
23476 /* Redraw the part of window W intersection rectangle FR. Pixel
23477 coordinates in FR are frame-relative. Call this function with
23478 input blocked. Value is non-zero if the exposure overwrites
23479 mouse-face. */
23480
23481 static int
23482 expose_window (w, fr)
23483 struct window *w;
23484 XRectangle *fr;
23485 {
23486 struct frame *f = XFRAME (w->frame);
23487 XRectangle wr, r;
23488 int mouse_face_overwritten_p = 0;
23489
23490 /* If window is not yet fully initialized, do nothing. This can
23491 happen when toolkit scroll bars are used and a window is split.
23492 Reconfiguring the scroll bar will generate an expose for a newly
23493 created window. */
23494 if (w->current_matrix == NULL)
23495 return 0;
23496
23497 /* When we're currently updating the window, display and current
23498 matrix usually don't agree. Arrange for a thorough display
23499 later. */
23500 if (w == updated_window)
23501 {
23502 SET_FRAME_GARBAGED (f);
23503 return 0;
23504 }
23505
23506 /* Frame-relative pixel rectangle of W. */
23507 wr.x = WINDOW_LEFT_EDGE_X (w);
23508 wr.y = WINDOW_TOP_EDGE_Y (w);
23509 wr.width = WINDOW_TOTAL_WIDTH (w);
23510 wr.height = WINDOW_TOTAL_HEIGHT (w);
23511
23512 if (x_intersect_rectangles (fr, &wr, &r))
23513 {
23514 int yb = window_text_bottom_y (w);
23515 struct glyph_row *row;
23516 int cursor_cleared_p;
23517 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23518
23519 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23520 r.x, r.y, r.width, r.height));
23521
23522 /* Convert to window coordinates. */
23523 r.x -= WINDOW_LEFT_EDGE_X (w);
23524 r.y -= WINDOW_TOP_EDGE_Y (w);
23525
23526 /* Turn off the cursor. */
23527 if (!w->pseudo_window_p
23528 && phys_cursor_in_rect_p (w, &r))
23529 {
23530 x_clear_cursor (w);
23531 cursor_cleared_p = 1;
23532 }
23533 else
23534 cursor_cleared_p = 0;
23535
23536 /* Update lines intersecting rectangle R. */
23537 first_overlapping_row = last_overlapping_row = NULL;
23538 for (row = w->current_matrix->rows;
23539 row->enabled_p;
23540 ++row)
23541 {
23542 int y0 = row->y;
23543 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23544
23545 if ((y0 >= r.y && y0 < r.y + r.height)
23546 || (y1 > r.y && y1 < r.y + r.height)
23547 || (r.y >= y0 && r.y < y1)
23548 || (r.y + r.height > y0 && r.y + r.height < y1))
23549 {
23550 /* A header line may be overlapping, but there is no need
23551 to fix overlapping areas for them. KFS 2005-02-12 */
23552 if (row->overlapping_p && !row->mode_line_p)
23553 {
23554 if (first_overlapping_row == NULL)
23555 first_overlapping_row = row;
23556 last_overlapping_row = row;
23557 }
23558
23559 if (expose_line (w, row, &r))
23560 mouse_face_overwritten_p = 1;
23561 }
23562
23563 if (y1 >= yb)
23564 break;
23565 }
23566
23567 /* Display the mode line if there is one. */
23568 if (WINDOW_WANTS_MODELINE_P (w)
23569 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23570 row->enabled_p)
23571 && row->y < r.y + r.height)
23572 {
23573 if (expose_line (w, row, &r))
23574 mouse_face_overwritten_p = 1;
23575 }
23576
23577 if (!w->pseudo_window_p)
23578 {
23579 /* Fix the display of overlapping rows. */
23580 if (first_overlapping_row)
23581 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23582
23583 /* Draw border between windows. */
23584 x_draw_vertical_border (w);
23585
23586 /* Turn the cursor on again. */
23587 if (cursor_cleared_p)
23588 update_window_cursor (w, 1);
23589 }
23590 }
23591
23592 return mouse_face_overwritten_p;
23593 }
23594
23595
23596
23597 /* Redraw (parts) of all windows in the window tree rooted at W that
23598 intersect R. R contains frame pixel coordinates. Value is
23599 non-zero if the exposure overwrites mouse-face. */
23600
23601 static int
23602 expose_window_tree (w, r)
23603 struct window *w;
23604 XRectangle *r;
23605 {
23606 struct frame *f = XFRAME (w->frame);
23607 int mouse_face_overwritten_p = 0;
23608
23609 while (w && !FRAME_GARBAGED_P (f))
23610 {
23611 if (!NILP (w->hchild))
23612 mouse_face_overwritten_p
23613 |= expose_window_tree (XWINDOW (w->hchild), r);
23614 else if (!NILP (w->vchild))
23615 mouse_face_overwritten_p
23616 |= expose_window_tree (XWINDOW (w->vchild), r);
23617 else
23618 mouse_face_overwritten_p |= expose_window (w, r);
23619
23620 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23621 }
23622
23623 return mouse_face_overwritten_p;
23624 }
23625
23626
23627 /* EXPORT:
23628 Redisplay an exposed area of frame F. X and Y are the upper-left
23629 corner of the exposed rectangle. W and H are width and height of
23630 the exposed area. All are pixel values. W or H zero means redraw
23631 the entire frame. */
23632
23633 void
23634 expose_frame (f, x, y, w, h)
23635 struct frame *f;
23636 int x, y, w, h;
23637 {
23638 XRectangle r;
23639 int mouse_face_overwritten_p = 0;
23640
23641 TRACE ((stderr, "expose_frame "));
23642
23643 /* No need to redraw if frame will be redrawn soon. */
23644 if (FRAME_GARBAGED_P (f))
23645 {
23646 TRACE ((stderr, " garbaged\n"));
23647 return;
23648 }
23649
23650 /* If basic faces haven't been realized yet, there is no point in
23651 trying to redraw anything. This can happen when we get an expose
23652 event while Emacs is starting, e.g. by moving another window. */
23653 if (FRAME_FACE_CACHE (f) == NULL
23654 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23655 {
23656 TRACE ((stderr, " no faces\n"));
23657 return;
23658 }
23659
23660 if (w == 0 || h == 0)
23661 {
23662 r.x = r.y = 0;
23663 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23664 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23665 }
23666 else
23667 {
23668 r.x = x;
23669 r.y = y;
23670 r.width = w;
23671 r.height = h;
23672 }
23673
23674 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23675 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23676
23677 if (WINDOWP (f->tool_bar_window))
23678 mouse_face_overwritten_p
23679 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23680
23681 #ifdef HAVE_X_WINDOWS
23682 #ifndef MSDOS
23683 #ifndef USE_X_TOOLKIT
23684 if (WINDOWP (f->menu_bar_window))
23685 mouse_face_overwritten_p
23686 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23687 #endif /* not USE_X_TOOLKIT */
23688 #endif
23689 #endif
23690
23691 /* Some window managers support a focus-follows-mouse style with
23692 delayed raising of frames. Imagine a partially obscured frame,
23693 and moving the mouse into partially obscured mouse-face on that
23694 frame. The visible part of the mouse-face will be highlighted,
23695 then the WM raises the obscured frame. With at least one WM, KDE
23696 2.1, Emacs is not getting any event for the raising of the frame
23697 (even tried with SubstructureRedirectMask), only Expose events.
23698 These expose events will draw text normally, i.e. not
23699 highlighted. Which means we must redo the highlight here.
23700 Subsume it under ``we love X''. --gerd 2001-08-15 */
23701 /* Included in Windows version because Windows most likely does not
23702 do the right thing if any third party tool offers
23703 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23704 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23705 {
23706 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23707 if (f == dpyinfo->mouse_face_mouse_frame)
23708 {
23709 int x = dpyinfo->mouse_face_mouse_x;
23710 int y = dpyinfo->mouse_face_mouse_y;
23711 clear_mouse_face (dpyinfo);
23712 note_mouse_highlight (f, x, y);
23713 }
23714 }
23715 }
23716
23717
23718 /* EXPORT:
23719 Determine the intersection of two rectangles R1 and R2. Return
23720 the intersection in *RESULT. Value is non-zero if RESULT is not
23721 empty. */
23722
23723 int
23724 x_intersect_rectangles (r1, r2, result)
23725 XRectangle *r1, *r2, *result;
23726 {
23727 XRectangle *left, *right;
23728 XRectangle *upper, *lower;
23729 int intersection_p = 0;
23730
23731 /* Rearrange so that R1 is the left-most rectangle. */
23732 if (r1->x < r2->x)
23733 left = r1, right = r2;
23734 else
23735 left = r2, right = r1;
23736
23737 /* X0 of the intersection is right.x0, if this is inside R1,
23738 otherwise there is no intersection. */
23739 if (right->x <= left->x + left->width)
23740 {
23741 result->x = right->x;
23742
23743 /* The right end of the intersection is the minimum of the
23744 the right ends of left and right. */
23745 result->width = (min (left->x + left->width, right->x + right->width)
23746 - result->x);
23747
23748 /* Same game for Y. */
23749 if (r1->y < r2->y)
23750 upper = r1, lower = r2;
23751 else
23752 upper = r2, lower = r1;
23753
23754 /* The upper end of the intersection is lower.y0, if this is inside
23755 of upper. Otherwise, there is no intersection. */
23756 if (lower->y <= upper->y + upper->height)
23757 {
23758 result->y = lower->y;
23759
23760 /* The lower end of the intersection is the minimum of the lower
23761 ends of upper and lower. */
23762 result->height = (min (lower->y + lower->height,
23763 upper->y + upper->height)
23764 - result->y);
23765 intersection_p = 1;
23766 }
23767 }
23768
23769 return intersection_p;
23770 }
23771
23772 #endif /* HAVE_WINDOW_SYSTEM */
23773
23774 \f
23775 /***********************************************************************
23776 Initialization
23777 ***********************************************************************/
23778
23779 void
23780 syms_of_xdisp ()
23781 {
23782 Vwith_echo_area_save_vector = Qnil;
23783 staticpro (&Vwith_echo_area_save_vector);
23784
23785 Vmessage_stack = Qnil;
23786 staticpro (&Vmessage_stack);
23787
23788 Qinhibit_redisplay = intern ("inhibit-redisplay");
23789 staticpro (&Qinhibit_redisplay);
23790
23791 message_dolog_marker1 = Fmake_marker ();
23792 staticpro (&message_dolog_marker1);
23793 message_dolog_marker2 = Fmake_marker ();
23794 staticpro (&message_dolog_marker2);
23795 message_dolog_marker3 = Fmake_marker ();
23796 staticpro (&message_dolog_marker3);
23797
23798 #if GLYPH_DEBUG
23799 defsubr (&Sdump_frame_glyph_matrix);
23800 defsubr (&Sdump_glyph_matrix);
23801 defsubr (&Sdump_glyph_row);
23802 defsubr (&Sdump_tool_bar_row);
23803 defsubr (&Strace_redisplay);
23804 defsubr (&Strace_to_stderr);
23805 #endif
23806 #ifdef HAVE_WINDOW_SYSTEM
23807 defsubr (&Stool_bar_lines_needed);
23808 defsubr (&Slookup_image_map);
23809 #endif
23810 defsubr (&Sformat_mode_line);
23811
23812 staticpro (&Qmenu_bar_update_hook);
23813 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23814
23815 staticpro (&Qoverriding_terminal_local_map);
23816 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23817
23818 staticpro (&Qoverriding_local_map);
23819 Qoverriding_local_map = intern ("overriding-local-map");
23820
23821 staticpro (&Qwindow_scroll_functions);
23822 Qwindow_scroll_functions = intern ("window-scroll-functions");
23823
23824 staticpro (&Qredisplay_end_trigger_functions);
23825 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23826
23827 staticpro (&Qinhibit_point_motion_hooks);
23828 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23829
23830 QCdata = intern (":data");
23831 staticpro (&QCdata);
23832 Qdisplay = intern ("display");
23833 staticpro (&Qdisplay);
23834 Qspace_width = intern ("space-width");
23835 staticpro (&Qspace_width);
23836 Qraise = intern ("raise");
23837 staticpro (&Qraise);
23838 Qslice = intern ("slice");
23839 staticpro (&Qslice);
23840 Qspace = intern ("space");
23841 staticpro (&Qspace);
23842 Qmargin = intern ("margin");
23843 staticpro (&Qmargin);
23844 Qpointer = intern ("pointer");
23845 staticpro (&Qpointer);
23846 Qleft_margin = intern ("left-margin");
23847 staticpro (&Qleft_margin);
23848 Qright_margin = intern ("right-margin");
23849 staticpro (&Qright_margin);
23850 Qcenter = intern ("center");
23851 staticpro (&Qcenter);
23852 Qline_height = intern ("line-height");
23853 staticpro (&Qline_height);
23854 QCalign_to = intern (":align-to");
23855 staticpro (&QCalign_to);
23856 QCrelative_width = intern (":relative-width");
23857 staticpro (&QCrelative_width);
23858 QCrelative_height = intern (":relative-height");
23859 staticpro (&QCrelative_height);
23860 QCeval = intern (":eval");
23861 staticpro (&QCeval);
23862 QCpropertize = intern (":propertize");
23863 staticpro (&QCpropertize);
23864 QCfile = intern (":file");
23865 staticpro (&QCfile);
23866 Qfontified = intern ("fontified");
23867 staticpro (&Qfontified);
23868 Qfontification_functions = intern ("fontification-functions");
23869 staticpro (&Qfontification_functions);
23870 Qtrailing_whitespace = intern ("trailing-whitespace");
23871 staticpro (&Qtrailing_whitespace);
23872 Qescape_glyph = intern ("escape-glyph");
23873 staticpro (&Qescape_glyph);
23874 Qnobreak_space = intern ("nobreak-space");
23875 staticpro (&Qnobreak_space);
23876 Qimage = intern ("image");
23877 staticpro (&Qimage);
23878 QCmap = intern (":map");
23879 staticpro (&QCmap);
23880 QCpointer = intern (":pointer");
23881 staticpro (&QCpointer);
23882 Qrect = intern ("rect");
23883 staticpro (&Qrect);
23884 Qcircle = intern ("circle");
23885 staticpro (&Qcircle);
23886 Qpoly = intern ("poly");
23887 staticpro (&Qpoly);
23888 Qmessage_truncate_lines = intern ("message-truncate-lines");
23889 staticpro (&Qmessage_truncate_lines);
23890 Qgrow_only = intern ("grow-only");
23891 staticpro (&Qgrow_only);
23892 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23893 staticpro (&Qinhibit_menubar_update);
23894 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23895 staticpro (&Qinhibit_eval_during_redisplay);
23896 Qposition = intern ("position");
23897 staticpro (&Qposition);
23898 Qbuffer_position = intern ("buffer-position");
23899 staticpro (&Qbuffer_position);
23900 Qobject = intern ("object");
23901 staticpro (&Qobject);
23902 Qbar = intern ("bar");
23903 staticpro (&Qbar);
23904 Qhbar = intern ("hbar");
23905 staticpro (&Qhbar);
23906 Qbox = intern ("box");
23907 staticpro (&Qbox);
23908 Qhollow = intern ("hollow");
23909 staticpro (&Qhollow);
23910 Qhand = intern ("hand");
23911 staticpro (&Qhand);
23912 Qarrow = intern ("arrow");
23913 staticpro (&Qarrow);
23914 Qtext = intern ("text");
23915 staticpro (&Qtext);
23916 Qrisky_local_variable = intern ("risky-local-variable");
23917 staticpro (&Qrisky_local_variable);
23918 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23919 staticpro (&Qinhibit_free_realized_faces);
23920
23921 list_of_error = Fcons (Fcons (intern ("error"),
23922 Fcons (intern ("void-variable"), Qnil)),
23923 Qnil);
23924 staticpro (&list_of_error);
23925
23926 Qlast_arrow_position = intern ("last-arrow-position");
23927 staticpro (&Qlast_arrow_position);
23928 Qlast_arrow_string = intern ("last-arrow-string");
23929 staticpro (&Qlast_arrow_string);
23930
23931 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23932 staticpro (&Qoverlay_arrow_string);
23933 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23934 staticpro (&Qoverlay_arrow_bitmap);
23935
23936 echo_buffer[0] = echo_buffer[1] = Qnil;
23937 staticpro (&echo_buffer[0]);
23938 staticpro (&echo_buffer[1]);
23939
23940 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23941 staticpro (&echo_area_buffer[0]);
23942 staticpro (&echo_area_buffer[1]);
23943
23944 Vmessages_buffer_name = build_string ("*Messages*");
23945 staticpro (&Vmessages_buffer_name);
23946
23947 mode_line_proptrans_alist = Qnil;
23948 staticpro (&mode_line_proptrans_alist);
23949 mode_line_string_list = Qnil;
23950 staticpro (&mode_line_string_list);
23951 mode_line_string_face = Qnil;
23952 staticpro (&mode_line_string_face);
23953 mode_line_string_face_prop = Qnil;
23954 staticpro (&mode_line_string_face_prop);
23955 Vmode_line_unwind_vector = Qnil;
23956 staticpro (&Vmode_line_unwind_vector);
23957
23958 help_echo_string = Qnil;
23959 staticpro (&help_echo_string);
23960 help_echo_object = Qnil;
23961 staticpro (&help_echo_object);
23962 help_echo_window = Qnil;
23963 staticpro (&help_echo_window);
23964 previous_help_echo_string = Qnil;
23965 staticpro (&previous_help_echo_string);
23966 help_echo_pos = -1;
23967
23968 #ifdef HAVE_WINDOW_SYSTEM
23969 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23970 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23971 For example, if a block cursor is over a tab, it will be drawn as
23972 wide as that tab on the display. */);
23973 x_stretch_cursor_p = 0;
23974 #endif
23975
23976 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23977 doc: /* *Non-nil means highlight trailing whitespace.
23978 The face used for trailing whitespace is `trailing-whitespace'. */);
23979 Vshow_trailing_whitespace = Qnil;
23980
23981 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23982 doc: /* *Control highlighting of nobreak space and soft hyphen.
23983 A value of t means highlight the character itself (for nobreak space,
23984 use face `nobreak-space').
23985 A value of nil means no highlighting.
23986 Other values mean display the escape glyph followed by an ordinary
23987 space or ordinary hyphen. */);
23988 Vnobreak_char_display = Qt;
23989
23990 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23991 doc: /* *The pointer shape to show in void text areas.
23992 A value of nil means to show the text pointer. Other options are `arrow',
23993 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23994 Vvoid_text_area_pointer = Qarrow;
23995
23996 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23997 doc: /* Non-nil means don't actually do any redisplay.
23998 This is used for internal purposes. */);
23999 Vinhibit_redisplay = Qnil;
24000
24001 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24002 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24003 Vglobal_mode_string = Qnil;
24004
24005 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24006 doc: /* Marker for where to display an arrow on top of the buffer text.
24007 This must be the beginning of a line in order to work.
24008 See also `overlay-arrow-string'. */);
24009 Voverlay_arrow_position = Qnil;
24010
24011 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24012 doc: /* String to display as an arrow in non-window frames.
24013 See also `overlay-arrow-position'. */);
24014 Voverlay_arrow_string = build_string ("=>");
24015
24016 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24017 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24018 The symbols on this list are examined during redisplay to determine
24019 where to display overlay arrows. */);
24020 Voverlay_arrow_variable_list
24021 = Fcons (intern ("overlay-arrow-position"), Qnil);
24022
24023 DEFVAR_INT ("scroll-step", &scroll_step,
24024 doc: /* *The number of lines to try scrolling a window by when point moves out.
24025 If that fails to bring point back on frame, point is centered instead.
24026 If this is zero, point is always centered after it moves off frame.
24027 If you want scrolling to always be a line at a time, you should set
24028 `scroll-conservatively' to a large value rather than set this to 1. */);
24029
24030 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24031 doc: /* *Scroll up to this many lines, to bring point back on screen.
24032 A value of zero means to scroll the text to center point vertically
24033 in the window. */);
24034 scroll_conservatively = 0;
24035
24036 DEFVAR_INT ("scroll-margin", &scroll_margin,
24037 doc: /* *Number of lines of margin at the top and bottom of a window.
24038 Recenter the window whenever point gets within this many lines
24039 of the top or bottom of the window. */);
24040 scroll_margin = 0;
24041
24042 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24043 doc: /* Pixels per inch value for non-window system displays.
24044 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24045 Vdisplay_pixels_per_inch = make_float (72.0);
24046
24047 #if GLYPH_DEBUG
24048 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24049 #endif
24050
24051 DEFVAR_BOOL ("truncate-partial-width-windows",
24052 &truncate_partial_width_windows,
24053 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24054 truncate_partial_width_windows = 1;
24055
24056 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24057 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24058 Any other value means to use the appropriate face, `mode-line',
24059 `header-line', or `menu' respectively. */);
24060 mode_line_inverse_video = 1;
24061
24062 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24063 doc: /* *Maximum buffer size for which line number should be displayed.
24064 If the buffer is bigger than this, the line number does not appear
24065 in the mode line. A value of nil means no limit. */);
24066 Vline_number_display_limit = Qnil;
24067
24068 DEFVAR_INT ("line-number-display-limit-width",
24069 &line_number_display_limit_width,
24070 doc: /* *Maximum line width (in characters) for line number display.
24071 If the average length of the lines near point is bigger than this, then the
24072 line number may be omitted from the mode line. */);
24073 line_number_display_limit_width = 200;
24074
24075 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24076 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24077 highlight_nonselected_windows = 0;
24078
24079 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24080 doc: /* Non-nil if more than one frame is visible on this display.
24081 Minibuffer-only frames don't count, but iconified frames do.
24082 This variable is not guaranteed to be accurate except while processing
24083 `frame-title-format' and `icon-title-format'. */);
24084
24085 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24086 doc: /* Template for displaying the title bar of visible frames.
24087 \(Assuming the window manager supports this feature.)
24088
24089 This variable has the same structure as `mode-line-format', except that
24090 the %c and %l constructs are ignored. It is used only on frames for
24091 which no explicit name has been set \(see `modify-frame-parameters'). */);
24092
24093 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24094 doc: /* Template for displaying the title bar of an iconified frame.
24095 \(Assuming the window manager supports this feature.)
24096 This variable has the same structure as `mode-line-format' (which see),
24097 and is used only on frames for which no explicit name has been set
24098 \(see `modify-frame-parameters'). */);
24099 Vicon_title_format
24100 = Vframe_title_format
24101 = Fcons (intern ("multiple-frames"),
24102 Fcons (build_string ("%b"),
24103 Fcons (Fcons (empty_string,
24104 Fcons (intern ("invocation-name"),
24105 Fcons (build_string ("@"),
24106 Fcons (intern ("system-name"),
24107 Qnil)))),
24108 Qnil)));
24109
24110 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24111 doc: /* Maximum number of lines to keep in the message log buffer.
24112 If nil, disable message logging. If t, log messages but don't truncate
24113 the buffer when it becomes large. */);
24114 Vmessage_log_max = make_number (100);
24115
24116 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24117 doc: /* Functions called before redisplay, if window sizes have changed.
24118 The value should be a list of functions that take one argument.
24119 Just before redisplay, for each frame, if any of its windows have changed
24120 size since the last redisplay, or have been split or deleted,
24121 all the functions in the list are called, with the frame as argument. */);
24122 Vwindow_size_change_functions = Qnil;
24123
24124 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24125 doc: /* List of functions to call before redisplaying a window with scrolling.
24126 Each function is called with two arguments, the window
24127 and its new display-start position. Note that the value of `window-end'
24128 is not valid when these functions are called. */);
24129 Vwindow_scroll_functions = Qnil;
24130
24131 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24132 doc: /* Functions called when redisplay of a window reaches the end trigger.
24133 Each function is called with two arguments, the window and the end trigger value.
24134 See `set-window-redisplay-end-trigger'. */);
24135 Vredisplay_end_trigger_functions = Qnil;
24136
24137 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24138 doc: /* *Non-nil means autoselect window with mouse pointer.
24139 If nil, do not autoselect windows.
24140 A positive number means delay autoselection by that many seconds: a
24141 window is autoselected only after the mouse has remained in that
24142 window for the duration of the delay.
24143 A negative number has a similar effect, but causes windows to be
24144 autoselected only after the mouse has stopped moving. \(Because of
24145 the way Emacs compares mouse events, you will occasionally wait twice
24146 that time before the window gets selected.\)
24147 Any other value means to autoselect window instantaneously when the
24148 mouse pointer enters it.
24149
24150 Autoselection selects the minibuffer only if it is active, and never
24151 unselects the minibuffer if it is active.
24152
24153 When customizing this variable make sure that the actual value of
24154 `focus-follows-mouse' matches the behavior of your window manager. */);
24155 Vmouse_autoselect_window = Qnil;
24156
24157 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24158 doc: /* *Non-nil means automatically resize tool-bars.
24159 This dynamically changes the tool-bar's height to the minimum height
24160 that is needed to make all tool-bar items visible.
24161 If value is `grow-only', the tool-bar's height is only increased
24162 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24163 Vauto_resize_tool_bars = Qt;
24164
24165 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24166 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24167 auto_raise_tool_bar_buttons_p = 1;
24168
24169 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24170 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24171 make_cursor_line_fully_visible_p = 1;
24172
24173 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24174 doc: /* *Border below tool-bar in pixels.
24175 If an integer, use it as the height of the border.
24176 If it is one of `internal-border-width' or `border-width', use the
24177 value of the corresponding frame parameter.
24178 Otherwise, no border is added below the tool-bar. */);
24179 Vtool_bar_border = Qinternal_border_width;
24180
24181 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24182 doc: /* *Margin around tool-bar buttons in pixels.
24183 If an integer, use that for both horizontal and vertical margins.
24184 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24185 HORZ specifying the horizontal margin, and VERT specifying the
24186 vertical margin. */);
24187 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24188
24189 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24190 doc: /* *Relief thickness of tool-bar buttons. */);
24191 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24192
24193 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24194 doc: /* List of functions to call to fontify regions of text.
24195 Each function is called with one argument POS. Functions must
24196 fontify a region starting at POS in the current buffer, and give
24197 fontified regions the property `fontified'. */);
24198 Vfontification_functions = Qnil;
24199 Fmake_variable_buffer_local (Qfontification_functions);
24200
24201 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24202 &unibyte_display_via_language_environment,
24203 doc: /* *Non-nil means display unibyte text according to language environment.
24204 Specifically this means that unibyte non-ASCII characters
24205 are displayed by converting them to the equivalent multibyte characters
24206 according to the current language environment. As a result, they are
24207 displayed according to the current fontset. */);
24208 unibyte_display_via_language_environment = 0;
24209
24210 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24211 doc: /* *Maximum height for resizing mini-windows.
24212 If a float, it specifies a fraction of the mini-window frame's height.
24213 If an integer, it specifies a number of lines. */);
24214 Vmax_mini_window_height = make_float (0.25);
24215
24216 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24217 doc: /* *How to resize mini-windows.
24218 A value of nil means don't automatically resize mini-windows.
24219 A value of t means resize them to fit the text displayed in them.
24220 A value of `grow-only', the default, means let mini-windows grow
24221 only, until their display becomes empty, at which point the windows
24222 go back to their normal size. */);
24223 Vresize_mini_windows = Qgrow_only;
24224
24225 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24226 doc: /* Alist specifying how to blink the cursor off.
24227 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24228 `cursor-type' frame-parameter or variable equals ON-STATE,
24229 comparing using `equal', Emacs uses OFF-STATE to specify
24230 how to blink it off. ON-STATE and OFF-STATE are values for
24231 the `cursor-type' frame parameter.
24232
24233 If a frame's ON-STATE has no entry in this list,
24234 the frame's other specifications determine how to blink the cursor off. */);
24235 Vblink_cursor_alist = Qnil;
24236
24237 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24238 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24239 automatic_hscrolling_p = 1;
24240
24241 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24242 doc: /* *How many columns away from the window edge point is allowed to get
24243 before automatic hscrolling will horizontally scroll the window. */);
24244 hscroll_margin = 5;
24245
24246 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24247 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24248 When point is less than `hscroll-margin' columns from the window
24249 edge, automatic hscrolling will scroll the window by the amount of columns
24250 determined by this variable. If its value is a positive integer, scroll that
24251 many columns. If it's a positive floating-point number, it specifies the
24252 fraction of the window's width to scroll. If it's nil or zero, point will be
24253 centered horizontally after the scroll. Any other value, including negative
24254 numbers, are treated as if the value were zero.
24255
24256 Automatic hscrolling always moves point outside the scroll margin, so if
24257 point was more than scroll step columns inside the margin, the window will
24258 scroll more than the value given by the scroll step.
24259
24260 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24261 and `scroll-right' overrides this variable's effect. */);
24262 Vhscroll_step = make_number (0);
24263
24264 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24265 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24266 Bind this around calls to `message' to let it take effect. */);
24267 message_truncate_lines = 0;
24268
24269 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24270 doc: /* Normal hook run to update the menu bar definitions.
24271 Redisplay runs this hook before it redisplays the menu bar.
24272 This is used to update submenus such as Buffers,
24273 whose contents depend on various data. */);
24274 Vmenu_bar_update_hook = Qnil;
24275
24276 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24277 doc: /* Frame for which we are updating a menu.
24278 The enable predicate for a menu binding should check this variable. */);
24279 Vmenu_updating_frame = Qnil;
24280
24281 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24282 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24283 inhibit_menubar_update = 0;
24284
24285 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24286 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24287 inhibit_eval_during_redisplay = 0;
24288
24289 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24290 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24291 inhibit_free_realized_faces = 0;
24292
24293 #if GLYPH_DEBUG
24294 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24295 doc: /* Inhibit try_window_id display optimization. */);
24296 inhibit_try_window_id = 0;
24297
24298 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24299 doc: /* Inhibit try_window_reusing display optimization. */);
24300 inhibit_try_window_reusing = 0;
24301
24302 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24303 doc: /* Inhibit try_cursor_movement display optimization. */);
24304 inhibit_try_cursor_movement = 0;
24305 #endif /* GLYPH_DEBUG */
24306
24307 DEFVAR_INT ("overline-margin", &overline_margin,
24308 doc: /* *Space between overline and text, in pixels.
24309 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24310 margin to the caracter height. */);
24311 overline_margin = 2;
24312 }
24313
24314
24315 /* Initialize this module when Emacs starts. */
24316
24317 void
24318 init_xdisp ()
24319 {
24320 Lisp_Object root_window;
24321 struct window *mini_w;
24322
24323 current_header_line_height = current_mode_line_height = -1;
24324
24325 CHARPOS (this_line_start_pos) = 0;
24326
24327 mini_w = XWINDOW (minibuf_window);
24328 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24329
24330 if (!noninteractive)
24331 {
24332 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24333 int i;
24334
24335 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24336 set_window_height (root_window,
24337 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24338 0);
24339 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24340 set_window_height (minibuf_window, 1, 0);
24341
24342 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24343 mini_w->total_cols = make_number (FRAME_COLS (f));
24344
24345 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24346 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24347 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24348
24349 /* The default ellipsis glyphs `...'. */
24350 for (i = 0; i < 3; ++i)
24351 default_invis_vector[i] = make_number ('.');
24352 }
24353
24354 {
24355 /* Allocate the buffer for frame titles.
24356 Also used for `format-mode-line'. */
24357 int size = 100;
24358 mode_line_noprop_buf = (char *) xmalloc (size);
24359 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24360 mode_line_noprop_ptr = mode_line_noprop_buf;
24361 mode_line_target = MODE_LINE_DISPLAY;
24362 }
24363
24364 help_echo_showing_p = 0;
24365 }
24366
24367
24368 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24369 (do not change this comment) */