(XDrawLine) [USE_MAC_IMAGE_IO]: Remove spurious return.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008 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, Lisp_Object,
971 struct text_pos *, 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 #ifndef HAVE_CARBON
1704 static
1705 #endif
1706 struct glyph *
1707 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1708 struct window *w;
1709 int x, y;
1710 int *hpos, *vpos, *dx, *dy, *area;
1711 {
1712 struct glyph *glyph, *end;
1713 struct glyph_row *row = NULL;
1714 int x0, i;
1715
1716 /* Find row containing Y. Give up if some row is not enabled. */
1717 for (i = 0; i < w->current_matrix->nrows; ++i)
1718 {
1719 row = MATRIX_ROW (w->current_matrix, i);
1720 if (!row->enabled_p)
1721 return NULL;
1722 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1723 break;
1724 }
1725
1726 *vpos = i;
1727 *hpos = 0;
1728
1729 /* Give up if Y is not in the window. */
1730 if (i == w->current_matrix->nrows)
1731 return NULL;
1732
1733 /* Get the glyph area containing X. */
1734 if (w->pseudo_window_p)
1735 {
1736 *area = TEXT_AREA;
1737 x0 = 0;
1738 }
1739 else
1740 {
1741 if (x < window_box_left_offset (w, TEXT_AREA))
1742 {
1743 *area = LEFT_MARGIN_AREA;
1744 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1745 }
1746 else if (x < window_box_right_offset (w, TEXT_AREA))
1747 {
1748 *area = TEXT_AREA;
1749 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1750 }
1751 else
1752 {
1753 *area = RIGHT_MARGIN_AREA;
1754 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1755 }
1756 }
1757
1758 /* Find glyph containing X. */
1759 glyph = row->glyphs[*area];
1760 end = glyph + row->used[*area];
1761 x -= x0;
1762 while (glyph < end && x >= glyph->pixel_width)
1763 {
1764 x -= glyph->pixel_width;
1765 ++glyph;
1766 }
1767
1768 if (glyph == end)
1769 return NULL;
1770
1771 if (dx)
1772 {
1773 *dx = x;
1774 *dy = y - (row->y + row->ascent - glyph->ascent);
1775 }
1776
1777 *hpos = glyph - row->glyphs[*area];
1778 return glyph;
1779 }
1780
1781
1782 /* EXPORT:
1783 Convert frame-relative x/y to coordinates relative to window W.
1784 Takes pseudo-windows into account. */
1785
1786 void
1787 frame_to_window_pixel_xy (w, x, y)
1788 struct window *w;
1789 int *x, *y;
1790 {
1791 if (w->pseudo_window_p)
1792 {
1793 /* A pseudo-window is always full-width, and starts at the
1794 left edge of the frame, plus a frame border. */
1795 struct frame *f = XFRAME (w->frame);
1796 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1797 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1798 }
1799 else
1800 {
1801 *x -= WINDOW_LEFT_EDGE_X (w);
1802 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1803 }
1804 }
1805
1806 /* EXPORT:
1807 Return in RECTS[] at most N clipping rectangles for glyph string S.
1808 Return the number of stored rectangles. */
1809
1810 int
1811 get_glyph_string_clip_rects (s, rects, n)
1812 struct glyph_string *s;
1813 NativeRectangle *rects;
1814 int n;
1815 {
1816 XRectangle r;
1817
1818 if (n <= 0)
1819 return 0;
1820
1821 if (s->row->full_width_p)
1822 {
1823 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1824 r.x = WINDOW_LEFT_EDGE_X (s->w);
1825 r.width = WINDOW_TOTAL_WIDTH (s->w);
1826
1827 /* Unless displaying a mode or menu bar line, which are always
1828 fully visible, clip to the visible part of the row. */
1829 if (s->w->pseudo_window_p)
1830 r.height = s->row->visible_height;
1831 else
1832 r.height = s->height;
1833 }
1834 else
1835 {
1836 /* This is a text line that may be partially visible. */
1837 r.x = window_box_left (s->w, s->area);
1838 r.width = window_box_width (s->w, s->area);
1839 r.height = s->row->visible_height;
1840 }
1841
1842 if (s->clip_head)
1843 if (r.x < s->clip_head->x)
1844 {
1845 if (r.width >= s->clip_head->x - r.x)
1846 r.width -= s->clip_head->x - r.x;
1847 else
1848 r.width = 0;
1849 r.x = s->clip_head->x;
1850 }
1851 if (s->clip_tail)
1852 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1853 {
1854 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1855 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1856 else
1857 r.width = 0;
1858 }
1859
1860 /* If S draws overlapping rows, it's sufficient to use the top and
1861 bottom of the window for clipping because this glyph string
1862 intentionally draws over other lines. */
1863 if (s->for_overlaps)
1864 {
1865 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1866 r.height = window_text_bottom_y (s->w) - r.y;
1867
1868 /* Alas, the above simple strategy does not work for the
1869 environments with anti-aliased text: if the same text is
1870 drawn onto the same place multiple times, it gets thicker.
1871 If the overlap we are processing is for the erased cursor, we
1872 take the intersection with the rectagle of the cursor. */
1873 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1874 {
1875 XRectangle rc, r_save = r;
1876
1877 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1878 rc.y = s->w->phys_cursor.y;
1879 rc.width = s->w->phys_cursor_width;
1880 rc.height = s->w->phys_cursor_height;
1881
1882 x_intersect_rectangles (&r_save, &rc, &r);
1883 }
1884 }
1885 else
1886 {
1887 /* Don't use S->y for clipping because it doesn't take partially
1888 visible lines into account. For example, it can be negative for
1889 partially visible lines at the top of a window. */
1890 if (!s->row->full_width_p
1891 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1892 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1893 else
1894 r.y = max (0, s->row->y);
1895
1896 /* If drawing a tool-bar window, draw it over the internal border
1897 at the top of the window. */
1898 if (WINDOWP (s->f->tool_bar_window)
1899 && s->w == XWINDOW (s->f->tool_bar_window))
1900 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1901 }
1902
1903 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1904
1905 /* If drawing the cursor, don't let glyph draw outside its
1906 advertised boundaries. Cleartype does this under some circumstances. */
1907 if (s->hl == DRAW_CURSOR)
1908 {
1909 struct glyph *glyph = s->first_glyph;
1910 int height, max_y;
1911
1912 if (s->x > r.x)
1913 {
1914 r.width -= s->x - r.x;
1915 r.x = s->x;
1916 }
1917 r.width = min (r.width, glyph->pixel_width);
1918
1919 /* If r.y is below window bottom, ensure that we still see a cursor. */
1920 height = min (glyph->ascent + glyph->descent,
1921 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1922 max_y = window_text_bottom_y (s->w) - height;
1923 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1924 if (s->ybase - glyph->ascent > max_y)
1925 {
1926 r.y = max_y;
1927 r.height = height;
1928 }
1929 else
1930 {
1931 /* Don't draw cursor glyph taller than our actual glyph. */
1932 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1933 if (height < r.height)
1934 {
1935 max_y = r.y + r.height;
1936 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1937 r.height = min (max_y - r.y, height);
1938 }
1939 }
1940 }
1941
1942 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1943 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1944 {
1945 #ifdef CONVERT_FROM_XRECT
1946 CONVERT_FROM_XRECT (r, *rects);
1947 #else
1948 *rects = r;
1949 #endif
1950 return 1;
1951 }
1952 else
1953 {
1954 /* If we are processing overlapping and allowed to return
1955 multiple clipping rectangles, we exclude the row of the glyph
1956 string from the clipping rectangle. This is to avoid drawing
1957 the same text on the environment with anti-aliasing. */
1958 #ifdef CONVERT_FROM_XRECT
1959 XRectangle rs[2];
1960 #else
1961 XRectangle *rs = rects;
1962 #endif
1963 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1964
1965 if (s->for_overlaps & OVERLAPS_PRED)
1966 {
1967 rs[i] = r;
1968 if (r.y + r.height > row_y)
1969 {
1970 if (r.y < row_y)
1971 rs[i].height = row_y - r.y;
1972 else
1973 rs[i].height = 0;
1974 }
1975 i++;
1976 }
1977 if (s->for_overlaps & OVERLAPS_SUCC)
1978 {
1979 rs[i] = r;
1980 if (r.y < row_y + s->row->visible_height)
1981 {
1982 if (r.y + r.height > row_y + s->row->visible_height)
1983 {
1984 rs[i].y = row_y + s->row->visible_height;
1985 rs[i].height = r.y + r.height - rs[i].y;
1986 }
1987 else
1988 rs[i].height = 0;
1989 }
1990 i++;
1991 }
1992
1993 n = i;
1994 #ifdef CONVERT_FROM_XRECT
1995 for (i = 0; i < n; i++)
1996 CONVERT_FROM_XRECT (rs[i], rects[i]);
1997 #endif
1998 return n;
1999 }
2000 }
2001
2002 /* EXPORT:
2003 Return in *NR the clipping rectangle for glyph string S. */
2004
2005 void
2006 get_glyph_string_clip_rect (s, nr)
2007 struct glyph_string *s;
2008 NativeRectangle *nr;
2009 {
2010 get_glyph_string_clip_rects (s, nr, 1);
2011 }
2012
2013
2014 /* EXPORT:
2015 Return the position and height of the phys cursor in window W.
2016 Set w->phys_cursor_width to width of phys cursor.
2017 */
2018
2019 void
2020 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2021 struct window *w;
2022 struct glyph_row *row;
2023 struct glyph *glyph;
2024 int *xp, *yp, *heightp;
2025 {
2026 struct frame *f = XFRAME (WINDOW_FRAME (w));
2027 int x, y, wd, h, h0, y0;
2028
2029 /* Compute the width of the rectangle to draw. If on a stretch
2030 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2031 rectangle as wide as the glyph, but use a canonical character
2032 width instead. */
2033 wd = glyph->pixel_width - 1;
2034 #ifdef HAVE_NTGUI
2035 wd++; /* Why? */
2036 #endif
2037
2038 x = w->phys_cursor.x;
2039 if (x < 0)
2040 {
2041 wd += x;
2042 x = 0;
2043 }
2044
2045 if (glyph->type == STRETCH_GLYPH
2046 && !x_stretch_cursor_p)
2047 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2048 w->phys_cursor_width = wd;
2049
2050 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2051
2052 /* If y is below window bottom, ensure that we still see a cursor. */
2053 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2054
2055 h = max (h0, glyph->ascent + glyph->descent);
2056 h0 = min (h0, glyph->ascent + glyph->descent);
2057
2058 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2059 if (y < y0)
2060 {
2061 h = max (h - (y0 - y) + 1, h0);
2062 y = y0 - 1;
2063 }
2064 else
2065 {
2066 y0 = window_text_bottom_y (w) - h0;
2067 if (y > y0)
2068 {
2069 h += y - y0;
2070 y = y0;
2071 }
2072 }
2073
2074 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2075 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2076 *heightp = h;
2077 }
2078
2079 /*
2080 * Remember which glyph the mouse is over.
2081 */
2082
2083 void
2084 remember_mouse_glyph (f, gx, gy, rect)
2085 struct frame *f;
2086 int gx, gy;
2087 NativeRectangle *rect;
2088 {
2089 Lisp_Object window;
2090 struct window *w;
2091 struct glyph_row *r, *gr, *end_row;
2092 enum window_part part;
2093 enum glyph_row_area area;
2094 int x, y, width, height;
2095
2096 /* Try to determine frame pixel position and size of the glyph under
2097 frame pixel coordinates X/Y on frame F. */
2098
2099 if (!f->glyphs_initialized_p
2100 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2101 NILP (window)))
2102 {
2103 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2104 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2105 goto virtual_glyph;
2106 }
2107
2108 w = XWINDOW (window);
2109 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2110 height = WINDOW_FRAME_LINE_HEIGHT (w);
2111
2112 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2113 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2114
2115 if (w->pseudo_window_p)
2116 {
2117 area = TEXT_AREA;
2118 part = ON_MODE_LINE; /* Don't adjust margin. */
2119 goto text_glyph;
2120 }
2121
2122 switch (part)
2123 {
2124 case ON_LEFT_MARGIN:
2125 area = LEFT_MARGIN_AREA;
2126 goto text_glyph;
2127
2128 case ON_RIGHT_MARGIN:
2129 area = RIGHT_MARGIN_AREA;
2130 goto text_glyph;
2131
2132 case ON_HEADER_LINE:
2133 case ON_MODE_LINE:
2134 gr = (part == ON_HEADER_LINE
2135 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2136 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2137 gy = gr->y;
2138 area = TEXT_AREA;
2139 goto text_glyph_row_found;
2140
2141 case ON_TEXT:
2142 area = TEXT_AREA;
2143
2144 text_glyph:
2145 gr = 0; gy = 0;
2146 for (; r <= end_row && r->enabled_p; ++r)
2147 if (r->y + r->height > y)
2148 {
2149 gr = r; gy = r->y;
2150 break;
2151 }
2152
2153 text_glyph_row_found:
2154 if (gr && gy <= y)
2155 {
2156 struct glyph *g = gr->glyphs[area];
2157 struct glyph *end = g + gr->used[area];
2158
2159 height = gr->height;
2160 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2161 if (gx + g->pixel_width > x)
2162 break;
2163
2164 if (g < end)
2165 {
2166 if (g->type == IMAGE_GLYPH)
2167 {
2168 /* Don't remember when mouse is over image, as
2169 image may have hot-spots. */
2170 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2171 return;
2172 }
2173 width = g->pixel_width;
2174 }
2175 else
2176 {
2177 /* Use nominal char spacing at end of line. */
2178 x -= gx;
2179 gx += (x / width) * width;
2180 }
2181
2182 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2183 gx += window_box_left_offset (w, area);
2184 }
2185 else
2186 {
2187 /* Use nominal line height at end of window. */
2188 gx = (x / width) * width;
2189 y -= gy;
2190 gy += (y / height) * height;
2191 }
2192 break;
2193
2194 case ON_LEFT_FRINGE:
2195 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2196 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2197 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2198 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2199 goto row_glyph;
2200
2201 case ON_RIGHT_FRINGE:
2202 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2203 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2204 : window_box_right_offset (w, TEXT_AREA));
2205 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2206 goto row_glyph;
2207
2208 case ON_SCROLL_BAR:
2209 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2210 ? 0
2211 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2212 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2213 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2214 : 0)));
2215 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2216
2217 row_glyph:
2218 gr = 0, gy = 0;
2219 for (; r <= end_row && r->enabled_p; ++r)
2220 if (r->y + r->height > y)
2221 {
2222 gr = r; gy = r->y;
2223 break;
2224 }
2225
2226 if (gr && gy <= y)
2227 height = gr->height;
2228 else
2229 {
2230 /* Use nominal line height at end of window. */
2231 y -= gy;
2232 gy += (y / height) * height;
2233 }
2234 break;
2235
2236 default:
2237 ;
2238 virtual_glyph:
2239 /* If there is no glyph under the mouse, then we divide the screen
2240 into a grid of the smallest glyph in the frame, and use that
2241 as our "glyph". */
2242
2243 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2244 round down even for negative values. */
2245 if (gx < 0)
2246 gx -= width - 1;
2247 if (gy < 0)
2248 gy -= height - 1;
2249
2250 gx = (gx / width) * width;
2251 gy = (gy / height) * height;
2252
2253 goto store_rect;
2254 }
2255
2256 gx += WINDOW_LEFT_EDGE_X (w);
2257 gy += WINDOW_TOP_EDGE_Y (w);
2258
2259 store_rect:
2260 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2261
2262 /* Visible feedback for debugging. */
2263 #if 0
2264 #if HAVE_X_WINDOWS
2265 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2266 f->output_data.x->normal_gc,
2267 gx, gy, width, height);
2268 #endif
2269 #endif
2270 }
2271
2272
2273 #endif /* HAVE_WINDOW_SYSTEM */
2274
2275 \f
2276 /***********************************************************************
2277 Lisp form evaluation
2278 ***********************************************************************/
2279
2280 /* Error handler for safe_eval and safe_call. */
2281
2282 static Lisp_Object
2283 safe_eval_handler (arg)
2284 Lisp_Object arg;
2285 {
2286 add_to_log ("Error during redisplay: %s", arg, Qnil);
2287 return Qnil;
2288 }
2289
2290
2291 /* Evaluate SEXPR and return the result, or nil if something went
2292 wrong. Prevent redisplay during the evaluation. */
2293
2294 Lisp_Object
2295 safe_eval (sexpr)
2296 Lisp_Object sexpr;
2297 {
2298 Lisp_Object val;
2299
2300 if (inhibit_eval_during_redisplay)
2301 val = Qnil;
2302 else
2303 {
2304 int count = SPECPDL_INDEX ();
2305 struct gcpro gcpro1;
2306
2307 GCPRO1 (sexpr);
2308 specbind (Qinhibit_redisplay, Qt);
2309 /* Use Qt to ensure debugger does not run,
2310 so there is no possibility of wanting to redisplay. */
2311 val = internal_condition_case_1 (Feval, sexpr, Qt,
2312 safe_eval_handler);
2313 UNGCPRO;
2314 val = unbind_to (count, val);
2315 }
2316
2317 return val;
2318 }
2319
2320
2321 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2322 Return the result, or nil if something went wrong. Prevent
2323 redisplay during the evaluation. */
2324
2325 Lisp_Object
2326 safe_call (nargs, args)
2327 int nargs;
2328 Lisp_Object *args;
2329 {
2330 Lisp_Object val;
2331
2332 if (inhibit_eval_during_redisplay)
2333 val = Qnil;
2334 else
2335 {
2336 int count = SPECPDL_INDEX ();
2337 struct gcpro gcpro1;
2338
2339 GCPRO1 (args[0]);
2340 gcpro1.nvars = nargs;
2341 specbind (Qinhibit_redisplay, Qt);
2342 /* Use Qt to ensure debugger does not run,
2343 so there is no possibility of wanting to redisplay. */
2344 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2345 safe_eval_handler);
2346 UNGCPRO;
2347 val = unbind_to (count, val);
2348 }
2349
2350 return val;
2351 }
2352
2353
2354 /* Call function FN with one argument ARG.
2355 Return the result, or nil if something went wrong. */
2356
2357 Lisp_Object
2358 safe_call1 (fn, arg)
2359 Lisp_Object fn, arg;
2360 {
2361 Lisp_Object args[2];
2362 args[0] = fn;
2363 args[1] = arg;
2364 return safe_call (2, args);
2365 }
2366
2367
2368 \f
2369 /***********************************************************************
2370 Debugging
2371 ***********************************************************************/
2372
2373 #if 0
2374
2375 /* Define CHECK_IT to perform sanity checks on iterators.
2376 This is for debugging. It is too slow to do unconditionally. */
2377
2378 static void
2379 check_it (it)
2380 struct it *it;
2381 {
2382 if (it->method == GET_FROM_STRING)
2383 {
2384 xassert (STRINGP (it->string));
2385 xassert (IT_STRING_CHARPOS (*it) >= 0);
2386 }
2387 else
2388 {
2389 xassert (IT_STRING_CHARPOS (*it) < 0);
2390 if (it->method == GET_FROM_BUFFER)
2391 {
2392 /* Check that character and byte positions agree. */
2393 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2394 }
2395 }
2396
2397 if (it->dpvec)
2398 xassert (it->current.dpvec_index >= 0);
2399 else
2400 xassert (it->current.dpvec_index < 0);
2401 }
2402
2403 #define CHECK_IT(IT) check_it ((IT))
2404
2405 #else /* not 0 */
2406
2407 #define CHECK_IT(IT) (void) 0
2408
2409 #endif /* not 0 */
2410
2411
2412 #if GLYPH_DEBUG
2413
2414 /* Check that the window end of window W is what we expect it
2415 to be---the last row in the current matrix displaying text. */
2416
2417 static void
2418 check_window_end (w)
2419 struct window *w;
2420 {
2421 if (!MINI_WINDOW_P (w)
2422 && !NILP (w->window_end_valid))
2423 {
2424 struct glyph_row *row;
2425 xassert ((row = MATRIX_ROW (w->current_matrix,
2426 XFASTINT (w->window_end_vpos)),
2427 !row->enabled_p
2428 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2429 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2430 }
2431 }
2432
2433 #define CHECK_WINDOW_END(W) check_window_end ((W))
2434
2435 #else /* not GLYPH_DEBUG */
2436
2437 #define CHECK_WINDOW_END(W) (void) 0
2438
2439 #endif /* not GLYPH_DEBUG */
2440
2441
2442 \f
2443 /***********************************************************************
2444 Iterator initialization
2445 ***********************************************************************/
2446
2447 /* Initialize IT for displaying current_buffer in window W, starting
2448 at character position CHARPOS. CHARPOS < 0 means that no buffer
2449 position is specified which is useful when the iterator is assigned
2450 a position later. BYTEPOS is the byte position corresponding to
2451 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2452
2453 If ROW is not null, calls to produce_glyphs with IT as parameter
2454 will produce glyphs in that row.
2455
2456 BASE_FACE_ID is the id of a base face to use. It must be one of
2457 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2458 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2459 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2460
2461 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2462 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2463 will be initialized to use the corresponding mode line glyph row of
2464 the desired matrix of W. */
2465
2466 void
2467 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2468 struct it *it;
2469 struct window *w;
2470 int charpos, bytepos;
2471 struct glyph_row *row;
2472 enum face_id base_face_id;
2473 {
2474 int highlight_region_p;
2475
2476 /* Some precondition checks. */
2477 xassert (w != NULL && it != NULL);
2478 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2479 && charpos <= ZV));
2480
2481 /* If face attributes have been changed since the last redisplay,
2482 free realized faces now because they depend on face definitions
2483 that might have changed. Don't free faces while there might be
2484 desired matrices pending which reference these faces. */
2485 if (face_change_count && !inhibit_free_realized_faces)
2486 {
2487 face_change_count = 0;
2488 free_all_realized_faces (Qnil);
2489 }
2490
2491 /* Use one of the mode line rows of W's desired matrix if
2492 appropriate. */
2493 if (row == NULL)
2494 {
2495 if (base_face_id == MODE_LINE_FACE_ID
2496 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2497 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2498 else if (base_face_id == HEADER_LINE_FACE_ID)
2499 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2500 }
2501
2502 /* Clear IT. */
2503 bzero (it, sizeof *it);
2504 it->current.overlay_string_index = -1;
2505 it->current.dpvec_index = -1;
2506 it->base_face_id = base_face_id;
2507 it->string = Qnil;
2508 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2509
2510 /* The window in which we iterate over current_buffer: */
2511 XSETWINDOW (it->window, w);
2512 it->w = w;
2513 it->f = XFRAME (w->frame);
2514
2515 /* Extra space between lines (on window systems only). */
2516 if (base_face_id == DEFAULT_FACE_ID
2517 && FRAME_WINDOW_P (it->f))
2518 {
2519 if (NATNUMP (current_buffer->extra_line_spacing))
2520 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2521 else if (FLOATP (current_buffer->extra_line_spacing))
2522 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2523 * FRAME_LINE_HEIGHT (it->f));
2524 else if (it->f->extra_line_spacing > 0)
2525 it->extra_line_spacing = it->f->extra_line_spacing;
2526 it->max_extra_line_spacing = 0;
2527 }
2528
2529 /* If realized faces have been removed, e.g. because of face
2530 attribute changes of named faces, recompute them. When running
2531 in batch mode, the face cache of Vterminal_frame is null. If
2532 we happen to get called, make a dummy face cache. */
2533 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2534 init_frame_faces (it->f);
2535 if (FRAME_FACE_CACHE (it->f)->used == 0)
2536 recompute_basic_faces (it->f);
2537
2538 /* Current value of the `slice', `space-width', and 'height' properties. */
2539 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2540 it->space_width = Qnil;
2541 it->font_height = Qnil;
2542 it->override_ascent = -1;
2543
2544 /* Are control characters displayed as `^C'? */
2545 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2546
2547 /* -1 means everything between a CR and the following line end
2548 is invisible. >0 means lines indented more than this value are
2549 invisible. */
2550 it->selective = (INTEGERP (current_buffer->selective_display)
2551 ? XFASTINT (current_buffer->selective_display)
2552 : (!NILP (current_buffer->selective_display)
2553 ? -1 : 0));
2554 it->selective_display_ellipsis_p
2555 = !NILP (current_buffer->selective_display_ellipses);
2556
2557 /* Display table to use. */
2558 it->dp = window_display_table (w);
2559
2560 /* Are multibyte characters enabled in current_buffer? */
2561 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2562
2563 /* Non-zero if we should highlight the region. */
2564 highlight_region_p
2565 = (!NILP (Vtransient_mark_mode)
2566 && !NILP (current_buffer->mark_active)
2567 && XMARKER (current_buffer->mark)->buffer != 0);
2568
2569 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2570 start and end of a visible region in window IT->w. Set both to
2571 -1 to indicate no region. */
2572 if (highlight_region_p
2573 /* Maybe highlight only in selected window. */
2574 && (/* Either show region everywhere. */
2575 highlight_nonselected_windows
2576 /* Or show region in the selected window. */
2577 || w == XWINDOW (selected_window)
2578 /* Or show the region if we are in the mini-buffer and W is
2579 the window the mini-buffer refers to. */
2580 || (MINI_WINDOW_P (XWINDOW (selected_window))
2581 && WINDOWP (minibuf_selected_window)
2582 && w == XWINDOW (minibuf_selected_window))))
2583 {
2584 int charpos = marker_position (current_buffer->mark);
2585 it->region_beg_charpos = min (PT, charpos);
2586 it->region_end_charpos = max (PT, charpos);
2587 }
2588 else
2589 it->region_beg_charpos = it->region_end_charpos = -1;
2590
2591 /* Get the position at which the redisplay_end_trigger hook should
2592 be run, if it is to be run at all. */
2593 if (MARKERP (w->redisplay_end_trigger)
2594 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2595 it->redisplay_end_trigger_charpos
2596 = marker_position (w->redisplay_end_trigger);
2597 else if (INTEGERP (w->redisplay_end_trigger))
2598 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2599
2600 /* Correct bogus values of tab_width. */
2601 it->tab_width = XINT (current_buffer->tab_width);
2602 if (it->tab_width <= 0 || it->tab_width > 1000)
2603 it->tab_width = 8;
2604
2605 /* Are lines in the display truncated? */
2606 it->truncate_lines_p
2607 = (base_face_id != DEFAULT_FACE_ID
2608 || XINT (it->w->hscroll)
2609 || (truncate_partial_width_windows
2610 && !WINDOW_FULL_WIDTH_P (it->w))
2611 || !NILP (current_buffer->truncate_lines));
2612
2613 /* Get dimensions of truncation and continuation glyphs. These are
2614 displayed as fringe bitmaps under X, so we don't need them for such
2615 frames. */
2616 if (!FRAME_WINDOW_P (it->f))
2617 {
2618 if (it->truncate_lines_p)
2619 {
2620 /* We will need the truncation glyph. */
2621 xassert (it->glyph_row == NULL);
2622 produce_special_glyphs (it, IT_TRUNCATION);
2623 it->truncation_pixel_width = it->pixel_width;
2624 }
2625 else
2626 {
2627 /* We will need the continuation glyph. */
2628 xassert (it->glyph_row == NULL);
2629 produce_special_glyphs (it, IT_CONTINUATION);
2630 it->continuation_pixel_width = it->pixel_width;
2631 }
2632
2633 /* Reset these values to zero because the produce_special_glyphs
2634 above has changed them. */
2635 it->pixel_width = it->ascent = it->descent = 0;
2636 it->phys_ascent = it->phys_descent = 0;
2637 }
2638
2639 /* Set this after getting the dimensions of truncation and
2640 continuation glyphs, so that we don't produce glyphs when calling
2641 produce_special_glyphs, above. */
2642 it->glyph_row = row;
2643 it->area = TEXT_AREA;
2644
2645 /* Get the dimensions of the display area. The display area
2646 consists of the visible window area plus a horizontally scrolled
2647 part to the left of the window. All x-values are relative to the
2648 start of this total display area. */
2649 if (base_face_id != DEFAULT_FACE_ID)
2650 {
2651 /* Mode lines, menu bar in terminal frames. */
2652 it->first_visible_x = 0;
2653 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2654 }
2655 else
2656 {
2657 it->first_visible_x
2658 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2659 it->last_visible_x = (it->first_visible_x
2660 + window_box_width (w, TEXT_AREA));
2661
2662 /* If we truncate lines, leave room for the truncator glyph(s) at
2663 the right margin. Otherwise, leave room for the continuation
2664 glyph(s). Truncation and continuation glyphs are not inserted
2665 for window-based redisplay. */
2666 if (!FRAME_WINDOW_P (it->f))
2667 {
2668 if (it->truncate_lines_p)
2669 it->last_visible_x -= it->truncation_pixel_width;
2670 else
2671 it->last_visible_x -= it->continuation_pixel_width;
2672 }
2673
2674 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2675 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2676 }
2677
2678 /* Leave room for a border glyph. */
2679 if (!FRAME_WINDOW_P (it->f)
2680 && !WINDOW_RIGHTMOST_P (it->w))
2681 it->last_visible_x -= 1;
2682
2683 it->last_visible_y = window_text_bottom_y (w);
2684
2685 /* For mode lines and alike, arrange for the first glyph having a
2686 left box line if the face specifies a box. */
2687 if (base_face_id != DEFAULT_FACE_ID)
2688 {
2689 struct face *face;
2690
2691 it->face_id = base_face_id;
2692
2693 /* If we have a boxed mode line, make the first character appear
2694 with a left box line. */
2695 face = FACE_FROM_ID (it->f, base_face_id);
2696 if (face->box != FACE_NO_BOX)
2697 it->start_of_box_run_p = 1;
2698 }
2699
2700 /* If a buffer position was specified, set the iterator there,
2701 getting overlays and face properties from that position. */
2702 if (charpos >= BUF_BEG (current_buffer))
2703 {
2704 it->end_charpos = ZV;
2705 it->face_id = -1;
2706 IT_CHARPOS (*it) = charpos;
2707
2708 /* Compute byte position if not specified. */
2709 if (bytepos < charpos)
2710 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2711 else
2712 IT_BYTEPOS (*it) = bytepos;
2713
2714 it->start = it->current;
2715
2716 /* Compute faces etc. */
2717 reseat (it, it->current.pos, 1);
2718 }
2719
2720 CHECK_IT (it);
2721 }
2722
2723
2724 /* Initialize IT for the display of window W with window start POS. */
2725
2726 void
2727 start_display (it, w, pos)
2728 struct it *it;
2729 struct window *w;
2730 struct text_pos pos;
2731 {
2732 struct glyph_row *row;
2733 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2734
2735 row = w->desired_matrix->rows + first_vpos;
2736 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2737 it->first_vpos = first_vpos;
2738
2739 /* Don't reseat to previous visible line start if current start
2740 position is in a string or image. */
2741 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2742 {
2743 int start_at_line_beg_p;
2744 int first_y = it->current_y;
2745
2746 /* If window start is not at a line start, skip forward to POS to
2747 get the correct continuation lines width. */
2748 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2749 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2750 if (!start_at_line_beg_p)
2751 {
2752 int new_x;
2753
2754 reseat_at_previous_visible_line_start (it);
2755 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2756
2757 new_x = it->current_x + it->pixel_width;
2758
2759 /* If lines are continued, this line may end in the middle
2760 of a multi-glyph character (e.g. a control character
2761 displayed as \003, or in the middle of an overlay
2762 string). In this case move_it_to above will not have
2763 taken us to the start of the continuation line but to the
2764 end of the continued line. */
2765 if (it->current_x > 0
2766 && !it->truncate_lines_p /* Lines are continued. */
2767 && (/* And glyph doesn't fit on the line. */
2768 new_x > it->last_visible_x
2769 /* Or it fits exactly and we're on a window
2770 system frame. */
2771 || (new_x == it->last_visible_x
2772 && FRAME_WINDOW_P (it->f))))
2773 {
2774 if (it->current.dpvec_index >= 0
2775 || it->current.overlay_string_index >= 0)
2776 {
2777 set_iterator_to_next (it, 1);
2778 move_it_in_display_line_to (it, -1, -1, 0);
2779 }
2780
2781 it->continuation_lines_width += it->current_x;
2782 }
2783
2784 /* We're starting a new display line, not affected by the
2785 height of the continued line, so clear the appropriate
2786 fields in the iterator structure. */
2787 it->max_ascent = it->max_descent = 0;
2788 it->max_phys_ascent = it->max_phys_descent = 0;
2789
2790 it->current_y = first_y;
2791 it->vpos = 0;
2792 it->current_x = it->hpos = 0;
2793 }
2794 }
2795
2796 #if 0 /* Don't assert the following because start_display is sometimes
2797 called intentionally with a window start that is not at a
2798 line start. Please leave this code in as a comment. */
2799
2800 /* Window start should be on a line start, now. */
2801 xassert (it->continuation_lines_width
2802 || IT_CHARPOS (it) == BEGV
2803 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2804 #endif /* 0 */
2805 }
2806
2807
2808 /* Return 1 if POS is a position in ellipses displayed for invisible
2809 text. W is the window we display, for text property lookup. */
2810
2811 static int
2812 in_ellipses_for_invisible_text_p (pos, w)
2813 struct display_pos *pos;
2814 struct window *w;
2815 {
2816 Lisp_Object prop, window;
2817 int ellipses_p = 0;
2818 int charpos = CHARPOS (pos->pos);
2819
2820 /* If POS specifies a position in a display vector, this might
2821 be for an ellipsis displayed for invisible text. We won't
2822 get the iterator set up for delivering that ellipsis unless
2823 we make sure that it gets aware of the invisible text. */
2824 if (pos->dpvec_index >= 0
2825 && pos->overlay_string_index < 0
2826 && CHARPOS (pos->string_pos) < 0
2827 && charpos > BEGV
2828 && (XSETWINDOW (window, w),
2829 prop = Fget_char_property (make_number (charpos),
2830 Qinvisible, window),
2831 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2832 {
2833 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2834 window);
2835 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2836 }
2837
2838 return ellipses_p;
2839 }
2840
2841
2842 /* Initialize IT for stepping through current_buffer in window W,
2843 starting at position POS that includes overlay string and display
2844 vector/ control character translation position information. Value
2845 is zero if there are overlay strings with newlines at POS. */
2846
2847 static int
2848 init_from_display_pos (it, w, pos)
2849 struct it *it;
2850 struct window *w;
2851 struct display_pos *pos;
2852 {
2853 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2854 int i, overlay_strings_with_newlines = 0;
2855
2856 /* If POS specifies a position in a display vector, this might
2857 be for an ellipsis displayed for invisible text. We won't
2858 get the iterator set up for delivering that ellipsis unless
2859 we make sure that it gets aware of the invisible text. */
2860 if (in_ellipses_for_invisible_text_p (pos, w))
2861 {
2862 --charpos;
2863 bytepos = 0;
2864 }
2865
2866 /* Keep in mind: the call to reseat in init_iterator skips invisible
2867 text, so we might end up at a position different from POS. This
2868 is only a problem when POS is a row start after a newline and an
2869 overlay starts there with an after-string, and the overlay has an
2870 invisible property. Since we don't skip invisible text in
2871 display_line and elsewhere immediately after consuming the
2872 newline before the row start, such a POS will not be in a string,
2873 but the call to init_iterator below will move us to the
2874 after-string. */
2875 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2876
2877 /* This only scans the current chunk -- it should scan all chunks.
2878 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2879 to 16 in 22.1 to make this a lesser problem. */
2880 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2881 {
2882 const char *s = SDATA (it->overlay_strings[i]);
2883 const char *e = s + SBYTES (it->overlay_strings[i]);
2884
2885 while (s < e && *s != '\n')
2886 ++s;
2887
2888 if (s < e)
2889 {
2890 overlay_strings_with_newlines = 1;
2891 break;
2892 }
2893 }
2894
2895 /* If position is within an overlay string, set up IT to the right
2896 overlay string. */
2897 if (pos->overlay_string_index >= 0)
2898 {
2899 int relative_index;
2900
2901 /* If the first overlay string happens to have a `display'
2902 property for an image, the iterator will be set up for that
2903 image, and we have to undo that setup first before we can
2904 correct the overlay string index. */
2905 if (it->method == GET_FROM_IMAGE)
2906 pop_it (it);
2907
2908 /* We already have the first chunk of overlay strings in
2909 IT->overlay_strings. Load more until the one for
2910 pos->overlay_string_index is in IT->overlay_strings. */
2911 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2912 {
2913 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2914 it->current.overlay_string_index = 0;
2915 while (n--)
2916 {
2917 load_overlay_strings (it, 0);
2918 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2919 }
2920 }
2921
2922 it->current.overlay_string_index = pos->overlay_string_index;
2923 relative_index = (it->current.overlay_string_index
2924 % OVERLAY_STRING_CHUNK_SIZE);
2925 it->string = it->overlay_strings[relative_index];
2926 xassert (STRINGP (it->string));
2927 it->current.string_pos = pos->string_pos;
2928 it->method = GET_FROM_STRING;
2929 }
2930
2931 #if 0 /* This is bogus because POS not having an overlay string
2932 position does not mean it's after the string. Example: A
2933 line starting with a before-string and initialization of IT
2934 to the previous row's end position. */
2935 else if (it->current.overlay_string_index >= 0)
2936 {
2937 /* If POS says we're already after an overlay string ending at
2938 POS, make sure to pop the iterator because it will be in
2939 front of that overlay string. When POS is ZV, we've thereby
2940 also ``processed'' overlay strings at ZV. */
2941 while (it->sp)
2942 pop_it (it);
2943 xassert (it->current.overlay_string_index == -1);
2944 xassert (it->method == GET_FROM_BUFFER);
2945 if (CHARPOS (pos->pos) == ZV)
2946 it->overlay_strings_at_end_processed_p = 1;
2947 }
2948 #endif /* 0 */
2949
2950 if (CHARPOS (pos->string_pos) >= 0)
2951 {
2952 /* Recorded position is not in an overlay string, but in another
2953 string. This can only be a string from a `display' property.
2954 IT should already be filled with that string. */
2955 it->current.string_pos = pos->string_pos;
2956 xassert (STRINGP (it->string));
2957 }
2958
2959 /* Restore position in display vector translations, control
2960 character translations or ellipses. */
2961 if (pos->dpvec_index >= 0)
2962 {
2963 if (it->dpvec == NULL)
2964 get_next_display_element (it);
2965 xassert (it->dpvec && it->current.dpvec_index == 0);
2966 it->current.dpvec_index = pos->dpvec_index;
2967 }
2968
2969 CHECK_IT (it);
2970 return !overlay_strings_with_newlines;
2971 }
2972
2973
2974 /* Initialize IT for stepping through current_buffer in window W
2975 starting at ROW->start. */
2976
2977 static void
2978 init_to_row_start (it, w, row)
2979 struct it *it;
2980 struct window *w;
2981 struct glyph_row *row;
2982 {
2983 init_from_display_pos (it, w, &row->start);
2984 it->start = row->start;
2985 it->continuation_lines_width = row->continuation_lines_width;
2986 CHECK_IT (it);
2987 }
2988
2989
2990 /* Initialize IT for stepping through current_buffer in window W
2991 starting in the line following ROW, i.e. starting at ROW->end.
2992 Value is zero if there are overlay strings with newlines at ROW's
2993 end position. */
2994
2995 static int
2996 init_to_row_end (it, w, row)
2997 struct it *it;
2998 struct window *w;
2999 struct glyph_row *row;
3000 {
3001 int success = 0;
3002
3003 if (init_from_display_pos (it, w, &row->end))
3004 {
3005 if (row->continued_p)
3006 it->continuation_lines_width
3007 = row->continuation_lines_width + row->pixel_width;
3008 CHECK_IT (it);
3009 success = 1;
3010 }
3011
3012 return success;
3013 }
3014
3015
3016
3017 \f
3018 /***********************************************************************
3019 Text properties
3020 ***********************************************************************/
3021
3022 /* Called when IT reaches IT->stop_charpos. Handle text property and
3023 overlay changes. Set IT->stop_charpos to the next position where
3024 to stop. */
3025
3026 static void
3027 handle_stop (it)
3028 struct it *it;
3029 {
3030 enum prop_handled handled;
3031 int handle_overlay_change_p;
3032 struct props *p;
3033
3034 it->dpvec = NULL;
3035 it->current.dpvec_index = -1;
3036 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3037 it->ignore_overlay_strings_at_pos_p = 0;
3038
3039 /* Use face of preceding text for ellipsis (if invisible) */
3040 if (it->selective_display_ellipsis_p)
3041 it->saved_face_id = it->face_id;
3042
3043 do
3044 {
3045 handled = HANDLED_NORMALLY;
3046
3047 /* Call text property handlers. */
3048 for (p = it_props; p->handler; ++p)
3049 {
3050 handled = p->handler (it);
3051
3052 if (handled == HANDLED_RECOMPUTE_PROPS)
3053 break;
3054 else if (handled == HANDLED_RETURN)
3055 {
3056 /* We still want to show before and after strings from
3057 overlays even if the actual buffer text is replaced. */
3058 if (!handle_overlay_change_p || it->sp > 1)
3059 return;
3060 if (!get_overlay_strings_1 (it, 0, 0))
3061 return;
3062 it->ignore_overlay_strings_at_pos_p = 1;
3063 it->string_from_display_prop_p = 0;
3064 handle_overlay_change_p = 0;
3065 handled = HANDLED_RECOMPUTE_PROPS;
3066 break;
3067 }
3068 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3069 handle_overlay_change_p = 0;
3070 }
3071
3072 if (handled != HANDLED_RECOMPUTE_PROPS)
3073 {
3074 /* Don't check for overlay strings below when set to deliver
3075 characters from a display vector. */
3076 if (it->method == GET_FROM_DISPLAY_VECTOR)
3077 handle_overlay_change_p = 0;
3078
3079 /* Handle overlay changes.
3080 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3081 if it finds overlays. */
3082 if (handle_overlay_change_p)
3083 handled = handle_overlay_change (it);
3084 }
3085 }
3086 while (handled == HANDLED_RECOMPUTE_PROPS);
3087
3088 /* Determine where to stop next. */
3089 if (handled == HANDLED_NORMALLY)
3090 compute_stop_pos (it);
3091 }
3092
3093
3094 /* Compute IT->stop_charpos from text property and overlay change
3095 information for IT's current position. */
3096
3097 static void
3098 compute_stop_pos (it)
3099 struct it *it;
3100 {
3101 register INTERVAL iv, next_iv;
3102 Lisp_Object object, limit, position;
3103
3104 /* If nowhere else, stop at the end. */
3105 it->stop_charpos = it->end_charpos;
3106
3107 if (STRINGP (it->string))
3108 {
3109 /* Strings are usually short, so don't limit the search for
3110 properties. */
3111 object = it->string;
3112 limit = Qnil;
3113 position = make_number (IT_STRING_CHARPOS (*it));
3114 }
3115 else
3116 {
3117 int charpos;
3118
3119 /* If next overlay change is in front of the current stop pos
3120 (which is IT->end_charpos), stop there. Note: value of
3121 next_overlay_change is point-max if no overlay change
3122 follows. */
3123 charpos = next_overlay_change (IT_CHARPOS (*it));
3124 if (charpos < it->stop_charpos)
3125 it->stop_charpos = charpos;
3126
3127 /* If showing the region, we have to stop at the region
3128 start or end because the face might change there. */
3129 if (it->region_beg_charpos > 0)
3130 {
3131 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3132 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3133 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3134 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3135 }
3136
3137 /* Set up variables for computing the stop position from text
3138 property changes. */
3139 XSETBUFFER (object, current_buffer);
3140 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3141 position = make_number (IT_CHARPOS (*it));
3142
3143 }
3144
3145 /* Get the interval containing IT's position. Value is a null
3146 interval if there isn't such an interval. */
3147 iv = validate_interval_range (object, &position, &position, 0);
3148 if (!NULL_INTERVAL_P (iv))
3149 {
3150 Lisp_Object values_here[LAST_PROP_IDX];
3151 struct props *p;
3152
3153 /* Get properties here. */
3154 for (p = it_props; p->handler; ++p)
3155 values_here[p->idx] = textget (iv->plist, *p->name);
3156
3157 /* Look for an interval following iv that has different
3158 properties. */
3159 for (next_iv = next_interval (iv);
3160 (!NULL_INTERVAL_P (next_iv)
3161 && (NILP (limit)
3162 || XFASTINT (limit) > next_iv->position));
3163 next_iv = next_interval (next_iv))
3164 {
3165 for (p = it_props; p->handler; ++p)
3166 {
3167 Lisp_Object new_value;
3168
3169 new_value = textget (next_iv->plist, *p->name);
3170 if (!EQ (values_here[p->idx], new_value))
3171 break;
3172 }
3173
3174 if (p->handler)
3175 break;
3176 }
3177
3178 if (!NULL_INTERVAL_P (next_iv))
3179 {
3180 if (INTEGERP (limit)
3181 && next_iv->position >= XFASTINT (limit))
3182 /* No text property change up to limit. */
3183 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3184 else
3185 /* Text properties change in next_iv. */
3186 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3187 }
3188 }
3189
3190 xassert (STRINGP (it->string)
3191 || (it->stop_charpos >= BEGV
3192 && it->stop_charpos >= IT_CHARPOS (*it)));
3193 }
3194
3195
3196 /* Return the position of the next overlay change after POS in
3197 current_buffer. Value is point-max if no overlay change
3198 follows. This is like `next-overlay-change' but doesn't use
3199 xmalloc. */
3200
3201 static int
3202 next_overlay_change (pos)
3203 int pos;
3204 {
3205 int noverlays;
3206 int endpos;
3207 Lisp_Object *overlays;
3208 int i;
3209
3210 /* Get all overlays at the given position. */
3211 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3212
3213 /* If any of these overlays ends before endpos,
3214 use its ending point instead. */
3215 for (i = 0; i < noverlays; ++i)
3216 {
3217 Lisp_Object oend;
3218 int oendpos;
3219
3220 oend = OVERLAY_END (overlays[i]);
3221 oendpos = OVERLAY_POSITION (oend);
3222 endpos = min (endpos, oendpos);
3223 }
3224
3225 return endpos;
3226 }
3227
3228
3229 \f
3230 /***********************************************************************
3231 Fontification
3232 ***********************************************************************/
3233
3234 /* Handle changes in the `fontified' property of the current buffer by
3235 calling hook functions from Qfontification_functions to fontify
3236 regions of text. */
3237
3238 static enum prop_handled
3239 handle_fontified_prop (it)
3240 struct it *it;
3241 {
3242 Lisp_Object prop, pos;
3243 enum prop_handled handled = HANDLED_NORMALLY;
3244
3245 if (!NILP (Vmemory_full))
3246 return handled;
3247
3248 /* Get the value of the `fontified' property at IT's current buffer
3249 position. (The `fontified' property doesn't have a special
3250 meaning in strings.) If the value is nil, call functions from
3251 Qfontification_functions. */
3252 if (!STRINGP (it->string)
3253 && it->s == NULL
3254 && !NILP (Vfontification_functions)
3255 && !NILP (Vrun_hooks)
3256 && (pos = make_number (IT_CHARPOS (*it)),
3257 prop = Fget_char_property (pos, Qfontified, Qnil),
3258 /* Ignore the special cased nil value always present at EOB since
3259 no amount of fontifying will be able to change it. */
3260 NILP (prop) && IT_CHARPOS (*it) < Z))
3261 {
3262 int count = SPECPDL_INDEX ();
3263 Lisp_Object val;
3264
3265 val = Vfontification_functions;
3266 specbind (Qfontification_functions, Qnil);
3267
3268 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3269 safe_call1 (val, pos);
3270 else
3271 {
3272 Lisp_Object globals, fn;
3273 struct gcpro gcpro1, gcpro2;
3274
3275 globals = Qnil;
3276 GCPRO2 (val, globals);
3277
3278 for (; CONSP (val); val = XCDR (val))
3279 {
3280 fn = XCAR (val);
3281
3282 if (EQ (fn, Qt))
3283 {
3284 /* A value of t indicates this hook has a local
3285 binding; it means to run the global binding too.
3286 In a global value, t should not occur. If it
3287 does, we must ignore it to avoid an endless
3288 loop. */
3289 for (globals = Fdefault_value (Qfontification_functions);
3290 CONSP (globals);
3291 globals = XCDR (globals))
3292 {
3293 fn = XCAR (globals);
3294 if (!EQ (fn, Qt))
3295 safe_call1 (fn, pos);
3296 }
3297 }
3298 else
3299 safe_call1 (fn, pos);
3300 }
3301
3302 UNGCPRO;
3303 }
3304
3305 unbind_to (count, Qnil);
3306
3307 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3308 something. This avoids an endless loop if they failed to
3309 fontify the text for which reason ever. */
3310 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3311 handled = HANDLED_RECOMPUTE_PROPS;
3312 }
3313
3314 return handled;
3315 }
3316
3317
3318 \f
3319 /***********************************************************************
3320 Faces
3321 ***********************************************************************/
3322
3323 /* Set up iterator IT from face properties at its current position.
3324 Called from handle_stop. */
3325
3326 static enum prop_handled
3327 handle_face_prop (it)
3328 struct it *it;
3329 {
3330 int new_face_id, next_stop;
3331
3332 if (!STRINGP (it->string))
3333 {
3334 new_face_id
3335 = face_at_buffer_position (it->w,
3336 IT_CHARPOS (*it),
3337 it->region_beg_charpos,
3338 it->region_end_charpos,
3339 &next_stop,
3340 (IT_CHARPOS (*it)
3341 + TEXT_PROP_DISTANCE_LIMIT),
3342 0);
3343
3344 /* Is this a start of a run of characters with box face?
3345 Caveat: this can be called for a freshly initialized
3346 iterator; face_id is -1 in this case. We know that the new
3347 face will not change until limit, i.e. if the new face has a
3348 box, all characters up to limit will have one. But, as
3349 usual, we don't know whether limit is really the end. */
3350 if (new_face_id != it->face_id)
3351 {
3352 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3353
3354 /* If new face has a box but old face has not, this is
3355 the start of a run of characters with box, i.e. it has
3356 a shadow on the left side. The value of face_id of the
3357 iterator will be -1 if this is the initial call that gets
3358 the face. In this case, we have to look in front of IT's
3359 position and see whether there is a face != new_face_id. */
3360 it->start_of_box_run_p
3361 = (new_face->box != FACE_NO_BOX
3362 && (it->face_id >= 0
3363 || IT_CHARPOS (*it) == BEG
3364 || new_face_id != face_before_it_pos (it)));
3365 it->face_box_p = new_face->box != FACE_NO_BOX;
3366 }
3367 }
3368 else
3369 {
3370 int base_face_id, bufpos;
3371 int i;
3372 Lisp_Object from_overlay
3373 = (it->current.overlay_string_index >= 0
3374 ? it->string_overlays[it->current.overlay_string_index]
3375 : Qnil);
3376
3377 /* See if we got to this string directly or indirectly from
3378 an overlay property. That includes the before-string or
3379 after-string of an overlay, strings in display properties
3380 provided by an overlay, their text properties, etc.
3381
3382 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3383 if (! NILP (from_overlay))
3384 for (i = it->sp - 1; i >= 0; i--)
3385 {
3386 if (it->stack[i].current.overlay_string_index >= 0)
3387 from_overlay
3388 = it->string_overlays[it->stack[i].current.overlay_string_index];
3389 else if (! NILP (it->stack[i].from_overlay))
3390 from_overlay = it->stack[i].from_overlay;
3391
3392 if (!NILP (from_overlay))
3393 break;
3394 }
3395
3396 if (! NILP (from_overlay))
3397 {
3398 bufpos = IT_CHARPOS (*it);
3399 /* For a string from an overlay, the base face depends
3400 only on text properties and ignores overlays. */
3401 base_face_id
3402 = face_for_overlay_string (it->w,
3403 IT_CHARPOS (*it),
3404 it->region_beg_charpos,
3405 it->region_end_charpos,
3406 &next_stop,
3407 (IT_CHARPOS (*it)
3408 + TEXT_PROP_DISTANCE_LIMIT),
3409 0,
3410 from_overlay);
3411 }
3412 else
3413 {
3414 bufpos = 0;
3415
3416 /* For strings from a `display' property, use the face at
3417 IT's current buffer position as the base face to merge
3418 with, so that overlay strings appear in the same face as
3419 surrounding text, unless they specify their own
3420 faces. */
3421 base_face_id = underlying_face_id (it);
3422 }
3423
3424 new_face_id = face_at_string_position (it->w,
3425 it->string,
3426 IT_STRING_CHARPOS (*it),
3427 bufpos,
3428 it->region_beg_charpos,
3429 it->region_end_charpos,
3430 &next_stop,
3431 base_face_id, 0);
3432
3433 #if 0 /* This shouldn't be neccessary. Let's check it. */
3434 /* If IT is used to display a mode line we would really like to
3435 use the mode line face instead of the frame's default face. */
3436 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3437 && new_face_id == DEFAULT_FACE_ID)
3438 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3439 #endif
3440
3441 /* Is this a start of a run of characters with box? Caveat:
3442 this can be called for a freshly allocated iterator; face_id
3443 is -1 is this case. We know that the new face will not
3444 change until the next check pos, i.e. if the new face has a
3445 box, all characters up to that position will have a
3446 box. But, as usual, we don't know whether that position
3447 is really the end. */
3448 if (new_face_id != it->face_id)
3449 {
3450 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3451 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3452
3453 /* If new face has a box but old face hasn't, this is the
3454 start of a run of characters with box, i.e. it has a
3455 shadow on the left side. */
3456 it->start_of_box_run_p
3457 = new_face->box && (old_face == NULL || !old_face->box);
3458 it->face_box_p = new_face->box != FACE_NO_BOX;
3459 }
3460 }
3461
3462 it->face_id = new_face_id;
3463 return HANDLED_NORMALLY;
3464 }
3465
3466
3467 /* Return the ID of the face ``underlying'' IT's current position,
3468 which is in a string. If the iterator is associated with a
3469 buffer, return the face at IT's current buffer position.
3470 Otherwise, use the iterator's base_face_id. */
3471
3472 static int
3473 underlying_face_id (it)
3474 struct it *it;
3475 {
3476 int face_id = it->base_face_id, i;
3477
3478 xassert (STRINGP (it->string));
3479
3480 for (i = it->sp - 1; i >= 0; --i)
3481 if (NILP (it->stack[i].string))
3482 face_id = it->stack[i].face_id;
3483
3484 return face_id;
3485 }
3486
3487
3488 /* Compute the face one character before or after the current position
3489 of IT. BEFORE_P non-zero means get the face in front of IT's
3490 position. Value is the id of the face. */
3491
3492 static int
3493 face_before_or_after_it_pos (it, before_p)
3494 struct it *it;
3495 int before_p;
3496 {
3497 int face_id, limit;
3498 int next_check_charpos;
3499 struct text_pos pos;
3500
3501 xassert (it->s == NULL);
3502
3503 if (STRINGP (it->string))
3504 {
3505 int bufpos, base_face_id;
3506
3507 /* No face change past the end of the string (for the case
3508 we are padding with spaces). No face change before the
3509 string start. */
3510 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3511 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3512 return it->face_id;
3513
3514 /* Set pos to the position before or after IT's current position. */
3515 if (before_p)
3516 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3517 else
3518 /* For composition, we must check the character after the
3519 composition. */
3520 pos = (it->what == IT_COMPOSITION
3521 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3522 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3523
3524 if (it->current.overlay_string_index >= 0)
3525 bufpos = IT_CHARPOS (*it);
3526 else
3527 bufpos = 0;
3528
3529 base_face_id = underlying_face_id (it);
3530
3531 /* Get the face for ASCII, or unibyte. */
3532 face_id = face_at_string_position (it->w,
3533 it->string,
3534 CHARPOS (pos),
3535 bufpos,
3536 it->region_beg_charpos,
3537 it->region_end_charpos,
3538 &next_check_charpos,
3539 base_face_id, 0);
3540
3541 /* Correct the face for charsets different from ASCII. Do it
3542 for the multibyte case only. The face returned above is
3543 suitable for unibyte text if IT->string is unibyte. */
3544 if (STRING_MULTIBYTE (it->string))
3545 {
3546 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3547 int rest = SBYTES (it->string) - BYTEPOS (pos);
3548 int c, len;
3549 struct face *face = FACE_FROM_ID (it->f, face_id);
3550
3551 c = string_char_and_length (p, rest, &len);
3552 face_id = FACE_FOR_CHAR (it->f, face, c);
3553 }
3554 }
3555 else
3556 {
3557 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3558 || (IT_CHARPOS (*it) <= BEGV && before_p))
3559 return it->face_id;
3560
3561 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3562 pos = it->current.pos;
3563
3564 if (before_p)
3565 DEC_TEXT_POS (pos, it->multibyte_p);
3566 else
3567 {
3568 if (it->what == IT_COMPOSITION)
3569 /* For composition, we must check the position after the
3570 composition. */
3571 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3572 else
3573 INC_TEXT_POS (pos, it->multibyte_p);
3574 }
3575
3576 /* Determine face for CHARSET_ASCII, or unibyte. */
3577 face_id = face_at_buffer_position (it->w,
3578 CHARPOS (pos),
3579 it->region_beg_charpos,
3580 it->region_end_charpos,
3581 &next_check_charpos,
3582 limit, 0);
3583
3584 /* Correct the face for charsets different from ASCII. Do it
3585 for the multibyte case only. The face returned above is
3586 suitable for unibyte text if current_buffer is unibyte. */
3587 if (it->multibyte_p)
3588 {
3589 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3590 struct face *face = FACE_FROM_ID (it->f, face_id);
3591 face_id = FACE_FOR_CHAR (it->f, face, c);
3592 }
3593 }
3594
3595 return face_id;
3596 }
3597
3598
3599 \f
3600 /***********************************************************************
3601 Invisible text
3602 ***********************************************************************/
3603
3604 /* Set up iterator IT from invisible properties at its current
3605 position. Called from handle_stop. */
3606
3607 static enum prop_handled
3608 handle_invisible_prop (it)
3609 struct it *it;
3610 {
3611 enum prop_handled handled = HANDLED_NORMALLY;
3612
3613 if (STRINGP (it->string))
3614 {
3615 extern Lisp_Object Qinvisible;
3616 Lisp_Object prop, end_charpos, limit, charpos;
3617
3618 /* Get the value of the invisible text property at the
3619 current position. Value will be nil if there is no such
3620 property. */
3621 charpos = make_number (IT_STRING_CHARPOS (*it));
3622 prop = Fget_text_property (charpos, Qinvisible, it->string);
3623
3624 if (!NILP (prop)
3625 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3626 {
3627 handled = HANDLED_RECOMPUTE_PROPS;
3628
3629 /* Get the position at which the next change of the
3630 invisible text property can be found in IT->string.
3631 Value will be nil if the property value is the same for
3632 all the rest of IT->string. */
3633 XSETINT (limit, SCHARS (it->string));
3634 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3635 it->string, limit);
3636
3637 /* Text at current position is invisible. The next
3638 change in the property is at position end_charpos.
3639 Move IT's current position to that position. */
3640 if (INTEGERP (end_charpos)
3641 && XFASTINT (end_charpos) < XFASTINT (limit))
3642 {
3643 struct text_pos old;
3644 old = it->current.string_pos;
3645 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3646 compute_string_pos (&it->current.string_pos, old, it->string);
3647 }
3648 else
3649 {
3650 /* The rest of the string is invisible. If this is an
3651 overlay string, proceed with the next overlay string
3652 or whatever comes and return a character from there. */
3653 if (it->current.overlay_string_index >= 0)
3654 {
3655 next_overlay_string (it);
3656 /* Don't check for overlay strings when we just
3657 finished processing them. */
3658 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3659 }
3660 else
3661 {
3662 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3663 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3664 }
3665 }
3666 }
3667 }
3668 else
3669 {
3670 int invis_p, newpos, next_stop, start_charpos;
3671 Lisp_Object pos, prop, overlay;
3672
3673 /* First of all, is there invisible text at this position? */
3674 start_charpos = IT_CHARPOS (*it);
3675 pos = make_number (IT_CHARPOS (*it));
3676 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3677 &overlay);
3678 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3679
3680 /* If we are on invisible text, skip over it. */
3681 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3682 {
3683 /* Record whether we have to display an ellipsis for the
3684 invisible text. */
3685 int display_ellipsis_p = invis_p == 2;
3686
3687 handled = HANDLED_RECOMPUTE_PROPS;
3688
3689 /* Loop skipping over invisible text. The loop is left at
3690 ZV or with IT on the first char being visible again. */
3691 do
3692 {
3693 /* Try to skip some invisible text. Return value is the
3694 position reached which can be equal to IT's position
3695 if there is nothing invisible here. This skips both
3696 over invisible text properties and overlays with
3697 invisible property. */
3698 newpos = skip_invisible (IT_CHARPOS (*it),
3699 &next_stop, ZV, it->window);
3700
3701 /* If we skipped nothing at all we weren't at invisible
3702 text in the first place. If everything to the end of
3703 the buffer was skipped, end the loop. */
3704 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3705 invis_p = 0;
3706 else
3707 {
3708 /* We skipped some characters but not necessarily
3709 all there are. Check if we ended up on visible
3710 text. Fget_char_property returns the property of
3711 the char before the given position, i.e. if we
3712 get invis_p = 0, this means that the char at
3713 newpos is visible. */
3714 pos = make_number (newpos);
3715 prop = Fget_char_property (pos, Qinvisible, it->window);
3716 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3717 }
3718
3719 /* If we ended up on invisible text, proceed to
3720 skip starting with next_stop. */
3721 if (invis_p)
3722 IT_CHARPOS (*it) = next_stop;
3723
3724 /* If there are adjacent invisible texts, don't lose the
3725 second one's ellipsis. */
3726 if (invis_p == 2)
3727 display_ellipsis_p = 1;
3728 }
3729 while (invis_p);
3730
3731 /* The position newpos is now either ZV or on visible text. */
3732 IT_CHARPOS (*it) = newpos;
3733 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3734
3735 /* If there are before-strings at the start of invisible
3736 text, and the text is invisible because of a text
3737 property, arrange to show before-strings because 20.x did
3738 it that way. (If the text is invisible because of an
3739 overlay property instead of a text property, this is
3740 already handled in the overlay code.) */
3741 if (NILP (overlay)
3742 && get_overlay_strings (it, start_charpos))
3743 {
3744 handled = HANDLED_RECOMPUTE_PROPS;
3745 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3746 }
3747 else if (display_ellipsis_p)
3748 {
3749 /* Make sure that the glyphs of the ellipsis will get
3750 correct `charpos' values. If we would not update
3751 it->position here, the glyphs would belong to the
3752 last visible character _before_ the invisible
3753 text, which confuses `set_cursor_from_row'.
3754
3755 We use the last invisible position instead of the
3756 first because this way the cursor is always drawn on
3757 the first "." of the ellipsis, whenever PT is inside
3758 the invisible text. Otherwise the cursor would be
3759 placed _after_ the ellipsis when the point is after the
3760 first invisible character. */
3761 if (!STRINGP (it->object))
3762 {
3763 it->position.charpos = IT_CHARPOS (*it) - 1;
3764 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3765 }
3766 setup_for_ellipsis (it, 0);
3767 /* Let the ellipsis display before
3768 considering any properties of the following char.
3769 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3770 handled = HANDLED_RETURN;
3771 }
3772 }
3773 }
3774
3775 return handled;
3776 }
3777
3778
3779 /* Make iterator IT return `...' next.
3780 Replaces LEN characters from buffer. */
3781
3782 static void
3783 setup_for_ellipsis (it, len)
3784 struct it *it;
3785 int len;
3786 {
3787 /* Use the display table definition for `...'. Invalid glyphs
3788 will be handled by the method returning elements from dpvec. */
3789 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3790 {
3791 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3792 it->dpvec = v->contents;
3793 it->dpend = v->contents + v->size;
3794 }
3795 else
3796 {
3797 /* Default `...'. */
3798 it->dpvec = default_invis_vector;
3799 it->dpend = default_invis_vector + 3;
3800 }
3801
3802 it->dpvec_char_len = len;
3803 it->current.dpvec_index = 0;
3804 it->dpvec_face_id = -1;
3805
3806 /* Remember the current face id in case glyphs specify faces.
3807 IT's face is restored in set_iterator_to_next.
3808 saved_face_id was set to preceding char's face in handle_stop. */
3809 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3810 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3811
3812 it->method = GET_FROM_DISPLAY_VECTOR;
3813 it->ellipsis_p = 1;
3814 }
3815
3816
3817 \f
3818 /***********************************************************************
3819 'display' property
3820 ***********************************************************************/
3821
3822 /* Set up iterator IT from `display' property at its current position.
3823 Called from handle_stop.
3824 We return HANDLED_RETURN if some part of the display property
3825 overrides the display of the buffer text itself.
3826 Otherwise we return HANDLED_NORMALLY. */
3827
3828 static enum prop_handled
3829 handle_display_prop (it)
3830 struct it *it;
3831 {
3832 Lisp_Object prop, object, overlay;
3833 struct text_pos *position;
3834 /* Nonzero if some property replaces the display of the text itself. */
3835 int display_replaced_p = 0;
3836
3837 if (STRINGP (it->string))
3838 {
3839 object = it->string;
3840 position = &it->current.string_pos;
3841 }
3842 else
3843 {
3844 XSETWINDOW (object, it->w);
3845 position = &it->current.pos;
3846 }
3847
3848 /* Reset those iterator values set from display property values. */
3849 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3850 it->space_width = Qnil;
3851 it->font_height = Qnil;
3852 it->voffset = 0;
3853
3854 /* We don't support recursive `display' properties, i.e. string
3855 values that have a string `display' property, that have a string
3856 `display' property etc. */
3857 if (!it->string_from_display_prop_p)
3858 it->area = TEXT_AREA;
3859
3860 prop = get_char_property_and_overlay (make_number (position->charpos),
3861 Qdisplay, object, &overlay);
3862 if (NILP (prop))
3863 return HANDLED_NORMALLY;
3864 /* Now OVERLAY is the overlay that gave us this property, or nil
3865 if it was a text property. */
3866
3867 if (!STRINGP (it->string))
3868 object = it->w->buffer;
3869
3870 if (CONSP (prop)
3871 /* Simple properties. */
3872 && !EQ (XCAR (prop), Qimage)
3873 && !EQ (XCAR (prop), Qspace)
3874 && !EQ (XCAR (prop), Qwhen)
3875 && !EQ (XCAR (prop), Qslice)
3876 && !EQ (XCAR (prop), Qspace_width)
3877 && !EQ (XCAR (prop), Qheight)
3878 && !EQ (XCAR (prop), Qraise)
3879 /* Marginal area specifications. */
3880 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3881 && !EQ (XCAR (prop), Qleft_fringe)
3882 && !EQ (XCAR (prop), Qright_fringe)
3883 && !NILP (XCAR (prop)))
3884 {
3885 for (; CONSP (prop); prop = XCDR (prop))
3886 {
3887 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3888 position, display_replaced_p))
3889 {
3890 display_replaced_p = 1;
3891 /* If some text in a string is replaced, `position' no
3892 longer points to the position of `object'. */
3893 if (STRINGP (object))
3894 break;
3895 }
3896 }
3897 }
3898 else if (VECTORP (prop))
3899 {
3900 int i;
3901 for (i = 0; i < ASIZE (prop); ++i)
3902 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3903 position, display_replaced_p))
3904 {
3905 display_replaced_p = 1;
3906 /* If some text in a string is replaced, `position' no
3907 longer points to the position of `object'. */
3908 if (STRINGP (object))
3909 break;
3910 }
3911 }
3912 else
3913 {
3914 int ret = handle_single_display_spec (it, prop, object, overlay,
3915 position, 0);
3916 if (ret < 0) /* Replaced by "", i.e. nothing. */
3917 return HANDLED_RECOMPUTE_PROPS;
3918 if (ret)
3919 display_replaced_p = 1;
3920 }
3921
3922 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3923 }
3924
3925
3926 /* Value is the position of the end of the `display' property starting
3927 at START_POS in OBJECT. */
3928
3929 static struct text_pos
3930 display_prop_end (it, object, start_pos)
3931 struct it *it;
3932 Lisp_Object object;
3933 struct text_pos start_pos;
3934 {
3935 Lisp_Object end;
3936 struct text_pos end_pos;
3937
3938 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3939 Qdisplay, object, Qnil);
3940 CHARPOS (end_pos) = XFASTINT (end);
3941 if (STRINGP (object))
3942 compute_string_pos (&end_pos, start_pos, it->string);
3943 else
3944 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3945
3946 return end_pos;
3947 }
3948
3949
3950 /* Set up IT from a single `display' specification PROP. OBJECT
3951 is the object in which the `display' property was found. *POSITION
3952 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3953 means that we previously saw a display specification which already
3954 replaced text display with something else, for example an image;
3955 we ignore such properties after the first one has been processed.
3956
3957 OVERLAY is the overlay this `display' property came from,
3958 or nil if it was a text property.
3959
3960 If PROP is a `space' or `image' specification, and in some other
3961 cases too, set *POSITION to the position where the `display'
3962 property ends.
3963
3964 Value is non-zero if something was found which replaces the display
3965 of buffer or string text. Specifically, the value is -1 if that
3966 "something" is "nothing". */
3967
3968 static int
3969 handle_single_display_spec (it, spec, object, overlay, position,
3970 display_replaced_before_p)
3971 struct it *it;
3972 Lisp_Object spec;
3973 Lisp_Object object;
3974 Lisp_Object overlay;
3975 struct text_pos *position;
3976 int display_replaced_before_p;
3977 {
3978 Lisp_Object form;
3979 Lisp_Object location, value;
3980 struct text_pos start_pos, save_pos;
3981 int valid_p;
3982
3983 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3984 If the result is non-nil, use VALUE instead of SPEC. */
3985 form = Qt;
3986 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3987 {
3988 spec = XCDR (spec);
3989 if (!CONSP (spec))
3990 return 0;
3991 form = XCAR (spec);
3992 spec = XCDR (spec);
3993 }
3994
3995 if (!NILP (form) && !EQ (form, Qt))
3996 {
3997 int count = SPECPDL_INDEX ();
3998 struct gcpro gcpro1;
3999
4000 /* Bind `object' to the object having the `display' property, a
4001 buffer or string. Bind `position' to the position in the
4002 object where the property was found, and `buffer-position'
4003 to the current position in the buffer. */
4004 specbind (Qobject, object);
4005 specbind (Qposition, make_number (CHARPOS (*position)));
4006 specbind (Qbuffer_position,
4007 make_number (STRINGP (object)
4008 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4009 GCPRO1 (form);
4010 form = safe_eval (form);
4011 UNGCPRO;
4012 unbind_to (count, Qnil);
4013 }
4014
4015 if (NILP (form))
4016 return 0;
4017
4018 /* Handle `(height HEIGHT)' specifications. */
4019 if (CONSP (spec)
4020 && EQ (XCAR (spec), Qheight)
4021 && CONSP (XCDR (spec)))
4022 {
4023 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4024 return 0;
4025
4026 it->font_height = XCAR (XCDR (spec));
4027 if (!NILP (it->font_height))
4028 {
4029 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4030 int new_height = -1;
4031
4032 if (CONSP (it->font_height)
4033 && (EQ (XCAR (it->font_height), Qplus)
4034 || EQ (XCAR (it->font_height), Qminus))
4035 && CONSP (XCDR (it->font_height))
4036 && INTEGERP (XCAR (XCDR (it->font_height))))
4037 {
4038 /* `(+ N)' or `(- N)' where N is an integer. */
4039 int steps = XINT (XCAR (XCDR (it->font_height)));
4040 if (EQ (XCAR (it->font_height), Qplus))
4041 steps = - steps;
4042 it->face_id = smaller_face (it->f, it->face_id, steps);
4043 }
4044 else if (FUNCTIONP (it->font_height))
4045 {
4046 /* Call function with current height as argument.
4047 Value is the new height. */
4048 Lisp_Object height;
4049 height = safe_call1 (it->font_height,
4050 face->lface[LFACE_HEIGHT_INDEX]);
4051 if (NUMBERP (height))
4052 new_height = XFLOATINT (height);
4053 }
4054 else if (NUMBERP (it->font_height))
4055 {
4056 /* Value is a multiple of the canonical char height. */
4057 struct face *face;
4058
4059 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
4060 new_height = (XFLOATINT (it->font_height)
4061 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4062 }
4063 else
4064 {
4065 /* Evaluate IT->font_height with `height' bound to the
4066 current specified height to get the new height. */
4067 int count = SPECPDL_INDEX ();
4068
4069 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4070 value = safe_eval (it->font_height);
4071 unbind_to (count, Qnil);
4072
4073 if (NUMBERP (value))
4074 new_height = XFLOATINT (value);
4075 }
4076
4077 if (new_height > 0)
4078 it->face_id = face_with_height (it->f, it->face_id, new_height);
4079 }
4080
4081 return 0;
4082 }
4083
4084 /* Handle `(space-width WIDTH)'. */
4085 if (CONSP (spec)
4086 && EQ (XCAR (spec), Qspace_width)
4087 && CONSP (XCDR (spec)))
4088 {
4089 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4090 return 0;
4091
4092 value = XCAR (XCDR (spec));
4093 if (NUMBERP (value) && XFLOATINT (value) > 0)
4094 it->space_width = value;
4095
4096 return 0;
4097 }
4098
4099 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4100 if (CONSP (spec)
4101 && EQ (XCAR (spec), Qslice))
4102 {
4103 Lisp_Object tem;
4104
4105 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4106 return 0;
4107
4108 if (tem = XCDR (spec), CONSP (tem))
4109 {
4110 it->slice.x = XCAR (tem);
4111 if (tem = XCDR (tem), CONSP (tem))
4112 {
4113 it->slice.y = XCAR (tem);
4114 if (tem = XCDR (tem), CONSP (tem))
4115 {
4116 it->slice.width = XCAR (tem);
4117 if (tem = XCDR (tem), CONSP (tem))
4118 it->slice.height = XCAR (tem);
4119 }
4120 }
4121 }
4122
4123 return 0;
4124 }
4125
4126 /* Handle `(raise FACTOR)'. */
4127 if (CONSP (spec)
4128 && EQ (XCAR (spec), Qraise)
4129 && CONSP (XCDR (spec)))
4130 {
4131 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4132 return 0;
4133
4134 #ifdef HAVE_WINDOW_SYSTEM
4135 value = XCAR (XCDR (spec));
4136 if (NUMBERP (value))
4137 {
4138 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4139 it->voffset = - (XFLOATINT (value)
4140 * (FONT_HEIGHT (face->font)));
4141 }
4142 #endif /* HAVE_WINDOW_SYSTEM */
4143
4144 return 0;
4145 }
4146
4147 /* Don't handle the other kinds of display specifications
4148 inside a string that we got from a `display' property. */
4149 if (it->string_from_display_prop_p)
4150 return 0;
4151
4152 /* Characters having this form of property are not displayed, so
4153 we have to find the end of the property. */
4154 start_pos = *position;
4155 *position = display_prop_end (it, object, start_pos);
4156 value = Qnil;
4157
4158 /* Stop the scan at that end position--we assume that all
4159 text properties change there. */
4160 it->stop_charpos = position->charpos;
4161
4162 /* Handle `(left-fringe BITMAP [FACE])'
4163 and `(right-fringe BITMAP [FACE])'. */
4164 if (CONSP (spec)
4165 && (EQ (XCAR (spec), Qleft_fringe)
4166 || EQ (XCAR (spec), Qright_fringe))
4167 && CONSP (XCDR (spec)))
4168 {
4169 int face_id = DEFAULT_FACE_ID;
4170 int fringe_bitmap;
4171
4172 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4173 /* If we return here, POSITION has been advanced
4174 across the text with this property. */
4175 return 0;
4176
4177 #ifdef HAVE_WINDOW_SYSTEM
4178 value = XCAR (XCDR (spec));
4179 if (!SYMBOLP (value)
4180 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4181 /* If we return here, POSITION has been advanced
4182 across the text with this property. */
4183 return 0;
4184
4185 if (CONSP (XCDR (XCDR (spec))))
4186 {
4187 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4188 int face_id2 = lookup_derived_face (it->f, face_name,
4189 'A', FRINGE_FACE_ID, 0);
4190 if (face_id2 >= 0)
4191 face_id = face_id2;
4192 }
4193
4194 /* Save current settings of IT so that we can restore them
4195 when we are finished with the glyph property value. */
4196
4197 save_pos = it->position;
4198 it->position = *position;
4199 push_it (it);
4200 it->position = save_pos;
4201
4202 it->area = TEXT_AREA;
4203 it->what = IT_IMAGE;
4204 it->image_id = -1; /* no image */
4205 it->position = start_pos;
4206 it->object = NILP (object) ? it->w->buffer : object;
4207 it->method = GET_FROM_IMAGE;
4208 it->from_overlay = Qnil;
4209 it->face_id = face_id;
4210
4211 /* Say that we haven't consumed the characters with
4212 `display' property yet. The call to pop_it in
4213 set_iterator_to_next will clean this up. */
4214 *position = start_pos;
4215
4216 if (EQ (XCAR (spec), Qleft_fringe))
4217 {
4218 it->left_user_fringe_bitmap = fringe_bitmap;
4219 it->left_user_fringe_face_id = face_id;
4220 }
4221 else
4222 {
4223 it->right_user_fringe_bitmap = fringe_bitmap;
4224 it->right_user_fringe_face_id = face_id;
4225 }
4226 #endif /* HAVE_WINDOW_SYSTEM */
4227 return 1;
4228 }
4229
4230 /* Prepare to handle `((margin left-margin) ...)',
4231 `((margin right-margin) ...)' and `((margin nil) ...)'
4232 prefixes for display specifications. */
4233 location = Qunbound;
4234 if (CONSP (spec) && CONSP (XCAR (spec)))
4235 {
4236 Lisp_Object tem;
4237
4238 value = XCDR (spec);
4239 if (CONSP (value))
4240 value = XCAR (value);
4241
4242 tem = XCAR (spec);
4243 if (EQ (XCAR (tem), Qmargin)
4244 && (tem = XCDR (tem),
4245 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4246 (NILP (tem)
4247 || EQ (tem, Qleft_margin)
4248 || EQ (tem, Qright_margin))))
4249 location = tem;
4250 }
4251
4252 if (EQ (location, Qunbound))
4253 {
4254 location = Qnil;
4255 value = spec;
4256 }
4257
4258 /* After this point, VALUE is the property after any
4259 margin prefix has been stripped. It must be a string,
4260 an image specification, or `(space ...)'.
4261
4262 LOCATION specifies where to display: `left-margin',
4263 `right-margin' or nil. */
4264
4265 valid_p = (STRINGP (value)
4266 #ifdef HAVE_WINDOW_SYSTEM
4267 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4268 #endif /* not HAVE_WINDOW_SYSTEM */
4269 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4270
4271 if (valid_p && !display_replaced_before_p)
4272 {
4273 /* Save current settings of IT so that we can restore them
4274 when we are finished with the glyph property value. */
4275 save_pos = it->position;
4276 it->position = *position;
4277 push_it (it);
4278 it->position = save_pos;
4279 it->from_overlay = overlay;
4280
4281 if (NILP (location))
4282 it->area = TEXT_AREA;
4283 else if (EQ (location, Qleft_margin))
4284 it->area = LEFT_MARGIN_AREA;
4285 else
4286 it->area = RIGHT_MARGIN_AREA;
4287
4288 if (STRINGP (value))
4289 {
4290 if (SCHARS (value) == 0)
4291 {
4292 pop_it (it);
4293 return -1; /* Replaced by "", i.e. nothing. */
4294 }
4295 it->string = value;
4296 it->multibyte_p = STRING_MULTIBYTE (it->string);
4297 it->current.overlay_string_index = -1;
4298 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4299 it->end_charpos = it->string_nchars = SCHARS (it->string);
4300 it->method = GET_FROM_STRING;
4301 it->stop_charpos = 0;
4302 it->string_from_display_prop_p = 1;
4303 /* Say that we haven't consumed the characters with
4304 `display' property yet. The call to pop_it in
4305 set_iterator_to_next will clean this up. */
4306 if (BUFFERP (object))
4307 *position = start_pos;
4308 }
4309 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4310 {
4311 it->method = GET_FROM_STRETCH;
4312 it->object = value;
4313 *position = it->position = start_pos;
4314 }
4315 #ifdef HAVE_WINDOW_SYSTEM
4316 else
4317 {
4318 it->what = IT_IMAGE;
4319 it->image_id = lookup_image (it->f, value);
4320 it->position = start_pos;
4321 it->object = NILP (object) ? it->w->buffer : object;
4322 it->method = GET_FROM_IMAGE;
4323
4324 /* Say that we haven't consumed the characters with
4325 `display' property yet. The call to pop_it in
4326 set_iterator_to_next will clean this up. */
4327 *position = start_pos;
4328 }
4329 #endif /* HAVE_WINDOW_SYSTEM */
4330
4331 return 1;
4332 }
4333
4334 /* Invalid property or property not supported. Restore
4335 POSITION to what it was before. */
4336 *position = start_pos;
4337 return 0;
4338 }
4339
4340
4341 /* Check if SPEC is a display specification value whose text should be
4342 treated as intangible. */
4343
4344 static int
4345 single_display_spec_intangible_p (prop)
4346 Lisp_Object prop;
4347 {
4348 /* Skip over `when FORM'. */
4349 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4350 {
4351 prop = XCDR (prop);
4352 if (!CONSP (prop))
4353 return 0;
4354 prop = XCDR (prop);
4355 }
4356
4357 if (STRINGP (prop))
4358 return 1;
4359
4360 if (!CONSP (prop))
4361 return 0;
4362
4363 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4364 we don't need to treat text as intangible. */
4365 if (EQ (XCAR (prop), Qmargin))
4366 {
4367 prop = XCDR (prop);
4368 if (!CONSP (prop))
4369 return 0;
4370
4371 prop = XCDR (prop);
4372 if (!CONSP (prop)
4373 || EQ (XCAR (prop), Qleft_margin)
4374 || EQ (XCAR (prop), Qright_margin))
4375 return 0;
4376 }
4377
4378 return (CONSP (prop)
4379 && (EQ (XCAR (prop), Qimage)
4380 || EQ (XCAR (prop), Qspace)));
4381 }
4382
4383
4384 /* Check if PROP is a display property value whose text should be
4385 treated as intangible. */
4386
4387 int
4388 display_prop_intangible_p (prop)
4389 Lisp_Object prop;
4390 {
4391 if (CONSP (prop)
4392 && CONSP (XCAR (prop))
4393 && !EQ (Qmargin, XCAR (XCAR (prop))))
4394 {
4395 /* A list of sub-properties. */
4396 while (CONSP (prop))
4397 {
4398 if (single_display_spec_intangible_p (XCAR (prop)))
4399 return 1;
4400 prop = XCDR (prop);
4401 }
4402 }
4403 else if (VECTORP (prop))
4404 {
4405 /* A vector of sub-properties. */
4406 int i;
4407 for (i = 0; i < ASIZE (prop); ++i)
4408 if (single_display_spec_intangible_p (AREF (prop, i)))
4409 return 1;
4410 }
4411 else
4412 return single_display_spec_intangible_p (prop);
4413
4414 return 0;
4415 }
4416
4417
4418 /* Return 1 if PROP is a display sub-property value containing STRING. */
4419
4420 static int
4421 single_display_spec_string_p (prop, string)
4422 Lisp_Object prop, string;
4423 {
4424 if (EQ (string, prop))
4425 return 1;
4426
4427 /* Skip over `when FORM'. */
4428 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4429 {
4430 prop = XCDR (prop);
4431 if (!CONSP (prop))
4432 return 0;
4433 prop = XCDR (prop);
4434 }
4435
4436 if (CONSP (prop))
4437 /* Skip over `margin LOCATION'. */
4438 if (EQ (XCAR (prop), Qmargin))
4439 {
4440 prop = XCDR (prop);
4441 if (!CONSP (prop))
4442 return 0;
4443
4444 prop = XCDR (prop);
4445 if (!CONSP (prop))
4446 return 0;
4447 }
4448
4449 return CONSP (prop) && EQ (XCAR (prop), string);
4450 }
4451
4452
4453 /* Return 1 if STRING appears in the `display' property PROP. */
4454
4455 static int
4456 display_prop_string_p (prop, string)
4457 Lisp_Object prop, string;
4458 {
4459 if (CONSP (prop)
4460 && CONSP (XCAR (prop))
4461 && !EQ (Qmargin, XCAR (XCAR (prop))))
4462 {
4463 /* A list of sub-properties. */
4464 while (CONSP (prop))
4465 {
4466 if (single_display_spec_string_p (XCAR (prop), string))
4467 return 1;
4468 prop = XCDR (prop);
4469 }
4470 }
4471 else if (VECTORP (prop))
4472 {
4473 /* A vector of sub-properties. */
4474 int i;
4475 for (i = 0; i < ASIZE (prop); ++i)
4476 if (single_display_spec_string_p (AREF (prop, i), string))
4477 return 1;
4478 }
4479 else
4480 return single_display_spec_string_p (prop, string);
4481
4482 return 0;
4483 }
4484
4485
4486 /* Determine from which buffer position in W's buffer STRING comes
4487 from. AROUND_CHARPOS is an approximate position where it could
4488 be from. Value is the buffer position or 0 if it couldn't be
4489 determined.
4490
4491 W's buffer must be current.
4492
4493 This function is necessary because we don't record buffer positions
4494 in glyphs generated from strings (to keep struct glyph small).
4495 This function may only use code that doesn't eval because it is
4496 called asynchronously from note_mouse_highlight. */
4497
4498 int
4499 string_buffer_position (w, string, around_charpos)
4500 struct window *w;
4501 Lisp_Object string;
4502 int around_charpos;
4503 {
4504 Lisp_Object limit, prop, pos;
4505 const int MAX_DISTANCE = 1000;
4506 int found = 0;
4507
4508 pos = make_number (around_charpos);
4509 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4510 while (!found && !EQ (pos, limit))
4511 {
4512 prop = Fget_char_property (pos, Qdisplay, Qnil);
4513 if (!NILP (prop) && display_prop_string_p (prop, string))
4514 found = 1;
4515 else
4516 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4517 }
4518
4519 if (!found)
4520 {
4521 pos = make_number (around_charpos);
4522 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4523 while (!found && !EQ (pos, limit))
4524 {
4525 prop = Fget_char_property (pos, Qdisplay, Qnil);
4526 if (!NILP (prop) && display_prop_string_p (prop, string))
4527 found = 1;
4528 else
4529 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4530 limit);
4531 }
4532 }
4533
4534 return found ? XINT (pos) : 0;
4535 }
4536
4537
4538 \f
4539 /***********************************************************************
4540 `composition' property
4541 ***********************************************************************/
4542
4543 /* Set up iterator IT from `composition' property at its current
4544 position. Called from handle_stop. */
4545
4546 static enum prop_handled
4547 handle_composition_prop (it)
4548 struct it *it;
4549 {
4550 Lisp_Object prop, string;
4551 int pos, pos_byte, end;
4552 enum prop_handled handled = HANDLED_NORMALLY;
4553
4554 if (STRINGP (it->string))
4555 {
4556 pos = IT_STRING_CHARPOS (*it);
4557 pos_byte = IT_STRING_BYTEPOS (*it);
4558 string = it->string;
4559 }
4560 else
4561 {
4562 pos = IT_CHARPOS (*it);
4563 pos_byte = IT_BYTEPOS (*it);
4564 string = Qnil;
4565 }
4566
4567 /* If there's a valid composition and point is not inside of the
4568 composition (in the case that the composition is from the current
4569 buffer), draw a glyph composed from the composition components. */
4570 if (find_composition (pos, -1, &pos, &end, &prop, string)
4571 && COMPOSITION_VALID_P (pos, end, prop)
4572 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4573 {
4574 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4575
4576 if (id >= 0)
4577 {
4578 struct composition *cmp = composition_table[id];
4579
4580 if (cmp->glyph_len == 0)
4581 {
4582 /* No glyph. */
4583 if (STRINGP (it->string))
4584 {
4585 IT_STRING_CHARPOS (*it) = end;
4586 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4587 end);
4588 }
4589 else
4590 {
4591 IT_CHARPOS (*it) = end;
4592 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4593 }
4594 return HANDLED_RECOMPUTE_PROPS;
4595 }
4596
4597 it->stop_charpos = end;
4598 push_it (it);
4599
4600 it->method = GET_FROM_COMPOSITION;
4601 it->cmp_id = id;
4602 it->cmp_len = COMPOSITION_LENGTH (prop);
4603 /* For a terminal, draw only the first character of the
4604 components. */
4605 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4606 it->len = (STRINGP (it->string)
4607 ? string_char_to_byte (it->string, end)
4608 : CHAR_TO_BYTE (end)) - pos_byte;
4609 handled = HANDLED_RETURN;
4610 }
4611 }
4612
4613 return handled;
4614 }
4615
4616
4617 \f
4618 /***********************************************************************
4619 Overlay strings
4620 ***********************************************************************/
4621
4622 /* The following structure is used to record overlay strings for
4623 later sorting in load_overlay_strings. */
4624
4625 struct overlay_entry
4626 {
4627 Lisp_Object overlay;
4628 Lisp_Object string;
4629 int priority;
4630 int after_string_p;
4631 };
4632
4633
4634 /* Set up iterator IT from overlay strings at its current position.
4635 Called from handle_stop. */
4636
4637 static enum prop_handled
4638 handle_overlay_change (it)
4639 struct it *it;
4640 {
4641 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4642 return HANDLED_RECOMPUTE_PROPS;
4643 else
4644 return HANDLED_NORMALLY;
4645 }
4646
4647
4648 /* Set up the next overlay string for delivery by IT, if there is an
4649 overlay string to deliver. Called by set_iterator_to_next when the
4650 end of the current overlay string is reached. If there are more
4651 overlay strings to display, IT->string and
4652 IT->current.overlay_string_index are set appropriately here.
4653 Otherwise IT->string is set to nil. */
4654
4655 static void
4656 next_overlay_string (it)
4657 struct it *it;
4658 {
4659 ++it->current.overlay_string_index;
4660 if (it->current.overlay_string_index == it->n_overlay_strings)
4661 {
4662 /* No more overlay strings. Restore IT's settings to what
4663 they were before overlay strings were processed, and
4664 continue to deliver from current_buffer. */
4665 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4666
4667 pop_it (it);
4668 xassert (it->sp > 0
4669 || it->method == GET_FROM_COMPOSITION
4670 || (NILP (it->string)
4671 && it->method == GET_FROM_BUFFER
4672 && it->stop_charpos >= BEGV
4673 && it->stop_charpos <= it->end_charpos));
4674 it->current.overlay_string_index = -1;
4675 it->n_overlay_strings = 0;
4676
4677 /* If we're at the end of the buffer, record that we have
4678 processed the overlay strings there already, so that
4679 next_element_from_buffer doesn't try it again. */
4680 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4681 it->overlay_strings_at_end_processed_p = 1;
4682
4683 /* If we have to display `...' for invisible text, set
4684 the iterator up for that. */
4685 if (display_ellipsis_p)
4686 setup_for_ellipsis (it, 0);
4687 }
4688 else
4689 {
4690 /* There are more overlay strings to process. If
4691 IT->current.overlay_string_index has advanced to a position
4692 where we must load IT->overlay_strings with more strings, do
4693 it. */
4694 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4695
4696 if (it->current.overlay_string_index && i == 0)
4697 load_overlay_strings (it, 0);
4698
4699 /* Initialize IT to deliver display elements from the overlay
4700 string. */
4701 it->string = it->overlay_strings[i];
4702 it->multibyte_p = STRING_MULTIBYTE (it->string);
4703 SET_TEXT_POS (it->current.string_pos, 0, 0);
4704 it->method = GET_FROM_STRING;
4705 it->stop_charpos = 0;
4706 }
4707
4708 CHECK_IT (it);
4709 }
4710
4711
4712 /* Compare two overlay_entry structures E1 and E2. Used as a
4713 comparison function for qsort in load_overlay_strings. Overlay
4714 strings for the same position are sorted so that
4715
4716 1. All after-strings come in front of before-strings, except
4717 when they come from the same overlay.
4718
4719 2. Within after-strings, strings are sorted so that overlay strings
4720 from overlays with higher priorities come first.
4721
4722 2. Within before-strings, strings are sorted so that overlay
4723 strings from overlays with higher priorities come last.
4724
4725 Value is analogous to strcmp. */
4726
4727
4728 static int
4729 compare_overlay_entries (e1, e2)
4730 void *e1, *e2;
4731 {
4732 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4733 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4734 int result;
4735
4736 if (entry1->after_string_p != entry2->after_string_p)
4737 {
4738 /* Let after-strings appear in front of before-strings if
4739 they come from different overlays. */
4740 if (EQ (entry1->overlay, entry2->overlay))
4741 result = entry1->after_string_p ? 1 : -1;
4742 else
4743 result = entry1->after_string_p ? -1 : 1;
4744 }
4745 else if (entry1->after_string_p)
4746 /* After-strings sorted in order of decreasing priority. */
4747 result = entry2->priority - entry1->priority;
4748 else
4749 /* Before-strings sorted in order of increasing priority. */
4750 result = entry1->priority - entry2->priority;
4751
4752 return result;
4753 }
4754
4755
4756 /* Load the vector IT->overlay_strings with overlay strings from IT's
4757 current buffer position, or from CHARPOS if that is > 0. Set
4758 IT->n_overlays to the total number of overlay strings found.
4759
4760 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4761 a time. On entry into load_overlay_strings,
4762 IT->current.overlay_string_index gives the number of overlay
4763 strings that have already been loaded by previous calls to this
4764 function.
4765
4766 IT->add_overlay_start contains an additional overlay start
4767 position to consider for taking overlay strings from, if non-zero.
4768 This position comes into play when the overlay has an `invisible'
4769 property, and both before and after-strings. When we've skipped to
4770 the end of the overlay, because of its `invisible' property, we
4771 nevertheless want its before-string to appear.
4772 IT->add_overlay_start will contain the overlay start position
4773 in this case.
4774
4775 Overlay strings are sorted so that after-string strings come in
4776 front of before-string strings. Within before and after-strings,
4777 strings are sorted by overlay priority. See also function
4778 compare_overlay_entries. */
4779
4780 static void
4781 load_overlay_strings (it, charpos)
4782 struct it *it;
4783 int charpos;
4784 {
4785 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4786 Lisp_Object overlay, window, str, invisible;
4787 struct Lisp_Overlay *ov;
4788 int start, end;
4789 int size = 20;
4790 int n = 0, i, j, invis_p;
4791 struct overlay_entry *entries
4792 = (struct overlay_entry *) alloca (size * sizeof *entries);
4793
4794 if (charpos <= 0)
4795 charpos = IT_CHARPOS (*it);
4796
4797 /* Append the overlay string STRING of overlay OVERLAY to vector
4798 `entries' which has size `size' and currently contains `n'
4799 elements. AFTER_P non-zero means STRING is an after-string of
4800 OVERLAY. */
4801 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4802 do \
4803 { \
4804 Lisp_Object priority; \
4805 \
4806 if (n == size) \
4807 { \
4808 int new_size = 2 * size; \
4809 struct overlay_entry *old = entries; \
4810 entries = \
4811 (struct overlay_entry *) alloca (new_size \
4812 * sizeof *entries); \
4813 bcopy (old, entries, size * sizeof *entries); \
4814 size = new_size; \
4815 } \
4816 \
4817 entries[n].string = (STRING); \
4818 entries[n].overlay = (OVERLAY); \
4819 priority = Foverlay_get ((OVERLAY), Qpriority); \
4820 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4821 entries[n].after_string_p = (AFTER_P); \
4822 ++n; \
4823 } \
4824 while (0)
4825
4826 /* Process overlay before the overlay center. */
4827 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4828 {
4829 XSETMISC (overlay, ov);
4830 xassert (OVERLAYP (overlay));
4831 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4832 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4833
4834 if (end < charpos)
4835 break;
4836
4837 /* Skip this overlay if it doesn't start or end at IT's current
4838 position. */
4839 if (end != charpos && start != charpos)
4840 continue;
4841
4842 /* Skip this overlay if it doesn't apply to IT->w. */
4843 window = Foverlay_get (overlay, Qwindow);
4844 if (WINDOWP (window) && XWINDOW (window) != it->w)
4845 continue;
4846
4847 /* If the text ``under'' the overlay is invisible, both before-
4848 and after-strings from this overlay are visible; start and
4849 end position are indistinguishable. */
4850 invisible = Foverlay_get (overlay, Qinvisible);
4851 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4852
4853 /* If overlay has a non-empty before-string, record it. */
4854 if ((start == charpos || (end == charpos && invis_p))
4855 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4856 && SCHARS (str))
4857 RECORD_OVERLAY_STRING (overlay, str, 0);
4858
4859 /* If overlay has a non-empty after-string, record it. */
4860 if ((end == charpos || (start == charpos && invis_p))
4861 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4862 && SCHARS (str))
4863 RECORD_OVERLAY_STRING (overlay, str, 1);
4864 }
4865
4866 /* Process overlays after the overlay center. */
4867 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4868 {
4869 XSETMISC (overlay, ov);
4870 xassert (OVERLAYP (overlay));
4871 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4872 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4873
4874 if (start > charpos)
4875 break;
4876
4877 /* Skip this overlay if it doesn't start or end at IT's current
4878 position. */
4879 if (end != charpos && start != charpos)
4880 continue;
4881
4882 /* Skip this overlay if it doesn't apply to IT->w. */
4883 window = Foverlay_get (overlay, Qwindow);
4884 if (WINDOWP (window) && XWINDOW (window) != it->w)
4885 continue;
4886
4887 /* If the text ``under'' the overlay is invisible, it has a zero
4888 dimension, and both before- and after-strings apply. */
4889 invisible = Foverlay_get (overlay, Qinvisible);
4890 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4891
4892 /* If overlay has a non-empty before-string, record it. */
4893 if ((start == charpos || (end == charpos && invis_p))
4894 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4895 && SCHARS (str))
4896 RECORD_OVERLAY_STRING (overlay, str, 0);
4897
4898 /* If overlay has a non-empty after-string, record it. */
4899 if ((end == charpos || (start == charpos && invis_p))
4900 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4901 && SCHARS (str))
4902 RECORD_OVERLAY_STRING (overlay, str, 1);
4903 }
4904
4905 #undef RECORD_OVERLAY_STRING
4906
4907 /* Sort entries. */
4908 if (n > 1)
4909 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4910
4911 /* Record the total number of strings to process. */
4912 it->n_overlay_strings = n;
4913
4914 /* IT->current.overlay_string_index is the number of overlay strings
4915 that have already been consumed by IT. Copy some of the
4916 remaining overlay strings to IT->overlay_strings. */
4917 i = 0;
4918 j = it->current.overlay_string_index;
4919 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4920 {
4921 it->overlay_strings[i] = entries[j].string;
4922 it->string_overlays[i++] = entries[j++].overlay;
4923 }
4924
4925 CHECK_IT (it);
4926 }
4927
4928
4929 /* Get the first chunk of overlay strings at IT's current buffer
4930 position, or at CHARPOS if that is > 0. Value is non-zero if at
4931 least one overlay string was found. */
4932
4933 static int
4934 get_overlay_strings_1 (it, charpos, compute_stop_p)
4935 struct it *it;
4936 int charpos;
4937 int compute_stop_p;
4938 {
4939 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4940 process. This fills IT->overlay_strings with strings, and sets
4941 IT->n_overlay_strings to the total number of strings to process.
4942 IT->pos.overlay_string_index has to be set temporarily to zero
4943 because load_overlay_strings needs this; it must be set to -1
4944 when no overlay strings are found because a zero value would
4945 indicate a position in the first overlay string. */
4946 it->current.overlay_string_index = 0;
4947 load_overlay_strings (it, charpos);
4948
4949 /* If we found overlay strings, set up IT to deliver display
4950 elements from the first one. Otherwise set up IT to deliver
4951 from current_buffer. */
4952 if (it->n_overlay_strings)
4953 {
4954 /* Make sure we know settings in current_buffer, so that we can
4955 restore meaningful values when we're done with the overlay
4956 strings. */
4957 if (compute_stop_p)
4958 compute_stop_pos (it);
4959 xassert (it->face_id >= 0);
4960
4961 /* Save IT's settings. They are restored after all overlay
4962 strings have been processed. */
4963 xassert (!compute_stop_p || it->sp == 0);
4964 push_it (it);
4965
4966 /* Set up IT to deliver display elements from the first overlay
4967 string. */
4968 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4969 it->string = it->overlay_strings[0];
4970 it->from_overlay = Qnil;
4971 it->stop_charpos = 0;
4972 xassert (STRINGP (it->string));
4973 it->end_charpos = SCHARS (it->string);
4974 it->multibyte_p = STRING_MULTIBYTE (it->string);
4975 it->method = GET_FROM_STRING;
4976 return 1;
4977 }
4978
4979 it->current.overlay_string_index = -1;
4980 return 0;
4981 }
4982
4983 static int
4984 get_overlay_strings (it, charpos)
4985 struct it *it;
4986 int charpos;
4987 {
4988 it->string = Qnil;
4989 it->method = GET_FROM_BUFFER;
4990
4991 (void) get_overlay_strings_1 (it, charpos, 1);
4992
4993 CHECK_IT (it);
4994
4995 /* Value is non-zero if we found at least one overlay string. */
4996 return STRINGP (it->string);
4997 }
4998
4999
5000 \f
5001 /***********************************************************************
5002 Saving and restoring state
5003 ***********************************************************************/
5004
5005 /* Save current settings of IT on IT->stack. Called, for example,
5006 before setting up IT for an overlay string, to be able to restore
5007 IT's settings to what they were after the overlay string has been
5008 processed. */
5009
5010 static void
5011 push_it (it)
5012 struct it *it;
5013 {
5014 struct iterator_stack_entry *p;
5015
5016 xassert (it->sp < IT_STACK_SIZE);
5017 p = it->stack + it->sp;
5018
5019 p->stop_charpos = it->stop_charpos;
5020 xassert (it->face_id >= 0);
5021 p->face_id = it->face_id;
5022 p->string = it->string;
5023 p->method = it->method;
5024 p->from_overlay = it->from_overlay;
5025 switch (p->method)
5026 {
5027 case GET_FROM_IMAGE:
5028 p->u.image.object = it->object;
5029 p->u.image.image_id = it->image_id;
5030 p->u.image.slice = it->slice;
5031 break;
5032 case GET_FROM_COMPOSITION:
5033 p->u.comp.object = it->object;
5034 p->u.comp.c = it->c;
5035 p->u.comp.len = it->len;
5036 p->u.comp.cmp_id = it->cmp_id;
5037 p->u.comp.cmp_len = it->cmp_len;
5038 break;
5039 case GET_FROM_STRETCH:
5040 p->u.stretch.object = it->object;
5041 break;
5042 }
5043 p->position = it->position;
5044 p->current = it->current;
5045 p->end_charpos = it->end_charpos;
5046 p->string_nchars = it->string_nchars;
5047 p->area = it->area;
5048 p->multibyte_p = it->multibyte_p;
5049 p->space_width = it->space_width;
5050 p->font_height = it->font_height;
5051 p->voffset = it->voffset;
5052 p->string_from_display_prop_p = it->string_from_display_prop_p;
5053 p->display_ellipsis_p = 0;
5054 ++it->sp;
5055 }
5056
5057
5058 /* Restore IT's settings from IT->stack. Called, for example, when no
5059 more overlay strings must be processed, and we return to delivering
5060 display elements from a buffer, or when the end of a string from a
5061 `display' property is reached and we return to delivering display
5062 elements from an overlay string, or from a buffer. */
5063
5064 static void
5065 pop_it (it)
5066 struct it *it;
5067 {
5068 struct iterator_stack_entry *p;
5069
5070 xassert (it->sp > 0);
5071 --it->sp;
5072 p = it->stack + it->sp;
5073 it->stop_charpos = p->stop_charpos;
5074 it->face_id = p->face_id;
5075 it->current = p->current;
5076 it->position = p->position;
5077 it->string = p->string;
5078 it->from_overlay = p->from_overlay;
5079 if (NILP (it->string))
5080 SET_TEXT_POS (it->current.string_pos, -1, -1);
5081 it->method = p->method;
5082 switch (it->method)
5083 {
5084 case GET_FROM_IMAGE:
5085 it->image_id = p->u.image.image_id;
5086 it->object = p->u.image.object;
5087 it->slice = p->u.image.slice;
5088 break;
5089 case GET_FROM_COMPOSITION:
5090 it->object = p->u.comp.object;
5091 it->c = p->u.comp.c;
5092 it->len = p->u.comp.len;
5093 it->cmp_id = p->u.comp.cmp_id;
5094 it->cmp_len = p->u.comp.cmp_len;
5095 break;
5096 case GET_FROM_STRETCH:
5097 it->object = p->u.comp.object;
5098 break;
5099 case GET_FROM_BUFFER:
5100 it->object = it->w->buffer;
5101 break;
5102 case GET_FROM_STRING:
5103 it->object = it->string;
5104 break;
5105 }
5106 it->end_charpos = p->end_charpos;
5107 it->string_nchars = p->string_nchars;
5108 it->area = p->area;
5109 it->multibyte_p = p->multibyte_p;
5110 it->space_width = p->space_width;
5111 it->font_height = p->font_height;
5112 it->voffset = p->voffset;
5113 it->string_from_display_prop_p = p->string_from_display_prop_p;
5114 }
5115
5116
5117 \f
5118 /***********************************************************************
5119 Moving over lines
5120 ***********************************************************************/
5121
5122 /* Set IT's current position to the previous line start. */
5123
5124 static void
5125 back_to_previous_line_start (it)
5126 struct it *it;
5127 {
5128 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5129 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5130 }
5131
5132
5133 /* Move IT to the next line start.
5134
5135 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5136 we skipped over part of the text (as opposed to moving the iterator
5137 continuously over the text). Otherwise, don't change the value
5138 of *SKIPPED_P.
5139
5140 Newlines may come from buffer text, overlay strings, or strings
5141 displayed via the `display' property. That's the reason we can't
5142 simply use find_next_newline_no_quit.
5143
5144 Note that this function may not skip over invisible text that is so
5145 because of text properties and immediately follows a newline. If
5146 it would, function reseat_at_next_visible_line_start, when called
5147 from set_iterator_to_next, would effectively make invisible
5148 characters following a newline part of the wrong glyph row, which
5149 leads to wrong cursor motion. */
5150
5151 static int
5152 forward_to_next_line_start (it, skipped_p)
5153 struct it *it;
5154 int *skipped_p;
5155 {
5156 int old_selective, newline_found_p, n;
5157 const int MAX_NEWLINE_DISTANCE = 500;
5158
5159 /* If already on a newline, just consume it to avoid unintended
5160 skipping over invisible text below. */
5161 if (it->what == IT_CHARACTER
5162 && it->c == '\n'
5163 && CHARPOS (it->position) == IT_CHARPOS (*it))
5164 {
5165 set_iterator_to_next (it, 0);
5166 it->c = 0;
5167 return 1;
5168 }
5169
5170 /* Don't handle selective display in the following. It's (a)
5171 unnecessary because it's done by the caller, and (b) leads to an
5172 infinite recursion because next_element_from_ellipsis indirectly
5173 calls this function. */
5174 old_selective = it->selective;
5175 it->selective = 0;
5176
5177 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5178 from buffer text. */
5179 for (n = newline_found_p = 0;
5180 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5181 n += STRINGP (it->string) ? 0 : 1)
5182 {
5183 if (!get_next_display_element (it))
5184 return 0;
5185 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5186 set_iterator_to_next (it, 0);
5187 }
5188
5189 /* If we didn't find a newline near enough, see if we can use a
5190 short-cut. */
5191 if (!newline_found_p)
5192 {
5193 int start = IT_CHARPOS (*it);
5194 int limit = find_next_newline_no_quit (start, 1);
5195 Lisp_Object pos;
5196
5197 xassert (!STRINGP (it->string));
5198
5199 /* If there isn't any `display' property in sight, and no
5200 overlays, we can just use the position of the newline in
5201 buffer text. */
5202 if (it->stop_charpos >= limit
5203 || ((pos = Fnext_single_property_change (make_number (start),
5204 Qdisplay,
5205 Qnil, make_number (limit)),
5206 NILP (pos))
5207 && next_overlay_change (start) == ZV))
5208 {
5209 IT_CHARPOS (*it) = limit;
5210 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5211 *skipped_p = newline_found_p = 1;
5212 }
5213 else
5214 {
5215 while (get_next_display_element (it)
5216 && !newline_found_p)
5217 {
5218 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5219 set_iterator_to_next (it, 0);
5220 }
5221 }
5222 }
5223
5224 it->selective = old_selective;
5225 return newline_found_p;
5226 }
5227
5228
5229 /* Set IT's current position to the previous visible line start. Skip
5230 invisible text that is so either due to text properties or due to
5231 selective display. Caution: this does not change IT->current_x and
5232 IT->hpos. */
5233
5234 static void
5235 back_to_previous_visible_line_start (it)
5236 struct it *it;
5237 {
5238 while (IT_CHARPOS (*it) > BEGV)
5239 {
5240 back_to_previous_line_start (it);
5241
5242 if (IT_CHARPOS (*it) <= BEGV)
5243 break;
5244
5245 /* If selective > 0, then lines indented more than that values
5246 are invisible. */
5247 if (it->selective > 0
5248 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5249 (double) it->selective)) /* iftc */
5250 continue;
5251
5252 /* Check the newline before point for invisibility. */
5253 {
5254 Lisp_Object prop;
5255 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5256 Qinvisible, it->window);
5257 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5258 continue;
5259 }
5260
5261 if (IT_CHARPOS (*it) <= BEGV)
5262 break;
5263
5264 {
5265 struct it it2;
5266 int pos;
5267 int beg, end;
5268 Lisp_Object val, overlay;
5269
5270 /* If newline is part of a composition, continue from start of composition */
5271 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5272 && beg < IT_CHARPOS (*it))
5273 goto replaced;
5274
5275 /* If newline is replaced by a display property, find start of overlay
5276 or interval and continue search from that point. */
5277 it2 = *it;
5278 pos = --IT_CHARPOS (it2);
5279 --IT_BYTEPOS (it2);
5280 it2.sp = 0;
5281 if (handle_display_prop (&it2) == HANDLED_RETURN
5282 && !NILP (val = get_char_property_and_overlay
5283 (make_number (pos), Qdisplay, Qnil, &overlay))
5284 && (OVERLAYP (overlay)
5285 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5286 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5287 goto replaced;
5288
5289 /* Newline is not replaced by anything -- so we are done. */
5290 break;
5291
5292 replaced:
5293 if (beg < BEGV)
5294 beg = BEGV;
5295 IT_CHARPOS (*it) = beg;
5296 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5297 }
5298 }
5299
5300 it->continuation_lines_width = 0;
5301
5302 xassert (IT_CHARPOS (*it) >= BEGV);
5303 xassert (IT_CHARPOS (*it) == BEGV
5304 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5305 CHECK_IT (it);
5306 }
5307
5308
5309 /* Reseat iterator IT at the previous visible line start. Skip
5310 invisible text that is so either due to text properties or due to
5311 selective display. At the end, update IT's overlay information,
5312 face information etc. */
5313
5314 void
5315 reseat_at_previous_visible_line_start (it)
5316 struct it *it;
5317 {
5318 back_to_previous_visible_line_start (it);
5319 reseat (it, it->current.pos, 1);
5320 CHECK_IT (it);
5321 }
5322
5323
5324 /* Reseat iterator IT on the next visible line start in the current
5325 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5326 preceding the line start. Skip over invisible text that is so
5327 because of selective display. Compute faces, overlays etc at the
5328 new position. Note that this function does not skip over text that
5329 is invisible because of text properties. */
5330
5331 static void
5332 reseat_at_next_visible_line_start (it, on_newline_p)
5333 struct it *it;
5334 int on_newline_p;
5335 {
5336 int newline_found_p, skipped_p = 0;
5337
5338 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5339
5340 /* Skip over lines that are invisible because they are indented
5341 more than the value of IT->selective. */
5342 if (it->selective > 0)
5343 while (IT_CHARPOS (*it) < ZV
5344 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5345 (double) it->selective)) /* iftc */
5346 {
5347 xassert (IT_BYTEPOS (*it) == BEGV
5348 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5349 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5350 }
5351
5352 /* Position on the newline if that's what's requested. */
5353 if (on_newline_p && newline_found_p)
5354 {
5355 if (STRINGP (it->string))
5356 {
5357 if (IT_STRING_CHARPOS (*it) > 0)
5358 {
5359 --IT_STRING_CHARPOS (*it);
5360 --IT_STRING_BYTEPOS (*it);
5361 }
5362 }
5363 else if (IT_CHARPOS (*it) > BEGV)
5364 {
5365 --IT_CHARPOS (*it);
5366 --IT_BYTEPOS (*it);
5367 reseat (it, it->current.pos, 0);
5368 }
5369 }
5370 else if (skipped_p)
5371 reseat (it, it->current.pos, 0);
5372
5373 CHECK_IT (it);
5374 }
5375
5376
5377 \f
5378 /***********************************************************************
5379 Changing an iterator's position
5380 ***********************************************************************/
5381
5382 /* Change IT's current position to POS in current_buffer. If FORCE_P
5383 is non-zero, always check for text properties at the new position.
5384 Otherwise, text properties are only looked up if POS >=
5385 IT->check_charpos of a property. */
5386
5387 static void
5388 reseat (it, pos, force_p)
5389 struct it *it;
5390 struct text_pos pos;
5391 int force_p;
5392 {
5393 int original_pos = IT_CHARPOS (*it);
5394
5395 reseat_1 (it, pos, 0);
5396
5397 /* Determine where to check text properties. Avoid doing it
5398 where possible because text property lookup is very expensive. */
5399 if (force_p
5400 || CHARPOS (pos) > it->stop_charpos
5401 || CHARPOS (pos) < original_pos)
5402 handle_stop (it);
5403
5404 CHECK_IT (it);
5405 }
5406
5407
5408 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5409 IT->stop_pos to POS, also. */
5410
5411 static void
5412 reseat_1 (it, pos, set_stop_p)
5413 struct it *it;
5414 struct text_pos pos;
5415 int set_stop_p;
5416 {
5417 /* Don't call this function when scanning a C string. */
5418 xassert (it->s == NULL);
5419
5420 /* POS must be a reasonable value. */
5421 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5422
5423 it->current.pos = it->position = pos;
5424 it->end_charpos = ZV;
5425 it->dpvec = NULL;
5426 it->current.dpvec_index = -1;
5427 it->current.overlay_string_index = -1;
5428 IT_STRING_CHARPOS (*it) = -1;
5429 IT_STRING_BYTEPOS (*it) = -1;
5430 it->string = Qnil;
5431 it->method = GET_FROM_BUFFER;
5432 it->object = it->w->buffer;
5433 it->area = TEXT_AREA;
5434 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5435 it->sp = 0;
5436 it->string_from_display_prop_p = 0;
5437 it->face_before_selective_p = 0;
5438
5439 if (set_stop_p)
5440 it->stop_charpos = CHARPOS (pos);
5441 }
5442
5443
5444 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5445 If S is non-null, it is a C string to iterate over. Otherwise,
5446 STRING gives a Lisp string to iterate over.
5447
5448 If PRECISION > 0, don't return more then PRECISION number of
5449 characters from the string.
5450
5451 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5452 characters have been returned. FIELD_WIDTH < 0 means an infinite
5453 field width.
5454
5455 MULTIBYTE = 0 means disable processing of multibyte characters,
5456 MULTIBYTE > 0 means enable it,
5457 MULTIBYTE < 0 means use IT->multibyte_p.
5458
5459 IT must be initialized via a prior call to init_iterator before
5460 calling this function. */
5461
5462 static void
5463 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5464 struct it *it;
5465 unsigned char *s;
5466 Lisp_Object string;
5467 int charpos;
5468 int precision, field_width, multibyte;
5469 {
5470 /* No region in strings. */
5471 it->region_beg_charpos = it->region_end_charpos = -1;
5472
5473 /* No text property checks performed by default, but see below. */
5474 it->stop_charpos = -1;
5475
5476 /* Set iterator position and end position. */
5477 bzero (&it->current, sizeof it->current);
5478 it->current.overlay_string_index = -1;
5479 it->current.dpvec_index = -1;
5480 xassert (charpos >= 0);
5481
5482 /* If STRING is specified, use its multibyteness, otherwise use the
5483 setting of MULTIBYTE, if specified. */
5484 if (multibyte >= 0)
5485 it->multibyte_p = multibyte > 0;
5486
5487 if (s == NULL)
5488 {
5489 xassert (STRINGP (string));
5490 it->string = string;
5491 it->s = NULL;
5492 it->end_charpos = it->string_nchars = SCHARS (string);
5493 it->method = GET_FROM_STRING;
5494 it->current.string_pos = string_pos (charpos, string);
5495 }
5496 else
5497 {
5498 it->s = s;
5499 it->string = Qnil;
5500
5501 /* Note that we use IT->current.pos, not it->current.string_pos,
5502 for displaying C strings. */
5503 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5504 if (it->multibyte_p)
5505 {
5506 it->current.pos = c_string_pos (charpos, s, 1);
5507 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5508 }
5509 else
5510 {
5511 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5512 it->end_charpos = it->string_nchars = strlen (s);
5513 }
5514
5515 it->method = GET_FROM_C_STRING;
5516 }
5517
5518 /* PRECISION > 0 means don't return more than PRECISION characters
5519 from the string. */
5520 if (precision > 0 && it->end_charpos - charpos > precision)
5521 it->end_charpos = it->string_nchars = charpos + precision;
5522
5523 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5524 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5525 FIELD_WIDTH < 0 means infinite field width. This is useful for
5526 padding with `-' at the end of a mode line. */
5527 if (field_width < 0)
5528 field_width = INFINITY;
5529 if (field_width > it->end_charpos - charpos)
5530 it->end_charpos = charpos + field_width;
5531
5532 /* Use the standard display table for displaying strings. */
5533 if (DISP_TABLE_P (Vstandard_display_table))
5534 it->dp = XCHAR_TABLE (Vstandard_display_table);
5535
5536 it->stop_charpos = charpos;
5537 CHECK_IT (it);
5538 }
5539
5540
5541 \f
5542 /***********************************************************************
5543 Iteration
5544 ***********************************************************************/
5545
5546 /* Map enum it_method value to corresponding next_element_from_* function. */
5547
5548 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5549 {
5550 next_element_from_buffer,
5551 next_element_from_display_vector,
5552 next_element_from_composition,
5553 next_element_from_string,
5554 next_element_from_c_string,
5555 next_element_from_image,
5556 next_element_from_stretch
5557 };
5558
5559
5560 /* Load IT's display element fields with information about the next
5561 display element from the current position of IT. Value is zero if
5562 end of buffer (or C string) is reached. */
5563
5564 static struct frame *last_escape_glyph_frame = NULL;
5565 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5566 static int last_escape_glyph_merged_face_id = 0;
5567
5568 int
5569 get_next_display_element (it)
5570 struct it *it;
5571 {
5572 /* Non-zero means that we found a display element. Zero means that
5573 we hit the end of what we iterate over. Performance note: the
5574 function pointer `method' used here turns out to be faster than
5575 using a sequence of if-statements. */
5576 int success_p;
5577
5578 get_next:
5579 success_p = (*get_next_element[it->method]) (it);
5580
5581 if (it->what == IT_CHARACTER)
5582 {
5583 /* Map via display table or translate control characters.
5584 IT->c, IT->len etc. have been set to the next character by
5585 the function call above. If we have a display table, and it
5586 contains an entry for IT->c, translate it. Don't do this if
5587 IT->c itself comes from a display table, otherwise we could
5588 end up in an infinite recursion. (An alternative could be to
5589 count the recursion depth of this function and signal an
5590 error when a certain maximum depth is reached.) Is it worth
5591 it? */
5592 if (success_p && it->dpvec == NULL)
5593 {
5594 Lisp_Object dv;
5595
5596 if (it->dp
5597 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5598 VECTORP (dv)))
5599 {
5600 struct Lisp_Vector *v = XVECTOR (dv);
5601
5602 /* Return the first character from the display table
5603 entry, if not empty. If empty, don't display the
5604 current character. */
5605 if (v->size)
5606 {
5607 it->dpvec_char_len = it->len;
5608 it->dpvec = v->contents;
5609 it->dpend = v->contents + v->size;
5610 it->current.dpvec_index = 0;
5611 it->dpvec_face_id = -1;
5612 it->saved_face_id = it->face_id;
5613 it->method = GET_FROM_DISPLAY_VECTOR;
5614 it->ellipsis_p = 0;
5615 }
5616 else
5617 {
5618 set_iterator_to_next (it, 0);
5619 }
5620 goto get_next;
5621 }
5622
5623 /* Translate control characters into `\003' or `^C' form.
5624 Control characters coming from a display table entry are
5625 currently not translated because we use IT->dpvec to hold
5626 the translation. This could easily be changed but I
5627 don't believe that it is worth doing.
5628
5629 If it->multibyte_p is nonzero, eight-bit characters and
5630 non-printable multibyte characters are also translated to
5631 octal form.
5632
5633 If it->multibyte_p is zero, eight-bit characters that
5634 don't have corresponding multibyte char code are also
5635 translated to octal form. */
5636 else if ((it->c < ' '
5637 && (it->area != TEXT_AREA
5638 /* In mode line, treat \n like other crl chars. */
5639 || (it->c != '\t'
5640 && it->glyph_row && it->glyph_row->mode_line_p)
5641 || (it->c != '\n' && it->c != '\t')))
5642 || (it->multibyte_p
5643 ? ((it->c >= 127
5644 && it->len == 1)
5645 || !CHAR_PRINTABLE_P (it->c)
5646 || (!NILP (Vnobreak_char_display)
5647 && (it->c == 0x8a0 || it->c == 0x8ad
5648 || it->c == 0x920 || it->c == 0x92d
5649 || it->c == 0xe20 || it->c == 0xe2d
5650 || it->c == 0xf20 || it->c == 0xf2d)))
5651 : (it->c >= 127
5652 && (!unibyte_display_via_language_environment
5653 || it->c == unibyte_char_to_multibyte (it->c)))))
5654 {
5655 /* IT->c is a control character which must be displayed
5656 either as '\003' or as `^C' where the '\\' and '^'
5657 can be defined in the display table. Fill
5658 IT->ctl_chars with glyphs for what we have to
5659 display. Then, set IT->dpvec to these glyphs. */
5660 GLYPH g;
5661 int ctl_len;
5662 int face_id, lface_id = 0 ;
5663 GLYPH escape_glyph;
5664
5665 /* Handle control characters with ^. */
5666
5667 if (it->c < 128 && it->ctl_arrow_p)
5668 {
5669 g = '^'; /* default glyph for Control */
5670 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5671 if (it->dp
5672 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5673 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5674 {
5675 g = XINT (DISP_CTRL_GLYPH (it->dp));
5676 lface_id = FAST_GLYPH_FACE (g);
5677 }
5678 if (lface_id)
5679 {
5680 g = FAST_GLYPH_CHAR (g);
5681 face_id = merge_faces (it->f, Qt, lface_id,
5682 it->face_id);
5683 }
5684 else if (it->f == last_escape_glyph_frame
5685 && it->face_id == last_escape_glyph_face_id)
5686 {
5687 face_id = last_escape_glyph_merged_face_id;
5688 }
5689 else
5690 {
5691 /* Merge the escape-glyph face into the current face. */
5692 face_id = merge_faces (it->f, Qescape_glyph, 0,
5693 it->face_id);
5694 last_escape_glyph_frame = it->f;
5695 last_escape_glyph_face_id = it->face_id;
5696 last_escape_glyph_merged_face_id = face_id;
5697 }
5698
5699 XSETINT (it->ctl_chars[0], g);
5700 g = it->c ^ 0100;
5701 XSETINT (it->ctl_chars[1], g);
5702 ctl_len = 2;
5703 goto display_control;
5704 }
5705
5706 /* Handle non-break space in the mode where it only gets
5707 highlighting. */
5708
5709 if (EQ (Vnobreak_char_display, Qt)
5710 && (it->c == 0x8a0 || it->c == 0x920
5711 || it->c == 0xe20 || it->c == 0xf20))
5712 {
5713 /* Merge the no-break-space face into the current face. */
5714 face_id = merge_faces (it->f, Qnobreak_space, 0,
5715 it->face_id);
5716
5717 g = it->c = ' ';
5718 XSETINT (it->ctl_chars[0], g);
5719 ctl_len = 1;
5720 goto display_control;
5721 }
5722
5723 /* Handle sequences that start with the "escape glyph". */
5724
5725 /* the default escape glyph is \. */
5726 escape_glyph = '\\';
5727
5728 if (it->dp
5729 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5730 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5731 {
5732 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5733 lface_id = FAST_GLYPH_FACE (escape_glyph);
5734 }
5735 if (lface_id)
5736 {
5737 /* The display table specified a face.
5738 Merge it into face_id and also into escape_glyph. */
5739 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5740 face_id = merge_faces (it->f, Qt, lface_id,
5741 it->face_id);
5742 }
5743 else if (it->f == last_escape_glyph_frame
5744 && it->face_id == last_escape_glyph_face_id)
5745 {
5746 face_id = last_escape_glyph_merged_face_id;
5747 }
5748 else
5749 {
5750 /* Merge the escape-glyph face into the current face. */
5751 face_id = merge_faces (it->f, Qescape_glyph, 0,
5752 it->face_id);
5753 last_escape_glyph_frame = it->f;
5754 last_escape_glyph_face_id = it->face_id;
5755 last_escape_glyph_merged_face_id = face_id;
5756 }
5757
5758 /* Handle soft hyphens in the mode where they only get
5759 highlighting. */
5760
5761 if (EQ (Vnobreak_char_display, Qt)
5762 && (it->c == 0x8ad || it->c == 0x92d
5763 || it->c == 0xe2d || it->c == 0xf2d))
5764 {
5765 g = it->c = '-';
5766 XSETINT (it->ctl_chars[0], g);
5767 ctl_len = 1;
5768 goto display_control;
5769 }
5770
5771 /* Handle non-break space and soft hyphen
5772 with the escape glyph. */
5773
5774 if (it->c == 0x8a0 || it->c == 0x8ad
5775 || it->c == 0x920 || it->c == 0x92d
5776 || it->c == 0xe20 || it->c == 0xe2d
5777 || it->c == 0xf20 || it->c == 0xf2d)
5778 {
5779 XSETINT (it->ctl_chars[0], escape_glyph);
5780 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5781 XSETINT (it->ctl_chars[1], g);
5782 ctl_len = 2;
5783 goto display_control;
5784 }
5785
5786 {
5787 unsigned char str[MAX_MULTIBYTE_LENGTH];
5788 int len;
5789 int i;
5790
5791 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5792 if (SINGLE_BYTE_CHAR_P (it->c))
5793 str[0] = it->c, len = 1;
5794 else
5795 {
5796 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5797 if (len < 0)
5798 {
5799 /* It's an invalid character, which shouldn't
5800 happen actually, but due to bugs it may
5801 happen. Let's print the char as is, there's
5802 not much meaningful we can do with it. */
5803 str[0] = it->c;
5804 str[1] = it->c >> 8;
5805 str[2] = it->c >> 16;
5806 str[3] = it->c >> 24;
5807 len = 4;
5808 }
5809 }
5810
5811 for (i = 0; i < len; i++)
5812 {
5813 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5814 /* Insert three more glyphs into IT->ctl_chars for
5815 the octal display of the character. */
5816 g = ((str[i] >> 6) & 7) + '0';
5817 XSETINT (it->ctl_chars[i * 4 + 1], g);
5818 g = ((str[i] >> 3) & 7) + '0';
5819 XSETINT (it->ctl_chars[i * 4 + 2], g);
5820 g = (str[i] & 7) + '0';
5821 XSETINT (it->ctl_chars[i * 4 + 3], g);
5822 }
5823 ctl_len = len * 4;
5824 }
5825
5826 display_control:
5827 /* Set up IT->dpvec and return first character from it. */
5828 it->dpvec_char_len = it->len;
5829 it->dpvec = it->ctl_chars;
5830 it->dpend = it->dpvec + ctl_len;
5831 it->current.dpvec_index = 0;
5832 it->dpvec_face_id = face_id;
5833 it->saved_face_id = it->face_id;
5834 it->method = GET_FROM_DISPLAY_VECTOR;
5835 it->ellipsis_p = 0;
5836 goto get_next;
5837 }
5838 }
5839
5840 /* Adjust face id for a multibyte character. There are no
5841 multibyte character in unibyte text. */
5842 if (it->multibyte_p
5843 && success_p
5844 && FRAME_WINDOW_P (it->f))
5845 {
5846 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5847 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5848 }
5849 }
5850
5851 /* Is this character the last one of a run of characters with
5852 box? If yes, set IT->end_of_box_run_p to 1. */
5853 if (it->face_box_p
5854 && it->s == NULL)
5855 {
5856 int face_id;
5857 struct face *face;
5858
5859 it->end_of_box_run_p
5860 = ((face_id = face_after_it_pos (it),
5861 face_id != it->face_id)
5862 && (face = FACE_FROM_ID (it->f, face_id),
5863 face->box == FACE_NO_BOX));
5864 }
5865
5866 /* Value is 0 if end of buffer or string reached. */
5867 return success_p;
5868 }
5869
5870
5871 /* Move IT to the next display element.
5872
5873 RESEAT_P non-zero means if called on a newline in buffer text,
5874 skip to the next visible line start.
5875
5876 Functions get_next_display_element and set_iterator_to_next are
5877 separate because I find this arrangement easier to handle than a
5878 get_next_display_element function that also increments IT's
5879 position. The way it is we can first look at an iterator's current
5880 display element, decide whether it fits on a line, and if it does,
5881 increment the iterator position. The other way around we probably
5882 would either need a flag indicating whether the iterator has to be
5883 incremented the next time, or we would have to implement a
5884 decrement position function which would not be easy to write. */
5885
5886 void
5887 set_iterator_to_next (it, reseat_p)
5888 struct it *it;
5889 int reseat_p;
5890 {
5891 /* Reset flags indicating start and end of a sequence of characters
5892 with box. Reset them at the start of this function because
5893 moving the iterator to a new position might set them. */
5894 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5895
5896 switch (it->method)
5897 {
5898 case GET_FROM_BUFFER:
5899 /* The current display element of IT is a character from
5900 current_buffer. Advance in the buffer, and maybe skip over
5901 invisible lines that are so because of selective display. */
5902 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5903 reseat_at_next_visible_line_start (it, 0);
5904 else
5905 {
5906 xassert (it->len != 0);
5907 IT_BYTEPOS (*it) += it->len;
5908 IT_CHARPOS (*it) += 1;
5909 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5910 }
5911 break;
5912
5913 case GET_FROM_COMPOSITION:
5914 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5915 xassert (it->sp > 0);
5916 pop_it (it);
5917 if (it->method == GET_FROM_STRING)
5918 {
5919 IT_STRING_BYTEPOS (*it) += it->len;
5920 IT_STRING_CHARPOS (*it) += it->cmp_len;
5921 goto consider_string_end;
5922 }
5923 else if (it->method == GET_FROM_BUFFER)
5924 {
5925 IT_BYTEPOS (*it) += it->len;
5926 IT_CHARPOS (*it) += it->cmp_len;
5927 }
5928 break;
5929
5930 case GET_FROM_C_STRING:
5931 /* Current display element of IT is from a C string. */
5932 IT_BYTEPOS (*it) += it->len;
5933 IT_CHARPOS (*it) += 1;
5934 break;
5935
5936 case GET_FROM_DISPLAY_VECTOR:
5937 /* Current display element of IT is from a display table entry.
5938 Advance in the display table definition. Reset it to null if
5939 end reached, and continue with characters from buffers/
5940 strings. */
5941 ++it->current.dpvec_index;
5942
5943 /* Restore face of the iterator to what they were before the
5944 display vector entry (these entries may contain faces). */
5945 it->face_id = it->saved_face_id;
5946
5947 if (it->dpvec + it->current.dpvec_index == it->dpend)
5948 {
5949 int recheck_faces = it->ellipsis_p;
5950
5951 if (it->s)
5952 it->method = GET_FROM_C_STRING;
5953 else if (STRINGP (it->string))
5954 it->method = GET_FROM_STRING;
5955 else
5956 {
5957 it->method = GET_FROM_BUFFER;
5958 it->object = it->w->buffer;
5959 }
5960
5961 it->dpvec = NULL;
5962 it->current.dpvec_index = -1;
5963
5964 /* Skip over characters which were displayed via IT->dpvec. */
5965 if (it->dpvec_char_len < 0)
5966 reseat_at_next_visible_line_start (it, 1);
5967 else if (it->dpvec_char_len > 0)
5968 {
5969 if (it->method == GET_FROM_STRING
5970 && it->n_overlay_strings > 0)
5971 it->ignore_overlay_strings_at_pos_p = 1;
5972 it->len = it->dpvec_char_len;
5973 set_iterator_to_next (it, reseat_p);
5974 }
5975
5976 /* Maybe recheck faces after display vector */
5977 if (recheck_faces)
5978 it->stop_charpos = IT_CHARPOS (*it);
5979 }
5980 break;
5981
5982 case GET_FROM_STRING:
5983 /* Current display element is a character from a Lisp string. */
5984 xassert (it->s == NULL && STRINGP (it->string));
5985 IT_STRING_BYTEPOS (*it) += it->len;
5986 IT_STRING_CHARPOS (*it) += 1;
5987
5988 consider_string_end:
5989
5990 if (it->current.overlay_string_index >= 0)
5991 {
5992 /* IT->string is an overlay string. Advance to the
5993 next, if there is one. */
5994 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5995 next_overlay_string (it);
5996 }
5997 else
5998 {
5999 /* IT->string is not an overlay string. If we reached
6000 its end, and there is something on IT->stack, proceed
6001 with what is on the stack. This can be either another
6002 string, this time an overlay string, or a buffer. */
6003 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6004 && it->sp > 0)
6005 {
6006 pop_it (it);
6007 if (it->method == GET_FROM_STRING)
6008 goto consider_string_end;
6009 }
6010 }
6011 break;
6012
6013 case GET_FROM_IMAGE:
6014 case GET_FROM_STRETCH:
6015 /* The position etc with which we have to proceed are on
6016 the stack. The position may be at the end of a string,
6017 if the `display' property takes up the whole string. */
6018 xassert (it->sp > 0);
6019 pop_it (it);
6020 if (it->method == GET_FROM_STRING)
6021 goto consider_string_end;
6022 break;
6023
6024 default:
6025 /* There are no other methods defined, so this should be a bug. */
6026 abort ();
6027 }
6028
6029 xassert (it->method != GET_FROM_STRING
6030 || (STRINGP (it->string)
6031 && IT_STRING_CHARPOS (*it) >= 0));
6032 }
6033
6034 /* Load IT's display element fields with information about the next
6035 display element which comes from a display table entry or from the
6036 result of translating a control character to one of the forms `^C'
6037 or `\003'.
6038
6039 IT->dpvec holds the glyphs to return as characters.
6040 IT->saved_face_id holds the face id before the display vector--
6041 it is restored into IT->face_idin set_iterator_to_next. */
6042
6043 static int
6044 next_element_from_display_vector (it)
6045 struct it *it;
6046 {
6047 /* Precondition. */
6048 xassert (it->dpvec && it->current.dpvec_index >= 0);
6049
6050 it->face_id = it->saved_face_id;
6051
6052 if (INTEGERP (*it->dpvec)
6053 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
6054 {
6055 GLYPH g;
6056
6057 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
6058 it->c = FAST_GLYPH_CHAR (g);
6059 it->len = CHAR_BYTES (it->c);
6060
6061 /* The entry may contain a face id to use. Such a face id is
6062 the id of a Lisp face, not a realized face. A face id of
6063 zero means no face is specified. */
6064 if (it->dpvec_face_id >= 0)
6065 it->face_id = it->dpvec_face_id;
6066 else
6067 {
6068 int lface_id = FAST_GLYPH_FACE (g);
6069 if (lface_id > 0)
6070 it->face_id = merge_faces (it->f, Qt, lface_id,
6071 it->saved_face_id);
6072 }
6073 }
6074 else
6075 /* Display table entry is invalid. Return a space. */
6076 it->c = ' ', it->len = 1;
6077
6078 /* Don't change position and object of the iterator here. They are
6079 still the values of the character that had this display table
6080 entry or was translated, and that's what we want. */
6081 it->what = IT_CHARACTER;
6082 return 1;
6083 }
6084
6085
6086 /* Load IT with the next display element from Lisp string IT->string.
6087 IT->current.string_pos is the current position within the string.
6088 If IT->current.overlay_string_index >= 0, the Lisp string is an
6089 overlay string. */
6090
6091 static int
6092 next_element_from_string (it)
6093 struct it *it;
6094 {
6095 struct text_pos position;
6096
6097 xassert (STRINGP (it->string));
6098 xassert (IT_STRING_CHARPOS (*it) >= 0);
6099 position = it->current.string_pos;
6100
6101 /* Time to check for invisible text? */
6102 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6103 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6104 {
6105 handle_stop (it);
6106
6107 /* Since a handler may have changed IT->method, we must
6108 recurse here. */
6109 return get_next_display_element (it);
6110 }
6111
6112 if (it->current.overlay_string_index >= 0)
6113 {
6114 /* Get the next character from an overlay string. In overlay
6115 strings, There is no field width or padding with spaces to
6116 do. */
6117 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6118 {
6119 it->what = IT_EOB;
6120 return 0;
6121 }
6122 else if (STRING_MULTIBYTE (it->string))
6123 {
6124 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6125 const unsigned char *s = (SDATA (it->string)
6126 + IT_STRING_BYTEPOS (*it));
6127 it->c = string_char_and_length (s, remaining, &it->len);
6128 }
6129 else
6130 {
6131 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6132 it->len = 1;
6133 }
6134 }
6135 else
6136 {
6137 /* Get the next character from a Lisp string that is not an
6138 overlay string. Such strings come from the mode line, for
6139 example. We may have to pad with spaces, or truncate the
6140 string. See also next_element_from_c_string. */
6141 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6142 {
6143 it->what = IT_EOB;
6144 return 0;
6145 }
6146 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6147 {
6148 /* Pad with spaces. */
6149 it->c = ' ', it->len = 1;
6150 CHARPOS (position) = BYTEPOS (position) = -1;
6151 }
6152 else if (STRING_MULTIBYTE (it->string))
6153 {
6154 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6155 const unsigned char *s = (SDATA (it->string)
6156 + IT_STRING_BYTEPOS (*it));
6157 it->c = string_char_and_length (s, maxlen, &it->len);
6158 }
6159 else
6160 {
6161 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6162 it->len = 1;
6163 }
6164 }
6165
6166 /* Record what we have and where it came from. */
6167 it->what = IT_CHARACTER;
6168 it->object = it->string;
6169 it->position = position;
6170 return 1;
6171 }
6172
6173
6174 /* Load IT with next display element from C string IT->s.
6175 IT->string_nchars is the maximum number of characters to return
6176 from the string. IT->end_charpos may be greater than
6177 IT->string_nchars when this function is called, in which case we
6178 may have to return padding spaces. Value is zero if end of string
6179 reached, including padding spaces. */
6180
6181 static int
6182 next_element_from_c_string (it)
6183 struct it *it;
6184 {
6185 int success_p = 1;
6186
6187 xassert (it->s);
6188 it->what = IT_CHARACTER;
6189 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6190 it->object = Qnil;
6191
6192 /* IT's position can be greater IT->string_nchars in case a field
6193 width or precision has been specified when the iterator was
6194 initialized. */
6195 if (IT_CHARPOS (*it) >= it->end_charpos)
6196 {
6197 /* End of the game. */
6198 it->what = IT_EOB;
6199 success_p = 0;
6200 }
6201 else if (IT_CHARPOS (*it) >= it->string_nchars)
6202 {
6203 /* Pad with spaces. */
6204 it->c = ' ', it->len = 1;
6205 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6206 }
6207 else if (it->multibyte_p)
6208 {
6209 /* Implementation note: The calls to strlen apparently aren't a
6210 performance problem because there is no noticeable performance
6211 difference between Emacs running in unibyte or multibyte mode. */
6212 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6213 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6214 maxlen, &it->len);
6215 }
6216 else
6217 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6218
6219 return success_p;
6220 }
6221
6222
6223 /* Set up IT to return characters from an ellipsis, if appropriate.
6224 The definition of the ellipsis glyphs may come from a display table
6225 entry. This function Fills IT with the first glyph from the
6226 ellipsis if an ellipsis is to be displayed. */
6227
6228 static int
6229 next_element_from_ellipsis (it)
6230 struct it *it;
6231 {
6232 if (it->selective_display_ellipsis_p)
6233 setup_for_ellipsis (it, it->len);
6234 else
6235 {
6236 /* The face at the current position may be different from the
6237 face we find after the invisible text. Remember what it
6238 was in IT->saved_face_id, and signal that it's there by
6239 setting face_before_selective_p. */
6240 it->saved_face_id = it->face_id;
6241 it->method = GET_FROM_BUFFER;
6242 it->object = it->w->buffer;
6243 reseat_at_next_visible_line_start (it, 1);
6244 it->face_before_selective_p = 1;
6245 }
6246
6247 return get_next_display_element (it);
6248 }
6249
6250
6251 /* Deliver an image display element. The iterator IT is already
6252 filled with image information (done in handle_display_prop). Value
6253 is always 1. */
6254
6255
6256 static int
6257 next_element_from_image (it)
6258 struct it *it;
6259 {
6260 it->what = IT_IMAGE;
6261 return 1;
6262 }
6263
6264
6265 /* Fill iterator IT with next display element from a stretch glyph
6266 property. IT->object is the value of the text property. Value is
6267 always 1. */
6268
6269 static int
6270 next_element_from_stretch (it)
6271 struct it *it;
6272 {
6273 it->what = IT_STRETCH;
6274 return 1;
6275 }
6276
6277
6278 /* Load IT with the next display element from current_buffer. Value
6279 is zero if end of buffer reached. IT->stop_charpos is the next
6280 position at which to stop and check for text properties or buffer
6281 end. */
6282
6283 static int
6284 next_element_from_buffer (it)
6285 struct it *it;
6286 {
6287 int success_p = 1;
6288
6289 /* Check this assumption, otherwise, we would never enter the
6290 if-statement, below. */
6291 xassert (IT_CHARPOS (*it) >= BEGV
6292 && IT_CHARPOS (*it) <= it->stop_charpos);
6293
6294 if (IT_CHARPOS (*it) >= it->stop_charpos)
6295 {
6296 if (IT_CHARPOS (*it) >= it->end_charpos)
6297 {
6298 int overlay_strings_follow_p;
6299
6300 /* End of the game, except when overlay strings follow that
6301 haven't been returned yet. */
6302 if (it->overlay_strings_at_end_processed_p)
6303 overlay_strings_follow_p = 0;
6304 else
6305 {
6306 it->overlay_strings_at_end_processed_p = 1;
6307 overlay_strings_follow_p = get_overlay_strings (it, 0);
6308 }
6309
6310 if (overlay_strings_follow_p)
6311 success_p = get_next_display_element (it);
6312 else
6313 {
6314 it->what = IT_EOB;
6315 it->position = it->current.pos;
6316 success_p = 0;
6317 }
6318 }
6319 else
6320 {
6321 handle_stop (it);
6322 return get_next_display_element (it);
6323 }
6324 }
6325 else
6326 {
6327 /* No face changes, overlays etc. in sight, so just return a
6328 character from current_buffer. */
6329 unsigned char *p;
6330
6331 /* Maybe run the redisplay end trigger hook. Performance note:
6332 This doesn't seem to cost measurable time. */
6333 if (it->redisplay_end_trigger_charpos
6334 && it->glyph_row
6335 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6336 run_redisplay_end_trigger_hook (it);
6337
6338 /* Get the next character, maybe multibyte. */
6339 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6340 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6341 {
6342 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6343 - IT_BYTEPOS (*it));
6344 it->c = string_char_and_length (p, maxlen, &it->len);
6345 }
6346 else
6347 it->c = *p, it->len = 1;
6348
6349 /* Record what we have and where it came from. */
6350 it->what = IT_CHARACTER;
6351 it->object = it->w->buffer;
6352 it->position = it->current.pos;
6353
6354 /* Normally we return the character found above, except when we
6355 really want to return an ellipsis for selective display. */
6356 if (it->selective)
6357 {
6358 if (it->c == '\n')
6359 {
6360 /* A value of selective > 0 means hide lines indented more
6361 than that number of columns. */
6362 if (it->selective > 0
6363 && IT_CHARPOS (*it) + 1 < ZV
6364 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6365 IT_BYTEPOS (*it) + 1,
6366 (double) it->selective)) /* iftc */
6367 {
6368 success_p = next_element_from_ellipsis (it);
6369 it->dpvec_char_len = -1;
6370 }
6371 }
6372 else if (it->c == '\r' && it->selective == -1)
6373 {
6374 /* A value of selective == -1 means that everything from the
6375 CR to the end of the line is invisible, with maybe an
6376 ellipsis displayed for it. */
6377 success_p = next_element_from_ellipsis (it);
6378 it->dpvec_char_len = -1;
6379 }
6380 }
6381 }
6382
6383 /* Value is zero if end of buffer reached. */
6384 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6385 return success_p;
6386 }
6387
6388
6389 /* Run the redisplay end trigger hook for IT. */
6390
6391 static void
6392 run_redisplay_end_trigger_hook (it)
6393 struct it *it;
6394 {
6395 Lisp_Object args[3];
6396
6397 /* IT->glyph_row should be non-null, i.e. we should be actually
6398 displaying something, or otherwise we should not run the hook. */
6399 xassert (it->glyph_row);
6400
6401 /* Set up hook arguments. */
6402 args[0] = Qredisplay_end_trigger_functions;
6403 args[1] = it->window;
6404 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6405 it->redisplay_end_trigger_charpos = 0;
6406
6407 /* Since we are *trying* to run these functions, don't try to run
6408 them again, even if they get an error. */
6409 it->w->redisplay_end_trigger = Qnil;
6410 Frun_hook_with_args (3, args);
6411
6412 /* Notice if it changed the face of the character we are on. */
6413 handle_face_prop (it);
6414 }
6415
6416
6417 /* Deliver a composition display element. The iterator IT is already
6418 filled with composition information (done in
6419 handle_composition_prop). Value is always 1. */
6420
6421 static int
6422 next_element_from_composition (it)
6423 struct it *it;
6424 {
6425 it->what = IT_COMPOSITION;
6426 it->position = (STRINGP (it->string)
6427 ? it->current.string_pos
6428 : it->current.pos);
6429 if (STRINGP (it->string))
6430 it->object = it->string;
6431 else
6432 it->object = it->w->buffer;
6433 return 1;
6434 }
6435
6436
6437 \f
6438 /***********************************************************************
6439 Moving an iterator without producing glyphs
6440 ***********************************************************************/
6441
6442 /* Check if iterator is at a position corresponding to a valid buffer
6443 position after some move_it_ call. */
6444
6445 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6446 ((it)->method == GET_FROM_STRING \
6447 ? IT_STRING_CHARPOS (*it) == 0 \
6448 : 1)
6449
6450
6451 /* Move iterator IT to a specified buffer or X position within one
6452 line on the display without producing glyphs.
6453
6454 OP should be a bit mask including some or all of these bits:
6455 MOVE_TO_X: Stop on reaching x-position TO_X.
6456 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6457 Regardless of OP's value, stop in reaching the end of the display line.
6458
6459 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6460 This means, in particular, that TO_X includes window's horizontal
6461 scroll amount.
6462
6463 The return value has several possible values that
6464 say what condition caused the scan to stop:
6465
6466 MOVE_POS_MATCH_OR_ZV
6467 - when TO_POS or ZV was reached.
6468
6469 MOVE_X_REACHED
6470 -when TO_X was reached before TO_POS or ZV were reached.
6471
6472 MOVE_LINE_CONTINUED
6473 - when we reached the end of the display area and the line must
6474 be continued.
6475
6476 MOVE_LINE_TRUNCATED
6477 - when we reached the end of the display area and the line is
6478 truncated.
6479
6480 MOVE_NEWLINE_OR_CR
6481 - when we stopped at a line end, i.e. a newline or a CR and selective
6482 display is on. */
6483
6484 static enum move_it_result
6485 move_it_in_display_line_to (it, to_charpos, to_x, op)
6486 struct it *it;
6487 int to_charpos, to_x, op;
6488 {
6489 enum move_it_result result = MOVE_UNDEFINED;
6490 struct glyph_row *saved_glyph_row;
6491
6492 /* Don't produce glyphs in produce_glyphs. */
6493 saved_glyph_row = it->glyph_row;
6494 it->glyph_row = NULL;
6495
6496 #define BUFFER_POS_REACHED_P() \
6497 ((op & MOVE_TO_POS) != 0 \
6498 && BUFFERP (it->object) \
6499 && IT_CHARPOS (*it) >= to_charpos \
6500 && (it->method == GET_FROM_BUFFER \
6501 || (it->method == GET_FROM_DISPLAY_VECTOR \
6502 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6503
6504
6505 while (1)
6506 {
6507 int x, i, ascent = 0, descent = 0;
6508
6509 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6510 if ((op & MOVE_TO_POS) != 0
6511 && BUFFERP (it->object)
6512 && it->method == GET_FROM_BUFFER
6513 && IT_CHARPOS (*it) > to_charpos)
6514 {
6515 result = MOVE_POS_MATCH_OR_ZV;
6516 break;
6517 }
6518
6519 /* Stop when ZV reached.
6520 We used to stop here when TO_CHARPOS reached as well, but that is
6521 too soon if this glyph does not fit on this line. So we handle it
6522 explicitly below. */
6523 if (!get_next_display_element (it)
6524 || (it->truncate_lines_p
6525 && BUFFER_POS_REACHED_P ()))
6526 {
6527 result = MOVE_POS_MATCH_OR_ZV;
6528 break;
6529 }
6530
6531 /* The call to produce_glyphs will get the metrics of the
6532 display element IT is loaded with. We record in x the
6533 x-position before this display element in case it does not
6534 fit on the line. */
6535 x = it->current_x;
6536
6537 /* Remember the line height so far in case the next element doesn't
6538 fit on the line. */
6539 if (!it->truncate_lines_p)
6540 {
6541 ascent = it->max_ascent;
6542 descent = it->max_descent;
6543 }
6544
6545 PRODUCE_GLYPHS (it);
6546
6547 if (it->area != TEXT_AREA)
6548 {
6549 set_iterator_to_next (it, 1);
6550 continue;
6551 }
6552
6553 /* The number of glyphs we get back in IT->nglyphs will normally
6554 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6555 character on a terminal frame, or (iii) a line end. For the
6556 second case, IT->nglyphs - 1 padding glyphs will be present
6557 (on X frames, there is only one glyph produced for a
6558 composite character.
6559
6560 The behavior implemented below means, for continuation lines,
6561 that as many spaces of a TAB as fit on the current line are
6562 displayed there. For terminal frames, as many glyphs of a
6563 multi-glyph character are displayed in the current line, too.
6564 This is what the old redisplay code did, and we keep it that
6565 way. Under X, the whole shape of a complex character must
6566 fit on the line or it will be completely displayed in the
6567 next line.
6568
6569 Note that both for tabs and padding glyphs, all glyphs have
6570 the same width. */
6571 if (it->nglyphs)
6572 {
6573 /* More than one glyph or glyph doesn't fit on line. All
6574 glyphs have the same width. */
6575 int single_glyph_width = it->pixel_width / it->nglyphs;
6576 int new_x;
6577 int x_before_this_char = x;
6578 int hpos_before_this_char = it->hpos;
6579
6580 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6581 {
6582 new_x = x + single_glyph_width;
6583
6584 /* We want to leave anything reaching TO_X to the caller. */
6585 if ((op & MOVE_TO_X) && new_x > to_x)
6586 {
6587 if (BUFFER_POS_REACHED_P ())
6588 goto buffer_pos_reached;
6589 it->current_x = x;
6590 result = MOVE_X_REACHED;
6591 break;
6592 }
6593 else if (/* Lines are continued. */
6594 !it->truncate_lines_p
6595 && (/* And glyph doesn't fit on the line. */
6596 new_x > it->last_visible_x
6597 /* Or it fits exactly and we're on a window
6598 system frame. */
6599 || (new_x == it->last_visible_x
6600 && FRAME_WINDOW_P (it->f))))
6601 {
6602 if (/* IT->hpos == 0 means the very first glyph
6603 doesn't fit on the line, e.g. a wide image. */
6604 it->hpos == 0
6605 || (new_x == it->last_visible_x
6606 && FRAME_WINDOW_P (it->f)))
6607 {
6608 ++it->hpos;
6609 it->current_x = new_x;
6610
6611 /* The character's last glyph just barely fits
6612 in this row. */
6613 if (i == it->nglyphs - 1)
6614 {
6615 /* If this is the destination position,
6616 return a position *before* it in this row,
6617 now that we know it fits in this row. */
6618 if (BUFFER_POS_REACHED_P ())
6619 {
6620 it->hpos = hpos_before_this_char;
6621 it->current_x = x_before_this_char;
6622 result = MOVE_POS_MATCH_OR_ZV;
6623 break;
6624 }
6625
6626 set_iterator_to_next (it, 1);
6627 #ifdef HAVE_WINDOW_SYSTEM
6628 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6629 {
6630 if (!get_next_display_element (it))
6631 {
6632 result = MOVE_POS_MATCH_OR_ZV;
6633 break;
6634 }
6635 if (BUFFER_POS_REACHED_P ())
6636 {
6637 if (ITERATOR_AT_END_OF_LINE_P (it))
6638 result = MOVE_POS_MATCH_OR_ZV;
6639 else
6640 result = MOVE_LINE_CONTINUED;
6641 break;
6642 }
6643 if (ITERATOR_AT_END_OF_LINE_P (it))
6644 {
6645 result = MOVE_NEWLINE_OR_CR;
6646 break;
6647 }
6648 }
6649 #endif /* HAVE_WINDOW_SYSTEM */
6650 }
6651 }
6652 else
6653 {
6654 it->current_x = x;
6655 it->max_ascent = ascent;
6656 it->max_descent = descent;
6657 }
6658
6659 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6660 IT_CHARPOS (*it)));
6661 result = MOVE_LINE_CONTINUED;
6662 break;
6663 }
6664 else if (BUFFER_POS_REACHED_P ())
6665 goto buffer_pos_reached;
6666 else if (new_x > it->first_visible_x)
6667 {
6668 /* Glyph is visible. Increment number of glyphs that
6669 would be displayed. */
6670 ++it->hpos;
6671 }
6672 else
6673 {
6674 /* Glyph is completely off the left margin of the display
6675 area. Nothing to do. */
6676 }
6677 }
6678
6679 if (result != MOVE_UNDEFINED)
6680 break;
6681 }
6682 else if (BUFFER_POS_REACHED_P ())
6683 {
6684 buffer_pos_reached:
6685 it->current_x = x;
6686 it->max_ascent = ascent;
6687 it->max_descent = descent;
6688 result = MOVE_POS_MATCH_OR_ZV;
6689 break;
6690 }
6691 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6692 {
6693 /* Stop when TO_X specified and reached. This check is
6694 necessary here because of lines consisting of a line end,
6695 only. The line end will not produce any glyphs and we
6696 would never get MOVE_X_REACHED. */
6697 xassert (it->nglyphs == 0);
6698 result = MOVE_X_REACHED;
6699 break;
6700 }
6701
6702 /* Is this a line end? If yes, we're done. */
6703 if (ITERATOR_AT_END_OF_LINE_P (it))
6704 {
6705 result = MOVE_NEWLINE_OR_CR;
6706 break;
6707 }
6708
6709 /* The current display element has been consumed. Advance
6710 to the next. */
6711 set_iterator_to_next (it, 1);
6712
6713 /* Stop if lines are truncated and IT's current x-position is
6714 past the right edge of the window now. */
6715 if (it->truncate_lines_p
6716 && it->current_x >= it->last_visible_x)
6717 {
6718 #ifdef HAVE_WINDOW_SYSTEM
6719 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6720 {
6721 if (!get_next_display_element (it)
6722 || BUFFER_POS_REACHED_P ())
6723 {
6724 result = MOVE_POS_MATCH_OR_ZV;
6725 break;
6726 }
6727 if (ITERATOR_AT_END_OF_LINE_P (it))
6728 {
6729 result = MOVE_NEWLINE_OR_CR;
6730 break;
6731 }
6732 }
6733 #endif /* HAVE_WINDOW_SYSTEM */
6734 result = MOVE_LINE_TRUNCATED;
6735 break;
6736 }
6737 }
6738
6739 #undef BUFFER_POS_REACHED_P
6740
6741 /* Restore the iterator settings altered at the beginning of this
6742 function. */
6743 it->glyph_row = saved_glyph_row;
6744 return result;
6745 }
6746
6747
6748 /* Move IT forward until it satisfies one or more of the criteria in
6749 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6750
6751 OP is a bit-mask that specifies where to stop, and in particular,
6752 which of those four position arguments makes a difference. See the
6753 description of enum move_operation_enum.
6754
6755 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6756 screen line, this function will set IT to the next position >
6757 TO_CHARPOS. */
6758
6759 void
6760 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6761 struct it *it;
6762 int to_charpos, to_x, to_y, to_vpos;
6763 int op;
6764 {
6765 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6766 int line_height;
6767 int reached = 0;
6768
6769 for (;;)
6770 {
6771 if (op & MOVE_TO_VPOS)
6772 {
6773 /* If no TO_CHARPOS and no TO_X specified, stop at the
6774 start of the line TO_VPOS. */
6775 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6776 {
6777 if (it->vpos == to_vpos)
6778 {
6779 reached = 1;
6780 break;
6781 }
6782 else
6783 skip = move_it_in_display_line_to (it, -1, -1, 0);
6784 }
6785 else
6786 {
6787 /* TO_VPOS >= 0 means stop at TO_X in the line at
6788 TO_VPOS, or at TO_POS, whichever comes first. */
6789 if (it->vpos == to_vpos)
6790 {
6791 reached = 2;
6792 break;
6793 }
6794
6795 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6796
6797 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6798 {
6799 reached = 3;
6800 break;
6801 }
6802 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6803 {
6804 /* We have reached TO_X but not in the line we want. */
6805 skip = move_it_in_display_line_to (it, to_charpos,
6806 -1, MOVE_TO_POS);
6807 if (skip == MOVE_POS_MATCH_OR_ZV)
6808 {
6809 reached = 4;
6810 break;
6811 }
6812 }
6813 }
6814 }
6815 else if (op & MOVE_TO_Y)
6816 {
6817 struct it it_backup;
6818
6819 /* TO_Y specified means stop at TO_X in the line containing
6820 TO_Y---or at TO_CHARPOS if this is reached first. The
6821 problem is that we can't really tell whether the line
6822 contains TO_Y before we have completely scanned it, and
6823 this may skip past TO_X. What we do is to first scan to
6824 TO_X.
6825
6826 If TO_X is not specified, use a TO_X of zero. The reason
6827 is to make the outcome of this function more predictable.
6828 If we didn't use TO_X == 0, we would stop at the end of
6829 the line which is probably not what a caller would expect
6830 to happen. */
6831 skip = move_it_in_display_line_to (it, to_charpos,
6832 ((op & MOVE_TO_X)
6833 ? to_x : 0),
6834 (MOVE_TO_X
6835 | (op & MOVE_TO_POS)));
6836
6837 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6838 if (skip == MOVE_POS_MATCH_OR_ZV)
6839 {
6840 reached = 5;
6841 break;
6842 }
6843
6844 /* If TO_X was reached, we would like to know whether TO_Y
6845 is in the line. This can only be said if we know the
6846 total line height which requires us to scan the rest of
6847 the line. */
6848 if (skip == MOVE_X_REACHED)
6849 {
6850 it_backup = *it;
6851 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6852 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6853 op & MOVE_TO_POS);
6854 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6855 }
6856
6857 /* Now, decide whether TO_Y is in this line. */
6858 line_height = it->max_ascent + it->max_descent;
6859 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6860
6861 if (to_y >= it->current_y
6862 && to_y < it->current_y + line_height)
6863 {
6864 if (skip == MOVE_X_REACHED)
6865 /* If TO_Y is in this line and TO_X was reached above,
6866 we scanned too far. We have to restore IT's settings
6867 to the ones before skipping. */
6868 *it = it_backup;
6869 reached = 6;
6870 }
6871 else if (skip == MOVE_X_REACHED)
6872 {
6873 skip = skip2;
6874 if (skip == MOVE_POS_MATCH_OR_ZV)
6875 reached = 7;
6876 }
6877
6878 if (reached)
6879 break;
6880 }
6881 else if (BUFFERP (it->object)
6882 && it->method == GET_FROM_BUFFER
6883 && IT_CHARPOS (*it) >= to_charpos)
6884 skip = MOVE_POS_MATCH_OR_ZV;
6885 else
6886 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6887
6888 switch (skip)
6889 {
6890 case MOVE_POS_MATCH_OR_ZV:
6891 reached = 8;
6892 goto out;
6893
6894 case MOVE_NEWLINE_OR_CR:
6895 set_iterator_to_next (it, 1);
6896 it->continuation_lines_width = 0;
6897 break;
6898
6899 case MOVE_LINE_TRUNCATED:
6900 it->continuation_lines_width = 0;
6901 reseat_at_next_visible_line_start (it, 0);
6902 if ((op & MOVE_TO_POS) != 0
6903 && IT_CHARPOS (*it) > to_charpos)
6904 {
6905 reached = 9;
6906 goto out;
6907 }
6908 break;
6909
6910 case MOVE_LINE_CONTINUED:
6911 /* For continued lines ending in a tab, some of the glyphs
6912 associated with the tab are displayed on the current
6913 line. Since it->current_x does not include these glyphs,
6914 we use it->last_visible_x instead. */
6915 it->continuation_lines_width +=
6916 (it->c == '\t') ? it->last_visible_x : it->current_x;
6917 break;
6918
6919 default:
6920 abort ();
6921 }
6922
6923 /* Reset/increment for the next run. */
6924 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6925 it->current_x = it->hpos = 0;
6926 it->current_y += it->max_ascent + it->max_descent;
6927 ++it->vpos;
6928 last_height = it->max_ascent + it->max_descent;
6929 last_max_ascent = it->max_ascent;
6930 it->max_ascent = it->max_descent = 0;
6931 }
6932
6933 out:
6934
6935 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6936 }
6937
6938
6939 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6940
6941 If DY > 0, move IT backward at least that many pixels. DY = 0
6942 means move IT backward to the preceding line start or BEGV. This
6943 function may move over more than DY pixels if IT->current_y - DY
6944 ends up in the middle of a line; in this case IT->current_y will be
6945 set to the top of the line moved to. */
6946
6947 void
6948 move_it_vertically_backward (it, dy)
6949 struct it *it;
6950 int dy;
6951 {
6952 int nlines, h;
6953 struct it it2, it3;
6954 int start_pos;
6955
6956 move_further_back:
6957 xassert (dy >= 0);
6958
6959 start_pos = IT_CHARPOS (*it);
6960
6961 /* Estimate how many newlines we must move back. */
6962 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6963
6964 /* Set the iterator's position that many lines back. */
6965 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6966 back_to_previous_visible_line_start (it);
6967
6968 /* Reseat the iterator here. When moving backward, we don't want
6969 reseat to skip forward over invisible text, set up the iterator
6970 to deliver from overlay strings at the new position etc. So,
6971 use reseat_1 here. */
6972 reseat_1 (it, it->current.pos, 1);
6973
6974 /* We are now surely at a line start. */
6975 it->current_x = it->hpos = 0;
6976 it->continuation_lines_width = 0;
6977
6978 /* Move forward and see what y-distance we moved. First move to the
6979 start of the next line so that we get its height. We need this
6980 height to be able to tell whether we reached the specified
6981 y-distance. */
6982 it2 = *it;
6983 it2.max_ascent = it2.max_descent = 0;
6984 do
6985 {
6986 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6987 MOVE_TO_POS | MOVE_TO_VPOS);
6988 }
6989 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6990 xassert (IT_CHARPOS (*it) >= BEGV);
6991 it3 = it2;
6992
6993 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6994 xassert (IT_CHARPOS (*it) >= BEGV);
6995 /* H is the actual vertical distance from the position in *IT
6996 and the starting position. */
6997 h = it2.current_y - it->current_y;
6998 /* NLINES is the distance in number of lines. */
6999 nlines = it2.vpos - it->vpos;
7000
7001 /* Correct IT's y and vpos position
7002 so that they are relative to the starting point. */
7003 it->vpos -= nlines;
7004 it->current_y -= h;
7005
7006 if (dy == 0)
7007 {
7008 /* DY == 0 means move to the start of the screen line. The
7009 value of nlines is > 0 if continuation lines were involved. */
7010 if (nlines > 0)
7011 move_it_by_lines (it, nlines, 1);
7012 #if 0
7013 /* I think this assert is bogus if buffer contains
7014 invisible text or images. KFS. */
7015 xassert (IT_CHARPOS (*it) <= start_pos);
7016 #endif
7017 }
7018 else
7019 {
7020 /* The y-position we try to reach, relative to *IT.
7021 Note that H has been subtracted in front of the if-statement. */
7022 int target_y = it->current_y + h - dy;
7023 int y0 = it3.current_y;
7024 int y1 = line_bottom_y (&it3);
7025 int line_height = y1 - y0;
7026
7027 /* If we did not reach target_y, try to move further backward if
7028 we can. If we moved too far backward, try to move forward. */
7029 if (target_y < it->current_y
7030 /* This is heuristic. In a window that's 3 lines high, with
7031 a line height of 13 pixels each, recentering with point
7032 on the bottom line will try to move -39/2 = 19 pixels
7033 backward. Try to avoid moving into the first line. */
7034 && (it->current_y - target_y
7035 > min (window_box_height (it->w), line_height * 2 / 3))
7036 && IT_CHARPOS (*it) > BEGV)
7037 {
7038 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7039 target_y - it->current_y));
7040 dy = it->current_y - target_y;
7041 goto move_further_back;
7042 }
7043 else if (target_y >= it->current_y + line_height
7044 && IT_CHARPOS (*it) < ZV)
7045 {
7046 /* Should move forward by at least one line, maybe more.
7047
7048 Note: Calling move_it_by_lines can be expensive on
7049 terminal frames, where compute_motion is used (via
7050 vmotion) to do the job, when there are very long lines
7051 and truncate-lines is nil. That's the reason for
7052 treating terminal frames specially here. */
7053
7054 if (!FRAME_WINDOW_P (it->f))
7055 move_it_vertically (it, target_y - (it->current_y + line_height));
7056 else
7057 {
7058 do
7059 {
7060 move_it_by_lines (it, 1, 1);
7061 }
7062 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7063 }
7064
7065 #if 0
7066 /* I think this assert is bogus if buffer contains
7067 invisible text or images. KFS. */
7068 xassert (IT_CHARPOS (*it) >= BEGV);
7069 #endif
7070 }
7071 }
7072 }
7073
7074
7075 /* Move IT by a specified amount of pixel lines DY. DY negative means
7076 move backwards. DY = 0 means move to start of screen line. At the
7077 end, IT will be on the start of a screen line. */
7078
7079 void
7080 move_it_vertically (it, dy)
7081 struct it *it;
7082 int dy;
7083 {
7084 if (dy <= 0)
7085 move_it_vertically_backward (it, -dy);
7086 else
7087 {
7088 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7089 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7090 MOVE_TO_POS | MOVE_TO_Y);
7091 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7092
7093 /* If buffer ends in ZV without a newline, move to the start of
7094 the line to satisfy the post-condition. */
7095 if (IT_CHARPOS (*it) == ZV
7096 && ZV > BEGV
7097 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7098 move_it_by_lines (it, 0, 0);
7099 }
7100 }
7101
7102
7103 /* Move iterator IT past the end of the text line it is in. */
7104
7105 void
7106 move_it_past_eol (it)
7107 struct it *it;
7108 {
7109 enum move_it_result rc;
7110
7111 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7112 if (rc == MOVE_NEWLINE_OR_CR)
7113 set_iterator_to_next (it, 0);
7114 }
7115
7116
7117 #if 0 /* Currently not used. */
7118
7119 /* Return non-zero if some text between buffer positions START_CHARPOS
7120 and END_CHARPOS is invisible. IT->window is the window for text
7121 property lookup. */
7122
7123 static int
7124 invisible_text_between_p (it, start_charpos, end_charpos)
7125 struct it *it;
7126 int start_charpos, end_charpos;
7127 {
7128 Lisp_Object prop, limit;
7129 int invisible_found_p;
7130
7131 xassert (it != NULL && start_charpos <= end_charpos);
7132
7133 /* Is text at START invisible? */
7134 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7135 it->window);
7136 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7137 invisible_found_p = 1;
7138 else
7139 {
7140 limit = Fnext_single_char_property_change (make_number (start_charpos),
7141 Qinvisible, Qnil,
7142 make_number (end_charpos));
7143 invisible_found_p = XFASTINT (limit) < end_charpos;
7144 }
7145
7146 return invisible_found_p;
7147 }
7148
7149 #endif /* 0 */
7150
7151
7152 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7153 negative means move up. DVPOS == 0 means move to the start of the
7154 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7155 NEED_Y_P is zero, IT->current_y will be left unchanged.
7156
7157 Further optimization ideas: If we would know that IT->f doesn't use
7158 a face with proportional font, we could be faster for
7159 truncate-lines nil. */
7160
7161 void
7162 move_it_by_lines (it, dvpos, need_y_p)
7163 struct it *it;
7164 int dvpos, need_y_p;
7165 {
7166 struct position pos;
7167
7168 /* The commented-out optimization uses vmotion on terminals. This
7169 gives bad results, because elements like it->what, on which
7170 callers such as pos_visible_p rely, aren't updated. */
7171 /* if (!FRAME_WINDOW_P (it->f))
7172 {
7173 struct text_pos textpos;
7174
7175 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7176 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7177 reseat (it, textpos, 1);
7178 it->vpos += pos.vpos;
7179 it->current_y += pos.vpos;
7180 }
7181 else */
7182
7183 if (dvpos == 0)
7184 {
7185 /* DVPOS == 0 means move to the start of the screen line. */
7186 move_it_vertically_backward (it, 0);
7187 xassert (it->current_x == 0 && it->hpos == 0);
7188 /* Let next call to line_bottom_y calculate real line height */
7189 last_height = 0;
7190 }
7191 else if (dvpos > 0)
7192 {
7193 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7194 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7195 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7196 }
7197 else
7198 {
7199 struct it it2;
7200 int start_charpos, i;
7201
7202 /* Start at the beginning of the screen line containing IT's
7203 position. This may actually move vertically backwards,
7204 in case of overlays, so adjust dvpos accordingly. */
7205 dvpos += it->vpos;
7206 move_it_vertically_backward (it, 0);
7207 dvpos -= it->vpos;
7208
7209 /* Go back -DVPOS visible lines and reseat the iterator there. */
7210 start_charpos = IT_CHARPOS (*it);
7211 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7212 back_to_previous_visible_line_start (it);
7213 reseat (it, it->current.pos, 1);
7214
7215 /* Move further back if we end up in a string or an image. */
7216 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7217 {
7218 /* First try to move to start of display line. */
7219 dvpos += it->vpos;
7220 move_it_vertically_backward (it, 0);
7221 dvpos -= it->vpos;
7222 if (IT_POS_VALID_AFTER_MOVE_P (it))
7223 break;
7224 /* If start of line is still in string or image,
7225 move further back. */
7226 back_to_previous_visible_line_start (it);
7227 reseat (it, it->current.pos, 1);
7228 dvpos--;
7229 }
7230
7231 it->current_x = it->hpos = 0;
7232
7233 /* Above call may have moved too far if continuation lines
7234 are involved. Scan forward and see if it did. */
7235 it2 = *it;
7236 it2.vpos = it2.current_y = 0;
7237 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7238 it->vpos -= it2.vpos;
7239 it->current_y -= it2.current_y;
7240 it->current_x = it->hpos = 0;
7241
7242 /* If we moved too far back, move IT some lines forward. */
7243 if (it2.vpos > -dvpos)
7244 {
7245 int delta = it2.vpos + dvpos;
7246 it2 = *it;
7247 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7248 /* Move back again if we got too far ahead. */
7249 if (IT_CHARPOS (*it) >= start_charpos)
7250 *it = it2;
7251 }
7252 }
7253 }
7254
7255 /* Return 1 if IT points into the middle of a display vector. */
7256
7257 int
7258 in_display_vector_p (it)
7259 struct it *it;
7260 {
7261 return (it->method == GET_FROM_DISPLAY_VECTOR
7262 && it->current.dpvec_index > 0
7263 && it->dpvec + it->current.dpvec_index != it->dpend);
7264 }
7265
7266 \f
7267 /***********************************************************************
7268 Messages
7269 ***********************************************************************/
7270
7271
7272 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7273 to *Messages*. */
7274
7275 void
7276 add_to_log (format, arg1, arg2)
7277 char *format;
7278 Lisp_Object arg1, arg2;
7279 {
7280 Lisp_Object args[3];
7281 Lisp_Object msg, fmt;
7282 char *buffer;
7283 int len;
7284 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7285 USE_SAFE_ALLOCA;
7286
7287 /* Do nothing if called asynchronously. Inserting text into
7288 a buffer may call after-change-functions and alike and
7289 that would means running Lisp asynchronously. */
7290 if (handling_signal)
7291 return;
7292
7293 fmt = msg = Qnil;
7294 GCPRO4 (fmt, msg, arg1, arg2);
7295
7296 args[0] = fmt = build_string (format);
7297 args[1] = arg1;
7298 args[2] = arg2;
7299 msg = Fformat (3, args);
7300
7301 len = SBYTES (msg) + 1;
7302 SAFE_ALLOCA (buffer, char *, len);
7303 bcopy (SDATA (msg), buffer, len);
7304
7305 message_dolog (buffer, len - 1, 1, 0);
7306 SAFE_FREE ();
7307
7308 UNGCPRO;
7309 }
7310
7311
7312 /* Output a newline in the *Messages* buffer if "needs" one. */
7313
7314 void
7315 message_log_maybe_newline ()
7316 {
7317 if (message_log_need_newline)
7318 message_dolog ("", 0, 1, 0);
7319 }
7320
7321
7322 /* Add a string M of length NBYTES to the message log, optionally
7323 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7324 nonzero, means interpret the contents of M as multibyte. This
7325 function calls low-level routines in order to bypass text property
7326 hooks, etc. which might not be safe to run.
7327
7328 This may GC (insert may run before/after change hooks),
7329 so the buffer M must NOT point to a Lisp string. */
7330
7331 void
7332 message_dolog (m, nbytes, nlflag, multibyte)
7333 const char *m;
7334 int nbytes, nlflag, multibyte;
7335 {
7336 if (!NILP (Vmemory_full))
7337 return;
7338
7339 if (!NILP (Vmessage_log_max))
7340 {
7341 struct buffer *oldbuf;
7342 Lisp_Object oldpoint, oldbegv, oldzv;
7343 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7344 int point_at_end = 0;
7345 int zv_at_end = 0;
7346 Lisp_Object old_deactivate_mark, tem;
7347 struct gcpro gcpro1;
7348
7349 old_deactivate_mark = Vdeactivate_mark;
7350 oldbuf = current_buffer;
7351 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7352 current_buffer->undo_list = Qt;
7353
7354 oldpoint = message_dolog_marker1;
7355 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7356 oldbegv = message_dolog_marker2;
7357 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7358 oldzv = message_dolog_marker3;
7359 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7360 GCPRO1 (old_deactivate_mark);
7361
7362 if (PT == Z)
7363 point_at_end = 1;
7364 if (ZV == Z)
7365 zv_at_end = 1;
7366
7367 BEGV = BEG;
7368 BEGV_BYTE = BEG_BYTE;
7369 ZV = Z;
7370 ZV_BYTE = Z_BYTE;
7371 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7372
7373 /* Insert the string--maybe converting multibyte to single byte
7374 or vice versa, so that all the text fits the buffer. */
7375 if (multibyte
7376 && NILP (current_buffer->enable_multibyte_characters))
7377 {
7378 int i, c, char_bytes;
7379 unsigned char work[1];
7380
7381 /* Convert a multibyte string to single-byte
7382 for the *Message* buffer. */
7383 for (i = 0; i < nbytes; i += char_bytes)
7384 {
7385 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7386 work[0] = (SINGLE_BYTE_CHAR_P (c)
7387 ? c
7388 : multibyte_char_to_unibyte (c, Qnil));
7389 insert_1_both (work, 1, 1, 1, 0, 0);
7390 }
7391 }
7392 else if (! multibyte
7393 && ! NILP (current_buffer->enable_multibyte_characters))
7394 {
7395 int i, c, char_bytes;
7396 unsigned char *msg = (unsigned char *) m;
7397 unsigned char str[MAX_MULTIBYTE_LENGTH];
7398 /* Convert a single-byte string to multibyte
7399 for the *Message* buffer. */
7400 for (i = 0; i < nbytes; i++)
7401 {
7402 c = unibyte_char_to_multibyte (msg[i]);
7403 char_bytes = CHAR_STRING (c, str);
7404 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7405 }
7406 }
7407 else if (nbytes)
7408 insert_1 (m, nbytes, 1, 0, 0);
7409
7410 if (nlflag)
7411 {
7412 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7413 insert_1 ("\n", 1, 1, 0, 0);
7414
7415 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7416 this_bol = PT;
7417 this_bol_byte = PT_BYTE;
7418
7419 /* See if this line duplicates the previous one.
7420 If so, combine duplicates. */
7421 if (this_bol > BEG)
7422 {
7423 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7424 prev_bol = PT;
7425 prev_bol_byte = PT_BYTE;
7426
7427 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7428 this_bol, this_bol_byte);
7429 if (dup)
7430 {
7431 del_range_both (prev_bol, prev_bol_byte,
7432 this_bol, this_bol_byte, 0);
7433 if (dup > 1)
7434 {
7435 char dupstr[40];
7436 int duplen;
7437
7438 /* If you change this format, don't forget to also
7439 change message_log_check_duplicate. */
7440 sprintf (dupstr, " [%d times]", dup);
7441 duplen = strlen (dupstr);
7442 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7443 insert_1 (dupstr, duplen, 1, 0, 1);
7444 }
7445 }
7446 }
7447
7448 /* If we have more than the desired maximum number of lines
7449 in the *Messages* buffer now, delete the oldest ones.
7450 This is safe because we don't have undo in this buffer. */
7451
7452 if (NATNUMP (Vmessage_log_max))
7453 {
7454 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7455 -XFASTINT (Vmessage_log_max) - 1, 0);
7456 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7457 }
7458 }
7459 BEGV = XMARKER (oldbegv)->charpos;
7460 BEGV_BYTE = marker_byte_position (oldbegv);
7461
7462 if (zv_at_end)
7463 {
7464 ZV = Z;
7465 ZV_BYTE = Z_BYTE;
7466 }
7467 else
7468 {
7469 ZV = XMARKER (oldzv)->charpos;
7470 ZV_BYTE = marker_byte_position (oldzv);
7471 }
7472
7473 if (point_at_end)
7474 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7475 else
7476 /* We can't do Fgoto_char (oldpoint) because it will run some
7477 Lisp code. */
7478 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7479 XMARKER (oldpoint)->bytepos);
7480
7481 UNGCPRO;
7482 unchain_marker (XMARKER (oldpoint));
7483 unchain_marker (XMARKER (oldbegv));
7484 unchain_marker (XMARKER (oldzv));
7485
7486 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7487 set_buffer_internal (oldbuf);
7488 if (NILP (tem))
7489 windows_or_buffers_changed = old_windows_or_buffers_changed;
7490 message_log_need_newline = !nlflag;
7491 Vdeactivate_mark = old_deactivate_mark;
7492 }
7493 }
7494
7495
7496 /* We are at the end of the buffer after just having inserted a newline.
7497 (Note: We depend on the fact we won't be crossing the gap.)
7498 Check to see if the most recent message looks a lot like the previous one.
7499 Return 0 if different, 1 if the new one should just replace it, or a
7500 value N > 1 if we should also append " [N times]". */
7501
7502 static int
7503 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7504 int prev_bol, this_bol;
7505 int prev_bol_byte, this_bol_byte;
7506 {
7507 int i;
7508 int len = Z_BYTE - 1 - this_bol_byte;
7509 int seen_dots = 0;
7510 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7511 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7512
7513 for (i = 0; i < len; i++)
7514 {
7515 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7516 seen_dots = 1;
7517 if (p1[i] != p2[i])
7518 return seen_dots;
7519 }
7520 p1 += len;
7521 if (*p1 == '\n')
7522 return 2;
7523 if (*p1++ == ' ' && *p1++ == '[')
7524 {
7525 int n = 0;
7526 while (*p1 >= '0' && *p1 <= '9')
7527 n = n * 10 + *p1++ - '0';
7528 if (strncmp (p1, " times]\n", 8) == 0)
7529 return n+1;
7530 }
7531 return 0;
7532 }
7533 \f
7534
7535 /* Display an echo area message M with a specified length of NBYTES
7536 bytes. The string may include null characters. If M is 0, clear
7537 out any existing message, and let the mini-buffer text show
7538 through.
7539
7540 This may GC, so the buffer M must NOT point to a Lisp string. */
7541
7542 void
7543 message2 (m, nbytes, multibyte)
7544 const char *m;
7545 int nbytes;
7546 int multibyte;
7547 {
7548 /* First flush out any partial line written with print. */
7549 message_log_maybe_newline ();
7550 if (m)
7551 message_dolog (m, nbytes, 1, multibyte);
7552 message2_nolog (m, nbytes, multibyte);
7553 }
7554
7555
7556 /* The non-logging counterpart of message2. */
7557
7558 void
7559 message2_nolog (m, nbytes, multibyte)
7560 const char *m;
7561 int nbytes, multibyte;
7562 {
7563 struct frame *sf = SELECTED_FRAME ();
7564 message_enable_multibyte = multibyte;
7565
7566 if (noninteractive)
7567 {
7568 if (noninteractive_need_newline)
7569 putc ('\n', stderr);
7570 noninteractive_need_newline = 0;
7571 if (m)
7572 fwrite (m, nbytes, 1, stderr);
7573 if (cursor_in_echo_area == 0)
7574 fprintf (stderr, "\n");
7575 fflush (stderr);
7576 }
7577 /* A null message buffer means that the frame hasn't really been
7578 initialized yet. Error messages get reported properly by
7579 cmd_error, so this must be just an informative message; toss it. */
7580 else if (INTERACTIVE
7581 && sf->glyphs_initialized_p
7582 && FRAME_MESSAGE_BUF (sf))
7583 {
7584 Lisp_Object mini_window;
7585 struct frame *f;
7586
7587 /* Get the frame containing the mini-buffer
7588 that the selected frame is using. */
7589 mini_window = FRAME_MINIBUF_WINDOW (sf);
7590 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7591
7592 FRAME_SAMPLE_VISIBILITY (f);
7593 if (FRAME_VISIBLE_P (sf)
7594 && ! FRAME_VISIBLE_P (f))
7595 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7596
7597 if (m)
7598 {
7599 set_message (m, Qnil, nbytes, multibyte);
7600 if (minibuffer_auto_raise)
7601 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7602 }
7603 else
7604 clear_message (1, 1);
7605
7606 do_pending_window_change (0);
7607 echo_area_display (1);
7608 do_pending_window_change (0);
7609 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7610 (*frame_up_to_date_hook) (f);
7611 }
7612 }
7613
7614
7615 /* Display an echo area message M with a specified length of NBYTES
7616 bytes. The string may include null characters. If M is not a
7617 string, clear out any existing message, and let the mini-buffer
7618 text show through.
7619
7620 This function cancels echoing. */
7621
7622 void
7623 message3 (m, nbytes, multibyte)
7624 Lisp_Object m;
7625 int nbytes;
7626 int multibyte;
7627 {
7628 struct gcpro gcpro1;
7629
7630 GCPRO1 (m);
7631 clear_message (1,1);
7632 cancel_echoing ();
7633
7634 /* First flush out any partial line written with print. */
7635 message_log_maybe_newline ();
7636 if (STRINGP (m))
7637 {
7638 char *buffer;
7639 USE_SAFE_ALLOCA;
7640
7641 SAFE_ALLOCA (buffer, char *, nbytes);
7642 bcopy (SDATA (m), buffer, nbytes);
7643 message_dolog (buffer, nbytes, 1, multibyte);
7644 SAFE_FREE ();
7645 }
7646 message3_nolog (m, nbytes, multibyte);
7647
7648 UNGCPRO;
7649 }
7650
7651
7652 /* The non-logging version of message3.
7653 This does not cancel echoing, because it is used for echoing.
7654 Perhaps we need to make a separate function for echoing
7655 and make this cancel echoing. */
7656
7657 void
7658 message3_nolog (m, nbytes, multibyte)
7659 Lisp_Object m;
7660 int nbytes, multibyte;
7661 {
7662 struct frame *sf = SELECTED_FRAME ();
7663 message_enable_multibyte = multibyte;
7664
7665 if (noninteractive)
7666 {
7667 if (noninteractive_need_newline)
7668 putc ('\n', stderr);
7669 noninteractive_need_newline = 0;
7670 if (STRINGP (m))
7671 fwrite (SDATA (m), nbytes, 1, stderr);
7672 if (cursor_in_echo_area == 0)
7673 fprintf (stderr, "\n");
7674 fflush (stderr);
7675 }
7676 /* A null message buffer means that the frame hasn't really been
7677 initialized yet. Error messages get reported properly by
7678 cmd_error, so this must be just an informative message; toss it. */
7679 else if (INTERACTIVE
7680 && sf->glyphs_initialized_p
7681 && FRAME_MESSAGE_BUF (sf))
7682 {
7683 Lisp_Object mini_window;
7684 Lisp_Object frame;
7685 struct frame *f;
7686
7687 /* Get the frame containing the mini-buffer
7688 that the selected frame is using. */
7689 mini_window = FRAME_MINIBUF_WINDOW (sf);
7690 frame = XWINDOW (mini_window)->frame;
7691 f = XFRAME (frame);
7692
7693 FRAME_SAMPLE_VISIBILITY (f);
7694 if (FRAME_VISIBLE_P (sf)
7695 && !FRAME_VISIBLE_P (f))
7696 Fmake_frame_visible (frame);
7697
7698 if (STRINGP (m) && SCHARS (m) > 0)
7699 {
7700 set_message (NULL, m, nbytes, multibyte);
7701 if (minibuffer_auto_raise)
7702 Fraise_frame (frame);
7703 /* Assume we are not echoing.
7704 (If we are, echo_now will override this.) */
7705 echo_message_buffer = Qnil;
7706 }
7707 else
7708 clear_message (1, 1);
7709
7710 do_pending_window_change (0);
7711 echo_area_display (1);
7712 do_pending_window_change (0);
7713 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7714 (*frame_up_to_date_hook) (f);
7715 }
7716 }
7717
7718
7719 /* Display a null-terminated echo area message M. If M is 0, clear
7720 out any existing message, and let the mini-buffer text show through.
7721
7722 The buffer M must continue to exist until after the echo area gets
7723 cleared or some other message gets displayed there. Do not pass
7724 text that is stored in a Lisp string. Do not pass text in a buffer
7725 that was alloca'd. */
7726
7727 void
7728 message1 (m)
7729 char *m;
7730 {
7731 message2 (m, (m ? strlen (m) : 0), 0);
7732 }
7733
7734
7735 /* The non-logging counterpart of message1. */
7736
7737 void
7738 message1_nolog (m)
7739 char *m;
7740 {
7741 message2_nolog (m, (m ? strlen (m) : 0), 0);
7742 }
7743
7744 /* Display a message M which contains a single %s
7745 which gets replaced with STRING. */
7746
7747 void
7748 message_with_string (m, string, log)
7749 char *m;
7750 Lisp_Object string;
7751 int log;
7752 {
7753 CHECK_STRING (string);
7754
7755 if (noninteractive)
7756 {
7757 if (m)
7758 {
7759 if (noninteractive_need_newline)
7760 putc ('\n', stderr);
7761 noninteractive_need_newline = 0;
7762 fprintf (stderr, m, SDATA (string));
7763 if (cursor_in_echo_area == 0)
7764 fprintf (stderr, "\n");
7765 fflush (stderr);
7766 }
7767 }
7768 else if (INTERACTIVE)
7769 {
7770 /* The frame whose minibuffer we're going to display the message on.
7771 It may be larger than the selected frame, so we need
7772 to use its buffer, not the selected frame's buffer. */
7773 Lisp_Object mini_window;
7774 struct frame *f, *sf = SELECTED_FRAME ();
7775
7776 /* Get the frame containing the minibuffer
7777 that the selected frame is using. */
7778 mini_window = FRAME_MINIBUF_WINDOW (sf);
7779 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7780
7781 /* A null message buffer means that the frame hasn't really been
7782 initialized yet. Error messages get reported properly by
7783 cmd_error, so this must be just an informative message; toss it. */
7784 if (FRAME_MESSAGE_BUF (f))
7785 {
7786 Lisp_Object args[2], message;
7787 struct gcpro gcpro1, gcpro2;
7788
7789 args[0] = build_string (m);
7790 args[1] = message = string;
7791 GCPRO2 (args[0], message);
7792 gcpro1.nvars = 2;
7793
7794 message = Fformat (2, args);
7795
7796 if (log)
7797 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7798 else
7799 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7800
7801 UNGCPRO;
7802
7803 /* Print should start at the beginning of the message
7804 buffer next time. */
7805 message_buf_print = 0;
7806 }
7807 }
7808 }
7809
7810
7811 /* Dump an informative message to the minibuf. If M is 0, clear out
7812 any existing message, and let the mini-buffer text show through. */
7813
7814 /* VARARGS 1 */
7815 void
7816 message (m, a1, a2, a3)
7817 char *m;
7818 EMACS_INT a1, a2, a3;
7819 {
7820 if (noninteractive)
7821 {
7822 if (m)
7823 {
7824 if (noninteractive_need_newline)
7825 putc ('\n', stderr);
7826 noninteractive_need_newline = 0;
7827 fprintf (stderr, m, a1, a2, a3);
7828 if (cursor_in_echo_area == 0)
7829 fprintf (stderr, "\n");
7830 fflush (stderr);
7831 }
7832 }
7833 else if (INTERACTIVE)
7834 {
7835 /* The frame whose mini-buffer we're going to display the message
7836 on. It may be larger than the selected frame, so we need to
7837 use its buffer, not the selected frame's buffer. */
7838 Lisp_Object mini_window;
7839 struct frame *f, *sf = SELECTED_FRAME ();
7840
7841 /* Get the frame containing the mini-buffer
7842 that the selected frame is using. */
7843 mini_window = FRAME_MINIBUF_WINDOW (sf);
7844 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7845
7846 /* A null message buffer means that the frame hasn't really been
7847 initialized yet. Error messages get reported properly by
7848 cmd_error, so this must be just an informative message; toss
7849 it. */
7850 if (FRAME_MESSAGE_BUF (f))
7851 {
7852 if (m)
7853 {
7854 int len;
7855 #ifdef NO_ARG_ARRAY
7856 char *a[3];
7857 a[0] = (char *) a1;
7858 a[1] = (char *) a2;
7859 a[2] = (char *) a3;
7860
7861 len = doprnt (FRAME_MESSAGE_BUF (f),
7862 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7863 #else
7864 len = doprnt (FRAME_MESSAGE_BUF (f),
7865 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7866 (char **) &a1);
7867 #endif /* NO_ARG_ARRAY */
7868
7869 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7870 }
7871 else
7872 message1 (0);
7873
7874 /* Print should start at the beginning of the message
7875 buffer next time. */
7876 message_buf_print = 0;
7877 }
7878 }
7879 }
7880
7881
7882 /* The non-logging version of message. */
7883
7884 void
7885 message_nolog (m, a1, a2, a3)
7886 char *m;
7887 EMACS_INT a1, a2, a3;
7888 {
7889 Lisp_Object old_log_max;
7890 old_log_max = Vmessage_log_max;
7891 Vmessage_log_max = Qnil;
7892 message (m, a1, a2, a3);
7893 Vmessage_log_max = old_log_max;
7894 }
7895
7896
7897 /* Display the current message in the current mini-buffer. This is
7898 only called from error handlers in process.c, and is not time
7899 critical. */
7900
7901 void
7902 update_echo_area ()
7903 {
7904 if (!NILP (echo_area_buffer[0]))
7905 {
7906 Lisp_Object string;
7907 string = Fcurrent_message ();
7908 message3 (string, SBYTES (string),
7909 !NILP (current_buffer->enable_multibyte_characters));
7910 }
7911 }
7912
7913
7914 /* Make sure echo area buffers in `echo_buffers' are live.
7915 If they aren't, make new ones. */
7916
7917 static void
7918 ensure_echo_area_buffers ()
7919 {
7920 int i;
7921
7922 for (i = 0; i < 2; ++i)
7923 if (!BUFFERP (echo_buffer[i])
7924 || NILP (XBUFFER (echo_buffer[i])->name))
7925 {
7926 char name[30];
7927 Lisp_Object old_buffer;
7928 int j;
7929
7930 old_buffer = echo_buffer[i];
7931 sprintf (name, " *Echo Area %d*", i);
7932 echo_buffer[i] = Fget_buffer_create (build_string (name));
7933 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7934
7935 for (j = 0; j < 2; ++j)
7936 if (EQ (old_buffer, echo_area_buffer[j]))
7937 echo_area_buffer[j] = echo_buffer[i];
7938 }
7939 }
7940
7941
7942 /* Call FN with args A1..A4 with either the current or last displayed
7943 echo_area_buffer as current buffer.
7944
7945 WHICH zero means use the current message buffer
7946 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7947 from echo_buffer[] and clear it.
7948
7949 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7950 suitable buffer from echo_buffer[] and clear it.
7951
7952 Value is what FN returns. */
7953
7954 static int
7955 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7956 struct window *w;
7957 int which;
7958 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7959 EMACS_INT a1;
7960 Lisp_Object a2;
7961 EMACS_INT a3, a4;
7962 {
7963 Lisp_Object buffer;
7964 int this_one, the_other, clear_buffer_p, rc;
7965 int count = SPECPDL_INDEX ();
7966
7967 /* If buffers aren't live, make new ones. */
7968 ensure_echo_area_buffers ();
7969
7970 clear_buffer_p = 0;
7971
7972 if (which == 0)
7973 this_one = 0, the_other = 1;
7974 else if (which > 0)
7975 this_one = 1, the_other = 0;
7976
7977 /* Choose a suitable buffer from echo_buffer[] is we don't
7978 have one. */
7979 if (NILP (echo_area_buffer[this_one]))
7980 {
7981 echo_area_buffer[this_one]
7982 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7983 ? echo_buffer[the_other]
7984 : echo_buffer[this_one]);
7985 clear_buffer_p = 1;
7986 }
7987
7988 buffer = echo_area_buffer[this_one];
7989
7990 /* Don't get confused by reusing the buffer used for echoing
7991 for a different purpose. */
7992 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7993 cancel_echoing ();
7994
7995 record_unwind_protect (unwind_with_echo_area_buffer,
7996 with_echo_area_buffer_unwind_data (w));
7997
7998 /* Make the echo area buffer current. Note that for display
7999 purposes, it is not necessary that the displayed window's buffer
8000 == current_buffer, except for text property lookup. So, let's
8001 only set that buffer temporarily here without doing a full
8002 Fset_window_buffer. We must also change w->pointm, though,
8003 because otherwise an assertions in unshow_buffer fails, and Emacs
8004 aborts. */
8005 set_buffer_internal_1 (XBUFFER (buffer));
8006 if (w)
8007 {
8008 w->buffer = buffer;
8009 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8010 }
8011
8012 current_buffer->undo_list = Qt;
8013 current_buffer->read_only = Qnil;
8014 specbind (Qinhibit_read_only, Qt);
8015 specbind (Qinhibit_modification_hooks, Qt);
8016
8017 if (clear_buffer_p && Z > BEG)
8018 del_range (BEG, Z);
8019
8020 xassert (BEGV >= BEG);
8021 xassert (ZV <= Z && ZV >= BEGV);
8022
8023 rc = fn (a1, a2, a3, a4);
8024
8025 xassert (BEGV >= BEG);
8026 xassert (ZV <= Z && ZV >= BEGV);
8027
8028 unbind_to (count, Qnil);
8029 return rc;
8030 }
8031
8032
8033 /* Save state that should be preserved around the call to the function
8034 FN called in with_echo_area_buffer. */
8035
8036 static Lisp_Object
8037 with_echo_area_buffer_unwind_data (w)
8038 struct window *w;
8039 {
8040 int i = 0;
8041 Lisp_Object vector;
8042
8043 /* Reduce consing by keeping one vector in
8044 Vwith_echo_area_save_vector. */
8045 vector = Vwith_echo_area_save_vector;
8046 Vwith_echo_area_save_vector = Qnil;
8047
8048 if (NILP (vector))
8049 vector = Fmake_vector (make_number (7), Qnil);
8050
8051 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
8052 AREF (vector, i) = Vdeactivate_mark, ++i;
8053 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
8054
8055 if (w)
8056 {
8057 XSETWINDOW (AREF (vector, i), w); ++i;
8058 AREF (vector, i) = w->buffer; ++i;
8059 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
8060 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
8061 }
8062 else
8063 {
8064 int end = i + 4;
8065 for (; i < end; ++i)
8066 AREF (vector, i) = Qnil;
8067 }
8068
8069 xassert (i == ASIZE (vector));
8070 return vector;
8071 }
8072
8073
8074 /* Restore global state from VECTOR which was created by
8075 with_echo_area_buffer_unwind_data. */
8076
8077 static Lisp_Object
8078 unwind_with_echo_area_buffer (vector)
8079 Lisp_Object vector;
8080 {
8081 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8082 Vdeactivate_mark = AREF (vector, 1);
8083 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8084
8085 if (WINDOWP (AREF (vector, 3)))
8086 {
8087 struct window *w;
8088 Lisp_Object buffer, charpos, bytepos;
8089
8090 w = XWINDOW (AREF (vector, 3));
8091 buffer = AREF (vector, 4);
8092 charpos = AREF (vector, 5);
8093 bytepos = AREF (vector, 6);
8094
8095 w->buffer = buffer;
8096 set_marker_both (w->pointm, buffer,
8097 XFASTINT (charpos), XFASTINT (bytepos));
8098 }
8099
8100 Vwith_echo_area_save_vector = vector;
8101 return Qnil;
8102 }
8103
8104
8105 /* Set up the echo area for use by print functions. MULTIBYTE_P
8106 non-zero means we will print multibyte. */
8107
8108 void
8109 setup_echo_area_for_printing (multibyte_p)
8110 int multibyte_p;
8111 {
8112 /* If we can't find an echo area any more, exit. */
8113 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8114 Fkill_emacs (Qnil);
8115
8116 ensure_echo_area_buffers ();
8117
8118 if (!message_buf_print)
8119 {
8120 /* A message has been output since the last time we printed.
8121 Choose a fresh echo area buffer. */
8122 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8123 echo_area_buffer[0] = echo_buffer[1];
8124 else
8125 echo_area_buffer[0] = echo_buffer[0];
8126
8127 /* Switch to that buffer and clear it. */
8128 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8129 current_buffer->truncate_lines = Qnil;
8130
8131 if (Z > BEG)
8132 {
8133 int count = SPECPDL_INDEX ();
8134 specbind (Qinhibit_read_only, Qt);
8135 /* Note that undo recording is always disabled. */
8136 del_range (BEG, Z);
8137 unbind_to (count, Qnil);
8138 }
8139 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8140
8141 /* Set up the buffer for the multibyteness we need. */
8142 if (multibyte_p
8143 != !NILP (current_buffer->enable_multibyte_characters))
8144 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8145
8146 /* Raise the frame containing the echo area. */
8147 if (minibuffer_auto_raise)
8148 {
8149 struct frame *sf = SELECTED_FRAME ();
8150 Lisp_Object mini_window;
8151 mini_window = FRAME_MINIBUF_WINDOW (sf);
8152 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8153 }
8154
8155 message_log_maybe_newline ();
8156 message_buf_print = 1;
8157 }
8158 else
8159 {
8160 if (NILP (echo_area_buffer[0]))
8161 {
8162 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8163 echo_area_buffer[0] = echo_buffer[1];
8164 else
8165 echo_area_buffer[0] = echo_buffer[0];
8166 }
8167
8168 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8169 {
8170 /* Someone switched buffers between print requests. */
8171 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8172 current_buffer->truncate_lines = Qnil;
8173 }
8174 }
8175 }
8176
8177
8178 /* Display an echo area message in window W. Value is non-zero if W's
8179 height is changed. If display_last_displayed_message_p is
8180 non-zero, display the message that was last displayed, otherwise
8181 display the current message. */
8182
8183 static int
8184 display_echo_area (w)
8185 struct window *w;
8186 {
8187 int i, no_message_p, window_height_changed_p, count;
8188
8189 /* Temporarily disable garbage collections while displaying the echo
8190 area. This is done because a GC can print a message itself.
8191 That message would modify the echo area buffer's contents while a
8192 redisplay of the buffer is going on, and seriously confuse
8193 redisplay. */
8194 count = inhibit_garbage_collection ();
8195
8196 /* If there is no message, we must call display_echo_area_1
8197 nevertheless because it resizes the window. But we will have to
8198 reset the echo_area_buffer in question to nil at the end because
8199 with_echo_area_buffer will sets it to an empty buffer. */
8200 i = display_last_displayed_message_p ? 1 : 0;
8201 no_message_p = NILP (echo_area_buffer[i]);
8202
8203 window_height_changed_p
8204 = with_echo_area_buffer (w, display_last_displayed_message_p,
8205 display_echo_area_1,
8206 (EMACS_INT) w, Qnil, 0, 0);
8207
8208 if (no_message_p)
8209 echo_area_buffer[i] = Qnil;
8210
8211 unbind_to (count, Qnil);
8212 return window_height_changed_p;
8213 }
8214
8215
8216 /* Helper for display_echo_area. Display the current buffer which
8217 contains the current echo area message in window W, a mini-window,
8218 a pointer to which is passed in A1. A2..A4 are currently not used.
8219 Change the height of W so that all of the message is displayed.
8220 Value is non-zero if height of W was changed. */
8221
8222 static int
8223 display_echo_area_1 (a1, a2, a3, a4)
8224 EMACS_INT a1;
8225 Lisp_Object a2;
8226 EMACS_INT a3, a4;
8227 {
8228 struct window *w = (struct window *) a1;
8229 Lisp_Object window;
8230 struct text_pos start;
8231 int window_height_changed_p = 0;
8232
8233 /* Do this before displaying, so that we have a large enough glyph
8234 matrix for the display. If we can't get enough space for the
8235 whole text, display the last N lines. That works by setting w->start. */
8236 window_height_changed_p = resize_mini_window (w, 0);
8237
8238 /* Use the starting position chosen by resize_mini_window. */
8239 SET_TEXT_POS_FROM_MARKER (start, w->start);
8240
8241 /* Display. */
8242 clear_glyph_matrix (w->desired_matrix);
8243 XSETWINDOW (window, w);
8244 try_window (window, start, 0);
8245
8246 return window_height_changed_p;
8247 }
8248
8249
8250 /* Resize the echo area window to exactly the size needed for the
8251 currently displayed message, if there is one. If a mini-buffer
8252 is active, don't shrink it. */
8253
8254 void
8255 resize_echo_area_exactly ()
8256 {
8257 if (BUFFERP (echo_area_buffer[0])
8258 && WINDOWP (echo_area_window))
8259 {
8260 struct window *w = XWINDOW (echo_area_window);
8261 int resized_p;
8262 Lisp_Object resize_exactly;
8263
8264 if (minibuf_level == 0)
8265 resize_exactly = Qt;
8266 else
8267 resize_exactly = Qnil;
8268
8269 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8270 (EMACS_INT) w, resize_exactly, 0, 0);
8271 if (resized_p)
8272 {
8273 ++windows_or_buffers_changed;
8274 ++update_mode_lines;
8275 redisplay_internal (0);
8276 }
8277 }
8278 }
8279
8280
8281 /* Callback function for with_echo_area_buffer, when used from
8282 resize_echo_area_exactly. A1 contains a pointer to the window to
8283 resize, EXACTLY non-nil means resize the mini-window exactly to the
8284 size of the text displayed. A3 and A4 are not used. Value is what
8285 resize_mini_window returns. */
8286
8287 static int
8288 resize_mini_window_1 (a1, exactly, a3, a4)
8289 EMACS_INT a1;
8290 Lisp_Object exactly;
8291 EMACS_INT a3, a4;
8292 {
8293 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8294 }
8295
8296
8297 /* Resize mini-window W to fit the size of its contents. EXACT_P
8298 means size the window exactly to the size needed. Otherwise, it's
8299 only enlarged until W's buffer is empty.
8300
8301 Set W->start to the right place to begin display. If the whole
8302 contents fit, start at the beginning. Otherwise, start so as
8303 to make the end of the contents appear. This is particularly
8304 important for y-or-n-p, but seems desirable generally.
8305
8306 Value is non-zero if the window height has been changed. */
8307
8308 int
8309 resize_mini_window (w, exact_p)
8310 struct window *w;
8311 int exact_p;
8312 {
8313 struct frame *f = XFRAME (w->frame);
8314 int window_height_changed_p = 0;
8315
8316 xassert (MINI_WINDOW_P (w));
8317
8318 /* By default, start display at the beginning. */
8319 set_marker_both (w->start, w->buffer,
8320 BUF_BEGV (XBUFFER (w->buffer)),
8321 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8322
8323 /* Don't resize windows while redisplaying a window; it would
8324 confuse redisplay functions when the size of the window they are
8325 displaying changes from under them. Such a resizing can happen,
8326 for instance, when which-func prints a long message while
8327 we are running fontification-functions. We're running these
8328 functions with safe_call which binds inhibit-redisplay to t. */
8329 if (!NILP (Vinhibit_redisplay))
8330 return 0;
8331
8332 /* Nil means don't try to resize. */
8333 if (NILP (Vresize_mini_windows)
8334 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8335 return 0;
8336
8337 if (!FRAME_MINIBUF_ONLY_P (f))
8338 {
8339 struct it it;
8340 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8341 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8342 int height, max_height;
8343 int unit = FRAME_LINE_HEIGHT (f);
8344 struct text_pos start;
8345 struct buffer *old_current_buffer = NULL;
8346
8347 if (current_buffer != XBUFFER (w->buffer))
8348 {
8349 old_current_buffer = current_buffer;
8350 set_buffer_internal (XBUFFER (w->buffer));
8351 }
8352
8353 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8354
8355 /* Compute the max. number of lines specified by the user. */
8356 if (FLOATP (Vmax_mini_window_height))
8357 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8358 else if (INTEGERP (Vmax_mini_window_height))
8359 max_height = XINT (Vmax_mini_window_height);
8360 else
8361 max_height = total_height / 4;
8362
8363 /* Correct that max. height if it's bogus. */
8364 max_height = max (1, max_height);
8365 max_height = min (total_height, max_height);
8366
8367 /* Find out the height of the text in the window. */
8368 if (it.truncate_lines_p)
8369 height = 1;
8370 else
8371 {
8372 last_height = 0;
8373 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8374 if (it.max_ascent == 0 && it.max_descent == 0)
8375 height = it.current_y + last_height;
8376 else
8377 height = it.current_y + it.max_ascent + it.max_descent;
8378 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8379 height = (height + unit - 1) / unit;
8380 }
8381
8382 /* Compute a suitable window start. */
8383 if (height > max_height)
8384 {
8385 height = max_height;
8386 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8387 move_it_vertically_backward (&it, (height - 1) * unit);
8388 start = it.current.pos;
8389 }
8390 else
8391 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8392 SET_MARKER_FROM_TEXT_POS (w->start, start);
8393
8394 if (EQ (Vresize_mini_windows, Qgrow_only))
8395 {
8396 /* Let it grow only, until we display an empty message, in which
8397 case the window shrinks again. */
8398 if (height > WINDOW_TOTAL_LINES (w))
8399 {
8400 int old_height = WINDOW_TOTAL_LINES (w);
8401 freeze_window_starts (f, 1);
8402 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8403 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8404 }
8405 else if (height < WINDOW_TOTAL_LINES (w)
8406 && (exact_p || BEGV == ZV))
8407 {
8408 int old_height = WINDOW_TOTAL_LINES (w);
8409 freeze_window_starts (f, 0);
8410 shrink_mini_window (w);
8411 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8412 }
8413 }
8414 else
8415 {
8416 /* Always resize to exact size needed. */
8417 if (height > WINDOW_TOTAL_LINES (w))
8418 {
8419 int old_height = WINDOW_TOTAL_LINES (w);
8420 freeze_window_starts (f, 1);
8421 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8422 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8423 }
8424 else if (height < WINDOW_TOTAL_LINES (w))
8425 {
8426 int old_height = WINDOW_TOTAL_LINES (w);
8427 freeze_window_starts (f, 0);
8428 shrink_mini_window (w);
8429
8430 if (height)
8431 {
8432 freeze_window_starts (f, 1);
8433 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8434 }
8435
8436 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8437 }
8438 }
8439
8440 if (old_current_buffer)
8441 set_buffer_internal (old_current_buffer);
8442 }
8443
8444 return window_height_changed_p;
8445 }
8446
8447
8448 /* Value is the current message, a string, or nil if there is no
8449 current message. */
8450
8451 Lisp_Object
8452 current_message ()
8453 {
8454 Lisp_Object msg;
8455
8456 if (NILP (echo_area_buffer[0]))
8457 msg = Qnil;
8458 else
8459 {
8460 with_echo_area_buffer (0, 0, current_message_1,
8461 (EMACS_INT) &msg, Qnil, 0, 0);
8462 if (NILP (msg))
8463 echo_area_buffer[0] = Qnil;
8464 }
8465
8466 return msg;
8467 }
8468
8469
8470 static int
8471 current_message_1 (a1, a2, a3, a4)
8472 EMACS_INT a1;
8473 Lisp_Object a2;
8474 EMACS_INT a3, a4;
8475 {
8476 Lisp_Object *msg = (Lisp_Object *) a1;
8477
8478 if (Z > BEG)
8479 *msg = make_buffer_string (BEG, Z, 1);
8480 else
8481 *msg = Qnil;
8482 return 0;
8483 }
8484
8485
8486 /* Push the current message on Vmessage_stack for later restauration
8487 by restore_message. Value is non-zero if the current message isn't
8488 empty. This is a relatively infrequent operation, so it's not
8489 worth optimizing. */
8490
8491 int
8492 push_message ()
8493 {
8494 Lisp_Object msg;
8495 msg = current_message ();
8496 Vmessage_stack = Fcons (msg, Vmessage_stack);
8497 return STRINGP (msg);
8498 }
8499
8500
8501 /* Restore message display from the top of Vmessage_stack. */
8502
8503 void
8504 restore_message ()
8505 {
8506 Lisp_Object msg;
8507
8508 xassert (CONSP (Vmessage_stack));
8509 msg = XCAR (Vmessage_stack);
8510 if (STRINGP (msg))
8511 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8512 else
8513 message3_nolog (msg, 0, 0);
8514 }
8515
8516
8517 /* Handler for record_unwind_protect calling pop_message. */
8518
8519 Lisp_Object
8520 pop_message_unwind (dummy)
8521 Lisp_Object dummy;
8522 {
8523 pop_message ();
8524 return Qnil;
8525 }
8526
8527 /* Pop the top-most entry off Vmessage_stack. */
8528
8529 void
8530 pop_message ()
8531 {
8532 xassert (CONSP (Vmessage_stack));
8533 Vmessage_stack = XCDR (Vmessage_stack);
8534 }
8535
8536
8537 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8538 exits. If the stack is not empty, we have a missing pop_message
8539 somewhere. */
8540
8541 void
8542 check_message_stack ()
8543 {
8544 if (!NILP (Vmessage_stack))
8545 abort ();
8546 }
8547
8548
8549 /* Truncate to NCHARS what will be displayed in the echo area the next
8550 time we display it---but don't redisplay it now. */
8551
8552 void
8553 truncate_echo_area (nchars)
8554 int nchars;
8555 {
8556 if (nchars == 0)
8557 echo_area_buffer[0] = Qnil;
8558 /* A null message buffer means that the frame hasn't really been
8559 initialized yet. Error messages get reported properly by
8560 cmd_error, so this must be just an informative message; toss it. */
8561 else if (!noninteractive
8562 && INTERACTIVE
8563 && !NILP (echo_area_buffer[0]))
8564 {
8565 struct frame *sf = SELECTED_FRAME ();
8566 if (FRAME_MESSAGE_BUF (sf))
8567 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8568 }
8569 }
8570
8571
8572 /* Helper function for truncate_echo_area. Truncate the current
8573 message to at most NCHARS characters. */
8574
8575 static int
8576 truncate_message_1 (nchars, a2, a3, a4)
8577 EMACS_INT nchars;
8578 Lisp_Object a2;
8579 EMACS_INT a3, a4;
8580 {
8581 if (BEG + nchars < Z)
8582 del_range (BEG + nchars, Z);
8583 if (Z == BEG)
8584 echo_area_buffer[0] = Qnil;
8585 return 0;
8586 }
8587
8588
8589 /* Set the current message to a substring of S or STRING.
8590
8591 If STRING is a Lisp string, set the message to the first NBYTES
8592 bytes from STRING. NBYTES zero means use the whole string. If
8593 STRING is multibyte, the message will be displayed multibyte.
8594
8595 If S is not null, set the message to the first LEN bytes of S. LEN
8596 zero means use the whole string. MULTIBYTE_P non-zero means S is
8597 multibyte. Display the message multibyte in that case.
8598
8599 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8600 to t before calling set_message_1 (which calls insert).
8601 */
8602
8603 void
8604 set_message (s, string, nbytes, multibyte_p)
8605 const char *s;
8606 Lisp_Object string;
8607 int nbytes, multibyte_p;
8608 {
8609 message_enable_multibyte
8610 = ((s && multibyte_p)
8611 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8612
8613 with_echo_area_buffer (0, 0, set_message_1,
8614 (EMACS_INT) s, string, nbytes, multibyte_p);
8615 message_buf_print = 0;
8616 help_echo_showing_p = 0;
8617 }
8618
8619
8620 /* Helper function for set_message. Arguments have the same meaning
8621 as there, with A1 corresponding to S and A2 corresponding to STRING
8622 This function is called with the echo area buffer being
8623 current. */
8624
8625 static int
8626 set_message_1 (a1, a2, nbytes, multibyte_p)
8627 EMACS_INT a1;
8628 Lisp_Object a2;
8629 EMACS_INT nbytes, multibyte_p;
8630 {
8631 const char *s = (const char *) a1;
8632 Lisp_Object string = a2;
8633
8634 /* Change multibyteness of the echo buffer appropriately. */
8635 if (message_enable_multibyte
8636 != !NILP (current_buffer->enable_multibyte_characters))
8637 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8638
8639 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8640
8641 /* Insert new message at BEG. */
8642 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8643 Ferase_buffer ();
8644
8645 if (STRINGP (string))
8646 {
8647 int nchars;
8648
8649 if (nbytes == 0)
8650 nbytes = SBYTES (string);
8651 nchars = string_byte_to_char (string, nbytes);
8652
8653 /* This function takes care of single/multibyte conversion. We
8654 just have to ensure that the echo area buffer has the right
8655 setting of enable_multibyte_characters. */
8656 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8657 }
8658 else if (s)
8659 {
8660 if (nbytes == 0)
8661 nbytes = strlen (s);
8662
8663 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8664 {
8665 /* Convert from multi-byte to single-byte. */
8666 int i, c, n;
8667 unsigned char work[1];
8668
8669 /* Convert a multibyte string to single-byte. */
8670 for (i = 0; i < nbytes; i += n)
8671 {
8672 c = string_char_and_length (s + i, nbytes - i, &n);
8673 work[0] = (SINGLE_BYTE_CHAR_P (c)
8674 ? c
8675 : multibyte_char_to_unibyte (c, Qnil));
8676 insert_1_both (work, 1, 1, 1, 0, 0);
8677 }
8678 }
8679 else if (!multibyte_p
8680 && !NILP (current_buffer->enable_multibyte_characters))
8681 {
8682 /* Convert from single-byte to multi-byte. */
8683 int i, c, n;
8684 const unsigned char *msg = (const unsigned char *) s;
8685 unsigned char str[MAX_MULTIBYTE_LENGTH];
8686
8687 /* Convert a single-byte string to multibyte. */
8688 for (i = 0; i < nbytes; i++)
8689 {
8690 c = unibyte_char_to_multibyte (msg[i]);
8691 n = CHAR_STRING (c, str);
8692 insert_1_both (str, 1, n, 1, 0, 0);
8693 }
8694 }
8695 else
8696 insert_1 (s, nbytes, 1, 0, 0);
8697 }
8698
8699 return 0;
8700 }
8701
8702
8703 /* Clear messages. CURRENT_P non-zero means clear the current
8704 message. LAST_DISPLAYED_P non-zero means clear the message
8705 last displayed. */
8706
8707 void
8708 clear_message (current_p, last_displayed_p)
8709 int current_p, last_displayed_p;
8710 {
8711 if (current_p)
8712 {
8713 echo_area_buffer[0] = Qnil;
8714 message_cleared_p = 1;
8715 }
8716
8717 if (last_displayed_p)
8718 echo_area_buffer[1] = Qnil;
8719
8720 message_buf_print = 0;
8721 }
8722
8723 /* Clear garbaged frames.
8724
8725 This function is used where the old redisplay called
8726 redraw_garbaged_frames which in turn called redraw_frame which in
8727 turn called clear_frame. The call to clear_frame was a source of
8728 flickering. I believe a clear_frame is not necessary. It should
8729 suffice in the new redisplay to invalidate all current matrices,
8730 and ensure a complete redisplay of all windows. */
8731
8732 static void
8733 clear_garbaged_frames ()
8734 {
8735 if (frame_garbaged)
8736 {
8737 Lisp_Object tail, frame;
8738 int changed_count = 0;
8739
8740 FOR_EACH_FRAME (tail, frame)
8741 {
8742 struct frame *f = XFRAME (frame);
8743
8744 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8745 {
8746 if (f->resized_p)
8747 {
8748 Fredraw_frame (frame);
8749 f->force_flush_display_p = 1;
8750 }
8751 clear_current_matrices (f);
8752 changed_count++;
8753 f->garbaged = 0;
8754 f->resized_p = 0;
8755 }
8756 }
8757
8758 frame_garbaged = 0;
8759 if (changed_count)
8760 ++windows_or_buffers_changed;
8761 }
8762 }
8763
8764
8765 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8766 is non-zero update selected_frame. Value is non-zero if the
8767 mini-windows height has been changed. */
8768
8769 static int
8770 echo_area_display (update_frame_p)
8771 int update_frame_p;
8772 {
8773 Lisp_Object mini_window;
8774 struct window *w;
8775 struct frame *f;
8776 int window_height_changed_p = 0;
8777 struct frame *sf = SELECTED_FRAME ();
8778
8779 mini_window = FRAME_MINIBUF_WINDOW (sf);
8780 w = XWINDOW (mini_window);
8781 f = XFRAME (WINDOW_FRAME (w));
8782
8783 /* Don't display if frame is invisible or not yet initialized. */
8784 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8785 return 0;
8786
8787 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8788 #ifndef MAC_OS8
8789 #ifdef HAVE_WINDOW_SYSTEM
8790 /* When Emacs starts, selected_frame may be a visible terminal
8791 frame, even if we run under a window system. If we let this
8792 through, a message would be displayed on the terminal. */
8793 if (EQ (selected_frame, Vterminal_frame)
8794 && !NILP (Vwindow_system))
8795 return 0;
8796 #endif /* HAVE_WINDOW_SYSTEM */
8797 #endif
8798
8799 /* Redraw garbaged frames. */
8800 if (frame_garbaged)
8801 clear_garbaged_frames ();
8802
8803 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8804 {
8805 echo_area_window = mini_window;
8806 window_height_changed_p = display_echo_area (w);
8807 w->must_be_updated_p = 1;
8808
8809 /* Update the display, unless called from redisplay_internal.
8810 Also don't update the screen during redisplay itself. The
8811 update will happen at the end of redisplay, and an update
8812 here could cause confusion. */
8813 if (update_frame_p && !redisplaying_p)
8814 {
8815 int n = 0;
8816
8817 /* If the display update has been interrupted by pending
8818 input, update mode lines in the frame. Due to the
8819 pending input, it might have been that redisplay hasn't
8820 been called, so that mode lines above the echo area are
8821 garbaged. This looks odd, so we prevent it here. */
8822 if (!display_completed)
8823 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8824
8825 if (window_height_changed_p
8826 /* Don't do this if Emacs is shutting down. Redisplay
8827 needs to run hooks. */
8828 && !NILP (Vrun_hooks))
8829 {
8830 /* Must update other windows. Likewise as in other
8831 cases, don't let this update be interrupted by
8832 pending input. */
8833 int count = SPECPDL_INDEX ();
8834 specbind (Qredisplay_dont_pause, Qt);
8835 windows_or_buffers_changed = 1;
8836 redisplay_internal (0);
8837 unbind_to (count, Qnil);
8838 }
8839 else if (FRAME_WINDOW_P (f) && n == 0)
8840 {
8841 /* Window configuration is the same as before.
8842 Can do with a display update of the echo area,
8843 unless we displayed some mode lines. */
8844 update_single_window (w, 1);
8845 rif->flush_display (f);
8846 }
8847 else
8848 update_frame (f, 1, 1);
8849
8850 /* If cursor is in the echo area, make sure that the next
8851 redisplay displays the minibuffer, so that the cursor will
8852 be replaced with what the minibuffer wants. */
8853 if (cursor_in_echo_area)
8854 ++windows_or_buffers_changed;
8855 }
8856 }
8857 else if (!EQ (mini_window, selected_window))
8858 windows_or_buffers_changed++;
8859
8860 /* The current message is now also the last one displayed. */
8861 echo_area_buffer[1] = echo_area_buffer[0];
8862
8863 /* Prevent redisplay optimization in redisplay_internal by resetting
8864 this_line_start_pos. This is done because the mini-buffer now
8865 displays the message instead of its buffer text. */
8866 if (EQ (mini_window, selected_window))
8867 CHARPOS (this_line_start_pos) = 0;
8868
8869 return window_height_changed_p;
8870 }
8871
8872
8873 \f
8874 /***********************************************************************
8875 Mode Lines and Frame Titles
8876 ***********************************************************************/
8877
8878 /* A buffer for constructing non-propertized mode-line strings and
8879 frame titles in it; allocated from the heap in init_xdisp and
8880 resized as needed in store_mode_line_noprop_char. */
8881
8882 static char *mode_line_noprop_buf;
8883
8884 /* The buffer's end, and a current output position in it. */
8885
8886 static char *mode_line_noprop_buf_end;
8887 static char *mode_line_noprop_ptr;
8888
8889 #define MODE_LINE_NOPROP_LEN(start) \
8890 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8891
8892 static enum {
8893 MODE_LINE_DISPLAY = 0,
8894 MODE_LINE_TITLE,
8895 MODE_LINE_NOPROP,
8896 MODE_LINE_STRING
8897 } mode_line_target;
8898
8899 /* Alist that caches the results of :propertize.
8900 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8901 static Lisp_Object mode_line_proptrans_alist;
8902
8903 /* List of strings making up the mode-line. */
8904 static Lisp_Object mode_line_string_list;
8905
8906 /* Base face property when building propertized mode line string. */
8907 static Lisp_Object mode_line_string_face;
8908 static Lisp_Object mode_line_string_face_prop;
8909
8910
8911 /* Unwind data for mode line strings */
8912
8913 static Lisp_Object Vmode_line_unwind_vector;
8914
8915 static Lisp_Object
8916 format_mode_line_unwind_data (obuf, save_proptrans)
8917 struct buffer *obuf;
8918 {
8919 Lisp_Object vector;
8920
8921 /* Reduce consing by keeping one vector in
8922 Vwith_echo_area_save_vector. */
8923 vector = Vmode_line_unwind_vector;
8924 Vmode_line_unwind_vector = Qnil;
8925
8926 if (NILP (vector))
8927 vector = Fmake_vector (make_number (7), Qnil);
8928
8929 AREF (vector, 0) = make_number (mode_line_target);
8930 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8931 AREF (vector, 2) = mode_line_string_list;
8932 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8933 AREF (vector, 4) = mode_line_string_face;
8934 AREF (vector, 5) = mode_line_string_face_prop;
8935
8936 if (obuf)
8937 XSETBUFFER (AREF (vector, 6), obuf);
8938 else
8939 AREF (vector, 6) = Qnil;
8940
8941 return vector;
8942 }
8943
8944 static Lisp_Object
8945 unwind_format_mode_line (vector)
8946 Lisp_Object vector;
8947 {
8948 mode_line_target = XINT (AREF (vector, 0));
8949 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8950 mode_line_string_list = AREF (vector, 2);
8951 if (! EQ (AREF (vector, 3), Qt))
8952 mode_line_proptrans_alist = AREF (vector, 3);
8953 mode_line_string_face = AREF (vector, 4);
8954 mode_line_string_face_prop = AREF (vector, 5);
8955
8956 if (!NILP (AREF (vector, 6)))
8957 {
8958 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8959 AREF (vector, 6) = Qnil;
8960 }
8961
8962 Vmode_line_unwind_vector = vector;
8963 return Qnil;
8964 }
8965
8966
8967 /* Store a single character C for the frame title in mode_line_noprop_buf.
8968 Re-allocate mode_line_noprop_buf if necessary. */
8969
8970 static void
8971 #ifdef PROTOTYPES
8972 store_mode_line_noprop_char (char c)
8973 #else
8974 store_mode_line_noprop_char (c)
8975 char c;
8976 #endif
8977 {
8978 /* If output position has reached the end of the allocated buffer,
8979 double the buffer's size. */
8980 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8981 {
8982 int len = MODE_LINE_NOPROP_LEN (0);
8983 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8984 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8985 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8986 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8987 }
8988
8989 *mode_line_noprop_ptr++ = c;
8990 }
8991
8992
8993 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8994 mode_line_noprop_ptr. STR is the string to store. Do not copy
8995 characters that yield more columns than PRECISION; PRECISION <= 0
8996 means copy the whole string. Pad with spaces until FIELD_WIDTH
8997 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8998 pad. Called from display_mode_element when it is used to build a
8999 frame title. */
9000
9001 static int
9002 store_mode_line_noprop (str, field_width, precision)
9003 const unsigned char *str;
9004 int field_width, precision;
9005 {
9006 int n = 0;
9007 int dummy, nbytes;
9008
9009 /* Copy at most PRECISION chars from STR. */
9010 nbytes = strlen (str);
9011 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9012 while (nbytes--)
9013 store_mode_line_noprop_char (*str++);
9014
9015 /* Fill up with spaces until FIELD_WIDTH reached. */
9016 while (field_width > 0
9017 && n < field_width)
9018 {
9019 store_mode_line_noprop_char (' ');
9020 ++n;
9021 }
9022
9023 return n;
9024 }
9025
9026 /***********************************************************************
9027 Frame Titles
9028 ***********************************************************************/
9029
9030 #ifdef HAVE_WINDOW_SYSTEM
9031
9032 /* Set the title of FRAME, if it has changed. The title format is
9033 Vicon_title_format if FRAME is iconified, otherwise it is
9034 frame_title_format. */
9035
9036 static void
9037 x_consider_frame_title (frame)
9038 Lisp_Object frame;
9039 {
9040 struct frame *f = XFRAME (frame);
9041
9042 if (FRAME_WINDOW_P (f)
9043 || FRAME_MINIBUF_ONLY_P (f)
9044 || f->explicit_name)
9045 {
9046 /* Do we have more than one visible frame on this X display? */
9047 Lisp_Object tail;
9048 Lisp_Object fmt;
9049 int title_start;
9050 char *title;
9051 int len;
9052 struct it it;
9053 int count = SPECPDL_INDEX ();
9054
9055 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9056 {
9057 Lisp_Object other_frame = XCAR (tail);
9058 struct frame *tf = XFRAME (other_frame);
9059
9060 if (tf != f
9061 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9062 && !FRAME_MINIBUF_ONLY_P (tf)
9063 && !EQ (other_frame, tip_frame)
9064 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9065 break;
9066 }
9067
9068 /* Set global variable indicating that multiple frames exist. */
9069 multiple_frames = CONSP (tail);
9070
9071 /* Switch to the buffer of selected window of the frame. Set up
9072 mode_line_target so that display_mode_element will output into
9073 mode_line_noprop_buf; then display the title. */
9074 record_unwind_protect (unwind_format_mode_line,
9075 format_mode_line_unwind_data (current_buffer, 0));
9076
9077 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9078 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9079
9080 mode_line_target = MODE_LINE_TITLE;
9081 title_start = MODE_LINE_NOPROP_LEN (0);
9082 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9083 NULL, DEFAULT_FACE_ID);
9084 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9085 len = MODE_LINE_NOPROP_LEN (title_start);
9086 title = mode_line_noprop_buf + title_start;
9087 unbind_to (count, Qnil);
9088
9089 /* Set the title only if it's changed. This avoids consing in
9090 the common case where it hasn't. (If it turns out that we've
9091 already wasted too much time by walking through the list with
9092 display_mode_element, then we might need to optimize at a
9093 higher level than this.) */
9094 if (! STRINGP (f->name)
9095 || SBYTES (f->name) != len
9096 || bcmp (title, SDATA (f->name), len) != 0)
9097 x_implicitly_set_name (f, make_string (title, len), Qnil);
9098 }
9099 }
9100
9101 #endif /* not HAVE_WINDOW_SYSTEM */
9102
9103
9104
9105 \f
9106 /***********************************************************************
9107 Menu Bars
9108 ***********************************************************************/
9109
9110
9111 /* Prepare for redisplay by updating menu-bar item lists when
9112 appropriate. This can call eval. */
9113
9114 void
9115 prepare_menu_bars ()
9116 {
9117 int all_windows;
9118 struct gcpro gcpro1, gcpro2;
9119 struct frame *f;
9120 Lisp_Object tooltip_frame;
9121
9122 #ifdef HAVE_WINDOW_SYSTEM
9123 tooltip_frame = tip_frame;
9124 #else
9125 tooltip_frame = Qnil;
9126 #endif
9127
9128 /* Update all frame titles based on their buffer names, etc. We do
9129 this before the menu bars so that the buffer-menu will show the
9130 up-to-date frame titles. */
9131 #ifdef HAVE_WINDOW_SYSTEM
9132 if (windows_or_buffers_changed || update_mode_lines)
9133 {
9134 Lisp_Object tail, frame;
9135
9136 FOR_EACH_FRAME (tail, frame)
9137 {
9138 f = XFRAME (frame);
9139 if (!EQ (frame, tooltip_frame)
9140 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9141 x_consider_frame_title (frame);
9142 }
9143 }
9144 #endif /* HAVE_WINDOW_SYSTEM */
9145
9146 /* Update the menu bar item lists, if appropriate. This has to be
9147 done before any actual redisplay or generation of display lines. */
9148 all_windows = (update_mode_lines
9149 || buffer_shared > 1
9150 || windows_or_buffers_changed);
9151 if (all_windows)
9152 {
9153 Lisp_Object tail, frame;
9154 int count = SPECPDL_INDEX ();
9155 /* 1 means that update_menu_bar has run its hooks
9156 so any further calls to update_menu_bar shouldn't do so again. */
9157 int menu_bar_hooks_run = 0;
9158
9159 record_unwind_save_match_data ();
9160
9161 FOR_EACH_FRAME (tail, frame)
9162 {
9163 f = XFRAME (frame);
9164
9165 /* Ignore tooltip frame. */
9166 if (EQ (frame, tooltip_frame))
9167 continue;
9168
9169 /* If a window on this frame changed size, report that to
9170 the user and clear the size-change flag. */
9171 if (FRAME_WINDOW_SIZES_CHANGED (f))
9172 {
9173 Lisp_Object functions;
9174
9175 /* Clear flag first in case we get an error below. */
9176 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9177 functions = Vwindow_size_change_functions;
9178 GCPRO2 (tail, functions);
9179
9180 while (CONSP (functions))
9181 {
9182 call1 (XCAR (functions), frame);
9183 functions = XCDR (functions);
9184 }
9185 UNGCPRO;
9186 }
9187
9188 GCPRO1 (tail);
9189 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9190 #ifdef HAVE_WINDOW_SYSTEM
9191 update_tool_bar (f, 0);
9192 #ifdef MAC_OS
9193 mac_update_title_bar (f, 0);
9194 #endif
9195 #endif
9196 UNGCPRO;
9197 }
9198
9199 unbind_to (count, Qnil);
9200 }
9201 else
9202 {
9203 struct frame *sf = SELECTED_FRAME ();
9204 update_menu_bar (sf, 1, 0);
9205 #ifdef HAVE_WINDOW_SYSTEM
9206 update_tool_bar (sf, 1);
9207 #ifdef MAC_OS
9208 mac_update_title_bar (sf, 1);
9209 #endif
9210 #endif
9211 }
9212
9213 /* Motif needs this. See comment in xmenu.c. Turn it off when
9214 pending_menu_activation is not defined. */
9215 #ifdef USE_X_TOOLKIT
9216 pending_menu_activation = 0;
9217 #endif
9218 }
9219
9220
9221 /* Update the menu bar item list for frame F. This has to be done
9222 before we start to fill in any display lines, because it can call
9223 eval.
9224
9225 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9226
9227 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9228 already ran the menu bar hooks for this redisplay, so there
9229 is no need to run them again. The return value is the
9230 updated value of this flag, to pass to the next call. */
9231
9232 static int
9233 update_menu_bar (f, save_match_data, hooks_run)
9234 struct frame *f;
9235 int save_match_data;
9236 int hooks_run;
9237 {
9238 Lisp_Object window;
9239 register struct window *w;
9240
9241 /* If called recursively during a menu update, do nothing. This can
9242 happen when, for instance, an activate-menubar-hook causes a
9243 redisplay. */
9244 if (inhibit_menubar_update)
9245 return hooks_run;
9246
9247 window = FRAME_SELECTED_WINDOW (f);
9248 w = XWINDOW (window);
9249
9250 #if 0 /* The if statement below this if statement used to include the
9251 condition !NILP (w->update_mode_line), rather than using
9252 update_mode_lines directly, and this if statement may have
9253 been added to make that condition work. Now the if
9254 statement below matches its comment, this isn't needed. */
9255 if (update_mode_lines)
9256 w->update_mode_line = Qt;
9257 #endif
9258
9259 if (FRAME_WINDOW_P (f)
9260 ?
9261 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9262 || defined (USE_GTK)
9263 FRAME_EXTERNAL_MENU_BAR (f)
9264 #else
9265 FRAME_MENU_BAR_LINES (f) > 0
9266 #endif
9267 : FRAME_MENU_BAR_LINES (f) > 0)
9268 {
9269 /* If the user has switched buffers or windows, we need to
9270 recompute to reflect the new bindings. But we'll
9271 recompute when update_mode_lines is set too; that means
9272 that people can use force-mode-line-update to request
9273 that the menu bar be recomputed. The adverse effect on
9274 the rest of the redisplay algorithm is about the same as
9275 windows_or_buffers_changed anyway. */
9276 if (windows_or_buffers_changed
9277 /* This used to test w->update_mode_line, but we believe
9278 there is no need to recompute the menu in that case. */
9279 || update_mode_lines
9280 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9281 < BUF_MODIFF (XBUFFER (w->buffer)))
9282 != !NILP (w->last_had_star))
9283 || ((!NILP (Vtransient_mark_mode)
9284 && !NILP (XBUFFER (w->buffer)->mark_active))
9285 != !NILP (w->region_showing)))
9286 {
9287 struct buffer *prev = current_buffer;
9288 int count = SPECPDL_INDEX ();
9289
9290 specbind (Qinhibit_menubar_update, Qt);
9291
9292 set_buffer_internal_1 (XBUFFER (w->buffer));
9293 if (save_match_data)
9294 record_unwind_save_match_data ();
9295 if (NILP (Voverriding_local_map_menu_flag))
9296 {
9297 specbind (Qoverriding_terminal_local_map, Qnil);
9298 specbind (Qoverriding_local_map, Qnil);
9299 }
9300
9301 if (!hooks_run)
9302 {
9303 /* Run the Lucid hook. */
9304 safe_run_hooks (Qactivate_menubar_hook);
9305
9306 /* If it has changed current-menubar from previous value,
9307 really recompute the menu-bar from the value. */
9308 if (! NILP (Vlucid_menu_bar_dirty_flag))
9309 call0 (Qrecompute_lucid_menubar);
9310
9311 safe_run_hooks (Qmenu_bar_update_hook);
9312
9313 hooks_run = 1;
9314 }
9315
9316 XSETFRAME (Vmenu_updating_frame, f);
9317 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9318
9319 /* Redisplay the menu bar in case we changed it. */
9320 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9321 || defined (USE_GTK)
9322 if (FRAME_WINDOW_P (f))
9323 {
9324 #ifdef MAC_OS
9325 /* All frames on Mac OS share the same menubar. So only
9326 the selected frame should be allowed to set it. */
9327 if (f == SELECTED_FRAME ())
9328 #endif
9329 set_frame_menubar (f, 0, 0);
9330 }
9331 else
9332 /* On a terminal screen, the menu bar is an ordinary screen
9333 line, and this makes it get updated. */
9334 w->update_mode_line = Qt;
9335 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9336 /* In the non-toolkit version, the menu bar is an ordinary screen
9337 line, and this makes it get updated. */
9338 w->update_mode_line = Qt;
9339 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9340
9341 unbind_to (count, Qnil);
9342 set_buffer_internal_1 (prev);
9343 }
9344 }
9345
9346 return hooks_run;
9347 }
9348
9349
9350 \f
9351 /***********************************************************************
9352 Output Cursor
9353 ***********************************************************************/
9354
9355 #ifdef HAVE_WINDOW_SYSTEM
9356
9357 /* EXPORT:
9358 Nominal cursor position -- where to draw output.
9359 HPOS and VPOS are window relative glyph matrix coordinates.
9360 X and Y are window relative pixel coordinates. */
9361
9362 struct cursor_pos output_cursor;
9363
9364
9365 /* EXPORT:
9366 Set the global variable output_cursor to CURSOR. All cursor
9367 positions are relative to updated_window. */
9368
9369 void
9370 set_output_cursor (cursor)
9371 struct cursor_pos *cursor;
9372 {
9373 output_cursor.hpos = cursor->hpos;
9374 output_cursor.vpos = cursor->vpos;
9375 output_cursor.x = cursor->x;
9376 output_cursor.y = cursor->y;
9377 }
9378
9379
9380 /* EXPORT for RIF:
9381 Set a nominal cursor position.
9382
9383 HPOS and VPOS are column/row positions in a window glyph matrix. X
9384 and Y are window text area relative pixel positions.
9385
9386 If this is done during an update, updated_window will contain the
9387 window that is being updated and the position is the future output
9388 cursor position for that window. If updated_window is null, use
9389 selected_window and display the cursor at the given position. */
9390
9391 void
9392 x_cursor_to (vpos, hpos, y, x)
9393 int vpos, hpos, y, x;
9394 {
9395 struct window *w;
9396
9397 /* If updated_window is not set, work on selected_window. */
9398 if (updated_window)
9399 w = updated_window;
9400 else
9401 w = XWINDOW (selected_window);
9402
9403 /* Set the output cursor. */
9404 output_cursor.hpos = hpos;
9405 output_cursor.vpos = vpos;
9406 output_cursor.x = x;
9407 output_cursor.y = y;
9408
9409 /* If not called as part of an update, really display the cursor.
9410 This will also set the cursor position of W. */
9411 if (updated_window == NULL)
9412 {
9413 BLOCK_INPUT;
9414 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9415 if (rif->flush_display_optional)
9416 rif->flush_display_optional (SELECTED_FRAME ());
9417 UNBLOCK_INPUT;
9418 }
9419 }
9420
9421 #endif /* HAVE_WINDOW_SYSTEM */
9422
9423 \f
9424 /***********************************************************************
9425 Tool-bars
9426 ***********************************************************************/
9427
9428 #ifdef HAVE_WINDOW_SYSTEM
9429
9430 /* Where the mouse was last time we reported a mouse event. */
9431
9432 FRAME_PTR last_mouse_frame;
9433
9434 /* Tool-bar item index of the item on which a mouse button was pressed
9435 or -1. */
9436
9437 int last_tool_bar_item;
9438
9439
9440 /* Update the tool-bar item list for frame F. This has to be done
9441 before we start to fill in any display lines. Called from
9442 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9443 and restore it here. */
9444
9445 static void
9446 update_tool_bar (f, save_match_data)
9447 struct frame *f;
9448 int save_match_data;
9449 {
9450 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9451 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9452 #else
9453 int do_update = WINDOWP (f->tool_bar_window)
9454 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9455 #endif
9456
9457 if (do_update)
9458 {
9459 Lisp_Object window;
9460 struct window *w;
9461
9462 window = FRAME_SELECTED_WINDOW (f);
9463 w = XWINDOW (window);
9464
9465 /* If the user has switched buffers or windows, we need to
9466 recompute to reflect the new bindings. But we'll
9467 recompute when update_mode_lines is set too; that means
9468 that people can use force-mode-line-update to request
9469 that the menu bar be recomputed. The adverse effect on
9470 the rest of the redisplay algorithm is about the same as
9471 windows_or_buffers_changed anyway. */
9472 if (windows_or_buffers_changed
9473 || !NILP (w->update_mode_line)
9474 || update_mode_lines
9475 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9476 < BUF_MODIFF (XBUFFER (w->buffer)))
9477 != !NILP (w->last_had_star))
9478 || ((!NILP (Vtransient_mark_mode)
9479 && !NILP (XBUFFER (w->buffer)->mark_active))
9480 != !NILP (w->region_showing)))
9481 {
9482 struct buffer *prev = current_buffer;
9483 int count = SPECPDL_INDEX ();
9484 Lisp_Object new_tool_bar;
9485 int new_n_tool_bar;
9486 struct gcpro gcpro1;
9487
9488 /* Set current_buffer to the buffer of the selected
9489 window of the frame, so that we get the right local
9490 keymaps. */
9491 set_buffer_internal_1 (XBUFFER (w->buffer));
9492
9493 /* Save match data, if we must. */
9494 if (save_match_data)
9495 record_unwind_save_match_data ();
9496
9497 /* Make sure that we don't accidentally use bogus keymaps. */
9498 if (NILP (Voverriding_local_map_menu_flag))
9499 {
9500 specbind (Qoverriding_terminal_local_map, Qnil);
9501 specbind (Qoverriding_local_map, Qnil);
9502 }
9503
9504 GCPRO1 (new_tool_bar);
9505
9506 /* Build desired tool-bar items from keymaps. */
9507 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9508 &new_n_tool_bar);
9509
9510 /* Redisplay the tool-bar if we changed it. */
9511 if (new_n_tool_bar != f->n_tool_bar_items
9512 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9513 {
9514 /* Redisplay that happens asynchronously due to an expose event
9515 may access f->tool_bar_items. Make sure we update both
9516 variables within BLOCK_INPUT so no such event interrupts. */
9517 BLOCK_INPUT;
9518 f->tool_bar_items = new_tool_bar;
9519 f->n_tool_bar_items = new_n_tool_bar;
9520 w->update_mode_line = Qt;
9521 UNBLOCK_INPUT;
9522 }
9523
9524 UNGCPRO;
9525
9526 unbind_to (count, Qnil);
9527 set_buffer_internal_1 (prev);
9528 }
9529 }
9530 }
9531
9532
9533 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9534 F's desired tool-bar contents. F->tool_bar_items must have
9535 been set up previously by calling prepare_menu_bars. */
9536
9537 static void
9538 build_desired_tool_bar_string (f)
9539 struct frame *f;
9540 {
9541 int i, size, size_needed;
9542 struct gcpro gcpro1, gcpro2, gcpro3;
9543 Lisp_Object image, plist, props;
9544
9545 image = plist = props = Qnil;
9546 GCPRO3 (image, plist, props);
9547
9548 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9549 Otherwise, make a new string. */
9550
9551 /* The size of the string we might be able to reuse. */
9552 size = (STRINGP (f->desired_tool_bar_string)
9553 ? SCHARS (f->desired_tool_bar_string)
9554 : 0);
9555
9556 /* We need one space in the string for each image. */
9557 size_needed = f->n_tool_bar_items;
9558
9559 /* Reuse f->desired_tool_bar_string, if possible. */
9560 if (size < size_needed || NILP (f->desired_tool_bar_string))
9561 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9562 make_number (' '));
9563 else
9564 {
9565 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9566 Fremove_text_properties (make_number (0), make_number (size),
9567 props, f->desired_tool_bar_string);
9568 }
9569
9570 /* Put a `display' property on the string for the images to display,
9571 put a `menu_item' property on tool-bar items with a value that
9572 is the index of the item in F's tool-bar item vector. */
9573 for (i = 0; i < f->n_tool_bar_items; ++i)
9574 {
9575 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9576
9577 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9578 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9579 int hmargin, vmargin, relief, idx, end;
9580 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9581
9582 /* If image is a vector, choose the image according to the
9583 button state. */
9584 image = PROP (TOOL_BAR_ITEM_IMAGES);
9585 if (VECTORP (image))
9586 {
9587 if (enabled_p)
9588 idx = (selected_p
9589 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9590 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9591 else
9592 idx = (selected_p
9593 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9594 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9595
9596 xassert (ASIZE (image) >= idx);
9597 image = AREF (image, idx);
9598 }
9599 else
9600 idx = -1;
9601
9602 /* Ignore invalid image specifications. */
9603 if (!valid_image_p (image))
9604 continue;
9605
9606 /* Display the tool-bar button pressed, or depressed. */
9607 plist = Fcopy_sequence (XCDR (image));
9608
9609 /* Compute margin and relief to draw. */
9610 relief = (tool_bar_button_relief >= 0
9611 ? tool_bar_button_relief
9612 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9613 hmargin = vmargin = relief;
9614
9615 if (INTEGERP (Vtool_bar_button_margin)
9616 && XINT (Vtool_bar_button_margin) > 0)
9617 {
9618 hmargin += XFASTINT (Vtool_bar_button_margin);
9619 vmargin += XFASTINT (Vtool_bar_button_margin);
9620 }
9621 else if (CONSP (Vtool_bar_button_margin))
9622 {
9623 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9624 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9625 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9626
9627 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9628 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9629 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9630 }
9631
9632 if (auto_raise_tool_bar_buttons_p)
9633 {
9634 /* Add a `:relief' property to the image spec if the item is
9635 selected. */
9636 if (selected_p)
9637 {
9638 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9639 hmargin -= relief;
9640 vmargin -= relief;
9641 }
9642 }
9643 else
9644 {
9645 /* If image is selected, display it pressed, i.e. with a
9646 negative relief. If it's not selected, display it with a
9647 raised relief. */
9648 plist = Fplist_put (plist, QCrelief,
9649 (selected_p
9650 ? make_number (-relief)
9651 : make_number (relief)));
9652 hmargin -= relief;
9653 vmargin -= relief;
9654 }
9655
9656 /* Put a margin around the image. */
9657 if (hmargin || vmargin)
9658 {
9659 if (hmargin == vmargin)
9660 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9661 else
9662 plist = Fplist_put (plist, QCmargin,
9663 Fcons (make_number (hmargin),
9664 make_number (vmargin)));
9665 }
9666
9667 /* If button is not enabled, and we don't have special images
9668 for the disabled state, make the image appear disabled by
9669 applying an appropriate algorithm to it. */
9670 if (!enabled_p && idx < 0)
9671 plist = Fplist_put (plist, QCconversion, Qdisabled);
9672
9673 /* Put a `display' text property on the string for the image to
9674 display. Put a `menu-item' property on the string that gives
9675 the start of this item's properties in the tool-bar items
9676 vector. */
9677 image = Fcons (Qimage, plist);
9678 props = list4 (Qdisplay, image,
9679 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9680
9681 /* Let the last image hide all remaining spaces in the tool bar
9682 string. The string can be longer than needed when we reuse a
9683 previous string. */
9684 if (i + 1 == f->n_tool_bar_items)
9685 end = SCHARS (f->desired_tool_bar_string);
9686 else
9687 end = i + 1;
9688 Fadd_text_properties (make_number (i), make_number (end),
9689 props, f->desired_tool_bar_string);
9690 #undef PROP
9691 }
9692
9693 UNGCPRO;
9694 }
9695
9696
9697 /* Display one line of the tool-bar of frame IT->f.
9698
9699 HEIGHT specifies the desired height of the tool-bar line.
9700 If the actual height of the glyph row is less than HEIGHT, the
9701 row's height is increased to HEIGHT, and the icons are centered
9702 vertically in the new height.
9703
9704 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9705 count a final empty row in case the tool-bar width exactly matches
9706 the window width.
9707 */
9708
9709 static void
9710 display_tool_bar_line (it, height)
9711 struct it *it;
9712 int height;
9713 {
9714 struct glyph_row *row = it->glyph_row;
9715 int max_x = it->last_visible_x;
9716 struct glyph *last;
9717
9718 prepare_desired_row (row);
9719 row->y = it->current_y;
9720
9721 /* Note that this isn't made use of if the face hasn't a box,
9722 so there's no need to check the face here. */
9723 it->start_of_box_run_p = 1;
9724
9725 while (it->current_x < max_x)
9726 {
9727 int x, n_glyphs_before, i, nglyphs;
9728 struct it it_before;
9729
9730 /* Get the next display element. */
9731 if (!get_next_display_element (it))
9732 {
9733 /* Don't count empty row if we are counting needed tool-bar lines. */
9734 if (height < 0 && !it->hpos)
9735 return;
9736 break;
9737 }
9738
9739 /* Produce glyphs. */
9740 n_glyphs_before = row->used[TEXT_AREA];
9741 it_before = *it;
9742
9743 PRODUCE_GLYPHS (it);
9744
9745 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9746 i = 0;
9747 x = it_before.current_x;
9748 while (i < nglyphs)
9749 {
9750 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9751
9752 if (x + glyph->pixel_width > max_x)
9753 {
9754 /* Glyph doesn't fit on line. Backtrack. */
9755 row->used[TEXT_AREA] = n_glyphs_before;
9756 *it = it_before;
9757 /* If this is the only glyph on this line, it will never fit on the
9758 toolbar, so skip it. But ensure there is at least one glyph,
9759 so we don't accidentally disable the tool-bar. */
9760 if (n_glyphs_before == 0
9761 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9762 break;
9763 goto out;
9764 }
9765
9766 ++it->hpos;
9767 x += glyph->pixel_width;
9768 ++i;
9769 }
9770
9771 /* Stop at line ends. */
9772 if (ITERATOR_AT_END_OF_LINE_P (it))
9773 break;
9774
9775 set_iterator_to_next (it, 1);
9776 }
9777
9778 out:;
9779
9780 row->displays_text_p = row->used[TEXT_AREA] != 0;
9781
9782 /* Use default face for the border below the tool bar.
9783
9784 FIXME: When auto-resize-tool-bars is grow-only, there is
9785 no additional border below the possibly empty tool-bar lines.
9786 So to make the extra empty lines look "normal", we have to
9787 use the tool-bar face for the border too. */
9788 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9789 it->face_id = DEFAULT_FACE_ID;
9790
9791 extend_face_to_end_of_line (it);
9792 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9793 last->right_box_line_p = 1;
9794 if (last == row->glyphs[TEXT_AREA])
9795 last->left_box_line_p = 1;
9796
9797 /* Make line the desired height and center it vertically. */
9798 if ((height -= it->max_ascent + it->max_descent) > 0)
9799 {
9800 /* Don't add more than one line height. */
9801 height %= FRAME_LINE_HEIGHT (it->f);
9802 it->max_ascent += height / 2;
9803 it->max_descent += (height + 1) / 2;
9804 }
9805
9806 compute_line_metrics (it);
9807
9808 /* If line is empty, make it occupy the rest of the tool-bar. */
9809 if (!row->displays_text_p)
9810 {
9811 row->height = row->phys_height = it->last_visible_y - row->y;
9812 row->visible_height = row->height;
9813 row->ascent = row->phys_ascent = 0;
9814 row->extra_line_spacing = 0;
9815 }
9816
9817 row->full_width_p = 1;
9818 row->continued_p = 0;
9819 row->truncated_on_left_p = 0;
9820 row->truncated_on_right_p = 0;
9821
9822 it->current_x = it->hpos = 0;
9823 it->current_y += row->height;
9824 ++it->vpos;
9825 ++it->glyph_row;
9826 }
9827
9828
9829 /* Max tool-bar height. */
9830
9831 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9832 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9833
9834 /* Value is the number of screen lines needed to make all tool-bar
9835 items of frame F visible. The number of actual rows needed is
9836 returned in *N_ROWS if non-NULL. */
9837
9838 static int
9839 tool_bar_lines_needed (f, n_rows)
9840 struct frame *f;
9841 int *n_rows;
9842 {
9843 struct window *w = XWINDOW (f->tool_bar_window);
9844 struct it it;
9845 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9846 the desired matrix, so use (unused) mode-line row as temporary row to
9847 avoid destroying the first tool-bar row. */
9848 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9849
9850 /* Initialize an iterator for iteration over
9851 F->desired_tool_bar_string in the tool-bar window of frame F. */
9852 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9853 it.first_visible_x = 0;
9854 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9855 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9856
9857 while (!ITERATOR_AT_END_P (&it))
9858 {
9859 clear_glyph_row (temp_row);
9860 it.glyph_row = temp_row;
9861 display_tool_bar_line (&it, -1);
9862 }
9863 clear_glyph_row (temp_row);
9864
9865 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9866 if (n_rows)
9867 *n_rows = it.vpos > 0 ? it.vpos : -1;
9868
9869 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9870 }
9871
9872
9873 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9874 0, 1, 0,
9875 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9876 (frame)
9877 Lisp_Object frame;
9878 {
9879 struct frame *f;
9880 struct window *w;
9881 int nlines = 0;
9882
9883 if (NILP (frame))
9884 frame = selected_frame;
9885 else
9886 CHECK_FRAME (frame);
9887 f = XFRAME (frame);
9888
9889 if (WINDOWP (f->tool_bar_window)
9890 || (w = XWINDOW (f->tool_bar_window),
9891 WINDOW_TOTAL_LINES (w) > 0))
9892 {
9893 update_tool_bar (f, 1);
9894 if (f->n_tool_bar_items)
9895 {
9896 build_desired_tool_bar_string (f);
9897 nlines = tool_bar_lines_needed (f, NULL);
9898 }
9899 }
9900
9901 return make_number (nlines);
9902 }
9903
9904
9905 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9906 height should be changed. */
9907
9908 static int
9909 redisplay_tool_bar (f)
9910 struct frame *f;
9911 {
9912 struct window *w;
9913 struct it it;
9914 struct glyph_row *row;
9915
9916 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9917 if (FRAME_EXTERNAL_TOOL_BAR (f))
9918 update_frame_tool_bar (f);
9919 return 0;
9920 #endif
9921
9922 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9923 do anything. This means you must start with tool-bar-lines
9924 non-zero to get the auto-sizing effect. Or in other words, you
9925 can turn off tool-bars by specifying tool-bar-lines zero. */
9926 if (!WINDOWP (f->tool_bar_window)
9927 || (w = XWINDOW (f->tool_bar_window),
9928 WINDOW_TOTAL_LINES (w) == 0))
9929 return 0;
9930
9931 /* Set up an iterator for the tool-bar window. */
9932 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9933 it.first_visible_x = 0;
9934 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9935 row = it.glyph_row;
9936
9937 /* Build a string that represents the contents of the tool-bar. */
9938 build_desired_tool_bar_string (f);
9939 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9940
9941 if (f->n_tool_bar_rows == 0)
9942 {
9943 int nlines;
9944
9945 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9946 nlines != WINDOW_TOTAL_LINES (w)))
9947 {
9948 extern Lisp_Object Qtool_bar_lines;
9949 Lisp_Object frame;
9950 int old_height = WINDOW_TOTAL_LINES (w);
9951
9952 XSETFRAME (frame, f);
9953 Fmodify_frame_parameters (frame,
9954 Fcons (Fcons (Qtool_bar_lines,
9955 make_number (nlines)),
9956 Qnil));
9957 if (WINDOW_TOTAL_LINES (w) != old_height)
9958 {
9959 clear_glyph_matrix (w->desired_matrix);
9960 fonts_changed_p = 1;
9961 return 1;
9962 }
9963 }
9964 }
9965
9966 /* Display as many lines as needed to display all tool-bar items. */
9967
9968 if (f->n_tool_bar_rows > 0)
9969 {
9970 int border, rows, height, extra;
9971
9972 if (INTEGERP (Vtool_bar_border))
9973 border = XINT (Vtool_bar_border);
9974 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9975 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9976 else if (EQ (Vtool_bar_border, Qborder_width))
9977 border = f->border_width;
9978 else
9979 border = 0;
9980 if (border < 0)
9981 border = 0;
9982
9983 rows = f->n_tool_bar_rows;
9984 height = max (1, (it.last_visible_y - border) / rows);
9985 extra = it.last_visible_y - border - height * rows;
9986
9987 while (it.current_y < it.last_visible_y)
9988 {
9989 int h = 0;
9990 if (extra > 0 && rows-- > 0)
9991 {
9992 h = (extra + rows - 1) / rows;
9993 extra -= h;
9994 }
9995 display_tool_bar_line (&it, height + h);
9996 }
9997 }
9998 else
9999 {
10000 while (it.current_y < it.last_visible_y)
10001 display_tool_bar_line (&it, 0);
10002 }
10003
10004 /* It doesn't make much sense to try scrolling in the tool-bar
10005 window, so don't do it. */
10006 w->desired_matrix->no_scrolling_p = 1;
10007 w->must_be_updated_p = 1;
10008
10009 if (!NILP (Vauto_resize_tool_bars))
10010 {
10011 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10012 int change_height_p = 0;
10013
10014 /* If we couldn't display everything, change the tool-bar's
10015 height if there is room for more. */
10016 if (IT_STRING_CHARPOS (it) < it.end_charpos
10017 && it.current_y < max_tool_bar_height)
10018 change_height_p = 1;
10019
10020 row = it.glyph_row - 1;
10021
10022 /* If there are blank lines at the end, except for a partially
10023 visible blank line at the end that is smaller than
10024 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10025 if (!row->displays_text_p
10026 && row->height >= FRAME_LINE_HEIGHT (f))
10027 change_height_p = 1;
10028
10029 /* If row displays tool-bar items, but is partially visible,
10030 change the tool-bar's height. */
10031 if (row->displays_text_p
10032 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10033 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10034 change_height_p = 1;
10035
10036 /* Resize windows as needed by changing the `tool-bar-lines'
10037 frame parameter. */
10038 if (change_height_p)
10039 {
10040 extern Lisp_Object Qtool_bar_lines;
10041 Lisp_Object frame;
10042 int old_height = WINDOW_TOTAL_LINES (w);
10043 int nrows;
10044 int nlines = tool_bar_lines_needed (f, &nrows);
10045
10046 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10047 && !f->minimize_tool_bar_window_p)
10048 ? (nlines > old_height)
10049 : (nlines != old_height));
10050 f->minimize_tool_bar_window_p = 0;
10051
10052 if (change_height_p)
10053 {
10054 XSETFRAME (frame, f);
10055 Fmodify_frame_parameters (frame,
10056 Fcons (Fcons (Qtool_bar_lines,
10057 make_number (nlines)),
10058 Qnil));
10059 if (WINDOW_TOTAL_LINES (w) != old_height)
10060 {
10061 clear_glyph_matrix (w->desired_matrix);
10062 f->n_tool_bar_rows = nrows;
10063 fonts_changed_p = 1;
10064 return 1;
10065 }
10066 }
10067 }
10068 }
10069
10070 f->minimize_tool_bar_window_p = 0;
10071 return 0;
10072 }
10073
10074
10075 /* Get information about the tool-bar item which is displayed in GLYPH
10076 on frame F. Return in *PROP_IDX the index where tool-bar item
10077 properties start in F->tool_bar_items. Value is zero if
10078 GLYPH doesn't display a tool-bar item. */
10079
10080 static int
10081 tool_bar_item_info (f, glyph, prop_idx)
10082 struct frame *f;
10083 struct glyph *glyph;
10084 int *prop_idx;
10085 {
10086 Lisp_Object prop;
10087 int success_p;
10088 int charpos;
10089
10090 /* This function can be called asynchronously, which means we must
10091 exclude any possibility that Fget_text_property signals an
10092 error. */
10093 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10094 charpos = max (0, charpos);
10095
10096 /* Get the text property `menu-item' at pos. The value of that
10097 property is the start index of this item's properties in
10098 F->tool_bar_items. */
10099 prop = Fget_text_property (make_number (charpos),
10100 Qmenu_item, f->current_tool_bar_string);
10101 if (INTEGERP (prop))
10102 {
10103 *prop_idx = XINT (prop);
10104 success_p = 1;
10105 }
10106 else
10107 success_p = 0;
10108
10109 return success_p;
10110 }
10111
10112 \f
10113 /* Get information about the tool-bar item at position X/Y on frame F.
10114 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10115 the current matrix of the tool-bar window of F, or NULL if not
10116 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10117 item in F->tool_bar_items. Value is
10118
10119 -1 if X/Y is not on a tool-bar item
10120 0 if X/Y is on the same item that was highlighted before.
10121 1 otherwise. */
10122
10123 static int
10124 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10125 struct frame *f;
10126 int x, y;
10127 struct glyph **glyph;
10128 int *hpos, *vpos, *prop_idx;
10129 {
10130 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10131 struct window *w = XWINDOW (f->tool_bar_window);
10132 int area;
10133
10134 /* Find the glyph under X/Y. */
10135 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10136 if (*glyph == NULL)
10137 return -1;
10138
10139 /* Get the start of this tool-bar item's properties in
10140 f->tool_bar_items. */
10141 if (!tool_bar_item_info (f, *glyph, prop_idx))
10142 return -1;
10143
10144 /* Is mouse on the highlighted item? */
10145 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10146 && *vpos >= dpyinfo->mouse_face_beg_row
10147 && *vpos <= dpyinfo->mouse_face_end_row
10148 && (*vpos > dpyinfo->mouse_face_beg_row
10149 || *hpos >= dpyinfo->mouse_face_beg_col)
10150 && (*vpos < dpyinfo->mouse_face_end_row
10151 || *hpos < dpyinfo->mouse_face_end_col
10152 || dpyinfo->mouse_face_past_end))
10153 return 0;
10154
10155 return 1;
10156 }
10157
10158
10159 /* EXPORT:
10160 Handle mouse button event on the tool-bar of frame F, at
10161 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10162 0 for button release. MODIFIERS is event modifiers for button
10163 release. */
10164
10165 void
10166 handle_tool_bar_click (f, x, y, down_p, modifiers)
10167 struct frame *f;
10168 int x, y, down_p;
10169 unsigned int modifiers;
10170 {
10171 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10172 struct window *w = XWINDOW (f->tool_bar_window);
10173 int hpos, vpos, prop_idx;
10174 struct glyph *glyph;
10175 Lisp_Object enabled_p;
10176
10177 /* If not on the highlighted tool-bar item, return. */
10178 frame_to_window_pixel_xy (w, &x, &y);
10179 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10180 return;
10181
10182 /* If item is disabled, do nothing. */
10183 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10184 if (NILP (enabled_p))
10185 return;
10186
10187 if (down_p)
10188 {
10189 /* Show item in pressed state. */
10190 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10191 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10192 last_tool_bar_item = prop_idx;
10193 }
10194 else
10195 {
10196 Lisp_Object key, frame;
10197 struct input_event event;
10198 EVENT_INIT (event);
10199
10200 /* Show item in released state. */
10201 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10202 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10203
10204 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10205
10206 XSETFRAME (frame, f);
10207 event.kind = TOOL_BAR_EVENT;
10208 event.frame_or_window = frame;
10209 event.arg = frame;
10210 kbd_buffer_store_event (&event);
10211
10212 event.kind = TOOL_BAR_EVENT;
10213 event.frame_or_window = frame;
10214 event.arg = key;
10215 event.modifiers = modifiers;
10216 kbd_buffer_store_event (&event);
10217 last_tool_bar_item = -1;
10218 }
10219 }
10220
10221
10222 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10223 tool-bar window-relative coordinates X/Y. Called from
10224 note_mouse_highlight. */
10225
10226 static void
10227 note_tool_bar_highlight (f, x, y)
10228 struct frame *f;
10229 int x, y;
10230 {
10231 Lisp_Object window = f->tool_bar_window;
10232 struct window *w = XWINDOW (window);
10233 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10234 int hpos, vpos;
10235 struct glyph *glyph;
10236 struct glyph_row *row;
10237 int i;
10238 Lisp_Object enabled_p;
10239 int prop_idx;
10240 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10241 int mouse_down_p, rc;
10242
10243 /* Function note_mouse_highlight is called with negative x(y
10244 values when mouse moves outside of the frame. */
10245 if (x <= 0 || y <= 0)
10246 {
10247 clear_mouse_face (dpyinfo);
10248 return;
10249 }
10250
10251 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10252 if (rc < 0)
10253 {
10254 /* Not on tool-bar item. */
10255 clear_mouse_face (dpyinfo);
10256 return;
10257 }
10258 else if (rc == 0)
10259 /* On same tool-bar item as before. */
10260 goto set_help_echo;
10261
10262 clear_mouse_face (dpyinfo);
10263
10264 /* Mouse is down, but on different tool-bar item? */
10265 mouse_down_p = (dpyinfo->grabbed
10266 && f == last_mouse_frame
10267 && FRAME_LIVE_P (f));
10268 if (mouse_down_p
10269 && last_tool_bar_item != prop_idx)
10270 return;
10271
10272 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10273 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10274
10275 /* If tool-bar item is not enabled, don't highlight it. */
10276 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10277 if (!NILP (enabled_p))
10278 {
10279 /* Compute the x-position of the glyph. In front and past the
10280 image is a space. We include this in the highlighted area. */
10281 row = MATRIX_ROW (w->current_matrix, vpos);
10282 for (i = x = 0; i < hpos; ++i)
10283 x += row->glyphs[TEXT_AREA][i].pixel_width;
10284
10285 /* Record this as the current active region. */
10286 dpyinfo->mouse_face_beg_col = hpos;
10287 dpyinfo->mouse_face_beg_row = vpos;
10288 dpyinfo->mouse_face_beg_x = x;
10289 dpyinfo->mouse_face_beg_y = row->y;
10290 dpyinfo->mouse_face_past_end = 0;
10291
10292 dpyinfo->mouse_face_end_col = hpos + 1;
10293 dpyinfo->mouse_face_end_row = vpos;
10294 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10295 dpyinfo->mouse_face_end_y = row->y;
10296 dpyinfo->mouse_face_window = window;
10297 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10298
10299 /* Display it as active. */
10300 show_mouse_face (dpyinfo, draw);
10301 dpyinfo->mouse_face_image_state = draw;
10302 }
10303
10304 set_help_echo:
10305
10306 /* Set help_echo_string to a help string to display for this tool-bar item.
10307 XTread_socket does the rest. */
10308 help_echo_object = help_echo_window = Qnil;
10309 help_echo_pos = -1;
10310 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10311 if (NILP (help_echo_string))
10312 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10313 }
10314
10315 #endif /* HAVE_WINDOW_SYSTEM */
10316
10317
10318 \f
10319 /************************************************************************
10320 Horizontal scrolling
10321 ************************************************************************/
10322
10323 static int hscroll_window_tree P_ ((Lisp_Object));
10324 static int hscroll_windows P_ ((Lisp_Object));
10325
10326 /* For all leaf windows in the window tree rooted at WINDOW, set their
10327 hscroll value so that PT is (i) visible in the window, and (ii) so
10328 that it is not within a certain margin at the window's left and
10329 right border. Value is non-zero if any window's hscroll has been
10330 changed. */
10331
10332 static int
10333 hscroll_window_tree (window)
10334 Lisp_Object window;
10335 {
10336 int hscrolled_p = 0;
10337 int hscroll_relative_p = FLOATP (Vhscroll_step);
10338 int hscroll_step_abs = 0;
10339 double hscroll_step_rel = 0;
10340
10341 if (hscroll_relative_p)
10342 {
10343 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10344 if (hscroll_step_rel < 0)
10345 {
10346 hscroll_relative_p = 0;
10347 hscroll_step_abs = 0;
10348 }
10349 }
10350 else if (INTEGERP (Vhscroll_step))
10351 {
10352 hscroll_step_abs = XINT (Vhscroll_step);
10353 if (hscroll_step_abs < 0)
10354 hscroll_step_abs = 0;
10355 }
10356 else
10357 hscroll_step_abs = 0;
10358
10359 while (WINDOWP (window))
10360 {
10361 struct window *w = XWINDOW (window);
10362
10363 if (WINDOWP (w->hchild))
10364 hscrolled_p |= hscroll_window_tree (w->hchild);
10365 else if (WINDOWP (w->vchild))
10366 hscrolled_p |= hscroll_window_tree (w->vchild);
10367 else if (w->cursor.vpos >= 0)
10368 {
10369 int h_margin;
10370 int text_area_width;
10371 struct glyph_row *current_cursor_row
10372 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10373 struct glyph_row *desired_cursor_row
10374 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10375 struct glyph_row *cursor_row
10376 = (desired_cursor_row->enabled_p
10377 ? desired_cursor_row
10378 : current_cursor_row);
10379
10380 text_area_width = window_box_width (w, TEXT_AREA);
10381
10382 /* Scroll when cursor is inside this scroll margin. */
10383 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10384
10385 if ((XFASTINT (w->hscroll)
10386 && w->cursor.x <= h_margin)
10387 || (cursor_row->enabled_p
10388 && cursor_row->truncated_on_right_p
10389 && (w->cursor.x >= text_area_width - h_margin)))
10390 {
10391 struct it it;
10392 int hscroll;
10393 struct buffer *saved_current_buffer;
10394 int pt;
10395 int wanted_x;
10396
10397 /* Find point in a display of infinite width. */
10398 saved_current_buffer = current_buffer;
10399 current_buffer = XBUFFER (w->buffer);
10400
10401 if (w == XWINDOW (selected_window))
10402 pt = BUF_PT (current_buffer);
10403 else
10404 {
10405 pt = marker_position (w->pointm);
10406 pt = max (BEGV, pt);
10407 pt = min (ZV, pt);
10408 }
10409
10410 /* Move iterator to pt starting at cursor_row->start in
10411 a line with infinite width. */
10412 init_to_row_start (&it, w, cursor_row);
10413 it.last_visible_x = INFINITY;
10414 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10415 current_buffer = saved_current_buffer;
10416
10417 /* Position cursor in window. */
10418 if (!hscroll_relative_p && hscroll_step_abs == 0)
10419 hscroll = max (0, (it.current_x
10420 - (ITERATOR_AT_END_OF_LINE_P (&it)
10421 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10422 : (text_area_width / 2))))
10423 / FRAME_COLUMN_WIDTH (it.f);
10424 else if (w->cursor.x >= text_area_width - h_margin)
10425 {
10426 if (hscroll_relative_p)
10427 wanted_x = text_area_width * (1 - hscroll_step_rel)
10428 - h_margin;
10429 else
10430 wanted_x = text_area_width
10431 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10432 - h_margin;
10433 hscroll
10434 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10435 }
10436 else
10437 {
10438 if (hscroll_relative_p)
10439 wanted_x = text_area_width * hscroll_step_rel
10440 + h_margin;
10441 else
10442 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10443 + h_margin;
10444 hscroll
10445 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10446 }
10447 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10448
10449 /* Don't call Fset_window_hscroll if value hasn't
10450 changed because it will prevent redisplay
10451 optimizations. */
10452 if (XFASTINT (w->hscroll) != hscroll)
10453 {
10454 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10455 w->hscroll = make_number (hscroll);
10456 hscrolled_p = 1;
10457 }
10458 }
10459 }
10460
10461 window = w->next;
10462 }
10463
10464 /* Value is non-zero if hscroll of any leaf window has been changed. */
10465 return hscrolled_p;
10466 }
10467
10468
10469 /* Set hscroll so that cursor is visible and not inside horizontal
10470 scroll margins for all windows in the tree rooted at WINDOW. See
10471 also hscroll_window_tree above. Value is non-zero if any window's
10472 hscroll has been changed. If it has, desired matrices on the frame
10473 of WINDOW are cleared. */
10474
10475 static int
10476 hscroll_windows (window)
10477 Lisp_Object window;
10478 {
10479 int hscrolled_p;
10480
10481 if (automatic_hscrolling_p)
10482 {
10483 hscrolled_p = hscroll_window_tree (window);
10484 if (hscrolled_p)
10485 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10486 }
10487 else
10488 hscrolled_p = 0;
10489 return hscrolled_p;
10490 }
10491
10492
10493 \f
10494 /************************************************************************
10495 Redisplay
10496 ************************************************************************/
10497
10498 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10499 to a non-zero value. This is sometimes handy to have in a debugger
10500 session. */
10501
10502 #if GLYPH_DEBUG
10503
10504 /* First and last unchanged row for try_window_id. */
10505
10506 int debug_first_unchanged_at_end_vpos;
10507 int debug_last_unchanged_at_beg_vpos;
10508
10509 /* Delta vpos and y. */
10510
10511 int debug_dvpos, debug_dy;
10512
10513 /* Delta in characters and bytes for try_window_id. */
10514
10515 int debug_delta, debug_delta_bytes;
10516
10517 /* Values of window_end_pos and window_end_vpos at the end of
10518 try_window_id. */
10519
10520 EMACS_INT debug_end_pos, debug_end_vpos;
10521
10522 /* Append a string to W->desired_matrix->method. FMT is a printf
10523 format string. A1...A9 are a supplement for a variable-length
10524 argument list. If trace_redisplay_p is non-zero also printf the
10525 resulting string to stderr. */
10526
10527 static void
10528 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10529 struct window *w;
10530 char *fmt;
10531 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10532 {
10533 char buffer[512];
10534 char *method = w->desired_matrix->method;
10535 int len = strlen (method);
10536 int size = sizeof w->desired_matrix->method;
10537 int remaining = size - len - 1;
10538
10539 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10540 if (len && remaining)
10541 {
10542 method[len] = '|';
10543 --remaining, ++len;
10544 }
10545
10546 strncpy (method + len, buffer, remaining);
10547
10548 if (trace_redisplay_p)
10549 fprintf (stderr, "%p (%s): %s\n",
10550 w,
10551 ((BUFFERP (w->buffer)
10552 && STRINGP (XBUFFER (w->buffer)->name))
10553 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10554 : "no buffer"),
10555 buffer);
10556 }
10557
10558 #endif /* GLYPH_DEBUG */
10559
10560
10561 /* Value is non-zero if all changes in window W, which displays
10562 current_buffer, are in the text between START and END. START is a
10563 buffer position, END is given as a distance from Z. Used in
10564 redisplay_internal for display optimization. */
10565
10566 static INLINE int
10567 text_outside_line_unchanged_p (w, start, end)
10568 struct window *w;
10569 int start, end;
10570 {
10571 int unchanged_p = 1;
10572
10573 /* If text or overlays have changed, see where. */
10574 if (XFASTINT (w->last_modified) < MODIFF
10575 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10576 {
10577 /* Gap in the line? */
10578 if (GPT < start || Z - GPT < end)
10579 unchanged_p = 0;
10580
10581 /* Changes start in front of the line, or end after it? */
10582 if (unchanged_p
10583 && (BEG_UNCHANGED < start - 1
10584 || END_UNCHANGED < end))
10585 unchanged_p = 0;
10586
10587 /* If selective display, can't optimize if changes start at the
10588 beginning of the line. */
10589 if (unchanged_p
10590 && INTEGERP (current_buffer->selective_display)
10591 && XINT (current_buffer->selective_display) > 0
10592 && (BEG_UNCHANGED < start || GPT <= start))
10593 unchanged_p = 0;
10594
10595 /* If there are overlays at the start or end of the line, these
10596 may have overlay strings with newlines in them. A change at
10597 START, for instance, may actually concern the display of such
10598 overlay strings as well, and they are displayed on different
10599 lines. So, quickly rule out this case. (For the future, it
10600 might be desirable to implement something more telling than
10601 just BEG/END_UNCHANGED.) */
10602 if (unchanged_p)
10603 {
10604 if (BEG + BEG_UNCHANGED == start
10605 && overlay_touches_p (start))
10606 unchanged_p = 0;
10607 if (END_UNCHANGED == end
10608 && overlay_touches_p (Z - end))
10609 unchanged_p = 0;
10610 }
10611 }
10612
10613 return unchanged_p;
10614 }
10615
10616
10617 /* Do a frame update, taking possible shortcuts into account. This is
10618 the main external entry point for redisplay.
10619
10620 If the last redisplay displayed an echo area message and that message
10621 is no longer requested, we clear the echo area or bring back the
10622 mini-buffer if that is in use. */
10623
10624 void
10625 redisplay ()
10626 {
10627 redisplay_internal (0);
10628 }
10629
10630
10631 static Lisp_Object
10632 overlay_arrow_string_or_property (var)
10633 Lisp_Object var;
10634 {
10635 Lisp_Object val;
10636
10637 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10638 return val;
10639
10640 return Voverlay_arrow_string;
10641 }
10642
10643 /* Return 1 if there are any overlay-arrows in current_buffer. */
10644 static int
10645 overlay_arrow_in_current_buffer_p ()
10646 {
10647 Lisp_Object vlist;
10648
10649 for (vlist = Voverlay_arrow_variable_list;
10650 CONSP (vlist);
10651 vlist = XCDR (vlist))
10652 {
10653 Lisp_Object var = XCAR (vlist);
10654 Lisp_Object val;
10655
10656 if (!SYMBOLP (var))
10657 continue;
10658 val = find_symbol_value (var);
10659 if (MARKERP (val)
10660 && current_buffer == XMARKER (val)->buffer)
10661 return 1;
10662 }
10663 return 0;
10664 }
10665
10666
10667 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10668 has changed. */
10669
10670 static int
10671 overlay_arrows_changed_p ()
10672 {
10673 Lisp_Object vlist;
10674
10675 for (vlist = Voverlay_arrow_variable_list;
10676 CONSP (vlist);
10677 vlist = XCDR (vlist))
10678 {
10679 Lisp_Object var = XCAR (vlist);
10680 Lisp_Object val, pstr;
10681
10682 if (!SYMBOLP (var))
10683 continue;
10684 val = find_symbol_value (var);
10685 if (!MARKERP (val))
10686 continue;
10687 if (! EQ (COERCE_MARKER (val),
10688 Fget (var, Qlast_arrow_position))
10689 || ! (pstr = overlay_arrow_string_or_property (var),
10690 EQ (pstr, Fget (var, Qlast_arrow_string))))
10691 return 1;
10692 }
10693 return 0;
10694 }
10695
10696 /* Mark overlay arrows to be updated on next redisplay. */
10697
10698 static void
10699 update_overlay_arrows (up_to_date)
10700 int up_to_date;
10701 {
10702 Lisp_Object vlist;
10703
10704 for (vlist = Voverlay_arrow_variable_list;
10705 CONSP (vlist);
10706 vlist = XCDR (vlist))
10707 {
10708 Lisp_Object var = XCAR (vlist);
10709
10710 if (!SYMBOLP (var))
10711 continue;
10712
10713 if (up_to_date > 0)
10714 {
10715 Lisp_Object val = find_symbol_value (var);
10716 Fput (var, Qlast_arrow_position,
10717 COERCE_MARKER (val));
10718 Fput (var, Qlast_arrow_string,
10719 overlay_arrow_string_or_property (var));
10720 }
10721 else if (up_to_date < 0
10722 || !NILP (Fget (var, Qlast_arrow_position)))
10723 {
10724 Fput (var, Qlast_arrow_position, Qt);
10725 Fput (var, Qlast_arrow_string, Qt);
10726 }
10727 }
10728 }
10729
10730
10731 /* Return overlay arrow string to display at row.
10732 Return integer (bitmap number) for arrow bitmap in left fringe.
10733 Return nil if no overlay arrow. */
10734
10735 static Lisp_Object
10736 overlay_arrow_at_row (it, row)
10737 struct it *it;
10738 struct glyph_row *row;
10739 {
10740 Lisp_Object vlist;
10741
10742 for (vlist = Voverlay_arrow_variable_list;
10743 CONSP (vlist);
10744 vlist = XCDR (vlist))
10745 {
10746 Lisp_Object var = XCAR (vlist);
10747 Lisp_Object val;
10748
10749 if (!SYMBOLP (var))
10750 continue;
10751
10752 val = find_symbol_value (var);
10753
10754 if (MARKERP (val)
10755 && current_buffer == XMARKER (val)->buffer
10756 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10757 {
10758 if (FRAME_WINDOW_P (it->f)
10759 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10760 {
10761 #ifdef HAVE_WINDOW_SYSTEM
10762 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10763 {
10764 int fringe_bitmap;
10765 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10766 return make_number (fringe_bitmap);
10767 }
10768 #endif
10769 return make_number (-1); /* Use default arrow bitmap */
10770 }
10771 return overlay_arrow_string_or_property (var);
10772 }
10773 }
10774
10775 return Qnil;
10776 }
10777
10778 /* Return 1 if point moved out of or into a composition. Otherwise
10779 return 0. PREV_BUF and PREV_PT are the last point buffer and
10780 position. BUF and PT are the current point buffer and position. */
10781
10782 int
10783 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10784 struct buffer *prev_buf, *buf;
10785 int prev_pt, pt;
10786 {
10787 int start, end;
10788 Lisp_Object prop;
10789 Lisp_Object buffer;
10790
10791 XSETBUFFER (buffer, buf);
10792 /* Check a composition at the last point if point moved within the
10793 same buffer. */
10794 if (prev_buf == buf)
10795 {
10796 if (prev_pt == pt)
10797 /* Point didn't move. */
10798 return 0;
10799
10800 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10801 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10802 && COMPOSITION_VALID_P (start, end, prop)
10803 && start < prev_pt && end > prev_pt)
10804 /* The last point was within the composition. Return 1 iff
10805 point moved out of the composition. */
10806 return (pt <= start || pt >= end);
10807 }
10808
10809 /* Check a composition at the current point. */
10810 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10811 && find_composition (pt, -1, &start, &end, &prop, buffer)
10812 && COMPOSITION_VALID_P (start, end, prop)
10813 && start < pt && end > pt);
10814 }
10815
10816
10817 /* Reconsider the setting of B->clip_changed which is displayed
10818 in window W. */
10819
10820 static INLINE void
10821 reconsider_clip_changes (w, b)
10822 struct window *w;
10823 struct buffer *b;
10824 {
10825 if (b->clip_changed
10826 && !NILP (w->window_end_valid)
10827 && w->current_matrix->buffer == b
10828 && w->current_matrix->zv == BUF_ZV (b)
10829 && w->current_matrix->begv == BUF_BEGV (b))
10830 b->clip_changed = 0;
10831
10832 /* If display wasn't paused, and W is not a tool bar window, see if
10833 point has been moved into or out of a composition. In that case,
10834 we set b->clip_changed to 1 to force updating the screen. If
10835 b->clip_changed has already been set to 1, we can skip this
10836 check. */
10837 if (!b->clip_changed
10838 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10839 {
10840 int pt;
10841
10842 if (w == XWINDOW (selected_window))
10843 pt = BUF_PT (current_buffer);
10844 else
10845 pt = marker_position (w->pointm);
10846
10847 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10848 || pt != XINT (w->last_point))
10849 && check_point_in_composition (w->current_matrix->buffer,
10850 XINT (w->last_point),
10851 XBUFFER (w->buffer), pt))
10852 b->clip_changed = 1;
10853 }
10854 }
10855 \f
10856
10857 /* Select FRAME to forward the values of frame-local variables into C
10858 variables so that the redisplay routines can access those values
10859 directly. */
10860
10861 static void
10862 select_frame_for_redisplay (frame)
10863 Lisp_Object frame;
10864 {
10865 Lisp_Object tail, sym, val;
10866 Lisp_Object old = selected_frame;
10867
10868 selected_frame = frame;
10869
10870 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10871 if (CONSP (XCAR (tail))
10872 && (sym = XCAR (XCAR (tail)),
10873 SYMBOLP (sym))
10874 && (sym = indirect_variable (sym),
10875 val = SYMBOL_VALUE (sym),
10876 (BUFFER_LOCAL_VALUEP (val)
10877 || SOME_BUFFER_LOCAL_VALUEP (val)))
10878 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10879 /* Use find_symbol_value rather than Fsymbol_value
10880 to avoid an error if it is void. */
10881 find_symbol_value (sym);
10882
10883 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10884 if (CONSP (XCAR (tail))
10885 && (sym = XCAR (XCAR (tail)),
10886 SYMBOLP (sym))
10887 && (sym = indirect_variable (sym),
10888 val = SYMBOL_VALUE (sym),
10889 (BUFFER_LOCAL_VALUEP (val)
10890 || SOME_BUFFER_LOCAL_VALUEP (val)))
10891 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10892 find_symbol_value (sym);
10893 }
10894
10895
10896 #define STOP_POLLING \
10897 do { if (! polling_stopped_here) stop_polling (); \
10898 polling_stopped_here = 1; } while (0)
10899
10900 #define RESUME_POLLING \
10901 do { if (polling_stopped_here) start_polling (); \
10902 polling_stopped_here = 0; } while (0)
10903
10904
10905 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10906 response to any user action; therefore, we should preserve the echo
10907 area. (Actually, our caller does that job.) Perhaps in the future
10908 avoid recentering windows if it is not necessary; currently that
10909 causes some problems. */
10910
10911 static void
10912 redisplay_internal (preserve_echo_area)
10913 int preserve_echo_area;
10914 {
10915 struct window *w = XWINDOW (selected_window);
10916 struct frame *f;
10917 int pause;
10918 int must_finish = 0;
10919 struct text_pos tlbufpos, tlendpos;
10920 int number_of_visible_frames;
10921 int count, count1;
10922 struct frame *sf;
10923 int polling_stopped_here = 0;
10924
10925 /* Non-zero means redisplay has to consider all windows on all
10926 frames. Zero means, only selected_window is considered. */
10927 int consider_all_windows_p;
10928
10929 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10930
10931 /* No redisplay if running in batch mode or frame is not yet fully
10932 initialized, or redisplay is explicitly turned off by setting
10933 Vinhibit_redisplay. */
10934 if (noninteractive
10935 || !NILP (Vinhibit_redisplay))
10936 return;
10937
10938 /* Don't examine these until after testing Vinhibit_redisplay.
10939 When Emacs is shutting down, perhaps because its connection to
10940 X has dropped, we should not look at them at all. */
10941 f = XFRAME (w->frame);
10942 sf = SELECTED_FRAME ();
10943
10944 if (!f->glyphs_initialized_p)
10945 return;
10946
10947 /* The flag redisplay_performed_directly_p is set by
10948 direct_output_for_insert when it already did the whole screen
10949 update necessary. */
10950 if (redisplay_performed_directly_p)
10951 {
10952 redisplay_performed_directly_p = 0;
10953 if (!hscroll_windows (selected_window))
10954 return;
10955 }
10956
10957 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10958 if (popup_activated ())
10959 return;
10960 #endif
10961
10962 /* I don't think this happens but let's be paranoid. */
10963 if (redisplaying_p)
10964 return;
10965
10966 /* Record a function that resets redisplaying_p to its old value
10967 when we leave this function. */
10968 count = SPECPDL_INDEX ();
10969 record_unwind_protect (unwind_redisplay,
10970 Fcons (make_number (redisplaying_p), selected_frame));
10971 ++redisplaying_p;
10972 specbind (Qinhibit_free_realized_faces, Qnil);
10973
10974 {
10975 Lisp_Object tail, frame;
10976
10977 FOR_EACH_FRAME (tail, frame)
10978 {
10979 struct frame *f = XFRAME (frame);
10980 f->already_hscrolled_p = 0;
10981 }
10982 }
10983
10984 retry:
10985 pause = 0;
10986 reconsider_clip_changes (w, current_buffer);
10987 last_escape_glyph_frame = NULL;
10988 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10989
10990 /* If new fonts have been loaded that make a glyph matrix adjustment
10991 necessary, do it. */
10992 if (fonts_changed_p)
10993 {
10994 adjust_glyphs (NULL);
10995 ++windows_or_buffers_changed;
10996 fonts_changed_p = 0;
10997 }
10998
10999 /* If face_change_count is non-zero, init_iterator will free all
11000 realized faces, which includes the faces referenced from current
11001 matrices. So, we can't reuse current matrices in this case. */
11002 if (face_change_count)
11003 ++windows_or_buffers_changed;
11004
11005 if (! FRAME_WINDOW_P (sf)
11006 && previous_terminal_frame != sf)
11007 {
11008 /* Since frames on an ASCII terminal share the same display
11009 area, displaying a different frame means redisplay the whole
11010 thing. */
11011 windows_or_buffers_changed++;
11012 SET_FRAME_GARBAGED (sf);
11013 XSETFRAME (Vterminal_frame, sf);
11014 }
11015 previous_terminal_frame = sf;
11016
11017 /* Set the visible flags for all frames. Do this before checking
11018 for resized or garbaged frames; they want to know if their frames
11019 are visible. See the comment in frame.h for
11020 FRAME_SAMPLE_VISIBILITY. */
11021 {
11022 Lisp_Object tail, frame;
11023
11024 number_of_visible_frames = 0;
11025
11026 FOR_EACH_FRAME (tail, frame)
11027 {
11028 struct frame *f = XFRAME (frame);
11029
11030 FRAME_SAMPLE_VISIBILITY (f);
11031 if (FRAME_VISIBLE_P (f))
11032 ++number_of_visible_frames;
11033 clear_desired_matrices (f);
11034 }
11035 }
11036
11037 /* Notice any pending interrupt request to change frame size. */
11038 do_pending_window_change (1);
11039
11040 /* Clear frames marked as garbaged. */
11041 if (frame_garbaged)
11042 clear_garbaged_frames ();
11043
11044 /* Build menubar and tool-bar items. */
11045 if (NILP (Vmemory_full))
11046 prepare_menu_bars ();
11047
11048 if (windows_or_buffers_changed)
11049 update_mode_lines++;
11050
11051 /* Detect case that we need to write or remove a star in the mode line. */
11052 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11053 {
11054 w->update_mode_line = Qt;
11055 if (buffer_shared > 1)
11056 update_mode_lines++;
11057 }
11058
11059 /* Avoid invocation of point motion hooks by `current_column' below. */
11060 count1 = SPECPDL_INDEX ();
11061 specbind (Qinhibit_point_motion_hooks, Qt);
11062
11063 /* If %c is in the mode line, update it if needed. */
11064 if (!NILP (w->column_number_displayed)
11065 /* This alternative quickly identifies a common case
11066 where no change is needed. */
11067 && !(PT == XFASTINT (w->last_point)
11068 && XFASTINT (w->last_modified) >= MODIFF
11069 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11070 && (XFASTINT (w->column_number_displayed)
11071 != (int) current_column ())) /* iftc */
11072 w->update_mode_line = Qt;
11073
11074 unbind_to (count1, Qnil);
11075
11076 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11077
11078 /* The variable buffer_shared is set in redisplay_window and
11079 indicates that we redisplay a buffer in different windows. See
11080 there. */
11081 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11082 || cursor_type_changed);
11083
11084 /* If specs for an arrow have changed, do thorough redisplay
11085 to ensure we remove any arrow that should no longer exist. */
11086 if (overlay_arrows_changed_p ())
11087 consider_all_windows_p = windows_or_buffers_changed = 1;
11088
11089 /* Normally the message* functions will have already displayed and
11090 updated the echo area, but the frame may have been trashed, or
11091 the update may have been preempted, so display the echo area
11092 again here. Checking message_cleared_p captures the case that
11093 the echo area should be cleared. */
11094 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11095 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11096 || (message_cleared_p
11097 && minibuf_level == 0
11098 /* If the mini-window is currently selected, this means the
11099 echo-area doesn't show through. */
11100 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11101 {
11102 int window_height_changed_p = echo_area_display (0);
11103 must_finish = 1;
11104
11105 /* If we don't display the current message, don't clear the
11106 message_cleared_p flag, because, if we did, we wouldn't clear
11107 the echo area in the next redisplay which doesn't preserve
11108 the echo area. */
11109 if (!display_last_displayed_message_p)
11110 message_cleared_p = 0;
11111
11112 if (fonts_changed_p)
11113 goto retry;
11114 else if (window_height_changed_p)
11115 {
11116 consider_all_windows_p = 1;
11117 ++update_mode_lines;
11118 ++windows_or_buffers_changed;
11119
11120 /* If window configuration was changed, frames may have been
11121 marked garbaged. Clear them or we will experience
11122 surprises wrt scrolling. */
11123 if (frame_garbaged)
11124 clear_garbaged_frames ();
11125 }
11126 }
11127 else if (EQ (selected_window, minibuf_window)
11128 && (current_buffer->clip_changed
11129 || XFASTINT (w->last_modified) < MODIFF
11130 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11131 && resize_mini_window (w, 0))
11132 {
11133 /* Resized active mini-window to fit the size of what it is
11134 showing if its contents might have changed. */
11135 must_finish = 1;
11136 consider_all_windows_p = 1;
11137 ++windows_or_buffers_changed;
11138 ++update_mode_lines;
11139
11140 /* If window configuration was changed, frames may have been
11141 marked garbaged. Clear them or we will experience
11142 surprises wrt scrolling. */
11143 if (frame_garbaged)
11144 clear_garbaged_frames ();
11145 }
11146
11147
11148 /* If showing the region, and mark has changed, we must redisplay
11149 the whole window. The assignment to this_line_start_pos prevents
11150 the optimization directly below this if-statement. */
11151 if (((!NILP (Vtransient_mark_mode)
11152 && !NILP (XBUFFER (w->buffer)->mark_active))
11153 != !NILP (w->region_showing))
11154 || (!NILP (w->region_showing)
11155 && !EQ (w->region_showing,
11156 Fmarker_position (XBUFFER (w->buffer)->mark))))
11157 CHARPOS (this_line_start_pos) = 0;
11158
11159 /* Optimize the case that only the line containing the cursor in the
11160 selected window has changed. Variables starting with this_ are
11161 set in display_line and record information about the line
11162 containing the cursor. */
11163 tlbufpos = this_line_start_pos;
11164 tlendpos = this_line_end_pos;
11165 if (!consider_all_windows_p
11166 && CHARPOS (tlbufpos) > 0
11167 && NILP (w->update_mode_line)
11168 && !current_buffer->clip_changed
11169 && !current_buffer->prevent_redisplay_optimizations_p
11170 && FRAME_VISIBLE_P (XFRAME (w->frame))
11171 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11172 /* Make sure recorded data applies to current buffer, etc. */
11173 && this_line_buffer == current_buffer
11174 && current_buffer == XBUFFER (w->buffer)
11175 && NILP (w->force_start)
11176 && NILP (w->optional_new_start)
11177 /* Point must be on the line that we have info recorded about. */
11178 && PT >= CHARPOS (tlbufpos)
11179 && PT <= Z - CHARPOS (tlendpos)
11180 /* All text outside that line, including its final newline,
11181 must be unchanged */
11182 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11183 CHARPOS (tlendpos)))
11184 {
11185 if (CHARPOS (tlbufpos) > BEGV
11186 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11187 && (CHARPOS (tlbufpos) == ZV
11188 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11189 /* Former continuation line has disappeared by becoming empty */
11190 goto cancel;
11191 else if (XFASTINT (w->last_modified) < MODIFF
11192 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11193 || MINI_WINDOW_P (w))
11194 {
11195 /* We have to handle the case of continuation around a
11196 wide-column character (See the comment in indent.c around
11197 line 885).
11198
11199 For instance, in the following case:
11200
11201 -------- Insert --------
11202 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11203 J_I_ ==> J_I_ `^^' are cursors.
11204 ^^ ^^
11205 -------- --------
11206
11207 As we have to redraw the line above, we should goto cancel. */
11208
11209 struct it it;
11210 int line_height_before = this_line_pixel_height;
11211
11212 /* Note that start_display will handle the case that the
11213 line starting at tlbufpos is a continuation lines. */
11214 start_display (&it, w, tlbufpos);
11215
11216 /* Implementation note: It this still necessary? */
11217 if (it.current_x != this_line_start_x)
11218 goto cancel;
11219
11220 TRACE ((stderr, "trying display optimization 1\n"));
11221 w->cursor.vpos = -1;
11222 overlay_arrow_seen = 0;
11223 it.vpos = this_line_vpos;
11224 it.current_y = this_line_y;
11225 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11226 display_line (&it);
11227
11228 /* If line contains point, is not continued,
11229 and ends at same distance from eob as before, we win */
11230 if (w->cursor.vpos >= 0
11231 /* Line is not continued, otherwise this_line_start_pos
11232 would have been set to 0 in display_line. */
11233 && CHARPOS (this_line_start_pos)
11234 /* Line ends as before. */
11235 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11236 /* Line has same height as before. Otherwise other lines
11237 would have to be shifted up or down. */
11238 && this_line_pixel_height == line_height_before)
11239 {
11240 /* If this is not the window's last line, we must adjust
11241 the charstarts of the lines below. */
11242 if (it.current_y < it.last_visible_y)
11243 {
11244 struct glyph_row *row
11245 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11246 int delta, delta_bytes;
11247
11248 if (Z - CHARPOS (tlendpos) == ZV)
11249 {
11250 /* This line ends at end of (accessible part of)
11251 buffer. There is no newline to count. */
11252 delta = (Z
11253 - CHARPOS (tlendpos)
11254 - MATRIX_ROW_START_CHARPOS (row));
11255 delta_bytes = (Z_BYTE
11256 - BYTEPOS (tlendpos)
11257 - MATRIX_ROW_START_BYTEPOS (row));
11258 }
11259 else
11260 {
11261 /* This line ends in a newline. Must take
11262 account of the newline and the rest of the
11263 text that follows. */
11264 delta = (Z
11265 - CHARPOS (tlendpos)
11266 - MATRIX_ROW_START_CHARPOS (row));
11267 delta_bytes = (Z_BYTE
11268 - BYTEPOS (tlendpos)
11269 - MATRIX_ROW_START_BYTEPOS (row));
11270 }
11271
11272 increment_matrix_positions (w->current_matrix,
11273 this_line_vpos + 1,
11274 w->current_matrix->nrows,
11275 delta, delta_bytes);
11276 }
11277
11278 /* If this row displays text now but previously didn't,
11279 or vice versa, w->window_end_vpos may have to be
11280 adjusted. */
11281 if ((it.glyph_row - 1)->displays_text_p)
11282 {
11283 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11284 XSETINT (w->window_end_vpos, this_line_vpos);
11285 }
11286 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11287 && this_line_vpos > 0)
11288 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11289 w->window_end_valid = Qnil;
11290
11291 /* Update hint: No need to try to scroll in update_window. */
11292 w->desired_matrix->no_scrolling_p = 1;
11293
11294 #if GLYPH_DEBUG
11295 *w->desired_matrix->method = 0;
11296 debug_method_add (w, "optimization 1");
11297 #endif
11298 #ifdef HAVE_WINDOW_SYSTEM
11299 update_window_fringes (w, 0);
11300 #endif
11301 goto update;
11302 }
11303 else
11304 goto cancel;
11305 }
11306 else if (/* Cursor position hasn't changed. */
11307 PT == XFASTINT (w->last_point)
11308 /* Make sure the cursor was last displayed
11309 in this window. Otherwise we have to reposition it. */
11310 && 0 <= w->cursor.vpos
11311 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11312 {
11313 if (!must_finish)
11314 {
11315 do_pending_window_change (1);
11316
11317 /* We used to always goto end_of_redisplay here, but this
11318 isn't enough if we have a blinking cursor. */
11319 if (w->cursor_off_p == w->last_cursor_off_p)
11320 goto end_of_redisplay;
11321 }
11322 goto update;
11323 }
11324 /* If highlighting the region, or if the cursor is in the echo area,
11325 then we can't just move the cursor. */
11326 else if (! (!NILP (Vtransient_mark_mode)
11327 && !NILP (current_buffer->mark_active))
11328 && (EQ (selected_window, current_buffer->last_selected_window)
11329 || highlight_nonselected_windows)
11330 && NILP (w->region_showing)
11331 && NILP (Vshow_trailing_whitespace)
11332 && !cursor_in_echo_area)
11333 {
11334 struct it it;
11335 struct glyph_row *row;
11336
11337 /* Skip from tlbufpos to PT and see where it is. Note that
11338 PT may be in invisible text. If so, we will end at the
11339 next visible position. */
11340 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11341 NULL, DEFAULT_FACE_ID);
11342 it.current_x = this_line_start_x;
11343 it.current_y = this_line_y;
11344 it.vpos = this_line_vpos;
11345
11346 /* The call to move_it_to stops in front of PT, but
11347 moves over before-strings. */
11348 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11349
11350 if (it.vpos == this_line_vpos
11351 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11352 row->enabled_p))
11353 {
11354 xassert (this_line_vpos == it.vpos);
11355 xassert (this_line_y == it.current_y);
11356 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11357 #if GLYPH_DEBUG
11358 *w->desired_matrix->method = 0;
11359 debug_method_add (w, "optimization 3");
11360 #endif
11361 goto update;
11362 }
11363 else
11364 goto cancel;
11365 }
11366
11367 cancel:
11368 /* Text changed drastically or point moved off of line. */
11369 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11370 }
11371
11372 CHARPOS (this_line_start_pos) = 0;
11373 consider_all_windows_p |= buffer_shared > 1;
11374 ++clear_face_cache_count;
11375 #ifdef HAVE_WINDOW_SYSTEM
11376 ++clear_image_cache_count;
11377 #endif
11378
11379 /* Build desired matrices, and update the display. If
11380 consider_all_windows_p is non-zero, do it for all windows on all
11381 frames. Otherwise do it for selected_window, only. */
11382
11383 if (consider_all_windows_p)
11384 {
11385 Lisp_Object tail, frame;
11386
11387 FOR_EACH_FRAME (tail, frame)
11388 XFRAME (frame)->updated_p = 0;
11389
11390 /* Recompute # windows showing selected buffer. This will be
11391 incremented each time such a window is displayed. */
11392 buffer_shared = 0;
11393
11394 FOR_EACH_FRAME (tail, frame)
11395 {
11396 struct frame *f = XFRAME (frame);
11397
11398 if (FRAME_WINDOW_P (f) || f == sf)
11399 {
11400 if (! EQ (frame, selected_frame))
11401 /* Select the frame, for the sake of frame-local
11402 variables. */
11403 select_frame_for_redisplay (frame);
11404
11405 /* Mark all the scroll bars to be removed; we'll redeem
11406 the ones we want when we redisplay their windows. */
11407 if (condemn_scroll_bars_hook)
11408 condemn_scroll_bars_hook (f);
11409
11410 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11411 redisplay_windows (FRAME_ROOT_WINDOW (f));
11412
11413 /* Any scroll bars which redisplay_windows should have
11414 nuked should now go away. */
11415 if (judge_scroll_bars_hook)
11416 judge_scroll_bars_hook (f);
11417
11418 /* If fonts changed, display again. */
11419 /* ??? rms: I suspect it is a mistake to jump all the way
11420 back to retry here. It should just retry this frame. */
11421 if (fonts_changed_p)
11422 goto retry;
11423
11424 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11425 {
11426 /* See if we have to hscroll. */
11427 if (!f->already_hscrolled_p)
11428 {
11429 f->already_hscrolled_p = 1;
11430 if (hscroll_windows (f->root_window))
11431 goto retry;
11432 }
11433
11434 /* Prevent various kinds of signals during display
11435 update. stdio is not robust about handling
11436 signals, which can cause an apparent I/O
11437 error. */
11438 if (interrupt_input)
11439 unrequest_sigio ();
11440 STOP_POLLING;
11441
11442 /* Update the display. */
11443 set_window_update_flags (XWINDOW (f->root_window), 1);
11444 pause |= update_frame (f, 0, 0);
11445 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11446 if (pause)
11447 break;
11448 #endif
11449
11450 f->updated_p = 1;
11451 }
11452 }
11453 }
11454
11455 if (!pause)
11456 {
11457 /* Do the mark_window_display_accurate after all windows have
11458 been redisplayed because this call resets flags in buffers
11459 which are needed for proper redisplay. */
11460 FOR_EACH_FRAME (tail, frame)
11461 {
11462 struct frame *f = XFRAME (frame);
11463 if (f->updated_p)
11464 {
11465 mark_window_display_accurate (f->root_window, 1);
11466 if (frame_up_to_date_hook)
11467 frame_up_to_date_hook (f);
11468 }
11469 }
11470 }
11471 }
11472 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11473 {
11474 Lisp_Object mini_window;
11475 struct frame *mini_frame;
11476
11477 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11478 /* Use list_of_error, not Qerror, so that
11479 we catch only errors and don't run the debugger. */
11480 internal_condition_case_1 (redisplay_window_1, selected_window,
11481 list_of_error,
11482 redisplay_window_error);
11483
11484 /* Compare desired and current matrices, perform output. */
11485
11486 update:
11487 /* If fonts changed, display again. */
11488 if (fonts_changed_p)
11489 goto retry;
11490
11491 /* Prevent various kinds of signals during display update.
11492 stdio is not robust about handling signals,
11493 which can cause an apparent I/O error. */
11494 if (interrupt_input)
11495 unrequest_sigio ();
11496 STOP_POLLING;
11497
11498 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11499 {
11500 if (hscroll_windows (selected_window))
11501 goto retry;
11502
11503 XWINDOW (selected_window)->must_be_updated_p = 1;
11504 pause = update_frame (sf, 0, 0);
11505 }
11506
11507 /* We may have called echo_area_display at the top of this
11508 function. If the echo area is on another frame, that may
11509 have put text on a frame other than the selected one, so the
11510 above call to update_frame would not have caught it. Catch
11511 it here. */
11512 mini_window = FRAME_MINIBUF_WINDOW (sf);
11513 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11514
11515 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11516 {
11517 XWINDOW (mini_window)->must_be_updated_p = 1;
11518 pause |= update_frame (mini_frame, 0, 0);
11519 if (!pause && hscroll_windows (mini_window))
11520 goto retry;
11521 }
11522 }
11523
11524 /* If display was paused because of pending input, make sure we do a
11525 thorough update the next time. */
11526 if (pause)
11527 {
11528 /* Prevent the optimization at the beginning of
11529 redisplay_internal that tries a single-line update of the
11530 line containing the cursor in the selected window. */
11531 CHARPOS (this_line_start_pos) = 0;
11532
11533 /* Let the overlay arrow be updated the next time. */
11534 update_overlay_arrows (0);
11535
11536 /* If we pause after scrolling, some rows in the current
11537 matrices of some windows are not valid. */
11538 if (!WINDOW_FULL_WIDTH_P (w)
11539 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11540 update_mode_lines = 1;
11541 }
11542 else
11543 {
11544 if (!consider_all_windows_p)
11545 {
11546 /* This has already been done above if
11547 consider_all_windows_p is set. */
11548 mark_window_display_accurate_1 (w, 1);
11549
11550 /* Say overlay arrows are up to date. */
11551 update_overlay_arrows (1);
11552
11553 if (frame_up_to_date_hook != 0)
11554 frame_up_to_date_hook (sf);
11555 }
11556
11557 update_mode_lines = 0;
11558 windows_or_buffers_changed = 0;
11559 cursor_type_changed = 0;
11560 }
11561
11562 /* Start SIGIO interrupts coming again. Having them off during the
11563 code above makes it less likely one will discard output, but not
11564 impossible, since there might be stuff in the system buffer here.
11565 But it is much hairier to try to do anything about that. */
11566 if (interrupt_input)
11567 request_sigio ();
11568 RESUME_POLLING;
11569
11570 /* If a frame has become visible which was not before, redisplay
11571 again, so that we display it. Expose events for such a frame
11572 (which it gets when becoming visible) don't call the parts of
11573 redisplay constructing glyphs, so simply exposing a frame won't
11574 display anything in this case. So, we have to display these
11575 frames here explicitly. */
11576 if (!pause)
11577 {
11578 Lisp_Object tail, frame;
11579 int new_count = 0;
11580
11581 FOR_EACH_FRAME (tail, frame)
11582 {
11583 int this_is_visible = 0;
11584
11585 if (XFRAME (frame)->visible)
11586 this_is_visible = 1;
11587 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11588 if (XFRAME (frame)->visible)
11589 this_is_visible = 1;
11590
11591 if (this_is_visible)
11592 new_count++;
11593 }
11594
11595 if (new_count != number_of_visible_frames)
11596 windows_or_buffers_changed++;
11597 }
11598
11599 /* Change frame size now if a change is pending. */
11600 do_pending_window_change (1);
11601
11602 /* If we just did a pending size change, or have additional
11603 visible frames, redisplay again. */
11604 if (windows_or_buffers_changed && !pause)
11605 goto retry;
11606
11607 /* Clear the face cache eventually. */
11608 if (consider_all_windows_p)
11609 {
11610 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11611 {
11612 clear_face_cache (0);
11613 clear_face_cache_count = 0;
11614 }
11615 #ifdef HAVE_WINDOW_SYSTEM
11616 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11617 {
11618 Lisp_Object tail, frame;
11619 FOR_EACH_FRAME (tail, frame)
11620 {
11621 struct frame *f = XFRAME (frame);
11622 if (FRAME_WINDOW_P (f))
11623 clear_image_cache (f, 0);
11624 }
11625 clear_image_cache_count = 0;
11626 }
11627 #endif /* HAVE_WINDOW_SYSTEM */
11628 }
11629
11630 end_of_redisplay:
11631 unbind_to (count, Qnil);
11632 RESUME_POLLING;
11633 }
11634
11635
11636 /* Redisplay, but leave alone any recent echo area message unless
11637 another message has been requested in its place.
11638
11639 This is useful in situations where you need to redisplay but no
11640 user action has occurred, making it inappropriate for the message
11641 area to be cleared. See tracking_off and
11642 wait_reading_process_output for examples of these situations.
11643
11644 FROM_WHERE is an integer saying from where this function was
11645 called. This is useful for debugging. */
11646
11647 void
11648 redisplay_preserve_echo_area (from_where)
11649 int from_where;
11650 {
11651 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11652
11653 if (!NILP (echo_area_buffer[1]))
11654 {
11655 /* We have a previously displayed message, but no current
11656 message. Redisplay the previous message. */
11657 display_last_displayed_message_p = 1;
11658 redisplay_internal (1);
11659 display_last_displayed_message_p = 0;
11660 }
11661 else
11662 redisplay_internal (1);
11663
11664 if (rif != NULL && rif->flush_display_optional)
11665 rif->flush_display_optional (NULL);
11666 }
11667
11668
11669 /* Function registered with record_unwind_protect in
11670 redisplay_internal. Reset redisplaying_p to the value it had
11671 before redisplay_internal was called, and clear
11672 prevent_freeing_realized_faces_p. It also selects the previously
11673 selected frame. */
11674
11675 static Lisp_Object
11676 unwind_redisplay (val)
11677 Lisp_Object val;
11678 {
11679 Lisp_Object old_redisplaying_p, old_frame;
11680
11681 old_redisplaying_p = XCAR (val);
11682 redisplaying_p = XFASTINT (old_redisplaying_p);
11683 old_frame = XCDR (val);
11684 if (! EQ (old_frame, selected_frame))
11685 select_frame_for_redisplay (old_frame);
11686 return Qnil;
11687 }
11688
11689
11690 /* Mark the display of window W as accurate or inaccurate. If
11691 ACCURATE_P is non-zero mark display of W as accurate. If
11692 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11693 redisplay_internal is called. */
11694
11695 static void
11696 mark_window_display_accurate_1 (w, accurate_p)
11697 struct window *w;
11698 int accurate_p;
11699 {
11700 if (BUFFERP (w->buffer))
11701 {
11702 struct buffer *b = XBUFFER (w->buffer);
11703
11704 w->last_modified
11705 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11706 w->last_overlay_modified
11707 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11708 w->last_had_star
11709 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11710
11711 if (accurate_p)
11712 {
11713 b->clip_changed = 0;
11714 b->prevent_redisplay_optimizations_p = 0;
11715
11716 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11717 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11718 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11719 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11720
11721 w->current_matrix->buffer = b;
11722 w->current_matrix->begv = BUF_BEGV (b);
11723 w->current_matrix->zv = BUF_ZV (b);
11724
11725 w->last_cursor = w->cursor;
11726 w->last_cursor_off_p = w->cursor_off_p;
11727
11728 if (w == XWINDOW (selected_window))
11729 w->last_point = make_number (BUF_PT (b));
11730 else
11731 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11732 }
11733 }
11734
11735 if (accurate_p)
11736 {
11737 w->window_end_valid = w->buffer;
11738 #if 0 /* This is incorrect with variable-height lines. */
11739 xassert (XINT (w->window_end_vpos)
11740 < (WINDOW_TOTAL_LINES (w)
11741 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11742 #endif
11743 w->update_mode_line = Qnil;
11744 }
11745 }
11746
11747
11748 /* Mark the display of windows in the window tree rooted at WINDOW as
11749 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11750 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11751 be redisplayed the next time redisplay_internal is called. */
11752
11753 void
11754 mark_window_display_accurate (window, accurate_p)
11755 Lisp_Object window;
11756 int accurate_p;
11757 {
11758 struct window *w;
11759
11760 for (; !NILP (window); window = w->next)
11761 {
11762 w = XWINDOW (window);
11763 mark_window_display_accurate_1 (w, accurate_p);
11764
11765 if (!NILP (w->vchild))
11766 mark_window_display_accurate (w->vchild, accurate_p);
11767 if (!NILP (w->hchild))
11768 mark_window_display_accurate (w->hchild, accurate_p);
11769 }
11770
11771 if (accurate_p)
11772 {
11773 update_overlay_arrows (1);
11774 }
11775 else
11776 {
11777 /* Force a thorough redisplay the next time by setting
11778 last_arrow_position and last_arrow_string to t, which is
11779 unequal to any useful value of Voverlay_arrow_... */
11780 update_overlay_arrows (-1);
11781 }
11782 }
11783
11784
11785 /* Return value in display table DP (Lisp_Char_Table *) for character
11786 C. Since a display table doesn't have any parent, we don't have to
11787 follow parent. Do not call this function directly but use the
11788 macro DISP_CHAR_VECTOR. */
11789
11790 Lisp_Object
11791 disp_char_vector (dp, c)
11792 struct Lisp_Char_Table *dp;
11793 int c;
11794 {
11795 int code[4], i;
11796 Lisp_Object val;
11797
11798 if (SINGLE_BYTE_CHAR_P (c))
11799 return (dp->contents[c]);
11800
11801 SPLIT_CHAR (c, code[0], code[1], code[2]);
11802 if (code[1] < 32)
11803 code[1] = -1;
11804 else if (code[2] < 32)
11805 code[2] = -1;
11806
11807 /* Here, the possible range of code[0] (== charset ID) is
11808 128..max_charset. Since the top level char table contains data
11809 for multibyte characters after 256th element, we must increment
11810 code[0] by 128 to get a correct index. */
11811 code[0] += 128;
11812 code[3] = -1; /* anchor */
11813
11814 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11815 {
11816 val = dp->contents[code[i]];
11817 if (!SUB_CHAR_TABLE_P (val))
11818 return (NILP (val) ? dp->defalt : val);
11819 }
11820
11821 /* Here, val is a sub char table. We return the default value of
11822 it. */
11823 return (dp->defalt);
11824 }
11825
11826
11827 \f
11828 /***********************************************************************
11829 Window Redisplay
11830 ***********************************************************************/
11831
11832 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11833
11834 static void
11835 redisplay_windows (window)
11836 Lisp_Object window;
11837 {
11838 while (!NILP (window))
11839 {
11840 struct window *w = XWINDOW (window);
11841
11842 if (!NILP (w->hchild))
11843 redisplay_windows (w->hchild);
11844 else if (!NILP (w->vchild))
11845 redisplay_windows (w->vchild);
11846 else
11847 {
11848 displayed_buffer = XBUFFER (w->buffer);
11849 /* Use list_of_error, not Qerror, so that
11850 we catch only errors and don't run the debugger. */
11851 internal_condition_case_1 (redisplay_window_0, window,
11852 list_of_error,
11853 redisplay_window_error);
11854 }
11855
11856 window = w->next;
11857 }
11858 }
11859
11860 static Lisp_Object
11861 redisplay_window_error ()
11862 {
11863 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11864 return Qnil;
11865 }
11866
11867 static Lisp_Object
11868 redisplay_window_0 (window)
11869 Lisp_Object window;
11870 {
11871 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11872 redisplay_window (window, 0);
11873 return Qnil;
11874 }
11875
11876 static Lisp_Object
11877 redisplay_window_1 (window)
11878 Lisp_Object window;
11879 {
11880 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11881 redisplay_window (window, 1);
11882 return Qnil;
11883 }
11884 \f
11885
11886 /* Increment GLYPH until it reaches END or CONDITION fails while
11887 adding (GLYPH)->pixel_width to X. */
11888
11889 #define SKIP_GLYPHS(glyph, end, x, condition) \
11890 do \
11891 { \
11892 (x) += (glyph)->pixel_width; \
11893 ++(glyph); \
11894 } \
11895 while ((glyph) < (end) && (condition))
11896
11897
11898 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11899 DELTA is the number of bytes by which positions recorded in ROW
11900 differ from current buffer positions.
11901
11902 Return 0 if cursor is not on this row. 1 otherwise. */
11903
11904 int
11905 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11906 struct window *w;
11907 struct glyph_row *row;
11908 struct glyph_matrix *matrix;
11909 int delta, delta_bytes, dy, dvpos;
11910 {
11911 struct glyph *glyph = row->glyphs[TEXT_AREA];
11912 struct glyph *end = glyph + row->used[TEXT_AREA];
11913 struct glyph *cursor = NULL;
11914 /* The first glyph that starts a sequence of glyphs from string. */
11915 struct glyph *string_start;
11916 /* The X coordinate of string_start. */
11917 int string_start_x;
11918 /* The last known character position. */
11919 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11920 /* The last known character position before string_start. */
11921 int string_before_pos;
11922 int x = row->x;
11923 int cursor_x = x;
11924 int cursor_from_overlay_pos = 0;
11925 int pt_old = PT - delta;
11926
11927 /* Skip over glyphs not having an object at the start of the row.
11928 These are special glyphs like truncation marks on terminal
11929 frames. */
11930 if (row->displays_text_p)
11931 while (glyph < end
11932 && INTEGERP (glyph->object)
11933 && glyph->charpos < 0)
11934 {
11935 x += glyph->pixel_width;
11936 ++glyph;
11937 }
11938
11939 string_start = NULL;
11940 while (glyph < end
11941 && !INTEGERP (glyph->object)
11942 && (!BUFFERP (glyph->object)
11943 || (last_pos = glyph->charpos) < pt_old))
11944 {
11945 if (! STRINGP (glyph->object))
11946 {
11947 string_start = NULL;
11948 x += glyph->pixel_width;
11949 ++glyph;
11950 if (cursor_from_overlay_pos
11951 && last_pos >= cursor_from_overlay_pos)
11952 {
11953 cursor_from_overlay_pos = 0;
11954 cursor = 0;
11955 }
11956 }
11957 else
11958 {
11959 if (string_start == NULL)
11960 {
11961 string_before_pos = last_pos;
11962 string_start = glyph;
11963 string_start_x = x;
11964 }
11965 /* Skip all glyphs from string. */
11966 do
11967 {
11968 Lisp_Object cprop;
11969 int pos;
11970 if ((cursor == NULL || glyph > cursor)
11971 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11972 Qcursor, (glyph)->object),
11973 !NILP (cprop))
11974 && (pos = string_buffer_position (w, glyph->object,
11975 string_before_pos),
11976 (pos == 0 /* From overlay */
11977 || pos == pt_old)))
11978 {
11979 /* Estimate overlay buffer position from the buffer
11980 positions of the glyphs before and after the overlay.
11981 Add 1 to last_pos so that if point corresponds to the
11982 glyph right after the overlay, we still use a 'cursor'
11983 property found in that overlay. */
11984 cursor_from_overlay_pos = (pos ? 0 : last_pos
11985 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11986 cursor = glyph;
11987 cursor_x = x;
11988 }
11989 x += glyph->pixel_width;
11990 ++glyph;
11991 }
11992 while (glyph < end && EQ (glyph->object, string_start->object));
11993 }
11994 }
11995
11996 if (cursor != NULL)
11997 {
11998 glyph = cursor;
11999 x = cursor_x;
12000 }
12001 else if (row->ends_in_ellipsis_p && glyph == end)
12002 {
12003 /* Scan back over the ellipsis glyphs, decrementing positions. */
12004 while (glyph > row->glyphs[TEXT_AREA]
12005 && (glyph - 1)->charpos == last_pos)
12006 glyph--, x -= glyph->pixel_width;
12007 /* That loop always goes one position too far,
12008 including the glyph before the ellipsis.
12009 So scan forward over that one. */
12010 x += glyph->pixel_width;
12011 glyph++;
12012 }
12013 else if (string_start
12014 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12015 {
12016 /* We may have skipped over point because the previous glyphs
12017 are from string. As there's no easy way to know the
12018 character position of the current glyph, find the correct
12019 glyph on point by scanning from string_start again. */
12020 Lisp_Object limit;
12021 Lisp_Object string;
12022 struct glyph *stop = glyph;
12023 int pos;
12024
12025 limit = make_number (pt_old + 1);
12026 glyph = string_start;
12027 x = string_start_x;
12028 string = glyph->object;
12029 pos = string_buffer_position (w, string, string_before_pos);
12030 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12031 because we always put cursor after overlay strings. */
12032 while (pos == 0 && glyph < stop)
12033 {
12034 string = glyph->object;
12035 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12036 if (glyph < stop)
12037 pos = string_buffer_position (w, glyph->object, string_before_pos);
12038 }
12039
12040 while (glyph < stop)
12041 {
12042 pos = XINT (Fnext_single_char_property_change
12043 (make_number (pos), Qdisplay, Qnil, limit));
12044 if (pos > pt_old)
12045 break;
12046 /* Skip glyphs from the same string. */
12047 string = glyph->object;
12048 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12049 /* Skip glyphs from an overlay. */
12050 while (glyph < stop
12051 && ! string_buffer_position (w, glyph->object, pos))
12052 {
12053 string = glyph->object;
12054 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12055 }
12056 }
12057
12058 /* If we reached the end of the line, and end was from a string,
12059 cursor is not on this line. */
12060 if (glyph == end && row->continued_p)
12061 return 0;
12062 }
12063
12064 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12065 w->cursor.x = x;
12066 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12067 w->cursor.y = row->y + dy;
12068
12069 if (w == XWINDOW (selected_window))
12070 {
12071 if (!row->continued_p
12072 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12073 && row->x == 0)
12074 {
12075 this_line_buffer = XBUFFER (w->buffer);
12076
12077 CHARPOS (this_line_start_pos)
12078 = MATRIX_ROW_START_CHARPOS (row) + delta;
12079 BYTEPOS (this_line_start_pos)
12080 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12081
12082 CHARPOS (this_line_end_pos)
12083 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12084 BYTEPOS (this_line_end_pos)
12085 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12086
12087 this_line_y = w->cursor.y;
12088 this_line_pixel_height = row->height;
12089 this_line_vpos = w->cursor.vpos;
12090 this_line_start_x = row->x;
12091 }
12092 else
12093 CHARPOS (this_line_start_pos) = 0;
12094 }
12095
12096 return 1;
12097 }
12098
12099
12100 /* Run window scroll functions, if any, for WINDOW with new window
12101 start STARTP. Sets the window start of WINDOW to that position.
12102
12103 We assume that the window's buffer is really current. */
12104
12105 static INLINE struct text_pos
12106 run_window_scroll_functions (window, startp)
12107 Lisp_Object window;
12108 struct text_pos startp;
12109 {
12110 struct window *w = XWINDOW (window);
12111 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12112
12113 if (current_buffer != XBUFFER (w->buffer))
12114 abort ();
12115
12116 if (!NILP (Vwindow_scroll_functions))
12117 {
12118 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12119 make_number (CHARPOS (startp)));
12120 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12121 /* In case the hook functions switch buffers. */
12122 if (current_buffer != XBUFFER (w->buffer))
12123 set_buffer_internal_1 (XBUFFER (w->buffer));
12124 }
12125
12126 return startp;
12127 }
12128
12129
12130 /* Make sure the line containing the cursor is fully visible.
12131 A value of 1 means there is nothing to be done.
12132 (Either the line is fully visible, or it cannot be made so,
12133 or we cannot tell.)
12134
12135 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12136 is higher than window.
12137
12138 A value of 0 means the caller should do scrolling
12139 as if point had gone off the screen. */
12140
12141 static int
12142 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12143 struct window *w;
12144 int force_p;
12145 int current_matrix_p;
12146 {
12147 struct glyph_matrix *matrix;
12148 struct glyph_row *row;
12149 int window_height;
12150
12151 if (!make_cursor_line_fully_visible_p)
12152 return 1;
12153
12154 /* It's not always possible to find the cursor, e.g, when a window
12155 is full of overlay strings. Don't do anything in that case. */
12156 if (w->cursor.vpos < 0)
12157 return 1;
12158
12159 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12160 row = MATRIX_ROW (matrix, w->cursor.vpos);
12161
12162 /* If the cursor row is not partially visible, there's nothing to do. */
12163 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12164 return 1;
12165
12166 /* If the row the cursor is in is taller than the window's height,
12167 it's not clear what to do, so do nothing. */
12168 window_height = window_box_height (w);
12169 if (row->height >= window_height)
12170 {
12171 if (!force_p || MINI_WINDOW_P (w)
12172 || w->vscroll || w->cursor.vpos == 0)
12173 return 1;
12174 }
12175 return 0;
12176
12177 #if 0
12178 /* This code used to try to scroll the window just enough to make
12179 the line visible. It returned 0 to say that the caller should
12180 allocate larger glyph matrices. */
12181
12182 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12183 {
12184 int dy = row->height - row->visible_height;
12185 w->vscroll = 0;
12186 w->cursor.y += dy;
12187 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12188 }
12189 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12190 {
12191 int dy = - (row->height - row->visible_height);
12192 w->vscroll = dy;
12193 w->cursor.y += dy;
12194 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12195 }
12196
12197 /* When we change the cursor y-position of the selected window,
12198 change this_line_y as well so that the display optimization for
12199 the cursor line of the selected window in redisplay_internal uses
12200 the correct y-position. */
12201 if (w == XWINDOW (selected_window))
12202 this_line_y = w->cursor.y;
12203
12204 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12205 redisplay with larger matrices. */
12206 if (matrix->nrows < required_matrix_height (w))
12207 {
12208 fonts_changed_p = 1;
12209 return 0;
12210 }
12211
12212 return 1;
12213 #endif /* 0 */
12214 }
12215
12216
12217 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12218 non-zero means only WINDOW is redisplayed in redisplay_internal.
12219 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12220 in redisplay_window to bring a partially visible line into view in
12221 the case that only the cursor has moved.
12222
12223 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12224 last screen line's vertical height extends past the end of the screen.
12225
12226 Value is
12227
12228 1 if scrolling succeeded
12229
12230 0 if scrolling didn't find point.
12231
12232 -1 if new fonts have been loaded so that we must interrupt
12233 redisplay, adjust glyph matrices, and try again. */
12234
12235 enum
12236 {
12237 SCROLLING_SUCCESS,
12238 SCROLLING_FAILED,
12239 SCROLLING_NEED_LARGER_MATRICES
12240 };
12241
12242 static int
12243 try_scrolling (window, just_this_one_p, scroll_conservatively,
12244 scroll_step, temp_scroll_step, last_line_misfit)
12245 Lisp_Object window;
12246 int just_this_one_p;
12247 EMACS_INT scroll_conservatively, scroll_step;
12248 int temp_scroll_step;
12249 int last_line_misfit;
12250 {
12251 struct window *w = XWINDOW (window);
12252 struct frame *f = XFRAME (w->frame);
12253 struct text_pos scroll_margin_pos;
12254 struct text_pos pos;
12255 struct text_pos startp;
12256 struct it it;
12257 Lisp_Object window_end;
12258 int this_scroll_margin;
12259 int dy = 0;
12260 int scroll_max;
12261 int rc;
12262 int amount_to_scroll = 0;
12263 Lisp_Object aggressive;
12264 int height;
12265 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12266
12267 #if GLYPH_DEBUG
12268 debug_method_add (w, "try_scrolling");
12269 #endif
12270
12271 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12272
12273 /* Compute scroll margin height in pixels. We scroll when point is
12274 within this distance from the top or bottom of the window. */
12275 if (scroll_margin > 0)
12276 {
12277 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12278 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12279 }
12280 else
12281 this_scroll_margin = 0;
12282
12283 /* Force scroll_conservatively to have a reasonable value so it doesn't
12284 cause an overflow while computing how much to scroll. */
12285 if (scroll_conservatively)
12286 scroll_conservatively = min (scroll_conservatively,
12287 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12288
12289 /* Compute how much we should try to scroll maximally to bring point
12290 into view. */
12291 if (scroll_step || scroll_conservatively || temp_scroll_step)
12292 scroll_max = max (scroll_step,
12293 max (scroll_conservatively, temp_scroll_step));
12294 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12295 || NUMBERP (current_buffer->scroll_up_aggressively))
12296 /* We're trying to scroll because of aggressive scrolling
12297 but no scroll_step is set. Choose an arbitrary one. Maybe
12298 there should be a variable for this. */
12299 scroll_max = 10;
12300 else
12301 scroll_max = 0;
12302 scroll_max *= FRAME_LINE_HEIGHT (f);
12303
12304 /* Decide whether we have to scroll down. Start at the window end
12305 and move this_scroll_margin up to find the position of the scroll
12306 margin. */
12307 window_end = Fwindow_end (window, Qt);
12308
12309 too_near_end:
12310
12311 CHARPOS (scroll_margin_pos) = XINT (window_end);
12312 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12313
12314 if (this_scroll_margin || extra_scroll_margin_lines)
12315 {
12316 start_display (&it, w, scroll_margin_pos);
12317 if (this_scroll_margin)
12318 move_it_vertically_backward (&it, this_scroll_margin);
12319 if (extra_scroll_margin_lines)
12320 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12321 scroll_margin_pos = it.current.pos;
12322 }
12323
12324 if (PT >= CHARPOS (scroll_margin_pos))
12325 {
12326 int y0;
12327
12328 /* Point is in the scroll margin at the bottom of the window, or
12329 below. Compute a new window start that makes point visible. */
12330
12331 /* Compute the distance from the scroll margin to PT.
12332 Give up if the distance is greater than scroll_max. */
12333 start_display (&it, w, scroll_margin_pos);
12334 y0 = it.current_y;
12335 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12336 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12337
12338 /* To make point visible, we have to move the window start
12339 down so that the line the cursor is in is visible, which
12340 means we have to add in the height of the cursor line. */
12341 dy = line_bottom_y (&it) - y0;
12342
12343 if (dy > scroll_max)
12344 return SCROLLING_FAILED;
12345
12346 /* Move the window start down. If scrolling conservatively,
12347 move it just enough down to make point visible. If
12348 scroll_step is set, move it down by scroll_step. */
12349 start_display (&it, w, startp);
12350
12351 if (scroll_conservatively)
12352 /* Set AMOUNT_TO_SCROLL to at least one line,
12353 and at most scroll_conservatively lines. */
12354 amount_to_scroll
12355 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12356 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12357 else if (scroll_step || temp_scroll_step)
12358 amount_to_scroll = scroll_max;
12359 else
12360 {
12361 aggressive = current_buffer->scroll_up_aggressively;
12362 height = WINDOW_BOX_TEXT_HEIGHT (w);
12363 if (NUMBERP (aggressive))
12364 {
12365 double float_amount = XFLOATINT (aggressive) * height;
12366 amount_to_scroll = float_amount;
12367 if (amount_to_scroll == 0 && float_amount > 0)
12368 amount_to_scroll = 1;
12369 }
12370 }
12371
12372 if (amount_to_scroll <= 0)
12373 return SCROLLING_FAILED;
12374
12375 /* If moving by amount_to_scroll leaves STARTP unchanged,
12376 move it down one screen line. */
12377
12378 move_it_vertically (&it, amount_to_scroll);
12379 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12380 move_it_by_lines (&it, 1, 1);
12381 startp = it.current.pos;
12382 }
12383 else
12384 {
12385 /* See if point is inside the scroll margin at the top of the
12386 window. */
12387 scroll_margin_pos = startp;
12388 if (this_scroll_margin)
12389 {
12390 start_display (&it, w, startp);
12391 move_it_vertically (&it, this_scroll_margin);
12392 scroll_margin_pos = it.current.pos;
12393 }
12394
12395 if (PT < CHARPOS (scroll_margin_pos))
12396 {
12397 /* Point is in the scroll margin at the top of the window or
12398 above what is displayed in the window. */
12399 int y0;
12400
12401 /* Compute the vertical distance from PT to the scroll
12402 margin position. Give up if distance is greater than
12403 scroll_max. */
12404 SET_TEXT_POS (pos, PT, PT_BYTE);
12405 start_display (&it, w, pos);
12406 y0 = it.current_y;
12407 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12408 it.last_visible_y, -1,
12409 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12410 dy = it.current_y - y0;
12411 if (dy > scroll_max)
12412 return SCROLLING_FAILED;
12413
12414 /* Compute new window start. */
12415 start_display (&it, w, startp);
12416
12417 if (scroll_conservatively)
12418 amount_to_scroll
12419 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12420 else if (scroll_step || temp_scroll_step)
12421 amount_to_scroll = scroll_max;
12422 else
12423 {
12424 aggressive = current_buffer->scroll_down_aggressively;
12425 height = WINDOW_BOX_TEXT_HEIGHT (w);
12426 if (NUMBERP (aggressive))
12427 {
12428 double float_amount = XFLOATINT (aggressive) * height;
12429 amount_to_scroll = float_amount;
12430 if (amount_to_scroll == 0 && float_amount > 0)
12431 amount_to_scroll = 1;
12432 }
12433 }
12434
12435 if (amount_to_scroll <= 0)
12436 return SCROLLING_FAILED;
12437
12438 move_it_vertically_backward (&it, amount_to_scroll);
12439 startp = it.current.pos;
12440 }
12441 }
12442
12443 /* Run window scroll functions. */
12444 startp = run_window_scroll_functions (window, startp);
12445
12446 /* Display the window. Give up if new fonts are loaded, or if point
12447 doesn't appear. */
12448 if (!try_window (window, startp, 0))
12449 rc = SCROLLING_NEED_LARGER_MATRICES;
12450 else if (w->cursor.vpos < 0)
12451 {
12452 clear_glyph_matrix (w->desired_matrix);
12453 rc = SCROLLING_FAILED;
12454 }
12455 else
12456 {
12457 /* Maybe forget recorded base line for line number display. */
12458 if (!just_this_one_p
12459 || current_buffer->clip_changed
12460 || BEG_UNCHANGED < CHARPOS (startp))
12461 w->base_line_number = Qnil;
12462
12463 /* If cursor ends up on a partially visible line,
12464 treat that as being off the bottom of the screen. */
12465 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12466 {
12467 clear_glyph_matrix (w->desired_matrix);
12468 ++extra_scroll_margin_lines;
12469 goto too_near_end;
12470 }
12471 rc = SCROLLING_SUCCESS;
12472 }
12473
12474 return rc;
12475 }
12476
12477
12478 /* Compute a suitable window start for window W if display of W starts
12479 on a continuation line. Value is non-zero if a new window start
12480 was computed.
12481
12482 The new window start will be computed, based on W's width, starting
12483 from the start of the continued line. It is the start of the
12484 screen line with the minimum distance from the old start W->start. */
12485
12486 static int
12487 compute_window_start_on_continuation_line (w)
12488 struct window *w;
12489 {
12490 struct text_pos pos, start_pos;
12491 int window_start_changed_p = 0;
12492
12493 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12494
12495 /* If window start is on a continuation line... Window start may be
12496 < BEGV in case there's invisible text at the start of the
12497 buffer (M-x rmail, for example). */
12498 if (CHARPOS (start_pos) > BEGV
12499 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12500 {
12501 struct it it;
12502 struct glyph_row *row;
12503
12504 /* Handle the case that the window start is out of range. */
12505 if (CHARPOS (start_pos) < BEGV)
12506 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12507 else if (CHARPOS (start_pos) > ZV)
12508 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12509
12510 /* Find the start of the continued line. This should be fast
12511 because scan_buffer is fast (newline cache). */
12512 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12513 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12514 row, DEFAULT_FACE_ID);
12515 reseat_at_previous_visible_line_start (&it);
12516
12517 /* If the line start is "too far" away from the window start,
12518 say it takes too much time to compute a new window start. */
12519 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12520 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12521 {
12522 int min_distance, distance;
12523
12524 /* Move forward by display lines to find the new window
12525 start. If window width was enlarged, the new start can
12526 be expected to be > the old start. If window width was
12527 decreased, the new window start will be < the old start.
12528 So, we're looking for the display line start with the
12529 minimum distance from the old window start. */
12530 pos = it.current.pos;
12531 min_distance = INFINITY;
12532 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12533 distance < min_distance)
12534 {
12535 min_distance = distance;
12536 pos = it.current.pos;
12537 move_it_by_lines (&it, 1, 0);
12538 }
12539
12540 /* Set the window start there. */
12541 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12542 window_start_changed_p = 1;
12543 }
12544 }
12545
12546 return window_start_changed_p;
12547 }
12548
12549
12550 /* Try cursor movement in case text has not changed in window WINDOW,
12551 with window start STARTP. Value is
12552
12553 CURSOR_MOVEMENT_SUCCESS if successful
12554
12555 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12556
12557 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12558 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12559 we want to scroll as if scroll-step were set to 1. See the code.
12560
12561 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12562 which case we have to abort this redisplay, and adjust matrices
12563 first. */
12564
12565 enum
12566 {
12567 CURSOR_MOVEMENT_SUCCESS,
12568 CURSOR_MOVEMENT_CANNOT_BE_USED,
12569 CURSOR_MOVEMENT_MUST_SCROLL,
12570 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12571 };
12572
12573 static int
12574 try_cursor_movement (window, startp, scroll_step)
12575 Lisp_Object window;
12576 struct text_pos startp;
12577 int *scroll_step;
12578 {
12579 struct window *w = XWINDOW (window);
12580 struct frame *f = XFRAME (w->frame);
12581 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12582
12583 #if GLYPH_DEBUG
12584 if (inhibit_try_cursor_movement)
12585 return rc;
12586 #endif
12587
12588 /* Handle case where text has not changed, only point, and it has
12589 not moved off the frame. */
12590 if (/* Point may be in this window. */
12591 PT >= CHARPOS (startp)
12592 /* Selective display hasn't changed. */
12593 && !current_buffer->clip_changed
12594 /* Function force-mode-line-update is used to force a thorough
12595 redisplay. It sets either windows_or_buffers_changed or
12596 update_mode_lines. So don't take a shortcut here for these
12597 cases. */
12598 && !update_mode_lines
12599 && !windows_or_buffers_changed
12600 && !cursor_type_changed
12601 /* Can't use this case if highlighting a region. When a
12602 region exists, cursor movement has to do more than just
12603 set the cursor. */
12604 && !(!NILP (Vtransient_mark_mode)
12605 && !NILP (current_buffer->mark_active))
12606 && NILP (w->region_showing)
12607 && NILP (Vshow_trailing_whitespace)
12608 /* Right after splitting windows, last_point may be nil. */
12609 && INTEGERP (w->last_point)
12610 /* This code is not used for mini-buffer for the sake of the case
12611 of redisplaying to replace an echo area message; since in
12612 that case the mini-buffer contents per se are usually
12613 unchanged. This code is of no real use in the mini-buffer
12614 since the handling of this_line_start_pos, etc., in redisplay
12615 handles the same cases. */
12616 && !EQ (window, minibuf_window)
12617 /* When splitting windows or for new windows, it happens that
12618 redisplay is called with a nil window_end_vpos or one being
12619 larger than the window. This should really be fixed in
12620 window.c. I don't have this on my list, now, so we do
12621 approximately the same as the old redisplay code. --gerd. */
12622 && INTEGERP (w->window_end_vpos)
12623 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12624 && (FRAME_WINDOW_P (f)
12625 || !overlay_arrow_in_current_buffer_p ()))
12626 {
12627 int this_scroll_margin, top_scroll_margin;
12628 struct glyph_row *row = NULL;
12629
12630 #if GLYPH_DEBUG
12631 debug_method_add (w, "cursor movement");
12632 #endif
12633
12634 /* Scroll if point within this distance from the top or bottom
12635 of the window. This is a pixel value. */
12636 this_scroll_margin = max (0, scroll_margin);
12637 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12638 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12639
12640 top_scroll_margin = this_scroll_margin;
12641 if (WINDOW_WANTS_HEADER_LINE_P (w))
12642 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12643
12644 /* Start with the row the cursor was displayed during the last
12645 not paused redisplay. Give up if that row is not valid. */
12646 if (w->last_cursor.vpos < 0
12647 || w->last_cursor.vpos >= w->current_matrix->nrows)
12648 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12649 else
12650 {
12651 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12652 if (row->mode_line_p)
12653 ++row;
12654 if (!row->enabled_p)
12655 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12656 }
12657
12658 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12659 {
12660 int scroll_p = 0;
12661 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12662
12663 if (PT > XFASTINT (w->last_point))
12664 {
12665 /* Point has moved forward. */
12666 while (MATRIX_ROW_END_CHARPOS (row) < PT
12667 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12668 {
12669 xassert (row->enabled_p);
12670 ++row;
12671 }
12672
12673 /* The end position of a row equals the start position
12674 of the next row. If PT is there, we would rather
12675 display it in the next line. */
12676 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12677 && MATRIX_ROW_END_CHARPOS (row) == PT
12678 && !cursor_row_p (w, row))
12679 ++row;
12680
12681 /* If within the scroll margin, scroll. Note that
12682 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12683 the next line would be drawn, and that
12684 this_scroll_margin can be zero. */
12685 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12686 || PT > MATRIX_ROW_END_CHARPOS (row)
12687 /* Line is completely visible last line in window
12688 and PT is to be set in the next line. */
12689 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12690 && PT == MATRIX_ROW_END_CHARPOS (row)
12691 && !row->ends_at_zv_p
12692 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12693 scroll_p = 1;
12694 }
12695 else if (PT < XFASTINT (w->last_point))
12696 {
12697 /* Cursor has to be moved backward. Note that PT >=
12698 CHARPOS (startp) because of the outer if-statement. */
12699 while (!row->mode_line_p
12700 && (MATRIX_ROW_START_CHARPOS (row) > PT
12701 || (MATRIX_ROW_START_CHARPOS (row) == PT
12702 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12703 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12704 row > w->current_matrix->rows
12705 && (row-1)->ends_in_newline_from_string_p))))
12706 && (row->y > top_scroll_margin
12707 || CHARPOS (startp) == BEGV))
12708 {
12709 xassert (row->enabled_p);
12710 --row;
12711 }
12712
12713 /* Consider the following case: Window starts at BEGV,
12714 there is invisible, intangible text at BEGV, so that
12715 display starts at some point START > BEGV. It can
12716 happen that we are called with PT somewhere between
12717 BEGV and START. Try to handle that case. */
12718 if (row < w->current_matrix->rows
12719 || row->mode_line_p)
12720 {
12721 row = w->current_matrix->rows;
12722 if (row->mode_line_p)
12723 ++row;
12724 }
12725
12726 /* Due to newlines in overlay strings, we may have to
12727 skip forward over overlay strings. */
12728 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12729 && MATRIX_ROW_END_CHARPOS (row) == PT
12730 && !cursor_row_p (w, row))
12731 ++row;
12732
12733 /* If within the scroll margin, scroll. */
12734 if (row->y < top_scroll_margin
12735 && CHARPOS (startp) != BEGV)
12736 scroll_p = 1;
12737 }
12738 else
12739 {
12740 /* Cursor did not move. So don't scroll even if cursor line
12741 is partially visible, as it was so before. */
12742 rc = CURSOR_MOVEMENT_SUCCESS;
12743 }
12744
12745 if (PT < MATRIX_ROW_START_CHARPOS (row)
12746 || PT > MATRIX_ROW_END_CHARPOS (row))
12747 {
12748 /* if PT is not in the glyph row, give up. */
12749 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12750 }
12751 else if (rc != CURSOR_MOVEMENT_SUCCESS
12752 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12753 && make_cursor_line_fully_visible_p)
12754 {
12755 if (PT == MATRIX_ROW_END_CHARPOS (row)
12756 && !row->ends_at_zv_p
12757 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12758 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12759 else if (row->height > window_box_height (w))
12760 {
12761 /* If we end up in a partially visible line, let's
12762 make it fully visible, except when it's taller
12763 than the window, in which case we can't do much
12764 about it. */
12765 *scroll_step = 1;
12766 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12767 }
12768 else
12769 {
12770 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12771 if (!cursor_row_fully_visible_p (w, 0, 1))
12772 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12773 else
12774 rc = CURSOR_MOVEMENT_SUCCESS;
12775 }
12776 }
12777 else if (scroll_p)
12778 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12779 else
12780 {
12781 do
12782 {
12783 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12784 {
12785 rc = CURSOR_MOVEMENT_SUCCESS;
12786 break;
12787 }
12788 ++row;
12789 }
12790 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12791 && MATRIX_ROW_START_CHARPOS (row) == PT
12792 && cursor_row_p (w, row));
12793 }
12794 }
12795 }
12796
12797 return rc;
12798 }
12799
12800 void
12801 set_vertical_scroll_bar (w)
12802 struct window *w;
12803 {
12804 int start, end, whole;
12805
12806 /* Calculate the start and end positions for the current window.
12807 At some point, it would be nice to choose between scrollbars
12808 which reflect the whole buffer size, with special markers
12809 indicating narrowing, and scrollbars which reflect only the
12810 visible region.
12811
12812 Note that mini-buffers sometimes aren't displaying any text. */
12813 if (!MINI_WINDOW_P (w)
12814 || (w == XWINDOW (minibuf_window)
12815 && NILP (echo_area_buffer[0])))
12816 {
12817 struct buffer *buf = XBUFFER (w->buffer);
12818 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12819 start = marker_position (w->start) - BUF_BEGV (buf);
12820 /* I don't think this is guaranteed to be right. For the
12821 moment, we'll pretend it is. */
12822 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12823
12824 if (end < start)
12825 end = start;
12826 if (whole < (end - start))
12827 whole = end - start;
12828 }
12829 else
12830 start = end = whole = 0;
12831
12832 /* Indicate what this scroll bar ought to be displaying now. */
12833 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12834 }
12835
12836
12837 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12838 selected_window is redisplayed.
12839
12840 We can return without actually redisplaying the window if
12841 fonts_changed_p is nonzero. In that case, redisplay_internal will
12842 retry. */
12843
12844 static void
12845 redisplay_window (window, just_this_one_p)
12846 Lisp_Object window;
12847 int just_this_one_p;
12848 {
12849 struct window *w = XWINDOW (window);
12850 struct frame *f = XFRAME (w->frame);
12851 struct buffer *buffer = XBUFFER (w->buffer);
12852 struct buffer *old = current_buffer;
12853 struct text_pos lpoint, opoint, startp;
12854 int update_mode_line;
12855 int tem;
12856 struct it it;
12857 /* Record it now because it's overwritten. */
12858 int current_matrix_up_to_date_p = 0;
12859 int used_current_matrix_p = 0;
12860 /* This is less strict than current_matrix_up_to_date_p.
12861 It indictes that the buffer contents and narrowing are unchanged. */
12862 int buffer_unchanged_p = 0;
12863 int temp_scroll_step = 0;
12864 int count = SPECPDL_INDEX ();
12865 int rc;
12866 int centering_position = -1;
12867 int last_line_misfit = 0;
12868 int save_beg_unchanged, save_end_unchanged;
12869
12870 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12871 opoint = lpoint;
12872
12873 /* W must be a leaf window here. */
12874 xassert (!NILP (w->buffer));
12875 #if GLYPH_DEBUG
12876 *w->desired_matrix->method = 0;
12877 #endif
12878
12879 specbind (Qinhibit_point_motion_hooks, Qt);
12880
12881 reconsider_clip_changes (w, buffer);
12882
12883 /* Has the mode line to be updated? */
12884 update_mode_line = (!NILP (w->update_mode_line)
12885 || update_mode_lines
12886 || buffer->clip_changed
12887 || buffer->prevent_redisplay_optimizations_p);
12888
12889 if (MINI_WINDOW_P (w))
12890 {
12891 if (w == XWINDOW (echo_area_window)
12892 && !NILP (echo_area_buffer[0]))
12893 {
12894 if (update_mode_line)
12895 /* We may have to update a tty frame's menu bar or a
12896 tool-bar. Example `M-x C-h C-h C-g'. */
12897 goto finish_menu_bars;
12898 else
12899 /* We've already displayed the echo area glyphs in this window. */
12900 goto finish_scroll_bars;
12901 }
12902 else if ((w != XWINDOW (minibuf_window)
12903 || minibuf_level == 0)
12904 /* When buffer is nonempty, redisplay window normally. */
12905 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12906 /* Quail displays non-mini buffers in minibuffer window.
12907 In that case, redisplay the window normally. */
12908 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12909 {
12910 /* W is a mini-buffer window, but it's not active, so clear
12911 it. */
12912 int yb = window_text_bottom_y (w);
12913 struct glyph_row *row;
12914 int y;
12915
12916 for (y = 0, row = w->desired_matrix->rows;
12917 y < yb;
12918 y += row->height, ++row)
12919 blank_row (w, row, y);
12920 goto finish_scroll_bars;
12921 }
12922
12923 clear_glyph_matrix (w->desired_matrix);
12924 }
12925
12926 /* Otherwise set up data on this window; select its buffer and point
12927 value. */
12928 /* Really select the buffer, for the sake of buffer-local
12929 variables. */
12930 set_buffer_internal_1 (XBUFFER (w->buffer));
12931 SET_TEXT_POS (opoint, PT, PT_BYTE);
12932
12933 save_beg_unchanged = BEG_UNCHANGED;
12934 save_end_unchanged = END_UNCHANGED;
12935
12936 current_matrix_up_to_date_p
12937 = (!NILP (w->window_end_valid)
12938 && !current_buffer->clip_changed
12939 && !current_buffer->prevent_redisplay_optimizations_p
12940 && XFASTINT (w->last_modified) >= MODIFF
12941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12942
12943 buffer_unchanged_p
12944 = (!NILP (w->window_end_valid)
12945 && !current_buffer->clip_changed
12946 && XFASTINT (w->last_modified) >= MODIFF
12947 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12948
12949 /* When windows_or_buffers_changed is non-zero, we can't rely on
12950 the window end being valid, so set it to nil there. */
12951 if (windows_or_buffers_changed)
12952 {
12953 /* If window starts on a continuation line, maybe adjust the
12954 window start in case the window's width changed. */
12955 if (XMARKER (w->start)->buffer == current_buffer)
12956 compute_window_start_on_continuation_line (w);
12957
12958 w->window_end_valid = Qnil;
12959 }
12960
12961 /* Some sanity checks. */
12962 CHECK_WINDOW_END (w);
12963 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12964 abort ();
12965 if (BYTEPOS (opoint) < CHARPOS (opoint))
12966 abort ();
12967
12968 /* If %c is in mode line, update it if needed. */
12969 if (!NILP (w->column_number_displayed)
12970 /* This alternative quickly identifies a common case
12971 where no change is needed. */
12972 && !(PT == XFASTINT (w->last_point)
12973 && XFASTINT (w->last_modified) >= MODIFF
12974 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12975 && (XFASTINT (w->column_number_displayed)
12976 != (int) current_column ())) /* iftc */
12977 update_mode_line = 1;
12978
12979 /* Count number of windows showing the selected buffer. An indirect
12980 buffer counts as its base buffer. */
12981 if (!just_this_one_p)
12982 {
12983 struct buffer *current_base, *window_base;
12984 current_base = current_buffer;
12985 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12986 if (current_base->base_buffer)
12987 current_base = current_base->base_buffer;
12988 if (window_base->base_buffer)
12989 window_base = window_base->base_buffer;
12990 if (current_base == window_base)
12991 buffer_shared++;
12992 }
12993
12994 /* Point refers normally to the selected window. For any other
12995 window, set up appropriate value. */
12996 if (!EQ (window, selected_window))
12997 {
12998 int new_pt = XMARKER (w->pointm)->charpos;
12999 int new_pt_byte = marker_byte_position (w->pointm);
13000 if (new_pt < BEGV)
13001 {
13002 new_pt = BEGV;
13003 new_pt_byte = BEGV_BYTE;
13004 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13005 }
13006 else if (new_pt > (ZV - 1))
13007 {
13008 new_pt = ZV;
13009 new_pt_byte = ZV_BYTE;
13010 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13011 }
13012
13013 /* We don't use SET_PT so that the point-motion hooks don't run. */
13014 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13015 }
13016
13017 /* If any of the character widths specified in the display table
13018 have changed, invalidate the width run cache. It's true that
13019 this may be a bit late to catch such changes, but the rest of
13020 redisplay goes (non-fatally) haywire when the display table is
13021 changed, so why should we worry about doing any better? */
13022 if (current_buffer->width_run_cache)
13023 {
13024 struct Lisp_Char_Table *disptab = buffer_display_table ();
13025
13026 if (! disptab_matches_widthtab (disptab,
13027 XVECTOR (current_buffer->width_table)))
13028 {
13029 invalidate_region_cache (current_buffer,
13030 current_buffer->width_run_cache,
13031 BEG, Z);
13032 recompute_width_table (current_buffer, disptab);
13033 }
13034 }
13035
13036 /* If window-start is screwed up, choose a new one. */
13037 if (XMARKER (w->start)->buffer != current_buffer)
13038 goto recenter;
13039
13040 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13041
13042 /* If someone specified a new starting point but did not insist,
13043 check whether it can be used. */
13044 if (!NILP (w->optional_new_start)
13045 && CHARPOS (startp) >= BEGV
13046 && CHARPOS (startp) <= ZV)
13047 {
13048 w->optional_new_start = Qnil;
13049 start_display (&it, w, startp);
13050 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13051 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13052 if (IT_CHARPOS (it) == PT)
13053 w->force_start = Qt;
13054 /* IT may overshoot PT if text at PT is invisible. */
13055 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13056 w->force_start = Qt;
13057 }
13058
13059 force_start:
13060
13061 /* Handle case where place to start displaying has been specified,
13062 unless the specified location is outside the accessible range. */
13063 if (!NILP (w->force_start)
13064 || w->frozen_window_start_p)
13065 {
13066 /* We set this later on if we have to adjust point. */
13067 int new_vpos = -1;
13068 int val;
13069
13070 w->force_start = Qnil;
13071 w->vscroll = 0;
13072 w->window_end_valid = Qnil;
13073
13074 /* Forget any recorded base line for line number display. */
13075 if (!buffer_unchanged_p)
13076 w->base_line_number = Qnil;
13077
13078 /* Redisplay the mode line. Select the buffer properly for that.
13079 Also, run the hook window-scroll-functions
13080 because we have scrolled. */
13081 /* Note, we do this after clearing force_start because
13082 if there's an error, it is better to forget about force_start
13083 than to get into an infinite loop calling the hook functions
13084 and having them get more errors. */
13085 if (!update_mode_line
13086 || ! NILP (Vwindow_scroll_functions))
13087 {
13088 update_mode_line = 1;
13089 w->update_mode_line = Qt;
13090 startp = run_window_scroll_functions (window, startp);
13091 }
13092
13093 w->last_modified = make_number (0);
13094 w->last_overlay_modified = make_number (0);
13095 if (CHARPOS (startp) < BEGV)
13096 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13097 else if (CHARPOS (startp) > ZV)
13098 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13099
13100 /* Redisplay, then check if cursor has been set during the
13101 redisplay. Give up if new fonts were loaded. */
13102 val = try_window (window, startp, 1);
13103 if (!val)
13104 {
13105 w->force_start = Qt;
13106 clear_glyph_matrix (w->desired_matrix);
13107 goto need_larger_matrices;
13108 }
13109 /* Point was outside the scroll margins. */
13110 if (val < 0)
13111 new_vpos = window_box_height (w) / 2;
13112
13113 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13114 {
13115 /* If point does not appear, try to move point so it does
13116 appear. The desired matrix has been built above, so we
13117 can use it here. */
13118 new_vpos = window_box_height (w) / 2;
13119 }
13120
13121 if (!cursor_row_fully_visible_p (w, 0, 0))
13122 {
13123 /* Point does appear, but on a line partly visible at end of window.
13124 Move it back to a fully-visible line. */
13125 new_vpos = window_box_height (w);
13126 }
13127
13128 /* If we need to move point for either of the above reasons,
13129 now actually do it. */
13130 if (new_vpos >= 0)
13131 {
13132 struct glyph_row *row;
13133
13134 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13135 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13136 ++row;
13137
13138 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13139 MATRIX_ROW_START_BYTEPOS (row));
13140
13141 if (w != XWINDOW (selected_window))
13142 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13143 else if (current_buffer == old)
13144 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13145
13146 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13147
13148 /* If we are highlighting the region, then we just changed
13149 the region, so redisplay to show it. */
13150 if (!NILP (Vtransient_mark_mode)
13151 && !NILP (current_buffer->mark_active))
13152 {
13153 clear_glyph_matrix (w->desired_matrix);
13154 if (!try_window (window, startp, 0))
13155 goto need_larger_matrices;
13156 }
13157 }
13158
13159 #if GLYPH_DEBUG
13160 debug_method_add (w, "forced window start");
13161 #endif
13162 goto done;
13163 }
13164
13165 /* Handle case where text has not changed, only point, and it has
13166 not moved off the frame, and we are not retrying after hscroll.
13167 (current_matrix_up_to_date_p is nonzero when retrying.) */
13168 if (current_matrix_up_to_date_p
13169 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13170 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13171 {
13172 switch (rc)
13173 {
13174 case CURSOR_MOVEMENT_SUCCESS:
13175 used_current_matrix_p = 1;
13176 goto done;
13177
13178 #if 0 /* try_cursor_movement never returns this value. */
13179 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13180 goto need_larger_matrices;
13181 #endif
13182
13183 case CURSOR_MOVEMENT_MUST_SCROLL:
13184 goto try_to_scroll;
13185
13186 default:
13187 abort ();
13188 }
13189 }
13190 /* If current starting point was originally the beginning of a line
13191 but no longer is, find a new starting point. */
13192 else if (!NILP (w->start_at_line_beg)
13193 && !(CHARPOS (startp) <= BEGV
13194 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13195 {
13196 #if GLYPH_DEBUG
13197 debug_method_add (w, "recenter 1");
13198 #endif
13199 goto recenter;
13200 }
13201
13202 /* Try scrolling with try_window_id. Value is > 0 if update has
13203 been done, it is -1 if we know that the same window start will
13204 not work. It is 0 if unsuccessful for some other reason. */
13205 else if ((tem = try_window_id (w)) != 0)
13206 {
13207 #if GLYPH_DEBUG
13208 debug_method_add (w, "try_window_id %d", tem);
13209 #endif
13210
13211 if (fonts_changed_p)
13212 goto need_larger_matrices;
13213 if (tem > 0)
13214 goto done;
13215
13216 /* Otherwise try_window_id has returned -1 which means that we
13217 don't want the alternative below this comment to execute. */
13218 }
13219 else if (CHARPOS (startp) >= BEGV
13220 && CHARPOS (startp) <= ZV
13221 && PT >= CHARPOS (startp)
13222 && (CHARPOS (startp) < ZV
13223 /* Avoid starting at end of buffer. */
13224 || CHARPOS (startp) == BEGV
13225 || (XFASTINT (w->last_modified) >= MODIFF
13226 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13227 {
13228
13229 /* If first window line is a continuation line, and window start
13230 is inside the modified region, but the first change is before
13231 current window start, we must select a new window start.
13232
13233 However, if this is the result of a down-mouse event (e.g. by
13234 extending the mouse-drag-overlay), we don't want to select a
13235 new window start, since that would change the position under
13236 the mouse, resulting in an unwanted mouse-movement rather
13237 than a simple mouse-click. */
13238 if (NILP (w->start_at_line_beg)
13239 && NILP (do_mouse_tracking)
13240 && CHARPOS (startp) > BEGV
13241 && CHARPOS (startp) > BEG + save_beg_unchanged
13242 && CHARPOS (startp) <= Z - save_end_unchanged)
13243 {
13244 w->force_start = Qt;
13245 if (XMARKER (w->start)->buffer == current_buffer)
13246 compute_window_start_on_continuation_line (w);
13247 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13248 goto force_start;
13249 }
13250
13251 #if GLYPH_DEBUG
13252 debug_method_add (w, "same window start");
13253 #endif
13254
13255 /* Try to redisplay starting at same place as before.
13256 If point has not moved off frame, accept the results. */
13257 if (!current_matrix_up_to_date_p
13258 /* Don't use try_window_reusing_current_matrix in this case
13259 because a window scroll function can have changed the
13260 buffer. */
13261 || !NILP (Vwindow_scroll_functions)
13262 || MINI_WINDOW_P (w)
13263 || !(used_current_matrix_p
13264 = try_window_reusing_current_matrix (w)))
13265 {
13266 IF_DEBUG (debug_method_add (w, "1"));
13267 if (try_window (window, startp, 1) < 0)
13268 /* -1 means we need to scroll.
13269 0 means we need new matrices, but fonts_changed_p
13270 is set in that case, so we will detect it below. */
13271 goto try_to_scroll;
13272 }
13273
13274 if (fonts_changed_p)
13275 goto need_larger_matrices;
13276
13277 if (w->cursor.vpos >= 0)
13278 {
13279 if (!just_this_one_p
13280 || current_buffer->clip_changed
13281 || BEG_UNCHANGED < CHARPOS (startp))
13282 /* Forget any recorded base line for line number display. */
13283 w->base_line_number = Qnil;
13284
13285 if (!cursor_row_fully_visible_p (w, 1, 0))
13286 {
13287 clear_glyph_matrix (w->desired_matrix);
13288 last_line_misfit = 1;
13289 }
13290 /* Drop through and scroll. */
13291 else
13292 goto done;
13293 }
13294 else
13295 clear_glyph_matrix (w->desired_matrix);
13296 }
13297
13298 try_to_scroll:
13299
13300 w->last_modified = make_number (0);
13301 w->last_overlay_modified = make_number (0);
13302
13303 /* Redisplay the mode line. Select the buffer properly for that. */
13304 if (!update_mode_line)
13305 {
13306 update_mode_line = 1;
13307 w->update_mode_line = Qt;
13308 }
13309
13310 /* Try to scroll by specified few lines. */
13311 if ((scroll_conservatively
13312 || scroll_step
13313 || temp_scroll_step
13314 || NUMBERP (current_buffer->scroll_up_aggressively)
13315 || NUMBERP (current_buffer->scroll_down_aggressively))
13316 && !current_buffer->clip_changed
13317 && CHARPOS (startp) >= BEGV
13318 && CHARPOS (startp) <= ZV)
13319 {
13320 /* The function returns -1 if new fonts were loaded, 1 if
13321 successful, 0 if not successful. */
13322 int rc = try_scrolling (window, just_this_one_p,
13323 scroll_conservatively,
13324 scroll_step,
13325 temp_scroll_step, last_line_misfit);
13326 switch (rc)
13327 {
13328 case SCROLLING_SUCCESS:
13329 goto done;
13330
13331 case SCROLLING_NEED_LARGER_MATRICES:
13332 goto need_larger_matrices;
13333
13334 case SCROLLING_FAILED:
13335 break;
13336
13337 default:
13338 abort ();
13339 }
13340 }
13341
13342 /* Finally, just choose place to start which centers point */
13343
13344 recenter:
13345 if (centering_position < 0)
13346 centering_position = window_box_height (w) / 2;
13347
13348 #if GLYPH_DEBUG
13349 debug_method_add (w, "recenter");
13350 #endif
13351
13352 /* w->vscroll = 0; */
13353
13354 /* Forget any previously recorded base line for line number display. */
13355 if (!buffer_unchanged_p)
13356 w->base_line_number = Qnil;
13357
13358 /* Move backward half the height of the window. */
13359 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13360 it.current_y = it.last_visible_y;
13361 move_it_vertically_backward (&it, centering_position);
13362 xassert (IT_CHARPOS (it) >= BEGV);
13363
13364 /* The function move_it_vertically_backward may move over more
13365 than the specified y-distance. If it->w is small, e.g. a
13366 mini-buffer window, we may end up in front of the window's
13367 display area. Start displaying at the start of the line
13368 containing PT in this case. */
13369 if (it.current_y <= 0)
13370 {
13371 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13372 move_it_vertically_backward (&it, 0);
13373 #if 0
13374 /* I think this assert is bogus if buffer contains
13375 invisible text or images. KFS. */
13376 xassert (IT_CHARPOS (it) <= PT);
13377 #endif
13378 it.current_y = 0;
13379 }
13380
13381 it.current_x = it.hpos = 0;
13382
13383 /* Set startp here explicitly in case that helps avoid an infinite loop
13384 in case the window-scroll-functions functions get errors. */
13385 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13386
13387 /* Run scroll hooks. */
13388 startp = run_window_scroll_functions (window, it.current.pos);
13389
13390 /* Redisplay the window. */
13391 if (!current_matrix_up_to_date_p
13392 || windows_or_buffers_changed
13393 || cursor_type_changed
13394 /* Don't use try_window_reusing_current_matrix in this case
13395 because it can have changed the buffer. */
13396 || !NILP (Vwindow_scroll_functions)
13397 || !just_this_one_p
13398 || MINI_WINDOW_P (w)
13399 || !(used_current_matrix_p
13400 = try_window_reusing_current_matrix (w)))
13401 try_window (window, startp, 0);
13402
13403 /* If new fonts have been loaded (due to fontsets), give up. We
13404 have to start a new redisplay since we need to re-adjust glyph
13405 matrices. */
13406 if (fonts_changed_p)
13407 goto need_larger_matrices;
13408
13409 /* If cursor did not appear assume that the middle of the window is
13410 in the first line of the window. Do it again with the next line.
13411 (Imagine a window of height 100, displaying two lines of height
13412 60. Moving back 50 from it->last_visible_y will end in the first
13413 line.) */
13414 if (w->cursor.vpos < 0)
13415 {
13416 if (!NILP (w->window_end_valid)
13417 && PT >= Z - XFASTINT (w->window_end_pos))
13418 {
13419 clear_glyph_matrix (w->desired_matrix);
13420 move_it_by_lines (&it, 1, 0);
13421 try_window (window, it.current.pos, 0);
13422 }
13423 else if (PT < IT_CHARPOS (it))
13424 {
13425 clear_glyph_matrix (w->desired_matrix);
13426 move_it_by_lines (&it, -1, 0);
13427 try_window (window, it.current.pos, 0);
13428 }
13429 else
13430 {
13431 /* Not much we can do about it. */
13432 }
13433 }
13434
13435 /* Consider the following case: Window starts at BEGV, there is
13436 invisible, intangible text at BEGV, so that display starts at
13437 some point START > BEGV. It can happen that we are called with
13438 PT somewhere between BEGV and START. Try to handle that case. */
13439 if (w->cursor.vpos < 0)
13440 {
13441 struct glyph_row *row = w->current_matrix->rows;
13442 if (row->mode_line_p)
13443 ++row;
13444 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13445 }
13446
13447 if (!cursor_row_fully_visible_p (w, 0, 0))
13448 {
13449 /* If vscroll is enabled, disable it and try again. */
13450 if (w->vscroll)
13451 {
13452 w->vscroll = 0;
13453 clear_glyph_matrix (w->desired_matrix);
13454 goto recenter;
13455 }
13456
13457 /* If centering point failed to make the whole line visible,
13458 put point at the top instead. That has to make the whole line
13459 visible, if it can be done. */
13460 if (centering_position == 0)
13461 goto done;
13462
13463 clear_glyph_matrix (w->desired_matrix);
13464 centering_position = 0;
13465 goto recenter;
13466 }
13467
13468 done:
13469
13470 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13471 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13472 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13473 ? Qt : Qnil);
13474
13475 /* Display the mode line, if we must. */
13476 if ((update_mode_line
13477 /* If window not full width, must redo its mode line
13478 if (a) the window to its side is being redone and
13479 (b) we do a frame-based redisplay. This is a consequence
13480 of how inverted lines are drawn in frame-based redisplay. */
13481 || (!just_this_one_p
13482 && !FRAME_WINDOW_P (f)
13483 && !WINDOW_FULL_WIDTH_P (w))
13484 /* Line number to display. */
13485 || INTEGERP (w->base_line_pos)
13486 /* Column number is displayed and different from the one displayed. */
13487 || (!NILP (w->column_number_displayed)
13488 && (XFASTINT (w->column_number_displayed)
13489 != (int) current_column ()))) /* iftc */
13490 /* This means that the window has a mode line. */
13491 && (WINDOW_WANTS_MODELINE_P (w)
13492 || WINDOW_WANTS_HEADER_LINE_P (w)))
13493 {
13494 display_mode_lines (w);
13495
13496 /* If mode line height has changed, arrange for a thorough
13497 immediate redisplay using the correct mode line height. */
13498 if (WINDOW_WANTS_MODELINE_P (w)
13499 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13500 {
13501 fonts_changed_p = 1;
13502 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13503 = DESIRED_MODE_LINE_HEIGHT (w);
13504 }
13505
13506 /* If top line height has changed, arrange for a thorough
13507 immediate redisplay using the correct mode line height. */
13508 if (WINDOW_WANTS_HEADER_LINE_P (w)
13509 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13510 {
13511 fonts_changed_p = 1;
13512 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13513 = DESIRED_HEADER_LINE_HEIGHT (w);
13514 }
13515
13516 if (fonts_changed_p)
13517 goto need_larger_matrices;
13518 }
13519
13520 if (!line_number_displayed
13521 && !BUFFERP (w->base_line_pos))
13522 {
13523 w->base_line_pos = Qnil;
13524 w->base_line_number = Qnil;
13525 }
13526
13527 finish_menu_bars:
13528
13529 /* When we reach a frame's selected window, redo the frame's menu bar. */
13530 if (update_mode_line
13531 && EQ (FRAME_SELECTED_WINDOW (f), window))
13532 {
13533 int redisplay_menu_p = 0;
13534 int redisplay_tool_bar_p = 0;
13535
13536 if (FRAME_WINDOW_P (f))
13537 {
13538 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13539 || defined (USE_GTK)
13540 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13541 #else
13542 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13543 #endif
13544 }
13545 else
13546 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13547
13548 if (redisplay_menu_p)
13549 display_menu_bar (w);
13550
13551 #ifdef HAVE_WINDOW_SYSTEM
13552 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13553 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13554 #else
13555 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13556 && (FRAME_TOOL_BAR_LINES (f) > 0
13557 || !NILP (Vauto_resize_tool_bars));
13558
13559 #endif
13560
13561 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13562 {
13563 extern int ignore_mouse_drag_p;
13564 ignore_mouse_drag_p = 1;
13565 }
13566 #endif
13567 }
13568
13569 #ifdef HAVE_WINDOW_SYSTEM
13570 if (FRAME_WINDOW_P (f)
13571 && update_window_fringes (w, (just_this_one_p
13572 || (!used_current_matrix_p && !overlay_arrow_seen)
13573 || w->pseudo_window_p)))
13574 {
13575 update_begin (f);
13576 BLOCK_INPUT;
13577 if (draw_window_fringes (w, 1))
13578 x_draw_vertical_border (w);
13579 UNBLOCK_INPUT;
13580 update_end (f);
13581 }
13582 #endif /* HAVE_WINDOW_SYSTEM */
13583
13584 /* We go to this label, with fonts_changed_p nonzero,
13585 if it is necessary to try again using larger glyph matrices.
13586 We have to redeem the scroll bar even in this case,
13587 because the loop in redisplay_internal expects that. */
13588 need_larger_matrices:
13589 ;
13590 finish_scroll_bars:
13591
13592 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13593 {
13594 /* Set the thumb's position and size. */
13595 set_vertical_scroll_bar (w);
13596
13597 /* Note that we actually used the scroll bar attached to this
13598 window, so it shouldn't be deleted at the end of redisplay. */
13599 redeem_scroll_bar_hook (w);
13600 }
13601
13602 /* Restore current_buffer and value of point in it. */
13603 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13604 set_buffer_internal_1 (old);
13605 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13606 shorter. This can be caused by log truncation in *Messages*. */
13607 if (CHARPOS (lpoint) <= ZV)
13608 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13609
13610 unbind_to (count, Qnil);
13611 }
13612
13613
13614 /* Build the complete desired matrix of WINDOW with a window start
13615 buffer position POS.
13616
13617 Value is 1 if successful. It is zero if fonts were loaded during
13618 redisplay which makes re-adjusting glyph matrices necessary, and -1
13619 if point would appear in the scroll margins.
13620 (We check that only if CHECK_MARGINS is nonzero. */
13621
13622 int
13623 try_window (window, pos, check_margins)
13624 Lisp_Object window;
13625 struct text_pos pos;
13626 int check_margins;
13627 {
13628 struct window *w = XWINDOW (window);
13629 struct it it;
13630 struct glyph_row *last_text_row = NULL;
13631 struct frame *f = XFRAME (w->frame);
13632
13633 /* Make POS the new window start. */
13634 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13635
13636 /* Mark cursor position as unknown. No overlay arrow seen. */
13637 w->cursor.vpos = -1;
13638 overlay_arrow_seen = 0;
13639
13640 /* Initialize iterator and info to start at POS. */
13641 start_display (&it, w, pos);
13642
13643 /* Display all lines of W. */
13644 while (it.current_y < it.last_visible_y)
13645 {
13646 if (display_line (&it))
13647 last_text_row = it.glyph_row - 1;
13648 if (fonts_changed_p)
13649 return 0;
13650 }
13651
13652 /* Don't let the cursor end in the scroll margins. */
13653 if (check_margins
13654 && !MINI_WINDOW_P (w))
13655 {
13656 int this_scroll_margin;
13657
13658 this_scroll_margin = max (0, scroll_margin);
13659 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13660 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13661
13662 if ((w->cursor.y >= 0 /* not vscrolled */
13663 && w->cursor.y < this_scroll_margin
13664 && CHARPOS (pos) > BEGV
13665 && IT_CHARPOS (it) < ZV)
13666 /* rms: considering make_cursor_line_fully_visible_p here
13667 seems to give wrong results. We don't want to recenter
13668 when the last line is partly visible, we want to allow
13669 that case to be handled in the usual way. */
13670 || (w->cursor.y + 1) > it.last_visible_y)
13671 {
13672 w->cursor.vpos = -1;
13673 clear_glyph_matrix (w->desired_matrix);
13674 return -1;
13675 }
13676 }
13677
13678 /* If bottom moved off end of frame, change mode line percentage. */
13679 if (XFASTINT (w->window_end_pos) <= 0
13680 && Z != IT_CHARPOS (it))
13681 w->update_mode_line = Qt;
13682
13683 /* Set window_end_pos to the offset of the last character displayed
13684 on the window from the end of current_buffer. Set
13685 window_end_vpos to its row number. */
13686 if (last_text_row)
13687 {
13688 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13689 w->window_end_bytepos
13690 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13691 w->window_end_pos
13692 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13693 w->window_end_vpos
13694 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13695 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13696 ->displays_text_p);
13697 }
13698 else
13699 {
13700 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13701 w->window_end_pos = make_number (Z - ZV);
13702 w->window_end_vpos = make_number (0);
13703 }
13704
13705 /* But that is not valid info until redisplay finishes. */
13706 w->window_end_valid = Qnil;
13707 return 1;
13708 }
13709
13710
13711 \f
13712 /************************************************************************
13713 Window redisplay reusing current matrix when buffer has not changed
13714 ************************************************************************/
13715
13716 /* Try redisplay of window W showing an unchanged buffer with a
13717 different window start than the last time it was displayed by
13718 reusing its current matrix. Value is non-zero if successful.
13719 W->start is the new window start. */
13720
13721 static int
13722 try_window_reusing_current_matrix (w)
13723 struct window *w;
13724 {
13725 struct frame *f = XFRAME (w->frame);
13726 struct glyph_row *row, *bottom_row;
13727 struct it it;
13728 struct run run;
13729 struct text_pos start, new_start;
13730 int nrows_scrolled, i;
13731 struct glyph_row *last_text_row;
13732 struct glyph_row *last_reused_text_row;
13733 struct glyph_row *start_row;
13734 int start_vpos, min_y, max_y;
13735
13736 #if GLYPH_DEBUG
13737 if (inhibit_try_window_reusing)
13738 return 0;
13739 #endif
13740
13741 if (/* This function doesn't handle terminal frames. */
13742 !FRAME_WINDOW_P (f)
13743 /* Don't try to reuse the display if windows have been split
13744 or such. */
13745 || windows_or_buffers_changed
13746 || cursor_type_changed)
13747 return 0;
13748
13749 /* Can't do this if region may have changed. */
13750 if ((!NILP (Vtransient_mark_mode)
13751 && !NILP (current_buffer->mark_active))
13752 || !NILP (w->region_showing)
13753 || !NILP (Vshow_trailing_whitespace))
13754 return 0;
13755
13756 /* If top-line visibility has changed, give up. */
13757 if (WINDOW_WANTS_HEADER_LINE_P (w)
13758 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13759 return 0;
13760
13761 /* Give up if old or new display is scrolled vertically. We could
13762 make this function handle this, but right now it doesn't. */
13763 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13764 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13765 return 0;
13766
13767 /* The variable new_start now holds the new window start. The old
13768 start `start' can be determined from the current matrix. */
13769 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13770 start = start_row->start.pos;
13771 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13772
13773 /* Clear the desired matrix for the display below. */
13774 clear_glyph_matrix (w->desired_matrix);
13775
13776 if (CHARPOS (new_start) <= CHARPOS (start))
13777 {
13778 int first_row_y;
13779
13780 /* Don't use this method if the display starts with an ellipsis
13781 displayed for invisible text. It's not easy to handle that case
13782 below, and it's certainly not worth the effort since this is
13783 not a frequent case. */
13784 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13785 return 0;
13786
13787 IF_DEBUG (debug_method_add (w, "twu1"));
13788
13789 /* Display up to a row that can be reused. The variable
13790 last_text_row is set to the last row displayed that displays
13791 text. Note that it.vpos == 0 if or if not there is a
13792 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13793 start_display (&it, w, new_start);
13794 first_row_y = it.current_y;
13795 w->cursor.vpos = -1;
13796 last_text_row = last_reused_text_row = NULL;
13797
13798 while (it.current_y < it.last_visible_y
13799 && !fonts_changed_p)
13800 {
13801 /* If we have reached into the characters in the START row,
13802 that means the line boundaries have changed. So we
13803 can't start copying with the row START. Maybe it will
13804 work to start copying with the following row. */
13805 while (IT_CHARPOS (it) > CHARPOS (start))
13806 {
13807 /* Advance to the next row as the "start". */
13808 start_row++;
13809 start = start_row->start.pos;
13810 /* If there are no more rows to try, or just one, give up. */
13811 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13812 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13813 || CHARPOS (start) == ZV)
13814 {
13815 clear_glyph_matrix (w->desired_matrix);
13816 return 0;
13817 }
13818
13819 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13820 }
13821 /* If we have reached alignment,
13822 we can copy the rest of the rows. */
13823 if (IT_CHARPOS (it) == CHARPOS (start))
13824 break;
13825
13826 if (display_line (&it))
13827 last_text_row = it.glyph_row - 1;
13828 }
13829
13830 /* A value of current_y < last_visible_y means that we stopped
13831 at the previous window start, which in turn means that we
13832 have at least one reusable row. */
13833 if (it.current_y < it.last_visible_y)
13834 {
13835 /* IT.vpos always starts from 0; it counts text lines. */
13836 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13837
13838 /* Find PT if not already found in the lines displayed. */
13839 if (w->cursor.vpos < 0)
13840 {
13841 int dy = it.current_y - start_row->y;
13842
13843 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13844 row = row_containing_pos (w, PT, row, NULL, dy);
13845 if (row)
13846 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13847 dy, nrows_scrolled);
13848 else
13849 {
13850 clear_glyph_matrix (w->desired_matrix);
13851 return 0;
13852 }
13853 }
13854
13855 /* Scroll the display. Do it before the current matrix is
13856 changed. The problem here is that update has not yet
13857 run, i.e. part of the current matrix is not up to date.
13858 scroll_run_hook will clear the cursor, and use the
13859 current matrix to get the height of the row the cursor is
13860 in. */
13861 run.current_y = start_row->y;
13862 run.desired_y = it.current_y;
13863 run.height = it.last_visible_y - it.current_y;
13864
13865 if (run.height > 0 && run.current_y != run.desired_y)
13866 {
13867 update_begin (f);
13868 rif->update_window_begin_hook (w);
13869 rif->clear_window_mouse_face (w);
13870 rif->scroll_run_hook (w, &run);
13871 rif->update_window_end_hook (w, 0, 0);
13872 update_end (f);
13873 }
13874
13875 /* Shift current matrix down by nrows_scrolled lines. */
13876 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13877 rotate_matrix (w->current_matrix,
13878 start_vpos,
13879 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13880 nrows_scrolled);
13881
13882 /* Disable lines that must be updated. */
13883 for (i = 0; i < nrows_scrolled; ++i)
13884 (start_row + i)->enabled_p = 0;
13885
13886 /* Re-compute Y positions. */
13887 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13888 max_y = it.last_visible_y;
13889 for (row = start_row + nrows_scrolled;
13890 row < bottom_row;
13891 ++row)
13892 {
13893 row->y = it.current_y;
13894 row->visible_height = row->height;
13895
13896 if (row->y < min_y)
13897 row->visible_height -= min_y - row->y;
13898 if (row->y + row->height > max_y)
13899 row->visible_height -= row->y + row->height - max_y;
13900 row->redraw_fringe_bitmaps_p = 1;
13901
13902 it.current_y += row->height;
13903
13904 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13905 last_reused_text_row = row;
13906 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13907 break;
13908 }
13909
13910 /* Disable lines in the current matrix which are now
13911 below the window. */
13912 for (++row; row < bottom_row; ++row)
13913 row->enabled_p = row->mode_line_p = 0;
13914 }
13915
13916 /* Update window_end_pos etc.; last_reused_text_row is the last
13917 reused row from the current matrix containing text, if any.
13918 The value of last_text_row is the last displayed line
13919 containing text. */
13920 if (last_reused_text_row)
13921 {
13922 w->window_end_bytepos
13923 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13924 w->window_end_pos
13925 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13926 w->window_end_vpos
13927 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13928 w->current_matrix));
13929 }
13930 else if (last_text_row)
13931 {
13932 w->window_end_bytepos
13933 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13934 w->window_end_pos
13935 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13936 w->window_end_vpos
13937 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13938 }
13939 else
13940 {
13941 /* This window must be completely empty. */
13942 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13943 w->window_end_pos = make_number (Z - ZV);
13944 w->window_end_vpos = make_number (0);
13945 }
13946 w->window_end_valid = Qnil;
13947
13948 /* Update hint: don't try scrolling again in update_window. */
13949 w->desired_matrix->no_scrolling_p = 1;
13950
13951 #if GLYPH_DEBUG
13952 debug_method_add (w, "try_window_reusing_current_matrix 1");
13953 #endif
13954 return 1;
13955 }
13956 else if (CHARPOS (new_start) > CHARPOS (start))
13957 {
13958 struct glyph_row *pt_row, *row;
13959 struct glyph_row *first_reusable_row;
13960 struct glyph_row *first_row_to_display;
13961 int dy;
13962 int yb = window_text_bottom_y (w);
13963
13964 /* Find the row starting at new_start, if there is one. Don't
13965 reuse a partially visible line at the end. */
13966 first_reusable_row = start_row;
13967 while (first_reusable_row->enabled_p
13968 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13969 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13970 < CHARPOS (new_start)))
13971 ++first_reusable_row;
13972
13973 /* Give up if there is no row to reuse. */
13974 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13975 || !first_reusable_row->enabled_p
13976 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13977 != CHARPOS (new_start)))
13978 return 0;
13979
13980 /* We can reuse fully visible rows beginning with
13981 first_reusable_row to the end of the window. Set
13982 first_row_to_display to the first row that cannot be reused.
13983 Set pt_row to the row containing point, if there is any. */
13984 pt_row = NULL;
13985 for (first_row_to_display = first_reusable_row;
13986 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13987 ++first_row_to_display)
13988 {
13989 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13990 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13991 pt_row = first_row_to_display;
13992 }
13993
13994 /* Start displaying at the start of first_row_to_display. */
13995 xassert (first_row_to_display->y < yb);
13996 init_to_row_start (&it, w, first_row_to_display);
13997
13998 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13999 - start_vpos);
14000 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14001 - nrows_scrolled);
14002 it.current_y = (first_row_to_display->y - first_reusable_row->y
14003 + WINDOW_HEADER_LINE_HEIGHT (w));
14004
14005 /* Display lines beginning with first_row_to_display in the
14006 desired matrix. Set last_text_row to the last row displayed
14007 that displays text. */
14008 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14009 if (pt_row == NULL)
14010 w->cursor.vpos = -1;
14011 last_text_row = NULL;
14012 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14013 if (display_line (&it))
14014 last_text_row = it.glyph_row - 1;
14015
14016 /* Give up If point isn't in a row displayed or reused. */
14017 if (w->cursor.vpos < 0)
14018 {
14019 clear_glyph_matrix (w->desired_matrix);
14020 return 0;
14021 }
14022
14023 /* If point is in a reused row, adjust y and vpos of the cursor
14024 position. */
14025 if (pt_row)
14026 {
14027 w->cursor.vpos -= nrows_scrolled;
14028 w->cursor.y -= first_reusable_row->y - start_row->y;
14029 }
14030
14031 /* Scroll the display. */
14032 run.current_y = first_reusable_row->y;
14033 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14034 run.height = it.last_visible_y - run.current_y;
14035 dy = run.current_y - run.desired_y;
14036
14037 if (run.height)
14038 {
14039 update_begin (f);
14040 rif->update_window_begin_hook (w);
14041 rif->clear_window_mouse_face (w);
14042 rif->scroll_run_hook (w, &run);
14043 rif->update_window_end_hook (w, 0, 0);
14044 update_end (f);
14045 }
14046
14047 /* Adjust Y positions of reused rows. */
14048 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14049 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14050 max_y = it.last_visible_y;
14051 for (row = first_reusable_row; row < first_row_to_display; ++row)
14052 {
14053 row->y -= dy;
14054 row->visible_height = row->height;
14055 if (row->y < min_y)
14056 row->visible_height -= min_y - row->y;
14057 if (row->y + row->height > max_y)
14058 row->visible_height -= row->y + row->height - max_y;
14059 row->redraw_fringe_bitmaps_p = 1;
14060 }
14061
14062 /* Scroll the current matrix. */
14063 xassert (nrows_scrolled > 0);
14064 rotate_matrix (w->current_matrix,
14065 start_vpos,
14066 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14067 -nrows_scrolled);
14068
14069 /* Disable rows not reused. */
14070 for (row -= nrows_scrolled; row < bottom_row; ++row)
14071 row->enabled_p = 0;
14072
14073 /* Point may have moved to a different line, so we cannot assume that
14074 the previous cursor position is valid; locate the correct row. */
14075 if (pt_row)
14076 {
14077 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14078 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14079 row++)
14080 {
14081 w->cursor.vpos++;
14082 w->cursor.y = row->y;
14083 }
14084 if (row < bottom_row)
14085 {
14086 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14087 while (glyph->charpos < PT)
14088 {
14089 w->cursor.hpos++;
14090 w->cursor.x += glyph->pixel_width;
14091 glyph++;
14092 }
14093 }
14094 }
14095
14096 /* Adjust window end. A null value of last_text_row means that
14097 the window end is in reused rows which in turn means that
14098 only its vpos can have changed. */
14099 if (last_text_row)
14100 {
14101 w->window_end_bytepos
14102 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14103 w->window_end_pos
14104 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14105 w->window_end_vpos
14106 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14107 }
14108 else
14109 {
14110 w->window_end_vpos
14111 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14112 }
14113
14114 w->window_end_valid = Qnil;
14115 w->desired_matrix->no_scrolling_p = 1;
14116
14117 #if GLYPH_DEBUG
14118 debug_method_add (w, "try_window_reusing_current_matrix 2");
14119 #endif
14120 return 1;
14121 }
14122
14123 return 0;
14124 }
14125
14126
14127 \f
14128 /************************************************************************
14129 Window redisplay reusing current matrix when buffer has changed
14130 ************************************************************************/
14131
14132 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14133 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14134 int *, int *));
14135 static struct glyph_row *
14136 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14137 struct glyph_row *));
14138
14139
14140 /* Return the last row in MATRIX displaying text. If row START is
14141 non-null, start searching with that row. IT gives the dimensions
14142 of the display. Value is null if matrix is empty; otherwise it is
14143 a pointer to the row found. */
14144
14145 static struct glyph_row *
14146 find_last_row_displaying_text (matrix, it, start)
14147 struct glyph_matrix *matrix;
14148 struct it *it;
14149 struct glyph_row *start;
14150 {
14151 struct glyph_row *row, *row_found;
14152
14153 /* Set row_found to the last row in IT->w's current matrix
14154 displaying text. The loop looks funny but think of partially
14155 visible lines. */
14156 row_found = NULL;
14157 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14158 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14159 {
14160 xassert (row->enabled_p);
14161 row_found = row;
14162 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14163 break;
14164 ++row;
14165 }
14166
14167 return row_found;
14168 }
14169
14170
14171 /* Return the last row in the current matrix of W that is not affected
14172 by changes at the start of current_buffer that occurred since W's
14173 current matrix was built. Value is null if no such row exists.
14174
14175 BEG_UNCHANGED us the number of characters unchanged at the start of
14176 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14177 first changed character in current_buffer. Characters at positions <
14178 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14179 when the current matrix was built. */
14180
14181 static struct glyph_row *
14182 find_last_unchanged_at_beg_row (w)
14183 struct window *w;
14184 {
14185 int first_changed_pos = BEG + BEG_UNCHANGED;
14186 struct glyph_row *row;
14187 struct glyph_row *row_found = NULL;
14188 int yb = window_text_bottom_y (w);
14189
14190 /* Find the last row displaying unchanged text. */
14191 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14192 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14193 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14194 {
14195 if (/* If row ends before first_changed_pos, it is unchanged,
14196 except in some case. */
14197 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14198 /* When row ends in ZV and we write at ZV it is not
14199 unchanged. */
14200 && !row->ends_at_zv_p
14201 /* When first_changed_pos is the end of a continued line,
14202 row is not unchanged because it may be no longer
14203 continued. */
14204 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14205 && (row->continued_p
14206 || row->exact_window_width_line_p)))
14207 row_found = row;
14208
14209 /* Stop if last visible row. */
14210 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14211 break;
14212
14213 ++row;
14214 }
14215
14216 return row_found;
14217 }
14218
14219
14220 /* Find the first glyph row in the current matrix of W that is not
14221 affected by changes at the end of current_buffer since the
14222 time W's current matrix was built.
14223
14224 Return in *DELTA the number of chars by which buffer positions in
14225 unchanged text at the end of current_buffer must be adjusted.
14226
14227 Return in *DELTA_BYTES the corresponding number of bytes.
14228
14229 Value is null if no such row exists, i.e. all rows are affected by
14230 changes. */
14231
14232 static struct glyph_row *
14233 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14234 struct window *w;
14235 int *delta, *delta_bytes;
14236 {
14237 struct glyph_row *row;
14238 struct glyph_row *row_found = NULL;
14239
14240 *delta = *delta_bytes = 0;
14241
14242 /* Display must not have been paused, otherwise the current matrix
14243 is not up to date. */
14244 if (NILP (w->window_end_valid))
14245 abort ();
14246
14247 /* A value of window_end_pos >= END_UNCHANGED means that the window
14248 end is in the range of changed text. If so, there is no
14249 unchanged row at the end of W's current matrix. */
14250 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14251 return NULL;
14252
14253 /* Set row to the last row in W's current matrix displaying text. */
14254 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14255
14256 /* If matrix is entirely empty, no unchanged row exists. */
14257 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14258 {
14259 /* The value of row is the last glyph row in the matrix having a
14260 meaningful buffer position in it. The end position of row
14261 corresponds to window_end_pos. This allows us to translate
14262 buffer positions in the current matrix to current buffer
14263 positions for characters not in changed text. */
14264 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14265 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14266 int last_unchanged_pos, last_unchanged_pos_old;
14267 struct glyph_row *first_text_row
14268 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14269
14270 *delta = Z - Z_old;
14271 *delta_bytes = Z_BYTE - Z_BYTE_old;
14272
14273 /* Set last_unchanged_pos to the buffer position of the last
14274 character in the buffer that has not been changed. Z is the
14275 index + 1 of the last character in current_buffer, i.e. by
14276 subtracting END_UNCHANGED we get the index of the last
14277 unchanged character, and we have to add BEG to get its buffer
14278 position. */
14279 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14280 last_unchanged_pos_old = last_unchanged_pos - *delta;
14281
14282 /* Search backward from ROW for a row displaying a line that
14283 starts at a minimum position >= last_unchanged_pos_old. */
14284 for (; row > first_text_row; --row)
14285 {
14286 /* This used to abort, but it can happen.
14287 It is ok to just stop the search instead here. KFS. */
14288 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14289 break;
14290
14291 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14292 row_found = row;
14293 }
14294 }
14295
14296 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14297 abort ();
14298
14299 return row_found;
14300 }
14301
14302
14303 /* Make sure that glyph rows in the current matrix of window W
14304 reference the same glyph memory as corresponding rows in the
14305 frame's frame matrix. This function is called after scrolling W's
14306 current matrix on a terminal frame in try_window_id and
14307 try_window_reusing_current_matrix. */
14308
14309 static void
14310 sync_frame_with_window_matrix_rows (w)
14311 struct window *w;
14312 {
14313 struct frame *f = XFRAME (w->frame);
14314 struct glyph_row *window_row, *window_row_end, *frame_row;
14315
14316 /* Preconditions: W must be a leaf window and full-width. Its frame
14317 must have a frame matrix. */
14318 xassert (NILP (w->hchild) && NILP (w->vchild));
14319 xassert (WINDOW_FULL_WIDTH_P (w));
14320 xassert (!FRAME_WINDOW_P (f));
14321
14322 /* If W is a full-width window, glyph pointers in W's current matrix
14323 have, by definition, to be the same as glyph pointers in the
14324 corresponding frame matrix. Note that frame matrices have no
14325 marginal areas (see build_frame_matrix). */
14326 window_row = w->current_matrix->rows;
14327 window_row_end = window_row + w->current_matrix->nrows;
14328 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14329 while (window_row < window_row_end)
14330 {
14331 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14332 struct glyph *end = window_row->glyphs[LAST_AREA];
14333
14334 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14335 frame_row->glyphs[TEXT_AREA] = start;
14336 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14337 frame_row->glyphs[LAST_AREA] = end;
14338
14339 /* Disable frame rows whose corresponding window rows have
14340 been disabled in try_window_id. */
14341 if (!window_row->enabled_p)
14342 frame_row->enabled_p = 0;
14343
14344 ++window_row, ++frame_row;
14345 }
14346 }
14347
14348
14349 /* Find the glyph row in window W containing CHARPOS. Consider all
14350 rows between START and END (not inclusive). END null means search
14351 all rows to the end of the display area of W. Value is the row
14352 containing CHARPOS or null. */
14353
14354 struct glyph_row *
14355 row_containing_pos (w, charpos, start, end, dy)
14356 struct window *w;
14357 int charpos;
14358 struct glyph_row *start, *end;
14359 int dy;
14360 {
14361 struct glyph_row *row = start;
14362 int last_y;
14363
14364 /* If we happen to start on a header-line, skip that. */
14365 if (row->mode_line_p)
14366 ++row;
14367
14368 if ((end && row >= end) || !row->enabled_p)
14369 return NULL;
14370
14371 last_y = window_text_bottom_y (w) - dy;
14372
14373 while (1)
14374 {
14375 /* Give up if we have gone too far. */
14376 if (end && row >= end)
14377 return NULL;
14378 /* This formerly returned if they were equal.
14379 I think that both quantities are of a "last plus one" type;
14380 if so, when they are equal, the row is within the screen. -- rms. */
14381 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14382 return NULL;
14383
14384 /* If it is in this row, return this row. */
14385 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14386 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14387 /* The end position of a row equals the start
14388 position of the next row. If CHARPOS is there, we
14389 would rather display it in the next line, except
14390 when this line ends in ZV. */
14391 && !row->ends_at_zv_p
14392 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14393 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14394 return row;
14395 ++row;
14396 }
14397 }
14398
14399
14400 /* Try to redisplay window W by reusing its existing display. W's
14401 current matrix must be up to date when this function is called,
14402 i.e. window_end_valid must not be nil.
14403
14404 Value is
14405
14406 1 if display has been updated
14407 0 if otherwise unsuccessful
14408 -1 if redisplay with same window start is known not to succeed
14409
14410 The following steps are performed:
14411
14412 1. Find the last row in the current matrix of W that is not
14413 affected by changes at the start of current_buffer. If no such row
14414 is found, give up.
14415
14416 2. Find the first row in W's current matrix that is not affected by
14417 changes at the end of current_buffer. Maybe there is no such row.
14418
14419 3. Display lines beginning with the row + 1 found in step 1 to the
14420 row found in step 2 or, if step 2 didn't find a row, to the end of
14421 the window.
14422
14423 4. If cursor is not known to appear on the window, give up.
14424
14425 5. If display stopped at the row found in step 2, scroll the
14426 display and current matrix as needed.
14427
14428 6. Maybe display some lines at the end of W, if we must. This can
14429 happen under various circumstances, like a partially visible line
14430 becoming fully visible, or because newly displayed lines are displayed
14431 in smaller font sizes.
14432
14433 7. Update W's window end information. */
14434
14435 static int
14436 try_window_id (w)
14437 struct window *w;
14438 {
14439 struct frame *f = XFRAME (w->frame);
14440 struct glyph_matrix *current_matrix = w->current_matrix;
14441 struct glyph_matrix *desired_matrix = w->desired_matrix;
14442 struct glyph_row *last_unchanged_at_beg_row;
14443 struct glyph_row *first_unchanged_at_end_row;
14444 struct glyph_row *row;
14445 struct glyph_row *bottom_row;
14446 int bottom_vpos;
14447 struct it it;
14448 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14449 struct text_pos start_pos;
14450 struct run run;
14451 int first_unchanged_at_end_vpos = 0;
14452 struct glyph_row *last_text_row, *last_text_row_at_end;
14453 struct text_pos start;
14454 int first_changed_charpos, last_changed_charpos;
14455
14456 #if GLYPH_DEBUG
14457 if (inhibit_try_window_id)
14458 return 0;
14459 #endif
14460
14461 /* This is handy for debugging. */
14462 #if 0
14463 #define GIVE_UP(X) \
14464 do { \
14465 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14466 return 0; \
14467 } while (0)
14468 #else
14469 #define GIVE_UP(X) return 0
14470 #endif
14471
14472 SET_TEXT_POS_FROM_MARKER (start, w->start);
14473
14474 /* Don't use this for mini-windows because these can show
14475 messages and mini-buffers, and we don't handle that here. */
14476 if (MINI_WINDOW_P (w))
14477 GIVE_UP (1);
14478
14479 /* This flag is used to prevent redisplay optimizations. */
14480 if (windows_or_buffers_changed || cursor_type_changed)
14481 GIVE_UP (2);
14482
14483 /* Verify that narrowing has not changed.
14484 Also verify that we were not told to prevent redisplay optimizations.
14485 It would be nice to further
14486 reduce the number of cases where this prevents try_window_id. */
14487 if (current_buffer->clip_changed
14488 || current_buffer->prevent_redisplay_optimizations_p)
14489 GIVE_UP (3);
14490
14491 /* Window must either use window-based redisplay or be full width. */
14492 if (!FRAME_WINDOW_P (f)
14493 && (!line_ins_del_ok
14494 || !WINDOW_FULL_WIDTH_P (w)))
14495 GIVE_UP (4);
14496
14497 /* Give up if point is not known NOT to appear in W. */
14498 if (PT < CHARPOS (start))
14499 GIVE_UP (5);
14500
14501 /* Another way to prevent redisplay optimizations. */
14502 if (XFASTINT (w->last_modified) == 0)
14503 GIVE_UP (6);
14504
14505 /* Verify that window is not hscrolled. */
14506 if (XFASTINT (w->hscroll) != 0)
14507 GIVE_UP (7);
14508
14509 /* Verify that display wasn't paused. */
14510 if (NILP (w->window_end_valid))
14511 GIVE_UP (8);
14512
14513 /* Can't use this if highlighting a region because a cursor movement
14514 will do more than just set the cursor. */
14515 if (!NILP (Vtransient_mark_mode)
14516 && !NILP (current_buffer->mark_active))
14517 GIVE_UP (9);
14518
14519 /* Likewise if highlighting trailing whitespace. */
14520 if (!NILP (Vshow_trailing_whitespace))
14521 GIVE_UP (11);
14522
14523 /* Likewise if showing a region. */
14524 if (!NILP (w->region_showing))
14525 GIVE_UP (10);
14526
14527 /* Can use this if overlay arrow position and or string have changed. */
14528 if (overlay_arrows_changed_p ())
14529 GIVE_UP (12);
14530
14531
14532 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14533 only if buffer has really changed. The reason is that the gap is
14534 initially at Z for freshly visited files. The code below would
14535 set end_unchanged to 0 in that case. */
14536 if (MODIFF > SAVE_MODIFF
14537 /* This seems to happen sometimes after saving a buffer. */
14538 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14539 {
14540 if (GPT - BEG < BEG_UNCHANGED)
14541 BEG_UNCHANGED = GPT - BEG;
14542 if (Z - GPT < END_UNCHANGED)
14543 END_UNCHANGED = Z - GPT;
14544 }
14545
14546 /* The position of the first and last character that has been changed. */
14547 first_changed_charpos = BEG + BEG_UNCHANGED;
14548 last_changed_charpos = Z - END_UNCHANGED;
14549
14550 /* If window starts after a line end, and the last change is in
14551 front of that newline, then changes don't affect the display.
14552 This case happens with stealth-fontification. Note that although
14553 the display is unchanged, glyph positions in the matrix have to
14554 be adjusted, of course. */
14555 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14556 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14557 && ((last_changed_charpos < CHARPOS (start)
14558 && CHARPOS (start) == BEGV)
14559 || (last_changed_charpos < CHARPOS (start) - 1
14560 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14561 {
14562 int Z_old, delta, Z_BYTE_old, delta_bytes;
14563 struct glyph_row *r0;
14564
14565 /* Compute how many chars/bytes have been added to or removed
14566 from the buffer. */
14567 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14568 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14569 delta = Z - Z_old;
14570 delta_bytes = Z_BYTE - Z_BYTE_old;
14571
14572 /* Give up if PT is not in the window. Note that it already has
14573 been checked at the start of try_window_id that PT is not in
14574 front of the window start. */
14575 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14576 GIVE_UP (13);
14577
14578 /* If window start is unchanged, we can reuse the whole matrix
14579 as is, after adjusting glyph positions. No need to compute
14580 the window end again, since its offset from Z hasn't changed. */
14581 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14582 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14583 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14584 /* PT must not be in a partially visible line. */
14585 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14586 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14587 {
14588 /* Adjust positions in the glyph matrix. */
14589 if (delta || delta_bytes)
14590 {
14591 struct glyph_row *r1
14592 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14593 increment_matrix_positions (w->current_matrix,
14594 MATRIX_ROW_VPOS (r0, current_matrix),
14595 MATRIX_ROW_VPOS (r1, current_matrix),
14596 delta, delta_bytes);
14597 }
14598
14599 /* Set the cursor. */
14600 row = row_containing_pos (w, PT, r0, NULL, 0);
14601 if (row)
14602 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14603 else
14604 abort ();
14605 return 1;
14606 }
14607 }
14608
14609 /* Handle the case that changes are all below what is displayed in
14610 the window, and that PT is in the window. This shortcut cannot
14611 be taken if ZV is visible in the window, and text has been added
14612 there that is visible in the window. */
14613 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14614 /* ZV is not visible in the window, or there are no
14615 changes at ZV, actually. */
14616 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14617 || first_changed_charpos == last_changed_charpos))
14618 {
14619 struct glyph_row *r0;
14620
14621 /* Give up if PT is not in the window. Note that it already has
14622 been checked at the start of try_window_id that PT is not in
14623 front of the window start. */
14624 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14625 GIVE_UP (14);
14626
14627 /* If window start is unchanged, we can reuse the whole matrix
14628 as is, without changing glyph positions since no text has
14629 been added/removed in front of the window end. */
14630 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14631 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14632 /* PT must not be in a partially visible line. */
14633 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14634 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14635 {
14636 /* We have to compute the window end anew since text
14637 can have been added/removed after it. */
14638 w->window_end_pos
14639 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14640 w->window_end_bytepos
14641 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14642
14643 /* Set the cursor. */
14644 row = row_containing_pos (w, PT, r0, NULL, 0);
14645 if (row)
14646 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14647 else
14648 abort ();
14649 return 2;
14650 }
14651 }
14652
14653 /* Give up if window start is in the changed area.
14654
14655 The condition used to read
14656
14657 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14658
14659 but why that was tested escapes me at the moment. */
14660 if (CHARPOS (start) >= first_changed_charpos
14661 && CHARPOS (start) <= last_changed_charpos)
14662 GIVE_UP (15);
14663
14664 /* Check that window start agrees with the start of the first glyph
14665 row in its current matrix. Check this after we know the window
14666 start is not in changed text, otherwise positions would not be
14667 comparable. */
14668 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14669 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14670 GIVE_UP (16);
14671
14672 /* Give up if the window ends in strings. Overlay strings
14673 at the end are difficult to handle, so don't try. */
14674 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14675 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14676 GIVE_UP (20);
14677
14678 /* Compute the position at which we have to start displaying new
14679 lines. Some of the lines at the top of the window might be
14680 reusable because they are not displaying changed text. Find the
14681 last row in W's current matrix not affected by changes at the
14682 start of current_buffer. Value is null if changes start in the
14683 first line of window. */
14684 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14685 if (last_unchanged_at_beg_row)
14686 {
14687 /* Avoid starting to display in the moddle of a character, a TAB
14688 for instance. This is easier than to set up the iterator
14689 exactly, and it's not a frequent case, so the additional
14690 effort wouldn't really pay off. */
14691 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14692 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14693 && last_unchanged_at_beg_row > w->current_matrix->rows)
14694 --last_unchanged_at_beg_row;
14695
14696 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14697 GIVE_UP (17);
14698
14699 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14700 GIVE_UP (18);
14701 start_pos = it.current.pos;
14702
14703 /* Start displaying new lines in the desired matrix at the same
14704 vpos we would use in the current matrix, i.e. below
14705 last_unchanged_at_beg_row. */
14706 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14707 current_matrix);
14708 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14709 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14710
14711 xassert (it.hpos == 0 && it.current_x == 0);
14712 }
14713 else
14714 {
14715 /* There are no reusable lines at the start of the window.
14716 Start displaying in the first text line. */
14717 start_display (&it, w, start);
14718 it.vpos = it.first_vpos;
14719 start_pos = it.current.pos;
14720 }
14721
14722 /* Find the first row that is not affected by changes at the end of
14723 the buffer. Value will be null if there is no unchanged row, in
14724 which case we must redisplay to the end of the window. delta
14725 will be set to the value by which buffer positions beginning with
14726 first_unchanged_at_end_row have to be adjusted due to text
14727 changes. */
14728 first_unchanged_at_end_row
14729 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14730 IF_DEBUG (debug_delta = delta);
14731 IF_DEBUG (debug_delta_bytes = delta_bytes);
14732
14733 /* Set stop_pos to the buffer position up to which we will have to
14734 display new lines. If first_unchanged_at_end_row != NULL, this
14735 is the buffer position of the start of the line displayed in that
14736 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14737 that we don't stop at a buffer position. */
14738 stop_pos = 0;
14739 if (first_unchanged_at_end_row)
14740 {
14741 xassert (last_unchanged_at_beg_row == NULL
14742 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14743
14744 /* If this is a continuation line, move forward to the next one
14745 that isn't. Changes in lines above affect this line.
14746 Caution: this may move first_unchanged_at_end_row to a row
14747 not displaying text. */
14748 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14749 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14750 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14751 < it.last_visible_y))
14752 ++first_unchanged_at_end_row;
14753
14754 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14755 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14756 >= it.last_visible_y))
14757 first_unchanged_at_end_row = NULL;
14758 else
14759 {
14760 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14761 + delta);
14762 first_unchanged_at_end_vpos
14763 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14764 xassert (stop_pos >= Z - END_UNCHANGED);
14765 }
14766 }
14767 else if (last_unchanged_at_beg_row == NULL)
14768 GIVE_UP (19);
14769
14770
14771 #if GLYPH_DEBUG
14772
14773 /* Either there is no unchanged row at the end, or the one we have
14774 now displays text. This is a necessary condition for the window
14775 end pos calculation at the end of this function. */
14776 xassert (first_unchanged_at_end_row == NULL
14777 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14778
14779 debug_last_unchanged_at_beg_vpos
14780 = (last_unchanged_at_beg_row
14781 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14782 : -1);
14783 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14784
14785 #endif /* GLYPH_DEBUG != 0 */
14786
14787
14788 /* Display new lines. Set last_text_row to the last new line
14789 displayed which has text on it, i.e. might end up as being the
14790 line where the window_end_vpos is. */
14791 w->cursor.vpos = -1;
14792 last_text_row = NULL;
14793 overlay_arrow_seen = 0;
14794 while (it.current_y < it.last_visible_y
14795 && !fonts_changed_p
14796 && (first_unchanged_at_end_row == NULL
14797 || IT_CHARPOS (it) < stop_pos))
14798 {
14799 if (display_line (&it))
14800 last_text_row = it.glyph_row - 1;
14801 }
14802
14803 if (fonts_changed_p)
14804 return -1;
14805
14806
14807 /* Compute differences in buffer positions, y-positions etc. for
14808 lines reused at the bottom of the window. Compute what we can
14809 scroll. */
14810 if (first_unchanged_at_end_row
14811 /* No lines reused because we displayed everything up to the
14812 bottom of the window. */
14813 && it.current_y < it.last_visible_y)
14814 {
14815 dvpos = (it.vpos
14816 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14817 current_matrix));
14818 dy = it.current_y - first_unchanged_at_end_row->y;
14819 run.current_y = first_unchanged_at_end_row->y;
14820 run.desired_y = run.current_y + dy;
14821 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14822 }
14823 else
14824 {
14825 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14826 first_unchanged_at_end_row = NULL;
14827 }
14828 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14829
14830
14831 /* Find the cursor if not already found. We have to decide whether
14832 PT will appear on this window (it sometimes doesn't, but this is
14833 not a very frequent case.) This decision has to be made before
14834 the current matrix is altered. A value of cursor.vpos < 0 means
14835 that PT is either in one of the lines beginning at
14836 first_unchanged_at_end_row or below the window. Don't care for
14837 lines that might be displayed later at the window end; as
14838 mentioned, this is not a frequent case. */
14839 if (w->cursor.vpos < 0)
14840 {
14841 /* Cursor in unchanged rows at the top? */
14842 if (PT < CHARPOS (start_pos)
14843 && last_unchanged_at_beg_row)
14844 {
14845 row = row_containing_pos (w, PT,
14846 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14847 last_unchanged_at_beg_row + 1, 0);
14848 if (row)
14849 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14850 }
14851
14852 /* Start from first_unchanged_at_end_row looking for PT. */
14853 else if (first_unchanged_at_end_row)
14854 {
14855 row = row_containing_pos (w, PT - delta,
14856 first_unchanged_at_end_row, NULL, 0);
14857 if (row)
14858 set_cursor_from_row (w, row, w->current_matrix, delta,
14859 delta_bytes, dy, dvpos);
14860 }
14861
14862 /* Give up if cursor was not found. */
14863 if (w->cursor.vpos < 0)
14864 {
14865 clear_glyph_matrix (w->desired_matrix);
14866 return -1;
14867 }
14868 }
14869
14870 /* Don't let the cursor end in the scroll margins. */
14871 {
14872 int this_scroll_margin, cursor_height;
14873
14874 this_scroll_margin = max (0, scroll_margin);
14875 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14876 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14877 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14878
14879 if ((w->cursor.y < this_scroll_margin
14880 && CHARPOS (start) > BEGV)
14881 /* Old redisplay didn't take scroll margin into account at the bottom,
14882 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14883 || (w->cursor.y + (make_cursor_line_fully_visible_p
14884 ? cursor_height + this_scroll_margin
14885 : 1)) > it.last_visible_y)
14886 {
14887 w->cursor.vpos = -1;
14888 clear_glyph_matrix (w->desired_matrix);
14889 return -1;
14890 }
14891 }
14892
14893 /* Scroll the display. Do it before changing the current matrix so
14894 that xterm.c doesn't get confused about where the cursor glyph is
14895 found. */
14896 if (dy && run.height)
14897 {
14898 update_begin (f);
14899
14900 if (FRAME_WINDOW_P (f))
14901 {
14902 rif->update_window_begin_hook (w);
14903 rif->clear_window_mouse_face (w);
14904 rif->scroll_run_hook (w, &run);
14905 rif->update_window_end_hook (w, 0, 0);
14906 }
14907 else
14908 {
14909 /* Terminal frame. In this case, dvpos gives the number of
14910 lines to scroll by; dvpos < 0 means scroll up. */
14911 int first_unchanged_at_end_vpos
14912 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14913 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14914 int end = (WINDOW_TOP_EDGE_LINE (w)
14915 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14916 + window_internal_height (w));
14917
14918 /* Perform the operation on the screen. */
14919 if (dvpos > 0)
14920 {
14921 /* Scroll last_unchanged_at_beg_row to the end of the
14922 window down dvpos lines. */
14923 set_terminal_window (end);
14924
14925 /* On dumb terminals delete dvpos lines at the end
14926 before inserting dvpos empty lines. */
14927 if (!scroll_region_ok)
14928 ins_del_lines (end - dvpos, -dvpos);
14929
14930 /* Insert dvpos empty lines in front of
14931 last_unchanged_at_beg_row. */
14932 ins_del_lines (from, dvpos);
14933 }
14934 else if (dvpos < 0)
14935 {
14936 /* Scroll up last_unchanged_at_beg_vpos to the end of
14937 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14938 set_terminal_window (end);
14939
14940 /* Delete dvpos lines in front of
14941 last_unchanged_at_beg_vpos. ins_del_lines will set
14942 the cursor to the given vpos and emit |dvpos| delete
14943 line sequences. */
14944 ins_del_lines (from + dvpos, dvpos);
14945
14946 /* On a dumb terminal insert dvpos empty lines at the
14947 end. */
14948 if (!scroll_region_ok)
14949 ins_del_lines (end + dvpos, -dvpos);
14950 }
14951
14952 set_terminal_window (0);
14953 }
14954
14955 update_end (f);
14956 }
14957
14958 /* Shift reused rows of the current matrix to the right position.
14959 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14960 text. */
14961 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14962 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14963 if (dvpos < 0)
14964 {
14965 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14966 bottom_vpos, dvpos);
14967 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14968 bottom_vpos, 0);
14969 }
14970 else if (dvpos > 0)
14971 {
14972 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14973 bottom_vpos, dvpos);
14974 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14975 first_unchanged_at_end_vpos + dvpos, 0);
14976 }
14977
14978 /* For frame-based redisplay, make sure that current frame and window
14979 matrix are in sync with respect to glyph memory. */
14980 if (!FRAME_WINDOW_P (f))
14981 sync_frame_with_window_matrix_rows (w);
14982
14983 /* Adjust buffer positions in reused rows. */
14984 if (delta || delta_bytes)
14985 increment_matrix_positions (current_matrix,
14986 first_unchanged_at_end_vpos + dvpos,
14987 bottom_vpos, delta, delta_bytes);
14988
14989 /* Adjust Y positions. */
14990 if (dy)
14991 shift_glyph_matrix (w, current_matrix,
14992 first_unchanged_at_end_vpos + dvpos,
14993 bottom_vpos, dy);
14994
14995 if (first_unchanged_at_end_row)
14996 {
14997 first_unchanged_at_end_row += dvpos;
14998 if (first_unchanged_at_end_row->y >= it.last_visible_y
14999 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15000 first_unchanged_at_end_row = NULL;
15001 }
15002
15003 /* If scrolling up, there may be some lines to display at the end of
15004 the window. */
15005 last_text_row_at_end = NULL;
15006 if (dy < 0)
15007 {
15008 /* Scrolling up can leave for example a partially visible line
15009 at the end of the window to be redisplayed. */
15010 /* Set last_row to the glyph row in the current matrix where the
15011 window end line is found. It has been moved up or down in
15012 the matrix by dvpos. */
15013 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15014 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15015
15016 /* If last_row is the window end line, it should display text. */
15017 xassert (last_row->displays_text_p);
15018
15019 /* If window end line was partially visible before, begin
15020 displaying at that line. Otherwise begin displaying with the
15021 line following it. */
15022 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15023 {
15024 init_to_row_start (&it, w, last_row);
15025 it.vpos = last_vpos;
15026 it.current_y = last_row->y;
15027 }
15028 else
15029 {
15030 init_to_row_end (&it, w, last_row);
15031 it.vpos = 1 + last_vpos;
15032 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15033 ++last_row;
15034 }
15035
15036 /* We may start in a continuation line. If so, we have to
15037 get the right continuation_lines_width and current_x. */
15038 it.continuation_lines_width = last_row->continuation_lines_width;
15039 it.hpos = it.current_x = 0;
15040
15041 /* Display the rest of the lines at the window end. */
15042 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15043 while (it.current_y < it.last_visible_y
15044 && !fonts_changed_p)
15045 {
15046 /* Is it always sure that the display agrees with lines in
15047 the current matrix? I don't think so, so we mark rows
15048 displayed invalid in the current matrix by setting their
15049 enabled_p flag to zero. */
15050 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15051 if (display_line (&it))
15052 last_text_row_at_end = it.glyph_row - 1;
15053 }
15054 }
15055
15056 /* Update window_end_pos and window_end_vpos. */
15057 if (first_unchanged_at_end_row
15058 && !last_text_row_at_end)
15059 {
15060 /* Window end line if one of the preserved rows from the current
15061 matrix. Set row to the last row displaying text in current
15062 matrix starting at first_unchanged_at_end_row, after
15063 scrolling. */
15064 xassert (first_unchanged_at_end_row->displays_text_p);
15065 row = find_last_row_displaying_text (w->current_matrix, &it,
15066 first_unchanged_at_end_row);
15067 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15068
15069 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15070 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15071 w->window_end_vpos
15072 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15073 xassert (w->window_end_bytepos >= 0);
15074 IF_DEBUG (debug_method_add (w, "A"));
15075 }
15076 else if (last_text_row_at_end)
15077 {
15078 w->window_end_pos
15079 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15080 w->window_end_bytepos
15081 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15082 w->window_end_vpos
15083 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15084 xassert (w->window_end_bytepos >= 0);
15085 IF_DEBUG (debug_method_add (w, "B"));
15086 }
15087 else if (last_text_row)
15088 {
15089 /* We have displayed either to the end of the window or at the
15090 end of the window, i.e. the last row with text is to be found
15091 in the desired matrix. */
15092 w->window_end_pos
15093 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15094 w->window_end_bytepos
15095 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15096 w->window_end_vpos
15097 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15098 xassert (w->window_end_bytepos >= 0);
15099 }
15100 else if (first_unchanged_at_end_row == NULL
15101 && last_text_row == NULL
15102 && last_text_row_at_end == NULL)
15103 {
15104 /* Displayed to end of window, but no line containing text was
15105 displayed. Lines were deleted at the end of the window. */
15106 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15107 int vpos = XFASTINT (w->window_end_vpos);
15108 struct glyph_row *current_row = current_matrix->rows + vpos;
15109 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15110
15111 for (row = NULL;
15112 row == NULL && vpos >= first_vpos;
15113 --vpos, --current_row, --desired_row)
15114 {
15115 if (desired_row->enabled_p)
15116 {
15117 if (desired_row->displays_text_p)
15118 row = desired_row;
15119 }
15120 else if (current_row->displays_text_p)
15121 row = current_row;
15122 }
15123
15124 xassert (row != NULL);
15125 w->window_end_vpos = make_number (vpos + 1);
15126 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15127 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15128 xassert (w->window_end_bytepos >= 0);
15129 IF_DEBUG (debug_method_add (w, "C"));
15130 }
15131 else
15132 abort ();
15133
15134 #if 0 /* This leads to problems, for instance when the cursor is
15135 at ZV, and the cursor line displays no text. */
15136 /* Disable rows below what's displayed in the window. This makes
15137 debugging easier. */
15138 enable_glyph_matrix_rows (current_matrix,
15139 XFASTINT (w->window_end_vpos) + 1,
15140 bottom_vpos, 0);
15141 #endif
15142
15143 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15144 debug_end_vpos = XFASTINT (w->window_end_vpos));
15145
15146 /* Record that display has not been completed. */
15147 w->window_end_valid = Qnil;
15148 w->desired_matrix->no_scrolling_p = 1;
15149 return 3;
15150
15151 #undef GIVE_UP
15152 }
15153
15154
15155 \f
15156 /***********************************************************************
15157 More debugging support
15158 ***********************************************************************/
15159
15160 #if GLYPH_DEBUG
15161
15162 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15163 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15164 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15165
15166
15167 /* Dump the contents of glyph matrix MATRIX on stderr.
15168
15169 GLYPHS 0 means don't show glyph contents.
15170 GLYPHS 1 means show glyphs in short form
15171 GLYPHS > 1 means show glyphs in long form. */
15172
15173 void
15174 dump_glyph_matrix (matrix, glyphs)
15175 struct glyph_matrix *matrix;
15176 int glyphs;
15177 {
15178 int i;
15179 for (i = 0; i < matrix->nrows; ++i)
15180 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15181 }
15182
15183
15184 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15185 the glyph row and area where the glyph comes from. */
15186
15187 void
15188 dump_glyph (row, glyph, area)
15189 struct glyph_row *row;
15190 struct glyph *glyph;
15191 int area;
15192 {
15193 if (glyph->type == CHAR_GLYPH)
15194 {
15195 fprintf (stderr,
15196 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15197 glyph - row->glyphs[TEXT_AREA],
15198 'C',
15199 glyph->charpos,
15200 (BUFFERP (glyph->object)
15201 ? 'B'
15202 : (STRINGP (glyph->object)
15203 ? 'S'
15204 : '-')),
15205 glyph->pixel_width,
15206 glyph->u.ch,
15207 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15208 ? glyph->u.ch
15209 : '.'),
15210 glyph->face_id,
15211 glyph->left_box_line_p,
15212 glyph->right_box_line_p);
15213 }
15214 else if (glyph->type == STRETCH_GLYPH)
15215 {
15216 fprintf (stderr,
15217 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15218 glyph - row->glyphs[TEXT_AREA],
15219 'S',
15220 glyph->charpos,
15221 (BUFFERP (glyph->object)
15222 ? 'B'
15223 : (STRINGP (glyph->object)
15224 ? 'S'
15225 : '-')),
15226 glyph->pixel_width,
15227 0,
15228 '.',
15229 glyph->face_id,
15230 glyph->left_box_line_p,
15231 glyph->right_box_line_p);
15232 }
15233 else if (glyph->type == IMAGE_GLYPH)
15234 {
15235 fprintf (stderr,
15236 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15237 glyph - row->glyphs[TEXT_AREA],
15238 'I',
15239 glyph->charpos,
15240 (BUFFERP (glyph->object)
15241 ? 'B'
15242 : (STRINGP (glyph->object)
15243 ? 'S'
15244 : '-')),
15245 glyph->pixel_width,
15246 glyph->u.img_id,
15247 '.',
15248 glyph->face_id,
15249 glyph->left_box_line_p,
15250 glyph->right_box_line_p);
15251 }
15252 else if (glyph->type == COMPOSITE_GLYPH)
15253 {
15254 fprintf (stderr,
15255 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15256 glyph - row->glyphs[TEXT_AREA],
15257 '+',
15258 glyph->charpos,
15259 (BUFFERP (glyph->object)
15260 ? 'B'
15261 : (STRINGP (glyph->object)
15262 ? 'S'
15263 : '-')),
15264 glyph->pixel_width,
15265 glyph->u.cmp_id,
15266 '.',
15267 glyph->face_id,
15268 glyph->left_box_line_p,
15269 glyph->right_box_line_p);
15270 }
15271 }
15272
15273
15274 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15275 GLYPHS 0 means don't show glyph contents.
15276 GLYPHS 1 means show glyphs in short form
15277 GLYPHS > 1 means show glyphs in long form. */
15278
15279 void
15280 dump_glyph_row (row, vpos, glyphs)
15281 struct glyph_row *row;
15282 int vpos, glyphs;
15283 {
15284 if (glyphs != 1)
15285 {
15286 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15287 fprintf (stderr, "======================================================================\n");
15288
15289 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15290 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15291 vpos,
15292 MATRIX_ROW_START_CHARPOS (row),
15293 MATRIX_ROW_END_CHARPOS (row),
15294 row->used[TEXT_AREA],
15295 row->contains_overlapping_glyphs_p,
15296 row->enabled_p,
15297 row->truncated_on_left_p,
15298 row->truncated_on_right_p,
15299 row->continued_p,
15300 MATRIX_ROW_CONTINUATION_LINE_P (row),
15301 row->displays_text_p,
15302 row->ends_at_zv_p,
15303 row->fill_line_p,
15304 row->ends_in_middle_of_char_p,
15305 row->starts_in_middle_of_char_p,
15306 row->mouse_face_p,
15307 row->x,
15308 row->y,
15309 row->pixel_width,
15310 row->height,
15311 row->visible_height,
15312 row->ascent,
15313 row->phys_ascent);
15314 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15315 row->end.overlay_string_index,
15316 row->continuation_lines_width);
15317 fprintf (stderr, "%9d %5d\n",
15318 CHARPOS (row->start.string_pos),
15319 CHARPOS (row->end.string_pos));
15320 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15321 row->end.dpvec_index);
15322 }
15323
15324 if (glyphs > 1)
15325 {
15326 int area;
15327
15328 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15329 {
15330 struct glyph *glyph = row->glyphs[area];
15331 struct glyph *glyph_end = glyph + row->used[area];
15332
15333 /* Glyph for a line end in text. */
15334 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15335 ++glyph_end;
15336
15337 if (glyph < glyph_end)
15338 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15339
15340 for (; glyph < glyph_end; ++glyph)
15341 dump_glyph (row, glyph, area);
15342 }
15343 }
15344 else if (glyphs == 1)
15345 {
15346 int area;
15347
15348 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15349 {
15350 char *s = (char *) alloca (row->used[area] + 1);
15351 int i;
15352
15353 for (i = 0; i < row->used[area]; ++i)
15354 {
15355 struct glyph *glyph = row->glyphs[area] + i;
15356 if (glyph->type == CHAR_GLYPH
15357 && glyph->u.ch < 0x80
15358 && glyph->u.ch >= ' ')
15359 s[i] = glyph->u.ch;
15360 else
15361 s[i] = '.';
15362 }
15363
15364 s[i] = '\0';
15365 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15366 }
15367 }
15368 }
15369
15370
15371 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15372 Sdump_glyph_matrix, 0, 1, "p",
15373 doc: /* Dump the current matrix of the selected window to stderr.
15374 Shows contents of glyph row structures. With non-nil
15375 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15376 glyphs in short form, otherwise show glyphs in long form. */)
15377 (glyphs)
15378 Lisp_Object glyphs;
15379 {
15380 struct window *w = XWINDOW (selected_window);
15381 struct buffer *buffer = XBUFFER (w->buffer);
15382
15383 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15384 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15385 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15386 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15387 fprintf (stderr, "=============================================\n");
15388 dump_glyph_matrix (w->current_matrix,
15389 NILP (glyphs) ? 0 : XINT (glyphs));
15390 return Qnil;
15391 }
15392
15393
15394 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15395 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15396 ()
15397 {
15398 struct frame *f = XFRAME (selected_frame);
15399 dump_glyph_matrix (f->current_matrix, 1);
15400 return Qnil;
15401 }
15402
15403
15404 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15405 doc: /* Dump glyph row ROW to stderr.
15406 GLYPH 0 means don't dump glyphs.
15407 GLYPH 1 means dump glyphs in short form.
15408 GLYPH > 1 or omitted means dump glyphs in long form. */)
15409 (row, glyphs)
15410 Lisp_Object row, glyphs;
15411 {
15412 struct glyph_matrix *matrix;
15413 int vpos;
15414
15415 CHECK_NUMBER (row);
15416 matrix = XWINDOW (selected_window)->current_matrix;
15417 vpos = XINT (row);
15418 if (vpos >= 0 && vpos < matrix->nrows)
15419 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15420 vpos,
15421 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15422 return Qnil;
15423 }
15424
15425
15426 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15427 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15428 GLYPH 0 means don't dump glyphs.
15429 GLYPH 1 means dump glyphs in short form.
15430 GLYPH > 1 or omitted means dump glyphs in long form. */)
15431 (row, glyphs)
15432 Lisp_Object row, glyphs;
15433 {
15434 struct frame *sf = SELECTED_FRAME ();
15435 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15436 int vpos;
15437
15438 CHECK_NUMBER (row);
15439 vpos = XINT (row);
15440 if (vpos >= 0 && vpos < m->nrows)
15441 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15442 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15443 return Qnil;
15444 }
15445
15446
15447 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15448 doc: /* Toggle tracing of redisplay.
15449 With ARG, turn tracing on if and only if ARG is positive. */)
15450 (arg)
15451 Lisp_Object arg;
15452 {
15453 if (NILP (arg))
15454 trace_redisplay_p = !trace_redisplay_p;
15455 else
15456 {
15457 arg = Fprefix_numeric_value (arg);
15458 trace_redisplay_p = XINT (arg) > 0;
15459 }
15460
15461 return Qnil;
15462 }
15463
15464
15465 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15466 doc: /* Like `format', but print result to stderr.
15467 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15468 (nargs, args)
15469 int nargs;
15470 Lisp_Object *args;
15471 {
15472 Lisp_Object s = Fformat (nargs, args);
15473 fprintf (stderr, "%s", SDATA (s));
15474 return Qnil;
15475 }
15476
15477 #endif /* GLYPH_DEBUG */
15478
15479
15480 \f
15481 /***********************************************************************
15482 Building Desired Matrix Rows
15483 ***********************************************************************/
15484
15485 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15486 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15487
15488 static struct glyph_row *
15489 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15490 struct window *w;
15491 Lisp_Object overlay_arrow_string;
15492 {
15493 struct frame *f = XFRAME (WINDOW_FRAME (w));
15494 struct buffer *buffer = XBUFFER (w->buffer);
15495 struct buffer *old = current_buffer;
15496 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15497 int arrow_len = SCHARS (overlay_arrow_string);
15498 const unsigned char *arrow_end = arrow_string + arrow_len;
15499 const unsigned char *p;
15500 struct it it;
15501 int multibyte_p;
15502 int n_glyphs_before;
15503
15504 set_buffer_temp (buffer);
15505 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15506 it.glyph_row->used[TEXT_AREA] = 0;
15507 SET_TEXT_POS (it.position, 0, 0);
15508
15509 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15510 p = arrow_string;
15511 while (p < arrow_end)
15512 {
15513 Lisp_Object face, ilisp;
15514
15515 /* Get the next character. */
15516 if (multibyte_p)
15517 it.c = string_char_and_length (p, arrow_len, &it.len);
15518 else
15519 it.c = *p, it.len = 1;
15520 p += it.len;
15521
15522 /* Get its face. */
15523 ilisp = make_number (p - arrow_string);
15524 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15525 it.face_id = compute_char_face (f, it.c, face);
15526
15527 /* Compute its width, get its glyphs. */
15528 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15529 SET_TEXT_POS (it.position, -1, -1);
15530 PRODUCE_GLYPHS (&it);
15531
15532 /* If this character doesn't fit any more in the line, we have
15533 to remove some glyphs. */
15534 if (it.current_x > it.last_visible_x)
15535 {
15536 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15537 break;
15538 }
15539 }
15540
15541 set_buffer_temp (old);
15542 return it.glyph_row;
15543 }
15544
15545
15546 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15547 glyphs are only inserted for terminal frames since we can't really
15548 win with truncation glyphs when partially visible glyphs are
15549 involved. Which glyphs to insert is determined by
15550 produce_special_glyphs. */
15551
15552 static void
15553 insert_left_trunc_glyphs (it)
15554 struct it *it;
15555 {
15556 struct it truncate_it;
15557 struct glyph *from, *end, *to, *toend;
15558
15559 xassert (!FRAME_WINDOW_P (it->f));
15560
15561 /* Get the truncation glyphs. */
15562 truncate_it = *it;
15563 truncate_it.current_x = 0;
15564 truncate_it.face_id = DEFAULT_FACE_ID;
15565 truncate_it.glyph_row = &scratch_glyph_row;
15566 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15567 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15568 truncate_it.object = make_number (0);
15569 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15570
15571 /* Overwrite glyphs from IT with truncation glyphs. */
15572 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15573 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15574 to = it->glyph_row->glyphs[TEXT_AREA];
15575 toend = to + it->glyph_row->used[TEXT_AREA];
15576
15577 while (from < end)
15578 *to++ = *from++;
15579
15580 /* There may be padding glyphs left over. Overwrite them too. */
15581 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15582 {
15583 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15584 while (from < end)
15585 *to++ = *from++;
15586 }
15587
15588 if (to > toend)
15589 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15590 }
15591
15592
15593 /* Compute the pixel height and width of IT->glyph_row.
15594
15595 Most of the time, ascent and height of a display line will be equal
15596 to the max_ascent and max_height values of the display iterator
15597 structure. This is not the case if
15598
15599 1. We hit ZV without displaying anything. In this case, max_ascent
15600 and max_height will be zero.
15601
15602 2. We have some glyphs that don't contribute to the line height.
15603 (The glyph row flag contributes_to_line_height_p is for future
15604 pixmap extensions).
15605
15606 The first case is easily covered by using default values because in
15607 these cases, the line height does not really matter, except that it
15608 must not be zero. */
15609
15610 static void
15611 compute_line_metrics (it)
15612 struct it *it;
15613 {
15614 struct glyph_row *row = it->glyph_row;
15615 int area, i;
15616
15617 if (FRAME_WINDOW_P (it->f))
15618 {
15619 int i, min_y, max_y;
15620
15621 /* The line may consist of one space only, that was added to
15622 place the cursor on it. If so, the row's height hasn't been
15623 computed yet. */
15624 if (row->height == 0)
15625 {
15626 if (it->max_ascent + it->max_descent == 0)
15627 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15628 row->ascent = it->max_ascent;
15629 row->height = it->max_ascent + it->max_descent;
15630 row->phys_ascent = it->max_phys_ascent;
15631 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15632 row->extra_line_spacing = it->max_extra_line_spacing;
15633 }
15634
15635 /* Compute the width of this line. */
15636 row->pixel_width = row->x;
15637 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15638 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15639
15640 xassert (row->pixel_width >= 0);
15641 xassert (row->ascent >= 0 && row->height > 0);
15642
15643 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15644 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15645
15646 /* If first line's physical ascent is larger than its logical
15647 ascent, use the physical ascent, and make the row taller.
15648 This makes accented characters fully visible. */
15649 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15650 && row->phys_ascent > row->ascent)
15651 {
15652 row->height += row->phys_ascent - row->ascent;
15653 row->ascent = row->phys_ascent;
15654 }
15655
15656 /* Compute how much of the line is visible. */
15657 row->visible_height = row->height;
15658
15659 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15660 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15661
15662 if (row->y < min_y)
15663 row->visible_height -= min_y - row->y;
15664 if (row->y + row->height > max_y)
15665 row->visible_height -= row->y + row->height - max_y;
15666 }
15667 else
15668 {
15669 row->pixel_width = row->used[TEXT_AREA];
15670 if (row->continued_p)
15671 row->pixel_width -= it->continuation_pixel_width;
15672 else if (row->truncated_on_right_p)
15673 row->pixel_width -= it->truncation_pixel_width;
15674 row->ascent = row->phys_ascent = 0;
15675 row->height = row->phys_height = row->visible_height = 1;
15676 row->extra_line_spacing = 0;
15677 }
15678
15679 /* Compute a hash code for this row. */
15680 row->hash = 0;
15681 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15682 for (i = 0; i < row->used[area]; ++i)
15683 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15684 + row->glyphs[area][i].u.val
15685 + row->glyphs[area][i].face_id
15686 + row->glyphs[area][i].padding_p
15687 + (row->glyphs[area][i].type << 2));
15688
15689 it->max_ascent = it->max_descent = 0;
15690 it->max_phys_ascent = it->max_phys_descent = 0;
15691 }
15692
15693
15694 /* Append one space to the glyph row of iterator IT if doing a
15695 window-based redisplay. The space has the same face as
15696 IT->face_id. Value is non-zero if a space was added.
15697
15698 This function is called to make sure that there is always one glyph
15699 at the end of a glyph row that the cursor can be set on under
15700 window-systems. (If there weren't such a glyph we would not know
15701 how wide and tall a box cursor should be displayed).
15702
15703 At the same time this space let's a nicely handle clearing to the
15704 end of the line if the row ends in italic text. */
15705
15706 static int
15707 append_space_for_newline (it, default_face_p)
15708 struct it *it;
15709 int default_face_p;
15710 {
15711 if (FRAME_WINDOW_P (it->f))
15712 {
15713 int n = it->glyph_row->used[TEXT_AREA];
15714
15715 if (it->glyph_row->glyphs[TEXT_AREA] + n
15716 < it->glyph_row->glyphs[1 + TEXT_AREA])
15717 {
15718 /* Save some values that must not be changed.
15719 Must save IT->c and IT->len because otherwise
15720 ITERATOR_AT_END_P wouldn't work anymore after
15721 append_space_for_newline has been called. */
15722 enum display_element_type saved_what = it->what;
15723 int saved_c = it->c, saved_len = it->len;
15724 int saved_x = it->current_x;
15725 int saved_face_id = it->face_id;
15726 struct text_pos saved_pos;
15727 Lisp_Object saved_object;
15728 struct face *face;
15729
15730 saved_object = it->object;
15731 saved_pos = it->position;
15732
15733 it->what = IT_CHARACTER;
15734 bzero (&it->position, sizeof it->position);
15735 it->object = make_number (0);
15736 it->c = ' ';
15737 it->len = 1;
15738
15739 if (default_face_p)
15740 it->face_id = DEFAULT_FACE_ID;
15741 else if (it->face_before_selective_p)
15742 it->face_id = it->saved_face_id;
15743 face = FACE_FROM_ID (it->f, it->face_id);
15744 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15745
15746 PRODUCE_GLYPHS (it);
15747
15748 it->override_ascent = -1;
15749 it->constrain_row_ascent_descent_p = 0;
15750 it->current_x = saved_x;
15751 it->object = saved_object;
15752 it->position = saved_pos;
15753 it->what = saved_what;
15754 it->face_id = saved_face_id;
15755 it->len = saved_len;
15756 it->c = saved_c;
15757 return 1;
15758 }
15759 }
15760
15761 return 0;
15762 }
15763
15764
15765 /* Extend the face of the last glyph in the text area of IT->glyph_row
15766 to the end of the display line. Called from display_line.
15767 If the glyph row is empty, add a space glyph to it so that we
15768 know the face to draw. Set the glyph row flag fill_line_p. */
15769
15770 static void
15771 extend_face_to_end_of_line (it)
15772 struct it *it;
15773 {
15774 struct face *face;
15775 struct frame *f = it->f;
15776
15777 /* If line is already filled, do nothing. */
15778 if (it->current_x >= it->last_visible_x)
15779 return;
15780
15781 /* Face extension extends the background and box of IT->face_id
15782 to the end of the line. If the background equals the background
15783 of the frame, we don't have to do anything. */
15784 if (it->face_before_selective_p)
15785 face = FACE_FROM_ID (it->f, it->saved_face_id);
15786 else
15787 face = FACE_FROM_ID (f, it->face_id);
15788
15789 if (FRAME_WINDOW_P (f)
15790 && it->glyph_row->displays_text_p
15791 && face->box == FACE_NO_BOX
15792 && face->background == FRAME_BACKGROUND_PIXEL (f)
15793 && !face->stipple)
15794 return;
15795
15796 /* Set the glyph row flag indicating that the face of the last glyph
15797 in the text area has to be drawn to the end of the text area. */
15798 it->glyph_row->fill_line_p = 1;
15799
15800 /* If current character of IT is not ASCII, make sure we have the
15801 ASCII face. This will be automatically undone the next time
15802 get_next_display_element returns a multibyte character. Note
15803 that the character will always be single byte in unibyte text. */
15804 if (!SINGLE_BYTE_CHAR_P (it->c))
15805 {
15806 it->face_id = FACE_FOR_CHAR (f, face, 0);
15807 }
15808
15809 if (FRAME_WINDOW_P (f))
15810 {
15811 /* If the row is empty, add a space with the current face of IT,
15812 so that we know which face to draw. */
15813 if (it->glyph_row->used[TEXT_AREA] == 0)
15814 {
15815 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15816 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15817 it->glyph_row->used[TEXT_AREA] = 1;
15818 }
15819 }
15820 else
15821 {
15822 /* Save some values that must not be changed. */
15823 int saved_x = it->current_x;
15824 struct text_pos saved_pos;
15825 Lisp_Object saved_object;
15826 enum display_element_type saved_what = it->what;
15827 int saved_face_id = it->face_id;
15828
15829 saved_object = it->object;
15830 saved_pos = it->position;
15831
15832 it->what = IT_CHARACTER;
15833 bzero (&it->position, sizeof it->position);
15834 it->object = make_number (0);
15835 it->c = ' ';
15836 it->len = 1;
15837 it->face_id = face->id;
15838
15839 PRODUCE_GLYPHS (it);
15840
15841 while (it->current_x <= it->last_visible_x)
15842 PRODUCE_GLYPHS (it);
15843
15844 /* Don't count these blanks really. It would let us insert a left
15845 truncation glyph below and make us set the cursor on them, maybe. */
15846 it->current_x = saved_x;
15847 it->object = saved_object;
15848 it->position = saved_pos;
15849 it->what = saved_what;
15850 it->face_id = saved_face_id;
15851 }
15852 }
15853
15854
15855 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15856 trailing whitespace. */
15857
15858 static int
15859 trailing_whitespace_p (charpos)
15860 int charpos;
15861 {
15862 int bytepos = CHAR_TO_BYTE (charpos);
15863 int c = 0;
15864
15865 while (bytepos < ZV_BYTE
15866 && (c = FETCH_CHAR (bytepos),
15867 c == ' ' || c == '\t'))
15868 ++bytepos;
15869
15870 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15871 {
15872 if (bytepos != PT_BYTE)
15873 return 1;
15874 }
15875 return 0;
15876 }
15877
15878
15879 /* Highlight trailing whitespace, if any, in ROW. */
15880
15881 void
15882 highlight_trailing_whitespace (f, row)
15883 struct frame *f;
15884 struct glyph_row *row;
15885 {
15886 int used = row->used[TEXT_AREA];
15887
15888 if (used)
15889 {
15890 struct glyph *start = row->glyphs[TEXT_AREA];
15891 struct glyph *glyph = start + used - 1;
15892
15893 /* Skip over glyphs inserted to display the cursor at the
15894 end of a line, for extending the face of the last glyph
15895 to the end of the line on terminals, and for truncation
15896 and continuation glyphs. */
15897 while (glyph >= start
15898 && glyph->type == CHAR_GLYPH
15899 && INTEGERP (glyph->object))
15900 --glyph;
15901
15902 /* If last glyph is a space or stretch, and it's trailing
15903 whitespace, set the face of all trailing whitespace glyphs in
15904 IT->glyph_row to `trailing-whitespace'. */
15905 if (glyph >= start
15906 && BUFFERP (glyph->object)
15907 && (glyph->type == STRETCH_GLYPH
15908 || (glyph->type == CHAR_GLYPH
15909 && glyph->u.ch == ' '))
15910 && trailing_whitespace_p (glyph->charpos))
15911 {
15912 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15913 if (face_id < 0)
15914 return;
15915
15916 while (glyph >= start
15917 && BUFFERP (glyph->object)
15918 && (glyph->type == STRETCH_GLYPH
15919 || (glyph->type == CHAR_GLYPH
15920 && glyph->u.ch == ' ')))
15921 (glyph--)->face_id = face_id;
15922 }
15923 }
15924 }
15925
15926
15927 /* Value is non-zero if glyph row ROW in window W should be
15928 used to hold the cursor. */
15929
15930 static int
15931 cursor_row_p (w, row)
15932 struct window *w;
15933 struct glyph_row *row;
15934 {
15935 int cursor_row_p = 1;
15936
15937 if (PT == MATRIX_ROW_END_CHARPOS (row))
15938 {
15939 /* Suppose the row ends on a string.
15940 Unless the row is continued, that means it ends on a newline
15941 in the string. If it's anything other than a display string
15942 (e.g. a before-string from an overlay), we don't want the
15943 cursor there. (This heuristic seems to give the optimal
15944 behavior for the various types of multi-line strings.) */
15945 if (CHARPOS (row->end.string_pos) >= 0)
15946 {
15947 if (row->continued_p)
15948 cursor_row_p = 1;
15949 else
15950 {
15951 /* Check for `display' property. */
15952 struct glyph *beg = row->glyphs[TEXT_AREA];
15953 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15954 struct glyph *glyph;
15955
15956 cursor_row_p = 0;
15957 for (glyph = end; glyph >= beg; --glyph)
15958 if (STRINGP (glyph->object))
15959 {
15960 Lisp_Object prop
15961 = Fget_char_property (make_number (PT),
15962 Qdisplay, Qnil);
15963 cursor_row_p =
15964 (!NILP (prop)
15965 && display_prop_string_p (prop, glyph->object));
15966 break;
15967 }
15968 }
15969 }
15970 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15971 {
15972 /* If the row ends in middle of a real character,
15973 and the line is continued, we want the cursor here.
15974 That's because MATRIX_ROW_END_CHARPOS would equal
15975 PT if PT is before the character. */
15976 if (!row->ends_in_ellipsis_p)
15977 cursor_row_p = row->continued_p;
15978 else
15979 /* If the row ends in an ellipsis, then
15980 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15981 We want that position to be displayed after the ellipsis. */
15982 cursor_row_p = 0;
15983 }
15984 /* If the row ends at ZV, display the cursor at the end of that
15985 row instead of at the start of the row below. */
15986 else if (row->ends_at_zv_p)
15987 cursor_row_p = 1;
15988 else
15989 cursor_row_p = 0;
15990 }
15991
15992 return cursor_row_p;
15993 }
15994
15995
15996 /* Construct the glyph row IT->glyph_row in the desired matrix of
15997 IT->w from text at the current position of IT. See dispextern.h
15998 for an overview of struct it. Value is non-zero if
15999 IT->glyph_row displays text, as opposed to a line displaying ZV
16000 only. */
16001
16002 static int
16003 display_line (it)
16004 struct it *it;
16005 {
16006 struct glyph_row *row = it->glyph_row;
16007 Lisp_Object overlay_arrow_string;
16008
16009 /* We always start displaying at hpos zero even if hscrolled. */
16010 xassert (it->hpos == 0 && it->current_x == 0);
16011
16012 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16013 >= it->w->desired_matrix->nrows)
16014 {
16015 it->w->nrows_scale_factor++;
16016 fonts_changed_p = 1;
16017 return 0;
16018 }
16019
16020 /* Is IT->w showing the region? */
16021 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16022
16023 /* Clear the result glyph row and enable it. */
16024 prepare_desired_row (row);
16025
16026 row->y = it->current_y;
16027 row->start = it->start;
16028 row->continuation_lines_width = it->continuation_lines_width;
16029 row->displays_text_p = 1;
16030 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16031 it->starts_in_middle_of_char_p = 0;
16032
16033 /* Arrange the overlays nicely for our purposes. Usually, we call
16034 display_line on only one line at a time, in which case this
16035 can't really hurt too much, or we call it on lines which appear
16036 one after another in the buffer, in which case all calls to
16037 recenter_overlay_lists but the first will be pretty cheap. */
16038 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16039
16040 /* Move over display elements that are not visible because we are
16041 hscrolled. This may stop at an x-position < IT->first_visible_x
16042 if the first glyph is partially visible or if we hit a line end. */
16043 if (it->current_x < it->first_visible_x)
16044 {
16045 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16046 MOVE_TO_POS | MOVE_TO_X);
16047 }
16048
16049 /* Get the initial row height. This is either the height of the
16050 text hscrolled, if there is any, or zero. */
16051 row->ascent = it->max_ascent;
16052 row->height = it->max_ascent + it->max_descent;
16053 row->phys_ascent = it->max_phys_ascent;
16054 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16055 row->extra_line_spacing = it->max_extra_line_spacing;
16056
16057 /* Loop generating characters. The loop is left with IT on the next
16058 character to display. */
16059 while (1)
16060 {
16061 int n_glyphs_before, hpos_before, x_before;
16062 int x, i, nglyphs;
16063 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16064
16065 /* Retrieve the next thing to display. Value is zero if end of
16066 buffer reached. */
16067 if (!get_next_display_element (it))
16068 {
16069 /* Maybe add a space at the end of this line that is used to
16070 display the cursor there under X. Set the charpos of the
16071 first glyph of blank lines not corresponding to any text
16072 to -1. */
16073 #ifdef HAVE_WINDOW_SYSTEM
16074 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16075 row->exact_window_width_line_p = 1;
16076 else
16077 #endif /* HAVE_WINDOW_SYSTEM */
16078 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16079 || row->used[TEXT_AREA] == 0)
16080 {
16081 row->glyphs[TEXT_AREA]->charpos = -1;
16082 row->displays_text_p = 0;
16083
16084 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16085 && (!MINI_WINDOW_P (it->w)
16086 || (minibuf_level && EQ (it->window, minibuf_window))))
16087 row->indicate_empty_line_p = 1;
16088 }
16089
16090 it->continuation_lines_width = 0;
16091 row->ends_at_zv_p = 1;
16092 break;
16093 }
16094
16095 /* Now, get the metrics of what we want to display. This also
16096 generates glyphs in `row' (which is IT->glyph_row). */
16097 n_glyphs_before = row->used[TEXT_AREA];
16098 x = it->current_x;
16099
16100 /* Remember the line height so far in case the next element doesn't
16101 fit on the line. */
16102 if (!it->truncate_lines_p)
16103 {
16104 ascent = it->max_ascent;
16105 descent = it->max_descent;
16106 phys_ascent = it->max_phys_ascent;
16107 phys_descent = it->max_phys_descent;
16108 }
16109
16110 PRODUCE_GLYPHS (it);
16111
16112 /* If this display element was in marginal areas, continue with
16113 the next one. */
16114 if (it->area != TEXT_AREA)
16115 {
16116 row->ascent = max (row->ascent, it->max_ascent);
16117 row->height = max (row->height, it->max_ascent + it->max_descent);
16118 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16119 row->phys_height = max (row->phys_height,
16120 it->max_phys_ascent + it->max_phys_descent);
16121 row->extra_line_spacing = max (row->extra_line_spacing,
16122 it->max_extra_line_spacing);
16123 set_iterator_to_next (it, 1);
16124 continue;
16125 }
16126
16127 /* Does the display element fit on the line? If we truncate
16128 lines, we should draw past the right edge of the window. If
16129 we don't truncate, we want to stop so that we can display the
16130 continuation glyph before the right margin. If lines are
16131 continued, there are two possible strategies for characters
16132 resulting in more than 1 glyph (e.g. tabs): Display as many
16133 glyphs as possible in this line and leave the rest for the
16134 continuation line, or display the whole element in the next
16135 line. Original redisplay did the former, so we do it also. */
16136 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16137 hpos_before = it->hpos;
16138 x_before = x;
16139
16140 if (/* Not a newline. */
16141 nglyphs > 0
16142 /* Glyphs produced fit entirely in the line. */
16143 && it->current_x < it->last_visible_x)
16144 {
16145 it->hpos += nglyphs;
16146 row->ascent = max (row->ascent, it->max_ascent);
16147 row->height = max (row->height, it->max_ascent + it->max_descent);
16148 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16149 row->phys_height = max (row->phys_height,
16150 it->max_phys_ascent + it->max_phys_descent);
16151 row->extra_line_spacing = max (row->extra_line_spacing,
16152 it->max_extra_line_spacing);
16153 if (it->current_x - it->pixel_width < it->first_visible_x)
16154 row->x = x - it->first_visible_x;
16155 }
16156 else
16157 {
16158 int new_x;
16159 struct glyph *glyph;
16160
16161 for (i = 0; i < nglyphs; ++i, x = new_x)
16162 {
16163 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16164 new_x = x + glyph->pixel_width;
16165
16166 if (/* Lines are continued. */
16167 !it->truncate_lines_p
16168 && (/* Glyph doesn't fit on the line. */
16169 new_x > it->last_visible_x
16170 /* Or it fits exactly on a window system frame. */
16171 || (new_x == it->last_visible_x
16172 && FRAME_WINDOW_P (it->f))))
16173 {
16174 /* End of a continued line. */
16175
16176 if (it->hpos == 0
16177 || (new_x == it->last_visible_x
16178 && FRAME_WINDOW_P (it->f)))
16179 {
16180 /* Current glyph is the only one on the line or
16181 fits exactly on the line. We must continue
16182 the line because we can't draw the cursor
16183 after the glyph. */
16184 row->continued_p = 1;
16185 it->current_x = new_x;
16186 it->continuation_lines_width += new_x;
16187 ++it->hpos;
16188 if (i == nglyphs - 1)
16189 {
16190 set_iterator_to_next (it, 1);
16191 #ifdef HAVE_WINDOW_SYSTEM
16192 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16193 {
16194 if (!get_next_display_element (it))
16195 {
16196 row->exact_window_width_line_p = 1;
16197 it->continuation_lines_width = 0;
16198 row->continued_p = 0;
16199 row->ends_at_zv_p = 1;
16200 }
16201 else if (ITERATOR_AT_END_OF_LINE_P (it))
16202 {
16203 row->continued_p = 0;
16204 row->exact_window_width_line_p = 1;
16205 }
16206 }
16207 #endif /* HAVE_WINDOW_SYSTEM */
16208 }
16209 }
16210 else if (CHAR_GLYPH_PADDING_P (*glyph)
16211 && !FRAME_WINDOW_P (it->f))
16212 {
16213 /* A padding glyph that doesn't fit on this line.
16214 This means the whole character doesn't fit
16215 on the line. */
16216 row->used[TEXT_AREA] = n_glyphs_before;
16217
16218 /* Fill the rest of the row with continuation
16219 glyphs like in 20.x. */
16220 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16221 < row->glyphs[1 + TEXT_AREA])
16222 produce_special_glyphs (it, IT_CONTINUATION);
16223
16224 row->continued_p = 1;
16225 it->current_x = x_before;
16226 it->continuation_lines_width += x_before;
16227
16228 /* Restore the height to what it was before the
16229 element not fitting on the line. */
16230 it->max_ascent = ascent;
16231 it->max_descent = descent;
16232 it->max_phys_ascent = phys_ascent;
16233 it->max_phys_descent = phys_descent;
16234 }
16235 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16236 {
16237 /* A TAB that extends past the right edge of the
16238 window. This produces a single glyph on
16239 window system frames. We leave the glyph in
16240 this row and let it fill the row, but don't
16241 consume the TAB. */
16242 it->continuation_lines_width += it->last_visible_x;
16243 row->ends_in_middle_of_char_p = 1;
16244 row->continued_p = 1;
16245 glyph->pixel_width = it->last_visible_x - x;
16246 it->starts_in_middle_of_char_p = 1;
16247 }
16248 else
16249 {
16250 /* Something other than a TAB that draws past
16251 the right edge of the window. Restore
16252 positions to values before the element. */
16253 row->used[TEXT_AREA] = n_glyphs_before + i;
16254
16255 /* Display continuation glyphs. */
16256 if (!FRAME_WINDOW_P (it->f))
16257 produce_special_glyphs (it, IT_CONTINUATION);
16258 row->continued_p = 1;
16259
16260 it->current_x = x_before;
16261 it->continuation_lines_width += x;
16262 extend_face_to_end_of_line (it);
16263
16264 if (nglyphs > 1 && i > 0)
16265 {
16266 row->ends_in_middle_of_char_p = 1;
16267 it->starts_in_middle_of_char_p = 1;
16268 }
16269
16270 /* Restore the height to what it was before the
16271 element not fitting on the line. */
16272 it->max_ascent = ascent;
16273 it->max_descent = descent;
16274 it->max_phys_ascent = phys_ascent;
16275 it->max_phys_descent = phys_descent;
16276 }
16277
16278 break;
16279 }
16280 else if (new_x > it->first_visible_x)
16281 {
16282 /* Increment number of glyphs actually displayed. */
16283 ++it->hpos;
16284
16285 if (x < it->first_visible_x)
16286 /* Glyph is partially visible, i.e. row starts at
16287 negative X position. */
16288 row->x = x - it->first_visible_x;
16289 }
16290 else
16291 {
16292 /* Glyph is completely off the left margin of the
16293 window. This should not happen because of the
16294 move_it_in_display_line at the start of this
16295 function, unless the text display area of the
16296 window is empty. */
16297 xassert (it->first_visible_x <= it->last_visible_x);
16298 }
16299 }
16300
16301 row->ascent = max (row->ascent, it->max_ascent);
16302 row->height = max (row->height, it->max_ascent + it->max_descent);
16303 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16304 row->phys_height = max (row->phys_height,
16305 it->max_phys_ascent + it->max_phys_descent);
16306 row->extra_line_spacing = max (row->extra_line_spacing,
16307 it->max_extra_line_spacing);
16308
16309 /* End of this display line if row is continued. */
16310 if (row->continued_p || row->ends_at_zv_p)
16311 break;
16312 }
16313
16314 at_end_of_line:
16315 /* Is this a line end? If yes, we're also done, after making
16316 sure that a non-default face is extended up to the right
16317 margin of the window. */
16318 if (ITERATOR_AT_END_OF_LINE_P (it))
16319 {
16320 int used_before = row->used[TEXT_AREA];
16321
16322 row->ends_in_newline_from_string_p = STRINGP (it->object);
16323
16324 #ifdef HAVE_WINDOW_SYSTEM
16325 /* Add a space at the end of the line that is used to
16326 display the cursor there. */
16327 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16328 append_space_for_newline (it, 0);
16329 #endif /* HAVE_WINDOW_SYSTEM */
16330
16331 /* Extend the face to the end of the line. */
16332 extend_face_to_end_of_line (it);
16333
16334 /* Make sure we have the position. */
16335 if (used_before == 0)
16336 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16337
16338 /* Consume the line end. This skips over invisible lines. */
16339 set_iterator_to_next (it, 1);
16340 it->continuation_lines_width = 0;
16341 break;
16342 }
16343
16344 /* Proceed with next display element. Note that this skips
16345 over lines invisible because of selective display. */
16346 set_iterator_to_next (it, 1);
16347
16348 /* If we truncate lines, we are done when the last displayed
16349 glyphs reach past the right margin of the window. */
16350 if (it->truncate_lines_p
16351 && (FRAME_WINDOW_P (it->f)
16352 ? (it->current_x >= it->last_visible_x)
16353 : (it->current_x > it->last_visible_x)))
16354 {
16355 /* Maybe add truncation glyphs. */
16356 if (!FRAME_WINDOW_P (it->f))
16357 {
16358 int i, n;
16359
16360 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16361 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16362 break;
16363
16364 for (n = row->used[TEXT_AREA]; i < n; ++i)
16365 {
16366 row->used[TEXT_AREA] = i;
16367 produce_special_glyphs (it, IT_TRUNCATION);
16368 }
16369 }
16370 #ifdef HAVE_WINDOW_SYSTEM
16371 else
16372 {
16373 /* Don't truncate if we can overflow newline into fringe. */
16374 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16375 {
16376 if (!get_next_display_element (it))
16377 {
16378 it->continuation_lines_width = 0;
16379 row->ends_at_zv_p = 1;
16380 row->exact_window_width_line_p = 1;
16381 break;
16382 }
16383 if (ITERATOR_AT_END_OF_LINE_P (it))
16384 {
16385 row->exact_window_width_line_p = 1;
16386 goto at_end_of_line;
16387 }
16388 }
16389 }
16390 #endif /* HAVE_WINDOW_SYSTEM */
16391
16392 row->truncated_on_right_p = 1;
16393 it->continuation_lines_width = 0;
16394 reseat_at_next_visible_line_start (it, 0);
16395 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16396 it->hpos = hpos_before;
16397 it->current_x = x_before;
16398 break;
16399 }
16400 }
16401
16402 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16403 at the left window margin. */
16404 if (it->first_visible_x
16405 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16406 {
16407 if (!FRAME_WINDOW_P (it->f))
16408 insert_left_trunc_glyphs (it);
16409 row->truncated_on_left_p = 1;
16410 }
16411
16412 /* If the start of this line is the overlay arrow-position, then
16413 mark this glyph row as the one containing the overlay arrow.
16414 This is clearly a mess with variable size fonts. It would be
16415 better to let it be displayed like cursors under X. */
16416 if ((row->displays_text_p || !overlay_arrow_seen)
16417 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16418 !NILP (overlay_arrow_string)))
16419 {
16420 /* Overlay arrow in window redisplay is a fringe bitmap. */
16421 if (STRINGP (overlay_arrow_string))
16422 {
16423 struct glyph_row *arrow_row
16424 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16425 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16426 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16427 struct glyph *p = row->glyphs[TEXT_AREA];
16428 struct glyph *p2, *end;
16429
16430 /* Copy the arrow glyphs. */
16431 while (glyph < arrow_end)
16432 *p++ = *glyph++;
16433
16434 /* Throw away padding glyphs. */
16435 p2 = p;
16436 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16437 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16438 ++p2;
16439 if (p2 > p)
16440 {
16441 while (p2 < end)
16442 *p++ = *p2++;
16443 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16444 }
16445 }
16446 else
16447 {
16448 xassert (INTEGERP (overlay_arrow_string));
16449 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16450 }
16451 overlay_arrow_seen = 1;
16452 }
16453
16454 /* Compute pixel dimensions of this line. */
16455 compute_line_metrics (it);
16456
16457 /* Remember the position at which this line ends. */
16458 row->end = it->current;
16459
16460 /* Record whether this row ends inside an ellipsis. */
16461 row->ends_in_ellipsis_p
16462 = (it->method == GET_FROM_DISPLAY_VECTOR
16463 && it->ellipsis_p);
16464
16465 /* Save fringe bitmaps in this row. */
16466 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16467 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16468 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16469 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16470
16471 it->left_user_fringe_bitmap = 0;
16472 it->left_user_fringe_face_id = 0;
16473 it->right_user_fringe_bitmap = 0;
16474 it->right_user_fringe_face_id = 0;
16475
16476 /* Maybe set the cursor. */
16477 if (it->w->cursor.vpos < 0
16478 && PT >= MATRIX_ROW_START_CHARPOS (row)
16479 && PT <= MATRIX_ROW_END_CHARPOS (row)
16480 && cursor_row_p (it->w, row))
16481 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16482
16483 /* Highlight trailing whitespace. */
16484 if (!NILP (Vshow_trailing_whitespace))
16485 highlight_trailing_whitespace (it->f, it->glyph_row);
16486
16487 /* Prepare for the next line. This line starts horizontally at (X
16488 HPOS) = (0 0). Vertical positions are incremented. As a
16489 convenience for the caller, IT->glyph_row is set to the next
16490 row to be used. */
16491 it->current_x = it->hpos = 0;
16492 it->current_y += row->height;
16493 ++it->vpos;
16494 ++it->glyph_row;
16495 it->start = it->current;
16496 return row->displays_text_p;
16497 }
16498
16499
16500 \f
16501 /***********************************************************************
16502 Menu Bar
16503 ***********************************************************************/
16504
16505 /* Redisplay the menu bar in the frame for window W.
16506
16507 The menu bar of X frames that don't have X toolkit support is
16508 displayed in a special window W->frame->menu_bar_window.
16509
16510 The menu bar of terminal frames is treated specially as far as
16511 glyph matrices are concerned. Menu bar lines are not part of
16512 windows, so the update is done directly on the frame matrix rows
16513 for the menu bar. */
16514
16515 static void
16516 display_menu_bar (w)
16517 struct window *w;
16518 {
16519 struct frame *f = XFRAME (WINDOW_FRAME (w));
16520 struct it it;
16521 Lisp_Object items;
16522 int i;
16523
16524 /* Don't do all this for graphical frames. */
16525 #ifdef HAVE_NTGUI
16526 if (!NILP (Vwindow_system))
16527 return;
16528 #endif
16529 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16530 if (FRAME_X_P (f))
16531 return;
16532 #endif
16533 #ifdef MAC_OS
16534 if (FRAME_MAC_P (f))
16535 return;
16536 #endif
16537
16538 #ifdef USE_X_TOOLKIT
16539 xassert (!FRAME_WINDOW_P (f));
16540 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16541 it.first_visible_x = 0;
16542 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16543 #else /* not USE_X_TOOLKIT */
16544 if (FRAME_WINDOW_P (f))
16545 {
16546 /* Menu bar lines are displayed in the desired matrix of the
16547 dummy window menu_bar_window. */
16548 struct window *menu_w;
16549 xassert (WINDOWP (f->menu_bar_window));
16550 menu_w = XWINDOW (f->menu_bar_window);
16551 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16552 MENU_FACE_ID);
16553 it.first_visible_x = 0;
16554 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16555 }
16556 else
16557 {
16558 /* This is a TTY frame, i.e. character hpos/vpos are used as
16559 pixel x/y. */
16560 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16561 MENU_FACE_ID);
16562 it.first_visible_x = 0;
16563 it.last_visible_x = FRAME_COLS (f);
16564 }
16565 #endif /* not USE_X_TOOLKIT */
16566
16567 if (! mode_line_inverse_video)
16568 /* Force the menu-bar to be displayed in the default face. */
16569 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16570
16571 /* Clear all rows of the menu bar. */
16572 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16573 {
16574 struct glyph_row *row = it.glyph_row + i;
16575 clear_glyph_row (row);
16576 row->enabled_p = 1;
16577 row->full_width_p = 1;
16578 }
16579
16580 /* Display all items of the menu bar. */
16581 items = FRAME_MENU_BAR_ITEMS (it.f);
16582 for (i = 0; i < XVECTOR (items)->size; i += 4)
16583 {
16584 Lisp_Object string;
16585
16586 /* Stop at nil string. */
16587 string = AREF (items, i + 1);
16588 if (NILP (string))
16589 break;
16590
16591 /* Remember where item was displayed. */
16592 AREF (items, i + 3) = make_number (it.hpos);
16593
16594 /* Display the item, pad with one space. */
16595 if (it.current_x < it.last_visible_x)
16596 display_string (NULL, string, Qnil, 0, 0, &it,
16597 SCHARS (string) + 1, 0, 0, -1);
16598 }
16599
16600 /* Fill out the line with spaces. */
16601 if (it.current_x < it.last_visible_x)
16602 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16603
16604 /* Compute the total height of the lines. */
16605 compute_line_metrics (&it);
16606 }
16607
16608
16609 \f
16610 /***********************************************************************
16611 Mode Line
16612 ***********************************************************************/
16613
16614 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16615 FORCE is non-zero, redisplay mode lines unconditionally.
16616 Otherwise, redisplay only mode lines that are garbaged. Value is
16617 the number of windows whose mode lines were redisplayed. */
16618
16619 static int
16620 redisplay_mode_lines (window, force)
16621 Lisp_Object window;
16622 int force;
16623 {
16624 int nwindows = 0;
16625
16626 while (!NILP (window))
16627 {
16628 struct window *w = XWINDOW (window);
16629
16630 if (WINDOWP (w->hchild))
16631 nwindows += redisplay_mode_lines (w->hchild, force);
16632 else if (WINDOWP (w->vchild))
16633 nwindows += redisplay_mode_lines (w->vchild, force);
16634 else if (force
16635 || FRAME_GARBAGED_P (XFRAME (w->frame))
16636 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16637 {
16638 struct text_pos lpoint;
16639 struct buffer *old = current_buffer;
16640
16641 /* Set the window's buffer for the mode line display. */
16642 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16643 set_buffer_internal_1 (XBUFFER (w->buffer));
16644
16645 /* Point refers normally to the selected window. For any
16646 other window, set up appropriate value. */
16647 if (!EQ (window, selected_window))
16648 {
16649 struct text_pos pt;
16650
16651 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16652 if (CHARPOS (pt) < BEGV)
16653 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16654 else if (CHARPOS (pt) > (ZV - 1))
16655 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16656 else
16657 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16658 }
16659
16660 /* Display mode lines. */
16661 clear_glyph_matrix (w->desired_matrix);
16662 if (display_mode_lines (w))
16663 {
16664 ++nwindows;
16665 w->must_be_updated_p = 1;
16666 }
16667
16668 /* Restore old settings. */
16669 set_buffer_internal_1 (old);
16670 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16671 }
16672
16673 window = w->next;
16674 }
16675
16676 return nwindows;
16677 }
16678
16679
16680 /* Display the mode and/or top line of window W. Value is the number
16681 of mode lines displayed. */
16682
16683 static int
16684 display_mode_lines (w)
16685 struct window *w;
16686 {
16687 Lisp_Object old_selected_window, old_selected_frame;
16688 int n = 0;
16689
16690 old_selected_frame = selected_frame;
16691 selected_frame = w->frame;
16692 old_selected_window = selected_window;
16693 XSETWINDOW (selected_window, w);
16694
16695 /* These will be set while the mode line specs are processed. */
16696 line_number_displayed = 0;
16697 w->column_number_displayed = Qnil;
16698
16699 if (WINDOW_WANTS_MODELINE_P (w))
16700 {
16701 struct window *sel_w = XWINDOW (old_selected_window);
16702
16703 /* Select mode line face based on the real selected window. */
16704 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16705 current_buffer->mode_line_format);
16706 ++n;
16707 }
16708
16709 if (WINDOW_WANTS_HEADER_LINE_P (w))
16710 {
16711 display_mode_line (w, HEADER_LINE_FACE_ID,
16712 current_buffer->header_line_format);
16713 ++n;
16714 }
16715
16716 selected_frame = old_selected_frame;
16717 selected_window = old_selected_window;
16718 return n;
16719 }
16720
16721
16722 /* Display mode or top line of window W. FACE_ID specifies which line
16723 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16724 FORMAT is the mode line format to display. Value is the pixel
16725 height of the mode line displayed. */
16726
16727 static int
16728 display_mode_line (w, face_id, format)
16729 struct window *w;
16730 enum face_id face_id;
16731 Lisp_Object format;
16732 {
16733 struct it it;
16734 struct face *face;
16735 int count = SPECPDL_INDEX ();
16736
16737 init_iterator (&it, w, -1, -1, NULL, face_id);
16738 /* Don't extend on a previously drawn mode-line.
16739 This may happen if called from pos_visible_p. */
16740 it.glyph_row->enabled_p = 0;
16741 prepare_desired_row (it.glyph_row);
16742
16743 it.glyph_row->mode_line_p = 1;
16744
16745 if (! mode_line_inverse_video)
16746 /* Force the mode-line to be displayed in the default face. */
16747 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16748
16749 record_unwind_protect (unwind_format_mode_line,
16750 format_mode_line_unwind_data (NULL, 0));
16751
16752 mode_line_target = MODE_LINE_DISPLAY;
16753
16754 /* Temporarily make frame's keyboard the current kboard so that
16755 kboard-local variables in the mode_line_format will get the right
16756 values. */
16757 push_frame_kboard (it.f);
16758 record_unwind_save_match_data ();
16759 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16760 pop_frame_kboard ();
16761
16762 unbind_to (count, Qnil);
16763
16764 /* Fill up with spaces. */
16765 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16766
16767 compute_line_metrics (&it);
16768 it.glyph_row->full_width_p = 1;
16769 it.glyph_row->continued_p = 0;
16770 it.glyph_row->truncated_on_left_p = 0;
16771 it.glyph_row->truncated_on_right_p = 0;
16772
16773 /* Make a 3D mode-line have a shadow at its right end. */
16774 face = FACE_FROM_ID (it.f, face_id);
16775 extend_face_to_end_of_line (&it);
16776 if (face->box != FACE_NO_BOX)
16777 {
16778 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16779 + it.glyph_row->used[TEXT_AREA] - 1);
16780 last->right_box_line_p = 1;
16781 }
16782
16783 return it.glyph_row->height;
16784 }
16785
16786 /* Move element ELT in LIST to the front of LIST.
16787 Return the updated list. */
16788
16789 static Lisp_Object
16790 move_elt_to_front (elt, list)
16791 Lisp_Object elt, list;
16792 {
16793 register Lisp_Object tail, prev;
16794 register Lisp_Object tem;
16795
16796 tail = list;
16797 prev = Qnil;
16798 while (CONSP (tail))
16799 {
16800 tem = XCAR (tail);
16801
16802 if (EQ (elt, tem))
16803 {
16804 /* Splice out the link TAIL. */
16805 if (NILP (prev))
16806 list = XCDR (tail);
16807 else
16808 Fsetcdr (prev, XCDR (tail));
16809
16810 /* Now make it the first. */
16811 Fsetcdr (tail, list);
16812 return tail;
16813 }
16814 else
16815 prev = tail;
16816 tail = XCDR (tail);
16817 QUIT;
16818 }
16819
16820 /* Not found--return unchanged LIST. */
16821 return list;
16822 }
16823
16824 /* Contribute ELT to the mode line for window IT->w. How it
16825 translates into text depends on its data type.
16826
16827 IT describes the display environment in which we display, as usual.
16828
16829 DEPTH is the depth in recursion. It is used to prevent
16830 infinite recursion here.
16831
16832 FIELD_WIDTH is the number of characters the display of ELT should
16833 occupy in the mode line, and PRECISION is the maximum number of
16834 characters to display from ELT's representation. See
16835 display_string for details.
16836
16837 Returns the hpos of the end of the text generated by ELT.
16838
16839 PROPS is a property list to add to any string we encounter.
16840
16841 If RISKY is nonzero, remove (disregard) any properties in any string
16842 we encounter, and ignore :eval and :propertize.
16843
16844 The global variable `mode_line_target' determines whether the
16845 output is passed to `store_mode_line_noprop',
16846 `store_mode_line_string', or `display_string'. */
16847
16848 static int
16849 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16850 struct it *it;
16851 int depth;
16852 int field_width, precision;
16853 Lisp_Object elt, props;
16854 int risky;
16855 {
16856 int n = 0, field, prec;
16857 int literal = 0;
16858
16859 tail_recurse:
16860 if (depth > 100)
16861 elt = build_string ("*too-deep*");
16862
16863 depth++;
16864
16865 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16866 {
16867 case Lisp_String:
16868 {
16869 /* A string: output it and check for %-constructs within it. */
16870 unsigned char c;
16871 int offset = 0;
16872
16873 if (SCHARS (elt) > 0
16874 && (!NILP (props) || risky))
16875 {
16876 Lisp_Object oprops, aelt;
16877 oprops = Ftext_properties_at (make_number (0), elt);
16878
16879 /* If the starting string's properties are not what
16880 we want, translate the string. Also, if the string
16881 is risky, do that anyway. */
16882
16883 if (NILP (Fequal (props, oprops)) || risky)
16884 {
16885 /* If the starting string has properties,
16886 merge the specified ones onto the existing ones. */
16887 if (! NILP (oprops) && !risky)
16888 {
16889 Lisp_Object tem;
16890
16891 oprops = Fcopy_sequence (oprops);
16892 tem = props;
16893 while (CONSP (tem))
16894 {
16895 oprops = Fplist_put (oprops, XCAR (tem),
16896 XCAR (XCDR (tem)));
16897 tem = XCDR (XCDR (tem));
16898 }
16899 props = oprops;
16900 }
16901
16902 aelt = Fassoc (elt, mode_line_proptrans_alist);
16903 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16904 {
16905 /* AELT is what we want. Move it to the front
16906 without consing. */
16907 elt = XCAR (aelt);
16908 mode_line_proptrans_alist
16909 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16910 }
16911 else
16912 {
16913 Lisp_Object tem;
16914
16915 /* If AELT has the wrong props, it is useless.
16916 so get rid of it. */
16917 if (! NILP (aelt))
16918 mode_line_proptrans_alist
16919 = Fdelq (aelt, mode_line_proptrans_alist);
16920
16921 elt = Fcopy_sequence (elt);
16922 Fset_text_properties (make_number (0), Flength (elt),
16923 props, elt);
16924 /* Add this item to mode_line_proptrans_alist. */
16925 mode_line_proptrans_alist
16926 = Fcons (Fcons (elt, props),
16927 mode_line_proptrans_alist);
16928 /* Truncate mode_line_proptrans_alist
16929 to at most 50 elements. */
16930 tem = Fnthcdr (make_number (50),
16931 mode_line_proptrans_alist);
16932 if (! NILP (tem))
16933 XSETCDR (tem, Qnil);
16934 }
16935 }
16936 }
16937
16938 offset = 0;
16939
16940 if (literal)
16941 {
16942 prec = precision - n;
16943 switch (mode_line_target)
16944 {
16945 case MODE_LINE_NOPROP:
16946 case MODE_LINE_TITLE:
16947 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16948 break;
16949 case MODE_LINE_STRING:
16950 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16951 break;
16952 case MODE_LINE_DISPLAY:
16953 n += display_string (NULL, elt, Qnil, 0, 0, it,
16954 0, prec, 0, STRING_MULTIBYTE (elt));
16955 break;
16956 }
16957
16958 break;
16959 }
16960
16961 /* Handle the non-literal case. */
16962
16963 while ((precision <= 0 || n < precision)
16964 && SREF (elt, offset) != 0
16965 && (mode_line_target != MODE_LINE_DISPLAY
16966 || it->current_x < it->last_visible_x))
16967 {
16968 int last_offset = offset;
16969
16970 /* Advance to end of string or next format specifier. */
16971 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16972 ;
16973
16974 if (offset - 1 != last_offset)
16975 {
16976 int nchars, nbytes;
16977
16978 /* Output to end of string or up to '%'. Field width
16979 is length of string. Don't output more than
16980 PRECISION allows us. */
16981 offset--;
16982
16983 prec = c_string_width (SDATA (elt) + last_offset,
16984 offset - last_offset, precision - n,
16985 &nchars, &nbytes);
16986
16987 switch (mode_line_target)
16988 {
16989 case MODE_LINE_NOPROP:
16990 case MODE_LINE_TITLE:
16991 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16992 break;
16993 case MODE_LINE_STRING:
16994 {
16995 int bytepos = last_offset;
16996 int charpos = string_byte_to_char (elt, bytepos);
16997 int endpos = (precision <= 0
16998 ? string_byte_to_char (elt, offset)
16999 : charpos + nchars);
17000
17001 n += store_mode_line_string (NULL,
17002 Fsubstring (elt, make_number (charpos),
17003 make_number (endpos)),
17004 0, 0, 0, Qnil);
17005 }
17006 break;
17007 case MODE_LINE_DISPLAY:
17008 {
17009 int bytepos = last_offset;
17010 int charpos = string_byte_to_char (elt, bytepos);
17011
17012 if (precision <= 0)
17013 nchars = string_byte_to_char (elt, offset) - charpos;
17014 n += display_string (NULL, elt, Qnil, 0, charpos,
17015 it, 0, nchars, 0,
17016 STRING_MULTIBYTE (elt));
17017 }
17018 break;
17019 }
17020 }
17021 else /* c == '%' */
17022 {
17023 int percent_position = offset;
17024
17025 /* Get the specified minimum width. Zero means
17026 don't pad. */
17027 field = 0;
17028 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17029 field = field * 10 + c - '0';
17030
17031 /* Don't pad beyond the total padding allowed. */
17032 if (field_width - n > 0 && field > field_width - n)
17033 field = field_width - n;
17034
17035 /* Note that either PRECISION <= 0 or N < PRECISION. */
17036 prec = precision - n;
17037
17038 if (c == 'M')
17039 n += display_mode_element (it, depth, field, prec,
17040 Vglobal_mode_string, props,
17041 risky);
17042 else if (c != 0)
17043 {
17044 int multibyte;
17045 int bytepos, charpos;
17046 unsigned char *spec;
17047
17048 bytepos = percent_position;
17049 charpos = (STRING_MULTIBYTE (elt)
17050 ? string_byte_to_char (elt, bytepos)
17051 : bytepos);
17052
17053 spec
17054 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17055
17056 switch (mode_line_target)
17057 {
17058 case MODE_LINE_NOPROP:
17059 case MODE_LINE_TITLE:
17060 n += store_mode_line_noprop (spec, field, prec);
17061 break;
17062 case MODE_LINE_STRING:
17063 {
17064 int len = strlen (spec);
17065 Lisp_Object tem = make_string (spec, len);
17066 props = Ftext_properties_at (make_number (charpos), elt);
17067 /* Should only keep face property in props */
17068 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17069 }
17070 break;
17071 case MODE_LINE_DISPLAY:
17072 {
17073 int nglyphs_before, nwritten;
17074
17075 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17076 nwritten = display_string (spec, Qnil, elt,
17077 charpos, 0, it,
17078 field, prec, 0,
17079 multibyte);
17080
17081 /* Assign to the glyphs written above the
17082 string where the `%x' came from, position
17083 of the `%'. */
17084 if (nwritten > 0)
17085 {
17086 struct glyph *glyph
17087 = (it->glyph_row->glyphs[TEXT_AREA]
17088 + nglyphs_before);
17089 int i;
17090
17091 for (i = 0; i < nwritten; ++i)
17092 {
17093 glyph[i].object = elt;
17094 glyph[i].charpos = charpos;
17095 }
17096
17097 n += nwritten;
17098 }
17099 }
17100 break;
17101 }
17102 }
17103 else /* c == 0 */
17104 break;
17105 }
17106 }
17107 }
17108 break;
17109
17110 case Lisp_Symbol:
17111 /* A symbol: process the value of the symbol recursively
17112 as if it appeared here directly. Avoid error if symbol void.
17113 Special case: if value of symbol is a string, output the string
17114 literally. */
17115 {
17116 register Lisp_Object tem;
17117
17118 /* If the variable is not marked as risky to set
17119 then its contents are risky to use. */
17120 if (NILP (Fget (elt, Qrisky_local_variable)))
17121 risky = 1;
17122
17123 tem = Fboundp (elt);
17124 if (!NILP (tem))
17125 {
17126 tem = Fsymbol_value (elt);
17127 /* If value is a string, output that string literally:
17128 don't check for % within it. */
17129 if (STRINGP (tem))
17130 literal = 1;
17131
17132 if (!EQ (tem, elt))
17133 {
17134 /* Give up right away for nil or t. */
17135 elt = tem;
17136 goto tail_recurse;
17137 }
17138 }
17139 }
17140 break;
17141
17142 case Lisp_Cons:
17143 {
17144 register Lisp_Object car, tem;
17145
17146 /* A cons cell: five distinct cases.
17147 If first element is :eval or :propertize, do something special.
17148 If first element is a string or a cons, process all the elements
17149 and effectively concatenate them.
17150 If first element is a negative number, truncate displaying cdr to
17151 at most that many characters. If positive, pad (with spaces)
17152 to at least that many characters.
17153 If first element is a symbol, process the cadr or caddr recursively
17154 according to whether the symbol's value is non-nil or nil. */
17155 car = XCAR (elt);
17156 if (EQ (car, QCeval))
17157 {
17158 /* An element of the form (:eval FORM) means evaluate FORM
17159 and use the result as mode line elements. */
17160
17161 if (risky)
17162 break;
17163
17164 if (CONSP (XCDR (elt)))
17165 {
17166 Lisp_Object spec;
17167 spec = safe_eval (XCAR (XCDR (elt)));
17168 n += display_mode_element (it, depth, field_width - n,
17169 precision - n, spec, props,
17170 risky);
17171 }
17172 }
17173 else if (EQ (car, QCpropertize))
17174 {
17175 /* An element of the form (:propertize ELT PROPS...)
17176 means display ELT but applying properties PROPS. */
17177
17178 if (risky)
17179 break;
17180
17181 if (CONSP (XCDR (elt)))
17182 n += display_mode_element (it, depth, field_width - n,
17183 precision - n, XCAR (XCDR (elt)),
17184 XCDR (XCDR (elt)), risky);
17185 }
17186 else if (SYMBOLP (car))
17187 {
17188 tem = Fboundp (car);
17189 elt = XCDR (elt);
17190 if (!CONSP (elt))
17191 goto invalid;
17192 /* elt is now the cdr, and we know it is a cons cell.
17193 Use its car if CAR has a non-nil value. */
17194 if (!NILP (tem))
17195 {
17196 tem = Fsymbol_value (car);
17197 if (!NILP (tem))
17198 {
17199 elt = XCAR (elt);
17200 goto tail_recurse;
17201 }
17202 }
17203 /* Symbol's value is nil (or symbol is unbound)
17204 Get the cddr of the original list
17205 and if possible find the caddr and use that. */
17206 elt = XCDR (elt);
17207 if (NILP (elt))
17208 break;
17209 else if (!CONSP (elt))
17210 goto invalid;
17211 elt = XCAR (elt);
17212 goto tail_recurse;
17213 }
17214 else if (INTEGERP (car))
17215 {
17216 register int lim = XINT (car);
17217 elt = XCDR (elt);
17218 if (lim < 0)
17219 {
17220 /* Negative int means reduce maximum width. */
17221 if (precision <= 0)
17222 precision = -lim;
17223 else
17224 precision = min (precision, -lim);
17225 }
17226 else if (lim > 0)
17227 {
17228 /* Padding specified. Don't let it be more than
17229 current maximum. */
17230 if (precision > 0)
17231 lim = min (precision, lim);
17232
17233 /* If that's more padding than already wanted, queue it.
17234 But don't reduce padding already specified even if
17235 that is beyond the current truncation point. */
17236 field_width = max (lim, field_width);
17237 }
17238 goto tail_recurse;
17239 }
17240 else if (STRINGP (car) || CONSP (car))
17241 {
17242 register int limit = 50;
17243 /* Limit is to protect against circular lists. */
17244 while (CONSP (elt)
17245 && --limit > 0
17246 && (precision <= 0 || n < precision))
17247 {
17248 n += display_mode_element (it, depth,
17249 /* Do padding only after the last
17250 element in the list. */
17251 (! CONSP (XCDR (elt))
17252 ? field_width - n
17253 : 0),
17254 precision - n, XCAR (elt),
17255 props, risky);
17256 elt = XCDR (elt);
17257 }
17258 }
17259 }
17260 break;
17261
17262 default:
17263 invalid:
17264 elt = build_string ("*invalid*");
17265 goto tail_recurse;
17266 }
17267
17268 /* Pad to FIELD_WIDTH. */
17269 if (field_width > 0 && n < field_width)
17270 {
17271 switch (mode_line_target)
17272 {
17273 case MODE_LINE_NOPROP:
17274 case MODE_LINE_TITLE:
17275 n += store_mode_line_noprop ("", field_width - n, 0);
17276 break;
17277 case MODE_LINE_STRING:
17278 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17279 break;
17280 case MODE_LINE_DISPLAY:
17281 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17282 0, 0, 0);
17283 break;
17284 }
17285 }
17286
17287 return n;
17288 }
17289
17290 /* Store a mode-line string element in mode_line_string_list.
17291
17292 If STRING is non-null, display that C string. Otherwise, the Lisp
17293 string LISP_STRING is displayed.
17294
17295 FIELD_WIDTH is the minimum number of output glyphs to produce.
17296 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17297 with spaces. FIELD_WIDTH <= 0 means don't pad.
17298
17299 PRECISION is the maximum number of characters to output from
17300 STRING. PRECISION <= 0 means don't truncate the string.
17301
17302 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17303 properties to the string.
17304
17305 PROPS are the properties to add to the string.
17306 The mode_line_string_face face property is always added to the string.
17307 */
17308
17309 static int
17310 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17311 char *string;
17312 Lisp_Object lisp_string;
17313 int copy_string;
17314 int field_width;
17315 int precision;
17316 Lisp_Object props;
17317 {
17318 int len;
17319 int n = 0;
17320
17321 if (string != NULL)
17322 {
17323 len = strlen (string);
17324 if (precision > 0 && len > precision)
17325 len = precision;
17326 lisp_string = make_string (string, len);
17327 if (NILP (props))
17328 props = mode_line_string_face_prop;
17329 else if (!NILP (mode_line_string_face))
17330 {
17331 Lisp_Object face = Fplist_get (props, Qface);
17332 props = Fcopy_sequence (props);
17333 if (NILP (face))
17334 face = mode_line_string_face;
17335 else
17336 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17337 props = Fplist_put (props, Qface, face);
17338 }
17339 Fadd_text_properties (make_number (0), make_number (len),
17340 props, lisp_string);
17341 }
17342 else
17343 {
17344 len = XFASTINT (Flength (lisp_string));
17345 if (precision > 0 && len > precision)
17346 {
17347 len = precision;
17348 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17349 precision = -1;
17350 }
17351 if (!NILP (mode_line_string_face))
17352 {
17353 Lisp_Object face;
17354 if (NILP (props))
17355 props = Ftext_properties_at (make_number (0), lisp_string);
17356 face = Fplist_get (props, Qface);
17357 if (NILP (face))
17358 face = mode_line_string_face;
17359 else
17360 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17361 props = Fcons (Qface, Fcons (face, Qnil));
17362 if (copy_string)
17363 lisp_string = Fcopy_sequence (lisp_string);
17364 }
17365 if (!NILP (props))
17366 Fadd_text_properties (make_number (0), make_number (len),
17367 props, lisp_string);
17368 }
17369
17370 if (len > 0)
17371 {
17372 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17373 n += len;
17374 }
17375
17376 if (field_width > len)
17377 {
17378 field_width -= len;
17379 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17380 if (!NILP (props))
17381 Fadd_text_properties (make_number (0), make_number (field_width),
17382 props, lisp_string);
17383 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17384 n += field_width;
17385 }
17386
17387 return n;
17388 }
17389
17390
17391 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17392 1, 4, 0,
17393 doc: /* Format a string out of a mode line format specification.
17394 First arg FORMAT specifies the mode line format (see `mode-line-format'
17395 for details) to use.
17396
17397 Optional second arg FACE specifies the face property to put
17398 on all characters for which no face is specified.
17399 The value t means whatever face the window's mode line currently uses
17400 \(either `mode-line' or `mode-line-inactive', depending).
17401 A value of nil means the default is no face property.
17402 If FACE is an integer, the value string has no text properties.
17403
17404 Optional third and fourth args WINDOW and BUFFER specify the window
17405 and buffer to use as the context for the formatting (defaults
17406 are the selected window and the window's buffer). */)
17407 (format, face, window, buffer)
17408 Lisp_Object format, face, window, buffer;
17409 {
17410 struct it it;
17411 int len;
17412 struct window *w;
17413 struct buffer *old_buffer = NULL;
17414 int face_id = -1;
17415 int no_props = INTEGERP (face);
17416 int count = SPECPDL_INDEX ();
17417 Lisp_Object str;
17418 int string_start = 0;
17419
17420 if (NILP (window))
17421 window = selected_window;
17422 CHECK_WINDOW (window);
17423 w = XWINDOW (window);
17424
17425 if (NILP (buffer))
17426 buffer = w->buffer;
17427 CHECK_BUFFER (buffer);
17428
17429 /* Make formatting the modeline a non-op when noninteractive, otherwise
17430 there will be problems later caused by a partially initialized frame. */
17431 if (NILP (format) || noninteractive)
17432 return build_string ("");
17433
17434 if (no_props)
17435 face = Qnil;
17436
17437 if (!NILP (face))
17438 {
17439 if (EQ (face, Qt))
17440 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17441 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17442 }
17443
17444 if (face_id < 0)
17445 face_id = DEFAULT_FACE_ID;
17446
17447 if (XBUFFER (buffer) != current_buffer)
17448 old_buffer = current_buffer;
17449
17450 /* Save things including mode_line_proptrans_alist,
17451 and set that to nil so that we don't alter the outer value. */
17452 record_unwind_protect (unwind_format_mode_line,
17453 format_mode_line_unwind_data (old_buffer, 1));
17454 mode_line_proptrans_alist = Qnil;
17455
17456 if (old_buffer)
17457 set_buffer_internal_1 (XBUFFER (buffer));
17458
17459 init_iterator (&it, w, -1, -1, NULL, face_id);
17460
17461 if (no_props)
17462 {
17463 mode_line_target = MODE_LINE_NOPROP;
17464 mode_line_string_face_prop = Qnil;
17465 mode_line_string_list = Qnil;
17466 string_start = MODE_LINE_NOPROP_LEN (0);
17467 }
17468 else
17469 {
17470 mode_line_target = MODE_LINE_STRING;
17471 mode_line_string_list = Qnil;
17472 mode_line_string_face = face;
17473 mode_line_string_face_prop
17474 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17475 }
17476
17477 push_frame_kboard (it.f);
17478 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17479 pop_frame_kboard ();
17480
17481 if (no_props)
17482 {
17483 len = MODE_LINE_NOPROP_LEN (string_start);
17484 str = make_string (mode_line_noprop_buf + string_start, len);
17485 }
17486 else
17487 {
17488 mode_line_string_list = Fnreverse (mode_line_string_list);
17489 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17490 make_string ("", 0));
17491 }
17492
17493 unbind_to (count, Qnil);
17494 return str;
17495 }
17496
17497 /* Write a null-terminated, right justified decimal representation of
17498 the positive integer D to BUF using a minimal field width WIDTH. */
17499
17500 static void
17501 pint2str (buf, width, d)
17502 register char *buf;
17503 register int width;
17504 register int d;
17505 {
17506 register char *p = buf;
17507
17508 if (d <= 0)
17509 *p++ = '0';
17510 else
17511 {
17512 while (d > 0)
17513 {
17514 *p++ = d % 10 + '0';
17515 d /= 10;
17516 }
17517 }
17518
17519 for (width -= (int) (p - buf); width > 0; --width)
17520 *p++ = ' ';
17521 *p-- = '\0';
17522 while (p > buf)
17523 {
17524 d = *buf;
17525 *buf++ = *p;
17526 *p-- = d;
17527 }
17528 }
17529
17530 /* Write a null-terminated, right justified decimal and "human
17531 readable" representation of the nonnegative integer D to BUF using
17532 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17533
17534 static const char power_letter[] =
17535 {
17536 0, /* not used */
17537 'k', /* kilo */
17538 'M', /* mega */
17539 'G', /* giga */
17540 'T', /* tera */
17541 'P', /* peta */
17542 'E', /* exa */
17543 'Z', /* zetta */
17544 'Y' /* yotta */
17545 };
17546
17547 static void
17548 pint2hrstr (buf, width, d)
17549 char *buf;
17550 int width;
17551 int d;
17552 {
17553 /* We aim to represent the nonnegative integer D as
17554 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17555 int quotient = d;
17556 int remainder = 0;
17557 /* -1 means: do not use TENTHS. */
17558 int tenths = -1;
17559 int exponent = 0;
17560
17561 /* Length of QUOTIENT.TENTHS as a string. */
17562 int length;
17563
17564 char * psuffix;
17565 char * p;
17566
17567 if (1000 <= quotient)
17568 {
17569 /* Scale to the appropriate EXPONENT. */
17570 do
17571 {
17572 remainder = quotient % 1000;
17573 quotient /= 1000;
17574 exponent++;
17575 }
17576 while (1000 <= quotient);
17577
17578 /* Round to nearest and decide whether to use TENTHS or not. */
17579 if (quotient <= 9)
17580 {
17581 tenths = remainder / 100;
17582 if (50 <= remainder % 100)
17583 {
17584 if (tenths < 9)
17585 tenths++;
17586 else
17587 {
17588 quotient++;
17589 if (quotient == 10)
17590 tenths = -1;
17591 else
17592 tenths = 0;
17593 }
17594 }
17595 }
17596 else
17597 if (500 <= remainder)
17598 {
17599 if (quotient < 999)
17600 quotient++;
17601 else
17602 {
17603 quotient = 1;
17604 exponent++;
17605 tenths = 0;
17606 }
17607 }
17608 }
17609
17610 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17611 if (tenths == -1 && quotient <= 99)
17612 if (quotient <= 9)
17613 length = 1;
17614 else
17615 length = 2;
17616 else
17617 length = 3;
17618 p = psuffix = buf + max (width, length);
17619
17620 /* Print EXPONENT. */
17621 if (exponent)
17622 *psuffix++ = power_letter[exponent];
17623 *psuffix = '\0';
17624
17625 /* Print TENTHS. */
17626 if (tenths >= 0)
17627 {
17628 *--p = '0' + tenths;
17629 *--p = '.';
17630 }
17631
17632 /* Print QUOTIENT. */
17633 do
17634 {
17635 int digit = quotient % 10;
17636 *--p = '0' + digit;
17637 }
17638 while ((quotient /= 10) != 0);
17639
17640 /* Print leading spaces. */
17641 while (buf < p)
17642 *--p = ' ';
17643 }
17644
17645 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17646 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17647 type of CODING_SYSTEM. Return updated pointer into BUF. */
17648
17649 static unsigned char invalid_eol_type[] = "(*invalid*)";
17650
17651 static char *
17652 decode_mode_spec_coding (coding_system, buf, eol_flag)
17653 Lisp_Object coding_system;
17654 register char *buf;
17655 int eol_flag;
17656 {
17657 Lisp_Object val;
17658 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17659 const unsigned char *eol_str;
17660 int eol_str_len;
17661 /* The EOL conversion we are using. */
17662 Lisp_Object eoltype;
17663
17664 val = Fget (coding_system, Qcoding_system);
17665 eoltype = Qnil;
17666
17667 if (!VECTORP (val)) /* Not yet decided. */
17668 {
17669 if (multibyte)
17670 *buf++ = '-';
17671 if (eol_flag)
17672 eoltype = eol_mnemonic_undecided;
17673 /* Don't mention EOL conversion if it isn't decided. */
17674 }
17675 else
17676 {
17677 Lisp_Object eolvalue;
17678
17679 eolvalue = Fget (coding_system, Qeol_type);
17680
17681 if (multibyte)
17682 *buf++ = XFASTINT (AREF (val, 1));
17683
17684 if (eol_flag)
17685 {
17686 /* The EOL conversion that is normal on this system. */
17687
17688 if (NILP (eolvalue)) /* Not yet decided. */
17689 eoltype = eol_mnemonic_undecided;
17690 else if (VECTORP (eolvalue)) /* Not yet decided. */
17691 eoltype = eol_mnemonic_undecided;
17692 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17693 eoltype = (XFASTINT (eolvalue) == 0
17694 ? eol_mnemonic_unix
17695 : (XFASTINT (eolvalue) == 1
17696 ? eol_mnemonic_dos : eol_mnemonic_mac));
17697 }
17698 }
17699
17700 if (eol_flag)
17701 {
17702 /* Mention the EOL conversion if it is not the usual one. */
17703 if (STRINGP (eoltype))
17704 {
17705 eol_str = SDATA (eoltype);
17706 eol_str_len = SBYTES (eoltype);
17707 }
17708 else if (INTEGERP (eoltype)
17709 && CHAR_VALID_P (XINT (eoltype), 0))
17710 {
17711 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17712 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17713 eol_str = tmp;
17714 }
17715 else
17716 {
17717 eol_str = invalid_eol_type;
17718 eol_str_len = sizeof (invalid_eol_type) - 1;
17719 }
17720 bcopy (eol_str, buf, eol_str_len);
17721 buf += eol_str_len;
17722 }
17723
17724 return buf;
17725 }
17726
17727 /* Return a string for the output of a mode line %-spec for window W,
17728 generated by character C. PRECISION >= 0 means don't return a
17729 string longer than that value. FIELD_WIDTH > 0 means pad the
17730 string returned with spaces to that value. Return 1 in *MULTIBYTE
17731 if the result is multibyte text.
17732
17733 Note we operate on the current buffer for most purposes,
17734 the exception being w->base_line_pos. */
17735
17736 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17737
17738 static char *
17739 decode_mode_spec (w, c, field_width, precision, multibyte)
17740 struct window *w;
17741 register int c;
17742 int field_width, precision;
17743 int *multibyte;
17744 {
17745 Lisp_Object obj;
17746 struct frame *f = XFRAME (WINDOW_FRAME (w));
17747 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17748 struct buffer *b = current_buffer;
17749
17750 obj = Qnil;
17751 *multibyte = 0;
17752
17753 switch (c)
17754 {
17755 case '*':
17756 if (!NILP (b->read_only))
17757 return "%";
17758 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17759 return "*";
17760 return "-";
17761
17762 case '+':
17763 /* This differs from %* only for a modified read-only buffer. */
17764 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17765 return "*";
17766 if (!NILP (b->read_only))
17767 return "%";
17768 return "-";
17769
17770 case '&':
17771 /* This differs from %* in ignoring read-only-ness. */
17772 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17773 return "*";
17774 return "-";
17775
17776 case '%':
17777 return "%";
17778
17779 case '[':
17780 {
17781 int i;
17782 char *p;
17783
17784 if (command_loop_level > 5)
17785 return "[[[... ";
17786 p = decode_mode_spec_buf;
17787 for (i = 0; i < command_loop_level; i++)
17788 *p++ = '[';
17789 *p = 0;
17790 return decode_mode_spec_buf;
17791 }
17792
17793 case ']':
17794 {
17795 int i;
17796 char *p;
17797
17798 if (command_loop_level > 5)
17799 return " ...]]]";
17800 p = decode_mode_spec_buf;
17801 for (i = 0; i < command_loop_level; i++)
17802 *p++ = ']';
17803 *p = 0;
17804 return decode_mode_spec_buf;
17805 }
17806
17807 case '-':
17808 {
17809 register int i;
17810
17811 /* Let lots_of_dashes be a string of infinite length. */
17812 if (mode_line_target == MODE_LINE_NOPROP ||
17813 mode_line_target == MODE_LINE_STRING)
17814 return "--";
17815 if (field_width <= 0
17816 || field_width > sizeof (lots_of_dashes))
17817 {
17818 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17819 decode_mode_spec_buf[i] = '-';
17820 decode_mode_spec_buf[i] = '\0';
17821 return decode_mode_spec_buf;
17822 }
17823 else
17824 return lots_of_dashes;
17825 }
17826
17827 case 'b':
17828 obj = b->name;
17829 break;
17830
17831 case 'c':
17832 /* %c and %l are ignored in `frame-title-format'.
17833 (In redisplay_internal, the frame title is drawn _before_ the
17834 windows are updated, so the stuff which depends on actual
17835 window contents (such as %l) may fail to render properly, or
17836 even crash emacs.) */
17837 if (mode_line_target == MODE_LINE_TITLE)
17838 return "";
17839 else
17840 {
17841 int col = (int) current_column (); /* iftc */
17842 w->column_number_displayed = make_number (col);
17843 pint2str (decode_mode_spec_buf, field_width, col);
17844 return decode_mode_spec_buf;
17845 }
17846
17847 case 'e':
17848 #ifndef SYSTEM_MALLOC
17849 {
17850 if (NILP (Vmemory_full))
17851 return "";
17852 else
17853 return "!MEM FULL! ";
17854 }
17855 #else
17856 return "";
17857 #endif
17858
17859 case 'F':
17860 /* %F displays the frame name. */
17861 if (!NILP (f->title))
17862 return (char *) SDATA (f->title);
17863 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17864 return (char *) SDATA (f->name);
17865 return "Emacs";
17866
17867 case 'f':
17868 obj = b->filename;
17869 break;
17870
17871 case 'i':
17872 {
17873 int size = ZV - BEGV;
17874 pint2str (decode_mode_spec_buf, field_width, size);
17875 return decode_mode_spec_buf;
17876 }
17877
17878 case 'I':
17879 {
17880 int size = ZV - BEGV;
17881 pint2hrstr (decode_mode_spec_buf, field_width, size);
17882 return decode_mode_spec_buf;
17883 }
17884
17885 case 'l':
17886 {
17887 int startpos, startpos_byte, line, linepos, linepos_byte;
17888 int topline, nlines, junk, height;
17889
17890 /* %c and %l are ignored in `frame-title-format'. */
17891 if (mode_line_target == MODE_LINE_TITLE)
17892 return "";
17893
17894 startpos = XMARKER (w->start)->charpos;
17895 startpos_byte = marker_byte_position (w->start);
17896 height = WINDOW_TOTAL_LINES (w);
17897
17898 /* If we decided that this buffer isn't suitable for line numbers,
17899 don't forget that too fast. */
17900 if (EQ (w->base_line_pos, w->buffer))
17901 goto no_value;
17902 /* But do forget it, if the window shows a different buffer now. */
17903 else if (BUFFERP (w->base_line_pos))
17904 w->base_line_pos = Qnil;
17905
17906 /* If the buffer is very big, don't waste time. */
17907 if (INTEGERP (Vline_number_display_limit)
17908 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17909 {
17910 w->base_line_pos = Qnil;
17911 w->base_line_number = Qnil;
17912 goto no_value;
17913 }
17914
17915 if (!NILP (w->base_line_number)
17916 && !NILP (w->base_line_pos)
17917 && XFASTINT (w->base_line_pos) <= startpos)
17918 {
17919 line = XFASTINT (w->base_line_number);
17920 linepos = XFASTINT (w->base_line_pos);
17921 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17922 }
17923 else
17924 {
17925 line = 1;
17926 linepos = BUF_BEGV (b);
17927 linepos_byte = BUF_BEGV_BYTE (b);
17928 }
17929
17930 /* Count lines from base line to window start position. */
17931 nlines = display_count_lines (linepos, linepos_byte,
17932 startpos_byte,
17933 startpos, &junk);
17934
17935 topline = nlines + line;
17936
17937 /* Determine a new base line, if the old one is too close
17938 or too far away, or if we did not have one.
17939 "Too close" means it's plausible a scroll-down would
17940 go back past it. */
17941 if (startpos == BUF_BEGV (b))
17942 {
17943 w->base_line_number = make_number (topline);
17944 w->base_line_pos = make_number (BUF_BEGV (b));
17945 }
17946 else if (nlines < height + 25 || nlines > height * 3 + 50
17947 || linepos == BUF_BEGV (b))
17948 {
17949 int limit = BUF_BEGV (b);
17950 int limit_byte = BUF_BEGV_BYTE (b);
17951 int position;
17952 int distance = (height * 2 + 30) * line_number_display_limit_width;
17953
17954 if (startpos - distance > limit)
17955 {
17956 limit = startpos - distance;
17957 limit_byte = CHAR_TO_BYTE (limit);
17958 }
17959
17960 nlines = display_count_lines (startpos, startpos_byte,
17961 limit_byte,
17962 - (height * 2 + 30),
17963 &position);
17964 /* If we couldn't find the lines we wanted within
17965 line_number_display_limit_width chars per line,
17966 give up on line numbers for this window. */
17967 if (position == limit_byte && limit == startpos - distance)
17968 {
17969 w->base_line_pos = w->buffer;
17970 w->base_line_number = Qnil;
17971 goto no_value;
17972 }
17973
17974 w->base_line_number = make_number (topline - nlines);
17975 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17976 }
17977
17978 /* Now count lines from the start pos to point. */
17979 nlines = display_count_lines (startpos, startpos_byte,
17980 PT_BYTE, PT, &junk);
17981
17982 /* Record that we did display the line number. */
17983 line_number_displayed = 1;
17984
17985 /* Make the string to show. */
17986 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17987 return decode_mode_spec_buf;
17988 no_value:
17989 {
17990 char* p = decode_mode_spec_buf;
17991 int pad = field_width - 2;
17992 while (pad-- > 0)
17993 *p++ = ' ';
17994 *p++ = '?';
17995 *p++ = '?';
17996 *p = '\0';
17997 return decode_mode_spec_buf;
17998 }
17999 }
18000 break;
18001
18002 case 'm':
18003 obj = b->mode_name;
18004 break;
18005
18006 case 'n':
18007 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18008 return " Narrow";
18009 break;
18010
18011 case 'p':
18012 {
18013 int pos = marker_position (w->start);
18014 int total = BUF_ZV (b) - BUF_BEGV (b);
18015
18016 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18017 {
18018 if (pos <= BUF_BEGV (b))
18019 return "All";
18020 else
18021 return "Bottom";
18022 }
18023 else if (pos <= BUF_BEGV (b))
18024 return "Top";
18025 else
18026 {
18027 if (total > 1000000)
18028 /* Do it differently for a large value, to avoid overflow. */
18029 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18030 else
18031 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18032 /* We can't normally display a 3-digit number,
18033 so get us a 2-digit number that is close. */
18034 if (total == 100)
18035 total = 99;
18036 sprintf (decode_mode_spec_buf, "%2d%%", total);
18037 return decode_mode_spec_buf;
18038 }
18039 }
18040
18041 /* Display percentage of size above the bottom of the screen. */
18042 case 'P':
18043 {
18044 int toppos = marker_position (w->start);
18045 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18046 int total = BUF_ZV (b) - BUF_BEGV (b);
18047
18048 if (botpos >= BUF_ZV (b))
18049 {
18050 if (toppos <= BUF_BEGV (b))
18051 return "All";
18052 else
18053 return "Bottom";
18054 }
18055 else
18056 {
18057 if (total > 1000000)
18058 /* Do it differently for a large value, to avoid overflow. */
18059 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18060 else
18061 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18062 /* We can't normally display a 3-digit number,
18063 so get us a 2-digit number that is close. */
18064 if (total == 100)
18065 total = 99;
18066 if (toppos <= BUF_BEGV (b))
18067 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18068 else
18069 sprintf (decode_mode_spec_buf, "%2d%%", total);
18070 return decode_mode_spec_buf;
18071 }
18072 }
18073
18074 case 's':
18075 /* status of process */
18076 obj = Fget_buffer_process (Fcurrent_buffer ());
18077 if (NILP (obj))
18078 return "no process";
18079 #ifdef subprocesses
18080 obj = Fsymbol_name (Fprocess_status (obj));
18081 #endif
18082 break;
18083
18084 case 't': /* indicate TEXT or BINARY */
18085 #ifdef MODE_LINE_BINARY_TEXT
18086 return MODE_LINE_BINARY_TEXT (b);
18087 #else
18088 return "T";
18089 #endif
18090
18091 case 'z':
18092 /* coding-system (not including end-of-line format) */
18093 case 'Z':
18094 /* coding-system (including end-of-line type) */
18095 {
18096 int eol_flag = (c == 'Z');
18097 char *p = decode_mode_spec_buf;
18098
18099 if (! FRAME_WINDOW_P (f))
18100 {
18101 /* No need to mention EOL here--the terminal never needs
18102 to do EOL conversion. */
18103 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
18104 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
18105 }
18106 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18107 p, eol_flag);
18108
18109 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18110 #ifdef subprocesses
18111 obj = Fget_buffer_process (Fcurrent_buffer ());
18112 if (PROCESSP (obj))
18113 {
18114 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18115 p, eol_flag);
18116 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18117 p, eol_flag);
18118 }
18119 #endif /* subprocesses */
18120 #endif /* 0 */
18121 *p = 0;
18122 return decode_mode_spec_buf;
18123 }
18124 }
18125
18126 if (STRINGP (obj))
18127 {
18128 *multibyte = STRING_MULTIBYTE (obj);
18129 return (char *) SDATA (obj);
18130 }
18131 else
18132 return "";
18133 }
18134
18135
18136 /* Count up to COUNT lines starting from START / START_BYTE.
18137 But don't go beyond LIMIT_BYTE.
18138 Return the number of lines thus found (always nonnegative).
18139
18140 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18141
18142 static int
18143 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18144 int start, start_byte, limit_byte, count;
18145 int *byte_pos_ptr;
18146 {
18147 register unsigned char *cursor;
18148 unsigned char *base;
18149
18150 register int ceiling;
18151 register unsigned char *ceiling_addr;
18152 int orig_count = count;
18153
18154 /* If we are not in selective display mode,
18155 check only for newlines. */
18156 int selective_display = (!NILP (current_buffer->selective_display)
18157 && !INTEGERP (current_buffer->selective_display));
18158
18159 if (count > 0)
18160 {
18161 while (start_byte < limit_byte)
18162 {
18163 ceiling = BUFFER_CEILING_OF (start_byte);
18164 ceiling = min (limit_byte - 1, ceiling);
18165 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18166 base = (cursor = BYTE_POS_ADDR (start_byte));
18167 while (1)
18168 {
18169 if (selective_display)
18170 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18171 ;
18172 else
18173 while (*cursor != '\n' && ++cursor != ceiling_addr)
18174 ;
18175
18176 if (cursor != ceiling_addr)
18177 {
18178 if (--count == 0)
18179 {
18180 start_byte += cursor - base + 1;
18181 *byte_pos_ptr = start_byte;
18182 return orig_count;
18183 }
18184 else
18185 if (++cursor == ceiling_addr)
18186 break;
18187 }
18188 else
18189 break;
18190 }
18191 start_byte += cursor - base;
18192 }
18193 }
18194 else
18195 {
18196 while (start_byte > limit_byte)
18197 {
18198 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18199 ceiling = max (limit_byte, ceiling);
18200 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18201 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18202 while (1)
18203 {
18204 if (selective_display)
18205 while (--cursor != ceiling_addr
18206 && *cursor != '\n' && *cursor != 015)
18207 ;
18208 else
18209 while (--cursor != ceiling_addr && *cursor != '\n')
18210 ;
18211
18212 if (cursor != ceiling_addr)
18213 {
18214 if (++count == 0)
18215 {
18216 start_byte += cursor - base + 1;
18217 *byte_pos_ptr = start_byte;
18218 /* When scanning backwards, we should
18219 not count the newline posterior to which we stop. */
18220 return - orig_count - 1;
18221 }
18222 }
18223 else
18224 break;
18225 }
18226 /* Here we add 1 to compensate for the last decrement
18227 of CURSOR, which took it past the valid range. */
18228 start_byte += cursor - base + 1;
18229 }
18230 }
18231
18232 *byte_pos_ptr = limit_byte;
18233
18234 if (count < 0)
18235 return - orig_count + count;
18236 return orig_count - count;
18237
18238 }
18239
18240
18241 \f
18242 /***********************************************************************
18243 Displaying strings
18244 ***********************************************************************/
18245
18246 /* Display a NUL-terminated string, starting with index START.
18247
18248 If STRING is non-null, display that C string. Otherwise, the Lisp
18249 string LISP_STRING is displayed.
18250
18251 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18252 FACE_STRING. Display STRING or LISP_STRING with the face at
18253 FACE_STRING_POS in FACE_STRING:
18254
18255 Display the string in the environment given by IT, but use the
18256 standard display table, temporarily.
18257
18258 FIELD_WIDTH is the minimum number of output glyphs to produce.
18259 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18260 with spaces. If STRING has more characters, more than FIELD_WIDTH
18261 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18262
18263 PRECISION is the maximum number of characters to output from
18264 STRING. PRECISION < 0 means don't truncate the string.
18265
18266 This is roughly equivalent to printf format specifiers:
18267
18268 FIELD_WIDTH PRECISION PRINTF
18269 ----------------------------------------
18270 -1 -1 %s
18271 -1 10 %.10s
18272 10 -1 %10s
18273 20 10 %20.10s
18274
18275 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18276 display them, and < 0 means obey the current buffer's value of
18277 enable_multibyte_characters.
18278
18279 Value is the number of columns displayed. */
18280
18281 static int
18282 display_string (string, lisp_string, face_string, face_string_pos,
18283 start, it, field_width, precision, max_x, multibyte)
18284 unsigned char *string;
18285 Lisp_Object lisp_string;
18286 Lisp_Object face_string;
18287 int face_string_pos;
18288 int start;
18289 struct it *it;
18290 int field_width, precision, max_x;
18291 int multibyte;
18292 {
18293 int hpos_at_start = it->hpos;
18294 int saved_face_id = it->face_id;
18295 struct glyph_row *row = it->glyph_row;
18296
18297 /* Initialize the iterator IT for iteration over STRING beginning
18298 with index START. */
18299 reseat_to_string (it, string, lisp_string, start,
18300 precision, field_width, multibyte);
18301
18302 /* If displaying STRING, set up the face of the iterator
18303 from LISP_STRING, if that's given. */
18304 if (STRINGP (face_string))
18305 {
18306 int endptr;
18307 struct face *face;
18308
18309 it->face_id
18310 = face_at_string_position (it->w, face_string, face_string_pos,
18311 0, it->region_beg_charpos,
18312 it->region_end_charpos,
18313 &endptr, it->base_face_id, 0);
18314 face = FACE_FROM_ID (it->f, it->face_id);
18315 it->face_box_p = face->box != FACE_NO_BOX;
18316 }
18317
18318 /* Set max_x to the maximum allowed X position. Don't let it go
18319 beyond the right edge of the window. */
18320 if (max_x <= 0)
18321 max_x = it->last_visible_x;
18322 else
18323 max_x = min (max_x, it->last_visible_x);
18324
18325 /* Skip over display elements that are not visible. because IT->w is
18326 hscrolled. */
18327 if (it->current_x < it->first_visible_x)
18328 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18329 MOVE_TO_POS | MOVE_TO_X);
18330
18331 row->ascent = it->max_ascent;
18332 row->height = it->max_ascent + it->max_descent;
18333 row->phys_ascent = it->max_phys_ascent;
18334 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18335 row->extra_line_spacing = it->max_extra_line_spacing;
18336
18337 /* This condition is for the case that we are called with current_x
18338 past last_visible_x. */
18339 while (it->current_x < max_x)
18340 {
18341 int x_before, x, n_glyphs_before, i, nglyphs;
18342
18343 /* Get the next display element. */
18344 if (!get_next_display_element (it))
18345 break;
18346
18347 /* Produce glyphs. */
18348 x_before = it->current_x;
18349 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18350 PRODUCE_GLYPHS (it);
18351
18352 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18353 i = 0;
18354 x = x_before;
18355 while (i < nglyphs)
18356 {
18357 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18358
18359 if (!it->truncate_lines_p
18360 && x + glyph->pixel_width > max_x)
18361 {
18362 /* End of continued line or max_x reached. */
18363 if (CHAR_GLYPH_PADDING_P (*glyph))
18364 {
18365 /* A wide character is unbreakable. */
18366 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18367 it->current_x = x_before;
18368 }
18369 else
18370 {
18371 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18372 it->current_x = x;
18373 }
18374 break;
18375 }
18376 else if (x + glyph->pixel_width > it->first_visible_x)
18377 {
18378 /* Glyph is at least partially visible. */
18379 ++it->hpos;
18380 if (x < it->first_visible_x)
18381 it->glyph_row->x = x - it->first_visible_x;
18382 }
18383 else
18384 {
18385 /* Glyph is off the left margin of the display area.
18386 Should not happen. */
18387 abort ();
18388 }
18389
18390 row->ascent = max (row->ascent, it->max_ascent);
18391 row->height = max (row->height, it->max_ascent + it->max_descent);
18392 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18393 row->phys_height = max (row->phys_height,
18394 it->max_phys_ascent + it->max_phys_descent);
18395 row->extra_line_spacing = max (row->extra_line_spacing,
18396 it->max_extra_line_spacing);
18397 x += glyph->pixel_width;
18398 ++i;
18399 }
18400
18401 /* Stop if max_x reached. */
18402 if (i < nglyphs)
18403 break;
18404
18405 /* Stop at line ends. */
18406 if (ITERATOR_AT_END_OF_LINE_P (it))
18407 {
18408 it->continuation_lines_width = 0;
18409 break;
18410 }
18411
18412 set_iterator_to_next (it, 1);
18413
18414 /* Stop if truncating at the right edge. */
18415 if (it->truncate_lines_p
18416 && it->current_x >= it->last_visible_x)
18417 {
18418 /* Add truncation mark, but don't do it if the line is
18419 truncated at a padding space. */
18420 if (IT_CHARPOS (*it) < it->string_nchars)
18421 {
18422 if (!FRAME_WINDOW_P (it->f))
18423 {
18424 int i, n;
18425
18426 if (it->current_x > it->last_visible_x)
18427 {
18428 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18429 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18430 break;
18431 for (n = row->used[TEXT_AREA]; i < n; ++i)
18432 {
18433 row->used[TEXT_AREA] = i;
18434 produce_special_glyphs (it, IT_TRUNCATION);
18435 }
18436 }
18437 produce_special_glyphs (it, IT_TRUNCATION);
18438 }
18439 it->glyph_row->truncated_on_right_p = 1;
18440 }
18441 break;
18442 }
18443 }
18444
18445 /* Maybe insert a truncation at the left. */
18446 if (it->first_visible_x
18447 && IT_CHARPOS (*it) > 0)
18448 {
18449 if (!FRAME_WINDOW_P (it->f))
18450 insert_left_trunc_glyphs (it);
18451 it->glyph_row->truncated_on_left_p = 1;
18452 }
18453
18454 it->face_id = saved_face_id;
18455
18456 /* Value is number of columns displayed. */
18457 return it->hpos - hpos_at_start;
18458 }
18459
18460
18461 \f
18462 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18463 appears as an element of LIST or as the car of an element of LIST.
18464 If PROPVAL is a list, compare each element against LIST in that
18465 way, and return 1/2 if any element of PROPVAL is found in LIST.
18466 Otherwise return 0. This function cannot quit.
18467 The return value is 2 if the text is invisible but with an ellipsis
18468 and 1 if it's invisible and without an ellipsis. */
18469
18470 int
18471 invisible_p (propval, list)
18472 register Lisp_Object propval;
18473 Lisp_Object list;
18474 {
18475 register Lisp_Object tail, proptail;
18476
18477 for (tail = list; CONSP (tail); tail = XCDR (tail))
18478 {
18479 register Lisp_Object tem;
18480 tem = XCAR (tail);
18481 if (EQ (propval, tem))
18482 return 1;
18483 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18484 return NILP (XCDR (tem)) ? 1 : 2;
18485 }
18486
18487 if (CONSP (propval))
18488 {
18489 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18490 {
18491 Lisp_Object propelt;
18492 propelt = XCAR (proptail);
18493 for (tail = list; CONSP (tail); tail = XCDR (tail))
18494 {
18495 register Lisp_Object tem;
18496 tem = XCAR (tail);
18497 if (EQ (propelt, tem))
18498 return 1;
18499 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18500 return NILP (XCDR (tem)) ? 1 : 2;
18501 }
18502 }
18503 }
18504
18505 return 0;
18506 }
18507
18508 /* Calculate a width or height in pixels from a specification using
18509 the following elements:
18510
18511 SPEC ::=
18512 NUM - a (fractional) multiple of the default font width/height
18513 (NUM) - specifies exactly NUM pixels
18514 UNIT - a fixed number of pixels, see below.
18515 ELEMENT - size of a display element in pixels, see below.
18516 (NUM . SPEC) - equals NUM * SPEC
18517 (+ SPEC SPEC ...) - add pixel values
18518 (- SPEC SPEC ...) - subtract pixel values
18519 (- SPEC) - negate pixel value
18520
18521 NUM ::=
18522 INT or FLOAT - a number constant
18523 SYMBOL - use symbol's (buffer local) variable binding.
18524
18525 UNIT ::=
18526 in - pixels per inch *)
18527 mm - pixels per 1/1000 meter *)
18528 cm - pixels per 1/100 meter *)
18529 width - width of current font in pixels.
18530 height - height of current font in pixels.
18531
18532 *) using the ratio(s) defined in display-pixels-per-inch.
18533
18534 ELEMENT ::=
18535
18536 left-fringe - left fringe width in pixels
18537 right-fringe - right fringe width in pixels
18538
18539 left-margin - left margin width in pixels
18540 right-margin - right margin width in pixels
18541
18542 scroll-bar - scroll-bar area width in pixels
18543
18544 Examples:
18545
18546 Pixels corresponding to 5 inches:
18547 (5 . in)
18548
18549 Total width of non-text areas on left side of window (if scroll-bar is on left):
18550 '(space :width (+ left-fringe left-margin scroll-bar))
18551
18552 Align to first text column (in header line):
18553 '(space :align-to 0)
18554
18555 Align to middle of text area minus half the width of variable `my-image'
18556 containing a loaded image:
18557 '(space :align-to (0.5 . (- text my-image)))
18558
18559 Width of left margin minus width of 1 character in the default font:
18560 '(space :width (- left-margin 1))
18561
18562 Width of left margin minus width of 2 characters in the current font:
18563 '(space :width (- left-margin (2 . width)))
18564
18565 Center 1 character over left-margin (in header line):
18566 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18567
18568 Different ways to express width of left fringe plus left margin minus one pixel:
18569 '(space :width (- (+ left-fringe left-margin) (1)))
18570 '(space :width (+ left-fringe left-margin (- (1))))
18571 '(space :width (+ left-fringe left-margin (-1)))
18572
18573 */
18574
18575 #define NUMVAL(X) \
18576 ((INTEGERP (X) || FLOATP (X)) \
18577 ? XFLOATINT (X) \
18578 : - 1)
18579
18580 int
18581 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18582 double *res;
18583 struct it *it;
18584 Lisp_Object prop;
18585 void *font;
18586 int width_p, *align_to;
18587 {
18588 double pixels;
18589
18590 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18591 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18592
18593 if (NILP (prop))
18594 return OK_PIXELS (0);
18595
18596 if (SYMBOLP (prop))
18597 {
18598 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18599 {
18600 char *unit = SDATA (SYMBOL_NAME (prop));
18601
18602 if (unit[0] == 'i' && unit[1] == 'n')
18603 pixels = 1.0;
18604 else if (unit[0] == 'm' && unit[1] == 'm')
18605 pixels = 25.4;
18606 else if (unit[0] == 'c' && unit[1] == 'm')
18607 pixels = 2.54;
18608 else
18609 pixels = 0;
18610 if (pixels > 0)
18611 {
18612 double ppi;
18613 #ifdef HAVE_WINDOW_SYSTEM
18614 if (FRAME_WINDOW_P (it->f)
18615 && (ppi = (width_p
18616 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18617 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18618 ppi > 0))
18619 return OK_PIXELS (ppi / pixels);
18620 #endif
18621
18622 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18623 || (CONSP (Vdisplay_pixels_per_inch)
18624 && (ppi = (width_p
18625 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18626 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18627 ppi > 0)))
18628 return OK_PIXELS (ppi / pixels);
18629
18630 return 0;
18631 }
18632 }
18633
18634 #ifdef HAVE_WINDOW_SYSTEM
18635 if (EQ (prop, Qheight))
18636 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18637 if (EQ (prop, Qwidth))
18638 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18639 #else
18640 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18641 return OK_PIXELS (1);
18642 #endif
18643
18644 if (EQ (prop, Qtext))
18645 return OK_PIXELS (width_p
18646 ? window_box_width (it->w, TEXT_AREA)
18647 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18648
18649 if (align_to && *align_to < 0)
18650 {
18651 *res = 0;
18652 if (EQ (prop, Qleft))
18653 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18654 if (EQ (prop, Qright))
18655 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18656 if (EQ (prop, Qcenter))
18657 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18658 + window_box_width (it->w, TEXT_AREA) / 2);
18659 if (EQ (prop, Qleft_fringe))
18660 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18661 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18662 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18663 if (EQ (prop, Qright_fringe))
18664 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18665 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18666 : window_box_right_offset (it->w, TEXT_AREA));
18667 if (EQ (prop, Qleft_margin))
18668 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18669 if (EQ (prop, Qright_margin))
18670 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18671 if (EQ (prop, Qscroll_bar))
18672 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18673 ? 0
18674 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18675 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18676 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18677 : 0)));
18678 }
18679 else
18680 {
18681 if (EQ (prop, Qleft_fringe))
18682 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18683 if (EQ (prop, Qright_fringe))
18684 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18685 if (EQ (prop, Qleft_margin))
18686 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18687 if (EQ (prop, Qright_margin))
18688 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18689 if (EQ (prop, Qscroll_bar))
18690 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18691 }
18692
18693 prop = Fbuffer_local_value (prop, it->w->buffer);
18694 }
18695
18696 if (INTEGERP (prop) || FLOATP (prop))
18697 {
18698 int base_unit = (width_p
18699 ? FRAME_COLUMN_WIDTH (it->f)
18700 : FRAME_LINE_HEIGHT (it->f));
18701 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18702 }
18703
18704 if (CONSP (prop))
18705 {
18706 Lisp_Object car = XCAR (prop);
18707 Lisp_Object cdr = XCDR (prop);
18708
18709 if (SYMBOLP (car))
18710 {
18711 #ifdef HAVE_WINDOW_SYSTEM
18712 if (valid_image_p (prop))
18713 {
18714 int id = lookup_image (it->f, prop);
18715 struct image *img = IMAGE_FROM_ID (it->f, id);
18716
18717 return OK_PIXELS (width_p ? img->width : img->height);
18718 }
18719 #endif
18720 if (EQ (car, Qplus) || EQ (car, Qminus))
18721 {
18722 int first = 1;
18723 double px;
18724
18725 pixels = 0;
18726 while (CONSP (cdr))
18727 {
18728 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18729 font, width_p, align_to))
18730 return 0;
18731 if (first)
18732 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18733 else
18734 pixels += px;
18735 cdr = XCDR (cdr);
18736 }
18737 if (EQ (car, Qminus))
18738 pixels = -pixels;
18739 return OK_PIXELS (pixels);
18740 }
18741
18742 car = Fbuffer_local_value (car, it->w->buffer);
18743 }
18744
18745 if (INTEGERP (car) || FLOATP (car))
18746 {
18747 double fact;
18748 pixels = XFLOATINT (car);
18749 if (NILP (cdr))
18750 return OK_PIXELS (pixels);
18751 if (calc_pixel_width_or_height (&fact, it, cdr,
18752 font, width_p, align_to))
18753 return OK_PIXELS (pixels * fact);
18754 return 0;
18755 }
18756
18757 return 0;
18758 }
18759
18760 return 0;
18761 }
18762
18763 \f
18764 /***********************************************************************
18765 Glyph Display
18766 ***********************************************************************/
18767
18768 #ifdef HAVE_WINDOW_SYSTEM
18769
18770 #if GLYPH_DEBUG
18771
18772 void
18773 dump_glyph_string (s)
18774 struct glyph_string *s;
18775 {
18776 fprintf (stderr, "glyph string\n");
18777 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18778 s->x, s->y, s->width, s->height);
18779 fprintf (stderr, " ybase = %d\n", s->ybase);
18780 fprintf (stderr, " hl = %d\n", s->hl);
18781 fprintf (stderr, " left overhang = %d, right = %d\n",
18782 s->left_overhang, s->right_overhang);
18783 fprintf (stderr, " nchars = %d\n", s->nchars);
18784 fprintf (stderr, " extends to end of line = %d\n",
18785 s->extends_to_end_of_line_p);
18786 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18787 fprintf (stderr, " bg width = %d\n", s->background_width);
18788 }
18789
18790 #endif /* GLYPH_DEBUG */
18791
18792 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18793 of XChar2b structures for S; it can't be allocated in
18794 init_glyph_string because it must be allocated via `alloca'. W
18795 is the window on which S is drawn. ROW and AREA are the glyph row
18796 and area within the row from which S is constructed. START is the
18797 index of the first glyph structure covered by S. HL is a
18798 face-override for drawing S. */
18799
18800 #ifdef HAVE_NTGUI
18801 #define OPTIONAL_HDC(hdc) hdc,
18802 #define DECLARE_HDC(hdc) HDC hdc;
18803 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18804 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18805 #endif
18806
18807 #ifndef OPTIONAL_HDC
18808 #define OPTIONAL_HDC(hdc)
18809 #define DECLARE_HDC(hdc)
18810 #define ALLOCATE_HDC(hdc, f)
18811 #define RELEASE_HDC(hdc, f)
18812 #endif
18813
18814 static void
18815 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18816 struct glyph_string *s;
18817 DECLARE_HDC (hdc)
18818 XChar2b *char2b;
18819 struct window *w;
18820 struct glyph_row *row;
18821 enum glyph_row_area area;
18822 int start;
18823 enum draw_glyphs_face hl;
18824 {
18825 bzero (s, sizeof *s);
18826 s->w = w;
18827 s->f = XFRAME (w->frame);
18828 #ifdef HAVE_NTGUI
18829 s->hdc = hdc;
18830 #endif
18831 s->display = FRAME_X_DISPLAY (s->f);
18832 s->window = FRAME_X_WINDOW (s->f);
18833 s->char2b = char2b;
18834 s->hl = hl;
18835 s->row = row;
18836 s->area = area;
18837 s->first_glyph = row->glyphs[area] + start;
18838 s->height = row->height;
18839 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18840
18841 /* Display the internal border below the tool-bar window. */
18842 if (WINDOWP (s->f->tool_bar_window)
18843 && s->w == XWINDOW (s->f->tool_bar_window))
18844 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18845
18846 s->ybase = s->y + row->ascent;
18847 }
18848
18849
18850 /* Append the list of glyph strings with head H and tail T to the list
18851 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18852
18853 static INLINE void
18854 append_glyph_string_lists (head, tail, h, t)
18855 struct glyph_string **head, **tail;
18856 struct glyph_string *h, *t;
18857 {
18858 if (h)
18859 {
18860 if (*head)
18861 (*tail)->next = h;
18862 else
18863 *head = h;
18864 h->prev = *tail;
18865 *tail = t;
18866 }
18867 }
18868
18869
18870 /* Prepend the list of glyph strings with head H and tail T to the
18871 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18872 result. */
18873
18874 static INLINE void
18875 prepend_glyph_string_lists (head, tail, h, t)
18876 struct glyph_string **head, **tail;
18877 struct glyph_string *h, *t;
18878 {
18879 if (h)
18880 {
18881 if (*head)
18882 (*head)->prev = t;
18883 else
18884 *tail = t;
18885 t->next = *head;
18886 *head = h;
18887 }
18888 }
18889
18890
18891 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18892 Set *HEAD and *TAIL to the resulting list. */
18893
18894 static INLINE void
18895 append_glyph_string (head, tail, s)
18896 struct glyph_string **head, **tail;
18897 struct glyph_string *s;
18898 {
18899 s->next = s->prev = NULL;
18900 append_glyph_string_lists (head, tail, s, s);
18901 }
18902
18903
18904 /* Get face and two-byte form of character glyph GLYPH on frame F.
18905 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18906 a pointer to a realized face that is ready for display. */
18907
18908 static INLINE struct face *
18909 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18910 struct frame *f;
18911 struct glyph *glyph;
18912 XChar2b *char2b;
18913 int *two_byte_p;
18914 {
18915 struct face *face;
18916
18917 xassert (glyph->type == CHAR_GLYPH);
18918 face = FACE_FROM_ID (f, glyph->face_id);
18919
18920 if (two_byte_p)
18921 *two_byte_p = 0;
18922
18923 if (!glyph->multibyte_p)
18924 {
18925 /* Unibyte case. We don't have to encode, but we have to make
18926 sure to use a face suitable for unibyte. */
18927 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18928 }
18929 else if (glyph->u.ch < 128)
18930 {
18931 /* Case of ASCII in a face known to fit ASCII. */
18932 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18933 }
18934 else
18935 {
18936 int c1, c2, charset;
18937
18938 /* Split characters into bytes. If c2 is -1 afterwards, C is
18939 really a one-byte character so that byte1 is zero. */
18940 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18941 if (c2 > 0)
18942 STORE_XCHAR2B (char2b, c1, c2);
18943 else
18944 STORE_XCHAR2B (char2b, 0, c1);
18945
18946 /* Maybe encode the character in *CHAR2B. */
18947 if (charset != CHARSET_ASCII)
18948 {
18949 struct font_info *font_info
18950 = FONT_INFO_FROM_ID (f, face->font_info_id);
18951 if (font_info)
18952 glyph->font_type
18953 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18954 }
18955 }
18956
18957 /* Make sure X resources of the face are allocated. */
18958 xassert (face != NULL);
18959 PREPARE_FACE_FOR_DISPLAY (f, face);
18960 return face;
18961 }
18962
18963
18964 /* Fill glyph string S with composition components specified by S->cmp.
18965
18966 FACES is an array of faces for all components of this composition.
18967 S->gidx is the index of the first component for S.
18968
18969 OVERLAPS non-zero means S should draw the foreground only, and use
18970 its physical height for clipping. See also draw_glyphs.
18971
18972 Value is the index of a component not in S. */
18973
18974 static int
18975 fill_composite_glyph_string (s, faces, overlaps)
18976 struct glyph_string *s;
18977 struct face **faces;
18978 int overlaps;
18979 {
18980 int i;
18981
18982 xassert (s);
18983
18984 s->for_overlaps = overlaps;
18985
18986 s->face = faces[s->gidx];
18987 s->font = s->face->font;
18988 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18989
18990 /* For all glyphs of this composition, starting at the offset
18991 S->gidx, until we reach the end of the definition or encounter a
18992 glyph that requires the different face, add it to S. */
18993 ++s->nchars;
18994 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18995 ++s->nchars;
18996
18997 /* All glyph strings for the same composition has the same width,
18998 i.e. the width set for the first component of the composition. */
18999
19000 s->width = s->first_glyph->pixel_width;
19001
19002 /* If the specified font could not be loaded, use the frame's
19003 default font, but record the fact that we couldn't load it in
19004 the glyph string so that we can draw rectangles for the
19005 characters of the glyph string. */
19006 if (s->font == NULL)
19007 {
19008 s->font_not_found_p = 1;
19009 s->font = FRAME_FONT (s->f);
19010 }
19011
19012 /* Adjust base line for subscript/superscript text. */
19013 s->ybase += s->first_glyph->voffset;
19014
19015 xassert (s->face && s->face->gc);
19016
19017 /* This glyph string must always be drawn with 16-bit functions. */
19018 s->two_byte_p = 1;
19019
19020 return s->gidx + s->nchars;
19021 }
19022
19023
19024 /* Fill glyph string S from a sequence of character glyphs.
19025
19026 FACE_ID is the face id of the string. START is the index of the
19027 first glyph to consider, END is the index of the last + 1.
19028 OVERLAPS non-zero means S should draw the foreground only, and use
19029 its physical height for clipping. See also draw_glyphs.
19030
19031 Value is the index of the first glyph not in S. */
19032
19033 static int
19034 fill_glyph_string (s, face_id, start, end, overlaps)
19035 struct glyph_string *s;
19036 int face_id;
19037 int start, end, overlaps;
19038 {
19039 struct glyph *glyph, *last;
19040 int voffset;
19041 int glyph_not_available_p;
19042
19043 xassert (s->f == XFRAME (s->w->frame));
19044 xassert (s->nchars == 0);
19045 xassert (start >= 0 && end > start);
19046
19047 s->for_overlaps = overlaps,
19048 glyph = s->row->glyphs[s->area] + start;
19049 last = s->row->glyphs[s->area] + end;
19050 voffset = glyph->voffset;
19051
19052 glyph_not_available_p = glyph->glyph_not_available_p;
19053
19054 while (glyph < last
19055 && glyph->type == CHAR_GLYPH
19056 && glyph->voffset == voffset
19057 /* Same face id implies same font, nowadays. */
19058 && glyph->face_id == face_id
19059 && glyph->glyph_not_available_p == glyph_not_available_p)
19060 {
19061 int two_byte_p;
19062
19063 s->face = get_glyph_face_and_encoding (s->f, glyph,
19064 s->char2b + s->nchars,
19065 &two_byte_p);
19066 s->two_byte_p = two_byte_p;
19067 ++s->nchars;
19068 xassert (s->nchars <= end - start);
19069 s->width += glyph->pixel_width;
19070 ++glyph;
19071 }
19072
19073 s->font = s->face->font;
19074 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19075
19076 /* If the specified font could not be loaded, use the frame's font,
19077 but record the fact that we couldn't load it in
19078 S->font_not_found_p so that we can draw rectangles for the
19079 characters of the glyph string. */
19080 if (s->font == NULL || glyph_not_available_p)
19081 {
19082 s->font_not_found_p = 1;
19083 s->font = FRAME_FONT (s->f);
19084 }
19085
19086 /* Adjust base line for subscript/superscript text. */
19087 s->ybase += voffset;
19088
19089 xassert (s->face && s->face->gc);
19090 return glyph - s->row->glyphs[s->area];
19091 }
19092
19093
19094 /* Fill glyph string S from image glyph S->first_glyph. */
19095
19096 static void
19097 fill_image_glyph_string (s)
19098 struct glyph_string *s;
19099 {
19100 xassert (s->first_glyph->type == IMAGE_GLYPH);
19101 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19102 xassert (s->img);
19103 s->slice = s->first_glyph->slice;
19104 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19105 s->font = s->face->font;
19106 s->width = s->first_glyph->pixel_width;
19107
19108 /* Adjust base line for subscript/superscript text. */
19109 s->ybase += s->first_glyph->voffset;
19110 }
19111
19112
19113 /* Fill glyph string S from a sequence of stretch glyphs.
19114
19115 ROW is the glyph row in which the glyphs are found, AREA is the
19116 area within the row. START is the index of the first glyph to
19117 consider, END is the index of the last + 1.
19118
19119 Value is the index of the first glyph not in S. */
19120
19121 static int
19122 fill_stretch_glyph_string (s, row, area, start, end)
19123 struct glyph_string *s;
19124 struct glyph_row *row;
19125 enum glyph_row_area area;
19126 int start, end;
19127 {
19128 struct glyph *glyph, *last;
19129 int voffset, face_id;
19130
19131 xassert (s->first_glyph->type == STRETCH_GLYPH);
19132
19133 glyph = s->row->glyphs[s->area] + start;
19134 last = s->row->glyphs[s->area] + end;
19135 face_id = glyph->face_id;
19136 s->face = FACE_FROM_ID (s->f, face_id);
19137 s->font = s->face->font;
19138 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19139 s->width = glyph->pixel_width;
19140 s->nchars = 1;
19141 voffset = glyph->voffset;
19142
19143 for (++glyph;
19144 (glyph < last
19145 && glyph->type == STRETCH_GLYPH
19146 && glyph->voffset == voffset
19147 && glyph->face_id == face_id);
19148 ++glyph)
19149 s->width += glyph->pixel_width;
19150
19151 /* Adjust base line for subscript/superscript text. */
19152 s->ybase += voffset;
19153
19154 /* The case that face->gc == 0 is handled when drawing the glyph
19155 string by calling PREPARE_FACE_FOR_DISPLAY. */
19156 xassert (s->face);
19157 return glyph - s->row->glyphs[s->area];
19158 }
19159
19160
19161 /* EXPORT for RIF:
19162 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19163 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19164 assumed to be zero. */
19165
19166 void
19167 x_get_glyph_overhangs (glyph, f, left, right)
19168 struct glyph *glyph;
19169 struct frame *f;
19170 int *left, *right;
19171 {
19172 *left = *right = 0;
19173
19174 if (glyph->type == CHAR_GLYPH)
19175 {
19176 XFontStruct *font;
19177 struct face *face;
19178 struct font_info *font_info;
19179 XChar2b char2b;
19180 XCharStruct *pcm;
19181
19182 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19183 font = face->font;
19184 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19185 if (font /* ++KFS: Should this be font_info ? */
19186 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19187 {
19188 if (pcm->rbearing > pcm->width)
19189 *right = pcm->rbearing - pcm->width;
19190 if (pcm->lbearing < 0)
19191 *left = -pcm->lbearing;
19192 }
19193 }
19194 }
19195
19196
19197 /* Return the index of the first glyph preceding glyph string S that
19198 is overwritten by S because of S's left overhang. Value is -1
19199 if no glyphs are overwritten. */
19200
19201 static int
19202 left_overwritten (s)
19203 struct glyph_string *s;
19204 {
19205 int k;
19206
19207 if (s->left_overhang)
19208 {
19209 int x = 0, i;
19210 struct glyph *glyphs = s->row->glyphs[s->area];
19211 int first = s->first_glyph - glyphs;
19212
19213 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19214 x -= glyphs[i].pixel_width;
19215
19216 k = i + 1;
19217 }
19218 else
19219 k = -1;
19220
19221 return k;
19222 }
19223
19224
19225 /* Return the index of the first glyph preceding glyph string S that
19226 is overwriting S because of its right overhang. Value is -1 if no
19227 glyph in front of S overwrites S. */
19228
19229 static int
19230 left_overwriting (s)
19231 struct glyph_string *s;
19232 {
19233 int i, k, x;
19234 struct glyph *glyphs = s->row->glyphs[s->area];
19235 int first = s->first_glyph - glyphs;
19236
19237 k = -1;
19238 x = 0;
19239 for (i = first - 1; i >= 0; --i)
19240 {
19241 int left, right;
19242 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19243 if (x + right > 0)
19244 k = i;
19245 x -= glyphs[i].pixel_width;
19246 }
19247
19248 return k;
19249 }
19250
19251
19252 /* Return the index of the last glyph following glyph string S that is
19253 not overwritten by S because of S's right overhang. Value is -1 if
19254 no such glyph is found. */
19255
19256 static int
19257 right_overwritten (s)
19258 struct glyph_string *s;
19259 {
19260 int k = -1;
19261
19262 if (s->right_overhang)
19263 {
19264 int x = 0, i;
19265 struct glyph *glyphs = s->row->glyphs[s->area];
19266 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19267 int end = s->row->used[s->area];
19268
19269 for (i = first; i < end && s->right_overhang > x; ++i)
19270 x += glyphs[i].pixel_width;
19271
19272 k = i;
19273 }
19274
19275 return k;
19276 }
19277
19278
19279 /* Return the index of the last glyph following glyph string S that
19280 overwrites S because of its left overhang. Value is negative
19281 if no such glyph is found. */
19282
19283 static int
19284 right_overwriting (s)
19285 struct glyph_string *s;
19286 {
19287 int i, k, x;
19288 int end = s->row->used[s->area];
19289 struct glyph *glyphs = s->row->glyphs[s->area];
19290 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19291
19292 k = -1;
19293 x = 0;
19294 for (i = first; i < end; ++i)
19295 {
19296 int left, right;
19297 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19298 if (x - left < 0)
19299 k = i;
19300 x += glyphs[i].pixel_width;
19301 }
19302
19303 return k;
19304 }
19305
19306
19307 /* Get face and two-byte form of character C in face FACE_ID on frame
19308 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19309 means we want to display multibyte text. DISPLAY_P non-zero means
19310 make sure that X resources for the face returned are allocated.
19311 Value is a pointer to a realized face that is ready for display if
19312 DISPLAY_P is non-zero. */
19313
19314 static INLINE struct face *
19315 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19316 struct frame *f;
19317 int c, face_id;
19318 XChar2b *char2b;
19319 int multibyte_p, display_p;
19320 {
19321 struct face *face = FACE_FROM_ID (f, face_id);
19322
19323 if (!multibyte_p)
19324 {
19325 /* Unibyte case. We don't have to encode, but we have to make
19326 sure to use a face suitable for unibyte. */
19327 STORE_XCHAR2B (char2b, 0, c);
19328 face_id = FACE_FOR_CHAR (f, face, c);
19329 face = FACE_FROM_ID (f, face_id);
19330 }
19331 else if (c < 128)
19332 {
19333 /* Case of ASCII in a face known to fit ASCII. */
19334 STORE_XCHAR2B (char2b, 0, c);
19335 }
19336 else
19337 {
19338 int c1, c2, charset;
19339
19340 /* Split characters into bytes. If c2 is -1 afterwards, C is
19341 really a one-byte character so that byte1 is zero. */
19342 SPLIT_CHAR (c, charset, c1, c2);
19343 if (c2 > 0)
19344 STORE_XCHAR2B (char2b, c1, c2);
19345 else
19346 STORE_XCHAR2B (char2b, 0, c1);
19347
19348 /* Maybe encode the character in *CHAR2B. */
19349 if (face->font != NULL)
19350 {
19351 struct font_info *font_info
19352 = FONT_INFO_FROM_ID (f, face->font_info_id);
19353 if (font_info)
19354 rif->encode_char (c, char2b, font_info, 0);
19355 }
19356 }
19357
19358 /* Make sure X resources of the face are allocated. */
19359 #ifdef HAVE_X_WINDOWS
19360 if (display_p)
19361 #endif
19362 {
19363 xassert (face != NULL);
19364 PREPARE_FACE_FOR_DISPLAY (f, face);
19365 }
19366
19367 return face;
19368 }
19369
19370
19371 /* Set background width of glyph string S. START is the index of the
19372 first glyph following S. LAST_X is the right-most x-position + 1
19373 in the drawing area. */
19374
19375 static INLINE void
19376 set_glyph_string_background_width (s, start, last_x)
19377 struct glyph_string *s;
19378 int start;
19379 int last_x;
19380 {
19381 /* If the face of this glyph string has to be drawn to the end of
19382 the drawing area, set S->extends_to_end_of_line_p. */
19383
19384 if (start == s->row->used[s->area]
19385 && s->area == TEXT_AREA
19386 && ((s->row->fill_line_p
19387 && (s->hl == DRAW_NORMAL_TEXT
19388 || s->hl == DRAW_IMAGE_RAISED
19389 || s->hl == DRAW_IMAGE_SUNKEN))
19390 || s->hl == DRAW_MOUSE_FACE))
19391 s->extends_to_end_of_line_p = 1;
19392
19393 /* If S extends its face to the end of the line, set its
19394 background_width to the distance to the right edge of the drawing
19395 area. */
19396 if (s->extends_to_end_of_line_p)
19397 s->background_width = last_x - s->x + 1;
19398 else
19399 s->background_width = s->width;
19400 }
19401
19402
19403 /* Compute overhangs and x-positions for glyph string S and its
19404 predecessors, or successors. X is the starting x-position for S.
19405 BACKWARD_P non-zero means process predecessors. */
19406
19407 static void
19408 compute_overhangs_and_x (s, x, backward_p)
19409 struct glyph_string *s;
19410 int x;
19411 int backward_p;
19412 {
19413 if (backward_p)
19414 {
19415 while (s)
19416 {
19417 if (rif->compute_glyph_string_overhangs)
19418 rif->compute_glyph_string_overhangs (s);
19419 x -= s->width;
19420 s->x = x;
19421 s = s->prev;
19422 }
19423 }
19424 else
19425 {
19426 while (s)
19427 {
19428 if (rif->compute_glyph_string_overhangs)
19429 rif->compute_glyph_string_overhangs (s);
19430 s->x = x;
19431 x += s->width;
19432 s = s->next;
19433 }
19434 }
19435 }
19436
19437
19438
19439 /* The following macros are only called from draw_glyphs below.
19440 They reference the following parameters of that function directly:
19441 `w', `row', `area', and `overlap_p'
19442 as well as the following local variables:
19443 `s', `f', and `hdc' (in W32) */
19444
19445 #ifdef HAVE_NTGUI
19446 /* On W32, silently add local `hdc' variable to argument list of
19447 init_glyph_string. */
19448 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19449 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19450 #else
19451 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19452 init_glyph_string (s, char2b, w, row, area, start, hl)
19453 #endif
19454
19455 /* Add a glyph string for a stretch glyph to the list of strings
19456 between HEAD and TAIL. START is the index of the stretch glyph in
19457 row area AREA of glyph row ROW. END is the index of the last glyph
19458 in that glyph row area. X is the current output position assigned
19459 to the new glyph string constructed. HL overrides that face of the
19460 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19461 is the right-most x-position of the drawing area. */
19462
19463 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19464 and below -- keep them on one line. */
19465 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19466 do \
19467 { \
19468 s = (struct glyph_string *) alloca (sizeof *s); \
19469 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19470 START = fill_stretch_glyph_string (s, row, area, START, END); \
19471 append_glyph_string (&HEAD, &TAIL, s); \
19472 s->x = (X); \
19473 } \
19474 while (0)
19475
19476
19477 /* Add a glyph string for an image glyph to the list of strings
19478 between HEAD and TAIL. START is the index of the image glyph in
19479 row area AREA of glyph row ROW. END is the index of the last glyph
19480 in that glyph row area. X is the current output position assigned
19481 to the new glyph string constructed. HL overrides that face of the
19482 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19483 is the right-most x-position of the drawing area. */
19484
19485 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19486 do \
19487 { \
19488 s = (struct glyph_string *) alloca (sizeof *s); \
19489 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19490 fill_image_glyph_string (s); \
19491 append_glyph_string (&HEAD, &TAIL, s); \
19492 ++START; \
19493 s->x = (X); \
19494 } \
19495 while (0)
19496
19497
19498 /* Add a glyph string for a sequence of character glyphs to the list
19499 of strings between HEAD and TAIL. START is the index of the first
19500 glyph in row area AREA of glyph row ROW that is part of the new
19501 glyph string. END is the index of the last glyph in that glyph row
19502 area. X is the current output position assigned to the new glyph
19503 string constructed. HL overrides that face of the glyph; e.g. it
19504 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19505 right-most x-position of the drawing area. */
19506
19507 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19508 do \
19509 { \
19510 int c, face_id; \
19511 XChar2b *char2b; \
19512 \
19513 c = (row)->glyphs[area][START].u.ch; \
19514 face_id = (row)->glyphs[area][START].face_id; \
19515 \
19516 s = (struct glyph_string *) alloca (sizeof *s); \
19517 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19518 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19519 append_glyph_string (&HEAD, &TAIL, s); \
19520 s->x = (X); \
19521 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19522 } \
19523 while (0)
19524
19525
19526 /* Add a glyph string for a composite sequence to the list of strings
19527 between HEAD and TAIL. START is the index of the first glyph in
19528 row area AREA of glyph row ROW that is part of the new glyph
19529 string. END is the index of the last glyph in that glyph row area.
19530 X is the current output position assigned to the new glyph string
19531 constructed. HL overrides that face of the glyph; e.g. it is
19532 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19533 x-position of the drawing area. */
19534
19535 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19536 do { \
19537 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19538 int face_id = (row)->glyphs[area][START].face_id; \
19539 struct face *base_face = FACE_FROM_ID (f, face_id); \
19540 struct composition *cmp = composition_table[cmp_id]; \
19541 int glyph_len = cmp->glyph_len; \
19542 XChar2b *char2b; \
19543 struct face **faces; \
19544 struct glyph_string *first_s = NULL; \
19545 int n; \
19546 \
19547 base_face = base_face->ascii_face; \
19548 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19549 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19550 /* At first, fill in `char2b' and `faces'. */ \
19551 for (n = 0; n < glyph_len; n++) \
19552 { \
19553 int c = COMPOSITION_GLYPH (cmp, n); \
19554 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19555 faces[n] = FACE_FROM_ID (f, this_face_id); \
19556 get_char_face_and_encoding (f, c, this_face_id, \
19557 char2b + n, 1, 1); \
19558 } \
19559 \
19560 /* Make glyph_strings for each glyph sequence that is drawable by \
19561 the same face, and append them to HEAD/TAIL. */ \
19562 for (n = 0; n < cmp->glyph_len;) \
19563 { \
19564 s = (struct glyph_string *) alloca (sizeof *s); \
19565 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19566 append_glyph_string (&(HEAD), &(TAIL), s); \
19567 s->cmp = cmp; \
19568 s->gidx = n; \
19569 s->x = (X); \
19570 \
19571 if (n == 0) \
19572 first_s = s; \
19573 \
19574 n = fill_composite_glyph_string (s, faces, overlaps); \
19575 } \
19576 \
19577 ++START; \
19578 s = first_s; \
19579 } while (0)
19580
19581
19582 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19583 of AREA of glyph row ROW on window W between indices START and END.
19584 HL overrides the face for drawing glyph strings, e.g. it is
19585 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19586 x-positions of the drawing area.
19587
19588 This is an ugly monster macro construct because we must use alloca
19589 to allocate glyph strings (because draw_glyphs can be called
19590 asynchronously). */
19591
19592 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19593 do \
19594 { \
19595 HEAD = TAIL = NULL; \
19596 while (START < END) \
19597 { \
19598 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19599 switch (first_glyph->type) \
19600 { \
19601 case CHAR_GLYPH: \
19602 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19603 HL, X, LAST_X); \
19604 break; \
19605 \
19606 case COMPOSITE_GLYPH: \
19607 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19608 HL, X, LAST_X); \
19609 break; \
19610 \
19611 case STRETCH_GLYPH: \
19612 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19613 HL, X, LAST_X); \
19614 break; \
19615 \
19616 case IMAGE_GLYPH: \
19617 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19618 HL, X, LAST_X); \
19619 break; \
19620 \
19621 default: \
19622 abort (); \
19623 } \
19624 \
19625 set_glyph_string_background_width (s, START, LAST_X); \
19626 (X) += s->width; \
19627 } \
19628 } \
19629 while (0)
19630
19631
19632 /* Draw glyphs between START and END in AREA of ROW on window W,
19633 starting at x-position X. X is relative to AREA in W. HL is a
19634 face-override with the following meaning:
19635
19636 DRAW_NORMAL_TEXT draw normally
19637 DRAW_CURSOR draw in cursor face
19638 DRAW_MOUSE_FACE draw in mouse face.
19639 DRAW_INVERSE_VIDEO draw in mode line face
19640 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19641 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19642
19643 If OVERLAPS is non-zero, draw only the foreground of characters and
19644 clip to the physical height of ROW. Non-zero value also defines
19645 the overlapping part to be drawn:
19646
19647 OVERLAPS_PRED overlap with preceding rows
19648 OVERLAPS_SUCC overlap with succeeding rows
19649 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19650 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19651
19652 Value is the x-position reached, relative to AREA of W. */
19653
19654 static int
19655 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19656 struct window *w;
19657 int x;
19658 struct glyph_row *row;
19659 enum glyph_row_area area;
19660 int start, end;
19661 enum draw_glyphs_face hl;
19662 int overlaps;
19663 {
19664 struct glyph_string *head, *tail;
19665 struct glyph_string *s;
19666 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19667 int last_x, area_width;
19668 int x_reached;
19669 int i, j;
19670 struct frame *f = XFRAME (WINDOW_FRAME (w));
19671 DECLARE_HDC (hdc);
19672
19673 ALLOCATE_HDC (hdc, f);
19674
19675 /* Let's rather be paranoid than getting a SEGV. */
19676 end = min (end, row->used[area]);
19677 start = max (0, start);
19678 start = min (end, start);
19679
19680 /* Translate X to frame coordinates. Set last_x to the right
19681 end of the drawing area. */
19682 if (row->full_width_p)
19683 {
19684 /* X is relative to the left edge of W, without scroll bars
19685 or fringes. */
19686 x += WINDOW_LEFT_EDGE_X (w);
19687 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19688 }
19689 else
19690 {
19691 int area_left = window_box_left (w, area);
19692 x += area_left;
19693 area_width = window_box_width (w, area);
19694 last_x = area_left + area_width;
19695 }
19696
19697 /* Build a doubly-linked list of glyph_string structures between
19698 head and tail from what we have to draw. Note that the macro
19699 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19700 the reason we use a separate variable `i'. */
19701 i = start;
19702 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19703 if (tail)
19704 x_reached = tail->x + tail->background_width;
19705 else
19706 x_reached = x;
19707
19708 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19709 the row, redraw some glyphs in front or following the glyph
19710 strings built above. */
19711 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19712 {
19713 int dummy_x = 0;
19714 struct glyph_string *h, *t;
19715
19716 /* Compute overhangs for all glyph strings. */
19717 if (rif->compute_glyph_string_overhangs)
19718 for (s = head; s; s = s->next)
19719 rif->compute_glyph_string_overhangs (s);
19720
19721 /* Prepend glyph strings for glyphs in front of the first glyph
19722 string that are overwritten because of the first glyph
19723 string's left overhang. The background of all strings
19724 prepended must be drawn because the first glyph string
19725 draws over it. */
19726 i = left_overwritten (head);
19727 if (i >= 0)
19728 {
19729 j = i;
19730 BUILD_GLYPH_STRINGS (j, start, h, t,
19731 DRAW_NORMAL_TEXT, dummy_x, last_x);
19732 start = i;
19733 compute_overhangs_and_x (t, head->x, 1);
19734 prepend_glyph_string_lists (&head, &tail, h, t);
19735 clip_head = head;
19736 }
19737
19738 /* Prepend glyph strings for glyphs in front of the first glyph
19739 string that overwrite that glyph string because of their
19740 right overhang. For these strings, only the foreground must
19741 be drawn, because it draws over the glyph string at `head'.
19742 The background must not be drawn because this would overwrite
19743 right overhangs of preceding glyphs for which no glyph
19744 strings exist. */
19745 i = left_overwriting (head);
19746 if (i >= 0)
19747 {
19748 clip_head = head;
19749 BUILD_GLYPH_STRINGS (i, start, h, t,
19750 DRAW_NORMAL_TEXT, dummy_x, last_x);
19751 for (s = h; s; s = s->next)
19752 s->background_filled_p = 1;
19753 compute_overhangs_and_x (t, head->x, 1);
19754 prepend_glyph_string_lists (&head, &tail, h, t);
19755 }
19756
19757 /* Append glyphs strings for glyphs following the last glyph
19758 string tail that are overwritten by tail. The background of
19759 these strings has to be drawn because tail's foreground draws
19760 over it. */
19761 i = right_overwritten (tail);
19762 if (i >= 0)
19763 {
19764 BUILD_GLYPH_STRINGS (end, i, h, t,
19765 DRAW_NORMAL_TEXT, x, last_x);
19766 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19767 append_glyph_string_lists (&head, &tail, h, t);
19768 clip_tail = tail;
19769 }
19770
19771 /* Append glyph strings for glyphs following the last glyph
19772 string tail that overwrite tail. The foreground of such
19773 glyphs has to be drawn because it writes into the background
19774 of tail. The background must not be drawn because it could
19775 paint over the foreground of following glyphs. */
19776 i = right_overwriting (tail);
19777 if (i >= 0)
19778 {
19779 clip_tail = tail;
19780 BUILD_GLYPH_STRINGS (end, i, h, t,
19781 DRAW_NORMAL_TEXT, x, last_x);
19782 for (s = h; s; s = s->next)
19783 s->background_filled_p = 1;
19784 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19785 append_glyph_string_lists (&head, &tail, h, t);
19786 }
19787 if (clip_head || clip_tail)
19788 for (s = head; s; s = s->next)
19789 {
19790 s->clip_head = clip_head;
19791 s->clip_tail = clip_tail;
19792 }
19793 }
19794
19795 /* Draw all strings. */
19796 for (s = head; s; s = s->next)
19797 rif->draw_glyph_string (s);
19798
19799 if (area == TEXT_AREA
19800 && !row->full_width_p
19801 /* When drawing overlapping rows, only the glyph strings'
19802 foreground is drawn, which doesn't erase a cursor
19803 completely. */
19804 && !overlaps)
19805 {
19806 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19807 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19808 : (tail ? tail->x + tail->background_width : x));
19809
19810 int text_left = window_box_left (w, TEXT_AREA);
19811 x0 -= text_left;
19812 x1 -= text_left;
19813
19814 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19815 row->y, MATRIX_ROW_BOTTOM_Y (row));
19816 }
19817
19818 /* Value is the x-position up to which drawn, relative to AREA of W.
19819 This doesn't include parts drawn because of overhangs. */
19820 if (row->full_width_p)
19821 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19822 else
19823 x_reached -= window_box_left (w, area);
19824
19825 RELEASE_HDC (hdc, f);
19826
19827 return x_reached;
19828 }
19829
19830 /* Expand row matrix if too narrow. Don't expand if area
19831 is not present. */
19832
19833 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19834 { \
19835 if (!fonts_changed_p \
19836 && (it->glyph_row->glyphs[area] \
19837 < it->glyph_row->glyphs[area + 1])) \
19838 { \
19839 it->w->ncols_scale_factor++; \
19840 fonts_changed_p = 1; \
19841 } \
19842 }
19843
19844 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19845 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19846
19847 static INLINE void
19848 append_glyph (it)
19849 struct it *it;
19850 {
19851 struct glyph *glyph;
19852 enum glyph_row_area area = it->area;
19853
19854 xassert (it->glyph_row);
19855 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19856
19857 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19858 if (glyph < it->glyph_row->glyphs[area + 1])
19859 {
19860 glyph->charpos = CHARPOS (it->position);
19861 glyph->object = it->object;
19862 glyph->pixel_width = it->pixel_width;
19863 glyph->ascent = it->ascent;
19864 glyph->descent = it->descent;
19865 glyph->voffset = it->voffset;
19866 glyph->type = CHAR_GLYPH;
19867 glyph->multibyte_p = it->multibyte_p;
19868 glyph->left_box_line_p = it->start_of_box_run_p;
19869 glyph->right_box_line_p = it->end_of_box_run_p;
19870 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19871 || it->phys_descent > it->descent);
19872 glyph->padding_p = 0;
19873 glyph->glyph_not_available_p = it->glyph_not_available_p;
19874 glyph->face_id = it->face_id;
19875 glyph->u.ch = it->char_to_display;
19876 glyph->slice = null_glyph_slice;
19877 glyph->font_type = FONT_TYPE_UNKNOWN;
19878 ++it->glyph_row->used[area];
19879 }
19880 else
19881 IT_EXPAND_MATRIX_WIDTH (it, area);
19882 }
19883
19884 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19885 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19886
19887 static INLINE void
19888 append_composite_glyph (it)
19889 struct it *it;
19890 {
19891 struct glyph *glyph;
19892 enum glyph_row_area area = it->area;
19893
19894 xassert (it->glyph_row);
19895
19896 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19897 if (glyph < it->glyph_row->glyphs[area + 1])
19898 {
19899 glyph->charpos = CHARPOS (it->position);
19900 glyph->object = it->object;
19901 glyph->pixel_width = it->pixel_width;
19902 glyph->ascent = it->ascent;
19903 glyph->descent = it->descent;
19904 glyph->voffset = it->voffset;
19905 glyph->type = COMPOSITE_GLYPH;
19906 glyph->multibyte_p = it->multibyte_p;
19907 glyph->left_box_line_p = it->start_of_box_run_p;
19908 glyph->right_box_line_p = it->end_of_box_run_p;
19909 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19910 || it->phys_descent > it->descent);
19911 glyph->padding_p = 0;
19912 glyph->glyph_not_available_p = 0;
19913 glyph->face_id = it->face_id;
19914 glyph->u.cmp_id = it->cmp_id;
19915 glyph->slice = null_glyph_slice;
19916 glyph->font_type = FONT_TYPE_UNKNOWN;
19917 ++it->glyph_row->used[area];
19918 }
19919 else
19920 IT_EXPAND_MATRIX_WIDTH (it, area);
19921 }
19922
19923
19924 /* Change IT->ascent and IT->height according to the setting of
19925 IT->voffset. */
19926
19927 static INLINE void
19928 take_vertical_position_into_account (it)
19929 struct it *it;
19930 {
19931 if (it->voffset)
19932 {
19933 if (it->voffset < 0)
19934 /* Increase the ascent so that we can display the text higher
19935 in the line. */
19936 it->ascent -= it->voffset;
19937 else
19938 /* Increase the descent so that we can display the text lower
19939 in the line. */
19940 it->descent += it->voffset;
19941 }
19942 }
19943
19944
19945 /* Produce glyphs/get display metrics for the image IT is loaded with.
19946 See the description of struct display_iterator in dispextern.h for
19947 an overview of struct display_iterator. */
19948
19949 static void
19950 produce_image_glyph (it)
19951 struct it *it;
19952 {
19953 struct image *img;
19954 struct face *face;
19955 int glyph_ascent, crop;
19956 struct glyph_slice slice;
19957
19958 xassert (it->what == IT_IMAGE);
19959
19960 face = FACE_FROM_ID (it->f, it->face_id);
19961 xassert (face);
19962 /* Make sure X resources of the face is loaded. */
19963 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19964
19965 if (it->image_id < 0)
19966 {
19967 /* Fringe bitmap. */
19968 it->ascent = it->phys_ascent = 0;
19969 it->descent = it->phys_descent = 0;
19970 it->pixel_width = 0;
19971 it->nglyphs = 0;
19972 return;
19973 }
19974
19975 img = IMAGE_FROM_ID (it->f, it->image_id);
19976 xassert (img);
19977 /* Make sure X resources of the image is loaded. */
19978 prepare_image_for_display (it->f, img);
19979
19980 slice.x = slice.y = 0;
19981 slice.width = img->width;
19982 slice.height = img->height;
19983
19984 if (INTEGERP (it->slice.x))
19985 slice.x = XINT (it->slice.x);
19986 else if (FLOATP (it->slice.x))
19987 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19988
19989 if (INTEGERP (it->slice.y))
19990 slice.y = XINT (it->slice.y);
19991 else if (FLOATP (it->slice.y))
19992 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19993
19994 if (INTEGERP (it->slice.width))
19995 slice.width = XINT (it->slice.width);
19996 else if (FLOATP (it->slice.width))
19997 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19998
19999 if (INTEGERP (it->slice.height))
20000 slice.height = XINT (it->slice.height);
20001 else if (FLOATP (it->slice.height))
20002 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20003
20004 if (slice.x >= img->width)
20005 slice.x = img->width;
20006 if (slice.y >= img->height)
20007 slice.y = img->height;
20008 if (slice.x + slice.width >= img->width)
20009 slice.width = img->width - slice.x;
20010 if (slice.y + slice.height > img->height)
20011 slice.height = img->height - slice.y;
20012
20013 if (slice.width == 0 || slice.height == 0)
20014 return;
20015
20016 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20017
20018 it->descent = slice.height - glyph_ascent;
20019 if (slice.y == 0)
20020 it->descent += img->vmargin;
20021 if (slice.y + slice.height == img->height)
20022 it->descent += img->vmargin;
20023 it->phys_descent = it->descent;
20024
20025 it->pixel_width = slice.width;
20026 if (slice.x == 0)
20027 it->pixel_width += img->hmargin;
20028 if (slice.x + slice.width == img->width)
20029 it->pixel_width += img->hmargin;
20030
20031 /* It's quite possible for images to have an ascent greater than
20032 their height, so don't get confused in that case. */
20033 if (it->descent < 0)
20034 it->descent = 0;
20035
20036 #if 0 /* this breaks image tiling */
20037 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20038 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20039 if (face_ascent > it->ascent)
20040 it->ascent = it->phys_ascent = face_ascent;
20041 #endif
20042
20043 it->nglyphs = 1;
20044
20045 if (face->box != FACE_NO_BOX)
20046 {
20047 if (face->box_line_width > 0)
20048 {
20049 if (slice.y == 0)
20050 it->ascent += face->box_line_width;
20051 if (slice.y + slice.height == img->height)
20052 it->descent += face->box_line_width;
20053 }
20054
20055 if (it->start_of_box_run_p && slice.x == 0)
20056 it->pixel_width += abs (face->box_line_width);
20057 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20058 it->pixel_width += abs (face->box_line_width);
20059 }
20060
20061 take_vertical_position_into_account (it);
20062
20063 /* Automatically crop wide image glyphs at right edge so we can
20064 draw the cursor on same display row. */
20065 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20066 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20067 {
20068 it->pixel_width -= crop;
20069 slice.width -= crop;
20070 }
20071
20072 if (it->glyph_row)
20073 {
20074 struct glyph *glyph;
20075 enum glyph_row_area area = it->area;
20076
20077 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20078 if (glyph < it->glyph_row->glyphs[area + 1])
20079 {
20080 glyph->charpos = CHARPOS (it->position);
20081 glyph->object = it->object;
20082 glyph->pixel_width = it->pixel_width;
20083 glyph->ascent = glyph_ascent;
20084 glyph->descent = it->descent;
20085 glyph->voffset = it->voffset;
20086 glyph->type = IMAGE_GLYPH;
20087 glyph->multibyte_p = it->multibyte_p;
20088 glyph->left_box_line_p = it->start_of_box_run_p;
20089 glyph->right_box_line_p = it->end_of_box_run_p;
20090 glyph->overlaps_vertically_p = 0;
20091 glyph->padding_p = 0;
20092 glyph->glyph_not_available_p = 0;
20093 glyph->face_id = it->face_id;
20094 glyph->u.img_id = img->id;
20095 glyph->slice = slice;
20096 glyph->font_type = FONT_TYPE_UNKNOWN;
20097 ++it->glyph_row->used[area];
20098 }
20099 else
20100 IT_EXPAND_MATRIX_WIDTH (it, area);
20101 }
20102 }
20103
20104
20105 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20106 of the glyph, WIDTH and HEIGHT are the width and height of the
20107 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20108
20109 static void
20110 append_stretch_glyph (it, object, width, height, ascent)
20111 struct it *it;
20112 Lisp_Object object;
20113 int width, height;
20114 int ascent;
20115 {
20116 struct glyph *glyph;
20117 enum glyph_row_area area = it->area;
20118
20119 xassert (ascent >= 0 && ascent <= height);
20120
20121 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20122 if (glyph < it->glyph_row->glyphs[area + 1])
20123 {
20124 glyph->charpos = CHARPOS (it->position);
20125 glyph->object = object;
20126 glyph->pixel_width = width;
20127 glyph->ascent = ascent;
20128 glyph->descent = height - ascent;
20129 glyph->voffset = it->voffset;
20130 glyph->type = STRETCH_GLYPH;
20131 glyph->multibyte_p = it->multibyte_p;
20132 glyph->left_box_line_p = it->start_of_box_run_p;
20133 glyph->right_box_line_p = it->end_of_box_run_p;
20134 glyph->overlaps_vertically_p = 0;
20135 glyph->padding_p = 0;
20136 glyph->glyph_not_available_p = 0;
20137 glyph->face_id = it->face_id;
20138 glyph->u.stretch.ascent = ascent;
20139 glyph->u.stretch.height = height;
20140 glyph->slice = null_glyph_slice;
20141 glyph->font_type = FONT_TYPE_UNKNOWN;
20142 ++it->glyph_row->used[area];
20143 }
20144 else
20145 IT_EXPAND_MATRIX_WIDTH (it, area);
20146 }
20147
20148
20149 /* Produce a stretch glyph for iterator IT. IT->object is the value
20150 of the glyph property displayed. The value must be a list
20151 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20152 being recognized:
20153
20154 1. `:width WIDTH' specifies that the space should be WIDTH *
20155 canonical char width wide. WIDTH may be an integer or floating
20156 point number.
20157
20158 2. `:relative-width FACTOR' specifies that the width of the stretch
20159 should be computed from the width of the first character having the
20160 `glyph' property, and should be FACTOR times that width.
20161
20162 3. `:align-to HPOS' specifies that the space should be wide enough
20163 to reach HPOS, a value in canonical character units.
20164
20165 Exactly one of the above pairs must be present.
20166
20167 4. `:height HEIGHT' specifies that the height of the stretch produced
20168 should be HEIGHT, measured in canonical character units.
20169
20170 5. `:relative-height FACTOR' specifies that the height of the
20171 stretch should be FACTOR times the height of the characters having
20172 the glyph property.
20173
20174 Either none or exactly one of 4 or 5 must be present.
20175
20176 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20177 of the stretch should be used for the ascent of the stretch.
20178 ASCENT must be in the range 0 <= ASCENT <= 100. */
20179
20180 static void
20181 produce_stretch_glyph (it)
20182 struct it *it;
20183 {
20184 /* (space :width WIDTH :height HEIGHT ...) */
20185 Lisp_Object prop, plist;
20186 int width = 0, height = 0, align_to = -1;
20187 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20188 int ascent = 0;
20189 double tem;
20190 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20191 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20192
20193 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20194
20195 /* List should start with `space'. */
20196 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20197 plist = XCDR (it->object);
20198
20199 /* Compute the width of the stretch. */
20200 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20201 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20202 {
20203 /* Absolute width `:width WIDTH' specified and valid. */
20204 zero_width_ok_p = 1;
20205 width = (int)tem;
20206 }
20207 else if (prop = Fplist_get (plist, QCrelative_width),
20208 NUMVAL (prop) > 0)
20209 {
20210 /* Relative width `:relative-width FACTOR' specified and valid.
20211 Compute the width of the characters having the `glyph'
20212 property. */
20213 struct it it2;
20214 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20215
20216 it2 = *it;
20217 if (it->multibyte_p)
20218 {
20219 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20220 - IT_BYTEPOS (*it));
20221 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20222 }
20223 else
20224 it2.c = *p, it2.len = 1;
20225
20226 it2.glyph_row = NULL;
20227 it2.what = IT_CHARACTER;
20228 x_produce_glyphs (&it2);
20229 width = NUMVAL (prop) * it2.pixel_width;
20230 }
20231 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20232 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20233 {
20234 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20235 align_to = (align_to < 0
20236 ? 0
20237 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20238 else if (align_to < 0)
20239 align_to = window_box_left_offset (it->w, TEXT_AREA);
20240 width = max (0, (int)tem + align_to - it->current_x);
20241 zero_width_ok_p = 1;
20242 }
20243 else
20244 /* Nothing specified -> width defaults to canonical char width. */
20245 width = FRAME_COLUMN_WIDTH (it->f);
20246
20247 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20248 width = 1;
20249
20250 /* Compute height. */
20251 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20252 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20253 {
20254 height = (int)tem;
20255 zero_height_ok_p = 1;
20256 }
20257 else if (prop = Fplist_get (plist, QCrelative_height),
20258 NUMVAL (prop) > 0)
20259 height = FONT_HEIGHT (font) * NUMVAL (prop);
20260 else
20261 height = FONT_HEIGHT (font);
20262
20263 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20264 height = 1;
20265
20266 /* Compute percentage of height used for ascent. If
20267 `:ascent ASCENT' is present and valid, use that. Otherwise,
20268 derive the ascent from the font in use. */
20269 if (prop = Fplist_get (plist, QCascent),
20270 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20271 ascent = height * NUMVAL (prop) / 100.0;
20272 else if (!NILP (prop)
20273 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20274 ascent = min (max (0, (int)tem), height);
20275 else
20276 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20277
20278 if (width > 0 && !it->truncate_lines_p
20279 && it->current_x + width > it->last_visible_x)
20280 width = it->last_visible_x - it->current_x - 1;
20281
20282 if (width > 0 && height > 0 && it->glyph_row)
20283 {
20284 Lisp_Object object = it->stack[it->sp - 1].string;
20285 if (!STRINGP (object))
20286 object = it->w->buffer;
20287 append_stretch_glyph (it, object, width, height, ascent);
20288 }
20289
20290 it->pixel_width = width;
20291 it->ascent = it->phys_ascent = ascent;
20292 it->descent = it->phys_descent = height - it->ascent;
20293 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20294
20295 take_vertical_position_into_account (it);
20296 }
20297
20298 /* Get line-height and line-spacing property at point.
20299 If line-height has format (HEIGHT TOTAL), return TOTAL
20300 in TOTAL_HEIGHT. */
20301
20302 static Lisp_Object
20303 get_line_height_property (it, prop)
20304 struct it *it;
20305 Lisp_Object prop;
20306 {
20307 Lisp_Object position;
20308
20309 if (STRINGP (it->object))
20310 position = make_number (IT_STRING_CHARPOS (*it));
20311 else if (BUFFERP (it->object))
20312 position = make_number (IT_CHARPOS (*it));
20313 else
20314 return Qnil;
20315
20316 return Fget_char_property (position, prop, it->object);
20317 }
20318
20319 /* Calculate line-height and line-spacing properties.
20320 An integer value specifies explicit pixel value.
20321 A float value specifies relative value to current face height.
20322 A cons (float . face-name) specifies relative value to
20323 height of specified face font.
20324
20325 Returns height in pixels, or nil. */
20326
20327
20328 static Lisp_Object
20329 calc_line_height_property (it, val, font, boff, override)
20330 struct it *it;
20331 Lisp_Object val;
20332 XFontStruct *font;
20333 int boff, override;
20334 {
20335 Lisp_Object face_name = Qnil;
20336 int ascent, descent, height;
20337
20338 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20339 return val;
20340
20341 if (CONSP (val))
20342 {
20343 face_name = XCAR (val);
20344 val = XCDR (val);
20345 if (!NUMBERP (val))
20346 val = make_number (1);
20347 if (NILP (face_name))
20348 {
20349 height = it->ascent + it->descent;
20350 goto scale;
20351 }
20352 }
20353
20354 if (NILP (face_name))
20355 {
20356 font = FRAME_FONT (it->f);
20357 boff = FRAME_BASELINE_OFFSET (it->f);
20358 }
20359 else if (EQ (face_name, Qt))
20360 {
20361 override = 0;
20362 }
20363 else
20364 {
20365 int face_id;
20366 struct face *face;
20367 struct font_info *font_info;
20368
20369 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20370 if (face_id < 0)
20371 return make_number (-1);
20372
20373 face = FACE_FROM_ID (it->f, face_id);
20374 font = face->font;
20375 if (font == NULL)
20376 return make_number (-1);
20377
20378 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20379 boff = font_info->baseline_offset;
20380 if (font_info->vertical_centering)
20381 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20382 }
20383
20384 ascent = FONT_BASE (font) + boff;
20385 descent = FONT_DESCENT (font) - boff;
20386
20387 if (override)
20388 {
20389 it->override_ascent = ascent;
20390 it->override_descent = descent;
20391 it->override_boff = boff;
20392 }
20393
20394 height = ascent + descent;
20395
20396 scale:
20397 if (FLOATP (val))
20398 height = (int)(XFLOAT_DATA (val) * height);
20399 else if (INTEGERP (val))
20400 height *= XINT (val);
20401
20402 return make_number (height);
20403 }
20404
20405
20406 /* RIF:
20407 Produce glyphs/get display metrics for the display element IT is
20408 loaded with. See the description of struct it in dispextern.h
20409 for an overview of struct it. */
20410
20411 void
20412 x_produce_glyphs (it)
20413 struct it *it;
20414 {
20415 int extra_line_spacing = it->extra_line_spacing;
20416
20417 it->glyph_not_available_p = 0;
20418
20419 if (it->what == IT_CHARACTER)
20420 {
20421 XChar2b char2b;
20422 XFontStruct *font;
20423 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20424 XCharStruct *pcm;
20425 int font_not_found_p;
20426 struct font_info *font_info;
20427 int boff; /* baseline offset */
20428 /* We may change it->multibyte_p upon unibyte<->multibyte
20429 conversion. So, save the current value now and restore it
20430 later.
20431
20432 Note: It seems that we don't have to record multibyte_p in
20433 struct glyph because the character code itself tells if or
20434 not the character is multibyte. Thus, in the future, we must
20435 consider eliminating the field `multibyte_p' in the struct
20436 glyph. */
20437 int saved_multibyte_p = it->multibyte_p;
20438
20439 /* Maybe translate single-byte characters to multibyte, or the
20440 other way. */
20441 it->char_to_display = it->c;
20442 if (!ASCII_BYTE_P (it->c))
20443 {
20444 if (unibyte_display_via_language_environment
20445 && SINGLE_BYTE_CHAR_P (it->c)
20446 && (it->c >= 0240
20447 || !NILP (Vnonascii_translation_table)))
20448 {
20449 it->char_to_display = unibyte_char_to_multibyte (it->c);
20450 it->multibyte_p = 1;
20451 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20452 face = FACE_FROM_ID (it->f, it->face_id);
20453 }
20454 else if (!SINGLE_BYTE_CHAR_P (it->c)
20455 && !it->multibyte_p)
20456 {
20457 it->multibyte_p = 1;
20458 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20459 face = FACE_FROM_ID (it->f, it->face_id);
20460 }
20461 }
20462
20463 /* Get font to use. Encode IT->char_to_display. */
20464 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20465 &char2b, it->multibyte_p, 0);
20466 font = face->font;
20467
20468 /* When no suitable font found, use the default font. */
20469 font_not_found_p = font == NULL;
20470 if (font_not_found_p)
20471 {
20472 font = FRAME_FONT (it->f);
20473 boff = FRAME_BASELINE_OFFSET (it->f);
20474 font_info = NULL;
20475 }
20476 else
20477 {
20478 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20479 boff = font_info->baseline_offset;
20480 if (font_info->vertical_centering)
20481 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20482 }
20483
20484 if (it->char_to_display >= ' '
20485 && (!it->multibyte_p || it->char_to_display < 128))
20486 {
20487 /* Either unibyte or ASCII. */
20488 int stretched_p;
20489
20490 it->nglyphs = 1;
20491
20492 pcm = rif->per_char_metric (font, &char2b,
20493 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20494
20495 if (it->override_ascent >= 0)
20496 {
20497 it->ascent = it->override_ascent;
20498 it->descent = it->override_descent;
20499 boff = it->override_boff;
20500 }
20501 else
20502 {
20503 it->ascent = FONT_BASE (font) + boff;
20504 it->descent = FONT_DESCENT (font) - boff;
20505 }
20506
20507 if (pcm)
20508 {
20509 it->phys_ascent = pcm->ascent + boff;
20510 it->phys_descent = pcm->descent - boff;
20511 it->pixel_width = pcm->width;
20512 }
20513 else
20514 {
20515 it->glyph_not_available_p = 1;
20516 it->phys_ascent = it->ascent;
20517 it->phys_descent = it->descent;
20518 it->pixel_width = FONT_WIDTH (font);
20519 }
20520
20521 if (it->constrain_row_ascent_descent_p)
20522 {
20523 if (it->descent > it->max_descent)
20524 {
20525 it->ascent += it->descent - it->max_descent;
20526 it->descent = it->max_descent;
20527 }
20528 if (it->ascent > it->max_ascent)
20529 {
20530 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20531 it->ascent = it->max_ascent;
20532 }
20533 it->phys_ascent = min (it->phys_ascent, it->ascent);
20534 it->phys_descent = min (it->phys_descent, it->descent);
20535 extra_line_spacing = 0;
20536 }
20537
20538 /* If this is a space inside a region of text with
20539 `space-width' property, change its width. */
20540 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20541 if (stretched_p)
20542 it->pixel_width *= XFLOATINT (it->space_width);
20543
20544 /* If face has a box, add the box thickness to the character
20545 height. If character has a box line to the left and/or
20546 right, add the box line width to the character's width. */
20547 if (face->box != FACE_NO_BOX)
20548 {
20549 int thick = face->box_line_width;
20550
20551 if (thick > 0)
20552 {
20553 it->ascent += thick;
20554 it->descent += thick;
20555 }
20556 else
20557 thick = -thick;
20558
20559 if (it->start_of_box_run_p)
20560 it->pixel_width += thick;
20561 if (it->end_of_box_run_p)
20562 it->pixel_width += thick;
20563 }
20564
20565 /* If face has an overline, add the height of the overline
20566 (1 pixel) and a 1 pixel margin to the character height. */
20567 if (face->overline_p)
20568 it->ascent += overline_margin;
20569
20570 if (it->constrain_row_ascent_descent_p)
20571 {
20572 if (it->ascent > it->max_ascent)
20573 it->ascent = it->max_ascent;
20574 if (it->descent > it->max_descent)
20575 it->descent = it->max_descent;
20576 }
20577
20578 take_vertical_position_into_account (it);
20579
20580 /* If we have to actually produce glyphs, do it. */
20581 if (it->glyph_row)
20582 {
20583 if (stretched_p)
20584 {
20585 /* Translate a space with a `space-width' property
20586 into a stretch glyph. */
20587 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20588 / FONT_HEIGHT (font));
20589 append_stretch_glyph (it, it->object, it->pixel_width,
20590 it->ascent + it->descent, ascent);
20591 }
20592 else
20593 append_glyph (it);
20594
20595 /* If characters with lbearing or rbearing are displayed
20596 in this line, record that fact in a flag of the
20597 glyph row. This is used to optimize X output code. */
20598 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20599 it->glyph_row->contains_overlapping_glyphs_p = 1;
20600 }
20601 }
20602 else if (it->char_to_display == '\n')
20603 {
20604 /* A newline has no width but we need the height of the line.
20605 But if previous part of the line set a height, don't
20606 increase that height */
20607
20608 Lisp_Object height;
20609 Lisp_Object total_height = Qnil;
20610
20611 it->override_ascent = -1;
20612 it->pixel_width = 0;
20613 it->nglyphs = 0;
20614
20615 height = get_line_height_property(it, Qline_height);
20616 /* Split (line-height total-height) list */
20617 if (CONSP (height)
20618 && CONSP (XCDR (height))
20619 && NILP (XCDR (XCDR (height))))
20620 {
20621 total_height = XCAR (XCDR (height));
20622 height = XCAR (height);
20623 }
20624 height = calc_line_height_property(it, height, font, boff, 1);
20625
20626 if (it->override_ascent >= 0)
20627 {
20628 it->ascent = it->override_ascent;
20629 it->descent = it->override_descent;
20630 boff = it->override_boff;
20631 }
20632 else
20633 {
20634 it->ascent = FONT_BASE (font) + boff;
20635 it->descent = FONT_DESCENT (font) - boff;
20636 }
20637
20638 if (EQ (height, Qt))
20639 {
20640 if (it->descent > it->max_descent)
20641 {
20642 it->ascent += it->descent - it->max_descent;
20643 it->descent = it->max_descent;
20644 }
20645 if (it->ascent > it->max_ascent)
20646 {
20647 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20648 it->ascent = it->max_ascent;
20649 }
20650 it->phys_ascent = min (it->phys_ascent, it->ascent);
20651 it->phys_descent = min (it->phys_descent, it->descent);
20652 it->constrain_row_ascent_descent_p = 1;
20653 extra_line_spacing = 0;
20654 }
20655 else
20656 {
20657 Lisp_Object spacing;
20658
20659 it->phys_ascent = it->ascent;
20660 it->phys_descent = it->descent;
20661
20662 if ((it->max_ascent > 0 || it->max_descent > 0)
20663 && face->box != FACE_NO_BOX
20664 && face->box_line_width > 0)
20665 {
20666 it->ascent += face->box_line_width;
20667 it->descent += face->box_line_width;
20668 }
20669 if (!NILP (height)
20670 && XINT (height) > it->ascent + it->descent)
20671 it->ascent = XINT (height) - it->descent;
20672
20673 if (!NILP (total_height))
20674 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20675 else
20676 {
20677 spacing = get_line_height_property(it, Qline_spacing);
20678 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20679 }
20680 if (INTEGERP (spacing))
20681 {
20682 extra_line_spacing = XINT (spacing);
20683 if (!NILP (total_height))
20684 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20685 }
20686 }
20687 }
20688 else if (it->char_to_display == '\t')
20689 {
20690 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20691 int x = it->current_x + it->continuation_lines_width;
20692 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20693
20694 /* If the distance from the current position to the next tab
20695 stop is less than a space character width, use the
20696 tab stop after that. */
20697 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20698 next_tab_x += tab_width;
20699
20700 it->pixel_width = next_tab_x - x;
20701 it->nglyphs = 1;
20702 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20703 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20704
20705 if (it->glyph_row)
20706 {
20707 append_stretch_glyph (it, it->object, it->pixel_width,
20708 it->ascent + it->descent, it->ascent);
20709 }
20710 }
20711 else
20712 {
20713 /* A multi-byte character. Assume that the display width of the
20714 character is the width of the character multiplied by the
20715 width of the font. */
20716
20717 /* If we found a font, this font should give us the right
20718 metrics. If we didn't find a font, use the frame's
20719 default font and calculate the width of the character
20720 from the charset width; this is what old redisplay code
20721 did. */
20722
20723 pcm = rif->per_char_metric (font, &char2b,
20724 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20725
20726 if (font_not_found_p || !pcm)
20727 {
20728 int charset = CHAR_CHARSET (it->char_to_display);
20729
20730 it->glyph_not_available_p = 1;
20731 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20732 * CHARSET_WIDTH (charset));
20733 it->phys_ascent = FONT_BASE (font) + boff;
20734 it->phys_descent = FONT_DESCENT (font) - boff;
20735 }
20736 else
20737 {
20738 it->pixel_width = pcm->width;
20739 it->phys_ascent = pcm->ascent + boff;
20740 it->phys_descent = pcm->descent - boff;
20741 if (it->glyph_row
20742 && (pcm->lbearing < 0
20743 || pcm->rbearing > pcm->width))
20744 it->glyph_row->contains_overlapping_glyphs_p = 1;
20745 }
20746 it->nglyphs = 1;
20747 it->ascent = FONT_BASE (font) + boff;
20748 it->descent = FONT_DESCENT (font) - boff;
20749 if (face->box != FACE_NO_BOX)
20750 {
20751 int thick = face->box_line_width;
20752
20753 if (thick > 0)
20754 {
20755 it->ascent += thick;
20756 it->descent += thick;
20757 }
20758 else
20759 thick = - thick;
20760
20761 if (it->start_of_box_run_p)
20762 it->pixel_width += thick;
20763 if (it->end_of_box_run_p)
20764 it->pixel_width += thick;
20765 }
20766
20767 /* If face has an overline, add the height of the overline
20768 (1 pixel) and a 1 pixel margin to the character height. */
20769 if (face->overline_p)
20770 it->ascent += overline_margin;
20771
20772 take_vertical_position_into_account (it);
20773
20774 if (it->glyph_row)
20775 append_glyph (it);
20776 }
20777 it->multibyte_p = saved_multibyte_p;
20778 }
20779 else if (it->what == IT_COMPOSITION)
20780 {
20781 /* Note: A composition is represented as one glyph in the
20782 glyph matrix. There are no padding glyphs. */
20783 XChar2b char2b;
20784 XFontStruct *font;
20785 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20786 XCharStruct *pcm;
20787 int font_not_found_p;
20788 struct font_info *font_info;
20789 int boff; /* baseline offset */
20790 struct composition *cmp = composition_table[it->cmp_id];
20791
20792 /* Maybe translate single-byte characters to multibyte. */
20793 it->char_to_display = it->c;
20794 if (unibyte_display_via_language_environment
20795 && SINGLE_BYTE_CHAR_P (it->c)
20796 && (it->c >= 0240
20797 || (it->c >= 0200
20798 && !NILP (Vnonascii_translation_table))))
20799 {
20800 it->char_to_display = unibyte_char_to_multibyte (it->c);
20801 }
20802
20803 /* Get face and font to use. Encode IT->char_to_display. */
20804 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20805 face = FACE_FROM_ID (it->f, it->face_id);
20806 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20807 &char2b, it->multibyte_p, 0);
20808 font = face->font;
20809
20810 /* When no suitable font found, use the default font. */
20811 font_not_found_p = font == NULL;
20812 if (font_not_found_p)
20813 {
20814 font = FRAME_FONT (it->f);
20815 boff = FRAME_BASELINE_OFFSET (it->f);
20816 font_info = NULL;
20817 }
20818 else
20819 {
20820 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20821 boff = font_info->baseline_offset;
20822 if (font_info->vertical_centering)
20823 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20824 }
20825
20826 /* There are no padding glyphs, so there is only one glyph to
20827 produce for the composition. Important is that pixel_width,
20828 ascent and descent are the values of what is drawn by
20829 draw_glyphs (i.e. the values of the overall glyphs composed). */
20830 it->nglyphs = 1;
20831
20832 /* If we have not yet calculated pixel size data of glyphs of
20833 the composition for the current face font, calculate them
20834 now. Theoretically, we have to check all fonts for the
20835 glyphs, but that requires much time and memory space. So,
20836 here we check only the font of the first glyph. This leads
20837 to incorrect display very rarely, and C-l (recenter) can
20838 correct the display anyway. */
20839 if (cmp->font != (void *) font)
20840 {
20841 /* Ascent and descent of the font of the first character of
20842 this composition (adjusted by baseline offset). Ascent
20843 and descent of overall glyphs should not be less than
20844 them respectively. */
20845 int font_ascent = FONT_BASE (font) + boff;
20846 int font_descent = FONT_DESCENT (font) - boff;
20847 /* Bounding box of the overall glyphs. */
20848 int leftmost, rightmost, lowest, highest;
20849 int i, width, ascent, descent;
20850
20851 cmp->font = (void *) font;
20852
20853 /* Initialize the bounding box. */
20854 if (font_info
20855 && (pcm = rif->per_char_metric (font, &char2b,
20856 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20857 {
20858 width = pcm->width;
20859 ascent = pcm->ascent;
20860 descent = pcm->descent;
20861 }
20862 else
20863 {
20864 width = FONT_WIDTH (font);
20865 ascent = FONT_BASE (font);
20866 descent = FONT_DESCENT (font);
20867 }
20868
20869 rightmost = width;
20870 lowest = - descent + boff;
20871 highest = ascent + boff;
20872 leftmost = 0;
20873
20874 if (font_info
20875 && font_info->default_ascent
20876 && CHAR_TABLE_P (Vuse_default_ascent)
20877 && !NILP (Faref (Vuse_default_ascent,
20878 make_number (it->char_to_display))))
20879 highest = font_info->default_ascent + boff;
20880
20881 /* Draw the first glyph at the normal position. It may be
20882 shifted to right later if some other glyphs are drawn at
20883 the left. */
20884 cmp->offsets[0] = 0;
20885 cmp->offsets[1] = boff;
20886
20887 /* Set cmp->offsets for the remaining glyphs. */
20888 for (i = 1; i < cmp->glyph_len; i++)
20889 {
20890 int left, right, btm, top;
20891 int ch = COMPOSITION_GLYPH (cmp, i);
20892 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20893
20894 face = FACE_FROM_ID (it->f, face_id);
20895 get_char_face_and_encoding (it->f, ch, face->id,
20896 &char2b, it->multibyte_p, 0);
20897 font = face->font;
20898 if (font == NULL)
20899 {
20900 font = FRAME_FONT (it->f);
20901 boff = FRAME_BASELINE_OFFSET (it->f);
20902 font_info = NULL;
20903 }
20904 else
20905 {
20906 font_info
20907 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20908 boff = font_info->baseline_offset;
20909 if (font_info->vertical_centering)
20910 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20911 }
20912
20913 if (font_info
20914 && (pcm = rif->per_char_metric (font, &char2b,
20915 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20916 {
20917 width = pcm->width;
20918 ascent = pcm->ascent;
20919 descent = pcm->descent;
20920 }
20921 else
20922 {
20923 width = FONT_WIDTH (font);
20924 ascent = 1;
20925 descent = 0;
20926 }
20927
20928 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20929 {
20930 /* Relative composition with or without
20931 alternate chars. */
20932 left = (leftmost + rightmost - width) / 2;
20933 btm = - descent + boff;
20934 if (font_info && font_info->relative_compose
20935 && (! CHAR_TABLE_P (Vignore_relative_composition)
20936 || NILP (Faref (Vignore_relative_composition,
20937 make_number (ch)))))
20938 {
20939
20940 if (- descent >= font_info->relative_compose)
20941 /* One extra pixel between two glyphs. */
20942 btm = highest + 1;
20943 else if (ascent <= 0)
20944 /* One extra pixel between two glyphs. */
20945 btm = lowest - 1 - ascent - descent;
20946 }
20947 }
20948 else
20949 {
20950 /* A composition rule is specified by an integer
20951 value that encodes global and new reference
20952 points (GREF and NREF). GREF and NREF are
20953 specified by numbers as below:
20954
20955 0---1---2 -- ascent
20956 | |
20957 | |
20958 | |
20959 9--10--11 -- center
20960 | |
20961 ---3---4---5--- baseline
20962 | |
20963 6---7---8 -- descent
20964 */
20965 int rule = COMPOSITION_RULE (cmp, i);
20966 int gref, nref, grefx, grefy, nrefx, nrefy;
20967
20968 COMPOSITION_DECODE_RULE (rule, gref, nref);
20969 grefx = gref % 3, nrefx = nref % 3;
20970 grefy = gref / 3, nrefy = nref / 3;
20971
20972 left = (leftmost
20973 + grefx * (rightmost - leftmost) / 2
20974 - nrefx * width / 2);
20975 btm = ((grefy == 0 ? highest
20976 : grefy == 1 ? 0
20977 : grefy == 2 ? lowest
20978 : (highest + lowest) / 2)
20979 - (nrefy == 0 ? ascent + descent
20980 : nrefy == 1 ? descent - boff
20981 : nrefy == 2 ? 0
20982 : (ascent + descent) / 2));
20983 }
20984
20985 cmp->offsets[i * 2] = left;
20986 cmp->offsets[i * 2 + 1] = btm + descent;
20987
20988 /* Update the bounding box of the overall glyphs. */
20989 right = left + width;
20990 top = btm + descent + ascent;
20991 if (left < leftmost)
20992 leftmost = left;
20993 if (right > rightmost)
20994 rightmost = right;
20995 if (top > highest)
20996 highest = top;
20997 if (btm < lowest)
20998 lowest = btm;
20999 }
21000
21001 /* If there are glyphs whose x-offsets are negative,
21002 shift all glyphs to the right and make all x-offsets
21003 non-negative. */
21004 if (leftmost < 0)
21005 {
21006 for (i = 0; i < cmp->glyph_len; i++)
21007 cmp->offsets[i * 2] -= leftmost;
21008 rightmost -= leftmost;
21009 }
21010
21011 cmp->pixel_width = rightmost;
21012 cmp->ascent = highest;
21013 cmp->descent = - lowest;
21014 if (cmp->ascent < font_ascent)
21015 cmp->ascent = font_ascent;
21016 if (cmp->descent < font_descent)
21017 cmp->descent = font_descent;
21018 }
21019
21020 it->pixel_width = cmp->pixel_width;
21021 it->ascent = it->phys_ascent = cmp->ascent;
21022 it->descent = it->phys_descent = cmp->descent;
21023
21024 if (face->box != FACE_NO_BOX)
21025 {
21026 int thick = face->box_line_width;
21027
21028 if (thick > 0)
21029 {
21030 it->ascent += thick;
21031 it->descent += thick;
21032 }
21033 else
21034 thick = - thick;
21035
21036 if (it->start_of_box_run_p)
21037 it->pixel_width += thick;
21038 if (it->end_of_box_run_p)
21039 it->pixel_width += thick;
21040 }
21041
21042 /* If face has an overline, add the height of the overline
21043 (1 pixel) and a 1 pixel margin to the character height. */
21044 if (face->overline_p)
21045 it->ascent += overline_margin;
21046
21047 take_vertical_position_into_account (it);
21048
21049 if (it->glyph_row)
21050 append_composite_glyph (it);
21051 }
21052 else if (it->what == IT_IMAGE)
21053 produce_image_glyph (it);
21054 else if (it->what == IT_STRETCH)
21055 produce_stretch_glyph (it);
21056
21057 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21058 because this isn't true for images with `:ascent 100'. */
21059 xassert (it->ascent >= 0 && it->descent >= 0);
21060 if (it->area == TEXT_AREA)
21061 it->current_x += it->pixel_width;
21062
21063 if (extra_line_spacing > 0)
21064 {
21065 it->descent += extra_line_spacing;
21066 if (extra_line_spacing > it->max_extra_line_spacing)
21067 it->max_extra_line_spacing = extra_line_spacing;
21068 }
21069
21070 it->max_ascent = max (it->max_ascent, it->ascent);
21071 it->max_descent = max (it->max_descent, it->descent);
21072 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21073 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21074 }
21075
21076 /* EXPORT for RIF:
21077 Output LEN glyphs starting at START at the nominal cursor position.
21078 Advance the nominal cursor over the text. The global variable
21079 updated_window contains the window being updated, updated_row is
21080 the glyph row being updated, and updated_area is the area of that
21081 row being updated. */
21082
21083 void
21084 x_write_glyphs (start, len)
21085 struct glyph *start;
21086 int len;
21087 {
21088 int x, hpos;
21089
21090 xassert (updated_window && updated_row);
21091 BLOCK_INPUT;
21092
21093 /* Write glyphs. */
21094
21095 hpos = start - updated_row->glyphs[updated_area];
21096 x = draw_glyphs (updated_window, output_cursor.x,
21097 updated_row, updated_area,
21098 hpos, hpos + len,
21099 DRAW_NORMAL_TEXT, 0);
21100
21101 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21102 if (updated_area == TEXT_AREA
21103 && updated_window->phys_cursor_on_p
21104 && updated_window->phys_cursor.vpos == output_cursor.vpos
21105 && updated_window->phys_cursor.hpos >= hpos
21106 && updated_window->phys_cursor.hpos < hpos + len)
21107 updated_window->phys_cursor_on_p = 0;
21108
21109 UNBLOCK_INPUT;
21110
21111 /* Advance the output cursor. */
21112 output_cursor.hpos += len;
21113 output_cursor.x = x;
21114 }
21115
21116
21117 /* EXPORT for RIF:
21118 Insert LEN glyphs from START at the nominal cursor position. */
21119
21120 void
21121 x_insert_glyphs (start, len)
21122 struct glyph *start;
21123 int len;
21124 {
21125 struct frame *f;
21126 struct window *w;
21127 int line_height, shift_by_width, shifted_region_width;
21128 struct glyph_row *row;
21129 struct glyph *glyph;
21130 int frame_x, frame_y, hpos;
21131
21132 xassert (updated_window && updated_row);
21133 BLOCK_INPUT;
21134 w = updated_window;
21135 f = XFRAME (WINDOW_FRAME (w));
21136
21137 /* Get the height of the line we are in. */
21138 row = updated_row;
21139 line_height = row->height;
21140
21141 /* Get the width of the glyphs to insert. */
21142 shift_by_width = 0;
21143 for (glyph = start; glyph < start + len; ++glyph)
21144 shift_by_width += glyph->pixel_width;
21145
21146 /* Get the width of the region to shift right. */
21147 shifted_region_width = (window_box_width (w, updated_area)
21148 - output_cursor.x
21149 - shift_by_width);
21150
21151 /* Shift right. */
21152 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21153 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21154
21155 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21156 line_height, shift_by_width);
21157
21158 /* Write the glyphs. */
21159 hpos = start - row->glyphs[updated_area];
21160 draw_glyphs (w, output_cursor.x, row, updated_area,
21161 hpos, hpos + len,
21162 DRAW_NORMAL_TEXT, 0);
21163
21164 /* Advance the output cursor. */
21165 output_cursor.hpos += len;
21166 output_cursor.x += shift_by_width;
21167 UNBLOCK_INPUT;
21168 }
21169
21170
21171 /* EXPORT for RIF:
21172 Erase the current text line from the nominal cursor position
21173 (inclusive) to pixel column TO_X (exclusive). The idea is that
21174 everything from TO_X onward is already erased.
21175
21176 TO_X is a pixel position relative to updated_area of
21177 updated_window. TO_X == -1 means clear to the end of this area. */
21178
21179 void
21180 x_clear_end_of_line (to_x)
21181 int to_x;
21182 {
21183 struct frame *f;
21184 struct window *w = updated_window;
21185 int max_x, min_y, max_y;
21186 int from_x, from_y, to_y;
21187
21188 xassert (updated_window && updated_row);
21189 f = XFRAME (w->frame);
21190
21191 if (updated_row->full_width_p)
21192 max_x = WINDOW_TOTAL_WIDTH (w);
21193 else
21194 max_x = window_box_width (w, updated_area);
21195 max_y = window_text_bottom_y (w);
21196
21197 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21198 of window. For TO_X > 0, truncate to end of drawing area. */
21199 if (to_x == 0)
21200 return;
21201 else if (to_x < 0)
21202 to_x = max_x;
21203 else
21204 to_x = min (to_x, max_x);
21205
21206 to_y = min (max_y, output_cursor.y + updated_row->height);
21207
21208 /* Notice if the cursor will be cleared by this operation. */
21209 if (!updated_row->full_width_p)
21210 notice_overwritten_cursor (w, updated_area,
21211 output_cursor.x, -1,
21212 updated_row->y,
21213 MATRIX_ROW_BOTTOM_Y (updated_row));
21214
21215 from_x = output_cursor.x;
21216
21217 /* Translate to frame coordinates. */
21218 if (updated_row->full_width_p)
21219 {
21220 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21221 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21222 }
21223 else
21224 {
21225 int area_left = window_box_left (w, updated_area);
21226 from_x += area_left;
21227 to_x += area_left;
21228 }
21229
21230 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21231 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21232 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21233
21234 /* Prevent inadvertently clearing to end of the X window. */
21235 if (to_x > from_x && to_y > from_y)
21236 {
21237 BLOCK_INPUT;
21238 rif->clear_frame_area (f, from_x, from_y,
21239 to_x - from_x, to_y - from_y);
21240 UNBLOCK_INPUT;
21241 }
21242 }
21243
21244 #endif /* HAVE_WINDOW_SYSTEM */
21245
21246
21247 \f
21248 /***********************************************************************
21249 Cursor types
21250 ***********************************************************************/
21251
21252 /* Value is the internal representation of the specified cursor type
21253 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21254 of the bar cursor. */
21255
21256 static enum text_cursor_kinds
21257 get_specified_cursor_type (arg, width)
21258 Lisp_Object arg;
21259 int *width;
21260 {
21261 enum text_cursor_kinds type;
21262
21263 if (NILP (arg))
21264 return NO_CURSOR;
21265
21266 if (EQ (arg, Qbox))
21267 return FILLED_BOX_CURSOR;
21268
21269 if (EQ (arg, Qhollow))
21270 return HOLLOW_BOX_CURSOR;
21271
21272 if (EQ (arg, Qbar))
21273 {
21274 *width = 2;
21275 return BAR_CURSOR;
21276 }
21277
21278 if (CONSP (arg)
21279 && EQ (XCAR (arg), Qbar)
21280 && INTEGERP (XCDR (arg))
21281 && XINT (XCDR (arg)) >= 0)
21282 {
21283 *width = XINT (XCDR (arg));
21284 return BAR_CURSOR;
21285 }
21286
21287 if (EQ (arg, Qhbar))
21288 {
21289 *width = 2;
21290 return HBAR_CURSOR;
21291 }
21292
21293 if (CONSP (arg)
21294 && EQ (XCAR (arg), Qhbar)
21295 && INTEGERP (XCDR (arg))
21296 && XINT (XCDR (arg)) >= 0)
21297 {
21298 *width = XINT (XCDR (arg));
21299 return HBAR_CURSOR;
21300 }
21301
21302 /* Treat anything unknown as "hollow box cursor".
21303 It was bad to signal an error; people have trouble fixing
21304 .Xdefaults with Emacs, when it has something bad in it. */
21305 type = HOLLOW_BOX_CURSOR;
21306
21307 return type;
21308 }
21309
21310 /* Set the default cursor types for specified frame. */
21311 void
21312 set_frame_cursor_types (f, arg)
21313 struct frame *f;
21314 Lisp_Object arg;
21315 {
21316 int width;
21317 Lisp_Object tem;
21318
21319 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21320 FRAME_CURSOR_WIDTH (f) = width;
21321
21322 /* By default, set up the blink-off state depending on the on-state. */
21323
21324 tem = Fassoc (arg, Vblink_cursor_alist);
21325 if (!NILP (tem))
21326 {
21327 FRAME_BLINK_OFF_CURSOR (f)
21328 = get_specified_cursor_type (XCDR (tem), &width);
21329 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21330 }
21331 else
21332 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21333 }
21334
21335
21336 /* Return the cursor we want to be displayed in window W. Return
21337 width of bar/hbar cursor through WIDTH arg. Return with
21338 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21339 (i.e. if the `system caret' should track this cursor).
21340
21341 In a mini-buffer window, we want the cursor only to appear if we
21342 are reading input from this window. For the selected window, we
21343 want the cursor type given by the frame parameter or buffer local
21344 setting of cursor-type. If explicitly marked off, draw no cursor.
21345 In all other cases, we want a hollow box cursor. */
21346
21347 static enum text_cursor_kinds
21348 get_window_cursor_type (w, glyph, width, active_cursor)
21349 struct window *w;
21350 struct glyph *glyph;
21351 int *width;
21352 int *active_cursor;
21353 {
21354 struct frame *f = XFRAME (w->frame);
21355 struct buffer *b = XBUFFER (w->buffer);
21356 int cursor_type = DEFAULT_CURSOR;
21357 Lisp_Object alt_cursor;
21358 int non_selected = 0;
21359
21360 *active_cursor = 1;
21361
21362 /* Echo area */
21363 if (cursor_in_echo_area
21364 && FRAME_HAS_MINIBUF_P (f)
21365 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21366 {
21367 if (w == XWINDOW (echo_area_window))
21368 {
21369 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21370 {
21371 *width = FRAME_CURSOR_WIDTH (f);
21372 return FRAME_DESIRED_CURSOR (f);
21373 }
21374 else
21375 return get_specified_cursor_type (b->cursor_type, width);
21376 }
21377
21378 *active_cursor = 0;
21379 non_selected = 1;
21380 }
21381
21382 /* Detect a nonselected window or nonselected frame. */
21383 else if (w != XWINDOW (f->selected_window)
21384 #ifdef HAVE_WINDOW_SYSTEM
21385 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21386 #endif
21387 )
21388 {
21389 *active_cursor = 0;
21390
21391 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21392 return NO_CURSOR;
21393
21394 non_selected = 1;
21395 }
21396
21397 /* Never display a cursor in a window in which cursor-type is nil. */
21398 if (NILP (b->cursor_type))
21399 return NO_CURSOR;
21400
21401 /* Get the normal cursor type for this window. */
21402 if (EQ (b->cursor_type, Qt))
21403 {
21404 cursor_type = FRAME_DESIRED_CURSOR (f);
21405 *width = FRAME_CURSOR_WIDTH (f);
21406 }
21407 else
21408 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21409
21410 /* Use cursor-in-non-selected-windows instead
21411 for non-selected window or frame. */
21412 if (non_selected)
21413 {
21414 alt_cursor = b->cursor_in_non_selected_windows;
21415 if (!EQ (Qt, alt_cursor))
21416 return get_specified_cursor_type (alt_cursor, width);
21417 /* t means modify the normal cursor type. */
21418 if (cursor_type == FILLED_BOX_CURSOR)
21419 cursor_type = HOLLOW_BOX_CURSOR;
21420 else if (cursor_type == BAR_CURSOR && *width > 1)
21421 --*width;
21422 return cursor_type;
21423 }
21424
21425 /* Use normal cursor if not blinked off. */
21426 if (!w->cursor_off_p)
21427 {
21428 #ifdef HAVE_WINDOW_SYSTEM
21429 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21430 {
21431 if (cursor_type == FILLED_BOX_CURSOR)
21432 {
21433 /* Using a block cursor on large images can be very annoying.
21434 So use a hollow cursor for "large" images.
21435 If image is not transparent (no mask), also use hollow cursor. */
21436 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21437 if (img != NULL && IMAGEP (img->spec))
21438 {
21439 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21440 where N = size of default frame font size.
21441 This should cover most of the "tiny" icons people may use. */
21442 if (!img->mask
21443 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21444 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21445 cursor_type = HOLLOW_BOX_CURSOR;
21446 }
21447 }
21448 else if (cursor_type != NO_CURSOR)
21449 {
21450 /* Display current only supports BOX and HOLLOW cursors for images.
21451 So for now, unconditionally use a HOLLOW cursor when cursor is
21452 not a solid box cursor. */
21453 cursor_type = HOLLOW_BOX_CURSOR;
21454 }
21455 }
21456 #endif
21457 return cursor_type;
21458 }
21459
21460 /* Cursor is blinked off, so determine how to "toggle" it. */
21461
21462 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21463 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21464 return get_specified_cursor_type (XCDR (alt_cursor), width);
21465
21466 /* Then see if frame has specified a specific blink off cursor type. */
21467 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21468 {
21469 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21470 return FRAME_BLINK_OFF_CURSOR (f);
21471 }
21472
21473 #if 0
21474 /* Some people liked having a permanently visible blinking cursor,
21475 while others had very strong opinions against it. So it was
21476 decided to remove it. KFS 2003-09-03 */
21477
21478 /* Finally perform built-in cursor blinking:
21479 filled box <-> hollow box
21480 wide [h]bar <-> narrow [h]bar
21481 narrow [h]bar <-> no cursor
21482 other type <-> no cursor */
21483
21484 if (cursor_type == FILLED_BOX_CURSOR)
21485 return HOLLOW_BOX_CURSOR;
21486
21487 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21488 {
21489 *width = 1;
21490 return cursor_type;
21491 }
21492 #endif
21493
21494 return NO_CURSOR;
21495 }
21496
21497
21498 #ifdef HAVE_WINDOW_SYSTEM
21499
21500 /* Notice when the text cursor of window W has been completely
21501 overwritten by a drawing operation that outputs glyphs in AREA
21502 starting at X0 and ending at X1 in the line starting at Y0 and
21503 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21504 the rest of the line after X0 has been written. Y coordinates
21505 are window-relative. */
21506
21507 static void
21508 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21509 struct window *w;
21510 enum glyph_row_area area;
21511 int x0, y0, x1, y1;
21512 {
21513 int cx0, cx1, cy0, cy1;
21514 struct glyph_row *row;
21515
21516 if (!w->phys_cursor_on_p)
21517 return;
21518 if (area != TEXT_AREA)
21519 return;
21520
21521 if (w->phys_cursor.vpos < 0
21522 || w->phys_cursor.vpos >= w->current_matrix->nrows
21523 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21524 !(row->enabled_p && row->displays_text_p)))
21525 return;
21526
21527 if (row->cursor_in_fringe_p)
21528 {
21529 row->cursor_in_fringe_p = 0;
21530 draw_fringe_bitmap (w, row, 0);
21531 w->phys_cursor_on_p = 0;
21532 return;
21533 }
21534
21535 cx0 = w->phys_cursor.x;
21536 cx1 = cx0 + w->phys_cursor_width;
21537 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21538 return;
21539
21540 /* The cursor image will be completely removed from the
21541 screen if the output area intersects the cursor area in
21542 y-direction. When we draw in [y0 y1[, and some part of
21543 the cursor is at y < y0, that part must have been drawn
21544 before. When scrolling, the cursor is erased before
21545 actually scrolling, so we don't come here. When not
21546 scrolling, the rows above the old cursor row must have
21547 changed, and in this case these rows must have written
21548 over the cursor image.
21549
21550 Likewise if part of the cursor is below y1, with the
21551 exception of the cursor being in the first blank row at
21552 the buffer and window end because update_text_area
21553 doesn't draw that row. (Except when it does, but
21554 that's handled in update_text_area.) */
21555
21556 cy0 = w->phys_cursor.y;
21557 cy1 = cy0 + w->phys_cursor_height;
21558 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21559 return;
21560
21561 w->phys_cursor_on_p = 0;
21562 }
21563
21564 #endif /* HAVE_WINDOW_SYSTEM */
21565
21566 \f
21567 /************************************************************************
21568 Mouse Face
21569 ************************************************************************/
21570
21571 #ifdef HAVE_WINDOW_SYSTEM
21572
21573 /* EXPORT for RIF:
21574 Fix the display of area AREA of overlapping row ROW in window W
21575 with respect to the overlapping part OVERLAPS. */
21576
21577 void
21578 x_fix_overlapping_area (w, row, area, overlaps)
21579 struct window *w;
21580 struct glyph_row *row;
21581 enum glyph_row_area area;
21582 int overlaps;
21583 {
21584 int i, x;
21585
21586 BLOCK_INPUT;
21587
21588 x = 0;
21589 for (i = 0; i < row->used[area];)
21590 {
21591 if (row->glyphs[area][i].overlaps_vertically_p)
21592 {
21593 int start = i, start_x = x;
21594
21595 do
21596 {
21597 x += row->glyphs[area][i].pixel_width;
21598 ++i;
21599 }
21600 while (i < row->used[area]
21601 && row->glyphs[area][i].overlaps_vertically_p);
21602
21603 draw_glyphs (w, start_x, row, area,
21604 start, i,
21605 DRAW_NORMAL_TEXT, overlaps);
21606 }
21607 else
21608 {
21609 x += row->glyphs[area][i].pixel_width;
21610 ++i;
21611 }
21612 }
21613
21614 UNBLOCK_INPUT;
21615 }
21616
21617
21618 /* EXPORT:
21619 Draw the cursor glyph of window W in glyph row ROW. See the
21620 comment of draw_glyphs for the meaning of HL. */
21621
21622 void
21623 draw_phys_cursor_glyph (w, row, hl)
21624 struct window *w;
21625 struct glyph_row *row;
21626 enum draw_glyphs_face hl;
21627 {
21628 /* If cursor hpos is out of bounds, don't draw garbage. This can
21629 happen in mini-buffer windows when switching between echo area
21630 glyphs and mini-buffer. */
21631 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21632 {
21633 int on_p = w->phys_cursor_on_p;
21634 int x1;
21635 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21636 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21637 hl, 0);
21638 w->phys_cursor_on_p = on_p;
21639
21640 if (hl == DRAW_CURSOR)
21641 w->phys_cursor_width = x1 - w->phys_cursor.x;
21642 /* When we erase the cursor, and ROW is overlapped by other
21643 rows, make sure that these overlapping parts of other rows
21644 are redrawn. */
21645 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21646 {
21647 w->phys_cursor_width = x1 - w->phys_cursor.x;
21648
21649 if (row > w->current_matrix->rows
21650 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21651 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21652 OVERLAPS_ERASED_CURSOR);
21653
21654 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21655 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21656 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21657 OVERLAPS_ERASED_CURSOR);
21658 }
21659 }
21660 }
21661
21662
21663 /* EXPORT:
21664 Erase the image of a cursor of window W from the screen. */
21665
21666 void
21667 erase_phys_cursor (w)
21668 struct window *w;
21669 {
21670 struct frame *f = XFRAME (w->frame);
21671 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21672 int hpos = w->phys_cursor.hpos;
21673 int vpos = w->phys_cursor.vpos;
21674 int mouse_face_here_p = 0;
21675 struct glyph_matrix *active_glyphs = w->current_matrix;
21676 struct glyph_row *cursor_row;
21677 struct glyph *cursor_glyph;
21678 enum draw_glyphs_face hl;
21679
21680 /* No cursor displayed or row invalidated => nothing to do on the
21681 screen. */
21682 if (w->phys_cursor_type == NO_CURSOR)
21683 goto mark_cursor_off;
21684
21685 /* VPOS >= active_glyphs->nrows means that window has been resized.
21686 Don't bother to erase the cursor. */
21687 if (vpos >= active_glyphs->nrows)
21688 goto mark_cursor_off;
21689
21690 /* If row containing cursor is marked invalid, there is nothing we
21691 can do. */
21692 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21693 if (!cursor_row->enabled_p)
21694 goto mark_cursor_off;
21695
21696 /* If line spacing is > 0, old cursor may only be partially visible in
21697 window after split-window. So adjust visible height. */
21698 cursor_row->visible_height = min (cursor_row->visible_height,
21699 window_text_bottom_y (w) - cursor_row->y);
21700
21701 /* If row is completely invisible, don't attempt to delete a cursor which
21702 isn't there. This can happen if cursor is at top of a window, and
21703 we switch to a buffer with a header line in that window. */
21704 if (cursor_row->visible_height <= 0)
21705 goto mark_cursor_off;
21706
21707 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21708 if (cursor_row->cursor_in_fringe_p)
21709 {
21710 cursor_row->cursor_in_fringe_p = 0;
21711 draw_fringe_bitmap (w, cursor_row, 0);
21712 goto mark_cursor_off;
21713 }
21714
21715 /* This can happen when the new row is shorter than the old one.
21716 In this case, either draw_glyphs or clear_end_of_line
21717 should have cleared the cursor. Note that we wouldn't be
21718 able to erase the cursor in this case because we don't have a
21719 cursor glyph at hand. */
21720 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21721 goto mark_cursor_off;
21722
21723 /* If the cursor is in the mouse face area, redisplay that when
21724 we clear the cursor. */
21725 if (! NILP (dpyinfo->mouse_face_window)
21726 && w == XWINDOW (dpyinfo->mouse_face_window)
21727 && (vpos > dpyinfo->mouse_face_beg_row
21728 || (vpos == dpyinfo->mouse_face_beg_row
21729 && hpos >= dpyinfo->mouse_face_beg_col))
21730 && (vpos < dpyinfo->mouse_face_end_row
21731 || (vpos == dpyinfo->mouse_face_end_row
21732 && hpos < dpyinfo->mouse_face_end_col))
21733 /* Don't redraw the cursor's spot in mouse face if it is at the
21734 end of a line (on a newline). The cursor appears there, but
21735 mouse highlighting does not. */
21736 && cursor_row->used[TEXT_AREA] > hpos)
21737 mouse_face_here_p = 1;
21738
21739 /* Maybe clear the display under the cursor. */
21740 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21741 {
21742 int x, y, left_x;
21743 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21744 int width;
21745
21746 cursor_glyph = get_phys_cursor_glyph (w);
21747 if (cursor_glyph == NULL)
21748 goto mark_cursor_off;
21749
21750 width = cursor_glyph->pixel_width;
21751 left_x = window_box_left_offset (w, TEXT_AREA);
21752 x = w->phys_cursor.x;
21753 if (x < left_x)
21754 width -= left_x - x;
21755 width = min (width, window_box_width (w, TEXT_AREA) - x);
21756 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21757 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21758
21759 if (width > 0)
21760 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21761 }
21762
21763 /* Erase the cursor by redrawing the character underneath it. */
21764 if (mouse_face_here_p)
21765 hl = DRAW_MOUSE_FACE;
21766 else
21767 hl = DRAW_NORMAL_TEXT;
21768 draw_phys_cursor_glyph (w, cursor_row, hl);
21769
21770 mark_cursor_off:
21771 w->phys_cursor_on_p = 0;
21772 w->phys_cursor_type = NO_CURSOR;
21773 }
21774
21775
21776 /* EXPORT:
21777 Display or clear cursor of window W. If ON is zero, clear the
21778 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21779 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21780
21781 void
21782 display_and_set_cursor (w, on, hpos, vpos, x, y)
21783 struct window *w;
21784 int on, hpos, vpos, x, y;
21785 {
21786 struct frame *f = XFRAME (w->frame);
21787 int new_cursor_type;
21788 int new_cursor_width;
21789 int active_cursor;
21790 struct glyph_row *glyph_row;
21791 struct glyph *glyph;
21792
21793 /* This is pointless on invisible frames, and dangerous on garbaged
21794 windows and frames; in the latter case, the frame or window may
21795 be in the midst of changing its size, and x and y may be off the
21796 window. */
21797 if (! FRAME_VISIBLE_P (f)
21798 || FRAME_GARBAGED_P (f)
21799 || vpos >= w->current_matrix->nrows
21800 || hpos >= w->current_matrix->matrix_w)
21801 return;
21802
21803 /* If cursor is off and we want it off, return quickly. */
21804 if (!on && !w->phys_cursor_on_p)
21805 return;
21806
21807 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21808 /* If cursor row is not enabled, we don't really know where to
21809 display the cursor. */
21810 if (!glyph_row->enabled_p)
21811 {
21812 w->phys_cursor_on_p = 0;
21813 return;
21814 }
21815
21816 glyph = NULL;
21817 if (!glyph_row->exact_window_width_line_p
21818 || hpos < glyph_row->used[TEXT_AREA])
21819 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21820
21821 xassert (interrupt_input_blocked);
21822
21823 /* Set new_cursor_type to the cursor we want to be displayed. */
21824 new_cursor_type = get_window_cursor_type (w, glyph,
21825 &new_cursor_width, &active_cursor);
21826
21827 /* If cursor is currently being shown and we don't want it to be or
21828 it is in the wrong place, or the cursor type is not what we want,
21829 erase it. */
21830 if (w->phys_cursor_on_p
21831 && (!on
21832 || w->phys_cursor.x != x
21833 || w->phys_cursor.y != y
21834 || new_cursor_type != w->phys_cursor_type
21835 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21836 && new_cursor_width != w->phys_cursor_width)))
21837 erase_phys_cursor (w);
21838
21839 /* Don't check phys_cursor_on_p here because that flag is only set
21840 to zero in some cases where we know that the cursor has been
21841 completely erased, to avoid the extra work of erasing the cursor
21842 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21843 still not be visible, or it has only been partly erased. */
21844 if (on)
21845 {
21846 w->phys_cursor_ascent = glyph_row->ascent;
21847 w->phys_cursor_height = glyph_row->height;
21848
21849 /* Set phys_cursor_.* before x_draw_.* is called because some
21850 of them may need the information. */
21851 w->phys_cursor.x = x;
21852 w->phys_cursor.y = glyph_row->y;
21853 w->phys_cursor.hpos = hpos;
21854 w->phys_cursor.vpos = vpos;
21855 }
21856
21857 rif->draw_window_cursor (w, glyph_row, x, y,
21858 new_cursor_type, new_cursor_width,
21859 on, active_cursor);
21860 }
21861
21862
21863 /* Switch the display of W's cursor on or off, according to the value
21864 of ON. */
21865
21866 static void
21867 update_window_cursor (w, on)
21868 struct window *w;
21869 int on;
21870 {
21871 /* Don't update cursor in windows whose frame is in the process
21872 of being deleted. */
21873 if (w->current_matrix)
21874 {
21875 BLOCK_INPUT;
21876 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21877 w->phys_cursor.x, w->phys_cursor.y);
21878 UNBLOCK_INPUT;
21879 }
21880 }
21881
21882
21883 /* Call update_window_cursor with parameter ON_P on all leaf windows
21884 in the window tree rooted at W. */
21885
21886 static void
21887 update_cursor_in_window_tree (w, on_p)
21888 struct window *w;
21889 int on_p;
21890 {
21891 while (w)
21892 {
21893 if (!NILP (w->hchild))
21894 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21895 else if (!NILP (w->vchild))
21896 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21897 else
21898 update_window_cursor (w, on_p);
21899
21900 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21901 }
21902 }
21903
21904
21905 /* EXPORT:
21906 Display the cursor on window W, or clear it, according to ON_P.
21907 Don't change the cursor's position. */
21908
21909 void
21910 x_update_cursor (f, on_p)
21911 struct frame *f;
21912 int on_p;
21913 {
21914 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21915 }
21916
21917
21918 /* EXPORT:
21919 Clear the cursor of window W to background color, and mark the
21920 cursor as not shown. This is used when the text where the cursor
21921 is is about to be rewritten. */
21922
21923 void
21924 x_clear_cursor (w)
21925 struct window *w;
21926 {
21927 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21928 update_window_cursor (w, 0);
21929 }
21930
21931
21932 /* EXPORT:
21933 Display the active region described by mouse_face_* according to DRAW. */
21934
21935 void
21936 show_mouse_face (dpyinfo, draw)
21937 Display_Info *dpyinfo;
21938 enum draw_glyphs_face draw;
21939 {
21940 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21941 struct frame *f = XFRAME (WINDOW_FRAME (w));
21942
21943 if (/* If window is in the process of being destroyed, don't bother
21944 to do anything. */
21945 w->current_matrix != NULL
21946 /* Don't update mouse highlight if hidden */
21947 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21948 /* Recognize when we are called to operate on rows that don't exist
21949 anymore. This can happen when a window is split. */
21950 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21951 {
21952 int phys_cursor_on_p = w->phys_cursor_on_p;
21953 struct glyph_row *row, *first, *last;
21954
21955 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21956 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21957
21958 for (row = first; row <= last && row->enabled_p; ++row)
21959 {
21960 int start_hpos, end_hpos, start_x;
21961
21962 /* For all but the first row, the highlight starts at column 0. */
21963 if (row == first)
21964 {
21965 start_hpos = dpyinfo->mouse_face_beg_col;
21966 start_x = dpyinfo->mouse_face_beg_x;
21967 }
21968 else
21969 {
21970 start_hpos = 0;
21971 start_x = 0;
21972 }
21973
21974 if (row == last)
21975 end_hpos = dpyinfo->mouse_face_end_col;
21976 else
21977 {
21978 end_hpos = row->used[TEXT_AREA];
21979 if (draw == DRAW_NORMAL_TEXT)
21980 row->fill_line_p = 1; /* Clear to end of line */
21981 }
21982
21983 if (end_hpos > start_hpos)
21984 {
21985 draw_glyphs (w, start_x, row, TEXT_AREA,
21986 start_hpos, end_hpos,
21987 draw, 0);
21988
21989 row->mouse_face_p
21990 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21991 }
21992 }
21993
21994 /* When we've written over the cursor, arrange for it to
21995 be displayed again. */
21996 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21997 {
21998 BLOCK_INPUT;
21999 display_and_set_cursor (w, 1,
22000 w->phys_cursor.hpos, w->phys_cursor.vpos,
22001 w->phys_cursor.x, w->phys_cursor.y);
22002 UNBLOCK_INPUT;
22003 }
22004 }
22005
22006 /* Change the mouse cursor. */
22007 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22008 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22009 else if (draw == DRAW_MOUSE_FACE)
22010 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22011 else
22012 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22013 }
22014
22015 /* EXPORT:
22016 Clear out the mouse-highlighted active region.
22017 Redraw it un-highlighted first. Value is non-zero if mouse
22018 face was actually drawn unhighlighted. */
22019
22020 int
22021 clear_mouse_face (dpyinfo)
22022 Display_Info *dpyinfo;
22023 {
22024 int cleared = 0;
22025
22026 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22027 {
22028 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22029 cleared = 1;
22030 }
22031
22032 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22033 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22034 dpyinfo->mouse_face_window = Qnil;
22035 dpyinfo->mouse_face_overlay = Qnil;
22036 return cleared;
22037 }
22038
22039
22040 /* EXPORT:
22041 Non-zero if physical cursor of window W is within mouse face. */
22042
22043 int
22044 cursor_in_mouse_face_p (w)
22045 struct window *w;
22046 {
22047 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22048 int in_mouse_face = 0;
22049
22050 if (WINDOWP (dpyinfo->mouse_face_window)
22051 && XWINDOW (dpyinfo->mouse_face_window) == w)
22052 {
22053 int hpos = w->phys_cursor.hpos;
22054 int vpos = w->phys_cursor.vpos;
22055
22056 if (vpos >= dpyinfo->mouse_face_beg_row
22057 && vpos <= dpyinfo->mouse_face_end_row
22058 && (vpos > dpyinfo->mouse_face_beg_row
22059 || hpos >= dpyinfo->mouse_face_beg_col)
22060 && (vpos < dpyinfo->mouse_face_end_row
22061 || hpos < dpyinfo->mouse_face_end_col
22062 || dpyinfo->mouse_face_past_end))
22063 in_mouse_face = 1;
22064 }
22065
22066 return in_mouse_face;
22067 }
22068
22069
22070
22071 \f
22072 /* Find the glyph matrix position of buffer position CHARPOS in window
22073 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22074 current glyphs must be up to date. If CHARPOS is above window
22075 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22076 of last line in W. In the row containing CHARPOS, stop before glyphs
22077 having STOP as object. */
22078
22079 #if 1 /* This is a version of fast_find_position that's more correct
22080 in the presence of hscrolling, for example. I didn't install
22081 it right away because the problem fixed is minor, it failed
22082 in 20.x as well, and I think it's too risky to install
22083 so near the release of 21.1. 2001-09-25 gerd. */
22084
22085 #ifndef HAVE_CARBON
22086 static
22087 #endif
22088 int
22089 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22090 struct window *w;
22091 int charpos;
22092 int *hpos, *vpos, *x, *y;
22093 Lisp_Object stop;
22094 {
22095 struct glyph_row *row, *first;
22096 struct glyph *glyph, *end;
22097 int past_end = 0;
22098
22099 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22100 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22101 {
22102 *x = first->x;
22103 *y = first->y;
22104 *hpos = 0;
22105 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22106 return 1;
22107 }
22108
22109 row = row_containing_pos (w, charpos, first, NULL, 0);
22110 if (row == NULL)
22111 {
22112 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22113 past_end = 1;
22114 }
22115
22116 /* If whole rows or last part of a row came from a display overlay,
22117 row_containing_pos will skip over such rows because their end pos
22118 equals the start pos of the overlay or interval.
22119
22120 Move back if we have a STOP object and previous row's
22121 end glyph came from STOP. */
22122 if (!NILP (stop))
22123 {
22124 struct glyph_row *prev;
22125 while ((prev = row - 1, prev >= first)
22126 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22127 && prev->used[TEXT_AREA] > 0)
22128 {
22129 struct glyph *beg = prev->glyphs[TEXT_AREA];
22130 glyph = beg + prev->used[TEXT_AREA];
22131 while (--glyph >= beg
22132 && INTEGERP (glyph->object));
22133 if (glyph < beg
22134 || !EQ (stop, glyph->object))
22135 break;
22136 row = prev;
22137 }
22138 }
22139
22140 *x = row->x;
22141 *y = row->y;
22142 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22143
22144 glyph = row->glyphs[TEXT_AREA];
22145 end = glyph + row->used[TEXT_AREA];
22146
22147 /* Skip over glyphs not having an object at the start of the row.
22148 These are special glyphs like truncation marks on terminal
22149 frames. */
22150 if (row->displays_text_p)
22151 while (glyph < end
22152 && INTEGERP (glyph->object)
22153 && !EQ (stop, glyph->object)
22154 && glyph->charpos < 0)
22155 {
22156 *x += glyph->pixel_width;
22157 ++glyph;
22158 }
22159
22160 while (glyph < end
22161 && !INTEGERP (glyph->object)
22162 && !EQ (stop, glyph->object)
22163 && (!BUFFERP (glyph->object)
22164 || glyph->charpos < charpos))
22165 {
22166 *x += glyph->pixel_width;
22167 ++glyph;
22168 }
22169
22170 *hpos = glyph - row->glyphs[TEXT_AREA];
22171 return !past_end;
22172 }
22173
22174 #else /* not 1 */
22175
22176 static int
22177 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22178 struct window *w;
22179 int pos;
22180 int *hpos, *vpos, *x, *y;
22181 Lisp_Object stop;
22182 {
22183 int i;
22184 int lastcol;
22185 int maybe_next_line_p = 0;
22186 int line_start_position;
22187 int yb = window_text_bottom_y (w);
22188 struct glyph_row *row, *best_row;
22189 int row_vpos, best_row_vpos;
22190 int current_x;
22191
22192 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22193 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22194
22195 while (row->y < yb)
22196 {
22197 if (row->used[TEXT_AREA])
22198 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22199 else
22200 line_start_position = 0;
22201
22202 if (line_start_position > pos)
22203 break;
22204 /* If the position sought is the end of the buffer,
22205 don't include the blank lines at the bottom of the window. */
22206 else if (line_start_position == pos
22207 && pos == BUF_ZV (XBUFFER (w->buffer)))
22208 {
22209 maybe_next_line_p = 1;
22210 break;
22211 }
22212 else if (line_start_position > 0)
22213 {
22214 best_row = row;
22215 best_row_vpos = row_vpos;
22216 }
22217
22218 if (row->y + row->height >= yb)
22219 break;
22220
22221 ++row;
22222 ++row_vpos;
22223 }
22224
22225 /* Find the right column within BEST_ROW. */
22226 lastcol = 0;
22227 current_x = best_row->x;
22228 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22229 {
22230 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22231 int charpos = glyph->charpos;
22232
22233 if (BUFFERP (glyph->object))
22234 {
22235 if (charpos == pos)
22236 {
22237 *hpos = i;
22238 *vpos = best_row_vpos;
22239 *x = current_x;
22240 *y = best_row->y;
22241 return 1;
22242 }
22243 else if (charpos > pos)
22244 break;
22245 }
22246 else if (EQ (glyph->object, stop))
22247 break;
22248
22249 if (charpos > 0)
22250 lastcol = i;
22251 current_x += glyph->pixel_width;
22252 }
22253
22254 /* If we're looking for the end of the buffer,
22255 and we didn't find it in the line we scanned,
22256 use the start of the following line. */
22257 if (maybe_next_line_p)
22258 {
22259 ++best_row;
22260 ++best_row_vpos;
22261 lastcol = 0;
22262 current_x = best_row->x;
22263 }
22264
22265 *vpos = best_row_vpos;
22266 *hpos = lastcol + 1;
22267 *x = current_x;
22268 *y = best_row->y;
22269 return 0;
22270 }
22271
22272 #endif /* not 1 */
22273
22274
22275 /* Find the position of the glyph for position POS in OBJECT in
22276 window W's current matrix, and return in *X, *Y the pixel
22277 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22278
22279 RIGHT_P non-zero means return the position of the right edge of the
22280 glyph, RIGHT_P zero means return the left edge position.
22281
22282 If no glyph for POS exists in the matrix, return the position of
22283 the glyph with the next smaller position that is in the matrix, if
22284 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22285 exists in the matrix, return the position of the glyph with the
22286 next larger position in OBJECT.
22287
22288 Value is non-zero if a glyph was found. */
22289
22290 static int
22291 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22292 struct window *w;
22293 int pos;
22294 Lisp_Object object;
22295 int *hpos, *vpos, *x, *y;
22296 int right_p;
22297 {
22298 int yb = window_text_bottom_y (w);
22299 struct glyph_row *r;
22300 struct glyph *best_glyph = NULL;
22301 struct glyph_row *best_row = NULL;
22302 int best_x = 0;
22303
22304 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22305 r->enabled_p && r->y < yb;
22306 ++r)
22307 {
22308 struct glyph *g = r->glyphs[TEXT_AREA];
22309 struct glyph *e = g + r->used[TEXT_AREA];
22310 int gx;
22311
22312 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22313 if (EQ (g->object, object))
22314 {
22315 if (g->charpos == pos)
22316 {
22317 best_glyph = g;
22318 best_x = gx;
22319 best_row = r;
22320 goto found;
22321 }
22322 else if (best_glyph == NULL
22323 || ((abs (g->charpos - pos)
22324 < abs (best_glyph->charpos - pos))
22325 && (right_p
22326 ? g->charpos < pos
22327 : g->charpos > pos)))
22328 {
22329 best_glyph = g;
22330 best_x = gx;
22331 best_row = r;
22332 }
22333 }
22334 }
22335
22336 found:
22337
22338 if (best_glyph)
22339 {
22340 *x = best_x;
22341 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22342
22343 if (right_p)
22344 {
22345 *x += best_glyph->pixel_width;
22346 ++*hpos;
22347 }
22348
22349 *y = best_row->y;
22350 *vpos = best_row - w->current_matrix->rows;
22351 }
22352
22353 return best_glyph != NULL;
22354 }
22355
22356
22357 /* See if position X, Y is within a hot-spot of an image. */
22358
22359 static int
22360 on_hot_spot_p (hot_spot, x, y)
22361 Lisp_Object hot_spot;
22362 int x, y;
22363 {
22364 if (!CONSP (hot_spot))
22365 return 0;
22366
22367 if (EQ (XCAR (hot_spot), Qrect))
22368 {
22369 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22370 Lisp_Object rect = XCDR (hot_spot);
22371 Lisp_Object tem;
22372 if (!CONSP (rect))
22373 return 0;
22374 if (!CONSP (XCAR (rect)))
22375 return 0;
22376 if (!CONSP (XCDR (rect)))
22377 return 0;
22378 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22379 return 0;
22380 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22381 return 0;
22382 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22383 return 0;
22384 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22385 return 0;
22386 return 1;
22387 }
22388 else if (EQ (XCAR (hot_spot), Qcircle))
22389 {
22390 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22391 Lisp_Object circ = XCDR (hot_spot);
22392 Lisp_Object lr, lx0, ly0;
22393 if (CONSP (circ)
22394 && CONSP (XCAR (circ))
22395 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22396 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22397 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22398 {
22399 double r = XFLOATINT (lr);
22400 double dx = XINT (lx0) - x;
22401 double dy = XINT (ly0) - y;
22402 return (dx * dx + dy * dy <= r * r);
22403 }
22404 }
22405 else if (EQ (XCAR (hot_spot), Qpoly))
22406 {
22407 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22408 if (VECTORP (XCDR (hot_spot)))
22409 {
22410 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22411 Lisp_Object *poly = v->contents;
22412 int n = v->size;
22413 int i;
22414 int inside = 0;
22415 Lisp_Object lx, ly;
22416 int x0, y0;
22417
22418 /* Need an even number of coordinates, and at least 3 edges. */
22419 if (n < 6 || n & 1)
22420 return 0;
22421
22422 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22423 If count is odd, we are inside polygon. Pixels on edges
22424 may or may not be included depending on actual geometry of the
22425 polygon. */
22426 if ((lx = poly[n-2], !INTEGERP (lx))
22427 || (ly = poly[n-1], !INTEGERP (lx)))
22428 return 0;
22429 x0 = XINT (lx), y0 = XINT (ly);
22430 for (i = 0; i < n; i += 2)
22431 {
22432 int x1 = x0, y1 = y0;
22433 if ((lx = poly[i], !INTEGERP (lx))
22434 || (ly = poly[i+1], !INTEGERP (ly)))
22435 return 0;
22436 x0 = XINT (lx), y0 = XINT (ly);
22437
22438 /* Does this segment cross the X line? */
22439 if (x0 >= x)
22440 {
22441 if (x1 >= x)
22442 continue;
22443 }
22444 else if (x1 < x)
22445 continue;
22446 if (y > y0 && y > y1)
22447 continue;
22448 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22449 inside = !inside;
22450 }
22451 return inside;
22452 }
22453 }
22454 /* If we don't understand the format, pretend we're not in the hot-spot. */
22455 return 0;
22456 }
22457
22458 Lisp_Object
22459 find_hot_spot (map, x, y)
22460 Lisp_Object map;
22461 int x, y;
22462 {
22463 while (CONSP (map))
22464 {
22465 if (CONSP (XCAR (map))
22466 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22467 return XCAR (map);
22468 map = XCDR (map);
22469 }
22470
22471 return Qnil;
22472 }
22473
22474 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22475 3, 3, 0,
22476 doc: /* Lookup in image map MAP coordinates X and Y.
22477 An image map is an alist where each element has the format (AREA ID PLIST).
22478 An AREA is specified as either a rectangle, a circle, or a polygon:
22479 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22480 pixel coordinates of the upper left and bottom right corners.
22481 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22482 and the radius of the circle; r may be a float or integer.
22483 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22484 vector describes one corner in the polygon.
22485 Returns the alist element for the first matching AREA in MAP. */)
22486 (map, x, y)
22487 Lisp_Object map;
22488 Lisp_Object x, y;
22489 {
22490 if (NILP (map))
22491 return Qnil;
22492
22493 CHECK_NUMBER (x);
22494 CHECK_NUMBER (y);
22495
22496 return find_hot_spot (map, XINT (x), XINT (y));
22497 }
22498
22499
22500 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22501 static void
22502 define_frame_cursor1 (f, cursor, pointer)
22503 struct frame *f;
22504 Cursor cursor;
22505 Lisp_Object pointer;
22506 {
22507 /* Do not change cursor shape while dragging mouse. */
22508 if (!NILP (do_mouse_tracking))
22509 return;
22510
22511 if (!NILP (pointer))
22512 {
22513 if (EQ (pointer, Qarrow))
22514 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22515 else if (EQ (pointer, Qhand))
22516 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22517 else if (EQ (pointer, Qtext))
22518 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22519 else if (EQ (pointer, intern ("hdrag")))
22520 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22521 #ifdef HAVE_X_WINDOWS
22522 else if (EQ (pointer, intern ("vdrag")))
22523 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22524 #endif
22525 else if (EQ (pointer, intern ("hourglass")))
22526 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22527 else if (EQ (pointer, Qmodeline))
22528 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22529 else
22530 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22531 }
22532
22533 if (cursor != No_Cursor)
22534 rif->define_frame_cursor (f, cursor);
22535 }
22536
22537 /* Take proper action when mouse has moved to the mode or header line
22538 or marginal area AREA of window W, x-position X and y-position Y.
22539 X is relative to the start of the text display area of W, so the
22540 width of bitmap areas and scroll bars must be subtracted to get a
22541 position relative to the start of the mode line. */
22542
22543 static void
22544 note_mode_line_or_margin_highlight (window, x, y, area)
22545 Lisp_Object window;
22546 int x, y;
22547 enum window_part area;
22548 {
22549 struct window *w = XWINDOW (window);
22550 struct frame *f = XFRAME (w->frame);
22551 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22552 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22553 Lisp_Object pointer = Qnil;
22554 int charpos, dx, dy, width, height;
22555 Lisp_Object string, object = Qnil;
22556 Lisp_Object pos, help;
22557
22558 Lisp_Object mouse_face;
22559 int original_x_pixel = x;
22560 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22561 struct glyph_row *row;
22562
22563 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22564 {
22565 int x0;
22566 struct glyph *end;
22567
22568 string = mode_line_string (w, area, &x, &y, &charpos,
22569 &object, &dx, &dy, &width, &height);
22570
22571 row = (area == ON_MODE_LINE
22572 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22573 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22574
22575 /* Find glyph */
22576 if (row->mode_line_p && row->enabled_p)
22577 {
22578 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22579 end = glyph + row->used[TEXT_AREA];
22580
22581 for (x0 = original_x_pixel;
22582 glyph < end && x0 >= glyph->pixel_width;
22583 ++glyph)
22584 x0 -= glyph->pixel_width;
22585
22586 if (glyph >= end)
22587 glyph = NULL;
22588 }
22589 }
22590 else
22591 {
22592 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22593 string = marginal_area_string (w, area, &x, &y, &charpos,
22594 &object, &dx, &dy, &width, &height);
22595 }
22596
22597 help = Qnil;
22598
22599 if (IMAGEP (object))
22600 {
22601 Lisp_Object image_map, hotspot;
22602 if ((image_map = Fplist_get (XCDR (object), QCmap),
22603 !NILP (image_map))
22604 && (hotspot = find_hot_spot (image_map, dx, dy),
22605 CONSP (hotspot))
22606 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22607 {
22608 Lisp_Object area_id, plist;
22609
22610 area_id = XCAR (hotspot);
22611 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22612 If so, we could look for mouse-enter, mouse-leave
22613 properties in PLIST (and do something...). */
22614 hotspot = XCDR (hotspot);
22615 if (CONSP (hotspot)
22616 && (plist = XCAR (hotspot), CONSP (plist)))
22617 {
22618 pointer = Fplist_get (plist, Qpointer);
22619 if (NILP (pointer))
22620 pointer = Qhand;
22621 help = Fplist_get (plist, Qhelp_echo);
22622 if (!NILP (help))
22623 {
22624 help_echo_string = help;
22625 /* Is this correct? ++kfs */
22626 XSETWINDOW (help_echo_window, w);
22627 help_echo_object = w->buffer;
22628 help_echo_pos = charpos;
22629 }
22630 }
22631 }
22632 if (NILP (pointer))
22633 pointer = Fplist_get (XCDR (object), QCpointer);
22634 }
22635
22636 if (STRINGP (string))
22637 {
22638 pos = make_number (charpos);
22639 /* If we're on a string with `help-echo' text property, arrange
22640 for the help to be displayed. This is done by setting the
22641 global variable help_echo_string to the help string. */
22642 if (NILP (help))
22643 {
22644 help = Fget_text_property (pos, Qhelp_echo, string);
22645 if (!NILP (help))
22646 {
22647 help_echo_string = help;
22648 XSETWINDOW (help_echo_window, w);
22649 help_echo_object = string;
22650 help_echo_pos = charpos;
22651 }
22652 }
22653
22654 if (NILP (pointer))
22655 pointer = Fget_text_property (pos, Qpointer, string);
22656
22657 /* Change the mouse pointer according to what is under X/Y. */
22658 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22659 {
22660 Lisp_Object map;
22661 map = Fget_text_property (pos, Qlocal_map, string);
22662 if (!KEYMAPP (map))
22663 map = Fget_text_property (pos, Qkeymap, string);
22664 if (!KEYMAPP (map))
22665 cursor = dpyinfo->vertical_scroll_bar_cursor;
22666 }
22667
22668 /* Change the mouse face according to what is under X/Y. */
22669 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22670 if (!NILP (mouse_face)
22671 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22672 && glyph)
22673 {
22674 Lisp_Object b, e;
22675
22676 struct glyph * tmp_glyph;
22677
22678 int gpos;
22679 int gseq_length;
22680 int total_pixel_width;
22681 int ignore;
22682
22683 int vpos, hpos;
22684
22685 b = Fprevious_single_property_change (make_number (charpos + 1),
22686 Qmouse_face, string, Qnil);
22687 if (NILP (b))
22688 b = make_number (0);
22689
22690 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22691 if (NILP (e))
22692 e = make_number (SCHARS (string));
22693
22694 /* Calculate the position(glyph position: GPOS) of GLYPH in
22695 displayed string. GPOS is different from CHARPOS.
22696
22697 CHARPOS is the position of glyph in internal string
22698 object. A mode line string format has structures which
22699 is converted to a flatten by emacs lisp interpreter.
22700 The internal string is an element of the structures.
22701 The displayed string is the flatten string. */
22702 gpos = 0;
22703 if (glyph > row_start_glyph)
22704 {
22705 tmp_glyph = glyph - 1;
22706 while (tmp_glyph >= row_start_glyph
22707 && tmp_glyph->charpos >= XINT (b)
22708 && EQ (tmp_glyph->object, glyph->object))
22709 {
22710 tmp_glyph--;
22711 gpos++;
22712 }
22713 }
22714
22715 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22716 displayed string holding GLYPH.
22717
22718 GSEQ_LENGTH is different from SCHARS (STRING).
22719 SCHARS (STRING) returns the length of the internal string. */
22720 for (tmp_glyph = glyph, gseq_length = gpos;
22721 tmp_glyph->charpos < XINT (e);
22722 tmp_glyph++, gseq_length++)
22723 {
22724 if (!EQ (tmp_glyph->object, glyph->object))
22725 break;
22726 }
22727
22728 total_pixel_width = 0;
22729 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22730 total_pixel_width += tmp_glyph->pixel_width;
22731
22732 /* Pre calculation of re-rendering position */
22733 vpos = (x - gpos);
22734 hpos = (area == ON_MODE_LINE
22735 ? (w->current_matrix)->nrows - 1
22736 : 0);
22737
22738 /* If the re-rendering position is included in the last
22739 re-rendering area, we should do nothing. */
22740 if ( EQ (window, dpyinfo->mouse_face_window)
22741 && dpyinfo->mouse_face_beg_col <= vpos
22742 && vpos < dpyinfo->mouse_face_end_col
22743 && dpyinfo->mouse_face_beg_row == hpos )
22744 return;
22745
22746 if (clear_mouse_face (dpyinfo))
22747 cursor = No_Cursor;
22748
22749 dpyinfo->mouse_face_beg_col = vpos;
22750 dpyinfo->mouse_face_beg_row = hpos;
22751
22752 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22753 dpyinfo->mouse_face_beg_y = 0;
22754
22755 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22756 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22757
22758 dpyinfo->mouse_face_end_x = 0;
22759 dpyinfo->mouse_face_end_y = 0;
22760
22761 dpyinfo->mouse_face_past_end = 0;
22762 dpyinfo->mouse_face_window = window;
22763
22764 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22765 charpos,
22766 0, 0, 0, &ignore,
22767 glyph->face_id, 1);
22768 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22769
22770 if (NILP (pointer))
22771 pointer = Qhand;
22772 }
22773 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22774 clear_mouse_face (dpyinfo);
22775 }
22776 define_frame_cursor1 (f, cursor, pointer);
22777 }
22778
22779
22780 /* EXPORT:
22781 Take proper action when the mouse has moved to position X, Y on
22782 frame F as regards highlighting characters that have mouse-face
22783 properties. Also de-highlighting chars where the mouse was before.
22784 X and Y can be negative or out of range. */
22785
22786 void
22787 note_mouse_highlight (f, x, y)
22788 struct frame *f;
22789 int x, y;
22790 {
22791 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22792 enum window_part part;
22793 Lisp_Object window;
22794 struct window *w;
22795 Cursor cursor = No_Cursor;
22796 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22797 struct buffer *b;
22798
22799 /* When a menu is active, don't highlight because this looks odd. */
22800 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22801 if (popup_activated ())
22802 return;
22803 #endif
22804
22805 if (NILP (Vmouse_highlight)
22806 || !f->glyphs_initialized_p)
22807 return;
22808
22809 dpyinfo->mouse_face_mouse_x = x;
22810 dpyinfo->mouse_face_mouse_y = y;
22811 dpyinfo->mouse_face_mouse_frame = f;
22812
22813 if (dpyinfo->mouse_face_defer)
22814 return;
22815
22816 if (gc_in_progress)
22817 {
22818 dpyinfo->mouse_face_deferred_gc = 1;
22819 return;
22820 }
22821
22822 /* Which window is that in? */
22823 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22824
22825 /* If we were displaying active text in another window, clear that.
22826 Also clear if we move out of text area in same window. */
22827 if (! EQ (window, dpyinfo->mouse_face_window)
22828 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22829 && !NILP (dpyinfo->mouse_face_window)))
22830 clear_mouse_face (dpyinfo);
22831
22832 /* Not on a window -> return. */
22833 if (!WINDOWP (window))
22834 return;
22835
22836 /* Reset help_echo_string. It will get recomputed below. */
22837 help_echo_string = Qnil;
22838
22839 /* Convert to window-relative pixel coordinates. */
22840 w = XWINDOW (window);
22841 frame_to_window_pixel_xy (w, &x, &y);
22842
22843 /* Handle tool-bar window differently since it doesn't display a
22844 buffer. */
22845 if (EQ (window, f->tool_bar_window))
22846 {
22847 note_tool_bar_highlight (f, x, y);
22848 return;
22849 }
22850
22851 /* Mouse is on the mode, header line or margin? */
22852 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22853 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22854 {
22855 note_mode_line_or_margin_highlight (window, x, y, part);
22856 return;
22857 }
22858
22859 if (part == ON_VERTICAL_BORDER)
22860 {
22861 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22862 help_echo_string = build_string ("drag-mouse-1: resize");
22863 }
22864 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22865 || part == ON_SCROLL_BAR)
22866 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22867 else
22868 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22869
22870 /* Are we in a window whose display is up to date?
22871 And verify the buffer's text has not changed. */
22872 b = XBUFFER (w->buffer);
22873 if (part == ON_TEXT
22874 && EQ (w->window_end_valid, w->buffer)
22875 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22876 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22877 {
22878 int hpos, vpos, pos, i, dx, dy, area;
22879 struct glyph *glyph;
22880 Lisp_Object object;
22881 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22882 Lisp_Object *overlay_vec = NULL;
22883 int noverlays;
22884 struct buffer *obuf;
22885 int obegv, ozv, same_region;
22886
22887 /* Find the glyph under X/Y. */
22888 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22889
22890 /* Look for :pointer property on image. */
22891 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22892 {
22893 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22894 if (img != NULL && IMAGEP (img->spec))
22895 {
22896 Lisp_Object image_map, hotspot;
22897 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22898 !NILP (image_map))
22899 && (hotspot = find_hot_spot (image_map,
22900 glyph->slice.x + dx,
22901 glyph->slice.y + dy),
22902 CONSP (hotspot))
22903 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22904 {
22905 Lisp_Object area_id, plist;
22906
22907 area_id = XCAR (hotspot);
22908 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22909 If so, we could look for mouse-enter, mouse-leave
22910 properties in PLIST (and do something...). */
22911 hotspot = XCDR (hotspot);
22912 if (CONSP (hotspot)
22913 && (plist = XCAR (hotspot), CONSP (plist)))
22914 {
22915 pointer = Fplist_get (plist, Qpointer);
22916 if (NILP (pointer))
22917 pointer = Qhand;
22918 help_echo_string = Fplist_get (plist, Qhelp_echo);
22919 if (!NILP (help_echo_string))
22920 {
22921 help_echo_window = window;
22922 help_echo_object = glyph->object;
22923 help_echo_pos = glyph->charpos;
22924 }
22925 }
22926 }
22927 if (NILP (pointer))
22928 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22929 }
22930 }
22931
22932 /* Clear mouse face if X/Y not over text. */
22933 if (glyph == NULL
22934 || area != TEXT_AREA
22935 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22936 {
22937 if (clear_mouse_face (dpyinfo))
22938 cursor = No_Cursor;
22939 if (NILP (pointer))
22940 {
22941 if (area != TEXT_AREA)
22942 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22943 else
22944 pointer = Vvoid_text_area_pointer;
22945 }
22946 goto set_cursor;
22947 }
22948
22949 pos = glyph->charpos;
22950 object = glyph->object;
22951 if (!STRINGP (object) && !BUFFERP (object))
22952 goto set_cursor;
22953
22954 /* If we get an out-of-range value, return now; avoid an error. */
22955 if (BUFFERP (object) && pos > BUF_Z (b))
22956 goto set_cursor;
22957
22958 /* Make the window's buffer temporarily current for
22959 overlays_at and compute_char_face. */
22960 obuf = current_buffer;
22961 current_buffer = b;
22962 obegv = BEGV;
22963 ozv = ZV;
22964 BEGV = BEG;
22965 ZV = Z;
22966
22967 /* Is this char mouse-active or does it have help-echo? */
22968 position = make_number (pos);
22969
22970 if (BUFFERP (object))
22971 {
22972 /* Put all the overlays we want in a vector in overlay_vec. */
22973 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22974 /* Sort overlays into increasing priority order. */
22975 noverlays = sort_overlays (overlay_vec, noverlays, w);
22976 }
22977 else
22978 noverlays = 0;
22979
22980 same_region = (EQ (window, dpyinfo->mouse_face_window)
22981 && vpos >= dpyinfo->mouse_face_beg_row
22982 && vpos <= dpyinfo->mouse_face_end_row
22983 && (vpos > dpyinfo->mouse_face_beg_row
22984 || hpos >= dpyinfo->mouse_face_beg_col)
22985 && (vpos < dpyinfo->mouse_face_end_row
22986 || hpos < dpyinfo->mouse_face_end_col
22987 || dpyinfo->mouse_face_past_end));
22988
22989 if (same_region)
22990 cursor = No_Cursor;
22991
22992 /* Check mouse-face highlighting. */
22993 if (! same_region
22994 /* If there exists an overlay with mouse-face overlapping
22995 the one we are currently highlighting, we have to
22996 check if we enter the overlapping overlay, and then
22997 highlight only that. */
22998 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22999 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23000 {
23001 /* Find the highest priority overlay that has a mouse-face
23002 property. */
23003 overlay = Qnil;
23004 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23005 {
23006 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23007 if (!NILP (mouse_face))
23008 overlay = overlay_vec[i];
23009 }
23010
23011 /* If we're actually highlighting the same overlay as
23012 before, there's no need to do that again. */
23013 if (!NILP (overlay)
23014 && EQ (overlay, dpyinfo->mouse_face_overlay))
23015 goto check_help_echo;
23016
23017 dpyinfo->mouse_face_overlay = overlay;
23018
23019 /* Clear the display of the old active region, if any. */
23020 if (clear_mouse_face (dpyinfo))
23021 cursor = No_Cursor;
23022
23023 /* If no overlay applies, get a text property. */
23024 if (NILP (overlay))
23025 mouse_face = Fget_text_property (position, Qmouse_face, object);
23026
23027 /* Handle the overlay case. */
23028 if (!NILP (overlay))
23029 {
23030 /* Find the range of text around this char that
23031 should be active. */
23032 Lisp_Object before, after;
23033 int ignore;
23034
23035 before = Foverlay_start (overlay);
23036 after = Foverlay_end (overlay);
23037 /* Record this as the current active region. */
23038 fast_find_position (w, XFASTINT (before),
23039 &dpyinfo->mouse_face_beg_col,
23040 &dpyinfo->mouse_face_beg_row,
23041 &dpyinfo->mouse_face_beg_x,
23042 &dpyinfo->mouse_face_beg_y, Qnil);
23043
23044 dpyinfo->mouse_face_past_end
23045 = !fast_find_position (w, XFASTINT (after),
23046 &dpyinfo->mouse_face_end_col,
23047 &dpyinfo->mouse_face_end_row,
23048 &dpyinfo->mouse_face_end_x,
23049 &dpyinfo->mouse_face_end_y, Qnil);
23050 dpyinfo->mouse_face_window = window;
23051
23052 dpyinfo->mouse_face_face_id
23053 = face_at_buffer_position (w, pos, 0, 0,
23054 &ignore, pos + 1,
23055 !dpyinfo->mouse_face_hidden);
23056
23057 /* Display it as active. */
23058 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23059 cursor = No_Cursor;
23060 }
23061 /* Handle the text property case. */
23062 else if (!NILP (mouse_face) && BUFFERP (object))
23063 {
23064 /* Find the range of text around this char that
23065 should be active. */
23066 Lisp_Object before, after, beginning, end;
23067 int ignore;
23068
23069 beginning = Fmarker_position (w->start);
23070 end = make_number (BUF_Z (XBUFFER (object))
23071 - XFASTINT (w->window_end_pos));
23072 before
23073 = Fprevious_single_property_change (make_number (pos + 1),
23074 Qmouse_face,
23075 object, beginning);
23076 after
23077 = Fnext_single_property_change (position, Qmouse_face,
23078 object, end);
23079
23080 /* Record this as the current active region. */
23081 fast_find_position (w, XFASTINT (before),
23082 &dpyinfo->mouse_face_beg_col,
23083 &dpyinfo->mouse_face_beg_row,
23084 &dpyinfo->mouse_face_beg_x,
23085 &dpyinfo->mouse_face_beg_y, Qnil);
23086 dpyinfo->mouse_face_past_end
23087 = !fast_find_position (w, XFASTINT (after),
23088 &dpyinfo->mouse_face_end_col,
23089 &dpyinfo->mouse_face_end_row,
23090 &dpyinfo->mouse_face_end_x,
23091 &dpyinfo->mouse_face_end_y, Qnil);
23092 dpyinfo->mouse_face_window = window;
23093
23094 if (BUFFERP (object))
23095 dpyinfo->mouse_face_face_id
23096 = face_at_buffer_position (w, pos, 0, 0,
23097 &ignore, pos + 1,
23098 !dpyinfo->mouse_face_hidden);
23099
23100 /* Display it as active. */
23101 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23102 cursor = No_Cursor;
23103 }
23104 else if (!NILP (mouse_face) && STRINGP (object))
23105 {
23106 Lisp_Object b, e;
23107 int ignore;
23108
23109 b = Fprevious_single_property_change (make_number (pos + 1),
23110 Qmouse_face,
23111 object, Qnil);
23112 e = Fnext_single_property_change (position, Qmouse_face,
23113 object, Qnil);
23114 if (NILP (b))
23115 b = make_number (0);
23116 if (NILP (e))
23117 e = make_number (SCHARS (object) - 1);
23118
23119 fast_find_string_pos (w, XINT (b), object,
23120 &dpyinfo->mouse_face_beg_col,
23121 &dpyinfo->mouse_face_beg_row,
23122 &dpyinfo->mouse_face_beg_x,
23123 &dpyinfo->mouse_face_beg_y, 0);
23124 fast_find_string_pos (w, XINT (e), object,
23125 &dpyinfo->mouse_face_end_col,
23126 &dpyinfo->mouse_face_end_row,
23127 &dpyinfo->mouse_face_end_x,
23128 &dpyinfo->mouse_face_end_y, 1);
23129 dpyinfo->mouse_face_past_end = 0;
23130 dpyinfo->mouse_face_window = window;
23131 dpyinfo->mouse_face_face_id
23132 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23133 glyph->face_id, 1);
23134 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23135 cursor = No_Cursor;
23136 }
23137 else if (STRINGP (object) && NILP (mouse_face))
23138 {
23139 /* A string which doesn't have mouse-face, but
23140 the text ``under'' it might have. */
23141 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23142 int start = MATRIX_ROW_START_CHARPOS (r);
23143
23144 pos = string_buffer_position (w, object, start);
23145 if (pos > 0)
23146 mouse_face = get_char_property_and_overlay (make_number (pos),
23147 Qmouse_face,
23148 w->buffer,
23149 &overlay);
23150 if (!NILP (mouse_face) && !NILP (overlay))
23151 {
23152 Lisp_Object before = Foverlay_start (overlay);
23153 Lisp_Object after = Foverlay_end (overlay);
23154 int ignore;
23155
23156 /* Note that we might not be able to find position
23157 BEFORE in the glyph matrix if the overlay is
23158 entirely covered by a `display' property. In
23159 this case, we overshoot. So let's stop in
23160 the glyph matrix before glyphs for OBJECT. */
23161 fast_find_position (w, XFASTINT (before),
23162 &dpyinfo->mouse_face_beg_col,
23163 &dpyinfo->mouse_face_beg_row,
23164 &dpyinfo->mouse_face_beg_x,
23165 &dpyinfo->mouse_face_beg_y,
23166 object);
23167
23168 dpyinfo->mouse_face_past_end
23169 = !fast_find_position (w, XFASTINT (after),
23170 &dpyinfo->mouse_face_end_col,
23171 &dpyinfo->mouse_face_end_row,
23172 &dpyinfo->mouse_face_end_x,
23173 &dpyinfo->mouse_face_end_y,
23174 Qnil);
23175 dpyinfo->mouse_face_window = window;
23176 dpyinfo->mouse_face_face_id
23177 = face_at_buffer_position (w, pos, 0, 0,
23178 &ignore, pos + 1,
23179 !dpyinfo->mouse_face_hidden);
23180
23181 /* Display it as active. */
23182 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23183 cursor = No_Cursor;
23184 }
23185 }
23186 }
23187
23188 check_help_echo:
23189
23190 /* Look for a `help-echo' property. */
23191 if (NILP (help_echo_string)) {
23192 Lisp_Object help, overlay;
23193
23194 /* Check overlays first. */
23195 help = overlay = Qnil;
23196 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23197 {
23198 overlay = overlay_vec[i];
23199 help = Foverlay_get (overlay, Qhelp_echo);
23200 }
23201
23202 if (!NILP (help))
23203 {
23204 help_echo_string = help;
23205 help_echo_window = window;
23206 help_echo_object = overlay;
23207 help_echo_pos = pos;
23208 }
23209 else
23210 {
23211 Lisp_Object object = glyph->object;
23212 int charpos = glyph->charpos;
23213
23214 /* Try text properties. */
23215 if (STRINGP (object)
23216 && charpos >= 0
23217 && charpos < SCHARS (object))
23218 {
23219 help = Fget_text_property (make_number (charpos),
23220 Qhelp_echo, object);
23221 if (NILP (help))
23222 {
23223 /* If the string itself doesn't specify a help-echo,
23224 see if the buffer text ``under'' it does. */
23225 struct glyph_row *r
23226 = MATRIX_ROW (w->current_matrix, vpos);
23227 int start = MATRIX_ROW_START_CHARPOS (r);
23228 int pos = string_buffer_position (w, object, start);
23229 if (pos > 0)
23230 {
23231 help = Fget_char_property (make_number (pos),
23232 Qhelp_echo, w->buffer);
23233 if (!NILP (help))
23234 {
23235 charpos = pos;
23236 object = w->buffer;
23237 }
23238 }
23239 }
23240 }
23241 else if (BUFFERP (object)
23242 && charpos >= BEGV
23243 && charpos < ZV)
23244 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23245 object);
23246
23247 if (!NILP (help))
23248 {
23249 help_echo_string = help;
23250 help_echo_window = window;
23251 help_echo_object = object;
23252 help_echo_pos = charpos;
23253 }
23254 }
23255 }
23256
23257 /* Look for a `pointer' property. */
23258 if (NILP (pointer))
23259 {
23260 /* Check overlays first. */
23261 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23262 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23263
23264 if (NILP (pointer))
23265 {
23266 Lisp_Object object = glyph->object;
23267 int charpos = glyph->charpos;
23268
23269 /* Try text properties. */
23270 if (STRINGP (object)
23271 && charpos >= 0
23272 && charpos < SCHARS (object))
23273 {
23274 pointer = Fget_text_property (make_number (charpos),
23275 Qpointer, object);
23276 if (NILP (pointer))
23277 {
23278 /* If the string itself doesn't specify a pointer,
23279 see if the buffer text ``under'' it does. */
23280 struct glyph_row *r
23281 = MATRIX_ROW (w->current_matrix, vpos);
23282 int start = MATRIX_ROW_START_CHARPOS (r);
23283 int pos = string_buffer_position (w, object, start);
23284 if (pos > 0)
23285 pointer = Fget_char_property (make_number (pos),
23286 Qpointer, w->buffer);
23287 }
23288 }
23289 else if (BUFFERP (object)
23290 && charpos >= BEGV
23291 && charpos < ZV)
23292 pointer = Fget_text_property (make_number (charpos),
23293 Qpointer, object);
23294 }
23295 }
23296
23297 BEGV = obegv;
23298 ZV = ozv;
23299 current_buffer = obuf;
23300 }
23301
23302 set_cursor:
23303
23304 define_frame_cursor1 (f, cursor, pointer);
23305 }
23306
23307
23308 /* EXPORT for RIF:
23309 Clear any mouse-face on window W. This function is part of the
23310 redisplay interface, and is called from try_window_id and similar
23311 functions to ensure the mouse-highlight is off. */
23312
23313 void
23314 x_clear_window_mouse_face (w)
23315 struct window *w;
23316 {
23317 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23318 Lisp_Object window;
23319
23320 BLOCK_INPUT;
23321 XSETWINDOW (window, w);
23322 if (EQ (window, dpyinfo->mouse_face_window))
23323 clear_mouse_face (dpyinfo);
23324 UNBLOCK_INPUT;
23325 }
23326
23327
23328 /* EXPORT:
23329 Just discard the mouse face information for frame F, if any.
23330 This is used when the size of F is changed. */
23331
23332 void
23333 cancel_mouse_face (f)
23334 struct frame *f;
23335 {
23336 Lisp_Object window;
23337 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23338
23339 window = dpyinfo->mouse_face_window;
23340 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23341 {
23342 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23343 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23344 dpyinfo->mouse_face_window = Qnil;
23345 }
23346 }
23347
23348
23349 #endif /* HAVE_WINDOW_SYSTEM */
23350
23351 \f
23352 /***********************************************************************
23353 Exposure Events
23354 ***********************************************************************/
23355
23356 #ifdef HAVE_WINDOW_SYSTEM
23357
23358 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23359 which intersects rectangle R. R is in window-relative coordinates. */
23360
23361 static void
23362 expose_area (w, row, r, area)
23363 struct window *w;
23364 struct glyph_row *row;
23365 XRectangle *r;
23366 enum glyph_row_area area;
23367 {
23368 struct glyph *first = row->glyphs[area];
23369 struct glyph *end = row->glyphs[area] + row->used[area];
23370 struct glyph *last;
23371 int first_x, start_x, x;
23372
23373 if (area == TEXT_AREA && row->fill_line_p)
23374 /* If row extends face to end of line write the whole line. */
23375 draw_glyphs (w, 0, row, area,
23376 0, row->used[area],
23377 DRAW_NORMAL_TEXT, 0);
23378 else
23379 {
23380 /* Set START_X to the window-relative start position for drawing glyphs of
23381 AREA. The first glyph of the text area can be partially visible.
23382 The first glyphs of other areas cannot. */
23383 start_x = window_box_left_offset (w, area);
23384 x = start_x;
23385 if (area == TEXT_AREA)
23386 x += row->x;
23387
23388 /* Find the first glyph that must be redrawn. */
23389 while (first < end
23390 && x + first->pixel_width < r->x)
23391 {
23392 x += first->pixel_width;
23393 ++first;
23394 }
23395
23396 /* Find the last one. */
23397 last = first;
23398 first_x = x;
23399 while (last < end
23400 && x < r->x + r->width)
23401 {
23402 x += last->pixel_width;
23403 ++last;
23404 }
23405
23406 /* Repaint. */
23407 if (last > first)
23408 draw_glyphs (w, first_x - start_x, row, area,
23409 first - row->glyphs[area], last - row->glyphs[area],
23410 DRAW_NORMAL_TEXT, 0);
23411 }
23412 }
23413
23414
23415 /* Redraw the parts of the glyph row ROW on window W intersecting
23416 rectangle R. R is in window-relative coordinates. Value is
23417 non-zero if mouse-face was overwritten. */
23418
23419 static int
23420 expose_line (w, row, r)
23421 struct window *w;
23422 struct glyph_row *row;
23423 XRectangle *r;
23424 {
23425 xassert (row->enabled_p);
23426
23427 if (row->mode_line_p || w->pseudo_window_p)
23428 draw_glyphs (w, 0, row, TEXT_AREA,
23429 0, row->used[TEXT_AREA],
23430 DRAW_NORMAL_TEXT, 0);
23431 else
23432 {
23433 if (row->used[LEFT_MARGIN_AREA])
23434 expose_area (w, row, r, LEFT_MARGIN_AREA);
23435 if (row->used[TEXT_AREA])
23436 expose_area (w, row, r, TEXT_AREA);
23437 if (row->used[RIGHT_MARGIN_AREA])
23438 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23439 draw_row_fringe_bitmaps (w, row);
23440 }
23441
23442 return row->mouse_face_p;
23443 }
23444
23445
23446 /* Redraw those parts of glyphs rows during expose event handling that
23447 overlap other rows. Redrawing of an exposed line writes over parts
23448 of lines overlapping that exposed line; this function fixes that.
23449
23450 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23451 row in W's current matrix that is exposed and overlaps other rows.
23452 LAST_OVERLAPPING_ROW is the last such row. */
23453
23454 static void
23455 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23456 struct window *w;
23457 struct glyph_row *first_overlapping_row;
23458 struct glyph_row *last_overlapping_row;
23459 {
23460 struct glyph_row *row;
23461
23462 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23463 if (row->overlapping_p)
23464 {
23465 xassert (row->enabled_p && !row->mode_line_p);
23466
23467 if (row->used[LEFT_MARGIN_AREA])
23468 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23469
23470 if (row->used[TEXT_AREA])
23471 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23472
23473 if (row->used[RIGHT_MARGIN_AREA])
23474 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23475 }
23476 }
23477
23478
23479 /* Return non-zero if W's cursor intersects rectangle R. */
23480
23481 static int
23482 phys_cursor_in_rect_p (w, r)
23483 struct window *w;
23484 XRectangle *r;
23485 {
23486 XRectangle cr, result;
23487 struct glyph *cursor_glyph;
23488 struct glyph_row *row;
23489
23490 if (w->phys_cursor.vpos >= 0
23491 && w->phys_cursor.vpos < w->current_matrix->nrows
23492 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
23493 row->enabled_p)
23494 && row->cursor_in_fringe_p)
23495 {
23496 /* Cursor is in the fringe. */
23497 cr.x = window_box_right_offset (w,
23498 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
23499 ? RIGHT_MARGIN_AREA
23500 : TEXT_AREA));
23501 cr.y = row->y;
23502 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
23503 cr.height = row->height;
23504 return x_intersect_rectangles (&cr, r, &result);
23505 }
23506
23507 cursor_glyph = get_phys_cursor_glyph (w);
23508 if (cursor_glyph)
23509 {
23510 /* r is relative to W's box, but w->phys_cursor.x is relative
23511 to left edge of W's TEXT area. Adjust it. */
23512 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23513 cr.y = w->phys_cursor.y;
23514 cr.width = cursor_glyph->pixel_width;
23515 cr.height = w->phys_cursor_height;
23516 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23517 I assume the effect is the same -- and this is portable. */
23518 return x_intersect_rectangles (&cr, r, &result);
23519 }
23520 else
23521 return 0;
23522 }
23523
23524
23525 /* EXPORT:
23526 Draw a vertical window border to the right of window W if W doesn't
23527 have vertical scroll bars. */
23528
23529 void
23530 x_draw_vertical_border (w)
23531 struct window *w;
23532 {
23533 /* We could do better, if we knew what type of scroll-bar the adjacent
23534 windows (on either side) have... But we don't :-(
23535 However, I think this works ok. ++KFS 2003-04-25 */
23536
23537 /* Redraw borders between horizontally adjacent windows. Don't
23538 do it for frames with vertical scroll bars because either the
23539 right scroll bar of a window, or the left scroll bar of its
23540 neighbor will suffice as a border. */
23541 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23542 return;
23543
23544 if (!WINDOW_RIGHTMOST_P (w)
23545 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23546 {
23547 int x0, x1, y0, y1;
23548
23549 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23550 y1 -= 1;
23551
23552 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23553 x1 -= 1;
23554
23555 rif->draw_vertical_window_border (w, x1, y0, y1);
23556 }
23557 else if (!WINDOW_LEFTMOST_P (w)
23558 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23559 {
23560 int x0, x1, y0, y1;
23561
23562 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23563 y1 -= 1;
23564
23565 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23566 x0 -= 1;
23567
23568 rif->draw_vertical_window_border (w, x0, y0, y1);
23569 }
23570 }
23571
23572
23573 /* Redraw the part of window W intersection rectangle FR. Pixel
23574 coordinates in FR are frame-relative. Call this function with
23575 input blocked. Value is non-zero if the exposure overwrites
23576 mouse-face. */
23577
23578 static int
23579 expose_window (w, fr)
23580 struct window *w;
23581 XRectangle *fr;
23582 {
23583 struct frame *f = XFRAME (w->frame);
23584 XRectangle wr, r;
23585 int mouse_face_overwritten_p = 0;
23586
23587 /* If window is not yet fully initialized, do nothing. This can
23588 happen when toolkit scroll bars are used and a window is split.
23589 Reconfiguring the scroll bar will generate an expose for a newly
23590 created window. */
23591 if (w->current_matrix == NULL)
23592 return 0;
23593
23594 /* When we're currently updating the window, display and current
23595 matrix usually don't agree. Arrange for a thorough display
23596 later. */
23597 if (w == updated_window)
23598 {
23599 SET_FRAME_GARBAGED (f);
23600 return 0;
23601 }
23602
23603 /* Frame-relative pixel rectangle of W. */
23604 wr.x = WINDOW_LEFT_EDGE_X (w);
23605 wr.y = WINDOW_TOP_EDGE_Y (w);
23606 wr.width = WINDOW_TOTAL_WIDTH (w);
23607 wr.height = WINDOW_TOTAL_HEIGHT (w);
23608
23609 if (x_intersect_rectangles (fr, &wr, &r))
23610 {
23611 int yb = window_text_bottom_y (w);
23612 struct glyph_row *row;
23613 int cursor_cleared_p;
23614 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23615
23616 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23617 r.x, r.y, r.width, r.height));
23618
23619 /* Convert to window coordinates. */
23620 r.x -= WINDOW_LEFT_EDGE_X (w);
23621 r.y -= WINDOW_TOP_EDGE_Y (w);
23622
23623 /* Turn off the cursor. */
23624 if (!w->pseudo_window_p
23625 && phys_cursor_in_rect_p (w, &r))
23626 {
23627 x_clear_cursor (w);
23628 cursor_cleared_p = 1;
23629 }
23630 else
23631 cursor_cleared_p = 0;
23632
23633 /* Update lines intersecting rectangle R. */
23634 first_overlapping_row = last_overlapping_row = NULL;
23635 for (row = w->current_matrix->rows;
23636 row->enabled_p;
23637 ++row)
23638 {
23639 int y0 = row->y;
23640 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23641
23642 if ((y0 >= r.y && y0 < r.y + r.height)
23643 || (y1 > r.y && y1 < r.y + r.height)
23644 || (r.y >= y0 && r.y < y1)
23645 || (r.y + r.height > y0 && r.y + r.height < y1))
23646 {
23647 /* A header line may be overlapping, but there is no need
23648 to fix overlapping areas for them. KFS 2005-02-12 */
23649 if (row->overlapping_p && !row->mode_line_p)
23650 {
23651 if (first_overlapping_row == NULL)
23652 first_overlapping_row = row;
23653 last_overlapping_row = row;
23654 }
23655
23656 if (expose_line (w, row, &r))
23657 mouse_face_overwritten_p = 1;
23658 }
23659
23660 if (y1 >= yb)
23661 break;
23662 }
23663
23664 /* Display the mode line if there is one. */
23665 if (WINDOW_WANTS_MODELINE_P (w)
23666 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23667 row->enabled_p)
23668 && row->y < r.y + r.height)
23669 {
23670 if (expose_line (w, row, &r))
23671 mouse_face_overwritten_p = 1;
23672 }
23673
23674 if (!w->pseudo_window_p)
23675 {
23676 /* Fix the display of overlapping rows. */
23677 if (first_overlapping_row)
23678 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23679
23680 /* Draw border between windows. */
23681 x_draw_vertical_border (w);
23682
23683 /* Turn the cursor on again. */
23684 if (cursor_cleared_p)
23685 update_window_cursor (w, 1);
23686 }
23687 }
23688
23689 return mouse_face_overwritten_p;
23690 }
23691
23692
23693
23694 /* Redraw (parts) of all windows in the window tree rooted at W that
23695 intersect R. R contains frame pixel coordinates. Value is
23696 non-zero if the exposure overwrites mouse-face. */
23697
23698 static int
23699 expose_window_tree (w, r)
23700 struct window *w;
23701 XRectangle *r;
23702 {
23703 struct frame *f = XFRAME (w->frame);
23704 int mouse_face_overwritten_p = 0;
23705
23706 while (w && !FRAME_GARBAGED_P (f))
23707 {
23708 if (!NILP (w->hchild))
23709 mouse_face_overwritten_p
23710 |= expose_window_tree (XWINDOW (w->hchild), r);
23711 else if (!NILP (w->vchild))
23712 mouse_face_overwritten_p
23713 |= expose_window_tree (XWINDOW (w->vchild), r);
23714 else
23715 mouse_face_overwritten_p |= expose_window (w, r);
23716
23717 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23718 }
23719
23720 return mouse_face_overwritten_p;
23721 }
23722
23723
23724 /* EXPORT:
23725 Redisplay an exposed area of frame F. X and Y are the upper-left
23726 corner of the exposed rectangle. W and H are width and height of
23727 the exposed area. All are pixel values. W or H zero means redraw
23728 the entire frame. */
23729
23730 void
23731 expose_frame (f, x, y, w, h)
23732 struct frame *f;
23733 int x, y, w, h;
23734 {
23735 XRectangle r;
23736 int mouse_face_overwritten_p = 0;
23737
23738 TRACE ((stderr, "expose_frame "));
23739
23740 /* No need to redraw if frame will be redrawn soon. */
23741 if (FRAME_GARBAGED_P (f))
23742 {
23743 TRACE ((stderr, " garbaged\n"));
23744 return;
23745 }
23746
23747 /* If basic faces haven't been realized yet, there is no point in
23748 trying to redraw anything. This can happen when we get an expose
23749 event while Emacs is starting, e.g. by moving another window. */
23750 if (FRAME_FACE_CACHE (f) == NULL
23751 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23752 {
23753 TRACE ((stderr, " no faces\n"));
23754 return;
23755 }
23756
23757 if (w == 0 || h == 0)
23758 {
23759 r.x = r.y = 0;
23760 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23761 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23762 }
23763 else
23764 {
23765 r.x = x;
23766 r.y = y;
23767 r.width = w;
23768 r.height = h;
23769 }
23770
23771 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23772 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23773
23774 if (WINDOWP (f->tool_bar_window))
23775 mouse_face_overwritten_p
23776 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23777
23778 #ifdef HAVE_X_WINDOWS
23779 #ifndef MSDOS
23780 #ifndef USE_X_TOOLKIT
23781 if (WINDOWP (f->menu_bar_window))
23782 mouse_face_overwritten_p
23783 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23784 #endif /* not USE_X_TOOLKIT */
23785 #endif
23786 #endif
23787
23788 /* Some window managers support a focus-follows-mouse style with
23789 delayed raising of frames. Imagine a partially obscured frame,
23790 and moving the mouse into partially obscured mouse-face on that
23791 frame. The visible part of the mouse-face will be highlighted,
23792 then the WM raises the obscured frame. With at least one WM, KDE
23793 2.1, Emacs is not getting any event for the raising of the frame
23794 (even tried with SubstructureRedirectMask), only Expose events.
23795 These expose events will draw text normally, i.e. not
23796 highlighted. Which means we must redo the highlight here.
23797 Subsume it under ``we love X''. --gerd 2001-08-15 */
23798 /* Included in Windows version because Windows most likely does not
23799 do the right thing if any third party tool offers
23800 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23801 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23802 {
23803 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23804 if (f == dpyinfo->mouse_face_mouse_frame)
23805 {
23806 int x = dpyinfo->mouse_face_mouse_x;
23807 int y = dpyinfo->mouse_face_mouse_y;
23808 clear_mouse_face (dpyinfo);
23809 note_mouse_highlight (f, x, y);
23810 }
23811 }
23812 }
23813
23814
23815 /* EXPORT:
23816 Determine the intersection of two rectangles R1 and R2. Return
23817 the intersection in *RESULT. Value is non-zero if RESULT is not
23818 empty. */
23819
23820 int
23821 x_intersect_rectangles (r1, r2, result)
23822 XRectangle *r1, *r2, *result;
23823 {
23824 XRectangle *left, *right;
23825 XRectangle *upper, *lower;
23826 int intersection_p = 0;
23827
23828 /* Rearrange so that R1 is the left-most rectangle. */
23829 if (r1->x < r2->x)
23830 left = r1, right = r2;
23831 else
23832 left = r2, right = r1;
23833
23834 /* X0 of the intersection is right.x0, if this is inside R1,
23835 otherwise there is no intersection. */
23836 if (right->x <= left->x + left->width)
23837 {
23838 result->x = right->x;
23839
23840 /* The right end of the intersection is the minimum of the
23841 the right ends of left and right. */
23842 result->width = (min (left->x + left->width, right->x + right->width)
23843 - result->x);
23844
23845 /* Same game for Y. */
23846 if (r1->y < r2->y)
23847 upper = r1, lower = r2;
23848 else
23849 upper = r2, lower = r1;
23850
23851 /* The upper end of the intersection is lower.y0, if this is inside
23852 of upper. Otherwise, there is no intersection. */
23853 if (lower->y <= upper->y + upper->height)
23854 {
23855 result->y = lower->y;
23856
23857 /* The lower end of the intersection is the minimum of the lower
23858 ends of upper and lower. */
23859 result->height = (min (lower->y + lower->height,
23860 upper->y + upper->height)
23861 - result->y);
23862 intersection_p = 1;
23863 }
23864 }
23865
23866 return intersection_p;
23867 }
23868
23869 #endif /* HAVE_WINDOW_SYSTEM */
23870
23871 \f
23872 /***********************************************************************
23873 Initialization
23874 ***********************************************************************/
23875
23876 void
23877 syms_of_xdisp ()
23878 {
23879 Vwith_echo_area_save_vector = Qnil;
23880 staticpro (&Vwith_echo_area_save_vector);
23881
23882 Vmessage_stack = Qnil;
23883 staticpro (&Vmessage_stack);
23884
23885 Qinhibit_redisplay = intern ("inhibit-redisplay");
23886 staticpro (&Qinhibit_redisplay);
23887
23888 message_dolog_marker1 = Fmake_marker ();
23889 staticpro (&message_dolog_marker1);
23890 message_dolog_marker2 = Fmake_marker ();
23891 staticpro (&message_dolog_marker2);
23892 message_dolog_marker3 = Fmake_marker ();
23893 staticpro (&message_dolog_marker3);
23894
23895 #if GLYPH_DEBUG
23896 defsubr (&Sdump_frame_glyph_matrix);
23897 defsubr (&Sdump_glyph_matrix);
23898 defsubr (&Sdump_glyph_row);
23899 defsubr (&Sdump_tool_bar_row);
23900 defsubr (&Strace_redisplay);
23901 defsubr (&Strace_to_stderr);
23902 #endif
23903 #ifdef HAVE_WINDOW_SYSTEM
23904 defsubr (&Stool_bar_lines_needed);
23905 defsubr (&Slookup_image_map);
23906 #endif
23907 defsubr (&Sformat_mode_line);
23908
23909 staticpro (&Qmenu_bar_update_hook);
23910 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23911
23912 staticpro (&Qoverriding_terminal_local_map);
23913 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23914
23915 staticpro (&Qoverriding_local_map);
23916 Qoverriding_local_map = intern ("overriding-local-map");
23917
23918 staticpro (&Qwindow_scroll_functions);
23919 Qwindow_scroll_functions = intern ("window-scroll-functions");
23920
23921 staticpro (&Qredisplay_end_trigger_functions);
23922 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23923
23924 staticpro (&Qinhibit_point_motion_hooks);
23925 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23926
23927 QCdata = intern (":data");
23928 staticpro (&QCdata);
23929 Qdisplay = intern ("display");
23930 staticpro (&Qdisplay);
23931 Qspace_width = intern ("space-width");
23932 staticpro (&Qspace_width);
23933 Qraise = intern ("raise");
23934 staticpro (&Qraise);
23935 Qslice = intern ("slice");
23936 staticpro (&Qslice);
23937 Qspace = intern ("space");
23938 staticpro (&Qspace);
23939 Qmargin = intern ("margin");
23940 staticpro (&Qmargin);
23941 Qpointer = intern ("pointer");
23942 staticpro (&Qpointer);
23943 Qleft_margin = intern ("left-margin");
23944 staticpro (&Qleft_margin);
23945 Qright_margin = intern ("right-margin");
23946 staticpro (&Qright_margin);
23947 Qcenter = intern ("center");
23948 staticpro (&Qcenter);
23949 Qline_height = intern ("line-height");
23950 staticpro (&Qline_height);
23951 QCalign_to = intern (":align-to");
23952 staticpro (&QCalign_to);
23953 QCrelative_width = intern (":relative-width");
23954 staticpro (&QCrelative_width);
23955 QCrelative_height = intern (":relative-height");
23956 staticpro (&QCrelative_height);
23957 QCeval = intern (":eval");
23958 staticpro (&QCeval);
23959 QCpropertize = intern (":propertize");
23960 staticpro (&QCpropertize);
23961 QCfile = intern (":file");
23962 staticpro (&QCfile);
23963 Qfontified = intern ("fontified");
23964 staticpro (&Qfontified);
23965 Qfontification_functions = intern ("fontification-functions");
23966 staticpro (&Qfontification_functions);
23967 Qtrailing_whitespace = intern ("trailing-whitespace");
23968 staticpro (&Qtrailing_whitespace);
23969 Qescape_glyph = intern ("escape-glyph");
23970 staticpro (&Qescape_glyph);
23971 Qnobreak_space = intern ("nobreak-space");
23972 staticpro (&Qnobreak_space);
23973 Qimage = intern ("image");
23974 staticpro (&Qimage);
23975 QCmap = intern (":map");
23976 staticpro (&QCmap);
23977 QCpointer = intern (":pointer");
23978 staticpro (&QCpointer);
23979 Qrect = intern ("rect");
23980 staticpro (&Qrect);
23981 Qcircle = intern ("circle");
23982 staticpro (&Qcircle);
23983 Qpoly = intern ("poly");
23984 staticpro (&Qpoly);
23985 Qmessage_truncate_lines = intern ("message-truncate-lines");
23986 staticpro (&Qmessage_truncate_lines);
23987 Qgrow_only = intern ("grow-only");
23988 staticpro (&Qgrow_only);
23989 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23990 staticpro (&Qinhibit_menubar_update);
23991 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23992 staticpro (&Qinhibit_eval_during_redisplay);
23993 Qposition = intern ("position");
23994 staticpro (&Qposition);
23995 Qbuffer_position = intern ("buffer-position");
23996 staticpro (&Qbuffer_position);
23997 Qobject = intern ("object");
23998 staticpro (&Qobject);
23999 Qbar = intern ("bar");
24000 staticpro (&Qbar);
24001 Qhbar = intern ("hbar");
24002 staticpro (&Qhbar);
24003 Qbox = intern ("box");
24004 staticpro (&Qbox);
24005 Qhollow = intern ("hollow");
24006 staticpro (&Qhollow);
24007 Qhand = intern ("hand");
24008 staticpro (&Qhand);
24009 Qarrow = intern ("arrow");
24010 staticpro (&Qarrow);
24011 Qtext = intern ("text");
24012 staticpro (&Qtext);
24013 Qrisky_local_variable = intern ("risky-local-variable");
24014 staticpro (&Qrisky_local_variable);
24015 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24016 staticpro (&Qinhibit_free_realized_faces);
24017
24018 list_of_error = Fcons (Fcons (intern ("error"),
24019 Fcons (intern ("void-variable"), Qnil)),
24020 Qnil);
24021 staticpro (&list_of_error);
24022
24023 Qlast_arrow_position = intern ("last-arrow-position");
24024 staticpro (&Qlast_arrow_position);
24025 Qlast_arrow_string = intern ("last-arrow-string");
24026 staticpro (&Qlast_arrow_string);
24027
24028 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24029 staticpro (&Qoverlay_arrow_string);
24030 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24031 staticpro (&Qoverlay_arrow_bitmap);
24032
24033 echo_buffer[0] = echo_buffer[1] = Qnil;
24034 staticpro (&echo_buffer[0]);
24035 staticpro (&echo_buffer[1]);
24036
24037 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24038 staticpro (&echo_area_buffer[0]);
24039 staticpro (&echo_area_buffer[1]);
24040
24041 Vmessages_buffer_name = build_string ("*Messages*");
24042 staticpro (&Vmessages_buffer_name);
24043
24044 mode_line_proptrans_alist = Qnil;
24045 staticpro (&mode_line_proptrans_alist);
24046 mode_line_string_list = Qnil;
24047 staticpro (&mode_line_string_list);
24048 mode_line_string_face = Qnil;
24049 staticpro (&mode_line_string_face);
24050 mode_line_string_face_prop = Qnil;
24051 staticpro (&mode_line_string_face_prop);
24052 Vmode_line_unwind_vector = Qnil;
24053 staticpro (&Vmode_line_unwind_vector);
24054
24055 help_echo_string = Qnil;
24056 staticpro (&help_echo_string);
24057 help_echo_object = Qnil;
24058 staticpro (&help_echo_object);
24059 help_echo_window = Qnil;
24060 staticpro (&help_echo_window);
24061 previous_help_echo_string = Qnil;
24062 staticpro (&previous_help_echo_string);
24063 help_echo_pos = -1;
24064
24065 #ifdef HAVE_WINDOW_SYSTEM
24066 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24067 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24068 For example, if a block cursor is over a tab, it will be drawn as
24069 wide as that tab on the display. */);
24070 x_stretch_cursor_p = 0;
24071 #endif
24072
24073 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24074 doc: /* *Non-nil means highlight trailing whitespace.
24075 The face used for trailing whitespace is `trailing-whitespace'. */);
24076 Vshow_trailing_whitespace = Qnil;
24077
24078 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24079 doc: /* *Control highlighting of nobreak space and soft hyphen.
24080 A value of t means highlight the character itself (for nobreak space,
24081 use face `nobreak-space').
24082 A value of nil means no highlighting.
24083 Other values mean display the escape glyph followed by an ordinary
24084 space or ordinary hyphen. */);
24085 Vnobreak_char_display = Qt;
24086
24087 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24088 doc: /* *The pointer shape to show in void text areas.
24089 A value of nil means to show the text pointer. Other options are `arrow',
24090 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24091 Vvoid_text_area_pointer = Qarrow;
24092
24093 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24094 doc: /* Non-nil means don't actually do any redisplay.
24095 This is used for internal purposes. */);
24096 Vinhibit_redisplay = Qnil;
24097
24098 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24099 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24100 Vglobal_mode_string = Qnil;
24101
24102 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24103 doc: /* Marker for where to display an arrow on top of the buffer text.
24104 This must be the beginning of a line in order to work.
24105 See also `overlay-arrow-string'. */);
24106 Voverlay_arrow_position = Qnil;
24107
24108 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24109 doc: /* String to display as an arrow in non-window frames.
24110 See also `overlay-arrow-position'. */);
24111 Voverlay_arrow_string = build_string ("=>");
24112
24113 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24114 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24115 The symbols on this list are examined during redisplay to determine
24116 where to display overlay arrows. */);
24117 Voverlay_arrow_variable_list
24118 = Fcons (intern ("overlay-arrow-position"), Qnil);
24119
24120 DEFVAR_INT ("scroll-step", &scroll_step,
24121 doc: /* *The number of lines to try scrolling a window by when point moves out.
24122 If that fails to bring point back on frame, point is centered instead.
24123 If this is zero, point is always centered after it moves off frame.
24124 If you want scrolling to always be a line at a time, you should set
24125 `scroll-conservatively' to a large value rather than set this to 1. */);
24126
24127 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24128 doc: /* *Scroll up to this many lines, to bring point back on screen.
24129 If point moves off-screen, redisplay will scroll by up to
24130 `scroll-conservatively' lines in order to bring point just barely
24131 onto the screen again. If that cannot be done, then redisplay
24132 recenters point as usual.
24133
24134 A value of zero means always recenter point if it moves off screen. */);
24135 scroll_conservatively = 0;
24136
24137 DEFVAR_INT ("scroll-margin", &scroll_margin,
24138 doc: /* *Number of lines of margin at the top and bottom of a window.
24139 Recenter the window whenever point gets within this many lines
24140 of the top or bottom of the window. */);
24141 scroll_margin = 0;
24142
24143 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24144 doc: /* Pixels per inch value for non-window system displays.
24145 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24146 Vdisplay_pixels_per_inch = make_float (72.0);
24147
24148 #if GLYPH_DEBUG
24149 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24150 #endif
24151
24152 DEFVAR_BOOL ("truncate-partial-width-windows",
24153 &truncate_partial_width_windows,
24154 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24155 truncate_partial_width_windows = 1;
24156
24157 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24158 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24159 Any other value means to use the appropriate face, `mode-line',
24160 `header-line', or `menu' respectively. */);
24161 mode_line_inverse_video = 1;
24162
24163 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24164 doc: /* *Maximum buffer size for which line number should be displayed.
24165 If the buffer is bigger than this, the line number does not appear
24166 in the mode line. A value of nil means no limit. */);
24167 Vline_number_display_limit = Qnil;
24168
24169 DEFVAR_INT ("line-number-display-limit-width",
24170 &line_number_display_limit_width,
24171 doc: /* *Maximum line width (in characters) for line number display.
24172 If the average length of the lines near point is bigger than this, then the
24173 line number may be omitted from the mode line. */);
24174 line_number_display_limit_width = 200;
24175
24176 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24177 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24178 highlight_nonselected_windows = 0;
24179
24180 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24181 doc: /* Non-nil if more than one frame is visible on this display.
24182 Minibuffer-only frames don't count, but iconified frames do.
24183 This variable is not guaranteed to be accurate except while processing
24184 `frame-title-format' and `icon-title-format'. */);
24185
24186 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24187 doc: /* Template for displaying the title bar of visible frames.
24188 \(Assuming the window manager supports this feature.)
24189
24190 This variable has the same structure as `mode-line-format', except that
24191 the %c and %l constructs are ignored. It is used only on frames for
24192 which no explicit name has been set \(see `modify-frame-parameters'). */);
24193
24194 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24195 doc: /* Template for displaying the title bar of an iconified frame.
24196 \(Assuming the window manager supports this feature.)
24197 This variable has the same structure as `mode-line-format' (which see),
24198 and is used only on frames for which no explicit name has been set
24199 \(see `modify-frame-parameters'). */);
24200 Vicon_title_format
24201 = Vframe_title_format
24202 = Fcons (intern ("multiple-frames"),
24203 Fcons (build_string ("%b"),
24204 Fcons (Fcons (empty_string,
24205 Fcons (intern ("invocation-name"),
24206 Fcons (build_string ("@"),
24207 Fcons (intern ("system-name"),
24208 Qnil)))),
24209 Qnil)));
24210
24211 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24212 doc: /* Maximum number of lines to keep in the message log buffer.
24213 If nil, disable message logging. If t, log messages but don't truncate
24214 the buffer when it becomes large. */);
24215 Vmessage_log_max = make_number (100);
24216
24217 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24218 doc: /* Functions called before redisplay, if window sizes have changed.
24219 The value should be a list of functions that take one argument.
24220 Just before redisplay, for each frame, if any of its windows have changed
24221 size since the last redisplay, or have been split or deleted,
24222 all the functions in the list are called, with the frame as argument. */);
24223 Vwindow_size_change_functions = Qnil;
24224
24225 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24226 doc: /* List of functions to call before redisplaying a window with scrolling.
24227 Each function is called with two arguments, the window
24228 and its new display-start position. Note that the value of `window-end'
24229 is not valid when these functions are called. */);
24230 Vwindow_scroll_functions = Qnil;
24231
24232 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24233 doc: /* Functions called when redisplay of a window reaches the end trigger.
24234 Each function is called with two arguments, the window and the end trigger value.
24235 See `set-window-redisplay-end-trigger'. */);
24236 Vredisplay_end_trigger_functions = Qnil;
24237
24238 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24239 doc: /* *Non-nil means autoselect window with mouse pointer.
24240 If nil, do not autoselect windows.
24241 A positive number means delay autoselection by that many seconds: a
24242 window is autoselected only after the mouse has remained in that
24243 window for the duration of the delay.
24244 A negative number has a similar effect, but causes windows to be
24245 autoselected only after the mouse has stopped moving. \(Because of
24246 the way Emacs compares mouse events, you will occasionally wait twice
24247 that time before the window gets selected.\)
24248 Any other value means to autoselect window instantaneously when the
24249 mouse pointer enters it.
24250
24251 Autoselection selects the minibuffer only if it is active, and never
24252 unselects the minibuffer if it is active.
24253
24254 When customizing this variable make sure that the actual value of
24255 `focus-follows-mouse' matches the behavior of your window manager. */);
24256 Vmouse_autoselect_window = Qnil;
24257
24258 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24259 doc: /* *Non-nil means automatically resize tool-bars.
24260 This dynamically changes the tool-bar's height to the minimum height
24261 that is needed to make all tool-bar items visible.
24262 If value is `grow-only', the tool-bar's height is only increased
24263 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24264 Vauto_resize_tool_bars = Qt;
24265
24266 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24267 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24268 auto_raise_tool_bar_buttons_p = 1;
24269
24270 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24271 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24272 make_cursor_line_fully_visible_p = 1;
24273
24274 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24275 doc: /* *Border below tool-bar in pixels.
24276 If an integer, use it as the height of the border.
24277 If it is one of `internal-border-width' or `border-width', use the
24278 value of the corresponding frame parameter.
24279 Otherwise, no border is added below the tool-bar. */);
24280 Vtool_bar_border = Qinternal_border_width;
24281
24282 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24283 doc: /* *Margin around tool-bar buttons in pixels.
24284 If an integer, use that for both horizontal and vertical margins.
24285 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24286 HORZ specifying the horizontal margin, and VERT specifying the
24287 vertical margin. */);
24288 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24289
24290 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24291 doc: /* *Relief thickness of tool-bar buttons. */);
24292 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24293
24294 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24295 doc: /* List of functions to call to fontify regions of text.
24296 Each function is called with one argument POS. Functions must
24297 fontify a region starting at POS in the current buffer, and give
24298 fontified regions the property `fontified'. */);
24299 Vfontification_functions = Qnil;
24300 Fmake_variable_buffer_local (Qfontification_functions);
24301
24302 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24303 &unibyte_display_via_language_environment,
24304 doc: /* *Non-nil means display unibyte text according to language environment.
24305 Specifically this means that unibyte non-ASCII characters
24306 are displayed by converting them to the equivalent multibyte characters
24307 according to the current language environment. As a result, they are
24308 displayed according to the current fontset. */);
24309 unibyte_display_via_language_environment = 0;
24310
24311 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24312 doc: /* *Maximum height for resizing mini-windows.
24313 If a float, it specifies a fraction of the mini-window frame's height.
24314 If an integer, it specifies a number of lines. */);
24315 Vmax_mini_window_height = make_float (0.25);
24316
24317 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24318 doc: /* *How to resize mini-windows.
24319 A value of nil means don't automatically resize mini-windows.
24320 A value of t means resize them to fit the text displayed in them.
24321 A value of `grow-only', the default, means let mini-windows grow
24322 only, until their display becomes empty, at which point the windows
24323 go back to their normal size. */);
24324 Vresize_mini_windows = Qgrow_only;
24325
24326 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24327 doc: /* Alist specifying how to blink the cursor off.
24328 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24329 `cursor-type' frame-parameter or variable equals ON-STATE,
24330 comparing using `equal', Emacs uses OFF-STATE to specify
24331 how to blink it off. ON-STATE and OFF-STATE are values for
24332 the `cursor-type' frame parameter.
24333
24334 If a frame's ON-STATE has no entry in this list,
24335 the frame's other specifications determine how to blink the cursor off. */);
24336 Vblink_cursor_alist = Qnil;
24337
24338 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24339 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24340 automatic_hscrolling_p = 1;
24341
24342 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24343 doc: /* *How many columns away from the window edge point is allowed to get
24344 before automatic hscrolling will horizontally scroll the window. */);
24345 hscroll_margin = 5;
24346
24347 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24348 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24349 When point is less than `hscroll-margin' columns from the window
24350 edge, automatic hscrolling will scroll the window by the amount of columns
24351 determined by this variable. If its value is a positive integer, scroll that
24352 many columns. If it's a positive floating-point number, it specifies the
24353 fraction of the window's width to scroll. If it's nil or zero, point will be
24354 centered horizontally after the scroll. Any other value, including negative
24355 numbers, are treated as if the value were zero.
24356
24357 Automatic hscrolling always moves point outside the scroll margin, so if
24358 point was more than scroll step columns inside the margin, the window will
24359 scroll more than the value given by the scroll step.
24360
24361 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24362 and `scroll-right' overrides this variable's effect. */);
24363 Vhscroll_step = make_number (0);
24364
24365 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24366 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24367 Bind this around calls to `message' to let it take effect. */);
24368 message_truncate_lines = 0;
24369
24370 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24371 doc: /* Normal hook run to update the menu bar definitions.
24372 Redisplay runs this hook before it redisplays the menu bar.
24373 This is used to update submenus such as Buffers,
24374 whose contents depend on various data. */);
24375 Vmenu_bar_update_hook = Qnil;
24376
24377 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24378 doc: /* Frame for which we are updating a menu.
24379 The enable predicate for a menu binding should check this variable. */);
24380 Vmenu_updating_frame = Qnil;
24381
24382 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24383 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24384 inhibit_menubar_update = 0;
24385
24386 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24387 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24388 inhibit_eval_during_redisplay = 0;
24389
24390 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24391 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24392 inhibit_free_realized_faces = 0;
24393
24394 #if GLYPH_DEBUG
24395 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24396 doc: /* Inhibit try_window_id display optimization. */);
24397 inhibit_try_window_id = 0;
24398
24399 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24400 doc: /* Inhibit try_window_reusing display optimization. */);
24401 inhibit_try_window_reusing = 0;
24402
24403 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24404 doc: /* Inhibit try_cursor_movement display optimization. */);
24405 inhibit_try_cursor_movement = 0;
24406 #endif /* GLYPH_DEBUG */
24407
24408 DEFVAR_INT ("overline-margin", &overline_margin,
24409 doc: /* *Space between overline and text, in pixels.
24410 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24411 margin to the caracter height. */);
24412 overline_margin = 2;
24413 }
24414
24415
24416 /* Initialize this module when Emacs starts. */
24417
24418 void
24419 init_xdisp ()
24420 {
24421 Lisp_Object root_window;
24422 struct window *mini_w;
24423
24424 current_header_line_height = current_mode_line_height = -1;
24425
24426 CHARPOS (this_line_start_pos) = 0;
24427
24428 mini_w = XWINDOW (minibuf_window);
24429 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24430
24431 if (!noninteractive)
24432 {
24433 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24434 int i;
24435
24436 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24437 set_window_height (root_window,
24438 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24439 0);
24440 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24441 set_window_height (minibuf_window, 1, 0);
24442
24443 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24444 mini_w->total_cols = make_number (FRAME_COLS (f));
24445
24446 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24447 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24448 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24449
24450 /* The default ellipsis glyphs `...'. */
24451 for (i = 0; i < 3; ++i)
24452 default_invis_vector[i] = make_number ('.');
24453 }
24454
24455 {
24456 /* Allocate the buffer for frame titles.
24457 Also used for `format-mode-line'. */
24458 int size = 100;
24459 mode_line_noprop_buf = (char *) xmalloc (size);
24460 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24461 mode_line_noprop_ptr = mode_line_noprop_buf;
24462 mode_line_target = MODE_LINE_DISPLAY;
24463 }
24464
24465 help_echo_showing_p = 0;
24466 }
24467
24468
24469 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24470 (do not change this comment) */